blob: 741a32aa512e30859a213f7926e97b8b3aa4a3ad [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);
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800605 if (delpol)
606 __xfrm_policy_unlink(delpol, dir);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800607 policy->index = delpol ? delpol->index : xfrm_gen_index(net, dir);
608 hlist_add_head(&policy->byidx, net->xfrm.policy_byidx+idx_hash(net, policy->index));
James Morris9d729f72007-03-04 16:12:44 -0800609 policy->curlft.add_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 policy->curlft.use_time = 0;
611 if (!mod_timer(&policy->timer, jiffies + HZ))
612 xfrm_pol_hold(policy);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800613 list_add(&policy->walk.all, &net->xfrm.policy_all);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 write_unlock_bh(&xfrm_policy_lock);
615
David S. Miller9b78a822005-12-22 07:39:48 -0800616 if (delpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 xfrm_policy_kill(delpol);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800618 else if (xfrm_bydst_should_resize(net, dir, NULL))
619 schedule_work(&net->xfrm.policy_hash_work);
David S. Miller9b78a822005-12-22 07:39:48 -0800620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 return 0;
622}
623EXPORT_SYMBOL(xfrm_policy_insert);
624
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +0000625struct xfrm_policy *xfrm_policy_bysel_ctx(struct net *net, u32 mark, u8 type,
626 int dir, struct xfrm_selector *sel,
Eric Parisef41aaa2007-03-07 15:37:58 -0800627 struct xfrm_sec_ctx *ctx, int delete,
628 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
David S. Miller2518c7c2006-08-24 04:45:07 -0700630 struct xfrm_policy *pol, *ret;
631 struct hlist_head *chain;
632 struct hlist_node *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Eric Parisef41aaa2007-03-07 15:37:58 -0800634 *err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 write_lock_bh(&xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800636 chain = policy_hash_bysel(net, sel, sel->family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700637 ret = NULL;
638 hlist_for_each_entry(pol, entry, chain, bydst) {
639 if (pol->type == type &&
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000640 (mark & pol->mark.m) == pol->mark.v &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700641 !selector_cmp(sel, &pol->selector) &&
642 xfrm_sec_ctx_match(ctx, pol->security)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700644 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -0700645 *err = security_xfrm_policy_delete(
646 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -0800647 if (*err) {
648 write_unlock_bh(&xfrm_policy_lock);
649 return pol;
650 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800651 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700652 }
653 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 break;
655 }
656 }
657 write_unlock_bh(&xfrm_policy_lock);
658
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000659 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -0700660 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -0700661 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662}
Trent Jaegerdf718372005-12-13 23:12:27 -0800663EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +0000665struct xfrm_policy *xfrm_policy_byid(struct net *net, u32 mark, u8 type,
666 int dir, u32 id, int delete, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
David S. Miller2518c7c2006-08-24 04:45:07 -0700668 struct xfrm_policy *pol, *ret;
669 struct hlist_head *chain;
670 struct hlist_node *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Herbert Xub5505c62007-05-14 02:15:47 -0700672 *err = -ENOENT;
673 if (xfrm_policy_id2dir(id) != dir)
674 return NULL;
675
Eric Parisef41aaa2007-03-07 15:37:58 -0800676 *err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 write_lock_bh(&xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800678 chain = net->xfrm.policy_byidx + idx_hash(net, id);
David S. Miller2518c7c2006-08-24 04:45:07 -0700679 ret = NULL;
680 hlist_for_each_entry(pol, entry, chain, byidx) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000681 if (pol->type == type && pol->index == id &&
682 (mark & pol->mark.m) == pol->mark.v) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700684 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -0700685 *err = security_xfrm_policy_delete(
686 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -0800687 if (*err) {
688 write_unlock_bh(&xfrm_policy_lock);
689 return pol;
690 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800691 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700692 }
693 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 break;
695 }
696 }
697 write_unlock_bh(&xfrm_policy_lock);
698
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000699 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -0700700 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -0700701 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702}
703EXPORT_SYMBOL(xfrm_policy_byid);
704
Joy Latten4aa2e622007-06-04 19:05:57 -0400705#ifdef CONFIG_SECURITY_NETWORK_XFRM
706static inline int
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800707xfrm_policy_flush_secctx_check(struct net *net, u8 type, struct xfrm_audit *audit_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
Joy Latten4aa2e622007-06-04 19:05:57 -0400709 int dir, err = 0;
710
711 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
712 struct xfrm_policy *pol;
713 struct hlist_node *entry;
714 int i;
715
716 hlist_for_each_entry(pol, entry,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800717 &net->xfrm.policy_inexact[dir], bydst) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400718 if (pol->type != type)
719 continue;
Paul Moore03e1ad72008-04-12 19:07:52 -0700720 err = security_xfrm_policy_delete(pol->security);
Joy Latten4aa2e622007-06-04 19:05:57 -0400721 if (err) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700722 xfrm_audit_policy_delete(pol, 0,
723 audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400724 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700725 audit_info->secid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400726 return err;
727 }
YOSHIFUJI Hideaki7dc12d62007-07-19 10:45:15 +0900728 }
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800729 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400730 hlist_for_each_entry(pol, entry,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800731 net->xfrm.policy_bydst[dir].table + i,
Joy Latten4aa2e622007-06-04 19:05:57 -0400732 bydst) {
733 if (pol->type != type)
734 continue;
Paul Moore03e1ad72008-04-12 19:07:52 -0700735 err = security_xfrm_policy_delete(
736 pol->security);
Joy Latten4aa2e622007-06-04 19:05:57 -0400737 if (err) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700738 xfrm_audit_policy_delete(pol, 0,
739 audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400740 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700741 audit_info->secid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400742 return err;
743 }
744 }
745 }
746 }
747 return err;
748}
749#else
750static inline int
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800751xfrm_policy_flush_secctx_check(struct net *net, u8 type, struct xfrm_audit *audit_info)
Joy Latten4aa2e622007-06-04 19:05:57 -0400752{
753 return 0;
754}
755#endif
756
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800757int xfrm_policy_flush(struct net *net, u8 type, struct xfrm_audit *audit_info)
Joy Latten4aa2e622007-06-04 19:05:57 -0400758{
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +0000759 int dir, err = 0, cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761 write_lock_bh(&xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400762
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800763 err = xfrm_policy_flush_secctx_check(net, type, audit_info);
Joy Latten4aa2e622007-06-04 19:05:57 -0400764 if (err)
765 goto out;
766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700768 struct xfrm_policy *pol;
769 struct hlist_node *entry;
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800770 int i;
David S. Miller2518c7c2006-08-24 04:45:07 -0700771
772 again1:
773 hlist_for_each_entry(pol, entry,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800774 &net->xfrm.policy_inexact[dir], bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700775 if (pol->type != type)
776 continue;
Timo Teräsea2dea92010-03-31 00:17:05 +0000777 __xfrm_policy_unlink(pol, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 write_unlock_bh(&xfrm_policy_lock);
Timo Teräsea2dea92010-03-31 00:17:05 +0000779 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Joy Lattenab5f5e82007-09-17 11:51:22 -0700781 xfrm_audit_policy_delete(pol, 1, audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400782 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700783 audit_info->secid);
Joy Latten161a09e2006-11-27 13:11:54 -0600784
David S. Miller2518c7c2006-08-24 04:45:07 -0700785 xfrm_policy_kill(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
787 write_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700788 goto again1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700790
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800791 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700792 again2:
793 hlist_for_each_entry(pol, entry,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800794 net->xfrm.policy_bydst[dir].table + i,
David S. Miller2518c7c2006-08-24 04:45:07 -0700795 bydst) {
796 if (pol->type != type)
797 continue;
Timo Teräsea2dea92010-03-31 00:17:05 +0000798 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700799 write_unlock_bh(&xfrm_policy_lock);
Timo Teräsea2dea92010-03-31 00:17:05 +0000800 cnt++;
David S. Miller2518c7c2006-08-24 04:45:07 -0700801
Joy Lattenab5f5e82007-09-17 11:51:22 -0700802 xfrm_audit_policy_delete(pol, 1,
803 audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400804 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700805 audit_info->secid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700806 xfrm_policy_kill(pol);
807
808 write_lock_bh(&xfrm_policy_lock);
809 goto again2;
810 }
811 }
812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 }
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +0000814 if (!cnt)
815 err = -ESRCH;
Joy Latten4aa2e622007-06-04 19:05:57 -0400816out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 write_unlock_bh(&xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400818 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819}
820EXPORT_SYMBOL(xfrm_policy_flush);
821
Alexey Dobriyancdcbca72008-11-25 17:34:49 -0800822int xfrm_policy_walk(struct net *net, struct xfrm_policy_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -0800823 int (*func)(struct xfrm_policy *, int, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 void *data)
825{
Herbert Xu12a169e2008-10-01 07:03:24 -0700826 struct xfrm_policy *pol;
827 struct xfrm_policy_walk_entry *x;
Timo Teras4c563f72008-02-28 21:31:08 -0800828 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Timo Teras4c563f72008-02-28 21:31:08 -0800830 if (walk->type >= XFRM_POLICY_TYPE_MAX &&
831 walk->type != XFRM_POLICY_TYPE_ANY)
832 return -EINVAL;
833
Herbert Xu12a169e2008-10-01 07:03:24 -0700834 if (list_empty(&walk->walk.all) && walk->seq != 0)
Timo Teras4c563f72008-02-28 21:31:08 -0800835 return 0;
836
Herbert Xu12a169e2008-10-01 07:03:24 -0700837 write_lock_bh(&xfrm_policy_lock);
838 if (list_empty(&walk->walk.all))
Alexey Dobriyancdcbca72008-11-25 17:34:49 -0800839 x = list_first_entry(&net->xfrm.policy_all, struct xfrm_policy_walk_entry, all);
Herbert Xu12a169e2008-10-01 07:03:24 -0700840 else
841 x = list_entry(&walk->walk.all, struct xfrm_policy_walk_entry, all);
Alexey Dobriyancdcbca72008-11-25 17:34:49 -0800842 list_for_each_entry_from(x, &net->xfrm.policy_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -0700843 if (x->dead)
Timo Teras4c563f72008-02-28 21:31:08 -0800844 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -0700845 pol = container_of(x, struct xfrm_policy, walk);
846 if (walk->type != XFRM_POLICY_TYPE_ANY &&
847 walk->type != pol->type)
848 continue;
849 error = func(pol, xfrm_policy_id2dir(pol->index),
850 walk->seq, data);
851 if (error) {
852 list_move_tail(&walk->walk.all, &x->all);
853 goto out;
Timo Teras4c563f72008-02-28 21:31:08 -0800854 }
Herbert Xu12a169e2008-10-01 07:03:24 -0700855 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 }
Herbert Xu12a169e2008-10-01 07:03:24 -0700857 if (walk->seq == 0) {
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -0800858 error = -ENOENT;
859 goto out;
860 }
Herbert Xu12a169e2008-10-01 07:03:24 -0700861 list_del_init(&walk->walk.all);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862out:
Herbert Xu12a169e2008-10-01 07:03:24 -0700863 write_unlock_bh(&xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 return error;
865}
866EXPORT_SYMBOL(xfrm_policy_walk);
867
Herbert Xu12a169e2008-10-01 07:03:24 -0700868void xfrm_policy_walk_init(struct xfrm_policy_walk *walk, u8 type)
869{
870 INIT_LIST_HEAD(&walk->walk.all);
871 walk->walk.dead = 1;
872 walk->type = type;
873 walk->seq = 0;
874}
875EXPORT_SYMBOL(xfrm_policy_walk_init);
876
877void xfrm_policy_walk_done(struct xfrm_policy_walk *walk)
878{
879 if (list_empty(&walk->walk.all))
880 return;
881
882 write_lock_bh(&xfrm_policy_lock);
883 list_del(&walk->walk.all);
884 write_unlock_bh(&xfrm_policy_lock);
885}
886EXPORT_SYMBOL(xfrm_policy_walk_done);
887
James Morris134b0fc2006-10-05 15:42:27 -0500888/*
889 * Find policy to apply to this flow.
890 *
891 * Returns 0 if policy found, else an -errno.
892 */
David S. Millerf299d552011-02-24 01:23:30 -0500893static int xfrm_policy_match(const struct xfrm_policy *pol,
894 const struct flowi *fl,
David S. Miller2518c7c2006-08-24 04:45:07 -0700895 u8 type, u16 family, int dir)
896{
David S. Millerf299d552011-02-24 01:23:30 -0500897 const struct xfrm_selector *sel = &pol->selector;
David S. Millerbc9b35a2012-05-15 15:04:57 -0400898 int ret = -ESRCH;
899 bool match;
David S. Miller2518c7c2006-08-24 04:45:07 -0700900
901 if (pol->family != family ||
David S. Miller1d28f422011-03-12 00:29:39 -0500902 (fl->flowi_mark & pol->mark.m) != pol->mark.v ||
David S. Miller2518c7c2006-08-24 04:45:07 -0700903 pol->type != type)
James Morris134b0fc2006-10-05 15:42:27 -0500904 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -0700905
906 match = xfrm_selector_match(sel, fl, family);
James Morris134b0fc2006-10-05 15:42:27 -0500907 if (match)
David S. Miller1d28f422011-03-12 00:29:39 -0500908 ret = security_xfrm_policy_lookup(pol->security, fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -0700909 dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700910
James Morris134b0fc2006-10-05 15:42:27 -0500911 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -0700912}
913
Alexey Dobriyan52479b62008-11-25 17:35:18 -0800914static struct xfrm_policy *xfrm_policy_lookup_bytype(struct net *net, u8 type,
David S. Miller062cdb42011-02-22 18:31:08 -0800915 const struct flowi *fl,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700916 u16 family, u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
James Morris134b0fc2006-10-05 15:42:27 -0500918 int err;
David S. Miller2518c7c2006-08-24 04:45:07 -0700919 struct xfrm_policy *pol, *ret;
David S. Miller0b597e72011-02-24 01:22:48 -0500920 const xfrm_address_t *daddr, *saddr;
David S. Miller2518c7c2006-08-24 04:45:07 -0700921 struct hlist_node *entry;
922 struct hlist_head *chain;
David S. Milleracba48e2006-08-25 15:46:46 -0700923 u32 priority = ~0U;
David S. Miller2518c7c2006-08-24 04:45:07 -0700924
925 daddr = xfrm_flowi_daddr(fl, family);
926 saddr = xfrm_flowi_saddr(fl, family);
927 if (unlikely(!daddr || !saddr))
928 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
930 read_lock_bh(&xfrm_policy_lock);
Alexey Dobriyan52479b62008-11-25 17:35:18 -0800931 chain = policy_hash_direct(net, daddr, saddr, family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700932 ret = NULL;
933 hlist_for_each_entry(pol, entry, chain, bydst) {
James Morris134b0fc2006-10-05 15:42:27 -0500934 err = xfrm_policy_match(pol, fl, type, family, dir);
935 if (err) {
936 if (err == -ESRCH)
937 continue;
938 else {
939 ret = ERR_PTR(err);
940 goto fail;
941 }
942 } else {
David S. Milleracba48e2006-08-25 15:46:46 -0700943 ret = pol;
944 priority = ret->priority;
945 break;
946 }
947 }
Alexey Dobriyan52479b62008-11-25 17:35:18 -0800948 chain = &net->xfrm.policy_inexact[dir];
David S. Milleracba48e2006-08-25 15:46:46 -0700949 hlist_for_each_entry(pol, entry, chain, bydst) {
James Morris134b0fc2006-10-05 15:42:27 -0500950 err = xfrm_policy_match(pol, fl, type, family, dir);
951 if (err) {
952 if (err == -ESRCH)
953 continue;
954 else {
955 ret = ERR_PTR(err);
956 goto fail;
957 }
958 } else if (pol->priority < priority) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700959 ret = pol;
960 break;
961 }
962 }
David S. Milleracba48e2006-08-25 15:46:46 -0700963 if (ret)
964 xfrm_pol_hold(ret);
James Morris134b0fc2006-10-05 15:42:27 -0500965fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 read_unlock_bh(&xfrm_policy_lock);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700967
David S. Miller2518c7c2006-08-24 04:45:07 -0700968 return ret;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700969}
970
Timo Teräs80c802f2010-04-07 00:30:05 +0000971static struct xfrm_policy *
David S. Miller73ff93c2011-02-22 18:33:42 -0800972__xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir)
Timo Teräs80c802f2010-04-07 00:30:05 +0000973{
974#ifdef CONFIG_XFRM_SUB_POLICY
975 struct xfrm_policy *pol;
976
977 pol = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_SUB, fl, family, dir);
978 if (pol != NULL)
979 return pol;
980#endif
981 return xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN, fl, family, dir);
982}
983
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000984static struct flow_cache_object *
David S. Millerdee9f4b2011-02-22 18:44:31 -0800985xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family,
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000986 u8 dir, struct flow_cache_object *old_obj, void *ctx)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700987{
988 struct xfrm_policy *pol;
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000989
990 if (old_obj)
991 xfrm_pol_put(container_of(old_obj, struct xfrm_policy, flo));
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700992
Timo Teräs80c802f2010-04-07 00:30:05 +0000993 pol = __xfrm_policy_lookup(net, fl, family, dir);
994 if (IS_ERR_OR_NULL(pol))
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000995 return ERR_CAST(pol);
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000996
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000997 /* Resolver returns two references:
998 * one for cache and one for caller of flow_cache_lookup() */
999 xfrm_pol_hold(pol);
1000
1001 return &pol->flo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002}
1003
Trent Jaegerdf718372005-12-13 23:12:27 -08001004static inline int policy_to_flow_dir(int dir)
1005{
1006 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001007 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1008 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1009 return dir;
1010 switch (dir) {
1011 default:
1012 case XFRM_POLICY_IN:
1013 return FLOW_DIR_IN;
1014 case XFRM_POLICY_OUT:
1015 return FLOW_DIR_OUT;
1016 case XFRM_POLICY_FWD:
1017 return FLOW_DIR_FWD;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001018 }
Trent Jaegerdf718372005-12-13 23:12:27 -08001019}
1020
David S. Millerdee9f4b2011-02-22 18:44:31 -08001021static struct xfrm_policy *xfrm_sk_policy_lookup(struct sock *sk, int dir,
1022 const struct flowi *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023{
1024 struct xfrm_policy *pol;
1025
1026 read_lock_bh(&xfrm_policy_lock);
1027 if ((pol = sk->sk_policy[dir]) != NULL) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04001028 bool match = xfrm_selector_match(&pol->selector, fl,
1029 sk->sk_family);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001030 int err = 0;
Trent Jaegerdf718372005-12-13 23:12:27 -08001031
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001032 if (match) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001033 if ((sk->sk_mark & pol->mark.m) != pol->mark.v) {
1034 pol = NULL;
1035 goto out;
1036 }
Paul Moore03e1ad72008-04-12 19:07:52 -07001037 err = security_xfrm_policy_lookup(pol->security,
David S. Miller1d28f422011-03-12 00:29:39 -05001038 fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -07001039 policy_to_flow_dir(dir));
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001040 if (!err)
1041 xfrm_pol_hold(pol);
1042 else if (err == -ESRCH)
1043 pol = NULL;
1044 else
1045 pol = ERR_PTR(err);
1046 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 pol = NULL;
1048 }
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001049out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 read_unlock_bh(&xfrm_policy_lock);
1051 return pol;
1052}
1053
1054static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
1055{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001056 struct net *net = xp_net(pol);
Alexey Dobriyan11219942008-11-25 17:33:06 -08001057 struct hlist_head *chain = policy_hash_bysel(net, &pol->selector,
David S. Miller2518c7c2006-08-24 04:45:07 -07001058 pol->family, dir);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001059
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001060 list_add(&pol->walk.all, &net->xfrm.policy_all);
David S. Miller2518c7c2006-08-24 04:45:07 -07001061 hlist_add_head(&pol->bydst, chain);
Alexey Dobriyane92303f2008-11-25 17:32:41 -08001062 hlist_add_head(&pol->byidx, net->xfrm.policy_byidx+idx_hash(net, pol->index));
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001063 net->xfrm.policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -07001065
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001066 if (xfrm_bydst_should_resize(net, dir, NULL))
1067 schedule_work(&net->xfrm.policy_hash_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068}
1069
1070static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
1071 int dir)
1072{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001073 struct net *net = xp_net(pol);
1074
David S. Miller2518c7c2006-08-24 04:45:07 -07001075 if (hlist_unhashed(&pol->bydst))
1076 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
David S. Miller2518c7c2006-08-24 04:45:07 -07001078 hlist_del(&pol->bydst);
1079 hlist_del(&pol->byidx);
Herbert Xu12a169e2008-10-01 07:03:24 -07001080 list_del(&pol->walk.all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001081 net->xfrm.policy_count[dir]--;
David S. Miller2518c7c2006-08-24 04:45:07 -07001082
1083 return pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084}
1085
Herbert Xu4666faa2005-06-18 22:43:22 -07001086int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087{
1088 write_lock_bh(&xfrm_policy_lock);
1089 pol = __xfrm_policy_unlink(pol, dir);
1090 write_unlock_bh(&xfrm_policy_lock);
1091 if (pol) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 xfrm_policy_kill(pol);
Herbert Xu4666faa2005-06-18 22:43:22 -07001093 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 }
Herbert Xu4666faa2005-06-18 22:43:22 -07001095 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096}
David S. Millera70fcb02006-03-20 19:18:52 -08001097EXPORT_SYMBOL(xfrm_policy_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
1099int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
1100{
Alexey Dobriyan11219942008-11-25 17:33:06 -08001101 struct net *net = xp_net(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 struct xfrm_policy *old_pol;
1103
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001104#ifdef CONFIG_XFRM_SUB_POLICY
1105 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
1106 return -EINVAL;
1107#endif
1108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 write_lock_bh(&xfrm_policy_lock);
1110 old_pol = sk->sk_policy[dir];
1111 sk->sk_policy[dir] = pol;
1112 if (pol) {
James Morris9d729f72007-03-04 16:12:44 -08001113 pol->curlft.add_time = get_seconds();
Alexey Dobriyan11219942008-11-25 17:33:06 -08001114 pol->index = xfrm_gen_index(net, XFRM_POLICY_MAX+dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 __xfrm_policy_link(pol, XFRM_POLICY_MAX+dir);
1116 }
1117 if (old_pol)
Timo Teräsea2dea92010-03-31 00:17:05 +00001118 /* Unlinking succeeds always. This is the only function
1119 * allowed to delete or replace socket policy.
1120 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 __xfrm_policy_unlink(old_pol, XFRM_POLICY_MAX+dir);
1122 write_unlock_bh(&xfrm_policy_lock);
1123
1124 if (old_pol) {
1125 xfrm_policy_kill(old_pol);
1126 }
1127 return 0;
1128}
1129
David S. Millerd3e40a92011-02-24 01:25:41 -05001130static struct xfrm_policy *clone_policy(const struct xfrm_policy *old, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131{
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -08001132 struct xfrm_policy *newp = xfrm_policy_alloc(xp_net(old), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
1134 if (newp) {
1135 newp->selector = old->selector;
Paul Moore03e1ad72008-04-12 19:07:52 -07001136 if (security_xfrm_policy_clone(old->security,
1137 &newp->security)) {
Trent Jaegerdf718372005-12-13 23:12:27 -08001138 kfree(newp);
1139 return NULL; /* ENOMEM */
1140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 newp->lft = old->lft;
1142 newp->curlft = old->curlft;
Jamal Hadi Salimfb977e22010-02-23 15:09:53 -08001143 newp->mark = old->mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 newp->action = old->action;
1145 newp->flags = old->flags;
1146 newp->xfrm_nr = old->xfrm_nr;
1147 newp->index = old->index;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001148 newp->type = old->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 memcpy(newp->xfrm_vec, old->xfrm_vec,
1150 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
1151 write_lock_bh(&xfrm_policy_lock);
1152 __xfrm_policy_link(newp, XFRM_POLICY_MAX+dir);
1153 write_unlock_bh(&xfrm_policy_lock);
1154 xfrm_pol_put(newp);
1155 }
1156 return newp;
1157}
1158
1159int __xfrm_sk_clone_policy(struct sock *sk)
1160{
1161 struct xfrm_policy *p0 = sk->sk_policy[0],
1162 *p1 = sk->sk_policy[1];
1163
1164 sk->sk_policy[0] = sk->sk_policy[1] = NULL;
1165 if (p0 && (sk->sk_policy[0] = clone_policy(p0, 0)) == NULL)
1166 return -ENOMEM;
1167 if (p1 && (sk->sk_policy[1] = clone_policy(p1, 1)) == NULL)
1168 return -ENOMEM;
1169 return 0;
1170}
1171
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001172static int
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001173xfrm_get_saddr(struct net *net, xfrm_address_t *local, xfrm_address_t *remote,
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001174 unsigned short family)
1175{
1176 int err;
1177 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1178
1179 if (unlikely(afinfo == NULL))
1180 return -EINVAL;
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001181 err = afinfo->get_saddr(net, local, remote);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001182 xfrm_policy_put_afinfo(afinfo);
1183 return err;
1184}
1185
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186/* Resolve list of templates for the flow, given policy. */
1187
1188static int
David S. Millera6c2e612011-02-22 18:35:39 -08001189xfrm_tmpl_resolve_one(struct xfrm_policy *policy, const struct flowi *fl,
1190 struct xfrm_state **xfrm, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191{
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001192 struct net *net = xp_net(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 int nx;
1194 int i, error;
1195 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
1196 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001197 xfrm_address_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
1199 for (nx=0, i = 0; i < policy->xfrm_nr; i++) {
1200 struct xfrm_state *x;
1201 xfrm_address_t *remote = daddr;
1202 xfrm_address_t *local = saddr;
1203 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
1204
Joakim Koskela48b8d782007-07-26 00:08:42 -07001205 if (tmpl->mode == XFRM_MODE_TUNNEL ||
1206 tmpl->mode == XFRM_MODE_BEET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 remote = &tmpl->id.daddr;
1208 local = &tmpl->saddr;
Thomas Egerer8444cf72010-09-20 11:11:38 -07001209 if (xfrm_addr_any(local, tmpl->encap_family)) {
1210 error = xfrm_get_saddr(net, &tmp, remote, tmpl->encap_family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001211 if (error)
1212 goto fail;
1213 local = &tmp;
1214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 }
1216
1217 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
1218
1219 if (x && x->km.state == XFRM_STATE_VALID) {
1220 xfrm[nx++] = x;
1221 daddr = remote;
1222 saddr = local;
1223 continue;
1224 }
1225 if (x) {
1226 error = (x->km.state == XFRM_STATE_ERROR ?
1227 -EINVAL : -EAGAIN);
1228 xfrm_state_put(x);
1229 }
fernando@oss.ntt.coa43222662008-10-23 04:27:19 +00001230 else if (error == -ESRCH)
1231 error = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
1233 if (!tmpl->optional)
1234 goto fail;
1235 }
1236 return nx;
1237
1238fail:
1239 for (nx--; nx>=0; nx--)
1240 xfrm_state_put(xfrm[nx]);
1241 return error;
1242}
1243
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001244static int
David S. Millera6c2e612011-02-22 18:35:39 -08001245xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, const struct flowi *fl,
1246 struct xfrm_state **xfrm, unsigned short family)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001247{
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001248 struct xfrm_state *tp[XFRM_MAX_DEPTH];
1249 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001250 int cnx = 0;
1251 int error;
1252 int ret;
1253 int i;
1254
1255 for (i = 0; i < npols; i++) {
1256 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
1257 error = -ENOBUFS;
1258 goto fail;
1259 }
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001260
1261 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001262 if (ret < 0) {
1263 error = ret;
1264 goto fail;
1265 } else
1266 cnx += ret;
1267 }
1268
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001269 /* found states are sorted for outbound processing */
1270 if (npols > 1)
1271 xfrm_state_sort(xfrm, tpp, cnx, family);
1272
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001273 return cnx;
1274
1275 fail:
1276 for (cnx--; cnx>=0; cnx--)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001277 xfrm_state_put(tpp[cnx]);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001278 return error;
1279
1280}
1281
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282/* Check that the bundle accepts the flow and its components are
1283 * still valid.
1284 */
1285
David S. Miller05d84022011-02-22 17:47:10 -08001286static inline int xfrm_get_tos(const struct flowi *fl, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08001287{
1288 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1289 int tos;
1290
1291 if (!afinfo)
1292 return -EINVAL;
1293
1294 tos = afinfo->get_tos(fl);
1295
1296 xfrm_policy_put_afinfo(afinfo);
1297
1298 return tos;
1299}
1300
Timo Teräs80c802f2010-04-07 00:30:05 +00001301static struct flow_cache_object *xfrm_bundle_flo_get(struct flow_cache_object *flo)
1302{
1303 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1304 struct dst_entry *dst = &xdst->u.dst;
1305
1306 if (xdst->route == NULL) {
1307 /* Dummy bundle - if it has xfrms we were not
1308 * able to build bundle as template resolution failed.
1309 * It means we need to try again resolving. */
1310 if (xdst->num_xfrms > 0)
1311 return NULL;
1312 } else {
1313 /* Real bundle */
1314 if (stale_bundle(dst))
1315 return NULL;
1316 }
1317
1318 dst_hold(dst);
1319 return flo;
1320}
1321
1322static int xfrm_bundle_flo_check(struct flow_cache_object *flo)
1323{
1324 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1325 struct dst_entry *dst = &xdst->u.dst;
1326
1327 if (!xdst->route)
1328 return 0;
1329 if (stale_bundle(dst))
1330 return 0;
1331
1332 return 1;
1333}
1334
1335static void xfrm_bundle_flo_delete(struct flow_cache_object *flo)
1336{
1337 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1338 struct dst_entry *dst = &xdst->u.dst;
1339
1340 dst_free(dst);
1341}
1342
1343static const struct flow_cache_ops xfrm_bundle_fc_ops = {
1344 .get = xfrm_bundle_flo_get,
1345 .check = xfrm_bundle_flo_check,
1346 .delete = xfrm_bundle_flo_delete,
1347};
1348
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001349static inline struct xfrm_dst *xfrm_alloc_dst(struct net *net, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08001350{
1351 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001352 struct dst_ops *dst_ops;
Herbert Xu25ee3282007-12-11 09:32:34 -08001353 struct xfrm_dst *xdst;
1354
1355 if (!afinfo)
1356 return ERR_PTR(-EINVAL);
1357
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001358 switch (family) {
1359 case AF_INET:
1360 dst_ops = &net->xfrm.xfrm4_dst_ops;
1361 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001362#if IS_ENABLED(CONFIG_IPV6)
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001363 case AF_INET6:
1364 dst_ops = &net->xfrm.xfrm6_dst_ops;
1365 break;
1366#endif
1367 default:
1368 BUG();
1369 }
David S. Millerf5b0a872012-07-19 12:31:33 -07001370 xdst = dst_alloc(dst_ops, NULL, 0, DST_OBSOLETE_NONE, 0);
Herbert Xu25ee3282007-12-11 09:32:34 -08001371
Madalin Bucurd4cae562011-09-26 07:04:36 +00001372 if (likely(xdst)) {
Steffen Klassert141e3692012-07-05 23:39:34 +00001373 struct dst_entry *dst = &xdst->u.dst;
1374
1375 memset(dst + 1, 0, sizeof(*xdst) - sizeof(*dst));
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08001376 xdst->flo.ops = &xfrm_bundle_fc_ops;
Patrick McHardy9d7b0fc2012-08-20 02:56:56 -07001377 if (afinfo->init_dst)
1378 afinfo->init_dst(net, xdst);
Madalin Bucurd4cae562011-09-26 07:04:36 +00001379 } else
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08001380 xdst = ERR_PTR(-ENOBUFS);
Timo Teräs80c802f2010-04-07 00:30:05 +00001381
Madalin Bucurd4cae562011-09-26 07:04:36 +00001382 xfrm_policy_put_afinfo(afinfo);
1383
Herbert Xu25ee3282007-12-11 09:32:34 -08001384 return xdst;
1385}
1386
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001387static inline int xfrm_init_path(struct xfrm_dst *path, struct dst_entry *dst,
1388 int nfheader_len)
1389{
1390 struct xfrm_policy_afinfo *afinfo =
1391 xfrm_policy_get_afinfo(dst->ops->family);
1392 int err;
1393
1394 if (!afinfo)
1395 return -EINVAL;
1396
1397 err = afinfo->init_path(path, dst, nfheader_len);
1398
1399 xfrm_policy_put_afinfo(afinfo);
1400
1401 return err;
1402}
1403
Herbert Xu87c1e122010-03-02 02:51:56 +00001404static inline int xfrm_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
David S. Miller0c7b3ee2011-02-22 17:48:57 -08001405 const struct flowi *fl)
Herbert Xu25ee3282007-12-11 09:32:34 -08001406{
1407 struct xfrm_policy_afinfo *afinfo =
1408 xfrm_policy_get_afinfo(xdst->u.dst.ops->family);
1409 int err;
1410
1411 if (!afinfo)
1412 return -EINVAL;
1413
Herbert Xu87c1e122010-03-02 02:51:56 +00001414 err = afinfo->fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001415
1416 xfrm_policy_put_afinfo(afinfo);
1417
1418 return err;
1419}
1420
Timo Teräs80c802f2010-04-07 00:30:05 +00001421
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422/* Allocate chain of dst_entry's, attach known xfrm's, calculate
1423 * all the metrics... Shortly, bundle a bundle.
1424 */
1425
Herbert Xu25ee3282007-12-11 09:32:34 -08001426static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy,
1427 struct xfrm_state **xfrm, int nx,
David S. Miller98313ad2011-02-22 18:36:50 -08001428 const struct flowi *fl,
Herbert Xu25ee3282007-12-11 09:32:34 -08001429 struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430{
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001431 struct net *net = xp_net(policy);
Herbert Xu25ee3282007-12-11 09:32:34 -08001432 unsigned long now = jiffies;
1433 struct net_device *dev;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001434 struct xfrm_mode *inner_mode;
Herbert Xu25ee3282007-12-11 09:32:34 -08001435 struct dst_entry *dst_prev = NULL;
1436 struct dst_entry *dst0 = NULL;
1437 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 int err;
Herbert Xu25ee3282007-12-11 09:32:34 -08001439 int header_len = 0;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001440 int nfheader_len = 0;
Herbert Xu25ee3282007-12-11 09:32:34 -08001441 int trailer_len = 0;
1442 int tos;
1443 int family = policy->selector.family;
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +09001444 xfrm_address_t saddr, daddr;
1445
1446 xfrm_flowi_addr_get(fl, &saddr, &daddr, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001447
1448 tos = xfrm_get_tos(fl, family);
1449 err = tos;
1450 if (tos < 0)
1451 goto put_states;
1452
1453 dst_hold(dst);
1454
1455 for (; i < nx; i++) {
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001456 struct xfrm_dst *xdst = xfrm_alloc_dst(net, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001457 struct dst_entry *dst1 = &xdst->u.dst;
1458
1459 err = PTR_ERR(xdst);
1460 if (IS_ERR(xdst)) {
1461 dst_release(dst);
1462 goto put_states;
1463 }
1464
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001465 if (xfrm[i]->sel.family == AF_UNSPEC) {
1466 inner_mode = xfrm_ip2inner_mode(xfrm[i],
1467 xfrm_af2proto(family));
1468 if (!inner_mode) {
1469 err = -EAFNOSUPPORT;
1470 dst_release(dst);
1471 goto put_states;
1472 }
1473 } else
1474 inner_mode = xfrm[i]->inner_mode;
1475
Herbert Xu25ee3282007-12-11 09:32:34 -08001476 if (!dst_prev)
1477 dst0 = dst1;
1478 else {
1479 dst_prev->child = dst_clone(dst1);
1480 dst1->flags |= DST_NOHASH;
1481 }
1482
1483 xdst->route = dst;
David S. Millerdefb3512010-12-08 21:16:57 -08001484 dst_copy_metrics(dst1, dst);
Herbert Xu25ee3282007-12-11 09:32:34 -08001485
1486 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
1487 family = xfrm[i]->props.family;
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +09001488 dst = xfrm_dst_lookup(xfrm[i], tos, &saddr, &daddr,
1489 family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001490 err = PTR_ERR(dst);
1491 if (IS_ERR(dst))
1492 goto put_states;
1493 } else
1494 dst_hold(dst);
1495
1496 dst1->xfrm = xfrm[i];
Timo Teräs80c802f2010-04-07 00:30:05 +00001497 xdst->xfrm_genid = xfrm[i]->genid;
Herbert Xu25ee3282007-12-11 09:32:34 -08001498
David S. Millerf5b0a872012-07-19 12:31:33 -07001499 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
Herbert Xu25ee3282007-12-11 09:32:34 -08001500 dst1->flags |= DST_HOST;
1501 dst1->lastuse = now;
1502
1503 dst1->input = dst_discard;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001504 dst1->output = inner_mode->afinfo->output;
Herbert Xu25ee3282007-12-11 09:32:34 -08001505
1506 dst1->next = dst_prev;
1507 dst_prev = dst1;
1508
1509 header_len += xfrm[i]->props.header_len;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001510 if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT)
1511 nfheader_len += xfrm[i]->props.header_len;
Herbert Xu25ee3282007-12-11 09:32:34 -08001512 trailer_len += xfrm[i]->props.trailer_len;
1513 }
1514
1515 dst_prev->child = dst;
1516 dst0->path = dst;
1517
1518 err = -ENODEV;
1519 dev = dst->dev;
1520 if (!dev)
1521 goto free_dst;
1522
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001523 xfrm_init_path((struct xfrm_dst *)dst0, dst, nfheader_len);
Herbert Xu25ee3282007-12-11 09:32:34 -08001524 xfrm_init_pmtu(dst_prev);
1525
1526 for (dst_prev = dst0; dst_prev != dst; dst_prev = dst_prev->child) {
1527 struct xfrm_dst *xdst = (struct xfrm_dst *)dst_prev;
1528
Herbert Xu87c1e122010-03-02 02:51:56 +00001529 err = xfrm_fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001530 if (err)
1531 goto free_dst;
1532
1533 dst_prev->header_len = header_len;
1534 dst_prev->trailer_len = trailer_len;
1535 header_len -= xdst->u.dst.xfrm->props.header_len;
1536 trailer_len -= xdst->u.dst.xfrm->props.trailer_len;
1537 }
1538
1539out:
1540 return dst0;
1541
1542put_states:
1543 for (; i < nx; i++)
1544 xfrm_state_put(xfrm[i]);
1545free_dst:
1546 if (dst0)
1547 dst_free(dst0);
1548 dst0 = ERR_PTR(err);
1549 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550}
1551
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001552static int inline
David S. Miller3f0e18f2011-02-22 18:38:14 -08001553xfrm_dst_alloc_copy(void **target, const void *src, int size)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001554{
1555 if (!*target) {
1556 *target = kmalloc(size, GFP_ATOMIC);
1557 if (!*target)
1558 return -ENOMEM;
1559 }
1560 memcpy(*target, src, size);
1561 return 0;
1562}
1563
1564static int inline
David S. Miller1786b382011-02-24 01:32:54 -05001565xfrm_dst_update_parent(struct dst_entry *dst, const struct xfrm_selector *sel)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001566{
1567#ifdef CONFIG_XFRM_SUB_POLICY
1568 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1569 return xfrm_dst_alloc_copy((void **)&(xdst->partner),
1570 sel, sizeof(*sel));
1571#else
1572 return 0;
1573#endif
1574}
1575
1576static int inline
David S. Miller3f0e18f2011-02-22 18:38:14 -08001577xfrm_dst_update_origin(struct dst_entry *dst, const struct flowi *fl)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001578{
1579#ifdef CONFIG_XFRM_SUB_POLICY
1580 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1581 return xfrm_dst_alloc_copy((void **)&(xdst->origin), fl, sizeof(*fl));
1582#else
1583 return 0;
1584#endif
1585}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
David S. Miller73ff93c2011-02-22 18:33:42 -08001587static int xfrm_expand_policies(const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001588 struct xfrm_policy **pols,
1589 int *num_pols, int *num_xfrms)
1590{
1591 int i;
1592
1593 if (*num_pols == 0 || !pols[0]) {
1594 *num_pols = 0;
1595 *num_xfrms = 0;
1596 return 0;
1597 }
1598 if (IS_ERR(pols[0]))
1599 return PTR_ERR(pols[0]);
1600
1601 *num_xfrms = pols[0]->xfrm_nr;
1602
1603#ifdef CONFIG_XFRM_SUB_POLICY
1604 if (pols[0] && pols[0]->action == XFRM_POLICY_ALLOW &&
1605 pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1606 pols[1] = xfrm_policy_lookup_bytype(xp_net(pols[0]),
1607 XFRM_POLICY_TYPE_MAIN,
1608 fl, family,
1609 XFRM_POLICY_OUT);
1610 if (pols[1]) {
1611 if (IS_ERR(pols[1])) {
1612 xfrm_pols_put(pols, *num_pols);
1613 return PTR_ERR(pols[1]);
1614 }
1615 (*num_pols) ++;
1616 (*num_xfrms) += pols[1]->xfrm_nr;
1617 }
1618 }
1619#endif
1620 for (i = 0; i < *num_pols; i++) {
1621 if (pols[i]->action != XFRM_POLICY_ALLOW) {
1622 *num_xfrms = -1;
1623 break;
1624 }
1625 }
1626
1627 return 0;
1628
1629}
1630
1631static struct xfrm_dst *
1632xfrm_resolve_and_create_bundle(struct xfrm_policy **pols, int num_pols,
David S. Miller4ca2e682011-02-22 18:38:51 -08001633 const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001634 struct dst_entry *dst_orig)
1635{
1636 struct net *net = xp_net(pols[0]);
1637 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
1638 struct dst_entry *dst;
1639 struct xfrm_dst *xdst;
1640 int err;
1641
1642 /* Try to instantiate a bundle */
1643 err = xfrm_tmpl_resolve(pols, num_pols, fl, xfrm, family);
Timo Teräsd809ec82010-07-12 21:29:42 +00001644 if (err <= 0) {
1645 if (err != 0 && err != -EAGAIN)
Timo Teräs80c802f2010-04-07 00:30:05 +00001646 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
1647 return ERR_PTR(err);
1648 }
1649
1650 dst = xfrm_bundle_create(pols[0], xfrm, err, fl, dst_orig);
1651 if (IS_ERR(dst)) {
1652 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLEGENERROR);
1653 return ERR_CAST(dst);
1654 }
1655
1656 xdst = (struct xfrm_dst *)dst;
1657 xdst->num_xfrms = err;
1658 if (num_pols > 1)
1659 err = xfrm_dst_update_parent(dst, &pols[1]->selector);
1660 else
1661 err = xfrm_dst_update_origin(dst, fl);
1662 if (unlikely(err)) {
1663 dst_free(dst);
1664 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLECHECKERROR);
1665 return ERR_PTR(err);
1666 }
1667
1668 xdst->num_pols = num_pols;
1669 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy*) * num_pols);
1670 xdst->policy_genid = atomic_read(&pols[0]->genid);
1671
1672 return xdst;
1673}
1674
1675static struct flow_cache_object *
David S. Millerdee9f4b2011-02-22 18:44:31 -08001676xfrm_bundle_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir,
Timo Teräs80c802f2010-04-07 00:30:05 +00001677 struct flow_cache_object *oldflo, void *ctx)
1678{
1679 struct dst_entry *dst_orig = (struct dst_entry *)ctx;
1680 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
1681 struct xfrm_dst *xdst, *new_xdst;
1682 int num_pols = 0, num_xfrms = 0, i, err, pol_dead;
1683
1684 /* Check if the policies from old bundle are usable */
1685 xdst = NULL;
1686 if (oldflo) {
1687 xdst = container_of(oldflo, struct xfrm_dst, flo);
1688 num_pols = xdst->num_pols;
1689 num_xfrms = xdst->num_xfrms;
1690 pol_dead = 0;
1691 for (i = 0; i < num_pols; i++) {
1692 pols[i] = xdst->pols[i];
1693 pol_dead |= pols[i]->walk.dead;
1694 }
1695 if (pol_dead) {
1696 dst_free(&xdst->u.dst);
1697 xdst = NULL;
1698 num_pols = 0;
1699 num_xfrms = 0;
1700 oldflo = NULL;
1701 }
1702 }
1703
1704 /* Resolve policies to use if we couldn't get them from
1705 * previous cache entry */
1706 if (xdst == NULL) {
1707 num_pols = 1;
1708 pols[0] = __xfrm_policy_lookup(net, fl, family, dir);
1709 err = xfrm_expand_policies(fl, family, pols,
1710 &num_pols, &num_xfrms);
1711 if (err < 0)
1712 goto inc_error;
1713 if (num_pols == 0)
1714 return NULL;
1715 if (num_xfrms <= 0)
1716 goto make_dummy_bundle;
1717 }
1718
1719 new_xdst = xfrm_resolve_and_create_bundle(pols, num_pols, fl, family, dst_orig);
1720 if (IS_ERR(new_xdst)) {
1721 err = PTR_ERR(new_xdst);
1722 if (err != -EAGAIN)
1723 goto error;
1724 if (oldflo == NULL)
1725 goto make_dummy_bundle;
1726 dst_hold(&xdst->u.dst);
1727 return oldflo;
Timo Teräsd809ec82010-07-12 21:29:42 +00001728 } else if (new_xdst == NULL) {
1729 num_xfrms = 0;
1730 if (oldflo == NULL)
1731 goto make_dummy_bundle;
1732 xdst->num_xfrms = 0;
1733 dst_hold(&xdst->u.dst);
1734 return oldflo;
Timo Teräs80c802f2010-04-07 00:30:05 +00001735 }
1736
1737 /* Kill the previous bundle */
1738 if (xdst) {
1739 /* The policies were stolen for newly generated bundle */
1740 xdst->num_pols = 0;
1741 dst_free(&xdst->u.dst);
1742 }
1743
1744 /* Flow cache does not have reference, it dst_free()'s,
1745 * but we do need to return one reference for original caller */
1746 dst_hold(&new_xdst->u.dst);
1747 return &new_xdst->flo;
1748
1749make_dummy_bundle:
1750 /* We found policies, but there's no bundles to instantiate:
1751 * either because the policy blocks, has no transformations or
1752 * we could not build template (no xfrm_states).*/
1753 xdst = xfrm_alloc_dst(net, family);
1754 if (IS_ERR(xdst)) {
1755 xfrm_pols_put(pols, num_pols);
1756 return ERR_CAST(xdst);
1757 }
1758 xdst->num_pols = num_pols;
1759 xdst->num_xfrms = num_xfrms;
1760 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy*) * num_pols);
1761
1762 dst_hold(&xdst->u.dst);
1763 return &xdst->flo;
1764
1765inc_error:
1766 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
1767error:
1768 if (xdst != NULL)
1769 dst_free(&xdst->u.dst);
1770 else
1771 xfrm_pols_put(pols, num_pols);
1772 return ERR_PTR(err);
1773}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
David S. Miller2774c132011-03-01 14:59:04 -08001775static struct dst_entry *make_blackhole(struct net *net, u16 family,
1776 struct dst_entry *dst_orig)
1777{
1778 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1779 struct dst_entry *ret;
1780
1781 if (!afinfo) {
1782 dst_release(dst_orig);
1783 ret = ERR_PTR(-EINVAL);
1784 } else {
1785 ret = afinfo->blackhole_route(net, dst_orig);
1786 }
1787 xfrm_policy_put_afinfo(afinfo);
1788
1789 return ret;
1790}
1791
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792/* Main function: finds/creates a bundle for given flow.
1793 *
1794 * At the moment we eat a raw IP route. Mostly to speed up lookups
1795 * on interfaces with disabled IPsec.
1796 */
David S. Miller452edd52011-03-02 13:27:41 -08001797struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
1798 const struct flowi *fl,
1799 struct sock *sk, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800{
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001801 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
Timo Teräs80c802f2010-04-07 00:30:05 +00001802 struct flow_cache_object *flo;
1803 struct xfrm_dst *xdst;
David S. Miller452edd52011-03-02 13:27:41 -08001804 struct dst_entry *dst, *route;
Timo Teräs80c802f2010-04-07 00:30:05 +00001805 u16 family = dst_orig->ops->family;
Trent Jaegerdf718372005-12-13 23:12:27 -08001806 u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
Changli Gao4b021622010-04-27 21:20:22 +00001807 int i, err, num_pols, num_xfrms = 0, drop_pols = 0;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07001808
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809restart:
Timo Teräs80c802f2010-04-07 00:30:05 +00001810 dst = NULL;
1811 xdst = NULL;
1812 route = NULL;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001813
Thomas Graff7944fb2007-08-25 13:46:55 -07001814 if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
Timo Teräs80c802f2010-04-07 00:30:05 +00001815 num_pols = 1;
1816 pols[0] = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
1817 err = xfrm_expand_policies(fl, family, pols,
1818 &num_pols, &num_xfrms);
1819 if (err < 0)
Herbert Xu75b8c132007-12-11 04:38:08 -08001820 goto dropdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00001821
1822 if (num_pols) {
1823 if (num_xfrms <= 0) {
1824 drop_pols = num_pols;
1825 goto no_transform;
1826 }
1827
1828 xdst = xfrm_resolve_and_create_bundle(
1829 pols, num_pols, fl,
1830 family, dst_orig);
1831 if (IS_ERR(xdst)) {
1832 xfrm_pols_put(pols, num_pols);
1833 err = PTR_ERR(xdst);
1834 goto dropdst;
Timo Teräsd809ec82010-07-12 21:29:42 +00001835 } else if (xdst == NULL) {
1836 num_xfrms = 0;
1837 drop_pols = num_pols;
1838 goto no_transform;
Timo Teräs80c802f2010-04-07 00:30:05 +00001839 }
1840
Steffen Klassertfbd50602011-03-15 21:12:49 +00001841 dst_hold(&xdst->u.dst);
1842
Timo Teräs80c802f2010-04-07 00:30:05 +00001843 spin_lock_bh(&xfrm_policy_sk_bundle_lock);
1844 xdst->u.dst.next = xfrm_policy_sk_bundles;
1845 xfrm_policy_sk_bundles = &xdst->u.dst;
1846 spin_unlock_bh(&xfrm_policy_sk_bundle_lock);
1847
1848 route = xdst->route;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08001849 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001850 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
Timo Teräs80c802f2010-04-07 00:30:05 +00001852 if (xdst == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 /* To accelerate a bit... */
David S. Miller2518c7c2006-08-24 04:45:07 -07001854 if ((dst_orig->flags & DST_NOXFRM) ||
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001855 !net->xfrm.policy_count[XFRM_POLICY_OUT])
Herbert Xu8b7817f2007-12-12 10:44:43 -08001856 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
Timo Teräs80c802f2010-04-07 00:30:05 +00001858 flo = flow_cache_lookup(net, fl, family, dir,
1859 xfrm_bundle_lookup, dst_orig);
1860 if (flo == NULL)
1861 goto nopol;
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001862 if (IS_ERR(flo)) {
Timo Teräs80c802f2010-04-07 00:30:05 +00001863 err = PTR_ERR(flo);
Herbert Xu75b8c132007-12-11 04:38:08 -08001864 goto dropdst;
Masahide NAKAMURAd66e37a2008-01-07 21:46:15 -08001865 }
Timo Teräs80c802f2010-04-07 00:30:05 +00001866 xdst = container_of(flo, struct xfrm_dst, flo);
1867
1868 num_pols = xdst->num_pols;
1869 num_xfrms = xdst->num_xfrms;
1870 memcpy(pols, xdst->pols, sizeof(struct xfrm_policy*) * num_pols);
1871 route = xdst->route;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 }
1873
Timo Teräs80c802f2010-04-07 00:30:05 +00001874 dst = &xdst->u.dst;
1875 if (route == NULL && num_xfrms > 0) {
1876 /* The only case when xfrm_bundle_lookup() returns a
1877 * bundle with null route, is when the template could
1878 * not be resolved. It means policies are there, but
1879 * bundle could not be created, since we don't yet
1880 * have the xfrm_state's. We need to wait for KM to
1881 * negotiate new SA's or bail out with error.*/
1882 if (net->xfrm.sysctl_larval_drop) {
1883 /* EREMOTE tells the caller to generate
1884 * a one-shot blackhole route. */
1885 dst_release(dst);
Timo Terasa1aa3482010-05-15 23:49:26 -07001886 xfrm_pols_put(pols, drop_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00001887 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
David S. Miller2774c132011-03-01 14:59:04 -08001888
David S. Miller452edd52011-03-02 13:27:41 -08001889 return make_blackhole(net, family, dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00001890 }
David S. Miller1d28f422011-03-12 00:29:39 -05001891 if (fl->flowi_flags & FLOWI_FLAG_CAN_SLEEP) {
Timo Teräs80c802f2010-04-07 00:30:05 +00001892 DECLARE_WAITQUEUE(wait, current);
1893
1894 add_wait_queue(&net->xfrm.km_waitq, &wait);
1895 set_current_state(TASK_INTERRUPTIBLE);
1896 schedule();
1897 set_current_state(TASK_RUNNING);
1898 remove_wait_queue(&net->xfrm.km_waitq, &wait);
1899
1900 if (!signal_pending(current)) {
1901 dst_release(dst);
1902 goto restart;
1903 }
1904
1905 err = -ERESTART;
1906 } else
1907 err = -EAGAIN;
1908
1909 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
1910 goto error;
1911 }
1912
1913no_transform:
1914 if (num_pols == 0)
Herbert Xu8b7817f2007-12-12 10:44:43 -08001915 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
Timo Teräs80c802f2010-04-07 00:30:05 +00001917 if ((flags & XFRM_LOOKUP_ICMP) &&
1918 !(pols[0]->flags & XFRM_POLICY_ICMP)) {
1919 err = -ENOENT;
Herbert Xu8b7817f2007-12-12 10:44:43 -08001920 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00001921 }
Herbert Xu8b7817f2007-12-12 10:44:43 -08001922
Timo Teräs80c802f2010-04-07 00:30:05 +00001923 for (i = 0; i < num_pols; i++)
1924 pols[i]->curlft.use_time = get_seconds();
Herbert Xu8b7817f2007-12-12 10:44:43 -08001925
Timo Teräs80c802f2010-04-07 00:30:05 +00001926 if (num_xfrms < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 /* Prohibit the flow */
Alexey Dobriyan59c99402008-11-25 17:59:52 -08001928 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLBLOCK);
Patrick McHardye1044112005-09-08 15:11:55 -07001929 err = -EPERM;
1930 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00001931 } else if (num_xfrms > 0) {
1932 /* Flow transformed */
Timo Teräs80c802f2010-04-07 00:30:05 +00001933 dst_release(dst_orig);
1934 } else {
1935 /* Flow passes untransformed */
1936 dst_release(dst);
David S. Miller452edd52011-03-02 13:27:41 -08001937 dst = dst_orig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 }
Timo Teräs80c802f2010-04-07 00:30:05 +00001939ok:
1940 xfrm_pols_put(pols, drop_pols);
Gao feng0c183372012-05-26 01:30:53 +00001941 if (dst && dst->xfrm &&
1942 dst->xfrm->props.mode == XFRM_MODE_TUNNEL)
1943 dst->flags |= DST_XFRM_TUNNEL;
David S. Miller452edd52011-03-02 13:27:41 -08001944 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
Timo Teräs80c802f2010-04-07 00:30:05 +00001946nopol:
David S. Miller452edd52011-03-02 13:27:41 -08001947 if (!(flags & XFRM_LOOKUP_ICMP)) {
1948 dst = dst_orig;
Timo Teräs80c802f2010-04-07 00:30:05 +00001949 goto ok;
David S. Miller452edd52011-03-02 13:27:41 -08001950 }
Timo Teräs80c802f2010-04-07 00:30:05 +00001951 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952error:
Timo Teräs80c802f2010-04-07 00:30:05 +00001953 dst_release(dst);
Herbert Xu75b8c132007-12-11 04:38:08 -08001954dropdst:
1955 dst_release(dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00001956 xfrm_pols_put(pols, drop_pols);
David S. Miller452edd52011-03-02 13:27:41 -08001957 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958}
1959EXPORT_SYMBOL(xfrm_lookup);
1960
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001961static inline int
David S. Miller8f029de2011-02-22 17:59:59 -08001962xfrm_secpath_reject(int idx, struct sk_buff *skb, const struct flowi *fl)
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001963{
1964 struct xfrm_state *x;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001965
1966 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
1967 return 0;
1968 x = skb->sp->xvec[idx];
1969 if (!x->type->reject)
1970 return 0;
Herbert Xu1ecafed2007-10-09 13:24:07 -07001971 return x->type->reject(x, skb, fl);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001972}
1973
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974/* When skb is transformed back to its "native" form, we have to
1975 * check policy restrictions. At the moment we make this in maximally
1976 * stupid way. Shame on me. :-) Of course, connected sockets must
1977 * have policy cached at them.
1978 */
1979
1980static inline int
David S. Miller7db454b2011-02-24 01:43:01 -05001981xfrm_state_ok(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 unsigned short family)
1983{
1984 if (xfrm_state_kern(x))
Kazunori MIYAZAWA928ba412007-02-13 12:57:16 -08001985 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 return x->id.proto == tmpl->id.proto &&
1987 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
1988 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
1989 x->props.mode == tmpl->mode &&
Herbert Xuc5d18e92008-04-22 00:46:42 -07001990 (tmpl->allalgs || (tmpl->aalgos & (1<<x->props.aalgo)) ||
Masahide NAKAMURAf3bd4842006-08-23 18:00:48 -07001991 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07001992 !(x->props.mode != XFRM_MODE_TRANSPORT &&
1993 xfrm_state_addr_cmp(tmpl, x, family));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994}
1995
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07001996/*
1997 * 0 or more than 0 is returned when validation is succeeded (either bypass
1998 * because of optional transport mode, or next index of the mathced secpath
1999 * state with the template.
2000 * -1 is returned when no matching template is found.
2001 * Otherwise "-2 - errored_index" is returned.
2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003static inline int
David S. Miller22cccb72011-02-24 01:43:33 -05002004xfrm_policy_ok(const struct xfrm_tmpl *tmpl, const struct sec_path *sp, int start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 unsigned short family)
2006{
2007 int idx = start;
2008
2009 if (tmpl->optional) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002010 if (tmpl->mode == XFRM_MODE_TRANSPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 return start;
2012 } else
2013 start = -1;
2014 for (; idx < sp->len; idx++) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002015 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 return ++idx;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002017 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
2018 if (start == -1)
2019 start = -2-idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 break;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002021 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 }
2023 return start;
2024}
2025
Herbert Xud5422ef2007-12-12 10:44:16 -08002026int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
2027 unsigned int family, int reverse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028{
2029 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002030 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031
2032 if (unlikely(afinfo == NULL))
2033 return -EAFNOSUPPORT;
2034
Herbert Xud5422ef2007-12-12 10:44:16 -08002035 afinfo->decode_session(skb, fl, reverse);
David S. Miller1d28f422011-03-12 00:29:39 -05002036 err = security_xfrm_decode_session(skb, &fl->flowi_secid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 xfrm_policy_put_afinfo(afinfo);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002038 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039}
Herbert Xud5422ef2007-12-12 10:44:16 -08002040EXPORT_SYMBOL(__xfrm_decode_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
David S. Miller9a7386e2011-02-24 01:44:12 -05002042static inline int secpath_has_nontransport(const struct sec_path *sp, int k, int *idxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043{
2044 for (; k < sp->len; k++) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002045 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002046 *idxp = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 return 1;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002048 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 }
2050
2051 return 0;
2052}
2053
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002054int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 unsigned short family)
2056{
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002057 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 struct xfrm_policy *pol;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002059 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2060 int npols = 0;
2061 int xfrm_nr;
2062 int pi;
Herbert Xud5422ef2007-12-12 10:44:16 -08002063 int reverse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 struct flowi fl;
Herbert Xud5422ef2007-12-12 10:44:16 -08002065 u8 fl_dir;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002066 int xerr_idx = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
Herbert Xud5422ef2007-12-12 10:44:16 -08002068 reverse = dir & ~XFRM_POLICY_MASK;
2069 dir &= XFRM_POLICY_MASK;
2070 fl_dir = policy_to_flow_dir(dir);
2071
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002072 if (__xfrm_decode_session(skb, &fl, family, reverse) < 0) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002073 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002075 }
2076
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -08002077 nf_nat_decode_session(skb, &fl, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
2079 /* First, check used SA against their selectors. */
2080 if (skb->sp) {
2081 int i;
2082
2083 for (i=skb->sp->len-1; i>=0; i--) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002084 struct xfrm_state *x = skb->sp->xvec[i];
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002085 if (!xfrm_selector_match(&x->sel, &fl, family)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002086 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002088 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 }
2090 }
2091
2092 pol = NULL;
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002093 if (sk && sk->sk_policy[dir]) {
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002094 pol = xfrm_sk_policy_lookup(sk, dir, &fl);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002095 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002096 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002097 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002098 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002099 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
Timo Teräsfe1a5f02010-04-07 00:30:04 +00002101 if (!pol) {
2102 struct flow_cache_object *flo;
2103
2104 flo = flow_cache_lookup(net, &fl, family, fl_dir,
2105 xfrm_policy_lookup, NULL);
2106 if (IS_ERR_OR_NULL(flo))
2107 pol = ERR_CAST(flo);
2108 else
2109 pol = container_of(flo, struct xfrm_policy, flo);
2110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002112 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002113 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002114 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002115 }
James Morris134b0fc2006-10-05 15:42:27 -05002116
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002117 if (!pol) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002118 if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002119 xfrm_secpath_reject(xerr_idx, skb, &fl);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002120 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOPOLS);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002121 return 0;
2122 }
2123 return 1;
2124 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
James Morris9d729f72007-03-04 16:12:44 -08002126 pol->curlft.use_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002128 pols[0] = pol;
2129 npols ++;
2130#ifdef CONFIG_XFRM_SUB_POLICY
2131 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002132 pols[1] = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002133 &fl, family,
2134 XFRM_POLICY_IN);
2135 if (pols[1]) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002136 if (IS_ERR(pols[1])) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002137 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002138 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002139 }
James Morris9d729f72007-03-04 16:12:44 -08002140 pols[1]->curlft.use_time = get_seconds();
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002141 npols ++;
2142 }
2143 }
2144#endif
2145
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 if (pol->action == XFRM_POLICY_ALLOW) {
2147 struct sec_path *sp;
2148 static struct sec_path dummy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002149 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002150 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002151 struct xfrm_tmpl **tpp = tp;
2152 int ti = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 int i, k;
2154
2155 if ((sp = skb->sp) == NULL)
2156 sp = &dummy;
2157
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002158 for (pi = 0; pi < npols; pi++) {
2159 if (pols[pi] != pol &&
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002160 pols[pi]->action != XFRM_POLICY_ALLOW) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002161 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002162 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002163 }
2164 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002165 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002166 goto reject_error;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002167 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002168 for (i = 0; i < pols[pi]->xfrm_nr; i++)
2169 tpp[ti++] = &pols[pi]->xfrm_vec[i];
2170 }
2171 xfrm_nr = ti;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002172 if (npols > 1) {
2173 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family);
2174 tpp = stp;
2175 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002176
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 /* For each tunnel xfrm, find the first matching tmpl.
2178 * For each tmpl before that, find corresponding xfrm.
2179 * Order is _important_. Later we will implement
2180 * some barriers, but at the moment barriers
2181 * are implied between each two transformations.
2182 */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002183 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
2184 k = xfrm_policy_ok(tpp[i], sp, k, family);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002185 if (k < 0) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002186 if (k < -1)
2187 /* "-2 - errored_index" returned */
2188 xerr_idx = -(2+k);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002189 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 goto reject;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 }
2193
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002194 if (secpath_has_nontransport(sp, k, &xerr_idx)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002195 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002199 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 return 1;
2201 }
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002202 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
2204reject:
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002205 xfrm_secpath_reject(xerr_idx, skb, &fl);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002206reject_error:
2207 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 return 0;
2209}
2210EXPORT_SYMBOL(__xfrm_policy_check);
2211
2212int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
2213{
Alexey Dobriyan99a66652008-11-25 17:36:13 -08002214 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 struct flowi fl;
Eric Dumazetadf30902009-06-02 05:19:30 +00002216 struct dst_entry *dst;
Eric Dumazet73137142011-03-15 15:26:43 -07002217 int res = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002219 if (xfrm_decode_session(skb, &fl, family) < 0) {
jamal72032fd2010-02-18 03:35:07 +00002220 XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223
Eric Dumazetfafeeb62010-06-01 10:04:49 +00002224 skb_dst_force(skb);
Eric Dumazetadf30902009-06-02 05:19:30 +00002225
David S. Miller452edd52011-03-02 13:27:41 -08002226 dst = xfrm_lookup(net, skb_dst(skb), &fl, NULL, 0);
2227 if (IS_ERR(dst)) {
Eric Dumazet73137142011-03-15 15:26:43 -07002228 res = 0;
David S. Miller452edd52011-03-02 13:27:41 -08002229 dst = NULL;
2230 }
Eric Dumazetadf30902009-06-02 05:19:30 +00002231 skb_dst_set(skb, dst);
2232 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233}
2234EXPORT_SYMBOL(__xfrm_route_forward);
2235
David S. Millerd49c73c2006-08-13 18:55:53 -07002236/* Optimize later using cookies and generation ids. */
2237
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
2239{
David S. Millerd49c73c2006-08-13 18:55:53 -07002240 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
David S. Millerf5b0a872012-07-19 12:31:33 -07002241 * to DST_OBSOLETE_FORCE_CHK to force all XFRM destinations to
2242 * get validated by dst_ops->check on every use. We do this
2243 * because when a normal route referenced by an XFRM dst is
2244 * obsoleted we do not go looking around for all parent
2245 * referencing XFRM dsts so that we can invalidate them. It
2246 * is just too much work. Instead we make the checks here on
2247 * every use. For example:
David S. Millerd49c73c2006-08-13 18:55:53 -07002248 *
2249 * XFRM dst A --> IPv4 dst X
2250 *
2251 * X is the "xdst->route" of A (X is also the "dst->path" of A
2252 * in this example). If X is marked obsolete, "A" will not
2253 * notice. That's what we are validating here via the
2254 * stale_bundle() check.
2255 *
2256 * When a policy's bundle is pruned, we dst_free() the XFRM
David S. Millerf5b0a872012-07-19 12:31:33 -07002257 * dst which causes it's ->obsolete field to be set to
2258 * DST_OBSOLETE_DEAD. If an XFRM dst has been pruned like
2259 * this, we want to force a new route lookup.
David S. Miller399c1802005-12-19 14:23:23 -08002260 */
David S. Millerd49c73c2006-08-13 18:55:53 -07002261 if (dst->obsolete < 0 && !stale_bundle(dst))
2262 return dst;
2263
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 return NULL;
2265}
2266
2267static int stale_bundle(struct dst_entry *dst)
2268{
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002269 return !xfrm_bundle_ok((struct xfrm_dst *)dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270}
2271
Herbert Xuaabc9762005-05-03 16:27:10 -07002272void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002275 dst->dev = dev_net(dev)->loopback_dev;
Daniel Lezcanode3cb742007-09-25 19:16:28 -07002276 dev_hold(dst->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 dev_put(dev);
2278 }
2279}
Herbert Xuaabc9762005-05-03 16:27:10 -07002280EXPORT_SYMBOL(xfrm_dst_ifdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281
2282static void xfrm_link_failure(struct sk_buff *skb)
2283{
2284 /* Impossible. Such dst must be popped before reaches point of failure. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285}
2286
2287static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
2288{
2289 if (dst) {
2290 if (dst->obsolete) {
2291 dst_release(dst);
2292 dst = NULL;
2293 }
2294 }
2295 return dst;
2296}
2297
Alexey Dobriyanddcfd792008-11-25 17:37:23 -08002298static void __xfrm_garbage_collect(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299{
Timo Teräs80c802f2010-04-07 00:30:05 +00002300 struct dst_entry *head, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301
Timo Teräs80c802f2010-04-07 00:30:05 +00002302 spin_lock_bh(&xfrm_policy_sk_bundle_lock);
2303 head = xfrm_policy_sk_bundles;
2304 xfrm_policy_sk_bundles = NULL;
2305 spin_unlock_bh(&xfrm_policy_sk_bundle_lock);
2306
2307 while (head) {
2308 next = head->next;
2309 dst_free(head);
2310 head = next;
2311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312}
2313
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002314static void xfrm_garbage_collect(struct net *net)
2315{
2316 flow_cache_flush();
2317 __xfrm_garbage_collect(net);
2318}
2319
2320static void xfrm_garbage_collect_deferred(struct net *net)
2321{
2322 flow_cache_flush_deferred();
2323 __xfrm_garbage_collect(net);
2324}
2325
Herbert Xu25ee3282007-12-11 09:32:34 -08002326static void xfrm_init_pmtu(struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327{
2328 do {
2329 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2330 u32 pmtu, route_mtu_cached;
2331
2332 pmtu = dst_mtu(dst->child);
2333 xdst->child_mtu_cached = pmtu;
2334
2335 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
2336
2337 route_mtu_cached = dst_mtu(xdst->route);
2338 xdst->route_mtu_cached = route_mtu_cached;
2339
2340 if (pmtu > route_mtu_cached)
2341 pmtu = route_mtu_cached;
2342
David S. Millerdefb3512010-12-08 21:16:57 -08002343 dst_metric_set(dst, RTAX_MTU, pmtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 } while ((dst = dst->next));
2345}
2346
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347/* Check that the bundle accepts the flow and its components are
2348 * still valid.
2349 */
2350
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002351static int xfrm_bundle_ok(struct xfrm_dst *first)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352{
2353 struct dst_entry *dst = &first->u.dst;
2354 struct xfrm_dst *last;
2355 u32 mtu;
2356
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07002357 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 (dst->dev && !netif_running(dst->dev)))
2359 return 0;
2360
2361 last = NULL;
2362
2363 do {
2364 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2365
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 if (dst->xfrm->km.state != XFRM_STATE_VALID)
2367 return 0;
Timo Teräs80c802f2010-04-07 00:30:05 +00002368 if (xdst->xfrm_genid != dst->xfrm->genid)
2369 return 0;
Timo Teräsb1312c82010-06-24 14:35:00 -07002370 if (xdst->num_pols > 0 &&
2371 xdst->policy_genid != atomic_read(&xdst->pols[0]->genid))
David S. Miller9d4a7062006-08-24 03:18:09 -07002372 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373
2374 mtu = dst_mtu(dst->child);
2375 if (xdst->child_mtu_cached != mtu) {
2376 last = xdst;
2377 xdst->child_mtu_cached = mtu;
2378 }
2379
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07002380 if (!dst_check(xdst->route, xdst->route_cookie))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 return 0;
2382 mtu = dst_mtu(xdst->route);
2383 if (xdst->route_mtu_cached != mtu) {
2384 last = xdst;
2385 xdst->route_mtu_cached = mtu;
2386 }
2387
2388 dst = dst->child;
2389 } while (dst->xfrm);
2390
2391 if (likely(!last))
2392 return 1;
2393
2394 mtu = last->child_mtu_cached;
2395 for (;;) {
2396 dst = &last->u.dst;
2397
2398 mtu = xfrm_state_mtu(dst->xfrm, mtu);
2399 if (mtu > last->route_mtu_cached)
2400 mtu = last->route_mtu_cached;
David S. Millerdefb3512010-12-08 21:16:57 -08002401 dst_metric_set(dst, RTAX_MTU, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402
2403 if (last == first)
2404 break;
2405
Patrick McHardybd0bf072007-07-18 01:55:52 -07002406 last = (struct xfrm_dst *)last->u.dst.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 last->child_mtu_cached = mtu;
2408 }
2409
2410 return 1;
2411}
2412
David S. Miller0dbaee32010-12-13 12:52:14 -08002413static unsigned int xfrm_default_advmss(const struct dst_entry *dst)
2414{
2415 return dst_metric_advmss(dst->path);
2416}
2417
Steffen Klassertebb762f2011-11-23 02:12:51 +00002418static unsigned int xfrm_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08002419{
Steffen Klassert618f9bc2011-11-23 02:13:31 +00002420 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
2421
2422 return mtu ? : dst_mtu(dst->path);
David S. Millerd33e4552010-12-14 13:01:14 -08002423}
2424
David S. Millerf894cbf2012-07-02 21:52:24 -07002425static struct neighbour *xfrm_neigh_lookup(const struct dst_entry *dst,
2426 struct sk_buff *skb,
2427 const void *daddr)
David S. Millerd3aaeb32011-07-18 00:40:17 -07002428{
David S. Millerf894cbf2012-07-02 21:52:24 -07002429 return dst->path->ops->neigh_lookup(dst, skb, daddr);
David S. Millerd3aaeb32011-07-18 00:40:17 -07002430}
2431
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
2433{
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002434 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 int err = 0;
2436 if (unlikely(afinfo == NULL))
2437 return -EINVAL;
2438 if (unlikely(afinfo->family >= NPROTO))
2439 return -EAFNOSUPPORT;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002440 spin_lock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
2442 err = -ENOBUFS;
2443 else {
2444 struct dst_ops *dst_ops = afinfo->dst_ops;
2445 if (likely(dst_ops->kmem_cachep == NULL))
2446 dst_ops->kmem_cachep = xfrm_dst_cache;
2447 if (likely(dst_ops->check == NULL))
2448 dst_ops->check = xfrm_dst_check;
David S. Miller0dbaee32010-12-13 12:52:14 -08002449 if (likely(dst_ops->default_advmss == NULL))
2450 dst_ops->default_advmss = xfrm_default_advmss;
Steffen Klassertebb762f2011-11-23 02:12:51 +00002451 if (likely(dst_ops->mtu == NULL))
2452 dst_ops->mtu = xfrm_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 if (likely(dst_ops->negative_advice == NULL))
2454 dst_ops->negative_advice = xfrm_negative_advice;
2455 if (likely(dst_ops->link_failure == NULL))
2456 dst_ops->link_failure = xfrm_link_failure;
David S. Millerd3aaeb32011-07-18 00:40:17 -07002457 if (likely(dst_ops->neigh_lookup == NULL))
2458 dst_ops->neigh_lookup = xfrm_neigh_lookup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 if (likely(afinfo->garbage_collect == NULL))
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002460 afinfo->garbage_collect = xfrm_garbage_collect_deferred;
Priyanka Jain418a99a2012-08-12 21:22:29 +00002461 rcu_assign_pointer(xfrm_policy_afinfo[afinfo->family], afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02002463 spin_unlock(&xfrm_policy_afinfo_lock);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002464
2465 rtnl_lock();
2466 for_each_net(net) {
2467 struct dst_ops *xfrm_dst_ops;
2468
2469 switch (afinfo->family) {
2470 case AF_INET:
2471 xfrm_dst_ops = &net->xfrm.xfrm4_dst_ops;
2472 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002473#if IS_ENABLED(CONFIG_IPV6)
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002474 case AF_INET6:
2475 xfrm_dst_ops = &net->xfrm.xfrm6_dst_ops;
2476 break;
2477#endif
2478 default:
2479 BUG();
2480 }
2481 *xfrm_dst_ops = *afinfo->dst_ops;
2482 }
2483 rtnl_unlock();
2484
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 return err;
2486}
2487EXPORT_SYMBOL(xfrm_policy_register_afinfo);
2488
2489int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
2490{
2491 int err = 0;
2492 if (unlikely(afinfo == NULL))
2493 return -EINVAL;
2494 if (unlikely(afinfo->family >= NPROTO))
2495 return -EAFNOSUPPORT;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002496 spin_lock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
2498 if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
2499 err = -EINVAL;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002500 else
2501 RCU_INIT_POINTER(xfrm_policy_afinfo[afinfo->family],
2502 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02002504 spin_unlock(&xfrm_policy_afinfo_lock);
2505 if (!err) {
2506 struct dst_ops *dst_ops = afinfo->dst_ops;
2507
2508 synchronize_rcu();
2509
2510 dst_ops->kmem_cachep = NULL;
2511 dst_ops->check = NULL;
2512 dst_ops->negative_advice = NULL;
2513 dst_ops->link_failure = NULL;
2514 afinfo->garbage_collect = NULL;
2515 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 return err;
2517}
2518EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
2519
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002520static void __net_init xfrm_dst_ops_init(struct net *net)
2521{
2522 struct xfrm_policy_afinfo *afinfo;
2523
Eric Dumazetef8531b2012-08-19 12:31:48 +02002524 rcu_read_lock();
2525 afinfo = rcu_dereference(xfrm_policy_afinfo[AF_INET]);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002526 if (afinfo)
2527 net->xfrm.xfrm4_dst_ops = *afinfo->dst_ops;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002528#if IS_ENABLED(CONFIG_IPV6)
Eric Dumazetef8531b2012-08-19 12:31:48 +02002529 afinfo = rcu_dereference(xfrm_policy_afinfo[AF_INET6]);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002530 if (afinfo)
2531 net->xfrm.xfrm6_dst_ops = *afinfo->dst_ops;
2532#endif
Priyanka Jain418a99a2012-08-12 21:22:29 +00002533 rcu_read_unlock();
Herbert Xu546be242006-05-27 23:03:58 -07002534}
2535
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
2537{
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02002538 struct net_device *dev = ptr;
2539
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 switch (event) {
2541 case NETDEV_DOWN:
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002542 xfrm_garbage_collect(dev_net(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 }
2544 return NOTIFY_DONE;
2545}
2546
2547static struct notifier_block xfrm_dev_notifier = {
Alexey Dobriyand5917a32008-10-31 00:41:59 -07002548 .notifier_call = xfrm_dev_event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549};
2550
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002551#ifdef CONFIG_XFRM_STATISTICS
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002552static int __net_init xfrm_statistics_init(struct net *net)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002553{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002554 int rv;
2555
Tejun Heo7d720c32010-02-16 15:20:26 +00002556 if (snmp_mib_init((void __percpu **)net->mib.xfrm_statistics,
Eric Dumazet1823e4c82010-06-22 20:58:41 +00002557 sizeof(struct linux_xfrm_mib),
2558 __alignof__(struct linux_xfrm_mib)) < 0)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002559 return -ENOMEM;
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002560 rv = xfrm_proc_init(net);
2561 if (rv < 0)
Tejun Heo7d720c32010-02-16 15:20:26 +00002562 snmp_mib_free((void __percpu **)net->mib.xfrm_statistics);
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002563 return rv;
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002564}
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002565
2566static void xfrm_statistics_fini(struct net *net)
2567{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002568 xfrm_proc_fini(net);
Tejun Heo7d720c32010-02-16 15:20:26 +00002569 snmp_mib_free((void __percpu **)net->mib.xfrm_statistics);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002570}
2571#else
2572static int __net_init xfrm_statistics_init(struct net *net)
2573{
2574 return 0;
2575}
2576
2577static void xfrm_statistics_fini(struct net *net)
2578{
2579}
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002580#endif
2581
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002582static int __net_init xfrm_policy_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583{
David S. Miller2518c7c2006-08-24 04:45:07 -07002584 unsigned int hmask, sz;
2585 int dir;
2586
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002587 if (net_eq(net, &init_net))
2588 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 sizeof(struct xfrm_dst),
Alexey Dobriyane5d679f2006-08-26 19:25:52 -07002590 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002591 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592
David S. Miller2518c7c2006-08-24 04:45:07 -07002593 hmask = 8 - 1;
2594 sz = (hmask+1) * sizeof(struct hlist_head);
2595
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002596 net->xfrm.policy_byidx = xfrm_hash_alloc(sz);
2597 if (!net->xfrm.policy_byidx)
2598 goto out_byidx;
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08002599 net->xfrm.policy_idx_hmask = hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -07002600
2601 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
2602 struct xfrm_policy_hash *htab;
2603
Alexey Dobriyandc2caba2008-11-25 17:24:15 -08002604 net->xfrm.policy_count[dir] = 0;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002605 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
David S. Miller2518c7c2006-08-24 04:45:07 -07002606
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002607 htab = &net->xfrm.policy_bydst[dir];
David S. Miller44e36b42006-08-24 04:50:50 -07002608 htab->table = xfrm_hash_alloc(sz);
David S. Miller2518c7c2006-08-24 04:45:07 -07002609 if (!htab->table)
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002610 goto out_bydst;
2611 htab->hmask = hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -07002612 }
2613
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08002614 INIT_LIST_HEAD(&net->xfrm.policy_all);
Alexey Dobriyan66caf622008-11-25 17:28:57 -08002615 INIT_WORK(&net->xfrm.policy_hash_work, xfrm_hash_resize);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002616 if (net_eq(net, &init_net))
2617 register_netdevice_notifier(&xfrm_dev_notifier);
2618 return 0;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002619
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002620out_bydst:
2621 for (dir--; dir >= 0; dir--) {
2622 struct xfrm_policy_hash *htab;
2623
2624 htab = &net->xfrm.policy_bydst[dir];
2625 xfrm_hash_free(htab->table, sz);
2626 }
2627 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002628out_byidx:
2629 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630}
2631
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002632static void xfrm_policy_fini(struct net *net)
2633{
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002634 struct xfrm_audit audit_info;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002635 unsigned int sz;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002636 int dir;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002637
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002638 flush_work(&net->xfrm.policy_hash_work);
2639#ifdef CONFIG_XFRM_SUB_POLICY
2640 audit_info.loginuid = -1;
2641 audit_info.sessionid = -1;
2642 audit_info.secid = 0;
2643 xfrm_policy_flush(net, XFRM_POLICY_TYPE_SUB, &audit_info);
2644#endif
2645 audit_info.loginuid = -1;
2646 audit_info.sessionid = -1;
2647 audit_info.secid = 0;
2648 xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, &audit_info);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002649
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08002650 WARN_ON(!list_empty(&net->xfrm.policy_all));
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002651
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002652 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002653 struct xfrm_policy_hash *htab;
2654
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002655 WARN_ON(!hlist_empty(&net->xfrm.policy_inexact[dir]));
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002656
2657 htab = &net->xfrm.policy_bydst[dir];
2658 sz = (htab->hmask + 1);
2659 WARN_ON(!hlist_empty(htab->table));
2660 xfrm_hash_free(htab->table, sz);
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002661 }
2662
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08002663 sz = (net->xfrm.policy_idx_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002664 WARN_ON(!hlist_empty(net->xfrm.policy_byidx));
2665 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002666}
2667
2668static int __net_init xfrm_net_init(struct net *net)
2669{
2670 int rv;
2671
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002672 rv = xfrm_statistics_init(net);
2673 if (rv < 0)
2674 goto out_statistics;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002675 rv = xfrm_state_init(net);
2676 if (rv < 0)
2677 goto out_state;
2678 rv = xfrm_policy_init(net);
2679 if (rv < 0)
2680 goto out_policy;
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002681 xfrm_dst_ops_init(net);
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08002682 rv = xfrm_sysctl_init(net);
2683 if (rv < 0)
2684 goto out_sysctl;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002685 return 0;
2686
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08002687out_sysctl:
2688 xfrm_policy_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002689out_policy:
2690 xfrm_state_fini(net);
2691out_state:
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002692 xfrm_statistics_fini(net);
2693out_statistics:
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002694 return rv;
2695}
2696
2697static void __net_exit xfrm_net_exit(struct net *net)
2698{
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08002699 xfrm_sysctl_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002700 xfrm_policy_fini(net);
2701 xfrm_state_fini(net);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002702 xfrm_statistics_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002703}
2704
2705static struct pernet_operations __net_initdata xfrm_net_ops = {
2706 .init = xfrm_net_init,
2707 .exit = xfrm_net_exit,
2708};
2709
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710void __init xfrm_init(void)
2711{
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002712 register_pernet_subsys(&xfrm_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 xfrm_input_init();
2714}
2715
Joy Lattenab5f5e82007-09-17 11:51:22 -07002716#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinen1486cbd2008-01-12 03:20:03 -08002717static void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
2718 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002719{
Paul Moore875179f2007-12-01 23:27:18 +11002720 struct xfrm_sec_ctx *ctx = xp->security;
2721 struct xfrm_selector *sel = &xp->selector;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002722
Paul Moore875179f2007-12-01 23:27:18 +11002723 if (ctx)
2724 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
2725 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
2726
2727 switch(sel->family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07002728 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07002729 audit_log_format(audit_buf, " src=%pI4", &sel->saddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11002730 if (sel->prefixlen_s != 32)
2731 audit_log_format(audit_buf, " src_prefixlen=%d",
2732 sel->prefixlen_s);
Harvey Harrison21454aa2008-10-31 00:54:56 -07002733 audit_log_format(audit_buf, " dst=%pI4", &sel->daddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11002734 if (sel->prefixlen_d != 32)
2735 audit_log_format(audit_buf, " dst_prefixlen=%d",
2736 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002737 break;
2738 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002739 audit_log_format(audit_buf, " src=%pI6", sel->saddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11002740 if (sel->prefixlen_s != 128)
2741 audit_log_format(audit_buf, " src_prefixlen=%d",
2742 sel->prefixlen_s);
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002743 audit_log_format(audit_buf, " dst=%pI6", sel->daddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11002744 if (sel->prefixlen_d != 128)
2745 audit_log_format(audit_buf, " dst_prefixlen=%d",
2746 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002747 break;
2748 }
2749}
2750
Paul Moore68277ac2007-12-20 20:49:33 -08002751void xfrm_audit_policy_add(struct xfrm_policy *xp, int result,
Eric Paris25323862008-04-18 10:09:25 -04002752 uid_t auid, u32 sessionid, u32 secid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002753{
2754 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002755
Paul Mooreafeb14b2007-12-21 14:58:11 -08002756 audit_buf = xfrm_audit_start("SPD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002757 if (audit_buf == NULL)
2758 return;
Eric Paris25323862008-04-18 10:09:25 -04002759 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002760 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002761 xfrm_audit_common_policyinfo(xp, audit_buf);
2762 audit_log_end(audit_buf);
2763}
2764EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
2765
Paul Moore68277ac2007-12-20 20:49:33 -08002766void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
Eric Paris25323862008-04-18 10:09:25 -04002767 uid_t auid, u32 sessionid, u32 secid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002768{
2769 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002770
Paul Mooreafeb14b2007-12-21 14:58:11 -08002771 audit_buf = xfrm_audit_start("SPD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002772 if (audit_buf == NULL)
2773 return;
Eric Paris25323862008-04-18 10:09:25 -04002774 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002775 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002776 xfrm_audit_common_policyinfo(xp, audit_buf);
2777 audit_log_end(audit_buf);
2778}
2779EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
2780#endif
2781
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002782#ifdef CONFIG_XFRM_MIGRATE
David S. Millerbc9b35a2012-05-15 15:04:57 -04002783static bool xfrm_migrate_selector_match(const struct xfrm_selector *sel_cmp,
2784 const struct xfrm_selector *sel_tgt)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002785{
2786 if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
2787 if (sel_tgt->family == sel_cmp->family &&
2788 xfrm_addr_cmp(&sel_tgt->daddr, &sel_cmp->daddr,
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002789 sel_cmp->family) == 0 &&
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002790 xfrm_addr_cmp(&sel_tgt->saddr, &sel_cmp->saddr,
2791 sel_cmp->family) == 0 &&
2792 sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
2793 sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04002794 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002795 }
2796 } else {
2797 if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04002798 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002799 }
2800 }
David S. Millerbc9b35a2012-05-15 15:04:57 -04002801 return false;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002802}
2803
David S. Millerb4b7c0b2011-02-24 00:35:06 -05002804static struct xfrm_policy * xfrm_migrate_policy_find(const struct xfrm_selector *sel,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002805 u8 dir, u8 type)
2806{
2807 struct xfrm_policy *pol, *ret = NULL;
2808 struct hlist_node *entry;
2809 struct hlist_head *chain;
2810 u32 priority = ~0U;
2811
2812 read_lock_bh(&xfrm_policy_lock);
Alexey Dobriyan11219942008-11-25 17:33:06 -08002813 chain = policy_hash_direct(&init_net, &sel->daddr, &sel->saddr, sel->family, dir);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002814 hlist_for_each_entry(pol, entry, chain, bydst) {
2815 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
2816 pol->type == type) {
2817 ret = pol;
2818 priority = ret->priority;
2819 break;
2820 }
2821 }
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002822 chain = &init_net.xfrm.policy_inexact[dir];
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002823 hlist_for_each_entry(pol, entry, chain, bydst) {
2824 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
2825 pol->type == type &&
2826 pol->priority < priority) {
2827 ret = pol;
2828 break;
2829 }
2830 }
2831
2832 if (ret)
2833 xfrm_pol_hold(ret);
2834
2835 read_unlock_bh(&xfrm_policy_lock);
2836
2837 return ret;
2838}
2839
David S. Millerdd701752011-02-24 00:21:08 -05002840static int migrate_tmpl_match(const struct xfrm_migrate *m, const struct xfrm_tmpl *t)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002841{
2842 int match = 0;
2843
2844 if (t->mode == m->mode && t->id.proto == m->proto &&
2845 (m->reqid == 0 || t->reqid == m->reqid)) {
2846 switch (t->mode) {
2847 case XFRM_MODE_TUNNEL:
2848 case XFRM_MODE_BEET:
2849 if (xfrm_addr_cmp(&t->id.daddr, &m->old_daddr,
2850 m->old_family) == 0 &&
2851 xfrm_addr_cmp(&t->saddr, &m->old_saddr,
2852 m->old_family) == 0) {
2853 match = 1;
2854 }
2855 break;
2856 case XFRM_MODE_TRANSPORT:
2857 /* in case of transport mode, template does not store
2858 any IP addresses, hence we just compare mode and
2859 protocol */
2860 match = 1;
2861 break;
2862 default:
2863 break;
2864 }
2865 }
2866 return match;
2867}
2868
2869/* update endpoint address(es) of template(s) */
2870static int xfrm_policy_migrate(struct xfrm_policy *pol,
2871 struct xfrm_migrate *m, int num_migrate)
2872{
2873 struct xfrm_migrate *mp;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002874 int i, j, n = 0;
2875
2876 write_lock_bh(&pol->lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07002877 if (unlikely(pol->walk.dead)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002878 /* target policy has been deleted */
2879 write_unlock_bh(&pol->lock);
2880 return -ENOENT;
2881 }
2882
2883 for (i = 0; i < pol->xfrm_nr; i++) {
2884 for (j = 0, mp = m; j < num_migrate; j++, mp++) {
2885 if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
2886 continue;
2887 n++;
Herbert Xu1bfcb102007-10-17 21:31:50 -07002888 if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
2889 pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002890 continue;
2891 /* update endpoints */
2892 memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
2893 sizeof(pol->xfrm_vec[i].id.daddr));
2894 memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
2895 sizeof(pol->xfrm_vec[i].saddr));
2896 pol->xfrm_vec[i].encap_family = mp->new_family;
2897 /* flush bundles */
Timo Teräs80c802f2010-04-07 00:30:05 +00002898 atomic_inc(&pol->genid);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002899 }
2900 }
2901
2902 write_unlock_bh(&pol->lock);
2903
2904 if (!n)
2905 return -ENODATA;
2906
2907 return 0;
2908}
2909
David S. Millerdd701752011-02-24 00:21:08 -05002910static int xfrm_migrate_check(const struct xfrm_migrate *m, int num_migrate)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002911{
2912 int i, j;
2913
2914 if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
2915 return -EINVAL;
2916
2917 for (i = 0; i < num_migrate; i++) {
2918 if ((xfrm_addr_cmp(&m[i].old_daddr, &m[i].new_daddr,
2919 m[i].old_family) == 0) &&
2920 (xfrm_addr_cmp(&m[i].old_saddr, &m[i].new_saddr,
2921 m[i].old_family) == 0))
2922 return -EINVAL;
2923 if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
2924 xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
2925 return -EINVAL;
2926
2927 /* check if there is any duplicated entry */
2928 for (j = i + 1; j < num_migrate; j++) {
2929 if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
2930 sizeof(m[i].old_daddr)) &&
2931 !memcmp(&m[i].old_saddr, &m[j].old_saddr,
2932 sizeof(m[i].old_saddr)) &&
2933 m[i].proto == m[j].proto &&
2934 m[i].mode == m[j].mode &&
2935 m[i].reqid == m[j].reqid &&
2936 m[i].old_family == m[j].old_family)
2937 return -EINVAL;
2938 }
2939 }
2940
2941 return 0;
2942}
2943
David S. Millerb4b7c0b2011-02-24 00:35:06 -05002944int xfrm_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002945 struct xfrm_migrate *m, int num_migrate,
2946 struct xfrm_kmaddress *k)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002947{
2948 int i, err, nx_cur = 0, nx_new = 0;
2949 struct xfrm_policy *pol = NULL;
2950 struct xfrm_state *x, *xc;
2951 struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
2952 struct xfrm_state *x_new[XFRM_MAX_DEPTH];
2953 struct xfrm_migrate *mp;
2954
2955 if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
2956 goto out;
2957
2958 /* Stage 1 - find policy */
2959 if ((pol = xfrm_migrate_policy_find(sel, dir, type)) == NULL) {
2960 err = -ENOENT;
2961 goto out;
2962 }
2963
2964 /* Stage 2 - find and update state(s) */
2965 for (i = 0, mp = m; i < num_migrate; i++, mp++) {
2966 if ((x = xfrm_migrate_state_find(mp))) {
2967 x_cur[nx_cur] = x;
2968 nx_cur++;
2969 if ((xc = xfrm_state_migrate(x, mp))) {
2970 x_new[nx_new] = xc;
2971 nx_new++;
2972 } else {
2973 err = -ENODATA;
2974 goto restore_state;
2975 }
2976 }
2977 }
2978
2979 /* Stage 3 - update policy */
2980 if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
2981 goto restore_state;
2982
2983 /* Stage 4 - delete old state(s) */
2984 if (nx_cur) {
2985 xfrm_states_put(x_cur, nx_cur);
2986 xfrm_states_delete(x_cur, nx_cur);
2987 }
2988
2989 /* Stage 5 - announce */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07002990 km_migrate(sel, dir, type, m, num_migrate, k);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08002991
2992 xfrm_pol_put(pol);
2993
2994 return 0;
2995out:
2996 return err;
2997
2998restore_state:
2999 if (pol)
3000 xfrm_pol_put(pol);
3001 if (nx_cur)
3002 xfrm_states_put(x_cur, nx_cur);
3003 if (nx_new)
3004 xfrm_states_delete(x_new, nx_new);
3005
3006 return err;
3007}
David S. Millere610e672007-02-08 13:29:15 -08003008EXPORT_SYMBOL(xfrm_migrate);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003009#endif