af_inet6.c 25 KB

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