File: arch_arp.c

package info (click to toggle)
faumachine 20100527-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 53,836 kB
  • ctags: 20,552
  • sloc: ansic: 179,550; asm: 3,645; makefile: 3,611; perl: 2,103; sh: 1,529; python: 600; xml: 563; lex: 210; vhdl: 204
file content (294 lines) | stat: -rw-r--r-- 7,046 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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/* $Id: arch_arp.c,v 1.13 2009-10-15 13:39:01 potyra Exp $ 
 *
 * Copyright (C) 2008-2009 FAUmachine Team <info@faumachine.org>.
 * This program is free software. You can redistribute it and/or modify it
 * under the terms of the GNU General Public License, either version 2 of
 * the License, or (at your option) any later version. See COPYING.
 */

#ifdef INCLUDE

/* Hack to get ntohl etc working */
#define _LINUX_BYTEORDER_GENERIC_H	1

#include "config.h"

#if defined(OPENBSD) || defined(DARWIN)
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in_systm.h>
#include <net/if.h>
#include <termios.h>
/*
 * IEEE 802.3 Ethernet magic constants.  The frame sizes omit the preamble
 * and FCS/CRC (frame check sequence).
 * Taken from Linux /usr/include/linux/if_ether.h
 */

#define ETH_ALEN        6	/* Octets in one ethernet addr   */
#define ETH_HLEN        14	/* Total octets in header.       */
#define ETH_FRAME_LEN   1514	/* Max. octets in frame sans FCS */

#else
#include <net/ethernet.h>
#endif

#include <net/if_arp.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/if_ether.h>
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_UTIL_H
#include <util.h>
#endif

#ifndef ETHERTYPE_IPV6
#define ETHERTYPE_IPV6 0x86dd
#endif

#endif /* INCLUDE */

#ifdef STATE

#define ARP_CACHE_HASH 16

struct {
	struct arp_host {
		unsigned char mac[ETH_ALEN];
		uint32_t ip;
		struct arp_host *next;
	} *arp_cache_static[ARP_CACHE_HASH],
	  *arp_cache_dynamic[ARP_CACHE_HASH];
} NAME;

#endif /* STATE */

#ifdef BEHAVIOR

/** remember ip/mac accociation */
static int
NAME_(_add_mac)(
	struct cpssp *cpssp,
	struct arp_host **table,
	uint32_t ip,
	const unsigned char *mac
)
{
	struct arp_host *h;

	for (h = table[ip % ARP_CACHE_HASH]; h; h = h->next) {
		if (h->ip != ip)
			continue;

		/* already there, refresh it */
		memcpy(h->mac, mac, ETH_ALEN);
		return 0;
	}
	/* create new entry */
	h = (struct arp_host*) malloc(sizeof(struct arp_host));
	h->ip = ip;
	memcpy(h->mac, mac, ETH_ALEN);
	h->next = table[ip % ARP_CACHE_HASH];
	table[ip % ARP_CACHE_HASH] = h;
	return 0;
}

/** lookup mac address by ip */
static int
NAME_(_ip_to_mac)(
	struct cpssp *cpssp,
	struct arp_host **table,
	uint32_t ip,
	unsigned char *mac
)
{
	struct arp_host *h;

	for (h = table[ip % ARP_CACHE_HASH]; h; h = h->next) {
		if (h->ip != ip)
			continue;

		memcpy(mac, h->mac, ETH_ALEN);
		return 0;
	}

	errno = ENOENT;
	return -1;
}

static int
NAME_(ip_to_mac)(struct cpssp *cpssp, uint32_t ip, unsigned char *mac)
{
	/* lookup mac address, use default gw if we can't find one */
	return NAME_(_ip_to_mac)(cpssp, cpssp->NAME.arp_cache_static,
			ip, mac)
	    && NAME_(_ip_to_mac)(cpssp, cpssp->NAME.arp_cache_dynamic,
			ip, mac);
}

static int
NAME_(resolve)(struct cpssp *cpssp, uint32_t ip, unsigned char *mac)
{
	int ret;

	ret = NAME_(_ip_to_mac)(cpssp, cpssp->NAME.arp_cache_static,
			ip, mac);
	return ret;
}

