| 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 |
| 5 | |
| 6 | Signed-off-by: Felix Fietkau <nbd@openwrt.org> |
| 7 | |
| 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 */ |
| 15 | + |
| 16 | |
| 17 | /* Packet socket options */ |
| 18 | |
| 19 | @@ -50,6 +52,7 @@ struct sockaddr_ll { |
| 20 | #define PACKET_TX_TIMESTAMP 16 |
| 21 | #define PACKET_TIMESTAMP 17 |
| 22 | #define PACKET_FANOUT 18 |
| 23 | +#define PACKET_RECV_TYPE 19 |
| 24 | |
| 25 | #define PACKET_FANOUT_HASH 0 |
| 26 | #define PACKET_FANOUT_LB 1 |
| 27 | --- a/net/packet/af_packet.c |
| 28 | +++ b/net/packet/af_packet.c |
| 29 | @@ -212,6 +212,7 @@ struct packet_sock { |
| 30 | unsigned int tp_loss:1; |
| 31 | unsigned int tp_tstamp; |
| 32 | struct packet_type prot_hook ____cacheline_aligned_in_smp; |
| 33 | + unsigned int pkt_type; |
| 34 | }; |
| 35 | |
| 36 | #define PACKET_FANOUT_MAX 256 |
| 37 | @@ -661,6 +662,7 @@ static int packet_rcv_spkt(struct sk_buf |
| 38 | { |
| 39 | struct sock *sk; |
| 40 | struct sockaddr_pkt *spkt; |
| 41 | + struct packet_sock *po; |
| 42 | |
| 43 | /* |
| 44 | * When we registered the protocol we saved the socket in the data |
| 45 | @@ -668,6 +670,7 @@ static int packet_rcv_spkt(struct sk_buf |
| 46 | */ |
| 47 | |
| 48 | sk = pt->af_packet_priv; |
| 49 | + po = pkt_sk(sk); |
| 50 | |
| 51 | /* |
| 52 | * Yank back the headers [hope the device set this |
| 53 | @@ -680,7 +683,7 @@ static int packet_rcv_spkt(struct sk_buf |
| 54 | * so that this procedure is noop. |
| 55 | */ |
| 56 | |
| 57 | - if (skb->pkt_type == PACKET_LOOPBACK) |
| 58 | + if (!(po->pkt_type & (1 << skb->pkt_type))) |
| 59 | goto out; |
| 60 | |
| 61 | if (!net_eq(dev_net(dev), sock_net(sk))) |
| 62 | @@ -873,12 +876,12 @@ static int packet_rcv(struct sk_buff *sk |
| 63 | int skb_len = skb->len; |
| 64 | unsigned int snaplen, res; |
| 65 | |
| 66 | - if (skb->pkt_type == PACKET_LOOPBACK) |
| 67 | - goto drop; |
| 68 | - |
| 69 | sk = pt->af_packet_priv; |
| 70 | po = pkt_sk(sk); |
| 71 | |
| 72 | + if (!(po->pkt_type & (1 << skb->pkt_type))) |
| 73 | + goto drop; |
| 74 | + |
| 75 | if (!net_eq(dev_net(dev), sock_net(sk))) |
| 76 | goto drop; |
| 77 | |
| 78 | @@ -994,12 +997,12 @@ static int tpacket_rcv(struct sk_buff *s |
| 79 | struct timespec ts; |
| 80 | struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb); |
| 81 | |
| 82 | - if (skb->pkt_type == PACKET_LOOPBACK) |
| 83 | - goto drop; |
| 84 | - |
| 85 | sk = pt->af_packet_priv; |
| 86 | po = pkt_sk(sk); |
| 87 | |
| 88 | + if (!(po->pkt_type & (1 << skb->pkt_type))) |
| 89 | + goto drop; |
| 90 | + |
| 91 | if (!net_eq(dev_net(dev), sock_net(sk))) |
| 92 | goto drop; |
| 93 | |
| 94 | @@ -1826,6 +1829,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); |
| 99 | |
| 100 | if (sock->type == SOCK_PACKET) |
| 101 | po->prot_hook.func = packet_rcv_spkt; |
| 102 | @@ -2410,6 +2414,16 @@ packet_setsockopt(struct socket *sock, i |
| 103 | |
| 104 | return fanout_add(sk, val & 0xffff, val >> 16); |
| 105 | } |
| 106 | + case PACKET_RECV_TYPE: |
| 107 | + { |
| 108 | + unsigned int val; |
| 109 | + if (optlen != sizeof(val)) |
| 110 | + return -EINVAL; |
| 111 | + if (copy_from_user(&val, optval, sizeof(val))) |
| 112 | + return -EFAULT; |
| 113 | + po->pkt_type = val & ~PACKET_LOOPBACK; |
| 114 | + return 0; |
| 115 | + } |
| 116 | default: |
| 117 | return -ENOPROTOOPT; |
| 118 | } |
| 119 | @@ -2467,6 +2481,13 @@ static int packet_getsockopt(struct sock |
| 120 | |
| 121 | data = &val; |
| 122 | break; |
| 123 | + case PACKET_RECV_TYPE: |
| 124 | + if (len > sizeof(unsigned int)) |
| 125 | + len = sizeof(unsigned int); |
| 126 | + val = po->pkt_type; |
| 127 | + |
| 128 | + data = &val; |
| 129 | + break; |
| 130 | case PACKET_VERSION: |
| 131 | if (len > sizeof(int)) |
| 132 | len = sizeof(int); |
| 133 | |