Florian Westphal | c539f01 | 2013-01-11 06:30:44 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) 2013 Astaro GmbH & Co KG |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 as |
| 6 | * published by the Free Software Foundation. |
| 7 | */ |
| 8 | |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/skbuff.h> |
| 11 | #include <net/netfilter/nf_conntrack.h> |
Florian Westphal | 857ed31 | 2016-07-21 12:51:17 +0200 | [diff] [blame] | 12 | #include <net/netfilter/nf_conntrack_ecache.h> |
Florian Westphal | c539f01 | 2013-01-11 06:30:44 +0000 | [diff] [blame] | 13 | #include <net/netfilter/nf_conntrack_labels.h> |
| 14 | #include <linux/netfilter/x_tables.h> |
| 15 | |
| 16 | MODULE_LICENSE("GPL"); |
| 17 | MODULE_AUTHOR("Florian Westphal <fw@strlen.de>"); |
| 18 | MODULE_DESCRIPTION("Xtables: add/match connection trackling labels"); |
| 19 | MODULE_ALIAS("ipt_connlabel"); |
| 20 | MODULE_ALIAS("ip6t_connlabel"); |
| 21 | |
| 22 | static bool |
| 23 | connlabel_mt(const struct sk_buff *skb, struct xt_action_param *par) |
| 24 | { |
| 25 | const struct xt_connlabel_mtinfo *info = par->matchinfo; |
| 26 | enum ip_conntrack_info ctinfo; |
Florian Westphal | 857ed31 | 2016-07-21 12:51:17 +0200 | [diff] [blame] | 27 | struct nf_conn_labels *labels; |
Florian Westphal | c539f01 | 2013-01-11 06:30:44 +0000 | [diff] [blame] | 28 | struct nf_conn *ct; |
| 29 | bool invert = info->options & XT_CONNLABEL_OP_INVERT; |
| 30 | |
| 31 | ct = nf_ct_get(skb, &ctinfo); |
Florian Westphal | ab8bc7e | 2017-04-14 20:31:09 +0200 | [diff] [blame] | 32 | if (ct == NULL) |
Florian Westphal | c539f01 | 2013-01-11 06:30:44 +0000 | [diff] [blame] | 33 | return invert; |
| 34 | |
Florian Westphal | 857ed31 | 2016-07-21 12:51:17 +0200 | [diff] [blame] | 35 | labels = nf_ct_labels_find(ct); |
| 36 | if (!labels) |
| 37 | return invert; |
Florian Westphal | c539f01 | 2013-01-11 06:30:44 +0000 | [diff] [blame] | 38 | |
Florian Westphal | 857ed31 | 2016-07-21 12:51:17 +0200 | [diff] [blame] | 39 | if (test_bit(info->bit, labels->bits)) |
| 40 | return !invert; |
| 41 | |
| 42 | if (info->options & XT_CONNLABEL_OP_SET) { |
| 43 | if (!test_and_set_bit(info->bit, labels->bits)) |
| 44 | nf_conntrack_event_cache(IPCT_LABEL, ct); |
| 45 | |
| 46 | return !invert; |
| 47 | } |
| 48 | |
| 49 | return invert; |
Florian Westphal | c539f01 | 2013-01-11 06:30:44 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | static int connlabel_mt_check(const struct xt_mtchk_param *par) |
| 53 | { |
| 54 | const int options = XT_CONNLABEL_OP_INVERT | |
| 55 | XT_CONNLABEL_OP_SET; |
| 56 | struct xt_connlabel_mtinfo *info = par->matchinfo; |
| 57 | int ret; |
Florian Westphal | c539f01 | 2013-01-11 06:30:44 +0000 | [diff] [blame] | 58 | |
| 59 | if (info->options & ~options) { |
Florian Westphal | b260664 | 2018-02-09 15:52:07 +0100 | [diff] [blame] | 60 | pr_info_ratelimited("Unknown options in mask %x\n", |
| 61 | info->options); |
Florian Westphal | c539f01 | 2013-01-11 06:30:44 +0000 | [diff] [blame] | 62 | return -EINVAL; |
| 63 | } |
| 64 | |
Florian Westphal | ecb2421 | 2016-11-15 21:36:40 +0100 | [diff] [blame] | 65 | ret = nf_ct_netns_get(par->net, par->family); |
Florian Westphal | c539f01 | 2013-01-11 06:30:44 +0000 | [diff] [blame] | 66 | if (ret < 0) { |
Florian Westphal | b260664 | 2018-02-09 15:52:07 +0100 | [diff] [blame] | 67 | pr_info_ratelimited("cannot load conntrack support for proto=%u\n", |
| 68 | par->family); |
Florian Westphal | c539f01 | 2013-01-11 06:30:44 +0000 | [diff] [blame] | 69 | return ret; |
| 70 | } |
| 71 | |
Florian Westphal | adff6c6 | 2016-04-12 18:14:25 +0200 | [diff] [blame] | 72 | ret = nf_connlabels_get(par->net, info->bit); |
Joe Stringer | 86ca02e | 2015-08-26 11:31:51 -0700 | [diff] [blame] | 73 | if (ret < 0) |
Florian Westphal | ecb2421 | 2016-11-15 21:36:40 +0100 | [diff] [blame] | 74 | nf_ct_netns_put(par->net, par->family); |
Florian Westphal | c539f01 | 2013-01-11 06:30:44 +0000 | [diff] [blame] | 75 | return ret; |
| 76 | } |
| 77 | |
| 78 | static void connlabel_mt_destroy(const struct xt_mtdtor_param *par) |
| 79 | { |
Joe Stringer | 86ca02e | 2015-08-26 11:31:51 -0700 | [diff] [blame] | 80 | nf_connlabels_put(par->net); |
Florian Westphal | ecb2421 | 2016-11-15 21:36:40 +0100 | [diff] [blame] | 81 | nf_ct_netns_put(par->net, par->family); |
Florian Westphal | c539f01 | 2013-01-11 06:30:44 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | static struct xt_match connlabels_mt_reg __read_mostly = { |
| 85 | .name = "connlabel", |
| 86 | .family = NFPROTO_UNSPEC, |
| 87 | .checkentry = connlabel_mt_check, |
| 88 | .match = connlabel_mt, |
| 89 | .matchsize = sizeof(struct xt_connlabel_mtinfo), |
| 90 | .destroy = connlabel_mt_destroy, |
| 91 | .me = THIS_MODULE, |
| 92 | }; |
| 93 | |
| 94 | static int __init connlabel_mt_init(void) |
| 95 | { |
| 96 | return xt_register_match(&connlabels_mt_reg); |
| 97 | } |
| 98 | |
| 99 | static void __exit connlabel_mt_exit(void) |
| 100 | { |
| 101 | xt_unregister_match(&connlabels_mt_reg); |
| 102 | } |
| 103 | |
| 104 | module_init(connlabel_mt_init); |
| 105 | module_exit(connlabel_mt_exit); |