eiwd

no dbus iwd with iwctl functionality
git clone https://git.daat.foo/repos/eiwd.git
Log | Files | Refs | Submodules | README | LICENSE

udev.c (5884B)


      1 /*
      2  *
      3  *  Wireless daemon for Linux
      4  *
      5  *  Copyright (C) 2024  Intel Corporation. All rights reserved.
      6  *
      7  *  This library is free software; you can redistribute it and/or
      8  *  modify it under the terms of the GNU Lesser General Public
      9  *  License as published by the Free Software Foundation; either
     10  *  version 2.1 of the License, or (at your option) any later version.
     11  *
     12  *  This library is distributed in the hope that it will be useful,
     13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     15  *  Lesser General Public License for more details.
     16  *
     17  *  You should have received a copy of the GNU Lesser General Public
     18  *  License along with this library; if not, write to the Free Software
     19  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     20  *
     21  */
     22 
     23 #ifdef HAVE_CONFIG_H
     24 #include <config.h>
     25 #endif
     26 
     27 #define _GNU_SOURCE
     28 #include <errno.h>
     29 #include <unistd.h>
     30 #include <sys/socket.h>
     31 #include <linux/netlink.h>
     32 #include <linux/filter.h>
     33 #include <ell/ell.h>
     34 
     35 #include "src/module.h"
     36 
     37 static void receive_props(const void *buf, uint32_t len)
     38 {
     39 	const char *action = NULL;
     40 	const char *interface = NULL;
     41 	const char *ifindex = NULL;
     42 
     43 	while (len > 0) {
     44 		const char *s = buf;
     45 		size_t l = strlen(s);
     46 		const char *t;
     47 
     48 		if (l < 2)
     49 			break;
     50 
     51 		t = strchr(s, '=');
     52 		if (t) {
     53 			size_t p = t - s;
     54 
     55 			if (!strncmp(s, "ACTION", p))
     56 				action = t + 1;
     57 			else if (!strncmp(s, "INTERFACE", p))
     58 				interface = t + 1;
     59 			else if (!strncmp(s, "IFINDEX", p))
     60 				ifindex = t + 1;
     61 		}
     62 
     63 		buf += l + 1;
     64 		len -= l + 1;
     65 	}
     66 
     67 	if (action && !strcmp(action, "add"))
     68 		l_info("udev interface=%s ifindex=%s", interface, ifindex);
     69 }
     70 
     71 static struct l_io *udev_io;
     72 
     73 static int create_socket(uint32_t *pid)
     74 {
     75 	struct sockaddr_nl addr;
     76 	socklen_t addrlen = sizeof(addr);
     77 	int fd, pktinfo = 1;
     78 
     79 	fd = socket(PF_NETLINK, SOCK_RAW | SOCK_CLOEXEC | SOCK_NONBLOCK,
     80 						NETLINK_KOBJECT_UEVENT);
     81 	if (fd < 0)
     82 		return -1;
     83 
     84 	memset(&addr, 0, sizeof(addr));
     85 	addr.nl_family = AF_NETLINK;
     86 
     87 	if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
     88 		close(fd);
     89 		return -1;
     90 	}
     91 
     92 	if (getsockname(fd, (struct sockaddr *) &addr, &addrlen) < 0) {
     93 		close(fd);
     94 		return -1;
     95 	}
     96 
     97 	if (setsockopt(fd, SOL_NETLINK, NETLINK_PKTINFO,
     98 					&pktinfo, sizeof(pktinfo)) < 0) {
     99 		close(fd);
    100 		return -1;
    101 	}
    102 
    103 	if (pid)
    104 		*pid = addr.nl_pid;
    105 
    106 	return fd;
    107 }
    108 
    109 static struct sock_filter subsys_filter[] = {
    110 	{ 0x20,  0,  0, 0x00000008 },	/* ldw #magic	*/
    111 	{ 0x15,  0,  3, 0xfeedcafe },	/* jne #feedcafe, drop	*/
    112 	{ 0x20,  0,  0, 0x00000018 },	/* ldw #subsys_hash	*/
    113 	{ 0x15,  0,  1, 0xa74d3cc8 },	/* jne #net, drop	*/
    114 	{ 0x06,  0,  0, 0xffffffff },	/* keep: ret #-1	*/
    115 	{ 0x06,  0,  0, 0000000000 },	/* drop: ret #0		*/
    116 };
    117 
    118 static const struct sock_fprog subsys_fprog = { .len = 6,
    119 						.filter = subsys_filter };
    120 
    121 static bool attach_filter(struct l_io *io)
    122 {
    123 	int fd;
    124 
    125 	fd = l_io_get_fd(io);
    126 	if (fd < 0)
    127 		return false;
    128 
    129 	if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER,
    130 				&subsys_fprog, sizeof(subsys_fprog)) < 0)
    131 		return false;
    132 
    133 	return true;
    134 }
    135 
    136 static bool add_membership(struct l_io *io, uint32_t group)
    137 {
    138 	int fd, value = group;
    139 
    140 	fd = l_io_get_fd(io);
    141 	if (fd < 0)
    142 		return false;
    143 
    144 	if (setsockopt(fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP,
    145 						&value, sizeof(value)) < 0)
    146 		return false;
    147 
    148 	return true;
    149 }
    150 
    151 static bool drop_membership(struct l_io *io, uint32_t group)
    152 {
    153 	int fd, value = group;
    154 
    155 	fd = l_io_get_fd(io);
    156 	if (fd < 0)
    157 		return false;
    158 
    159 	if (setsockopt(fd, SOL_NETLINK, NETLINK_DROP_MEMBERSHIP,
    160 						&value, sizeof(value)) < 0)
    161 		return false;
    162 
    163 	return true;
    164 }
    165 
    166 static const uint8_t LIBUDEV_PREFIX[8] = { "libudev" };
    167 static uint32_t LIBUDEV_MAGIC = 0xfeedcafe;
    168 
    169 struct udev_hdr {
    170 	char prefix[8];
    171 	uint32_t magic;
    172 	uint32_t hdr_size;
    173 	uint32_t props_off;
    174 	uint32_t props_len;
    175 	uint32_t flt_subsys_hash;
    176 	uint32_t flt_devtype_hash;
    177 	uint32_t flt_tag_bloom_hi;
    178 	uint32_t flt_tag_bloom_lo;
    179 };
    180 
    181 static void receive_msg(const void *buf, uint32_t len)
    182 {
    183 	const struct udev_hdr *hdr = buf;
    184 
    185 	if (len < sizeof(struct udev_hdr))
    186 		return;
    187 
    188 	if (memcmp(hdr->prefix, LIBUDEV_PREFIX, 8))
    189 		return;
    190 
    191 	if (L_BE32_TO_CPU(hdr->magic) != LIBUDEV_MAGIC)
    192 		return;
    193 
    194 	if (hdr->hdr_size > len)
    195 		return;
    196 
    197 	if (hdr->props_off + hdr->props_len != len)
    198 		return;
    199 
    200 	receive_props(buf + hdr->props_off, hdr->props_len);
    201 }
    202 
    203 enum {
    204 	GROUP_NONE,
    205 	GROUP_KERNEL,
    206 	GROUP_UDEV,
    207 };
    208 
    209 static bool can_read_data(struct l_io *io, void *user_data)
    210 {
    211 	struct cmsghdr *cmsg;
    212 	struct msghdr msg;
    213 	struct iovec iov;
    214 	unsigned char buffer[4096];
    215 	unsigned char control[32];
    216 	uint32_t group = GROUP_NONE;
    217 	ssize_t len;
    218 	int fd;
    219 
    220 	memset(buffer, 0, sizeof(buffer));
    221 	memset(control, 0, sizeof(control));
    222 
    223 	fd = l_io_get_fd(io);
    224 
    225 	iov.iov_base = buffer;
    226 	iov.iov_len = sizeof(buffer);
    227 
    228 	memset(&msg, 0, sizeof(msg));
    229 	msg.msg_iov = &iov;
    230 	msg.msg_iovlen = 1;
    231 	msg.msg_control = control;
    232 	msg.msg_controllen = sizeof(control);
    233 
    234 	len = recvmsg(fd, &msg, 0);
    235 	if (len < 0)
    236 		return false;
    237 
    238 	for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
    239 		struct nl_pktinfo *pktinfo;
    240 
    241 		if (cmsg->cmsg_level != SOL_NETLINK)
    242 			continue;
    243 
    244 		if (cmsg->cmsg_type != NETLINK_PKTINFO)
    245 			continue;
    246 
    247 		pktinfo = (void *) CMSG_DATA(cmsg);
    248 		group = pktinfo->group;
    249 	}
    250 
    251 	if (group == GROUP_UDEV)
    252 		receive_msg(buffer, len);
    253 
    254 	return true;
    255 }
    256 
    257 static int udev_init(void)
    258 {
    259 	int fd;
    260 
    261 	l_debug("");
    262 
    263 	fd = create_socket(NULL);
    264 	if (fd < 0)
    265 		return -EIO;
    266 
    267 	udev_io = l_io_new(fd);
    268 	l_io_set_close_on_destroy(udev_io, true);
    269 	l_io_set_read_handler(udev_io, can_read_data, NULL, NULL);
    270 	attach_filter(udev_io);
    271 	add_membership(udev_io, GROUP_UDEV);
    272 
    273 	return 0;
    274 }
    275 
    276 static void udev_exit(void)
    277 {
    278 	l_debug("");
    279 
    280 	drop_membership(udev_io, GROUP_UDEV);
    281 	l_io_destroy(udev_io);
    282 }
    283 
    284 IWD_MODULE(udev, udev_init, udev_exit);
    285 IWD_MODULE_DEPENDS(udev, netdev);