]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-device/device-monitor.c
Merge pull request #10967 from evverx/check-directives
[thirdparty/systemd.git] / src / libsystemd / sd-device / device-monitor.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <errno.h>
4 #include <linux/filter.h>
5 #include <linux/netlink.h>
6 #include <sys/socket.h>
7
8 #include "sd-device.h"
9 #include "sd-event.h"
10
11 #include "MurmurHash2.h"
12 #include "alloc-util.h"
13 #include "device-monitor-private.h"
14 #include "device-private.h"
15 #include "device-util.h"
16 #include "fd-util.h"
17 #include "format-util.h"
18 #include "hashmap.h"
19 #include "io-util.h"
20 #include "missing.h"
21 #include "mount-util.h"
22 #include "set.h"
23 #include "socket-util.h"
24 #include "string-util.h"
25 #include "strv.h"
26
27 struct sd_device_monitor {
28 unsigned n_ref;
29
30 int sock;
31 union sockaddr_union snl;
32 union sockaddr_union snl_trusted_sender;
33 bool bound;
34
35 Hashmap *subsystem_filter;
36 Set *tag_filter;
37 bool filter_uptodate;
38
39 sd_event *event;
40 sd_event_source *event_source;
41 sd_device_monitor_handler_t callback;
42 void *userdata;
43 };
44
45 #define UDEV_MONITOR_MAGIC 0xfeedcafe
46
47 typedef struct monitor_netlink_header {
48 /* "libudev" prefix to distinguish libudev and kernel messages */
49 char prefix[8];
50 /* Magic to protect against daemon <-> Library message format mismatch
51 * Used in the kernel from socket filter rules; needs to be stored in network order */
52 unsigned magic;
53 /* Total length of header structure known to the sender */
54 unsigned header_size;
55 /* Properties string buffer */
56 unsigned properties_off;
57 unsigned properties_len;
58 /* Hashes of primary device properties strings, to let libudev subscribers
59 * use in-kernel socket filters; values need to be stored in network order */
60 unsigned filter_subsystem_hash;
61 unsigned filter_devtype_hash;
62 unsigned filter_tag_bloom_hi;
63 unsigned filter_tag_bloom_lo;
64 } monitor_netlink_header;
65
66 static int monitor_set_nl_address(sd_device_monitor *m) {
67 union sockaddr_union snl;
68 socklen_t addrlen;
69
70 assert(m);
71
72 /* Get the address the kernel has assigned us.
73 * It is usually, but not necessarily the pid. */
74 addrlen = sizeof(struct sockaddr_nl);
75 if (getsockname(m->sock, &snl.sa, &addrlen) < 0)
76 return -errno;
77
78 m->snl.nl.nl_pid = snl.nl.nl_pid;
79 return 0;
80 }
81
82 int device_monitor_allow_unicast_sender(sd_device_monitor *m, sd_device_monitor *sender) {
83 assert_return(m, -EINVAL);
84 assert_return(sender, -EINVAL);
85
86 m->snl_trusted_sender.nl.nl_pid = sender->snl.nl.nl_pid;
87 return 0;
88 }
89
90 _public_ int sd_device_monitor_set_receive_buffer_size(sd_device_monitor *m, size_t size) {
91 int r, n = (int) size;
92
93 assert_return(m, -EINVAL);
94 assert_return((size_t) n == size, -EINVAL);
95
96 if (setsockopt_int(m->sock, SOL_SOCKET, SO_RCVBUF, n) < 0) {
97 r = setsockopt_int(m->sock, SOL_SOCKET, SO_RCVBUFFORCE, n);
98 if (r < 0)
99 return r;
100 }
101
102 return 0;
103 }
104
105 int device_monitor_disconnect(sd_device_monitor *m) {
106 assert(m);
107
108 m->sock = safe_close(m->sock);
109 return 0;
110 }
111
112 int device_monitor_get_fd(sd_device_monitor *m) {
113 assert_return(m, -EINVAL);
114
115 return m->sock;
116 }
117
118 int device_monitor_new_full(sd_device_monitor **ret, MonitorNetlinkGroup group, int fd) {
119 _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *m = NULL;
120 _cleanup_close_ int sock = -1;
121 int r;
122
123 assert_return(ret, -EINVAL);
124 assert_return(group >= 0 && group < _MONITOR_NETLINK_GROUP_MAX, -EINVAL);
125
126 if (group == MONITOR_GROUP_UDEV &&
127 access("/run/udev/control", F_OK) < 0 &&
128 dev_is_devtmpfs() <= 0) {
129
130 /*
131 * We do not support subscribing to uevents if no instance of
132 * udev is running. Uevents would otherwise broadcast the
133 * processing data of the host into containers, which is not
134 * desired.
135 *
136 * Containers will currently not get any udev uevents, until
137 * a supporting infrastructure is available.
138 *
139 * We do not set a netlink multicast group here, so the socket
140 * will not receive any messages.
141 */
142
143 log_debug("sd-device-monitor: The udev service seems not to be active, disabling the monitor");
144 group = MONITOR_GROUP_NONE;
145 }
146
147 if (fd < 0) {
148 sock = socket(PF_NETLINK, SOCK_RAW|SOCK_CLOEXEC|SOCK_NONBLOCK, NETLINK_KOBJECT_UEVENT);
149 if (sock < 0)
150 return log_debug_errno(errno, "sd-device-monitor: Failed to create socket: %m");
151 }
152
153 m = new(sd_device_monitor, 1);
154 if (!m)
155 return -ENOMEM;
156
157 *m = (sd_device_monitor) {
158 .n_ref = 1,
159 .sock = fd >= 0 ? fd : TAKE_FD(sock),
160 .bound = fd >= 0,
161 .snl.nl.nl_family = AF_NETLINK,
162 .snl.nl.nl_groups = group,
163 };
164
165 if (fd >= 0) {
166 r = monitor_set_nl_address(m);
167 if (r < 0)
168 return log_debug_errno(r, "sd-device-monitor: Failed to set netlink address: %m");
169 }
170
171 *ret = TAKE_PTR(m);
172 return 0;
173 }
174
175 _public_ int sd_device_monitor_new(sd_device_monitor **ret) {
176 return device_monitor_new_full(ret, MONITOR_GROUP_UDEV, -1);
177 }
178
179 _public_ int sd_device_monitor_stop(sd_device_monitor *m) {
180 assert_return(m, -EINVAL);
181
182 m->event_source = sd_event_source_unref(m->event_source);
183 (void) device_monitor_disconnect(m);
184
185 return 0;
186 }
187
188 static int device_monitor_event_handler(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
189 _cleanup_(sd_device_unrefp) sd_device *device = NULL;
190 sd_device_monitor *m = userdata;
191
192 assert(m);
193
194 if (device_monitor_receive_device(m, &device) <= 0)
195 return 0;
196
197 if (m->callback)
198 return m->callback(m, device, m->userdata);
199
200 return 0;
201 }
202
203 _public_ int sd_device_monitor_start(sd_device_monitor *m, sd_device_monitor_handler_t callback, void *userdata) {
204 int r;
205
206 assert_return(m, -EINVAL);
207
208 if (!m->event) {
209 r = sd_device_monitor_attach_event(m, NULL);
210 if (r < 0)
211 return r;
212 }
213
214 if (!m->bound) {
215 r = device_monitor_enable_receiving(m);
216 if (r < 0)
217 return r;
218 }
219
220 m->callback = callback;
221 m->userdata = userdata;
222
223 r = sd_event_add_io(m->event, &m->event_source, m->sock, EPOLLIN, device_monitor_event_handler, m);
224 if (r < 0)
225 return r;
226
227 (void) sd_event_source_set_description(m->event_source, "sd-device-monitor");
228
229 return 0;
230 }
231
232 _public_ int sd_device_monitor_detach_event(sd_device_monitor *m) {
233 assert_return(m, -EINVAL);
234
235 (void) sd_device_monitor_stop(m);
236 m->event = sd_event_unref(m->event);
237
238 return 0;
239 }
240
241 _public_ int sd_device_monitor_attach_event(sd_device_monitor *m, sd_event *event) {
242 int r;
243
244 assert_return(m, -EINVAL);
245 assert_return(!m->event, -EBUSY);
246
247 if (event)
248 m->event = sd_event_ref(event);
249 else {
250 r = sd_event_default(&m->event);
251 if (r < 0)
252 return r;
253 }
254
255 return 0;
256 }
257
258 _public_ sd_event *sd_device_monitor_get_event(sd_device_monitor *m) {
259 assert_return(m, NULL);
260
261 return m->event;
262 }
263
264 _public_ sd_event_source *sd_device_monitor_get_event_source(sd_device_monitor *m) {
265 assert_return(m, NULL);
266
267 return m->event_source;
268 }
269
270 int device_monitor_enable_receiving(sd_device_monitor *m) {
271 int r;
272
273 assert_return(m, -EINVAL);
274
275 r = sd_device_monitor_filter_update(m);
276 if (r < 0)
277 return log_debug_errno(r, "sd-device-monitor: Failed to update filter: %m");
278
279 if (!m->bound) {
280 if (bind(m->sock, &m->snl.sa, sizeof(struct sockaddr_nl)) < 0)
281 return log_debug_errno(errno, "sd-device-monitor: Failed to bind monitoring socket: %m");
282
283 m->bound = true;
284 }
285
286 r = monitor_set_nl_address(m);
287 if (r < 0)
288 return log_debug_errno(r, "sd-device-monitor: Failed to set address: %m");
289
290 /* enable receiving of sender credentials */
291 r = setsockopt_int(m->sock, SOL_SOCKET, SO_PASSCRED, true);
292 if (r < 0)
293 return log_debug_errno(r, "sd-device-monitor: Failed to set socket option SO_PASSCRED: %m");
294
295 return 0;
296 }
297
298 static sd_device_monitor *device_monitor_free(sd_device_monitor *m) {
299 assert(m);
300
301 (void) sd_device_monitor_detach_event(m);
302
303 hashmap_free_free_free(m->subsystem_filter);
304 set_free_free(m->tag_filter);
305
306 return mfree(m);
307 }
308
309 DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_device_monitor, sd_device_monitor, device_monitor_free);
310
311 static int passes_filter(sd_device_monitor *m, sd_device *device) {
312 const char *tag, *subsystem, *devtype, *s, *d = NULL;
313 Iterator i;
314 int r;
315
316 assert_return(m, -EINVAL);
317 assert_return(device, -EINVAL);
318
319 if (hashmap_isempty(m->subsystem_filter))
320 goto tag;
321
322 r = sd_device_get_subsystem(device, &s);
323 if (r < 0)
324 return r;
325
326 r = sd_device_get_devtype(device, &d);
327 if (r < 0 && r != -ENOENT)
328 return r;
329
330 HASHMAP_FOREACH_KEY(devtype, subsystem, m->subsystem_filter, i) {
331 if (!streq(s, subsystem))
332 continue;
333
334 if (!devtype)
335 goto tag;
336
337 if (!d)
338 continue;
339
340 if (streq(d, devtype))
341 goto tag;
342 }
343
344 return 0;
345
346 tag:
347 if (set_isempty(m->tag_filter))
348 return 1;
349
350 SET_FOREACH(tag, m->tag_filter, i)
351 if (sd_device_has_tag(device, tag) > 0)
352 return 1;
353
354 return 0;
355 }
356
357 int device_monitor_receive_device(sd_device_monitor *m, sd_device **ret) {
358 _cleanup_(sd_device_unrefp) sd_device *device = NULL;
359 union {
360 monitor_netlink_header nlh;
361 char raw[8192];
362 } buf;
363 struct iovec iov = {
364 .iov_base = &buf,
365 .iov_len = sizeof(buf)
366 };
367 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
368 union sockaddr_union snl;
369 struct msghdr smsg = {
370 .msg_iov = &iov,
371 .msg_iovlen = 1,
372 .msg_control = cred_msg,
373 .msg_controllen = sizeof(cred_msg),
374 .msg_name = &snl,
375 .msg_namelen = sizeof(snl),
376 };
377 struct cmsghdr *cmsg;
378 struct ucred *cred;
379 ssize_t buflen, bufpos;
380 bool is_initialized = false;
381 int r;
382
383 assert(ret);
384
385 buflen = recvmsg(m->sock, &smsg, 0);
386 if (buflen < 0) {
387 if (errno != EINTR)
388 log_debug_errno(errno, "sd-device-monitor: Failed to receive message: %m");
389 return -errno;
390 }
391
392 if (buflen < 32 || (smsg.msg_flags & MSG_TRUNC))
393 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
394 "sd-device-monitor: Invalid message length.");
395
396 if (snl.nl.nl_groups == MONITOR_GROUP_NONE) {
397 /* unicast message, check if we trust the sender */
398 if (m->snl_trusted_sender.nl.nl_pid == 0 ||
399 snl.nl.nl_pid != m->snl_trusted_sender.nl.nl_pid)
400 return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN),
401 "sd-device-monitor: Unicast netlink message ignored.");
402
403 } else if (snl.nl.nl_groups == MONITOR_GROUP_KERNEL) {
404 if (snl.nl.nl_pid > 0)
405 return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN),
406 "sd-device-monitor: Multicast kernel netlink message from PID %"PRIu32" ignored.", snl.nl.nl_pid);
407 }
408
409 cmsg = CMSG_FIRSTHDR(&smsg);
410 if (!cmsg || cmsg->cmsg_type != SCM_CREDENTIALS)
411 return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN),
412 "sd-device-monitor: No sender credentials received, message ignored.");
413
414 cred = (struct ucred*) CMSG_DATA(cmsg);
415 if (cred->uid != 0)
416 return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN),
417 "sd-device-monitor: Sender uid="UID_FMT", message ignored.", cred->uid);
418
419 if (streq(buf.raw, "libudev")) {
420 /* udev message needs proper version magic */
421 if (buf.nlh.magic != htobe32(UDEV_MONITOR_MAGIC))
422 return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN),
423 "sd-device-monitor: Invalid message signature (%x != %x)",
424 buf.nlh.magic, htobe32(UDEV_MONITOR_MAGIC));
425
426 if (buf.nlh.properties_off+32 > (size_t) buflen)
427 return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN),
428 "sd-device-monitor: Invalid message length (%u > %zd)",
429 buf.nlh.properties_off+32, buflen);
430
431 bufpos = buf.nlh.properties_off;
432
433 /* devices received from udev are always initialized */
434 is_initialized = true;
435
436 } else {
437 /* kernel message with header */
438 bufpos = strlen(buf.raw) + 1;
439 if ((size_t) bufpos < sizeof("a@/d") || bufpos >= buflen)
440 return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN),
441 "sd-device-monitor: Invalid message length");
442
443 /* check message header */
444 if (!strstr(buf.raw, "@/"))
445 return log_debug_errno(SYNTHETIC_ERRNO(EAGAIN),
446 "sd-device-monitor: Invalid message header");
447 }
448
449 r = device_new_from_nulstr(&device, (uint8_t*) &buf.raw[bufpos], buflen - bufpos);
450 if (r < 0)
451 return log_debug_errno(r, "sd-device-monitor: Failed to create device from received message: %m");
452
453 if (is_initialized)
454 device_set_is_initialized(device);
455
456 /* Skip device, if it does not pass the current filter */
457 r = passes_filter(m, device);
458 if (r < 0)
459 return log_device_debug_errno(device, r, "sd-device-monitor: Failed to check received device passing filter: %m");
460 if (r == 0)
461 log_device_debug(device, "sd-device-monitor: Received device does not pass filter, ignoring");
462 else
463 *ret = TAKE_PTR(device);
464
465 return r;
466 }
467
468 static uint32_t string_hash32(const char *str) {
469 return MurmurHash2(str, strlen(str), 0);
470 }
471
472 /* Get a bunch of bit numbers out of the hash, and set the bits in our bit field */
473 static uint64_t string_bloom64(const char *str) {
474 uint64_t bits = 0;
475 uint32_t hash = string_hash32(str);
476
477 bits |= 1LLU << (hash & 63);
478 bits |= 1LLU << ((hash >> 6) & 63);
479 bits |= 1LLU << ((hash >> 12) & 63);
480 bits |= 1LLU << ((hash >> 18) & 63);
481 return bits;
482 }
483
484 int device_monitor_send_device(
485 sd_device_monitor *m,
486 sd_device_monitor *destination,
487 sd_device *device) {
488
489 monitor_netlink_header nlh = {
490 .prefix = "libudev",
491 .magic = htobe32(UDEV_MONITOR_MAGIC),
492 .header_size = sizeof nlh,
493 };
494 struct iovec iov[2] = {
495 { .iov_base = &nlh, .iov_len = sizeof nlh },
496 };
497 struct msghdr smsg = {
498 .msg_iov = iov,
499 .msg_iovlen = 2,
500 };
501 /* default destination for sending */
502 union sockaddr_union default_destination = {
503 .nl.nl_family = AF_NETLINK,
504 .nl.nl_groups = MONITOR_GROUP_UDEV,
505 };
506 uint64_t tag_bloom_bits;
507 const char *buf, *val;
508 ssize_t count;
509 size_t blen;
510 int r;
511
512 assert(m);
513 assert(device);
514
515 r = device_get_properties_nulstr(device, (const uint8_t **) &buf, &blen);
516 if (r < 0)
517 return log_device_debug_errno(device, r, "sd-device-monitor: Failed to get device properties: %m");
518 if (blen < 32) {
519 log_device_debug(device, "sd-device-monitor: Length of device property nulstr is too small to contain valid device information");
520 return -EINVAL;
521 }
522
523 /* fill in versioned header */
524 r = sd_device_get_subsystem(device, &val);
525 if (r < 0)
526 return log_device_debug_errno(device, r, "sd-device-monitor: Failed to get device subsystem: %m");
527 nlh.filter_subsystem_hash = htobe32(string_hash32(val));
528
529 if (sd_device_get_devtype(device, &val) >= 0)
530 nlh.filter_devtype_hash = htobe32(string_hash32(val));
531
532 /* add tag bloom filter */
533 tag_bloom_bits = 0;
534 FOREACH_DEVICE_TAG(device, val)
535 tag_bloom_bits |= string_bloom64(val);
536
537 if (tag_bloom_bits > 0) {
538 nlh.filter_tag_bloom_hi = htobe32(tag_bloom_bits >> 32);
539 nlh.filter_tag_bloom_lo = htobe32(tag_bloom_bits & 0xffffffff);
540 }
541
542 /* add properties list */
543 nlh.properties_off = iov[0].iov_len;
544 nlh.properties_len = blen;
545 iov[1] = IOVEC_MAKE((char*) buf, blen);
546
547 /*
548 * Use custom address for target, or the default one.
549 *
550 * If we send to a multicast group, we will get
551 * ECONNREFUSED, which is expected.
552 */
553 smsg.msg_name = destination ? &destination->snl : &default_destination;
554 smsg.msg_namelen = sizeof(struct sockaddr_nl);
555 count = sendmsg(m->sock, &smsg, 0);
556 if (count < 0) {
557 if (!destination && errno == ECONNREFUSED) {
558 log_device_debug(device, "sd-device-monitor: Passed to netlink monitor");
559 return 0;
560 } else
561 return log_device_debug_errno(device, errno, "sd-device-monitor: Failed to send device to netlink monitor: %m");
562 }
563
564 log_device_debug(device, "sd-device-monitor: Passed %zi byte to netlink monitor", count);
565 return count;
566 }
567
568 static void bpf_stmt(struct sock_filter *ins, unsigned *i,
569 unsigned short code, unsigned data) {
570 ins[(*i)++] = (struct sock_filter) {
571 .code = code,
572 .k = data,
573 };
574 }
575
576 static void bpf_jmp(struct sock_filter *ins, unsigned *i,
577 unsigned short code, unsigned data,
578 unsigned short jt, unsigned short jf) {
579 ins[(*i)++] = (struct sock_filter) {
580 .code = code,
581 .jt = jt,
582 .jf = jf,
583 .k = data,
584 };
585 }
586
587 _public_ int sd_device_monitor_filter_update(sd_device_monitor *m) {
588 struct sock_filter ins[512] = {};
589 struct sock_fprog filter;
590 const char *subsystem, *devtype, *tag;
591 unsigned i = 0;
592 Iterator it;
593
594 assert_return(m, -EINVAL);
595
596 if (m->filter_uptodate)
597 return 0;
598
599 if (hashmap_isempty(m->subsystem_filter) &&
600 set_isempty(m->tag_filter)) {
601 m->filter_uptodate = true;
602 return 0;
603 }
604
605 /* load magic in A */
606 bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(monitor_netlink_header, magic));
607 /* jump if magic matches */
608 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, UDEV_MONITOR_MAGIC, 1, 0);
609 /* wrong magic, pass packet */
610 bpf_stmt(ins, &i, BPF_RET|BPF_K, 0xffffffff);
611
612 if (!set_isempty(m->tag_filter)) {
613 int tag_matches = set_size(m->tag_filter);
614
615 /* add all tags matches */
616 SET_FOREACH(tag, m->tag_filter, it) {
617 uint64_t tag_bloom_bits = string_bloom64(tag);
618 uint32_t tag_bloom_hi = tag_bloom_bits >> 32;
619 uint32_t tag_bloom_lo = tag_bloom_bits & 0xffffffff;
620
621 /* load device bloom bits in A */
622 bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(monitor_netlink_header, filter_tag_bloom_hi));
623 /* clear bits (tag bits & bloom bits) */
624 bpf_stmt(ins, &i, BPF_ALU|BPF_AND|BPF_K, tag_bloom_hi);
625 /* jump to next tag if it does not match */
626 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, tag_bloom_hi, 0, 3);
627
628 /* load device bloom bits in A */
629 bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(monitor_netlink_header, filter_tag_bloom_lo));
630 /* clear bits (tag bits & bloom bits) */
631 bpf_stmt(ins, &i, BPF_ALU|BPF_AND|BPF_K, tag_bloom_lo);
632 /* jump behind end of tag match block if tag matches */
633 tag_matches--;
634 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, tag_bloom_lo, 1 + (tag_matches * 6), 0);
635 }
636
637 /* nothing matched, drop packet */
638 bpf_stmt(ins, &i, BPF_RET|BPF_K, 0);
639 }
640
641 /* add all subsystem matches */
642 if (!hashmap_isempty(m->subsystem_filter)) {
643 HASHMAP_FOREACH_KEY(devtype, subsystem, m->subsystem_filter, it) {
644 uint32_t hash = string_hash32(subsystem);
645
646 /* load device subsystem value in A */
647 bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(monitor_netlink_header, filter_subsystem_hash));
648 if (!devtype) {
649 /* jump if subsystem does not match */
650 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, hash, 0, 1);
651 } else {
652 /* jump if subsystem does not match */
653 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, hash, 0, 3);
654 /* load device devtype value in A */
655 bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(monitor_netlink_header, filter_devtype_hash));
656 /* jump if value does not match */
657 hash = string_hash32(devtype);
658 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, hash, 0, 1);
659 }
660
661 /* matched, pass packet */
662 bpf_stmt(ins, &i, BPF_RET|BPF_K, 0xffffffff);
663
664 if (i+1 >= ELEMENTSOF(ins))
665 return -E2BIG;
666 }
667
668 /* nothing matched, drop packet */
669 bpf_stmt(ins, &i, BPF_RET|BPF_K, 0);
670 }
671
672 /* matched, pass packet */
673 bpf_stmt(ins, &i, BPF_RET|BPF_K, 0xffffffff);
674
675 /* install filter */
676 filter = (struct sock_fprog) {
677 .len = i,
678 .filter = ins,
679 };
680 if (setsockopt(m->sock, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter)) < 0)
681 return -errno;
682
683 m->filter_uptodate = true;
684 return 0;
685 }
686
687 _public_ int sd_device_monitor_filter_add_match_subsystem_devtype(sd_device_monitor *m, const char *subsystem, const char *devtype) {
688 _cleanup_free_ char *s = NULL, *d = NULL;
689 int r;
690
691 assert_return(m, -EINVAL);
692 assert_return(subsystem, -EINVAL);
693
694 s = strdup(subsystem);
695 if (!s)
696 return -ENOMEM;
697
698 if (devtype) {
699 d = strdup(devtype);
700 if (!d)
701 return -ENOMEM;
702 }
703
704 r = hashmap_ensure_allocated(&m->subsystem_filter, NULL);
705 if (r < 0)
706 return r;
707
708 r = hashmap_put(m->subsystem_filter, s, d);
709 if (r < 0)
710 return r;
711
712 s = d = NULL;
713 m->filter_uptodate = false;
714
715 return 0;
716 }
717
718 _public_ int sd_device_monitor_filter_add_match_tag(sd_device_monitor *m, const char *tag) {
719 _cleanup_free_ char *t = NULL;
720 int r;
721
722 assert_return(m, -EINVAL);
723 assert_return(tag, -EINVAL);
724
725 t = strdup(tag);
726 if (!t)
727 return -ENOMEM;
728
729 r = set_ensure_allocated(&m->tag_filter, &string_hash_ops);
730 if (r < 0)
731 return r;
732
733 r = set_put(m->tag_filter, t);
734 if (r == -EEXIST)
735 return 0;
736 if (r < 0)
737 return r;
738
739 TAKE_PTR(t);
740 m->filter_uptodate = false;
741
742 return 0;
743 }
744
745 _public_ int sd_device_monitor_filter_remove(sd_device_monitor *m) {
746 static const struct sock_fprog filter = { 0, NULL };
747
748 assert_return(m, -EINVAL);
749
750 m->subsystem_filter = hashmap_free_free_free(m->subsystem_filter);
751 m->tag_filter = set_free_free(m->tag_filter);
752
753 if (setsockopt(m->sock, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter)) < 0)
754 return -errno;
755
756 m->filter_uptodate = true;
757 return 0;
758 }