3 @@ -1631,14 +1631,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 int 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 int 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 @@ -570,7 +639,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,13 @@ 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 + .type = &ubus_dns_object_type,
176 static void ubus_subscribe_cb(struct ubus_context *ctx, struct ubus_object *obj)
179 @@ -105,13 +112,21 @@ static void ubus_disconnect_cb(struct ub
182 struct ubus_context *ubus = NULL;
186 if (!(ubus = ubus_connect(NULL)))
189 + dns_name = whine_malloc(strlen(daemon->ubus_name) + 5);
190 + sprintf(dns_name, "%s.dns", daemon->ubus_name);
192 ubus_object.name = daemon->ubus_name;
193 + ubus_dns_object.name = dns_name;
195 ret = ubus_add_object(ubus, &ubus_object);
197 + ret = ubus_add_object(ubus, &ubus_dns_object);
201 @@ -181,6 +196,17 @@ void check_ubus_listeners()
205 +void drop_ubus_listeners()
207 + struct ubus_context *ubus = (struct ubus_context *)daemon->ubus;
213 + daemon->ubus = NULL;
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:
223 +struct blob_buf *ubus_dns_notify_prepare(void)
225 + struct ubus_context *ubus = (struct ubus_context *)daemon->ubus;
227 + if (!ubus || !ubus_dns_object.has_subscribers)
230 + blob_buf_init(&b, 0);
234 +struct ubus_dns_notify_req {
235 + struct ubus_notify_request req;
236 + ubus_dns_notify_cb cb;
240 +static void dns_notify_cb(struct ubus_notify_request *req, int type, struct blob_attr *msg)
242 + struct ubus_dns_notify_req *dreq = container_of(req, struct ubus_dns_notify_req, req);
244 + dreq->cb(msg, dreq->priv);
247 +int ubus_dns_notify(const char *type, ubus_dns_notify_cb cb, void *priv)
249 + struct ubus_context *ubus = (struct ubus_context *)daemon->ubus;
250 + struct ubus_dns_notify_req dreq;
253 + if (!ubus || !ubus_dns_object.has_subscribers)
256 + ret = ubus_notify_async(ubus, &ubus_dns_object, type, b.head, &dreq.req);
260 + dreq.req.data_cb = dns_notify_cb;
264 + return ubus_complete_request(ubus, &dreq.req.req, 100);
267 void ubus_event_bcast(const char *type, const char *mac, const char *ip, const char *name, const char *interface)
269 struct ubus_context *ubus = (struct ubus_context *)daemon->ubus;
272 @@ -2003,6 +2003,10 @@ static void check_dns_listeners(time_t n
273 daemon->pipe_to_parent = pipefd[1];
277 + drop_ubus_listeners();
280 /* start with no upstream connections. */
281 for (s = daemon->servers; s; s = s->next)