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