rtnetlink.c (f01387d2693813eb5271a3448e6a082322c7d75d) rtnetlink.c (b0ab2fabb5b91da99c189db02e91ae10bc8355c5)
1/*
2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
5 *
6 * Routing netlink socket interface: protocol independent part.
7 *
8 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>

--- 285 unchanged lines hidden (view full) ---

294 *
295 * Returns 0 on success or a negative error code.
296 */
297int __rtnl_link_register(struct rtnl_link_ops *ops)
298{
299 if (rtnl_link_ops_get(ops->kind))
300 return -EEXIST;
301
1/*
2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
5 *
6 * Routing netlink socket interface: protocol independent part.
7 *
8 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>

--- 285 unchanged lines hidden (view full) ---

294 *
295 * Returns 0 on success or a negative error code.
296 */
297int __rtnl_link_register(struct rtnl_link_ops *ops)
298{
299 if (rtnl_link_ops_get(ops->kind))
300 return -EEXIST;
301
302 if (!ops->dellink)
302 /* The check for setup is here because if ops
303 * does not have that filled up, it is not possible
304 * to use the ops for creating device. So do not
305 * fill up dellink as well. That disables rtnl_dellink.
306 */
307 if (ops->setup && !ops->dellink)
303 ops->dellink = unregister_netdevice_queue;
304
305 list_add_tail(&ops->list, &link_ops);
306 return 0;
307}
308EXPORT_SYMBOL_GPL(__rtnl_link_register);
309
310/**

--- 482 unchanged lines hidden (view full) ---

793 if (dev->dev.parent && dev_is_pci(dev->dev.parent) &&
794 (ext_filter_mask & RTEXT_FILTER_VF)) {
795 int num_vfs = dev_num_vf(dev->dev.parent);
796 size_t size = nla_total_size(sizeof(struct nlattr));
797 size += nla_total_size(num_vfs * sizeof(struct nlattr));
798 size += num_vfs *
799 (nla_total_size(sizeof(struct ifla_vf_mac)) +
800 nla_total_size(sizeof(struct ifla_vf_vlan)) +
308 ops->dellink = unregister_netdevice_queue;
309
310 list_add_tail(&ops->list, &link_ops);
311 return 0;
312}
313EXPORT_SYMBOL_GPL(__rtnl_link_register);
314
315/**

--- 482 unchanged lines hidden (view full) ---

798 if (dev->dev.parent && dev_is_pci(dev->dev.parent) &&
799 (ext_filter_mask & RTEXT_FILTER_VF)) {
800 int num_vfs = dev_num_vf(dev->dev.parent);
801 size_t size = nla_total_size(sizeof(struct nlattr));
802 size += nla_total_size(num_vfs * sizeof(struct nlattr));
803 size += num_vfs *
804 (nla_total_size(sizeof(struct ifla_vf_mac)) +
805 nla_total_size(sizeof(struct ifla_vf_vlan)) +
801 nla_total_size(sizeof(struct ifla_vf_tx_rate)) +
802 nla_total_size(sizeof(struct ifla_vf_spoofchk)));
806 nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
807 nla_total_size(sizeof(struct ifla_vf_rate)));
803 return size;
804 } else
805 return 0;
806}
807
808static size_t rtnl_port_size(const struct net_device *dev,
809 u32 ext_filter_mask)
810{

--- 249 unchanged lines hidden (view full) ---

1060
1061 vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
1062 if (!vfinfo)
1063 goto nla_put_failure;
1064 for (i = 0; i < num_vfs; i++) {
1065 struct ifla_vf_info ivi;
1066 struct ifla_vf_mac vf_mac;
1067 struct ifla_vf_vlan vf_vlan;
808 return size;
809 } else
810 return 0;
811}
812
813static size_t rtnl_port_size(const struct net_device *dev,
814 u32 ext_filter_mask)
815{

--- 249 unchanged lines hidden (view full) ---

1065
1066 vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
1067 if (!vfinfo)
1068 goto nla_put_failure;
1069 for (i = 0; i < num_vfs; i++) {
1070 struct ifla_vf_info ivi;
1071 struct ifla_vf_mac vf_mac;
1072 struct ifla_vf_vlan vf_vlan;
1073 struct ifla_vf_rate vf_rate;
1068 struct ifla_vf_tx_rate vf_tx_rate;
1069 struct ifla_vf_spoofchk vf_spoofchk;
1070 struct ifla_vf_link_state vf_linkstate;
1071
1072 /*
1073 * Not all SR-IOV capable drivers support the
1074 * spoofcheck query. Preset to -1 so the user
1075 * space tool can detect that the driver didn't

--- 4 unchanged lines hidden (view full) ---

1080 /* The default value for VF link state is "auto"
1081 * IFLA_VF_LINK_STATE_AUTO which equals zero
1082 */
1083 ivi.linkstate = 0;
1084 if (dev->netdev_ops->ndo_get_vf_config(dev, i, &ivi))
1085 break;
1086 vf_mac.vf =
1087 vf_vlan.vf =
1074 struct ifla_vf_tx_rate vf_tx_rate;
1075 struct ifla_vf_spoofchk vf_spoofchk;
1076 struct ifla_vf_link_state vf_linkstate;
1077
1078 /*
1079 * Not all SR-IOV capable drivers support the
1080 * spoofcheck query. Preset to -1 so the user
1081 * space tool can detect that the driver didn't

--- 4 unchanged lines hidden (view full) ---

1086 /* The default value for VF link state is "auto"
1087 * IFLA_VF_LINK_STATE_AUTO which equals zero
1088 */
1089 ivi.linkstate = 0;
1090 if (dev->netdev_ops->ndo_get_vf_config(dev, i, &ivi))
1091 break;
1092 vf_mac.vf =
1093 vf_vlan.vf =
1094 vf_rate.vf =
1088 vf_tx_rate.vf =
1089 vf_spoofchk.vf =
1090 vf_linkstate.vf = ivi.vf;
1091
1092 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1093 vf_vlan.vlan = ivi.vlan;
1094 vf_vlan.qos = ivi.qos;
1095 vf_tx_rate.vf =
1096 vf_spoofchk.vf =
1097 vf_linkstate.vf = ivi.vf;
1098
1099 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1100 vf_vlan.vlan = ivi.vlan;
1101 vf_vlan.qos = ivi.qos;
1095 vf_tx_rate.rate = ivi.tx_rate;
1102 vf_tx_rate.rate = ivi.max_tx_rate;
1103 vf_rate.min_tx_rate = ivi.min_tx_rate;
1104 vf_rate.max_tx_rate = ivi.max_tx_rate;
1096 vf_spoofchk.setting = ivi.spoofchk;
1097 vf_linkstate.link_state = ivi.linkstate;
1098 vf = nla_nest_start(skb, IFLA_VF_INFO);
1099 if (!vf) {
1100 nla_nest_cancel(skb, vfinfo);
1101 goto nla_put_failure;
1102 }
1103 if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1104 nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1105 vf_spoofchk.setting = ivi.spoofchk;
1106 vf_linkstate.link_state = ivi.linkstate;
1107 vf = nla_nest_start(skb, IFLA_VF_INFO);
1108 if (!vf) {
1109 nla_nest_cancel(skb, vfinfo);
1110 goto nla_put_failure;
1111 }
1112 if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1113 nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1114 nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1115 &vf_rate) ||
1105 nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1106 &vf_tx_rate) ||
1107 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1108 &vf_spoofchk) ||
1109 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1110 &vf_linkstate))
1111 goto nla_put_failure;
1112 nla_nest_end(skb, vf);

