summaryrefslogtreecommitdiffstats
path: root/ffvisd/ffvisd.c
blob: 7950e0282e427bbb5e14572a16b0751e4bf0797c (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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
/*
  Copyright (c) 2012, Matthias Schiffer <mschiffer@universe-factory.net>
  All rights reserved.

  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 COPYRIGHT HOLDERS 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 COPYRIGHT HOLDER 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.
*/


#define _GNU_SOURCE

#include <alloca.h>
#include <errno.h>
#include <libgen.h>
#include <poll.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

#include <arpa/inet.h>

#include <linux/rtnetlink.h>

#include <net/ethernet.h>
#include <net/if.h>
#include <net/if_arp.h>

#include <netinet/in.h>

#include <netpacket/packet.h>

#include <sys/types.h>
#include <sys/stat.h>


#define ANNOUNCE_INTERVAL 10
#define ANNOUNCE_TIMEOUT (3*ANNOUNCE_INTERVAL)
#define ANNOUNCE_PRUNE (3*ANNOUNCE_TIMEOUT)

#define SYSFS_PATH_MAX 256
#define SYSFS_CLASS_NET "/sys/class/net"

#define FFD_MAGIC 0xffd
#define FFVISD_VERSION 0

#define FFD_PROTO 0xffd
#define FFD_PORT 0xffd


typedef struct __attribute__((__packed__)) _eth_addr_t {
	uint8_t address[ETH_ALEN];
} eth_addr_t;

typedef struct __attribute__((__packed__)) _ffvisd_ipv6_host_addr_t {
	uint8_t address[8];
} ffvisd_ipv6_host_addr_t;

typedef struct __attribute__((__packed__)) _ffvisd_packet_server_announce_t {
	uint16_t seqno;
	eth_addr_t eth_address;
	ffvisd_ipv6_host_addr_t address;
} ffvisd_packet_server_announce_t;

typedef struct __attribute__((__packed__)) _ffvisd_packet_announce_t {
	uint16_t magic;
	uint16_t version;
	uint16_t csum;
	uint16_t n_servers;
	ffvisd_packet_server_announce_t servers[];
} ffvisd_packet_announce_t;

typedef enum _ffvisd_iftype_t {
	IF_UNKNOWN = 0,
	IF_WIRED,
	IF_WIRELESS,
	IF_VIRTUAL,
	IF_BRIDGE,
	IF_MESH,
	IF_MAX
} ffvisd_iftype_t;

typedef struct _ffvisd_announce_t {
	struct _ffvisd_announce_t *next;

	struct timespec received;
	ffvisd_packet_server_announce_t announce;
} ffvisd_announce_t;

/*static const char *ffvisd_iftype_names[IF_MAX] = {
	[IF_UNKNOWN] = "unknown",
	[IF_WIRED] = "wired",
	[IF_WIRELESS] = "wireless",
	[IF_VIRTUAL] = "virtual",
	[IF_BRIDGE] = "bridge",
	[IF_MESH] = "mesh",
	};*/


typedef void (*iface_cb)(const char *ifname, unsigned ifindex, void *arg);


static const eth_addr_t eth_addr_unspec = {{0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};

static const eth_addr_t ffd_addr = {{0x03, 0x00, 0x00, 0x00, 0x0f, 0xfd}};


static int sockfd;
static char *mesh = "bat0";

static struct timespec now;
static ffvisd_announce_t *announcements = NULL;


/* ones' complement add */
static inline u_int16_t add16(u_int16_t a, u_int16_t b) {
        a += b;
        return a + (a < b);
}

static inline bool is_eth_addr_unspec(const eth_addr_t *address) {
	const uint8_t *a = address->address;

	if (a[0]||a[1]||a[2]||a[3]||a[4]||a[5])
		return false;
	else
		return true;
}

static inline bool are_eth_addrs_equal(const eth_addr_t *address1, const eth_addr_t *address2) {
	const uint8_t *a = address1->address;
	const uint8_t *b = address2->address;

	return (a[0]==b[0] && a[1]==b[1] && a[2]==b[2] && a[3]==b[3] && a[4]==b[4] && a[5]==b[5]);
}

static inline bool are_ipv6_host_addrs_equal(const ffvisd_ipv6_host_addr_t *address1, const ffvisd_ipv6_host_addr_t *address2) {
	const uint8_t *a = address1->address;
	const uint8_t *b = address2->address;

	return (a[0]==b[0] && a[1]==b[1] && a[2]==b[2] && a[3]==b[3] && a[4]==b[4] && a[5]==b[5] && a[6]==b[6] && a[7]==b[7]);
}

/* returns (tp1 - tp2) in milliseconds  */
static inline int timespec_diff(const struct timespec *tp1, const struct timespec *tp2) {
	return ((tp1->tv_sec - tp2->tv_sec))*1000 + (tp1->tv_nsec - tp2->tv_nsec)/1e6;
}


static inline bool are_announcements_equal(const ffvisd_packet_server_announce_t *a1, const ffvisd_packet_server_announce_t *a2) {
	return (are_eth_addrs_equal(&a1->eth_address, &a2->eth_address) && are_ipv6_host_addrs_equal(&a1->address, &a2->address));
}

static inline bool is_announcement_local(const ffvisd_announce_t *a) {
	return (a->received.tv_sec == 0);
}

static inline bool is_announcement_valid(const ffvisd_announce_t *a) {
	if (is_announcement_local(a))
		return true;

	int diff = timespec_diff(&now, &a->received);
	return (diff < ANNOUNCE_TIMEOUT*1000);
}


static bool file_readv(const char *file, const char *format, va_list ap) {
	FILE *f = fopen(file, "r");
	if (!f)
		return false;

	int ret = vfscanf(f, format, ap);

	fclose(f);

	return (ret > 0);
}

static bool iface_file_read(const char *ifname, const char *file, const char *format, ...) {
	char filename[SYSFS_PATH_MAX];

	snprintf(filename, SYSFS_PATH_MAX, SYSFS_CLASS_NET"/%s/%s", ifname, file);

	va_list ap;
	va_start(ap, format);
	bool ret = file_readv(filename, format, ap);
	va_end(ap);

	return ret;
}

static bool iface_file_exists(const char *ifname, const char *file) {
	char filename[SYSFS_PATH_MAX];

	snprintf(filename, SYSFS_PATH_MAX, SYSFS_CLASS_NET"/%s/%s", ifname, file);

	struct stat st;
	return (stat(filename, &st) == 0);
}

static bool iface_is_mesh(const char *ifname) {
	return iface_file_exists(ifname, "mesh");
}

static char* iface_get_mesh(const char *ifname) {
	char *mesh = NULL;

	if (!iface_file_read(ifname, "batman_adv/mesh_iface", "%as", &mesh) || !strcmp(mesh, "none")) {
		free(mesh);
		return NULL;
	}

	return mesh;
}

static char* iface_get_bridge(const char *ifname) {
	char filename[SYSFS_PATH_MAX], filename2[SYSFS_PATH_MAX] = {0};

	snprintf(filename, SYSFS_PATH_MAX, SYSFS_CLASS_NET"/%s/brport/bridge", ifname);
	if (readlink(filename, filename2, sizeof(filename2)) < 0)
		return NULL;

	return strdup(basename(filename2));
}

static eth_addr_t iface_get_eth_address(const char *ifname) {
	eth_addr_t ret;
	uint8_t *a = ret.address;

	if (!iface_file_read(ifname, "address", "%x:%x:%x:%x:%x:%x", &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]))
		return eth_addr_unspec;

	return ret;
}

static struct in6_addr iface_get_address(unsigned ifindex, int scope) {
	struct msg {
		struct nlmsghdr nh;
		struct ifaddrmsg addr;
		uint8_t attrbuf[16384];
	} msg;

	memset(&msg, 0, sizeof(msg));

	int rtnetlink_sk = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);

	msg.nh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
	msg.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ROOT;
	msg.nh.nlmsg_type = RTM_GETADDR;

	msg.addr.ifa_family = AF_INET6;

	if(write(rtnetlink_sk, &msg, msg.nh.nlmsg_len) <= 0) {
		fprintf(stderr, "error: write: %m\n");
		close(rtnetlink_sk);
		return in6addr_any;
	}

	int readlen = read(rtnetlink_sk, &msg, sizeof(msg));

	if (readlen <= 0) {
		fprintf(stderr, "error: read: %m\n");
		close(rtnetlink_sk);
		return in6addr_any;
	}

	close(rtnetlink_sk);

	struct msg *chunk;

	for (chunk = &msg; readlen > sizeof(struct nlmsghdr);) {
		int len = chunk->nh.nlmsg_len - sizeof(struct nlmsghdr);

		if (len < sizeof(struct ifaddrmsg) || readlen < len)
			return in6addr_any;

		if (!NLMSG_OK(&chunk->nh, readlen))
			return in6addr_any;

		if (chunk->nh.nlmsg_type == RTM_NEWADDR && chunk->addr.ifa_scope == scope && chunk->addr.ifa_index == ifindex) {
			struct rtattr *rta = (struct rtattr *)IFA_RTA(&chunk->addr);
			int rtattrlen = IFA_PAYLOAD(&chunk->nh);

			for (; RTA_OK(rta, rtattrlen); rta = RTA_NEXT(rta, rtattrlen)) {
				if(rta->rta_type == IFA_ADDRESS && rta->rta_len == RTA_LENGTH(16)) {
					struct in6_addr ret;
					memcpy(ret.s6_addr, RTA_DATA(rta), 16);
					return ret;
				}
			}
		}

		readlen -= NLMSG_ALIGN(chunk->nh.nlmsg_len);
		chunk = (struct msg*)((uint8_t*)chunk + NLMSG_ALIGN(chunk->nh.nlmsg_len));
	}

	return in6addr_any;
}

