af_inet6.c 24 KB

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