ip6_output.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296
  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 struct dst_entry *ip6_sk_dst_check(struct sock *sk,
  612. struct dst_entry *dst,
  613. struct flowi *fl)
  614. {
  615. struct ipv6_pinfo *np = inet6_sk(sk);
  616. struct rt6_info *rt = (struct rt6_info *)dst;
  617. if (!dst)
  618. goto out;
  619. /* Yes, checking route validity in not connected
  620. * case is not very simple. Take into account,
  621. * that we do not support routing by source, TOS,
  622. * and MSG_DONTROUTE --ANK (980726)
  623. *
  624. * 1. If route was host route, check that
  625. * cached destination is current.
  626. * If it is network route, we still may
  627. * check its validity using saved pointer
  628. * to the last used address: daddr_cache.
  629. * We do not want to save whole address now,
  630. * (because main consumer of this service
  631. * is tcp, which has not this problem),
  632. * so that the last trick works only on connected
  633. * sockets.
  634. * 2. oif also should be the same.
  635. */
  636. if (((rt->rt6i_dst.plen != 128 ||
  637. !ipv6_addr_equal(&fl->fl6_dst, &rt->rt6i_dst.addr))
  638. && (np->daddr_cache == NULL ||
  639. !ipv6_addr_equal(&fl->fl6_dst, np->daddr_cache)))
  640. || (fl->oif && fl->oif != dst->dev->ifindex)) {
  641. dst_release(dst);
  642. dst = NULL;
  643. }
  644. out:
  645. return dst;
  646. }
  647. static int ip6_dst_lookup_tail(struct sock *sk,
  648. struct dst_entry **dst, struct flowi *fl)
  649. {
  650. int err;
  651. if (*dst == NULL)
  652. *dst = ip6_route_output(sk, fl);
  653. if ((err = (*dst)->error))
  654. goto out_err_release;
  655. if (ipv6_addr_any(&fl->fl6_src)) {
  656. err = ipv6_get_saddr(*dst, &fl->fl6_dst, &fl->fl6_src);
  657. if (err)
  658. goto out_err_release;
  659. }
  660. return 0;
  661. out_err_release:
  662. dst_release(*dst);
  663. *dst = NULL;
  664. return err;
  665. }
  666. /**
  667. * ip6_dst_lookup - perform route lookup on flow
  668. * @sk: socket which provides route info
  669. * @dst: pointer to dst_entry * for result
  670. * @fl: flow to lookup
  671. *
  672. * This function performs a route lookup on the given flow.
  673. *
  674. * It returns zero on success, or a standard errno code on error.
  675. */
  676. int ip6_dst_lookup(struct sock *sk, struct dst_entry **dst, struct flowi *fl)
  677. {
  678. *dst = NULL;
  679. return ip6_dst_lookup_tail(sk, dst, fl);
  680. }
  681. EXPORT_SYMBOL_GPL(ip6_dst_lookup);
  682. /**
  683. * ip6_sk_dst_lookup - perform socket cached route lookup on flow
  684. * @sk: socket which provides the dst cache and route info
  685. * @dst: pointer to dst_entry * for result
  686. * @fl: flow to lookup
  687. *
  688. * This function performs a route lookup on the given flow with the
  689. * possibility of using the cached route in the socket if it is valid.
  690. * It will take the socket dst lock when operating on the dst cache.
  691. * As a result, this function can only be used in process context.
  692. *
  693. * It returns zero on success, or a standard errno code on error.
  694. */
  695. int ip6_sk_dst_lookup(struct sock *sk, struct dst_entry **dst, struct flowi *fl)
  696. {
  697. *dst = NULL;
  698. if (sk) {
  699. *dst = sk_dst_check(sk, inet6_sk(sk)->dst_cookie);
  700. *dst = ip6_sk_dst_check(sk, *dst, fl);
  701. }
  702. return ip6_dst_lookup_tail(sk, dst, fl);
  703. }
  704. EXPORT_SYMBOL_GPL(ip6_sk_dst_lookup);
  705. static inline int ip6_ufo_append_data(struct sock *sk,
  706. int getfrag(void *from, char *to, int offset, int len,
  707. int odd, struct sk_buff *skb),
  708. void *from, int length, int hh_len, int fragheaderlen,
  709. int transhdrlen, int mtu,unsigned int flags)
  710. {
  711. struct sk_buff *skb;
  712. int err;
  713. /* There is support for UDP large send offload by network
  714. * device, so create one single skb packet containing complete
  715. * udp datagram
  716. */
  717. if ((skb = skb_peek_tail(&sk->sk_write_queue)) == NULL) {
  718. skb = sock_alloc_send_skb(sk,
  719. hh_len + fragheaderlen + transhdrlen + 20,
  720. (flags & MSG_DONTWAIT), &err);
  721. if (skb == NULL)
  722. return -ENOMEM;
  723. /* reserve space for Hardware header */
  724. skb_reserve(skb, hh_len);
  725. /* create space for UDP/IP header */
  726. skb_put(skb,fragheaderlen + transhdrlen);
  727. /* initialize network header pointer */
  728. skb->nh.raw = skb->data;
  729. /* initialize protocol header pointer */
  730. skb->h.raw = skb->data + fragheaderlen;
  731. skb->ip_summed = CHECKSUM_HW;
  732. skb->csum = 0;
  733. sk->sk_sndmsg_off = 0;
  734. }
  735. err = skb_append_datato_frags(sk,skb, getfrag, from,
  736. (length - transhdrlen));
  737. if (!err) {
  738. struct frag_hdr fhdr;
  739. /* specify the length of each IP datagram fragment*/
  740. skb_shinfo(skb)->gso_size = mtu - fragheaderlen -
  741. sizeof(struct frag_hdr);
  742. skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
  743. ipv6_select_ident(skb, &fhdr);
  744. skb_shinfo(skb)->ip6_frag_id = fhdr.identification;
  745. __skb_queue_tail(&sk->sk_write_queue, skb);
  746. return 0;
  747. }
  748. /* There is not enough support do UPD LSO,
  749. * so follow normal path
  750. */
  751. kfree_skb(skb);
  752. return err;
  753. }
  754. int ip6_append_data(struct sock *sk, int getfrag(void *from, char *to,
  755. int offset, int len, int odd, struct sk_buff *skb),
  756. void *from, int length, int transhdrlen,
  757. int hlimit, int tclass, struct ipv6_txoptions *opt, struct flowi *fl,
  758. struct rt6_info *rt, unsigned int flags)
  759. {
  760. struct inet_sock *inet = inet_sk(sk);
  761. struct ipv6_pinfo *np = inet6_sk(sk);
  762. struct sk_buff *skb;
  763. unsigned int maxfraglen, fragheaderlen;
  764. int exthdrlen;
  765. int hh_len;
  766. int mtu;
  767. int copy;
  768. int err;
  769. int offset = 0;
  770. int csummode = CHECKSUM_NONE;
  771. if (flags&MSG_PROBE)
  772. return 0;
  773. if (skb_queue_empty(&sk->sk_write_queue)) {
  774. /*
  775. * setup for corking
  776. */
  777. if (opt) {
  778. if (np->cork.opt == NULL) {
  779. np->cork.opt = kmalloc(opt->tot_len,
  780. sk->sk_allocation);
  781. if (unlikely(np->cork.opt == NULL))
  782. return -ENOBUFS;
  783. } else if (np->cork.opt->tot_len < opt->tot_len) {
  784. printk(KERN_DEBUG "ip6_append_data: invalid option length\n");
  785. return -EINVAL;
  786. }
  787. memcpy(np->cork.opt, opt, opt->tot_len);
  788. inet->cork.flags |= IPCORK_OPT;
  789. /* need source address above miyazawa*/
  790. }
  791. dst_hold(&rt->u.dst);
  792. np->cork.rt = rt;
  793. inet->cork.fl = *fl;
  794. np->cork.hop_limit = hlimit;
  795. np->cork.tclass = tclass;
  796. mtu = dst_mtu(rt->u.dst.path);
  797. if (np->frag_size < mtu) {
  798. if (np->frag_size)
  799. mtu = np->frag_size;
  800. }
  801. inet->cork.fragsize = mtu;
  802. if (dst_allfrag(rt->u.dst.path))
  803. inet->cork.flags |= IPCORK_ALLFRAG;
  804. inet->cork.length = 0;
  805. sk->sk_sndmsg_page = NULL;
  806. sk->sk_sndmsg_off = 0;
  807. exthdrlen = rt->u.dst.header_len + (opt ? opt->opt_flen : 0);
  808. length += exthdrlen;
  809. transhdrlen += exthdrlen;
  810. } else {
  811. rt = np->cork.rt;
  812. fl = &inet->cork.fl;
  813. if (inet->cork.flags & IPCORK_OPT)
  814. opt = np->cork.opt;
  815. transhdrlen = 0;
  816. exthdrlen = 0;
  817. mtu = inet->cork.fragsize;
  818. }
  819. hh_len = LL_RESERVED_SPACE(rt->u.dst.dev);
  820. fragheaderlen = sizeof(struct ipv6hdr) + (opt ? opt->opt_nflen : 0);
  821. maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen - sizeof(struct frag_hdr);
  822. if (mtu <= sizeof(struct ipv6hdr) + IPV6_MAXPLEN) {
  823. if (inet->cork.length + length > sizeof(struct ipv6hdr) + IPV6_MAXPLEN - fragheaderlen) {
  824. ipv6_local_error(sk, EMSGSIZE, fl, mtu-exthdrlen);
  825. return -EMSGSIZE;
  826. }
  827. }
  828. /*
  829. * Let's try using as much space as possible.
  830. * Use MTU if total length of the message fits into the MTU.
  831. * Otherwise, we need to reserve fragment header and
  832. * fragment alignment (= 8-15 octects, in total).
  833. *
  834. * Note that we may need to "move" the data from the tail of
  835. * of the buffer to the new fragment when we split
  836. * the message.
  837. *
  838. * FIXME: It may be fragmented into multiple chunks
  839. * at once if non-fragmentable extension headers
  840. * are too large.
  841. * --yoshfuji
  842. */
  843. inet->cork.length += length;
  844. if (((length > mtu) && (sk->sk_protocol == IPPROTO_UDP)) &&
  845. (rt->u.dst.dev->features & NETIF_F_UFO)) {
  846. err = ip6_ufo_append_data(sk, getfrag, from, length, hh_len,
  847. fragheaderlen, transhdrlen, mtu,
  848. flags);
  849. if (err)
  850. goto error;
  851. return 0;
  852. }
  853. if ((skb = skb_peek_tail(&sk->sk_write_queue)) == NULL)
  854. goto alloc_new_skb;
  855. while (length > 0) {
  856. /* Check if the remaining data fits into current packet. */
  857. copy = (inet->cork.length <= mtu && !(inet->cork.flags & IPCORK_ALLFRAG) ? mtu : maxfraglen) - skb->len;
  858. if (copy < length)
  859. copy = maxfraglen - skb->len;
  860. if (copy <= 0) {
  861. char *data;
  862. unsigned int datalen;
  863. unsigned int fraglen;
  864. unsigned int fraggap;
  865. unsigned int alloclen;
  866. struct sk_buff *skb_prev;
  867. alloc_new_skb:
  868. skb_prev = skb;
  869. /* There's no room in the current skb */
  870. if (skb_prev)
  871. fraggap = skb_prev->len - maxfraglen;
  872. else
  873. fraggap = 0;
  874. /*
  875. * If remaining data exceeds the mtu,
  876. * we know we need more fragment(s).
  877. */
  878. datalen = length + fraggap;
  879. if (datalen > (inet->cork.length <= mtu && !(inet->cork.flags & IPCORK_ALLFRAG) ? mtu : maxfraglen) - fragheaderlen)
  880. datalen = maxfraglen - fragheaderlen;
  881. fraglen = datalen + fragheaderlen;
  882. if ((flags & MSG_MORE) &&
  883. !(rt->u.dst.dev->features&NETIF_F_SG))
  884. alloclen = mtu;
  885. else
  886. alloclen = datalen + fragheaderlen;
  887. /*
  888. * The last fragment gets additional space at tail.
  889. * Note: we overallocate on fragments with MSG_MODE
  890. * because we have no idea if we're the last one.
  891. */
  892. if (datalen == length + fraggap)
  893. alloclen += rt->u.dst.trailer_len;
  894. /*
  895. * We just reserve space for fragment header.
  896. * Note: this may be overallocation if the message
  897. * (without MSG_MORE) fits into the MTU.
  898. */
  899. alloclen += sizeof(struct frag_hdr);
  900. if (transhdrlen) {
  901. skb = sock_alloc_send_skb(sk,
  902. alloclen + hh_len,
  903. (flags & MSG_DONTWAIT), &err);
  904. } else {
  905. skb = NULL;
  906. if (atomic_read(&sk->sk_wmem_alloc) <=
  907. 2 * sk->sk_sndbuf)
  908. skb = sock_wmalloc(sk,
  909. alloclen + hh_len, 1,
  910. sk->sk_allocation);
  911. if (unlikely(skb == NULL))
  912. err = -ENOBUFS;
  913. }
  914. if (skb == NULL)
  915. goto error;
  916. /*
  917. * Fill in the control structures
  918. */
  919. skb->ip_summed = csummode;
  920. skb->csum = 0;
  921. /* reserve for fragmentation */
  922. skb_reserve(skb, hh_len+sizeof(struct frag_hdr));
  923. /*
  924. * Find where to start putting bytes
  925. */
  926. data = skb_put(skb, fraglen);
  927. skb->nh.raw = data + exthdrlen;
  928. data += fragheaderlen;
  929. skb->h.raw = data + exthdrlen;
  930. if (fraggap) {
  931. skb->csum = skb_copy_and_csum_bits(
  932. skb_prev, maxfraglen,
  933. data + transhdrlen, fraggap, 0);
  934. skb_prev->csum = csum_sub(skb_prev->csum,
  935. skb->csum);
  936. data += fraggap;
  937. pskb_trim_unique(skb_prev, maxfraglen);
  938. }
  939. copy = datalen - transhdrlen - fraggap;
  940. if (copy < 0) {
  941. err = -EINVAL;
  942. kfree_skb(skb);
  943. goto error;
  944. } else if (copy > 0 && getfrag(from, data + transhdrlen, offset, copy, fraggap, skb) < 0) {
  945. err = -EFAULT;
  946. kfree_skb(skb);
  947. goto error;
  948. }
  949. offset += copy;
  950. length -= datalen - fraggap;
  951. transhdrlen = 0;
  952. exthdrlen = 0;
  953. csummode = CHECKSUM_NONE;
  954. /*
  955. * Put the packet on the pending queue
  956. */
  957. __skb_queue_tail(&sk->sk_write_queue, skb);
  958. continue;
  959. }
  960. if (copy > length)
  961. copy = length;
  962. if (!(rt->u.dst.dev->features&NETIF_F_SG)) {
  963. unsigned int off;
  964. off = skb->len;
  965. if (getfrag(from, skb_put(skb, copy),
  966. offset, copy, off, skb) < 0) {
  967. __skb_trim(skb, off);
  968. err = -EFAULT;
  969. goto error;
  970. }
  971. } else {
  972. int i = skb_shinfo(skb)->nr_frags;
  973. skb_frag_t *frag = &skb_shinfo(skb)->frags[i-1];
  974. struct page *page = sk->sk_sndmsg_page;
  975. int off = sk->sk_sndmsg_off;
  976. unsigned int left;
  977. if (page && (left = PAGE_SIZE - off) > 0) {
  978. if (copy >= left)
  979. copy = left;
  980. if (page != frag->page) {
  981. if (i == MAX_SKB_FRAGS) {
  982. err = -EMSGSIZE;
  983. goto error;
  984. }
  985. get_page(page);
  986. skb_fill_page_desc(skb, i, page, sk->sk_sndmsg_off, 0);
  987. frag = &skb_shinfo(skb)->frags[i];
  988. }
  989. } else if(i < MAX_SKB_FRAGS) {
  990. if (copy > PAGE_SIZE)
  991. copy = PAGE_SIZE;
  992. page = alloc_pages(sk->sk_allocation, 0);
  993. if (page == NULL) {
  994. err = -ENOMEM;
  995. goto error;
  996. }
  997. sk->sk_sndmsg_page = page;
  998. sk->sk_sndmsg_off = 0;
  999. skb_fill_page_desc(skb, i, page, 0, 0);
  1000. frag = &skb_shinfo(skb)->frags[i];
  1001. skb->truesize += PAGE_SIZE;
  1002. atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
  1003. } else {
  1004. err = -EMSGSIZE;
  1005. goto error;
  1006. }
  1007. if (getfrag(from, page_address(frag->page)+frag->page_offset+frag->size, offset, copy, skb->len, skb) < 0) {
  1008. err = -EFAULT;
  1009. goto error;
  1010. }
  1011. sk->sk_sndmsg_off += copy;
  1012. frag->size += copy;
  1013. skb->len += copy;
  1014. skb->data_len += copy;
  1015. }
  1016. offset += copy;
  1017. length -= copy;
  1018. }
  1019. return 0;
  1020. error:
  1021. inet->cork.length -= length;
  1022. IP6_INC_STATS(IPSTATS_MIB_OUTDISCARDS);
  1023. return err;
  1024. }
  1025. int ip6_push_pending_frames(struct sock *sk)
  1026. {
  1027. struct sk_buff *skb, *tmp_skb;
  1028. struct sk_buff **tail_skb;
  1029. struct in6_addr final_dst_buf, *final_dst = &final_dst_buf;
  1030. struct inet_sock *inet = inet_sk(sk);
  1031. struct ipv6_pinfo *np = inet6_sk(sk);
  1032. struct ipv6hdr *hdr;
  1033. struct ipv6_txoptions *opt = np->cork.opt;
  1034. struct rt6_info *rt = np->cork.rt;
  1035. struct flowi *fl = &inet->cork.fl;
  1036. unsigned char proto = fl->proto;
  1037. int err = 0;
  1038. if ((skb = __skb_dequeue(&sk->sk_write_queue)) == NULL)
  1039. goto out;
  1040. tail_skb = &(skb_shinfo(skb)->frag_list);
  1041. /* move skb->data to ip header from ext header */
  1042. if (skb->data < skb->nh.raw)
  1043. __skb_pull(skb, skb->nh.raw - skb->data);
  1044. while ((tmp_skb = __skb_dequeue(&sk->sk_write_queue)) != NULL) {
  1045. __skb_pull(tmp_skb, skb->h.raw - skb->nh.raw);
  1046. *tail_skb = tmp_skb;
  1047. tail_skb = &(tmp_skb->next);
  1048. skb->len += tmp_skb->len;
  1049. skb->data_len += tmp_skb->len;
  1050. skb->truesize += tmp_skb->truesize;
  1051. __sock_put(tmp_skb->sk);
  1052. tmp_skb->destructor = NULL;
  1053. tmp_skb->sk = NULL;
  1054. }
  1055. ipv6_addr_copy(final_dst, &fl->fl6_dst);
  1056. __skb_pull(skb, skb->h.raw - skb->nh.raw);
  1057. if (opt && opt->opt_flen)
  1058. ipv6_push_frag_opts(skb, opt, &proto);
  1059. if (opt && opt->opt_nflen)
  1060. ipv6_push_nfrag_opts(skb, opt, &proto, &final_dst);
  1061. skb->nh.ipv6h = hdr = (struct ipv6hdr*) skb_push(skb, sizeof(struct ipv6hdr));
  1062. *(u32*)hdr = fl->fl6_flowlabel |
  1063. htonl(0x60000000 | ((int)np->cork.tclass << 20));
  1064. if (skb->len <= sizeof(struct ipv6hdr) + IPV6_MAXPLEN)
  1065. hdr->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
  1066. else
  1067. hdr->payload_len = 0;
  1068. hdr->hop_limit = np->cork.hop_limit;
  1069. hdr->nexthdr = proto;
  1070. ipv6_addr_copy(&hdr->saddr, &fl->fl6_src);
  1071. ipv6_addr_copy(&hdr->daddr, final_dst);
  1072. skb->priority = sk->sk_priority;
  1073. skb->dst = dst_clone(&rt->u.dst);
  1074. IP6_INC_STATS(IPSTATS_MIB_OUTREQUESTS);
  1075. err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL, skb->dst->dev, dst_output);
  1076. if (err) {
  1077. if (err > 0)
  1078. err = np->recverr ? net_xmit_errno(err) : 0;
  1079. if (err)
  1080. goto error;
  1081. }
  1082. out:
  1083. inet->cork.flags &= ~IPCORK_OPT;
  1084. kfree(np->cork.opt);
  1085. np->cork.opt = NULL;
  1086. if (np->cork.rt) {
  1087. dst_release(&np->cork.rt->u.dst);
  1088. np->cork.rt = NULL;
  1089. inet->cork.flags &= ~IPCORK_ALLFRAG;
  1090. }
  1091. memset(&inet->cork.fl, 0, sizeof(inet->cork.fl));
  1092. return err;
  1093. error:
  1094. goto out;
  1095. }
  1096. void ip6_flush_pending_frames(struct sock *sk)
  1097. {
  1098. struct inet_sock *inet = inet_sk(sk);
  1099. struct ipv6_pinfo *np = inet6_sk(sk);
  1100. struct sk_buff *skb;
  1101. while ((skb = __skb_dequeue_tail(&sk->sk_write_queue)) != NULL) {
  1102. IP6_INC_STATS(IPSTATS_MIB_OUTDISCARDS);
  1103. kfree_skb(skb);
  1104. }
  1105. inet->cork.flags &= ~IPCORK_OPT;
  1106. kfree(np->cork.opt);
  1107. np->cork.opt = NULL;
  1108. if (np->cork.rt) {
  1109. dst_release(&np->cork.rt->u.dst);
  1110. np->cork.rt = NULL;
  1111. inet->cork.flags &= ~IPCORK_ALLFRAG;
  1112. }
  1113. memset(&inet->cork.fl, 0, sizeof(inet->cork.fl));
  1114. }