af_inet6.c 23 KB

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