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(Address *address, Link *link,
59 sd_rtnl_message_handler_t callback) {
60 _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *req = NULL;
63 assert(link->manager);
65 r = sd_rtnl_message_addr_new(RTM_NEWADDR, link->ifindex,
66 address->family, address->prefixlen,
67 IFA_F_PERMANENT, RT_SCOPE_UNIVERSE, &req);
69 log_error("Could not allocate RTM_NEWADDR message: %s",
74 r = sd_rtnl_message_append(req, IFA_LOCAL, &address->in_addr);
76 log_error("Could not append IFA_LOCAL attribute: %s",
81 if (address->family == AF_INET) {
82 struct in_addr broadcast;
84 broadcast.s_addr = address->in_addr.in.s_addr | address->netmask.s_addr;
86 r = sd_rtnl_message_append(req, IFA_BROADCAST, &broadcast);
88 log_error("Could not append IFA_BROADCAST attribute: %s",
95 r = sd_rtnl_message_append(req, IFA_LABEL, address->label);
97 log_error("Could not append IFA_LABEL attribute: %s",
103 r = sd_rtnl_call_async(link->manager->rtnl, req, callback, link, 0, NULL);
105 log_error("Could not send rtnetlink message: %s", strerror(-r));
109 link->rtnl_messages ++;
114 int config_parse_address(const char *unit,
115 const char *filename,
123 _cleanup_address_free_ Address *n = NULL;
124 _cleanup_free_ char *address = NULL;
133 r = address_new(userdata, &n);
137 /* Address=address/prefixlen */
140 e = strchr(rvalue, '/');
143 r = safe_atou(e + 1, &i);
145 log_syntax(unit, LOG_ERR, filename, line, EINVAL,
146 "Interface prefix length is invalid, "
147 "ignoring assignment: %s", e + 1);
151 n->prefixlen = (unsigned char) i;
152 n->netmask.s_addr = htonl(0xfffffffflu >> n->prefixlen);
154 address = strndup(rvalue, e - rvalue);
158 address = strdup(rvalue);
163 r = net_parse_inaddr(address, &n->family, &n->in_addr);
165 log_syntax(unit, LOG_ERR, filename, line, EINVAL,
166 "Address is invalid, ignoring assignment: %s", address);