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