Skip to content

Commit 5bb7357

Browse files
committed
Merge branch 'cls_flower-Use-extack-in-fl_set_key'
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]>
2 parents 6a86473 + e304e21 commit 5bb7357

File tree

2 files changed

+49
-19
lines changed

2 files changed

+49
-19
lines changed

include/net/pkt_cls.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -502,12 +502,16 @@ tcf_change_indev(struct net *net, struct nlattr *indev_tlv,
502502
struct net_device *dev;
503503

504504
if (nla_strlcpy(indev, indev_tlv, IFNAMSIZ) >= IFNAMSIZ) {
505-
NL_SET_ERR_MSG(extack, "Interface name too long");
505+
NL_SET_ERR_MSG_ATTR(extack, indev_tlv,
506+
"Interface name too long");
506507
return -EINVAL;
507508
}
508509
dev = __dev_get_by_name(net, indev);
509-
if (!dev)
510+
if (!dev) {
511+
NL_SET_ERR_MSG_ATTR(extack, indev_tlv,
512+
"Network device not found");
510513
return -ENODEV;
514+
}
511515
return dev->ifindex;
512516
}
513517

net/sched/cls_flower.c

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,8 @@ static void fl_set_key_val(struct nlattr **tb,
738738
}
739739

740740
static int fl_set_key_port_range(struct nlattr **tb, struct fl_flow_key *key,
741-
struct fl_flow_key *mask)
741+
struct fl_flow_key *mask,
742+
struct netlink_ext_ack *extack)
742743
{
743744
fl_set_key_val(tb, &key->tp_range.tp_min.dst,
744745
TCA_FLOWER_KEY_PORT_DST_MIN, &mask->tp_range.tp_min.dst,
@@ -753,20 +754,30 @@ static int fl_set_key_port_range(struct nlattr **tb, struct fl_flow_key *key,
753754
TCA_FLOWER_KEY_PORT_SRC_MAX, &mask->tp_range.tp_max.src,
754755
TCA_FLOWER_UNSPEC, sizeof(key->tp_range.tp_max.src));
755756

756-
if ((mask->tp_range.tp_min.dst && mask->tp_range.tp_max.dst &&
757-
htons(key->tp_range.tp_max.dst) <=
758-
htons(key->tp_range.tp_min.dst)) ||
759-
(mask->tp_range.tp_min.src && mask->tp_range.tp_max.src &&
760-
htons(key->tp_range.tp_max.src) <=
761-
htons(key->tp_range.tp_min.src)))
757+
if (mask->tp_range.tp_min.dst && mask->tp_range.tp_max.dst &&
758+
htons(key->tp_range.tp_max.dst) <=
759+
htons(key->tp_range.tp_min.dst)) {
760+
NL_SET_ERR_MSG_ATTR(extack,
761+
tb[TCA_FLOWER_KEY_PORT_DST_MIN],
762+
"Invalid destination port range (min must be strictly smaller than max)");
762763
return -EINVAL;
764+
}
765+
if (mask->tp_range.tp_min.src && mask->tp_range.tp_max.src &&
766+
htons(key->tp_range.tp_max.src) <=
767+
htons(key->tp_range.tp_min.src)) {
768+
NL_SET_ERR_MSG_ATTR(extack,
769+
tb[TCA_FLOWER_KEY_PORT_SRC_MIN],
770+
"Invalid source port range (min must be strictly smaller than max)");
771+
return -EINVAL;
772+
}
763773

764774
return 0;
765775
}
766776

767777
static int fl_set_key_mpls(struct nlattr **tb,
768778
struct flow_dissector_key_mpls *key_val,
769-
struct flow_dissector_key_mpls *key_mask)
779+
struct flow_dissector_key_mpls *key_mask,
780+
struct netlink_ext_ack *extack)
770781
{
771782
if (tb[TCA_FLOWER_KEY_MPLS_TTL]) {
772783
key_val->mpls_ttl = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TTL]);
@@ -775,24 +786,36 @@ static int fl_set_key_mpls(struct nlattr **tb,
775786
if (tb[TCA_FLOWER_KEY_MPLS_BOS]) {
776787
u8 bos = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_BOS]);
777788

