blob: 167c67d46c6a56a87baed9d3f6be17d137ffbe1a [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
Steffen Klasserta0073fe2013-02-05 12:52:55 +010038#define XFRM_QUEUE_TMO_MIN ((unsigned)(HZ/10))
39#define XFRM_QUEUE_TMO_MAX ((unsigned)(60*HZ))
40#define XFRM_MAX_QUEUE_LEN 100
41
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080042DEFINE_MUTEX(xfrm_cfg_mutex);
43EXPORT_SYMBOL(xfrm_cfg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Timo Teräs80c802f2010-04-07 00:30:05 +000045static DEFINE_SPINLOCK(xfrm_policy_sk_bundle_lock);
46static struct dst_entry *xfrm_policy_sk_bundles;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047static DEFINE_RWLOCK(xfrm_policy_lock);
48
Priyanka Jain418a99a2012-08-12 21:22:29 +000049static DEFINE_SPINLOCK(xfrm_policy_afinfo_lock);
50static struct xfrm_policy_afinfo __rcu *xfrm_policy_afinfo[NPROTO]
51 __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Christoph Lametere18b8902006-12-06 20:33:20 -080053static struct kmem_cache *xfrm_dst_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Herbert Xu25ee3282007-12-11 09:32:34 -080055static void xfrm_init_pmtu(struct dst_entry *dst);
Timo Teräs80c802f2010-04-07 00:30:05 +000056static int stale_bundle(struct dst_entry *dst);
Steffen Klassert12fdb4d2011-06-29 23:18:20 +000057static int xfrm_bundle_ok(struct xfrm_dst *xdst);
Steffen Klasserta0073fe2013-02-05 12:52:55 +010058static void xfrm_policy_queue_process(unsigned long arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Wei Yongjun29fa0b302008-12-03 00:33:09 -080060static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
61 int dir);
62
David S. Millerbc9b35a2012-05-15 15:04:57 -040063static inline bool
David S. Miller200ce962011-02-24 00:12:25 -050064__xfrm4_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
Andrew Morton77681022006-11-08 22:46:26 -080065{
David S. Miller7e1dc7b2011-03-12 02:42:11 -050066 const struct flowi4 *fl4 = &fl->u.ip4;
67
Alexey Dobriyan26bff942011-11-22 06:46:02 +000068 return addr4_match(fl4->daddr, sel->daddr.a4, sel->prefixlen_d) &&
69 addr4_match(fl4->saddr, sel->saddr.a4, sel->prefixlen_s) &&
David S. Miller7e1dc7b2011-03-12 02:42:11 -050070 !((xfrm_flowi_dport(fl, &fl4->uli) ^ sel->dport) & sel->dport_mask) &&
71 !((xfrm_flowi_sport(fl, &fl4->uli) ^ sel->sport) & sel->sport_mask) &&
72 (fl4->flowi4_proto == sel->proto || !sel->proto) &&
73 (fl4->flowi4_oif == sel->ifindex || !sel->ifindex);
Andrew Morton77681022006-11-08 22:46:26 -080074}
75
David S. Millerbc9b35a2012-05-15 15:04:57 -040076static inline bool
David S. Miller200ce962011-02-24 00:12:25 -050077__xfrm6_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
Andrew Morton77681022006-11-08 22:46:26 -080078{
David S. Miller7e1dc7b2011-03-12 02:42:11 -050079 const struct flowi6 *fl6 = &fl->u.ip6;
80
81 return addr_match(&fl6->daddr, &sel->daddr, sel->prefixlen_d) &&
82 addr_match(&fl6->saddr, &sel->saddr, sel->prefixlen_s) &&
83 !((xfrm_flowi_dport(fl, &fl6->uli) ^ sel->dport) & sel->dport_mask) &&
84 !((xfrm_flowi_sport(fl, &fl6->uli) ^ sel->sport) & sel->sport_mask) &&
85 (fl6->flowi6_proto == sel->proto || !sel->proto) &&
86 (fl6->flowi6_oif == sel->ifindex || !sel->ifindex);
Andrew Morton77681022006-11-08 22:46:26 -080087}
88
David S. Millerbc9b35a2012-05-15 15:04:57 -040089bool xfrm_selector_match(const struct xfrm_selector *sel, const struct flowi *fl,
90 unsigned short family)
Andrew Morton77681022006-11-08 22:46:26 -080091{
92 switch (family) {
93 case AF_INET:
94 return __xfrm4_selector_match(sel, fl);
95 case AF_INET6:
96 return __xfrm6_selector_match(sel, fl);
97 }
David S. Millerbc9b35a2012-05-15 15:04:57 -040098 return false;
Andrew Morton77681022006-11-08 22:46:26 -080099}
100
Eric Dumazetef8531b2012-08-19 12:31:48 +0200101static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
102{
103 struct xfrm_policy_afinfo *afinfo;
104
105 if (unlikely(family >= NPROTO))
106 return NULL;
107 rcu_read_lock();
108 afinfo = rcu_dereference(xfrm_policy_afinfo[family]);
109 if (unlikely(!afinfo))
110 rcu_read_unlock();
111 return afinfo;
112}
113
114static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
115{
116 rcu_read_unlock();
117}
118
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800119static inline struct dst_entry *__xfrm_dst_lookup(struct net *net, int tos,
David S. Miller6418c4e2011-02-24 00:16:53 -0500120 const xfrm_address_t *saddr,
121 const xfrm_address_t *daddr,
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900122 int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800124 struct xfrm_policy_afinfo *afinfo;
125 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Herbert Xu25ee3282007-12-11 09:32:34 -0800127 afinfo = xfrm_policy_get_afinfo(family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 if (unlikely(afinfo == NULL))
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800129 return ERR_PTR(-EAFNOSUPPORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800131 dst = afinfo->dst_lookup(net, tos, saddr, daddr);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 xfrm_policy_put_afinfo(afinfo);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900134
135 return dst;
136}
137
138static inline struct dst_entry *xfrm_dst_lookup(struct xfrm_state *x, int tos,
139 xfrm_address_t *prev_saddr,
140 xfrm_address_t *prev_daddr,
141 int family)
142{
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800143 struct net *net = xs_net(x);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900144 xfrm_address_t *saddr = &x->props.saddr;
145 xfrm_address_t *daddr = &x->id.daddr;
146 struct dst_entry *dst;
147
148 if (x->type->flags & XFRM_TYPE_LOCAL_COADDR) {
149 saddr = x->coaddr;
150 daddr = prev_daddr;
151 }
152 if (x->type->flags & XFRM_TYPE_REMOTE_COADDR) {
153 saddr = prev_saddr;
154 daddr = x->coaddr;
155 }
156
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800157 dst = __xfrm_dst_lookup(net, tos, saddr, daddr, family);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900158
159 if (!IS_ERR(dst)) {
160 if (prev_saddr != saddr)
161 memcpy(prev_saddr, saddr, sizeof(*prev_saddr));
162 if (prev_daddr != daddr)
163 memcpy(prev_daddr, daddr, sizeof(*prev_daddr));
164 }
165
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800166 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169static inline unsigned long make_jiffies(long secs)
170{
171 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
172 return MAX_SCHEDULE_TIMEOUT-1;
173 else
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900174 return secs*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
176
177static void xfrm_policy_timer(unsigned long data)
178{
179 struct xfrm_policy *xp = (struct xfrm_policy*)data;
James Morris9d729f72007-03-04 16:12:44 -0800180 unsigned long now = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 long next = LONG_MAX;
182 int warn = 0;
183 int dir;
184
185 read_lock(&xp->lock);
186
Timo Teräsea2dea92010-03-31 00:17:05 +0000187 if (unlikely(xp->walk.dead))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 goto out;
189
Herbert Xu77d8d7a2005-10-05 12:15:12 -0700190 dir = xfrm_policy_id2dir(xp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 if (xp->lft.hard_add_expires_seconds) {
193 long tmo = xp->lft.hard_add_expires_seconds +
194 xp->curlft.add_time - now;
195 if (tmo <= 0)
196 goto expired;
197 if (tmo < next)
198 next = tmo;
199 }
200 if (xp->lft.hard_use_expires_seconds) {
201 long tmo = xp->lft.hard_use_expires_seconds +
202 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
203 if (tmo <= 0)
204 goto expired;
205 if (tmo < next)
206 next = tmo;
207 }
208 if (xp->lft.soft_add_expires_seconds) {
209 long tmo = xp->lft.soft_add_expires_seconds +
210 xp->curlft.add_time - now;
211 if (tmo <= 0) {
212 warn = 1;
213 tmo = XFRM_KM_TIMEOUT;
214 }
215 if (tmo < next)
216 next = tmo;
217 }
218 if (xp->lft.soft_use_expires_seconds) {
219 long tmo = xp->lft.soft_use_expires_seconds +
220 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
221 if (tmo <= 0) {
222 warn = 1;
223 tmo = XFRM_KM_TIMEOUT;
224 }
225 if (tmo < next)
226 next = tmo;
227 }
228
229 if (warn)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800230 km_policy_expired(xp, dir, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 if (next != LONG_MAX &&
232 !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
233 xfrm_pol_hold(xp);
234
235out:
236 read_unlock(&xp->lock);
237 xfrm_pol_put(xp);
238 return;
239
240expired:
241 read_unlock(&xp->lock);
Herbert Xu4666faa2005-06-18 22:43:22 -0700242 if (!xfrm_policy_delete(xp, dir))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800243 km_policy_expired(xp, dir, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 xfrm_pol_put(xp);
245}
246
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000247static struct flow_cache_object *xfrm_policy_flo_get(struct flow_cache_object *flo)
248{
249 struct xfrm_policy *pol = container_of(flo, struct xfrm_policy, flo);
250
251 if (unlikely(pol->walk.dead))
252 flo = NULL;
253 else
254 xfrm_pol_hold(pol);
255
256 return flo;
257}
258
259static int xfrm_policy_flo_check(struct flow_cache_object *flo)
260{
261 struct xfrm_policy *pol = container_of(flo, struct xfrm_policy, flo);
262
263 return !pol->walk.dead;
264}
265
266static void xfrm_policy_flo_delete(struct flow_cache_object *flo)
267{
268 xfrm_pol_put(container_of(flo, struct xfrm_policy, flo));
269}
270
271static const struct flow_cache_ops xfrm_policy_fc_ops = {
272 .get = xfrm_policy_flo_get,
273 .check = xfrm_policy_flo_check,
274 .delete = xfrm_policy_flo_delete,
275};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277/* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
278 * SPD calls.
279 */
280
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -0800281struct xfrm_policy *xfrm_policy_alloc(struct net *net, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
283 struct xfrm_policy *policy;
284
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700285 policy = kzalloc(sizeof(struct xfrm_policy), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 if (policy) {
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -0800288 write_pnet(&policy->xp_net, net);
Herbert Xu12a169e2008-10-01 07:03:24 -0700289 INIT_LIST_HEAD(&policy->walk.all);
David S. Miller2518c7c2006-08-24 04:45:07 -0700290 INIT_HLIST_NODE(&policy->bydst);
291 INIT_HLIST_NODE(&policy->byidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 rwlock_init(&policy->lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700293 atomic_set(&policy->refcnt, 1);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100294 skb_queue_head_init(&policy->polq.hold_queue);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800295 setup_timer(&policy->timer, xfrm_policy_timer,
296 (unsigned long)policy);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100297 setup_timer(&policy->polq.hold_timer, xfrm_policy_queue_process,
298 (unsigned long)policy);
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000299 policy->flo.ops = &xfrm_policy_fc_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 }
301 return policy;
302}
303EXPORT_SYMBOL(xfrm_policy_alloc);
304
305/* Destroy xfrm_policy: descendant resources must be released to this moment. */
306
WANG Cong64c31b32008-01-07 22:34:29 -0800307void xfrm_policy_destroy(struct xfrm_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
Herbert Xu12a169e2008-10-01 07:03:24 -0700309 BUG_ON(!policy->walk.dead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 if (del_timer(&policy->timer))
312 BUG();
313
Paul Moore03e1ad72008-04-12 19:07:52 -0700314 security_xfrm_policy_free(policy->security);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 kfree(policy);
316}
WANG Cong64c31b32008-01-07 22:34:29 -0800317EXPORT_SYMBOL(xfrm_policy_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100319static void xfrm_queue_purge(struct sk_buff_head *list)
320{
321 struct sk_buff *skb;
322
323 while ((skb = skb_dequeue(list)) != NULL) {
324 dev_put(skb->dev);
325 kfree_skb(skb);
326 }
327}
328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329/* Rule must be locked. Release descentant resources, announce
330 * entry dead. The rule must be unlinked from lists to the moment.
331 */
332
333static void xfrm_policy_kill(struct xfrm_policy *policy)
334{
Herbert Xu12a169e2008-10-01 07:03:24 -0700335 policy->walk.dead = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Timo Teräs285ead12010-04-07 00:30:06 +0000337 atomic_inc(&policy->genid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100339 del_timer(&policy->polq.hold_timer);
340 xfrm_queue_purge(&policy->polq.hold_queue);
341
Timo Teräs285ead12010-04-07 00:30:06 +0000342 if (del_timer(&policy->timer))
343 xfrm_pol_put(policy);
344
345 xfrm_pol_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346}
347
David S. Miller2518c7c2006-08-24 04:45:07 -0700348static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024;
349
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800350static inline unsigned int idx_hash(struct net *net, u32 index)
David S. Miller2518c7c2006-08-24 04:45:07 -0700351{
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800352 return __idx_hash(index, net->xfrm.policy_idx_hmask);
David S. Miller2518c7c2006-08-24 04:45:07 -0700353}
354
David S. Miller5f803b52011-02-24 00:33:19 -0500355static struct hlist_head *policy_hash_bysel(struct net *net,
356 const struct xfrm_selector *sel,
357 unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700358{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800359 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700360 unsigned int hash = __sel_hash(sel, family, hmask);
361
362 return (hash == hmask + 1 ?
Alexey Dobriyan11219942008-11-25 17:33:06 -0800363 &net->xfrm.policy_inexact[dir] :
364 net->xfrm.policy_bydst[dir].table + hash);
David S. Miller2518c7c2006-08-24 04:45:07 -0700365}
366
David S. Miller5f803b52011-02-24 00:33:19 -0500367static struct hlist_head *policy_hash_direct(struct net *net,
368 const xfrm_address_t *daddr,
369 const xfrm_address_t *saddr,
370 unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700371{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800372 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700373 unsigned int hash = __addr_hash(daddr, saddr, family, hmask);
374
Alexey Dobriyan11219942008-11-25 17:33:06 -0800375 return net->xfrm.policy_bydst[dir].table + hash;
David S. Miller2518c7c2006-08-24 04:45:07 -0700376}
377
David S. Miller2518c7c2006-08-24 04:45:07 -0700378static void xfrm_dst_hash_transfer(struct hlist_head *list,
379 struct hlist_head *ndsttable,
380 unsigned int nhashmask)
381{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800382 struct hlist_node *tmp, *entry0 = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700383 struct xfrm_policy *pol;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800384 unsigned int h0 = 0;
David S. Miller2518c7c2006-08-24 04:45:07 -0700385
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800386redo:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800387 hlist_for_each_entry_safe(pol, tmp, list, bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700388 unsigned int h;
389
390 h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
391 pol->family, nhashmask);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800392 if (!entry0) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800393 hlist_del(&pol->bydst);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800394 hlist_add_head(&pol->bydst, ndsttable+h);
395 h0 = h;
396 } else {
397 if (h != h0)
398 continue;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800399 hlist_del(&pol->bydst);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800400 hlist_add_after(entry0, &pol->bydst);
401 }
Sasha Levinb67bfe02013-02-27 17:06:00 -0800402 entry0 = &pol->bydst;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800403 }
404 if (!hlist_empty(list)) {
405 entry0 = NULL;
406 goto redo;
David S. Miller2518c7c2006-08-24 04:45:07 -0700407 }
408}
409
410static void xfrm_idx_hash_transfer(struct hlist_head *list,
411 struct hlist_head *nidxtable,
412 unsigned int nhashmask)
413{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800414 struct hlist_node *tmp;
David S. Miller2518c7c2006-08-24 04:45:07 -0700415 struct xfrm_policy *pol;
416
Sasha Levinb67bfe02013-02-27 17:06:00 -0800417 hlist_for_each_entry_safe(pol, tmp, list, byidx) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700418 unsigned int h;
419
420 h = __idx_hash(pol->index, nhashmask);
421 hlist_add_head(&pol->byidx, nidxtable+h);
422 }
423}
424
425static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
426{
427 return ((old_hmask + 1) << 1) - 1;
428}
429
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800430static void xfrm_bydst_resize(struct net *net, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700431{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800432 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700433 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
434 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800435 struct hlist_head *odst = net->xfrm.policy_bydst[dir].table;
David S. Miller44e36b42006-08-24 04:50:50 -0700436 struct hlist_head *ndst = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700437 int i;
438
439 if (!ndst)
440 return;
441
442 write_lock_bh(&xfrm_policy_lock);
443
444 for (i = hmask; i >= 0; i--)
445 xfrm_dst_hash_transfer(odst + i, ndst, nhashmask);
446
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800447 net->xfrm.policy_bydst[dir].table = ndst;
448 net->xfrm.policy_bydst[dir].hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700449
450 write_unlock_bh(&xfrm_policy_lock);
451
David S. Miller44e36b42006-08-24 04:50:50 -0700452 xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700453}
454
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800455static void xfrm_byidx_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700456{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800457 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700458 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
459 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800460 struct hlist_head *oidx = net->xfrm.policy_byidx;
David S. Miller44e36b42006-08-24 04:50:50 -0700461 struct hlist_head *nidx = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700462 int i;
463
464 if (!nidx)
465 return;
466
467 write_lock_bh(&xfrm_policy_lock);
468
469 for (i = hmask; i >= 0; i--)
470 xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
471
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800472 net->xfrm.policy_byidx = nidx;
473 net->xfrm.policy_idx_hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700474
475 write_unlock_bh(&xfrm_policy_lock);
476
David S. Miller44e36b42006-08-24 04:50:50 -0700477 xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700478}
479
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800480static inline int xfrm_bydst_should_resize(struct net *net, int dir, int *total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700481{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800482 unsigned int cnt = net->xfrm.policy_count[dir];
483 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700484
485 if (total)
486 *total += cnt;
487
488 if ((hmask + 1) < xfrm_policy_hashmax &&
489 cnt > hmask)
490 return 1;
491
492 return 0;
493}
494
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800495static inline int xfrm_byidx_should_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700496{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800497 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700498
499 if ((hmask + 1) < xfrm_policy_hashmax &&
500 total > hmask)
501 return 1;
502
503 return 0;
504}
505
Alexey Dobriyane0710412010-01-23 13:37:10 +0000506void xfrm_spd_getinfo(struct net *net, struct xfrmk_spdinfo *si)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700507{
508 read_lock_bh(&xfrm_policy_lock);
Alexey Dobriyane0710412010-01-23 13:37:10 +0000509 si->incnt = net->xfrm.policy_count[XFRM_POLICY_IN];
510 si->outcnt = net->xfrm.policy_count[XFRM_POLICY_OUT];
511 si->fwdcnt = net->xfrm.policy_count[XFRM_POLICY_FWD];
512 si->inscnt = net->xfrm.policy_count[XFRM_POLICY_IN+XFRM_POLICY_MAX];
513 si->outscnt = net->xfrm.policy_count[XFRM_POLICY_OUT+XFRM_POLICY_MAX];
514 si->fwdscnt = net->xfrm.policy_count[XFRM_POLICY_FWD+XFRM_POLICY_MAX];
515 si->spdhcnt = net->xfrm.policy_idx_hmask;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700516 si->spdhmcnt = xfrm_policy_hashmax;
517 read_unlock_bh(&xfrm_policy_lock);
518}
519EXPORT_SYMBOL(xfrm_spd_getinfo);
David S. Miller2518c7c2006-08-24 04:45:07 -0700520
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700521static DEFINE_MUTEX(hash_resize_mutex);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800522static void xfrm_hash_resize(struct work_struct *work)
David S. Miller2518c7c2006-08-24 04:45:07 -0700523{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800524 struct net *net = container_of(work, struct net, xfrm.policy_hash_work);
David S. Miller2518c7c2006-08-24 04:45:07 -0700525 int dir, total;
526
527 mutex_lock(&hash_resize_mutex);
528
529 total = 0;
530 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800531 if (xfrm_bydst_should_resize(net, dir, &total))
532 xfrm_bydst_resize(net, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700533 }
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800534 if (xfrm_byidx_should_resize(net, total))
535 xfrm_byidx_resize(net, total);
David S. Miller2518c7c2006-08-24 04:45:07 -0700536
537 mutex_unlock(&hash_resize_mutex);
538}
539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540/* Generate new index... KAME seems to generate them ordered by cost
541 * of an absolute inpredictability of ordering of rules. This will not pass. */
Alexey Dobriyan11219942008-11-25 17:33:06 -0800542static u32 xfrm_gen_index(struct net *net, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 static u32 idx_generator;
545
546 for (;;) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700547 struct hlist_head *list;
548 struct xfrm_policy *p;
549 u32 idx;
550 int found;
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 idx = (idx_generator | dir);
553 idx_generator += 8;
554 if (idx == 0)
555 idx = 8;
Alexey Dobriyan11219942008-11-25 17:33:06 -0800556 list = net->xfrm.policy_byidx + idx_hash(net, idx);
David S. Miller2518c7c2006-08-24 04:45:07 -0700557 found = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800558 hlist_for_each_entry(p, list, byidx) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700559 if (p->index == idx) {
560 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 break;
David S. Miller2518c7c2006-08-24 04:45:07 -0700562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700564 if (!found)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 return idx;
566 }
567}
568
David S. Miller2518c7c2006-08-24 04:45:07 -0700569static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
570{
571 u32 *p1 = (u32 *) s1;
572 u32 *p2 = (u32 *) s2;
573 int len = sizeof(struct xfrm_selector) / sizeof(u32);
574 int i;
575
576 for (i = 0; i < len; i++) {
577 if (p1[i] != p2[i])
578 return 1;
579 }
580
581 return 0;
582}
583
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100584static void xfrm_policy_requeue(struct xfrm_policy *old,
585 struct xfrm_policy *new)
586{
587 struct xfrm_policy_queue *pq = &old->polq;
588 struct sk_buff_head list;
589
590 __skb_queue_head_init(&list);
591
592 spin_lock_bh(&pq->hold_queue.lock);
593 skb_queue_splice_init(&pq->hold_queue, &list);
594 del_timer(&pq->hold_timer);
595 spin_unlock_bh(&pq->hold_queue.lock);
596
597 if (skb_queue_empty(&list))
598 return;
599
600 pq = &new->polq;
601
602 spin_lock_bh(&pq->hold_queue.lock);
603 skb_queue_splice(&list, &pq->hold_queue);
604 pq->timeout = XFRM_QUEUE_TMO_MIN;
605 mod_timer(&pq->hold_timer, jiffies);
606 spin_unlock_bh(&pq->hold_queue.lock);
607}
608
Steffen Klassert7cb8a932013-02-11 07:02:36 +0100609static bool xfrm_policy_mark_match(struct xfrm_policy *policy,
610 struct xfrm_policy *pol)
611{
612 u32 mark = policy->mark.v & policy->mark.m;
613
614 if (policy->mark.v == pol->mark.v && policy->mark.m == pol->mark.m)
615 return true;
616
617 if ((mark & pol->mark.m) == pol->mark.v &&
618 policy->priority == pol->priority)
619 return true;
620
621 return false;
622}
623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
625{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800626 struct net *net = xp_net(policy);
David S. Miller2518c7c2006-08-24 04:45:07 -0700627 struct xfrm_policy *pol;
628 struct xfrm_policy *delpol;
629 struct hlist_head *chain;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800630 struct hlist_node *newpos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632 write_lock_bh(&xfrm_policy_lock);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800633 chain = policy_hash_bysel(net, &policy->selector, policy->family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700634 delpol = NULL;
635 newpos = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800636 hlist_for_each_entry(pol, chain, bydst) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800637 if (pol->type == policy->type &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700638 !selector_cmp(&pol->selector, &policy->selector) &&
Steffen Klassert7cb8a932013-02-11 07:02:36 +0100639 xfrm_policy_mark_match(policy, pol) &&
Herbert Xua6c7ab52007-01-16 16:52:02 -0800640 xfrm_sec_ctx_match(pol->security, policy->security) &&
641 !WARN_ON(delpol)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 if (excl) {
643 write_unlock_bh(&xfrm_policy_lock);
644 return -EEXIST;
645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 delpol = pol;
647 if (policy->priority > pol->priority)
648 continue;
649 } else if (policy->priority >= pol->priority) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800650 newpos = &pol->bydst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 continue;
652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 if (delpol)
654 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 }
656 if (newpos)
David S. Miller2518c7c2006-08-24 04:45:07 -0700657 hlist_add_after(newpos, &policy->bydst);
658 else
659 hlist_add_head(&policy->bydst, chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 xfrm_pol_hold(policy);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800661 net->xfrm.policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 atomic_inc(&flow_cache_genid);
Nicolas Dichtelee8372d2012-09-10 22:09:45 +0000663 rt_genid_bump(net);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100664 if (delpol) {
665 xfrm_policy_requeue(delpol, policy);
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800666 __xfrm_policy_unlink(delpol, dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100667 }
Alexey Dobriyan11219942008-11-25 17:33:06 -0800668 policy->index = delpol ? delpol->index : xfrm_gen_index(net, dir);
669 hlist_add_head(&policy->byidx, net->xfrm.policy_byidx+idx_hash(net, policy->index));
James Morris9d729f72007-03-04 16:12:44 -0800670 policy->curlft.add_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 policy->curlft.use_time = 0;
672 if (!mod_timer(&policy->timer, jiffies + HZ))
673 xfrm_pol_hold(policy);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800674 list_add(&policy->walk.all, &net->xfrm.policy_all);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 write_unlock_bh(&xfrm_policy_lock);
676
David S. Miller9b78a822005-12-22 07:39:48 -0800677 if (delpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 xfrm_policy_kill(delpol);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800679 else if (xfrm_bydst_should_resize(net, dir, NULL))
680 schedule_work(&net->xfrm.policy_hash_work);
David S. Miller9b78a822005-12-22 07:39:48 -0800681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 return 0;
683}
684EXPORT_SYMBOL(xfrm_policy_insert);
685
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +0000686struct xfrm_policy *xfrm_policy_bysel_ctx(struct net *net, u32 mark, u8 type,
687 int dir, struct xfrm_selector *sel,
Eric Parisef41aaa2007-03-07 15:37:58 -0800688 struct xfrm_sec_ctx *ctx, int delete,
689 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
David S. Miller2518c7c2006-08-24 04:45:07 -0700691 struct xfrm_policy *pol, *ret;
692 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Eric Parisef41aaa2007-03-07 15:37:58 -0800694 *err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 write_lock_bh(&xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800696 chain = policy_hash_bysel(net, sel, sel->family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700697 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800698 hlist_for_each_entry(pol, chain, bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700699 if (pol->type == type &&
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000700 (mark & pol->mark.m) == pol->mark.v &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700701 !selector_cmp(sel, &pol->selector) &&
702 xfrm_sec_ctx_match(ctx, pol->security)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700704 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -0700705 *err = security_xfrm_policy_delete(
706 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -0800707 if (*err) {
708 write_unlock_bh(&xfrm_policy_lock);
709 return pol;
710 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800711 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700712 }
713 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 break;
715 }
716 }
717 write_unlock_bh(&xfrm_policy_lock);
718
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000719 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -0700720 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -0700721 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722}
Trent Jaegerdf718372005-12-13 23:12:27 -0800723EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +0000725struct xfrm_policy *xfrm_policy_byid(struct net *net, u32 mark, u8 type,
726 int dir, u32 id, int delete, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
David S. Miller2518c7c2006-08-24 04:45:07 -0700728 struct xfrm_policy *pol, *ret;
729 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Herbert Xub5505c62007-05-14 02:15:47 -0700731 *err = -ENOENT;
732 if (xfrm_policy_id2dir(id) != dir)
733 return NULL;
734
Eric Parisef41aaa2007-03-07 15:37:58 -0800735 *err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 write_lock_bh(&xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800737 chain = net->xfrm.policy_byidx + idx_hash(net, id);
David S. Miller2518c7c2006-08-24 04:45:07 -0700738 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800739 hlist_for_each_entry(pol, chain, byidx) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000740 if (pol->type == type && pol->index == id &&
741 (mark & pol->mark.m) == pol->mark.v) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700743 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -0700744 *err = security_xfrm_policy_delete(
745 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -0800746 if (*err) {
747 write_unlock_bh(&xfrm_policy_lock);
748 return pol;
749 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800750 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700751 }
752 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 break;
754 }
755 }
756 write_unlock_bh(&xfrm_policy_lock);
757
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000758 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -0700759 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -0700760 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761}
762EXPORT_SYMBOL(xfrm_policy_byid);
763
Joy Latten4aa2e622007-06-04 19:05:57 -0400764#ifdef CONFIG_SECURITY_NETWORK_XFRM
765static inline int
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800766xfrm_policy_flush_secctx_check(struct net *net, u8 type, struct xfrm_audit *audit_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
Joy Latten4aa2e622007-06-04 19:05:57 -0400768 int dir, err = 0;
769
770 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
771 struct xfrm_policy *pol;
Joy Latten4aa2e622007-06-04 19:05:57 -0400772 int i;
773
Sasha Levinb67bfe02013-02-27 17:06:00 -0800774 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800775 &net->xfrm.policy_inexact[dir], bydst) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400776 if (pol->type != type)
777 continue;
Paul Moore03e1ad72008-04-12 19:07:52 -0700778 err = security_xfrm_policy_delete(pol->security);
Joy Latten4aa2e622007-06-04 19:05:57 -0400779 if (err) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700780 xfrm_audit_policy_delete(pol, 0,
781 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 Latten4aa2e622007-06-04 19:05:57 -0400784 return err;
785 }
YOSHIFUJI Hideaki7dc12d62007-07-19 10:45:15 +0900786 }
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800787 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800788 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800789 net->xfrm.policy_bydst[dir].table + i,
Joy Latten4aa2e622007-06-04 19:05:57 -0400790 bydst) {
791 if (pol->type != type)
792 continue;
Paul Moore03e1ad72008-04-12 19:07:52 -0700793 err = security_xfrm_policy_delete(
794 pol->security);
Joy Latten4aa2e622007-06-04 19:05:57 -0400795 if (err) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700796 xfrm_audit_policy_delete(pol, 0,
797 audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400798 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700799 audit_info->secid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400800 return err;
801 }
802 }
803 }
804 }
805 return err;
806}
807#else
808static inline int
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800809xfrm_policy_flush_secctx_check(struct net *net, u8 type, struct xfrm_audit *audit_info)
Joy Latten4aa2e622007-06-04 19:05:57 -0400810{
811 return 0;
812}
813#endif
814
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800815int xfrm_policy_flush(struct net *net, u8 type, struct xfrm_audit *audit_info)
Joy Latten4aa2e622007-06-04 19:05:57 -0400816{
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +0000817 int dir, err = 0, cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819 write_lock_bh(&xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400820
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800821 err = xfrm_policy_flush_secctx_check(net, type, audit_info);
Joy Latten4aa2e622007-06-04 19:05:57 -0400822 if (err)
823 goto out;
824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700826 struct xfrm_policy *pol;
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800827 int i;
David S. Miller2518c7c2006-08-24 04:45:07 -0700828
829 again1:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800830 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800831 &net->xfrm.policy_inexact[dir], bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700832 if (pol->type != type)
833 continue;
Timo Teräsea2dea92010-03-31 00:17:05 +0000834 __xfrm_policy_unlink(pol, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 write_unlock_bh(&xfrm_policy_lock);
Timo Teräsea2dea92010-03-31 00:17:05 +0000836 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Joy Lattenab5f5e82007-09-17 11:51:22 -0700838 xfrm_audit_policy_delete(pol, 1, audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400839 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700840 audit_info->secid);
Joy Latten161a09e2006-11-27 13:11:54 -0600841
David S. Miller2518c7c2006-08-24 04:45:07 -0700842 xfrm_policy_kill(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
844 write_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700845 goto again1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700847
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800848 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700849 again2:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800850 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800851 net->xfrm.policy_bydst[dir].table + i,
David S. Miller2518c7c2006-08-24 04:45:07 -0700852 bydst) {
853 if (pol->type != type)
854 continue;
Timo Teräsea2dea92010-03-31 00:17:05 +0000855 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700856 write_unlock_bh(&xfrm_policy_lock);
Timo Teräsea2dea92010-03-31 00:17:05 +0000857 cnt++;
David S. Miller2518c7c2006-08-24 04:45:07 -0700858
Joy Lattenab5f5e82007-09-17 11:51:22 -0700859 xfrm_audit_policy_delete(pol, 1,
860 audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400861 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700862 audit_info->secid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700863 xfrm_policy_kill(pol);
864
865 write_lock_bh(&xfrm_policy_lock);
866 goto again2;
867 }
868 }
869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 }
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +0000871 if (!cnt)
872 err = -ESRCH;
Joy Latten4aa2e622007-06-04 19:05:57 -0400873out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 write_unlock_bh(&xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400875 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876}
877EXPORT_SYMBOL(xfrm_policy_flush);
878
Alexey Dobriyancdcbca72008-11-25 17:34:49 -0800879int xfrm_policy_walk(struct net *net, struct xfrm_policy_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -0800880 int (*func)(struct xfrm_policy *, int, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 void *data)
882{
Herbert Xu12a169e2008-10-01 07:03:24 -0700883 struct xfrm_policy *pol;
884 struct xfrm_policy_walk_entry *x;
Timo Teras4c563f72008-02-28 21:31:08 -0800885 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Timo Teras4c563f72008-02-28 21:31:08 -0800887 if (walk->type >= XFRM_POLICY_TYPE_MAX &&
888 walk->type != XFRM_POLICY_TYPE_ANY)
889 return -EINVAL;
890
Herbert Xu12a169e2008-10-01 07:03:24 -0700891 if (list_empty(&walk->walk.all) && walk->seq != 0)
Timo Teras4c563f72008-02-28 21:31:08 -0800892 return 0;
893
Herbert Xu12a169e2008-10-01 07:03:24 -0700894 write_lock_bh(&xfrm_policy_lock);
895 if (list_empty(&walk->walk.all))
Alexey Dobriyancdcbca72008-11-25 17:34:49 -0800896 x = list_first_entry(&net->xfrm.policy_all, struct xfrm_policy_walk_entry, all);
Herbert Xu12a169e2008-10-01 07:03:24 -0700897 else
898 x = list_entry(&walk->walk.all, struct xfrm_policy_walk_entry, all);
Alexey Dobriyancdcbca72008-11-25 17:34:49 -0800899 list_for_each_entry_from(x, &net->xfrm.policy_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -0700900 if (x->dead)
Timo Teras4c563f72008-02-28 21:31:08 -0800901 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -0700902 pol = container_of(x, struct xfrm_policy, walk);
903 if (walk->type != XFRM_POLICY_TYPE_ANY &&
904 walk->type != pol->type)
905 continue;
906 error = func(pol, xfrm_policy_id2dir(pol->index),
907 walk->seq, data);
908 if (error) {
909 list_move_tail(&walk->walk.all, &x->all);
910 goto out;
Timo Teras4c563f72008-02-28 21:31:08 -0800911 }
Herbert Xu12a169e2008-10-01 07:03:24 -0700912 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 }
Herbert Xu12a169e2008-10-01 07:03:24 -0700914 if (walk->seq == 0) {
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -0800915 error = -ENOENT;
916 goto out;
917 }
Herbert Xu12a169e2008-10-01 07:03:24 -0700918 list_del_init(&walk->walk.all);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919out:
Herbert Xu12a169e2008-10-01 07:03:24 -0700920 write_unlock_bh(&xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 return error;
922}
923EXPORT_SYMBOL(xfrm_policy_walk);
924
Herbert Xu12a169e2008-10-01 07:03:24 -0700925void xfrm_policy_walk_init(struct xfrm_policy_walk *walk, u8 type)
926{
927 INIT_LIST_HEAD(&walk->walk.all);
928 walk->walk.dead = 1;
929 walk->type = type;
930 walk->seq = 0;
931}
932EXPORT_SYMBOL(xfrm_policy_walk_init);
933
934void xfrm_policy_walk_done(struct xfrm_policy_walk *walk)
935{
936 if (list_empty(&walk->walk.all))
937 return;
938
939 write_lock_bh(&xfrm_policy_lock);
940 list_del(&walk->walk.all);
941 write_unlock_bh(&xfrm_policy_lock);
942}
943EXPORT_SYMBOL(xfrm_policy_walk_done);
944
James Morris134b0fc2006-10-05 15:42:27 -0500945/*
946 * Find policy to apply to this flow.
947 *
948 * Returns 0 if policy found, else an -errno.
949 */
David S. Millerf299d552011-02-24 01:23:30 -0500950static int xfrm_policy_match(const struct xfrm_policy *pol,
951 const struct flowi *fl,
David S. Miller2518c7c2006-08-24 04:45:07 -0700952 u8 type, u16 family, int dir)
953{
David S. Millerf299d552011-02-24 01:23:30 -0500954 const struct xfrm_selector *sel = &pol->selector;
David S. Millerbc9b35a2012-05-15 15:04:57 -0400955 int ret = -ESRCH;
956 bool match;
David S. Miller2518c7c2006-08-24 04:45:07 -0700957
958 if (pol->family != family ||
David S. Miller1d28f422011-03-12 00:29:39 -0500959 (fl->flowi_mark & pol->mark.m) != pol->mark.v ||
David S. Miller2518c7c2006-08-24 04:45:07 -0700960 pol->type != type)
James Morris134b0fc2006-10-05 15:42:27 -0500961 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -0700962
963 match = xfrm_selector_match(sel, fl, family);
James Morris134b0fc2006-10-05 15:42:27 -0500964 if (match)
David S. Miller1d28f422011-03-12 00:29:39 -0500965 ret = security_xfrm_policy_lookup(pol->security, fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -0700966 dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700967
James Morris134b0fc2006-10-05 15:42:27 -0500968 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -0700969}
970
Alexey Dobriyan52479b62008-11-25 17:35:18 -0800971static struct xfrm_policy *xfrm_policy_lookup_bytype(struct net *net, u8 type,
David S. Miller062cdb42011-02-22 18:31:08 -0800972 const struct flowi *fl,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700973 u16 family, u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974{
James Morris134b0fc2006-10-05 15:42:27 -0500975 int err;
David S. Miller2518c7c2006-08-24 04:45:07 -0700976 struct xfrm_policy *pol, *ret;
David S. Miller0b597e72011-02-24 01:22:48 -0500977 const xfrm_address_t *daddr, *saddr;
David S. Miller2518c7c2006-08-24 04:45:07 -0700978 struct hlist_head *chain;
David S. Milleracba48e2006-08-25 15:46:46 -0700979 u32 priority = ~0U;
David S. Miller2518c7c2006-08-24 04:45:07 -0700980
981 daddr = xfrm_flowi_daddr(fl, family);
982 saddr = xfrm_flowi_saddr(fl, family);
983 if (unlikely(!daddr || !saddr))
984 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
986 read_lock_bh(&xfrm_policy_lock);
Alexey Dobriyan52479b62008-11-25 17:35:18 -0800987 chain = policy_hash_direct(net, daddr, saddr, family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700988 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800989 hlist_for_each_entry(pol, chain, bydst) {
James Morris134b0fc2006-10-05 15:42:27 -0500990 err = xfrm_policy_match(pol, fl, type, family, dir);
991 if (err) {
992 if (err == -ESRCH)
993 continue;
994 else {
995 ret = ERR_PTR(err);
996 goto fail;
997 }
998 } else {
David S. Milleracba48e2006-08-25 15:46:46 -0700999 ret = pol;
1000 priority = ret->priority;
1001 break;
1002 }
1003 }
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001004 chain = &net->xfrm.policy_inexact[dir];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001005 hlist_for_each_entry(pol, chain, bydst) {
James Morris134b0fc2006-10-05 15:42:27 -05001006 err = xfrm_policy_match(pol, fl, type, family, dir);
1007 if (err) {
1008 if (err == -ESRCH)
1009 continue;
1010 else {
1011 ret = ERR_PTR(err);
1012 goto fail;
1013 }
1014 } else if (pol->priority < priority) {
David S. Miller2518c7c2006-08-24 04:45:07 -07001015 ret = pol;
1016 break;
1017 }
1018 }
David S. Milleracba48e2006-08-25 15:46:46 -07001019 if (ret)
1020 xfrm_pol_hold(ret);
James Morris134b0fc2006-10-05 15:42:27 -05001021fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 read_unlock_bh(&xfrm_policy_lock);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001023
David S. Miller2518c7c2006-08-24 04:45:07 -07001024 return ret;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001025}
1026
Timo Teräs80c802f2010-04-07 00:30:05 +00001027static struct xfrm_policy *
David S. Miller73ff93c2011-02-22 18:33:42 -08001028__xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir)
Timo Teräs80c802f2010-04-07 00:30:05 +00001029{
1030#ifdef CONFIG_XFRM_SUB_POLICY
1031 struct xfrm_policy *pol;
1032
1033 pol = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_SUB, fl, family, dir);
1034 if (pol != NULL)
1035 return pol;
1036#endif
1037 return xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN, fl, family, dir);
1038}
1039
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001040static struct flow_cache_object *
David S. Millerdee9f4b2011-02-22 18:44:31 -08001041xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family,
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001042 u8 dir, struct flow_cache_object *old_obj, void *ctx)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001043{
1044 struct xfrm_policy *pol;
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001045
1046 if (old_obj)
1047 xfrm_pol_put(container_of(old_obj, struct xfrm_policy, flo));
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001048
Timo Teräs80c802f2010-04-07 00:30:05 +00001049 pol = __xfrm_policy_lookup(net, fl, family, dir);
1050 if (IS_ERR_OR_NULL(pol))
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001051 return ERR_CAST(pol);
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001052
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001053 /* Resolver returns two references:
1054 * one for cache and one for caller of flow_cache_lookup() */
1055 xfrm_pol_hold(pol);
1056
1057 return &pol->flo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058}
1059
Trent Jaegerdf718372005-12-13 23:12:27 -08001060static inline int policy_to_flow_dir(int dir)
1061{
1062 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001063 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1064 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1065 return dir;
1066 switch (dir) {
1067 default:
1068 case XFRM_POLICY_IN:
1069 return FLOW_DIR_IN;
1070 case XFRM_POLICY_OUT:
1071 return FLOW_DIR_OUT;
1072 case XFRM_POLICY_FWD:
1073 return FLOW_DIR_FWD;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001074 }
Trent Jaegerdf718372005-12-13 23:12:27 -08001075}
1076
David S. Millerdee9f4b2011-02-22 18:44:31 -08001077static struct xfrm_policy *xfrm_sk_policy_lookup(struct sock *sk, int dir,
1078 const struct flowi *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079{
1080 struct xfrm_policy *pol;
1081
1082 read_lock_bh(&xfrm_policy_lock);
1083 if ((pol = sk->sk_policy[dir]) != NULL) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04001084 bool match = xfrm_selector_match(&pol->selector, fl,
1085 sk->sk_family);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001086 int err = 0;
Trent Jaegerdf718372005-12-13 23:12:27 -08001087
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001088 if (match) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001089 if ((sk->sk_mark & pol->mark.m) != pol->mark.v) {
1090 pol = NULL;
1091 goto out;
1092 }
Paul Moore03e1ad72008-04-12 19:07:52 -07001093 err = security_xfrm_policy_lookup(pol->security,
David S. Miller1d28f422011-03-12 00:29:39 -05001094 fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -07001095 policy_to_flow_dir(dir));
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001096 if (!err)
1097 xfrm_pol_hold(pol);
1098 else if (err == -ESRCH)
1099 pol = NULL;
1100 else
1101 pol = ERR_PTR(err);
1102 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 pol = NULL;
1104 }
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001105out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 read_unlock_bh(&xfrm_policy_lock);
1107 return pol;
1108}
1109
1110static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
1111{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001112 struct net *net = xp_net(pol);
Alexey Dobriyan11219942008-11-25 17:33:06 -08001113 struct hlist_head *chain = policy_hash_bysel(net, &pol->selector,
David S. Miller2518c7c2006-08-24 04:45:07 -07001114 pol->family, dir);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001115
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001116 list_add(&pol->walk.all, &net->xfrm.policy_all);
David S. Miller2518c7c2006-08-24 04:45:07 -07001117 hlist_add_head(&pol->bydst, chain);
Alexey Dobriyane92303f2008-11-25 17:32:41 -08001118 hlist_add_head(&pol->byidx, net->xfrm.policy_byidx+idx_hash(net, pol->index));
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001119 net->xfrm.policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -07001121
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001122 if (xfrm_bydst_should_resize(net, dir, NULL))
1123 schedule_work(&net->xfrm.policy_hash_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124}
1125
1126static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
1127 int dir)
1128{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001129 struct net *net = xp_net(pol);
1130
David S. Miller2518c7c2006-08-24 04:45:07 -07001131 if (hlist_unhashed(&pol->bydst))
1132 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
David S. Miller2518c7c2006-08-24 04:45:07 -07001134 hlist_del(&pol->bydst);
1135 hlist_del(&pol->byidx);
Herbert Xu12a169e2008-10-01 07:03:24 -07001136 list_del(&pol->walk.all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001137 net->xfrm.policy_count[dir]--;
David S. Miller2518c7c2006-08-24 04:45:07 -07001138
1139 return pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140}
1141
Herbert Xu4666faa2005-06-18 22:43:22 -07001142int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143{
1144 write_lock_bh(&xfrm_policy_lock);
1145 pol = __xfrm_policy_unlink(pol, dir);
1146 write_unlock_bh(&xfrm_policy_lock);
1147 if (pol) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 xfrm_policy_kill(pol);
Herbert Xu4666faa2005-06-18 22:43:22 -07001149 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 }
Herbert Xu4666faa2005-06-18 22:43:22 -07001151 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152}
David S. Millera70fcb02006-03-20 19:18:52 -08001153EXPORT_SYMBOL(xfrm_policy_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
1155int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
1156{
Alexey Dobriyan11219942008-11-25 17:33:06 -08001157 struct net *net = xp_net(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 struct xfrm_policy *old_pol;
1159
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001160#ifdef CONFIG_XFRM_SUB_POLICY
1161 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
1162 return -EINVAL;
1163#endif
1164
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 write_lock_bh(&xfrm_policy_lock);
1166 old_pol = sk->sk_policy[dir];
1167 sk->sk_policy[dir] = pol;
1168 if (pol) {
James Morris9d729f72007-03-04 16:12:44 -08001169 pol->curlft.add_time = get_seconds();
Alexey Dobriyan11219942008-11-25 17:33:06 -08001170 pol->index = xfrm_gen_index(net, XFRM_POLICY_MAX+dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 __xfrm_policy_link(pol, XFRM_POLICY_MAX+dir);
1172 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001173 if (old_pol) {
1174 if (pol)
1175 xfrm_policy_requeue(old_pol, pol);
1176
Timo Teräsea2dea92010-03-31 00:17:05 +00001177 /* Unlinking succeeds always. This is the only function
1178 * allowed to delete or replace socket policy.
1179 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 __xfrm_policy_unlink(old_pol, XFRM_POLICY_MAX+dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 write_unlock_bh(&xfrm_policy_lock);
1183
1184 if (old_pol) {
1185 xfrm_policy_kill(old_pol);
1186 }
1187 return 0;
1188}
1189
David S. Millerd3e40a92011-02-24 01:25:41 -05001190static struct xfrm_policy *clone_policy(const struct xfrm_policy *old, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191{
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -08001192 struct xfrm_policy *newp = xfrm_policy_alloc(xp_net(old), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
1194 if (newp) {
1195 newp->selector = old->selector;
Paul Moore03e1ad72008-04-12 19:07:52 -07001196 if (security_xfrm_policy_clone(old->security,
1197 &newp->security)) {
Trent Jaegerdf718372005-12-13 23:12:27 -08001198 kfree(newp);
1199 return NULL; /* ENOMEM */
1200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 newp->lft = old->lft;
1202 newp->curlft = old->curlft;
Jamal Hadi Salimfb977e22010-02-23 15:09:53 -08001203 newp->mark = old->mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 newp->action = old->action;
1205 newp->flags = old->flags;
1206 newp->xfrm_nr = old->xfrm_nr;
1207 newp->index = old->index;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001208 newp->type = old->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 memcpy(newp->xfrm_vec, old->xfrm_vec,
1210 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
1211 write_lock_bh(&xfrm_policy_lock);
1212 __xfrm_policy_link(newp, XFRM_POLICY_MAX+dir);
1213 write_unlock_bh(&xfrm_policy_lock);
1214 xfrm_pol_put(newp);
1215 }
1216 return newp;
1217}
1218
1219int __xfrm_sk_clone_policy(struct sock *sk)
1220{
1221 struct xfrm_policy *p0 = sk->sk_policy[0],
1222 *p1 = sk->sk_policy[1];
1223
1224 sk->sk_policy[0] = sk->sk_policy[1] = NULL;
1225 if (p0 && (sk->sk_policy[0] = clone_policy(p0, 0)) == NULL)
1226 return -ENOMEM;
1227 if (p1 && (sk->sk_policy[1] = clone_policy(p1, 1)) == NULL)
1228 return -ENOMEM;
1229 return 0;
1230}
1231
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001232static int
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001233xfrm_get_saddr(struct net *net, xfrm_address_t *local, xfrm_address_t *remote,
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001234 unsigned short family)
1235{
1236 int err;
1237 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1238
1239 if (unlikely(afinfo == NULL))
1240 return -EINVAL;
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001241 err = afinfo->get_saddr(net, local, remote);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001242 xfrm_policy_put_afinfo(afinfo);
1243 return err;
1244}
1245
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246/* Resolve list of templates for the flow, given policy. */
1247
1248static int
David S. Millera6c2e612011-02-22 18:35:39 -08001249xfrm_tmpl_resolve_one(struct xfrm_policy *policy, const struct flowi *fl,
1250 struct xfrm_state **xfrm, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251{
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001252 struct net *net = xp_net(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 int nx;
1254 int i, error;
1255 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
1256 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001257 xfrm_address_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
1259 for (nx=0, i = 0; i < policy->xfrm_nr; i++) {
1260 struct xfrm_state *x;
1261 xfrm_address_t *remote = daddr;
1262 xfrm_address_t *local = saddr;
1263 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
1264
Joakim Koskela48b8d782007-07-26 00:08:42 -07001265 if (tmpl->mode == XFRM_MODE_TUNNEL ||
1266 tmpl->mode == XFRM_MODE_BEET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 remote = &tmpl->id.daddr;
1268 local = &tmpl->saddr;
Thomas Egerer8444cf72010-09-20 11:11:38 -07001269 if (xfrm_addr_any(local, tmpl->encap_family)) {
1270 error = xfrm_get_saddr(net, &tmp, remote, tmpl->encap_family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001271 if (error)
1272 goto fail;
1273 local = &tmp;
1274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 }
1276
1277 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
1278
1279 if (x && x->km.state == XFRM_STATE_VALID) {
1280 xfrm[nx++] = x;
1281 daddr = remote;
1282 saddr = local;
1283 continue;
1284 }
1285 if (x) {
1286 error = (x->km.state == XFRM_STATE_ERROR ?
1287 -EINVAL : -EAGAIN);
1288 xfrm_state_put(x);
1289 }
fernando@oss.ntt.coa43222662008-10-23 04:27:19 +00001290 else if (error == -ESRCH)
1291 error = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
1293 if (!tmpl->optional)
1294 goto fail;
1295 }
1296 return nx;
1297
1298fail:
1299 for (nx--; nx>=0; nx--)
1300 xfrm_state_put(xfrm[nx]);
1301 return error;
1302}
1303
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001304static int
David S. Millera6c2e612011-02-22 18:35:39 -08001305xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, const struct flowi *fl,
1306 struct xfrm_state **xfrm, unsigned short family)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001307{
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001308 struct xfrm_state *tp[XFRM_MAX_DEPTH];
1309 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001310 int cnx = 0;
1311 int error;
1312 int ret;
1313 int i;
1314
1315 for (i = 0; i < npols; i++) {
1316 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
1317 error = -ENOBUFS;
1318 goto fail;
1319 }
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001320
1321 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001322 if (ret < 0) {
1323 error = ret;
1324 goto fail;
1325 } else
1326 cnx += ret;
1327 }
1328
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001329 /* found states are sorted for outbound processing */
1330 if (npols > 1)
1331 xfrm_state_sort(xfrm, tpp, cnx, family);
1332
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001333 return cnx;
1334
1335 fail:
1336 for (cnx--; cnx>=0; cnx--)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001337 xfrm_state_put(tpp[cnx]);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001338 return error;
1339
1340}
1341
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342/* Check that the bundle accepts the flow and its components are
1343 * still valid.
1344 */
1345
David S. Miller05d84022011-02-22 17:47:10 -08001346static inline int xfrm_get_tos(const struct flowi *fl, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08001347{
1348 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1349 int tos;
1350
1351 if (!afinfo)
1352 return -EINVAL;
1353
1354 tos = afinfo->get_tos(fl);
1355
1356 xfrm_policy_put_afinfo(afinfo);
1357
1358 return tos;
1359}
1360
Timo Teräs80c802f2010-04-07 00:30:05 +00001361static struct flow_cache_object *xfrm_bundle_flo_get(struct flow_cache_object *flo)
1362{
1363 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1364 struct dst_entry *dst = &xdst->u.dst;
1365
1366 if (xdst->route == NULL) {
1367 /* Dummy bundle - if it has xfrms we were not
1368 * able to build bundle as template resolution failed.
1369 * It means we need to try again resolving. */
1370 if (xdst->num_xfrms > 0)
1371 return NULL;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001372 } else if (dst->flags & DST_XFRM_QUEUE) {
1373 return NULL;
Timo Teräs80c802f2010-04-07 00:30:05 +00001374 } else {
1375 /* Real bundle */
1376 if (stale_bundle(dst))
1377 return NULL;
1378 }
1379
1380 dst_hold(dst);
1381 return flo;
1382}
1383
1384static int xfrm_bundle_flo_check(struct flow_cache_object *flo)
1385{
1386 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1387 struct dst_entry *dst = &xdst->u.dst;
1388
1389 if (!xdst->route)
1390 return 0;
1391 if (stale_bundle(dst))
1392 return 0;
1393
1394 return 1;
1395}
1396
1397static void xfrm_bundle_flo_delete(struct flow_cache_object *flo)
1398{
1399 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1400 struct dst_entry *dst = &xdst->u.dst;
1401
1402 dst_free(dst);
1403}
1404
1405static const struct flow_cache_ops xfrm_bundle_fc_ops = {
1406 .get = xfrm_bundle_flo_get,
1407 .check = xfrm_bundle_flo_check,
1408 .delete = xfrm_bundle_flo_delete,
1409};
1410
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001411static inline struct xfrm_dst *xfrm_alloc_dst(struct net *net, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08001412{
1413 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001414 struct dst_ops *dst_ops;
Herbert Xu25ee3282007-12-11 09:32:34 -08001415 struct xfrm_dst *xdst;
1416
1417 if (!afinfo)
1418 return ERR_PTR(-EINVAL);
1419
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001420 switch (family) {
1421 case AF_INET:
1422 dst_ops = &net->xfrm.xfrm4_dst_ops;
1423 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001424#if IS_ENABLED(CONFIG_IPV6)
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001425 case AF_INET6:
1426 dst_ops = &net->xfrm.xfrm6_dst_ops;
1427 break;
1428#endif
1429 default:
1430 BUG();
1431 }
David S. Millerf5b0a872012-07-19 12:31:33 -07001432 xdst = dst_alloc(dst_ops, NULL, 0, DST_OBSOLETE_NONE, 0);
Herbert Xu25ee3282007-12-11 09:32:34 -08001433
Madalin Bucurd4cae562011-09-26 07:04:36 +00001434 if (likely(xdst)) {
Steffen Klassert141e3692012-07-05 23:39:34 +00001435 struct dst_entry *dst = &xdst->u.dst;
1436
1437 memset(dst + 1, 0, sizeof(*xdst) - sizeof(*dst));
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08001438 xdst->flo.ops = &xfrm_bundle_fc_ops;
Patrick McHardy9d7b0fc2012-08-20 02:56:56 -07001439 if (afinfo->init_dst)
1440 afinfo->init_dst(net, xdst);
Madalin Bucurd4cae562011-09-26 07:04:36 +00001441 } else
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08001442 xdst = ERR_PTR(-ENOBUFS);
Timo Teräs80c802f2010-04-07 00:30:05 +00001443
Madalin Bucurd4cae562011-09-26 07:04:36 +00001444 xfrm_policy_put_afinfo(afinfo);
1445
Herbert Xu25ee3282007-12-11 09:32:34 -08001446 return xdst;
1447}
1448
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001449static inline int xfrm_init_path(struct xfrm_dst *path, struct dst_entry *dst,
1450 int nfheader_len)
1451{
1452 struct xfrm_policy_afinfo *afinfo =
1453 xfrm_policy_get_afinfo(dst->ops->family);
1454 int err;
1455
1456 if (!afinfo)
1457 return -EINVAL;
1458
1459 err = afinfo->init_path(path, dst, nfheader_len);
1460
1461 xfrm_policy_put_afinfo(afinfo);
1462
1463 return err;
1464}
1465
Herbert Xu87c1e122010-03-02 02:51:56 +00001466static inline int xfrm_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
David S. Miller0c7b3ee2011-02-22 17:48:57 -08001467 const struct flowi *fl)
Herbert Xu25ee3282007-12-11 09:32:34 -08001468{
1469 struct xfrm_policy_afinfo *afinfo =
1470 xfrm_policy_get_afinfo(xdst->u.dst.ops->family);
1471 int err;
1472
1473 if (!afinfo)
1474 return -EINVAL;
1475
Herbert Xu87c1e122010-03-02 02:51:56 +00001476 err = afinfo->fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001477
1478 xfrm_policy_put_afinfo(afinfo);
1479
1480 return err;
1481}
1482
Timo Teräs80c802f2010-04-07 00:30:05 +00001483
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484/* Allocate chain of dst_entry's, attach known xfrm's, calculate
1485 * all the metrics... Shortly, bundle a bundle.
1486 */
1487
Herbert Xu25ee3282007-12-11 09:32:34 -08001488static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy,
1489 struct xfrm_state **xfrm, int nx,
David S. Miller98313ad2011-02-22 18:36:50 -08001490 const struct flowi *fl,
Herbert Xu25ee3282007-12-11 09:32:34 -08001491 struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492{
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001493 struct net *net = xp_net(policy);
Herbert Xu25ee3282007-12-11 09:32:34 -08001494 unsigned long now = jiffies;
1495 struct net_device *dev;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001496 struct xfrm_mode *inner_mode;
Herbert Xu25ee3282007-12-11 09:32:34 -08001497 struct dst_entry *dst_prev = NULL;
1498 struct dst_entry *dst0 = NULL;
1499 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 int err;
Herbert Xu25ee3282007-12-11 09:32:34 -08001501 int header_len = 0;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001502 int nfheader_len = 0;
Herbert Xu25ee3282007-12-11 09:32:34 -08001503 int trailer_len = 0;
1504 int tos;
1505 int family = policy->selector.family;
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +09001506 xfrm_address_t saddr, daddr;
1507
1508 xfrm_flowi_addr_get(fl, &saddr, &daddr, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001509
1510 tos = xfrm_get_tos(fl, family);
1511 err = tos;
1512 if (tos < 0)
1513 goto put_states;
1514
1515 dst_hold(dst);
1516
1517 for (; i < nx; i++) {
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001518 struct xfrm_dst *xdst = xfrm_alloc_dst(net, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001519 struct dst_entry *dst1 = &xdst->u.dst;
1520
1521 err = PTR_ERR(xdst);
1522 if (IS_ERR(xdst)) {
1523 dst_release(dst);
1524 goto put_states;
1525 }
1526
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001527 if (xfrm[i]->sel.family == AF_UNSPEC) {
1528 inner_mode = xfrm_ip2inner_mode(xfrm[i],
1529 xfrm_af2proto(family));
1530 if (!inner_mode) {
1531 err = -EAFNOSUPPORT;
1532 dst_release(dst);
1533 goto put_states;
1534 }
1535 } else
1536 inner_mode = xfrm[i]->inner_mode;
1537
Herbert Xu25ee3282007-12-11 09:32:34 -08001538 if (!dst_prev)
1539 dst0 = dst1;
1540 else {
1541 dst_prev->child = dst_clone(dst1);
1542 dst1->flags |= DST_NOHASH;
1543 }
1544
1545 xdst->route = dst;
David S. Millerdefb3512010-12-08 21:16:57 -08001546 dst_copy_metrics(dst1, dst);
Herbert Xu25ee3282007-12-11 09:32:34 -08001547
1548 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
1549 family = xfrm[i]->props.family;
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +09001550 dst = xfrm_dst_lookup(xfrm[i], tos, &saddr, &daddr,
1551 family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001552 err = PTR_ERR(dst);
1553 if (IS_ERR(dst))
1554 goto put_states;
1555 } else
1556 dst_hold(dst);
1557
1558 dst1->xfrm = xfrm[i];
Timo Teräs80c802f2010-04-07 00:30:05 +00001559 xdst->xfrm_genid = xfrm[i]->genid;
Herbert Xu25ee3282007-12-11 09:32:34 -08001560
David S. Millerf5b0a872012-07-19 12:31:33 -07001561 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
Herbert Xu25ee3282007-12-11 09:32:34 -08001562 dst1->flags |= DST_HOST;
1563 dst1->lastuse = now;
1564
1565 dst1->input = dst_discard;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001566 dst1->output = inner_mode->afinfo->output;
Herbert Xu25ee3282007-12-11 09:32:34 -08001567
1568 dst1->next = dst_prev;
1569 dst_prev = dst1;
1570
1571 header_len += xfrm[i]->props.header_len;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001572 if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT)
1573 nfheader_len += xfrm[i]->props.header_len;
Herbert Xu25ee3282007-12-11 09:32:34 -08001574 trailer_len += xfrm[i]->props.trailer_len;
1575 }
1576
1577 dst_prev->child = dst;
1578 dst0->path = dst;
1579
1580 err = -ENODEV;
1581 dev = dst->dev;
1582 if (!dev)
1583 goto free_dst;
1584
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001585 xfrm_init_path((struct xfrm_dst *)dst0, dst, nfheader_len);
Herbert Xu25ee3282007-12-11 09:32:34 -08001586 xfrm_init_pmtu(dst_prev);
1587
1588 for (dst_prev = dst0; dst_prev != dst; dst_prev = dst_prev->child) {
1589 struct xfrm_dst *xdst = (struct xfrm_dst *)dst_prev;
1590
Herbert Xu87c1e122010-03-02 02:51:56 +00001591 err = xfrm_fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001592 if (err)
1593 goto free_dst;
1594
1595 dst_prev->header_len = header_len;
1596 dst_prev->trailer_len = trailer_len;
1597 header_len -= xdst->u.dst.xfrm->props.header_len;
1598 trailer_len -= xdst->u.dst.xfrm->props.trailer_len;
1599 }
1600
1601out:
1602 return dst0;
1603
1604put_states:
1605 for (; i < nx; i++)
1606 xfrm_state_put(xfrm[i]);
1607free_dst:
1608 if (dst0)
1609 dst_free(dst0);
1610 dst0 = ERR_PTR(err);
1611 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612}
1613
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001614static int inline
David S. Miller3f0e18f2011-02-22 18:38:14 -08001615xfrm_dst_alloc_copy(void **target, const void *src, int size)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001616{
1617 if (!*target) {
1618 *target = kmalloc(size, GFP_ATOMIC);
1619 if (!*target)
1620 return -ENOMEM;
1621 }
1622 memcpy(*target, src, size);
1623 return 0;
1624}
1625
1626static int inline
David S. Miller1786b382011-02-24 01:32:54 -05001627xfrm_dst_update_parent(struct dst_entry *dst, const struct xfrm_selector *sel)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001628{
1629#ifdef CONFIG_XFRM_SUB_POLICY
1630 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1631 return xfrm_dst_alloc_copy((void **)&(xdst->partner),
1632 sel, sizeof(*sel));
1633#else
1634 return 0;
1635#endif
1636}
1637
1638static int inline
David S. Miller3f0e18f2011-02-22 18:38:14 -08001639xfrm_dst_update_origin(struct dst_entry *dst, const struct flowi *fl)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001640{
1641#ifdef CONFIG_XFRM_SUB_POLICY
1642 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1643 return xfrm_dst_alloc_copy((void **)&(xdst->origin), fl, sizeof(*fl));
1644#else
1645 return 0;
1646#endif
1647}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
David S. Miller73ff93c2011-02-22 18:33:42 -08001649static int xfrm_expand_policies(const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001650 struct xfrm_policy **pols,
1651 int *num_pols, int *num_xfrms)
1652{
1653 int i;
1654
1655 if (*num_pols == 0 || !pols[0]) {
1656 *num_pols = 0;
1657 *num_xfrms = 0;
1658 return 0;
1659 }
1660 if (IS_ERR(pols[0]))
1661 return PTR_ERR(pols[0]);
1662
1663 *num_xfrms = pols[0]->xfrm_nr;
1664
1665#ifdef CONFIG_XFRM_SUB_POLICY
1666 if (pols[0] && pols[0]->action == XFRM_POLICY_ALLOW &&
1667 pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1668 pols[1] = xfrm_policy_lookup_bytype(xp_net(pols[0]),
1669 XFRM_POLICY_TYPE_MAIN,
1670 fl, family,
1671 XFRM_POLICY_OUT);
1672 if (pols[1]) {
1673 if (IS_ERR(pols[1])) {
1674 xfrm_pols_put(pols, *num_pols);
1675 return PTR_ERR(pols[1]);
1676 }
1677 (*num_pols) ++;
1678 (*num_xfrms) += pols[1]->xfrm_nr;
1679 }
1680 }
1681#endif
1682 for (i = 0; i < *num_pols; i++) {
1683 if (pols[i]->action != XFRM_POLICY_ALLOW) {
1684 *num_xfrms = -1;
1685 break;
1686 }
1687 }
1688
1689 return 0;
1690
1691}
1692
1693static struct xfrm_dst *
1694xfrm_resolve_and_create_bundle(struct xfrm_policy **pols, int num_pols,
David S. Miller4ca2e682011-02-22 18:38:51 -08001695 const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001696 struct dst_entry *dst_orig)
1697{
1698 struct net *net = xp_net(pols[0]);
1699 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
1700 struct dst_entry *dst;
1701 struct xfrm_dst *xdst;
1702 int err;
1703
1704 /* Try to instantiate a bundle */
1705 err = xfrm_tmpl_resolve(pols, num_pols, fl, xfrm, family);
Timo Teräsd809ec82010-07-12 21:29:42 +00001706 if (err <= 0) {
1707 if (err != 0 && err != -EAGAIN)
Timo Teräs80c802f2010-04-07 00:30:05 +00001708 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
1709 return ERR_PTR(err);
1710 }
1711
1712 dst = xfrm_bundle_create(pols[0], xfrm, err, fl, dst_orig);
1713 if (IS_ERR(dst)) {
1714 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLEGENERROR);
1715 return ERR_CAST(dst);
1716 }
1717
1718 xdst = (struct xfrm_dst *)dst;
1719 xdst->num_xfrms = err;
1720 if (num_pols > 1)
1721 err = xfrm_dst_update_parent(dst, &pols[1]->selector);
1722 else
1723 err = xfrm_dst_update_origin(dst, fl);
1724 if (unlikely(err)) {
1725 dst_free(dst);
1726 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLECHECKERROR);
1727 return ERR_PTR(err);
1728 }
1729
1730 xdst->num_pols = num_pols;
1731 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy*) * num_pols);
1732 xdst->policy_genid = atomic_read(&pols[0]->genid);
1733
1734 return xdst;
1735}
1736
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001737static void xfrm_policy_queue_process(unsigned long arg)
1738{
1739 int err = 0;
1740 struct sk_buff *skb;
1741 struct sock *sk;
1742 struct dst_entry *dst;
1743 struct net_device *dev;
1744 struct xfrm_policy *pol = (struct xfrm_policy *)arg;
1745 struct xfrm_policy_queue *pq = &pol->polq;
1746 struct flowi fl;
1747 struct sk_buff_head list;
1748
1749 spin_lock(&pq->hold_queue.lock);
1750 skb = skb_peek(&pq->hold_queue);
1751 dst = skb_dst(skb);
1752 sk = skb->sk;
1753 xfrm_decode_session(skb, &fl, dst->ops->family);
1754 spin_unlock(&pq->hold_queue.lock);
1755
1756 dst_hold(dst->path);
1757 dst = xfrm_lookup(xp_net(pol), dst->path, &fl,
1758 sk, 0);
1759 if (IS_ERR(dst))
1760 goto purge_queue;
1761
1762 if (dst->flags & DST_XFRM_QUEUE) {
1763 dst_release(dst);
1764
1765 if (pq->timeout >= XFRM_QUEUE_TMO_MAX)
1766 goto purge_queue;
1767
1768 pq->timeout = pq->timeout << 1;
1769 mod_timer(&pq->hold_timer, jiffies + pq->timeout);
1770 return;
1771 }
1772
1773 dst_release(dst);
1774
1775 __skb_queue_head_init(&list);
1776
1777 spin_lock(&pq->hold_queue.lock);
1778 pq->timeout = 0;
1779 skb_queue_splice_init(&pq->hold_queue, &list);
1780 spin_unlock(&pq->hold_queue.lock);
1781
1782 while (!skb_queue_empty(&list)) {
1783 skb = __skb_dequeue(&list);
1784
1785 xfrm_decode_session(skb, &fl, skb_dst(skb)->ops->family);
1786 dst_hold(skb_dst(skb)->path);
1787 dst = xfrm_lookup(xp_net(pol), skb_dst(skb)->path,
1788 &fl, skb->sk, 0);
1789 if (IS_ERR(dst)) {
1790 dev_put(skb->dev);
1791 kfree_skb(skb);
1792 continue;
1793 }
1794
1795 nf_reset(skb);
1796 skb_dst_drop(skb);
1797 skb_dst_set(skb, dst);
1798
1799 dev = skb->dev;
1800 err = dst_output(skb);
1801 dev_put(dev);
1802 }
1803
1804 return;
1805
1806purge_queue:
1807 pq->timeout = 0;
1808 xfrm_queue_purge(&pq->hold_queue);
1809}
1810
1811static int xdst_queue_output(struct sk_buff *skb)
1812{
1813 unsigned long sched_next;
1814 struct dst_entry *dst = skb_dst(skb);
1815 struct xfrm_dst *xdst = (struct xfrm_dst *) dst;
1816 struct xfrm_policy_queue *pq = &xdst->pols[0]->polq;
1817
1818 if (pq->hold_queue.qlen > XFRM_MAX_QUEUE_LEN) {
1819 kfree_skb(skb);
1820 return -EAGAIN;
1821 }
1822
1823 skb_dst_force(skb);
1824 dev_hold(skb->dev);
1825
1826 spin_lock_bh(&pq->hold_queue.lock);
1827
1828 if (!pq->timeout)
1829 pq->timeout = XFRM_QUEUE_TMO_MIN;
1830
1831 sched_next = jiffies + pq->timeout;
1832
1833 if (del_timer(&pq->hold_timer)) {
1834 if (time_before(pq->hold_timer.expires, sched_next))
1835 sched_next = pq->hold_timer.expires;
1836 }
1837
1838 __skb_queue_tail(&pq->hold_queue, skb);
1839 mod_timer(&pq->hold_timer, sched_next);
1840
1841 spin_unlock_bh(&pq->hold_queue.lock);
1842
1843 return 0;
1844}
1845
1846static struct xfrm_dst *xfrm_create_dummy_bundle(struct net *net,
1847 struct dst_entry *dst,
1848 const struct flowi *fl,
1849 int num_xfrms,
1850 u16 family)
1851{
1852 int err;
1853 struct net_device *dev;
1854 struct dst_entry *dst1;
1855 struct xfrm_dst *xdst;
1856
1857 xdst = xfrm_alloc_dst(net, family);
1858 if (IS_ERR(xdst))
1859 return xdst;
1860
1861 if (net->xfrm.sysctl_larval_drop || num_xfrms <= 0 ||
1862 (fl->flowi_flags & FLOWI_FLAG_CAN_SLEEP))
1863 return xdst;
1864
1865 dst1 = &xdst->u.dst;
1866 dst_hold(dst);
1867 xdst->route = dst;
1868
1869 dst_copy_metrics(dst1, dst);
1870
1871 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
1872 dst1->flags |= DST_HOST | DST_XFRM_QUEUE;
1873 dst1->lastuse = jiffies;
1874
1875 dst1->input = dst_discard;
1876 dst1->output = xdst_queue_output;
1877
1878 dst_hold(dst);
1879 dst1->child = dst;
1880 dst1->path = dst;
1881
1882 xfrm_init_path((struct xfrm_dst *)dst1, dst, 0);
1883
1884 err = -ENODEV;
1885 dev = dst->dev;
1886 if (!dev)
1887 goto free_dst;
1888
1889 err = xfrm_fill_dst(xdst, dev, fl);
1890 if (err)
1891 goto free_dst;
1892
1893out:
1894 return xdst;
1895
1896free_dst:
1897 dst_release(dst1);
1898 xdst = ERR_PTR(err);
1899 goto out;
1900}
1901
Timo Teräs80c802f2010-04-07 00:30:05 +00001902static struct flow_cache_object *
David S. Millerdee9f4b2011-02-22 18:44:31 -08001903xfrm_bundle_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir,
Timo Teräs80c802f2010-04-07 00:30:05 +00001904 struct flow_cache_object *oldflo, void *ctx)
1905{
1906 struct dst_entry *dst_orig = (struct dst_entry *)ctx;
1907 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
1908 struct xfrm_dst *xdst, *new_xdst;
1909 int num_pols = 0, num_xfrms = 0, i, err, pol_dead;
1910
1911 /* Check if the policies from old bundle are usable */
1912 xdst = NULL;
1913 if (oldflo) {
1914 xdst = container_of(oldflo, struct xfrm_dst, flo);
1915 num_pols = xdst->num_pols;
1916 num_xfrms = xdst->num_xfrms;
1917 pol_dead = 0;
1918 for (i = 0; i < num_pols; i++) {
1919 pols[i] = xdst->pols[i];
1920 pol_dead |= pols[i]->walk.dead;
1921 }
1922 if (pol_dead) {
1923 dst_free(&xdst->u.dst);
1924 xdst = NULL;
1925 num_pols = 0;
1926 num_xfrms = 0;
1927 oldflo = NULL;
1928 }
1929 }
1930
1931 /* Resolve policies to use if we couldn't get them from
1932 * previous cache entry */
1933 if (xdst == NULL) {
1934 num_pols = 1;
1935 pols[0] = __xfrm_policy_lookup(net, fl, family, dir);
1936 err = xfrm_expand_policies(fl, family, pols,
1937 &num_pols, &num_xfrms);
1938 if (err < 0)
1939 goto inc_error;
1940 if (num_pols == 0)
1941 return NULL;
1942 if (num_xfrms <= 0)
1943 goto make_dummy_bundle;
1944 }
1945
1946 new_xdst = xfrm_resolve_and_create_bundle(pols, num_pols, fl, family, dst_orig);
1947 if (IS_ERR(new_xdst)) {
1948 err = PTR_ERR(new_xdst);
1949 if (err != -EAGAIN)
1950 goto error;
1951 if (oldflo == NULL)
1952 goto make_dummy_bundle;
1953 dst_hold(&xdst->u.dst);
1954 return oldflo;
Timo Teräsd809ec82010-07-12 21:29:42 +00001955 } else if (new_xdst == NULL) {
1956 num_xfrms = 0;
1957 if (oldflo == NULL)
1958 goto make_dummy_bundle;
1959 xdst->num_xfrms = 0;
1960 dst_hold(&xdst->u.dst);
1961 return oldflo;
Timo Teräs80c802f2010-04-07 00:30:05 +00001962 }
1963
1964 /* Kill the previous bundle */
1965 if (xdst) {
1966 /* The policies were stolen for newly generated bundle */
1967 xdst->num_pols = 0;
1968 dst_free(&xdst->u.dst);
1969 }
1970
1971 /* Flow cache does not have reference, it dst_free()'s,
1972 * but we do need to return one reference for original caller */
1973 dst_hold(&new_xdst->u.dst);
1974 return &new_xdst->flo;
1975
1976make_dummy_bundle:
1977 /* We found policies, but there's no bundles to instantiate:
1978 * either because the policy blocks, has no transformations or
1979 * we could not build template (no xfrm_states).*/
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001980 xdst = xfrm_create_dummy_bundle(net, dst_orig, fl, num_xfrms, family);
Timo Teräs80c802f2010-04-07 00:30:05 +00001981 if (IS_ERR(xdst)) {
1982 xfrm_pols_put(pols, num_pols);
1983 return ERR_CAST(xdst);
1984 }
1985 xdst->num_pols = num_pols;
1986 xdst->num_xfrms = num_xfrms;
1987 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy*) * num_pols);
1988
1989 dst_hold(&xdst->u.dst);
1990 return &xdst->flo;
1991
1992inc_error:
1993 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
1994error:
1995 if (xdst != NULL)
1996 dst_free(&xdst->u.dst);
1997 else
1998 xfrm_pols_put(pols, num_pols);
1999 return ERR_PTR(err);
2000}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
David S. Miller2774c132011-03-01 14:59:04 -08002002static struct dst_entry *make_blackhole(struct net *net, u16 family,
2003 struct dst_entry *dst_orig)
2004{
2005 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
2006 struct dst_entry *ret;
2007
2008 if (!afinfo) {
2009 dst_release(dst_orig);
Li RongQing433a1952012-09-17 22:40:10 +00002010 return ERR_PTR(-EINVAL);
David S. Miller2774c132011-03-01 14:59:04 -08002011 } else {
2012 ret = afinfo->blackhole_route(net, dst_orig);
2013 }
2014 xfrm_policy_put_afinfo(afinfo);
2015
2016 return ret;
2017}
2018
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019/* Main function: finds/creates a bundle for given flow.
2020 *
2021 * At the moment we eat a raw IP route. Mostly to speed up lookups
2022 * on interfaces with disabled IPsec.
2023 */
David S. Miller452edd52011-03-02 13:27:41 -08002024struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
2025 const struct flowi *fl,
2026 struct sock *sk, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027{
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002028 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
Timo Teräs80c802f2010-04-07 00:30:05 +00002029 struct flow_cache_object *flo;
2030 struct xfrm_dst *xdst;
David S. Miller452edd52011-03-02 13:27:41 -08002031 struct dst_entry *dst, *route;
Timo Teräs80c802f2010-04-07 00:30:05 +00002032 u16 family = dst_orig->ops->family;
Trent Jaegerdf718372005-12-13 23:12:27 -08002033 u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
Changli Gao4b021622010-04-27 21:20:22 +00002034 int i, err, num_pols, num_xfrms = 0, drop_pols = 0;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002035
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036restart:
Timo Teräs80c802f2010-04-07 00:30:05 +00002037 dst = NULL;
2038 xdst = NULL;
2039 route = NULL;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002040
Thomas Graff7944fb2007-08-25 13:46:55 -07002041 if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002042 num_pols = 1;
2043 pols[0] = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
2044 err = xfrm_expand_policies(fl, family, pols,
2045 &num_pols, &num_xfrms);
2046 if (err < 0)
Herbert Xu75b8c132007-12-11 04:38:08 -08002047 goto dropdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002048
2049 if (num_pols) {
2050 if (num_xfrms <= 0) {
2051 drop_pols = num_pols;
2052 goto no_transform;
2053 }
2054
2055 xdst = xfrm_resolve_and_create_bundle(
2056 pols, num_pols, fl,
2057 family, dst_orig);
2058 if (IS_ERR(xdst)) {
2059 xfrm_pols_put(pols, num_pols);
2060 err = PTR_ERR(xdst);
2061 goto dropdst;
Timo Teräsd809ec82010-07-12 21:29:42 +00002062 } else if (xdst == NULL) {
2063 num_xfrms = 0;
2064 drop_pols = num_pols;
2065 goto no_transform;
Timo Teräs80c802f2010-04-07 00:30:05 +00002066 }
2067
Steffen Klassertfbd50602011-03-15 21:12:49 +00002068 dst_hold(&xdst->u.dst);
2069
Timo Teräs80c802f2010-04-07 00:30:05 +00002070 spin_lock_bh(&xfrm_policy_sk_bundle_lock);
2071 xdst->u.dst.next = xfrm_policy_sk_bundles;
2072 xfrm_policy_sk_bundles = &xdst->u.dst;
2073 spin_unlock_bh(&xfrm_policy_sk_bundle_lock);
2074
2075 route = xdst->route;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002076 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002077 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
Timo Teräs80c802f2010-04-07 00:30:05 +00002079 if (xdst == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 /* To accelerate a bit... */
David S. Miller2518c7c2006-08-24 04:45:07 -07002081 if ((dst_orig->flags & DST_NOXFRM) ||
Alexey Dobriyan52479b62008-11-25 17:35:18 -08002082 !net->xfrm.policy_count[XFRM_POLICY_OUT])
Herbert Xu8b7817f2007-12-12 10:44:43 -08002083 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
Timo Teräs80c802f2010-04-07 00:30:05 +00002085 flo = flow_cache_lookup(net, fl, family, dir,
2086 xfrm_bundle_lookup, dst_orig);
2087 if (flo == NULL)
2088 goto nopol;
Timo Teräsfe1a5f02010-04-07 00:30:04 +00002089 if (IS_ERR(flo)) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002090 err = PTR_ERR(flo);
Herbert Xu75b8c132007-12-11 04:38:08 -08002091 goto dropdst;
Masahide NAKAMURAd66e37a2008-01-07 21:46:15 -08002092 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002093 xdst = container_of(flo, struct xfrm_dst, flo);
2094
2095 num_pols = xdst->num_pols;
2096 num_xfrms = xdst->num_xfrms;
2097 memcpy(pols, xdst->pols, sizeof(struct xfrm_policy*) * num_pols);
2098 route = xdst->route;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 }
2100
Timo Teräs80c802f2010-04-07 00:30:05 +00002101 dst = &xdst->u.dst;
2102 if (route == NULL && num_xfrms > 0) {
2103 /* The only case when xfrm_bundle_lookup() returns a
2104 * bundle with null route, is when the template could
2105 * not be resolved. It means policies are there, but
2106 * bundle could not be created, since we don't yet
2107 * have the xfrm_state's. We need to wait for KM to
2108 * negotiate new SA's or bail out with error.*/
2109 if (net->xfrm.sysctl_larval_drop) {
2110 /* EREMOTE tells the caller to generate
2111 * a one-shot blackhole route. */
2112 dst_release(dst);
Timo Terasa1aa3482010-05-15 23:49:26 -07002113 xfrm_pols_put(pols, drop_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002114 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
David S. Miller2774c132011-03-01 14:59:04 -08002115
David S. Miller452edd52011-03-02 13:27:41 -08002116 return make_blackhole(net, family, dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002117 }
David S. Miller1d28f422011-03-12 00:29:39 -05002118 if (fl->flowi_flags & FLOWI_FLAG_CAN_SLEEP) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002119 DECLARE_WAITQUEUE(wait, current);
2120
2121 add_wait_queue(&net->xfrm.km_waitq, &wait);
2122 set_current_state(TASK_INTERRUPTIBLE);
2123 schedule();
2124 set_current_state(TASK_RUNNING);
2125 remove_wait_queue(&net->xfrm.km_waitq, &wait);
2126
2127 if (!signal_pending(current)) {
2128 dst_release(dst);
2129 goto restart;
2130 }
2131
2132 err = -ERESTART;
2133 } else
2134 err = -EAGAIN;
2135
2136 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
2137 goto error;
2138 }
2139
2140no_transform:
2141 if (num_pols == 0)
Herbert Xu8b7817f2007-12-12 10:44:43 -08002142 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143
Timo Teräs80c802f2010-04-07 00:30:05 +00002144 if ((flags & XFRM_LOOKUP_ICMP) &&
2145 !(pols[0]->flags & XFRM_POLICY_ICMP)) {
2146 err = -ENOENT;
Herbert Xu8b7817f2007-12-12 10:44:43 -08002147 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002148 }
Herbert Xu8b7817f2007-12-12 10:44:43 -08002149
Timo Teräs80c802f2010-04-07 00:30:05 +00002150 for (i = 0; i < num_pols; i++)
2151 pols[i]->curlft.use_time = get_seconds();
Herbert Xu8b7817f2007-12-12 10:44:43 -08002152
Timo Teräs80c802f2010-04-07 00:30:05 +00002153 if (num_xfrms < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 /* Prohibit the flow */
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002155 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLBLOCK);
Patrick McHardye104411b2005-09-08 15:11:55 -07002156 err = -EPERM;
2157 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002158 } else if (num_xfrms > 0) {
2159 /* Flow transformed */
Timo Teräs80c802f2010-04-07 00:30:05 +00002160 dst_release(dst_orig);
2161 } else {
2162 /* Flow passes untransformed */
2163 dst_release(dst);
David S. Miller452edd52011-03-02 13:27:41 -08002164 dst = dst_orig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002166ok:
2167 xfrm_pols_put(pols, drop_pols);
Gao feng0c183372012-05-26 01:30:53 +00002168 if (dst && dst->xfrm &&
2169 dst->xfrm->props.mode == XFRM_MODE_TUNNEL)
2170 dst->flags |= DST_XFRM_TUNNEL;
David S. Miller452edd52011-03-02 13:27:41 -08002171 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
Timo Teräs80c802f2010-04-07 00:30:05 +00002173nopol:
David S. Miller452edd52011-03-02 13:27:41 -08002174 if (!(flags & XFRM_LOOKUP_ICMP)) {
2175 dst = dst_orig;
Timo Teräs80c802f2010-04-07 00:30:05 +00002176 goto ok;
David S. Miller452edd52011-03-02 13:27:41 -08002177 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002178 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179error:
Timo Teräs80c802f2010-04-07 00:30:05 +00002180 dst_release(dst);
Herbert Xu75b8c132007-12-11 04:38:08 -08002181dropdst:
2182 dst_release(dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002183 xfrm_pols_put(pols, drop_pols);
David S. Miller452edd52011-03-02 13:27:41 -08002184 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185}
2186EXPORT_SYMBOL(xfrm_lookup);
2187
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002188static inline int
David S. Miller8f029de2011-02-22 17:59:59 -08002189xfrm_secpath_reject(int idx, struct sk_buff *skb, const struct flowi *fl)
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002190{
2191 struct xfrm_state *x;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002192
2193 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
2194 return 0;
2195 x = skb->sp->xvec[idx];
2196 if (!x->type->reject)
2197 return 0;
Herbert Xu1ecafed2007-10-09 13:24:07 -07002198 return x->type->reject(x, skb, fl);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002199}
2200
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201/* When skb is transformed back to its "native" form, we have to
2202 * check policy restrictions. At the moment we make this in maximally
2203 * stupid way. Shame on me. :-) Of course, connected sockets must
2204 * have policy cached at them.
2205 */
2206
2207static inline int
David S. Miller7db454b2011-02-24 01:43:01 -05002208xfrm_state_ok(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 unsigned short family)
2210{
2211 if (xfrm_state_kern(x))
Kazunori MIYAZAWA928ba412007-02-13 12:57:16 -08002212 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 return x->id.proto == tmpl->id.proto &&
2214 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
2215 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
2216 x->props.mode == tmpl->mode &&
Herbert Xuc5d18e92008-04-22 00:46:42 -07002217 (tmpl->allalgs || (tmpl->aalgos & (1<<x->props.aalgo)) ||
Masahide NAKAMURAf3bd4842006-08-23 18:00:48 -07002218 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002219 !(x->props.mode != XFRM_MODE_TRANSPORT &&
2220 xfrm_state_addr_cmp(tmpl, x, family));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221}
2222
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002223/*
2224 * 0 or more than 0 is returned when validation is succeeded (either bypass
2225 * because of optional transport mode, or next index of the mathced secpath
2226 * state with the template.
2227 * -1 is returned when no matching template is found.
2228 * Otherwise "-2 - errored_index" is returned.
2229 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230static inline int
David S. Miller22cccb72011-02-24 01:43:33 -05002231xfrm_policy_ok(const struct xfrm_tmpl *tmpl, const struct sec_path *sp, int start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 unsigned short family)
2233{
2234 int idx = start;
2235
2236 if (tmpl->optional) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002237 if (tmpl->mode == XFRM_MODE_TRANSPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 return start;
2239 } else
2240 start = -1;
2241 for (; idx < sp->len; idx++) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002242 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 return ++idx;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002244 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
2245 if (start == -1)
2246 start = -2-idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 break;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 }
2250 return start;
2251}
2252
Herbert Xud5422ef2007-12-12 10:44:16 -08002253int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
2254 unsigned int family, int reverse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255{
2256 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002257 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
2259 if (unlikely(afinfo == NULL))
2260 return -EAFNOSUPPORT;
2261
Herbert Xud5422ef2007-12-12 10:44:16 -08002262 afinfo->decode_session(skb, fl, reverse);
David S. Miller1d28f422011-03-12 00:29:39 -05002263 err = security_xfrm_decode_session(skb, &fl->flowi_secid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 xfrm_policy_put_afinfo(afinfo);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002265 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266}
Herbert Xud5422ef2007-12-12 10:44:16 -08002267EXPORT_SYMBOL(__xfrm_decode_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
David S. Miller9a7386e2011-02-24 01:44:12 -05002269static inline int secpath_has_nontransport(const struct sec_path *sp, int k, int *idxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270{
2271 for (; k < sp->len; k++) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002272 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002273 *idxp = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 return 1;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 }
2277
2278 return 0;
2279}
2280
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002281int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 unsigned short family)
2283{
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002284 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 struct xfrm_policy *pol;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002286 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2287 int npols = 0;
2288 int xfrm_nr;
2289 int pi;
Herbert Xud5422ef2007-12-12 10:44:16 -08002290 int reverse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 struct flowi fl;
Herbert Xud5422ef2007-12-12 10:44:16 -08002292 u8 fl_dir;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002293 int xerr_idx = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294
Herbert Xud5422ef2007-12-12 10:44:16 -08002295 reverse = dir & ~XFRM_POLICY_MASK;
2296 dir &= XFRM_POLICY_MASK;
2297 fl_dir = policy_to_flow_dir(dir);
2298
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002299 if (__xfrm_decode_session(skb, &fl, family, reverse) < 0) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002300 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002302 }
2303
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -08002304 nf_nat_decode_session(skb, &fl, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305
2306 /* First, check used SA against their selectors. */
2307 if (skb->sp) {
2308 int i;
2309
2310 for (i=skb->sp->len-1; i>=0; i--) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002311 struct xfrm_state *x = skb->sp->xvec[i];
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002312 if (!xfrm_selector_match(&x->sel, &fl, family)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002313 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 }
2317 }
2318
2319 pol = NULL;
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002320 if (sk && sk->sk_policy[dir]) {
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002321 pol = xfrm_sk_policy_lookup(sk, dir, &fl);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002322 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002323 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002324 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002325 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
Timo Teräsfe1a5f02010-04-07 00:30:04 +00002328 if (!pol) {
2329 struct flow_cache_object *flo;
2330
2331 flo = flow_cache_lookup(net, &fl, family, fl_dir,
2332 xfrm_policy_lookup, NULL);
2333 if (IS_ERR_OR_NULL(flo))
2334 pol = ERR_CAST(flo);
2335 else
2336 pol = container_of(flo, struct xfrm_policy, flo);
2337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002339 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002340 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002341 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002342 }
James Morris134b0fc2006-10-05 15:42:27 -05002343
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002344 if (!pol) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002345 if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002346 xfrm_secpath_reject(xerr_idx, skb, &fl);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002347 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOPOLS);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002348 return 0;
2349 }
2350 return 1;
2351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
James Morris9d729f72007-03-04 16:12:44 -08002353 pol->curlft.use_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002355 pols[0] = pol;
2356 npols ++;
2357#ifdef CONFIG_XFRM_SUB_POLICY
2358 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002359 pols[1] = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002360 &fl, family,
2361 XFRM_POLICY_IN);
2362 if (pols[1]) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002363 if (IS_ERR(pols[1])) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002364 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002365 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002366 }
James Morris9d729f72007-03-04 16:12:44 -08002367 pols[1]->curlft.use_time = get_seconds();
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002368 npols ++;
2369 }
2370 }
2371#endif
2372
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 if (pol->action == XFRM_POLICY_ALLOW) {
2374 struct sec_path *sp;
2375 static struct sec_path dummy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002376 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002377 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002378 struct xfrm_tmpl **tpp = tp;
2379 int ti = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 int i, k;
2381
2382 if ((sp = skb->sp) == NULL)
2383 sp = &dummy;
2384
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002385 for (pi = 0; pi < npols; pi++) {
2386 if (pols[pi] != pol &&
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002387 pols[pi]->action != XFRM_POLICY_ALLOW) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002388 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002389 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002390 }
2391 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002392 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002393 goto reject_error;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002394 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002395 for (i = 0; i < pols[pi]->xfrm_nr; i++)
2396 tpp[ti++] = &pols[pi]->xfrm_vec[i];
2397 }
2398 xfrm_nr = ti;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002399 if (npols > 1) {
2400 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family);
2401 tpp = stp;
2402 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002403
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 /* For each tunnel xfrm, find the first matching tmpl.
2405 * For each tmpl before that, find corresponding xfrm.
2406 * Order is _important_. Later we will implement
2407 * some barriers, but at the moment barriers
2408 * are implied between each two transformations.
2409 */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002410 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
2411 k = xfrm_policy_ok(tpp[i], sp, k, family);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002412 if (k < 0) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002413 if (k < -1)
2414 /* "-2 - errored_index" returned */
2415 xerr_idx = -(2+k);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002416 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 goto reject;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 }
2420
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002421 if (secpath_has_nontransport(sp, k, &xerr_idx)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002422 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002426 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 return 1;
2428 }
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002429 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430
2431reject:
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002432 xfrm_secpath_reject(xerr_idx, skb, &fl);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002433reject_error:
2434 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 return 0;
2436}
2437EXPORT_SYMBOL(__xfrm_policy_check);
2438
2439int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
2440{
Alexey Dobriyan99a66652008-11-25 17:36:13 -08002441 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 struct flowi fl;
Eric Dumazetadf30902009-06-02 05:19:30 +00002443 struct dst_entry *dst;
Eric Dumazet73137142011-03-15 15:26:43 -07002444 int res = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002446 if (xfrm_decode_session(skb, &fl, family) < 0) {
jamal72032fd2010-02-18 03:35:07 +00002447 XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
Eric Dumazetfafeeb62010-06-01 10:04:49 +00002451 skb_dst_force(skb);
Eric Dumazetadf30902009-06-02 05:19:30 +00002452
David S. Miller452edd52011-03-02 13:27:41 -08002453 dst = xfrm_lookup(net, skb_dst(skb), &fl, NULL, 0);
2454 if (IS_ERR(dst)) {
Eric Dumazet73137142011-03-15 15:26:43 -07002455 res = 0;
David S. Miller452edd52011-03-02 13:27:41 -08002456 dst = NULL;
2457 }
Eric Dumazetadf30902009-06-02 05:19:30 +00002458 skb_dst_set(skb, dst);
2459 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460}
2461EXPORT_SYMBOL(__xfrm_route_forward);
2462
David S. Millerd49c73c2006-08-13 18:55:53 -07002463/* Optimize later using cookies and generation ids. */
2464
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
2466{
David S. Millerd49c73c2006-08-13 18:55:53 -07002467 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
David S. Millerf5b0a872012-07-19 12:31:33 -07002468 * to DST_OBSOLETE_FORCE_CHK to force all XFRM destinations to
2469 * get validated by dst_ops->check on every use. We do this
2470 * because when a normal route referenced by an XFRM dst is
2471 * obsoleted we do not go looking around for all parent
2472 * referencing XFRM dsts so that we can invalidate them. It
2473 * is just too much work. Instead we make the checks here on
2474 * every use. For example:
David S. Millerd49c73c2006-08-13 18:55:53 -07002475 *
2476 * XFRM dst A --> IPv4 dst X
2477 *
2478 * X is the "xdst->route" of A (X is also the "dst->path" of A
2479 * in this example). If X is marked obsolete, "A" will not
2480 * notice. That's what we are validating here via the
2481 * stale_bundle() check.
2482 *
2483 * When a policy's bundle is pruned, we dst_free() the XFRM
David S. Millerf5b0a872012-07-19 12:31:33 -07002484 * dst which causes it's ->obsolete field to be set to
2485 * DST_OBSOLETE_DEAD. If an XFRM dst has been pruned like
2486 * this, we want to force a new route lookup.
David S. Miller399c1802005-12-19 14:23:23 -08002487 */
David S. Millerd49c73c2006-08-13 18:55:53 -07002488 if (dst->obsolete < 0 && !stale_bundle(dst))
2489 return dst;
2490
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 return NULL;
2492}
2493
2494static int stale_bundle(struct dst_entry *dst)
2495{
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002496 return !xfrm_bundle_ok((struct xfrm_dst *)dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497}
2498
Herbert Xuaabc9762005-05-03 16:27:10 -07002499void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002502 dst->dev = dev_net(dev)->loopback_dev;
Daniel Lezcanode3cb742007-09-25 19:16:28 -07002503 dev_hold(dst->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 dev_put(dev);
2505 }
2506}
Herbert Xuaabc9762005-05-03 16:27:10 -07002507EXPORT_SYMBOL(xfrm_dst_ifdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508
2509static void xfrm_link_failure(struct sk_buff *skb)
2510{
2511 /* Impossible. Such dst must be popped before reaches point of failure. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512}
2513
2514static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
2515{
2516 if (dst) {
2517 if (dst->obsolete) {
2518 dst_release(dst);
2519 dst = NULL;
2520 }
2521 }
2522 return dst;
2523}
2524
Alexey Dobriyanddcfd792008-11-25 17:37:23 -08002525static void __xfrm_garbage_collect(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526{
Timo Teräs80c802f2010-04-07 00:30:05 +00002527 struct dst_entry *head, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528
Timo Teräs80c802f2010-04-07 00:30:05 +00002529 spin_lock_bh(&xfrm_policy_sk_bundle_lock);
2530 head = xfrm_policy_sk_bundles;
2531 xfrm_policy_sk_bundles = NULL;
2532 spin_unlock_bh(&xfrm_policy_sk_bundle_lock);
2533
2534 while (head) {
2535 next = head->next;
2536 dst_free(head);
2537 head = next;
2538 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539}
2540
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002541static void xfrm_garbage_collect(struct net *net)
2542{
2543 flow_cache_flush();
2544 __xfrm_garbage_collect(net);
2545}
2546
2547static void xfrm_garbage_collect_deferred(struct net *net)
2548{
2549 flow_cache_flush_deferred();
2550 __xfrm_garbage_collect(net);
2551}
2552
Herbert Xu25ee3282007-12-11 09:32:34 -08002553static void xfrm_init_pmtu(struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554{
2555 do {
2556 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2557 u32 pmtu, route_mtu_cached;
2558
2559 pmtu = dst_mtu(dst->child);
2560 xdst->child_mtu_cached = pmtu;
2561
2562 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
2563
2564 route_mtu_cached = dst_mtu(xdst->route);
2565 xdst->route_mtu_cached = route_mtu_cached;
2566
2567 if (pmtu > route_mtu_cached)
2568 pmtu = route_mtu_cached;
2569
David S. Millerdefb3512010-12-08 21:16:57 -08002570 dst_metric_set(dst, RTAX_MTU, pmtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 } while ((dst = dst->next));
2572}
2573
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574/* Check that the bundle accepts the flow and its components are
2575 * still valid.
2576 */
2577
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002578static int xfrm_bundle_ok(struct xfrm_dst *first)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579{
2580 struct dst_entry *dst = &first->u.dst;
2581 struct xfrm_dst *last;
2582 u32 mtu;
2583
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07002584 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 (dst->dev && !netif_running(dst->dev)))
2586 return 0;
2587
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002588 if (dst->flags & DST_XFRM_QUEUE)
2589 return 1;
2590
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 last = NULL;
2592
2593 do {
2594 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2595
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 if (dst->xfrm->km.state != XFRM_STATE_VALID)
2597 return 0;
Timo Teräs80c802f2010-04-07 00:30:05 +00002598 if (xdst->xfrm_genid != dst->xfrm->genid)
2599 return 0;
Timo Teräsb1312c82010-06-24 14:35:00 -07002600 if (xdst->num_pols > 0 &&
2601 xdst->policy_genid != atomic_read(&xdst->pols[0]->genid))
David S. Miller9d4a7062006-08-24 03:18:09 -07002602 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603
2604 mtu = dst_mtu(dst->child);
2605 if (xdst->child_mtu_cached != mtu) {
2606 last = xdst;
2607 xdst->child_mtu_cached = mtu;
2608 }
2609
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07002610 if (!dst_check(xdst->route, xdst->route_cookie))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 return 0;
2612 mtu = dst_mtu(xdst->route);
2613 if (xdst->route_mtu_cached != mtu) {
2614 last = xdst;
2615 xdst->route_mtu_cached = mtu;
2616 }
2617
2618 dst = dst->child;
2619 } while (dst->xfrm);
2620
2621 if (likely(!last))
2622 return 1;
2623
2624 mtu = last->child_mtu_cached;
2625 for (;;) {
2626 dst = &last->u.dst;
2627
2628 mtu = xfrm_state_mtu(dst->xfrm, mtu);
2629 if (mtu > last->route_mtu_cached)
2630 mtu = last->route_mtu_cached;
David S. Millerdefb3512010-12-08 21:16:57 -08002631 dst_metric_set(dst, RTAX_MTU, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632
2633 if (last == first)
2634 break;
2635
Patrick McHardybd0bf072007-07-18 01:55:52 -07002636 last = (struct xfrm_dst *)last->u.dst.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 last->child_mtu_cached = mtu;
2638 }
2639
2640 return 1;
2641}
2642
David S. Miller0dbaee32010-12-13 12:52:14 -08002643static unsigned int xfrm_default_advmss(const struct dst_entry *dst)
2644{
2645 return dst_metric_advmss(dst->path);
2646}
2647
Steffen Klassertebb762f2011-11-23 02:12:51 +00002648static unsigned int xfrm_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08002649{
Steffen Klassert618f9bc2011-11-23 02:13:31 +00002650 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
2651
2652 return mtu ? : dst_mtu(dst->path);
David S. Millerd33e4552010-12-14 13:01:14 -08002653}
2654
David S. Millerf894cbf2012-07-02 21:52:24 -07002655static struct neighbour *xfrm_neigh_lookup(const struct dst_entry *dst,
2656 struct sk_buff *skb,
2657 const void *daddr)
David S. Millerd3aaeb32011-07-18 00:40:17 -07002658{
David S. Millerf894cbf2012-07-02 21:52:24 -07002659 return dst->path->ops->neigh_lookup(dst, skb, daddr);
David S. Millerd3aaeb32011-07-18 00:40:17 -07002660}
2661
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
2663{
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002664 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 int err = 0;
2666 if (unlikely(afinfo == NULL))
2667 return -EINVAL;
2668 if (unlikely(afinfo->family >= NPROTO))
2669 return -EAFNOSUPPORT;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002670 spin_lock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
2672 err = -ENOBUFS;
2673 else {
2674 struct dst_ops *dst_ops = afinfo->dst_ops;
2675 if (likely(dst_ops->kmem_cachep == NULL))
2676 dst_ops->kmem_cachep = xfrm_dst_cache;
2677 if (likely(dst_ops->check == NULL))
2678 dst_ops->check = xfrm_dst_check;
David S. Miller0dbaee32010-12-13 12:52:14 -08002679 if (likely(dst_ops->default_advmss == NULL))
2680 dst_ops->default_advmss = xfrm_default_advmss;
Steffen Klassertebb762f2011-11-23 02:12:51 +00002681 if (likely(dst_ops->mtu == NULL))
2682 dst_ops->mtu = xfrm_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 if (likely(dst_ops->negative_advice == NULL))
2684 dst_ops->negative_advice = xfrm_negative_advice;
2685 if (likely(dst_ops->link_failure == NULL))
2686 dst_ops->link_failure = xfrm_link_failure;
David S. Millerd3aaeb32011-07-18 00:40:17 -07002687 if (likely(dst_ops->neigh_lookup == NULL))
2688 dst_ops->neigh_lookup = xfrm_neigh_lookup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 if (likely(afinfo->garbage_collect == NULL))
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002690 afinfo->garbage_collect = xfrm_garbage_collect_deferred;
Priyanka Jain418a99a2012-08-12 21:22:29 +00002691 rcu_assign_pointer(xfrm_policy_afinfo[afinfo->family], afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02002693 spin_unlock(&xfrm_policy_afinfo_lock);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002694
2695 rtnl_lock();
2696 for_each_net(net) {
2697 struct dst_ops *xfrm_dst_ops;
2698
2699 switch (afinfo->family) {
2700 case AF_INET:
2701 xfrm_dst_ops = &net->xfrm.xfrm4_dst_ops;
2702 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002703#if IS_ENABLED(CONFIG_IPV6)
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002704 case AF_INET6:
2705 xfrm_dst_ops = &net->xfrm.xfrm6_dst_ops;
2706 break;
2707#endif
2708 default:
2709 BUG();
2710 }
2711 *xfrm_dst_ops = *afinfo->dst_ops;
2712 }
2713 rtnl_unlock();
2714
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 return err;
2716}
2717EXPORT_SYMBOL(xfrm_policy_register_afinfo);
2718
2719int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
2720{
2721 int err = 0;
2722 if (unlikely(afinfo == NULL))
2723 return -EINVAL;
2724 if (unlikely(afinfo->family >= NPROTO))
2725 return -EAFNOSUPPORT;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002726 spin_lock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
2728 if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
2729 err = -EINVAL;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002730 else
2731 RCU_INIT_POINTER(xfrm_policy_afinfo[afinfo->family],
2732 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02002734 spin_unlock(&xfrm_policy_afinfo_lock);
2735 if (!err) {
2736 struct dst_ops *dst_ops = afinfo->dst_ops;
2737
2738 synchronize_rcu();
2739
2740 dst_ops->kmem_cachep = NULL;
2741 dst_ops->check = NULL;
2742 dst_ops->negative_advice = NULL;
2743 dst_ops->link_failure = NULL;
2744 afinfo->garbage_collect = NULL;
2745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 return err;
2747}
2748EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
2749
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002750static void __net_init xfrm_dst_ops_init(struct net *net)
2751{
2752 struct xfrm_policy_afinfo *afinfo;
2753
Eric Dumazetef8531b2012-08-19 12:31:48 +02002754 rcu_read_lock();
2755 afinfo = rcu_dereference(xfrm_policy_afinfo[AF_INET]);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002756 if (afinfo)
2757 net->xfrm.xfrm4_dst_ops = *afinfo->dst_ops;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002758#if IS_ENABLED(CONFIG_IPV6)
Eric Dumazetef8531b2012-08-19 12:31:48 +02002759 afinfo = rcu_dereference(xfrm_policy_afinfo[AF_INET6]);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002760 if (afinfo)
2761 net->xfrm.xfrm6_dst_ops = *afinfo->dst_ops;
2762#endif
Priyanka Jain418a99a2012-08-12 21:22:29 +00002763 rcu_read_unlock();
Herbert Xu546be242006-05-27 23:03:58 -07002764}
2765
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
2767{
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02002768 struct net_device *dev = ptr;
2769
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 switch (event) {
2771 case NETDEV_DOWN:
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002772 xfrm_garbage_collect(dev_net(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 }
2774 return NOTIFY_DONE;
2775}
2776
2777static struct notifier_block xfrm_dev_notifier = {
Alexey Dobriyand5917a32008-10-31 00:41:59 -07002778 .notifier_call = xfrm_dev_event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779};
2780
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002781#ifdef CONFIG_XFRM_STATISTICS
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002782static int __net_init xfrm_statistics_init(struct net *net)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002783{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002784 int rv;
2785
Tejun Heo7d720c32010-02-16 15:20:26 +00002786 if (snmp_mib_init((void __percpu **)net->mib.xfrm_statistics,
Eric Dumazet1823e4c82010-06-22 20:58:41 +00002787 sizeof(struct linux_xfrm_mib),
2788 __alignof__(struct linux_xfrm_mib)) < 0)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002789 return -ENOMEM;
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002790 rv = xfrm_proc_init(net);
2791 if (rv < 0)
Tejun Heo7d720c32010-02-16 15:20:26 +00002792 snmp_mib_free((void __percpu **)net->mib.xfrm_statistics);
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002793 return rv;
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002794}
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002795
2796static void xfrm_statistics_fini(struct net *net)
2797{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002798 xfrm_proc_fini(net);
Tejun Heo7d720c32010-02-16 15:20:26 +00002799 snmp_mib_free((void __percpu **)net->mib.xfrm_statistics);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002800}
2801#else
2802static int __net_init xfrm_statistics_init(struct net *net)
2803{
2804 return 0;
2805}
2806
2807static void xfrm_statistics_fini(struct net *net)
2808{
2809}
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002810#endif
2811
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002812static int __net_init xfrm_policy_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813{
David S. Miller2518c7c2006-08-24 04:45:07 -07002814 unsigned int hmask, sz;
2815 int dir;
2816
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002817 if (net_eq(net, &init_net))
2818 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 sizeof(struct xfrm_dst),
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002820 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002821 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822
David S. Miller2518c7c2006-08-24 04:45:07 -07002823 hmask = 8 - 1;
2824 sz = (hmask+1) * sizeof(struct hlist_head);
2825
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002826 net->xfrm.policy_byidx = xfrm_hash_alloc(sz);
2827 if (!net->xfrm.policy_byidx)
2828 goto out_byidx;
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08002829 net->xfrm.policy_idx_hmask = hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -07002830
2831 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
2832 struct xfrm_policy_hash *htab;
2833
Alexey Dobriyandc2caba2008-11-25 17:24:15 -08002834 net->xfrm.policy_count[dir] = 0;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002835 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
David S. Miller2518c7c2006-08-24 04:45:07 -07002836
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002837 htab = &net->xfrm.policy_bydst[dir];
David S. Miller44e36b42006-08-24 04:50:50 -07002838 htab->table = xfrm_hash_alloc(sz);
David S. Miller2518c7c2006-08-24 04:45:07 -07002839 if (!htab->table)
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002840 goto out_bydst;
2841 htab->hmask = hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -07002842 }
2843
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08002844 INIT_LIST_HEAD(&net->xfrm.policy_all);
Alexey Dobriyan66caf622008-11-25 17:28:57 -08002845 INIT_WORK(&net->xfrm.policy_hash_work, xfrm_hash_resize);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002846 if (net_eq(net, &init_net))
2847 register_netdevice_notifier(&xfrm_dev_notifier);
2848 return 0;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002849
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002850out_bydst:
2851 for (dir--; dir >= 0; dir--) {
2852 struct xfrm_policy_hash *htab;
2853
2854 htab = &net->xfrm.policy_bydst[dir];
2855 xfrm_hash_free(htab->table, sz);
2856 }
2857 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002858out_byidx:
2859 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860}
2861
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002862static void xfrm_policy_fini(struct net *net)
2863{
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002864 struct xfrm_audit audit_info;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002865 unsigned int sz;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002866 int dir;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002867
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002868 flush_work(&net->xfrm.policy_hash_work);
2869#ifdef CONFIG_XFRM_SUB_POLICY
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07002870 audit_info.loginuid = INVALID_UID;
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002871 audit_info.sessionid = -1;
2872 audit_info.secid = 0;
2873 xfrm_policy_flush(net, XFRM_POLICY_TYPE_SUB, &audit_info);
2874#endif
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07002875 audit_info.loginuid = INVALID_UID;
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002876 audit_info.sessionid = -1;
2877 audit_info.secid = 0;
2878 xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, &audit_info);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002879
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08002880 WARN_ON(!list_empty(&net->xfrm.policy_all));
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002881
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002882 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002883 struct xfrm_policy_hash *htab;
2884
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002885 WARN_ON(!hlist_empty(&net->xfrm.policy_inexact[dir]));
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002886
2887 htab = &net->xfrm.policy_bydst[dir];
Michal Kubecek5b653b22013-01-18 16:03:48 +01002888 sz = (htab->hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002889 WARN_ON(!hlist_empty(htab->table));
2890 xfrm_hash_free(htab->table, sz);
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002891 }
2892
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08002893 sz = (net->xfrm.policy_idx_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002894 WARN_ON(!hlist_empty(net->xfrm.policy_byidx));
2895 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002896}
2897
2898static int __net_init xfrm_net_init(struct net *net)
2899{
2900 int rv;
2901
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002902 rv = xfrm_statistics_init(net);
2903 if (rv < 0)
2904 goto out_statistics;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002905 rv = xfrm_state_init(net);
2906 if (rv < 0)
2907 goto out_state;
2908 rv = xfrm_policy_init(net);
2909 if (rv < 0)
2910 goto out_policy;
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002911 xfrm_dst_ops_init(net);
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08002912 rv = xfrm_sysctl_init(net);
2913 if (rv < 0)
2914 goto out_sysctl;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002915 return 0;
2916
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08002917out_sysctl:
2918 xfrm_policy_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002919out_policy:
2920 xfrm_state_fini(net);
2921out_state:
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002922 xfrm_statistics_fini(net);
2923out_statistics:
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002924 return rv;
2925}
2926
2927static void __net_exit xfrm_net_exit(struct net *net)
2928{
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08002929 xfrm_sysctl_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002930 xfrm_policy_fini(net);
2931 xfrm_state_fini(net);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002932 xfrm_statistics_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002933}
2934
2935static struct pernet_operations __net_initdata xfrm_net_ops = {
2936 .init = xfrm_net_init,
2937 .exit = xfrm_net_exit,
2938};
2939
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940void __init xfrm_init(void)
2941{
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002942 register_pernet_subsys(&xfrm_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 xfrm_input_init();
2944}
2945
Joy Lattenab5f5e82007-09-17 11:51:22 -07002946#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinen1486cbd72008-01-12 03:20:03 -08002947static void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
2948 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002949{
Paul Moore875179f2007-12-01 23:27:18 +11002950 struct xfrm_sec_ctx *ctx = xp->security;
2951 struct xfrm_selector *sel = &xp->selector;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002952
Paul Moore875179f2007-12-01 23:27:18 +11002953 if (ctx)
2954 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
2955 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
2956
2957 switch(sel->family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07002958 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07002959 audit_log_format(audit_buf, " src=%pI4", &sel->saddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11002960 if (sel->prefixlen_s != 32)
2961 audit_log_format(audit_buf, " src_prefixlen=%d",
2962 sel->prefixlen_s);
Harvey Harrison21454aa2008-10-31 00:54:56 -07002963 audit_log_format(audit_buf, " dst=%pI4", &sel->daddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11002964 if (sel->prefixlen_d != 32)
2965 audit_log_format(audit_buf, " dst_prefixlen=%d",
2966 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002967 break;
2968 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002969 audit_log_format(audit_buf, " src=%pI6", sel->saddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11002970 if (sel->prefixlen_s != 128)
2971 audit_log_format(audit_buf, " src_prefixlen=%d",
2972 sel->prefixlen_s);
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002973 audit_log_format(audit_buf, " dst=%pI6", sel->daddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11002974 if (sel->prefixlen_d != 128)
2975 audit_log_format(audit_buf, " dst_prefixlen=%d",
2976 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002977 break;
2978 }
2979}
2980
Paul Moore68277ac2007-12-20 20:49:33 -08002981void xfrm_audit_policy_add(struct xfrm_policy *xp, int result,
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07002982 kuid_t auid, u32 sessionid, u32 secid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002983{
2984 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002985
Paul Mooreafeb14b2007-12-21 14:58:11 -08002986 audit_buf = xfrm_audit_start("SPD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002987 if (audit_buf == NULL)
2988 return;
Eric Paris25323862008-04-18 10:09:25 -04002989 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002990 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002991 xfrm_audit_common_policyinfo(xp, audit_buf);
2992 audit_log_end(audit_buf);
2993}
2994EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
2995
Paul Moore68277ac2007-12-20 20:49:33 -08002996void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07002997 kuid_t auid, u32 sessionid, u32 secid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002998{
2999 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003000
Paul Mooreafeb14b2007-12-21 14:58:11 -08003001 audit_buf = xfrm_audit_start("SPD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07003002 if (audit_buf == NULL)
3003 return;
Eric Paris25323862008-04-18 10:09:25 -04003004 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08003005 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003006 xfrm_audit_common_policyinfo(xp, audit_buf);
3007 audit_log_end(audit_buf);
3008}
3009EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
3010#endif
3011
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003012#ifdef CONFIG_XFRM_MIGRATE
David S. Millerbc9b35a2012-05-15 15:04:57 -04003013static bool xfrm_migrate_selector_match(const struct xfrm_selector *sel_cmp,
3014 const struct xfrm_selector *sel_tgt)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003015{
3016 if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
3017 if (sel_tgt->family == sel_cmp->family &&
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003018 xfrm_addr_equal(&sel_tgt->daddr, &sel_cmp->daddr,
3019 sel_cmp->family) &&
3020 xfrm_addr_equal(&sel_tgt->saddr, &sel_cmp->saddr,
3021 sel_cmp->family) &&
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003022 sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
3023 sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04003024 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003025 }
3026 } else {
3027 if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04003028 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003029 }
3030 }
David S. Millerbc9b35a2012-05-15 15:04:57 -04003031 return false;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003032}
3033
David S. Millerb4b7c0b2011-02-24 00:35:06 -05003034static struct xfrm_policy * xfrm_migrate_policy_find(const struct xfrm_selector *sel,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003035 u8 dir, u8 type)
3036{
3037 struct xfrm_policy *pol, *ret = NULL;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003038 struct hlist_head *chain;
3039 u32 priority = ~0U;
3040
3041 read_lock_bh(&xfrm_policy_lock);
Alexey Dobriyan11219942008-11-25 17:33:06 -08003042 chain = policy_hash_direct(&init_net, &sel->daddr, &sel->saddr, sel->family, dir);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003043 hlist_for_each_entry(pol, chain, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003044 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
3045 pol->type == type) {
3046 ret = pol;
3047 priority = ret->priority;
3048 break;
3049 }
3050 }
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003051 chain = &init_net.xfrm.policy_inexact[dir];
Sasha Levinb67bfe02013-02-27 17:06:00 -08003052 hlist_for_each_entry(pol, chain, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003053 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
3054 pol->type == type &&
3055 pol->priority < priority) {
3056 ret = pol;
3057 break;
3058 }
3059 }
3060
3061 if (ret)
3062 xfrm_pol_hold(ret);
3063
3064 read_unlock_bh(&xfrm_policy_lock);
3065
3066 return ret;
3067}
3068
David S. Millerdd701752011-02-24 00:21:08 -05003069static int migrate_tmpl_match(const struct xfrm_migrate *m, const struct xfrm_tmpl *t)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003070{
3071 int match = 0;
3072
3073 if (t->mode == m->mode && t->id.proto == m->proto &&
3074 (m->reqid == 0 || t->reqid == m->reqid)) {
3075 switch (t->mode) {
3076 case XFRM_MODE_TUNNEL:
3077 case XFRM_MODE_BEET:
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003078 if (xfrm_addr_equal(&t->id.daddr, &m->old_daddr,
3079 m->old_family) &&
3080 xfrm_addr_equal(&t->saddr, &m->old_saddr,
3081 m->old_family)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003082 match = 1;
3083 }
3084 break;
3085 case XFRM_MODE_TRANSPORT:
3086 /* in case of transport mode, template does not store
3087 any IP addresses, hence we just compare mode and
3088 protocol */
3089 match = 1;
3090 break;
3091 default:
3092 break;
3093 }
3094 }
3095 return match;
3096}
3097
3098/* update endpoint address(es) of template(s) */
3099static int xfrm_policy_migrate(struct xfrm_policy *pol,
3100 struct xfrm_migrate *m, int num_migrate)
3101{
3102 struct xfrm_migrate *mp;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003103 int i, j, n = 0;
3104
3105 write_lock_bh(&pol->lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07003106 if (unlikely(pol->walk.dead)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003107 /* target policy has been deleted */
3108 write_unlock_bh(&pol->lock);
3109 return -ENOENT;
3110 }
3111
3112 for (i = 0; i < pol->xfrm_nr; i++) {
3113 for (j = 0, mp = m; j < num_migrate; j++, mp++) {
3114 if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
3115 continue;
3116 n++;
Herbert Xu1bfcb102007-10-17 21:31:50 -07003117 if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
3118 pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003119 continue;
3120 /* update endpoints */
3121 memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
3122 sizeof(pol->xfrm_vec[i].id.daddr));
3123 memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
3124 sizeof(pol->xfrm_vec[i].saddr));
3125 pol->xfrm_vec[i].encap_family = mp->new_family;
3126 /* flush bundles */
Timo Teräs80c802f2010-04-07 00:30:05 +00003127 atomic_inc(&pol->genid);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003128 }
3129 }
3130
3131 write_unlock_bh(&pol->lock);
3132
3133 if (!n)
3134 return -ENODATA;
3135
3136 return 0;
3137}
3138
David S. Millerdd701752011-02-24 00:21:08 -05003139static int xfrm_migrate_check(const struct xfrm_migrate *m, int num_migrate)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003140{
3141 int i, j;
3142
3143 if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
3144 return -EINVAL;
3145
3146 for (i = 0; i < num_migrate; i++) {
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003147 if (xfrm_addr_equal(&m[i].old_daddr, &m[i].new_daddr,
3148 m[i].old_family) &&
3149 xfrm_addr_equal(&m[i].old_saddr, &m[i].new_saddr,
3150 m[i].old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003151 return -EINVAL;
3152 if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
3153 xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
3154 return -EINVAL;
3155
3156 /* check if there is any duplicated entry */
3157 for (j = i + 1; j < num_migrate; j++) {
3158 if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
3159 sizeof(m[i].old_daddr)) &&
3160 !memcmp(&m[i].old_saddr, &m[j].old_saddr,
3161 sizeof(m[i].old_saddr)) &&
3162 m[i].proto == m[j].proto &&
3163 m[i].mode == m[j].mode &&
3164 m[i].reqid == m[j].reqid &&
3165 m[i].old_family == m[j].old_family)
3166 return -EINVAL;
3167 }
3168 }
3169
3170 return 0;
3171}
3172
David S. Millerb4b7c0b2011-02-24 00:35:06 -05003173int xfrm_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003174 struct xfrm_migrate *m, int num_migrate,
3175 struct xfrm_kmaddress *k)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003176{
3177 int i, err, nx_cur = 0, nx_new = 0;
3178 struct xfrm_policy *pol = NULL;
3179 struct xfrm_state *x, *xc;
3180 struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
3181 struct xfrm_state *x_new[XFRM_MAX_DEPTH];
3182 struct xfrm_migrate *mp;
3183
3184 if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
3185 goto out;
3186
3187 /* Stage 1 - find policy */
3188 if ((pol = xfrm_migrate_policy_find(sel, dir, type)) == NULL) {
3189 err = -ENOENT;
3190 goto out;
3191 }
3192
3193 /* Stage 2 - find and update state(s) */
3194 for (i = 0, mp = m; i < num_migrate; i++, mp++) {
3195 if ((x = xfrm_migrate_state_find(mp))) {
3196 x_cur[nx_cur] = x;
3197 nx_cur++;
3198 if ((xc = xfrm_state_migrate(x, mp))) {
3199 x_new[nx_new] = xc;
3200 nx_new++;
3201 } else {
3202 err = -ENODATA;
3203 goto restore_state;
3204 }
3205 }
3206 }
3207
3208 /* Stage 3 - update policy */
3209 if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
3210 goto restore_state;
3211
3212 /* Stage 4 - delete old state(s) */
3213 if (nx_cur) {
3214 xfrm_states_put(x_cur, nx_cur);
3215 xfrm_states_delete(x_cur, nx_cur);
3216 }
3217
3218 /* Stage 5 - announce */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003219 km_migrate(sel, dir, type, m, num_migrate, k);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003220
3221 xfrm_pol_put(pol);
3222
3223 return 0;
3224out:
3225 return err;
3226
3227restore_state:
3228 if (pol)
3229 xfrm_pol_put(pol);
3230 if (nx_cur)
3231 xfrm_states_put(x_cur, nx_cur);
3232 if (nx_new)
3233 xfrm_states_delete(x_new, nx_new);
3234
3235 return err;
3236}
David S. Millere610e672007-02-08 13:29:15 -08003237EXPORT_SYMBOL(xfrm_migrate);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003238#endif