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