--- 90 unchanged lines hidden (view full) ---

1203 [IFLA_VF_MAC] = { .type = NLA_BINARY,
1204 .len = sizeof(struct ifla_vf_mac) },
1205 [IFLA_VF_VLAN] = { .type = NLA_BINARY,
1206 .len = sizeof(struct ifla_vf_vlan) },
1207 [IFLA_VF_TX_RATE] = { .type = NLA_BINARY,
1208 .len = sizeof(struct ifla_vf_tx_rate) },
1209 [IFLA_VF_SPOOFCHK] = { .type = NLA_BINARY,
1210 .len = sizeof(struct ifla_vf_spoofchk) },
1116 nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1117 &vf_tx_rate) ||
1118 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1119 &vf_spoofchk) ||
1120 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1121 &vf_linkstate))
1122 goto nla_put_failure;
1123 nla_nest_end(skb, vf);

--- 90 unchanged lines hidden (view full) ---

1214 [IFLA_VF_MAC] = { .type = NLA_BINARY,
1215 .len = sizeof(struct ifla_vf_mac) },
1216 [IFLA_VF_VLAN] = { .type = NLA_BINARY,
1217 .len = sizeof(struct ifla_vf_vlan) },
1218 [IFLA_VF_TX_RATE] = { .type = NLA_BINARY,
1219 .len = sizeof(struct ifla_vf_tx_rate) },
1220 [IFLA_VF_SPOOFCHK] = { .type = NLA_BINARY,
1221 .len = sizeof(struct ifla_vf_spoofchk) },
1222 [IFLA_VF_RATE] = { .type = NLA_BINARY,
1223 .len = sizeof(struct ifla_vf_rate) },
1224 [IFLA_VF_LINK_STATE] = { .type = NLA_BINARY,
1225 .len = sizeof(struct ifla_vf_link_state) },
1211};
1212
1213static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1214 [IFLA_PORT_VF] = { .type = NLA_U32 },
1215 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
1216 .len = PORT_PROFILE_MAX },
1217 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
1218 .len = sizeof(struct ifla_port_vsi)},

