udev-builtin-net_id.c revision f610d6de38119b372b377ec41b2a6089872d3294
/***
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
* wl -- wlan
* ww -- wwan
*
* type of names:
* o<index> -- on-board device index number
* s<slot>[f<function>] -- hotplug slot index number
* x<MAC> -- MAC address
* p<bus>s<slot>[f<function>] -- PCI geographical location
* p<bus>s<slot>[f<function>][u<port>][u<port>][c<config>][i<interface>]
* -- USB port number chain
*
* All multi-function devices will carry the [f<function>] number in the
* device name, including the function 0 device.
*
* PCI card with firmware index
* ID_NET_NAME_ONBOARD=eno1
* ID_NET_NAME_ONBOARD_LABEL=Ethernet Port 1
*
* PCI card
* ID_NET_NAME_MAC=enx000000000466
* ID_NET_NAME_PATH=enp5s0
* ID_NET_NAME_SLOT=ens1
*
* PCI card in hotplug slot with firmware index number:
* ID_NET_NAME_MAC=enx000000000466
* ID_NET_NAME_PATH=enp5s0
* ID_NET_NAME_SLOT=ens1
*
* 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 <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <linux/pci_regs.h>
#include "udev.h"
enum netname_type{
};
struct netnames {
enum netname_type type;
bool mac_valid;
struct udev_device *pcidev;
char pci_onboard[IFNAMSIZ];
const char *pci_onboard_label;
struct udev_device *usbdev;
};
/* retrieve on-board index number and label from firmware */
const char *index;
int idx;
/* ACPI _DSM -- device specific method for naming a PCI or PCI Express device */
/* SMBIOS type 41 -- Onboard Devices Extended Information */
if (!index)
if (!index)
return -ENOENT;
if (idx <= 0)
return -EINVAL;
return 0;
}
/* read the 256 bytes PCI configuration space to check the multi-function bit */
char filename[256];
FILE *f;
char config[64];
bool single = false;
if (!f)
goto out;
goto out;
single = true;
out:
fclose(f);
return single;
}
unsigned int bus;
unsigned int slot;
unsigned int func;
char slots[256];
char str[256];
int hotplug_slot = 0;
int err = 0;
/* compose a name based on the raw kernel's PCI bus, slot numbers */
return -ENOENT;
else
/* 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) {
else
}
out:
return err;
}
struct udev_device *parent;
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 name[256];
char *ports;
char *config;
char *interf;
size_t l;
char *s;
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;
}
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;
unsigned int i;
const char *devtype;
const char *prefix = "en";
int err;
/* handle only ARPHRD_ETHER devices */
if (!s)
return EXIT_FAILURE;
if (i != 1)
return 0;
if (devtype) {
prefix = "wl";
prefix = "ww";
}
}
/* 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))
}
out:
return EXIT_SUCCESS;
}
const struct udev_builtin udev_builtin_net_id = {
.name = "net_id",
.cmd = builtin_net_id,
.help = "network device properties",
};