xref: /NextBSD/contrib/ipfilter/ipsend/resend.c (revision e1dd16d965b177f109afb771e59432e36f335d0a)
1 /*	$FreeBSD$	*/
2 
3 /*
4  * resend.c (C) 1995-1998 Darren Reed
5  *
6  * See the IPFILTER.LICENCE file for details on licencing.
7  *
8  */
9 #if !defined(lint)
10 static const char sccsid[] = "@(#)resend.c	1.3 1/11/96 (C)1995 Darren Reed";
11 static const char rcsid[] = "@(#)$Id$";
12 #endif
13 #include <sys/param.h>
14 #include <sys/types.h>
15 #include <sys/time.h>
16 #include <sys/socket.h>
17 #include <net/if.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
20 #include <netinet/in_systm.h>
21 #include <netinet/ip.h>
22 #ifndef	linux
23 # include <netinet/ip_var.h>
24 # include <netinet/if_ether.h>
25 #endif
26 #include <stdio.h>
27 #include <netdb.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include "ipsend.h"
32 
33 extern	int	opts;
34 
35 void	dumppacket __P((ip_t *));
36 
37 
dumppacket(ip)38 void dumppacket(ip)
39 	ip_t	*ip;
40 {
41 	tcphdr_t *t;
42 	int i, j;
43 
44 	t = (tcphdr_t *)((char *)ip + (IP_HL(ip) << 2));
45 	if (ip->ip_tos)
46 		printf("tos %#x ", ip->ip_tos);
47 	if (ip->ip_off & 0x3fff)
48 		printf("frag @%#x ", (ip->ip_off & 0x1fff) << 3);
49 	printf("len %d id %d ", ip->ip_len, ip->ip_id);
50 	printf("ttl %d p %d src %s", ip->ip_ttl, ip->ip_p,
51 		inet_ntoa(ip->ip_src));
52 	if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_UDP)
53 		printf(",%d", t->th_sport);
54 	printf(" dst %s", inet_ntoa(ip->ip_dst));
55 	if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_UDP)
56 		printf(",%d", t->th_dport);
57 	if (ip->ip_p == IPPROTO_TCP) {
58 		printf(" seq %lu:%lu flags ",
59 			(u_long)t->th_seq, (u_long)t->th_ack);
60 		for (j = 0, i = 1; i < 256; i *= 2, j++)
61 			if (t->th_flags & i)
62 				printf("%c", "FSRPAU--"[j]);
63 	}
64 	putchar('\n');
65 }
66 
67 
ip_resend(dev,mtu,r,gwip,datain)68 int	ip_resend(dev, mtu, r, gwip, datain)
69 	char	*dev;
70 	int	mtu;
71 	struct	in_addr	gwip;
72 	struct	ipread	*r;
73 	char	*datain;
74 {
75 	ether_header_t	*eh;
76 	char	dhost[6];
77 	ip_t	*ip;
78 	int	fd, wfd = initdevice(dev, 5), len, i;
79 	mb_t	mb;
80 
81 	if (wfd == -1)
82 		return -1;
83 
84 	if (datain)
85 		fd = (*r->r_open)(datain);
86 	else
87 		fd = (*r->r_open)("-");
88 
89 	if (fd < 0)
90 		exit(-1);
91 
92 	ip = (struct ip *)mb.mb_buf;
93 	eh = (ether_header_t *)malloc(sizeof(*eh));
94 	if(!eh)
95 	    {
96 		perror("malloc failed");
97 		return -2;
98 	    }
99 
100 	bzero((char *)A_A eh->ether_shost, sizeof(eh->ether_shost));
101 	if (gwip.s_addr && (arp((char *)&gwip, dhost) == -1))
102 	    {
103 		perror("arp");
104 		free(eh);
105 		return -2;
106 	    }
107 
108 	while ((i = (*r->r_readip)(&mb, NULL, NULL)) > 0)
109 	    {
110 		if (!(opts & OPT_RAW)) {
111 			len = ntohs(ip->ip_len);
112 			eh = (ether_header_t *)realloc((char *)eh, sizeof(*eh) + len);
113 			eh->ether_type = htons((u_short)ETHERTYPE_IP);
114 			if (!gwip.s_addr) {
115 				if (arp((char *)&gwip,
116 					(char *)A_A eh->ether_dhost) == -1) {
117 					perror("arp");
118 					continue;
119 				}
120 			} else
121 				bcopy(dhost, (char *)A_A eh->ether_dhost,
122 				      sizeof(dhost));
123 			if (!ip->ip_sum)
124 				ip->ip_sum = chksum((u_short *)ip,
125 						    IP_HL(ip) << 2);
126 			bcopy(ip, (char *)(eh + 1), len);
127 			len += sizeof(*eh);
128 			dumppacket(ip);
129 		} else {
130 			eh = (ether_header_t *)mb.mb_buf;
131 			len = i;
132 		}
133 
134 		if (sendip(wfd, (char *)eh, len) == -1)
135 		    {
136 			perror("send_packet");
137 			break;
138 		    }
139 	    }
140 	(*r->r_close)();
141 	free(eh);
142 	return 0;
143 }
144