chiark / gitweb /
rule-generator: net - whitelist NICs that violate MAC local scheme
[elogind.git] / udev / lib / libudev-monitor.c
1 /*
2  * libudev - interface to udev device information
3  *
4  * Copyright (C) 2008 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 struct udev_monitor {
31         struct udev *udev;
32         int refcount;
33         int sock;
34         struct sockaddr_nl snl;
35         struct sockaddr_nl snl_peer;
36         struct sockaddr_un sun;
37         socklen_t addrlen;
38         struct udev_list_node filter_subsystem_list;
39 };
40
41 enum udev_monitor_netlink_group {
42         UDEV_MONITOR_KERNEL     = 1,
43         UDEV_MONITOR_UDEV       = 2,
44 };
45
46 #define UDEV_MONITOR_MAGIC              0xcafe1dea
47 struct udev_monitor_netlink_header {
48         /* udev version text */
49         char version[16];
50         /*
51          * magic to protect against daemon <-> library message format mismatch
52          * used in the kernel from socket filter rules; needs to be stored in network order
53          */
54         unsigned int magic;
55         /* properties buffer */
56         unsigned short properties_off;
57         unsigned short properties_len;
58         /*
59          * hashes of some common device properties strings to filter with socket filters in
60          * the client used in the kernel from socket filter rules; needs to be stored in
61          * network order
62          */
63         unsigned int filter_subsystem;
64         unsigned int filter_devtype;
65 };
66
67 static struct udev_monitor *udev_monitor_new(struct udev *udev)
68 {
69         struct udev_monitor *udev_monitor;
70
71         udev_monitor = calloc(1, sizeof(struct udev_monitor));
72         if (udev_monitor == NULL)
73                 return NULL;
74         udev_monitor->refcount = 1;
75         udev_monitor->udev = udev;
76         udev_list_init(&udev_monitor->filter_subsystem_list);
77         return udev_monitor;
78 }
79
80 /**
81  * udev_monitor_new_from_socket:
82  * @udev: udev library context
83  * @socket_path: unix socket path
84  *
85  * Create new udev monitor and connect to a specified socket. The
86  * path to a socket either points to an existing socket file, or if
87  * the socket path starts with a '@' character, an abstract namespace
88  * socket will be used.
89  *
90  * A socket file will not be created. If it does not already exist,
91  * it will fall-back and connect to an abstract namespace socket with
92  * the given path. The permissions adjustment of a socket file, as
93  * well as the later cleanup, needs to be done by the caller.
94  *
95  * The initial refcount is 1, and needs to be decremented to
96  * release the resources of the udev monitor.
97  *
98  * Returns: a new udev monitor, or #NULL, in case of an error
99  **/
100 struct udev_monitor *udev_monitor_new_from_socket(struct udev *udev, const char *socket_path)
101 {
102         struct udev_monitor *udev_monitor;
103         struct stat statbuf;
104
105         if (udev == NULL)
106                 return NULL;
107         if (socket_path == NULL)
108                 return NULL;
109         udev_monitor = udev_monitor_new(udev);
110         if (udev_monitor == NULL)
111                 return NULL;
112
113         udev_monitor->sun.sun_family = AF_LOCAL;
114         if (socket_path[0] == '@') {
115                 /* translate leading '@' to abstract namespace */
116                 util_strlcpy(udev_monitor->sun.sun_path, socket_path, sizeof(udev_monitor->sun.sun_path));
117                 udev_monitor->sun.sun_path[0] = '\0';
118                 udev_monitor->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(socket_path);
119         } else if (stat(socket_path, &statbuf) == 0 && S_ISSOCK(statbuf.st_mode)) {
120                 /* existing socket file */
121                 util_strlcpy(udev_monitor->sun.sun_path, socket_path, sizeof(udev_monitor->sun.sun_path));
122                 udev_monitor->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(socket_path);
123         } else {
124                 /* no socket file, assume abstract namespace socket */
125                 util_strlcpy(&udev_monitor->sun.sun_path[1], socket_path, sizeof(udev_monitor->sun.sun_path)-1);
126                 udev_monitor->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(socket_path)+1;
127         }
128         udev_monitor->sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
129         if (udev_monitor->sock == -1) {
130                 err(udev, "error getting socket: %m\n");
131                 free(udev_monitor);
132                 return NULL;
133         }
134         util_set_fd_cloexec(udev_monitor->sock);
135
136         dbg(udev, "monitor %p created with '%s'\n", udev_monitor, socket_path);
137         return udev_monitor;
138 }
139
140 /**
141  * udev_monitor_new_from_netlink:
142  * @udev: udev library context
143  * @name: name of event source
144  *
145  * Create new udev monitor and connect to a specified event
146  * source. Valid sources identifiers are "udev" and "kernel".
147  *
148  * Applications should usually not connect directly to the
149  * "kernel" events, because the devices might not be useable
150  * at that time, before udev has configured them, and created
151  * device nodes.
152  *
153  * Accessing devices at the same time as udev, might result
154  * in unpredictable behavior.
155  *
156  * The "udev" events are sent out after udev has finished its
157  * event processing, all rules have been processed, and needed
158  * device nodes are created.
159  *
160  * The initial refcount is 1, and needs to be decremented to
161  * release the resources of the udev monitor.
162  *
163  * Returns: a new udev monitor, or #NULL, in case of an error
164  **/
165 struct udev_monitor *udev_monitor_new_from_netlink(struct udev *udev, const char *name)
166 {
167         struct udev_monitor *udev_monitor;
168         unsigned int group;
169
170         if (udev == NULL)
171                 return NULL;
172
173         if (name == NULL)
174                 return NULL;
175         if (strcmp(name, "kernel") == 0)
176                 group = UDEV_MONITOR_KERNEL;
177         else if (strcmp(name, "udev") == 0)
178                 group = UDEV_MONITOR_UDEV;
179         else
180                 return NULL;
181
182         udev_monitor = udev_monitor_new(udev);
183         if (udev_monitor == NULL)
184                 return NULL;
185
186         udev_monitor->sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
187         if (udev_monitor->sock == -1) {
188                 err(udev, "error getting socket: %m\n");
189                 free(udev_monitor);
190                 return NULL;
191         }
192         util_set_fd_cloexec(udev_monitor->sock);
193
194         udev_monitor->snl.nl_family = AF_NETLINK;
195         udev_monitor->snl.nl_groups = group;
196         udev_monitor->snl_peer.nl_family = AF_NETLINK;
197         udev_monitor->snl_peer.nl_groups = UDEV_MONITOR_UDEV;
198
199         dbg(udev, "monitor %p created with NETLINK_KOBJECT_UEVENT (%u)\n", udev_monitor, group);
200         return udev_monitor;
201 }
202
203 static inline void bpf_stmt(struct sock_filter *inss, unsigned int *i,
204                             unsigned short code, unsigned int data)
205 {
206         struct sock_filter *ins = &inss[*i];
207
208         ins->code = code;
209         ins->k = data;
210         (*i)++;
211 }
212
213 static inline void bpf_jmp(struct sock_filter *inss, unsigned int *i,
214                            unsigned short code, unsigned int data,
215                            unsigned short jt, unsigned short jf)
216 {
217         struct sock_filter *ins = &inss[*i];
218
219         ins->code = code;
220         ins->jt = jt;
221         ins->jf = jf;
222         ins->k = data;
223         (*i)++;
224 }
225
226 int udev_monitor_filter_update(struct udev_monitor *udev_monitor)
227 {
228         static struct sock_filter ins[256];
229         static struct sock_fprog filter;
230         unsigned int i;
231         struct udev_list_entry *list_entry;
232         int err;
233
234         if (udev_list_get_entry(&udev_monitor->filter_subsystem_list) == NULL)
235                 return 0;
236
237         memset(ins, 0x00, sizeof(ins));
238         i = 0;
239
240         /* load magic in A */
241         bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, magic));
242         /* jump if magic matches */
243         bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, UDEV_MONITOR_MAGIC, 1, 0);
244         /* wrong magic, pass packet */
245         bpf_stmt(ins, &i, BPF_RET|BPF_K, 0xffffffff);
246
247         /* add all subsystem match values */
248         udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_subsystem_list)) {
249                 unsigned int hash;
250
251                 /* load filter_subsystem value in A */
252                 bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_subsystem));
253                 hash = util_string_hash32(udev_list_entry_get_name(list_entry));
254                 if (udev_list_entry_get_value(list_entry) == NULL) {
255                         /* jump if subsystem does not match */
256                         bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, hash, 0, 1);
257                 } else {
258                         /* jump if subsystem does not match */
259                         bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, hash, 0, 3);
260
261                         /* load filter_devtype value in A */
262                         bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_devtype));
263                         /* jump if value does not match */
264                         hash = util_string_hash32(udev_list_entry_get_value(list_entry));
265                         bpf_jmp(ins, &i, BPF_JMP|BPF_JEQ|BPF_K, hash, 0, 1);
266                 }
267
268                 /* matched, pass packet */
269                 bpf_stmt(ins, &i, BPF_RET|BPF_K, 0xffffffff);
270
271                 if (i+1 >= ARRAY_SIZE(ins))
272                         return -1;
273         }
274         /* nothing matched, drop packet */
275         bpf_stmt(ins, &i, BPF_RET|BPF_K, 0);
276
277         /* install filter */
278         filter.len = i;
279         filter.filter = ins;
280         err = setsockopt(udev_monitor->sock, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter));
281         return err;
282 }
283
284 int udev_monitor_enable_receiving(struct udev_monitor *udev_monitor)
285 {
286         int err;
287         const int on = 1;
288
289         if (udev_monitor->sun.sun_family != 0) {
290                 err = bind(udev_monitor->sock,
291                            (struct sockaddr *)&udev_monitor->sun, udev_monitor->addrlen);
292         } else if (udev_monitor->snl.nl_family != 0) {
293                 udev_monitor_filter_update(udev_monitor);
294                 err = bind(udev_monitor->sock,
295                            (struct sockaddr *)&udev_monitor->snl, sizeof(struct sockaddr_nl));
296         } else {
297                 return -EINVAL;
298         }
299
300         if (err < 0) {
301                 err(udev_monitor->udev, "bind failed: %m\n");
302                 return err;
303         }
304
305         /* enable receiving of sender credentials */
306         setsockopt(udev_monitor->sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
307         return 0;
308 }
309
310 int udev_monitor_set_receive_buffer_size(struct udev_monitor *udev_monitor, int size)
311 {
312         if (udev_monitor == NULL)
313                 return -1;
314         return setsockopt(udev_monitor->sock, SOL_SOCKET, SO_RCVBUFFORCE, &size, sizeof(size));
315 }
316
317 /**
318  * udev_monitor_ref:
319  * @udev_monitor: udev monitor
320  *
321  * Take a reference of a udev monitor.
322  *
323  * Returns: the passed udev monitor
324  **/
325 struct udev_monitor *udev_monitor_ref(struct udev_monitor *udev_monitor)
326 {
327         if (udev_monitor == NULL)
328                 return NULL;
329         udev_monitor->refcount++;
330         return udev_monitor;
331 }
332
333 /**
334  * udev_monitor_unref:
335  * @udev_monitor: udev monitor
336  *
337  * Drop a reference of a udev monitor. If the refcount reaches zero,
338  * the bound socket will be closed, and the resources of the monitor
339  * will be released.
340  *
341  **/
342 void udev_monitor_unref(struct udev_monitor *udev_monitor)
343 {
344         if (udev_monitor == NULL)
345                 return;
346         udev_monitor->refcount--;
347         if (udev_monitor->refcount > 0)
348                 return;
349         if (udev_monitor->sock >= 0)
350                 close(udev_monitor->sock);
351         udev_list_cleanup_entries(udev_monitor->udev, &udev_monitor->filter_subsystem_list);
352         dbg(udev_monitor->udev, "monitor %p released\n", udev_monitor);
353         free(udev_monitor);
354 }
355
356 /**
357  * udev_monitor_get_udev:
358  * @udev_monitor: udev monitor
359  *
360  * Retrieve the udev library context the monitor was created with.
361  *
362  * Returns: the udev library context
363  **/
364 struct udev *udev_monitor_get_udev(struct udev_monitor *udev_monitor)
365 {
366         if (udev_monitor == NULL)
367                 return NULL;
368         return udev_monitor->udev;
369 }
370
371 /**
372  * udev_monitor_get_fd:
373  * @udev_monitor: udev monitor
374  *
375  * Retrieve the socket file descriptor associated with the monitor.
376  *
377  * Returns: the socket file descriptor
378  **/
379 int udev_monitor_get_fd(struct udev_monitor *udev_monitor)
380 {
381         if (udev_monitor == NULL)
382                 return -1;
383         return udev_monitor->sock;
384 }
385
386 static int passes_filter(struct udev_monitor *udev_monitor, struct udev_device *udev_device)
387 {
388         struct udev_list_entry *list_entry;
389
390         if (udev_list_get_entry(&udev_monitor->filter_subsystem_list) == NULL)
391                 return 1;
392
393         udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_monitor->filter_subsystem_list)) {
394                 const char *subsys = udev_list_entry_get_name(list_entry);
395                 const char *dsubsys = udev_device_get_subsystem(udev_device);
396                 const char *devtype;
397                 const char *ddevtype;
398
399                 if (strcmp(dsubsys, subsys) != 0)
400                         continue;
401
402                 devtype = udev_list_entry_get_value(list_entry);
403                 if (devtype == NULL)
404                         return 1;
405                 ddevtype = udev_device_get_devtype(udev_device);
406                 if (ddevtype == NULL)
407                         continue;
408                 if (strcmp(ddevtype, devtype) == 0)
409                         return 1;
410         }
411         return 0;
412 }
413
414 /**
415  * udev_monitor_receive_device:
416  * @udev_monitor: udev monitor
417  *
418  * Receive data from the udev monitor socket, allocate a new udev
419  * device, fill in the received data, and return the device.
420  *
421  * Only socket connections with uid=0 are accepted. The caller
422  * needs to make sure that there is data to read from the socket.
423  * The call will block until the socket becomes readable.
424  *
425  * The initial refcount is 1, and needs to be decremented to
426  * release the resources of the udev device.
427  *
428  * Returns: a new udev device, or #NULL, in case of an error
429  **/
430 struct udev_device *udev_monitor_receive_device(struct udev_monitor *udev_monitor)
431 {
432         struct udev_device *udev_device;
433         struct msghdr smsg;
434         struct iovec iov;
435         char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
436         struct cmsghdr *cmsg;
437         struct sockaddr_nl snl;
438         struct ucred *cred;
439         char buf[8192];
440         ssize_t buflen;
441         ssize_t bufpos;
442         struct udev_monitor_netlink_header *nlh;
443         int devpath_set = 0;
444         int subsystem_set = 0;
445         int action_set = 0;
446         int maj = 0;
447         int min = 0;
448
449 retry:
450         if (udev_monitor == NULL)
451                 return NULL;
452         memset(buf, 0x00, sizeof(buf));
453         iov.iov_base = &buf;
454         iov.iov_len = sizeof(buf);
455         memset (&smsg, 0x00, sizeof(struct msghdr));
456         smsg.msg_iov = &iov;
457         smsg.msg_iovlen = 1;
458         smsg.msg_control = cred_msg;
459         smsg.msg_controllen = sizeof(cred_msg);
460
461         if (udev_monitor->snl.nl_family != 0) {
462                 smsg.msg_name = &snl;
463                 smsg.msg_namelen = sizeof(snl);
464         }
465
466         buflen = recvmsg(udev_monitor->sock, &smsg, 0);
467         if (buflen < 0) {
468                 if (errno != EINTR)
469                         info(udev_monitor->udev, "unable to receive message\n");
470                 return NULL;
471         }
472
473         if (buflen < 32 || (size_t)buflen >= sizeof(buf)) {
474                 info(udev_monitor->udev, "invalid message length\n");
475                 return NULL;
476         }
477
478         if (udev_monitor->snl.nl_family != 0) {
479                 if (snl.nl_groups == 0) {
480                         info(udev_monitor->udev, "unicast netlink message ignored\n");
481                         return NULL;
482                 }
483                 if ((snl.nl_groups == UDEV_MONITOR_KERNEL) && (snl.nl_pid > 0)) {
484                         info(udev_monitor->udev, "multicast kernel netlink message from pid %d ignored\n", snl.nl_pid);
485                         return NULL;
486                 }
487         }
488
489         cmsg = CMSG_FIRSTHDR(&smsg);
490         if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
491                 info(udev_monitor->udev, "no sender credentials received, message ignored\n");
492                 return NULL;
493         }
494
495         cred = (struct ucred *)CMSG_DATA(cmsg);
496         if (cred->uid != 0) {
497                 info(udev_monitor->udev, "sender uid=%d, message ignored\n", cred->uid);
498                 return NULL;
499         }
500
501         if (strncmp(buf, "udev-", 5) == 0) {
502                 /* udev message needs proper version magic */
503                 nlh = (struct udev_monitor_netlink_header *) buf;
504                 if (nlh->magic != htonl(UDEV_MONITOR_MAGIC))
505                         return NULL;
506                 if (nlh->properties_off < sizeof(struct udev_monitor_netlink_header))
507                         return NULL;
508                 if (nlh->properties_off+32U > buflen)
509                         return NULL;
510                 bufpos = nlh->properties_off;
511         } else {
512                 /* kernel message with header */
513                 bufpos = strlen(buf) + 1;
514                 if ((size_t)bufpos < sizeof("a@/d") || bufpos >= buflen) {
515                         info(udev_monitor->udev, "invalid message length\n");
516                         return NULL;
517                 }
518
519                 /* check message header */
520                 if (strstr(buf, "@/") == NULL) {
521                         info(udev_monitor->udev, "unrecognized message header\n");
522                         return NULL;
523                 }
524         }
525
526         udev_device = device_new(udev_monitor->udev);
527         if (udev_device == NULL) {
528                 return NULL;
529         }
530
531         while (bufpos < buflen) {
532                 char *key;
533                 size_t keylen;
534
535                 key = &buf[bufpos];
536                 keylen = strlen(key);
537                 if (keylen == 0)
538                         break;
539                 bufpos += keylen + 1;
540
541                 if (strncmp(key, "DEVPATH=", 8) == 0) {
542                         char path[UTIL_PATH_SIZE];
543
544                         util_strlcpy(path, udev_get_sys_path(udev_monitor->udev), sizeof(path));
545                         util_strlcat(path, &key[8], sizeof(path));
546                         udev_device_set_syspath(udev_device, path);
547                         devpath_set = 1;
548                 } else if (strncmp(key, "SUBSYSTEM=", 10) == 0) {
549                         udev_device_set_subsystem(udev_device, &key[10]);
550                         subsystem_set = 1;
551                 } else if (strncmp(key, "DEVTYPE=", 8) == 0) {
552                         udev_device_set_devtype(udev_device, &key[8]);
553                 } else if (strncmp(key, "DEVNAME=", 8) == 0) {
554                         udev_device_set_devnode(udev_device, &key[8]);
555                 } else if (strncmp(key, "DEVLINKS=", 9) == 0) {
556                         char devlinks[UTIL_PATH_SIZE];
557                         char *slink;
558                         char *next;
559
560                         util_strlcpy(devlinks, &key[9], sizeof(devlinks));
561                         slink = devlinks;
562                         next = strchr(slink, ' ');
563                         while (next != NULL) {
564                                 next[0] = '\0';
565                                 udev_device_add_devlink(udev_device, slink);
566                                 slink = &next[1];
567                                 next = strchr(slink, ' ');
568                         }
569                         if (slink[0] != '\0')
570                                 udev_device_add_devlink(udev_device, slink);
571                 } else if (strncmp(key, "DRIVER=", 7) == 0) {
572                         udev_device_set_driver(udev_device, &key[7]);
573                 } else if (strncmp(key, "ACTION=", 7) == 0) {
574                         udev_device_set_action(udev_device, &key[7]);
575                         action_set = 1;
576                 } else if (strncmp(key, "MAJOR=", 6) == 0) {
577                         maj = strtoull(&key[6], NULL, 10);
578                 } else if (strncmp(key, "MINOR=", 6) == 0) {
579                         min = strtoull(&key[6], NULL, 10);
580                 } else if (strncmp(key, "DEVPATH_OLD=", 12) == 0) {
581                         udev_device_set_devpath_old(udev_device, &key[12]);
582                 } else if (strncmp(key, "PHYSDEVPATH=", 12) == 0) {
583                         udev_device_set_physdevpath(udev_device, &key[12]);
584                 } else if (strncmp(key, "SEQNUM=", 7) == 0) {
585                         udev_device_set_seqnum(udev_device, strtoull(&key[7], NULL, 10));
586                 } else if (strncmp(key, "TIMEOUT=", 8) == 0) {
587                         udev_device_set_timeout(udev_device, strtoull(&key[8], NULL, 10));
588                 } else if (strncmp(key, "PHYSDEV", 7) == 0) {
589                         /* skip deprecated values */
590                         continue;
591                 } else {
592                         udev_device_add_property_from_string(udev_device, key);
593                 }
594         }
595         if (!devpath_set || !subsystem_set || !action_set) {
596                 info(udev_monitor->udev, "missing values, skip\n");
597                 udev_device_unref(udev_device);
598                 return NULL;
599         }
600
601         /* skip device, if it does not pass the current filter */
602         if (!passes_filter(udev_monitor, udev_device)) {
603                 struct pollfd pfd[1];
604                 int rc;
605
606                 udev_device_unref(udev_device);
607
608                 /* if something is queued, get next device */
609                 pfd[0].fd = udev_monitor->sock;
610                 pfd[0].events = POLLIN;
611                 rc = poll(pfd, 1, 0);
612                 if (rc > 0)
613                         goto retry;
614                 return NULL;
615         }
616
617         if (maj > 0)
618                 udev_device_set_devnum(udev_device, makedev(maj, min));
619         udev_device_set_info_loaded(udev_device);
620         return udev_device;
621 }
622
623 int udev_monitor_send_device(struct udev_monitor *udev_monitor, struct udev_device *udev_device)
624 {
625         struct msghdr smsg;
626         struct iovec iov[2];
627         const char *buf;
628         ssize_t blen;
629         ssize_t count;
630
631         blen = udev_device_get_properties_monitor_buf(udev_device, &buf);
632         if (blen < 32)
633                 return -1;
634
635         if (udev_monitor->sun.sun_family != 0) {
636                 const char *action;
637                 char header[2048];
638                 size_t hlen;
639
640                 /* header <action>@<devpath> */
641                 action = udev_device_get_action(udev_device);
642                 if (action == NULL)
643                         return -EINVAL;
644                 util_strlcpy(header, action, sizeof(header));
645                 util_strlcat(header, "@", sizeof(header));
646                 hlen = util_strlcat(header, udev_device_get_devpath(udev_device), sizeof(header))+1;
647                 if (hlen >= sizeof(header))
648                         return -EINVAL;
649                 iov[0].iov_base = header;
650                 iov[0].iov_len = hlen;
651
652                 /* add properties list */
653                 iov[1].iov_base = (char *)buf;
654                 iov[1].iov_len = blen;
655
656                 memset(&smsg, 0x00, sizeof(struct msghdr));
657                 smsg.msg_iov = iov;
658                 smsg.msg_iovlen = 2;
659                 smsg.msg_name = &udev_monitor->sun;
660                 smsg.msg_namelen = udev_monitor->addrlen;
661         } else if (udev_monitor->snl.nl_family != 0) {
662                 const char *val;
663                 struct udev_monitor_netlink_header nlh;
664
665
666                 /* add versioned header */
667                 memset(&nlh, 0x00, sizeof(struct udev_monitor_netlink_header));
668                 util_strlcpy(nlh.version, "udev-" VERSION, sizeof(nlh.version));
669                 nlh.magic = htonl(UDEV_MONITOR_MAGIC);
670                 val = udev_device_get_subsystem(udev_device);
671                 nlh.filter_subsystem = htonl(util_string_hash32(val));
672                 val = udev_device_get_devtype(udev_device);
673                 if (val != NULL)
674                         nlh.filter_devtype = htonl(util_string_hash32(val));
675                 iov[0].iov_base = &nlh;
676                 iov[0].iov_len = sizeof(struct udev_monitor_netlink_header);
677
678                 /* add properties list */
679                 nlh.properties_off = iov[0].iov_len;
680                 nlh.properties_len = blen;
681                 iov[1].iov_base = (char *)buf;
682                 iov[1].iov_len = blen;
683
684                 memset(&smsg, 0x00, sizeof(struct msghdr));
685                 smsg.msg_iov = iov;
686                 smsg.msg_iovlen = 2;
687                 /* no destination besides the muticast group, we will always get ECONNREFUSED */
688                 smsg.msg_name = &udev_monitor->snl_peer;
689                 smsg.msg_namelen = sizeof(struct sockaddr_nl);
690         } else {
691                 return -1;
692         }
693
694         count = sendmsg(udev_monitor->sock, &smsg, 0);
695         info(udev_monitor->udev, "passed %zi bytes to monitor %p\n", count, udev_monitor);
696         return count;
697 }
698
699 int udev_monitor_filter_add_match_subsystem_devtype(struct udev_monitor *udev_monitor, const char *subsystem, const char *devtype)
700 {
701         if (udev_monitor == NULL)
702                 return -EINVAL;
703         if (subsystem == NULL)
704                 return 0;
705         if (udev_list_entry_add(udev_monitor->udev,
706                                 &udev_monitor->filter_subsystem_list, subsystem, devtype, 0, 0) == NULL)
707                 return -ENOMEM;
708         return 0;
709 }
710
711 int udev_monitor_filter_remove(struct udev_monitor *udev_monitor)
712 {
713         static struct sock_fprog filter = { 0, NULL };
714
715         udev_list_cleanup_entries(udev_monitor->udev, &udev_monitor->filter_subsystem_list);
716         return setsockopt(udev_monitor->sock, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter));
717 }