1997-12-27 19:31:11 +00:00
|
|
|
/*
|
1999-03-07 18:23:56 +00:00
|
|
|
* natd - Network Address Translation Daemon for FreeBSD.
|
|
|
|
*
|
1997-12-30 00:38:56 +00:00
|
|
|
* This software is provided free of charge, with no
|
|
|
|
* warranty of any kind, either expressed or implied.
|
|
|
|
* Use at your own risk.
|
|
|
|
*
|
1999-03-07 18:23:56 +00:00
|
|
|
* You may copy, modify and distribute this software (icmp.c) freely.
|
1997-12-30 00:38:56 +00:00
|
|
|
*
|
|
|
|
* Ari Suutari <suutari@iki.fi>
|
|
|
|
*
|
1999-08-28 00:22:10 +00:00
|
|
|
* $FreeBSD$
|
1997-12-27 19:31:11 +00:00
|
|
|
*/
|
|
|
|
|
1997-06-22 04:19:08 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <signal.h>
|
|
|
|
|
|
|
|
#include <netdb.h>
|
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
#include <netinet/ip.h>
|
|
|
|
#include <netinet/ip_icmp.h>
|
|
|
|
#include <machine/in_cksum.h>
|
|
|
|
|
|
|
|
#include <alias.h>
|
|
|
|
|
|
|
|
#include "natd.h"
|
|
|
|
|
|
|
|
int SendNeedFragIcmp (int sock, struct ip* failedDgram, int mtu)
|
|
|
|
{
|
|
|
|
char icmpBuf[IP_MAXPACKET];
|
|
|
|
struct ip* ip;
|
|
|
|
struct icmp* icmp;
|
|
|
|
int icmpLen;
|
|
|
|
int failBytes;
|
|
|
|
int failHdrLen;
|
|
|
|
struct sockaddr_in addr;
|
|
|
|
int wrote;
|
|
|
|
struct in_addr swap;
|
|
|
|
/*
|
|
|
|
* Don't send error if packet is
|
|
|
|
* not the first fragment.
|
|
|
|
*/
|
|
|
|
if (ntohs (failedDgram->ip_off) & ~(IP_MF | IP_DF))
|
|
|
|
return 0;
|
|
|
|
/*
|
|
|
|
* Dont respond if failed datagram is ICMP.
|
|
|
|
*/
|
|
|
|
if (failedDgram->ip_p == IPPROTO_ICMP)
|
|
|
|
return 0;
|
|
|
|
/*
|
|
|
|
* Start building the message.
|
|
|
|
*/
|
|
|
|
ip = (struct ip*) icmpBuf;
|
|
|
|
icmp = (struct icmp*) (icmpBuf + sizeof (struct ip));
|
|
|
|
/*
|
|
|
|
* Complete ICMP part.
|
|
|
|
*/
|
|
|
|
icmp->icmp_type = ICMP_UNREACH;
|
|
|
|
icmp->icmp_code = ICMP_UNREACH_NEEDFRAG;
|
|
|
|
icmp->icmp_cksum = 0;
|
|
|
|
icmp->icmp_void = 0;
|
|
|
|
icmp->icmp_nextmtu = htons (mtu);
|
|
|
|
/*
|
|
|
|
* Copy header + 64 bits of original datagram.
|
|
|
|
*/
|
|
|
|
failHdrLen = (failedDgram->ip_hl << 2);
|
|
|
|
failBytes = failedDgram->ip_len - failHdrLen;
|
|
|
|
if (failBytes > 8)
|
|
|
|
failBytes = 8;
|
|
|
|
|
|
|
|
failBytes += failHdrLen;
|
|
|
|
icmpLen = ICMP_MINLEN + failBytes;
|
|
|
|
|
|
|
|
memcpy (&icmp->icmp_ip, failedDgram, failBytes);
|
|
|
|
/*
|
|
|
|
* Calculate checksum.
|
|
|
|
*/
|
1997-08-10 21:55:52 +00:00
|
|
|
icmp->icmp_cksum = PacketAliasInternetChecksum ((u_short*) icmp,
|
|
|
|
icmpLen);
|
1997-06-22 04:19:08 +00:00
|
|
|
/*
|
|
|
|
* Add IP header using old IP header as template.
|
|
|
|
*/
|
|
|
|
memcpy (ip, failedDgram, sizeof (struct ip));
|
|
|
|
|
|
|
|
ip->ip_v = 4;
|
|
|
|
ip->ip_hl = 5;
|
|
|
|
ip->ip_len = htons (sizeof (struct ip) + icmpLen);
|
|
|
|
ip->ip_p = IPPROTO_ICMP;
|
|
|
|
ip->ip_tos = 0;
|
|
|
|
|
|
|
|
swap = ip->ip_dst;
|
|
|
|
ip->ip_dst = ip->ip_src;
|
|
|
|
ip->ip_src = swap;
|
|
|
|
|
|
|
|
PacketAliasIn ((char*) ip, IP_MAXPACKET);
|
|
|
|
|
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
addr.sin_addr = ip->ip_dst;
|
|
|
|
addr.sin_port = 0;
|
|
|
|
/*
|
|
|
|
* Put packet into processing queue.
|
|
|
|
*/
|
|
|
|
wrote = sendto (sock,
|
|
|
|
icmp,
|
|
|
|
icmpLen,
|
|
|
|
0,
|
|
|
|
(struct sockaddr*) &addr,
|
|
|
|
sizeof addr);
|
|
|
|
|
|
|
|
if (wrote != icmpLen)
|
|
|
|
Warn ("Cannot send ICMP message.");
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|