]>
git.saurik.com Git - apple/network_cmds.git/blob - natd.tproj/icmp.c
e05f2553643c9d927e1295a742eec563015f300a
2 * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * natd - Network Address Translation Daemon for FreeBSD.
28 * This software is provided free of charge, with no
29 * warranty of any kind, either expressed or implied.
30 * Use at your own risk.
32 * You may copy, modify and distribute this software (icmp.c) freely.
34 * Ari Suutari <suutari@iki.fi>
37 * $FreeBSD: src/sbin/natd/icmp.c,v 1.6 1999/08/28 00:13:45 peter Exp $
46 #include <sys/types.h>
47 #include <sys/socket.h>
54 #include <netinet/in.h>
55 #include <netinet/in_systm.h>
56 #include <netinet/ip.h>
57 #include <netinet/ip_icmp.h>
63 int SendNeedFragIcmp (int sock
, struct ip
* failedDgram
, int mtu
)
65 char icmpBuf
[IP_MAXPACKET
];
71 struct sockaddr_in addr
;
75 * Don't send error if packet is
76 * not the first fragment.
78 if (ntohs (failedDgram
->ip_off
) & ~(IP_MF
| IP_DF
))
81 * Dont respond if failed datagram is ICMP.
83 if (failedDgram
->ip_p
== IPPROTO_ICMP
)
86 * Start building the message.
88 ip
= (struct ip
*) icmpBuf
;
89 icmp
= (struct icmp
*) (icmpBuf
+ sizeof (struct ip
));
93 icmp
->icmp_type
= ICMP_UNREACH
;
94 icmp
->icmp_code
= ICMP_UNREACH_NEEDFRAG
;
97 icmp
->icmp_nextmtu
= htons (mtu
);
99 * Copy header + 64 bits of original datagram.
101 failHdrLen
= (failedDgram
->ip_hl
<< 2);
102 failBytes
= failedDgram
->ip_len
- failHdrLen
;
106 failBytes
+= failHdrLen
;
107 icmpLen
= ICMP_MINLEN
+ failBytes
;
109 memcpy (&icmp
->icmp_ip
, failedDgram
, failBytes
);
111 * Calculate checksum.
113 icmp
->icmp_cksum
= PacketAliasInternetChecksum ((u_short
*) icmp
,
116 * Add IP header using old IP header as template.
118 memcpy (ip
, failedDgram
, sizeof (struct ip
));
122 ip
->ip_len
= htons (sizeof (struct ip
) + icmpLen
);
123 ip
->ip_p
= IPPROTO_ICMP
;
127 ip
->ip_dst
= ip
->ip_src
;
130 PacketAliasIn ((char*) ip
, IP_MAXPACKET
);
132 addr
.sin_family
= AF_INET
;
133 addr
.sin_addr
= ip
->ip_dst
;
136 * Put packet into processing queue.
138 wrote
= sendto (sock
,
142 (struct sockaddr
*) &addr
,
145 if (wrote
!= icmpLen
)
146 Warn ("Cannot send ICMP message.");