summaryrefslogtreecommitdiff
path: root/lib/route/classid.c
blob: 6af0ee37039001da045ea36e970ac66e192c1258 (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
/*
 * lib/route/classid.c       ClassID Management
 *
 *	This library is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU Lesser General Public
 *	License as published by the Free Software Foundation version 2.1
 *	of the License.
 *
 * Copyright (c) 2010 Thomas Graf <tgraf@suug.ch>
 */

/**
 * @ingroup tc
 * @defgroup classid ClassID Management
 * @{
 */

#include <netlink-local.h>
#include <netlink-tc.h>
#include <netlink/netlink.h>
#include <netlink/utils.h>
#include <netlink/route/tc.h>

struct classid_map
{
	uint32_t		classid;
	char *			name;
	struct nl_list_head	name_list;
};

#define CLASSID_NAME_HT_SIZ 256

static struct nl_list_head tbl_name[CLASSID_NAME_HT_SIZ];

static void *id_root = NULL;

static int compare_id(const void *pa, const void *pb)
{
	const struct classid_map *ma = pa;
	const struct classid_map *mb = pb;

	if (ma->classid < mb->classid)
		return -1;
	
	if (ma->classid > mb->classid)
		return 1;

	return 0;
}

/* djb2 */
static unsigned int classid_tbl_hash(const char *str)
{
	unsigned long hash = 5381;
	int c;

	while ((c = *str++))
		hash = ((hash << 5) + hash) + c; /* hash * 33 + c */

	return hash % CLASSID_NAME_HT_SIZ;
}

static int classid_lookup(const char *name, uint32_t *result)
{
	struct classid_map *map;
	int n = classid_tbl_hash(name);

	nl_list_for_each_entry(map, &tbl_name[n], name_list) {
		if (!strcasecmp(map->name, name)) {
			*result = map->classid;
			return 0;
		}
	}

	return -NLE_OBJ_NOTFOUND;
}

static char *name_lookup(const uint32_t classid)
{
	void *res;
	struct classid_map cm = {
		.classid = classid,
		.name = "search entry",
	};

	if ((res = tfind(&cm, &id_root, &compare_id)))
		return (*(struct classid_map **) res)->name;

	return NULL;
}

/**
 * @name Traffic Control Handle Translations
 * @{
 */

/**
 * Convert a traffic control handle to a character string (Reentrant).
 * @arg handle		traffic control handle
 * @arg buf		destination buffer
 * @arg len		buffer length
 *
 * Converts a tarffic control handle to a character string in the
 * form of \c MAJ:MIN and stores it in the specified destination buffer.
 *
 * @return The destination buffer or the type encoded in hexidecimal
 *         form if no match was found.
 */
char * rtnl_tc_handle2str(uint32_t handle, char *buf, size_t len)
{
	if (TC_H_ROOT == handle)
		snprintf(buf, len, "root");
	else if (TC_H_UNSPEC == handle)
		snprintf(buf, len, "none");
	else if (TC_H_INGRESS == handle)
		snprintf(buf, len, "ingress");
	else {
		char *name;

		if ((name = name_lookup(handle)))
			snprintf(buf, len, "%s", name);
		else if (0 == TC_H_MAJ(handle))
			snprintf(buf, len, ":%02x", TC_H_MIN(handle));
		else if (0 == TC_H_MIN(handle))
			snprintf(buf, len, "%02x:", TC_H_MAJ(handle) >> 16);
		else
			snprintf(buf, len, "%02x:%02x",
				TC_H_MAJ(handle) >> 16, TC_H_MIN(handle));
	}

	return buf;
}

/**
 * Convert a charactering strint to a traffic control handle
 * @arg str		traffic control handle as character string
 * @arg res		destination buffer
 *
 * Converts the provided character string specifying a traffic
 * control handle to the corresponding numeric value.
 *
 * The handle must be provided in one of the following formats:
 *  - NAME
 *  - root
 *  - none
 *  - MAJ:
 *  - :MIN
 *  - NAME:MIN
 *  - MAJ:MIN
 *  - MAJMIN
 *
 * @return 0 on success or a negative error code
 */
int rtnl_tc_str2handle(const char *str, uint32_t *res)
{
	char *colon, *end;
	uint32_t h, err;

	if (!strcasecmp(str, "root")) {
		*res = TC_H_ROOT;
		return 0;
	}

	if (!strcasecmp(str, "none")) {
		*res = TC_H_UNSPEC;
		return 0;
	}

	h = strtoul(str, &colon, 16);

	/* MAJ is not a number */
	if (colon == str) {
not_a_number:
		if (*colon == ':') {
			/* :YYYY */
			h = 0;
		} else {
			size_t len;
			char name[64] = { 0 };

			if (!(colon = strpbrk(str, ":"))) {
				/* NAME */
				return classid_lookup(str, res);
			} else {
				/* NAME:YYYY */
				len = colon - str;
				if (len >= sizeof(name))
					return -NLE_INVAL;

				memcpy(name, str, len);

				if ((err = classid_lookup(name, &h)) < 0)
					return err;

				/* Name must point to a qdisc alias */
				if (TC_H_MIN(h))
					return -NLE_INVAL;

				/* NAME: is not allowed */
				if (colon[1] == '\0')
					return -NLE_INVAL;

				goto update;
			}
		}
	}

	if (':' == *colon) {
		/* check if we would lose bits */
		if (TC_H_MAJ(h))
			return -NLE_RANGE;
		h <<= 16;

		if ('\0' == colon[1]) {
			/* XXXX: */
			*res = h;
		} else {
			/* XXXX:YYYY */
			uint32_t l;
			
update:
			l = strtoul(colon+1, &end, 16);

			/* check if we overlap with major part */
			if (TC_H_MAJ(l))
				return -NLE_RANGE;

			if ('\0' != *end)
				return -NLE_INVAL;

			*res = (h | l);
		}
	} else if ('\0' == *colon) {
		/* XXXXYYYY */
		*res = h;
	} else
		goto not_a_number;

	return 0;
}

static void free_nothing(void *arg)
{
}

static void classid_map_free(struct classid_map *map)
{
	if (!map)
		return;

	free(map->name);
	free(map);
}

static void clear_hashtable(void)
{
	int i;

	for (i = 0; i < CLASSID_NAME_HT_SIZ; i++) {
		struct classid_map *map, *n;

		nl_list_for_each_entry_safe(map, n, &tbl_name[i], name_list)
			classid_map_free(map);

		nl_init_list_head(&tbl_name[i]);

	}

	if (id_root) {
		tdestroy(&id_root, &free_nothing);
		id_root = NULL;
	}
}

static int classid_map_add(uint32_t classid, const char *name)
{
	struct classid_map *map;
	int n;

	if (!(map = calloc(1, sizeof(*map))))
		return -NLE_NOMEM;

	map->classid = classid;
	map->name = strdup(name);

	n = classid_tbl_hash(map->name);
	nl_list_add_tail(&map->name_list, &tbl_name[n]);

	if (!tsearch((void *) map, &id_root, &compare_id)) {
		classid_map_free(map);
		return -NLE_NOMEM;
	}

	return 0;
}

/**
 * (Re-)read classid file
 * 
 * Rereads the contents of the classid file (typically found at the location
 * /etc/libnl/classid) and refreshes the classid maps.
 *
 * @return 0 on success or a negative error code.
 */
int rtnl_tc_read_classid_file(void)
{
	static time_t last_read;
	struct stat st = {0};
	char buf[256], *path;
	FILE *fd;
	int err;

	if (build_sysconf_path(&path, "classid") < 0)
		return -NLE_NOMEM;

	/* if stat fails, just (re-)read the file */
	if (stat(path, &st) == 0) {
		/* Don't re-read file if file is unchanged */
		if (last_read == st.st_mtime) {
			err = 0;
			goto errout;
		}
	}

	if (!(fd = fopen(path, "r"))) {
		err = -nl_syserr2nlerr(errno);
		goto errout;
	}

	clear_hashtable();

	while (fgets(buf, sizeof(buf), fd)) {
		uint32_t classid;
		char *ptr, *tok;

		/* ignore comments and empty lines */
		if (*buf == '#' || *buf == '\n' || *buf == '\r')
			continue;

		/* token 1 */
		if (!(tok = strtok_r(buf, " \t", &ptr))) {
			err = -NLE_INVAL;
			goto errout_close;
		}

		if ((err = rtnl_tc_str2handle(tok, &classid)) < 0)
			goto errout_close;

		if (!(tok = strtok_r(NULL, " \t\n\r#", &ptr))) {
			err = -NLE_INVAL;
			goto errout_close;
		}

		if ((err = classid_map_add(classid, tok)) < 0)
			goto errout_close;
	}

	err = 0;
	last_read = st.st_mtime;

errout_close:
	fclose(fd);
errout:
	free(path);

	return err;

}

int rtnl_classid_generate(const char *name, uint32_t *result, uint32_t parent)
{
	static uint32_t base = 0x4000 << 16;
	uint32_t classid;
	char *path;
	FILE *fd;
	int err = 0;

	if (parent == TC_H_ROOT || parent == TC_H_INGRESS) {
		do {
			base += (1 << 16);
			if (base == TC_H_MAJ(TC_H_ROOT))
				base = 0x4000 << 16;
		} while (name_lookup(base));

		classid = base;
	} else {
		classid = TC_H_MAJ(parent);
		do {
			if (TC_H_MIN(++classid) == TC_H_MIN(TC_H_ROOT))
				return -NLE_RANGE;
		} while (name_lookup(classid));
	}

	NL_DBG(2, "Generated new classid %#x\n", classid);

	if (build_sysconf_path(&path, "classid") < 0)
		return -NLE_NOMEM;

	if (!(fd = fopen(path, "a"))) {
		err = -nl_syserr2nlerr(errno);
		goto errout;
	}

	fprintf(fd, "%x:", TC_H_MAJ(classid) >> 16);
	if (TC_H_MIN(classid))
		fprintf(fd, "%x", TC_H_MIN(classid));
	fprintf(fd, "\t\t\t%s\n", name);

	fclose(fd);

	if ((err = classid_map_add(classid, name)) < 0) {
		/* 
		 * Error adding classid map, re-read classid file is best
		 * option here. It is likely to fail as well but better
		 * than nothing, entry was added to the file already anyway.
		 */
		rtnl_tc_read_classid_file();
	}

	*result = classid;
	err = 0;
errout:
	free(path);

	return err;
}

/** @} */

static void __init classid_init(void)
{
	int err, i;

	for (i = 0; i < CLASSID_NAME_HT_SIZ; i++)
		nl_init_list_head(&tbl_name[i]);

	if ((err = rtnl_tc_read_classid_file()) < 0)
		fprintf(stderr, "Failed to read classid file: %s\n", nl_geterror(err));
}

/** @} */