network-internal.c revision c6f7c917a1b494d4455800823472227463f87438
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek This file is part of systemd.
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek Copyright (C) 2013 Tom Gundersen <teg@jklm.no>
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek systemd is free software; you can redistribute it and/or modify it
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek under the terms of the GNU Lesser General Public License as published by
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek the Free Software Foundation; either version 2.1 of the License, or
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek (at your option) any later version.
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek systemd is distributed in the hope that it will be useful, but
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek WITHOUT ANY WARRANTY; without even the implied warranty of
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek Lesser General Public License for more details.
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek You should have received a copy of the GNU Lesser General Public License
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek along with systemd; If not, see <http://www.gnu.org/licenses/>.
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek#define HASH_KEY SD_ID128_MAKE(d3,1e,48,fa,90,fe,4b,4c,9d,af,d5,d7,a1,b1,2e,8a)
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozekint net_get_unique_predictable_data(struct udev_device *device, uint8_t result[8]) {
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek /* fetch some persistent data unique (on this machine) to this device */
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek FOREACH_STRING(field, "ID_NET_NAME_ONBOARD", "ID_NET_NAME_SLOT", "ID_NET_NAME_PATH", "ID_NET_NAME_MAC") {
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek name = udev_device_get_property_value(device, field);
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek /* fetch some persistent data unique to this machine */
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek /* Let's hash the machine ID plus the device name. We
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek * use a fixed, but originally randomly created hash
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek * key here. */
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozekbool net_match_config(const struct ether_addr *match_mac,
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek const char *dev_name) {
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek if (match_host && !condition_test_host(match_host))
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek if (match_virt && !condition_test_virtualization(match_virt))
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek if (match_kernel && !condition_test_kernel_command_line(match_kernel))
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek if (match_arch && !condition_test_architecture(match_arch))
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek if (match_mac && (!dev_mac || memcmp(match_mac, ether_aton(dev_mac), ETH_ALEN)))
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek if (match_path && (!dev_path || fnmatch(match_path, dev_path, 0)))
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek if (dev_parent_driver && !streq(match_driver, dev_parent_driver))
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek if (match_type && !streq_ptr(match_type, dev_type))
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek if (match_name && (!dev_name || fnmatch(match_name, dev_name, 0)))
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozekunsigned net_netmask_to_prefixlen(const struct in_addr *addr) {
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozekint config_parse_net_condition(const char *unit,
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek if (!ascii_is_valid(n) || strlen(n) >= IFNAMSIZ) {
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek log_syntax(unit, LOG_ERR, filename, line, EINVAL,
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek "Interface name is not ASCII clean or is too long, ignoring assignment: %s", rvalue);
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek if (!ascii_is_valid(n) || strlen(n) >= IFALIASZ) {
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek log_syntax(unit, LOG_ERR, filename, line, EINVAL,
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek "Interface alias is not ASCII clean or is too long, ignoring assignment: %s", rvalue);
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek r = sscanf(rvalue, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek if (r != 6) {
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek log_syntax(unit, LOG_ERR, filename, line, EINVAL,
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek "Not a valid MAC address, ignoring assignment: %s", rvalue);
b3d110fbc424a03674a6e50e489a7cbab9702f0bJakub Hrozekint net_parse_inaddr(const char *address, unsigned char *family, void *dst) {
b3d110fbc424a03674a6e50e489a7cbab9702f0bJakub Hrozek if (r > 0) {
b3d110fbc424a03674a6e50e489a7cbab9702f0bJakub Hrozek /* succsefully parsed IPv4 address */
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek } else if (r < 0)
b3d110fbc424a03674a6e50e489a7cbab9702f0bJakub Hrozek /* not an IPv4 address, so let's try IPv6 */
90afedb00608547ae1f32aa7aafd552c4b306909Jakub Hrozek if (r > 0) {
b3d110fbc424a03674a6e50e489a7cbab9702f0bJakub Hrozek /* successfully parsed IPv6 address */
b3d110fbc424a03674a6e50e489a7cbab9702f0bJakub Hrozek } else if (r < 0)