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 
rtnl_message_type_is_neigh(uint16_t type)32 static inline bool rtnl_message_type_is_neigh(uint16_t type) {
33         return IN_SET(type, RTM_NEWNEIGH, RTM_GETNEIGH, RTM_DELNEIGH);
34 }
35 
rtnl_message_type_is_route(uint16_t type)36 static inline bool rtnl_message_type_is_route(uint16_t type) {
37         return IN_SET(type, RTM_NEWROUTE, RTM_GETROUTE, RTM_DELROUTE);
38 }
39 
rtnl_message_type_is_nexthop(uint16_t type)40 static inline bool rtnl_message_type_is_nexthop(uint16_t type) {
41         return IN_SET(type, RTM_NEWNEXTHOP, RTM_GETNEXTHOP, RTM_DELNEXTHOP);
42 }
43 
rtnl_message_type_is_link(uint16_t type)44 static inline bool rtnl_message_type_is_link(uint16_t type) {
45         return IN_SET(type,
46                       RTM_NEWLINK, RTM_SETLINK, RTM_GETLINK, RTM_DELLINK,
47                       RTM_NEWLINKPROP, RTM_DELLINKPROP, RTM_GETLINKPROP);
48 }
49 
rtnl_message_type_is_addr(uint16_t type)50 static inline bool rtnl_message_type_is_addr(uint16_t type) {
51         return IN_SET(type, RTM_NEWADDR, RTM_GETADDR, RTM_DELADDR);
52 }
53 
rtnl_message_type_is_addrlabel(uint16_t type)54 static inline bool rtnl_message_type_is_addrlabel(uint16_t type) {
55         return IN_SET(type, RTM_NEWADDRLABEL, RTM_DELADDRLABEL, RTM_GETADDRLABEL);
56 }
57 
rtnl_message_type_is_routing_policy_rule(uint16_t type)58 static inline bool rtnl_message_type_is_routing_policy_rule(uint16_t type) {
59         return IN_SET(type, RTM_NEWRULE, RTM_DELRULE, RTM_GETRULE);
60 }
61 
rtnl_message_type_is_traffic_control(uint16_t type)62 static inline bool rtnl_message_type_is_traffic_control(uint16_t type) {
63         return IN_SET(type,
64                       RTM_NEWQDISC, RTM_DELQDISC, RTM_GETQDISC,
65                       RTM_NEWTCLASS, RTM_DELTCLASS, RTM_GETTCLASS);
66 }
67 
rtnl_message_type_is_mdb(uint16_t type)68 static inline bool rtnl_message_type_is_mdb(uint16_t type) {
69         return IN_SET(type, RTM_NEWMDB, RTM_DELMDB, RTM_GETMDB);
70 }
71 
72 int rtnl_set_link_name(sd_netlink **rtnl, int ifindex, const char *name);
73 int rtnl_set_link_properties(
74                 sd_netlink **rtnl,
75                 int ifindex,
76                 const char *alias,
77                 const struct hw_addr_data *hw_addr,
78                 uint32_t txqueues,
79                 uint32_t rxqueues,
80                 uint32_t txqueuelen,
81                 uint32_t mtu,
82                 uint32_t gso_max_size,
83                 size_t gso_max_segments);
84 int rtnl_get_link_alternative_names(sd_netlink **rtnl, int ifindex, char ***ret);
85 int rtnl_set_link_alternative_names(sd_netlink **rtnl, int ifindex, char * const *alternative_names);
86 int rtnl_set_link_alternative_names_by_ifname(sd_netlink **rtnl, const char *ifname, char * const *alternative_names);
87 int rtnl_delete_link_alternative_names(sd_netlink **rtnl, int ifindex, char * const *alternative_names);
88 int rtnl_resolve_link_alternative_name(sd_netlink **rtnl, const char *name, char **ret);
89 int rtnl_resolve_ifname(sd_netlink **rtnl, const char *name);
90 int rtnl_resolve_interface(sd_netlink **rtnl, const char *name);
91 int rtnl_resolve_interface_or_warn(sd_netlink **rtnl, const char *name);
92 int rtnl_get_link_info(
93                 sd_netlink **rtnl,
94                 int ifindex,
95                 unsigned short *ret_iftype,
96                 unsigned *ret_flags,
97                 char **ret_kind,
98                 struct hw_addr_data *ret_hw_addr,
99                 struct hw_addr_data *ret_permanent_hw_addr);
100 
101 int rtnl_log_parse_error(int r);
102 int rtnl_log_create_error(int r);
103 
104 #define netlink_call_async(nl, ret_slot, message, callback, destroy_callback, userdata) \
105         ({                                                              \
106                 int (*_callback_)(sd_netlink *, sd_netlink_message *, typeof(userdata)) = callback; \
107                 void (*_destroy_)(typeof(userdata)) = destroy_callback; \
108                 sd_netlink_call_async(nl, ret_slot, message,            \
109                                       (sd_netlink_message_handler_t) _callback_, \
110                                       (sd_netlink_destroy_t) _destroy_, \
111                                       userdata, 0, __func__);           \
112         })
113 
114 #define netlink_add_match(nl, ret_slot, match, callback, destroy_callback, userdata, description) \
115         ({                                                              \
116                 int (*_callback_)(sd_netlink *, sd_netlink_message *, typeof(userdata)) = callback; \
117                 void (*_destroy_)(typeof(userdata)) = destroy_callback; \
118                 sd_netlink_add_match(nl, ret_slot, match,               \
119                                      (sd_netlink_message_handler_t) _callback_, \
120                                      (sd_netlink_destroy_t) _destroy_,  \
121                                      userdata, description);            \
122         })
123 
124 #define genl_add_match(nl, ret_slot, family, group, cmd, callback, destroy_callback, userdata, description) \
125         ({                                                              \
126                 int (*_callback_)(sd_netlink *, sd_netlink_message *, typeof(userdata)) = callback; \
127                 void (*_destroy_)(typeof(userdata)) = destroy_callback; \
128                 sd_genl_add_match(nl, ret_slot, family, group, cmd,     \
129                                   (sd_netlink_message_handler_t) _callback_, \
130                                   (sd_netlink_destroy_t) _destroy_,     \
131                                   userdata, description);               \
132         })
133 
134 int netlink_message_append_hw_addr(sd_netlink_message *m, unsigned short type, const struct hw_addr_data *data);
135 int netlink_message_append_in_addr_union(sd_netlink_message *m, unsigned short type, int family, const union in_addr_union *data);
136 int netlink_message_append_sockaddr_union(sd_netlink_message *m, unsigned short type, const union sockaddr_union *data);
137 
138 int netlink_message_read_hw_addr(sd_netlink_message *m, unsigned short type, struct hw_addr_data *data);
139 int netlink_message_read_in_addr_union(sd_netlink_message *m, unsigned short type, int family, union in_addr_union *data);
140 
141 void rtattr_append_attribute_internal(struct rtattr *rta, unsigned short type, const void *data, size_t data_length);
142 int rtattr_append_attribute(struct rtattr **rta, unsigned short type, const void *data, size_t data_length);
143 
144 int rtattr_read_nexthop(const struct rtnexthop *rtnh, size_t size, int family, OrderedSet **ret);
145