2 * natd - Network Address Translation Daemon for FreeBSD.
4 * This software is provided free of charge, with no
5 * warranty of any kind, either expressed or implied.
6 * Use at your own risk.
8 * You may copy, modify and distribute this software (icmp.c) freely.
10 * Ari Suutari <suutari@iki.fi>
12 * $FreeBSD: src/sbin/natd/icmp.c,v 1.6 1999/08/28 00:13:45 peter Exp $
13 * $DragonFly: src/sbin/natd/icmp.c,v 1.2 2003/06/17 04:27:34 dillon Exp $
22 #include <sys/types.h>
23 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <netinet/in_systm.h>
32 #include <netinet/ip.h>
33 #include <netinet/ip_icmp.h>
34 #include <machine/in_cksum.h>
40 int SendNeedFragIcmp (int sock, struct ip* failedDgram, int mtu)
42 char icmpBuf[IP_MAXPACKET];
48 struct sockaddr_in addr;
52 * Don't send error if packet is
53 * not the first fragment.
55 if (ntohs (failedDgram->ip_off) & ~(IP_MF | IP_DF))
58 * Dont respond if failed datagram is ICMP.
60 if (failedDgram->ip_p == IPPROTO_ICMP)
63 * Start building the message.
65 ip = (struct ip*) icmpBuf;
66 icmp = (struct icmp*) (icmpBuf + sizeof (struct ip));
70 icmp->icmp_type = ICMP_UNREACH;
71 icmp->icmp_code = ICMP_UNREACH_NEEDFRAG;
74 icmp->icmp_nextmtu = htons (mtu);
76 * Copy header + 64 bits of original datagram.
78 failHdrLen = (failedDgram->ip_hl << 2);
79 failBytes = failedDgram->ip_len - failHdrLen;
83 failBytes += failHdrLen;
84 icmpLen = ICMP_MINLEN + failBytes;
86 memcpy (&icmp->icmp_ip, failedDgram, failBytes);
90 icmp->icmp_cksum = PacketAliasInternetChecksum ((u_short*) icmp,
93 * Add IP header using old IP header as template.
95 memcpy (ip, failedDgram, sizeof (struct ip));
99 ip->ip_len = htons (sizeof (struct ip) + icmpLen);
100 ip->ip_p = IPPROTO_ICMP;
104 ip->ip_dst = ip->ip_src;
107 PacketAliasIn ((char*) ip, IP_MAXPACKET);
109 addr.sin_family = AF_INET;
110 addr.sin_addr = ip->ip_dst;
113 * Put packet into processing queue.
115 wrote = sendto (sock,
119 (struct sockaddr*) &addr,
122 if (wrote != icmpLen)
123 Warn ("Cannot send ICMP message.");