19 #include <linux/errno.h>
20 #include <linux/module.h>
21 #include <linux/types.h>
22 #include <linux/socket.h>
23 #include <linux/kernel.h>
25 #include <linux/string.h>
27 #include <linux/net.h>
28 #include <linux/fcntl.h>
30 #include <linux/slab.h>
32 #include <linux/capability.h>
38 #include <linux/if_bridge.h>
39 #include <linux/pci.h>
42 #include <asm/uaccess.h>
45 #include <linux/netdevice.h>
91 return mutex_is_locked(&rtnl_mutex);
95 #ifdef CONFIG_PROVE_LOCKING
96 int lockdep_rtnl_is_held(
void)
98 return lockdep_is_held(&rtnl_mutex);
105 static inline int rtm_msgindex(
int msgtype)
131 return tab ? tab[msgindex].
doit :
NULL;
190 msgindex = rtm_msgindex(msgtype);
228 panic(
"Unable to register rtnetlink message handler, "
229 "protocol = %d, message type = %d\n",
246 msgindex = rtm_msgindex(msgtype);
248 if (rtnl_msg_handlers[protocol] ==
NULL)
269 kfree(rtnl_msg_handlers[protocol]);
299 if (rtnl_link_ops_get(ops->
kind))
350 __rtnl_kill_links(net, ops);
368 static size_t rtnl_link_get_size(
const struct net_device *dev)
376 size = nla_total_size(
sizeof(
struct nlattr)) +
381 size += nla_total_size(
sizeof(
struct nlattr)) +
398 if (ops->
family == family)
461 static size_t rtnl_link_get_af_size(
const struct net_device *dev)
467 size = nla_total_size(
sizeof(
struct nlattr));
472 size += nla_total_size(
sizeof(
struct nlattr)) +
487 if (linkinfo ==
NULL)
491 goto err_cancel_link;
495 goto err_cancel_link;
500 goto err_cancel_link;
503 goto err_cancel_data;
504 nla_nest_end(skb, data);
507 nla_nest_end(skb, linkinfo);
511 nla_nest_cancel(skb, data);
513 nla_nest_cancel(skb, linkinfo);
562 return nlmsg_unicast(rtnl, skb, pid);
573 report = nlmsg_report(nlh);
599 if (nla_put_u32(skb, i+1, metrics[i]))
600 goto nla_put_failure;
605 nla_nest_cancel(skb, mx);
609 return nla_nest_end(skb, mx);
612 nla_nest_cancel(skb, mx);
622 .rta_used = dst->
__use,
639 static void set_operstate(
struct net_device *dev,
unsigned char transition)
641 unsigned char operstate = dev->
operstate;
643 switch (transition) {
666 static unsigned int rtnl_dev_get_flags(
const struct net_device *dev)
672 static unsigned int rtnl_dev_combine_flags(
const struct net_device *dev,
723 static inline int rtnl_vfinfo_size(
const struct net_device *dev,
729 size_t size = nla_total_size(
sizeof(
struct nlattr));
730 size += nla_total_size(num_vfs *
sizeof(
struct nlattr));
741 static size_t rtnl_port_size(
const struct net_device *dev)
743 size_t port_size = nla_total_size(4)
751 size_t vf_ports_size = nla_total_size(
sizeof(
struct nlattr));
752 size_t vf_port_size = nla_total_size(
sizeof(
struct nlattr))
754 size_t port_self_size = nla_total_size(
sizeof(
struct nlattr))
760 return port_self_size + vf_ports_size +
763 return port_self_size;
788 + nla_total_size(ext_filter_mask
789 & RTEXT_FILTER_VF ? 4 : 0)
790 + rtnl_vfinfo_size(dev, ext_filter_mask)
791 + rtnl_port_size(dev)
792 + rtnl_link_get_size(dev)
793 + rtnl_link_get_af_size(dev);
810 goto nla_put_failure;
812 goto nla_put_failure;
813 err = dev->
netdev_ops->ndo_get_vf_port(dev, vf, skb);
815 goto nla_put_failure;
817 nla_nest_cancel(skb, vf_port);
820 nla_nest_end(skb, vf_port);
823 nla_nest_end(skb, vf_ports);
828 nla_nest_cancel(skb, vf_ports);
843 nla_nest_cancel(skb, port_self);
844 return (err == -
EMSGSIZE) ? err : 0;
847 nla_nest_end(skb, port_self);
859 err = rtnl_port_self_fill(skb, dev);
864 err = rtnl_vf_ports_fill(skb, dev);
874 unsigned int flags,
u32 ext_filter_mask)
884 nlh = nlmsg_put(skb, pid, seq, type,
sizeof(*ifm), flags);
888 ifm = nlmsg_data(nlh);
916 goto nla_put_failure;
928 goto nla_put_failure;
934 goto nla_put_failure;
940 goto nla_put_failure;
943 copy_rtnl_link_stats(nla_data(attr), stats);
948 goto nla_put_failure;
949 copy_rtnl_link_stats64(nla_data(attr), stats);
951 if (dev->
dev.parent && (ext_filter_mask & RTEXT_FILTER_VF) &&
953 goto nla_put_failure;
956 && (ext_filter_mask & RTEXT_FILTER_VF)) {
964 goto nla_put_failure;
965 for (i = 0; i < num_vfs; i++) {
979 if (dev->
netdev_ops->ndo_get_vf_config(dev, i, &ivi))
984 vf_spoofchk.vf = ivi.vf;
986 memcpy(vf_mac.mac, ivi.mac,
sizeof(ivi.mac));
987 vf_vlan.vlan = ivi.vlan;
988 vf_vlan.qos = ivi.qos;
989 vf_tx_rate.rate = ivi.tx_rate;
990 vf_spoofchk.setting = ivi.spoofchk;
993 nla_nest_cancel(skb, vfinfo);
994 goto nla_put_failure;
1002 goto nla_put_failure;
1003 nla_nest_end(skb, vf);
1005 nla_nest_end(skb, vfinfo);
1008 if (rtnl_port_fill(skb, dev))
1009 goto nla_put_failure;
1012 if (rtnl_link_fill(skb, dev) < 0)
1013 goto nla_put_failure;
1017 goto nla_put_failure;
1024 if (!(af = nla_nest_start(skb, af_ops->
family)))
1025 goto nla_put_failure;
1036 nla_nest_cancel(skb, af);
1038 goto nla_put_failure;
1040 nla_nest_end(skb, af);
1044 nla_nest_end(skb, af_spec);
1046 return nlmsg_end(skb, nlh);
1049 nlmsg_cancel(skb, nlh);
1055 struct net *net = sock_net(skb->
sk);
1062 u32 ext_filter_mask = 0;
1065 s_idx = cb->
args[1];
1074 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1080 hlist_for_each_entry_rcu(dev, node, head, index_hlist) {
1085 cb->
nlh->nlmsg_seq, 0,
1087 ext_filter_mask) <= 0)
1090 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
1108 [IFLA_MTU] = { .type =
NLA_U32 },
1130 static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1135 static const struct nla_policy ifla_vfinfo_policy[IFLA_VF_INFO_MAX+1] = {
1139 static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1150 static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1156 [IFLA_PORT_INSTANCE_UUID] = { .type =
NLA_BINARY,
1175 net = get_net(src_net);
1199 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1223 switch (nla_type(vf)) {
1233 case IFLA_VF_VLAN: {
1243 case IFLA_VF_TX_RATE: {
1252 case IFLA_VF_SPOOFCHK: {
1271 static int do_set_master(
struct net_device *dev,
int ifindex)
1278 if (dev->
master->ifindex == ifindex)
1280 ops = dev->
master->netdev_ops;
1307 struct nlattr **tb,
char *ifname,
int modified)
1310 int send_addr_notify = 0;
1335 if (!netif_device_present(dev)) {
1340 u_map = nla_data(tb[IFLA_MAP]);
1342 k_map.mem_end = (
unsigned long) u_map->
mem_end;
1344 k_map.irq = (
unsigned char) u_map->
irq;
1345 k_map.dma = (
unsigned char) u_map->
dma;
1346 k_map.port = (
unsigned char) u_map->
port;
1355 if (tb[IFLA_ADDRESS]) {
1364 if (!netif_device_present(dev)) {
1382 send_addr_notify = 1;
1388 err =
dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
1413 nla_len(tb[IFLA_IFALIAS]));
1421 send_addr_notify = 1;
1431 err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
1441 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
1445 dev->
link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
1457 err = do_setvfinfo(dev, attr);
1478 err = nla_parse_nested(port, IFLA_PORT_MAX,
1479 attr, ifla_port_policy);
1486 vf = nla_get_u32(port[IFLA_PORT_VF]);
1496 struct nlattr *port[IFLA_PORT_MAX+1];
1498 err = nla_parse_nested(port, IFLA_PORT_MAX,
1499 tb[IFLA_PORT_SELF], ifla_port_policy);
1511 if (tb[IFLA_AF_SPEC]) {
1518 if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1531 if (err < 0 && modified)
1532 net_warn_ratelimited(
"A link change request failed with some changes committed already. Interface %s may have been left with an inconsistent configuration, please check.\n",
1535 if (send_addr_notify)
1543 struct net *net = sock_net(skb->
sk);
1550 err = nlmsg_parse(nlh,
sizeof(*ifm), tb,
IFLA_MAX, ifla_policy);
1560 ifm = nlmsg_data(nlh);
1563 else if (tb[IFLA_IFNAME])
1573 err = validate_linkmsg(dev, tb);
1577 err = do_setlink(dev, ifm, tb, ifname, 0);
1582 static int rtnl_dellink(
struct sk_buff *skb,
struct nlmsghdr *nlh,
void *arg)
1584 struct net *net = sock_net(skb->
sk);
1593 err = nlmsg_parse(nlh,
sizeof(*ifm), tb,
IFLA_MAX, ifla_policy);
1597 if (tb[IFLA_IFNAME])
1600 ifm = nlmsg_data(nlh);
1603 else if (tb[IFLA_IFNAME])
1615 ops->
dellink(dev, &list_kill);
1623 unsigned int old_flags;
1626 old_flags = dev->
flags;
1647 unsigned int num_rx_queues = 1;
1650 num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
1655 num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
1661 num_tx_queues, num_rx_queues);
1665 dev_net_set(dev, net);
1670 dev->
mtu = nla_get_u32(tb[IFLA_MTU]);
1671 if (tb[IFLA_ADDRESS])
1673 nla_len(tb[IFLA_ADDRESS]));
1674 if (tb[IFLA_BROADCAST])
1676 nla_len(tb[IFLA_BROADCAST]));
1677 if (tb[IFLA_TXQLEN])
1679 if (tb[IFLA_OPERSTATE])
1680 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
1681 if (tb[IFLA_LINKMODE])
1682 dev->
link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
1689 return ERR_PTR(err);
1693 static int rtnl_group_changelink(
struct net *net,
int group,
1701 if (dev->
group == group) {
1702 err = do_setlink(dev, ifm, tb,
NULL, 0);
1711 static int rtnl_newlink(
struct sk_buff *skb,
struct nlmsghdr *nlh,
void *arg)
1713 struct net *net = sock_net(skb->
sk);
1720 struct nlattr *linkinfo[IFLA_INFO_MAX+1];
1723 #ifdef CONFIG_MODULES
1726 err = nlmsg_parse(nlh,
sizeof(*ifm), tb,
IFLA_MAX, ifla_policy);
1730 if (tb[IFLA_IFNAME])
1735 ifm = nlmsg_data(nlh);
1745 err = validate_linkmsg(dev, tb);
1750 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
1751 tb[IFLA_LINKINFO], ifla_info_policy);
1755 memset(linkinfo, 0,
sizeof(linkinfo));
1758 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND],
sizeof(kind));
1759 ops = rtnl_link_ops_get(kind);
1767 struct net *dest_net;
1771 err = nla_parse_nested(attr, ops->
maxtype,
1772 linkinfo[IFLA_INFO_DATA],
1793 if (linkinfo[IFLA_INFO_DATA]) {
1804 return do_setlink(dev, ifm, tb, ifname, modified);
1808 if (ifm->
ifi_index == 0 && tb[IFLA_GROUP])
1809 return rtnl_group_changelink(net,
1810 nla_get_u32(tb[IFLA_GROUP]),
1819 #ifdef CONFIG_MODULES
1822 request_module(
"rtnl-link-%s", kind);
1824 ops = rtnl_link_ops_get(kind);
1836 if (IS_ERR(dest_net))
1837 return PTR_ERR(dest_net);
1848 err = ops->
newlink(net, dev, tb, data);
1852 if (err < 0 && !IS_ERR(dev))
1859 unregister_netdevice(dev);
1866 static int rtnl_getlink(
struct sk_buff *skb,
struct nlmsghdr* nlh,
void *arg)
1868 struct net *net = sock_net(skb->
sk);
1875 u32 ext_filter_mask = 0;
1877 err = nlmsg_parse(nlh,
sizeof(*ifm), tb,
IFLA_MAX, ifla_policy);
1881 if (tb[IFLA_IFNAME])
1885 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1887 ifm = nlmsg_data(nlh);
1890 else if (tb[IFLA_IFNAME])
1898 nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask),
GFP_KERNEL);
1916 struct net *net = sock_net(skb->
sk);
1919 u32 ext_filter_mask = 0;
1920 u16 min_ifinfo_dump_size = 0;
1923 ifla_policy) >= 0) {
1924 if (tb[IFLA_EXT_MASK])
1925 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1928 if (!ext_filter_mask)
1935 min_ifinfo_dump_size =
max_t(
u16, min_ifinfo_dump_size,
1940 return min_ifinfo_dump_size;
1954 if (rtnl_msg_handlers[idx] ==
NULL ||
1955 rtnl_msg_handlers[idx][type].dumpit ==
NULL)
1959 if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
1969 struct net *net = dev_net(dev);
1972 size_t if_info_size;
1974 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)),
GFP_KERNEL);
1978 err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0);
1992 static int nlmsg_populate_fdb_fill(
struct sk_buff *skb,
1995 int type,
unsigned int flags)
2000 nlh = nlmsg_put(skb, pid, seq, type,
sizeof(*ndm),
NLM_F_MULTI);
2004 ndm = nlmsg_data(nlh);
2014 goto nla_put_failure;
2016 return nlmsg_end(skb, nlh);
2019 nlmsg_cancel(skb, nlh);
2023 static inline size_t rtnl_fdb_nlmsg_size(
void)
2028 static void rtnl_fdb_notify(
struct net_device *dev,
u8 *addr,
int type)
2030 struct net *net = dev_net(dev);
2034 skb = nlmsg_new(rtnl_fdb_nlmsg_size(),
GFP_ATOMIC);
2038 err = nlmsg_populate_fdb_fill(skb, dev, addr, 0, 0, type,
NTF_SELF);
2050 static int rtnl_fdb_add(
struct sk_buff *skb,
struct nlmsghdr *nlh,
void *arg)
2052 struct net *net = sock_net(skb->
sk);
2060 err = nlmsg_parse(nlh,
sizeof(*ndm), tb,
NDA_MAX,
NULL);
2064 ndm = nlmsg_data(nlh);
2066 pr_info(
"PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n");
2072 pr_info(
"PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n");
2077 pr_info(
"PF_BRIDGE: RTM_NEWNEIGH with invalid address\n");
2081 addr = nla_data(tb[NDA_LLADDR]);
2082 if (!is_valid_ether_addr(addr)) {
2083 pr_info(
"PF_BRIDGE: RTM_NEWNEIGH with invalid ether address\n");
2093 err = master->
netdev_ops->ndo_fdb_add(ndm, tb,
2117 static int rtnl_fdb_del(
struct sk_buff *skb,
struct nlmsghdr *nlh,
void *arg)
2119 struct net *net = sock_net(skb->
sk);
2126 if (nlmsg_len(nlh) <
sizeof(*ndm))
2129 ndm = nlmsg_data(nlh);
2131 pr_info(
"PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n");
2137 pr_info(
"PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n");
2141 llattr = nlmsg_find_attr(nlh,
sizeof(*ndm), NDA_LLADDR);
2143 pr_info(
"PF_BRIGDE: RTM_DELNEIGH with invalid address\n");
2147 addr = nla_data(llattr);
2156 err = master->
netdev_ops->ndo_fdb_del(ndm, dev, addr);
2166 err = dev->
netdev_ops->ndo_fdb_del(ndm, dev, addr);
2177 static int nlmsg_populate_fdb(
struct sk_buff *skb,
2188 seq = cb->
nlh->nlmsg_seq;
2191 if (*idx < cb->args[0])
2194 err = nlmsg_populate_fdb_fill(skb, dev, ha->
addr,
2220 netif_addr_lock_bh(dev);
2221 err = nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->
uc);
2224 nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->
mc);
2226 netif_addr_unlock_bh(dev);
2234 struct net *net = sock_net(skb->
sk);
2248 idx = dev->
netdev_ops->ndo_fdb_dump(skb, cb, dev, idx);
2257 static struct rtattr **rta_buf;
2258 static int rtattr_max;
2262 static int rtnetlink_rcv_msg(
struct sk_buff *skb,
struct nlmsghdr *nlh)
2264 struct net *net = sock_net(skb->
sk);
2293 u16 min_dump_alloc = 0;
2295 dumpit = rtnl_get_dumpit(family, type);
2298 calcit = rtnl_get_calcit(family, type);
2300 min_dump_alloc = calcit(skb, nlh);
2309 err = netlink_dump_start(rtnl, skb, nlh, &c);
2315 memset(rta_buf, 0, (rtattr_max *
sizeof(
struct rtattr *)));
2317 min_len = rtm_min[sz_idx];
2325 while (
RTA_OK(attr, attrlen)) {
2326 unsigned int flavor = attr->
rta_type;
2328 if (flavor > rta_max[sz_idx])
2330 rta_buf[flavor-1] =
attr;
2336 doit = rtnl_get_doit(family, type);
2340 return doit(skb, nlh, (
void *)&rta_buf[0]);
2343 static void rtnetlink_rcv(
struct sk_buff *skb)
2376 .notifier_call = rtnetlink_event,
2380 static int __net_init rtnetlink_net_init(
struct net *net)
2385 .input = rtnetlink_rcv,
2386 .cb_mutex = &rtnl_mutex,
2397 static void __net_exit rtnetlink_net_exit(
struct net *net)
2404 .init = rtnetlink_net_init,
2405 .exit = rtnetlink_net_exit,
2414 if (rta_max[i] > rtattr_max)
2415 rtattr_max = rta_max[
i];
2418 panic(
"rtnetlink_init: cannot allocate rta_buf\n");
2421 panic(
"rtnetlink_init: cannot initialize rtnetlink\n");
2426 rtnl_dump_ifinfo, rtnl_calcit);