af_inet6.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  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);
  147. err = -ENOBUFS;
  148. sk = sk_alloc(net, PF_INET6, GFP_KERNEL, answer_prot, kern);
  149. if (!sk)
  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. sk->sk_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 (sk->sk_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 (!net->ipv4.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 ((snum || !inet->bind_address_no_port) &&
  314. 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. sk->sk_ipv6only = 1;
  323. }
  324. if (snum)
  325. sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
  326. inet->inet_sport = htons(inet->inet_num);
  327. inet->inet_dport = 0;
  328. inet->inet_daddr = 0;
  329. out:
  330. release_sock(sk);
  331. return err;
  332. out_unlock:
  333. rcu_read_unlock();
  334. goto out;
  335. }
  336. EXPORT_SYMBOL(inet6_bind);
  337. int inet6_release(struct socket *sock)
  338. {
  339. struct sock *sk = sock->sk;
  340. if (!sk)
  341. return -EINVAL;
  342. /* Free mc lists */
  343. ipv6_sock_mc_close(sk);
  344. /* Free ac lists */
  345. ipv6_sock_ac_close(sk);
  346. return inet_release(sock);
  347. }
  348. EXPORT_SYMBOL(inet6_release);
  349. void inet6_destroy_sock(struct sock *sk)
  350. {
  351. struct ipv6_pinfo *np = inet6_sk(sk);
  352. struct sk_buff *skb;
  353. struct ipv6_txoptions *opt;
  354. /* Release rx options */
  355. skb = xchg(&np->pktoptions, NULL);
  356. if (skb)
  357. kfree_skb(skb);
  358. skb = xchg(&np->rxpmtu, NULL);
  359. if (skb)
  360. kfree_skb(skb);
  361. /* Free flowlabels */
  362. fl6_free_socklist(sk);
  363. /* Free tx options */
  364. opt = xchg(&np->opt, NULL);
  365. if (opt)
  366. sock_kfree_s(sk, opt, opt->tot_len);
  367. }
  368. EXPORT_SYMBOL_GPL(inet6_destroy_sock);
  369. /*
  370. * This does both peername and sockname.
  371. */
  372. int inet6_getname(struct socket *sock, struct sockaddr *uaddr,
  373. int *uaddr_len, int peer)
  374. {
  375. struct sockaddr_in6 *sin = (struct sockaddr_in6 *)uaddr;
  376. struct sock *sk = sock->sk;
  377. struct inet_sock *inet = inet_sk(sk);
  378. struct ipv6_pinfo *np = inet6_sk(sk);
  379. sin->sin6_family = AF_INET6;
  380. sin->sin6_flowinfo = 0;
  381. sin->sin6_scope_id = 0;
  382. if (peer) {
  383. if (!inet->inet_dport)
  384. return -ENOTCONN;
  385. if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
  386. peer == 1)
  387. return -ENOTCONN;
  388. sin->sin6_port = inet->inet_dport;
  389. sin->sin6_addr = sk->sk_v6_daddr;
  390. if (np->sndflow)
  391. sin->sin6_flowinfo = np->flow_label;
  392. } else {
  393. if (ipv6_addr_any(&sk->sk_v6_rcv_saddr))
  394. sin->sin6_addr = np->saddr;
  395. else
  396. sin->sin6_addr = sk->sk_v6_rcv_saddr;
  397. sin->sin6_port = inet->inet_sport;
  398. }
  399. sin->sin6_scope_id = ipv6_iface_scope_id(&sin->sin6_addr,
  400. sk->sk_bound_dev_if);
  401. *uaddr_len = sizeof(*sin);
  402. return 0;
  403. }
  404. EXPORT_SYMBOL(inet6_getname);
  405. int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  406. {
  407. struct sock *sk = sock->sk;
  408. struct net *net = sock_net(sk);
  409. switch (cmd) {
  410. case SIOCGSTAMP:
  411. return sock_get_timestamp(sk, (struct timeval __user *)arg);
  412. case SIOCGSTAMPNS:
  413. return sock_get_timestampns(sk, (struct timespec __user *)arg);
  414. case SIOCADDRT:
  415. case SIOCDELRT:
  416. return ipv6_route_ioctl(net, cmd, (void __user *)arg);
  417. case SIOCSIFADDR:
  418. return addrconf_add_ifaddr(net, (void __user *) arg);
  419. case SIOCDIFADDR:
  420. return addrconf_del_ifaddr(net, (void __user *) arg);
  421. case SIOCSIFDSTADDR:
  422. return addrconf_set_dstaddr(net, (void __user *) arg);
  423. default:
  424. if (!sk->sk_prot->ioctl)
  425. return -ENOIOCTLCMD;
  426. return sk->sk_prot->ioctl(sk, cmd, arg);
  427. }
  428. /*NOTREACHED*/
  429. return 0;
  430. }
  431. EXPORT_SYMBOL(inet6_ioctl);
  432. const struct proto_ops inet6_stream_ops = {
  433. .family = PF_INET6,
  434. .owner = THIS_MODULE,
  435. .release = inet6_release,
  436. .bind = inet6_bind,
  437. .connect = inet_stream_connect, /* ok */
  438. .socketpair = sock_no_socketpair, /* a do nothing */
  439. .accept = inet_accept, /* ok */
  440. .getname = inet6_getname,
  441. .poll = tcp_poll, /* ok */
  442. .ioctl = inet6_ioctl, /* must change */
  443. .listen = inet_listen, /* ok */
  444. .shutdown = inet_shutdown, /* ok */
  445. .setsockopt = sock_common_setsockopt, /* ok */
  446. .getsockopt = sock_common_getsockopt, /* ok */
  447. .sendmsg = inet_sendmsg, /* ok */
  448. .recvmsg = inet_recvmsg, /* ok */
  449. .mmap = sock_no_mmap,
  450. .sendpage = inet_sendpage,
  451. .splice_read = tcp_splice_read,
  452. #ifdef CONFIG_COMPAT
  453. .compat_setsockopt = compat_sock_common_setsockopt,
  454. .compat_getsockopt = compat_sock_common_getsockopt,
  455. #endif
  456. };
  457. const struct proto_ops inet6_dgram_ops = {
  458. .family = PF_INET6,
  459. .owner = THIS_MODULE,
  460. .release = inet6_release,
  461. .bind = inet6_bind,
  462. .connect = inet_dgram_connect, /* ok */
  463. .socketpair = sock_no_socketpair, /* a do nothing */
  464. .accept = sock_no_accept, /* a do nothing */
  465. .getname = inet6_getname,
  466. .poll = udp_poll, /* ok */
  467. .ioctl = inet6_ioctl, /* must change */
  468. .listen = sock_no_listen, /* ok */
  469. .shutdown = inet_shutdown, /* ok */
  470. .setsockopt = sock_common_setsockopt, /* ok */
  471. .getsockopt = sock_common_getsockopt, /* ok */
  472. .sendmsg = inet_sendmsg, /* ok */
  473. .recvmsg = inet_recvmsg, /* ok */
  474. .mmap = sock_no_mmap,
  475. .sendpage = sock_no_sendpage,
  476. #ifdef CONFIG_COMPAT
  477. .compat_setsockopt = compat_sock_common_setsockopt,
  478. .compat_getsockopt = compat_sock_common_getsockopt,
  479. #endif
  480. };
  481. static const struct net_proto_family inet6_family_ops = {
  482. .family = PF_INET6,
  483. .create = inet6_create,
  484. .owner = THIS_MODULE,
  485. };
  486. int inet6_register_protosw(struct inet_protosw *p)
  487. {
  488. struct list_head *lh;
  489. struct inet_protosw *answer;
  490. struct list_head *last_perm;
  491. int protocol = p->protocol;
  492. int ret;
  493. spin_lock_bh(&inetsw6_lock);
  494. ret = -EINVAL;
  495. if (p->type >= SOCK_MAX)
  496. goto out_illegal;
  497. /* If we are trying to override a permanent protocol, bail. */
  498. answer = NULL;
  499. ret = -EPERM;
  500. last_perm = &inetsw6[p->type];
  501. list_for_each(lh, &inetsw6[p->type]) {
  502. answer = list_entry(lh, struct inet_protosw, list);
  503. /* Check only the non-wild match. */
  504. if (INET_PROTOSW_PERMANENT & answer->flags) {
  505. if (protocol == answer->protocol)
  506. break;
  507. last_perm = lh;
  508. }
  509. answer = NULL;
  510. }
  511. if (answer)
  512. goto out_permanent;
  513. /* Add the new entry after the last permanent entry if any, so that
  514. * the new entry does not override a permanent entry when matched with
  515. * a wild-card protocol. But it is allowed to override any existing
  516. * non-permanent entry. This means that when we remove this entry, the
  517. * system automatically returns to the old behavior.
  518. */
  519. list_add_rcu(&p->list, last_perm);
  520. ret = 0;
  521. out:
  522. spin_unlock_bh(&inetsw6_lock);
  523. return ret;
  524. out_permanent:
  525. pr_err("Attempt to override permanent protocol %d\n", protocol);
  526. goto out;
  527. out_illegal:
  528. pr_err("Ignoring attempt to register invalid socket type %d\n",
  529. p->type);
  530. goto out;
  531. }
  532. EXPORT_SYMBOL(inet6_register_protosw);
  533. void
  534. inet6_unregister_protosw(struct inet_protosw *p)
  535. {
  536. if (INET_PROTOSW_PERMANENT & p->flags) {
  537. pr_err("Attempt to unregister permanent protocol %d\n",
  538. p->protocol);
  539. } else {
  540. spin_lock_bh(&inetsw6_lock);
  541. list_del_rcu(&p->list);
  542. spin_unlock_bh(&inetsw6_lock);
  543. synchronize_net();
  544. }
  545. }
  546. EXPORT_SYMBOL(inet6_unregister_protosw);
  547. int inet6_sk_rebuild_header(struct sock *sk)
  548. {
  549. struct ipv6_pinfo *np = inet6_sk(sk);
  550. struct dst_entry *dst;
  551. dst = __sk_dst_check(sk, np->dst_cookie);
  552. if (!dst) {
  553. struct inet_sock *inet = inet_sk(sk);
  554. struct in6_addr *final_p, final;
  555. struct flowi6 fl6;
  556. memset(&fl6, 0, sizeof(fl6));
  557. fl6.flowi6_proto = sk->sk_protocol;
  558. fl6.daddr = sk->sk_v6_daddr;
  559. fl6.saddr = np->saddr;
  560. fl6.flowlabel = np->flow_label;
  561. fl6.flowi6_oif = sk->sk_bound_dev_if;
  562. fl6.flowi6_mark = sk->sk_mark;
  563. fl6.fl6_dport = inet->inet_dport;
  564. fl6.fl6_sport = inet->inet_sport;
  565. security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
  566. final_p = fl6_update_dst(&fl6, np->opt, &final);
  567. dst = ip6_dst_lookup_flow(sk, &fl6, final_p);
  568. if (IS_ERR(dst)) {
  569. sk->sk_route_caps = 0;
  570. sk->sk_err_soft = -PTR_ERR(dst);
  571. return PTR_ERR(dst);
  572. }
  573. __ip6_dst_store(sk, dst, NULL, NULL);
  574. }
  575. return 0;
  576. }
  577. EXPORT_SYMBOL_GPL(inet6_sk_rebuild_header);
  578. bool ipv6_opt_accepted(const struct sock *sk, const struct sk_buff *skb,
  579. const struct inet6_skb_parm *opt)
  580. {
  581. const struct ipv6_pinfo *np = inet6_sk(sk);
  582. if (np->rxopt.all) {
  583. if ((opt->hop && (np->rxopt.bits.hopopts ||
  584. np->rxopt.bits.ohopopts)) ||
  585. (ip6_flowinfo((struct ipv6hdr *) skb_network_header(skb)) &&
  586. np->rxopt.bits.rxflow) ||
  587. (opt->srcrt && (np->rxopt.bits.srcrt ||
  588. np->rxopt.bits.osrcrt)) ||
  589. ((opt->dst1 || opt->dst0) &&
  590. (np->rxopt.bits.dstopts || np->rxopt.bits.odstopts)))
  591. return true;
  592. }
  593. return false;
  594. }
  595. EXPORT_SYMBOL_GPL(ipv6_opt_accepted);
  596. static struct packet_type ipv6_packet_type __read_mostly = {
  597. .type = cpu_to_be16(ETH_P_IPV6),
  598. .func = ipv6_rcv,
  599. };
  600. static int __init ipv6_packet_init(void)
  601. {
  602. dev_add_pack(&ipv6_packet_type);
  603. return 0;
  604. }
  605. static void ipv6_packet_cleanup(void)
  606. {
  607. dev_remove_pack(&ipv6_packet_type);
  608. }
  609. static int __net_init ipv6_init_mibs(struct net *net)
  610. {
  611. int i;
  612. net->mib.udp_stats_in6 = alloc_percpu(struct udp_mib);
  613. if (!net->mib.udp_stats_in6)
  614. return -ENOMEM;
  615. net->mib.udplite_stats_in6 = alloc_percpu(struct udp_mib);
  616. if (!net->mib.udplite_stats_in6)
  617. goto err_udplite_mib;
  618. net->mib.ipv6_statistics = alloc_percpu(struct ipstats_mib);
  619. if (!net->mib.ipv6_statistics)
  620. goto err_ip_mib;
  621. for_each_possible_cpu(i) {
  622. struct ipstats_mib *af_inet6_stats;
  623. af_inet6_stats = per_cpu_ptr(net->mib.ipv6_statistics, i);
  624. u64_stats_init(&af_inet6_stats->syncp);
  625. }
  626. net->mib.icmpv6_statistics = alloc_percpu(struct icmpv6_mib);
  627. if (!net->mib.icmpv6_statistics)
  628. goto err_icmp_mib;
  629. net->mib.icmpv6msg_statistics = kzalloc(sizeof(struct icmpv6msg_mib),
  630. GFP_KERNEL);
  631. if (!net->mib.icmpv6msg_statistics)
  632. goto err_icmpmsg_mib;
  633. return 0;
  634. err_icmpmsg_mib:
  635. free_percpu(net->mib.icmpv6_statistics);
  636. err_icmp_mib:
  637. free_percpu(net->mib.ipv6_statistics);
  638. err_ip_mib:
  639. free_percpu(net->mib.udplite_stats_in6);
  640. err_udplite_mib:
  641. free_percpu(net->mib.udp_stats_in6);
  642. return -ENOMEM;
  643. }
  644. static void ipv6_cleanup_mibs(struct net *net)
  645. {
  646. free_percpu(net->mib.udp_stats_in6);
  647. free_percpu(net->mib.udplite_stats_in6);
  648. free_percpu(net->mib.ipv6_statistics);
  649. free_percpu(net->mib.icmpv6_statistics);
  650. kfree(net->mib.icmpv6msg_statistics);
  651. }
  652. static int __net_init inet6_net_init(struct net *net)
  653. {
  654. int err = 0;
  655. net->ipv6.sysctl.bindv6only = 0;
  656. net->ipv6.sysctl.icmpv6_time = 1*HZ;
  657. net->ipv6.sysctl.flowlabel_consistency = 1;
  658. net->ipv6.sysctl.auto_flowlabels = 0;
  659. net->ipv6.sysctl.idgen_retries = 3;
  660. net->ipv6.sysctl.idgen_delay = 1 * HZ;
  661. net->ipv6.sysctl.flowlabel_state_ranges = 1;
  662. atomic_set(&net->ipv6.fib6_sernum, 1);
  663. err = ipv6_init_mibs(net);
  664. if (err)
  665. return err;
  666. #ifdef CONFIG_PROC_FS
  667. err = udp6_proc_init(net);
  668. if (err)
  669. goto out;
  670. err = tcp6_proc_init(net);
  671. if (err)
  672. goto proc_tcp6_fail;
  673. err = ac6_proc_init(net);
  674. if (err)
  675. goto proc_ac6_fail;
  676. #endif
  677. return err;
  678. #ifdef CONFIG_PROC_FS
  679. proc_ac6_fail:
  680. tcp6_proc_exit(net);
  681. proc_tcp6_fail:
  682. udp6_proc_exit(net);
  683. out:
  684. ipv6_cleanup_mibs(net);
  685. return err;
  686. #endif
  687. }
  688. static void __net_exit inet6_net_exit(struct net *net)
  689. {
  690. #ifdef CONFIG_PROC_FS
  691. udp6_proc_exit(net);
  692. tcp6_proc_exit(net);
  693. ac6_proc_exit(net);
  694. #endif
  695. ipv6_cleanup_mibs(net);
  696. }
  697. static struct pernet_operations inet6_net_ops = {
  698. .init = inet6_net_init,
  699. .exit = inet6_net_exit,
  700. };
  701. static const struct ipv6_stub ipv6_stub_impl = {
  702. .ipv6_sock_mc_join = ipv6_sock_mc_join,
  703. .ipv6_sock_mc_drop = ipv6_sock_mc_drop,
  704. .ipv6_dst_lookup = ip6_dst_lookup,
  705. .udpv6_encap_enable = udpv6_encap_enable,
  706. .ndisc_send_na = ndisc_send_na,
  707. .nd_tbl = &nd_tbl,
  708. };
  709. static int __init inet6_init(void)
  710. {
  711. struct list_head *r;
  712. int err = 0;
  713. sock_skb_cb_check_size(sizeof(struct inet6_skb_parm));
  714. /* Register the socket-side information for inet6_create. */
  715. for (r = &inetsw6[0]; r < &inetsw6[SOCK_MAX]; ++r)
  716. INIT_LIST_HEAD(r);
  717. if (disable_ipv6_mod) {
  718. pr_info("Loaded, but administratively disabled, reboot required to enable\n");
  719. goto out;
  720. }
  721. err = proto_register(&tcpv6_prot, 1);
  722. if (err)
  723. goto out;
  724. err = proto_register(&udpv6_prot, 1);
  725. if (err)
  726. goto out_unregister_tcp_proto;
  727. err = proto_register(&udplitev6_prot, 1);
  728. if (err)
  729. goto out_unregister_udp_proto;
  730. err = proto_register(&rawv6_prot, 1);
  731. if (err)
  732. goto out_unregister_udplite_proto;
  733. err = proto_register(&pingv6_prot, 1);
  734. if (err)
  735. goto out_unregister_ping_proto;
  736. /* We MUST register RAW sockets before we create the ICMP6,
  737. * IGMP6, or NDISC control sockets.
  738. */
  739. err = rawv6_init();
  740. if (err)
  741. goto out_unregister_raw_proto;
  742. /* Register the family here so that the init calls below will
  743. * be able to create sockets. (?? is this dangerous ??)
  744. */
  745. err = sock_register(&inet6_family_ops);
  746. if (err)
  747. goto out_sock_register_fail;
  748. /*
  749. * ipngwg API draft makes clear that the correct semantics
  750. * for TCP and UDP is to consider one TCP and UDP instance
  751. * in a host available by both INET and INET6 APIs and
  752. * able to communicate via both network protocols.
  753. */
  754. err = register_pernet_subsys(&inet6_net_ops);
  755. if (err)
  756. goto register_pernet_fail;
  757. err = icmpv6_init();
  758. if (err)
  759. goto icmp_fail;
  760. err = ip6_mr_init();
  761. if (err)
  762. goto ipmr_fail;
  763. err = ndisc_init();
  764. if (err)
  765. goto ndisc_fail;
  766. err = igmp6_init();
  767. if (err)
  768. goto igmp_fail;
  769. ipv6_stub = &ipv6_stub_impl;
  770. err = ipv6_netfilter_init();
  771. if (err)
  772. goto netfilter_fail;
  773. /* Create /proc/foo6 entries. */
  774. #ifdef CONFIG_PROC_FS
  775. err = -ENOMEM;
  776. if (raw6_proc_init())
  777. goto proc_raw6_fail;
  778. if (udplite6_proc_init())
  779. goto proc_udplite6_fail;
  780. if (ipv6_misc_proc_init())
  781. goto proc_misc6_fail;
  782. if (if6_proc_init())
  783. goto proc_if6_fail;
  784. #endif
  785. err = ip6_route_init();
  786. if (err)
  787. goto ip6_route_fail;
  788. err = ndisc_late_init();
  789. if (err)
  790. goto ndisc_late_fail;
  791. err = ip6_flowlabel_init();
  792. if (err)
  793. goto ip6_flowlabel_fail;
  794. err = addrconf_init();
  795. if (err)
  796. goto addrconf_fail;
  797. /* Init v6 extension headers. */
  798. err = ipv6_exthdrs_init();
  799. if (err)
  800. goto ipv6_exthdrs_fail;
  801. err = ipv6_frag_init();
  802. if (err)
  803. goto ipv6_frag_fail;
  804. /* Init v6 transport protocols. */
  805. err = udpv6_init();
  806. if (err)
  807. goto udpv6_fail;
  808. err = udplitev6_init();
  809. if (err)
  810. goto udplitev6_fail;
  811. err = tcpv6_init();
  812. if (err)
  813. goto tcpv6_fail;
  814. err = ipv6_packet_init();
  815. if (err)
  816. goto ipv6_packet_fail;
  817. err = pingv6_init();
  818. if (err)
  819. goto pingv6_fail;
  820. #ifdef CONFIG_SYSCTL
  821. err = ipv6_sysctl_register();
  822. if (err)
  823. goto sysctl_fail;
  824. #endif
  825. out:
  826. return err;
  827. #ifdef CONFIG_SYSCTL
  828. sysctl_fail:
  829. pingv6_exit();
  830. #endif
  831. pingv6_fail:
  832. ipv6_packet_cleanup();
  833. ipv6_packet_fail:
  834. tcpv6_exit();
  835. tcpv6_fail:
  836. udplitev6_exit();
  837. udplitev6_fail:
  838. udpv6_exit();
  839. udpv6_fail:
  840. ipv6_frag_exit();
  841. ipv6_frag_fail:
  842. ipv6_exthdrs_exit();
  843. ipv6_exthdrs_fail:
  844. addrconf_cleanup();
  845. addrconf_fail:
  846. ip6_flowlabel_cleanup();
  847. ip6_flowlabel_fail:
  848. ndisc_late_cleanup();
  849. ndisc_late_fail:
  850. ip6_route_cleanup();
  851. ip6_route_fail:
  852. #ifdef CONFIG_PROC_FS
  853. if6_proc_exit();
  854. proc_if6_fail:
  855. ipv6_misc_proc_exit();
  856. proc_misc6_fail:
  857. udplite6_proc_exit();
  858. proc_udplite6_fail:
  859. raw6_proc_exit();
  860. proc_raw6_fail:
  861. #endif
  862. ipv6_netfilter_fini();
  863. netfilter_fail:
  864. igmp6_cleanup();
  865. igmp_fail:
  866. ndisc_cleanup();
  867. ndisc_fail:
  868. ip6_mr_cleanup();
  869. ipmr_fail:
  870. icmpv6_cleanup();
  871. icmp_fail:
  872. unregister_pernet_subsys(&inet6_net_ops);
  873. register_pernet_fail:
  874. sock_unregister(PF_INET6);
  875. rtnl_unregister_all(PF_INET6);
  876. out_sock_register_fail:
  877. rawv6_exit();
  878. out_unregister_ping_proto:
  879. proto_unregister(&pingv6_prot);
  880. out_unregister_raw_proto:
  881. proto_unregister(&rawv6_prot);
  882. out_unregister_udplite_proto:
  883. proto_unregister(&udplitev6_prot);
  884. out_unregister_udp_proto:
  885. proto_unregister(&udpv6_prot);
  886. out_unregister_tcp_proto:
  887. proto_unregister(&tcpv6_prot);
  888. goto out;
  889. }
  890. module_init(inet6_init);
  891. MODULE_ALIAS_NETPROTO(PF_INET6);