af_inet6.c 22 KB

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