af_inet6.c 26 KB

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