Skip to content

Commit 61e8462

Browse files
jarodwilsondavem330
authored andcommitted
net: centralize net_device min/max MTU checking
While looking into an MTU issue with sfc, I started noticing that almost every NIC driver with an ndo_change_mtu function implemented almost exactly the same range checks, and in many cases, that was the only practical thing their ndo_change_mtu function was doing. Quite a few drivers have either 68, 64, 60 or 46 as their minimum MTU value checked, and then various sizes from 1500 to 65535 for their maximum MTU value. We can remove a whole lot of redundant code here if we simple store min_mtu and max_mtu in net_device, and check against those in net/core/dev.c's dev_set_mtu(). In theory, there should be zero functional change with this patch, it just puts the infrastructure in place. Subsequent patches will attempt to start using said infrastructure, with theoretically zero change in functionality. CC: [email protected] Signed-off-by: Jarod Wilson <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 1b83099 commit 61e8462

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

include/linux/netdevice.h

+4
Original file line numberDiff line numberDiff line change
@@ -1506,6 +1506,8 @@ enum netdev_priv_flags {
15061506
* @if_port: Selectable AUI, TP, ...
15071507
* @dma: DMA channel
15081508
* @mtu: Interface MTU value
1509+
* @min_mtu: Interface Minimum MTU value
1510+
* @max_mtu: Interface Maximum MTU value
15091511
* @type: Interface hardware type
15101512
* @hard_header_len: Maximum hardware header length.
15111513
*
@@ -1726,6 +1728,8 @@ struct net_device {
17261728
unsigned char dma;
17271729

17281730
unsigned int mtu;
1731+
unsigned int min_mtu;
1732+
unsigned int max_mtu;
17291733
unsigned short type;
17301734
unsigned short hard_header_len;
17311735

net/core/dev.c

+11-2
Original file line numberDiff line numberDiff line change
@@ -6499,9 +6499,18 @@ int dev_set_mtu(struct net_device *dev, int new_mtu)
64996499
if (new_mtu == dev->mtu)
65006500
return 0;
65016501

6502-
/* MTU must be positive. */
6503-
if (new_mtu < 0)
6502+
/* MTU must be positive, and in range */
6503+
if (new_mtu < 0 || new_mtu < dev->min_mtu) {
6504+
net_err_ratelimited("%s: Invalid MTU %d requested, hw min %d\n",
6505+
dev->name, new_mtu, dev->min_mtu);
65046506
return -EINVAL;
6507+
}
6508+
6509+
if (dev->max_mtu > 0 && new_mtu > dev->max_mtu) {
6510+
net_err_ratelimited("%s: Invalid MTU %d requested, hw max %d\n",
6511+
dev->name, new_mtu, dev->min_mtu);
6512+
return -EINVAL;
6513+
}
65056514

65066515
if (!netif_device_present(dev))
65076516
return -ENODEV;

0 commit comments

Comments
 (0)