]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/commitdiff
packet: add sockopt to ignore outgoing packets
authorVincent Whitchurch <vincent.whitchurch@axis.com>
Mon, 3 Sep 2018 14:23:36 +0000 (16:23 +0200)
committerDavid S. Miller <davem@davemloft.net>
Thu, 6 Sep 2018 05:09:37 +0000 (22:09 -0700)
Currently, the only way to ignore outgoing packets on a packet socket is
via the BPF filter.  With MSG_ZEROCOPY, packets that are looped into
AF_PACKET are copied in dev_queue_xmit_nit(), and this copy happens even
if the filter run from packet_rcv() would reject them.  So the presence
of a packet socket on the interface takes away the benefits of
MSG_ZEROCOPY, even if the packet socket is not interested in outgoing
packets.  (Even when MSG_ZEROCOPY is not used, the skb is unnecessarily
cloned, but the cost for that is much lower.)

Add a socket option to allow AF_PACKET sockets to ignore outgoing
packets to solve this.  Note that the *BSDs already have something
similar: BIOCSSEESENT/BIOCSDIRECTION and BIOCSDIRFILT.

The first intended user is lldpd.

Signed-off-by: Vincent Whitchurch <vincent.whitchurch@axis.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
include/linux/netdevice.h
include/uapi/linux/if_packet.h
net/core/dev.c
net/packet/af_packet.c

index 4271f6b4e4197afbb9eed2aafdcfccdbdc08819c..e2b3bd750c98d8182ce88af44b88d96b4c36ebdb 100644 (file)
@@ -2343,6 +2343,7 @@ static inline struct sk_buff *call_gro_receive_sk(gro_receive_sk_t cb,
 
 struct packet_type {
        __be16                  type;   /* This is really htons(ether_type). */
+       bool                    ignore_outgoing;
        struct net_device       *dev;   /* NULL is wildcarded here           */
        int                     (*func) (struct sk_buff *,
                                         struct net_device *,
index 67b61d91d89bf4fc8a54e01ffa3ca253a7fc45c9..467b654bd4c7df2aab3e8db892dbd14739ff21fd 100644 (file)
@@ -57,6 +57,7 @@ struct sockaddr_ll {
 #define PACKET_QDISC_BYPASS            20
 #define PACKET_ROLLOVER_STATS          21
 #define PACKET_FANOUT_DATA             22
+#define PACKET_IGNORE_OUTGOING         23
 
 #define PACKET_FANOUT_HASH             0
 #define PACKET_FANOUT_LB               1
index 82114e1111e6558d5b8ecc2207aac679e21698c6..ca78dc5a79a36a1c2e2cc04d65fe1646bafd4015 100644 (file)
@@ -1969,6 +1969,9 @@ void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev)
        rcu_read_lock();
 again:
        list_for_each_entry_rcu(ptype, ptype_list, list) {
+               if (ptype->ignore_outgoing)
+                       continue;
+
                /* Never send packets back to the socket
                 * they originated from - MvS (miquels@drinkel.ow.org)
                 */
index 75c92a87e7b2481141161c8945f5e7eef8e0abf8..f85f67b5c1f41e7cd938ee6ffac8d1d2e6b2f46b 100644 (file)
@@ -3805,6 +3805,20 @@ packet_setsockopt(struct socket *sock, int level, int optname, char __user *optv
 
                return fanout_set_data(po, optval, optlen);
        }
+       case PACKET_IGNORE_OUTGOING:
+       {
+               int val;
+
+               if (optlen != sizeof(val))
+                       return -EINVAL;
+               if (copy_from_user(&val, optval, sizeof(val)))
+                       return -EFAULT;
+               if (val < 0 || val > 1)
+                       return -EINVAL;
+
+               po->prot_hook.ignore_outgoing = !!val;
+               return 0;
+       }
        case PACKET_TX_HAS_OFF:
        {
                unsigned int val;
@@ -3928,6 +3942,9 @@ static int packet_getsockopt(struct socket *sock, int level, int optname,
                        ((u32)po->fanout->flags << 24)) :
                       0);
                break;
+       case PACKET_IGNORE_OUTGOING:
+               val = po->prot_hook.ignore_outgoing;
+               break;
        case PACKET_ROLLOVER_STATS:
                if (!po->rollover)
                        return -EINVAL;