af_inet6.c 20 KB

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