af_inet6.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124
  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. * $Id: af_inet6.c,v 1.66 2002/02/01 22:01:04 davem Exp $
  11. *
  12. * Fixes:
  13. * piggy, Karl Knutson : Socket protocol table
  14. * Hideaki YOSHIFUJI : sin6_scope_id support
  15. * Arnaldo Melo : check proc_net_create return, cleanups
  16. *
  17. * This program is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU General Public License
  19. * as published by the Free Software Foundation; either version
  20. * 2 of the License, or (at your option) any later version.
  21. */
  22. #include <linux/module.h>
  23. #include <linux/capability.h>
  24. #include <linux/errno.h>
  25. #include <linux/types.h>
  26. #include <linux/socket.h>
  27. #include <linux/in.h>
  28. #include <linux/kernel.h>
  29. #include <linux/timer.h>
  30. #include <linux/string.h>
  31. #include <linux/sockios.h>
  32. #include <linux/net.h>
  33. #include <linux/fcntl.h>
  34. #include <linux/mm.h>
  35. #include <linux/interrupt.h>
  36. #include <linux/proc_fs.h>
  37. #include <linux/stat.h>
  38. #include <linux/init.h>
  39. #include <linux/inet.h>
  40. #include <linux/netdevice.h>
  41. #include <linux/icmpv6.h>
  42. #include <linux/netfilter_ipv6.h>
  43. #include <net/ip.h>
  44. #include <net/ipv6.h>
  45. #include <net/udp.h>
  46. #include <net/udplite.h>
  47. #include <net/tcp.h>
  48. #include <net/ipip.h>
  49. #include <net/protocol.h>
  50. #include <net/inet_common.h>
  51. #include <net/transp_v6.h>
  52. #include <net/ip6_route.h>
  53. #include <net/addrconf.h>
  54. #ifdef CONFIG_IPV6_TUNNEL
  55. #include <net/ip6_tunnel.h>
  56. #endif
  57. #include <asm/uaccess.h>
  58. #include <asm/system.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 list_head *p;
  78. struct inet_protosw *answer;
  79. struct proto *answer_prot;
  80. unsigned char answer_flags;
  81. char answer_no_check;
  82. int try_loading_module = 0;
  83. int err;
  84. if (sock->type != SOCK_RAW &&
  85. sock->type != SOCK_DGRAM &&
  86. !inet_ehash_secret)
  87. build_ehash_secret();
  88. /* Look for the requested type/protocol pair. */
  89. answer = NULL;
  90. lookup_protocol:
  91. err = -ESOCKTNOSUPPORT;
  92. rcu_read_lock();
  93. list_for_each_rcu(p, &inetsw6[sock->type]) {
  94. answer = list_entry(p, struct inet_protosw, list);
  95. /* Check the non-wild match. */
  96. if (protocol == answer->protocol) {
  97. if (protocol != IPPROTO_IP)
  98. break;
  99. } else {
  100. /* Check for the two wild cases. */
  101. if (IPPROTO_IP == protocol) {
  102. protocol = answer->protocol;
  103. break;
  104. }
  105. if (IPPROTO_IP == answer->protocol)
  106. break;
  107. }
  108. err = -EPROTONOSUPPORT;
  109. answer = NULL;
  110. }
  111. if (!answer) {
  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. BUG_TRAP(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 = init_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. v4addr = addr->sin6_addr.s6_addr32[3];
  242. if (inet_addr_type(net, v4addr) != RTN_LOCAL) {
  243. err = -EADDRNOTAVAIL;
  244. goto out;
  245. }
  246. } else {
  247. if (addr_type != IPV6_ADDR_ANY) {
  248. struct net_device *dev = NULL;
  249. if (addr_type & IPV6_ADDR_LINKLOCAL) {
  250. if (addr_len >= sizeof(struct sockaddr_in6) &&
  251. addr->sin6_scope_id) {
  252. /* Override any existing binding, if another one
  253. * is supplied by user.
  254. */
  255. sk->sk_bound_dev_if = addr->sin6_scope_id;
  256. }
  257. /* Binding to link-local address requires an interface */
  258. if (!sk->sk_bound_dev_if) {
  259. err = -EINVAL;
  260. goto out;
  261. }
  262. dev = dev_get_by_index(net, sk->sk_bound_dev_if);
  263. if (!dev) {
  264. err = -ENODEV;
  265. goto out;
  266. }
  267. }
  268. /* ipv4 addr of the socket is invalid. Only the
  269. * unspecified and mapped address have a v4 equivalent.
  270. */
  271. v4addr = LOOPBACK4_IPV6;
  272. if (!(addr_type & IPV6_ADDR_MULTICAST)) {
  273. if (!ipv6_chk_addr(net, &addr->sin6_addr,
  274. dev, 0)) {
  275. if (dev)
  276. dev_put(dev);
  277. err = -EADDRNOTAVAIL;
  278. goto out;
  279. }
  280. }
  281. if (dev)
  282. dev_put(dev);
  283. }
  284. }
  285. inet->rcv_saddr = v4addr;
  286. inet->saddr = v4addr;
  287. ipv6_addr_copy(&np->rcv_saddr, &addr->sin6_addr);
  288. if (!(addr_type & IPV6_ADDR_MULTICAST))
  289. ipv6_addr_copy(&np->saddr, &addr->sin6_addr);
  290. /* Make sure we are allowed to bind here. */
  291. if (sk->sk_prot->get_port(sk, snum)) {
  292. inet_reset_saddr(sk);
  293. err = -EADDRINUSE;
  294. goto out;
  295. }
  296. if (addr_type != IPV6_ADDR_ANY)
  297. sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
  298. if (snum)
  299. sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
  300. inet->sport = htons(inet->num);
  301. inet->dport = 0;
  302. inet->daddr = 0;
  303. out:
  304. release_sock(sk);
  305. return err;
  306. }
  307. EXPORT_SYMBOL(inet6_bind);
  308. int inet6_release(struct socket *sock)
  309. {
  310. struct sock *sk = sock->sk;
  311. if (sk == NULL)
  312. return -EINVAL;
  313. /* Free mc lists */
  314. ipv6_sock_mc_close(sk);
  315. /* Free ac lists */
  316. ipv6_sock_ac_close(sk);
  317. return inet_release(sock);
  318. }
  319. EXPORT_SYMBOL(inet6_release);
  320. int inet6_destroy_sock(struct sock *sk)
  321. {
  322. struct ipv6_pinfo *np = inet6_sk(sk);
  323. struct sk_buff *skb;
  324. struct ipv6_txoptions *opt;
  325. /* Release rx options */
  326. if ((skb = xchg(&np->pktoptions, NULL)) != NULL)
  327. kfree_skb(skb);
  328. /* Free flowlabels */
  329. fl6_free_socklist(sk);
  330. /* Free tx options */
  331. if ((opt = xchg(&np->opt, NULL)) != NULL)
  332. sock_kfree_s(sk, opt, opt->tot_len);
  333. return 0;
  334. }
  335. EXPORT_SYMBOL_GPL(inet6_destroy_sock);
  336. /*
  337. * This does both peername and sockname.
  338. */
  339. int inet6_getname(struct socket *sock, struct sockaddr *uaddr,
  340. int *uaddr_len, int peer)
  341. {
  342. struct sockaddr_in6 *sin=(struct sockaddr_in6 *)uaddr;
  343. struct sock *sk = sock->sk;
  344. struct inet_sock *inet = inet_sk(sk);
  345. struct ipv6_pinfo *np = inet6_sk(sk);
  346. sin->sin6_family = AF_INET6;
  347. sin->sin6_flowinfo = 0;
  348. sin->sin6_scope_id = 0;
  349. if (peer) {
  350. if (!inet->dport)
  351. return -ENOTCONN;
  352. if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
  353. peer == 1)
  354. return -ENOTCONN;
  355. sin->sin6_port = inet->dport;
  356. ipv6_addr_copy(&sin->sin6_addr, &np->daddr);
  357. if (np->sndflow)
  358. sin->sin6_flowinfo = np->flow_label;
  359. } else {
  360. if (ipv6_addr_any(&np->rcv_saddr))
  361. ipv6_addr_copy(&sin->sin6_addr, &np->saddr);
  362. else
  363. ipv6_addr_copy(&sin->sin6_addr, &np->rcv_saddr);
  364. sin->sin6_port = inet->sport;
  365. }
  366. if (ipv6_addr_type(&sin->sin6_addr) & IPV6_ADDR_LINKLOCAL)
  367. sin->sin6_scope_id = sk->sk_bound_dev_if;
  368. *uaddr_len = sizeof(*sin);
  369. return(0);
  370. }
  371. EXPORT_SYMBOL(inet6_getname);
  372. int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  373. {
  374. struct sock *sk = sock->sk;
  375. struct net *net = sock_net(sk);
  376. switch(cmd)
  377. {
  378. case SIOCGSTAMP:
  379. return sock_get_timestamp(sk, (struct timeval __user *)arg);
  380. case SIOCGSTAMPNS:
  381. return sock_get_timestampns(sk, (struct timespec __user *)arg);
  382. case SIOCADDRT:
  383. case SIOCDELRT:
  384. return(ipv6_route_ioctl(net, cmd, (void __user *)arg));
  385. case SIOCSIFADDR:
  386. return addrconf_add_ifaddr(net, (void __user *) arg);
  387. case SIOCDIFADDR:
  388. return addrconf_del_ifaddr(net, (void __user *) arg);
  389. case SIOCSIFDSTADDR:
  390. return addrconf_set_dstaddr(net, (void __user *) arg);
  391. default:
  392. if (!sk->sk_prot->ioctl)
  393. return -ENOIOCTLCMD;
  394. return sk->sk_prot->ioctl(sk, cmd, arg);
  395. }
  396. /*NOTREACHED*/
  397. return(0);
  398. }
  399. EXPORT_SYMBOL(inet6_ioctl);
  400. const struct proto_ops inet6_stream_ops = {
  401. .family = PF_INET6,
  402. .owner = THIS_MODULE,
  403. .release = inet6_release,
  404. .bind = inet6_bind,
  405. .connect = inet_stream_connect, /* ok */
  406. .socketpair = sock_no_socketpair, /* a do nothing */
  407. .accept = inet_accept, /* ok */
  408. .getname = inet6_getname,
  409. .poll = tcp_poll, /* ok */
  410. .ioctl = inet6_ioctl, /* must change */
  411. .listen = inet_listen, /* ok */
  412. .shutdown = inet_shutdown, /* ok */
  413. .setsockopt = sock_common_setsockopt, /* ok */
  414. .getsockopt = sock_common_getsockopt, /* ok */
  415. .sendmsg = tcp_sendmsg, /* ok */
  416. .recvmsg = sock_common_recvmsg, /* ok */
  417. .mmap = sock_no_mmap,
  418. .sendpage = tcp_sendpage,
  419. .splice_read = tcp_splice_read,
  420. #ifdef CONFIG_COMPAT
  421. .compat_setsockopt = compat_sock_common_setsockopt,
  422. .compat_getsockopt = compat_sock_common_getsockopt,
  423. #endif
  424. };
  425. const struct proto_ops inet6_dgram_ops = {
  426. .family = PF_INET6,
  427. .owner = THIS_MODULE,
  428. .release = inet6_release,
  429. .bind = inet6_bind,
  430. .connect = inet_dgram_connect, /* ok */
  431. .socketpair = sock_no_socketpair, /* a do nothing */
  432. .accept = sock_no_accept, /* a do nothing */
  433. .getname = inet6_getname,
  434. .poll = udp_poll, /* ok */
  435. .ioctl = inet6_ioctl, /* must change */
  436. .listen = sock_no_listen, /* ok */
  437. .shutdown = inet_shutdown, /* ok */
  438. .setsockopt = sock_common_setsockopt, /* ok */
  439. .getsockopt = sock_common_getsockopt, /* ok */
  440. .sendmsg = inet_sendmsg, /* ok */
  441. .recvmsg = sock_common_recvmsg, /* ok */
  442. .mmap = sock_no_mmap,
  443. .sendpage = sock_no_sendpage,
  444. #ifdef CONFIG_COMPAT
  445. .compat_setsockopt = compat_sock_common_setsockopt,
  446. .compat_getsockopt = compat_sock_common_getsockopt,
  447. #endif
  448. };
  449. static struct net_proto_family inet6_family_ops = {
  450. .family = PF_INET6,
  451. .create = inet6_create,
  452. .owner = THIS_MODULE,
  453. };
  454. int inet6_register_protosw(struct inet_protosw *p)
  455. {
  456. struct list_head *lh;
  457. struct inet_protosw *answer;
  458. struct list_head *last_perm;
  459. int protocol = p->protocol;
  460. int ret;
  461. spin_lock_bh(&inetsw6_lock);
  462. ret = -EINVAL;
  463. if (p->type >= SOCK_MAX)
  464. goto out_illegal;
  465. /* If we are trying to override a permanent protocol, bail. */
  466. answer = NULL;
  467. ret = -EPERM;
  468. last_perm = &inetsw6[p->type];
  469. list_for_each(lh, &inetsw6[p->type]) {
  470. answer = list_entry(lh, struct inet_protosw, list);
  471. /* Check only the non-wild match. */
  472. if (INET_PROTOSW_PERMANENT & answer->flags) {
  473. if (protocol == answer->protocol)
  474. break;
  475. last_perm = lh;
  476. }
  477. answer = NULL;
  478. }
  479. if (answer)
  480. goto out_permanent;
  481. /* Add the new entry after the last permanent entry if any, so that
  482. * the new entry does not override a permanent entry when matched with
  483. * a wild-card protocol. But it is allowed to override any existing
  484. * non-permanent entry. This means that when we remove this entry, the
  485. * system automatically returns to the old behavior.
  486. */
  487. list_add_rcu(&p->list, last_perm);
  488. ret = 0;
  489. out:
  490. spin_unlock_bh(&inetsw6_lock);
  491. return ret;
  492. out_permanent:
  493. printk(KERN_ERR "Attempt to override permanent protocol %d.\n",
  494. protocol);
  495. goto out;
  496. out_illegal:
  497. printk(KERN_ERR
  498. "Ignoring attempt to register invalid socket type %d.\n",
  499. p->type);
  500. goto out;
  501. }
  502. EXPORT_SYMBOL(inet6_register_protosw);
  503. void
  504. inet6_unregister_protosw(struct inet_protosw *p)
  505. {
  506. if (INET_PROTOSW_PERMANENT & p->flags) {
  507. printk(KERN_ERR
  508. "Attempt to unregister permanent protocol %d.\n",
  509. p->protocol);
  510. } else {
  511. spin_lock_bh(&inetsw6_lock);
  512. list_del_rcu(&p->list);
  513. spin_unlock_bh(&inetsw6_lock);
  514. synchronize_net();
  515. }
  516. }
  517. EXPORT_SYMBOL(inet6_unregister_protosw);
  518. int inet6_sk_rebuild_header(struct sock *sk)
  519. {
  520. int err;
  521. struct dst_entry *dst;
  522. struct ipv6_pinfo *np = inet6_sk(sk);
  523. dst = __sk_dst_check(sk, np->dst_cookie);
  524. if (dst == NULL) {
  525. struct inet_sock *inet = inet_sk(sk);
  526. struct in6_addr *final_p = NULL, final;
  527. struct flowi fl;
  528. memset(&fl, 0, sizeof(fl));
  529. fl.proto = sk->sk_protocol;
  530. ipv6_addr_copy(&fl.fl6_dst, &np->daddr);
  531. ipv6_addr_copy(&fl.fl6_src, &np->saddr);
  532. fl.fl6_flowlabel = np->flow_label;
  533. fl.oif = sk->sk_bound_dev_if;
  534. fl.fl_ip_dport = inet->dport;
  535. fl.fl_ip_sport = inet->sport;
  536. security_sk_classify_flow(sk, &fl);
  537. if (np->opt && np->opt->srcrt) {
  538. struct rt0_hdr *rt0 = (struct rt0_hdr *) np->opt->srcrt;
  539. ipv6_addr_copy(&final, &fl.fl6_dst);
  540. ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
  541. final_p = &final;
  542. }
  543. err = ip6_dst_lookup(sk, &dst, &fl);
  544. if (err) {
  545. sk->sk_route_caps = 0;
  546. return err;
  547. }
  548. if (final_p)
  549. ipv6_addr_copy(&fl.fl6_dst, final_p);
  550. if ((err = xfrm_lookup(&dst, &fl, sk, 0)) < 0) {
  551. sk->sk_err_soft = -err;
  552. return err;
  553. }
  554. __ip6_dst_store(sk, dst, NULL, NULL);
  555. }
  556. return 0;
  557. }
  558. EXPORT_SYMBOL_GPL(inet6_sk_rebuild_header);
  559. int ipv6_opt_accepted(struct sock *sk, struct sk_buff *skb)
  560. {
  561. struct ipv6_pinfo *np = inet6_sk(sk);
  562. struct inet6_skb_parm *opt = IP6CB(skb);
  563. if (np->rxopt.all) {
  564. if ((opt->hop && (np->rxopt.bits.hopopts ||
  565. np->rxopt.bits.ohopopts)) ||
  566. ((IPV6_FLOWINFO_MASK &
  567. *(__be32 *)skb_network_header(skb)) &&
  568. np->rxopt.bits.rxflow) ||
  569. (opt->srcrt && (np->rxopt.bits.srcrt ||
  570. np->rxopt.bits.osrcrt)) ||
  571. ((opt->dst1 || opt->dst0) &&
  572. (np->rxopt.bits.dstopts || np->rxopt.bits.odstopts)))
  573. return 1;
  574. }
  575. return 0;
  576. }
  577. EXPORT_SYMBOL_GPL(ipv6_opt_accepted);
  578. static struct inet6_protocol *ipv6_gso_pull_exthdrs(struct sk_buff *skb,
  579. int proto)
  580. {
  581. struct inet6_protocol *ops = NULL;
  582. for (;;) {
  583. struct ipv6_opt_hdr *opth;
  584. int len;
  585. if (proto != NEXTHDR_HOP) {
  586. ops = rcu_dereference(inet6_protos[proto]);
  587. if (unlikely(!ops))
  588. break;
  589. if (!(ops->flags & INET6_PROTO_GSO_EXTHDR))
  590. break;
  591. }
  592. if (unlikely(!pskb_may_pull(skb, 8)))
  593. break;
  594. opth = (void *)skb->data;
  595. len = ipv6_optlen(opth);
  596. if (unlikely(!pskb_may_pull(skb, len)))
  597. break;
  598. proto = opth->nexthdr;
  599. __skb_pull(skb, len);
  600. }
  601. return ops;
  602. }
  603. static int ipv6_gso_send_check(struct sk_buff *skb)
  604. {
  605. struct ipv6hdr *ipv6h;
  606. struct inet6_protocol *ops;
  607. int err = -EINVAL;
  608. if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h))))
  609. goto out;
  610. ipv6h = ipv6_hdr(skb);
  611. __skb_pull(skb, sizeof(*ipv6h));
  612. err = -EPROTONOSUPPORT;
  613. rcu_read_lock();
  614. ops = ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr);
  615. if (likely(ops && ops->gso_send_check)) {
  616. skb_reset_transport_header(skb);
  617. err = ops->gso_send_check(skb);
  618. }
  619. rcu_read_unlock();
  620. out:
  621. return err;
  622. }
  623. static struct sk_buff *ipv6_gso_segment(struct sk_buff *skb, int features)
  624. {
  625. struct sk_buff *segs = ERR_PTR(-EINVAL);
  626. struct ipv6hdr *ipv6h;
  627. struct inet6_protocol *ops;
  628. if (!(features & NETIF_F_V6_CSUM))
  629. features &= ~NETIF_F_SG;
  630. if (unlikely(skb_shinfo(skb)->gso_type &
  631. ~(SKB_GSO_UDP |
  632. SKB_GSO_DODGY |
  633. SKB_GSO_TCP_ECN |
  634. SKB_GSO_TCPV6 |
  635. 0)))
  636. goto out;
  637. if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h))))
  638. goto out;
  639. ipv6h = ipv6_hdr(skb);
  640. __skb_pull(skb, sizeof(*ipv6h));
  641. segs = ERR_PTR(-EPROTONOSUPPORT);
  642. rcu_read_lock();
  643. ops = ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr);
  644. if (likely(ops && ops->gso_segment)) {
  645. skb_reset_transport_header(skb);
  646. segs = ops->gso_segment(skb, features);
  647. }
  648. rcu_read_unlock();
  649. if (unlikely(IS_ERR(segs)))
  650. goto out;
  651. for (skb = segs; skb; skb = skb->next) {
  652. ipv6h = ipv6_hdr(skb);
  653. ipv6h->payload_len = htons(skb->len - skb->mac_len -
  654. sizeof(*ipv6h));
  655. }
  656. out:
  657. return segs;
  658. }
  659. static struct packet_type ipv6_packet_type = {
  660. .type = __constant_htons(ETH_P_IPV6),
  661. .func = ipv6_rcv,
  662. .gso_send_check = ipv6_gso_send_check,
  663. .gso_segment = ipv6_gso_segment,
  664. };
  665. static int __init ipv6_packet_init(void)
  666. {
  667. dev_add_pack(&ipv6_packet_type);
  668. return 0;
  669. }
  670. static void ipv6_packet_cleanup(void)
  671. {
  672. dev_remove_pack(&ipv6_packet_type);
  673. }
  674. static int __init init_ipv6_mibs(void)
  675. {
  676. if (snmp_mib_init((void **)ipv6_statistics,
  677. sizeof(struct ipstats_mib)) < 0)
  678. goto err_ip_mib;
  679. if (snmp_mib_init((void **)icmpv6_statistics,
  680. sizeof(struct icmpv6_mib)) < 0)
  681. goto err_icmp_mib;
  682. if (snmp_mib_init((void **)icmpv6msg_statistics,
  683. sizeof(struct icmpv6msg_mib)) < 0)
  684. goto err_icmpmsg_mib;
  685. if (snmp_mib_init((void **)udp_stats_in6, sizeof (struct udp_mib)) < 0)
  686. goto err_udp_mib;
  687. if (snmp_mib_init((void **)udplite_stats_in6,
  688. sizeof (struct udp_mib)) < 0)
  689. goto err_udplite_mib;
  690. return 0;
  691. err_udplite_mib:
  692. snmp_mib_free((void **)udp_stats_in6);
  693. err_udp_mib:
  694. snmp_mib_free((void **)icmpv6msg_statistics);
  695. err_icmpmsg_mib:
  696. snmp_mib_free((void **)icmpv6_statistics);
  697. err_icmp_mib:
  698. snmp_mib_free((void **)ipv6_statistics);
  699. err_ip_mib:
  700. return -ENOMEM;
  701. }
  702. static void cleanup_ipv6_mibs(void)
  703. {
  704. snmp_mib_free((void **)ipv6_statistics);
  705. snmp_mib_free((void **)icmpv6_statistics);
  706. snmp_mib_free((void **)icmpv6msg_statistics);
  707. snmp_mib_free((void **)udp_stats_in6);
  708. snmp_mib_free((void **)udplite_stats_in6);
  709. }
  710. static int inet6_net_init(struct net *net)
  711. {
  712. int err = 0;
  713. net->ipv6.sysctl.bindv6only = 0;
  714. net->ipv6.sysctl.flush_delay = 0;
  715. net->ipv6.sysctl.ip6_rt_max_size = 4096;
  716. net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
  717. net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
  718. net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
  719. net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
  720. net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
  721. net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
  722. net->ipv6.sysctl.icmpv6_time = 1*HZ;
  723. #ifdef CONFIG_PROC_FS
  724. err = udp6_proc_init(net);
  725. if (err)
  726. goto out;
  727. err = tcp6_proc_init(net);
  728. if (err)
  729. goto proc_tcp6_fail;
  730. err = ac6_proc_init(net);
  731. if (err)
  732. goto proc_ac6_fail;
  733. out:
  734. #endif
  735. return err;
  736. #ifdef CONFIG_PROC_FS
  737. proc_ac6_fail:
  738. tcp6_proc_exit(net);
  739. proc_tcp6_fail:
  740. udp6_proc_exit(net);
  741. goto out;
  742. #endif
  743. }
  744. static void inet6_net_exit(struct net *net)
  745. {
  746. #ifdef CONFIG_PROC_FS
  747. udp6_proc_exit(net);
  748. tcp6_proc_exit(net);
  749. ac6_proc_exit(net);
  750. #endif
  751. }
  752. static struct pernet_operations inet6_net_ops = {
  753. .init = inet6_net_init,
  754. .exit = inet6_net_exit,
  755. };
  756. static int __init inet6_init(void)
  757. {
  758. struct sk_buff *dummy_skb;
  759. struct list_head *r;
  760. int err;
  761. BUILD_BUG_ON(sizeof(struct inet6_skb_parm) > sizeof(dummy_skb->cb));
  762. err = proto_register(&tcpv6_prot, 1);
  763. if (err)
  764. goto out;
  765. err = proto_register(&udpv6_prot, 1);
  766. if (err)
  767. goto out_unregister_tcp_proto;
  768. err = proto_register(&udplitev6_prot, 1);
  769. if (err)
  770. goto out_unregister_udp_proto;
  771. err = proto_register(&rawv6_prot, 1);
  772. if (err)
  773. goto out_unregister_udplite_proto;
  774. /* Register the socket-side information for inet6_create. */
  775. for(r = &inetsw6[0]; r < &inetsw6[SOCK_MAX]; ++r)
  776. INIT_LIST_HEAD(r);
  777. /* We MUST register RAW sockets before we create the ICMP6,
  778. * IGMP6, or NDISC control sockets.
  779. */
  780. err = rawv6_init();
  781. if (err)
  782. goto out_unregister_raw_proto;
  783. /* Register the family here so that the init calls below will
  784. * be able to create sockets. (?? is this dangerous ??)
  785. */
  786. err = sock_register(&inet6_family_ops);
  787. if (err)
  788. goto out_sock_register_fail;
  789. /* Initialise ipv6 mibs */
  790. err = init_ipv6_mibs();
  791. if (err)
  792. goto out_unregister_sock;
  793. /*
  794. * ipngwg API draft makes clear that the correct semantics
  795. * for TCP and UDP is to consider one TCP and UDP instance
  796. * in a host availiable by both INET and INET6 APIs and
  797. * able to communicate via both network protocols.
  798. */
  799. err = register_pernet_subsys(&inet6_net_ops);
  800. if (err)
  801. goto register_pernet_fail;
  802. err = icmpv6_init();
  803. if (err)
  804. goto icmp_fail;
  805. err = ndisc_init();
  806. if (err)
  807. goto ndisc_fail;
  808. err = igmp6_init();
  809. if (err)
  810. goto igmp_fail;
  811. err = ipv6_netfilter_init();
  812. if (err)
  813. goto netfilter_fail;
  814. /* Create /proc/foo6 entries. */
  815. #ifdef CONFIG_PROC_FS
  816. err = -ENOMEM;
  817. if (raw6_proc_init())
  818. goto proc_raw6_fail;
  819. if (udplite6_proc_init())
  820. goto proc_udplite6_fail;
  821. if (ipv6_misc_proc_init())
  822. goto proc_misc6_fail;
  823. if (if6_proc_init())
  824. goto proc_if6_fail;
  825. #endif
  826. err = ip6_route_init();
  827. if (err)
  828. goto ip6_route_fail;
  829. err = ip6_flowlabel_init();
  830. if (err)
  831. goto ip6_flowlabel_fail;
  832. err = addrconf_init();
  833. if (err)
  834. goto addrconf_fail;
  835. /* Init v6 extension headers. */
  836. err = ipv6_exthdrs_init();
  837. if (err)
  838. goto ipv6_exthdrs_fail;
  839. err = ipv6_frag_init();
  840. if (err)
  841. goto ipv6_frag_fail;
  842. /* Init v6 transport protocols. */
  843. err = udpv6_init();
  844. if (err)
  845. goto udpv6_fail;
  846. err = udplitev6_init();
  847. if (err)
  848. goto udplitev6_fail;
  849. err = tcpv6_init();
  850. if (err)
  851. goto tcpv6_fail;
  852. err = ipv6_packet_init();
  853. if (err)
  854. goto ipv6_packet_fail;
  855. #ifdef CONFIG_SYSCTL
  856. err = ipv6_sysctl_register();
  857. if (err)
  858. goto sysctl_fail;
  859. #endif
  860. out:
  861. return err;
  862. #ifdef CONFIG_SYSCTL
  863. sysctl_fail:
  864. ipv6_packet_cleanup();
  865. #endif
  866. ipv6_packet_fail:
  867. tcpv6_exit();
  868. tcpv6_fail:
  869. udplitev6_exit();
  870. udplitev6_fail:
  871. udpv6_exit();
  872. udpv6_fail:
  873. ipv6_frag_exit();
  874. ipv6_frag_fail:
  875. ipv6_exthdrs_exit();
  876. ipv6_exthdrs_fail:
  877. addrconf_cleanup();
  878. addrconf_fail:
  879. ip6_flowlabel_cleanup();
  880. ip6_flowlabel_fail:
  881. ip6_route_cleanup();
  882. ip6_route_fail:
  883. #ifdef CONFIG_PROC_FS
  884. if6_proc_exit();
  885. proc_if6_fail:
  886. ipv6_misc_proc_exit();
  887. proc_misc6_fail:
  888. udplite6_proc_exit();
  889. proc_udplite6_fail:
  890. raw6_proc_exit();
  891. proc_raw6_fail:
  892. #endif
  893. ipv6_netfilter_fini();
  894. netfilter_fail:
  895. igmp6_cleanup();
  896. igmp_fail:
  897. ndisc_cleanup();
  898. ndisc_fail:
  899. icmpv6_cleanup();
  900. icmp_fail:
  901. unregister_pernet_subsys(&inet6_net_ops);
  902. register_pernet_fail:
  903. cleanup_ipv6_mibs();
  904. out_unregister_sock:
  905. sock_unregister(PF_INET6);
  906. rtnl_unregister_all(PF_INET6);
  907. out_sock_register_fail:
  908. rawv6_exit();
  909. out_unregister_raw_proto:
  910. proto_unregister(&rawv6_prot);
  911. out_unregister_udplite_proto:
  912. proto_unregister(&udplitev6_prot);
  913. out_unregister_udp_proto:
  914. proto_unregister(&udpv6_prot);
  915. out_unregister_tcp_proto:
  916. proto_unregister(&tcpv6_prot);
  917. goto out;
  918. }
  919. module_init(inet6_init);
  920. static void __exit inet6_exit(void)
  921. {
  922. /* First of all disallow new sockets creation. */
  923. sock_unregister(PF_INET6);
  924. /* Disallow any further netlink messages */
  925. rtnl_unregister_all(PF_INET6);
  926. #ifdef CONFIG_SYSCTL
  927. ipv6_sysctl_unregister();
  928. #endif
  929. udpv6_exit();
  930. udplitev6_exit();
  931. tcpv6_exit();
  932. /* Cleanup code parts. */
  933. ipv6_packet_cleanup();
  934. ipv6_frag_exit();
  935. ipv6_exthdrs_exit();
  936. addrconf_cleanup();
  937. ip6_flowlabel_cleanup();
  938. ip6_route_cleanup();
  939. #ifdef CONFIG_PROC_FS
  940. /* Cleanup code parts. */
  941. if6_proc_exit();
  942. ipv6_misc_proc_exit();
  943. udplite6_proc_exit();
  944. raw6_proc_exit();
  945. #endif
  946. ipv6_netfilter_fini();
  947. igmp6_cleanup();
  948. ndisc_cleanup();
  949. icmpv6_cleanup();
  950. rawv6_exit();
  951. unregister_pernet_subsys(&inet6_net_ops);
  952. cleanup_ipv6_mibs();
  953. proto_unregister(&rawv6_prot);
  954. proto_unregister(&udplitev6_prot);
  955. proto_unregister(&udpv6_prot);
  956. proto_unregister(&tcpv6_prot);
  957. }
  958. module_exit(inet6_exit);
  959. MODULE_ALIAS_NETPROTO(PF_INET6);