network-internal.c revision fc5414305d04799e0bdb6f8ca46ec70acd487f89
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering This file is part of systemd.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Copyright (C) 2013 Tom Gundersen <teg@jklm.no>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is free software; you can redistribute it and/or modify it
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering under the terms of the GNU Lesser General Public License as published by
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering (at your option) any later version.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is distributed in the hope that it will be useful, but
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Lesser General Public License for more details.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering You should have received a copy of the GNU Lesser General Public License
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringconst char *net_get_name(struct udev_device *device) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering /* fetch some persistent data unique (on this machine) to this device */
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering FOREACH_STRING(field, "ID_NET_NAME_ONBOARD", "ID_NET_NAME_SLOT",
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering name = udev_device_get_property_value(device, field);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#define HASH_KEY SD_ID128_MAKE(d3,1e,48,fa,90,fe,4b,4c,9d,af,d5,d7,a1,b1,2e,8a)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringint net_get_unique_predictable_data(struct udev_device *device, uint8_t result[8]) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering /* fetch some persistent data unique to this machine */
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = sd_id128_get_machine((sd_id128_t*) v);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering /* Let's hash the machine ID plus the device name. We
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering * use a fixed, but originally randomly created hash
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering * key here. */
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering siphash24(result, v, sz, HASH_KEY.bytes);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringbool net_match_config(const struct ether_addr *match_mac,
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (match_host && !condition_test_host(match_host))
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (match_virt && !condition_test_virtualization(match_virt))
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (match_kernel && !condition_test_kernel_command_line(match_kernel))
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (match_arch && !condition_test_architecture(match_arch))
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (match_mac && (!dev_mac || memcmp(match_mac, dev_mac, ETH_ALEN)))
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (match_path && (!dev_path || fnmatch(match_path, dev_path, 0)))
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (dev_parent_driver && !streq(match_driver, dev_parent_driver))
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering else if (!streq_ptr(match_driver, dev_driver))
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (match_type && !streq_ptr(match_type, dev_type))
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (match_name && (!dev_name || fnmatch(match_name, dev_name, 0)))
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poetteringunsigned net_netmask_to_prefixlen(const struct in_addr *addr) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return 32 - u32ctz(be32toh(addr->s_addr));
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poetteringint config_parse_net_condition(const char *unit,
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering c = condition_new(cond, s, false, negate);
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering if (!ascii_is_valid(n) || strlen(n) >= IFNAMSIZ) {
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering log_syntax(unit, LOG_ERR, filename, line, EINVAL,
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering "Interface name is not ASCII clean or is too long, ignoring assignment: %s", rvalue);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringint config_parse_ifalias(const char *unit,
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (!ascii_is_valid(n) || strlen(n) >= IFALIASZ) {
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek log_syntax(unit, LOG_ERR, filename, line, EINVAL,
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek "Interface alias is not ASCII clean or is too long, ignoring assignment: %s", rvalue);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering r = sscanf(rvalue, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering log_syntax(unit, LOG_ERR, filename, line, EINVAL,
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering "Not a valid MAC address, ignoring assignment: %s", rvalue);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poetteringint net_parse_inaddr(const char *address, unsigned char *family, void *dst) {
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering /* succsefully parsed IPv4 address */
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering } else if (r < 0)
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering /* not an IPv4 address, so let's try IPv6 */
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering /* successfully parsed IPv6 address */
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek } else if (r < 0)
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poetteringvoid serialize_in_addrs(FILE *f, const char *key, struct in_addr *addresses, size_t size) {
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering for (i = 0; i < size; i++)
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering fprintf(f, "%s%s", inet_ntoa(addresses[i]),
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poetteringint deserialize_in_addrs(struct in_addr **ret, size_t *ret_size, const char *string) {
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek _cleanup_free_ struct in_addr *addresses = NULL;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek FOREACH_WORD(word, len, string, state) {
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek _cleanup_free_ char *addr_str = NULL;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek new_addresses = realloc(addresses, (size + 1) * sizeof(struct in_addr));
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek r = inet_pton(AF_INET, addr_str, &(addresses[size]));
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmekint deserialize_in6_addrs(struct in6_addr **ret, size_t *ret_size, const char *string) {
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek _cleanup_free_ struct in6_addr *addresses = NULL;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek FOREACH_WORD(word, len, string, state) {
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek _cleanup_free_ char *addr_str = NULL;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek new_addresses = realloc(addresses, (size + 1) * sizeof(struct in6_addr));