af_inet6.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260
  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 int disable_ipv6 = 0;
  68. module_param_named(disable, disable_ipv6, int, 0);
  69. MODULE_PARM_DESC(disable, "Disable IPv6 such that it is non-functional");
  70. static __inline__ struct ipv6_pinfo *inet6_sk_generic(struct sock *sk)
  71. {
  72. const int offset = sk->sk_prot->obj_size - sizeof(struct ipv6_pinfo);
  73. return (struct ipv6_pinfo *)(((u8 *)sk) + offset);
  74. }
  75. static int inet6_create(struct net *net, struct socket *sock, int protocol)
  76. {
  77. struct inet_sock *inet;
  78. struct ipv6_pinfo *np;
  79. struct sock *sk;
  80. struct inet_protosw *answer;
  81. struct proto *answer_prot;
  82. unsigned char answer_flags;
  83. char answer_no_check;
  84. int try_loading_module = 0;
  85. int err;
  86. if (sock->type != SOCK_RAW &&
  87. sock->type != SOCK_DGRAM &&
  88. !inet_ehash_secret)
  89. build_ehash_secret();
  90. /* Look for the requested type/protocol pair. */
  91. lookup_protocol:
  92. err = -ESOCKTNOSUPPORT;
  93. rcu_read_lock();
  94. list_for_each_entry_rcu(answer, &inetsw6[sock->type], list) {
  95. err = 0;
  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. }
  111. if (err) {
  112. if (try_loading_module < 2) {
  113. rcu_read_unlock();
  114. /*
  115. * Be more specific, e.g. net-pf-10-proto-132-type-1
  116. * (net-pf-PF_INET6-proto-IPPROTO_SCTP-type-SOCK_STREAM)
  117. */
  118. if (++try_loading_module == 1)
  119. request_module("net-pf-%d-proto-%d-type-%d",
  120. PF_INET6, protocol, sock->type);
  121. /*
  122. * Fall back to generic, e.g. net-pf-10-proto-132
  123. * (net-pf-PF_INET6-proto-IPPROTO_SCTP)
  124. */
  125. else
  126. request_module("net-pf-%d-proto-%d",
  127. PF_INET6, protocol);
  128. goto lookup_protocol;
  129. } else
  130. goto out_rcu_unlock;
  131. }
  132. err = -EPERM;
  133. if (answer->capability > 0 && !capable(answer->capability))
  134. goto out_rcu_unlock;
  135. sock->ops = answer->ops;
  136. answer_prot = answer->prot;
  137. answer_no_check = answer->no_check;
  138. answer_flags = answer->flags;
  139. rcu_read_unlock();
  140. WARN_ON(answer_prot->slab == NULL);
  141. err = -ENOBUFS;
  142. sk = sk_alloc(net, PF_INET6, GFP_KERNEL, answer_prot);
  143. if (sk == NULL)
  144. goto out;
  145. sock_init_data(sock, sk);
  146. err = 0;
  147. sk->sk_no_check = answer_no_check;
  148. if (INET_PROTOSW_REUSE & answer_flags)
  149. sk->sk_reuse = 1;
  150. inet = inet_sk(sk);
  151. inet->is_icsk = (INET_PROTOSW_ICSK & answer_flags) != 0;
  152. if (SOCK_RAW == sock->type) {
  153. inet->num = protocol;
  154. if (IPPROTO_RAW == protocol)
  155. inet->hdrincl = 1;
  156. }
  157. sk->sk_destruct = inet_sock_destruct;
  158. sk->sk_family = PF_INET6;
  159. sk->sk_protocol = protocol;
  160. sk->sk_backlog_rcv = answer->prot->backlog_rcv;
  161. inet_sk(sk)->pinet6 = np = inet6_sk_generic(sk);
  162. np->hop_limit = -1;
  163. np->mcast_hops = -1;
  164. np->mc_loop = 1;
  165. np->pmtudisc = IPV6_PMTUDISC_WANT;
  166. np->ipv6only = net->ipv6.sysctl.bindv6only;
  167. /* Init the ipv4 part of the socket since we can have sockets
  168. * using v6 API for ipv4.
  169. */
  170. inet->uc_ttl = -1;
  171. inet->mc_loop = 1;
  172. inet->mc_ttl = 1;
  173. inet->mc_index = 0;
  174. inet->mc_list = NULL;
  175. if (ipv4_config.no_pmtu_disc)
  176. inet->pmtudisc = IP_PMTUDISC_DONT;
  177. else
  178. inet->pmtudisc = IP_PMTUDISC_WANT;
  179. /*
  180. * Increment only the relevant sk_prot->socks debug field, this changes
  181. * the previous behaviour of incrementing both the equivalent to
  182. * answer->prot->socks (inet6_sock_nr) and inet_sock_nr.
  183. *
  184. * This allows better debug granularity as we'll know exactly how many
  185. * UDPv6, TCPv6, etc socks were allocated, not the sum of all IPv6
  186. * transport protocol socks. -acme
  187. */
  188. sk_refcnt_debug_inc(sk);
  189. if (inet->num) {
  190. /* It assumes that any protocol which allows
  191. * the user to assign a number at socket
  192. * creation time automatically shares.
  193. */
  194. inet->sport = htons(inet->num);
  195. sk->sk_prot->hash(sk);
  196. }
  197. if (sk->sk_prot->init) {
  198. err = sk->sk_prot->init(sk);
  199. if (err) {
  200. sk_common_release(sk);
  201. goto out;
  202. }
  203. }
  204. out:
  205. return err;
  206. out_rcu_unlock:
  207. rcu_read_unlock();
  208. goto out;
  209. }
  210. /* bind for INET6 API */
  211. int inet6_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
  212. {
  213. struct sockaddr_in6 *addr=(struct sockaddr_in6 *)uaddr;
  214. struct sock *sk = sock->sk;
  215. struct inet_sock *inet = inet_sk(sk);
  216. struct ipv6_pinfo *np = inet6_sk(sk);
  217. struct net *net = sock_net(sk);
  218. __be32 v4addr = 0;
  219. unsigned short snum;
  220. int addr_type = 0;
  221. int err = 0;
  222. /* If the socket has its own bind function then use it. */
  223. if (sk->sk_prot->bind)
  224. return sk->sk_prot->bind(sk, uaddr, addr_len);
  225. if (addr_len < SIN6_LEN_RFC2133)
  226. return -EINVAL;
  227. addr_type = ipv6_addr_type(&addr->sin6_addr);
  228. if ((addr_type & IPV6_ADDR_MULTICAST) && sock->type == SOCK_STREAM)
  229. return -EINVAL;
  230. snum = ntohs(addr->sin6_port);
  231. if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
  232. return -EACCES;
  233. lock_sock(sk);
  234. /* Check these errors (active socket, double bind). */
  235. if (sk->sk_state != TCP_CLOSE || inet->num) {
  236. err = -EINVAL;
  237. goto out;
  238. }
  239. /* Check if the address belongs to the host. */
  240. if (addr_type == IPV6_ADDR_MAPPED) {
  241. /* Binding to v4-mapped address on a v6-only socket
  242. * makes no sense
  243. */
  244. if (np->ipv6only) {
  245. err = -EINVAL;
  246. goto out;
  247. }
  248. v4addr = addr->sin6_addr.s6_addr32[3];
  249. if (inet_addr_type(net, v4addr) != RTN_LOCAL) {
  250. err = -EADDRNOTAVAIL;
  251. goto out;
  252. }
  253. } else {
  254. if (addr_type != IPV6_ADDR_ANY) {
  255. struct net_device *dev = NULL;
  256. if (addr_type & IPV6_ADDR_LINKLOCAL) {
  257. if (addr_len >= sizeof(struct sockaddr_in6) &&
  258. addr->sin6_scope_id) {
  259. /* Override any existing binding, if another one
  260. * is supplied by user.
  261. */
  262. sk->sk_bound_dev_if = addr->sin6_scope_id;
  263. }
  264. /* Binding to link-local address requires an interface */
  265. if (!sk->sk_bound_dev_if) {
  266. err = -EINVAL;
  267. goto out;
  268. }
  269. dev = dev_get_by_index(net, sk->sk_bound_dev_if);
  270. if (!dev) {
  271. err = -ENODEV;
  272. goto out;
  273. }
  274. }
  275. /* ipv4 addr of the socket is invalid. Only the
  276. * unspecified and mapped address have a v4 equivalent.
  277. */
  278. v4addr = LOOPBACK4_IPV6;
  279. if (!(addr_type & IPV6_ADDR_MULTICAST)) {
  280. if (!ipv6_chk_addr(net, &addr->sin6_addr,
  281. dev, 0)) {
  282. if (dev)
  283. dev_put(dev);
  284. err = -EADDRNOTAVAIL;
  285. goto out;
  286. }
  287. }
  288. if (dev)
  289. dev_put(dev);
  290. }
  291. }
  292. inet->rcv_saddr = v4addr;
  293. inet->saddr = v4addr;
  294. ipv6_addr_copy(&np->rcv_saddr, &addr->sin6_addr);
  295. if (!(addr_type & IPV6_ADDR_MULTICAST))
  296. ipv6_addr_copy(&np->saddr, &addr->sin6_addr);
  297. /* Make sure we are allowed to bind here. */
  298. if (sk->sk_prot->get_port(sk, snum)) {
  299. inet_reset_saddr(sk);
  300. err = -EADDRINUSE;
  301. goto out;
  302. }
  303. if (addr_type != IPV6_ADDR_ANY)
  304. sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
  305. if (snum)
  306. sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
  307. inet->sport = htons(inet->num);
  308. inet->dport = 0;
  309. inet->daddr = 0;
  310. out:
  311. release_sock(sk);
  312. return err;
  313. }
  314. EXPORT_SYMBOL(inet6_bind);
  315. int inet6_release(struct socket *sock)
  316. {
  317. struct sock *sk = sock->sk;
  318. if (sk == NULL)
  319. return -EINVAL;
  320. /* Free mc lists */
  321. ipv6_sock_mc_close(sk);
  322. /* Free ac lists */
  323. ipv6_sock_ac_close(sk);
  324. return inet_release(sock);
  325. }
  326. EXPORT_SYMBOL(inet6_release);
  327. void inet6_destroy_sock(struct sock *sk)
  328. {
  329. struct ipv6_pinfo *np = inet6_sk(sk);
  330. struct sk_buff *skb;
  331. struct ipv6_txoptions *opt;
  332. /* Release rx options */
  333. if ((skb = xchg(&np->pktoptions, NULL)) != NULL)
  334. kfree_skb(skb);
  335. /* Free flowlabels */
  336. fl6_free_socklist(sk);
  337. /* Free tx options */
  338. if ((opt = xchg(&np->opt, NULL)) != NULL)
  339. sock_kfree_s(sk, opt, opt->tot_len);
  340. }
  341. EXPORT_SYMBOL_GPL(inet6_destroy_sock);
  342. /*
  343. * This does both peername and sockname.
  344. */
  345. int inet6_getname(struct socket *sock, struct sockaddr *uaddr,
  346. int *uaddr_len, int peer)
  347. {
  348. struct sockaddr_in6 *sin=(struct sockaddr_in6 *)uaddr;
  349. struct sock *sk = sock->sk;
  350. struct inet_sock *inet = inet_sk(sk);
  351. struct ipv6_pinfo *np = inet6_sk(sk);
  352. sin->sin6_family = AF_INET6;
  353. sin->sin6_flowinfo = 0;
  354. sin->sin6_scope_id = 0;
  355. if (peer) {
  356. if (!inet->dport)
  357. return -ENOTCONN;
  358. if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
  359. peer == 1)
  360. return -ENOTCONN;
  361. sin->sin6_port = inet->dport;
  362. ipv6_addr_copy(&sin->sin6_addr, &np->daddr);
  363. if (np->sndflow)
  364. sin->sin6_flowinfo = np->flow_label;
  365. } else {
  366. if (ipv6_addr_any(&np->rcv_saddr))
  367. ipv6_addr_copy(&sin->sin6_addr, &np->saddr);
  368. else
  369. ipv6_addr_copy(&sin->sin6_addr, &np->rcv_saddr);
  370. sin->sin6_port = inet->sport;
  371. }
  372. if (ipv6_addr_type(&sin->sin6_addr) & IPV6_ADDR_LINKLOCAL)
  373. sin->sin6_scope_id = sk->sk_bound_dev_if;
  374. *uaddr_len = sizeof(*sin);
  375. return(0);
  376. }
  377. EXPORT_SYMBOL(inet6_getname);
  378. int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  379. {
  380. struct sock *sk = sock->sk;
  381. struct net *net = sock_net(sk);
  382. switch(cmd)
  383. {
  384. case SIOCGSTAMP:
  385. return sock_get_timestamp(sk, (struct timeval __user *)arg);
  386. case SIOCGSTAMPNS:
  387. return sock_get_timestampns(sk, (struct timespec __user *)arg);
  388. case SIOCADDRT:
  389. case SIOCDELRT:
  390. return(ipv6_route_ioctl(net, cmd, (void __user *)arg));
  391. case SIOCSIFADDR:
  392. return addrconf_add_ifaddr(net, (void __user *) arg);
  393. case SIOCDIFADDR:
  394. return addrconf_del_ifaddr(net, (void __user *) arg);
  395. case SIOCSIFDSTADDR:
  396. return addrconf_set_dstaddr(net, (void __user *) arg);
  397. default:
  398. if (!sk->sk_prot->ioctl)
  399. return -ENOIOCTLCMD;
  400. return sk->sk_prot->ioctl(sk, cmd, arg);
  401. }
  402. /*NOTREACHED*/
  403. return(0);
  404. }
  405. EXPORT_SYMBOL(inet6_ioctl);
  406. const struct proto_ops inet6_stream_ops = {
  407. .family = PF_INET6,
  408. .owner = THIS_MODULE,
  409. .release = inet6_release,
  410. .bind = inet6_bind,
  411. .connect = inet_stream_connect, /* ok */
  412. .socketpair = sock_no_socketpair, /* a do nothing */
  413. .accept = inet_accept, /* ok */
  414. .getname = inet6_getname,
  415. .poll = tcp_poll, /* ok */
  416. .ioctl = inet6_ioctl, /* must change */
  417. .listen = inet_listen, /* ok */
  418. .shutdown = inet_shutdown, /* ok */
  419. .setsockopt = sock_common_setsockopt, /* ok */
  420. .getsockopt = sock_common_getsockopt, /* ok */
  421. .sendmsg = tcp_sendmsg, /* ok */
  422. .recvmsg = sock_common_recvmsg, /* ok */
  423. .mmap = sock_no_mmap,
  424. .sendpage = tcp_sendpage,
  425. .splice_read = tcp_splice_read,
  426. #ifdef CONFIG_COMPAT
  427. .compat_setsockopt = compat_sock_common_setsockopt,
  428. .compat_getsockopt = compat_sock_common_getsockopt,
  429. #endif
  430. };
  431. const struct proto_ops inet6_dgram_ops = {
  432. .family = PF_INET6,
  433. .owner = THIS_MODULE,
  434. .release = inet6_release,
  435. .bind = inet6_bind,
  436. .connect = inet_dgram_connect, /* ok */
  437. .socketpair = sock_no_socketpair, /* a do nothing */
  438. .accept = sock_no_accept, /* a do nothing */
  439. .getname = inet6_getname,
  440. .poll = udp_poll, /* ok */
  441. .ioctl = inet6_ioctl, /* must change */
  442. .listen = sock_no_listen, /* ok */
  443. .shutdown = inet_shutdown, /* ok */
  444. .setsockopt = sock_common_setsockopt, /* ok */
  445. .getsockopt = sock_common_getsockopt, /* ok */
  446. .sendmsg = inet_sendmsg, /* ok */
  447. .recvmsg = sock_common_recvmsg, /* ok */
  448. .mmap = sock_no_mmap,
  449. .sendpage = sock_no_sendpage,
  450. #ifdef CONFIG_COMPAT
  451. .compat_setsockopt = compat_sock_common_setsockopt,
  452. .compat_getsockopt = compat_sock_common_getsockopt,
  453. #endif
  454. };
  455. static struct net_proto_family inet6_family_ops = {
  456. .family = PF_INET6,
  457. .create = inet6_create,
  458. .owner = THIS_MODULE,
  459. };
  460. int inet6_register_protosw(struct inet_protosw *p)
  461. {
  462. struct list_head *lh;
  463. struct inet_protosw *answer;
  464. struct list_head *last_perm;
  465. int protocol = p->protocol;
  466. int ret;
  467. spin_lock_bh(&inetsw6_lock);
  468. ret = -EINVAL;
  469. if (p->type >= SOCK_MAX)
  470. goto out_illegal;
  471. /* If we are trying to override a permanent protocol, bail. */
  472. answer = NULL;
  473. ret = -EPERM;
  474. last_perm = &inetsw6[p->type];
  475. list_for_each(lh, &inetsw6[p->type]) {
  476. answer = list_entry(lh, struct inet_protosw, list);
  477. /* Check only the non-wild match. */
  478. if (INET_PROTOSW_PERMANENT & answer->flags) {
  479. if (protocol == answer->protocol)
  480. break;
  481. last_perm = lh;
  482. }
  483. answer = NULL;
  484. }
  485. if (answer)
  486. goto out_permanent;
  487. /* Add the new entry after the last permanent entry if any, so that
  488. * the new entry does not override a permanent entry when matched with
  489. * a wild-card protocol. But it is allowed to override any existing
  490. * non-permanent entry. This means that when we remove this entry, the
  491. * system automatically returns to the old behavior.
  492. */
  493. list_add_rcu(&p->list, last_perm);
  494. ret = 0;
  495. out:
  496. spin_unlock_bh(&inetsw6_lock);
  497. return ret;
  498. out_permanent:
  499. printk(KERN_ERR "Attempt to override permanent protocol %d.\n",
  500. protocol);
  501. goto out;
  502. out_illegal:
  503. printk(KERN_ERR
  504. "Ignoring attempt to register invalid socket type %d.\n",
  505. p->type);
  506. goto out;
  507. }
  508. EXPORT_SYMBOL(inet6_register_protosw);
  509. void
  510. inet6_unregister_protosw(struct inet_protosw *p)
  511. {
  512. if (INET_PROTOSW_PERMANENT & p->flags) {
  513. printk(KERN_ERR
  514. "Attempt to unregister permanent protocol %d.\n",
  515. p->protocol);
  516. } else {
  517. spin_lock_bh(&inetsw6_lock);
  518. list_del_rcu(&p->list);
  519. spin_unlock_bh(&inetsw6_lock);
  520. synchronize_net();
  521. }
  522. }
  523. EXPORT_SYMBOL(inet6_unregister_protosw);
  524. int inet6_sk_rebuild_header(struct sock *sk)
  525. {
  526. int err;
  527. struct dst_entry *dst;
  528. struct ipv6_pinfo *np = inet6_sk(sk);
  529. dst = __sk_dst_check(sk, np->dst_cookie);
  530. if (dst == NULL) {
  531. struct inet_sock *inet = inet_sk(sk);
  532. struct in6_addr *final_p = NULL, final;
  533. struct flowi fl;
  534. memset(&fl, 0, sizeof(fl));
  535. fl.proto = sk->sk_protocol;
  536. ipv6_addr_copy(&fl.fl6_dst, &np->daddr);
  537. ipv6_addr_copy(&fl.fl6_src, &np->saddr);
  538. fl.fl6_flowlabel = np->flow_label;
  539. fl.oif = sk->sk_bound_dev_if;
  540. fl.fl_ip_dport = inet->dport;
  541. fl.fl_ip_sport = inet->sport;
  542. security_sk_classify_flow(sk, &fl);
  543. if (np->opt && np->opt->srcrt) {
  544. struct rt0_hdr *rt0 = (struct rt0_hdr *) np->opt->srcrt;
  545. ipv6_addr_copy(&final, &fl.fl6_dst);
  546. ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
  547. final_p = &final;
  548. }
  549. err = ip6_dst_lookup(sk, &dst, &fl);
  550. if (err) {
  551. sk->sk_route_caps = 0;
  552. return err;
  553. }
  554. if (final_p)
  555. ipv6_addr_copy(&fl.fl6_dst, final_p);
  556. if ((err = xfrm_lookup(sock_net(sk), &dst, &fl, sk, 0)) < 0) {
  557. sk->sk_err_soft = -err;
  558. return err;
  559. }
  560. __ip6_dst_store(sk, dst, NULL, NULL);
  561. }
  562. return 0;
  563. }
  564. EXPORT_SYMBOL_GPL(inet6_sk_rebuild_header);
  565. int ipv6_opt_accepted(struct sock *sk, struct sk_buff *skb)
  566. {
  567. struct ipv6_pinfo *np = inet6_sk(sk);
  568. struct inet6_skb_parm *opt = IP6CB(skb);
  569. if (np->rxopt.all) {
  570. if ((opt->hop && (np->rxopt.bits.hopopts ||
  571. np->rxopt.bits.ohopopts)) ||
  572. ((IPV6_FLOWINFO_MASK &
  573. *(__be32 *)skb_network_header(skb)) &&
  574. np->rxopt.bits.rxflow) ||
  575. (opt->srcrt && (np->rxopt.bits.srcrt ||
  576. np->rxopt.bits.osrcrt)) ||
  577. ((opt->dst1 || opt->dst0) &&
  578. (np->rxopt.bits.dstopts || np->rxopt.bits.odstopts)))
  579. return 1;
  580. }
  581. return 0;
  582. }
  583. EXPORT_SYMBOL_GPL(ipv6_opt_accepted);
  584. static int ipv6_gso_pull_exthdrs(struct sk_buff *skb, int proto)
  585. {
  586. struct inet6_protocol *ops = NULL;
  587. for (;;) {
  588. struct ipv6_opt_hdr *opth;
  589. int len;
  590. if (proto != NEXTHDR_HOP) {
  591. ops = rcu_dereference(inet6_protos[proto]);
  592. if (unlikely(!ops))
  593. break;
  594. if (!(ops->flags & INET6_PROTO_GSO_EXTHDR))
  595. break;
  596. }
  597. if (unlikely(!pskb_may_pull(skb, 8)))
  598. break;
  599. opth = (void *)skb->data;
  600. len = ipv6_optlen(opth);
  601. if (unlikely(!pskb_may_pull(skb, len)))
  602. break;
  603. proto = opth->nexthdr;
  604. __skb_pull(skb, len);
  605. }
  606. return proto;
  607. }
  608. static int ipv6_gso_send_check(struct sk_buff *skb)
  609. {
  610. struct ipv6hdr *ipv6h;
  611. struct inet6_protocol *ops;
  612. int err = -EINVAL;
  613. if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h))))
  614. goto out;
  615. ipv6h = ipv6_hdr(skb);
  616. __skb_pull(skb, sizeof(*ipv6h));
  617. err = -EPROTONOSUPPORT;
  618. rcu_read_lock();
  619. ops = rcu_dereference(inet6_protos[
  620. ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr)]);
  621. if (likely(ops && ops->gso_send_check)) {
  622. skb_reset_transport_header(skb);
  623. err = ops->gso_send_check(skb);
  624. }
  625. rcu_read_unlock();
  626. out:
  627. return err;
  628. }
  629. static struct sk_buff *ipv6_gso_segment(struct sk_buff *skb, int features)
  630. {
  631. struct sk_buff *segs = ERR_PTR(-EINVAL);
  632. struct ipv6hdr *ipv6h;
  633. struct inet6_protocol *ops;
  634. if (!(features & NETIF_F_V6_CSUM))
  635. features &= ~NETIF_F_SG;
  636. if (unlikely(skb_shinfo(skb)->gso_type &
  637. ~(SKB_GSO_UDP |
  638. SKB_GSO_DODGY |
  639. SKB_GSO_TCP_ECN |
  640. SKB_GSO_TCPV6 |
  641. 0)))
  642. goto out;
  643. if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h))))
  644. goto out;
  645. ipv6h = ipv6_hdr(skb);
  646. __skb_pull(skb, sizeof(*ipv6h));
  647. segs = ERR_PTR(-EPROTONOSUPPORT);
  648. rcu_read_lock();
  649. ops = rcu_dereference(inet6_protos[
  650. ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr)]);
  651. if (likely(ops && ops->gso_segment)) {
  652. skb_reset_transport_header(skb);
  653. segs = ops->gso_segment(skb, features);
  654. }
  655. rcu_read_unlock();
  656. if (unlikely(IS_ERR(segs)))
  657. goto out;
  658. for (skb = segs; skb; skb = skb->next) {
  659. ipv6h = ipv6_hdr(skb);
  660. ipv6h->payload_len = htons(skb->len - skb->mac_len -
  661. sizeof(*ipv6h));
  662. }
  663. out:
  664. return segs;
  665. }
  666. struct ipv6_gro_cb {
  667. struct napi_gro_cb napi;
  668. int proto;
  669. };
  670. #define IPV6_GRO_CB(skb) ((struct ipv6_gro_cb *)(skb)->cb)
  671. static struct sk_buff **ipv6_gro_receive(struct sk_buff **head,
  672. struct sk_buff *skb)
  673. {
  674. struct inet6_protocol *ops;
  675. struct sk_buff **pp = NULL;
  676. struct sk_buff *p;
  677. struct ipv6hdr *iph;
  678. unsigned int nlen;
  679. int flush = 1;
  680. int proto;
  681. __wsum csum;
  682. iph = skb_gro_header(skb, sizeof(*iph));
  683. if (unlikely(!iph))
  684. goto out;
  685. skb_gro_pull(skb, sizeof(*iph));
  686. skb_set_transport_header(skb, skb_gro_offset(skb));
  687. flush += ntohs(iph->payload_len) != skb_gro_len(skb);
  688. rcu_read_lock();
  689. proto = iph->nexthdr;
  690. ops = rcu_dereference(inet6_protos[proto]);
  691. if (!ops || !ops->gro_receive) {
  692. __pskb_pull(skb, skb_gro_offset(skb));
  693. proto = ipv6_gso_pull_exthdrs(skb, proto);
  694. skb_gro_pull(skb, -skb_transport_offset(skb));
  695. skb_reset_transport_header(skb);
  696. __skb_push(skb, skb_gro_offset(skb));
  697. if (!ops || !ops->gro_receive)
  698. goto out_unlock;
  699. iph = ipv6_hdr(skb);
  700. }
  701. IPV6_GRO_CB(skb)->proto = proto;
  702. flush--;
  703. nlen = skb_network_header_len(skb);
  704. for (p = *head; p; p = p->next) {
  705. struct ipv6hdr *iph2;
  706. if (!NAPI_GRO_CB(p)->same_flow)
  707. continue;
  708. iph2 = ipv6_hdr(p);
  709. /* All fields must match except length. */
  710. if (nlen != skb_network_header_len(p) ||
  711. memcmp(iph, iph2, offsetof(struct ipv6hdr, payload_len)) ||
  712. memcmp(&iph->nexthdr, &iph2->nexthdr,
  713. nlen - offsetof(struct ipv6hdr, nexthdr))) {
  714. NAPI_GRO_CB(p)->same_flow = 0;
  715. continue;
  716. }
  717. NAPI_GRO_CB(p)->flush |= flush;
  718. }
  719. NAPI_GRO_CB(skb)->flush |= flush;
  720. csum = skb->csum;
  721. skb_postpull_rcsum(skb, iph, skb_network_header_len(skb));
  722. pp = ops->gro_receive(head, skb);
  723. skb->csum = csum;
  724. out_unlock:
  725. rcu_read_unlock();
  726. out:
  727. NAPI_GRO_CB(skb)->flush |= flush;
  728. return pp;
  729. }
  730. static int ipv6_gro_complete(struct sk_buff *skb)
  731. {
  732. struct inet6_protocol *ops;
  733. struct ipv6hdr *iph = ipv6_hdr(skb);
  734. int err = -ENOSYS;
  735. iph->payload_len = htons(skb->len - skb_network_offset(skb) -
  736. sizeof(*iph));
  737. rcu_read_lock();
  738. ops = rcu_dereference(inet6_protos[IPV6_GRO_CB(skb)->proto]);
  739. if (WARN_ON(!ops || !ops->gro_complete))
  740. goto out_unlock;
  741. err = ops->gro_complete(skb);
  742. out_unlock:
  743. rcu_read_unlock();
  744. return err;
  745. }
  746. static struct packet_type ipv6_packet_type __read_mostly = {
  747. .type = cpu_to_be16(ETH_P_IPV6),
  748. .func = ipv6_rcv,
  749. .gso_send_check = ipv6_gso_send_check,
  750. .gso_segment = ipv6_gso_segment,
  751. .gro_receive = ipv6_gro_receive,
  752. .gro_complete = ipv6_gro_complete,
  753. };
  754. static int __init ipv6_packet_init(void)
  755. {
  756. dev_add_pack(&ipv6_packet_type);
  757. return 0;
  758. }
  759. static void ipv6_packet_cleanup(void)
  760. {
  761. dev_remove_pack(&ipv6_packet_type);
  762. }
  763. static int __net_init ipv6_init_mibs(struct net *net)
  764. {
  765. if (snmp_mib_init((void **)net->mib.udp_stats_in6,
  766. sizeof (struct udp_mib)) < 0)
  767. return -ENOMEM;
  768. if (snmp_mib_init((void **)net->mib.udplite_stats_in6,
  769. sizeof (struct udp_mib)) < 0)
  770. goto err_udplite_mib;
  771. if (snmp_mib_init((void **)net->mib.ipv6_statistics,
  772. sizeof(struct ipstats_mib)) < 0)
  773. goto err_ip_mib;
  774. if (snmp_mib_init((void **)net->mib.icmpv6_statistics,
  775. sizeof(struct icmpv6_mib)) < 0)
  776. goto err_icmp_mib;
  777. if (snmp_mib_init((void **)net->mib.icmpv6msg_statistics,
  778. sizeof(struct icmpv6msg_mib)) < 0)
  779. goto err_icmpmsg_mib;
  780. return 0;
  781. err_icmpmsg_mib:
  782. snmp_mib_free((void **)net->mib.icmpv6_statistics);
  783. err_icmp_mib:
  784. snmp_mib_free((void **)net->mib.ipv6_statistics);
  785. err_ip_mib:
  786. snmp_mib_free((void **)net->mib.udplite_stats_in6);
  787. err_udplite_mib:
  788. snmp_mib_free((void **)net->mib.udp_stats_in6);
  789. return -ENOMEM;
  790. }
  791. static void __net_exit ipv6_cleanup_mibs(struct net *net)
  792. {
  793. snmp_mib_free((void **)net->mib.udp_stats_in6);
  794. snmp_mib_free((void **)net->mib.udplite_stats_in6);
  795. snmp_mib_free((void **)net->mib.ipv6_statistics);
  796. snmp_mib_free((void **)net->mib.icmpv6_statistics);
  797. snmp_mib_free((void **)net->mib.icmpv6msg_statistics);
  798. }
  799. static int __net_init inet6_net_init(struct net *net)
  800. {
  801. int err = 0;
  802. net->ipv6.sysctl.bindv6only = 0;
  803. net->ipv6.sysctl.icmpv6_time = 1*HZ;
  804. err = ipv6_init_mibs(net);
  805. if (err)
  806. return err;
  807. #ifdef CONFIG_PROC_FS
  808. err = udp6_proc_init(net);
  809. if (err)
  810. goto out;
  811. err = tcp6_proc_init(net);
  812. if (err)
  813. goto proc_tcp6_fail;
  814. err = ac6_proc_init(net);
  815. if (err)
  816. goto proc_ac6_fail;
  817. #endif
  818. return err;
  819. #ifdef CONFIG_PROC_FS
  820. proc_ac6_fail:
  821. tcp6_proc_exit(net);
  822. proc_tcp6_fail:
  823. udp6_proc_exit(net);
  824. out:
  825. ipv6_cleanup_mibs(net);
  826. return err;
  827. #endif
  828. }
  829. static void inet6_net_exit(struct net *net)
  830. {
  831. #ifdef CONFIG_PROC_FS
  832. udp6_proc_exit(net);
  833. tcp6_proc_exit(net);
  834. ac6_proc_exit(net);
  835. #endif
  836. ipv6_cleanup_mibs(net);
  837. }
  838. static struct pernet_operations inet6_net_ops = {
  839. .init = inet6_net_init,
  840. .exit = inet6_net_exit,
  841. };
  842. static int __init inet6_init(void)
  843. {
  844. struct sk_buff *dummy_skb;
  845. struct list_head *r;
  846. int err = 0;
  847. BUILD_BUG_ON(sizeof(struct inet6_skb_parm) > sizeof(dummy_skb->cb));
  848. /* Register the socket-side information for inet6_create. */
  849. for(r = &inetsw6[0]; r < &inetsw6[SOCK_MAX]; ++r)
  850. INIT_LIST_HEAD(r);
  851. if (disable_ipv6) {
  852. printk(KERN_INFO
  853. "IPv6: Loaded, but administratively disabled, "
  854. "reboot required to enable\n");
  855. goto out;
  856. }
  857. err = proto_register(&tcpv6_prot, 1);
  858. if (err)
  859. goto out;
  860. err = proto_register(&udpv6_prot, 1);
  861. if (err)
  862. goto out_unregister_tcp_proto;
  863. err = proto_register(&udplitev6_prot, 1);
  864. if (err)
  865. goto out_unregister_udp_proto;
  866. err = proto_register(&rawv6_prot, 1);
  867. if (err)
  868. goto out_unregister_udplite_proto;
  869. /* We MUST register RAW sockets before we create the ICMP6,
  870. * IGMP6, or NDISC control sockets.
  871. */
  872. err = rawv6_init();
  873. if (err)
  874. goto out_unregister_raw_proto;
  875. /* Register the family here so that the init calls below will
  876. * be able to create sockets. (?? is this dangerous ??)
  877. */
  878. err = sock_register(&inet6_family_ops);
  879. if (err)
  880. goto out_sock_register_fail;
  881. #ifdef CONFIG_SYSCTL
  882. err = ipv6_static_sysctl_register();
  883. if (err)
  884. goto static_sysctl_fail;
  885. #endif
  886. /*
  887. * ipngwg API draft makes clear that the correct semantics
  888. * for TCP and UDP is to consider one TCP and UDP instance
  889. * in a host availiable by both INET and INET6 APIs and
  890. * able to communicate via both network protocols.
  891. */
  892. err = register_pernet_subsys(&inet6_net_ops);
  893. if (err)
  894. goto register_pernet_fail;
  895. err = icmpv6_init();
  896. if (err)
  897. goto icmp_fail;
  898. err = ip6_mr_init();
  899. if (err)
  900. goto ipmr_fail;
  901. err = ndisc_init();
  902. if (err)
  903. goto ndisc_fail;
  904. err = igmp6_init();
  905. if (err)
  906. goto igmp_fail;
  907. err = ipv6_netfilter_init();
  908. if (err)
  909. goto netfilter_fail;
  910. /* Create /proc/foo6 entries. */
  911. #ifdef CONFIG_PROC_FS
  912. err = -ENOMEM;
  913. if (raw6_proc_init())
  914. goto proc_raw6_fail;
  915. if (udplite6_proc_init())
  916. goto proc_udplite6_fail;
  917. if (ipv6_misc_proc_init())
  918. goto proc_misc6_fail;
  919. if (if6_proc_init())
  920. goto proc_if6_fail;
  921. #endif
  922. err = ip6_route_init();
  923. if (err)
  924. goto ip6_route_fail;
  925. err = ip6_flowlabel_init();
  926. if (err)
  927. goto ip6_flowlabel_fail;
  928. err = addrconf_init();
  929. if (err)
  930. goto addrconf_fail;
  931. /* Init v6 extension headers. */
  932. err = ipv6_exthdrs_init();
  933. if (err)
  934. goto ipv6_exthdrs_fail;
  935. err = ipv6_frag_init();
  936. if (err)
  937. goto ipv6_frag_fail;
  938. /* Init v6 transport protocols. */
  939. err = udpv6_init();
  940. if (err)
  941. goto udpv6_fail;
  942. err = udplitev6_init();
  943. if (err)
  944. goto udplitev6_fail;
  945. err = tcpv6_init();
  946. if (err)
  947. goto tcpv6_fail;
  948. err = ipv6_packet_init();
  949. if (err)
  950. goto ipv6_packet_fail;
  951. #ifdef CONFIG_SYSCTL
  952. err = ipv6_sysctl_register();
  953. if (err)
  954. goto sysctl_fail;
  955. #endif
  956. out:
  957. return err;
  958. #ifdef CONFIG_SYSCTL
  959. sysctl_fail:
  960. ipv6_packet_cleanup();
  961. #endif
  962. ipv6_packet_fail:
  963. tcpv6_exit();
  964. tcpv6_fail:
  965. udplitev6_exit();
  966. udplitev6_fail:
  967. udpv6_exit();
  968. udpv6_fail:
  969. ipv6_frag_exit();
  970. ipv6_frag_fail:
  971. ipv6_exthdrs_exit();
  972. ipv6_exthdrs_fail:
  973. addrconf_cleanup();
  974. addrconf_fail:
  975. ip6_flowlabel_cleanup();
  976. ip6_flowlabel_fail:
  977. ip6_route_cleanup();
  978. ip6_route_fail:
  979. #ifdef CONFIG_PROC_FS
  980. if6_proc_exit();
  981. proc_if6_fail:
  982. ipv6_misc_proc_exit();
  983. proc_misc6_fail:
  984. udplite6_proc_exit();
  985. proc_udplite6_fail:
  986. raw6_proc_exit();
  987. proc_raw6_fail:
  988. #endif
  989. ipv6_netfilter_fini();
  990. netfilter_fail:
  991. igmp6_cleanup();
  992. igmp_fail:
  993. ndisc_cleanup();
  994. ndisc_fail:
  995. ip6_mr_cleanup();
  996. ipmr_fail:
  997. icmpv6_cleanup();
  998. icmp_fail:
  999. unregister_pernet_subsys(&inet6_net_ops);
  1000. register_pernet_fail:
  1001. #ifdef CONFIG_SYSCTL
  1002. ipv6_static_sysctl_unregister();
  1003. static_sysctl_fail:
  1004. #endif
  1005. sock_unregister(PF_INET6);
  1006. rtnl_unregister_all(PF_INET6);
  1007. out_sock_register_fail:
  1008. rawv6_exit();
  1009. out_unregister_raw_proto:
  1010. proto_unregister(&rawv6_prot);
  1011. out_unregister_udplite_proto:
  1012. proto_unregister(&udplitev6_prot);
  1013. out_unregister_udp_proto:
  1014. proto_unregister(&udpv6_prot);
  1015. out_unregister_tcp_proto:
  1016. proto_unregister(&tcpv6_prot);
  1017. goto out;
  1018. }
  1019. module_init(inet6_init);
  1020. static void __exit inet6_exit(void)
  1021. {
  1022. if (disable_ipv6)
  1023. return;
  1024. /* First of all disallow new sockets creation. */
  1025. sock_unregister(PF_INET6);
  1026. /* Disallow any further netlink messages */
  1027. rtnl_unregister_all(PF_INET6);
  1028. #ifdef CONFIG_SYSCTL
  1029. ipv6_sysctl_unregister();
  1030. #endif
  1031. udpv6_exit();
  1032. udplitev6_exit();
  1033. tcpv6_exit();
  1034. /* Cleanup code parts. */
  1035. ipv6_packet_cleanup();
  1036. ipv6_frag_exit();
  1037. ipv6_exthdrs_exit();
  1038. addrconf_cleanup();
  1039. ip6_flowlabel_cleanup();
  1040. ip6_route_cleanup();
  1041. #ifdef CONFIG_PROC_FS
  1042. /* Cleanup code parts. */
  1043. if6_proc_exit();
  1044. ipv6_misc_proc_exit();
  1045. udplite6_proc_exit();
  1046. raw6_proc_exit();
  1047. #endif
  1048. ipv6_netfilter_fini();
  1049. igmp6_cleanup();
  1050. ndisc_cleanup();
  1051. ip6_mr_cleanup();
  1052. icmpv6_cleanup();
  1053. rawv6_exit();
  1054. unregister_pernet_subsys(&inet6_net_ops);
  1055. #ifdef CONFIG_SYSCTL
  1056. ipv6_static_sysctl_unregister();
  1057. #endif
  1058. proto_unregister(&rawv6_prot);
  1059. proto_unregister(&udplitev6_prot);
  1060. proto_unregister(&udpv6_prot);
  1061. proto_unregister(&tcpv6_prot);
  1062. }
  1063. module_exit(inet6_exit);
  1064. MODULE_ALIAS_NETPROTO(PF_INET6);