af_inet6.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  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. /*
  321. * Release destination entry
  322. */
  323. sk_dst_reset(sk);
  324. /* Release rx options */
  325. if ((skb = xchg(&np->pktoptions, NULL)) != NULL)
  326. kfree_skb(skb);
  327. /* Free flowlabels */
  328. fl6_free_socklist(sk);
  329. /* Free tx options */
  330. if ((opt = xchg(&np->opt, NULL)) != NULL)
  331. sock_kfree_s(sk, opt, opt->tot_len);
  332. return 0;
  333. }
  334. EXPORT_SYMBOL_GPL(inet6_destroy_sock);
  335. /*
  336. * This does both peername and sockname.
  337. */
  338. int inet6_getname(struct socket *sock, struct sockaddr *uaddr,
  339. int *uaddr_len, int peer)
  340. {
  341. struct sockaddr_in6 *sin=(struct sockaddr_in6 *)uaddr;
  342. struct sock *sk = sock->sk;
  343. struct inet_sock *inet = inet_sk(sk);
  344. struct ipv6_pinfo *np = inet6_sk(sk);
  345. sin->sin6_family = AF_INET6;
  346. sin->sin6_flowinfo = 0;
  347. sin->sin6_scope_id = 0;
  348. if (peer) {
  349. if (!inet->dport)
  350. return -ENOTCONN;
  351. if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
  352. peer == 1)
  353. return -ENOTCONN;
  354. sin->sin6_port = inet->dport;
  355. ipv6_addr_copy(&sin->sin6_addr, &np->daddr);
  356. if (np->sndflow)
  357. sin->sin6_flowinfo = np->flow_label;
  358. } else {
  359. if (ipv6_addr_any(&np->rcv_saddr))
  360. ipv6_addr_copy(&sin->sin6_addr, &np->saddr);
  361. else
  362. ipv6_addr_copy(&sin->sin6_addr, &np->rcv_saddr);
  363. sin->sin6_port = inet->sport;
  364. }
  365. if (ipv6_addr_type(&sin->sin6_addr) & IPV6_ADDR_LINKLOCAL)
  366. sin->sin6_scope_id = sk->sk_bound_dev_if;
  367. *uaddr_len = sizeof(*sin);
  368. return(0);
  369. }
  370. int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  371. {
  372. struct sock *sk = sock->sk;
  373. switch(cmd)
  374. {
  375. case SIOCGSTAMP:
  376. return sock_get_timestamp(sk, (struct timeval __user *)arg);
  377. case SIOCADDRT:
  378. case SIOCDELRT:
  379. return(ipv6_route_ioctl(cmd,(void __user *)arg));
  380. case SIOCSIFADDR:
  381. return addrconf_add_ifaddr((void __user *) arg);
  382. case SIOCDIFADDR:
  383. return addrconf_del_ifaddr((void __user *) arg);
  384. case SIOCSIFDSTADDR:
  385. return addrconf_set_dstaddr((void __user *) arg);
  386. default:
  387. if (!sk->sk_prot->ioctl)
  388. return -ENOIOCTLCMD;
  389. return sk->sk_prot->ioctl(sk, cmd, arg);
  390. }
  391. /*NOTREACHED*/
  392. return(0);
  393. }
  394. const struct proto_ops inet6_stream_ops = {
  395. .family = PF_INET6,
  396. .owner = THIS_MODULE,
  397. .release = inet6_release,
  398. .bind = inet6_bind,
  399. .connect = inet_stream_connect, /* ok */
  400. .socketpair = sock_no_socketpair, /* a do nothing */
  401. .accept = inet_accept, /* ok */
  402. .getname = inet6_getname,
  403. .poll = tcp_poll, /* ok */
  404. .ioctl = inet6_ioctl, /* must change */
  405. .listen = inet_listen, /* ok */
  406. .shutdown = inet_shutdown, /* ok */
  407. .setsockopt = sock_common_setsockopt, /* ok */
  408. .getsockopt = sock_common_getsockopt, /* ok */
  409. .sendmsg = inet_sendmsg, /* ok */
  410. .recvmsg = sock_common_recvmsg, /* ok */
  411. .mmap = sock_no_mmap,
  412. .sendpage = tcp_sendpage
  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. };
  434. static struct net_proto_family inet6_family_ops = {
  435. .family = PF_INET6,
  436. .create = inet6_create,
  437. .owner = THIS_MODULE,
  438. };
  439. /* Same as inet6_dgram_ops, sans udp_poll. */
  440. static const struct proto_ops inet6_sockraw_ops = {
  441. .family = PF_INET6,
  442. .owner = THIS_MODULE,
  443. .release = inet6_release,
  444. .bind = inet6_bind,
  445. .connect = inet_dgram_connect, /* ok */
  446. .socketpair = sock_no_socketpair, /* a do nothing */
  447. .accept = sock_no_accept, /* a do nothing */
  448. .getname = inet6_getname,
  449. .poll = datagram_poll, /* ok */
  450. .ioctl = inet6_ioctl, /* must change */
  451. .listen = sock_no_listen, /* ok */
  452. .shutdown = inet_shutdown, /* ok */
  453. .setsockopt = sock_common_setsockopt, /* ok */
  454. .getsockopt = sock_common_getsockopt, /* ok */
  455. .sendmsg = inet_sendmsg, /* ok */
  456. .recvmsg = sock_common_recvmsg, /* ok */
  457. .mmap = sock_no_mmap,
  458. .sendpage = sock_no_sendpage,
  459. };
  460. static struct inet_protosw rawv6_protosw = {
  461. .type = SOCK_RAW,
  462. .protocol = IPPROTO_IP, /* wild card */
  463. .prot = &rawv6_prot,
  464. .ops = &inet6_sockraw_ops,
  465. .capability = CAP_NET_RAW,
  466. .no_check = UDP_CSUM_DEFAULT,
  467. .flags = INET_PROTOSW_REUSE,
  468. };
  469. void
  470. inet6_register_protosw(struct inet_protosw *p)
  471. {
  472. struct list_head *lh;
  473. struct inet_protosw *answer;
  474. int protocol = p->protocol;
  475. struct list_head *last_perm;
  476. spin_lock_bh(&inetsw6_lock);
  477. if (p->type >= SOCK_MAX)
  478. goto out_illegal;
  479. /* If we are trying to override a permanent protocol, bail. */
  480. answer = NULL;
  481. last_perm = &inetsw6[p->type];
  482. list_for_each(lh, &inetsw6[p->type]) {
  483. answer = list_entry(lh, struct inet_protosw, list);
  484. /* Check only the non-wild match. */
  485. if (INET_PROTOSW_PERMANENT & answer->flags) {
  486. if (protocol == answer->protocol)
  487. break;
  488. last_perm = lh;
  489. }
  490. answer = NULL;
  491. }
  492. if (answer)
  493. goto out_permanent;
  494. /* Add the new entry after the last permanent entry if any, so that
  495. * the new entry does not override a permanent entry when matched with
  496. * a wild-card protocol. But it is allowed to override any existing
  497. * non-permanent entry. This means that when we remove this entry, the
  498. * system automatically returns to the old behavior.
  499. */
  500. list_add_rcu(&p->list, last_perm);
  501. out:
  502. spin_unlock_bh(&inetsw6_lock);
  503. return;
  504. out_permanent:
  505. printk(KERN_ERR "Attempt to override permanent protocol %d.\n",
  506. protocol);
  507. goto out;
  508. out_illegal:
  509. printk(KERN_ERR
  510. "Ignoring attempt to register invalid socket type %d.\n",
  511. p->type);
  512. goto out;
  513. }
  514. void
  515. inet6_unregister_protosw(struct inet_protosw *p)
  516. {
  517. if (INET_PROTOSW_PERMANENT & p->flags) {
  518. printk(KERN_ERR
  519. "Attempt to unregister permanent protocol %d.\n",
  520. p->protocol);
  521. } else {
  522. spin_lock_bh(&inetsw6_lock);
  523. list_del_rcu(&p->list);
  524. spin_unlock_bh(&inetsw6_lock);
  525. synchronize_net();
  526. }
  527. }
  528. int inet6_sk_rebuild_header(struct sock *sk)
  529. {
  530. int err;
  531. struct dst_entry *dst;
  532. struct ipv6_pinfo *np = inet6_sk(sk);
  533. dst = __sk_dst_check(sk, np->dst_cookie);
  534. if (dst == NULL) {
  535. struct inet_sock *inet = inet_sk(sk);
  536. struct in6_addr *final_p = NULL, final;
  537. struct flowi fl;
  538. memset(&fl, 0, sizeof(fl));
  539. fl.proto = sk->sk_protocol;
  540. ipv6_addr_copy(&fl.fl6_dst, &np->daddr);
  541. ipv6_addr_copy(&fl.fl6_src, &np->saddr);
  542. fl.fl6_flowlabel = np->flow_label;
  543. fl.oif = sk->sk_bound_dev_if;
  544. fl.fl_ip_dport = inet->dport;
  545. fl.fl_ip_sport = inet->sport;
  546. if (np->opt && np->opt->srcrt) {
  547. struct rt0_hdr *rt0 = (struct rt0_hdr *) np->opt->srcrt;
  548. ipv6_addr_copy(&final, &fl.fl6_dst);
  549. ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
  550. final_p = &final;
  551. }
  552. err = ip6_dst_lookup(sk, &dst, &fl);
  553. if (err) {
  554. sk->sk_route_caps = 0;
  555. return err;
  556. }
  557. if (final_p)
  558. ipv6_addr_copy(&fl.fl6_dst, final_p);
  559. if ((err = xfrm_lookup(&dst, &fl, sk, 0)) < 0) {
  560. sk->sk_err_soft = -err;
  561. return err;
  562. }
  563. ip6_dst_store(sk, dst, NULL);
  564. sk->sk_route_caps = dst->dev->features &
  565. ~(NETIF_F_IP_CSUM | NETIF_F_TSO);
  566. }
  567. return 0;
  568. }
  569. EXPORT_SYMBOL_GPL(inet6_sk_rebuild_header);
  570. int ipv6_opt_accepted(struct sock *sk, struct sk_buff *skb)
  571. {
  572. struct ipv6_pinfo *np = inet6_sk(sk);
  573. struct inet6_skb_parm *opt = IP6CB(skb);
  574. if (np->rxopt.all) {
  575. if ((opt->hop && (np->rxopt.bits.hopopts ||
  576. np->rxopt.bits.ohopopts)) ||
  577. ((IPV6_FLOWINFO_MASK & *(u32*)skb->nh.raw) &&
  578. np->rxopt.bits.rxflow) ||
  579. (opt->srcrt && (np->rxopt.bits.srcrt ||
  580. np->rxopt.bits.osrcrt)) ||
  581. ((opt->dst1 || opt->dst0) &&
  582. (np->rxopt.bits.dstopts || np->rxopt.bits.odstopts)))
  583. return 1;
  584. }
  585. return 0;
  586. }
  587. EXPORT_SYMBOL_GPL(ipv6_opt_accepted);
  588. int
  589. snmp6_mib_init(void *ptr[2], size_t mibsize, size_t mibalign)
  590. {
  591. if (ptr == NULL)
  592. return -EINVAL;
  593. ptr[0] = __alloc_percpu(mibsize);
  594. if (!ptr[0])
  595. goto err0;
  596. ptr[1] = __alloc_percpu(mibsize);
  597. if (!ptr[1])
  598. goto err1;
  599. return 0;
  600. err1:
  601. free_percpu(ptr[0]);
  602. ptr[0] = NULL;
  603. err0:
  604. return -ENOMEM;
  605. }
  606. void
  607. snmp6_mib_free(void *ptr[2])
  608. {
  609. if (ptr == NULL)
  610. return;
  611. if (ptr[0])
  612. free_percpu(ptr[0]);
  613. if (ptr[1])
  614. free_percpu(ptr[1]);
  615. ptr[0] = ptr[1] = NULL;
  616. }
  617. static int __init init_ipv6_mibs(void)
  618. {
  619. if (snmp6_mib_init((void **)ipv6_statistics, sizeof (struct ipstats_mib),
  620. __alignof__(struct ipstats_mib)) < 0)
  621. goto err_ip_mib;
  622. if (snmp6_mib_init((void **)icmpv6_statistics, sizeof (struct icmpv6_mib),
  623. __alignof__(struct icmpv6_mib)) < 0)
  624. goto err_icmp_mib;
  625. if (snmp6_mib_init((void **)udp_stats_in6, sizeof (struct udp_mib),
  626. __alignof__(struct udp_mib)) < 0)
  627. goto err_udp_mib;
  628. return 0;
  629. err_udp_mib:
  630. snmp6_mib_free((void **)icmpv6_statistics);
  631. err_icmp_mib:
  632. snmp6_mib_free((void **)ipv6_statistics);
  633. err_ip_mib:
  634. return -ENOMEM;
  635. }
  636. static void cleanup_ipv6_mibs(void)
  637. {
  638. snmp6_mib_free((void **)ipv6_statistics);
  639. snmp6_mib_free((void **)icmpv6_statistics);
  640. snmp6_mib_free((void **)udp_stats_in6);
  641. }
  642. static int __init inet6_init(void)
  643. {
  644. struct sk_buff *dummy_skb;
  645. struct list_head *r;
  646. int err;
  647. #ifdef MODULE
  648. #if 0 /* FIXME --RR */
  649. if (!mod_member_present(&__this_module, can_unload))
  650. return -EINVAL;
  651. __this_module.can_unload = &ipv6_unload;
  652. #endif
  653. #endif
  654. if (sizeof(struct inet6_skb_parm) > sizeof(dummy_skb->cb)) {
  655. printk(KERN_CRIT "inet6_proto_init: size fault\n");
  656. return -EINVAL;
  657. }
  658. err = proto_register(&tcpv6_prot, 1);
  659. if (err)
  660. goto out;
  661. err = proto_register(&udpv6_prot, 1);
  662. if (err)
  663. goto out_unregister_tcp_proto;
  664. err = proto_register(&rawv6_prot, 1);
  665. if (err)
  666. goto out_unregister_udp_proto;
  667. /* Register the socket-side information for inet6_create. */
  668. for(r = &inetsw6[0]; r < &inetsw6[SOCK_MAX]; ++r)
  669. INIT_LIST_HEAD(r);
  670. /* We MUST register RAW sockets before we create the ICMP6,
  671. * IGMP6, or NDISC control sockets.
  672. */
  673. inet6_register_protosw(&rawv6_protosw);
  674. /* Register the family here so that the init calls below will
  675. * be able to create sockets. (?? is this dangerous ??)
  676. */
  677. err = sock_register(&inet6_family_ops);
  678. if (err)
  679. goto out_unregister_raw_proto;
  680. /* Initialise ipv6 mibs */
  681. err = init_ipv6_mibs();
  682. if (err)
  683. goto out_unregister_sock;
  684. /*
  685. * ipngwg API draft makes clear that the correct semantics
  686. * for TCP and UDP is to consider one TCP and UDP instance
  687. * in a host availiable by both INET and INET6 APIs and
  688. * able to communicate via both network protocols.
  689. */
  690. #ifdef CONFIG_SYSCTL
  691. ipv6_sysctl_register();
  692. #endif
  693. err = icmpv6_init(&inet6_family_ops);
  694. if (err)
  695. goto icmp_fail;
  696. err = ndisc_init(&inet6_family_ops);
  697. if (err)
  698. goto ndisc_fail;
  699. err = igmp6_init(&inet6_family_ops);
  700. if (err)
  701. goto igmp_fail;
  702. err = ipv6_netfilter_init();
  703. if (err)
  704. goto netfilter_fail;
  705. /* Create /proc/foo6 entries. */
  706. #ifdef CONFIG_PROC_FS
  707. err = -ENOMEM;
  708. if (raw6_proc_init())
  709. goto proc_raw6_fail;
  710. if (tcp6_proc_init())
  711. goto proc_tcp6_fail;
  712. if (udp6_proc_init())
  713. goto proc_udp6_fail;
  714. if (ipv6_misc_proc_init())
  715. goto proc_misc6_fail;
  716. if (ac6_proc_init())
  717. goto proc_anycast6_fail;
  718. if (if6_proc_init())
  719. goto proc_if6_fail;
  720. #endif
  721. ip6_route_init();
  722. ip6_flowlabel_init();
  723. err = addrconf_init();
  724. if (err)
  725. goto addrconf_fail;
  726. sit_init();
  727. /* Init v6 extension headers. */
  728. ipv6_rthdr_init();
  729. ipv6_frag_init();
  730. ipv6_nodata_init();
  731. ipv6_destopt_init();
  732. /* Init v6 transport protocols. */
  733. udpv6_init();
  734. tcpv6_init();
  735. ipv6_packet_init();
  736. err = 0;
  737. out:
  738. return err;
  739. addrconf_fail:
  740. ip6_flowlabel_cleanup();
  741. ip6_route_cleanup();
  742. #ifdef CONFIG_PROC_FS
  743. if6_proc_exit();
  744. proc_if6_fail:
  745. ac6_proc_exit();
  746. proc_anycast6_fail:
  747. ipv6_misc_proc_exit();
  748. proc_misc6_fail:
  749. udp6_proc_exit();
  750. proc_udp6_fail:
  751. tcp6_proc_exit();
  752. proc_tcp6_fail:
  753. raw6_proc_exit();
  754. proc_raw6_fail:
  755. #endif
  756. ipv6_netfilter_fini();
  757. netfilter_fail:
  758. igmp6_cleanup();
  759. igmp_fail:
  760. ndisc_cleanup();
  761. ndisc_fail:
  762. icmpv6_cleanup();
  763. icmp_fail:
  764. #ifdef CONFIG_SYSCTL
  765. ipv6_sysctl_unregister();
  766. #endif
  767. cleanup_ipv6_mibs();
  768. out_unregister_sock:
  769. sock_unregister(PF_INET6);
  770. out_unregister_raw_proto:
  771. proto_unregister(&rawv6_prot);
  772. out_unregister_udp_proto:
  773. proto_unregister(&udpv6_prot);
  774. out_unregister_tcp_proto:
  775. proto_unregister(&tcpv6_prot);
  776. goto out;
  777. }
  778. module_init(inet6_init);
  779. static void __exit inet6_exit(void)
  780. {
  781. /* First of all disallow new sockets creation. */
  782. sock_unregister(PF_INET6);
  783. #ifdef CONFIG_PROC_FS
  784. if6_proc_exit();
  785. ac6_proc_exit();
  786. ipv6_misc_proc_exit();
  787. udp6_proc_exit();
  788. tcp6_proc_exit();
  789. raw6_proc_exit();
  790. #endif
  791. /* Cleanup code parts. */
  792. sit_cleanup();
  793. ip6_flowlabel_cleanup();
  794. addrconf_cleanup();
  795. ip6_route_cleanup();
  796. ipv6_packet_cleanup();
  797. igmp6_cleanup();
  798. ipv6_netfilter_fini();
  799. ndisc_cleanup();
  800. icmpv6_cleanup();
  801. #ifdef CONFIG_SYSCTL
  802. ipv6_sysctl_unregister();
  803. #endif
  804. cleanup_ipv6_mibs();
  805. proto_unregister(&rawv6_prot);
  806. proto_unregister(&udpv6_prot);
  807. proto_unregister(&tcpv6_prot);
  808. }
  809. module_exit(inet6_exit);
  810. MODULE_ALIAS_NETPROTO(PF_INET6);