fcabb62f5e906208784a691b2695afc8a447c028
[openwrt/staging/wigyori.git] / target / linux / layerscape / patches-4.9 / 001-net-centralize-net_device-min-max-MTU-checking.patch
1 From 95b8bbff6ecf0692747622af16d917a67313f8cc Mon Sep 17 00:00:00 2001
2 From: Jarod Wilson <jarod@redhat.com>
3 Date: Fri, 7 Oct 2016 22:04:33 -0400
4 Subject: [PATCH] net: centralize net_device min/max MTU checking
5
6 While looking into an MTU issue with sfc, I started noticing that almost
7 every NIC driver with an ndo_change_mtu function implemented almost
8 exactly the same range checks, and in many cases, that was the only
9 practical thing their ndo_change_mtu function was doing. Quite a few
10 drivers have either 68, 64, 60 or 46 as their minimum MTU value checked,
11 and then various sizes from 1500 to 65535 for their maximum MTU value. We
12 can remove a whole lot of redundant code here if we simple store min_mtu
13 and max_mtu in net_device, and check against those in net/core/dev.c's
14 dev_set_mtu().
15
16 In theory, there should be zero functional change with this patch, it just
17 puts the infrastructure in place. Subsequent patches will attempt to start
18 using said infrastructure, with theoretically zero change in
19 functionality.
20
21 CC: netdev@vger.kernel.org
22 Signed-off-by: Jarod Wilson <jarod@redhat.com>
23 Signed-off-by: David S. Miller <davem@davemloft.net>
24 ---
25 include/linux/netdevice.h | 4 ++++
26 net/core/dev.c | 13 +++++++++++--
27 2 files changed, 15 insertions(+), 2 deletions(-)
28
29 diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h
30 index 780e7171f548..2082b7d02a77 100644
31 --- a/include/linux/netdevice.h
32 +++ b/include/linux/netdevice.h
33 @@ -1507,6 +1507,8 @@ enum netdev_priv_flags {
34 * @if_port: Selectable AUI, TP, ...
35 * @dma: DMA channel
36 * @mtu: Interface MTU value
37 + * @min_mtu: Interface Minimum MTU value
38 + * @max_mtu: Interface Maximum MTU value
39 * @type: Interface hardware type
40 * @hard_header_len: Maximum hardware header length.
41 * @min_header_len: Minimum hardware header length
42 @@ -1728,6 +1730,8 @@ struct net_device {
43 unsigned char dma;
44
45 unsigned int mtu;
46 + unsigned int min_mtu;
47 + unsigned int max_mtu;
48 unsigned short type;
49 unsigned short hard_header_len;
50 unsigned short min_header_len;
51 diff --git a/net/core/dev.c b/net/core/dev.c
52 index 2e04fd188081..c7ec56e8659a 100644
53 --- a/net/core/dev.c
54 +++ b/net/core/dev.c
55 @@ -6524,9 +6524,18 @@ int dev_set_mtu(struct net_device *dev, int new_mtu)
56 if (new_mtu == dev->mtu)
57 return 0;
58
59 - /* MTU must be positive. */
60 - if (new_mtu < 0)
61 + /* MTU must be positive, and in range */
62 + if (new_mtu < 0 || new_mtu < dev->min_mtu) {
63 + net_err_ratelimited("%s: Invalid MTU %d requested, hw min %d\n",
64 + dev->name, new_mtu, dev->min_mtu);
65 return -EINVAL;
66 + }
67 +
68 + if (dev->max_mtu > 0 && new_mtu > dev->max_mtu) {
69 + net_err_ratelimited("%s: Invalid MTU %d requested, hw max %d\n",
70 + dev->name, new_mtu, dev->min_mtu);
71 + return -EINVAL;
72 + }
73
74 if (!netif_device_present(dev))
75 return -ENODEV;
76 --
77 2.11.1
78