af_inet6.c 23 KB

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