summaryrefslogtreecommitdiffstats
path: root/rtemsbsd/rtems/rtems-bsd-arp-processor.c
blob: 7f65a4b4899c9c0673467ad7e7a059280448ce0e (plain) (blame)
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
/*
 * Copyright (c) 2014 embedded brains GmbH.  All rights reserved.
 *
 *  embedded brains GmbH
 *  Dornierstr. 4
 *  82178 Puchheim
 *  Germany
 *  <rtems@embedded-brains.de>
 *
 * Copyright (c) 2006-2013 Roy Marples <roy@marples.name>
 *
 * Some parts are derived from the dhcpcd sources.
 *
 * http://roy.marples.name/projects/dhcpcd
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <rtems/bsd/util.h>

#include <sys/cdefs.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>

#include <net/bpf.h>
#include <net/if_dl.h>
#include <arpa/inet.h>

#include <errno.h>
#include <fcntl.h>
#include <ifaddrs.h>
#include <stdlib.h>
#include <string.h>

struct rtems_bsd_arp_processor_context {
	int fd;
	uint8_t eaddr[ETHER_ADDR_LEN];
	size_t buf_size;
	uint8_t buf[];
};

#ifndef BPF_ETHCOOK
# define BPF_ETHCOOK 0
#endif
#ifndef BPF_WHOLEPACKET
# define BPF_WHOLEPACKET ~0U
#endif
static const struct bpf_insn arp_bpf_filter[] = {
	/* Make sure this is an ARP packet... */
	BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 12),
	BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_ARP, 0, 3),
	/* Make sure this is an ARP REQUEST... */
	BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 20 + BPF_ETHCOOK),
	BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ARPOP_REQUEST, 2, 0),
	/* or ARP REPLY... */
	BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 20 + BPF_ETHCOOK),
	BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ARPOP_REPLY, 0, 1),
	/* If we passed all the tests, ask for the whole packet. */
	BPF_STMT(BPF_RET + BPF_K, BPF_WHOLEPACKET),
	/* Otherwise, drop it. */
	BPF_STMT(BPF_RET + BPF_K, 0)
};

static const size_t arp_bpf_filter_len =
    sizeof(arp_bpf_filter) / sizeof(arp_bpf_filter[0]);

static void
process_arp_packet(const uint8_t *pkt, size_t pkt_size, int fd,
    const uint8_t eaddr[ETHER_ADDR_LEN], rtems_bsd_arp_processor processor,
    void *arg)
{
	struct arphdr ar;
	uint32_t spa;
	uint32_t tpa;
	const uint8_t *sha;
	const uint8_t *tha;

	/* We must have a full ARP header */
	if (pkt_size < sizeof(ar))
		return;

	memcpy(&ar, pkt, sizeof(ar));

	/* Protocol must be IP. */
	if (ar.ar_pro != htons(ETHERTYPE_IP))
		return;

	if (ar.ar_pln != sizeof(spa))
		return;

	/* Only these types are recognised */
	if (ar.ar_op != htons(ARPOP_REPLY) &&
	    ar.ar_op != htons(ARPOP_REQUEST))
		return;

	/* Get pointers to the hardware addresses */
	sha = pkt + sizeof(ar);
	tha = sha + ar.ar_hln + ar.ar_pln;

	/* Ensure we got all the data */
	if ((tha + ar.ar_hln + ar.ar_pln) - pkt > pkt_size)
		return;

	/* Ignore messages from ourself */
	if (ar.ar_hln == ETHER_ADDR_LEN &&
	    memcmp(sha, &eaddr[0], ETHER_ADDR_LEN) == 0)
		return;

	/* Copy out the IP addresses */
	memcpy(&spa, sha + ar.ar_hln, ar.ar_pln);
	memcpy(&tpa, tha + ar.ar_hln, ar.ar_pln);

	(*processor)(arg, fd, eaddr, &ar, spa, tpa, sha, tha);
}

rtems_bsd_arp_processor_context *
rtems_bsd_arp_processor_create(const char *ifname)
{
	rtems_bsd_arp_processor_context *ctx = NULL;
	int fd = -1;
	struct ifreq ifr;
	int buf_len;
	size_t buf_size;
	struct bpf_version pv;
	struct bpf_program pf;
	int flags;
	int rv;

	fd = open("/dev/bpf", O_RDWR);
	if (fd < 0) {
		goto error;
	}

	rv = ioctl(fd, BIOCVERSION, &pv);
	if (rv != 0 || pv.bv_major != BPF_MAJOR_VERSION ||
	    pv.bv_minor < BPF_MINOR_VERSION) {
		goto error;
	}

	memset(&ifr, 0, sizeof(ifr));
	strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
	rv = ioctl(fd, BIOCSETIF, &ifr);
	if (rv != 0) {
		goto error;
	}

	rv = ioctl(fd, BIOCGBLEN, &buf_len);
	if (rv != 0 || buf_len <= 0) {
		goto error;
	}
	buf_size = (size_t) buf_len;

	flags = 1;
	rv = ioctl(fd, BIOCIMMEDIATE, &flags);
	if (rv != 0) {
		goto error;
	}

	pf.bf_insns = __DECONST(struct bpf_insn *, arp_bpf_filter);
	pf.bf_len = arp_bpf_filter_len;
	rv = ioctl(fd, BIOCSETF, &pf);
	if (rv != 0) {
		goto error;
	}

	ctx = malloc(sizeof(*ctx) + buf_size);
	if (ctx == NULL) {
		goto error;
	}

	rv = rtems_bsd_get_ethernet_addr(ifname, &ctx->eaddr[0]);
	if (rv != 0) {
		goto error;
	}

	ctx->fd = fd;
	ctx->buf_size = buf_size;

	return ctx;

error:
	close(fd);
	free(ctx);

	return NULL;
}

int
rtems_bsd_arp_processor_get_file_descriptor(
    const rtems_bsd_arp_processor_context *ctx)
{
	return ctx->fd;
}

int
rtems_bsd_arp_processor_process(
    rtems_bsd_arp_processor_context *ctx,
    rtems_bsd_arp_processor processor, void *arg)
{
	ssize_t n;
	size_t buf_avail;
	size_t buf_pos;

	n = read(ctx->fd, &ctx->buf[0], ctx->buf_size);
	if (n < 0) {
		return -1;
	}

	buf_avail = (size_t) n;
	buf_pos = 0;

	while (buf_pos < buf_avail) {
		const uint8_t *pkt = &ctx->buf[0] + buf_pos;
		struct bpf_hdr bp;

		memcpy(&bp, pkt, sizeof(bp));

		if (bp.bh_caplen == bp.bh_datalen &&
		    buf_pos + bp.bh_caplen + bp.bh_hdrlen <= buf_avail) {
			const uint8_t *payload = pkt + bp.bh_hdrlen +
			    ETHER_HDR_LEN;
			size_t payload_size = bp.bh_caplen - ETHER_HDR_LEN;

			process_arp_packet(payload, payload_size, ctx->fd, ctx->eaddr,
			    processor, NULL);
		}

		buf_pos += BPF_WORDALIGN(bp.bh_hdrlen + bp.bh_caplen);
	}

	return 0;
}