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>
33 #include <machine/in_cksum.h>
39 int SendNeedFragIcmp (int sock, struct ip* failedDgram, int mtu)
41 char icmpBuf[IP_MAXPACKET];
47 struct sockaddr_in addr;
51 * Don't send error if packet is
52 * not the first fragment.
54 if (ntohs (failedDgram->ip_off) & ~(IP_MF | IP_DF))
57 * Dont respond if failed datagram is ICMP.
59 if (failedDgram->ip_p == IPPROTO_ICMP)
62 * Start building the message.
64 ip = (struct ip*) icmpBuf;
65 icmp = (struct icmp*) (icmpBuf + sizeof (struct ip));
69 icmp->icmp_type = ICMP_UNREACH;
70 icmp->icmp_code = ICMP_UNREACH_NEEDFRAG;
73 icmp->icmp_nextmtu = htons (mtu);
75 * Copy header + 64 bits of original datagram.
77 failHdrLen = (failedDgram->ip_hl << 2);
78 failBytes = failedDgram->ip_len - failHdrLen;
82 failBytes += failHdrLen;
83 icmpLen = ICMP_MINLEN + failBytes;
85 memcpy (&icmp->icmp_ip, failedDgram, failBytes);
89 icmp->icmp_cksum = LibAliasInternetChecksum (mla, (u_short*) icmp,
92 * Add IP header using old IP header as template.
94 memcpy (ip, failedDgram, sizeof (struct ip));
98 ip->ip_len = htons (sizeof (struct ip) + icmpLen);
99 ip->ip_p = IPPROTO_ICMP;
103 ip->ip_dst = ip->ip_src;
106 LibAliasIn (mla, (char*) ip, IP_MAXPACKET);
108 addr.sin_family = AF_INET;
109 addr.sin_addr = ip->ip_dst;
112 * Put packet into processing queue.
114 wrote = sendto (sock,
118 (struct sockaddr*) &addr,
121 if (wrote != icmpLen)
122 Warn ("Cannot send ICMP message.");