Branch data Line data Source code
1 : : /*
2 : : * PF_INET6 socket protocol family
3 : : * Linux INET6 implementation
4 : : *
5 : : * Authors:
6 : : * Pedro Roque <roque@di.fc.ul.pt>
7 : : *
8 : : * Adapted from linux/net/ipv4/af_inet.c
9 : : *
10 : : * Fixes:
11 : : * piggy, Karl Knutson : Socket protocol table
12 : : * Hideaki YOSHIFUJI : sin6_scope_id support
13 : : * Arnaldo Melo : check proc_net_create return, cleanups
14 : : *
15 : : * This program is free software; you can redistribute it and/or
16 : : * modify it under the terms of the GNU General Public License
17 : : * as published by the Free Software Foundation; either version
18 : : * 2 of the License, or (at your option) any later version.
19 : : */
20 : :
21 : : #define pr_fmt(fmt) "IPv6: " fmt
22 : :
23 : : #include <linux/module.h>
24 : : #include <linux/capability.h>
25 : : #include <linux/errno.h>
26 : : #include <linux/types.h>
27 : : #include <linux/socket.h>
28 : : #include <linux/in.h>
29 : : #include <linux/kernel.h>
30 : : #include <linux/timer.h>
31 : : #include <linux/string.h>
32 : : #include <linux/sockios.h>
33 : : #include <linux/net.h>
34 : : #include <linux/fcntl.h>
35 : : #include <linux/mm.h>
36 : : #include <linux/interrupt.h>
37 : : #include <linux/proc_fs.h>
38 : : #include <linux/stat.h>
39 : : #include <linux/init.h>
40 : : #include <linux/slab.h>
41 : :
42 : : #include <linux/inet.h>
43 : : #include <linux/netdevice.h>
44 : : #include <linux/icmpv6.h>
45 : : #include <linux/netfilter_ipv6.h>
46 : :
47 : : #include <net/ip.h>
48 : : #include <net/ipv6.h>
49 : : #include <net/udp.h>
50 : : #include <net/udplite.h>
51 : : #include <net/tcp.h>
52 : : #include <net/ping.h>
53 : : #include <net/protocol.h>
54 : : #include <net/inet_common.h>
55 : : #include <net/route.h>
56 : : #include <net/transp_v6.h>
57 : : #include <net/ip6_route.h>
58 : : #include <net/addrconf.h>
59 : : #include <net/ndisc.h>
60 : : #ifdef CONFIG_IPV6_TUNNEL
61 : : #include <net/ip6_tunnel.h>
62 : : #endif
63 : :
64 : : #include <asm/uaccess.h>
65 : : #include <linux/mroute6.h>
66 : :
67 : : #ifdef CONFIG_ANDROID_PARANOID_NETWORK
68 : : #include <linux/android_aid.h>
69 : :
70 : : static inline int current_has_network(void)
71 : : {
72 : : return in_egroup_p(AID_INET) || capable(CAP_NET_RAW);
73 : : }
74 : : #else
75 : : static inline int current_has_network(void)
76 : : {
77 : : return 1;
78 : : }
79 : : #endif
80 : :
81 : : MODULE_AUTHOR("Cast of dozens");
82 : : MODULE_DESCRIPTION("IPv6 protocol stack for Linux");
83 : : MODULE_LICENSE("GPL");
84 : :
85 : : /* The inetsw6 table contains everything that inet6_create needs to
86 : : * build a new socket.
87 : : */
88 : : static struct list_head inetsw6[SOCK_MAX];
89 : : static DEFINE_SPINLOCK(inetsw6_lock);
90 : :
91 : : struct ipv6_params ipv6_defaults = {
92 : : .disable_ipv6 = 0,
93 : : .autoconf = 1,
94 : : };
95 : :
96 : : static int disable_ipv6_mod;
97 : :
98 : : module_param_named(disable, disable_ipv6_mod, int, 0444);
99 : : MODULE_PARM_DESC(disable, "Disable IPv6 module such that it is non-functional");
100 : :
101 : : module_param_named(disable_ipv6, ipv6_defaults.disable_ipv6, int, 0444);
102 : : MODULE_PARM_DESC(disable_ipv6, "Disable IPv6 on all interfaces");
103 : :
104 : : module_param_named(autoconf, ipv6_defaults.autoconf, int, 0444);
105 : : MODULE_PARM_DESC(autoconf, "Enable IPv6 address autoconfiguration on all interfaces");
106 : :
107 : : static __inline__ struct ipv6_pinfo *inet6_sk_generic(struct sock *sk)
108 : : {
109 : 0 : const int offset = sk->sk_prot->obj_size - sizeof(struct ipv6_pinfo);
110 : :
111 : 0 : return (struct ipv6_pinfo *)(((u8 *)sk) + offset);
112 : : }
113 : :
114 : 0 : static int inet6_create(struct net *net, struct socket *sock, int protocol,
115 : : int kern)
116 : : {
117 : : struct inet_sock *inet;
118 : : struct ipv6_pinfo *np;
119 : : struct sock *sk;
120 : : struct inet_protosw *answer;
121 : : struct proto *answer_prot;
122 : : unsigned char answer_flags;
123 : : char answer_no_check;
124 : : int try_loading_module = 0;
125 : : int err;
126 : :
127 : : if (!current_has_network())
128 : : return -EACCES;
129 : :
130 : : /* Look for the requested type/protocol pair. */
131 : : lookup_protocol:
132 : : err = -ESOCKTNOSUPPORT;
133 : : rcu_read_lock();
134 [ # # ]: 0 : list_for_each_entry_rcu(answer, &inetsw6[sock->type], list) {
135 : :
136 : : err = 0;
137 : : /* Check the non-wild match. */
138 [ # # ]: 0 : if (protocol == answer->protocol) {
139 [ # # ]: 0 : if (protocol != IPPROTO_IP)
140 : : break;
141 : : } else {
142 : : /* Check for the two wild cases. */
143 [ # # ]: 0 : if (IPPROTO_IP == protocol) {
144 : : protocol = answer->protocol;
145 : : break;
146 : : }
147 [ # # ]: 0 : if (IPPROTO_IP == answer->protocol)
148 : : break;
149 : : }
150 : : err = -EPROTONOSUPPORT;
151 : : }
152 : :
153 [ # # ]: 0 : if (err) {
154 [ # # ]: 0 : if (try_loading_module < 2) {
155 : : rcu_read_unlock();
156 : : /*
157 : : * Be more specific, e.g. net-pf-10-proto-132-type-1
158 : : * (net-pf-PF_INET6-proto-IPPROTO_SCTP-type-SOCK_STREAM)
159 : : */
160 [ # # ]: 0 : if (++try_loading_module == 1)
161 : 0 : request_module("net-pf-%d-proto-%d-type-%d",
162 : : PF_INET6, protocol, sock->type);
163 : : /*
164 : : * Fall back to generic, e.g. net-pf-10-proto-132
165 : : * (net-pf-PF_INET6-proto-IPPROTO_SCTP)
166 : : */
167 : : else
168 : 0 : request_module("net-pf-%d-proto-%d",
169 : : PF_INET6, protocol);
170 : : goto lookup_protocol;
171 : : } else
172 : : goto out_rcu_unlock;
173 : : }
174 : :
175 : : err = -EPERM;
176 [ # # ][ # # ]: 0 : if (sock->type == SOCK_RAW && !kern && !capable(CAP_NET_RAW))
[ # # ]
177 : : goto out_rcu_unlock;
178 : :
179 : 0 : sock->ops = answer->ops;
180 : 0 : answer_prot = answer->prot;
181 : 0 : answer_no_check = answer->no_check;
182 : 0 : answer_flags = answer->flags;
183 : : rcu_read_unlock();
184 : :
185 [ # # ]: 0 : WARN_ON(answer_prot->slab == NULL);
186 : :
187 : : err = -ENOBUFS;
188 : 0 : sk = sk_alloc(net, PF_INET6, GFP_KERNEL, answer_prot);
189 [ # # ]: 0 : if (sk == NULL)
190 : : goto out;
191 : :
192 : 0 : sock_init_data(sock, sk);
193 : :
194 : : err = 0;
195 : 0 : sk->sk_no_check = answer_no_check;
196 [ # # ]: 0 : if (INET_PROTOSW_REUSE & answer_flags)
197 : 0 : sk->sk_reuse = SK_CAN_REUSE;
198 : :
199 : : inet = inet_sk(sk);
200 : 0 : inet->is_icsk = (INET_PROTOSW_ICSK & answer_flags) != 0;
201 : :
202 [ # # ]: 0 : if (SOCK_RAW == sock->type) {
203 : 0 : inet->inet_num = protocol;
204 [ # # ]: 0 : if (IPPROTO_RAW == protocol)
205 : 0 : inet->hdrincl = 1;
206 : : }
207 : :
208 : 0 : sk->sk_destruct = inet_sock_destruct;
209 : 0 : sk->sk_family = PF_INET6;
210 : 0 : sk->sk_protocol = protocol;
211 : :
212 : 0 : sk->sk_backlog_rcv = answer->prot->backlog_rcv;
213 : :
214 : 0 : inet_sk(sk)->pinet6 = np = inet6_sk_generic(sk);
215 : 0 : np->hop_limit = -1;
216 : 0 : np->mcast_hops = IPV6_DEFAULT_MCASTHOPS;
217 : 0 : np->mc_loop = 1;
218 : 0 : np->pmtudisc = IPV6_PMTUDISC_WANT;
219 : 0 : np->ipv6only = net->ipv6.sysctl.bindv6only;
220 : :
221 : : /* Init the ipv4 part of the socket since we can have sockets
222 : : * using v6 API for ipv4.
223 : : */
224 : 0 : inet->uc_ttl = -1;
225 : :
226 : 0 : inet->mc_loop = 1;
227 : 0 : inet->mc_ttl = 1;
228 : 0 : inet->mc_index = 0;
229 : 0 : inet->mc_list = NULL;
230 : 0 : inet->rcv_tos = 0;
231 : :
232 [ # # ]: 0 : if (net->ipv4.sysctl_ip_no_pmtu_disc)
233 : 0 : inet->pmtudisc = IP_PMTUDISC_DONT;
234 : : else
235 : 0 : inet->pmtudisc = IP_PMTUDISC_WANT;
236 : : /*
237 : : * Increment only the relevant sk_prot->socks debug field, this changes
238 : : * the previous behaviour of incrementing both the equivalent to
239 : : * answer->prot->socks (inet6_sock_nr) and inet_sock_nr.
240 : : *
241 : : * This allows better debug granularity as we'll know exactly how many
242 : : * UDPv6, TCPv6, etc socks were allocated, not the sum of all IPv6
243 : : * transport protocol socks. -acme
244 : : */
245 : : sk_refcnt_debug_inc(sk);
246 : :
247 [ # # ]: 0 : if (inet->inet_num) {
248 : : /* It assumes that any protocol which allows
249 : : * the user to assign a number at socket
250 : : * creation time automatically shares.
251 : : */
252 [ # # ]: 0 : inet->inet_sport = htons(inet->inet_num);
253 : 0 : sk->sk_prot->hash(sk);
254 : : }
255 [ # # ]: 0 : if (sk->sk_prot->init) {
256 : 0 : err = sk->sk_prot->init(sk);
257 [ # # ]: 0 : if (err) {
258 : 0 : sk_common_release(sk);
259 : 0 : goto out;
260 : : }
261 : : }
262 : : out:
263 : : return err;
264 : : out_rcu_unlock:
265 : : rcu_read_unlock();
266 : : goto out;
267 : : }
268 : :
269 : :
270 : : /* bind for INET6 API */
271 : 0 : int inet6_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
272 : : {
273 : : struct sockaddr_in6 *addr = (struct sockaddr_in6 *)uaddr;
274 : 0 : struct sock *sk = sock->sk;
275 : : struct inet_sock *inet = inet_sk(sk);
276 : : struct ipv6_pinfo *np = inet6_sk(sk);
277 : : struct net *net = sock_net(sk);
278 : : __be32 v4addr = 0;
279 : : unsigned short snum;
280 : : int addr_type = 0;
281 : : int err = 0;
282 : :
283 : : /* If the socket has its own bind function then use it. */
284 [ # # ]: 0 : if (sk->sk_prot->bind)
285 : 0 : return sk->sk_prot->bind(sk, uaddr, addr_len);
286 : :
287 [ # # ]: 0 : if (addr_len < SIN6_LEN_RFC2133)
288 : : return -EINVAL;
289 : :
290 [ # # ]: 0 : if (addr->sin6_family != AF_INET6)
291 : : return -EAFNOSUPPORT;
292 : :
293 : 0 : addr_type = ipv6_addr_type(&addr->sin6_addr);
294 [ # # ][ # # ]: 0 : if ((addr_type & IPV6_ADDR_MULTICAST) && sock->type == SOCK_STREAM)
295 : : return -EINVAL;
296 : :
297 [ # # ]: 0 : snum = ntohs(addr->sin6_port);
298 [ # # ][ # # ]: 0 : if (snum && snum < PROT_SOCK && !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
299 : : return -EACCES;
300 : :
301 : : lock_sock(sk);
302 : :
303 : : /* Check these errors (active socket, double bind). */
304 [ # # ][ # # ]: 0 : if (sk->sk_state != TCP_CLOSE || inet->inet_num) {
305 : : err = -EINVAL;
306 : : goto out;
307 : : }
308 : :
309 : : /* Check if the address belongs to the host. */
310 [ # # ]: 0 : if (addr_type == IPV6_ADDR_MAPPED) {
311 : : int chk_addr_ret;
312 : :
313 : : /* Binding to v4-mapped address on a v6-only socket
314 : : * makes no sense
315 : : */
316 [ # # ]: 0 : if (np->ipv6only) {
317 : : err = -EINVAL;
318 : : goto out;
319 : : }
320 : :
321 : : /* Reproduce AF_INET checks to make the bindings consistent */
322 : 0 : v4addr = addr->sin6_addr.s6_addr32[3];
323 : 0 : chk_addr_ret = inet_addr_type(net, v4addr);
324 [ # # ][ # # ]: 0 : if (!sysctl_ip_nonlocal_bind &&
325 : 0 : !(inet->freebind || inet->transparent) &&
326 [ # # ]: 0 : v4addr != htonl(INADDR_ANY) &&
327 : 0 : chk_addr_ret != RTN_LOCAL &&
328 [ # # ]: 0 : chk_addr_ret != RTN_MULTICAST &&
329 : 0 : chk_addr_ret != RTN_BROADCAST) {
330 : : err = -EADDRNOTAVAIL;
331 : : goto out;
332 : : }
333 : : } else {
334 [ # # ]: 0 : if (addr_type != IPV6_ADDR_ANY) {
335 : : struct net_device *dev = NULL;
336 : :
337 : : rcu_read_lock();
338 [ # # ]: 0 : if (__ipv6_addr_needs_scope_id(addr_type)) {
339 [ # # ][ # # ]: 0 : if (addr_len >= sizeof(struct sockaddr_in6) &&
340 : 0 : addr->sin6_scope_id) {
341 : : /* Override any existing binding, if another one
342 : : * is supplied by user.
343 : : */
344 : 0 : sk->sk_bound_dev_if = addr->sin6_scope_id;
345 : : }
346 : :
347 : : /* Binding to link-local address requires an interface */
348 [ # # ]: 0 : if (!sk->sk_bound_dev_if) {
349 : : err = -EINVAL;
350 : : goto out_unlock;
351 : : }
352 : 0 : dev = dev_get_by_index_rcu(net, sk->sk_bound_dev_if);
353 [ # # ]: 0 : if (!dev) {
354 : : err = -ENODEV;
355 : : goto out_unlock;
356 : : }
357 : : }
358 : :
359 : : /* ipv4 addr of the socket is invalid. Only the
360 : : * unspecified and mapped address have a v4 equivalent.
361 : : */
362 : : v4addr = LOOPBACK4_IPV6;
363 [ # # ]: 0 : if (!(addr_type & IPV6_ADDR_MULTICAST)) {
364 [ # # # # ]: 0 : if (!(inet->freebind || inet->transparent) &&
365 : 0 : !ipv6_chk_addr(net, &addr->sin6_addr,
366 : : dev, 0)) {
367 : : err = -EADDRNOTAVAIL;
368 : : goto out_unlock;
369 : : }
370 : : }
371 : : rcu_read_unlock();
372 : : }
373 : : }
374 : :
375 : 0 : inet->inet_rcv_saddr = v4addr;
376 : 0 : inet->inet_saddr = v4addr;
377 : :
378 : 0 : sk->sk_v6_rcv_saddr = addr->sin6_addr;
379 : :
380 [ # # ]: 0 : if (!(addr_type & IPV6_ADDR_MULTICAST))
381 : 0 : np->saddr = addr->sin6_addr;
382 : :
383 : : /* Make sure we are allowed to bind here. */
384 [ # # ]: 0 : if (sk->sk_prot->get_port(sk, snum)) {
385 : : inet_reset_saddr(sk);
386 : : err = -EADDRINUSE;
387 : : goto out;
388 : : }
389 : :
390 [ # # ]: 0 : if (addr_type != IPV6_ADDR_ANY) {
391 : 0 : sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
392 [ # # ]: 0 : if (addr_type != IPV6_ADDR_MAPPED)
393 : 0 : np->ipv6only = 1;
394 : : }
395 [ # # ]: 0 : if (snum)
396 : 0 : sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
397 [ # # ]: 0 : inet->inet_sport = htons(inet->inet_num);
398 : 0 : inet->inet_dport = 0;
399 : 0 : inet->inet_daddr = 0;
400 : : out:
401 : 0 : release_sock(sk);
402 : 0 : return err;
403 : : out_unlock:
404 : : rcu_read_unlock();
405 : : goto out;
406 : : }
407 : : EXPORT_SYMBOL(inet6_bind);
408 : :
409 : 0 : int inet6_release(struct socket *sock)
410 : : {
411 : 0 : struct sock *sk = sock->sk;
412 : :
413 [ # # ]: 0 : if (sk == NULL)
414 : : return -EINVAL;
415 : :
416 : : /* Free mc lists */
417 : 0 : ipv6_sock_mc_close(sk);
418 : :
419 : : /* Free ac lists */
420 : 0 : ipv6_sock_ac_close(sk);
421 : :
422 : 0 : return inet_release(sock);
423 : : }
424 : : EXPORT_SYMBOL(inet6_release);
425 : :
426 : 0 : void inet6_destroy_sock(struct sock *sk)
427 : : {
428 : : struct ipv6_pinfo *np = inet6_sk(sk);
429 : : struct sk_buff *skb;
430 : : struct ipv6_txoptions *opt;
431 : :
432 : : /* Release rx options */
433 : :
434 : 0 : skb = xchg(&np->pktoptions, NULL);
435 [ # # ]: 0 : if (skb != NULL)
436 : 0 : kfree_skb(skb);
437 : :
438 : 0 : skb = xchg(&np->rxpmtu, NULL);
439 [ # # ]: 0 : if (skb != NULL)
440 : 0 : kfree_skb(skb);
441 : :
442 : : /* Free flowlabels */
443 : 0 : fl6_free_socklist(sk);
444 : :
445 : : /* Free tx options */
446 : :
447 : 0 : opt = xchg(&np->opt, NULL);
448 [ # # ]: 0 : if (opt != NULL)
449 : 0 : sock_kfree_s(sk, opt, opt->tot_len);
450 : 0 : }
451 : : EXPORT_SYMBOL_GPL(inet6_destroy_sock);
452 : :
453 : : /*
454 : : * This does both peername and sockname.
455 : : */
456 : :
457 : 0 : int inet6_getname(struct socket *sock, struct sockaddr *uaddr,
458 : : int *uaddr_len, int peer)
459 : : {
460 : : struct sockaddr_in6 *sin = (struct sockaddr_in6 *)uaddr;
461 : 0 : struct sock *sk = sock->sk;
462 : : struct inet_sock *inet = inet_sk(sk);
463 : : struct ipv6_pinfo *np = inet6_sk(sk);
464 : :
465 : 0 : sin->sin6_family = AF_INET6;
466 : 0 : sin->sin6_flowinfo = 0;
467 : 0 : sin->sin6_scope_id = 0;
468 [ # # ]: 0 : if (peer) {
469 [ # # ]: 0 : if (!inet->inet_dport)
470 : : return -ENOTCONN;
471 [ # # ][ # # ]: 0 : if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
472 : : peer == 1)
473 : : return -ENOTCONN;
474 : 0 : sin->sin6_port = inet->inet_dport;
475 : 0 : sin->sin6_addr = sk->sk_v6_daddr;
476 [ # # ]: 0 : if (np->sndflow)
477 : 0 : sin->sin6_flowinfo = np->flow_label;
478 : : } else {
479 [ # # ]: 0 : if (ipv6_addr_any(&sk->sk_v6_rcv_saddr))
480 : 0 : sin->sin6_addr = np->saddr;
481 : : else
482 : 0 : sin->sin6_addr = sk->sk_v6_rcv_saddr;
483 : :
484 : 0 : sin->sin6_port = inet->inet_sport;
485 : : }
486 : 0 : sin->sin6_scope_id = ipv6_iface_scope_id(&sin->sin6_addr,
487 : : sk->sk_bound_dev_if);
488 : 0 : *uaddr_len = sizeof(*sin);
489 : 0 : return 0;
490 : : }
491 : : EXPORT_SYMBOL(inet6_getname);
492 : :
493 : 0 : int inet6_killaddr_ioctl(struct net *net, void __user *arg) {
494 : : struct in6_ifreq ireq;
495 : : struct sockaddr_in6 sin6;
496 : :
497 [ # # ]: 0 : if (!capable(CAP_NET_ADMIN))
498 : : return -EACCES;
499 : :
500 [ # # ]: 0 : if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
501 : : return -EFAULT;
502 : :
503 : 0 : sin6.sin6_family = AF_INET6;
504 : 0 : sin6.sin6_addr = ireq.ifr6_addr;
505 : 0 : return tcp_nuke_addr(net, (struct sockaddr *) &sin6);
506 : : }
507 : :
508 : 0 : int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
509 : : {
510 : 0 : struct sock *sk = sock->sk;
511 : : struct net *net = sock_net(sk);
512 : :
513 [ # # # # : 0 : switch (cmd) {
# # # # ]
514 : : case SIOCGSTAMP:
515 : 0 : return sock_get_timestamp(sk, (struct timeval __user *)arg);
516 : :
517 : : case SIOCGSTAMPNS:
518 : 0 : return sock_get_timestampns(sk, (struct timespec __user *)arg);
519 : :
520 : : case SIOCADDRT:
521 : : case SIOCDELRT:
522 : :
523 : 0 : return ipv6_route_ioctl(net, cmd, (void __user *)arg);
524 : :
525 : : case SIOCSIFADDR:
526 : 0 : return addrconf_add_ifaddr(net, (void __user *) arg);
527 : : case SIOCDIFADDR:
528 : 0 : return addrconf_del_ifaddr(net, (void __user *) arg);
529 : : case SIOCSIFDSTADDR:
530 : 0 : return addrconf_set_dstaddr(net, (void __user *) arg);
531 : : case SIOCKILLADDR:
532 : 0 : return inet6_killaddr_ioctl(net, (void __user *) arg);
533 : : default:
534 [ # # ]: 0 : if (!sk->sk_prot->ioctl)
535 : : return -ENOIOCTLCMD;
536 : 0 : return sk->sk_prot->ioctl(sk, cmd, arg);
537 : : }
538 : : /*NOTREACHED*/
539 : : return 0;
540 : : }
541 : : EXPORT_SYMBOL(inet6_ioctl);
542 : :
543 : : const struct proto_ops inet6_stream_ops = {
544 : : .family = PF_INET6,
545 : : .owner = THIS_MODULE,
546 : : .release = inet6_release,
547 : : .bind = inet6_bind,
548 : : .connect = inet_stream_connect, /* ok */
549 : : .socketpair = sock_no_socketpair, /* a do nothing */
550 : : .accept = inet_accept, /* ok */
551 : : .getname = inet6_getname,
552 : : .poll = tcp_poll, /* ok */
553 : : .ioctl = inet6_ioctl, /* must change */
554 : : .listen = inet_listen, /* ok */
555 : : .shutdown = inet_shutdown, /* ok */
556 : : .setsockopt = sock_common_setsockopt, /* ok */
557 : : .getsockopt = sock_common_getsockopt, /* ok */
558 : : .sendmsg = inet_sendmsg, /* ok */
559 : : .recvmsg = inet_recvmsg, /* ok */
560 : : .mmap = sock_no_mmap,
561 : : .sendpage = inet_sendpage,
562 : : .splice_read = tcp_splice_read,
563 : : #ifdef CONFIG_COMPAT
564 : : .compat_setsockopt = compat_sock_common_setsockopt,
565 : : .compat_getsockopt = compat_sock_common_getsockopt,
566 : : #endif
567 : : };
568 : :
569 : : const struct proto_ops inet6_dgram_ops = {
570 : : .family = PF_INET6,
571 : : .owner = THIS_MODULE,
572 : : .release = inet6_release,
573 : : .bind = inet6_bind,
574 : : .connect = inet_dgram_connect, /* ok */
575 : : .socketpair = sock_no_socketpair, /* a do nothing */
576 : : .accept = sock_no_accept, /* a do nothing */
577 : : .getname = inet6_getname,
578 : : .poll = udp_poll, /* ok */
579 : : .ioctl = inet6_ioctl, /* must change */
580 : : .listen = sock_no_listen, /* ok */
581 : : .shutdown = inet_shutdown, /* ok */
582 : : .setsockopt = sock_common_setsockopt, /* ok */
583 : : .getsockopt = sock_common_getsockopt, /* ok */
584 : : .sendmsg = inet_sendmsg, /* ok */
585 : : .recvmsg = inet_recvmsg, /* ok */
586 : : .mmap = sock_no_mmap,
587 : : .sendpage = sock_no_sendpage,
588 : : #ifdef CONFIG_COMPAT
589 : : .compat_setsockopt = compat_sock_common_setsockopt,
590 : : .compat_getsockopt = compat_sock_common_getsockopt,
591 : : #endif
592 : : };
593 : :
594 : : static const struct net_proto_family inet6_family_ops = {
595 : : .family = PF_INET6,
596 : : .create = inet6_create,
597 : : .owner = THIS_MODULE,
598 : : };
599 : :
600 : 0 : int inet6_register_protosw(struct inet_protosw *p)
601 : : {
602 : : struct list_head *lh;
603 : : struct inet_protosw *answer;
604 : : struct list_head *last_perm;
605 : 0 : int protocol = p->protocol;
606 : : int ret;
607 : :
608 : : spin_lock_bh(&inetsw6_lock);
609 : :
610 : : ret = -EINVAL;
611 [ # # ]: 0 : if (p->type >= SOCK_MAX)
612 : : goto out_illegal;
613 : :
614 : : /* If we are trying to override a permanent protocol, bail. */
615 : : answer = NULL;
616 : : ret = -EPERM;
617 : 0 : last_perm = &inetsw6[p->type];
618 [ # # ]: 0 : list_for_each(lh, &inetsw6[p->type]) {
619 : : answer = list_entry(lh, struct inet_protosw, list);
620 : :
621 : : /* Check only the non-wild match. */
622 [ # # ]: 0 : if (INET_PROTOSW_PERMANENT & answer->flags) {
623 [ # # ]: 0 : if (protocol == answer->protocol)
624 : : break;
625 : : last_perm = lh;
626 : : }
627 : :
628 : : answer = NULL;
629 : : }
630 [ # # ]: 0 : if (answer)
631 : : goto out_permanent;
632 : :
633 : : /* Add the new entry after the last permanent entry if any, so that
634 : : * the new entry does not override a permanent entry when matched with
635 : : * a wild-card protocol. But it is allowed to override any existing
636 : : * non-permanent entry. This means that when we remove this entry, the
637 : : * system automatically returns to the old behavior.
638 : : */
639 : 0 : list_add_rcu(&p->list, last_perm);
640 : : ret = 0;
641 : : out:
642 : : spin_unlock_bh(&inetsw6_lock);
643 : 0 : return ret;
644 : :
645 : : out_permanent:
646 : 0 : pr_err("Attempt to override permanent protocol %d\n", protocol);
647 : 0 : goto out;
648 : :
649 : : out_illegal:
650 : 0 : pr_err("Ignoring attempt to register invalid socket type %d\n",
651 : : p->type);
652 : 0 : goto out;
653 : : }
654 : : EXPORT_SYMBOL(inet6_register_protosw);
655 : :
656 : : void
657 : 0 : inet6_unregister_protosw(struct inet_protosw *p)
658 : : {
659 [ # # ]: 0 : if (INET_PROTOSW_PERMANENT & p->flags) {
660 : 0 : pr_err("Attempt to unregister permanent protocol %d\n",
661 : : p->protocol);
662 : : } else {
663 : : spin_lock_bh(&inetsw6_lock);
664 : : list_del_rcu(&p->list);
665 : : spin_unlock_bh(&inetsw6_lock);
666 : :
667 : 0 : synchronize_net();
668 : : }
669 : 0 : }
670 : : EXPORT_SYMBOL(inet6_unregister_protosw);
671 : :
672 : 0 : int inet6_sk_rebuild_header(struct sock *sk)
673 : : {
674 : : struct ipv6_pinfo *np = inet6_sk(sk);
675 : : struct dst_entry *dst;
676 : :
677 : 0 : dst = __sk_dst_check(sk, np->dst_cookie);
678 : :
679 [ # # ]: 0 : if (dst == NULL) {
680 : : struct inet_sock *inet = inet_sk(sk);
681 : : struct in6_addr *final_p, final;
682 : : struct flowi6 fl6;
683 : :
684 : 0 : memset(&fl6, 0, sizeof(fl6));
685 : 0 : fl6.flowi6_proto = sk->sk_protocol;
686 : 0 : fl6.daddr = sk->sk_v6_daddr;
687 : 0 : fl6.saddr = np->saddr;
688 : 0 : fl6.flowlabel = np->flow_label;
689 : 0 : fl6.flowi6_oif = sk->sk_bound_dev_if;
690 : 0 : fl6.flowi6_mark = sk->sk_mark;
691 : 0 : fl6.fl6_dport = inet->inet_dport;
692 : 0 : fl6.fl6_sport = inet->inet_sport;
693 : 0 : security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
694 : :
695 : 0 : final_p = fl6_update_dst(&fl6, np->opt, &final);
696 : :
697 : 0 : dst = ip6_dst_lookup_flow(sk, &fl6, final_p);
698 [ # # ]: 0 : if (IS_ERR(dst)) {
699 : 0 : sk->sk_route_caps = 0;
700 : 0 : sk->sk_err_soft = -PTR_ERR(dst);
701 : 0 : return PTR_ERR(dst);
702 : : }
703 : :
704 : : __ip6_dst_store(sk, dst, NULL, NULL);
705 : : }
706 : :
707 : : return 0;
708 : : }
709 : : EXPORT_SYMBOL_GPL(inet6_sk_rebuild_header);
710 : :
711 : 0 : bool ipv6_opt_accepted(const struct sock *sk, const struct sk_buff *skb)
712 : : {
713 : : const struct ipv6_pinfo *np = inet6_sk(sk);
714 : : const struct inet6_skb_parm *opt = IP6CB(skb);
715 : :
716 [ # # ]: 0 : if (np->rxopt.all) {
717 [ # # ][ # # ]: 0 : if ((opt->hop && (np->rxopt.bits.hopopts ||
718 [ # # ]: 0 : np->rxopt.bits.ohopopts)) ||
719 [ # # ]: 0 : (ip6_flowinfo((struct ipv6hdr *) skb_network_header(skb)) &&
720 [ # # ]: 0 : np->rxopt.bits.rxflow) ||
721 [ # # ]: 0 : (opt->srcrt && (np->rxopt.bits.srcrt ||
722 [ # # ]: 0 : np->rxopt.bits.osrcrt)) ||
723 [ # # ][ # # ]: 0 : ((opt->dst1 || opt->dst0) &&
724 : 0 : (np->rxopt.bits.dstopts || np->rxopt.bits.odstopts)))
725 : : return true;
726 : : }
727 : 0 : return false;
728 : : }
729 : : EXPORT_SYMBOL_GPL(ipv6_opt_accepted);
730 : :
731 : : static struct packet_type ipv6_packet_type __read_mostly = {
732 : : .type = cpu_to_be16(ETH_P_IPV6),
733 : : .func = ipv6_rcv,
734 : : };
735 : :
736 : : static int __init ipv6_packet_init(void)
737 : : {
738 : 0 : dev_add_pack(&ipv6_packet_type);
739 : : return 0;
740 : : }
741 : :
742 : : static void ipv6_packet_cleanup(void)
743 : : {
744 : 0 : dev_remove_pack(&ipv6_packet_type);
745 : : }
746 : :
747 : 0 : static int __net_init ipv6_init_mibs(struct net *net)
748 : : {
749 : : int i;
750 : :
751 [ # # ]: 0 : if (snmp_mib_init((void __percpu **)net->mib.udp_stats_in6,
752 : : sizeof(struct udp_mib),
753 : : __alignof__(struct udp_mib)) < 0)
754 : : return -ENOMEM;
755 [ # # ]: 0 : if (snmp_mib_init((void __percpu **)net->mib.udplite_stats_in6,
756 : : sizeof(struct udp_mib),
757 : : __alignof__(struct udp_mib)) < 0)
758 : : goto err_udplite_mib;
759 [ # # ]: 0 : if (snmp_mib_init((void __percpu **)net->mib.ipv6_statistics,
760 : : sizeof(struct ipstats_mib),
761 : : __alignof__(struct ipstats_mib)) < 0)
762 : : goto err_ip_mib;
763 : :
764 [ # # ]: 0 : for_each_possible_cpu(i) {
765 : : struct ipstats_mib *af_inet6_stats;
766 : 0 : af_inet6_stats = per_cpu_ptr(net->mib.ipv6_statistics[0], i);
767 : : u64_stats_init(&af_inet6_stats->syncp);
768 : : #if SNMP_ARRAY_SZ == 2
769 : : af_inet6_stats = per_cpu_ptr(net->mib.ipv6_statistics[1], i);
770 : : u64_stats_init(&af_inet6_stats->syncp);
771 : : #endif
772 : : }
773 : :
774 : :
775 [ # # ]: 0 : if (snmp_mib_init((void __percpu **)net->mib.icmpv6_statistics,
776 : : sizeof(struct icmpv6_mib),
777 : : __alignof__(struct icmpv6_mib)) < 0)
778 : : goto err_icmp_mib;
779 : 0 : net->mib.icmpv6msg_statistics = kzalloc(sizeof(struct icmpv6msg_mib),
780 : : GFP_KERNEL);
781 [ # # ]: 0 : if (!net->mib.icmpv6msg_statistics)
782 : : goto err_icmpmsg_mib;
783 : : return 0;
784 : :
785 : : err_icmpmsg_mib:
786 : : snmp_mib_free((void __percpu **)net->mib.icmpv6_statistics);
787 : : err_icmp_mib:
788 : : snmp_mib_free((void __percpu **)net->mib.ipv6_statistics);
789 : : err_ip_mib:
790 : : snmp_mib_free((void __percpu **)net->mib.udplite_stats_in6);
791 : : err_udplite_mib:
792 : : snmp_mib_free((void __percpu **)net->mib.udp_stats_in6);
793 : : return -ENOMEM;
794 : : }
795 : :
796 : 0 : static void ipv6_cleanup_mibs(struct net *net)
797 : : {
798 : 0 : snmp_mib_free((void __percpu **)net->mib.udp_stats_in6);
799 : 0 : snmp_mib_free((void __percpu **)net->mib.udplite_stats_in6);
800 : 0 : snmp_mib_free((void __percpu **)net->mib.ipv6_statistics);
801 : 0 : snmp_mib_free((void __percpu **)net->mib.icmpv6_statistics);
802 : 0 : kfree(net->mib.icmpv6msg_statistics);
803 : 0 : }
804 : :
805 : 0 : static int __net_init inet6_net_init(struct net *net)
806 : : {
807 : : int err = 0;
808 : :
809 : 0 : net->ipv6.sysctl.bindv6only = 0;
810 : 0 : net->ipv6.sysctl.icmpv6_time = 1*HZ;
811 : 0 : net->ipv6.sysctl.flowlabel_consistency = 1;
812 : 0 : atomic_set(&net->ipv6.rt_genid, 0);
813 : :
814 : 0 : err = ipv6_init_mibs(net);
815 [ # # ]: 0 : if (err)
816 : : return err;
817 : : #ifdef CONFIG_PROC_FS
818 : 0 : err = udp6_proc_init(net);
819 [ # # ]: 0 : if (err)
820 : : goto out;
821 : 0 : err = tcp6_proc_init(net);
822 [ # # ]: 0 : if (err)
823 : : goto proc_tcp6_fail;
824 : 0 : err = ac6_proc_init(net);
825 [ # # ]: 0 : if (err)
826 : : goto proc_ac6_fail;
827 : : #endif
828 : : return err;
829 : :
830 : : #ifdef CONFIG_PROC_FS
831 : : proc_ac6_fail:
832 : 0 : tcp6_proc_exit(net);
833 : : proc_tcp6_fail:
834 : 0 : udp6_proc_exit(net);
835 : : out:
836 : 0 : ipv6_cleanup_mibs(net);
837 : 0 : return err;
838 : : #endif
839 : : }
840 : :
841 : 0 : static void __net_exit inet6_net_exit(struct net *net)
842 : : {
843 : : #ifdef CONFIG_PROC_FS
844 : 0 : udp6_proc_exit(net);
845 : 0 : tcp6_proc_exit(net);
846 : 0 : ac6_proc_exit(net);
847 : : #endif
848 : 0 : ipv6_cleanup_mibs(net);
849 : 0 : }
850 : :
851 : : static struct pernet_operations inet6_net_ops = {
852 : : .init = inet6_net_init,
853 : : .exit = inet6_net_exit,
854 : : };
855 : :
856 : : static const struct ipv6_stub ipv6_stub_impl = {
857 : : .ipv6_sock_mc_join = ipv6_sock_mc_join,
858 : : .ipv6_sock_mc_drop = ipv6_sock_mc_drop,
859 : : .ipv6_dst_lookup = ip6_dst_lookup,
860 : : .udpv6_encap_enable = udpv6_encap_enable,
861 : : .ndisc_send_na = ndisc_send_na,
862 : : .nd_tbl = &nd_tbl,
863 : : };
864 : :
865 : 0 : static int __init inet6_init(void)
866 : : {
867 : : struct list_head *r;
868 : : int err = 0;
869 : :
870 : : BUILD_BUG_ON(sizeof(struct inet6_skb_parm) > FIELD_SIZEOF(struct sk_buff, cb));
871 : :
872 : : /* Register the socket-side information for inet6_create. */
873 [ # # ]: 0 : for (r = &inetsw6[0]; r < &inetsw6[SOCK_MAX]; ++r)
874 : : INIT_LIST_HEAD(r);
875 : :
876 [ # # ]: 0 : if (disable_ipv6_mod) {
877 : 0 : pr_info("Loaded, but administratively disabled, reboot required to enable\n");
878 : 0 : goto out;
879 : : }
880 : :
881 : 0 : err = proto_register(&tcpv6_prot, 1);
882 [ # # ]: 0 : if (err)
883 : : goto out;
884 : :
885 : 0 : err = proto_register(&udpv6_prot, 1);
886 [ # # ]: 0 : if (err)
887 : : goto out_unregister_tcp_proto;
888 : :
889 : 0 : err = proto_register(&udplitev6_prot, 1);
890 [ # # ]: 0 : if (err)
891 : : goto out_unregister_udp_proto;
892 : :
893 : 0 : err = proto_register(&rawv6_prot, 1);
894 [ # # ]: 0 : if (err)
895 : : goto out_unregister_udplite_proto;
896 : :
897 : 0 : err = proto_register(&pingv6_prot, 1);
898 [ # # ]: 0 : if (err)
899 : : goto out_unregister_ping_proto;
900 : :
901 : : /* We MUST register RAW sockets before we create the ICMP6,
902 : : * IGMP6, or NDISC control sockets.
903 : : */
904 : 0 : err = rawv6_init();
905 [ # # ]: 0 : if (err)
906 : : goto out_unregister_raw_proto;
907 : :
908 : : /* Register the family here so that the init calls below will
909 : : * be able to create sockets. (?? is this dangerous ??)
910 : : */
911 : 0 : err = sock_register(&inet6_family_ops);
912 [ # # ]: 0 : if (err)
913 : : goto out_sock_register_fail;
914 : :
915 : : /*
916 : : * ipngwg API draft makes clear that the correct semantics
917 : : * for TCP and UDP is to consider one TCP and UDP instance
918 : : * in a host available by both INET and INET6 APIs and
919 : : * able to communicate via both network protocols.
920 : : */
921 : :
922 : 0 : err = register_pernet_subsys(&inet6_net_ops);
923 [ # # ]: 0 : if (err)
924 : : goto register_pernet_fail;
925 : 0 : err = icmpv6_init();
926 [ # # ]: 0 : if (err)
927 : : goto icmp_fail;
928 : : err = ip6_mr_init();
929 : : if (err)
930 : : goto ipmr_fail;
931 : 0 : err = ndisc_init();
932 [ # # ]: 0 : if (err)
933 : : goto ndisc_fail;
934 : 0 : err = igmp6_init();
935 [ # # ]: 0 : if (err)
936 : : goto igmp_fail;
937 : :
938 : 0 : ipv6_stub = &ipv6_stub_impl;
939 : :
940 : 0 : err = ipv6_netfilter_init();
941 [ # # ]: 0 : if (err)
942 : : goto netfilter_fail;
943 : : /* Create /proc/foo6 entries. */
944 : : #ifdef CONFIG_PROC_FS
945 : : err = -ENOMEM;
946 [ # # ]: 0 : if (raw6_proc_init())
947 : : goto proc_raw6_fail;
948 [ # # ]: 0 : if (udplite6_proc_init())
949 : : goto proc_udplite6_fail;
950 [ # # ]: 0 : if (ipv6_misc_proc_init())
951 : : goto proc_misc6_fail;
952 [ # # ]: 0 : if (if6_proc_init())
953 : : goto proc_if6_fail;
954 : : #endif
955 : 0 : err = ip6_route_init();
956 [ # # ]: 0 : if (err)
957 : : goto ip6_route_fail;
958 : 0 : err = ndisc_late_init();
959 [ # # ]: 0 : if (err)
960 : : goto ndisc_late_fail;
961 : 0 : err = ip6_flowlabel_init();
962 [ # # ]: 0 : if (err)
963 : : goto ip6_flowlabel_fail;
964 : 0 : err = addrconf_init();
965 [ # # ]: 0 : if (err)
966 : : goto addrconf_fail;
967 : :
968 : : /* Init v6 extension headers. */
969 : 0 : err = ipv6_exthdrs_init();
970 [ # # ]: 0 : if (err)
971 : : goto ipv6_exthdrs_fail;
972 : :
973 : 0 : err = ipv6_frag_init();
974 [ # # ]: 0 : if (err)
975 : : goto ipv6_frag_fail;
976 : :
977 : : /* Init v6 transport protocols. */
978 : 0 : err = udpv6_init();
979 [ # # ]: 0 : if (err)
980 : : goto udpv6_fail;
981 : :
982 : 0 : err = udplitev6_init();
983 [ # # ]: 0 : if (err)
984 : : goto udplitev6_fail;
985 : :
986 : 0 : err = tcpv6_init();
987 [ # # ]: 0 : if (err)
988 : : goto tcpv6_fail;
989 : :
990 : : err = ipv6_packet_init();
991 : : if (err)
992 : : goto ipv6_packet_fail;
993 : :
994 : 0 : err = pingv6_init();
995 [ # # ]: 0 : if (err)
996 : : goto pingv6_fail;
997 : :
998 : : #ifdef CONFIG_SYSCTL
999 : 0 : err = ipv6_sysctl_register();
1000 [ # # ]: 0 : if (err)
1001 : : goto sysctl_fail;
1002 : : #endif
1003 : : out:
1004 : 0 : return err;
1005 : :
1006 : : #ifdef CONFIG_SYSCTL
1007 : : sysctl_fail:
1008 : 0 : pingv6_exit();
1009 : : #endif
1010 : : pingv6_fail:
1011 : : ipv6_packet_cleanup();
1012 : : ipv6_packet_fail:
1013 : 0 : tcpv6_exit();
1014 : : tcpv6_fail:
1015 : 0 : udplitev6_exit();
1016 : : udplitev6_fail:
1017 : 0 : udpv6_exit();
1018 : : udpv6_fail:
1019 : 0 : ipv6_frag_exit();
1020 : : ipv6_frag_fail:
1021 : 0 : ipv6_exthdrs_exit();
1022 : : ipv6_exthdrs_fail:
1023 : 0 : addrconf_cleanup();
1024 : : addrconf_fail:
1025 : 0 : ip6_flowlabel_cleanup();
1026 : : ip6_flowlabel_fail:
1027 : 0 : ndisc_late_cleanup();
1028 : : ndisc_late_fail:
1029 : 0 : ip6_route_cleanup();
1030 : : ip6_route_fail:
1031 : : #ifdef CONFIG_PROC_FS
1032 : 0 : if6_proc_exit();
1033 : : proc_if6_fail:
1034 : 0 : ipv6_misc_proc_exit();
1035 : : proc_misc6_fail:
1036 : 0 : udplite6_proc_exit();
1037 : : proc_udplite6_fail:
1038 : 0 : raw6_proc_exit();
1039 : : proc_raw6_fail:
1040 : : #endif
1041 : 0 : ipv6_netfilter_fini();
1042 : : netfilter_fail:
1043 : 0 : igmp6_cleanup();
1044 : : igmp_fail:
1045 : 0 : ndisc_cleanup();
1046 : : ndisc_fail:
1047 : : ip6_mr_cleanup();
1048 : : ipmr_fail:
1049 : 0 : icmpv6_cleanup();
1050 : : icmp_fail:
1051 : 0 : unregister_pernet_subsys(&inet6_net_ops);
1052 : : register_pernet_fail:
1053 : 0 : sock_unregister(PF_INET6);
1054 : 0 : rtnl_unregister_all(PF_INET6);
1055 : : out_sock_register_fail:
1056 : 0 : rawv6_exit();
1057 : : out_unregister_ping_proto:
1058 : 0 : proto_unregister(&pingv6_prot);
1059 : : out_unregister_raw_proto:
1060 : 0 : proto_unregister(&rawv6_prot);
1061 : : out_unregister_udplite_proto:
1062 : 0 : proto_unregister(&udplitev6_prot);
1063 : : out_unregister_udp_proto:
1064 : 0 : proto_unregister(&udpv6_prot);
1065 : : out_unregister_tcp_proto:
1066 : 0 : proto_unregister(&tcpv6_prot);
1067 : 0 : goto out;
1068 : : }
1069 : : module_init(inet6_init);
1070 : :
1071 : : MODULE_ALIAS_NETPROTO(PF_INET6);
|