af_inet6.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128
  1. /*
  2. * PF_INET6 socket protocol family
  3. * Linux INET6 implementation
  4. *
  5. * Authors:
  6. * Pedro Roque <roque@di.fc.ul.pt>
  7. *
  8. * Adapted from linux/net/ipv4/af_inet.c
  9. *
  10. * $Id: af_inet6.c,v 1.66 2002/02/01 22:01:04 davem Exp $
  11. *
  12. * Fixes:
  13. * piggy, Karl Knutson : Socket protocol table
  14. * Hideaki YOSHIFUJI : sin6_scope_id support
  15. * Arnaldo Melo : check proc_net_create return, cleanups
  16. *
  17. * This program is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU General Public License
  19. * as published by the Free Software Foundation; either version
  20. * 2 of the License, or (at your option) any later version.
  21. */
  22. #include <linux/module.h>
  23. #include <linux/capability.h>
  24. #include <linux/errno.h>
  25. #include <linux/types.h>
  26. #include <linux/socket.h>
  27. #include <linux/in.h>
  28. #include <linux/kernel.h>
  29. #include <linux/timer.h>
  30. #include <linux/string.h>
  31. #include <linux/sockios.h>
  32. #include <linux/net.h>
  33. #include <linux/fcntl.h>
  34. #include <linux/mm.h>
  35. #include <linux/interrupt.h>
  36. #include <linux/proc_fs.h>
  37. #include <linux/stat.h>
  38. #include <linux/init.h>
  39. #include <linux/inet.h>
  40. #include <linux/netdevice.h>
  41. #include <linux/icmpv6.h>
  42. #include <linux/netfilter_ipv6.h>
  43. #include <net/ip.h>
  44. #include <net/ipv6.h>
  45. #include <net/udp.h>
  46. #include <net/udplite.h>
  47. #include <net/tcp.h>
  48. #include <net/ipip.h>
  49. #include <net/protocol.h>
  50. #include <net/inet_common.h>
  51. #include <net/transp_v6.h>
  52. #include <net/ip6_route.h>
  53. #include <net/addrconf.h>
  54. #ifdef CONFIG_IPV6_TUNNEL
  55. #include <net/ip6_tunnel.h>
  56. #endif
  57. #include <asm/uaccess.h>
  58. #include <asm/system.h>
  59. MODULE_AUTHOR("Cast of dozens");
  60. MODULE_DESCRIPTION("IPv6 protocol stack for Linux");
  61. MODULE_LICENSE("GPL");
  62. /* The inetsw6 table contains everything that inet6_create needs to
  63. * build a new socket.
  64. */
  65. static struct list_head inetsw6[SOCK_MAX];
  66. static DEFINE_SPINLOCK(inetsw6_lock);
  67. static __inline__ struct ipv6_pinfo *inet6_sk_generic(struct sock *sk)
  68. {
  69. const int offset = sk->sk_prot->obj_size - sizeof(struct ipv6_pinfo);
  70. return (struct ipv6_pinfo *)(((u8 *)sk) + offset);
  71. }
  72. static int inet6_create(struct net *net, struct socket *sock, int protocol)
  73. {
  74. struct inet_sock *inet;
  75. struct ipv6_pinfo *np;
  76. struct sock *sk;
  77. struct list_head *p;
  78. struct inet_protosw *answer;
  79. struct proto *answer_prot;
  80. unsigned char answer_flags;
  81. char answer_no_check;
  82. int try_loading_module = 0;
  83. int err;
  84. if (sock->type != SOCK_RAW &&
  85. sock->type != SOCK_DGRAM &&
  86. !inet_ehash_secret)
  87. build_ehash_secret();
  88. /* Look for the requested type/protocol pair. */
  89. answer = NULL;
  90. lookup_protocol:
  91. err = -ESOCKTNOSUPPORT;
  92. rcu_read_lock();
  93. list_for_each_rcu(p, &inetsw6[sock->type]) {
  94. answer = list_entry(p, struct inet_protosw, list);
  95. /* Check the non-wild match. */
  96. if (protocol == answer->protocol) {
  97. if (protocol != IPPROTO_IP)
  98. break;
  99. } else {
  100. /* Check for the two wild cases. */
  101. if (IPPROTO_IP == protocol) {
  102. protocol = answer->protocol;
  103. break;
  104. }
  105. if (IPPROTO_IP == answer->protocol)
  106. break;
  107. }
  108. err = -EPROTONOSUPPORT;
  109. answer = NULL;
  110. }
  111. if (!answer) {
  112. if (try_loading_module < 2) {
  113. rcu_read_unlock();
  114. /*
  115. * Be more specific, e.g. net-pf-10-proto-132-type-1
  116. * (net-pf-PF_INET6-proto-IPPROTO_SCTP-type-SOCK_STREAM)
  117. */
  118. if (++try_loading_module == 1)
  119. request_module("net-pf-%d-proto-%d-type-%d",
  120. PF_INET6, protocol, sock->type);
  121. /*
  122. * Fall back to generic, e.g. net-pf-10-proto-132
  123. * (net-pf-PF_INET6-proto-IPPROTO_SCTP)
  124. */
  125. else
  126. request_module("net-pf-%d-proto-%d",
  127. PF_INET6, protocol);
  128. goto lookup_protocol;
  129. } else
  130. goto out_rcu_unlock;
  131. }
  132. err = -EPERM;
  133. if (answer->capability > 0 && !capable(answer->capability))
  134. goto out_rcu_unlock;
  135. sock->ops = answer->ops;
  136. answer_prot = answer->prot;
  137. answer_no_check = answer->no_check;
  138. answer_flags = answer->flags;
  139. rcu_read_unlock();
  140. BUG_TRAP(answer_prot->slab != NULL);
  141. err = -ENOBUFS;
  142. sk = sk_alloc(net, PF_INET6, GFP_KERNEL, answer_prot);
  143. if (sk == NULL)
  144. goto out;
  145. sock_init_data(sock, sk);
  146. err = 0;
  147. sk->sk_no_check = answer_no_check;
  148. if (INET_PROTOSW_REUSE & answer_flags)
  149. sk->sk_reuse = 1;
  150. inet = inet_sk(sk);
  151. inet->is_icsk = (INET_PROTOSW_ICSK & answer_flags) != 0;
  152. if (SOCK_RAW == sock->type) {
  153. inet->num = protocol;
  154. if (IPPROTO_RAW == protocol)
  155. inet->hdrincl = 1;
  156. }
  157. sk->sk_destruct = inet_sock_destruct;
  158. sk->sk_family = PF_INET6;
  159. sk->sk_protocol = protocol;
  160. sk->sk_backlog_rcv = answer->prot->backlog_rcv;
  161. inet_sk(sk)->pinet6 = np = inet6_sk_generic(sk);
  162. np->hop_limit = -1;
  163. np->mcast_hops = -1;
  164. np->mc_loop = 1;
  165. np->pmtudisc = IPV6_PMTUDISC_WANT;
  166. np->ipv6only = init_net.ipv6.sysctl.bindv6only;
  167. /* Init the ipv4 part of the socket since we can have sockets
  168. * using v6 API for ipv4.
  169. */
  170. inet->uc_ttl = -1;
  171. inet->mc_loop = 1;
  172. inet->mc_ttl = 1;
  173. inet->mc_index = 0;
  174. inet->mc_list = NULL;
  175. if (ipv4_config.no_pmtu_disc)
  176. inet->pmtudisc = IP_PMTUDISC_DONT;
  177. else
  178. inet->pmtudisc = IP_PMTUDISC_WANT;
  179. /*
  180. * Increment only the relevant sk_prot->socks debug field, this changes
  181. * the previous behaviour of incrementing both the equivalent to
  182. * answer->prot->socks (inet6_sock_nr) and inet_sock_nr.
  183. *
  184. * This allows better debug granularity as we'll know exactly how many
  185. * UDPv6, TCPv6, etc socks were allocated, not the sum of all IPv6
  186. * transport protocol socks. -acme
  187. */
  188. sk_refcnt_debug_inc(sk);
  189. if (inet->num) {
  190. /* It assumes that any protocol which allows
  191. * the user to assign a number at socket
  192. * creation time automatically shares.
  193. */
  194. inet->sport = htons(inet->num);
  195. sk->sk_prot->hash(sk);
  196. }
  197. if (sk->sk_prot->init) {
  198. err = sk->sk_prot->init(sk);
  199. if (err) {
  200. sk_common_release(sk);
  201. goto out;
  202. }
  203. }
  204. out:
  205. return err;
  206. out_rcu_unlock:
  207. rcu_read_unlock();
  208. goto out;
  209. }
  210. /* bind for INET6 API */
  211. int inet6_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
  212. {
  213. struct sockaddr_in6 *addr=(struct sockaddr_in6 *)uaddr;
  214. struct sock *sk = sock->sk;
  215. struct inet_sock *inet = inet_sk(sk);
  216. struct ipv6_pinfo *np = inet6_sk(sk);
  217. struct net *net = sk->sk_net;
  218. __be32 v4addr = 0;
  219. unsigned short snum;
  220. int addr_type = 0;
  221. int err = 0;
  222. /* If the socket has its own bind function then use it. */
  223. if (sk->sk_prot->bind)
  224. return sk->sk_prot->bind(sk, uaddr, addr_len);
  225. if (addr_len < SIN6_LEN_RFC2133)
  226. return -EINVAL;
  227. addr_type = ipv6_addr_type(&addr->sin6_addr);
  228. if ((addr_type & IPV6_ADDR_MULTICAST) && sock->type == SOCK_STREAM)
  229. return -EINVAL;
  230. snum = ntohs(addr->sin6_port);
  231. if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
  232. return -EACCES;
  233. lock_sock(sk);
  234. /* Check these errors (active socket, double bind). */
  235. if (sk->sk_state != TCP_CLOSE || inet->num) {
  236. err = -EINVAL;
  237. goto out;
  238. }
  239. /* Check if the address belongs to the host. */
  240. if (addr_type == IPV6_ADDR_MAPPED) {
  241. v4addr = addr->sin6_addr.s6_addr32[3];
  242. if (inet_addr_type(net, v4addr) != RTN_LOCAL) {
  243. err = -EADDRNOTAVAIL;
  244. goto out;
  245. }
  246. } else {
  247. if (addr_type != IPV6_ADDR_ANY) {
  248. struct net_device *dev = NULL;
  249. if (addr_type & IPV6_ADDR_LINKLOCAL) {
  250. if (addr_len >= sizeof(struct sockaddr_in6) &&
  251. addr->sin6_scope_id) {
  252. /* Override any existing binding, if another one
  253. * is supplied by user.
  254. */
  255. sk->sk_bound_dev_if = addr->sin6_scope_id;
  256. }
  257. /* Binding to link-local address requires an interface */
  258. if (!sk->sk_bound_dev_if) {
  259. err = -EINVAL;
  260. goto out;
  261. }
  262. dev = dev_get_by_index(net, sk->sk_bound_dev_if);
  263. if (!dev) {
  264. err = -ENODEV;
  265. goto out;
  266. }
  267. }
  268. /* ipv4 addr of the socket is invalid. Only the
  269. * unspecified and mapped address have a v4 equivalent.
  270. */
  271. v4addr = LOOPBACK4_IPV6;
  272. if (!(addr_type & IPV6_ADDR_MULTICAST)) {
  273. if (!ipv6_chk_addr(net, &addr->sin6_addr,
  274. dev, 0)) {
  275. if (dev)
  276. dev_put(dev);
  277. err = -EADDRNOTAVAIL;
  278. goto out;
  279. }
  280. }
  281. if (dev)
  282. dev_put(dev);
  283. }
  284. }
  285. inet->rcv_saddr = v4addr;
  286. inet->saddr = v4addr;
  287. ipv6_addr_copy(&np->rcv_saddr, &addr->sin6_addr);
  288. if (!(addr_type & IPV6_ADDR_MULTICAST))
  289. ipv6_addr_copy(&np->saddr, &addr->sin6_addr);
  290. /* Make sure we are allowed to bind here. */
  291. if (sk->sk_prot->get_port(sk, snum)) {
  292. inet_reset_saddr(sk);
  293. err = -EADDRINUSE;
  294. goto out;
  295. }
  296. if (addr_type != IPV6_ADDR_ANY)
  297. sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
  298. if (snum)
  299. sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
  300. inet->sport = htons(inet->num);
  301. inet->dport = 0;
  302. inet->daddr = 0;
  303. out:
  304. release_sock(sk);
  305. return err;
  306. }
  307. EXPORT_SYMBOL(inet6_bind);
  308. int inet6_release(struct socket *sock)
  309. {
  310. struct sock *sk = sock->sk;
  311. if (sk == NULL)
  312. return -EINVAL;
  313. /* Free mc lists */
  314. ipv6_sock_mc_close(sk);
  315. /* Free ac lists */
  316. ipv6_sock_ac_close(sk);
  317. return inet_release(sock);
  318. }
  319. EXPORT_SYMBOL(inet6_release);
  320. int inet6_destroy_sock(struct sock *sk)
  321. {
  322. struct ipv6_pinfo *np = inet6_sk(sk);
  323. struct sk_buff *skb;
  324. struct ipv6_txoptions *opt;
  325. /* Release rx options */
  326. if ((skb = xchg(&np->pktoptions, NULL)) != NULL)
  327. kfree_skb(skb);
  328. /* Free flowlabels */
  329. fl6_free_socklist(sk);
  330. /* Free tx options */
  331. if ((opt = xchg(&np->opt, NULL)) != NULL)
  332. sock_kfree_s(sk, opt, opt->tot_len);
  333. return 0;
  334. }
  335. EXPORT_SYMBOL_GPL(inet6_destroy_sock);
  336. /*
  337. * This does both peername and sockname.
  338. */
  339. int inet6_getname(struct socket *sock, struct sockaddr *uaddr,
  340. int *uaddr_len, int peer)
  341. {
  342. struct sockaddr_in6 *sin=(struct sockaddr_in6 *)uaddr;
  343. struct sock *sk = sock->sk;
  344. struct inet_sock *inet = inet_sk(sk);
  345. struct ipv6_pinfo *np = inet6_sk(sk);
  346. sin->sin6_family = AF_INET6;
  347. sin->sin6_flowinfo = 0;
  348. sin->sin6_scope_id = 0;
  349. if (peer) {
  350. if (!inet->dport)
  351. return -ENOTCONN;
  352. if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
  353. peer == 1)
  354. return -ENOTCONN;
  355. sin->sin6_port = inet->dport;
  356. ipv6_addr_copy(&sin->sin6_addr, &np->daddr);
  357. if (np->sndflow)
  358. sin->sin6_flowinfo = np->flow_label;
  359. } else {
  360. if (ipv6_addr_any(&np->rcv_saddr))
  361. ipv6_addr_copy(&sin->sin6_addr, &np->saddr);
  362. else
  363. ipv6_addr_copy(&sin->sin6_addr, &np->rcv_saddr);
  364. sin->sin6_port = inet->sport;
  365. }
  366. if (ipv6_addr_type(&sin->sin6_addr) & IPV6_ADDR_LINKLOCAL)
  367. sin->sin6_scope_id = sk->sk_bound_dev_if;
  368. *uaddr_len = sizeof(*sin);
  369. return(0);
  370. }
  371. EXPORT_SYMBOL(inet6_getname);
  372. int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  373. {
  374. struct sock *sk = sock->sk;
  375. struct net *net = sk->sk_net;
  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. #ifdef CONFIG_IP_UDPLITE
  688. if (snmp_mib_init((void **)udplite_stats_in6,
  689. sizeof (struct udp_mib)) < 0)
  690. goto err_udplite_mib;
  691. #endif
  692. return 0;
  693. #ifdef CONFIG_IP_UDPLITE
  694. err_udplite_mib:
  695. #endif
  696. snmp_mib_free((void **)udp_stats_in6);
  697. err_udp_mib:
  698. snmp_mib_free((void **)icmpv6msg_statistics);
  699. err_icmpmsg_mib:
  700. snmp_mib_free((void **)icmpv6_statistics);
  701. err_icmp_mib:
  702. snmp_mib_free((void **)ipv6_statistics);
  703. err_ip_mib:
  704. return -ENOMEM;
  705. }
  706. static void cleanup_ipv6_mibs(void)
  707. {
  708. snmp_mib_free((void **)ipv6_statistics);
  709. snmp_mib_free((void **)icmpv6_statistics);
  710. snmp_mib_free((void **)icmpv6msg_statistics);
  711. snmp_mib_free((void **)udp_stats_in6);
  712. #ifdef CONFIG_IP_UDPLITE
  713. snmp_mib_free((void **)udplite_stats_in6);
  714. #endif
  715. }
  716. static int inet6_net_init(struct net *net)
  717. {
  718. net->ipv6.sysctl.bindv6only = 0;
  719. net->ipv6.sysctl.flush_delay = 0;
  720. net->ipv6.sysctl.ip6_rt_max_size = 4096;
  721. net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
  722. net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
  723. net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
  724. net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
  725. net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
  726. net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
  727. net->ipv6.sysctl.icmpv6_time = 1*HZ;
  728. return 0;
  729. }
  730. static void inet6_net_exit(struct net *net)
  731. {
  732. return;
  733. }
  734. static struct pernet_operations inet6_net_ops = {
  735. .init = inet6_net_init,
  736. .exit = inet6_net_exit,
  737. };
  738. static int __init inet6_init(void)
  739. {
  740. struct sk_buff *dummy_skb;
  741. struct list_head *r;
  742. int err;
  743. BUILD_BUG_ON(sizeof(struct inet6_skb_parm) > sizeof(dummy_skb->cb));
  744. err = proto_register(&tcpv6_prot, 1);
  745. if (err)
  746. goto out;
  747. err = proto_register(&udpv6_prot, 1);
  748. if (err)
  749. goto out_unregister_tcp_proto;
  750. #ifdef CONFIG_IP_UDPLITE
  751. err = proto_register(&udplitev6_prot, 1);
  752. if (err)
  753. goto out_unregister_udp_proto;
  754. #endif
  755. err = proto_register(&rawv6_prot, 1);
  756. if (err)
  757. goto out_unregister_udplite_proto;
  758. /* Register the socket-side information for inet6_create. */
  759. for(r = &inetsw6[0]; r < &inetsw6[SOCK_MAX]; ++r)
  760. INIT_LIST_HEAD(r);
  761. /* We MUST register RAW sockets before we create the ICMP6,
  762. * IGMP6, or NDISC control sockets.
  763. */
  764. err = rawv6_init();
  765. if (err)
  766. goto out_unregister_raw_proto;
  767. /* Register the family here so that the init calls below will
  768. * be able to create sockets. (?? is this dangerous ??)
  769. */
  770. err = sock_register(&inet6_family_ops);
  771. if (err)
  772. goto out_sock_register_fail;
  773. /* Initialise ipv6 mibs */
  774. err = init_ipv6_mibs();
  775. if (err)
  776. goto out_unregister_sock;
  777. /*
  778. * ipngwg API draft makes clear that the correct semantics
  779. * for TCP and UDP is to consider one TCP and UDP instance
  780. * in a host availiable by both INET and INET6 APIs and
  781. * able to communicate via both network protocols.
  782. */
  783. err = register_pernet_subsys(&inet6_net_ops);
  784. if (err)
  785. goto register_pernet_fail;
  786. err = icmpv6_init();
  787. if (err)
  788. goto icmp_fail;
  789. err = ndisc_init();
  790. if (err)
  791. goto ndisc_fail;
  792. err = igmp6_init();
  793. if (err)
  794. goto igmp_fail;
  795. err = ipv6_netfilter_init();
  796. if (err)
  797. goto netfilter_fail;
  798. /* Create /proc/foo6 entries. */
  799. #ifdef CONFIG_PROC_FS
  800. err = -ENOMEM;
  801. if (raw6_proc_init())
  802. goto proc_raw6_fail;
  803. if (tcp6_proc_init())
  804. goto proc_tcp6_fail;
  805. if (udp6_proc_init())
  806. goto proc_udp6_fail;
  807. if (udplite6_proc_init())
  808. goto proc_udplite6_fail;
  809. if (ipv6_misc_proc_init())
  810. goto proc_misc6_fail;
  811. if (ac6_proc_init())
  812. goto proc_anycast6_fail;
  813. if (if6_proc_init())
  814. goto proc_if6_fail;
  815. #endif
  816. err = ip6_route_init();
  817. if (err)
  818. goto ip6_route_fail;
  819. err = ip6_flowlabel_init();
  820. if (err)
  821. goto ip6_flowlabel_fail;
  822. err = addrconf_init();
  823. if (err)
  824. goto addrconf_fail;
  825. /* Init v6 extension headers. */
  826. err = ipv6_exthdrs_init();
  827. if (err)
  828. goto ipv6_exthdrs_fail;
  829. err = ipv6_frag_init();
  830. if (err)
  831. goto ipv6_frag_fail;
  832. /* Init v6 transport protocols. */
  833. err = udpv6_init();
  834. if (err)
  835. goto udpv6_fail;
  836. err = udplitev6_init();
  837. if (err)
  838. goto udplitev6_fail;
  839. err = tcpv6_init();
  840. if (err)
  841. goto tcpv6_fail;
  842. err = ipv6_packet_init();
  843. if (err)
  844. goto ipv6_packet_fail;
  845. #ifdef CONFIG_SYSCTL
  846. err = ipv6_sysctl_register();
  847. if (err)
  848. goto sysctl_fail;
  849. #endif
  850. out:
  851. return err;
  852. #ifdef CONFIG_SYSCTL
  853. sysctl_fail:
  854. ipv6_packet_cleanup();
  855. #endif
  856. ipv6_packet_fail:
  857. tcpv6_exit();
  858. tcpv6_fail:
  859. udplitev6_exit();
  860. udplitev6_fail:
  861. udpv6_exit();
  862. udpv6_fail:
  863. ipv6_frag_exit();
  864. ipv6_frag_fail:
  865. ipv6_exthdrs_exit();
  866. ipv6_exthdrs_fail:
  867. addrconf_cleanup();
  868. addrconf_fail:
  869. ip6_flowlabel_cleanup();
  870. ip6_flowlabel_fail:
  871. ip6_route_cleanup();
  872. ip6_route_fail:
  873. #ifdef CONFIG_PROC_FS
  874. if6_proc_exit();
  875. proc_if6_fail:
  876. ac6_proc_exit();
  877. proc_anycast6_fail:
  878. ipv6_misc_proc_exit();
  879. proc_misc6_fail:
  880. udplite6_proc_exit();
  881. proc_udplite6_fail:
  882. udp6_proc_exit();
  883. proc_udp6_fail:
  884. tcp6_proc_exit();
  885. proc_tcp6_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. icmpv6_cleanup();
  896. icmp_fail:
  897. unregister_pernet_subsys(&inet6_net_ops);
  898. register_pernet_fail:
  899. cleanup_ipv6_mibs();
  900. out_unregister_sock:
  901. sock_unregister(PF_INET6);
  902. rtnl_unregister_all(PF_INET6);
  903. out_sock_register_fail:
  904. rawv6_exit();
  905. out_unregister_raw_proto:
  906. proto_unregister(&rawv6_prot);
  907. out_unregister_udplite_proto:
  908. #ifdef CONFIG_IP_UDPLITE
  909. proto_unregister(&udplitev6_prot);
  910. out_unregister_udp_proto:
  911. #endif
  912. proto_unregister(&udpv6_prot);
  913. out_unregister_tcp_proto:
  914. proto_unregister(&tcpv6_prot);
  915. goto out;
  916. }
  917. module_init(inet6_init);
  918. static void __exit inet6_exit(void)
  919. {
  920. /* First of all disallow new sockets creation. */
  921. sock_unregister(PF_INET6);
  922. /* Disallow any further netlink messages */
  923. rtnl_unregister_all(PF_INET6);
  924. #ifdef CONFIG_SYSCTL
  925. ipv6_sysctl_unregister();
  926. #endif
  927. udpv6_exit();
  928. #ifdef CONFIG_IP_UDPLITE
  929. udplitev6_exit();
  930. #endif
  931. tcpv6_exit();
  932. /* Cleanup code parts. */
  933. ipv6_packet_cleanup();
  934. ipv6_frag_exit();
  935. ipv6_exthdrs_exit();
  936. addrconf_cleanup();
  937. ip6_flowlabel_cleanup();
  938. ip6_route_cleanup();
  939. #ifdef CONFIG_PROC_FS
  940. /* Cleanup code parts. */
  941. if6_proc_exit();
  942. ac6_proc_exit();
  943. ipv6_misc_proc_exit();
  944. udplite6_proc_exit();
  945. udp6_proc_exit();
  946. tcp6_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. #ifdef CONFIG_IP_UDPLITE
  958. proto_unregister(&udplitev6_prot);
  959. #endif
  960. proto_unregister(&udpv6_prot);
  961. proto_unregister(&tcpv6_prot);
  962. }
  963. module_exit(inet6_exit);
  964. MODULE_ALIAS_NETPROTO(PF_INET6);