Commit 7a56493f authored by David S. Miller's avatar David S. Miller

Merge branch 'netdev-altnames'

Jiri Pirko says:

====================
net: introduce alternative names for network interfaces

In the past, there was repeatedly discussed the IFNAMSIZ (16) limit for
netdevice name length. Now when we have PF and VF representors
with port names like "pfXvfY", it became quite common to hit this limit:
0123456789012345
enp131s0f1npf0vf6
enp131s0f1npf0vf22

Udev cannot rename these interfaces out-of-the-box and user needs to
create custom rules to handle them.

Also, udev has multiple schemes of netdev names. From udev code:
 * Type of names:
 *   b<number>                             - BCMA bus core number
 *   c<bus_id>                             - bus id of a grouped CCW or CCW device,
 *                                           with all leading zeros stripped [s390]
 *   o<index>[n<phys_port_name>|d<dev_port>]
 *                                         - on-board device index number
 *   s<slot>[f<function>][n<phys_port_name>|d<dev_port>]
 *                                         - hotplug slot index number
 *   x<MAC>                                - MAC address
 *   [P<domain>]p<bus>s<slot>[f<function>][n<phys_port_name>|d<dev_port>]
 *                                         - PCI geographical location
 *   [P<domain>]p<bus>s<slot>[f<function>][u<port>][..][c<config>][i<interface>]
 *                                         - USB port number chain
 *   v<slot>                               - VIO slot number (IBM PowerVM)
 *   a<vendor><model>i<instance>           - Platform bus ACPI instance id
 *   i<addr>n<phys_port_name>              - Netdevsim bus address and port name

One device can be often renamed by multiple patterns at the
same time (e.g. pci address/mac).

This patchset introduces alternative names for network interfaces.
Main goal is to:
1) Overcome the IFNAMSIZ limitation (altname limitation is 128 bytes)
2) Allow to have multiple names at the same time (multiple udev patterns)
3) Allow to use alternative names as handle for commands

The patchset introduces two new commands to add/delete list of properties.
Currently only alternative names are implemented but the ifrastructure
could be easily extended later on. This is very similar to the list of vlan
and tunnels being added/deleted to/from bridge ports.

See following examples.

$ ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: dummy0: <BROADCAST,NOARP> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
    link/ether ae:67:a9:67:46:86 brd ff:ff:ff:ff:ff:ff

-> Add alternative names for dummy0:

$ ip link prop add dummy0 altname someothername
$ ip link prop add dummy0 altname someotherveryveryveryverylongname
$ ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: dummy0: <BROADCAST,NOARP> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
    link/ether ae:67:a9:67:46:86 brd ff:ff:ff:ff:ff:ff
    altname someothername
    altname someotherveryveryveryverylongname
$ ip link show someotherveryveryveryverylongname
2: dummy0: <BROADCAST,NOARP> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
    link/ether ae:67:a9:67:46:86 brd ff:ff:ff:ff:ff:ff
    altname someothername
    altname someotherveryveryveryverylongname

-> Add bridge brx, add it's alternative name and use alternative names to
   do enslavement.

$ ip link add name brx type bridge
$ ip link prop add brx altname mypersonalsuperspecialbridge
$ ip link set someotherveryveryveryverylongname master mypersonalsuperspecialbridge
$ ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: dummy0: <BROADCAST,NOARP> mtu 1500 qdisc noop master brx state DOWN mode DEFAULT group default qlen 1000
    link/ether ae:67:a9:67:46:86 brd ff:ff:ff:ff:ff:ff
    altname someothername
    altname someotherveryveryveryverylongname
3: brx: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
    link/ether ae:67:a9:67:46:86 brd ff:ff:ff:ff:ff:ff
    altname mypersonalsuperspecialbridge

-> Add ipv4 address to the bridge using alternative name:

$ ip addr add 192.168.0.1/24 dev mypersonalsuperspecialbridge
$ ip addr show mypersonalsuperspecialbridge
3: brx: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether ae:67:a9:67:46:86 brd ff:ff:ff:ff:ff:ff
    altname mypersonalsuperspecialbridge
    inet 192.168.0.1/24 scope global brx
       valid_lft forever preferred_lft forever

-> Delete one of dummy0 alternative names:

$ ip link prop del dummy0 altname someotherveryveryveryverylongname
$ ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: dummy0: <BROADCAST,NOARP> mtu 1500 qdisc noop master brx state DOWN mode DEFAULT group default qlen 1000
    link/ether ae:67:a9:67:46:86 brd ff:ff:ff:ff:ff:ff
    altname someothername
