blob: 41eabc46f110d9cb607cb24f8af68054b91bcccd [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>
Eric Paris6ce74ec2012-02-16 15:08:39 -050029#include <net/flow.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <net/xfrm.h>
31#include <net/ip.h>
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -080032#ifdef CONFIG_XFRM_STATISTICS
33#include <net/snmp.h>
34#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
David S. Miller44e36b42006-08-24 04:50:50 -070036#include "xfrm_hash.h"
37
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080038DEFINE_MUTEX(xfrm_cfg_mutex);
39EXPORT_SYMBOL(xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Timo Teräs80c802f2010-04-07 00:30:05 +000041static DEFINE_SPINLOCK(xfrm_policy_sk_bundle_lock);
42static struct dst_entry *xfrm_policy_sk_bundles;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043static DEFINE_RWLOCK(xfrm_policy_lock);
44
Priyanka Jain418a99a2012-08-12 21:22:29 +000045static DEFINE_SPINLOCK(xfrm_policy_afinfo_lock);
46static struct xfrm_policy_afinfo __rcu *xfrm_policy_afinfo[NPROTO]
47 __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Christoph Lametere18b8902006-12-06 20:33:20 -080049static struct kmem_cache *xfrm_dst_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Herbert Xu25ee3282007-12-11 09:32:34 -080051static void xfrm_init_pmtu(struct dst_entry *dst);
Timo Teräs80c802f2010-04-07 00:30:05 +000052static int stale_bundle(struct dst_entry *dst);
Steffen Klassert12fdb4d2011-06-29 23:18:20 +000053static int xfrm_bundle_ok(struct xfrm_dst *xdst);
stephen hemminger1c4c40c2010-10-15 05:14:19 +000054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Wei Yongjun29fa0b302008-12-03 00:33:09 -080056static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
57 int dir);
58
David S. Millerbc9b35a2012-05-15 15:04:57 -040059static inline bool
David S. Miller200ce962011-02-24 00:12:25 -050060__xfrm4_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
Andrew Morton77681022006-11-08 22:46:26 -080061{
David S. Miller7e1dc7b2011-03-12 02:42:11 -050062 const struct flowi4 *fl4 = &fl->u.ip4;
63
Alexey Dobriyan26bff942011-11-22 06:46:02 +000064 return addr4_match(fl4->daddr, sel->daddr.a4, sel->prefixlen_d) &&
65 addr4_match(fl4->saddr, sel->saddr.a4, sel->prefixlen_s) &&
David S. Miller7e1dc7b2011-03-12 02:42:11 -050066 !((xfrm_flowi_dport(fl, &fl4->uli) ^ sel->dport) & sel->dport_mask) &&
67 !((xfrm_flowi_sport(fl, &fl4->uli) ^ sel->sport) & sel->sport_mask) &&
68 (fl4->flowi4_proto == sel->proto || !sel->proto) &&
69 (fl4->flowi4_oif == sel->ifindex || !sel->ifindex);
Andrew Morton77681022006-11-08 22:46:26 -080070}
71
David S. Millerbc9b35a2012-05-15 15:04:57 -040072static inline bool
David S. Miller200ce962011-02-24 00:12:25 -050073__xfrm6_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
Andrew Morton77681022006-11-08 22:46:26 -080074{
David S. Miller7e1dc7b2011-03-12 02:42:11 -050075 const struct flowi6 *fl6 = &fl->u.ip6;
76
77 return addr_match(&fl6->daddr, &sel->daddr, sel->prefixlen_d) &&
78 addr_match(&fl6->saddr, &sel->saddr, sel->prefixlen_s) &&
79 !((xfrm_flowi_dport(fl, &fl6->uli) ^ sel->dport) & sel->dport_mask) &&
80 !((xfrm_flowi_sport(fl, &fl6->uli) ^ sel->sport) & sel->sport_mask) &&
81 (fl6->flowi6_proto == sel->proto || !sel->proto) &&
82 (fl6->flowi6_oif == sel->ifindex || !sel->ifindex);
Andrew Morton77681022006-11-08 22:46:26 -080083}
84
David S. Millerbc9b35a2012-05-15 15:04:57 -040085bool xfrm_selector_match(const struct xfrm_selector *sel, const struct flowi *fl,
86 unsigned short family)
Andrew Morton77681022006-11-08 22:46:26 -080087{
88 switch (family) {
89 case AF_INET:
90 return __xfrm4_selector_match(sel, fl);
91 case AF_INET6:
92 return __xfrm6_selector_match(sel, fl);
93 }
David S. Millerbc9b35a2012-05-15 15:04:57 -040094 return false;
Andrew Morton77681022006-11-08 22:46:26 -080095}
96
Eric Dumazetef8531b2012-08-19 12:31:48 +020097static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
98{
99 struct xfrm_policy_afinfo *afinfo;
100
101 if (unlikely(family >= NPROTO))
102 return NULL;
103 rcu_read_lock();
104 afinfo = rcu_dereference(xfrm_policy_afinfo[family]);
105 if (unlikely(!afinfo))
106 rcu_read_unlock();
107 return afinfo;
108}
109
110static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
111{
112 rcu_read_unlock();
113}
114
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800115static inline struct dst_entry *__xfrm_dst_lookup(struct net *net, int tos,
David S. Miller6418c4e2011-02-24 00:16:53 -0500116 const xfrm_address_t *saddr,
117 const xfrm_address_t *daddr,
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900118 int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800120 struct xfrm_policy_afinfo *afinfo;
121 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Herbert Xu25ee3282007-12-11 09:32:34 -0800123 afinfo = xfrm_policy_get_afinfo(family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 if (unlikely(afinfo == NULL))
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800125 return ERR_PTR(-EAFNOSUPPORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800127 dst = afinfo->dst_lookup(net, tos, saddr, daddr);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 xfrm_policy_put_afinfo(afinfo);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900130
131 return dst;
132}
133
134static inline struct dst_entry *xfrm_dst_lookup(struct xfrm_state *x, int tos,
135 xfrm_address_t *prev_saddr,
136 xfrm_address_t *prev_daddr,
137 int family)
138{
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800139 struct net *net = xs_net(x);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900140 xfrm_address_t *saddr = &x->props.saddr;
141 xfrm_address_t *daddr = &x->id.daddr;
142 struct dst_entry *dst;
143
144 if (x->type->flags & XFRM_TYPE_LOCAL_COADDR) {
145 saddr = x->coaddr;
146 daddr = prev_daddr;
147 }
148 if (x->type->flags & XFRM_TYPE_REMOTE_COADDR) {
149 saddr = prev_saddr;
150 daddr = x->coaddr;
151 }
152
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800153 dst = __xfrm_dst_lookup(net, tos, saddr, daddr, family);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900154
155 if (!IS_ERR(dst)) {
156 if (prev_saddr != saddr)
157 memcpy(prev_saddr, saddr, sizeof(*prev_saddr));
158 if (prev_daddr != daddr)
159 memcpy(prev_daddr, daddr, sizeof(*prev_daddr));
160 }
161
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800162 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165static inline unsigned long make_jiffies(long secs)
166{
167 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
168 return MAX_SCHEDULE_TIMEOUT-1;
169 else
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900170 return secs*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
173static void xfrm_policy_timer(unsigned long data)
174{
175 struct xfrm_policy *xp = (struct xfrm_policy*)data;
James Morris9d729f72007-03-04 16:12:44 -0800176 unsigned long now = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 long next = LONG_MAX;
178 int warn = 0;
179 int dir;
180
181 read_lock(&xp->lock);
182
Timo Teräsea2dea92010-03-31 00:17:05 +0000183 if (unlikely(xp->walk.dead))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 goto out;
185
Herbert Xu77d8d7a2005-10-05 12:15:12 -0700186 dir = xfrm_policy_id2dir(xp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188 if (xp->lft.hard_add_expires_seconds) {
189 long tmo = xp->lft.hard_add_expires_seconds +
190 xp->curlft.add_time - now;
191 if (tmo <= 0)
192 goto expired;
193 if (tmo < next)
194 next = tmo;
195 }
196 if (xp->lft.hard_use_expires_seconds) {
197 long tmo = xp->lft.hard_use_expires_seconds +
198 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
199 if (tmo <= 0)
200 goto expired;
201 if (tmo < next)
202 next = tmo;
203 }
204 if (xp->lft.soft_add_expires_seconds) {
205 long tmo = xp->lft.soft_add_expires_seconds +
206 xp->curlft.add_time - now;
207 if (tmo <= 0) {
208 warn = 1;
209 tmo = XFRM_KM_TIMEOUT;
210 }
211 if (tmo < next)
212 next = tmo;
213 }
214 if (xp->lft.soft_use_expires_seconds) {
215 long tmo = xp->lft.soft_use_expires_seconds +
216 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
217 if (tmo <= 0) {
218 warn = 1;
219 tmo = XFRM_KM_TIMEOUT;
220 }
221 if (tmo < next)
222 next = tmo;
223 }
224
225 if (warn)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800226 km_policy_expired(xp, dir, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 if (next != LONG_MAX &&
228 !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
229 xfrm_pol_hold(xp);
230
231out:
232 read_unlock(&xp->lock);
233 xfrm_pol_put(xp);
234 return;
235
236expired:
237 read_unlock(&xp->lock);
Herbert Xu4666faa2005-06-18 22:43:22 -0700238 if (!xfrm_policy_delete(xp, dir))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800239 km_policy_expired(xp, dir, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 xfrm_pol_put(xp);
241}
242
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000243static struct flow_cache_object *xfrm_policy_flo_get(struct flow_cache_object *flo)
244{
245 struct xfrm_policy *pol = container_of(flo, struct xfrm_policy, flo);
246
247 if (unlikely(pol->walk.dead))
248 flo = NULL;
249 else
250 xfrm_pol_hold(pol);
251
252 return flo;
253}
254
255static int xfrm_policy_flo_check(struct flow_cache_object *flo)
256{
257 struct xfrm_policy *pol = container_of(flo, struct xfrm_policy, flo);
258
259 return !pol->walk.dead;
260}
261
262static void xfrm_policy_flo_delete(struct flow_cache_object *flo)
263{
264 xfrm_pol_put(container_of(flo, struct xfrm_policy, flo));
265}
266
267static const struct flow_cache_ops xfrm_policy_fc_ops = {
268 .get = xfrm_policy_flo_get,
269 .check = xfrm_policy_flo_check,
270 .delete = xfrm_policy_flo_delete,
271};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273/* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
274 * SPD calls.
275 */
276
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -0800277struct xfrm_policy *xfrm_policy_alloc(struct net *net, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
279 struct xfrm_policy *policy;
280
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700281 policy = kzalloc(sizeof(struct xfrm_policy), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 if (policy) {
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -0800284 write_pnet(&policy->xp_net, net);
Herbert Xu12a169e2008-10-01 07:03:24 -0700285 INIT_LIST_HEAD(&policy->walk.all);
David S. Miller2518c7c2006-08-24 04:45:07 -0700286 INIT_HLIST_NODE(&policy->bydst);
287 INIT_HLIST_NODE(&policy->byidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 rwlock_init(&policy->lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700289 atomic_set(&policy->refcnt, 1);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800290 setup_timer(&policy->timer, xfrm_policy_timer,
291 (unsigned long)policy);
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000292 policy->flo.ops = &xfrm_policy_fc_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
294 return policy;
295}
296EXPORT_SYMBOL(xfrm_policy_alloc);
297
298/* Destroy xfrm_policy: descendant resources must be released to this moment. */
299
WANG Cong64c31b32008-01-07 22:34:29 -0800300void xfrm_policy_destroy(struct xfrm_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
Herbert Xu12a169e2008-10-01 07:03:24 -0700302 BUG_ON(!policy->walk.dead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 if (del_timer(&policy->timer))
305 BUG();
306
Paul Moore03e1ad72008-04-12 19:07:52 -0700307 security_xfrm_policy_free(policy->security);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 kfree(policy);
309}
WANG Cong64c31b32008-01-07 22:34:29 -0800310EXPORT_SYMBOL(xfrm_policy_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312/* Rule must be locked. Release descentant resources, announce
313 * entry dead. The rule must be unlinked from lists to the moment.
314 */
315
316static void xfrm_policy_kill(struct xfrm_policy *policy)
317{
Herbert Xu12a169e2008-10-01 07:03:24 -0700318 policy->walk.dead = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Timo Teräs285ead12010-04-07 00:30:06 +0000320 atomic_inc(&policy->genid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Timo Teräs285ead12010-04-07 00:30:06 +0000322 if (del_timer(&policy->timer))
323 xfrm_pol_put(policy);
324
325 xfrm_pol_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326}
327
David S. Miller2518c7c2006-08-24 04:45:07 -0700328static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024;
329
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800330static inline unsigned int idx_hash(struct net *net, u32 index)
David S. Miller2518c7c2006-08-24 04:45:07 -0700331{
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800332 return __idx_hash(index, net->xfrm.policy_idx_hmask);
David S. Miller2518c7c2006-08-24 04:45:07 -0700333}
334
David S. Miller5f803b52011-02-24 00:33:19 -0500335static struct hlist_head *policy_hash_bysel(struct net *net,
336 const struct xfrm_selector *sel,
337 unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700338{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800339 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700340 unsigned int hash = __sel_hash(sel, family, hmask);
341
342 return (hash == hmask + 1 ?
Alexey Dobriyan11219942008-11-25 17:33:06 -0800343 &net->xfrm.policy_inexact[dir] :
344 net->xfrm.policy_bydst[dir].table + hash);
David S. Miller2518c7c2006-08-24 04:45:07 -0700345}
346
David S. Miller5f803b52011-02-24 00:33:19 -0500347static struct hlist_head *policy_hash_direct(struct net *net,
348 const xfrm_address_t *daddr,
349 const xfrm_address_t *saddr,
350 unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700351{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800352 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700353 unsigned int hash = __addr_hash(daddr, saddr, family, hmask);
354
Alexey Dobriyan11219942008-11-25 17:33:06 -0800355 return net->xfrm.policy_bydst[dir].table + hash;
David S. Miller2518c7c2006-08-24 04:45:07 -0700356}
357
David S. Miller2518c7c2006-08-24 04:45:07 -0700358static void xfrm_dst_hash_transfer(struct hlist_head *list,
359 struct hlist_head *ndsttable,
360 unsigned int nhashmask)
361{
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800362 struct hlist_node *entry, *tmp, *entry0 = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700363 struct xfrm_policy *pol;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800364 unsigned int h0 = 0;
David S. Miller2518c7c2006-08-24 04:45:07 -0700365
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800366redo:
David S. Miller2518c7c2006-08-24 04:45:07 -0700367 hlist_for_each_entry_safe(pol, entry, tmp, list, bydst) {
368 unsigned int h;
369
370 h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
371 pol->family, nhashmask);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800372 if (!entry0) {
373 hlist_del(entry);
374 hlist_add_head(&pol->bydst, ndsttable+h);
375 h0 = h;
376 } else {
377 if (h != h0)
378 continue;
379 hlist_del(entry);
380 hlist_add_after(entry0, &pol->bydst);
381 }
382 entry0 = entry;
383 }
384 if (!hlist_empty(list)) {
385 entry0 = NULL;
386 goto redo;
David S. Miller2518c7c2006-08-24 04:45:07 -0700387 }
388}
389
390static void xfrm_idx_hash_transfer(struct hlist_head *list,
391 struct hlist_head *nidxtable,
392 unsigned int nhashmask)
393{
394 struct hlist_node *entry, *tmp;
395 struct xfrm_policy *pol;
396
397 hlist_for_each_entry_safe(pol, entry, tmp, list, byidx) {
398 unsigned int h;
399
400 h = __idx_hash(pol->index, nhashmask);
401 hlist_add_head(&pol->byidx, nidxtable+h);
402 }
403}
404
405static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
406{
407 return ((old_hmask + 1) << 1) - 1;
408}
409
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800410static void xfrm_bydst_resize(struct net *net, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700411{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800412 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700413 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
414 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800415 struct hlist_head *odst = net->xfrm.policy_bydst[dir].table;
David S. Miller44e36b42006-08-24 04:50:50 -0700416 struct hlist_head *ndst = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700417 int i;
418
419 if (!ndst)
420 return;
421
422 write_lock_bh(&xfrm_policy_lock);
423
424 for (i = hmask; i >= 0; i--)
425 xfrm_dst_hash_transfer(odst + i, ndst, nhashmask);
426
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800427 net->xfrm.policy_bydst[dir].table = ndst;
428 net->xfrm.policy_bydst[dir].hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700429
430 write_unlock_bh(&xfrm_policy_lock);
431
David S. Miller44e36b42006-08-24 04:50:50 -0700432 xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700433}
434
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800435static void xfrm_byidx_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700436{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800437 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700438 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
439 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800440 struct hlist_head *oidx = net->xfrm.policy_byidx;
David S. Miller44e36b42006-08-24 04:50:50 -0700441 struct hlist_head *nidx = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700442 int i;
443
444 if (!nidx)
445 return;
446
447 write_lock_bh(&xfrm_policy_lock);
448
449 for (i = hmask; i >= 0; i--)
450 xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
451
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800452 net->xfrm.policy_byidx = nidx;
453 net->xfrm.policy_idx_hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700454
455 write_unlock_bh(&xfrm_policy_lock);
456
David S. Miller44e36b42006-08-24 04:50:50 -0700457 xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700458}
459
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800460static inline int xfrm_bydst_should_resize(struct net *net, int dir, int *total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700461{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800462 unsigned int cnt = net->xfrm.policy_count[dir];
463 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700464
465 if (total)
466 *total += cnt;
467
468 if ((hmask + 1) < xfrm_policy_hashmax &&
469 cnt > hmask)
470 return 1;
471
472 return 0;
473}
474
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800475static inline int xfrm_byidx_should_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700476{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800477 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700478
479 if ((hmask + 1) < xfrm_policy_hashmax &&
480 total > hmask)
481 return 1;
482
483 return 0;
484}
485
Alexey Dobriyane0710412010-01-23 13:37:10 +0000486void xfrm_spd_getinfo(struct net *net, struct xfrmk_spdinfo *si)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700487{
488 read_lock_bh(&xfrm_policy_lock);
Alexey Dobriyane0710412010-01-23 13:37:10 +0000489 si->incnt = net->xfrm.policy_count[XFRM_POLICY_IN];
490 si->outcnt = net->xfrm.policy_count[XFRM_POLICY_OUT];
491 si->fwdcnt = net->xfrm.policy_count[XFRM_POLICY_FWD];
492 si->inscnt = net->xfrm.policy_count[XFRM_POLICY_IN+XFRM_POLICY_MAX];
493 si->outscnt = net->xfrm.policy_count[XFRM_POLICY_OUT+XFRM_POLICY_MAX];
494 si->fwdscnt = net->xfrm.policy_count[XFRM_POLICY_FWD+XFRM_POLICY_MAX];
495 si->spdhcnt = net->xfrm.policy_idx_hmask;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700496 si->spdhmcnt = xfrm_policy_hashmax;
497 read_unlock_bh(&xfrm_policy_lock);
498}
499EXPORT_SYMBOL(xfrm_spd_getinfo);
David S. Miller2518c7c2006-08-24 04:45:07 -0700500
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700501static DEFINE_MUTEX(hash_resize_mutex);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800502static void xfrm_hash_resize(struct work_struct *work)
David S. Miller2518c7c2006-08-24 04:45:07 -0700503{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800504 struct net *net = container_of(work, struct net, xfrm.policy_hash_work);
David S. Miller2518c7c2006-08-24 04:45:07 -0700505 int dir, total;
506
507 mutex_lock(&hash_resize_mutex);
508
509 total = 0;
510 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800511 if (xfrm_bydst_should_resize(net, dir, &total))
512 xfrm_bydst_resize(net, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700513 }
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800514 if (xfrm_byidx_should_resize(net, total))
515 xfrm_byidx_resize(net, total);
David S. Miller2518c7c2006-08-24 04:45:07 -0700516
517 mutex_unlock(&hash_resize_mutex);
518}
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520/* Generate new index... KAME seems to generate them ordered by cost
521 * of an absolute inpredictability of ordering of rules. This will not pass. */
Alexey Dobriyan11219942008-11-25 17:33:06 -0800522static u32 xfrm_gen_index(struct net *net, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 static u32 idx_generator;
525
526 for (;;) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700527 struct hlist_node *entry;
528 struct hlist_head *list;
529 struct xfrm_policy *p;
530 u32 idx;
531 int found;
532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 idx = (idx_generator | dir);
534 idx_generator += 8;
535 if (idx == 0)
536 idx = 8;
Alexey Dobriyan11219942008-11-25 17:33:06 -0800537 list = net->xfrm.policy_byidx + idx_hash(net, idx);
David S. Miller2518c7c2006-08-24 04:45:07 -0700538 found = 0;
539 hlist_for_each_entry(p, entry, list, byidx) {
540 if (p->index == idx) {
541 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 break;
David S. Miller2518c7c2006-08-24 04:45:07 -0700543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700545 if (!found)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 return idx;
547 }
548}
549
David S. Miller2518c7c2006-08-24 04:45:07 -0700550static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
551{
552 u32 *p1 = (u32 *) s1;
553 u32 *p2 = (u32 *) s2;
554 int len = sizeof(struct xfrm_selector) / sizeof(u32);
555 int i;
556
557 for (i = 0; i < len; i++) {
558 if (p1[i] != p2[i])
559 return 1;
560 }
561
562 return 0;
563}
564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
566{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800567 struct net *net = xp_net(policy);
David S. Miller2518c7c2006-08-24 04:45:07 -0700568 struct xfrm_policy *pol;
569 struct xfrm_policy *delpol;
570 struct hlist_head *chain;
Herbert Xua6c7ab52007-01-16 16:52:02 -0800571 struct hlist_node *entry, *newpos;
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000572 u32 mark = policy->mark.v & policy->mark.m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574 write_lock_bh(&xfrm_policy_lock);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800575 chain = policy_hash_bysel(net, &policy->selector, policy->family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700576 delpol = NULL;
577 newpos = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700578 hlist_for_each_entry(pol, entry, chain, bydst) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800579 if (pol->type == policy->type &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700580 !selector_cmp(&pol->selector, &policy->selector) &&
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000581 (mark & pol->mark.m) == pol->mark.v &&
Herbert Xua6c7ab52007-01-16 16:52:02 -0800582 xfrm_sec_ctx_match(pol->security, policy->security) &&
583 !WARN_ON(delpol)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 if (excl) {
585 write_unlock_bh(&xfrm_policy_lock);
586 return -EEXIST;
587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 delpol = pol;
589 if (policy->priority > pol->priority)
590 continue;
591 } else if (policy->priority >= pol->priority) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800592 newpos = &pol->bydst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 continue;
594 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 if (delpol)
596 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 }
598 if (newpos)
David S. Miller2518c7c2006-08-24 04:45:07 -0700599 hlist_add_after(newpos, &policy->bydst);
600 else
601 hlist_add_head(&policy->bydst, chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 xfrm_pol_hold(policy);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800603 net->xfrm.policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 atomic_inc(&flow_cache_genid);
Nicolas Dichtelee8372d2012-09-10 22:09:45 +0000605 rt_genid_bump(net);
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800606 if (delpol)
607 __xfrm_policy_unlink(delpol, dir);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800608 policy->index = delpol ? delpol->index : xfrm_gen_index(net, dir);
609 hlist_add_head(&policy->byidx, net->xfrm.policy_byidx+idx_hash(net, policy->index));
James Morris9d729f72007-03-04 16:12:44 -0800610 policy->curlft.add_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 policy->curlft.use_time = 0;
612 if (!mod_timer(&policy->timer, jiffies + HZ))
613 xfrm_pol_hold(policy);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800614 list_add(&policy->walk.all, &net->xfrm.policy_all);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 write_unlock_bh(&xfrm_policy_lock);
616
David S. Miller9b78a822005-12-22 07:39:48 -0800617 if (delpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 xfrm_policy_kill(delpol);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800619 else if (xfrm_bydst_should_resize(net, dir, NULL))
620 schedule_work(&net->xfrm.policy_hash_work);
David S. Miller9b78a822005-12-22 07:39:48 -0800621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 return 0;
623}
624EXPORT_SYMBOL(xfrm_policy_insert);
625
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +0000626struct xfrm_policy *xfrm_policy_bysel_ctx(struct net *net, u32 mark, u8 type,
627 int dir, struct xfrm_selector *sel,
Eric Parisef41aaa2007-03-07 15:37:58 -0800628 struct xfrm_sec_ctx *ctx, int delete,
629 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
David S. Miller2518c7c2006-08-24 04:45:07 -0700631 struct xfrm_policy *pol, *ret;
632 struct hlist_head *chain;
633 struct hlist_node *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Eric Parisef41aaa2007-03-07 15:37:58 -0800635 *err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 write_lock_bh(&xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800637 chain = policy_hash_bysel(net, sel, sel->family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700638 ret = NULL;
639 hlist_for_each_entry(pol, entry, chain, bydst) {
640 if (pol->type == type &&
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000641 (mark & pol->mark.m) == pol->mark.v &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700642 !selector_cmp(sel, &pol->selector) &&
643 xfrm_sec_ctx_match(ctx, pol->security)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700645 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -0700646 *err = security_xfrm_policy_delete(
647 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -0800648 if (*err) {
649 write_unlock_bh(&xfrm_policy_lock);
650 return pol;
651 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800652 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700653 }
654 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 break;
656 }
657 }
658 write_unlock_bh(&xfrm_policy_lock);
659
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000660 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -0700661 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -0700662 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663}
Trent Jaegerdf718372005-12-13 23:12:27 -0800664EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +0000666struct xfrm_policy *xfrm_policy_byid(struct net *net, u32 mark, u8 type,
667 int dir, u32 id, int delete, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
David S. Miller2518c7c2006-08-24 04:45:07 -0700669 struct xfrm_policy *pol, *ret;
670 struct hlist_head *chain;
671 struct hlist_node *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Herbert Xub5505c62007-05-14 02:15:47 -0700673 *err = -ENOENT;
674 if (xfrm_policy_id2dir(id) != dir)
675 return NULL;
676
Eric Parisef41aaa2007-03-07 15:37:58 -0800677 *err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 write_lock_bh(&xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800679 chain = net->xfrm.policy_byidx + idx_hash(net, id);
David S. Miller2518c7c2006-08-24 04:45:07 -0700680 ret = NULL;
681 hlist_for_each_entry(pol, entry, chain, byidx) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000682 if (pol->type == type && pol->index == id &&
683 (mark & pol->mark.m) == pol->mark.v) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700685 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -0700686 *err = security_xfrm_policy_delete(
687 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -0800688 if (*err) {
689 write_unlock_bh(&xfrm_policy_lock);
690 return pol;
691 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800692 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700693 }
694 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 break;
696 }
697 }
698 write_unlock_bh(&xfrm_policy_lock);
699
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000700 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -0700701 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -0700702 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703}
704EXPORT_SYMBOL(xfrm_policy_byid);
705
Joy Latten4aa2e622007-06-04 19:05:57 -0400706#ifdef CONFIG_SECURITY_NETWORK_XFRM
707static inline int
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800708xfrm_policy_flush_secctx_check(struct net *net, u8 type, struct xfrm_audit *audit_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
Joy Latten4aa2e622007-06-04 19:05:57 -0400710 int dir, err = 0;
711
712 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
713 struct xfrm_policy *pol;
714 struct hlist_node *entry;
715 int i;
716
717 hlist_for_each_entry(pol, entry,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800718 &net->xfrm.policy_inexact[dir], bydst) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400719 if (pol->type != type)
720 continue;
Paul Moore03e1ad72008-04-12 19:07:52 -0700721 err = security_xfrm_policy_delete(pol->security);
Joy Latten4aa2e622007-06-04 19:05:57 -0400722 if (err) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700723 xfrm_audit_policy_delete(pol, 0,
724 audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400725 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700726 audit_info->secid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400727 return err;
728 }
YOSHIFUJI Hideaki7dc12d62007-07-19 10:45:15 +0900729 }
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800730 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400731 hlist_for_each_entry(pol, entry,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800732 net->xfrm.policy_bydst[dir].table + i,
Joy Latten4aa2e622007-06-04 19:05:57 -0400733 bydst) {
734 if (pol->type != type)
735 continue;
Paul Moore03e1ad72008-04-12 19:07:52 -0700736 err = security_xfrm_policy_delete(
737 pol->security);
Joy Latten4aa2e622007-06-04 19:05:57 -0400738 if (err) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700739 xfrm_audit_policy_delete(pol, 0,
740 audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400741 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700742 audit_info->secid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400743 return err;
744 }
745 }
746 }
747 }
748 return err;
749}
750#else
751static inline int
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800752xfrm_policy_flush_secctx_check(struct net *net, u8 type, struct xfrm_audit *audit_info)
Joy Latten4aa2e622007-06-04 19:05:57 -0400753{
754 return 0;
755}
756#endif
757
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800758int xfrm_policy_flush(struct net *net, u8 type, struct xfrm_audit *audit_info)
Joy Latten4aa2e622007-06-04 19:05:57 -0400759{
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +0000760 int dir, err = 0, cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762 write_lock_bh(&xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400763
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800764 err = xfrm_policy_flush_secctx_check(net, type, audit_info);
Joy Latten4aa2e622007-06-04 19:05:57 -0400765 if (err)
766 goto out;
767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700769 struct xfrm_policy *pol;
770 struct hlist_node *entry;
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800771 int i;
David S. Miller2518c7c2006-08-24 04:45:07 -0700772
773 again1:
774 hlist_for_each_entry(pol, entry,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800775 &net->xfrm.policy_inexact[dir], bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700776 if (pol->type != type)
777 continue;
Timo Teräsea2dea92010-03-31 00:17:05 +0000778 __xfrm_policy_unlink(pol, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 write_unlock_bh(&xfrm_policy_lock);
Timo Teräsea2dea92010-03-31 00:17:05 +0000780 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Joy Lattenab5f5e82007-09-17 11:51:22 -0700782 xfrm_audit_policy_delete(pol, 1, audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400783 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700784 audit_info->secid);
Joy Latten161a09e2006-11-27 13:11:54 -0600785
David S. Miller2518c7c2006-08-24 04:45:07 -0700786 xfrm_policy_kill(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
788 write_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700789 goto again1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700791
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800792 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700793 again2:
794 hlist_for_each_entry(pol, entry,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800795 net->xfrm.policy_bydst[dir].table + i,
David S. Miller2518c7c2006-08-24 04:45:07 -0700796 bydst) {
797 if (pol->type != type)
798 continue;
Timo Teräsea2dea92010-03-31 00:17:05 +0000799 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700800 write_unlock_bh(&xfrm_policy_lock);
Timo Teräsea2dea92010-03-31 00:17:05 +0000801 cnt++;
David S. Miller2518c7c2006-08-24 04:45:07 -0700802
Joy Lattenab5f5e82007-09-17 11:51:22 -0700803 xfrm_audit_policy_delete(pol, 1,
804 audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400805 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700806 audit_info->secid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700807 xfrm_policy_kill(pol);
808
809 write_lock_bh(&xfrm_policy_lock);
810 goto again2;
811 }
812 }
813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 }
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +0000815 if (!cnt)
816 err = -ESRCH;
Joy Latten4aa2e622007-06-04 19:05:57 -0400817out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 write_unlock_bh(&xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400819 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820}
821EXPORT_SYMBOL(xfrm_policy_flush);
822
Alexey Dobriyancdcbca72008-11-25 17:34:49 -0800823int xfrm_policy_walk(struct net *net, struct xfrm_policy_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -0800824 int (*func)(struct xfrm_policy *, int, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 void *data)
826{
Herbert Xu12a169e2008-10-01 07:03:24 -0700827 struct xfrm_policy *pol;
828 struct xfrm_policy_walk_entry *x;
Timo Teras4c563f72008-02-28 21:31:08 -0800829 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Timo Teras4c563f72008-02-28 21:31:08 -0800831 if (walk->type >= XFRM_POLICY_TYPE_MAX &&
832 walk->type != XFRM_POLICY_TYPE_ANY)
833 return -EINVAL;
834
Herbert Xu12a169e2008-10-01 07:03:24 -0700835 if (list_empty(&walk->walk.all) && walk->seq != 0)
Timo Teras4c563f72008-02-28 21:31:08 -0800836 return 0;
837
Herbert Xu12a169e2008-10-01 07:03:24 -0700838 write_lock_bh(&xfrm_policy_lock);
839 if (list_empty(&walk->walk.all))
Alexey Dobriyancdcbca72008-11-25 17:34:49 -0800840 x = list_first_entry(&net->xfrm.policy_all, struct xfrm_policy_walk_entry, all);
Herbert Xu12a169e2008-10-01 07:03:24 -0700841 else
842 x = list_entry(&walk->walk.all, struct xfrm_policy_walk_entry, all);
Alexey Dobriyancdcbca72008-11-25 17:34:49 -0800843 list_for_each_entry_from(x, &net->xfrm.policy_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -0700844 if (x->dead)
Timo Teras4c563f72008-02-28 21:31:08 -0800845 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -0700846 pol = container_of(x, struct xfrm_policy, walk);
847 if (walk->type != XFRM_POLICY_TYPE_ANY &&
848 walk->type != pol->type)
849 continue;
850 error = func(pol, xfrm_policy_id2dir(pol->index),
851 walk->seq, data);
852 if (error) {
853 list_move_tail(&walk->walk.all, &x->all);
854 goto out;
Timo Teras4c563f72008-02-28 21:31:08 -0800855 }
Herbert Xu12a169e2008-10-01 07:03:24 -0700856 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 }
Herbert Xu12a169e2008-10-01 07:03:24 -0700858 if (walk->seq == 0) {
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -0800859 error = -ENOENT;
860 goto out;
861 }
Herbert Xu12a169e2008-10-01 07:03:24 -0700862 list_del_init(&walk->walk.all);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863out:
Herbert Xu12a169e2008-10-01 07:03:24 -0700864 write_unlock_bh(&xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 return error;
866}
867EXPORT_SYMBOL(xfrm_policy_walk);
868
Herbert Xu12a169e2008-10-01 07:03:24 -0700869void xfrm_policy_walk_init(struct xfrm_policy_walk *walk, u8 type)
870{
871 INIT_LIST_HEAD(&walk->walk.all);
872 walk->walk.dead = 1;
873 walk->type = type;
874 walk->seq = 0;
875}
876EXPORT_SYMBOL(xfrm_policy_walk_init);
877
878void xfrm_policy_walk_done(struct xfrm_policy_walk *walk)
879{
880 if (list_empty(&walk->walk.all))
881 return;
882
883 write_lock_bh(&xfrm_policy_lock);
884 list_del(&walk->walk.all);
885 write_unlock_bh(&xfrm_policy_lock);
886}
887EXPORT_SYMBOL(xfrm_policy_walk_done);
888
James Morris134b0fc2006-10-05 15:42:27 -0500889/*
890 * Find policy to apply to this flow.
891 *
892 * Returns 0 if policy found, else an -errno.
893 */
David S. Millerf299d552011-02-24 01:23:30 -0500894static int xfrm_policy_match(const struct xfrm_policy *pol,
895 const struct flowi *fl,
David S. Miller2518c7c2006-08-24 04:45:07 -0700896 u8 type, u16 family, int dir)
897{
David S. Millerf299d552011-02-24 01:23:30 -0500898 const struct xfrm_selector *sel = &pol->selector;
David S. Millerbc9b35a2012-05-15 15:04:57 -0400899 int ret = -ESRCH;
900 bool match;
David S. Miller2518c7c2006-08-24 04:45:07 -0700901
902 if (pol->family != family ||
David S. Miller1d28f422011-03-12 00:29:39 -0500903 (fl->flowi_mark & pol->mark.m) != pol->mark.v ||
David S. Miller2518c7c2006-08-24 04:45:07 -0700904 pol->type != type)
James Morris134b0fc2006-10-05 15:42:27 -0500905 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -0700906
907 match = xfrm_selector_match(sel, fl, family);
James Morris134b0fc2006-10-05 15:42:27 -0500908 if (match)
David S. Miller1d28f422011-03-12 00:29:39 -0500909 ret = security_xfrm_policy_lookup(pol->security, fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -0700910 dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700911
James Morris134b0fc2006-10-05 15:42:27 -0500912 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -0700913}
914
Alexey Dobriyan52479b62008-11-25 17:35:18 -0800915static struct xfrm_policy *xfrm_policy_lookup_bytype(struct net *net, u8 type,
David S. Miller062cdb42011-02-22 18:31:08 -0800916 const struct flowi *fl,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700917 u16 family, u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918{
James Morris134b0fc2006-10-05 15:42:27 -0500919 int err;
David S. Miller2518c7c2006-08-24 04:45:07 -0700920 struct xfrm_policy *pol, *ret;
David S. Miller0b597e72011-02-24 01:22:48 -0500921 const xfrm_address_t *daddr, *saddr;
David S. Miller2518c7c2006-08-24 04:45:07 -0700922 struct hlist_node *entry;
923 struct hlist_head *chain;
David S. Milleracba48e2006-08-25 15:46:46 -0700924 u32 priority = ~0U;
David S. Miller2518c7c2006-08-24 04:45:07 -0700925
926 daddr = xfrm_flowi_daddr(fl, family);
927 saddr = xfrm_flowi_saddr(fl, family);
928 if (unlikely(!daddr || !saddr))
929 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
931 read_lock_bh(&xfrm_policy_lock);
Alexey Dobriyan52479b62008-11-25 17:35:18 -0800932 chain = policy_hash_direct(net, daddr, saddr, family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700933 ret = NULL;
934 hlist_for_each_entry(pol, entry, chain, bydst) {
James Morris134b0fc2006-10-05 15:42:27 -0500935 err = xfrm_policy_match(pol, fl, type, family, dir);
936 if (err) {
937 if (err == -ESRCH)
938 continue;
939 else {
940 ret = ERR_PTR(err);
941 goto fail;
942 }
943 } else {
David S. Milleracba48e2006-08-25 15:46:46 -0700944 ret = pol;
945 priority = ret->priority;
946 break;
947 }
948 }
Alexey Dobriyan52479b62008-11-25 17:35:18 -0800949 chain = &net->xfrm.policy_inexact[dir];
David S. Milleracba48e2006-08-25 15:46:46 -0700950 hlist_for_each_entry(pol, entry, chain, bydst) {
James Morris134b0fc2006-10-05 15:42:27 -0500951 err = xfrm_policy_match(pol, fl, type, family, dir);
952 if (err) {
953 if (err == -ESRCH)
954 continue;
955 else {
956 ret = ERR_PTR(err);
957 goto fail;
958 }
959 } else if (pol->priority < priority) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700960 ret = pol;
961 break;
962 }
963 }
David S. Milleracba48e2006-08-25 15:46:46 -0700964 if (ret)
965 xfrm_pol_hold(ret);
James Morris134b0fc2006-10-05 15:42:27 -0500966fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 read_unlock_bh(&xfrm_policy_lock);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700968
David S. Miller2518c7c2006-08-24 04:45:07 -0700969 return ret;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700970}
971
Timo Teräs80c802f2010-04-07 00:30:05 +0000972static struct xfrm_policy *
David S. Miller73ff93c2011-02-22 18:33:42 -0800973__xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir)
Timo Teräs80c802f2010-04-07 00:30:05 +0000974{
975#ifdef CONFIG_XFRM_SUB_POLICY
976 struct xfrm_policy *pol;
977
978 pol = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_SUB, fl, family, dir);
979 if (pol != NULL)
980 return pol;
981#endif
982 return xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN, fl, family, dir);
983}
984
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000985static struct flow_cache_object *
David S. Millerdee9f4b2011-02-22 18:44:31 -0800986xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family,
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000987 u8 dir, struct flow_cache_object *old_obj, void *ctx)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700988{
989 struct xfrm_policy *pol;
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000990
991 if (old_obj)
992 xfrm_pol_put(container_of(old_obj, struct xfrm_policy, flo));
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700993
Timo Teräs80c802f2010-04-07 00:30:05 +0000994 pol = __xfrm_policy_lookup(net, fl, family, dir);
995 if (IS_ERR_OR_NULL(pol))
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000996 return ERR_CAST(pol);
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000997
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000998 /* Resolver returns two references:
999 * one for cache and one for caller of flow_cache_lookup() */
1000 xfrm_pol_hold(pol);
1001
1002 return &pol->flo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003}
1004
Trent Jaegerdf718372005-12-13 23:12:27 -08001005static inline int policy_to_flow_dir(int dir)
1006{
1007 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001008 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1009 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1010 return dir;
1011 switch (dir) {
1012 default:
1013 case XFRM_POLICY_IN:
1014 return FLOW_DIR_IN;
1015 case XFRM_POLICY_OUT:
1016 return FLOW_DIR_OUT;
1017 case XFRM_POLICY_FWD:
1018 return FLOW_DIR_FWD;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001019 }
Trent Jaegerdf718372005-12-13 23:12:27 -08001020}
1021
David S. Millerdee9f4b2011-02-22 18:44:31 -08001022static struct xfrm_policy *xfrm_sk_policy_lookup(struct sock *sk, int dir,
1023 const struct flowi *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
1025 struct xfrm_policy *pol;
1026
1027 read_lock_bh(&xfrm_policy_lock);
1028 if ((pol = sk->sk_policy[dir]) != NULL) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04001029 bool match = xfrm_selector_match(&pol->selector, fl,
1030 sk->sk_family);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001031 int err = 0;
Trent Jaegerdf718372005-12-13 23:12:27 -08001032
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001033 if (match) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001034 if ((sk->sk_mark & pol->mark.m) != pol->mark.v) {
1035 pol = NULL;
1036 goto out;
1037 }
Paul Moore03e1ad72008-04-12 19:07:52 -07001038 err = security_xfrm_policy_lookup(pol->security,
David S. Miller1d28f422011-03-12 00:29:39 -05001039 fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -07001040 policy_to_flow_dir(dir));
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001041 if (!err)
1042 xfrm_pol_hold(pol);
1043 else if (err == -ESRCH)
1044 pol = NULL;
1045 else
1046 pol = ERR_PTR(err);
1047 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 pol = NULL;
1049 }
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001050out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 read_unlock_bh(&xfrm_policy_lock);
1052 return pol;
1053}
1054
1055static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
1056{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001057 struct net *net = xp_net(pol);
Alexey Dobriyan11219942008-11-25 17:33:06 -08001058 struct hlist_head *chain = policy_hash_bysel(net, &pol->selector,
David S. Miller2518c7c2006-08-24 04:45:07 -07001059 pol->family, dir);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001060
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001061 list_add(&pol->walk.all, &net->xfrm.policy_all);
David S. Miller2518c7c2006-08-24 04:45:07 -07001062 hlist_add_head(&pol->bydst, chain);
Alexey Dobriyane92303f2008-11-25 17:32:41 -08001063 hlist_add_head(&pol->byidx, net->xfrm.policy_byidx+idx_hash(net, pol->index));
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001064 net->xfrm.policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -07001066
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001067 if (xfrm_bydst_should_resize(net, dir, NULL))
1068 schedule_work(&net->xfrm.policy_hash_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069}
1070
1071static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
1072 int dir)
1073{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001074 struct net *net = xp_net(pol);
1075
David S. Miller2518c7c2006-08-24 04:45:07 -07001076 if (hlist_unhashed(&pol->bydst))
1077 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
David S. Miller2518c7c2006-08-24 04:45:07 -07001079 hlist_del(&pol->bydst);
1080 hlist_del(&pol->byidx);
Herbert Xu12a169e2008-10-01 07:03:24 -07001081 list_del(&pol->walk.all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001082 net->xfrm.policy_count[dir]--;
David S. Miller2518c7c2006-08-24 04:45:07 -07001083
1084 return pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085}
1086
Herbert Xu4666faa2005-06-18 22:43:22 -07001087int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088{
1089 write_lock_bh(&xfrm_policy_lock);
1090 pol = __xfrm_policy_unlink(pol, dir);
1091 write_unlock_bh(&xfrm_policy_lock);
1092 if (pol) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 xfrm_policy_kill(pol);
Herbert Xu4666faa2005-06-18 22:43:22 -07001094 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 }
Herbert Xu4666faa2005-06-18 22:43:22 -07001096 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097}
David S. Millera70fcb02006-03-20 19:18:52 -08001098EXPORT_SYMBOL(xfrm_policy_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
1100int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
1101{
Alexey Dobriyan11219942008-11-25 17:33:06 -08001102 struct net *net = xp_net(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 struct xfrm_policy *old_pol;
1104
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001105#ifdef CONFIG_XFRM_SUB_POLICY
1106 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
1107 return -EINVAL;
1108#endif
1109
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 write_lock_bh(&xfrm_policy_lock);
1111 old_pol = sk->sk_policy[dir];
1112 sk->sk_policy[dir] = pol;
1113 if (pol) {
James Morris9d729f72007-03-04 16:12:44 -08001114 pol->curlft.add_time = get_seconds();
Alexey Dobriyan11219942008-11-25 17:33:06 -08001115 pol->index = xfrm_gen_index(net, XFRM_POLICY_MAX+dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 __xfrm_policy_link(pol, XFRM_POLICY_MAX+dir);
1117 }
1118 if (old_pol)
Timo Teräsea2dea92010-03-31 00:17:05 +00001119 /* Unlinking succeeds always. This is the only function
1120 * allowed to delete or replace socket policy.
1121 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 __xfrm_policy_unlink(old_pol, XFRM_POLICY_MAX+dir);
1123 write_unlock_bh(&xfrm_policy_lock);
1124
1125 if (old_pol) {
1126 xfrm_policy_kill(old_pol);
1127 }
1128 return 0;
1129}
1130
David S. Millerd3e40a92011-02-24 01:25:41 -05001131static struct xfrm_policy *clone_policy(const struct xfrm_policy *old, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132{
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -08001133 struct xfrm_policy *newp = xfrm_policy_alloc(xp_net(old), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
1135 if (newp) {
1136 newp->selector = old->selector;
Paul Moore03e1ad72008-04-12 19:07:52 -07001137 if (security_xfrm_policy_clone(old->security,
1138 &newp->security)) {
Trent Jaegerdf718372005-12-13 23:12:27 -08001139 kfree(newp);
1140 return NULL; /* ENOMEM */
1141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 newp->lft = old->lft;
1143 newp->curlft = old->curlft;
Jamal Hadi Salimfb977e22010-02-23 15:09:53 -08001144 newp->mark = old->mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 newp->action = old->action;
1146 newp->flags = old->flags;
1147 newp->xfrm_nr = old->xfrm_nr;
1148 newp->index = old->index;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001149 newp->type = old->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 memcpy(newp->xfrm_vec, old->xfrm_vec,
1151 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
1152 write_lock_bh(&xfrm_policy_lock);
1153 __xfrm_policy_link(newp, XFRM_POLICY_MAX+dir);
1154 write_unlock_bh(&xfrm_policy_lock);
1155 xfrm_pol_put(newp);
1156 }
1157 return newp;
1158}
1159
1160int __xfrm_sk_clone_policy(struct sock *sk)
1161{
1162 struct xfrm_policy *p0 = sk->sk_policy[0],
1163 *p1 = sk->sk_policy[1];
1164
1165 sk->sk_policy[0] = sk->sk_policy[1] = NULL;
1166 if (p0 && (sk->sk_policy[0] = clone_policy(p0, 0)) == NULL)
1167 return -ENOMEM;
1168 if (p1 && (sk->sk_policy[1] = clone_policy(p1, 1)) == NULL)
1169 return -ENOMEM;
1170 return 0;
1171}
1172
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001173static int
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001174xfrm_get_saddr(struct net *net, xfrm_address_t *local, xfrm_address_t *remote,
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001175 unsigned short family)
1176{
1177 int err;
1178 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1179
1180 if (unlikely(afinfo == NULL))
1181 return -EINVAL;
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001182 err = afinfo->get_saddr(net, local, remote);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001183 xfrm_policy_put_afinfo(afinfo);
1184 return err;
1185}
1186
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187/* Resolve list of templates for the flow, given policy. */
1188
1189static int
David S. Millera6c2e612011-02-22 18:35:39 -08001190xfrm_tmpl_resolve_one(struct xfrm_policy *policy, const struct flowi *fl,
1191 struct xfrm_state **xfrm, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192{
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001193 struct net *net = xp_net(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 int nx;
1195 int i, error;
1196 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
1197 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001198 xfrm_address_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
1200 for (nx=0, i = 0; i < policy->xfrm_nr; i++) {
1201 struct xfrm_state *x;
1202 xfrm_address_t *remote = daddr;
1203 xfrm_address_t *local = saddr;
1204 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
1205
Joakim Koskela48b8d782007-07-26 00:08:42 -07001206 if (tmpl->mode == XFRM_MODE_TUNNEL ||
1207 tmpl->mode == XFRM_MODE_BEET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 remote = &tmpl->id.daddr;
1209 local = &tmpl->saddr;
Thomas Egerer8444cf72010-09-20 11:11:38 -07001210 if (xfrm_addr_any(local, tmpl->encap_family)) {
1211 error = xfrm_get_saddr(net, &tmp, remote, tmpl->encap_family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001212 if (error)
1213 goto fail;
1214 local = &tmp;
1215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 }
1217
1218 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
1219
1220 if (x && x->km.state == XFRM_STATE_VALID) {
1221 xfrm[nx++] = x;
1222 daddr = remote;
1223 saddr = local;
1224 continue;
1225 }
1226 if (x) {
1227 error = (x->km.state == XFRM_STATE_ERROR ?
1228 -EINVAL : -EAGAIN);
1229 xfrm_state_put(x);
1230 }
fernando@oss.ntt.coa43222662008-10-23 04:27:19 +00001231 else if (error == -ESRCH)
1232 error = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
1234 if (!tmpl->optional)
1235 goto fail;
1236 }
1237 return nx;
1238
1239fail:
1240 for (nx--; nx>=0; nx--)
1241 xfrm_state_put(xfrm[nx]);
1242 return error;
1243}
1244
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001245static int
David S. Millera6c2e612011-02-22 18:35:39 -08001246xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, const struct flowi *fl,
1247 struct xfrm_state **xfrm, unsigned short family)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001248{
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001249 struct xfrm_state *tp[XFRM_MAX_DEPTH];
1250 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001251 int cnx = 0;
1252 int error;
1253 int ret;
1254 int i;
1255
1256 for (i = 0; i < npols; i++) {
1257 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
1258 error = -ENOBUFS;
1259 goto fail;
1260 }
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001261
1262 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001263 if (ret < 0) {
1264 error = ret;
1265 goto fail;
1266 } else
1267 cnx += ret;
1268 }
1269
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001270 /* found states are sorted for outbound processing */
1271 if (npols > 1)
1272 xfrm_state_sort(xfrm, tpp, cnx, family);
1273
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001274 return cnx;
1275
1276 fail:
1277 for (cnx--; cnx>=0; cnx--)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001278 xfrm_state_put(tpp[cnx]);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001279 return error;
1280
1281}
1282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283/* Check that the bundle accepts the flow and its components are
1284 * still valid.
1285 */
1286
David S. Miller05d84022011-02-22 17:47:10 -08001287static inline int xfrm_get_tos(const struct flowi *fl, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08001288{
1289 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1290 int tos;
1291
1292 if (!afinfo)
1293 return -EINVAL;
1294
1295 tos = afinfo->get_tos(fl);
1296
1297 xfrm_policy_put_afinfo(afinfo);
1298
1299 return tos;
1300}
1301
Timo Teräs80c802f2010-04-07 00:30:05 +00001302static struct flow_cache_object *xfrm_bundle_flo_get(struct flow_cache_object *flo)
1303{
1304 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1305 struct dst_entry *dst = &xdst->u.dst;
1306
1307 if (xdst->route == NULL) {
1308 /* Dummy bundle - if it has xfrms we were not
1309 * able to build bundle as template resolution failed.
1310 * It means we need to try again resolving. */
1311 if (xdst->num_xfrms > 0)
1312 return NULL;
1313 } else {
1314 /* Real bundle */
1315 if (stale_bundle(dst))
1316 return NULL;
1317 }
1318
1319 dst_hold(dst);
1320 return flo;
1321}
1322
1323static int xfrm_bundle_flo_check(struct flow_cache_object *flo)
1324{
1325 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1326 struct dst_entry *dst = &xdst->u.dst;
1327
1328 if (!xdst->route)
1329 return 0;
1330 if (stale_bundle(dst))
1331 return 0;
1332
1333 return 1;
1334}
1335
1336static void xfrm_bundle_flo_delete(struct flow_cache_object *flo)
1337{
1338 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1339 struct dst_entry *dst = &xdst->u.dst;
1340
1341 dst_free(dst);
1342}
1343
1344static const struct flow_cache_ops xfrm_bundle_fc_ops = {
1345 .get = xfrm_bundle_flo_get,
1346 .check = xfrm_bundle_flo_check,
1347 .delete = xfrm_bundle_flo_delete,
1348};
1349
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001350static inline struct xfrm_dst *xfrm_alloc_dst(struct net *net, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08001351{
1352 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001353 struct dst_ops *dst_ops;
Herbert Xu25ee3282007-12-11 09:32:34 -08001354 struct xfrm_dst *xdst;
1355
1356 if (!afinfo)
1357 return ERR_PTR(-EINVAL);
1358
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001359 switch (family) {
1360 case AF_INET:
1361 dst_ops = &net->xfrm.xfrm4_dst_ops;
1362 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001363#if IS_ENABLED(CONFIG_IPV6)
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001364 case AF_INET6:
1365 dst_ops = &net->xfrm.xfrm6_dst_ops;
1366 break;
1367#endif
1368 default:
1369 BUG();
1370 }
David S. Millerf5b0a872012-07-19 12:31:33 -07001371 xdst = dst_alloc(dst_ops, NULL, 0, DST_OBSOLETE_NONE, 0);
Herbert Xu25ee3282007-12-11 09:32:34 -08001372
Madalin Bucurd4cae562011-09-26 07:04:36 +00001373 if (likely(xdst)) {
Steffen Klassert141e3692012-07-05 23:39:34 +00001374 struct dst_entry *dst = &xdst->u.dst;
1375
1376 memset(dst + 1, 0, sizeof(*xdst) - sizeof(*dst));
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08001377 xdst->flo.ops = &xfrm_bundle_fc_ops;
Patrick McHardy9d7b0fc2012-08-20 02:56:56 -07001378 if (afinfo->init_dst)
1379 afinfo->init_dst(net, xdst);
Madalin Bucurd4cae562011-09-26 07:04:36 +00001380 } else
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08001381 xdst = ERR_PTR(-ENOBUFS);
Timo Teräs80c802f2010-04-07 00:30:05 +00001382
Madalin Bucurd4cae562011-09-26 07:04:36 +00001383 xfrm_policy_put_afinfo(afinfo);
1384
Herbert Xu25ee3282007-12-11 09:32:34 -08001385 return xdst;
1386}
1387
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001388static inline int xfrm_init_path(struct xfrm_dst *path, struct dst_entry *dst,
1389 int nfheader_len)
1390{
1391 struct xfrm_policy_afinfo *afinfo =
1392 xfrm_policy_get_afinfo(dst->ops->family);
1393 int err;
1394
1395 if (!afinfo)
1396 return -EINVAL;
1397
1398 err = afinfo->init_path(path, dst, nfheader_len);
1399
1400 xfrm_policy_put_afinfo(afinfo);
1401
1402 return err;
1403}
1404
Herbert Xu87c1e122010-03-02 02:51:56 +00001405static inline int xfrm_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
David S. Miller0c7b3ee2011-02-22 17:48:57 -08001406 const struct flowi *fl)
Herbert Xu25ee3282007-12-11 09:32:34 -08001407{
1408 struct xfrm_policy_afinfo *afinfo =
1409 xfrm_policy_get_afinfo(xdst->u.dst.ops->family);
1410 int err;
1411
1412 if (!afinfo)
1413 return -EINVAL;
1414
Herbert Xu87c1e122010-03-02 02:51:56 +00001415 err = afinfo->fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001416
1417 xfrm_policy_put_afinfo(afinfo);
1418
1419 return err;
1420}
1421
Timo Teräs80c802f2010-04-07 00:30:05 +00001422
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423/* Allocate chain of dst_entry's, attach known xfrm's, calculate
1424 * all the metrics... Shortly, bundle a bundle.
1425 */
1426
Herbert Xu25ee3282007-12-11 09:32:34 -08001427static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy,
1428 struct xfrm_state **xfrm, int nx,
David S. Miller98313ad2011-02-22 18:36:50 -08001429 const struct flowi *fl,
Herbert Xu25ee3282007-12-11 09:32:34 -08001430 struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431{
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001432 struct net *net = xp_net(policy);
Herbert Xu25ee3282007-12-11 09:32:34 -08001433 unsigned long now = jiffies;
1434 struct net_device *dev;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001435 struct xfrm_mode *inner_mode;
Herbert Xu25ee3282007-12-11 09:32:34 -08001436 struct dst_entry *dst_prev = NULL;
1437 struct dst_entry *dst0 = NULL;
1438 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 int err;
Herbert Xu25ee3282007-12-11 09:32:34 -08001440 int header_len = 0;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001441 int nfheader_len = 0;
Herbert Xu25ee3282007-12-11 09:32:34 -08001442 int trailer_len = 0;
1443 int tos;
1444 int family = policy->selector.family;
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +09001445 xfrm_address_t saddr, daddr;
1446
1447 xfrm_flowi_addr_get(fl, &saddr, &daddr, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001448
1449 tos = xfrm_get_tos(fl, family);
1450 err = tos;
1451 if (tos < 0)
1452 goto put_states;
1453
1454 dst_hold(dst);
1455
1456 for (; i < nx; i++) {
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001457 struct xfrm_dst *xdst = xfrm_alloc_dst(net, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001458 struct dst_entry *dst1 = &xdst->u.dst;
1459
1460 err = PTR_ERR(xdst);
1461 if (IS_ERR(xdst)) {
1462 dst_release(dst);
1463 goto put_states;
1464 }
1465
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001466 if (xfrm[i]->sel.family == AF_UNSPEC) {
1467 inner_mode = xfrm_ip2inner_mode(xfrm[i],
1468 xfrm_af2proto(family));
1469 if (!inner_mode) {
1470 err = -EAFNOSUPPORT;
1471 dst_release(dst);
1472 goto put_states;
1473 }
1474 } else
1475 inner_mode = xfrm[i]->inner_mode;
1476
Herbert Xu25ee3282007-12-11 09:32:34 -08001477 if (!dst_prev)
1478 dst0 = dst1;
1479 else {
1480 dst_prev->child = dst_clone(dst1);
1481 dst1->flags |= DST_NOHASH;
1482 }
1483
1484 xdst->route = dst;
David S. Millerdefb3512010-12-08 21:16:57 -08001485 dst_copy_metrics(dst1, dst);
Herbert Xu25ee3282007-12-11 09:32:34 -08001486
1487 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
1488 family = xfrm[i]->props.family;
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +09001489 dst = xfrm_dst_lookup(xfrm[i], tos, &saddr, &daddr,
1490 family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001491 err = PTR_ERR(dst);
1492 if (IS_ERR(dst))
1493 goto put_states;
1494 } else
1495 dst_hold(dst);
1496
1497 dst1->xfrm = xfrm[i];
Timo Teräs80c802f2010-04-07 00:30:05 +00001498 xdst->xfrm_genid = xfrm[i]->genid;
Herbert Xu25ee3282007-12-11 09:32:34 -08001499
David S. Millerf5b0a872012-07-19 12:31:33 -07001500 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
Herbert Xu25ee3282007-12-11 09:32:34 -08001501 dst1->flags |= DST_HOST;
1502 dst1->lastuse = now;
1503
1504 dst1->input = dst_discard;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001505 dst1->output = inner_mode->afinfo->output;
Herbert Xu25ee3282007-12-11 09:32:34 -08001506
1507 dst1->next = dst_prev;
1508 dst_prev = dst1;
1509
1510 header_len += xfrm[i]->props.header_len;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001511 if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT)
1512 nfheader_len += xfrm[i]->props.header_len;
Herbert Xu25ee3282007-12-11 09:32:34 -08001513 trailer_len += xfrm[i]->props.trailer_len;
1514 }
1515
1516 dst_prev->child = dst;
1517 dst0->path = dst;
1518
1519 err = -ENODEV;
1520 dev = dst->dev;
1521 if (!dev)
1522 goto free_dst;
1523
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001524 xfrm_init_path((struct xfrm_dst *)dst0, dst, nfheader_len);
Herbert Xu25ee3282007-12-11 09:32:34 -08001525 xfrm_init_pmtu(dst_prev);
1526
1527 for (dst_prev = dst0; dst_prev != dst; dst_prev = dst_prev->child) {
1528 struct xfrm_dst *xdst = (struct xfrm_dst *)dst_prev;
1529
Herbert Xu87c1e122010-03-02 02:51:56 +00001530 err = xfrm_fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001531 if (err)
1532 goto free_dst;
1533
1534 dst_prev->header_len = header_len;
1535 dst_prev->trailer_len = trailer_len;
1536 header_len -= xdst->u.dst.xfrm->props.header_len;
1537 trailer_len -= xdst->u.dst.xfrm->props.trailer_len;
1538 }
1539
1540out:
1541 return dst0;
1542
1543put_states:
1544 for (; i < nx; i++)
1545 xfrm_state_put(xfrm[i]);
1546free_dst:
1547 if (dst0)
1548 dst_free(dst0);
1549 dst0 = ERR_PTR(err);
1550 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551}
1552
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001553static int inline
David S. Miller3f0e18f2011-02-22 18:38:14 -08001554xfrm_dst_alloc_copy(void **target, const void *src, int size)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001555{
1556 if (!*target) {
1557 *target = kmalloc(size, GFP_ATOMIC);
1558 if (!*target)
1559 return -ENOMEM;
1560 }
1561 memcpy(*target, src, size);
1562 return 0;
1563}
1564
1565static int inline
David S. Miller1786b382011-02-24 01:32:54 -05001566xfrm_dst_update_parent(struct dst_entry *dst, const struct xfrm_selector *sel)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001567{
1568#ifdef CONFIG_XFRM_SUB_POLICY
1569 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1570 return xfrm_dst_alloc_copy((void **)&(xdst->partner),
1571 sel, sizeof(*sel));
1572#else
1573 return 0;
1574#endif
1575}
1576
1577static int inline
David S. Miller3f0e18f2011-02-22 18:38:14 -08001578xfrm_dst_update_origin(struct dst_entry *dst, const struct flowi *fl)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001579{
1580#ifdef CONFIG_XFRM_SUB_POLICY
1581 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1582 return xfrm_dst_alloc_copy((void **)&(xdst->origin), fl, sizeof(*fl));
1583#else
1584 return 0;
1585#endif
1586}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
David S. Miller73ff93c2011-02-22 18:33:42 -08001588static int xfrm_expand_policies(const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001589 struct xfrm_policy **pols,
1590 int *num_pols, int *num_xfrms)
1591{
1592 int i;
1593
1594 if (*num_pols == 0 || !pols[0]) {
1595 *num_pols = 0;
1596 *num_xfrms = 0;
1597 return 0;
1598 }
1599 if (IS_ERR(pols[0]))
1600 return PTR_ERR(pols[0]);
1601
1602 *num_xfrms = pols[0]->xfrm_nr;
1603
1604#ifdef CONFIG_XFRM_SUB_POLICY
1605 if (pols[0] && pols[0]->action == XFRM_POLICY_ALLOW &&
1606 pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1607 pols[1] = xfrm_policy_lookup_bytype(xp_net(pols[0]),
1608 XFRM_POLICY_TYPE_MAIN,
1609 fl, family,
1610 XFRM_POLICY_OUT);
1611 if (pols[1]) {
1612 if (IS_ERR(pols[1])) {
1613 xfrm_pols_put(pols, *num_pols);
1614 return PTR_ERR(pols[1]);
1615 }
1616 (*num_pols) ++;
1617 (*num_xfrms) += pols[1]->xfrm_nr;
1618 }
1619 }
1620#endif
1621 for (i = 0; i < *num_pols; i++) {
1622 if (pols[i]->action != XFRM_POLICY_ALLOW) {
1623 *num_xfrms = -1;
1624 break;
1625 }
1626 }
1627
1628 return 0;
1629
1630}
1631
1632static struct xfrm_dst *
1633xfrm_resolve_and_create_bundle(struct xfrm_policy **pols, int num_pols,
David S. Miller4ca2e682011-02-22 18:38:51 -08001634 const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001635 struct dst_entry *dst_orig)
1636{
1637 struct net *net = xp_net(pols[0]);
1638 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
1639 struct dst_entry *dst;
1640 struct xfrm_dst *xdst;
1641 int err;
1642
1643 /* Try to instantiate a bundle */
1644 err = xfrm_tmpl_resolve(pols, num_pols, fl, xfrm, family);
Timo Teräsd809ec82010-07-12 21:29:42 +00001645 if (err <= 0) {
1646 if (err != 0 && err != -EAGAIN)
Timo Teräs80c802f2010-04-07 00:30:05 +00001647 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
1648 return ERR_PTR(err);
1649 }
1650
1651 dst = xfrm_bundle_create(pols[0], xfrm, err, fl, dst_orig);
1652 if (IS_ERR(dst)) {
1653 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLEGENERROR);
1654 return ERR_CAST(dst);
1655 }
1656
1657 xdst = (struct xfrm_dst *)dst;
1658 xdst->num_xfrms = err;
1659 if (num_pols > 1)
1660 err = xfrm_dst_update_parent(dst, &pols[1]->selector);
1661 else
1662 err = xfrm_dst_update_origin(dst, fl);
1663 if (unlikely(err)) {
1664 dst_free(dst);
1665 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLECHECKERROR);
1666 return ERR_PTR(err);
1667 }
1668
1669 xdst->num_pols = num_pols;
1670 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy*) * num_pols);
1671 xdst->policy_genid = atomic_read(&pols[0]->genid);
1672
1673 return xdst;
1674}
1675
1676static struct flow_cache_object *
David S. Millerdee9f4b2011-02-22 18:44:31 -08001677xfrm_bundle_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir,
Timo Teräs80c802f2010-04-07 00:30:05 +00001678 struct flow_cache_object *oldflo, void *ctx)
1679{
1680 struct dst_entry *dst_orig = (struct dst_entry *)ctx;
1681 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
1682 struct xfrm_dst *xdst, *new_xdst;
1683 int num_pols = 0, num_xfrms = 0, i, err, pol_dead;
1684
1685 /* Check if the policies from old bundle are usable */
1686 xdst = NULL;
1687 if (oldflo) {
1688 xdst = container_of(oldflo, struct xfrm_dst, flo);
1689 num_pols = xdst->num_pols;
1690 num_xfrms = xdst->num_xfrms;
1691 pol_dead = 0;
1692 for (i = 0; i < num_pols; i++) {
1693 pols[i] = xdst->pols[i];
1694 pol_dead |= pols[i]->walk.dead;
1695 }
1696 if (pol_dead) {
1697 dst_free(&xdst->u.dst);
1698 xdst = NULL;
1699 num_pols = 0;
1700 num_xfrms = 0;
1701 oldflo = NULL;
1702 }
1703 }
1704
1705 /* Resolve policies to use if we couldn't get them from
1706 * previous cache entry */
1707 if (xdst == NULL) {
1708 num_pols = 1;
1709 pols[0] = __xfrm_policy_lookup(net, fl, family, dir);
1710 err = xfrm_expand_policies(fl, family, pols,
1711 &num_pols, &num_xfrms);
1712 if (err < 0)
1713 goto inc_error;
1714 if (num_pols == 0)
1715 return NULL;
1716 if (num_xfrms <= 0)
1717 goto make_dummy_bundle;
1718 }
1719
1720 new_xdst = xfrm_resolve_and_create_bundle(pols, num_pols, fl, family, dst_orig);
1721 if (IS_ERR(new_xdst)) {
1722 err = PTR_ERR(new_xdst);
1723 if (err != -EAGAIN)
1724 goto error;
1725 if (oldflo == NULL)
1726 goto make_dummy_bundle;
1727 dst_hold(&xdst->u.dst);
1728 return oldflo;
Timo Teräsd809ec82010-07-12 21:29:42 +00001729 } else if (new_xdst == NULL) {
1730 num_xfrms = 0;
1731 if (oldflo == NULL)
1732 goto make_dummy_bundle;
1733 xdst->num_xfrms = 0;
1734 dst_hold(&xdst->u.dst);
1735 return oldflo;
Timo Teräs80c802f2010-04-07 00:30:05 +00001736 }
1737
1738 /* Kill the previous bundle */
1739 if (xdst) {
1740 /* The policies were stolen for newly generated bundle */
1741 xdst->num_pols = 0;
1742 dst_free(&xdst->u.dst);
1743 }
1744
1745 /* Flow cache does not have reference, it dst_free()'s,
1746 * but we do need to return one reference for original caller */
1747 dst_hold(&new_xdst->u.dst);
1748 return &new_xdst->flo;
1749
1750make_dummy_bundle:
1751 /* We found policies, but there's no bundles to instantiate:
1752 * either because the policy blocks, has no transformations or
1753 * we could not build template (no xfrm_states).*/
1754 xdst = xfrm_alloc_dst(net, family);
1755 if (IS_ERR(xdst)) {
1756 xfrm_pols_put(pols, num_pols);
1757 return ERR_CAST(xdst);
1758 }
1759 xdst->num_pols = num_pols;
1760 xdst->num_xfrms = num_xfrms;
1761 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy*) * num_pols);
1762
1763 dst_hold(&xdst->u.dst);
1764 return &xdst->flo;
1765
1766inc_error:
1767 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
1768error:
1769 if (xdst != NULL)
1770 dst_free(&xdst->u.dst);
1771 else
1772 xfrm_pols_put(pols, num_pols);
1773 return ERR_PTR(err);
1774}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
David S. Miller2774c132011-03-01 14:59:04 -08001776static struct dst_entry *make_blackhole(struct net *net, u16 family,
1777 struct dst_entry *dst_orig)
1778{
1779 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1780 struct dst_entry *ret;
1781
1782 if (!afinfo) {
1783 dst_release(dst_orig);
Li RongQing433a1952012-09-17 22:40:10 +00001784 return ERR_PTR(-EINVAL);
David S. Miller2774c132011-03-01 14:59:04 -08001785 } else {
1786 ret = afinfo->blackhole_route(net, dst_orig);
1787 }
1788 xfrm_policy_put_afinfo(afinfo);
1789
1790 return ret;
1791}
1792
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793/* Main function: finds/creates a bundle for given flow.
1794 *
1795 * At the moment we eat a raw IP route. Mostly to speed up lookups
1796 * on interfaces with disabled IPsec.
1797 */
David S. Miller452edd52011-03-02 13:27:41 -08001798struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
1799 const struct flowi *fl,
1800 struct sock *sk, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801{
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001802 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
Timo Teräs80c802f2010-04-07 00:30:05 +00001803 struct flow_cache_object *flo;
1804 struct xfrm_dst *xdst;
David S. Miller452edd52011-03-02 13:27:41 -08001805 struct dst_entry *dst, *route;
Timo Teräs80c802f2010-04-07 00:30:05 +00001806 u16 family = dst_orig->ops->family;
Trent Jaegerdf718372005-12-13 23:12:27 -08001807 u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
Changli Gao4b021622010-04-27 21:20:22 +00001808 int i, err, num_pols, num_xfrms = 0, drop_pols = 0;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001809
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810restart:
Timo Teräs80c802f2010-04-07 00:30:05 +00001811 dst = NULL;
1812 xdst = NULL;
1813 route = NULL;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001814
Thomas Graff7944fb2007-08-25 13:46:55 -07001815 if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
Timo Teräs80c802f2010-04-07 00:30:05 +00001816 num_pols = 1;
1817 pols[0] = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
1818 err = xfrm_expand_policies(fl, family, pols,
1819 &num_pols, &num_xfrms);
1820 if (err < 0)
Herbert Xu75b8c132007-12-11 04:38:08 -08001821 goto dropdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00001822
1823 if (num_pols) {
1824 if (num_xfrms <= 0) {
1825 drop_pols = num_pols;
1826 goto no_transform;
1827 }
1828
1829 xdst = xfrm_resolve_and_create_bundle(
1830 pols, num_pols, fl,
1831 family, dst_orig);
1832 if (IS_ERR(xdst)) {
1833 xfrm_pols_put(pols, num_pols);
1834 err = PTR_ERR(xdst);
1835 goto dropdst;
Timo Teräsd809ec82010-07-12 21:29:42 +00001836 } else if (xdst == NULL) {
1837 num_xfrms = 0;
1838 drop_pols = num_pols;
1839 goto no_transform;
Timo Teräs80c802f2010-04-07 00:30:05 +00001840 }
1841
Steffen Klassertfbd50602011-03-15 21:12:49 +00001842 dst_hold(&xdst->u.dst);
1843
Timo Teräs80c802f2010-04-07 00:30:05 +00001844 spin_lock_bh(&xfrm_policy_sk_bundle_lock);
1845 xdst->u.dst.next = xfrm_policy_sk_bundles;
1846 xfrm_policy_sk_bundles = &xdst->u.dst;
1847 spin_unlock_bh(&xfrm_policy_sk_bundle_lock);
1848
1849 route = xdst->route;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001850 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
Timo Teräs80c802f2010-04-07 00:30:05 +00001853 if (xdst == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 /* To accelerate a bit... */
David S. Miller2518c7c2006-08-24 04:45:07 -07001855 if ((dst_orig->flags & DST_NOXFRM) ||
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001856 !net->xfrm.policy_count[XFRM_POLICY_OUT])
Herbert Xu8b7817f2007-12-12 10:44:43 -08001857 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
Timo Teräs80c802f2010-04-07 00:30:05 +00001859 flo = flow_cache_lookup(net, fl, family, dir,
1860 xfrm_bundle_lookup, dst_orig);
1861 if (flo == NULL)
1862 goto nopol;
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001863 if (IS_ERR(flo)) {
Timo Teräs80c802f2010-04-07 00:30:05 +00001864 err = PTR_ERR(flo);
Herbert Xu75b8c132007-12-11 04:38:08 -08001865 goto dropdst;
Masahide NAKAMURAd66e37a2008-01-07 21:46:15 -08001866 }
Timo Teräs80c802f2010-04-07 00:30:05 +00001867 xdst = container_of(flo, struct xfrm_dst, flo);
1868
1869 num_pols = xdst->num_pols;
1870 num_xfrms = xdst->num_xfrms;
1871 memcpy(pols, xdst->pols, sizeof(struct xfrm_policy*) * num_pols);
1872 route = xdst->route;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 }
1874
Timo Teräs80c802f2010-04-07 00:30:05 +00001875 dst = &xdst->u.dst;
1876 if (route == NULL && num_xfrms > 0) {
1877 /* The only case when xfrm_bundle_lookup() returns a
1878 * bundle with null route, is when the template could
1879 * not be resolved. It means policies are there, but
1880 * bundle could not be created, since we don't yet
1881 * have the xfrm_state's. We need to wait for KM to
1882 * negotiate new SA's or bail out with error.*/
1883 if (net->xfrm.sysctl_larval_drop) {
1884 /* EREMOTE tells the caller to generate
1885 * a one-shot blackhole route. */
1886 dst_release(dst);
Timo Terasa1aa3482010-05-15 23:49:26 -07001887 xfrm_pols_put(pols, drop_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00001888 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
David S. Miller2774c132011-03-01 14:59:04 -08001889
David S. Miller452edd52011-03-02 13:27:41 -08001890 return make_blackhole(net, family, dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00001891 }
David S. Miller1d28f422011-03-12 00:29:39 -05001892 if (fl->flowi_flags & FLOWI_FLAG_CAN_SLEEP) {
Timo Teräs80c802f2010-04-07 00:30:05 +00001893 DECLARE_WAITQUEUE(wait, current);
1894
1895 add_wait_queue(&net->xfrm.km_waitq, &wait);
1896 set_current_state(TASK_INTERRUPTIBLE);
1897 schedule();
1898 set_current_state(TASK_RUNNING);
1899 remove_wait_queue(&net->xfrm.km_waitq, &wait);
1900
1901 if (!signal_pending(current)) {
1902 dst_release(dst);
1903 goto restart;
1904 }
1905
1906 err = -ERESTART;
1907 } else
1908 err = -EAGAIN;
1909
1910 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
1911 goto error;
1912 }
1913
1914no_transform:
1915 if (num_pols == 0)
Herbert Xu8b7817f2007-12-12 10:44:43 -08001916 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
Timo Teräs80c802f2010-04-07 00:30:05 +00001918 if ((flags & XFRM_LOOKUP_ICMP) &&
1919 !(pols[0]->flags & XFRM_POLICY_ICMP)) {
1920 err = -ENOENT;
Herbert Xu8b7817f2007-12-12 10:44:43 -08001921 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00001922 }
Herbert Xu8b7817f2007-12-12 10:44:43 -08001923
Timo Teräs80c802f2010-04-07 00:30:05 +00001924 for (i = 0; i < num_pols; i++)
1925 pols[i]->curlft.use_time = get_seconds();
Herbert Xu8b7817f2007-12-12 10:44:43 -08001926
Timo Teräs80c802f2010-04-07 00:30:05 +00001927 if (num_xfrms < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 /* Prohibit the flow */
Alexey Dobriyan59c99402008-11-25 17:59:52 -08001929 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLBLOCK);
Patrick McHardye104411b2005-09-08 15:11:55 -07001930 err = -EPERM;
1931 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00001932 } else if (num_xfrms > 0) {
1933 /* Flow transformed */
Timo Teräs80c802f2010-04-07 00:30:05 +00001934 dst_release(dst_orig);
1935 } else {
1936 /* Flow passes untransformed */
1937 dst_release(dst);
David S. Miller452edd52011-03-02 13:27:41 -08001938 dst = dst_orig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 }
Timo Teräs80c802f2010-04-07 00:30:05 +00001940ok:
1941 xfrm_pols_put(pols, drop_pols);
Gao feng0c183372012-05-26 01:30:53 +00001942 if (dst && dst->xfrm &&
1943 dst->xfrm->props.mode == XFRM_MODE_TUNNEL)
1944 dst->flags |= DST_XFRM_TUNNEL;
David S. Miller452edd52011-03-02 13:27:41 -08001945 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
Timo Teräs80c802f2010-04-07 00:30:05 +00001947nopol:
David S. Miller452edd52011-03-02 13:27:41 -08001948 if (!(flags & XFRM_LOOKUP_ICMP)) {
1949 dst = dst_orig;
Timo Teräs80c802f2010-04-07 00:30:05 +00001950 goto ok;
David S. Miller452edd52011-03-02 13:27:41 -08001951 }
Timo Teräs80c802f2010-04-07 00:30:05 +00001952 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953error:
Timo Teräs80c802f2010-04-07 00:30:05 +00001954 dst_release(dst);
Herbert Xu75b8c132007-12-11 04:38:08 -08001955dropdst:
1956 dst_release(dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00001957 xfrm_pols_put(pols, drop_pols);
David S. Miller452edd52011-03-02 13:27:41 -08001958 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959}
1960EXPORT_SYMBOL(xfrm_lookup);
1961
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001962static inline int
David S. Miller8f029de2011-02-22 17:59:59 -08001963xfrm_secpath_reject(int idx, struct sk_buff *skb, const struct flowi *fl)
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001964{
1965 struct xfrm_state *x;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001966
1967 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
1968 return 0;
1969 x = skb->sp->xvec[idx];
1970 if (!x->type->reject)
1971 return 0;
Herbert Xu1ecafed2007-10-09 13:24:07 -07001972 return x->type->reject(x, skb, fl);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001973}
1974
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975/* When skb is transformed back to its "native" form, we have to
1976 * check policy restrictions. At the moment we make this in maximally
1977 * stupid way. Shame on me. :-) Of course, connected sockets must
1978 * have policy cached at them.
1979 */
1980
1981static inline int
David S. Miller7db454b2011-02-24 01:43:01 -05001982xfrm_state_ok(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 unsigned short family)
1984{
1985 if (xfrm_state_kern(x))
Kazunori MIYAZAWA928ba412007-02-13 12:57:16 -08001986 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 return x->id.proto == tmpl->id.proto &&
1988 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
1989 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
1990 x->props.mode == tmpl->mode &&
Herbert Xuc5d18e92008-04-22 00:46:42 -07001991 (tmpl->allalgs || (tmpl->aalgos & (1<<x->props.aalgo)) ||
Masahide NAKAMURAf3bd4842006-08-23 18:00:48 -07001992 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07001993 !(x->props.mode != XFRM_MODE_TRANSPORT &&
1994 xfrm_state_addr_cmp(tmpl, x, family));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995}
1996
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001997/*
1998 * 0 or more than 0 is returned when validation is succeeded (either bypass
1999 * because of optional transport mode, or next index of the mathced secpath
2000 * state with the template.
2001 * -1 is returned when no matching template is found.
2002 * Otherwise "-2 - errored_index" is returned.
2003 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004static inline int
David S. Miller22cccb72011-02-24 01:43:33 -05002005xfrm_policy_ok(const struct xfrm_tmpl *tmpl, const struct sec_path *sp, int start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 unsigned short family)
2007{
2008 int idx = start;
2009
2010 if (tmpl->optional) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002011 if (tmpl->mode == XFRM_MODE_TRANSPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 return start;
2013 } else
2014 start = -1;
2015 for (; idx < sp->len; idx++) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002016 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 return ++idx;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002018 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
2019 if (start == -1)
2020 start = -2-idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 break;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002022 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 }
2024 return start;
2025}
2026
Herbert Xud5422ef2007-12-12 10:44:16 -08002027int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
2028 unsigned int family, int reverse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029{
2030 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002031 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032
2033 if (unlikely(afinfo == NULL))
2034 return -EAFNOSUPPORT;
2035
Herbert Xud5422ef2007-12-12 10:44:16 -08002036 afinfo->decode_session(skb, fl, reverse);
David S. Miller1d28f422011-03-12 00:29:39 -05002037 err = security_xfrm_decode_session(skb, &fl->flowi_secid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 xfrm_policy_put_afinfo(afinfo);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002039 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040}
Herbert Xud5422ef2007-12-12 10:44:16 -08002041EXPORT_SYMBOL(__xfrm_decode_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
David S. Miller9a7386e2011-02-24 01:44:12 -05002043static inline int secpath_has_nontransport(const struct sec_path *sp, int k, int *idxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044{
2045 for (; k < sp->len; k++) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002046 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002047 *idxp = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 return 1;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002049 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 }
2051
2052 return 0;
2053}
2054
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002055int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 unsigned short family)
2057{
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002058 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 struct xfrm_policy *pol;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002060 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2061 int npols = 0;
2062 int xfrm_nr;
2063 int pi;
Herbert Xud5422ef2007-12-12 10:44:16 -08002064 int reverse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 struct flowi fl;
Herbert Xud5422ef2007-12-12 10:44:16 -08002066 u8 fl_dir;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002067 int xerr_idx = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
Herbert Xud5422ef2007-12-12 10:44:16 -08002069 reverse = dir & ~XFRM_POLICY_MASK;
2070 dir &= XFRM_POLICY_MASK;
2071 fl_dir = policy_to_flow_dir(dir);
2072
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002073 if (__xfrm_decode_session(skb, &fl, family, reverse) < 0) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002074 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002076 }
2077
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -08002078 nf_nat_decode_session(skb, &fl, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
2080 /* First, check used SA against their selectors. */
2081 if (skb->sp) {
2082 int i;
2083
2084 for (i=skb->sp->len-1; i>=0; i--) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002085 struct xfrm_state *x = skb->sp->xvec[i];
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002086 if (!xfrm_selector_match(&x->sel, &fl, family)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002087 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 }
2091 }
2092
2093 pol = NULL;
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002094 if (sk && sk->sk_policy[dir]) {
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002095 pol = xfrm_sk_policy_lookup(sk, dir, &fl);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002096 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002097 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002098 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002099 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
Timo Teräsfe1a5f02010-04-07 00:30:04 +00002102 if (!pol) {
2103 struct flow_cache_object *flo;
2104
2105 flo = flow_cache_lookup(net, &fl, family, fl_dir,
2106 xfrm_policy_lookup, NULL);
2107 if (IS_ERR_OR_NULL(flo))
2108 pol = ERR_CAST(flo);
2109 else
2110 pol = container_of(flo, struct xfrm_policy, flo);
2111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002113 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002114 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002115 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002116 }
James Morris134b0fc2006-10-05 15:42:27 -05002117
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002118 if (!pol) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002119 if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002120 xfrm_secpath_reject(xerr_idx, skb, &fl);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002121 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOPOLS);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002122 return 0;
2123 }
2124 return 1;
2125 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
James Morris9d729f72007-03-04 16:12:44 -08002127 pol->curlft.use_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002129 pols[0] = pol;
2130 npols ++;
2131#ifdef CONFIG_XFRM_SUB_POLICY
2132 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002133 pols[1] = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002134 &fl, family,
2135 XFRM_POLICY_IN);
2136 if (pols[1]) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002137 if (IS_ERR(pols[1])) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002138 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002139 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002140 }
James Morris9d729f72007-03-04 16:12:44 -08002141 pols[1]->curlft.use_time = get_seconds();
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002142 npols ++;
2143 }
2144 }
2145#endif
2146
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 if (pol->action == XFRM_POLICY_ALLOW) {
2148 struct sec_path *sp;
2149 static struct sec_path dummy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002150 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002151 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002152 struct xfrm_tmpl **tpp = tp;
2153 int ti = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 int i, k;
2155
2156 if ((sp = skb->sp) == NULL)
2157 sp = &dummy;
2158
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002159 for (pi = 0; pi < npols; pi++) {
2160 if (pols[pi] != pol &&
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002161 pols[pi]->action != XFRM_POLICY_ALLOW) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002162 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002163 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002164 }
2165 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002166 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002167 goto reject_error;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002168 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002169 for (i = 0; i < pols[pi]->xfrm_nr; i++)
2170 tpp[ti++] = &pols[pi]->xfrm_vec[i];
2171 }
2172 xfrm_nr = ti;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002173 if (npols > 1) {
2174 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family);
2175 tpp = stp;
2176 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002177
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 /* For each tunnel xfrm, find the first matching tmpl.
2179 * For each tmpl before that, find corresponding xfrm.
2180 * Order is _important_. Later we will implement
2181 * some barriers, but at the moment barriers
2182 * are implied between each two transformations.
2183 */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002184 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
2185 k = xfrm_policy_ok(tpp[i], sp, k, family);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002186 if (k < 0) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002187 if (k < -1)
2188 /* "-2 - errored_index" returned */
2189 xerr_idx = -(2+k);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002190 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 goto reject;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 }
2194
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002195 if (secpath_has_nontransport(sp, k, &xerr_idx)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002196 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002200 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 return 1;
2202 }
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002203 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
2205reject:
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002206 xfrm_secpath_reject(xerr_idx, skb, &fl);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002207reject_error:
2208 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 return 0;
2210}
2211EXPORT_SYMBOL(__xfrm_policy_check);
2212
2213int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
2214{
Alexey Dobriyan99a66652008-11-25 17:36:13 -08002215 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 struct flowi fl;
Eric Dumazetadf30902009-06-02 05:19:30 +00002217 struct dst_entry *dst;
Eric Dumazet73137142011-03-15 15:26:43 -07002218 int res = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002220 if (xfrm_decode_session(skb, &fl, family) < 0) {
jamal72032fd2010-02-18 03:35:07 +00002221 XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224
Eric Dumazetfafeeb62010-06-01 10:04:49 +00002225 skb_dst_force(skb);
Eric Dumazetadf30902009-06-02 05:19:30 +00002226
David S. Miller452edd52011-03-02 13:27:41 -08002227 dst = xfrm_lookup(net, skb_dst(skb), &fl, NULL, 0);
2228 if (IS_ERR(dst)) {
Eric Dumazet73137142011-03-15 15:26:43 -07002229 res = 0;
David S. Miller452edd52011-03-02 13:27:41 -08002230 dst = NULL;
2231 }
Eric Dumazetadf30902009-06-02 05:19:30 +00002232 skb_dst_set(skb, dst);
2233 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234}
2235EXPORT_SYMBOL(__xfrm_route_forward);
2236
David S. Millerd49c73c2006-08-13 18:55:53 -07002237/* Optimize later using cookies and generation ids. */
2238
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
2240{
David S. Millerd49c73c2006-08-13 18:55:53 -07002241 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
David S. Millerf5b0a872012-07-19 12:31:33 -07002242 * to DST_OBSOLETE_FORCE_CHK to force all XFRM destinations to
2243 * get validated by dst_ops->check on every use. We do this
2244 * because when a normal route referenced by an XFRM dst is
2245 * obsoleted we do not go looking around for all parent
2246 * referencing XFRM dsts so that we can invalidate them. It
2247 * is just too much work. Instead we make the checks here on
2248 * every use. For example:
David S. Millerd49c73c2006-08-13 18:55:53 -07002249 *
2250 * XFRM dst A --> IPv4 dst X
2251 *
2252 * X is the "xdst->route" of A (X is also the "dst->path" of A
2253 * in this example). If X is marked obsolete, "A" will not
2254 * notice. That's what we are validating here via the
2255 * stale_bundle() check.
2256 *
2257 * When a policy's bundle is pruned, we dst_free() the XFRM
David S. Millerf5b0a872012-07-19 12:31:33 -07002258 * dst which causes it's ->obsolete field to be set to
2259 * DST_OBSOLETE_DEAD. If an XFRM dst has been pruned like
2260 * this, we want to force a new route lookup.
David S. Miller399c1802005-12-19 14:23:23 -08002261 */
David S. Millerd49c73c2006-08-13 18:55:53 -07002262 if (dst->obsolete < 0 && !stale_bundle(dst))
2263 return dst;
2264
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 return NULL;
2266}
2267
2268static int stale_bundle(struct dst_entry *dst)
2269{
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002270 return !xfrm_bundle_ok((struct xfrm_dst *)dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271}
2272
Herbert Xuaabc9762005-05-03 16:27:10 -07002273void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002276 dst->dev = dev_net(dev)->loopback_dev;
Daniel Lezcanode3cb742007-09-25 19:16:28 -07002277 dev_hold(dst->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 dev_put(dev);
2279 }
2280}
Herbert Xuaabc9762005-05-03 16:27:10 -07002281EXPORT_SYMBOL(xfrm_dst_ifdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282
2283static void xfrm_link_failure(struct sk_buff *skb)
2284{
2285 /* Impossible. Such dst must be popped before reaches point of failure. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286}
2287
2288static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
2289{
2290 if (dst) {
2291 if (dst->obsolete) {
2292 dst_release(dst);
2293 dst = NULL;
2294 }
2295 }
2296 return dst;
2297}
2298
Alexey Dobriyanddcfd792008-11-25 17:37:23 -08002299static void __xfrm_garbage_collect(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300{
Timo Teräs80c802f2010-04-07 00:30:05 +00002301 struct dst_entry *head, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302
Timo Teräs80c802f2010-04-07 00:30:05 +00002303 spin_lock_bh(&xfrm_policy_sk_bundle_lock);
2304 head = xfrm_policy_sk_bundles;
2305 xfrm_policy_sk_bundles = NULL;
2306 spin_unlock_bh(&xfrm_policy_sk_bundle_lock);
2307
2308 while (head) {
2309 next = head->next;
2310 dst_free(head);
2311 head = next;
2312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313}
2314
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002315static void xfrm_garbage_collect(struct net *net)
2316{
2317 flow_cache_flush();
2318 __xfrm_garbage_collect(net);
2319}
2320
2321static void xfrm_garbage_collect_deferred(struct net *net)
2322{
2323 flow_cache_flush_deferred();
2324 __xfrm_garbage_collect(net);
2325}
2326
Herbert Xu25ee3282007-12-11 09:32:34 -08002327static void xfrm_init_pmtu(struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328{
2329 do {
2330 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2331 u32 pmtu, route_mtu_cached;
2332
2333 pmtu = dst_mtu(dst->child);
2334 xdst->child_mtu_cached = pmtu;
2335
2336 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
2337
2338 route_mtu_cached = dst_mtu(xdst->route);
2339 xdst->route_mtu_cached = route_mtu_cached;
2340
2341 if (pmtu > route_mtu_cached)
2342 pmtu = route_mtu_cached;
2343
David S. Millerdefb3512010-12-08 21:16:57 -08002344 dst_metric_set(dst, RTAX_MTU, pmtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 } while ((dst = dst->next));
2346}
2347
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348/* Check that the bundle accepts the flow and its components are
2349 * still valid.
2350 */
2351
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002352static int xfrm_bundle_ok(struct xfrm_dst *first)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353{
2354 struct dst_entry *dst = &first->u.dst;
2355 struct xfrm_dst *last;
2356 u32 mtu;
2357
Hideaki YOSHIFUJI92d63dec2005-05-26 12:58:04 -07002358 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 (dst->dev && !netif_running(dst->dev)))
2360 return 0;
2361
2362 last = NULL;
2363
2364 do {
2365 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2366
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 if (dst->xfrm->km.state != XFRM_STATE_VALID)
2368 return 0;
Timo Teräs80c802f2010-04-07 00:30:05 +00002369 if (xdst->xfrm_genid != dst->xfrm->genid)
2370 return 0;
Timo Teräsb1312c82010-06-24 14:35:00 -07002371 if (xdst->num_pols > 0 &&
2372 xdst->policy_genid != atomic_read(&xdst->pols[0]->genid))
David S. Miller9d4a7062006-08-24 03:18:09 -07002373 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
2375 mtu = dst_mtu(dst->child);
2376 if (xdst->child_mtu_cached != mtu) {
2377 last = xdst;
2378 xdst->child_mtu_cached = mtu;
2379 }
2380
Hideaki YOSHIFUJI92d63dec2005-05-26 12:58:04 -07002381 if (!dst_check(xdst->route, xdst->route_cookie))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 return 0;
2383 mtu = dst_mtu(xdst->route);
2384 if (xdst->route_mtu_cached != mtu) {
2385 last = xdst;
2386 xdst->route_mtu_cached = mtu;
2387 }
2388
2389 dst = dst->child;
2390 } while (dst->xfrm);
2391
2392 if (likely(!last))
2393 return 1;
2394
2395 mtu = last->child_mtu_cached;
2396 for (;;) {
2397 dst = &last->u.dst;
2398
2399 mtu = xfrm_state_mtu(dst->xfrm, mtu);
2400 if (mtu > last->route_mtu_cached)
2401 mtu = last->route_mtu_cached;
David S. Millerdefb3512010-12-08 21:16:57 -08002402 dst_metric_set(dst, RTAX_MTU, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
2404 if (last == first)
2405 break;
2406
Patrick McHardybd0bf072007-07-18 01:55:52 -07002407 last = (struct xfrm_dst *)last->u.dst.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 last->child_mtu_cached = mtu;
2409 }
2410
2411 return 1;
2412}
2413
David S. Miller0dbaee32010-12-13 12:52:14 -08002414static unsigned int xfrm_default_advmss(const struct dst_entry *dst)
2415{
2416 return dst_metric_advmss(dst->path);
2417}
2418
Steffen Klassertebb762f2011-11-23 02:12:51 +00002419static unsigned int xfrm_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08002420{
Steffen Klassert618f9bc2011-11-23 02:13:31 +00002421 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
2422
2423 return mtu ? : dst_mtu(dst->path);
David S. Millerd33e4552010-12-14 13:01:14 -08002424}
2425
David S. Millerf894cbf2012-07-02 21:52:24 -07002426static struct neighbour *xfrm_neigh_lookup(const struct dst_entry *dst,
2427 struct sk_buff *skb,
2428 const void *daddr)
David S. Millerd3aaeb32011-07-18 00:40:17 -07002429{
David S. Millerf894cbf2012-07-02 21:52:24 -07002430 return dst->path->ops->neigh_lookup(dst, skb, daddr);
David S. Millerd3aaeb32011-07-18 00:40:17 -07002431}
2432
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
2434{
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002435 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 int err = 0;
2437 if (unlikely(afinfo == NULL))
2438 return -EINVAL;
2439 if (unlikely(afinfo->family >= NPROTO))
2440 return -EAFNOSUPPORT;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002441 spin_lock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
2443 err = -ENOBUFS;
2444 else {
2445 struct dst_ops *dst_ops = afinfo->dst_ops;
2446 if (likely(dst_ops->kmem_cachep == NULL))
2447 dst_ops->kmem_cachep = xfrm_dst_cache;
2448 if (likely(dst_ops->check == NULL))
2449 dst_ops->check = xfrm_dst_check;
David S. Miller0dbaee32010-12-13 12:52:14 -08002450 if (likely(dst_ops->default_advmss == NULL))
2451 dst_ops->default_advmss = xfrm_default_advmss;
Steffen Klassertebb762f2011-11-23 02:12:51 +00002452 if (likely(dst_ops->mtu == NULL))
2453 dst_ops->mtu = xfrm_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 if (likely(dst_ops->negative_advice == NULL))
2455 dst_ops->negative_advice = xfrm_negative_advice;
2456 if (likely(dst_ops->link_failure == NULL))
2457 dst_ops->link_failure = xfrm_link_failure;
David S. Millerd3aaeb32011-07-18 00:40:17 -07002458 if (likely(dst_ops->neigh_lookup == NULL))
2459 dst_ops->neigh_lookup = xfrm_neigh_lookup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 if (likely(afinfo->garbage_collect == NULL))
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002461 afinfo->garbage_collect = xfrm_garbage_collect_deferred;
Priyanka Jain418a99a2012-08-12 21:22:29 +00002462 rcu_assign_pointer(xfrm_policy_afinfo[afinfo->family], afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02002464 spin_unlock(&xfrm_policy_afinfo_lock);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002465
2466 rtnl_lock();
2467 for_each_net(net) {
2468 struct dst_ops *xfrm_dst_ops;
2469
2470 switch (afinfo->family) {
2471 case AF_INET:
2472 xfrm_dst_ops = &net->xfrm.xfrm4_dst_ops;
2473 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002474#if IS_ENABLED(CONFIG_IPV6)
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002475 case AF_INET6:
2476 xfrm_dst_ops = &net->xfrm.xfrm6_dst_ops;
2477 break;
2478#endif
2479 default:
2480 BUG();
2481 }
2482 *xfrm_dst_ops = *afinfo->dst_ops;
2483 }
2484 rtnl_unlock();
2485
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 return err;
2487}
2488EXPORT_SYMBOL(xfrm_policy_register_afinfo);
2489
2490int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
2491{
2492 int err = 0;
2493 if (unlikely(afinfo == NULL))
2494 return -EINVAL;
2495 if (unlikely(afinfo->family >= NPROTO))
2496 return -EAFNOSUPPORT;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002497 spin_lock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
2499 if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
2500 err = -EINVAL;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002501 else
2502 RCU_INIT_POINTER(xfrm_policy_afinfo[afinfo->family],
2503 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02002505 spin_unlock(&xfrm_policy_afinfo_lock);
2506 if (!err) {
2507 struct dst_ops *dst_ops = afinfo->dst_ops;
2508
2509 synchronize_rcu();
2510
2511 dst_ops->kmem_cachep = NULL;
2512 dst_ops->check = NULL;
2513 dst_ops->negative_advice = NULL;
2514 dst_ops->link_failure = NULL;
2515 afinfo->garbage_collect = NULL;
2516 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 return err;
2518}
2519EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
2520
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002521static void __net_init xfrm_dst_ops_init(struct net *net)
2522{
2523 struct xfrm_policy_afinfo *afinfo;
2524
Eric Dumazetef8531b2012-08-19 12:31:48 +02002525 rcu_read_lock();
2526 afinfo = rcu_dereference(xfrm_policy_afinfo[AF_INET]);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002527 if (afinfo)
2528 net->xfrm.xfrm4_dst_ops = *afinfo->dst_ops;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002529#if IS_ENABLED(CONFIG_IPV6)
Eric Dumazetef8531b2012-08-19 12:31:48 +02002530 afinfo = rcu_dereference(xfrm_policy_afinfo[AF_INET6]);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002531 if (afinfo)
2532 net->xfrm.xfrm6_dst_ops = *afinfo->dst_ops;
2533#endif
Priyanka Jain418a99a2012-08-12 21:22:29 +00002534 rcu_read_unlock();
Herbert Xu546be242006-05-27 23:03:58 -07002535}
2536
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
2538{
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02002539 struct net_device *dev = ptr;
2540
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 switch (event) {
2542 case NETDEV_DOWN:
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002543 xfrm_garbage_collect(dev_net(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 }
2545 return NOTIFY_DONE;
2546}
2547
2548static struct notifier_block xfrm_dev_notifier = {
Alexey Dobriyand5917a32008-10-31 00:41:59 -07002549 .notifier_call = xfrm_dev_event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550};
2551
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002552#ifdef CONFIG_XFRM_STATISTICS
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002553static int __net_init xfrm_statistics_init(struct net *net)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002554{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002555 int rv;
2556
Tejun Heo7d720c32010-02-16 15:20:26 +00002557 if (snmp_mib_init((void __percpu **)net->mib.xfrm_statistics,
Eric Dumazet1823e4c82010-06-22 20:58:41 +00002558 sizeof(struct linux_xfrm_mib),
2559 __alignof__(struct linux_xfrm_mib)) < 0)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002560 return -ENOMEM;
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002561 rv = xfrm_proc_init(net);
2562 if (rv < 0)
Tejun Heo7d720c32010-02-16 15:20:26 +00002563 snmp_mib_free((void __percpu **)net->mib.xfrm_statistics);
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002564 return rv;
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002565}
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002566
2567static void xfrm_statistics_fini(struct net *net)
2568{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002569 xfrm_proc_fini(net);
Tejun Heo7d720c32010-02-16 15:20:26 +00002570 snmp_mib_free((void __percpu **)net->mib.xfrm_statistics);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002571}
2572#else
2573static int __net_init xfrm_statistics_init(struct net *net)
2574{
2575 return 0;
2576}
2577
2578static void xfrm_statistics_fini(struct net *net)
2579{
2580}
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002581#endif
2582
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002583static int __net_init xfrm_policy_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584{
David S. Miller2518c7c2006-08-24 04:45:07 -07002585 unsigned int hmask, sz;
2586 int dir;
2587
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002588 if (net_eq(net, &init_net))
2589 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 sizeof(struct xfrm_dst),
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002591 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002592 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593
David S. Miller2518c7c2006-08-24 04:45:07 -07002594 hmask = 8 - 1;
2595 sz = (hmask+1) * sizeof(struct hlist_head);
2596
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002597 net->xfrm.policy_byidx = xfrm_hash_alloc(sz);
2598 if (!net->xfrm.policy_byidx)
2599 goto out_byidx;
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08002600 net->xfrm.policy_idx_hmask = hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -07002601
2602 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
2603 struct xfrm_policy_hash *htab;
2604
Alexey Dobriyandc2caba2008-11-25 17:24:15 -08002605 net->xfrm.policy_count[dir] = 0;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002606 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
David S. Miller2518c7c2006-08-24 04:45:07 -07002607
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002608 htab = &net->xfrm.policy_bydst[dir];
David S. Miller44e36b42006-08-24 04:50:50 -07002609 htab->table = xfrm_hash_alloc(sz);
David S. Miller2518c7c2006-08-24 04:45:07 -07002610 if (!htab->table)
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002611 goto out_bydst;
2612 htab->hmask = hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -07002613 }
2614
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08002615 INIT_LIST_HEAD(&net->xfrm.policy_all);
Alexey Dobriyan66caf622008-11-25 17:28:57 -08002616 INIT_WORK(&net->xfrm.policy_hash_work, xfrm_hash_resize);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002617 if (net_eq(net, &init_net))
2618 register_netdevice_notifier(&xfrm_dev_notifier);
2619 return 0;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002620
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002621out_bydst:
2622 for (dir--; dir >= 0; dir--) {
2623 struct xfrm_policy_hash *htab;
2624
2625 htab = &net->xfrm.policy_bydst[dir];
2626 xfrm_hash_free(htab->table, sz);
2627 }
2628 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002629out_byidx:
2630 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631}
2632
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002633static void xfrm_policy_fini(struct net *net)
2634{
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002635 struct xfrm_audit audit_info;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002636 unsigned int sz;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002637 int dir;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002638
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002639 flush_work(&net->xfrm.policy_hash_work);
2640#ifdef CONFIG_XFRM_SUB_POLICY
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07002641 audit_info.loginuid = INVALID_UID;
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002642 audit_info.sessionid = -1;
2643 audit_info.secid = 0;
2644 xfrm_policy_flush(net, XFRM_POLICY_TYPE_SUB, &audit_info);
2645#endif
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07002646 audit_info.loginuid = INVALID_UID;
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002647 audit_info.sessionid = -1;
2648 audit_info.secid = 0;
2649 xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, &audit_info);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002650
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08002651 WARN_ON(!list_empty(&net->xfrm.policy_all));
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002652
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002653 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002654 struct xfrm_policy_hash *htab;
2655
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002656 WARN_ON(!hlist_empty(&net->xfrm.policy_inexact[dir]));
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002657
2658 htab = &net->xfrm.policy_bydst[dir];
2659 sz = (htab->hmask + 1);
2660 WARN_ON(!hlist_empty(htab->table));
2661 xfrm_hash_free(htab->table, sz);
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002662 }
2663
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08002664 sz = (net->xfrm.policy_idx_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002665 WARN_ON(!hlist_empty(net->xfrm.policy_byidx));
2666 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002667}
2668
2669static int __net_init xfrm_net_init(struct net *net)
2670{
2671 int rv;
2672
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002673 rv = xfrm_statistics_init(net);
2674 if (rv < 0)
2675 goto out_statistics;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002676 rv = xfrm_state_init(net);
2677 if (rv < 0)
2678 goto out_state;
2679 rv = xfrm_policy_init(net);
2680 if (rv < 0)
2681 goto out_policy;
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002682 xfrm_dst_ops_init(net);
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08002683 rv = xfrm_sysctl_init(net);
2684 if (rv < 0)
2685 goto out_sysctl;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002686 return 0;
2687
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08002688out_sysctl:
2689 xfrm_policy_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002690out_policy:
2691 xfrm_state_fini(net);
2692out_state:
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002693 xfrm_statistics_fini(net);
2694out_statistics:
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002695 return rv;
2696}
2697
2698static void __net_exit xfrm_net_exit(struct net *net)
2699{
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08002700 xfrm_sysctl_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002701 xfrm_policy_fini(net);
2702 xfrm_state_fini(net);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002703 xfrm_statistics_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002704}
2705
2706static struct pernet_operations __net_initdata xfrm_net_ops = {
2707 .init = xfrm_net_init,
2708 .exit = xfrm_net_exit,
2709};
2710
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711void __init xfrm_init(void)
2712{
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002713 register_pernet_subsys(&xfrm_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 xfrm_input_init();
2715}
2716
Joy Lattenab5f5e82007-09-17 11:51:22 -07002717#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinen1486cbd72008-01-12 03:20:03 -08002718static void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
2719 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002720{
Paul Moore875179f2007-12-01 23:27:18 +11002721 struct xfrm_sec_ctx *ctx = xp->security;
2722 struct xfrm_selector *sel = &xp->selector;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002723
Paul Moore875179f2007-12-01 23:27:18 +11002724 if (ctx)
2725 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
2726 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
2727
2728 switch(sel->family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07002729 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07002730 audit_log_format(audit_buf, " src=%pI4", &sel->saddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11002731 if (sel->prefixlen_s != 32)
2732 audit_log_format(audit_buf, " src_prefixlen=%d",
2733 sel->prefixlen_s);
Harvey Harrison21454aa2008-10-31 00:54:56 -07002734 audit_log_format(audit_buf, " dst=%pI4", &sel->daddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11002735 if (sel->prefixlen_d != 32)
2736 audit_log_format(audit_buf, " dst_prefixlen=%d",
2737 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002738 break;
2739 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002740 audit_log_format(audit_buf, " src=%pI6", sel->saddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11002741 if (sel->prefixlen_s != 128)
2742 audit_log_format(audit_buf, " src_prefixlen=%d",
2743 sel->prefixlen_s);
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002744 audit_log_format(audit_buf, " dst=%pI6", sel->daddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11002745 if (sel->prefixlen_d != 128)
2746 audit_log_format(audit_buf, " dst_prefixlen=%d",
2747 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002748 break;
2749 }
2750}
2751
Paul Moore68277ac2007-12-20 20:49:33 -08002752void xfrm_audit_policy_add(struct xfrm_policy *xp, int result,
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07002753 kuid_t auid, u32 sessionid, u32 secid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002754{
2755 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002756
Paul Mooreafeb14b2007-12-21 14:58:11 -08002757 audit_buf = xfrm_audit_start("SPD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002758 if (audit_buf == NULL)
2759 return;
Eric Paris25323862008-04-18 10:09:25 -04002760 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002761 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002762 xfrm_audit_common_policyinfo(xp, audit_buf);
2763 audit_log_end(audit_buf);
2764}
2765EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
2766
Paul Moore68277ac2007-12-20 20:49:33 -08002767void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07002768 kuid_t auid, u32 sessionid, u32 secid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002769{
2770 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002771
Paul Mooreafeb14b2007-12-21 14:58:11 -08002772 audit_buf = xfrm_audit_start("SPD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002773 if (audit_buf == NULL)
2774 return;
Eric Paris25323862008-04-18 10:09:25 -04002775 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002776 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002777 xfrm_audit_common_policyinfo(xp, audit_buf);
2778 audit_log_end(audit_buf);
2779}
2780EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
2781#endif
2782
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002783#ifdef CONFIG_XFRM_MIGRATE
David S. Millerbc9b35a2012-05-15 15:04:57 -04002784static bool xfrm_migrate_selector_match(const struct xfrm_selector *sel_cmp,
2785 const struct xfrm_selector *sel_tgt)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002786{
2787 if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
2788 if (sel_tgt->family == sel_cmp->family &&
2789 xfrm_addr_cmp(&sel_tgt->daddr, &sel_cmp->daddr,
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002790 sel_cmp->family) == 0 &&
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002791 xfrm_addr_cmp(&sel_tgt->saddr, &sel_cmp->saddr,
2792 sel_cmp->family) == 0 &&
2793 sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
2794 sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04002795 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002796 }
2797 } else {
2798 if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04002799 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002800 }
2801 }
David S. Millerbc9b35a2012-05-15 15:04:57 -04002802 return false;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002803}
2804
David S. Millerb4b7c0b2011-02-24 00:35:06 -05002805static struct xfrm_policy * xfrm_migrate_policy_find(const struct xfrm_selector *sel,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002806 u8 dir, u8 type)
2807{
2808 struct xfrm_policy *pol, *ret = NULL;
2809 struct hlist_node *entry;
2810 struct hlist_head *chain;
2811 u32 priority = ~0U;
2812
2813 read_lock_bh(&xfrm_policy_lock);
Alexey Dobriyan11219942008-11-25 17:33:06 -08002814 chain = policy_hash_direct(&init_net, &sel->daddr, &sel->saddr, sel->family, dir);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002815 hlist_for_each_entry(pol, entry, chain, bydst) {
2816 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
2817 pol->type == type) {
2818 ret = pol;
2819 priority = ret->priority;
2820 break;
2821 }
2822 }
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002823 chain = &init_net.xfrm.policy_inexact[dir];
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002824 hlist_for_each_entry(pol, entry, chain, bydst) {
2825 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
2826 pol->type == type &&
2827 pol->priority < priority) {
2828 ret = pol;
2829 break;
2830 }
2831 }
2832
2833 if (ret)
2834 xfrm_pol_hold(ret);
2835
2836 read_unlock_bh(&xfrm_policy_lock);
2837
2838 return ret;
2839}
2840
David S. Millerdd701752011-02-24 00:21:08 -05002841static int migrate_tmpl_match(const struct xfrm_migrate *m, const struct xfrm_tmpl *t)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002842{
2843 int match = 0;
2844
2845 if (t->mode == m->mode && t->id.proto == m->proto &&
2846 (m->reqid == 0 || t->reqid == m->reqid)) {
2847 switch (t->mode) {
2848 case XFRM_MODE_TUNNEL:
2849 case XFRM_MODE_BEET:
2850 if (xfrm_addr_cmp(&t->id.daddr, &m->old_daddr,
2851 m->old_family) == 0 &&
2852 xfrm_addr_cmp(&t->saddr, &m->old_saddr,
2853 m->old_family) == 0) {
2854 match = 1;
2855 }
2856 break;
2857 case XFRM_MODE_TRANSPORT:
2858 /* in case of transport mode, template does not store
2859 any IP addresses, hence we just compare mode and
2860 protocol */
2861 match = 1;
2862 break;
2863 default:
2864 break;
2865 }
2866 }
2867 return match;
2868}
2869
2870/* update endpoint address(es) of template(s) */
2871static int xfrm_policy_migrate(struct xfrm_policy *pol,
2872 struct xfrm_migrate *m, int num_migrate)
2873{
2874 struct xfrm_migrate *mp;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002875 int i, j, n = 0;
2876
2877 write_lock_bh(&pol->lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07002878 if (unlikely(pol->walk.dead)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002879 /* target policy has been deleted */
2880 write_unlock_bh(&pol->lock);
2881 return -ENOENT;
2882 }
2883
2884 for (i = 0; i < pol->xfrm_nr; i++) {
2885 for (j = 0, mp = m; j < num_migrate; j++, mp++) {
2886 if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
2887 continue;
2888 n++;
Herbert Xu1bfcb102007-10-17 21:31:50 -07002889 if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
2890 pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002891 continue;
2892 /* update endpoints */
2893 memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
2894 sizeof(pol->xfrm_vec[i].id.daddr));
2895 memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
2896 sizeof(pol->xfrm_vec[i].saddr));
2897 pol->xfrm_vec[i].encap_family = mp->new_family;
2898 /* flush bundles */
Timo Teräs80c802f2010-04-07 00:30:05 +00002899 atomic_inc(&pol->genid);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002900 }
2901 }
2902
2903 write_unlock_bh(&pol->lock);
2904
2905 if (!n)
2906 return -ENODATA;
2907
2908 return 0;
2909}
2910
David S. Millerdd701752011-02-24 00:21:08 -05002911static int xfrm_migrate_check(const struct xfrm_migrate *m, int num_migrate)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002912{
2913 int i, j;
2914
2915 if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
2916 return -EINVAL;
2917
2918 for (i = 0; i < num_migrate; i++) {
2919 if ((xfrm_addr_cmp(&m[i].old_daddr, &m[i].new_daddr,
2920 m[i].old_family) == 0) &&
2921 (xfrm_addr_cmp(&m[i].old_saddr, &m[i].new_saddr,
2922 m[i].old_family) == 0))
2923 return -EINVAL;
2924 if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
2925 xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
2926 return -EINVAL;
2927
2928 /* check if there is any duplicated entry */
2929 for (j = i + 1; j < num_migrate; j++) {
2930 if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
2931 sizeof(m[i].old_daddr)) &&
2932 !memcmp(&m[i].old_saddr, &m[j].old_saddr,
2933 sizeof(m[i].old_saddr)) &&
2934 m[i].proto == m[j].proto &&
2935 m[i].mode == m[j].mode &&
2936 m[i].reqid == m[j].reqid &&
2937 m[i].old_family == m[j].old_family)
2938 return -EINVAL;
2939 }
2940 }
2941
2942 return 0;
2943}
2944
David S. Millerb4b7c0b2011-02-24 00:35:06 -05002945int xfrm_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002946 struct xfrm_migrate *m, int num_migrate,
2947 struct xfrm_kmaddress *k)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002948{
2949 int i, err, nx_cur = 0, nx_new = 0;
2950 struct xfrm_policy *pol = NULL;
2951 struct xfrm_state *x, *xc;
2952 struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
2953 struct xfrm_state *x_new[XFRM_MAX_DEPTH];
2954 struct xfrm_migrate *mp;
2955
2956 if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
2957 goto out;
2958
2959 /* Stage 1 - find policy */
2960 if ((pol = xfrm_migrate_policy_find(sel, dir, type)) == NULL) {
2961 err = -ENOENT;
2962 goto out;
2963 }
2964
2965 /* Stage 2 - find and update state(s) */
2966 for (i = 0, mp = m; i < num_migrate; i++, mp++) {
2967 if ((x = xfrm_migrate_state_find(mp))) {
2968 x_cur[nx_cur] = x;
2969 nx_cur++;
2970 if ((xc = xfrm_state_migrate(x, mp))) {
2971 x_new[nx_new] = xc;
2972 nx_new++;
2973 } else {
2974 err = -ENODATA;
2975 goto restore_state;
2976 }
2977 }
2978 }
2979
2980 /* Stage 3 - update policy */
2981 if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
2982 goto restore_state;
2983
2984 /* Stage 4 - delete old state(s) */
2985 if (nx_cur) {
2986 xfrm_states_put(x_cur, nx_cur);
2987 xfrm_states_delete(x_cur, nx_cur);
2988 }
2989
2990 /* Stage 5 - announce */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002991 km_migrate(sel, dir, type, m, num_migrate, k);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002992
2993 xfrm_pol_put(pol);
2994
2995 return 0;
2996out:
2997 return err;
2998
2999restore_state:
3000 if (pol)
3001 xfrm_pol_put(pol);
3002 if (nx_cur)
3003 xfrm_states_put(x_cur, nx_cur);
3004 if (nx_new)
3005 xfrm_states_delete(x_new, nx_new);
3006
3007 return err;
3008}
David S. Millere610e672007-02-08 13:29:15 -08003009EXPORT_SYMBOL(xfrm_migrate);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003010#endif