Simon Kelley | 2a8710a | 2020-01-05 16:40:06 +0000 | [diff] [blame] | 1 | /* dnsmasq is Copyright (c) 2000-2020 Simon Kelley |
Simon Kelley | 8c1b6a5 | 2018-07-21 22:12:32 +0100 | [diff] [blame] | 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 | |
Simon Kelley | 8c1b6a5 | 2018-07-21 22:12:32 +0100 | [diff] [blame] | 23 | static struct blob_buf b; |
Jan Willem Janssen | a2b8220 | 2019-03-25 12:42:23 +0100 | [diff] [blame] | 24 | static int notify; |
| 25 | static int error_logged = 0; |
Simon Kelley | 8c1b6a5 | 2018-07-21 22:12:32 +0100 | [diff] [blame] | 26 | |
| 27 | static int ubus_handle_metrics(struct ubus_context *ctx, struct ubus_object *obj, |
Simon Kelley | 6f835ed | 2018-07-29 22:15:36 +0100 | [diff] [blame] | 28 | struct ubus_request_data *req, const char *method, |
| 29 | struct blob_attr *msg); |
Jan Willem Janssen | a2b8220 | 2019-03-25 12:42:23 +0100 | [diff] [blame] | 30 | |
| 31 | static void ubus_subscribe_cb(struct ubus_context *ctx, struct ubus_object *obj); |
| 32 | |
| 33 | static const struct ubus_method ubus_object_methods[] = { |
| 34 | UBUS_METHOD_NOARG("metrics", ubus_handle_metrics), |
Simon Kelley | 8c1b6a5 | 2018-07-21 22:12:32 +0100 | [diff] [blame] | 35 | }; |
| 36 | |
Jan Willem Janssen | a2b8220 | 2019-03-25 12:42:23 +0100 | [diff] [blame] | 37 | static struct ubus_object_type ubus_object_type = |
| 38 | UBUS_OBJECT_TYPE("dnsmasq", ubus_object_methods); |
Simon Kelley | 8c1b6a5 | 2018-07-21 22:12:32 +0100 | [diff] [blame] | 39 | |
| 40 | static struct ubus_object ubus_object = { |
Oldřich Jedlička | d162bee | 2020-03-20 22:18:57 +0100 | [diff] [blame] | 41 | .name = NULL, |
Simon Kelley | 6f835ed | 2018-07-29 22:15:36 +0100 | [diff] [blame] | 42 | .type = &ubus_object_type, |
| 43 | .methods = ubus_object_methods, |
| 44 | .n_methods = ARRAY_SIZE(ubus_object_methods), |
Jan Willem Janssen | a2b8220 | 2019-03-25 12:42:23 +0100 | [diff] [blame] | 45 | .subscribe_cb = ubus_subscribe_cb, |
Simon Kelley | 8c1b6a5 | 2018-07-21 22:12:32 +0100 | [diff] [blame] | 46 | }; |
| 47 | |
Jan Willem Janssen | a2b8220 | 2019-03-25 12:42:23 +0100 | [diff] [blame] | 48 | static void ubus_subscribe_cb(struct ubus_context *ctx, struct ubus_object *obj) |
| 49 | { |
| 50 | (void)ctx; |
| 51 | |
| 52 | my_syslog(LOG_DEBUG, _("UBus subscription callback: %s subscriber(s)"), obj->has_subscribers ? "1" : "0"); |
| 53 | notify = obj->has_subscribers; |
| 54 | } |
| 55 | |
| 56 | static void ubus_destroy(struct ubus_context *ubus) |
| 57 | { |
| 58 | // Forces re-initialization when we're reusing the same definitions later on. |
| 59 | ubus_object.id = 0; |
| 60 | ubus_object_type.id = 0; |
| 61 | |
| 62 | ubus_free(ubus); |
| 63 | daemon->ubus = NULL; |
| 64 | } |
| 65 | |
| 66 | static void ubus_disconnect_cb(struct ubus_context *ubus) |
| 67 | { |
| 68 | int ret; |
| 69 | |
| 70 | ret = ubus_reconnect(ubus, NULL); |
| 71 | if (ret) |
| 72 | { |
| 73 | my_syslog(LOG_ERR, _("Cannot reconnect to UBus: %s"), ubus_strerror(ret)); |
| 74 | |
| 75 | ubus_destroy(ubus); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | void ubus_init() |
| 80 | { |
| 81 | struct ubus_context *ubus = NULL; |
| 82 | int ret = 0; |
| 83 | |
| 84 | ubus = ubus_connect(NULL); |
| 85 | if (!ubus) |
| 86 | { |
| 87 | if (!error_logged) |
| 88 | { |
| 89 | my_syslog(LOG_ERR, _("Cannot initialize UBus: connection failed")); |
| 90 | error_logged = 1; |
| 91 | } |
| 92 | |
| 93 | ubus_destroy(ubus); |
| 94 | return; |
| 95 | } |
| 96 | |
Oldřich Jedlička | d162bee | 2020-03-20 22:18:57 +0100 | [diff] [blame] | 97 | ubus_object.name = daemon->ubus_name; |
Jan Willem Janssen | a2b8220 | 2019-03-25 12:42:23 +0100 | [diff] [blame] | 98 | ret = ubus_add_object(ubus, &ubus_object); |
| 99 | if (ret) |
| 100 | { |
| 101 | if (!error_logged) |
| 102 | { |
| 103 | my_syslog(LOG_ERR, _("Cannot add object to UBus: %s"), ubus_strerror(ret)); |
| 104 | error_logged = 1; |
| 105 | } |
Oldřich Jedlička | 3f60ecd | 2020-03-18 22:24:55 +0100 | [diff] [blame] | 106 | ubus_destroy(ubus); |
Jan Willem Janssen | a2b8220 | 2019-03-25 12:42:23 +0100 | [diff] [blame] | 107 | return; |
| 108 | } |
| 109 | |
| 110 | ubus->connection_lost = ubus_disconnect_cb; |
| 111 | daemon->ubus = ubus; |
| 112 | error_logged = 0; |
| 113 | |
| 114 | my_syslog(LOG_INFO, _("Connected to system UBus")); |
| 115 | } |
| 116 | |
Simon Kelley | 8c1b6a5 | 2018-07-21 22:12:32 +0100 | [diff] [blame] | 117 | void set_ubus_listeners() |
| 118 | { |
Jan Willem Janssen | a2b8220 | 2019-03-25 12:42:23 +0100 | [diff] [blame] | 119 | struct ubus_context *ubus = (struct ubus_context *)daemon->ubus; |
Simon Kelley | 6f835ed | 2018-07-29 22:15:36 +0100 | [diff] [blame] | 120 | if (!ubus) |
Jan Willem Janssen | a2b8220 | 2019-03-25 12:42:23 +0100 | [diff] [blame] | 121 | { |
| 122 | if (!error_logged) |
| 123 | { |
| 124 | my_syslog(LOG_ERR, _("Cannot set UBus listeners: no connection")); |
| 125 | error_logged = 1; |
| 126 | } |
| 127 | return; |
| 128 | } |
| 129 | |
| 130 | error_logged = 0; |
Simon Kelley | 8c1b6a5 | 2018-07-21 22:12:32 +0100 | [diff] [blame] | 131 | |
Simon Kelley | 6f835ed | 2018-07-29 22:15:36 +0100 | [diff] [blame] | 132 | poll_listen(ubus->sock.fd, POLLIN); |
| 133 | poll_listen(ubus->sock.fd, POLLERR); |
| 134 | poll_listen(ubus->sock.fd, POLLHUP); |
Simon Kelley | 8c1b6a5 | 2018-07-21 22:12:32 +0100 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | void check_ubus_listeners() |
| 138 | { |
Jan Willem Janssen | a2b8220 | 2019-03-25 12:42:23 +0100 | [diff] [blame] | 139 | struct ubus_context *ubus = (struct ubus_context *)daemon->ubus; |
Simon Kelley | 6f835ed | 2018-07-29 22:15:36 +0100 | [diff] [blame] | 140 | if (!ubus) |
| 141 | { |
Jan Willem Janssen | a2b8220 | 2019-03-25 12:42:23 +0100 | [diff] [blame] | 142 | if (!error_logged) |
| 143 | { |
| 144 | my_syslog(LOG_ERR, _("Cannot poll UBus listeners: no connection")); |
| 145 | error_logged = 1; |
| 146 | } |
| 147 | return; |
Simon Kelley | 6f835ed | 2018-07-29 22:15:36 +0100 | [diff] [blame] | 148 | } |
| 149 | |
Jan Willem Janssen | a2b8220 | 2019-03-25 12:42:23 +0100 | [diff] [blame] | 150 | error_logged = 0; |
| 151 | |
Simon Kelley | 6f835ed | 2018-07-29 22:15:36 +0100 | [diff] [blame] | 152 | if (poll_check(ubus->sock.fd, POLLIN)) |
| 153 | ubus_handle_event(ubus); |
| 154 | |
Jan Willem Janssen | a2b8220 | 2019-03-25 12:42:23 +0100 | [diff] [blame] | 155 | if (poll_check(ubus->sock.fd, POLLHUP | POLLERR)) |
Simon Kelley | 6f835ed | 2018-07-29 22:15:36 +0100 | [diff] [blame] | 156 | { |
Jan Willem Janssen | a2b8220 | 2019-03-25 12:42:23 +0100 | [diff] [blame] | 157 | my_syslog(LOG_INFO, _("Disconnecting from UBus")); |
| 158 | |
| 159 | ubus_destroy(ubus); |
Simon Kelley | 6f835ed | 2018-07-29 22:15:36 +0100 | [diff] [blame] | 160 | } |
Simon Kelley | 8c1b6a5 | 2018-07-21 22:12:32 +0100 | [diff] [blame] | 161 | } |
| 162 | |
Simon Kelley | 8c1b6a5 | 2018-07-21 22:12:32 +0100 | [diff] [blame] | 163 | static int ubus_handle_metrics(struct ubus_context *ctx, struct ubus_object *obj, |
Simon Kelley | 6f835ed | 2018-07-29 22:15:36 +0100 | [diff] [blame] | 164 | struct ubus_request_data *req, const char *method, |
| 165 | struct blob_attr *msg) |
Simon Kelley | 8c1b6a5 | 2018-07-21 22:12:32 +0100 | [diff] [blame] | 166 | { |
Simon Kelley | 1dfed16 | 2018-07-29 22:16:41 +0100 | [diff] [blame] | 167 | int i; |
Simon Kelley | 8c1b6a5 | 2018-07-21 22:12:32 +0100 | [diff] [blame] | 168 | |
Jan Willem Janssen | a2b8220 | 2019-03-25 12:42:23 +0100 | [diff] [blame] | 169 | (void)obj; |
| 170 | (void)method; |
| 171 | (void)msg; |
| 172 | |
| 173 | blob_buf_init(&b, BLOBMSG_TYPE_TABLE); |
| 174 | |
| 175 | for (i=0; i < __METRIC_MAX; i++) |
Simon Kelley | 6f835ed | 2018-07-29 22:15:36 +0100 | [diff] [blame] | 176 | blobmsg_add_u32(&b, get_metric_name(i), daemon->metrics[i]); |
| 177 | |
Jan Willem Janssen | a2b8220 | 2019-03-25 12:42:23 +0100 | [diff] [blame] | 178 | return ubus_send_reply(ctx, req, b.head); |
Simon Kelley | 8c1b6a5 | 2018-07-21 22:12:32 +0100 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | void ubus_event_bcast(const char *type, const char *mac, const char *ip, const char *name, const char *interface) |
| 182 | { |
Jan Willem Janssen | a2b8220 | 2019-03-25 12:42:23 +0100 | [diff] [blame] | 183 | struct ubus_context *ubus = (struct ubus_context *)daemon->ubus; |
| 184 | int ret; |
| 185 | |
| 186 | if (!ubus || !notify) |
Simon Kelley | 6f835ed | 2018-07-29 22:15:36 +0100 | [diff] [blame] | 187 | return; |
Simon Kelley | 8c1b6a5 | 2018-07-21 22:12:32 +0100 | [diff] [blame] | 188 | |
Jan Willem Janssen | a2b8220 | 2019-03-25 12:42:23 +0100 | [diff] [blame] | 189 | blob_buf_init(&b, BLOBMSG_TYPE_TABLE); |
Simon Kelley | 6f835ed | 2018-07-29 22:15:36 +0100 | [diff] [blame] | 190 | if (mac) |
| 191 | blobmsg_add_string(&b, "mac", mac); |
| 192 | if (ip) |
| 193 | blobmsg_add_string(&b, "ip", ip); |
| 194 | if (name) |
| 195 | blobmsg_add_string(&b, "name", name); |
| 196 | if (interface) |
| 197 | blobmsg_add_string(&b, "interface", interface); |
| 198 | |
Jan Willem Janssen | a2b8220 | 2019-03-25 12:42:23 +0100 | [diff] [blame] | 199 | ret = ubus_notify(ubus, &ubus_object, type, b.head, -1); |
| 200 | if (!ret) |
| 201 | my_syslog(LOG_ERR, _("Failed to send UBus event: %s"), ubus_strerror(ret)); |
Simon Kelley | 8c1b6a5 | 2018-07-21 22:12:32 +0100 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | |
| 205 | #endif /* HAVE_UBUS */ |