--- 10 unchanged lines hidden (view full) ---

1229 struct net *net = sock_net(skb->sk);
1230 int h, s_h;
1231 int idx = 0, s_idx;
1232 struct net_device *dev;
1233 struct hlist_head *head;
1234 struct nlattr *tb[IFLA_MAX+1];
1235 u32 ext_filter_mask = 0;
1236 int err;
1226};
1227
1228static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1229 [IFLA_PORT_VF] = { .type = NLA_U32 },
1230 [IFLA_PORT_PROFILE] = { .type = NLA_STRING,
1231 .len = PORT_PROFILE_MAX },
1232 [IFLA_PORT_VSI_TYPE] = { .type = NLA_BINARY,
1233 .len = sizeof(struct ifla_port_vsi)},

--- 10 unchanged lines hidden (view full) ---

1244 struct net *net = sock_net(skb->sk);
1245 int h, s_h;
1246 int idx = 0, s_idx;
1247 struct net_device *dev;
1248 struct hlist_head *head;
1249 struct nlattr *tb[IFLA_MAX+1];
1250 u32 ext_filter_mask = 0;
1251 int err;
1252 int hdrlen;
1237
1238 s_h = cb->args[0];
1239 s_idx = cb->args[1];
1240
1241 rcu_read_lock();
1242 cb->seq = net->dev_base_seq;
1243
1253
1254 s_h = cb->args[0];
1255 s_idx = cb->args[1];
1256
1257 rcu_read_lock();
1258 cb->seq = net->dev_base_seq;
1259
1244 if (nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX,
1245 ifla_policy) >= 0) {
1260 /* A hack to preserve kernel<->userspace interface.
1261 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1262 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1263 * what iproute2 < v3.9.0 used.
1264 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1265 * attribute, its netlink message is shorter than struct ifinfomsg.
1266 */
1267 hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ?
1268 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1246
1269
1270 if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) {
1271
1247 if (tb[IFLA_EXT_MASK])
1248 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1249 }
1250
1251 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1252 idx = 0;
1253 head = &net->dev_index_head[h];
1254 hlist_for_each_entry_rcu(dev, head, index_hlist) {

--- 107 unchanged lines hidden (view full) ---

1362 if (ops->ndo_set_vf_vlan)
1363 err = ops->ndo_set_vf_vlan(dev, ivv->vf,
1364 ivv->vlan,
1365 ivv->qos);
1366 break;
1367 }
1368 case IFLA_VF_TX_RATE: {
1369 struct ifla_vf_tx_rate *ivt;
1272 if (tb[IFLA_EXT_MASK])
1273 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1274 }
1275
1276 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1277 idx = 0;
1278 head = &net->dev_index_head[h];
1279 hlist_for_each_entry_rcu(dev, head, index_hlist) {

--- 107 unchanged lines hidden (view full) ---

1387 if (ops->ndo_set_vf_vlan)
1388 err = ops->ndo_set_vf_vlan(dev, ivv->vf,
1389 ivv->vlan,
1390 ivv->qos);
1391 break;
1392 }
1393 case IFLA_VF_TX_RATE: {
1394 struct ifla_vf_tx_rate *ivt;
1395 struct ifla_vf_info ivf;
1370 ivt = nla_data(vf);
1371 err = -EOPNOTSUPP;
1396 ivt = nla_data(vf);
1397 err = -EOPNOTSUPP;
1372 if (ops->ndo_set_vf_tx_rate)
1373 err = ops->ndo_set_vf_tx_rate(dev, ivt->vf,
1374 ivt->rate);
1398 if (ops->ndo_get_vf_config)
1399 err = ops->ndo_get_vf_config(dev, ivt->vf,
1400 &ivf);
1401 if (err)
1402 break;
1403 err = -EOPNOTSUPP;
1404 if (ops->ndo_set_vf_rate)
1405 err = ops->ndo_set_vf_rate(dev, ivt->vf,
1406 ivf.min_tx_rate,
1407 ivt->rate);
1375 break;
1376 }
1408 break;
1409 }
1410 case IFLA_VF_RATE: {
1411 struct ifla_vf_rate *ivt;
1412 ivt = nla_data(vf);
1413 err = -EOPNOTSUPP;
1414 if (ops->ndo_set_vf_rate)
1415 err = ops->ndo_set_vf_rate(dev, ivt->vf,
1416 ivt->min_tx_rate,
1417 ivt->max_tx_rate);
1418 break;
1419 }
1377 case IFLA_VF_SPOOFCHK: {
1378 struct ifla_vf_spoofchk *ivs;
1379 ivs = nla_data(vf);
1380 err = -EOPNOTSUPP;
1381 if (ops->ndo_set_vf_spoofchk)
1382 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
1383 ivs->setting);
1384 break;

