| 1 |
/* dnsmasq is Copyright (c) 2000-2026 Simon Kelley |
| 2 |
|
| 3 |
This program is free software; you can redistribute it and/or modify |
| 4 |
it under the terms of the GNU General Public License as published by |
| 5 |
the Free Software Foundation; version 2 dated June, 1991, or |
| 6 |
(at your option) version 3 dated 29 June, 2007. |
| 7 |
|
| 8 |
This program is distributed in the hope that it will be useful, |
| 9 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
GNU General Public License for more details. |
| 12 |
|
| 13 |
You should have received a copy of the GNU General Public License |
| 14 |
along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
*/ |
| 16 |
|
| 17 |
#include "dnsmasq.h" |
| 18 |
|
| 19 |
#ifdef HAVE_UBUS |
| 20 |
|
| 21 |
#include <libubus.h> |
| 22 |
|
| 23 |
static struct blob_buf b; |
| 24 |
static int error_logged = 0; |
| 25 |
|
| 26 |
static int ubus_handle_metrics(struct ubus_context *ctx, struct ubus_object *obj, |
| 27 |
struct ubus_request_data *req, const char *method, |
| 28 |
struct blob_attr *msg); |
| 29 |
|
| 30 |
#ifdef HAVE_CONNTRACK |
| 31 |
enum { |
| 32 |
SET_CONNMARK_ALLOWLIST_MARK, |
| 33 |
SET_CONNMARK_ALLOWLIST_MASK, |
| 34 |
SET_CONNMARK_ALLOWLIST_PATTERNS |
| 35 |
}; |
| 36 |
static const struct blobmsg_policy set_connmark_allowlist_policy[] = { |
| 37 |
[SET_CONNMARK_ALLOWLIST_MARK] = { |
| 38 |
.name = "mark", |
| 39 |
.type = BLOBMSG_TYPE_INT32 |
| 40 |
}, |
| 41 |
[SET_CONNMARK_ALLOWLIST_MASK] = { |
| 42 |
.name = "mask", |
| 43 |
.type = BLOBMSG_TYPE_INT32 |
| 44 |
}, |
| 45 |
[SET_CONNMARK_ALLOWLIST_PATTERNS] = { |
| 46 |
.name = "patterns", |
| 47 |
.type = BLOBMSG_TYPE_ARRAY |
| 48 |
} |
| 49 |
}; |
| 50 |
static int ubus_handle_set_connmark_allowlist(struct ubus_context *ctx, struct ubus_object *obj, |
| 51 |
struct ubus_request_data *req, const char *method, |
| 52 |
struct blob_attr *msg); |
| 53 |
#endif |
| 54 |
|
| 55 |
static void ubus_subscribe_cb(struct ubus_context *ctx, struct ubus_object *obj); |
| 56 |
|
| 57 |
static const struct ubus_method ubus_object_methods[] = { |
| 58 |
UBUS_METHOD_NOARG("metrics", ubus_handle_metrics), |
| 59 |
#ifdef HAVE_CONNTRACK |
| 60 |
UBUS_METHOD("set_connmark_allowlist", ubus_handle_set_connmark_allowlist, set_connmark_allowlist_policy), |
| 61 |
#endif |
| 62 |
}; |
| 63 |
|
| 64 |
static struct ubus_object_type ubus_object_type = |
| 65 |
UBUS_OBJECT_TYPE("dnsmasq", ubus_object_methods); |
| 66 |
|
| 67 |
static struct ubus_object ubus_object = { |
| 68 |
.name = NULL, |
| 69 |
.type = &ubus_object_type, |
| 70 |
.methods = ubus_object_methods, |
| 71 |
.n_methods = ARRAY_SIZE(ubus_object_methods), |
| 72 |
.subscribe_cb = ubus_subscribe_cb, |
| 73 |
}; |
| 74 |
|
| 75 |
static void ubus_subscribe_cb(struct ubus_context *ctx, struct ubus_object *obj) |
| 76 |
{ |
| 77 |
(void)ctx; |
| 78 |
|
| 79 |
my_syslog(LOG_DEBUG, _("UBus subscription callback: %s subscriber(s)"), obj->has_subscribers ? "1" : "0"); |
| 80 |
} |
| 81 |
|
| 82 |
static void ubus_destroy(struct ubus_context *ubus) |
| 83 |
{ |
| 84 |
ubus_free(ubus); |
| 85 |
daemon->ubus = NULL; |
| 86 |
|
| 87 |
/* Forces re-initialization when we're reusing the same definitions later on. */ |
| 88 |
ubus_object.id = 0; |
| 89 |
ubus_object_type.id = 0; |
| 90 |
} |
| 91 |
|
| 92 |
static void ubus_disconnect_cb(struct ubus_context *ubus) |
| 93 |
{ |
| 94 |
int ret; |
| 95 |
|
| 96 |
ret = ubus_reconnect(ubus, NULL); |
| 97 |
if (ret) |
| 98 |
{ |
| 99 |
my_syslog(LOG_ERR, _("Cannot reconnect to UBus: %s"), ubus_strerror(ret)); |
| 100 |
|
| 101 |
ubus_destroy(ubus); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
char *ubus_init() |
| 106 |
{ |
| 107 |
struct ubus_context *ubus = NULL; |
| 108 |
int ret = 0; |
| 109 |
|
| 110 |
if (!(ubus = ubus_connect(NULL))) |
| 111 |
return NULL; |
| 112 |
|
| 113 |
ubus_object.name = daemon->ubus_name; |
| 114 |
ret = ubus_add_object(ubus, &ubus_object); |
| 115 |
if (ret) |
| 116 |
{ |
| 117 |
ubus_destroy(ubus); |
| 118 |
return (char *)ubus_strerror(ret); |
| 119 |
} |
| 120 |
|
| 121 |
ubus->connection_lost = ubus_disconnect_cb; |
| 122 |
daemon->ubus = ubus; |
| 123 |
error_logged = 0; |
| 124 |
|
| 125 |
return NULL; |
| 126 |
} |
| 127 |
|
| 128 |
void set_ubus_listeners() |
| 129 |
{ |
| 130 |
struct ubus_context *ubus = (struct ubus_context *)daemon->ubus; |
| 131 |
if (!ubus) |
| 132 |
{ |
| 133 |
if (!error_logged) |
| 134 |
{ |
| 135 |
my_syslog(LOG_ERR, _("Cannot set UBus listeners: no connection")); |
| 136 |
error_logged = 1; |
| 137 |
} |
| 138 |
return; |
| 139 |
} |
| 140 |
|
| 141 |
error_logged = 0; |
| 142 |
|
| 143 |
poll_listen(ubus->sock.fd, POLLIN); |
| 144 |
poll_listen(ubus->sock.fd, POLLERR); |
| 145 |
poll_listen(ubus->sock.fd, POLLHUP); |
| 146 |
} |
| 147 |
|
| 148 |
void check_ubus_listeners() |
| 149 |
{ |
| 150 |
struct ubus_context *ubus = (struct ubus_context *)daemon->ubus; |
| 151 |
if (!ubus) |
| 152 |
{ |
| 153 |
if (!error_logged) |
| 154 |
{ |
| 155 |
my_syslog(LOG_ERR, _("Cannot poll UBus listeners: no connection")); |
| 156 |
error_logged = 1; |
| 157 |
} |
| 158 |
return; |
| 159 |
} |
| 160 |
|
| 161 |
error_logged = 0; |
| 162 |
|
| 163 |
if (poll_check(ubus->sock.fd, POLLIN)) |
| 164 |
ubus_handle_event(ubus); |
| 165 |
|
| 166 |
if (poll_check(ubus->sock.fd, POLLHUP | POLLERR)) |
| 167 |
{ |
| 168 |
my_syslog(LOG_INFO, _("Disconnecting from UBus")); |
| 169 |
|
| 170 |
ubus_destroy(ubus); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
#define CHECK(stmt) \ |
| 175 |
do { \ |
| 176 |
int e = (stmt); \ |
| 177 |
if (e) \ |
| 178 |
{ \ |
| 179 |
my_syslog(LOG_ERR, _("UBus command failed: %d (%s)"), e, #stmt); \ |
| 180 |
return (UBUS_STATUS_UNKNOWN_ERROR); \ |
| 181 |
} \ |
| 182 |
} while (0) |
| 183 |
|
| 184 |
static int ubus_handle_metrics(struct ubus_context *ctx, struct ubus_object *obj, |
| 185 |
struct ubus_request_data *req, const char *method, |
| 186 |
struct blob_attr *msg) |
| 187 |
{ |
| 188 |
int i; |
| 189 |
|
| 190 |
(void)obj; |
| 191 |
(void)method; |
| 192 |
(void)msg; |
| 193 |
|
| 194 |
CHECK(blob_buf_init(&b, BLOBMSG_TYPE_TABLE)); |
| 195 |
|
| 196 |
for (i=0; i < __METRIC_MAX; i++) |
| 197 |
CHECK(blobmsg_add_u32(&b, get_metric_name(i), daemon->metrics[i])); |
| 198 |
|
| 199 |
CHECK(ubus_send_reply(ctx, req, b.head)); |
| 200 |
return UBUS_STATUS_OK; |
| 201 |
} |
| 202 |
|
| 203 |
#ifdef HAVE_CONNTRACK |
| 204 |
static int ubus_handle_set_connmark_allowlist(struct ubus_context *ctx, struct ubus_object *obj, |
| 205 |
struct ubus_request_data *req, const char *method, |
| 206 |
struct blob_attr *msg) |
| 207 |
{ |
| 208 |
const struct blobmsg_policy *policy = set_connmark_allowlist_policy; |
| 209 |
size_t policy_len = countof(set_connmark_allowlist_policy); |
| 210 |
struct allowlist *allowlists = NULL, **allowlists_pos; |
| 211 |
char **patterns = NULL, **patterns_pos; |
| 212 |
u32 mark, mask = UINT32_MAX; |
| 213 |
size_t num_patterns = 0; |
| 214 |
struct blob_attr *tb[policy_len]; |
| 215 |
struct blob_attr *attr; |
| 216 |
|
| 217 |
(void)ctx; |
| 218 |
(void)obj; |
| 219 |
(void)req; |
| 220 |
(void)method; |
| 221 |
|
| 222 |
if (blobmsg_parse(policy, policy_len, tb, blob_data(msg), blob_len(msg))) |
| 223 |
return UBUS_STATUS_INVALID_ARGUMENT; |
| 224 |
|
| 225 |
if (!tb[SET_CONNMARK_ALLOWLIST_MARK]) |
| 226 |
return UBUS_STATUS_INVALID_ARGUMENT; |
| 227 |
mark = blobmsg_get_u32(tb[SET_CONNMARK_ALLOWLIST_MARK]); |
| 228 |
if (!mark) |
| 229 |
return UBUS_STATUS_INVALID_ARGUMENT; |
| 230 |
|
| 231 |
if (tb[SET_CONNMARK_ALLOWLIST_MASK]) |
| 232 |
{ |
| 233 |
mask = blobmsg_get_u32(tb[SET_CONNMARK_ALLOWLIST_MASK]); |
| 234 |
if (!mask || (mark & ~mask)) |
| 235 |
return UBUS_STATUS_INVALID_ARGUMENT; |
| 236 |
} |
| 237 |
|
| 238 |
if (tb[SET_CONNMARK_ALLOWLIST_PATTERNS]) |
| 239 |
{ |
| 240 |
struct blob_attr *head = blobmsg_data(tb[SET_CONNMARK_ALLOWLIST_PATTERNS]); |
| 241 |
size_t len = blobmsg_data_len(tb[SET_CONNMARK_ALLOWLIST_PATTERNS]); |
| 242 |
__blob_for_each_attr(attr, head, len) |
| 243 |
{ |
| 244 |
char *pattern; |
| 245 |
if (blob_id(attr) != BLOBMSG_TYPE_STRING) |
| 246 |
return UBUS_STATUS_INVALID_ARGUMENT; |
| 247 |
if (!(pattern = blobmsg_get_string(attr))) |
| 248 |
return UBUS_STATUS_INVALID_ARGUMENT; |
| 249 |
if (strcmp(pattern, "*") && !is_valid_dns_name_pattern(pattern)) |
| 250 |
return UBUS_STATUS_INVALID_ARGUMENT; |
| 251 |
num_patterns++; |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
for (allowlists_pos = &daemon->allowlists; *allowlists_pos; allowlists_pos = &(*allowlists_pos)->next) |
| 256 |
if ((*allowlists_pos)->mark == mark && (*allowlists_pos)->mask == mask) |
| 257 |
{ |
| 258 |
struct allowlist *allowlists_next = (*allowlists_pos)->next; |
| 259 |
for (patterns_pos = (*allowlists_pos)->patterns; *patterns_pos; patterns_pos++) |
| 260 |
{ |
| 261 |
free(*patterns_pos); |
| 262 |
*patterns_pos = NULL; |
| 263 |
} |
| 264 |
free((*allowlists_pos)->patterns); |
| 265 |
(*allowlists_pos)->patterns = NULL; |
| 266 |
free(*allowlists_pos); |
| 267 |
*allowlists_pos = allowlists_next; |
| 268 |
break; |
| 269 |
} |
| 270 |
|
| 271 |
if (!num_patterns) |
| 272 |
return UBUS_STATUS_OK; |
| 273 |
|
| 274 |
patterns = whine_malloc((num_patterns + 1) * sizeof(char *)); |
| 275 |
if (!patterns) |
| 276 |
goto fail; |
| 277 |
patterns_pos = patterns; |
| 278 |
if (tb[SET_CONNMARK_ALLOWLIST_PATTERNS]) |
| 279 |
{ |
| 280 |
struct blob_attr *head = blobmsg_data(tb[SET_CONNMARK_ALLOWLIST_PATTERNS]); |
| 281 |
size_t len = blobmsg_data_len(tb[SET_CONNMARK_ALLOWLIST_PATTERNS]); |
| 282 |
__blob_for_each_attr(attr, head, len) |
| 283 |
{ |
| 284 |
char *pattern; |
| 285 |
if (!(pattern = blobmsg_get_string(attr))) |
| 286 |
goto fail; |
| 287 |
if (!(*patterns_pos = whine_malloc(strlen(pattern) + 1))) |
| 288 |
goto fail; |
| 289 |
strcpy(*patterns_pos++, pattern); |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
allowlists = whine_malloc(sizeof(struct allowlist)); |
| 294 |
if (!allowlists) |
| 295 |
goto fail; |
| 296 |
memset(allowlists, 0, sizeof(struct allowlist)); |
| 297 |
allowlists->mark = mark; |
| 298 |
allowlists->mask = mask; |
| 299 |
allowlists->patterns = patterns; |
| 300 |
allowlists->next = daemon->allowlists; |
| 301 |
daemon->allowlists = allowlists; |
| 302 |
return UBUS_STATUS_OK; |
| 303 |
|
| 304 |
fail: |
| 305 |
if (patterns) |
| 306 |
{ |
| 307 |
for (patterns_pos = patterns; *patterns_pos; patterns_pos++) |
| 308 |
{ |
| 309 |
free(*patterns_pos); |
| 310 |
*patterns_pos = NULL; |
| 311 |
} |
| 312 |
free(patterns); |
| 313 |
patterns = NULL; |
| 314 |
} |
| 315 |
if (allowlists) |
| 316 |
{ |
| 317 |
free(allowlists); |
| 318 |
allowlists = NULL; |
| 319 |
} |
| 320 |
return UBUS_STATUS_UNKNOWN_ERROR; |
| 321 |
} |
| 322 |
#endif |
| 323 |
|
| 324 |
#undef CHECK |
| 325 |
|
| 326 |
#define CHECK(stmt) \ |
| 327 |
do { \ |
| 328 |
int e = (stmt); \ |
| 329 |
if (e) \ |
| 330 |
{ \ |
| 331 |
my_syslog(LOG_ERR, _("UBus command failed: %d (%s)"), e, #stmt); \ |
| 332 |
return; \ |
| 333 |
} \ |
| 334 |
} while (0) |
| 335 |
|
| 336 |
void ubus_event_bcast(const char *type, const char *mac, const char *ip, const char *name, const char *interface) |
| 337 |
{ |
| 338 |
struct ubus_context *ubus = (struct ubus_context *)daemon->ubus; |
| 339 |
|
| 340 |
if (!ubus || !ubus_object.has_subscribers) |
| 341 |
return; |
| 342 |
|
| 343 |
CHECK(blob_buf_init(&b, BLOBMSG_TYPE_TABLE)); |
| 344 |
if (mac) |
| 345 |
CHECK(blobmsg_add_string(&b, "mac", mac)); |
| 346 |
if (ip) |
| 347 |
CHECK(blobmsg_add_string(&b, "ip", ip)); |
| 348 |
if (name) |
| 349 |
CHECK(blobmsg_add_string(&b, "name", name)); |
| 350 |
if (interface) |
| 351 |
CHECK(blobmsg_add_string(&b, "interface", interface)); |
| 352 |
|
| 353 |
CHECK(ubus_notify(ubus, &ubus_object, type, b.head, -1)); |
| 354 |
} |
| 355 |
|
| 356 |
#ifdef HAVE_CONNTRACK |
| 357 |
void ubus_event_bcast_connmark_allowlist_refused(u32 mark, const char *name) |
| 358 |
{ |
| 359 |
struct ubus_context *ubus = (struct ubus_context *)daemon->ubus; |
| 360 |
|
| 361 |
if (!ubus || !ubus_object.has_subscribers) |
| 362 |
return; |
| 363 |
|
| 364 |
CHECK(blob_buf_init(&b, 0)); |
| 365 |
CHECK(blobmsg_add_u32(&b, "mark", mark)); |
| 366 |
CHECK(blobmsg_add_string(&b, "name", name)); |
| 367 |
|
| 368 |
CHECK(ubus_notify(ubus, &ubus_object, "connmark-allowlist.refused", b.head, -1)); |
| 369 |
} |
| 370 |
|
| 371 |
void ubus_event_bcast_connmark_allowlist_resolved(u32 mark, const char *name, const char *value, u32 ttl) |
| 372 |
{ |
| 373 |
struct ubus_context *ubus = (struct ubus_context *)daemon->ubus; |
| 374 |
|
| 375 |
if (!ubus || !ubus_object.has_subscribers) |
| 376 |
return; |
| 377 |
|
| 378 |
CHECK(blob_buf_init(&b, 0)); |
| 379 |
CHECK(blobmsg_add_u32(&b, "mark", mark)); |
| 380 |
CHECK(blobmsg_add_string(&b, "name", name)); |
| 381 |
CHECK(blobmsg_add_string(&b, "value", value)); |
| 382 |
CHECK(blobmsg_add_u32(&b, "ttl", ttl)); |
| 383 |
|
| 384 |
/* Set timeout to allow UBus subscriber to configure firewall rules before returning. */ |
| 385 |
CHECK(ubus_notify(ubus, &ubus_object, "connmark-allowlist.resolved", b.head, /* timeout: */ 1000)); |
| 386 |
} |
| 387 |
#endif |
| 388 |
|
| 389 |
#undef CHECK |
| 390 |
|
| 391 |
#endif /* HAVE_UBUS */ |