]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-netlink/netlink-util.h
Merge pull request #17185 from yuwata/ethtool-update
[thirdparty/systemd.git] / src / libsystemd / sd-netlink / netlink-util.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 #pragma once
3
4 #include <linux/rtnetlink.h>
5
6 #include "sd-netlink.h"
7
8 #include "in-addr-util.h"
9 #include "ordered-set.h"
10 #include "socket-util.h"
11 #include "util.h"
12
13 /* See struct rtvia in rtnetlink.h */
14 typedef struct RouteVia {
15 uint16_t family;
16 union in_addr_union address;
17 } _packed_ RouteVia;
18
19 typedef struct MultipathRoute {
20 RouteVia gateway;
21 int ifindex;
22 uint32_t weight;
23 } MultipathRoute;
24
25 int rtnl_message_new_synthetic_error(sd_netlink *rtnl, int error, uint32_t serial, sd_netlink_message **ret);
26 uint32_t rtnl_message_get_serial(sd_netlink_message *m);
27 void rtnl_message_seal(sd_netlink_message *m);
28
29 static inline bool rtnl_message_type_is_neigh(uint16_t type) {
30 return IN_SET(type, RTM_NEWNEIGH, RTM_GETNEIGH, RTM_DELNEIGH);
31 }
32
33 static inline bool rtnl_message_type_is_route(uint16_t type) {
34 return IN_SET(type, RTM_NEWROUTE, RTM_GETROUTE, RTM_DELROUTE);
35 }
36
37 static inline bool rtnl_message_type_is_nexthop(uint16_t type) {
38 return IN_SET(type, RTM_NEWNEXTHOP, RTM_GETNEXTHOP, RTM_DELNEXTHOP);
39 }
40
41 static inline bool rtnl_message_type_is_link(uint16_t type) {
42 return IN_SET(type,
43 RTM_NEWLINK, RTM_SETLINK, RTM_GETLINK, RTM_DELLINK,
44 RTM_NEWLINKPROP, RTM_DELLINKPROP, RTM_GETLINKPROP);
45 }
46
47 static inline bool rtnl_message_type_is_addr(uint16_t type) {
48 return IN_SET(type, RTM_NEWADDR, RTM_GETADDR, RTM_DELADDR);
49 }
50
51 static inline bool rtnl_message_type_is_addrlabel(uint16_t type) {
52 return IN_SET(type, RTM_NEWADDRLABEL, RTM_DELADDRLABEL, RTM_GETADDRLABEL);
53 }
54
55 static inline bool rtnl_message_type_is_routing_policy_rule(uint16_t type) {
56 return IN_SET(type, RTM_NEWRULE, RTM_DELRULE, RTM_GETRULE);
57 }
58
59 static inline bool rtnl_message_type_is_qdisc(uint16_t type) {
60 return IN_SET(type, RTM_NEWQDISC, RTM_DELQDISC, RTM_GETQDISC);
61 }
62
63 static inline bool rtnl_message_type_is_tclass(uint16_t type) {
64 return IN_SET(type, RTM_NEWTCLASS, RTM_DELTCLASS, RTM_GETTCLASS);
65 }
66
67 static inline bool rtnl_message_type_is_mdb(uint16_t type) {
68 return IN_SET(type, RTM_NEWMDB, RTM_DELMDB, RTM_GETMDB);
69 }
70
71 int rtnl_set_link_name(sd_netlink **rtnl, int ifindex, const char *name);
72 int rtnl_set_link_properties(sd_netlink **rtnl, int ifindex, const char *alias, const struct ether_addr *mac, uint32_t mtu);
73 int rtnl_get_link_alternative_names(sd_netlink **rtnl, int ifindex, char ***ret);
74 int rtnl_set_link_alternative_names(sd_netlink **rtnl, int ifindex, char * const *alternative_names);
75 int rtnl_set_link_alternative_names_by_ifname(sd_netlink **rtnl, const char *ifname, char * const *alternative_names);
76 int rtnl_delete_link_alternative_names(sd_netlink **rtnl, int ifindex, char * const *alternative_names);
77 int rtnl_resolve_link_alternative_name(sd_netlink **rtnl, const char *name);
78 int rtnl_get_link_iftype(sd_netlink **rtnl, int ifindex, unsigned short *ret);
79
80 int rtnl_log_parse_error(int r);
81 int rtnl_log_create_error(int r);
82
83 #define netlink_call_async(nl, ret_slot, message, callback, destroy_callback, userdata) \
84 ({ \
85 int (*_callback_)(sd_netlink *, sd_netlink_message *, typeof(userdata)) = callback; \
86 void (*_destroy_)(typeof(userdata)) = destroy_callback; \
87 sd_netlink_call_async(nl, ret_slot, message, \
88 (sd_netlink_message_handler_t) _callback_, \
89 (sd_netlink_destroy_t) _destroy_, \
90 userdata, 0, __func__); \
91 })
92
93 #define netlink_add_match(nl, ret_slot, match, callback, destroy_callback, userdata, description) \
94 ({ \
95 int (*_callback_)(sd_netlink *, sd_netlink_message *, typeof(userdata)) = callback; \
96 void (*_destroy_)(typeof(userdata)) = destroy_callback; \
97 sd_netlink_add_match(nl, ret_slot, match, \
98 (sd_netlink_message_handler_t) _callback_, \
99 (sd_netlink_destroy_t) _destroy_, \
100 userdata, description); \
101 })
102
103 int netlink_message_append_in_addr_union(sd_netlink_message *m, unsigned short type, int family, const union in_addr_union *data);
104 int netlink_message_append_sockaddr_union(sd_netlink_message *m, unsigned short type, const union sockaddr_union *data);
105
106 int netlink_message_read_in_addr_union(sd_netlink_message *m, unsigned short type, int family, union in_addr_union *data);
107
108 void rtattr_append_attribute_internal(struct rtattr *rta, unsigned short type, const void *data, size_t data_length);
109 int rtattr_append_attribute(struct rtattr **rta, unsigned short type, const void *data, size_t data_length);
110
111 int rtattr_read_nexthop(const struct rtnexthop *rtnh, size_t size, int family, OrderedSet **ret);