778-
if (bos & ~MPLS_BOS_MASK)
789+
if (bos & ~MPLS_BOS_MASK) {
790+
NL_SET_ERR_MSG_ATTR(extack,
791+
tb[TCA_FLOWER_KEY_MPLS_BOS],
792+
"Bottom Of Stack (BOS) must be 0 or 1");
779793
return -EINVAL;
794+
}
780795
key_val->mpls_bos = bos;
781796
key_mask->mpls_bos = MPLS_BOS_MASK;
782797
}
783798
if (tb[TCA_FLOWER_KEY_MPLS_TC]) {
784799
u8 tc = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TC]);
785800

786-
if (tc & ~MPLS_TC_MASK)
801+
if (tc & ~MPLS_TC_MASK) {
802+
NL_SET_ERR_MSG_ATTR(extack,
803+
tb[TCA_FLOWER_KEY_MPLS_TC],
804+
"Traffic Class (TC) must be between 0 and 7");
787805
return -EINVAL;
806+
}
788807
key_val->mpls_tc = tc;
789808
key_mask->mpls_tc = MPLS_TC_MASK;
790809
}
791810
if (tb[TCA_FLOWER_KEY_MPLS_LABEL]) {
792811
u32 label = nla_get_u32(tb[TCA_FLOWER_KEY_MPLS_LABEL]);
793812

794-
if (label & ~MPLS_LABEL_MASK)
813+
if (label & ~MPLS_LABEL_MASK) {
814+
NL_SET_ERR_MSG_ATTR(extack,
815+
tb[TCA_FLOWER_KEY_MPLS_LABEL],
816+
"Label must be between 0 and 1048575");
795817
return -EINVAL;
818+
}
796819
key_val->mpls_label = label;
797820
key_mask->mpls_label = MPLS_LABEL_MASK;
798821
}
@@ -833,14 +856,16 @@ static void fl_set_key_flag(u32 flower_key, u32 flower_mask,
833856
}
834857
}
835858

836-
static int fl_set_key_flags(struct nlattr **tb,
837-
u32 *flags_key, u32 *flags_mask)
859+
static int fl_set_key_flags(struct nlattr **tb, u32 *flags_key,
860+
u32 *flags_mask, struct netlink_ext_ack *extack)
838861
{
839862
u32 key, mask;
840863

841864
/* mask is mandatory for flags */
842-
if (!tb[TCA_FLOWER_KEY_FLAGS_MASK])
865+
if (!tb[TCA_FLOWER_KEY_FLAGS_MASK]) {
866+
NL_SET_ERR_MSG(extack, "Missing flags mask");
843867
return -EINVAL;
868+
}
844869

845870
key = be32_to_cpu(nla_get_u32(tb[TCA_FLOWER_KEY_FLAGS]));
846871
mask = be32_to_cpu(nla_get_u32(tb[TCA_FLOWER_KEY_FLAGS_MASK]));
@@ -1364,7 +1389,7 @@ static int fl_set_key(struct net *net, struct nlattr **tb,
13641389
sizeof(key->icmp.code));
13651390
} else if (key->basic.n_proto == htons(ETH_P_MPLS_UC) ||
13661391
key->basic.n_proto == htons(ETH_P_MPLS_MC)) {
1367-
ret = fl_set_key_mpls(tb, &key->mpls, &mask->mpls);
1392+
ret = fl_set_key_mpls(tb, &key->mpls, &mask->mpls, extack);
13681393
if (ret)
13691394
return ret;
13701395
} else if (key->basic.n_proto == htons(ETH_P_ARP) ||
@@ -1389,7 +1414,7 @@ static int fl_set_key(struct net *net, struct nlattr **tb,
13891414
if (key->basic.ip_proto == IPPROTO_TCP ||
13901415
key->basic.ip_proto == IPPROTO_UDP ||
13911416
key->basic.ip_proto == IPPROTO_SCTP) {
1392-
ret = fl_set_key_port_range(tb, key, mask);
1417+
ret = fl_set_key_port_range(tb, key, mask, extack);
13931418
if (ret)
13941419
return ret;
13951420
}
@@ -1451,7 +1476,8 @@ static int fl_set_key(struct net *net, struct nlattr **tb,
14511476
return ret;
14521477

14531478
if (tb[TCA_FLOWER_KEY_FLAGS])
1454-
ret = fl_set_key_flags(tb, &key->control.flags, &mask->control.flags);
1479+
ret = fl_set_key_flags(tb, &key->control.flags,
1480+
&mask->control.flags, extack);
14551481

14561482
return ret;
14571483
}

0 commit comments

Comments
 (0)