af_inet6.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945
  1. /*
  2. * PF_INET6 socket protocol family
  3. * Linux INET6 implementation
  4. *
  5. * Authors:
  6. * Pedro Roque <roque@di.fc.ul.pt>
  7. *
  8. * Adapted from linux/net/ipv4/af_inet.c
  9. *
  10. * $Id: af_inet6.c,v 1.66 2002/02/01 22:01:04 davem Exp $
  11. *
  12. * Fixes:
  13. * piggy, Karl Knutson : Socket protocol table
  14. * Hideaki YOSHIFUJI : sin6_scope_id support
  15. * Arnaldo Melo : check proc_net_create return, cleanups
  16. *
  17. * This program is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU General Public License
  19. * as published by the Free Software Foundation; either version
  20. * 2 of the License, or (at your option) any later version.
  21. */
  22. #include <linux/module.h>
  23. #include <linux/capability.h>
  24. #include <linux/config.h>
  25. #include <linux/errno.h>
  26. #include <linux/types.h>
  27. #include <linux/socket.h>
  28. #include <linux/in.h>
  29. #include <linux/kernel.h>
  30. #include <linux/sched.h>
  31. #include <linux/timer.h>
  32. #include <linux/string.h>
  33. #include <linux/sockios.h>
  34. #include <linux/net.h>
  35. #include <linux/fcntl.h>
  36. #include <linux/mm.h>
  37. #include <linux/interrupt.h>
  38. #include <linux/proc_fs.h>
  39. #include <linux/stat.h>
  40. #include <linux/init.h>
  41. #include <linux/inet.h>
  42. #include <linux/netdevice.h>
  43. #include <linux/icmpv6.h>
  44. #include <linux/smp_lock.h>
  45. #include <linux/netfilter_ipv6.h>
  46. #include <net/ip.h>
  47. #include <net/ipv6.h>
  48. #include <net/udp.h>
  49. #include <net/tcp.h>
  50. #include <net/ipip.h>
  51. #include <net/protocol.h>
  52. #include <net/inet_common.h>
  53. #include <net/transp_v6.h>
  54. #include <net/ip6_route.h>
  55. #include <net/addrconf.h>
  56. #ifdef CONFIG_IPV6_TUNNEL
  57. #include <net/ip6_tunnel.h>
  58. #endif
  59. #include <asm/uaccess.h>
  60. #include <asm/system.h>
  61. MODULE_AUTHOR("Cast of dozens");
  62. MODULE_DESCRIPTION("IPv6 protocol stack for Linux");
  63. MODULE_LICENSE("GPL");
  64. int sysctl_ipv6_bindv6only;
  65. /* The inetsw table contains everything that inet_create needs to
  66. * build a new socket.
  67. */
  68. static struct list_head inetsw6[SOCK_MAX];
  69. static DEFINE_SPINLOCK(inetsw6_lock);
  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 socket *sock, int protocol)
  76. {
  77. struct inet_sock *inet;
  78. struct ipv6_pinfo *np;
  79. struct sock *sk;
  80. struct list_head *p;
  81. struct inet_protosw *answer;
  82. struct proto *answer_prot;
  83. unsigned char answer_flags;
  84. char answer_no_check;
  85. int try_loading_module = 0;
  86. int err;
  87. /* Look for the requested type/protocol pair. */
  88. answer = NULL;
  89. lookup_protocol:
  90. err = -ESOCKTNOSUPPORT;
  91. rcu_read_lock();
  92. list_for_each_rcu(p, &inetsw6[sock->type]) {
  93. answer = list_entry(p, struct inet_protosw, list);
  94. /* Check the non-wild match. */
  95. if (protocol == answer->protocol) {
  96. if (protocol != IPPROTO_IP)
  97. break;
  98. } else {
  99. /* Check for the two wild cases. */
  100. if (IPPROTO_IP == protocol) {
  101. protocol = answer->protocol;
  102. break;
  103. }
  104. if (IPPROTO_IP == answer->protocol)
  105. break;
  106. }
  107. err = -EPROTONOSUPPORT;
  108. answer = NULL;
  109. }
  110. if (!answer) {
  111. if (try_loading_module < 2) {
  112. rcu_read_unlock();
  113. /*
  114. * Be more specific, e.g. net-pf-10-proto-132-type-1
  115. * (net-pf-PF_INET6-proto-IPPROTO_SCTP-type-SOCK_STREAM)
  116. */
  117. if (++try_loading_module == 1)
  118. request_module("net-pf-%d-proto-%d-type-%d",
  119. PF_INET6, protocol, sock->type);
  120. /*
  121. * Fall back to generic, e.g. net-pf-10-proto-132
  122. * (net-pf-PF_INET6-proto-IPPROTO_SCTP)
  123. */
  124. else
  125. request_module("net-pf-%d-proto-%d",
  126. PF_INET6, protocol);
  127. goto lookup_protocol;
  128. } else
  129. goto out_rcu_unlock;
  130. }
  131. err = -EPERM;
  132. if (answer->capability > 0 && !capable(answer->capability))
  133. goto out_rcu_unlock;
  134. sock->ops = answer->ops;
  135. answer_prot = answer->prot;
  136. answer_no_check = answer->no_check;
  137. answer_flags = answer->flags;
  138. rcu_read_unlock();
  139. BUG_TRAP(answer_prot->slab != NULL);
  140. err = -ENOBUFS;
  141. sk = sk_alloc(PF_INET6, GFP_KERNEL, answer_prot, 1);
  142. if (sk == NULL)
  143. goto out;
  144. sock_init_data(sock, sk);
  145. err = 0;
  146. sk->sk_no_check = answer_no_check;
  147. if (INET_PROTOSW_REUSE & answer_flags)
  148. sk->sk_reuse = 1;
  149. inet = inet_sk(sk);
  150. inet->is_icsk = INET_PROTOSW_ICSK & answer_flags;
  151. if (SOCK_RAW == sock->type) {
  152. inet->num = protocol;
  153. if (IPPROTO_RAW == protocol)
  154. inet->hdrincl = 1;
  155. }
  156. sk->sk_destruct = inet_sock_destruct;
  157. sk->sk_family = PF_INET6;
  158. sk->sk_protocol = protocol;
  159. sk->sk_backlog_rcv = answer->prot->backlog_rcv;
  160. inet_sk(sk)->pinet6 = np = inet6_sk_generic(sk);
  161. np->hop_limit = -1;
  162. np->mcast_hops = -1;
  163. np->mc_loop = 1;
  164. np->pmtudisc = IPV6_PMTUDISC_WANT;
  165. np->ipv6only = sysctl_ipv6_bindv6only;
  166. /* Init the ipv4 part of the socket since we can have sockets
  167. * using v6 API for ipv4.
  168. */
  169. inet->uc_ttl = -1;
  170. inet->mc_loop = 1;
  171. inet->mc_ttl = 1;
  172. inet->mc_index = 0;
  173. inet->mc_list = NULL;
  174. if (ipv4_config.no_pmtu_disc)
  175. inet->pmtudisc = IP_PMTUDISC_DONT;
  176. else
  177. inet->pmtudisc = IP_PMTUDISC_WANT;
  178. /*
  179. * Increment only the relevant sk_prot->socks debug field, this changes
  180. * the previous behaviour of incrementing both the equivalent to
  181. * answer->prot->socks (inet6_sock_nr) and inet_sock_nr.
  182. *
  183. * This allows better debug granularity as we'll know exactly how many
  184. * UDPv6, TCPv6, etc socks were allocated, not the sum of all IPv6
  185. * transport protocol socks. -acme
  186. */
  187. sk_refcnt_debug_inc(sk);
  188. if (inet->num) {
  189. /* It assumes that any protocol which allows
  190. * the user to assign a number at socket
  191. * creation time automatically shares.
  192. */
  193. inet->sport = ntohs(inet->num);
  194. sk->sk_prot->hash(sk);
  195. }
  196. if (sk->sk_prot->init) {
  197. err = sk->sk_prot->init(sk);
  198. if (err) {
  199. sk_common_release(sk);
  200. goto out;
  201. }
  202. }
  203. out:
  204. return err;
  205. out_rcu_unlock:
  206. rcu_read_unlock();
  207. goto out;
  208. }
  209. /* bind for INET6 API */
  210. int inet6_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
  211. {
  212. struct sockaddr_in6 *addr=(struct sockaddr_in6 *)uaddr;
  213. struct sock *sk = sock->sk;
  214. struct inet_sock *inet = inet_sk(sk);
  215. struct ipv6_pinfo *np = inet6_sk(sk);
  216. __u32 v4addr = 0;
  217. unsigned short snum;
  218. int addr_type = 0;
  219. int err = 0;
  220. /* If the socket has its own bind function then use it. */
  221. if (sk->sk_prot->bind)
  222. return sk->sk_prot->bind(sk, uaddr, addr_len);
  223. if (addr_len < SIN6_LEN_RFC2133)
  224. return -EINVAL;
  225. addr_type = ipv6_addr_type(&addr->sin6_addr);
  226. if ((addr_type & IPV6_ADDR_MULTICAST) && sock->type == SOCK_STREAM)
  227. return -EINVAL;
  228. snum = ntohs(addr->sin6_port);
  229. if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
  230. return -EACCES;
  231. lock_sock(sk);
  232. /* Check these errors (active socket, double bind). */
  233. if (sk->sk_state != TCP_CLOSE || inet->num) {
  234. err = -EINVAL;
  235. goto out;
  236. }
  237. /* Check if the address belongs to the host. */
  238. if (addr_type == IPV6_ADDR_MAPPED) {
  239. v4addr = addr->sin6_addr.s6_addr32[3];
  240. if (inet_addr_type(v4addr) != RTN_LOCAL) {
  241. err = -EADDRNOTAVAIL;
  242. goto out;
  243. }
  244. } else {
  245. if (addr_type != IPV6_ADDR_ANY) {
  246. struct net_device *dev = NULL;
  247. if (addr_type & IPV6_ADDR_LINKLOCAL) {
  248. if (addr_len >= sizeof(struct sockaddr_in6) &&
  249. addr->sin6_scope_id) {
  250. /* Override any existing binding, if another one
  251. * is supplied by user.
  252. */
  253. sk->sk_bound_dev_if = addr->sin6_scope_id;
  254. }
  255. /* Binding to link-local address requires an interface */
  256. if (!sk->sk_bound_dev_if) {
  257. err = -EINVAL;
  258. goto out;
  259. }
  260. dev = dev_get_by_index(sk->sk_bound_dev_if);
  261. if (!dev) {
  262. err = -ENODEV;
  263. goto out;
  264. }
  265. }
  266. /* ipv4 addr of the socket is invalid. Only the
  267. * unspecified and mapped address have a v4 equivalent.
  268. */
  269. v4addr = LOOPBACK4_IPV6;
  270. if (!(addr_type & IPV6_ADDR_MULTICAST)) {
  271. if (!ipv6_chk_addr(&addr->sin6_addr, 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 = ntohs(inet->num);
  298. inet->dport = 0;
  299. inet->daddr = 0;
  300. out:
  301. release_sock(sk);
  302. return err;
  303. }
  304. int inet6_release(struct socket *sock)
  305. {
  306. struct sock *sk = sock->sk;
  307. if (sk == NULL)
  308. return -EINVAL;
  309. /* Free mc lists */
  310. ipv6_sock_mc_close(sk);
  311. /* Free ac lists */
  312. ipv6_sock_ac_close(sk);
  313. return inet_release(sock);
  314. }
  315. int inet6_destroy_sock(struct sock *sk)
  316. {
  317. struct ipv6_pinfo *np = inet6_sk(sk);
  318. struct sk_buff *skb;
  319. struct ipv6_txoptions *opt;
  320. /* Release rx options */
  321. if ((skb = xchg(&np->pktoptions, NULL)) != NULL)
  322. kfree_skb(skb);
  323. /* Free flowlabels */
  324. fl6_free_socklist(sk);
  325. /* Free tx options */
  326. if ((opt = xchg(&np->opt, NULL)) != NULL)
  327. sock_kfree_s(sk, opt, opt->tot_len);
  328. return 0;
  329. }
  330. EXPORT_SYMBOL_GPL(inet6_destroy_sock);
  331. /*
  332. * This does both peername and sockname.
  333. */
  334. int inet6_getname(struct socket *sock, struct sockaddr *uaddr,
  335. int *uaddr_len, int peer)
  336. {
  337. struct sockaddr_in6 *sin=(struct sockaddr_in6 *)uaddr;
  338. struct sock *sk = sock->sk;
  339. struct inet_sock *inet = inet_sk(sk);
  340. struct ipv6_pinfo *np = inet6_sk(sk);
  341. sin->sin6_family = AF_INET6;
  342. sin->sin6_flowinfo = 0;
  343. sin->sin6_scope_id = 0;
  344. if (peer) {
  345. if (!inet->dport)
  346. return -ENOTCONN;
  347. if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
  348. peer == 1)
  349. return -ENOTCONN;
  350. sin->sin6_port = inet->dport;
  351. ipv6_addr_copy(&sin->sin6_addr, &np->daddr);
  352. if (np->sndflow)
  353. sin->sin6_flowinfo = np->flow_label;
  354. } else {
  355. if (ipv6_addr_any(&np->rcv_saddr))
  356. ipv6_addr_copy(&sin->sin6_addr, &np->saddr);
  357. else
  358. ipv6_addr_copy(&sin->sin6_addr, &np->rcv_saddr);
  359. sin->sin6_port = inet->sport;
  360. }
  361. if (ipv6_addr_type(&sin->sin6_addr) & IPV6_ADDR_LINKLOCAL)
  362. sin->sin6_scope_id = sk->sk_bound_dev_if;
  363. *uaddr_len = sizeof(*sin);
  364. return(0);
  365. }
  366. int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  367. {
  368. struct sock *sk = sock->sk;
  369. switch(cmd)
  370. {
  371. case SIOCGSTAMP:
  372. return sock_get_timestamp(sk, (struct timeval __user *)arg);
  373. case SIOCADDRT:
  374. case SIOCDELRT:
  375. return(ipv6_route_ioctl(cmd,(void __user *)arg));
  376. case SIOCSIFADDR:
  377. return addrconf_add_ifaddr((void __user *) arg);
  378. case SIOCDIFADDR:
  379. return addrconf_del_ifaddr((void __user *) arg);
  380. case SIOCSIFDSTADDR:
  381. return addrconf_set_dstaddr((void __user *) arg);
  382. default:
  383. if (!sk->sk_prot->ioctl)
  384. return -ENOIOCTLCMD;
  385. return sk->sk_prot->ioctl(sk, cmd, arg);
  386. }
  387. /*NOTREACHED*/
  388. return(0);
  389. }
  390. const struct proto_ops inet6_stream_ops = {
  391. .family = PF_INET6,
  392. .owner = THIS_MODULE,
  393. .release = inet6_release,
  394. .bind = inet6_bind,
  395. .connect = inet_stream_connect, /* ok */
  396. .socketpair = sock_no_socketpair, /* a do nothing */
  397. .accept = inet_accept, /* ok */
  398. .getname = inet6_getname,
  399. .poll = tcp_poll, /* ok */
  400. .ioctl = inet6_ioctl, /* must change */
  401. .listen = inet_listen, /* ok */
  402. .shutdown = inet_shutdown, /* ok */
  403. .setsockopt = sock_common_setsockopt, /* ok */
  404. .getsockopt = sock_common_getsockopt, /* ok */
  405. .sendmsg = inet_sendmsg, /* ok */
  406. .recvmsg = sock_common_recvmsg, /* ok */
  407. .mmap = sock_no_mmap,
  408. .sendpage = tcp_sendpage,
  409. #ifdef CONFIG_COMPAT
  410. .compat_setsockopt = compat_sock_common_setsockopt,
  411. .compat_getsockopt = compat_sock_common_getsockopt,
  412. #endif
  413. };
  414. const struct proto_ops inet6_dgram_ops = {
  415. .family = PF_INET6,
  416. .owner = THIS_MODULE,
  417. .release = inet6_release,
  418. .bind = inet6_bind,
  419. .connect = inet_dgram_connect, /* ok */
  420. .socketpair = sock_no_socketpair, /* a do nothing */
  421. .accept = sock_no_accept, /* a do nothing */
  422. .getname = inet6_getname,
  423. .poll = udp_poll, /* ok */
  424. .ioctl = inet6_ioctl, /* must change */
  425. .listen = sock_no_listen, /* ok */
  426. .shutdown = inet_shutdown, /* ok */
  427. .setsockopt = sock_common_setsockopt, /* ok */
  428. .getsockopt = sock_common_getsockopt, /* ok */
  429. .sendmsg = inet_sendmsg, /* ok */
  430. .recvmsg = sock_common_recvmsg, /* ok */
  431. .mmap = sock_no_mmap,
  432. .sendpage = sock_no_sendpage,
  433. #ifdef CONFIG_COMPAT
  434. .compat_setsockopt = compat_sock_common_setsockopt,
  435. .compat_getsockopt = compat_sock_common_getsockopt,
  436. #endif
  437. };
  438. static struct net_proto_family inet6_family_ops = {
  439. .family = PF_INET6,
  440. .create = inet6_create,
  441. .owner = THIS_MODULE,
  442. };
  443. /* Same as inet6_dgram_ops, sans udp_poll. */
  444. static const struct proto_ops inet6_sockraw_ops = {
  445. .family = PF_INET6,
  446. .owner = THIS_MODULE,
  447. .release = inet6_release,
  448. .bind = inet6_bind,
  449. .connect = inet_dgram_connect, /* ok */
  450. .socketpair = sock_no_socketpair, /* a do nothing */
  451. .accept = sock_no_accept, /* a do nothing */
  452. .getname = inet6_getname,
  453. .poll = datagram_poll, /* ok */
  454. .ioctl = inet6_ioctl, /* must change */
  455. .listen = sock_no_listen, /* ok */
  456. .shutdown = inet_shutdown, /* ok */
  457. .setsockopt = sock_common_setsockopt, /* ok */
  458. .getsockopt = sock_common_getsockopt, /* ok */
  459. .sendmsg = inet_sendmsg, /* ok */
  460. .recvmsg = sock_common_recvmsg, /* ok */
  461. .mmap = sock_no_mmap,
  462. .sendpage = sock_no_sendpage,
  463. #ifdef CONFIG_COMPAT
  464. .compat_setsockopt = compat_sock_common_setsockopt,
  465. .compat_getsockopt = compat_sock_common_getsockopt,
  466. #endif
  467. };
  468. static struct inet_protosw rawv6_protosw = {
  469. .type = SOCK_RAW,
  470. .protocol = IPPROTO_IP, /* wild card */
  471. .prot = &rawv6_prot,
  472. .ops = &inet6_sockraw_ops,
  473. .capability = CAP_NET_RAW,
  474. .no_check = UDP_CSUM_DEFAULT,
  475. .flags = INET_PROTOSW_REUSE,
  476. };
  477. void
  478. inet6_register_protosw(struct inet_protosw *p)
  479. {
  480. struct list_head *lh;
  481. struct inet_protosw *answer;
  482. int protocol = p->protocol;
  483. struct list_head *last_perm;
  484. spin_lock_bh(&inetsw6_lock);
  485. if (p->type >= SOCK_MAX)
  486. goto out_illegal;
  487. /* If we are trying to override a permanent protocol, bail. */
  488. answer = NULL;
  489. last_perm = &inetsw6[p->type];
  490. list_for_each(lh, &inetsw6[p->type]) {
  491. answer = list_entry(lh, struct inet_protosw, list);
  492. /* Check only the non-wild match. */
  493. if (INET_PROTOSW_PERMANENT & answer->flags) {
  494. if (protocol == answer->protocol)
  495. break;
  496. last_perm = lh;
  497. }
  498. answer = NULL;
  499. }
  500. if (answer)
  501. goto out_permanent;
  502. /* Add the new entry after the last permanent entry if any, so that
  503. * the new entry does not override a permanent entry when matched with
  504. * a wild-card protocol. But it is allowed to override any existing
  505. * non-permanent entry. This means that when we remove this entry, the
  506. * system automatically returns to the old behavior.
  507. */
  508. list_add_rcu(&p->list, last_perm);
  509. out:
  510. spin_unlock_bh(&inetsw6_lock);
  511. return;
  512. out_permanent:
  513. printk(KERN_ERR "Attempt to override permanent protocol %d.\n",
  514. protocol);
  515. goto out;
  516. out_illegal:
  517. printk(KERN_ERR
  518. "Ignoring attempt to register invalid socket type %d.\n",
  519. p->type);
  520. goto out;
  521. }
  522. void
  523. inet6_unregister_protosw(struct inet_protosw *p)
  524. {
  525. if (INET_PROTOSW_PERMANENT & p->flags) {
  526. printk(KERN_ERR
  527. "Attempt to unregister permanent protocol %d.\n",
  528. p->protocol);
  529. } else {
  530. spin_lock_bh(&inetsw6_lock);
  531. list_del_rcu(&p->list);
  532. spin_unlock_bh(&inetsw6_lock);
  533. synchronize_net();
  534. }
  535. }
  536. int inet6_sk_rebuild_header(struct sock *sk)
  537. {
  538. int err;
  539. struct dst_entry *dst;
  540. struct ipv6_pinfo *np = inet6_sk(sk);
  541. dst = __sk_dst_check(sk, np->dst_cookie);
  542. if (dst == NULL) {
  543. struct inet_sock *inet = inet_sk(sk);
  544. struct in6_addr *final_p = NULL, final;
  545. struct flowi fl;
  546. memset(&fl, 0, sizeof(fl));
  547. fl.proto = sk->sk_protocol;
  548. ipv6_addr_copy(&fl.fl6_dst, &np->daddr);
  549. ipv6_addr_copy(&fl.fl6_src, &np->saddr);
  550. fl.fl6_flowlabel = np->flow_label;
  551. fl.oif = sk->sk_bound_dev_if;
  552. fl.fl_ip_dport = inet->dport;
  553. fl.fl_ip_sport = inet->sport;
  554. if (np->opt && np->opt->srcrt) {
  555. struct rt0_hdr *rt0 = (struct rt0_hdr *) np->opt->srcrt;
  556. ipv6_addr_copy(&final, &fl.fl6_dst);
  557. ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
  558. final_p = &final;
  559. }
  560. err = ip6_dst_lookup(sk, &dst, &fl);
  561. if (err) {
  562. sk->sk_route_caps = 0;
  563. return err;
  564. }
  565. if (final_p)
  566. ipv6_addr_copy(&fl.fl6_dst, final_p);
  567. if ((err = xfrm_lookup(&dst, &fl, sk, 0)) < 0) {
  568. sk->sk_err_soft = -err;
  569. return err;
  570. }
  571. ip6_dst_store(sk, dst, NULL);
  572. sk->sk_route_caps = dst->dev->features &
  573. ~(NETIF_F_IP_CSUM | NETIF_F_TSO);
  574. }
  575. return 0;
  576. }
  577. EXPORT_SYMBOL_GPL(inet6_sk_rebuild_header);
  578. int ipv6_opt_accepted(struct sock *sk, struct sk_buff *skb)
  579. {
  580. struct ipv6_pinfo *np = inet6_sk(sk);
  581. struct inet6_skb_parm *opt = IP6CB(skb);
  582. if (np->rxopt.all) {
  583. if ((opt->hop && (np->rxopt.bits.hopopts ||
  584. np->rxopt.bits.ohopopts)) ||
  585. ((IPV6_FLOWINFO_MASK & *(u32*)skb->nh.raw) &&
  586. np->rxopt.bits.rxflow) ||
  587. (opt->srcrt && (np->rxopt.bits.srcrt ||
  588. np->rxopt.bits.osrcrt)) ||
  589. ((opt->dst1 || opt->dst0) &&
  590. (np->rxopt.bits.dstopts || np->rxopt.bits.odstopts)))
  591. return 1;
  592. }
  593. return 0;
  594. }
  595. EXPORT_SYMBOL_GPL(ipv6_opt_accepted);
  596. int
  597. snmp6_mib_init(void *ptr[2], size_t mibsize, size_t mibalign)
  598. {
  599. if (ptr == NULL)
  600. return -EINVAL;
  601. ptr[0] = __alloc_percpu(mibsize);
  602. if (!ptr[0])
  603. goto err0;
  604. ptr[1] = __alloc_percpu(mibsize);
  605. if (!ptr[1])
  606. goto err1;
  607. return 0;
  608. err1:
  609. free_percpu(ptr[0]);
  610. ptr[0] = NULL;
  611. err0:
  612. return -ENOMEM;
  613. }
  614. void
  615. snmp6_mib_free(void *ptr[2])
  616. {
  617. if (ptr == NULL)
  618. return;
  619. if (ptr[0])
  620. free_percpu(ptr[0]);
  621. if (ptr[1])
  622. free_percpu(ptr[1]);
  623. ptr[0] = ptr[1] = NULL;
  624. }
  625. static int __init init_ipv6_mibs(void)
  626. {
  627. if (snmp6_mib_init((void **)ipv6_statistics, sizeof (struct ipstats_mib),
  628. __alignof__(struct ipstats_mib)) < 0)
  629. goto err_ip_mib;
  630. if (snmp6_mib_init((void **)icmpv6_statistics, sizeof (struct icmpv6_mib),
  631. __alignof__(struct icmpv6_mib)) < 0)
  632. goto err_icmp_mib;
  633. if (snmp6_mib_init((void **)udp_stats_in6, sizeof (struct udp_mib),
  634. __alignof__(struct udp_mib)) < 0)
  635. goto err_udp_mib;
  636. return 0;
  637. err_udp_mib:
  638. snmp6_mib_free((void **)icmpv6_statistics);
  639. err_icmp_mib:
  640. snmp6_mib_free((void **)ipv6_statistics);
  641. err_ip_mib:
  642. return -ENOMEM;
  643. }
  644. static void cleanup_ipv6_mibs(void)
  645. {
  646. snmp6_mib_free((void **)ipv6_statistics);
  647. snmp6_mib_free((void **)icmpv6_statistics);
  648. snmp6_mib_free((void **)udp_stats_in6);
  649. }
  650. static int __init inet6_init(void)
  651. {
  652. struct sk_buff *dummy_skb;
  653. struct list_head *r;
  654. int err;
  655. #ifdef MODULE
  656. #if 0 /* FIXME --RR */
  657. if (!mod_member_present(&__this_module, can_unload))
  658. return -EINVAL;
  659. __this_module.can_unload = &ipv6_unload;
  660. #endif
  661. #endif
  662. if (sizeof(struct inet6_skb_parm) > sizeof(dummy_skb->cb)) {
  663. printk(KERN_CRIT "inet6_proto_init: size fault\n");
  664. return -EINVAL;
  665. }
  666. err = proto_register(&tcpv6_prot, 1);
  667. if (err)
  668. goto out;
  669. err = proto_register(&udpv6_prot, 1);
  670. if (err)
  671. goto out_unregister_tcp_proto;
  672. err = proto_register(&rawv6_prot, 1);
  673. if (err)
  674. goto out_unregister_udp_proto;
  675. /* Register the socket-side information for inet6_create. */
  676. for(r = &inetsw6[0]; r < &inetsw6[SOCK_MAX]; ++r)
  677. INIT_LIST_HEAD(r);
  678. /* We MUST register RAW sockets before we create the ICMP6,
  679. * IGMP6, or NDISC control sockets.
  680. */
  681. inet6_register_protosw(&rawv6_protosw);
  682. /* Register the family here so that the init calls below will
  683. * be able to create sockets. (?? is this dangerous ??)
  684. */
  685. err = sock_register(&inet6_family_ops);
  686. if (err)
  687. goto out_unregister_raw_proto;
  688. /* Initialise ipv6 mibs */
  689. err = init_ipv6_mibs();
  690. if (err)
  691. goto out_unregister_sock;
  692. /*
  693. * ipngwg API draft makes clear that the correct semantics
  694. * for TCP and UDP is to consider one TCP and UDP instance
  695. * in a host availiable by both INET and INET6 APIs and
  696. * able to communicate via both network protocols.
  697. */
  698. #ifdef CONFIG_SYSCTL
  699. ipv6_sysctl_register();
  700. #endif
  701. err = icmpv6_init(&inet6_family_ops);
  702. if (err)
  703. goto icmp_fail;
  704. err = ndisc_init(&inet6_family_ops);
  705. if (err)
  706. goto ndisc_fail;
  707. err = igmp6_init(&inet6_family_ops);
  708. if (err)
  709. goto igmp_fail;
  710. err = ipv6_netfilter_init();
  711. if (err)
  712. goto netfilter_fail;
  713. /* Create /proc/foo6 entries. */
  714. #ifdef CONFIG_PROC_FS
  715. err = -ENOMEM;
  716. if (raw6_proc_init())
  717. goto proc_raw6_fail;
  718. if (tcp6_proc_init())
  719. goto proc_tcp6_fail;
  720. if (udp6_proc_init())
  721. goto proc_udp6_fail;
  722. if (ipv6_misc_proc_init())
  723. goto proc_misc6_fail;
  724. if (ac6_proc_init())
  725. goto proc_anycast6_fail;
  726. if (if6_proc_init())
  727. goto proc_if6_fail;
  728. #endif
  729. ip6_route_init();
  730. ip6_flowlabel_init();
  731. err = addrconf_init();
  732. if (err)
  733. goto addrconf_fail;
  734. sit_init();
  735. /* Init v6 extension headers. */
  736. ipv6_rthdr_init();
  737. ipv6_frag_init();
  738. ipv6_nodata_init();
  739. ipv6_destopt_init();
  740. /* Init v6 transport protocols. */
  741. udpv6_init();
  742. tcpv6_init();
  743. ipv6_packet_init();
  744. err = 0;
  745. out:
  746. return err;
  747. addrconf_fail:
  748. ip6_flowlabel_cleanup();
  749. ip6_route_cleanup();
  750. #ifdef CONFIG_PROC_FS
  751. if6_proc_exit();
  752. proc_if6_fail:
  753. ac6_proc_exit();
  754. proc_anycast6_fail:
  755. ipv6_misc_proc_exit();
  756. proc_misc6_fail:
  757. udp6_proc_exit();
  758. proc_udp6_fail:
  759. tcp6_proc_exit();
  760. proc_tcp6_fail:
  761. raw6_proc_exit();
  762. proc_raw6_fail:
  763. #endif
  764. ipv6_netfilter_fini();
  765. netfilter_fail:
  766. igmp6_cleanup();
  767. igmp_fail:
  768. ndisc_cleanup();
  769. ndisc_fail:
  770. icmpv6_cleanup();
  771. icmp_fail:
  772. #ifdef CONFIG_SYSCTL
  773. ipv6_sysctl_unregister();
  774. #endif
  775. cleanup_ipv6_mibs();
  776. out_unregister_sock:
  777. sock_unregister(PF_INET6);
  778. out_unregister_raw_proto:
  779. proto_unregister(&rawv6_prot);
  780. out_unregister_udp_proto:
  781. proto_unregister(&udpv6_prot);
  782. out_unregister_tcp_proto:
  783. proto_unregister(&tcpv6_prot);
  784. goto out;
  785. }
  786. module_init(inet6_init);
  787. static void __exit inet6_exit(void)
  788. {
  789. /* First of all disallow new sockets creation. */
  790. sock_unregister(PF_INET6);
  791. #ifdef CONFIG_PROC_FS
  792. if6_proc_exit();
  793. ac6_proc_exit();
  794. ipv6_misc_proc_exit();
  795. udp6_proc_exit();
  796. tcp6_proc_exit();
  797. raw6_proc_exit();
  798. #endif
  799. /* Cleanup code parts. */
  800. sit_cleanup();
  801. ip6_flowlabel_cleanup();
  802. addrconf_cleanup();
  803. ip6_route_cleanup();
  804. ipv6_packet_cleanup();
  805. igmp6_cleanup();
  806. ipv6_netfilter_fini();
  807. ndisc_cleanup();
  808. icmpv6_cleanup();
  809. #ifdef CONFIG_SYSCTL
  810. ipv6_sysctl_unregister();
  811. #endif
  812. cleanup_ipv6_mibs();
  813. proto_unregister(&rawv6_prot);
  814. proto_unregister(&udpv6_prot);
  815. proto_unregister(&tcpv6_prot);
  816. }
  817. module_exit(inet6_exit);
  818. MODULE_ALIAS_NETPROTO(PF_INET6);