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