af_inet6.c 24 KB

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