summaryrefslogtreecommitdiff
path: root/usr.sbin/ipsend/common/ip.c
blob: e05b4b9a3734d84528b1c7fe1abbaa10ba5e46d0 (plain)
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
 * ip.c (C) 1995 Darren Reed
 *
 * The author provides this program as-is, with no gaurantee for its
 * suitability for any specific purpose.  The author takes no responsibility
 * for the misuse/abuse of this program and provides it for the sole purpose
 * of testing packet filter policies.  This file maybe distributed freely
 * providing it is not modified and that this notice remains in tact.
 */
#ifndef	lint
static	char	sccsid[] = "%W% %G% (C)1995";
#endif
#include <errno.h>
#include <stdio.h>
#include <sys/types.h>
#include <netinet/in_systm.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include <netinet/ip_icmp.h>
#ifndef	linux
#include <netinet/if_ether.h>
#include <netinet/ip_var.h>
#include <netinet/tcpip.h>
#endif
#include "ip_compat.h"
#ifdef	linux
#include "tcpip.h"
#endif


static	char	*ipbuf = NULL, *ethbuf = NULL;


u_short	chksum(buf,len)
u_short	*buf;
int	len;
{
	u_long	sum = 0;
	int	nwords = len >> 1;

	for(; nwords > 0; nwords--)
		sum += *buf++;
	sum = (sum>>16) + (sum & 0xffff);
	sum += (sum >>16);
	return (~sum);
}


int	send_ether(nfd, buf, len, gwip)
int	nfd, len;
char	*buf;
struct	in_addr	gwip;
{
	static	struct	in_addr	last_gw;
	static	char	last_arp[6] = { 0, 0, 0, 0, 0, 0};
	ether_header_t	*eh;
	char	*s;
	int	err;

	if (!ethbuf)
		ethbuf = (char *)calloc(1, 65536+1024);
	s = ethbuf;
	eh = (ether_header_t *)s;

	bcopy((char *)buf, s + sizeof(*eh), len);
	if (gwip.s_addr == last_gw.s_addr)
		bcopy(last_arp, eh->ether_dhost, 6);
	else if (arp((char *)&gwip, &eh->ether_dhost) == -1)
	    {
		perror("arp");
		return -2;
	    }
	eh->ether_type = ETHERTYPE_IP;
	last_gw.s_addr = gwip.s_addr;
	err = sendip(nfd, s, sizeof(*eh) + len);
	return err;
}


/*
 */
int	send_ip(nfd, mtu, ip, gwip, frag)
int	nfd, mtu;
ip_t	*ip;
struct	in_addr	gwip;
int	frag;
{
	static	struct	in_addr	last_gw;
	static	char	last_arp[6] = { 0, 0, 0, 0, 0, 0};
	static	u_short	id = 0;
	ether_header_t	*eh;
	ip_t	ipsv;
	int	err;

	if (!ipbuf)
		ipbuf = (char *)malloc(65536);
	eh = (ether_header_t *)ipbuf;

	bzero(&eh->ether_shost, sizeof(eh->ether_shost));
	if (last_gw.s_addr && (gwip.s_addr == last_gw.s_addr))
		bcopy(last_arp, eh->ether_dhost, 6);
	else if (arp((char *)&gwip, &eh->ether_dhost) == -1)
	    {
		perror("arp");
		return -2;
	    }
	bcopy(eh->ether_dhost, last_arp, sizeof(last_arp));
	eh->ether_type = ETHERTYPE_IP;

	bcopy((char *)ip, (char *)&ipsv, sizeof(*ip));
	last_gw.s_addr = gwip.s_addr;
	ip->ip_len = htons(ip->ip_len);
	ip->ip_off = htons(ip->ip_off);
	if (!ip->ip_v)
		ip->ip_v   = IPVERSION;
	if (!ip->ip_id)
		ip->ip_id  = htons(id++);
	if (!ip->ip_ttl)
		ip->ip_ttl = 60;

	if (!frag || (sizeof(*eh) + ntohs(ip->ip_len) < mtu))
	    {
		ip->ip_sum = 0;
		ip->ip_sum = chksum(ip, ip->ip_hl << 2);

		bcopy((char *)ip, ipbuf + sizeof(*eh), ntohs(ip->ip_len));
		err =  sendip(nfd, ipbuf, sizeof(*eh) + ntohs(ip->ip_len));
	    }
	else
	    {
		/*
		 * Actually, this is bogus because we're putting all IP
		 * options in every packet, which isn't always what should be
		 * done.  Will do for now.
		 */
		ether_header_t	eth;
		char	optcpy[48], ol;
		char	*s;
		int	i, iplen, sent = 0, ts, hlen, olen;

		hlen = ip->ip_hl << 2;
		if (mtu < (hlen + 8)) {
			fprintf(stderr, "mtu (%d) < ip header size (%d) + 8\n",
				mtu, hlen);
			fprintf(stderr, "can't fragment data\n");
			return -2;
		}
		ol = (ip->ip_hl << 2) - sizeof(*ip);
		for (i = 0, s = (char*)(ip + 1); ol > 0; )
			if (*s == IPOPT_EOL) {
				optcpy[i++] = *s;
				break;
			} else if (*s == IPOPT_NOP) {
				s++;
				ol--;
			} else
			    {
				olen = (int)(*(u_char *)(s + 1));
				ol -= olen;
				if (IPOPT_COPIED(*s))
				    {
					bcopy(s, optcpy + i, olen);
					i += olen;
					s += olen;
				    }
			    }
		if (i)
		    {
			/*
			 * pad out
			 */
			while ((i & 3) && (i & 3) != 3)
				optcpy[i++] = IPOPT_NOP;
			if (i & 3 == 3)
				optcpy[i++] = IPOPT_EOL;
		    }

		bcopy((char *)eh, (char *)&eth, sizeof(eth));
		s = (char *)ip + hlen;
		iplen = ntohs(ip->ip_len) - hlen;
		ip->ip_off |= htons(IP_MF);

		while (1)
		    {
			if ((sent + (mtu - hlen)) >= iplen)
			    {
				ip->ip_off ^= htons(IP_MF);
				ts = iplen - sent;
			    }
			else
				ts = (mtu - hlen);
			ip->ip_off &= htons(0xe000);
			ip->ip_off |= htons(sent >> 3);
			ts += hlen;
			ip->ip_len = htons(ts);
			ip->ip_sum = 0;
			ip->ip_sum = chksum(ip, hlen);
			bcopy((char *)ip, ipbuf + sizeof(*eh), hlen);
			bcopy(s + sent, ipbuf + sizeof(*eh) + hlen, ts - hlen);
			err =  sendip(nfd, ipbuf, sizeof(*eh) + ts);

			bcopy((char *)&eth, ipbuf, sizeof(eth));
			sent += (ts - hlen);
			if (!(ntohs(ip->ip_off) & IP_MF))
				break;
			else if (!(ip->ip_off & htons(0x1fff)))
			    {
				hlen = i + sizeof(*ip);
				ip->ip_hl = (sizeof(*ip) + i) >> 2;
				bcopy(optcpy, (char *)(ip + 1), i);
			    }
		    }
	    }

	bcopy((char *)&ipsv, (char *)ip, sizeof(*ip));
	return err;
}


