af_inet6.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271
  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. #include <linux/module.h>
  21. #include <linux/capability.h>
  22. #include <linux/errno.h>
  23. #include <linux/types.h>
  24. #include <linux/socket.h>
  25. #include <linux/in.h>
  26. #include <linux/kernel.h>
  27. #include <linux/timer.h>
  28. #include <linux/string.h>
  29. #include <linux/sockios.h>
  30. #include <linux/net.h>
  31. #include <linux/fcntl.h>
  32. #include <linux/mm.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/proc_fs.h>
  35. #include <linux/stat.h>
  36. #include <linux/init.h>
  37. #include <linux/inet.h>
  38. #include <linux/netdevice.h>
  39. #include <linux/icmpv6.h>
  40. #include <linux/netfilter_ipv6.h>
  41. #include <net/ip.h>
  42. #include <net/ipv6.h>
  43. #include <net/udp.h>
  44. #include <net/udplite.h>
  45. #include <net/tcp.h>
  46. #include <net/ipip.h>
  47. #include <net/protocol.h>
  48. #include <net/inet_common.h>
  49. #include <net/route.h>
  50. #include <net/transp_v6.h>
  51. #include <net/ip6_route.h>
  52. #include <net/addrconf.h>
  53. #ifdef CONFIG_IPV6_TUNNEL
  54. #include <net/ip6_tunnel.h>
  55. #endif
  56. #include <asm/uaccess.h>
  57. #include <asm/system.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. static int disable_ipv6 = 0;
  68. module_param_named(disable, disable_ipv6, int, 0);
  69. MODULE_PARM_DESC(disable, "Disable IPv6 such that it is non-functional");
  70. static __inline__ struct ipv6_pinfo *inet6_sk_generic(struct sock *sk)
  71. {
  72. const int offset = sk->sk_prot->obj_size - sizeof(struct ipv6_pinfo);
  73. return (struct ipv6_pinfo *)(((u8 *)sk) + offset);
  74. }
  75. static int inet6_create(struct net *net, struct socket *sock, int protocol)
  76. {
  77. struct inet_sock *inet;
  78. struct ipv6_pinfo *np;
  79. struct sock *sk;
  80. struct inet_protosw *answer;
  81. struct proto *answer_prot;
  82. unsigned char answer_flags;
  83. char answer_no_check;
  84. int try_loading_module = 0;
  85. int err;
  86. if (sock->type != SOCK_RAW &&
  87. sock->type != SOCK_DGRAM &&
  88. !inet_ehash_secret)
  89. build_ehash_secret();
  90. /* Look for the requested type/protocol pair. */
  91. lookup_protocol:
  92. err = -ESOCKTNOSUPPORT;
  93. rcu_read_lock();
  94. list_for_each_entry_rcu(answer, &inetsw6[sock->type], list) {
  95. err = 0;
  96. /* Check the non-wild match. */
  97. if (protocol == answer->protocol) {
  98. if (protocol != IPPROTO_IP)
  99. break;
  100. } else {
  101. /* Check for the two wild cases. */
  102. if (IPPROTO_IP == protocol) {
  103. protocol = answer->protocol;
  104. break;
  105. }
  106. if (IPPROTO_IP == answer->protocol)
  107. break;
  108. }
  109. err = -EPROTONOSUPPORT;
  110. }
  111. if (err) {
  112. if (try_loading_module < 2) {
  113. rcu_read_unlock();
  114. /*
  115. * Be more specific, e.g. net-pf-10-proto-132-type-1
  116. * (net-pf-PF_INET6-proto-IPPROTO_SCTP-type-SOCK_STREAM)
  117. */
  118. if (++try_loading_module == 1)
  119. request_module("net-pf-%d-proto-%d-type-%d",
  120. PF_INET6, protocol, sock->type);
  121. /*
  122. * Fall back to generic, e.g. net-pf-10-proto-132
  123. * (net-pf-PF_INET6-proto-IPPROTO_SCTP)
  124. */
  125. else
  126. request_module("net-pf-%d-proto-%d",
  127. PF_INET6, protocol);
  128. goto lookup_protocol;
  129. } else
  130. goto out_rcu_unlock;
  131. }
  132. err = -EPERM;
  133. if (answer->capability > 0 && !capable(answer->capability))
  134. goto out_rcu_unlock;
  135. sock->ops = answer->ops;
  136. answer_prot = answer->prot;
  137. answer_no_check = answer->no_check;
  138. answer_flags = answer->flags;
  139. rcu_read_unlock();
  140. WARN_ON(answer_prot->slab == NULL);
  141. err = -ENOBUFS;
  142. sk = sk_alloc(net, PF_INET6, GFP_KERNEL, answer_prot);
  143. if (sk == NULL)
  144. goto out;
  145. sock_init_data(sock, sk);
  146. err = 0;
  147. sk->sk_no_check = answer_no_check;
  148. if (INET_PROTOSW_REUSE & answer_flags)
  149. sk->sk_reuse = 1;
  150. inet = inet_sk(sk);
  151. inet->is_icsk = (INET_PROTOSW_ICSK & answer_flags) != 0;
  152. if (SOCK_RAW == sock->type) {
  153. inet->num = protocol;
  154. if (IPPROTO_RAW == protocol)
  155. inet->hdrincl = 1;
  156. }
  157. sk->sk_destruct = inet_sock_destruct;
  158. sk->sk_family = PF_INET6;
  159. sk->sk_protocol = protocol;
  160. sk->sk_backlog_rcv = answer->prot->backlog_rcv;
  161. inet_sk(sk)->pinet6 = np = inet6_sk_generic(sk);
  162. np->hop_limit = -1;
  163. np->mcast_hops = -1;
  164. np->mc_loop = 1;
  165. np->pmtudisc = IPV6_PMTUDISC_WANT;
  166. np->ipv6only = net->ipv6.sysctl.bindv6only;
  167. /* Init the ipv4 part of the socket since we can have sockets
  168. * using v6 API for ipv4.
  169. */
  170. inet->uc_ttl = -1;
  171. inet->mc_loop = 1;
  172. inet->mc_ttl = 1;
  173. inet->mc_index = 0;
  174. inet->mc_list = NULL;
  175. if (ipv4_config.no_pmtu_disc)
  176. inet->pmtudisc = IP_PMTUDISC_DONT;
  177. else
  178. inet->pmtudisc = IP_PMTUDISC_WANT;
  179. /*
  180. * Increment only the relevant sk_prot->socks debug field, this changes
  181. * the previous behaviour of incrementing both the equivalent to
  182. * answer->prot->socks (inet6_sock_nr) and inet_sock_nr.
  183. *
  184. * This allows better debug granularity as we'll know exactly how many
  185. * UDPv6, TCPv6, etc socks were allocated, not the sum of all IPv6
  186. * transport protocol socks. -acme
  187. */
  188. sk_refcnt_debug_inc(sk);
  189. if (inet->num) {
  190. /* It assumes that any protocol which allows
  191. * the user to assign a number at socket
  192. * creation time automatically shares.
  193. */
  194. inet->sport = htons(inet->num);
  195. sk->sk_prot->hash(sk);
  196. }
  197. if (sk->sk_prot->init) {
  198. err = sk->sk_prot->init(sk);
  199. if (err) {
  200. sk_common_release(sk);
  201. goto out;
  202. }
  203. }
  204. out:
  205. return err;
  206. out_rcu_unlock:
  207. rcu_read_unlock();
  208. goto out;
  209. }
  210. /* bind for INET6 API */
  211. int inet6_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
  212. {
  213. struct sockaddr_in6 *addr=(struct sockaddr_in6 *)uaddr;
  214. struct sock *sk = sock->sk;
  215. struct inet_sock *inet = inet_sk(sk);
  216. struct ipv6_pinfo *np = inet6_sk(sk);
  217. struct net *net = sock_net(sk);
  218. __be32 v4addr = 0;
  219. unsigned short snum;
  220. int addr_type = 0;
  221. int err = 0;
  222. /* If the socket has its own bind function then use it. */
  223. if (sk->sk_prot->bind)
  224. return sk->sk_prot->bind(sk, uaddr, addr_len);
  225. if (addr_len < SIN6_LEN_RFC2133)
  226. return -EINVAL;
  227. addr_type = ipv6_addr_type(&addr->sin6_addr);
  228. if ((addr_type & IPV6_ADDR_MULTICAST) && sock->type == SOCK_STREAM)
  229. return -EINVAL;
  230. snum = ntohs(addr->sin6_port);
  231. if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
  232. return -EACCES;
  233. lock_sock(sk);
  234. /* Check these errors (active socket, double bind). */
  235. if (sk->sk_state != TCP_CLOSE || inet->num) {
  236. err = -EINVAL;
  237. goto out;
  238. }
  239. /* Check if the address belongs to the host. */
  240. if (addr_type == IPV6_ADDR_MAPPED) {
  241. int chk_addr_ret;
  242. /* Binding to v4-mapped address on a v6-only socket
  243. * makes no sense
  244. */
  245. if (np->ipv6only) {
  246. err = -EINVAL;
  247. goto out;
  248. }
  249. /* Reproduce AF_INET checks to make the bindings consitant */
  250. v4addr = addr->sin6_addr.s6_addr32[3];
  251. chk_addr_ret = inet_addr_type(net, v4addr);
  252. if (!sysctl_ip_nonlocal_bind &&
  253. !(inet->freebind || inet->transparent) &&
  254. v4addr != htonl(INADDR_ANY) &&
  255. chk_addr_ret != RTN_LOCAL &&
  256. chk_addr_ret != RTN_MULTICAST &&
  257. chk_addr_ret != RTN_BROADCAST)
  258. goto out;
  259. } else {
  260. if (addr_type != IPV6_ADDR_ANY) {
  261. struct net_device *dev = NULL;
  262. if (addr_type & IPV6_ADDR_LINKLOCAL) {
  263. if (addr_len >= sizeof(struct sockaddr_in6) &&
  264. addr->sin6_scope_id) {
  265. /* Override any existing binding, if another one
  266. * is supplied by user.
  267. */
  268. sk->sk_bound_dev_if = addr->sin6_scope_id;
  269. }
  270. /* Binding to link-local address requires an interface */
  271. if (!sk->sk_bound_dev_if) {
  272. err = -EINVAL;
  273. goto out;
  274. }
  275. dev = dev_get_by_index(net, sk->sk_bound_dev_if);
  276. if (!dev) {
  277. err = -ENODEV;
  278. goto out;
  279. }
  280. }
  281. /* ipv4 addr of the socket is invalid. Only the
  282. * unspecified and mapped address have a v4 equivalent.
  283. */
  284. v4addr = LOOPBACK4_IPV6;
  285. if (!(addr_type & IPV6_ADDR_MULTICAST)) {
  286. if (!ipv6_chk_addr(net, &addr->sin6_addr,
  287. dev, 0)) {
  288. if (dev)
  289. dev_put(dev);
  290. err = -EADDRNOTAVAIL;
  291. goto out;
  292. }
  293. }
  294. if (dev)
  295. dev_put(dev);
  296. }
  297. }
  298. inet->rcv_saddr = v4addr;
  299. inet->saddr = v4addr;
  300. ipv6_addr_copy(&np->rcv_saddr, &addr->sin6_addr);
  301. if (!(addr_type & IPV6_ADDR_MULTICAST))
  302. ipv6_addr_copy(&np->saddr, &addr->sin6_addr);
  303. /* Make sure we are allowed to bind here. */
  304. if (sk->sk_prot->get_port(sk, snum)) {
  305. inet_reset_saddr(sk);
  306. err = -EADDRINUSE;
  307. goto out;
  308. }
  309. if (addr_type != IPV6_ADDR_ANY) {
  310. sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
  311. if (addr_type != IPV6_ADDR_MAPPED)
  312. np->ipv6only = 1;
  313. }
  314. if (snum)
  315. sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
  316. inet->sport = htons(inet->num);
  317. inet->dport = 0;
  318. inet->daddr = 0;
  319. out:
  320. release_sock(sk);
  321. return err;
  322. }
  323. EXPORT_SYMBOL(inet6_bind);
  324. int inet6_release(struct socket *sock)
  325. {
  326. struct sock *sk = sock->sk;
  327. if (sk == NULL)
  328. return -EINVAL;
  329. /* Free mc lists */
  330. ipv6_sock_mc_close(sk);
  331. /* Free ac lists */
  332. ipv6_sock_ac_close(sk);
  333. return inet_release(sock);
  334. }
  335. EXPORT_SYMBOL(inet6_release);
  336. void inet6_destroy_sock(struct sock *sk)
  337. {
  338. struct ipv6_pinfo *np = inet6_sk(sk);
  339. struct sk_buff *skb;
  340. struct ipv6_txoptions *opt;
  341. /* Release rx options */
  342. if ((skb = xchg(&np->pktoptions, NULL)) != NULL)
  343. kfree_skb(skb);
  344. /* Free flowlabels */
  345. fl6_free_socklist(sk);
  346. /* Free tx options */
  347. if ((opt = xchg(&np->opt, NULL)) != NULL)
  348. sock_kfree_s(sk, opt, opt->tot_len);
  349. }
  350. EXPORT_SYMBOL_GPL(inet6_destroy_sock);
  351. /*
  352. * This does both peername and sockname.
  353. */
  354. int inet6_getname(struct socket *sock, struct sockaddr *uaddr,
  355. int *uaddr_len, int peer)
  356. {
  357. struct sockaddr_in6 *sin=(struct sockaddr_in6 *)uaddr;
  358. struct sock *sk = sock->sk;
  359. struct inet_sock *inet = inet_sk(sk);
  360. struct ipv6_pinfo *np = inet6_sk(sk);
  361. sin->sin6_family = AF_INET6;
  362. sin->sin6_flowinfo = 0;
  363. sin->sin6_scope_id = 0;
  364. if (peer) {
  365. if (!inet->dport)
  366. return -ENOTCONN;
  367. if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
  368. peer == 1)
  369. return -ENOTCONN;
  370. sin->sin6_port = inet->dport;
  371. ipv6_addr_copy(&sin->sin6_addr, &np->daddr);
  372. if (np->sndflow)
  373. sin->sin6_flowinfo = np->flow_label;
  374. } else {
  375. if (ipv6_addr_any(&np->rcv_saddr))
  376. ipv6_addr_copy(&sin->sin6_addr, &np->saddr);
  377. else
  378. ipv6_addr_copy(&sin->sin6_addr, &np->rcv_saddr);
  379. sin->sin6_port = inet->sport;
  380. }
  381. if (ipv6_addr_type(&sin->sin6_addr) & IPV6_ADDR_LINKLOCAL)
  382. sin->sin6_scope_id = sk->sk_bound_dev_if;
  383. *uaddr_len = sizeof(*sin);
  384. return(0);
  385. }
  386. EXPORT_SYMBOL(inet6_getname);
  387. int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  388. {
  389. struct sock *sk = sock->sk;
  390. struct net *net = sock_net(sk);
  391. switch(cmd)
  392. {
  393. case SIOCGSTAMP:
  394. return sock_get_timestamp(sk, (struct timeval __user *)arg);
  395. case SIOCGSTAMPNS:
  396. return sock_get_timestampns(sk, (struct timespec __user *)arg);
  397. case SIOCADDRT:
  398. case SIOCDELRT:
  399. return(ipv6_route_ioctl(net, cmd, (void __user *)arg));
  400. case SIOCSIFADDR:
  401. return addrconf_add_ifaddr(net, (void __user *) arg);
  402. case SIOCDIFADDR:
  403. return addrconf_del_ifaddr(net, (void __user *) arg);
  404. case SIOCSIFDSTADDR:
  405. return addrconf_set_dstaddr(net, (void __user *) arg);
  406. default:
  407. if (!sk->sk_prot->ioctl)
  408. return -ENOIOCTLCMD;
  409. return sk->sk_prot->ioctl(sk, cmd, arg);
  410. }
  411. /*NOTREACHED*/
  412. return(0);
  413. }
  414. EXPORT_SYMBOL(inet6_ioctl);
  415. const struct proto_ops inet6_stream_ops = {
  416. .family = PF_INET6,
  417. .owner = THIS_MODULE,
  418. .release = inet6_release,
  419. .bind = inet6_bind,
  420. .connect = inet_stream_connect, /* ok */
  421. .socketpair = sock_no_socketpair, /* a do nothing */
  422. .accept = inet_accept, /* ok */
  423. .getname = inet6_getname,
  424. .poll = tcp_poll, /* ok */
  425. .ioctl = inet6_ioctl, /* must change */
  426. .listen = inet_listen, /* ok */
  427. .shutdown = inet_shutdown, /* ok */
  428. .setsockopt = sock_common_setsockopt, /* ok */
  429. .getsockopt = sock_common_getsockopt, /* ok */
  430. .sendmsg = tcp_sendmsg, /* ok */
  431. .recvmsg = sock_common_recvmsg, /* ok */
  432. .mmap = sock_no_mmap,
  433. .sendpage = tcp_sendpage,
  434. .splice_read = tcp_splice_read,
  435. #ifdef CONFIG_COMPAT
  436. .compat_setsockopt = compat_sock_common_setsockopt,
  437. .compat_getsockopt = compat_sock_common_getsockopt,
  438. #endif
  439. };
  440. const struct proto_ops inet6_dgram_ops = {
  441. .family = PF_INET6,
  442. .owner = THIS_MODULE,
  443. .release = inet6_release,
  444. .bind = inet6_bind,
  445. .connect = inet_dgram_connect, /* ok */
  446. .socketpair = sock_no_socketpair, /* a do nothing */
  447. .accept = sock_no_accept, /* a do nothing */
  448. .getname = inet6_getname,
  449. .poll = udp_poll, /* ok */
  450. .ioctl = inet6_ioctl, /* must change */
  451. .listen = sock_no_listen, /* ok */
  452. .shutdown = inet_shutdown, /* ok */
  453. .setsockopt = sock_common_setsockopt, /* ok */
  454. .getsockopt = sock_common_getsockopt, /* ok */
  455. .sendmsg = inet_sendmsg, /* ok */
  456. .recvmsg = sock_common_recvmsg, /* ok */
  457. .mmap = sock_no_mmap,
  458. .sendpage = sock_no_sendpage,
  459. #ifdef CONFIG_COMPAT
  460. .compat_setsockopt = compat_sock_common_setsockopt,
  461. .compat_getsockopt = compat_sock_common_getsockopt,
  462. #endif
  463. };
  464. static struct net_proto_family inet6_family_ops = {
  465. .family = PF_INET6,
  466. .create = inet6_create,
  467. .owner = THIS_MODULE,
  468. };
  469. int inet6_register_protosw(struct inet_protosw *p)
  470. {
  471. struct list_head *lh;
  472. struct inet_protosw *answer;
  473. struct list_head *last_perm;
  474. int protocol = p->protocol;
  475. int ret;
  476. spin_lock_bh(&inetsw6_lock);
  477. ret = -EINVAL;
  478. if (p->type >= SOCK_MAX)
  479. goto out_illegal;
  480. /* If we are trying to override a permanent protocol, bail. */
  481. answer = NULL;
  482. ret = -EPERM;
  483. last_perm = &inetsw6[p->type];
  484. list_for_each(lh, &inetsw6[p->type]) {
  485. answer = list_entry(lh, struct inet_protosw, list);
  486. /* Check only the non-wild match. */
  487. if (INET_PROTOSW_PERMANENT & answer->flags) {
  488. if (protocol == answer->protocol)
  489. break;
  490. last_perm = lh;
  491. }
  492. answer = NULL;
  493. }
  494. if (answer)
  495. goto out_permanent;
  496. /* Add the new entry after the last permanent entry if any, so that
  497. * the new entry does not override a permanent entry when matched with
  498. * a wild-card protocol. But it is allowed to override any existing
  499. * non-permanent entry. This means that when we remove this entry, the
  500. * system automatically returns to the old behavior.
  501. */
  502. list_add_rcu(&p->list, last_perm);
  503. ret = 0;
  504. out:
  505. spin_unlock_bh(&inetsw6_lock);
  506. return ret;
  507. out_permanent:
  508. printk(KERN_ERR "Attempt to override permanent protocol %d.\n",
  509. protocol);
  510. goto out;
  511. out_illegal:
  512. printk(KERN_ERR
  513. "Ignoring attempt to register invalid socket type %d.\n",
  514. p->type);
  515. goto out;
  516. }
  517. EXPORT_SYMBOL(inet6_register_protosw);
  518. void
  519. inet6_unregister_protosw(struct inet_protosw *p)
  520. {
  521. if (INET_PROTOSW_PERMANENT & p->flags) {
  522. printk(KERN_ERR
  523. "Attempt to unregister permanent protocol %d.\n",
  524. p->protocol);
  525. } else {
  526. spin_lock_bh(&inetsw6_lock);
  527. list_del_rcu(&p->list);
  528. spin_unlock_bh(&inetsw6_lock);
  529. synchronize_net();
  530. }
  531. }
  532. EXPORT_SYMBOL(inet6_unregister_protosw);
  533. int inet6_sk_rebuild_header(struct sock *sk)
  534. {
  535. int err;
  536. struct dst_entry *dst;
  537. struct ipv6_pinfo *np = inet6_sk(sk);
  538. dst = __sk_dst_check(sk, np->dst_cookie);
  539. if (dst == NULL) {
  540. struct inet_sock *inet = inet_sk(sk);
  541. struct in6_addr *final_p = NULL, final;
  542. struct flowi fl;
  543. memset(&fl, 0, sizeof(fl));
  544. fl.proto = sk->sk_protocol;
  545. ipv6_addr_copy(&fl.fl6_dst, &np->daddr);
  546. ipv6_addr_copy(&fl.fl6_src, &np->saddr);
  547. fl.fl6_flowlabel = np->flow_label;
  548. fl.oif = sk->sk_bound_dev_if;
  549. fl.fl_ip_dport = inet->dport;
  550. fl.fl_ip_sport = inet->sport;
  551. security_sk_classify_flow(sk, &fl);
  552. if (np->opt && np->opt->srcrt) {
  553. struct rt0_hdr *rt0 = (struct rt0_hdr *) np->opt->srcrt;
  554. ipv6_addr_copy(&final, &fl.fl6_dst);
  555. ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
  556. final_p = &final;
  557. }
  558. err = ip6_dst_lookup(sk, &dst, &fl);
  559. if (err) {
  560. sk->sk_route_caps = 0;
  561. return err;
  562. }
  563. if (final_p)
  564. ipv6_addr_copy(&fl.fl6_dst, final_p);
  565. if ((err = xfrm_lookup(sock_net(sk), &dst, &fl, sk, 0)) < 0) {
  566. sk->sk_err_soft = -err;
  567. return err;
  568. }
  569. __ip6_dst_store(sk, dst, NULL, NULL);
  570. }
  571. return 0;
  572. }
  573. EXPORT_SYMBOL_GPL(inet6_sk_rebuild_header);
  574. int ipv6_opt_accepted(struct sock *sk, struct sk_buff *skb)
  575. {
  576. struct ipv6_pinfo *np = inet6_sk(sk);
  577. struct inet6_skb_parm *opt = IP6CB(skb);
  578. if (np->rxopt.all) {
  579. if ((opt->hop && (np->rxopt.bits.hopopts ||
  580. np->rxopt.bits.ohopopts)) ||
  581. ((IPV6_FLOWINFO_MASK &
  582. *(__be32 *)skb_network_header(skb)) &&
  583. np->rxopt.bits.rxflow) ||
  584. (opt->srcrt && (np->rxopt.bits.srcrt ||
  585. np->rxopt.bits.osrcrt)) ||
  586. ((opt->dst1 || opt->dst0) &&
  587. (np->rxopt.bits.dstopts || np->rxopt.bits.odstopts)))
  588. return 1;
  589. }
  590. return 0;
  591. }
  592. EXPORT_SYMBOL_GPL(ipv6_opt_accepted);
  593. static int ipv6_gso_pull_exthdrs(struct sk_buff *skb, int proto)
  594. {
  595. struct inet6_protocol *ops = NULL;
  596. for (;;) {
  597. struct ipv6_opt_hdr *opth;
  598. int len;
  599. if (proto != NEXTHDR_HOP) {
  600. ops = rcu_dereference(inet6_protos[proto]);
  601. if (unlikely(!ops))
  602. break;
  603. if (!(ops->flags & INET6_PROTO_GSO_EXTHDR))
  604. break;
  605. }
  606. if (unlikely(!pskb_may_pull(skb, 8)))
  607. break;
  608. opth = (void *)skb->data;
  609. len = ipv6_optlen(opth);
  610. if (unlikely(!pskb_may_pull(skb, len)))
  611. break;
  612. proto = opth->nexthdr;
  613. __skb_pull(skb, len);
  614. }
  615. return proto;
  616. }
  617. static int ipv6_gso_send_check(struct sk_buff *skb)
  618. {
  619. struct ipv6hdr *ipv6h;
  620. struct inet6_protocol *ops;
  621. int err = -EINVAL;
  622. if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h))))
  623. goto out;
  624. ipv6h = ipv6_hdr(skb);
  625. __skb_pull(skb, sizeof(*ipv6h));
  626. err = -EPROTONOSUPPORT;
  627. rcu_read_lock();
  628. ops = rcu_dereference(inet6_protos[
  629. ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr)]);
  630. if (likely(ops && ops->gso_send_check)) {
  631. skb_reset_transport_header(skb);
  632. err = ops->gso_send_check(skb);
  633. }
  634. rcu_read_unlock();
  635. out:
  636. return err;
  637. }
  638. static struct sk_buff *ipv6_gso_segment(struct sk_buff *skb, int features)
  639. {
  640. struct sk_buff *segs = ERR_PTR(-EINVAL);
  641. struct ipv6hdr *ipv6h;
  642. struct inet6_protocol *ops;
  643. if (!(features & NETIF_F_V6_CSUM))
  644. features &= ~NETIF_F_SG;
  645. if (unlikely(skb_shinfo(skb)->gso_type &
  646. ~(SKB_GSO_UDP |
  647. SKB_GSO_DODGY |
  648. SKB_GSO_TCP_ECN |
  649. SKB_GSO_TCPV6 |
  650. 0)))
  651. goto out;
  652. if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h))))
  653. goto out;
  654. ipv6h = ipv6_hdr(skb);
  655. __skb_pull(skb, sizeof(*ipv6h));
  656. segs = ERR_PTR(-EPROTONOSUPPORT);
  657. rcu_read_lock();
  658. ops = rcu_dereference(inet6_protos[
  659. ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr)]);
  660. if (likely(ops && ops->gso_segment)) {
  661. skb_reset_transport_header(skb);
  662. segs = ops->gso_segment(skb, features);
  663. }
  664. rcu_read_unlock();
  665. if (unlikely(IS_ERR(segs)))
  666. goto out;
  667. for (skb = segs; skb; skb = skb->next) {
  668. ipv6h = ipv6_hdr(skb);
  669. ipv6h->payload_len = htons(skb->len - skb->mac_len -
  670. sizeof(*ipv6h));
  671. }
  672. out:
  673. return segs;
  674. }
  675. struct ipv6_gro_cb {
  676. struct napi_gro_cb napi;
  677. int proto;
  678. };
  679. #define IPV6_GRO_CB(skb) ((struct ipv6_gro_cb *)(skb)->cb)
  680. static struct sk_buff **ipv6_gro_receive(struct sk_buff **head,
  681. struct sk_buff *skb)
  682. {
  683. struct inet6_protocol *ops;
  684. struct sk_buff **pp = NULL;
  685. struct sk_buff *p;
  686. struct ipv6hdr *iph;
  687. unsigned int nlen;
  688. int flush = 1;
  689. int proto;
  690. __wsum csum;
  691. iph = skb_gro_header(skb, sizeof(*iph));
  692. if (unlikely(!iph))
  693. goto out;
  694. skb_gro_pull(skb, sizeof(*iph));
  695. skb_set_transport_header(skb, skb_gro_offset(skb));
  696. flush += ntohs(iph->payload_len) != skb_gro_len(skb);
  697. rcu_read_lock();
  698. proto = iph->nexthdr;
  699. ops = rcu_dereference(inet6_protos[proto]);
  700. if (!ops || !ops->gro_receive) {
  701. __pskb_pull(skb, skb_gro_offset(skb));
  702. proto = ipv6_gso_pull_exthdrs(skb, proto);
  703. skb_gro_pull(skb, -skb_transport_offset(skb));
  704. skb_reset_transport_header(skb);
  705. __skb_push(skb, skb_gro_offset(skb));
  706. if (!ops || !ops->gro_receive)
  707. goto out_unlock;
  708. iph = ipv6_hdr(skb);
  709. }
  710. IPV6_GRO_CB(skb)->proto = proto;
  711. flush--;
  712. nlen = skb_network_header_len(skb);
  713. for (p = *head; p; p = p->next) {
  714. struct ipv6hdr *iph2;
  715. if (!NAPI_GRO_CB(p)->same_flow)
  716. continue;
  717. iph2 = ipv6_hdr(p);
  718. /* All fields must match except length. */
  719. if (nlen != skb_network_header_len(p) ||
  720. memcmp(iph, iph2, offsetof(struct ipv6hdr, payload_len)) ||
  721. memcmp(&iph->nexthdr, &iph2->nexthdr,
  722. nlen - offsetof(struct ipv6hdr, nexthdr))) {
  723. NAPI_GRO_CB(p)->same_flow = 0;
  724. continue;
  725. }
  726. NAPI_GRO_CB(p)->flush |= flush;
  727. }
  728. NAPI_GRO_CB(skb)->flush |= flush;
  729. csum = skb->csum;
  730. skb_postpull_rcsum(skb, iph, skb_network_header_len(skb));
  731. pp = ops->gro_receive(head, skb);
  732. skb->csum = csum;
  733. out_unlock:
  734. rcu_read_unlock();
  735. out:
  736. NAPI_GRO_CB(skb)->flush |= flush;
  737. return pp;
  738. }
  739. static int ipv6_gro_complete(struct sk_buff *skb)
  740. {
  741. struct inet6_protocol *ops;
  742. struct ipv6hdr *iph = ipv6_hdr(skb);
  743. int err = -ENOSYS;
  744. iph->payload_len = htons(skb->len - skb_network_offset(skb) -
  745. sizeof(*iph));
  746. rcu_read_lock();
  747. ops = rcu_dereference(inet6_protos[IPV6_GRO_CB(skb)->proto]);
  748. if (WARN_ON(!ops || !ops->gro_complete))
  749. goto out_unlock;
  750. err = ops->gro_complete(skb);
  751. out_unlock:
  752. rcu_read_unlock();
  753. return err;
  754. }
  755. static struct packet_type ipv6_packet_type __read_mostly = {
  756. .type = cpu_to_be16(ETH_P_IPV6),
  757. .func = ipv6_rcv,
  758. .gso_send_check = ipv6_gso_send_check,
  759. .gso_segment = ipv6_gso_segment,
  760. .gro_receive = ipv6_gro_receive,
  761. .gro_complete = ipv6_gro_complete,
  762. };
  763. static int __init ipv6_packet_init(void)
  764. {
  765. dev_add_pack(&ipv6_packet_type);
  766. return 0;
  767. }
  768. static void ipv6_packet_cleanup(void)
  769. {
  770. dev_remove_pack(&ipv6_packet_type);
  771. }
  772. static int __net_init ipv6_init_mibs(struct net *net)
  773. {
  774. if (snmp_mib_init((void **)net->mib.udp_stats_in6,
  775. sizeof (struct udp_mib)) < 0)
  776. return -ENOMEM;
  777. if (snmp_mib_init((void **)net->mib.udplite_stats_in6,
  778. sizeof (struct udp_mib)) < 0)
  779. goto err_udplite_mib;
  780. if (snmp_mib_init((void **)net->mib.ipv6_statistics,
  781. sizeof(struct ipstats_mib)) < 0)
  782. goto err_ip_mib;
  783. if (snmp_mib_init((void **)net->mib.icmpv6_statistics,
  784. sizeof(struct icmpv6_mib)) < 0)
  785. goto err_icmp_mib;
  786. if (snmp_mib_init((void **)net->mib.icmpv6msg_statistics,
  787. sizeof(struct icmpv6msg_mib)) < 0)
  788. goto err_icmpmsg_mib;
  789. return 0;
  790. err_icmpmsg_mib:
  791. snmp_mib_free((void **)net->mib.icmpv6_statistics);
  792. err_icmp_mib:
  793. snmp_mib_free((void **)net->mib.ipv6_statistics);
  794. err_ip_mib:
  795. snmp_mib_free((void **)net->mib.udplite_stats_in6);
  796. err_udplite_mib:
  797. snmp_mib_free((void **)net->mib.udp_stats_in6);
  798. return -ENOMEM;
  799. }
  800. static void __net_exit ipv6_cleanup_mibs(struct net *net)
  801. {
  802. snmp_mib_free((void **)net->mib.udp_stats_in6);
  803. snmp_mib_free((void **)net->mib.udplite_stats_in6);
  804. snmp_mib_free((void **)net->mib.ipv6_statistics);
  805. snmp_mib_free((void **)net->mib.icmpv6_statistics);
  806. snmp_mib_free((void **)net->mib.icmpv6msg_statistics);
  807. }
  808. static int __net_init inet6_net_init(struct net *net)
  809. {
  810. int err = 0;
  811. net->ipv6.sysctl.bindv6only = 0;
  812. net->ipv6.sysctl.icmpv6_time = 1*HZ;
  813. err = ipv6_init_mibs(net);
  814. if (err)
  815. return err;
  816. #ifdef CONFIG_PROC_FS
  817. err = udp6_proc_init(net);
  818. if (err)
  819. goto out;
  820. err = tcp6_proc_init(net);
  821. if (err)
  822. goto proc_tcp6_fail;
  823. err = ac6_proc_init(net);
  824. if (err)
  825. goto proc_ac6_fail;
  826. #endif
  827. return err;
  828. #ifdef CONFIG_PROC_FS
  829. proc_ac6_fail:
  830. tcp6_proc_exit(net);
  831. proc_tcp6_fail:
  832. udp6_proc_exit(net);
  833. out:
  834. ipv6_cleanup_mibs(net);
  835. return err;
  836. #endif
  837. }
  838. static void inet6_net_exit(struct net *net)
  839. {
  840. #ifdef CONFIG_PROC_FS
  841. udp6_proc_exit(net);
  842. tcp6_proc_exit(net);
  843. ac6_proc_exit(net);
  844. #endif
  845. ipv6_cleanup_mibs(net);
  846. }
  847. static struct pernet_operations inet6_net_ops = {
  848. .init = inet6_net_init,
  849. .exit = inet6_net_exit,
  850. };
  851. static int __init inet6_init(void)
  852. {
  853. struct sk_buff *dummy_skb;
  854. struct list_head *r;
  855. int err = 0;
  856. BUILD_BUG_ON(sizeof(struct inet6_skb_parm) > sizeof(dummy_skb->cb));
  857. /* Register the socket-side information for inet6_create. */
  858. for(r = &inetsw6[0]; r < &inetsw6[SOCK_MAX]; ++r)
  859. INIT_LIST_HEAD(r);
  860. if (disable_ipv6) {
  861. printk(KERN_INFO
  862. "IPv6: Loaded, but administratively disabled, "
  863. "reboot required to enable\n");
  864. goto out;
  865. }
  866. err = proto_register(&tcpv6_prot, 1);
  867. if (err)
  868. goto out;
  869. err = proto_register(&udpv6_prot, 1);
  870. if (err)
  871. goto out_unregister_tcp_proto;
  872. err = proto_register(&udplitev6_prot, 1);
  873. if (err)
  874. goto out_unregister_udp_proto;
  875. err = proto_register(&rawv6_prot, 1);
  876. if (err)
  877. goto out_unregister_udplite_proto;
  878. /* We MUST register RAW sockets before we create the ICMP6,
  879. * IGMP6, or NDISC control sockets.
  880. */
  881. err = rawv6_init();
  882. if (err)
  883. goto out_unregister_raw_proto;
  884. /* Register the family here so that the init calls below will
  885. * be able to create sockets. (?? is this dangerous ??)
  886. */
  887. err = sock_register(&inet6_family_ops);
  888. if (err)
  889. goto out_sock_register_fail;
  890. #ifdef CONFIG_SYSCTL
  891. err = ipv6_static_sysctl_register();
  892. if (err)
  893. goto static_sysctl_fail;
  894. #endif
  895. /*
  896. * ipngwg API draft makes clear that the correct semantics
  897. * for TCP and UDP is to consider one TCP and UDP instance
  898. * in a host availiable by both INET and INET6 APIs and
  899. * able to communicate via both network protocols.
  900. */
  901. err = register_pernet_subsys(&inet6_net_ops);
  902. if (err)
  903. goto register_pernet_fail;
  904. err = icmpv6_init();
  905. if (err)
  906. goto icmp_fail;
  907. err = ip6_mr_init();
  908. if (err)
  909. goto ipmr_fail;
  910. err = ndisc_init();
  911. if (err)
  912. goto ndisc_fail;
  913. err = igmp6_init();
  914. if (err)
  915. goto igmp_fail;
  916. err = ipv6_netfilter_init();
  917. if (err)
  918. goto netfilter_fail;
  919. /* Create /proc/foo6 entries. */
  920. #ifdef CONFIG_PROC_FS
  921. err = -ENOMEM;
  922. if (raw6_proc_init())
  923. goto proc_raw6_fail;
  924. if (udplite6_proc_init())
  925. goto proc_udplite6_fail;
  926. if (ipv6_misc_proc_init())
  927. goto proc_misc6_fail;
  928. if (if6_proc_init())
  929. goto proc_if6_fail;
  930. #endif
  931. err = ip6_route_init();
  932. if (err)
  933. goto ip6_route_fail;
  934. err = ip6_flowlabel_init();
  935. if (err)
  936. goto ip6_flowlabel_fail;
  937. err = addrconf_init();
  938. if (err)
  939. goto addrconf_fail;
  940. /* Init v6 extension headers. */
  941. err = ipv6_exthdrs_init();
  942. if (err)
  943. goto ipv6_exthdrs_fail;
  944. err = ipv6_frag_init();
  945. if (err)
  946. goto ipv6_frag_fail;
  947. /* Init v6 transport protocols. */
  948. err = udpv6_init();
  949. if (err)
  950. goto udpv6_fail;
  951. err = udplitev6_init();
  952. if (err)
  953. goto udplitev6_fail;
  954. err = tcpv6_init();
  955. if (err)
  956. goto tcpv6_fail;
  957. err = ipv6_packet_init();
  958. if (err)
  959. goto ipv6_packet_fail;
  960. #ifdef CONFIG_SYSCTL
  961. err = ipv6_sysctl_register();
  962. if (err)
  963. goto sysctl_fail;
  964. #endif
  965. out:
  966. return err;
  967. #ifdef CONFIG_SYSCTL
  968. sysctl_fail:
  969. ipv6_packet_cleanup();
  970. #endif
  971. ipv6_packet_fail:
  972. tcpv6_exit();
  973. tcpv6_fail:
  974. udplitev6_exit();
  975. udplitev6_fail:
  976. udpv6_exit();
  977. udpv6_fail:
  978. ipv6_frag_exit();
  979. ipv6_frag_fail:
  980. ipv6_exthdrs_exit();
  981. ipv6_exthdrs_fail:
  982. addrconf_cleanup();
  983. addrconf_fail:
  984. ip6_flowlabel_cleanup();
  985. ip6_flowlabel_fail:
  986. ip6_route_cleanup();
  987. ip6_route_fail:
  988. #ifdef CONFIG_PROC_FS
  989. if6_proc_exit();
  990. proc_if6_fail:
  991. ipv6_misc_proc_exit();
  992. proc_misc6_fail:
  993. udplite6_proc_exit();
  994. proc_udplite6_fail:
  995. raw6_proc_exit();
  996. proc_raw6_fail:
  997. #endif
  998. ipv6_netfilter_fini();
  999. netfilter_fail:
  1000. igmp6_cleanup();
  1001. igmp_fail:
  1002. ndisc_cleanup();
  1003. ndisc_fail:
  1004. ip6_mr_cleanup();
  1005. ipmr_fail:
  1006. icmpv6_cleanup();
  1007. icmp_fail:
  1008. unregister_pernet_subsys(&inet6_net_ops);
  1009. register_pernet_fail:
  1010. #ifdef CONFIG_SYSCTL
  1011. ipv6_static_sysctl_unregister();
  1012. static_sysctl_fail:
  1013. #endif
  1014. sock_unregister(PF_INET6);
  1015. rtnl_unregister_all(PF_INET6);
  1016. out_sock_register_fail:
  1017. rawv6_exit();
  1018. out_unregister_raw_proto:
  1019. proto_unregister(&rawv6_prot);
  1020. out_unregister_udplite_proto:
  1021. proto_unregister(&udplitev6_prot);
  1022. out_unregister_udp_proto:
  1023. proto_unregister(&udpv6_prot);
  1024. out_unregister_tcp_proto:
  1025. proto_unregister(&tcpv6_prot);
  1026. goto out;
  1027. }
  1028. module_init(inet6_init);
  1029. static void __exit inet6_exit(void)
  1030. {
  1031. if (disable_ipv6)
  1032. return;
  1033. /* First of all disallow new sockets creation. */
  1034. sock_unregister(PF_INET6);
  1035. /* Disallow any further netlink messages */
  1036. rtnl_unregister_all(PF_INET6);
  1037. #ifdef CONFIG_SYSCTL
  1038. ipv6_sysctl_unregister();
  1039. #endif
  1040. udpv6_exit();
  1041. udplitev6_exit();
  1042. tcpv6_exit();
  1043. /* Cleanup code parts. */
  1044. ipv6_packet_cleanup();
  1045. ipv6_frag_exit();
  1046. ipv6_exthdrs_exit();
  1047. addrconf_cleanup();
  1048. ip6_flowlabel_cleanup();
  1049. ip6_route_cleanup();
  1050. #ifdef CONFIG_PROC_FS
  1051. /* Cleanup code parts. */
  1052. if6_proc_exit();
  1053. ipv6_misc_proc_exit();
  1054. udplite6_proc_exit();
  1055. raw6_proc_exit();
  1056. #endif
  1057. ipv6_netfilter_fini();
  1058. igmp6_cleanup();
  1059. ndisc_cleanup();
  1060. ip6_mr_cleanup();
  1061. icmpv6_cleanup();
  1062. rawv6_exit();
  1063. unregister_pernet_subsys(&inet6_net_ops);
  1064. #ifdef CONFIG_SYSCTL
  1065. ipv6_static_sysctl_unregister();
  1066. #endif
  1067. proto_unregister(&rawv6_prot);
  1068. proto_unregister(&udplitev6_prot);
  1069. proto_unregister(&udpv6_prot);
  1070. proto_unregister(&tcpv6_prot);
  1071. }
  1072. module_exit(inet6_exit);
  1073. MODULE_ALIAS_NETPROTO(PF_INET6);