af_inet6.c 25 KB

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