static void ffvisd_iface_foreach(iface_cb cb, void *arg) {
	struct if_nameindex *ifaces = if_nameindex();

	if (!ifaces) {
		fprintf(stderr, "error: if_nameindex: %m\n");
		return;
	}

	int i;
	for (i = 0; ifaces[i].if_name; i++)
		cb(ifaces[i].if_name, ifaces[i].if_index, arg);

	if_freenameindex(ifaces);
}


static void join_mcast(const char *ifname, unsigned ifindex, void *arg) {
	char *if_mesh = iface_get_mesh(ifname);

	if (if_mesh && !strcmp(if_mesh, mesh)) {
		struct packet_mreq mr;
		memset(&mr, 0, sizeof(mr));
		mr.mr_ifindex = ifindex;
		mr.mr_type = PACKET_MR_MULTICAST;
		mr.mr_alen = ETH_ALEN;
		memcpy(mr.mr_address, ffd_addr.address, ETH_ALEN);
		if (setsockopt(sockfd, SOL_PACKET, PACKET_ADD_MEMBERSHIP, &mr, sizeof(mr)) && errno != EADDRINUSE)
			fprintf(stderr, "warning: setsockopt: %m\n");
	}

	free(if_mesh);
}

static void pr_announcement_packet(const ffvisd_packet_server_announce_t *announce) {
	const uint8_t *e = announce->eth_address.address, *a = announce->address.address;

	printf("seqno: %u, ether: %02x:%02x:%02x:%02x:%02x:%02x, address: fe80::%02x%02x:%02x%02x:%02x%02x:%02x%02x\n",
	       ntohs(announce->seqno),
	       e[0], e[1], e[2], e[3], e[4], e[5],
	       a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7]);
}

