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