af_inet6.c 26 KB

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