summaryrefslogtreecommitdiffstats
path: root/net/udhcpsnoop/src/dev.c
blob: 8adcfdef431b95a271a12967147bf4ccefc44e96 (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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright (C) 2022 Felix Fietkau <nbd@nbd.name>
 */
#include <netinet/if_ether.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip6.h>
#include <netinet/udp.h>
#include <netpacket/packet.h>
#include <net/if.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>

#include <libubox/vlist.h>
#include <libubox/avl-cmp.h>

#include "dhcpsnoop.h"

#define APPEND(_buf, _ofs, _format, ...) _ofs += snprintf(_buf + _ofs, sizeof(_buf) - _ofs, _format, ##__VA_ARGS__)

struct vlan_hdr {
	uint16_t tci;
	uint16_t proto;
};

struct gre_hdr {
	uint16_t flags;
	uint16_t proto;
};

struct packet {
	void *buffer;
	unsigned int len;
};


struct device {
	struct vlist_node node;
	char ifname[IFNAMSIZ + 1];

	int ifindex;
	bool ingress;
	bool egress;

	bool changed;
	bool active;
};

static void dev_update_cb(struct vlist_tree *tree, struct vlist_node *node_new,
			  struct vlist_node *node_old);

static struct uloop_fd ufd;
static VLIST_TREE(devices, avl_strcmp, dev_update_cb, true, false);

static void *pkt_peek(struct packet *pkt, unsigned int len)
{
	if (len > pkt->len)
		return NULL;

	return pkt->buffer;
}


static void *pkt_pull(struct packet *pkt, unsigned int len)
{
	void *ret = pkt_peek(pkt, len);

	if (!ret)
		return NULL;

	pkt->buffer += len;
	pkt->len -= len;

	return ret;
}

static bool
proto_is_vlan(uint16_t proto)
{
	return proto == ETH_P_8021Q || proto == ETH_P_8021AD;
}

static void
dhcpsnoop_packet_cb(struct packet *pkt)
{
	struct ethhdr *eth;
	struct ip6_hdr *ip6;
	struct ip *ip;
	struct udphdr *udp;
	uint16_t proto, port;
	const char *type;
	bool ipv6 = false;
	uint32_t rebind = 0;

inside_tunnel:
	eth = pkt_pull(pkt, sizeof(*eth));
	if (!eth)
		return;

	proto = be16_to_cpu(eth->h_proto);
	if (proto_is_vlan(proto)) {
		struct vlan_hdr *vlan;

		vlan = pkt_pull(pkt, sizeof(*vlan));
		if (!vlan)
			return;

		proto = be16_to_cpu(vlan->proto);
	}

	switch (proto) {
	case ETH_P_IP:
		ip = pkt_peek(pkt, sizeof(struct ip));
		if (!ip)
			return;

		if (!pkt_pull(pkt, ip->ip_hl * 4))
			return;

		proto = ip->ip_p;
		break;
	case ETH_P_IPV6:
		ip6 = pkt_pull(pkt, sizeof(*ip6));
		if (!ip6)
			return;

		proto = ip6->ip6_nxt;
		ipv6 = true;
		break;
	default:
		return;
	}

	if (proto == IPPROTO_GRE) {
		struct gre_hdr *gre;
		gre = pkt_pull(pkt, sizeof(*gre));
		if (!gre) return;
		proto = be16_to_cpu(gre->proto);
		if (proto != 0x6558) return;
		goto inside_tunnel;
	}

	if (proto != IPPROTO_UDP)
		return;

	udp = pkt_pull(pkt, sizeof(struct udphdr));
	if (!udp)
		return;

	port = ntohs(udp->uh_sport);

	if (!ipv6)
		type = dhcpsnoop_parse_ipv4(pkt->buffer, pkt->len, port, &rebind);
	else
		type = dhcpsnoop_parse_ipv6(pkt->buffer, pkt->len, port);

	if (!type)
		return;

	dhcpsnoop_ubus_notify(type, pkt->buffer, pkt->len);
	if (!ipv6 && !strcmp(type, "ack") && rebind)
		cache_entry(pkt->buffer, rebind);
}

static void
dhcpsnoop_socket_cb(struct uloop_fd *fd, unsigned int events)
{
	static uint8_t buf[8192];
	struct packet pkt = {
		.buffer = buf,
	};
	int len;

retry:
	len = recvfrom(fd->fd, buf, sizeof(buf), MSG_DONTWAIT, NULL, NULL);
	if (len < 0) {
		if (errno == EINTR)
			goto retry;
		return;
	}

	if (!len)
		return;

	pkt.len = len;
	dhcpsnoop_packet_cb(&pkt);
}

static int
dhcpsnoop_open_socket(void)
{
	struct sockaddr_ll sll = {
		.sll_family = AF_PACKET,
		.sll_protocol = htons(ETH_P_ALL),
	};
	int sock;

	sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
	if (sock == -1) {
		ULOG_ERR("failed to create raw socket: %s\n", strerror(errno));
		return -1;
	}

	sll.sll_ifindex = if_nametoindex(DHCPSNOOP_IFB_NAME);
	if (bind(sock, (struct sockaddr *)&sll, sizeof(sll))) {
		ULOG_ERR("failed to bind socket to "DHCPSNOOP_IFB_NAME": %s\n",
			 strerror(errno));
		goto error;
	}

	fcntl(sock, F_SETFL, fcntl(sock, F_GETFL) | O_NONBLOCK);

	ufd.fd = sock;
	ufd.cb = dhcpsnoop_socket_cb;
	uloop_fd_add(&ufd, ULOOP_READ);

	return 0;

error:
	close(sock);
	return -1;
}

static int
prepare_filter_cmd(char *buf, int len, const char *dev, int prio, bool add, bool egress)
{
	return snprintf(buf, len, "tc filter %s dev '%s' %sgress prio %d",
			add ? "add" : "del", dev, egress ? "e" : "in", prio);
}

#define MATCH_GRE_ETH_IP_UDP_DHCP_67 \
	" match u16 0x6558 0xffff at 22 " \
	" match u16 0x0800 0xffff at 36 " \
	" match u8 17 0xff at 47 " \
	" match u16 67 0xffff at 58 "

#define MATCH_GRE_ETH_VLAN_IP_UDP_DHCP_67 \
	" match u16 0x6558 0xffff at 22 " \
	" match u16 0x8100 0xffff at 36 " \
	" match u16 0x0800 0xffff at 40 " \
	" match u8 17 0xff at 51 " \
	" match u16 67 0xffff at 62 "

#define MATCH_GRE_ETH_IP_UDP_DHCP_68 \
	" match u16 0x6558 0xffff at 22 " \
	" match u16 0x0800 0xffff at 36 " \
	" match u8 17 0xff at 47 " \
	" match u16 68 0xffff at 58 "

#define MATCH_GRE_ETH_VLAN_IP_UDP_DHCP_68 \
	" match u16 0x6558 0xffff at 22 " \
	" match u16 0x8100 0xffff at 36 " \
	" match u16 0x0800 0xffff at 40 " \
	" match u8 17 0xff at 51 " \
	" match u16 68 0xffff at 62 "

static void
dhcpsnoop_dev_attach_filters(struct device *dev, bool egress)
{
	int prio = DHCPSNOOP_PRIO_BASE;
	char buf[350];
	int ofs;

	ofs = prepare_filter_cmd(buf, sizeof(buf), dev->ifname, prio++, true, egress);
	APPEND(buf, ofs, " protocol ip u32 match ip sport 67 0xffff"
			 " flowid 1:1 action mirred ingress mirror dev " DHCPSNOOP_IFB_NAME " continue");
	dhcpsnoop_run_cmd(buf, false);

	ofs = prepare_filter_cmd(buf, sizeof(buf), dev->ifname, prio++, true, egress);
	APPEND(buf, ofs, " protocol 802.1Q u32 offset plus 4 match ip sport 67 0xffff"
			 " flowid 1:1 action mirred ingress mirror dev " DHCPSNOOP_IFB_NAME " continue");
	dhcpsnoop_run_cmd(buf, false);

	ofs = prepare_filter_cmd(buf, sizeof(buf), dev->ifname, prio++, true, egress);
	APPEND(buf, ofs, " protocol ip u32 match ip sport 68 0xffff"
			 " flowid 1:1 action mirred ingress mirror dev " DHCPSNOOP_IFB_NAME " continue");
	dhcpsnoop_run_cmd(buf, false);

	ofs = prepare_filter_cmd(buf, sizeof(buf), dev->ifname, prio++, true, egress);
	APPEND(buf, ofs, " protocol 802.1Q u32 offset plus 4 match ip sport 68 0xffff"
			 " flowid 1:1 action mirred ingress mirror dev " DHCPSNOOP_IFB_NAME " continue");
	dhcpsnoop_run_cmd(buf, false);

	/* GRE */
	ofs = prepare_filter_cmd(buf, sizeof(buf), dev->ifname, prio++, true, egress);
	APPEND(buf, ofs, " protocol ip u32 match ip protocol 47 0xff"
			 MATCH_GRE_ETH_IP_UDP_DHCP_67
			 " flowid 1:1 action mirred ingress mirror dev " DHCPSNOOP_IFB_NAME " continue");
	dhcpsnoop_run_cmd(buf, false);

	ofs = prepare_filter_cmd(buf, sizeof(buf), dev->ifname, prio++, true, egress);
	APPEND(buf, ofs, " protocol ip u32 match ip protocol 47 0xff"
			 MATCH_GRE_ETH_IP_UDP_DHCP_68
			 " flowid 1:1 action mirred ingress mirror dev " DHCPSNOOP_IFB_NAME " continue");
	dhcpsnoop_run_cmd(buf, false);

	ofs = prepare_filter_cmd(buf, sizeof(buf), dev->ifname, prio++, true, egress);
	APPEND(buf, ofs, " protocol ip u32 match ip protocol 47 0xff "
			 MATCH_GRE_ETH_VLAN_IP_UDP_DHCP_67
			 " flowid 1:1 action mirred ingress mirror dev " DHCPSNOOP_IFB_NAME " continue");
	dhcpsnoop_run_cmd(buf, false);

	ofs = prepare_filter_cmd(buf, sizeof(buf), dev->ifname, prio++, true, egress);
	APPEND(buf, ofs, " protocol ip u32 match ip protocol 47 0xff"
			 MATCH_GRE_ETH_VLAN_IP_UDP_DHCP_68
			 " flowid 1:1 action mirred ingress mirror dev " DHCPSNOOP_IFB_NAME " continue");
	dhcpsnoop_run_cmd(buf, false);

	/* IPv6 */
	ofs = prepare_filter_cmd(buf, sizeof(buf), dev->ifname, prio++, true, egress);
	APPEND(buf, ofs, " protocol ipv6 u32 match ip6 sport 546 0xfffe"
			 " flowid 1:1 action mirred ingress mirror dev " DHCPSNOOP_IFB_NAME " continue");
	dhcpsnoop_run_cmd(buf, false);

	ofs = prepare_filter_cmd(buf, sizeof(buf), dev->ifname, prio++, true, egress);
	APPEND(buf, ofs, " protocol 802.1Q u32 offset plus 4 match ip6 sport 546 0xfffe"
			 " flowid 1:1 action mirred ingress mirror dev " DHCPSNOOP_IFB_NAME " continue");
	dhcpsnoop_run_cmd(buf, false);
}

static void
dhcpsnoop_dev_cleanup_filters(struct device *dev, bool egress)
{
	char buf[128];
	int i;

	for (i = DHCPSNOOP_PRIO_BASE; i < DHCPSNOOP_PRIO_BASE + 10; i++) {
		prepare_filter_cmd(buf, sizeof(buf), dev->ifname, i, false, egress);
		dhcpsnoop_run_cmd(buf, true);
	}
}

static void
dhcpsnoop_dev_attach(struct device *dev)
{
	char buf[64];

	dev->active = true;
	snprintf(buf, sizeof(buf), "tc qdisc add dev '%s' clsact", dev->ifname);
	dhcpsnoop_run_cmd(buf, true);

	if (dev->ingress)
		dhcpsnoop_dev_attach_filters(dev, false);
	if (dev->egress)
		dhcpsnoop_dev_attach_filters(dev, true);
}

static void
dhcpsnoop_dev_cleanup(struct device *dev)
{
	dev->active = false;
	dhcpsnoop_dev_cleanup_filters(dev, true);
	dhcpsnoop_dev_cleanup_filters(dev, false);
}

static void
__dhcpsnoop_dev_check(struct device *dev)
{
	int ifindex;

	ifindex = if_nametoindex(dev->ifname);
	if (ifindex != dev->ifindex) {
		dev->ifindex = ifindex;
		dev->changed = true;
	}

	if (!dev->changed)
		return;

	dev->changed = false;
	dhcpsnoop_dev_cleanup(dev);
	if (ifindex)
		dhcpsnoop_dev_attach(dev);
}

static void dev_update_cb(struct vlist_tree *tree, struct vlist_node *node_new,
			  struct vlist_node *node_old)
{
	struct device *dev = NULL, *dev_free = NULL;

	if (node_old && node_new) {
		dev = container_of(node_old, struct device, node);
		dev_free = container_of(node_new, struct device, node);

		if (dev->ingress != dev_free->ingress ||
			dev->egress != dev_free->egress)
			dev->changed = true;

		dev->ingress = dev_free->ingress;
		dev->egress = dev_free->egress;
	} else if (node_old) {
		dev_free = container_of(node_old, struct device, node);
		if (dev_free->active)
			dhcpsnoop_dev_cleanup(dev_free);
	} else if (node_new) {
		dev = container_of(node_new, struct device, node);
	}

	if (dev)
		__dhcpsnoop_dev_check(dev);
	if (dev_free)
		free(dev_free);
}

static void
dhcpsnoop_dev_config_add(struct blob_attr *data)
{
	enum {
		DEV_ATTR_INGRESS,
		DEV_ATTR_EGRESS,
		__DEV_ATTR_MAX
	};
	static const struct blobmsg_policy policy[__DEV_ATTR_MAX] = {
		[DEV_ATTR_INGRESS] = { "ingress", BLOBMSG_TYPE_BOOL },
		[DEV_ATTR_EGRESS] = { "egress", BLOBMSG_TYPE_BOOL },
	};
	struct blob_attr *tb[__DEV_ATTR_MAX];
	struct blob_attr *cur;
	struct device *dev;
	int len;

	if (blobmsg_type(data) != BLOBMSG_TYPE_TABLE)
		return;

	dev = calloc(1, sizeof(*dev));
	len = snprintf(dev->ifname, sizeof(dev->ifname), "%s", blobmsg_name(data));
	if (!len || len > IFNAMSIZ)
		goto free;

	blobmsg_parse(policy, ARRAY_SIZE(tb), tb, blobmsg_data(data), blobmsg_len(data));

	if ((cur = tb[DEV_ATTR_INGRESS]) != NULL)
		dev->ingress = blobmsg_get_bool(cur);
	if ((cur = tb[DEV_ATTR_EGRESS]) != NULL)
		dev->egress = blobmsg_get_bool(cur);

	if (!dev->ingress && !dev->egress)
		goto free;

	vlist_add(&devices, &dev->node, dev->ifname);
	return;

free:
	free(dev);
	return;
}

void dhcpsnoop_dev_config_update(struct blob_attr *data, bool add_only)
{
	struct blob_attr *cur;
	int rem;

	if (!add_only)
		vlist_update(&devices);

	blobmsg_for_each_attr(cur, data, rem)
		dhcpsnoop_dev_config_add(cur);

	if (!add_only)
		vlist_flush(&devices);
}

void dhcpsnoop_dev_check(void)
{
	struct device *dev;

	vlist_for_each_element(&devices, dev, node)
		__dhcpsnoop_dev_check(dev);
}

int dhcpsnoop_dev_init(void)
{
	dhcpsnoop_dev_done();

	if (dhcpsnoop_run_cmd("ip link add "DHCPSNOOP_IFB_NAME" type ifb", false) ||
	    dhcpsnoop_run_cmd("ip link set dev "DHCPSNOOP_IFB_NAME" up", false) ||
	    dhcpsnoop_open_socket())
		return -1;

	return 0;
}

void dhcpsnoop_dev_done(void)
{
	if (ufd.registered) {
		uloop_fd_delete(&ufd);
		close(ufd.fd);
	}

	dhcpsnoop_run_cmd("ip link del "DHCPSNOOP_IFB_NAME, true);
	vlist_flush_all(&devices);
}