summaryrefslogtreecommitdiff
path: root/mesh/rpl.c
blob: 1213ee66e745fe0a3e47a26f4fe88139e3077e43 (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
// SPDX-License-Identifier: LGPL-2.1-or-later
/*
 *
 *  BlueZ - Bluetooth protocol stack for Linux
 *
 *  Copyright (C) 2020  Intel Corporation. All rights reserved.
 *
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#define _GNU_SOURCE
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <unistd.h>
#include <dirent.h>
#include <errno.h>

#include <sys/stat.h>

#include <ell/ell.h>

#include "mesh/mesh-defs.h"

#include "mesh/node.h"
#include "mesh/net.h"
#include "mesh/util.h"
#include "mesh/rpl.h"

const char *rpl_dir = "/rpl";

bool rpl_put_entry(struct mesh_node *node, uint16_t src, uint32_t iv_index,
								uint32_t seq)
{
	const char *node_path;
	char src_file[PATH_MAX];
	char seq_txt[7];
	bool result = false;
	DIR *dir;
	int fd;

	if (!IS_UNICAST(src))
		return false;

	node_path = node_get_storage_dir(node);

	if (strlen(node_path) + strlen(rpl_dir) + 15 >= PATH_MAX)
		return false;

	snprintf(src_file, PATH_MAX, "%s%s/%8.8x", node_path, rpl_dir,
								iv_index);
	dir = opendir(src_file);

	if (!dir) {
		if (mkdir(src_file, 0755) != 0)
			l_error("Failed to create dir: %s", src_file);
	} else
		closedir(dir);

	snprintf(src_file, PATH_MAX, "%s%s/%8.8x/%4.4x", node_path, rpl_dir,
								iv_index, src);

	fd = open(src_file, O_WRONLY | O_CREAT | O_TRUNC, 0600);
	if (fd >= 0) {
		snprintf(seq_txt, 7, "%6.6x", seq);
		if (write(fd, seq_txt, 6) == 6)
			result = true;

		close(fd);
	}

	if (!result)
		return false;

	/* Delete RPL entry from old iv_index (if it exists) */
	iv_index--;
	snprintf(src_file, PATH_MAX, "%s%s/%8.8x/%4.4x", node_path, rpl_dir,
								iv_index, src);
	if (remove(src_file) < 0 && errno != ENOENT)
		l_error("Failed to remove(%d): %s", errno, src_file);

	return result;
}

void rpl_del_entry(struct mesh_node *node, uint16_t src)
{
	const char *node_path;
	char rpl_path[PATH_MAX];
	struct dirent *entry;
	DIR *dir;

	if (!IS_UNICAST(src))
		return;

	node_path = node_get_storage_dir(node);

	if (strlen(node_path) + strlen(rpl_dir) + 15 >= PATH_MAX)
		return;

	snprintf(rpl_path, PATH_MAX, "%s%s", node_path, rpl_dir);
	dir = opendir(rpl_path);

	if (!dir)
		return;

	/* Remove all instances of src address */
	while ((entry = readdir(dir)) != NULL) {
		if (entry->d_type == DT_DIR && entry->d_name[0] != '.') {
			snprintf(rpl_path, PATH_MAX, "%s%s/%s/%4.4x",
					node_path, rpl_dir, entry->d_name, src);
			if (remove(rpl_path) < 0)
				l_error("Failed to remove(%d): %s", errno,
								rpl_path);
		}
	}

	closedir(dir);
}

static bool match_src(const void *a, const void *b)
{
	const struct mesh_rpl *rpl = a;
	uint16_t src = L_PTR_TO_UINT(b);

	return rpl->src == src;
}

