af_inet6.c 23 KB

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