/***
This file is part of systemd.
Copyright 2012 Kay Sievers <kay@vrfy.org>
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
/*
* Predictable network interface device names based on:
* - firmware/bios-provided index numbers for on-board devices
* - firmware-provided pci-express hotplug slot index number
* - physical/geographical location of the hardware
* - the interface's MAC address
*
*
* Two character prefixes based on the type of interface:
* en -- Ethernet
* sl -- serial line IP (slip)
* wl -- wlan
* ww -- wwan
*
* Type of names:
* b<number> -- BCMA bus core number
* c<bus_id> -- CCW bus group name, without leading zeros [s390]
* o<index>[d<dev_port>] -- on-board device index number
* s<slot>[f<function>][d<dev_port>] -- hotplug slot index number
* x<MAC> -- MAC address
* [P<domain>]p<bus>s<slot>[f<function>][d<dev_port>]
* -- PCI geographical location
* [P<domain>]p<bus>s<slot>[f<function>][u<port>][..][c<config>][i<interface>]
* -- USB port number chain
*
* All multi-function PCI devices will carry the [f<function>] number in the
* device name, including the function 0 device.
*
* When using PCI geography, The PCI domain is only prepended when it is not 0.
*
* For USB devices the full chain of port numbers of hubs is composed. If the
* name gets longer than the maximum number of 15 characters, the name is not
* exported.
* The usual USB configuration == 1 and interface == 0 values are suppressed.
*
* PCI Ethernet card with firmware index "1":
* ID_NET_NAME_ONBOARD=eno1
* ID_NET_NAME_ONBOARD_LABEL=Ethernet Port 1
*
* PCI Ethernet card in hotplug slot with firmware index number:
* ID_NET_NAME_MAC=enx000000000466
* ID_NET_NAME_PATH=enp5s0
* ID_NET_NAME_SLOT=ens1
*
* PCI Ethernet multi-function card with 2 ports:
* ID_NET_NAME_MAC=enx78e7d1ea46da
* ID_NET_NAME_PATH=enp2s0f0
* ID_NET_NAME_MAC=enx78e7d1ea46dc
* ID_NET_NAME_PATH=enp2s0f1
*
* PCI wlan card:
* ID_NET_NAME_MAC=wlx0024d7e31130
* ID_NET_NAME_PATH=wlp3s0
*
* USB built-in 3G modem:
* ID_NET_NAME_MAC=wwx028037ec0200
* ID_NET_NAME_PATH=wwp0s29u1u4i6
*
* USB Android phone:
* ID_NET_NAME_MAC=enxd626b3450fb5
* ID_NET_NAME_PATH=enp0s29u1u2
*/
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <linux/pci_regs.h>
#include "fd-util.h"
#include "fileio.h"
#include "stdio-util.h"
#include "string-util.h"
#include "udev.h"
enum netname_type{
};
struct netnames {
bool mac_valid;
const char *pci_onboard_label;
};
/* retrieve on-board index number and label from firmware */
unsigned dev_port = 0;
size_t l;
char *s;
const char *attr;
int idx;
/* ACPI _DSM -- device specific method for naming a PCI or PCI Express device */
/* SMBIOS type 41 -- Onboard Devices Extended Information */
if (!attr)
if (!attr)
return -ENOENT;
if (idx <= 0)
return -EINVAL;
/* Some BIOSes report rubbish indexes that are excessively high (2^24-1 is an index VMware likes to report for
* example). Let's define a cut-off where we don't consider the index reliable anymore. We pick some arbitrary
* cut-off, which is somewhere beyond the realistic number of physical network interface a system might
* have. Ideally the kernel would already filter his crap for us, but it doesn't currently. */
if (idx > ONBOARD_INDEX_MAX)
return -ENOENT;
/* kernel provided port index for multiple ports on a single PCI function */
if (attr)
s = names->pci_onboard;
l = sizeof(names->pci_onboard);
if (dev_port > 0)
if (l == 0)
return 0;
}
/* read the 256 bytes PCI configuration space to check the multi-function bit */
const char *filename;
if (fd < 0)
return false;
return false;
return true;
return false;
}
size_t l;
char *s;
const char *attr;
if (sscanf(udev_device_get_sysname(names->pcidev), "%x:%x:%x.%u", &domain, &bus, &slot, &func) != 4)
return -ENOENT;
/* kernel provided port index for multiple ports on a single PCI function */
if (attr)
/* compose a name based on the raw kernel's PCI bus, slot numbers */
if (domain > 0)
if (dev_port > 0)
if (l == 0)
/* ACPI _SUN -- slot user number */
if (!pci) {
goto out;
}
if (!dir) {
goto out;
}
int i;
char *rest;
char *address;
continue;
if (rest[0] != '\0')
continue;
if (i < 1)
continue;
/* match slot address with device by stripping the function */
hotplug_slot = i;
}
if (hotplug_slot > 0)
break;
}
if (hotplug_slot > 0) {
if (domain > 0)
if (dev_port > 0)
if (l == 0)
}
out:
return err;
}
/* there can only ever be one virtio bus per parent device, so we can
safely ignore any virtio buses. see
if (!parent)
return -ENOENT;
/* check if our direct parent is a PCI device with no other bus in-between */
} else {
return -ENOENT;
}
return 0;
}
char *ports;
char *config;
char *interf;
size_t l;
char *s;
if (!usbdev)
return -ENOENT;
/* get USB port number chain, configuration, interface */
if (!s)
return -EINVAL;
ports = s+1;
if (!s)
return -EINVAL;
s[0] = '\0';
config = s+1;
if (!s)
return -EINVAL;
s[0] = '\0';
interf = s+1;
/* prefix every port number in the chain with "u" */
s = ports;
while ((s = strchr(s, '.')))
s[0] = 'u';
/* append USB config number, suppress the common config == 1 */
/* append USB interface number, suppress the interface == 0 */
if (l == 0)
return -ENAMETOOLONG;
return 0;
}
unsigned int core;
if (!bcmadev)
return -ENOENT;
/* bus num:core num */
return -EINVAL;
/* suppress the common core == 0 */
if (core > 0)
return 0;
}
const char *bus_id;
int rc;
/* Retrieve the associated CCW device */
if (!cdev)
return -ENOENT;
/* Network devices are always grouped CCW devices */
return -ENOENT;
/* Retrieve bus-ID of the grouped CCW device. The bus-ID uniquely
* identifies the network device on the Linux on System z channel
* subsystem. Note that the bus-ID contains lowercase characters.
*/
if (!bus_id)
return -ENOENT;
/* Check the length of the bus-ID. Rely on that the kernel provides
* a correct bus-ID; alternatively, improve this check and parse and
* verify each bus-ID part...
*/
return -EINVAL;
/* Strip leading zeros from the bus id for aesthetic purposes. This
* keeps the ccw names stable, yet much shorter in general case of
* bus_id 0.0.0600 -> 600. This is similar to e.g. how PCI domain is
* not prepended when it is zero.
*/
/* Store the CCW bus-ID for use as network device name */
return 0;
}
const char *s;
unsigned int i;
/* check for NET_ADDR_PERM, skip random MAC addresses */
if (!s)
return EXIT_FAILURE;
if (i != 0)
return 0;
if (!s)
return -ENOENT;
return -EINVAL;
/* skip empty MAC addresses */
return -EINVAL;
return 0;
}
/* IEEE Organizationally Unique Identifier vendor string */
return -ENOENT;
/* skip commonly misused 00:00:00 (Xerox) prefix */
return -EINVAL;
return 0;
}
const char *s;
const char *p;
unsigned int i;
const char *devtype;
int err;
/* handle only ARPHRD_ETHER and ARPHRD_SLIP devices */
if (!s)
return EXIT_FAILURE;
switch (i) {
case ARPHRD_ETHER:
prefix = "en";
break;
case ARPHRD_SLIP:
prefix = "sl";
break;
default:
return 0;
}
/* skip stacked devices, like VLANs, ... */
if (!s)
return EXIT_FAILURE;
if (!p)
return EXIT_FAILURE;
if (!streq(s, p))
return 0;
if (devtype) {
prefix = "wl";
prefix = "ww";
}
}
/* get path names for Linux on System z network devices */
goto out;
}
/* get PCI based path names, we compose only PCI based paths */
if (err < 0)
goto out;
/* plain PCI device */
if (names.pci_onboard[0])
if (names.pci_onboard_label)
goto out;
}
/* USB device */
if (snprintf(str, sizeof(str), "%s%s%s", prefix, names.pci_path, names.usb_ports) < (int)sizeof(str))
if (snprintf(str, sizeof(str), "%s%s%s", prefix, names.pci_slot, names.usb_ports) < (int)sizeof(str))
goto out;
}
/* Broadcom bus */
if (snprintf(str, sizeof(str), "%s%s%s", prefix, names.pci_path, names.bcma_core) < (int)sizeof(str))
if (snprintf(str, sizeof(str), "%s%s%s", prefix, names.pci_slot, names.bcma_core) < (int)sizeof(str))
goto out;
}
out:
return EXIT_SUCCESS;
}
.name = "net_id",
.cmd = builtin_net_id,
.help = "Network device properties",
};