Patrick McHardy | c4b8851 | 2006-03-20 18:03:40 -0800 | [diff] [blame] | 1 | /* IP tables module for matching IPsec policy |
| 2 | * |
| 3 | * Copyright (c) 2004,2005 Patrick McHardy, <kaber@trash.net> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation. |
| 8 | */ |
Jan Engelhardt | 8bee4ba | 2010-03-17 16:04:40 +0100 | [diff] [blame] | 9 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
Patrick McHardy | c4b8851 | 2006-03-20 18:03:40 -0800 | [diff] [blame] | 10 | #include <linux/kernel.h> |
Patrick McHardy | c4b8851 | 2006-03-20 18:03:40 -0800 | [diff] [blame] | 11 | #include <linux/module.h> |
| 12 | #include <linux/skbuff.h> |
| 13 | #include <linux/init.h> |
| 14 | #include <net/xfrm.h> |
| 15 | |
Jan Engelhardt | 917b6fb | 2008-01-14 23:42:06 -0800 | [diff] [blame] | 16 | #include <linux/netfilter.h> |
Patrick McHardy | c4b8851 | 2006-03-20 18:03:40 -0800 | [diff] [blame] | 17 | #include <linux/netfilter/xt_policy.h> |
| 18 | #include <linux/netfilter/x_tables.h> |
| 19 | |
| 20 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); |
Jan Engelhardt | 2ae15b6 | 2008-01-14 23:42:28 -0800 | [diff] [blame] | 21 | MODULE_DESCRIPTION("Xtables: IPsec policy match"); |
Patrick McHardy | c4b8851 | 2006-03-20 18:03:40 -0800 | [diff] [blame] | 22 | MODULE_LICENSE("GPL"); |
| 23 | |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 24 | static inline bool |
Jan Engelhardt | 917b6fb | 2008-01-14 23:42:06 -0800 | [diff] [blame] | 25 | xt_addr_cmp(const union nf_inet_addr *a1, const union nf_inet_addr *m, |
| 26 | const union nf_inet_addr *a2, unsigned short family) |
Patrick McHardy | c4b8851 | 2006-03-20 18:03:40 -0800 | [diff] [blame] | 27 | { |
| 28 | switch (family) { |
Jan Engelhardt | ee999d8 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 29 | case NFPROTO_IPV4: |
Jan Engelhardt | 917b6fb | 2008-01-14 23:42:06 -0800 | [diff] [blame] | 30 | return ((a1->ip ^ a2->ip) & m->ip) == 0; |
Jan Engelhardt | ee999d8 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 31 | case NFPROTO_IPV6: |
Jan Engelhardt | 917b6fb | 2008-01-14 23:42:06 -0800 | [diff] [blame] | 32 | return ipv6_masked_addr_cmp(&a1->in6, &m->in6, &a2->in6) == 0; |
Patrick McHardy | c4b8851 | 2006-03-20 18:03:40 -0800 | [diff] [blame] | 33 | } |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 34 | return false; |
Patrick McHardy | c4b8851 | 2006-03-20 18:03:40 -0800 | [diff] [blame] | 35 | } |
| 36 | |
Ilpo Järvinen | b9aed45 | 2008-01-12 21:26:31 -0800 | [diff] [blame] | 37 | static bool |
Jan Engelhardt | a47362a | 2007-07-07 22:16:55 -0700 | [diff] [blame] | 38 | match_xfrm_state(const struct xfrm_state *x, const struct xt_policy_elem *e, |
Patrick McHardy | c4b8851 | 2006-03-20 18:03:40 -0800 | [diff] [blame] | 39 | unsigned short family) |
| 40 | { |
| 41 | #define MATCH_ADDR(x,y,z) (!e->match.x || \ |
Jan Engelhardt | 917b6fb | 2008-01-14 23:42:06 -0800 | [diff] [blame] | 42 | (xt_addr_cmp(&e->x, &e->y, (const union nf_inet_addr *)(z), family) \ |
Patrick McHardy | c4b8851 | 2006-03-20 18:03:40 -0800 | [diff] [blame] | 43 | ^ e->invert.x)) |
| 44 | #define MATCH(x,y) (!e->match.x || ((e->x == (y)) ^ e->invert.x)) |
| 45 | |
Jan Engelhardt | 917b6fb | 2008-01-14 23:42:06 -0800 | [diff] [blame] | 46 | return MATCH_ADDR(saddr, smask, &x->props.saddr) && |
| 47 | MATCH_ADDR(daddr, dmask, &x->id.daddr) && |
Patrick McHardy | c4b8851 | 2006-03-20 18:03:40 -0800 | [diff] [blame] | 48 | MATCH(proto, x->id.proto) && |
| 49 | MATCH(mode, x->props.mode) && |
| 50 | MATCH(spi, x->id.spi) && |
| 51 | MATCH(reqid, x->props.reqid); |
| 52 | } |
| 53 | |
| 54 | static int |
| 55 | match_policy_in(const struct sk_buff *skb, const struct xt_policy_info *info, |
| 56 | unsigned short family) |
| 57 | { |
| 58 | const struct xt_policy_elem *e; |
Jan Engelhardt | a47362a | 2007-07-07 22:16:55 -0700 | [diff] [blame] | 59 | const struct sec_path *sp = skb->sp; |
Patrick McHardy | c4b8851 | 2006-03-20 18:03:40 -0800 | [diff] [blame] | 60 | int strict = info->flags & XT_POLICY_MATCH_STRICT; |
| 61 | int i, pos; |
| 62 | |
| 63 | if (sp == NULL) |
| 64 | return -1; |
| 65 | if (strict && info->len != sp->len) |
| 66 | return 0; |
| 67 | |
| 68 | for (i = sp->len - 1; i >= 0; i--) { |
| 69 | pos = strict ? i - sp->len + 1 : 0; |
| 70 | if (pos >= info->len) |
| 71 | return 0; |
| 72 | e = &info->pol[pos]; |
| 73 | |
Herbert Xu | dbe5b4a | 2006-04-01 00:54:16 -0800 | [diff] [blame] | 74 | if (match_xfrm_state(sp->xvec[i], e, family)) { |
Patrick McHardy | c4b8851 | 2006-03-20 18:03:40 -0800 | [diff] [blame] | 75 | if (!strict) |
| 76 | return 1; |
| 77 | } else if (strict) |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | return strict ? 1 : 0; |
| 82 | } |
| 83 | |
| 84 | static int |
| 85 | match_policy_out(const struct sk_buff *skb, const struct xt_policy_info *info, |
| 86 | unsigned short family) |
| 87 | { |
| 88 | const struct xt_policy_elem *e; |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 89 | const struct dst_entry *dst = skb_dst(skb); |
Patrick McHardy | c4b8851 | 2006-03-20 18:03:40 -0800 | [diff] [blame] | 90 | int strict = info->flags & XT_POLICY_MATCH_STRICT; |
| 91 | int i, pos; |
| 92 | |
| 93 | if (dst->xfrm == NULL) |
| 94 | return -1; |
| 95 | |
| 96 | for (i = 0; dst && dst->xfrm; dst = dst->child, i++) { |
| 97 | pos = strict ? i : 0; |
| 98 | if (pos >= info->len) |
| 99 | return 0; |
| 100 | e = &info->pol[pos]; |
| 101 | |
| 102 | if (match_xfrm_state(dst->xfrm, e, family)) { |
| 103 | if (!strict) |
| 104 | return 1; |
| 105 | } else if (strict) |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | return strict ? i == info->len : 0; |
| 110 | } |
| 111 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 112 | static bool |
Jan Engelhardt | 62fc805 | 2009-07-07 20:42:08 +0200 | [diff] [blame] | 113 | policy_mt(const struct sk_buff *skb, struct xt_action_param *par) |
Patrick McHardy | c4b8851 | 2006-03-20 18:03:40 -0800 | [diff] [blame] | 114 | { |
Jan Engelhardt | f7108a20 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 115 | const struct xt_policy_info *info = par->matchinfo; |
Patrick McHardy | c4b8851 | 2006-03-20 18:03:40 -0800 | [diff] [blame] | 116 | int ret; |
| 117 | |
| 118 | if (info->flags & XT_POLICY_MATCH_IN) |
Jan Engelhardt | aa5fa31 | 2010-03-18 00:44:52 +0100 | [diff] [blame] | 119 | ret = match_policy_in(skb, info, par->family); |
Patrick McHardy | c4b8851 | 2006-03-20 18:03:40 -0800 | [diff] [blame] | 120 | else |
Jan Engelhardt | aa5fa31 | 2010-03-18 00:44:52 +0100 | [diff] [blame] | 121 | ret = match_policy_out(skb, info, par->family); |
Patrick McHardy | c4b8851 | 2006-03-20 18:03:40 -0800 | [diff] [blame] | 122 | |
| 123 | if (ret < 0) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 124 | ret = info->flags & XT_POLICY_MATCH_NONE ? true : false; |
Patrick McHardy | c4b8851 | 2006-03-20 18:03:40 -0800 | [diff] [blame] | 125 | else if (info->flags & XT_POLICY_MATCH_NONE) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 126 | ret = false; |
Patrick McHardy | c4b8851 | 2006-03-20 18:03:40 -0800 | [diff] [blame] | 127 | |
| 128 | return ret; |
| 129 | } |
| 130 | |
Jan Engelhardt | b0f3845 | 2010-03-19 17:16:42 +0100 | [diff] [blame] | 131 | static int policy_mt_check(const struct xt_mtchk_param *par) |
Patrick McHardy | c4b8851 | 2006-03-20 18:03:40 -0800 | [diff] [blame] | 132 | { |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 133 | const struct xt_policy_info *info = par->matchinfo; |
Patrick McHardy | c4b8851 | 2006-03-20 18:03:40 -0800 | [diff] [blame] | 134 | |
| 135 | if (!(info->flags & (XT_POLICY_MATCH_IN|XT_POLICY_MATCH_OUT))) { |
Jan Engelhardt | 8bee4ba | 2010-03-17 16:04:40 +0100 | [diff] [blame] | 136 | pr_info("neither incoming nor outgoing policy selected\n"); |
Jan Engelhardt | bd414ee | 2010-03-23 16:35:56 +0100 | [diff] [blame] | 137 | return -EINVAL; |
Patrick McHardy | c4b8851 | 2006-03-20 18:03:40 -0800 | [diff] [blame] | 138 | } |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 139 | if (par->hook_mask & ((1 << NF_INET_PRE_ROUTING) | |
| 140 | (1 << NF_INET_LOCAL_IN)) && info->flags & XT_POLICY_MATCH_OUT) { |
Jan Engelhardt | 8bee4ba | 2010-03-17 16:04:40 +0100 | [diff] [blame] | 141 | pr_info("output policy not valid in PREROUTING and INPUT\n"); |
Jan Engelhardt | bd414ee | 2010-03-23 16:35:56 +0100 | [diff] [blame] | 142 | return -EINVAL; |
Patrick McHardy | c4b8851 | 2006-03-20 18:03:40 -0800 | [diff] [blame] | 143 | } |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 144 | if (par->hook_mask & ((1 << NF_INET_POST_ROUTING) | |
| 145 | (1 << NF_INET_LOCAL_OUT)) && info->flags & XT_POLICY_MATCH_IN) { |
Jan Engelhardt | 8bee4ba | 2010-03-17 16:04:40 +0100 | [diff] [blame] | 146 | pr_info("input policy not valid in POSTROUTING and OUTPUT\n"); |
Jan Engelhardt | bd414ee | 2010-03-23 16:35:56 +0100 | [diff] [blame] | 147 | return -EINVAL; |
Patrick McHardy | c4b8851 | 2006-03-20 18:03:40 -0800 | [diff] [blame] | 148 | } |
| 149 | if (info->len > XT_POLICY_MAX_ELEM) { |
Jan Engelhardt | 8bee4ba | 2010-03-17 16:04:40 +0100 | [diff] [blame] | 150 | pr_info("too many policy elements\n"); |
Jan Engelhardt | bd414ee | 2010-03-23 16:35:56 +0100 | [diff] [blame] | 151 | return -EINVAL; |
Patrick McHardy | c4b8851 | 2006-03-20 18:03:40 -0800 | [diff] [blame] | 152 | } |
Jan Engelhardt | bd414ee | 2010-03-23 16:35:56 +0100 | [diff] [blame] | 153 | return 0; |
Patrick McHardy | c4b8851 | 2006-03-20 18:03:40 -0800 | [diff] [blame] | 154 | } |
| 155 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 156 | static struct xt_match policy_mt_reg[] __read_mostly = { |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 157 | { |
| 158 | .name = "policy", |
Jan Engelhardt | ee999d8 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 159 | .family = NFPROTO_IPV4, |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 160 | .checkentry = policy_mt_check, |
| 161 | .match = policy_mt, |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 162 | .matchsize = sizeof(struct xt_policy_info), |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 163 | .me = THIS_MODULE, |
| 164 | }, |
| 165 | { |
| 166 | .name = "policy", |
Jan Engelhardt | ee999d8 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 167 | .family = NFPROTO_IPV6, |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 168 | .checkentry = policy_mt_check, |
| 169 | .match = policy_mt, |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 170 | .matchsize = sizeof(struct xt_policy_info), |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 171 | .me = THIS_MODULE, |
| 172 | }, |
Patrick McHardy | c4b8851 | 2006-03-20 18:03:40 -0800 | [diff] [blame] | 173 | }; |
| 174 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 175 | static int __init policy_mt_init(void) |
Patrick McHardy | c4b8851 | 2006-03-20 18:03:40 -0800 | [diff] [blame] | 176 | { |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 177 | return xt_register_matches(policy_mt_reg, ARRAY_SIZE(policy_mt_reg)); |
Patrick McHardy | c4b8851 | 2006-03-20 18:03:40 -0800 | [diff] [blame] | 178 | } |
| 179 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 180 | static void __exit policy_mt_exit(void) |
Patrick McHardy | c4b8851 | 2006-03-20 18:03:40 -0800 | [diff] [blame] | 181 | { |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 182 | xt_unregister_matches(policy_mt_reg, ARRAY_SIZE(policy_mt_reg)); |
Patrick McHardy | c4b8851 | 2006-03-20 18:03:40 -0800 | [diff] [blame] | 183 | } |
| 184 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 185 | module_init(policy_mt_init); |
| 186 | module_exit(policy_mt_exit); |
Patrick McHardy | c4b8851 | 2006-03-20 18:03:40 -0800 | [diff] [blame] | 187 | MODULE_ALIAS("ipt_policy"); |
| 188 | MODULE_ALIAS("ip6t_policy"); |