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, Address **ret) {
32 _cleanup_address_free_ Address *address = NULL;
34 address = new0(Address, 1);
38 address->network = network;
40 LIST_PREPEND(addresses, network->addresses, address);
48 void address_free(Address *address) {
52 LIST_REMOVE(addresses, address->network->addresses, address);
58 int address_configure(Manager *manager, Address *address, Link *link) {
59 _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *req = NULL;
62 r = sd_rtnl_message_addr_new(RTM_NEWADDR, link->ifindex,
63 address->family, address->prefixlen,
64 IFA_F_PERMANENT, RT_SCOPE_UNIVERSE, &req);
66 log_error("Could not allocate RTM_NEWADDR message: %s",
71 r = sd_rtnl_message_append(req, IFA_LOCAL, &address->in_addr);
73 log_error("Could not append IFA_LOCAL attribute: %s",
78 if (address->family == AF_INET) {
79 struct in_addr broadcast;
81 broadcast.s_addr = address->in_addr.in.s_addr |
82 htonl(0xfffffffflu >> address->prefixlen);
84 r = sd_rtnl_message_append(req, IFA_BROADCAST, &broadcast);
86 log_error("Could not append IFA_BROADCAST attribute: %s",
93 r = sd_rtnl_message_append(req, IFA_LABEL, address->label);
95 log_error("Could not append IFA_LABEL attribute: %s",
101 r = sd_rtnl_send_with_reply_and_block(manager->rtnl, req, 0, NULL);
103 log_error("Could not configure address: %s", strerror(-r));
104 return r != -EEXIST ? r : 0;
107 log_info("Configured interface address");
112 int config_parse_address(const char *unit,
113 const char *filename,
121 _cleanup_address_free_ Address *n = NULL;
122 _cleanup_free_ char *address = NULL;
131 r = address_new(userdata, &n);
135 /* Address=address/prefixlen */
138 e = strchr(rvalue, '/');
141 r = safe_atou(e + 1, &i);
143 log_syntax(unit, LOG_ERR, filename, line, EINVAL,
144 "Interface prefix length is invalid, "
145 "ignoring assignment: %s", e + 1);
149 n->prefixlen = (unsigned char) i;
150 address = strndup(rvalue, e - rvalue);
154 address = strdup(rvalue);
159 r = net_parse_inaddr(address, &n->family, &n->in_addr);
161 log_syntax(unit, LOG_ERR, filename, line, EINVAL,
162 "Address is invalid, ignoring assignment: %s", address);