1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright (C) 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/>.
22 #include <netinet/ether.h>
24 #include <arpa/inet.h>
31 #include "conf-parser.h"
32 #include "condition.h"
34 bool net_match_config(const struct ether_addr *match_mac,
35 const char *match_path,
36 const char *match_driver,
37 const char *match_type,
38 const char *match_name,
39 Condition *match_host,
40 Condition *match_virt,
41 Condition *match_kernel,
44 const char *dev_driver,
46 const char *dev_name) {
48 if (match_host && !condition_test_host(match_host))
51 if (match_virt && !condition_test_virtualization(match_virt))
54 if (match_kernel && !condition_test_kernel_command_line(match_kernel))
57 if (match_mac && (!dev_mac || memcmp(match_mac, ether_aton(dev_mac), ETH_ALEN)))
60 if (match_path && (!dev_path || fnmatch(match_path, dev_path, 0)))
63 if (match_driver && !streq_ptr(match_driver, dev_driver))
66 if (match_type && !streq_ptr(match_type, dev_type))
69 if (match_name && (!dev_path || fnmatch(match_name, dev_name, 0)))
75 unsigned net_netmask_to_prefixlen(const struct in_addr *addr) {
78 return 32 - u32ctz(be32toh(addr->s_addr));
81 int config_parse_net_condition(const char *unit,
85 unsigned section_line,
92 ConditionType cond = ltype;
93 Condition **ret = data;
96 _cleanup_free_ char *s = NULL;
103 negate = rvalue[0] == '!';
111 c = condition_new(cond, s, false, negate);
116 condition_free(*ret);
122 int config_parse_ifname(const char *unit,
123 const char *filename,
126 unsigned section_line,
145 if (!ascii_is_valid(n) || strlen(n) >= IFNAMSIZ) {
146 log_syntax(unit, LOG_ERR, filename, line, EINVAL,
147 "Interface name is not ASCII clean or is too long, ignoring assignment: %s", rvalue);
163 int config_parse_ifalias(const char *unit,
164 const char *filename,
167 unsigned section_line,
186 if (!ascii_is_valid(n) || strlen(n) >= IFALIASZ) {
187 log_syntax(unit, LOG_ERR, filename, line, EINVAL,
188 "Interface alias is not ASCII clean or is too long, ignoring assignment: %s", rvalue);
204 int config_parse_hwaddr(const char *unit,
205 const char *filename,
208 unsigned section_line,
214 struct ether_addr **hwaddr = data;
215 struct ether_addr *n;
223 n = new0(struct ether_addr, 1);
227 r = sscanf(rvalue, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
228 &n->ether_addr_octet[0],
229 &n->ether_addr_octet[1],
230 &n->ether_addr_octet[2],
231 &n->ether_addr_octet[3],
232 &n->ether_addr_octet[4],
233 &n->ether_addr_octet[5]);
235 log_syntax(unit, LOG_ERR, filename, line, EINVAL,
236 "Not a valid MAC address, ignoring assignment: %s", rvalue);
247 int net_parse_inaddr(const char *address, unsigned char *family, void *dst) {
255 r = inet_pton(AF_INET, address, dst);
257 /* succsefully parsed IPv4 address */
258 if (*family == AF_UNSPEC)
260 else if (*family != AF_INET)
265 /* not an IPv4 address, so let's try IPv6 */
266 r = inet_pton(AF_INET6, address, dst);
268 /* successfully parsed IPv6 address */
269 if (*family == AF_UNSPEC)
271 else if (*family != AF_INET6)