af_inet6.c 25 KB

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