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