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>
21 #include <sys/types.h>
22 #include <sys/socket.h>
29 #include <netinet/in.h>
30 #include <netinet/in_systm.h>
31 #include <netinet/ip.h>
32 #include <netinet/ip_icmp.h>
38 int SendNeedFragIcmp (int sock, struct ip* failedDgram, int mtu)
40 char icmpBuf[IP_MAXPACKET];
46 struct sockaddr_in addr;
50 * Don't send error if packet is
51 * not the first fragment.
53 if (ntohs (failedDgram->ip_off) & ~(IP_MF | IP_DF))
56 * Dont respond if failed datagram is ICMP.
58 if (failedDgram->ip_p == IPPROTO_ICMP)
61 * Start building the message.
63 ip = (struct ip*) icmpBuf;
64 icmp = (struct icmp*) (icmpBuf + sizeof (struct ip));
68 icmp->icmp_type = ICMP_UNREACH;
69 icmp->icmp_code = ICMP_UNREACH_NEEDFRAG;
72 icmp->icmp_nextmtu = htons (mtu);
74 * Copy header + 64 bits of original datagram.
76 failHdrLen = (failedDgram->ip_hl << 2);
77 failBytes = failedDgram->ip_len - failHdrLen;
81 failBytes += failHdrLen;
82 icmpLen = ICMP_MINLEN + failBytes;
84 memcpy (&icmp->icmp_ip, failedDgram, failBytes);
88 icmp->icmp_cksum = LibAliasInternetChecksum (mla, (u_short*) icmp,
91 * Add IP header using old IP header as template.
93 memcpy (ip, failedDgram, sizeof (struct ip));
97 ip->ip_len = htons (sizeof (struct ip) + icmpLen);
98 ip->ip_p = IPPROTO_ICMP;
102 ip->ip_dst = ip->ip_src;
105 LibAliasIn (mla, (char*) ip, IP_MAXPACKET);
107 addr.sin_family = AF_INET;
108 addr.sin_addr = ip->ip_dst;
111 * Put packet into processing queue.
113 wrote = sendto (sock,
117 (struct sockaddr*) &addr,
120 if (wrote != icmpLen)
121 Warn ("Cannot send ICMP message.");