af_inet6.c 27 KB

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