1
0

0018-net-allow-PHY-drivers-to-insert-packet-mangle-hooks.patch 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. From ebca842041d737b7441748a17ffd535aab851fce Mon Sep 17 00:00:00 2001
  2. From: Phil Sutter <phil@nwl.cc>
  3. Date: Tue, 13 May 2014 01:32:11 +0200
  4. Subject: [PATCH] net: allow PHY drivers to insert packet mangle hooks
  5. ---
  6. include/linux/netdevice.h | 8 ++++++++
  7. include/linux/skbuff.h | 14 ++++----------
  8. include/uapi/linux/if.h | 1 +
  9. net/Kconfig | 6 ++++++
  10. net/core/dev.c | 36 ++++++++++++++++++++++++++++--------
  11. net/core/skbuff.c | 17 +++++++++++++++++
  12. net/ethernet/eth.c | 6 ++++++
  13. 7 files changed, 70 insertions(+), 18 deletions(-)
  14. diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h
  15. index 911718f..8e8dd46 100644
  16. --- a/include/linux/netdevice.h
  17. +++ b/include/linux/netdevice.h
  18. @@ -1245,6 +1245,11 @@ struct net_device {
  19. const struct ethtool_ops *ethtool_ops;
  20. const struct forwarding_accel_ops *fwd_ops;
  21. +#ifdef CONFIG_ETHERNET_PACKET_MANGLE
  22. + void (*eth_mangle_rx)(struct net_device *dev, struct sk_buff *skb);
  23. + struct sk_buff *(*eth_mangle_tx)(struct net_device *dev, struct sk_buff *skb);
  24. +#endif
  25. +
  26. /* Hardware header description */
  27. const struct header_ops *header_ops;
  28. @@ -1313,6 +1318,9 @@ struct net_device {
  29. void *ax25_ptr; /* AX.25 specific data */
  30. struct wireless_dev *ieee80211_ptr; /* IEEE 802.11 specific data,
  31. assign before registering */
  32. +#ifdef CONFIG_ETHERNET_PACKET_MANGLE
  33. + void *phy_ptr; /* PHY device specific data */
  34. +#endif
  35. /*
  36. * Cache lines mostly used on receive path (including eth_type_trans())
  37. diff --git a/include/linux/skbuff.h b/include/linux/skbuff.h
  38. index 15ede6a..5530766 100644
  39. --- a/include/linux/skbuff.h
  40. +++ b/include/linux/skbuff.h
  41. @@ -1858,6 +1858,10 @@ static inline int pskb_trim(struct sk_buff *skb, unsigned int len)
  42. return (len < skb->len) ? __pskb_trim(skb, len) : 0;
  43. }
  44. +extern struct sk_buff *__netdev_alloc_skb_ip_align(struct net_device *dev,
  45. + unsigned int length, gfp_t gfp);
  46. +
  47. +
  48. /**
  49. * pskb_trim_unique - remove end from a paged unique (not cloned) buffer
  50. * @skb: buffer to alter
  51. @@ -1966,16 +1970,6 @@ static inline struct sk_buff *dev_alloc_skb(unsigned int length)
  52. }
  53. -static inline struct sk_buff *__netdev_alloc_skb_ip_align(struct net_device *dev,
  54. - unsigned int length, gfp_t gfp)
  55. -{
  56. - struct sk_buff *skb = __netdev_alloc_skb(dev, length + NET_IP_ALIGN, gfp);
  57. -
  58. - if (NET_IP_ALIGN && skb)
  59. - skb_reserve(skb, NET_IP_ALIGN);
  60. - return skb;
  61. -}
  62. -
  63. static inline struct sk_buff *netdev_alloc_skb_ip_align(struct net_device *dev,
  64. unsigned int length)
  65. {
  66. diff --git a/include/uapi/linux/if.h b/include/uapi/linux/if.h
  67. index d758163..7ffa548 100644
  68. --- a/include/uapi/linux/if.h
  69. +++ b/include/uapi/linux/if.h
  70. @@ -84,6 +84,7 @@
  71. #define IFF_LIVE_ADDR_CHANGE 0x100000 /* device supports hardware address
  72. * change when it's running */
  73. #define IFF_MACVLAN 0x200000 /* Macvlan device */
  74. +#define IFF_NO_IP_ALIGN 0x400000 /* do not ip-align allocated rx pkts */
  75. #define IF_GET_IFACE 0x0001 /* for querying only */
  76. diff --git a/net/Kconfig b/net/Kconfig
  77. index e411046..970c52a 100644
  78. --- a/net/Kconfig
  79. +++ b/net/Kconfig
  80. @@ -24,6 +24,12 @@ menuconfig NET
  81. if NET
  82. +config ETHERNET_PACKET_MANGLE
  83. + bool
  84. + help
  85. + This option can be selected by phy drivers that need to mangle
  86. + packets going in or out of an ethernet device.
  87. +
  88. config WANT_COMPAT_NETLINK_MESSAGES
  89. bool
  90. help
  91. diff --git a/net/core/dev.c b/net/core/dev.c
  92. index fccc195..2e0ba23 100644
  93. --- a/net/core/dev.c
  94. +++ b/net/core/dev.c
  95. @@ -2607,10 +2607,20 @@ int dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev,
  96. if (!list_empty(&ptype_all))
  97. dev_queue_xmit_nit(skb, dev);
  98. - skb_len = skb->len;
  99. - trace_net_dev_start_xmit(skb, dev);
  100. - rc = ops->ndo_start_xmit(skb, dev);
  101. - trace_net_dev_xmit(skb, rc, dev, skb_len);
  102. +#ifdef CONFIG_ETHERNET_PACKET_MANGLE
  103. + if (!dev->eth_mangle_tx ||
  104. + (skb = dev->eth_mangle_tx(dev, skb)) != NULL)
  105. +#else
  106. + if (1)
  107. +#endif
  108. + {
  109. + skb_len = skb->len;
  110. + trace_net_dev_start_xmit(skb, dev);
  111. + rc = ops->ndo_start_xmit(skb, dev);
  112. + trace_net_dev_xmit(skb, rc, dev, skb_len);
  113. + } else {
  114. + rc = NETDEV_TX_OK;
  115. + }
  116. if (rc == NETDEV_TX_OK)
  117. txq_trans_update(txq);
  118. return rc;
  119. @@ -2626,10 +2636,20 @@ gso:
  120. if (!list_empty(&ptype_all))
  121. dev_queue_xmit_nit(nskb, dev);
  122. - skb_len = nskb->len;
  123. - trace_net_dev_start_xmit(nskb, dev);
  124. - rc = ops->ndo_start_xmit(nskb, dev);
  125. - trace_net_dev_xmit(nskb, rc, dev, skb_len);
  126. +#ifdef CONFIG_ETHERNET_PACKET_MANGLE
  127. + if (!dev->eth_mangle_tx ||
  128. + (nskb = dev->eth_mangle_tx(dev, nskb)) != NULL)
  129. +#else
  130. + if (1)
  131. +#endif
  132. + {
  133. + skb_len = nskb->len;
  134. + trace_net_dev_start_xmit(nskb, dev);
  135. + rc = ops->ndo_start_xmit(nskb, dev);
  136. + trace_net_dev_xmit(nskb, rc, dev, skb_len);
  137. + } else {
  138. + rc = NETDEV_TX_OK;
  139. + }
  140. if (unlikely(rc != NETDEV_TX_OK)) {
  141. if (rc & ~NETDEV_TX_MASK)
  142. goto out_kfree_gso_skb;
  143. diff --git a/net/core/skbuff.c b/net/core/skbuff.c
  144. index e5ae776e..400ff2a 100644
  145. --- a/net/core/skbuff.c
  146. +++ b/net/core/skbuff.c
  147. @@ -62,6 +62,7 @@
  148. #include <linux/scatterlist.h>
  149. #include <linux/errqueue.h>
  150. #include <linux/prefetch.h>
  151. +#include <uapi/linux/if.h>
  152. #include <net/protocol.h>
  153. #include <net/dst.h>
  154. @@ -439,6 +440,22 @@ struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
  155. }
  156. EXPORT_SYMBOL(__netdev_alloc_skb);
  157. +struct sk_buff *__netdev_alloc_skb_ip_align(struct net_device *dev,
  158. + unsigned int length, gfp_t gfp)
  159. +{
  160. + struct sk_buff *skb = __netdev_alloc_skb(dev, length + NET_IP_ALIGN, gfp);
  161. +
  162. +#ifdef CONFIG_ETHERNET_PACKET_MANGLE
  163. + if (dev && (dev->priv_flags & IFF_NO_IP_ALIGN))
  164. + return skb;
  165. +#endif
  166. +
  167. + if (NET_IP_ALIGN && skb)
  168. + skb_reserve(skb, NET_IP_ALIGN);
  169. + return skb;
  170. +}
  171. +EXPORT_SYMBOL(__netdev_alloc_skb_ip_align);
  172. +
  173. void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
  174. int size, unsigned int truesize)
  175. {
  176. diff --git a/net/ethernet/eth.c b/net/ethernet/eth.c
  177. index 5dc638c..f4fd124 100644
  178. --- a/net/ethernet/eth.c
  179. +++ b/net/ethernet/eth.c
  180. @@ -161,6 +161,12 @@ __be16 eth_type_trans(struct sk_buff *skb, struct net_device *dev)
  181. const struct ethhdr *eth;
  182. skb->dev = dev;
  183. +
  184. +#ifdef CONFIG_ETHERNET_PACKET_MANGLE
  185. + if (dev->eth_mangle_rx)
  186. + dev->eth_mangle_rx(dev, skb);
  187. +#endif
  188. +
  189. skb_reset_mac_header(skb);
  190. skb_pull_inline(skb, ETH_HLEN);
  191. eth = eth_hdr(skb);
  192. --
  193. 1.8.5.3