ip6_output.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304
  1. /*
  2. * IPv6 output functions
  3. * Linux INET6 implementation
  4. *
  5. * Authors:
  6. * Pedro Roque <roque@di.fc.ul.pt>
  7. *
  8. * $Id: ip6_output.c,v 1.34 2002/02/01 22:01:04 davem Exp $
  9. *
  10. * Based on linux/net/ipv4/ip_output.c
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version
  15. * 2 of the License, or (at your option) any later version.
  16. *
  17. * Changes:
  18. * A.N.Kuznetsov : airthmetics in fragmentation.
  19. * extension headers are implemented.
  20. * route changes now work.
  21. * ip6_forward does not confuse sniffers.
  22. * etc.
  23. *
  24. * H. von Brand : Added missing #include <linux/string.h>
  25. * Imran Patel : frag id should be in NBO
  26. * Kazunori MIYAZAWA @USAGI
  27. * : add ip6_append_data and related functions
  28. * for datagram xmit
  29. */
  30. #include <linux/errno.h>
  31. #include <linux/types.h>
  32. #include <linux/string.h>
  33. #include <linux/socket.h>
  34. #include <linux/net.h>
  35. #include <linux/netdevice.h>
  36. #include <linux/if_arp.h>
  37. #include <linux/in6.h>
  38. #include <linux/tcp.h>
  39. #include <linux/route.h>
  40. #include <linux/module.h>
  41. #include <linux/netfilter.h>
  42. #include <linux/netfilter_ipv6.h>
  43. #include <net/sock.h>
  44. #include <net/snmp.h>
  45. #include <net/ipv6.h>
  46. #include <net/ndisc.h>
  47. #include <net/protocol.h>
  48. #include <net/ip6_route.h>
  49. #include <net/addrconf.h>
  50. #include <net/rawv6.h>
  51. #include <net/icmp.h>
  52. #include <net/xfrm.h>
  53. #include <net/checksum.h>
  54. static int ip6_fragment(struct sk_buff *skb, int (*output)(struct sk_buff *));
  55. static __inline__ void ipv6_select_ident(struct sk_buff *skb, struct frag_hdr *fhdr)
  56. {
  57. static u32 ipv6_fragmentation_id = 1;
  58. static DEFINE_SPINLOCK(ip6_id_lock);
  59. spin_lock_bh(&ip6_id_lock);
  60. fhdr->identification = htonl(ipv6_fragmentation_id);
  61. if (++ipv6_fragmentation_id == 0)
  62. ipv6_fragmentation_id = 1;
  63. spin_unlock_bh(&ip6_id_lock);
  64. }
  65. static inline int ip6_output_finish(struct sk_buff *skb)
  66. {
  67. struct dst_entry *dst = skb->dst;
  68. struct hh_cache *hh = dst->hh;
  69. if (hh) {
  70. int hh_alen;
  71. read_lock_bh(&hh->hh_lock);
  72. hh_alen = HH_DATA_ALIGN(hh->hh_len);
  73. memcpy(skb->data - hh_alen, hh->hh_data, hh_alen);
  74. read_unlock_bh(&hh->hh_lock);
  75. skb_push(skb, hh->hh_len);
  76. return hh->hh_output(skb);
  77. } else if (dst->neighbour)
  78. return dst->neighbour->output(skb);
  79. IP6_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES);
  80. kfree_skb(skb);
  81. return -EINVAL;
  82. }
  83. /* dev_loopback_xmit for use with netfilter. */
  84. static int ip6_dev_loopback_xmit(struct sk_buff *newskb)
  85. {
  86. newskb->mac.raw = newskb->data;
  87. __skb_pull(newskb, newskb->nh.raw - newskb->data);
  88. newskb->pkt_type = PACKET_LOOPBACK;
  89. newskb->ip_summed = CHECKSUM_UNNECESSARY;
  90. BUG_TRAP(newskb->dst);
  91. netif_rx(newskb);
  92. return 0;
  93. }
  94. static int ip6_output2(struct sk_buff *skb)
  95. {
  96. struct dst_entry *dst = skb->dst;
  97. struct net_device *dev = dst->dev;
  98. skb->protocol = htons(ETH_P_IPV6);
  99. skb->dev = dev;
  100. if (ipv6_addr_is_multicast(&skb->nh.ipv6h->daddr)) {
  101. struct ipv6_pinfo* np = skb->sk ? inet6_sk(skb->sk) : NULL;
  102. if (!(dev->flags & IFF_LOOPBACK) && (!np || np->mc_loop) &&
  103. ipv6_chk_mcast_addr(dev, &skb->nh.ipv6h->daddr,
  104. &skb->nh.ipv6h->saddr)) {
  105. struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
  106. /* Do not check for IFF_ALLMULTI; multicast routing
  107. is not supported in any case.
  108. */
  109. if (newskb)
  110. NF_HOOK(PF_INET6, NF_IP6_POST_ROUTING, newskb, NULL,
  111. newskb->dev,
  112. ip6_dev_loopback_xmit);
  113. if (skb->nh.ipv6h->hop_limit == 0) {
  114. IP6_INC_STATS(IPSTATS_MIB_OUTDISCARDS);
  115. kfree_skb(skb);
  116. return 0;
  117. }
  118. }
  119. IP6_INC_STATS(IPSTATS_MIB_OUTMCASTPKTS);
  120. }
  121. return NF_HOOK(PF_INET6, NF_IP6_POST_ROUTING, skb,NULL, skb->dev,ip6_output_finish);
  122. }
  123. int ip6_output(struct sk_buff *skb)
  124. {
  125. if ((skb->len > dst_mtu(skb->dst) && !skb_is_gso(skb)) ||
  126. dst_allfrag(skb->dst))
  127. return ip6_fragment(skb, ip6_output2);
  128. else
  129. return ip6_output2(skb);
  130. }
  131. /*
  132. * xmit an sk_buff (used by TCP)
  133. */
  134. int ip6_xmit(struct sock *sk, struct sk_buff *skb, struct flowi *fl,
  135. struct ipv6_txoptions *opt, int ipfragok)
  136. {
  137. struct ipv6_pinfo *np = inet6_sk(sk);
  138. struct in6_addr *first_hop = &fl->fl6_dst;
  139. struct dst_entry *dst = skb->dst;
  140. struct ipv6hdr *hdr;
  141. u8 proto = fl->proto;
  142. int seg_len = skb->len;
  143. int hlimit, tclass;
  144. u32 mtu;
  145. if (opt) {
  146. int head_room;
  147. /* First: exthdrs may take lots of space (~8K for now)
  148. MAX_HEADER is not enough.
  149. */
  150. head_room = opt->opt_nflen + opt->opt_flen;
  151. seg_len += head_room;
  152. head_room += sizeof(struct ipv6hdr) + LL_RESERVED_SPACE(dst->dev);
  153. if (skb_headroom(skb) < head_room) {
  154. struct sk_buff *skb2 = skb_realloc_headroom(skb, head_room);
  155. kfree_skb(skb);
  156. skb = skb2;
  157. if (skb == NULL) {
  158. IP6_INC_STATS(IPSTATS_MIB_OUTDISCARDS);
  159. return -ENOBUFS;
  160. }
  161. if (sk)
  162. skb_set_owner_w(skb, sk);
  163. }
  164. if (opt->opt_flen)
  165. ipv6_push_frag_opts(skb, opt, &proto);
  166. if (opt->opt_nflen)
  167. ipv6_push_nfrag_opts(skb, opt, &proto, &first_hop);
  168. }
  169. hdr = skb->nh.ipv6h = (struct ipv6hdr*)skb_push(skb, sizeof(struct ipv6hdr));
  170. /*
  171. * Fill in the IPv6 header
  172. */
  173. hlimit = -1;
  174. if (np)
  175. hlimit = np->hop_limit;
  176. if (hlimit < 0)
  177. hlimit = dst_metric(dst, RTAX_HOPLIMIT);
  178. if (hlimit < 0)
  179. hlimit = ipv6_get_hoplimit(dst->dev);
  180. tclass = -1;
  181. if (np)
  182. tclass = np->tclass;
  183. if (tclass < 0)
  184. tclass = 0;
  185. *(u32 *)hdr = htonl(0x60000000 | (tclass << 20)) | fl->fl6_flowlabel;
  186. hdr->payload_len = htons(seg_len);
  187. hdr->nexthdr = proto;
  188. hdr->hop_limit = hlimit;
  189. ipv6_addr_copy(&hdr->saddr, &fl->fl6_src);
  190. ipv6_addr_copy(&hdr->daddr, first_hop);
  191. skb->priority = sk->sk_priority;
  192. mtu = dst_mtu(dst);
  193. if ((skb->len <= mtu) || ipfragok || skb_is_gso(skb)) {
  194. IP6_INC_STATS(IPSTATS_MIB_OUTREQUESTS);
  195. return NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL, dst->dev,
  196. dst_output);
  197. }
  198. if (net_ratelimit())
  199. printk(KERN_DEBUG "IPv6: sending pkt_too_big to self\n");
  200. skb->dev = dst->dev;
  201. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, skb->dev);
  202. IP6_INC_STATS(IPSTATS_MIB_FRAGFAILS);
  203. kfree_skb(skb);
  204. return -EMSGSIZE;
  205. }
  206. /*
  207. * To avoid extra problems ND packets are send through this
  208. * routine. It's code duplication but I really want to avoid
  209. * extra checks since ipv6_build_header is used by TCP (which
  210. * is for us performance critical)
  211. */
  212. int ip6_nd_hdr(struct sock *sk, struct sk_buff *skb, struct net_device *dev,
  213. struct in6_addr *saddr, struct in6_addr *daddr,
  214. int proto, int len)
  215. {
  216. struct ipv6_pinfo *np = inet6_sk(sk);
  217. struct ipv6hdr *hdr;
  218. int totlen;
  219. skb->protocol = htons(ETH_P_IPV6);
  220. skb->dev = dev;
  221. totlen = len + sizeof(struct ipv6hdr);
  222. hdr = (struct ipv6hdr *) skb_put(skb, sizeof(struct ipv6hdr));
  223. skb->nh.ipv6h = hdr;
  224. *(u32*)hdr = htonl(0x60000000);
  225. hdr->payload_len = htons(len);
  226. hdr->nexthdr = proto;
  227. hdr->hop_limit = np->hop_limit;
  228. ipv6_addr_copy(&hdr->saddr, saddr);
  229. ipv6_addr_copy(&hdr->daddr, daddr);
  230. return 0;
  231. }
  232. static int ip6_call_ra_chain(struct sk_buff *skb, int sel)
  233. {
  234. struct ip6_ra_chain *ra;
  235. struct sock *last = NULL;
  236. read_lock(&ip6_ra_lock);
  237. for (ra = ip6_ra_chain; ra; ra = ra->next) {
  238. struct sock *sk = ra->sk;
  239. if (sk && ra->sel == sel &&
  240. (!sk->sk_bound_dev_if ||
  241. sk->sk_bound_dev_if == skb->dev->ifindex)) {
  242. if (last) {
  243. struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
  244. if (skb2)
  245. rawv6_rcv(last, skb2);
  246. }
  247. last = sk;
  248. }
  249. }
  250. if (last) {
  251. rawv6_rcv(last, skb);
  252. read_unlock(&ip6_ra_lock);
  253. return 1;
  254. }
  255. read_unlock(&ip6_ra_lock);
  256. return 0;
  257. }
  258. static inline int ip6_forward_finish(struct sk_buff *skb)
  259. {
  260. return dst_output(skb);
  261. }
  262. int ip6_forward(struct sk_buff *skb)
  263. {
  264. struct dst_entry *dst = skb->dst;
  265. struct ipv6hdr *hdr = skb->nh.ipv6h;
  266. struct inet6_skb_parm *opt = IP6CB(skb);
  267. if (ipv6_devconf.forwarding == 0)
  268. goto error;
  269. if (!xfrm6_policy_check(NULL, XFRM_POLICY_FWD, skb)) {
  270. IP6_INC_STATS(IPSTATS_MIB_INDISCARDS);
  271. goto drop;
  272. }
  273. skb->ip_summed = CHECKSUM_NONE;
  274. /*
  275. * We DO NOT make any processing on
  276. * RA packets, pushing them to user level AS IS
  277. * without ane WARRANTY that application will be able
  278. * to interpret them. The reason is that we
  279. * cannot make anything clever here.
  280. *
  281. * We are not end-node, so that if packet contains
  282. * AH/ESP, we cannot make anything.
  283. * Defragmentation also would be mistake, RA packets
  284. * cannot be fragmented, because there is no warranty
  285. * that different fragments will go along one path. --ANK
  286. */
  287. if (opt->ra) {
  288. u8 *ptr = skb->nh.raw + opt->ra;
  289. if (ip6_call_ra_chain(skb, (ptr[2]<<8) + ptr[3]))
  290. return 0;
  291. }
  292. /*
  293. * check and decrement ttl
  294. */
  295. if (hdr->hop_limit <= 1) {
  296. /* Force OUTPUT device used as source address */
  297. skb->dev = dst->dev;
  298. icmpv6_send(skb, ICMPV6_TIME_EXCEED, ICMPV6_EXC_HOPLIMIT,
  299. 0, skb->dev);
  300. IP6_INC_STATS_BH(IPSTATS_MIB_INHDRERRORS);
  301. kfree_skb(skb);
  302. return -ETIMEDOUT;
  303. }
  304. if (!xfrm6_route_forward(skb)) {
  305. IP6_INC_STATS(IPSTATS_MIB_INDISCARDS);
  306. goto drop;
  307. }
  308. dst = skb->dst;
  309. /* IPv6 specs say nothing about it, but it is clear that we cannot
  310. send redirects to source routed frames.
  311. */
  312. if (skb->dev == dst->dev && dst->neighbour && opt->srcrt == 0) {
  313. struct in6_addr *target = NULL;
  314. struct rt6_info *rt;
  315. struct neighbour *n = dst->neighbour;
  316. /*
  317. * incoming and outgoing devices are the same
  318. * send a redirect.
  319. */
  320. rt = (struct rt6_info *) dst;
  321. if ((rt->rt6i_flags & RTF_GATEWAY))
  322. target = (struct in6_addr*)&n->primary_key;
  323. else
  324. target = &hdr->daddr;
  325. /* Limit redirects both by destination (here)
  326. and by source (inside ndisc_send_redirect)
  327. */
  328. if (xrlim_allow(dst, 1*HZ))
  329. ndisc_send_redirect(skb, n, target);
  330. } else if (ipv6_addr_type(&hdr->saddr)&(IPV6_ADDR_MULTICAST|IPV6_ADDR_LOOPBACK
  331. |IPV6_ADDR_LINKLOCAL)) {
  332. /* This check is security critical. */
  333. goto error;
  334. }
  335. if (skb->len > dst_mtu(dst)) {
  336. /* Again, force OUTPUT device used as source address */
  337. skb->dev = dst->dev;
  338. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, dst_mtu(dst), skb->dev);
  339. IP6_INC_STATS_BH(IPSTATS_MIB_INTOOBIGERRORS);
  340. IP6_INC_STATS_BH(IPSTATS_MIB_FRAGFAILS);
  341. kfree_skb(skb);
  342. return -EMSGSIZE;
  343. }
  344. if (skb_cow(skb, dst->dev->hard_header_len)) {
  345. IP6_INC_STATS(IPSTATS_MIB_OUTDISCARDS);
  346. goto drop;
  347. }
  348. hdr = skb->nh.ipv6h;
  349. /* Mangling hops number delayed to point after skb COW */
  350. hdr->hop_limit--;
  351. IP6_INC_STATS_BH(IPSTATS_MIB_OUTFORWDATAGRAMS);
  352. return NF_HOOK(PF_INET6,NF_IP6_FORWARD, skb, skb->dev, dst->dev, ip6_forward_finish);
  353. error:
  354. IP6_INC_STATS_BH(IPSTATS_MIB_INADDRERRORS);
  355. drop:
  356. kfree_skb(skb);
  357. return -EINVAL;
  358. }
  359. static void ip6_copy_metadata(struct sk_buff *to, struct sk_buff *from)
  360. {
  361. to->pkt_type = from->pkt_type;
  362. to->priority = from->priority;
  363. to->protocol = from->protocol;
  364. dst_release(to->dst);
  365. to->dst = dst_clone(from->dst);
  366. to->dev = from->dev;
  367. #ifdef CONFIG_NET_SCHED
  368. to->tc_index = from->tc_index;
  369. #endif
  370. #ifdef CONFIG_NETFILTER
  371. to->nfmark = from->nfmark;
  372. /* Connection association is same as pre-frag packet */
  373. nf_conntrack_put(to->nfct);
  374. to->nfct = from->nfct;
  375. nf_conntrack_get(to->nfct);
  376. to->nfctinfo = from->nfctinfo;
  377. #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
  378. nf_conntrack_put_reasm(to->nfct_reasm);
  379. to->nfct_reasm = from->nfct_reasm;
  380. nf_conntrack_get_reasm(to->nfct_reasm);
  381. #endif
  382. #ifdef CONFIG_BRIDGE_NETFILTER
  383. nf_bridge_put(to->nf_bridge);
  384. to->nf_bridge = from->nf_bridge;
  385. nf_bridge_get(to->nf_bridge);
  386. #endif
  387. #endif
  388. skb_copy_secmark(to, from);
  389. }
  390. int ip6_find_1stfragopt(struct sk_buff *skb, u8 **nexthdr)
  391. {
  392. u16 offset = sizeof(struct ipv6hdr);
  393. struct ipv6_opt_hdr *exthdr = (struct ipv6_opt_hdr*)(skb->nh.ipv6h + 1);
  394. unsigned int packet_len = skb->tail - skb->nh.raw;
  395. int found_rhdr = 0;
  396. *nexthdr = &skb->nh.ipv6h->nexthdr;
  397. while (offset + 1 <= packet_len) {
  398. switch (**nexthdr) {
  399. case NEXTHDR_HOP:
  400. case NEXTHDR_ROUTING:
  401. case NEXTHDR_DEST:
  402. if (**nexthdr == NEXTHDR_ROUTING) found_rhdr = 1;
  403. if (**nexthdr == NEXTHDR_DEST && found_rhdr) return offset;
  404. offset += ipv6_optlen(exthdr);
  405. *nexthdr = &exthdr->nexthdr;
  406. exthdr = (struct ipv6_opt_hdr*)(skb->nh.raw + offset);
  407. break;
  408. default :
  409. return offset;
  410. }
  411. }
  412. return offset;
  413. }
  414. EXPORT_SYMBOL_GPL(ip6_find_1stfragopt);
  415. static int ip6_fragment(struct sk_buff *skb, int (*output)(struct sk_buff *))
  416. {
  417. struct net_device *dev;
  418. struct sk_buff *frag;
  419. struct rt6_info *rt = (struct rt6_info*)skb->dst;
  420. struct ipv6_pinfo *np = skb->sk ? inet6_sk(skb->sk) : NULL;
  421. struct ipv6hdr *tmp_hdr;
  422. struct frag_hdr *fh;
  423. unsigned int mtu, hlen, left, len;
  424. u32 frag_id = 0;
  425. int ptr, offset = 0, err=0;
  426. u8 *prevhdr, nexthdr = 0;
  427. dev = rt->u.dst.dev;
  428. hlen = ip6_find_1stfragopt(skb, &prevhdr);
  429. nexthdr = *prevhdr;
  430. mtu = dst_mtu(&rt->u.dst);
  431. if (np && np->frag_size < mtu) {
  432. if (np->frag_size)
  433. mtu = np->frag_size;
  434. }
  435. mtu -= hlen + sizeof(struct frag_hdr);
  436. if (skb_shinfo(skb)->frag_list) {
  437. int first_len = skb_pagelen(skb);
  438. if (first_len - hlen > mtu ||
  439. ((first_len - hlen) & 7) ||
  440. skb_cloned(skb))
  441. goto slow_path;
  442. for (frag = skb_shinfo(skb)->frag_list; frag; frag = frag->next) {
  443. /* Correct geometry. */
  444. if (frag->len > mtu ||
  445. ((frag->len & 7) && frag->next) ||
  446. skb_headroom(frag) < hlen)
  447. goto slow_path;
  448. /* Partially cloned skb? */
  449. if (skb_shared(frag))
  450. goto slow_path;
  451. BUG_ON(frag->sk);
  452. if (skb->sk) {
  453. sock_hold(skb->sk);
  454. frag->sk = skb->sk;
  455. frag->destructor = sock_wfree;
  456. skb->truesize -= frag->truesize;
  457. }
  458. }
  459. err = 0;
  460. offset = 0;
  461. frag = skb_shinfo(skb)->frag_list;
  462. skb_shinfo(skb)->frag_list = NULL;
  463. /* BUILD HEADER */
  464. tmp_hdr = kmalloc(hlen, GFP_ATOMIC);
  465. if (!tmp_hdr) {
  466. IP6_INC_STATS(IPSTATS_MIB_FRAGFAILS);
  467. return -ENOMEM;
  468. }
  469. *prevhdr = NEXTHDR_FRAGMENT;
  470. memcpy(tmp_hdr, skb->nh.raw, hlen);
  471. __skb_pull(skb, hlen);
  472. fh = (struct frag_hdr*)__skb_push(skb, sizeof(struct frag_hdr));
  473. skb->nh.raw = __skb_push(skb, hlen);
  474. memcpy(skb->nh.raw, tmp_hdr, hlen);
  475. ipv6_select_ident(skb, fh);
  476. fh->nexthdr = nexthdr;
  477. fh->reserved = 0;
  478. fh->frag_off = htons(IP6_MF);
  479. frag_id = fh->identification;
  480. first_len = skb_pagelen(skb);
  481. skb->data_len = first_len - skb_headlen(skb);
  482. skb->len = first_len;
  483. skb->nh.ipv6h->payload_len = htons(first_len - sizeof(struct ipv6hdr));
  484. for (;;) {
  485. /* Prepare header of the next frame,
  486. * before previous one went down. */
  487. if (frag) {
  488. frag->ip_summed = CHECKSUM_NONE;
  489. frag->h.raw = frag->data;
  490. fh = (struct frag_hdr*)__skb_push(frag, sizeof(struct frag_hdr));
  491. frag->nh.raw = __skb_push(frag, hlen);
  492. memcpy(frag->nh.raw, tmp_hdr, hlen);
  493. offset += skb->len - hlen - sizeof(struct frag_hdr);
  494. fh->nexthdr = nexthdr;
  495. fh->reserved = 0;
  496. fh->frag_off = htons(offset);
  497. if (frag->next != NULL)
  498. fh->frag_off |= htons(IP6_MF);
  499. fh->identification = frag_id;
  500. frag->nh.ipv6h->payload_len = htons(frag->len - sizeof(struct ipv6hdr));
  501. ip6_copy_metadata(frag, skb);
  502. }
  503. err = output(skb);
  504. if(!err)
  505. IP6_INC_STATS(IPSTATS_MIB_FRAGCREATES);
  506. if (err || !frag)
  507. break;
  508. skb = frag;
  509. frag = skb->next;
  510. skb->next = NULL;
  511. }
  512. kfree(tmp_hdr);
  513. if (err == 0) {
  514. IP6_INC_STATS(IPSTATS_MIB_FRAGOKS);
  515. return 0;
  516. }
  517. while (frag) {
  518. skb = frag->next;
  519. kfree_skb(frag);
  520. frag = skb;
  521. }
  522. IP6_INC_STATS(IPSTATS_MIB_FRAGFAILS);
  523. return err;
  524. }
  525. slow_path:
  526. left = skb->len - hlen; /* Space per frame */
  527. ptr = hlen; /* Where to start from */
  528. /*
  529. * Fragment the datagram.
  530. */
  531. *prevhdr = NEXTHDR_FRAGMENT;
  532. /*
  533. * Keep copying data until we run out.
  534. */
  535. while(left > 0) {
  536. len = left;
  537. /* IF: it doesn't fit, use 'mtu' - the data space left */
  538. if (len > mtu)
  539. len = mtu;
  540. /* IF: we are not sending upto and including the packet end
  541. then align the next start on an eight byte boundary */
  542. if (len < left) {
  543. len &= ~7;
  544. }
  545. /*
  546. * Allocate buffer.
  547. */
  548. if ((frag = alloc_skb(len+hlen+sizeof(struct frag_hdr)+LL_RESERVED_SPACE(rt->u.dst.dev), GFP_ATOMIC)) == NULL) {
  549. NETDEBUG(KERN_INFO "IPv6: frag: no memory for new fragment!\n");
  550. IP6_INC_STATS(IPSTATS_MIB_FRAGFAILS);
  551. err = -ENOMEM;
  552. goto fail;
  553. }
  554. /*
  555. * Set up data on packet
  556. */
  557. ip6_copy_metadata(frag, skb);
  558. skb_reserve(frag, LL_RESERVED_SPACE(rt->u.dst.dev));
  559. skb_put(frag, len + hlen + sizeof(struct frag_hdr));
  560. frag->nh.raw = frag->data;
  561. fh = (struct frag_hdr*)(frag->data + hlen);
  562. frag->h.raw = frag->data + hlen + sizeof(struct frag_hdr);
  563. /*
  564. * Charge the memory for the fragment to any owner
  565. * it might possess
  566. */
  567. if (skb->sk)
  568. skb_set_owner_w(frag, skb->sk);
  569. /*
  570. * Copy the packet header into the new buffer.
  571. */
  572. memcpy(frag->nh.raw, skb->data, hlen);
  573. /*
  574. * Build fragment header.
  575. */
  576. fh->nexthdr = nexthdr;
  577. fh->reserved = 0;
  578. if (!frag_id) {
  579. ipv6_select_ident(skb, fh);
  580. frag_id = fh->identification;
  581. } else
  582. fh->identification = frag_id;
  583. /*
  584. * Copy a block of the IP datagram.
  585. */
  586. if (skb_copy_bits(skb, ptr, frag->h.raw, len))
  587. BUG();
  588. left -= len;
  589. fh->frag_off = htons(offset);
  590. if (left > 0)
  591. fh->frag_off |= htons(IP6_MF);
  592. frag->nh.ipv6h->payload_len = htons(frag->len - sizeof(struct ipv6hdr));
  593. ptr += len;
  594. offset += len;
  595. /*
  596. * Put this fragment into the sending queue.
  597. */
  598. err = output(frag);
  599. if (err)
  600. goto fail;
  601. IP6_INC_STATS(IPSTATS_MIB_FRAGCREATES);
  602. }
  603. kfree_skb(skb);
  604. IP6_INC_STATS(IPSTATS_MIB_FRAGOKS);
  605. return err;
  606. fail:
  607. kfree_skb(skb);
  608. IP6_INC_STATS(IPSTATS_MIB_FRAGFAILS);
  609. return err;
  610. }
  611. static inline int ip6_rt_check(struct rt6key *rt_key,
  612. struct in6_addr *fl_addr,
  613. struct in6_addr *addr_cache)
  614. {
  615. return ((rt_key->plen != 128 || !ipv6_addr_equal(fl_addr, &rt_key->addr)) &&
  616. (addr_cache == NULL || !ipv6_addr_equal(fl_addr, addr_cache)));
  617. }
  618. static struct dst_entry *ip6_sk_dst_check(struct sock *sk,
  619. struct dst_entry *dst,
  620. struct flowi *fl)
  621. {
  622. struct ipv6_pinfo *np = inet6_sk(sk);
  623. struct rt6_info *rt = (struct rt6_info *)dst;
  624. if (!dst)
  625. goto out;
  626. /* Yes, checking route validity in not connected
  627. * case is not very simple. Take into account,
  628. * that we do not support routing by source, TOS,
  629. * and MSG_DONTROUTE --ANK (980726)
  630. *
  631. * 1. ip6_rt_check(): If route was host route,
  632. * check that cached destination is current.
  633. * If it is network route, we still may
  634. * check its validity using saved pointer
  635. * to the last used address: daddr_cache.
  636. * We do not want to save whole address now,
  637. * (because main consumer of this service
  638. * is tcp, which has not this problem),
  639. * so that the last trick works only on connected
  640. * sockets.
  641. * 2. oif also should be the same.
  642. */
  643. if (ip6_rt_check(&rt->rt6i_dst, &fl->fl6_dst, np->daddr_cache) ||
  644. #ifdef CONFIG_IPV6_SUBTREES
  645. ip6_rt_check(&rt->rt6i_src, &fl->fl6_src, np->saddr_cache) ||
  646. #endif
  647. (fl->oif && fl->oif != dst->dev->ifindex)) {
  648. dst_release(dst);
  649. dst = NULL;
  650. }
  651. out:
  652. return dst;
  653. }
  654. static int ip6_dst_lookup_tail(struct sock *sk,
  655. struct dst_entry **dst, struct flowi *fl)
  656. {
  657. int err;
  658. if (*dst == NULL)
  659. *dst = ip6_route_output(sk, fl);
  660. if ((err = (*dst)->error))
  661. goto out_err_release;
  662. if (ipv6_addr_any(&fl->fl6_src)) {
  663. err = ipv6_get_saddr(*dst, &fl->fl6_dst, &fl->fl6_src);
  664. if (err)
  665. goto out_err_release;
  666. }
  667. return 0;
  668. out_err_release:
  669. dst_release(*dst);
  670. *dst = NULL;
  671. return err;
  672. }
  673. /**
  674. * ip6_dst_lookup - perform route lookup on flow
  675. * @sk: socket which provides route info
  676. * @dst: pointer to dst_entry * for result
  677. * @fl: flow to lookup
  678. *
  679. * This function performs a route lookup on the given flow.
  680. *
  681. * It returns zero on success, or a standard errno code on error.
  682. */
  683. int ip6_dst_lookup(struct sock *sk, struct dst_entry **dst, struct flowi *fl)
  684. {
  685. *dst = NULL;
  686. return ip6_dst_lookup_tail(sk, dst, fl);
  687. }
  688. EXPORT_SYMBOL_GPL(ip6_dst_lookup);
  689. /**
  690. * ip6_sk_dst_lookup - perform socket cached route lookup on flow
  691. * @sk: socket which provides the dst cache and route info
  692. * @dst: pointer to dst_entry * for result
  693. * @fl: flow to lookup
  694. *
  695. * This function performs a route lookup on the given flow with the
  696. * possibility of using the cached route in the socket if it is valid.
  697. * It will take the socket dst lock when operating on the dst cache.
  698. * As a result, this function can only be used in process context.
  699. *
  700. * It returns zero on success, or a standard errno code on error.
  701. */
  702. int ip6_sk_dst_lookup(struct sock *sk, struct dst_entry **dst, struct flowi *fl)
  703. {
  704. *dst = NULL;
  705. if (sk) {
  706. *dst = sk_dst_check(sk, inet6_sk(sk)->dst_cookie);
  707. *dst = ip6_sk_dst_check(sk, *dst, fl);
  708. }
  709. return ip6_dst_lookup_tail(sk, dst, fl);
  710. }
  711. EXPORT_SYMBOL_GPL(ip6_sk_dst_lookup);
  712. static inline int ip6_ufo_append_data(struct sock *sk,
  713. int getfrag(void *from, char *to, int offset, int len,
  714. int odd, struct sk_buff *skb),
  715. void *from, int length, int hh_len, int fragheaderlen,
  716. int transhdrlen, int mtu,unsigned int flags)
  717. {
  718. struct sk_buff *skb;
  719. int err;
  720. /* There is support for UDP large send offload by network
  721. * device, so create one single skb packet containing complete
  722. * udp datagram
  723. */
  724. if ((skb = skb_peek_tail(&sk->sk_write_queue)) == NULL) {
  725. skb = sock_alloc_send_skb(sk,
  726. hh_len + fragheaderlen + transhdrlen + 20,
  727. (flags & MSG_DONTWAIT), &err);
  728. if (skb == NULL)
  729. return -ENOMEM;
  730. /* reserve space for Hardware header */
  731. skb_reserve(skb, hh_len);
  732. /* create space for UDP/IP header */
  733. skb_put(skb,fragheaderlen + transhdrlen);
  734. /* initialize network header pointer */
  735. skb->nh.raw = skb->data;
  736. /* initialize protocol header pointer */
  737. skb->h.raw = skb->data + fragheaderlen;
  738. skb->ip_summed = CHECKSUM_PARTIAL;
  739. skb->csum = 0;
  740. sk->sk_sndmsg_off = 0;
  741. }
  742. err = skb_append_datato_frags(sk,skb, getfrag, from,
  743. (length - transhdrlen));
  744. if (!err) {
  745. struct frag_hdr fhdr;
  746. /* specify the length of each IP datagram fragment*/
  747. skb_shinfo(skb)->gso_size = mtu - fragheaderlen -
  748. sizeof(struct frag_hdr);
  749. skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
  750. ipv6_select_ident(skb, &fhdr);
  751. skb_shinfo(skb)->ip6_frag_id = fhdr.identification;
  752. __skb_queue_tail(&sk->sk_write_queue, skb);
  753. return 0;
  754. }
  755. /* There is not enough support do UPD LSO,
  756. * so follow normal path
  757. */
  758. kfree_skb(skb);
  759. return err;
  760. }
  761. int ip6_append_data(struct sock *sk, int getfrag(void *from, char *to,
  762. int offset, int len, int odd, struct sk_buff *skb),
  763. void *from, int length, int transhdrlen,
  764. int hlimit, int tclass, struct ipv6_txoptions *opt, struct flowi *fl,
  765. struct rt6_info *rt, unsigned int flags)
  766. {
  767. struct inet_sock *inet = inet_sk(sk);
  768. struct ipv6_pinfo *np = inet6_sk(sk);
  769. struct sk_buff *skb;
  770. unsigned int maxfraglen, fragheaderlen;
  771. int exthdrlen;
  772. int hh_len;
  773. int mtu;
  774. int copy;
  775. int err;
  776. int offset = 0;
  777. int csummode = CHECKSUM_NONE;
  778. if (flags&MSG_PROBE)
  779. return 0;
  780. if (skb_queue_empty(&sk->sk_write_queue)) {
  781. /*
  782. * setup for corking
  783. */
  784. if (opt) {
  785. if (np->cork.opt == NULL) {
  786. np->cork.opt = kmalloc(opt->tot_len,
  787. sk->sk_allocation);
  788. if (unlikely(np->cork.opt == NULL))
  789. return -ENOBUFS;
  790. } else if (np->cork.opt->tot_len < opt->tot_len) {
  791. printk(KERN_DEBUG "ip6_append_data: invalid option length\n");
  792. return -EINVAL;
  793. }
  794. memcpy(np->cork.opt, opt, opt->tot_len);
  795. inet->cork.flags |= IPCORK_OPT;
  796. /* need source address above miyazawa*/
  797. }
  798. dst_hold(&rt->u.dst);
  799. np->cork.rt = rt;
  800. inet->cork.fl = *fl;
  801. np->cork.hop_limit = hlimit;
  802. np->cork.tclass = tclass;
  803. mtu = dst_mtu(rt->u.dst.path);
  804. if (np->frag_size < mtu) {
  805. if (np->frag_size)
  806. mtu = np->frag_size;
  807. }
  808. inet->cork.fragsize = mtu;
  809. if (dst_allfrag(rt->u.dst.path))
  810. inet->cork.flags |= IPCORK_ALLFRAG;
  811. inet->cork.length = 0;
  812. sk->sk_sndmsg_page = NULL;
  813. sk->sk_sndmsg_off = 0;
  814. exthdrlen = rt->u.dst.header_len + (opt ? opt->opt_flen : 0);
  815. length += exthdrlen;
  816. transhdrlen += exthdrlen;
  817. } else {
  818. rt = np->cork.rt;
  819. fl = &inet->cork.fl;
  820. if (inet->cork.flags & IPCORK_OPT)
  821. opt = np->cork.opt;
  822. transhdrlen = 0;
  823. exthdrlen = 0;
  824. mtu = inet->cork.fragsize;
  825. }
  826. hh_len = LL_RESERVED_SPACE(rt->u.dst.dev);
  827. fragheaderlen = sizeof(struct ipv6hdr) + (opt ? opt->opt_nflen : 0);
  828. maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen - sizeof(struct frag_hdr);
  829. if (mtu <= sizeof(struct ipv6hdr) + IPV6_MAXPLEN) {
  830. if (inet->cork.length + length > sizeof(struct ipv6hdr) + IPV6_MAXPLEN - fragheaderlen) {
  831. ipv6_local_error(sk, EMSGSIZE, fl, mtu-exthdrlen);
  832. return -EMSGSIZE;
  833. }
  834. }
  835. /*
  836. * Let's try using as much space as possible.
  837. * Use MTU if total length of the message fits into the MTU.
  838. * Otherwise, we need to reserve fragment header and
  839. * fragment alignment (= 8-15 octects, in total).
  840. *
  841. * Note that we may need to "move" the data from the tail of
  842. * of the buffer to the new fragment when we split
  843. * the message.
  844. *
  845. * FIXME: It may be fragmented into multiple chunks
  846. * at once if non-fragmentable extension headers
  847. * are too large.
  848. * --yoshfuji
  849. */
  850. inet->cork.length += length;
  851. if (((length > mtu) && (sk->sk_protocol == IPPROTO_UDP)) &&
  852. (rt->u.dst.dev->features & NETIF_F_UFO)) {
  853. err = ip6_ufo_append_data(sk, getfrag, from, length, hh_len,
  854. fragheaderlen, transhdrlen, mtu,
  855. flags);
  856. if (err)
  857. goto error;
  858. return 0;
  859. }
  860. if ((skb = skb_peek_tail(&sk->sk_write_queue)) == NULL)
  861. goto alloc_new_skb;
  862. while (length > 0) {
  863. /* Check if the remaining data fits into current packet. */
  864. copy = (inet->cork.length <= mtu && !(inet->cork.flags & IPCORK_ALLFRAG) ? mtu : maxfraglen) - skb->len;
  865. if (copy < length)
  866. copy = maxfraglen - skb->len;
  867. if (copy <= 0) {
  868. char *data;
  869. unsigned int datalen;
  870. unsigned int fraglen;
  871. unsigned int fraggap;
  872. unsigned int alloclen;
  873. struct sk_buff *skb_prev;
  874. alloc_new_skb:
  875. skb_prev = skb;
  876. /* There's no room in the current skb */
  877. if (skb_prev)
  878. fraggap = skb_prev->len - maxfraglen;
  879. else
  880. fraggap = 0;
  881. /*
  882. * If remaining data exceeds the mtu,
  883. * we know we need more fragment(s).
  884. */
  885. datalen = length + fraggap;
  886. if (datalen > (inet->cork.length <= mtu && !(inet->cork.flags & IPCORK_ALLFRAG) ? mtu : maxfraglen) - fragheaderlen)
  887. datalen = maxfraglen - fragheaderlen;
  888. fraglen = datalen + fragheaderlen;
  889. if ((flags & MSG_MORE) &&
  890. !(rt->u.dst.dev->features&NETIF_F_SG))
  891. alloclen = mtu;
  892. else
  893. alloclen = datalen + fragheaderlen;
  894. /*
  895. * The last fragment gets additional space at tail.
  896. * Note: we overallocate on fragments with MSG_MODE
  897. * because we have no idea if we're the last one.
  898. */
  899. if (datalen == length + fraggap)
  900. alloclen += rt->u.dst.trailer_len;
  901. /*
  902. * We just reserve space for fragment header.
  903. * Note: this may be overallocation if the message
  904. * (without MSG_MORE) fits into the MTU.
  905. */
  906. alloclen += sizeof(struct frag_hdr);
  907. if (transhdrlen) {
  908. skb = sock_alloc_send_skb(sk,
  909. alloclen + hh_len,
  910. (flags & MSG_DONTWAIT), &err);
  911. } else {
  912. skb = NULL;
  913. if (atomic_read(&sk->sk_wmem_alloc) <=
  914. 2 * sk->sk_sndbuf)
  915. skb = sock_wmalloc(sk,
  916. alloclen + hh_len, 1,
  917. sk->sk_allocation);
  918. if (unlikely(skb == NULL))
  919. err = -ENOBUFS;
  920. }
  921. if (skb == NULL)
  922. goto error;
  923. /*
  924. * Fill in the control structures
  925. */
  926. skb->ip_summed = csummode;
  927. skb->csum = 0;
  928. /* reserve for fragmentation */
  929. skb_reserve(skb, hh_len+sizeof(struct frag_hdr));
  930. /*
  931. * Find where to start putting bytes
  932. */
  933. data = skb_put(skb, fraglen);
  934. skb->nh.raw = data + exthdrlen;
  935. data += fragheaderlen;
  936. skb->h.raw = data + exthdrlen;
  937. if (fraggap) {
  938. skb->csum = skb_copy_and_csum_bits(
  939. skb_prev, maxfraglen,
  940. data + transhdrlen, fraggap, 0);
  941. skb_prev->csum = csum_sub(skb_prev->csum,
  942. skb->csum);
  943. data += fraggap;
  944. pskb_trim_unique(skb_prev, maxfraglen);
  945. }
  946. copy = datalen - transhdrlen - fraggap;
  947. if (copy < 0) {
  948. err = -EINVAL;
  949. kfree_skb(skb);
  950. goto error;
  951. } else if (copy > 0 && getfrag(from, data + transhdrlen, offset, copy, fraggap, skb) < 0) {
  952. err = -EFAULT;
  953. kfree_skb(skb);
  954. goto error;
  955. }
  956. offset += copy;
  957. length -= datalen - fraggap;
  958. transhdrlen = 0;
  959. exthdrlen = 0;
  960. csummode = CHECKSUM_NONE;
  961. /*
  962. * Put the packet on the pending queue
  963. */
  964. __skb_queue_tail(&sk->sk_write_queue, skb);
  965. continue;
  966. }
  967. if (copy > length)
  968. copy = length;
  969. if (!(rt->u.dst.dev->features&NETIF_F_SG)) {
  970. unsigned int off;
  971. off = skb->len;
  972. if (getfrag(from, skb_put(skb, copy),
  973. offset, copy, off, skb) < 0) {
  974. __skb_trim(skb, off);
  975. err = -EFAULT;
  976. goto error;
  977. }
  978. } else {
  979. int i = skb_shinfo(skb)->nr_frags;
  980. skb_frag_t *frag = &skb_shinfo(skb)->frags[i-1];
  981. struct page *page = sk->sk_sndmsg_page;
  982. int off = sk->sk_sndmsg_off;
  983. unsigned int left;
  984. if (page && (left = PAGE_SIZE - off) > 0) {
  985. if (copy >= left)
  986. copy = left;
  987. if (page != frag->page) {
  988. if (i == MAX_SKB_FRAGS) {
  989. err = -EMSGSIZE;
  990. goto error;
  991. }
  992. get_page(page);
  993. skb_fill_page_desc(skb, i, page, sk->sk_sndmsg_off, 0);
  994. frag = &skb_shinfo(skb)->frags[i];
  995. }
  996. } else if(i < MAX_SKB_FRAGS) {
  997. if (copy > PAGE_SIZE)
  998. copy = PAGE_SIZE;
  999. page = alloc_pages(sk->sk_allocation, 0);
  1000. if (page == NULL) {
  1001. err = -ENOMEM;
  1002. goto error;
  1003. }
  1004. sk->sk_sndmsg_page = page;
  1005. sk->sk_sndmsg_off = 0;
  1006. skb_fill_page_desc(skb, i, page, 0, 0);
  1007. frag = &skb_shinfo(skb)->frags[i];
  1008. skb->truesize += PAGE_SIZE;
  1009. atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
  1010. } else {
  1011. err = -EMSGSIZE;
  1012. goto error;
  1013. }
  1014. if (getfrag(from, page_address(frag->page)+frag->page_offset+frag->size, offset, copy, skb->len, skb) < 0) {
  1015. err = -EFAULT;
  1016. goto error;
  1017. }
  1018. sk->sk_sndmsg_off += copy;
  1019. frag->size += copy;
  1020. skb->len += copy;
  1021. skb->data_len += copy;
  1022. }
  1023. offset += copy;
  1024. length -= copy;
  1025. }
  1026. return 0;
  1027. error:
  1028. inet->cork.length -= length;
  1029. IP6_INC_STATS(IPSTATS_MIB_OUTDISCARDS);
  1030. return err;
  1031. }
  1032. int ip6_push_pending_frames(struct sock *sk)
  1033. {
  1034. struct sk_buff *skb, *tmp_skb;
  1035. struct sk_buff **tail_skb;
  1036. struct in6_addr final_dst_buf, *final_dst = &final_dst_buf;
  1037. struct inet_sock *inet = inet_sk(sk);
  1038. struct ipv6_pinfo *np = inet6_sk(sk);
  1039. struct ipv6hdr *hdr;
  1040. struct ipv6_txoptions *opt = np->cork.opt;
  1041. struct rt6_info *rt = np->cork.rt;
  1042. struct flowi *fl = &inet->cork.fl;
  1043. unsigned char proto = fl->proto;
  1044. int err = 0;
  1045. if ((skb = __skb_dequeue(&sk->sk_write_queue)) == NULL)
  1046. goto out;
  1047. tail_skb = &(skb_shinfo(skb)->frag_list);
  1048. /* move skb->data to ip header from ext header */
  1049. if (skb->data < skb->nh.raw)
  1050. __skb_pull(skb, skb->nh.raw - skb->data);
  1051. while ((tmp_skb = __skb_dequeue(&sk->sk_write_queue)) != NULL) {
  1052. __skb_pull(tmp_skb, skb->h.raw - skb->nh.raw);
  1053. *tail_skb = tmp_skb;
  1054. tail_skb = &(tmp_skb->next);
  1055. skb->len += tmp_skb->len;
  1056. skb->data_len += tmp_skb->len;
  1057. skb->truesize += tmp_skb->truesize;
  1058. __sock_put(tmp_skb->sk);
  1059. tmp_skb->destructor = NULL;
  1060. tmp_skb->sk = NULL;
  1061. }
  1062. ipv6_addr_copy(final_dst, &fl->fl6_dst);
  1063. __skb_pull(skb, skb->h.raw - skb->nh.raw);
  1064. if (opt && opt->opt_flen)
  1065. ipv6_push_frag_opts(skb, opt, &proto);
  1066. if (opt && opt->opt_nflen)
  1067. ipv6_push_nfrag_opts(skb, opt, &proto, &final_dst);
  1068. skb->nh.ipv6h = hdr = (struct ipv6hdr*) skb_push(skb, sizeof(struct ipv6hdr));
  1069. *(u32*)hdr = fl->fl6_flowlabel |
  1070. htonl(0x60000000 | ((int)np->cork.tclass << 20));
  1071. if (skb->len <= sizeof(struct ipv6hdr) + IPV6_MAXPLEN)
  1072. hdr->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
  1073. else
  1074. hdr->payload_len = 0;
  1075. hdr->hop_limit = np->cork.hop_limit;
  1076. hdr->nexthdr = proto;
  1077. ipv6_addr_copy(&hdr->saddr, &fl->fl6_src);
  1078. ipv6_addr_copy(&hdr->daddr, final_dst);
  1079. skb->priority = sk->sk_priority;
  1080. skb->dst = dst_clone(&rt->u.dst);
  1081. IP6_INC_STATS(IPSTATS_MIB_OUTREQUESTS);
  1082. err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL, skb->dst->dev, dst_output);
  1083. if (err) {
  1084. if (err > 0)
  1085. err = np->recverr ? net_xmit_errno(err) : 0;
  1086. if (err)
  1087. goto error;
  1088. }
  1089. out:
  1090. inet->cork.flags &= ~IPCORK_OPT;
  1091. kfree(np->cork.opt);
  1092. np->cork.opt = NULL;
  1093. if (np->cork.rt) {
  1094. dst_release(&np->cork.rt->u.dst);
  1095. np->cork.rt = NULL;
  1096. inet->cork.flags &= ~IPCORK_ALLFRAG;
  1097. }
  1098. memset(&inet->cork.fl, 0, sizeof(inet->cork.fl));
  1099. return err;
  1100. error:
  1101. goto out;
  1102. }
  1103. void ip6_flush_pending_frames(struct sock *sk)
  1104. {
  1105. struct inet_sock *inet = inet_sk(sk);
  1106. struct ipv6_pinfo *np = inet6_sk(sk);
  1107. struct sk_buff *skb;
  1108. while ((skb = __skb_dequeue_tail(&sk->sk_write_queue)) != NULL) {
  1109. IP6_INC_STATS(IPSTATS_MIB_OUTDISCARDS);
  1110. kfree_skb(skb);
  1111. }
  1112. inet->cork.flags &= ~IPCORK_OPT;
  1113. kfree(np->cork.opt);
  1114. np->cork.opt = NULL;
  1115. if (np->cork.rt) {
  1116. dst_release(&np->cork.rt->u.dst);
  1117. np->cork.rt = NULL;
  1118. inet->cork.flags &= ~IPCORK_ALLFRAG;
  1119. }
  1120. memset(&inet->cork.fl, 0, sizeof(inet->cork.fl));
  1121. }