From: Yu Watanabe Date: Mon, 18 Oct 2021 17:41:38 +0000 (+0900) Subject: sd-netlink: introduce sd_netlink_attach_filter() X-Git-Tag: v250-rc1~468^2~1 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=dc317a9aed3ef43f76f05cb6eeb49c7c82c0e2ad;p=thirdparty%2Fsystemd.git sd-netlink: introduce sd_netlink_attach_filter() --- diff --git a/src/libsystemd/sd-netlink/sd-netlink.c b/src/libsystemd/sd-netlink/sd-netlink.c index 2bf26523bde..9de13f4c46d 100644 --- a/src/libsystemd/sd-netlink/sd-netlink.c +++ b/src/libsystemd/sd-netlink/sd-netlink.c @@ -1023,3 +1023,18 @@ int sd_netlink_add_match( return netlink_add_match_internal(rtnl, ret_slot, groups, n_groups, type, 0, callback, destroy_callback, userdata, description); } + +int sd_netlink_attach_filter(sd_netlink *nl, size_t len, struct sock_filter *filter) { + assert_return(nl, -EINVAL); + assert_return(len == 0 || filter, -EINVAL); + + if (setsockopt(nl->fd, SOL_SOCKET, + len == 0 ? SO_DETACH_FILTER : SO_ATTACH_FILTER, + &(struct sock_fprog) { + .len = len, + .filter = filter, + }, sizeof(struct sock_fprog)) < 0) + return -errno; + + return 0; +} diff --git a/src/systemd/sd-netlink.h b/src/systemd/sd-netlink.h index fdf802041a9..66a1bb6a33f 100644 --- a/src/systemd/sd-netlink.h +++ b/src/systemd/sd-netlink.h @@ -21,6 +21,7 @@ #include #include #include +#include #include #include @@ -68,6 +69,7 @@ int sd_netlink_add_match(sd_netlink *nl, sd_netlink_slot **ret_slot, uint16_t ma int sd_netlink_attach_event(sd_netlink *nl, sd_event *e, int64_t priority); int sd_netlink_detach_event(sd_netlink *nl); +int sd_netlink_attach_filter(sd_netlink *nl, size_t len, struct sock_filter *filter); /* message */ int sd_netlink_message_append_string(sd_netlink_message *m, unsigned short type, const char *data);