]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-netlink/netlink-util.h
netlink: move resolve_ifname() or friends to netlink-util.[ch]
[thirdparty/systemd.git] / src / libsystemd / sd-netlink / netlink-util.h
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 #include <linux/rtnetlink.h>
5
6 #include "sd-netlink.h"
7
8 #include "ether-addr-util.h"
9 #include "in-addr-util.h"
10 #include "ordered-set.h"
11 #include "socket-util.h"
12 #include "util.h"
13
14 /* See struct rtvia in rtnetlink.h */
15 typedef struct RouteVia {
16 uint16_t family;
17 union in_addr_union address;
18 } _packed_ RouteVia;
19
20 typedef struct MultipathRoute {
21 RouteVia gateway;
22 uint32_t weight;
23 int ifindex;
24 char *ifname;
25 } MultipathRoute;
26
27 MultipathRoute *multipath_route_free(MultipathRoute *m);
28 DEFINE_TRIVIAL_CLEANUP_FUNC(MultipathRoute*, multipath_route_free);
29
30 int multipath_route_dup(const MultipathRoute *m, MultipathRoute **ret);
31
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);
35
36 static inline bool rtnl_message_type_is_neigh(uint16_t type) {
37 return IN_SET(type, RTM_NEWNEIGH, RTM_GETNEIGH, RTM_DELNEIGH);
38 }
39
40 static inline bool rtnl_message_type_is_route(uint16_t type) {
41 return IN_SET(type, RTM_NEWROUTE, RTM_GETROUTE, RTM_DELROUTE);
42 }
43
44 static inline bool rtnl_message_type_is_nexthop(uint16_t type) {
45 return IN_SET(type, RTM_NEWNEXTHOP, RTM_GETNEXTHOP, RTM_DELNEXTHOP);
46 }
47
48 static inline bool rtnl_message_type_is_link(uint16_t type) {
49 return IN_SET(type,
50 RTM_NEWLINK, RTM_SETLINK, RTM_GETLINK, RTM_DELLINK,
51 RTM_NEWLINKPROP, RTM_DELLINKPROP, RTM_GETLINKPROP);
52 }
53
54 static inline bool rtnl_message_type_is_addr(uint16_t type) {
55 return IN_SET(type, RTM_NEWADDR, RTM_GETADDR, RTM_DELADDR);
56 }
57
58 static inline bool rtnl_message_type_is_addrlabel(uint16_t type) {
59 return IN_SET(type, RTM_NEWADDRLABEL, RTM_DELADDRLABEL, RTM_GETADDRLABEL);
60 }
61
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);
64 }
65
66 static inline bool rtnl_message_type_is_qdisc(uint16_t type) {
67 return IN_SET(type, RTM_NEWQDISC, RTM_DELQDISC, RTM_GETQDISC);
68 }
69
70 static inline bool rtnl_message_type_is_tclass(uint16_t type) {
71 return IN_SET(type, RTM_NEWTCLASS, RTM_DELTCLASS, RTM_GETTCLASS);
72 }
73
74 static inline bool rtnl_message_type_is_mdb(uint16_t type) {
75 return IN_SET(type, RTM_NEWMDB, RTM_DELMDB, RTM_GETMDB);
76 }
77
78 int rtnl_set_link_name(sd_netlink **rtnl, int ifindex, const char *name);
79 int rtnl_set_link_properties(
80 sd_netlink **rtnl,
81 int ifindex,
82 const char *alias,
83 const struct ether_addr *mac,
84 uint32_t txqueues,
85 uint32_t rxqueues,
86 uint32_t txqueuelen,
87 uint32_t mtu,
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);
99
100 int rtnl_log_parse_error(int r);
101 int rtnl_log_create_error(int r);
102
103 #define netlink_call_async(nl, ret_slot, message, callback, destroy_callback, userdata) \
104 ({ \
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__); \
111 })
112
113 #define netlink_add_match(nl, ret_slot, match, callback, destroy_callback, userdata, description) \
114 ({ \
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); \
121 })
122
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);
126
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);
129
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);
132
133 int rtattr_read_nexthop(const struct rtnexthop *rtnh, size_t size, int family, OrderedSet **ret);