af_inet6.c 25 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. * 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. int 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. return 0;
  335. }
  336. EXPORT_SYMBOL_GPL(inet6_destroy_sock);
  337. /*
  338. * This does both peername and sockname.
  339. */
  340. int inet6_getname(struct socket *sock, struct sockaddr *uaddr,
  341. int *uaddr_len, int peer)
  342. {
  343. struct sockaddr_in6 *sin=(struct sockaddr_in6 *)uaddr;
  344. struct sock *sk = sock->sk;
  345. struct inet_sock *inet = inet_sk(sk);
  346. struct ipv6_pinfo *np = inet6_sk(sk);
  347. sin->sin6_family = AF_INET6;
  348. sin->sin6_flowinfo = 0;
  349. sin->sin6_scope_id = 0;
  350. if (peer) {
  351. if (!inet->dport)
  352. return -ENOTCONN;
  353. if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
  354. peer == 1)
  355. return -ENOTCONN;
  356. sin->sin6_port = inet->dport;
  357. ipv6_addr_copy(&sin->sin6_addr, &np->daddr);
  358. if (np->sndflow)
  359. sin->sin6_flowinfo = np->flow_label;
  360. } else {
  361. if (ipv6_addr_any(&np->rcv_saddr))
  362. ipv6_addr_copy(&sin->sin6_addr, &np->saddr);
  363. else
  364. ipv6_addr_copy(&sin->sin6_addr, &np->rcv_saddr);
  365. sin->sin6_port = inet->sport;
  366. }
  367. if (ipv6_addr_type(&sin->sin6_addr) & IPV6_ADDR_LINKLOCAL)
  368. sin->sin6_scope_id = sk->sk_bound_dev_if;
  369. *uaddr_len = sizeof(*sin);
  370. return(0);
  371. }
  372. EXPORT_SYMBOL(inet6_getname);
  373. int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  374. {
  375. struct sock *sk = sock->sk;
  376. struct net *net = sock_net(sk);
  377. switch(cmd)
  378. {
  379. case SIOCGSTAMP:
  380. return sock_get_timestamp(sk, (struct timeval __user *)arg);
  381. case SIOCGSTAMPNS:
  382. return sock_get_timestampns(sk, (struct timespec __user *)arg);
  383. case SIOCADDRT:
  384. case SIOCDELRT:
  385. return(ipv6_route_ioctl(net, cmd, (void __user *)arg));
  386. case SIOCSIFADDR:
  387. return addrconf_add_ifaddr(net, (void __user *) arg);
  388. case SIOCDIFADDR:
  389. return addrconf_del_ifaddr(net, (void __user *) arg);
  390. case SIOCSIFDSTADDR:
  391. return addrconf_set_dstaddr(net, (void __user *) arg);
  392. default:
  393. if (!sk->sk_prot->ioctl)
  394. return -ENOIOCTLCMD;
  395. return sk->sk_prot->ioctl(sk, cmd, arg);
  396. }
  397. /*NOTREACHED*/
  398. return(0);
  399. }
  400. EXPORT_SYMBOL(inet6_ioctl);
  401. const struct proto_ops inet6_stream_ops = {
  402. .family = PF_INET6,
  403. .owner = THIS_MODULE,
  404. .release = inet6_release,
  405. .bind = inet6_bind,
  406. .connect = inet_stream_connect, /* ok */
  407. .socketpair = sock_no_socketpair, /* a do nothing */
  408. .accept = inet_accept, /* ok */
  409. .getname = inet6_getname,
  410. .poll = tcp_poll, /* ok */
  411. .ioctl = inet6_ioctl, /* must change */
  412. .listen = inet_listen, /* ok */
  413. .shutdown = inet_shutdown, /* ok */
  414. .setsockopt = sock_common_setsockopt, /* ok */
  415. .getsockopt = sock_common_getsockopt, /* ok */
  416. .sendmsg = tcp_sendmsg, /* ok */
  417. .recvmsg = sock_common_recvmsg, /* ok */
  418. .mmap = sock_no_mmap,
  419. .sendpage = tcp_sendpage,
  420. .splice_read = tcp_splice_read,
  421. #ifdef CONFIG_COMPAT
  422. .compat_setsockopt = compat_sock_common_setsockopt,
  423. .compat_getsockopt = compat_sock_common_getsockopt,
  424. #endif
  425. };
  426. const struct proto_ops inet6_dgram_ops = {
  427. .family = PF_INET6,
  428. .owner = THIS_MODULE,
  429. .release = inet6_release,
  430. .bind = inet6_bind,
  431. .connect = inet_dgram_connect, /* ok */
  432. .socketpair = sock_no_socketpair, /* a do nothing */
  433. .accept = sock_no_accept, /* a do nothing */
  434. .getname = inet6_getname,
  435. .poll = udp_poll, /* ok */
  436. .ioctl = inet6_ioctl, /* must change */
  437. .listen = sock_no_listen, /* ok */
  438. .shutdown = inet_shutdown, /* ok */
  439. .setsockopt = sock_common_setsockopt, /* ok */
  440. .getsockopt = sock_common_getsockopt, /* ok */
  441. .sendmsg = inet_sendmsg, /* ok */
  442. .recvmsg = sock_common_recvmsg, /* ok */
  443. .mmap = sock_no_mmap,
  444. .sendpage = sock_no_sendpage,
  445. #ifdef CONFIG_COMPAT
  446. .compat_setsockopt = compat_sock_common_setsockopt,
  447. .compat_getsockopt = compat_sock_common_getsockopt,
  448. #endif
  449. };
  450. static struct net_proto_family inet6_family_ops = {
  451. .family = PF_INET6,
  452. .create = inet6_create,
  453. .owner = THIS_MODULE,
  454. };
  455. int inet6_register_protosw(struct inet_protosw *p)
  456. {
  457. struct list_head *lh;
  458. struct inet_protosw *answer;
  459. struct list_head *last_perm;
  460. int protocol = p->protocol;
  461. int ret;
  462. spin_lock_bh(&inetsw6_lock);
  463. ret = -EINVAL;
  464. if (p->type >= SOCK_MAX)
  465. goto out_illegal;
  466. /* If we are trying to override a permanent protocol, bail. */
  467. answer = NULL;
  468. ret = -EPERM;
  469. last_perm = &inetsw6[p->type];
  470. list_for_each(lh, &inetsw6[p->type]) {
  471. answer = list_entry(lh, struct inet_protosw, list);
  472. /* Check only the non-wild match. */
  473. if (INET_PROTOSW_PERMANENT & answer->flags) {
  474. if (protocol == answer->protocol)
  475. break;
  476. last_perm = lh;
  477. }
  478. answer = NULL;
  479. }
  480. if (answer)
  481. goto out_permanent;
  482. /* Add the new entry after the last permanent entry if any, so that
  483. * the new entry does not override a permanent entry when matched with
  484. * a wild-card protocol. But it is allowed to override any existing
  485. * non-permanent entry. This means that when we remove this entry, the
  486. * system automatically returns to the old behavior.
  487. */
  488. list_add_rcu(&p->list, last_perm);
  489. ret = 0;
  490. out:
  491. spin_unlock_bh(&inetsw6_lock);
  492. return ret;
  493. out_permanent:
  494. printk(KERN_ERR "Attempt to override permanent protocol %d.\n",
  495. protocol);
  496. goto out;
  497. out_illegal:
  498. printk(KERN_ERR
  499. "Ignoring attempt to register invalid socket type %d.\n",
  500. p->type);
  501. goto out;
  502. }
  503. EXPORT_SYMBOL(inet6_register_protosw);
  504. void
  505. inet6_unregister_protosw(struct inet_protosw *p)
  506. {
  507. if (INET_PROTOSW_PERMANENT & p->flags) {
  508. printk(KERN_ERR
  509. "Attempt to unregister permanent protocol %d.\n",
  510. p->protocol);
  511. } else {
  512. spin_lock_bh(&inetsw6_lock);
  513. list_del_rcu(&p->list);
  514. spin_unlock_bh(&inetsw6_lock);
  515. synchronize_net();
  516. }
  517. }
  518. EXPORT_SYMBOL(inet6_unregister_protosw);
  519. int inet6_sk_rebuild_header(struct sock *sk)
  520. {
  521. int err;
  522. struct dst_entry *dst;
  523. struct ipv6_pinfo *np = inet6_sk(sk);
  524. dst = __sk_dst_check(sk, np->dst_cookie);
  525. if (dst == NULL) {
  526. struct inet_sock *inet = inet_sk(sk);
  527. struct in6_addr *final_p = NULL, final;
  528. struct flowi fl;
  529. memset(&fl, 0, sizeof(fl));
  530. fl.proto = sk->sk_protocol;
  531. ipv6_addr_copy(&fl.fl6_dst, &np->daddr);
  532. ipv6_addr_copy(&fl.fl6_src, &np->saddr);
  533. fl.fl6_flowlabel = np->flow_label;
  534. fl.oif = sk->sk_bound_dev_if;
  535. fl.fl_ip_dport = inet->dport;
  536. fl.fl_ip_sport = inet->sport;
  537. security_sk_classify_flow(sk, &fl);
  538. if (np->opt && np->opt->srcrt) {
  539. struct rt0_hdr *rt0 = (struct rt0_hdr *) np->opt->srcrt;
  540. ipv6_addr_copy(&final, &fl.fl6_dst);
  541. ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
  542. final_p = &final;
  543. }
  544. err = ip6_dst_lookup(sk, &dst, &fl);
  545. if (err) {
  546. sk->sk_route_caps = 0;
  547. return err;
  548. }
  549. if (final_p)
  550. ipv6_addr_copy(&fl.fl6_dst, final_p);
  551. if ((err = xfrm_lookup(&dst, &fl, sk, 0)) < 0) {
  552. sk->sk_err_soft = -err;
  553. return err;
  554. }
  555. __ip6_dst_store(sk, dst, NULL, NULL);
  556. }
  557. return 0;
  558. }
  559. EXPORT_SYMBOL_GPL(inet6_sk_rebuild_header);
  560. int ipv6_opt_accepted(struct sock *sk, struct sk_buff *skb)
  561. {
  562. struct ipv6_pinfo *np = inet6_sk(sk);
  563. struct inet6_skb_parm *opt = IP6CB(skb);
  564. if (np->rxopt.all) {
  565. if ((opt->hop && (np->rxopt.bits.hopopts ||
  566. np->rxopt.bits.ohopopts)) ||
  567. ((IPV6_FLOWINFO_MASK &
  568. *(__be32 *)skb_network_header(skb)) &&
  569. np->rxopt.bits.rxflow) ||
  570. (opt->srcrt && (np->rxopt.bits.srcrt ||
  571. np->rxopt.bits.osrcrt)) ||
  572. ((opt->dst1 || opt->dst0) &&
  573. (np->rxopt.bits.dstopts || np->rxopt.bits.odstopts)))
  574. return 1;
  575. }
  576. return 0;
  577. }
  578. EXPORT_SYMBOL_GPL(ipv6_opt_accepted);
  579. static struct inet6_protocol *ipv6_gso_pull_exthdrs(struct sk_buff *skb,
  580. int proto)
  581. {
  582. struct inet6_protocol *ops = NULL;
  583. for (;;) {
  584. struct ipv6_opt_hdr *opth;
  585. int len;
  586. if (proto != NEXTHDR_HOP) {
  587. ops = rcu_dereference(inet6_protos[proto]);
  588. if (unlikely(!ops))
  589. break;
  590. if (!(ops->flags & INET6_PROTO_GSO_EXTHDR))
  591. break;
  592. }
  593. if (unlikely(!pskb_may_pull(skb, 8)))
  594. break;
  595. opth = (void *)skb->data;
  596. len = ipv6_optlen(opth);
  597. if (unlikely(!pskb_may_pull(skb, len)))
  598. break;
  599. proto = opth->nexthdr;
  600. __skb_pull(skb, len);
  601. }
  602. return ops;
  603. }
  604. static int ipv6_gso_send_check(struct sk_buff *skb)
  605. {
  606. struct ipv6hdr *ipv6h;
  607. struct inet6_protocol *ops;
  608. int err = -EINVAL;
  609. if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h))))
  610. goto out;
  611. ipv6h = ipv6_hdr(skb);
  612. __skb_pull(skb, sizeof(*ipv6h));
  613. err = -EPROTONOSUPPORT;
  614. rcu_read_lock();
  615. ops = ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr);
  616. if (likely(ops && ops->gso_send_check)) {
  617. skb_reset_transport_header(skb);
  618. err = ops->gso_send_check(skb);
  619. }
  620. rcu_read_unlock();
  621. out:
  622. return err;
  623. }
  624. static struct sk_buff *ipv6_gso_segment(struct sk_buff *skb, int features)
  625. {
  626. struct sk_buff *segs = ERR_PTR(-EINVAL);
  627. struct ipv6hdr *ipv6h;
  628. struct inet6_protocol *ops;
  629. if (!(features & NETIF_F_V6_CSUM))
  630. features &= ~NETIF_F_SG;
  631. if (unlikely(skb_shinfo(skb)->gso_type &
  632. ~(SKB_GSO_UDP |
  633. SKB_GSO_DODGY |
  634. SKB_GSO_TCP_ECN |
  635. SKB_GSO_TCPV6 |
  636. 0)))
  637. goto out;
  638. if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h))))
  639. goto out;
  640. ipv6h = ipv6_hdr(skb);
  641. __skb_pull(skb, sizeof(*ipv6h));
  642. segs = ERR_PTR(-EPROTONOSUPPORT);
  643. rcu_read_lock();
  644. ops = ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr);
  645. if (likely(ops && ops->gso_segment)) {
  646. skb_reset_transport_header(skb);
  647. segs = ops->gso_segment(skb, features);
  648. }
  649. rcu_read_unlock();
  650. if (unlikely(IS_ERR(segs)))
  651. goto out;
  652. for (skb = segs; skb; skb = skb->next) {
  653. ipv6h = ipv6_hdr(skb);
  654. ipv6h->payload_len = htons(skb->len - skb->mac_len -
  655. sizeof(*ipv6h));
  656. }
  657. out:
  658. return segs;
  659. }
  660. static struct packet_type ipv6_packet_type = {
  661. .type = __constant_htons(ETH_P_IPV6),
  662. .func = ipv6_rcv,
  663. .gso_send_check = ipv6_gso_send_check,
  664. .gso_segment = ipv6_gso_segment,
  665. };
  666. static int __init ipv6_packet_init(void)
  667. {
  668. dev_add_pack(&ipv6_packet_type);
  669. return 0;
  670. }
  671. static void ipv6_packet_cleanup(void)
  672. {
  673. dev_remove_pack(&ipv6_packet_type);
  674. }
  675. static int __init init_ipv6_mibs(void)
  676. {
  677. if (snmp_mib_init((void **)ipv6_statistics,
  678. sizeof(struct ipstats_mib)) < 0)
  679. goto err_ip_mib;
  680. if (snmp_mib_init((void **)icmpv6_statistics,
  681. sizeof(struct icmpv6_mib)) < 0)
  682. goto err_icmp_mib;
  683. if (snmp_mib_init((void **)icmpv6msg_statistics,
  684. sizeof(struct icmpv6msg_mib)) < 0)
  685. goto err_icmpmsg_mib;
  686. if (snmp_mib_init((void **)udp_stats_in6, sizeof (struct udp_mib)) < 0)
  687. goto err_udp_mib;
  688. if (snmp_mib_init((void **)udplite_stats_in6,
  689. sizeof (struct udp_mib)) < 0)
  690. goto err_udplite_mib;
  691. return 0;
  692. err_udplite_mib:
  693. snmp_mib_free((void **)udp_stats_in6);
  694. err_udp_mib:
  695. snmp_mib_free((void **)icmpv6msg_statistics);
  696. err_icmpmsg_mib:
  697. snmp_mib_free((void **)icmpv6_statistics);
  698. err_icmp_mib:
  699. snmp_mib_free((void **)ipv6_statistics);
  700. err_ip_mib:
  701. return -ENOMEM;
  702. }
  703. static void cleanup_ipv6_mibs(void)
  704. {
  705. snmp_mib_free((void **)ipv6_statistics);
  706. snmp_mib_free((void **)icmpv6_statistics);
  707. snmp_mib_free((void **)icmpv6msg_statistics);
  708. snmp_mib_free((void **)udp_stats_in6);
  709. snmp_mib_free((void **)udplite_stats_in6);
  710. }
  711. static int inet6_net_init(struct net *net)
  712. {
  713. int err = 0;
  714. net->ipv6.sysctl.bindv6only = 0;
  715. net->ipv6.sysctl.flush_delay = 0;
  716. net->ipv6.sysctl.ip6_rt_max_size = 4096;
  717. net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
  718. net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
  719. net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
  720. net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
  721. net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
  722. net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
  723. net->ipv6.sysctl.icmpv6_time = 1*HZ;
  724. #ifdef CONFIG_PROC_FS
  725. err = udp6_proc_init(net);
  726. if (err)
  727. goto out;
  728. err = tcp6_proc_init(net);
  729. if (err)
  730. goto proc_tcp6_fail;
  731. err = ac6_proc_init(net);
  732. if (err)
  733. goto proc_ac6_fail;
  734. out:
  735. #endif
  736. return err;
  737. #ifdef CONFIG_PROC_FS
  738. proc_ac6_fail:
  739. tcp6_proc_exit(net);
  740. proc_tcp6_fail:
  741. udp6_proc_exit(net);
  742. goto out;
  743. #endif
  744. }
  745. static void inet6_net_exit(struct net *net)
  746. {
  747. #ifdef CONFIG_PROC_FS
  748. udp6_proc_exit(net);
  749. tcp6_proc_exit(net);
  750. ac6_proc_exit(net);
  751. #endif
  752. }
  753. static struct pernet_operations inet6_net_ops = {
  754. .init = inet6_net_init,
  755. .exit = inet6_net_exit,
  756. };
  757. static int __init inet6_init(void)
  758. {
  759. struct sk_buff *dummy_skb;
  760. struct list_head *r;
  761. int err;
  762. BUILD_BUG_ON(sizeof(struct inet6_skb_parm) > sizeof(dummy_skb->cb));
  763. err = proto_register(&tcpv6_prot, 1);
  764. if (err)
  765. goto out;
  766. err = proto_register(&udpv6_prot, 1);
  767. if (err)
  768. goto out_unregister_tcp_proto;
  769. err = proto_register(&udplitev6_prot, 1);
  770. if (err)
  771. goto out_unregister_udp_proto;
  772. err = proto_register(&rawv6_prot, 1);
  773. if (err)
  774. goto out_unregister_udplite_proto;
  775. /* Register the socket-side information for inet6_create. */
  776. for(r = &inetsw6[0]; r < &inetsw6[SOCK_MAX]; ++r)
  777. INIT_LIST_HEAD(r);
  778. /* We MUST register RAW sockets before we create the ICMP6,
  779. * IGMP6, or NDISC control sockets.
  780. */
  781. err = rawv6_init();
  782. if (err)
  783. goto out_unregister_raw_proto;
  784. /* Register the family here so that the init calls below will
  785. * be able to create sockets. (?? is this dangerous ??)
  786. */
  787. err = sock_register(&inet6_family_ops);
  788. if (err)
  789. goto out_sock_register_fail;
  790. /* Initialise ipv6 mibs */
  791. err = init_ipv6_mibs();
  792. if (err)
  793. goto out_unregister_sock;
  794. /*
  795. * ipngwg API draft makes clear that the correct semantics
  796. * for TCP and UDP is to consider one TCP and UDP instance
  797. * in a host availiable by both INET and INET6 APIs and
  798. * able to communicate via both network protocols.
  799. */
  800. err = register_pernet_subsys(&inet6_net_ops);
  801. if (err)
  802. goto register_pernet_fail;
  803. err = icmpv6_init();
  804. if (err)
  805. goto icmp_fail;
  806. #ifdef CONFIG_IPV6_MROUTE
  807. ip6_mr_init();
  808. #endif
  809. err = ndisc_init();
  810. if (err)
  811. goto ndisc_fail;
  812. err = igmp6_init();
  813. if (err)
  814. goto igmp_fail;
  815. err = ipv6_netfilter_init();
  816. if (err)
  817. goto netfilter_fail;
  818. /* Create /proc/foo6 entries. */
  819. #ifdef CONFIG_PROC_FS
  820. err = -ENOMEM;
  821. if (raw6_proc_init())
  822. goto proc_raw6_fail;
  823. if (udplite6_proc_init())
  824. goto proc_udplite6_fail;
  825. if (ipv6_misc_proc_init())
  826. goto proc_misc6_fail;
  827. if (if6_proc_init())
  828. goto proc_if6_fail;
  829. #endif
  830. err = ip6_route_init();
  831. if (err)
  832. goto ip6_route_fail;
  833. err = ip6_flowlabel_init();
  834. if (err)
  835. goto ip6_flowlabel_fail;
  836. err = addrconf_init();
  837. if (err)
  838. goto addrconf_fail;
  839. /* Init v6 extension headers. */
  840. err = ipv6_exthdrs_init();
  841. if (err)
  842. goto ipv6_exthdrs_fail;
  843. err = ipv6_frag_init();
  844. if (err)
  845. goto ipv6_frag_fail;
  846. /* Init v6 transport protocols. */
  847. err = udpv6_init();
  848. if (err)
  849. goto udpv6_fail;
  850. err = udplitev6_init();
  851. if (err)
  852. goto udplitev6_fail;
  853. err = tcpv6_init();
  854. if (err)
  855. goto tcpv6_fail;
  856. err = ipv6_packet_init();
  857. if (err)
  858. goto ipv6_packet_fail;
  859. #ifdef CONFIG_SYSCTL
  860. err = ipv6_sysctl_register();
  861. if (err)
  862. goto sysctl_fail;
  863. #endif
  864. out:
  865. return err;
  866. #ifdef CONFIG_SYSCTL
  867. sysctl_fail:
  868. ipv6_packet_cleanup();
  869. #endif
  870. ipv6_packet_fail:
  871. tcpv6_exit();
  872. tcpv6_fail:
  873. udplitev6_exit();
  874. udplitev6_fail:
  875. udpv6_exit();
  876. udpv6_fail:
  877. ipv6_frag_exit();
  878. ipv6_frag_fail:
  879. ipv6_exthdrs_exit();
  880. ipv6_exthdrs_fail:
  881. addrconf_cleanup();
  882. addrconf_fail:
  883. ip6_flowlabel_cleanup();
  884. ip6_flowlabel_fail:
  885. ip6_route_cleanup();
  886. ip6_route_fail:
  887. #ifdef CONFIG_PROC_FS
  888. if6_proc_exit();
  889. proc_if6_fail:
  890. ipv6_misc_proc_exit();
  891. proc_misc6_fail:
  892. udplite6_proc_exit();
  893. proc_udplite6_fail:
  894. raw6_proc_exit();
  895. proc_raw6_fail:
  896. #endif
  897. ipv6_netfilter_fini();
  898. netfilter_fail:
  899. igmp6_cleanup();
  900. igmp_fail:
  901. ndisc_cleanup();
  902. ndisc_fail:
  903. icmpv6_cleanup();
  904. icmp_fail:
  905. unregister_pernet_subsys(&inet6_net_ops);
  906. register_pernet_fail:
  907. cleanup_ipv6_mibs();
  908. out_unregister_sock:
  909. sock_unregister(PF_INET6);
  910. rtnl_unregister_all(PF_INET6);
  911. out_sock_register_fail:
  912. rawv6_exit();
  913. out_unregister_raw_proto:
  914. proto_unregister(&rawv6_prot);
  915. out_unregister_udplite_proto:
  916. proto_unregister(&udplitev6_prot);
  917. out_unregister_udp_proto:
  918. proto_unregister(&udpv6_prot);
  919. out_unregister_tcp_proto:
  920. proto_unregister(&tcpv6_prot);
  921. goto out;
  922. }
  923. module_init(inet6_init);
  924. static void __exit inet6_exit(void)
  925. {
  926. /* First of all disallow new sockets creation. */
  927. sock_unregister(PF_INET6);
  928. /* Disallow any further netlink messages */
  929. rtnl_unregister_all(PF_INET6);
  930. #ifdef CONFIG_SYSCTL
  931. ipv6_sysctl_unregister();
  932. #endif
  933. udpv6_exit();
  934. udplitev6_exit();
  935. tcpv6_exit();
  936. /* Cleanup code parts. */
  937. ipv6_packet_cleanup();
  938. ipv6_frag_exit();
  939. ipv6_exthdrs_exit();
  940. addrconf_cleanup();
  941. ip6_flowlabel_cleanup();
  942. ip6_route_cleanup();
  943. #ifdef CONFIG_PROC_FS
  944. /* Cleanup code parts. */
  945. if6_proc_exit();
  946. ipv6_misc_proc_exit();
  947. udplite6_proc_exit();
  948. raw6_proc_exit();
  949. #endif
  950. ipv6_netfilter_fini();
  951. igmp6_cleanup();
  952. ndisc_cleanup();
  953. icmpv6_cleanup();
  954. rawv6_exit();
  955. unregister_pernet_subsys(&inet6_net_ops);
  956. cleanup_ipv6_mibs();
  957. proto_unregister(&rawv6_prot);
  958. proto_unregister(&udplitev6_prot);
  959. proto_unregister(&udpv6_prot);
  960. proto_unregister(&tcpv6_prot);
  961. }
  962. module_exit(inet6_exit);
  963. MODULE_ALIAS_NETPROTO(PF_INET6);