af_inet6.c 25 KB

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