af_inet6.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129
  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. if (snmp_mib_init((void **)ipv6_statistics,
  673. sizeof(struct ipstats_mib)) < 0)
  674. goto err_ip_mib;
  675. if (snmp_mib_init((void **)icmpv6_statistics,
  676. sizeof(struct icmpv6_mib)) < 0)
  677. goto err_icmp_mib;
  678. if (snmp_mib_init((void **)icmpv6msg_statistics,
  679. sizeof(struct icmpv6msg_mib)) < 0)
  680. goto err_icmpmsg_mib;
  681. if (snmp_mib_init((void **)udp_stats_in6, sizeof (struct udp_mib)) < 0)
  682. goto err_udp_mib;
  683. if (snmp_mib_init((void **)udplite_stats_in6,
  684. sizeof (struct udp_mib)) < 0)
  685. goto err_udplite_mib;
  686. return 0;
  687. err_udplite_mib:
  688. snmp_mib_free((void **)udp_stats_in6);
  689. err_udp_mib:
  690. snmp_mib_free((void **)icmpv6msg_statistics);
  691. err_icmpmsg_mib:
  692. snmp_mib_free((void **)icmpv6_statistics);
  693. err_icmp_mib:
  694. snmp_mib_free((void **)ipv6_statistics);
  695. err_ip_mib:
  696. return -ENOMEM;
  697. }
  698. static void cleanup_ipv6_mibs(void)
  699. {
  700. snmp_mib_free((void **)ipv6_statistics);
  701. snmp_mib_free((void **)icmpv6_statistics);
  702. snmp_mib_free((void **)icmpv6msg_statistics);
  703. snmp_mib_free((void **)udp_stats_in6);
  704. snmp_mib_free((void **)udplite_stats_in6);
  705. }
  706. static int inet6_net_init(struct net *net)
  707. {
  708. int err = 0;
  709. net->ipv6.sysctl.bindv6only = 0;
  710. net->ipv6.sysctl.icmpv6_time = 1*HZ;
  711. #ifdef CONFIG_PROC_FS
  712. err = udp6_proc_init(net);
  713. if (err)
  714. goto out;
  715. err = tcp6_proc_init(net);
  716. if (err)
  717. goto proc_tcp6_fail;
  718. err = ac6_proc_init(net);
  719. if (err)
  720. goto proc_ac6_fail;
  721. out:
  722. #endif
  723. return err;
  724. #ifdef CONFIG_PROC_FS
  725. proc_ac6_fail:
  726. tcp6_proc_exit(net);
  727. proc_tcp6_fail:
  728. udp6_proc_exit(net);
  729. goto out;
  730. #endif
  731. }
  732. static void inet6_net_exit(struct net *net)
  733. {
  734. #ifdef CONFIG_PROC_FS
  735. udp6_proc_exit(net);
  736. tcp6_proc_exit(net);
  737. ac6_proc_exit(net);
  738. #endif
  739. }
  740. static struct pernet_operations inet6_net_ops = {
  741. .init = inet6_net_init,
  742. .exit = inet6_net_exit,
  743. };
  744. static int __init inet6_init(void)
  745. {
  746. struct sk_buff *dummy_skb;
  747. struct list_head *r;
  748. int err;
  749. BUILD_BUG_ON(sizeof(struct inet6_skb_parm) > sizeof(dummy_skb->cb));
  750. err = proto_register(&tcpv6_prot, 1);
  751. if (err)
  752. goto out;
  753. err = proto_register(&udpv6_prot, 1);
  754. if (err)
  755. goto out_unregister_tcp_proto;
  756. err = proto_register(&udplitev6_prot, 1);
  757. if (err)
  758. goto out_unregister_udp_proto;
  759. err = proto_register(&rawv6_prot, 1);
  760. if (err)
  761. goto out_unregister_udplite_proto;
  762. /* Register the socket-side information for inet6_create. */
  763. for(r = &inetsw6[0]; r < &inetsw6[SOCK_MAX]; ++r)
  764. INIT_LIST_HEAD(r);
  765. /* We MUST register RAW sockets before we create the ICMP6,
  766. * IGMP6, or NDISC control sockets.
  767. */
  768. err = rawv6_init();
  769. if (err)
  770. goto out_unregister_raw_proto;
  771. /* Register the family here so that the init calls below will
  772. * be able to create sockets. (?? is this dangerous ??)
  773. */
  774. err = sock_register(&inet6_family_ops);
  775. if (err)
  776. goto out_sock_register_fail;
  777. /* Initialise ipv6 mibs */
  778. err = init_ipv6_mibs();
  779. if (err)
  780. goto out_unregister_sock;
  781. #ifdef CONFIG_SYSCTL
  782. err = ipv6_static_sysctl_register();
  783. if (err)
  784. goto static_sysctl_fail;
  785. #endif
  786. /*
  787. * ipngwg API draft makes clear that the correct semantics
  788. * for TCP and UDP is to consider one TCP and UDP instance
  789. * in a host availiable by both INET and INET6 APIs and
  790. * able to communicate via both network protocols.
  791. */
  792. err = register_pernet_subsys(&inet6_net_ops);
  793. if (err)
  794. goto register_pernet_fail;
  795. err = icmpv6_init();
  796. if (err)
  797. goto icmp_fail;
  798. err = ip6_mr_init();
  799. if (err)
  800. goto ipmr_fail;
  801. err = ndisc_init();
  802. if (err)
  803. goto ndisc_fail;
  804. err = igmp6_init();
  805. if (err)
  806. goto igmp_fail;
  807. err = ipv6_netfilter_init();
  808. if (err)
  809. goto netfilter_fail;
  810. /* Create /proc/foo6 entries. */
  811. #ifdef CONFIG_PROC_FS
  812. err = -ENOMEM;
  813. if (raw6_proc_init())
  814. goto proc_raw6_fail;
  815. if (udplite6_proc_init())
  816. goto proc_udplite6_fail;
  817. if (ipv6_misc_proc_init())
  818. goto proc_misc6_fail;
  819. if (if6_proc_init())
  820. goto proc_if6_fail;
  821. #endif
  822. err = ip6_route_init();
  823. if (err)
  824. goto ip6_route_fail;
  825. err = ip6_flowlabel_init();
  826. if (err)
  827. goto ip6_flowlabel_fail;
  828. err = addrconf_init();
  829. if (err)
  830. goto addrconf_fail;
  831. /* Init v6 extension headers. */
  832. err = ipv6_exthdrs_init();
  833. if (err)
  834. goto ipv6_exthdrs_fail;
  835. err = ipv6_frag_init();
  836. if (err)
  837. goto ipv6_frag_fail;
  838. /* Init v6 transport protocols. */
  839. err = udpv6_init();
  840. if (err)
  841. goto udpv6_fail;
  842. err = udplitev6_init();
  843. if (err)
  844. goto udplitev6_fail;
  845. err = tcpv6_init();
  846. if (err)
  847. goto tcpv6_fail;
  848. err = ipv6_packet_init();
  849. if (err)
  850. goto ipv6_packet_fail;
  851. #ifdef CONFIG_SYSCTL
  852. err = ipv6_sysctl_register();
  853. if (err)
  854. goto sysctl_fail;
  855. #endif
  856. out:
  857. return err;
  858. #ifdef CONFIG_SYSCTL
  859. sysctl_fail:
  860. ipv6_packet_cleanup();
  861. #endif
  862. ipv6_packet_fail:
  863. tcpv6_exit();
  864. tcpv6_fail:
  865. udplitev6_exit();
  866. udplitev6_fail:
  867. udpv6_exit();
  868. udpv6_fail:
  869. ipv6_frag_exit();
  870. ipv6_frag_fail:
  871. ipv6_exthdrs_exit();
  872. ipv6_exthdrs_fail:
  873. addrconf_cleanup();
  874. addrconf_fail:
  875. ip6_flowlabel_cleanup();
  876. ip6_flowlabel_fail:
  877. ip6_route_cleanup();
  878. ip6_route_fail:
  879. #ifdef CONFIG_PROC_FS
  880. if6_proc_exit();
  881. proc_if6_fail:
  882. ipv6_misc_proc_exit();
  883. proc_misc6_fail:
  884. udplite6_proc_exit();
  885. proc_udplite6_fail:
  886. raw6_proc_exit();
  887. proc_raw6_fail:
  888. #endif
  889. ipv6_netfilter_fini();
  890. netfilter_fail:
  891. igmp6_cleanup();
  892. igmp_fail:
  893. ndisc_cleanup();
  894. ndisc_fail:
  895. ip6_mr_cleanup();
  896. ipmr_fail:
  897. icmpv6_cleanup();
  898. icmp_fail:
  899. unregister_pernet_subsys(&inet6_net_ops);
  900. register_pernet_fail:
  901. #ifdef CONFIG_SYSCTL
  902. ipv6_static_sysctl_unregister();
  903. static_sysctl_fail:
  904. #endif
  905. cleanup_ipv6_mibs();
  906. out_unregister_sock:
  907. sock_unregister(PF_INET6);
  908. rtnl_unregister_all(PF_INET6);
  909. out_sock_register_fail:
  910. rawv6_exit();
  911. out_unregister_raw_proto:
  912. proto_unregister(&rawv6_prot);
  913. out_unregister_udplite_proto:
  914. proto_unregister(&udplitev6_prot);
  915. out_unregister_udp_proto:
  916. proto_unregister(&udpv6_prot);
  917. out_unregister_tcp_proto:
  918. proto_unregister(&tcpv6_prot);
  919. goto out;
  920. }
  921. module_init(inet6_init);
  922. static void __exit inet6_exit(void)
  923. {
  924. /* First of all disallow new sockets creation. */
  925. sock_unregister(PF_INET6);
  926. /* Disallow any further netlink messages */
  927. rtnl_unregister_all(PF_INET6);
  928. #ifdef CONFIG_SYSCTL
  929. ipv6_sysctl_unregister();
  930. #endif
  931. udpv6_exit();
  932. udplitev6_exit();
  933. tcpv6_exit();
  934. /* Cleanup code parts. */
  935. ipv6_packet_cleanup();
  936. ipv6_frag_exit();
  937. ipv6_exthdrs_exit();
  938. addrconf_cleanup();
  939. ip6_flowlabel_cleanup();
  940. ip6_route_cleanup();
  941. #ifdef CONFIG_PROC_FS
  942. /* Cleanup code parts. */
  943. if6_proc_exit();
  944. ipv6_misc_proc_exit();
  945. udplite6_proc_exit();
  946. raw6_proc_exit();
  947. #endif
  948. ipv6_netfilter_fini();
  949. igmp6_cleanup();
  950. ndisc_cleanup();
  951. ip6_mr_cleanup();
  952. icmpv6_cleanup();
  953. rawv6_exit();
  954. unregister_pernet_subsys(&inet6_net_ops);
  955. #ifdef CONFIG_SYSCTL
  956. ipv6_static_sysctl_unregister();
  957. #endif
  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);