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