File: fragroute.c

package info (click to toggle)
fragroute 1.2-7.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 692 kB
  • ctags: 520
  • sloc: ansic: 4,331; sh: 2,968; makefile: 74
file content (271 lines) | stat: -rw-r--r-- 5,523 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
 * fragroute.c
 *
 * Copyright (c) 2001 Dug Song <dugsong@monkey.org>
 *
 * $Id: fragroute.c,v 1.16 2002/04/07 22:55:20 dugsong Exp $
 */

#include "config.h"

#include <err.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "pkt.h"
#include "mod.h"
#include "tun.h"

struct fr_ctx {
	struct addr	 src;
	struct addr	 dst;
	struct addr	 smac;
	struct addr	 dmac;
	
	int		 mtu;
	
	arp_t		*arp;
	eth_t		*eth;
	intf_t		*intf;
	route_t		*route;
	tun_t		*tun;
};

static struct fr_ctx 	 ctx;

/* XXX - these should be in event.h */
extern int		(*event_sigcb)(void);
extern int		 event_gotsig;

static void
usage(void)
{
	fprintf(stderr, "Usage: fragroute [-f file] dst\n");
	fprintf(stderr, "Rules:\n");
	mod_usage();
	exit(1);
}

#ifndef WIN32
#define SOCKET		int
#define closesocket(x)	close(x)
#endif

static void _resend_outgoing(struct pkt *pkt);

static void
_timed_outgoing(int fd, short event, void *arg)
{
	struct pkt *pkt = (struct pkt *)arg;
	
	memset(&pkt->pkt_ts, 0, sizeof(pkt->pkt_ts));
	_resend_outgoing(pkt);
}

static void
_resend_outgoing(struct pkt *pkt)
{
	if (timerisset(&pkt->pkt_ts)) {
		timeout_set(&pkt->pkt_ev, _timed_outgoing, pkt);
		timeout_add(&pkt->pkt_ev, &pkt->pkt_ts);
	} else {
		eth_pack_hdr(pkt->pkt_eth, ctx.dmac.addr_eth,
		    ctx.smac.addr_eth, ETH_TYPE_IP);
		if (eth_send(ctx.eth, pkt->pkt_data,
		    pkt->pkt_end - pkt->pkt_data) < 0)
			warn("eth_send");
		pkt_free(pkt);
	}
}

static int
fragroute_close(void)
{
	if (ctx.tun != NULL)	tun_close(ctx.tun);
	if (ctx.route != NULL)	route_close(ctx.route);
	if (ctx.intf != NULL)	intf_close(ctx.intf);
	if (ctx.eth != NULL)	eth_close(ctx.eth);
	if (ctx.arp != NULL)	arp_close(ctx.arp);
#ifdef WIN32
	WSACleanup();
#endif
	return (-1);
}

static void
fragroute_process(void *buf, size_t len, void *arg)
{
	struct pktq pktq;
	struct pkt *pkt, *next;
	
	if ((pkt = pkt_new()) == NULL) {
		warn("pkt_new");
		return;
	}
	if (ETH_HDR_LEN + len > PKT_BUF_LEN) {
		warn("dropping oversized packet");
		return;
	}
	memcpy(pkt->pkt_data + ETH_HDR_LEN, buf, len);
	pkt->pkt_end = pkt->pkt_data + ETH_HDR_LEN + len;
	
	pkt_decorate(pkt);
	
	if (pkt->pkt_ip == NULL) {
		warn("dropping non-IP packet");
		return;
	}
	eth_pack_hdr(pkt->pkt_eth, ctx.dmac.addr_eth,
	    ctx.smac.addr_eth, ETH_TYPE_IP);
	
	pkt->pkt_ip->ip_src = ctx.src.addr_ip;
	ip_checksum(pkt->pkt_ip, len);

	TAILQ_INIT(&pktq);
	TAILQ_INSERT_TAIL(&pktq, pkt, pkt_next);
	
	mod_apply(&pktq);

	for (pkt = TAILQ_FIRST(&pktq); pkt != TAILQ_END(&pktq); pkt = next) {
		next = TAILQ_NEXT(pkt, pkt_next);
		_resend_outgoing(pkt);
	}
}

