af_inet6.c 24 KB

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