2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
6 * The Internet Protocol (IP) output module.
8 * Version: $Id: ip_output.c,v 1.100 2002/02/01 22:01:03 davem Exp $
11 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
12 * Donald Becker, <becker@super.org>
13 * Alan Cox, <Alan.Cox@linux.org>
15 * Stefan Becker, <stefanb@yello.ping.de>
16 * Jorge Cwik, <jorge@laser.satlink.net>
17 * Arnt Gulbrandsen, <agulbra@nvg.unit.no>
18 * Hirokazu Takahashi, <taka@valinux.co.jp>
20 * See ip_input.c for original log
23 * Alan Cox : Missing nonblock feature in ip_build_xmit.
24 * Mike Kilburn : htons() missing in ip_build_xmit.
25 * Bradford Johnson: Fix faulty handling of some frames when
27 * Alexander Demenshin: Missing sk/skb free in ip_queue_xmit
28 * (in case if packet not accepted by
29 * output firewall rules)
30 * Mike McLagan : Routing by source
31 * Alexey Kuznetsov: use new route cache
32 * Andi Kleen: Fix broken PMTU recovery and remove
33 * some redundant tests.
34 * Vitaly E. Lavrov : Transparent proxy revived after year coma.
35 * Andi Kleen : Replace ip_reply with ip_send_reply.
36 * Andi Kleen : Split fast and slow ip_build_xmit path
37 * for decreased register pressure on x86
38 * and more readibility.
39 * Marc Boucher : When call_out_firewall returns FW_QUEUE,
40 * silently drop skb instead of failing with -EPERM.
41 * Detlev Wengorz : Copy protocol for fragments.
42 * Hirokazu Takahashi: HW checksumming for outgoing UDP
44 * Hirokazu Takahashi: sendfile() on UDP works now.
47 #include <asm/uaccess.h>
48 #include <asm/system.h>
49 #include <linux/module.h>
50 #include <linux/types.h>
51 #include <linux/kernel.h>
53 #include <linux/string.h>
54 #include <linux/errno.h>
55 #include <linux/highmem.h>
57 #include <linux/socket.h>
58 #include <linux/sockios.h>
60 #include <linux/inet.h>
61 #include <linux/netdevice.h>
62 #include <linux/etherdevice.h>
63 #include <linux/proc_fs.h>
64 #include <linux/stat.h>
65 #include <linux/init.h>
69 #include <net/protocol.h>
70 #include <net/route.h>
72 #include <linux/skbuff.h>
76 #include <net/checksum.h>
77 #include <net/inetpeer.h>
78 #include <net/checksum.h>
79 #include <linux/igmp.h>
80 #include <linux/netfilter_ipv4.h>
81 #include <linux/netfilter_bridge.h>
82 #include <linux/mroute.h>
83 #include <linux/netlink.h>
84 #include <linux/tcp.h>
86 int sysctl_ip_default_ttl __read_mostly
= IPDEFTTL
;
88 /* Generate a checksum for an outgoing IP datagram. */
89 __inline__
void ip_send_check(struct iphdr
*iph
)
92 iph
->check
= ip_fast_csum((unsigned char *)iph
, iph
->ihl
);
95 int __ip_local_out(struct sk_buff
*skb
)
97 struct iphdr
*iph
= ip_hdr(skb
);
99 iph
->tot_len
= htons(skb
->len
);
101 return nf_hook(PF_INET
, NF_IP_LOCAL_OUT
, skb
, NULL
, skb
->dst
->dev
,
105 int ip_local_out(struct sk_buff
*skb
)
109 err
= __ip_local_out(skb
);
110 if (likely(err
== 1))
111 err
= dst_output(skb
);
115 EXPORT_SYMBOL_GPL(ip_local_out
);
117 /* dev_loopback_xmit for use with netfilter. */
118 static int ip_dev_loopback_xmit(struct sk_buff
*newskb
)
120 skb_reset_mac_header(newskb
);
121 __skb_pull(newskb
, skb_network_offset(newskb
));
122 newskb
->pkt_type
= PACKET_LOOPBACK
;
123 newskb
->ip_summed
= CHECKSUM_UNNECESSARY
;
124 BUG_TRAP(newskb
->dst
);
129 static inline int ip_select_ttl(struct inet_sock
*inet
, struct dst_entry
*dst
)
131 int ttl
= inet
->uc_ttl
;
134 ttl
= dst_metric(dst
, RTAX_HOPLIMIT
);
139 * Add an ip header to a skbuff and send it out.
142 int ip_build_and_send_pkt(struct sk_buff
*skb
, struct sock
*sk
,
143 __be32 saddr
, __be32 daddr
, struct ip_options
*opt
)
145 struct inet_sock
*inet
= inet_sk(sk
);
146 struct rtable
*rt
= (struct rtable
*)skb
->dst
;
149 /* Build the IP header. */
150 skb_push(skb
, sizeof(struct iphdr
) + (opt
? opt
->optlen
: 0));
151 skb_reset_network_header(skb
);
155 iph
->tos
= inet
->tos
;
156 if (ip_dont_fragment(sk
, &rt
->u
.dst
))
157 iph
->frag_off
= htons(IP_DF
);
160 iph
->ttl
= ip_select_ttl(inet
, &rt
->u
.dst
);
161 iph
->daddr
= rt
->rt_dst
;
162 iph
->saddr
= rt
->rt_src
;
163 iph
->protocol
= sk
->sk_protocol
;
164 ip_select_ident(iph
, &rt
->u
.dst
, sk
);
166 if (opt
&& opt
->optlen
) {
167 iph
->ihl
+= opt
->optlen
>>2;
168 ip_options_build(skb
, opt
, daddr
, rt
, 0);
171 skb
->priority
= sk
->sk_priority
;
174 return ip_local_out(skb
);
177 EXPORT_SYMBOL_GPL(ip_build_and_send_pkt
);
179 static inline int ip_finish_output2(struct sk_buff
*skb
)
181 struct dst_entry
*dst
= skb
->dst
;
182 struct rtable
*rt
= (struct rtable
*)dst
;
183 struct net_device
*dev
= dst
->dev
;
184 int hh_len
= LL_RESERVED_SPACE(dev
);
186 if (rt
->rt_type
== RTN_MULTICAST
)
187 IP_INC_STATS(IPSTATS_MIB_OUTMCASTPKTS
);
188 else if (rt
->rt_type
== RTN_BROADCAST
)
189 IP_INC_STATS(IPSTATS_MIB_OUTBCASTPKTS
);
191 /* Be paranoid, rather than too clever. */
192 if (unlikely(skb_headroom(skb
) < hh_len
&& dev
->hard_header
)) {
193 struct sk_buff
*skb2
;
195 skb2
= skb_realloc_headroom(skb
, LL_RESERVED_SPACE(dev
));
201 skb_set_owner_w(skb2
, skb
->sk
);
207 return neigh_hh_output(dst
->hh
, skb
);
208 else if (dst
->neighbour
)
209 return dst
->neighbour
->output(skb
);
212 printk(KERN_DEBUG
"ip_finish_output2: No header cache and no neighbour!\n");
217 static inline int ip_skb_dst_mtu(struct sk_buff
*skb
)
219 struct inet_sock
*inet
= skb
->sk
? inet_sk(skb
->sk
) : NULL
;
221 return (inet
&& inet
->pmtudisc
== IP_PMTUDISC_PROBE
) ?
222 skb
->dst
->dev
->mtu
: dst_mtu(skb
->dst
);
225 static inline int ip_finish_output(struct sk_buff
*skb
)
227 #if defined(CONFIG_NETFILTER) && defined(CONFIG_XFRM)
228 /* Policy lookup after SNAT yielded a new policy */
229 if (skb
->dst
->xfrm
!= NULL
) {
230 IPCB(skb
)->flags
|= IPSKB_REROUTED
;
231 return dst_output(skb
);
234 if (skb
->len
> ip_skb_dst_mtu(skb
) && !skb_is_gso(skb
))
235 return ip_fragment(skb
, ip_finish_output2
);
237 return ip_finish_output2(skb
);
240 int ip_mc_output(struct sk_buff
*skb
)
242 struct sock
*sk
= skb
->sk
;
243 struct rtable
*rt
= (struct rtable
*)skb
->dst
;
244 struct net_device
*dev
= rt
->u
.dst
.dev
;
247 * If the indicated interface is up and running, send the packet.
249 IP_INC_STATS(IPSTATS_MIB_OUTREQUESTS
);
252 skb
->protocol
= htons(ETH_P_IP
);
255 * Multicasts are looped back for other local users
258 if (rt
->rt_flags
&RTCF_MULTICAST
) {
259 if ((!sk
|| inet_sk(sk
)->mc_loop
)
260 #ifdef CONFIG_IP_MROUTE
261 /* Small optimization: do not loopback not local frames,
262 which returned after forwarding; they will be dropped
263 by ip_mr_input in any case.
264 Note, that local frames are looped back to be delivered
267 This check is duplicated in ip_mr_input at the moment.
269 && ((rt
->rt_flags
&RTCF_LOCAL
) || !(IPCB(skb
)->flags
&IPSKB_FORWARDED
))
272 struct sk_buff
*newskb
= skb_clone(skb
, GFP_ATOMIC
);
274 NF_HOOK(PF_INET
, NF_IP_POST_ROUTING
, newskb
, NULL
,
276 ip_dev_loopback_xmit
);
279 /* Multicasts with ttl 0 must not go beyond the host */
281 if (ip_hdr(skb
)->ttl
== 0) {
287 if (rt
->rt_flags
&RTCF_BROADCAST
) {
288 struct sk_buff
*newskb
= skb_clone(skb
, GFP_ATOMIC
);
290 NF_HOOK(PF_INET
, NF_IP_POST_ROUTING
, newskb
, NULL
,
291 newskb
->dev
, ip_dev_loopback_xmit
);
294 return NF_HOOK_COND(PF_INET
, NF_IP_POST_ROUTING
, skb
, NULL
, skb
->dev
,
296 !(IPCB(skb
)->flags
& IPSKB_REROUTED
));
299 int ip_output(struct sk_buff
*skb
)
301 struct net_device
*dev
= skb
->dst
->dev
;
303 IP_INC_STATS(IPSTATS_MIB_OUTREQUESTS
);
306 skb
->protocol
= htons(ETH_P_IP
);
308 return NF_HOOK_COND(PF_INET
, NF_IP_POST_ROUTING
, skb
, NULL
, dev
,
310 !(IPCB(skb
)->flags
& IPSKB_REROUTED
));
313 int ip_queue_xmit(struct sk_buff
*skb
, int ipfragok
)
315 struct sock
*sk
= skb
->sk
;
316 struct inet_sock
*inet
= inet_sk(sk
);
317 struct ip_options
*opt
= inet
->opt
;
321 /* Skip all of this if the packet is already routed,
322 * f.e. by something like SCTP.
324 rt
= (struct rtable
*) skb
->dst
;
328 /* Make sure we can route this packet. */
329 rt
= (struct rtable
*)__sk_dst_check(sk
, 0);
333 /* Use correct destination address if we have options. */
339 struct flowi fl
= { .oif
= sk
->sk_bound_dev_if
,
342 .saddr
= inet
->saddr
,
343 .tos
= RT_CONN_FLAGS(sk
) } },
344 .proto
= sk
->sk_protocol
,
346 { .sport
= inet
->sport
,
347 .dport
= inet
->dport
} } };
349 /* If this fails, retransmit mechanism of transport layer will
350 * keep trying until route appears or the connection times
353 security_sk_classify_flow(sk
, &fl
);
354 if (ip_route_output_flow(&rt
, &fl
, sk
, 0))
357 sk_setup_caps(sk
, &rt
->u
.dst
);
359 skb
->dst
= dst_clone(&rt
->u
.dst
);
362 if (opt
&& opt
->is_strictroute
&& rt
->rt_dst
!= rt
->rt_gateway
)
365 /* OK, we know where to send it, allocate and build IP header. */
366 skb_push(skb
, sizeof(struct iphdr
) + (opt
? opt
->optlen
: 0));
367 skb_reset_network_header(skb
);
369 *((__be16
*)iph
) = htons((4 << 12) | (5 << 8) | (inet
->tos
& 0xff));
370 if (ip_dont_fragment(sk
, &rt
->u
.dst
) && !ipfragok
)
371 iph
->frag_off
= htons(IP_DF
);
374 iph
->ttl
= ip_select_ttl(inet
, &rt
->u
.dst
);
375 iph
->protocol
= sk
->sk_protocol
;
376 iph
->saddr
= rt
->rt_src
;
377 iph
->daddr
= rt
->rt_dst
;
378 /* Transport layer set skb->h.foo itself. */
380 if (opt
&& opt
->optlen
) {
381 iph
->ihl
+= opt
->optlen
>> 2;
382 ip_options_build(skb
, opt
, inet
->daddr
, rt
, 0);
385 ip_select_ident_more(iph
, &rt
->u
.dst
, sk
,
386 (skb_shinfo(skb
)->gso_segs
?: 1) - 1);
388 skb
->priority
= sk
->sk_priority
;
390 return ip_local_out(skb
);
393 IP_INC_STATS(IPSTATS_MIB_OUTNOROUTES
);
395 return -EHOSTUNREACH
;
399 static void ip_copy_metadata(struct sk_buff
*to
, struct sk_buff
*from
)
401 to
->pkt_type
= from
->pkt_type
;
402 to
->priority
= from
->priority
;
403 to
->protocol
= from
->protocol
;
404 dst_release(to
->dst
);
405 to
->dst
= dst_clone(from
->dst
);
407 to
->mark
= from
->mark
;
409 /* Copy the flags to each fragment. */
410 IPCB(to
)->flags
= IPCB(from
)->flags
;
412 #ifdef CONFIG_NET_SCHED
413 to
->tc_index
= from
->tc_index
;
416 #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
417 to
->ipvs_property
= from
->ipvs_property
;
419 skb_copy_secmark(to
, from
);
423 * This IP datagram is too large to be sent in one piece. Break it up into
424 * smaller pieces (each of size equal to IP header plus
425 * a block of the data of the original IP data part) that will yet fit in a
426 * single device frame, and queue such a frame for sending.
429 int ip_fragment(struct sk_buff
*skb
, int (*output
)(struct sk_buff
*))
434 struct net_device
*dev
;
435 struct sk_buff
*skb2
;
436 unsigned int mtu
, hlen
, left
, len
, ll_rs
, pad
;
438 __be16 not_last_frag
;
439 struct rtable
*rt
= (struct rtable
*)skb
->dst
;
445 * Point into the IP datagram header.
450 if (unlikely((iph
->frag_off
& htons(IP_DF
)) && !skb
->local_df
)) {
451 IP_INC_STATS(IPSTATS_MIB_FRAGFAILS
);
452 icmp_send(skb
, ICMP_DEST_UNREACH
, ICMP_FRAG_NEEDED
,
453 htonl(ip_skb_dst_mtu(skb
)));
459 * Setup starting values.
463 mtu
= dst_mtu(&rt
->u
.dst
) - hlen
; /* Size of data space */
464 IPCB(skb
)->flags
|= IPSKB_FRAG_COMPLETE
;
466 /* When frag_list is given, use it. First, check its validity:
467 * some transformers could create wrong frag_list or break existing
468 * one, it is not prohibited. In this case fall back to copying.
470 * LATER: this step can be merged to real generation of fragments,
471 * we can switch to copy when see the first bad fragment.
473 if (skb_shinfo(skb
)->frag_list
) {
474 struct sk_buff
*frag
, *frag2
;
475 int first_len
= skb_pagelen(skb
);
477 if (first_len
- hlen
> mtu
||
478 ((first_len
- hlen
) & 7) ||
479 (iph
->frag_off
& htons(IP_MF
|IP_OFFSET
)) ||
483 for (frag
= skb_shinfo(skb
)->frag_list
; frag
; frag
= frag
->next
) {
484 /* Correct geometry. */
485 if (frag
->len
> mtu
||
486 ((frag
->len
& 7) && frag
->next
) ||
487 skb_headroom(frag
) < hlen
)
488 goto slow_path_clean
;
490 /* Partially cloned skb? */
491 if (skb_shared(frag
))
492 goto slow_path_clean
;
497 frag
->destructor
= sock_wfree
;
499 skb
->truesize
-= frag
->truesize
;
502 /* Everything is OK. Generate! */
506 frag
= skb_shinfo(skb
)->frag_list
;
507 skb_shinfo(skb
)->frag_list
= NULL
;
508 skb
->data_len
= first_len
- skb_headlen(skb
);
509 skb
->len
= first_len
;
510 iph
->tot_len
= htons(first_len
);
511 iph
->frag_off
= htons(IP_MF
);
515 /* Prepare header of the next frame,
516 * before previous one went down. */
518 frag
->ip_summed
= CHECKSUM_NONE
;
519 skb_reset_transport_header(frag
);
520 __skb_push(frag
, hlen
);
521 skb_reset_network_header(frag
);
522 memcpy(skb_network_header(frag
), iph
, hlen
);
524 iph
->tot_len
= htons(frag
->len
);
525 ip_copy_metadata(frag
, skb
);
527 ip_options_fragment(frag
);
528 offset
+= skb
->len
- hlen
;
529 iph
->frag_off
= htons(offset
>>3);
530 if (frag
->next
!= NULL
)
531 iph
->frag_off
|= htons(IP_MF
);
532 /* Ready, complete checksum */
539 IP_INC_STATS(IPSTATS_MIB_FRAGCREATES
);
549 IP_INC_STATS(IPSTATS_MIB_FRAGOKS
);
558 IP_INC_STATS(IPSTATS_MIB_FRAGFAILS
);
562 for (frag2
= skb_shinfo(skb
)->frag_list
; frag2
; frag2
= frag2
->next
) {
566 frag2
->destructor
= NULL
;
567 skb
->truesize
+= frag2
->truesize
;
572 left
= skb
->len
- hlen
; /* Space per frame */
573 ptr
= raw
+ hlen
; /* Where to start from */
575 /* for bridged IP traffic encapsulated inside f.e. a vlan header,
576 * we need to make room for the encapsulating header
578 pad
= nf_bridge_pad(skb
);
579 ll_rs
= LL_RESERVED_SPACE_EXTRA(rt
->u
.dst
.dev
, pad
);
583 * Fragment the datagram.
586 offset
= (ntohs(iph
->frag_off
) & IP_OFFSET
) << 3;
587 not_last_frag
= iph
->frag_off
& htons(IP_MF
);
590 * Keep copying data until we run out.
595 /* IF: it doesn't fit, use 'mtu' - the data space left */
598 /* IF: we are not sending upto and including the packet end
599 then align the next start on an eight byte boundary */
607 if ((skb2
= alloc_skb(len
+hlen
+ll_rs
, GFP_ATOMIC
)) == NULL
) {
608 NETDEBUG(KERN_INFO
"IP: frag: no memory for new fragment!\n");
614 * Set up data on packet
617 ip_copy_metadata(skb2
, skb
);
618 skb_reserve(skb2
, ll_rs
);
619 skb_put(skb2
, len
+ hlen
);
620 skb_reset_network_header(skb2
);
621 skb2
->transport_header
= skb2
->network_header
+ hlen
;
624 * Charge the memory for the fragment to any owner
629 skb_set_owner_w(skb2
, skb
->sk
);
632 * Copy the packet header into the new buffer.
635 skb_copy_from_linear_data(skb
, skb_network_header(skb2
), hlen
);
638 * Copy a block of the IP datagram.
640 if (skb_copy_bits(skb
, ptr
, skb_transport_header(skb2
), len
))
645 * Fill in the new header fields.
648 iph
->frag_off
= htons((offset
>> 3));
650 /* ANK: dirty, but effective trick. Upgrade options only if
651 * the segment to be fragmented was THE FIRST (otherwise,
652 * options are already fixed) and make it ONCE
653 * on the initial skb, so that all the following fragments
654 * will inherit fixed options.
657 ip_options_fragment(skb
);
660 * Added AC : If we are fragmenting a fragment that's not the
661 * last fragment then keep MF on each bit
663 if (left
> 0 || not_last_frag
)
664 iph
->frag_off
|= htons(IP_MF
);
669 * Put this fragment into the sending queue.
671 iph
->tot_len
= htons(len
+ hlen
);
679 IP_INC_STATS(IPSTATS_MIB_FRAGCREATES
);
682 IP_INC_STATS(IPSTATS_MIB_FRAGOKS
);
687 IP_INC_STATS(IPSTATS_MIB_FRAGFAILS
);
691 EXPORT_SYMBOL(ip_fragment
);
694 ip_generic_getfrag(void *from
, char *to
, int offset
, int len
, int odd
, struct sk_buff
*skb
)
696 struct iovec
*iov
= from
;
698 if (skb
->ip_summed
== CHECKSUM_PARTIAL
) {
699 if (memcpy_fromiovecend(to
, iov
, offset
, len
) < 0)
703 if (csum_partial_copy_fromiovecend(to
, iov
, offset
, len
, &csum
) < 0)
705 skb
->csum
= csum_block_add(skb
->csum
, csum
, odd
);
711 csum_page(struct page
*page
, int offset
, int copy
)
716 csum
= csum_partial(kaddr
+ offset
, copy
, 0);
721 static inline int ip_ufo_append_data(struct sock
*sk
,
722 int getfrag(void *from
, char *to
, int offset
, int len
,
723 int odd
, struct sk_buff
*skb
),
724 void *from
, int length
, int hh_len
, int fragheaderlen
,
725 int transhdrlen
, int mtu
,unsigned int flags
)
730 /* There is support for UDP fragmentation offload by network
731 * device, so create one single skb packet containing complete
734 if ((skb
= skb_peek_tail(&sk
->sk_write_queue
)) == NULL
) {
735 skb
= sock_alloc_send_skb(sk
,
736 hh_len
+ fragheaderlen
+ transhdrlen
+ 20,
737 (flags
& MSG_DONTWAIT
), &err
);
742 /* reserve space for Hardware header */
743 skb_reserve(skb
, hh_len
);
745 /* create space for UDP/IP header */
746 skb_put(skb
,fragheaderlen
+ transhdrlen
);
748 /* initialize network header pointer */
749 skb_reset_network_header(skb
);
751 /* initialize protocol header pointer */
752 skb
->transport_header
= skb
->network_header
+ fragheaderlen
;
754 skb
->ip_summed
= CHECKSUM_PARTIAL
;
756 sk
->sk_sndmsg_off
= 0;
758 /* specify the length of each IP datagram fragment */
759 skb_shinfo(skb
)->gso_size
= mtu
- fragheaderlen
;
760 skb_shinfo(skb
)->gso_type
= SKB_GSO_UDP
;
761 __skb_queue_tail(&sk
->sk_write_queue
, skb
);
764 return skb_append_datato_frags(sk
, skb
, getfrag
, from
,
765 (length
- transhdrlen
));
769 * ip_append_data() and ip_append_page() can make one large IP datagram
770 * from many pieces of data. Each pieces will be holded on the socket
771 * until ip_push_pending_frames() is called. Each piece can be a page
774 * Not only UDP, other transport protocols - e.g. raw sockets - can use
775 * this interface potentially.
777 * LATER: length must be adjusted by pad at tail, when it is required.
779 int ip_append_data(struct sock
*sk
,
780 int getfrag(void *from
, char *to
, int offset
, int len
,
781 int odd
, struct sk_buff
*skb
),
782 void *from
, int length
, int transhdrlen
,
783 struct ipcm_cookie
*ipc
, struct rtable
*rt
,
786 struct inet_sock
*inet
= inet_sk(sk
);
789 struct ip_options
*opt
= NULL
;
796 unsigned int maxfraglen
, fragheaderlen
;
797 int csummode
= CHECKSUM_NONE
;
802 if (skb_queue_empty(&sk
->sk_write_queue
)) {
808 if (inet
->cork
.opt
== NULL
) {
809 inet
->cork
.opt
= kmalloc(sizeof(struct ip_options
) + 40, sk
->sk_allocation
);
810 if (unlikely(inet
->cork
.opt
== NULL
))
813 memcpy(inet
->cork
.opt
, opt
, sizeof(struct ip_options
)+opt
->optlen
);
814 inet
->cork
.flags
|= IPCORK_OPT
;
815 inet
->cork
.addr
= ipc
->addr
;
817 dst_hold(&rt
->u
.dst
);
818 inet
->cork
.fragsize
= mtu
= inet
->pmtudisc
== IP_PMTUDISC_PROBE
?
820 dst_mtu(rt
->u
.dst
.path
);
822 inet
->cork
.length
= 0;
823 sk
->sk_sndmsg_page
= NULL
;
824 sk
->sk_sndmsg_off
= 0;
825 if ((exthdrlen
= rt
->u
.dst
.header_len
) != 0) {
827 transhdrlen
+= exthdrlen
;
831 if (inet
->cork
.flags
& IPCORK_OPT
)
832 opt
= inet
->cork
.opt
;
836 mtu
= inet
->cork
.fragsize
;
838 hh_len
= LL_RESERVED_SPACE(rt
->u
.dst
.dev
);
840 fragheaderlen
= sizeof(struct iphdr
) + (opt
? opt
->optlen
: 0);
841 maxfraglen
= ((mtu
- fragheaderlen
) & ~7) + fragheaderlen
;
843 if (inet
->cork
.length
+ length
> 0xFFFF - fragheaderlen
) {
844 ip_local_error(sk
, EMSGSIZE
, rt
->rt_dst
, inet
->dport
, mtu
-exthdrlen
);
849 * transhdrlen > 0 means that this is the first fragment and we wish
850 * it won't be fragmented in the future.
853 length
+ fragheaderlen
<= mtu
&&
854 rt
->u
.dst
.dev
->features
& NETIF_F_ALL_CSUM
&&
856 csummode
= CHECKSUM_PARTIAL
;
858 inet
->cork
.length
+= length
;
859 if (((length
> mtu
) || !skb_queue_empty(&sk
->sk_write_queue
)) &&
860 (sk
->sk_protocol
== IPPROTO_UDP
) &&
861 (rt
->u
.dst
.dev
->features
& NETIF_F_UFO
)) {
862 err
= ip_ufo_append_data(sk
, getfrag
, from
, length
, hh_len
,
863 fragheaderlen
, transhdrlen
, mtu
,
870 /* So, what's going on in the loop below?
872 * We use calculated fragment length to generate chained skb,
873 * each of segments is IP fragment ready for sending to network after
874 * adding appropriate IP header.
877 if ((skb
= skb_peek_tail(&sk
->sk_write_queue
)) == NULL
)
881 /* Check if the remaining data fits into current packet. */
882 copy
= mtu
- skb
->len
;
884 copy
= maxfraglen
- skb
->len
;
887 unsigned int datalen
;
888 unsigned int fraglen
;
889 unsigned int fraggap
;
890 unsigned int alloclen
;
891 struct sk_buff
*skb_prev
;
895 fraggap
= skb_prev
->len
- maxfraglen
;
900 * If remaining data exceeds the mtu,
901 * we know we need more fragment(s).
903 datalen
= length
+ fraggap
;
904 if (datalen
> mtu
- fragheaderlen
)
905 datalen
= maxfraglen
- fragheaderlen
;
906 fraglen
= datalen
+ fragheaderlen
;
908 if ((flags
& MSG_MORE
) &&
909 !(rt
->u
.dst
.dev
->features
&NETIF_F_SG
))
912 alloclen
= datalen
+ fragheaderlen
;
914 /* The last fragment gets additional space at tail.
915 * Note, with MSG_MORE we overallocate on fragments,
916 * because we have no idea what fragment will be
919 if (datalen
== length
+ fraggap
)
920 alloclen
+= rt
->u
.dst
.trailer_len
;
923 skb
= sock_alloc_send_skb(sk
,
924 alloclen
+ hh_len
+ 15,
925 (flags
& MSG_DONTWAIT
), &err
);
928 if (atomic_read(&sk
->sk_wmem_alloc
) <=
930 skb
= sock_wmalloc(sk
,
931 alloclen
+ hh_len
+ 15, 1,
933 if (unlikely(skb
== NULL
))
940 * Fill in the control structures
942 skb
->ip_summed
= csummode
;
944 skb_reserve(skb
, hh_len
);
947 * Find where to start putting bytes.
949 data
= skb_put(skb
, fraglen
);
950 skb_set_network_header(skb
, exthdrlen
);
951 skb
->transport_header
= (skb
->network_header
+
953 data
+= fragheaderlen
;
956 skb
->csum
= skb_copy_and_csum_bits(
957 skb_prev
, maxfraglen
,
958 data
+ transhdrlen
, fraggap
, 0);
959 skb_prev
->csum
= csum_sub(skb_prev
->csum
,
962 pskb_trim_unique(skb_prev
, maxfraglen
);
965 copy
= datalen
- transhdrlen
- fraggap
;
966 if (copy
> 0 && getfrag(from
, data
+ transhdrlen
, offset
, copy
, fraggap
, skb
) < 0) {
973 length
-= datalen
- fraggap
;
976 csummode
= CHECKSUM_NONE
;
979 * Put the packet on the pending queue.
981 __skb_queue_tail(&sk
->sk_write_queue
, skb
);
988 if (!(rt
->u
.dst
.dev
->features
&NETIF_F_SG
)) {
992 if (getfrag(from
, skb_put(skb
, copy
),
993 offset
, copy
, off
, skb
) < 0) {
994 __skb_trim(skb
, off
);
999 int i
= skb_shinfo(skb
)->nr_frags
;
1000 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
-1];
1001 struct page
*page
= sk
->sk_sndmsg_page
;
1002 int off
= sk
->sk_sndmsg_off
;
1005 if (page
&& (left
= PAGE_SIZE
- off
) > 0) {
1008 if (page
!= frag
->page
) {
1009 if (i
== MAX_SKB_FRAGS
) {
1014 skb_fill_page_desc(skb
, i
, page
, sk
->sk_sndmsg_off
, 0);
1015 frag
= &skb_shinfo(skb
)->frags
[i
];
1017 } else if (i
< MAX_SKB_FRAGS
) {
1018 if (copy
> PAGE_SIZE
)
1020 page
= alloc_pages(sk
->sk_allocation
, 0);
1025 sk
->sk_sndmsg_page
= page
;
1026 sk
->sk_sndmsg_off
= 0;
1028 skb_fill_page_desc(skb
, i
, page
, 0, 0);
1029 frag
= &skb_shinfo(skb
)->frags
[i
];
1034 if (getfrag(from
, page_address(frag
->page
)+frag
->page_offset
+frag
->size
, offset
, copy
, skb
->len
, skb
) < 0) {
1038 sk
->sk_sndmsg_off
+= copy
;
1041 skb
->data_len
+= copy
;
1042 skb
->truesize
+= copy
;
1043 atomic_add(copy
, &sk
->sk_wmem_alloc
);
1052 inet
->cork
.length
-= length
;
1053 IP_INC_STATS(IPSTATS_MIB_OUTDISCARDS
);
1057 ssize_t
ip_append_page(struct sock
*sk
, struct page
*page
,
1058 int offset
, size_t size
, int flags
)
1060 struct inet_sock
*inet
= inet_sk(sk
);
1061 struct sk_buff
*skb
;
1063 struct ip_options
*opt
= NULL
;
1068 unsigned int maxfraglen
, fragheaderlen
, fraggap
;
1073 if (flags
&MSG_PROBE
)
1076 if (skb_queue_empty(&sk
->sk_write_queue
))
1080 if (inet
->cork
.flags
& IPCORK_OPT
)
1081 opt
= inet
->cork
.opt
;
1083 if (!(rt
->u
.dst
.dev
->features
&NETIF_F_SG
))
1086 hh_len
= LL_RESERVED_SPACE(rt
->u
.dst
.dev
);
1087 mtu
= inet
->cork
.fragsize
;
1089 fragheaderlen
= sizeof(struct iphdr
) + (opt
? opt
->optlen
: 0);
1090 maxfraglen
= ((mtu
- fragheaderlen
) & ~7) + fragheaderlen
;
1092 if (inet
->cork
.length
+ size
> 0xFFFF - fragheaderlen
) {
1093 ip_local_error(sk
, EMSGSIZE
, rt
->rt_dst
, inet
->dport
, mtu
);
1097 if ((skb
= skb_peek_tail(&sk
->sk_write_queue
)) == NULL
)
1100 inet
->cork
.length
+= size
;
1101 if ((sk
->sk_protocol
== IPPROTO_UDP
) &&
1102 (rt
->u
.dst
.dev
->features
& NETIF_F_UFO
)) {
1103 skb_shinfo(skb
)->gso_size
= mtu
- fragheaderlen
;
1104 skb_shinfo(skb
)->gso_type
= SKB_GSO_UDP
;
1111 if (skb_is_gso(skb
))
1115 /* Check if the remaining data fits into current packet. */
1116 len
= mtu
- skb
->len
;
1118 len
= maxfraglen
- skb
->len
;
1121 struct sk_buff
*skb_prev
;
1125 fraggap
= skb_prev
->len
- maxfraglen
;
1127 alloclen
= fragheaderlen
+ hh_len
+ fraggap
+ 15;
1128 skb
= sock_wmalloc(sk
, alloclen
, 1, sk
->sk_allocation
);
1129 if (unlikely(!skb
)) {
1135 * Fill in the control structures
1137 skb
->ip_summed
= CHECKSUM_NONE
;
1139 skb_reserve(skb
, hh_len
);
1142 * Find where to start putting bytes.
1144 skb_put(skb
, fragheaderlen
+ fraggap
);
1145 skb_reset_network_header(skb
);
1146 skb
->transport_header
= (skb
->network_header
+
1149 skb
->csum
= skb_copy_and_csum_bits(skb_prev
,
1151 skb_transport_header(skb
),
1153 skb_prev
->csum
= csum_sub(skb_prev
->csum
,
1155 pskb_trim_unique(skb_prev
, maxfraglen
);
1159 * Put the packet on the pending queue.
1161 __skb_queue_tail(&sk
->sk_write_queue
, skb
);
1165 i
= skb_shinfo(skb
)->nr_frags
;
1168 if (skb_can_coalesce(skb
, i
, page
, offset
)) {
1169 skb_shinfo(skb
)->frags
[i
-1].size
+= len
;
1170 } else if (i
< MAX_SKB_FRAGS
) {
1172 skb_fill_page_desc(skb
, i
, page
, offset
, len
);
1178 if (skb
->ip_summed
== CHECKSUM_NONE
) {
1180 csum
= csum_page(page
, offset
, len
);
1181 skb
->csum
= csum_block_add(skb
->csum
, csum
, skb
->len
);
1185 skb
->data_len
+= len
;
1186 skb
->truesize
+= len
;
1187 atomic_add(len
, &sk
->sk_wmem_alloc
);
1194 inet
->cork
.length
-= size
;
1195 IP_INC_STATS(IPSTATS_MIB_OUTDISCARDS
);
1200 * Combined all pending IP fragments on the socket as one IP datagram
1201 * and push them out.
1203 int ip_push_pending_frames(struct sock
*sk
)
1205 struct sk_buff
*skb
, *tmp_skb
;
1206 struct sk_buff
**tail_skb
;
1207 struct inet_sock
*inet
= inet_sk(sk
);
1208 struct ip_options
*opt
= NULL
;
1209 struct rtable
*rt
= inet
->cork
.rt
;
1215 if ((skb
= __skb_dequeue(&sk
->sk_write_queue
)) == NULL
)
1217 tail_skb
= &(skb_shinfo(skb
)->frag_list
);
1219 /* move skb->data to ip header from ext header */
1220 if (skb
->data
< skb_network_header(skb
))
1221 __skb_pull(skb
, skb_network_offset(skb
));
1222 while ((tmp_skb
= __skb_dequeue(&sk
->sk_write_queue
)) != NULL
) {
1223 __skb_pull(tmp_skb
, skb_network_header_len(skb
));
1224 *tail_skb
= tmp_skb
;
1225 tail_skb
= &(tmp_skb
->next
);
1226 skb
->len
+= tmp_skb
->len
;
1227 skb
->data_len
+= tmp_skb
->len
;
1228 skb
->truesize
+= tmp_skb
->truesize
;
1229 tmp_skb
->destructor
= NULL
;
1233 /* Unless user demanded real pmtu discovery (IP_PMTUDISC_DO), we allow
1234 * to fragment the frame generated here. No matter, what transforms
1235 * how transforms change size of the packet, it will come out.
1237 if (inet
->pmtudisc
< IP_PMTUDISC_DO
)
1240 /* DF bit is set when we want to see DF on outgoing frames.
1241 * If local_df is set too, we still allow to fragment this frame
1243 if (inet
->pmtudisc
>= IP_PMTUDISC_DO
||
1244 (skb
->len
<= dst_mtu(&rt
->u
.dst
) &&
1245 ip_dont_fragment(sk
, &rt
->u
.dst
)))
1248 if (inet
->cork
.flags
& IPCORK_OPT
)
1249 opt
= inet
->cork
.opt
;
1251 if (rt
->rt_type
== RTN_MULTICAST
)
1254 ttl
= ip_select_ttl(inet
, &rt
->u
.dst
);
1256 iph
= (struct iphdr
*)skb
->data
;
1260 iph
->ihl
+= opt
->optlen
>>2;
1261 ip_options_build(skb
, opt
, inet
->cork
.addr
, rt
, 0);
1263 iph
->tos
= inet
->tos
;
1265 ip_select_ident(iph
, &rt
->u
.dst
, sk
);
1267 iph
->protocol
= sk
->sk_protocol
;
1268 iph
->saddr
= rt
->rt_src
;
1269 iph
->daddr
= rt
->rt_dst
;
1271 skb
->priority
= sk
->sk_priority
;
1272 skb
->dst
= dst_clone(&rt
->u
.dst
);
1274 /* Netfilter gets whole the not fragmented skb. */
1275 err
= ip_local_out(skb
);
1278 err
= inet
->recverr
? net_xmit_errno(err
) : 0;
1284 inet
->cork
.flags
&= ~IPCORK_OPT
;
1285 kfree(inet
->cork
.opt
);
1286 inet
->cork
.opt
= NULL
;
1287 if (inet
->cork
.rt
) {
1288 ip_rt_put(inet
->cork
.rt
);
1289 inet
->cork
.rt
= NULL
;
1294 IP_INC_STATS(IPSTATS_MIB_OUTDISCARDS
);
1299 * Throw away all pending data on the socket.
1301 void ip_flush_pending_frames(struct sock
*sk
)
1303 struct inet_sock
*inet
= inet_sk(sk
);
1304 struct sk_buff
*skb
;
1306 while ((skb
= __skb_dequeue_tail(&sk
->sk_write_queue
)) != NULL
)
1309 inet
->cork
.flags
&= ~IPCORK_OPT
;
1310 kfree(inet
->cork
.opt
);
1311 inet
->cork
.opt
= NULL
;
1312 if (inet
->cork
.rt
) {
1313 ip_rt_put(inet
->cork
.rt
);
1314 inet
->cork
.rt
= NULL
;
1320 * Fetch data from kernel space and fill in checksum if needed.
1322 static int ip_reply_glue_bits(void *dptr
, char *to
, int offset
,
1323 int len
, int odd
, struct sk_buff
*skb
)
1327 csum
= csum_partial_copy_nocheck(dptr
+offset
, to
, len
, 0);
1328 skb
->csum
= csum_block_add(skb
->csum
, csum
, odd
);
1333 * Generic function to send a packet as reply to another packet.
1334 * Used to send TCP resets so far. ICMP should use this function too.
1336 * Should run single threaded per socket because it uses the sock
1337 * structure to pass arguments.
1339 * LATER: switch from ip_build_xmit to ip_append_*
1341 void ip_send_reply(struct sock
*sk
, struct sk_buff
*skb
, struct ip_reply_arg
*arg
,
1344 struct inet_sock
*inet
= inet_sk(sk
);
1346 struct ip_options opt
;
1349 struct ipcm_cookie ipc
;
1351 struct rtable
*rt
= (struct rtable
*)skb
->dst
;
1353 if (ip_options_echo(&replyopts
.opt
, skb
))
1356 daddr
= ipc
.addr
= rt
->rt_src
;
1359 if (replyopts
.opt
.optlen
) {
1360 ipc
.opt
= &replyopts
.opt
;
1363 daddr
= replyopts
.opt
.faddr
;
1367 struct flowi fl
= { .oif
= arg
->bound_dev_if
,
1370 .saddr
= rt
->rt_spec_dst
,
1371 .tos
= RT_TOS(ip_hdr(skb
)->tos
) } },
1372 /* Not quite clean, but right. */
1374 { .sport
= tcp_hdr(skb
)->dest
,
1375 .dport
= tcp_hdr(skb
)->source
} },
1376 .proto
= sk
->sk_protocol
};
1377 security_skb_classify_flow(skb
, &fl
);
1378 if (ip_route_output_key(&rt
, &fl
))
1382 /* And let IP do all the hard work.
1384 This chunk is not reenterable, hence spinlock.
1385 Note that it uses the fact, that this function is called
1386 with locally disabled BH and that sk cannot be already spinlocked.
1389 inet
->tos
= ip_hdr(skb
)->tos
;
1390 sk
->sk_priority
= skb
->priority
;
1391 sk
->sk_protocol
= ip_hdr(skb
)->protocol
;
1392 sk
->sk_bound_dev_if
= arg
->bound_dev_if
;
1393 ip_append_data(sk
, ip_reply_glue_bits
, arg
->iov
->iov_base
, len
, 0,
1394 &ipc
, rt
, MSG_DONTWAIT
);
1395 if ((skb
= skb_peek(&sk
->sk_write_queue
)) != NULL
) {
1396 if (arg
->csumoffset
>= 0)
1397 *((__sum16
*)skb_transport_header(skb
) +
1398 arg
->csumoffset
) = csum_fold(csum_add(skb
->csum
,
1400 skb
->ip_summed
= CHECKSUM_NONE
;
1401 ip_push_pending_frames(sk
);
1409 void __init
ip_init(void)
1414 #if defined(CONFIG_IP_MULTICAST) && defined(CONFIG_PROC_FS)
1415 igmp_mc_proc_init();
1419 EXPORT_SYMBOL(ip_generic_getfrag
);
1420 EXPORT_SYMBOL(ip_queue_xmit
);
1421 EXPORT_SYMBOL(ip_send_check
);