af_inet6.c 25 KB

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