dns: allow limiting dns entry matching to cname name
[project/qosify.git] / qosify.h
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (C) 2021 Felix Fietkau <nbd@nbd.name>
4 */
5 #ifndef __QOS_CLASSIFY_H
6 #define __QOS_CLASSIFY_H
7
8 #include <stdbool.h>
9 #include <regex.h>
10
11 #include <bpf/bpf.h>
12 #include <bpf/libbpf.h>
13
14 #include "qosify-bpf.h"
15
16 #include <libubox/utils.h>
17 #include <libubox/avl.h>
18 #include <libubox/blobmsg.h>
19 #include <libubox/ulog.h>
20
21 #include <netinet/in.h>
22
23 #define CLASSIFY_PROG_PATH "/lib/bpf/qosify-bpf.o"
24 #define CLASSIFY_PIN_PATH "/sys/fs/bpf/qosify"
25 #define CLASSIFY_DATA_PATH "/sys/fs/bpf/qosify_data"
26
27 #define QOSIFY_DNS_IFNAME "ifb-dns"
28
29 enum qosify_map_id {
30 CL_MAP_TCP_PORTS,
31 CL_MAP_UDP_PORTS,
32 CL_MAP_IPV4_ADDR,
33 CL_MAP_IPV6_ADDR,
34 CL_MAP_CLASS,
35 CL_MAP_CONFIG,
36 CL_MAP_DNS,
37 __CL_MAP_MAX,
38 };
39
40 struct qosify_map_data {
41 enum qosify_map_id id;
42
43 bool file : 1;
44 bool user : 1;
45
46 uint8_t dscp;
47 uint8_t file_dscp;
48
49 union {
50 uint32_t port;
51 struct in_addr ip;
52 struct in6_addr ip6;
53 struct {
54 uint32_t seq : 30;
55 uint32_t only_cname : 1;
56 const char *pattern;
57 regex_t regex;
58 } dns;
59 } addr;
60 };
61
62 struct qosify_map_entry {
63 struct avl_node avl;
64
65 uint32_t timeout;
66
67 struct qosify_map_data data;
68 };
69
70
71 extern int qosify_map_timeout;
72 extern int qosify_active_timeout;
73 extern struct qosify_config config;
74 extern struct qosify_flow_config flow_config;
75
76 int qosify_run_cmd(char *cmd, bool ignore_error);
77
78 int qosify_loader_init(void);
79
80 int qosify_map_init(void);
81 int qosify_map_dscp_value(const char *val, uint8_t *dscp);
82 int qosify_map_load_file(const char *file);
83 void __qosify_map_set_entry(struct qosify_map_data *data);
84 int qosify_map_set_entry(enum qosify_map_id id, bool file, const char *str,
85 uint8_t dscp);
86 void qosify_map_reload(void);
87 void qosify_map_clear_files(void);
88 void qosify_map_gc(void);
89 void qosify_map_dump(struct blob_buf *b);
90 void qosify_map_set_dscp_default(enum qosify_map_id id, uint8_t val);
91 void qosify_map_reset_config(void);
92 void qosify_map_update_config(void);
93 void qosify_map_set_classes(struct blob_attr *val);
94 int qosify_map_lookup_dns_entry(char *host, bool cname, uint8_t *dscp, uint32_t *seq);
95 int qosify_map_add_dns_host(char *host, const char *addr, const char *type, int ttl);
96 int map_parse_flow_config(struct qosify_flow_config *cfg, struct blob_attr *attr,
97 bool reset);
98 int map_fill_dscp_value(uint8_t *dest, struct blob_attr *attr, bool reset);
99
100 int qosify_iface_init(void);
101 void qosify_iface_config_update(struct blob_attr *ifaces, struct blob_attr *devs);
102 void qosify_iface_check(void);
103 void qosify_iface_status(struct blob_buf *b);
104 void qosify_iface_stop(void);
105
106 int qosify_dns_init(void);
107 void qosify_dns_stop(void);
108
109 int qosify_ubus_init(void);
110 void qosify_ubus_stop(void);
111 int qosify_ubus_check_interface(const char *name, char *ifname, int ifname_len);
112
113 #endif