static void pr_announcement(const ffvisd_announce_t *announce) {
	if (is_announcement_local(announce)) {
		printf("local, ");
	}
	else {
		struct timespec tv;
		clock_gettime(CLOCK_MONOTONIC, &tv);
		printf("age: %.1fs, ", timespec_diff(&tv, &announce->received)/1000.0);
	}

	printf("valid: %s, ", is_announcement_valid(announce) ? "yes" : "no");

	pr_announcement_packet(&announce->announce);
}

static bool check_config() {
	if (!iface_is_mesh(mesh)) {
		fprintf(stderr, "error: configured interface is no mesh\n");
		return false;
	}

	return true;
}

static bool init_socket() {
	sockfd = socket(AF_PACKET, SOCK_DGRAM, htons(FFD_PROTO));
	if (sockfd < 0) {
		fprintf(stderr, "error: socket: %m\n");
		return false;
	}

	return true;
}

static bool init_announcements() {
	char *ifname = iface_get_bridge(mesh);

	if (!ifname)
		ifname = strdup(mesh);

	unsigned ifindex = if_nametoindex(ifname);

	if (!ifindex) {
		free(ifname);
		return false;
	}

	ffvisd_announce_t *announce = announcements = calloc(1, sizeof(ffvisd_announce_t));

	announce->announce.eth_address = iface_get_eth_address(ifname);
	if (is_eth_addr_unspec(&announce->announce.eth_address)) {
		free(ifname);
		return false;
	}

	struct in6_addr addr = iface_get_address(ifindex, RT_SCOPE_LINK);
	if (IN6_IS_ADDR_UNSPECIFIED(&addr)) {
		free(ifname);
		return false;
	}

	memcpy(announce->announce.address.address, addr.s6_addr+8, 8);

	free(ifname);

	return true;
}

static void maintenance() {
	clock_gettime(CLOCK_MONOTONIC, &now);

	ffvisd_announce_t **announce, **next;

	for(announce = &announcements; *announce; announce = next) {
		next = &(*announce)->next;

		if (is_announcement_local(*announce)) {
			(*announce)->announce.seqno = htons(ntohs((*announce)->announce.seqno)+1);
			continue;
		}

		int diff = timespec_diff(&now, &(*announce)->received);
		if (diff >= ANNOUNCE_PRUNE*1000) {
			ffvisd_announce_t *del = *announce;

			*announce = *next;
			next = announce;

			free(del);
		}
	}
}

static uint16_t do_checksum(void *data, unsigned n_words) {
	uint16_t ret = 0;
	uint16_t *words = data;

	while(n_words)
		ret = add16(ret, words[--n_words]);

	return ~ret;
}

