blob: 35b85a9a358c3ab7881e9ff408f3490a314b710f [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
Steffen Klassertb8c203b2014-09-16 10:08:49 +020042struct xfrm_flo {
43 struct dst_entry *dst_orig;
44 u8 flags;
45};
46
Priyanka Jain418a99a2012-08-12 21:22:29 +000047static DEFINE_SPINLOCK(xfrm_policy_afinfo_lock);
48static struct xfrm_policy_afinfo __rcu *xfrm_policy_afinfo[NPROTO]
49 __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Christoph Lametere18b8902006-12-06 20:33:20 -080051static struct kmem_cache *xfrm_dst_cache __read_mostly;
Florian Westphal30846092016-08-11 15:17:54 +020052static __read_mostly seqcount_t xfrm_policy_hash_generation;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Herbert Xu25ee3282007-12-11 09:32:34 -080054static void xfrm_init_pmtu(struct dst_entry *dst);
Timo Teräs80c802f2010-04-07 00:30:05 +000055static int stale_bundle(struct dst_entry *dst);
Steffen Klassert12fdb4d2011-06-29 23:18:20 +000056static int xfrm_bundle_ok(struct xfrm_dst *xdst);
Steffen Klasserta0073fe2013-02-05 12:52:55 +010057static void xfrm_policy_queue_process(unsigned long arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Herbert Xu12bfa8b2014-11-13 17:09:50 +080059static void __xfrm_policy_link(struct xfrm_policy *pol, int dir);
Wei Yongjun29fa0b302008-12-03 00:33:09 -080060static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
61 int dir);
62
Florian Westphale37cc8a2016-08-11 15:17:55 +020063static inline bool xfrm_pol_hold_rcu(struct xfrm_policy *policy)
64{
65 return atomic_inc_not_zero(&policy->refcnt);
66}
67
David S. Millerbc9b35a2012-05-15 15:04:57 -040068static inline bool
David S. Miller200ce962011-02-24 00:12:25 -050069__xfrm4_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
Andrew Morton77681022006-11-08 22:46:26 -080070{
David S. Miller7e1dc7b2011-03-12 02:42:11 -050071 const struct flowi4 *fl4 = &fl->u.ip4;
72
Alexey Dobriyan26bff942011-11-22 06:46:02 +000073 return addr4_match(fl4->daddr, sel->daddr.a4, sel->prefixlen_d) &&
74 addr4_match(fl4->saddr, sel->saddr.a4, sel->prefixlen_s) &&
David S. Miller7e1dc7b2011-03-12 02:42:11 -050075 !((xfrm_flowi_dport(fl, &fl4->uli) ^ sel->dport) & sel->dport_mask) &&
76 !((xfrm_flowi_sport(fl, &fl4->uli) ^ sel->sport) & sel->sport_mask) &&
77 (fl4->flowi4_proto == sel->proto || !sel->proto) &&
78 (fl4->flowi4_oif == sel->ifindex || !sel->ifindex);
Andrew Morton77681022006-11-08 22:46:26 -080079}
80
David S. Millerbc9b35a2012-05-15 15:04:57 -040081static inline bool
David S. Miller200ce962011-02-24 00:12:25 -050082__xfrm6_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
Andrew Morton77681022006-11-08 22:46:26 -080083{
David S. Miller7e1dc7b2011-03-12 02:42:11 -050084 const struct flowi6 *fl6 = &fl->u.ip6;
85
86 return addr_match(&fl6->daddr, &sel->daddr, sel->prefixlen_d) &&
87 addr_match(&fl6->saddr, &sel->saddr, sel->prefixlen_s) &&
88 !((xfrm_flowi_dport(fl, &fl6->uli) ^ sel->dport) & sel->dport_mask) &&
89 !((xfrm_flowi_sport(fl, &fl6->uli) ^ sel->sport) & sel->sport_mask) &&
90 (fl6->flowi6_proto == sel->proto || !sel->proto) &&
91 (fl6->flowi6_oif == sel->ifindex || !sel->ifindex);
Andrew Morton77681022006-11-08 22:46:26 -080092}
93
David S. Millerbc9b35a2012-05-15 15:04:57 -040094bool xfrm_selector_match(const struct xfrm_selector *sel, const struct flowi *fl,
95 unsigned short family)
Andrew Morton77681022006-11-08 22:46:26 -080096{
97 switch (family) {
98 case AF_INET:
99 return __xfrm4_selector_match(sel, fl);
100 case AF_INET6:
101 return __xfrm6_selector_match(sel, fl);
102 }
David S. Millerbc9b35a2012-05-15 15:04:57 -0400103 return false;
Andrew Morton77681022006-11-08 22:46:26 -0800104}
105
Eric Dumazetef8531b2012-08-19 12:31:48 +0200106static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
107{
108 struct xfrm_policy_afinfo *afinfo;
109
110 if (unlikely(family >= NPROTO))
111 return NULL;
112 rcu_read_lock();
113 afinfo = rcu_dereference(xfrm_policy_afinfo[family]);
114 if (unlikely(!afinfo))
115 rcu_read_unlock();
116 return afinfo;
117}
118
119static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
120{
121 rcu_read_unlock();
122}
123
David Ahern42a7b322015-08-10 16:58:11 -0600124static inline struct dst_entry *__xfrm_dst_lookup(struct net *net,
125 int tos, int oif,
David S. Miller6418c4e2011-02-24 00:16:53 -0500126 const xfrm_address_t *saddr,
127 const xfrm_address_t *daddr,
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900128 int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800130 struct xfrm_policy_afinfo *afinfo;
131 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Herbert Xu25ee3282007-12-11 09:32:34 -0800133 afinfo = xfrm_policy_get_afinfo(family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 if (unlikely(afinfo == NULL))
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800135 return ERR_PTR(-EAFNOSUPPORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
David Ahern42a7b322015-08-10 16:58:11 -0600137 dst = afinfo->dst_lookup(net, tos, oif, saddr, daddr);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 xfrm_policy_put_afinfo(afinfo);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900140
141 return dst;
142}
143
David Ahern42a7b322015-08-10 16:58:11 -0600144static inline struct dst_entry *xfrm_dst_lookup(struct xfrm_state *x,
145 int tos, int oif,
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900146 xfrm_address_t *prev_saddr,
147 xfrm_address_t *prev_daddr,
148 int family)
149{
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800150 struct net *net = xs_net(x);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900151 xfrm_address_t *saddr = &x->props.saddr;
152 xfrm_address_t *daddr = &x->id.daddr;
153 struct dst_entry *dst;
154
155 if (x->type->flags & XFRM_TYPE_LOCAL_COADDR) {
156 saddr = x->coaddr;
157 daddr = prev_daddr;
158 }
159 if (x->type->flags & XFRM_TYPE_REMOTE_COADDR) {
160 saddr = prev_saddr;
161 daddr = x->coaddr;
162 }
163
David Ahern42a7b322015-08-10 16:58:11 -0600164 dst = __xfrm_dst_lookup(net, tos, oif, saddr, daddr, family);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900165
166 if (!IS_ERR(dst)) {
167 if (prev_saddr != saddr)
168 memcpy(prev_saddr, saddr, sizeof(*prev_saddr));
169 if (prev_daddr != daddr)
170 memcpy(prev_daddr, daddr, sizeof(*prev_daddr));
171 }
172
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800173 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176static inline unsigned long make_jiffies(long secs)
177{
178 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
179 return MAX_SCHEDULE_TIMEOUT-1;
180 else
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900181 return secs*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182}
183
184static void xfrm_policy_timer(unsigned long data)
185{
Weilong Chen3e94c2d2013-12-24 09:43:47 +0800186 struct xfrm_policy *xp = (struct xfrm_policy *)data;
James Morris9d729f72007-03-04 16:12:44 -0800187 unsigned long now = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 long next = LONG_MAX;
189 int warn = 0;
190 int dir;
191
192 read_lock(&xp->lock);
193
Timo Teräsea2dea9d2010-03-31 00:17:05 +0000194 if (unlikely(xp->walk.dead))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 goto out;
196
Herbert Xu77d8d7a2005-10-05 12:15:12 -0700197 dir = xfrm_policy_id2dir(xp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 if (xp->lft.hard_add_expires_seconds) {
200 long tmo = xp->lft.hard_add_expires_seconds +
201 xp->curlft.add_time - now;
202 if (tmo <= 0)
203 goto expired;
204 if (tmo < next)
205 next = tmo;
206 }
207 if (xp->lft.hard_use_expires_seconds) {
208 long tmo = xp->lft.hard_use_expires_seconds +
209 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
210 if (tmo <= 0)
211 goto expired;
212 if (tmo < next)
213 next = tmo;
214 }
215 if (xp->lft.soft_add_expires_seconds) {
216 long tmo = xp->lft.soft_add_expires_seconds +
217 xp->curlft.add_time - now;
218 if (tmo <= 0) {
219 warn = 1;
220 tmo = XFRM_KM_TIMEOUT;
221 }
222 if (tmo < next)
223 next = tmo;
224 }
225 if (xp->lft.soft_use_expires_seconds) {
226 long tmo = xp->lft.soft_use_expires_seconds +
227 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
228 if (tmo <= 0) {
229 warn = 1;
230 tmo = XFRM_KM_TIMEOUT;
231 }
232 if (tmo < next)
233 next = tmo;
234 }
235
236 if (warn)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800237 km_policy_expired(xp, dir, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 if (next != LONG_MAX &&
239 !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
240 xfrm_pol_hold(xp);
241
242out:
243 read_unlock(&xp->lock);
244 xfrm_pol_put(xp);
245 return;
246
247expired:
248 read_unlock(&xp->lock);
Herbert Xu4666faa2005-06-18 22:43:22 -0700249 if (!xfrm_policy_delete(xp, dir))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800250 km_policy_expired(xp, dir, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 xfrm_pol_put(xp);
252}
253
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000254static struct flow_cache_object *xfrm_policy_flo_get(struct flow_cache_object *flo)
255{
256 struct xfrm_policy *pol = container_of(flo, struct xfrm_policy, flo);
257
258 if (unlikely(pol->walk.dead))
259 flo = NULL;
260 else
261 xfrm_pol_hold(pol);
262
263 return flo;
264}
265
266static int xfrm_policy_flo_check(struct flow_cache_object *flo)
267{
268 struct xfrm_policy *pol = container_of(flo, struct xfrm_policy, flo);
269
270 return !pol->walk.dead;
271}
272
273static void xfrm_policy_flo_delete(struct flow_cache_object *flo)
274{
275 xfrm_pol_put(container_of(flo, struct xfrm_policy, flo));
276}
277
278static const struct flow_cache_ops xfrm_policy_fc_ops = {
279 .get = xfrm_policy_flo_get,
280 .check = xfrm_policy_flo_check,
281 .delete = xfrm_policy_flo_delete,
282};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284/* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
285 * SPD calls.
286 */
287
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -0800288struct xfrm_policy *xfrm_policy_alloc(struct net *net, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
290 struct xfrm_policy *policy;
291
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700292 policy = kzalloc(sizeof(struct xfrm_policy), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294 if (policy) {
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -0800295 write_pnet(&policy->xp_net, net);
Herbert Xu12a169e2008-10-01 07:03:24 -0700296 INIT_LIST_HEAD(&policy->walk.all);
David S. Miller2518c7c2006-08-24 04:45:07 -0700297 INIT_HLIST_NODE(&policy->bydst);
298 INIT_HLIST_NODE(&policy->byidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 rwlock_init(&policy->lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700300 atomic_set(&policy->refcnt, 1);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100301 skb_queue_head_init(&policy->polq.hold_queue);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800302 setup_timer(&policy->timer, xfrm_policy_timer,
303 (unsigned long)policy);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100304 setup_timer(&policy->polq.hold_timer, xfrm_policy_queue_process,
305 (unsigned long)policy);
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000306 policy->flo.ops = &xfrm_policy_fc_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 }
308 return policy;
309}
310EXPORT_SYMBOL(xfrm_policy_alloc);
311
Eric Dumazet56f04732015-12-08 07:22:01 -0800312static void xfrm_policy_destroy_rcu(struct rcu_head *head)
313{
314 struct xfrm_policy *policy = container_of(head, struct xfrm_policy, rcu);
315
316 security_xfrm_policy_free(policy->security);
317 kfree(policy);
318}
319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320/* Destroy xfrm_policy: descendant resources must be released to this moment. */
321
WANG Cong64c31b32008-01-07 22:34:29 -0800322void xfrm_policy_destroy(struct xfrm_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
Herbert Xu12a169e2008-10-01 07:03:24 -0700324 BUG_ON(!policy->walk.dead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Fan Du0659eea2013-08-01 18:08:36 +0800326 if (del_timer(&policy->timer) || del_timer(&policy->polq.hold_timer))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 BUG();
328
Eric Dumazet56f04732015-12-08 07:22:01 -0800329 call_rcu(&policy->rcu, xfrm_policy_destroy_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
WANG Cong64c31b32008-01-07 22:34:29 -0800331EXPORT_SYMBOL(xfrm_policy_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333/* Rule must be locked. Release descentant resources, announce
334 * entry dead. The rule must be unlinked from lists to the moment.
335 */
336
337static void xfrm_policy_kill(struct xfrm_policy *policy)
338{
Herbert Xu12a169e2008-10-01 07:03:24 -0700339 policy->walk.dead = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Timo Teräs285ead12010-04-07 00:30:06 +0000341 atomic_inc(&policy->genid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200343 if (del_timer(&policy->polq.hold_timer))
344 xfrm_pol_put(policy);
Li RongQing1ee5e662015-04-22 15:51:16 +0800345 skb_queue_purge(&policy->polq.hold_queue);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100346
Timo Teräs285ead12010-04-07 00:30:06 +0000347 if (del_timer(&policy->timer))
348 xfrm_pol_put(policy);
349
350 xfrm_pol_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351}
352
David S. Miller2518c7c2006-08-24 04:45:07 -0700353static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024;
354
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800355static inline unsigned int idx_hash(struct net *net, u32 index)
David S. Miller2518c7c2006-08-24 04:45:07 -0700356{
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800357 return __idx_hash(index, net->xfrm.policy_idx_hmask);
David S. Miller2518c7c2006-08-24 04:45:07 -0700358}
359
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200360/* calculate policy hash thresholds */
361static void __get_hash_thresh(struct net *net,
362 unsigned short family, int dir,
363 u8 *dbits, u8 *sbits)
364{
365 switch (family) {
366 case AF_INET:
367 *dbits = net->xfrm.policy_bydst[dir].dbits4;
368 *sbits = net->xfrm.policy_bydst[dir].sbits4;
369 break;
370
371 case AF_INET6:
372 *dbits = net->xfrm.policy_bydst[dir].dbits6;
373 *sbits = net->xfrm.policy_bydst[dir].sbits6;
374 break;
375
376 default:
377 *dbits = 0;
378 *sbits = 0;
379 }
380}
381
David S. Miller5f803b52011-02-24 00:33:19 -0500382static struct hlist_head *policy_hash_bysel(struct net *net,
383 const struct xfrm_selector *sel,
384 unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700385{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800386 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200387 unsigned int hash;
388 u8 dbits;
389 u8 sbits;
390
391 __get_hash_thresh(net, family, dir, &dbits, &sbits);
392 hash = __sel_hash(sel, family, hmask, dbits, sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700393
Florian Westphale1e551b2016-08-11 15:17:53 +0200394 if (hash == hmask + 1)
395 return &net->xfrm.policy_inexact[dir];
396
397 return rcu_dereference_check(net->xfrm.policy_bydst[dir].table,
398 lockdep_is_held(&net->xfrm.xfrm_policy_lock)) + hash;
David S. Miller2518c7c2006-08-24 04:45:07 -0700399}
400
David S. Miller5f803b52011-02-24 00:33:19 -0500401static struct hlist_head *policy_hash_direct(struct net *net,
402 const xfrm_address_t *daddr,
403 const xfrm_address_t *saddr,
404 unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700405{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800406 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200407 unsigned int hash;
408 u8 dbits;
409 u8 sbits;
410
411 __get_hash_thresh(net, family, dir, &dbits, &sbits);
412 hash = __addr_hash(daddr, saddr, family, hmask, dbits, sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700413
Florian Westphale1e551b2016-08-11 15:17:53 +0200414 return rcu_dereference_check(net->xfrm.policy_bydst[dir].table,
415 lockdep_is_held(&net->xfrm.xfrm_policy_lock)) + hash;
David S. Miller2518c7c2006-08-24 04:45:07 -0700416}
417
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200418static void xfrm_dst_hash_transfer(struct net *net,
419 struct hlist_head *list,
David S. Miller2518c7c2006-08-24 04:45:07 -0700420 struct hlist_head *ndsttable,
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200421 unsigned int nhashmask,
422 int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700423{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800424 struct hlist_node *tmp, *entry0 = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700425 struct xfrm_policy *pol;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800426 unsigned int h0 = 0;
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200427 u8 dbits;
428 u8 sbits;
David S. Miller2518c7c2006-08-24 04:45:07 -0700429
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800430redo:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800431 hlist_for_each_entry_safe(pol, tmp, list, bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700432 unsigned int h;
433
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200434 __get_hash_thresh(net, pol->family, dir, &dbits, &sbits);
David S. Miller2518c7c2006-08-24 04:45:07 -0700435 h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200436 pol->family, nhashmask, dbits, sbits);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800437 if (!entry0) {
Florian Westphala5eefc12016-08-11 15:17:52 +0200438 hlist_del_rcu(&pol->bydst);
439 hlist_add_head_rcu(&pol->bydst, ndsttable + h);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800440 h0 = h;
441 } else {
442 if (h != h0)
443 continue;
Florian Westphala5eefc12016-08-11 15:17:52 +0200444 hlist_del_rcu(&pol->bydst);
445 hlist_add_behind_rcu(&pol->bydst, entry0);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800446 }
Sasha Levinb67bfe02013-02-27 17:06:00 -0800447 entry0 = &pol->bydst;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800448 }
449 if (!hlist_empty(list)) {
450 entry0 = NULL;
451 goto redo;
David S. Miller2518c7c2006-08-24 04:45:07 -0700452 }
453}
454
455static void xfrm_idx_hash_transfer(struct hlist_head *list,
456 struct hlist_head *nidxtable,
457 unsigned int nhashmask)
458{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800459 struct hlist_node *tmp;
David S. Miller2518c7c2006-08-24 04:45:07 -0700460 struct xfrm_policy *pol;
461
Sasha Levinb67bfe02013-02-27 17:06:00 -0800462 hlist_for_each_entry_safe(pol, tmp, list, byidx) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700463 unsigned int h;
464
465 h = __idx_hash(pol->index, nhashmask);
466 hlist_add_head(&pol->byidx, nidxtable+h);
467 }
468}
469
470static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
471{
472 return ((old_hmask + 1) << 1) - 1;
473}
474
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800475static void xfrm_bydst_resize(struct net *net, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700476{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800477 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700478 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
479 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
David S. Miller44e36b42006-08-24 04:50:50 -0700480 struct hlist_head *ndst = xfrm_hash_alloc(nsize);
Florian Westphale1e551b2016-08-11 15:17:53 +0200481 struct hlist_head *odst;
David S. Miller2518c7c2006-08-24 04:45:07 -0700482 int i;
483
484 if (!ndst)
485 return;
486
Fan Du283bc9f2013-11-07 17:47:50 +0800487 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Florian Westphal30846092016-08-11 15:17:54 +0200488 write_seqcount_begin(&xfrm_policy_hash_generation);
489
490 odst = rcu_dereference_protected(net->xfrm.policy_bydst[dir].table,
491 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
David S. Miller2518c7c2006-08-24 04:45:07 -0700492
Florian Westphale1e551b2016-08-11 15:17:53 +0200493 odst = rcu_dereference_protected(net->xfrm.policy_bydst[dir].table,
494 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
495
David S. Miller2518c7c2006-08-24 04:45:07 -0700496 for (i = hmask; i >= 0; i--)
Christophe Gouaultb58555f2014-08-29 16:16:04 +0200497 xfrm_dst_hash_transfer(net, odst + i, ndst, nhashmask, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700498
Florian Westphale1e551b2016-08-11 15:17:53 +0200499 rcu_assign_pointer(net->xfrm.policy_bydst[dir].table, ndst);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800500 net->xfrm.policy_bydst[dir].hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700501
Florian Westphal30846092016-08-11 15:17:54 +0200502 write_seqcount_end(&xfrm_policy_hash_generation);
Fan Du283bc9f2013-11-07 17:47:50 +0800503 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700504
Florian Westphale1e551b2016-08-11 15:17:53 +0200505 synchronize_rcu();
506
David S. Miller44e36b42006-08-24 04:50:50 -0700507 xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700508}
509
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800510static void xfrm_byidx_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700511{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800512 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700513 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
514 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800515 struct hlist_head *oidx = net->xfrm.policy_byidx;
David S. Miller44e36b42006-08-24 04:50:50 -0700516 struct hlist_head *nidx = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700517 int i;
518
519 if (!nidx)
520 return;
521
Fan Du283bc9f2013-11-07 17:47:50 +0800522 write_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700523
524 for (i = hmask; i >= 0; i--)
525 xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
526
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800527 net->xfrm.policy_byidx = nidx;
528 net->xfrm.policy_idx_hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700529
Fan Du283bc9f2013-11-07 17:47:50 +0800530 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700531
David S. Miller44e36b42006-08-24 04:50:50 -0700532 xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700533}
534
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800535static inline int xfrm_bydst_should_resize(struct net *net, int dir, int *total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700536{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800537 unsigned int cnt = net->xfrm.policy_count[dir];
538 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700539
540 if (total)
541 *total += cnt;
542
543 if ((hmask + 1) < xfrm_policy_hashmax &&
544 cnt > hmask)
545 return 1;
546
547 return 0;
548}
549
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800550static inline int xfrm_byidx_should_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700551{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800552 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700553
554 if ((hmask + 1) < xfrm_policy_hashmax &&
555 total > hmask)
556 return 1;
557
558 return 0;
559}
560
Alexey Dobriyane0710412010-01-23 13:37:10 +0000561void xfrm_spd_getinfo(struct net *net, struct xfrmk_spdinfo *si)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700562{
Alexey Dobriyane0710412010-01-23 13:37:10 +0000563 si->incnt = net->xfrm.policy_count[XFRM_POLICY_IN];
564 si->outcnt = net->xfrm.policy_count[XFRM_POLICY_OUT];
565 si->fwdcnt = net->xfrm.policy_count[XFRM_POLICY_FWD];
566 si->inscnt = net->xfrm.policy_count[XFRM_POLICY_IN+XFRM_POLICY_MAX];
567 si->outscnt = net->xfrm.policy_count[XFRM_POLICY_OUT+XFRM_POLICY_MAX];
568 si->fwdscnt = net->xfrm.policy_count[XFRM_POLICY_FWD+XFRM_POLICY_MAX];
569 si->spdhcnt = net->xfrm.policy_idx_hmask;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700570 si->spdhmcnt = xfrm_policy_hashmax;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700571}
572EXPORT_SYMBOL(xfrm_spd_getinfo);
David S. Miller2518c7c2006-08-24 04:45:07 -0700573
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700574static DEFINE_MUTEX(hash_resize_mutex);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800575static void xfrm_hash_resize(struct work_struct *work)
David S. Miller2518c7c2006-08-24 04:45:07 -0700576{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800577 struct net *net = container_of(work, struct net, xfrm.policy_hash_work);
David S. Miller2518c7c2006-08-24 04:45:07 -0700578 int dir, total;
579
580 mutex_lock(&hash_resize_mutex);
581
582 total = 0;
Herbert Xu53c2e282014-11-13 17:09:49 +0800583 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800584 if (xfrm_bydst_should_resize(net, dir, &total))
585 xfrm_bydst_resize(net, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700586 }
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800587 if (xfrm_byidx_should_resize(net, total))
588 xfrm_byidx_resize(net, total);
David S. Miller2518c7c2006-08-24 04:45:07 -0700589
590 mutex_unlock(&hash_resize_mutex);
591}
592
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200593static void xfrm_hash_rebuild(struct work_struct *work)
594{
595 struct net *net = container_of(work, struct net,
596 xfrm.policy_hthresh.work);
597 unsigned int hmask;
598 struct xfrm_policy *pol;
599 struct xfrm_policy *policy;
600 struct hlist_head *chain;
601 struct hlist_head *odst;
602 struct hlist_node *newpos;
603 int i;
604 int dir;
605 unsigned seq;
606 u8 lbits4, rbits4, lbits6, rbits6;
607
608 mutex_lock(&hash_resize_mutex);
609
610 /* read selector prefixlen thresholds */
611 do {
612 seq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
613
614 lbits4 = net->xfrm.policy_hthresh.lbits4;
615 rbits4 = net->xfrm.policy_hthresh.rbits4;
616 lbits6 = net->xfrm.policy_hthresh.lbits6;
617 rbits6 = net->xfrm.policy_hthresh.rbits6;
618 } while (read_seqretry(&net->xfrm.policy_hthresh.lock, seq));
619
620 write_lock_bh(&net->xfrm.xfrm_policy_lock);
621
622 /* reset the bydst and inexact table in all directions */
Herbert Xu53c2e282014-11-13 17:09:49 +0800623 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Christophe Gouault880a6fa2014-08-29 16:16:05 +0200624 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
625 hmask = net->xfrm.policy_bydst[dir].hmask;
626 odst = net->xfrm.policy_bydst[dir].table;
627 for (i = hmask; i >= 0; i--)
628 INIT_HLIST_HEAD(odst + i);
629 if ((dir & XFRM_POLICY_MASK) == XFRM_POLICY_OUT) {
630 /* dir out => dst = remote, src = local */
631 net->xfrm.policy_bydst[dir].dbits4 = rbits4;
632 net->xfrm.policy_bydst[dir].sbits4 = lbits4;
633 net->xfrm.policy_bydst[dir].dbits6 = rbits6;
634 net->xfrm.policy_bydst[dir].sbits6 = lbits6;
635 } else {
636 /* dir in/fwd => dst = local, src = remote */
637 net->xfrm.policy_bydst[dir].dbits4 = lbits4;
638 net->xfrm.policy_bydst[dir].sbits4 = rbits4;
639 net->xfrm.policy_bydst[dir].dbits6 = lbits6;
640 net->xfrm.policy_bydst[dir].sbits6 = rbits6;
641 }
642 }
643
644 /* re-insert all policies by order of creation */
645 list_for_each_entry_reverse(policy, &net->xfrm.policy_all, walk.all) {
646 newpos = NULL;
647 chain = policy_hash_bysel(net, &policy->selector,
648 policy->family,
649 xfrm_policy_id2dir(policy->index));
650 hlist_for_each_entry(pol, chain, bydst) {
651 if (policy->priority >= pol->priority)
652 newpos = &pol->bydst;
653 else
654 break;
655 }
656 if (newpos)
657 hlist_add_behind(&policy->bydst, newpos);
658 else
659 hlist_add_head(&policy->bydst, chain);
660 }
661
662 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
663
664 mutex_unlock(&hash_resize_mutex);
665}
666
667void xfrm_policy_hash_rebuild(struct net *net)
668{
669 schedule_work(&net->xfrm.policy_hthresh.work);
670}
671EXPORT_SYMBOL(xfrm_policy_hash_rebuild);
672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673/* Generate new index... KAME seems to generate them ordered by cost
674 * of an absolute inpredictability of ordering of rules. This will not pass. */
Fan Due682adf02013-11-07 17:47:48 +0800675static u32 xfrm_gen_index(struct net *net, int dir, u32 index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 static u32 idx_generator;
678
679 for (;;) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700680 struct hlist_head *list;
681 struct xfrm_policy *p;
682 u32 idx;
683 int found;
684
Fan Due682adf02013-11-07 17:47:48 +0800685 if (!index) {
686 idx = (idx_generator | dir);
687 idx_generator += 8;
688 } else {
689 idx = index;
690 index = 0;
691 }
692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 if (idx == 0)
694 idx = 8;
Alexey Dobriyan11219942008-11-25 17:33:06 -0800695 list = net->xfrm.policy_byidx + idx_hash(net, idx);
David S. Miller2518c7c2006-08-24 04:45:07 -0700696 found = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800697 hlist_for_each_entry(p, list, byidx) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700698 if (p->index == idx) {
699 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 break;
David S. Miller2518c7c2006-08-24 04:45:07 -0700701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700703 if (!found)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 return idx;
705 }
706}
707
David S. Miller2518c7c2006-08-24 04:45:07 -0700708static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
709{
710 u32 *p1 = (u32 *) s1;
711 u32 *p2 = (u32 *) s2;
712 int len = sizeof(struct xfrm_selector) / sizeof(u32);
713 int i;
714
715 for (i = 0; i < len; i++) {
716 if (p1[i] != p2[i])
717 return 1;
718 }
719
720 return 0;
721}
722
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100723static void xfrm_policy_requeue(struct xfrm_policy *old,
724 struct xfrm_policy *new)
725{
726 struct xfrm_policy_queue *pq = &old->polq;
727 struct sk_buff_head list;
728
Li RongQingde2ad482015-04-30 17:25:19 +0800729 if (skb_queue_empty(&pq->hold_queue))
730 return;
731
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100732 __skb_queue_head_init(&list);
733
734 spin_lock_bh(&pq->hold_queue.lock);
735 skb_queue_splice_init(&pq->hold_queue, &list);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200736 if (del_timer(&pq->hold_timer))
737 xfrm_pol_put(old);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100738 spin_unlock_bh(&pq->hold_queue.lock);
739
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100740 pq = &new->polq;
741
742 spin_lock_bh(&pq->hold_queue.lock);
743 skb_queue_splice(&list, &pq->hold_queue);
744 pq->timeout = XFRM_QUEUE_TMO_MIN;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200745 if (!mod_timer(&pq->hold_timer, jiffies))
746 xfrm_pol_hold(new);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100747 spin_unlock_bh(&pq->hold_queue.lock);
748}
749
Steffen Klassert7cb8a932013-02-11 07:02:36 +0100750static bool xfrm_policy_mark_match(struct xfrm_policy *policy,
751 struct xfrm_policy *pol)
752{
753 u32 mark = policy->mark.v & policy->mark.m;
754
755 if (policy->mark.v == pol->mark.v && policy->mark.m == pol->mark.m)
756 return true;
757
758 if ((mark & pol->mark.m) == pol->mark.v &&
759 policy->priority == pol->priority)
760 return true;
761
762 return false;
763}
764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
766{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800767 struct net *net = xp_net(policy);
David S. Miller2518c7c2006-08-24 04:45:07 -0700768 struct xfrm_policy *pol;
769 struct xfrm_policy *delpol;
770 struct hlist_head *chain;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800771 struct hlist_node *newpos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Fan Du283bc9f2013-11-07 17:47:50 +0800773 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800774 chain = policy_hash_bysel(net, &policy->selector, policy->family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700775 delpol = NULL;
776 newpos = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800777 hlist_for_each_entry(pol, chain, bydst) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800778 if (pol->type == policy->type &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700779 !selector_cmp(&pol->selector, &policy->selector) &&
Steffen Klassert7cb8a932013-02-11 07:02:36 +0100780 xfrm_policy_mark_match(policy, pol) &&
Herbert Xua6c7ab52007-01-16 16:52:02 -0800781 xfrm_sec_ctx_match(pol->security, policy->security) &&
782 !WARN_ON(delpol)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 if (excl) {
Fan Du283bc9f2013-11-07 17:47:50 +0800784 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 return -EEXIST;
786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 delpol = pol;
788 if (policy->priority > pol->priority)
789 continue;
790 } else if (policy->priority >= pol->priority) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800791 newpos = &pol->bydst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 continue;
793 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 if (delpol)
795 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 }
797 if (newpos)
Ken Helias1d023282014-08-06 16:09:16 -0700798 hlist_add_behind(&policy->bydst, newpos);
David S. Miller2518c7c2006-08-24 04:45:07 -0700799 else
800 hlist_add_head(&policy->bydst, chain);
Herbert Xu12bfa8b2014-11-13 17:09:50 +0800801 __xfrm_policy_link(policy, dir);
Fan Duca925cf2014-01-18 09:55:27 +0800802 atomic_inc(&net->xfrm.flow_cache_genid);
fan.duca4c3fc2013-07-30 08:33:53 +0800803
804 /* After previous checking, family can either be AF_INET or AF_INET6 */
805 if (policy->family == AF_INET)
806 rt_genid_bump_ipv4(net);
807 else
808 rt_genid_bump_ipv6(net);
809
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100810 if (delpol) {
811 xfrm_policy_requeue(delpol, policy);
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800812 __xfrm_policy_unlink(delpol, dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100813 }
Fan Due682adf02013-11-07 17:47:48 +0800814 policy->index = delpol ? delpol->index : xfrm_gen_index(net, dir, policy->index);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800815 hlist_add_head(&policy->byidx, net->xfrm.policy_byidx+idx_hash(net, policy->index));
James Morris9d729f72007-03-04 16:12:44 -0800816 policy->curlft.add_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 policy->curlft.use_time = 0;
818 if (!mod_timer(&policy->timer, jiffies + HZ))
819 xfrm_pol_hold(policy);
Fan Du283bc9f2013-11-07 17:47:50 +0800820 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
David S. Miller9b78a822005-12-22 07:39:48 -0800822 if (delpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 xfrm_policy_kill(delpol);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800824 else if (xfrm_bydst_should_resize(net, dir, NULL))
825 schedule_work(&net->xfrm.policy_hash_work);
David S. Miller9b78a822005-12-22 07:39:48 -0800826
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 return 0;
828}
829EXPORT_SYMBOL(xfrm_policy_insert);
830
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +0000831struct xfrm_policy *xfrm_policy_bysel_ctx(struct net *net, u32 mark, u8 type,
832 int dir, struct xfrm_selector *sel,
Eric Parisef41aaa2007-03-07 15:37:58 -0800833 struct xfrm_sec_ctx *ctx, int delete,
834 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
David S. Miller2518c7c2006-08-24 04:45:07 -0700836 struct xfrm_policy *pol, *ret;
837 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Eric Parisef41aaa2007-03-07 15:37:58 -0800839 *err = 0;
Fan Du283bc9f2013-11-07 17:47:50 +0800840 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800841 chain = policy_hash_bysel(net, sel, sel->family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700842 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800843 hlist_for_each_entry(pol, chain, bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700844 if (pol->type == type &&
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000845 (mark & pol->mark.m) == pol->mark.v &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700846 !selector_cmp(sel, &pol->selector) &&
847 xfrm_sec_ctx_match(ctx, pol->security)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700849 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -0700850 *err = security_xfrm_policy_delete(
851 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -0800852 if (*err) {
Fan Du283bc9f2013-11-07 17:47:50 +0800853 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Eric Parisef41aaa2007-03-07 15:37:58 -0800854 return pol;
855 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800856 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700857 }
858 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 break;
860 }
861 }
Fan Du283bc9f2013-11-07 17:47:50 +0800862 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000864 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -0700865 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -0700866 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867}
Trent Jaegerdf718372005-12-13 23:12:27 -0800868EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +0000870struct xfrm_policy *xfrm_policy_byid(struct net *net, u32 mark, u8 type,
871 int dir, u32 id, int delete, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
David S. Miller2518c7c2006-08-24 04:45:07 -0700873 struct xfrm_policy *pol, *ret;
874 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Herbert Xub5505c62007-05-14 02:15:47 -0700876 *err = -ENOENT;
877 if (xfrm_policy_id2dir(id) != dir)
878 return NULL;
879
Eric Parisef41aaa2007-03-07 15:37:58 -0800880 *err = 0;
Fan Du283bc9f2013-11-07 17:47:50 +0800881 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800882 chain = net->xfrm.policy_byidx + idx_hash(net, id);
David S. Miller2518c7c2006-08-24 04:45:07 -0700883 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800884 hlist_for_each_entry(pol, chain, byidx) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000885 if (pol->type == type && pol->index == id &&
886 (mark & pol->mark.m) == pol->mark.v) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700888 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -0700889 *err = security_xfrm_policy_delete(
890 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -0800891 if (*err) {
Fan Du283bc9f2013-11-07 17:47:50 +0800892 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Eric Parisef41aaa2007-03-07 15:37:58 -0800893 return pol;
894 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800895 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700896 }
897 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 break;
899 }
900 }
Fan Du283bc9f2013-11-07 17:47:50 +0800901 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000903 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -0700904 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -0700905 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906}
907EXPORT_SYMBOL(xfrm_policy_byid);
908
Joy Latten4aa2e622007-06-04 19:05:57 -0400909#ifdef CONFIG_SECURITY_NETWORK_XFRM
910static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900911xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912{
Joy Latten4aa2e622007-06-04 19:05:57 -0400913 int dir, err = 0;
914
915 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
916 struct xfrm_policy *pol;
Joy Latten4aa2e622007-06-04 19:05:57 -0400917 int i;
918
Sasha Levinb67bfe02013-02-27 17:06:00 -0800919 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800920 &net->xfrm.policy_inexact[dir], bydst) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400921 if (pol->type != type)
922 continue;
Paul Moore03e1ad72008-04-12 19:07:52 -0700923 err = security_xfrm_policy_delete(pol->security);
Joy Latten4aa2e622007-06-04 19:05:57 -0400924 if (err) {
Tetsuo Handa2e710292014-04-22 21:48:30 +0900925 xfrm_audit_policy_delete(pol, 0, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400926 return err;
927 }
YOSHIFUJI Hideaki7dc12d62007-07-19 10:45:15 +0900928 }
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800929 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800930 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800931 net->xfrm.policy_bydst[dir].table + i,
Joy Latten4aa2e622007-06-04 19:05:57 -0400932 bydst) {
933 if (pol->type != type)
934 continue;
Paul Moore03e1ad72008-04-12 19:07:52 -0700935 err = security_xfrm_policy_delete(
936 pol->security);
Joy Latten4aa2e622007-06-04 19:05:57 -0400937 if (err) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700938 xfrm_audit_policy_delete(pol, 0,
Tetsuo Handa2e710292014-04-22 21:48:30 +0900939 task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400940 return err;
941 }
942 }
943 }
944 }
945 return err;
946}
947#else
948static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900949xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -0400950{
951 return 0;
952}
953#endif
954
Tetsuo Handa2e710292014-04-22 21:48:30 +0900955int xfrm_policy_flush(struct net *net, u8 type, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -0400956{
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +0000957 int dir, err = 0, cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Fan Du283bc9f2013-11-07 17:47:50 +0800959 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400960
Tetsuo Handa2e710292014-04-22 21:48:30 +0900961 err = xfrm_policy_flush_secctx_check(net, type, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400962 if (err)
963 goto out;
964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700966 struct xfrm_policy *pol;
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800967 int i;
David S. Miller2518c7c2006-08-24 04:45:07 -0700968
969 again1:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800970 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800971 &net->xfrm.policy_inexact[dir], bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700972 if (pol->type != type)
973 continue;
Timo Teräsea2dea9d2010-03-31 00:17:05 +0000974 __xfrm_policy_unlink(pol, dir);
Fan Du283bc9f2013-11-07 17:47:50 +0800975 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Timo Teräsea2dea9d2010-03-31 00:17:05 +0000976 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Tetsuo Handa2e710292014-04-22 21:48:30 +0900978 xfrm_audit_policy_delete(pol, 1, task_valid);
Joy Latten161a09e2006-11-27 13:11:54 -0600979
David S. Miller2518c7c2006-08-24 04:45:07 -0700980 xfrm_policy_kill(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Fan Du283bc9f2013-11-07 17:47:50 +0800982 write_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700983 goto again1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700985
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800986 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700987 again2:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800988 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800989 net->xfrm.policy_bydst[dir].table + i,
David S. Miller2518c7c2006-08-24 04:45:07 -0700990 bydst) {
991 if (pol->type != type)
992 continue;
Timo Teräsea2dea9d2010-03-31 00:17:05 +0000993 __xfrm_policy_unlink(pol, dir);
Fan Du283bc9f2013-11-07 17:47:50 +0800994 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Timo Teräsea2dea9d2010-03-31 00:17:05 +0000995 cnt++;
David S. Miller2518c7c2006-08-24 04:45:07 -0700996
Tetsuo Handa2e710292014-04-22 21:48:30 +0900997 xfrm_audit_policy_delete(pol, 1, task_valid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700998 xfrm_policy_kill(pol);
999
Fan Du283bc9f2013-11-07 17:47:50 +08001000 write_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -07001001 goto again2;
1002 }
1003 }
1004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 }
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +00001006 if (!cnt)
1007 err = -ESRCH;
Joy Latten4aa2e622007-06-04 19:05:57 -04001008out:
Fan Du283bc9f2013-11-07 17:47:50 +08001009 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -04001010 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011}
1012EXPORT_SYMBOL(xfrm_policy_flush);
1013
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001014int xfrm_policy_walk(struct net *net, struct xfrm_policy_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -08001015 int (*func)(struct xfrm_policy *, int, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 void *data)
1017{
Herbert Xu12a169e2008-10-01 07:03:24 -07001018 struct xfrm_policy *pol;
1019 struct xfrm_policy_walk_entry *x;
Timo Teras4c563f72008-02-28 21:31:08 -08001020 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Timo Teras4c563f72008-02-28 21:31:08 -08001022 if (walk->type >= XFRM_POLICY_TYPE_MAX &&
1023 walk->type != XFRM_POLICY_TYPE_ANY)
1024 return -EINVAL;
1025
Herbert Xu12a169e2008-10-01 07:03:24 -07001026 if (list_empty(&walk->walk.all) && walk->seq != 0)
Timo Teras4c563f72008-02-28 21:31:08 -08001027 return 0;
1028
Fan Du283bc9f2013-11-07 17:47:50 +08001029 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001030 if (list_empty(&walk->walk.all))
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001031 x = list_first_entry(&net->xfrm.policy_all, struct xfrm_policy_walk_entry, all);
Herbert Xu12a169e2008-10-01 07:03:24 -07001032 else
Li RongQing80077702015-04-22 17:09:54 +08001033 x = list_first_entry(&walk->walk.all,
1034 struct xfrm_policy_walk_entry, all);
1035
Alexey Dobriyancdcbca72008-11-25 17:34:49 -08001036 list_for_each_entry_from(x, &net->xfrm.policy_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -07001037 if (x->dead)
Timo Teras4c563f72008-02-28 21:31:08 -08001038 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -07001039 pol = container_of(x, struct xfrm_policy, walk);
1040 if (walk->type != XFRM_POLICY_TYPE_ANY &&
1041 walk->type != pol->type)
1042 continue;
1043 error = func(pol, xfrm_policy_id2dir(pol->index),
1044 walk->seq, data);
1045 if (error) {
1046 list_move_tail(&walk->walk.all, &x->all);
1047 goto out;
Timo Teras4c563f72008-02-28 21:31:08 -08001048 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001049 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001051 if (walk->seq == 0) {
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -08001052 error = -ENOENT;
1053 goto out;
1054 }
Herbert Xu12a169e2008-10-01 07:03:24 -07001055 list_del_init(&walk->walk.all);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056out:
Fan Du283bc9f2013-11-07 17:47:50 +08001057 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 return error;
1059}
1060EXPORT_SYMBOL(xfrm_policy_walk);
1061
Herbert Xu12a169e2008-10-01 07:03:24 -07001062void xfrm_policy_walk_init(struct xfrm_policy_walk *walk, u8 type)
1063{
1064 INIT_LIST_HEAD(&walk->walk.all);
1065 walk->walk.dead = 1;
1066 walk->type = type;
1067 walk->seq = 0;
1068}
1069EXPORT_SYMBOL(xfrm_policy_walk_init);
1070
Fan Du283bc9f2013-11-07 17:47:50 +08001071void xfrm_policy_walk_done(struct xfrm_policy_walk *walk, struct net *net)
Herbert Xu12a169e2008-10-01 07:03:24 -07001072{
1073 if (list_empty(&walk->walk.all))
1074 return;
1075
Fan Du283bc9f2013-11-07 17:47:50 +08001076 write_lock_bh(&net->xfrm.xfrm_policy_lock); /*FIXME where is net? */
Herbert Xu12a169e2008-10-01 07:03:24 -07001077 list_del(&walk->walk.all);
Fan Du283bc9f2013-11-07 17:47:50 +08001078 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07001079}
1080EXPORT_SYMBOL(xfrm_policy_walk_done);
1081
James Morris134b0fc2006-10-05 15:42:27 -05001082/*
1083 * Find policy to apply to this flow.
1084 *
1085 * Returns 0 if policy found, else an -errno.
1086 */
David S. Millerf299d552011-02-24 01:23:30 -05001087static int xfrm_policy_match(const struct xfrm_policy *pol,
1088 const struct flowi *fl,
David S. Miller2518c7c2006-08-24 04:45:07 -07001089 u8 type, u16 family, int dir)
1090{
David S. Millerf299d552011-02-24 01:23:30 -05001091 const struct xfrm_selector *sel = &pol->selector;
David S. Millerbc9b35a2012-05-15 15:04:57 -04001092 int ret = -ESRCH;
1093 bool match;
David S. Miller2518c7c2006-08-24 04:45:07 -07001094
1095 if (pol->family != family ||
David S. Miller1d28f422011-03-12 00:29:39 -05001096 (fl->flowi_mark & pol->mark.m) != pol->mark.v ||
David S. Miller2518c7c2006-08-24 04:45:07 -07001097 pol->type != type)
James Morris134b0fc2006-10-05 15:42:27 -05001098 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -07001099
1100 match = xfrm_selector_match(sel, fl, family);
James Morris134b0fc2006-10-05 15:42:27 -05001101 if (match)
David S. Miller1d28f422011-03-12 00:29:39 -05001102 ret = security_xfrm_policy_lookup(pol->security, fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -07001103 dir);
David S. Miller2518c7c2006-08-24 04:45:07 -07001104
James Morris134b0fc2006-10-05 15:42:27 -05001105 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -07001106}
1107
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001108static struct xfrm_policy *xfrm_policy_lookup_bytype(struct net *net, u8 type,
David S. Miller062cdb42011-02-22 18:31:08 -08001109 const struct flowi *fl,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001110 u16 family, u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111{
James Morris134b0fc2006-10-05 15:42:27 -05001112 int err;
David S. Miller2518c7c2006-08-24 04:45:07 -07001113 struct xfrm_policy *pol, *ret;
David S. Miller0b597e72011-02-24 01:22:48 -05001114 const xfrm_address_t *daddr, *saddr;
David S. Miller2518c7c2006-08-24 04:45:07 -07001115 struct hlist_head *chain;
Florian Westphal30846092016-08-11 15:17:54 +02001116 unsigned int sequence;
1117 u32 priority;
David S. Miller2518c7c2006-08-24 04:45:07 -07001118
1119 daddr = xfrm_flowi_daddr(fl, family);
1120 saddr = xfrm_flowi_saddr(fl, family);
1121 if (unlikely(!daddr || !saddr))
1122 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Florian Westphala7c442472016-08-11 15:17:56 +02001124 rcu_read_lock();
Florian Westphal30846092016-08-11 15:17:54 +02001125 retry:
1126 do {
1127 sequence = read_seqcount_begin(&xfrm_policy_hash_generation);
1128 chain = policy_hash_direct(net, daddr, saddr, family, dir);
1129 } while (read_seqcount_retry(&xfrm_policy_hash_generation, sequence));
1130
1131 priority = ~0U;
David S. Miller2518c7c2006-08-24 04:45:07 -07001132 ret = NULL;
Florian Westphala5eefc12016-08-11 15:17:52 +02001133 hlist_for_each_entry_rcu(pol, chain, bydst) {
James Morris134b0fc2006-10-05 15:42:27 -05001134 err = xfrm_policy_match(pol, fl, type, family, dir);
1135 if (err) {
1136 if (err == -ESRCH)
1137 continue;
1138 else {
1139 ret = ERR_PTR(err);
1140 goto fail;
1141 }
1142 } else {
David S. Milleracba48e2006-08-25 15:46:46 -07001143 ret = pol;
1144 priority = ret->priority;
1145 break;
1146 }
1147 }
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001148 chain = &net->xfrm.policy_inexact[dir];
Florian Westphala5eefc12016-08-11 15:17:52 +02001149 hlist_for_each_entry_rcu(pol, chain, bydst) {
Li RongQing8faf4912015-05-14 11:16:59 +08001150 if ((pol->priority >= priority) && ret)
1151 break;
1152
James Morris134b0fc2006-10-05 15:42:27 -05001153 err = xfrm_policy_match(pol, fl, type, family, dir);
1154 if (err) {
1155 if (err == -ESRCH)
1156 continue;
1157 else {
1158 ret = ERR_PTR(err);
1159 goto fail;
1160 }
Li RongQing8faf4912015-05-14 11:16:59 +08001161 } else {
David S. Miller2518c7c2006-08-24 04:45:07 -07001162 ret = pol;
1163 break;
1164 }
1165 }
Li RongQing586f2eb2015-04-30 17:13:41 +08001166
Florian Westphal30846092016-08-11 15:17:54 +02001167 if (read_seqcount_retry(&xfrm_policy_hash_generation, sequence))
1168 goto retry;
1169
Florian Westphale37cc8a2016-08-11 15:17:55 +02001170 if (ret && !xfrm_pol_hold_rcu(ret))
1171 goto retry;
James Morris134b0fc2006-10-05 15:42:27 -05001172fail:
Florian Westphala7c442472016-08-11 15:17:56 +02001173 rcu_read_unlock();
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001174
David S. Miller2518c7c2006-08-24 04:45:07 -07001175 return ret;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001176}
1177
Timo Teräs80c802f2010-04-07 00:30:05 +00001178static struct xfrm_policy *
David S. Miller73ff93c2011-02-22 18:33:42 -08001179__xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir)
Timo Teräs80c802f2010-04-07 00:30:05 +00001180{
1181#ifdef CONFIG_XFRM_SUB_POLICY
1182 struct xfrm_policy *pol;
1183
1184 pol = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_SUB, fl, family, dir);
1185 if (pol != NULL)
1186 return pol;
1187#endif
1188 return xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN, fl, family, dir);
1189}
1190
Baker Zhangb5fb82c2013-03-19 04:24:30 +00001191static int flow_to_policy_dir(int dir)
1192{
1193 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
1194 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1195 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1196 return dir;
1197
1198 switch (dir) {
1199 default:
1200 case FLOW_DIR_IN:
1201 return XFRM_POLICY_IN;
1202 case FLOW_DIR_OUT:
1203 return XFRM_POLICY_OUT;
1204 case FLOW_DIR_FWD:
1205 return XFRM_POLICY_FWD;
1206 }
1207}
1208
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001209static struct flow_cache_object *
David S. Millerdee9f4b2011-02-22 18:44:31 -08001210xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family,
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001211 u8 dir, struct flow_cache_object *old_obj, void *ctx)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001212{
1213 struct xfrm_policy *pol;
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001214
1215 if (old_obj)
1216 xfrm_pol_put(container_of(old_obj, struct xfrm_policy, flo));
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001217
Baker Zhangb5fb82c2013-03-19 04:24:30 +00001218 pol = __xfrm_policy_lookup(net, fl, family, flow_to_policy_dir(dir));
Timo Teräs80c802f2010-04-07 00:30:05 +00001219 if (IS_ERR_OR_NULL(pol))
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001220 return ERR_CAST(pol);
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001221
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001222 /* Resolver returns two references:
1223 * one for cache and one for caller of flow_cache_lookup() */
1224 xfrm_pol_hold(pol);
1225
1226 return &pol->flo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227}
1228
Trent Jaegerdf718372005-12-13 23:12:27 -08001229static inline int policy_to_flow_dir(int dir)
1230{
1231 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001232 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1233 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1234 return dir;
1235 switch (dir) {
1236 default:
1237 case XFRM_POLICY_IN:
1238 return FLOW_DIR_IN;
1239 case XFRM_POLICY_OUT:
1240 return FLOW_DIR_OUT;
1241 case XFRM_POLICY_FWD:
1242 return FLOW_DIR_FWD;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001243 }
Trent Jaegerdf718372005-12-13 23:12:27 -08001244}
1245
Eric Dumazet6f9c9612015-09-25 07:39:10 -07001246static struct xfrm_policy *xfrm_sk_policy_lookup(const struct sock *sk, int dir,
David S. Millerdee9f4b2011-02-22 18:44:31 -08001247 const struct flowi *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248{
1249 struct xfrm_policy *pol;
1250
Eric Dumazetd188ba82015-12-08 07:22:02 -08001251 rcu_read_lock();
Florian Westphalae337862016-08-11 15:17:57 +02001252 again:
Eric Dumazetd188ba82015-12-08 07:22:02 -08001253 pol = rcu_dereference(sk->sk_policy[dir]);
1254 if (pol != NULL) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04001255 bool match = xfrm_selector_match(&pol->selector, fl,
1256 sk->sk_family);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001257 int err = 0;
Trent Jaegerdf718372005-12-13 23:12:27 -08001258
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001259 if (match) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001260 if ((sk->sk_mark & pol->mark.m) != pol->mark.v) {
1261 pol = NULL;
1262 goto out;
1263 }
Paul Moore03e1ad72008-04-12 19:07:52 -07001264 err = security_xfrm_policy_lookup(pol->security,
David S. Miller1d28f422011-03-12 00:29:39 -05001265 fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -07001266 policy_to_flow_dir(dir));
Florian Westphalae337862016-08-11 15:17:57 +02001267 if (!err && !xfrm_pol_hold_rcu(pol))
1268 goto again;
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001269 else if (err == -ESRCH)
1270 pol = NULL;
1271 else
1272 pol = ERR_PTR(err);
1273 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 pol = NULL;
1275 }
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001276out:
Eric Dumazetd188ba82015-12-08 07:22:02 -08001277 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 return pol;
1279}
1280
1281static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
1282{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001283 struct net *net = xp_net(pol);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001284
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001285 list_add(&pol->walk.all, &net->xfrm.policy_all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001286 net->xfrm.policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 xfrm_pol_hold(pol);
1288}
1289
1290static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
1291 int dir)
1292{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001293 struct net *net = xp_net(pol);
1294
Herbert Xu53c2e282014-11-13 17:09:49 +08001295 if (list_empty(&pol->walk.all))
David S. Miller2518c7c2006-08-24 04:45:07 -07001296 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
Herbert Xu53c2e282014-11-13 17:09:49 +08001298 /* Socket policies are not hashed. */
1299 if (!hlist_unhashed(&pol->bydst)) {
Florian Westphala5eefc12016-08-11 15:17:52 +02001300 hlist_del_rcu(&pol->bydst);
Herbert Xu53c2e282014-11-13 17:09:49 +08001301 hlist_del(&pol->byidx);
1302 }
1303
1304 list_del_init(&pol->walk.all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001305 net->xfrm.policy_count[dir]--;
David S. Miller2518c7c2006-08-24 04:45:07 -07001306
1307 return pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308}
1309
Herbert Xu53c2e282014-11-13 17:09:49 +08001310static void xfrm_sk_policy_link(struct xfrm_policy *pol, int dir)
1311{
1312 __xfrm_policy_link(pol, XFRM_POLICY_MAX + dir);
1313}
1314
1315static void xfrm_sk_policy_unlink(struct xfrm_policy *pol, int dir)
1316{
1317 __xfrm_policy_unlink(pol, XFRM_POLICY_MAX + dir);
1318}
1319
Herbert Xu4666faa2005-06-18 22:43:22 -07001320int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321{
Fan Du283bc9f2013-11-07 17:47:50 +08001322 struct net *net = xp_net(pol);
1323
1324 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 pol = __xfrm_policy_unlink(pol, dir);
Fan Du283bc9f2013-11-07 17:47:50 +08001326 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 if (pol) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 xfrm_policy_kill(pol);
Herbert Xu4666faa2005-06-18 22:43:22 -07001329 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 }
Herbert Xu4666faa2005-06-18 22:43:22 -07001331 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332}
David S. Millera70fcb02006-03-20 19:18:52 -08001333EXPORT_SYMBOL(xfrm_policy_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
1335int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
1336{
Alexey Dobriyan11219942008-11-25 17:33:06 -08001337 struct net *net = xp_net(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 struct xfrm_policy *old_pol;
1339
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001340#ifdef CONFIG_XFRM_SUB_POLICY
1341 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
1342 return -EINVAL;
1343#endif
1344
Fan Du283bc9f2013-11-07 17:47:50 +08001345 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Eric Dumazetd188ba82015-12-08 07:22:02 -08001346 old_pol = rcu_dereference_protected(sk->sk_policy[dir],
1347 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 if (pol) {
James Morris9d729f72007-03-04 16:12:44 -08001349 pol->curlft.add_time = get_seconds();
Fan Due682adf02013-11-07 17:47:48 +08001350 pol->index = xfrm_gen_index(net, XFRM_POLICY_MAX+dir, 0);
Herbert Xu53c2e282014-11-13 17:09:49 +08001351 xfrm_sk_policy_link(pol, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 }
Eric Dumazetd188ba82015-12-08 07:22:02 -08001353 rcu_assign_pointer(sk->sk_policy[dir], pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001354 if (old_pol) {
1355 if (pol)
1356 xfrm_policy_requeue(old_pol, pol);
1357
Timo Teräsea2dea9d2010-03-31 00:17:05 +00001358 /* Unlinking succeeds always. This is the only function
1359 * allowed to delete or replace socket policy.
1360 */
Herbert Xu53c2e282014-11-13 17:09:49 +08001361 xfrm_sk_policy_unlink(old_pol, dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001362 }
Fan Du283bc9f2013-11-07 17:47:50 +08001363 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
1365 if (old_pol) {
1366 xfrm_policy_kill(old_pol);
1367 }
1368 return 0;
1369}
1370
David S. Millerd3e40a92011-02-24 01:25:41 -05001371static struct xfrm_policy *clone_policy(const struct xfrm_policy *old, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372{
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -08001373 struct xfrm_policy *newp = xfrm_policy_alloc(xp_net(old), GFP_ATOMIC);
Fan Du283bc9f2013-11-07 17:47:50 +08001374 struct net *net = xp_net(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
1376 if (newp) {
1377 newp->selector = old->selector;
Paul Moore03e1ad72008-04-12 19:07:52 -07001378 if (security_xfrm_policy_clone(old->security,
1379 &newp->security)) {
Trent Jaegerdf718372005-12-13 23:12:27 -08001380 kfree(newp);
1381 return NULL; /* ENOMEM */
1382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 newp->lft = old->lft;
1384 newp->curlft = old->curlft;
Jamal Hadi Salimfb977e22010-02-23 15:09:53 -08001385 newp->mark = old->mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 newp->action = old->action;
1387 newp->flags = old->flags;
1388 newp->xfrm_nr = old->xfrm_nr;
1389 newp->index = old->index;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001390 newp->type = old->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 memcpy(newp->xfrm_vec, old->xfrm_vec,
1392 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
Fan Du283bc9f2013-11-07 17:47:50 +08001393 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu53c2e282014-11-13 17:09:49 +08001394 xfrm_sk_policy_link(newp, dir);
Fan Du283bc9f2013-11-07 17:47:50 +08001395 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 xfrm_pol_put(newp);
1397 }
1398 return newp;
1399}
1400
Eric Dumazetd188ba82015-12-08 07:22:02 -08001401int __xfrm_sk_clone_policy(struct sock *sk, const struct sock *osk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402{
Eric Dumazetd188ba82015-12-08 07:22:02 -08001403 const struct xfrm_policy *p;
1404 struct xfrm_policy *np;
1405 int i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Eric Dumazetd188ba82015-12-08 07:22:02 -08001407 rcu_read_lock();
1408 for (i = 0; i < 2; i++) {
1409 p = rcu_dereference(osk->sk_policy[i]);
1410 if (p) {
1411 np = clone_policy(p, i);
1412 if (unlikely(!np)) {
1413 ret = -ENOMEM;
1414 break;
1415 }
1416 rcu_assign_pointer(sk->sk_policy[i], np);
1417 }
1418 }
1419 rcu_read_unlock();
1420 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421}
1422
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001423static int
David Ahern42a7b322015-08-10 16:58:11 -06001424xfrm_get_saddr(struct net *net, int oif, xfrm_address_t *local,
1425 xfrm_address_t *remote, unsigned short family)
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001426{
1427 int err;
1428 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1429
1430 if (unlikely(afinfo == NULL))
1431 return -EINVAL;
David Ahern42a7b322015-08-10 16:58:11 -06001432 err = afinfo->get_saddr(net, oif, local, remote);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001433 xfrm_policy_put_afinfo(afinfo);
1434 return err;
1435}
1436
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437/* Resolve list of templates for the flow, given policy. */
1438
1439static int
David S. Millera6c2e612011-02-22 18:35:39 -08001440xfrm_tmpl_resolve_one(struct xfrm_policy *policy, const struct flowi *fl,
1441 struct xfrm_state **xfrm, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442{
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001443 struct net *net = xp_net(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 int nx;
1445 int i, error;
1446 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
1447 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001448 xfrm_address_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Weilong Chen9b7a7872013-12-24 09:43:46 +08001450 for (nx = 0, i = 0; i < policy->xfrm_nr; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 struct xfrm_state *x;
1452 xfrm_address_t *remote = daddr;
1453 xfrm_address_t *local = saddr;
1454 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
1455
Joakim Koskela48b8d782007-07-26 00:08:42 -07001456 if (tmpl->mode == XFRM_MODE_TUNNEL ||
1457 tmpl->mode == XFRM_MODE_BEET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 remote = &tmpl->id.daddr;
1459 local = &tmpl->saddr;
Thomas Egerer8444cf72010-09-20 11:11:38 -07001460 if (xfrm_addr_any(local, tmpl->encap_family)) {
David Ahern42a7b322015-08-10 16:58:11 -06001461 error = xfrm_get_saddr(net, fl->flowi_oif,
1462 &tmp, remote,
1463 tmpl->encap_family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001464 if (error)
1465 goto fail;
1466 local = &tmp;
1467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 }
1469
1470 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
1471
1472 if (x && x->km.state == XFRM_STATE_VALID) {
1473 xfrm[nx++] = x;
1474 daddr = remote;
1475 saddr = local;
1476 continue;
1477 }
1478 if (x) {
1479 error = (x->km.state == XFRM_STATE_ERROR ?
1480 -EINVAL : -EAGAIN);
1481 xfrm_state_put(x);
Weilong Chen420545692013-12-24 09:43:49 +08001482 } else if (error == -ESRCH) {
fernando@oss.ntt.coa43222662008-10-23 04:27:19 +00001483 error = -EAGAIN;
Weilong Chen420545692013-12-24 09:43:49 +08001484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
1486 if (!tmpl->optional)
1487 goto fail;
1488 }
1489 return nx;
1490
1491fail:
Weilong Chen9b7a7872013-12-24 09:43:46 +08001492 for (nx--; nx >= 0; nx--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 xfrm_state_put(xfrm[nx]);
1494 return error;
1495}
1496
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001497static int
David S. Millera6c2e612011-02-22 18:35:39 -08001498xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, const struct flowi *fl,
1499 struct xfrm_state **xfrm, unsigned short family)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001500{
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001501 struct xfrm_state *tp[XFRM_MAX_DEPTH];
1502 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001503 int cnx = 0;
1504 int error;
1505 int ret;
1506 int i;
1507
1508 for (i = 0; i < npols; i++) {
1509 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
1510 error = -ENOBUFS;
1511 goto fail;
1512 }
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001513
1514 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001515 if (ret < 0) {
1516 error = ret;
1517 goto fail;
1518 } else
1519 cnx += ret;
1520 }
1521
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001522 /* found states are sorted for outbound processing */
1523 if (npols > 1)
1524 xfrm_state_sort(xfrm, tpp, cnx, family);
1525
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001526 return cnx;
1527
1528 fail:
Weilong Chen9b7a7872013-12-24 09:43:46 +08001529 for (cnx--; cnx >= 0; cnx--)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001530 xfrm_state_put(tpp[cnx]);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001531 return error;
1532
1533}
1534
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535/* Check that the bundle accepts the flow and its components are
1536 * still valid.
1537 */
1538
David S. Miller05d84022011-02-22 17:47:10 -08001539static inline int xfrm_get_tos(const struct flowi *fl, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08001540{
1541 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1542 int tos;
1543
1544 if (!afinfo)
1545 return -EINVAL;
1546
1547 tos = afinfo->get_tos(fl);
1548
1549 xfrm_policy_put_afinfo(afinfo);
1550
1551 return tos;
1552}
1553
Timo Teräs80c802f2010-04-07 00:30:05 +00001554static struct flow_cache_object *xfrm_bundle_flo_get(struct flow_cache_object *flo)
1555{
1556 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1557 struct dst_entry *dst = &xdst->u.dst;
1558
1559 if (xdst->route == NULL) {
1560 /* Dummy bundle - if it has xfrms we were not
1561 * able to build bundle as template resolution failed.
1562 * It means we need to try again resolving. */
1563 if (xdst->num_xfrms > 0)
1564 return NULL;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001565 } else if (dst->flags & DST_XFRM_QUEUE) {
1566 return NULL;
Timo Teräs80c802f2010-04-07 00:30:05 +00001567 } else {
1568 /* Real bundle */
1569 if (stale_bundle(dst))
1570 return NULL;
1571 }
1572
1573 dst_hold(dst);
1574 return flo;
1575}
1576
1577static int xfrm_bundle_flo_check(struct flow_cache_object *flo)
1578{
1579 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1580 struct dst_entry *dst = &xdst->u.dst;
1581
1582 if (!xdst->route)
1583 return 0;
1584 if (stale_bundle(dst))
1585 return 0;
1586
1587 return 1;
1588}
1589
1590static void xfrm_bundle_flo_delete(struct flow_cache_object *flo)
1591{
1592 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1593 struct dst_entry *dst = &xdst->u.dst;
1594
1595 dst_free(dst);
1596}
1597
1598static const struct flow_cache_ops xfrm_bundle_fc_ops = {
1599 .get = xfrm_bundle_flo_get,
1600 .check = xfrm_bundle_flo_check,
1601 .delete = xfrm_bundle_flo_delete,
1602};
1603
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001604static inline struct xfrm_dst *xfrm_alloc_dst(struct net *net, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08001605{
1606 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001607 struct dst_ops *dst_ops;
Herbert Xu25ee3282007-12-11 09:32:34 -08001608 struct xfrm_dst *xdst;
1609
1610 if (!afinfo)
1611 return ERR_PTR(-EINVAL);
1612
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001613 switch (family) {
1614 case AF_INET:
1615 dst_ops = &net->xfrm.xfrm4_dst_ops;
1616 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001617#if IS_ENABLED(CONFIG_IPV6)
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001618 case AF_INET6:
1619 dst_ops = &net->xfrm.xfrm6_dst_ops;
1620 break;
1621#endif
1622 default:
1623 BUG();
1624 }
David S. Millerf5b0a872012-07-19 12:31:33 -07001625 xdst = dst_alloc(dst_ops, NULL, 0, DST_OBSOLETE_NONE, 0);
Herbert Xu25ee3282007-12-11 09:32:34 -08001626
Madalin Bucurd4cae562011-09-26 07:04:36 +00001627 if (likely(xdst)) {
Steffen Klassert141e3692012-07-05 23:39:34 +00001628 struct dst_entry *dst = &xdst->u.dst;
1629
1630 memset(dst + 1, 0, sizeof(*xdst) - sizeof(*dst));
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08001631 xdst->flo.ops = &xfrm_bundle_fc_ops;
Madalin Bucurd4cae562011-09-26 07:04:36 +00001632 } else
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08001633 xdst = ERR_PTR(-ENOBUFS);
Timo Teräs80c802f2010-04-07 00:30:05 +00001634
Madalin Bucurd4cae562011-09-26 07:04:36 +00001635 xfrm_policy_put_afinfo(afinfo);
1636
Herbert Xu25ee3282007-12-11 09:32:34 -08001637 return xdst;
1638}
1639
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001640static inline int xfrm_init_path(struct xfrm_dst *path, struct dst_entry *dst,
1641 int nfheader_len)
1642{
1643 struct xfrm_policy_afinfo *afinfo =
1644 xfrm_policy_get_afinfo(dst->ops->family);
1645 int err;
1646
1647 if (!afinfo)
1648 return -EINVAL;
1649
1650 err = afinfo->init_path(path, dst, nfheader_len);
1651
1652 xfrm_policy_put_afinfo(afinfo);
1653
1654 return err;
1655}
1656
Herbert Xu87c1e122010-03-02 02:51:56 +00001657static inline int xfrm_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
David S. Miller0c7b3ee2011-02-22 17:48:57 -08001658 const struct flowi *fl)
Herbert Xu25ee3282007-12-11 09:32:34 -08001659{
1660 struct xfrm_policy_afinfo *afinfo =
1661 xfrm_policy_get_afinfo(xdst->u.dst.ops->family);
1662 int err;
1663
1664 if (!afinfo)
1665 return -EINVAL;
1666
Herbert Xu87c1e122010-03-02 02:51:56 +00001667 err = afinfo->fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001668
1669 xfrm_policy_put_afinfo(afinfo);
1670
1671 return err;
1672}
1673
Timo Teräs80c802f2010-04-07 00:30:05 +00001674
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675/* Allocate chain of dst_entry's, attach known xfrm's, calculate
1676 * all the metrics... Shortly, bundle a bundle.
1677 */
1678
Herbert Xu25ee3282007-12-11 09:32:34 -08001679static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy,
1680 struct xfrm_state **xfrm, int nx,
David S. Miller98313ad2011-02-22 18:36:50 -08001681 const struct flowi *fl,
Herbert Xu25ee3282007-12-11 09:32:34 -08001682 struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683{
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001684 struct net *net = xp_net(policy);
Herbert Xu25ee3282007-12-11 09:32:34 -08001685 unsigned long now = jiffies;
1686 struct net_device *dev;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001687 struct xfrm_mode *inner_mode;
Herbert Xu25ee3282007-12-11 09:32:34 -08001688 struct dst_entry *dst_prev = NULL;
1689 struct dst_entry *dst0 = NULL;
1690 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 int err;
Herbert Xu25ee3282007-12-11 09:32:34 -08001692 int header_len = 0;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001693 int nfheader_len = 0;
Herbert Xu25ee3282007-12-11 09:32:34 -08001694 int trailer_len = 0;
1695 int tos;
1696 int family = policy->selector.family;
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +09001697 xfrm_address_t saddr, daddr;
1698
1699 xfrm_flowi_addr_get(fl, &saddr, &daddr, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001700
1701 tos = xfrm_get_tos(fl, family);
1702 err = tos;
1703 if (tos < 0)
1704 goto put_states;
1705
1706 dst_hold(dst);
1707
1708 for (; i < nx; i++) {
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001709 struct xfrm_dst *xdst = xfrm_alloc_dst(net, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001710 struct dst_entry *dst1 = &xdst->u.dst;
1711
1712 err = PTR_ERR(xdst);
1713 if (IS_ERR(xdst)) {
1714 dst_release(dst);
1715 goto put_states;
1716 }
1717
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001718 if (xfrm[i]->sel.family == AF_UNSPEC) {
1719 inner_mode = xfrm_ip2inner_mode(xfrm[i],
1720 xfrm_af2proto(family));
1721 if (!inner_mode) {
1722 err = -EAFNOSUPPORT;
1723 dst_release(dst);
1724 goto put_states;
1725 }
1726 } else
1727 inner_mode = xfrm[i]->inner_mode;
1728
Herbert Xu25ee3282007-12-11 09:32:34 -08001729 if (!dst_prev)
1730 dst0 = dst1;
1731 else {
1732 dst_prev->child = dst_clone(dst1);
1733 dst1->flags |= DST_NOHASH;
1734 }
1735
1736 xdst->route = dst;
David S. Millerdefb3512010-12-08 21:16:57 -08001737 dst_copy_metrics(dst1, dst);
Herbert Xu25ee3282007-12-11 09:32:34 -08001738
1739 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
1740 family = xfrm[i]->props.family;
David Ahern42a7b322015-08-10 16:58:11 -06001741 dst = xfrm_dst_lookup(xfrm[i], tos, fl->flowi_oif,
1742 &saddr, &daddr, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001743 err = PTR_ERR(dst);
1744 if (IS_ERR(dst))
1745 goto put_states;
1746 } else
1747 dst_hold(dst);
1748
1749 dst1->xfrm = xfrm[i];
Timo Teräs80c802f2010-04-07 00:30:05 +00001750 xdst->xfrm_genid = xfrm[i]->genid;
Herbert Xu25ee3282007-12-11 09:32:34 -08001751
David S. Millerf5b0a872012-07-19 12:31:33 -07001752 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
Herbert Xu25ee3282007-12-11 09:32:34 -08001753 dst1->flags |= DST_HOST;
1754 dst1->lastuse = now;
1755
1756 dst1->input = dst_discard;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001757 dst1->output = inner_mode->afinfo->output;
Herbert Xu25ee3282007-12-11 09:32:34 -08001758
1759 dst1->next = dst_prev;
1760 dst_prev = dst1;
1761
1762 header_len += xfrm[i]->props.header_len;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001763 if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT)
1764 nfheader_len += xfrm[i]->props.header_len;
Herbert Xu25ee3282007-12-11 09:32:34 -08001765 trailer_len += xfrm[i]->props.trailer_len;
1766 }
1767
1768 dst_prev->child = dst;
1769 dst0->path = dst;
1770
1771 err = -ENODEV;
1772 dev = dst->dev;
1773 if (!dev)
1774 goto free_dst;
1775
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001776 xfrm_init_path((struct xfrm_dst *)dst0, dst, nfheader_len);
Herbert Xu25ee3282007-12-11 09:32:34 -08001777 xfrm_init_pmtu(dst_prev);
1778
1779 for (dst_prev = dst0; dst_prev != dst; dst_prev = dst_prev->child) {
1780 struct xfrm_dst *xdst = (struct xfrm_dst *)dst_prev;
1781
Herbert Xu87c1e122010-03-02 02:51:56 +00001782 err = xfrm_fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001783 if (err)
1784 goto free_dst;
1785
1786 dst_prev->header_len = header_len;
1787 dst_prev->trailer_len = trailer_len;
1788 header_len -= xdst->u.dst.xfrm->props.header_len;
1789 trailer_len -= xdst->u.dst.xfrm->props.trailer_len;
1790 }
1791
1792out:
1793 return dst0;
1794
1795put_states:
1796 for (; i < nx; i++)
1797 xfrm_state_put(xfrm[i]);
1798free_dst:
1799 if (dst0)
1800 dst_free(dst0);
1801 dst0 = ERR_PTR(err);
1802 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803}
1804
Ying Xueda7c2242014-01-08 10:26:39 +08001805#ifdef CONFIG_XFRM_SUB_POLICY
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001806static int xfrm_dst_alloc_copy(void **target, const void *src, int size)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001807{
1808 if (!*target) {
1809 *target = kmalloc(size, GFP_ATOMIC);
1810 if (!*target)
1811 return -ENOMEM;
1812 }
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001813
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001814 memcpy(*target, src, size);
1815 return 0;
1816}
Ying Xueda7c2242014-01-08 10:26:39 +08001817#endif
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001818
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001819static int xfrm_dst_update_parent(struct dst_entry *dst,
1820 const struct xfrm_selector *sel)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001821{
1822#ifdef CONFIG_XFRM_SUB_POLICY
1823 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1824 return xfrm_dst_alloc_copy((void **)&(xdst->partner),
1825 sel, sizeof(*sel));
1826#else
1827 return 0;
1828#endif
1829}
1830
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001831static int xfrm_dst_update_origin(struct dst_entry *dst,
1832 const struct flowi *fl)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001833{
1834#ifdef CONFIG_XFRM_SUB_POLICY
1835 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1836 return xfrm_dst_alloc_copy((void **)&(xdst->origin), fl, sizeof(*fl));
1837#else
1838 return 0;
1839#endif
1840}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
David S. Miller73ff93c2011-02-22 18:33:42 -08001842static int xfrm_expand_policies(const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001843 struct xfrm_policy **pols,
1844 int *num_pols, int *num_xfrms)
1845{
1846 int i;
1847
1848 if (*num_pols == 0 || !pols[0]) {
1849 *num_pols = 0;
1850 *num_xfrms = 0;
1851 return 0;
1852 }
1853 if (IS_ERR(pols[0]))
1854 return PTR_ERR(pols[0]);
1855
1856 *num_xfrms = pols[0]->xfrm_nr;
1857
1858#ifdef CONFIG_XFRM_SUB_POLICY
1859 if (pols[0] && pols[0]->action == XFRM_POLICY_ALLOW &&
1860 pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1861 pols[1] = xfrm_policy_lookup_bytype(xp_net(pols[0]),
1862 XFRM_POLICY_TYPE_MAIN,
1863 fl, family,
1864 XFRM_POLICY_OUT);
1865 if (pols[1]) {
1866 if (IS_ERR(pols[1])) {
1867 xfrm_pols_put(pols, *num_pols);
1868 return PTR_ERR(pols[1]);
1869 }
Weilong Chen02d08922013-12-24 09:43:48 +08001870 (*num_pols)++;
Timo Teräs80c802f2010-04-07 00:30:05 +00001871 (*num_xfrms) += pols[1]->xfrm_nr;
1872 }
1873 }
1874#endif
1875 for (i = 0; i < *num_pols; i++) {
1876 if (pols[i]->action != XFRM_POLICY_ALLOW) {
1877 *num_xfrms = -1;
1878 break;
1879 }
1880 }
1881
1882 return 0;
1883
1884}
1885
1886static struct xfrm_dst *
1887xfrm_resolve_and_create_bundle(struct xfrm_policy **pols, int num_pols,
David S. Miller4ca2e682011-02-22 18:38:51 -08001888 const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001889 struct dst_entry *dst_orig)
1890{
1891 struct net *net = xp_net(pols[0]);
1892 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
1893 struct dst_entry *dst;
1894 struct xfrm_dst *xdst;
1895 int err;
1896
1897 /* Try to instantiate a bundle */
1898 err = xfrm_tmpl_resolve(pols, num_pols, fl, xfrm, family);
Timo Teräsd809ec82010-07-12 21:29:42 +00001899 if (err <= 0) {
1900 if (err != 0 && err != -EAGAIN)
Timo Teräs80c802f2010-04-07 00:30:05 +00001901 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
1902 return ERR_PTR(err);
1903 }
1904
1905 dst = xfrm_bundle_create(pols[0], xfrm, err, fl, dst_orig);
1906 if (IS_ERR(dst)) {
1907 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLEGENERROR);
1908 return ERR_CAST(dst);
1909 }
1910
1911 xdst = (struct xfrm_dst *)dst;
1912 xdst->num_xfrms = err;
1913 if (num_pols > 1)
1914 err = xfrm_dst_update_parent(dst, &pols[1]->selector);
1915 else
1916 err = xfrm_dst_update_origin(dst, fl);
1917 if (unlikely(err)) {
1918 dst_free(dst);
1919 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLECHECKERROR);
1920 return ERR_PTR(err);
1921 }
1922
1923 xdst->num_pols = num_pols;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08001924 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00001925 xdst->policy_genid = atomic_read(&pols[0]->genid);
1926
1927 return xdst;
1928}
1929
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001930static void xfrm_policy_queue_process(unsigned long arg)
1931{
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001932 struct sk_buff *skb;
1933 struct sock *sk;
1934 struct dst_entry *dst;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001935 struct xfrm_policy *pol = (struct xfrm_policy *)arg;
Eric W. Biederman3f5312a2015-10-07 16:48:34 -05001936 struct net *net = xp_net(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001937 struct xfrm_policy_queue *pq = &pol->polq;
1938 struct flowi fl;
1939 struct sk_buff_head list;
1940
1941 spin_lock(&pq->hold_queue.lock);
1942 skb = skb_peek(&pq->hold_queue);
Steffen Klassert2bb53e22013-10-08 10:49:51 +02001943 if (!skb) {
1944 spin_unlock(&pq->hold_queue.lock);
1945 goto out;
1946 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001947 dst = skb_dst(skb);
1948 sk = skb->sk;
1949 xfrm_decode_session(skb, &fl, dst->ops->family);
1950 spin_unlock(&pq->hold_queue.lock);
1951
1952 dst_hold(dst->path);
Eric W. Biederman3f5312a2015-10-07 16:48:34 -05001953 dst = xfrm_lookup(net, dst->path, &fl, sk, 0);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001954 if (IS_ERR(dst))
1955 goto purge_queue;
1956
1957 if (dst->flags & DST_XFRM_QUEUE) {
1958 dst_release(dst);
1959
1960 if (pq->timeout >= XFRM_QUEUE_TMO_MAX)
1961 goto purge_queue;
1962
1963 pq->timeout = pq->timeout << 1;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001964 if (!mod_timer(&pq->hold_timer, jiffies + pq->timeout))
1965 xfrm_pol_hold(pol);
1966 goto out;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001967 }
1968
1969 dst_release(dst);
1970
1971 __skb_queue_head_init(&list);
1972
1973 spin_lock(&pq->hold_queue.lock);
1974 pq->timeout = 0;
1975 skb_queue_splice_init(&pq->hold_queue, &list);
1976 spin_unlock(&pq->hold_queue.lock);
1977
1978 while (!skb_queue_empty(&list)) {
1979 skb = __skb_dequeue(&list);
1980
1981 xfrm_decode_session(skb, &fl, skb_dst(skb)->ops->family);
1982 dst_hold(skb_dst(skb)->path);
Eric W. Biederman3f5312a2015-10-07 16:48:34 -05001983 dst = xfrm_lookup(net, skb_dst(skb)->path, &fl, skb->sk, 0);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001984 if (IS_ERR(dst)) {
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001985 kfree_skb(skb);
1986 continue;
1987 }
1988
1989 nf_reset(skb);
1990 skb_dst_drop(skb);
1991 skb_dst_set(skb, dst);
1992
Eric W. Biederman13206b62015-10-07 16:48:35 -05001993 dst_output(net, skb->sk, skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001994 }
1995
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001996out:
1997 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001998 return;
1999
2000purge_queue:
2001 pq->timeout = 0;
Li RongQing1ee5e662015-04-22 15:51:16 +08002002 skb_queue_purge(&pq->hold_queue);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002003 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002004}
2005
Eric W. Biedermanede20592015-10-07 16:48:47 -05002006static int xdst_queue_output(struct net *net, struct sock *sk, struct sk_buff *skb)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002007{
2008 unsigned long sched_next;
2009 struct dst_entry *dst = skb_dst(skb);
2010 struct xfrm_dst *xdst = (struct xfrm_dst *) dst;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002011 struct xfrm_policy *pol = xdst->pols[0];
2012 struct xfrm_policy_queue *pq = &pol->polq;
Steffen Klassert4d53eff2013-10-16 13:42:46 +02002013
Eric Dumazet39bb5e62014-10-30 10:32:34 -07002014 if (unlikely(skb_fclone_busy(sk, skb))) {
Steffen Klassert4d53eff2013-10-16 13:42:46 +02002015 kfree_skb(skb);
2016 return 0;
2017 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002018
2019 if (pq->hold_queue.qlen > XFRM_MAX_QUEUE_LEN) {
2020 kfree_skb(skb);
2021 return -EAGAIN;
2022 }
2023
2024 skb_dst_force(skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002025
2026 spin_lock_bh(&pq->hold_queue.lock);
2027
2028 if (!pq->timeout)
2029 pq->timeout = XFRM_QUEUE_TMO_MIN;
2030
2031 sched_next = jiffies + pq->timeout;
2032
2033 if (del_timer(&pq->hold_timer)) {
2034 if (time_before(pq->hold_timer.expires, sched_next))
2035 sched_next = pq->hold_timer.expires;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002036 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002037 }
2038
2039 __skb_queue_tail(&pq->hold_queue, skb);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02002040 if (!mod_timer(&pq->hold_timer, sched_next))
2041 xfrm_pol_hold(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002042
2043 spin_unlock_bh(&pq->hold_queue.lock);
2044
2045 return 0;
2046}
2047
2048static struct xfrm_dst *xfrm_create_dummy_bundle(struct net *net,
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002049 struct xfrm_flo *xflo,
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002050 const struct flowi *fl,
2051 int num_xfrms,
2052 u16 family)
2053{
2054 int err;
2055 struct net_device *dev;
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002056 struct dst_entry *dst;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002057 struct dst_entry *dst1;
2058 struct xfrm_dst *xdst;
2059
2060 xdst = xfrm_alloc_dst(net, family);
2061 if (IS_ERR(xdst))
2062 return xdst;
2063
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002064 if (!(xflo->flags & XFRM_LOOKUP_QUEUE) ||
2065 net->xfrm.sysctl_larval_drop ||
2066 num_xfrms <= 0)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002067 return xdst;
2068
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002069 dst = xflo->dst_orig;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002070 dst1 = &xdst->u.dst;
2071 dst_hold(dst);
2072 xdst->route = dst;
2073
2074 dst_copy_metrics(dst1, dst);
2075
2076 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
2077 dst1->flags |= DST_HOST | DST_XFRM_QUEUE;
2078 dst1->lastuse = jiffies;
2079
2080 dst1->input = dst_discard;
2081 dst1->output = xdst_queue_output;
2082
2083 dst_hold(dst);
2084 dst1->child = dst;
2085 dst1->path = dst;
2086
2087 xfrm_init_path((struct xfrm_dst *)dst1, dst, 0);
2088
2089 err = -ENODEV;
2090 dev = dst->dev;
2091 if (!dev)
2092 goto free_dst;
2093
2094 err = xfrm_fill_dst(xdst, dev, fl);
2095 if (err)
2096 goto free_dst;
2097
2098out:
2099 return xdst;
2100
2101free_dst:
2102 dst_release(dst1);
2103 xdst = ERR_PTR(err);
2104 goto out;
2105}
2106
Timo Teräs80c802f2010-04-07 00:30:05 +00002107static struct flow_cache_object *
David S. Millerdee9f4b2011-02-22 18:44:31 -08002108xfrm_bundle_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir,
Timo Teräs80c802f2010-04-07 00:30:05 +00002109 struct flow_cache_object *oldflo, void *ctx)
2110{
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002111 struct xfrm_flo *xflo = (struct xfrm_flo *)ctx;
Timo Teräs80c802f2010-04-07 00:30:05 +00002112 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2113 struct xfrm_dst *xdst, *new_xdst;
2114 int num_pols = 0, num_xfrms = 0, i, err, pol_dead;
2115
2116 /* Check if the policies from old bundle are usable */
2117 xdst = NULL;
2118 if (oldflo) {
2119 xdst = container_of(oldflo, struct xfrm_dst, flo);
2120 num_pols = xdst->num_pols;
2121 num_xfrms = xdst->num_xfrms;
2122 pol_dead = 0;
2123 for (i = 0; i < num_pols; i++) {
2124 pols[i] = xdst->pols[i];
2125 pol_dead |= pols[i]->walk.dead;
2126 }
2127 if (pol_dead) {
2128 dst_free(&xdst->u.dst);
2129 xdst = NULL;
2130 num_pols = 0;
2131 num_xfrms = 0;
2132 oldflo = NULL;
2133 }
2134 }
2135
2136 /* Resolve policies to use if we couldn't get them from
2137 * previous cache entry */
2138 if (xdst == NULL) {
2139 num_pols = 1;
Baker Zhangb5fb82c2013-03-19 04:24:30 +00002140 pols[0] = __xfrm_policy_lookup(net, fl, family,
2141 flow_to_policy_dir(dir));
Timo Teräs80c802f2010-04-07 00:30:05 +00002142 err = xfrm_expand_policies(fl, family, pols,
2143 &num_pols, &num_xfrms);
2144 if (err < 0)
2145 goto inc_error;
2146 if (num_pols == 0)
2147 return NULL;
2148 if (num_xfrms <= 0)
2149 goto make_dummy_bundle;
2150 }
2151
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002152 new_xdst = xfrm_resolve_and_create_bundle(pols, num_pols, fl, family,
2153 xflo->dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002154 if (IS_ERR(new_xdst)) {
2155 err = PTR_ERR(new_xdst);
2156 if (err != -EAGAIN)
2157 goto error;
2158 if (oldflo == NULL)
2159 goto make_dummy_bundle;
2160 dst_hold(&xdst->u.dst);
2161 return oldflo;
Timo Teräsd809ec82010-07-12 21:29:42 +00002162 } else if (new_xdst == NULL) {
2163 num_xfrms = 0;
2164 if (oldflo == NULL)
2165 goto make_dummy_bundle;
2166 xdst->num_xfrms = 0;
2167 dst_hold(&xdst->u.dst);
2168 return oldflo;
Timo Teräs80c802f2010-04-07 00:30:05 +00002169 }
2170
2171 /* Kill the previous bundle */
2172 if (xdst) {
2173 /* The policies were stolen for newly generated bundle */
2174 xdst->num_pols = 0;
2175 dst_free(&xdst->u.dst);
2176 }
2177
2178 /* Flow cache does not have reference, it dst_free()'s,
2179 * but we do need to return one reference for original caller */
2180 dst_hold(&new_xdst->u.dst);
2181 return &new_xdst->flo;
2182
2183make_dummy_bundle:
2184 /* We found policies, but there's no bundles to instantiate:
2185 * either because the policy blocks, has no transformations or
2186 * we could not build template (no xfrm_states).*/
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002187 xdst = xfrm_create_dummy_bundle(net, xflo, fl, num_xfrms, family);
Timo Teräs80c802f2010-04-07 00:30:05 +00002188 if (IS_ERR(xdst)) {
2189 xfrm_pols_put(pols, num_pols);
2190 return ERR_CAST(xdst);
2191 }
2192 xdst->num_pols = num_pols;
2193 xdst->num_xfrms = num_xfrms;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08002194 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002195
2196 dst_hold(&xdst->u.dst);
2197 return &xdst->flo;
2198
2199inc_error:
2200 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
2201error:
2202 if (xdst != NULL)
2203 dst_free(&xdst->u.dst);
2204 else
2205 xfrm_pols_put(pols, num_pols);
2206 return ERR_PTR(err);
2207}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
David S. Miller2774c132011-03-01 14:59:04 -08002209static struct dst_entry *make_blackhole(struct net *net, u16 family,
2210 struct dst_entry *dst_orig)
2211{
2212 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
2213 struct dst_entry *ret;
2214
2215 if (!afinfo) {
2216 dst_release(dst_orig);
Li RongQing433a1952012-09-17 22:40:10 +00002217 return ERR_PTR(-EINVAL);
David S. Miller2774c132011-03-01 14:59:04 -08002218 } else {
2219 ret = afinfo->blackhole_route(net, dst_orig);
2220 }
2221 xfrm_policy_put_afinfo(afinfo);
2222
2223 return ret;
2224}
2225
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226/* Main function: finds/creates a bundle for given flow.
2227 *
2228 * At the moment we eat a raw IP route. Mostly to speed up lookups
2229 * on interfaces with disabled IPsec.
2230 */
David S. Miller452edd52011-03-02 13:27:41 -08002231struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
2232 const struct flowi *fl,
Eric Dumazet6f9c9612015-09-25 07:39:10 -07002233 const struct sock *sk, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234{
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002235 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
Timo Teräs80c802f2010-04-07 00:30:05 +00002236 struct flow_cache_object *flo;
2237 struct xfrm_dst *xdst;
David S. Miller452edd52011-03-02 13:27:41 -08002238 struct dst_entry *dst, *route;
Timo Teräs80c802f2010-04-07 00:30:05 +00002239 u16 family = dst_orig->ops->family;
Trent Jaegerdf718372005-12-13 23:12:27 -08002240 u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
Changli Gao4b021622010-04-27 21:20:22 +00002241 int i, err, num_pols, num_xfrms = 0, drop_pols = 0;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002242
Timo Teräs80c802f2010-04-07 00:30:05 +00002243 dst = NULL;
2244 xdst = NULL;
2245 route = NULL;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002246
Eric Dumazetbd5eb352015-12-07 08:53:17 -08002247 sk = sk_const_to_full_sk(sk);
Thomas Graff7944fb2007-08-25 13:46:55 -07002248 if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002249 num_pols = 1;
2250 pols[0] = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
2251 err = xfrm_expand_policies(fl, family, pols,
2252 &num_pols, &num_xfrms);
2253 if (err < 0)
Herbert Xu75b8c132007-12-11 04:38:08 -08002254 goto dropdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002255
2256 if (num_pols) {
2257 if (num_xfrms <= 0) {
2258 drop_pols = num_pols;
2259 goto no_transform;
2260 }
2261
2262 xdst = xfrm_resolve_and_create_bundle(
2263 pols, num_pols, fl,
2264 family, dst_orig);
2265 if (IS_ERR(xdst)) {
2266 xfrm_pols_put(pols, num_pols);
2267 err = PTR_ERR(xdst);
2268 goto dropdst;
Timo Teräsd809ec82010-07-12 21:29:42 +00002269 } else if (xdst == NULL) {
2270 num_xfrms = 0;
2271 drop_pols = num_pols;
2272 goto no_transform;
Timo Teräs80c802f2010-04-07 00:30:05 +00002273 }
2274
Steffen Klassertb7eea452014-06-18 12:34:21 +02002275 dst_hold(&xdst->u.dst);
2276 xdst->u.dst.flags |= DST_NOCACHE;
Timo Teräs80c802f2010-04-07 00:30:05 +00002277 route = xdst->route;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002278 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280
Timo Teräs80c802f2010-04-07 00:30:05 +00002281 if (xdst == NULL) {
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002282 struct xfrm_flo xflo;
2283
2284 xflo.dst_orig = dst_orig;
2285 xflo.flags = flags;
2286
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 /* To accelerate a bit... */
David S. Miller2518c7c2006-08-24 04:45:07 -07002288 if ((dst_orig->flags & DST_NOXFRM) ||
Alexey Dobriyan52479b62008-11-25 17:35:18 -08002289 !net->xfrm.policy_count[XFRM_POLICY_OUT])
Herbert Xu8b7817f2007-12-12 10:44:43 -08002290 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
Timo Teräs80c802f2010-04-07 00:30:05 +00002292 flo = flow_cache_lookup(net, fl, family, dir,
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002293 xfrm_bundle_lookup, &xflo);
Timo Teräs80c802f2010-04-07 00:30:05 +00002294 if (flo == NULL)
2295 goto nopol;
Timo Teräsfe1a5f02010-04-07 00:30:04 +00002296 if (IS_ERR(flo)) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002297 err = PTR_ERR(flo);
Herbert Xu75b8c132007-12-11 04:38:08 -08002298 goto dropdst;
Masahide NAKAMURAd66e37a2008-01-07 21:46:15 -08002299 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002300 xdst = container_of(flo, struct xfrm_dst, flo);
2301
2302 num_pols = xdst->num_pols;
2303 num_xfrms = xdst->num_xfrms;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08002304 memcpy(pols, xdst->pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002305 route = xdst->route;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 }
2307
Timo Teräs80c802f2010-04-07 00:30:05 +00002308 dst = &xdst->u.dst;
2309 if (route == NULL && num_xfrms > 0) {
2310 /* The only case when xfrm_bundle_lookup() returns a
2311 * bundle with null route, is when the template could
2312 * not be resolved. It means policies are there, but
2313 * bundle could not be created, since we don't yet
2314 * have the xfrm_state's. We need to wait for KM to
2315 * negotiate new SA's or bail out with error.*/
2316 if (net->xfrm.sysctl_larval_drop) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002317 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
huaibin Wangac37e252015-02-11 18:10:36 +01002318 err = -EREMOTE;
2319 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002320 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002321
Steffen Klassert5b8ef342013-08-27 13:43:30 +02002322 err = -EAGAIN;
Timo Teräs80c802f2010-04-07 00:30:05 +00002323
2324 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
2325 goto error;
2326 }
2327
2328no_transform:
2329 if (num_pols == 0)
Herbert Xu8b7817f2007-12-12 10:44:43 -08002330 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
Timo Teräs80c802f2010-04-07 00:30:05 +00002332 if ((flags & XFRM_LOOKUP_ICMP) &&
2333 !(pols[0]->flags & XFRM_POLICY_ICMP)) {
2334 err = -ENOENT;
Herbert Xu8b7817f2007-12-12 10:44:43 -08002335 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002336 }
Herbert Xu8b7817f2007-12-12 10:44:43 -08002337
Timo Teräs80c802f2010-04-07 00:30:05 +00002338 for (i = 0; i < num_pols; i++)
2339 pols[i]->curlft.use_time = get_seconds();
Herbert Xu8b7817f2007-12-12 10:44:43 -08002340
Timo Teräs80c802f2010-04-07 00:30:05 +00002341 if (num_xfrms < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 /* Prohibit the flow */
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002343 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLBLOCK);
Patrick McHardye104411b2005-09-08 15:11:55 -07002344 err = -EPERM;
2345 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002346 } else if (num_xfrms > 0) {
2347 /* Flow transformed */
Timo Teräs80c802f2010-04-07 00:30:05 +00002348 dst_release(dst_orig);
2349 } else {
2350 /* Flow passes untransformed */
2351 dst_release(dst);
David S. Miller452edd52011-03-02 13:27:41 -08002352 dst = dst_orig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002354ok:
2355 xfrm_pols_put(pols, drop_pols);
Gao feng0c183372012-05-26 01:30:53 +00002356 if (dst && dst->xfrm &&
2357 dst->xfrm->props.mode == XFRM_MODE_TUNNEL)
2358 dst->flags |= DST_XFRM_TUNNEL;
David S. Miller452edd52011-03-02 13:27:41 -08002359 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
Timo Teräs80c802f2010-04-07 00:30:05 +00002361nopol:
David S. Miller452edd52011-03-02 13:27:41 -08002362 if (!(flags & XFRM_LOOKUP_ICMP)) {
2363 dst = dst_orig;
Timo Teräs80c802f2010-04-07 00:30:05 +00002364 goto ok;
David S. Miller452edd52011-03-02 13:27:41 -08002365 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002366 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367error:
Timo Teräs80c802f2010-04-07 00:30:05 +00002368 dst_release(dst);
Herbert Xu75b8c132007-12-11 04:38:08 -08002369dropdst:
huaibin Wangac37e252015-02-11 18:10:36 +01002370 if (!(flags & XFRM_LOOKUP_KEEP_DST_REF))
2371 dst_release(dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002372 xfrm_pols_put(pols, drop_pols);
David S. Miller452edd52011-03-02 13:27:41 -08002373 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374}
2375EXPORT_SYMBOL(xfrm_lookup);
2376
Steffen Klassertf92ee612014-09-16 10:08:40 +02002377/* Callers of xfrm_lookup_route() must ensure a call to dst_output().
2378 * Otherwise we may send out blackholed packets.
2379 */
2380struct dst_entry *xfrm_lookup_route(struct net *net, struct dst_entry *dst_orig,
2381 const struct flowi *fl,
Eric Dumazet6f9c9612015-09-25 07:39:10 -07002382 const struct sock *sk, int flags)
Steffen Klassertf92ee612014-09-16 10:08:40 +02002383{
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002384 struct dst_entry *dst = xfrm_lookup(net, dst_orig, fl, sk,
huaibin Wangac37e252015-02-11 18:10:36 +01002385 flags | XFRM_LOOKUP_QUEUE |
2386 XFRM_LOOKUP_KEEP_DST_REF);
Steffen Klassertf92ee612014-09-16 10:08:40 +02002387
2388 if (IS_ERR(dst) && PTR_ERR(dst) == -EREMOTE)
2389 return make_blackhole(net, dst_orig->ops->family, dst_orig);
2390
2391 return dst;
2392}
2393EXPORT_SYMBOL(xfrm_lookup_route);
2394
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002395static inline int
David S. Miller8f029de2011-02-22 17:59:59 -08002396xfrm_secpath_reject(int idx, struct sk_buff *skb, const struct flowi *fl)
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002397{
2398 struct xfrm_state *x;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002399
2400 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
2401 return 0;
2402 x = skb->sp->xvec[idx];
2403 if (!x->type->reject)
2404 return 0;
Herbert Xu1ecafed2007-10-09 13:24:07 -07002405 return x->type->reject(x, skb, fl);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002406}
2407
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408/* When skb is transformed back to its "native" form, we have to
2409 * check policy restrictions. At the moment we make this in maximally
2410 * stupid way. Shame on me. :-) Of course, connected sockets must
2411 * have policy cached at them.
2412 */
2413
2414static inline int
David S. Miller7db454b2011-02-24 01:43:01 -05002415xfrm_state_ok(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 unsigned short family)
2417{
2418 if (xfrm_state_kern(x))
Kazunori MIYAZAWA928ba412007-02-13 12:57:16 -08002419 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 return x->id.proto == tmpl->id.proto &&
2421 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
2422 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
2423 x->props.mode == tmpl->mode &&
Herbert Xuc5d18e92008-04-22 00:46:42 -07002424 (tmpl->allalgs || (tmpl->aalgos & (1<<x->props.aalgo)) ||
Masahide NAKAMURAf3bd4842006-08-23 18:00:48 -07002425 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002426 !(x->props.mode != XFRM_MODE_TRANSPORT &&
2427 xfrm_state_addr_cmp(tmpl, x, family));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428}
2429
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002430/*
2431 * 0 or more than 0 is returned when validation is succeeded (either bypass
2432 * because of optional transport mode, or next index of the mathced secpath
2433 * state with the template.
2434 * -1 is returned when no matching template is found.
2435 * Otherwise "-2 - errored_index" is returned.
2436 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437static inline int
David S. Miller22cccb72011-02-24 01:43:33 -05002438xfrm_policy_ok(const struct xfrm_tmpl *tmpl, const struct sec_path *sp, int start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 unsigned short family)
2440{
2441 int idx = start;
2442
2443 if (tmpl->optional) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002444 if (tmpl->mode == XFRM_MODE_TRANSPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 return start;
2446 } else
2447 start = -1;
2448 for (; idx < sp->len; idx++) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002449 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 return ++idx;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002451 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
2452 if (start == -1)
2453 start = -2-idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 break;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 }
2457 return start;
2458}
2459
Herbert Xud5422ef2007-12-12 10:44:16 -08002460int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
2461 unsigned int family, int reverse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462{
2463 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002464 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465
2466 if (unlikely(afinfo == NULL))
2467 return -EAFNOSUPPORT;
2468
Herbert Xud5422ef2007-12-12 10:44:16 -08002469 afinfo->decode_session(skb, fl, reverse);
David S. Miller1d28f422011-03-12 00:29:39 -05002470 err = security_xfrm_decode_session(skb, &fl->flowi_secid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 xfrm_policy_put_afinfo(afinfo);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002472 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473}
Herbert Xud5422ef2007-12-12 10:44:16 -08002474EXPORT_SYMBOL(__xfrm_decode_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
David S. Miller9a7386e2011-02-24 01:44:12 -05002476static inline int secpath_has_nontransport(const struct sec_path *sp, int k, int *idxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477{
2478 for (; k < sp->len; k++) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002479 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002480 *idxp = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 return 1;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 }
2484
2485 return 0;
2486}
2487
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002488int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 unsigned short family)
2490{
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002491 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 struct xfrm_policy *pol;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002493 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2494 int npols = 0;
2495 int xfrm_nr;
2496 int pi;
Herbert Xud5422ef2007-12-12 10:44:16 -08002497 int reverse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 struct flowi fl;
Herbert Xud5422ef2007-12-12 10:44:16 -08002499 u8 fl_dir;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002500 int xerr_idx = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501
Herbert Xud5422ef2007-12-12 10:44:16 -08002502 reverse = dir & ~XFRM_POLICY_MASK;
2503 dir &= XFRM_POLICY_MASK;
2504 fl_dir = policy_to_flow_dir(dir);
2505
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002506 if (__xfrm_decode_session(skb, &fl, family, reverse) < 0) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002507 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002509 }
2510
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -08002511 nf_nat_decode_session(skb, &fl, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512
2513 /* First, check used SA against their selectors. */
2514 if (skb->sp) {
2515 int i;
2516
Weilong Chen9b7a7872013-12-24 09:43:46 +08002517 for (i = skb->sp->len-1; i >= 0; i--) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002518 struct xfrm_state *x = skb->sp->xvec[i];
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002519 if (!xfrm_selector_match(&x->sel, &fl, family)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002520 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 }
2524 }
2525
2526 pol = NULL;
Eric Dumazetbd5eb352015-12-07 08:53:17 -08002527 sk = sk_to_full_sk(sk);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002528 if (sk && sk->sk_policy[dir]) {
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002529 pol = xfrm_sk_policy_lookup(sk, dir, &fl);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002530 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002531 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002532 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002533 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002534 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535
Timo Teräsfe1a5f02010-04-07 00:30:04 +00002536 if (!pol) {
2537 struct flow_cache_object *flo;
2538
2539 flo = flow_cache_lookup(net, &fl, family, fl_dir,
2540 xfrm_policy_lookup, NULL);
2541 if (IS_ERR_OR_NULL(flo))
2542 pol = ERR_CAST(flo);
2543 else
2544 pol = container_of(flo, struct xfrm_policy, flo);
2545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002547 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002548 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002549 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002550 }
James Morris134b0fc2006-10-05 15:42:27 -05002551
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002552 if (!pol) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002553 if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002554 xfrm_secpath_reject(xerr_idx, skb, &fl);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002555 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOPOLS);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002556 return 0;
2557 }
2558 return 1;
2559 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560
James Morris9d729f72007-03-04 16:12:44 -08002561 pol->curlft.use_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002563 pols[0] = pol;
Weilong Chen02d08922013-12-24 09:43:48 +08002564 npols++;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002565#ifdef CONFIG_XFRM_SUB_POLICY
2566 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002567 pols[1] = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002568 &fl, family,
2569 XFRM_POLICY_IN);
2570 if (pols[1]) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002571 if (IS_ERR(pols[1])) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002572 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002573 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002574 }
James Morris9d729f72007-03-04 16:12:44 -08002575 pols[1]->curlft.use_time = get_seconds();
Weilong Chen02d08922013-12-24 09:43:48 +08002576 npols++;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002577 }
2578 }
2579#endif
2580
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 if (pol->action == XFRM_POLICY_ALLOW) {
2582 struct sec_path *sp;
2583 static struct sec_path dummy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002584 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002585 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002586 struct xfrm_tmpl **tpp = tp;
2587 int ti = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 int i, k;
2589
2590 if ((sp = skb->sp) == NULL)
2591 sp = &dummy;
2592
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002593 for (pi = 0; pi < npols; pi++) {
2594 if (pols[pi] != pol &&
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002595 pols[pi]->action != XFRM_POLICY_ALLOW) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002596 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002597 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002598 }
2599 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002600 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002601 goto reject_error;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002602 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002603 for (i = 0; i < pols[pi]->xfrm_nr; i++)
2604 tpp[ti++] = &pols[pi]->xfrm_vec[i];
2605 }
2606 xfrm_nr = ti;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002607 if (npols > 1) {
Fan Du283bc9f2013-11-07 17:47:50 +08002608 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family, net);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002609 tpp = stp;
2610 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002611
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 /* For each tunnel xfrm, find the first matching tmpl.
2613 * For each tmpl before that, find corresponding xfrm.
2614 * Order is _important_. Later we will implement
2615 * some barriers, but at the moment barriers
2616 * are implied between each two transformations.
2617 */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002618 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
2619 k = xfrm_policy_ok(tpp[i], sp, k, family);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002620 if (k < 0) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002621 if (k < -1)
2622 /* "-2 - errored_index" returned */
2623 xerr_idx = -(2+k);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002624 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 goto reject;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 }
2628
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002629 if (secpath_has_nontransport(sp, k, &xerr_idx)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002630 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002634 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 return 1;
2636 }
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002637 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638
2639reject:
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002640 xfrm_secpath_reject(xerr_idx, skb, &fl);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002641reject_error:
2642 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 return 0;
2644}
2645EXPORT_SYMBOL(__xfrm_policy_check);
2646
2647int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
2648{
Alexey Dobriyan99a66652008-11-25 17:36:13 -08002649 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 struct flowi fl;
Eric Dumazetadf30902009-06-02 05:19:30 +00002651 struct dst_entry *dst;
Eric Dumazet73137142011-03-15 15:26:43 -07002652 int res = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002654 if (xfrm_decode_session(skb, &fl, family) < 0) {
jamal72032fd2010-02-18 03:35:07 +00002655 XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658
Eric Dumazetfafeeb62010-06-01 10:04:49 +00002659 skb_dst_force(skb);
Eric Dumazetadf30902009-06-02 05:19:30 +00002660
Steffen Klassertb8c203b2014-09-16 10:08:49 +02002661 dst = xfrm_lookup(net, skb_dst(skb), &fl, NULL, XFRM_LOOKUP_QUEUE);
David S. Miller452edd52011-03-02 13:27:41 -08002662 if (IS_ERR(dst)) {
Eric Dumazet73137142011-03-15 15:26:43 -07002663 res = 0;
David S. Miller452edd52011-03-02 13:27:41 -08002664 dst = NULL;
2665 }
Eric Dumazetadf30902009-06-02 05:19:30 +00002666 skb_dst_set(skb, dst);
2667 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668}
2669EXPORT_SYMBOL(__xfrm_route_forward);
2670
David S. Millerd49c73c2006-08-13 18:55:53 -07002671/* Optimize later using cookies and generation ids. */
2672
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
2674{
David S. Millerd49c73c2006-08-13 18:55:53 -07002675 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
David S. Millerf5b0a872012-07-19 12:31:33 -07002676 * to DST_OBSOLETE_FORCE_CHK to force all XFRM destinations to
2677 * get validated by dst_ops->check on every use. We do this
2678 * because when a normal route referenced by an XFRM dst is
2679 * obsoleted we do not go looking around for all parent
2680 * referencing XFRM dsts so that we can invalidate them. It
2681 * is just too much work. Instead we make the checks here on
2682 * every use. For example:
David S. Millerd49c73c2006-08-13 18:55:53 -07002683 *
2684 * XFRM dst A --> IPv4 dst X
2685 *
2686 * X is the "xdst->route" of A (X is also the "dst->path" of A
2687 * in this example). If X is marked obsolete, "A" will not
2688 * notice. That's what we are validating here via the
2689 * stale_bundle() check.
2690 *
2691 * When a policy's bundle is pruned, we dst_free() the XFRM
David S. Millerf5b0a872012-07-19 12:31:33 -07002692 * dst which causes it's ->obsolete field to be set to
2693 * DST_OBSOLETE_DEAD. If an XFRM dst has been pruned like
2694 * this, we want to force a new route lookup.
David S. Miller399c1802005-12-19 14:23:23 -08002695 */
David S. Millerd49c73c2006-08-13 18:55:53 -07002696 if (dst->obsolete < 0 && !stale_bundle(dst))
2697 return dst;
2698
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 return NULL;
2700}
2701
2702static int stale_bundle(struct dst_entry *dst)
2703{
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002704 return !xfrm_bundle_ok((struct xfrm_dst *)dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705}
2706
Herbert Xuaabc9762005-05-03 16:27:10 -07002707void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002710 dst->dev = dev_net(dev)->loopback_dev;
Daniel Lezcanode3cb742007-09-25 19:16:28 -07002711 dev_hold(dst->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 dev_put(dev);
2713 }
2714}
Herbert Xuaabc9762005-05-03 16:27:10 -07002715EXPORT_SYMBOL(xfrm_dst_ifdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716
2717static void xfrm_link_failure(struct sk_buff *skb)
2718{
2719 /* Impossible. Such dst must be popped before reaches point of failure. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720}
2721
2722static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
2723{
2724 if (dst) {
2725 if (dst->obsolete) {
2726 dst_release(dst);
2727 dst = NULL;
2728 }
2729 }
2730 return dst;
2731}
2732
Paul Mooree4c17212013-05-29 07:36:25 +00002733void xfrm_garbage_collect(struct net *net)
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002734{
Fan Duca925cf2014-01-18 09:55:27 +08002735 flow_cache_flush(net);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002736}
Paul Mooree4c17212013-05-29 07:36:25 +00002737EXPORT_SYMBOL(xfrm_garbage_collect);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002738
2739static void xfrm_garbage_collect_deferred(struct net *net)
2740{
Fan Duca925cf2014-01-18 09:55:27 +08002741 flow_cache_flush_deferred(net);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002742}
2743
Herbert Xu25ee3282007-12-11 09:32:34 -08002744static void xfrm_init_pmtu(struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745{
2746 do {
2747 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2748 u32 pmtu, route_mtu_cached;
2749
2750 pmtu = dst_mtu(dst->child);
2751 xdst->child_mtu_cached = pmtu;
2752
2753 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
2754
2755 route_mtu_cached = dst_mtu(xdst->route);
2756 xdst->route_mtu_cached = route_mtu_cached;
2757
2758 if (pmtu > route_mtu_cached)
2759 pmtu = route_mtu_cached;
2760
David S. Millerdefb3512010-12-08 21:16:57 -08002761 dst_metric_set(dst, RTAX_MTU, pmtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 } while ((dst = dst->next));
2763}
2764
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765/* Check that the bundle accepts the flow and its components are
2766 * still valid.
2767 */
2768
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002769static int xfrm_bundle_ok(struct xfrm_dst *first)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770{
2771 struct dst_entry *dst = &first->u.dst;
2772 struct xfrm_dst *last;
2773 u32 mtu;
2774
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07002775 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 (dst->dev && !netif_running(dst->dev)))
2777 return 0;
2778
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002779 if (dst->flags & DST_XFRM_QUEUE)
2780 return 1;
2781
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 last = NULL;
2783
2784 do {
2785 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2786
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 if (dst->xfrm->km.state != XFRM_STATE_VALID)
2788 return 0;
Timo Teräs80c802f2010-04-07 00:30:05 +00002789 if (xdst->xfrm_genid != dst->xfrm->genid)
2790 return 0;
Timo Teräsb1312c82010-06-24 14:35:00 -07002791 if (xdst->num_pols > 0 &&
2792 xdst->policy_genid != atomic_read(&xdst->pols[0]->genid))
David S. Miller9d4a7062006-08-24 03:18:09 -07002793 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794
2795 mtu = dst_mtu(dst->child);
2796 if (xdst->child_mtu_cached != mtu) {
2797 last = xdst;
2798 xdst->child_mtu_cached = mtu;
2799 }
2800
Hideaki YOSHIFUJI92d63de2005-05-26 12:58:04 -07002801 if (!dst_check(xdst->route, xdst->route_cookie))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 return 0;
2803 mtu = dst_mtu(xdst->route);
2804 if (xdst->route_mtu_cached != mtu) {
2805 last = xdst;
2806 xdst->route_mtu_cached = mtu;
2807 }
2808
2809 dst = dst->child;
2810 } while (dst->xfrm);
2811
2812 if (likely(!last))
2813 return 1;
2814
2815 mtu = last->child_mtu_cached;
2816 for (;;) {
2817 dst = &last->u.dst;
2818
2819 mtu = xfrm_state_mtu(dst->xfrm, mtu);
2820 if (mtu > last->route_mtu_cached)
2821 mtu = last->route_mtu_cached;
David S. Millerdefb3512010-12-08 21:16:57 -08002822 dst_metric_set(dst, RTAX_MTU, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823
2824 if (last == first)
2825 break;
2826
Patrick McHardybd0bf072007-07-18 01:55:52 -07002827 last = (struct xfrm_dst *)last->u.dst.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 last->child_mtu_cached = mtu;
2829 }
2830
2831 return 1;
2832}
2833
David S. Miller0dbaee32010-12-13 12:52:14 -08002834static unsigned int xfrm_default_advmss(const struct dst_entry *dst)
2835{
2836 return dst_metric_advmss(dst->path);
2837}
2838
Steffen Klassertebb762f2011-11-23 02:12:51 +00002839static unsigned int xfrm_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08002840{
Steffen Klassert618f9bc2011-11-23 02:13:31 +00002841 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
2842
2843 return mtu ? : dst_mtu(dst->path);
David S. Millerd33e4552010-12-14 13:01:14 -08002844}
2845
David S. Millerf894cbf2012-07-02 21:52:24 -07002846static struct neighbour *xfrm_neigh_lookup(const struct dst_entry *dst,
2847 struct sk_buff *skb,
2848 const void *daddr)
David S. Millerd3aaeb32011-07-18 00:40:17 -07002849{
David S. Millerf894cbf2012-07-02 21:52:24 -07002850 return dst->path->ops->neigh_lookup(dst, skb, daddr);
David S. Millerd3aaeb32011-07-18 00:40:17 -07002851}
2852
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
2854{
2855 int err = 0;
2856 if (unlikely(afinfo == NULL))
2857 return -EINVAL;
2858 if (unlikely(afinfo->family >= NPROTO))
2859 return -EAFNOSUPPORT;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002860 spin_lock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
Li RongQingf31e8d4f2015-04-23 11:06:53 +08002862 err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 else {
2864 struct dst_ops *dst_ops = afinfo->dst_ops;
2865 if (likely(dst_ops->kmem_cachep == NULL))
2866 dst_ops->kmem_cachep = xfrm_dst_cache;
2867 if (likely(dst_ops->check == NULL))
2868 dst_ops->check = xfrm_dst_check;
David S. Miller0dbaee32010-12-13 12:52:14 -08002869 if (likely(dst_ops->default_advmss == NULL))
2870 dst_ops->default_advmss = xfrm_default_advmss;
Steffen Klassertebb762f2011-11-23 02:12:51 +00002871 if (likely(dst_ops->mtu == NULL))
2872 dst_ops->mtu = xfrm_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 if (likely(dst_ops->negative_advice == NULL))
2874 dst_ops->negative_advice = xfrm_negative_advice;
2875 if (likely(dst_ops->link_failure == NULL))
2876 dst_ops->link_failure = xfrm_link_failure;
David S. Millerd3aaeb32011-07-18 00:40:17 -07002877 if (likely(dst_ops->neigh_lookup == NULL))
2878 dst_ops->neigh_lookup = xfrm_neigh_lookup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 if (likely(afinfo->garbage_collect == NULL))
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002880 afinfo->garbage_collect = xfrm_garbage_collect_deferred;
Priyanka Jain418a99a2012-08-12 21:22:29 +00002881 rcu_assign_pointer(xfrm_policy_afinfo[afinfo->family], afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02002883 spin_unlock(&xfrm_policy_afinfo_lock);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002884
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 return err;
2886}
2887EXPORT_SYMBOL(xfrm_policy_register_afinfo);
2888
2889int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
2890{
2891 int err = 0;
2892 if (unlikely(afinfo == NULL))
2893 return -EINVAL;
2894 if (unlikely(afinfo->family >= NPROTO))
2895 return -EAFNOSUPPORT;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002896 spin_lock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
2898 if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
2899 err = -EINVAL;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002900 else
2901 RCU_INIT_POINTER(xfrm_policy_afinfo[afinfo->family],
2902 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02002904 spin_unlock(&xfrm_policy_afinfo_lock);
2905 if (!err) {
2906 struct dst_ops *dst_ops = afinfo->dst_ops;
2907
2908 synchronize_rcu();
2909
2910 dst_ops->kmem_cachep = NULL;
2911 dst_ops->check = NULL;
2912 dst_ops->negative_advice = NULL;
2913 dst_ops->link_failure = NULL;
2914 afinfo->garbage_collect = NULL;
2915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 return err;
2917}
2918EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
2919
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
2921{
Jiri Pirko351638e2013-05-28 01:30:21 +00002922 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02002923
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 switch (event) {
2925 case NETDEV_DOWN:
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002926 xfrm_garbage_collect(dev_net(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 }
2928 return NOTIFY_DONE;
2929}
2930
2931static struct notifier_block xfrm_dev_notifier = {
Alexey Dobriyand5917a32008-10-31 00:41:59 -07002932 .notifier_call = xfrm_dev_event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933};
2934
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002935#ifdef CONFIG_XFRM_STATISTICS
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002936static int __net_init xfrm_statistics_init(struct net *net)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002937{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002938 int rv;
WANG Cong698365f2014-05-05 15:55:55 -07002939 net->mib.xfrm_statistics = alloc_percpu(struct linux_xfrm_mib);
2940 if (!net->mib.xfrm_statistics)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002941 return -ENOMEM;
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002942 rv = xfrm_proc_init(net);
2943 if (rv < 0)
WANG Cong698365f2014-05-05 15:55:55 -07002944 free_percpu(net->mib.xfrm_statistics);
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002945 return rv;
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002946}
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002947
2948static void xfrm_statistics_fini(struct net *net)
2949{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002950 xfrm_proc_fini(net);
WANG Cong698365f2014-05-05 15:55:55 -07002951 free_percpu(net->mib.xfrm_statistics);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002952}
2953#else
2954static int __net_init xfrm_statistics_init(struct net *net)
2955{
2956 return 0;
2957}
2958
2959static void xfrm_statistics_fini(struct net *net)
2960{
2961}
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002962#endif
2963
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002964static int __net_init xfrm_policy_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965{
David S. Miller2518c7c2006-08-24 04:45:07 -07002966 unsigned int hmask, sz;
2967 int dir;
2968
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002969 if (net_eq(net, &init_net))
2970 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 sizeof(struct xfrm_dst),
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002972 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002973 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974
David S. Miller2518c7c2006-08-24 04:45:07 -07002975 hmask = 8 - 1;
2976 sz = (hmask+1) * sizeof(struct hlist_head);
2977
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002978 net->xfrm.policy_byidx = xfrm_hash_alloc(sz);
2979 if (!net->xfrm.policy_byidx)
2980 goto out_byidx;
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08002981 net->xfrm.policy_idx_hmask = hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -07002982
Herbert Xu53c2e282014-11-13 17:09:49 +08002983 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -07002984 struct xfrm_policy_hash *htab;
2985
Alexey Dobriyandc2caba2008-11-25 17:24:15 -08002986 net->xfrm.policy_count[dir] = 0;
Herbert Xu53c2e282014-11-13 17:09:49 +08002987 net->xfrm.policy_count[XFRM_POLICY_MAX + dir] = 0;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002988 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
David S. Miller2518c7c2006-08-24 04:45:07 -07002989
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002990 htab = &net->xfrm.policy_bydst[dir];
David S. Miller44e36b42006-08-24 04:50:50 -07002991 htab->table = xfrm_hash_alloc(sz);
David S. Miller2518c7c2006-08-24 04:45:07 -07002992 if (!htab->table)
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002993 goto out_bydst;
2994 htab->hmask = hmask;
Christophe Gouaultb58555f2014-08-29 16:16:04 +02002995 htab->dbits4 = 32;
2996 htab->sbits4 = 32;
2997 htab->dbits6 = 128;
2998 htab->sbits6 = 128;
David S. Miller2518c7c2006-08-24 04:45:07 -07002999 }
Christophe Gouault880a6fa2014-08-29 16:16:05 +02003000 net->xfrm.policy_hthresh.lbits4 = 32;
3001 net->xfrm.policy_hthresh.rbits4 = 32;
3002 net->xfrm.policy_hthresh.lbits6 = 128;
3003 net->xfrm.policy_hthresh.rbits6 = 128;
3004
3005 seqlock_init(&net->xfrm.policy_hthresh.lock);
David S. Miller2518c7c2006-08-24 04:45:07 -07003006
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08003007 INIT_LIST_HEAD(&net->xfrm.policy_all);
Alexey Dobriyan66caf622008-11-25 17:28:57 -08003008 INIT_WORK(&net->xfrm.policy_hash_work, xfrm_hash_resize);
Christophe Gouault880a6fa2014-08-29 16:16:05 +02003009 INIT_WORK(&net->xfrm.policy_hthresh.work, xfrm_hash_rebuild);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003010 if (net_eq(net, &init_net))
3011 register_netdevice_notifier(&xfrm_dev_notifier);
3012 return 0;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003013
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003014out_bydst:
3015 for (dir--; dir >= 0; dir--) {
3016 struct xfrm_policy_hash *htab;
3017
3018 htab = &net->xfrm.policy_bydst[dir];
3019 xfrm_hash_free(htab->table, sz);
3020 }
3021 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003022out_byidx:
3023 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024}
3025
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003026static void xfrm_policy_fini(struct net *net)
3027{
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003028 unsigned int sz;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003029 int dir;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003030
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08003031 flush_work(&net->xfrm.policy_hash_work);
3032#ifdef CONFIG_XFRM_SUB_POLICY
Tetsuo Handa2e710292014-04-22 21:48:30 +09003033 xfrm_policy_flush(net, XFRM_POLICY_TYPE_SUB, false);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08003034#endif
Tetsuo Handa2e710292014-04-22 21:48:30 +09003035 xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, false);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08003036
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08003037 WARN_ON(!list_empty(&net->xfrm.policy_all));
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003038
Herbert Xu53c2e282014-11-13 17:09:49 +08003039 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003040 struct xfrm_policy_hash *htab;
3041
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003042 WARN_ON(!hlist_empty(&net->xfrm.policy_inexact[dir]));
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003043
3044 htab = &net->xfrm.policy_bydst[dir];
Michal Kubecek5b653b22013-01-18 16:03:48 +01003045 sz = (htab->hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08003046 WARN_ON(!hlist_empty(htab->table));
3047 xfrm_hash_free(htab->table, sz);
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08003048 }
3049
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08003050 sz = (net->xfrm.policy_idx_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08003051 WARN_ON(!hlist_empty(net->xfrm.policy_byidx));
3052 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003053}
3054
3055static int __net_init xfrm_net_init(struct net *net)
3056{
3057 int rv;
3058
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003059 rv = xfrm_statistics_init(net);
3060 if (rv < 0)
3061 goto out_statistics;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003062 rv = xfrm_state_init(net);
3063 if (rv < 0)
3064 goto out_state;
3065 rv = xfrm_policy_init(net);
3066 if (rv < 0)
3067 goto out_policy;
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08003068 rv = xfrm_sysctl_init(net);
3069 if (rv < 0)
3070 goto out_sysctl;
Steffen Klassert4a93f502014-03-12 09:43:17 +01003071 rv = flow_cache_init(net);
3072 if (rv < 0)
3073 goto out;
Fan Du283bc9f2013-11-07 17:47:50 +08003074
3075 /* Initialize the per-net locks here */
3076 spin_lock_init(&net->xfrm.xfrm_state_lock);
3077 rwlock_init(&net->xfrm.xfrm_policy_lock);
Fan Du283bc9f2013-11-07 17:47:50 +08003078 mutex_init(&net->xfrm.xfrm_cfg_mutex);
3079
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003080 return 0;
3081
Steffen Klassert4a93f502014-03-12 09:43:17 +01003082out:
3083 xfrm_sysctl_fini(net);
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08003084out_sysctl:
3085 xfrm_policy_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003086out_policy:
3087 xfrm_state_fini(net);
3088out_state:
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003089 xfrm_statistics_fini(net);
3090out_statistics:
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003091 return rv;
3092}
3093
3094static void __net_exit xfrm_net_exit(struct net *net)
3095{
Steffen Klassert4a93f502014-03-12 09:43:17 +01003096 flow_cache_fini(net);
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08003097 xfrm_sysctl_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003098 xfrm_policy_fini(net);
3099 xfrm_state_fini(net);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08003100 xfrm_statistics_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003101}
3102
3103static struct pernet_operations __net_initdata xfrm_net_ops = {
3104 .init = xfrm_net_init,
3105 .exit = xfrm_net_exit,
3106};
3107
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108void __init xfrm_init(void)
3109{
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08003110 register_pernet_subsys(&xfrm_net_ops);
Florian Westphal30846092016-08-11 15:17:54 +02003111 seqcount_init(&xfrm_policy_hash_generation);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 xfrm_input_init();
3113}
3114
Joy Lattenab5f5e82007-09-17 11:51:22 -07003115#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinen1486cbd72008-01-12 03:20:03 -08003116static void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
3117 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003118{
Paul Moore875179f2007-12-01 23:27:18 +11003119 struct xfrm_sec_ctx *ctx = xp->security;
3120 struct xfrm_selector *sel = &xp->selector;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003121
Paul Moore875179f2007-12-01 23:27:18 +11003122 if (ctx)
3123 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
3124 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
3125
Weilong Chen9b7a7872013-12-24 09:43:46 +08003126 switch (sel->family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07003127 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07003128 audit_log_format(audit_buf, " src=%pI4", &sel->saddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11003129 if (sel->prefixlen_s != 32)
3130 audit_log_format(audit_buf, " src_prefixlen=%d",
3131 sel->prefixlen_s);
Harvey Harrison21454aa2008-10-31 00:54:56 -07003132 audit_log_format(audit_buf, " dst=%pI4", &sel->daddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11003133 if (sel->prefixlen_d != 32)
3134 audit_log_format(audit_buf, " dst_prefixlen=%d",
3135 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003136 break;
3137 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07003138 audit_log_format(audit_buf, " src=%pI6", sel->saddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11003139 if (sel->prefixlen_s != 128)
3140 audit_log_format(audit_buf, " src_prefixlen=%d",
3141 sel->prefixlen_s);
Harvey Harrison5b095d9892008-10-29 12:52:50 -07003142 audit_log_format(audit_buf, " dst=%pI6", sel->daddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11003143 if (sel->prefixlen_d != 128)
3144 audit_log_format(audit_buf, " dst_prefixlen=%d",
3145 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003146 break;
3147 }
3148}
3149
Tetsuo Handa2e710292014-04-22 21:48:30 +09003150void xfrm_audit_policy_add(struct xfrm_policy *xp, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003151{
3152 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003153
Paul Mooreafeb14b2007-12-21 14:58:11 -08003154 audit_buf = xfrm_audit_start("SPD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07003155 if (audit_buf == NULL)
3156 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09003157 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08003158 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003159 xfrm_audit_common_policyinfo(xp, audit_buf);
3160 audit_log_end(audit_buf);
3161}
3162EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
3163
Paul Moore68277ac2007-12-20 20:49:33 -08003164void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
Tetsuo Handa2e710292014-04-22 21:48:30 +09003165 bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07003166{
3167 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07003168
Paul Mooreafeb14b2007-12-21 14:58:11 -08003169 audit_buf = xfrm_audit_start("SPD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07003170 if (audit_buf == NULL)
3171 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09003172 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08003173 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07003174 xfrm_audit_common_policyinfo(xp, audit_buf);
3175 audit_log_end(audit_buf);
3176}
3177EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
3178#endif
3179
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003180#ifdef CONFIG_XFRM_MIGRATE
David S. Millerbc9b35a2012-05-15 15:04:57 -04003181static bool xfrm_migrate_selector_match(const struct xfrm_selector *sel_cmp,
3182 const struct xfrm_selector *sel_tgt)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003183{
3184 if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
3185 if (sel_tgt->family == sel_cmp->family &&
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003186 xfrm_addr_equal(&sel_tgt->daddr, &sel_cmp->daddr,
3187 sel_cmp->family) &&
3188 xfrm_addr_equal(&sel_tgt->saddr, &sel_cmp->saddr,
3189 sel_cmp->family) &&
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003190 sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
3191 sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04003192 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003193 }
3194 } else {
3195 if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04003196 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003197 }
3198 }
David S. Millerbc9b35a2012-05-15 15:04:57 -04003199 return false;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003200}
3201
Weilong Chen3e94c2d2013-12-24 09:43:47 +08003202static struct xfrm_policy *xfrm_migrate_policy_find(const struct xfrm_selector *sel,
3203 u8 dir, u8 type, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003204{
3205 struct xfrm_policy *pol, *ret = NULL;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003206 struct hlist_head *chain;
3207 u32 priority = ~0U;
3208
Fan Du283bc9f2013-11-07 17:47:50 +08003209 read_lock_bh(&net->xfrm.xfrm_policy_lock); /*FIXME*/
Fan Du8d549c42013-11-07 17:47:49 +08003210 chain = policy_hash_direct(net, &sel->daddr, &sel->saddr, sel->family, dir);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003211 hlist_for_each_entry(pol, chain, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003212 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
3213 pol->type == type) {
3214 ret = pol;
3215 priority = ret->priority;
3216 break;
3217 }
3218 }
Fan Du8d549c42013-11-07 17:47:49 +08003219 chain = &net->xfrm.policy_inexact[dir];
Sasha Levinb67bfe02013-02-27 17:06:00 -08003220 hlist_for_each_entry(pol, chain, bydst) {
Li RongQing8faf4912015-05-14 11:16:59 +08003221 if ((pol->priority >= priority) && ret)
3222 break;
3223
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003224 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
Li RongQing8faf4912015-05-14 11:16:59 +08003225 pol->type == type) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003226 ret = pol;
3227 break;
3228 }
3229 }
3230
Li RongQing586f2eb2015-04-30 17:13:41 +08003231 xfrm_pol_hold(ret);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003232
Fan Du283bc9f2013-11-07 17:47:50 +08003233 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003234
3235 return ret;
3236}
3237
David S. Millerdd701752011-02-24 00:21:08 -05003238static int migrate_tmpl_match(const struct xfrm_migrate *m, const struct xfrm_tmpl *t)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003239{
3240 int match = 0;
3241
3242 if (t->mode == m->mode && t->id.proto == m->proto &&
3243 (m->reqid == 0 || t->reqid == m->reqid)) {
3244 switch (t->mode) {
3245 case XFRM_MODE_TUNNEL:
3246 case XFRM_MODE_BEET:
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003247 if (xfrm_addr_equal(&t->id.daddr, &m->old_daddr,
3248 m->old_family) &&
3249 xfrm_addr_equal(&t->saddr, &m->old_saddr,
3250 m->old_family)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003251 match = 1;
3252 }
3253 break;
3254 case XFRM_MODE_TRANSPORT:
3255 /* in case of transport mode, template does not store
3256 any IP addresses, hence we just compare mode and
3257 protocol */
3258 match = 1;
3259 break;
3260 default:
3261 break;
3262 }
3263 }
3264 return match;
3265}
3266
3267/* update endpoint address(es) of template(s) */
3268static int xfrm_policy_migrate(struct xfrm_policy *pol,
3269 struct xfrm_migrate *m, int num_migrate)
3270{
3271 struct xfrm_migrate *mp;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003272 int i, j, n = 0;
3273
3274 write_lock_bh(&pol->lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07003275 if (unlikely(pol->walk.dead)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003276 /* target policy has been deleted */
3277 write_unlock_bh(&pol->lock);
3278 return -ENOENT;
3279 }
3280
3281 for (i = 0; i < pol->xfrm_nr; i++) {
3282 for (j = 0, mp = m; j < num_migrate; j++, mp++) {
3283 if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
3284 continue;
3285 n++;
Herbert Xu1bfcb102007-10-17 21:31:50 -07003286 if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
3287 pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003288 continue;
3289 /* update endpoints */
3290 memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
3291 sizeof(pol->xfrm_vec[i].id.daddr));
3292 memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
3293 sizeof(pol->xfrm_vec[i].saddr));
3294 pol->xfrm_vec[i].encap_family = mp->new_family;
3295 /* flush bundles */
Timo Teräs80c802f2010-04-07 00:30:05 +00003296 atomic_inc(&pol->genid);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003297 }
3298 }
3299
3300 write_unlock_bh(&pol->lock);
3301
3302 if (!n)
3303 return -ENODATA;
3304
3305 return 0;
3306}
3307
David S. Millerdd701752011-02-24 00:21:08 -05003308static int xfrm_migrate_check(const struct xfrm_migrate *m, int num_migrate)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003309{
3310 int i, j;
3311
3312 if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
3313 return -EINVAL;
3314
3315 for (i = 0; i < num_migrate; i++) {
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003316 if (xfrm_addr_equal(&m[i].old_daddr, &m[i].new_daddr,
3317 m[i].old_family) &&
3318 xfrm_addr_equal(&m[i].old_saddr, &m[i].new_saddr,
3319 m[i].old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003320 return -EINVAL;
3321 if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
3322 xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
3323 return -EINVAL;
3324
3325 /* check if there is any duplicated entry */
3326 for (j = i + 1; j < num_migrate; j++) {
3327 if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
3328 sizeof(m[i].old_daddr)) &&
3329 !memcmp(&m[i].old_saddr, &m[j].old_saddr,
3330 sizeof(m[i].old_saddr)) &&
3331 m[i].proto == m[j].proto &&
3332 m[i].mode == m[j].mode &&
3333 m[i].reqid == m[j].reqid &&
3334 m[i].old_family == m[j].old_family)
3335 return -EINVAL;
3336 }
3337 }
3338
3339 return 0;
3340}
3341
David S. Millerb4b7c0b2011-02-24 00:35:06 -05003342int xfrm_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003343 struct xfrm_migrate *m, int num_migrate,
Fan Du8d549c42013-11-07 17:47:49 +08003344 struct xfrm_kmaddress *k, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003345{
3346 int i, err, nx_cur = 0, nx_new = 0;
3347 struct xfrm_policy *pol = NULL;
3348 struct xfrm_state *x, *xc;
3349 struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
3350 struct xfrm_state *x_new[XFRM_MAX_DEPTH];
3351 struct xfrm_migrate *mp;
3352
3353 if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
3354 goto out;
3355
3356 /* Stage 1 - find policy */
Fan Du8d549c42013-11-07 17:47:49 +08003357 if ((pol = xfrm_migrate_policy_find(sel, dir, type, net)) == NULL) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003358 err = -ENOENT;
3359 goto out;
3360 }
3361
3362 /* Stage 2 - find and update state(s) */
3363 for (i = 0, mp = m; i < num_migrate; i++, mp++) {
Fan Du283bc9f2013-11-07 17:47:50 +08003364 if ((x = xfrm_migrate_state_find(mp, net))) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003365 x_cur[nx_cur] = x;
3366 nx_cur++;
3367 if ((xc = xfrm_state_migrate(x, mp))) {
3368 x_new[nx_new] = xc;
3369 nx_new++;
3370 } else {
3371 err = -ENODATA;
3372 goto restore_state;
3373 }
3374 }
3375 }
3376
3377 /* Stage 3 - update policy */
3378 if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
3379 goto restore_state;
3380
3381 /* Stage 4 - delete old state(s) */
3382 if (nx_cur) {
3383 xfrm_states_put(x_cur, nx_cur);
3384 xfrm_states_delete(x_cur, nx_cur);
3385 }
3386
3387 /* Stage 5 - announce */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003388 km_migrate(sel, dir, type, m, num_migrate, k);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003389
3390 xfrm_pol_put(pol);
3391
3392 return 0;
3393out:
3394 return err;
3395
3396restore_state:
3397 if (pol)
3398 xfrm_pol_put(pol);
3399 if (nx_cur)
3400 xfrm_states_put(x_cur, nx_cur);
3401 if (nx_new)
3402 xfrm_states_delete(x_new, nx_new);
3403
3404 return err;
3405}
David S. Millere610e672007-02-08 13:29:15 -08003406EXPORT_SYMBOL(xfrm_migrate);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003407#endif