af_inet6.c 32 KB

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