]> nv-tegra.nvidia Code Review - linux-2.6.git/blob - net/ipv4/netfilter/ipt_policy.c
[NETFILTER]: Convert ip_tables matches/targets to centralized error checking
[linux-2.6.git] / net / ipv4 / netfilter / ipt_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_ipv4.h>
18 #include <linux/netfilter_ipv4/ip_tables.h>
19 #include <linux/netfilter_ipv4/ipt_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 ipt_policy_elem *e)
28 {
29 #define MATCH_ADDR(x,y,z)       (!e->match.x ||                              \
30                                  ((e->x.a4.s_addr == (e->y.a4.s_addr & (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, x->props.saddr.a4) &&
35                MATCH_ADDR(daddr, dmask, x->id.daddr.a4) &&
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 ipt_policy_info *info)
44 {
45         const struct ipt_policy_elem *e;
46         struct sec_path *sp = skb->sp;
47         int strict = info->flags & IPT_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 ipt_policy_info *info)
73 {
74         const struct ipt_policy_elem *e;
75         struct dst_entry *dst = skb->dst;
76         int strict = info->flags & IPT_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 void *matchinfo,
102                  int offset,
103                  unsigned int protoff,
104                  int *hotdrop)
105 {
106         const struct ipt_policy_info *info = matchinfo;
107         int ret;
108
109         if (info->flags & IPT_POLICY_MATCH_IN)
110                 ret = match_policy_in(skb, info);
111         else
112                 ret = match_policy_out(skb, info);
113
114         if (ret < 0)
115                 ret = info->flags & IPT_POLICY_MATCH_NONE ? 1 : 0;
116         else if (info->flags & IPT_POLICY_MATCH_NONE)
117                 ret = 0;
118
119         return ret;
120 }
121
122 static int checkentry(const char *tablename, const void *ip_void,
123                       void *matchinfo, unsigned int matchsize,
124                       unsigned int hook_mask)
125 {
126         struct ipt_policy_info *info = matchinfo;
127
128         if (!(info->flags & (IPT_POLICY_MATCH_IN|IPT_POLICY_MATCH_OUT))) {
129                 printk(KERN_ERR "ipt_policy: neither incoming nor "
130                                 "outgoing policy selected\n");
131                 return 0;
132         }
133         if (hook_mask & (1 << NF_IP_PRE_ROUTING | 1 << NF_IP_LOCAL_IN)
134             && info->flags & IPT_POLICY_MATCH_OUT) {
135                 printk(KERN_ERR "ipt_policy: output policy not valid in "
136                                 "PRE_ROUTING and INPUT\n");
137                 return 0;
138         }
139         if (hook_mask & (1 << NF_IP_POST_ROUTING | 1 << NF_IP_LOCAL_OUT)
140             && info->flags & IPT_POLICY_MATCH_IN) {
141                 printk(KERN_ERR "ipt_policy: input policy not valid in "
142                                 "POST_ROUTING and OUTPUT\n");
143                 return 0;
144         }
145         if (info->len > IPT_POLICY_MAX_ELEM) {
146                 printk(KERN_ERR "ipt_policy: too many policy elements\n");
147                 return 0;
148         }
149
150         return 1;
151 }
152
153 static struct ipt_match policy_match = {
154         .name           = "policy",
155         .match          = match,
156         .matchsize      = sizeof(struct ipt_policy_info),
157         .checkentry     = checkentry,
158         .me             = THIS_MODULE,
159 };
160
161 static int __init init(void)
162 {
163         return ipt_register_match(&policy_match);
164 }
165
166 static void __exit fini(void)
167 {
168         ipt_unregister_match(&policy_match);
169 }
170
171 module_init(init);
172 module_exit(fini);