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