af_inet6.c 20 KB

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