1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2021 Felix Fietkau <nbd@nbd.name>
15 #include <libubox/uloop.h>
19 static int qosify_map_entry_cmp(const void *k1
, const void *k2
, void *ptr
);
21 static int qosify_map_fds
[__CL_MAP_MAX
];
22 static AVL_TREE(map_data
, qosify_map_entry_cmp
, false, NULL
);
23 static LIST_HEAD(map_files
);
24 static uint32_t next_timeout
;
25 static uint8_t qosify_dscp_default
[2] = { 0xff, 0xff };
26 int qosify_map_timeout
;
27 int qosify_active_timeout
;
28 struct qosify_config config
;
30 struct qosify_map_file
{
31 struct list_head list
;
37 const char *type_name
;
38 } qosify_map_info
[] = {
39 [CL_MAP_TCP_PORTS
] = { "tcp_ports", "tcp_port" },
40 [CL_MAP_UDP_PORTS
] = { "udp_ports", "udp_port" },
41 [CL_MAP_IPV4_ADDR
] = { "ipv4_map", "ipv4_addr" },
42 [CL_MAP_IPV6_ADDR
] = { "ipv6_map", "ipv6_addr" },
43 [CL_MAP_CONFIG
] = { "config", "config" },
44 [CL_MAP_DNS
] = { "dns", "dns" },
77 static void qosify_map_timer_cb(struct uloop_timeout
*t
)
82 static struct uloop_timeout qosify_map_timer
= {
83 .cb
= qosify_map_timer_cb
,
86 static uint32_t qosify_gettime(void)
90 clock_gettime(CLOCK_MONOTONIC
, &ts
);
96 qosify_map_path(enum qosify_map_id id
)
98 static char path
[128];
101 if (id
>= ARRAY_SIZE(qosify_map_info
))
104 name
= qosify_map_info
[id
].name
;
108 snprintf(path
, sizeof(path
), "%s/%s", CLASSIFY_DATA_PATH
, name
);
113 static int qosify_map_get_fd(enum qosify_map_id id
)
115 const char *path
= qosify_map_path(id
);
121 fd
= bpf_obj_get(path
);
123 fprintf(stderr
, "Failed to open map %s: %s\n", path
, strerror(errno
));
128 static void qosify_map_clear_list(enum qosify_map_id id
)
130 int fd
= qosify_map_fds
[id
];
133 while (bpf_map_get_next_key(fd
, &key
, &key
) != -1)
134 bpf_map_delete_elem(fd
, &key
);
137 static void __qosify_map_set_dscp_default(enum qosify_map_id id
, uint8_t val
)
139 struct qosify_map_data data
= {
142 int fd
= qosify_map_fds
[id
];
145 val
|= QOSIFY_DSCP_DEFAULT_FLAG
;
147 for (i
= 0; i
< (1 << 16); i
++) {
148 data
.addr
.port
= htons(i
);
149 if (avl_find(&map_data
, &data
))
152 bpf_map_update_elem(fd
, &data
.addr
, &val
, BPF_ANY
);
156 void qosify_map_set_dscp_default(enum qosify_map_id id
, uint8_t val
)
160 if (id
== CL_MAP_TCP_PORTS
)
162 else if (id
== CL_MAP_UDP_PORTS
)
167 if (qosify_dscp_default
[udp
] == val
)
170 qosify_dscp_default
[udp
] = val
;
171 __qosify_map_set_dscp_default(id
, val
);
174 int qosify_map_init(void)
178 for (i
= 0; i
< CL_MAP_DNS
; i
++) {
179 qosify_map_fds
[i
] = qosify_map_get_fd(i
);
180 if (qosify_map_fds
[i
] < 0)
184 qosify_map_clear_list(CL_MAP_IPV4_ADDR
);
185 qosify_map_clear_list(CL_MAP_IPV6_ADDR
);
186 qosify_map_reset_config();
191 static char *str_skip(char *str
, bool space
)
193 while (*str
&& isspace(*str
) == space
)
200 qosify_map_codepoint(const char *val
)
204 for (i
= 0; i
< ARRAY_SIZE(codepoints
); i
++)
205 if (!strcmp(codepoints
[i
].name
, val
))
206 return codepoints
[i
].val
;
211 static int qosify_map_entry_cmp(const void *k1
, const void *k2
, void *ptr
)
213 const struct qosify_map_data
*d1
= k1
;
214 const struct qosify_map_data
*d2
= k2
;
216 if (d1
->id
!= d2
->id
)
217 return d2
->id
- d1
->id
;
219 if (d1
->id
== CL_MAP_DNS
)
220 return strcmp(d1
->addr
.dns
.pattern
, d2
->addr
.dns
.pattern
);
222 return memcmp(&d1
->addr
, &d2
->addr
, sizeof(d1
->addr
));
225 static struct qosify_map_entry
*
226 __qosify_map_alloc_entry(struct qosify_map_data
*data
)
228 struct qosify_map_entry
*e
;
232 if (data
->id
< CL_MAP_DNS
) {
233 e
= calloc(1, sizeof(*e
));
234 memcpy(&e
->data
.addr
, &data
->addr
, sizeof(e
->data
.addr
));
239 e
= calloc_a(sizeof(*e
), &pattern
, strlen(data
->addr
.dns
.pattern
) + 1);
240 strcpy(pattern
, data
->addr
.dns
.pattern
);
241 e
->data
.addr
.dns
.pattern
= pattern
;
243 for (c
= pattern
; *c
; c
++)
246 if (pattern
[0] == '/' &&
247 regcomp(&e
->data
.addr
.dns
.regex
, pattern
+ 1,
248 REG_EXTENDED
| REG_NOSUB
)) {
256 static void __qosify_map_set_entry(struct qosify_map_data
*data
)
258 int fd
= qosify_map_fds
[data
->id
];
259 struct qosify_map_entry
*e
;
260 bool file
= data
->file
;
262 bool add
= data
->dscp
!= 0xff;
263 uint8_t prev_dscp
= 0xff;
265 e
= avl_find_element(&map_data
, data
, e
, avl
);
270 e
= __qosify_map_alloc_entry(data
);
274 e
->avl
.key
= &e
->data
;
275 e
->data
.id
= data
->id
;
276 avl_insert(&map_data
, &e
->avl
);
278 prev_dscp
= e
->data
.dscp
;
288 e
->data
.file_dscp
= data
->dscp
;
289 if (!e
->data
.user
|| !file
)
290 e
->data
.dscp
= data
->dscp
;
291 } else if (e
->data
.file
&& !file
) {
292 e
->data
.dscp
= e
->data
.file_dscp
;
295 if (e
->data
.dscp
!= prev_dscp
&& data
->id
< CL_MAP_DNS
) {
296 struct qosify_ip_map_val val
= {
297 .dscp
= e
->data
.dscp
,
301 bpf_map_update_elem(fd
, &data
->addr
, &val
, BPF_ANY
);
305 if (qosify_map_timeout
== ~0 || file
) {
310 e
->timeout
= qosify_gettime() + qosify_map_timeout
;
311 delta
= e
->timeout
- next_timeout
;
312 if (next_timeout
&& delta
>= 0)
316 uloop_timeout_set(&qosify_map_timer
, 1);
320 qosify_map_set_port(struct qosify_map_data
*data
, const char *str
)
322 unsigned long start_port
, end_port
;
326 start_port
= end_port
= strtoul(str
, &err
, 0);
329 end_port
= strtoul(err
+ 1, &err
, 0);
334 if (!start_port
|| end_port
< start_port
||
338 for (i
= start_port
; i
<= end_port
; i
++) {
339 data
->addr
.port
= htons(i
);
340 __qosify_map_set_entry(data
);
347 qosify_map_fill_ip(struct qosify_map_data
*data
, const char *str
)
351 if (data
->id
== CL_MAP_IPV6_ADDR
)
356 if (inet_pton(af
, str
, &data
->addr
) != 1)
362 int qosify_map_set_entry(enum qosify_map_id id
, bool file
, const char *str
, uint8_t dscp
)
364 struct qosify_map_data data
= {
372 data
.addr
.dns
.pattern
= str
;
374 case CL_MAP_TCP_PORTS
:
375 case CL_MAP_UDP_PORTS
:
376 return qosify_map_set_port(&data
, str
);
377 case CL_MAP_IPV4_ADDR
:
378 case CL_MAP_IPV6_ADDR
:
379 if (qosify_map_fill_ip(&data
, str
))
386 __qosify_map_set_entry(&data
);
391 int qosify_map_dscp_value(const char *val
)
395 bool fallback
= false;
402 dscp
= strtoul(val
, &err
, 0);
404 dscp
= qosify_map_codepoint(val
);
409 return dscp
+ (fallback
<< 6);
413 qosify_map_dscp_codepoint_str(char *dest
, int len
, uint8_t dscp
)
417 if (dscp
& QOSIFY_DSCP_FALLBACK_FLAG
) {
420 dscp
&= ~QOSIFY_DSCP_FALLBACK_FLAG
;
423 for (i
= 0; i
< ARRAY_SIZE(codepoints
); i
++) {
424 if (codepoints
[i
].val
!= dscp
)
427 snprintf(dest
, len
, "%s", codepoints
[i
].name
);
431 snprintf(dest
, len
, "0x%x", dscp
);
435 qosify_map_parse_line(char *str
)
437 const char *key
, *value
;
440 str
= str_skip(str
, true);
443 str
= str_skip(str
, false);
448 str
= str_skip(str
, true);
451 dscp
= qosify_map_dscp_value(value
);
455 if (!strncmp(key
, "dns:", 4))
456 qosify_map_set_entry(CL_MAP_DNS
, true, key
+ 4, dscp
);
457 if (!strncmp(key
, "tcp:", 4))
458 qosify_map_set_entry(CL_MAP_TCP_PORTS
, true, key
+ 4, dscp
);
459 else if (!strncmp(key
, "udp:", 4))
460 qosify_map_set_entry(CL_MAP_UDP_PORTS
, true, key
+ 4, dscp
);
461 else if (strchr(key
, ':'))
462 qosify_map_set_entry(CL_MAP_IPV6_ADDR
, true, key
, dscp
);
463 else if (strchr(key
, '.'))
464 qosify_map_set_entry(CL_MAP_IPV4_ADDR
, true, key
, dscp
);
468 __qosify_map_load_file_data(FILE *f
)
473 while (fgets(line
, sizeof(line
), f
)) {
474 cur
= strchr(line
, '#');
478 cur
= line
+ strlen(line
);
482 while (cur
> line
&& isspace(cur
[-1]))
486 qosify_map_parse_line(line
);
492 __qosify_map_load_file(const char *file
)
501 glob(file
, 0, NULL
, &gl
);
503 for (i
= 0; i
< gl
.gl_pathc
; i
++) {
504 f
= fopen(file
, "r");
508 __qosify_map_load_file_data(f
);
517 int qosify_map_load_file(const char *file
)
519 struct qosify_map_file
*f
;
524 f
= calloc(1, sizeof(*f
) + strlen(file
) + 1);
525 strcpy(f
->filename
, file
);
526 list_add_tail(&f
->list
, &map_files
);
528 return __qosify_map_load_file(file
);
531 static void qosify_map_reset_file_entries(void)
533 struct qosify_map_entry
*e
;
535 avl_for_each_element(&map_data
, e
, avl
)
536 e
->data
.file
= false;
539 void qosify_map_clear_files(void)
541 struct qosify_map_file
*f
, *tmp
;
543 qosify_map_reset_file_entries();
545 list_for_each_entry_safe(f
, tmp
, &map_files
, list
) {
551 void qosify_map_reset_config(void)
553 qosify_map_clear_files();
554 qosify_map_set_dscp_default(CL_MAP_TCP_PORTS
, 0);
555 qosify_map_set_dscp_default(CL_MAP_UDP_PORTS
, 0);
556 qosify_map_timeout
= 3600;
557 qosify_active_timeout
= 300;
559 memset(&config
, 0, sizeof(config
));
560 config
.dscp_prio
= 0xff;
561 config
.dscp_bulk
= 0xff;
562 config
.dscp_icmp
= 0xff;
565 void qosify_map_reload(void)
567 struct qosify_map_file
*f
;
569 qosify_map_reset_file_entries();
571 list_for_each_entry(f
, &map_files
, list
)
572 __qosify_map_load_file(f
->filename
);
577 static void qosify_map_free_entry(struct qosify_map_entry
*e
)
579 int fd
= qosify_map_fds
[e
->data
.id
];
581 avl_delete(&map_data
, &e
->avl
);
582 if (e
->data
.id
< CL_MAP_DNS
)
583 bpf_map_delete_elem(fd
, &e
->data
.addr
);
588 qosify_map_entry_refresh_timeout(struct qosify_map_entry
*e
)
590 struct qosify_ip_map_val val
;
591 int fd
= qosify_map_fds
[e
->data
.id
];
593 if (e
->data
.id
!= CL_MAP_IPV4_ADDR
&&
594 e
->data
.id
!= CL_MAP_IPV6_ADDR
)
597 if (bpf_map_lookup_elem(fd
, &e
->data
.addr
, &val
))
603 e
->timeout
= qosify_gettime() + qosify_active_timeout
;
605 bpf_map_update_elem(fd
, &e
->data
.addr
, &val
, BPF_ANY
);
610 void qosify_map_gc(void)
612 struct qosify_map_entry
*e
, *tmp
;
614 uint32_t cur_time
= qosify_gettime();
617 avl_for_each_element_safe(&map_data
, e
, avl
, tmp
) {
620 if (e
->data
.user
&& e
->timeout
!= ~0) {
621 cur_timeout
= e
->timeout
- cur_time
;
622 if (cur_timeout
<= 0 &&
623 qosify_map_entry_refresh_timeout(e
))
624 cur_timeout
= e
->timeout
- cur_time
;
625 if (cur_timeout
<= 0) {
626 e
->data
.user
= false;
627 e
->data
.dscp
= e
->data
.file_dscp
;
628 } else if (!timeout
|| cur_timeout
< timeout
) {
629 timeout
= cur_timeout
;
630 next_timeout
= e
->timeout
;
634 if (e
->data
.file
|| e
->data
.user
)
637 qosify_map_free_entry(e
);
643 uloop_timeout_set(&qosify_map_timer
, timeout
* 1000);
647 int qosify_map_add_dns_host(char *host
, const char *addr
, const char *type
, int ttl
)
649 struct qosify_map_data data
= {
651 .addr
.dns
.pattern
= "",
653 struct qosify_map_entry
*e
;
654 int prev_timeout
= qosify_map_timeout
;
657 e
= avl_find_ge_element(&map_data
, &data
, e
, avl
);
661 memset(&data
, 0, sizeof(data
));
663 if (!strcmp(type
, "A"))
664 data
.id
= CL_MAP_IPV4_ADDR
;
665 else if (!strcmp(type
, "AAAA"))
666 data
.id
= CL_MAP_IPV6_ADDR
;
670 if (qosify_map_fill_ip(&data
, addr
))
673 for (c
= host
; *c
; c
++)
676 avl_for_element_to_last(&map_data
, e
, e
, avl
) {
677 regex_t
*regex
= &e
->data
.addr
.dns
.regex
;
679 if (e
->data
.id
!= CL_MAP_DNS
)
682 if (e
->data
.addr
.dns
.pattern
[0] == '/') {
683 if (regexec(regex
, host
, 0, NULL
, 0) != 0)
686 if (fnmatch(e
->data
.addr
.dns
.pattern
, host
, 0))
691 qosify_map_timeout
= ttl
;
692 data
.dscp
= e
->data
.dscp
;
693 __qosify_map_set_entry(&data
);
694 qosify_map_timeout
= prev_timeout
;
701 void qosify_map_dump(struct blob_buf
*b
)
703 struct qosify_map_entry
*e
;
704 uint32_t cur_time
= qosify_gettime();
705 int buf_len
= INET6_ADDRSTRLEN
+ 1;
710 a
= blobmsg_open_array(b
, "entries");
711 avl_for_each_element(&map_data
, e
, avl
) {
714 if (!e
->data
.file
&& !e
->data
.user
)
717 c
= blobmsg_open_table(b
, NULL
);
718 if (e
->data
.user
&& e
->timeout
!= ~0) {
719 int32_t cur_timeout
= e
->timeout
- cur_time
;
724 blobmsg_add_u32(b
, "timeout", cur_timeout
);
727 blobmsg_add_u8(b
, "file", e
->data
.file
);
728 blobmsg_add_u8(b
, "user", e
->data
.user
);
730 buf
= blobmsg_alloc_string_buffer(b
, "dscp", buf_len
);
731 qosify_map_dscp_codepoint_str(buf
, buf_len
, e
->data
.dscp
);
732 blobmsg_add_string_buffer(b
);
734 blobmsg_add_string(b
, "type", qosify_map_info
[e
->data
.id
].type_name
);
736 switch (e
->data
.id
) {
737 case CL_MAP_TCP_PORTS
:
738 case CL_MAP_UDP_PORTS
:
739 blobmsg_printf(b
, "addr", "%d", ntohs(e
->data
.addr
.port
));
741 case CL_MAP_IPV4_ADDR
:
742 case CL_MAP_IPV6_ADDR
:
743 buf
= blobmsg_alloc_string_buffer(b
, "addr", buf_len
);
744 af
= e
->data
.id
== CL_MAP_IPV6_ADDR
? AF_INET6
: AF_INET
;
745 inet_ntop(af
, &e
->data
.addr
, buf
, buf_len
);
746 blobmsg_add_string_buffer(b
);
749 blobmsg_add_string(b
, "addr", e
->data
.addr
.dns
.pattern
);
755 blobmsg_close_table(b
, c
);
757 blobmsg_close_array(b
, a
);
760 void qosify_map_update_config(void)
762 int fd
= qosify_map_fds
[CL_MAP_CONFIG
];
765 bpf_map_update_elem(fd
, &key
, &config
, BPF_ANY
);