Florian Westphal | c539f01 | 2013-01-11 06:30:44 +0000 | [diff] [blame] | 1 | /* |
| 2 | * test/set flag bits stored in conntrack extension area. |
| 3 | * |
| 4 | * (C) 2013 Astaro GmbH & Co KG |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | */ |
| 10 | |
Florian Westphal | c539f01 | 2013-01-11 06:30:44 +0000 | [diff] [blame] | 11 | #include <linux/export.h> |
Florian Westphal | c539f01 | 2013-01-11 06:30:44 +0000 | [diff] [blame] | 12 | #include <linux/types.h> |
Florian Westphal | c539f01 | 2013-01-11 06:30:44 +0000 | [diff] [blame] | 13 | |
| 14 | #include <net/netfilter/nf_conntrack_ecache.h> |
| 15 | #include <net/netfilter/nf_conntrack_labels.h> |
| 16 | |
Joe Stringer | 86ca02e | 2015-08-26 11:31:51 -0700 | [diff] [blame] | 17 | static spinlock_t nf_connlabels_lock; |
| 18 | |
Florian Westphal | 5a8145f | 2016-04-12 18:14:24 +0200 | [diff] [blame] | 19 | static int replace_u32(u32 *address, u32 mask, u32 new) |
Florian Westphal | 9b21f6a | 2013-01-11 06:30:46 +0000 | [diff] [blame] | 20 | { |
| 21 | u32 old, tmp; |
| 22 | |
| 23 | do { |
| 24 | old = *address; |
| 25 | tmp = (old & mask) ^ new; |
Florian Westphal | 5a8145f | 2016-04-12 18:14:24 +0200 | [diff] [blame] | 26 | if (old == tmp) |
| 27 | return 0; |
Florian Westphal | 9b21f6a | 2013-01-11 06:30:46 +0000 | [diff] [blame] | 28 | } while (cmpxchg(address, old, tmp) != old); |
Florian Westphal | 5a8145f | 2016-04-12 18:14:24 +0200 | [diff] [blame] | 29 | |
| 30 | return 1; |
Florian Westphal | 9b21f6a | 2013-01-11 06:30:46 +0000 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | int nf_connlabels_replace(struct nf_conn *ct, |
| 34 | const u32 *data, |
| 35 | const u32 *mask, unsigned int words32) |
| 36 | { |
| 37 | struct nf_conn_labels *labels; |
| 38 | unsigned int size, i; |
Florian Westphal | 5a8145f | 2016-04-12 18:14:24 +0200 | [diff] [blame] | 39 | int changed = 0; |
Florian Westphal | 9b21f6a | 2013-01-11 06:30:46 +0000 | [diff] [blame] | 40 | u32 *dst; |
| 41 | |
| 42 | labels = nf_ct_labels_find(ct); |
| 43 | if (!labels) |
| 44 | return -ENOSPC; |
| 45 | |
Florian Westphal | 2301401 | 2016-07-21 12:51:16 +0200 | [diff] [blame] | 46 | size = sizeof(labels->bits); |
Florian Westphal | 9b21f6a | 2013-01-11 06:30:46 +0000 | [diff] [blame] | 47 | if (size < (words32 * sizeof(u32))) |
| 48 | words32 = size / sizeof(u32); |
| 49 | |
| 50 | dst = (u32 *) labels->bits; |
Florian Westphal | 5a8145f | 2016-04-12 18:14:24 +0200 | [diff] [blame] | 51 | for (i = 0; i < words32; i++) |
| 52 | changed |= replace_u32(&dst[i], mask ? ~mask[i] : 0, data[i]); |
Florian Westphal | 9b21f6a | 2013-01-11 06:30:46 +0000 | [diff] [blame] | 53 | |
| 54 | size /= sizeof(u32); |
| 55 | for (i = words32; i < size; i++) /* pad */ |
| 56 | replace_u32(&dst[i], 0, 0); |
| 57 | |
Florian Westphal | 5a8145f | 2016-04-12 18:14:24 +0200 | [diff] [blame] | 58 | if (changed) |
| 59 | nf_conntrack_event_cache(IPCT_LABEL, ct); |
Florian Westphal | 9b21f6a | 2013-01-11 06:30:46 +0000 | [diff] [blame] | 60 | return 0; |
| 61 | } |
| 62 | EXPORT_SYMBOL_GPL(nf_connlabels_replace); |
Florian Westphal | 9b21f6a | 2013-01-11 06:30:46 +0000 | [diff] [blame] | 63 | |
Florian Westphal | adff6c6 | 2016-04-12 18:14:25 +0200 | [diff] [blame] | 64 | int nf_connlabels_get(struct net *net, unsigned int bits) |
Joe Stringer | 86ca02e | 2015-08-26 11:31:51 -0700 | [diff] [blame] | 65 | { |
Florian Westphal | 2301401 | 2016-07-21 12:51:16 +0200 | [diff] [blame] | 66 | if (BIT_WORD(bits) >= NF_CT_LABELS_MAX_SIZE / sizeof(long)) |
Joe Stringer | 86ca02e | 2015-08-26 11:31:51 -0700 | [diff] [blame] | 67 | return -ERANGE; |
| 68 | |
Joe Stringer | 86ca02e | 2015-08-26 11:31:51 -0700 | [diff] [blame] | 69 | spin_lock(&nf_connlabels_lock); |
| 70 | net->ct.labels_used++; |
Joe Stringer | 86ca02e | 2015-08-26 11:31:51 -0700 | [diff] [blame] | 71 | spin_unlock(&nf_connlabels_lock); |
| 72 | |
| 73 | return 0; |
| 74 | } |
| 75 | EXPORT_SYMBOL_GPL(nf_connlabels_get); |
| 76 | |
| 77 | void nf_connlabels_put(struct net *net) |
| 78 | { |
| 79 | spin_lock(&nf_connlabels_lock); |
| 80 | net->ct.labels_used--; |
Joe Stringer | 86ca02e | 2015-08-26 11:31:51 -0700 | [diff] [blame] | 81 | spin_unlock(&nf_connlabels_lock); |
| 82 | } |
| 83 | EXPORT_SYMBOL_GPL(nf_connlabels_put); |
| 84 | |
Florian Westphal | c539f01 | 2013-01-11 06:30:44 +0000 | [diff] [blame] | 85 | static struct nf_ct_ext_type labels_extend __read_mostly = { |
| 86 | .len = sizeof(struct nf_conn_labels), |
| 87 | .align = __alignof__(struct nf_conn_labels), |
| 88 | .id = NF_CT_EXT_LABELS, |
| 89 | }; |
| 90 | |
Gao feng | 5f69b8f | 2013-01-21 22:10:31 +0000 | [diff] [blame] | 91 | int nf_conntrack_labels_init(void) |
Florian Westphal | c539f01 | 2013-01-11 06:30:44 +0000 | [diff] [blame] | 92 | { |
Florian Westphal | adff6c6 | 2016-04-12 18:14:25 +0200 | [diff] [blame] | 93 | BUILD_BUG_ON(NF_CT_LABELS_MAX_SIZE / sizeof(long) >= U8_MAX); |
| 94 | |
Joe Stringer | 86ca02e | 2015-08-26 11:31:51 -0700 | [diff] [blame] | 95 | spin_lock_init(&nf_connlabels_lock); |
Gao feng | 5f69b8f | 2013-01-21 22:10:31 +0000 | [diff] [blame] | 96 | return nf_ct_extend_register(&labels_extend); |
Florian Westphal | c539f01 | 2013-01-11 06:30:44 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Gao feng | 5f69b8f | 2013-01-21 22:10:31 +0000 | [diff] [blame] | 99 | void nf_conntrack_labels_fini(void) |
Florian Westphal | c539f01 | 2013-01-11 06:30:44 +0000 | [diff] [blame] | 100 | { |
Gao feng | 5f69b8f | 2013-01-21 22:10:31 +0000 | [diff] [blame] | 101 | nf_ct_extend_unregister(&labels_extend); |
Florian Westphal | c539f01 | 2013-01-11 06:30:44 +0000 | [diff] [blame] | 102 | } |