af_inet6.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  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. * $Id: af_inet6.c,v 1.66 2002/02/01 22:01:04 davem Exp $
  11. *
  12. * Fixes:
  13. * piggy, Karl Knutson : Socket protocol table
  14. * Hideaki YOSHIFUJI : sin6_scope_id support
  15. * Arnaldo Melo : check proc_net_create return, cleanups
  16. *
  17. * This program is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU General Public License
  19. * as published by the Free Software Foundation; either version
  20. * 2 of the License, or (at your option) any later version.
  21. */
  22. #include <linux/module.h>
  23. #include <linux/capability.h>
  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>
  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. #include <linux/inet.h>
  40. #include <linux/netdevice.h>
  41. #include <linux/icmpv6.h>
  42. #include <linux/netfilter_ipv6.h>
  43. #include <net/ip.h>
  44. #include <net/ipv6.h>
  45. #include <net/udp.h>
  46. #include <net/udplite.h>
  47. #include <net/tcp.h>
  48. #include <net/ipip.h>
  49. #include <net/protocol.h>
  50. #include <net/inet_common.h>
  51. #include <net/transp_v6.h>
  52. #include <net/ip6_route.h>
  53. #include <net/addrconf.h>
  54. #ifdef CONFIG_IPV6_TUNNEL
  55. #include <net/ip6_tunnel.h>
  56. #endif
  57. #include <asm/uaccess.h>
  58. #include <asm/system.h>
  59. MODULE_AUTHOR("Cast of dozens");
  60. MODULE_DESCRIPTION("IPv6 protocol stack for Linux");
  61. MODULE_LICENSE("GPL");
  62. /* The inetsw6 table contains everything that inet6_create needs to
  63. * build a new socket.
  64. */
  65. static struct list_head inetsw6[SOCK_MAX];
  66. static DEFINE_SPINLOCK(inetsw6_lock);
  67. void ipv6_frag_sysctl_init(struct net *net);
  68. static __inline__ struct ipv6_pinfo *inet6_sk_generic(struct sock *sk)
  69. {
  70. const int offset = sk->sk_prot->obj_size - sizeof(struct ipv6_pinfo);
  71. return (struct ipv6_pinfo *)(((u8 *)sk) + offset);
  72. }
  73. static int inet6_create(struct net *net, struct socket *sock, int protocol)
  74. {
  75. struct inet_sock *inet;
  76. struct ipv6_pinfo *np;
  77. struct sock *sk;
  78. struct list_head *p;
  79. struct inet_protosw *answer;
  80. struct proto *answer_prot;
  81. unsigned char answer_flags;
  82. char answer_no_check;
  83. int try_loading_module = 0;
  84. int err;
  85. if (net != &init_net)
  86. return -EAFNOSUPPORT;
  87. if (sock->type != SOCK_RAW &&
  88. sock->type != SOCK_DGRAM &&
  89. !inet_ehash_secret)
  90. build_ehash_secret();
  91. /* Look for the requested type/protocol pair. */
  92. answer = NULL;
  93. lookup_protocol:
  94. err = -ESOCKTNOSUPPORT;
  95. rcu_read_lock();
  96. list_for_each_rcu(p, &inetsw6[sock->type]) {
  97. answer = list_entry(p, struct inet_protosw, list);
  98. /* Check the non-wild match. */
  99. if (protocol == answer->protocol) {
  100. if (protocol != IPPROTO_IP)
  101. break;
  102. } else {
  103. /* Check for the two wild cases. */
  104. if (IPPROTO_IP == protocol) {
  105. protocol = answer->protocol;
  106. break;
  107. }
  108. if (IPPROTO_IP == answer->protocol)
  109. break;
  110. }
  111. err = -EPROTONOSUPPORT;
  112. answer = NULL;
  113. }
  114. if (!answer) {
  115. if (try_loading_module < 2) {
  116. rcu_read_unlock();
  117. /*
  118. * Be more specific, e.g. net-pf-10-proto-132-type-1
  119. * (net-pf-PF_INET6-proto-IPPROTO_SCTP-type-SOCK_STREAM)
  120. */
  121. if (++try_loading_module == 1)
  122. request_module("net-pf-%d-proto-%d-type-%d",
  123. PF_INET6, protocol, sock->type);
  124. /*
  125. * Fall back to generic, e.g. net-pf-10-proto-132
  126. * (net-pf-PF_INET6-proto-IPPROTO_SCTP)
  127. */
  128. else
  129. request_module("net-pf-%d-proto-%d",
  130. PF_INET6, protocol);
  131. goto lookup_protocol;
  132. } else
  133. goto out_rcu_unlock;
  134. }
  135. err = -EPERM;
  136. if (answer->capability > 0 && !capable(answer->capability))
  137. goto out_rcu_unlock;
  138. sock->ops = answer->ops;
  139. answer_prot = answer->prot;
  140. answer_no_check = answer->no_check;
  141. answer_flags = answer->flags;
  142. rcu_read_unlock();
  143. BUG_TRAP(answer_prot->slab != NULL);
  144. err = -ENOBUFS;
  145. sk = sk_alloc(net, PF_INET6, GFP_KERNEL, answer_prot);
  146. if (sk == NULL)
  147. goto out;
  148. sock_init_data(sock, sk);
  149. err = 0;
  150. sk->sk_no_check = answer_no_check;
  151. if (INET_PROTOSW_REUSE & answer_flags)
  152. sk->sk_reuse = 1;
  153. inet = inet_sk(sk);
  154. inet->is_icsk = (INET_PROTOSW_ICSK & answer_flags) != 0;
  155. if (SOCK_RAW == sock->type) {
  156. inet->num = protocol;
  157. if (IPPROTO_RAW == protocol)
  158. inet->hdrincl = 1;
  159. }
  160. sk->sk_destruct = inet_sock_destruct;
  161. sk->sk_family = PF_INET6;
  162. sk->sk_protocol = protocol;
  163. sk->sk_backlog_rcv = answer->prot->backlog_rcv;
  164. inet_sk(sk)->pinet6 = np = inet6_sk_generic(sk);
  165. np->hop_limit = -1;
  166. np->mcast_hops = -1;
  167. np->mc_loop = 1;
  168. np->pmtudisc = IPV6_PMTUDISC_WANT;
  169. np->ipv6only = init_net.ipv6.sysctl.bindv6only;
  170. /* Init the ipv4 part of the socket since we can have sockets
  171. * using v6 API for ipv4.
  172. */
  173. inet->uc_ttl = -1;
  174. inet->mc_loop = 1;
  175. inet->mc_ttl = 1;
  176. inet->mc_index = 0;
  177. inet->mc_list = NULL;
  178. if (ipv4_config.no_pmtu_disc)
  179. inet->pmtudisc = IP_PMTUDISC_DONT;
  180. else
  181. inet->pmtudisc = IP_PMTUDISC_WANT;
  182. /*
  183. * Increment only the relevant sk_prot->socks debug field, this changes
  184. * the previous behaviour of incrementing both the equivalent to
  185. * answer->prot->socks (inet6_sock_nr) and inet_sock_nr.
  186. *
  187. * This allows better debug granularity as we'll know exactly how many
  188. * UDPv6, TCPv6, etc socks were allocated, not the sum of all IPv6
  189. * transport protocol socks. -acme
  190. */
  191. sk_refcnt_debug_inc(sk);
  192. if (inet->num) {
  193. /* It assumes that any protocol which allows
  194. * the user to assign a number at socket
  195. * creation time automatically shares.
  196. */
  197. inet->sport = htons(inet->num);
  198. sk->sk_prot->hash(sk);
  199. }
  200. if (sk->sk_prot->init) {
  201. err = sk->sk_prot->init(sk);
  202. if (err) {
  203. sk_common_release(sk);
  204. goto out;
  205. }
  206. }
  207. out:
  208. return err;
  209. out_rcu_unlock:
  210. rcu_read_unlock();
  211. goto out;
  212. }
  213. /* bind for INET6 API */
  214. int inet6_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
  215. {
  216. struct sockaddr_in6 *addr=(struct sockaddr_in6 *)uaddr;
  217. struct sock *sk = sock->sk;
  218. struct inet_sock *inet = inet_sk(sk);
  219. struct ipv6_pinfo *np = inet6_sk(sk);
  220. __be32 v4addr = 0;
  221. unsigned short snum;
  222. int addr_type = 0;
  223. int err = 0;
  224. /* If the socket has its own bind function then use it. */
  225. if (sk->sk_prot->bind)
  226. return sk->sk_prot->bind(sk, uaddr, addr_len);
  227. if (addr_len < SIN6_LEN_RFC2133)
  228. return -EINVAL;
  229. addr_type = ipv6_addr_type(&addr->sin6_addr);
  230. if ((addr_type & IPV6_ADDR_MULTICAST) && sock->type == SOCK_STREAM)
  231. return -EINVAL;
  232. snum = ntohs(addr->sin6_port);
  233. if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
  234. return -EACCES;
  235. lock_sock(sk);
  236. /* Check these errors (active socket, double bind). */
  237. if (sk->sk_state != TCP_CLOSE || inet->num) {
  238. err = -EINVAL;
  239. goto out;
  240. }
  241. /* Check if the address belongs to the host. */
  242. if (addr_type == IPV6_ADDR_MAPPED) {
  243. v4addr = addr->sin6_addr.s6_addr32[3];
  244. if (inet_addr_type(&init_net, v4addr) != RTN_LOCAL) {
  245. err = -EADDRNOTAVAIL;
  246. goto out;
  247. }
  248. } else {
  249. if (addr_type != IPV6_ADDR_ANY) {
  250. struct net_device *dev = NULL;
  251. if (addr_type & IPV6_ADDR_LINKLOCAL) {
  252. if (addr_len >= sizeof(struct sockaddr_in6) &&
  253. addr->sin6_scope_id) {
  254. /* Override any existing binding, if another one
  255. * is supplied by user.
  256. */
  257. sk->sk_bound_dev_if = addr->sin6_scope_id;
  258. }
  259. /* Binding to link-local address requires an interface */
  260. if (!sk->sk_bound_dev_if) {
  261. err = -EINVAL;
  262. goto out;
  263. }
  264. dev = dev_get_by_index(&init_net, sk->sk_bound_dev_if);
  265. if (!dev) {
  266. err = -ENODEV;
  267. goto out;
  268. }
  269. }
  270. /* ipv4 addr of the socket is invalid. Only the
  271. * unspecified and mapped address have a v4 equivalent.
  272. */
  273. v4addr = LOOPBACK4_IPV6;
  274. if (!(addr_type & IPV6_ADDR_MULTICAST)) {
  275. if (!ipv6_chk_addr(&init_net, &addr->sin6_addr,
  276. dev, 0)) {
  277. if (dev)
  278. dev_put(dev);
  279. err = -EADDRNOTAVAIL;
  280. goto out;
  281. }
  282. }
  283. if (dev)
  284. dev_put(dev);
  285. }
  286. }
  287. inet->rcv_saddr = v4addr;
  288. inet->saddr = v4addr;
  289. ipv6_addr_copy(&np->rcv_saddr, &addr->sin6_addr);
  290. if (!(addr_type & IPV6_ADDR_MULTICAST))
  291. ipv6_addr_copy(&np->saddr, &addr->sin6_addr);
  292. /* Make sure we are allowed to bind here. */
  293. if (sk->sk_prot->get_port(sk, snum)) {
  294. inet_reset_saddr(sk);
  295. err = -EADDRINUSE;
  296. goto out;
  297. }
  298. if (addr_type != IPV6_ADDR_ANY)
  299. sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
  300. if (snum)
  301. sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
  302. inet->sport = htons(inet->num);
  303. inet->dport = 0;
  304. inet->daddr = 0;
  305. out:
  306. release_sock(sk);
  307. return err;
  308. }
  309. EXPORT_SYMBOL(inet6_bind);
  310. int inet6_release(struct socket *sock)
  311. {
  312. struct sock *sk = sock->sk;
  313. if (sk == NULL)
  314. return -EINVAL;
  315. /* Free mc lists */
  316. ipv6_sock_mc_close(sk);
  317. /* Free ac lists */
  318. ipv6_sock_ac_close(sk);
  319. return inet_release(sock);
  320. }
  321. EXPORT_SYMBOL(inet6_release);
  322. int inet6_destroy_sock(struct sock *sk)
  323. {
  324. struct ipv6_pinfo *np = inet6_sk(sk);
  325. struct sk_buff *skb;
  326. struct ipv6_txoptions *opt;
  327. /* Release rx options */
  328. if ((skb = xchg(&np->pktoptions, NULL)) != NULL)
  329. kfree_skb(skb);
  330. /* Free flowlabels */
  331. fl6_free_socklist(sk);
  332. /* Free tx options */
  333. if ((opt = xchg(&np->opt, NULL)) != NULL)
  334. sock_kfree_s(sk, opt, opt->tot_len);
  335. return 0;
  336. }
  337. EXPORT_SYMBOL_GPL(inet6_destroy_sock);
  338. /*
  339. * This does both peername and sockname.
  340. */
  341. int inet6_getname(struct socket *sock, struct sockaddr *uaddr,
  342. int *uaddr_len, int peer)
  343. {
  344. struct sockaddr_in6 *sin=(struct sockaddr_in6 *)uaddr;
  345. struct sock *sk = sock->sk;
  346. struct inet_sock *inet = inet_sk(sk);
  347. struct ipv6_pinfo *np = inet6_sk(sk);
  348. sin->sin6_family = AF_INET6;
  349. sin->sin6_flowinfo = 0;
  350. sin->sin6_scope_id = 0;
  351. if (peer) {
  352. if (!inet->dport)
  353. return -ENOTCONN;
  354. if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
  355. peer == 1)
  356. return -ENOTCONN;
  357. sin->sin6_port = inet->dport;
  358. ipv6_addr_copy(&sin->sin6_addr, &np->daddr);
  359. if (np->sndflow)
  360. sin->sin6_flowinfo = np->flow_label;
  361. } else {
  362. if (ipv6_addr_any(&np->rcv_saddr))
  363. ipv6_addr_copy(&sin->sin6_addr, &np->saddr);
  364. else
  365. ipv6_addr_copy(&sin->sin6_addr, &np->rcv_saddr);
  366. sin->sin6_port = inet->sport;
  367. }
  368. if (ipv6_addr_type(&sin->sin6_addr) & IPV6_ADDR_LINKLOCAL)
  369. sin->sin6_scope_id = sk->sk_bound_dev_if;
  370. *uaddr_len = sizeof(*sin);
  371. return(0);
  372. }
  373. EXPORT_SYMBOL(inet6_getname);
  374. int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  375. {
  376. struct sock *sk = sock->sk;
  377. switch(cmd)
  378. {
  379. case SIOCGSTAMP:
  380. return sock_get_timestamp(sk, (struct timeval __user *)arg);
  381. case SIOCGSTAMPNS:
  382. return sock_get_timestampns(sk, (struct timespec __user *)arg);
  383. case SIOCADDRT:
  384. case SIOCDELRT:
  385. return(ipv6_route_ioctl(cmd,(void __user *)arg));
  386. case SIOCSIFADDR:
  387. return addrconf_add_ifaddr((void __user *) arg);
  388. case SIOCDIFADDR:
  389. return addrconf_del_ifaddr((void __user *) arg);
  390. case SIOCSIFDSTADDR:
  391. return addrconf_set_dstaddr((void __user *) arg);
  392. default:
  393. if (!sk->sk_prot->ioctl)
  394. return -ENOIOCTLCMD;
  395. return sk->sk_prot->ioctl(sk, cmd, arg);
  396. }
  397. /*NOTREACHED*/
  398. return(0);
  399. }
  400. EXPORT_SYMBOL(inet6_ioctl);
  401. const struct proto_ops inet6_stream_ops = {
  402. .family = PF_INET6,
  403. .owner = THIS_MODULE,
  404. .release = inet6_release,
  405. .bind = inet6_bind,
  406. .connect = inet_stream_connect, /* ok */
  407. .socketpair = sock_no_socketpair, /* a do nothing */
  408. .accept = inet_accept, /* ok */
  409. .getname = inet6_getname,
  410. .poll = tcp_poll, /* ok */
  411. .ioctl = inet6_ioctl, /* must change */
  412. .listen = inet_listen, /* ok */
  413. .shutdown = inet_shutdown, /* ok */
  414. .setsockopt = sock_common_setsockopt, /* ok */
  415. .getsockopt = sock_common_getsockopt, /* ok */
  416. .sendmsg = tcp_sendmsg, /* ok */
  417. .recvmsg = sock_common_recvmsg, /* ok */
  418. .mmap = sock_no_mmap,
  419. .sendpage = tcp_sendpage,
  420. .splice_read = tcp_splice_read,
  421. #ifdef CONFIG_COMPAT
  422. .compat_setsockopt = compat_sock_common_setsockopt,
  423. .compat_getsockopt = compat_sock_common_getsockopt,
  424. #endif
  425. };
  426. const struct proto_ops inet6_dgram_ops = {
  427. .family = PF_INET6,
  428. .owner = THIS_MODULE,
  429. .release = inet6_release,
  430. .bind = inet6_bind,
  431. .connect = inet_dgram_connect, /* ok */
  432. .socketpair = sock_no_socketpair, /* a do nothing */
  433. .accept = sock_no_accept, /* a do nothing */
  434. .getname = inet6_getname,
  435. .poll = udp_poll, /* ok */
  436. .ioctl = inet6_ioctl, /* must change */
  437. .listen = sock_no_listen, /* ok */
  438. .shutdown = inet_shutdown, /* ok */
  439. .setsockopt = sock_common_setsockopt, /* ok */
  440. .getsockopt = sock_common_getsockopt, /* ok */
  441. .sendmsg = inet_sendmsg, /* ok */
  442. .recvmsg = sock_common_recvmsg, /* ok */
  443. .mmap = sock_no_mmap,
  444. .sendpage = sock_no_sendpage,
  445. #ifdef CONFIG_COMPAT
  446. .compat_setsockopt = compat_sock_common_setsockopt,
  447. .compat_getsockopt = compat_sock_common_getsockopt,
  448. #endif
  449. };
  450. static struct net_proto_family inet6_family_ops = {
  451. .family = PF_INET6,
  452. .create = inet6_create,
  453. .owner = THIS_MODULE,
  454. };
  455. int inet6_register_protosw(struct inet_protosw *p)
  456. {
  457. struct list_head *lh;
  458. struct inet_protosw *answer;
  459. struct list_head *last_perm;
  460. int protocol = p->protocol;
  461. int ret;
  462. spin_lock_bh(&inetsw6_lock);
  463. ret = -EINVAL;
  464. if (p->type >= SOCK_MAX)
  465. goto out_illegal;
  466. /* If we are trying to override a permanent protocol, bail. */
  467. answer = NULL;
  468. ret = -EPERM;
  469. last_perm = &inetsw6[p->type];
  470. list_for_each(lh, &inetsw6[p->type]) {
  471. answer = list_entry(lh, struct inet_protosw, list);
  472. /* Check only the non-wild match. */
  473. if (INET_PROTOSW_PERMANENT & answer->flags) {
  474. if (protocol == answer->protocol)
  475. break;
  476. last_perm = lh;
  477. }
  478. answer = NULL;
  479. }
  480. if (answer)
  481. goto out_permanent;
  482. /* Add the new entry after the last permanent entry if any, so that
  483. * the new entry does not override a permanent entry when matched with
  484. * a wild-card protocol. But it is allowed to override any existing
  485. * non-permanent entry. This means that when we remove this entry, the
  486. * system automatically returns to the old behavior.
  487. */
  488. list_add_rcu(&p->list, last_perm);
  489. ret = 0;
  490. out:
  491. spin_unlock_bh(&inetsw6_lock);
  492. return ret;
  493. out_permanent:
  494. printk(KERN_ERR "Attempt to override permanent protocol %d.\n",
  495. protocol);
  496. goto out;
  497. out_illegal:
  498. printk(KERN_ERR
  499. "Ignoring attempt to register invalid socket type %d.\n",
  500. p->type);
  501. goto out;
  502. }
  503. EXPORT_SYMBOL(inet6_register_protosw);
  504. void
  505. inet6_unregister_protosw(struct inet_protosw *p)
  506. {
  507. if (INET_PROTOSW_PERMANENT & p->flags) {
  508. printk(KERN_ERR
  509. "Attempt to unregister permanent protocol %d.\n",
  510. p->protocol);
  511. } else {
  512. spin_lock_bh(&inetsw6_lock);
  513. list_del_rcu(&p->list);
  514. spin_unlock_bh(&inetsw6_lock);
  515. synchronize_net();
  516. }
  517. }
  518. EXPORT_SYMBOL(inet6_unregister_protosw);
  519. int inet6_sk_rebuild_header(struct sock *sk)
  520. {
  521. int err;
  522. struct dst_entry *dst;
  523. struct ipv6_pinfo *np = inet6_sk(sk);
  524. dst = __sk_dst_check(sk, np->dst_cookie);
  525. if (dst == NULL) {
  526. struct inet_sock *inet = inet_sk(sk);
  527. struct in6_addr *final_p = NULL, final;
  528. struct flowi fl;
  529. memset(&fl, 0, sizeof(fl));
  530. fl.proto = sk->sk_protocol;
  531. ipv6_addr_copy(&fl.fl6_dst, &np->daddr);
  532. ipv6_addr_copy(&fl.fl6_src, &np->saddr);
  533. fl.fl6_flowlabel = np->flow_label;
  534. fl.oif = sk->sk_bound_dev_if;
  535. fl.fl_ip_dport = inet->dport;
  536. fl.fl_ip_sport = inet->sport;
  537. security_sk_classify_flow(sk, &fl);
  538. if (np->opt && np->opt->srcrt) {
  539. struct rt0_hdr *rt0 = (struct rt0_hdr *) np->opt->srcrt;
  540. ipv6_addr_copy(&final, &fl.fl6_dst);
  541. ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
  542. final_p = &final;
  543. }
  544. err = ip6_dst_lookup(sk, &dst, &fl);
  545. if (err) {
  546. sk->sk_route_caps = 0;
  547. return err;
  548. }
  549. if (final_p)
  550. ipv6_addr_copy(&fl.fl6_dst, final_p);
  551. if ((err = xfrm_lookup(&dst, &fl, sk, 0)) < 0) {
  552. sk->sk_err_soft = -err;
  553. return err;
  554. }
  555. __ip6_dst_store(sk, dst, NULL, NULL);
  556. }
  557. return 0;
  558. }
  559. EXPORT_SYMBOL_GPL(inet6_sk_rebuild_header);
  560. int ipv6_opt_accepted(struct sock *sk, struct sk_buff *skb)
  561. {
  562. struct ipv6_pinfo *np = inet6_sk(sk);
  563. struct inet6_skb_parm *opt = IP6CB(skb);
  564. if (np->rxopt.all) {
  565. if ((opt->hop && (np->rxopt.bits.hopopts ||
  566. np->rxopt.bits.ohopopts)) ||
  567. ((IPV6_FLOWINFO_MASK &
  568. *(__be32 *)skb_network_header(skb)) &&
  569. np->rxopt.bits.rxflow) ||
  570. (opt->srcrt && (np->rxopt.bits.srcrt ||
  571. np->rxopt.bits.osrcrt)) ||
  572. ((opt->dst1 || opt->dst0) &&
  573. (np->rxopt.bits.dstopts || np->rxopt.bits.odstopts)))
  574. return 1;
  575. }
  576. return 0;
  577. }
  578. EXPORT_SYMBOL_GPL(ipv6_opt_accepted);
  579. static int __init init_ipv6_mibs(void)
  580. {
  581. if (snmp_mib_init((void **)ipv6_statistics,
  582. sizeof(struct ipstats_mib)) < 0)
  583. goto err_ip_mib;
  584. if (snmp_mib_init((void **)icmpv6_statistics,
  585. sizeof(struct icmpv6_mib)) < 0)
  586. goto err_icmp_mib;
  587. if (snmp_mib_init((void **)icmpv6msg_statistics,
  588. sizeof(struct icmpv6msg_mib)) < 0)
  589. goto err_icmpmsg_mib;
  590. if (snmp_mib_init((void **)udp_stats_in6, sizeof (struct udp_mib)) < 0)
  591. goto err_udp_mib;
  592. if (snmp_mib_init((void **)udplite_stats_in6,
  593. sizeof (struct udp_mib)) < 0)
  594. goto err_udplite_mib;
  595. return 0;
  596. err_udplite_mib:
  597. snmp_mib_free((void **)udp_stats_in6);
  598. err_udp_mib:
  599. snmp_mib_free((void **)icmpv6msg_statistics);
  600. err_icmpmsg_mib:
  601. snmp_mib_free((void **)icmpv6_statistics);
  602. err_icmp_mib:
  603. snmp_mib_free((void **)ipv6_statistics);
  604. err_ip_mib:
  605. return -ENOMEM;
  606. }
  607. static void cleanup_ipv6_mibs(void)
  608. {
  609. snmp_mib_free((void **)ipv6_statistics);
  610. snmp_mib_free((void **)icmpv6_statistics);
  611. snmp_mib_free((void **)icmpv6msg_statistics);
  612. snmp_mib_free((void **)udp_stats_in6);
  613. snmp_mib_free((void **)udplite_stats_in6);
  614. }
  615. static int inet6_net_init(struct net *net)
  616. {
  617. net->ipv6.sysctl.bindv6only = 0;
  618. net->ipv6.sysctl.frags.high_thresh = 256 * 1024;
  619. net->ipv6.sysctl.frags.low_thresh = 192 * 1024;
  620. net->ipv6.sysctl.frags.timeout = IPV6_FRAG_TIMEOUT;
  621. net->ipv6.sysctl.frags.secret_interval = 10 * 60 * HZ;
  622. net->ipv6.sysctl.flush_delay = 0;
  623. net->ipv6.sysctl.ip6_rt_max_size = 4096;
  624. net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
  625. net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
  626. net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
  627. net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
  628. net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
  629. net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
  630. net->ipv6.sysctl.icmpv6_time = 1*HZ;
  631. ipv6_frag_sysctl_init(net);
  632. return 0;
  633. }
  634. static void inet6_net_exit(struct net *net)
  635. {
  636. return;
  637. }
  638. static struct pernet_operations inet6_net_ops = {
  639. .init = inet6_net_init,
  640. .exit = inet6_net_exit,
  641. };
  642. static int __init inet6_init(void)
  643. {
  644. struct sk_buff *dummy_skb;
  645. struct list_head *r;
  646. int err;
  647. BUILD_BUG_ON(sizeof(struct inet6_skb_parm) > sizeof(dummy_skb->cb));
  648. #ifdef MODULE
  649. #if 0 /* FIXME --RR */
  650. if (!mod_member_present(&__this_module, can_unload))
  651. return -EINVAL;
  652. __this_module.can_unload = &ipv6_unload;
  653. #endif
  654. #endif
  655. err = proto_register(&tcpv6_prot, 1);
  656. if (err)
  657. goto out;
  658. err = proto_register(&udpv6_prot, 1);
  659. if (err)
  660. goto out_unregister_tcp_proto;
  661. err = proto_register(&udplitev6_prot, 1);
  662. if (err)
  663. goto out_unregister_udp_proto;
  664. err = proto_register(&rawv6_prot, 1);
  665. if (err)
  666. goto out_unregister_udplite_proto;
  667. /* Register the socket-side information for inet6_create. */
  668. for(r = &inetsw6[0]; r < &inetsw6[SOCK_MAX]; ++r)
  669. INIT_LIST_HEAD(r);
  670. /* We MUST register RAW sockets before we create the ICMP6,
  671. * IGMP6, or NDISC control sockets.
  672. */
  673. err = rawv6_init();
  674. if (err)
  675. goto out_unregister_raw_proto;
  676. /* Register the family here so that the init calls below will
  677. * be able to create sockets. (?? is this dangerous ??)
  678. */
  679. err = sock_register(&inet6_family_ops);
  680. if (err)
  681. goto out_sock_register_fail;
  682. /* Initialise ipv6 mibs */
  683. err = init_ipv6_mibs();
  684. if (err)
  685. goto out_unregister_sock;
  686. /*
  687. * ipngwg API draft makes clear that the correct semantics
  688. * for TCP and UDP is to consider one TCP and UDP instance
  689. * in a host availiable by both INET and INET6 APIs and
  690. * able to communicate via both network protocols.
  691. */
  692. err = register_pernet_subsys(&inet6_net_ops);
  693. if (err)
  694. goto register_pernet_fail;
  695. #ifdef CONFIG_SYSCTL
  696. err = ipv6_sysctl_register();
  697. if (err)
  698. goto sysctl_fail;
  699. #endif
  700. err = icmpv6_init(&inet6_family_ops);
  701. if (err)
  702. goto icmp_fail;
  703. err = ndisc_init(&inet6_family_ops);
  704. if (err)
  705. goto ndisc_fail;
  706. err = igmp6_init(&inet6_family_ops);
  707. if (err)
  708. goto igmp_fail;
  709. err = ipv6_netfilter_init();
  710. if (err)
  711. goto netfilter_fail;
  712. /* Create /proc/foo6 entries. */
  713. #ifdef CONFIG_PROC_FS
  714. err = -ENOMEM;
  715. if (raw6_proc_init())
  716. goto proc_raw6_fail;
  717. if (tcp6_proc_init())
  718. goto proc_tcp6_fail;
  719. if (udp6_proc_init())
  720. goto proc_udp6_fail;
  721. if (udplite6_proc_init())
  722. goto proc_udplite6_fail;
  723. if (ipv6_misc_proc_init())
  724. goto proc_misc6_fail;
  725. if (ac6_proc_init())
  726. goto proc_anycast6_fail;
  727. if (if6_proc_init())
  728. goto proc_if6_fail;
  729. #endif
  730. err = ip6_route_init();
  731. if (err)
  732. goto ip6_route_fail;
  733. err = ip6_flowlabel_init();
  734. if (err)
  735. goto ip6_flowlabel_fail;
  736. err = addrconf_init();
  737. if (err)
  738. goto addrconf_fail;
  739. /* Init v6 extension headers. */
  740. err = ipv6_exthdrs_init();
  741. if (err)
  742. goto ipv6_exthdrs_fail;
  743. err = ipv6_frag_init();
  744. if (err)
  745. goto ipv6_frag_fail;
  746. /* Init v6 transport protocols. */
  747. err = udpv6_init();
  748. if (err)
  749. goto udpv6_fail;
  750. err = udplitev6_init();
  751. if (err)
  752. goto udplitev6_fail;
  753. err = tcpv6_init();
  754. if (err)
  755. goto tcpv6_fail;
  756. err = ipv6_packet_init();
  757. if (err)
  758. goto ipv6_packet_fail;
  759. out:
  760. return err;
  761. ipv6_packet_fail:
  762. tcpv6_exit();
  763. tcpv6_fail:
  764. udplitev6_exit();
  765. udplitev6_fail:
  766. udpv6_exit();
  767. udpv6_fail:
  768. ipv6_frag_exit();
  769. ipv6_frag_fail:
  770. ipv6_exthdrs_exit();
  771. ipv6_exthdrs_fail:
  772. addrconf_cleanup();
  773. addrconf_fail:
  774. ip6_flowlabel_cleanup();
  775. ip6_flowlabel_fail:
  776. ip6_route_cleanup();
  777. ip6_route_fail:
  778. #ifdef CONFIG_PROC_FS
  779. if6_proc_exit();
  780. proc_if6_fail:
  781. ac6_proc_exit();
  782. proc_anycast6_fail:
  783. ipv6_misc_proc_exit();
  784. proc_misc6_fail:
  785. udplite6_proc_exit();
  786. proc_udplite6_fail:
  787. udp6_proc_exit();
  788. proc_udp6_fail:
  789. tcp6_proc_exit();
  790. proc_tcp6_fail:
  791. raw6_proc_exit();
  792. proc_raw6_fail:
  793. #endif
  794. ipv6_netfilter_fini();
  795. netfilter_fail:
  796. igmp6_cleanup();
  797. igmp_fail:
  798. ndisc_cleanup();
  799. ndisc_fail:
  800. icmpv6_cleanup();
  801. icmp_fail:
  802. #ifdef CONFIG_SYSCTL
  803. ipv6_sysctl_unregister();
  804. sysctl_fail:
  805. #endif
  806. unregister_pernet_subsys(&inet6_net_ops);
  807. register_pernet_fail:
  808. cleanup_ipv6_mibs();
  809. out_unregister_sock:
  810. sock_unregister(PF_INET6);
  811. rtnl_unregister_all(PF_INET6);
  812. out_sock_register_fail:
  813. rawv6_exit();
  814. out_unregister_raw_proto:
  815. proto_unregister(&rawv6_prot);
  816. out_unregister_udplite_proto:
  817. proto_unregister(&udplitev6_prot);
  818. out_unregister_udp_proto:
  819. proto_unregister(&udpv6_prot);
  820. out_unregister_tcp_proto:
  821. proto_unregister(&tcpv6_prot);
  822. goto out;
  823. }
  824. module_init(inet6_init);
  825. static void __exit inet6_exit(void)
  826. {
  827. /* First of all disallow new sockets creation. */
  828. sock_unregister(PF_INET6);
  829. /* Disallow any further netlink messages */
  830. rtnl_unregister_all(PF_INET6);
  831. udpv6_exit();
  832. udplitev6_exit();
  833. tcpv6_exit();
  834. /* Cleanup code parts. */
  835. ipv6_packet_cleanup();
  836. ipv6_frag_exit();
  837. ipv6_exthdrs_exit();
  838. addrconf_cleanup();
  839. ip6_flowlabel_cleanup();
  840. ip6_route_cleanup();
  841. #ifdef CONFIG_PROC_FS
  842. /* Cleanup code parts. */
  843. if6_proc_exit();
  844. ac6_proc_exit();
  845. ipv6_misc_proc_exit();
  846. udplite6_proc_exit();
  847. udp6_proc_exit();
  848. tcp6_proc_exit();
  849. raw6_proc_exit();
  850. #endif
  851. ipv6_netfilter_fini();
  852. igmp6_cleanup();
  853. ndisc_cleanup();
  854. icmpv6_cleanup();
  855. rawv6_exit();
  856. #ifdef CONFIG_SYSCTL
  857. ipv6_sysctl_unregister();
  858. #endif
  859. unregister_pernet_subsys(&inet6_net_ops);
  860. cleanup_ipv6_mibs();
  861. proto_unregister(&rawv6_prot);
  862. proto_unregister(&udplitev6_prot);
  863. proto_unregister(&udpv6_prot);
  864. proto_unregister(&tcpv6_prot);
  865. }
  866. module_exit(inet6_exit);
  867. MODULE_ALIAS_NETPROTO(PF_INET6);