af_inet6.c 27 KB

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