dnsmasq: bump to v2.88
[openwrt/staging/jow.git] / package / network / services / dnsmasq / patches / 200-ubus_dns.patch
1 --- a/src/dnsmasq.h
2 +++ b/src/dnsmasq.h
3 @@ -1630,14 +1630,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 int 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 0;
70 +
71 + b = ubus_dns_notify_prepare();
72 + if (!b)
73 + return 0;
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 0;
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 0;
93 +
94 + return inet_pton(af, addr, p) == 1;
95 +}
96 +#else
97 +static int ubus_dns_doctor(const char *name, int ttl, void *p, int af)
98 +{
99 + return 0;
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 @@ -570,7 +639,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,13 @@ 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 + .type = &ubus_dns_object_type,
174 +};
175 +
176 static void ubus_subscribe_cb(struct ubus_context *ctx, struct ubus_object *obj)
177 {
178 (void)ctx;
179 @@ -105,13 +112,21 @@ static void ubus_disconnect_cb(struct ub
180 char *ubus_init()
181 {
182 struct ubus_context *ubus = NULL;
183 + char *dns_name;
184 int ret = 0;
185
186 if (!(ubus = ubus_connect(NULL)))
187 return NULL;
188
189 + dns_name = whine_malloc(strlen(daemon->ubus_name) + 5);
190 + sprintf(dns_name, "%s.dns", daemon->ubus_name);
191 +
192 ubus_object.name = daemon->ubus_name;
193 + ubus_dns_object.name = dns_name;
194 +
195 ret = ubus_add_object(ubus, &ubus_object);
196 + if (!ret)
197 + ret = ubus_add_object(ubus, &ubus_dns_object);
198 if (ret)
199 {
200 ubus_destroy(ubus);
201 @@ -181,6 +196,17 @@ void check_ubus_listeners()
202 } \
203 } while (0)
204
205 +void drop_ubus_listeners()
206 +{
207 + struct ubus_context *ubus = (struct ubus_context *)daemon->ubus;
208 +
209 + if (!ubus)
210 + return;
211 +
212 + ubus_free(ubus);
213 + ubus = NULL;
214 +}
215 +
216 static int ubus_handle_metrics(struct ubus_context *ctx, struct ubus_object *obj,
217 struct ubus_request_data *req, const char *method,
218 struct blob_attr *msg)
219 @@ -328,6 +354,50 @@ fail:
220 } \
221 } while (0)
222
223 +struct blob_buf *ubus_dns_notify_prepare(void)
224 +{
225 + struct ubus_context *ubus = (struct ubus_context *)daemon->ubus;
226 +
227 + if (!ubus || !ubus_dns_object.has_subscribers)
228 + return NULL;
229 +
230 + blob_buf_init(&b, 0);
231 + return &b;
232 +}
233 +
234 +struct ubus_dns_notify_req {
235 + struct ubus_notify_request req;
236 + ubus_dns_notify_cb cb;
237 + void *priv;
238 +};
239 +
240 +static void dns_notify_cb(struct ubus_notify_request *req, int type, struct blob_attr *msg)
241 +{
242 + struct ubus_dns_notify_req *dreq = container_of(req, struct ubus_dns_notify_req, req);
243 +
244 + dreq->cb(msg, dreq->priv);
245 +}
246 +
247 +int ubus_dns_notify(const char *type, ubus_dns_notify_cb cb, void *priv)
248 +{
249 + struct ubus_context *ubus = (struct ubus_context *)daemon->ubus;
250 + struct ubus_dns_notify_req dreq;
251 + int ret;
252 +
253 + if (!ubus || !ubus_dns_object.has_subscribers)
254 + return 0;
255 +
256 + ret = ubus_notify_async(ubus, &ubus_dns_object, type, b.head, &dreq.req);
257 + if (ret)
258 + return ret;
259 +
260 + dreq.req.data_cb = dns_notify_cb;
261 + dreq.cb = cb;
262 + dreq.priv = priv;
263 +
264 + return ubus_complete_request(ubus, &dreq.req.req, 100);
265 +}
266 +
267 void ubus_event_bcast(const char *type, const char *mac, const char *ip, const char *name, const char *interface)
268 {
269 struct ubus_context *ubus = (struct ubus_context *)daemon->ubus;
270 --- a/src/dnsmasq.c
271 +++ b/src/dnsmasq.c
272 @@ -2003,6 +2003,10 @@ static void check_dns_listeners(time_t n
273 daemon->pipe_to_parent = pipefd[1];
274 }
275
276 +#ifdef HAVE_UBUS
277 + drop_ubus_listeners();
278 +#endif
279 +
280 /* start with no upstream connections. */
281 for (s = daemon->servers; s; s = s->next)
282 s->tcpfd = -1;