udev-builtin-net_id.c revision ad37f393fa97f4274cc3bf97a0d8c388a429037e
cfb5b3805759e63dc5e0cae6e92e1df885b5c5b6Tom Gundersen This file is part of systemd.
cfb5b3805759e63dc5e0cae6e92e1df885b5c5b6Tom Gundersen Copyright 2012 Kay Sievers <kay@vrfy.org>
cfb5b3805759e63dc5e0cae6e92e1df885b5c5b6Tom Gundersen systemd is free software; you can redistribute it and/or modify it
cfb5b3805759e63dc5e0cae6e92e1df885b5c5b6Tom Gundersen under the terms of the GNU Lesser General Public License as published by
cfb5b3805759e63dc5e0cae6e92e1df885b5c5b6Tom Gundersen the Free Software Foundation; either version 2.1 of the License, or
cfb5b3805759e63dc5e0cae6e92e1df885b5c5b6Tom Gundersen (at your option) any later version.
cfb5b3805759e63dc5e0cae6e92e1df885b5c5b6Tom Gundersen systemd is distributed in the hope that it will be useful, but
cfb5b3805759e63dc5e0cae6e92e1df885b5c5b6Tom Gundersen WITHOUT ANY WARRANTY; without even the implied warranty of
cfb5b3805759e63dc5e0cae6e92e1df885b5c5b6Tom Gundersen MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
cfb5b3805759e63dc5e0cae6e92e1df885b5c5b6Tom Gundersen Lesser General Public License for more details.
cfb5b3805759e63dc5e0cae6e92e1df885b5c5b6Tom Gundersen You should have received a copy of the GNU Lesser General Public License
cfb5b3805759e63dc5e0cae6e92e1df885b5c5b6Tom Gundersen along with systemd; If not, see <http://www.gnu.org/licenses/>.
cfb5b3805759e63dc5e0cae6e92e1df885b5c5b6Tom Gundersen * Predictable network interface device names based on:
cfb5b3805759e63dc5e0cae6e92e1df885b5c5b6Tom Gundersen * - firmware/bios-provided index numbers for on-board devices
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering * - firmware-provided pci-express hotplug slot index number
cfb5b3805759e63dc5e0cae6e92e1df885b5c5b6Tom Gundersen * - physical/geographical location of the hardware
cfb5b3805759e63dc5e0cae6e92e1df885b5c5b6Tom Gundersen * - the interface's MAC address
cfb5b3805759e63dc5e0cae6e92e1df885b5c5b6Tom Gundersen * Two character prefixes based on the type of interface:
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering * en -- ethernet
cfb5b3805759e63dc5e0cae6e92e1df885b5c5b6Tom Gundersen * Type of names:
cfb5b3805759e63dc5e0cae6e92e1df885b5c5b6Tom Gundersen * o<index> -- on-board device index number
cfb5b3805759e63dc5e0cae6e92e1df885b5c5b6Tom Gundersen * s<slot>[f<function>] -- hotplug slot index number
cfb5b3805759e63dc5e0cae6e92e1df885b5c5b6Tom Gundersen * x<MAC> -- MAC address
cfb5b3805759e63dc5e0cae6e92e1df885b5c5b6Tom Gundersen * p<bus>s<slot>[f<function>] -- PCI geographical location
cfb5b3805759e63dc5e0cae6e92e1df885b5c5b6Tom Gundersen * p<bus>s<slot>[f<function>][u<port>][...][c<config>][i<interface>]
dbe81cbd2a93088236a2e4e41eeb33378940f7b9Martin Pitt * -- USB port number chain
cfb5b3805759e63dc5e0cae6e92e1df885b5c5b6Tom Gundersen * All multi-function PCI devices will carry the [f<function>] number in the
cfb5b3805759e63dc5e0cae6e92e1df885b5c5b6Tom Gundersen * device name, including the function 0 device.
cfb5b3805759e63dc5e0cae6e92e1df885b5c5b6Tom Gundersen * For USB devices the fill chain of port numbers of hubs is composed. If the
cfb5b3805759e63dc5e0cae6e92e1df885b5c5b6Tom Gundersen * string would gt longer than the maximum of 15 characters, the name is not
cfb5b3805759e63dc5e0cae6e92e1df885b5c5b6Tom Gundersen * exported. The usual USB configuration == 1 and interface == 0 values are
cfb5b3805759e63dc5e0cae6e92e1df885b5c5b6Tom Gundersen * suppressed.
9ee18af3a036074c4021c82ae2e67f5ccaa9ea9dTom Gundersen * PCI ethernet card with firmware index
9ee18af3a036074c4021c82ae2e67f5ccaa9ea9dTom Gundersen * ID_NET_NAME_ONBOARD=eno1
9ee18af3a036074c4021c82ae2e67f5ccaa9ea9dTom Gundersen * ID_NET_NAME_ONBOARD_LABEL=Ethernet Port 1
cfb5b3805759e63dc5e0cae6e92e1df885b5c5b6Tom Gundersen * PCI ethernet card
cfb5b3805759e63dc5e0cae6e92e1df885b5c5b6Tom Gundersen * /sys/devices/pci0000:00/0000:00:1c.3/0000:05:00.0/net/ens1
dbe81cbd2a93088236a2e4e41eeb33378940f7b9Martin Pitt * ID_NET_NAME_MAC=enx000000000466
933f9caeeb2b3c1b951d330e04beb04226e5a890Daniel Mack * ID_NET_NAME_PATH=enp5s0
dbe81cbd2a93088236a2e4e41eeb33378940f7b9Martin Pitt * ID_NET_NAME_SLOT=ens1
cfb5b3805759e63dc5e0cae6e92e1df885b5c5b6Tom Gundersen * PCI ethernet card in hotplug slot with firmware index number:
cfb5b3805759e63dc5e0cae6e92e1df885b5c5b6Tom Gundersen * /sys/devices/pci0000:00/0000:00:1c.3/0000:05:00.0/net/ens1
cfb5b3805759e63dc5e0cae6e92e1df885b5c5b6Tom Gundersen * ID_NET_NAME_MAC=enx000000000466
9ee18af3a036074c4021c82ae2e67f5ccaa9ea9dTom Gundersen * ID_NET_NAME_PATH=enp5s0
cfb5b3805759e63dc5e0cae6e92e1df885b5c5b6Tom Gundersen * ID_NET_NAME_SLOT=ens1
cfb5b3805759e63dc5e0cae6e92e1df885b5c5b6Tom Gundersen * PCI wlan card:
cfb5b3805759e63dc5e0cae6e92e1df885b5c5b6Tom Gundersen * /sys/devices/pci0000:00/0000:00:1c.1/0000:03:00.0/net/wlp3s0
cfb5b3805759e63dc5e0cae6e92e1df885b5c5b6Tom Gundersen * ID_NET_NAME_MAC=wlx0024d7e31130
cfb5b3805759e63dc5e0cae6e92e1df885b5c5b6Tom Gundersen * ID_NET_NAME_PATH=wlp3s0
cfb5b3805759e63dc5e0cae6e92e1df885b5c5b6Tom Gundersen * USB built-in 3G modem:
cfb5b3805759e63dc5e0cae6e92e1df885b5c5b6Tom Gundersen * /sys/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.4/2-1.4:1.6/net/wwp0s29u1u4i6
cfb5b3805759e63dc5e0cae6e92e1df885b5c5b6Tom Gundersen * ID_NET_NAME_MAC=wwx028037ec0200
cfb5b3805759e63dc5e0cae6e92e1df885b5c5b6Tom Gundersen * ID_NET_NAME_PATH=wwp0s29u1u4i6
cfb5b3805759e63dc5e0cae6e92e1df885b5c5b6Tom Gundersen * USB Android phone:
cfb5b3805759e63dc5e0cae6e92e1df885b5c5b6Tom Gundersen * /sys/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.2/2-1.2:1.0/net/enp0s29u1u2
cfb5b3805759e63dc5e0cae6e92e1df885b5c5b6Tom Gundersen * ID_NET_NAME_MAC=enxd626b3450fb5
cfb5b3805759e63dc5e0cae6e92e1df885b5c5b6Tom Gundersen * ID_NET_NAME_PATH=enp0s29u1u2
bool mac_valid;
const char *pci_onboard_label;
const char *index;
int idx;
if (!index)
if (!index)
return -ENOENT;
if (idx <= 0)
return -EINVAL;
FILE *f;
bool single = false;
goto out;
goto out;
single = true;
out:
fclose(f);
return single;
unsigned int bus;
unsigned int slot;
unsigned int func;
int hotplug_slot = 0;
int err = 0;
return -ENOENT;
if (!pci) {
goto out;
if (!dir) {
goto out;
char *rest;
char *address;
hotplug_slot = i;
if (hotplug_slot > 0)
if (hotplug_slot > 0) {
out:
return err;
if (!parent)
return -ENOENT;
return -ENOENT;
char *ports;
char *config;
char *interf;
size_t l;
return -ENOENT;
return -EINVAL;
return -EINVAL;
return -EINVAL;
s = ports;
return -ENAMETOOLONG;
return EXIT_FAILURE;
return -ENOENT;
return -EINVAL;
return -EINVAL;
return -ENOENT;
return -EINVAL;
const char *devtype;
int err;
return EXIT_FAILURE;
if (devtype) {
if (err < 0)
goto out;
goto out;
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))
out:
return EXIT_SUCCESS;