af_inet6.c 25 KB

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