chiark / gitweb /
54dd57be598b3d781e5f77046432d7a411b4d9fd
[elogind.git] / src / udev / net / link-config-parse.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4  This file is part of systemd.
5
6  Copyright (C) 2013 Tom Gundersen <teg@jklm.no>
7
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.
12
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.
17
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/>.
20 ***/
21
22 #include <netinet/ether.h>
23 #include <net/if.h>
24
25 #include "link-config.h"
26
27 #include "utf8.h"
28 #include "conf-parser.h"
29
30 static const char* const mac_policy_table[] = {
31         [MACPOLICY_PERSISTENT] = "persistent",
32         [MACPOLICY_RANDOM] = "random"
33 };
34
35 DEFINE_STRING_TABLE_LOOKUP(mac_policy, MACPolicy);
36 DEFINE_CONFIG_PARSE_ENUM(config_parse_mac_policy, mac_policy, MACPolicy, "Failed to parse MAC address policy");
37
38 static const char* const name_policy_table[] = {
39         [NAMEPOLICY_ONBOARD] = "onboard",
40         [NAMEPOLICY_SLOT] = "slot",
41         [NAMEPOLICY_PATH] = "path",
42         [NAMEPOLICY_MAC] = "mac"
43 };
44
45 DEFINE_STRING_TABLE_LOOKUP(name_policy, NamePolicy);
46 DEFINE_CONFIG_PARSE_ENUMV(config_parse_name_policy, name_policy, NamePolicy, _NAMEPOLICY_INVALID, "Failed to parse interface name policy");
47
48 int config_parse_ifname(const char *unit,
49                         const char *filename,
50                         unsigned line,
51                         const char *section,
52                         const char *lvalue,
53                         int ltype,
54                         const char *rvalue,
55                         void *data,
56                         void *userdata) {
57
58         char **s = data;
59         char *n;
60
61         assert(filename);
62         assert(lvalue);
63         assert(rvalue);
64         assert(data);
65
66         n = strdup(rvalue);
67         if (!n)
68                 return log_oom();
69
70         if (!ascii_is_valid(n) || strlen(n) >= IFNAMSIZ) {
71                 log_syntax(unit, LOG_ERR, filename, line, EINVAL,
72                            "Interface name is not ASCII clean or is too long, ignoring assignment: %s", rvalue);
73                 free(n);
74                 return 0;
75         }
76
77         free(*s);
78         if (*n)
79                 *s = n;
80         else {
81                 free(n);
82                 *s = NULL;
83         }
84
85         return 0;
86 }
87
88 int config_parse_hwaddr(const char *unit,
89                         const char *filename,
90                         unsigned line,
91                         const char *section,
92                         const char *lvalue,
93                         int ltype,
94                         const char *rvalue,
95                         void *data,
96                         void *userdata) {
97         struct ether_addr **hwaddr = data;
98         struct ether_addr *n;
99         int r;
100
101         assert(filename);
102         assert(lvalue);
103         assert(rvalue);
104         assert(data);
105
106         n = calloc(1, sizeof(struct ether_addr));
107         if (!n)
108                 return log_oom();
109
110         r = sscanf(rvalue, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
111                    &n->ether_addr_octet[0],
112                    &n->ether_addr_octet[1],
113                    &n->ether_addr_octet[2],
114                    &n->ether_addr_octet[3],
115                    &n->ether_addr_octet[4],
116                    &n->ether_addr_octet[5]);
117         if (r != 6) {
118                 log_syntax(unit, LOG_ERR, filename, line, EINVAL,
119                            "Not a valid MAC address, ignoring assignment: %s", rvalue);
120                 free(n);
121                 return 0;
122         }
123
124         free(*hwaddr);
125         *hwaddr = n;
126
127         return 0;
128 }