af_inet6.c 27 KB

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