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