#ifdef WIN32
static BOOL CALLBACK
fragroute_signal(DWORD sig)
{
	warnx("exiting at user request");
	event_gotsig++;
	return (TRUE);
}
#else
static void
fragroute_signal(int sig)
{
	warnx("exiting on signal %d", sig);
	event_gotsig++;
}
#endif

static void
fragroute_init(const char *dst)
{
	struct arp_entry arpent;
	struct intf_entry ifent;
	struct route_entry rtent;
#ifdef WIN32
	WSADATA wsdata;
	
	if (WSAStartup(MAKEWORD(2, 2), &wsdata) != 0)
		err(1, "couldn't initialize Winsock");

	SetConsoleCtrlHandler(fragroute_signal, TRUE);
#else
	signal(SIGINT, fragroute_signal);
	signal(SIGTERM, fragroute_signal);
#endif
	if (addr_aton(dst, &ctx.dst) < 0)
		err(1, "destination address invalid");
	
	if (ctx.dst.addr_bits != IP_ADDR_BITS)
		errx(1, "only /32 destinations supported at this time");
	
	pkt_init(128);
	
	event_init();
	event_sigcb = fragroute_close;
	
	if ((ctx.arp = arp_open()) == NULL ||
	    (ctx.intf = intf_open()) == NULL ||
	    (ctx.route = route_open()) == NULL)
		err(1, "couldn't open kernel networking interfaces");
	
	/* Find outgoing interface, addresses, and MTU. */
	ifent.intf_len = sizeof(ifent);
	if (intf_get_dst(ctx.intf, &ifent, &ctx.dst) < 0)
		err(1, "couldn't determine outgoing interface");
	
	memcpy(&ctx.src, &ifent.intf_addr, sizeof(ctx.src));
	ctx.src.addr_bits = IP_ADDR_BITS;
	memcpy(&ctx.smac, &ifent.intf_link_addr, sizeof(ctx.smac));
	ctx.mtu = ifent.intf_mtu;
	
	/* Open outgoing interface for sending. */
	if ((ctx.eth = eth_open(ifent.intf_name)) == NULL)
		err(1, "couldn't open %s for sending", ifent.intf_name);
	
	/* Find destination MAC address. */
	memcpy(&arpent.arp_pa, &ctx.dst, sizeof(arpent.arp_pa));
	
	if (arp_get(ctx.arp, &arpent) < 0) {
		memcpy(&rtent.route_dst, &ctx.dst, sizeof(rtent.route_dst));
		
		if (route_get(ctx.route, &rtent) < 0)
			err(1, "no route to %s", addr_ntoa(&rtent.route_dst));
		
		memcpy(&arpent.arp_pa, &rtent.route_gw, sizeof(arpent.arp_pa));
		
		if (arp_get(ctx.arp, &arpent) < 0)
			err(1, "no ARP entry for %s",
			    addr_ntoa(&arpent.arp_pa));
	}
	memcpy(&ctx.dmac, &arpent.arp_ha, sizeof(ctx.dmac));
	
	/* Setup our tunnel. */
	if ((ctx.tun = tun_open(&ctx.src, &ctx.dst, ctx.mtu)) == NULL)
		err(1, "couldn't initialize tunnel interface");
	
	tun_register(ctx.tun, fragroute_process, &ctx);
}

static void
fragroute_config(char *config)
{
	if (mod_open(config) < 0) {
		fragroute_close();
		exit(1);
	}
}

static void
fragroute_dispatch(void)
{
	event_dispatch();
}

int
main(int argc, char *argv[])
{
	extern char *optarg;
	extern int optind;
	char *conf;
	int c;

	conf = FRAGROUTE_CONF;
	
	while ((c = getopt(argc, argv, "f:h?")) != -1) {
		switch (c) {
		case 'f':
			conf = optarg;
			break;
		default:
			usage();
		}
	}
	argc -= optind;
	argv += optind;
	
	if (argc != 1)
		usage();
	
	fragroute_init(argv[0]);
	fragroute_config(conf);
	fragroute_dispatch();
	
	exit(0);
}