af_inet6.c 32 KB

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