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