af_inet6.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  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. /* 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. EXPORT_SYMBOL(inet6_bind);
  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. EXPORT_SYMBOL(inet6_release);
  319. int inet6_destroy_sock(struct sock *sk)
  320. {
  321. struct ipv6_pinfo *np = inet6_sk(sk);
  322. struct sk_buff *skb;
  323. struct ipv6_txoptions *opt;
  324. /* Release rx options */
  325. if ((skb = xchg(&np->pktoptions, NULL)) != NULL)
  326. kfree_skb(skb);
  327. /* Free flowlabels */
  328. fl6_free_socklist(sk);
  329. /* Free tx options */
  330. if ((opt = xchg(&np->opt, NULL)) != NULL)
  331. sock_kfree_s(sk, opt, opt->tot_len);
  332. return 0;
  333. }
  334. EXPORT_SYMBOL_GPL(inet6_destroy_sock);
  335. /*
  336. * This does both peername and sockname.
  337. */
  338. int inet6_getname(struct socket *sock, struct sockaddr *uaddr,
  339. int *uaddr_len, int peer)
  340. {
  341. struct sockaddr_in6 *sin=(struct sockaddr_in6 *)uaddr;
  342. struct sock *sk = sock->sk;
  343. struct inet_sock *inet = inet_sk(sk);
  344. struct ipv6_pinfo *np = inet6_sk(sk);
  345. sin->sin6_family = AF_INET6;
  346. sin->sin6_flowinfo = 0;
  347. sin->sin6_scope_id = 0;
  348. if (peer) {
  349. if (!inet->dport)
  350. return -ENOTCONN;
  351. if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
  352. peer == 1)
  353. return -ENOTCONN;
  354. sin->sin6_port = inet->dport;
  355. ipv6_addr_copy(&sin->sin6_addr, &np->daddr);
  356. if (np->sndflow)
  357. sin->sin6_flowinfo = np->flow_label;
  358. } else {
  359. if (ipv6_addr_any(&np->rcv_saddr))
  360. ipv6_addr_copy(&sin->sin6_addr, &np->saddr);
  361. else
  362. ipv6_addr_copy(&sin->sin6_addr, &np->rcv_saddr);
  363. sin->sin6_port = inet->sport;
  364. }
  365. if (ipv6_addr_type(&sin->sin6_addr) & IPV6_ADDR_LINKLOCAL)
  366. sin->sin6_scope_id = sk->sk_bound_dev_if;
  367. *uaddr_len = sizeof(*sin);
  368. return(0);
  369. }
  370. EXPORT_SYMBOL(inet6_getname);
  371. int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  372. {
  373. struct sock *sk = sock->sk;
  374. switch(cmd)
  375. {
  376. case SIOCGSTAMP:
  377. return sock_get_timestamp(sk, (struct timeval __user *)arg);
  378. case SIOCGSTAMPNS:
  379. return sock_get_timestampns(sk, (struct timespec __user *)arg);
  380. case SIOCADDRT:
  381. case SIOCDELRT:
  382. return(ipv6_route_ioctl(cmd,(void __user *)arg));
  383. case SIOCSIFADDR:
  384. return addrconf_add_ifaddr((void __user *) arg);
  385. case SIOCDIFADDR:
  386. return addrconf_del_ifaddr((void __user *) arg);
  387. case SIOCSIFDSTADDR:
  388. return addrconf_set_dstaddr((void __user *) arg);
  389. default:
  390. if (!sk->sk_prot->ioctl)
  391. return -ENOIOCTLCMD;
  392. return sk->sk_prot->ioctl(sk, cmd, arg);
  393. }
  394. /*NOTREACHED*/
  395. return(0);
  396. }
  397. EXPORT_SYMBOL(inet6_ioctl);
  398. const struct proto_ops inet6_stream_ops = {
  399. .family = PF_INET6,
  400. .owner = THIS_MODULE,
  401. .release = inet6_release,
  402. .bind = inet6_bind,
  403. .connect = inet_stream_connect, /* ok */
  404. .socketpair = sock_no_socketpair, /* a do nothing */
  405. .accept = inet_accept, /* ok */
  406. .getname = inet6_getname,
  407. .poll = tcp_poll, /* ok */
  408. .ioctl = inet6_ioctl, /* must change */
  409. .listen = inet_listen, /* ok */
  410. .shutdown = inet_shutdown, /* ok */
  411. .setsockopt = sock_common_setsockopt, /* ok */
  412. .getsockopt = sock_common_getsockopt, /* ok */
  413. .sendmsg = inet_sendmsg, /* ok */
  414. .recvmsg = sock_common_recvmsg, /* ok */
  415. .mmap = sock_no_mmap,
  416. .sendpage = tcp_sendpage,
  417. #ifdef CONFIG_COMPAT
  418. .compat_setsockopt = compat_sock_common_setsockopt,
  419. .compat_getsockopt = compat_sock_common_getsockopt,
  420. #endif
  421. };
  422. const struct proto_ops inet6_dgram_ops = {
  423. .family = PF_INET6,
  424. .owner = THIS_MODULE,
  425. .release = inet6_release,
  426. .bind = inet6_bind,
  427. .connect = inet_dgram_connect, /* ok */
  428. .socketpair = sock_no_socketpair, /* a do nothing */
  429. .accept = sock_no_accept, /* a do nothing */
  430. .getname = inet6_getname,
  431. .poll = udp_poll, /* ok */
  432. .ioctl = inet6_ioctl, /* must change */
  433. .listen = sock_no_listen, /* ok */
  434. .shutdown = inet_shutdown, /* ok */
  435. .setsockopt = sock_common_setsockopt, /* ok */
  436. .getsockopt = sock_common_getsockopt, /* ok */
  437. .sendmsg = inet_sendmsg, /* ok */
  438. .recvmsg = sock_common_recvmsg, /* ok */
  439. .mmap = sock_no_mmap,
  440. .sendpage = sock_no_sendpage,
  441. #ifdef CONFIG_COMPAT
  442. .compat_setsockopt = compat_sock_common_setsockopt,
  443. .compat_getsockopt = compat_sock_common_getsockopt,
  444. #endif
  445. };
  446. static struct net_proto_family inet6_family_ops = {
  447. .family = PF_INET6,
  448. .create = inet6_create,
  449. .owner = THIS_MODULE,
  450. };
  451. /* Same as inet6_dgram_ops, sans udp_poll. */
  452. static const struct proto_ops inet6_sockraw_ops = {
  453. .family = PF_INET6,
  454. .owner = THIS_MODULE,
  455. .release = inet6_release,
  456. .bind = inet6_bind,
  457. .connect = inet_dgram_connect, /* ok */
  458. .socketpair = sock_no_socketpair, /* a do nothing */
  459. .accept = sock_no_accept, /* a do nothing */
  460. .getname = inet6_getname,
  461. .poll = datagram_poll, /* ok */
  462. .ioctl = inet6_ioctl, /* must change */
  463. .listen = sock_no_listen, /* ok */
  464. .shutdown = inet_shutdown, /* ok */
  465. .setsockopt = sock_common_setsockopt, /* ok */
  466. .getsockopt = sock_common_getsockopt, /* ok */
  467. .sendmsg = inet_sendmsg, /* ok */
  468. .recvmsg = sock_common_recvmsg, /* ok */
  469. .mmap = sock_no_mmap,
  470. .sendpage = sock_no_sendpage,
  471. #ifdef CONFIG_COMPAT
  472. .compat_setsockopt = compat_sock_common_setsockopt,
  473. .compat_getsockopt = compat_sock_common_getsockopt,
  474. #endif
  475. };
  476. static struct inet_protosw rawv6_protosw = {
  477. .type = SOCK_RAW,
  478. .protocol = IPPROTO_IP, /* wild card */
  479. .prot = &rawv6_prot,
  480. .ops = &inet6_sockraw_ops,
  481. .capability = CAP_NET_RAW,
  482. .no_check = UDP_CSUM_DEFAULT,
  483. .flags = INET_PROTOSW_REUSE,
  484. };
  485. void
  486. inet6_register_protosw(struct inet_protosw *p)
  487. {
  488. struct list_head *lh;
  489. struct inet_protosw *answer;
  490. int protocol = p->protocol;
  491. struct list_head *last_perm;
  492. spin_lock_bh(&inetsw6_lock);
  493. if (p->type >= SOCK_MAX)
  494. goto out_illegal;
  495. /* If we are trying to override a permanent protocol, bail. */
  496. answer = NULL;
  497. last_perm = &inetsw6[p->type];
  498. list_for_each(lh, &inetsw6[p->type]) {
  499. answer = list_entry(lh, struct inet_protosw, list);
  500. /* Check only the non-wild match. */
  501. if (INET_PROTOSW_PERMANENT & answer->flags) {
  502. if (protocol == answer->protocol)
  503. break;
  504. last_perm = lh;
  505. }
  506. answer = NULL;
  507. }
  508. if (answer)
  509. goto out_permanent;
  510. /* Add the new entry after the last permanent entry if any, so that
  511. * the new entry does not override a permanent entry when matched with
  512. * a wild-card protocol. But it is allowed to override any existing
  513. * non-permanent entry. This means that when we remove this entry, the
  514. * system automatically returns to the old behavior.
  515. */
  516. list_add_rcu(&p->list, last_perm);
  517. out:
  518. spin_unlock_bh(&inetsw6_lock);
  519. return;
  520. out_permanent:
  521. printk(KERN_ERR "Attempt to override permanent protocol %d.\n",
  522. protocol);
  523. goto out;
  524. out_illegal:
  525. printk(KERN_ERR
  526. "Ignoring attempt to register invalid socket type %d.\n",
  527. p->type);
  528. goto out;
  529. }
  530. EXPORT_SYMBOL(inet6_register_protosw);
  531. void
  532. inet6_unregister_protosw(struct inet_protosw *p)
  533. {
  534. if (INET_PROTOSW_PERMANENT & p->flags) {
  535. printk(KERN_ERR
  536. "Attempt to unregister permanent protocol %d.\n",
  537. p->protocol);
  538. } else {
  539. spin_lock_bh(&inetsw6_lock);
  540. list_del_rcu(&p->list);
  541. spin_unlock_bh(&inetsw6_lock);
  542. synchronize_net();
  543. }
  544. }
  545. EXPORT_SYMBOL(inet6_unregister_protosw);
  546. int inet6_sk_rebuild_header(struct sock *sk)
  547. {
  548. int err;
  549. struct dst_entry *dst;
  550. struct ipv6_pinfo *np = inet6_sk(sk);
  551. dst = __sk_dst_check(sk, np->dst_cookie);
  552. if (dst == NULL) {
  553. struct inet_sock *inet = inet_sk(sk);
  554. struct in6_addr *final_p = NULL, final;
  555. struct flowi fl;
  556. memset(&fl, 0, sizeof(fl));
  557. fl.proto = sk->sk_protocol;
  558. ipv6_addr_copy(&fl.fl6_dst, &np->daddr);
  559. ipv6_addr_copy(&fl.fl6_src, &np->saddr);
  560. fl.fl6_flowlabel = np->flow_label;
  561. fl.oif = sk->sk_bound_dev_if;
  562. fl.fl_ip_dport = inet->dport;
  563. fl.fl_ip_sport = inet->sport;
  564. security_sk_classify_flow(sk, &fl);
  565. if (np->opt && np->opt->srcrt) {
  566. struct rt0_hdr *rt0 = (struct rt0_hdr *) np->opt->srcrt;
  567. ipv6_addr_copy(&final, &fl.fl6_dst);
  568. ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
  569. final_p = &final;
  570. }
  571. err = ip6_dst_lookup(sk, &dst, &fl);
  572. if (err) {
  573. sk->sk_route_caps = 0;
  574. return err;
  575. }
  576. if (final_p)
  577. ipv6_addr_copy(&fl.fl6_dst, final_p);
  578. if ((err = xfrm_lookup(&dst, &fl, sk, 0)) < 0) {
  579. sk->sk_err_soft = -err;
  580. return err;
  581. }
  582. __ip6_dst_store(sk, dst, NULL, NULL);
  583. }
  584. return 0;
  585. }
  586. EXPORT_SYMBOL_GPL(inet6_sk_rebuild_header);
  587. int ipv6_opt_accepted(struct sock *sk, struct sk_buff *skb)
  588. {
  589. struct ipv6_pinfo *np = inet6_sk(sk);
  590. struct inet6_skb_parm *opt = IP6CB(skb);
  591. if (np->rxopt.all) {
  592. if ((opt->hop && (np->rxopt.bits.hopopts ||
  593. np->rxopt.bits.ohopopts)) ||
  594. ((IPV6_FLOWINFO_MASK & *(__be32*)skb->nh.raw) &&
  595. np->rxopt.bits.rxflow) ||
  596. (opt->srcrt && (np->rxopt.bits.srcrt ||
  597. np->rxopt.bits.osrcrt)) ||
  598. ((opt->dst1 || opt->dst0) &&
  599. (np->rxopt.bits.dstopts || np->rxopt.bits.odstopts)))
  600. return 1;
  601. }
  602. return 0;
  603. }
  604. EXPORT_SYMBOL_GPL(ipv6_opt_accepted);
  605. int
  606. snmp6_mib_init(void *ptr[2], size_t mibsize, size_t mibalign)
  607. {
  608. if (ptr == NULL)
  609. return -EINVAL;
  610. ptr[0] = __alloc_percpu(mibsize);
  611. if (!ptr[0])
  612. goto err0;
  613. ptr[1] = __alloc_percpu(mibsize);
  614. if (!ptr[1])
  615. goto err1;
  616. return 0;
  617. err1:
  618. free_percpu(ptr[0]);
  619. ptr[0] = NULL;
  620. err0:
  621. return -ENOMEM;
  622. }
  623. void
  624. snmp6_mib_free(void *ptr[2])
  625. {
  626. if (ptr == NULL)
  627. return;
  628. free_percpu(ptr[0]);
  629. free_percpu(ptr[1]);
  630. ptr[0] = ptr[1] = NULL;
  631. }
  632. static int __init init_ipv6_mibs(void)
  633. {
  634. if (snmp6_mib_init((void **)ipv6_statistics, sizeof (struct ipstats_mib),
  635. __alignof__(struct ipstats_mib)) < 0)
  636. goto err_ip_mib;
  637. if (snmp6_mib_init((void **)icmpv6_statistics, sizeof (struct icmpv6_mib),
  638. __alignof__(struct icmpv6_mib)) < 0)
  639. goto err_icmp_mib;
  640. if (snmp6_mib_init((void **)udp_stats_in6, sizeof (struct udp_mib),
  641. __alignof__(struct udp_mib)) < 0)
  642. goto err_udp_mib;
  643. if (snmp6_mib_init((void **)udplite_stats_in6, sizeof (struct udp_mib),
  644. __alignof__(struct udp_mib)) < 0)
  645. goto err_udplite_mib;
  646. return 0;
  647. err_udplite_mib:
  648. snmp6_mib_free((void **)udp_stats_in6);
  649. err_udp_mib:
  650. snmp6_mib_free((void **)icmpv6_statistics);
  651. err_icmp_mib:
  652. snmp6_mib_free((void **)ipv6_statistics);
  653. err_ip_mib:
  654. return -ENOMEM;
  655. }
  656. static void cleanup_ipv6_mibs(void)
  657. {
  658. snmp6_mib_free((void **)ipv6_statistics);
  659. snmp6_mib_free((void **)icmpv6_statistics);
  660. snmp6_mib_free((void **)udp_stats_in6);
  661. snmp6_mib_free((void **)udplite_stats_in6);
  662. }
  663. static int __init inet6_init(void)
  664. {
  665. struct sk_buff *dummy_skb;
  666. struct list_head *r;
  667. int err;
  668. BUILD_BUG_ON(sizeof(struct inet6_skb_parm) > sizeof(dummy_skb->cb));
  669. #ifdef MODULE
  670. #if 0 /* FIXME --RR */
  671. if (!mod_member_present(&__this_module, can_unload))
  672. return -EINVAL;
  673. __this_module.can_unload = &ipv6_unload;
  674. #endif
  675. #endif
  676. err = proto_register(&tcpv6_prot, 1);
  677. if (err)
  678. goto out;
  679. err = proto_register(&udpv6_prot, 1);
  680. if (err)
  681. goto out_unregister_tcp_proto;
  682. err = proto_register(&udplitev6_prot, 1);
  683. if (err)
  684. goto out_unregister_udp_proto;
  685. err = proto_register(&rawv6_prot, 1);
  686. if (err)
  687. goto out_unregister_udplite_proto;
  688. /* Register the socket-side information for inet6_create. */
  689. for(r = &inetsw6[0]; r < &inetsw6[SOCK_MAX]; ++r)
  690. INIT_LIST_HEAD(r);
  691. /* We MUST register RAW sockets before we create the ICMP6,
  692. * IGMP6, or NDISC control sockets.
  693. */
  694. inet6_register_protosw(&rawv6_protosw);
  695. /* Register the family here so that the init calls below will
  696. * be able to create sockets. (?? is this dangerous ??)
  697. */
  698. err = sock_register(&inet6_family_ops);
  699. if (err)
  700. goto out_unregister_raw_proto;
  701. /* Initialise ipv6 mibs */
  702. err = init_ipv6_mibs();
  703. if (err)
  704. goto out_unregister_sock;
  705. /*
  706. * ipngwg API draft makes clear that the correct semantics
  707. * for TCP and UDP is to consider one TCP and UDP instance
  708. * in a host availiable by both INET and INET6 APIs and
  709. * able to communicate via both network protocols.
  710. */
  711. #ifdef CONFIG_SYSCTL
  712. ipv6_sysctl_register();
  713. #endif
  714. err = icmpv6_init(&inet6_family_ops);
  715. if (err)
  716. goto icmp_fail;
  717. err = ndisc_init(&inet6_family_ops);
  718. if (err)
  719. goto ndisc_fail;
  720. err = igmp6_init(&inet6_family_ops);
  721. if (err)
  722. goto igmp_fail;
  723. err = ipv6_netfilter_init();
  724. if (err)
  725. goto netfilter_fail;
  726. /* Create /proc/foo6 entries. */
  727. #ifdef CONFIG_PROC_FS
  728. err = -ENOMEM;
  729. if (raw6_proc_init())
  730. goto proc_raw6_fail;
  731. if (tcp6_proc_init())
  732. goto proc_tcp6_fail;
  733. if (udp6_proc_init())
  734. goto proc_udp6_fail;
  735. if (udplite6_proc_init())
  736. goto proc_udplite6_fail;
  737. if (ipv6_misc_proc_init())
  738. goto proc_misc6_fail;
  739. if (ac6_proc_init())
  740. goto proc_anycast6_fail;
  741. if (if6_proc_init())
  742. goto proc_if6_fail;
  743. #endif
  744. ip6_route_init();
  745. ip6_flowlabel_init();
  746. err = addrconf_init();
  747. if (err)
  748. goto addrconf_fail;
  749. /* Init v6 extension headers. */
  750. ipv6_rthdr_init();
  751. ipv6_frag_init();
  752. ipv6_nodata_init();
  753. ipv6_destopt_init();
  754. #ifdef CONFIG_IPV6_MIP6
  755. mip6_init();
  756. #endif
  757. /* Init v6 transport protocols. */
  758. udpv6_init();
  759. udplitev6_init();
  760. tcpv6_init();
  761. ipv6_packet_init();
  762. err = 0;
  763. out:
  764. return err;
  765. addrconf_fail:
  766. ip6_flowlabel_cleanup();
  767. ip6_route_cleanup();
  768. #ifdef CONFIG_PROC_FS
  769. if6_proc_exit();
  770. proc_if6_fail:
  771. ac6_proc_exit();
  772. proc_anycast6_fail:
  773. ipv6_misc_proc_exit();
  774. proc_misc6_fail:
  775. udplite6_proc_exit();
  776. proc_udplite6_fail:
  777. udp6_proc_exit();
  778. proc_udp6_fail:
  779. tcp6_proc_exit();
  780. proc_tcp6_fail:
  781. raw6_proc_exit();
  782. proc_raw6_fail:
  783. #endif
  784. ipv6_netfilter_fini();
  785. netfilter_fail:
  786. igmp6_cleanup();
  787. igmp_fail:
  788. ndisc_cleanup();
  789. ndisc_fail:
  790. icmpv6_cleanup();
  791. icmp_fail:
  792. #ifdef CONFIG_SYSCTL
  793. ipv6_sysctl_unregister();
  794. #endif
  795. cleanup_ipv6_mibs();
  796. out_unregister_sock:
  797. sock_unregister(PF_INET6);
  798. out_unregister_raw_proto:
  799. proto_unregister(&rawv6_prot);
  800. out_unregister_udplite_proto:
  801. proto_unregister(&udplitev6_prot);
  802. out_unregister_udp_proto:
  803. proto_unregister(&udpv6_prot);
  804. out_unregister_tcp_proto:
  805. proto_unregister(&tcpv6_prot);
  806. goto out;
  807. }
  808. module_init(inet6_init);
  809. static void __exit inet6_exit(void)
  810. {
  811. /* First of all disallow new sockets creation. */
  812. sock_unregister(PF_INET6);
  813. /* Cleanup code parts. */
  814. ipv6_packet_cleanup();
  815. #ifdef CONFIG_IPV6_MIP6
  816. mip6_fini();
  817. #endif
  818. addrconf_cleanup();
  819. ip6_flowlabel_cleanup();
  820. ip6_route_cleanup();
  821. #ifdef CONFIG_PROC_FS
  822. /* Cleanup code parts. */
  823. if6_proc_exit();
  824. ac6_proc_exit();
  825. ipv6_misc_proc_exit();
  826. udplite6_proc_exit();
  827. udp6_proc_exit();
  828. tcp6_proc_exit();
  829. raw6_proc_exit();
  830. #endif
  831. ipv6_netfilter_fini();
  832. igmp6_cleanup();
  833. ndisc_cleanup();
  834. icmpv6_cleanup();
  835. #ifdef CONFIG_SYSCTL
  836. ipv6_sysctl_unregister();
  837. #endif
  838. cleanup_ipv6_mibs();
  839. proto_unregister(&rawv6_prot);
  840. proto_unregister(&udplitev6_prot);
  841. proto_unregister(&udpv6_prot);
  842. proto_unregister(&tcpv6_prot);
  843. }
  844. module_exit(inet6_exit);
  845. MODULE_ALIAS_NETPROTO(PF_INET6);