506a413ae2e0c4db94b0a7776b5a6499a7232006
[openwrt/staging/jow.git] / package / network / services / dnsmasq / patches / 200-ubus_dns.patch
1 --- a/src/dnsmasq.h
2 +++ b/src/dnsmasq.h
3 @@ -1564,14 +1564,26 @@ void emit_dbus_signal(int action, struct
4
5 /* ubus.c */
6 #ifdef HAVE_UBUS
7 +struct blob_attr;
8 +typedef void (*ubus_dns_notify_cb)(struct blob_attr *msg, void *priv);
9 +
10 char *ubus_init(void);
11 void set_ubus_listeners(void);
12 void check_ubus_listeners(void);
13 +void drop_ubus_listeners(void);
14 +struct blob_buf *ubus_dns_notify_prepare(void);
15 +int ubus_dns_notify(const char *type, ubus_dns_notify_cb cb, void *priv);
16 void ubus_event_bcast(const char *type, const char *mac, const char *ip, const char *name, const char *interface);
17 # ifdef HAVE_CONNTRACK
18 void ubus_event_bcast_connmark_allowlist_refused(u32 mark, const char *name);
19 void ubus_event_bcast_connmark_allowlist_resolved(u32 mark, const char *pattern, const char *ip, u32 ttl);
20 # endif
21 +#else
22 +struct blob_buf;
23 +static inline struct blob_buf *ubus_dns_notify_prepare(void)
24 +{
25 + return NULL;
26 +}
27 #endif
28
29 /* ipset.c */
30 --- a/src/rfc1035.c
31 +++ b/src/rfc1035.c
32 @@ -13,8 +13,10 @@
33 You should have received a copy of the GNU General Public License
34 along with this program. If not, see <http://www.gnu.org/licenses/>.
35 */
36 -
37 #include "dnsmasq.h"
38 +#ifdef HAVE_UBUS
39 +#include <libubox/blobmsg.h>
40 +#endif
41
42 int extract_name(struct dns_header *header, size_t plen, unsigned char **pp,
43 char *name, int isExtract, int extrabytes)
44 @@ -394,9 +396,64 @@ static int private_net6(struct in6_addr
45 ((u32 *)a)[0] == htonl(0x20010db8); /* RFC 6303 4.6 */
46 }
47
48 +#ifdef HAVE_UBUS
49 +static void ubus_dns_doctor_cb(struct blob_attr *msg, void *priv)
50 +{
51 + static const struct blobmsg_policy policy = {
52 + .name = "address",
53 + .type = BLOBMSG_TYPE_STRING,
54 + };
55 + struct blob_attr *val;
56 + char **dest = priv;
57 +
58 + blobmsg_parse(&policy, 1, &val, blobmsg_data(msg), blobmsg_data_len(msg));
59 + if (val)
60 + *dest = blobmsg_get_string(val);
61 +}
62 +
63 +static bool ubus_dns_doctor(const char *name, int ttl, void *p, int af)
64 +{
65 + struct blob_buf *b;
66 + char *addr;
67 +
68 + if (!name)
69 + return false;
70 +
71 + b = ubus_dns_notify_prepare();
72 + if (!b)
73 + return false;
74 +
75 + blobmsg_add_string(b, "name", name);
76 +
77 + blobmsg_add_u32(b, "ttl", ttl);
78 +
79 + blobmsg_add_string(b, "type", af == AF_INET6 ? "AAAA" : "A");
80 +
81 + addr = blobmsg_alloc_string_buffer(b, "address", INET6_ADDRSTRLEN);
82 + if (!addr)
83 + return false;
84 +
85 + inet_ntop(af, p, addr, INET6_ADDRSTRLEN);
86 + blobmsg_add_string_buffer(b);
87 +
88 + addr = NULL;
89 + ubus_dns_notify("dns_result", ubus_dns_doctor_cb, &addr);
90 +
91 + if (!addr)
92 + return false;
93 +
94 + return inet_pton(af, addr, p) == 1;
95 +}
96 +#else
97 +static bool ubus_dns_doctor(const char *name, int ttl, void *p, int af)
98 +{
99 + return false;
100 +}
101 +#endif
102 +
103 static unsigned char *do_doctor(unsigned char *p, int count, struct dns_header *header, size_t qlen, int *doctored)
104 {
105 - int i, qtype, qclass, rdlen;
106 + int i, qtype, qclass, rdlen, ttl;
107
108 for (i = count; i != 0; i--)
109 {
110 @@ -405,7 +462,7 @@ static unsigned char *do_doctor(unsigned
111
112 GETSHORT(qtype, p);
113 GETSHORT(qclass, p);
114 - p += 4; /* ttl */
115 + GETLONG(ttl, p); /* ttl */
116 GETSHORT(rdlen, p);
117
118 if (qclass == C_IN && qtype == T_A)
119 @@ -416,6 +473,9 @@ static unsigned char *do_doctor(unsigned
120 if (!CHECK_LEN(header, p, qlen, INADDRSZ))
121 return 0;
122
123 + if (ubus_dns_doctor(daemon->namebuff, ttl, p, AF_INET))
124 + *doctored = 1;
125 +
126 /* alignment */
127 memcpy(&addr, p, INADDRSZ);
128
129 @@ -433,13 +493,22 @@ static unsigned char *do_doctor(unsigned
130 addr.s_addr &= ~doctor->mask.s_addr;
131 addr.s_addr |= (doctor->out.s_addr & doctor->mask.s_addr);
132 /* Since we munged the data, the server it came from is no longer authoritative */
133 - header->hb3 &= ~HB3_AA;
134 *doctored = 1;
135 memcpy(p, &addr, INADDRSZ);
136 break;
137 }
138 }
139 -
140 + else if (qclass == C_IN && qtype == T_AAAA)
141 + {
142 + if (!CHECK_LEN(header, p, qlen, IN6ADDRSZ))
143 + return 0;
144 +
145 + if (ubus_dns_doctor(daemon->namebuff, ttl, p, AF_INET6))
146 + *doctored = 1;
147 + }
148 +
149 + if (*doctored)
150 + header->hb3 &= ~HB3_AA;
151 if (!ADD_RDLEN(header, p, qlen, rdlen))
152 return 0; /* bad packet */
153 }
154 @@ -563,7 +632,7 @@ int extract_addresses(struct dns_header
155 cache_start_insert();
156
157 /* find_soa is needed for dns_doctor side effects, so don't call it lazily if there are any. */
158 - if (daemon->doctors || option_bool(OPT_DNSSEC_VALID))
159 + if (daemon->doctors || option_bool(OPT_DNSSEC_VALID) || ubus_dns_notify_prepare())
160 {
161 searched_soa = 1;
162 ttl = find_soa(header, qlen, doctored);
163 --- a/src/ubus.c
164 +++ b/src/ubus.c
165 @@ -72,6 +72,14 @@ static struct ubus_object ubus_object =
166 .subscribe_cb = ubus_subscribe_cb,
167 };
168
169 +static struct ubus_object_type ubus_dns_object_type =
170 + { .name = "dnsmasq.dns" };
171 +
172 +static struct ubus_object ubus_dns_object = {
173 + .name = "dnsmasq.dns",
174 + .type = &ubus_dns_object_type,
175 +};
176 +
177 static void ubus_subscribe_cb(struct ubus_context *ctx, struct ubus_object *obj)
178 {
179 (void)ctx;
180 @@ -112,6 +120,8 @@ char *ubus_init()
181
182 ubus_object.name = daemon->ubus_name;
183 ret = ubus_add_object(ubus, &ubus_object);
184 + if (!ret)
185 + ret = ubus_add_object(ubus, &ubus_dns_object);
186 if (ret)
187 {
188 ubus_destroy(ubus);
189 @@ -181,6 +191,17 @@ void check_ubus_listeners()
190 } \
191 } while (0)
192
193 +void drop_ubus_listeners()
194 +{
195 + struct ubus_context *ubus = (struct ubus_context *)daemon->ubus;
196 +
197 + if (!ubus)
198 + return;
199 +
200 + ubus_free(ubus);
201 + ubus = NULL;
202 +}
203 +
204 static int ubus_handle_metrics(struct ubus_context *ctx, struct ubus_object *obj,
205 struct ubus_request_data *req, const char *method,
206 struct blob_attr *msg)
207 @@ -328,6 +349,50 @@ fail:
208 } \
209 } while (0)
210
211 +struct blob_buf *ubus_dns_notify_prepare(void)
212 +{
213 + struct ubus_context *ubus = (struct ubus_context *)daemon->ubus;
214 +
215 + if (!ubus || !ubus_dns_object.has_subscribers)
216 + return NULL;
217 +
218 + blob_buf_init(&b, 0);
219 + return &b;
220 +}
221 +
222 +struct ubus_dns_notify_req {
223 + struct ubus_notify_request req;
224 + ubus_dns_notify_cb cb;
225 + void *priv;
226 +};
227 +
228 +static void dns_notify_cb(struct ubus_notify_request *req, int type, struct blob_attr *msg)
229 +{
230 + struct ubus_dns_notify_req *dreq = container_of(req, struct ubus_dns_notify_req, req);
231 +
232 + dreq->cb(msg, dreq->priv);
233 +}
234 +
235 +int ubus_dns_notify(const char *type, ubus_dns_notify_cb cb, void *priv)
236 +{
237 + struct ubus_context *ubus = (struct ubus_context *)daemon->ubus;
238 + struct ubus_dns_notify_req dreq;
239 + int ret;
240 +
241 + if (!ubus || !ubus_dns_object.has_subscribers)
242 + return 0;
243 +
244 + ret = ubus_notify_async(ubus, &ubus_dns_object, type, b.head, &dreq.req);
245 + if (ret)
246 + return ret;
247 +
248 + dreq.req.data_cb = dns_notify_cb;
249 + dreq.cb = cb;
250 + dreq.priv = priv;
251 +
252 + return ubus_complete_request(ubus, &dreq.req.req, 100);
253 +}
254 +
255 void ubus_event_bcast(const char *type, const char *mac, const char *ip, const char *name, const char *interface)
256 {
257 struct ubus_context *ubus = (struct ubus_context *)daemon->ubus;
258 --- a/src/dnsmasq.c
259 +++ b/src/dnsmasq.c
260 @@ -1972,6 +1972,10 @@ static void check_dns_listeners(time_t n
261 daemon->pipe_to_parent = pipefd[1];
262 }
263
264 +#ifdef HAVE_UBUS
265 + drop_ubus_listeners();
266 +#endif
267 +
268 /* start with no upstream connections. */
269 for (s = daemon->servers; s; s = s->next)
270 s->tcpfd = -1;