/*
 * send a tcp packet.
 */
int	send_tcp(nfd, mtu, ip, gwip)
int	nfd, mtu;
ip_t	*ip;
struct	in_addr	gwip;
{
	static	tcp_seq	iss = 2;
	struct	tcpiphdr *ti;
	int	thlen, i;
	u_long	lbuf[20];

	ti = (struct tcpiphdr *)lbuf;
	bzero((char *)ti, sizeof(*ti));
	thlen = sizeof(tcphdr_t);
	ip->ip_p = IPPROTO_TCP;
	ti->ti_pr = ip->ip_p;
	ti->ti_src = ip->ip_src;
	ti->ti_dst = ip->ip_dst;
	bcopy((char *)ip + (ip->ip_hl << 2),
	      (char *)&ti->ti_sport, sizeof(tcphdr_t));

	if (!ti->ti_win)
		ti->ti_win = htons(4096);
	if (!ti->ti_seq)
		ti->ti_seq = htonl(iss);
	iss += 64;

	if ((ti->ti_flags == TH_SYN) && !ip->ip_off)
	    {
		ip = (ip_t *)realloc((char *)ip, ntohs(ip->ip_len) + 4);
		i = sizeof(struct tcpiphdr) / sizeof(long);
		lbuf[i] = htonl(0x020405b4);
		bcopy((char *)(lbuf + i), (char*)ip + ntohs(ip->ip_len),
		      sizeof(u_long));
		thlen += 4;
	    }
	if (!ti->ti_off)
		ti->ti_off = thlen >> 2;
	ti->ti_len = htons(thlen);
	ip->ip_len = (ip->ip_hl << 2) + thlen;
	ti->ti_sum = 0;
	ti->ti_sum = chksum(ti, thlen + sizeof(ip_t));

	bcopy((char *)&ti->ti_sport,
	      (char *)ip + (ip->ip_hl << 2), thlen);
	return send_ip(nfd, mtu, ip, gwip, 1);
}


/*
 * send a udp packet.
 */
int	send_udp(nfd, mtu, ip, gwip)
int	nfd, mtu;
ip_t	*ip;
struct	in_addr	gwip;
{
	struct	tcpiphdr *ti;
	int	thlen, i;
	u_long	lbuf[20];

	ti = (struct tcpiphdr *)lbuf;
	bzero((char *)ti, sizeof(*ti));
	thlen = sizeof(udphdr_t);
	ti->ti_pr = ip->ip_p;
	ti->ti_src = ip->ip_src;
	ti->ti_dst = ip->ip_dst;
	bcopy((char *)ip + (ip->ip_hl << 2),
	      (char *)&ti->ti_sport, sizeof(udphdr_t));

	ti->ti_len = htons(thlen);
	ip->ip_len = (ip->ip_hl << 2) + thlen;
	ti->ti_sum = 0;
	ti->ti_sum = chksum(ti, thlen + sizeof(ip_t));

	bcopy((char *)&ti->ti_sport,
	      (char *)ip + (ip->ip_hl << 2), sizeof(udphdr_t));
	return send_ip(nfd, mtu, ip, gwip, 1);
}


/*
 * send an icmp packet.
 */
int	send_icmp(nfd, mtu, ip, gwip)
int	nfd, mtu;
ip_t	*ip;
struct	in_addr	gwip;
{
	struct	icmp	*ic;

	ic = (struct icmp *)((char *)ip + (ip->ip_hl << 2));

	ic->icmp_cksum = 0;
	ic->icmp_cksum = chksum((char *)ic, sizeof(struct icmp));

	return send_ip(nfd, mtu, ip, gwip, 1);
}


int	send_packet(nfd, mtu, ip, gwip)
int	nfd, mtu;
ip_t	*ip;
struct	in_addr	gwip;
{
        switch (ip->ip_p)
        {
        case IPPROTO_TCP :
                return send_tcp(nfd, mtu, ip, gwip);
        case IPPROTO_UDP :
                return send_udp(nfd, mtu, ip, gwip);
        case IPPROTO_ICMP :
                return send_icmp(nfd, mtu, ip, gwip);
        default :
                return send_ip(nfd, mtu, ip, gwip, 1);
        }
}