af_inet6.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * PF_INET6 socket protocol family
  4. * Linux INET6 implementation
  5. *
  6. * Authors:
  7. * Pedro Roque <[email protected]>
  8. *
  9. * Adapted from linux/net/ipv4/af_inet.c
  10. *
  11. * Fixes:
  12. * piggy, Karl Knutson : Socket protocol table
  13. * Hideaki YOSHIFUJI : sin6_scope_id support
  14. * Arnaldo Melo : check proc_net_create return, cleanups
  15. */
  16. #define pr_fmt(fmt) "IPv6: " fmt
  17. #include <linux/module.h>
  18. #include <linux/capability.h>
  19. #include <linux/errno.h>
  20. #include <linux/types.h>
  21. #include <linux/socket.h>
  22. #include <linux/in.h>
  23. #include <linux/kernel.h>
  24. #include <linux/timer.h>
  25. #include <linux/string.h>
  26. #include <linux/sockios.h>
  27. #include <linux/net.h>
  28. #include <linux/fcntl.h>
  29. #include <linux/mm.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/proc_fs.h>
  32. #include <linux/stat.h>
  33. #include <linux/init.h>
  34. #include <linux/slab.h>
  35. #include <linux/inet.h>
  36. #include <linux/netdevice.h>
  37. #include <linux/icmpv6.h>
  38. #include <linux/netfilter_ipv6.h>
  39. #include <net/ip.h>
  40. #include <net/ipv6.h>
  41. #include <net/udp.h>
  42. #include <net/udplite.h>
  43. #include <net/tcp.h>
  44. #include <net/ping.h>
  45. #include <net/protocol.h>
  46. #include <net/inet_common.h>
  47. #include <net/route.h>
  48. #include <net/transp_v6.h>
  49. #include <net/ip6_route.h>
  50. #include <net/addrconf.h>
  51. #include <net/ipv6_stubs.h>
  52. #include <net/ndisc.h>
  53. #ifdef CONFIG_IPV6_TUNNEL
  54. #include <net/ip6_tunnel.h>
  55. #endif
  56. #include <net/calipso.h>
  57. #include <net/seg6.h>
  58. #include <net/rpl.h>
  59. #include <net/compat.h>
  60. #include <net/xfrm.h>
  61. #include <net/ioam6.h>
  62. #include <net/rawv6.h>
  63. #include <linux/uaccess.h>
  64. #include <linux/mroute6.h>
  65. #include "ip6_offload.h"
  66. MODULE_AUTHOR("Cast of dozens");
  67. MODULE_DESCRIPTION("IPv6 protocol stack for Linux");
  68. MODULE_LICENSE("GPL");
  69. /* The inetsw6 table contains everything that inet6_create needs to
  70. * build a new socket.
  71. */
  72. static struct list_head inetsw6[SOCK_MAX];
  73. static DEFINE_SPINLOCK(inetsw6_lock);
  74. struct ipv6_params ipv6_defaults = {
  75. .disable_ipv6 = 0,
  76. .autoconf = 1,
  77. };
  78. static int disable_ipv6_mod;
  79. module_param_named(disable, disable_ipv6_mod, int, 0444);
  80. MODULE_PARM_DESC(disable, "Disable IPv6 module such that it is non-functional");
  81. module_param_named(disable_ipv6, ipv6_defaults.disable_ipv6, int, 0444);
  82. MODULE_PARM_DESC(disable_ipv6, "Disable IPv6 on all interfaces");
  83. module_param_named(autoconf, ipv6_defaults.autoconf, int, 0444);
  84. MODULE_PARM_DESC(autoconf, "Enable IPv6 address autoconfiguration on all interfaces");
  85. bool ipv6_mod_enabled(void)
  86. {
  87. return disable_ipv6_mod == 0;
  88. }
  89. EXPORT_SYMBOL_GPL(ipv6_mod_enabled);
  90. static __inline__ struct ipv6_pinfo *inet6_sk_generic(struct sock *sk)
  91. {
  92. const int offset = sk->sk_prot->obj_size - sizeof(struct ipv6_pinfo);
  93. return (struct ipv6_pinfo *)(((u8 *)sk) + offset);
  94. }
  95. void inet6_sock_destruct(struct sock *sk)
  96. {
  97. inet6_cleanup_sock(sk);
  98. inet_sock_destruct(sk);
  99. }
  100. EXPORT_SYMBOL_GPL(inet6_sock_destruct);
  101. static int inet6_create(struct net *net, struct socket *sock, int protocol,
  102. int kern)
  103. {
  104. struct inet_sock *inet;
  105. struct ipv6_pinfo *np;
  106. struct sock *sk;
  107. struct inet_protosw *answer;
  108. struct proto *answer_prot;
  109. unsigned char answer_flags;
  110. int try_loading_module = 0;
  111. int err;
  112. if (protocol < 0 || protocol >= IPPROTO_MAX)
  113. return -EINVAL;
  114. /* Look for the requested type/protocol pair. */
  115. lookup_protocol:
  116. err = -ESOCKTNOSUPPORT;
  117. rcu_read_lock();
  118. list_for_each_entry_rcu(answer, &inetsw6[sock->type], list) {
  119. err = 0;
  120. /* Check the non-wild match. */
  121. if (protocol == answer->protocol) {
  122. if (protocol != IPPROTO_IP)
  123. break;
  124. } else {
  125. /* Check for the two wild cases. */
  126. if (IPPROTO_IP == protocol) {
  127. protocol = answer->protocol;
  128. break;
  129. }
  130. if (IPPROTO_IP == answer->protocol)
  131. break;
  132. }
  133. err = -EPROTONOSUPPORT;
  134. }
  135. if (err) {
  136. if (try_loading_module < 2) {
  137. rcu_read_unlock();
  138. /*
  139. * Be more specific, e.g. net-pf-10-proto-132-type-1
  140. * (net-pf-PF_INET6-proto-IPPROTO_SCTP-type-SOCK_STREAM)
  141. */
  142. if (++try_loading_module == 1)
  143. request_module("net-pf-%d-proto-%d-type-%d",
  144. PF_INET6, protocol, sock->type);
  145. /*
  146. * Fall back to generic, e.g. net-pf-10-proto-132
  147. * (net-pf-PF_INET6-proto-IPPROTO_SCTP)
  148. */
  149. else
  150. request_module("net-pf-%d-proto-%d",
  151. PF_INET6, protocol);
  152. goto lookup_protocol;
  153. } else
  154. goto out_rcu_unlock;
  155. }
  156. err = -EPERM;
  157. if (sock->type == SOCK_RAW && !kern &&
  158. !ns_capable(net->user_ns, CAP_NET_RAW))
  159. goto out_rcu_unlock;
  160. sock->ops = answer->ops;
  161. answer_prot = answer->prot;
  162. answer_flags = answer->flags;
  163. rcu_read_unlock();
  164. WARN_ON(!answer_prot->slab);
  165. err = -ENOBUFS;
  166. sk = sk_alloc(net, PF_INET6, GFP_KERNEL, answer_prot, kern);
  167. if (!sk)
  168. goto out;
  169. sock_init_data(sock, sk);
  170. err = 0;
  171. if (INET_PROTOSW_REUSE & answer_flags)
  172. sk->sk_reuse = SK_CAN_REUSE;
  173. inet = inet_sk(sk);
  174. inet->is_icsk = (INET_PROTOSW_ICSK & answer_flags) != 0;
  175. if (SOCK_RAW == sock->type) {
  176. inet->inet_num = protocol;
  177. if (IPPROTO_RAW == protocol)
  178. inet->hdrincl = 1;
  179. }
  180. sk->sk_destruct = inet6_sock_destruct;
  181. sk->sk_family = PF_INET6;
  182. sk->sk_protocol = protocol;
  183. sk->sk_backlog_rcv = answer->prot->backlog_rcv;
  184. inet_sk(sk)->pinet6 = np = inet6_sk_generic(sk);
  185. np->hop_limit = -1;
  186. np->mcast_hops = IPV6_DEFAULT_MCASTHOPS;
  187. np->mc_loop = 1;
  188. np->mc_all = 1;
  189. np->pmtudisc = IPV6_PMTUDISC_WANT;
  190. np->repflow = net->ipv6.sysctl.flowlabel_reflect & FLOWLABEL_REFLECT_ESTABLISHED;
  191. sk->sk_ipv6only = net->ipv6.sysctl.bindv6only;
  192. sk->sk_txrehash = READ_ONCE(net->core.sysctl_txrehash);
  193. /* Init the ipv4 part of the socket since we can have sockets
  194. * using v6 API for ipv4.
  195. */
  196. inet->uc_ttl = -1;
  197. inet->mc_loop = 1;
  198. inet->mc_ttl = 1;
  199. inet->mc_index = 0;
  200. RCU_INIT_POINTER(inet->mc_list, NULL);
  201. inet->rcv_tos = 0;
  202. if (READ_ONCE(net->ipv4.sysctl_ip_no_pmtu_disc))
  203. inet->pmtudisc = IP_PMTUDISC_DONT;
  204. else
  205. inet->pmtudisc = IP_PMTUDISC_WANT;
  206. /*
  207. * Increment only the relevant sk_prot->socks debug field, this changes
  208. * the previous behaviour of incrementing both the equivalent to
  209. * answer->prot->socks (inet6_sock_nr) and inet_sock_nr.
  210. *
  211. * This allows better debug granularity as we'll know exactly how many
  212. * UDPv6, TCPv6, etc socks were allocated, not the sum of all IPv6
  213. * transport protocol socks. -acme
  214. */
  215. sk_refcnt_debug_inc(sk);
  216. if (inet->inet_num) {
  217. /* It assumes that any protocol which allows
  218. * the user to assign a number at socket
  219. * creation time automatically shares.
  220. */
  221. inet->inet_sport = htons(inet->inet_num);
  222. err = sk->sk_prot->hash(sk);
  223. if (err) {
  224. sk_common_release(sk);
  225. goto out;
  226. }
  227. }
  228. if (sk->sk_prot->init) {
  229. err = sk->sk_prot->init(sk);
  230. if (err) {
  231. sk_common_release(sk);
  232. goto out;
  233. }
  234. }
  235. if (!kern) {
  236. err = BPF_CGROUP_RUN_PROG_INET_SOCK(sk);
  237. if (err) {
  238. sk_common_release(sk);
  239. goto out;
  240. }
  241. }
  242. out:
  243. return err;
  244. out_rcu_unlock:
  245. rcu_read_unlock();
  246. goto out;
  247. }
  248. static int __inet6_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len,
  249. u32 flags)
  250. {
  251. struct sockaddr_in6 *addr = (struct sockaddr_in6 *)uaddr;
  252. struct inet_sock *inet = inet_sk(sk);
  253. struct ipv6_pinfo *np = inet6_sk(sk);
  254. struct net *net = sock_net(sk);
  255. __be32 v4addr = 0;
  256. unsigned short snum;
  257. bool saved_ipv6only;
  258. int addr_type = 0;
  259. int err = 0;
  260. if (addr->sin6_family != AF_INET6)
  261. return -EAFNOSUPPORT;
  262. addr_type = ipv6_addr_type(&addr->sin6_addr);
  263. if ((addr_type & IPV6_ADDR_MULTICAST) && sk->sk_type == SOCK_STREAM)
  264. return -EINVAL;
  265. snum = ntohs(addr->sin6_port);
  266. if (!(flags & BIND_NO_CAP_NET_BIND_SERVICE) &&
  267. snum && inet_port_requires_bind_service(net, snum) &&
  268. !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
  269. return -EACCES;
  270. if (flags & BIND_WITH_LOCK)
  271. lock_sock(sk);
  272. /* Check these errors (active socket, double bind). */
  273. if (sk->sk_state != TCP_CLOSE || inet->inet_num) {
  274. err = -EINVAL;
  275. goto out;
  276. }
  277. /* Check if the address belongs to the host. */
  278. if (addr_type == IPV6_ADDR_MAPPED) {
  279. struct net_device *dev = NULL;
  280. int chk_addr_ret;
  281. /* Binding to v4-mapped address on a v6-only socket
  282. * makes no sense
  283. */
  284. if (ipv6_only_sock(sk)) {
  285. err = -EINVAL;
  286. goto out;
  287. }
  288. rcu_read_lock();
  289. if (sk->sk_bound_dev_if) {
  290. dev = dev_get_by_index_rcu(net, sk->sk_bound_dev_if);
  291. if (!dev) {
  292. err = -ENODEV;
  293. goto out_unlock;
  294. }
  295. }
  296. /* Reproduce AF_INET checks to make the bindings consistent */
  297. v4addr = addr->sin6_addr.s6_addr32[3];
  298. chk_addr_ret = inet_addr_type_dev_table(net, dev, v4addr);
  299. rcu_read_unlock();
  300. if (!inet_addr_valid_or_nonlocal(net, inet, v4addr,
  301. chk_addr_ret)) {
  302. err = -EADDRNOTAVAIL;
  303. goto out;
  304. }
  305. } else {
  306. if (addr_type != IPV6_ADDR_ANY) {
  307. struct net_device *dev = NULL;
  308. rcu_read_lock();
  309. if (__ipv6_addr_needs_scope_id(addr_type)) {
  310. if (addr_len >= sizeof(struct sockaddr_in6) &&
  311. addr->sin6_scope_id) {
  312. /* Override any existing binding, if another one
  313. * is supplied by user.
  314. */
  315. sk->sk_bound_dev_if = addr->sin6_scope_id;
  316. }
  317. /* Binding to link-local address requires an interface */
  318. if (!sk->sk_bound_dev_if) {
  319. err = -EINVAL;
  320. goto out_unlock;
  321. }
  322. }
  323. if (sk->sk_bound_dev_if) {
  324. dev = dev_get_by_index_rcu(net, sk->sk_bound_dev_if);
  325. if (!dev) {
  326. err = -ENODEV;
  327. goto out_unlock;
  328. }
  329. }
  330. /* ipv4 addr of the socket is invalid. Only the
  331. * unspecified and mapped address have a v4 equivalent.
  332. */
  333. v4addr = LOOPBACK4_IPV6;
  334. if (!(addr_type & IPV6_ADDR_MULTICAST)) {
  335. if (!ipv6_can_nonlocal_bind(net, inet) &&
  336. !ipv6_chk_addr(net, &addr->sin6_addr,
  337. dev, 0)) {
  338. err = -EADDRNOTAVAIL;
  339. goto out_unlock;
  340. }
  341. }
  342. rcu_read_unlock();
  343. }
  344. }
  345. inet->inet_rcv_saddr = v4addr;
  346. inet->inet_saddr = v4addr;
  347. sk->sk_v6_rcv_saddr = addr->sin6_addr;
  348. if (!(addr_type & IPV6_ADDR_MULTICAST))
  349. np->saddr = addr->sin6_addr;
  350. saved_ipv6only = sk->sk_ipv6only;
  351. if (addr_type != IPV6_ADDR_ANY && addr_type != IPV6_ADDR_MAPPED)
  352. sk->sk_ipv6only = 1;
  353. /* Make sure we are allowed to bind here. */
  354. if (snum || !(inet->bind_address_no_port ||
  355. (flags & BIND_FORCE_ADDRESS_NO_PORT))) {
  356. err = sk->sk_prot->get_port(sk, snum);
  357. if (err) {
  358. sk->sk_ipv6only = saved_ipv6only;
  359. inet_reset_saddr(sk);
  360. goto out;
  361. }
  362. if (!(flags & BIND_FROM_BPF)) {
  363. err = BPF_CGROUP_RUN_PROG_INET6_POST_BIND(sk);
  364. if (err) {
  365. sk->sk_ipv6only = saved_ipv6only;
  366. inet_reset_saddr(sk);
  367. if (sk->sk_prot->put_port)
  368. sk->sk_prot->put_port(sk);
  369. goto out;
  370. }
  371. }
  372. }
  373. if (addr_type != IPV6_ADDR_ANY)
  374. sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
  375. if (snum)
  376. sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
  377. inet->inet_sport = htons(inet->inet_num);
  378. inet->inet_dport = 0;
  379. inet->inet_daddr = 0;
  380. out:
  381. if (flags & BIND_WITH_LOCK)
  382. release_sock(sk);
  383. return err;
  384. out_unlock:
  385. rcu_read_unlock();
  386. goto out;
  387. }
  388. /* bind for INET6 API */
  389. int inet6_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
  390. {
  391. struct sock *sk = sock->sk;
  392. u32 flags = BIND_WITH_LOCK;
  393. const struct proto *prot;
  394. int err = 0;
  395. /* IPV6_ADDRFORM can change sk->sk_prot under us. */
  396. prot = READ_ONCE(sk->sk_prot);
  397. /* If the socket has its own bind function then use it. */
  398. if (prot->bind)
  399. return prot->bind(sk, uaddr, addr_len);
  400. if (addr_len < SIN6_LEN_RFC2133)
  401. return -EINVAL;
  402. /* BPF prog is run before any checks are done so that if the prog
  403. * changes context in a wrong way it will be caught.
  404. */
  405. err = BPF_CGROUP_RUN_PROG_INET_BIND_LOCK(sk, uaddr,
  406. CGROUP_INET6_BIND, &flags);
  407. if (err)
  408. return err;
  409. return __inet6_bind(sk, uaddr, addr_len, flags);
  410. }
  411. EXPORT_SYMBOL(inet6_bind);
  412. int inet6_release(struct socket *sock)
  413. {
  414. struct sock *sk = sock->sk;
  415. if (!sk)
  416. return -EINVAL;
  417. /* Free mc lists */
  418. ipv6_sock_mc_close(sk);
  419. /* Free ac lists */
  420. ipv6_sock_ac_close(sk);
  421. return inet_release(sock);
  422. }
  423. EXPORT_SYMBOL(inet6_release);
  424. void inet6_destroy_sock(struct sock *sk)
  425. {
  426. struct ipv6_pinfo *np = inet6_sk(sk);
  427. struct sk_buff *skb;
  428. struct ipv6_txoptions *opt;
  429. /* Release rx options */
  430. skb = xchg(&np->pktoptions, NULL);
  431. kfree_skb(skb);
  432. skb = xchg(&np->rxpmtu, NULL);
  433. kfree_skb(skb);
  434. /* Free flowlabels */
  435. fl6_free_socklist(sk);
  436. /* Free tx options */
  437. opt = xchg((__force struct ipv6_txoptions **)&np->opt, NULL);
  438. if (opt) {
  439. atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
  440. txopt_put(opt);
  441. }
  442. }
  443. EXPORT_SYMBOL_GPL(inet6_destroy_sock);
  444. void inet6_cleanup_sock(struct sock *sk)
  445. {
  446. inet6_destroy_sock(sk);
  447. }
  448. EXPORT_SYMBOL_GPL(inet6_cleanup_sock);
  449. /*
  450. * This does both peername and sockname.
  451. */
  452. int inet6_getname(struct socket *sock, struct sockaddr *uaddr,
  453. int peer)
  454. {
  455. struct sockaddr_in6 *sin = (struct sockaddr_in6 *)uaddr;
  456. struct sock *sk = sock->sk;
  457. struct inet_sock *inet = inet_sk(sk);
  458. struct ipv6_pinfo *np = inet6_sk(sk);
  459. sin->sin6_family = AF_INET6;
  460. sin->sin6_flowinfo = 0;
  461. sin->sin6_scope_id = 0;
  462. lock_sock(sk);
  463. if (peer) {
  464. if (!inet->inet_dport ||
  465. (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
  466. peer == 1)) {
  467. release_sock(sk);
  468. return -ENOTCONN;
  469. }
  470. sin->sin6_port = inet->inet_dport;
  471. sin->sin6_addr = sk->sk_v6_daddr;
  472. if (np->sndflow)
  473. sin->sin6_flowinfo = np->flow_label;
  474. BPF_CGROUP_RUN_SA_PROG(sk, (struct sockaddr *)sin,
  475. CGROUP_INET6_GETPEERNAME);
  476. } else {
  477. if (ipv6_addr_any(&sk->sk_v6_rcv_saddr))
  478. sin->sin6_addr = np->saddr;
  479. else
  480. sin->sin6_addr = sk->sk_v6_rcv_saddr;
  481. sin->sin6_port = inet->inet_sport;
  482. BPF_CGROUP_RUN_SA_PROG(sk, (struct sockaddr *)sin,
  483. CGROUP_INET6_GETSOCKNAME);
  484. }
  485. sin->sin6_scope_id = ipv6_iface_scope_id(&sin->sin6_addr,
  486. sk->sk_bound_dev_if);
  487. release_sock(sk);
  488. return sizeof(*sin);
  489. }
  490. EXPORT_SYMBOL(inet6_getname);
  491. int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  492. {
  493. void __user *argp = (void __user *)arg;
  494. struct sock *sk = sock->sk;
  495. struct net *net = sock_net(sk);
  496. const struct proto *prot;
  497. switch (cmd) {
  498. case SIOCADDRT:
  499. case SIOCDELRT: {
  500. struct in6_rtmsg rtmsg;
  501. if (copy_from_user(&rtmsg, argp, sizeof(rtmsg)))
  502. return -EFAULT;
  503. return ipv6_route_ioctl(net, cmd, &rtmsg);
  504. }
  505. case SIOCSIFADDR:
  506. return addrconf_add_ifaddr(net, argp);
  507. case SIOCDIFADDR:
  508. return addrconf_del_ifaddr(net, argp);
  509. case SIOCSIFDSTADDR:
  510. return addrconf_set_dstaddr(net, argp);
  511. default:
  512. /* IPV6_ADDRFORM can change sk->sk_prot under us. */
  513. prot = READ_ONCE(sk->sk_prot);
  514. if (!prot->ioctl)
  515. return -ENOIOCTLCMD;
  516. return prot->ioctl(sk, cmd, arg);
  517. }
  518. /*NOTREACHED*/
  519. return 0;
  520. }
  521. EXPORT_SYMBOL(inet6_ioctl);
  522. #ifdef CONFIG_COMPAT
  523. struct compat_in6_rtmsg {
  524. struct in6_addr rtmsg_dst;
  525. struct in6_addr rtmsg_src;
  526. struct in6_addr rtmsg_gateway;
  527. u32 rtmsg_type;
  528. u16 rtmsg_dst_len;
  529. u16 rtmsg_src_len;
  530. u32 rtmsg_metric;
  531. u32 rtmsg_info;
  532. u32 rtmsg_flags;
  533. s32 rtmsg_ifindex;
  534. };
  535. static int inet6_compat_routing_ioctl(struct sock *sk, unsigned int cmd,
  536. struct compat_in6_rtmsg __user *ur)
  537. {
  538. struct in6_rtmsg rt;
  539. if (copy_from_user(&rt.rtmsg_dst, &ur->rtmsg_dst,
  540. 3 * sizeof(struct in6_addr)) ||
  541. get_user(rt.rtmsg_type, &ur->rtmsg_type) ||
  542. get_user(rt.rtmsg_dst_len, &ur->rtmsg_dst_len) ||
  543. get_user(rt.rtmsg_src_len, &ur->rtmsg_src_len) ||
  544. get_user(rt.rtmsg_metric, &ur->rtmsg_metric) ||
  545. get_user(rt.rtmsg_info, &ur->rtmsg_info) ||
  546. get_user(rt.rtmsg_flags, &ur->rtmsg_flags) ||
  547. get_user(rt.rtmsg_ifindex, &ur->rtmsg_ifindex))
  548. return -EFAULT;
  549. return ipv6_route_ioctl(sock_net(sk), cmd, &rt);
  550. }
  551. int inet6_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  552. {
  553. void __user *argp = compat_ptr(arg);
  554. struct sock *sk = sock->sk;
  555. switch (cmd) {
  556. case SIOCADDRT:
  557. case SIOCDELRT:
  558. return inet6_compat_routing_ioctl(sk, cmd, argp);
  559. default:
  560. return -ENOIOCTLCMD;
  561. }
  562. }
  563. EXPORT_SYMBOL_GPL(inet6_compat_ioctl);
  564. #endif /* CONFIG_COMPAT */
  565. INDIRECT_CALLABLE_DECLARE(int udpv6_sendmsg(struct sock *, struct msghdr *,
  566. size_t));
  567. int inet6_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
  568. {
  569. struct sock *sk = sock->sk;
  570. const struct proto *prot;
  571. if (unlikely(inet_send_prepare(sk)))
  572. return -EAGAIN;
  573. /* IPV6_ADDRFORM can change sk->sk_prot under us. */
  574. prot = READ_ONCE(sk->sk_prot);
  575. return INDIRECT_CALL_2(prot->sendmsg, tcp_sendmsg, udpv6_sendmsg,
  576. sk, msg, size);
  577. }
  578. INDIRECT_CALLABLE_DECLARE(int udpv6_recvmsg(struct sock *, struct msghdr *,
  579. size_t, int, int *));
  580. int inet6_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
  581. int flags)
  582. {
  583. struct sock *sk = sock->sk;
  584. const struct proto *prot;
  585. int addr_len = 0;
  586. int err;
  587. if (likely(!(flags & MSG_ERRQUEUE)))
  588. sock_rps_record_flow(sk);
  589. /* IPV6_ADDRFORM can change sk->sk_prot under us. */
  590. prot = READ_ONCE(sk->sk_prot);
  591. err = INDIRECT_CALL_2(prot->recvmsg, tcp_recvmsg, udpv6_recvmsg,
  592. sk, msg, size, flags, &addr_len);
  593. if (err >= 0)
  594. msg->msg_namelen = addr_len;
  595. return err;
  596. }
  597. const struct proto_ops inet6_stream_ops = {
  598. .family = PF_INET6,
  599. .owner = THIS_MODULE,
  600. .release = inet6_release,
  601. .bind = inet6_bind,
  602. .connect = inet_stream_connect, /* ok */
  603. .socketpair = sock_no_socketpair, /* a do nothing */
  604. .accept = inet_accept, /* ok */
  605. .getname = inet6_getname,
  606. .poll = tcp_poll, /* ok */
  607. .ioctl = inet6_ioctl, /* must change */
  608. .gettstamp = sock_gettstamp,
  609. .listen = inet_listen, /* ok */
  610. .shutdown = inet_shutdown, /* ok */
  611. .setsockopt = sock_common_setsockopt, /* ok */
  612. .getsockopt = sock_common_getsockopt, /* ok */
  613. .sendmsg = inet6_sendmsg, /* retpoline's sake */
  614. .recvmsg = inet6_recvmsg, /* retpoline's sake */
  615. #ifdef CONFIG_MMU
  616. .mmap = tcp_mmap,
  617. #endif
  618. .sendpage = inet_sendpage,
  619. .sendmsg_locked = tcp_sendmsg_locked,
  620. .sendpage_locked = tcp_sendpage_locked,
  621. .splice_read = tcp_splice_read,
  622. .read_sock = tcp_read_sock,
  623. .read_skb = tcp_read_skb,
  624. .peek_len = tcp_peek_len,
  625. #ifdef CONFIG_COMPAT
  626. .compat_ioctl = inet6_compat_ioctl,
  627. #endif
  628. .set_rcvlowat = tcp_set_rcvlowat,
  629. };
  630. const struct proto_ops inet6_dgram_ops = {
  631. .family = PF_INET6,
  632. .owner = THIS_MODULE,
  633. .release = inet6_release,
  634. .bind = inet6_bind,
  635. .connect = inet_dgram_connect, /* ok */
  636. .socketpair = sock_no_socketpair, /* a do nothing */
  637. .accept = sock_no_accept, /* a do nothing */
  638. .getname = inet6_getname,
  639. .poll = udp_poll, /* ok */
  640. .ioctl = inet6_ioctl, /* must change */
  641. .gettstamp = sock_gettstamp,
  642. .listen = sock_no_listen, /* ok */
  643. .shutdown = inet_shutdown, /* ok */
  644. .setsockopt = sock_common_setsockopt, /* ok */
  645. .getsockopt = sock_common_getsockopt, /* ok */
  646. .sendmsg = inet6_sendmsg, /* retpoline's sake */
  647. .recvmsg = inet6_recvmsg, /* retpoline's sake */
  648. .read_skb = udp_read_skb,
  649. .mmap = sock_no_mmap,
  650. .sendpage = sock_no_sendpage,
  651. .set_peek_off = sk_set_peek_off,
  652. #ifdef CONFIG_COMPAT
  653. .compat_ioctl = inet6_compat_ioctl,
  654. #endif
  655. };
  656. static const struct net_proto_family inet6_family_ops = {
  657. .family = PF_INET6,
  658. .create = inet6_create,
  659. .owner = THIS_MODULE,
  660. };
  661. int inet6_register_protosw(struct inet_protosw *p)
  662. {
  663. struct list_head *lh;
  664. struct inet_protosw *answer;
  665. struct list_head *last_perm;
  666. int protocol = p->protocol;
  667. int ret;
  668. spin_lock_bh(&inetsw6_lock);
  669. ret = -EINVAL;
  670. if (p->type >= SOCK_MAX)
  671. goto out_illegal;
  672. /* If we are trying to override a permanent protocol, bail. */
  673. answer = NULL;
  674. ret = -EPERM;
  675. last_perm = &inetsw6[p->type];
  676. list_for_each(lh, &inetsw6[p->type]) {
  677. answer = list_entry(lh, struct inet_protosw, list);
  678. /* Check only the non-wild match. */
  679. if (INET_PROTOSW_PERMANENT & answer->flags) {
  680. if (protocol == answer->protocol)
  681. break;
  682. last_perm = lh;
  683. }
  684. answer = NULL;
  685. }
  686. if (answer)
  687. goto out_permanent;
  688. /* Add the new entry after the last permanent entry if any, so that
  689. * the new entry does not override a permanent entry when matched with
  690. * a wild-card protocol. But it is allowed to override any existing
  691. * non-permanent entry. This means that when we remove this entry, the
  692. * system automatically returns to the old behavior.
  693. */
  694. list_add_rcu(&p->list, last_perm);
  695. ret = 0;
  696. out:
  697. spin_unlock_bh(&inetsw6_lock);
  698. return ret;
  699. out_permanent:
  700. pr_err("Attempt to override permanent protocol %d\n", protocol);
  701. goto out;
  702. out_illegal:
  703. pr_err("Ignoring attempt to register invalid socket type %d\n",
  704. p->type);
  705. goto out;
  706. }
  707. EXPORT_SYMBOL(inet6_register_protosw);
  708. void
  709. inet6_unregister_protosw(struct inet_protosw *p)
  710. {
  711. if (INET_PROTOSW_PERMANENT & p->flags) {
  712. pr_err("Attempt to unregister permanent protocol %d\n",
  713. p->protocol);
  714. } else {
  715. spin_lock_bh(&inetsw6_lock);
  716. list_del_rcu(&p->list);
  717. spin_unlock_bh(&inetsw6_lock);
  718. synchronize_net();
  719. }
  720. }
  721. EXPORT_SYMBOL(inet6_unregister_protosw);
  722. int inet6_sk_rebuild_header(struct sock *sk)
  723. {
  724. struct ipv6_pinfo *np = inet6_sk(sk);
  725. struct dst_entry *dst;
  726. dst = __sk_dst_check(sk, np->dst_cookie);
  727. if (!dst) {
  728. struct inet_sock *inet = inet_sk(sk);
  729. struct in6_addr *final_p, final;
  730. struct flowi6 fl6;
  731. memset(&fl6, 0, sizeof(fl6));
  732. fl6.flowi6_proto = sk->sk_protocol;
  733. fl6.daddr = sk->sk_v6_daddr;
  734. fl6.saddr = np->saddr;
  735. fl6.flowlabel = np->flow_label;
  736. fl6.flowi6_oif = sk->sk_bound_dev_if;
  737. fl6.flowi6_mark = sk->sk_mark;
  738. fl6.fl6_dport = inet->inet_dport;
  739. fl6.fl6_sport = inet->inet_sport;
  740. fl6.flowi6_uid = sk->sk_uid;
  741. security_sk_classify_flow(sk, flowi6_to_flowi_common(&fl6));
  742. rcu_read_lock();
  743. final_p = fl6_update_dst(&fl6, rcu_dereference(np->opt),
  744. &final);
  745. rcu_read_unlock();
  746. dst = ip6_dst_lookup_flow(sock_net(sk), sk, &fl6, final_p);
  747. if (IS_ERR(dst)) {
  748. sk->sk_route_caps = 0;
  749. sk->sk_err_soft = -PTR_ERR(dst);
  750. return PTR_ERR(dst);
  751. }
  752. ip6_dst_store(sk, dst, NULL, NULL);
  753. }
  754. return 0;
  755. }
  756. EXPORT_SYMBOL_GPL(inet6_sk_rebuild_header);
  757. bool ipv6_opt_accepted(const struct sock *sk, const struct sk_buff *skb,
  758. const struct inet6_skb_parm *opt)
  759. {
  760. const struct ipv6_pinfo *np = inet6_sk(sk);
  761. if (np->rxopt.all) {
  762. if (((opt->flags & IP6SKB_HOPBYHOP) &&
  763. (np->rxopt.bits.hopopts || np->rxopt.bits.ohopopts)) ||
  764. (ip6_flowinfo((struct ipv6hdr *) skb_network_header(skb)) &&
  765. np->rxopt.bits.rxflow) ||
  766. (opt->srcrt && (np->rxopt.bits.srcrt ||
  767. np->rxopt.bits.osrcrt)) ||
  768. ((opt->dst1 || opt->dst0) &&
  769. (np->rxopt.bits.dstopts || np->rxopt.bits.odstopts)))
  770. return true;
  771. }
  772. return false;
  773. }
  774. EXPORT_SYMBOL_GPL(ipv6_opt_accepted);
  775. static struct packet_type ipv6_packet_type __read_mostly = {
  776. .type = cpu_to_be16(ETH_P_IPV6),
  777. .func = ipv6_rcv,
  778. .list_func = ipv6_list_rcv,
  779. };
  780. static int __init ipv6_packet_init(void)
  781. {
  782. dev_add_pack(&ipv6_packet_type);
  783. return 0;
  784. }
  785. static void ipv6_packet_cleanup(void)
  786. {
  787. dev_remove_pack(&ipv6_packet_type);
  788. }
  789. static int __net_init ipv6_init_mibs(struct net *net)
  790. {
  791. int i;
  792. net->mib.udp_stats_in6 = alloc_percpu(struct udp_mib);
  793. if (!net->mib.udp_stats_in6)
  794. return -ENOMEM;
  795. net->mib.udplite_stats_in6 = alloc_percpu(struct udp_mib);
  796. if (!net->mib.udplite_stats_in6)
  797. goto err_udplite_mib;
  798. net->mib.ipv6_statistics = alloc_percpu(struct ipstats_mib);
  799. if (!net->mib.ipv6_statistics)
  800. goto err_ip_mib;
  801. for_each_possible_cpu(i) {
  802. struct ipstats_mib *af_inet6_stats;
  803. af_inet6_stats = per_cpu_ptr(net->mib.ipv6_statistics, i);
  804. u64_stats_init(&af_inet6_stats->syncp);
  805. }
  806. net->mib.icmpv6_statistics = alloc_percpu(struct icmpv6_mib);
  807. if (!net->mib.icmpv6_statistics)
  808. goto err_icmp_mib;
  809. net->mib.icmpv6msg_statistics = kzalloc(sizeof(struct icmpv6msg_mib),
  810. GFP_KERNEL);
  811. if (!net->mib.icmpv6msg_statistics)
  812. goto err_icmpmsg_mib;
  813. return 0;
  814. err_icmpmsg_mib:
  815. free_percpu(net->mib.icmpv6_statistics);
  816. err_icmp_mib:
  817. free_percpu(net->mib.ipv6_statistics);
  818. err_ip_mib:
  819. free_percpu(net->mib.udplite_stats_in6);
  820. err_udplite_mib:
  821. free_percpu(net->mib.udp_stats_in6);
  822. return -ENOMEM;
  823. }
  824. static void ipv6_cleanup_mibs(struct net *net)
  825. {
  826. free_percpu(net->mib.udp_stats_in6);
  827. free_percpu(net->mib.udplite_stats_in6);
  828. free_percpu(net->mib.ipv6_statistics);
  829. free_percpu(net->mib.icmpv6_statistics);
  830. kfree(net->mib.icmpv6msg_statistics);
  831. }
  832. static int __net_init inet6_net_init(struct net *net)
  833. {
  834. int err = 0;
  835. net->ipv6.sysctl.bindv6only = 0;
  836. net->ipv6.sysctl.icmpv6_time = 1*HZ;
  837. net->ipv6.sysctl.icmpv6_echo_ignore_all = 0;
  838. net->ipv6.sysctl.icmpv6_echo_ignore_multicast = 0;
  839. net->ipv6.sysctl.icmpv6_echo_ignore_anycast = 0;
  840. /* By default, rate limit error messages.
  841. * Except for pmtu discovery, it would break it.
  842. * proc_do_large_bitmap needs pointer to the bitmap.
  843. */
  844. bitmap_set(net->ipv6.sysctl.icmpv6_ratemask, 0, ICMPV6_ERRMSG_MAX + 1);
  845. bitmap_clear(net->ipv6.sysctl.icmpv6_ratemask, ICMPV6_PKT_TOOBIG, 1);
  846. net->ipv6.sysctl.icmpv6_ratemask_ptr = net->ipv6.sysctl.icmpv6_ratemask;
  847. net->ipv6.sysctl.flowlabel_consistency = 1;
  848. net->ipv6.sysctl.auto_flowlabels = IP6_DEFAULT_AUTO_FLOW_LABELS;
  849. net->ipv6.sysctl.idgen_retries = 3;
  850. net->ipv6.sysctl.idgen_delay = 1 * HZ;
  851. net->ipv6.sysctl.flowlabel_state_ranges = 0;
  852. net->ipv6.sysctl.max_dst_opts_cnt = IP6_DEFAULT_MAX_DST_OPTS_CNT;
  853. net->ipv6.sysctl.max_hbh_opts_cnt = IP6_DEFAULT_MAX_HBH_OPTS_CNT;
  854. net->ipv6.sysctl.max_dst_opts_len = IP6_DEFAULT_MAX_DST_OPTS_LEN;
  855. net->ipv6.sysctl.max_hbh_opts_len = IP6_DEFAULT_MAX_HBH_OPTS_LEN;
  856. net->ipv6.sysctl.fib_notify_on_flag_change = 0;
  857. atomic_set(&net->ipv6.fib6_sernum, 1);
  858. net->ipv6.sysctl.ioam6_id = IOAM6_DEFAULT_ID;
  859. net->ipv6.sysctl.ioam6_id_wide = IOAM6_DEFAULT_ID_WIDE;
  860. err = ipv6_init_mibs(net);
  861. if (err)
  862. return err;
  863. #ifdef CONFIG_PROC_FS
  864. err = udp6_proc_init(net);
  865. if (err)
  866. goto out;
  867. err = tcp6_proc_init(net);
  868. if (err)
  869. goto proc_tcp6_fail;
  870. err = ac6_proc_init(net);
  871. if (err)
  872. goto proc_ac6_fail;
  873. #endif
  874. return err;
  875. #ifdef CONFIG_PROC_FS
  876. proc_ac6_fail:
  877. tcp6_proc_exit(net);
  878. proc_tcp6_fail:
  879. udp6_proc_exit(net);
  880. out:
  881. ipv6_cleanup_mibs(net);
  882. return err;
  883. #endif
  884. }
  885. static void __net_exit inet6_net_exit(struct net *net)
  886. {
  887. #ifdef CONFIG_PROC_FS
  888. udp6_proc_exit(net);
  889. tcp6_proc_exit(net);
  890. ac6_proc_exit(net);
  891. #endif
  892. ipv6_cleanup_mibs(net);
  893. }
  894. static struct pernet_operations inet6_net_ops = {
  895. .init = inet6_net_init,
  896. .exit = inet6_net_exit,
  897. };
  898. static int ipv6_route_input(struct sk_buff *skb)
  899. {
  900. ip6_route_input(skb);
  901. return skb_dst(skb)->error;
  902. }
  903. static const struct ipv6_stub ipv6_stub_impl = {
  904. .ipv6_sock_mc_join = ipv6_sock_mc_join,
  905. .ipv6_sock_mc_drop = ipv6_sock_mc_drop,
  906. .ipv6_dst_lookup_flow = ip6_dst_lookup_flow,
  907. .ipv6_route_input = ipv6_route_input,
  908. .fib6_get_table = fib6_get_table,
  909. .fib6_table_lookup = fib6_table_lookup,
  910. .fib6_lookup = fib6_lookup,
  911. .fib6_select_path = fib6_select_path,
  912. .ip6_mtu_from_fib6 = ip6_mtu_from_fib6,
  913. .fib6_nh_init = fib6_nh_init,
  914. .fib6_nh_release = fib6_nh_release,
  915. .fib6_nh_release_dsts = fib6_nh_release_dsts,
  916. .fib6_update_sernum = fib6_update_sernum_stub,
  917. .fib6_rt_update = fib6_rt_update,
  918. .ip6_del_rt = ip6_del_rt,
  919. .udpv6_encap_enable = udpv6_encap_enable,
  920. .ndisc_send_na = ndisc_send_na,
  921. #if IS_ENABLED(CONFIG_XFRM)
  922. .xfrm6_local_rxpmtu = xfrm6_local_rxpmtu,
  923. .xfrm6_udp_encap_rcv = xfrm6_udp_encap_rcv,
  924. .xfrm6_rcv_encap = xfrm6_rcv_encap,
  925. #endif
  926. .nd_tbl = &nd_tbl,
  927. .ipv6_fragment = ip6_fragment,
  928. .ipv6_dev_find = ipv6_dev_find,
  929. };
  930. static const struct ipv6_bpf_stub ipv6_bpf_stub_impl = {
  931. .inet6_bind = __inet6_bind,
  932. .udp6_lib_lookup = __udp6_lib_lookup,
  933. .ipv6_setsockopt = do_ipv6_setsockopt,
  934. .ipv6_getsockopt = do_ipv6_getsockopt,
  935. };
  936. static int __init inet6_init(void)
  937. {
  938. struct list_head *r;
  939. int err = 0;
  940. sock_skb_cb_check_size(sizeof(struct inet6_skb_parm));
  941. /* Register the socket-side information for inet6_create. */
  942. for (r = &inetsw6[0]; r < &inetsw6[SOCK_MAX]; ++r)
  943. INIT_LIST_HEAD(r);
  944. raw_hashinfo_init(&raw_v6_hashinfo);
  945. if (disable_ipv6_mod) {
  946. pr_info("Loaded, but administratively disabled, reboot required to enable\n");
  947. goto out;
  948. }
  949. err = proto_register(&tcpv6_prot, 1);
  950. if (err)
  951. goto out;
  952. err = proto_register(&udpv6_prot, 1);
  953. if (err)
  954. goto out_unregister_tcp_proto;
  955. err = proto_register(&udplitev6_prot, 1);
  956. if (err)
  957. goto out_unregister_udp_proto;
  958. err = proto_register(&rawv6_prot, 1);
  959. if (err)
  960. goto out_unregister_udplite_proto;
  961. err = proto_register(&pingv6_prot, 1);
  962. if (err)
  963. goto out_unregister_raw_proto;
  964. /* We MUST register RAW sockets before we create the ICMP6,
  965. * IGMP6, or NDISC control sockets.
  966. */
  967. err = rawv6_init();
  968. if (err)
  969. goto out_unregister_ping_proto;
  970. /* Register the family here so that the init calls below will
  971. * be able to create sockets. (?? is this dangerous ??)
  972. */
  973. err = sock_register(&inet6_family_ops);
  974. if (err)
  975. goto out_sock_register_fail;
  976. /*
  977. * ipngwg API draft makes clear that the correct semantics
  978. * for TCP and UDP is to consider one TCP and UDP instance
  979. * in a host available by both INET and INET6 APIs and
  980. * able to communicate via both network protocols.
  981. */
  982. err = register_pernet_subsys(&inet6_net_ops);
  983. if (err)
  984. goto register_pernet_fail;
  985. err = ip6_mr_init();
  986. if (err)
  987. goto ipmr_fail;
  988. err = icmpv6_init();
  989. if (err)
  990. goto icmp_fail;
  991. err = ndisc_init();
  992. if (err)
  993. goto ndisc_fail;
  994. err = igmp6_init();
  995. if (err)
  996. goto igmp_fail;
  997. err = ipv6_netfilter_init();
  998. if (err)
  999. goto netfilter_fail;
  1000. /* Create /proc/foo6 entries. */
  1001. #ifdef CONFIG_PROC_FS
  1002. err = -ENOMEM;
  1003. if (raw6_proc_init())
  1004. goto proc_raw6_fail;
  1005. if (udplite6_proc_init())
  1006. goto proc_udplite6_fail;
  1007. if (ipv6_misc_proc_init())
  1008. goto proc_misc6_fail;
  1009. if (if6_proc_init())
  1010. goto proc_if6_fail;
  1011. #endif
  1012. err = ip6_route_init();
  1013. if (err)
  1014. goto ip6_route_fail;
  1015. err = ndisc_late_init();
  1016. if (err)
  1017. goto ndisc_late_fail;
  1018. err = ip6_flowlabel_init();
  1019. if (err)
  1020. goto ip6_flowlabel_fail;
  1021. err = ipv6_anycast_init();
  1022. if (err)
  1023. goto ipv6_anycast_fail;
  1024. err = addrconf_init();
  1025. if (err)
  1026. goto addrconf_fail;
  1027. /* Init v6 extension headers. */
  1028. err = ipv6_exthdrs_init();
  1029. if (err)
  1030. goto ipv6_exthdrs_fail;
  1031. err = ipv6_frag_init();
  1032. if (err)
  1033. goto ipv6_frag_fail;
  1034. /* Init v6 transport protocols. */
  1035. err = udpv6_init();
  1036. if (err)
  1037. goto udpv6_fail;
  1038. err = udplitev6_init();
  1039. if (err)
  1040. goto udplitev6_fail;
  1041. err = udpv6_offload_init();
  1042. if (err)
  1043. goto udpv6_offload_fail;
  1044. err = tcpv6_init();
  1045. if (err)
  1046. goto tcpv6_fail;
  1047. err = ipv6_packet_init();
  1048. if (err)
  1049. goto ipv6_packet_fail;
  1050. err = pingv6_init();
  1051. if (err)
  1052. goto pingv6_fail;
  1053. err = calipso_init();
  1054. if (err)
  1055. goto calipso_fail;
  1056. err = seg6_init();
  1057. if (err)
  1058. goto seg6_fail;
  1059. err = rpl_init();
  1060. if (err)
  1061. goto rpl_fail;
  1062. err = ioam6_init();
  1063. if (err)
  1064. goto ioam6_fail;
  1065. err = igmp6_late_init();
  1066. if (err)
  1067. goto igmp6_late_err;
  1068. #ifdef CONFIG_SYSCTL
  1069. err = ipv6_sysctl_register();
  1070. if (err)
  1071. goto sysctl_fail;
  1072. #endif
  1073. /* ensure that ipv6 stubs are visible only after ipv6 is ready */
  1074. wmb();
  1075. ipv6_stub = &ipv6_stub_impl;
  1076. ipv6_bpf_stub = &ipv6_bpf_stub_impl;
  1077. out:
  1078. return err;
  1079. #ifdef CONFIG_SYSCTL
  1080. sysctl_fail:
  1081. igmp6_late_cleanup();
  1082. #endif
  1083. igmp6_late_err:
  1084. ioam6_exit();
  1085. ioam6_fail:
  1086. rpl_exit();
  1087. rpl_fail:
  1088. seg6_exit();
  1089. seg6_fail:
  1090. calipso_exit();
  1091. calipso_fail:
  1092. pingv6_exit();
  1093. pingv6_fail:
  1094. ipv6_packet_cleanup();
  1095. ipv6_packet_fail:
  1096. tcpv6_exit();
  1097. tcpv6_fail:
  1098. udpv6_offload_exit();
  1099. udpv6_offload_fail:
  1100. udplitev6_exit();
  1101. udplitev6_fail:
  1102. udpv6_exit();
  1103. udpv6_fail:
  1104. ipv6_frag_exit();
  1105. ipv6_frag_fail:
  1106. ipv6_exthdrs_exit();
  1107. ipv6_exthdrs_fail:
  1108. addrconf_cleanup();
  1109. addrconf_fail:
  1110. ipv6_anycast_cleanup();
  1111. ipv6_anycast_fail:
  1112. ip6_flowlabel_cleanup();
  1113. ip6_flowlabel_fail:
  1114. ndisc_late_cleanup();
  1115. ndisc_late_fail:
  1116. ip6_route_cleanup();
  1117. ip6_route_fail:
  1118. #ifdef CONFIG_PROC_FS
  1119. if6_proc_exit();
  1120. proc_if6_fail:
  1121. ipv6_misc_proc_exit();
  1122. proc_misc6_fail:
  1123. udplite6_proc_exit();
  1124. proc_udplite6_fail:
  1125. raw6_proc_exit();
  1126. proc_raw6_fail:
  1127. #endif
  1128. ipv6_netfilter_fini();
  1129. netfilter_fail:
  1130. igmp6_cleanup();
  1131. igmp_fail:
  1132. ndisc_cleanup();
  1133. ndisc_fail:
  1134. icmpv6_cleanup();
  1135. icmp_fail:
  1136. ip6_mr_cleanup();
  1137. ipmr_fail:
  1138. unregister_pernet_subsys(&inet6_net_ops);
  1139. register_pernet_fail:
  1140. sock_unregister(PF_INET6);
  1141. rtnl_unregister_all(PF_INET6);
  1142. out_sock_register_fail:
  1143. rawv6_exit();
  1144. out_unregister_ping_proto:
  1145. proto_unregister(&pingv6_prot);
  1146. out_unregister_raw_proto:
  1147. proto_unregister(&rawv6_prot);
  1148. out_unregister_udplite_proto:
  1149. proto_unregister(&udplitev6_prot);
  1150. out_unregister_udp_proto:
  1151. proto_unregister(&udpv6_prot);
  1152. out_unregister_tcp_proto:
  1153. proto_unregister(&tcpv6_prot);
  1154. goto out;
  1155. }
  1156. module_init(inet6_init);
  1157. MODULE_ALIAS_NETPROTO(PF_INET6);