1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright © 2017 Susant Sahani
7 #include <linux/if_addrlabel.h>
9 #include "alloc-util.h"
10 #include "conf-parser.h"
11 #include "networkd-address-label.h"
12 #include "netlink-util.h"
13 #include "networkd-manager.h"
14 #include "parse-util.h"
15 #include "socket-util.h"
17 int address_label_new(AddressLabel
**ret
) {
18 _cleanup_(address_label_freep
) AddressLabel
*addrlabel
= NULL
;
20 addrlabel
= new0(AddressLabel
, 1);
24 *ret
= TAKE_PTR(addrlabel
);
29 void address_label_free(AddressLabel
*label
) {
34 LIST_REMOVE(labels
, label
->network
->address_labels
, label
);
35 assert(label
->network
->n_address_labels
> 0);
36 label
->network
->n_address_labels
--;
39 hashmap_remove(label
->network
->address_labels_by_section
, label
->section
);
40 network_config_section_free(label
->section
);
47 static int address_label_new_static(Network
*network
, const char *filename
, unsigned section_line
, AddressLabel
**ret
) {
48 _cleanup_(network_config_section_freep
) NetworkConfigSection
*n
= NULL
;
49 _cleanup_(address_label_freep
) AddressLabel
*label
= NULL
;
54 assert(!!filename
== (section_line
> 0));
56 r
= network_config_section_new(filename
, section_line
, &n
);
60 label
= hashmap_get(network
->address_labels_by_section
, n
);
62 *ret
= TAKE_PTR(label
);
67 r
= address_label_new(&label
);
71 label
->section
= TAKE_PTR(n
);
73 r
= hashmap_put(network
->address_labels_by_section
, label
->section
, label
);
77 label
->network
= network
;
78 LIST_APPEND(labels
, network
->address_labels
, label
);
79 network
->n_address_labels
++;
81 *ret
= TAKE_PTR(label
);
86 int address_label_configure(
89 sd_netlink_message_handler_t callback
,
92 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
97 assert(link
->ifindex
> 0);
98 assert(link
->manager
);
99 assert(link
->manager
->rtnl
);
101 r
= sd_rtnl_message_new_addrlabel(link
->manager
->rtnl
, &req
, RTM_NEWADDRLABEL
,
102 link
->ifindex
, AF_INET6
);
104 return log_error_errno(r
, "Could not allocate RTM_NEWADDR message: %m");
106 r
= sd_rtnl_message_addrlabel_set_prefixlen(req
, label
->prefixlen
);
108 return log_error_errno(r
, "Could not set prefixlen: %m");
110 r
= sd_netlink_message_append_u32(req
, IFAL_LABEL
, label
->label
);
112 return log_error_errno(r
, "Could not append IFAL_LABEL attribute: %m");
114 r
= sd_netlink_message_append_in6_addr(req
, IFA_ADDRESS
, &label
->in_addr
.in6
);
116 return log_error_errno(r
, "Could not append IFA_ADDRESS attribute: %m");
118 r
= sd_netlink_call_async(link
->manager
->rtnl
, req
, callback
, link
, 0, NULL
);
120 return log_error_errno(r
, "Could not send rtnetlink message: %m");
127 int config_parse_address_label_prefix(const char *unit
,
128 const char *filename
,
131 unsigned section_line
,
138 _cleanup_(address_label_freep
) AddressLabel
*n
= NULL
;
139 Network
*network
= userdata
;
148 r
= address_label_new_static(network
, filename
, section_line
, &n
);
152 r
= in_addr_prefix_from_string(rvalue
, AF_INET6
, &n
->in_addr
, &n
->prefixlen
);
154 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Address label is invalid, ignoring assignment: %s", rvalue
);
163 int config_parse_address_label(
165 const char *filename
,
168 unsigned section_line
,
175 _cleanup_(address_label_freep
) AddressLabel
*n
= NULL
;
176 Network
*network
= userdata
;
186 r
= address_label_new_static(network
, filename
, section_line
, &n
);
190 r
= safe_atou32(rvalue
, &k
);
192 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse address label, ignoring: %s", rvalue
);
196 if (k
== 0xffffffffUL
) {
197 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Adress label is invalid, ignoring: %s", rvalue
);