diff --git a/sys/compat/linsysfs/linsysfs.c b/sys/compat/linsysfs/linsysfs.c index 0c0744a054a0..3b1bdfc280fc 100644 --- a/sys/compat/linsysfs/linsysfs.c +++ b/sys/compat/linsysfs/linsysfs.c @@ -1,710 +1,741 @@ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * Copyright (c) 2006 IronPort Systems * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include struct scsi_host_queue { TAILQ_ENTRY(scsi_host_queue) scsi_host_next; char *path; char *name; }; TAILQ_HEAD(,scsi_host_queue) scsi_host_q; static int host_number = 0; static int atoi(const char *str) { return (int)strtol(str, (char **)NULL, 10); } static int linsysfs_ifnet_addr(PFS_FILL_ARGS) { + struct epoch_tracker et; struct l_sockaddr lsa; struct ifnet *ifp; - - ifp = ifname_linux_to_bsd(td, pn->pn_parent->pn_name, NULL); - if (ifp == NULL) - return (ENOENT); - if (linux_ifhwaddr(ifp, &lsa) != 0) - return (ENOENT); - sbuf_printf(sb, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx\n", - lsa.sa_data[0], lsa.sa_data[1], lsa.sa_data[2], - lsa.sa_data[3], lsa.sa_data[4], lsa.sa_data[5]); - return (0); + int error; + + CURVNET_SET(TD_TO_VNET(td)); + NET_EPOCH_ENTER(et); + ifp = ifname_linux_to_ifp(td, pn->pn_parent->pn_name); + if (ifp != NULL && (error = linux_ifhwaddr(ifp, &lsa)) == 0) + error = sbuf_printf(sb, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx\n", + lsa.sa_data[0], lsa.sa_data[1], lsa.sa_data[2], + lsa.sa_data[3], lsa.sa_data[4], lsa.sa_data[5]); + else + error = ENOENT; + NET_EPOCH_EXIT(et); + CURVNET_RESTORE(); + return (error == -1 ? ERANGE : error); } static int linsysfs_ifnet_addrlen(PFS_FILL_ARGS) { sbuf_printf(sb, "%d\n", LINUX_IFHWADDRLEN); return (0); } static int linsysfs_ifnet_flags(PFS_FILL_ARGS) { + struct epoch_tracker et; struct ifnet *ifp; - - ifp = ifname_linux_to_bsd(td, pn->pn_parent->pn_name, NULL); - if (ifp == NULL) - return (ENOENT); - sbuf_printf(sb, "0x%x\n", linux_ifflags(ifp)); - return (0); + int error; + + CURVNET_SET(TD_TO_VNET(td)); + NET_EPOCH_ENTER(et); + ifp = ifname_linux_to_ifp(td, pn->pn_parent->pn_name); + if (ifp != NULL) + error = sbuf_printf(sb, "0x%x\n", linux_ifflags(ifp)); + else + error = ENOENT; + NET_EPOCH_EXIT(et); + CURVNET_RESTORE(); + return (error == -1 ? ERANGE : error); } static int linsysfs_ifnet_ifindex(PFS_FILL_ARGS) { + struct epoch_tracker et; struct ifnet *ifp; - - ifp = ifname_linux_to_bsd(td, pn->pn_parent->pn_name, NULL); - if (ifp == NULL) - return (ENOENT); - sbuf_printf(sb, "%u\n", if_getindex(ifp)); - return (0); + int error; + + CURVNET_SET(TD_TO_VNET(td)); + NET_EPOCH_ENTER(et); + ifp = ifname_linux_to_ifp(td, pn->pn_parent->pn_name); + if (ifp != NULL) + error = sbuf_printf(sb, "%u\n", if_getindex(ifp)); + else + error = ENOENT; + NET_EPOCH_EXIT(et); + CURVNET_RESTORE(); + return (error == -1 ? ERANGE : error); } static int linsysfs_ifnet_mtu(PFS_FILL_ARGS) { + struct epoch_tracker et; struct ifnet *ifp; - - ifp = ifname_linux_to_bsd(td, pn->pn_parent->pn_name, NULL); - if (ifp == NULL) - return (ENOENT); - sbuf_printf(sb, "%u\n", if_getmtu(ifp)); - return (0); + int error; + + CURVNET_SET(TD_TO_VNET(td)); + NET_EPOCH_ENTER(et); + ifp = ifname_linux_to_ifp(td, pn->pn_parent->pn_name); + if (ifp != NULL) + error = sbuf_printf(sb, "%u\n", if_getmtu(ifp)); + else + error = ENOENT; + NET_EPOCH_EXIT(et); + CURVNET_RESTORE(); + return (error == -1 ? ERANGE : error); } static int linsysfs_ifnet_tx_queue_len(PFS_FILL_ARGS) { /* XXX */ sbuf_printf(sb, "1000\n"); return (0); } static int linsysfs_ifnet_type(PFS_FILL_ARGS) { + struct epoch_tracker et; struct l_sockaddr lsa; struct ifnet *ifp; - - ifp = ifname_linux_to_bsd(td, pn->pn_parent->pn_name, NULL); - if (ifp == NULL) - return (ENOENT); - if (linux_ifhwaddr(ifp, &lsa) != 0) - return (ENOENT); - sbuf_printf(sb, "%d\n", lsa.sa_family); - return (0); + int error; + + CURVNET_SET(TD_TO_VNET(td)); + NET_EPOCH_ENTER(et); + ifp = ifname_linux_to_ifp(td, pn->pn_parent->pn_name); + if (ifp != NULL && (error = linux_ifhwaddr(ifp, &lsa)) == 0) + error = sbuf_printf(sb, "%d\n", lsa.sa_family); + else + error = ENOENT; + NET_EPOCH_EXIT(et); + CURVNET_RESTORE(); + return (error == -1 ? ERANGE : error); } static void linsysfs_listnics(struct pfs_node *dir) { struct pfs_node *nic; struct pfs_node *lo; nic = pfs_create_dir(dir, "eth0", NULL, NULL, NULL, 0); pfs_create_file(nic, "address", &linsysfs_ifnet_addr, NULL, NULL, NULL, PFS_RD); pfs_create_file(nic, "addr_len", &linsysfs_ifnet_addrlen, NULL, NULL, NULL, PFS_RD); pfs_create_file(nic, "flags", &linsysfs_ifnet_flags, NULL, NULL, NULL, PFS_RD); pfs_create_file(nic, "ifindex", &linsysfs_ifnet_ifindex, NULL, NULL, NULL, PFS_RD); pfs_create_file(nic, "mtu", &linsysfs_ifnet_mtu, NULL, NULL, NULL, PFS_RD); pfs_create_file(nic, "tx_queue_len", &linsysfs_ifnet_tx_queue_len, NULL, NULL, NULL, PFS_RD); pfs_create_file(nic, "type", &linsysfs_ifnet_type, NULL, NULL, NULL, PFS_RD); lo = pfs_create_dir(dir, "lo", NULL, NULL, NULL, 0); pfs_create_file(lo, "address", &linsysfs_ifnet_addr, NULL, NULL, NULL, PFS_RD); pfs_create_file(lo, "addr_len", &linsysfs_ifnet_addrlen, NULL, NULL, NULL, PFS_RD); pfs_create_file(lo, "flags", &linsysfs_ifnet_flags, NULL, NULL, NULL, PFS_RD); pfs_create_file(lo, "ifindex", &linsysfs_ifnet_ifindex, NULL, NULL, NULL, PFS_RD); pfs_create_file(lo, "mtu", &linsysfs_ifnet_mtu, NULL, NULL, NULL, PFS_RD); pfs_create_file(lo, "tx_queue_len", &linsysfs_ifnet_tx_queue_len, NULL, NULL, NULL, PFS_RD); pfs_create_file(lo, "type", &linsysfs_ifnet_type, NULL, NULL, NULL, PFS_RD); } /* * Filler function for proc_name */ static int linsysfs_scsiname(PFS_FILL_ARGS) { struct scsi_host_queue *scsi_host; int index; if (strncmp(pn->pn_parent->pn_name, "host", 4) == 0) { index = atoi(&pn->pn_parent->pn_name[4]); } else { sbuf_printf(sb, "unknown\n"); return (0); } TAILQ_FOREACH(scsi_host, &scsi_host_q, scsi_host_next) { if (index-- == 0) { sbuf_printf(sb, "%s\n", scsi_host->name); return (0); } } sbuf_printf(sb, "unknown\n"); return (0); } /* * Filler function for device sym-link */ static int linsysfs_link_scsi_host(PFS_FILL_ARGS) { struct scsi_host_queue *scsi_host; int index; if (strncmp(pn->pn_parent->pn_name, "host", 4) == 0) { index = atoi(&pn->pn_parent->pn_name[4]); } else { sbuf_printf(sb, "unknown\n"); return (0); } TAILQ_FOREACH(scsi_host, &scsi_host_q, scsi_host_next) { if (index-- == 0) { sbuf_printf(sb, "../../../devices%s", scsi_host->path); return(0); } } sbuf_printf(sb, "unknown\n"); return (0); } static int linsysfs_fill_data(PFS_FILL_ARGS) { sbuf_printf(sb, "%s", (char *)pn->pn_data); return (0); } static int linsysfs_fill_vendor(PFS_FILL_ARGS) { sbuf_printf(sb, "0x%04x\n", pci_get_vendor((device_t)pn->pn_data)); return (0); } static int linsysfs_fill_device(PFS_FILL_ARGS) { sbuf_printf(sb, "0x%04x\n", pci_get_device((device_t)pn->pn_data)); return (0); } static int linsysfs_fill_subvendor(PFS_FILL_ARGS) { sbuf_printf(sb, "0x%04x\n", pci_get_subvendor((device_t)pn->pn_data)); return (0); } static int linsysfs_fill_subdevice(PFS_FILL_ARGS) { sbuf_printf(sb, "0x%04x\n", pci_get_subdevice((device_t)pn->pn_data)); return (0); } static int linsysfs_fill_revid(PFS_FILL_ARGS) { sbuf_printf(sb, "0x%x\n", pci_get_revid((device_t)pn->pn_data)); return (0); } static int linsysfs_fill_config(PFS_FILL_ARGS) { uint8_t config[48]; device_t dev; uint32_t reg; dev = (device_t)pn->pn_data; bzero(config, sizeof(config)); reg = pci_get_vendor(dev); config[0] = reg; config[1] = reg >> 8; reg = pci_get_device(dev); config[2] = reg; config[3] = reg >> 8; reg = pci_get_revid(dev); config[8] = reg; reg = pci_get_subvendor(dev); config[44] = reg; config[45] = reg >> 8; reg = pci_get_subdevice(dev); config[46] = reg; config[47] = reg >> 8; sbuf_bcat(sb, config, sizeof(config)); return (0); } /* * Filler function for PCI uevent file */ static int linsysfs_fill_uevent_pci(PFS_FILL_ARGS) { device_t dev; dev = (device_t)pn->pn_data; sbuf_printf(sb, "DRIVER=%s\nPCI_CLASS=%X\nPCI_ID=%04X:%04X\n" "PCI_SUBSYS_ID=%04X:%04X\nPCI_SLOT_NAME=%04d:%02x:%02x.%x\n", linux_driver_get_name_dev(dev), pci_get_class(dev), pci_get_vendor(dev), pci_get_device(dev), pci_get_subvendor(dev), pci_get_subdevice(dev), pci_get_domain(dev), pci_get_bus(dev), pci_get_slot(dev), pci_get_function(dev)); return (0); } /* * Filler function for drm uevent file */ static int linsysfs_fill_uevent_drm(PFS_FILL_ARGS) { device_t dev; int unit; dev = (device_t)pn->pn_data; unit = device_get_unit(dev); sbuf_printf(sb, "MAJOR=226\nMINOR=%d\nDEVNAME=dri/card%d\nDEVTYPE=dri_minor\n", unit, unit); return (0); } static char * get_full_pfs_path(struct pfs_node *cur) { char *temp, *path; temp = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); path[0] = '\0'; do { snprintf(temp, MAXPATHLEN, "%s/%s", cur->pn_name, path); strlcpy(path, temp, MAXPATHLEN); cur = cur->pn_parent; } while (cur->pn_parent != NULL); path[strlen(path) - 1] = '\0'; /* remove extra slash */ free(temp, M_TEMP); return (path); } /* * Filler function for symlink from drm char device to PCI device */ static int linsysfs_fill_vgapci(PFS_FILL_ARGS) { char *path; path = get_full_pfs_path((struct pfs_node*)pn->pn_data); sbuf_printf(sb, "../../../%s", path); free(path, M_TEMP); return (0); } #undef PCI_DEV #define PCI_DEV "pci" #define DRMN_DEV "drmn" static int linsysfs_run_bus(device_t dev, struct pfs_node *dir, struct pfs_node *scsi, struct pfs_node *chardev, struct pfs_node *drm, char *path, char *prefix) { struct scsi_host_queue *scsi_host; struct pfs_node *sub_dir, *cur_file; int i, nchildren, error; device_t *children, parent; devclass_t devclass; const char *name = NULL; struct pci_devinfo *dinfo; char *device, *host, *new_path, *devname; new_path = path; devname = malloc(16, M_TEMP, M_WAITOK); parent = device_get_parent(dev); if (parent) { devclass = device_get_devclass(parent); if (devclass != NULL) name = devclass_get_name(devclass); if (name && strcmp(name, PCI_DEV) == 0) { dinfo = device_get_ivars(dev); if (dinfo) { device = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); new_path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); new_path[0] = '\000'; strcpy(new_path, path); host = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); device[0] = '\000'; sprintf(device, "%s:%02x:%02x.%x", prefix, dinfo->cfg.bus, dinfo->cfg.slot, dinfo->cfg.func); strcat(new_path, "/"); strcat(new_path, device); dir = pfs_create_dir(dir, device, NULL, NULL, NULL, 0); cur_file = pfs_create_file(dir, "vendor", &linsysfs_fill_vendor, NULL, NULL, NULL, PFS_RD); cur_file->pn_data = (void*)dev; cur_file = pfs_create_file(dir, "device", &linsysfs_fill_device, NULL, NULL, NULL, PFS_RD); cur_file->pn_data = (void*)dev; cur_file = pfs_create_file(dir, "subsystem_vendor", &linsysfs_fill_subvendor, NULL, NULL, NULL, PFS_RD); cur_file->pn_data = (void*)dev; cur_file = pfs_create_file(dir, "subsystem_device", &linsysfs_fill_subdevice, NULL, NULL, NULL, PFS_RD); cur_file->pn_data = (void*)dev; cur_file = pfs_create_file(dir, "revision", &linsysfs_fill_revid, NULL, NULL, NULL, PFS_RD); cur_file->pn_data = (void*)dev; cur_file = pfs_create_file(dir, "config", &linsysfs_fill_config, NULL, NULL, NULL, PFS_RD); cur_file->pn_data = (void*)dev; cur_file = pfs_create_file(dir, "uevent", &linsysfs_fill_uevent_pci, NULL, NULL, NULL, PFS_RD); cur_file->pn_data = (void*)dev; cur_file = pfs_create_link(dir, "subsystem", &linsysfs_fill_data, NULL, NULL, NULL, 0); /* libdrm just checks that the link ends in "/pci" */ cur_file->pn_data = "/sys/bus/pci"; if (dinfo->cfg.baseclass == PCIC_STORAGE) { /* DJA only make this if needed */ sprintf(host, "host%d", host_number++); strcat(new_path, "/"); strcat(new_path, host); pfs_create_dir(dir, host, NULL, NULL, NULL, 0); scsi_host = malloc(sizeof( struct scsi_host_queue), M_DEVBUF, M_NOWAIT); scsi_host->path = malloc( strlen(new_path) + 1, M_DEVBUF, M_NOWAIT); scsi_host->path[0] = '\000'; bcopy(new_path, scsi_host->path, strlen(new_path) + 1); scsi_host->name = "unknown"; sub_dir = pfs_create_dir(scsi, host, NULL, NULL, NULL, 0); pfs_create_link(sub_dir, "device", &linsysfs_link_scsi_host, NULL, NULL, NULL, 0); pfs_create_file(sub_dir, "proc_name", &linsysfs_scsiname, NULL, NULL, NULL, PFS_RD); scsi_host->name = linux_driver_get_name_dev(dev); TAILQ_INSERT_TAIL(&scsi_host_q, scsi_host, scsi_host_next); } free(device, M_TEMP); free(host, M_TEMP); } } devclass = device_get_devclass(dev); if (devclass != NULL) name = devclass_get_name(devclass); else name = NULL; if (name != NULL && strcmp(name, DRMN_DEV) == 0 && device_get_unit(dev) >= 0) { dinfo = device_get_ivars(parent); if (dinfo != NULL && dinfo->cfg.baseclass == PCIC_DISPLAY) { pfs_create_dir(dir, "drm", NULL, NULL, NULL, 0); sprintf(devname, "226:%d", device_get_unit(dev)); sub_dir = pfs_create_dir(chardev, devname, NULL, NULL, NULL, 0); cur_file = pfs_create_link(sub_dir, "device", &linsysfs_fill_vgapci, NULL, NULL, NULL, PFS_RD); cur_file->pn_data = (void*)dir; cur_file = pfs_create_file(sub_dir, "uevent", &linsysfs_fill_uevent_drm, NULL, NULL, NULL, PFS_RD); cur_file->pn_data = (void*)dev; sprintf(devname, "card%d", device_get_unit(dev)); sub_dir = pfs_create_dir(drm, devname, NULL, NULL, NULL, 0); cur_file = pfs_create_link(sub_dir, "device", &linsysfs_fill_vgapci, NULL, NULL, NULL, PFS_RD); cur_file->pn_data = (void*)dir; } } } error = device_get_children(dev, &children, &nchildren); if (error == 0) { for (i = 0; i < nchildren; i++) if (children[i]) linsysfs_run_bus(children[i], dir, scsi, chardev, drm, new_path, prefix); free(children, M_TEMP); } if (new_path != path) free(new_path, M_TEMP); free(devname, M_TEMP); return (1); } /* * Filler function for sys/devices/system/cpu/{online,possible,present} */ static int linsysfs_cpuonline(PFS_FILL_ARGS) { sbuf_printf(sb, "%d-%d\n", CPU_FIRST(), mp_maxid); return (0); } /* * Filler function for sys/devices/system/cpu/cpuX/online */ static int linsysfs_cpuxonline(PFS_FILL_ARGS) { sbuf_printf(sb, "1\n"); return (0); } static void linsysfs_listcpus(struct pfs_node *dir) { struct pfs_node *cpu; char *name; int i, count, len; len = 1; count = mp_maxcpus; while (count > 10) { count /= 10; len++; } len += sizeof("cpu"); name = malloc(len, M_TEMP, M_WAITOK); for (i = 0; i < mp_ncpus; ++i) { /* /sys/devices/system/cpu/cpuX */ sprintf(name, "cpu%d", i); cpu = pfs_create_dir(dir, name, NULL, NULL, NULL, 0); pfs_create_file(cpu, "online", &linsysfs_cpuxonline, NULL, NULL, NULL, PFS_RD); } free(name, M_TEMP); } /* * Constructor */ static int linsysfs_init(PFS_INIT_ARGS) { struct pfs_node *root; struct pfs_node *class; struct pfs_node *dir, *sys, *cpu; struct pfs_node *drm; struct pfs_node *pci; struct pfs_node *scsi; struct pfs_node *net; struct pfs_node *devdir, *chardev; struct pfs_node *kernel; devclass_t devclass; device_t dev; TAILQ_INIT(&scsi_host_q); root = pi->pi_root; /* /sys/bus/... */ dir = pfs_create_dir(root, "bus", NULL, NULL, NULL, 0); /* /sys/class/... */ class = pfs_create_dir(root, "class", NULL, NULL, NULL, 0); scsi = pfs_create_dir(class, "scsi_host", NULL, NULL, NULL, 0); drm = pfs_create_dir(class, "drm", NULL, NULL, NULL, 0); pfs_create_dir(class, "power_supply", NULL, NULL, NULL, 0); /* /sys/class/net/.. */ net = pfs_create_dir(class, "net", NULL, NULL, NULL, 0); /* /sys/dev/... */ devdir = pfs_create_dir(root, "dev", NULL, NULL, NULL, 0); chardev = pfs_create_dir(devdir, "char", NULL, NULL, NULL, 0); /* /sys/devices/... */ dir = pfs_create_dir(root, "devices", NULL, NULL, NULL, 0); pci = pfs_create_dir(dir, "pci0000:00", NULL, NULL, NULL, 0); devclass = devclass_find("root"); if (devclass == NULL) { return (0); } dev = devclass_get_device(devclass, 0); linsysfs_run_bus(dev, pci, scsi, chardev, drm, "/pci0000:00", "0000"); /* /sys/devices/system */ sys = pfs_create_dir(dir, "system", NULL, NULL, NULL, 0); /* /sys/devices/system/cpu */ cpu = pfs_create_dir(sys, "cpu", NULL, NULL, NULL, 0); pfs_create_file(cpu, "online", &linsysfs_cpuonline, NULL, NULL, NULL, PFS_RD); pfs_create_file(cpu, "possible", &linsysfs_cpuonline, NULL, NULL, NULL, PFS_RD); pfs_create_file(cpu, "present", &linsysfs_cpuonline, NULL, NULL, NULL, PFS_RD); linsysfs_listcpus(cpu); linsysfs_listnics(net); /* /sys/kernel */ kernel = pfs_create_dir(root, "kernel", NULL, NULL, NULL, 0); /* /sys/kernel/debug, mountpoint for lindebugfs. */ pfs_create_dir(kernel, "debug", NULL, NULL, NULL, 0); /* /sys/subsystem/... */ dir = pfs_create_dir(root, "subsystem", NULL, NULL, NULL, 0); return (0); } /* * Destructor */ static int linsysfs_uninit(PFS_INIT_ARGS) { struct scsi_host_queue *scsi_host, *scsi_host_tmp; TAILQ_FOREACH_SAFE(scsi_host, &scsi_host_q, scsi_host_next, scsi_host_tmp) { TAILQ_REMOVE(&scsi_host_q, scsi_host, scsi_host_next); free(scsi_host->path, M_TEMP); free(scsi_host, M_TEMP); } return (0); } PSEUDOFS(linsysfs, 1, VFCF_JAIL); #if defined(__aarch64__) || defined(__amd64__) MODULE_DEPEND(linsysfs, linux_common, 1, 1, 1); #else MODULE_DEPEND(linsysfs, linux, 1, 1, 1); #endif diff --git a/sys/compat/linux/linux.c b/sys/compat/linux/linux.c index 135bc10e2dc1..f31a4b5e4f5c 100644 --- a/sys/compat/linux/linux.c +++ b/sys/compat/linux/linux.c @@ -1,864 +1,875 @@ /*- * Copyright (c) 2015 Dmitry Chagin * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include "opt_inet6.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include CTASSERT(LINUX_IFNAMSIZ == IFNAMSIZ); static bool use_real_ifnames = false; SYSCTL_BOOL(_compat_linux, OID_AUTO, use_real_ifnames, CTLFLAG_RWTUN, &use_real_ifnames, 0, "Use FreeBSD interface names instead of generating ethN aliases"); static int bsd_to_linux_sigtbl[LINUX_SIGTBLSZ] = { LINUX_SIGHUP, /* SIGHUP */ LINUX_SIGINT, /* SIGINT */ LINUX_SIGQUIT, /* SIGQUIT */ LINUX_SIGILL, /* SIGILL */ LINUX_SIGTRAP, /* SIGTRAP */ LINUX_SIGABRT, /* SIGABRT */ 0, /* SIGEMT */ LINUX_SIGFPE, /* SIGFPE */ LINUX_SIGKILL, /* SIGKILL */ LINUX_SIGBUS, /* SIGBUS */ LINUX_SIGSEGV, /* SIGSEGV */ LINUX_SIGSYS, /* SIGSYS */ LINUX_SIGPIPE, /* SIGPIPE */ LINUX_SIGALRM, /* SIGALRM */ LINUX_SIGTERM, /* SIGTERM */ LINUX_SIGURG, /* SIGURG */ LINUX_SIGSTOP, /* SIGSTOP */ LINUX_SIGTSTP, /* SIGTSTP */ LINUX_SIGCONT, /* SIGCONT */ LINUX_SIGCHLD, /* SIGCHLD */ LINUX_SIGTTIN, /* SIGTTIN */ LINUX_SIGTTOU, /* SIGTTOU */ LINUX_SIGIO, /* SIGIO */ LINUX_SIGXCPU, /* SIGXCPU */ LINUX_SIGXFSZ, /* SIGXFSZ */ LINUX_SIGVTALRM,/* SIGVTALRM */ LINUX_SIGPROF, /* SIGPROF */ LINUX_SIGWINCH, /* SIGWINCH */ 0, /* SIGINFO */ LINUX_SIGUSR1, /* SIGUSR1 */ LINUX_SIGUSR2 /* SIGUSR2 */ }; #define LINUX_SIGPWREMU (SIGRTMIN + (LINUX_SIGRTMAX - LINUX_SIGRTMIN) + 1) static int linux_to_bsd_sigtbl[LINUX_SIGTBLSZ] = { SIGHUP, /* LINUX_SIGHUP */ SIGINT, /* LINUX_SIGINT */ SIGQUIT, /* LINUX_SIGQUIT */ SIGILL, /* LINUX_SIGILL */ SIGTRAP, /* LINUX_SIGTRAP */ SIGABRT, /* LINUX_SIGABRT */ SIGBUS, /* LINUX_SIGBUS */ SIGFPE, /* LINUX_SIGFPE */ SIGKILL, /* LINUX_SIGKILL */ SIGUSR1, /* LINUX_SIGUSR1 */ SIGSEGV, /* LINUX_SIGSEGV */ SIGUSR2, /* LINUX_SIGUSR2 */ SIGPIPE, /* LINUX_SIGPIPE */ SIGALRM, /* LINUX_SIGALRM */ SIGTERM, /* LINUX_SIGTERM */ SIGBUS, /* LINUX_SIGSTKFLT */ SIGCHLD, /* LINUX_SIGCHLD */ SIGCONT, /* LINUX_SIGCONT */ SIGSTOP, /* LINUX_SIGSTOP */ SIGTSTP, /* LINUX_SIGTSTP */ SIGTTIN, /* LINUX_SIGTTIN */ SIGTTOU, /* LINUX_SIGTTOU */ SIGURG, /* LINUX_SIGURG */ SIGXCPU, /* LINUX_SIGXCPU */ SIGXFSZ, /* LINUX_SIGXFSZ */ SIGVTALRM, /* LINUX_SIGVTALARM */ SIGPROF, /* LINUX_SIGPROF */ SIGWINCH, /* LINUX_SIGWINCH */ SIGIO, /* LINUX_SIGIO */ /* * FreeBSD does not have SIGPWR signal, map Linux SIGPWR signal * to the first unused FreeBSD signal number. Since Linux supports * signals from 1 to 64 we are ok here as our SIGRTMIN = 65. */ LINUX_SIGPWREMU,/* LINUX_SIGPWR */ SIGSYS /* LINUX_SIGSYS */ }; static struct cdev *dev_shm_cdev; static struct cdevsw dev_shm_cdevsw = { .d_version = D_VERSION, .d_name = "dev_shm", }; /* * Map Linux RT signals to the FreeBSD RT signals. */ static inline int linux_to_bsd_rt_signal(int sig) { return (SIGRTMIN + sig - LINUX_SIGRTMIN); } static inline int bsd_to_linux_rt_signal(int sig) { return (sig - SIGRTMIN + LINUX_SIGRTMIN); } int linux_to_bsd_signal(int sig) { KASSERT(sig > 0 && sig <= LINUX_SIGRTMAX, ("invalid Linux signal %d\n", sig)); if (sig < LINUX_SIGRTMIN) return (linux_to_bsd_sigtbl[_SIG_IDX(sig)]); return (linux_to_bsd_rt_signal(sig)); } int bsd_to_linux_signal(int sig) { if (sig <= LINUX_SIGTBLSZ) return (bsd_to_linux_sigtbl[_SIG_IDX(sig)]); if (sig == LINUX_SIGPWREMU) return (LINUX_SIGPWR); return (bsd_to_linux_rt_signal(sig)); } int linux_to_bsd_sigaltstack(int lsa) { int bsa = 0; if (lsa & LINUX_SS_DISABLE) bsa |= SS_DISABLE; /* * Linux ignores SS_ONSTACK flag for ss * parameter while FreeBSD prohibits it. */ return (bsa); } int bsd_to_linux_sigaltstack(int bsa) { int lsa = 0; if (bsa & SS_DISABLE) lsa |= LINUX_SS_DISABLE; if (bsa & SS_ONSTACK) lsa |= LINUX_SS_ONSTACK; return (lsa); } void linux_to_bsd_sigset(l_sigset_t *lss, sigset_t *bss) { int b, l; SIGEMPTYSET(*bss); for (l = 1; l <= LINUX_SIGRTMAX; l++) { if (LINUX_SIGISMEMBER(*lss, l)) { b = linux_to_bsd_signal(l); if (b) SIGADDSET(*bss, b); } } } void bsd_to_linux_sigset(sigset_t *bss, l_sigset_t *lss) { int b, l; LINUX_SIGEMPTYSET(*lss); for (b = 1; b <= SIGRTMAX; b++) { if (SIGISMEMBER(*bss, b)) { l = bsd_to_linux_signal(b); if (l) LINUX_SIGADDSET(*lss, l); } } } /* * Translate a FreeBSD interface name to a Linux interface name * by interface name, and return the number of bytes copied to lxname. */ int ifname_bsd_to_linux_name(const char *bsdname, char *lxname, size_t len) { struct epoch_tracker et; struct ifnet *ifp; int ret; ret = 0; CURVNET_SET(TD_TO_VNET(curthread)); NET_EPOCH_ENTER(et); ifp = ifunit(bsdname); if (ifp != NULL) ret = ifname_bsd_to_linux_ifp(ifp, lxname, len); NET_EPOCH_EXIT(et); CURVNET_RESTORE(); return (ret); } /* * Translate a FreeBSD interface name to a Linux interface name * by interface index, and return the number of bytes copied to lxname. */ int ifname_bsd_to_linux_idx(u_int idx, char *lxname, size_t len) { struct epoch_tracker et; struct ifnet *ifp; int ret; ret = 0; CURVNET_SET(TD_TO_VNET(curthread)); NET_EPOCH_ENTER(et); ifp = ifnet_byindex(idx); if (ifp != NULL) ret = ifname_bsd_to_linux_ifp(ifp, lxname, len); NET_EPOCH_EXIT(et); CURVNET_RESTORE(); return (ret); } /* * Translate a FreeBSD interface name to a Linux interface name, * and return the number of bytes copied to lxname, 0 if interface * not found, -1 on error. */ struct ifname_bsd_to_linux_ifp_cb_s { struct ifnet *ifp; int ethno; char *lxname; size_t len; }; static int ifname_bsd_to_linux_ifp_cb(if_t ifp, void *arg) { struct ifname_bsd_to_linux_ifp_cb_s *cbs = arg; if (ifp == cbs->ifp) return (snprintf(cbs->lxname, cbs->len, "eth%d", cbs->ethno)); if (IFP_IS_ETH(ifp)) cbs->ethno++; return (0); } int ifname_bsd_to_linux_ifp(struct ifnet *ifp, char *lxname, size_t len) { struct ifname_bsd_to_linux_ifp_cb_s arg = { .ifp = ifp, .ethno = 0, .lxname = lxname, .len = len, }; NET_EPOCH_ASSERT(); /* * Linux loopback interface name is lo (not lo0), * we translate lo to lo0, loX to loX. */ if (IFP_IS_LOOP(ifp) && strncmp(ifp->if_xname, "lo0", IFNAMSIZ) == 0) return (strlcpy(lxname, "lo", len)); /* Short-circuit non ethernet interfaces. */ if (!IFP_IS_ETH(ifp) || linux_use_real_ifname(ifp)) return (strlcpy(lxname, if_name(ifp), len)); /* Determine the (relative) unit number for ethernet interfaces. */ return (if_foreach(ifname_bsd_to_linux_ifp_cb, &arg)); } /* * Translate a Linux interface name to a FreeBSD interface name, * and return the associated ifnet structure * bsdname and lxname need to be least IFNAMSIZ bytes long, but * can point to the same buffer. */ -struct ifname_linux_to_bsd_cb_s { +struct ifname_linux_to_ifp_cb_s { bool is_lo; bool is_eth; int ethno; int unit; const char *lxname; if_t ifp; }; static int -ifname_linux_to_bsd_cb(if_t ifp, void *arg) +ifname_linux_to_ifp_cb(if_t ifp, void *arg) { - struct ifname_linux_to_bsd_cb_s *cbs = arg; + struct ifname_linux_to_ifp_cb_s *cbs = arg; NET_EPOCH_ASSERT(); /* * Allow Linux programs to use FreeBSD names. Don't presume * we never have an interface named "eth", so don't make * the test optional based on is_eth. */ if (strncmp(if_name(ifp), cbs->lxname, LINUX_IFNAMSIZ) == 0) goto out; if (cbs->is_eth && IFP_IS_ETH(ifp) && cbs->unit == cbs->ethno) goto out; if (cbs->is_lo && IFP_IS_LOOP(ifp)) goto out; if (IFP_IS_ETH(ifp)) cbs->ethno++; return (0); out: cbs->ifp = ifp; return (1); } struct ifnet * -ifname_linux_to_bsd(struct thread *td, const char *lxname, char *bsdname) +ifname_linux_to_ifp(struct thread *td, const char *lxname) { - struct ifname_linux_to_bsd_cb_s arg = { + struct ifname_linux_to_ifp_cb_s arg = { .ethno = 0, .lxname = lxname, .ifp = NULL, }; - struct epoch_tracker et; - int len, ret; + int len; char *ep; + NET_EPOCH_ASSERT(); + for (len = 0; len < LINUX_IFNAMSIZ; ++len) if (!isalpha(lxname[len]) || lxname[len] == '\0') break; if (len == 0 || len == LINUX_IFNAMSIZ) return (NULL); /* * Linux loopback interface name is lo (not lo0), * we translate lo to lo0, loX to loX. */ arg.is_lo = (len == 2 && strncmp(lxname, "lo", LINUX_IFNAMSIZ) == 0); arg.unit = (int)strtoul(lxname + len, &ep, 10); if ((ep == NULL || ep == lxname + len || ep >= lxname + LINUX_IFNAMSIZ) && arg.is_lo == 0) return (NULL); arg.is_eth = (len == 3 && strncmp(lxname, "eth", len) == 0); + if_foreach(ifname_linux_to_ifp_cb, &arg); + return (arg.ifp); +} + +struct ifnet * +ifname_linux_to_bsd(struct thread *td, const char *lxname, char *bsdname) +{ + struct epoch_tracker et; + struct ifnet *ifp; + CURVNET_SET(TD_TO_VNET(td)); NET_EPOCH_ENTER(et); - ret = if_foreach(ifname_linux_to_bsd_cb, &arg); + ifp = ifname_linux_to_ifp(td, lxname); + if (ifp != NULL && bsdname != NULL) + strlcpy(bsdname, if_name(ifp), IFNAMSIZ); NET_EPOCH_EXIT(et); CURVNET_RESTORE(); - if (ret > 0 && arg.ifp != NULL && bsdname != NULL) - strlcpy(bsdname, if_name(arg.ifp), IFNAMSIZ); - return (arg.ifp); + return (ifp); } unsigned short linux_ifflags(struct ifnet *ifp) { unsigned short fl, flags; fl = (if_getflags(ifp) | if_getdrvflags(ifp)) & 0xffff; flags = 0; if (fl & IFF_UP) flags |= LINUX_IFF_UP; if (fl & IFF_BROADCAST) flags |= LINUX_IFF_BROADCAST; if (fl & IFF_DEBUG) flags |= LINUX_IFF_DEBUG; if (fl & IFF_LOOPBACK) flags |= LINUX_IFF_LOOPBACK; if (fl & IFF_POINTOPOINT) flags |= LINUX_IFF_POINTOPOINT; if (fl & IFF_DRV_RUNNING) flags |= LINUX_IFF_RUNNING; if (fl & IFF_NOARP) flags |= LINUX_IFF_NOARP; if (fl & IFF_PROMISC) flags |= LINUX_IFF_PROMISC; if (fl & IFF_ALLMULTI) flags |= LINUX_IFF_ALLMULTI; if (fl & IFF_MULTICAST) flags |= LINUX_IFF_MULTICAST; return (flags); } static u_int linux_ifhwaddr_cb(void *arg, struct ifaddr *ifa, u_int count) { struct sockaddr_dl *sdl = (struct sockaddr_dl *)ifa->ifa_addr; struct l_sockaddr *lsa = arg; if (count > 0) return (0); if (sdl->sdl_type != IFT_ETHER) return (0); bzero(lsa, sizeof(*lsa)); lsa->sa_family = LINUX_ARPHRD_ETHER; bcopy(LLADDR(sdl), lsa->sa_data, LINUX_IFHWADDRLEN); return (1); } int linux_ifhwaddr(struct ifnet *ifp, struct l_sockaddr *lsa) { if (IFP_IS_LOOP(ifp)) { bzero(lsa, sizeof(*lsa)); lsa->sa_family = LINUX_ARPHRD_LOOPBACK; return (0); } if (!IFP_IS_ETH(ifp)) return (ENOENT); if (if_foreach_addr_type(ifp, AF_LINK, linux_ifhwaddr_cb, lsa) > 0) return (0); return (ENOENT); } int linux_to_bsd_domain(int domain) { switch (domain) { case LINUX_AF_UNSPEC: return (AF_UNSPEC); case LINUX_AF_UNIX: return (AF_LOCAL); case LINUX_AF_INET: return (AF_INET); case LINUX_AF_INET6: return (AF_INET6); case LINUX_AF_AX25: return (AF_CCITT); case LINUX_AF_IPX: return (AF_IPX); case LINUX_AF_APPLETALK: return (AF_APPLETALK); case LINUX_AF_NETLINK: return (AF_NETLINK); } return (-1); } int bsd_to_linux_domain(int domain) { switch (domain) { case AF_UNSPEC: return (LINUX_AF_UNSPEC); case AF_LOCAL: return (LINUX_AF_UNIX); case AF_INET: return (LINUX_AF_INET); case AF_INET6: return (LINUX_AF_INET6); case AF_CCITT: return (LINUX_AF_AX25); case AF_IPX: return (LINUX_AF_IPX); case AF_APPLETALK: return (LINUX_AF_APPLETALK); case AF_NETLINK: return (LINUX_AF_NETLINK); } return (-1); } /* * Based on the fact that: * 1. Native and Linux storage of struct sockaddr * and struct sockaddr_in6 are equal. * 2. On Linux sa_family is the first member of all struct sockaddr. */ int bsd_to_linux_sockaddr(const struct sockaddr *sa, struct l_sockaddr **lsa, socklen_t len) { struct l_sockaddr *kosa; int bdom; *lsa = NULL; if (len < 2 || len > UCHAR_MAX) return (EINVAL); bdom = bsd_to_linux_domain(sa->sa_family); if (bdom == -1) return (EAFNOSUPPORT); kosa = malloc(len, M_LINUX, M_WAITOK); bcopy(sa, kosa, len); kosa->sa_family = bdom; *lsa = kosa; return (0); } int linux_to_bsd_sockaddr(const struct l_sockaddr *osa, struct sockaddr **sap, socklen_t *len) { struct sockaddr *sa; struct l_sockaddr *kosa; #ifdef INET6 struct sockaddr_in6 *sin6; bool oldv6size; #endif char *name; int salen, bdom, error, hdrlen, namelen; if (*len < 2 || *len > UCHAR_MAX) return (EINVAL); salen = *len; #ifdef INET6 oldv6size = false; /* * Check for old (pre-RFC2553) sockaddr_in6. We may accept it * if it's a v4-mapped address, so reserve the proper space * for it. */ if (salen == sizeof(struct sockaddr_in6) - sizeof(uint32_t)) { salen += sizeof(uint32_t); oldv6size = true; } #endif kosa = malloc(salen, M_SONAME, M_WAITOK); if ((error = copyin(osa, kosa, *len))) goto out; bdom = linux_to_bsd_domain(kosa->sa_family); if (bdom == -1) { error = EAFNOSUPPORT; goto out; } #ifdef INET6 /* * Older Linux IPv6 code uses obsolete RFC2133 struct sockaddr_in6, * which lacks the scope id compared with RFC2553 one. If we detect * the situation, reject the address and write a message to system log. * * Still accept addresses for which the scope id is not used. */ if (oldv6size) { if (bdom == AF_INET6) { sin6 = (struct sockaddr_in6 *)kosa; if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr) || (!IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) && !IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) && !IN6_IS_ADDR_V4COMPAT(&sin6->sin6_addr) && !IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) && !IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))) { sin6->sin6_scope_id = 0; } else { linux_msg(curthread, "obsolete pre-RFC2553 sockaddr_in6 rejected"); error = EINVAL; goto out; } } else salen -= sizeof(uint32_t); } #endif if (bdom == AF_INET) { if (salen < sizeof(struct sockaddr_in)) { error = EINVAL; goto out; } salen = sizeof(struct sockaddr_in); } if (bdom == AF_LOCAL && salen > sizeof(struct sockaddr_un)) { hdrlen = offsetof(struct sockaddr_un, sun_path); name = ((struct sockaddr_un *)kosa)->sun_path; if (*name == '\0') { /* * Linux abstract namespace starts with a NULL byte. * XXX We do not support abstract namespace yet. */ namelen = strnlen(name + 1, salen - hdrlen - 1) + 1; } else namelen = strnlen(name, salen - hdrlen); salen = hdrlen + namelen; if (salen > sizeof(struct sockaddr_un)) { error = ENAMETOOLONG; goto out; } } if (bdom == AF_NETLINK) { if (salen < sizeof(struct sockaddr_nl)) { error = EINVAL; goto out; } salen = sizeof(struct sockaddr_nl); } sa = (struct sockaddr *)kosa; sa->sa_family = bdom; sa->sa_len = salen; *sap = sa; *len = salen; return (0); out: free(kosa, M_SONAME); return (error); } void linux_dev_shm_create(void) { int error; error = make_dev_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK, &dev_shm_cdev, &dev_shm_cdevsw, NULL, UID_ROOT, GID_WHEEL, 0, "shm/.mountpoint"); if (error != 0) { printf("%s: failed to create device node, error %d\n", __func__, error); } } void linux_dev_shm_destroy(void) { destroy_dev(dev_shm_cdev); } int bsd_to_linux_bits_(int value, struct bsd_to_linux_bitmap *bitmap, size_t mapcnt, int no_value) { int bsd_mask, bsd_value, linux_mask, linux_value; int linux_ret; size_t i; bool applied; applied = false; linux_ret = 0; for (i = 0; i < mapcnt; ++i) { bsd_mask = bitmap[i].bsd_mask; bsd_value = bitmap[i].bsd_value; if (bsd_mask == 0) bsd_mask = bsd_value; linux_mask = bitmap[i].linux_mask; linux_value = bitmap[i].linux_value; if (linux_mask == 0) linux_mask = linux_value; /* * If a mask larger than just the value is set, we explicitly * want to make sure that only this bit we mapped within that * mask is set. */ if ((value & bsd_mask) == bsd_value) { linux_ret = (linux_ret & ~linux_mask) | linux_value; applied = true; } } if (!applied) return (no_value); return (linux_ret); } int linux_to_bsd_bits_(int value, struct bsd_to_linux_bitmap *bitmap, size_t mapcnt, int no_value) { int bsd_mask, bsd_value, linux_mask, linux_value; int bsd_ret; size_t i; bool applied; applied = false; bsd_ret = 0; for (i = 0; i < mapcnt; ++i) { bsd_mask = bitmap[i].bsd_mask; bsd_value = bitmap[i].bsd_value; if (bsd_mask == 0) bsd_mask = bsd_value; linux_mask = bitmap[i].linux_mask; linux_value = bitmap[i].linux_value; if (linux_mask == 0) linux_mask = linux_value; /* * If a mask larger than just the value is set, we explicitly * want to make sure that only this bit we mapped within that * mask is set. */ if ((value & linux_mask) == linux_value) { bsd_ret = (bsd_ret & ~bsd_mask) | bsd_value; applied = true; } } if (!applied) return (no_value); return (bsd_ret); } void linux_to_bsd_poll_events(struct thread *td, int fd, short lev, short *bev) { struct file *fp; int error; short bits = 0; if (lev & LINUX_POLLIN) bits |= POLLIN; if (lev & LINUX_POLLPRI) bits |= POLLPRI; if (lev & LINUX_POLLOUT) bits |= POLLOUT; if (lev & LINUX_POLLERR) bits |= POLLERR; if (lev & LINUX_POLLHUP) bits |= POLLHUP; if (lev & LINUX_POLLNVAL) bits |= POLLNVAL; if (lev & LINUX_POLLRDNORM) bits |= POLLRDNORM; if (lev & LINUX_POLLRDBAND) bits |= POLLRDBAND; if (lev & LINUX_POLLWRBAND) bits |= POLLWRBAND; if (lev & LINUX_POLLWRNORM) bits |= POLLWRNORM; if (lev & LINUX_POLLRDHUP) { /* * It seems that the Linux silencly ignores POLLRDHUP * on non-socket file descriptors unlike FreeBSD, where * events bits is more strictly checked (POLLSTANDARD). */ error = fget_unlocked(td, fd, &cap_no_rights, &fp); if (error == 0) { /* * XXX. On FreeBSD POLLRDHUP applies only to * stream sockets. */ if (fp->f_type == DTYPE_SOCKET) bits |= POLLRDHUP; fdrop(fp, td); } } if (lev & LINUX_POLLMSG) LINUX_RATELIMIT_MSG_OPT1("unsupported POLLMSG, events(%d)", lev); if (lev & LINUX_POLLREMOVE) LINUX_RATELIMIT_MSG_OPT1("unsupported POLLREMOVE, events(%d)", lev); *bev = bits; } void bsd_to_linux_poll_events(short bev, short *lev) { short bits = 0; if (bev & POLLIN) bits |= LINUX_POLLIN; if (bev & POLLPRI) bits |= LINUX_POLLPRI; if (bev & (POLLOUT | POLLWRNORM)) /* * POLLWRNORM is equal to POLLOUT on FreeBSD, * but not on Linux */ bits |= LINUX_POLLOUT; if (bev & POLLERR) bits |= LINUX_POLLERR; if (bev & POLLHUP) bits |= LINUX_POLLHUP; if (bev & POLLNVAL) bits |= LINUX_POLLNVAL; if (bev & POLLRDNORM) bits |= LINUX_POLLRDNORM; if (bev & POLLRDBAND) bits |= LINUX_POLLRDBAND; if (bev & POLLWRBAND) bits |= LINUX_POLLWRBAND; if (bev & POLLRDHUP) bits |= LINUX_POLLRDHUP; *lev = bits; } bool linux_use_real_ifname(const struct ifnet *ifp) { return (use_real_ifnames); } diff --git a/sys/compat/linux/linux_common.h b/sys/compat/linux/linux_common.h index c25c4abf18ae..4b693ccaf868 100644 --- a/sys/compat/linux/linux_common.h +++ b/sys/compat/linux/linux_common.h @@ -1,52 +1,53 @@ /*- * SPDX-License-Identifier: BSD-2-Clause * * Copyright (c) 2019 Dmitry Chagin * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ #ifndef _LINUX_COMMON_H_ #define _LINUX_COMMON_H_ int ifname_bsd_to_linux_ifp(struct ifnet *, char *, size_t); int ifname_bsd_to_linux_idx(u_int, char *, size_t); int ifname_bsd_to_linux_name(const char *, char *, size_t); +struct ifnet *ifname_linux_to_ifp(struct thread *, const char *); struct ifnet *ifname_linux_to_bsd(struct thread *td, const char *lxname, char *bsdname); unsigned short linux_ifflags(struct ifnet *); int linux_ifhwaddr(struct ifnet *ifp, struct l_sockaddr *lsa); int linux_to_bsd_domain(int domain); int bsd_to_linux_domain(int domain); int bsd_to_linux_sockaddr(const struct sockaddr *sa, struct l_sockaddr **lsa, socklen_t len); int linux_to_bsd_sockaddr(const struct l_sockaddr *lsa, struct sockaddr **sap, socklen_t *len); void linux_to_bsd_poll_events(struct thread *td, int fd, short lev, short *bev); void bsd_to_linux_poll_events(short bev, short *lev); #endif /* _LINUX_COMMON_H_ */