1 This patch allows the user to specify desired packet types (outgoing,
2 broadcast, unicast, etc.) on packet sockets via setsockopt.
3 This can reduce the load in situations where only a limited number
4 of packet types are necessary
6 Signed-off-by: Felix Fietkau <nbd@openwrt.org>
8 --- a/include/linux/if_packet.h
9 +++ b/include/linux/if_packet.h
10 @@ -29,6 +29,8 @@ struct sockaddr_ll {
11 /* These ones are invisible by user level */
12 #define PACKET_LOOPBACK 5 /* MC/BRD frame looped back */
13 #define PACKET_FASTROUTE 6 /* Fastrouted frame */
14 +#define PACKET_MASK_ANY 0xffffffff /* mask for packet type bits */
17 /* Packet socket options */
19 @@ -47,6 +49,8 @@ struct sockaddr_ll {
20 #define PACKET_TX_RING 13
21 #define PACKET_LOSS 14
22 #define PACKET_VNET_HDR 15
23 +#define PACKET_RECV_TYPE 16
26 struct tpacket_stats {
27 unsigned int tp_packets;
28 --- a/net/packet/af_packet.c
29 +++ b/net/packet/af_packet.c
30 @@ -202,6 +202,7 @@ struct packet_sock {
31 unsigned int tp_reserve;
32 unsigned int tp_loss:1;
33 struct packet_type prot_hook ____cacheline_aligned_in_smp;
34 + unsigned int pkt_type;
37 struct packet_skb_cb {
38 @@ -336,6 +337,7 @@ static int packet_rcv_spkt(struct sk_buf
41 struct sockaddr_pkt *spkt;
42 + struct packet_sock *po;
45 * When we registered the protocol we saved the socket in the data
46 @@ -343,6 +345,7 @@ static int packet_rcv_spkt(struct sk_buf
49 sk = pt->af_packet_priv;
53 * Yank back the headers [hope the device set this
54 @@ -355,7 +358,7 @@ static int packet_rcv_spkt(struct sk_buf
55 * so that this procedure is noop.
58 - if (skb->pkt_type == PACKET_LOOPBACK)
59 + if (!(po->pkt_type & (1 << skb->pkt_type)))
62 if (!net_eq(dev_net(dev), sock_net(sk)))
63 @@ -531,12 +534,12 @@ static int packet_rcv(struct sk_buff *sk
64 int skb_len = skb->len;
65 unsigned int snaplen, res;
67 - if (skb->pkt_type == PACKET_LOOPBACK)
70 sk = pt->af_packet_priv;
73 + if (!(po->pkt_type & (1 << skb->pkt_type)))
76 if (!net_eq(dev_net(dev), sock_net(sk)))
79 @@ -651,12 +654,12 @@ static int tpacket_rcv(struct sk_buff *s
83 - if (skb->pkt_type == PACKET_LOOPBACK)
86 sk = pt->af_packet_priv;
89 + if (!(po->pkt_type & (1 << skb->pkt_type)))
92 if (!net_eq(dev_net(dev), sock_net(sk)))
95 @@ -1464,6 +1467,7 @@ static int packet_create(struct net *net
96 spin_lock_init(&po->bind_lock);
97 mutex_init(&po->pg_vec_lock);
98 po->prot_hook.func = packet_rcv;
99 + po->pkt_type = PACKET_MASK_ANY & ~(1 << PACKET_LOOPBACK);
101 if (sock->type == SOCK_PACKET)
102 po->prot_hook.func = packet_rcv_spkt;
103 @@ -1968,6 +1972,16 @@ packet_setsockopt(struct socket *sock, i
104 po->has_vnet_hdr = !!val;
107 + case PACKET_RECV_TYPE:
110 + if (optlen != sizeof(val))
112 + if (copy_from_user(&val, optval, sizeof(val)))
114 + po->pkt_type = val & ~PACKET_LOOPBACK;
120 @@ -2025,6 +2039,13 @@ static int packet_getsockopt(struct sock
124 + case PACKET_RECV_TYPE:
125 + if (len > sizeof(unsigned int))
126 + len = sizeof(unsigned int);
127 + val = po->pkt_type;
132 if (len > sizeof(int))