#include <linux/capability.h>
#include <linux/kernel.h>
#include <linux/netdevice.h>
#include <linux/if_arp.h>
#include <linux/sched/signal.h>
#include <linux/nsproxy.h>
#include <net/net_namespace.h>
#include <linux/rtnetlink.h>
#include <linux/vmalloc.h>
#include <linux/export.h>
#include <linux/jiffies.h>
#include <linux/pm_runtime.h>
#include <linux/of_net.h>
static const char fmt_hex[] = "%#x\n";
static const char fmt_dec[] = "%d\n";
static const char fmt_ulong[] = "%lu\n";
static const char fmt_u64[] = "%llu\n";
static inline int dev_isalive(const struct net_device *dev)
return dev->reg_state <= NETREG_REGISTERED;
static ssize_t netdev_show(const struct device *dev,
struct device_attribute *attr, char *buf,
ssize_t (*format)(const struct net_device *, char *))
struct net_device *ndev = to_net_dev(dev);
read_lock(&dev_base_lock);
ret = (*format)(ndev, buf);
read_unlock(&dev_base_lock);
#define NETDEVICE_SHOW(field, format_string) \
static ssize_t format_##field(const struct net_device *dev, char *buf) \
return sprintf(buf, format_string, dev->field); \
static ssize_t field##_show(struct device *dev, \
struct device_attribute *attr, char *buf) \
return netdev_show(dev, attr, buf, format_##field); \
#define NETDEVICE_SHOW_RO(field, format_string) \
NETDEVICE_SHOW(field, format_string); \
static DEVICE_ATTR_RO(field)
#define NETDEVICE_SHOW_RW(field, format_string) \
NETDEVICE_SHOW(field, format_string); \
static DEVICE_ATTR_RW(field)
static ssize_t netdev_store(struct device *dev, struct device_attribute *attr,
const char *buf, size_t len,
int (*set)(struct net_device *, unsigned long))
struct net_device *netdev = to_net_dev(dev);
struct net *net = dev_net(netdev);
if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
ret = kstrtoul(buf, 0, &new);