af_inet6.c 23 KB

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