Skip to content

Commit cf7afbf

Browse files
Thomas Grafdavem330
Thomas Graf
authored andcommitted
rtnl: make link af-specific updates atomic
As David pointed out correctly, updates to af-specific attributes are currently not atomic. If multiple changes are requested and one of them fails, previous updates may have been applied already leaving the link behind in a undefined state. This patch splits the function parse_link_af() into two functions validate_link_af() and set_link_at(). validate_link_af() is placed to validate_linkmsg() check for errors as early as possible before any changes to the link have been made. set_link_af() is called to commit the changes later. This method is not fail proof, while it is currently sufficient to make set_link_af() inerrable and thus 100% atomic, the validation function method will not be able to detect all error scenarios in the future, there will likely always be errors depending on states which are f.e. not protected by rtnl_mutex and thus may change between validation and setting. Also, instead of silently ignoring unknown address families and config blocks for address families which did not register a set function the errors EAFNOSUPPORT respectively EOPNOSUPPORT are returned to avoid comitting 4 out of 5 update requests without notifying the user. Signed-off-by: Thomas Graf <tgraf@infradead.org> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 89bf67f commit cf7afbf

File tree

4 files changed

+53
-20
lines changed

4 files changed

+53
-20
lines changed

include/net/rtnetlink.h

+8-4
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,10 @@ extern void rtnl_link_unregister(struct rtnl_link_ops *ops);
9292
* specific netlink attributes.
9393
* @get_link_af_size: Function to calculate size of address family specific
9494
* netlink attributes exlusive the container attribute.
95-
* @parse_link_af: Function to parse a IFLA_AF_SPEC attribute and modify
96-
* net_device accordingly.
95+
* @validate_link_af: Validate a IFLA_AF_SPEC attribute, must check attr
96+
* for invalid configuration settings.
97+
* @set_link_af: Function to parse a IFLA_AF_SPEC attribute and modify
98+
* net_device accordingly.
9799
*/
98100
struct rtnl_af_ops {
99101
struct list_head list;
@@ -103,8 +105,10 @@ struct rtnl_af_ops {
103105
const struct net_device *dev);
104106
size_t (*get_link_af_size)(const struct net_device *dev);
105107

106-
int (*parse_link_af)(struct net_device *dev,
107-
const struct nlattr *attr);
108+
int (*validate_link_af)(const struct net_device *dev,
109+
const struct nlattr *attr);
110+
int (*set_link_af)(struct net_device *dev,
111+
const struct nlattr *attr);
108112
};
109113

110114
extern int __rtnl_af_register(struct rtnl_af_ops *ops);

net/core/rtnetlink.c

+24-5
Original file line numberDiff line numberDiff line change
@@ -1107,6 +1107,28 @@ static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
11071107
return -EINVAL;
11081108
}
11091109

1110+
if (tb[IFLA_AF_SPEC]) {
1111+
struct nlattr *af;
1112+
int rem, err;
1113+
1114+
nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1115+
const struct rtnl_af_ops *af_ops;
1116+
1117+
if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1118+
return -EAFNOSUPPORT;
1119+
1120+
if (!af_ops->set_link_af)
1121+
return -EOPNOTSUPP;
1122+
1123+
if (af_ops->validate_link_af) {
1124+
err = af_ops->validate_link_af(dev,
1125+
tb[IFLA_AF_SPEC]);
1126+
if (err < 0)
1127+
return err;
1128+
}
1129+
}
1130+
}
1131+
11101132
return 0;
11111133
}
11121134

@@ -1356,12 +1378,9 @@ static int do_setlink(struct net_device *dev, struct ifinfomsg *ifm,
13561378
const struct rtnl_af_ops *af_ops;
13571379

13581380
if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1359-
continue;
1360-
1361-
if (!af_ops->parse_link_af)
1362-
continue;
1381+
BUG();
13631382

1364-
err = af_ops->parse_link_af(dev, af);
1383+
err = af_ops->set_link_af(dev, af);
13651384
if (err < 0)
13661385
goto errout;
13671386

net/ipv4/devinet.c

+21-5
Original file line numberDiff line numberDiff line change
@@ -1289,14 +1289,14 @@ static const struct nla_policy inet_af_policy[IFLA_INET_MAX+1] = {
12891289
[IFLA_INET_CONF] = { .type = NLA_NESTED },
12901290
};
12911291

1292-
static int inet_parse_link_af(struct net_device *dev, const struct nlattr *nla)
1292+
static int inet_validate_link_af(const struct net_device *dev,
1293+
const struct nlattr *nla)
12931294
{
1294-
struct in_device *in_dev = __in_dev_get_rcu(dev);
12951295
struct nlattr *a, *tb[IFLA_INET_MAX+1];
12961296
int err, rem;
12971297

1298-
if (!in_dev)
1299-
return -EOPNOTSUPP;
1298+
if (dev && !__in_dev_get_rcu(dev))
1299+
return -EAFNOSUPPORT;
13001300

13011301
err = nla_parse_nested(tb, IFLA_INET_MAX, nla, inet_af_policy);
13021302
if (err < 0)
@@ -1314,6 +1314,21 @@ static int inet_parse_link_af(struct net_device *dev, const struct nlattr *nla)
13141314
}
13151315
}
13161316

1317+
return 0;
1318+
}
1319+
1320+
static int inet_set_link_af(struct net_device *dev, const struct nlattr *nla)
1321+
{
1322+
struct in_device *in_dev = __in_dev_get_rcu(dev);
1323+
struct nlattr *a, *tb[IFLA_INET_MAX+1];
1324+
int rem;
1325+
1326+
if (!in_dev)
1327+
return -EAFNOSUPPORT;
1328+
1329+
if (nla_parse_nested(tb, IFLA_INET_MAX, nla, NULL) < 0)
1330+
BUG();
1331+
13171332
if (tb[IFLA_INET_CONF]) {
13181333
nla_for_each_nested(a, tb[IFLA_INET_CONF], rem)
13191334
ipv4_devconf_set(in_dev, nla_type(a), nla_get_u32(a));
@@ -1689,7 +1704,8 @@ static struct rtnl_af_ops inet_af_ops = {
16891704
.family = AF_INET,
16901705
.fill_link_af = inet_fill_link_af,
16911706
.get_link_af_size = inet_get_link_af_size,
1692-
.parse_link_af = inet_parse_link_af,
1707+
.validate_link_af = inet_validate_link_af,
1708+
.set_link_af = inet_set_link_af,
16931709
};
16941710

16951711
void __init devinet_init(void)

net/ipv6/addrconf.c

-6
Original file line numberDiff line numberDiff line change
@@ -3956,11 +3956,6 @@ static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev)
39563956
return 0;
39573957
}
39583958

3959-
static int inet6_parse_link_af(struct net_device *dev, const struct nlattr *nla)
3960-
{
3961-
return -EOPNOTSUPP;
3962-
}
3963-
39643959
static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
39653960
u32 pid, u32 seq, int event, unsigned int flags)
39663961
{
@@ -4670,7 +4665,6 @@ static struct rtnl_af_ops inet6_ops = {
46704665
.family = AF_INET6,
46714666
.fill_link_af = inet6_fill_link_af,
46724667
.get_link_af_size = inet6_get_link_af_size,
4673-
.parse_link_af = inet6_parse_link_af,
46744668
};
46754669

46764670
/*

0 commit comments

Comments
 (0)