af_inet6.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  1. /*
  2. * PF_INET6 socket protocol family
  3. * Linux INET6 implementation
  4. *
  5. * Authors:
  6. * Pedro Roque <roque@di.fc.ul.pt>
  7. *
  8. * Adapted from linux/net/ipv4/af_inet.c
  9. *
  10. * Fixes:
  11. * piggy, Karl Knutson : Socket protocol table
  12. * Hideaki YOSHIFUJI : sin6_scope_id support
  13. * Arnaldo Melo : check proc_net_create return, cleanups
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License
  17. * as published by the Free Software Foundation; either version
  18. * 2 of the License, or (at your option) any later version.
  19. */
  20. #include <linux/module.h>
  21. #include <linux/capability.h>
  22. #include <linux/errno.h>
  23. #include <linux/types.h>
  24. #include <linux/socket.h>
  25. #include <linux/in.h>
  26. #include <linux/kernel.h>
  27. #include <linux/timer.h>
  28. #include <linux/string.h>
  29. #include <linux/sockios.h>
  30. #include <linux/net.h>
  31. #include <linux/fcntl.h>
  32. #include <linux/mm.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/proc_fs.h>
  35. #include <linux/stat.h>
  36. #include <linux/init.h>
  37. #include <linux/inet.h>
  38. #include <linux/netdevice.h>
  39. #include <linux/icmpv6.h>
  40. #include <linux/netfilter_ipv6.h>
  41. #include <net/ip.h>
  42. #include <net/ipv6.h>
  43. #include <net/udp.h>
  44. #include <net/udplite.h>
  45. #include <net/tcp.h>
  46. #include <net/ipip.h>
  47. #include <net/protocol.h>
  48. #include <net/inet_common.h>
  49. #include <net/transp_v6.h>
  50. #include <net/ip6_route.h>
  51. #include <net/addrconf.h>
  52. #ifdef CONFIG_IPV6_TUNNEL
  53. #include <net/ip6_tunnel.h>
  54. #endif
  55. #include <asm/uaccess.h>
  56. #include <asm/system.h>
  57. #ifdef CONFIG_IPV6_MROUTE
  58. #include <linux/mroute6.h>
  59. #endif
  60. MODULE_AUTHOR("Cast of dozens");
  61. MODULE_DESCRIPTION("IPv6 protocol stack for Linux");
  62. MODULE_LICENSE("GPL");
  63. /* The inetsw6 table contains everything that inet6_create needs to
  64. * build a new socket.
  65. */
  66. static struct list_head inetsw6[SOCK_MAX];
  67. static DEFINE_SPINLOCK(inetsw6_lock);
  68. static __inline__ struct ipv6_pinfo *inet6_sk_generic(struct sock *sk)
  69. {
  70. const int offset = sk->sk_prot->obj_size - sizeof(struct ipv6_pinfo);
  71. return (struct ipv6_pinfo *)(((u8 *)sk) + offset);
  72. }
  73. static int inet6_create(struct net *net, struct socket *sock, int protocol)
  74. {
  75. struct inet_sock *inet;
  76. struct ipv6_pinfo *np;
  77. struct sock *sk;
  78. struct list_head *p;
  79. struct inet_protosw *answer;
  80. struct proto *answer_prot;
  81. unsigned char answer_flags;
  82. char answer_no_check;
  83. int try_loading_module = 0;
  84. int err;
  85. if (sock->type != SOCK_RAW &&
  86. sock->type != SOCK_DGRAM &&
  87. !inet_ehash_secret)
  88. build_ehash_secret();
  89. /* Look for the requested type/protocol pair. */
  90. answer = NULL;
  91. lookup_protocol:
  92. err = -ESOCKTNOSUPPORT;
  93. rcu_read_lock();
  94. list_for_each_rcu(p, &inetsw6[sock->type]) {
  95. answer = list_entry(p, struct inet_protosw, list);
  96. /* Check the non-wild match. */
  97. if (protocol == answer->protocol) {
  98. if (protocol != IPPROTO_IP)
  99. break;
  100. } else {
  101. /* Check for the two wild cases. */
  102. if (IPPROTO_IP == protocol) {
  103. protocol = answer->protocol;
  104. break;
  105. }
  106. if (IPPROTO_IP == answer->protocol)
  107. break;
  108. }
  109. err = -EPROTONOSUPPORT;
  110. answer = NULL;
  111. }
  112. if (!answer) {
  113. if (try_loading_module < 2) {
  114. rcu_read_unlock();
  115. /*
  116. * Be more specific, e.g. net-pf-10-proto-132-type-1
  117. * (net-pf-PF_INET6-proto-IPPROTO_SCTP-type-SOCK_STREAM)
  118. */
  119. if (++try_loading_module == 1)
  120. request_module("net-pf-%d-proto-%d-type-%d",
  121. PF_INET6, protocol, sock->type);
  122. /*
  123. * Fall back to generic, e.g. net-pf-10-proto-132
  124. * (net-pf-PF_INET6-proto-IPPROTO_SCTP)
  125. */
  126. else
  127. request_module("net-pf-%d-proto-%d",
  128. PF_INET6, protocol);
  129. goto lookup_protocol;
  130. } else
  131. goto out_rcu_unlock;
  132. }
  133. err = -EPERM;
  134. if (answer->capability > 0 && !capable(answer->capability))
  135. goto out_rcu_unlock;
  136. sock->ops = answer->ops;
  137. answer_prot = answer->prot;
  138. answer_no_check = answer->no_check;
  139. answer_flags = answer->flags;
  140. rcu_read_unlock();
  141. BUG_TRAP(answer_prot->slab != NULL);
  142. err = -ENOBUFS;
  143. sk = sk_alloc(net, PF_INET6, GFP_KERNEL, answer_prot);
  144. if (sk == NULL)
  145. goto out;
  146. sock_init_data(sock, sk);
  147. err = 0;
  148. sk->sk_no_check = answer_no_check;
  149. if (INET_PROTOSW_REUSE & answer_flags)
  150. sk->sk_reuse = 1;
  151. inet = inet_sk(sk);
  152. inet->is_icsk = (INET_PROTOSW_ICSK & answer_flags) != 0;
  153. if (SOCK_RAW == sock->type) {
  154. inet->num = protocol;
  155. if (IPPROTO_RAW == protocol)
  156. inet->hdrincl = 1;
  157. }
  158. sk->sk_destruct = inet_sock_destruct;
  159. sk->sk_family = PF_INET6;
  160. sk->sk_protocol = protocol;
  161. sk->sk_backlog_rcv = answer->prot->backlog_rcv;
  162. inet_sk(sk)->pinet6 = np = inet6_sk_generic(sk);
  163. np->hop_limit = -1;
  164. np->mcast_hops = -1;
  165. np->mc_loop = 1;
  166. np->pmtudisc = IPV6_PMTUDISC_WANT;
  167. np->ipv6only = net->ipv6.sysctl.bindv6only;
  168. /* Init the ipv4 part of the socket since we can have sockets
  169. * using v6 API for ipv4.
  170. */
  171. inet->uc_ttl = -1;
  172. inet->mc_loop = 1;
  173. inet->mc_ttl = 1;
  174. inet->mc_index = 0;
  175. inet->mc_list = NULL;
  176. if (ipv4_config.no_pmtu_disc)
  177. inet->pmtudisc = IP_PMTUDISC_DONT;
  178. else
  179. inet->pmtudisc = IP_PMTUDISC_WANT;
  180. /*
  181. * Increment only the relevant sk_prot->socks debug field, this changes
  182. * the previous behaviour of incrementing both the equivalent to
  183. * answer->prot->socks (inet6_sock_nr) and inet_sock_nr.
  184. *
  185. * This allows better debug granularity as we'll know exactly how many
  186. * UDPv6, TCPv6, etc socks were allocated, not the sum of all IPv6
  187. * transport protocol socks. -acme
  188. */
  189. sk_refcnt_debug_inc(sk);
  190. if (inet->num) {
  191. /* It assumes that any protocol which allows
  192. * the user to assign a number at socket
  193. * creation time automatically shares.
  194. */
  195. inet->sport = htons(inet->num);
  196. sk->sk_prot->hash(sk);
  197. }
  198. if (sk->sk_prot->init) {
  199. err = sk->sk_prot->init(sk);
  200. if (err) {
  201. sk_common_release(sk);
  202. goto out;
  203. }
  204. }
  205. out:
  206. return err;
  207. out_rcu_unlock:
  208. rcu_read_unlock();
  209. goto out;
  210. }
  211. /* bind for INET6 API */
  212. int inet6_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
  213. {
  214. struct sockaddr_in6 *addr=(struct sockaddr_in6 *)uaddr;
  215. struct sock *sk = sock->sk;
  216. struct inet_sock *inet = inet_sk(sk);
  217. struct ipv6_pinfo *np = inet6_sk(sk);
  218. struct net *net = sock_net(sk);
  219. __be32 v4addr = 0;
  220. unsigned short snum;
  221. int addr_type = 0;
  222. int err = 0;
  223. /* If the socket has its own bind function then use it. */
  224. if (sk->sk_prot->bind)
  225. return sk->sk_prot->bind(sk, uaddr, addr_len);
  226. if (addr_len < SIN6_LEN_RFC2133)
  227. return -EINVAL;
  228. addr_type = ipv6_addr_type(&addr->sin6_addr);
  229. if ((addr_type & IPV6_ADDR_MULTICAST) && sock->type == SOCK_STREAM)
  230. return -EINVAL;
  231. snum = ntohs(addr->sin6_port);
  232. if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
  233. return -EACCES;
  234. lock_sock(sk);
  235. /* Check these errors (active socket, double bind). */
  236. if (sk->sk_state != TCP_CLOSE || inet->num) {
  237. err = -EINVAL;
  238. goto out;
  239. }
  240. /* Check if the address belongs to the host. */
  241. if (addr_type == IPV6_ADDR_MAPPED) {
  242. v4addr = addr->sin6_addr.s6_addr32[3];
  243. if (inet_addr_type(net, v4addr) != RTN_LOCAL) {
  244. err = -EADDRNOTAVAIL;
  245. goto out;
  246. }
  247. } else {
  248. if (addr_type != IPV6_ADDR_ANY) {
  249. struct net_device *dev = NULL;
  250. if (addr_type & IPV6_ADDR_LINKLOCAL) {
  251. if (addr_len >= sizeof(struct sockaddr_in6) &&
  252. addr->sin6_scope_id) {
  253. /* Override any existing binding, if another one
  254. * is supplied by user.
  255. */
  256. sk->sk_bound_dev_if = addr->sin6_scope_id;
  257. }
  258. /* Binding to link-local address requires an interface */
  259. if (!sk->sk_bound_dev_if) {
  260. err = -EINVAL;
  261. goto out;
  262. }
  263. dev = dev_get_by_index(net, sk->sk_bound_dev_if);
  264. if (!dev) {
  265. err = -ENODEV;
  266. goto out;
  267. }
  268. }
  269. /* ipv4 addr of the socket is invalid. Only the
  270. * unspecified and mapped address have a v4 equivalent.
  271. */
  272. v4addr = LOOPBACK4_IPV6;
  273. if (!(addr_type & IPV6_ADDR_MULTICAST)) {
  274. if (!ipv6_chk_addr(net, &addr->sin6_addr,
  275. dev, 0)) {
  276. if (dev)
  277. dev_put(dev);
  278. err = -EADDRNOTAVAIL;
  279. goto out;
  280. }
  281. }
  282. if (dev)
  283. dev_put(dev);
  284. }
  285. }
  286. inet->rcv_saddr = v4addr;
  287. inet->saddr = v4addr;
  288. ipv6_addr_copy(&np->rcv_saddr, &addr->sin6_addr);
  289. if (!(addr_type & IPV6_ADDR_MULTICAST))
  290. ipv6_addr_copy(&np->saddr, &addr->sin6_addr);
  291. /* Make sure we are allowed to bind here. */
  292. if (sk->sk_prot->get_port(sk, snum)) {
  293. inet_reset_saddr(sk);
  294. err = -EADDRINUSE;
  295. goto out;
  296. }
  297. if (addr_type != IPV6_ADDR_ANY)
  298. sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
  299. if (snum)
  300. sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
  301. inet->sport = htons(inet->num);
  302. inet->dport = 0;
  303. inet->daddr = 0;
  304. out:
  305. release_sock(sk);
  306. return err;
  307. }
  308. EXPORT_SYMBOL(inet6_bind);
  309. int inet6_release(struct socket *sock)
  310. {
  311. struct sock *sk = sock->sk;
  312. if (sk == NULL)
  313. return -EINVAL;
  314. /* Free mc lists */
  315. ipv6_sock_mc_close(sk);
  316. /* Free ac lists */
  317. ipv6_sock_ac_close(sk);
  318. return inet_release(sock);
  319. }
  320. EXPORT_SYMBOL(inet6_release);
  321. void inet6_destroy_sock(struct sock *sk)
  322. {
  323. struct ipv6_pinfo *np = inet6_sk(sk);
  324. struct sk_buff *skb;
  325. struct ipv6_txoptions *opt;
  326. /* Release rx options */
  327. if ((skb = xchg(&np->pktoptions, NULL)) != NULL)
  328. kfree_skb(skb);
  329. /* Free flowlabels */
  330. fl6_free_socklist(sk);
  331. /* Free tx options */
  332. if ((opt = xchg(&np->opt, NULL)) != NULL)
  333. sock_kfree_s(sk, opt, opt->tot_len);
  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. #ifdef CONFIG_IPV6_MROUTE
  806. ip6_mr_init();
  807. #endif
  808. err = ndisc_init();
  809. if (err)
  810. goto ndisc_fail;
  811. err = igmp6_init();
  812. if (err)
  813. goto igmp_fail;
  814. err = ipv6_netfilter_init();
  815. if (err)
  816. goto netfilter_fail;
  817. /* Create /proc/foo6 entries. */
  818. #ifdef CONFIG_PROC_FS
  819. err = -ENOMEM;
  820. if (raw6_proc_init())
  821. goto proc_raw6_fail;
  822. if (udplite6_proc_init())
  823. goto proc_udplite6_fail;
  824. if (ipv6_misc_proc_init())
  825. goto proc_misc6_fail;
  826. if (if6_proc_init())
  827. goto proc_if6_fail;
  828. #endif
  829. err = ip6_route_init();
  830. if (err)
  831. goto ip6_route_fail;
  832. err = ip6_flowlabel_init();
  833. if (err)
  834. goto ip6_flowlabel_fail;
  835. err = addrconf_init();
  836. if (err)
  837. goto addrconf_fail;
  838. /* Init v6 extension headers. */
  839. err = ipv6_exthdrs_init();
  840. if (err)
  841. goto ipv6_exthdrs_fail;
  842. err = ipv6_frag_init();
  843. if (err)
  844. goto ipv6_frag_fail;
  845. /* Init v6 transport protocols. */
  846. err = udpv6_init();
  847. if (err)
  848. goto udpv6_fail;
  849. err = udplitev6_init();
  850. if (err)
  851. goto udplitev6_fail;
  852. err = tcpv6_init();
  853. if (err)
  854. goto tcpv6_fail;
  855. err = ipv6_packet_init();
  856. if (err)
  857. goto ipv6_packet_fail;
  858. #ifdef CONFIG_SYSCTL
  859. err = ipv6_sysctl_register();
  860. if (err)
  861. goto sysctl_fail;
  862. #endif
  863. out:
  864. return err;
  865. #ifdef CONFIG_SYSCTL
  866. sysctl_fail:
  867. ipv6_packet_cleanup();
  868. #endif
  869. ipv6_packet_fail:
  870. tcpv6_exit();
  871. tcpv6_fail:
  872. udplitev6_exit();
  873. udplitev6_fail:
  874. udpv6_exit();
  875. udpv6_fail:
  876. ipv6_frag_exit();
  877. ipv6_frag_fail:
  878. ipv6_exthdrs_exit();
  879. ipv6_exthdrs_fail:
  880. addrconf_cleanup();
  881. addrconf_fail:
  882. ip6_flowlabel_cleanup();
  883. ip6_flowlabel_fail:
  884. ip6_route_cleanup();
  885. ip6_route_fail:
  886. #ifdef CONFIG_PROC_FS
  887. if6_proc_exit();
  888. proc_if6_fail:
  889. ipv6_misc_proc_exit();
  890. proc_misc6_fail:
  891. udplite6_proc_exit();
  892. proc_udplite6_fail:
  893. raw6_proc_exit();
  894. proc_raw6_fail:
  895. #endif
  896. ipv6_netfilter_fini();
  897. netfilter_fail:
  898. igmp6_cleanup();
  899. igmp_fail:
  900. ndisc_cleanup();
  901. ndisc_fail:
  902. icmpv6_cleanup();
  903. icmp_fail:
  904. unregister_pernet_subsys(&inet6_net_ops);
  905. register_pernet_fail:
  906. cleanup_ipv6_mibs();
  907. out_unregister_sock:
  908. sock_unregister(PF_INET6);
  909. rtnl_unregister_all(PF_INET6);
  910. out_sock_register_fail:
  911. rawv6_exit();
  912. out_unregister_raw_proto:
  913. proto_unregister(&rawv6_prot);
  914. out_unregister_udplite_proto:
  915. proto_unregister(&udplitev6_prot);
  916. out_unregister_udp_proto:
  917. proto_unregister(&udpv6_prot);
  918. out_unregister_tcp_proto:
  919. proto_unregister(&tcpv6_prot);
  920. goto out;
  921. }
  922. module_init(inet6_init);
  923. static void __exit inet6_exit(void)
  924. {
  925. /* First of all disallow new sockets creation. */
  926. sock_unregister(PF_INET6);
  927. /* Disallow any further netlink messages */
  928. rtnl_unregister_all(PF_INET6);
  929. #ifdef CONFIG_SYSCTL
  930. ipv6_sysctl_unregister();
  931. #endif
  932. udpv6_exit();
  933. udplitev6_exit();
  934. tcpv6_exit();
  935. /* Cleanup code parts. */
  936. ipv6_packet_cleanup();
  937. ipv6_frag_exit();
  938. ipv6_exthdrs_exit();
  939. addrconf_cleanup();
  940. ip6_flowlabel_cleanup();
  941. ip6_route_cleanup();
  942. #ifdef CONFIG_PROC_FS
  943. /* Cleanup code parts. */
  944. if6_proc_exit();
  945. ipv6_misc_proc_exit();
  946. udplite6_proc_exit();
  947. raw6_proc_exit();
  948. #endif
  949. ipv6_netfilter_fini();
  950. igmp6_cleanup();
  951. ndisc_cleanup();
  952. icmpv6_cleanup();
  953. rawv6_exit();
  954. unregister_pernet_subsys(&inet6_net_ops);
  955. cleanup_ipv6_mibs();
  956. proto_unregister(&rawv6_prot);
  957. proto_unregister(&udplitev6_prot);
  958. proto_unregister(&udpv6_prot);
  959. proto_unregister(&tcpv6_prot);
  960. }
  961. module_exit(inet6_exit);
  962. MODULE_ALIAS_NETPROTO(PF_INET6);