af_inet6.c 26 KB

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