1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2013 Tom Gundersen <teg@jklm.no>
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
28 #include "conf-parser.h"
31 int address_new(Network *network, unsigned section, Address **ret) {
32 _cleanup_address_free_ Address *address = NULL;
35 uint64_t key = section;
36 address = hashmap_get(network->addresses_by_section, &key);
45 address = new0(Address, 1);
49 address->network = network;
51 LIST_PREPEND(addresses, network->addresses, address);
54 address->section = section;
55 hashmap_put(network->addresses_by_section, &address->section, address);
64 void address_free(Address *address) {
68 LIST_REMOVE(addresses, address->network->addresses, address);
71 hashmap_remove(address->network->addresses_by_section,
77 int address_configure(Address *address, Link *link,
78 sd_rtnl_message_handler_t callback) {
79 _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *req = NULL;
83 assert(address->family == AF_INET || address->family == AF_INET6);
85 assert(link->ifindex > 0);
86 assert(link->manager);
87 assert(link->manager->rtnl);
89 r = sd_rtnl_message_addr_new(RTM_NEWADDR, link->ifindex,
90 address->family, address->prefixlen,
91 IFA_F_PERMANENT, RT_SCOPE_UNIVERSE, &req);
93 log_error("Could not allocate RTM_NEWADDR message: %s",
98 if (address->family == AF_INET)
99 r = sd_rtnl_message_append_in_addr(req, IFA_LOCAL, &address->in_addr.in);
100 else if (address->family == AF_INET6)
101 r = sd_rtnl_message_append_in6_addr(req, IFA_LOCAL, &address->in_addr.in6);
103 log_error("Could not append IFA_LOCAL attribute: %s",
108 if (address->family == AF_INET) {
109 struct in_addr broadcast;
111 broadcast.s_addr = address->in_addr.in.s_addr | address->netmask.s_addr;
113 r = sd_rtnl_message_append_in_addr(req, IFA_BROADCAST, &broadcast);
115 log_error("Could not append IFA_BROADCAST attribute: %s",
121 if (address->label) {
122 r = sd_rtnl_message_append_string(req, IFA_LABEL, address->label);
124 log_error("Could not append IFA_LABEL attribute: %s",
130 r = sd_rtnl_call_async(link->manager->rtnl, req, callback, link, 0, NULL);
132 log_error("Could not send rtnetlink message: %s", strerror(-r));
139 int config_parse_address(const char *unit,
140 const char *filename,
143 unsigned section_line,
149 Network *network = userdata;
150 _cleanup_address_free_ Address *n = NULL;
151 _cleanup_free_ char *address = NULL;
161 if (streq(section, "Network")) {
162 /* we are not in an Address section, so treat
163 * this as the special '0' section */
167 r = address_new(network, section_line, &n);
171 /* Address=address/prefixlen */
174 e = strchr(rvalue, '/');
177 r = safe_atou(e + 1, &i);
179 log_syntax(unit, LOG_ERR, filename, line, EINVAL,
180 "Interface prefix length is invalid, "
181 "ignoring assignment: %s", e + 1);
185 n->prefixlen = (unsigned char) i;
186 n->netmask.s_addr = htonl(0xfffffffflu >> n->prefixlen);
188 address = strndup(rvalue, e - rvalue);
192 address = strdup(rvalue);
197 r = net_parse_inaddr(address, &n->family, &n->in_addr);
199 log_syntax(unit, LOG_ERR, filename, line, EINVAL,
200 "Address is invalid, ignoring assignment: %s", address);
209 int config_parse_label(const char *unit,
210 const char *filename,
213 unsigned section_line,
219 Network *network = userdata;
220 _cleanup_address_free_ Address *n = NULL;
230 r = address_new(network, section_line, &n);
234 label = strdup(rvalue);
238 if (!ascii_is_valid(label) || strlen(label) >= IFNAMSIZ) {
239 log_syntax(unit, LOG_ERR, filename, line, EINVAL,
240 "Interface label is not ASCII clean or is too"
241 " long, ignoring assignment: %s", rvalue);