af_inet6.c 25 KB

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