af_inet6.c 24 KB

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