1 /* SPDX-License-Identifier: LGPL-2.1+ */
4 #include <linux/if_addrlabel.h>
6 #include "alloc-util.h"
7 #include "conf-parser.h"
8 #include "networkd-address-label.h"
9 #include "netlink-util.h"
10 #include "networkd-manager.h"
11 #include "parse-util.h"
12 #include "socket-util.h"
14 void address_label_free(AddressLabel
*label
) {
19 LIST_REMOVE(labels
, label
->network
->address_labels
, label
);
20 assert(label
->network
->n_address_labels
> 0);
21 label
->network
->n_address_labels
--;
24 hashmap_remove(label
->network
->address_labels_by_section
, label
->section
);
25 network_config_section_free(label
->section
);
32 static int address_label_new_static(Network
*network
, const char *filename
, unsigned section_line
, AddressLabel
**ret
) {
33 _cleanup_(network_config_section_freep
) NetworkConfigSection
*n
= NULL
;
34 _cleanup_(address_label_freep
) AddressLabel
*label
= NULL
;
39 assert(!!filename
== (section_line
> 0));
42 r
= network_config_section_new(filename
, section_line
, &n
);
46 label
= hashmap_get(network
->address_labels_by_section
, n
);
48 *ret
= TAKE_PTR(label
);
54 label
= new(AddressLabel
, 1);
58 *label
= (AddressLabel
) {
62 LIST_APPEND(labels
, network
->address_labels
, label
);
63 network
->n_address_labels
++;
66 label
->section
= TAKE_PTR(n
);
68 r
= hashmap_ensure_allocated(&network
->address_labels_by_section
, &network_config_hash_ops
);
72 r
= hashmap_put(network
->address_labels_by_section
, label
->section
, label
);
77 *ret
= TAKE_PTR(label
);
82 static int address_label_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
89 assert(link
->address_label_messages
> 0);
91 link
->address_label_messages
--;
93 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
96 r
= sd_netlink_message_get_errno(m
);
97 if (r
< 0 && r
!= -EEXIST
)
98 log_link_warning_errno(link
, r
, "could not set address label: %m");
100 manager_rtnl_process_address(rtnl
, m
, link
->manager
);
102 if (link
->address_label_messages
== 0)
103 log_link_debug(link
, "Addresses label set");
108 int address_label_configure(
111 link_netlink_message_handler_t callback
,
114 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
119 assert(link
->ifindex
> 0);
120 assert(link
->manager
);
121 assert(link
->manager
->rtnl
);
123 r
= sd_rtnl_message_new_addrlabel(link
->manager
->rtnl
, &req
, RTM_NEWADDRLABEL
,
124 link
->ifindex
, AF_INET6
);
126 return log_error_errno(r
, "Could not allocate RTM_NEWADDR message: %m");
128 r
= sd_rtnl_message_addrlabel_set_prefixlen(req
, label
->prefixlen
);
130 return log_error_errno(r
, "Could not set prefixlen: %m");
132 r
= sd_netlink_message_append_u32(req
, IFAL_LABEL
, label
->label
);
134 return log_error_errno(r
, "Could not append IFAL_LABEL attribute: %m");
136 r
= sd_netlink_message_append_in6_addr(req
, IFA_ADDRESS
, &label
->in_addr
.in6
);
138 return log_error_errno(r
, "Could not append IFA_ADDRESS attribute: %m");
140 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
,
141 callback
?: address_label_handler
,
142 link_netlink_destroy_callback
, link
);
144 return log_error_errno(r
, "Could not send rtnetlink message: %m");
151 int config_parse_address_label_prefix(const char *unit
,
152 const char *filename
,
155 unsigned section_line
,
162 _cleanup_(address_label_free_or_set_invalidp
) AddressLabel
*n
= NULL
;
163 Network
*network
= userdata
;
172 r
= address_label_new_static(network
, filename
, section_line
, &n
);
176 r
= in_addr_prefix_from_string(rvalue
, AF_INET6
, &n
->in_addr
, &n
->prefixlen
);
178 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Address label is invalid, ignoring assignment: %s", rvalue
);
187 int config_parse_address_label(
189 const char *filename
,
192 unsigned section_line
,
199 _cleanup_(address_label_free_or_set_invalidp
) AddressLabel
*n
= NULL
;
200 Network
*network
= userdata
;
210 r
= address_label_new_static(network
, filename
, section_line
, &n
);
214 r
= safe_atou32(rvalue
, &k
);
216 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse address label, ignoring: %s", rvalue
);
220 if (k
== 0xffffffffUL
) {
221 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Address label is invalid, ignoring: %s", rvalue
);