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