/** handle arp request */
static void
NAME_(recv)(struct cpssp *cpssp, const unsigned char *_buf, int len)
{
	char buf[1024];
	struct ether_header *ethhdr = (struct ether_header*) buf;
	struct ether_arp *arphdr = (struct ether_arp*)
				(buf + sizeof(struct ether_header));
	uint32_t ip;
	uint32_t tmp;

	memcpy(buf, _buf, len);

#if 0
	/* check */
	errno = EINVAL; /* just in case we fail */
	if( ntohs(arphdr->arp_op) != ARPOP_REQUEST ) return -1;
	if( ntohs(arphdr->arp_hrd) != ARPHRD_ETHER ) return -1;
	//if( arphdr->arp_pro != XXX ) return -1;
	if( arphdr->arp_hln != 6 ) return -1;
	if( arphdr->arp_pln != 4 ) return -1;
#endif

	memcpy(&tmp, &arphdr->arp_tpa, sizeof(tmp));
	ip = ntohl(tmp);

	/* transform request -> reply */
	memcpy(arphdr->arp_tha, arphdr->arp_sha,
			arphdr->arp_hln+arphdr->arp_pln);
	tmp = htonl(ip);
	memcpy(&arphdr->arp_spa, &tmp, sizeof(arphdr->arp_spa));
	arphdr->arp_op = htons(ARPOP_REPLY);
	if (NAME_(resolve)(cpssp, ip, arphdr->arp_sha) != 0) {
		return;
	}

	memcpy(ethhdr->ether_dhost, ethhdr->ether_shost, ETH_ALEN);
	memcpy(ethhdr->ether_shost, arphdr->arp_sha, ETH_ALEN);

	/* send the reply for this request */
	errno = 0;
	NAME_(to_eth)(cpssp, buf, len);
}

/** cache source addr of incoming packets */
static int
NAME_(recv_eth)(struct cpssp *cpssp, const unsigned char *buf, int len)
{
	const struct ether_header *ethhdr = (const struct ether_header *) buf;
	const struct ip *iphdr = (const struct ip*) (buf + sizeof(struct ether_header));

	if(ntohs(ethhdr->ether_type) != ETHERTYPE_IP) return 0;

	return NAME_(_add_mac)(cpssp, cpssp->NAME.arp_cache_dynamic,
			ntohl(iphdr->ip_src.s_addr), ethhdr->ether_shost);
}

static void
NAME_(from_route_ip)(
	struct cpssp *cpssp,
	const unsigned char *ip_buf,
	unsigned int ip_len
)
{
	char buf[2048];
	unsigned int len;
	struct ether_header *ethhdr = (struct ether_header*) buf;
	struct ip *iphdr = (struct ip*) (buf + sizeof(struct ether_header));

	assert(sizeof(struct ether_header) + ip_len < sizeof(buf));

	memcpy(iphdr, ip_buf, ip_len);
	len = sizeof(struct ether_header) + ip_len;

	/*
	 * Initialize eth frame.
	 */
	/* Destination MAC */
	if (iphdr->ip_dst.s_addr == 0xffffffff) {
		/* Broadcast */
		ethhdr->ether_dhost[0] = 0xff;
		ethhdr->ether_dhost[1] = 0xff;
		ethhdr->ether_dhost[2] = 0xff;
		ethhdr->ether_dhost[3] = 0xff;
		ethhdr->ether_dhost[4] = 0xff;
		ethhdr->ether_dhost[5] = 0xff;
	} else {
		NAME_(ip_to_mac)(cpssp, ntohl(iphdr->ip_dst.s_addr),
				ethhdr->ether_dhost);
	}

	/* Source MAC */
	if (NAME_(ip_to_mac)(cpssp, ntohl(iphdr->ip_src.s_addr),
				ethhdr->ether_shost) < 0) {
		/* last try: 0.0.0.0 */
		NAME_(ip_to_mac)(cpssp, 0x00000000, ethhdr->ether_shost);
	}
	ethhdr->ether_type = htons(ETHERTYPE_IP);

	NAME_(to_eth)(cpssp, buf, len);
}

static void
NAME_(from_eth)(struct cpssp *cpssp, const unsigned char *buf, unsigned int len)
{
	const struct ether_header *hdr = (const struct ether_header*) buf;

	/* parse eth header */
	NAME_(recv_eth)(cpssp, buf, len);

	/* delegate according to protocol */
	switch (ntohs(hdr->ether_type)) {
	case ETHERTYPE_ARP:
		NAME_(recv)(cpssp, buf, len);
		break;
	case ETHERTYPE_IP:
		route_ip_from_arp(cpssp, buf + ETH_HLEN, len - ETH_HLEN);
		break;
	case ETHERTYPE_IPV6:
		/* not supported yet */
		break;
	default:
		fprintf(stderr, "arp_from_eth: unknown ether_type=0x%x\n",
				ntohs(hdr->ether_type));
		break;
	}
}

/* methods to configure the arp list */

static void
NAME_(create)(struct cpssp *cpssp, const char *mac, uint32_t ip)
{
	unsigned int i;

	for (i = 0; i < sizeof(cpssp->NAME.arp_cache_static) / sizeof(cpssp->NAME.arp_cache_static[0]); i++) {
		cpssp->NAME.arp_cache_static[i] = NULL;
	}
	for (i = 0; i < sizeof(cpssp->NAME.arp_cache_dynamic) / sizeof(cpssp->NAME.arp_cache_dynamic[0]); i++) {
		cpssp->NAME.arp_cache_dynamic[i] = NULL;
	}

	NAME_(_add_mac)(cpssp, cpssp->NAME.arp_cache_static, ip, mac);
	NAME_(_add_mac)(cpssp, cpssp->NAME.arp_cache_static, 0x00000000, mac);
}

static void
NAME_(destroy)(struct cpssp *cpssp)
{
}

#endif /* BEHAVIOR */