Skip to content

Commit

Permalink
Merge branch 'cls_flower-Use-extack-in-fl_set_key'
Browse files Browse the repository at this point in the history
Guillaume Nault says:

====================
cls_flower: Use extack in fl_set_key()

Add missing extack messages in fl_set_key(), so that users can get more
meaningfull error messages when netlink attributes are rejected.

Patch 1 also extends extack in tcf_change_indev() (in pkt_cls.h) since
this function is used by fl_set_key().
====================

Signed-off-by: David S. Miller <[email protected]>
  • Loading branch information
davem330 committed Mar 27, 2020
2 parents 6a86473 + e304e21 commit 5bb7357
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 19 deletions.
8 changes: 6 additions & 2 deletions include/net/pkt_cls.h
Original file line number Diff line number Diff line change
Expand Up @@ -502,12 +502,16 @@ tcf_change_indev(struct net *net, struct nlattr *indev_tlv,
struct net_device *dev;

if (nla_strlcpy(indev, indev_tlv, IFNAMSIZ) >= IFNAMSIZ) {
NL_SET_ERR_MSG(extack, "Interface name too long");
NL_SET_ERR_MSG_ATTR(extack, indev_tlv,
"Interface name too long");
return -EINVAL;
}
dev = __dev_get_by_name(net, indev);
if (!dev)
if (!dev) {
NL_SET_ERR_MSG_ATTR(extack, indev_tlv,
"Network device not found");
return -ENODEV;
}
return dev->ifindex;
}

Expand Down
60 changes: 43 additions & 17 deletions net/sched/cls_flower.c
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,8 @@ static void fl_set_key_val(struct nlattr **tb,
}

static int fl_set_key_port_range(struct nlattr **tb, struct fl_flow_key *key,
struct fl_flow_key *mask)
struct fl_flow_key *mask,
struct netlink_ext_ack *extack)
{
fl_set_key_val(tb, &key->tp_range.tp_min.dst,
TCA_FLOWER_KEY_PORT_DST_MIN, &mask->tp_range.tp_min.dst,
Expand All @@ -753,20 +754,30 @@ static int fl_set_key_port_range(struct nlattr **tb, struct fl_flow_key *key,
TCA_FLOWER_KEY_PORT_SRC_MAX, &mask->tp_range.tp_max.src,
TCA_FLOWER_UNSPEC, sizeof(key->tp_range.tp_max.src));

if ((mask->tp_range.tp_min.dst && mask->tp_range.tp_max.dst &&
htons(key->tp_range.tp_max.dst) <=
htons(key->tp_range.tp_min.dst)) ||
(mask->tp_range.tp_min.src && mask->tp_range.tp_max.src &&
htons(key->tp_range.tp_max.src) <=
htons(key->tp_range.tp_min.src)))
if (mask->tp_range.tp_min.dst && mask->tp_range.tp_max.dst &&
htons(key->tp_range.tp_max.dst) <=
htons(key->tp_range.tp_min.dst)) {
NL_SET_ERR_MSG_ATTR(extack,
tb[TCA_FLOWER_KEY_PORT_DST_MIN],
"Invalid destination port range (min must be strictly smaller than max)");
return -EINVAL;
}
if (mask->tp_range.tp_min.src && mask->tp_range.tp_max.src &&
htons(key->tp_range.tp_max.src) <=
htons(key->tp_range.tp_min.src)) {
NL_SET_ERR_MSG_ATTR(extack,
tb[TCA_FLOWER_KEY_PORT_SRC_MIN],
"Invalid source port range (min must be strictly smaller than max)");
return -EINVAL;
}

return 0;
}

static int fl_set_key_mpls(struct nlattr **tb,
struct flow_dissector_key_mpls *key_val,
struct flow_dissector_key_mpls *key_mask)
struct flow_dissector_key_mpls *key_mask,
struct netlink_ext_ack *extack)
{
if (tb[TCA_FLOWER_KEY_MPLS_TTL]) {
key_val->mpls_ttl = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TTL]);
Expand All @@ -775,24 +786,36 @@ static int fl_set_key_mpls(struct nlattr **tb,
if (tb[TCA_FLOWER_KEY_MPLS_BOS]) {
u8 bos = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_BOS]);

