af_inet6.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292
  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. #include <linux/module.h>
  21. #include <linux/capability.h>
  22. #include <linux/errno.h>
  23. #include <linux/types.h>
  24. #include <linux/socket.h>
  25. #include <linux/in.h>
  26. #include <linux/kernel.h>
  27. #include <linux/timer.h>
  28. #include <linux/string.h>
  29. #include <linux/sockios.h>
  30. #include <linux/net.h>
  31. #include <linux/fcntl.h>
  32. #include <linux/mm.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/proc_fs.h>
  35. #include <linux/stat.h>
  36. #include <linux/init.h>
  37. #include <linux/inet.h>
  38. #include <linux/netdevice.h>
  39. #include <linux/icmpv6.h>
  40. #include <linux/netfilter_ipv6.h>
  41. #include <net/ip.h>
  42. #include <net/ipv6.h>
  43. #include <net/udp.h>
  44. #include <net/udplite.h>
  45. #include <net/tcp.h>
  46. #include <net/ipip.h>
  47. #include <net/protocol.h>
  48. #include <net/inet_common.h>
  49. #include <net/route.h>
  50. #include <net/transp_v6.h>
  51. #include <net/ip6_route.h>
  52. #include <net/addrconf.h>
  53. #ifdef CONFIG_IPV6_TUNNEL
  54. #include <net/ip6_tunnel.h>
  55. #endif
  56. #include <asm/uaccess.h>
  57. #include <asm/system.h>
  58. #include <linux/mroute6.h>
  59. MODULE_AUTHOR("Cast of dozens");
  60. MODULE_DESCRIPTION("IPv6 protocol stack for Linux");
  61. MODULE_LICENSE("GPL");
  62. /* The inetsw6 table contains everything that inet6_create needs to
  63. * build a new socket.
  64. */
  65. static struct list_head inetsw6[SOCK_MAX];
  66. static DEFINE_SPINLOCK(inetsw6_lock);
  67. struct ipv6_params ipv6_defaults = {
  68. .disable_ipv6 = 0,
  69. .autoconf = 1,
  70. };
  71. static int disable_ipv6_mod = 0;
  72. module_param_named(disable, disable_ipv6_mod, int, 0444);
  73. MODULE_PARM_DESC(disable, "Disable IPv6 module such that it is non-functional");
  74. module_param_named(disable_ipv6, ipv6_defaults.disable_ipv6, int, 0444);
  75. MODULE_PARM_DESC(disable_ipv6, "Disable IPv6 on all interfaces");
  76. module_param_named(autoconf, ipv6_defaults.autoconf, int, 0444);
  77. MODULE_PARM_DESC(autoconf, "Enable IPv6 address autoconfiguration on all interfaces");
  78. static __inline__ struct ipv6_pinfo *inet6_sk_generic(struct sock *sk)
  79. {
  80. const int offset = sk->sk_prot->obj_size - sizeof(struct ipv6_pinfo);
  81. return (struct ipv6_pinfo *)(((u8 *)sk) + offset);
  82. }
  83. static int inet6_create(struct net *net, struct socket *sock, int protocol)
  84. {
  85. struct inet_sock *inet;
  86. struct ipv6_pinfo *np;
  87. struct sock *sk;
  88. struct inet_protosw *answer;
  89. struct proto *answer_prot;
  90. unsigned char answer_flags;
  91. char answer_no_check;
  92. int try_loading_module = 0;
  93. int err;
  94. if (sock->type != SOCK_RAW &&
  95. sock->type != SOCK_DGRAM &&
  96. !inet_ehash_secret)
  97. build_ehash_secret();
  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 (answer->capability > 0 && !capable(answer->capability))
  142. goto out_rcu_unlock;
  143. sock->ops = answer->ops;
  144. answer_prot = answer->prot;
  145. answer_no_check = answer->no_check;
  146. answer_flags = answer->flags;
  147. rcu_read_unlock();
  148. WARN_ON(answer_prot->slab == NULL);
  149. err = -ENOBUFS;
  150. sk = sk_alloc(net, PF_INET6, GFP_KERNEL, answer_prot);
  151. if (sk == NULL)
  152. goto out;
  153. sock_init_data(sock, sk);
  154. err = 0;
  155. sk->sk_no_check = answer_no_check;
  156. if (INET_PROTOSW_REUSE & answer_flags)
  157. sk->sk_reuse = 1;
  158. inet = inet_sk(sk);
  159. inet->is_icsk = (INET_PROTOSW_ICSK & answer_flags) != 0;
  160. if (SOCK_RAW == sock->type) {
  161. inet->num = protocol;
  162. if (IPPROTO_RAW == protocol)
  163. inet->hdrincl = 1;
  164. }
  165. sk->sk_destruct = inet_sock_destruct;
  166. sk->sk_family = PF_INET6;
  167. sk->sk_protocol = protocol;
  168. sk->sk_backlog_rcv = answer->prot->backlog_rcv;
  169. inet_sk(sk)->pinet6 = np = inet6_sk_generic(sk);
  170. np->hop_limit = -1;
  171. np->mcast_hops = -1;
  172. np->mc_loop = 1;
  173. np->pmtudisc = IPV6_PMTUDISC_WANT;
  174. np->ipv6only = net->ipv6.sysctl.bindv6only;
  175. /* Init the ipv4 part of the socket since we can have sockets
  176. * using v6 API for ipv4.
  177. */
  178. inet->uc_ttl = -1;
  179. inet->mc_loop = 1;
  180. inet->mc_ttl = 1;
  181. inet->mc_index = 0;
  182. inet->mc_list = NULL;
  183. if (ipv4_config.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->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->sport = htons(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. addr_type = ipv6_addr_type(&addr->sin6_addr);
  236. if ((addr_type & IPV6_ADDR_MULTICAST) && sock->type == SOCK_STREAM)
  237. return -EINVAL;
  238. snum = ntohs(addr->sin6_port);
  239. if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
  240. return -EACCES;
  241. lock_sock(sk);
  242. /* Check these errors (active socket, double bind). */
  243. if (sk->sk_state != TCP_CLOSE || inet->num) {
  244. err = -EINVAL;
  245. goto out;
  246. }
  247. /* Check if the address belongs to the host. */
  248. if (addr_type == IPV6_ADDR_MAPPED) {
  249. int chk_addr_ret;
  250. /* Binding to v4-mapped address on a v6-only socket
  251. * makes no sense
  252. */
  253. if (np->ipv6only) {
  254. err = -EINVAL;
  255. goto out;
  256. }
  257. /* Reproduce AF_INET checks to make the bindings consitant */
  258. v4addr = addr->sin6_addr.s6_addr32[3];
  259. chk_addr_ret = inet_addr_type(net, v4addr);
  260. if (!sysctl_ip_nonlocal_bind &&
  261. !(inet->freebind || inet->transparent) &&
  262. v4addr != htonl(INADDR_ANY) &&
  263. chk_addr_ret != RTN_LOCAL &&
  264. chk_addr_ret != RTN_MULTICAST &&
  265. chk_addr_ret != RTN_BROADCAST)
  266. goto out;
  267. } else {
  268. if (addr_type != IPV6_ADDR_ANY) {
  269. struct net_device *dev = NULL;
  270. if (addr_type & IPV6_ADDR_LINKLOCAL) {
  271. if (addr_len >= sizeof(struct sockaddr_in6) &&
  272. addr->sin6_scope_id) {
  273. /* Override any existing binding, if another one
  274. * is supplied by user.
  275. */
  276. sk->sk_bound_dev_if = addr->sin6_scope_id;
  277. }
  278. /* Binding to link-local address requires an interface */
  279. if (!sk->sk_bound_dev_if) {
  280. err = -EINVAL;
  281. goto out;
  282. }
  283. dev = dev_get_by_index(net, sk->sk_bound_dev_if);
  284. if (!dev) {
  285. err = -ENODEV;
  286. goto out;
  287. }
  288. }
  289. /* ipv4 addr of the socket is invalid. Only the
  290. * unspecified and mapped address have a v4 equivalent.
  291. */
  292. v4addr = LOOPBACK4_IPV6;
  293. if (!(addr_type & IPV6_ADDR_MULTICAST)) {
  294. if (!ipv6_chk_addr(net, &addr->sin6_addr,
  295. dev, 0)) {
  296. if (dev)
  297. dev_put(dev);
  298. err = -EADDRNOTAVAIL;
  299. goto out;
  300. }
  301. }
  302. if (dev)
  303. dev_put(dev);
  304. }
  305. }
  306. inet->rcv_saddr = v4addr;
  307. inet->saddr = v4addr;
  308. ipv6_addr_copy(&np->rcv_saddr, &addr->sin6_addr);
  309. if (!(addr_type & IPV6_ADDR_MULTICAST))
  310. ipv6_addr_copy(&np->saddr, &addr->sin6_addr);
  311. /* Make sure we are allowed to bind here. */
  312. if (sk->sk_prot->get_port(sk, snum)) {
  313. inet_reset_saddr(sk);
  314. err = -EADDRINUSE;
  315. goto out;
  316. }
  317. if (addr_type != IPV6_ADDR_ANY) {
  318. sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
  319. if (addr_type != IPV6_ADDR_MAPPED)
  320. np->ipv6only = 1;
  321. }
  322. if (snum)
  323. sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
  324. inet->sport = htons(inet->num);
  325. inet->dport = 0;
  326. inet->daddr = 0;
  327. out:
  328. release_sock(sk);
  329. return err;
  330. }
  331. EXPORT_SYMBOL(inet6_bind);
  332. int inet6_release(struct socket *sock)
  333. {
  334. struct sock *sk = sock->sk;
  335. if (sk == NULL)
  336. return -EINVAL;
  337. /* Free mc lists */
  338. ipv6_sock_mc_close(sk);
  339. /* Free ac lists */
  340. ipv6_sock_ac_close(sk);
  341. return inet_release(sock);
  342. }
  343. EXPORT_SYMBOL(inet6_release);
  344. void inet6_destroy_sock(struct sock *sk)
  345. {
  346. struct ipv6_pinfo *np = inet6_sk(sk);
  347. struct sk_buff *skb;
  348. struct ipv6_txoptions *opt;
  349. /* Release rx options */
  350. if ((skb = xchg(&np->pktoptions, NULL)) != NULL)
  351. kfree_skb(skb);
  352. /* Free flowlabels */
  353. fl6_free_socklist(sk);
  354. /* Free tx options */
  355. if ((opt = xchg(&np->opt, NULL)) != NULL)
  356. sock_kfree_s(sk, opt, opt->tot_len);
  357. }
  358. EXPORT_SYMBOL_GPL(inet6_destroy_sock);
  359. /*
  360. * This does both peername and sockname.
  361. */
  362. int inet6_getname(struct socket *sock, struct sockaddr *uaddr,
  363. int *uaddr_len, int peer)
  364. {
  365. struct sockaddr_in6 *sin=(struct sockaddr_in6 *)uaddr;
  366. struct sock *sk = sock->sk;
  367. struct inet_sock *inet = inet_sk(sk);
  368. struct ipv6_pinfo *np = inet6_sk(sk);
  369. sin->sin6_family = AF_INET6;
  370. sin->sin6_flowinfo = 0;
  371. sin->sin6_scope_id = 0;
  372. if (peer) {
  373. if (!inet->dport)
  374. return -ENOTCONN;
  375. if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
  376. peer == 1)
  377. return -ENOTCONN;
  378. sin->sin6_port = inet->dport;
  379. ipv6_addr_copy(&sin->sin6_addr, &np->daddr);
  380. if (np->sndflow)
  381. sin->sin6_flowinfo = np->flow_label;
  382. } else {
  383. if (ipv6_addr_any(&np->rcv_saddr))
  384. ipv6_addr_copy(&sin->sin6_addr, &np->saddr);
  385. else
  386. ipv6_addr_copy(&sin->sin6_addr, &np->rcv_saddr);
  387. sin->sin6_port = inet->sport;
  388. }
  389. if (ipv6_addr_type(&sin->sin6_addr) & IPV6_ADDR_LINKLOCAL)
  390. sin->sin6_scope_id = sk->sk_bound_dev_if;
  391. *uaddr_len = sizeof(*sin);
  392. return(0);
  393. }
  394. EXPORT_SYMBOL(inet6_getname);
  395. int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  396. {
  397. struct sock *sk = sock->sk;
  398. struct net *net = sock_net(sk);
  399. switch(cmd)
  400. {
  401. case SIOCGSTAMP:
  402. return sock_get_timestamp(sk, (struct timeval __user *)arg);
  403. case SIOCGSTAMPNS:
  404. return sock_get_timestampns(sk, (struct timespec __user *)arg);
  405. case SIOCADDRT:
  406. case SIOCDELRT:
  407. return(ipv6_route_ioctl(net, cmd, (void __user *)arg));
  408. case SIOCSIFADDR:
  409. return addrconf_add_ifaddr(net, (void __user *) arg);
  410. case SIOCDIFADDR:
  411. return addrconf_del_ifaddr(net, (void __user *) arg);
  412. case SIOCSIFDSTADDR:
  413. return addrconf_set_dstaddr(net, (void __user *) arg);
  414. default:
  415. if (!sk->sk_prot->ioctl)
  416. return -ENOIOCTLCMD;
  417. return sk->sk_prot->ioctl(sk, cmd, arg);
  418. }
  419. /*NOTREACHED*/
  420. return(0);
  421. }
  422. EXPORT_SYMBOL(inet6_ioctl);
  423. const struct proto_ops inet6_stream_ops = {
  424. .family = PF_INET6,
  425. .owner = THIS_MODULE,
  426. .release = inet6_release,
  427. .bind = inet6_bind,
  428. .connect = inet_stream_connect, /* ok */
  429. .socketpair = sock_no_socketpair, /* a do nothing */
  430. .accept = inet_accept, /* ok */
  431. .getname = inet6_getname,
  432. .poll = tcp_poll, /* ok */
  433. .ioctl = inet6_ioctl, /* must change */
  434. .listen = inet_listen, /* ok */
  435. .shutdown = inet_shutdown, /* ok */
  436. .setsockopt = sock_common_setsockopt, /* ok */
  437. .getsockopt = sock_common_getsockopt, /* ok */
  438. .sendmsg = tcp_sendmsg, /* ok */
  439. .recvmsg = sock_common_recvmsg, /* ok */
  440. .mmap = sock_no_mmap,
  441. .sendpage = tcp_sendpage,
  442. .splice_read = tcp_splice_read,
  443. #ifdef CONFIG_COMPAT
  444. .compat_setsockopt = compat_sock_common_setsockopt,
  445. .compat_getsockopt = compat_sock_common_getsockopt,
  446. #endif
  447. };
  448. const struct proto_ops inet6_dgram_ops = {
  449. .family = PF_INET6,
  450. .owner = THIS_MODULE,
  451. .release = inet6_release,
  452. .bind = inet6_bind,
  453. .connect = inet_dgram_connect, /* ok */
  454. .socketpair = sock_no_socketpair, /* a do nothing */
  455. .accept = sock_no_accept, /* a do nothing */
  456. .getname = inet6_getname,
  457. .poll = udp_poll, /* ok */
  458. .ioctl = inet6_ioctl, /* must change */
  459. .listen = sock_no_listen, /* ok */
  460. .shutdown = inet_shutdown, /* ok */
  461. .setsockopt = sock_common_setsockopt, /* ok */
  462. .getsockopt = sock_common_getsockopt, /* ok */
  463. .sendmsg = inet_sendmsg, /* ok */
  464. .recvmsg = sock_common_recvmsg, /* ok */
  465. .mmap = sock_no_mmap,
  466. .sendpage = sock_no_sendpage,
  467. #ifdef CONFIG_COMPAT
  468. .compat_setsockopt = compat_sock_common_setsockopt,
  469. .compat_getsockopt = compat_sock_common_getsockopt,
  470. #endif
  471. };
  472. static struct net_proto_family inet6_family_ops = {
  473. .family = PF_INET6,
  474. .create = inet6_create,
  475. .owner = THIS_MODULE,
  476. };
  477. int inet6_register_protosw(struct inet_protosw *p)
  478. {
  479. struct list_head *lh;
  480. struct inet_protosw *answer;
  481. struct list_head *last_perm;
  482. int protocol = p->protocol;
  483. int ret;
  484. spin_lock_bh(&inetsw6_lock);
  485. ret = -EINVAL;
  486. if (p->type >= SOCK_MAX)
  487. goto out_illegal;
  488. /* If we are trying to override a permanent protocol, bail. */
  489. answer = NULL;
  490. ret = -EPERM;
  491. last_perm = &inetsw6[p->type];
  492. list_for_each(lh, &inetsw6[p->type]) {
  493. answer = list_entry(lh, struct inet_protosw, list);
  494. /* Check only the non-wild match. */
  495. if (INET_PROTOSW_PERMANENT & answer->flags) {
  496. if (protocol == answer->protocol)
  497. break;
  498. last_perm = lh;
  499. }
  500. answer = NULL;
  501. }
  502. if (answer)
  503. goto out_permanent;
  504. /* Add the new entry after the last permanent entry if any, so that
  505. * the new entry does not override a permanent entry when matched with
  506. * a wild-card protocol. But it is allowed to override any existing
  507. * non-permanent entry. This means that when we remove this entry, the
  508. * system automatically returns to the old behavior.
  509. */
  510. list_add_rcu(&p->list, last_perm);
  511. ret = 0;
  512. out:
  513. spin_unlock_bh(&inetsw6_lock);
  514. return ret;
  515. out_permanent:
  516. printk(KERN_ERR "Attempt to override permanent protocol %d.\n",
  517. protocol);
  518. goto out;
  519. out_illegal:
  520. printk(KERN_ERR
  521. "Ignoring attempt to register invalid socket type %d.\n",
  522. p->type);
  523. goto out;
  524. }
  525. EXPORT_SYMBOL(inet6_register_protosw);
  526. void
  527. inet6_unregister_protosw(struct inet_protosw *p)
  528. {
  529. if (INET_PROTOSW_PERMANENT & p->flags) {
  530. printk(KERN_ERR
  531. "Attempt to unregister permanent protocol %d.\n",
  532. p->protocol);
  533. } else {
  534. spin_lock_bh(&inetsw6_lock);
  535. list_del_rcu(&p->list);
  536. spin_unlock_bh(&inetsw6_lock);
  537. synchronize_net();
  538. }
  539. }
  540. EXPORT_SYMBOL(inet6_unregister_protosw);
  541. int inet6_sk_rebuild_header(struct sock *sk)
  542. {
  543. int err;
  544. struct dst_entry *dst;
  545. struct ipv6_pinfo *np = inet6_sk(sk);
  546. dst = __sk_dst_check(sk, np->dst_cookie);
  547. if (dst == NULL) {
  548. struct inet_sock *inet = inet_sk(sk);
  549. struct in6_addr *final_p = NULL, final;
  550. struct flowi fl;
  551. memset(&fl, 0, sizeof(fl));
  552. fl.proto = sk->sk_protocol;
  553. ipv6_addr_copy(&fl.fl6_dst, &np->daddr);
  554. ipv6_addr_copy(&fl.fl6_src, &np->saddr);
  555. fl.fl6_flowlabel = np->flow_label;
  556. fl.oif = sk->sk_bound_dev_if;
  557. fl.fl_ip_dport = inet->dport;
  558. fl.fl_ip_sport = inet->sport;
  559. security_sk_classify_flow(sk, &fl);
  560. if (np->opt && np->opt->srcrt) {
  561. struct rt0_hdr *rt0 = (struct rt0_hdr *) np->opt->srcrt;
  562. ipv6_addr_copy(&final, &fl.fl6_dst);
  563. ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
  564. final_p = &final;
  565. }
  566. err = ip6_dst_lookup(sk, &dst, &fl);
  567. if (err) {
  568. sk->sk_route_caps = 0;
  569. return err;
  570. }
  571. if (final_p)
  572. ipv6_addr_copy(&fl.fl6_dst, final_p);
  573. if ((err = xfrm_lookup(sock_net(sk), &dst, &fl, sk, 0)) < 0) {
  574. sk->sk_err_soft = -err;
  575. return err;
  576. }
  577. __ip6_dst_store(sk, dst, NULL, NULL);
  578. }
  579. return 0;
  580. }
  581. EXPORT_SYMBOL_GPL(inet6_sk_rebuild_header);
  582. int ipv6_opt_accepted(struct sock *sk, struct sk_buff *skb)
  583. {
  584. struct ipv6_pinfo *np = inet6_sk(sk);
  585. struct inet6_skb_parm *opt = IP6CB(skb);
  586. if (np->rxopt.all) {
  587. if ((opt->hop && (np->rxopt.bits.hopopts ||
  588. np->rxopt.bits.ohopopts)) ||
  589. ((IPV6_FLOWINFO_MASK &
  590. *(__be32 *)skb_network_header(skb)) &&
  591. np->rxopt.bits.rxflow) ||
  592. (opt->srcrt && (np->rxopt.bits.srcrt ||
  593. np->rxopt.bits.osrcrt)) ||
  594. ((opt->dst1 || opt->dst0) &&
  595. (np->rxopt.bits.dstopts || np->rxopt.bits.odstopts)))
  596. return 1;
  597. }
  598. return 0;
  599. }
  600. EXPORT_SYMBOL_GPL(ipv6_opt_accepted);
  601. static int ipv6_gso_pull_exthdrs(struct sk_buff *skb, int proto)
  602. {
  603. struct inet6_protocol *ops = NULL;
  604. for (;;) {
  605. struct ipv6_opt_hdr *opth;
  606. int len;
  607. if (proto != NEXTHDR_HOP) {
  608. ops = rcu_dereference(inet6_protos[proto]);
  609. if (unlikely(!ops))
  610. break;
  611. if (!(ops->flags & INET6_PROTO_GSO_EXTHDR))
  612. break;
  613. }
  614. if (unlikely(!pskb_may_pull(skb, 8)))
  615. break;
  616. opth = (void *)skb->data;
  617. len = ipv6_optlen(opth);
  618. if (unlikely(!pskb_may_pull(skb, len)))
  619. break;
  620. proto = opth->nexthdr;
  621. __skb_pull(skb, len);
  622. }
  623. return proto;
  624. }
  625. static int ipv6_gso_send_check(struct sk_buff *skb)
  626. {
  627. struct ipv6hdr *ipv6h;
  628. struct inet6_protocol *ops;
  629. int err = -EINVAL;
  630. if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h))))
  631. goto out;
  632. ipv6h = ipv6_hdr(skb);
  633. __skb_pull(skb, sizeof(*ipv6h));
  634. err = -EPROTONOSUPPORT;
  635. rcu_read_lock();
  636. ops = rcu_dereference(inet6_protos[
  637. ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr)]);
  638. if (likely(ops && ops->gso_send_check)) {
  639. skb_reset_transport_header(skb);
  640. err = ops->gso_send_check(skb);
  641. }
  642. rcu_read_unlock();
  643. out:
  644. return err;
  645. }
  646. static struct sk_buff *ipv6_gso_segment(struct sk_buff *skb, int features)
  647. {
  648. struct sk_buff *segs = ERR_PTR(-EINVAL);
  649. struct ipv6hdr *ipv6h;
  650. struct inet6_protocol *ops;
  651. if (!(features & NETIF_F_V6_CSUM))
  652. features &= ~NETIF_F_SG;
  653. if (unlikely(skb_shinfo(skb)->gso_type &
  654. ~(SKB_GSO_UDP |
  655. SKB_GSO_DODGY |
  656. SKB_GSO_TCP_ECN |
  657. SKB_GSO_TCPV6 |
  658. 0)))
  659. goto out;
  660. if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h))))
  661. goto out;
  662. ipv6h = ipv6_hdr(skb);
  663. __skb_pull(skb, sizeof(*ipv6h));
  664. segs = ERR_PTR(-EPROTONOSUPPORT);
  665. rcu_read_lock();
  666. ops = rcu_dereference(inet6_protos[
  667. ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr)]);
  668. if (likely(ops && ops->gso_segment)) {
  669. skb_reset_transport_header(skb);
  670. segs = ops->gso_segment(skb, features);
  671. }
  672. rcu_read_unlock();
  673. if (unlikely(IS_ERR(segs)))
  674. goto out;
  675. for (skb = segs; skb; skb = skb->next) {
  676. ipv6h = ipv6_hdr(skb);
  677. ipv6h->payload_len = htons(skb->len - skb->mac_len -
  678. sizeof(*ipv6h));
  679. }
  680. out:
  681. return segs;
  682. }
  683. struct ipv6_gro_cb {
  684. struct napi_gro_cb napi;
  685. int proto;
  686. };
  687. #define IPV6_GRO_CB(skb) ((struct ipv6_gro_cb *)(skb)->cb)
  688. static struct sk_buff **ipv6_gro_receive(struct sk_buff **head,
  689. struct sk_buff *skb)
  690. {
  691. struct inet6_protocol *ops;
  692. struct sk_buff **pp = NULL;
  693. struct sk_buff *p;
  694. struct ipv6hdr *iph;
  695. unsigned int nlen;
  696. unsigned int hlen;
  697. unsigned int off;
  698. int flush = 1;
  699. int proto;
  700. __wsum csum;
  701. off = skb_gro_offset(skb);
  702. hlen = off + sizeof(*iph);
  703. iph = skb_gro_header_fast(skb, off);
  704. if (skb_gro_header_hard(skb, hlen)) {
  705. iph = skb_gro_header_slow(skb, hlen, off);
  706. if (unlikely(!iph))
  707. goto out;
  708. }
  709. skb_gro_pull(skb, sizeof(*iph));
  710. skb_set_transport_header(skb, skb_gro_offset(skb));
  711. flush += ntohs(iph->payload_len) != skb_gro_len(skb);
  712. rcu_read_lock();
  713. proto = iph->nexthdr;
  714. ops = rcu_dereference(inet6_protos[proto]);
  715. if (!ops || !ops->gro_receive) {
  716. __pskb_pull(skb, skb_gro_offset(skb));
  717. proto = ipv6_gso_pull_exthdrs(skb, proto);
  718. skb_gro_pull(skb, -skb_transport_offset(skb));
  719. skb_reset_transport_header(skb);
  720. __skb_push(skb, skb_gro_offset(skb));
  721. if (!ops || !ops->gro_receive)
  722. goto out_unlock;
  723. iph = ipv6_hdr(skb);
  724. }
  725. IPV6_GRO_CB(skb)->proto = proto;
  726. flush--;
  727. nlen = skb_network_header_len(skb);
  728. for (p = *head; p; p = p->next) {
  729. struct ipv6hdr *iph2;
  730. if (!NAPI_GRO_CB(p)->same_flow)
  731. continue;
  732. iph2 = ipv6_hdr(p);
  733. /* All fields must match except length. */
  734. if (nlen != skb_network_header_len(p) ||
  735. memcmp(iph, iph2, offsetof(struct ipv6hdr, payload_len)) ||
  736. memcmp(&iph->nexthdr, &iph2->nexthdr,
  737. nlen - offsetof(struct ipv6hdr, nexthdr))) {
  738. NAPI_GRO_CB(p)->same_flow = 0;
  739. continue;
  740. }
  741. NAPI_GRO_CB(p)->flush |= flush;
  742. }
  743. NAPI_GRO_CB(skb)->flush |= flush;
  744. csum = skb->csum;
  745. skb_postpull_rcsum(skb, iph, skb_network_header_len(skb));
  746. pp = ops->gro_receive(head, skb);
  747. skb->csum = csum;
  748. out_unlock:
  749. rcu_read_unlock();
  750. out:
  751. NAPI_GRO_CB(skb)->flush |= flush;
  752. return pp;
  753. }
  754. static int ipv6_gro_complete(struct sk_buff *skb)
  755. {
  756. struct inet6_protocol *ops;
  757. struct ipv6hdr *iph = ipv6_hdr(skb);
  758. int err = -ENOSYS;
  759. iph->payload_len = htons(skb->len - skb_network_offset(skb) -
  760. sizeof(*iph));
  761. rcu_read_lock();
  762. ops = rcu_dereference(inet6_protos[IPV6_GRO_CB(skb)->proto]);
  763. if (WARN_ON(!ops || !ops->gro_complete))
  764. goto out_unlock;
  765. err = ops->gro_complete(skb);
  766. out_unlock:
  767. rcu_read_unlock();
  768. return err;
  769. }
  770. static struct packet_type ipv6_packet_type __read_mostly = {
  771. .type = cpu_to_be16(ETH_P_IPV6),
  772. .func = ipv6_rcv,
  773. .gso_send_check = ipv6_gso_send_check,
  774. .gso_segment = ipv6_gso_segment,
  775. .gro_receive = ipv6_gro_receive,
  776. .gro_complete = ipv6_gro_complete,
  777. };
  778. static int __init ipv6_packet_init(void)
  779. {
  780. dev_add_pack(&ipv6_packet_type);
  781. return 0;
  782. }
  783. static void ipv6_packet_cleanup(void)
  784. {
  785. dev_remove_pack(&ipv6_packet_type);
  786. }
  787. static int __net_init ipv6_init_mibs(struct net *net)
  788. {
  789. if (snmp_mib_init((void **)net->mib.udp_stats_in6,
  790. sizeof (struct udp_mib)) < 0)
  791. return -ENOMEM;
  792. if (snmp_mib_init((void **)net->mib.udplite_stats_in6,
  793. sizeof (struct udp_mib)) < 0)
  794. goto err_udplite_mib;
  795. if (snmp_mib_init((void **)net->mib.ipv6_statistics,
  796. sizeof(struct ipstats_mib)) < 0)
  797. goto err_ip_mib;
  798. if (snmp_mib_init((void **)net->mib.icmpv6_statistics,
  799. sizeof(struct icmpv6_mib)) < 0)
  800. goto err_icmp_mib;
  801. if (snmp_mib_init((void **)net->mib.icmpv6msg_statistics,
  802. sizeof(struct icmpv6msg_mib)) < 0)
  803. goto err_icmpmsg_mib;
  804. return 0;
  805. err_icmpmsg_mib:
  806. snmp_mib_free((void **)net->mib.icmpv6_statistics);
  807. err_icmp_mib:
  808. snmp_mib_free((void **)net->mib.ipv6_statistics);
  809. err_ip_mib:
  810. snmp_mib_free((void **)net->mib.udplite_stats_in6);
  811. err_udplite_mib:
  812. snmp_mib_free((void **)net->mib.udp_stats_in6);
  813. return -ENOMEM;
  814. }
  815. static void __net_exit ipv6_cleanup_mibs(struct net *net)
  816. {
  817. snmp_mib_free((void **)net->mib.udp_stats_in6);
  818. snmp_mib_free((void **)net->mib.udplite_stats_in6);
  819. snmp_mib_free((void **)net->mib.ipv6_statistics);
  820. snmp_mib_free((void **)net->mib.icmpv6_statistics);
  821. snmp_mib_free((void **)net->mib.icmpv6msg_statistics);
  822. }
  823. static int __net_init inet6_net_init(struct net *net)
  824. {
  825. int err = 0;
  826. net->ipv6.sysctl.bindv6only = 0;
  827. net->ipv6.sysctl.icmpv6_time = 1*HZ;
  828. err = ipv6_init_mibs(net);
  829. if (err)
  830. return err;
  831. #ifdef CONFIG_PROC_FS
  832. err = udp6_proc_init(net);
  833. if (err)
  834. goto out;
  835. err = tcp6_proc_init(net);
  836. if (err)
  837. goto proc_tcp6_fail;
  838. err = ac6_proc_init(net);
  839. if (err)
  840. goto proc_ac6_fail;
  841. #endif
  842. return err;
  843. #ifdef CONFIG_PROC_FS
  844. proc_ac6_fail:
  845. tcp6_proc_exit(net);
  846. proc_tcp6_fail:
  847. udp6_proc_exit(net);
  848. out:
  849. ipv6_cleanup_mibs(net);
  850. return err;
  851. #endif
  852. }
  853. static void inet6_net_exit(struct net *net)
  854. {
  855. #ifdef CONFIG_PROC_FS
  856. udp6_proc_exit(net);
  857. tcp6_proc_exit(net);
  858. ac6_proc_exit(net);
  859. #endif
  860. ipv6_cleanup_mibs(net);
  861. }
  862. static struct pernet_operations inet6_net_ops = {
  863. .init = inet6_net_init,
  864. .exit = inet6_net_exit,
  865. };
  866. static int __init inet6_init(void)
  867. {
  868. struct sk_buff *dummy_skb;
  869. struct list_head *r;
  870. int err = 0;
  871. BUILD_BUG_ON(sizeof(struct inet6_skb_parm) > sizeof(dummy_skb->cb));
  872. /* Register the socket-side information for inet6_create. */
  873. for(r = &inetsw6[0]; r < &inetsw6[SOCK_MAX]; ++r)
  874. INIT_LIST_HEAD(r);
  875. if (disable_ipv6_mod) {
  876. printk(KERN_INFO
  877. "IPv6: Loaded, but administratively disabled, "
  878. "reboot required to enable\n");
  879. goto out;
  880. }
  881. err = proto_register(&tcpv6_prot, 1);
  882. if (err)
  883. goto out;
  884. err = proto_register(&udpv6_prot, 1);
  885. if (err)
  886. goto out_unregister_tcp_proto;
  887. err = proto_register(&udplitev6_prot, 1);
  888. if (err)
  889. goto out_unregister_udp_proto;
  890. err = proto_register(&rawv6_prot, 1);
  891. if (err)
  892. goto out_unregister_udplite_proto;
  893. /* We MUST register RAW sockets before we create the ICMP6,
  894. * IGMP6, or NDISC control sockets.
  895. */
  896. err = rawv6_init();
  897. if (err)
  898. goto out_unregister_raw_proto;
  899. /* Register the family here so that the init calls below will
  900. * be able to create sockets. (?? is this dangerous ??)
  901. */
  902. err = sock_register(&inet6_family_ops);
  903. if (err)
  904. goto out_sock_register_fail;
  905. #ifdef CONFIG_SYSCTL
  906. err = ipv6_static_sysctl_register();
  907. if (err)
  908. goto static_sysctl_fail;
  909. #endif
  910. /*
  911. * ipngwg API draft makes clear that the correct semantics
  912. * for TCP and UDP is to consider one TCP and UDP instance
  913. * in a host availiable by both INET and INET6 APIs and
  914. * able to communicate via both network protocols.
  915. */
  916. err = register_pernet_subsys(&inet6_net_ops);
  917. if (err)
  918. goto register_pernet_fail;
  919. err = icmpv6_init();
  920. if (err)
  921. goto icmp_fail;
  922. err = ip6_mr_init();
  923. if (err)
  924. goto ipmr_fail;
  925. err = ndisc_init();
  926. if (err)
  927. goto ndisc_fail;
  928. err = igmp6_init();
  929. if (err)
  930. goto igmp_fail;
  931. err = ipv6_netfilter_init();
  932. if (err)
  933. goto netfilter_fail;
  934. /* Create /proc/foo6 entries. */
  935. #ifdef CONFIG_PROC_FS
  936. err = -ENOMEM;
  937. if (raw6_proc_init())
  938. goto proc_raw6_fail;
  939. if (udplite6_proc_init())
  940. goto proc_udplite6_fail;
  941. if (ipv6_misc_proc_init())
  942. goto proc_misc6_fail;
  943. if (if6_proc_init())
  944. goto proc_if6_fail;
  945. #endif
  946. err = ip6_route_init();
  947. if (err)
  948. goto ip6_route_fail;
  949. err = ip6_flowlabel_init();
  950. if (err)
  951. goto ip6_flowlabel_fail;
  952. err = addrconf_init();
  953. if (err)
  954. goto addrconf_fail;
  955. /* Init v6 extension headers. */
  956. err = ipv6_exthdrs_init();
  957. if (err)
  958. goto ipv6_exthdrs_fail;
  959. err = ipv6_frag_init();
  960. if (err)
  961. goto ipv6_frag_fail;
  962. /* Init v6 transport protocols. */
  963. err = udpv6_init();
  964. if (err)
  965. goto udpv6_fail;
  966. err = udplitev6_init();
  967. if (err)
  968. goto udplitev6_fail;
  969. err = tcpv6_init();
  970. if (err)
  971. goto tcpv6_fail;
  972. err = ipv6_packet_init();
  973. if (err)
  974. goto ipv6_packet_fail;
  975. #ifdef CONFIG_SYSCTL
  976. err = ipv6_sysctl_register();
  977. if (err)
  978. goto sysctl_fail;
  979. #endif
  980. out:
  981. return err;
  982. #ifdef CONFIG_SYSCTL
  983. sysctl_fail:
  984. ipv6_packet_cleanup();
  985. #endif
  986. ipv6_packet_fail:
  987. tcpv6_exit();
  988. tcpv6_fail:
  989. udplitev6_exit();
  990. udplitev6_fail:
  991. udpv6_exit();
  992. udpv6_fail:
  993. ipv6_frag_exit();
  994. ipv6_frag_fail:
  995. ipv6_exthdrs_exit();
  996. ipv6_exthdrs_fail:
  997. addrconf_cleanup();
  998. addrconf_fail:
  999. ip6_flowlabel_cleanup();
  1000. ip6_flowlabel_fail:
  1001. ip6_route_cleanup();
  1002. ip6_route_fail:
  1003. #ifdef CONFIG_PROC_FS
  1004. if6_proc_exit();
  1005. proc_if6_fail:
  1006. ipv6_misc_proc_exit();
  1007. proc_misc6_fail:
  1008. udplite6_proc_exit();
  1009. proc_udplite6_fail:
  1010. raw6_proc_exit();
  1011. proc_raw6_fail:
  1012. #endif
  1013. ipv6_netfilter_fini();
  1014. netfilter_fail:
  1015. igmp6_cleanup();
  1016. igmp_fail:
  1017. ndisc_cleanup();
  1018. ndisc_fail:
  1019. ip6_mr_cleanup();
  1020. ipmr_fail:
  1021. icmpv6_cleanup();
  1022. icmp_fail:
  1023. unregister_pernet_subsys(&inet6_net_ops);
  1024. register_pernet_fail:
  1025. #ifdef CONFIG_SYSCTL
  1026. ipv6_static_sysctl_unregister();
  1027. static_sysctl_fail:
  1028. #endif
  1029. sock_unregister(PF_INET6);
  1030. rtnl_unregister_all(PF_INET6);
  1031. out_sock_register_fail:
  1032. rawv6_exit();
  1033. out_unregister_raw_proto:
  1034. proto_unregister(&rawv6_prot);
  1035. out_unregister_udplite_proto:
  1036. proto_unregister(&udplitev6_prot);
  1037. out_unregister_udp_proto:
  1038. proto_unregister(&udpv6_prot);
  1039. out_unregister_tcp_proto:
  1040. proto_unregister(&tcpv6_prot);
  1041. goto out;
  1042. }
  1043. module_init(inet6_init);
  1044. static void __exit inet6_exit(void)
  1045. {
  1046. if (disable_ipv6_mod)
  1047. return;
  1048. /* First of all disallow new sockets creation. */
  1049. sock_unregister(PF_INET6);
  1050. /* Disallow any further netlink messages */
  1051. rtnl_unregister_all(PF_INET6);
  1052. #ifdef CONFIG_SYSCTL
  1053. ipv6_sysctl_unregister();
  1054. #endif
  1055. udpv6_exit();
  1056. udplitev6_exit();
  1057. tcpv6_exit();
  1058. /* Cleanup code parts. */
  1059. ipv6_packet_cleanup();
  1060. ipv6_frag_exit();
  1061. ipv6_exthdrs_exit();
  1062. addrconf_cleanup();
  1063. ip6_flowlabel_cleanup();
  1064. ip6_route_cleanup();
  1065. #ifdef CONFIG_PROC_FS
  1066. /* Cleanup code parts. */
  1067. if6_proc_exit();
  1068. ipv6_misc_proc_exit();
  1069. udplite6_proc_exit();
  1070. raw6_proc_exit();
  1071. #endif
  1072. ipv6_netfilter_fini();
  1073. igmp6_cleanup();
  1074. ndisc_cleanup();
  1075. ip6_mr_cleanup();
  1076. icmpv6_cleanup();
  1077. rawv6_exit();
  1078. unregister_pernet_subsys(&inet6_net_ops);
  1079. #ifdef CONFIG_SYSCTL
  1080. ipv6_static_sysctl_unregister();
  1081. #endif
  1082. proto_unregister(&rawv6_prot);
  1083. proto_unregister(&udplitev6_prot);
  1084. proto_unregister(&udpv6_prot);
  1085. proto_unregister(&tcpv6_prot);
  1086. rcu_barrier(); /* Wait for completion of call_rcu()'s */
  1087. }
  1088. module_exit(inet6_exit);
  1089. MODULE_ALIAS_NETPROTO(PF_INET6);