blob: 38822b34ba7dae429a69a064025b04b47d020841 [file] [log] [blame]
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * xfrm_policy.c
3 *
4 * Changes:
5 * Mitsuru KANDA @USAGI
6 * Kazunori MIYAZAWA @USAGI
7 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
8 * IPv6 support
9 * Kazunori MIYAZAWA @USAGI
10 * YOSHIFUJI Hideaki
11 * Split up af-specific portion
12 * Derek Atkins <derek@ihtfp.com> Add the post_input processor
Trent Jaegerdf718372005-12-13 23:12:27 -080013 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 */
15
Herbert Xu66cdb3c2007-11-13 21:37:28 -080016#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/slab.h>
18#include <linux/kmod.h>
19#include <linux/list.h>
20#include <linux/spinlock.h>
21#include <linux/workqueue.h>
22#include <linux/notifier.h>
23#include <linux/netdevice.h>
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -080024#include <linux/netfilter.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/module.h>
David S. Miller2518c7c2006-08-24 04:45:07 -070026#include <linux/cache.h>
Paul Moore68277ac2007-12-20 20:49:33 -080027#include <linux/audit.h>
Herbert Xu25ee3282007-12-11 09:32:34 -080028#include <net/dst.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <net/xfrm.h>
30#include <net/ip.h>
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -080031#ifdef CONFIG_XFRM_STATISTICS
32#include <net/snmp.h>
33#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
David S. Miller44e36b42006-08-24 04:50:50 -070035#include "xfrm_hash.h"
36
David S. Miller28faa972008-09-09 16:08:51 -070037int sysctl_xfrm_larval_drop __read_mostly = 1;
David S. Miller14e50e52007-05-24 18:17:54 -070038
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080039DEFINE_MUTEX(xfrm_cfg_mutex);
40EXPORT_SYMBOL(xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42static DEFINE_RWLOCK(xfrm_policy_lock);
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044static DEFINE_RWLOCK(xfrm_policy_afinfo_lock);
45static struct xfrm_policy_afinfo *xfrm_policy_afinfo[NPROTO];
46
Christoph Lametere18b8902006-12-06 20:33:20 -080047static struct kmem_cache *xfrm_dst_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
David S. Miller2518c7c2006-08-24 04:45:07 -070049static HLIST_HEAD(xfrm_policy_gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050static DEFINE_SPINLOCK(xfrm_policy_gc_lock);
51
52static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family);
53static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo);
Herbert Xu25ee3282007-12-11 09:32:34 -080054static void xfrm_init_pmtu(struct dst_entry *dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Andrew Morton77681022006-11-08 22:46:26 -080056static inline int
57__xfrm4_selector_match(struct xfrm_selector *sel, struct flowi *fl)
58{
59 return addr_match(&fl->fl4_dst, &sel->daddr, sel->prefixlen_d) &&
60 addr_match(&fl->fl4_src, &sel->saddr, sel->prefixlen_s) &&
61 !((xfrm_flowi_dport(fl) ^ sel->dport) & sel->dport_mask) &&
62 !((xfrm_flowi_sport(fl) ^ sel->sport) & sel->sport_mask) &&
63 (fl->proto == sel->proto || !sel->proto) &&
64 (fl->oif == sel->ifindex || !sel->ifindex);
65}
66
67static inline int
68__xfrm6_selector_match(struct xfrm_selector *sel, struct flowi *fl)
69{
70 return addr_match(&fl->fl6_dst, &sel->daddr, sel->prefixlen_d) &&
71 addr_match(&fl->fl6_src, &sel->saddr, sel->prefixlen_s) &&
72 !((xfrm_flowi_dport(fl) ^ sel->dport) & sel->dport_mask) &&
73 !((xfrm_flowi_sport(fl) ^ sel->sport) & sel->sport_mask) &&
74 (fl->proto == sel->proto || !sel->proto) &&
75 (fl->oif == sel->ifindex || !sel->ifindex);
76}
77
78int xfrm_selector_match(struct xfrm_selector *sel, struct flowi *fl,
79 unsigned short family)
80{
81 switch (family) {
82 case AF_INET:
83 return __xfrm4_selector_match(sel, fl);
84 case AF_INET6:
85 return __xfrm6_selector_match(sel, fl);
86 }
87 return 0;
88}
89
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -080090static inline struct dst_entry *__xfrm_dst_lookup(struct net *net, int tos,
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +090091 xfrm_address_t *saddr,
92 xfrm_address_t *daddr,
93 int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
Herbert Xu66cdb3c2007-11-13 21:37:28 -080095 struct xfrm_policy_afinfo *afinfo;
96 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Herbert Xu25ee3282007-12-11 09:32:34 -080098 afinfo = xfrm_policy_get_afinfo(family);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 if (unlikely(afinfo == NULL))
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800100 return ERR_PTR(-EAFNOSUPPORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800102 dst = afinfo->dst_lookup(net, tos, saddr, daddr);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 xfrm_policy_put_afinfo(afinfo);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900105
106 return dst;
107}
108
109static inline struct dst_entry *xfrm_dst_lookup(struct xfrm_state *x, int tos,
110 xfrm_address_t *prev_saddr,
111 xfrm_address_t *prev_daddr,
112 int family)
113{
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800114 struct net *net = xs_net(x);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900115 xfrm_address_t *saddr = &x->props.saddr;
116 xfrm_address_t *daddr = &x->id.daddr;
117 struct dst_entry *dst;
118
119 if (x->type->flags & XFRM_TYPE_LOCAL_COADDR) {
120 saddr = x->coaddr;
121 daddr = prev_daddr;
122 }
123 if (x->type->flags & XFRM_TYPE_REMOTE_COADDR) {
124 saddr = prev_saddr;
125 daddr = x->coaddr;
126 }
127
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800128 dst = __xfrm_dst_lookup(net, tos, saddr, daddr, family);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900129
130 if (!IS_ERR(dst)) {
131 if (prev_saddr != saddr)
132 memcpy(prev_saddr, saddr, sizeof(*prev_saddr));
133 if (prev_daddr != daddr)
134 memcpy(prev_daddr, daddr, sizeof(*prev_daddr));
135 }
136
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800137 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140static inline unsigned long make_jiffies(long secs)
141{
142 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
143 return MAX_SCHEDULE_TIMEOUT-1;
144 else
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900145 return secs*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
147
148static void xfrm_policy_timer(unsigned long data)
149{
150 struct xfrm_policy *xp = (struct xfrm_policy*)data;
James Morris9d729f72007-03-04 16:12:44 -0800151 unsigned long now = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 long next = LONG_MAX;
153 int warn = 0;
154 int dir;
155
156 read_lock(&xp->lock);
157
Herbert Xu12a169e2008-10-01 07:03:24 -0700158 if (xp->walk.dead)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 goto out;
160
Herbert Xu77d8d7a2005-10-05 12:15:12 -0700161 dir = xfrm_policy_id2dir(xp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 if (xp->lft.hard_add_expires_seconds) {
164 long tmo = xp->lft.hard_add_expires_seconds +
165 xp->curlft.add_time - now;
166 if (tmo <= 0)
167 goto expired;
168 if (tmo < next)
169 next = tmo;
170 }
171 if (xp->lft.hard_use_expires_seconds) {
172 long tmo = xp->lft.hard_use_expires_seconds +
173 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
174 if (tmo <= 0)
175 goto expired;
176 if (tmo < next)
177 next = tmo;
178 }
179 if (xp->lft.soft_add_expires_seconds) {
180 long tmo = xp->lft.soft_add_expires_seconds +
181 xp->curlft.add_time - now;
182 if (tmo <= 0) {
183 warn = 1;
184 tmo = XFRM_KM_TIMEOUT;
185 }
186 if (tmo < next)
187 next = tmo;
188 }
189 if (xp->lft.soft_use_expires_seconds) {
190 long tmo = xp->lft.soft_use_expires_seconds +
191 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
192 if (tmo <= 0) {
193 warn = 1;
194 tmo = XFRM_KM_TIMEOUT;
195 }
196 if (tmo < next)
197 next = tmo;
198 }
199
200 if (warn)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800201 km_policy_expired(xp, dir, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 if (next != LONG_MAX &&
203 !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
204 xfrm_pol_hold(xp);
205
206out:
207 read_unlock(&xp->lock);
208 xfrm_pol_put(xp);
209 return;
210
211expired:
212 read_unlock(&xp->lock);
Herbert Xu4666faa2005-06-18 22:43:22 -0700213 if (!xfrm_policy_delete(xp, dir))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800214 km_policy_expired(xp, dir, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 xfrm_pol_put(xp);
216}
217
218
219/* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
220 * SPD calls.
221 */
222
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -0800223struct xfrm_policy *xfrm_policy_alloc(struct net *net, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
225 struct xfrm_policy *policy;
226
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700227 policy = kzalloc(sizeof(struct xfrm_policy), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 if (policy) {
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -0800230 write_pnet(&policy->xp_net, net);
Herbert Xu12a169e2008-10-01 07:03:24 -0700231 INIT_LIST_HEAD(&policy->walk.all);
David S. Miller2518c7c2006-08-24 04:45:07 -0700232 INIT_HLIST_NODE(&policy->bydst);
233 INIT_HLIST_NODE(&policy->byidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 rwlock_init(&policy->lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700235 atomic_set(&policy->refcnt, 1);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800236 setup_timer(&policy->timer, xfrm_policy_timer,
237 (unsigned long)policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 }
239 return policy;
240}
241EXPORT_SYMBOL(xfrm_policy_alloc);
242
243/* Destroy xfrm_policy: descendant resources must be released to this moment. */
244
WANG Cong64c31b32008-01-07 22:34:29 -0800245void xfrm_policy_destroy(struct xfrm_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
Herbert Xu12a169e2008-10-01 07:03:24 -0700247 BUG_ON(!policy->walk.dead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Kris Katterjohn09a62662006-01-08 22:24:28 -0800249 BUG_ON(policy->bundles);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 if (del_timer(&policy->timer))
252 BUG();
253
Paul Moore03e1ad72008-04-12 19:07:52 -0700254 security_xfrm_policy_free(policy->security);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 kfree(policy);
256}
WANG Cong64c31b32008-01-07 22:34:29 -0800257EXPORT_SYMBOL(xfrm_policy_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259static void xfrm_policy_gc_kill(struct xfrm_policy *policy)
260{
261 struct dst_entry *dst;
262
263 while ((dst = policy->bundles) != NULL) {
264 policy->bundles = dst->next;
265 dst_free(dst);
266 }
267
268 if (del_timer(&policy->timer))
269 atomic_dec(&policy->refcnt);
270
271 if (atomic_read(&policy->refcnt) > 1)
272 flow_cache_flush();
273
274 xfrm_pol_put(policy);
275}
276
David Howellsc4028952006-11-22 14:57:56 +0000277static void xfrm_policy_gc_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
279 struct xfrm_policy *policy;
David S. Miller2518c7c2006-08-24 04:45:07 -0700280 struct hlist_node *entry, *tmp;
281 struct hlist_head gc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 spin_lock_bh(&xfrm_policy_gc_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700284 gc_list.first = xfrm_policy_gc_list.first;
285 INIT_HLIST_HEAD(&xfrm_policy_gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 spin_unlock_bh(&xfrm_policy_gc_lock);
287
David S. Miller2518c7c2006-08-24 04:45:07 -0700288 hlist_for_each_entry_safe(policy, entry, tmp, &gc_list, bydst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 xfrm_policy_gc_kill(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
Alexey Dobriyanc9583962008-11-25 17:13:59 -0800291static DECLARE_WORK(xfrm_policy_gc_work, xfrm_policy_gc_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293/* Rule must be locked. Release descentant resources, announce
294 * entry dead. The rule must be unlinked from lists to the moment.
295 */
296
297static void xfrm_policy_kill(struct xfrm_policy *policy)
298{
299 int dead;
300
301 write_lock_bh(&policy->lock);
Herbert Xu12a169e2008-10-01 07:03:24 -0700302 dead = policy->walk.dead;
303 policy->walk.dead = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 write_unlock_bh(&policy->lock);
305
306 if (unlikely(dead)) {
307 WARN_ON(1);
308 return;
309 }
310
Alexey Dobriyanbbb770e2008-11-03 19:11:29 -0800311 spin_lock_bh(&xfrm_policy_gc_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700312 hlist_add_head(&policy->bydst, &xfrm_policy_gc_list);
Alexey Dobriyanbbb770e2008-11-03 19:11:29 -0800313 spin_unlock_bh(&xfrm_policy_gc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315 schedule_work(&xfrm_policy_gc_work);
316}
317
David S. Miller2518c7c2006-08-24 04:45:07 -0700318static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024;
319
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800320static inline unsigned int idx_hash(struct net *net, u32 index)
David S. Miller2518c7c2006-08-24 04:45:07 -0700321{
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800322 return __idx_hash(index, net->xfrm.policy_idx_hmask);
David S. Miller2518c7c2006-08-24 04:45:07 -0700323}
324
Alexey Dobriyan11219942008-11-25 17:33:06 -0800325static struct hlist_head *policy_hash_bysel(struct net *net, struct xfrm_selector *sel, unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700326{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800327 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700328 unsigned int hash = __sel_hash(sel, family, hmask);
329
330 return (hash == hmask + 1 ?
Alexey Dobriyan11219942008-11-25 17:33:06 -0800331 &net->xfrm.policy_inexact[dir] :
332 net->xfrm.policy_bydst[dir].table + hash);
David S. Miller2518c7c2006-08-24 04:45:07 -0700333}
334
Alexey Dobriyan11219942008-11-25 17:33:06 -0800335static struct hlist_head *policy_hash_direct(struct net *net, xfrm_address_t *daddr, xfrm_address_t *saddr, unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700336{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800337 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700338 unsigned int hash = __addr_hash(daddr, saddr, family, hmask);
339
Alexey Dobriyan11219942008-11-25 17:33:06 -0800340 return net->xfrm.policy_bydst[dir].table + hash;
David S. Miller2518c7c2006-08-24 04:45:07 -0700341}
342
David S. Miller2518c7c2006-08-24 04:45:07 -0700343static void xfrm_dst_hash_transfer(struct hlist_head *list,
344 struct hlist_head *ndsttable,
345 unsigned int nhashmask)
346{
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800347 struct hlist_node *entry, *tmp, *entry0 = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700348 struct xfrm_policy *pol;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800349 unsigned int h0 = 0;
David S. Miller2518c7c2006-08-24 04:45:07 -0700350
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800351redo:
David S. Miller2518c7c2006-08-24 04:45:07 -0700352 hlist_for_each_entry_safe(pol, entry, tmp, list, bydst) {
353 unsigned int h;
354
355 h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
356 pol->family, nhashmask);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800357 if (!entry0) {
358 hlist_del(entry);
359 hlist_add_head(&pol->bydst, ndsttable+h);
360 h0 = h;
361 } else {
362 if (h != h0)
363 continue;
364 hlist_del(entry);
365 hlist_add_after(entry0, &pol->bydst);
366 }
367 entry0 = entry;
368 }
369 if (!hlist_empty(list)) {
370 entry0 = NULL;
371 goto redo;
David S. Miller2518c7c2006-08-24 04:45:07 -0700372 }
373}
374
375static void xfrm_idx_hash_transfer(struct hlist_head *list,
376 struct hlist_head *nidxtable,
377 unsigned int nhashmask)
378{
379 struct hlist_node *entry, *tmp;
380 struct xfrm_policy *pol;
381
382 hlist_for_each_entry_safe(pol, entry, tmp, list, byidx) {
383 unsigned int h;
384
385 h = __idx_hash(pol->index, nhashmask);
386 hlist_add_head(&pol->byidx, nidxtable+h);
387 }
388}
389
390static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
391{
392 return ((old_hmask + 1) << 1) - 1;
393}
394
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800395static void xfrm_bydst_resize(struct net *net, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700396{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800397 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700398 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
399 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800400 struct hlist_head *odst = net->xfrm.policy_bydst[dir].table;
David S. Miller44e36b42006-08-24 04:50:50 -0700401 struct hlist_head *ndst = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700402 int i;
403
404 if (!ndst)
405 return;
406
407 write_lock_bh(&xfrm_policy_lock);
408
409 for (i = hmask; i >= 0; i--)
410 xfrm_dst_hash_transfer(odst + i, ndst, nhashmask);
411
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800412 net->xfrm.policy_bydst[dir].table = ndst;
413 net->xfrm.policy_bydst[dir].hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700414
415 write_unlock_bh(&xfrm_policy_lock);
416
David S. Miller44e36b42006-08-24 04:50:50 -0700417 xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700418}
419
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800420static void xfrm_byidx_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700421{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800422 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700423 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
424 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800425 struct hlist_head *oidx = net->xfrm.policy_byidx;
David S. Miller44e36b42006-08-24 04:50:50 -0700426 struct hlist_head *nidx = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700427 int i;
428
429 if (!nidx)
430 return;
431
432 write_lock_bh(&xfrm_policy_lock);
433
434 for (i = hmask; i >= 0; i--)
435 xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
436
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800437 net->xfrm.policy_byidx = nidx;
438 net->xfrm.policy_idx_hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700439
440 write_unlock_bh(&xfrm_policy_lock);
441
David S. Miller44e36b42006-08-24 04:50:50 -0700442 xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700443}
444
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800445static inline int xfrm_bydst_should_resize(struct net *net, int dir, int *total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700446{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800447 unsigned int cnt = net->xfrm.policy_count[dir];
448 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700449
450 if (total)
451 *total += cnt;
452
453 if ((hmask + 1) < xfrm_policy_hashmax &&
454 cnt > hmask)
455 return 1;
456
457 return 0;
458}
459
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800460static inline int xfrm_byidx_should_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700461{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800462 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700463
464 if ((hmask + 1) < xfrm_policy_hashmax &&
465 total > hmask)
466 return 1;
467
468 return 0;
469}
470
Jamal Hadi Salim5a6d3412007-05-04 12:55:39 -0700471void xfrm_spd_getinfo(struct xfrmk_spdinfo *si)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700472{
473 read_lock_bh(&xfrm_policy_lock);
Alexey Dobriyandc2caba2008-11-25 17:24:15 -0800474 si->incnt = init_net.xfrm.policy_count[XFRM_POLICY_IN];
475 si->outcnt = init_net.xfrm.policy_count[XFRM_POLICY_OUT];
476 si->fwdcnt = init_net.xfrm.policy_count[XFRM_POLICY_FWD];
477 si->inscnt = init_net.xfrm.policy_count[XFRM_POLICY_IN+XFRM_POLICY_MAX];
478 si->outscnt = init_net.xfrm.policy_count[XFRM_POLICY_OUT+XFRM_POLICY_MAX];
479 si->fwdscnt = init_net.xfrm.policy_count[XFRM_POLICY_FWD+XFRM_POLICY_MAX];
Alexey Dobriyan8100bea2008-11-25 17:22:58 -0800480 si->spdhcnt = init_net.xfrm.policy_idx_hmask;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700481 si->spdhmcnt = xfrm_policy_hashmax;
482 read_unlock_bh(&xfrm_policy_lock);
483}
484EXPORT_SYMBOL(xfrm_spd_getinfo);
David S. Miller2518c7c2006-08-24 04:45:07 -0700485
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700486static DEFINE_MUTEX(hash_resize_mutex);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800487static void xfrm_hash_resize(struct work_struct *work)
David S. Miller2518c7c2006-08-24 04:45:07 -0700488{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800489 struct net *net = container_of(work, struct net, xfrm.policy_hash_work);
David S. Miller2518c7c2006-08-24 04:45:07 -0700490 int dir, total;
491
492 mutex_lock(&hash_resize_mutex);
493
494 total = 0;
495 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800496 if (xfrm_bydst_should_resize(net, dir, &total))
497 xfrm_bydst_resize(net, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700498 }
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800499 if (xfrm_byidx_should_resize(net, total))
500 xfrm_byidx_resize(net, total);
David S. Miller2518c7c2006-08-24 04:45:07 -0700501
502 mutex_unlock(&hash_resize_mutex);
503}
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505/* Generate new index... KAME seems to generate them ordered by cost
506 * of an absolute inpredictability of ordering of rules. This will not pass. */
Alexey Dobriyan11219942008-11-25 17:33:06 -0800507static u32 xfrm_gen_index(struct net *net, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 static u32 idx_generator;
510
511 for (;;) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700512 struct hlist_node *entry;
513 struct hlist_head *list;
514 struct xfrm_policy *p;
515 u32 idx;
516 int found;
517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 idx = (idx_generator | dir);
519 idx_generator += 8;
520 if (idx == 0)
521 idx = 8;
Alexey Dobriyan11219942008-11-25 17:33:06 -0800522 list = net->xfrm.policy_byidx + idx_hash(net, idx);
David S. Miller2518c7c2006-08-24 04:45:07 -0700523 found = 0;
524 hlist_for_each_entry(p, entry, list, byidx) {
525 if (p->index == idx) {
526 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 break;
David S. Miller2518c7c2006-08-24 04:45:07 -0700528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700530 if (!found)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 return idx;
532 }
533}
534
David S. Miller2518c7c2006-08-24 04:45:07 -0700535static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
536{
537 u32 *p1 = (u32 *) s1;
538 u32 *p2 = (u32 *) s2;
539 int len = sizeof(struct xfrm_selector) / sizeof(u32);
540 int i;
541
542 for (i = 0; i < len; i++) {
543 if (p1[i] != p2[i])
544 return 1;
545 }
546
547 return 0;
548}
549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
551{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800552 struct net *net = xp_net(policy);
David S. Miller2518c7c2006-08-24 04:45:07 -0700553 struct xfrm_policy *pol;
554 struct xfrm_policy *delpol;
555 struct hlist_head *chain;
Herbert Xua6c7ab52007-01-16 16:52:02 -0800556 struct hlist_node *entry, *newpos;
David S. Miller9b78a822005-12-22 07:39:48 -0800557 struct dst_entry *gc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559 write_lock_bh(&xfrm_policy_lock);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800560 chain = policy_hash_bysel(net, &policy->selector, policy->family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700561 delpol = NULL;
562 newpos = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700563 hlist_for_each_entry(pol, entry, chain, bydst) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800564 if (pol->type == policy->type &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700565 !selector_cmp(&pol->selector, &policy->selector) &&
Herbert Xua6c7ab52007-01-16 16:52:02 -0800566 xfrm_sec_ctx_match(pol->security, policy->security) &&
567 !WARN_ON(delpol)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if (excl) {
569 write_unlock_bh(&xfrm_policy_lock);
570 return -EEXIST;
571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 delpol = pol;
573 if (policy->priority > pol->priority)
574 continue;
575 } else if (policy->priority >= pol->priority) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800576 newpos = &pol->bydst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 continue;
578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 if (delpol)
580 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 }
582 if (newpos)
David S. Miller2518c7c2006-08-24 04:45:07 -0700583 hlist_add_after(newpos, &policy->bydst);
584 else
585 hlist_add_head(&policy->bydst, chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 xfrm_pol_hold(policy);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800587 net->xfrm.policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 atomic_inc(&flow_cache_genid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700589 if (delpol) {
590 hlist_del(&delpol->bydst);
591 hlist_del(&delpol->byidx);
Herbert Xu12a169e2008-10-01 07:03:24 -0700592 list_del(&delpol->walk.all);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800593 net->xfrm.policy_count[dir]--;
David S. Miller2518c7c2006-08-24 04:45:07 -0700594 }
Alexey Dobriyan11219942008-11-25 17:33:06 -0800595 policy->index = delpol ? delpol->index : xfrm_gen_index(net, dir);
596 hlist_add_head(&policy->byidx, net->xfrm.policy_byidx+idx_hash(net, policy->index));
James Morris9d729f72007-03-04 16:12:44 -0800597 policy->curlft.add_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 policy->curlft.use_time = 0;
599 if (!mod_timer(&policy->timer, jiffies + HZ))
600 xfrm_pol_hold(policy);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800601 list_add(&policy->walk.all, &net->xfrm.policy_all);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 write_unlock_bh(&xfrm_policy_lock);
603
David S. Miller9b78a822005-12-22 07:39:48 -0800604 if (delpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 xfrm_policy_kill(delpol);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800606 else if (xfrm_bydst_should_resize(net, dir, NULL))
607 schedule_work(&net->xfrm.policy_hash_work);
David S. Miller9b78a822005-12-22 07:39:48 -0800608
609 read_lock_bh(&xfrm_policy_lock);
610 gc_list = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700611 entry = &policy->bydst;
612 hlist_for_each_entry_continue(policy, entry, bydst) {
David S. Miller9b78a822005-12-22 07:39:48 -0800613 struct dst_entry *dst;
614
615 write_lock(&policy->lock);
616 dst = policy->bundles;
617 if (dst) {
618 struct dst_entry *tail = dst;
619 while (tail->next)
620 tail = tail->next;
621 tail->next = gc_list;
622 gc_list = dst;
623
624 policy->bundles = NULL;
625 }
626 write_unlock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 }
David S. Miller9b78a822005-12-22 07:39:48 -0800628 read_unlock_bh(&xfrm_policy_lock);
629
630 while (gc_list) {
631 struct dst_entry *dst = gc_list;
632
633 gc_list = dst->next;
634 dst_free(dst);
635 }
636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 return 0;
638}
639EXPORT_SYMBOL(xfrm_policy_insert);
640
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800641struct xfrm_policy *xfrm_policy_bysel_ctx(struct net *net, u8 type, int dir,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700642 struct xfrm_selector *sel,
Eric Parisef41aaa2007-03-07 15:37:58 -0800643 struct xfrm_sec_ctx *ctx, int delete,
644 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
David S. Miller2518c7c2006-08-24 04:45:07 -0700646 struct xfrm_policy *pol, *ret;
647 struct hlist_head *chain;
648 struct hlist_node *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Eric Parisef41aaa2007-03-07 15:37:58 -0800650 *err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 write_lock_bh(&xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800652 chain = policy_hash_bysel(net, sel, sel->family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700653 ret = NULL;
654 hlist_for_each_entry(pol, entry, chain, bydst) {
655 if (pol->type == type &&
656 !selector_cmp(sel, &pol->selector) &&
657 xfrm_sec_ctx_match(ctx, pol->security)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700659 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -0700660 *err = security_xfrm_policy_delete(
661 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -0800662 if (*err) {
663 write_unlock_bh(&xfrm_policy_lock);
664 return pol;
665 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700666 hlist_del(&pol->bydst);
667 hlist_del(&pol->byidx);
Herbert Xu12a169e2008-10-01 07:03:24 -0700668 list_del(&pol->walk.all);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800669 net->xfrm.policy_count[dir]--;
David S. Miller2518c7c2006-08-24 04:45:07 -0700670 }
671 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 break;
673 }
674 }
675 write_unlock_bh(&xfrm_policy_lock);
676
David S. Miller2518c7c2006-08-24 04:45:07 -0700677 if (ret && delete) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 atomic_inc(&flow_cache_genid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700679 xfrm_policy_kill(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700681 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682}
Trent Jaegerdf718372005-12-13 23:12:27 -0800683EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800685struct xfrm_policy *xfrm_policy_byid(struct net *net, u8 type, int dir, u32 id,
686 int delete, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
David S. Miller2518c7c2006-08-24 04:45:07 -0700688 struct xfrm_policy *pol, *ret;
689 struct hlist_head *chain;
690 struct hlist_node *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Herbert Xub5505c62007-05-14 02:15:47 -0700692 *err = -ENOENT;
693 if (xfrm_policy_id2dir(id) != dir)
694 return NULL;
695
Eric Parisef41aaa2007-03-07 15:37:58 -0800696 *err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 write_lock_bh(&xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800698 chain = net->xfrm.policy_byidx + idx_hash(net, id);
David S. Miller2518c7c2006-08-24 04:45:07 -0700699 ret = NULL;
700 hlist_for_each_entry(pol, entry, chain, byidx) {
701 if (pol->type == type && pol->index == id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700703 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -0700704 *err = security_xfrm_policy_delete(
705 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -0800706 if (*err) {
707 write_unlock_bh(&xfrm_policy_lock);
708 return pol;
709 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700710 hlist_del(&pol->bydst);
711 hlist_del(&pol->byidx);
Herbert Xu12a169e2008-10-01 07:03:24 -0700712 list_del(&pol->walk.all);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800713 net->xfrm.policy_count[dir]--;
David S. Miller2518c7c2006-08-24 04:45:07 -0700714 }
715 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 break;
717 }
718 }
719 write_unlock_bh(&xfrm_policy_lock);
720
David S. Miller2518c7c2006-08-24 04:45:07 -0700721 if (ret && delete) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 atomic_inc(&flow_cache_genid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700723 xfrm_policy_kill(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700725 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726}
727EXPORT_SYMBOL(xfrm_policy_byid);
728
Joy Latten4aa2e622007-06-04 19:05:57 -0400729#ifdef CONFIG_SECURITY_NETWORK_XFRM
730static inline int
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800731xfrm_policy_flush_secctx_check(struct net *net, u8 type, struct xfrm_audit *audit_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
Joy Latten4aa2e622007-06-04 19:05:57 -0400733 int dir, err = 0;
734
735 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
736 struct xfrm_policy *pol;
737 struct hlist_node *entry;
738 int i;
739
740 hlist_for_each_entry(pol, entry,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800741 &net->xfrm.policy_inexact[dir], bydst) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400742 if (pol->type != type)
743 continue;
Paul Moore03e1ad72008-04-12 19:07:52 -0700744 err = security_xfrm_policy_delete(pol->security);
Joy Latten4aa2e622007-06-04 19:05:57 -0400745 if (err) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700746 xfrm_audit_policy_delete(pol, 0,
747 audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400748 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700749 audit_info->secid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400750 return err;
751 }
YOSHIFUJI Hideaki7dc12d62007-07-19 10:45:15 +0900752 }
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800753 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400754 hlist_for_each_entry(pol, entry,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800755 net->xfrm.policy_bydst[dir].table + i,
Joy Latten4aa2e622007-06-04 19:05:57 -0400756 bydst) {
757 if (pol->type != type)
758 continue;
Paul Moore03e1ad72008-04-12 19:07:52 -0700759 err = security_xfrm_policy_delete(
760 pol->security);
Joy Latten4aa2e622007-06-04 19:05:57 -0400761 if (err) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700762 xfrm_audit_policy_delete(pol, 0,
763 audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400764 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700765 audit_info->secid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400766 return err;
767 }
768 }
769 }
770 }
771 return err;
772}
773#else
774static inline int
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800775xfrm_policy_flush_secctx_check(struct net *net, u8 type, struct xfrm_audit *audit_info)
Joy Latten4aa2e622007-06-04 19:05:57 -0400776{
777 return 0;
778}
779#endif
780
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800781int xfrm_policy_flush(struct net *net, u8 type, struct xfrm_audit *audit_info)
Joy Latten4aa2e622007-06-04 19:05:57 -0400782{
783 int dir, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785 write_lock_bh(&xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400786
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800787 err = xfrm_policy_flush_secctx_check(net, type, audit_info);
Joy Latten4aa2e622007-06-04 19:05:57 -0400788 if (err)
789 goto out;
790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700792 struct xfrm_policy *pol;
793 struct hlist_node *entry;
David S. Millerae8c0572006-10-03 16:00:26 -0700794 int i, killed;
David S. Miller2518c7c2006-08-24 04:45:07 -0700795
David S. Millerae8c0572006-10-03 16:00:26 -0700796 killed = 0;
David S. Miller2518c7c2006-08-24 04:45:07 -0700797 again1:
798 hlist_for_each_entry(pol, entry,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800799 &net->xfrm.policy_inexact[dir], bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700800 if (pol->type != type)
801 continue;
802 hlist_del(&pol->bydst);
803 hlist_del(&pol->byidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 write_unlock_bh(&xfrm_policy_lock);
805
Joy Lattenab5f5e82007-09-17 11:51:22 -0700806 xfrm_audit_policy_delete(pol, 1, audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400807 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700808 audit_info->secid);
Joy Latten161a09e2006-11-27 13:11:54 -0600809
David S. Miller2518c7c2006-08-24 04:45:07 -0700810 xfrm_policy_kill(pol);
David S. Millerae8c0572006-10-03 16:00:26 -0700811 killed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
813 write_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700814 goto again1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700816
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800817 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700818 again2:
819 hlist_for_each_entry(pol, entry,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800820 net->xfrm.policy_bydst[dir].table + i,
David S. Miller2518c7c2006-08-24 04:45:07 -0700821 bydst) {
822 if (pol->type != type)
823 continue;
824 hlist_del(&pol->bydst);
825 hlist_del(&pol->byidx);
Herbert Xu12a169e2008-10-01 07:03:24 -0700826 list_del(&pol->walk.all);
David S. Miller2518c7c2006-08-24 04:45:07 -0700827 write_unlock_bh(&xfrm_policy_lock);
828
Joy Lattenab5f5e82007-09-17 11:51:22 -0700829 xfrm_audit_policy_delete(pol, 1,
830 audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400831 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700832 audit_info->secid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700833 xfrm_policy_kill(pol);
David S. Millerae8c0572006-10-03 16:00:26 -0700834 killed++;
David S. Miller2518c7c2006-08-24 04:45:07 -0700835
836 write_lock_bh(&xfrm_policy_lock);
837 goto again2;
838 }
839 }
840
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800841 net->xfrm.policy_count[dir] -= killed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 }
843 atomic_inc(&flow_cache_genid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400844out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 write_unlock_bh(&xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400846 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847}
848EXPORT_SYMBOL(xfrm_policy_flush);
849
Alexey Dobriyancdcbca72008-11-25 17:34:49 -0800850int xfrm_policy_walk(struct net *net, struct xfrm_policy_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -0800851 int (*func)(struct xfrm_policy *, int, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 void *data)
853{
Herbert Xu12a169e2008-10-01 07:03:24 -0700854 struct xfrm_policy *pol;
855 struct xfrm_policy_walk_entry *x;
Timo Teras4c563f72008-02-28 21:31:08 -0800856 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Timo Teras4c563f72008-02-28 21:31:08 -0800858 if (walk->type >= XFRM_POLICY_TYPE_MAX &&
859 walk->type != XFRM_POLICY_TYPE_ANY)
860 return -EINVAL;
861
Herbert Xu12a169e2008-10-01 07:03:24 -0700862 if (list_empty(&walk->walk.all) && walk->seq != 0)
Timo Teras4c563f72008-02-28 21:31:08 -0800863 return 0;
864
Herbert Xu12a169e2008-10-01 07:03:24 -0700865 write_lock_bh(&xfrm_policy_lock);
866 if (list_empty(&walk->walk.all))
Alexey Dobriyancdcbca72008-11-25 17:34:49 -0800867 x = list_first_entry(&net->xfrm.policy_all, struct xfrm_policy_walk_entry, all);
Herbert Xu12a169e2008-10-01 07:03:24 -0700868 else
869 x = list_entry(&walk->walk.all, struct xfrm_policy_walk_entry, all);
Alexey Dobriyancdcbca72008-11-25 17:34:49 -0800870 list_for_each_entry_from(x, &net->xfrm.policy_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -0700871 if (x->dead)
Timo Teras4c563f72008-02-28 21:31:08 -0800872 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -0700873 pol = container_of(x, struct xfrm_policy, walk);
874 if (walk->type != XFRM_POLICY_TYPE_ANY &&
875 walk->type != pol->type)
876 continue;
877 error = func(pol, xfrm_policy_id2dir(pol->index),
878 walk->seq, data);
879 if (error) {
880 list_move_tail(&walk->walk.all, &x->all);
881 goto out;
Timo Teras4c563f72008-02-28 21:31:08 -0800882 }
Herbert Xu12a169e2008-10-01 07:03:24 -0700883 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 }
Herbert Xu12a169e2008-10-01 07:03:24 -0700885 if (walk->seq == 0) {
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -0800886 error = -ENOENT;
887 goto out;
888 }
Herbert Xu12a169e2008-10-01 07:03:24 -0700889 list_del_init(&walk->walk.all);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890out:
Herbert Xu12a169e2008-10-01 07:03:24 -0700891 write_unlock_bh(&xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 return error;
893}
894EXPORT_SYMBOL(xfrm_policy_walk);
895
Herbert Xu12a169e2008-10-01 07:03:24 -0700896void xfrm_policy_walk_init(struct xfrm_policy_walk *walk, u8 type)
897{
898 INIT_LIST_HEAD(&walk->walk.all);
899 walk->walk.dead = 1;
900 walk->type = type;
901 walk->seq = 0;
902}
903EXPORT_SYMBOL(xfrm_policy_walk_init);
904
905void xfrm_policy_walk_done(struct xfrm_policy_walk *walk)
906{
907 if (list_empty(&walk->walk.all))
908 return;
909
910 write_lock_bh(&xfrm_policy_lock);
911 list_del(&walk->walk.all);
912 write_unlock_bh(&xfrm_policy_lock);
913}
914EXPORT_SYMBOL(xfrm_policy_walk_done);
915
James Morris134b0fc2006-10-05 15:42:27 -0500916/*
917 * Find policy to apply to this flow.
918 *
919 * Returns 0 if policy found, else an -errno.
920 */
David S. Miller2518c7c2006-08-24 04:45:07 -0700921static int xfrm_policy_match(struct xfrm_policy *pol, struct flowi *fl,
922 u8 type, u16 family, int dir)
923{
924 struct xfrm_selector *sel = &pol->selector;
James Morris134b0fc2006-10-05 15:42:27 -0500925 int match, ret = -ESRCH;
David S. Miller2518c7c2006-08-24 04:45:07 -0700926
927 if (pol->family != family ||
928 pol->type != type)
James Morris134b0fc2006-10-05 15:42:27 -0500929 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -0700930
931 match = xfrm_selector_match(sel, fl, family);
James Morris134b0fc2006-10-05 15:42:27 -0500932 if (match)
Paul Moore03e1ad72008-04-12 19:07:52 -0700933 ret = security_xfrm_policy_lookup(pol->security, fl->secid,
934 dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700935
James Morris134b0fc2006-10-05 15:42:27 -0500936 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -0700937}
938
Alexey Dobriyan52479b62008-11-25 17:35:18 -0800939static struct xfrm_policy *xfrm_policy_lookup_bytype(struct net *net, u8 type,
940 struct flowi *fl,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700941 u16 family, u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942{
James Morris134b0fc2006-10-05 15:42:27 -0500943 int err;
David S. Miller2518c7c2006-08-24 04:45:07 -0700944 struct xfrm_policy *pol, *ret;
945 xfrm_address_t *daddr, *saddr;
946 struct hlist_node *entry;
947 struct hlist_head *chain;
David S. Milleracba48e2006-08-25 15:46:46 -0700948 u32 priority = ~0U;
David S. Miller2518c7c2006-08-24 04:45:07 -0700949
950 daddr = xfrm_flowi_daddr(fl, family);
951 saddr = xfrm_flowi_saddr(fl, family);
952 if (unlikely(!daddr || !saddr))
953 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
955 read_lock_bh(&xfrm_policy_lock);
Alexey Dobriyan52479b62008-11-25 17:35:18 -0800956 chain = policy_hash_direct(net, daddr, saddr, family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700957 ret = NULL;
958 hlist_for_each_entry(pol, entry, chain, bydst) {
James Morris134b0fc2006-10-05 15:42:27 -0500959 err = xfrm_policy_match(pol, fl, type, family, dir);
960 if (err) {
961 if (err == -ESRCH)
962 continue;
963 else {
964 ret = ERR_PTR(err);
965 goto fail;
966 }
967 } else {
David S. Milleracba48e2006-08-25 15:46:46 -0700968 ret = pol;
969 priority = ret->priority;
970 break;
971 }
972 }
Alexey Dobriyan52479b62008-11-25 17:35:18 -0800973 chain = &net->xfrm.policy_inexact[dir];
David S. Milleracba48e2006-08-25 15:46:46 -0700974 hlist_for_each_entry(pol, entry, chain, bydst) {
James Morris134b0fc2006-10-05 15:42:27 -0500975 err = xfrm_policy_match(pol, fl, type, family, dir);
976 if (err) {
977 if (err == -ESRCH)
978 continue;
979 else {
980 ret = ERR_PTR(err);
981 goto fail;
982 }
983 } else if (pol->priority < priority) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700984 ret = pol;
985 break;
986 }
987 }
David S. Milleracba48e2006-08-25 15:46:46 -0700988 if (ret)
989 xfrm_pol_hold(ret);
James Morris134b0fc2006-10-05 15:42:27 -0500990fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 read_unlock_bh(&xfrm_policy_lock);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700992
David S. Miller2518c7c2006-08-24 04:45:07 -0700993 return ret;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700994}
995
Alexey Dobriyan52479b62008-11-25 17:35:18 -0800996static int xfrm_policy_lookup(struct net *net, struct flowi *fl, u16 family,
997 u8 dir, void **objp, atomic_t **obj_refp)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700998{
999 struct xfrm_policy *pol;
James Morris134b0fc2006-10-05 15:42:27 -05001000 int err = 0;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001001
1002#ifdef CONFIG_XFRM_SUB_POLICY
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001003 pol = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_SUB, fl, family, dir);
James Morris134b0fc2006-10-05 15:42:27 -05001004 if (IS_ERR(pol)) {
1005 err = PTR_ERR(pol);
1006 pol = NULL;
1007 }
1008 if (pol || err)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001009 goto end;
1010#endif
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001011 pol = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN, fl, family, dir);
James Morris134b0fc2006-10-05 15:42:27 -05001012 if (IS_ERR(pol)) {
1013 err = PTR_ERR(pol);
1014 pol = NULL;
1015 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001016#ifdef CONFIG_XFRM_SUB_POLICY
David S. Miller2518c7c2006-08-24 04:45:07 -07001017end:
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001018#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 if ((*objp = (void *) pol) != NULL)
1020 *obj_refp = &pol->refcnt;
James Morris134b0fc2006-10-05 15:42:27 -05001021 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022}
1023
Trent Jaegerdf718372005-12-13 23:12:27 -08001024static inline int policy_to_flow_dir(int dir)
1025{
1026 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001027 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1028 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1029 return dir;
1030 switch (dir) {
1031 default:
1032 case XFRM_POLICY_IN:
1033 return FLOW_DIR_IN;
1034 case XFRM_POLICY_OUT:
1035 return FLOW_DIR_OUT;
1036 case XFRM_POLICY_FWD:
1037 return FLOW_DIR_FWD;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001038 }
Trent Jaegerdf718372005-12-13 23:12:27 -08001039}
1040
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001041static struct xfrm_policy *xfrm_sk_policy_lookup(struct sock *sk, int dir, struct flowi *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
1043 struct xfrm_policy *pol;
1044
1045 read_lock_bh(&xfrm_policy_lock);
1046 if ((pol = sk->sk_policy[dir]) != NULL) {
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001047 int match = xfrm_selector_match(&pol->selector, fl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 sk->sk_family);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001049 int err = 0;
Trent Jaegerdf718372005-12-13 23:12:27 -08001050
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001051 if (match) {
Paul Moore03e1ad72008-04-12 19:07:52 -07001052 err = security_xfrm_policy_lookup(pol->security,
1053 fl->secid,
1054 policy_to_flow_dir(dir));
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001055 if (!err)
1056 xfrm_pol_hold(pol);
1057 else if (err == -ESRCH)
1058 pol = NULL;
1059 else
1060 pol = ERR_PTR(err);
1061 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 pol = NULL;
1063 }
1064 read_unlock_bh(&xfrm_policy_lock);
1065 return pol;
1066}
1067
1068static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
1069{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001070 struct net *net = xp_net(pol);
Alexey Dobriyan11219942008-11-25 17:33:06 -08001071 struct hlist_head *chain = policy_hash_bysel(net, &pol->selector,
David S. Miller2518c7c2006-08-24 04:45:07 -07001072 pol->family, dir);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001073
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001074 list_add(&pol->walk.all, &net->xfrm.policy_all);
David S. Miller2518c7c2006-08-24 04:45:07 -07001075 hlist_add_head(&pol->bydst, chain);
Alexey Dobriyane92303f2008-11-25 17:32:41 -08001076 hlist_add_head(&pol->byidx, net->xfrm.policy_byidx+idx_hash(net, pol->index));
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001077 net->xfrm.policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -07001079
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001080 if (xfrm_bydst_should_resize(net, dir, NULL))
1081 schedule_work(&net->xfrm.policy_hash_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082}
1083
1084static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
1085 int dir)
1086{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001087 struct net *net = xp_net(pol);
1088
David S. Miller2518c7c2006-08-24 04:45:07 -07001089 if (hlist_unhashed(&pol->bydst))
1090 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
David S. Miller2518c7c2006-08-24 04:45:07 -07001092 hlist_del(&pol->bydst);
1093 hlist_del(&pol->byidx);
Herbert Xu12a169e2008-10-01 07:03:24 -07001094 list_del(&pol->walk.all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001095 net->xfrm.policy_count[dir]--;
David S. Miller2518c7c2006-08-24 04:45:07 -07001096
1097 return pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098}
1099
Herbert Xu4666faa2005-06-18 22:43:22 -07001100int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101{
1102 write_lock_bh(&xfrm_policy_lock);
1103 pol = __xfrm_policy_unlink(pol, dir);
1104 write_unlock_bh(&xfrm_policy_lock);
1105 if (pol) {
1106 if (dir < XFRM_POLICY_MAX)
1107 atomic_inc(&flow_cache_genid);
1108 xfrm_policy_kill(pol);
Herbert Xu4666faa2005-06-18 22:43:22 -07001109 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 }
Herbert Xu4666faa2005-06-18 22:43:22 -07001111 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112}
David S. Millera70fcb02006-03-20 19:18:52 -08001113EXPORT_SYMBOL(xfrm_policy_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
1115int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
1116{
Alexey Dobriyan11219942008-11-25 17:33:06 -08001117 struct net *net = xp_net(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 struct xfrm_policy *old_pol;
1119
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001120#ifdef CONFIG_XFRM_SUB_POLICY
1121 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
1122 return -EINVAL;
1123#endif
1124
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 write_lock_bh(&xfrm_policy_lock);
1126 old_pol = sk->sk_policy[dir];
1127 sk->sk_policy[dir] = pol;
1128 if (pol) {
James Morris9d729f72007-03-04 16:12:44 -08001129 pol->curlft.add_time = get_seconds();
Alexey Dobriyan11219942008-11-25 17:33:06 -08001130 pol->index = xfrm_gen_index(net, XFRM_POLICY_MAX+dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 __xfrm_policy_link(pol, XFRM_POLICY_MAX+dir);
1132 }
1133 if (old_pol)
1134 __xfrm_policy_unlink(old_pol, XFRM_POLICY_MAX+dir);
1135 write_unlock_bh(&xfrm_policy_lock);
1136
1137 if (old_pol) {
1138 xfrm_policy_kill(old_pol);
1139 }
1140 return 0;
1141}
1142
1143static struct xfrm_policy *clone_policy(struct xfrm_policy *old, int dir)
1144{
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -08001145 struct xfrm_policy *newp = xfrm_policy_alloc(xp_net(old), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
1147 if (newp) {
1148 newp->selector = old->selector;
Paul Moore03e1ad72008-04-12 19:07:52 -07001149 if (security_xfrm_policy_clone(old->security,
1150 &newp->security)) {
Trent Jaegerdf718372005-12-13 23:12:27 -08001151 kfree(newp);
1152 return NULL; /* ENOMEM */
1153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 newp->lft = old->lft;
1155 newp->curlft = old->curlft;
1156 newp->action = old->action;
1157 newp->flags = old->flags;
1158 newp->xfrm_nr = old->xfrm_nr;
1159 newp->index = old->index;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001160 newp->type = old->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 memcpy(newp->xfrm_vec, old->xfrm_vec,
1162 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
1163 write_lock_bh(&xfrm_policy_lock);
1164 __xfrm_policy_link(newp, XFRM_POLICY_MAX+dir);
1165 write_unlock_bh(&xfrm_policy_lock);
1166 xfrm_pol_put(newp);
1167 }
1168 return newp;
1169}
1170
1171int __xfrm_sk_clone_policy(struct sock *sk)
1172{
1173 struct xfrm_policy *p0 = sk->sk_policy[0],
1174 *p1 = sk->sk_policy[1];
1175
1176 sk->sk_policy[0] = sk->sk_policy[1] = NULL;
1177 if (p0 && (sk->sk_policy[0] = clone_policy(p0, 0)) == NULL)
1178 return -ENOMEM;
1179 if (p1 && (sk->sk_policy[1] = clone_policy(p1, 1)) == NULL)
1180 return -ENOMEM;
1181 return 0;
1182}
1183
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001184static int
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001185xfrm_get_saddr(struct net *net, xfrm_address_t *local, xfrm_address_t *remote,
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001186 unsigned short family)
1187{
1188 int err;
1189 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1190
1191 if (unlikely(afinfo == NULL))
1192 return -EINVAL;
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001193 err = afinfo->get_saddr(net, local, remote);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001194 xfrm_policy_put_afinfo(afinfo);
1195 return err;
1196}
1197
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198/* Resolve list of templates for the flow, given policy. */
1199
1200static int
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001201xfrm_tmpl_resolve_one(struct xfrm_policy *policy, struct flowi *fl,
1202 struct xfrm_state **xfrm,
1203 unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204{
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001205 struct net *net = xp_net(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 int nx;
1207 int i, error;
1208 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
1209 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001210 xfrm_address_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
1212 for (nx=0, i = 0; i < policy->xfrm_nr; i++) {
1213 struct xfrm_state *x;
1214 xfrm_address_t *remote = daddr;
1215 xfrm_address_t *local = saddr;
1216 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
1217
Joakim Koskela48b8d782007-07-26 00:08:42 -07001218 if (tmpl->mode == XFRM_MODE_TUNNEL ||
1219 tmpl->mode == XFRM_MODE_BEET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 remote = &tmpl->id.daddr;
1221 local = &tmpl->saddr;
Miika Komu76b3f052006-11-30 16:40:43 -08001222 family = tmpl->encap_family;
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001223 if (xfrm_addr_any(local, family)) {
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001224 error = xfrm_get_saddr(net, &tmp, remote, family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001225 if (error)
1226 goto fail;
1227 local = &tmp;
1228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 }
1230
1231 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
1232
1233 if (x && x->km.state == XFRM_STATE_VALID) {
1234 xfrm[nx++] = x;
1235 daddr = remote;
1236 saddr = local;
1237 continue;
1238 }
1239 if (x) {
1240 error = (x->km.state == XFRM_STATE_ERROR ?
1241 -EINVAL : -EAGAIN);
1242 xfrm_state_put(x);
1243 }
fernando@oss.ntt.coa43222662008-10-23 04:27:19 +00001244 else if (error == -ESRCH)
1245 error = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
1247 if (!tmpl->optional)
1248 goto fail;
1249 }
1250 return nx;
1251
1252fail:
1253 for (nx--; nx>=0; nx--)
1254 xfrm_state_put(xfrm[nx]);
1255 return error;
1256}
1257
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001258static int
1259xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, struct flowi *fl,
1260 struct xfrm_state **xfrm,
1261 unsigned short family)
1262{
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001263 struct xfrm_state *tp[XFRM_MAX_DEPTH];
1264 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001265 int cnx = 0;
1266 int error;
1267 int ret;
1268 int i;
1269
1270 for (i = 0; i < npols; i++) {
1271 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
1272 error = -ENOBUFS;
1273 goto fail;
1274 }
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001275
1276 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001277 if (ret < 0) {
1278 error = ret;
1279 goto fail;
1280 } else
1281 cnx += ret;
1282 }
1283
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001284 /* found states are sorted for outbound processing */
1285 if (npols > 1)
1286 xfrm_state_sort(xfrm, tpp, cnx, family);
1287
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001288 return cnx;
1289
1290 fail:
1291 for (cnx--; cnx>=0; cnx--)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001292 xfrm_state_put(tpp[cnx]);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001293 return error;
1294
1295}
1296
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297/* Check that the bundle accepts the flow and its components are
1298 * still valid.
1299 */
1300
1301static struct dst_entry *
1302xfrm_find_bundle(struct flowi *fl, struct xfrm_policy *policy, unsigned short family)
1303{
1304 struct dst_entry *x;
1305 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1306 if (unlikely(afinfo == NULL))
1307 return ERR_PTR(-EINVAL);
1308 x = afinfo->find_bundle(fl, policy);
1309 xfrm_policy_put_afinfo(afinfo);
1310 return x;
1311}
1312
Herbert Xu25ee3282007-12-11 09:32:34 -08001313static inline int xfrm_get_tos(struct flowi *fl, int family)
1314{
1315 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1316 int tos;
1317
1318 if (!afinfo)
1319 return -EINVAL;
1320
1321 tos = afinfo->get_tos(fl);
1322
1323 xfrm_policy_put_afinfo(afinfo);
1324
1325 return tos;
1326}
1327
1328static inline struct xfrm_dst *xfrm_alloc_dst(int family)
1329{
1330 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1331 struct xfrm_dst *xdst;
1332
1333 if (!afinfo)
1334 return ERR_PTR(-EINVAL);
1335
1336 xdst = dst_alloc(afinfo->dst_ops) ?: ERR_PTR(-ENOBUFS);
1337
1338 xfrm_policy_put_afinfo(afinfo);
1339
1340 return xdst;
1341}
1342
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001343static inline int xfrm_init_path(struct xfrm_dst *path, struct dst_entry *dst,
1344 int nfheader_len)
1345{
1346 struct xfrm_policy_afinfo *afinfo =
1347 xfrm_policy_get_afinfo(dst->ops->family);
1348 int err;
1349
1350 if (!afinfo)
1351 return -EINVAL;
1352
1353 err = afinfo->init_path(path, dst, nfheader_len);
1354
1355 xfrm_policy_put_afinfo(afinfo);
1356
1357 return err;
1358}
1359
Herbert Xu25ee3282007-12-11 09:32:34 -08001360static inline int xfrm_fill_dst(struct xfrm_dst *xdst, struct net_device *dev)
1361{
1362 struct xfrm_policy_afinfo *afinfo =
1363 xfrm_policy_get_afinfo(xdst->u.dst.ops->family);
1364 int err;
1365
1366 if (!afinfo)
1367 return -EINVAL;
1368
1369 err = afinfo->fill_dst(xdst, dev);
1370
1371 xfrm_policy_put_afinfo(afinfo);
1372
1373 return err;
1374}
1375
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376/* Allocate chain of dst_entry's, attach known xfrm's, calculate
1377 * all the metrics... Shortly, bundle a bundle.
1378 */
1379
Herbert Xu25ee3282007-12-11 09:32:34 -08001380static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy,
1381 struct xfrm_state **xfrm, int nx,
1382 struct flowi *fl,
1383 struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384{
Herbert Xu25ee3282007-12-11 09:32:34 -08001385 unsigned long now = jiffies;
1386 struct net_device *dev;
1387 struct dst_entry *dst_prev = NULL;
1388 struct dst_entry *dst0 = NULL;
1389 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 int err;
Herbert Xu25ee3282007-12-11 09:32:34 -08001391 int header_len = 0;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001392 int nfheader_len = 0;
Herbert Xu25ee3282007-12-11 09:32:34 -08001393 int trailer_len = 0;
1394 int tos;
1395 int family = policy->selector.family;
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +09001396 xfrm_address_t saddr, daddr;
1397
1398 xfrm_flowi_addr_get(fl, &saddr, &daddr, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001399
1400 tos = xfrm_get_tos(fl, family);
1401 err = tos;
1402 if (tos < 0)
1403 goto put_states;
1404
1405 dst_hold(dst);
1406
1407 for (; i < nx; i++) {
1408 struct xfrm_dst *xdst = xfrm_alloc_dst(family);
1409 struct dst_entry *dst1 = &xdst->u.dst;
1410
1411 err = PTR_ERR(xdst);
1412 if (IS_ERR(xdst)) {
1413 dst_release(dst);
1414 goto put_states;
1415 }
1416
1417 if (!dst_prev)
1418 dst0 = dst1;
1419 else {
1420 dst_prev->child = dst_clone(dst1);
1421 dst1->flags |= DST_NOHASH;
1422 }
1423
1424 xdst->route = dst;
1425 memcpy(&dst1->metrics, &dst->metrics, sizeof(dst->metrics));
1426
1427 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
1428 family = xfrm[i]->props.family;
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +09001429 dst = xfrm_dst_lookup(xfrm[i], tos, &saddr, &daddr,
1430 family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001431 err = PTR_ERR(dst);
1432 if (IS_ERR(dst))
1433 goto put_states;
1434 } else
1435 dst_hold(dst);
1436
1437 dst1->xfrm = xfrm[i];
1438 xdst->genid = xfrm[i]->genid;
1439
1440 dst1->obsolete = -1;
1441 dst1->flags |= DST_HOST;
1442 dst1->lastuse = now;
1443
1444 dst1->input = dst_discard;
1445 dst1->output = xfrm[i]->outer_mode->afinfo->output;
1446
1447 dst1->next = dst_prev;
1448 dst_prev = dst1;
1449
1450 header_len += xfrm[i]->props.header_len;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001451 if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT)
1452 nfheader_len += xfrm[i]->props.header_len;
Herbert Xu25ee3282007-12-11 09:32:34 -08001453 trailer_len += xfrm[i]->props.trailer_len;
1454 }
1455
1456 dst_prev->child = dst;
1457 dst0->path = dst;
1458
1459 err = -ENODEV;
1460 dev = dst->dev;
1461 if (!dev)
1462 goto free_dst;
1463
1464 /* Copy neighbout for reachability confirmation */
1465 dst0->neighbour = neigh_clone(dst->neighbour);
1466
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001467 xfrm_init_path((struct xfrm_dst *)dst0, dst, nfheader_len);
Herbert Xu25ee3282007-12-11 09:32:34 -08001468 xfrm_init_pmtu(dst_prev);
1469
1470 for (dst_prev = dst0; dst_prev != dst; dst_prev = dst_prev->child) {
1471 struct xfrm_dst *xdst = (struct xfrm_dst *)dst_prev;
1472
1473 err = xfrm_fill_dst(xdst, dev);
1474 if (err)
1475 goto free_dst;
1476
1477 dst_prev->header_len = header_len;
1478 dst_prev->trailer_len = trailer_len;
1479 header_len -= xdst->u.dst.xfrm->props.header_len;
1480 trailer_len -= xdst->u.dst.xfrm->props.trailer_len;
1481 }
1482
1483out:
1484 return dst0;
1485
1486put_states:
1487 for (; i < nx; i++)
1488 xfrm_state_put(xfrm[i]);
1489free_dst:
1490 if (dst0)
1491 dst_free(dst0);
1492 dst0 = ERR_PTR(err);
1493 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494}
1495
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001496static int inline
1497xfrm_dst_alloc_copy(void **target, void *src, int size)
1498{
1499 if (!*target) {
1500 *target = kmalloc(size, GFP_ATOMIC);
1501 if (!*target)
1502 return -ENOMEM;
1503 }
1504 memcpy(*target, src, size);
1505 return 0;
1506}
1507
1508static int inline
1509xfrm_dst_update_parent(struct dst_entry *dst, struct xfrm_selector *sel)
1510{
1511#ifdef CONFIG_XFRM_SUB_POLICY
1512 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1513 return xfrm_dst_alloc_copy((void **)&(xdst->partner),
1514 sel, sizeof(*sel));
1515#else
1516 return 0;
1517#endif
1518}
1519
1520static int inline
1521xfrm_dst_update_origin(struct dst_entry *dst, struct flowi *fl)
1522{
1523#ifdef CONFIG_XFRM_SUB_POLICY
1524 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1525 return xfrm_dst_alloc_copy((void **)&(xdst->origin), fl, sizeof(*fl));
1526#else
1527 return 0;
1528#endif
1529}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
1531static int stale_bundle(struct dst_entry *dst);
1532
1533/* Main function: finds/creates a bundle for given flow.
1534 *
1535 * At the moment we eat a raw IP route. Mostly to speed up lookups
1536 * on interfaces with disabled IPsec.
1537 */
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001538int __xfrm_lookup(struct net *net, struct dst_entry **dst_p, struct flowi *fl,
David S. Miller14e50e52007-05-24 18:17:54 -07001539 struct sock *sk, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540{
1541 struct xfrm_policy *policy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001542 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
1543 int npols;
1544 int pol_dead;
1545 int xfrm_nr;
1546 int pi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
1548 struct dst_entry *dst, *dst_orig = *dst_p;
1549 int nx = 0;
1550 int err;
1551 u32 genid;
Patrick McHardy42cf93c2006-02-21 13:37:35 -08001552 u16 family;
Trent Jaegerdf718372005-12-13 23:12:27 -08001553 u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001554
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555restart:
1556 genid = atomic_read(&flow_cache_genid);
1557 policy = NULL;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001558 for (pi = 0; pi < ARRAY_SIZE(pols); pi++)
1559 pols[pi] = NULL;
1560 npols = 0;
1561 pol_dead = 0;
1562 xfrm_nr = 0;
1563
Thomas Graff7944fb2007-08-25 13:46:55 -07001564 if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001565 policy = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
Herbert Xu75b8c132007-12-11 04:38:08 -08001566 err = PTR_ERR(policy);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001567 if (IS_ERR(policy)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08001568 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
Herbert Xu75b8c132007-12-11 04:38:08 -08001569 goto dropdst;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001570 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
1573 if (!policy) {
1574 /* To accelerate a bit... */
David S. Miller2518c7c2006-08-24 04:45:07 -07001575 if ((dst_orig->flags & DST_NOXFRM) ||
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001576 !net->xfrm.policy_count[XFRM_POLICY_OUT])
Herbert Xu8b7817f2007-12-12 10:44:43 -08001577 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001579 policy = flow_cache_lookup(net, fl, dst_orig->ops->family,
Patrick McHardy42cf93c2006-02-21 13:37:35 -08001580 dir, xfrm_policy_lookup);
Herbert Xu75b8c132007-12-11 04:38:08 -08001581 err = PTR_ERR(policy);
Masahide NAKAMURAd66e37a2008-01-07 21:46:15 -08001582 if (IS_ERR(policy)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08001583 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
Herbert Xu75b8c132007-12-11 04:38:08 -08001584 goto dropdst;
Masahide NAKAMURAd66e37a2008-01-07 21:46:15 -08001585 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 }
1587
1588 if (!policy)
Herbert Xu8b7817f2007-12-12 10:44:43 -08001589 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
Patrick McHardy42cf93c2006-02-21 13:37:35 -08001591 family = dst_orig->ops->family;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001592 pols[0] = policy;
1593 npols ++;
1594 xfrm_nr += pols[0]->xfrm_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
Herbert Xuaef21782007-12-13 09:30:59 -08001596 err = -ENOENT;
Herbert Xu8b7817f2007-12-12 10:44:43 -08001597 if ((flags & XFRM_LOOKUP_ICMP) && !(policy->flags & XFRM_POLICY_ICMP))
1598 goto error;
1599
1600 policy->curlft.use_time = get_seconds();
1601
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 switch (policy->action) {
Herbert Xu5e5234f2007-11-30 00:50:31 +11001603 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 case XFRM_POLICY_BLOCK:
1605 /* Prohibit the flow */
Alexey Dobriyan59c99402008-11-25 17:59:52 -08001606 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLBLOCK);
Patrick McHardye104411b2005-09-08 15:11:55 -07001607 err = -EPERM;
1608 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
1610 case XFRM_POLICY_ALLOW:
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001611#ifndef CONFIG_XFRM_SUB_POLICY
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 if (policy->xfrm_nr == 0) {
1613 /* Flow passes not transformed. */
1614 xfrm_pol_put(policy);
1615 return 0;
1616 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001617#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
1619 /* Try to find matching bundle.
1620 *
1621 * LATER: help from flow cache. It is optional, this
1622 * is required only for output policy.
1623 */
1624 dst = xfrm_find_bundle(fl, policy, family);
1625 if (IS_ERR(dst)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08001626 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLECHECKERROR);
Patrick McHardye104411b2005-09-08 15:11:55 -07001627 err = PTR_ERR(dst);
1628 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 }
1630
1631 if (dst)
1632 break;
1633
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001634#ifdef CONFIG_XFRM_SUB_POLICY
1635 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001636 pols[1] = xfrm_policy_lookup_bytype(net,
1637 XFRM_POLICY_TYPE_MAIN,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001638 fl, family,
1639 XFRM_POLICY_OUT);
1640 if (pols[1]) {
James Morris134b0fc2006-10-05 15:42:27 -05001641 if (IS_ERR(pols[1])) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08001642 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05001643 err = PTR_ERR(pols[1]);
1644 goto error;
1645 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001646 if (pols[1]->action == XFRM_POLICY_BLOCK) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08001647 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLBLOCK);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001648 err = -EPERM;
1649 goto error;
1650 }
1651 npols ++;
1652 xfrm_nr += pols[1]->xfrm_nr;
1653 }
1654 }
1655
1656 /*
1657 * Because neither flowi nor bundle information knows about
1658 * transformation template size. On more than one policy usage
1659 * we can realize whether all of them is bypass or not after
1660 * they are searched. See above not-transformed bypass
1661 * is surrounded by non-sub policy configuration, too.
1662 */
1663 if (xfrm_nr == 0) {
1664 /* Flow passes not transformed. */
1665 xfrm_pols_put(pols, npols);
1666 return 0;
1667 }
1668
1669#endif
1670 nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
1672 if (unlikely(nx<0)) {
1673 err = nx;
David S. Miller14e50e52007-05-24 18:17:54 -07001674 if (err == -EAGAIN && sysctl_xfrm_larval_drop) {
1675 /* EREMOTE tells the caller to generate
1676 * a one-shot blackhole route.
1677 */
Alexey Dobriyan59c99402008-11-25 17:59:52 -08001678 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
David S. Miller14e50e52007-05-24 18:17:54 -07001679 xfrm_pol_put(policy);
1680 return -EREMOTE;
1681 }
Herbert Xu815f4e52007-12-12 10:36:59 -08001682 if (err == -EAGAIN && (flags & XFRM_LOOKUP_WAIT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 DECLARE_WAITQUEUE(wait, current);
1684
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001685 add_wait_queue(&net->xfrm.km_waitq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 set_current_state(TASK_INTERRUPTIBLE);
1687 schedule();
1688 set_current_state(TASK_RUNNING);
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001689 remove_wait_queue(&net->xfrm.km_waitq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001691 nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
1693 if (nx == -EAGAIN && signal_pending(current)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08001694 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 err = -ERESTART;
1696 goto error;
1697 }
1698 if (nx == -EAGAIN ||
1699 genid != atomic_read(&flow_cache_genid)) {
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001700 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 goto restart;
1702 }
1703 err = nx;
1704 }
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001705 if (err < 0) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08001706 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 goto error;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 }
1710 if (nx == 0) {
1711 /* Flow passes not transformed. */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001712 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 return 0;
1714 }
1715
Herbert Xu25ee3282007-12-11 09:32:34 -08001716 dst = xfrm_bundle_create(policy, xfrm, nx, fl, dst_orig);
1717 err = PTR_ERR(dst);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001718 if (IS_ERR(dst)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08001719 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLEGENERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 goto error;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001723 for (pi = 0; pi < npols; pi++) {
1724 read_lock_bh(&pols[pi]->lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001725 pol_dead |= pols[pi]->walk.dead;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001726 read_unlock_bh(&pols[pi]->lock);
1727 }
1728
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 write_lock_bh(&policy->lock);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001730 if (unlikely(pol_dead || stale_bundle(dst))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 /* Wow! While we worked on resolving, this
1732 * policy has gone. Retry. It is not paranoia,
1733 * we just cannot enlist new bundle to dead object.
1734 * We can't enlist stable bundles either.
1735 */
1736 write_unlock_bh(&policy->lock);
Julien Brunel9d7d7402008-09-02 17:24:28 -07001737 dst_free(dst);
Herbert Xu00de6512006-02-13 16:01:27 -08001738
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001739 if (pol_dead)
Alexey Dobriyan59c99402008-11-25 17:59:52 -08001740 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLDEAD);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001741 else
Alexey Dobriyan59c99402008-11-25 17:59:52 -08001742 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLECHECKERROR);
Herbert Xu00de6512006-02-13 16:01:27 -08001743 err = -EHOSTUNREACH;
1744 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 }
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001746
1747 if (npols > 1)
1748 err = xfrm_dst_update_parent(dst, &pols[1]->selector);
1749 else
1750 err = xfrm_dst_update_origin(dst, fl);
1751 if (unlikely(err)) {
1752 write_unlock_bh(&policy->lock);
Julien Brunel9d7d7402008-09-02 17:24:28 -07001753 dst_free(dst);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08001754 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLECHECKERROR);
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001755 goto error;
1756 }
1757
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 dst->next = policy->bundles;
1759 policy->bundles = dst;
1760 dst_hold(dst);
1761 write_unlock_bh(&policy->lock);
1762 }
1763 *dst_p = dst;
1764 dst_release(dst_orig);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001765 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 return 0;
1767
1768error:
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001769 xfrm_pols_put(pols, npols);
Herbert Xu75b8c132007-12-11 04:38:08 -08001770dropdst:
1771 dst_release(dst_orig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 *dst_p = NULL;
1773 return err;
Herbert Xu8b7817f2007-12-12 10:44:43 -08001774
1775nopol:
Herbert Xuaef21782007-12-13 09:30:59 -08001776 err = -ENOENT;
Herbert Xu8b7817f2007-12-12 10:44:43 -08001777 if (flags & XFRM_LOOKUP_ICMP)
1778 goto dropdst;
1779 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780}
David S. Miller14e50e52007-05-24 18:17:54 -07001781EXPORT_SYMBOL(__xfrm_lookup);
1782
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001783int xfrm_lookup(struct net *net, struct dst_entry **dst_p, struct flowi *fl,
David S. Miller14e50e52007-05-24 18:17:54 -07001784 struct sock *sk, int flags)
1785{
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001786 int err = __xfrm_lookup(net, dst_p, fl, sk, flags);
David S. Miller14e50e52007-05-24 18:17:54 -07001787
1788 if (err == -EREMOTE) {
1789 dst_release(*dst_p);
1790 *dst_p = NULL;
1791 err = -EAGAIN;
1792 }
1793
1794 return err;
1795}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796EXPORT_SYMBOL(xfrm_lookup);
1797
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001798static inline int
1799xfrm_secpath_reject(int idx, struct sk_buff *skb, struct flowi *fl)
1800{
1801 struct xfrm_state *x;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001802
1803 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
1804 return 0;
1805 x = skb->sp->xvec[idx];
1806 if (!x->type->reject)
1807 return 0;
Herbert Xu1ecafed2007-10-09 13:24:07 -07001808 return x->type->reject(x, skb, fl);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001809}
1810
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811/* When skb is transformed back to its "native" form, we have to
1812 * check policy restrictions. At the moment we make this in maximally
1813 * stupid way. Shame on me. :-) Of course, connected sockets must
1814 * have policy cached at them.
1815 */
1816
1817static inline int
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001818xfrm_state_ok(struct xfrm_tmpl *tmpl, struct xfrm_state *x,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 unsigned short family)
1820{
1821 if (xfrm_state_kern(x))
Kazunori MIYAZAWA928ba412007-02-13 12:57:16 -08001822 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 return x->id.proto == tmpl->id.proto &&
1824 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
1825 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
1826 x->props.mode == tmpl->mode &&
Herbert Xuc5d18e92008-04-22 00:46:42 -07001827 (tmpl->allalgs || (tmpl->aalgos & (1<<x->props.aalgo)) ||
Masahide NAKAMURAf3bd4842006-08-23 18:00:48 -07001828 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07001829 !(x->props.mode != XFRM_MODE_TRANSPORT &&
1830 xfrm_state_addr_cmp(tmpl, x, family));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831}
1832
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001833/*
1834 * 0 or more than 0 is returned when validation is succeeded (either bypass
1835 * because of optional transport mode, or next index of the mathced secpath
1836 * state with the template.
1837 * -1 is returned when no matching template is found.
1838 * Otherwise "-2 - errored_index" is returned.
1839 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840static inline int
1841xfrm_policy_ok(struct xfrm_tmpl *tmpl, struct sec_path *sp, int start,
1842 unsigned short family)
1843{
1844 int idx = start;
1845
1846 if (tmpl->optional) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07001847 if (tmpl->mode == XFRM_MODE_TRANSPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 return start;
1849 } else
1850 start = -1;
1851 for (; idx < sp->len; idx++) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08001852 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 return ++idx;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001854 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
1855 if (start == -1)
1856 start = -2-idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 break;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 }
1860 return start;
1861}
1862
Herbert Xud5422ef2007-12-12 10:44:16 -08001863int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
1864 unsigned int family, int reverse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865{
1866 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001867 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
1869 if (unlikely(afinfo == NULL))
1870 return -EAFNOSUPPORT;
1871
Herbert Xud5422ef2007-12-12 10:44:16 -08001872 afinfo->decode_session(skb, fl, reverse);
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07001873 err = security_xfrm_decode_session(skb, &fl->secid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 xfrm_policy_put_afinfo(afinfo);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001875 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876}
Herbert Xud5422ef2007-12-12 10:44:16 -08001877EXPORT_SYMBOL(__xfrm_decode_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001879static inline int secpath_has_nontransport(struct sec_path *sp, int k, int *idxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880{
1881 for (; k < sp->len; k++) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001882 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
James Morrisd1d9fac2006-09-01 00:32:12 -07001883 *idxp = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 return 1;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 }
1887
1888 return 0;
1889}
1890
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001891int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 unsigned short family)
1893{
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08001894 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 struct xfrm_policy *pol;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001896 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
1897 int npols = 0;
1898 int xfrm_nr;
1899 int pi;
Herbert Xud5422ef2007-12-12 10:44:16 -08001900 int reverse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 struct flowi fl;
Herbert Xud5422ef2007-12-12 10:44:16 -08001902 u8 fl_dir;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001903 int xerr_idx = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
Herbert Xud5422ef2007-12-12 10:44:16 -08001905 reverse = dir & ~XFRM_POLICY_MASK;
1906 dir &= XFRM_POLICY_MASK;
1907 fl_dir = policy_to_flow_dir(dir);
1908
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001909 if (__xfrm_decode_session(skb, &fl, family, reverse) < 0) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08001910 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001912 }
1913
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -08001914 nf_nat_decode_session(skb, &fl, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
1916 /* First, check used SA against their selectors. */
1917 if (skb->sp) {
1918 int i;
1919
1920 for (i=skb->sp->len-1; i>=0; i--) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08001921 struct xfrm_state *x = skb->sp->xvec[i];
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001922 if (!xfrm_selector_match(&x->sel, &fl, family)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08001923 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 }
1927 }
1928
1929 pol = NULL;
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001930 if (sk && sk->sk_policy[dir]) {
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001931 pol = xfrm_sk_policy_lookup(sk, dir, &fl);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001932 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08001933 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001934 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001935 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001936 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
1938 if (!pol)
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08001939 pol = flow_cache_lookup(net, &fl, family, fl_dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 xfrm_policy_lookup);
1941
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001942 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08001943 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05001944 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001945 }
James Morris134b0fc2006-10-05 15:42:27 -05001946
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001947 if (!pol) {
James Morrisd1d9fac2006-09-01 00:32:12 -07001948 if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001949 xfrm_secpath_reject(xerr_idx, skb, &fl);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08001950 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOPOLS);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001951 return 0;
1952 }
1953 return 1;
1954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
James Morris9d729f72007-03-04 16:12:44 -08001956 pol->curlft.use_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001958 pols[0] = pol;
1959 npols ++;
1960#ifdef CONFIG_XFRM_SUB_POLICY
1961 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08001962 pols[1] = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001963 &fl, family,
1964 XFRM_POLICY_IN);
1965 if (pols[1]) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001966 if (IS_ERR(pols[1])) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08001967 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05001968 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001969 }
James Morris9d729f72007-03-04 16:12:44 -08001970 pols[1]->curlft.use_time = get_seconds();
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001971 npols ++;
1972 }
1973 }
1974#endif
1975
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 if (pol->action == XFRM_POLICY_ALLOW) {
1977 struct sec_path *sp;
1978 static struct sec_path dummy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001979 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001980 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001981 struct xfrm_tmpl **tpp = tp;
1982 int ti = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 int i, k;
1984
1985 if ((sp = skb->sp) == NULL)
1986 sp = &dummy;
1987
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001988 for (pi = 0; pi < npols; pi++) {
1989 if (pols[pi] != pol &&
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001990 pols[pi]->action != XFRM_POLICY_ALLOW) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08001991 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001992 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001993 }
1994 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08001995 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001996 goto reject_error;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001997 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001998 for (i = 0; i < pols[pi]->xfrm_nr; i++)
1999 tpp[ti++] = &pols[pi]->xfrm_vec[i];
2000 }
2001 xfrm_nr = ti;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002002 if (npols > 1) {
2003 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family);
2004 tpp = stp;
2005 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002006
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 /* For each tunnel xfrm, find the first matching tmpl.
2008 * For each tmpl before that, find corresponding xfrm.
2009 * Order is _important_. Later we will implement
2010 * some barriers, but at the moment barriers
2011 * are implied between each two transformations.
2012 */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002013 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
2014 k = xfrm_policy_ok(tpp[i], sp, k, family);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002015 if (k < 0) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002016 if (k < -1)
2017 /* "-2 - errored_index" returned */
2018 xerr_idx = -(2+k);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002019 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 goto reject;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002021 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 }
2023
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002024 if (secpath_has_nontransport(sp, k, &xerr_idx)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002025 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002027 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002029 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 return 1;
2031 }
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002032 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
2034reject:
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002035 xfrm_secpath_reject(xerr_idx, skb, &fl);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002036reject_error:
2037 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 return 0;
2039}
2040EXPORT_SYMBOL(__xfrm_policy_check);
2041
2042int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
2043{
Alexey Dobriyan99a66652008-11-25 17:36:13 -08002044 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 struct flowi fl;
2046
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002047 if (xfrm_decode_session(skb, &fl, family) < 0) {
2048 /* XXX: we should have something like FWDHDRERROR here. */
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002049 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
Alexey Dobriyan99a66652008-11-25 17:36:13 -08002053 return xfrm_lookup(net, &skb->dst, &fl, NULL, 0) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054}
2055EXPORT_SYMBOL(__xfrm_route_forward);
2056
David S. Millerd49c73c2006-08-13 18:55:53 -07002057/* Optimize later using cookies and generation ids. */
2058
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
2060{
David S. Millerd49c73c2006-08-13 18:55:53 -07002061 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
2062 * to "-1" to force all XFRM destinations to get validated by
2063 * dst_ops->check on every use. We do this because when a
2064 * normal route referenced by an XFRM dst is obsoleted we do
2065 * not go looking around for all parent referencing XFRM dsts
2066 * so that we can invalidate them. It is just too much work.
2067 * Instead we make the checks here on every use. For example:
2068 *
2069 * XFRM dst A --> IPv4 dst X
2070 *
2071 * X is the "xdst->route" of A (X is also the "dst->path" of A
2072 * in this example). If X is marked obsolete, "A" will not
2073 * notice. That's what we are validating here via the
2074 * stale_bundle() check.
2075 *
2076 * When a policy's bundle is pruned, we dst_free() the XFRM
2077 * dst which causes it's ->obsolete field to be set to a
2078 * positive non-zero integer. If an XFRM dst has been pruned
2079 * like this, we want to force a new route lookup.
David S. Miller399c1802005-12-19 14:23:23 -08002080 */
David S. Millerd49c73c2006-08-13 18:55:53 -07002081 if (dst->obsolete < 0 && !stale_bundle(dst))
2082 return dst;
2083
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 return NULL;
2085}
2086
2087static int stale_bundle(struct dst_entry *dst)
2088{
Venkat Yekkirala5b368e62006-10-05 15:42:18 -05002089 return !xfrm_bundle_ok(NULL, (struct xfrm_dst *)dst, NULL, AF_UNSPEC, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090}
2091
Herbert Xuaabc9762005-05-03 16:27:10 -07002092void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002095 dst->dev = dev_net(dev)->loopback_dev;
Daniel Lezcanode3cb742007-09-25 19:16:28 -07002096 dev_hold(dst->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 dev_put(dev);
2098 }
2099}
Herbert Xuaabc9762005-05-03 16:27:10 -07002100EXPORT_SYMBOL(xfrm_dst_ifdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
2102static void xfrm_link_failure(struct sk_buff *skb)
2103{
2104 /* Impossible. Such dst must be popped before reaches point of failure. */
2105 return;
2106}
2107
2108static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
2109{
2110 if (dst) {
2111 if (dst->obsolete) {
2112 dst_release(dst);
2113 dst = NULL;
2114 }
2115 }
2116 return dst;
2117}
2118
David S. Miller2518c7c2006-08-24 04:45:07 -07002119static void prune_one_bundle(struct xfrm_policy *pol, int (*func)(struct dst_entry *), struct dst_entry **gc_list_p)
2120{
2121 struct dst_entry *dst, **dstp;
2122
2123 write_lock(&pol->lock);
2124 dstp = &pol->bundles;
2125 while ((dst=*dstp) != NULL) {
2126 if (func(dst)) {
2127 *dstp = dst->next;
2128 dst->next = *gc_list_p;
2129 *gc_list_p = dst;
2130 } else {
2131 dstp = &dst->next;
2132 }
2133 }
2134 write_unlock(&pol->lock);
2135}
2136
Alexey Dobriyan3dd0b492008-11-25 17:36:51 -08002137static void xfrm_prune_bundles(struct net *net, int (*func)(struct dst_entry *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138{
David S. Miller2518c7c2006-08-24 04:45:07 -07002139 struct dst_entry *gc_list = NULL;
2140 int dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
2142 read_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -07002143 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
2144 struct xfrm_policy *pol;
2145 struct hlist_node *entry;
2146 struct hlist_head *table;
2147 int i;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002148
David S. Miller2518c7c2006-08-24 04:45:07 -07002149 hlist_for_each_entry(pol, entry,
Alexey Dobriyan3dd0b492008-11-25 17:36:51 -08002150 &net->xfrm.policy_inexact[dir], bydst)
David S. Miller2518c7c2006-08-24 04:45:07 -07002151 prune_one_bundle(pol, func, &gc_list);
2152
Alexey Dobriyan3dd0b492008-11-25 17:36:51 -08002153 table = net->xfrm.policy_bydst[dir].table;
2154 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
David S. Miller2518c7c2006-08-24 04:45:07 -07002155 hlist_for_each_entry(pol, entry, table + i, bydst)
2156 prune_one_bundle(pol, func, &gc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 }
2158 }
2159 read_unlock_bh(&xfrm_policy_lock);
2160
2161 while (gc_list) {
David S. Miller2518c7c2006-08-24 04:45:07 -07002162 struct dst_entry *dst = gc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 gc_list = dst->next;
2164 dst_free(dst);
2165 }
2166}
2167
2168static int unused_bundle(struct dst_entry *dst)
2169{
2170 return !atomic_read(&dst->__refcnt);
2171}
2172
Alexey Dobriyanddcfd792008-11-25 17:37:23 -08002173static void __xfrm_garbage_collect(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174{
Alexey Dobriyanddcfd792008-11-25 17:37:23 -08002175 xfrm_prune_bundles(net, unused_bundle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176}
2177
Alexey Dobriyan3dd0b492008-11-25 17:36:51 -08002178static int xfrm_flush_bundles(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179{
Alexey Dobriyan3dd0b492008-11-25 17:36:51 -08002180 xfrm_prune_bundles(net, stale_bundle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 return 0;
2182}
2183
Herbert Xu25ee3282007-12-11 09:32:34 -08002184static void xfrm_init_pmtu(struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185{
2186 do {
2187 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2188 u32 pmtu, route_mtu_cached;
2189
2190 pmtu = dst_mtu(dst->child);
2191 xdst->child_mtu_cached = pmtu;
2192
2193 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
2194
2195 route_mtu_cached = dst_mtu(xdst->route);
2196 xdst->route_mtu_cached = route_mtu_cached;
2197
2198 if (pmtu > route_mtu_cached)
2199 pmtu = route_mtu_cached;
2200
2201 dst->metrics[RTAX_MTU-1] = pmtu;
2202 } while ((dst = dst->next));
2203}
2204
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205/* Check that the bundle accepts the flow and its components are
2206 * still valid.
2207 */
2208
Venkat Yekkirala5b368e62006-10-05 15:42:18 -05002209int xfrm_bundle_ok(struct xfrm_policy *pol, struct xfrm_dst *first,
2210 struct flowi *fl, int family, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211{
2212 struct dst_entry *dst = &first->u.dst;
2213 struct xfrm_dst *last;
2214 u32 mtu;
2215
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07002216 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 (dst->dev && !netif_running(dst->dev)))
2218 return 0;
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07002219#ifdef CONFIG_XFRM_SUB_POLICY
2220 if (fl) {
2221 if (first->origin && !flow_cache_uli_match(first->origin, fl))
2222 return 0;
2223 if (first->partner &&
2224 !xfrm_selector_match(first->partner, fl, family))
2225 return 0;
2226 }
2227#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
2229 last = NULL;
2230
2231 do {
2232 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2233
2234 if (fl && !xfrm_selector_match(&dst->xfrm->sel, fl, family))
2235 return 0;
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06002236 if (fl && pol &&
2237 !security_xfrm_state_pol_flow_match(dst->xfrm, pol, fl))
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002238 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 if (dst->xfrm->km.state != XFRM_STATE_VALID)
2240 return 0;
David S. Miller9d4a7062006-08-24 03:18:09 -07002241 if (xdst->genid != dst->xfrm->genid)
2242 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
Herbert Xu1bfcb102007-10-17 21:31:50 -07002244 if (strict && fl &&
Herbert Xu13996372007-10-17 21:35:51 -07002245 !(dst->xfrm->outer_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
Masahide NAKAMURAe53820d2006-08-23 19:12:01 -07002246 !xfrm_state_addr_flow_check(dst->xfrm, fl, family))
2247 return 0;
2248
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 mtu = dst_mtu(dst->child);
2250 if (xdst->child_mtu_cached != mtu) {
2251 last = xdst;
2252 xdst->child_mtu_cached = mtu;
2253 }
2254
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07002255 if (!dst_check(xdst->route, xdst->route_cookie))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 return 0;
2257 mtu = dst_mtu(xdst->route);
2258 if (xdst->route_mtu_cached != mtu) {
2259 last = xdst;
2260 xdst->route_mtu_cached = mtu;
2261 }
2262
2263 dst = dst->child;
2264 } while (dst->xfrm);
2265
2266 if (likely(!last))
2267 return 1;
2268
2269 mtu = last->child_mtu_cached;
2270 for (;;) {
2271 dst = &last->u.dst;
2272
2273 mtu = xfrm_state_mtu(dst->xfrm, mtu);
2274 if (mtu > last->route_mtu_cached)
2275 mtu = last->route_mtu_cached;
2276 dst->metrics[RTAX_MTU-1] = mtu;
2277
2278 if (last == first)
2279 break;
2280
Patrick McHardybd0bf072007-07-18 01:55:52 -07002281 last = (struct xfrm_dst *)last->u.dst.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 last->child_mtu_cached = mtu;
2283 }
2284
2285 return 1;
2286}
2287
2288EXPORT_SYMBOL(xfrm_bundle_ok);
2289
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
2291{
2292 int err = 0;
2293 if (unlikely(afinfo == NULL))
2294 return -EINVAL;
2295 if (unlikely(afinfo->family >= NPROTO))
2296 return -EAFNOSUPPORT;
Ingo Molnare959d812006-04-28 15:32:29 -07002297 write_lock_bh(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
2299 err = -ENOBUFS;
2300 else {
2301 struct dst_ops *dst_ops = afinfo->dst_ops;
2302 if (likely(dst_ops->kmem_cachep == NULL))
2303 dst_ops->kmem_cachep = xfrm_dst_cache;
2304 if (likely(dst_ops->check == NULL))
2305 dst_ops->check = xfrm_dst_check;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 if (likely(dst_ops->negative_advice == NULL))
2307 dst_ops->negative_advice = xfrm_negative_advice;
2308 if (likely(dst_ops->link_failure == NULL))
2309 dst_ops->link_failure = xfrm_link_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 if (likely(afinfo->garbage_collect == NULL))
2311 afinfo->garbage_collect = __xfrm_garbage_collect;
2312 xfrm_policy_afinfo[afinfo->family] = afinfo;
2313 }
Ingo Molnare959d812006-04-28 15:32:29 -07002314 write_unlock_bh(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 return err;
2316}
2317EXPORT_SYMBOL(xfrm_policy_register_afinfo);
2318
2319int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
2320{
2321 int err = 0;
2322 if (unlikely(afinfo == NULL))
2323 return -EINVAL;
2324 if (unlikely(afinfo->family >= NPROTO))
2325 return -EAFNOSUPPORT;
Ingo Molnare959d812006-04-28 15:32:29 -07002326 write_lock_bh(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
2328 if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
2329 err = -EINVAL;
2330 else {
2331 struct dst_ops *dst_ops = afinfo->dst_ops;
2332 xfrm_policy_afinfo[afinfo->family] = NULL;
2333 dst_ops->kmem_cachep = NULL;
2334 dst_ops->check = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 dst_ops->negative_advice = NULL;
2336 dst_ops->link_failure = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 afinfo->garbage_collect = NULL;
2338 }
2339 }
Ingo Molnare959d812006-04-28 15:32:29 -07002340 write_unlock_bh(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 return err;
2342}
2343EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
2344
2345static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
2346{
2347 struct xfrm_policy_afinfo *afinfo;
2348 if (unlikely(family >= NPROTO))
2349 return NULL;
2350 read_lock(&xfrm_policy_afinfo_lock);
2351 afinfo = xfrm_policy_afinfo[family];
Herbert Xu546be242006-05-27 23:03:58 -07002352 if (unlikely(!afinfo))
2353 read_unlock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 return afinfo;
2355}
2356
2357static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
2358{
Herbert Xu546be242006-05-27 23:03:58 -07002359 read_unlock(&xfrm_policy_afinfo_lock);
2360}
2361
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
2363{
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02002364 struct net_device *dev = ptr;
2365
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 switch (event) {
2367 case NETDEV_DOWN:
Alexey Dobriyan3dd0b492008-11-25 17:36:51 -08002368 xfrm_flush_bundles(dev_net(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 }
2370 return NOTIFY_DONE;
2371}
2372
2373static struct notifier_block xfrm_dev_notifier = {
Alexey Dobriyand5917a32008-10-31 00:41:59 -07002374 .notifier_call = xfrm_dev_event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375};
2376
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002377#ifdef CONFIG_XFRM_STATISTICS
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002378static int __net_init xfrm_statistics_init(struct net *net)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002379{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002380 int rv;
2381
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002382 if (snmp_mib_init((void **)net->mib.xfrm_statistics,
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002383 sizeof(struct linux_xfrm_mib)) < 0)
2384 return -ENOMEM;
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002385 rv = xfrm_proc_init(net);
2386 if (rv < 0)
2387 snmp_mib_free((void **)net->mib.xfrm_statistics);
2388 return rv;
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002389}
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002390
2391static void xfrm_statistics_fini(struct net *net)
2392{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002393 xfrm_proc_fini(net);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002394 snmp_mib_free((void **)net->mib.xfrm_statistics);
2395}
2396#else
2397static int __net_init xfrm_statistics_init(struct net *net)
2398{
2399 return 0;
2400}
2401
2402static void xfrm_statistics_fini(struct net *net)
2403{
2404}
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002405#endif
2406
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002407static int __net_init xfrm_policy_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408{
David S. Miller2518c7c2006-08-24 04:45:07 -07002409 unsigned int hmask, sz;
2410 int dir;
2411
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002412 if (net_eq(net, &init_net))
2413 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 sizeof(struct xfrm_dst),
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002415 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002416 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417
David S. Miller2518c7c2006-08-24 04:45:07 -07002418 hmask = 8 - 1;
2419 sz = (hmask+1) * sizeof(struct hlist_head);
2420
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002421 net->xfrm.policy_byidx = xfrm_hash_alloc(sz);
2422 if (!net->xfrm.policy_byidx)
2423 goto out_byidx;
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08002424 net->xfrm.policy_idx_hmask = hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -07002425
2426 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
2427 struct xfrm_policy_hash *htab;
2428
Alexey Dobriyandc2caba2008-11-25 17:24:15 -08002429 net->xfrm.policy_count[dir] = 0;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002430 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
David S. Miller2518c7c2006-08-24 04:45:07 -07002431
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002432 htab = &net->xfrm.policy_bydst[dir];
David S. Miller44e36b42006-08-24 04:50:50 -07002433 htab->table = xfrm_hash_alloc(sz);
David S. Miller2518c7c2006-08-24 04:45:07 -07002434 if (!htab->table)
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002435 goto out_bydst;
2436 htab->hmask = hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -07002437 }
2438
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08002439 INIT_LIST_HEAD(&net->xfrm.policy_all);
Alexey Dobriyan66caf622008-11-25 17:28:57 -08002440 INIT_WORK(&net->xfrm.policy_hash_work, xfrm_hash_resize);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002441 if (net_eq(net, &init_net))
2442 register_netdevice_notifier(&xfrm_dev_notifier);
2443 return 0;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002444
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002445out_bydst:
2446 for (dir--; dir >= 0; dir--) {
2447 struct xfrm_policy_hash *htab;
2448
2449 htab = &net->xfrm.policy_bydst[dir];
2450 xfrm_hash_free(htab->table, sz);
2451 }
2452 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002453out_byidx:
2454 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455}
2456
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002457static void xfrm_policy_fini(struct net *net)
2458{
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002459 struct xfrm_audit audit_info;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002460 unsigned int sz;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002461 int dir;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002462
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002463 flush_work(&net->xfrm.policy_hash_work);
2464#ifdef CONFIG_XFRM_SUB_POLICY
2465 audit_info.loginuid = -1;
2466 audit_info.sessionid = -1;
2467 audit_info.secid = 0;
2468 xfrm_policy_flush(net, XFRM_POLICY_TYPE_SUB, &audit_info);
2469#endif
2470 audit_info.loginuid = -1;
2471 audit_info.sessionid = -1;
2472 audit_info.secid = 0;
2473 xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, &audit_info);
2474 flush_work(&xfrm_policy_gc_work);
2475
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08002476 WARN_ON(!list_empty(&net->xfrm.policy_all));
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002477
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002478 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002479 struct xfrm_policy_hash *htab;
2480
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002481 WARN_ON(!hlist_empty(&net->xfrm.policy_inexact[dir]));
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002482
2483 htab = &net->xfrm.policy_bydst[dir];
2484 sz = (htab->hmask + 1);
2485 WARN_ON(!hlist_empty(htab->table));
2486 xfrm_hash_free(htab->table, sz);
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002487 }
2488
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08002489 sz = (net->xfrm.policy_idx_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002490 WARN_ON(!hlist_empty(net->xfrm.policy_byidx));
2491 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002492}
2493
2494static int __net_init xfrm_net_init(struct net *net)
2495{
2496 int rv;
2497
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002498 rv = xfrm_statistics_init(net);
2499 if (rv < 0)
2500 goto out_statistics;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002501 rv = xfrm_state_init(net);
2502 if (rv < 0)
2503 goto out_state;
2504 rv = xfrm_policy_init(net);
2505 if (rv < 0)
2506 goto out_policy;
2507 return 0;
2508
2509out_policy:
2510 xfrm_state_fini(net);
2511out_state:
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002512 xfrm_statistics_fini(net);
2513out_statistics:
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002514 return rv;
2515}
2516
2517static void __net_exit xfrm_net_exit(struct net *net)
2518{
2519 xfrm_policy_fini(net);
2520 xfrm_state_fini(net);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002521 xfrm_statistics_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002522}
2523
2524static struct pernet_operations __net_initdata xfrm_net_ops = {
2525 .init = xfrm_net_init,
2526 .exit = xfrm_net_exit,
2527};
2528
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529void __init xfrm_init(void)
2530{
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002531 register_pernet_subsys(&xfrm_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 xfrm_input_init();
2533}
2534
Joy Lattenab5f5e82007-09-17 11:51:22 -07002535#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinen1486cbd72008-01-12 03:20:03 -08002536static void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
2537 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002538{
Paul Moore875179f2007-12-01 23:27:18 +11002539 struct xfrm_sec_ctx *ctx = xp->security;
2540 struct xfrm_selector *sel = &xp->selector;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002541
Paul Moore875179f2007-12-01 23:27:18 +11002542 if (ctx)
2543 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
2544 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
2545
2546 switch(sel->family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07002547 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07002548 audit_log_format(audit_buf, " src=%pI4", &sel->saddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11002549 if (sel->prefixlen_s != 32)
2550 audit_log_format(audit_buf, " src_prefixlen=%d",
2551 sel->prefixlen_s);
Harvey Harrison21454aa2008-10-31 00:54:56 -07002552 audit_log_format(audit_buf, " dst=%pI4", &sel->daddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11002553 if (sel->prefixlen_d != 32)
2554 audit_log_format(audit_buf, " dst_prefixlen=%d",
2555 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002556 break;
2557 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002558 audit_log_format(audit_buf, " src=%pI6", sel->saddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11002559 if (sel->prefixlen_s != 128)
2560 audit_log_format(audit_buf, " src_prefixlen=%d",
2561 sel->prefixlen_s);
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002562 audit_log_format(audit_buf, " dst=%pI6", sel->daddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11002563 if (sel->prefixlen_d != 128)
2564 audit_log_format(audit_buf, " dst_prefixlen=%d",
2565 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002566 break;
2567 }
2568}
2569
Paul Moore68277ac2007-12-20 20:49:33 -08002570void xfrm_audit_policy_add(struct xfrm_policy *xp, int result,
Eric Paris25323862008-04-18 10:09:25 -04002571 uid_t auid, u32 sessionid, u32 secid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002572{
2573 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002574
Paul Mooreafeb14b2007-12-21 14:58:11 -08002575 audit_buf = xfrm_audit_start("SPD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002576 if (audit_buf == NULL)
2577 return;
Eric Paris25323862008-04-18 10:09:25 -04002578 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002579 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002580 xfrm_audit_common_policyinfo(xp, audit_buf);
2581 audit_log_end(audit_buf);
2582}
2583EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
2584
Paul Moore68277ac2007-12-20 20:49:33 -08002585void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
Eric Paris25323862008-04-18 10:09:25 -04002586 uid_t auid, u32 sessionid, u32 secid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002587{
2588 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002589
Paul Mooreafeb14b2007-12-21 14:58:11 -08002590 audit_buf = xfrm_audit_start("SPD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002591 if (audit_buf == NULL)
2592 return;
Eric Paris25323862008-04-18 10:09:25 -04002593 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002594 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002595 xfrm_audit_common_policyinfo(xp, audit_buf);
2596 audit_log_end(audit_buf);
2597}
2598EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
2599#endif
2600
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002601#ifdef CONFIG_XFRM_MIGRATE
2602static int xfrm_migrate_selector_match(struct xfrm_selector *sel_cmp,
2603 struct xfrm_selector *sel_tgt)
2604{
2605 if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
2606 if (sel_tgt->family == sel_cmp->family &&
2607 xfrm_addr_cmp(&sel_tgt->daddr, &sel_cmp->daddr,
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002608 sel_cmp->family) == 0 &&
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002609 xfrm_addr_cmp(&sel_tgt->saddr, &sel_cmp->saddr,
2610 sel_cmp->family) == 0 &&
2611 sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
2612 sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
2613 return 1;
2614 }
2615 } else {
2616 if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
2617 return 1;
2618 }
2619 }
2620 return 0;
2621}
2622
2623static struct xfrm_policy * xfrm_migrate_policy_find(struct xfrm_selector *sel,
2624 u8 dir, u8 type)
2625{
2626 struct xfrm_policy *pol, *ret = NULL;
2627 struct hlist_node *entry;
2628 struct hlist_head *chain;
2629 u32 priority = ~0U;
2630
2631 read_lock_bh(&xfrm_policy_lock);
Alexey Dobriyan11219942008-11-25 17:33:06 -08002632 chain = policy_hash_direct(&init_net, &sel->daddr, &sel->saddr, sel->family, dir);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002633 hlist_for_each_entry(pol, entry, chain, bydst) {
2634 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
2635 pol->type == type) {
2636 ret = pol;
2637 priority = ret->priority;
2638 break;
2639 }
2640 }
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002641 chain = &init_net.xfrm.policy_inexact[dir];
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002642 hlist_for_each_entry(pol, entry, chain, bydst) {
2643 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
2644 pol->type == type &&
2645 pol->priority < priority) {
2646 ret = pol;
2647 break;
2648 }
2649 }
2650
2651 if (ret)
2652 xfrm_pol_hold(ret);
2653
2654 read_unlock_bh(&xfrm_policy_lock);
2655
2656 return ret;
2657}
2658
2659static int migrate_tmpl_match(struct xfrm_migrate *m, struct xfrm_tmpl *t)
2660{
2661 int match = 0;
2662
2663 if (t->mode == m->mode && t->id.proto == m->proto &&
2664 (m->reqid == 0 || t->reqid == m->reqid)) {
2665 switch (t->mode) {
2666 case XFRM_MODE_TUNNEL:
2667 case XFRM_MODE_BEET:
2668 if (xfrm_addr_cmp(&t->id.daddr, &m->old_daddr,
2669 m->old_family) == 0 &&
2670 xfrm_addr_cmp(&t->saddr, &m->old_saddr,
2671 m->old_family) == 0) {
2672 match = 1;
2673 }
2674 break;
2675 case XFRM_MODE_TRANSPORT:
2676 /* in case of transport mode, template does not store
2677 any IP addresses, hence we just compare mode and
2678 protocol */
2679 match = 1;
2680 break;
2681 default:
2682 break;
2683 }
2684 }
2685 return match;
2686}
2687
2688/* update endpoint address(es) of template(s) */
2689static int xfrm_policy_migrate(struct xfrm_policy *pol,
2690 struct xfrm_migrate *m, int num_migrate)
2691{
2692 struct xfrm_migrate *mp;
2693 struct dst_entry *dst;
2694 int i, j, n = 0;
2695
2696 write_lock_bh(&pol->lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07002697 if (unlikely(pol->walk.dead)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002698 /* target policy has been deleted */
2699 write_unlock_bh(&pol->lock);
2700 return -ENOENT;
2701 }
2702
2703 for (i = 0; i < pol->xfrm_nr; i++) {
2704 for (j = 0, mp = m; j < num_migrate; j++, mp++) {
2705 if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
2706 continue;
2707 n++;
Herbert Xu1bfcb102007-10-17 21:31:50 -07002708 if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
2709 pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002710 continue;
2711 /* update endpoints */
2712 memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
2713 sizeof(pol->xfrm_vec[i].id.daddr));
2714 memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
2715 sizeof(pol->xfrm_vec[i].saddr));
2716 pol->xfrm_vec[i].encap_family = mp->new_family;
2717 /* flush bundles */
2718 while ((dst = pol->bundles) != NULL) {
2719 pol->bundles = dst->next;
2720 dst_free(dst);
2721 }
2722 }
2723 }
2724
2725 write_unlock_bh(&pol->lock);
2726
2727 if (!n)
2728 return -ENODATA;
2729
2730 return 0;
2731}
2732
2733static int xfrm_migrate_check(struct xfrm_migrate *m, int num_migrate)
2734{
2735 int i, j;
2736
2737 if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
2738 return -EINVAL;
2739
2740 for (i = 0; i < num_migrate; i++) {
2741 if ((xfrm_addr_cmp(&m[i].old_daddr, &m[i].new_daddr,
2742 m[i].old_family) == 0) &&
2743 (xfrm_addr_cmp(&m[i].old_saddr, &m[i].new_saddr,
2744 m[i].old_family) == 0))
2745 return -EINVAL;
2746 if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
2747 xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
2748 return -EINVAL;
2749
2750 /* check if there is any duplicated entry */
2751 for (j = i + 1; j < num_migrate; j++) {
2752 if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
2753 sizeof(m[i].old_daddr)) &&
2754 !memcmp(&m[i].old_saddr, &m[j].old_saddr,
2755 sizeof(m[i].old_saddr)) &&
2756 m[i].proto == m[j].proto &&
2757 m[i].mode == m[j].mode &&
2758 m[i].reqid == m[j].reqid &&
2759 m[i].old_family == m[j].old_family)
2760 return -EINVAL;
2761 }
2762 }
2763
2764 return 0;
2765}
2766
2767int xfrm_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002768 struct xfrm_migrate *m, int num_migrate,
2769 struct xfrm_kmaddress *k)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002770{
2771 int i, err, nx_cur = 0, nx_new = 0;
2772 struct xfrm_policy *pol = NULL;
2773 struct xfrm_state *x, *xc;
2774 struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
2775 struct xfrm_state *x_new[XFRM_MAX_DEPTH];
2776 struct xfrm_migrate *mp;
2777
2778 if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
2779 goto out;
2780
2781 /* Stage 1 - find policy */
2782 if ((pol = xfrm_migrate_policy_find(sel, dir, type)) == NULL) {
2783 err = -ENOENT;
2784 goto out;
2785 }
2786
2787 /* Stage 2 - find and update state(s) */
2788 for (i = 0, mp = m; i < num_migrate; i++, mp++) {
2789 if ((x = xfrm_migrate_state_find(mp))) {
2790 x_cur[nx_cur] = x;
2791 nx_cur++;
2792 if ((xc = xfrm_state_migrate(x, mp))) {
2793 x_new[nx_new] = xc;
2794 nx_new++;
2795 } else {
2796 err = -ENODATA;
2797 goto restore_state;
2798 }
2799 }
2800 }
2801
2802 /* Stage 3 - update policy */
2803 if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
2804 goto restore_state;
2805
2806 /* Stage 4 - delete old state(s) */
2807 if (nx_cur) {
2808 xfrm_states_put(x_cur, nx_cur);
2809 xfrm_states_delete(x_cur, nx_cur);
2810 }
2811
2812 /* Stage 5 - announce */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002813 km_migrate(sel, dir, type, m, num_migrate, k);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002814
2815 xfrm_pol_put(pol);
2816
2817 return 0;
2818out:
2819 return err;
2820
2821restore_state:
2822 if (pol)
2823 xfrm_pol_put(pol);
2824 if (nx_cur)
2825 xfrm_states_put(x_cur, nx_cur);
2826 if (nx_new)
2827 xfrm_states_delete(x_new, nx_new);
2828
2829 return err;
2830}
David S. Millere610e672007-02-08 13:29:15 -08002831EXPORT_SYMBOL(xfrm_migrate);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002832#endif