af_inet6.c 25 KB

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