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