af_inet6.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  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. };
  410. const struct proto_ops inet6_dgram_ops = {
  411. .family = PF_INET6,
  412. .owner = THIS_MODULE,
  413. .release = inet6_release,
  414. .bind = inet6_bind,
  415. .connect = inet_dgram_connect, /* ok */
  416. .socketpair = sock_no_socketpair, /* a do nothing */
  417. .accept = sock_no_accept, /* a do nothing */
  418. .getname = inet6_getname,
  419. .poll = udp_poll, /* ok */
  420. .ioctl = inet6_ioctl, /* must change */
  421. .listen = sock_no_listen, /* ok */
  422. .shutdown = inet_shutdown, /* ok */
  423. .setsockopt = sock_common_setsockopt, /* ok */
  424. .getsockopt = sock_common_getsockopt, /* ok */
  425. .sendmsg = inet_sendmsg, /* ok */
  426. .recvmsg = sock_common_recvmsg, /* ok */
  427. .mmap = sock_no_mmap,
  428. .sendpage = sock_no_sendpage,
  429. };
  430. static struct net_proto_family inet6_family_ops = {
  431. .family = PF_INET6,
  432. .create = inet6_create,
  433. .owner = THIS_MODULE,
  434. };
  435. /* Same as inet6_dgram_ops, sans udp_poll. */
  436. static const struct proto_ops inet6_sockraw_ops = {
  437. .family = PF_INET6,
  438. .owner = THIS_MODULE,
  439. .release = inet6_release,
  440. .bind = inet6_bind,
  441. .connect = inet_dgram_connect, /* ok */
  442. .socketpair = sock_no_socketpair, /* a do nothing */
  443. .accept = sock_no_accept, /* a do nothing */
  444. .getname = inet6_getname,
  445. .poll = datagram_poll, /* ok */
  446. .ioctl = inet6_ioctl, /* must change */
  447. .listen = sock_no_listen, /* ok */
  448. .shutdown = inet_shutdown, /* ok */
  449. .setsockopt = sock_common_setsockopt, /* ok */
  450. .getsockopt = sock_common_getsockopt, /* ok */
  451. .sendmsg = inet_sendmsg, /* ok */
  452. .recvmsg = sock_common_recvmsg, /* ok */
  453. .mmap = sock_no_mmap,
  454. .sendpage = sock_no_sendpage,
  455. };
  456. static struct inet_protosw rawv6_protosw = {
  457. .type = SOCK_RAW,
  458. .protocol = IPPROTO_IP, /* wild card */
  459. .prot = &rawv6_prot,
  460. .ops = &inet6_sockraw_ops,
  461. .capability = CAP_NET_RAW,
  462. .no_check = UDP_CSUM_DEFAULT,
  463. .flags = INET_PROTOSW_REUSE,
  464. };
  465. void
  466. inet6_register_protosw(struct inet_protosw *p)
  467. {
  468. struct list_head *lh;
  469. struct inet_protosw *answer;
  470. int protocol = p->protocol;
  471. struct list_head *last_perm;
  472. spin_lock_bh(&inetsw6_lock);
  473. if (p->type >= SOCK_MAX)
  474. goto out_illegal;
  475. /* If we are trying to override a permanent protocol, bail. */
  476. answer = NULL;
  477. last_perm = &inetsw6[p->type];
  478. list_for_each(lh, &inetsw6[p->type]) {
  479. answer = list_entry(lh, struct inet_protosw, list);
  480. /* Check only the non-wild match. */
  481. if (INET_PROTOSW_PERMANENT & answer->flags) {
  482. if (protocol == answer->protocol)
  483. break;
  484. last_perm = lh;
  485. }
  486. answer = NULL;
  487. }
  488. if (answer)
  489. goto out_permanent;
  490. /* Add the new entry after the last permanent entry if any, so that
  491. * the new entry does not override a permanent entry when matched with
  492. * a wild-card protocol. But it is allowed to override any existing
  493. * non-permanent entry. This means that when we remove this entry, the
  494. * system automatically returns to the old behavior.
  495. */
  496. list_add_rcu(&p->list, last_perm);
  497. out:
  498. spin_unlock_bh(&inetsw6_lock);
  499. return;
  500. out_permanent:
  501. printk(KERN_ERR "Attempt to override permanent protocol %d.\n",
  502. protocol);
  503. goto out;
  504. out_illegal:
  505. printk(KERN_ERR
  506. "Ignoring attempt to register invalid socket type %d.\n",
  507. p->type);
  508. goto out;
  509. }
  510. void
  511. inet6_unregister_protosw(struct inet_protosw *p)
  512. {
  513. if (INET_PROTOSW_PERMANENT & p->flags) {
  514. printk(KERN_ERR
  515. "Attempt to unregister permanent protocol %d.\n",
  516. p->protocol);
  517. } else {
  518. spin_lock_bh(&inetsw6_lock);
  519. list_del_rcu(&p->list);
  520. spin_unlock_bh(&inetsw6_lock);
  521. synchronize_net();
  522. }
  523. }
  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. if (np->opt && np->opt->srcrt) {
  543. struct rt0_hdr *rt0 = (struct rt0_hdr *) np->opt->srcrt;
  544. ipv6_addr_copy(&final, &fl.fl6_dst);
  545. ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
  546. final_p = &final;
  547. }
  548. err = ip6_dst_lookup(sk, &dst, &fl);
  549. if (err) {
  550. sk->sk_route_caps = 0;
  551. return err;
  552. }
  553. if (final_p)
  554. ipv6_addr_copy(&fl.fl6_dst, final_p);
  555. if ((err = xfrm_lookup(&dst, &fl, sk, 0)) < 0) {
  556. sk->sk_err_soft = -err;
  557. return err;
  558. }
  559. ip6_dst_store(sk, dst, NULL);
  560. sk->sk_route_caps = dst->dev->features &
  561. ~(NETIF_F_IP_CSUM | NETIF_F_TSO);
  562. }
  563. return 0;
  564. }
  565. EXPORT_SYMBOL_GPL(inet6_sk_rebuild_header);
  566. int ipv6_opt_accepted(struct sock *sk, struct sk_buff *skb)
  567. {
  568. struct ipv6_pinfo *np = inet6_sk(sk);
  569. struct inet6_skb_parm *opt = IP6CB(skb);
  570. if (np->rxopt.all) {
  571. if ((opt->hop && (np->rxopt.bits.hopopts ||
  572. np->rxopt.bits.ohopopts)) ||
  573. ((IPV6_FLOWINFO_MASK & *(u32*)skb->nh.raw) &&
  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. int
  585. snmp6_mib_init(void *ptr[2], size_t mibsize, size_t mibalign)
  586. {
  587. if (ptr == NULL)
  588. return -EINVAL;
  589. ptr[0] = __alloc_percpu(mibsize);
  590. if (!ptr[0])
  591. goto err0;
  592. ptr[1] = __alloc_percpu(mibsize);
  593. if (!ptr[1])
  594. goto err1;
  595. return 0;
  596. err1:
  597. free_percpu(ptr[0]);
  598. ptr[0] = NULL;
  599. err0:
  600. return -ENOMEM;
  601. }
  602. void
  603. snmp6_mib_free(void *ptr[2])
  604. {
  605. if (ptr == NULL)
  606. return;
  607. if (ptr[0])
  608. free_percpu(ptr[0]);
  609. if (ptr[1])
  610. free_percpu(ptr[1]);
  611. ptr[0] = ptr[1] = NULL;
  612. }
  613. static int __init init_ipv6_mibs(void)
  614. {
  615. if (snmp6_mib_init((void **)ipv6_statistics, sizeof (struct ipstats_mib),
  616. __alignof__(struct ipstats_mib)) < 0)
  617. goto err_ip_mib;
  618. if (snmp6_mib_init((void **)icmpv6_statistics, sizeof (struct icmpv6_mib),
  619. __alignof__(struct icmpv6_mib)) < 0)
  620. goto err_icmp_mib;
  621. if (snmp6_mib_init((void **)udp_stats_in6, sizeof (struct udp_mib),
  622. __alignof__(struct udp_mib)) < 0)
  623. goto err_udp_mib;
  624. return 0;
  625. err_udp_mib:
  626. snmp6_mib_free((void **)icmpv6_statistics);
  627. err_icmp_mib:
  628. snmp6_mib_free((void **)ipv6_statistics);
  629. err_ip_mib:
  630. return -ENOMEM;
  631. }
  632. static void cleanup_ipv6_mibs(void)
  633. {
  634. snmp6_mib_free((void **)ipv6_statistics);
  635. snmp6_mib_free((void **)icmpv6_statistics);
  636. snmp6_mib_free((void **)udp_stats_in6);
  637. }
  638. static int __init inet6_init(void)
  639. {
  640. struct sk_buff *dummy_skb;
  641. struct list_head *r;
  642. int err;
  643. #ifdef MODULE
  644. #if 0 /* FIXME --RR */
  645. if (!mod_member_present(&__this_module, can_unload))
  646. return -EINVAL;
  647. __this_module.can_unload = &ipv6_unload;
  648. #endif
  649. #endif
  650. if (sizeof(struct inet6_skb_parm) > sizeof(dummy_skb->cb)) {
  651. printk(KERN_CRIT "inet6_proto_init: size fault\n");
  652. return -EINVAL;
  653. }
  654. err = proto_register(&tcpv6_prot, 1);
  655. if (err)
  656. goto out;
  657. err = proto_register(&udpv6_prot, 1);
  658. if (err)
  659. goto out_unregister_tcp_proto;
  660. err = proto_register(&rawv6_prot, 1);
  661. if (err)
  662. goto out_unregister_udp_proto;
  663. /* Register the socket-side information for inet6_create. */
  664. for(r = &inetsw6[0]; r < &inetsw6[SOCK_MAX]; ++r)
  665. INIT_LIST_HEAD(r);
  666. /* We MUST register RAW sockets before we create the ICMP6,
  667. * IGMP6, or NDISC control sockets.
  668. */
  669. inet6_register_protosw(&rawv6_protosw);
  670. /* Register the family here so that the init calls below will
  671. * be able to create sockets. (?? is this dangerous ??)
  672. */
  673. err = sock_register(&inet6_family_ops);
  674. if (err)
  675. goto out_unregister_raw_proto;
  676. /* Initialise ipv6 mibs */
  677. err = init_ipv6_mibs();
  678. if (err)
  679. goto out_unregister_sock;
  680. /*
  681. * ipngwg API draft makes clear that the correct semantics
  682. * for TCP and UDP is to consider one TCP and UDP instance
  683. * in a host availiable by both INET and INET6 APIs and
  684. * able to communicate via both network protocols.
  685. */
  686. #ifdef CONFIG_SYSCTL
  687. ipv6_sysctl_register();
  688. #endif
  689. err = icmpv6_init(&inet6_family_ops);
  690. if (err)
  691. goto icmp_fail;
  692. err = ndisc_init(&inet6_family_ops);
  693. if (err)
  694. goto ndisc_fail;
  695. err = igmp6_init(&inet6_family_ops);
  696. if (err)
  697. goto igmp_fail;
  698. err = ipv6_netfilter_init();
  699. if (err)
  700. goto netfilter_fail;
  701. /* Create /proc/foo6 entries. */
  702. #ifdef CONFIG_PROC_FS
  703. err = -ENOMEM;
  704. if (raw6_proc_init())
  705. goto proc_raw6_fail;
  706. if (tcp6_proc_init())
  707. goto proc_tcp6_fail;
  708. if (udp6_proc_init())
  709. goto proc_udp6_fail;
  710. if (ipv6_misc_proc_init())
  711. goto proc_misc6_fail;
  712. if (ac6_proc_init())
  713. goto proc_anycast6_fail;
  714. if (if6_proc_init())
  715. goto proc_if6_fail;
  716. #endif
  717. ip6_route_init();
  718. ip6_flowlabel_init();
  719. err = addrconf_init();
  720. if (err)
  721. goto addrconf_fail;
  722. sit_init();
  723. /* Init v6 extension headers. */
  724. ipv6_rthdr_init();
  725. ipv6_frag_init();
  726. ipv6_nodata_init();
  727. ipv6_destopt_init();
  728. /* Init v6 transport protocols. */
  729. udpv6_init();
  730. tcpv6_init();
  731. ipv6_packet_init();
  732. err = 0;
  733. out:
  734. return err;
  735. addrconf_fail:
  736. ip6_flowlabel_cleanup();
  737. ip6_route_cleanup();
  738. #ifdef CONFIG_PROC_FS
  739. if6_proc_exit();
  740. proc_if6_fail:
  741. ac6_proc_exit();
  742. proc_anycast6_fail:
  743. ipv6_misc_proc_exit();
  744. proc_misc6_fail:
  745. udp6_proc_exit();
  746. proc_udp6_fail:
  747. tcp6_proc_exit();
  748. proc_tcp6_fail:
  749. raw6_proc_exit();
  750. proc_raw6_fail:
  751. #endif
  752. ipv6_netfilter_fini();
  753. netfilter_fail:
  754. igmp6_cleanup();
  755. igmp_fail:
  756. ndisc_cleanup();
  757. ndisc_fail:
  758. icmpv6_cleanup();
  759. icmp_fail:
  760. #ifdef CONFIG_SYSCTL
  761. ipv6_sysctl_unregister();
  762. #endif
  763. cleanup_ipv6_mibs();
  764. out_unregister_sock:
  765. sock_unregister(PF_INET6);
  766. out_unregister_raw_proto:
  767. proto_unregister(&rawv6_prot);
  768. out_unregister_udp_proto:
  769. proto_unregister(&udpv6_prot);
  770. out_unregister_tcp_proto:
  771. proto_unregister(&tcpv6_prot);
  772. goto out;
  773. }
  774. module_init(inet6_init);
  775. static void __exit inet6_exit(void)
  776. {
  777. /* First of all disallow new sockets creation. */
  778. sock_unregister(PF_INET6);
  779. #ifdef CONFIG_PROC_FS
  780. if6_proc_exit();
  781. ac6_proc_exit();
  782. ipv6_misc_proc_exit();
  783. udp6_proc_exit();
  784. tcp6_proc_exit();
  785. raw6_proc_exit();
  786. #endif
  787. /* Cleanup code parts. */
  788. sit_cleanup();
  789. ip6_flowlabel_cleanup();
  790. addrconf_cleanup();
  791. ip6_route_cleanup();
  792. ipv6_packet_cleanup();
  793. igmp6_cleanup();
  794. ipv6_netfilter_fini();
  795. ndisc_cleanup();
  796. icmpv6_cleanup();
  797. #ifdef CONFIG_SYSCTL
  798. ipv6_sysctl_unregister();
  799. #endif
  800. cleanup_ipv6_mibs();
  801. proto_unregister(&rawv6_prot);
  802. proto_unregister(&udpv6_prot);
  803. proto_unregister(&tcpv6_prot);
  804. }
  805. module_exit(inet6_exit);
  806. MODULE_ALIAS_NETPROTO(PF_INET6);