Skip to content

Commit d018684

Browse files
ffainellidavem330
authored andcommitted
net: vlan: Avoid using BUG() in vlan_proto_idx()
While we should always make sure that we specify a valid VLAN protocol to vlan_proto_idx(), killing the machine when an invalid value is specified is too harsh and not helpful for debugging. All callers are capable of dealing with an error returned by vlan_proto_idx() so check the index value and propagate it accordingly. Signed-off-by: Florian Fainelli <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 7806f65 commit d018684

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

net/8021q/vlan.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ static int vlan_group_prealloc_vid(struct vlan_group *vg,
5757
ASSERT_RTNL();
5858

5959
pidx = vlan_proto_idx(vlan_proto);
60+
if (pidx < 0)
61+
return -EINVAL;
62+
6063
vidx = vlan_id / VLAN_GROUP_ARRAY_PART_LEN;
6164
array = vg->vlan_devices_arrays[pidx][vidx];
6265
if (array != NULL)

net/8021q/vlan.h

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,16 @@ struct vlan_info {
3636
struct rcu_head rcu;
3737
};
3838

39-
static inline unsigned int vlan_proto_idx(__be16 proto)
39+
static inline int vlan_proto_idx(__be16 proto)
4040
{
4141
switch (proto) {
4242
case htons(ETH_P_8021Q):
4343
return VLAN_PROTO_8021Q;
4444
case htons(ETH_P_8021AD):
4545
return VLAN_PROTO_8021AD;
4646
default:
47-
BUG();
48-
return 0;
47+
WARN(1, "invalid VLAN protocol: 0x%04x\n", ntohs(proto));
48+
return -EINVAL;
4949
}
5050
}
5151

@@ -64,17 +64,24 @@ static inline struct net_device *vlan_group_get_device(struct vlan_group *vg,
6464
__be16 vlan_proto,
6565
u16 vlan_id)
6666
{
67-
return __vlan_group_get_device(vg, vlan_proto_idx(vlan_proto), vlan_id);
67+
int pidx = vlan_proto_idx(vlan_proto);
68+
69+
if (pidx < 0)
70+
return NULL;
71+
72+
return __vlan_group_get_device(vg, pidx, vlan_id);
6873
}
6974

7075
static inline void vlan_group_set_device(struct vlan_group *vg,
7176
__be16 vlan_proto, u16 vlan_id,
7277
struct net_device *dev)
7378
{
79+
int pidx = vlan_proto_idx(vlan_proto);
7480
struct net_device **array;
75-
if (!vg)
81+
82+
if (!vg || pidx < 0)
7683
return;
77-
array = vg->vlan_devices_arrays[vlan_proto_idx(vlan_proto)]
84+
array = vg->vlan_devices_arrays[pidx]
7885
[vlan_id / VLAN_GROUP_ARRAY_PART_LEN];
7986
array[vlan_id % VLAN_GROUP_ARRAY_PART_LEN] = dev;
8087
}

0 commit comments

Comments
 (0)