]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd-network/network-internal.c
2 This file is part of systemd.
4 Copyright (C) 2013 Tom Gundersen <teg@jklm.no>
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 #include <arpa/inet.h>
22 #include <netinet/ether.h>
26 #include "alloc-util.h"
27 #include "condition.h"
28 #include "conf-parser.h"
29 #include "dhcp-lease-internal.h"
30 #include "ether-addr-util.h"
31 #include "hexdecoct.h"
33 #include "network-internal.h"
34 #include "parse-util.h"
35 #include "siphash24.h"
36 #include "socket-util.h"
37 #include "string-util.h"
42 const char *net_get_name(struct udev_device
*device
) {
43 const char *name
, *field
;
47 /* fetch some persistent data unique (on this machine) to this device */
48 FOREACH_STRING(field
, "ID_NET_NAME_ONBOARD", "ID_NET_NAME_SLOT", "ID_NET_NAME_PATH", "ID_NET_NAME_MAC") {
49 name
= udev_device_get_property_value(device
, field
);
57 #define HASH_KEY SD_ID128_MAKE(d3,1e,48,fa,90,fe,4b,4c,9d,af,d5,d7,a1,b1,2e,8a)
59 int net_get_unique_predictable_data(struct udev_device
*device
, uint64_t *result
) {
61 const char *name
= NULL
;
67 name
= net_get_name(device
);
72 sz
= sizeof(sd_id128_t
) + l
;
75 /* fetch some persistent data unique to this machine */
76 r
= sd_id128_get_machine((sd_id128_t
*) v
);
79 memcpy(v
+ sizeof(sd_id128_t
), name
, l
);
81 /* Let's hash the machine ID plus the device name. We
82 * use a fixed, but originally randomly created hash
84 *result
= htole64(siphash24(v
, sz
, HASH_KEY
.bytes
));
89 bool net_match_config(const struct ether_addr
*match_mac
,
90 char * const *match_paths
,
91 char * const *match_drivers
,
92 char * const *match_types
,
93 char * const *match_names
,
94 Condition
*match_host
,
95 Condition
*match_virt
,
96 Condition
*match_kernel
,
97 Condition
*match_arch
,
98 const struct ether_addr
*dev_mac
,
100 const char *dev_parent_driver
,
101 const char *dev_driver
,
102 const char *dev_type
,
103 const char *dev_name
) {
105 if (match_host
&& !condition_test(match_host
))
108 if (match_virt
&& !condition_test(match_virt
))
111 if (match_kernel
&& !condition_test(match_kernel
))
114 if (match_arch
&& !condition_test(match_arch
))
117 if (match_mac
&& (!dev_mac
|| memcmp(match_mac
, dev_mac
, ETH_ALEN
)))
120 if (!strv_isempty(match_paths
) &&
121 (!dev_path
|| !strv_fnmatch(match_paths
, dev_path
, 0)))
124 if (!strv_isempty(match_drivers
) &&
125 (!dev_driver
|| !strv_fnmatch(match_drivers
, dev_driver
, 0)))
128 if (!strv_isempty(match_types
) &&
129 (!dev_type
|| !strv_fnmatch_or_empty(match_types
, dev_type
, 0)))
132 if (!strv_isempty(match_names
) &&
133 (!dev_name
|| !strv_fnmatch_or_empty(match_names
, dev_name
, 0)))
139 int config_parse_net_condition(const char *unit
,
140 const char *filename
,
143 unsigned section_line
,
150 ConditionType cond
= ltype
;
151 Condition
**ret
= data
;
154 _cleanup_free_
char *s
= NULL
;
161 negate
= rvalue
[0] == '!';
169 c
= condition_new(cond
, s
, false, negate
);
174 condition_free(*ret
);
180 int config_parse_ifnames(
182 const char *filename
,
185 unsigned section_line
,
201 _cleanup_free_
char *word
= NULL
;
203 r
= extract_first_word(&rvalue
, &word
, NULL
, 0);
205 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Failed to parse interface name list: %s", rvalue
);
211 if (!ifname_valid(word
)) {
212 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Interface name is not valid or too long, ignoring assignment: %s", rvalue
);
216 r
= strv_push(sv
, word
);
226 int config_parse_ifalias(const char *unit
,
227 const char *filename
,
230 unsigned section_line
,
238 _cleanup_free_
char *n
= NULL
;
249 if (!ascii_is_valid(n
) || strlen(n
) >= IFALIASZ
) {
250 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Interface alias is not ASCII clean or is too long, ignoring assignment: %s", rvalue
);
264 int config_parse_hwaddr(const char *unit
,
265 const char *filename
,
268 unsigned section_line
,
274 struct ether_addr
**hwaddr
= data
;
275 struct ether_addr
*n
;
285 n
= new0(struct ether_addr
, 1);
289 start
= rvalue
+ strspn(rvalue
, WHITESPACE
);
290 r
= ether_addr_from_string(start
, n
, &offset
);
292 if (r
|| (start
[offset
+ strspn(start
+ offset
, WHITESPACE
)] != '\0')) {
293 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Not a valid MAC address, ignoring assignment: %s", rvalue
);
304 int config_parse_iaid(const char *unit
,
305 const char *filename
,
308 unsigned section_line
,
322 r
= safe_atou32(rvalue
, &iaid
);
324 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
325 "Unable to read IAID, ignoring assignment: %s", rvalue
);
329 *((uint32_t *)data
) = iaid
;
334 void serialize_in_addrs(FILE *f
, const struct in_addr
*addresses
, size_t size
) {
341 for (i
= 0; i
< size
; i
++)
342 fprintf(f
, "%s%s", inet_ntoa(addresses
[i
]),
343 (i
< (size
- 1)) ? " ": "");
346 int deserialize_in_addrs(struct in_addr
**ret
, const char *string
) {
347 _cleanup_free_
struct in_addr
*addresses
= NULL
;
354 _cleanup_free_
char *word
= NULL
;
355 struct in_addr
*new_addresses
;
358 r
= extract_first_word(&string
, &word
, NULL
, 0);
364 new_addresses
= realloc(addresses
, (size
+ 1) * sizeof(struct in_addr
));
368 addresses
= new_addresses
;
370 r
= inet_pton(AF_INET
, word
, &(addresses
[size
]));
383 void serialize_in6_addrs(FILE *f
, const struct in6_addr
*addresses
, size_t size
) {
390 for (i
= 0; i
< size
; i
++) {
391 char buffer
[INET6_ADDRSTRLEN
];
393 fputs(inet_ntop(AF_INET6
, addresses
+i
, buffer
, sizeof(buffer
)), f
);
400 int deserialize_in6_addrs(struct in6_addr
**ret
, const char *string
) {
401 _cleanup_free_
struct in6_addr
*addresses
= NULL
;
408 _cleanup_free_
char *word
= NULL
;
409 struct in6_addr
*new_addresses
;
412 r
= extract_first_word(&string
, &word
, NULL
, 0);
418 new_addresses
= realloc(addresses
, (size
+ 1) * sizeof(struct in6_addr
));
422 addresses
= new_addresses
;
424 r
= inet_pton(AF_INET6
, word
, &(addresses
[size
]));
437 void serialize_dhcp_routes(FILE *f
, const char *key
, sd_dhcp_route
**routes
, size_t size
) {
445 fprintf(f
, "%s=", key
);
447 for (i
= 0; i
< size
; i
++) {
448 struct in_addr dest
, gw
;
451 assert_se(sd_dhcp_route_get_destination(routes
[i
], &dest
) >= 0);
452 assert_se(sd_dhcp_route_get_gateway(routes
[i
], &gw
) >= 0);
453 assert_se(sd_dhcp_route_get_destination_prefix_length(routes
[i
], &length
) >= 0);
455 fprintf(f
, "%s/%" PRIu8
, inet_ntoa(dest
), length
);
456 fprintf(f
, ",%s%s", inet_ntoa(gw
), (i
< (size
- 1)) ? " ": "");
462 int deserialize_dhcp_routes(struct sd_dhcp_route
**ret
, size_t *ret_size
, size_t *ret_allocated
, const char *string
) {
463 _cleanup_free_
struct sd_dhcp_route
*routes
= NULL
;
464 size_t size
= 0, allocated
= 0;
468 assert(ret_allocated
);
471 /* WORD FORMAT: dst_ip/dst_prefixlen,gw_ip */
473 _cleanup_free_
char *word
= NULL
;
478 r
= extract_first_word(&string
, &word
, NULL
, 0);
484 if (!GREEDY_REALLOC(routes
, allocated
, size
+ 1))
490 tok_end
= strchr(tok
, '/');
495 r
= inet_aton(tok
, &routes
[size
].dst_addr
);
501 /* get the prefixlen */
502 tok_end
= strchr(tok
, ',');
508 r
= safe_atou(tok
, &n
);
512 routes
[size
].dst_prefixlen
= (uint8_t) n
;
515 /* get the gateway */
516 r
= inet_aton(tok
, &routes
[size
].gw_addr
);
524 *ret_allocated
= allocated
;
531 int serialize_dhcp_option(FILE *f
, const char *key
, const void *data
, size_t size
) {
532 _cleanup_free_
char *hex_buf
= NULL
;
538 hex_buf
= hexmem(data
, size
);
542 fprintf(f
, "%s=%s\n", key
, hex_buf
);
547 int deserialize_dhcp_option(void **data
, size_t *data_len
, const char *string
) {
552 if (strlen(string
) % 2)
555 return unhexmem(string
, strlen(string
), (void **)data
, data_len
);