]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-netlink/netlink-util.h
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
4 #include <linux/rtnetlink.h>
6 #include "sd-netlink.h"
8 #include "ether-addr-util.h"
9 #include "in-addr-util.h"
10 #include "ordered-set.h"
11 #include "socket-util.h"
14 /* See struct rtvia in rtnetlink.h */
15 typedef struct RouteVia
{
17 union in_addr_union address
;
20 typedef struct MultipathRoute
{
27 MultipathRoute
*multipath_route_free(MultipathRoute
*m
);
28 DEFINE_TRIVIAL_CLEANUP_FUNC(MultipathRoute
*, multipath_route_free
);
30 int multipath_route_dup(const MultipathRoute
*m
, MultipathRoute
**ret
);
32 int rtnl_message_new_synthetic_error(sd_netlink
*rtnl
, int error
, uint32_t serial
, sd_netlink_message
**ret
);
33 uint32_t rtnl_message_get_serial(sd_netlink_message
*m
);
34 void rtnl_message_seal(sd_netlink_message
*m
);
36 static inline bool rtnl_message_type_is_neigh(uint16_t type
) {
37 return IN_SET(type
, RTM_NEWNEIGH
, RTM_GETNEIGH
, RTM_DELNEIGH
);
40 static inline bool rtnl_message_type_is_route(uint16_t type
) {
41 return IN_SET(type
, RTM_NEWROUTE
, RTM_GETROUTE
, RTM_DELROUTE
);
44 static inline bool rtnl_message_type_is_nexthop(uint16_t type
) {
45 return IN_SET(type
, RTM_NEWNEXTHOP
, RTM_GETNEXTHOP
, RTM_DELNEXTHOP
);
48 static inline bool rtnl_message_type_is_link(uint16_t type
) {
50 RTM_NEWLINK
, RTM_SETLINK
, RTM_GETLINK
, RTM_DELLINK
,
51 RTM_NEWLINKPROP
, RTM_DELLINKPROP
, RTM_GETLINKPROP
);
54 static inline bool rtnl_message_type_is_addr(uint16_t type
) {
55 return IN_SET(type
, RTM_NEWADDR
, RTM_GETADDR
, RTM_DELADDR
);
58 static inline bool rtnl_message_type_is_addrlabel(uint16_t type
) {
59 return IN_SET(type
, RTM_NEWADDRLABEL
, RTM_DELADDRLABEL
, RTM_GETADDRLABEL
);
62 static inline bool rtnl_message_type_is_routing_policy_rule(uint16_t type
) {
63 return IN_SET(type
, RTM_NEWRULE
, RTM_DELRULE
, RTM_GETRULE
);
66 static inline bool rtnl_message_type_is_qdisc(uint16_t type
) {
67 return IN_SET(type
, RTM_NEWQDISC
, RTM_DELQDISC
, RTM_GETQDISC
);
70 static inline bool rtnl_message_type_is_tclass(uint16_t type
) {
71 return IN_SET(type
, RTM_NEWTCLASS
, RTM_DELTCLASS
, RTM_GETTCLASS
);
74 static inline bool rtnl_message_type_is_mdb(uint16_t type
) {
75 return IN_SET(type
, RTM_NEWMDB
, RTM_DELMDB
, RTM_GETMDB
);
78 int rtnl_set_link_name(sd_netlink
**rtnl
, int ifindex
, const char *name
);
79 int rtnl_set_link_properties(
83 const struct ether_addr
*mac
,
88 uint32_t gso_max_size
,
89 size_t gso_max_segments
);
90 int rtnl_get_link_alternative_names(sd_netlink
**rtnl
, int ifindex
, char ***ret
);
91 int rtnl_set_link_alternative_names(sd_netlink
**rtnl
, int ifindex
, char * const *alternative_names
);
92 int rtnl_set_link_alternative_names_by_ifname(sd_netlink
**rtnl
, const char *ifname
, char * const *alternative_names
);
93 int rtnl_delete_link_alternative_names(sd_netlink
**rtnl
, int ifindex
, char * const *alternative_names
);
94 int rtnl_resolve_link_alternative_name(sd_netlink
**rtnl
, const char *name
);
95 int rtnl_resolve_ifname(sd_netlink
**rtnl
, const char *name
);
96 int rtnl_resolve_interface(sd_netlink
**rtnl
, const char *name
);
97 int rtnl_resolve_interface_or_warn(sd_netlink
**rtnl
, const char *name
);
98 int rtnl_get_link_info(sd_netlink
**rtnl
, int ifindex
, unsigned short *ret_iftype
, unsigned *ret_flags
);
100 int rtnl_log_parse_error(int r
);
101 int rtnl_log_create_error(int r
);
103 #define netlink_call_async(nl, ret_slot, message, callback, destroy_callback, userdata) \
105 int (*_callback_)(sd_netlink *, sd_netlink_message *, typeof(userdata)) = callback; \
106 void (*_destroy_)(typeof(userdata)) = destroy_callback; \
107 sd_netlink_call_async(nl, ret_slot, message, \
108 (sd_netlink_message_handler_t) _callback_, \
109 (sd_netlink_destroy_t) _destroy_, \
110 userdata, 0, __func__); \
113 #define netlink_add_match(nl, ret_slot, match, callback, destroy_callback, userdata, description) \
115 int (*_callback_)(sd_netlink *, sd_netlink_message *, typeof(userdata)) = callback; \
116 void (*_destroy_)(typeof(userdata)) = destroy_callback; \
117 sd_netlink_add_match(nl, ret_slot, match, \
118 (sd_netlink_message_handler_t) _callback_, \
119 (sd_netlink_destroy_t) _destroy_, \
120 userdata, description); \
123 int netlink_message_append_hw_addr(sd_netlink_message
*m
, unsigned short type
, const struct hw_addr_data
*data
);
124 int netlink_message_append_in_addr_union(sd_netlink_message
*m
, unsigned short type
, int family
, const union in_addr_union
*data
);
125 int netlink_message_append_sockaddr_union(sd_netlink_message
*m
, unsigned short type
, const union sockaddr_union
*data
);
127 int netlink_message_read_hw_addr(sd_netlink_message
*m
, unsigned short type
, struct hw_addr_data
*data
);
128 int netlink_message_read_in_addr_union(sd_netlink_message
*m
, unsigned short type
, int family
, union in_addr_union
*data
);
130 void rtattr_append_attribute_internal(struct rtattr
*rta
, unsigned short type
, const void *data
, size_t data_length
);
131 int rtattr_append_attribute(struct rtattr
**rta
, unsigned short type
, const void *data
, size_t data_length
);
133 int rtattr_read_nexthop(const struct rtnexthop
*rtnh
, size_t size
, int family
, OrderedSet
**ret
);