Skip to content
Navigation Menu
Toggle navigation
Sign in
In this repository
All GitHub Enterprise
↵
Jump to
↵
No suggested jump to results
In this repository
All GitHub Enterprise
↵
Jump to
↵
In this organization
All GitHub Enterprise
↵
Jump to
↵
In this repository
All GitHub Enterprise
↵
Jump to
↵
Sign in
Reseting focus
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
mariux64
/
linux
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
2
Pull requests
0
Actions
Projects
0
Wiki
Security
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security
Insights
Files
a42b99a
Documentation
arch
block
crypto
drivers
firmware
fs
include
init
ipc
kernel
lib
mm
net
802
8021q
9p
appletalk
atm
ax25
batman-adv
bluetooth
bridge
caif
can
ceph
core
dcb
dccp
decnet
dns_resolver
dsa
ethernet
hsr
ieee802154
ipv4
ipv6
ipx
irda
iucv
key
l2tp
lapb
llc
mac80211
mac802154
mpls
netfilter
ipset
ipvs
Kconfig
Makefile
core.c
nf_conntrack_acct.c
nf_conntrack_amanda.c
nf_conntrack_broadcast.c
nf_conntrack_core.c
nf_conntrack_ecache.c
nf_conntrack_expect.c
nf_conntrack_extend.c
nf_conntrack_ftp.c
nf_conntrack_h323_asn1.c
nf_conntrack_h323_main.c
nf_conntrack_h323_types.c
nf_conntrack_helper.c
nf_conntrack_irc.c
nf_conntrack_l3proto_generic.c
nf_conntrack_labels.c
nf_conntrack_netbios_ns.c
nf_conntrack_netlink.c
nf_conntrack_pptp.c
nf_conntrack_proto.c
nf_conntrack_proto_dccp.c
nf_conntrack_proto_generic.c
nf_conntrack_proto_gre.c
nf_conntrack_proto_sctp.c
nf_conntrack_proto_tcp.c
nf_conntrack_proto_udp.c
nf_conntrack_proto_udplite.c
nf_conntrack_sane.c
nf_conntrack_seqadj.c
nf_conntrack_sip.c
nf_conntrack_snmp.c
nf_conntrack_standalone.c
nf_conntrack_tftp.c
nf_conntrack_timeout.c
nf_conntrack_timestamp.c
nf_internals.h
nf_log.c
nf_nat_amanda.c
nf_nat_core.c
nf_nat_ftp.c
nf_nat_helper.c
nf_nat_irc.c
nf_nat_proto_common.c
nf_nat_proto_dccp.c
nf_nat_proto_sctp.c
nf_nat_proto_tcp.c
nf_nat_proto_udp.c
nf_nat_proto_udplite.c
nf_nat_proto_unknown.c
nf_nat_sip.c
nf_nat_tftp.c
nf_queue.c
nf_sockopt.c
nf_synproxy_core.c
nf_tables_api.c
nf_tables_core.c
nfnetlink.c
nfnetlink_acct.c
nfnetlink_cthelper.c
nfnetlink_cttimeout.c
nfnetlink_log.c
nfnetlink_queue_core.c
nfnetlink_queue_ct.c
nft_bitwise.c
nft_byteorder.c
nft_cmp.c
nft_compat.c
nft_counter.c
nft_ct.c
nft_expr_template.c
nft_exthdr.c
nft_hash.c
nft_immediate.c
nft_limit.c
nft_log.c
nft_lookup.c
nft_meta.c
nft_meta_target.c
nft_nat.c
nft_payload.c
nft_rbtree.c
x_tables.c
xt_AUDIT.c
xt_CHECKSUM.c
xt_CLASSIFY.c
xt_CONNSECMARK.c
xt_CT.c
xt_DSCP.c
xt_HL.c
xt_HMARK.c
xt_IDLETIMER.c
xt_LED.c
xt_LOG.c
xt_NETMAP.c
xt_NFLOG.c
xt_NFQUEUE.c
xt_RATEEST.c
xt_REDIRECT.c
xt_SECMARK.c
xt_TCPMSS.c
xt_TCPOPTSTRIP.c
xt_TEE.c
xt_TPROXY.c
xt_TRACE.c
xt_addrtype.c
xt_bpf.c
xt_cluster.c
xt_comment.c
xt_connbytes.c
xt_connlabel.c
xt_connlimit.c
xt_connmark.c
xt_conntrack.c
xt_cpu.c
xt_dccp.c
xt_devgroup.c
xt_dscp.c
xt_ecn.c
xt_esp.c
xt_hashlimit.c
xt_helper.c
xt_hl.c
xt_iprange.c
xt_ipvs.c
xt_length.c
xt_limit.c
xt_mac.c
xt_mark.c
xt_multiport.c
xt_nat.c
xt_nfacct.c
xt_osf.c
xt_owner.c
xt_physdev.c
xt_pkttype.c
xt_policy.c
xt_quota.c
xt_rateest.c
xt_realm.c
xt_recent.c
xt_repldata.h
xt_sctp.c
xt_set.c
xt_socket.c
xt_state.c
xt_statistic.c
xt_string.c
xt_tcpmss.c
xt_tcpudp.c
xt_time.c
xt_u32.c
netlabel
netlink
netrom
nfc
openvswitch
packet
phonet
rds
rfkill
rose
rxrpc
sched
sctp
sunrpc
tipc
unix
vmw_vsock
wimax
wireless
x25
xfrm
Kconfig
Makefile
compat.c
nonet.c
socket.c
sysctl_net.c
samples
scripts
security
sound
tools
usr
virt
.gitignore
.mailmap
COPYING
CREDITS
Kbuild
Kconfig
MAINTAINERS
Makefile
README
REPORTING-BUGS
Breadcrumbs
linux
/
net
/
netfilter
/
nft_hash.c
Copy path
Blame
Blame
Latest commit
Florian Westphal
and
Pablo Neira Ayuso
netfilter: avoid get_random_bytes calls
Dec 20, 2013
a42b99a
·
Dec 20, 2013
History
History
231 lines (193 loc) · 5.37 KB
Breadcrumbs
linux
/
net
/
netfilter
/
nft_hash.c
Top
File metadata and controls
Code
Blame
231 lines (193 loc) · 5.37 KB
Raw
/* * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. * * Development of this code funded by Astaro AG (http://www.astaro.com/) */ #include <linux/kernel.h> #include <linux/init.h> #include <linux/module.h> #include <linux/list.h> #include <linux/jhash.h> #include <linux/netlink.h> #include <linux/netfilter.h> #include <linux/netfilter/nf_tables.h> #include <net/netfilter/nf_tables.h> struct nft_hash { struct hlist_head *hash; unsigned int hsize; }; struct nft_hash_elem { struct hlist_node hnode; struct nft_data key; struct nft_data data[]; }; static u32 nft_hash_rnd __read_mostly; static bool nft_hash_rnd_initted __read_mostly; static unsigned int nft_hash_data(const struct nft_data *data, unsigned int hsize, unsigned int len) { unsigned int h; h = jhash(data->data, len, nft_hash_rnd); return ((u64)h * hsize) >> 32; } static bool nft_hash_lookup(const struct nft_set *set, const struct nft_data *key, struct nft_data *data) { const struct nft_hash *priv = nft_set_priv(set); const struct nft_hash_elem *he; unsigned int h; h = nft_hash_data(key, priv->hsize, set->klen); hlist_for_each_entry(he, &priv->hash[h], hnode) { if (nft_data_cmp(&he->key, key, set->klen)) continue; if (set->flags & NFT_SET_MAP) nft_data_copy(data, he->data); return true; } return false; } static void nft_hash_elem_destroy(const struct nft_set *set, struct nft_hash_elem *he) { nft_data_uninit(&he->key, NFT_DATA_VALUE); if (set->flags & NFT_SET_MAP) nft_data_uninit(he->data, set->dtype); kfree(he); } static int nft_hash_insert(const struct nft_set *set, const struct nft_set_elem *elem) { struct nft_hash *priv = nft_set_priv(set); struct nft_hash_elem *he; unsigned int size, h; if (elem->flags != 0) return -EINVAL; size = sizeof(*he); if (set->flags & NFT_SET_MAP) size += sizeof(he->data[0]); he = kzalloc(size, GFP_KERNEL); if (he == NULL) return -ENOMEM; nft_data_copy(&he->key, &elem->key); if (set->flags & NFT_SET_MAP) nft_data_copy(he->data, &elem->data); h = nft_hash_data(&he->key, priv->hsize, set->klen); hlist_add_head_rcu(&he->hnode, &priv->hash[h]); return 0; } static void nft_hash_remove(const struct nft_set *set, const struct nft_set_elem *elem) { struct nft_hash_elem *he = elem->cookie; hlist_del_rcu(&he->hnode); kfree(he); } static int nft_hash_get(const struct nft_set *set, struct nft_set_elem *elem) { const struct nft_hash *priv = nft_set_priv(set); struct nft_hash_elem *he; unsigned int h; h = nft_hash_data(&elem->key, priv->hsize, set->klen); hlist_for_each_entry(he, &priv->hash[h], hnode) { if (nft_data_cmp(&he->key, &elem->key, set->klen)) continue; elem->cookie = he; elem->flags = 0; if (set->flags & NFT_SET_MAP) nft_data_copy(&elem->data, he->data); return 0; } return -ENOENT; } static void nft_hash_walk(const struct nft_ctx *ctx, const struct nft_set *set, struct nft_set_iter *iter) { const struct nft_hash *priv = nft_set_priv(set); const struct nft_hash_elem *he; struct nft_set_elem elem; unsigned int i; for (i = 0; i < priv->hsize; i++) { hlist_for_each_entry(he, &priv->hash[i], hnode) { if (iter->count < iter->skip) goto cont; memcpy(&elem.key, &he->key, sizeof(elem.key)); if (set->flags & NFT_SET_MAP) memcpy(&elem.data, he->data, sizeof(elem.data)); elem.flags = 0; iter->err = iter->fn(ctx, set, iter, &elem); if (iter->err < 0) return; cont: iter->count++; } } } static unsigned int nft_hash_privsize(const struct nlattr * const nla[]) { return sizeof(struct nft_hash); } static int nft_hash_init(const struct nft_set *set, const struct nlattr * const tb[]) { struct nft_hash *priv = nft_set_priv(set); unsigned int cnt, i; if (unlikely(!nft_hash_rnd_initted)) { nft_hash_rnd = prandom_u32(); nft_hash_rnd_initted = true; } /* Aim for a load factor of 0.75 */ // FIXME: temporarily broken until we have set descriptions cnt = 100; cnt = cnt * 4 / 3; priv->hash = kcalloc(cnt, sizeof(struct hlist_head), GFP_KERNEL); if (priv->hash == NULL) return -ENOMEM; priv->hsize = cnt; for (i = 0; i < cnt; i++) INIT_HLIST_HEAD(&priv->hash[i]); return 0; } static void nft_hash_destroy(const struct nft_set *set) { const struct nft_hash *priv = nft_set_priv(set); const struct hlist_node *next; struct nft_hash_elem *elem; unsigned int i; for (i = 0; i < priv->hsize; i++) { hlist_for_each_entry_safe(elem, next, &priv->hash[i], hnode) { hlist_del(&elem->hnode); nft_hash_elem_destroy(set, elem); } } kfree(priv->hash); } static struct nft_set_ops nft_hash_ops __read_mostly = { .privsize = nft_hash_privsize, .init = nft_hash_init, .destroy = nft_hash_destroy, .get = nft_hash_get, .insert = nft_hash_insert, .remove = nft_hash_remove, .lookup = nft_hash_lookup, .walk = nft_hash_walk, .features = NFT_SET_MAP, .owner = THIS_MODULE, }; static int __init nft_hash_module_init(void) { return nft_register_set(&nft_hash_ops); } static void __exit nft_hash_module_exit(void) { nft_unregister_set(&nft_hash_ops); } module_init(nft_hash_module_init); module_exit(nft_hash_module_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); MODULE_ALIAS_NFT_SET();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
You can’t perform that action at this time.