af_inet6.c 25 KB

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