rename target/linux/generic-2.6 to generic
[15.05/openwrt.git] / target / linux / generic / patches-2.6.34 / 240-packet_socket_type.patch
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 @@ -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
24 +
25  
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 +       __u8                    pkt_type:3;
35  };
36  
37  struct packet_skb_cb {
38 @@ -336,6 +337,7 @@ static int packet_rcv_spkt(struct sk_buf
39  {
40         struct sock *sk;
41         struct sockaddr_pkt *spkt;
42 +       struct packet_sock *po;
43  
44         /*
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
47          */
48  
49         sk = pt->af_packet_priv;
50 +       po = pkt_sk(sk);
51  
52         /*
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.
56          */
57  
58 -       if (skb->pkt_type == PACKET_LOOPBACK)
59 +       if (!(po->pkt_type & (1 << skb->pkt_type)))
60                 goto out;
61  
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;
66  
67 -       if (skb->pkt_type == PACKET_LOOPBACK)
68 -               goto drop;
69 -
70         sk = pt->af_packet_priv;
71         po = pkt_sk(sk);
72  
73 +       if (!(po->pkt_type & (1 << skb->pkt_type)))
74 +               goto drop;
75 +
76         if (!net_eq(dev_net(dev), sock_net(sk)))
77                 goto drop;
78  
79 @@ -651,12 +654,12 @@ static int tpacket_rcv(struct sk_buff *s
80         struct timeval tv;
81         struct timespec ts;
82  
83 -       if (skb->pkt_type == PACKET_LOOPBACK)
84 -               goto drop;
85 -
86         sk = pt->af_packet_priv;
87         po = pkt_sk(sk);
88  
89 +       if (!(po->pkt_type & (1 << skb->pkt_type)))
90 +               goto drop;
91 +
92         if (!net_eq(dev_net(dev), sock_net(sk)))
93                 goto drop;
94  
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);
100  
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;
105                 return 0;
106         }
107 +        case PACKET_RECV_TYPE:
108 +        {
109 +                unsigned int val;
110 +                if (optlen != sizeof(val))
111 +                        return -EINVAL;
112 +                if (copy_from_user(&val, optval, sizeof(val)))
113 +                        return -EFAULT;
114 +                po->pkt_type = val & ~PACKET_LOOPBACK;
115 +                return 0;
116 +        }
117         default:
118                 return -ENOPROTOOPT;
119         }
120 @@ -2025,6 +2039,13 @@ static int packet_getsockopt(struct sock
121  
122                 data = &val;
123                 break;
124 +       case PACKET_RECV_TYPE:
125 +               if (len > sizeof(unsigned int))
126 +                       len = sizeof(unsigned int);
127 +               val = po->pkt_type;
128 +
129 +               data = &val;
130 +               break;
131         case PACKET_VERSION:
132                 if (len > sizeof(int))
133                         len = sizeof(int);