xref: /freebsd/sbin/ipf/ipsend/resend.c (revision 045c8f526484cb3b97f5fd693987f4376fa43c5f)
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 # include <netinet/ip_var.h>
23 # include <netinet/if_ether.h>
24 #include <stdio.h>
25 #include <netdb.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include "ipsend.h"
30 
31 extern	int	opts;
32 
33 void	dumppacket(ip_t *);
34 
35 
36 void
37 dumppacket(ip_t *ip)
38 {
39 	tcphdr_t *t;
40 	int i, j;
41 
42 	t = (tcphdr_t *)((char *)ip + (IP_HL(ip) << 2));
43 	if (ip->ip_tos)
44 		printf("tos %#x ", ip->ip_tos);
45 	if (ip->ip_off & 0x3fff)
46 		printf("frag @%#x ", (ip->ip_off & 0x1fff) << 3);
47 	printf("len %d id %d ", ip->ip_len, ip->ip_id);
48 	printf("ttl %d p %d src %s", ip->ip_ttl, ip->ip_p,
49 		inet_ntoa(ip->ip_src));
50 	if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_UDP)
51 		printf(",%d", t->th_sport);
52 	printf(" dst %s", inet_ntoa(ip->ip_dst));
53 	if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_UDP)
54 		printf(",%d", t->th_dport);
55 	if (ip->ip_p == IPPROTO_TCP) {
56 		printf(" seq %lu:%lu flags ",
57 			(u_long)t->th_seq, (u_long)t->th_ack);
58 		for (j = 0, i = 1; i < 256; i *= 2, j++)
59 			if (t->th_flags & i)
60 				printf("%c", "FSRPAU--"[j]);
61 	}
62 	putchar('\n');
63 }
64 
65 
66 int
67 ip_resend(char *dev, int mtu, struct  ipread  *r, struct  in_addr gwip,
68 	char *datain)
69 {
70 	ether_header_t	*eh;
71 	char	dhost[6];
72 	ip_t	*ip;
73 	int	fd, wfd = initdevice(dev, 5), len, i;
74 	mb_t	mb;
75 
76 	if (wfd == -1)
77 		return (-1);
78 
79 	if (datain)
80 		fd = (*r->r_open)(datain);
81 	else
82 		fd = (*r->r_open)("-");
83 
84 	if (fd < 0)
85 		exit(-1);
86 
87 	ip = (struct ip *)mb.mb_buf;
88 	eh = (ether_header_t *)malloc(sizeof(*eh));
89 	if(!eh)
90 	    {
91 		perror("malloc failed");
92 		return (-2);
93 	    }
94 
95 	bzero((char *) &eh->ether_shost, sizeof(eh->ether_shost));
96 	if (gwip.s_addr && (arp((char *)&gwip, dhost) == -1))
97 	    {
98 		perror("arp");
99 		free(eh);
100 		return (-2);
101 	    }
102 
103 	while ((i = (*r->r_readip)(&mb, NULL, NULL)) > 0)
104 	    {
105 		if (!(opts & OPT_RAW)) {
106 			len = ntohs(ip->ip_len);
107 			eh = (ether_header_t *)realloc((char *)eh, sizeof(*eh) + len);
108 			eh->ether_type = htons((u_short)ETHERTYPE_IP);
109 			if (!gwip.s_addr) {
110 				if (arp((char *)&gwip,
111 					(char *) &eh->ether_dhost) == -1) {
112 					perror("arp");
113 					continue;
114 				}
115 			} else
116 				bcopy(dhost, (char *) &eh->ether_dhost,
117 				      sizeof(dhost));
118 			if (!ip->ip_sum)
119 				ip->ip_sum = chksum((u_short *)ip,
120 						    IP_HL(ip) << 2);
121 			bcopy(ip, (char *)(eh + 1), len);
122 			len += sizeof(*eh);
123 			dumppacket(ip);
124 		} else {
125 			eh = (ether_header_t *)mb.mb_buf;
126 			len = i;
127 		}
128 
129 		if (sendip(wfd, (char *)eh, len) == -1)
130 		    {
131 			perror("send_packet");
132 			break;
133 		    }
134 	    }
135 	(*r->r_close)();
136 	free(eh);
137 	return (0);
138 }
139