af_inet6.c 26 KB

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