1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _NF_QUEUE_H
3 #define _NF_QUEUE_H
4
5 #include <linux/ip.h>
6 #include <linux/ipv6.h>
7 #include <linux/jhash.h>
8 #include <linux/netfilter.h>
9 #include <linux/rhashtable-types.h>
10 #include <linux/skbuff.h>
11
12 /* Each queued (to userspace) skbuff has one of these. */
13 struct nf_queue_entry {
14 struct list_head list;
15 struct rhash_head hash_node;
16 struct sk_buff *skb;
17 struct net_device *skb_dev;
18 unsigned int id;
19 unsigned int hook_index; /* index in hook_entries->hook[] */
20 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
21 struct net_device *bridge_dev;
22 struct net_device *physin;
23 struct net_device *physout;
24 #endif
25 struct nf_hook_state state;
26 bool nf_ct_is_unconfirmed;
27 u16 size; /* sizeof(entry) + saved route keys */
28
29 /* extra space to store route keys */
30 };
31
32 #define nf_queue_entry_reroute(x) ((void *)x + sizeof(struct nf_queue_entry))
33
34 /* Packet queuing */
35 struct nf_queue_handler {
36 int (*outfn)(struct nf_queue_entry *entry,
37 unsigned int queuenum);
38 void (*nf_hook_drop)(struct net *net);
39 };
40
41 void nf_register_queue_handler(const struct nf_queue_handler *qh);
42 void nf_unregister_queue_handler(void);
43
44 bool nf_queue_entry_get_refs(struct nf_queue_entry *entry);
45 void nf_queue_entry_free(struct nf_queue_entry *entry);
46
init_hashrandom(u32 * jhash_initval)47 static inline void init_hashrandom(u32 *jhash_initval)
48 {
49 while (*jhash_initval == 0)
50 *jhash_initval = get_random_u32();
51 }
52
hash_v4(const struct iphdr * iph,u32 initval)53 static inline u32 hash_v4(const struct iphdr *iph, u32 initval)
54 {
55 /* packets in either direction go into same queue */
56 if ((__force u32)iph->saddr < (__force u32)iph->daddr)
57 return jhash_3words((__force u32)iph->saddr,
58 (__force u32)iph->daddr, iph->protocol, initval);
59
60 return jhash_3words((__force u32)iph->daddr,
61 (__force u32)iph->saddr, iph->protocol, initval);
62 }
63
hash_v6(const struct ipv6hdr * ip6h,u32 initval)64 static inline u32 hash_v6(const struct ipv6hdr *ip6h, u32 initval)
65 {
66 u32 a, b, c;
67
68 if ((__force u32)ip6h->saddr.s6_addr32[3] <
69 (__force u32)ip6h->daddr.s6_addr32[3]) {
70 a = (__force u32) ip6h->saddr.s6_addr32[3];
71 b = (__force u32) ip6h->daddr.s6_addr32[3];
72 } else {
73 b = (__force u32) ip6h->saddr.s6_addr32[3];
74 a = (__force u32) ip6h->daddr.s6_addr32[3];
75 }
76
77 if ((__force u32)ip6h->saddr.s6_addr32[1] <
78 (__force u32)ip6h->daddr.s6_addr32[1])
79 c = (__force u32) ip6h->saddr.s6_addr32[1];
80 else
81 c = (__force u32) ip6h->daddr.s6_addr32[1];
82
83 return jhash_3words(a, b, c, initval);
84 }
85
hash_bridge(const struct sk_buff * skb,u32 initval)86 static inline u32 hash_bridge(const struct sk_buff *skb, u32 initval)
87 {
88 struct ipv6hdr *ip6h, _ip6h;
89 struct iphdr *iph, _iph;
90
91 switch (eth_hdr(skb)->h_proto) {
92 case htons(ETH_P_IP):
93 iph = skb_header_pointer(skb, skb_network_offset(skb),
94 sizeof(*iph), &_iph);
95 if (iph)
96 return hash_v4(iph, initval);
97 break;
98 case htons(ETH_P_IPV6):
99 ip6h = skb_header_pointer(skb, skb_network_offset(skb),
100 sizeof(*ip6h), &_ip6h);
101 if (ip6h)
102 return hash_v6(ip6h, initval);
103 break;
104 }
105
106 return 0;
107 }
108
109 static inline u32
nfqueue_hash(const struct sk_buff * skb,u16 queue,u16 queues_total,u8 family,u32 initval)110 nfqueue_hash(const struct sk_buff *skb, u16 queue, u16 queues_total, u8 family,
111 u32 initval)
112 {
113 switch (family) {
114 case NFPROTO_IPV4:
115 queue += reciprocal_scale(hash_v4(ip_hdr(skb), initval),
116 queues_total);
117 break;
118 case NFPROTO_IPV6:
119 queue += reciprocal_scale(hash_v6(ipv6_hdr(skb), initval),
120 queues_total);
121 break;
122 case NFPROTO_BRIDGE:
123 queue += reciprocal_scale(hash_bridge(skb, initval),
124 queues_total);
125 break;
126 }
127
128 return queue;
129 }
130
131 int nf_queue(struct sk_buff *skb, struct nf_hook_state *state,
132 unsigned int index, unsigned int verdict);
133
134 #endif /* _NF_QUEUE_H */
135