1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2021 Felix Fietkau <nbd@nbd.name>
13 #include <libubox/uloop.h>
17 static int qosify_map_entry_cmp(const void *k1
, const void *k2
, void *ptr
);
19 static int qosify_map_fds
[__CL_MAP_MAX
];
20 static AVL_TREE(map_data
, qosify_map_entry_cmp
, false, NULL
);
21 static LIST_HEAD(map_files
);
22 static uint32_t next_timeout
;
23 static uint8_t qosify_dscp_default
[2] = { 0xff, 0xff };
24 int qosify_map_timeout
= 3600;
25 struct qosify_config config
;
27 struct qosify_map_file
{
28 struct list_head list
;
34 const char *type_name
;
35 } qosify_map_info
[] = {
36 [CL_MAP_TCP_PORTS
] = { "tcp_ports", "tcp_port" },
37 [CL_MAP_UDP_PORTS
] = { "udp_ports", "udp_port" },
38 [CL_MAP_IPV4_ADDR
] = { "ipv4_map", "ipv4_addr" },
39 [CL_MAP_IPV6_ADDR
] = { "ipv6_map", "ipv6_addr" },
40 [CL_MAP_CONFIG
] = { "config", "config" },
41 [CL_MAP_DNS
] = { "dns", "dns" },
73 static void qosify_map_timer_cb(struct uloop_timeout
*t
)
78 static struct uloop_timeout qosify_map_timer
= {
79 .cb
= qosify_map_timer_cb
,
82 static uint32_t qosify_gettime(void)
86 clock_gettime(CLOCK_MONOTONIC
, &ts
);
92 qosify_map_path(enum qosify_map_id id
)
94 static char path
[128];
97 if (id
>= ARRAY_SIZE(qosify_map_info
))
100 name
= qosify_map_info
[id
].name
;
104 snprintf(path
, sizeof(path
), "%s/%s", CLASSIFY_DATA_PATH
, name
);
109 static int qosify_map_get_fd(enum qosify_map_id id
)
111 const char *path
= qosify_map_path(id
);
117 fd
= bpf_obj_get(path
);
119 fprintf(stderr
, "Failed to open map %s: %s\n", path
, strerror(errno
));
124 static void qosify_map_clear_list(enum qosify_map_id id
)
126 int fd
= qosify_map_fds
[id
];
129 while (bpf_map_get_next_key(fd
, &key
, &key
) != -1)
130 bpf_map_delete_elem(fd
, &key
);
133 static void __qosify_map_set_dscp_default(enum qosify_map_id id
, uint8_t val
)
135 struct qosify_map_data data
= {
138 int fd
= qosify_map_fds
[id
];
141 val
|= QOSIFY_DSCP_DEFAULT_FLAG
;
143 for (i
= 0; i
< (1 << 16); i
++) {
144 data
.addr
.port
= htons(i
);
145 if (avl_find(&map_data
, &data
))
148 bpf_map_update_elem(fd
, &data
.addr
, &val
, BPF_ANY
);
152 void qosify_map_set_dscp_default(enum qosify_map_id id
, uint8_t val
)
156 if (id
== CL_MAP_TCP_PORTS
)
158 else if (id
== CL_MAP_UDP_PORTS
)
163 if (qosify_dscp_default
[udp
] == val
)
166 qosify_dscp_default
[udp
] = val
;
167 __qosify_map_set_dscp_default(id
, val
);
170 int qosify_map_init(void)
174 for (i
= 0; i
< CL_MAP_DNS
; i
++) {
175 qosify_map_fds
[i
] = qosify_map_get_fd(i
);
176 if (qosify_map_fds
[i
] < 0)
180 qosify_map_clear_list(CL_MAP_IPV4_ADDR
);
181 qosify_map_clear_list(CL_MAP_IPV6_ADDR
);
182 qosify_map_reset_config();
187 static char *str_skip(char *str
, bool space
)
189 while (*str
&& isspace(*str
) == space
)
196 qosify_map_codepoint(const char *val
)
200 for (i
= 0; i
< ARRAY_SIZE(codepoints
); i
++)
201 if (!strcmp(codepoints
[i
].name
, val
))
202 return codepoints
[i
].val
;
207 static int qosify_map_entry_cmp(const void *k1
, const void *k2
, void *ptr
)
209 const struct qosify_map_data
*d1
= k1
;
210 const struct qosify_map_data
*d2
= k2
;
212 if (d1
->id
!= d2
->id
)
213 return d2
->id
- d1
->id
;
215 if (d1
->id
== CL_MAP_DNS
)
216 return strcmp(d1
->addr
.dns
.pattern
, d2
->addr
.dns
.pattern
);
218 return memcmp(&d1
->addr
, &d2
->addr
, sizeof(d1
->addr
));
221 static struct qosify_map_entry
*
222 __qosify_map_alloc_entry(struct qosify_map_data
*data
)
224 struct qosify_map_entry
*e
;
227 if (data
->id
< CL_MAP_DNS
) {
228 e
= calloc(1, sizeof(*e
));
229 memcpy(&e
->data
.addr
, &data
->addr
, sizeof(e
->data
.addr
));
234 e
= calloc_a(sizeof(*e
), &pattern
, strlen(data
->addr
.dns
.pattern
) + 1);
235 strcpy(pattern
, data
->addr
.dns
.pattern
);
236 e
->data
.addr
.dns
.pattern
= pattern
;
237 if (regcomp(&e
->data
.addr
.dns
.regex
, pattern
,
238 REG_EXTENDED
| REG_ICASE
| REG_NOSUB
)) {
246 static void __qosify_map_set_entry(struct qosify_map_data
*data
)
248 int fd
= qosify_map_fds
[data
->id
];
249 struct qosify_map_entry
*e
;
250 bool file
= data
->file
;
252 bool add
= data
->dscp
!= 0xff;
253 uint8_t prev_dscp
= 0xff;
255 e
= avl_find_element(&map_data
, data
, e
, avl
);
260 e
= __qosify_map_alloc_entry(data
);
264 e
->avl
.key
= &e
->data
;
265 e
->data
.id
= data
->id
;
266 avl_insert(&map_data
, &e
->avl
);
268 prev_dscp
= e
->data
.dscp
;
278 e
->data
.file_dscp
= data
->dscp
;
279 if (!e
->data
.user
|| !file
)
280 e
->data
.dscp
= data
->dscp
;
281 } else if (e
->data
.file
&& !file
) {
282 e
->data
.dscp
= e
->data
.file_dscp
;
285 if (e
->data
.dscp
!= prev_dscp
&& data
->id
< CL_MAP_DNS
)
286 bpf_map_update_elem(fd
, &data
->addr
, &e
->data
.dscp
, BPF_ANY
);
289 if (qosify_map_timeout
== ~0 || file
) {
294 e
->timeout
= qosify_gettime() + qosify_map_timeout
;
295 delta
= e
->timeout
- next_timeout
;
296 if (next_timeout
&& delta
>= 0)
300 uloop_timeout_set(&qosify_map_timer
, 1);
304 qosify_map_set_port(struct qosify_map_data
*data
, const char *str
)
306 unsigned long start_port
, end_port
;
310 start_port
= end_port
= strtoul(str
, &err
, 0);
313 end_port
= strtoul(err
+ 1, &err
, 0);
318 if (!start_port
|| end_port
< start_port
||
322 for (i
= start_port
; i
<= end_port
; i
++) {
323 data
->addr
.port
= htons(i
);
324 __qosify_map_set_entry(data
);
331 qosify_map_fill_ip(struct qosify_map_data
*data
, const char *str
)
335 if (data
->id
== CL_MAP_IPV6_ADDR
)
340 if (inet_pton(af
, str
, &data
->addr
) != 1)
346 int qosify_map_set_entry(enum qosify_map_id id
, bool file
, const char *str
, uint8_t dscp
)
348 struct qosify_map_data data
= {
356 data
.addr
.dns
.pattern
= str
;
358 case CL_MAP_TCP_PORTS
:
359 case CL_MAP_UDP_PORTS
:
360 return qosify_map_set_port(&data
, str
);
361 case CL_MAP_IPV4_ADDR
:
362 case CL_MAP_IPV6_ADDR
:
363 if (qosify_map_fill_ip(&data
, str
))
370 __qosify_map_set_entry(&data
);
375 int qosify_map_dscp_value(const char *val
)
379 bool fallback
= false;
386 dscp
= strtoul(val
, &err
, 0);
388 dscp
= qosify_map_codepoint(val
);
393 return dscp
+ (fallback
<< 6);
397 qosify_map_dscp_codepoint_str(char *dest
, int len
, uint8_t dscp
)
401 if (dscp
& QOSIFY_DSCP_FALLBACK_FLAG
) {
404 dscp
&= ~QOSIFY_DSCP_FALLBACK_FLAG
;
407 for (i
= 0; i
< ARRAY_SIZE(codepoints
); i
++) {
408 if (codepoints
[i
].val
!= dscp
)
411 snprintf(dest
, len
, "%s", codepoints
[i
].name
);
415 snprintf(dest
, len
, "0x%x", dscp
);
419 qosify_map_parse_line(char *str
)
421 const char *key
, *value
;
424 str
= str_skip(str
, true);
427 str
= str_skip(str
, false);
432 str
= str_skip(str
, true);
435 dscp
= qosify_map_dscp_value(value
);
439 if (!strncmp(key
, "dns:", 4))
440 qosify_map_set_entry(CL_MAP_DNS
, true, key
+ 4, dscp
);
441 if (!strncmp(key
, "tcp:", 4))
442 qosify_map_set_entry(CL_MAP_TCP_PORTS
, true, key
+ 4, dscp
);
443 else if (!strncmp(key
, "udp:", 4))
444 qosify_map_set_entry(CL_MAP_UDP_PORTS
, true, key
+ 4, dscp
);
445 else if (strchr(key
, ':'))
446 qosify_map_set_entry(CL_MAP_IPV6_ADDR
, true, key
, dscp
);
447 else if (strchr(key
, '.'))
448 qosify_map_set_entry(CL_MAP_IPV4_ADDR
, true, key
, dscp
);
451 static int __qosify_map_load_file(const char *file
)
460 f
= fopen(file
, "r");
462 fprintf(stderr
, "Can't open data file %s\n", file
);
466 while (fgets(line
, sizeof(line
), f
)) {
467 cur
= strchr(line
, '#');
471 cur
= line
+ strlen(line
);
475 while (cur
> line
&& isspace(cur
[-1]))
479 qosify_map_parse_line(line
);
487 int qosify_map_load_file(const char *file
)
489 struct qosify_map_file
*f
;
494 f
= calloc(1, sizeof(*f
) + strlen(file
) + 1);
495 strcpy(f
->filename
, file
);
496 list_add_tail(&f
->list
, &map_files
);
498 return __qosify_map_load_file(file
);
501 static void qosify_map_reset_file_entries(void)
503 struct qosify_map_entry
*e
;
505 avl_for_each_element(&map_data
, e
, avl
)
506 e
->data
.file
= false;
509 void qosify_map_clear_files(void)
511 struct qosify_map_file
*f
, *tmp
;
513 qosify_map_reset_file_entries();
515 list_for_each_entry_safe(f
, tmp
, &map_files
, list
) {
521 void qosify_map_reset_config(void)
523 qosify_map_clear_files();
524 qosify_map_set_dscp_default(CL_MAP_TCP_PORTS
, 0);
525 qosify_map_set_dscp_default(CL_MAP_UDP_PORTS
, 0);
526 qosify_map_timeout
= 3600;
528 memset(&config
, 0, sizeof(config
));
529 config
.dscp_prio
= 0xff;
530 config
.dscp_bulk
= 0xff;
531 config
.dscp_icmp
= 0xff;
534 void qosify_map_reload(void)
536 struct qosify_map_file
*f
;
538 qosify_map_reset_file_entries();
540 list_for_each_entry(f
, &map_files
, list
)
541 __qosify_map_load_file(f
->filename
);
546 static void qosify_map_free_entry(struct qosify_map_entry
*e
)
548 int fd
= qosify_map_fds
[e
->data
.id
];
550 avl_delete(&map_data
, &e
->avl
);
551 if (e
->data
.id
< CL_MAP_DNS
)
552 bpf_map_delete_elem(fd
, &e
->data
.addr
);
556 void qosify_map_gc(void)
558 struct qosify_map_entry
*e
, *tmp
;
560 uint32_t cur_time
= qosify_gettime();
563 avl_for_each_element_safe(&map_data
, e
, avl
, tmp
) {
566 if (e
->data
.user
&& e
->timeout
!= ~0) {
567 cur_timeout
= e
->timeout
- cur_time
;
568 if (cur_timeout
<= 0) {
569 e
->data
.user
= false;
570 e
->data
.dscp
= e
->data
.file_dscp
;
571 } else if (!timeout
|| cur_timeout
< timeout
) {
572 timeout
= cur_timeout
;
573 next_timeout
= e
->timeout
;
577 if (e
->data
.file
|| e
->data
.user
)
580 qosify_map_free_entry(e
);
586 uloop_timeout_set(&qosify_map_timer
, timeout
* 1000);
590 int qosify_map_add_dns_host(const char *host
, const char *addr
, const char *type
, int ttl
)
592 struct qosify_map_data data
= {
594 .addr
.dns
.pattern
= "",
596 struct qosify_map_entry
*e
;
597 int prev_timeout
= qosify_map_timeout
;
599 e
= avl_find_ge_element(&map_data
, &data
, e
, avl
);
603 memset(&data
, 0, sizeof(data
));
605 if (!strcmp(type
, "A"))
606 data
.id
= CL_MAP_IPV4_ADDR
;
607 else if (!strcmp(type
, "AAAA"))
608 data
.id
= CL_MAP_IPV6_ADDR
;
612 if (qosify_map_fill_ip(&data
, addr
))
615 avl_for_element_to_last(&map_data
, e
, e
, avl
) {
616 regex_t
*regex
= &e
->data
.addr
.dns
.regex
;
618 if (e
->data
.id
!= CL_MAP_DNS
)
621 if (regexec(regex
, host
, 0, NULL
, 0) != 0)
625 qosify_map_timeout
= ttl
;
626 data
.dscp
= e
->data
.dscp
;
627 __qosify_map_set_entry(&data
);
628 qosify_map_timeout
= prev_timeout
;
635 void qosify_map_dump(struct blob_buf
*b
)
637 struct qosify_map_entry
*e
;
638 uint32_t cur_time
= qosify_gettime();
639 int buf_len
= INET6_ADDRSTRLEN
+ 1;
644 a
= blobmsg_open_array(b
, "entries");
645 avl_for_each_element(&map_data
, e
, avl
) {
648 if (!e
->data
.file
&& !e
->data
.user
)
651 c
= blobmsg_open_table(b
, NULL
);
652 if (e
->data
.user
&& e
->timeout
!= ~0) {
653 int32_t cur_timeout
= e
->timeout
- cur_time
;
658 blobmsg_add_u32(b
, "timeout", cur_timeout
);
661 blobmsg_add_u8(b
, "file", e
->data
.file
);
662 blobmsg_add_u8(b
, "user", e
->data
.user
);
664 buf
= blobmsg_alloc_string_buffer(b
, "dscp", buf_len
);
665 qosify_map_dscp_codepoint_str(buf
, buf_len
, e
->data
.dscp
);
666 blobmsg_add_string_buffer(b
);
668 blobmsg_add_string(b
, "type", qosify_map_info
[e
->data
.id
].type_name
);
670 switch (e
->data
.id
) {
671 case CL_MAP_TCP_PORTS
:
672 case CL_MAP_UDP_PORTS
:
673 blobmsg_printf(b
, "addr", "%d", ntohs(e
->data
.addr
.port
));
675 case CL_MAP_IPV4_ADDR
:
676 case CL_MAP_IPV6_ADDR
:
677 buf
= blobmsg_alloc_string_buffer(b
, "addr", buf_len
);
678 af
= e
->data
.id
== CL_MAP_IPV6_ADDR
? AF_INET6
: AF_INET
;
679 inet_ntop(af
, &e
->data
.addr
, buf
, buf_len
);
680 blobmsg_add_string_buffer(b
);
683 blobmsg_add_string(b
, "addr", e
->data
.addr
.dns
.pattern
);
689 blobmsg_close_table(b
, c
);
691 blobmsg_close_array(b
, a
);
694 void qosify_map_update_config(void)
696 int fd
= qosify_map_fds
[CL_MAP_CONFIG
];
699 bpf_map_update_elem(fd
, &key
, &config
, BPF_ANY
);