--- 349 unchanged lines hidden (view full) ---

1734 dev = __dev_get_by_name(net, ifname);
1735 else
1736 return -EINVAL;
1737
1738 if (!dev)
1739 return -ENODEV;
1740
1741 ops = dev->rtnl_link_ops;
1420 case IFLA_VF_SPOOFCHK: {
1421 struct ifla_vf_spoofchk *ivs;
1422 ivs = nla_data(vf);
1423 err = -EOPNOTSUPP;
1424 if (ops->ndo_set_vf_spoofchk)
1425 err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
1426 ivs->setting);
1427 break;

--- 349 unchanged lines hidden (view full) ---

1777 dev = __dev_get_by_name(net, ifname);
1778 else
1779 return -EINVAL;
1780
1781 if (!dev)
1782 return -ENODEV;
1783
1784 ops = dev->rtnl_link_ops;
1742 if (!ops)
1785 if (!ops || !ops->dellink)
1743 return -EOPNOTSUPP;
1744
1745 ops->dellink(dev, &list_kill);
1746 unregister_netdevice_many(&list_kill);
1786 return -EOPNOTSUPP;
1787
1788 ops->dellink(dev, &list_kill);
1789 unregister_netdevice_many(&list_kill);
1747 list_del(&list_kill);
1748 return 0;
1749}
1750
1751int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
1752{
1753 unsigned int old_flags;
1754 int err;
1755

--- 240 unchanged lines hidden (view full) ---

1996 ops = rtnl_link_ops_get(kind);
1997 if (ops)
1998 goto replay;
1999 }
2000#endif
2001 return -EOPNOTSUPP;
2002 }
2003
1790 return 0;
1791}
1792
1793int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
1794{
1795 unsigned int old_flags;
1796 int err;
1797

--- 240 unchanged lines hidden (view full) ---

2038 ops = rtnl_link_ops_get(kind);
2039 if (ops)
2040 goto replay;
2041 }
2042#endif
2043 return -EOPNOTSUPP;
2044 }
2045
2046 if (!ops->setup)
2047 return -EOPNOTSUPP;
2048
2004 if (!ifname[0])
2005 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
2006
2007 dest_net = rtnl_link_get_net(net, tb);
2008 if (IS_ERR(dest_net))
2009 return PTR_ERR(dest_net);
2010
2011 dev = rtnl_create_link(dest_net, ifname, ops, tb);
2012 if (IS_ERR(dev)) {
2013 err = PTR_ERR(dev);
2014 goto out;
2015 }
2016
2017 dev->ifindex = ifm->ifi_index;
2018
2019 if (ops->newlink) {
2020 err = ops->newlink(net, dev, tb, data);
2021 /* Drivers should call free_netdev() in ->destructor
2049 if (!ifname[0])
2050 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
2051
2052 dest_net = rtnl_link_get_net(net, tb);
2053 if (IS_ERR(dest_net))
2054 return PTR_ERR(dest_net);
2055
2056 dev = rtnl_create_link(dest_net, ifname, ops, tb);
2057 if (IS_ERR(dev)) {
2058 err = PTR_ERR(dev);
2059 goto out;
2060 }
2061
2062 dev->ifindex = ifm->ifi_index;
2063
2064 if (ops->newlink) {
2065 err = ops->newlink(net, dev, tb, data);
2066 /* Drivers should call free_netdev() in ->destructor
2022 * and unregister it on failure so that device could be
2023 * finally freed in rtnl_unlock.
2067 * and unregister it on failure after registration
2068 * so that device could be finally freed in rtnl_unlock.
2024 */
2069 */
2025 if (err < 0)
2070 if (err < 0) {
2071 /* If device is not registered at all, free it now */
2072 if (dev->reg_state == NETREG_UNINITIALIZED)
2073 free_netdev(dev);
2026 goto out;
2074 goto out;
2075 }
2027 } else {
2028 err = register_netdevice(dev);
2029 if (err < 0) {
2030 free_netdev(dev);
2031 goto out;
2032 }
2033 }
2034 err = rtnl_configure_link(dev, ifm);

