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.83 2000/03/25 01:52:08 davem Exp $
10 * Authors: Ross Biro, <bir7@leland.Stanford.Edu>
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>
19 * See ip_input.c for original log
22 * Alan Cox : Missing nonblock feature in ip_build_xmit.
23 * Mike Kilburn : htons() missing in ip_build_xmit.
24 * Bradford Johnson: Fix faulty handling of some frames when
26 * Alexander Demenshin: Missing sk/skb free in ip_queue_xmit
27 * (in case if packet not accepted by
28 * output firewall rules)
29 * Mike McLagan : Routing by source
30 * Alexey Kuznetsov: use new route cache
31 * Andi Kleen: Fix broken PMTU recovery and remove
32 * some redundant tests.
33 * Vitaly E. Lavrov : Transparent proxy revived after year coma.
34 * Andi Kleen : Replace ip_reply with ip_send_reply.
35 * Andi Kleen : Split fast and slow ip_build_xmit path
36 * for decreased register pressure on x86
37 * and more readibility.
38 * Marc Boucher : When call_out_firewall returns FW_QUEUE,
39 * silently drop skb instead of failing with -EPERM.
42 #include <asm/uaccess.h>
43 #include <asm/system.h>
44 #include <linux/types.h>
45 #include <linux/kernel.h>
46 #include <linux/sched.h>
48 #include <linux/string.h>
49 #include <linux/errno.h>
50 #include <linux/config.h>
52 #include <linux/socket.h>
53 #include <linux/sockios.h>
55 #include <linux/inet.h>
56 #include <linux/netdevice.h>
57 #include <linux/etherdevice.h>
58 #include <linux/proc_fs.h>
59 #include <linux/stat.h>
60 #include <linux/init.h>
64 #include <net/protocol.h>
65 #include <net/route.h>
68 #include <linux/skbuff.h>
73 #include <net/checksum.h>
74 #include <net/inetpeer.h>
75 #include <linux/igmp.h>
76 #include <linux/netfilter_ipv4.h>
77 #include <linux/mroute.h>
78 #include <linux/netlink.h>
81 * Shall we try to damage output packets if routing dev changes?
84 int sysctl_ip_dynaddr
= 0;
85 int sysctl_ip_default_ttl
= IPDEFTTL
;
87 /* Generate a checksum for an outgoing IP datagram. */
88 __inline__
void ip_send_check(struct iphdr
*iph
)
91 iph
->check
= ip_fast_csum((unsigned char *)iph
, iph
->ihl
);
94 /* dev_loopback_xmit for use with netfilter. */
95 static int ip_dev_loopback_xmit(struct sk_buff
*newskb
)
97 newskb
->mac
.raw
= newskb
->data
;
98 skb_pull(newskb
, newskb
->nh
.raw
- newskb
->data
);
99 newskb
->pkt_type
= PACKET_LOOPBACK
;
100 newskb
->ip_summed
= CHECKSUM_UNNECESSARY
;
101 BUG_TRAP(newskb
->dst
);
103 #ifdef CONFIG_NETFILTER_DEBUG
104 nf_debug_ip_loopback_xmit(newskb
);
110 #ifdef CONFIG_NETFILTER
111 /* To preserve the cute illusion that a locally-generated packet can
112 be mangled before routing, we actually reroute if a hook altered
114 static int route_me_harder(struct sk_buff
*skb
)
116 struct iphdr
*iph
= skb
->nh
.iph
;
119 if (ip_route_output(&rt
, iph
->daddr
, iph
->saddr
,
120 RT_TOS(iph
->tos
) | RTO_CONN
,
121 skb
->sk
? skb
->sk
->bound_dev_if
: 0)) {
122 printk("route_me_harder: No more route.\n");
126 /* Drop old route. */
127 dst_release(skb
->dst
);
129 skb
->dst
= &rt
->u
.dst
;
134 /* Do route recalc if netfilter changes skb. */
136 output_maybe_reroute(struct sk_buff
*skb
)
138 #ifdef CONFIG_NETFILTER
139 if (skb
->nfcache
& NFC_ALTERED
) {
140 if (route_me_harder(skb
) != 0) {
146 return skb
->dst
->output(skb
);
150 * Add an ip header to a skbuff and send it out.
152 int ip_build_and_send_pkt(struct sk_buff
*skb
, struct sock
*sk
,
153 u32 saddr
, u32 daddr
, struct ip_options
*opt
)
155 struct rtable
*rt
= (struct rtable
*)skb
->dst
;
158 /* Build the IP header. */
160 iph
=(struct iphdr
*)skb_push(skb
,sizeof(struct iphdr
) + opt
->optlen
);
162 iph
=(struct iphdr
*)skb_push(skb
,sizeof(struct iphdr
));
166 iph
->tos
= sk
->protinfo
.af_inet
.tos
;
168 if (ip_dont_fragment(sk
, &rt
->u
.dst
))
169 iph
->frag_off
|= htons(IP_DF
);
170 iph
->ttl
= sk
->protinfo
.af_inet
.ttl
;
171 iph
->daddr
= rt
->rt_dst
;
172 iph
->saddr
= rt
->rt_src
;
173 iph
->protocol
= sk
->protocol
;
174 iph
->tot_len
= htons(skb
->len
);
175 ip_select_ident(iph
, &rt
->u
.dst
);
178 if (opt
&& opt
->optlen
) {
179 iph
->ihl
+= opt
->optlen
>>2;
180 ip_options_build(skb
, opt
, daddr
, rt
, 0);
185 return NF_HOOK(PF_INET
, NF_IP_LOCAL_OUT
, skb
, NULL
, rt
->u
.dst
.dev
,
186 output_maybe_reroute
);
189 static inline int ip_finish_output2(struct sk_buff
*skb
)
191 struct dst_entry
*dst
= skb
->dst
;
192 struct hh_cache
*hh
= dst
->hh
;
194 #ifdef CONFIG_NETFILTER_DEBUG
195 nf_debug_ip_finish_output2(skb
);
196 #endif /*CONFIG_NETFILTER_DEBUG*/
199 read_lock_bh(&hh
->hh_lock
);
200 memcpy(skb
->data
- 16, hh
->hh_data
, 16);
201 read_unlock_bh(&hh
->hh_lock
);
202 skb_push(skb
, hh
->hh_len
);
203 return hh
->hh_output(skb
);
204 } else if (dst
->neighbour
)
205 return dst
->neighbour
->output(skb
);
207 printk(KERN_DEBUG
"khm\n");
212 __inline__
int ip_finish_output(struct sk_buff
*skb
)
214 struct net_device
*dev
= skb
->dst
->dev
;
217 skb
->protocol
= __constant_htons(ETH_P_IP
);
219 return NF_HOOK(PF_INET
, NF_IP_POST_ROUTING
, skb
, NULL
, dev
,
223 int ip_mc_output(struct sk_buff
*skb
)
225 struct sock
*sk
= skb
->sk
;
226 struct rtable
*rt
= (struct rtable
*)skb
->dst
;
227 struct net_device
*dev
= rt
->u
.dst
.dev
;
230 * If the indicated interface is up and running, send the packet.
232 IP_INC_STATS(IpOutRequests
);
233 #ifdef CONFIG_IP_ROUTE_NAT
234 if (rt
->rt_flags
& RTCF_NAT
)
239 skb
->protocol
= __constant_htons(ETH_P_IP
);
242 * Multicasts are looped back for other local users
245 if (rt
->rt_flags
&RTCF_MULTICAST
&& (!sk
|| sk
->protinfo
.af_inet
.mc_loop
)) {
246 #ifdef CONFIG_IP_MROUTE
247 /* Small optimization: do not loopback not local frames,
248 which returned after forwarding; they will be dropped
249 by ip_mr_input in any case.
250 Note, that local frames are looped back to be delivered
253 This check is duplicated in ip_mr_input at the moment.
255 if ((rt
->rt_flags
&RTCF_LOCAL
) || !(IPCB(skb
)->flags
&IPSKB_FORWARDED
))
258 struct sk_buff
*newskb
= skb_clone(skb
, GFP_ATOMIC
);
260 NF_HOOK(PF_INET
, NF_IP_POST_ROUTING
, newskb
, NULL
,
262 ip_dev_loopback_xmit
);
265 /* Multicasts with ttl 0 must not go beyond the host */
267 if (skb
->nh
.iph
->ttl
== 0) {
273 if (rt
->rt_flags
&RTCF_BROADCAST
) {
274 struct sk_buff
*newskb
= skb_clone(skb
, GFP_ATOMIC
);
276 NF_HOOK(PF_INET
, NF_IP_POST_ROUTING
, newskb
, NULL
,
277 newskb
->dev
, ip_dev_loopback_xmit
);
280 return ip_finish_output(skb
);
283 int ip_output(struct sk_buff
*skb
)
285 #ifdef CONFIG_IP_ROUTE_NAT
286 struct rtable
*rt
= (struct rtable
*)skb
->dst
;
289 IP_INC_STATS(IpOutRequests
);
291 #ifdef CONFIG_IP_ROUTE_NAT
292 if (rt
->rt_flags
&RTCF_NAT
)
296 return ip_finish_output(skb
);
299 /* Queues a packet to be sent, and starts the transmitter if necessary.
300 * This routine also needs to put in the total length and compute the
301 * checksum. We use to do this in two stages, ip_build_header() then
302 * this, but that scheme created a mess when routes disappeared etc.
303 * So we do it all here, and the TCP send engine has been changed to
304 * match. (No more unroutable FIN disasters, etc. wheee...) This will
305 * most likely make other reliable transport layers above IP easier
306 * to implement under Linux.
308 static inline int ip_queue_xmit2(struct sk_buff
*skb
)
310 struct sock
*sk
= skb
->sk
;
311 struct rtable
*rt
= (struct rtable
*)skb
->dst
;
312 struct net_device
*dev
;
313 struct iphdr
*iph
= skb
->nh
.iph
;
315 #ifdef CONFIG_NETFILTER
316 /* BLUE-PEN-FOR-ALEXEY. I don't understand; you mean I can't
317 hold the route as I pass the packet to userspace? -- RR
319 You may hold it, if you really hold it. F.e. if netfilter
320 does not destroy handed skb with skb->dst attached, it
321 will be held. When it was stored in info->arg, then
322 it was not held apparently. Now (without second arg) it is evident,
323 that it is clean. --ANK
325 if (rt
==NULL
|| (skb
->nfcache
& NFC_ALTERED
)) {
326 if (route_me_harder(skb
) != 0) {
328 return -EHOSTUNREACH
;
335 /* This can happen when the transport layer has segments queued
336 * with a cached route, and by the time we get here things are
337 * re-routed to a device with a different MTU than the original
338 * device. Sick, but we must cover it.
340 if (skb_headroom(skb
) < dev
->hard_header_len
&& dev
->hard_header
) {
341 struct sk_buff
*skb2
;
343 skb2
= skb_realloc_headroom(skb
, (dev
->hard_header_len
+ 15) & ~15);
348 skb_set_owner_w(skb2
, sk
);
353 if (skb
->len
> rt
->u
.dst
.pmtu
)
356 if (ip_dont_fragment(sk
, &rt
->u
.dst
))
357 iph
->frag_off
|= __constant_htons(IP_DF
);
359 ip_select_ident(iph
, &rt
->u
.dst
);
361 /* Add an IP checksum. */
364 skb
->priority
= sk
->priority
;
365 return skb
->dst
->output(skb
);
368 if (ip_dont_fragment(sk
, &rt
->u
.dst
)) {
369 /* Reject packet ONLY if TCP might fragment
370 * it itself, if were careful enough.
372 iph
->frag_off
|= __constant_htons(IP_DF
);
373 NETDEBUG(printk(KERN_DEBUG
"sending pkt_too_big to self\n"));
375 icmp_send(skb
, ICMP_DEST_UNREACH
, ICMP_FRAG_NEEDED
,
376 htonl(rt
->u
.dst
.pmtu
));
380 ip_select_ident(iph
, &rt
->u
.dst
);
381 return ip_fragment(skb
, skb
->dst
->output
);
384 int ip_queue_xmit(struct sk_buff
*skb
)
386 struct sock
*sk
= skb
->sk
;
387 struct ip_options
*opt
= sk
->protinfo
.af_inet
.opt
;
391 /* Make sure we can route this packet. */
392 rt
= (struct rtable
*)__sk_dst_check(sk
, 0);
396 /* Use correct destination address if we have options. */
401 /* If this fails, retransmit mechanism of transport layer will
402 * keep trying until route appears or the connection times itself
405 if (ip_route_output(&rt
, daddr
, sk
->saddr
,
406 RT_TOS(sk
->protinfo
.af_inet
.tos
) | RTO_CONN
| sk
->localroute
,
409 __sk_dst_set(sk
, &rt
->u
.dst
);
411 skb
->dst
= dst_clone(&rt
->u
.dst
);
413 if (opt
&& opt
->is_strictroute
&& rt
->rt_dst
!= rt
->rt_gateway
)
416 /* OK, we know where to send it, allocate and build IP header. */
417 iph
= (struct iphdr
*) skb_push(skb
, sizeof(struct iphdr
) + (opt
? opt
->optlen
: 0));
418 *((__u16
*)iph
) = htons((4 << 12) | (5 << 8) | (sk
->protinfo
.af_inet
.tos
& 0xff));
419 iph
->tot_len
= htons(skb
->len
);
421 iph
->ttl
= sk
->protinfo
.af_inet
.ttl
;
422 iph
->protocol
= sk
->protocol
;
423 iph
->saddr
= rt
->rt_src
;
424 iph
->daddr
= rt
->rt_dst
;
426 /* Transport layer set skb->h.foo itself. */
428 if(opt
&& opt
->optlen
) {
429 iph
->ihl
+= opt
->optlen
>> 2;
430 ip_options_build(skb
, opt
, sk
->daddr
, rt
, 0);
433 return NF_HOOK(PF_INET
, NF_IP_LOCAL_OUT
, skb
, NULL
, rt
->u
.dst
.dev
,
437 IP_INC_STATS(IpOutNoRoutes
);
439 return -EHOSTUNREACH
;
443 * Build and send a packet, with as little as one copy
445 * Doesn't care much about ip options... option length can be
446 * different for fragment at 0 and other fragments.
448 * Note that the fragment at the highest offset is sent first,
449 * so the getfrag routine can fill in the TCP/UDP checksum header
450 * field in the last fragment it sends... actually it also helps
451 * the reassemblers, they can put most packets in at the head of
452 * the fragment queue, and they know the total size in advance. This
453 * last feature will measurably improve the Linux fragment handler one
456 * The callback has five args, an arbitrary pointer (copy of frag),
457 * the source IP address (may depend on the routing table), the
458 * destination address (char *), the offset to copy from, and the
459 * length to be copied.
462 static int ip_build_xmit_slow(struct sock
*sk
,
463 int getfrag (const void *,
469 struct ipcm_cookie
*ipc
,
473 unsigned int fraglen
, maxfraglen
, fragheaderlen
;
479 int hh_len
= (rt
->u
.dst
.dev
->hard_header_len
+ 15)&~15;
481 struct ip_options
*opt
= ipc
->opt
;
484 mtu
= rt
->u
.dst
.pmtu
;
485 if (ip_dont_fragment(sk
, &rt
->u
.dst
))
488 length
-= sizeof(struct iphdr
);
491 fragheaderlen
= sizeof(struct iphdr
) + opt
->optlen
;
492 maxfraglen
= ((mtu
-sizeof(struct iphdr
)-opt
->optlen
) & ~7) + fragheaderlen
;
494 fragheaderlen
= sizeof(struct iphdr
);
497 * Fragheaderlen is the size of 'overhead' on each buffer. Now work
498 * out the size of the frames to send.
501 maxfraglen
= ((mtu
-sizeof(struct iphdr
)) & ~7) + fragheaderlen
;
504 if (length
+ fragheaderlen
> 0xFFFF) {
505 ip_local_error(sk
, EMSGSIZE
, rt
->rt_dst
, sk
->dport
, mtu
);
510 * Start at the end of the frame by handling the remainder.
513 offset
= length
- (length
% (maxfraglen
- fragheaderlen
));
516 * Amount of memory to allocate for final fragment.
519 fraglen
= length
- offset
+ fragheaderlen
;
521 if (length
-offset
==0) {
522 fraglen
= maxfraglen
;
523 offset
-= maxfraglen
-fragheaderlen
;
527 * The last fragment will not have MF (more fragments) set.
533 * Don't fragment packets for path mtu discovery.
536 if (offset
> 0 && sk
->protinfo
.af_inet
.pmtudisc
==IP_PMTUDISC_DO
) {
537 ip_local_error(sk
, EMSGSIZE
, rt
->rt_dst
, sk
->dport
, mtu
);
544 * Begin outputting the bytes.
549 struct sk_buff
* skb
;
552 * Get the memory we require with some space left for alignment.
555 skb
= sock_alloc_send_skb(sk
, fraglen
+hh_len
+15, 0, flags
&MSG_DONTWAIT
, &err
);
560 * Fill in the control structures
563 skb
->priority
= sk
->priority
;
564 skb
->dst
= dst_clone(&rt
->u
.dst
);
565 skb_reserve(skb
, hh_len
);
568 * Find where to start putting bytes.
571 data
= skb_put(skb
, fraglen
);
572 skb
->nh
.iph
= (struct iphdr
*)data
;
575 * Only write IP header onto non-raw packets
579 struct iphdr
*iph
= (struct iphdr
*)data
;
584 iph
->ihl
+= opt
->optlen
>>2;
585 ip_options_build(skb
, opt
,
586 ipc
->addr
, rt
, offset
);
588 iph
->tos
= sk
->protinfo
.af_inet
.tos
;
589 iph
->tot_len
= htons(fraglen
- fragheaderlen
+ iph
->ihl
*4);
590 iph
->frag_off
= htons(offset
>>3)|mf
|df
;
594 /* Select an unpredictable ident only
595 * for packets without DF or having
598 __ip_select_ident(iph
, &rt
->u
.dst
);
603 * Any further fragments will have MF set.
607 if (rt
->rt_type
== RTN_MULTICAST
)
608 iph
->ttl
= sk
->protinfo
.af_inet
.mc_ttl
;
610 iph
->ttl
= sk
->protinfo
.af_inet
.ttl
;
611 iph
->protocol
= sk
->protocol
;
613 iph
->saddr
= rt
->rt_src
;
614 iph
->daddr
= rt
->rt_dst
;
615 iph
->check
= ip_fast_csum((unsigned char *)iph
, iph
->ihl
);
623 if (getfrag(frag
, data
, offset
, fraglen
-fragheaderlen
)) {
629 offset
-= (maxfraglen
-fragheaderlen
);
630 fraglen
= maxfraglen
;
634 err
= NF_HOOK(PF_INET
, NF_IP_LOCAL_OUT
, skb
, NULL
,
635 skb
->dst
->dev
, output_maybe_reroute
);
638 err
= sk
->protinfo
.af_inet
.recverr
? net_xmit_errno(err
) : 0;
642 } while (offset
>= 0);
645 ip_statistics
[smp_processor_id()*2 + !in_softirq()].IpFragCreates
+= nfrags
;
650 IP_INC_STATS(IpOutDiscards
);
652 ip_statistics
[smp_processor_id()*2 + !in_softirq()].IpFragCreates
+= nfrags
;
657 * Fast path for unfragmented packets.
659 int ip_build_xmit(struct sock
*sk
,
660 int getfrag (const void *,
666 struct ipcm_cookie
*ipc
,
676 * Try the simple case first. This leaves fragmented frames, and by
677 * choice RAW frames within 20 bytes of maximum size(rare) to the long path
680 if (!sk
->protinfo
.af_inet
.hdrincl
) {
681 length
+= sizeof(struct iphdr
);
684 * Check for slow path.
686 if (length
> rt
->u
.dst
.pmtu
|| ipc
->opt
!= NULL
)
687 return ip_build_xmit_slow(sk
,getfrag
,frag
,length
,ipc
,rt
,flags
);
689 if (length
> rt
->u
.dst
.dev
->mtu
) {
690 ip_local_error(sk
, EMSGSIZE
, rt
->rt_dst
, sk
->dport
, rt
->u
.dst
.dev
->mtu
);
698 * Do path mtu discovery if needed.
701 if (ip_dont_fragment(sk
, &rt
->u
.dst
))
705 * Fast path for unfragmented frames without options.
708 int hh_len
= (rt
->u
.dst
.dev
->hard_header_len
+ 15)&~15;
710 skb
= sock_alloc_send_skb(sk
, length
+hh_len
+15,
711 0, flags
&MSG_DONTWAIT
, &err
);
714 skb_reserve(skb
, hh_len
);
717 skb
->priority
= sk
->priority
;
718 skb
->dst
= dst_clone(&rt
->u
.dst
);
720 skb
->nh
.iph
= iph
= (struct iphdr
*)skb_put(skb
, length
);
722 if(!sk
->protinfo
.af_inet
.hdrincl
) {
725 iph
->tos
=sk
->protinfo
.af_inet
.tos
;
726 iph
->tot_len
= htons(length
);
728 iph
->ttl
=sk
->protinfo
.af_inet
.mc_ttl
;
729 ip_select_ident(iph
, &rt
->u
.dst
);
730 if (rt
->rt_type
!= RTN_MULTICAST
)
731 iph
->ttl
=sk
->protinfo
.af_inet
.ttl
;
732 iph
->protocol
=sk
->protocol
;
733 iph
->saddr
=rt
->rt_src
;
734 iph
->daddr
=rt
->rt_dst
;
736 iph
->check
= ip_fast_csum((unsigned char *)iph
, iph
->ihl
);
737 err
= getfrag(frag
, ((char *)iph
)+iph
->ihl
*4,0, length
-iph
->ihl
*4);
740 err
= getfrag(frag
, (void *)iph
, 0, length
);
745 err
= NF_HOOK(PF_INET
, NF_IP_LOCAL_OUT
, skb
, NULL
, rt
->u
.dst
.dev
,
746 output_maybe_reroute
);
748 err
= sk
->protinfo
.af_inet
.recverr
? net_xmit_errno(err
) : 0;
758 IP_INC_STATS(IpOutDiscards
);
763 * This IP datagram is too large to be sent in one piece. Break it up into
764 * smaller pieces (each of size equal to IP header plus
765 * a block of the data of the original IP data part) that will yet fit in a
766 * single device frame, and queue such a frame for sending.
768 * Yes this is inefficient, feel free to submit a quicker one.
771 int ip_fragment(struct sk_buff
*skb
, int (*output
)(struct sk_buff
*))
776 struct net_device
*dev
;
777 struct sk_buff
*skb2
;
778 unsigned int mtu
, hlen
, left
, len
;
781 struct rtable
*rt
= (struct rtable
*)skb
->dst
;
787 * Point into the IP datagram header.
791 iph
= (struct iphdr
*)raw
;
794 * Setup starting values.
798 left
= ntohs(iph
->tot_len
) - hlen
; /* Space per frame */
799 mtu
= rt
->u
.dst
.pmtu
- hlen
; /* Size of data space */
800 ptr
= raw
+ hlen
; /* Where to start from */
803 * Fragment the datagram.
806 offset
= (ntohs(iph
->frag_off
) & IP_OFFSET
) << 3;
807 not_last_frag
= iph
->frag_off
& htons(IP_MF
);
810 * Keep copying data until we run out.
815 /* IF: it doesn't fit, use 'mtu' - the data space left */
818 /* IF: we are not sending upto and including the packet end
819 then align the next start on an eight byte boundary */
827 if ((skb2
= alloc_skb(len
+hlen
+dev
->hard_header_len
+15,GFP_ATOMIC
)) == NULL
) {
828 NETDEBUG(printk(KERN_INFO
"IP: frag: no memory for new fragment!\n"));
834 * Set up data on packet
837 skb2
->pkt_type
= skb
->pkt_type
;
838 skb2
->priority
= skb
->priority
;
839 skb_reserve(skb2
, (dev
->hard_header_len
+15)&~15);
840 skb_put(skb2
, len
+ hlen
);
841 skb2
->nh
.raw
= skb2
->data
;
842 skb2
->h
.raw
= skb2
->data
+ hlen
;
845 * Charge the memory for the fragment to any owner
850 skb_set_owner_w(skb2
, skb
->sk
);
851 skb2
->dst
= dst_clone(skb
->dst
);
852 skb2
->dev
= skb
->dev
;
855 * Copy the packet header into the new buffer.
858 memcpy(skb2
->nh
.raw
, raw
, hlen
);
861 * Copy a block of the IP datagram.
863 memcpy(skb2
->h
.raw
, ptr
, len
);
867 * Fill in the new header fields.
870 iph
->frag_off
= htons((offset
>> 3));
872 /* ANK: dirty, but effective trick. Upgrade options only if
873 * the segment to be fragmented was THE FIRST (otherwise,
874 * options are already fixed) and make it ONCE
875 * on the initial skb, so that all the following fragments
876 * will inherit fixed options.
879 ip_options_fragment(skb
);
882 * Added AC : If we are fragmenting a fragment that's not the
883 * last fragment then keep MF on each bit
885 if (left
> 0 || not_last_frag
)
886 iph
->frag_off
|= htons(IP_MF
);
890 #ifdef CONFIG_NETFILTER
891 /* Connection association is same as pre-frag packet */
892 skb2
->nfct
= skb
->nfct
;
893 nf_conntrack_get(skb2
->nfct
);
894 #ifdef CONFIG_NETFILTER_DEBUG
895 skb2
->nf_debug
= skb
->nf_debug
;
900 * Put this fragment into the sending queue.
903 IP_INC_STATS(IpFragCreates
);
905 iph
->tot_len
= htons(len
+ hlen
);
914 IP_INC_STATS(IpFragOKs
);
919 IP_INC_STATS(IpFragFails
);
924 * Fetch data from kernel space and fill in checksum if needed.
926 static int ip_reply_glue_bits(const void *dptr
, char *to
, unsigned int offset
,
927 unsigned int fraglen
)
929 struct ip_reply_arg
*dp
= (struct ip_reply_arg
*)dptr
;
930 u16
*pktp
= (u16
*)to
;
936 if (offset
>= iov
->iov_len
) {
937 offset
-= iov
->iov_len
;
941 len
= iov
->iov_len
- offset
;
942 if (fraglen
> len
) { /* overlapping. */
943 dp
->csum
= csum_partial_copy_nocheck(iov
->iov_base
+offset
, to
, len
,
951 dp
->csum
= csum_partial_copy_nocheck(iov
->iov_base
+offset
, to
, fraglen
,
954 if (hdrflag
&& dp
->csumoffset
)
955 *(pktp
+ dp
->csumoffset
) = csum_fold(dp
->csum
); /* fill in checksum */
960 * Generic function to send a packet as reply to another packet.
961 * Used to send TCP resets so far. ICMP should use this function too.
963 * Should run single threaded per socket because it uses the sock
964 * structure to pass arguments.
966 void ip_send_reply(struct sock
*sk
, struct sk_buff
*skb
, struct ip_reply_arg
*arg
,
970 struct ip_options opt
;
973 struct ipcm_cookie ipc
;
975 struct rtable
*rt
= (struct rtable
*)skb
->dst
;
977 if (ip_options_echo(&replyopts
.opt
, skb
))
980 daddr
= ipc
.addr
= rt
->rt_src
;
983 if (replyopts
.opt
.optlen
) {
984 ipc
.opt
= &replyopts
.opt
;
987 daddr
= replyopts
.opt
.faddr
;
990 if (ip_route_output(&rt
, daddr
, rt
->rt_spec_dst
, RT_TOS(skb
->nh
.iph
->tos
), 0))
993 /* And let IP do all the hard work.
995 This chunk is not reenterable, hence spinlock.
996 Note that it uses the fact, that this function is called
997 with locally disabled BH and that sk cannot be already spinlocked.
1000 sk
->protinfo
.af_inet
.tos
= skb
->nh
.iph
->tos
;
1001 sk
->priority
= skb
->priority
;
1002 sk
->protocol
= skb
->nh
.iph
->protocol
;
1003 ip_build_xmit(sk
, ip_reply_glue_bits
, arg
, len
, &ipc
, rt
, MSG_DONTWAIT
);
1010 * IP protocol layer initialiser
1013 static struct packet_type ip_packet_type
=
1015 __constant_htons(ETH_P_IP
),
1016 NULL
, /* All devices */
1023 * IP registers the packet type and then calls the subprotocol initialisers
1026 void __init
ip_init(void)
1028 dev_add_pack(&ip_packet_type
);
1033 #ifdef CONFIG_IP_MULTICAST
1034 proc_net_create("igmp", 0, ip_mc_procinfo
);