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