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 @@ -46,6 +48,7 @@ struct sockaddr_ll {
20 #define PACKET_RESERVE 12
21 #define PACKET_TX_RING 13
22 #define PACKET_LOSS 14
23 +#define PACKET_RECV_TYPE 15
25 struct tpacket_stats {
26 unsigned int tp_packets;
27 --- a/net/packet/af_packet.c
28 +++ b/net/packet/af_packet.c
29 @@ -205,6 +205,7 @@ struct packet_sock {
30 unsigned int tp_loss:1;
32 struct packet_type prot_hook ____cacheline_aligned_in_smp;
33 + unsigned int pkt_type;
36 struct packet_skb_cb {
37 @@ -343,6 +344,7 @@ static int packet_rcv_spkt(struct sk_buf
40 struct sockaddr_pkt *spkt;
41 + struct packet_sock *po;
44 * When we registered the protocol we saved the socket in the data
45 @@ -350,6 +352,7 @@ static int packet_rcv_spkt(struct sk_buf
48 sk = pt->af_packet_priv;
52 * Yank back the headers [hope the device set this
53 @@ -362,7 +365,7 @@ static int packet_rcv_spkt(struct sk_buf
54 * so that this procedure is noop.
57 - if (skb->pkt_type == PACKET_LOOPBACK)
58 + if (!(po->pkt_type & (1 << skb->pkt_type)))
61 if (!net_eq(dev_net(dev), sock_net(sk)))
62 @@ -538,12 +541,12 @@ static int packet_rcv(struct sk_buff *sk
63 int skb_len = skb->len;
64 unsigned int snaplen, res;
66 - if (skb->pkt_type == PACKET_LOOPBACK)
69 sk = pt->af_packet_priv;
72 + if (!(po->pkt_type & (1 << skb->pkt_type)))
75 if (!net_eq(dev_net(dev), sock_net(sk)))
78 @@ -659,12 +662,12 @@ static int tpacket_rcv(struct sk_buff *s
82 - if (skb->pkt_type == PACKET_LOOPBACK)
85 sk = pt->af_packet_priv;
88 + if (!(po->pkt_type & (1 << skb->pkt_type)))
91 if (!net_eq(dev_net(dev), sock_net(sk)))
94 @@ -1386,6 +1389,7 @@ static int packet_create(struct net *net
95 spin_lock_init(&po->bind_lock);
96 mutex_init(&po->pg_vec_lock);
97 po->prot_hook.func = packet_rcv;
98 + po->pkt_type = PACKET_MASK_ANY & ~(1 << PACKET_LOOPBACK);
100 if (sock->type == SOCK_PACKET)
101 po->prot_hook.func = packet_rcv_spkt;
102 @@ -1731,6 +1735,16 @@ packet_setsockopt(struct socket *sock, i
103 ret = packet_mc_drop(sk, &mreq);
106 + case PACKET_RECV_TYPE:
109 + if (optlen != sizeof(val))
111 + if (copy_from_user(&val, optval, sizeof(val)))
113 + po->pkt_type = val & ~PACKET_LOOPBACK;
117 #ifdef CONFIG_PACKET_MMAP
119 @@ -1876,6 +1890,13 @@ static int packet_getsockopt(struct sock
123 + case PACKET_RECV_TYPE:
124 + if (len > sizeof(unsigned int))
125 + len = sizeof(unsigned int);
126 + val = po->pkt_type;
130 #ifdef CONFIG_PACKET_MMAP
132 if (len > sizeof(int))