--- 55 unchanged lines hidden (view full) ---

2090
2091static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
2092{
2093 struct net *net = sock_net(skb->sk);
2094 struct net_device *dev;
2095 struct nlattr *tb[IFLA_MAX+1];
2096 u32 ext_filter_mask = 0;
2097 u16 min_ifinfo_dump_size = 0;
2076 } else {
2077 err = register_netdevice(dev);
2078 if (err < 0) {
2079 free_netdev(dev);
2080 goto out;
2081 }
2082 }
2083 err = rtnl_configure_link(dev, ifm);

--- 55 unchanged lines hidden (view full) ---

2139
2140static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
2141{
2142 struct net *net = sock_net(skb->sk);
2143 struct net_device *dev;
2144 struct nlattr *tb[IFLA_MAX+1];
2145 u32 ext_filter_mask = 0;
2146 u16 min_ifinfo_dump_size = 0;
2147 int hdrlen;
2098
2148
2099 if (nlmsg_parse(nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX,
2100 ifla_policy) >= 0) {
2149 /* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
2150 hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
2151 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
2152
2153 if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) {
2101 if (tb[IFLA_EXT_MASK])
2102 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2103 }
2104
2105 if (!ext_filter_mask)
2106 return NLMSG_GOODSIZE;
2107 /*
2108 * traverse the list of net devices and compute the minimum

--- 837 unchanged lines hidden ---
2154 if (tb[IFLA_EXT_MASK])
2155 ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2156 }
2157
2158 if (!ext_filter_mask)
2159 return NLMSG_GOODSIZE;
2160 /*
2161 * traverse the list of net devices and compute the minimum

--- 837 unchanged lines hidden ---