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