static void send_announcement(const char *ifname, unsigned ifindex, void *arg) {
	char *if_mesh = iface_get_mesh(ifname);

	if (if_mesh && !strcmp(if_mesh, mesh)) {
		ffvisd_packet_announce_t *packet = arg;

		struct sockaddr_ll sa;
		memset(&sa, 0, sizeof(sa));

		sa.sll_family = AF_PACKET;
		sa.sll_protocol = htons(FFD_PROTO);
		sa.sll_ifindex = ifindex;
		sa.sll_halen = ETH_ALEN;
		memcpy(sa.sll_addr, ffd_addr.address, ETH_ALEN);

		unsigned len = sizeof(ffvisd_packet_announce_t) + ntohs(packet->n_servers)*sizeof(ffvisd_packet_server_announce_t);
		sendto(sockfd, packet, len > 46 ? len : 46, 0, (struct sockaddr*)&sa, sizeof(sa));
	}
	free(if_mesh);
}

static void send_announcements() {
	ffvisd_packet_announce_t *packet = alloca(sizeof(ffvisd_packet_announce_t) + 75*sizeof(ffvisd_packet_server_announce_t));

	memset(packet, 0, 46); /* minimum ethernet packet size is 46 bytes */

	packet->magic = htons(FFD_MAGIC);
	packet->version = htons(FFVISD_VERSION);
	packet->csum = 0;

	uint16_t n_servers = 0;
	ffvisd_announce_t *announce;
	for(announce = announcements; announce; announce = announce->next) {
		if (!is_announcement_valid(announce))
			continue;

		packet->servers[n_servers++] = announce->announce;
	}

	packet->n_servers = htons(n_servers);

	packet->csum = do_checksum(packet, (sizeof(ffvisd_packet_announce_t) + n_servers*sizeof(ffvisd_packet_server_announce_t))/2);

	ffvisd_iface_foreach(send_announcement, packet);
}

static void handle_announcement(const ffvisd_packet_server_announce_t *server) {
	ffvisd_announce_t *announce;
	for(announce = announcements; announce; announce = announce->next) {
		if (!are_announcements_equal(server, &announce->announce))
			continue;

		if (is_announcement_local(announce))
			return;

		uint16_t seqno_diff = ntohs(server->seqno) - ntohs(announce->announce.seqno);

		if (seqno_diff > 0 && seqno_diff < 0x8000) {
			announce->received = now;
			announce->announce = *server;
		}

		return;
	}

	/* no matching annouce was found */
	announce = calloc(1, sizeof(ffvisd_announce_t));
	announce->received = now;
	announce->announce = *server;

	announce->next = announcements;
	announcements = announce;
}

static void receive_announcement() {
	ffvisd_packet_announce_t *packet = alloca(sizeof(ffvisd_packet_announce_t) + 75*sizeof(ffvisd_packet_server_announce_t));

	int readlen = read(sockfd, packet, sizeof(ffvisd_packet_announce_t) + 75*sizeof(ffvisd_packet_server_announce_t));

	if (readlen < 0) {
		fprintf(stderr, "error: read: %m\n");
		return;
	}

	if (readlen < sizeof(ffvisd_packet_announce_t)) {
		fprintf(stderr, "warning: short read");
		return;
	}

	int n_servers = ntohs(packet->n_servers);

	if (readlen < sizeof(ffvisd_packet_announce_t) + n_servers*sizeof(ffvisd_packet_server_announce_t)) {
		fprintf(stderr, "warning: short read");
		return;
	}

	if (do_checksum(packet, (sizeof(ffvisd_packet_announce_t) + n_servers*sizeof(ffvisd_packet_server_announce_t))/2)) {
		fprintf(stderr, "warning: invalid checksum");
		return;
	}

	clock_gettime(CLOCK_MONOTONIC, &now);

	int i;
	for(i = 0; i < n_servers; i++)
		handle_announcement(&packet->servers[i]);
}

int main() {
	if (!check_config())
		return 1;

	if (!init_socket())
		return 1;

	if (!init_announcements())
		return 1;

	struct timespec next_announce;
	clock_gettime(CLOCK_MONOTONIC, &next_announce);

	while (true) {
		ffvisd_iface_foreach(join_mcast, NULL);

		maintenance();

		puts("Dumping announcements...");
		ffvisd_announce_t *announce;
		for(announce = announcements; announce; announce = announce->next)
			pr_announcement(announce);
		puts("");

		send_announcements();

		next_announce.tv_sec += ANNOUNCE_INTERVAL;

		while (true) {
			clock_gettime(CLOCK_MONOTONIC, &now);
			int timeout = timespec_diff(&next_announce, &now);

			if (timeout <= 0)
				break;

			struct pollfd fds[1];

			fds[0].fd = sockfd;
			fds[0].events = POLLIN;

			poll(fds, 1, timeout);

			if (fds[0].revents & POLLIN)
				receive_announcement();
		}
	}

	return 0;
}