2
0

001-net-centralize-net_device-min-max-MTU-checking.patch 2.8 KB

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