static void get_entries(const char *iv_path, struct l_queue *rpl_list)
{
	struct mesh_rpl *rpl;
	struct dirent *entry;
	DIR *dir;
	int fd;
	const char *iv_txt;
	char src_path[PATH_MAX];
	char seq_txt[7];
	uint32_t iv_index, seq;
	uint16_t src;

	dir = opendir(iv_path);

	if (!dir)
		return;

	iv_txt = basename(iv_path);
	if (sscanf(iv_txt, "%08x", &iv_index) != 1) {
		closedir(dir);
		return;
	}

	memset(seq_txt, 0, sizeof(seq_txt));

	while ((entry = readdir(dir)) != NULL) {
		/* RPL sequences are stored in src files under iv_index */
		if (entry->d_type == DT_REG) {
			if (sscanf(entry->d_name, "%04hx", &src) != 1)
				continue;

			snprintf(src_path, PATH_MAX, "%s/%4.4x", iv_path, src);
			fd = open(src_path, O_RDONLY);

			if (fd < 0)
				continue;

			if (read(fd, seq_txt, 6) == 6 &&
					sscanf(seq_txt, "%06x", &seq) == 1) {

				rpl = l_queue_find(rpl_list, match_src,
						L_UINT_TO_PTR(src));

				if (rpl) {
					/* Replace older entries */
					if (rpl->iv_index < iv_index) {
						rpl->iv_index = iv_index;
						rpl->seq = seq;
					}
				} else if (seq <= SEQ_MASK && IS_UNICAST(src)) {
					rpl = l_new(struct mesh_rpl, 1);
					rpl->src = src;
					rpl->iv_index = iv_index;
					rpl->seq = seq;

					l_queue_push_head(rpl_list, rpl);
				}
			}
			close(fd);
		}
	}

	closedir(dir);
}

bool rpl_get_list(struct mesh_node *node, struct l_queue *rpl_list)
{
	const char *node_path;
	struct dirent *entry;
	char *rpl_path;
	size_t len;
	DIR *dir;

	if (!rpl_list)
		return false;

	node_path = node_get_storage_dir(node);

	len = strlen(node_path) + strlen(rpl_dir) + 15;

	if (len > PATH_MAX)
		return false;

	rpl_path = l_malloc(len);
	snprintf(rpl_path, len, "%s%s", node_path, rpl_dir);

	dir = opendir(rpl_path);

	if (!dir) {
		l_error("Failed to read RPL dir: %s", rpl_path);
		l_free(rpl_path);
		return false;
	}

	while ((entry = readdir(dir)) != NULL) {
		/* RPL sequences are stored in files under iv_indexs */
		if (entry->d_type == DT_DIR && entry->d_name[0] != '.') {
			snprintf(rpl_path, len, "%s%s/%s",
					node_path, rpl_dir, entry->d_name);
			get_entries(rpl_path, rpl_list);
		}
	}

	l_free(rpl_path);
	closedir(dir);

	return true;
}

void rpl_update(struct mesh_node *node, uint32_t cur)
{
	uint32_t old = cur - 1;
	const char *node_path;
	struct dirent *entry;
	char path[PATH_MAX];
	DIR *dir;

	node_path = node_get_storage_dir(node);
	if (!node_path)
		return;

	if (strlen(node_path) + strlen(rpl_dir) + 15 >= PATH_MAX)
		return;

	/* Make sure path exists */
	snprintf(path, PATH_MAX, "%s%s", node_path, rpl_dir);
	if (mkdir(path, 0755) != 0 && errno != EEXIST)
		l_error("Failed to create dir(%d): %s", errno, path);

	dir = opendir(path);
	if (!dir)
		return;

	/* Cleanup any stale or malformed trees */
	while ((entry = readdir(dir)) != NULL) {
		if (entry->d_type == DT_DIR && entry->d_name[0] != '.') {
			uint32_t val;
			bool del = false;

			if (strlen(entry->d_name) != 8)
				del = true;
			else if (sscanf(entry->d_name, "%08x", &val) != 1)
				del = true;

			/* Delete all invalid iv_index trees */
			if (del || (val != cur && val != old)) {
				snprintf(path, PATH_MAX, "%s%s/%s",
					node_path, rpl_dir, entry->d_name);
				del_path(path);
			}
		}
	}

	closedir(dir);
}

bool rpl_init(const char *node_path)
{
	char path[PATH_MAX];

	if (strlen(node_path) + strlen(rpl_dir) + 15 >= PATH_MAX)
		return false;

	snprintf(path, PATH_MAX, "%s%s", node_path, rpl_dir);
	if (mkdir(path, 0755) != 0 && errno != EEXIST)
		l_error("Failed to create dir(%d): %s", errno, path);
	return true;
}