af_inet6.c 29 KB

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