af_inet6.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239
  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(sock_net(sk), &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 int ipv6_gso_pull_exthdrs(struct sk_buff *skb, int proto)
  575. {
  576. struct inet6_protocol *ops = NULL;
  577. for (;;) {
  578. struct ipv6_opt_hdr *opth;
  579. int len;
  580. if (proto != NEXTHDR_HOP) {
  581. ops = rcu_dereference(inet6_protos[proto]);
  582. if (unlikely(!ops))
  583. break;
  584. if (!(ops->flags & INET6_PROTO_GSO_EXTHDR))
  585. break;
  586. }
  587. if (unlikely(!pskb_may_pull(skb, 8)))
  588. break;
  589. opth = (void *)skb->data;
  590. len = ipv6_optlen(opth);
  591. if (unlikely(!pskb_may_pull(skb, len)))
  592. break;
  593. proto = opth->nexthdr;
  594. __skb_pull(skb, len);
  595. }
  596. return proto;
  597. }
  598. static int ipv6_gso_send_check(struct sk_buff *skb)
  599. {
  600. struct ipv6hdr *ipv6h;
  601. struct inet6_protocol *ops;
  602. int err = -EINVAL;
  603. if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h))))
  604. goto out;
  605. ipv6h = ipv6_hdr(skb);
  606. __skb_pull(skb, sizeof(*ipv6h));
  607. err = -EPROTONOSUPPORT;
  608. rcu_read_lock();
  609. ops = rcu_dereference(inet6_protos[
  610. 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 = rcu_dereference(inet6_protos[
  640. ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr)]);
  641. if (likely(ops && ops->gso_segment)) {
  642. skb_reset_transport_header(skb);
  643. segs = ops->gso_segment(skb, features);
  644. }
  645. rcu_read_unlock();
  646. if (unlikely(IS_ERR(segs)))
  647. goto out;
  648. for (skb = segs; skb; skb = skb->next) {
  649. ipv6h = ipv6_hdr(skb);
  650. ipv6h->payload_len = htons(skb->len - skb->mac_len -
  651. sizeof(*ipv6h));
  652. }
  653. out:
  654. return segs;
  655. }
  656. struct ipv6_gro_cb {
  657. struct napi_gro_cb napi;
  658. int proto;
  659. };
  660. #define IPV6_GRO_CB(skb) ((struct ipv6_gro_cb *)(skb)->cb)
  661. static struct sk_buff **ipv6_gro_receive(struct sk_buff **head,
  662. struct sk_buff *skb)
  663. {
  664. struct inet6_protocol *ops;
  665. struct sk_buff **pp = NULL;
  666. struct sk_buff *p;
  667. struct ipv6hdr *iph;
  668. unsigned int nlen;
  669. int flush = 1;
  670. int proto;
  671. __wsum csum;
  672. iph = skb_gro_header(skb, sizeof(*iph));
  673. if (unlikely(!iph))
  674. goto out;
  675. skb_gro_pull(skb, sizeof(*iph));
  676. skb_set_transport_header(skb, skb_gro_offset(skb));
  677. flush += ntohs(iph->payload_len) != skb_gro_len(skb);
  678. rcu_read_lock();
  679. proto = iph->nexthdr;
  680. ops = rcu_dereference(inet6_protos[proto]);
  681. if (!ops || !ops->gro_receive) {
  682. __pskb_pull(skb, skb_gro_offset(skb));
  683. proto = ipv6_gso_pull_exthdrs(skb, proto);
  684. skb_gro_pull(skb, -skb_transport_offset(skb));
  685. skb_reset_transport_header(skb);
  686. __skb_push(skb, skb_gro_offset(skb));
  687. if (!ops || !ops->gro_receive)
  688. goto out_unlock;
  689. iph = ipv6_hdr(skb);
  690. }
  691. IPV6_GRO_CB(skb)->proto = proto;
  692. flush--;
  693. nlen = skb_network_header_len(skb);
  694. for (p = *head; p; p = p->next) {
  695. struct ipv6hdr *iph2;
  696. if (!NAPI_GRO_CB(p)->same_flow)
  697. continue;
  698. iph2 = ipv6_hdr(p);
  699. /* All fields must match except length. */
  700. if (nlen != skb_network_header_len(p) ||
  701. memcmp(iph, iph2, offsetof(struct ipv6hdr, payload_len)) ||
  702. memcmp(&iph->nexthdr, &iph2->nexthdr,
  703. nlen - offsetof(struct ipv6hdr, nexthdr))) {
  704. NAPI_GRO_CB(p)->same_flow = 0;
  705. continue;
  706. }
  707. NAPI_GRO_CB(p)->flush |= flush;
  708. }
  709. NAPI_GRO_CB(skb)->flush |= flush;
  710. csum = skb->csum;
  711. skb_postpull_rcsum(skb, iph, skb_network_header_len(skb));
  712. pp = ops->gro_receive(head, skb);
  713. skb->csum = csum;
  714. out_unlock:
  715. rcu_read_unlock();
  716. out:
  717. NAPI_GRO_CB(skb)->flush |= flush;
  718. return pp;
  719. }
  720. static int ipv6_gro_complete(struct sk_buff *skb)
  721. {
  722. struct inet6_protocol *ops;
  723. struct ipv6hdr *iph = ipv6_hdr(skb);
  724. int err = -ENOSYS;
  725. iph->payload_len = htons(skb->len - skb_network_offset(skb) -
  726. sizeof(*iph));
  727. rcu_read_lock();
  728. ops = rcu_dereference(inet6_protos[IPV6_GRO_CB(skb)->proto]);
  729. if (WARN_ON(!ops || !ops->gro_complete))
  730. goto out_unlock;
  731. err = ops->gro_complete(skb);
  732. out_unlock:
  733. rcu_read_unlock();
  734. return err;
  735. }
  736. static struct packet_type ipv6_packet_type = {
  737. .type = cpu_to_be16(ETH_P_IPV6),
  738. .func = ipv6_rcv,
  739. .gso_send_check = ipv6_gso_send_check,
  740. .gso_segment = ipv6_gso_segment,
  741. .gro_receive = ipv6_gro_receive,
  742. .gro_complete = ipv6_gro_complete,
  743. };
  744. static int __init ipv6_packet_init(void)
  745. {
  746. dev_add_pack(&ipv6_packet_type);
  747. return 0;
  748. }
  749. static void ipv6_packet_cleanup(void)
  750. {
  751. dev_remove_pack(&ipv6_packet_type);
  752. }
  753. static int __net_init ipv6_init_mibs(struct net *net)
  754. {
  755. if (snmp_mib_init((void **)net->mib.udp_stats_in6,
  756. sizeof (struct udp_mib)) < 0)
  757. return -ENOMEM;
  758. if (snmp_mib_init((void **)net->mib.udplite_stats_in6,
  759. sizeof (struct udp_mib)) < 0)
  760. goto err_udplite_mib;
  761. if (snmp_mib_init((void **)net->mib.ipv6_statistics,
  762. sizeof(struct ipstats_mib)) < 0)
  763. goto err_ip_mib;
  764. if (snmp_mib_init((void **)net->mib.icmpv6_statistics,
  765. sizeof(struct icmpv6_mib)) < 0)
  766. goto err_icmp_mib;
  767. if (snmp_mib_init((void **)net->mib.icmpv6msg_statistics,
  768. sizeof(struct icmpv6msg_mib)) < 0)
  769. goto err_icmpmsg_mib;
  770. return 0;
  771. err_icmpmsg_mib:
  772. snmp_mib_free((void **)net->mib.icmpv6_statistics);
  773. err_icmp_mib:
  774. snmp_mib_free((void **)net->mib.ipv6_statistics);
  775. err_ip_mib:
  776. snmp_mib_free((void **)net->mib.udplite_stats_in6);
  777. err_udplite_mib:
  778. snmp_mib_free((void **)net->mib.udp_stats_in6);
  779. return -ENOMEM;
  780. }
  781. static void __net_exit ipv6_cleanup_mibs(struct net *net)
  782. {
  783. snmp_mib_free((void **)net->mib.udp_stats_in6);
  784. snmp_mib_free((void **)net->mib.udplite_stats_in6);
  785. snmp_mib_free((void **)net->mib.ipv6_statistics);
  786. snmp_mib_free((void **)net->mib.icmpv6_statistics);
  787. snmp_mib_free((void **)net->mib.icmpv6msg_statistics);
  788. }
  789. static int __net_init inet6_net_init(struct net *net)
  790. {
  791. int err = 0;
  792. net->ipv6.sysctl.bindv6only = 0;
  793. net->ipv6.sysctl.icmpv6_time = 1*HZ;
  794. err = ipv6_init_mibs(net);
  795. if (err)
  796. return err;
  797. #ifdef CONFIG_PROC_FS
  798. err = udp6_proc_init(net);
  799. if (err)
  800. goto out;
  801. err = tcp6_proc_init(net);
  802. if (err)
  803. goto proc_tcp6_fail;
  804. err = ac6_proc_init(net);
  805. if (err)
  806. goto proc_ac6_fail;
  807. #endif
  808. return err;
  809. #ifdef CONFIG_PROC_FS
  810. proc_ac6_fail:
  811. tcp6_proc_exit(net);
  812. proc_tcp6_fail:
  813. udp6_proc_exit(net);
  814. out:
  815. ipv6_cleanup_mibs(net);
  816. return err;
  817. #endif
  818. }
  819. static void inet6_net_exit(struct net *net)
  820. {
  821. #ifdef CONFIG_PROC_FS
  822. udp6_proc_exit(net);
  823. tcp6_proc_exit(net);
  824. ac6_proc_exit(net);
  825. #endif
  826. ipv6_cleanup_mibs(net);
  827. }
  828. static struct pernet_operations inet6_net_ops = {
  829. .init = inet6_net_init,
  830. .exit = inet6_net_exit,
  831. };
  832. static int __init inet6_init(void)
  833. {
  834. struct sk_buff *dummy_skb;
  835. struct list_head *r;
  836. int err;
  837. BUILD_BUG_ON(sizeof(struct inet6_skb_parm) > sizeof(dummy_skb->cb));
  838. err = proto_register(&tcpv6_prot, 1);
  839. if (err)
  840. goto out;
  841. err = proto_register(&udpv6_prot, 1);
  842. if (err)
  843. goto out_unregister_tcp_proto;
  844. err = proto_register(&udplitev6_prot, 1);
  845. if (err)
  846. goto out_unregister_udp_proto;
  847. err = proto_register(&rawv6_prot, 1);
  848. if (err)
  849. goto out_unregister_udplite_proto;
  850. /* Register the socket-side information for inet6_create. */
  851. for(r = &inetsw6[0]; r < &inetsw6[SOCK_MAX]; ++r)
  852. INIT_LIST_HEAD(r);
  853. /* We MUST register RAW sockets before we create the ICMP6,
  854. * IGMP6, or NDISC control sockets.
  855. */
  856. err = rawv6_init();
  857. if (err)
  858. goto out_unregister_raw_proto;
  859. /* Register the family here so that the init calls below will
  860. * be able to create sockets. (?? is this dangerous ??)
  861. */
  862. err = sock_register(&inet6_family_ops);
  863. if (err)
  864. goto out_sock_register_fail;
  865. #ifdef CONFIG_SYSCTL
  866. err = ipv6_static_sysctl_register();
  867. if (err)
  868. goto static_sysctl_fail;
  869. #endif
  870. /*
  871. * ipngwg API draft makes clear that the correct semantics
  872. * for TCP and UDP is to consider one TCP and UDP instance
  873. * in a host availiable by both INET and INET6 APIs and
  874. * able to communicate via both network protocols.
  875. */
  876. err = register_pernet_subsys(&inet6_net_ops);
  877. if (err)
  878. goto register_pernet_fail;
  879. err = icmpv6_init();
  880. if (err)
  881. goto icmp_fail;
  882. err = ip6_mr_init();
  883. if (err)
  884. goto ipmr_fail;
  885. err = ndisc_init();
  886. if (err)
  887. goto ndisc_fail;
  888. err = igmp6_init();
  889. if (err)
  890. goto igmp_fail;
  891. err = ipv6_netfilter_init();
  892. if (err)
  893. goto netfilter_fail;
  894. /* Create /proc/foo6 entries. */
  895. #ifdef CONFIG_PROC_FS
  896. err = -ENOMEM;
  897. if (raw6_proc_init())
  898. goto proc_raw6_fail;
  899. if (udplite6_proc_init())
  900. goto proc_udplite6_fail;
  901. if (ipv6_misc_proc_init())
  902. goto proc_misc6_fail;
  903. if (if6_proc_init())
  904. goto proc_if6_fail;
  905. #endif
  906. err = ip6_route_init();
  907. if (err)
  908. goto ip6_route_fail;
  909. err = ip6_flowlabel_init();
  910. if (err)
  911. goto ip6_flowlabel_fail;
  912. err = addrconf_init();
  913. if (err)
  914. goto addrconf_fail;
  915. /* Init v6 extension headers. */
  916. err = ipv6_exthdrs_init();
  917. if (err)
  918. goto ipv6_exthdrs_fail;
  919. err = ipv6_frag_init();
  920. if (err)
  921. goto ipv6_frag_fail;
  922. /* Init v6 transport protocols. */
  923. err = udpv6_init();
  924. if (err)
  925. goto udpv6_fail;
  926. err = udplitev6_init();
  927. if (err)
  928. goto udplitev6_fail;
  929. err = tcpv6_init();
  930. if (err)
  931. goto tcpv6_fail;
  932. err = ipv6_packet_init();
  933. if (err)
  934. goto ipv6_packet_fail;
  935. #ifdef CONFIG_SYSCTL
  936. err = ipv6_sysctl_register();
  937. if (err)
  938. goto sysctl_fail;
  939. #endif
  940. out:
  941. return err;
  942. #ifdef CONFIG_SYSCTL
  943. sysctl_fail:
  944. ipv6_packet_cleanup();
  945. #endif
  946. ipv6_packet_fail:
  947. tcpv6_exit();
  948. tcpv6_fail:
  949. udplitev6_exit();
  950. udplitev6_fail:
  951. udpv6_exit();
  952. udpv6_fail:
  953. ipv6_frag_exit();
  954. ipv6_frag_fail:
  955. ipv6_exthdrs_exit();
  956. ipv6_exthdrs_fail:
  957. addrconf_cleanup();
  958. addrconf_fail:
  959. ip6_flowlabel_cleanup();
  960. ip6_flowlabel_fail:
  961. ip6_route_cleanup();
  962. ip6_route_fail:
  963. #ifdef CONFIG_PROC_FS
  964. if6_proc_exit();
  965. proc_if6_fail:
  966. ipv6_misc_proc_exit();
  967. proc_misc6_fail:
  968. udplite6_proc_exit();
  969. proc_udplite6_fail:
  970. raw6_proc_exit();
  971. proc_raw6_fail:
  972. #endif
  973. ipv6_netfilter_fini();
  974. netfilter_fail:
  975. igmp6_cleanup();
  976. igmp_fail:
  977. ndisc_cleanup();
  978. ndisc_fail:
  979. ip6_mr_cleanup();
  980. ipmr_fail:
  981. icmpv6_cleanup();
  982. icmp_fail:
  983. unregister_pernet_subsys(&inet6_net_ops);
  984. register_pernet_fail:
  985. #ifdef CONFIG_SYSCTL
  986. ipv6_static_sysctl_unregister();
  987. static_sysctl_fail:
  988. #endif
  989. sock_unregister(PF_INET6);
  990. rtnl_unregister_all(PF_INET6);
  991. out_sock_register_fail:
  992. rawv6_exit();
  993. out_unregister_raw_proto:
  994. proto_unregister(&rawv6_prot);
  995. out_unregister_udplite_proto:
  996. proto_unregister(&udplitev6_prot);
  997. out_unregister_udp_proto:
  998. proto_unregister(&udpv6_prot);
  999. out_unregister_tcp_proto:
  1000. proto_unregister(&tcpv6_prot);
  1001. goto out;
  1002. }
  1003. module_init(inet6_init);
  1004. static void __exit inet6_exit(void)
  1005. {
  1006. /* First of all disallow new sockets creation. */
  1007. sock_unregister(PF_INET6);
  1008. /* Disallow any further netlink messages */
  1009. rtnl_unregister_all(PF_INET6);
  1010. #ifdef CONFIG_SYSCTL
  1011. ipv6_sysctl_unregister();
  1012. #endif
  1013. udpv6_exit();
  1014. udplitev6_exit();
  1015. tcpv6_exit();
  1016. /* Cleanup code parts. */
  1017. ipv6_packet_cleanup();
  1018. ipv6_frag_exit();
  1019. ipv6_exthdrs_exit();
  1020. addrconf_cleanup();
  1021. ip6_flowlabel_cleanup();
  1022. ip6_route_cleanup();
  1023. #ifdef CONFIG_PROC_FS
  1024. /* Cleanup code parts. */
  1025. if6_proc_exit();
  1026. ipv6_misc_proc_exit();
  1027. udplite6_proc_exit();
  1028. raw6_proc_exit();
  1029. #endif
  1030. ipv6_netfilter_fini();
  1031. igmp6_cleanup();
  1032. ndisc_cleanup();
  1033. ip6_mr_cleanup();
  1034. icmpv6_cleanup();
  1035. rawv6_exit();
  1036. unregister_pernet_subsys(&inet6_net_ops);
  1037. #ifdef CONFIG_SYSCTL
  1038. ipv6_static_sysctl_unregister();
  1039. #endif
  1040. proto_unregister(&rawv6_prot);
  1041. proto_unregister(&udplitev6_prot);
  1042. proto_unregister(&udpv6_prot);
  1043. proto_unregister(&tcpv6_prot);
  1044. }
  1045. module_exit(inet6_exit);
  1046. MODULE_ALIAS_NETPROTO(PF_INET6);