chiark / gitweb /
shared: net - use u32ctz to compute prefixlen
[elogind.git] / src / shared / net-util.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 <linux/if.h>
24 #include <arpa/inet.h>
25
26 #include "net-util.h"
27 #include "log.h"
28 #include "utf8.h"
29 #include "util.h"
30 #include "conf-parser.h"
31
32 bool net_match_config(const struct ether_addr *match_mac,
33                       const char *match_path,
34                       const char *match_driver,
35                       const char *match_type,
36                       const char *match_name,
37                       const char *dev_mac,
38                       const char *dev_path,
39                       const char *dev_driver,
40                       const char *dev_type,
41                       const char *dev_name) {
42
43         if (match_mac && (!dev_mac || memcmp(match_mac, ether_aton(dev_mac), ETH_ALEN)))
44                 return 0;
45
46         if (match_path && !streq_ptr(match_path, dev_path))
47                 return 0;
48
49         if (match_driver && !streq_ptr(match_driver, dev_driver))
50                 return 0;
51
52         if (match_type && !streq_ptr(match_type, dev_type))
53                 return 0;
54
55         if (match_name && !streq_ptr(match_name, dev_name))
56                 return 0;
57
58         return 1;
59 }
60
61 unsigned net_netmask_to_prefixlen(const struct in_addr *addr) {
62         assert(addr);
63
64         return 32 - u32ctz(be32toh(addr->s_addr));
65 }
66
67 int config_parse_ifname(const char *unit,
68                         const char *filename,
69                         unsigned line,
70                         const char *section,
71                         unsigned section_line,
72                         const char *lvalue,
73                         int ltype,
74                         const char *rvalue,
75                         void *data,
76                         void *userdata) {
77
78         char **s = data;
79         char *n;
80
81         assert(filename);
82         assert(lvalue);
83         assert(rvalue);
84         assert(data);
85
86         n = strdup(rvalue);
87         if (!n)
88                 return log_oom();
89
90         if (!ascii_is_valid(n) || strlen(n) >= IFNAMSIZ) {
91                 log_syntax(unit, LOG_ERR, filename, line, EINVAL,
92                            "Interface name is not ASCII clean or is too long, ignoring assignment: %s", rvalue);
93                 free(n);
94                 return 0;
95         }
96
97         free(*s);
98         if (*n)
99                 *s = n;
100         else {
101                 free(n);
102                 *s = NULL;
103         }
104
105         return 0;
106 }
107
108 int config_parse_ifalias(const char *unit,
109                          const char *filename,
110                          unsigned line,
111                          const char *section,
112                          unsigned section_line,
113                          const char *lvalue,
114                          int ltype,
115                          const char *rvalue,
116                          void *data,
117                          void *userdata) {
118
119         char **s = data;
120         char *n;
121
122         assert(filename);
123         assert(lvalue);
124         assert(rvalue);
125         assert(data);
126
127         n = strdup(rvalue);
128         if (!n)
129                 return log_oom();
130
131         if (!ascii_is_valid(n) || strlen(n) >= IFALIASZ) {
132                 log_syntax(unit, LOG_ERR, filename, line, EINVAL,
133                            "Interface alias is not ASCII clean or is too long, ignoring assignment: %s", rvalue);
134                 free(n);
135                 return 0;
136         }
137
138         free(*s);
139         if (*n)
140                 *s = n;
141         else {
142                 free(n);
143                 *s = NULL;
144         }
145
146         return 0;
147 }
148
149 int config_parse_hwaddr(const char *unit,
150                         const char *filename,
151                         unsigned line,
152                         const char *section,
153                         unsigned section_line,
154                         const char *lvalue,
155                         int ltype,
156                         const char *rvalue,
157                         void *data,
158                         void *userdata) {
159         struct ether_addr **hwaddr = data;
160         struct ether_addr *n;
161         int r;
162
163         assert(filename);
164         assert(lvalue);
165         assert(rvalue);
166         assert(data);
167
168         n = new0(struct ether_addr, 1);
169         if (!n)
170                 return log_oom();
171
172         r = sscanf(rvalue, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
173                    &n->ether_addr_octet[0],
174                    &n->ether_addr_octet[1],
175                    &n->ether_addr_octet[2],
176                    &n->ether_addr_octet[3],
177                    &n->ether_addr_octet[4],
178                    &n->ether_addr_octet[5]);
179         if (r != 6) {
180                 log_syntax(unit, LOG_ERR, filename, line, EINVAL,
181                            "Not a valid MAC address, ignoring assignment: %s", rvalue);
182                 free(n);
183                 return 0;
184         }
185
186         free(*hwaddr);
187         *hwaddr = n;
188
189         return 0;
190 }
191
192 int net_parse_inaddr(const char *address, unsigned char *family, void *dst) {
193         int r;
194
195         assert(address);
196         assert(family);
197         assert(dst);
198
199         /* IPv4 */
200         r = inet_pton(AF_INET, address, dst);
201         if (r > 0) {
202                 /* succsefully parsed IPv4 address */
203                 if (*family == AF_UNSPEC)
204                         *family = AF_INET;
205                 else if (*family != AF_INET)
206                         return -EINVAL;
207         } else  if (r < 0)
208                 return -errno;
209         else {
210                 /* not an IPv4 address, so let's try IPv6 */
211                 r = inet_pton(AF_INET6, address, dst);
212                 if (r > 0) {
213                         /* successfully parsed IPv6 address */
214                         if (*family == AF_UNSPEC)
215                                 *family = AF_INET6;
216                         else if (*family != AF_INET6)
217                                 return -EINVAL;
218                 } else if (r < 0)
219                         return -errno;
220                 else
221                         return -EINVAL;
222         }
223
224         return 0;
225 }