2 * IPv6 output functions
3 * Linux INET6 implementation
6 * Pedro Roque <roque@di.fc.ul.pt>
8 * $Id: ip6_output.c,v 1.34 2002/02/01 22:01:04 davem Exp $
10 * Based on linux/net/ipv4/ip_output.c
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.
18 * A.N.Kuznetsov : airthmetics in fragmentation.
19 * extension headers are implemented.
20 * route changes now work.
21 * ip6_forward does not confuse sniffers.
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
31 #include <linux/config.h>
32 #include <linux/errno.h>
33 #include <linux/types.h>
34 #include <linux/string.h>
35 #include <linux/socket.h>
36 #include <linux/net.h>
37 #include <linux/netdevice.h>
38 #include <linux/if_arp.h>
39 #include <linux/in6.h>
40 #include <linux/tcp.h>
41 #include <linux/route.h>
43 #include <linux/netfilter.h>
44 #include <linux/netfilter_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>
58 static int ip6_fragment(struct sk_buff **pskb, int (*output)(struct sk_buff**));
60 static __inline__ void ipv6_select_ident(struct sk_buff *skb, struct frag_hdr *fhdr)
62 static u32 ipv6_fragmentation_id = 1;
63 static spinlock_t ip6_id_lock = SPIN_LOCK_UNLOCKED;
65 spin_lock_bh(&ip6_id_lock);
66 fhdr->identification = htonl(ipv6_fragmentation_id);
67 if (++ipv6_fragmentation_id == 0)
68 ipv6_fragmentation_id = 1;
69 spin_unlock_bh(&ip6_id_lock);
72 static inline int ip6_output_finish(struct sk_buff *skb)
75 struct dst_entry *dst = skb->dst;
76 struct hh_cache *hh = dst->hh;
81 read_lock_bh(&hh->hh_lock);
82 hh_alen = HH_DATA_ALIGN(hh->hh_len);
83 memcpy(skb->data - hh_alen, hh->hh_data, hh_alen);
84 read_unlock_bh(&hh->hh_lock);
85 skb_push(skb, hh->hh_len);
86 return hh->hh_output(skb);
87 } else if (dst->neighbour)
88 return dst->neighbour->output(skb);
90 IP6_INC_STATS_BH(OutNoRoutes);
96 /* dev_loopback_xmit for use with netfilter. */
97 static int ip6_dev_loopback_xmit(struct sk_buff *newskb)
99 newskb->mac.raw = newskb->data;
100 __skb_pull(newskb, newskb->nh.raw - newskb->data);
101 newskb->pkt_type = PACKET_LOOPBACK;
102 newskb->ip_summed = CHECKSUM_UNNECESSARY;
103 BUG_TRAP(newskb->dst);
110 static int ip6_output2(struct sk_buff **pskb)
112 struct sk_buff *skb = *pskb;
113 struct dst_entry *dst = skb->dst;
114 struct net_device *dev = dst->dev;
116 skb->protocol = htons(ETH_P_IPV6);
119 if (ipv6_addr_is_multicast(&skb->nh.ipv6h->daddr)) {
120 struct ipv6_pinfo* np = skb->sk ? inet6_sk(skb->sk) : NULL;
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);
127 /* Do not check for IFF_ALLMULTI; multicast routing
128 is not supported in any case.
131 NF_HOOK(PF_INET6, NF_IP6_POST_ROUTING, newskb, NULL,
133 ip6_dev_loopback_xmit);
135 if (skb->nh.ipv6h->hop_limit == 0) {
136 IP6_INC_STATS(OutDiscards);
142 IP6_INC_STATS(OutMcastPkts);
145 return NF_HOOK(PF_INET6, NF_IP6_POST_ROUTING, skb,NULL, skb->dev,ip6_output_finish);
148 int ip6_output(struct sk_buff **pskb)
150 struct sk_buff *skb = *pskb;
152 if ((skb->len > dst_pmtu(skb->dst) || skb_shinfo(skb)->frag_list))
153 return ip6_fragment(pskb, ip6_output2);
155 return ip6_output2(pskb);
158 #ifdef CONFIG_NETFILTER
159 int ip6_route_me_harder(struct sk_buff *skb)
161 struct ipv6hdr *iph = skb->nh.ipv6h;
162 struct dst_entry *dst;
164 .oif = skb->sk ? skb->sk->sk_bound_dev_if : 0,
167 { .daddr = iph->daddr,
168 .saddr = iph->saddr, } },
169 .proto = iph->nexthdr,
172 dst = ip6_route_output(skb->sk, &fl);
175 IP6_INC_STATS(OutNoRoutes);
177 printk(KERN_DEBUG "ip6_route_me_harder: No more route.\n"));
182 /* Drop old route. */
183 dst_release(skb->dst);
190 static inline int ip6_maybe_reroute(struct sk_buff *skb)
192 #ifdef CONFIG_NETFILTER
193 if (skb->nfcache & NFC_ALTERED){
194 if (ip6_route_me_harder(skb) != 0){
199 #endif /* CONFIG_NETFILTER */
200 return dst_output(skb);
204 * xmit an sk_buff (used by TCP)
207 int ip6_xmit(struct sock *sk, struct sk_buff *skb, struct flowi *fl,
208 struct ipv6_txoptions *opt, int ipfragok)
210 struct ipv6_pinfo *np = sk ? inet6_sk(sk) : NULL;
211 struct in6_addr *first_hop = &fl->fl6_dst;
212 struct dst_entry *dst = skb->dst;
214 u8 proto = fl->proto;
215 int seg_len = skb->len;
222 /* First: exthdrs may take lots of space (~8K for now)
223 MAX_HEADER is not enough.
225 head_room = opt->opt_nflen + opt->opt_flen;
226 seg_len += head_room;
227 head_room += sizeof(struct ipv6hdr) + LL_RESERVED_SPACE(dst->dev);
229 if (skb_headroom(skb) < head_room) {
230 struct sk_buff *skb2 = skb_realloc_headroom(skb, head_room);
234 IP6_INC_STATS(OutDiscards);
238 skb_set_owner_w(skb, sk);
241 ipv6_push_frag_opts(skb, opt, &proto);
243 ipv6_push_nfrag_opts(skb, opt, &proto, &first_hop);
246 hdr = skb->nh.ipv6h = (struct ipv6hdr*)skb_push(skb, sizeof(struct ipv6hdr));
249 * Fill in the IPv6 header
252 *(u32*)hdr = htonl(0x60000000) | fl->fl6_flowlabel;
255 hlimit = np->hop_limit;
257 hlimit = dst_metric(dst, RTAX_HOPLIMIT);
259 hdr->payload_len = htons(seg_len);
260 hdr->nexthdr = proto;
261 hdr->hop_limit = hlimit;
263 ipv6_addr_copy(&hdr->saddr, &fl->fl6_src);
264 ipv6_addr_copy(&hdr->daddr, first_hop);
267 if ((skb->len <= mtu) || ipfragok) {
268 IP6_INC_STATS(OutRequests);
269 return NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL, dst->dev, ip6_maybe_reroute);
273 printk(KERN_DEBUG "IPv6: sending pkt_too_big to self\n");
275 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, skb->dev);
276 IP6_INC_STATS(FragFails);
282 * To avoid extra problems ND packets are send through this
283 * routine. It's code duplication but I really want to avoid
284 * extra checks since ipv6_build_header is used by TCP (which
285 * is for us performance critical)
288 int ip6_nd_hdr(struct sock *sk, struct sk_buff *skb, struct net_device *dev,
289 struct in6_addr *saddr, struct in6_addr *daddr,
292 struct ipv6_pinfo *np = inet6_sk(sk);
296 skb->protocol = htons(ETH_P_IPV6);
299 totlen = len + sizeof(struct ipv6hdr);
301 hdr = (struct ipv6hdr *) skb_put(skb, sizeof(struct ipv6hdr));
304 *(u32*)hdr = htonl(0x60000000);
306 hdr->payload_len = htons(len);
307 hdr->nexthdr = proto;
308 hdr->hop_limit = np->hop_limit;
310 ipv6_addr_copy(&hdr->saddr, saddr);
311 ipv6_addr_copy(&hdr->daddr, daddr);
316 int ip6_call_ra_chain(struct sk_buff *skb, int sel)
318 struct ip6_ra_chain *ra;
319 struct sock *last = NULL;
321 read_lock(&ip6_ra_lock);
322 for (ra = ip6_ra_chain; ra; ra = ra->next) {
323 struct sock *sk = ra->sk;
324 if (sk && ra->sel == sel) {
326 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
328 rawv6_rcv(last, skb2);
335 rawv6_rcv(last, skb);
336 read_unlock(&ip6_ra_lock);
339 read_unlock(&ip6_ra_lock);
343 static inline int ip6_forward_finish(struct sk_buff *skb)
345 return dst_output(skb);
348 int ip6_forward(struct sk_buff *skb)
350 struct dst_entry *dst = skb->dst;
351 struct ipv6hdr *hdr = skb->nh.ipv6h;
352 struct inet6_skb_parm *opt = IP6CB(skb);
354 if (ipv6_devconf.forwarding == 0)
357 if (!xfrm6_policy_check(NULL, XFRM_POLICY_FWD, skb)) {
358 IP6_INC_STATS(InDiscards);
362 skb->ip_summed = CHECKSUM_NONE;
365 * We DO NOT make any processing on
366 * RA packets, pushing them to user level AS IS
367 * without ane WARRANTY that application will be able
368 * to interpret them. The reason is that we
369 * cannot make anything clever here.
371 * We are not end-node, so that if packet contains
372 * AH/ESP, we cannot make anything.
373 * Defragmentation also would be mistake, RA packets
374 * cannot be fragmented, because there is no warranty
375 * that different fragments will go along one path. --ANK
378 u8 *ptr = skb->nh.raw + opt->ra;
379 if (ip6_call_ra_chain(skb, (ptr[2]<<8) + ptr[3]))
384 * check and decrement ttl
386 if (hdr->hop_limit <= 1) {
387 /* Force OUTPUT device used as source address */
389 icmpv6_send(skb, ICMPV6_TIME_EXCEED, ICMPV6_EXC_HOPLIMIT,
396 if (!xfrm6_route_forward(skb)) {
397 IP6_INC_STATS(InDiscards);
401 /* IPv6 specs say nothing about it, but it is clear that we cannot
402 send redirects to source routed frames.
404 if (skb->dev == dst->dev && dst->neighbour && opt->srcrt == 0) {
405 struct in6_addr *target = NULL;
407 struct neighbour *n = dst->neighbour;
410 * incoming and outgoing devices are the same
414 rt = (struct rt6_info *) dst;
415 if ((rt->rt6i_flags & RTF_GATEWAY))
416 target = (struct in6_addr*)&n->primary_key;
418 target = &hdr->daddr;
420 /* Limit redirects both by destination (here)
421 and by source (inside ndisc_send_redirect)
423 if (xrlim_allow(dst, 1*HZ))
424 ndisc_send_redirect(skb, n, target);
425 } else if (ipv6_addr_type(&hdr->saddr)&(IPV6_ADDR_MULTICAST|IPV6_ADDR_LOOPBACK
426 |IPV6_ADDR_LINKLOCAL)) {
427 /* This check is security critical. */
431 if (skb->len > dst_pmtu(dst)) {
432 /* Again, force OUTPUT device used as source address */
434 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, dst_pmtu(dst), skb->dev);
435 IP6_INC_STATS_BH(InTooBigErrors);
436 IP6_INC_STATS_BH(FragFails);
441 if (skb_cow(skb, dst->dev->hard_header_len)) {
442 IP6_INC_STATS(OutDiscards);
448 /* Mangling hops number delayed to point after skb COW */
452 IP6_INC_STATS_BH(OutForwDatagrams);
453 return NF_HOOK(PF_INET6,NF_IP6_FORWARD, skb, skb->dev, dst->dev, ip6_forward_finish);
456 IP6_INC_STATS_BH(InAddrErrors);
462 static void ip6_copy_metadata(struct sk_buff *to, struct sk_buff *from)
464 to->pkt_type = from->pkt_type;
465 to->priority = from->priority;
466 to->protocol = from->protocol;
467 to->security = from->security;
468 to->dst = dst_clone(from->dst);
471 #ifdef CONFIG_NET_SCHED
472 to->tc_index = from->tc_index;
474 #ifdef CONFIG_NETFILTER
475 to->nfmark = from->nfmark;
476 /* Connection association is same as pre-frag packet */
477 to->nfct = from->nfct;
478 nf_conntrack_get(to->nfct);
479 #ifdef CONFIG_BRIDGE_NETFILTER
480 nf_bridge_put(to->nf_bridge);
481 to->nf_bridge = from->nf_bridge;
482 nf_bridge_get(to->nf_bridge);
484 #ifdef CONFIG_NETFILTER_DEBUG
485 to->nf_debug = from->nf_debug;
490 int ip6_find_1stfragopt(struct sk_buff *skb, u8 **nexthdr)
492 u16 offset = sizeof(struct ipv6hdr);
493 struct ipv6_opt_hdr *exthdr = (struct ipv6_opt_hdr*)(skb->nh.ipv6h + 1);
494 unsigned int packet_len = skb->tail - skb->nh.raw;
496 *nexthdr = &skb->nh.ipv6h->nexthdr;
498 while (offset + 1 <= packet_len) {
503 case NEXTHDR_ROUTING:
505 if (**nexthdr == NEXTHDR_ROUTING) found_rhdr = 1;
506 if (**nexthdr == NEXTHDR_DEST && found_rhdr) return offset;
507 offset += ipv6_optlen(exthdr);
508 *nexthdr = &exthdr->nexthdr;
509 exthdr = (struct ipv6_opt_hdr*)(skb->nh.raw + offset);
519 static int ip6_fragment(struct sk_buff **pskb, int (*output)(struct sk_buff**))
521 struct net_device *dev;
522 struct sk_buff *frag, *skb = *pskb;
523 struct rt6_info *rt = (struct rt6_info*)skb->dst;
524 struct ipv6hdr *tmp_hdr;
526 unsigned int mtu, hlen, left, len;
528 int ptr, offset = 0, err=0;
529 u8 *prevhdr, nexthdr = 0;
532 hlen = ip6_find_1stfragopt(skb, &prevhdr);
535 mtu = dst_pmtu(&rt->u.dst) - hlen - sizeof(struct frag_hdr);
537 if (skb_shinfo(skb)->frag_list) {
538 int first_len = skb_pagelen(skb);
540 if (first_len - hlen > mtu ||
541 ((first_len - hlen) & 7) ||
545 for (frag = skb_shinfo(skb)->frag_list; frag; frag = frag->next) {
546 /* Correct geometry. */
547 if (frag->len > mtu ||
548 ((frag->len & 7) && frag->next) ||
549 skb_headroom(frag) < hlen)
552 /* Correct socket ownership. */
553 if (frag->sk == NULL)
556 /* Partially cloned skb? */
557 if (skb_shared(frag))
563 frag = skb_shinfo(skb)->frag_list;
564 skb_shinfo(skb)->frag_list = 0;
567 tmp_hdr = kmalloc(hlen, GFP_ATOMIC);
569 IP6_INC_STATS(FragFails);
573 *prevhdr = NEXTHDR_FRAGMENT;
574 memcpy(tmp_hdr, skb->nh.raw, hlen);
575 __skb_pull(skb, hlen);
576 fh = (struct frag_hdr*)__skb_push(skb, sizeof(struct frag_hdr));
577 skb->nh.raw = __skb_push(skb, hlen);
578 memcpy(skb->nh.raw, tmp_hdr, hlen);
580 ipv6_select_ident(skb, fh);
581 fh->nexthdr = nexthdr;
583 fh->frag_off = htons(IP6_MF);
584 frag_id = fh->identification;
586 first_len = skb_pagelen(skb);
587 skb->data_len = first_len - skb_headlen(skb);
588 skb->len = first_len;
589 skb->nh.ipv6h->payload_len = htons(first_len - sizeof(struct ipv6hdr));
593 /* Prepare header of the next frame,
594 * before previous one went down. */
596 frag->h.raw = frag->data;
597 fh = (struct frag_hdr*)__skb_push(frag, sizeof(struct frag_hdr));
598 frag->nh.raw = __skb_push(frag, hlen);
599 memcpy(frag->nh.raw, tmp_hdr, hlen);
600 offset += skb->len - hlen - sizeof(struct frag_hdr);
601 fh->nexthdr = nexthdr;
603 fh->frag_off = htons(offset);
604 if (frag->next != NULL)
605 fh->frag_off |= htons(IP6_MF);
606 fh->identification = frag_id;
607 frag->nh.ipv6h->payload_len = htons(frag->len - sizeof(struct ipv6hdr));
608 ip6_copy_metadata(frag, skb);
612 if (unlikely(skb != *pskb))
626 IP6_INC_STATS(FragOKs);
636 IP6_INC_STATS(FragFails);
641 left = skb->len - hlen; /* Space per frame */
642 ptr = hlen; /* Where to start from */
645 * Fragment the datagram.
648 *prevhdr = NEXTHDR_FRAGMENT;
651 * Keep copying data until we run out.
655 /* IF: it doesn't fit, use 'mtu' - the data space left */
658 /* IF: we are not sending upto and including the packet end
659 then align the next start on an eight byte boundary */
667 if ((frag = alloc_skb(len+hlen+sizeof(struct frag_hdr)+LL_RESERVED_SPACE(rt->u.dst.dev), GFP_ATOMIC)) == NULL) {
668 NETDEBUG(printk(KERN_INFO "IPv6: frag: no memory for new fragment!\n"));
669 IP6_INC_STATS(FragFails);
675 * Set up data on packet
678 ip6_copy_metadata(frag, skb);
679 skb_reserve(frag, LL_RESERVED_SPACE(rt->u.dst.dev));
680 skb_put(frag, len + hlen + sizeof(struct frag_hdr));
681 frag->nh.raw = frag->data;
682 fh = (struct frag_hdr*)(frag->data + hlen);
683 frag->h.raw = frag->data + hlen + sizeof(struct frag_hdr);
686 * Charge the memory for the fragment to any owner
690 skb_set_owner_w(frag, skb->sk);
693 * Copy the packet header into the new buffer.
695 memcpy(frag->nh.raw, skb->data, hlen);
698 * Build fragment header.
700 fh->nexthdr = nexthdr;
703 ipv6_select_ident(skb, fh);
704 frag_id = fh->identification;
706 fh->identification = frag_id;
709 * Copy a block of the IP datagram.
711 if (skb_copy_bits(skb, ptr, frag->h.raw, len))
715 fh->frag_off = htons(offset);
717 fh->frag_off |= htons(IP6_MF);
718 frag->nh.ipv6h->payload_len = htons(frag->len - sizeof(struct ipv6hdr));
724 * Put this fragment into the sending queue.
727 IP6_INC_STATS(FragCreates);
734 IP6_INC_STATS(FragOKs);
739 IP6_INC_STATS(FragFails);
743 int ip6_dst_lookup(struct sock *sk, struct dst_entry **dst, struct flowi *fl)
749 struct ipv6_pinfo *np = inet6_sk(sk);
751 *dst = __sk_dst_check(sk, np->dst_cookie);
753 struct rt6_info *rt = (struct rt6_info*)*dst;
755 /* Yes, checking route validity in not connected
756 case is not very simple. Take into account,
757 that we do not support routing by source, TOS,
758 and MSG_DONTROUTE --ANK (980726)
760 1. If route was host route, check that
761 cached destination is current.
762 If it is network route, we still may
763 check its validity using saved pointer
764 to the last used address: daddr_cache.
765 We do not want to save whole address now,
766 (because main consumer of this service
767 is tcp, which has not this problem),
768 so that the last trick works only on connected
770 2. oif also should be the same.
773 if (((rt->rt6i_dst.plen != 128 ||
774 ipv6_addr_cmp(&fl->fl6_dst, &rt->rt6i_dst.addr))
775 && (np->daddr_cache == NULL ||
776 ipv6_addr_cmp(&fl->fl6_dst, np->daddr_cache)))
777 || (fl->oif && fl->oif != (*dst)->dev->ifindex)) {
785 *dst = ip6_route_output(sk, fl);
787 if ((err = (*dst)->error))
788 goto out_err_release;
790 if (ipv6_addr_any(&fl->fl6_src)) {
791 err = ipv6_get_saddr(*dst, &fl->fl6_dst, &fl->fl6_src);
795 printk(KERN_DEBUG "ip6_dst_lookup: "
796 "no available source address\n");
798 goto out_err_release;
801 if ((err = xfrm_lookup(dst, fl, sk, 0)) < 0) {
803 goto out_err_release;
814 int ip6_append_data(struct sock *sk, int getfrag(void *from, char *to, int offset, int len, int odd, struct sk_buff *skb),
815 void *from, int length, int transhdrlen,
816 int hlimit, struct ipv6_txoptions *opt, struct flowi *fl, struct rt6_info *rt,
819 struct inet_opt *inet = inet_sk(sk);
820 struct ipv6_pinfo *np = inet6_sk(sk);
822 unsigned int maxfraglen, fragheaderlen;
829 int csummode = CHECKSUM_NONE;
833 if (skb_queue_empty(&sk->sk_write_queue)) {
838 if (np->cork.opt == NULL) {
839 np->cork.opt = kmalloc(opt->tot_len,
841 if (unlikely(np->cork.opt == NULL))
843 } else if (np->cork.opt->tot_len < opt->tot_len) {
844 printk(KERN_DEBUG "ip6_append_data: invalid option length\n");
847 memcpy(np->cork.opt, opt, opt->tot_len);
848 inet->cork.flags |= IPCORK_OPT;
849 /* need source address above miyazawa*/
851 dst_hold(&rt->u.dst);
854 np->cork.hop_limit = hlimit;
855 inet->cork.fragsize = mtu = dst_pmtu(&rt->u.dst);
856 inet->cork.length = 0;
857 inet->sndmsg_page = NULL;
858 inet->sndmsg_off = 0;
859 exthdrlen = rt->u.dst.header_len + (opt ? opt->opt_flen : 0);
861 transhdrlen += exthdrlen;
865 if (inet->cork.flags & IPCORK_OPT)
869 mtu = inet->cork.fragsize;
872 hh_len = LL_RESERVED_SPACE(rt->u.dst.dev);
874 fragheaderlen = sizeof(struct ipv6hdr) + (opt ? opt->opt_nflen : 0);
875 maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen - sizeof(struct frag_hdr);
877 if (mtu <= sizeof(struct ipv6hdr) + IPV6_MAXPLEN) {
878 if (inet->cork.length + length > sizeof(struct ipv6hdr) + IPV6_MAXPLEN - fragheaderlen) {
879 ipv6_local_error(sk, EMSGSIZE, fl, mtu-exthdrlen);
884 inet->cork.length += length;
886 if ((skb = skb_peek_tail(&sk->sk_write_queue)) == NULL)
890 if ((copy = maxfraglen - skb->len) <= 0) {
892 unsigned int datalen;
893 unsigned int fraglen;
894 unsigned int alloclen;
897 datalen = maxfraglen - fragheaderlen;
898 if (datalen > length)
900 fraglen = datalen + fragheaderlen;
901 if ((flags & MSG_MORE) &&
902 !(rt->u.dst.dev->features&NETIF_F_SG))
903 alloclen = maxfraglen;
906 alloclen += sizeof(struct frag_hdr);
908 skb = sock_alloc_send_skb(sk,
910 (flags & MSG_DONTWAIT), &err);
913 if (atomic_read(&sk->sk_wmem_alloc) <=
915 skb = sock_wmalloc(sk,
916 alloclen + hh_len, 1,
918 if (unlikely(skb == NULL))
924 * Fill in the control structures
926 skb->ip_summed = csummode;
928 /* reserve 8 byte for fragmentation */
929 skb_reserve(skb, hh_len+sizeof(struct frag_hdr));
932 * Find where to start putting bytes
934 data = skb_put(skb, fraglen);
935 skb->nh.raw = data + exthdrlen;
936 data += fragheaderlen;
937 skb->h.raw = data + exthdrlen;
938 copy = datalen - transhdrlen;
939 if (copy > 0 && getfrag(from, data + transhdrlen, offset, copy, 0, skb) < 0) {
949 csummode = CHECKSUM_NONE;
952 * Put the packet on the pending queue
954 __skb_queue_tail(&sk->sk_write_queue, skb);
961 if (!(rt->u.dst.dev->features&NETIF_F_SG)) {
965 if (getfrag(from, skb_put(skb, copy),
966 offset, copy, off, skb) < 0) {
967 __skb_trim(skb, off);
972 int i = skb_shinfo(skb)->nr_frags;
973 skb_frag_t *frag = &skb_shinfo(skb)->frags[i-1];
974 struct page *page = inet->sndmsg_page;
975 int off = inet->sndmsg_off;
978 if (page && (left = PAGE_SIZE - off) > 0) {
981 if (page != frag->page) {
982 if (i == MAX_SKB_FRAGS) {
987 skb_fill_page_desc(skb, i, page, inet->sndmsg_off, 0);
988 frag = &skb_shinfo(skb)->frags[i];
990 } else if(i < MAX_SKB_FRAGS) {
991 if (copy > PAGE_SIZE)
993 page = alloc_pages(sk->sk_allocation, 0);
998 inet->sndmsg_page = page;
999 inet->sndmsg_off = 0;
1001 skb_fill_page_desc(skb, i, page, 0, 0);
1002 frag = &skb_shinfo(skb)->frags[i];
1003 skb->truesize += PAGE_SIZE;
1004 atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
1009 if (getfrag(from, page_address(frag->page)+frag->page_offset+frag->size, offset, copy, skb->len, skb) < 0) {
1013 inet->sndmsg_off += copy;
1016 skb->data_len += copy;
1023 inet->cork.length -= length;
1024 IP6_INC_STATS(OutDiscards);
1028 int ip6_push_pending_frames(struct sock *sk)
1030 struct sk_buff *skb, *tmp_skb;
1031 struct sk_buff **tail_skb;
1032 struct in6_addr final_dst_buf, *final_dst = &final_dst_buf;
1033 struct inet_opt *inet = inet_sk(sk);
1034 struct ipv6_pinfo *np = inet6_sk(sk);
1035 struct ipv6hdr *hdr;
1036 struct ipv6_txoptions *opt = np->cork.opt;
1037 struct rt6_info *rt = np->cork.rt;
1038 struct flowi *fl = &inet->cork.fl;
1039 unsigned char proto = fl->proto;
1042 if ((skb = __skb_dequeue(&sk->sk_write_queue)) == NULL)
1044 tail_skb = &(skb_shinfo(skb)->frag_list);
1046 /* move skb->data to ip header from ext header */
1047 if (skb->data < skb->nh.raw)
1048 __skb_pull(skb, skb->nh.raw - skb->data);
1049 while ((tmp_skb = __skb_dequeue(&sk->sk_write_queue)) != NULL) {
1050 __skb_pull(tmp_skb, skb->h.raw - skb->nh.raw);
1051 *tail_skb = tmp_skb;
1052 tail_skb = &(tmp_skb->next);
1053 skb->len += tmp_skb->len;
1054 skb->data_len += tmp_skb->len;
1055 #if 0 /* Logically correct, but useless work, ip_fragment() will have to undo */
1056 skb->truesize += tmp_skb->truesize;
1057 __sock_put(tmp_skb->sk);
1058 tmp_skb->destructor = NULL;
1063 ipv6_addr_copy(final_dst, &fl->fl6_dst);
1064 __skb_pull(skb, skb->h.raw - skb->nh.raw);
1065 if (opt && opt->opt_flen)
1066 ipv6_push_frag_opts(skb, opt, &proto);
1067 if (opt && opt->opt_nflen)
1068 ipv6_push_nfrag_opts(skb, opt, &proto, &final_dst);
1070 skb->nh.ipv6h = hdr = (struct ipv6hdr*) skb_push(skb, sizeof(struct ipv6hdr));
1072 *(u32*)hdr = fl->fl6_flowlabel | htonl(0x60000000);
1074 if (skb->len <= sizeof(struct ipv6hdr) + IPV6_MAXPLEN)
1075 hdr->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
1077 hdr->payload_len = 0;
1078 hdr->hop_limit = np->cork.hop_limit;
1079 hdr->nexthdr = proto;
1080 ipv6_addr_copy(&hdr->saddr, &fl->fl6_src);
1081 ipv6_addr_copy(&hdr->daddr, final_dst);
1083 skb->dst = dst_clone(&rt->u.dst);
1084 IP6_INC_STATS(OutRequests);
1085 err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL, skb->dst->dev, dst_output);
1088 err = inet->recverr ? net_xmit_errno(err) : 0;
1094 inet->cork.flags &= ~IPCORK_OPT;
1096 kfree(np->cork.opt);
1097 np->cork.opt = NULL;
1100 dst_release(&np->cork.rt->u.dst);
1103 memset(&inet->cork.fl, 0, sizeof(inet->cork.fl));
1109 void ip6_flush_pending_frames(struct sock *sk)
1111 struct inet_opt *inet = inet_sk(sk);
1112 struct ipv6_pinfo *np = inet6_sk(sk);
1113 struct sk_buff *skb;
1115 while ((skb = __skb_dequeue_tail(&sk->sk_write_queue)) != NULL) {
1116 IP6_INC_STATS(OutDiscards);
1120 inet->cork.flags &= ~IPCORK_OPT;
1123 kfree(np->cork.opt);
1124 np->cork.opt = NULL;
1127 dst_release(&np->cork.rt->u.dst);
1130 memset(&inet->cork.fl, 0, sizeof(inet->cork.fl));