3 @@ -1564,14 +1564,26 @@ void emit_dbus_signal(int action, struct
8 +typedef void (*ubus_dns_notify_cb)(struct blob_attr *msg, void *priv);
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);
23 +static inline struct blob_buf *ubus_dns_notify_prepare(void)
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/>.
39 +#include <libubox/blobmsg.h>
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 */
49 +static void ubus_dns_doctor_cb(struct blob_attr *msg, void *priv)
51 + static const struct blobmsg_policy policy = {
53 + .type = BLOBMSG_TYPE_STRING,
55 + struct blob_attr *val;
58 + blobmsg_parse(&policy, 1, &val, blobmsg_data(msg), blobmsg_data_len(msg));
60 + *dest = blobmsg_get_string(val);
63 +static bool ubus_dns_doctor(const char *name, int ttl, void *p, int af)
71 + b = ubus_dns_notify_prepare();
75 + blobmsg_add_string(b, "name", name);
77 + blobmsg_add_u32(b, "ttl", ttl);
79 + blobmsg_add_string(b, "type", af == AF_INET6 ? "AAAA" : "A");
81 + addr = blobmsg_alloc_string_buffer(b, "address", INET6_ADDRSTRLEN);
85 + inet_ntop(af, p, addr, INET6_ADDRSTRLEN);
86 + blobmsg_add_string_buffer(b);
89 + ubus_dns_notify("dns_result", ubus_dns_doctor_cb, &addr);
94 + return inet_pton(af, addr, p) == 1;
97 +static bool ubus_dns_doctor(const char *name, int ttl, void *p, int af)
103 static unsigned char *do_doctor(unsigned char *p, int count, struct dns_header *header, size_t qlen, int *doctored)
105 - int i, qtype, qclass, rdlen;
106 + int i, qtype, qclass, rdlen, ttl;
108 for (i = count; i != 0; i--)
110 @@ -405,7 +462,7 @@ static unsigned char *do_doctor(unsigned
115 + GETLONG(ttl, p); /* ttl */
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))
123 + if (ubus_dns_doctor(daemon->namebuff, ttl, p, AF_INET))
127 memcpy(&addr, p, INADDRSZ);
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;
135 memcpy(p, &addr, INADDRSZ);
140 + else if (qclass == C_IN && qtype == T_AAAA)
142 + if (!CHECK_LEN(header, p, qlen, IN6ADDRSZ))
145 + if (ubus_dns_doctor(daemon->namebuff, ttl, p, AF_INET6))
150 + header->hb3 &= ~HB3_AA;
151 if (!ADD_RDLEN(header, p, qlen, rdlen))
152 return 0; /* bad packet */
154 @@ -563,7 +632,7 @@ int extract_addresses(struct dns_header
155 cache_start_insert();
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())
162 ttl = find_soa(header, qlen, doctored);
165 @@ -72,6 +72,14 @@ static struct ubus_object ubus_object =
166 .subscribe_cb = ubus_subscribe_cb,
169 +static struct ubus_object_type ubus_dns_object_type =
170 + { .name = "dnsmasq.dns" };
172 +static struct ubus_object ubus_dns_object = {
173 + .name = "dnsmasq.dns",
174 + .type = &ubus_dns_object_type,
177 static void ubus_subscribe_cb(struct ubus_context *ctx, struct ubus_object *obj)
180 @@ -112,6 +120,8 @@ char *ubus_init()
182 ubus_object.name = daemon->ubus_name;
183 ret = ubus_add_object(ubus, &ubus_object);
185 + ret = ubus_add_object(ubus, &ubus_dns_object);
189 @@ -181,6 +191,17 @@ void check_ubus_listeners()
193 +void drop_ubus_listeners()
195 + struct ubus_context *ubus = (struct ubus_context *)daemon->ubus;
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:
211 +struct blob_buf *ubus_dns_notify_prepare(void)
213 + struct ubus_context *ubus = (struct ubus_context *)daemon->ubus;
215 + if (!ubus || !ubus_dns_object.has_subscribers)
218 + blob_buf_init(&b, 0);
222 +struct ubus_dns_notify_req {
223 + struct ubus_notify_request req;
224 + ubus_dns_notify_cb cb;
228 +static void dns_notify_cb(struct ubus_notify_request *req, int type, struct blob_attr *msg)
230 + struct ubus_dns_notify_req *dreq = container_of(req, struct ubus_dns_notify_req, req);
232 + dreq->cb(msg, dreq->priv);
235 +int ubus_dns_notify(const char *type, ubus_dns_notify_cb cb, void *priv)
237 + struct ubus_context *ubus = (struct ubus_context *)daemon->ubus;
238 + struct ubus_dns_notify_req dreq;
241 + if (!ubus || !ubus_dns_object.has_subscribers)
244 + ret = ubus_notify_async(ubus, &ubus_dns_object, type, b.head, &dreq.req);
248 + dreq.req.data_cb = dns_notify_cb;
252 + return ubus_complete_request(ubus, &dreq.req.req, 100);
255 void ubus_event_bcast(const char *type, const char *mac, const char *ip, const char *name, const char *interface)
257 struct ubus_context *ubus = (struct ubus_context *)daemon->ubus;
260 @@ -1972,6 +1972,10 @@ static void check_dns_listeners(time_t n
261 daemon->pipe_to_parent = pipefd[1];
265 + drop_ubus_listeners();
268 /* start with no upstream connections. */
269 for (s = daemon->servers; s; s = s->next)