blob: 456b11b0f049341d7ba731b66c3ab0f26b2c468a [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{
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800382 struct hlist_node *entry, *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:
David S. Miller2518c7c2006-08-24 04:45:07 -0700387 hlist_for_each_entry_safe(pol, entry, tmp, list, bydst) {
388 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) {
393 hlist_del(entry);
394 hlist_add_head(&pol->bydst, ndsttable+h);
395 h0 = h;
396 } else {
397 if (h != h0)
398 continue;
399 hlist_del(entry);
400 hlist_add_after(entry0, &pol->bydst);
401 }
402 entry0 = entry;
403 }
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{
414 struct hlist_node *entry, *tmp;
415 struct xfrm_policy *pol;
416
417 hlist_for_each_entry_safe(pol, entry, tmp, list, byidx) {
418 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_node *entry;
548 struct hlist_head *list;
549 struct xfrm_policy *p;
550 u32 idx;
551 int found;
552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 idx = (idx_generator | dir);
554 idx_generator += 8;
555 if (idx == 0)
556 idx = 8;
Alexey Dobriyan11219942008-11-25 17:33:06 -0800557 list = net->xfrm.policy_byidx + idx_hash(net, idx);
David S. Miller2518c7c2006-08-24 04:45:07 -0700558 found = 0;
559 hlist_for_each_entry(p, entry, list, byidx) {
560 if (p->index == idx) {
561 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 break;
David S. Miller2518c7c2006-08-24 04:45:07 -0700563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700565 if (!found)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 return idx;
567 }
568}
569
David S. Miller2518c7c2006-08-24 04:45:07 -0700570static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
571{
572 u32 *p1 = (u32 *) s1;
573 u32 *p2 = (u32 *) s2;
574 int len = sizeof(struct xfrm_selector) / sizeof(u32);
575 int i;
576
577 for (i = 0; i < len; i++) {
578 if (p1[i] != p2[i])
579 return 1;
580 }
581
582 return 0;
583}
584
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100585static void xfrm_policy_requeue(struct xfrm_policy *old,
586 struct xfrm_policy *new)
587{
588 struct xfrm_policy_queue *pq = &old->polq;
589 struct sk_buff_head list;
590
591 __skb_queue_head_init(&list);
592
593 spin_lock_bh(&pq->hold_queue.lock);
594 skb_queue_splice_init(&pq->hold_queue, &list);
595 del_timer(&pq->hold_timer);
596 spin_unlock_bh(&pq->hold_queue.lock);
597
598 if (skb_queue_empty(&list))
599 return;
600
601 pq = &new->polq;
602
603 spin_lock_bh(&pq->hold_queue.lock);
604 skb_queue_splice(&list, &pq->hold_queue);
605 pq->timeout = XFRM_QUEUE_TMO_MIN;
606 mod_timer(&pq->hold_timer, jiffies);
607 spin_unlock_bh(&pq->hold_queue.lock);
608}
609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
611{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800612 struct net *net = xp_net(policy);
David S. Miller2518c7c2006-08-24 04:45:07 -0700613 struct xfrm_policy *pol;
614 struct xfrm_policy *delpol;
615 struct hlist_head *chain;
Herbert Xua6c7ab52007-01-16 16:52:02 -0800616 struct hlist_node *entry, *newpos;
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000617 u32 mark = policy->mark.v & policy->mark.m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619 write_lock_bh(&xfrm_policy_lock);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800620 chain = policy_hash_bysel(net, &policy->selector, policy->family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700621 delpol = NULL;
622 newpos = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700623 hlist_for_each_entry(pol, entry, chain, bydst) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800624 if (pol->type == policy->type &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700625 !selector_cmp(&pol->selector, &policy->selector) &&
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000626 (mark & pol->mark.m) == pol->mark.v &&
Herbert Xua6c7ab52007-01-16 16:52:02 -0800627 xfrm_sec_ctx_match(pol->security, policy->security) &&
628 !WARN_ON(delpol)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 if (excl) {
630 write_unlock_bh(&xfrm_policy_lock);
631 return -EEXIST;
632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 delpol = pol;
634 if (policy->priority > pol->priority)
635 continue;
636 } else if (policy->priority >= pol->priority) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800637 newpos = &pol->bydst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 continue;
639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 if (delpol)
641 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 }
643 if (newpos)
David S. Miller2518c7c2006-08-24 04:45:07 -0700644 hlist_add_after(newpos, &policy->bydst);
645 else
646 hlist_add_head(&policy->bydst, chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 xfrm_pol_hold(policy);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800648 net->xfrm.policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 atomic_inc(&flow_cache_genid);
Nicolas Dichtelee8372d2012-09-10 22:09:45 +0000650 rt_genid_bump(net);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100651 if (delpol) {
652 xfrm_policy_requeue(delpol, policy);
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800653 __xfrm_policy_unlink(delpol, dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100654 }
Alexey Dobriyan11219942008-11-25 17:33:06 -0800655 policy->index = delpol ? delpol->index : xfrm_gen_index(net, dir);
656 hlist_add_head(&policy->byidx, net->xfrm.policy_byidx+idx_hash(net, policy->index));
James Morris9d729f72007-03-04 16:12:44 -0800657 policy->curlft.add_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 policy->curlft.use_time = 0;
659 if (!mod_timer(&policy->timer, jiffies + HZ))
660 xfrm_pol_hold(policy);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800661 list_add(&policy->walk.all, &net->xfrm.policy_all);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 write_unlock_bh(&xfrm_policy_lock);
663
David S. Miller9b78a822005-12-22 07:39:48 -0800664 if (delpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 xfrm_policy_kill(delpol);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800666 else if (xfrm_bydst_should_resize(net, dir, NULL))
667 schedule_work(&net->xfrm.policy_hash_work);
David S. Miller9b78a822005-12-22 07:39:48 -0800668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return 0;
670}
671EXPORT_SYMBOL(xfrm_policy_insert);
672
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +0000673struct xfrm_policy *xfrm_policy_bysel_ctx(struct net *net, u32 mark, u8 type,
674 int dir, struct xfrm_selector *sel,
Eric Parisef41aaa2007-03-07 15:37:58 -0800675 struct xfrm_sec_ctx *ctx, int delete,
676 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
David S. Miller2518c7c2006-08-24 04:45:07 -0700678 struct xfrm_policy *pol, *ret;
679 struct hlist_head *chain;
680 struct hlist_node *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Eric Parisef41aaa2007-03-07 15:37:58 -0800682 *err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 write_lock_bh(&xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800684 chain = policy_hash_bysel(net, sel, sel->family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700685 ret = NULL;
686 hlist_for_each_entry(pol, entry, chain, bydst) {
687 if (pol->type == type &&
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000688 (mark & pol->mark.m) == pol->mark.v &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700689 !selector_cmp(sel, &pol->selector) &&
690 xfrm_sec_ctx_match(ctx, pol->security)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700692 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -0700693 *err = security_xfrm_policy_delete(
694 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -0800695 if (*err) {
696 write_unlock_bh(&xfrm_policy_lock);
697 return pol;
698 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800699 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700700 }
701 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 break;
703 }
704 }
705 write_unlock_bh(&xfrm_policy_lock);
706
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000707 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -0700708 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -0700709 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710}
Trent Jaegerdf718372005-12-13 23:12:27 -0800711EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +0000713struct xfrm_policy *xfrm_policy_byid(struct net *net, u32 mark, u8 type,
714 int dir, u32 id, int delete, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
David S. Miller2518c7c2006-08-24 04:45:07 -0700716 struct xfrm_policy *pol, *ret;
717 struct hlist_head *chain;
718 struct hlist_node *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Herbert Xub5505c62007-05-14 02:15:47 -0700720 *err = -ENOENT;
721 if (xfrm_policy_id2dir(id) != dir)
722 return NULL;
723
Eric Parisef41aaa2007-03-07 15:37:58 -0800724 *err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 write_lock_bh(&xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800726 chain = net->xfrm.policy_byidx + idx_hash(net, id);
David S. Miller2518c7c2006-08-24 04:45:07 -0700727 ret = NULL;
728 hlist_for_each_entry(pol, entry, chain, byidx) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000729 if (pol->type == type && pol->index == id &&
730 (mark & pol->mark.m) == pol->mark.v) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700732 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -0700733 *err = security_xfrm_policy_delete(
734 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -0800735 if (*err) {
736 write_unlock_bh(&xfrm_policy_lock);
737 return pol;
738 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800739 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700740 }
741 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 break;
743 }
744 }
745 write_unlock_bh(&xfrm_policy_lock);
746
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000747 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -0700748 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -0700749 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750}
751EXPORT_SYMBOL(xfrm_policy_byid);
752
Joy Latten4aa2e622007-06-04 19:05:57 -0400753#ifdef CONFIG_SECURITY_NETWORK_XFRM
754static inline int
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800755xfrm_policy_flush_secctx_check(struct net *net, u8 type, struct xfrm_audit *audit_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
Joy Latten4aa2e622007-06-04 19:05:57 -0400757 int dir, err = 0;
758
759 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
760 struct xfrm_policy *pol;
761 struct hlist_node *entry;
762 int i;
763
764 hlist_for_each_entry(pol, entry,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800765 &net->xfrm.policy_inexact[dir], bydst) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400766 if (pol->type != type)
767 continue;
Paul Moore03e1ad72008-04-12 19:07:52 -0700768 err = security_xfrm_policy_delete(pol->security);
Joy Latten4aa2e622007-06-04 19:05:57 -0400769 if (err) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700770 xfrm_audit_policy_delete(pol, 0,
771 audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400772 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700773 audit_info->secid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400774 return err;
775 }
YOSHIFUJI Hideaki7dc12d62007-07-19 10:45:15 +0900776 }
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800777 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400778 hlist_for_each_entry(pol, entry,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800779 net->xfrm.policy_bydst[dir].table + i,
Joy Latten4aa2e622007-06-04 19:05:57 -0400780 bydst) {
781 if (pol->type != type)
782 continue;
Paul Moore03e1ad72008-04-12 19:07:52 -0700783 err = security_xfrm_policy_delete(
784 pol->security);
Joy Latten4aa2e622007-06-04 19:05:57 -0400785 if (err) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700786 xfrm_audit_policy_delete(pol, 0,
787 audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400788 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700789 audit_info->secid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400790 return err;
791 }
792 }
793 }
794 }
795 return err;
796}
797#else
798static inline int
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800799xfrm_policy_flush_secctx_check(struct net *net, u8 type, struct xfrm_audit *audit_info)
Joy Latten4aa2e622007-06-04 19:05:57 -0400800{
801 return 0;
802}
803#endif
804
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800805int xfrm_policy_flush(struct net *net, u8 type, struct xfrm_audit *audit_info)
Joy Latten4aa2e622007-06-04 19:05:57 -0400806{
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +0000807 int dir, err = 0, cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809 write_lock_bh(&xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400810
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800811 err = xfrm_policy_flush_secctx_check(net, type, audit_info);
Joy Latten4aa2e622007-06-04 19:05:57 -0400812 if (err)
813 goto out;
814
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700816 struct xfrm_policy *pol;
817 struct hlist_node *entry;
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800818 int i;
David S. Miller2518c7c2006-08-24 04:45:07 -0700819
820 again1:
821 hlist_for_each_entry(pol, entry,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800822 &net->xfrm.policy_inexact[dir], bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700823 if (pol->type != type)
824 continue;
Timo Teräsea2dea92010-03-31 00:17:05 +0000825 __xfrm_policy_unlink(pol, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 write_unlock_bh(&xfrm_policy_lock);
Timo Teräsea2dea92010-03-31 00:17:05 +0000827 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Joy Lattenab5f5e82007-09-17 11:51:22 -0700829 xfrm_audit_policy_delete(pol, 1, audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400830 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700831 audit_info->secid);
Joy Latten161a09e2006-11-27 13:11:54 -0600832
David S. Miller2518c7c2006-08-24 04:45:07 -0700833 xfrm_policy_kill(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
835 write_lock_bh(&xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700836 goto again1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700838
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800839 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700840 again2:
841 hlist_for_each_entry(pol, entry,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800842 net->xfrm.policy_bydst[dir].table + i,
David S. Miller2518c7c2006-08-24 04:45:07 -0700843 bydst) {
844 if (pol->type != type)
845 continue;
Timo Teräsea2dea92010-03-31 00:17:05 +0000846 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700847 write_unlock_bh(&xfrm_policy_lock);
Timo Teräsea2dea92010-03-31 00:17:05 +0000848 cnt++;
David S. Miller2518c7c2006-08-24 04:45:07 -0700849
Joy Lattenab5f5e82007-09-17 11:51:22 -0700850 xfrm_audit_policy_delete(pol, 1,
851 audit_info->loginuid,
Eric Paris25323862008-04-18 10:09:25 -0400852 audit_info->sessionid,
Joy Lattenab5f5e82007-09-17 11:51:22 -0700853 audit_info->secid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700854 xfrm_policy_kill(pol);
855
856 write_lock_bh(&xfrm_policy_lock);
857 goto again2;
858 }
859 }
860
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 }
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +0000862 if (!cnt)
863 err = -ESRCH;
Joy Latten4aa2e622007-06-04 19:05:57 -0400864out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 write_unlock_bh(&xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400866 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867}
868EXPORT_SYMBOL(xfrm_policy_flush);
869
Alexey Dobriyancdcbca72008-11-25 17:34:49 -0800870int xfrm_policy_walk(struct net *net, struct xfrm_policy_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -0800871 int (*func)(struct xfrm_policy *, int, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 void *data)
873{
Herbert Xu12a169e2008-10-01 07:03:24 -0700874 struct xfrm_policy *pol;
875 struct xfrm_policy_walk_entry *x;
Timo Teras4c563f72008-02-28 21:31:08 -0800876 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Timo Teras4c563f72008-02-28 21:31:08 -0800878 if (walk->type >= XFRM_POLICY_TYPE_MAX &&
879 walk->type != XFRM_POLICY_TYPE_ANY)
880 return -EINVAL;
881
Herbert Xu12a169e2008-10-01 07:03:24 -0700882 if (list_empty(&walk->walk.all) && walk->seq != 0)
Timo Teras4c563f72008-02-28 21:31:08 -0800883 return 0;
884
Herbert Xu12a169e2008-10-01 07:03:24 -0700885 write_lock_bh(&xfrm_policy_lock);
886 if (list_empty(&walk->walk.all))
Alexey Dobriyancdcbca72008-11-25 17:34:49 -0800887 x = list_first_entry(&net->xfrm.policy_all, struct xfrm_policy_walk_entry, all);
Herbert Xu12a169e2008-10-01 07:03:24 -0700888 else
889 x = list_entry(&walk->walk.all, struct xfrm_policy_walk_entry, all);
Alexey Dobriyancdcbca72008-11-25 17:34:49 -0800890 list_for_each_entry_from(x, &net->xfrm.policy_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -0700891 if (x->dead)
Timo Teras4c563f72008-02-28 21:31:08 -0800892 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -0700893 pol = container_of(x, struct xfrm_policy, walk);
894 if (walk->type != XFRM_POLICY_TYPE_ANY &&
895 walk->type != pol->type)
896 continue;
897 error = func(pol, xfrm_policy_id2dir(pol->index),
898 walk->seq, data);
899 if (error) {
900 list_move_tail(&walk->walk.all, &x->all);
901 goto out;
Timo Teras4c563f72008-02-28 21:31:08 -0800902 }
Herbert Xu12a169e2008-10-01 07:03:24 -0700903 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 }
Herbert Xu12a169e2008-10-01 07:03:24 -0700905 if (walk->seq == 0) {
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -0800906 error = -ENOENT;
907 goto out;
908 }
Herbert Xu12a169e2008-10-01 07:03:24 -0700909 list_del_init(&walk->walk.all);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910out:
Herbert Xu12a169e2008-10-01 07:03:24 -0700911 write_unlock_bh(&xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 return error;
913}
914EXPORT_SYMBOL(xfrm_policy_walk);
915
Herbert Xu12a169e2008-10-01 07:03:24 -0700916void xfrm_policy_walk_init(struct xfrm_policy_walk *walk, u8 type)
917{
918 INIT_LIST_HEAD(&walk->walk.all);
919 walk->walk.dead = 1;
920 walk->type = type;
921 walk->seq = 0;
922}
923EXPORT_SYMBOL(xfrm_policy_walk_init);
924
925void xfrm_policy_walk_done(struct xfrm_policy_walk *walk)
926{
927 if (list_empty(&walk->walk.all))
928 return;
929
930 write_lock_bh(&xfrm_policy_lock);
931 list_del(&walk->walk.all);
932 write_unlock_bh(&xfrm_policy_lock);
933}
934EXPORT_SYMBOL(xfrm_policy_walk_done);
935
James Morris134b0fc2006-10-05 15:42:27 -0500936/*
937 * Find policy to apply to this flow.
938 *
939 * Returns 0 if policy found, else an -errno.
940 */
David S. Millerf299d552011-02-24 01:23:30 -0500941static int xfrm_policy_match(const struct xfrm_policy *pol,
942 const struct flowi *fl,
David S. Miller2518c7c2006-08-24 04:45:07 -0700943 u8 type, u16 family, int dir)
944{
David S. Millerf299d552011-02-24 01:23:30 -0500945 const struct xfrm_selector *sel = &pol->selector;
David S. Millerbc9b35a2012-05-15 15:04:57 -0400946 int ret = -ESRCH;
947 bool match;
David S. Miller2518c7c2006-08-24 04:45:07 -0700948
949 if (pol->family != family ||
David S. Miller1d28f422011-03-12 00:29:39 -0500950 (fl->flowi_mark & pol->mark.m) != pol->mark.v ||
David S. Miller2518c7c2006-08-24 04:45:07 -0700951 pol->type != type)
James Morris134b0fc2006-10-05 15:42:27 -0500952 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -0700953
954 match = xfrm_selector_match(sel, fl, family);
James Morris134b0fc2006-10-05 15:42:27 -0500955 if (match)
David S. Miller1d28f422011-03-12 00:29:39 -0500956 ret = security_xfrm_policy_lookup(pol->security, fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -0700957 dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700958
James Morris134b0fc2006-10-05 15:42:27 -0500959 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -0700960}
961
Alexey Dobriyan52479b62008-11-25 17:35:18 -0800962static struct xfrm_policy *xfrm_policy_lookup_bytype(struct net *net, u8 type,
David S. Miller062cdb42011-02-22 18:31:08 -0800963 const struct flowi *fl,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700964 u16 family, u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965{
James Morris134b0fc2006-10-05 15:42:27 -0500966 int err;
David S. Miller2518c7c2006-08-24 04:45:07 -0700967 struct xfrm_policy *pol, *ret;
David S. Miller0b597e72011-02-24 01:22:48 -0500968 const xfrm_address_t *daddr, *saddr;
David S. Miller2518c7c2006-08-24 04:45:07 -0700969 struct hlist_node *entry;
970 struct hlist_head *chain;
David S. Milleracba48e2006-08-25 15:46:46 -0700971 u32 priority = ~0U;
David S. Miller2518c7c2006-08-24 04:45:07 -0700972
973 daddr = xfrm_flowi_daddr(fl, family);
974 saddr = xfrm_flowi_saddr(fl, family);
975 if (unlikely(!daddr || !saddr))
976 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
978 read_lock_bh(&xfrm_policy_lock);
Alexey Dobriyan52479b62008-11-25 17:35:18 -0800979 chain = policy_hash_direct(net, daddr, saddr, family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700980 ret = NULL;
981 hlist_for_each_entry(pol, entry, chain, bydst) {
James Morris134b0fc2006-10-05 15:42:27 -0500982 err = xfrm_policy_match(pol, fl, type, family, dir);
983 if (err) {
984 if (err == -ESRCH)
985 continue;
986 else {
987 ret = ERR_PTR(err);
988 goto fail;
989 }
990 } else {
David S. Milleracba48e2006-08-25 15:46:46 -0700991 ret = pol;
992 priority = ret->priority;
993 break;
994 }
995 }
Alexey Dobriyan52479b62008-11-25 17:35:18 -0800996 chain = &net->xfrm.policy_inexact[dir];
David S. Milleracba48e2006-08-25 15:46:46 -0700997 hlist_for_each_entry(pol, entry, chain, bydst) {
James Morris134b0fc2006-10-05 15:42:27 -0500998 err = xfrm_policy_match(pol, fl, type, family, dir);
999 if (err) {
1000 if (err == -ESRCH)
1001 continue;
1002 else {
1003 ret = ERR_PTR(err);
1004 goto fail;
1005 }
1006 } else if (pol->priority < priority) {
David S. Miller2518c7c2006-08-24 04:45:07 -07001007 ret = pol;
1008 break;
1009 }
1010 }
David S. Milleracba48e2006-08-25 15:46:46 -07001011 if (ret)
1012 xfrm_pol_hold(ret);
James Morris134b0fc2006-10-05 15:42:27 -05001013fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 read_unlock_bh(&xfrm_policy_lock);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001015
David S. Miller2518c7c2006-08-24 04:45:07 -07001016 return ret;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001017}
1018
Timo Teräs80c802f2010-04-07 00:30:05 +00001019static struct xfrm_policy *
David S. Miller73ff93c2011-02-22 18:33:42 -08001020__xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir)
Timo Teräs80c802f2010-04-07 00:30:05 +00001021{
1022#ifdef CONFIG_XFRM_SUB_POLICY
1023 struct xfrm_policy *pol;
1024
1025 pol = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_SUB, fl, family, dir);
1026 if (pol != NULL)
1027 return pol;
1028#endif
1029 return xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN, fl, family, dir);
1030}
1031
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001032static struct flow_cache_object *
David S. Millerdee9f4b2011-02-22 18:44:31 -08001033xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family,
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001034 u8 dir, struct flow_cache_object *old_obj, void *ctx)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001035{
1036 struct xfrm_policy *pol;
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001037
1038 if (old_obj)
1039 xfrm_pol_put(container_of(old_obj, struct xfrm_policy, flo));
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001040
Timo Teräs80c802f2010-04-07 00:30:05 +00001041 pol = __xfrm_policy_lookup(net, fl, family, dir);
1042 if (IS_ERR_OR_NULL(pol))
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001043 return ERR_CAST(pol);
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001044
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001045 /* Resolver returns two references:
1046 * one for cache and one for caller of flow_cache_lookup() */
1047 xfrm_pol_hold(pol);
1048
1049 return &pol->flo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050}
1051
Trent Jaegerdf718372005-12-13 23:12:27 -08001052static inline int policy_to_flow_dir(int dir)
1053{
1054 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001055 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1056 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1057 return dir;
1058 switch (dir) {
1059 default:
1060 case XFRM_POLICY_IN:
1061 return FLOW_DIR_IN;
1062 case XFRM_POLICY_OUT:
1063 return FLOW_DIR_OUT;
1064 case XFRM_POLICY_FWD:
1065 return FLOW_DIR_FWD;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001066 }
Trent Jaegerdf718372005-12-13 23:12:27 -08001067}
1068
David S. Millerdee9f4b2011-02-22 18:44:31 -08001069static struct xfrm_policy *xfrm_sk_policy_lookup(struct sock *sk, int dir,
1070 const struct flowi *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
1072 struct xfrm_policy *pol;
1073
1074 read_lock_bh(&xfrm_policy_lock);
1075 if ((pol = sk->sk_policy[dir]) != NULL) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04001076 bool match = xfrm_selector_match(&pol->selector, fl,
1077 sk->sk_family);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001078 int err = 0;
Trent Jaegerdf718372005-12-13 23:12:27 -08001079
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001080 if (match) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001081 if ((sk->sk_mark & pol->mark.m) != pol->mark.v) {
1082 pol = NULL;
1083 goto out;
1084 }
Paul Moore03e1ad72008-04-12 19:07:52 -07001085 err = security_xfrm_policy_lookup(pol->security,
David S. Miller1d28f422011-03-12 00:29:39 -05001086 fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -07001087 policy_to_flow_dir(dir));
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001088 if (!err)
1089 xfrm_pol_hold(pol);
1090 else if (err == -ESRCH)
1091 pol = NULL;
1092 else
1093 pol = ERR_PTR(err);
1094 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 pol = NULL;
1096 }
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001097out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 read_unlock_bh(&xfrm_policy_lock);
1099 return pol;
1100}
1101
1102static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
1103{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001104 struct net *net = xp_net(pol);
Alexey Dobriyan11219942008-11-25 17:33:06 -08001105 struct hlist_head *chain = policy_hash_bysel(net, &pol->selector,
David S. Miller2518c7c2006-08-24 04:45:07 -07001106 pol->family, dir);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001107
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001108 list_add(&pol->walk.all, &net->xfrm.policy_all);
David S. Miller2518c7c2006-08-24 04:45:07 -07001109 hlist_add_head(&pol->bydst, chain);
Alexey Dobriyane92303f2008-11-25 17:32:41 -08001110 hlist_add_head(&pol->byidx, net->xfrm.policy_byidx+idx_hash(net, pol->index));
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001111 net->xfrm.policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -07001113
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001114 if (xfrm_bydst_should_resize(net, dir, NULL))
1115 schedule_work(&net->xfrm.policy_hash_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116}
1117
1118static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
1119 int dir)
1120{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001121 struct net *net = xp_net(pol);
1122
David S. Miller2518c7c2006-08-24 04:45:07 -07001123 if (hlist_unhashed(&pol->bydst))
1124 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
David S. Miller2518c7c2006-08-24 04:45:07 -07001126 hlist_del(&pol->bydst);
1127 hlist_del(&pol->byidx);
Herbert Xu12a169e2008-10-01 07:03:24 -07001128 list_del(&pol->walk.all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001129 net->xfrm.policy_count[dir]--;
David S. Miller2518c7c2006-08-24 04:45:07 -07001130
1131 return pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132}
1133
Herbert Xu4666faa2005-06-18 22:43:22 -07001134int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135{
1136 write_lock_bh(&xfrm_policy_lock);
1137 pol = __xfrm_policy_unlink(pol, dir);
1138 write_unlock_bh(&xfrm_policy_lock);
1139 if (pol) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 xfrm_policy_kill(pol);
Herbert Xu4666faa2005-06-18 22:43:22 -07001141 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 }
Herbert Xu4666faa2005-06-18 22:43:22 -07001143 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144}
David S. Millera70fcb02006-03-20 19:18:52 -08001145EXPORT_SYMBOL(xfrm_policy_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
1147int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
1148{
Alexey Dobriyan11219942008-11-25 17:33:06 -08001149 struct net *net = xp_net(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 struct xfrm_policy *old_pol;
1151
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001152#ifdef CONFIG_XFRM_SUB_POLICY
1153 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
1154 return -EINVAL;
1155#endif
1156
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 write_lock_bh(&xfrm_policy_lock);
1158 old_pol = sk->sk_policy[dir];
1159 sk->sk_policy[dir] = pol;
1160 if (pol) {
James Morris9d729f72007-03-04 16:12:44 -08001161 pol->curlft.add_time = get_seconds();
Alexey Dobriyan11219942008-11-25 17:33:06 -08001162 pol->index = xfrm_gen_index(net, XFRM_POLICY_MAX+dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 __xfrm_policy_link(pol, XFRM_POLICY_MAX+dir);
1164 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001165 if (old_pol) {
1166 if (pol)
1167 xfrm_policy_requeue(old_pol, pol);
1168
Timo Teräsea2dea92010-03-31 00:17:05 +00001169 /* Unlinking succeeds always. This is the only function
1170 * allowed to delete or replace socket policy.
1171 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 __xfrm_policy_unlink(old_pol, XFRM_POLICY_MAX+dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 write_unlock_bh(&xfrm_policy_lock);
1175
1176 if (old_pol) {
1177 xfrm_policy_kill(old_pol);
1178 }
1179 return 0;
1180}
1181
David S. Millerd3e40a92011-02-24 01:25:41 -05001182static struct xfrm_policy *clone_policy(const struct xfrm_policy *old, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183{
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -08001184 struct xfrm_policy *newp = xfrm_policy_alloc(xp_net(old), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
1186 if (newp) {
1187 newp->selector = old->selector;
Paul Moore03e1ad72008-04-12 19:07:52 -07001188 if (security_xfrm_policy_clone(old->security,
1189 &newp->security)) {
Trent Jaegerdf718372005-12-13 23:12:27 -08001190 kfree(newp);
1191 return NULL; /* ENOMEM */
1192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 newp->lft = old->lft;
1194 newp->curlft = old->curlft;
Jamal Hadi Salimfb977e22010-02-23 15:09:53 -08001195 newp->mark = old->mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 newp->action = old->action;
1197 newp->flags = old->flags;
1198 newp->xfrm_nr = old->xfrm_nr;
1199 newp->index = old->index;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001200 newp->type = old->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 memcpy(newp->xfrm_vec, old->xfrm_vec,
1202 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
1203 write_lock_bh(&xfrm_policy_lock);
1204 __xfrm_policy_link(newp, XFRM_POLICY_MAX+dir);
1205 write_unlock_bh(&xfrm_policy_lock);
1206 xfrm_pol_put(newp);
1207 }
1208 return newp;
1209}
1210
1211int __xfrm_sk_clone_policy(struct sock *sk)
1212{
1213 struct xfrm_policy *p0 = sk->sk_policy[0],
1214 *p1 = sk->sk_policy[1];
1215
1216 sk->sk_policy[0] = sk->sk_policy[1] = NULL;
1217 if (p0 && (sk->sk_policy[0] = clone_policy(p0, 0)) == NULL)
1218 return -ENOMEM;
1219 if (p1 && (sk->sk_policy[1] = clone_policy(p1, 1)) == NULL)
1220 return -ENOMEM;
1221 return 0;
1222}
1223
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001224static int
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001225xfrm_get_saddr(struct net *net, xfrm_address_t *local, xfrm_address_t *remote,
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001226 unsigned short family)
1227{
1228 int err;
1229 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1230
1231 if (unlikely(afinfo == NULL))
1232 return -EINVAL;
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001233 err = afinfo->get_saddr(net, local, remote);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001234 xfrm_policy_put_afinfo(afinfo);
1235 return err;
1236}
1237
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238/* Resolve list of templates for the flow, given policy. */
1239
1240static int
David S. Millera6c2e612011-02-22 18:35:39 -08001241xfrm_tmpl_resolve_one(struct xfrm_policy *policy, const struct flowi *fl,
1242 struct xfrm_state **xfrm, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243{
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001244 struct net *net = xp_net(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 int nx;
1246 int i, error;
1247 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
1248 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001249 xfrm_address_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
1251 for (nx=0, i = 0; i < policy->xfrm_nr; i++) {
1252 struct xfrm_state *x;
1253 xfrm_address_t *remote = daddr;
1254 xfrm_address_t *local = saddr;
1255 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
1256
Joakim Koskela48b8d782007-07-26 00:08:42 -07001257 if (tmpl->mode == XFRM_MODE_TUNNEL ||
1258 tmpl->mode == XFRM_MODE_BEET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 remote = &tmpl->id.daddr;
1260 local = &tmpl->saddr;
Thomas Egerer8444cf72010-09-20 11:11:38 -07001261 if (xfrm_addr_any(local, tmpl->encap_family)) {
1262 error = xfrm_get_saddr(net, &tmp, remote, tmpl->encap_family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001263 if (error)
1264 goto fail;
1265 local = &tmp;
1266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 }
1268
1269 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
1270
1271 if (x && x->km.state == XFRM_STATE_VALID) {
1272 xfrm[nx++] = x;
1273 daddr = remote;
1274 saddr = local;
1275 continue;
1276 }
1277 if (x) {
1278 error = (x->km.state == XFRM_STATE_ERROR ?
1279 -EINVAL : -EAGAIN);
1280 xfrm_state_put(x);
1281 }
fernando@oss.ntt.coa43222662008-10-23 04:27:19 +00001282 else if (error == -ESRCH)
1283 error = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285 if (!tmpl->optional)
1286 goto fail;
1287 }
1288 return nx;
1289
1290fail:
1291 for (nx--; nx>=0; nx--)
1292 xfrm_state_put(xfrm[nx]);
1293 return error;
1294}
1295
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001296static int
David S. Millera6c2e612011-02-22 18:35:39 -08001297xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, const struct flowi *fl,
1298 struct xfrm_state **xfrm, unsigned short family)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001299{
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001300 struct xfrm_state *tp[XFRM_MAX_DEPTH];
1301 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001302 int cnx = 0;
1303 int error;
1304 int ret;
1305 int i;
1306
1307 for (i = 0; i < npols; i++) {
1308 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
1309 error = -ENOBUFS;
1310 goto fail;
1311 }
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001312
1313 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001314 if (ret < 0) {
1315 error = ret;
1316 goto fail;
1317 } else
1318 cnx += ret;
1319 }
1320
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001321 /* found states are sorted for outbound processing */
1322 if (npols > 1)
1323 xfrm_state_sort(xfrm, tpp, cnx, family);
1324
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001325 return cnx;
1326
1327 fail:
1328 for (cnx--; cnx>=0; cnx--)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001329 xfrm_state_put(tpp[cnx]);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001330 return error;
1331
1332}
1333
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334/* Check that the bundle accepts the flow and its components are
1335 * still valid.
1336 */
1337
David S. Miller05d84022011-02-22 17:47:10 -08001338static inline int xfrm_get_tos(const struct flowi *fl, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08001339{
1340 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1341 int tos;
1342
1343 if (!afinfo)
1344 return -EINVAL;
1345
1346 tos = afinfo->get_tos(fl);
1347
1348 xfrm_policy_put_afinfo(afinfo);
1349
1350 return tos;
1351}
1352
Timo Teräs80c802f2010-04-07 00:30:05 +00001353static struct flow_cache_object *xfrm_bundle_flo_get(struct flow_cache_object *flo)
1354{
1355 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1356 struct dst_entry *dst = &xdst->u.dst;
1357
1358 if (xdst->route == NULL) {
1359 /* Dummy bundle - if it has xfrms we were not
1360 * able to build bundle as template resolution failed.
1361 * It means we need to try again resolving. */
1362 if (xdst->num_xfrms > 0)
1363 return NULL;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001364 } else if (dst->flags & DST_XFRM_QUEUE) {
1365 return NULL;
Timo Teräs80c802f2010-04-07 00:30:05 +00001366 } else {
1367 /* Real bundle */
1368 if (stale_bundle(dst))
1369 return NULL;
1370 }
1371
1372 dst_hold(dst);
1373 return flo;
1374}
1375
1376static int xfrm_bundle_flo_check(struct flow_cache_object *flo)
1377{
1378 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1379 struct dst_entry *dst = &xdst->u.dst;
1380
1381 if (!xdst->route)
1382 return 0;
1383 if (stale_bundle(dst))
1384 return 0;
1385
1386 return 1;
1387}
1388
1389static void xfrm_bundle_flo_delete(struct flow_cache_object *flo)
1390{
1391 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1392 struct dst_entry *dst = &xdst->u.dst;
1393
1394 dst_free(dst);
1395}
1396
1397static const struct flow_cache_ops xfrm_bundle_fc_ops = {
1398 .get = xfrm_bundle_flo_get,
1399 .check = xfrm_bundle_flo_check,
1400 .delete = xfrm_bundle_flo_delete,
1401};
1402
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001403static inline struct xfrm_dst *xfrm_alloc_dst(struct net *net, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08001404{
1405 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001406 struct dst_ops *dst_ops;
Herbert Xu25ee3282007-12-11 09:32:34 -08001407 struct xfrm_dst *xdst;
1408
1409 if (!afinfo)
1410 return ERR_PTR(-EINVAL);
1411
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001412 switch (family) {
1413 case AF_INET:
1414 dst_ops = &net->xfrm.xfrm4_dst_ops;
1415 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001416#if IS_ENABLED(CONFIG_IPV6)
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001417 case AF_INET6:
1418 dst_ops = &net->xfrm.xfrm6_dst_ops;
1419 break;
1420#endif
1421 default:
1422 BUG();
1423 }
David S. Millerf5b0a872012-07-19 12:31:33 -07001424 xdst = dst_alloc(dst_ops, NULL, 0, DST_OBSOLETE_NONE, 0);
Herbert Xu25ee3282007-12-11 09:32:34 -08001425
Madalin Bucurd4cae562011-09-26 07:04:36 +00001426 if (likely(xdst)) {
Steffen Klassert141e3692012-07-05 23:39:34 +00001427 struct dst_entry *dst = &xdst->u.dst;
1428
1429 memset(dst + 1, 0, sizeof(*xdst) - sizeof(*dst));
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08001430 xdst->flo.ops = &xfrm_bundle_fc_ops;
Patrick McHardy9d7b0fc2012-08-20 02:56:56 -07001431 if (afinfo->init_dst)
1432 afinfo->init_dst(net, xdst);
Madalin Bucurd4cae562011-09-26 07:04:36 +00001433 } else
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08001434 xdst = ERR_PTR(-ENOBUFS);
Timo Teräs80c802f2010-04-07 00:30:05 +00001435
Madalin Bucurd4cae562011-09-26 07:04:36 +00001436 xfrm_policy_put_afinfo(afinfo);
1437
Herbert Xu25ee3282007-12-11 09:32:34 -08001438 return xdst;
1439}
1440
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001441static inline int xfrm_init_path(struct xfrm_dst *path, struct dst_entry *dst,
1442 int nfheader_len)
1443{
1444 struct xfrm_policy_afinfo *afinfo =
1445 xfrm_policy_get_afinfo(dst->ops->family);
1446 int err;
1447
1448 if (!afinfo)
1449 return -EINVAL;
1450
1451 err = afinfo->init_path(path, dst, nfheader_len);
1452
1453 xfrm_policy_put_afinfo(afinfo);
1454
1455 return err;
1456}
1457
Herbert Xu87c1e122010-03-02 02:51:56 +00001458static inline int xfrm_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
David S. Miller0c7b3ee2011-02-22 17:48:57 -08001459 const struct flowi *fl)
Herbert Xu25ee3282007-12-11 09:32:34 -08001460{
1461 struct xfrm_policy_afinfo *afinfo =
1462 xfrm_policy_get_afinfo(xdst->u.dst.ops->family);
1463 int err;
1464
1465 if (!afinfo)
1466 return -EINVAL;
1467
Herbert Xu87c1e122010-03-02 02:51:56 +00001468 err = afinfo->fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001469
1470 xfrm_policy_put_afinfo(afinfo);
1471
1472 return err;
1473}
1474
Timo Teräs80c802f2010-04-07 00:30:05 +00001475
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476/* Allocate chain of dst_entry's, attach known xfrm's, calculate
1477 * all the metrics... Shortly, bundle a bundle.
1478 */
1479
Herbert Xu25ee3282007-12-11 09:32:34 -08001480static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy,
1481 struct xfrm_state **xfrm, int nx,
David S. Miller98313ad2011-02-22 18:36:50 -08001482 const struct flowi *fl,
Herbert Xu25ee3282007-12-11 09:32:34 -08001483 struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484{
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001485 struct net *net = xp_net(policy);
Herbert Xu25ee3282007-12-11 09:32:34 -08001486 unsigned long now = jiffies;
1487 struct net_device *dev;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001488 struct xfrm_mode *inner_mode;
Herbert Xu25ee3282007-12-11 09:32:34 -08001489 struct dst_entry *dst_prev = NULL;
1490 struct dst_entry *dst0 = NULL;
1491 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 int err;
Herbert Xu25ee3282007-12-11 09:32:34 -08001493 int header_len = 0;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001494 int nfheader_len = 0;
Herbert Xu25ee3282007-12-11 09:32:34 -08001495 int trailer_len = 0;
1496 int tos;
1497 int family = policy->selector.family;
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +09001498 xfrm_address_t saddr, daddr;
1499
1500 xfrm_flowi_addr_get(fl, &saddr, &daddr, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001501
1502 tos = xfrm_get_tos(fl, family);
1503 err = tos;
1504 if (tos < 0)
1505 goto put_states;
1506
1507 dst_hold(dst);
1508
1509 for (; i < nx; i++) {
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001510 struct xfrm_dst *xdst = xfrm_alloc_dst(net, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001511 struct dst_entry *dst1 = &xdst->u.dst;
1512
1513 err = PTR_ERR(xdst);
1514 if (IS_ERR(xdst)) {
1515 dst_release(dst);
1516 goto put_states;
1517 }
1518
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001519 if (xfrm[i]->sel.family == AF_UNSPEC) {
1520 inner_mode = xfrm_ip2inner_mode(xfrm[i],
1521 xfrm_af2proto(family));
1522 if (!inner_mode) {
1523 err = -EAFNOSUPPORT;
1524 dst_release(dst);
1525 goto put_states;
1526 }
1527 } else
1528 inner_mode = xfrm[i]->inner_mode;
1529
Herbert Xu25ee3282007-12-11 09:32:34 -08001530 if (!dst_prev)
1531 dst0 = dst1;
1532 else {
1533 dst_prev->child = dst_clone(dst1);
1534 dst1->flags |= DST_NOHASH;
1535 }
1536
1537 xdst->route = dst;
David S. Millerdefb3512010-12-08 21:16:57 -08001538 dst_copy_metrics(dst1, dst);
Herbert Xu25ee3282007-12-11 09:32:34 -08001539
1540 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
1541 family = xfrm[i]->props.family;
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +09001542 dst = xfrm_dst_lookup(xfrm[i], tos, &saddr, &daddr,
1543 family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001544 err = PTR_ERR(dst);
1545 if (IS_ERR(dst))
1546 goto put_states;
1547 } else
1548 dst_hold(dst);
1549
1550 dst1->xfrm = xfrm[i];
Timo Teräs80c802f2010-04-07 00:30:05 +00001551 xdst->xfrm_genid = xfrm[i]->genid;
Herbert Xu25ee3282007-12-11 09:32:34 -08001552
David S. Millerf5b0a872012-07-19 12:31:33 -07001553 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
Herbert Xu25ee3282007-12-11 09:32:34 -08001554 dst1->flags |= DST_HOST;
1555 dst1->lastuse = now;
1556
1557 dst1->input = dst_discard;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001558 dst1->output = inner_mode->afinfo->output;
Herbert Xu25ee3282007-12-11 09:32:34 -08001559
1560 dst1->next = dst_prev;
1561 dst_prev = dst1;
1562
1563 header_len += xfrm[i]->props.header_len;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001564 if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT)
1565 nfheader_len += xfrm[i]->props.header_len;
Herbert Xu25ee3282007-12-11 09:32:34 -08001566 trailer_len += xfrm[i]->props.trailer_len;
1567 }
1568
1569 dst_prev->child = dst;
1570 dst0->path = dst;
1571
1572 err = -ENODEV;
1573 dev = dst->dev;
1574 if (!dev)
1575 goto free_dst;
1576
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001577 xfrm_init_path((struct xfrm_dst *)dst0, dst, nfheader_len);
Herbert Xu25ee3282007-12-11 09:32:34 -08001578 xfrm_init_pmtu(dst_prev);
1579
1580 for (dst_prev = dst0; dst_prev != dst; dst_prev = dst_prev->child) {
1581 struct xfrm_dst *xdst = (struct xfrm_dst *)dst_prev;
1582
Herbert Xu87c1e122010-03-02 02:51:56 +00001583 err = xfrm_fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001584 if (err)
1585 goto free_dst;
1586
1587 dst_prev->header_len = header_len;
1588 dst_prev->trailer_len = trailer_len;
1589 header_len -= xdst->u.dst.xfrm->props.header_len;
1590 trailer_len -= xdst->u.dst.xfrm->props.trailer_len;
1591 }
1592
1593out:
1594 return dst0;
1595
1596put_states:
1597 for (; i < nx; i++)
1598 xfrm_state_put(xfrm[i]);
1599free_dst:
1600 if (dst0)
1601 dst_free(dst0);
1602 dst0 = ERR_PTR(err);
1603 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604}
1605
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001606static int inline
David S. Miller3f0e18f2011-02-22 18:38:14 -08001607xfrm_dst_alloc_copy(void **target, const void *src, int size)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001608{
1609 if (!*target) {
1610 *target = kmalloc(size, GFP_ATOMIC);
1611 if (!*target)
1612 return -ENOMEM;
1613 }
1614 memcpy(*target, src, size);
1615 return 0;
1616}
1617
1618static int inline
David S. Miller1786b382011-02-24 01:32:54 -05001619xfrm_dst_update_parent(struct dst_entry *dst, const struct xfrm_selector *sel)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001620{
1621#ifdef CONFIG_XFRM_SUB_POLICY
1622 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1623 return xfrm_dst_alloc_copy((void **)&(xdst->partner),
1624 sel, sizeof(*sel));
1625#else
1626 return 0;
1627#endif
1628}
1629
1630static int inline
David S. Miller3f0e18f2011-02-22 18:38:14 -08001631xfrm_dst_update_origin(struct dst_entry *dst, const struct flowi *fl)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001632{
1633#ifdef CONFIG_XFRM_SUB_POLICY
1634 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1635 return xfrm_dst_alloc_copy((void **)&(xdst->origin), fl, sizeof(*fl));
1636#else
1637 return 0;
1638#endif
1639}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
David S. Miller73ff93c2011-02-22 18:33:42 -08001641static int xfrm_expand_policies(const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001642 struct xfrm_policy **pols,
1643 int *num_pols, int *num_xfrms)
1644{
1645 int i;
1646
1647 if (*num_pols == 0 || !pols[0]) {
1648 *num_pols = 0;
1649 *num_xfrms = 0;
1650 return 0;
1651 }
1652 if (IS_ERR(pols[0]))
1653 return PTR_ERR(pols[0]);
1654
1655 *num_xfrms = pols[0]->xfrm_nr;
1656
1657#ifdef CONFIG_XFRM_SUB_POLICY
1658 if (pols[0] && pols[0]->action == XFRM_POLICY_ALLOW &&
1659 pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1660 pols[1] = xfrm_policy_lookup_bytype(xp_net(pols[0]),
1661 XFRM_POLICY_TYPE_MAIN,
1662 fl, family,
1663 XFRM_POLICY_OUT);
1664 if (pols[1]) {
1665 if (IS_ERR(pols[1])) {
1666 xfrm_pols_put(pols, *num_pols);
1667 return PTR_ERR(pols[1]);
1668 }
1669 (*num_pols) ++;
1670 (*num_xfrms) += pols[1]->xfrm_nr;
1671 }
1672 }
1673#endif
1674 for (i = 0; i < *num_pols; i++) {
1675 if (pols[i]->action != XFRM_POLICY_ALLOW) {
1676 *num_xfrms = -1;
1677 break;
1678 }
1679 }
1680
1681 return 0;
1682
1683}
1684
1685static struct xfrm_dst *
1686xfrm_resolve_and_create_bundle(struct xfrm_policy **pols, int num_pols,
David S. Miller4ca2e682011-02-22 18:38:51 -08001687 const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001688 struct dst_entry *dst_orig)
1689{
1690 struct net *net = xp_net(pols[0]);
1691 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
1692 struct dst_entry *dst;
1693 struct xfrm_dst *xdst;
1694 int err;
1695
1696 /* Try to instantiate a bundle */
1697 err = xfrm_tmpl_resolve(pols, num_pols, fl, xfrm, family);
Timo Teräsd809ec82010-07-12 21:29:42 +00001698 if (err <= 0) {
1699 if (err != 0 && err != -EAGAIN)
Timo Teräs80c802f2010-04-07 00:30:05 +00001700 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
1701 return ERR_PTR(err);
1702 }
1703
1704 dst = xfrm_bundle_create(pols[0], xfrm, err, fl, dst_orig);
1705 if (IS_ERR(dst)) {
1706 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLEGENERROR);
1707 return ERR_CAST(dst);
1708 }
1709
1710 xdst = (struct xfrm_dst *)dst;
1711 xdst->num_xfrms = err;
1712 if (num_pols > 1)
1713 err = xfrm_dst_update_parent(dst, &pols[1]->selector);
1714 else
1715 err = xfrm_dst_update_origin(dst, fl);
1716 if (unlikely(err)) {
1717 dst_free(dst);
1718 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLECHECKERROR);
1719 return ERR_PTR(err);
1720 }
1721
1722 xdst->num_pols = num_pols;
1723 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy*) * num_pols);
1724 xdst->policy_genid = atomic_read(&pols[0]->genid);
1725
1726 return xdst;
1727}
1728
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001729static void xfrm_policy_queue_process(unsigned long arg)
1730{
1731 int err = 0;
1732 struct sk_buff *skb;
1733 struct sock *sk;
1734 struct dst_entry *dst;
1735 struct net_device *dev;
1736 struct xfrm_policy *pol = (struct xfrm_policy *)arg;
1737 struct xfrm_policy_queue *pq = &pol->polq;
1738 struct flowi fl;
1739 struct sk_buff_head list;
1740
1741 spin_lock(&pq->hold_queue.lock);
1742 skb = skb_peek(&pq->hold_queue);
1743 dst = skb_dst(skb);
1744 sk = skb->sk;
1745 xfrm_decode_session(skb, &fl, dst->ops->family);
1746 spin_unlock(&pq->hold_queue.lock);
1747
1748 dst_hold(dst->path);
1749 dst = xfrm_lookup(xp_net(pol), dst->path, &fl,
1750 sk, 0);
1751 if (IS_ERR(dst))
1752 goto purge_queue;
1753
1754 if (dst->flags & DST_XFRM_QUEUE) {
1755 dst_release(dst);
1756
1757 if (pq->timeout >= XFRM_QUEUE_TMO_MAX)
1758 goto purge_queue;
1759
1760 pq->timeout = pq->timeout << 1;
1761 mod_timer(&pq->hold_timer, jiffies + pq->timeout);
1762 return;
1763 }
1764
1765 dst_release(dst);
1766
1767 __skb_queue_head_init(&list);
1768
1769 spin_lock(&pq->hold_queue.lock);
1770 pq->timeout = 0;
1771 skb_queue_splice_init(&pq->hold_queue, &list);
1772 spin_unlock(&pq->hold_queue.lock);
1773
1774 while (!skb_queue_empty(&list)) {
1775 skb = __skb_dequeue(&list);
1776
1777 xfrm_decode_session(skb, &fl, skb_dst(skb)->ops->family);
1778 dst_hold(skb_dst(skb)->path);
1779 dst = xfrm_lookup(xp_net(pol), skb_dst(skb)->path,
1780 &fl, skb->sk, 0);
1781 if (IS_ERR(dst)) {
1782 dev_put(skb->dev);
1783 kfree_skb(skb);
1784 continue;
1785 }
1786
1787 nf_reset(skb);
1788 skb_dst_drop(skb);
1789 skb_dst_set(skb, dst);
1790
1791 dev = skb->dev;
1792 err = dst_output(skb);
1793 dev_put(dev);
1794 }
1795
1796 return;
1797
1798purge_queue:
1799 pq->timeout = 0;
1800 xfrm_queue_purge(&pq->hold_queue);
1801}
1802
1803static int xdst_queue_output(struct sk_buff *skb)
1804{
1805 unsigned long sched_next;
1806 struct dst_entry *dst = skb_dst(skb);
1807 struct xfrm_dst *xdst = (struct xfrm_dst *) dst;
1808 struct xfrm_policy_queue *pq = &xdst->pols[0]->polq;
1809
1810 if (pq->hold_queue.qlen > XFRM_MAX_QUEUE_LEN) {
1811 kfree_skb(skb);
1812 return -EAGAIN;
1813 }
1814
1815 skb_dst_force(skb);
1816 dev_hold(skb->dev);
1817
1818 spin_lock_bh(&pq->hold_queue.lock);
1819
1820 if (!pq->timeout)
1821 pq->timeout = XFRM_QUEUE_TMO_MIN;
1822
1823 sched_next = jiffies + pq->timeout;
1824
1825 if (del_timer(&pq->hold_timer)) {
1826 if (time_before(pq->hold_timer.expires, sched_next))
1827 sched_next = pq->hold_timer.expires;
1828 }
1829
1830 __skb_queue_tail(&pq->hold_queue, skb);
1831 mod_timer(&pq->hold_timer, sched_next);
1832
1833 spin_unlock_bh(&pq->hold_queue.lock);
1834
1835 return 0;
1836}
1837
1838static struct xfrm_dst *xfrm_create_dummy_bundle(struct net *net,
1839 struct dst_entry *dst,
1840 const struct flowi *fl,
1841 int num_xfrms,
1842 u16 family)
1843{
1844 int err;
1845 struct net_device *dev;
1846 struct dst_entry *dst1;
1847 struct xfrm_dst *xdst;
1848
1849 xdst = xfrm_alloc_dst(net, family);
1850 if (IS_ERR(xdst))
1851 return xdst;
1852
1853 if (net->xfrm.sysctl_larval_drop || num_xfrms <= 0 ||
1854 (fl->flowi_flags & FLOWI_FLAG_CAN_SLEEP))
1855 return xdst;
1856
1857 dst1 = &xdst->u.dst;
1858 dst_hold(dst);
1859 xdst->route = dst;
1860
1861 dst_copy_metrics(dst1, dst);
1862
1863 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
1864 dst1->flags |= DST_HOST | DST_XFRM_QUEUE;
1865 dst1->lastuse = jiffies;
1866
1867 dst1->input = dst_discard;
1868 dst1->output = xdst_queue_output;
1869
1870 dst_hold(dst);
1871 dst1->child = dst;
1872 dst1->path = dst;
1873
1874 xfrm_init_path((struct xfrm_dst *)dst1, dst, 0);
1875
1876 err = -ENODEV;
1877 dev = dst->dev;
1878 if (!dev)
1879 goto free_dst;
1880
1881 err = xfrm_fill_dst(xdst, dev, fl);
1882 if (err)
1883 goto free_dst;
1884
1885out:
1886 return xdst;
1887
1888free_dst:
1889 dst_release(dst1);
1890 xdst = ERR_PTR(err);
1891 goto out;
1892}
1893
Timo Teräs80c802f2010-04-07 00:30:05 +00001894static struct flow_cache_object *
David S. Millerdee9f4b2011-02-22 18:44:31 -08001895xfrm_bundle_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir,
Timo Teräs80c802f2010-04-07 00:30:05 +00001896 struct flow_cache_object *oldflo, void *ctx)
1897{
1898 struct dst_entry *dst_orig = (struct dst_entry *)ctx;
1899 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
1900 struct xfrm_dst *xdst, *new_xdst;
1901 int num_pols = 0, num_xfrms = 0, i, err, pol_dead;
1902
1903 /* Check if the policies from old bundle are usable */
1904 xdst = NULL;
1905 if (oldflo) {
1906 xdst = container_of(oldflo, struct xfrm_dst, flo);
1907 num_pols = xdst->num_pols;
1908 num_xfrms = xdst->num_xfrms;
1909 pol_dead = 0;
1910 for (i = 0; i < num_pols; i++) {
1911 pols[i] = xdst->pols[i];
1912 pol_dead |= pols[i]->walk.dead;
1913 }
1914 if (pol_dead) {
1915 dst_free(&xdst->u.dst);
1916 xdst = NULL;
1917 num_pols = 0;
1918 num_xfrms = 0;
1919 oldflo = NULL;
1920 }
1921 }
1922
1923 /* Resolve policies to use if we couldn't get them from
1924 * previous cache entry */
1925 if (xdst == NULL) {
1926 num_pols = 1;
1927 pols[0] = __xfrm_policy_lookup(net, fl, family, dir);
1928 err = xfrm_expand_policies(fl, family, pols,
1929 &num_pols, &num_xfrms);
1930 if (err < 0)
1931 goto inc_error;
1932 if (num_pols == 0)
1933 return NULL;
1934 if (num_xfrms <= 0)
1935 goto make_dummy_bundle;
1936 }
1937
1938 new_xdst = xfrm_resolve_and_create_bundle(pols, num_pols, fl, family, dst_orig);
1939 if (IS_ERR(new_xdst)) {
1940 err = PTR_ERR(new_xdst);
1941 if (err != -EAGAIN)
1942 goto error;
1943 if (oldflo == NULL)
1944 goto make_dummy_bundle;
1945 dst_hold(&xdst->u.dst);
1946 return oldflo;
Timo Teräsd809ec82010-07-12 21:29:42 +00001947 } else if (new_xdst == NULL) {
1948 num_xfrms = 0;
1949 if (oldflo == NULL)
1950 goto make_dummy_bundle;
1951 xdst->num_xfrms = 0;
1952 dst_hold(&xdst->u.dst);
1953 return oldflo;
Timo Teräs80c802f2010-04-07 00:30:05 +00001954 }
1955
1956 /* Kill the previous bundle */
1957 if (xdst) {
1958 /* The policies were stolen for newly generated bundle */
1959 xdst->num_pols = 0;
1960 dst_free(&xdst->u.dst);
1961 }
1962
1963 /* Flow cache does not have reference, it dst_free()'s,
1964 * but we do need to return one reference for original caller */
1965 dst_hold(&new_xdst->u.dst);
1966 return &new_xdst->flo;
1967
1968make_dummy_bundle:
1969 /* We found policies, but there's no bundles to instantiate:
1970 * either because the policy blocks, has no transformations or
1971 * we could not build template (no xfrm_states).*/
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001972 xdst = xfrm_create_dummy_bundle(net, dst_orig, fl, num_xfrms, family);
Timo Teräs80c802f2010-04-07 00:30:05 +00001973 if (IS_ERR(xdst)) {
1974 xfrm_pols_put(pols, num_pols);
1975 return ERR_CAST(xdst);
1976 }
1977 xdst->num_pols = num_pols;
1978 xdst->num_xfrms = num_xfrms;
1979 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy*) * num_pols);
1980
1981 dst_hold(&xdst->u.dst);
1982 return &xdst->flo;
1983
1984inc_error:
1985 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
1986error:
1987 if (xdst != NULL)
1988 dst_free(&xdst->u.dst);
1989 else
1990 xfrm_pols_put(pols, num_pols);
1991 return ERR_PTR(err);
1992}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
David S. Miller2774c132011-03-01 14:59:04 -08001994static struct dst_entry *make_blackhole(struct net *net, u16 family,
1995 struct dst_entry *dst_orig)
1996{
1997 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1998 struct dst_entry *ret;
1999
2000 if (!afinfo) {
2001 dst_release(dst_orig);
Li RongQing433a1952012-09-17 22:40:10 +00002002 return ERR_PTR(-EINVAL);
David S. Miller2774c132011-03-01 14:59:04 -08002003 } else {
2004 ret = afinfo->blackhole_route(net, dst_orig);
2005 }
2006 xfrm_policy_put_afinfo(afinfo);
2007
2008 return ret;
2009}
2010
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011/* Main function: finds/creates a bundle for given flow.
2012 *
2013 * At the moment we eat a raw IP route. Mostly to speed up lookups
2014 * on interfaces with disabled IPsec.
2015 */
David S. Miller452edd52011-03-02 13:27:41 -08002016struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
2017 const struct flowi *fl,
2018 struct sock *sk, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019{
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002020 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
Timo Teräs80c802f2010-04-07 00:30:05 +00002021 struct flow_cache_object *flo;
2022 struct xfrm_dst *xdst;
David S. Miller452edd52011-03-02 13:27:41 -08002023 struct dst_entry *dst, *route;
Timo Teräs80c802f2010-04-07 00:30:05 +00002024 u16 family = dst_orig->ops->family;
Trent Jaegerdf718372005-12-13 23:12:27 -08002025 u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
Changli Gao4b021622010-04-27 21:20:22 +00002026 int i, err, num_pols, num_xfrms = 0, drop_pols = 0;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002027
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028restart:
Timo Teräs80c802f2010-04-07 00:30:05 +00002029 dst = NULL;
2030 xdst = NULL;
2031 route = NULL;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002032
Thomas Graff7944fb2007-08-25 13:46:55 -07002033 if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002034 num_pols = 1;
2035 pols[0] = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
2036 err = xfrm_expand_policies(fl, family, pols,
2037 &num_pols, &num_xfrms);
2038 if (err < 0)
Herbert Xu75b8c132007-12-11 04:38:08 -08002039 goto dropdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002040
2041 if (num_pols) {
2042 if (num_xfrms <= 0) {
2043 drop_pols = num_pols;
2044 goto no_transform;
2045 }
2046
2047 xdst = xfrm_resolve_and_create_bundle(
2048 pols, num_pols, fl,
2049 family, dst_orig);
2050 if (IS_ERR(xdst)) {
2051 xfrm_pols_put(pols, num_pols);
2052 err = PTR_ERR(xdst);
2053 goto dropdst;
Timo Teräsd809ec82010-07-12 21:29:42 +00002054 } else if (xdst == NULL) {
2055 num_xfrms = 0;
2056 drop_pols = num_pols;
2057 goto no_transform;
Timo Teräs80c802f2010-04-07 00:30:05 +00002058 }
2059
Steffen Klassertfbd50602011-03-15 21:12:49 +00002060 dst_hold(&xdst->u.dst);
2061
Timo Teräs80c802f2010-04-07 00:30:05 +00002062 spin_lock_bh(&xfrm_policy_sk_bundle_lock);
2063 xdst->u.dst.next = xfrm_policy_sk_bundles;
2064 xfrm_policy_sk_bundles = &xdst->u.dst;
2065 spin_unlock_bh(&xfrm_policy_sk_bundle_lock);
2066
2067 route = xdst->route;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002068 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
Timo Teräs80c802f2010-04-07 00:30:05 +00002071 if (xdst == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 /* To accelerate a bit... */
David S. Miller2518c7c2006-08-24 04:45:07 -07002073 if ((dst_orig->flags & DST_NOXFRM) ||
Alexey Dobriyan52479b62008-11-25 17:35:18 -08002074 !net->xfrm.policy_count[XFRM_POLICY_OUT])
Herbert Xu8b7817f2007-12-12 10:44:43 -08002075 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076
Timo Teräs80c802f2010-04-07 00:30:05 +00002077 flo = flow_cache_lookup(net, fl, family, dir,
2078 xfrm_bundle_lookup, dst_orig);
2079 if (flo == NULL)
2080 goto nopol;
Timo Teräsfe1a5f02010-04-07 00:30:04 +00002081 if (IS_ERR(flo)) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002082 err = PTR_ERR(flo);
Herbert Xu75b8c132007-12-11 04:38:08 -08002083 goto dropdst;
Masahide NAKAMURAd66e37a2008-01-07 21:46:15 -08002084 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002085 xdst = container_of(flo, struct xfrm_dst, flo);
2086
2087 num_pols = xdst->num_pols;
2088 num_xfrms = xdst->num_xfrms;
2089 memcpy(pols, xdst->pols, sizeof(struct xfrm_policy*) * num_pols);
2090 route = xdst->route;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 }
2092
Timo Teräs80c802f2010-04-07 00:30:05 +00002093 dst = &xdst->u.dst;
2094 if (route == NULL && num_xfrms > 0) {
2095 /* The only case when xfrm_bundle_lookup() returns a
2096 * bundle with null route, is when the template could
2097 * not be resolved. It means policies are there, but
2098 * bundle could not be created, since we don't yet
2099 * have the xfrm_state's. We need to wait for KM to
2100 * negotiate new SA's or bail out with error.*/
2101 if (net->xfrm.sysctl_larval_drop) {
2102 /* EREMOTE tells the caller to generate
2103 * a one-shot blackhole route. */
2104 dst_release(dst);
Timo Terasa1aa3482010-05-15 23:49:26 -07002105 xfrm_pols_put(pols, drop_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002106 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
David S. Miller2774c132011-03-01 14:59:04 -08002107
David S. Miller452edd52011-03-02 13:27:41 -08002108 return make_blackhole(net, family, dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002109 }
David S. Miller1d28f422011-03-12 00:29:39 -05002110 if (fl->flowi_flags & FLOWI_FLAG_CAN_SLEEP) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002111 DECLARE_WAITQUEUE(wait, current);
2112
2113 add_wait_queue(&net->xfrm.km_waitq, &wait);
2114 set_current_state(TASK_INTERRUPTIBLE);
2115 schedule();
2116 set_current_state(TASK_RUNNING);
2117 remove_wait_queue(&net->xfrm.km_waitq, &wait);
2118
2119 if (!signal_pending(current)) {
2120 dst_release(dst);
2121 goto restart;
2122 }
2123
2124 err = -ERESTART;
2125 } else
2126 err = -EAGAIN;
2127
2128 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
2129 goto error;
2130 }
2131
2132no_transform:
2133 if (num_pols == 0)
Herbert Xu8b7817f2007-12-12 10:44:43 -08002134 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
Timo Teräs80c802f2010-04-07 00:30:05 +00002136 if ((flags & XFRM_LOOKUP_ICMP) &&
2137 !(pols[0]->flags & XFRM_POLICY_ICMP)) {
2138 err = -ENOENT;
Herbert Xu8b7817f2007-12-12 10:44:43 -08002139 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002140 }
Herbert Xu8b7817f2007-12-12 10:44:43 -08002141
Timo Teräs80c802f2010-04-07 00:30:05 +00002142 for (i = 0; i < num_pols; i++)
2143 pols[i]->curlft.use_time = get_seconds();
Herbert Xu8b7817f2007-12-12 10:44:43 -08002144
Timo Teräs80c802f2010-04-07 00:30:05 +00002145 if (num_xfrms < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 /* Prohibit the flow */
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002147 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLBLOCK);
Patrick McHardye104411b2005-09-08 15:11:55 -07002148 err = -EPERM;
2149 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002150 } else if (num_xfrms > 0) {
2151 /* Flow transformed */
Timo Teräs80c802f2010-04-07 00:30:05 +00002152 dst_release(dst_orig);
2153 } else {
2154 /* Flow passes untransformed */
2155 dst_release(dst);
David S. Miller452edd52011-03-02 13:27:41 -08002156 dst = dst_orig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002158ok:
2159 xfrm_pols_put(pols, drop_pols);
Gao feng0c183372012-05-26 01:30:53 +00002160 if (dst && dst->xfrm &&
2161 dst->xfrm->props.mode == XFRM_MODE_TUNNEL)
2162 dst->flags |= DST_XFRM_TUNNEL;
David S. Miller452edd52011-03-02 13:27:41 -08002163 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
Timo Teräs80c802f2010-04-07 00:30:05 +00002165nopol:
David S. Miller452edd52011-03-02 13:27:41 -08002166 if (!(flags & XFRM_LOOKUP_ICMP)) {
2167 dst = dst_orig;
Timo Teräs80c802f2010-04-07 00:30:05 +00002168 goto ok;
David S. Miller452edd52011-03-02 13:27:41 -08002169 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002170 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171error:
Timo Teräs80c802f2010-04-07 00:30:05 +00002172 dst_release(dst);
Herbert Xu75b8c132007-12-11 04:38:08 -08002173dropdst:
2174 dst_release(dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002175 xfrm_pols_put(pols, drop_pols);
David S. Miller452edd52011-03-02 13:27:41 -08002176 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177}
2178EXPORT_SYMBOL(xfrm_lookup);
2179
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002180static inline int
David S. Miller8f029de2011-02-22 17:59:59 -08002181xfrm_secpath_reject(int idx, struct sk_buff *skb, const struct flowi *fl)
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002182{
2183 struct xfrm_state *x;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002184
2185 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
2186 return 0;
2187 x = skb->sp->xvec[idx];
2188 if (!x->type->reject)
2189 return 0;
Herbert Xu1ecafed2007-10-09 13:24:07 -07002190 return x->type->reject(x, skb, fl);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002191}
2192
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193/* When skb is transformed back to its "native" form, we have to
2194 * check policy restrictions. At the moment we make this in maximally
2195 * stupid way. Shame on me. :-) Of course, connected sockets must
2196 * have policy cached at them.
2197 */
2198
2199static inline int
David S. Miller7db454b2011-02-24 01:43:01 -05002200xfrm_state_ok(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 unsigned short family)
2202{
2203 if (xfrm_state_kern(x))
Kazunori MIYAZAWA928ba412007-02-13 12:57:16 -08002204 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 return x->id.proto == tmpl->id.proto &&
2206 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
2207 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
2208 x->props.mode == tmpl->mode &&
Herbert Xuc5d18e92008-04-22 00:46:42 -07002209 (tmpl->allalgs || (tmpl->aalgos & (1<<x->props.aalgo)) ||
Masahide NAKAMURAf3bd4842006-08-23 18:00:48 -07002210 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002211 !(x->props.mode != XFRM_MODE_TRANSPORT &&
2212 xfrm_state_addr_cmp(tmpl, x, family));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213}
2214
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002215/*
2216 * 0 or more than 0 is returned when validation is succeeded (either bypass
2217 * because of optional transport mode, or next index of the mathced secpath
2218 * state with the template.
2219 * -1 is returned when no matching template is found.
2220 * Otherwise "-2 - errored_index" is returned.
2221 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222static inline int
David S. Miller22cccb72011-02-24 01:43:33 -05002223xfrm_policy_ok(const struct xfrm_tmpl *tmpl, const struct sec_path *sp, int start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 unsigned short family)
2225{
2226 int idx = start;
2227
2228 if (tmpl->optional) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002229 if (tmpl->mode == XFRM_MODE_TRANSPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 return start;
2231 } else
2232 start = -1;
2233 for (; idx < sp->len; idx++) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002234 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 return ++idx;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002236 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
2237 if (start == -1)
2238 start = -2-idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 break;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 }
2242 return start;
2243}
2244
Herbert Xud5422ef2007-12-12 10:44:16 -08002245int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
2246 unsigned int family, int reverse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247{
2248 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002249 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250
2251 if (unlikely(afinfo == NULL))
2252 return -EAFNOSUPPORT;
2253
Herbert Xud5422ef2007-12-12 10:44:16 -08002254 afinfo->decode_session(skb, fl, reverse);
David S. Miller1d28f422011-03-12 00:29:39 -05002255 err = security_xfrm_decode_session(skb, &fl->flowi_secid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 xfrm_policy_put_afinfo(afinfo);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002257 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258}
Herbert Xud5422ef2007-12-12 10:44:16 -08002259EXPORT_SYMBOL(__xfrm_decode_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
David S. Miller9a7386e2011-02-24 01:44:12 -05002261static inline int secpath_has_nontransport(const struct sec_path *sp, int k, int *idxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262{
2263 for (; k < sp->len; k++) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002264 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002265 *idxp = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 return 1;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 }
2269
2270 return 0;
2271}
2272
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002273int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 unsigned short family)
2275{
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002276 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 struct xfrm_policy *pol;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002278 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2279 int npols = 0;
2280 int xfrm_nr;
2281 int pi;
Herbert Xud5422ef2007-12-12 10:44:16 -08002282 int reverse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 struct flowi fl;
Herbert Xud5422ef2007-12-12 10:44:16 -08002284 u8 fl_dir;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002285 int xerr_idx = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286
Herbert Xud5422ef2007-12-12 10:44:16 -08002287 reverse = dir & ~XFRM_POLICY_MASK;
2288 dir &= XFRM_POLICY_MASK;
2289 fl_dir = policy_to_flow_dir(dir);
2290
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002291 if (__xfrm_decode_session(skb, &fl, family, reverse) < 0) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002292 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002294 }
2295
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -08002296 nf_nat_decode_session(skb, &fl, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
2298 /* First, check used SA against their selectors. */
2299 if (skb->sp) {
2300 int i;
2301
2302 for (i=skb->sp->len-1; i>=0; i--) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002303 struct xfrm_state *x = skb->sp->xvec[i];
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002304 if (!xfrm_selector_match(&x->sel, &fl, family)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002305 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 }
2309 }
2310
2311 pol = NULL;
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002312 if (sk && sk->sk_policy[dir]) {
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002313 pol = xfrm_sk_policy_lookup(sk, dir, &fl);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002314 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002315 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002316 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002317 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319
Timo Teräsfe1a5f02010-04-07 00:30:04 +00002320 if (!pol) {
2321 struct flow_cache_object *flo;
2322
2323 flo = flow_cache_lookup(net, &fl, family, fl_dir,
2324 xfrm_policy_lookup, NULL);
2325 if (IS_ERR_OR_NULL(flo))
2326 pol = ERR_CAST(flo);
2327 else
2328 pol = container_of(flo, struct xfrm_policy, flo);
2329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002331 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002332 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002333 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002334 }
James Morris134b0fc2006-10-05 15:42:27 -05002335
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002336 if (!pol) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002337 if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002338 xfrm_secpath_reject(xerr_idx, skb, &fl);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002339 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOPOLS);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002340 return 0;
2341 }
2342 return 1;
2343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344
James Morris9d729f72007-03-04 16:12:44 -08002345 pol->curlft.use_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002347 pols[0] = pol;
2348 npols ++;
2349#ifdef CONFIG_XFRM_SUB_POLICY
2350 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002351 pols[1] = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002352 &fl, family,
2353 XFRM_POLICY_IN);
2354 if (pols[1]) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002355 if (IS_ERR(pols[1])) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002356 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002357 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002358 }
James Morris9d729f72007-03-04 16:12:44 -08002359 pols[1]->curlft.use_time = get_seconds();
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002360 npols ++;
2361 }
2362 }
2363#endif
2364
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 if (pol->action == XFRM_POLICY_ALLOW) {
2366 struct sec_path *sp;
2367 static struct sec_path dummy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002368 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002369 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002370 struct xfrm_tmpl **tpp = tp;
2371 int ti = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 int i, k;
2373
2374 if ((sp = skb->sp) == NULL)
2375 sp = &dummy;
2376
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002377 for (pi = 0; pi < npols; pi++) {
2378 if (pols[pi] != pol &&
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002379 pols[pi]->action != XFRM_POLICY_ALLOW) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002380 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002381 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002382 }
2383 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002384 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002385 goto reject_error;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002386 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002387 for (i = 0; i < pols[pi]->xfrm_nr; i++)
2388 tpp[ti++] = &pols[pi]->xfrm_vec[i];
2389 }
2390 xfrm_nr = ti;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002391 if (npols > 1) {
2392 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family);
2393 tpp = stp;
2394 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002395
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 /* For each tunnel xfrm, find the first matching tmpl.
2397 * For each tmpl before that, find corresponding xfrm.
2398 * Order is _important_. Later we will implement
2399 * some barriers, but at the moment barriers
2400 * are implied between each two transformations.
2401 */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002402 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
2403 k = xfrm_policy_ok(tpp[i], sp, k, family);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002404 if (k < 0) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002405 if (k < -1)
2406 /* "-2 - errored_index" returned */
2407 xerr_idx = -(2+k);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002408 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 goto reject;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 }
2412
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002413 if (secpath_has_nontransport(sp, k, &xerr_idx)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002414 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002418 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 return 1;
2420 }
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002421 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422
2423reject:
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002424 xfrm_secpath_reject(xerr_idx, skb, &fl);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002425reject_error:
2426 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 return 0;
2428}
2429EXPORT_SYMBOL(__xfrm_policy_check);
2430
2431int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
2432{
Alexey Dobriyan99a66652008-11-25 17:36:13 -08002433 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 struct flowi fl;
Eric Dumazetadf30902009-06-02 05:19:30 +00002435 struct dst_entry *dst;
Eric Dumazet73137142011-03-15 15:26:43 -07002436 int res = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002438 if (xfrm_decode_session(skb, &fl, family) < 0) {
jamal72032fd2010-02-18 03:35:07 +00002439 XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442
Eric Dumazetfafeeb62010-06-01 10:04:49 +00002443 skb_dst_force(skb);
Eric Dumazetadf30902009-06-02 05:19:30 +00002444
David S. Miller452edd52011-03-02 13:27:41 -08002445 dst = xfrm_lookup(net, skb_dst(skb), &fl, NULL, 0);
2446 if (IS_ERR(dst)) {
Eric Dumazet73137142011-03-15 15:26:43 -07002447 res = 0;
David S. Miller452edd52011-03-02 13:27:41 -08002448 dst = NULL;
2449 }
Eric Dumazetadf30902009-06-02 05:19:30 +00002450 skb_dst_set(skb, dst);
2451 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452}
2453EXPORT_SYMBOL(__xfrm_route_forward);
2454
David S. Millerd49c73c2006-08-13 18:55:53 -07002455/* Optimize later using cookies and generation ids. */
2456
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
2458{
David S. Millerd49c73c2006-08-13 18:55:53 -07002459 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
David S. Millerf5b0a872012-07-19 12:31:33 -07002460 * to DST_OBSOLETE_FORCE_CHK to force all XFRM destinations to
2461 * get validated by dst_ops->check on every use. We do this
2462 * because when a normal route referenced by an XFRM dst is
2463 * obsoleted we do not go looking around for all parent
2464 * referencing XFRM dsts so that we can invalidate them. It
2465 * is just too much work. Instead we make the checks here on
2466 * every use. For example:
David S. Millerd49c73c2006-08-13 18:55:53 -07002467 *
2468 * XFRM dst A --> IPv4 dst X
2469 *
2470 * X is the "xdst->route" of A (X is also the "dst->path" of A
2471 * in this example). If X is marked obsolete, "A" will not
2472 * notice. That's what we are validating here via the
2473 * stale_bundle() check.
2474 *
2475 * When a policy's bundle is pruned, we dst_free() the XFRM
David S. Millerf5b0a872012-07-19 12:31:33 -07002476 * dst which causes it's ->obsolete field to be set to
2477 * DST_OBSOLETE_DEAD. If an XFRM dst has been pruned like
2478 * this, we want to force a new route lookup.
David S. Miller399c1802005-12-19 14:23:23 -08002479 */
David S. Millerd49c73c2006-08-13 18:55:53 -07002480 if (dst->obsolete < 0 && !stale_bundle(dst))
2481 return dst;
2482
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 return NULL;
2484}
2485
2486static int stale_bundle(struct dst_entry *dst)
2487{
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002488 return !xfrm_bundle_ok((struct xfrm_dst *)dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489}
2490
Herbert Xuaabc9762005-05-03 16:27:10 -07002491void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002494 dst->dev = dev_net(dev)->loopback_dev;
Daniel Lezcanode3cb742007-09-25 19:16:28 -07002495 dev_hold(dst->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 dev_put(dev);
2497 }
2498}
Herbert Xuaabc9762005-05-03 16:27:10 -07002499EXPORT_SYMBOL(xfrm_dst_ifdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500
2501static void xfrm_link_failure(struct sk_buff *skb)
2502{
2503 /* Impossible. Such dst must be popped before reaches point of failure. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504}
2505
2506static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
2507{
2508 if (dst) {
2509 if (dst->obsolete) {
2510 dst_release(dst);
2511 dst = NULL;
2512 }
2513 }
2514 return dst;
2515}
2516
Alexey Dobriyanddcfd792008-11-25 17:37:23 -08002517static void __xfrm_garbage_collect(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518{
Timo Teräs80c802f2010-04-07 00:30:05 +00002519 struct dst_entry *head, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520
Timo Teräs80c802f2010-04-07 00:30:05 +00002521 spin_lock_bh(&xfrm_policy_sk_bundle_lock);
2522 head = xfrm_policy_sk_bundles;
2523 xfrm_policy_sk_bundles = NULL;
2524 spin_unlock_bh(&xfrm_policy_sk_bundle_lock);
2525
2526 while (head) {
2527 next = head->next;
2528 dst_free(head);
2529 head = next;
2530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531}
2532
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002533static void xfrm_garbage_collect(struct net *net)
2534{
2535 flow_cache_flush();
2536 __xfrm_garbage_collect(net);
2537}
2538
2539static void xfrm_garbage_collect_deferred(struct net *net)
2540{
2541 flow_cache_flush_deferred();
2542 __xfrm_garbage_collect(net);
2543}
2544
Herbert Xu25ee3282007-12-11 09:32:34 -08002545static void xfrm_init_pmtu(struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546{
2547 do {
2548 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2549 u32 pmtu, route_mtu_cached;
2550
2551 pmtu = dst_mtu(dst->child);
2552 xdst->child_mtu_cached = pmtu;
2553
2554 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
2555
2556 route_mtu_cached = dst_mtu(xdst->route);
2557 xdst->route_mtu_cached = route_mtu_cached;
2558
2559 if (pmtu > route_mtu_cached)
2560 pmtu = route_mtu_cached;
2561
David S. Millerdefb3512010-12-08 21:16:57 -08002562 dst_metric_set(dst, RTAX_MTU, pmtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 } while ((dst = dst->next));
2564}
2565
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566/* Check that the bundle accepts the flow and its components are
2567 * still valid.
2568 */
2569
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002570static int xfrm_bundle_ok(struct xfrm_dst *first)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571{
2572 struct dst_entry *dst = &first->u.dst;
2573 struct xfrm_dst *last;
2574 u32 mtu;
2575
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07002576 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 (dst->dev && !netif_running(dst->dev)))
2578 return 0;
2579
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002580 if (dst->flags & DST_XFRM_QUEUE)
2581 return 1;
2582
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 last = NULL;
2584
2585 do {
2586 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2587
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 if (dst->xfrm->km.state != XFRM_STATE_VALID)
2589 return 0;
Timo Teräs80c802f2010-04-07 00:30:05 +00002590 if (xdst->xfrm_genid != dst->xfrm->genid)
2591 return 0;
Timo Teräsb1312c82010-06-24 14:35:00 -07002592 if (xdst->num_pols > 0 &&
2593 xdst->policy_genid != atomic_read(&xdst->pols[0]->genid))
David S. Miller9d4a7062006-08-24 03:18:09 -07002594 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595
2596 mtu = dst_mtu(dst->child);
2597 if (xdst->child_mtu_cached != mtu) {
2598 last = xdst;
2599 xdst->child_mtu_cached = mtu;
2600 }
2601
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07002602 if (!dst_check(xdst->route, xdst->route_cookie))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 return 0;
2604 mtu = dst_mtu(xdst->route);
2605 if (xdst->route_mtu_cached != mtu) {
2606 last = xdst;
2607 xdst->route_mtu_cached = mtu;
2608 }
2609
2610 dst = dst->child;
2611 } while (dst->xfrm);
2612
2613 if (likely(!last))
2614 return 1;
2615
2616 mtu = last->child_mtu_cached;
2617 for (;;) {
2618 dst = &last->u.dst;
2619
2620 mtu = xfrm_state_mtu(dst->xfrm, mtu);
2621 if (mtu > last->route_mtu_cached)
2622 mtu = last->route_mtu_cached;
David S. Millerdefb3512010-12-08 21:16:57 -08002623 dst_metric_set(dst, RTAX_MTU, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624
2625 if (last == first)
2626 break;
2627
Patrick McHardybd0bf072007-07-18 01:55:52 -07002628 last = (struct xfrm_dst *)last->u.dst.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 last->child_mtu_cached = mtu;
2630 }
2631
2632 return 1;
2633}
2634
David S. Miller0dbaee32010-12-13 12:52:14 -08002635static unsigned int xfrm_default_advmss(const struct dst_entry *dst)
2636{
2637 return dst_metric_advmss(dst->path);
2638}
2639
Steffen Klassertebb762f2011-11-23 02:12:51 +00002640static unsigned int xfrm_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08002641{
Steffen Klassert618f9bc2011-11-23 02:13:31 +00002642 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
2643
2644 return mtu ? : dst_mtu(dst->path);
David S. Millerd33e4552010-12-14 13:01:14 -08002645}
2646
David S. Millerf894cbf2012-07-02 21:52:24 -07002647static struct neighbour *xfrm_neigh_lookup(const struct dst_entry *dst,
2648 struct sk_buff *skb,
2649 const void *daddr)
David S. Millerd3aaeb32011-07-18 00:40:17 -07002650{
David S. Millerf894cbf2012-07-02 21:52:24 -07002651 return dst->path->ops->neigh_lookup(dst, skb, daddr);
David S. Millerd3aaeb32011-07-18 00:40:17 -07002652}
2653
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
2655{
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002656 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 int err = 0;
2658 if (unlikely(afinfo == NULL))
2659 return -EINVAL;
2660 if (unlikely(afinfo->family >= NPROTO))
2661 return -EAFNOSUPPORT;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002662 spin_lock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
2664 err = -ENOBUFS;
2665 else {
2666 struct dst_ops *dst_ops = afinfo->dst_ops;
2667 if (likely(dst_ops->kmem_cachep == NULL))
2668 dst_ops->kmem_cachep = xfrm_dst_cache;
2669 if (likely(dst_ops->check == NULL))
2670 dst_ops->check = xfrm_dst_check;
David S. Miller0dbaee32010-12-13 12:52:14 -08002671 if (likely(dst_ops->default_advmss == NULL))
2672 dst_ops->default_advmss = xfrm_default_advmss;
Steffen Klassertebb762f2011-11-23 02:12:51 +00002673 if (likely(dst_ops->mtu == NULL))
2674 dst_ops->mtu = xfrm_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 if (likely(dst_ops->negative_advice == NULL))
2676 dst_ops->negative_advice = xfrm_negative_advice;
2677 if (likely(dst_ops->link_failure == NULL))
2678 dst_ops->link_failure = xfrm_link_failure;
David S. Millerd3aaeb32011-07-18 00:40:17 -07002679 if (likely(dst_ops->neigh_lookup == NULL))
2680 dst_ops->neigh_lookup = xfrm_neigh_lookup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 if (likely(afinfo->garbage_collect == NULL))
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002682 afinfo->garbage_collect = xfrm_garbage_collect_deferred;
Priyanka Jain418a99a2012-08-12 21:22:29 +00002683 rcu_assign_pointer(xfrm_policy_afinfo[afinfo->family], afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02002685 spin_unlock(&xfrm_policy_afinfo_lock);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002686
2687 rtnl_lock();
2688 for_each_net(net) {
2689 struct dst_ops *xfrm_dst_ops;
2690
2691 switch (afinfo->family) {
2692 case AF_INET:
2693 xfrm_dst_ops = &net->xfrm.xfrm4_dst_ops;
2694 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002695#if IS_ENABLED(CONFIG_IPV6)
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002696 case AF_INET6:
2697 xfrm_dst_ops = &net->xfrm.xfrm6_dst_ops;
2698 break;
2699#endif
2700 default:
2701 BUG();
2702 }
2703 *xfrm_dst_ops = *afinfo->dst_ops;
2704 }
2705 rtnl_unlock();
2706
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 return err;
2708}
2709EXPORT_SYMBOL(xfrm_policy_register_afinfo);
2710
2711int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
2712{
2713 int err = 0;
2714 if (unlikely(afinfo == NULL))
2715 return -EINVAL;
2716 if (unlikely(afinfo->family >= NPROTO))
2717 return -EAFNOSUPPORT;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002718 spin_lock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
2720 if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
2721 err = -EINVAL;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002722 else
2723 RCU_INIT_POINTER(xfrm_policy_afinfo[afinfo->family],
2724 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02002726 spin_unlock(&xfrm_policy_afinfo_lock);
2727 if (!err) {
2728 struct dst_ops *dst_ops = afinfo->dst_ops;
2729
2730 synchronize_rcu();
2731
2732 dst_ops->kmem_cachep = NULL;
2733 dst_ops->check = NULL;
2734 dst_ops->negative_advice = NULL;
2735 dst_ops->link_failure = NULL;
2736 afinfo->garbage_collect = NULL;
2737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 return err;
2739}
2740EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
2741
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002742static void __net_init xfrm_dst_ops_init(struct net *net)
2743{
2744 struct xfrm_policy_afinfo *afinfo;
2745
Eric Dumazetef8531b2012-08-19 12:31:48 +02002746 rcu_read_lock();
2747 afinfo = rcu_dereference(xfrm_policy_afinfo[AF_INET]);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002748 if (afinfo)
2749 net->xfrm.xfrm4_dst_ops = *afinfo->dst_ops;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002750#if IS_ENABLED(CONFIG_IPV6)
Eric Dumazetef8531b2012-08-19 12:31:48 +02002751 afinfo = rcu_dereference(xfrm_policy_afinfo[AF_INET6]);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002752 if (afinfo)
2753 net->xfrm.xfrm6_dst_ops = *afinfo->dst_ops;
2754#endif
Priyanka Jain418a99a2012-08-12 21:22:29 +00002755 rcu_read_unlock();
Herbert Xu546be242006-05-27 23:03:58 -07002756}
2757
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
2759{
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02002760 struct net_device *dev = ptr;
2761
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 switch (event) {
2763 case NETDEV_DOWN:
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002764 xfrm_garbage_collect(dev_net(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 }
2766 return NOTIFY_DONE;
2767}
2768
2769static struct notifier_block xfrm_dev_notifier = {
Alexey Dobriyand5917a32008-10-31 00:41:59 -07002770 .notifier_call = xfrm_dev_event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771};
2772
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002773#ifdef CONFIG_XFRM_STATISTICS
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002774static int __net_init xfrm_statistics_init(struct net *net)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002775{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002776 int rv;
2777
Tejun Heo7d720c32010-02-16 15:20:26 +00002778 if (snmp_mib_init((void __percpu **)net->mib.xfrm_statistics,
Eric Dumazet1823e4c82010-06-22 20:58:41 +00002779 sizeof(struct linux_xfrm_mib),
2780 __alignof__(struct linux_xfrm_mib)) < 0)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002781 return -ENOMEM;
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002782 rv = xfrm_proc_init(net);
2783 if (rv < 0)
Tejun Heo7d720c32010-02-16 15:20:26 +00002784 snmp_mib_free((void __percpu **)net->mib.xfrm_statistics);
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002785 return rv;
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002786}
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002787
2788static void xfrm_statistics_fini(struct net *net)
2789{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002790 xfrm_proc_fini(net);
Tejun Heo7d720c32010-02-16 15:20:26 +00002791 snmp_mib_free((void __percpu **)net->mib.xfrm_statistics);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002792}
2793#else
2794static int __net_init xfrm_statistics_init(struct net *net)
2795{
2796 return 0;
2797}
2798
2799static void xfrm_statistics_fini(struct net *net)
2800{
2801}
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002802#endif
2803
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002804static int __net_init xfrm_policy_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805{
David S. Miller2518c7c2006-08-24 04:45:07 -07002806 unsigned int hmask, sz;
2807 int dir;
2808
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002809 if (net_eq(net, &init_net))
2810 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 sizeof(struct xfrm_dst),
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002812 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002813 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814
David S. Miller2518c7c2006-08-24 04:45:07 -07002815 hmask = 8 - 1;
2816 sz = (hmask+1) * sizeof(struct hlist_head);
2817
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002818 net->xfrm.policy_byidx = xfrm_hash_alloc(sz);
2819 if (!net->xfrm.policy_byidx)
2820 goto out_byidx;
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08002821 net->xfrm.policy_idx_hmask = hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -07002822
2823 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
2824 struct xfrm_policy_hash *htab;
2825
Alexey Dobriyandc2caba2008-11-25 17:24:15 -08002826 net->xfrm.policy_count[dir] = 0;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002827 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
David S. Miller2518c7c2006-08-24 04:45:07 -07002828
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002829 htab = &net->xfrm.policy_bydst[dir];
David S. Miller44e36b42006-08-24 04:50:50 -07002830 htab->table = xfrm_hash_alloc(sz);
David S. Miller2518c7c2006-08-24 04:45:07 -07002831 if (!htab->table)
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002832 goto out_bydst;
2833 htab->hmask = hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -07002834 }
2835
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08002836 INIT_LIST_HEAD(&net->xfrm.policy_all);
Alexey Dobriyan66caf622008-11-25 17:28:57 -08002837 INIT_WORK(&net->xfrm.policy_hash_work, xfrm_hash_resize);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002838 if (net_eq(net, &init_net))
2839 register_netdevice_notifier(&xfrm_dev_notifier);
2840 return 0;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002841
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002842out_bydst:
2843 for (dir--; dir >= 0; dir--) {
2844 struct xfrm_policy_hash *htab;
2845
2846 htab = &net->xfrm.policy_bydst[dir];
2847 xfrm_hash_free(htab->table, sz);
2848 }
2849 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002850out_byidx:
2851 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852}
2853
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002854static void xfrm_policy_fini(struct net *net)
2855{
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002856 struct xfrm_audit audit_info;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002857 unsigned int sz;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002858 int dir;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002859
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002860 flush_work(&net->xfrm.policy_hash_work);
2861#ifdef CONFIG_XFRM_SUB_POLICY
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07002862 audit_info.loginuid = INVALID_UID;
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002863 audit_info.sessionid = -1;
2864 audit_info.secid = 0;
2865 xfrm_policy_flush(net, XFRM_POLICY_TYPE_SUB, &audit_info);
2866#endif
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07002867 audit_info.loginuid = INVALID_UID;
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002868 audit_info.sessionid = -1;
2869 audit_info.secid = 0;
2870 xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, &audit_info);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002871
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08002872 WARN_ON(!list_empty(&net->xfrm.policy_all));
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002873
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002874 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002875 struct xfrm_policy_hash *htab;
2876
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002877 WARN_ON(!hlist_empty(&net->xfrm.policy_inexact[dir]));
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002878
2879 htab = &net->xfrm.policy_bydst[dir];
2880 sz = (htab->hmask + 1);
2881 WARN_ON(!hlist_empty(htab->table));
2882 xfrm_hash_free(htab->table, sz);
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002883 }
2884
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08002885 sz = (net->xfrm.policy_idx_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002886 WARN_ON(!hlist_empty(net->xfrm.policy_byidx));
2887 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002888}
2889
2890static int __net_init xfrm_net_init(struct net *net)
2891{
2892 int rv;
2893
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002894 rv = xfrm_statistics_init(net);
2895 if (rv < 0)
2896 goto out_statistics;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002897 rv = xfrm_state_init(net);
2898 if (rv < 0)
2899 goto out_state;
2900 rv = xfrm_policy_init(net);
2901 if (rv < 0)
2902 goto out_policy;
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002903 xfrm_dst_ops_init(net);
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08002904 rv = xfrm_sysctl_init(net);
2905 if (rv < 0)
2906 goto out_sysctl;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002907 return 0;
2908
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08002909out_sysctl:
2910 xfrm_policy_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002911out_policy:
2912 xfrm_state_fini(net);
2913out_state:
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002914 xfrm_statistics_fini(net);
2915out_statistics:
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002916 return rv;
2917}
2918
2919static void __net_exit xfrm_net_exit(struct net *net)
2920{
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08002921 xfrm_sysctl_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002922 xfrm_policy_fini(net);
2923 xfrm_state_fini(net);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002924 xfrm_statistics_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002925}
2926
2927static struct pernet_operations __net_initdata xfrm_net_ops = {
2928 .init = xfrm_net_init,
2929 .exit = xfrm_net_exit,
2930};
2931
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932void __init xfrm_init(void)
2933{
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002934 register_pernet_subsys(&xfrm_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 xfrm_input_init();
2936}
2937
Joy Lattenab5f5e82007-09-17 11:51:22 -07002938#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinen1486cbd72008-01-12 03:20:03 -08002939static void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
2940 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002941{
Paul Moore875179f2007-12-01 23:27:18 +11002942 struct xfrm_sec_ctx *ctx = xp->security;
2943 struct xfrm_selector *sel = &xp->selector;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002944
Paul Moore875179f2007-12-01 23:27:18 +11002945 if (ctx)
2946 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
2947 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
2948
2949 switch(sel->family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07002950 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07002951 audit_log_format(audit_buf, " src=%pI4", &sel->saddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11002952 if (sel->prefixlen_s != 32)
2953 audit_log_format(audit_buf, " src_prefixlen=%d",
2954 sel->prefixlen_s);
Harvey Harrison21454aa2008-10-31 00:54:56 -07002955 audit_log_format(audit_buf, " dst=%pI4", &sel->daddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11002956 if (sel->prefixlen_d != 32)
2957 audit_log_format(audit_buf, " dst_prefixlen=%d",
2958 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002959 break;
2960 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002961 audit_log_format(audit_buf, " src=%pI6", sel->saddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11002962 if (sel->prefixlen_s != 128)
2963 audit_log_format(audit_buf, " src_prefixlen=%d",
2964 sel->prefixlen_s);
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002965 audit_log_format(audit_buf, " dst=%pI6", sel->daddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11002966 if (sel->prefixlen_d != 128)
2967 audit_log_format(audit_buf, " dst_prefixlen=%d",
2968 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002969 break;
2970 }
2971}
2972
Paul Moore68277ac2007-12-20 20:49:33 -08002973void xfrm_audit_policy_add(struct xfrm_policy *xp, int result,
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07002974 kuid_t auid, u32 sessionid, u32 secid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002975{
2976 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002977
Paul Mooreafeb14b2007-12-21 14:58:11 -08002978 audit_buf = xfrm_audit_start("SPD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002979 if (audit_buf == NULL)
2980 return;
Eric Paris25323862008-04-18 10:09:25 -04002981 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002982 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002983 xfrm_audit_common_policyinfo(xp, audit_buf);
2984 audit_log_end(audit_buf);
2985}
2986EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
2987
Paul Moore68277ac2007-12-20 20:49:33 -08002988void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07002989 kuid_t auid, u32 sessionid, u32 secid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002990{
2991 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002992
Paul Mooreafeb14b2007-12-21 14:58:11 -08002993 audit_buf = xfrm_audit_start("SPD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002994 if (audit_buf == NULL)
2995 return;
Eric Paris25323862008-04-18 10:09:25 -04002996 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002997 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002998 xfrm_audit_common_policyinfo(xp, audit_buf);
2999 audit_log_end(audit_buf);
3000}
3001EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
3002#endif
3003
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003004#ifdef CONFIG_XFRM_MIGRATE
David S. Millerbc9b35a2012-05-15 15:04:57 -04003005static bool xfrm_migrate_selector_match(const struct xfrm_selector *sel_cmp,
3006 const struct xfrm_selector *sel_tgt)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003007{
3008 if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
3009 if (sel_tgt->family == sel_cmp->family &&
3010 xfrm_addr_cmp(&sel_tgt->daddr, &sel_cmp->daddr,
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09003011 sel_cmp->family) == 0 &&
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003012 xfrm_addr_cmp(&sel_tgt->saddr, &sel_cmp->saddr,
3013 sel_cmp->family) == 0 &&
3014 sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
3015 sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04003016 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003017 }
3018 } else {
3019 if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04003020 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003021 }
3022 }
David S. Millerbc9b35a2012-05-15 15:04:57 -04003023 return false;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003024}
3025
David S. Millerb4b7c0b2011-02-24 00:35:06 -05003026static struct xfrm_policy * xfrm_migrate_policy_find(const struct xfrm_selector *sel,
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003027 u8 dir, u8 type)
3028{
3029 struct xfrm_policy *pol, *ret = NULL;
3030 struct hlist_node *entry;
3031 struct hlist_head *chain;
3032 u32 priority = ~0U;
3033
3034 read_lock_bh(&xfrm_policy_lock);
Alexey Dobriyan11219942008-11-25 17:33:06 -08003035 chain = policy_hash_direct(&init_net, &sel->daddr, &sel->saddr, sel->family, dir);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003036 hlist_for_each_entry(pol, entry, chain, bydst) {
3037 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
3038 pol->type == type) {
3039 ret = pol;
3040 priority = ret->priority;
3041 break;
3042 }
3043 }
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003044 chain = &init_net.xfrm.policy_inexact[dir];
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003045 hlist_for_each_entry(pol, entry, chain, bydst) {
3046 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
3047 pol->type == type &&
3048 pol->priority < priority) {
3049 ret = pol;
3050 break;
3051 }
3052 }
3053
3054 if (ret)
3055 xfrm_pol_hold(ret);
3056
3057 read_unlock_bh(&xfrm_policy_lock);
3058
3059 return ret;
3060}
3061
David S. Millerdd701752011-02-24 00:21:08 -05003062static int migrate_tmpl_match(const struct xfrm_migrate *m, const struct xfrm_tmpl *t)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003063{
3064 int match = 0;
3065
3066 if (t->mode == m->mode && t->id.proto == m->proto &&
3067 (m->reqid == 0 || t->reqid == m->reqid)) {
3068 switch (t->mode) {
3069 case XFRM_MODE_TUNNEL:
3070 case XFRM_MODE_BEET:
3071 if (xfrm_addr_cmp(&t->id.daddr, &m->old_daddr,
3072 m->old_family) == 0 &&
3073 xfrm_addr_cmp(&t->saddr, &m->old_saddr,
3074 m->old_family) == 0) {
3075 match = 1;
3076 }
3077 break;
3078 case XFRM_MODE_TRANSPORT:
3079 /* in case of transport mode, template does not store
3080 any IP addresses, hence we just compare mode and
3081 protocol */
3082 match = 1;
3083 break;
3084 default:
3085 break;
3086 }
3087 }
3088 return match;
3089}
3090
3091/* update endpoint address(es) of template(s) */
3092static int xfrm_policy_migrate(struct xfrm_policy *pol,
3093 struct xfrm_migrate *m, int num_migrate)
3094{
3095 struct xfrm_migrate *mp;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003096 int i, j, n = 0;
3097
3098 write_lock_bh(&pol->lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07003099 if (unlikely(pol->walk.dead)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003100 /* target policy has been deleted */
3101 write_unlock_bh(&pol->lock);
3102 return -ENOENT;
3103 }
3104
3105 for (i = 0; i < pol->xfrm_nr; i++) {
3106 for (j = 0, mp = m; j < num_migrate; j++, mp++) {
3107 if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
3108 continue;
3109 n++;
Herbert Xu1bfcb102007-10-17 21:31:50 -07003110 if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
3111 pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003112 continue;
3113 /* update endpoints */
3114 memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
3115 sizeof(pol->xfrm_vec[i].id.daddr));
3116 memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
3117 sizeof(pol->xfrm_vec[i].saddr));
3118 pol->xfrm_vec[i].encap_family = mp->new_family;
3119 /* flush bundles */
Timo Teräs80c802f2010-04-07 00:30:05 +00003120 atomic_inc(&pol->genid);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003121 }
3122 }
3123
3124 write_unlock_bh(&pol->lock);
3125
3126 if (!n)
3127 return -ENODATA;
3128
3129 return 0;
3130}
3131
David S. Millerdd701752011-02-24 00:21:08 -05003132static int xfrm_migrate_check(const struct xfrm_migrate *m, int num_migrate)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003133{
3134 int i, j;
3135
3136 if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
3137 return -EINVAL;
3138
3139 for (i = 0; i < num_migrate; i++) {
3140 if ((xfrm_addr_cmp(&m[i].old_daddr, &m[i].new_daddr,
3141 m[i].old_family) == 0) &&
3142 (xfrm_addr_cmp(&m[i].old_saddr, &m[i].new_saddr,
3143 m[i].old_family) == 0))
3144 return -EINVAL;
3145 if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
3146 xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
3147 return -EINVAL;
3148
3149 /* check if there is any duplicated entry */
3150 for (j = i + 1; j < num_migrate; j++) {
3151 if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
3152 sizeof(m[i].old_daddr)) &&
3153 !memcmp(&m[i].old_saddr, &m[j].old_saddr,
3154 sizeof(m[i].old_saddr)) &&
3155 m[i].proto == m[j].proto &&
3156 m[i].mode == m[j].mode &&
3157 m[i].reqid == m[j].reqid &&
3158 m[i].old_family == m[j].old_family)
3159 return -EINVAL;
3160 }
3161 }
3162
3163 return 0;
3164}
3165
David S. Millerb4b7c0b2011-02-24 00:35:06 -05003166int xfrm_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003167 struct xfrm_migrate *m, int num_migrate,
3168 struct xfrm_kmaddress *k)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003169{
3170 int i, err, nx_cur = 0, nx_new = 0;
3171 struct xfrm_policy *pol = NULL;
3172 struct xfrm_state *x, *xc;
3173 struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
3174 struct xfrm_state *x_new[XFRM_MAX_DEPTH];
3175 struct xfrm_migrate *mp;
3176
3177 if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
3178 goto out;
3179
3180 /* Stage 1 - find policy */
3181 if ((pol = xfrm_migrate_policy_find(sel, dir, type)) == NULL) {
3182 err = -ENOENT;
3183 goto out;
3184 }
3185
3186 /* Stage 2 - find and update state(s) */
3187 for (i = 0, mp = m; i < num_migrate; i++, mp++) {
3188 if ((x = xfrm_migrate_state_find(mp))) {
3189 x_cur[nx_cur] = x;
3190 nx_cur++;
3191 if ((xc = xfrm_state_migrate(x, mp))) {
3192 x_new[nx_new] = xc;
3193 nx_new++;
3194 } else {
3195 err = -ENODATA;
3196 goto restore_state;
3197 }
3198 }
3199 }
3200
3201 /* Stage 3 - update policy */
3202 if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
3203 goto restore_state;
3204
3205 /* Stage 4 - delete old state(s) */
3206 if (nx_cur) {
3207 xfrm_states_put(x_cur, nx_cur);
3208 xfrm_states_delete(x_cur, nx_cur);
3209 }
3210
3211 /* Stage 5 - announce */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003212 km_migrate(sel, dir, type, m, num_migrate, k);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003213
3214 xfrm_pol_put(pol);
3215
3216 return 0;
3217out:
3218 return err;
3219
3220restore_state:
3221 if (pol)
3222 xfrm_pol_put(pol);
3223 if (nx_cur)
3224 xfrm_states_put(x_cur, nx_cur);
3225 if (nx_new)
3226 xfrm_states_delete(x_new, nx_new);
3227
3228 return err;
3229}
David S. Millere610e672007-02-08 13:29:15 -08003230EXPORT_SYMBOL(xfrm_migrate);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003231#endif