]> the.earth.li Git - mqtt-arp.git/blob - mqtt-arp.c
Fix alignment when parsing Netlink messages
[mqtt-arp.git] / mqtt-arp.c
1 /*
2  * mqtt-arp.c - Watch the Linux ARP table to report device presence via MQTT
3  *
4  * Copyright 2018 Jonathan McDowell <noodles@earth.li>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 #include <getopt.h>
20 #include <signal.h>
21 #include <stdbool.h>
22 #include <stdint.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <strings.h>
27 #include <sys/types.h>
28 #include <sys/socket.h>
29 #include <time.h>
30 #include <unistd.h>
31 #include <linux/netlink.h>
32 #include <linux/rtnetlink.h>
33
34 #include <mosquitto.h>
35
36 /* Defaults. All overridable from command line. */
37 #define MQTT_HOST       "mqtt-host"
38 #define MQTT_PORT       8883
39 #define MQTT_TOPIC      "location/by-mac"
40 #define LOCATION        "home"
41
42 /* How often (in seconds) to report that we see a device */
43 #define REPORT_INTERVAL (2 * 60)
44 /* How long to wait without seeing a device before reporting it's gone */
45 #define EXPIRY_TIME     (10 * 60)
46 /* Maximum number of MAC addresses to watch for */
47 #define MAX_MACS        8
48
49 struct mac_entry {
50         bool valid;
51         uint8_t mac[6];
52         time_t last_seen;
53         time_t last_reported;
54 };
55
56 struct ma_config {
57         char *mqtt_host;
58         int mqtt_port;
59         char *mqtt_username;
60         char *mqtt_password;
61         char *mqtt_topic;
62         char *location;
63         char *capath;
64         struct mac_entry macs[MAX_MACS];
65 };
66
67 bool debug = false;
68 bool want_shutdown = false;
69
70 void shutdown_request(int signal)
71 {
72         want_shutdown = true;
73 }
74
75 bool mac_compare(uint8_t *a, uint8_t *b)
76 {
77         int i;
78
79         for (i = 0; i < 6; i++)
80                 if (a[i] != b[i])
81                         return false;
82
83         if (debug)
84                 printf("Matched: %02x:%02x:%02x:%02x:%02x:%02x\n",
85                                 a[0], a[1], a[2],
86                                 a[3], a[4], a[5]);
87
88         return true;
89 }
90
91 int mqtt_mac_presence(struct ma_config *config, struct mosquitto *mosq,
92                 uint8_t *mac, bool present)
93 {
94         char topic[128];
95         int ret;
96         time_t t;
97         int i;
98
99         t = time(NULL);
100
101         i = 0;
102         while (i < MAX_MACS && config->macs[i].valid) {
103                 if (mac_compare(mac, config->macs[i].mac))
104                         break;
105                 i++;
106         }
107
108         if (i >= MAX_MACS || !config->macs[i].valid)
109                 return 0;
110
111         config->macs[i].last_seen = t;
112         /* Report no more often than every 2 minutes */
113         if (present && config->macs[i].last_reported + REPORT_INTERVAL > t)
114                 return 0;
115
116         config->macs[i].last_reported = t;
117
118         snprintf(topic, sizeof(topic),
119                 "%s/%02X:%02X:%02X:%02X:%02X:%02X",
120                 config->mqtt_topic,
121                 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
122
123         if (debug)
124                 printf("Publishing to %s\n", topic);
125
126         if (present)
127                 ret = mosquitto_publish(mosq, NULL, topic,
128                                 strlen(config->location), config->location,
129                                 0, 0);
130         else
131                 ret = mosquitto_publish(mosq, NULL, topic,
132                                 strlen("unknown"), "unknown", 0, 0);
133
134         return ret;
135 }
136
137 void prune_macs(struct ma_config *config, struct mosquitto *mosq)
138 {
139         time_t t;
140         int i;
141
142         t = time(NULL);
143
144         i = 0;
145         while (i < MAX_MACS && config->macs[i].valid) {
146                 /* Expire if we haven't seen MAC in EXPIRY_TIME */
147                 if (config->macs[i].last_seen &&
148                                 config->macs[i].last_seen + EXPIRY_TIME < t) {
149                         mqtt_mac_presence(config, mosq,
150                                         config->macs[i].mac, false);
151                         config->macs[i].last_seen = 0;
152                         config->macs[i].last_reported = 0;
153                 }
154                 i++;
155         }
156 }
157
158 void mosq_log_callback(struct mosquitto *mosq, void *userdata, int level,
159                 const char *str)
160 {
161         if (debug)
162                 printf("%i:%s\n", level, str);
163 }
164
165 void main_loop(struct ma_config *config, struct mosquitto *mosq, int sock)
166 {
167         uint8_t buf[4096];
168         uint8_t *data;
169         struct nlmsghdr *hdr;
170         struct ndmsg *nd;
171         struct nlattr *attr;
172         ssize_t received;
173         time_t t;
174
175         hdr = (struct nlmsghdr *) buf;
176         nd = (struct ndmsg *) (hdr + 1);
177         while (!want_shutdown) {
178                 received = recv(sock, buf, sizeof(buf), 0);
179                 if (debug) {
180                         t = time(NULL);
181                         printf("%sReceived %zd bytes:\n", ctime(&t), received);
182                         printf("  Len: %d, type: %d, flags: %x, "
183                                 "seq: %d, pid: %d\n",
184                                 hdr->nlmsg_len, hdr->nlmsg_type,
185                                 hdr->nlmsg_flags, hdr->nlmsg_seq,
186                                 hdr->nlmsg_pid);
187                 }
188                 switch (hdr->nlmsg_type) {
189                 case RTM_NEWNEIGH:
190                         if (debug) {
191                                 printf("  Family: %d, interface: %d, "
192                                         "state: %x, flags: %x, type: %x\n",
193                                         nd->ndm_family, /* AF_INET etc */
194                                         nd->ndm_ifindex,
195                                         nd->ndm_state, /* NUD_REACHABLE etc */
196                                         nd->ndm_flags,
197                                         nd->ndm_type);
198                         }
199                         attr = (struct nlattr *) (nd + 1);
200                         while (((uint8_t *) attr - buf) < hdr->nlmsg_len) {
201                                 data = (((uint8_t *) attr) + NLA_HDRLEN);
202                                 if (attr->nla_type == NDA_LLADDR &&
203                                         nd->ndm_state == NUD_REACHABLE) {
204                                         mqtt_mac_presence(config, mosq,
205                                                         data, true);
206                                 }
207                                 attr = (struct nlattr *) (((uint8_t *) attr) +
208                                                 NLA_ALIGN(attr->nla_len));
209                         }
210                         break;
211                 case RTM_DELNEIGH:
212                 case RTM_GETNEIGH:
213                 default:
214                         printf("Unknown message type: %d\n", hdr->nlmsg_type);
215                 }
216
217                 prune_macs(config, mosq);
218         }
219
220 }
221
222 struct mosquitto *mqtt_init(struct ma_config *config)
223 {
224         struct mosquitto *mosq;
225         int ret;
226
227         mosquitto_lib_init();
228         mosq = mosquitto_new("mqtt-arp", true, NULL);
229         if (!mosq) {
230                 printf("Couldn't allocate mosquitto structure\n");
231                 exit(EXIT_FAILURE);
232         }
233
234         mosquitto_log_callback_set(mosq, mosq_log_callback);
235
236         /* DTRT if username is NULL */
237         mosquitto_username_pw_set(mosq,
238                         config->mqtt_username,
239                         config->mqtt_password);
240         if (config->capath)
241                 mosquitto_tls_set(mosq, config->capath,
242                                 NULL, NULL, NULL, NULL);
243
244         ret = mosquitto_connect(mosq, config->mqtt_host,
245                         config->mqtt_port, 60);
246         if (ret) {
247                 printf("Unable to connect to MQTT server.\n");
248                 exit(EXIT_FAILURE);
249         }
250
251         ret = mosquitto_loop_start(mosq);
252         if (ret) {
253                 printf("Unable to start Mosquitto loop.\n");
254                 exit(EXIT_FAILURE);
255         }
256
257         return mosq;
258 }
259
260 int netlink_init(void)
261 {
262         int sock;
263         struct sockaddr_nl group_addr;
264
265         bzero(&group_addr, sizeof(group_addr));
266         group_addr.nl_family = AF_NETLINK;
267         group_addr.nl_pid = getpid();
268         group_addr.nl_groups = RTMGRP_NEIGH;
269
270         sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
271         if (sock < 0) {
272                 perror("Couldn't open netlink socket");
273                 exit(EXIT_FAILURE);
274         }
275
276         if (bind(sock, (struct sockaddr *) &group_addr,
277                         sizeof(group_addr)) < 0) {
278                 perror("Failed to bind to netlink socket");
279                 exit(EXIT_FAILURE);
280         }
281
282         return sock;
283 }
284
285 struct option long_options[] = {
286         { "capath", required_argument, 0, 'c' },
287         { "host", required_argument, 0, 'h' },
288         { "location", required_argument, 0, 'l' },
289         { "mac", required_argument, 0, 'm' },
290         { "password", required_argument, 0, 'P' },
291         { "port", required_argument, 0, 'p' },
292         { "topic", required_argument, 0, 't' },
293         { "username", required_argument, 0, 'u' },
294         { "verbose", no_argument, 0, 'v' },
295         { 0, 0, 0, 0 }
296 };
297
298 int main(int argc, char *argv[])
299 {
300         int sock;
301         struct mosquitto *mosq;
302         struct ma_config config;
303         int option_index = 0;
304         int macs = 0;
305         char c;
306
307         bzero(&config, sizeof(config));
308         config.mqtt_port = MQTT_PORT;
309
310         while (1) {
311                 c = getopt_long(argc, argv, "c:h:l:m:p:P:t:u:v",
312                                 long_options, &option_index);
313
314                 if (c == -1)
315                         break;
316
317                 switch (c) {
318                 case 'c':
319                         config.capath = optarg;
320                         break;
321                 case 'h':
322                         config.mqtt_host = optarg;
323                         break;
324                 case 'l':
325                         config.location = optarg;
326                         break;
327                 case 'm':
328                         if (macs >= MAX_MACS) {
329                                 printf("Can only accept %d MAC addresses to"
330                                         " watch for.\n", MAX_MACS);
331                                 exit(EXIT_FAILURE);
332                         }
333                         sscanf(optarg,
334                                 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
335                                 &config.macs[macs].mac[0],
336                                 &config.macs[macs].mac[1],
337                                 &config.macs[macs].mac[2],
338                                 &config.macs[macs].mac[3],
339                                 &config.macs[macs].mac[4],
340                                 &config.macs[macs].mac[5]);
341                         config.macs[macs].valid = true;
342                         macs++;
343                         break;
344                 case 'p':
345                         config.mqtt_port = atoi(optarg);
346                         break;
347                 case 'P':
348                         config.mqtt_password = optarg;
349                         break;
350                 case 't':
351                         config.mqtt_topic = optarg;
352                         break;
353                 case 'u':
354                         config.mqtt_username = optarg;
355                         break;
356                 case 'v':
357                         debug = true;
358                         break;
359                 default:
360                         printf("Unrecognized option: %c\n", c);
361                         exit(EXIT_FAILURE);
362                 }
363         }
364
365         if (!config.mqtt_host)
366                 config.mqtt_host = MQTT_HOST;
367         if (!config.mqtt_topic)
368                 config.mqtt_host = MQTT_TOPIC;
369         if (!config.location)
370                 config.mqtt_host = LOCATION;
371
372         signal(SIGTERM, shutdown_request);
373
374         sock = netlink_init();
375         mosq = mqtt_init(&config);
376
377         main_loop(&config, mosq, sock);
378
379         mosquitto_disconnect(mosq);
380         mosquitto_loop_stop(mosq, true);
381         mosquitto_destroy(mosq);
382         mosquitto_lib_cleanup();
383         close(sock);
384 }