2005-04-25 17:31:50 +00:00
|
|
|
|
1997-02-09 22:50:16 +00:00
|
|
|
/*
|
1999-11-08 20:51:23 +00:00
|
|
|
* resend.c (C) 1995-1998 Darren Reed
|
1997-02-09 22:50:16 +00:00
|
|
|
*
|
2001-07-28 11:59:33 +00:00
|
|
|
* See the IPFILTER.LICENCE file for details on licencing.
|
2005-04-25 17:31:50 +00:00
|
|
|
*
|
1997-02-09 22:50:16 +00:00
|
|
|
*/
|
2005-04-25 17:31:50 +00:00
|
|
|
#if !defined(lint)
|
|
|
|
static const char sccsid[] = "@(#)resend.c 1.3 1/11/96 (C)1995 Darren Reed";
|
2013-08-11 14:28:45 +00:00
|
|
|
static const char rcsid[] = "@(#)$Id$";
|
2002-03-19 11:45:20 +00:00
|
|
|
#endif
|
2005-04-25 17:31:50 +00:00
|
|
|
#include <sys/param.h>
|
1997-02-09 22:50:16 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
#include <netinet/ip.h>
|
1997-11-16 04:52:19 +00:00
|
|
|
# include <netinet/ip_var.h>
|
|
|
|
# include <netinet/if_ether.h>
|
2005-04-25 17:31:50 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
1997-04-03 10:22:02 +00:00
|
|
|
#include "ipsend.h"
|
1997-02-09 22:50:16 +00:00
|
|
|
|
1997-11-16 04:52:19 +00:00
|
|
|
extern int opts;
|
1997-02-09 22:50:16 +00:00
|
|
|
|
2021-01-26 06:24:28 +00:00
|
|
|
void dumppacket(ip_t *);
|
1997-02-09 22:50:16 +00:00
|
|
|
|
|
|
|
|
2021-12-20 17:07:20 +00:00
|
|
|
void
|
|
|
|
dumppacket(ip_t *ip)
|
1997-02-09 22:50:16 +00:00
|
|
|
{
|
|
|
|
tcphdr_t *t;
|
|
|
|
int i, j;
|
|
|
|
|
2005-04-25 17:31:50 +00:00
|
|
|
t = (tcphdr_t *)((char *)ip + (IP_HL(ip) << 2));
|
1997-02-09 22:50:16 +00:00
|
|
|
if (ip->ip_tos)
|
|
|
|
printf("tos %#x ", ip->ip_tos);
|
|
|
|
if (ip->ip_off & 0x3fff)
|
|
|
|
printf("frag @%#x ", (ip->ip_off & 0x1fff) << 3);
|
|
|
|
printf("len %d id %d ", ip->ip_len, ip->ip_id);
|
|
|
|
printf("ttl %d p %d src %s", ip->ip_ttl, ip->ip_p,
|
|
|
|
inet_ntoa(ip->ip_src));
|
|
|
|
if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_UDP)
|
|
|
|
printf(",%d", t->th_sport);
|
|
|
|
printf(" dst %s", inet_ntoa(ip->ip_dst));
|
|
|
|
if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_UDP)
|
|
|
|
printf(",%d", t->th_dport);
|
|
|
|
if (ip->ip_p == IPPROTO_TCP) {
|
|
|
|
printf(" seq %lu:%lu flags ",
|
|
|
|
(u_long)t->th_seq, (u_long)t->th_ack);
|
|
|
|
for (j = 0, i = 1; i < 256; i *= 2, j++)
|
|
|
|
if (t->th_flags & i)
|
|
|
|
printf("%c", "FSRPAU--"[j]);
|
|
|
|
}
|
|
|
|
putchar('\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-12-20 17:07:20 +00:00
|
|
|
int
|
|
|
|
ip_resend(char *dev, int mtu, struct ipread *r, struct in_addr gwip,
|
|
|
|
char *datain)
|
1997-02-09 22:50:16 +00:00
|
|
|
{
|
|
|
|
ether_header_t *eh;
|
|
|
|
char dhost[6];
|
|
|
|
ip_t *ip;
|
2005-04-25 17:31:50 +00:00
|
|
|
int fd, wfd = initdevice(dev, 5), len, i;
|
2013-08-11 14:28:45 +00:00
|
|
|
mb_t mb;
|
1997-02-09 22:50:16 +00:00
|
|
|
|
2006-08-16 12:23:02 +00:00
|
|
|
if (wfd == -1)
|
2022-01-04 02:49:18 +00:00
|
|
|
return (-1);
|
2006-08-16 12:23:02 +00:00
|
|
|
|
1997-02-09 22:50:16 +00:00
|
|
|
if (datain)
|
|
|
|
fd = (*r->r_open)(datain);
|
|
|
|
else
|
|
|
|
fd = (*r->r_open)("-");
|
2005-04-25 17:31:50 +00:00
|
|
|
|
1997-02-09 22:50:16 +00:00
|
|
|
if (fd < 0)
|
|
|
|
exit(-1);
|
|
|
|
|
2013-08-11 14:28:45 +00:00
|
|
|
ip = (struct ip *)mb.mb_buf;
|
1997-02-09 22:50:16 +00:00
|
|
|
eh = (ether_header_t *)malloc(sizeof(*eh));
|
2001-02-04 14:25:38 +00:00
|
|
|
if(!eh)
|
|
|
|
{
|
|
|
|
perror("malloc failed");
|
2022-01-04 02:49:18 +00:00
|
|
|
return (-2);
|
2001-02-04 14:25:38 +00:00
|
|
|
}
|
1997-02-09 22:50:16 +00:00
|
|
|
|
2018-12-06 20:15:54 +00:00
|
|
|
bzero((char *) &eh->ether_shost, sizeof(eh->ether_shost));
|
1997-02-09 22:50:16 +00:00
|
|
|
if (gwip.s_addr && (arp((char *)&gwip, dhost) == -1))
|
|
|
|
{
|
|
|
|
perror("arp");
|
2006-08-16 12:23:02 +00:00
|
|
|
free(eh);
|
2022-01-04 02:49:18 +00:00
|
|
|
return (-2);
|
1997-02-09 22:50:16 +00:00
|
|
|
}
|
|
|
|
|
2013-08-11 14:28:45 +00:00
|
|
|
while ((i = (*r->r_readip)(&mb, NULL, NULL)) > 0)
|
1997-02-09 22:50:16 +00:00
|
|
|
{
|
1997-11-16 04:52:19 +00:00
|
|
|
if (!(opts & OPT_RAW)) {
|
|
|
|
len = ntohs(ip->ip_len);
|
|
|
|
eh = (ether_header_t *)realloc((char *)eh, sizeof(*eh) + len);
|
|
|
|
eh->ether_type = htons((u_short)ETHERTYPE_IP);
|
|
|
|
if (!gwip.s_addr) {
|
|
|
|
if (arp((char *)&gwip,
|
2018-12-06 20:15:54 +00:00
|
|
|
(char *) &eh->ether_dhost) == -1) {
|
1997-11-16 04:52:19 +00:00
|
|
|
perror("arp");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else
|
2018-12-06 20:15:54 +00:00
|
|
|
bcopy(dhost, (char *) &eh->ether_dhost,
|
1997-11-16 04:52:19 +00:00
|
|
|
sizeof(dhost));
|
|
|
|
if (!ip->ip_sum)
|
|
|
|
ip->ip_sum = chksum((u_short *)ip,
|
2005-04-25 17:31:50 +00:00
|
|
|
IP_HL(ip) << 2);
|
1997-11-16 04:52:19 +00:00
|
|
|
bcopy(ip, (char *)(eh + 1), len);
|
|
|
|
len += sizeof(*eh);
|
2013-08-11 14:28:45 +00:00
|
|
|
dumppacket(ip);
|
1997-11-16 04:52:19 +00:00
|
|
|
} else {
|
2013-08-11 14:28:45 +00:00
|
|
|
eh = (ether_header_t *)mb.mb_buf;
|
1997-11-16 04:52:19 +00:00
|
|
|
len = i;
|
|
|
|
}
|
1997-02-09 22:50:16 +00:00
|
|
|
|
1997-11-16 04:52:19 +00:00
|
|
|
if (sendip(wfd, (char *)eh, len) == -1)
|
1997-02-09 22:50:16 +00:00
|
|
|
{
|
|
|
|
perror("send_packet");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(*r->r_close)();
|
2006-08-16 12:23:02 +00:00
|
|
|
free(eh);
|
2022-01-04 02:49:18 +00:00
|
|
|
return (0);
|
1997-02-09 22:50:16 +00:00
|
|
|
}
|