af_inet6.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  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. * Fixes:
  11. * piggy, Karl Knutson : Socket protocol table
  12. * Hideaki YOSHIFUJI : sin6_scope_id support
  13. * Arnaldo Melo : check proc_net_create return, cleanups
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License
  17. * as published by the Free Software Foundation; either version
  18. * 2 of the License, or (at your option) any later version.
  19. */
  20. #define pr_fmt(fmt) "IPv6: " fmt
  21. #include <linux/module.h>
  22. #include <linux/capability.h>
  23. #include <linux/errno.h>
  24. #include <linux/types.h>
  25. #include <linux/socket.h>
  26. #include <linux/in.h>
  27. #include <linux/kernel.h>
  28. #include <linux/timer.h>
  29. #include <linux/string.h>
  30. #include <linux/sockios.h>
  31. #include <linux/net.h>
  32. #include <linux/fcntl.h>
  33. #include <linux/mm.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/proc_fs.h>
  36. #include <linux/stat.h>
  37. #include <linux/init.h>
  38. #include <linux/slab.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/route.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 <linux/mroute6.h>
  60. MODULE_AUTHOR("Cast of dozens");
  61. MODULE_DESCRIPTION("IPv6 protocol stack for Linux");
  62. MODULE_LICENSE("GPL");
  63. /* The inetsw6 table contains everything that inet6_create needs to
  64. * build a new socket.
  65. */
  66. static struct list_head inetsw6[SOCK_MAX];
  67. static DEFINE_SPINLOCK(inetsw6_lock);
  68. struct ipv6_params ipv6_defaults = {
  69. .disable_ipv6 = 0,
  70. .autoconf = 1,
  71. };
  72. static int disable_ipv6_mod;
  73. module_param_named(disable, disable_ipv6_mod, int, 0444);
  74. MODULE_PARM_DESC(disable, "Disable IPv6 module such that it is non-functional");
  75. module_param_named(disable_ipv6, ipv6_defaults.disable_ipv6, int, 0444);
  76. MODULE_PARM_DESC(disable_ipv6, "Disable IPv6 on all interfaces");
  77. module_param_named(autoconf, ipv6_defaults.autoconf, int, 0444);
  78. MODULE_PARM_DESC(autoconf, "Enable IPv6 address autoconfiguration on all interfaces");
  79. static __inline__ struct ipv6_pinfo *inet6_sk_generic(struct sock *sk)
  80. {
  81. const int offset = sk->sk_prot->obj_size - sizeof(struct ipv6_pinfo);
  82. return (struct ipv6_pinfo *)(((u8 *)sk) + offset);
  83. }
  84. static int inet6_create(struct net *net, struct socket *sock, int protocol,
  85. int kern)
  86. {
  87. struct inet_sock *inet;
  88. struct ipv6_pinfo *np;
  89. struct sock *sk;
  90. struct inet_protosw *answer;
  91. struct proto *answer_prot;
  92. unsigned char answer_flags;
  93. char answer_no_check;
  94. int try_loading_module = 0;
  95. int err;
  96. if (sock->type != SOCK_RAW &&
  97. sock->type != SOCK_DGRAM &&
  98. !inet_ehash_secret)
  99. build_ehash_secret();
  100. /* Look for the requested type/protocol pair. */
  101. lookup_protocol:
  102. err = -ESOCKTNOSUPPORT;
  103. rcu_read_lock();
  104. list_for_each_entry_rcu(answer, &inetsw6[sock->type], list) {
  105. err = 0;
  106. /* Check the non-wild match. */
  107. if (protocol == answer->protocol) {
  108. if (protocol != IPPROTO_IP)
  109. break;
  110. } else {
  111. /* Check for the two wild cases. */
  112. if (IPPROTO_IP == protocol) {
  113. protocol = answer->protocol;
  114. break;
  115. }
  116. if (IPPROTO_IP == answer->protocol)
  117. break;
  118. }
  119. err = -EPROTONOSUPPORT;
  120. }
  121. if (err) {
  122. if (try_loading_module < 2) {
  123. rcu_read_unlock();
  124. /*
  125. * Be more specific, e.g. net-pf-10-proto-132-type-1
  126. * (net-pf-PF_INET6-proto-IPPROTO_SCTP-type-SOCK_STREAM)
  127. */
  128. if (++try_loading_module == 1)
  129. request_module("net-pf-%d-proto-%d-type-%d",
  130. PF_INET6, protocol, sock->type);
  131. /*
  132. * Fall back to generic, e.g. net-pf-10-proto-132
  133. * (net-pf-PF_INET6-proto-IPPROTO_SCTP)
  134. */
  135. else
  136. request_module("net-pf-%d-proto-%d",
  137. PF_INET6, protocol);
  138. goto lookup_protocol;
  139. } else
  140. goto out_rcu_unlock;
  141. }
  142. err = -EPERM;
  143. if (sock->type == SOCK_RAW && !kern &&
  144. !ns_capable(net->user_ns, CAP_NET_RAW))
  145. goto out_rcu_unlock;
  146. sock->ops = answer->ops;
  147. answer_prot = answer->prot;
  148. answer_no_check = answer->no_check;
  149. answer_flags = answer->flags;
  150. rcu_read_unlock();
  151. WARN_ON(answer_prot->slab == NULL);
  152. err = -ENOBUFS;
  153. sk = sk_alloc(net, PF_INET6, GFP_KERNEL, answer_prot);
  154. if (sk == NULL)
  155. goto out;
  156. sock_init_data(sock, sk);
  157. err = 0;
  158. sk->sk_no_check = answer_no_check;
  159. if (INET_PROTOSW_REUSE & answer_flags)
  160. sk->sk_reuse = SK_CAN_REUSE;
  161. inet = inet_sk(sk);
  162. inet->is_icsk = (INET_PROTOSW_ICSK & answer_flags) != 0;
  163. if (SOCK_RAW == sock->type) {
  164. inet->inet_num = protocol;
  165. if (IPPROTO_RAW == protocol)
  166. inet->hdrincl = 1;
  167. }
  168. sk->sk_destruct = inet_sock_destruct;
  169. sk->sk_family = PF_INET6;
  170. sk->sk_protocol = protocol;
  171. sk->sk_backlog_rcv = answer->prot->backlog_rcv;
  172. inet_sk(sk)->pinet6 = np = inet6_sk_generic(sk);
  173. np->hop_limit = -1;
  174. np->mcast_hops = IPV6_DEFAULT_MCASTHOPS;
  175. np->mc_loop = 1;
  176. np->pmtudisc = IPV6_PMTUDISC_WANT;
  177. np->ipv6only = net->ipv6.sysctl.bindv6only;
  178. /* Init the ipv4 part of the socket since we can have sockets
  179. * using v6 API for ipv4.
  180. */
  181. inet->uc_ttl = -1;
  182. inet->mc_loop = 1;
  183. inet->mc_ttl = 1;
  184. inet->mc_index = 0;
  185. inet->mc_list = NULL;
  186. inet->rcv_tos = 0;
  187. if (ipv4_config.no_pmtu_disc)
  188. inet->pmtudisc = IP_PMTUDISC_DONT;
  189. else
  190. inet->pmtudisc = IP_PMTUDISC_WANT;
  191. /*
  192. * Increment only the relevant sk_prot->socks debug field, this changes
  193. * the previous behaviour of incrementing both the equivalent to
  194. * answer->prot->socks (inet6_sock_nr) and inet_sock_nr.
  195. *
  196. * This allows better debug granularity as we'll know exactly how many
  197. * UDPv6, TCPv6, etc socks were allocated, not the sum of all IPv6
  198. * transport protocol socks. -acme
  199. */
  200. sk_refcnt_debug_inc(sk);
  201. if (inet->inet_num) {
  202. /* It assumes that any protocol which allows
  203. * the user to assign a number at socket
  204. * creation time automatically shares.
  205. */
  206. inet->inet_sport = htons(inet->inet_num);
  207. sk->sk_prot->hash(sk);
  208. }
  209. if (sk->sk_prot->init) {
  210. err = sk->sk_prot->init(sk);
  211. if (err) {
  212. sk_common_release(sk);
  213. goto out;
  214. }
  215. }
  216. out:
  217. return err;
  218. out_rcu_unlock:
  219. rcu_read_unlock();
  220. goto out;
  221. }
  222. /* bind for INET6 API */
  223. int inet6_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
  224. {
  225. struct sockaddr_in6 *addr = (struct sockaddr_in6 *)uaddr;
  226. struct sock *sk = sock->sk;
  227. struct inet_sock *inet = inet_sk(sk);
  228. struct ipv6_pinfo *np = inet6_sk(sk);
  229. struct net *net = sock_net(sk);
  230. __be32 v4addr = 0;
  231. unsigned short snum;
  232. int addr_type = 0;
  233. int err = 0;
  234. /* If the socket has its own bind function then use it. */
  235. if (sk->sk_prot->bind)
  236. return sk->sk_prot->bind(sk, uaddr, addr_len);
  237. if (addr_len < SIN6_LEN_RFC2133)
  238. return -EINVAL;
  239. if (addr->sin6_family != AF_INET6)
  240. return -EAFNOSUPPORT;
  241. addr_type = ipv6_addr_type(&addr->sin6_addr);
  242. if ((addr_type & IPV6_ADDR_MULTICAST) && sock->type == SOCK_STREAM)
  243. return -EINVAL;
  244. snum = ntohs(addr->sin6_port);
  245. if (snum && snum < PROT_SOCK && !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
  246. return -EACCES;
  247. lock_sock(sk);
  248. /* Check these errors (active socket, double bind). */
  249. if (sk->sk_state != TCP_CLOSE || inet->inet_num) {
  250. err = -EINVAL;
  251. goto out;
  252. }
  253. /* Check if the address belongs to the host. */
  254. if (addr_type == IPV6_ADDR_MAPPED) {
  255. int chk_addr_ret;
  256. /* Binding to v4-mapped address on a v6-only socket
  257. * makes no sense
  258. */
  259. if (np->ipv6only) {
  260. err = -EINVAL;
  261. goto out;
  262. }
  263. /* Reproduce AF_INET checks to make the bindings consistent */
  264. v4addr = addr->sin6_addr.s6_addr32[3];
  265. chk_addr_ret = inet_addr_type(net, v4addr);
  266. if (!sysctl_ip_nonlocal_bind &&
  267. !(inet->freebind || inet->transparent) &&
  268. v4addr != htonl(INADDR_ANY) &&
  269. chk_addr_ret != RTN_LOCAL &&
  270. chk_addr_ret != RTN_MULTICAST &&
  271. chk_addr_ret != RTN_BROADCAST) {
  272. err = -EADDRNOTAVAIL;
  273. goto out;
  274. }
  275. } else {
  276. if (addr_type != IPV6_ADDR_ANY) {
  277. struct net_device *dev = NULL;
  278. rcu_read_lock();
  279. if (addr_type & IPV6_ADDR_LINKLOCAL) {
  280. if (addr_len >= sizeof(struct sockaddr_in6) &&
  281. addr->sin6_scope_id) {
  282. /* Override any existing binding, if another one
  283. * is supplied by user.
  284. */
  285. sk->sk_bound_dev_if = addr->sin6_scope_id;
  286. }
  287. /* Binding to link-local address requires an interface */
  288. if (!sk->sk_bound_dev_if) {
  289. err = -EINVAL;
  290. goto out_unlock;
  291. }
  292. dev = dev_get_by_index_rcu(net, sk->sk_bound_dev_if);
  293. if (!dev) {
  294. err = -ENODEV;
  295. goto out_unlock;
  296. }
  297. }
  298. /* ipv4 addr of the socket is invalid. Only the
  299. * unspecified and mapped address have a v4 equivalent.
  300. */
  301. v4addr = LOOPBACK4_IPV6;
  302. if (!(addr_type & IPV6_ADDR_MULTICAST)) {
  303. if (!(inet->freebind || inet->transparent) &&
  304. !ipv6_chk_addr(net, &addr->sin6_addr,
  305. dev, 0)) {
  306. err = -EADDRNOTAVAIL;
  307. goto out_unlock;
  308. }
  309. }
  310. rcu_read_unlock();
  311. }
  312. }
  313. inet->inet_rcv_saddr = v4addr;
  314. inet->inet_saddr = v4addr;
  315. np->rcv_saddr = addr->sin6_addr;
  316. if (!(addr_type & IPV6_ADDR_MULTICAST))
  317. np->saddr = addr->sin6_addr;
  318. /* Make sure we are allowed to bind here. */
  319. if (sk->sk_prot->get_port(sk, snum)) {
  320. inet_reset_saddr(sk);
  321. err = -EADDRINUSE;
  322. goto out;
  323. }
  324. if (addr_type != IPV6_ADDR_ANY) {
  325. sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
  326. if (addr_type != IPV6_ADDR_MAPPED)
  327. np->ipv6only = 1;
  328. }
  329. if (snum)
  330. sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
  331. inet->inet_sport = htons(inet->inet_num);
  332. inet->inet_dport = 0;
  333. inet->inet_daddr = 0;
  334. out:
  335. release_sock(sk);
  336. return err;
  337. out_unlock:
  338. rcu_read_unlock();
  339. goto out;
  340. }
  341. EXPORT_SYMBOL(inet6_bind);
  342. int inet6_release(struct socket *sock)
  343. {
  344. struct sock *sk = sock->sk;
  345. if (sk == NULL)
  346. return -EINVAL;
  347. /* Free mc lists */
  348. ipv6_sock_mc_close(sk);
  349. /* Free ac lists */
  350. ipv6_sock_ac_close(sk);
  351. return inet_release(sock);
  352. }
  353. EXPORT_SYMBOL(inet6_release);
  354. void inet6_destroy_sock(struct sock *sk)
  355. {
  356. struct ipv6_pinfo *np = inet6_sk(sk);
  357. struct sk_buff *skb;
  358. struct ipv6_txoptions *opt;
  359. /* Release rx options */
  360. skb = xchg(&np->pktoptions, NULL);
  361. if (skb != NULL)
  362. kfree_skb(skb);
  363. skb = xchg(&np->rxpmtu, NULL);
  364. if (skb != NULL)
  365. kfree_skb(skb);
  366. /* Free flowlabels */
  367. fl6_free_socklist(sk);
  368. /* Free tx options */
  369. opt = xchg(&np->opt, NULL);
  370. if (opt != NULL)
  371. sock_kfree_s(sk, opt, opt->tot_len);
  372. }
  373. EXPORT_SYMBOL_GPL(inet6_destroy_sock);
  374. /*
  375. * This does both peername and sockname.
  376. */
  377. int inet6_getname(struct socket *sock, struct sockaddr *uaddr,
  378. int *uaddr_len, int peer)
  379. {
  380. struct sockaddr_in6 *sin = (struct sockaddr_in6 *)uaddr;
  381. struct sock *sk = sock->sk;
  382. struct inet_sock *inet = inet_sk(sk);
  383. struct ipv6_pinfo *np = inet6_sk(sk);
  384. sin->sin6_family = AF_INET6;
  385. sin->sin6_flowinfo = 0;
  386. sin->sin6_scope_id = 0;
  387. if (peer) {
  388. if (!inet->inet_dport)
  389. return -ENOTCONN;
  390. if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
  391. peer == 1)
  392. return -ENOTCONN;
  393. sin->sin6_port = inet->inet_dport;
  394. sin->sin6_addr = np->daddr;
  395. if (np->sndflow)
  396. sin->sin6_flowinfo = np->flow_label;
  397. } else {
  398. if (ipv6_addr_any(&np->rcv_saddr))
  399. sin->sin6_addr = np->saddr;
  400. else
  401. sin->sin6_addr = np->rcv_saddr;
  402. sin->sin6_port = inet->inet_sport;
  403. }
  404. if (ipv6_addr_type(&sin->sin6_addr) & IPV6_ADDR_LINKLOCAL)
  405. sin->sin6_scope_id = sk->sk_bound_dev_if;
  406. *uaddr_len = sizeof(*sin);
  407. return 0;
  408. }
  409. EXPORT_SYMBOL(inet6_getname);
  410. int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  411. {
  412. struct sock *sk = sock->sk;
  413. struct net *net = sock_net(sk);
  414. switch (cmd) {
  415. case SIOCGSTAMP:
  416. return sock_get_timestamp(sk, (struct timeval __user *)arg);
  417. case SIOCGSTAMPNS:
  418. return sock_get_timestampns(sk, (struct timespec __user *)arg);
  419. case SIOCADDRT:
  420. case SIOCDELRT:
  421. return ipv6_route_ioctl(net, cmd, (void __user *)arg);
  422. case SIOCSIFADDR:
  423. return addrconf_add_ifaddr(net, (void __user *) arg);
  424. case SIOCDIFADDR:
  425. return addrconf_del_ifaddr(net, (void __user *) arg);
  426. case SIOCSIFDSTADDR:
  427. return addrconf_set_dstaddr(net, (void __user *) arg);
  428. default:
  429. if (!sk->sk_prot->ioctl)
  430. return -ENOIOCTLCMD;
  431. return sk->sk_prot->ioctl(sk, cmd, arg);
  432. }
  433. /*NOTREACHED*/
  434. return 0;
  435. }
  436. EXPORT_SYMBOL(inet6_ioctl);
  437. const struct proto_ops inet6_stream_ops = {
  438. .family = PF_INET6,
  439. .owner = THIS_MODULE,
  440. .release = inet6_release,
  441. .bind = inet6_bind,
  442. .connect = inet_stream_connect, /* ok */
  443. .socketpair = sock_no_socketpair, /* a do nothing */
  444. .accept = inet_accept, /* ok */
  445. .getname = inet6_getname,
  446. .poll = tcp_poll, /* ok */
  447. .ioctl = inet6_ioctl, /* must change */
  448. .listen = inet_listen, /* ok */
  449. .shutdown = inet_shutdown, /* ok */
  450. .setsockopt = sock_common_setsockopt, /* ok */
  451. .getsockopt = sock_common_getsockopt, /* ok */
  452. .sendmsg = inet_sendmsg, /* ok */
  453. .recvmsg = inet_recvmsg, /* ok */
  454. .mmap = sock_no_mmap,
  455. .sendpage = inet_sendpage,
  456. .splice_read = tcp_splice_read,
  457. #ifdef CONFIG_COMPAT
  458. .compat_setsockopt = compat_sock_common_setsockopt,
  459. .compat_getsockopt = compat_sock_common_getsockopt,
  460. #endif
  461. };
  462. const struct proto_ops inet6_dgram_ops = {
  463. .family = PF_INET6,
  464. .owner = THIS_MODULE,
  465. .release = inet6_release,
  466. .bind = inet6_bind,
  467. .connect = inet_dgram_connect, /* ok */
  468. .socketpair = sock_no_socketpair, /* a do nothing */
  469. .accept = sock_no_accept, /* a do nothing */
  470. .getname = inet6_getname,
  471. .poll = udp_poll, /* ok */
  472. .ioctl = inet6_ioctl, /* must change */
  473. .listen = sock_no_listen, /* ok */
  474. .shutdown = inet_shutdown, /* ok */
  475. .setsockopt = sock_common_setsockopt, /* ok */
  476. .getsockopt = sock_common_getsockopt, /* ok */
  477. .sendmsg = inet_sendmsg, /* ok */
  478. .recvmsg = inet_recvmsg, /* ok */
  479. .mmap = sock_no_mmap,
  480. .sendpage = sock_no_sendpage,
  481. #ifdef CONFIG_COMPAT
  482. .compat_setsockopt = compat_sock_common_setsockopt,
  483. .compat_getsockopt = compat_sock_common_getsockopt,
  484. #endif
  485. };
  486. static const struct net_proto_family inet6_family_ops = {
  487. .family = PF_INET6,
  488. .create = inet6_create,
  489. .owner = THIS_MODULE,
  490. };
  491. int inet6_register_protosw(struct inet_protosw *p)
  492. {
  493. struct list_head *lh;
  494. struct inet_protosw *answer;
  495. struct list_head *last_perm;
  496. int protocol = p->protocol;
  497. int ret;
  498. spin_lock_bh(&inetsw6_lock);
  499. ret = -EINVAL;
  500. if (p->type >= SOCK_MAX)
  501. goto out_illegal;
  502. /* If we are trying to override a permanent protocol, bail. */
  503. answer = NULL;
  504. ret = -EPERM;
  505. last_perm = &inetsw6[p->type];
  506. list_for_each(lh, &inetsw6[p->type]) {
  507. answer = list_entry(lh, struct inet_protosw, list);
  508. /* Check only the non-wild match. */
  509. if (INET_PROTOSW_PERMANENT & answer->flags) {
  510. if (protocol == answer->protocol)
  511. break;
  512. last_perm = lh;
  513. }
  514. answer = NULL;
  515. }
  516. if (answer)
  517. goto out_permanent;
  518. /* Add the new entry after the last permanent entry if any, so that
  519. * the new entry does not override a permanent entry when matched with
  520. * a wild-card protocol. But it is allowed to override any existing
  521. * non-permanent entry. This means that when we remove this entry, the
  522. * system automatically returns to the old behavior.
  523. */
  524. list_add_rcu(&p->list, last_perm);
  525. ret = 0;
  526. out:
  527. spin_unlock_bh(&inetsw6_lock);
  528. return ret;
  529. out_permanent:
  530. pr_err("Attempt to override permanent protocol %d\n", protocol);
  531. goto out;
  532. out_illegal:
  533. pr_err("Ignoring attempt to register invalid socket type %d\n",
  534. p->type);
  535. goto out;
  536. }
  537. EXPORT_SYMBOL(inet6_register_protosw);
  538. void
  539. inet6_unregister_protosw(struct inet_protosw *p)
  540. {
  541. if (INET_PROTOSW_PERMANENT & p->flags) {
  542. pr_err("Attempt to unregister permanent protocol %d\n",
  543. p->protocol);
  544. } else {
  545. spin_lock_bh(&inetsw6_lock);
  546. list_del_rcu(&p->list);
  547. spin_unlock_bh(&inetsw6_lock);
  548. synchronize_net();
  549. }
  550. }
  551. EXPORT_SYMBOL(inet6_unregister_protosw);
  552. int inet6_sk_rebuild_header(struct sock *sk)
  553. {
  554. struct ipv6_pinfo *np = inet6_sk(sk);
  555. struct dst_entry *dst;
  556. dst = __sk_dst_check(sk, np->dst_cookie);
  557. if (dst == NULL) {
  558. struct inet_sock *inet = inet_sk(sk);
  559. struct in6_addr *final_p, final;
  560. struct flowi6 fl6;
  561. memset(&fl6, 0, sizeof(fl6));
  562. fl6.flowi6_proto = sk->sk_protocol;
  563. fl6.daddr = np->daddr;
  564. fl6.saddr = np->saddr;
  565. fl6.flowlabel = np->flow_label;
  566. fl6.flowi6_oif = sk->sk_bound_dev_if;
  567. fl6.flowi6_mark = sk->sk_mark;
  568. fl6.fl6_dport = inet->inet_dport;
  569. fl6.fl6_sport = inet->inet_sport;
  570. security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
  571. final_p = fl6_update_dst(&fl6, np->opt, &final);
  572. dst = ip6_dst_lookup_flow(sk, &fl6, final_p, false);
  573. if (IS_ERR(dst)) {
  574. sk->sk_route_caps = 0;
  575. sk->sk_err_soft = -PTR_ERR(dst);
  576. return PTR_ERR(dst);
  577. }
  578. __ip6_dst_store(sk, dst, NULL, NULL);
  579. }
  580. return 0;
  581. }
  582. EXPORT_SYMBOL_GPL(inet6_sk_rebuild_header);
  583. bool ipv6_opt_accepted(const struct sock *sk, const struct sk_buff *skb)
  584. {
  585. const struct ipv6_pinfo *np = inet6_sk(sk);
  586. const struct inet6_skb_parm *opt = IP6CB(skb);
  587. if (np->rxopt.all) {
  588. if ((opt->hop && (np->rxopt.bits.hopopts ||
  589. np->rxopt.bits.ohopopts)) ||
  590. ((IPV6_FLOWINFO_MASK &
  591. *(__be32 *)skb_network_header(skb)) &&
  592. np->rxopt.bits.rxflow) ||
  593. (opt->srcrt && (np->rxopt.bits.srcrt ||
  594. np->rxopt.bits.osrcrt)) ||
  595. ((opt->dst1 || opt->dst0) &&
  596. (np->rxopt.bits.dstopts || np->rxopt.bits.odstopts)))
  597. return true;
  598. }
  599. return false;
  600. }
  601. EXPORT_SYMBOL_GPL(ipv6_opt_accepted);
  602. static struct packet_type ipv6_packet_type __read_mostly = {
  603. .type = cpu_to_be16(ETH_P_IPV6),
  604. .func = ipv6_rcv,
  605. };
  606. static int __init ipv6_packet_init(void)
  607. {
  608. dev_add_pack(&ipv6_packet_type);
  609. return 0;
  610. }
  611. static void ipv6_packet_cleanup(void)
  612. {
  613. dev_remove_pack(&ipv6_packet_type);
  614. }
  615. static int __net_init ipv6_init_mibs(struct net *net)
  616. {
  617. if (snmp_mib_init((void __percpu **)net->mib.udp_stats_in6,
  618. sizeof(struct udp_mib),
  619. __alignof__(struct udp_mib)) < 0)
  620. return -ENOMEM;
  621. if (snmp_mib_init((void __percpu **)net->mib.udplite_stats_in6,
  622. sizeof(struct udp_mib),
  623. __alignof__(struct udp_mib)) < 0)
  624. goto err_udplite_mib;
  625. if (snmp_mib_init((void __percpu **)net->mib.ipv6_statistics,
  626. sizeof(struct ipstats_mib),
  627. __alignof__(struct ipstats_mib)) < 0)
  628. goto err_ip_mib;
  629. if (snmp_mib_init((void __percpu **)net->mib.icmpv6_statistics,
  630. sizeof(struct icmpv6_mib),
  631. __alignof__(struct icmpv6_mib)) < 0)
  632. goto err_icmp_mib;
  633. net->mib.icmpv6msg_statistics = kzalloc(sizeof(struct icmpv6msg_mib),
  634. GFP_KERNEL);
  635. if (!net->mib.icmpv6msg_statistics)
  636. goto err_icmpmsg_mib;
  637. return 0;
  638. err_icmpmsg_mib:
  639. snmp_mib_free((void __percpu **)net->mib.icmpv6_statistics);
  640. err_icmp_mib:
  641. snmp_mib_free((void __percpu **)net->mib.ipv6_statistics);
  642. err_ip_mib:
  643. snmp_mib_free((void __percpu **)net->mib.udplite_stats_in6);
  644. err_udplite_mib:
  645. snmp_mib_free((void __percpu **)net->mib.udp_stats_in6);
  646. return -ENOMEM;
  647. }
  648. static void ipv6_cleanup_mibs(struct net *net)
  649. {
  650. snmp_mib_free((void __percpu **)net->mib.udp_stats_in6);
  651. snmp_mib_free((void __percpu **)net->mib.udplite_stats_in6);
  652. snmp_mib_free((void __percpu **)net->mib.ipv6_statistics);
  653. snmp_mib_free((void __percpu **)net->mib.icmpv6_statistics);
  654. kfree(net->mib.icmpv6msg_statistics);
  655. }
  656. static int __net_init inet6_net_init(struct net *net)
  657. {
  658. int err = 0;
  659. net->ipv6.sysctl.bindv6only = 0;
  660. net->ipv6.sysctl.icmpv6_time = 1*HZ;
  661. err = ipv6_init_mibs(net);
  662. if (err)
  663. return err;
  664. #ifdef CONFIG_PROC_FS
  665. err = udp6_proc_init(net);
  666. if (err)
  667. goto out;
  668. err = tcp6_proc_init(net);
  669. if (err)
  670. goto proc_tcp6_fail;
  671. err = ac6_proc_init(net);
  672. if (err)
  673. goto proc_ac6_fail;
  674. #endif
  675. return err;
  676. #ifdef CONFIG_PROC_FS
  677. proc_ac6_fail:
  678. tcp6_proc_exit(net);
  679. proc_tcp6_fail:
  680. udp6_proc_exit(net);
  681. out:
  682. ipv6_cleanup_mibs(net);
  683. return err;
  684. #endif
  685. }
  686. static void __net_exit inet6_net_exit(struct net *net)
  687. {
  688. #ifdef CONFIG_PROC_FS
  689. udp6_proc_exit(net);
  690. tcp6_proc_exit(net);
  691. ac6_proc_exit(net);
  692. #endif
  693. ipv6_cleanup_mibs(net);
  694. }
  695. static struct pernet_operations inet6_net_ops = {
  696. .init = inet6_net_init,
  697. .exit = inet6_net_exit,
  698. };
  699. static int __init inet6_init(void)
  700. {
  701. struct list_head *r;
  702. int err = 0;
  703. BUILD_BUG_ON(sizeof(struct inet6_skb_parm) > FIELD_SIZEOF(struct sk_buff, cb));
  704. /* Register the socket-side information for inet6_create. */
  705. for (r = &inetsw6[0]; r < &inetsw6[SOCK_MAX]; ++r)
  706. INIT_LIST_HEAD(r);
  707. if (disable_ipv6_mod) {
  708. pr_info("Loaded, but administratively disabled, reboot required to enable\n");
  709. goto out;
  710. }
  711. err = proto_register(&tcpv6_prot, 1);
  712. if (err)
  713. goto out;
  714. err = proto_register(&udpv6_prot, 1);
  715. if (err)
  716. goto out_unregister_tcp_proto;
  717. err = proto_register(&udplitev6_prot, 1);
  718. if (err)
  719. goto out_unregister_udp_proto;
  720. err = proto_register(&rawv6_prot, 1);
  721. if (err)
  722. goto out_unregister_udplite_proto;
  723. /* We MUST register RAW sockets before we create the ICMP6,
  724. * IGMP6, or NDISC control sockets.
  725. */
  726. err = rawv6_init();
  727. if (err)
  728. goto out_unregister_raw_proto;
  729. /* Register the family here so that the init calls below will
  730. * be able to create sockets. (?? is this dangerous ??)
  731. */
  732. err = sock_register(&inet6_family_ops);
  733. if (err)
  734. goto out_sock_register_fail;
  735. tcpv6_prot.sysctl_mem = init_net.ipv4.sysctl_tcp_mem;
  736. /*
  737. * ipngwg API draft makes clear that the correct semantics
  738. * for TCP and UDP is to consider one TCP and UDP instance
  739. * in a host available by both INET and INET6 APIs and
  740. * able to communicate via both network protocols.
  741. */
  742. err = register_pernet_subsys(&inet6_net_ops);
  743. if (err)
  744. goto register_pernet_fail;
  745. err = icmpv6_init();
  746. if (err)
  747. goto icmp_fail;
  748. err = ip6_mr_init();
  749. if (err)
  750. goto ipmr_fail;
  751. err = ndisc_init();
  752. if (err)
  753. goto ndisc_fail;
  754. err = igmp6_init();
  755. if (err)
  756. goto igmp_fail;
  757. err = ipv6_netfilter_init();
  758. if (err)
  759. goto netfilter_fail;
  760. /* Create /proc/foo6 entries. */
  761. #ifdef CONFIG_PROC_FS
  762. err = -ENOMEM;
  763. if (raw6_proc_init())
  764. goto proc_raw6_fail;
  765. if (udplite6_proc_init())
  766. goto proc_udplite6_fail;
  767. if (ipv6_misc_proc_init())
  768. goto proc_misc6_fail;
  769. if (if6_proc_init())
  770. goto proc_if6_fail;
  771. #endif
  772. err = ip6_route_init();
  773. if (err)
  774. goto ip6_route_fail;
  775. err = ip6_flowlabel_init();
  776. if (err)
  777. goto ip6_flowlabel_fail;
  778. err = addrconf_init();
  779. if (err)
  780. goto addrconf_fail;
  781. /* Init v6 extension headers. */
  782. err = ipv6_exthdrs_init();
  783. if (err)
  784. goto ipv6_exthdrs_fail;
  785. err = ipv6_frag_init();
  786. if (err)
  787. goto ipv6_frag_fail;
  788. /* Init v6 transport protocols. */
  789. err = udpv6_init();
  790. if (err)
  791. goto udpv6_fail;
  792. err = udplitev6_init();
  793. if (err)
  794. goto udplitev6_fail;
  795. err = tcpv6_init();
  796. if (err)
  797. goto tcpv6_fail;
  798. err = ipv6_packet_init();
  799. if (err)
  800. goto ipv6_packet_fail;
  801. #ifdef CONFIG_SYSCTL
  802. err = ipv6_sysctl_register();
  803. if (err)
  804. goto sysctl_fail;
  805. #endif
  806. out:
  807. return err;
  808. #ifdef CONFIG_SYSCTL
  809. sysctl_fail:
  810. ipv6_packet_cleanup();
  811. #endif
  812. ipv6_packet_fail:
  813. tcpv6_exit();
  814. tcpv6_fail:
  815. udplitev6_exit();
  816. udplitev6_fail:
  817. udpv6_exit();
  818. udpv6_fail:
  819. ipv6_frag_exit();
  820. ipv6_frag_fail:
  821. ipv6_exthdrs_exit();
  822. ipv6_exthdrs_fail:
  823. addrconf_cleanup();
  824. addrconf_fail:
  825. ip6_flowlabel_cleanup();
  826. ip6_flowlabel_fail:
  827. ip6_route_cleanup();
  828. ip6_route_fail:
  829. #ifdef CONFIG_PROC_FS
  830. if6_proc_exit();
  831. proc_if6_fail:
  832. ipv6_misc_proc_exit();
  833. proc_misc6_fail:
  834. udplite6_proc_exit();
  835. proc_udplite6_fail:
  836. raw6_proc_exit();
  837. proc_raw6_fail:
  838. #endif
  839. ipv6_netfilter_fini();
  840. netfilter_fail:
  841. igmp6_cleanup();
  842. igmp_fail:
  843. ndisc_cleanup();
  844. ndisc_fail:
  845. ip6_mr_cleanup();
  846. ipmr_fail:
  847. icmpv6_cleanup();
  848. icmp_fail:
  849. unregister_pernet_subsys(&inet6_net_ops);
  850. register_pernet_fail:
  851. sock_unregister(PF_INET6);
  852. rtnl_unregister_all(PF_INET6);
  853. out_sock_register_fail:
  854. rawv6_exit();
  855. out_unregister_raw_proto:
  856. proto_unregister(&rawv6_prot);
  857. out_unregister_udplite_proto:
  858. proto_unregister(&udplitev6_prot);
  859. out_unregister_udp_proto:
  860. proto_unregister(&udpv6_prot);
  861. out_unregister_tcp_proto:
  862. proto_unregister(&tcpv6_prot);
  863. goto out;
  864. }
  865. module_init(inet6_init);
  866. static void __exit inet6_exit(void)
  867. {
  868. if (disable_ipv6_mod)
  869. return;
  870. /* First of all disallow new sockets creation. */
  871. sock_unregister(PF_INET6);
  872. /* Disallow any further netlink messages */
  873. rtnl_unregister_all(PF_INET6);
  874. udpv6_exit();
  875. udplitev6_exit();
  876. tcpv6_exit();
  877. /* Cleanup code parts. */
  878. ipv6_packet_cleanup();
  879. ipv6_frag_exit();
  880. ipv6_exthdrs_exit();
  881. addrconf_cleanup();
  882. ip6_flowlabel_cleanup();
  883. ip6_route_cleanup();
  884. #ifdef CONFIG_PROC_FS
  885. /* Cleanup code parts. */
  886. if6_proc_exit();
  887. ipv6_misc_proc_exit();
  888. udplite6_proc_exit();
  889. raw6_proc_exit();
  890. #endif
  891. ipv6_netfilter_fini();
  892. igmp6_cleanup();
  893. ndisc_cleanup();
  894. ip6_mr_cleanup();
  895. icmpv6_cleanup();
  896. rawv6_exit();
  897. unregister_pernet_subsys(&inet6_net_ops);
  898. proto_unregister(&rawv6_prot);
  899. proto_unregister(&udplitev6_prot);
  900. proto_unregister(&udpv6_prot);
  901. proto_unregister(&tcpv6_prot);
  902. rcu_barrier(); /* Wait for completion of call_rcu()'s */
  903. }
  904. module_exit(inet6_exit);
  905. MODULE_ALIAS_NETPROTO(PF_INET6);