Skip to content

Commit 52e12d5

Browse files
0x7f454c46davem330
authored andcommitted
pktgen: Clean read user supplied flag mess
Don't use error-prone-brute-force way. Signed-off-by: Dmitry Safonov <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 99c6d3d commit 52e12d5

File tree

1 file changed

+39
-105
lines changed

1 file changed

+39
-105
lines changed

net/core/pktgen.c

Lines changed: 39 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,35 @@ static ssize_t get_labels(const char __user *buffer, struct pktgen_dev *pkt_dev)
830830
return i;
831831
}
832832

833+
static __u32 pktgen_read_flag(const char *f, bool *disable)
834+
{
835+
__u32 i;
836+
837+
if (f[0] == '!') {
838+
*disable = true;
839+
f++;
840+
}
841+
842+
for (i = 0; i < NR_PKT_FLAGS; i++) {
843+
if (!IS_ENABLED(CONFIG_XFRM) && i == IPSEC_SHIFT)
844+
continue;
845+
846+
/* allow only disabling ipv6 flag */
847+
if (!*disable && i == IPV6_SHIFT)
848+
continue;
849+
850+
if (strcmp(f, pkt_flag_names[i]) == 0)
851+
return 1 << i;
852+
}
853+
854+
if (strcmp(f, "FLOW_RND") == 0) {
855+
*disable = !*disable;
856+
return F_FLOW_SEQ;
857+
}
858+
859+
return 0;
860+
}
861+
833862
static ssize_t pktgen_if_write(struct file *file,
834863
const char __user * user_buffer, size_t count,
835864
loff_t * offset)
@@ -1187,7 +1216,10 @@ static ssize_t pktgen_if_write(struct file *file,
11871216
return count;
11881217
}
11891218
if (!strcmp(name, "flag")) {
1219+
__u32 flag;
11901220
char f[32];
1221+
bool disable = false;
1222+
11911223
memset(f, 0, 32);
11921224
len = strn_len(&user_buffer[i], sizeof(f) - 1);
11931225
if (len < 0)
@@ -1196,113 +1228,15 @@ static ssize_t pktgen_if_write(struct file *file,
11961228
if (copy_from_user(f, &user_buffer[i], len))
11971229
return -EFAULT;
11981230
i += len;
1199-
if (strcmp(f, "IPSRC_RND") == 0)
1200-
pkt_dev->flags |= F_IPSRC_RND;
1201-
1202-
else if (strcmp(f, "!IPSRC_RND") == 0)
1203-
pkt_dev->flags &= ~F_IPSRC_RND;
1204-
1205-
else if (strcmp(f, "TXSIZE_RND") == 0)
1206-
pkt_dev->flags |= F_TXSIZE_RND;
1207-
1208-
else if (strcmp(f, "!TXSIZE_RND") == 0)
1209-
pkt_dev->flags &= ~F_TXSIZE_RND;
1210-
1211-
else if (strcmp(f, "IPDST_RND") == 0)
1212-
pkt_dev->flags |= F_IPDST_RND;
1213-
1214-
else if (strcmp(f, "!IPDST_RND") == 0)
1215-
pkt_dev->flags &= ~F_IPDST_RND;
1216-
1217-
else if (strcmp(f, "UDPSRC_RND") == 0)
1218-
pkt_dev->flags |= F_UDPSRC_RND;
1219-
1220-
else if (strcmp(f, "!UDPSRC_RND") == 0)
1221-
pkt_dev->flags &= ~F_UDPSRC_RND;
1222-
1223-
else if (strcmp(f, "UDPDST_RND") == 0)
1224-
pkt_dev->flags |= F_UDPDST_RND;
1225-
1226-
else if (strcmp(f, "!UDPDST_RND") == 0)
1227-
pkt_dev->flags &= ~F_UDPDST_RND;
1228-
1229-
else if (strcmp(f, "MACSRC_RND") == 0)
1230-
pkt_dev->flags |= F_MACSRC_RND;
1231-
1232-
else if (strcmp(f, "!MACSRC_RND") == 0)
1233-
pkt_dev->flags &= ~F_MACSRC_RND;
12341231

1235-
else if (strcmp(f, "MACDST_RND") == 0)
1236-
pkt_dev->flags |= F_MACDST_RND;
1232+
flag = pktgen_read_flag(f, &disable);
12371233

1238-
else if (strcmp(f, "!MACDST_RND") == 0)
1239-
pkt_dev->flags &= ~F_MACDST_RND;
1240-
1241-
else if (strcmp(f, "MPLS_RND") == 0)
1242-
pkt_dev->flags |= F_MPLS_RND;
1243-
1244-
else if (strcmp(f, "!MPLS_RND") == 0)
1245-
pkt_dev->flags &= ~F_MPLS_RND;
1246-
1247-
else if (strcmp(f, "VID_RND") == 0)
1248-
pkt_dev->flags |= F_VID_RND;
1249-
1250-
else if (strcmp(f, "!VID_RND") == 0)
1251-
pkt_dev->flags &= ~F_VID_RND;
1252-
1253-
else if (strcmp(f, "SVID_RND") == 0)
1254-
pkt_dev->flags |= F_SVID_RND;
1255-
1256-
else if (strcmp(f, "!SVID_RND") == 0)
1257-
pkt_dev->flags &= ~F_SVID_RND;
1258-
1259-
else if (strcmp(f, "FLOW_SEQ") == 0 || strcmp(f, "!FLOW_RND") == 0)
1260-
pkt_dev->flags |= F_FLOW_SEQ;
1261-
1262-
else if (strcmp(f, "FLOW_RND") == 0 || strcmp(f, "!FLOW_SEQ") == 0)
1263-
pkt_dev->flags &= ~F_FLOW_SEQ;
1264-
1265-
else if (strcmp(f, "QUEUE_MAP_RND") == 0)
1266-
pkt_dev->flags |= F_QUEUE_MAP_RND;
1267-
1268-
else if (strcmp(f, "!QUEUE_MAP_RND") == 0)
1269-
pkt_dev->flags &= ~F_QUEUE_MAP_RND;
1270-
1271-
else if (strcmp(f, "QUEUE_MAP_CPU") == 0)
1272-
pkt_dev->flags |= F_QUEUE_MAP_CPU;
1273-
1274-
else if (strcmp(f, "!QUEUE_MAP_CPU") == 0)
1275-
pkt_dev->flags &= ~F_QUEUE_MAP_CPU;
1276-
#ifdef CONFIG_XFRM
1277-
else if (strcmp(f, "IPSEC") == 0)
1278-
pkt_dev->flags |= F_IPSEC;
1279-
1280-
else if (strcmp(f, "!IPSEC") == 0)
1281-
pkt_dev->flags &= ~F_IPSEC;
1282-
#endif
1283-
1284-
else if (strcmp(f, "!IPV6") == 0)
1285-
pkt_dev->flags &= ~F_IPV6;
1286-
1287-
else if (strcmp(f, "NODE_ALLOC") == 0)
1288-
pkt_dev->flags |= F_NODE;
1289-
1290-
else if (strcmp(f, "!NODE_ALLOC") == 0)
1291-
pkt_dev->flags &= ~F_NODE;
1292-
1293-
else if (strcmp(f, "UDPCSUM") == 0)
1294-
pkt_dev->flags |= F_UDPCSUM;
1295-
1296-
else if (strcmp(f, "!UDPCSUM") == 0)
1297-
pkt_dev->flags &= ~F_UDPCSUM;
1298-
1299-
else if (strcmp(f, "NO_TIMESTAMP") == 0)
1300-
pkt_dev->flags |= F_NO_TIMESTAMP;
1301-
1302-
else if (strcmp(f, "!NO_TIMESTAMP") == 0)
1303-
pkt_dev->flags &= ~F_NO_TIMESTAMP;
1304-
1305-
else {
1234+
if (flag) {
1235+
if (disable)
1236+
pkt_dev->flags &= ~flag;
1237+
else
1238+
pkt_dev->flags |= flag;
1239+
} else {
13061240
sprintf(pg_result,
13071241
"Flag -:%s:- unknown\nAvailable flags, (prepend ! to un-set flag):\n%s",
13081242
f,

0 commit comments

Comments
 (0)