af_inet6.c 25 KB

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