af_inet6.c 28 KB

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