]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - net/ipv6/ip6_output.c
[SK_BUFF]: Introduce skb_network_offset()
[mirror_ubuntu-zesty-kernel.git] / net / ipv6 / ip6_output.c
CommitLineData
1da177e4
LT
1/*
2 * IPv6 output functions
1ab1457c 3 * Linux INET6 implementation
1da177e4
LT
4 *
5 * Authors:
1ab1457c 6 * Pedro Roque <roque@di.fc.ul.pt>
1da177e4
LT
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
1da177e4
LT
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>
b59f45d0 41#include <linux/module.h>
1da177e4
LT
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
59static int ip6_fragment(struct sk_buff *skb, int (*output)(struct sk_buff *));
60
61static __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
73static inline int ip6_output_finish(struct sk_buff *skb)
74{
1da177e4 75 struct dst_entry *dst = skb->dst;
1da177e4 76
3644f0ce
SH
77 if (dst->hh)
78 return neigh_hh_output(dst->hh, skb);
79 else if (dst->neighbour)
1da177e4
LT
80 return dst->neighbour->output(skb);
81
a11d206d 82 IP6_INC_STATS_BH(ip6_dst_idev(dst), IPSTATS_MIB_OUTNOROUTES);
1da177e4
LT
83 kfree_skb(skb);
84 return -EINVAL;
85
86}
87
88/* dev_loopback_xmit for use with netfilter. */
89static int ip6_dev_loopback_xmit(struct sk_buff *newskb)
90{
459a98ed 91 skb_reset_mac_header(newskb);
bbe735e4 92 __skb_pull(newskb, skb_network_offset(newskb));
1da177e4
LT
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
102static 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;
a11d206d 112 struct inet6_dev *idev = ip6_dst_idev(skb->dst);
1da177e4
LT
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) {
a11d206d 128 IP6_INC_STATS(idev, IPSTATS_MIB_OUTDISCARDS);
1da177e4
LT
129 kfree_skb(skb);
130 return 0;
131 }
132 }
133
a11d206d 134 IP6_INC_STATS(idev, IPSTATS_MIB_OUTMCASTPKTS);
1da177e4
LT
135 }
136
137 return NF_HOOK(PF_INET6, NF_IP6_POST_ROUTING, skb,NULL, skb->dev,ip6_output_finish);
138}
139
140int ip6_output(struct sk_buff *skb)
141{
89114afd 142 if ((skb->len > dst_mtu(skb->dst) && !skb_is_gso(skb)) ||
e89e9cf5 143 dst_allfrag(skb->dst))
1da177e4
LT
144 return ip6_fragment(skb, ip6_output2);
145 else
146 return ip6_output2(skb);
147}
148
1da177e4
LT
149/*
150 * xmit an sk_buff (used by TCP)
151 */
152
153int ip6_xmit(struct sock *sk, struct sk_buff *skb, struct flowi *fl,
154 struct ipv6_txoptions *opt, int ipfragok)
155{
b30bd282 156 struct ipv6_pinfo *np = inet6_sk(sk);
1da177e4
LT
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;
41a1f8ea 162 int hlimit, tclass;
1da177e4
LT
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);
a11d206d
YH
177 if (skb2 == NULL) {
178 IP6_INC_STATS(ip6_dst_idev(skb->dst),
179 IPSTATS_MIB_OUTDISCARDS);
180 kfree_skb(skb);
1da177e4
LT
181 return -ENOBUFS;
182 }
a11d206d
YH
183 kfree_skb(skb);
184 skb = skb2;
1da177e4
LT
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
e2d1bca7
ACM
194 skb_push(skb, sizeof(struct ipv6hdr));
195 skb_reset_network_header(skb);
196 hdr = skb->nh.ipv6h;
1da177e4
LT
197
198 /*
199 * Fill in the IPv6 header
200 */
201
1da177e4
LT
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
41a1f8ea
YH
210 tclass = -1;
211 if (np)
212 tclass = np->tclass;
213 if (tclass < 0)
214 tclass = 0;
215
90bcaf7b 216 *(__be32 *)hdr = htonl(0x60000000 | (tclass << 20)) | fl->fl6_flowlabel;
41a1f8ea 217
1da177e4
LT
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
a2c2064f
PM
225 skb->priority = sk->sk_priority;
226
1da177e4 227 mtu = dst_mtu(dst);
89114afd 228 if ((skb->len <= mtu) || ipfragok || skb_is_gso(skb)) {
a11d206d
YH
229 IP6_INC_STATS(ip6_dst_idev(skb->dst),
230 IPSTATS_MIB_OUTREQUESTS);
6869c4d8
HW
231 return NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL, dst->dev,
232 dst_output);
1da177e4
LT
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);
a11d206d 239 IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_FRAGFAILS);
1da177e4
LT
240 kfree_skb(skb);
241 return -EMSGSIZE;
242}
243
7159039a
YH
244EXPORT_SYMBOL(ip6_xmit);
245
1da177e4
LT
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
253int 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
ae08e1f0 269 *(__be32*)hdr = htonl(0x60000000);
1da177e4
LT
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
281static 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;
0bd1b59b
AM
289 if (sk && ra->sel == sel &&
290 (!sk->sk_bound_dev_if ||
291 sk->sk_bound_dev_if == skb->dev->ifindex)) {
1da177e4
LT
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
e21e0b5f
VN
310static 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
74553b09
VN
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
e21e0b5f
VN
357 return 0;
358}
359
1da177e4
LT
360static inline int ip6_forward_finish(struct sk_buff *skb)
361{
362 return dst_output(skb);
363}
364
365int 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);
1ab1457c 370
1da177e4
LT
371 if (ipv6_devconf.forwarding == 0)
372 goto error;
373
374 if (!xfrm6_policy_check(NULL, XFRM_POLICY_FWD, skb)) {
a11d206d 375 IP6_INC_STATS(ip6_dst_idev(dst), IPSTATS_MIB_INDISCARDS);
1da177e4
LT
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);
a11d206d 408 IP6_INC_STATS_BH(ip6_dst_idev(dst), IPSTATS_MIB_INHDRERRORS);
1da177e4
LT
409
410 kfree_skb(skb);
411 return -ETIMEDOUT;
412 }
413
fbea49e1
YH
414 /* XXX: idev->cnf.proxy_ndp? */
415 if (ipv6_devconf.proxy_ndp &&
416 pneigh_lookup(&nd_tbl, &hdr->daddr, skb->dev, 0)) {
74553b09
VN
417 int proxied = ip6_forward_proxy_check(skb);
418 if (proxied > 0)
e21e0b5f 419 return ip6_input(skb);
74553b09 420 else if (proxied < 0) {
a11d206d 421 IP6_INC_STATS(ip6_dst_idev(dst), IPSTATS_MIB_INDISCARDS);
74553b09
VN
422 goto drop;
423 }
e21e0b5f
VN
424 }
425
1da177e4 426 if (!xfrm6_route_forward(skb)) {
a11d206d 427 IP6_INC_STATS(ip6_dst_idev(dst), IPSTATS_MIB_INDISCARDS);
1da177e4
LT
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);
a11d206d
YH
466 IP6_INC_STATS_BH(ip6_dst_idev(dst), IPSTATS_MIB_INTOOBIGERRORS);
467 IP6_INC_STATS_BH(ip6_dst_idev(dst), IPSTATS_MIB_FRAGFAILS);
1da177e4
LT
468 kfree_skb(skb);
469 return -EMSGSIZE;
470 }
471
472 if (skb_cow(skb, dst->dev->hard_header_len)) {
a11d206d 473 IP6_INC_STATS(ip6_dst_idev(dst), IPSTATS_MIB_OUTDISCARDS);
1da177e4
LT
474 goto drop;
475 }
476
477 hdr = skb->nh.ipv6h;
478
479 /* Mangling hops number delayed to point after skb COW */
1ab1457c 480
1da177e4
LT
481 hdr->hop_limit--;
482
a11d206d 483 IP6_INC_STATS_BH(ip6_dst_idev(dst), IPSTATS_MIB_OUTFORWDATAGRAMS);
1da177e4
LT
484 return NF_HOOK(PF_INET6,NF_IP6_FORWARD, skb, skb->dev, dst->dev, ip6_forward_finish);
485
486error:
a11d206d 487 IP6_INC_STATS_BH(ip6_dst_idev(dst), IPSTATS_MIB_INADDRERRORS);
1da177e4
LT
488drop:
489 kfree_skb(skb);
490 return -EINVAL;
491}
492
493static 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;
1da177e4
LT
498 dst_release(to->dst);
499 to->dst = dst_clone(from->dst);
500 to->dev = from->dev;
82e91ffe 501 to->mark = from->mark;
1da177e4
LT
502
503#ifdef CONFIG_NET_SCHED
504 to->tc_index = from->tc_index;
505#endif
506#ifdef CONFIG_NETFILTER
1da177e4 507 /* Connection association is same as pre-frag packet */
9fb9cbb1 508 nf_conntrack_put(to->nfct);
1da177e4
LT
509 to->nfct = from->nfct;
510 nf_conntrack_get(to->nfct);
511 to->nfctinfo = from->nfctinfo;
9fb9cbb1
YK
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
1da177e4
LT
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
1da177e4 522#endif
984bc16c 523 skb_copy_secmark(to, from);
1da177e4
LT
524}
525
526int 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:
27637df9 539 break;
1da177e4 540 case NEXTHDR_ROUTING:
27637df9
MN
541 found_rhdr = 1;
542 break;
1da177e4 543 case NEXTHDR_DEST:
27637df9
MN
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;
1da177e4
LT
550 break;
551 default :
552 return offset;
553 }
27637df9
MN
554
555 offset += ipv6_optlen(exthdr);
556 *nexthdr = &exthdr->nexthdr;
557 exthdr = (struct ipv6_opt_hdr*)(skb->nh.raw + offset);
1da177e4
LT
558 }
559
560 return offset;
561}
b59f45d0 562EXPORT_SYMBOL_GPL(ip6_find_1stfragopt);
1da177e4
LT
563
564static 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;
d91675f9 569 struct ipv6_pinfo *np = skb->sk ? inet6_sk(skb->sk) : NULL;
1da177e4
LT
570 struct ipv6hdr *tmp_hdr;
571 struct frag_hdr *fh;
572 unsigned int mtu, hlen, left, len;
ae08e1f0 573 __be32 frag_id = 0;
1da177e4
LT
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
d91675f9
YH
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);
1da177e4
LT
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
1da177e4
LT
603 /* Partially cloned skb? */
604 if (skb_shared(frag))
605 goto slow_path;
2fdba6b0
HX
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 }
1da177e4
LT
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
9a217a1c 622 *prevhdr = NEXTHDR_FRAGMENT;
af879cc7 623 tmp_hdr = kmemdup(skb->nh.raw, hlen, GFP_ATOMIC);
1da177e4 624 if (!tmp_hdr) {
a11d206d 625 IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_FRAGFAILS);
1da177e4
LT
626 return -ENOMEM;
627 }
628
1da177e4
LT
629 __skb_pull(skb, hlen);
630 fh = (struct frag_hdr*)__skb_push(skb, sizeof(struct frag_hdr));
e2d1bca7
ACM
631 __skb_push(skb, hlen);
632 skb_reset_network_header(skb);
1da177e4
LT
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));
a11d206d
YH
645
646 dst_hold(&rt->u.dst);
1da177e4
LT
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));
e2d1bca7
ACM
655 __skb_push(frag, hlen);
656 skb_reset_network_header(frag);
1da177e4
LT
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 }
1ab1457c 668
1da177e4 669 err = output(skb);
dafee490 670 if(!err)
a11d206d 671 IP6_INC_STATS(ip6_dst_idev(&rt->u.dst), IPSTATS_MIB_FRAGCREATES);
dafee490 672
1da177e4
LT
673 if (err || !frag)
674 break;
675
676 skb = frag;
677 frag = skb->next;
678 skb->next = NULL;
679 }
680
a51482bd 681 kfree(tmp_hdr);
1da177e4
LT
682
683 if (err == 0) {
a11d206d
YH
684 IP6_INC_STATS(ip6_dst_idev(&rt->u.dst), IPSTATS_MIB_FRAGOKS);
685 dst_release(&rt->u.dst);
1da177e4
LT
686 return 0;
687 }
688
689 while (frag) {
690 skb = frag->next;
691 kfree_skb(frag);
692 frag = skb;
693 }
694
a11d206d
YH
695 IP6_INC_STATS(ip6_dst_idev(&rt->u.dst), IPSTATS_MIB_FRAGFAILS);
696 dst_release(&rt->u.dst);
1da177e4
LT
697 return err;
698 }
699
700slow_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) {
64ce2073 728 NETDEBUG(KERN_INFO "IPv6: frag: no memory for new fragment!\n");
a11d206d
YH
729 IP6_INC_STATS(ip6_dst_idev(skb->dst),
730 IPSTATS_MIB_FRAGFAILS);
1da177e4
LT
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));
c1d2bbe1 742 skb_reset_network_header(frag);
1da177e4
LT
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;
f36d6ab1 763 if (!frag_id) {
1da177e4
LT
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 */
1da177e4
LT
787 err = output(frag);
788 if (err)
789 goto fail;
dafee490 790
a11d206d 791 IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_FRAGCREATES);
1da177e4 792 }
a11d206d
YH
793 IP6_INC_STATS(ip6_dst_idev(skb->dst),
794 IPSTATS_MIB_FRAGOKS);
1da177e4 795 kfree_skb(skb);
1da177e4
LT
796 return err;
797
798fail:
a11d206d
YH
799 IP6_INC_STATS(ip6_dst_idev(skb->dst),
800 IPSTATS_MIB_FRAGFAILS);
1ab1457c 801 kfree_skb(skb);
1da177e4
LT
802 return err;
803}
804
cf6b1982
YH
805static 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
497c615a
HX
813static struct dst_entry *ip6_sk_dst_check(struct sock *sk,
814 struct dst_entry *dst,
815 struct flowi *fl)
1da177e4 816{
497c615a
HX
817 struct ipv6_pinfo *np = inet6_sk(sk);
818 struct rt6_info *rt = (struct rt6_info *)dst;
1da177e4 819
497c615a
HX
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 *
cf6b1982
YH
828 * 1. ip6_rt_check(): If route was host route,
829 * check that cached destination is current.
497c615a
HX
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 */
cf6b1982 840 if (ip6_rt_check(&rt->rt6i_dst, &fl->fl6_dst, np->daddr_cache) ||
8e1ef0a9
YH
841#ifdef CONFIG_IPV6_SUBTREES
842 ip6_rt_check(&rt->rt6i_src, &fl->fl6_src, np->saddr_cache) ||
843#endif
cf6b1982 844 (fl->oif && fl->oif != dst->dev->ifindex)) {
497c615a
HX
845 dst_release(dst);
846 dst = NULL;
1da177e4
LT
847 }
848
497c615a
HX
849out:
850 return dst;
851}
852
853static int ip6_dst_lookup_tail(struct sock *sk,
854 struct dst_entry **dst, struct flowi *fl)
855{
856 int err;
857
1da177e4
LT
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);
44456d37 866 if (err)
1da177e4 867 goto out_err_release;
1da177e4
LT
868 }
869
95c385b4
NH
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
1da177e4
LT
905 return 0;
906
907out_err_release:
908 dst_release(*dst);
909 *dst = NULL;
910 return err;
911}
34a0b3cd 912
497c615a
HX
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 */
923int 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}
3cf3dc6c
ACM
928EXPORT_SYMBOL_GPL(ip6_dst_lookup);
929
497c615a
HX
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 */
943int 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}
953EXPORT_SYMBOL_GPL(ip6_sk_dst_lookup);
954
34a0b3cd 955static inline int ip6_ufo_append_data(struct sock *sk,
e89e9cf5
AR
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 */
c1d2bbe1 983 skb_reset_network_header(skb);
e89e9cf5
AR
984
985 /* initialize protocol header pointer */
986 skb->h.raw = skb->data + fragheaderlen;
987
84fa7933 988 skb->ip_summed = CHECKSUM_PARTIAL;
e89e9cf5
AR
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*/
1ab1457c 999 skb_shinfo(skb)->gso_size = mtu - fragheaderlen -
7967168c 1000 sizeof(struct frag_hdr);
f83ef8c0 1001 skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
e89e9cf5
AR
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}
1da177e4 1015
41a1f8ea
YH
1016int 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)
1da177e4
LT
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;
41a1f8ea 1058 np->cork.tclass = tclass;
d91675f9 1059 mtu = dst_mtu(rt->u.dst.path);
c7503609 1060 if (np->frag_size < mtu) {
d91675f9
YH
1061 if (np->frag_size)
1062 mtu = np->frag_size;
1063 }
1064 inet->cork.fragsize = mtu;
1da177e4
LT
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
1b5c2299 1085 fragheaderlen = sizeof(struct ipv6hdr) + rt->u.dst.nfheader_len + (opt ? opt->opt_nflen : 0);
1da177e4
LT
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
1ab1457c 1102 * of the buffer to the new fragment when we split
1da177e4
LT
1103 * the message.
1104 *
1ab1457c 1105 * FIXME: It may be fragmented into multiple chunks
1da177e4
LT
1106 * at once if non-fragmentable extension headers
1107 * are too large.
1ab1457c 1108 * --yoshfuji
1da177e4
LT
1109 */
1110
1111 inet->cork.length += length;
e89e9cf5
AR
1112 if (((length > mtu) && (sk->sk_protocol == IPPROTO_UDP)) &&
1113 (rt->u.dst.dev->features & NETIF_F_UFO)) {
1114
baa829d8
PM
1115 err = ip6_ufo_append_data(sk, getfrag, from, length, hh_len,
1116 fragheaderlen, transhdrlen, mtu,
1117 flags);
1118 if (err)
e89e9cf5 1119 goto error;
e89e9cf5
AR
1120 return 0;
1121 }
1da177e4
LT
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;
1139alloc_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.
1ab1457c 1173 * Note: this may be overallocation if the message
1da177e4
LT
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;
e9fa4f7b 1217 pskb_trim_unique(skb_prev, maxfraglen);
1da177e4
LT
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;
1307error:
1308 inet->cork.length -= length;
a11d206d 1309 IP6_INC_STATS(rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS);
1da177e4
LT
1310 return err;
1311}
1312
1313int 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)
bbe735e4 1333 __skb_pull(skb, skb_network_offset(skb));
1da177e4
LT
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;
1da177e4
LT
1340 skb->truesize += tmp_skb->truesize;
1341 __sock_put(tmp_skb->sk);
1342 tmp_skb->destructor = NULL;
1343 tmp_skb->sk = NULL;
1da177e4
LT
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
e2d1bca7
ACM
1353 skb_push(skb, sizeof(struct ipv6hdr));
1354 skb_reset_network_header(skb);
1355 hdr = skb->nh.ipv6h;
1ab1457c 1356
90bcaf7b 1357 *(__be32*)hdr = fl->fl6_flowlabel |
41a1f8ea 1358 htonl(0x60000000 | ((int)np->cork.tclass << 20));
1da177e4
LT
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
a2c2064f
PM
1369 skb->priority = sk->sk_priority;
1370
1da177e4 1371 skb->dst = dst_clone(&rt->u.dst);
a11d206d 1372 IP6_INC_STATS(rt->rt6i_idev, IPSTATS_MIB_OUTREQUESTS);
1da177e4
LT
1373 err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL, skb->dst->dev, dst_output);
1374 if (err) {
1375 if (err > 0)
3320da89 1376 err = np->recverr ? net_xmit_errno(err) : 0;
1da177e4
LT
1377 if (err)
1378 goto error;
1379 }
1380
1381out:
1382 inet->cork.flags &= ~IPCORK_OPT;
a51482bd
JJ
1383 kfree(np->cork.opt);
1384 np->cork.opt = NULL;
1da177e4
LT
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;
1392error:
1393 goto out;
1394}
1395
1396void 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) {
a11d206d
YH
1403 IP6_INC_STATS(ip6_dst_idev(skb->dst),
1404 IPSTATS_MIB_OUTDISCARDS);
1da177e4
LT
1405 kfree_skb(skb);
1406 }
1407
1408 inet->cork.flags &= ~IPCORK_OPT;
1409
a51482bd
JJ
1410 kfree(np->cork.opt);
1411 np->cork.opt = NULL;
1da177e4
LT
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}