1 /***
2 This file is part of systemd.
3
4 Copyright 2008-2012 Kay Sievers <kay@vrfy.org>
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <stddef.h>
23 #include <unistd.h>
24 #include <errno.h>
25 #include <string.h>
26 #include <dirent.h>
27 #include <poll.h>
28 #include <sys/stat.h>
29 #include <sys/socket.h>
30 #include <sys/un.h>
31 #include <arpa/inet.h>
32 #include <linux/netlink.h>
33 #include <linux/filter.h>
34
35 #include "libudev.h"
36 #include "libudev-private.h"
37 #include "socket-util.h"
38 #include "missing.h"
39
40 /**
41 * SECTION:libudev-monitor
42 * @short_description: device event source
43 *
44 * Connects to a device event source.
45 */
46
47 /**
48 * udev_monitor:
49 *
50 * Opaque object handling an event source.
51 */
52 struct udev_monitor {
53 struct udev *udev;
54 int refcount;
55 int sock;
56 union sockaddr_union snl;
57 union sockaddr_union snl_trusted_sender;
58 union sockaddr_union snl_destination;
59 socklen_t addrlen;
60 struct udev_list filter_subsystem_list;
61 struct udev_list filter_tag_list;
62 bool bound;
63 };
64
65 enum udev_monitor_netlink_group {
66 UDEV_MONITOR_NONE,
67 UDEV_MONITOR_KERNEL,
68 UDEV_MONITOR_UDEV,
69 };
70
71 #define UDEV_MONITOR_MAGIC 0xfeedcafe
72 struct udev_monitor_netlink_header {
73 /* "libudev" prefix to distinguish libudev and kernel messages */
74 char prefix[8];
75 /*
76 * magic to protect against daemon <-> library message format mismatch
77 * used in the kernel from socket filter rules; needs to be stored in network order
78 */
79 unsigned int magic;
80 /* total length of header structure known to the sender */
81 unsigned int header_size;
82 /* properties string buffer */
83 unsigned int properties_off;
84 unsigned int properties_len;
85 /*
86 * hashes of primary device properties strings, to let libudev subscribers
87 * use in-kernel socket filters; values need to be stored in network order
88 */
89 unsigned int filter_subsystem_hash;
90 unsigned int filter_devtype_hash;
91 unsigned int filter_tag_bloom_hi;
92 unsigned int filter_tag_bloom_lo;
93 };
94
udev_monitor_new(struct udev * udev)95 static struct udev_monitor *udev_monitor_new(struct udev *udev)
96 {
97 struct udev_monitor *udev_monitor;
98
99 udev_monitor = new0(struct udev_monitor, 1);
100 if (udev_monitor == NULL)
101 return NULL;
102 udev_monitor->refcount = 1;
103 udev_monitor->udev = udev;
104 udev_list_init(udev, &udev_monitor->filter_subsystem_list, false);
105 udev_list_init(udev, &udev_monitor->filter_tag_list, true);
106 return udev_monitor;
107 }
108
monitor_set_nl_address(struct udev_monitor * udev_monitor)109 static void monitor_set_nl_address(struct udev_monitor *udev_monitor) {
110 union sockaddr_union snl;
111 socklen_t addrlen;
112 int r;
113
114 assert(udev_monitor);
115
116 /* get the address the kernel has assigned us
117 * it is usually, but not necessarily the pid
118 */
119 addrlen = sizeof(struct sockaddr_nl);
120 r = getsockname(udev_monitor->sock, &snl.sa, &addrlen);
121 if (r >= 0)
122 udev_monitor->snl.nl.nl_pid = snl.nl.nl_pid;
123 }
124
udev_monitor_new_from_netlink_fd(struct udev * udev,const char * name,int fd)125 struct udev_monitor *udev_monitor_new_from_netlink_fd(struct udev *udev, const char *name, int fd)
126 {
127 struct udev_monitor *udev_monitor;
128 unsigned int group;
129
130 if (udev == NULL)
131 return NULL;
132
133 if (name == NULL)
134 group = UDEV_MONITOR_NONE;
135 else if (streq(name, "udev")) {
136 /*
137 * We do not support subscribing to uevents if no instance of
138 * udev is running. Uevents would otherwise broadcast the
139 * processing data of the host into containers, which is not
140 * desired.
141 *
142 * Containers will currently not get any udev uevents, until
143 * a supporting infrastructure is available.
144 *
145 * We do not set a netlink multicast group here, so the socket
146 * will not receive any messages.
147 */
148 if (access(UDEV_ROOT_RUN "/udev/control", F_OK) < 0) {
149 log_debug("the udev service seems not to be active, disable the monitor");
150 group = UDEV_MONITOR_NONE;
151 } else
152 group = UDEV_MONITOR_UDEV;
153 } else if (streq(name, "kernel"))
154 group = UDEV_MONITOR_KERNEL;
155 else
156 return NULL;
157
158 udev_monitor = udev_monitor_new(udev);
159 if (udev_monitor == NULL)
160 return NULL;
161
162 if (fd < 0) {
163 udev_monitor->sock = socket(PF_NETLINK, SOCK_RAW|SOCK_CLOEXEC|SOCK_NONBLOCK, NETLINK_KOBJECT_UEVENT);
164 if (udev_monitor->sock < 0) {
165 log_debug_errno(errno, "error getting socket: %m");
166 free(udev_monitor);
167 return NULL;
168 }
169 } else {
170 udev_monitor->bound = true;
171 udev_monitor->sock = fd;
172 monitor_set_nl_address(udev_monitor);
173 }
174
175 udev_monitor->snl.nl.nl_family = AF_NETLINK;
176 udev_monitor->snl.nl.nl_groups = group;
177
178 /* default destination for sending */
179 udev_monitor->snl_destination.nl.nl_family = AF_NETLINK;
180 udev_monitor->snl_destination.nl.nl_groups = UDEV_MONITOR_UDEV;
181
182 return udev_monitor;
183 }
184
185 /**
186 * udev_monitor_new_from_netlink:
187 * @udev: udev library context
188 * @name: name of event source
189 *
190 * Create new udev monitor and connect to a specified event
191 * source. Valid sources identifiers are "udev" and "kernel".
192 *
193 * Applications should usually not connect directly to the
194 * "kernel" events, because the devices might not be useable
195 * at that time, before udev has configured them, and created
196 * device nodes. Accessing devices at the same time as udev,
197 * might result in unpredictable behavior. The "udev" events
198 * are sent out after udev has finished its event processing,
199 * all rules have been processed, and needed device nodes are
200 * created.
201 *
202 * The initial refcount is 1, and needs to be decremented to
203 * release the resources of the udev monitor.
204 *
205 * Returns: a new udev monitor, or #NULL, in case of an error
206 **/
udev_monitor_new_from_netlink(struct udev * udev,const char * name)207 _public_ struct udev_monitor *udev_monitor_new_from_netlink(struct udev *udev, const char *name)
208 {
209 return udev_monitor_new_from_netlink_fd(udev, name, -1);
210 }
211
bpf_stmt(struct sock_filter * inss,unsigned int * i,unsigned short code,unsigned int data)212 static inline void bpf_stmt(struct sock_filter *inss, unsigned int *i,
213 unsigned short code, unsigned int data)
214 {
215 struct sock_filter *ins = &inss[*i];
216
217 ins->code = code;
218 ins->k = data;
219 (*i)++;
220 }
221
bpf_jmp(struct sock_filter * inss,unsigned int * i,unsigned short code,unsigned int data,unsigned short jt,unsigned short jf)222 static inline void bpf_jmp(struct sock_filter *inss, unsigned int *i,
223 unsigned short code, unsigned int data,
224 unsigned short jt, unsigned short jf)
225 {
226 struct sock_filter *ins = &inss[*i];
227
228 ins->code = code;
229 ins->jt = jt;
230 ins->jf = jf;
231 ins->k = data;
232 (*i)++;
233 }
234
235 /**
236 * udev_monitor_filter_update:
237 * @udev_monitor: monitor
238 *
239 * Update the installed socket filter. This is only needed,
240 * if the filter was removed or changed.
241 *
242 * Returns: 0 on success, otherwise a negative error value.
243 */
udev_monitor_filter_update(struct udev_monitor * udev_monitor)244 _public_ int udev_monitor_filter_update(struct udev_monitor *udev_monitor)
245 {
246 struct sock_filter ins[512];
247 struct sock_fprog filter;
248 unsigned int i;
249 struct udev_list_entry *list_entry;
250 int err;
251
252 if (udev_list_get_entry(&udev_monitor->filter_subsystem_list) == NULL &&
253 udev_list_get_entry(&udev_monitor->filter_tag_list) == NULL)
254 return 0;
255
256 memzero(ins, sizeof(ins));
257 i = 0;
258
259 /* load magic in A */
260 bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, magic));
261 /* jump if magic matches */
262 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, UDEV_MONITOR_MAGIC, 1, 0);
263 /* wrong magic, pass packet */
264 bpf_stmt(ins, &i, BPF_RET|BPF_K, 0xffffffff);
265
266 if (udev_list_get_entry(&udev_monitor->filter_tag_list) != NULL) {
267 int tag_matches;
268
269 /* count tag matches, to calculate end of tag match block */
270 tag_matches = 0;
271 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_tag_list))
272 tag_matches++;
273
274 /* add all tags matches */
275 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_tag_list)) {
276 uint64_t tag_bloom_bits = util_string_bloom64(udev_list_entry_get_name(list_entry));
277 uint32_t tag_bloom_hi = tag_bloom_bits >> 32;
278 uint32_t tag_bloom_lo = tag_bloom_bits & 0xffffffff;
279
280 /* load device bloom bits in A */
281 bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_tag_bloom_hi));
282 /* clear bits (tag bits & bloom bits) */
283 bpf_stmt(ins, &i, BPF_ALU|BPF_AND|BPF_K, tag_bloom_hi);
284 /* jump to next tag if it does not match */
285 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, tag_bloom_hi, 0, 3);
286
287 /* load device bloom bits in A */
288 bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_tag_bloom_lo));
289 /* clear bits (tag bits & bloom bits) */
290 bpf_stmt(ins, &i, BPF_ALU|BPF_AND|BPF_K, tag_bloom_lo);
291 /* jump behind end of tag match block if tag matches */
292 tag_matches--;
293 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, tag_bloom_lo, 1 + (tag_matches * 6), 0);
294 }
295
296 /* nothing matched, drop packet */
297 bpf_stmt(ins, &i, BPF_RET|BPF_K, 0);
298 }
299
300 /* add all subsystem matches */
301 if (udev_list_get_entry(&udev_monitor->filter_subsystem_list) != NULL) {
302 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_subsystem_list)) {
303 unsigned int hash = util_string_hash32(udev_list_entry_get_name(list_entry));
304
305 /* load device subsystem value in A */
306 bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_subsystem_hash));
307 if (udev_list_entry_get_value(list_entry) == NULL) {
308 /* jump if subsystem does not match */
309 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, hash, 0, 1);
310 } else {
311 /* jump if subsystem does not match */
312 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, hash, 0, 3);
313
314 /* load device devtype value in A */
315 bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_devtype_hash));
316 /* jump if value does not match */
317 hash = util_string_hash32(udev_list_entry_get_value(list_entry));
318 bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, hash, 0, 1);
319 }
320
321 /* matched, pass packet */
322 bpf_stmt(ins, &i, BPF_RET|BPF_K, 0xffffffff);
323
324 if (i+1 >= ELEMENTSOF(ins))
325 return -E2BIG;
326 }
327
328 /* nothing matched, drop packet */
329 bpf_stmt(ins, &i, BPF_RET|BPF_K, 0);
330 }
331
332 /* matched, pass packet */
333 bpf_stmt(ins, &i, BPF_RET|BPF_K, 0xffffffff);
334
335 /* install filter */
336 memzero(&filter, sizeof(filter));
337 filter.len = i;
338 filter.filter = ins;
339 err = setsockopt(udev_monitor->sock, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter));
340 return err < 0 ? -errno : 0;
341 }
342
udev_monitor_allow_unicast_sender(struct udev_monitor * udev_monitor,struct udev_monitor * sender)343 int udev_monitor_allow_unicast_sender(struct udev_monitor *udev_monitor, struct udev_monitor *sender)
344 {
345 udev_monitor->snl_trusted_sender.nl.nl_pid = sender->snl.nl.nl_pid;
346 return 0;
347 }
348
349 /**
350 * udev_monitor_enable_receiving:
351 * @udev_monitor: the monitor which should receive events
352 *
353 * Binds the @udev_monitor socket to the event source.
354 *
355 * Returns: 0 on success, otherwise a negative error value.
356 */
udev_monitor_enable_receiving(struct udev_monitor * udev_monitor)357 _public_ int udev_monitor_enable_receiving(struct udev_monitor *udev_monitor)
358 {
359 int err = 0;
360 const int on = 1;
361
362 udev_monitor_filter_update(udev_monitor);
363
364 if (!udev_monitor->bound) {
365 err = bind(udev_monitor->sock,
366 &udev_monitor->snl.sa, sizeof(struct sockaddr_nl));
367 if (err == 0)
368 udev_monitor->bound = true;
369 }
370
371 if (err >= 0)
372 monitor_set_nl_address(udev_monitor);
373 else {
374 log_debug_errno(errno, "bind failed: %m");
375 return -errno;
376 }
377
378 /* enable receiving of sender credentials */
379 err = setsockopt(udev_monitor->sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
380 if (err < 0)
381 log_debug_errno(errno, "setting SO_PASSCRED failed: %m");
382
383 return 0;
384 }
385
386 /**
387 * udev_monitor_set_receive_buffer_size:
388 * @udev_monitor: the monitor which should receive events
389 * @size: the size in bytes
390 *
391 * Set the size of the kernel socket buffer. This call needs the
392 * appropriate privileges to succeed.
393 *
394 * Returns: 0 on success, otherwise -1 on error.
395 */
udev_monitor_set_receive_buffer_size(struct udev_monitor * udev_monitor,int size)396 _public_ int udev_monitor_set_receive_buffer_size(struct udev_monitor *udev_monitor, int size)
397 {
398 if (udev_monitor == NULL)
399 return -EINVAL;
400 return setsockopt(udev_monitor->sock, SOL_SOCKET, SO_RCVBUFFORCE, &size, sizeof(size));
401 }
402
udev_monitor_disconnect(struct udev_monitor * udev_monitor)403 int udev_monitor_disconnect(struct udev_monitor *udev_monitor)
404 {
405 int err;
406
407 err = close(udev_monitor->sock);
408 udev_monitor->sock = -1;
409 return err < 0 ? -errno : 0;
410 }
411
412 /**
413 * udev_monitor_ref:
414 * @udev_monitor: udev monitor
415 *
416 * Take a reference of a udev monitor.
417 *
418 * Returns: the passed udev monitor
419 **/
udev_monitor_ref(struct udev_monitor * udev_monitor)420 _public_ struct udev_monitor *udev_monitor_ref(struct udev_monitor *udev_monitor)
421 {
422 if (udev_monitor == NULL)
423 return NULL;
424 udev_monitor->refcount++;
425 return udev_monitor;
426 }
427
428 /**
429 * udev_monitor_unref:
430 * @udev_monitor: udev monitor
431 *
432 * Drop a reference of a udev monitor. If the refcount reaches zero,
433 * the bound socket will be closed, and the resources of the monitor
434 * will be released.
435 *
436 * Returns: #NULL
437 **/
udev_monitor_unref(struct udev_monitor * udev_monitor)438 _public_ struct udev_monitor *udev_monitor_unref(struct udev_monitor *udev_monitor)
439 {
440 if (udev_monitor == NULL)
441 return NULL;
442 udev_monitor->refcount--;
443 if (udev_monitor->refcount > 0)
444 return NULL;
445 if (udev_monitor->sock >= 0)
446 close(udev_monitor->sock);
447 udev_list_cleanup(&udev_monitor->filter_subsystem_list);
448 udev_list_cleanup(&udev_monitor->filter_tag_list);
449 free(udev_monitor);
450 return NULL;
451 }
452
453 /**
454 * udev_monitor_get_udev:
455 * @udev_monitor: udev monitor
456 *
457 * Retrieve the udev library context the monitor was created with.
458 *
459 * Returns: the udev library context
460 **/
udev_monitor_get_udev(struct udev_monitor * udev_monitor)461 _public_ struct udev *udev_monitor_get_udev(struct udev_monitor *udev_monitor)
462 {
463 if (udev_monitor == NULL)
464 return NULL;
465 return udev_monitor->udev;
466 }
467
468 /**
469 * udev_monitor_get_fd:
470 * @udev_monitor: udev monitor
471 *
472 * Retrieve the socket file descriptor associated with the monitor.
473 *
474 * Returns: the socket file descriptor
475 **/
udev_monitor_get_fd(struct udev_monitor * udev_monitor)476 _public_ int udev_monitor_get_fd(struct udev_monitor *udev_monitor)
477 {
478 if (udev_monitor == NULL)
479 return -EINVAL;
480 return udev_monitor->sock;
481 }
482
passes_filter(struct udev_monitor * udev_monitor,struct udev_device * udev_device)483 static int passes_filter(struct udev_monitor *udev_monitor, struct udev_device *udev_device)
484 {
485 struct udev_list_entry *list_entry;
486
487 if (udev_list_get_entry(&udev_monitor->filter_subsystem_list) == NULL)
488 goto tag;
489 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_subsystem_list)) {
490 const char *subsys = udev_list_entry_get_name(list_entry);
491 const char *dsubsys = udev_device_get_subsystem(udev_device);
492 const char *devtype;
493 const char *ddevtype;
494
495 if (!streq(dsubsys, subsys))
496 continue;
497
498 devtype = udev_list_entry_get_value(list_entry);
499 if (devtype == NULL)
500 goto tag;
501 ddevtype = udev_device_get_devtype(udev_device);
502 if (ddevtype == NULL)
503 continue;
504 if (streq(ddevtype, devtype))
505 goto tag;
506 }
507 return 0;
508
509 tag:
510 if (udev_list_get_entry(&udev_monitor->filter_tag_list) == NULL)
511 return 1;
512 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_tag_list)) {
513 const char *tag = udev_list_entry_get_name(list_entry);
514
515 if (udev_device_has_tag(udev_device, tag))
516 return 1;
517 }
518 return 0;
519 }
520
521 /**
522 * udev_monitor_receive_device:
523 * @udev_monitor: udev monitor
524 *
525 * Receive data from the udev monitor socket, allocate a new udev
526 * device, fill in the received data, and return the device.
527 *
528 * Only socket connections with uid=0 are accepted.
529 *
530 * The monitor socket is by default set to NONBLOCK. A variant of poll() on
531 * the file descriptor returned by udev_monitor_get_fd() should to be used to
532 * wake up when new devices arrive, or alternatively the file descriptor
533 * switched into blocking mode.
534 *
535 * The initial refcount is 1, and needs to be decremented to
536 * release the resources of the udev device.
537 *
538 * Returns: a new udev device, or #NULL, in case of an error
539 **/
udev_monitor_receive_device(struct udev_monitor * udev_monitor)540 _public_ struct udev_device *udev_monitor_receive_device(struct udev_monitor *udev_monitor)
541 {
542 struct udev_device *udev_device;
543 struct msghdr smsg;
544 struct iovec iov;
545 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
546 struct cmsghdr *cmsg;
547 union sockaddr_union snl;
548 struct ucred *cred;
549 union {
550 struct udev_monitor_netlink_header nlh;
551 char raw[8192];
552 } buf;
553 ssize_t buflen;
554 ssize_t bufpos;
555 bool is_initialized = false;
556
557 retry:
558 if (udev_monitor == NULL)
559 return NULL;
560 iov.iov_base = &buf;
561 iov.iov_len = sizeof(buf);
562 memzero(&smsg, sizeof(struct msghdr));
563 smsg.msg_iov = &iov;
564 smsg.msg_iovlen = 1;
565 smsg.msg_control = cred_msg;
566 smsg.msg_controllen = sizeof(cred_msg);
567 smsg.msg_name = &snl;
568 smsg.msg_namelen = sizeof(snl);
569
570 buflen = recvmsg(udev_monitor->sock, &smsg, 0);
571 if (buflen < 0) {
572 if (errno != EINTR)
573 log_debug("unable to receive message");
574 return NULL;
575 }
576
577 if (buflen < 32 || (smsg.msg_flags & MSG_TRUNC)) {
578 log_debug("invalid message length");
579 return NULL;
580 }
581
582 if (snl.nl.nl_groups == 0) {
583 /* unicast message, check if we trust the sender */
584 if (udev_monitor->snl_trusted_sender.nl.nl_pid == 0 ||
585 snl.nl.nl_pid != udev_monitor->snl_trusted_sender.nl.nl_pid) {
586 log_debug("unicast netlink message ignored");
587 return NULL;
588 }
589 } else if (snl.nl.nl_groups == UDEV_MONITOR_KERNEL) {
590 if (snl.nl.nl_pid > 0) {
591 log_debug("multicast kernel netlink message from PID %"PRIu32" ignored",
592 snl.nl.nl_pid);
593 return NULL;
594 }
595 }
596
597 cmsg = CMSG_FIRSTHDR(&smsg);
598 if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
599 log_debug("no sender credentials received, message ignored");
600 return NULL;
601 }
602
603 cred = (struct ucred *)CMSG_DATA(cmsg);
604 if (cred->uid != 0) {
605 log_debug("sender uid="UID_FMT", message ignored", cred->uid);
606 return NULL;
607 }
608
609 if (memcmp(buf.raw, "libudev", 8) == 0) {
610 /* udev message needs proper version magic */
611 if (buf.nlh.magic != htonl(UDEV_MONITOR_MAGIC)) {
612 log_debug("unrecognized message signature (%x != %x)",
613 buf.nlh.magic, htonl(UDEV_MONITOR_MAGIC));
614 return NULL;
615 }
616 if (buf.nlh.properties_off+32 > (size_t)buflen) {
617 log_debug("message smaller than expected (%u > %zd)",
618 buf.nlh.properties_off+32, buflen);
619 return NULL;
620 }
621
622 bufpos = buf.nlh.properties_off;
623
624 /* devices received from udev are always initialized */
625 is_initialized = true;
626 } else {
627 /* kernel message with header */
628 bufpos = strlen(buf.raw) + 1;
629 if ((size_t)bufpos < sizeof("a@/d") || bufpos >= buflen) {
630 log_debug("invalid message length");
631 return NULL;
632 }
633
634 /* check message header */
635 if (strstr(buf.raw, "@/") == NULL) {
636 log_debug("unrecognized message header");
637 return NULL;
638 }
639 }
640
641 udev_device = udev_device_new_from_nulstr(udev_monitor->udev, &buf.raw[bufpos], buflen - bufpos);
642 if (!udev_device) {
643 log_debug("could not create device: %m");
644 return NULL;
645 }
646
647 if (is_initialized)
648 udev_device_set_is_initialized(udev_device);
649
650 /* skip device, if it does not pass the current filter */
651 if (!passes_filter(udev_monitor, udev_device)) {
652 struct pollfd pfd[1];
653 int rc;
654
655 udev_device_unref(udev_device);
656
657 /* if something is queued, get next device */
658 pfd[0].fd = udev_monitor->sock;
659 pfd[0].events = POLLIN;
660 rc = poll(pfd, 1, 0);
661 if (rc > 0)
662 goto retry;
663 return NULL;
664 }
665
666 return udev_device;
667 }
668
udev_monitor_send_device(struct udev_monitor * udev_monitor,struct udev_monitor * destination,struct udev_device * udev_device)669 int udev_monitor_send_device(struct udev_monitor *udev_monitor,
670 struct udev_monitor *destination, struct udev_device *udev_device)
671 {
672 const char *buf, *val;
673 ssize_t blen, count;
674 struct udev_monitor_netlink_header nlh = {
675 .prefix = "libudev",
676 .magic = htonl(UDEV_MONITOR_MAGIC),
677 .header_size = sizeof nlh,
678 };
679 struct iovec iov[2] = {
680 { .iov_base = &nlh, .iov_len = sizeof nlh },
681 };
682 struct msghdr smsg = {
683 .msg_iov = iov,
684 .msg_iovlen = 2,
685 };
686 struct udev_list_entry *list_entry;
687 uint64_t tag_bloom_bits;
688
689 blen = udev_device_get_properties_monitor_buf(udev_device, &buf);
690 if (blen < 32) {
691 log_debug("device buffer is too small to contain a valid device");
692 return -EINVAL;
693 }
694
695 /* fill in versioned header */
696 val = udev_device_get_subsystem(udev_device);
697 nlh.filter_subsystem_hash = htonl(util_string_hash32(val));
698
699 val = udev_device_get_devtype(udev_device);
700 if (val != NULL)
701 nlh.filter_devtype_hash = htonl(util_string_hash32(val));
702
703 /* add tag bloom filter */
704 tag_bloom_bits = 0;
705 udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(udev_device))
706 tag_bloom_bits |= util_string_bloom64(udev_list_entry_get_name(list_entry));
707 if (tag_bloom_bits > 0) {
708 nlh.filter_tag_bloom_hi = htonl(tag_bloom_bits >> 32);
709 nlh.filter_tag_bloom_lo = htonl(tag_bloom_bits & 0xffffffff);
710 }
711
712 /* add properties list */
713 nlh.properties_off = iov[0].iov_len;
714 nlh.properties_len = blen;
715 iov[1].iov_base = (char *)buf;
716 iov[1].iov_len = blen;
717
718 /*
719 * Use custom address for target, or the default one.
720 *
721 * If we send to a multicast group, we will get
722 * ECONNREFUSED, which is expected.
723 */
724 if (destination)
725 smsg.msg_name = &destination->snl;
726 else
727 smsg.msg_name = &udev_monitor->snl_destination;
728 smsg.msg_namelen = sizeof(struct sockaddr_nl);
729 count = sendmsg(udev_monitor->sock, &smsg, 0);
730 if (count < 0) {
731 if (!destination && errno == ECONNREFUSED) {
732 log_debug("passed device to netlink monitor %p", udev_monitor);
733 return 0;
734 } else
735 return -errno;
736 }
737
738 log_debug("passed %zi byte device to netlink monitor %p", count, udev_monitor);
739 return count;
740 }
741
742 /**
743 * udev_monitor_filter_add_match_subsystem_devtype:
744 * @udev_monitor: the monitor
745 * @subsystem: the subsystem value to match the incoming devices against
746 * @devtype: the devtype value to match the incoming devices against
747 *
748 * This filter is efficiently executed inside the kernel, and libudev subscribers
749 * will usually not be woken up for devices which do not match.
750 *
751 * The filter must be installed before the monitor is switched to listening mode.
752 *
753 * Returns: 0 on success, otherwise a negative error value.
754 */
udev_monitor_filter_add_match_subsystem_devtype(struct udev_monitor * udev_monitor,const char * subsystem,const char * devtype)755 _public_ int udev_monitor_filter_add_match_subsystem_devtype(struct udev_monitor *udev_monitor, const char *subsystem, const char *devtype)
756 {
757 if (udev_monitor == NULL)
758 return -EINVAL;
759 if (subsystem == NULL)
760 return -EINVAL;
761 if (udev_list_entry_add(&udev_monitor->filter_subsystem_list, subsystem, devtype) == NULL)
762 return -ENOMEM;
763 return 0;
764 }
765
766 /**
767 * udev_monitor_filter_add_match_tag:
768 * @udev_monitor: the monitor
769 * @tag: the name of a tag
770 *
771 * This filter is efficiently executed inside the kernel, and libudev subscribers
772 * will usually not be woken up for devices which do not match.
773 *
774 * The filter must be installed before the monitor is switched to listening mode.
775 *
776 * Returns: 0 on success, otherwise a negative error value.
777 */
udev_monitor_filter_add_match_tag(struct udev_monitor * udev_monitor,const char * tag)778 _public_ int udev_monitor_filter_add_match_tag(struct udev_monitor *udev_monitor, const char *tag)
779 {
780 if (udev_monitor == NULL)
781 return -EINVAL;
782 if (tag == NULL)
783 return -EINVAL;
784 if (udev_list_entry_add(&udev_monitor->filter_tag_list, tag, NULL) == NULL)
785 return -ENOMEM;
786 return 0;
787 }
788
789 /**
790 * udev_monitor_filter_remove:
791 * @udev_monitor: monitor
792 *
793 * Remove all filters from monitor.
794 *
795 * Returns: 0 on success, otherwise a negative error value.
796 */
udev_monitor_filter_remove(struct udev_monitor * udev_monitor)797 _public_ int udev_monitor_filter_remove(struct udev_monitor *udev_monitor)
798 {
799 static struct sock_fprog filter = { 0, NULL };
800
801 udev_list_cleanup(&udev_monitor->filter_subsystem_list);
802 return setsockopt(udev_monitor->sock, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter));
803 }
804