3: brx: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
    link/ether ae:67:a9:67:46:86 brd ff:ff:ff:ff:ff:ff
    altname mypersonalsuperspecialbridge

-> Add multiple alternative names at once

$ ip link prop add dummy0 altname a altname b altname c altname d
$ ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: dummy0: <BROADCAST,NOARP> mtu 1500 qdisc noop master brx state DOWN mode DEFAULT group default qlen 1000
    link/ether ae:67:a9:67:46:86 brd ff:ff:ff:ff:ff:ff
    altname someothername
    altname a
    altname b
    altname c
    altname d
3: brx: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
    link/ether ae:67:a9:67:46:86 brd ff:ff:ff:ff:ff:ff
    altname mypersonalsuperspecialbridge
====================
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parents be2644aa 76c9ac0e
...@@ -925,6 +925,16 @@ struct dev_ifalias { ...@@ -925,6 +925,16 @@ struct dev_ifalias {
struct devlink; struct devlink;
struct tlsdev_ops; struct tlsdev_ops;
struct netdev_name_node {
struct hlist_node hlist;
struct list_head list;
struct net_device *dev;
const char *name;
};
int netdev_name_node_alt_create(struct net_device *dev, const char *name);
int netdev_name_node_alt_destroy(struct net_device *dev, const char *name);
/* /*
* This structure defines the management hooks for network devices. * This structure defines the management hooks for network devices.
* The following hooks can be defined; unless noted otherwise, they are * The following hooks can be defined; unless noted otherwise, they are
...@@ -1564,7 +1574,7 @@ enum netdev_priv_flags { ...@@ -1564,7 +1574,7 @@ enum netdev_priv_flags {
* (i.e. as seen by users in the "Space.c" file). It is the name * (i.e. as seen by users in the "Space.c" file). It is the name
* of the interface. * of the interface.
* *
* @name_hlist: Device name hash chain, please keep it close to name[] * @name_node: Name hashlist node
* @ifalias: SNMP alias * @ifalias: SNMP alias
* @mem_end: Shared memory end * @mem_end: Shared memory end
* @mem_start: Shared memory start * @mem_start: Shared memory start
...@@ -1774,7 +1784,7 @@ enum netdev_priv_flags { ...@@ -1774,7 +1784,7 @@ enum netdev_priv_flags {
struct net_device { struct net_device {
char name[IFNAMSIZ]; char name[IFNAMSIZ];
struct hlist_node name_hlist; struct netdev_name_node *name_node;
struct dev_ifalias __rcu *ifalias; struct dev_ifalias __rcu *ifalias;
/* /*
* I/O specific fields * I/O specific fields
......
...@@ -33,6 +33,7 @@ ...@@ -33,6 +33,7 @@
#define IFNAMSIZ 16 #define IFNAMSIZ 16
#endif /* __UAPI_DEF_IF_IFNAMSIZ */ #endif /* __UAPI_DEF_IF_IFNAMSIZ */
#define IFALIASZ 256 #define IFALIASZ 256
#define ALTIFNAMSIZ 128
#include <linux/hdlc/ioctl.h> #include <linux/hdlc/ioctl.h>
/* For glibc compatibility. An empty enum does not compile. */ /* For glibc compatibility. An empty enum does not compile. */
......
...@@ -167,6 +167,8 @@ enum { ...@@ -167,6 +167,8 @@ enum {
IFLA_NEW_IFINDEX, IFLA_NEW_IFINDEX,
IFLA_MIN_MTU, IFLA_MIN_MTU,
IFLA_MAX_MTU, IFLA_MAX_MTU,
IFLA_PROP_LIST,
IFLA_ALT_IFNAME, /* Alternative ifname */
__IFLA_MAX __IFLA_MAX
}; };
......
...@@ -164,6 +164,13 @@ enum { ...@@ -164,6 +164,13 @@ enum {
RTM_GETNEXTHOP, RTM_GETNEXTHOP,
#define RTM_GETNEXTHOP RTM_GETNEXTHOP #define RTM_GETNEXTHOP RTM_GETNEXTHOP
RTM_NEWLINKPROP = 108,
#define RTM_NEWLINKPROP RTM_NEWLINKPROP
RTM_DELLINKPROP,
#define RTM_DELLINKPROP RTM_DELLINKPROP
RTM_GETLINKPROP,
#define RTM_GETLINKPROP RTM_GETLINKPROP
__RTM_MAX, __RTM_MAX,
#define RTM_MAX (((__RTM_MAX + 3) & ~3) - 1) #define RTM_MAX (((__RTM_MAX + 3) & ~3) - 1)
}; };
......
...@@ -228,6 +228,122 @@ static inline void rps_unlock(struct softnet_data *sd) ...@@ -228,6 +228,122 @@ static inline void rps_unlock(struct softnet_data *sd)
#endif #endif
} }
static struct netdev_name_node *netdev_name_node_alloc(struct net_device *dev,
const char *name)
{
struct netdev_name_node *name_node;
name_node = kmalloc(sizeof(*name_node), GFP_KERNEL);
if (!name_node)
return NULL;
INIT_HLIST_NODE(&name_node->hlist);
name_node->dev = dev;
name_node->name = name;
return name_node;
}
static struct netdev_name_node *
netdev_name_node_head_alloc(struct net_device *dev)
{
struct netdev_name_node *name_node;
name_node = netdev_name_node_alloc(dev, dev->name);
if (!name_node)
return NULL;
INIT_LIST_HEAD(&name_node->list);
return name_node;
}
static void netdev_name_node_free(struct netdev_name_node *name_node)
{
kfree(name_node);
}
static void netdev_name_node_add(struct net *net,
struct netdev_name_node *name_node)
{
hlist_add_head_rcu(&name_node->hlist,
dev_name_hash(net, name_node->name));
}
static void netdev_name_node_del(struct netdev_name_node *name_node)
{
hlist_del_rcu(&name_node->hlist);
}
static struct netdev_name_node *netdev_name_node_lookup(struct net *net,
const char *name)
{
struct hlist_head *head = dev_name_hash(net, name);
struct netdev_name_node *name_node;
hlist_for_each_entry(name_node, head, hlist)
if (!strcmp(name_node->name, name))
return name_node;
return NULL;
}
static struct netdev_name_node *netdev_name_node_lookup_rcu(struct net *net,
const char *name)
{
struct hlist_head *head = dev_name_hash(net, name);
struct netdev_name_node *name_node;
hlist_for_each_entry_rcu(name_node, head, hlist)
if (!strcmp(name_node->name, name))
return name_node;
return NULL;
}
int netdev_name_node_alt_create(struct net_device *dev, const char *name)
{
struct netdev_name_node *name_node;
struct net *net = dev_net(dev);
name_node = netdev_name_node_lookup(net, name);
if (name_node)
return -EEXIST;
name_node = netdev_name_node_alloc(dev, name);
if (!name_node)
return -ENOMEM;
netdev_name_node_add(net, name_node);
/* The node that holds dev->name acts as a head of per-device list. */
list_add_tail(&name_node->list, &dev->name_node->list);
return 0;
}
EXPORT_SYMBOL(netdev_name_node_alt_create);
static void __netdev_name_node_alt_destroy(struct netdev_name_node *name_node)
{
list_del(&name_node->list);
netdev_name_node_del(name_node);
kfree(name_node->name);
netdev_name_node_free(name_node);
}
int netdev_name_node_alt_destroy(struct net_device *dev, const char *name)
{
struct netdev_name_node *name_node;
struct net *net = dev_net(dev);
name_node = netdev_name_node_lookup(net, name);
if (!name_node)
return -ENOENT;
__netdev_name_node_alt_destroy(name_node);
return 0;
}
EXPORT_SYMBOL(netdev_name_node_alt_destroy);
static void netdev_name_node_alt_flush(struct net_device *dev)
{
struct netdev_name_node *name_node, *tmp;
list_for_each_entry_safe(name_node, tmp, &dev->name_node->list, list)
__netdev_name_node_alt_destroy(name_node);
}
/* Device list insertion */ /* Device list insertion */
static void list_netdevice(struct net_device *dev) static void list_netdevice(struct net_device *dev)
{ {
...@@ -237,7 +353,7 @@ static void list_netdevice(struct net_device *dev) ...@@ -237,7 +353,7 @@ static void list_netdevice(struct net_device *dev)
write_lock_bh(&dev_base_lock); write_lock_bh(&dev_base_lock);
list_add_tail_rcu(&dev->dev_list, &net->dev_base_head); list_add_tail_rcu(&dev->dev_list, &net->dev_base_head);
hlist_add_head_rcu(&dev->name_hlist, dev_name_hash(net, dev->name)); netdev_name_node_add(net, dev->name_node);
hlist_add_head_rcu(&dev->index_hlist, hlist_add_head_rcu(&dev->index_hlist,
dev_index_hash(net, dev->ifindex)); dev_index_hash(net, dev->ifindex));
write_unlock_bh(&dev_base_lock); write_unlock_bh(&dev_base_lock);
...@@ -255,7 +371,7 @@ static void unlist_netdevice(struct net_device *dev) ...@@ -255,7 +371,7 @@ static void unlist_netdevice(struct net_device *dev)
/* Unlink dev from the device chain */ /* Unlink dev from the device chain */
write_lock_bh(&dev_base_lock); write_lock_bh(&dev_base_lock);
list_del_rcu(&dev->dev_list); list_del_rcu(&dev->dev_list);
hlist_del_rcu(&dev->name_hlist); netdev_name_node_del(dev->name_node);
hlist_del_rcu(&dev->index_hlist); hlist_del_rcu(&dev->index_hlist);
write_unlock_bh(&dev_base_lock); write_unlock_bh(&dev_base_lock);
...@@ -733,14 +849,10 @@ EXPORT_SYMBOL_GPL(dev_fill_metadata_dst); ...@@ -733,14 +849,10 @@ EXPORT_SYMBOL_GPL(dev_fill_metadata_dst);
struct net_device *__dev_get_by_name(struct net *net, const char *name) struct net_device *__dev_get_by_name(struct net *net, const char *name)
{ {
struct net_device *dev; struct netdev_name_node *node_name;
struct hlist_head *head = dev_name_hash(net, name);
hlist_for_each_entry(dev, head, name_hlist)
if (!strncmp(dev->name, name, IFNAMSIZ))
return dev;
return NULL; node_name = netdev_name_node_lookup(net, name);
return node_name ? node_name->dev : NULL;
} }
EXPORT_SYMBOL(__dev_get_by_name); EXPORT_SYMBOL(__dev_get_by_name);
...@@ -758,14 +870,10 @@ EXPORT_SYMBOL(__dev_get_by_name); ...@@ -758,14 +870,10 @@ EXPORT_SYMBOL(__dev_get_by_name);
struct net_device *dev_get_by_name_rcu(struct net *net, const char *name) struct net_device *dev_get_by_name_rcu(struct net *net, const char *name)
{ {
struct net_device *dev; struct netdev_name_node *node_name;
struct hlist_head *head = dev_name_hash(net, name);
hlist_for_each_entry_rcu(dev, head, name_hlist)
if (!strncmp(dev->name, name, IFNAMSIZ))
return dev;
return NULL; node_name = netdev_name_node_lookup_rcu(net, name);
return node_name ? node_name->dev : NULL;
} }
EXPORT_SYMBOL(dev_get_by_name_rcu); EXPORT_SYMBOL(dev_get_by_name_rcu);
...@@ -1232,13 +1340,13 @@ int dev_change_name(struct net_device *dev, const char *newname) ...@@ -1232,13 +1340,13 @@ int dev_change_name(struct net_device *dev, const char *newname)
netdev_adjacent_rename_links(dev, oldname); netdev_adjacent_rename_links(dev, oldname);
write_lock_bh(&dev_base_lock); write_lock_bh(&dev_base_lock);
hlist_del_rcu(&dev->name_hlist); netdev_name_node_del(dev->name_node);
write_unlock_bh(&dev_base_lock); write_unlock_bh(&dev_base_lock);
synchronize_rcu(); synchronize_rcu();
write_lock_bh(&dev_base_lock); write_lock_bh(&dev_base_lock);
hlist_add_head_rcu(&dev->name_hlist, dev_name_hash(net, dev->name)); netdev_name_node_add(net, dev->name_node);
write_unlock_bh(&dev_base_lock); write_unlock_bh(&dev_base_lock);
ret = call_netdevice_notifiers(NETDEV_CHANGENAME, dev); ret = call_netdevice_notifiers(NETDEV_CHANGENAME, dev);
...@@ -8264,6 +8372,9 @@ static void rollback_registered_many(struct list_head *head) ...@@ -8264,6 +8372,9 @@ static void rollback_registered_many(struct list_head *head)
dev_uc_flush(dev); dev_uc_flush(dev);
dev_mc_flush(dev); dev_mc_flush(dev);
netdev_name_node_alt_flush(dev);
netdev_name_node_free(dev->name_node);
if (dev->netdev_ops->ndo_uninit) if (dev->netdev_ops->ndo_uninit)
dev->netdev_ops->ndo_uninit(dev); dev->netdev_ops->ndo_uninit(dev);
...@@ -8706,6 +8817,10 @@ int register_netdevice(struct net_device *dev) ...@@ -8706,6 +8817,10 @@ int register_netdevice(struct net_device *dev)
if (ret < 0) if (ret < 0)
goto out; goto out;
dev->name_node = netdev_name_node_head_alloc(dev);
if (!dev->name_node)
goto out;
/* Init, if this function is available */ /* Init, if this function is available */
if (dev->netdev_ops->ndo_init) { if (dev->netdev_ops->ndo_init) {
ret = dev->netdev_ops->ndo_init(dev); ret = dev->netdev_ops->ndo_init(dev);
...@@ -8827,6 +8942,8 @@ int register_netdevice(struct net_device *dev) ...@@ -8827,6 +8942,8 @@ int register_netdevice(struct net_device *dev)
return ret; return ret;
err_uninit: err_uninit:
if (dev->name_node)
netdev_name_node_free(dev->name_node);
if (dev->netdev_ops->ndo_uninit) if (dev->netdev_ops->ndo_uninit)
dev->netdev_ops->ndo_uninit(dev); dev->netdev_ops->ndo_uninit(dev);
if (dev->priv_destructor) if (dev->priv_destructor)
......
...@@ -20,8 +20,8 @@ static inline struct net_device *dev_from_same_bucket(struct seq_file *seq, loff ...@@ -20,8 +20,8 @@ static inline struct net_device *dev_from_same_bucket(struct seq_file *seq, loff
struct hlist_head *h; struct hlist_head *h;
unsigned int count = 0, offset = get_offset(*pos); unsigned int count = 0, offset = get_offset(*pos);
h = &net->dev_name_head[get_bucket(*pos)]; h = &net->dev_index_head[get_bucket(*pos)];
hlist_for_each_entry_rcu(dev, h, name_hlist) { hlist_for_each_entry_rcu(dev, h, index_hlist) {
if (++count == offset) if (++count == offset)
return dev; return dev;
} }
......
...@@ -980,6 +980,19 @@ static size_t rtnl_xdp_size(void) ...@@ -980,6 +980,19 @@ static size_t rtnl_xdp_size(void)
return xdp_size; return xdp_size;
} }
static size_t rtnl_prop_list_size(const struct net_device *dev)
{
struct netdev_name_node *name_node;
size_t size;
if (list_empty(&dev->name_node->list))
return 0;
size = nla_total_size(0);
list_for_each_entry(name_node, &dev->name_node->list, list)
size += nla_total_size(ALTIFNAMSIZ);
return size;
}
static noinline size_t if_nlmsg_size(const struct net_device *dev, static noinline size_t if_nlmsg_size(const struct net_device *dev,
u32 ext_filter_mask) u32 ext_filter_mask)
{ {
...@@ -1027,6 +1040,7 @@ static noinline size_t if_nlmsg_size(const struct net_device *dev, ...@@ -1027,6 +1040,7 @@ static noinline size_t if_nlmsg_size(const struct net_device *dev,
+ nla_total_size(4) /* IFLA_CARRIER_DOWN_COUNT */ + nla_total_size(4) /* IFLA_CARRIER_DOWN_COUNT */
+ nla_total_size(4) /* IFLA_MIN_MTU */ + nla_total_size(4) /* IFLA_MIN_MTU */
+ nla_total_size(4) /* IFLA_MAX_MTU */ + nla_total_size(4) /* IFLA_MAX_MTU */
+ rtnl_prop_list_size(dev)
+ 0; + 0;
} }
...@@ -1584,6 +1598,42 @@ static int rtnl_fill_link_af(struct sk_buff *skb, ...@@ -1584,6 +1598,42 @@ static int rtnl_fill_link_af(struct sk_buff *skb,
return 0; return 0;
} }
static int rtnl_fill_alt_ifnames(struct sk_buff *skb,
const struct net_device *dev)
{
struct netdev_name_node *name_node;
int count = 0;
list_for_each_entry(name_node, &dev->name_node->list, list) {
if (nla_put_string(skb, IFLA_ALT_IFNAME, name_node->name))
return -EMSGSIZE;
count++;
}
return count;
}
static int rtnl_fill_prop_list(struct sk_buff *skb,
const struct net_device *dev)
{
struct nlattr *prop_list;
int ret;
prop_list = nla_nest_start(skb, IFLA_PROP_LIST);
if (!prop_list)
return -EMSGSIZE;
ret = rtnl_fill_alt_ifnames(skb, dev);
if (ret <= 0)
goto nest_cancel;
nla_nest_end(skb, prop_list);
return 0;
nest_cancel:
nla_nest_cancel(skb, prop_list);
return ret;
}
static int rtnl_fill_ifinfo(struct sk_buff *skb, static int rtnl_fill_ifinfo(struct sk_buff *skb,
struct net_device *dev, struct net *src_net, struct net_device *dev, struct net *src_net,
int type, u32 pid, u32 seq, u32 change, int type, u32 pid, u32 seq, u32 change,
...@@ -1697,6 +1747,9 @@ static int rtnl_fill_ifinfo(struct sk_buff *skb, ...@@ -1697,6 +1747,9 @@ static int rtnl_fill_ifinfo(struct sk_buff *skb,
goto nla_put_failure_rcu; goto nla_put_failure_rcu;
rcu_read_unlock(); rcu_read_unlock();
if (rtnl_fill_prop_list(skb, dev))
goto nla_put_failure;
nlmsg_end(skb, nlh); nlmsg_end(skb, nlh);
return 0; return 0;
...@@ -1750,6 +1803,9 @@ static const struct nla_policy ifla_policy[IFLA_MAX+1] = { ...@@ -1750,6 +1803,9 @@ static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
[IFLA_CARRIER_DOWN_COUNT] = { .type = NLA_U32 }, [IFLA_CARRIER_DOWN_COUNT] = { .type = NLA_U32 },
[IFLA_MIN_MTU] = { .type = NLA_U32 }, [IFLA_MIN_MTU] = { .type = NLA_U32 },
[IFLA_MAX_MTU] = { .type = NLA_U32 }, [IFLA_MAX_MTU] = { .type = NLA_U32 },
[IFLA_PROP_LIST] = { .type = NLA_NESTED },
[IFLA_ALT_IFNAME] = { .type = NLA_STRING,
.len = ALTIFNAMSIZ - 1 },
}; };
static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = { static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
...@@ -2722,6 +2778,26 @@ static int do_setlink(const struct sk_buff *skb, ...@@ -2722,6 +2778,26 @@ static int do_setlink(const struct sk_buff *skb,
return err; return err;
} }
static struct net_device *rtnl_dev_get(struct net *net,
struct nlattr *ifname_attr,
struct nlattr *altifname_attr,
char *ifname)
{
char buffer[ALTIFNAMSIZ];
if (!ifname) {
ifname = buffer;
if (ifname_attr)
nla_strlcpy(ifname, ifname_attr, IFNAMSIZ);
else if (altifname_attr)
nla_strlcpy(ifname, altifname_attr, ALTIFNAMSIZ);
else
return NULL;
}
return __dev_get_by_name(net, ifname);
}
static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
struct netlink_ext_ack *extack) struct netlink_ext_ack *extack)
{ {
...@@ -2750,8 +2826,8 @@ static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, ...@@ -2750,8 +2826,8 @@ static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
ifm = nlmsg_data(nlh); ifm = nlmsg_data(nlh);
if (ifm->ifi_index > 0) if (ifm->ifi_index > 0)
dev = __dev_get_by_index(net, ifm->ifi_index); dev = __dev_get_by_index(net, ifm->ifi_index);
else if (tb[IFLA_IFNAME]) else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
dev = __dev_get_by_name(net, ifname); dev = rtnl_dev_get(net, NULL, tb[IFLA_ALT_IFNAME], ifname);
else else
goto errout; goto errout;
...@@ -2824,7 +2900,6 @@ static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, ...@@ -2824,7 +2900,6 @@ static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
struct net *tgt_net = net; struct net *tgt_net = net;
struct net_device *dev = NULL; struct net_device *dev = NULL;
struct ifinfomsg *ifm; struct ifinfomsg *ifm;
char ifname[IFNAMSIZ];
struct nlattr *tb[IFLA_MAX+1]; struct nlattr *tb[IFLA_MAX+1];
int err; int err;
int netnsid = -1; int netnsid = -1;
...@@ -2838,9 +2913,6 @@ static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, ...@@ -2838,9 +2913,6 @@ static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
if (err < 0) if (err < 0)
return err; return err;
if (tb[IFLA_IFNAME])
nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
if (tb[IFLA_TARGET_NETNSID]) { if (tb[IFLA_TARGET_NETNSID]) {
netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]); netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid); tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
...@@ -2852,8 +2924,9 @@ static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, ...@@ -2852,8 +2924,9 @@ static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
ifm = nlmsg_data(nlh); ifm = nlmsg_data(nlh);
if (ifm->ifi_index > 0) if (ifm->ifi_index > 0)
dev = __dev_get_by_index(tgt_net, ifm->ifi_index); dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
else if (tb[IFLA_IFNAME]) else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
dev = __dev_get_by_name(tgt_net, ifname); dev = rtnl_dev_get(net, tb[IFLA_IFNAME],
tb[IFLA_ALT_IFNAME], NULL);
else if (tb[IFLA_GROUP]) else if (tb[IFLA_GROUP])
err = rtnl_group_dellink(tgt_net, nla_get_u32(tb[IFLA_GROUP])); err = rtnl_group_dellink(tgt_net, nla_get_u32(tb[IFLA_GROUP]));
else else
...@@ -3024,12 +3097,10 @@ static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, ...@@ -3024,12 +3097,10 @@ static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
ifm = nlmsg_data(nlh); ifm = nlmsg_data(nlh);
if (ifm->ifi_index > 0) if (ifm->ifi_index > 0)
dev = __dev_get_by_index(net, ifm->ifi_index); dev = __dev_get_by_index(net, ifm->ifi_index);
else { else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
if (ifname[0]) dev = rtnl_dev_get(net, NULL, tb[IFLA_ALT_IFNAME], ifname);
dev = __dev_get_by_name(net, ifname);
else else
dev = NULL; dev = NULL;
}
if (dev) { if (dev) {
master_dev = netdev_master_upper_dev_get(dev); master_dev = netdev_master_upper_dev_get(dev);
...@@ -3291,6 +3362,7 @@ static int rtnl_valid_getlink_req(struct sk_buff *skb, ...@@ -3291,6 +3362,7 @@ static int rtnl_valid_getlink_req(struct sk_buff *skb,
switch (i) { switch (i) {
case IFLA_IFNAME: case IFLA_IFNAME:
case IFLA_ALT_IFNAME:
case IFLA_EXT_MASK: case IFLA_EXT_MASK:
case IFLA_TARGET_NETNSID: case IFLA_TARGET_NETNSID:
break; break;
...@@ -3309,7 +3381,6 @@ static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh, ...@@ -3309,7 +3381,6 @@ static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
struct net *net = sock_net(skb->sk); struct net *net = sock_net(skb->sk);
struct net *tgt_net = net; struct net *tgt_net = net;
struct ifinfomsg *ifm; struct ifinfomsg *ifm;
char ifname[IFNAMSIZ];
struct nlattr *tb[IFLA_MAX+1]; struct nlattr *tb[IFLA_MAX+1];
struct net_device *dev = NULL; struct net_device *dev = NULL;
struct sk_buff *nskb; struct sk_buff *nskb;
...@@ -3332,9 +3403,6 @@ static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh, ...@@ -3332,9 +3403,6 @@ static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
return PTR_ERR(tgt_net); return PTR_ERR(tgt_net);
} }
if (tb[IFLA_IFNAME])
nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
if (tb[IFLA_EXT_MASK]) if (tb[IFLA_EXT_MASK])
ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]); ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
...@@ -3342,8 +3410,9 @@ static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh, ...@@ -3342,8 +3410,9 @@ static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
ifm = nlmsg_data(nlh); ifm = nlmsg_data(nlh);
if (ifm->ifi_index > 0) if (ifm->ifi_index > 0)
dev = __dev_get_by_index(tgt_net, ifm->ifi_index); dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
else if (tb[IFLA_IFNAME]) else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
dev = __dev_get_by_name(tgt_net, ifname); dev = rtnl_dev_get(tgt_net, tb[IFLA_IFNAME],
tb[IFLA_ALT_IFNAME], NULL);
else else
goto out; goto out;
...@@ -3373,6 +3442,100 @@ static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh, ...@@ -3373,6 +3442,100 @@ static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
return err; return err;
} }
static int rtnl_alt_ifname(int cmd, struct net_device *dev, struct nlattr *attr,
bool *changed, struct netlink_ext_ack *extack)
{
char *alt_ifname;
int err;
err = nla_validate(attr, attr->nla_len, IFLA_MAX, ifla_policy, extack);
if (err)
return err;
alt_ifname = nla_data(attr);
if (cmd == RTM_NEWLINKPROP) {
alt_ifname = kstrdup(alt_ifname, GFP_KERNEL);
if (!alt_ifname)
return -ENOMEM;
err = netdev_name_node_alt_create(dev, alt_ifname);
if (err) {
kfree(alt_ifname);
return err;
}
} else if (cmd == RTM_DELLINKPROP) {
err = netdev_name_node_alt_destroy(dev, alt_ifname);
if (err)
return err;
} else {
WARN_ON(1);
return 0;
}
*changed = true;
return 0;
}
static int rtnl_linkprop(int cmd, struct sk_buff *skb, struct nlmsghdr *nlh,
struct netlink_ext_ack *extack)
{
struct net *net = sock_net(skb->sk);
struct nlattr *tb[IFLA_MAX + 1];
struct net_device *dev;
struct ifinfomsg *ifm;
bool changed = false;
struct nlattr *attr;
int err, rem;
err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
if (err)
return err;
err = rtnl_ensure_unique_netns(tb, extack, true);
if (err)
return err;
ifm = nlmsg_data(nlh);
if (ifm->ifi_index > 0)
dev = __dev_get_by_index(net, ifm->ifi_index);
else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
dev = rtnl_dev_get(net, tb[IFLA_IFNAME],
tb[IFLA_ALT_IFNAME], NULL);
else
return -EINVAL;
if (!dev)
return -ENODEV;
if (!tb[IFLA_PROP_LIST])
return 0;
nla_for_each_nested(attr, tb[IFLA_PROP_LIST], rem) {
switch (nla_type(attr)) {
case IFLA_ALT_IFNAME:
err = rtnl_alt_ifname(cmd, dev, attr, &changed, extack);
if (err)
return err;
break;
}
}
if (changed)
netdev_state_change(dev);
return 0;
}
static int rtnl_newlinkprop(struct sk_buff *skb, struct nlmsghdr *nlh,
struct netlink_ext_ack *extack)
{
return rtnl_linkprop(RTM_NEWLINKPROP, skb, nlh, extack);
}
static int rtnl_dellinkprop(struct sk_buff *skb, struct nlmsghdr *nlh,
struct netlink_ext_ack *extack)
{
return rtnl_linkprop(RTM_DELLINKPROP, skb, nlh, extack);
}
static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh) static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
{ {
struct net *net = sock_net(skb->sk); struct net *net = sock_net(skb->sk);
...@@ -5331,6 +5494,9 @@ void __init rtnetlink_init(void) ...@@ -5331,6 +5494,9 @@ void __init rtnetlink_init(void)
rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0); rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0);
rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0); rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0);
rtnl_register(PF_UNSPEC, RTM_NEWLINKPROP, rtnl_newlinkprop, NULL, 0);
rtnl_register(PF_UNSPEC, RTM_DELLINKPROP, rtnl_dellinkprop, NULL, 0);
rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0); rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0);
rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0); rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0);
rtnl_register(PF_BRIDGE, RTM_GETNEIGH, rtnl_fdb_get, rtnl_fdb_dump, 0); rtnl_register(PF_BRIDGE, RTM_GETNEIGH, rtnl_fdb_get, rtnl_fdb_dump, 0);
......
...@@ -83,6 +83,8 @@ static const struct nlmsg_perm nlmsg_route_perms[] = ...@@ -83,6 +83,8 @@ static const struct nlmsg_perm nlmsg_route_perms[] =
{ RTM_NEWNEXTHOP, NETLINK_ROUTE_SOCKET__NLMSG_WRITE }, { RTM_NEWNEXTHOP, NETLINK_ROUTE_SOCKET__NLMSG_WRITE },
{ RTM_DELNEXTHOP, NETLINK_ROUTE_SOCKET__NLMSG_WRITE }, { RTM_DELNEXTHOP, NETLINK_ROUTE_SOCKET__NLMSG_WRITE },
{ RTM_GETNEXTHOP, NETLINK_ROUTE_SOCKET__NLMSG_READ }, { RTM_GETNEXTHOP, NETLINK_ROUTE_SOCKET__NLMSG_READ },
{ RTM_NEWLINKPROP, NETLINK_ROUTE_SOCKET__NLMSG_WRITE },
{ RTM_DELLINKPROP, NETLINK_ROUTE_SOCKET__NLMSG_WRITE },
}; };
static const struct nlmsg_perm nlmsg_tcpdiag_perms[] = static const struct nlmsg_perm nlmsg_tcpdiag_perms[] =
...@@ -166,7 +168,7 @@ int selinux_nlmsg_lookup(u16 sclass, u16 nlmsg_type, u32 *perm) ...@@ -166,7 +168,7 @@ int selinux_nlmsg_lookup(u16 sclass, u16 nlmsg_type, u32 *perm)
* structures at the top of this file with the new mappings * structures at the top of this file with the new mappings
* before updating the BUILD_BUG_ON() macro! * before updating the BUILD_BUG_ON() macro!
*/ */
BUILD_BUG_ON(RTM_MAX != (RTM_NEWNEXTHOP + 3)); BUILD_BUG_ON(RTM_MAX != (RTM_NEWLINKPROP + 3));
err = nlmsg_perm(nlmsg_type, perm, nlmsg_route_perms, err = nlmsg_perm(nlmsg_type, perm, nlmsg_route_perms,
sizeof(nlmsg_route_perms)); sizeof(nlmsg_route_perms));
break; break;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment