Patrick McHardy | e16a8f0 | 2006-01-06 23:06:48 -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 | */ |
| 9 | |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/config.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/skbuff.h> |
| 14 | #include <linux/init.h> |
| 15 | #include <net/xfrm.h> |
| 16 | |
| 17 | #include <linux/netfilter_ipv6.h> |
| 18 | #include <linux/netfilter_ipv6/ip6_tables.h> |
| 19 | #include <linux/netfilter_ipv6/ip6t_policy.h> |
| 20 | |
| 21 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); |
| 22 | MODULE_DESCRIPTION("IPtables IPsec policy matching module"); |
| 23 | MODULE_LICENSE("GPL"); |
| 24 | |
| 25 | |
| 26 | static inline int |
| 27 | match_xfrm_state(struct xfrm_state *x, const struct ip6t_policy_elem *e) |
| 28 | { |
| 29 | #define MATCH_ADDR(x,y,z) (!e->match.x || \ |
| 30 | ((ip6_masked_addrcmp((z), &e->x, &e->y)) == 0) ^ e->invert.x) |
| 31 | #define MATCH(x,y) (!e->match.x || ((e->x == (y)) ^ e->invert.x)) |
| 32 | |
| 33 | return MATCH_ADDR(saddr, smask, (struct in6_addr *)&x->props.saddr.a6) && |
| 34 | MATCH_ADDR(daddr, dmask, (struct in6_addr *)&x->id.daddr.a6) && |
| 35 | MATCH(proto, x->id.proto) && |
| 36 | MATCH(mode, x->props.mode) && |
| 37 | MATCH(spi, x->id.spi) && |
| 38 | MATCH(reqid, x->props.reqid); |
| 39 | } |
| 40 | |
| 41 | static int |
| 42 | match_policy_in(const struct sk_buff *skb, const struct ip6t_policy_info *info) |
| 43 | { |
| 44 | const struct ip6t_policy_elem *e; |
| 45 | struct sec_path *sp = skb->sp; |
| 46 | int strict = info->flags & IP6T_POLICY_MATCH_STRICT; |
| 47 | int i, pos; |
| 48 | |
| 49 | if (sp == NULL) |
| 50 | return -1; |
| 51 | if (strict && info->len != sp->len) |
| 52 | return 0; |
| 53 | |
| 54 | for (i = sp->len - 1; i >= 0; i--) { |
| 55 | pos = strict ? i - sp->len + 1 : 0; |
| 56 | if (pos >= info->len) |
| 57 | return 0; |
| 58 | e = &info->pol[pos]; |
| 59 | |
| 60 | if (match_xfrm_state(sp->x[i].xvec, e)) { |
| 61 | if (!strict) |
| 62 | return 1; |
| 63 | } else if (strict) |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | return strict ? 1 : 0; |
| 68 | } |
| 69 | |
| 70 | static int |
| 71 | match_policy_out(const struct sk_buff *skb, const struct ip6t_policy_info *info) |
| 72 | { |
| 73 | const struct ip6t_policy_elem *e; |
| 74 | struct dst_entry *dst = skb->dst; |
| 75 | int strict = info->flags & IP6T_POLICY_MATCH_STRICT; |
| 76 | int i, pos; |
| 77 | |
| 78 | if (dst->xfrm == NULL) |
| 79 | return -1; |
| 80 | |
| 81 | for (i = 0; dst && dst->xfrm; dst = dst->child, i++) { |
| 82 | pos = strict ? i : 0; |
| 83 | if (pos >= info->len) |
| 84 | return 0; |
| 85 | e = &info->pol[pos]; |
| 86 | |
| 87 | if (match_xfrm_state(dst->xfrm, e)) { |
| 88 | if (!strict) |
| 89 | return 1; |
| 90 | } else if (strict) |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | return strict ? 1 : 0; |
| 95 | } |
| 96 | |
| 97 | static int match(const struct sk_buff *skb, |
| 98 | const struct net_device *in, |
| 99 | const struct net_device *out, |
| 100 | const void *matchinfo, |
| 101 | int offset, |
| 102 | unsigned int protoff, |
| 103 | int *hotdrop) |
| 104 | { |
| 105 | const struct ip6t_policy_info *info = matchinfo; |
| 106 | int ret; |
| 107 | |
| 108 | if (info->flags & IP6T_POLICY_MATCH_IN) |
| 109 | ret = match_policy_in(skb, info); |
| 110 | else |
| 111 | ret = match_policy_out(skb, info); |
| 112 | |
| 113 | if (ret < 0) |
| 114 | ret = info->flags & IP6T_POLICY_MATCH_NONE ? 1 : 0; |
| 115 | else if (info->flags & IP6T_POLICY_MATCH_NONE) |
| 116 | ret = 0; |
| 117 | |
| 118 | return ret; |
| 119 | } |
| 120 | |
| 121 | static int checkentry(const char *tablename, const struct ip6t_ip6 *ip, |
| 122 | void *matchinfo, unsigned int matchsize, |
| 123 | unsigned int hook_mask) |
| 124 | { |
| 125 | struct ip6t_policy_info *info = matchinfo; |
| 126 | |
| 127 | if (matchsize != IP6T_ALIGN(sizeof(*info))) { |
| 128 | printk(KERN_ERR "ip6t_policy: matchsize %u != %zu\n", |
| 129 | matchsize, IP6T_ALIGN(sizeof(*info))); |
| 130 | return 0; |
| 131 | } |
| 132 | if (!(info->flags & (IP6T_POLICY_MATCH_IN|IP6T_POLICY_MATCH_OUT))) { |
| 133 | printk(KERN_ERR "ip6t_policy: neither incoming nor " |
| 134 | "outgoing policy selected\n"); |
| 135 | return 0; |
| 136 | } |
| 137 | if (hook_mask & (1 << NF_IP6_PRE_ROUTING | 1 << NF_IP6_LOCAL_IN) |
| 138 | && info->flags & IP6T_POLICY_MATCH_OUT) { |
| 139 | printk(KERN_ERR "ip6t_policy: output policy not valid in " |
| 140 | "PRE_ROUTING and INPUT\n"); |
| 141 | return 0; |
| 142 | } |
| 143 | if (hook_mask & (1 << NF_IP6_POST_ROUTING | 1 << NF_IP6_LOCAL_OUT) |
| 144 | && info->flags & IP6T_POLICY_MATCH_IN) { |
| 145 | printk(KERN_ERR "ip6t_policy: input policy not valid in " |
| 146 | "POST_ROUTING and OUTPUT\n"); |
| 147 | return 0; |
| 148 | } |
| 149 | if (info->len > IP6T_POLICY_MAX_ELEM) { |
| 150 | printk(KERN_ERR "ip6t_policy: too many policy elements\n"); |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | return 1; |
| 155 | } |
| 156 | |
| 157 | static struct ip6t_match policy_match = { |
| 158 | .name = "policy", |
| 159 | .match = match, |
| 160 | .checkentry = checkentry, |
| 161 | .me = THIS_MODULE, |
| 162 | }; |
| 163 | |
| 164 | static int __init init(void) |
| 165 | { |
| 166 | return ip6t_register_match(&policy_match); |
| 167 | } |
| 168 | |
| 169 | static void __exit fini(void) |
| 170 | { |
| 171 | ip6t_unregister_match(&policy_match); |
| 172 | } |
| 173 | |
| 174 | module_init(init); |
| 175 | module_exit(fini); |