ip6_output.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655
  1. /*
  2. * IPv6 output functions
  3. * Linux INET6 implementation
  4. *
  5. * Authors:
  6. * Pedro Roque <roque@di.fc.ul.pt>
  7. *
  8. * Based on linux/net/ipv4/ip_output.c
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version
  13. * 2 of the License, or (at your option) any later version.
  14. *
  15. * Changes:
  16. * A.N.Kuznetsov : airthmetics in fragmentation.
  17. * extension headers are implemented.
  18. * route changes now work.
  19. * ip6_forward does not confuse sniffers.
  20. * etc.
  21. *
  22. * H. von Brand : Added missing #include <linux/string.h>
  23. * Imran Patel : frag id should be in NBO
  24. * Kazunori MIYAZAWA @USAGI
  25. * : add ip6_append_data and related functions
  26. * for datagram xmit
  27. */
  28. #include <linux/errno.h>
  29. #include <linux/kernel.h>
  30. #include <linux/string.h>
  31. #include <linux/socket.h>
  32. #include <linux/net.h>
  33. #include <linux/netdevice.h>
  34. #include <linux/if_arp.h>
  35. #include <linux/in6.h>
  36. #include <linux/tcp.h>
  37. #include <linux/route.h>
  38. #include <linux/module.h>
  39. #include <linux/slab.h>
  40. #include <linux/netfilter.h>
  41. #include <linux/netfilter_ipv6.h>
  42. #include <net/sock.h>
  43. #include <net/snmp.h>
  44. #include <net/ipv6.h>
  45. #include <net/ndisc.h>
  46. #include <net/protocol.h>
  47. #include <net/ip6_route.h>
  48. #include <net/addrconf.h>
  49. #include <net/rawv6.h>
  50. #include <net/icmp.h>
  51. #include <net/xfrm.h>
  52. #include <net/checksum.h>
  53. #include <linux/mroute6.h>
  54. int ip6_fragment(struct sk_buff *skb, int (*output)(struct sk_buff *));
  55. int __ip6_local_out(struct sk_buff *skb)
  56. {
  57. int len;
  58. len = skb->len - sizeof(struct ipv6hdr);
  59. if (len > IPV6_MAXPLEN)
  60. len = 0;
  61. ipv6_hdr(skb)->payload_len = htons(len);
  62. return nf_hook(NFPROTO_IPV6, NF_INET_LOCAL_OUT, skb, NULL,
  63. skb_dst(skb)->dev, dst_output);
  64. }
  65. int ip6_local_out(struct sk_buff *skb)
  66. {
  67. int err;
  68. err = __ip6_local_out(skb);
  69. if (likely(err == 1))
  70. err = dst_output(skb);
  71. return err;
  72. }
  73. EXPORT_SYMBOL_GPL(ip6_local_out);
  74. /* dev_loopback_xmit for use with netfilter. */
  75. static int ip6_dev_loopback_xmit(struct sk_buff *newskb)
  76. {
  77. skb_reset_mac_header(newskb);
  78. __skb_pull(newskb, skb_network_offset(newskb));
  79. newskb->pkt_type = PACKET_LOOPBACK;
  80. newskb->ip_summed = CHECKSUM_UNNECESSARY;
  81. WARN_ON(!skb_dst(newskb));
  82. netif_rx_ni(newskb);
  83. return 0;
  84. }
  85. static int ip6_finish_output2(struct sk_buff *skb)
  86. {
  87. struct dst_entry *dst = skb_dst(skb);
  88. struct net_device *dev = dst->dev;
  89. struct neighbour *neigh;
  90. skb->protocol = htons(ETH_P_IPV6);
  91. skb->dev = dev;
  92. if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr)) {
  93. struct inet6_dev *idev = ip6_dst_idev(skb_dst(skb));
  94. if (!(dev->flags & IFF_LOOPBACK) && sk_mc_loop(skb->sk) &&
  95. ((mroute6_socket(dev_net(dev), skb) &&
  96. !(IP6CB(skb)->flags & IP6SKB_FORWARDED)) ||
  97. ipv6_chk_mcast_addr(dev, &ipv6_hdr(skb)->daddr,
  98. &ipv6_hdr(skb)->saddr))) {
  99. struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
  100. /* Do not check for IFF_ALLMULTI; multicast routing
  101. is not supported in any case.
  102. */
  103. if (newskb)
  104. NF_HOOK(NFPROTO_IPV6, NF_INET_POST_ROUTING,
  105. newskb, NULL, newskb->dev,
  106. ip6_dev_loopback_xmit);
  107. if (ipv6_hdr(skb)->hop_limit == 0) {
  108. IP6_INC_STATS(dev_net(dev), idev,
  109. IPSTATS_MIB_OUTDISCARDS);
  110. kfree_skb(skb);
  111. return 0;
  112. }
  113. }
  114. IP6_UPD_PO_STATS(dev_net(dev), idev, IPSTATS_MIB_OUTMCAST,
  115. skb->len);
  116. }
  117. rcu_read_lock();
  118. neigh = dst_get_neighbour_noref(dst);
  119. if (neigh) {
  120. int res = neigh_output(neigh, skb);
  121. rcu_read_unlock();
  122. return res;
  123. }
  124. rcu_read_unlock();
  125. IP6_INC_STATS_BH(dev_net(dst->dev),
  126. ip6_dst_idev(dst), IPSTATS_MIB_OUTNOROUTES);
  127. kfree_skb(skb);
  128. return -EINVAL;
  129. }
  130. static int ip6_finish_output(struct sk_buff *skb)
  131. {
  132. if ((skb->len > ip6_skb_dst_mtu(skb) && !skb_is_gso(skb)) ||
  133. dst_allfrag(skb_dst(skb)))
  134. return ip6_fragment(skb, ip6_finish_output2);
  135. else
  136. return ip6_finish_output2(skb);
  137. }
  138. int ip6_output(struct sk_buff *skb)
  139. {
  140. struct net_device *dev = skb_dst(skb)->dev;
  141. struct inet6_dev *idev = ip6_dst_idev(skb_dst(skb));
  142. if (unlikely(idev->cnf.disable_ipv6)) {
  143. IP6_INC_STATS(dev_net(dev), idev,
  144. IPSTATS_MIB_OUTDISCARDS);
  145. kfree_skb(skb);
  146. return 0;
  147. }
  148. return NF_HOOK_COND(NFPROTO_IPV6, NF_INET_POST_ROUTING, skb, NULL, dev,
  149. ip6_finish_output,
  150. !(IP6CB(skb)->flags & IP6SKB_REROUTED));
  151. }
  152. /*
  153. * xmit an sk_buff (used by TCP, SCTP and DCCP)
  154. */
  155. int ip6_xmit(struct sock *sk, struct sk_buff *skb, struct flowi6 *fl6,
  156. struct ipv6_txoptions *opt, int tclass)
  157. {
  158. struct net *net = sock_net(sk);
  159. struct ipv6_pinfo *np = inet6_sk(sk);
  160. struct in6_addr *first_hop = &fl6->daddr;
  161. struct dst_entry *dst = skb_dst(skb);
  162. struct ipv6hdr *hdr;
  163. u8 proto = fl6->flowi6_proto;
  164. int seg_len = skb->len;
  165. int hlimit = -1;
  166. u32 mtu;
  167. if (opt) {
  168. unsigned int head_room;
  169. /* First: exthdrs may take lots of space (~8K for now)
  170. MAX_HEADER is not enough.
  171. */
  172. head_room = opt->opt_nflen + opt->opt_flen;
  173. seg_len += head_room;
  174. head_room += sizeof(struct ipv6hdr) + LL_RESERVED_SPACE(dst->dev);
  175. if (skb_headroom(skb) < head_room) {
  176. struct sk_buff *skb2 = skb_realloc_headroom(skb, head_room);
  177. if (skb2 == NULL) {
  178. IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  179. IPSTATS_MIB_OUTDISCARDS);
  180. kfree_skb(skb);
  181. return -ENOBUFS;
  182. }
  183. kfree_skb(skb);
  184. skb = skb2;
  185. skb_set_owner_w(skb, sk);
  186. }
  187. if (opt->opt_flen)
  188. ipv6_push_frag_opts(skb, opt, &proto);
  189. if (opt->opt_nflen)
  190. ipv6_push_nfrag_opts(skb, opt, &proto, &first_hop);
  191. }
  192. skb_push(skb, sizeof(struct ipv6hdr));
  193. skb_reset_network_header(skb);
  194. hdr = ipv6_hdr(skb);
  195. /*
  196. * Fill in the IPv6 header
  197. */
  198. if (np)
  199. hlimit = np->hop_limit;
  200. if (hlimit < 0)
  201. hlimit = ip6_dst_hoplimit(dst);
  202. *(__be32 *)hdr = htonl(0x60000000 | (tclass << 20)) | fl6->flowlabel;
  203. hdr->payload_len = htons(seg_len);
  204. hdr->nexthdr = proto;
  205. hdr->hop_limit = hlimit;
  206. hdr->saddr = fl6->saddr;
  207. hdr->daddr = *first_hop;
  208. skb->priority = sk->sk_priority;
  209. skb->mark = sk->sk_mark;
  210. mtu = dst_mtu(dst);
  211. if ((skb->len <= mtu) || skb->local_df || skb_is_gso(skb)) {
  212. IP6_UPD_PO_STATS(net, ip6_dst_idev(skb_dst(skb)),
  213. IPSTATS_MIB_OUT, skb->len);
  214. return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, skb, NULL,
  215. dst->dev, dst_output);
  216. }
  217. if (net_ratelimit())
  218. printk(KERN_DEBUG "IPv6: sending pkt_too_big to self\n");
  219. skb->dev = dst->dev;
  220. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  221. IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_FRAGFAILS);
  222. kfree_skb(skb);
  223. return -EMSGSIZE;
  224. }
  225. EXPORT_SYMBOL(ip6_xmit);
  226. /*
  227. * To avoid extra problems ND packets are send through this
  228. * routine. It's code duplication but I really want to avoid
  229. * extra checks since ipv6_build_header is used by TCP (which
  230. * is for us performance critical)
  231. */
  232. int ip6_nd_hdr(struct sock *sk, struct sk_buff *skb, struct net_device *dev,
  233. const struct in6_addr *saddr, const struct in6_addr *daddr,
  234. int proto, int len)
  235. {
  236. struct ipv6_pinfo *np = inet6_sk(sk);
  237. struct ipv6hdr *hdr;
  238. skb->protocol = htons(ETH_P_IPV6);
  239. skb->dev = dev;
  240. skb_reset_network_header(skb);
  241. skb_put(skb, sizeof(struct ipv6hdr));
  242. hdr = ipv6_hdr(skb);
  243. *(__be32*)hdr = htonl(0x60000000);
  244. hdr->payload_len = htons(len);
  245. hdr->nexthdr = proto;
  246. hdr->hop_limit = np->hop_limit;
  247. hdr->saddr = *saddr;
  248. hdr->daddr = *daddr;
  249. return 0;
  250. }
  251. static int ip6_call_ra_chain(struct sk_buff *skb, int sel)
  252. {
  253. struct ip6_ra_chain *ra;
  254. struct sock *last = NULL;
  255. read_lock(&ip6_ra_lock);
  256. for (ra = ip6_ra_chain; ra; ra = ra->next) {
  257. struct sock *sk = ra->sk;
  258. if (sk && ra->sel == sel &&
  259. (!sk->sk_bound_dev_if ||
  260. sk->sk_bound_dev_if == skb->dev->ifindex)) {
  261. if (last) {
  262. struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
  263. if (skb2)
  264. rawv6_rcv(last, skb2);
  265. }
  266. last = sk;
  267. }
  268. }
  269. if (last) {
  270. rawv6_rcv(last, skb);
  271. read_unlock(&ip6_ra_lock);
  272. return 1;
  273. }
  274. read_unlock(&ip6_ra_lock);
  275. return 0;
  276. }
  277. static int ip6_forward_proxy_check(struct sk_buff *skb)
  278. {
  279. struct ipv6hdr *hdr = ipv6_hdr(skb);
  280. u8 nexthdr = hdr->nexthdr;
  281. __be16 frag_off;
  282. int offset;
  283. if (ipv6_ext_hdr(nexthdr)) {
  284. offset = ipv6_skip_exthdr(skb, sizeof(*hdr), &nexthdr, &frag_off);
  285. if (offset < 0)
  286. return 0;
  287. } else
  288. offset = sizeof(struct ipv6hdr);
  289. if (nexthdr == IPPROTO_ICMPV6) {
  290. struct icmp6hdr *icmp6;
  291. if (!pskb_may_pull(skb, (skb_network_header(skb) +
  292. offset + 1 - skb->data)))
  293. return 0;
  294. icmp6 = (struct icmp6hdr *)(skb_network_header(skb) + offset);
  295. switch (icmp6->icmp6_type) {
  296. case NDISC_ROUTER_SOLICITATION:
  297. case NDISC_ROUTER_ADVERTISEMENT:
  298. case NDISC_NEIGHBOUR_SOLICITATION:
  299. case NDISC_NEIGHBOUR_ADVERTISEMENT:
  300. case NDISC_REDIRECT:
  301. /* For reaction involving unicast neighbor discovery
  302. * message destined to the proxied address, pass it to
  303. * input function.
  304. */
  305. return 1;
  306. default:
  307. break;
  308. }
  309. }
  310. /*
  311. * The proxying router can't forward traffic sent to a link-local
  312. * address, so signal the sender and discard the packet. This
  313. * behavior is clarified by the MIPv6 specification.
  314. */
  315. if (ipv6_addr_type(&hdr->daddr) & IPV6_ADDR_LINKLOCAL) {
  316. dst_link_failure(skb);
  317. return -1;
  318. }
  319. return 0;
  320. }
  321. static inline int ip6_forward_finish(struct sk_buff *skb)
  322. {
  323. return dst_output(skb);
  324. }
  325. int ip6_forward(struct sk_buff *skb)
  326. {
  327. struct dst_entry *dst = skb_dst(skb);
  328. struct ipv6hdr *hdr = ipv6_hdr(skb);
  329. struct inet6_skb_parm *opt = IP6CB(skb);
  330. struct net *net = dev_net(dst->dev);
  331. struct neighbour *n;
  332. u32 mtu;
  333. if (net->ipv6.devconf_all->forwarding == 0)
  334. goto error;
  335. if (skb_warn_if_lro(skb))
  336. goto drop;
  337. if (!xfrm6_policy_check(NULL, XFRM_POLICY_FWD, skb)) {
  338. IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_INDISCARDS);
  339. goto drop;
  340. }
  341. if (skb->pkt_type != PACKET_HOST)
  342. goto drop;
  343. skb_forward_csum(skb);
  344. /*
  345. * We DO NOT make any processing on
  346. * RA packets, pushing them to user level AS IS
  347. * without ane WARRANTY that application will be able
  348. * to interpret them. The reason is that we
  349. * cannot make anything clever here.
  350. *
  351. * We are not end-node, so that if packet contains
  352. * AH/ESP, we cannot make anything.
  353. * Defragmentation also would be mistake, RA packets
  354. * cannot be fragmented, because there is no warranty
  355. * that different fragments will go along one path. --ANK
  356. */
  357. if (opt->ra) {
  358. u8 *ptr = skb_network_header(skb) + opt->ra;
  359. if (ip6_call_ra_chain(skb, (ptr[2]<<8) + ptr[3]))
  360. return 0;
  361. }
  362. /*
  363. * check and decrement ttl
  364. */
  365. if (hdr->hop_limit <= 1) {
  366. /* Force OUTPUT device used as source address */
  367. skb->dev = dst->dev;
  368. icmpv6_send(skb, ICMPV6_TIME_EXCEED, ICMPV6_EXC_HOPLIMIT, 0);
  369. IP6_INC_STATS_BH(net,
  370. ip6_dst_idev(dst), IPSTATS_MIB_INHDRERRORS);
  371. kfree_skb(skb);
  372. return -ETIMEDOUT;
  373. }
  374. /* XXX: idev->cnf.proxy_ndp? */
  375. if (net->ipv6.devconf_all->proxy_ndp &&
  376. pneigh_lookup(&nd_tbl, net, &hdr->daddr, skb->dev, 0)) {
  377. int proxied = ip6_forward_proxy_check(skb);
  378. if (proxied > 0)
  379. return ip6_input(skb);
  380. else if (proxied < 0) {
  381. IP6_INC_STATS(net, ip6_dst_idev(dst),
  382. IPSTATS_MIB_INDISCARDS);
  383. goto drop;
  384. }
  385. }
  386. if (!xfrm6_route_forward(skb)) {
  387. IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_INDISCARDS);
  388. goto drop;
  389. }
  390. dst = skb_dst(skb);
  391. /* IPv6 specs say nothing about it, but it is clear that we cannot
  392. send redirects to source routed frames.
  393. We don't send redirects to frames decapsulated from IPsec.
  394. */
  395. n = dst_get_neighbour_noref(dst);
  396. if (skb->dev == dst->dev && n && opt->srcrt == 0 && !skb_sec_path(skb)) {
  397. struct in6_addr *target = NULL;
  398. struct rt6_info *rt;
  399. /*
  400. * incoming and outgoing devices are the same
  401. * send a redirect.
  402. */
  403. rt = (struct rt6_info *) dst;
  404. if ((rt->rt6i_flags & RTF_GATEWAY))
  405. target = (struct in6_addr*)&n->primary_key;
  406. else
  407. target = &hdr->daddr;
  408. if (!rt->rt6i_peer)
  409. rt6_bind_peer(rt, 1);
  410. /* Limit redirects both by destination (here)
  411. and by source (inside ndisc_send_redirect)
  412. */
  413. if (inet_peer_xrlim_allow(rt->rt6i_peer, 1*HZ))
  414. ndisc_send_redirect(skb, n, target);
  415. } else {
  416. int addrtype = ipv6_addr_type(&hdr->saddr);
  417. /* This check is security critical. */
  418. if (addrtype == IPV6_ADDR_ANY ||
  419. addrtype & (IPV6_ADDR_MULTICAST | IPV6_ADDR_LOOPBACK))
  420. goto error;
  421. if (addrtype & IPV6_ADDR_LINKLOCAL) {
  422. icmpv6_send(skb, ICMPV6_DEST_UNREACH,
  423. ICMPV6_NOT_NEIGHBOUR, 0);
  424. goto error;
  425. }
  426. }
  427. mtu = dst_mtu(dst);
  428. if (mtu < IPV6_MIN_MTU)
  429. mtu = IPV6_MIN_MTU;
  430. if (skb->len > mtu && !skb_is_gso(skb)) {
  431. /* Again, force OUTPUT device used as source address */
  432. skb->dev = dst->dev;
  433. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  434. IP6_INC_STATS_BH(net,
  435. ip6_dst_idev(dst), IPSTATS_MIB_INTOOBIGERRORS);
  436. IP6_INC_STATS_BH(net,
  437. ip6_dst_idev(dst), IPSTATS_MIB_FRAGFAILS);
  438. kfree_skb(skb);
  439. return -EMSGSIZE;
  440. }
  441. if (skb_cow(skb, dst->dev->hard_header_len)) {
  442. IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTDISCARDS);
  443. goto drop;
  444. }
  445. hdr = ipv6_hdr(skb);
  446. /* Mangling hops number delayed to point after skb COW */
  447. hdr->hop_limit--;
  448. IP6_INC_STATS_BH(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTFORWDATAGRAMS);
  449. return NF_HOOK(NFPROTO_IPV6, NF_INET_FORWARD, skb, skb->dev, dst->dev,
  450. ip6_forward_finish);
  451. error:
  452. IP6_INC_STATS_BH(net, ip6_dst_idev(dst), IPSTATS_MIB_INADDRERRORS);
  453. drop:
  454. kfree_skb(skb);
  455. return -EINVAL;
  456. }
  457. static void ip6_copy_metadata(struct sk_buff *to, struct sk_buff *from)
  458. {
  459. to->pkt_type = from->pkt_type;
  460. to->priority = from->priority;
  461. to->protocol = from->protocol;
  462. skb_dst_drop(to);
  463. skb_dst_set(to, dst_clone(skb_dst(from)));
  464. to->dev = from->dev;
  465. to->mark = from->mark;
  466. #ifdef CONFIG_NET_SCHED
  467. to->tc_index = from->tc_index;
  468. #endif
  469. nf_copy(to, from);
  470. #if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
  471. defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
  472. to->nf_trace = from->nf_trace;
  473. #endif
  474. skb_copy_secmark(to, from);
  475. }
  476. int ip6_find_1stfragopt(struct sk_buff *skb, u8 **nexthdr)
  477. {
  478. u16 offset = sizeof(struct ipv6hdr);
  479. struct ipv6_opt_hdr *exthdr =
  480. (struct ipv6_opt_hdr *)(ipv6_hdr(skb) + 1);
  481. unsigned int packet_len = skb->tail - skb->network_header;
  482. int found_rhdr = 0;
  483. *nexthdr = &ipv6_hdr(skb)->nexthdr;
  484. while (offset + 1 <= packet_len) {
  485. switch (**nexthdr) {
  486. case NEXTHDR_HOP:
  487. break;
  488. case NEXTHDR_ROUTING:
  489. found_rhdr = 1;
  490. break;
  491. case NEXTHDR_DEST:
  492. #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
  493. if (ipv6_find_tlv(skb, offset, IPV6_TLV_HAO) >= 0)
  494. break;
  495. #endif
  496. if (found_rhdr)
  497. return offset;
  498. break;
  499. default :
  500. return offset;
  501. }
  502. offset += ipv6_optlen(exthdr);
  503. *nexthdr = &exthdr->nexthdr;
  504. exthdr = (struct ipv6_opt_hdr *)(skb_network_header(skb) +
  505. offset);
  506. }
  507. return offset;
  508. }
  509. void ipv6_select_ident(struct frag_hdr *fhdr, struct rt6_info *rt)
  510. {
  511. static atomic_t ipv6_fragmentation_id;
  512. int old, new;
  513. if (rt && !(rt->dst.flags & DST_NOPEER)) {
  514. struct inet_peer *peer;
  515. if (!rt->rt6i_peer)
  516. rt6_bind_peer(rt, 1);
  517. peer = rt->rt6i_peer;
  518. if (peer) {
  519. fhdr->identification = htonl(inet_getid(peer, 0));
  520. return;
  521. }
  522. }
  523. do {
  524. old = atomic_read(&ipv6_fragmentation_id);
  525. new = old + 1;
  526. if (!new)
  527. new = 1;
  528. } while (atomic_cmpxchg(&ipv6_fragmentation_id, old, new) != old);
  529. fhdr->identification = htonl(new);
  530. }
  531. int ip6_fragment(struct sk_buff *skb, int (*output)(struct sk_buff *))
  532. {
  533. struct sk_buff *frag;
  534. struct rt6_info *rt = (struct rt6_info*)skb_dst(skb);
  535. struct ipv6_pinfo *np = skb->sk ? inet6_sk(skb->sk) : NULL;
  536. struct ipv6hdr *tmp_hdr;
  537. struct frag_hdr *fh;
  538. unsigned int mtu, hlen, left, len;
  539. int hroom, troom;
  540. __be32 frag_id = 0;
  541. int ptr, offset = 0, err=0;
  542. u8 *prevhdr, nexthdr = 0;
  543. struct net *net = dev_net(skb_dst(skb)->dev);
  544. hlen = ip6_find_1stfragopt(skb, &prevhdr);
  545. nexthdr = *prevhdr;
  546. mtu = ip6_skb_dst_mtu(skb);
  547. /* We must not fragment if the socket is set to force MTU discovery
  548. * or if the skb it not generated by a local socket.
  549. */
  550. if (!skb->local_df && skb->len > mtu) {
  551. skb->dev = skb_dst(skb)->dev;
  552. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
  553. IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  554. IPSTATS_MIB_FRAGFAILS);
  555. kfree_skb(skb);
  556. return -EMSGSIZE;
  557. }
  558. if (np && np->frag_size < mtu) {
  559. if (np->frag_size)
  560. mtu = np->frag_size;
  561. }
  562. mtu -= hlen + sizeof(struct frag_hdr);
  563. if (skb_has_frag_list(skb)) {
  564. int first_len = skb_pagelen(skb);
  565. struct sk_buff *frag2;
  566. if (first_len - hlen > mtu ||
  567. ((first_len - hlen) & 7) ||
  568. skb_cloned(skb))
  569. goto slow_path;
  570. skb_walk_frags(skb, frag) {
  571. /* Correct geometry. */
  572. if (frag->len > mtu ||
  573. ((frag->len & 7) && frag->next) ||
  574. skb_headroom(frag) < hlen)
  575. goto slow_path_clean;
  576. /* Partially cloned skb? */
  577. if (skb_shared(frag))
  578. goto slow_path_clean;
  579. BUG_ON(frag->sk);
  580. if (skb->sk) {
  581. frag->sk = skb->sk;
  582. frag->destructor = sock_wfree;
  583. }
  584. skb->truesize -= frag->truesize;
  585. }
  586. err = 0;
  587. offset = 0;
  588. frag = skb_shinfo(skb)->frag_list;
  589. skb_frag_list_init(skb);
  590. /* BUILD HEADER */
  591. *prevhdr = NEXTHDR_FRAGMENT;
  592. tmp_hdr = kmemdup(skb_network_header(skb), hlen, GFP_ATOMIC);
  593. if (!tmp_hdr) {
  594. IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  595. IPSTATS_MIB_FRAGFAILS);
  596. return -ENOMEM;
  597. }
  598. __skb_pull(skb, hlen);
  599. fh = (struct frag_hdr*)__skb_push(skb, sizeof(struct frag_hdr));
  600. __skb_push(skb, hlen);
  601. skb_reset_network_header(skb);
  602. memcpy(skb_network_header(skb), tmp_hdr, hlen);
  603. ipv6_select_ident(fh, rt);
  604. fh->nexthdr = nexthdr;
  605. fh->reserved = 0;
  606. fh->frag_off = htons(IP6_MF);
  607. frag_id = fh->identification;
  608. first_len = skb_pagelen(skb);
  609. skb->data_len = first_len - skb_headlen(skb);
  610. skb->len = first_len;
  611. ipv6_hdr(skb)->payload_len = htons(first_len -
  612. sizeof(struct ipv6hdr));
  613. dst_hold(&rt->dst);
  614. for (;;) {
  615. /* Prepare header of the next frame,
  616. * before previous one went down. */
  617. if (frag) {
  618. frag->ip_summed = CHECKSUM_NONE;
  619. skb_reset_transport_header(frag);
  620. fh = (struct frag_hdr*)__skb_push(frag, sizeof(struct frag_hdr));
  621. __skb_push(frag, hlen);
  622. skb_reset_network_header(frag);
  623. memcpy(skb_network_header(frag), tmp_hdr,
  624. hlen);
  625. offset += skb->len - hlen - sizeof(struct frag_hdr);
  626. fh->nexthdr = nexthdr;
  627. fh->reserved = 0;
  628. fh->frag_off = htons(offset);
  629. if (frag->next != NULL)
  630. fh->frag_off |= htons(IP6_MF);
  631. fh->identification = frag_id;
  632. ipv6_hdr(frag)->payload_len =
  633. htons(frag->len -
  634. sizeof(struct ipv6hdr));
  635. ip6_copy_metadata(frag, skb);
  636. }
  637. err = output(skb);
  638. if(!err)
  639. IP6_INC_STATS(net, ip6_dst_idev(&rt->dst),
  640. IPSTATS_MIB_FRAGCREATES);
  641. if (err || !frag)
  642. break;
  643. skb = frag;
  644. frag = skb->next;
  645. skb->next = NULL;
  646. }
  647. kfree(tmp_hdr);
  648. if (err == 0) {
  649. IP6_INC_STATS(net, ip6_dst_idev(&rt->dst),
  650. IPSTATS_MIB_FRAGOKS);
  651. dst_release(&rt->dst);
  652. return 0;
  653. }
  654. while (frag) {
  655. skb = frag->next;
  656. kfree_skb(frag);
  657. frag = skb;
  658. }
  659. IP6_INC_STATS(net, ip6_dst_idev(&rt->dst),
  660. IPSTATS_MIB_FRAGFAILS);
  661. dst_release(&rt->dst);
  662. return err;
  663. slow_path_clean:
  664. skb_walk_frags(skb, frag2) {
  665. if (frag2 == frag)
  666. break;
  667. frag2->sk = NULL;
  668. frag2->destructor = NULL;
  669. skb->truesize += frag2->truesize;
  670. }
  671. }
  672. slow_path:
  673. left = skb->len - hlen; /* Space per frame */
  674. ptr = hlen; /* Where to start from */
  675. /*
  676. * Fragment the datagram.
  677. */
  678. *prevhdr = NEXTHDR_FRAGMENT;
  679. hroom = LL_RESERVED_SPACE(rt->dst.dev);
  680. troom = rt->dst.dev->needed_tailroom;
  681. /*
  682. * Keep copying data until we run out.
  683. */
  684. while(left > 0) {
  685. len = left;
  686. /* IF: it doesn't fit, use 'mtu' - the data space left */
  687. if (len > mtu)
  688. len = mtu;
  689. /* IF: we are not sending up to and including the packet end
  690. then align the next start on an eight byte boundary */
  691. if (len < left) {
  692. len &= ~7;
  693. }
  694. /*
  695. * Allocate buffer.
  696. */
  697. if ((frag = alloc_skb(len + hlen + sizeof(struct frag_hdr) +
  698. hroom + troom, GFP_ATOMIC)) == NULL) {
  699. NETDEBUG(KERN_INFO "IPv6: frag: no memory for new fragment!\n");
  700. IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  701. IPSTATS_MIB_FRAGFAILS);
  702. err = -ENOMEM;
  703. goto fail;
  704. }
  705. /*
  706. * Set up data on packet
  707. */
  708. ip6_copy_metadata(frag, skb);
  709. skb_reserve(frag, hroom);
  710. skb_put(frag, len + hlen + sizeof(struct frag_hdr));
  711. skb_reset_network_header(frag);
  712. fh = (struct frag_hdr *)(skb_network_header(frag) + hlen);
  713. frag->transport_header = (frag->network_header + hlen +
  714. sizeof(struct frag_hdr));
  715. /*
  716. * Charge the memory for the fragment to any owner
  717. * it might possess
  718. */
  719. if (skb->sk)
  720. skb_set_owner_w(frag, skb->sk);
  721. /*
  722. * Copy the packet header into the new buffer.
  723. */
  724. skb_copy_from_linear_data(skb, skb_network_header(frag), hlen);
  725. /*
  726. * Build fragment header.
  727. */
  728. fh->nexthdr = nexthdr;
  729. fh->reserved = 0;
  730. if (!frag_id) {
  731. ipv6_select_ident(fh, rt);
  732. frag_id = fh->identification;
  733. } else
  734. fh->identification = frag_id;
  735. /*
  736. * Copy a block of the IP datagram.
  737. */
  738. if (skb_copy_bits(skb, ptr, skb_transport_header(frag), len))
  739. BUG();
  740. left -= len;
  741. fh->frag_off = htons(offset);
  742. if (left > 0)
  743. fh->frag_off |= htons(IP6_MF);
  744. ipv6_hdr(frag)->payload_len = htons(frag->len -
  745. sizeof(struct ipv6hdr));
  746. ptr += len;
  747. offset += len;
  748. /*
  749. * Put this fragment into the sending queue.
  750. */
  751. err = output(frag);
  752. if (err)
  753. goto fail;
  754. IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  755. IPSTATS_MIB_FRAGCREATES);
  756. }
  757. IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  758. IPSTATS_MIB_FRAGOKS);
  759. kfree_skb(skb);
  760. return err;
  761. fail:
  762. IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
  763. IPSTATS_MIB_FRAGFAILS);
  764. kfree_skb(skb);
  765. return err;
  766. }
  767. static inline int ip6_rt_check(const struct rt6key *rt_key,
  768. const struct in6_addr *fl_addr,
  769. const struct in6_addr *addr_cache)
  770. {
  771. return (rt_key->plen != 128 || !ipv6_addr_equal(fl_addr, &rt_key->addr)) &&
  772. (addr_cache == NULL || !ipv6_addr_equal(fl_addr, addr_cache));
  773. }
  774. static struct dst_entry *ip6_sk_dst_check(struct sock *sk,
  775. struct dst_entry *dst,
  776. const struct flowi6 *fl6)
  777. {
  778. struct ipv6_pinfo *np = inet6_sk(sk);
  779. struct rt6_info *rt = (struct rt6_info *)dst;
  780. if (!dst)
  781. goto out;
  782. /* Yes, checking route validity in not connected
  783. * case is not very simple. Take into account,
  784. * that we do not support routing by source, TOS,
  785. * and MSG_DONTROUTE --ANK (980726)
  786. *
  787. * 1. ip6_rt_check(): If route was host route,
  788. * check that cached destination is current.
  789. * If it is network route, we still may
  790. * check its validity using saved pointer
  791. * to the last used address: daddr_cache.
  792. * We do not want to save whole address now,
  793. * (because main consumer of this service
  794. * is tcp, which has not this problem),
  795. * so that the last trick works only on connected
  796. * sockets.
  797. * 2. oif also should be the same.
  798. */
  799. if (ip6_rt_check(&rt->rt6i_dst, &fl6->daddr, np->daddr_cache) ||
  800. #ifdef CONFIG_IPV6_SUBTREES
  801. ip6_rt_check(&rt->rt6i_src, &fl6->saddr, np->saddr_cache) ||
  802. #endif
  803. (fl6->flowi6_oif && fl6->flowi6_oif != dst->dev->ifindex)) {
  804. dst_release(dst);
  805. dst = NULL;
  806. }
  807. out:
  808. return dst;
  809. }
  810. static int ip6_dst_lookup_tail(struct sock *sk,
  811. struct dst_entry **dst, struct flowi6 *fl6)
  812. {
  813. struct net *net = sock_net(sk);
  814. #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
  815. struct neighbour *n;
  816. #endif
  817. int err;
  818. if (*dst == NULL)
  819. *dst = ip6_route_output(net, sk, fl6);
  820. if ((err = (*dst)->error))
  821. goto out_err_release;
  822. if (ipv6_addr_any(&fl6->saddr)) {
  823. struct rt6_info *rt = (struct rt6_info *) *dst;
  824. err = ip6_route_get_saddr(net, rt, &fl6->daddr,
  825. sk ? inet6_sk(sk)->srcprefs : 0,
  826. &fl6->saddr);
  827. if (err)
  828. goto out_err_release;
  829. }
  830. #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
  831. /*
  832. * Here if the dst entry we've looked up
  833. * has a neighbour entry that is in the INCOMPLETE
  834. * state and the src address from the flow is
  835. * marked as OPTIMISTIC, we release the found
  836. * dst entry and replace it instead with the
  837. * dst entry of the nexthop router
  838. */
  839. rcu_read_lock();
  840. n = dst_get_neighbour_noref(*dst);
  841. if (n && !(n->nud_state & NUD_VALID)) {
  842. struct inet6_ifaddr *ifp;
  843. struct flowi6 fl_gw6;
  844. int redirect;
  845. rcu_read_unlock();
  846. ifp = ipv6_get_ifaddr(net, &fl6->saddr,
  847. (*dst)->dev, 1);
  848. redirect = (ifp && ifp->flags & IFA_F_OPTIMISTIC);
  849. if (ifp)
  850. in6_ifa_put(ifp);
  851. if (redirect) {
  852. /*
  853. * We need to get the dst entry for the
  854. * default router instead
  855. */
  856. dst_release(*dst);
  857. memcpy(&fl_gw6, fl6, sizeof(struct flowi6));
  858. memset(&fl_gw6.daddr, 0, sizeof(struct in6_addr));
  859. *dst = ip6_route_output(net, sk, &fl_gw6);
  860. if ((err = (*dst)->error))
  861. goto out_err_release;
  862. }
  863. } else {
  864. rcu_read_unlock();
  865. }
  866. #endif
  867. return 0;
  868. out_err_release:
  869. if (err == -ENETUNREACH)
  870. IP6_INC_STATS_BH(net, NULL, IPSTATS_MIB_OUTNOROUTES);
  871. dst_release(*dst);
  872. *dst = NULL;
  873. return err;
  874. }
  875. /**
  876. * ip6_dst_lookup - perform route lookup on flow
  877. * @sk: socket which provides route info
  878. * @dst: pointer to dst_entry * for result
  879. * @fl6: flow to lookup
  880. *
  881. * This function performs a route lookup on the given flow.
  882. *
  883. * It returns zero on success, or a standard errno code on error.
  884. */
  885. int ip6_dst_lookup(struct sock *sk, struct dst_entry **dst, struct flowi6 *fl6)
  886. {
  887. *dst = NULL;
  888. return ip6_dst_lookup_tail(sk, dst, fl6);
  889. }
  890. EXPORT_SYMBOL_GPL(ip6_dst_lookup);
  891. /**
  892. * ip6_dst_lookup_flow - perform route lookup on flow with ipsec
  893. * @sk: socket which provides route info
  894. * @fl6: flow to lookup
  895. * @final_dst: final destination address for ipsec lookup
  896. * @can_sleep: we are in a sleepable context
  897. *
  898. * This function performs a route lookup on the given flow.
  899. *
  900. * It returns a valid dst pointer on success, or a pointer encoded
  901. * error code.
  902. */
  903. struct dst_entry *ip6_dst_lookup_flow(struct sock *sk, struct flowi6 *fl6,
  904. const struct in6_addr *final_dst,
  905. bool can_sleep)
  906. {
  907. struct dst_entry *dst = NULL;
  908. int err;
  909. err = ip6_dst_lookup_tail(sk, &dst, fl6);
  910. if (err)
  911. return ERR_PTR(err);
  912. if (final_dst)
  913. fl6->daddr = *final_dst;
  914. if (can_sleep)
  915. fl6->flowi6_flags |= FLOWI_FLAG_CAN_SLEEP;
  916. return xfrm_lookup(sock_net(sk), dst, flowi6_to_flowi(fl6), sk, 0);
  917. }
  918. EXPORT_SYMBOL_GPL(ip6_dst_lookup_flow);
  919. /**
  920. * ip6_sk_dst_lookup_flow - perform socket cached route lookup on flow
  921. * @sk: socket which provides the dst cache and route info
  922. * @fl6: flow to lookup
  923. * @final_dst: final destination address for ipsec lookup
  924. * @can_sleep: we are in a sleepable context
  925. *
  926. * This function performs a route lookup on the given flow with the
  927. * possibility of using the cached route in the socket if it is valid.
  928. * It will take the socket dst lock when operating on the dst cache.
  929. * As a result, this function can only be used in process context.
  930. *
  931. * It returns a valid dst pointer on success, or a pointer encoded
  932. * error code.
  933. */
  934. struct dst_entry *ip6_sk_dst_lookup_flow(struct sock *sk, struct flowi6 *fl6,
  935. const struct in6_addr *final_dst,
  936. bool can_sleep)
  937. {
  938. struct dst_entry *dst = sk_dst_check(sk, inet6_sk(sk)->dst_cookie);
  939. int err;
  940. dst = ip6_sk_dst_check(sk, dst, fl6);
  941. err = ip6_dst_lookup_tail(sk, &dst, fl6);
  942. if (err)
  943. return ERR_PTR(err);
  944. if (final_dst)
  945. fl6->daddr = *final_dst;
  946. if (can_sleep)
  947. fl6->flowi6_flags |= FLOWI_FLAG_CAN_SLEEP;
  948. return xfrm_lookup(sock_net(sk), dst, flowi6_to_flowi(fl6), sk, 0);
  949. }
  950. EXPORT_SYMBOL_GPL(ip6_sk_dst_lookup_flow);
  951. static inline int ip6_ufo_append_data(struct sock *sk,
  952. int getfrag(void *from, char *to, int offset, int len,
  953. int odd, struct sk_buff *skb),
  954. void *from, int length, int hh_len, int fragheaderlen,
  955. int transhdrlen, int mtu,unsigned int flags,
  956. struct rt6_info *rt)
  957. {
  958. struct sk_buff *skb;
  959. int err;
  960. /* There is support for UDP large send offload by network
  961. * device, so create one single skb packet containing complete
  962. * udp datagram
  963. */
  964. if ((skb = skb_peek_tail(&sk->sk_write_queue)) == NULL) {
  965. skb = sock_alloc_send_skb(sk,
  966. hh_len + fragheaderlen + transhdrlen + 20,
  967. (flags & MSG_DONTWAIT), &err);
  968. if (skb == NULL)
  969. return err;
  970. /* reserve space for Hardware header */
  971. skb_reserve(skb, hh_len);
  972. /* create space for UDP/IP header */
  973. skb_put(skb,fragheaderlen + transhdrlen);
  974. /* initialize network header pointer */
  975. skb_reset_network_header(skb);
  976. /* initialize protocol header pointer */
  977. skb->transport_header = skb->network_header + fragheaderlen;
  978. skb->ip_summed = CHECKSUM_PARTIAL;
  979. skb->csum = 0;
  980. }
  981. err = skb_append_datato_frags(sk,skb, getfrag, from,
  982. (length - transhdrlen));
  983. if (!err) {
  984. struct frag_hdr fhdr;
  985. /* Specify the length of each IPv6 datagram fragment.
  986. * It has to be a multiple of 8.
  987. */
  988. skb_shinfo(skb)->gso_size = (mtu - fragheaderlen -
  989. sizeof(struct frag_hdr)) & ~7;
  990. skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
  991. ipv6_select_ident(&fhdr, rt);
  992. skb_shinfo(skb)->ip6_frag_id = fhdr.identification;
  993. __skb_queue_tail(&sk->sk_write_queue, skb);
  994. return 0;
  995. }
  996. /* There is not enough support do UPD LSO,
  997. * so follow normal path
  998. */
  999. kfree_skb(skb);
  1000. return err;
  1001. }
  1002. static inline struct ipv6_opt_hdr *ip6_opt_dup(struct ipv6_opt_hdr *src,
  1003. gfp_t gfp)
  1004. {
  1005. return src ? kmemdup(src, (src->hdrlen + 1) * 8, gfp) : NULL;
  1006. }
  1007. static inline struct ipv6_rt_hdr *ip6_rthdr_dup(struct ipv6_rt_hdr *src,
  1008. gfp_t gfp)
  1009. {
  1010. return src ? kmemdup(src, (src->hdrlen + 1) * 8, gfp) : NULL;
  1011. }
  1012. int ip6_append_data(struct sock *sk, int getfrag(void *from, char *to,
  1013. int offset, int len, int odd, struct sk_buff *skb),
  1014. void *from, int length, int transhdrlen,
  1015. int hlimit, int tclass, struct ipv6_txoptions *opt, struct flowi6 *fl6,
  1016. struct rt6_info *rt, unsigned int flags, int dontfrag)
  1017. {
  1018. struct inet_sock *inet = inet_sk(sk);
  1019. struct ipv6_pinfo *np = inet6_sk(sk);
  1020. struct inet_cork *cork;
  1021. struct sk_buff *skb;
  1022. unsigned int maxfraglen, fragheaderlen;
  1023. int exthdrlen;
  1024. int dst_exthdrlen;
  1025. int hh_len;
  1026. int mtu;
  1027. int copy;
  1028. int err;
  1029. int offset = 0;
  1030. int csummode = CHECKSUM_NONE;
  1031. __u8 tx_flags = 0;
  1032. if (flags&MSG_PROBE)
  1033. return 0;
  1034. cork = &inet->cork.base;
  1035. if (skb_queue_empty(&sk->sk_write_queue)) {
  1036. /*
  1037. * setup for corking
  1038. */
  1039. if (opt) {
  1040. if (WARN_ON(np->cork.opt))
  1041. return -EINVAL;
  1042. np->cork.opt = kmalloc(opt->tot_len, sk->sk_allocation);
  1043. if (unlikely(np->cork.opt == NULL))
  1044. return -ENOBUFS;
  1045. np->cork.opt->tot_len = opt->tot_len;
  1046. np->cork.opt->opt_flen = opt->opt_flen;
  1047. np->cork.opt->opt_nflen = opt->opt_nflen;
  1048. np->cork.opt->dst0opt = ip6_opt_dup(opt->dst0opt,
  1049. sk->sk_allocation);
  1050. if (opt->dst0opt && !np->cork.opt->dst0opt)
  1051. return -ENOBUFS;
  1052. np->cork.opt->dst1opt = ip6_opt_dup(opt->dst1opt,
  1053. sk->sk_allocation);
  1054. if (opt->dst1opt && !np->cork.opt->dst1opt)
  1055. return -ENOBUFS;
  1056. np->cork.opt->hopopt = ip6_opt_dup(opt->hopopt,
  1057. sk->sk_allocation);
  1058. if (opt->hopopt && !np->cork.opt->hopopt)
  1059. return -ENOBUFS;
  1060. np->cork.opt->srcrt = ip6_rthdr_dup(opt->srcrt,
  1061. sk->sk_allocation);
  1062. if (opt->srcrt && !np->cork.opt->srcrt)
  1063. return -ENOBUFS;
  1064. /* need source address above miyazawa*/
  1065. }
  1066. dst_hold(&rt->dst);
  1067. cork->dst = &rt->dst;
  1068. inet->cork.fl.u.ip6 = *fl6;
  1069. np->cork.hop_limit = hlimit;
  1070. np->cork.tclass = tclass;
  1071. mtu = np->pmtudisc == IPV6_PMTUDISC_PROBE ?
  1072. rt->dst.dev->mtu : dst_mtu(&rt->dst);
  1073. if (np->frag_size < mtu) {
  1074. if (np->frag_size)
  1075. mtu = np->frag_size;
  1076. }
  1077. cork->fragsize = mtu;
  1078. if (dst_allfrag(rt->dst.path))
  1079. cork->flags |= IPCORK_ALLFRAG;
  1080. cork->length = 0;
  1081. sk->sk_sndmsg_page = NULL;
  1082. sk->sk_sndmsg_off = 0;
  1083. exthdrlen = (opt ? opt->opt_flen : 0) - rt->rt6i_nfheader_len;
  1084. length += exthdrlen;
  1085. transhdrlen += exthdrlen;
  1086. dst_exthdrlen = rt->dst.header_len;
  1087. } else {
  1088. rt = (struct rt6_info *)cork->dst;
  1089. fl6 = &inet->cork.fl.u.ip6;
  1090. opt = np->cork.opt;
  1091. transhdrlen = 0;
  1092. exthdrlen = 0;
  1093. dst_exthdrlen = 0;
  1094. mtu = cork->fragsize;
  1095. }
  1096. hh_len = LL_RESERVED_SPACE(rt->dst.dev);
  1097. fragheaderlen = sizeof(struct ipv6hdr) + rt->rt6i_nfheader_len +
  1098. (opt ? opt->opt_nflen : 0);
  1099. maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen - sizeof(struct frag_hdr);
  1100. if (mtu <= sizeof(struct ipv6hdr) + IPV6_MAXPLEN) {
  1101. if (cork->length + length > sizeof(struct ipv6hdr) + IPV6_MAXPLEN - fragheaderlen) {
  1102. ipv6_local_error(sk, EMSGSIZE, fl6, mtu-exthdrlen);
  1103. return -EMSGSIZE;
  1104. }
  1105. }
  1106. /* For UDP, check if TX timestamp is enabled */
  1107. if (sk->sk_type == SOCK_DGRAM) {
  1108. err = sock_tx_timestamp(sk, &tx_flags);
  1109. if (err)
  1110. goto error;
  1111. }
  1112. /*
  1113. * Let's try using as much space as possible.
  1114. * Use MTU if total length of the message fits into the MTU.
  1115. * Otherwise, we need to reserve fragment header and
  1116. * fragment alignment (= 8-15 octects, in total).
  1117. *
  1118. * Note that we may need to "move" the data from the tail of
  1119. * of the buffer to the new fragment when we split
  1120. * the message.
  1121. *
  1122. * FIXME: It may be fragmented into multiple chunks
  1123. * at once if non-fragmentable extension headers
  1124. * are too large.
  1125. * --yoshfuji
  1126. */
  1127. cork->length += length;
  1128. if (length > mtu) {
  1129. int proto = sk->sk_protocol;
  1130. if (dontfrag && (proto == IPPROTO_UDP || proto == IPPROTO_RAW)){
  1131. ipv6_local_rxpmtu(sk, fl6, mtu-exthdrlen);
  1132. return -EMSGSIZE;
  1133. }
  1134. if (proto == IPPROTO_UDP &&
  1135. (rt->dst.dev->features & NETIF_F_UFO)) {
  1136. err = ip6_ufo_append_data(sk, getfrag, from, length,
  1137. hh_len, fragheaderlen,
  1138. transhdrlen, mtu, flags, rt);
  1139. if (err)
  1140. goto error;
  1141. return 0;
  1142. }
  1143. }
  1144. if ((skb = skb_peek_tail(&sk->sk_write_queue)) == NULL)
  1145. goto alloc_new_skb;
  1146. while (length > 0) {
  1147. /* Check if the remaining data fits into current packet. */
  1148. copy = (cork->length <= mtu && !(cork->flags & IPCORK_ALLFRAG) ? mtu : maxfraglen) - skb->len;
  1149. if (copy < length)
  1150. copy = maxfraglen - skb->len;
  1151. if (copy <= 0) {
  1152. char *data;
  1153. unsigned int datalen;
  1154. unsigned int fraglen;
  1155. unsigned int fraggap;
  1156. unsigned int alloclen;
  1157. struct sk_buff *skb_prev;
  1158. alloc_new_skb:
  1159. skb_prev = skb;
  1160. /* There's no room in the current skb */
  1161. if (skb_prev)
  1162. fraggap = skb_prev->len - maxfraglen;
  1163. else
  1164. fraggap = 0;
  1165. /*
  1166. * If remaining data exceeds the mtu,
  1167. * we know we need more fragment(s).
  1168. */
  1169. datalen = length + fraggap;
  1170. if (datalen > (cork->length <= mtu && !(cork->flags & IPCORK_ALLFRAG) ? mtu : maxfraglen) - fragheaderlen)
  1171. datalen = maxfraglen - fragheaderlen;
  1172. fraglen = datalen + fragheaderlen;
  1173. if ((flags & MSG_MORE) &&
  1174. !(rt->dst.dev->features&NETIF_F_SG))
  1175. alloclen = mtu;
  1176. else
  1177. alloclen = datalen + fragheaderlen;
  1178. alloclen += dst_exthdrlen;
  1179. /*
  1180. * The last fragment gets additional space at tail.
  1181. * Note: we overallocate on fragments with MSG_MODE
  1182. * because we have no idea if we're the last one.
  1183. */
  1184. if (datalen == length + fraggap)
  1185. alloclen += rt->dst.trailer_len;
  1186. /*
  1187. * We just reserve space for fragment header.
  1188. * Note: this may be overallocation if the message
  1189. * (without MSG_MORE) fits into the MTU.
  1190. */
  1191. alloclen += sizeof(struct frag_hdr);
  1192. if (transhdrlen) {
  1193. skb = sock_alloc_send_skb(sk,
  1194. alloclen + hh_len,
  1195. (flags & MSG_DONTWAIT), &err);
  1196. } else {
  1197. skb = NULL;
  1198. if (atomic_read(&sk->sk_wmem_alloc) <=
  1199. 2 * sk->sk_sndbuf)
  1200. skb = sock_wmalloc(sk,
  1201. alloclen + hh_len, 1,
  1202. sk->sk_allocation);
  1203. if (unlikely(skb == NULL))
  1204. err = -ENOBUFS;
  1205. else {
  1206. /* Only the initial fragment
  1207. * is time stamped.
  1208. */
  1209. tx_flags = 0;
  1210. }
  1211. }
  1212. if (skb == NULL)
  1213. goto error;
  1214. /*
  1215. * Fill in the control structures
  1216. */
  1217. skb->ip_summed = csummode;
  1218. skb->csum = 0;
  1219. /* reserve for fragmentation */
  1220. skb_reserve(skb, hh_len+sizeof(struct frag_hdr));
  1221. if (sk->sk_type == SOCK_DGRAM)
  1222. skb_shinfo(skb)->tx_flags = tx_flags;
  1223. /*
  1224. * Find where to start putting bytes
  1225. */
  1226. data = skb_put(skb, fraglen + dst_exthdrlen);
  1227. skb_set_network_header(skb, exthdrlen + dst_exthdrlen);
  1228. data += fragheaderlen + dst_exthdrlen;
  1229. skb->transport_header = (skb->network_header +
  1230. fragheaderlen);
  1231. if (fraggap) {
  1232. skb->csum = skb_copy_and_csum_bits(
  1233. skb_prev, maxfraglen,
  1234. data + transhdrlen, fraggap, 0);
  1235. skb_prev->csum = csum_sub(skb_prev->csum,
  1236. skb->csum);
  1237. data += fraggap;
  1238. pskb_trim_unique(skb_prev, maxfraglen);
  1239. }
  1240. copy = datalen - transhdrlen - fraggap;
  1241. if (copy < 0) {
  1242. err = -EINVAL;
  1243. kfree_skb(skb);
  1244. goto error;
  1245. } else if (copy > 0 && getfrag(from, data + transhdrlen, offset, copy, fraggap, skb) < 0) {
  1246. err = -EFAULT;
  1247. kfree_skb(skb);
  1248. goto error;
  1249. }
  1250. offset += copy;
  1251. length -= datalen - fraggap;
  1252. transhdrlen = 0;
  1253. exthdrlen = 0;
  1254. dst_exthdrlen = 0;
  1255. csummode = CHECKSUM_NONE;
  1256. /*
  1257. * Put the packet on the pending queue
  1258. */
  1259. __skb_queue_tail(&sk->sk_write_queue, skb);
  1260. continue;
  1261. }
  1262. if (copy > length)
  1263. copy = length;
  1264. if (!(rt->dst.dev->features&NETIF_F_SG)) {
  1265. unsigned int off;
  1266. off = skb->len;
  1267. if (getfrag(from, skb_put(skb, copy),
  1268. offset, copy, off, skb) < 0) {
  1269. __skb_trim(skb, off);
  1270. err = -EFAULT;
  1271. goto error;
  1272. }
  1273. } else {
  1274. int i = skb_shinfo(skb)->nr_frags;
  1275. skb_frag_t *frag = &skb_shinfo(skb)->frags[i-1];
  1276. struct page *page = sk->sk_sndmsg_page;
  1277. int off = sk->sk_sndmsg_off;
  1278. unsigned int left;
  1279. if (page && (left = PAGE_SIZE - off) > 0) {
  1280. if (copy >= left)
  1281. copy = left;
  1282. if (page != skb_frag_page(frag)) {
  1283. if (i == MAX_SKB_FRAGS) {
  1284. err = -EMSGSIZE;
  1285. goto error;
  1286. }
  1287. skb_fill_page_desc(skb, i, page, sk->sk_sndmsg_off, 0);
  1288. skb_frag_ref(skb, i);
  1289. frag = &skb_shinfo(skb)->frags[i];
  1290. }
  1291. } else if(i < MAX_SKB_FRAGS) {
  1292. if (copy > PAGE_SIZE)
  1293. copy = PAGE_SIZE;
  1294. page = alloc_pages(sk->sk_allocation, 0);
  1295. if (page == NULL) {
  1296. err = -ENOMEM;
  1297. goto error;
  1298. }
  1299. sk->sk_sndmsg_page = page;
  1300. sk->sk_sndmsg_off = 0;
  1301. skb_fill_page_desc(skb, i, page, 0, 0);
  1302. frag = &skb_shinfo(skb)->frags[i];
  1303. } else {
  1304. err = -EMSGSIZE;
  1305. goto error;
  1306. }
  1307. if (getfrag(from,
  1308. skb_frag_address(frag) + skb_frag_size(frag),
  1309. offset, copy, skb->len, skb) < 0) {
  1310. err = -EFAULT;
  1311. goto error;
  1312. }
  1313. sk->sk_sndmsg_off += copy;
  1314. skb_frag_size_add(frag, copy);
  1315. skb->len += copy;
  1316. skb->data_len += copy;
  1317. skb->truesize += copy;
  1318. atomic_add(copy, &sk->sk_wmem_alloc);
  1319. }
  1320. offset += copy;
  1321. length -= copy;
  1322. }
  1323. return 0;
  1324. error:
  1325. cork->length -= length;
  1326. IP6_INC_STATS(sock_net(sk), rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS);
  1327. return err;
  1328. }
  1329. static void ip6_cork_release(struct inet_sock *inet, struct ipv6_pinfo *np)
  1330. {
  1331. if (np->cork.opt) {
  1332. kfree(np->cork.opt->dst0opt);
  1333. kfree(np->cork.opt->dst1opt);
  1334. kfree(np->cork.opt->hopopt);
  1335. kfree(np->cork.opt->srcrt);
  1336. kfree(np->cork.opt);
  1337. np->cork.opt = NULL;
  1338. }
  1339. if (inet->cork.base.dst) {
  1340. dst_release(inet->cork.base.dst);
  1341. inet->cork.base.dst = NULL;
  1342. inet->cork.base.flags &= ~IPCORK_ALLFRAG;
  1343. }
  1344. memset(&inet->cork.fl, 0, sizeof(inet->cork.fl));
  1345. }
  1346. int ip6_push_pending_frames(struct sock *sk)
  1347. {
  1348. struct sk_buff *skb, *tmp_skb;
  1349. struct sk_buff **tail_skb;
  1350. struct in6_addr final_dst_buf, *final_dst = &final_dst_buf;
  1351. struct inet_sock *inet = inet_sk(sk);
  1352. struct ipv6_pinfo *np = inet6_sk(sk);
  1353. struct net *net = sock_net(sk);
  1354. struct ipv6hdr *hdr;
  1355. struct ipv6_txoptions *opt = np->cork.opt;
  1356. struct rt6_info *rt = (struct rt6_info *)inet->cork.base.dst;
  1357. struct flowi6 *fl6 = &inet->cork.fl.u.ip6;
  1358. unsigned char proto = fl6->flowi6_proto;
  1359. int err = 0;
  1360. if ((skb = __skb_dequeue(&sk->sk_write_queue)) == NULL)
  1361. goto out;
  1362. tail_skb = &(skb_shinfo(skb)->frag_list);
  1363. /* move skb->data to ip header from ext header */
  1364. if (skb->data < skb_network_header(skb))
  1365. __skb_pull(skb, skb_network_offset(skb));
  1366. while ((tmp_skb = __skb_dequeue(&sk->sk_write_queue)) != NULL) {
  1367. __skb_pull(tmp_skb, skb_network_header_len(skb));
  1368. *tail_skb = tmp_skb;
  1369. tail_skb = &(tmp_skb->next);
  1370. skb->len += tmp_skb->len;
  1371. skb->data_len += tmp_skb->len;
  1372. skb->truesize += tmp_skb->truesize;
  1373. tmp_skb->destructor = NULL;
  1374. tmp_skb->sk = NULL;
  1375. }
  1376. /* Allow local fragmentation. */
  1377. if (np->pmtudisc < IPV6_PMTUDISC_DO)
  1378. skb->local_df = 1;
  1379. *final_dst = fl6->daddr;
  1380. __skb_pull(skb, skb_network_header_len(skb));
  1381. if (opt && opt->opt_flen)
  1382. ipv6_push_frag_opts(skb, opt, &proto);
  1383. if (opt && opt->opt_nflen)
  1384. ipv6_push_nfrag_opts(skb, opt, &proto, &final_dst);
  1385. skb_push(skb, sizeof(struct ipv6hdr));
  1386. skb_reset_network_header(skb);
  1387. hdr = ipv6_hdr(skb);
  1388. *(__be32*)hdr = fl6->flowlabel |
  1389. htonl(0x60000000 | ((int)np->cork.tclass << 20));
  1390. hdr->hop_limit = np->cork.hop_limit;
  1391. hdr->nexthdr = proto;
  1392. hdr->saddr = fl6->saddr;
  1393. hdr->daddr = *final_dst;
  1394. skb->priority = sk->sk_priority;
  1395. skb->mark = sk->sk_mark;
  1396. skb_dst_set(skb, dst_clone(&rt->dst));
  1397. IP6_UPD_PO_STATS(net, rt->rt6i_idev, IPSTATS_MIB_OUT, skb->len);
  1398. if (proto == IPPROTO_ICMPV6) {
  1399. struct inet6_dev *idev = ip6_dst_idev(skb_dst(skb));
  1400. ICMP6MSGOUT_INC_STATS_BH(net, idev, icmp6_hdr(skb)->icmp6_type);
  1401. ICMP6_INC_STATS_BH(net, idev, ICMP6_MIB_OUTMSGS);
  1402. }
  1403. err = ip6_local_out(skb);
  1404. if (err) {
  1405. if (err > 0)
  1406. err = net_xmit_errno(err);
  1407. if (err)
  1408. goto error;
  1409. }
  1410. out:
  1411. ip6_cork_release(inet, np);
  1412. return err;
  1413. error:
  1414. IP6_INC_STATS(net, rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS);
  1415. goto out;
  1416. }
  1417. void ip6_flush_pending_frames(struct sock *sk)
  1418. {
  1419. struct sk_buff *skb;
  1420. while ((skb = __skb_dequeue_tail(&sk->sk_write_queue)) != NULL) {
  1421. if (skb_dst(skb))
  1422. IP6_INC_STATS(sock_net(sk), ip6_dst_idev(skb_dst(skb)),
  1423. IPSTATS_MIB_OUTDISCARDS);
  1424. kfree_skb(skb);
  1425. }
  1426. ip6_cork_release(inet_sk(sk), inet6_sk(sk));
  1427. }