xref: /linux/include/net/netfilter/nf_queue.h (revision 82052e5b5ec36504fe1b9c3c28a711a78fcacf37)
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			in_nf_duplicate;
27 	bool			nf_ct_is_unconfirmed;
28 	u16			size; /* sizeof(entry) + saved route keys */
29 
30 	/* extra space to store route keys */
31 };
32 
33 #define nf_queue_entry_reroute(x) ((void *)x + sizeof(struct nf_queue_entry))
34 
35 /* Packet queuing */
36 struct nf_queue_handler {
37 	int		(*outfn)(struct nf_queue_entry *entry,
38 				 unsigned int queuenum);
39 	void		(*nf_hook_drop)(struct net *net,
40 					const struct nf_hook_ops *ops);
41 };
42 
43 void nf_register_queue_handler(const struct nf_queue_handler *qh);
44 void nf_unregister_queue_handler(void);
45 
46 bool nf_queue_entry_get_refs(struct nf_queue_entry *entry);
47 void nf_queue_entry_free(struct nf_queue_entry *entry);
48 
49 static inline void init_hashrandom(u32 *jhash_initval)
50 {
51 	while (*jhash_initval == 0)
52 		*jhash_initval = get_random_u32();
53 }
54 
55 static inline u32 hash_v4(const struct iphdr *iph, u32 initval)
56 {
57 	/* packets in either direction go into same queue */
58 	if ((__force u32)iph->saddr < (__force u32)iph->daddr)
59 		return jhash_3words((__force u32)iph->saddr,
60 			(__force u32)iph->daddr, iph->protocol, initval);
61 
62 	return jhash_3words((__force u32)iph->daddr,
63 			(__force u32)iph->saddr, iph->protocol, initval);
64 }
65 
66 static inline u32 hash_v6(const struct ipv6hdr *ip6h, u32 initval)
67 {
68 	u32 a, b, c;
69 
70 	if ((__force u32)ip6h->saddr.s6_addr32[3] <
71 	    (__force u32)ip6h->daddr.s6_addr32[3]) {
72 		a = (__force u32) ip6h->saddr.s6_addr32[3];
73 		b = (__force u32) ip6h->daddr.s6_addr32[3];
74 	} else {
75 		b = (__force u32) ip6h->saddr.s6_addr32[3];
76 		a = (__force u32) ip6h->daddr.s6_addr32[3];
77 	}
78 
79 	if ((__force u32)ip6h->saddr.s6_addr32[1] <
80 	    (__force u32)ip6h->daddr.s6_addr32[1])
81 		c = (__force u32) ip6h->saddr.s6_addr32[1];
82 	else
83 		c = (__force u32) ip6h->daddr.s6_addr32[1];
84 
85 	return jhash_3words(a, b, c, initval);
86 }
87 
88 static inline u32 hash_bridge(const struct sk_buff *skb, u32 initval)
89 {
90 	struct ipv6hdr *ip6h, _ip6h;
91 	struct iphdr *iph, _iph;
92 
93 	switch (eth_hdr(skb)->h_proto) {
94 	case htons(ETH_P_IP):
95 		iph = skb_header_pointer(skb, skb_network_offset(skb),
96 					 sizeof(*iph), &_iph);
97 		if (iph)
98 			return hash_v4(iph, initval);
99 		break;
100 	case htons(ETH_P_IPV6):
101 		ip6h = skb_header_pointer(skb, skb_network_offset(skb),
102 					  sizeof(*ip6h), &_ip6h);
103 		if (ip6h)
104 			return hash_v6(ip6h, initval);
105 		break;
106 	}
107 
108 	return 0;
109 }
110 
111 static inline u32
112 nfqueue_hash(const struct sk_buff *skb, u16 queue, u16 queues_total, u8 family,
113 	     u32 initval)
114 {
115 	switch (family) {
116 	case NFPROTO_IPV4:
117 		queue += reciprocal_scale(hash_v4(ip_hdr(skb), initval),
118 					  queues_total);
119 		break;
120 	case NFPROTO_IPV6:
121 		queue += reciprocal_scale(hash_v6(ipv6_hdr(skb), initval),
122 					  queues_total);
123 		break;
124 	case NFPROTO_BRIDGE:
125 		queue += reciprocal_scale(hash_bridge(skb, initval),
126 					  queues_total);
127 		break;
128 	}
129 
130 	return queue;
131 }
132 
133 int nf_queue(struct sk_buff *skb, struct nf_hook_state *state,
134 	     unsigned int index, unsigned int verdict);
135 
136 #endif /* _NF_QUEUE_H */
137