af_inet6.c 25 KB

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