af_inet6.c 19 KB

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