if (bos & ~MPLS_BOS_MASK)
if (bos & ~MPLS_BOS_MASK) {
NL_SET_ERR_MSG_ATTR(extack,
tb[TCA_FLOWER_KEY_MPLS_BOS],
"Bottom Of Stack (BOS) must be 0 or 1");
return -EINVAL;
}
key_val->mpls_bos = bos;
key_mask->mpls_bos = MPLS_BOS_MASK;
}
if (tb[TCA_FLOWER_KEY_MPLS_TC]) {
u8 tc = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TC]);

if (tc & ~MPLS_TC_MASK)
if (tc & ~MPLS_TC_MASK) {
NL_SET_ERR_MSG_ATTR(extack,
tb[TCA_FLOWER_KEY_MPLS_TC],
"Traffic Class (TC) must be between 0 and 7");
return -EINVAL;
}
key_val->mpls_tc = tc;
key_mask->mpls_tc = MPLS_TC_MASK;
}
if (tb[TCA_FLOWER_KEY_MPLS_LABEL]) {
u32 label = nla_get_u32(tb[TCA_FLOWER_KEY_MPLS_LABEL]);

if (label & ~MPLS_LABEL_MASK)
if (label & ~MPLS_LABEL_MASK) {
NL_SET_ERR_MSG_ATTR(extack,
tb[TCA_FLOWER_KEY_MPLS_LABEL],
"Label must be between 0 and 1048575");
return -EINVAL;
}
key_val->mpls_label = label;
key_mask->mpls_label = MPLS_LABEL_MASK;
}
Expand Down Expand Up @@ -833,14 +856,16 @@ static void fl_set_key_flag(u32 flower_key, u32 flower_mask,
}
}

static int fl_set_key_flags(struct nlattr **tb,
u32 *flags_key, u32 *flags_mask)
static int fl_set_key_flags(struct nlattr **tb, u32 *flags_key,
u32 *flags_mask, struct netlink_ext_ack *extack)
{
u32 key, mask;

/* mask is mandatory for flags */
if (!tb[TCA_FLOWER_KEY_FLAGS_MASK])
if (!tb[TCA_FLOWER_KEY_FLAGS_MASK]) {
NL_SET_ERR_MSG(extack, "Missing flags mask");
return -EINVAL;
}

key = be32_to_cpu(nla_get_u32(tb[TCA_FLOWER_KEY_FLAGS]));
mask = be32_to_cpu(nla_get_u32(tb[TCA_FLOWER_KEY_FLAGS_MASK]));
Expand Down Expand Up @@ -1364,7 +1389,7 @@ static int fl_set_key(struct net *net, struct nlattr **tb,
sizeof(key->icmp.code));
} else if (key->basic.n_proto == htons(ETH_P_MPLS_UC) ||
key->basic.n_proto == htons(ETH_P_MPLS_MC)) {
ret = fl_set_key_mpls(tb, &key->mpls, &mask->mpls);
ret = fl_set_key_mpls(tb, &key->mpls, &mask->mpls, extack);
if (ret)
return ret;
} else if (key->basic.n_proto == htons(ETH_P_ARP) ||
Expand All @@ -1389,7 +1414,7 @@ static int fl_set_key(struct net *net, struct nlattr **tb,
if (key->basic.ip_proto == IPPROTO_TCP ||
key->basic.ip_proto == IPPROTO_UDP ||
key->basic.ip_proto == IPPROTO_SCTP) {
ret = fl_set_key_port_range(tb, key, mask);
ret = fl_set_key_port_range(tb, key, mask, extack);
if (ret)
return ret;
}
Expand Down Expand Up @@ -1451,7 +1476,8 @@ static int fl_set_key(struct net *net, struct nlattr **tb,
return ret;

if (tb[TCA_FLOWER_KEY_FLAGS])
ret = fl_set_key_flags(tb, &key->control.flags, &mask->control.flags);
ret = fl_set_key_flags(tb, &key->control.flags,
&mask->control.flags, extack);

return ret;
}
Expand Down

0 comments on commit 5bb7357

Please sign in to comment.