blob: a8ef5108e0d86cbc5c411f3db378fde5a0d54f18 [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
Priyanka Jain418a99a2012-08-12 21:22:29 +000042static DEFINE_SPINLOCK(xfrm_policy_afinfo_lock);
43static struct xfrm_policy_afinfo __rcu *xfrm_policy_afinfo[NPROTO]
44 __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Christoph Lametere18b8902006-12-06 20:33:20 -080046static struct kmem_cache *xfrm_dst_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Herbert Xu25ee3282007-12-11 09:32:34 -080048static void xfrm_init_pmtu(struct dst_entry *dst);
Timo Teräs80c802f2010-04-07 00:30:05 +000049static int stale_bundle(struct dst_entry *dst);
Steffen Klassert12fdb4d2011-06-29 23:18:20 +000050static int xfrm_bundle_ok(struct xfrm_dst *xdst);
Steffen Klasserta0073fe2013-02-05 12:52:55 +010051static void xfrm_policy_queue_process(unsigned long arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Wei Yongjun29fa0b302008-12-03 00:33:09 -080053static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
54 int dir);
55
David S. Millerbc9b35a2012-05-15 15:04:57 -040056static inline bool
David S. Miller200ce962011-02-24 00:12:25 -050057__xfrm4_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
Andrew Morton77681022006-11-08 22:46:26 -080058{
David S. Miller7e1dc7b2011-03-12 02:42:11 -050059 const struct flowi4 *fl4 = &fl->u.ip4;
60
Alexey Dobriyan26bff942011-11-22 06:46:02 +000061 return addr4_match(fl4->daddr, sel->daddr.a4, sel->prefixlen_d) &&
62 addr4_match(fl4->saddr, sel->saddr.a4, sel->prefixlen_s) &&
David S. Miller7e1dc7b2011-03-12 02:42:11 -050063 !((xfrm_flowi_dport(fl, &fl4->uli) ^ sel->dport) & sel->dport_mask) &&
64 !((xfrm_flowi_sport(fl, &fl4->uli) ^ sel->sport) & sel->sport_mask) &&
65 (fl4->flowi4_proto == sel->proto || !sel->proto) &&
66 (fl4->flowi4_oif == sel->ifindex || !sel->ifindex);
Andrew Morton77681022006-11-08 22:46:26 -080067}
68
David S. Millerbc9b35a2012-05-15 15:04:57 -040069static inline bool
David S. Miller200ce962011-02-24 00:12:25 -050070__xfrm6_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
Andrew Morton77681022006-11-08 22:46:26 -080071{
David S. Miller7e1dc7b2011-03-12 02:42:11 -050072 const struct flowi6 *fl6 = &fl->u.ip6;
73
74 return addr_match(&fl6->daddr, &sel->daddr, sel->prefixlen_d) &&
75 addr_match(&fl6->saddr, &sel->saddr, sel->prefixlen_s) &&
76 !((xfrm_flowi_dport(fl, &fl6->uli) ^ sel->dport) & sel->dport_mask) &&
77 !((xfrm_flowi_sport(fl, &fl6->uli) ^ sel->sport) & sel->sport_mask) &&
78 (fl6->flowi6_proto == sel->proto || !sel->proto) &&
79 (fl6->flowi6_oif == sel->ifindex || !sel->ifindex);
Andrew Morton77681022006-11-08 22:46:26 -080080}
81
David S. Millerbc9b35a2012-05-15 15:04:57 -040082bool xfrm_selector_match(const struct xfrm_selector *sel, const struct flowi *fl,
83 unsigned short family)
Andrew Morton77681022006-11-08 22:46:26 -080084{
85 switch (family) {
86 case AF_INET:
87 return __xfrm4_selector_match(sel, fl);
88 case AF_INET6:
89 return __xfrm6_selector_match(sel, fl);
90 }
David S. Millerbc9b35a2012-05-15 15:04:57 -040091 return false;
Andrew Morton77681022006-11-08 22:46:26 -080092}
93
Eric Dumazetef8531b2012-08-19 12:31:48 +020094static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
95{
96 struct xfrm_policy_afinfo *afinfo;
97
98 if (unlikely(family >= NPROTO))
99 return NULL;
100 rcu_read_lock();
101 afinfo = rcu_dereference(xfrm_policy_afinfo[family]);
102 if (unlikely(!afinfo))
103 rcu_read_unlock();
104 return afinfo;
105}
106
107static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
108{
109 rcu_read_unlock();
110}
111
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800112static inline struct dst_entry *__xfrm_dst_lookup(struct net *net, int tos,
David S. Miller6418c4e2011-02-24 00:16:53 -0500113 const xfrm_address_t *saddr,
114 const xfrm_address_t *daddr,
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900115 int family)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800117 struct xfrm_policy_afinfo *afinfo;
118 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Herbert Xu25ee3282007-12-11 09:32:34 -0800120 afinfo = xfrm_policy_get_afinfo(family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 if (unlikely(afinfo == NULL))
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800122 return ERR_PTR(-EAFNOSUPPORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800124 dst = afinfo->dst_lookup(net, tos, saddr, daddr);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 xfrm_policy_put_afinfo(afinfo);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900127
128 return dst;
129}
130
131static inline struct dst_entry *xfrm_dst_lookup(struct xfrm_state *x, int tos,
132 xfrm_address_t *prev_saddr,
133 xfrm_address_t *prev_daddr,
134 int family)
135{
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800136 struct net *net = xs_net(x);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900137 xfrm_address_t *saddr = &x->props.saddr;
138 xfrm_address_t *daddr = &x->id.daddr;
139 struct dst_entry *dst;
140
141 if (x->type->flags & XFRM_TYPE_LOCAL_COADDR) {
142 saddr = x->coaddr;
143 daddr = prev_daddr;
144 }
145 if (x->type->flags & XFRM_TYPE_REMOTE_COADDR) {
146 saddr = prev_saddr;
147 daddr = x->coaddr;
148 }
149
Alexey Dobriyanc5b3cf42008-11-25 17:51:25 -0800150 dst = __xfrm_dst_lookup(net, tos, saddr, daddr, family);
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +0900151
152 if (!IS_ERR(dst)) {
153 if (prev_saddr != saddr)
154 memcpy(prev_saddr, saddr, sizeof(*prev_saddr));
155 if (prev_daddr != daddr)
156 memcpy(prev_daddr, daddr, sizeof(*prev_daddr));
157 }
158
Herbert Xu66cdb3c2007-11-13 21:37:28 -0800159 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162static inline unsigned long make_jiffies(long secs)
163{
164 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
165 return MAX_SCHEDULE_TIMEOUT-1;
166 else
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +0900167 return secs*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168}
169
170static void xfrm_policy_timer(unsigned long data)
171{
Weilong Chen3e94c2d2013-12-24 09:43:47 +0800172 struct xfrm_policy *xp = (struct xfrm_policy *)data;
James Morris9d729f72007-03-04 16:12:44 -0800173 unsigned long now = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 long next = LONG_MAX;
175 int warn = 0;
176 int dir;
177
178 read_lock(&xp->lock);
179
Timo Teräsea2dea92010-03-31 00:17:05 +0000180 if (unlikely(xp->walk.dead))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 goto out;
182
Herbert Xu77d8d7a2005-10-05 12:15:12 -0700183 dir = xfrm_policy_id2dir(xp->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185 if (xp->lft.hard_add_expires_seconds) {
186 long tmo = xp->lft.hard_add_expires_seconds +
187 xp->curlft.add_time - now;
188 if (tmo <= 0)
189 goto expired;
190 if (tmo < next)
191 next = tmo;
192 }
193 if (xp->lft.hard_use_expires_seconds) {
194 long tmo = xp->lft.hard_use_expires_seconds +
195 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
196 if (tmo <= 0)
197 goto expired;
198 if (tmo < next)
199 next = tmo;
200 }
201 if (xp->lft.soft_add_expires_seconds) {
202 long tmo = xp->lft.soft_add_expires_seconds +
203 xp->curlft.add_time - now;
204 if (tmo <= 0) {
205 warn = 1;
206 tmo = XFRM_KM_TIMEOUT;
207 }
208 if (tmo < next)
209 next = tmo;
210 }
211 if (xp->lft.soft_use_expires_seconds) {
212 long tmo = xp->lft.soft_use_expires_seconds +
213 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
214 if (tmo <= 0) {
215 warn = 1;
216 tmo = XFRM_KM_TIMEOUT;
217 }
218 if (tmo < next)
219 next = tmo;
220 }
221
222 if (warn)
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800223 km_policy_expired(xp, dir, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 if (next != LONG_MAX &&
225 !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
226 xfrm_pol_hold(xp);
227
228out:
229 read_unlock(&xp->lock);
230 xfrm_pol_put(xp);
231 return;
232
233expired:
234 read_unlock(&xp->lock);
Herbert Xu4666faa2005-06-18 22:43:22 -0700235 if (!xfrm_policy_delete(xp, dir))
Jamal Hadi Salim6c5c8ca2006-03-20 19:17:25 -0800236 km_policy_expired(xp, dir, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 xfrm_pol_put(xp);
238}
239
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000240static struct flow_cache_object *xfrm_policy_flo_get(struct flow_cache_object *flo)
241{
242 struct xfrm_policy *pol = container_of(flo, struct xfrm_policy, flo);
243
244 if (unlikely(pol->walk.dead))
245 flo = NULL;
246 else
247 xfrm_pol_hold(pol);
248
249 return flo;
250}
251
252static int xfrm_policy_flo_check(struct flow_cache_object *flo)
253{
254 struct xfrm_policy *pol = container_of(flo, struct xfrm_policy, flo);
255
256 return !pol->walk.dead;
257}
258
259static void xfrm_policy_flo_delete(struct flow_cache_object *flo)
260{
261 xfrm_pol_put(container_of(flo, struct xfrm_policy, flo));
262}
263
264static const struct flow_cache_ops xfrm_policy_fc_ops = {
265 .get = xfrm_policy_flo_get,
266 .check = xfrm_policy_flo_check,
267 .delete = xfrm_policy_flo_delete,
268};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270/* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
271 * SPD calls.
272 */
273
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -0800274struct xfrm_policy *xfrm_policy_alloc(struct net *net, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
276 struct xfrm_policy *policy;
277
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700278 policy = kzalloc(sizeof(struct xfrm_policy), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 if (policy) {
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -0800281 write_pnet(&policy->xp_net, net);
Herbert Xu12a169e2008-10-01 07:03:24 -0700282 INIT_LIST_HEAD(&policy->walk.all);
David S. Miller2518c7c2006-08-24 04:45:07 -0700283 INIT_HLIST_NODE(&policy->bydst);
284 INIT_HLIST_NODE(&policy->byidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 rwlock_init(&policy->lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700286 atomic_set(&policy->refcnt, 1);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100287 skb_queue_head_init(&policy->polq.hold_queue);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800288 setup_timer(&policy->timer, xfrm_policy_timer,
289 (unsigned long)policy);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100290 setup_timer(&policy->polq.hold_timer, xfrm_policy_queue_process,
291 (unsigned long)policy);
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000292 policy->flo.ops = &xfrm_policy_fc_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
294 return policy;
295}
296EXPORT_SYMBOL(xfrm_policy_alloc);
297
298/* Destroy xfrm_policy: descendant resources must be released to this moment. */
299
WANG Cong64c31b32008-01-07 22:34:29 -0800300void xfrm_policy_destroy(struct xfrm_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
Herbert Xu12a169e2008-10-01 07:03:24 -0700302 BUG_ON(!policy->walk.dead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Fan Du0659eea2013-08-01 18:08:36 +0800304 if (del_timer(&policy->timer) || del_timer(&policy->polq.hold_timer))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 BUG();
306
Paul Moore03e1ad72008-04-12 19:07:52 -0700307 security_xfrm_policy_free(policy->security);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 kfree(policy);
309}
WANG Cong64c31b32008-01-07 22:34:29 -0800310EXPORT_SYMBOL(xfrm_policy_destroy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100312static void xfrm_queue_purge(struct sk_buff_head *list)
313{
314 struct sk_buff *skb;
315
Steffen Klassert302a50b2013-08-28 08:47:14 +0200316 while ((skb = skb_dequeue(list)) != NULL)
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100317 kfree_skb(skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100318}
319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320/* Rule must be locked. Release descentant resources, announce
321 * entry dead. The rule must be unlinked from lists to the moment.
322 */
323
324static void xfrm_policy_kill(struct xfrm_policy *policy)
325{
Herbert Xu12a169e2008-10-01 07:03:24 -0700326 policy->walk.dead = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Timo Teräs285ead12010-04-07 00:30:06 +0000328 atomic_inc(&policy->genid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200330 if (del_timer(&policy->polq.hold_timer))
331 xfrm_pol_put(policy);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100332 xfrm_queue_purge(&policy->polq.hold_queue);
333
Timo Teräs285ead12010-04-07 00:30:06 +0000334 if (del_timer(&policy->timer))
335 xfrm_pol_put(policy);
336
337 xfrm_pol_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
339
David S. Miller2518c7c2006-08-24 04:45:07 -0700340static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024;
341
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800342static inline unsigned int idx_hash(struct net *net, u32 index)
David S. Miller2518c7c2006-08-24 04:45:07 -0700343{
Alexey Dobriyane92303f2008-11-25 17:32:41 -0800344 return __idx_hash(index, net->xfrm.policy_idx_hmask);
David S. Miller2518c7c2006-08-24 04:45:07 -0700345}
346
David S. Miller5f803b52011-02-24 00:33:19 -0500347static struct hlist_head *policy_hash_bysel(struct net *net,
348 const struct xfrm_selector *sel,
349 unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700350{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800351 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700352 unsigned int hash = __sel_hash(sel, family, hmask);
353
354 return (hash == hmask + 1 ?
Alexey Dobriyan11219942008-11-25 17:33:06 -0800355 &net->xfrm.policy_inexact[dir] :
356 net->xfrm.policy_bydst[dir].table + hash);
David S. Miller2518c7c2006-08-24 04:45:07 -0700357}
358
David S. Miller5f803b52011-02-24 00:33:19 -0500359static struct hlist_head *policy_hash_direct(struct net *net,
360 const xfrm_address_t *daddr,
361 const xfrm_address_t *saddr,
362 unsigned short family, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700363{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800364 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700365 unsigned int hash = __addr_hash(daddr, saddr, family, hmask);
366
Alexey Dobriyan11219942008-11-25 17:33:06 -0800367 return net->xfrm.policy_bydst[dir].table + hash;
David S. Miller2518c7c2006-08-24 04:45:07 -0700368}
369
David S. Miller2518c7c2006-08-24 04:45:07 -0700370static void xfrm_dst_hash_transfer(struct hlist_head *list,
371 struct hlist_head *ndsttable,
372 unsigned int nhashmask)
373{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800374 struct hlist_node *tmp, *entry0 = NULL;
David S. Miller2518c7c2006-08-24 04:45:07 -0700375 struct xfrm_policy *pol;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800376 unsigned int h0 = 0;
David S. Miller2518c7c2006-08-24 04:45:07 -0700377
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800378redo:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800379 hlist_for_each_entry_safe(pol, tmp, list, bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700380 unsigned int h;
381
382 h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
383 pol->family, nhashmask);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800384 if (!entry0) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800385 hlist_del(&pol->bydst);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800386 hlist_add_head(&pol->bydst, ndsttable+h);
387 h0 = h;
388 } else {
389 if (h != h0)
390 continue;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800391 hlist_del(&pol->bydst);
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800392 hlist_add_after(entry0, &pol->bydst);
393 }
Sasha Levinb67bfe02013-02-27 17:06:00 -0800394 entry0 = &pol->bydst;
YOSHIFUJI Hideakib7911602008-02-17 23:29:30 -0800395 }
396 if (!hlist_empty(list)) {
397 entry0 = NULL;
398 goto redo;
David S. Miller2518c7c2006-08-24 04:45:07 -0700399 }
400}
401
402static void xfrm_idx_hash_transfer(struct hlist_head *list,
403 struct hlist_head *nidxtable,
404 unsigned int nhashmask)
405{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800406 struct hlist_node *tmp;
David S. Miller2518c7c2006-08-24 04:45:07 -0700407 struct xfrm_policy *pol;
408
Sasha Levinb67bfe02013-02-27 17:06:00 -0800409 hlist_for_each_entry_safe(pol, tmp, list, byidx) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700410 unsigned int h;
411
412 h = __idx_hash(pol->index, nhashmask);
413 hlist_add_head(&pol->byidx, nidxtable+h);
414 }
415}
416
417static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
418{
419 return ((old_hmask + 1) << 1) - 1;
420}
421
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800422static void xfrm_bydst_resize(struct net *net, int dir)
David S. Miller2518c7c2006-08-24 04:45:07 -0700423{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800424 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700425 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
426 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800427 struct hlist_head *odst = net->xfrm.policy_bydst[dir].table;
David S. Miller44e36b42006-08-24 04:50:50 -0700428 struct hlist_head *ndst = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700429 int i;
430
431 if (!ndst)
432 return;
433
Fan Du283bc9f2013-11-07 17:47:50 +0800434 write_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700435
436 for (i = hmask; i >= 0; i--)
437 xfrm_dst_hash_transfer(odst + i, ndst, nhashmask);
438
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800439 net->xfrm.policy_bydst[dir].table = ndst;
440 net->xfrm.policy_bydst[dir].hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700441
Fan Du283bc9f2013-11-07 17:47:50 +0800442 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700443
David S. Miller44e36b42006-08-24 04:50:50 -0700444 xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700445}
446
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800447static void xfrm_byidx_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700448{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800449 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700450 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
451 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800452 struct hlist_head *oidx = net->xfrm.policy_byidx;
David S. Miller44e36b42006-08-24 04:50:50 -0700453 struct hlist_head *nidx = xfrm_hash_alloc(nsize);
David S. Miller2518c7c2006-08-24 04:45:07 -0700454 int i;
455
456 if (!nidx)
457 return;
458
Fan Du283bc9f2013-11-07 17:47:50 +0800459 write_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700460
461 for (i = hmask; i >= 0; i--)
462 xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
463
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800464 net->xfrm.policy_byidx = nidx;
465 net->xfrm.policy_idx_hmask = nhashmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700466
Fan Du283bc9f2013-11-07 17:47:50 +0800467 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700468
David S. Miller44e36b42006-08-24 04:50:50 -0700469 xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
David S. Miller2518c7c2006-08-24 04:45:07 -0700470}
471
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800472static inline int xfrm_bydst_should_resize(struct net *net, int dir, int *total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700473{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800474 unsigned int cnt = net->xfrm.policy_count[dir];
475 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700476
477 if (total)
478 *total += cnt;
479
480 if ((hmask + 1) < xfrm_policy_hashmax &&
481 cnt > hmask)
482 return 1;
483
484 return 0;
485}
486
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800487static inline int xfrm_byidx_should_resize(struct net *net, int total)
David S. Miller2518c7c2006-08-24 04:45:07 -0700488{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800489 unsigned int hmask = net->xfrm.policy_idx_hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -0700490
491 if ((hmask + 1) < xfrm_policy_hashmax &&
492 total > hmask)
493 return 1;
494
495 return 0;
496}
497
Alexey Dobriyane0710412010-01-23 13:37:10 +0000498void xfrm_spd_getinfo(struct net *net, struct xfrmk_spdinfo *si)
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700499{
Fan Du283bc9f2013-11-07 17:47:50 +0800500 read_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyane0710412010-01-23 13:37:10 +0000501 si->incnt = net->xfrm.policy_count[XFRM_POLICY_IN];
502 si->outcnt = net->xfrm.policy_count[XFRM_POLICY_OUT];
503 si->fwdcnt = net->xfrm.policy_count[XFRM_POLICY_FWD];
504 si->inscnt = net->xfrm.policy_count[XFRM_POLICY_IN+XFRM_POLICY_MAX];
505 si->outscnt = net->xfrm.policy_count[XFRM_POLICY_OUT+XFRM_POLICY_MAX];
506 si->fwdscnt = net->xfrm.policy_count[XFRM_POLICY_FWD+XFRM_POLICY_MAX];
507 si->spdhcnt = net->xfrm.policy_idx_hmask;
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700508 si->spdhmcnt = xfrm_policy_hashmax;
Fan Du283bc9f2013-11-07 17:47:50 +0800509 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700510}
511EXPORT_SYMBOL(xfrm_spd_getinfo);
David S. Miller2518c7c2006-08-24 04:45:07 -0700512
Jamal Hadi Salimecfd6b12007-04-28 21:20:32 -0700513static DEFINE_MUTEX(hash_resize_mutex);
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800514static void xfrm_hash_resize(struct work_struct *work)
David S. Miller2518c7c2006-08-24 04:45:07 -0700515{
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800516 struct net *net = container_of(work, struct net, xfrm.policy_hash_work);
David S. Miller2518c7c2006-08-24 04:45:07 -0700517 int dir, total;
518
519 mutex_lock(&hash_resize_mutex);
520
521 total = 0;
522 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800523 if (xfrm_bydst_should_resize(net, dir, &total))
524 xfrm_bydst_resize(net, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700525 }
Alexey Dobriyan66caf622008-11-25 17:28:57 -0800526 if (xfrm_byidx_should_resize(net, total))
527 xfrm_byidx_resize(net, total);
David S. Miller2518c7c2006-08-24 04:45:07 -0700528
529 mutex_unlock(&hash_resize_mutex);
530}
531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532/* Generate new index... KAME seems to generate them ordered by cost
533 * of an absolute inpredictability of ordering of rules. This will not pass. */
Fan Due682adf2013-11-07 17:47:48 +0800534static u32 xfrm_gen_index(struct net *net, int dir, u32 index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 static u32 idx_generator;
537
538 for (;;) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700539 struct hlist_head *list;
540 struct xfrm_policy *p;
541 u32 idx;
542 int found;
543
Fan Due682adf2013-11-07 17:47:48 +0800544 if (!index) {
545 idx = (idx_generator | dir);
546 idx_generator += 8;
547 } else {
548 idx = index;
549 index = 0;
550 }
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 if (idx == 0)
553 idx = 8;
Alexey Dobriyan11219942008-11-25 17:33:06 -0800554 list = net->xfrm.policy_byidx + idx_hash(net, idx);
David S. Miller2518c7c2006-08-24 04:45:07 -0700555 found = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800556 hlist_for_each_entry(p, list, byidx) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700557 if (p->index == idx) {
558 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 break;
David S. Miller2518c7c2006-08-24 04:45:07 -0700560 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700562 if (!found)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 return idx;
564 }
565}
566
David S. Miller2518c7c2006-08-24 04:45:07 -0700567static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
568{
569 u32 *p1 = (u32 *) s1;
570 u32 *p2 = (u32 *) s2;
571 int len = sizeof(struct xfrm_selector) / sizeof(u32);
572 int i;
573
574 for (i = 0; i < len; i++) {
575 if (p1[i] != p2[i])
576 return 1;
577 }
578
579 return 0;
580}
581
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100582static void xfrm_policy_requeue(struct xfrm_policy *old,
583 struct xfrm_policy *new)
584{
585 struct xfrm_policy_queue *pq = &old->polq;
586 struct sk_buff_head list;
587
588 __skb_queue_head_init(&list);
589
590 spin_lock_bh(&pq->hold_queue.lock);
591 skb_queue_splice_init(&pq->hold_queue, &list);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200592 if (del_timer(&pq->hold_timer))
593 xfrm_pol_put(old);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100594 spin_unlock_bh(&pq->hold_queue.lock);
595
596 if (skb_queue_empty(&list))
597 return;
598
599 pq = &new->polq;
600
601 spin_lock_bh(&pq->hold_queue.lock);
602 skb_queue_splice(&list, &pq->hold_queue);
603 pq->timeout = XFRM_QUEUE_TMO_MIN;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +0200604 if (!mod_timer(&pq->hold_timer, jiffies))
605 xfrm_pol_hold(new);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100606 spin_unlock_bh(&pq->hold_queue.lock);
607}
608
Steffen Klassert7cb8a932013-02-11 07:02:36 +0100609static bool xfrm_policy_mark_match(struct xfrm_policy *policy,
610 struct xfrm_policy *pol)
611{
612 u32 mark = policy->mark.v & policy->mark.m;
613
614 if (policy->mark.v == pol->mark.v && policy->mark.m == pol->mark.m)
615 return true;
616
617 if ((mark & pol->mark.m) == pol->mark.v &&
618 policy->priority == pol->priority)
619 return true;
620
621 return false;
622}
623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
625{
Alexey Dobriyan11219942008-11-25 17:33:06 -0800626 struct net *net = xp_net(policy);
David S. Miller2518c7c2006-08-24 04:45:07 -0700627 struct xfrm_policy *pol;
628 struct xfrm_policy *delpol;
629 struct hlist_head *chain;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800630 struct hlist_node *newpos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Fan Du283bc9f2013-11-07 17:47:50 +0800632 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800633 chain = policy_hash_bysel(net, &policy->selector, policy->family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700634 delpol = NULL;
635 newpos = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800636 hlist_for_each_entry(pol, chain, bydst) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800637 if (pol->type == policy->type &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700638 !selector_cmp(&pol->selector, &policy->selector) &&
Steffen Klassert7cb8a932013-02-11 07:02:36 +0100639 xfrm_policy_mark_match(policy, pol) &&
Herbert Xua6c7ab52007-01-16 16:52:02 -0800640 xfrm_sec_ctx_match(pol->security, policy->security) &&
641 !WARN_ON(delpol)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 if (excl) {
Fan Du283bc9f2013-11-07 17:47:50 +0800643 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 return -EEXIST;
645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 delpol = pol;
647 if (policy->priority > pol->priority)
648 continue;
649 } else if (policy->priority >= pol->priority) {
Herbert Xua6c7ab52007-01-16 16:52:02 -0800650 newpos = &pol->bydst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 continue;
652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 if (delpol)
654 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 }
656 if (newpos)
David S. Miller2518c7c2006-08-24 04:45:07 -0700657 hlist_add_after(newpos, &policy->bydst);
658 else
659 hlist_add_head(&policy->bydst, chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 xfrm_pol_hold(policy);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800661 net->xfrm.policy_count[dir]++;
Fan Duca925cf2014-01-18 09:55:27 +0800662 atomic_inc(&net->xfrm.flow_cache_genid);
fan.duca4c3fc2013-07-30 08:33:53 +0800663
664 /* After previous checking, family can either be AF_INET or AF_INET6 */
665 if (policy->family == AF_INET)
666 rt_genid_bump_ipv4(net);
667 else
668 rt_genid_bump_ipv6(net);
669
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100670 if (delpol) {
671 xfrm_policy_requeue(delpol, policy);
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800672 __xfrm_policy_unlink(delpol, dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +0100673 }
Fan Due682adf2013-11-07 17:47:48 +0800674 policy->index = delpol ? delpol->index : xfrm_gen_index(net, dir, policy->index);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800675 hlist_add_head(&policy->byidx, net->xfrm.policy_byidx+idx_hash(net, policy->index));
James Morris9d729f72007-03-04 16:12:44 -0800676 policy->curlft.add_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 policy->curlft.use_time = 0;
678 if (!mod_timer(&policy->timer, jiffies + HZ))
679 xfrm_pol_hold(policy);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800680 list_add(&policy->walk.all, &net->xfrm.policy_all);
Fan Du283bc9f2013-11-07 17:47:50 +0800681 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
David S. Miller9b78a822005-12-22 07:39:48 -0800683 if (delpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 xfrm_policy_kill(delpol);
Alexey Dobriyan11219942008-11-25 17:33:06 -0800685 else if (xfrm_bydst_should_resize(net, dir, NULL))
686 schedule_work(&net->xfrm.policy_hash_work);
David S. Miller9b78a822005-12-22 07:39:48 -0800687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 return 0;
689}
690EXPORT_SYMBOL(xfrm_policy_insert);
691
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +0000692struct xfrm_policy *xfrm_policy_bysel_ctx(struct net *net, u32 mark, u8 type,
693 int dir, struct xfrm_selector *sel,
Eric Parisef41aaa2007-03-07 15:37:58 -0800694 struct xfrm_sec_ctx *ctx, int delete,
695 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
David S. Miller2518c7c2006-08-24 04:45:07 -0700697 struct xfrm_policy *pol, *ret;
698 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Eric Parisef41aaa2007-03-07 15:37:58 -0800700 *err = 0;
Fan Du283bc9f2013-11-07 17:47:50 +0800701 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800702 chain = policy_hash_bysel(net, sel, sel->family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700703 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800704 hlist_for_each_entry(pol, chain, bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700705 if (pol->type == type &&
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000706 (mark & pol->mark.m) == pol->mark.v &&
David S. Miller2518c7c2006-08-24 04:45:07 -0700707 !selector_cmp(sel, &pol->selector) &&
708 xfrm_sec_ctx_match(ctx, pol->security)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700710 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -0700711 *err = security_xfrm_policy_delete(
712 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -0800713 if (*err) {
Fan Du283bc9f2013-11-07 17:47:50 +0800714 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Eric Parisef41aaa2007-03-07 15:37:58 -0800715 return pol;
716 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800717 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700718 }
719 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 break;
721 }
722 }
Fan Du283bc9f2013-11-07 17:47:50 +0800723 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000725 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -0700726 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -0700727 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728}
Trent Jaegerdf718372005-12-13 23:12:27 -0800729EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Jamal Hadi Salim8ca2e932010-02-22 11:32:57 +0000731struct xfrm_policy *xfrm_policy_byid(struct net *net, u32 mark, u8 type,
732 int dir, u32 id, int delete, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733{
David S. Miller2518c7c2006-08-24 04:45:07 -0700734 struct xfrm_policy *pol, *ret;
735 struct hlist_head *chain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Herbert Xub5505c62007-05-14 02:15:47 -0700737 *err = -ENOENT;
738 if (xfrm_policy_id2dir(id) != dir)
739 return NULL;
740
Eric Parisef41aaa2007-03-07 15:37:58 -0800741 *err = 0;
Fan Du283bc9f2013-11-07 17:47:50 +0800742 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan8d1211a2008-11-25 17:34:20 -0800743 chain = net->xfrm.policy_byidx + idx_hash(net, id);
David S. Miller2518c7c2006-08-24 04:45:07 -0700744 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800745 hlist_for_each_entry(pol, chain, byidx) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +0000746 if (pol->type == type && pol->index == id &&
747 (mark & pol->mark.m) == pol->mark.v) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -0700749 if (delete) {
Paul Moore03e1ad72008-04-12 19:07:52 -0700750 *err = security_xfrm_policy_delete(
751 pol->security);
Eric Parisef41aaa2007-03-07 15:37:58 -0800752 if (*err) {
Fan Du283bc9f2013-11-07 17:47:50 +0800753 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Eric Parisef41aaa2007-03-07 15:37:58 -0800754 return pol;
755 }
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800756 __xfrm_policy_unlink(pol, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700757 }
758 ret = pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 break;
760 }
761 }
Fan Du283bc9f2013-11-07 17:47:50 +0800762 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Timo Teräsfe1a5f02010-04-07 00:30:04 +0000764 if (ret && delete)
David S. Miller2518c7c2006-08-24 04:45:07 -0700765 xfrm_policy_kill(ret);
David S. Miller2518c7c2006-08-24 04:45:07 -0700766 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767}
768EXPORT_SYMBOL(xfrm_policy_byid);
769
Joy Latten4aa2e622007-06-04 19:05:57 -0400770#ifdef CONFIG_SECURITY_NETWORK_XFRM
771static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900772xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
Joy Latten4aa2e622007-06-04 19:05:57 -0400774 int dir, err = 0;
775
776 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
777 struct xfrm_policy *pol;
Joy Latten4aa2e622007-06-04 19:05:57 -0400778 int i;
779
Sasha Levinb67bfe02013-02-27 17:06:00 -0800780 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800781 &net->xfrm.policy_inexact[dir], bydst) {
Joy Latten4aa2e622007-06-04 19:05:57 -0400782 if (pol->type != type)
783 continue;
Paul Moore03e1ad72008-04-12 19:07:52 -0700784 err = security_xfrm_policy_delete(pol->security);
Joy Latten4aa2e622007-06-04 19:05:57 -0400785 if (err) {
Tetsuo Handa2e710292014-04-22 21:48:30 +0900786 xfrm_audit_policy_delete(pol, 0, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400787 return err;
788 }
YOSHIFUJI Hideaki7dc12d62007-07-19 10:45:15 +0900789 }
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800790 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800791 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800792 net->xfrm.policy_bydst[dir].table + i,
Joy Latten4aa2e622007-06-04 19:05:57 -0400793 bydst) {
794 if (pol->type != type)
795 continue;
Paul Moore03e1ad72008-04-12 19:07:52 -0700796 err = security_xfrm_policy_delete(
797 pol->security);
Joy Latten4aa2e622007-06-04 19:05:57 -0400798 if (err) {
Joy Lattenab5f5e82007-09-17 11:51:22 -0700799 xfrm_audit_policy_delete(pol, 0,
Tetsuo Handa2e710292014-04-22 21:48:30 +0900800 task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400801 return err;
802 }
803 }
804 }
805 }
806 return err;
807}
808#else
809static inline int
Tetsuo Handa2e710292014-04-22 21:48:30 +0900810xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -0400811{
812 return 0;
813}
814#endif
815
Tetsuo Handa2e710292014-04-22 21:48:30 +0900816int xfrm_policy_flush(struct net *net, u8 type, bool task_valid)
Joy Latten4aa2e622007-06-04 19:05:57 -0400817{
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +0000818 int dir, err = 0, cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Fan Du283bc9f2013-11-07 17:47:50 +0800820 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400821
Tetsuo Handa2e710292014-04-22 21:48:30 +0900822 err = xfrm_policy_flush_secctx_check(net, type, task_valid);
Joy Latten4aa2e622007-06-04 19:05:57 -0400823 if (err)
824 goto out;
825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700827 struct xfrm_policy *pol;
Wei Yongjun29fa0b302008-12-03 00:33:09 -0800828 int i;
David S. Miller2518c7c2006-08-24 04:45:07 -0700829
830 again1:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800831 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800832 &net->xfrm.policy_inexact[dir], bydst) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700833 if (pol->type != type)
834 continue;
Timo Teräsea2dea92010-03-31 00:17:05 +0000835 __xfrm_policy_unlink(pol, dir);
Fan Du283bc9f2013-11-07 17:47:50 +0800836 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Timo Teräsea2dea92010-03-31 00:17:05 +0000837 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Tetsuo Handa2e710292014-04-22 21:48:30 +0900839 xfrm_audit_policy_delete(pol, 1, task_valid);
Joy Latten161a09e2006-11-27 13:11:54 -0600840
David S. Miller2518c7c2006-08-24 04:45:07 -0700841 xfrm_policy_kill(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Fan Du283bc9f2013-11-07 17:47:50 +0800843 write_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700844 goto again1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 }
David S. Miller2518c7c2006-08-24 04:45:07 -0700846
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800847 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
David S. Miller2518c7c2006-08-24 04:45:07 -0700848 again2:
Sasha Levinb67bfe02013-02-27 17:06:00 -0800849 hlist_for_each_entry(pol,
Alexey Dobriyan33ffbbd2008-11-25 17:33:32 -0800850 net->xfrm.policy_bydst[dir].table + i,
David S. Miller2518c7c2006-08-24 04:45:07 -0700851 bydst) {
852 if (pol->type != type)
853 continue;
Timo Teräsea2dea92010-03-31 00:17:05 +0000854 __xfrm_policy_unlink(pol, dir);
Fan Du283bc9f2013-11-07 17:47:50 +0800855 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Timo Teräsea2dea92010-03-31 00:17:05 +0000856 cnt++;
David S. Miller2518c7c2006-08-24 04:45:07 -0700857
Tetsuo Handa2e710292014-04-22 21:48:30 +0900858 xfrm_audit_policy_delete(pol, 1, task_valid);
David S. Miller2518c7c2006-08-24 04:45:07 -0700859 xfrm_policy_kill(pol);
860
Fan Du283bc9f2013-11-07 17:47:50 +0800861 write_lock_bh(&net->xfrm.xfrm_policy_lock);
David S. Miller2518c7c2006-08-24 04:45:07 -0700862 goto again2;
863 }
864 }
865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 }
Jamal Hadi Salim2f1eb652010-02-19 02:00:42 +0000867 if (!cnt)
868 err = -ESRCH;
Joy Latten4aa2e622007-06-04 19:05:57 -0400869out:
Fan Du283bc9f2013-11-07 17:47:50 +0800870 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Joy Latten4aa2e622007-06-04 19:05:57 -0400871 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872}
873EXPORT_SYMBOL(xfrm_policy_flush);
874
Alexey Dobriyancdcbca72008-11-25 17:34:49 -0800875int xfrm_policy_walk(struct net *net, struct xfrm_policy_walk *walk,
Timo Teras4c563f72008-02-28 21:31:08 -0800876 int (*func)(struct xfrm_policy *, int, int, void*),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 void *data)
878{
Herbert Xu12a169e2008-10-01 07:03:24 -0700879 struct xfrm_policy *pol;
880 struct xfrm_policy_walk_entry *x;
Timo Teras4c563f72008-02-28 21:31:08 -0800881 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Timo Teras4c563f72008-02-28 21:31:08 -0800883 if (walk->type >= XFRM_POLICY_TYPE_MAX &&
884 walk->type != XFRM_POLICY_TYPE_ANY)
885 return -EINVAL;
886
Herbert Xu12a169e2008-10-01 07:03:24 -0700887 if (list_empty(&walk->walk.all) && walk->seq != 0)
Timo Teras4c563f72008-02-28 21:31:08 -0800888 return 0;
889
Fan Du283bc9f2013-11-07 17:47:50 +0800890 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -0700891 if (list_empty(&walk->walk.all))
Alexey Dobriyancdcbca72008-11-25 17:34:49 -0800892 x = list_first_entry(&net->xfrm.policy_all, struct xfrm_policy_walk_entry, all);
Herbert Xu12a169e2008-10-01 07:03:24 -0700893 else
894 x = list_entry(&walk->walk.all, struct xfrm_policy_walk_entry, all);
Alexey Dobriyancdcbca72008-11-25 17:34:49 -0800895 list_for_each_entry_from(x, &net->xfrm.policy_all, all) {
Herbert Xu12a169e2008-10-01 07:03:24 -0700896 if (x->dead)
Timo Teras4c563f72008-02-28 21:31:08 -0800897 continue;
Herbert Xu12a169e2008-10-01 07:03:24 -0700898 pol = container_of(x, struct xfrm_policy, walk);
899 if (walk->type != XFRM_POLICY_TYPE_ANY &&
900 walk->type != pol->type)
901 continue;
902 error = func(pol, xfrm_policy_id2dir(pol->index),
903 walk->seq, data);
904 if (error) {
905 list_move_tail(&walk->walk.all, &x->all);
906 goto out;
Timo Teras4c563f72008-02-28 21:31:08 -0800907 }
Herbert Xu12a169e2008-10-01 07:03:24 -0700908 walk->seq++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 }
Herbert Xu12a169e2008-10-01 07:03:24 -0700910 if (walk->seq == 0) {
Jamal Hadi Salimbaf5d742006-12-04 20:02:37 -0800911 error = -ENOENT;
912 goto out;
913 }
Herbert Xu12a169e2008-10-01 07:03:24 -0700914 list_del_init(&walk->walk.all);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915out:
Fan Du283bc9f2013-11-07 17:47:50 +0800916 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 return error;
918}
919EXPORT_SYMBOL(xfrm_policy_walk);
920
Herbert Xu12a169e2008-10-01 07:03:24 -0700921void xfrm_policy_walk_init(struct xfrm_policy_walk *walk, u8 type)
922{
923 INIT_LIST_HEAD(&walk->walk.all);
924 walk->walk.dead = 1;
925 walk->type = type;
926 walk->seq = 0;
927}
928EXPORT_SYMBOL(xfrm_policy_walk_init);
929
Fan Du283bc9f2013-11-07 17:47:50 +0800930void xfrm_policy_walk_done(struct xfrm_policy_walk *walk, struct net *net)
Herbert Xu12a169e2008-10-01 07:03:24 -0700931{
932 if (list_empty(&walk->walk.all))
933 return;
934
Fan Du283bc9f2013-11-07 17:47:50 +0800935 write_lock_bh(&net->xfrm.xfrm_policy_lock); /*FIXME where is net? */
Herbert Xu12a169e2008-10-01 07:03:24 -0700936 list_del(&walk->walk.all);
Fan Du283bc9f2013-11-07 17:47:50 +0800937 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Herbert Xu12a169e2008-10-01 07:03:24 -0700938}
939EXPORT_SYMBOL(xfrm_policy_walk_done);
940
James Morris134b0fc2006-10-05 15:42:27 -0500941/*
942 * Find policy to apply to this flow.
943 *
944 * Returns 0 if policy found, else an -errno.
945 */
David S. Millerf299d552011-02-24 01:23:30 -0500946static int xfrm_policy_match(const struct xfrm_policy *pol,
947 const struct flowi *fl,
David S. Miller2518c7c2006-08-24 04:45:07 -0700948 u8 type, u16 family, int dir)
949{
David S. Millerf299d552011-02-24 01:23:30 -0500950 const struct xfrm_selector *sel = &pol->selector;
David S. Millerbc9b35a2012-05-15 15:04:57 -0400951 int ret = -ESRCH;
952 bool match;
David S. Miller2518c7c2006-08-24 04:45:07 -0700953
954 if (pol->family != family ||
David S. Miller1d28f422011-03-12 00:29:39 -0500955 (fl->flowi_mark & pol->mark.m) != pol->mark.v ||
David S. Miller2518c7c2006-08-24 04:45:07 -0700956 pol->type != type)
James Morris134b0fc2006-10-05 15:42:27 -0500957 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -0700958
959 match = xfrm_selector_match(sel, fl, family);
James Morris134b0fc2006-10-05 15:42:27 -0500960 if (match)
David S. Miller1d28f422011-03-12 00:29:39 -0500961 ret = security_xfrm_policy_lookup(pol->security, fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -0700962 dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700963
James Morris134b0fc2006-10-05 15:42:27 -0500964 return ret;
David S. Miller2518c7c2006-08-24 04:45:07 -0700965}
966
Alexey Dobriyan52479b62008-11-25 17:35:18 -0800967static struct xfrm_policy *xfrm_policy_lookup_bytype(struct net *net, u8 type,
David S. Miller062cdb42011-02-22 18:31:08 -0800968 const struct flowi *fl,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -0700969 u16 family, u8 dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970{
James Morris134b0fc2006-10-05 15:42:27 -0500971 int err;
David S. Miller2518c7c2006-08-24 04:45:07 -0700972 struct xfrm_policy *pol, *ret;
David S. Miller0b597e72011-02-24 01:22:48 -0500973 const xfrm_address_t *daddr, *saddr;
David S. Miller2518c7c2006-08-24 04:45:07 -0700974 struct hlist_head *chain;
David S. Milleracba48e2006-08-25 15:46:46 -0700975 u32 priority = ~0U;
David S. Miller2518c7c2006-08-24 04:45:07 -0700976
977 daddr = xfrm_flowi_daddr(fl, family);
978 saddr = xfrm_flowi_saddr(fl, family);
979 if (unlikely(!daddr || !saddr))
980 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Fan Du283bc9f2013-11-07 17:47:50 +0800982 read_lock_bh(&net->xfrm.xfrm_policy_lock);
Alexey Dobriyan52479b62008-11-25 17:35:18 -0800983 chain = policy_hash_direct(net, daddr, saddr, family, dir);
David S. Miller2518c7c2006-08-24 04:45:07 -0700984 ret = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800985 hlist_for_each_entry(pol, chain, bydst) {
James Morris134b0fc2006-10-05 15:42:27 -0500986 err = xfrm_policy_match(pol, fl, type, family, dir);
987 if (err) {
988 if (err == -ESRCH)
989 continue;
990 else {
991 ret = ERR_PTR(err);
992 goto fail;
993 }
994 } else {
David S. Milleracba48e2006-08-25 15:46:46 -0700995 ret = pol;
996 priority = ret->priority;
997 break;
998 }
999 }
Alexey Dobriyan52479b62008-11-25 17:35:18 -08001000 chain = &net->xfrm.policy_inexact[dir];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001001 hlist_for_each_entry(pol, chain, bydst) {
James Morris134b0fc2006-10-05 15:42:27 -05001002 err = xfrm_policy_match(pol, fl, type, family, dir);
1003 if (err) {
1004 if (err == -ESRCH)
1005 continue;
1006 else {
1007 ret = ERR_PTR(err);
1008 goto fail;
1009 }
1010 } else if (pol->priority < priority) {
David S. Miller2518c7c2006-08-24 04:45:07 -07001011 ret = pol;
1012 break;
1013 }
1014 }
David S. Milleracba48e2006-08-25 15:46:46 -07001015 if (ret)
1016 xfrm_pol_hold(ret);
James Morris134b0fc2006-10-05 15:42:27 -05001017fail:
Fan Du283bc9f2013-11-07 17:47:50 +08001018 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001019
David S. Miller2518c7c2006-08-24 04:45:07 -07001020 return ret;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001021}
1022
Timo Teräs80c802f2010-04-07 00:30:05 +00001023static struct xfrm_policy *
David S. Miller73ff93c2011-02-22 18:33:42 -08001024__xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir)
Timo Teräs80c802f2010-04-07 00:30:05 +00001025{
1026#ifdef CONFIG_XFRM_SUB_POLICY
1027 struct xfrm_policy *pol;
1028
1029 pol = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_SUB, fl, family, dir);
1030 if (pol != NULL)
1031 return pol;
1032#endif
1033 return xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN, fl, family, dir);
1034}
1035
Baker Zhangb5fb82c2013-03-19 04:24:30 +00001036static int flow_to_policy_dir(int dir)
1037{
1038 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
1039 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1040 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1041 return dir;
1042
1043 switch (dir) {
1044 default:
1045 case FLOW_DIR_IN:
1046 return XFRM_POLICY_IN;
1047 case FLOW_DIR_OUT:
1048 return XFRM_POLICY_OUT;
1049 case FLOW_DIR_FWD:
1050 return XFRM_POLICY_FWD;
1051 }
1052}
1053
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001054static struct flow_cache_object *
David S. Millerdee9f4b2011-02-22 18:44:31 -08001055xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family,
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001056 u8 dir, struct flow_cache_object *old_obj, void *ctx)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001057{
1058 struct xfrm_policy *pol;
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001059
1060 if (old_obj)
1061 xfrm_pol_put(container_of(old_obj, struct xfrm_policy, flo));
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001062
Baker Zhangb5fb82c2013-03-19 04:24:30 +00001063 pol = __xfrm_policy_lookup(net, fl, family, flow_to_policy_dir(dir));
Timo Teräs80c802f2010-04-07 00:30:05 +00001064 if (IS_ERR_OR_NULL(pol))
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001065 return ERR_CAST(pol);
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001066
Timo Teräsfe1a5f02010-04-07 00:30:04 +00001067 /* Resolver returns two references:
1068 * one for cache and one for caller of flow_cache_lookup() */
1069 xfrm_pol_hold(pol);
1070
1071 return &pol->flo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072}
1073
Trent Jaegerdf718372005-12-13 23:12:27 -08001074static inline int policy_to_flow_dir(int dir)
1075{
1076 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001077 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1078 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1079 return dir;
1080 switch (dir) {
1081 default:
1082 case XFRM_POLICY_IN:
1083 return FLOW_DIR_IN;
1084 case XFRM_POLICY_OUT:
1085 return FLOW_DIR_OUT;
1086 case XFRM_POLICY_FWD:
1087 return FLOW_DIR_FWD;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001088 }
Trent Jaegerdf718372005-12-13 23:12:27 -08001089}
1090
David S. Millerdee9f4b2011-02-22 18:44:31 -08001091static struct xfrm_policy *xfrm_sk_policy_lookup(struct sock *sk, int dir,
1092 const struct flowi *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093{
1094 struct xfrm_policy *pol;
Fan Du283bc9f2013-11-07 17:47:50 +08001095 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
Fan Du283bc9f2013-11-07 17:47:50 +08001097 read_lock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 if ((pol = sk->sk_policy[dir]) != NULL) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04001099 bool match = xfrm_selector_match(&pol->selector, fl,
1100 sk->sk_family);
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09001101 int err = 0;
Trent Jaegerdf718372005-12-13 23:12:27 -08001102
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001103 if (match) {
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001104 if ((sk->sk_mark & pol->mark.m) != pol->mark.v) {
1105 pol = NULL;
1106 goto out;
1107 }
Paul Moore03e1ad72008-04-12 19:07:52 -07001108 err = security_xfrm_policy_lookup(pol->security,
David S. Miller1d28f422011-03-12 00:29:39 -05001109 fl->flowi_secid,
Paul Moore03e1ad72008-04-12 19:07:52 -07001110 policy_to_flow_dir(dir));
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05001111 if (!err)
1112 xfrm_pol_hold(pol);
1113 else if (err == -ESRCH)
1114 pol = NULL;
1115 else
1116 pol = ERR_PTR(err);
1117 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 pol = NULL;
1119 }
Jamal Hadi Salim34f8d882010-02-22 11:32:58 +00001120out:
Fan Du283bc9f2013-11-07 17:47:50 +08001121 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 return pol;
1123}
1124
1125static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
1126{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001127 struct net *net = xp_net(pol);
Alexey Dobriyan11219942008-11-25 17:33:06 -08001128 struct hlist_head *chain = policy_hash_bysel(net, &pol->selector,
David S. Miller2518c7c2006-08-24 04:45:07 -07001129 pol->family, dir);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001130
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001131 list_add(&pol->walk.all, &net->xfrm.policy_all);
David S. Miller2518c7c2006-08-24 04:45:07 -07001132 hlist_add_head(&pol->bydst, chain);
Alexey Dobriyane92303f2008-11-25 17:32:41 -08001133 hlist_add_head(&pol->byidx, net->xfrm.policy_byidx+idx_hash(net, pol->index));
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001134 net->xfrm.policy_count[dir]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 xfrm_pol_hold(pol);
David S. Miller2518c7c2006-08-24 04:45:07 -07001136
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001137 if (xfrm_bydst_should_resize(net, dir, NULL))
1138 schedule_work(&net->xfrm.policy_hash_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139}
1140
1141static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
1142 int dir)
1143{
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001144 struct net *net = xp_net(pol);
1145
David S. Miller2518c7c2006-08-24 04:45:07 -07001146 if (hlist_unhashed(&pol->bydst))
1147 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
Steffen Klassert3a9016f2014-02-19 10:07:34 +01001149 hlist_del_init(&pol->bydst);
David S. Miller2518c7c2006-08-24 04:45:07 -07001150 hlist_del(&pol->byidx);
Herbert Xu12a169e2008-10-01 07:03:24 -07001151 list_del(&pol->walk.all);
Alexey Dobriyan98806f72008-11-25 17:29:47 -08001152 net->xfrm.policy_count[dir]--;
David S. Miller2518c7c2006-08-24 04:45:07 -07001153
1154 return pol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155}
1156
Herbert Xu4666faa2005-06-18 22:43:22 -07001157int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158{
Fan Du283bc9f2013-11-07 17:47:50 +08001159 struct net *net = xp_net(pol);
1160
1161 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 pol = __xfrm_policy_unlink(pol, dir);
Fan Du283bc9f2013-11-07 17:47:50 +08001163 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 if (pol) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 xfrm_policy_kill(pol);
Herbert Xu4666faa2005-06-18 22:43:22 -07001166 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 }
Herbert Xu4666faa2005-06-18 22:43:22 -07001168 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169}
David S. Millera70fcb02006-03-20 19:18:52 -08001170EXPORT_SYMBOL(xfrm_policy_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
1172int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
1173{
Alexey Dobriyan11219942008-11-25 17:33:06 -08001174 struct net *net = xp_net(pol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 struct xfrm_policy *old_pol;
1176
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001177#ifdef CONFIG_XFRM_SUB_POLICY
1178 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
1179 return -EINVAL;
1180#endif
1181
Fan Du283bc9f2013-11-07 17:47:50 +08001182 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 old_pol = sk->sk_policy[dir];
1184 sk->sk_policy[dir] = pol;
1185 if (pol) {
James Morris9d729f72007-03-04 16:12:44 -08001186 pol->curlft.add_time = get_seconds();
Fan Due682adf2013-11-07 17:47:48 +08001187 pol->index = xfrm_gen_index(net, XFRM_POLICY_MAX+dir, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 __xfrm_policy_link(pol, XFRM_POLICY_MAX+dir);
1189 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001190 if (old_pol) {
1191 if (pol)
1192 xfrm_policy_requeue(old_pol, pol);
1193
Timo Teräsea2dea92010-03-31 00:17:05 +00001194 /* Unlinking succeeds always. This is the only function
1195 * allowed to delete or replace socket policy.
1196 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 __xfrm_policy_unlink(old_pol, XFRM_POLICY_MAX+dir);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001198 }
Fan Du283bc9f2013-11-07 17:47:50 +08001199 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
1201 if (old_pol) {
1202 xfrm_policy_kill(old_pol);
1203 }
1204 return 0;
1205}
1206
David S. Millerd3e40a92011-02-24 01:25:41 -05001207static struct xfrm_policy *clone_policy(const struct xfrm_policy *old, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208{
Alexey Dobriyan0331b1f2008-11-25 17:21:45 -08001209 struct xfrm_policy *newp = xfrm_policy_alloc(xp_net(old), GFP_ATOMIC);
Fan Du283bc9f2013-11-07 17:47:50 +08001210 struct net *net = xp_net(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
1212 if (newp) {
1213 newp->selector = old->selector;
Paul Moore03e1ad72008-04-12 19:07:52 -07001214 if (security_xfrm_policy_clone(old->security,
1215 &newp->security)) {
Trent Jaegerdf718372005-12-13 23:12:27 -08001216 kfree(newp);
1217 return NULL; /* ENOMEM */
1218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 newp->lft = old->lft;
1220 newp->curlft = old->curlft;
Jamal Hadi Salimfb977e22010-02-23 15:09:53 -08001221 newp->mark = old->mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 newp->action = old->action;
1223 newp->flags = old->flags;
1224 newp->xfrm_nr = old->xfrm_nr;
1225 newp->index = old->index;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001226 newp->type = old->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 memcpy(newp->xfrm_vec, old->xfrm_vec,
1228 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
Fan Du283bc9f2013-11-07 17:47:50 +08001229 write_lock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 __xfrm_policy_link(newp, XFRM_POLICY_MAX+dir);
Fan Du283bc9f2013-11-07 17:47:50 +08001231 write_unlock_bh(&net->xfrm.xfrm_policy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 xfrm_pol_put(newp);
1233 }
1234 return newp;
1235}
1236
1237int __xfrm_sk_clone_policy(struct sock *sk)
1238{
1239 struct xfrm_policy *p0 = sk->sk_policy[0],
1240 *p1 = sk->sk_policy[1];
1241
1242 sk->sk_policy[0] = sk->sk_policy[1] = NULL;
1243 if (p0 && (sk->sk_policy[0] = clone_policy(p0, 0)) == NULL)
1244 return -ENOMEM;
1245 if (p1 && (sk->sk_policy[1] = clone_policy(p1, 1)) == NULL)
1246 return -ENOMEM;
1247 return 0;
1248}
1249
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001250static int
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001251xfrm_get_saddr(struct net *net, xfrm_address_t *local, xfrm_address_t *remote,
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001252 unsigned short family)
1253{
1254 int err;
1255 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1256
1257 if (unlikely(afinfo == NULL))
1258 return -EINVAL;
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001259 err = afinfo->get_saddr(net, local, remote);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001260 xfrm_policy_put_afinfo(afinfo);
1261 return err;
1262}
1263
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264/* Resolve list of templates for the flow, given policy. */
1265
1266static int
David S. Millera6c2e612011-02-22 18:35:39 -08001267xfrm_tmpl_resolve_one(struct xfrm_policy *policy, const struct flowi *fl,
1268 struct xfrm_state **xfrm, unsigned short family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269{
Alexey Dobriyanfbda33b2008-11-25 17:56:49 -08001270 struct net *net = xp_net(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 int nx;
1272 int i, error;
1273 xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
1274 xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001275 xfrm_address_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
Weilong Chen9b7a7872013-12-24 09:43:46 +08001277 for (nx = 0, i = 0; i < policy->xfrm_nr; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 struct xfrm_state *x;
1279 xfrm_address_t *remote = daddr;
1280 xfrm_address_t *local = saddr;
1281 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
1282
Joakim Koskela48b8d782007-07-26 00:08:42 -07001283 if (tmpl->mode == XFRM_MODE_TUNNEL ||
1284 tmpl->mode == XFRM_MODE_BEET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 remote = &tmpl->id.daddr;
1286 local = &tmpl->saddr;
Thomas Egerer8444cf72010-09-20 11:11:38 -07001287 if (xfrm_addr_any(local, tmpl->encap_family)) {
1288 error = xfrm_get_saddr(net, &tmp, remote, tmpl->encap_family);
Patrick McHardya1e59ab2006-09-19 12:57:34 -07001289 if (error)
1290 goto fail;
1291 local = &tmp;
1292 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 }
1294
1295 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
1296
1297 if (x && x->km.state == XFRM_STATE_VALID) {
1298 xfrm[nx++] = x;
1299 daddr = remote;
1300 saddr = local;
1301 continue;
1302 }
1303 if (x) {
1304 error = (x->km.state == XFRM_STATE_ERROR ?
1305 -EINVAL : -EAGAIN);
1306 xfrm_state_put(x);
Weilong Chen420545692013-12-24 09:43:49 +08001307 } else if (error == -ESRCH) {
fernando@oss.ntt.coa43222662008-10-23 04:27:19 +00001308 error = -EAGAIN;
Weilong Chen420545692013-12-24 09:43:49 +08001309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
1311 if (!tmpl->optional)
1312 goto fail;
1313 }
1314 return nx;
1315
1316fail:
Weilong Chen9b7a7872013-12-24 09:43:46 +08001317 for (nx--; nx >= 0; nx--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 xfrm_state_put(xfrm[nx]);
1319 return error;
1320}
1321
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001322static int
David S. Millera6c2e612011-02-22 18:35:39 -08001323xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, const struct flowi *fl,
1324 struct xfrm_state **xfrm, unsigned short family)
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001325{
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001326 struct xfrm_state *tp[XFRM_MAX_DEPTH];
1327 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001328 int cnx = 0;
1329 int error;
1330 int ret;
1331 int i;
1332
1333 for (i = 0; i < npols; i++) {
1334 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
1335 error = -ENOBUFS;
1336 goto fail;
1337 }
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001338
1339 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001340 if (ret < 0) {
1341 error = ret;
1342 goto fail;
1343 } else
1344 cnx += ret;
1345 }
1346
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001347 /* found states are sorted for outbound processing */
1348 if (npols > 1)
1349 xfrm_state_sort(xfrm, tpp, cnx, family);
1350
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001351 return cnx;
1352
1353 fail:
Weilong Chen9b7a7872013-12-24 09:43:46 +08001354 for (cnx--; cnx >= 0; cnx--)
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07001355 xfrm_state_put(tpp[cnx]);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07001356 return error;
1357
1358}
1359
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360/* Check that the bundle accepts the flow and its components are
1361 * still valid.
1362 */
1363
David S. Miller05d84022011-02-22 17:47:10 -08001364static inline int xfrm_get_tos(const struct flowi *fl, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08001365{
1366 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1367 int tos;
1368
1369 if (!afinfo)
1370 return -EINVAL;
1371
1372 tos = afinfo->get_tos(fl);
1373
1374 xfrm_policy_put_afinfo(afinfo);
1375
1376 return tos;
1377}
1378
Timo Teräs80c802f2010-04-07 00:30:05 +00001379static struct flow_cache_object *xfrm_bundle_flo_get(struct flow_cache_object *flo)
1380{
1381 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1382 struct dst_entry *dst = &xdst->u.dst;
1383
1384 if (xdst->route == NULL) {
1385 /* Dummy bundle - if it has xfrms we were not
1386 * able to build bundle as template resolution failed.
1387 * It means we need to try again resolving. */
1388 if (xdst->num_xfrms > 0)
1389 return NULL;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001390 } else if (dst->flags & DST_XFRM_QUEUE) {
1391 return NULL;
Timo Teräs80c802f2010-04-07 00:30:05 +00001392 } else {
1393 /* Real bundle */
1394 if (stale_bundle(dst))
1395 return NULL;
1396 }
1397
1398 dst_hold(dst);
1399 return flo;
1400}
1401
1402static int xfrm_bundle_flo_check(struct flow_cache_object *flo)
1403{
1404 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1405 struct dst_entry *dst = &xdst->u.dst;
1406
1407 if (!xdst->route)
1408 return 0;
1409 if (stale_bundle(dst))
1410 return 0;
1411
1412 return 1;
1413}
1414
1415static void xfrm_bundle_flo_delete(struct flow_cache_object *flo)
1416{
1417 struct xfrm_dst *xdst = container_of(flo, struct xfrm_dst, flo);
1418 struct dst_entry *dst = &xdst->u.dst;
1419
1420 dst_free(dst);
1421}
1422
1423static const struct flow_cache_ops xfrm_bundle_fc_ops = {
1424 .get = xfrm_bundle_flo_get,
1425 .check = xfrm_bundle_flo_check,
1426 .delete = xfrm_bundle_flo_delete,
1427};
1428
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001429static inline struct xfrm_dst *xfrm_alloc_dst(struct net *net, int family)
Herbert Xu25ee3282007-12-11 09:32:34 -08001430{
1431 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001432 struct dst_ops *dst_ops;
Herbert Xu25ee3282007-12-11 09:32:34 -08001433 struct xfrm_dst *xdst;
1434
1435 if (!afinfo)
1436 return ERR_PTR(-EINVAL);
1437
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001438 switch (family) {
1439 case AF_INET:
1440 dst_ops = &net->xfrm.xfrm4_dst_ops;
1441 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001442#if IS_ENABLED(CONFIG_IPV6)
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001443 case AF_INET6:
1444 dst_ops = &net->xfrm.xfrm6_dst_ops;
1445 break;
1446#endif
1447 default:
1448 BUG();
1449 }
David S. Millerf5b0a872012-07-19 12:31:33 -07001450 xdst = dst_alloc(dst_ops, NULL, 0, DST_OBSOLETE_NONE, 0);
Herbert Xu25ee3282007-12-11 09:32:34 -08001451
Madalin Bucurd4cae562011-09-26 07:04:36 +00001452 if (likely(xdst)) {
Steffen Klassert141e3692012-07-05 23:39:34 +00001453 struct dst_entry *dst = &xdst->u.dst;
1454
1455 memset(dst + 1, 0, sizeof(*xdst) - sizeof(*dst));
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08001456 xdst->flo.ops = &xfrm_bundle_fc_ops;
Patrick McHardy9d7b0fc2012-08-20 02:56:56 -07001457 if (afinfo->init_dst)
1458 afinfo->init_dst(net, xdst);
Madalin Bucurd4cae562011-09-26 07:04:36 +00001459 } else
Hiroaki SHIMODA0b150932011-02-10 23:08:33 -08001460 xdst = ERR_PTR(-ENOBUFS);
Timo Teräs80c802f2010-04-07 00:30:05 +00001461
Madalin Bucurd4cae562011-09-26 07:04:36 +00001462 xfrm_policy_put_afinfo(afinfo);
1463
Herbert Xu25ee3282007-12-11 09:32:34 -08001464 return xdst;
1465}
1466
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001467static inline int xfrm_init_path(struct xfrm_dst *path, struct dst_entry *dst,
1468 int nfheader_len)
1469{
1470 struct xfrm_policy_afinfo *afinfo =
1471 xfrm_policy_get_afinfo(dst->ops->family);
1472 int err;
1473
1474 if (!afinfo)
1475 return -EINVAL;
1476
1477 err = afinfo->init_path(path, dst, nfheader_len);
1478
1479 xfrm_policy_put_afinfo(afinfo);
1480
1481 return err;
1482}
1483
Herbert Xu87c1e122010-03-02 02:51:56 +00001484static inline int xfrm_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
David S. Miller0c7b3ee2011-02-22 17:48:57 -08001485 const struct flowi *fl)
Herbert Xu25ee3282007-12-11 09:32:34 -08001486{
1487 struct xfrm_policy_afinfo *afinfo =
1488 xfrm_policy_get_afinfo(xdst->u.dst.ops->family);
1489 int err;
1490
1491 if (!afinfo)
1492 return -EINVAL;
1493
Herbert Xu87c1e122010-03-02 02:51:56 +00001494 err = afinfo->fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001495
1496 xfrm_policy_put_afinfo(afinfo);
1497
1498 return err;
1499}
1500
Timo Teräs80c802f2010-04-07 00:30:05 +00001501
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502/* Allocate chain of dst_entry's, attach known xfrm's, calculate
1503 * all the metrics... Shortly, bundle a bundle.
1504 */
1505
Herbert Xu25ee3282007-12-11 09:32:34 -08001506static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy,
1507 struct xfrm_state **xfrm, int nx,
David S. Miller98313ad2011-02-22 18:36:50 -08001508 const struct flowi *fl,
Herbert Xu25ee3282007-12-11 09:32:34 -08001509 struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510{
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001511 struct net *net = xp_net(policy);
Herbert Xu25ee3282007-12-11 09:32:34 -08001512 unsigned long now = jiffies;
1513 struct net_device *dev;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001514 struct xfrm_mode *inner_mode;
Herbert Xu25ee3282007-12-11 09:32:34 -08001515 struct dst_entry *dst_prev = NULL;
1516 struct dst_entry *dst0 = NULL;
1517 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 int err;
Herbert Xu25ee3282007-12-11 09:32:34 -08001519 int header_len = 0;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001520 int nfheader_len = 0;
Herbert Xu25ee3282007-12-11 09:32:34 -08001521 int trailer_len = 0;
1522 int tos;
1523 int family = policy->selector.family;
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +09001524 xfrm_address_t saddr, daddr;
1525
1526 xfrm_flowi_addr_get(fl, &saddr, &daddr, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001527
1528 tos = xfrm_get_tos(fl, family);
1529 err = tos;
1530 if (tos < 0)
1531 goto put_states;
1532
1533 dst_hold(dst);
1534
1535 for (; i < nx; i++) {
Alexey Dobriyand7c75442010-01-24 22:47:53 -08001536 struct xfrm_dst *xdst = xfrm_alloc_dst(net, family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001537 struct dst_entry *dst1 = &xdst->u.dst;
1538
1539 err = PTR_ERR(xdst);
1540 if (IS_ERR(xdst)) {
1541 dst_release(dst);
1542 goto put_states;
1543 }
1544
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001545 if (xfrm[i]->sel.family == AF_UNSPEC) {
1546 inner_mode = xfrm_ip2inner_mode(xfrm[i],
1547 xfrm_af2proto(family));
1548 if (!inner_mode) {
1549 err = -EAFNOSUPPORT;
1550 dst_release(dst);
1551 goto put_states;
1552 }
1553 } else
1554 inner_mode = xfrm[i]->inner_mode;
1555
Herbert Xu25ee3282007-12-11 09:32:34 -08001556 if (!dst_prev)
1557 dst0 = dst1;
1558 else {
1559 dst_prev->child = dst_clone(dst1);
1560 dst1->flags |= DST_NOHASH;
1561 }
1562
1563 xdst->route = dst;
David S. Millerdefb3512010-12-08 21:16:57 -08001564 dst_copy_metrics(dst1, dst);
Herbert Xu25ee3282007-12-11 09:32:34 -08001565
1566 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
1567 family = xfrm[i]->props.family;
YOSHIFUJI Hideaki9bb182a2008-02-22 14:48:22 +09001568 dst = xfrm_dst_lookup(xfrm[i], tos, &saddr, &daddr,
1569 family);
Herbert Xu25ee3282007-12-11 09:32:34 -08001570 err = PTR_ERR(dst);
1571 if (IS_ERR(dst))
1572 goto put_states;
1573 } else
1574 dst_hold(dst);
1575
1576 dst1->xfrm = xfrm[i];
Timo Teräs80c802f2010-04-07 00:30:05 +00001577 xdst->xfrm_genid = xfrm[i]->genid;
Herbert Xu25ee3282007-12-11 09:32:34 -08001578
David S. Millerf5b0a872012-07-19 12:31:33 -07001579 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
Herbert Xu25ee3282007-12-11 09:32:34 -08001580 dst1->flags |= DST_HOST;
1581 dst1->lastuse = now;
1582
1583 dst1->input = dst_discard;
Steffen Klassert43a4dea2011-05-09 19:36:38 +00001584 dst1->output = inner_mode->afinfo->output;
Herbert Xu25ee3282007-12-11 09:32:34 -08001585
1586 dst1->next = dst_prev;
1587 dst_prev = dst1;
1588
1589 header_len += xfrm[i]->props.header_len;
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001590 if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT)
1591 nfheader_len += xfrm[i]->props.header_len;
Herbert Xu25ee3282007-12-11 09:32:34 -08001592 trailer_len += xfrm[i]->props.trailer_len;
1593 }
1594
1595 dst_prev->child = dst;
1596 dst0->path = dst;
1597
1598 err = -ENODEV;
1599 dev = dst->dev;
1600 if (!dev)
1601 goto free_dst;
1602
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001603 xfrm_init_path((struct xfrm_dst *)dst0, dst, nfheader_len);
Herbert Xu25ee3282007-12-11 09:32:34 -08001604 xfrm_init_pmtu(dst_prev);
1605
1606 for (dst_prev = dst0; dst_prev != dst; dst_prev = dst_prev->child) {
1607 struct xfrm_dst *xdst = (struct xfrm_dst *)dst_prev;
1608
Herbert Xu87c1e122010-03-02 02:51:56 +00001609 err = xfrm_fill_dst(xdst, dev, fl);
Herbert Xu25ee3282007-12-11 09:32:34 -08001610 if (err)
1611 goto free_dst;
1612
1613 dst_prev->header_len = header_len;
1614 dst_prev->trailer_len = trailer_len;
1615 header_len -= xdst->u.dst.xfrm->props.header_len;
1616 trailer_len -= xdst->u.dst.xfrm->props.trailer_len;
1617 }
1618
1619out:
1620 return dst0;
1621
1622put_states:
1623 for (; i < nx; i++)
1624 xfrm_state_put(xfrm[i]);
1625free_dst:
1626 if (dst0)
1627 dst_free(dst0);
1628 dst0 = ERR_PTR(err);
1629 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630}
1631
Ying Xueda7c2242014-01-08 10:26:39 +08001632#ifdef CONFIG_XFRM_SUB_POLICY
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001633static int xfrm_dst_alloc_copy(void **target, const void *src, int size)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001634{
1635 if (!*target) {
1636 *target = kmalloc(size, GFP_ATOMIC);
1637 if (!*target)
1638 return -ENOMEM;
1639 }
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001640
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001641 memcpy(*target, src, size);
1642 return 0;
1643}
Ying Xueda7c2242014-01-08 10:26:39 +08001644#endif
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001645
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001646static int xfrm_dst_update_parent(struct dst_entry *dst,
1647 const struct xfrm_selector *sel)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001648{
1649#ifdef CONFIG_XFRM_SUB_POLICY
1650 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1651 return xfrm_dst_alloc_copy((void **)&(xdst->partner),
1652 sel, sizeof(*sel));
1653#else
1654 return 0;
1655#endif
1656}
1657
Daniel Borkmannbe7928d2014-01-07 23:20:27 +01001658static int xfrm_dst_update_origin(struct dst_entry *dst,
1659 const struct flowi *fl)
Masahide NAKAMURA157bfc22007-04-30 00:33:35 -07001660{
1661#ifdef CONFIG_XFRM_SUB_POLICY
1662 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1663 return xfrm_dst_alloc_copy((void **)&(xdst->origin), fl, sizeof(*fl));
1664#else
1665 return 0;
1666#endif
1667}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
David S. Miller73ff93c2011-02-22 18:33:42 -08001669static int xfrm_expand_policies(const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001670 struct xfrm_policy **pols,
1671 int *num_pols, int *num_xfrms)
1672{
1673 int i;
1674
1675 if (*num_pols == 0 || !pols[0]) {
1676 *num_pols = 0;
1677 *num_xfrms = 0;
1678 return 0;
1679 }
1680 if (IS_ERR(pols[0]))
1681 return PTR_ERR(pols[0]);
1682
1683 *num_xfrms = pols[0]->xfrm_nr;
1684
1685#ifdef CONFIG_XFRM_SUB_POLICY
1686 if (pols[0] && pols[0]->action == XFRM_POLICY_ALLOW &&
1687 pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1688 pols[1] = xfrm_policy_lookup_bytype(xp_net(pols[0]),
1689 XFRM_POLICY_TYPE_MAIN,
1690 fl, family,
1691 XFRM_POLICY_OUT);
1692 if (pols[1]) {
1693 if (IS_ERR(pols[1])) {
1694 xfrm_pols_put(pols, *num_pols);
1695 return PTR_ERR(pols[1]);
1696 }
Weilong Chen02d08922013-12-24 09:43:48 +08001697 (*num_pols)++;
Timo Teräs80c802f2010-04-07 00:30:05 +00001698 (*num_xfrms) += pols[1]->xfrm_nr;
1699 }
1700 }
1701#endif
1702 for (i = 0; i < *num_pols; i++) {
1703 if (pols[i]->action != XFRM_POLICY_ALLOW) {
1704 *num_xfrms = -1;
1705 break;
1706 }
1707 }
1708
1709 return 0;
1710
1711}
1712
1713static struct xfrm_dst *
1714xfrm_resolve_and_create_bundle(struct xfrm_policy **pols, int num_pols,
David S. Miller4ca2e682011-02-22 18:38:51 -08001715 const struct flowi *fl, u16 family,
Timo Teräs80c802f2010-04-07 00:30:05 +00001716 struct dst_entry *dst_orig)
1717{
1718 struct net *net = xp_net(pols[0]);
1719 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
1720 struct dst_entry *dst;
1721 struct xfrm_dst *xdst;
1722 int err;
1723
1724 /* Try to instantiate a bundle */
1725 err = xfrm_tmpl_resolve(pols, num_pols, fl, xfrm, family);
Timo Teräsd809ec82010-07-12 21:29:42 +00001726 if (err <= 0) {
1727 if (err != 0 && err != -EAGAIN)
Timo Teräs80c802f2010-04-07 00:30:05 +00001728 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
1729 return ERR_PTR(err);
1730 }
1731
1732 dst = xfrm_bundle_create(pols[0], xfrm, err, fl, dst_orig);
1733 if (IS_ERR(dst)) {
1734 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLEGENERROR);
1735 return ERR_CAST(dst);
1736 }
1737
1738 xdst = (struct xfrm_dst *)dst;
1739 xdst->num_xfrms = err;
1740 if (num_pols > 1)
1741 err = xfrm_dst_update_parent(dst, &pols[1]->selector);
1742 else
1743 err = xfrm_dst_update_origin(dst, fl);
1744 if (unlikely(err)) {
1745 dst_free(dst);
1746 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLECHECKERROR);
1747 return ERR_PTR(err);
1748 }
1749
1750 xdst->num_pols = num_pols;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08001751 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00001752 xdst->policy_genid = atomic_read(&pols[0]->genid);
1753
1754 return xdst;
1755}
1756
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001757static void xfrm_policy_queue_process(unsigned long arg)
1758{
1759 int err = 0;
1760 struct sk_buff *skb;
1761 struct sock *sk;
1762 struct dst_entry *dst;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001763 struct xfrm_policy *pol = (struct xfrm_policy *)arg;
1764 struct xfrm_policy_queue *pq = &pol->polq;
1765 struct flowi fl;
1766 struct sk_buff_head list;
1767
1768 spin_lock(&pq->hold_queue.lock);
1769 skb = skb_peek(&pq->hold_queue);
Steffen Klassert2bb53e22013-10-08 10:49:51 +02001770 if (!skb) {
1771 spin_unlock(&pq->hold_queue.lock);
1772 goto out;
1773 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001774 dst = skb_dst(skb);
1775 sk = skb->sk;
1776 xfrm_decode_session(skb, &fl, dst->ops->family);
1777 spin_unlock(&pq->hold_queue.lock);
1778
1779 dst_hold(dst->path);
1780 dst = xfrm_lookup(xp_net(pol), dst->path, &fl,
1781 sk, 0);
1782 if (IS_ERR(dst))
1783 goto purge_queue;
1784
1785 if (dst->flags & DST_XFRM_QUEUE) {
1786 dst_release(dst);
1787
1788 if (pq->timeout >= XFRM_QUEUE_TMO_MAX)
1789 goto purge_queue;
1790
1791 pq->timeout = pq->timeout << 1;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001792 if (!mod_timer(&pq->hold_timer, jiffies + pq->timeout))
1793 xfrm_pol_hold(pol);
1794 goto out;
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001795 }
1796
1797 dst_release(dst);
1798
1799 __skb_queue_head_init(&list);
1800
1801 spin_lock(&pq->hold_queue.lock);
1802 pq->timeout = 0;
1803 skb_queue_splice_init(&pq->hold_queue, &list);
1804 spin_unlock(&pq->hold_queue.lock);
1805
1806 while (!skb_queue_empty(&list)) {
1807 skb = __skb_dequeue(&list);
1808
1809 xfrm_decode_session(skb, &fl, skb_dst(skb)->ops->family);
1810 dst_hold(skb_dst(skb)->path);
1811 dst = xfrm_lookup(xp_net(pol), skb_dst(skb)->path,
1812 &fl, skb->sk, 0);
1813 if (IS_ERR(dst)) {
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001814 kfree_skb(skb);
1815 continue;
1816 }
1817
1818 nf_reset(skb);
1819 skb_dst_drop(skb);
1820 skb_dst_set(skb, dst);
1821
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001822 err = dst_output(skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001823 }
1824
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001825out:
1826 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001827 return;
1828
1829purge_queue:
1830 pq->timeout = 0;
1831 xfrm_queue_purge(&pq->hold_queue);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001832 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001833}
1834
Eric Dumazetaad88722014-04-15 13:47:15 -04001835static int xdst_queue_output(struct sock *sk, struct sk_buff *skb)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001836{
1837 unsigned long sched_next;
1838 struct dst_entry *dst = skb_dst(skb);
1839 struct xfrm_dst *xdst = (struct xfrm_dst *) dst;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001840 struct xfrm_policy *pol = xdst->pols[0];
1841 struct xfrm_policy_queue *pq = &pol->polq;
Steffen Klassert4d53eff2013-10-16 13:42:46 +02001842 const struct sk_buff *fclone = skb + 1;
1843
1844 if (unlikely(skb->fclone == SKB_FCLONE_ORIG &&
1845 fclone->fclone == SKB_FCLONE_CLONE)) {
1846 kfree_skb(skb);
1847 return 0;
1848 }
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001849
1850 if (pq->hold_queue.qlen > XFRM_MAX_QUEUE_LEN) {
1851 kfree_skb(skb);
1852 return -EAGAIN;
1853 }
1854
1855 skb_dst_force(skb);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001856
1857 spin_lock_bh(&pq->hold_queue.lock);
1858
1859 if (!pq->timeout)
1860 pq->timeout = XFRM_QUEUE_TMO_MIN;
1861
1862 sched_next = jiffies + pq->timeout;
1863
1864 if (del_timer(&pq->hold_timer)) {
1865 if (time_before(pq->hold_timer.expires, sched_next))
1866 sched_next = pq->hold_timer.expires;
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001867 xfrm_pol_put(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001868 }
1869
1870 __skb_queue_tail(&pq->hold_queue, skb);
Steffen Klasserte7d8f6c2013-10-08 10:49:45 +02001871 if (!mod_timer(&pq->hold_timer, sched_next))
1872 xfrm_pol_hold(pol);
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001873
1874 spin_unlock_bh(&pq->hold_queue.lock);
1875
1876 return 0;
1877}
1878
1879static struct xfrm_dst *xfrm_create_dummy_bundle(struct net *net,
1880 struct dst_entry *dst,
1881 const struct flowi *fl,
1882 int num_xfrms,
1883 u16 family)
1884{
1885 int err;
1886 struct net_device *dev;
1887 struct dst_entry *dst1;
1888 struct xfrm_dst *xdst;
1889
1890 xdst = xfrm_alloc_dst(net, family);
1891 if (IS_ERR(xdst))
1892 return xdst;
1893
Steffen Klassert5b8ef342013-08-27 13:43:30 +02001894 if (net->xfrm.sysctl_larval_drop || num_xfrms <= 0)
Steffen Klasserta0073fe2013-02-05 12:52:55 +01001895 return xdst;
1896
1897 dst1 = &xdst->u.dst;
1898 dst_hold(dst);
1899 xdst->route = dst;
1900
1901 dst_copy_metrics(dst1, dst);
1902
1903 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
1904 dst1->flags |= DST_HOST | DST_XFRM_QUEUE;
1905 dst1->lastuse = jiffies;
1906
1907 dst1->input = dst_discard;
1908 dst1->output = xdst_queue_output;
1909
1910 dst_hold(dst);
1911 dst1->child = dst;
1912 dst1->path = dst;
1913
1914 xfrm_init_path((struct xfrm_dst *)dst1, dst, 0);
1915
1916 err = -ENODEV;
1917 dev = dst->dev;
1918 if (!dev)
1919 goto free_dst;
1920
1921 err = xfrm_fill_dst(xdst, dev, fl);
1922 if (err)
1923 goto free_dst;
1924
1925out:
1926 return xdst;
1927
1928free_dst:
1929 dst_release(dst1);
1930 xdst = ERR_PTR(err);
1931 goto out;
1932}
1933
Timo Teräs80c802f2010-04-07 00:30:05 +00001934static struct flow_cache_object *
David S. Millerdee9f4b2011-02-22 18:44:31 -08001935xfrm_bundle_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir,
Timo Teräs80c802f2010-04-07 00:30:05 +00001936 struct flow_cache_object *oldflo, void *ctx)
1937{
1938 struct dst_entry *dst_orig = (struct dst_entry *)ctx;
1939 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
1940 struct xfrm_dst *xdst, *new_xdst;
1941 int num_pols = 0, num_xfrms = 0, i, err, pol_dead;
1942
1943 /* Check if the policies from old bundle are usable */
1944 xdst = NULL;
1945 if (oldflo) {
1946 xdst = container_of(oldflo, struct xfrm_dst, flo);
1947 num_pols = xdst->num_pols;
1948 num_xfrms = xdst->num_xfrms;
1949 pol_dead = 0;
1950 for (i = 0; i < num_pols; i++) {
1951 pols[i] = xdst->pols[i];
1952 pol_dead |= pols[i]->walk.dead;
1953 }
1954 if (pol_dead) {
1955 dst_free(&xdst->u.dst);
1956 xdst = NULL;
1957 num_pols = 0;
1958 num_xfrms = 0;
1959 oldflo = NULL;
1960 }
1961 }
1962
1963 /* Resolve policies to use if we couldn't get them from
1964 * previous cache entry */
1965 if (xdst == NULL) {
1966 num_pols = 1;
Baker Zhangb5fb82c2013-03-19 04:24:30 +00001967 pols[0] = __xfrm_policy_lookup(net, fl, family,
1968 flow_to_policy_dir(dir));
Timo Teräs80c802f2010-04-07 00:30:05 +00001969 err = xfrm_expand_policies(fl, family, pols,
1970 &num_pols, &num_xfrms);
1971 if (err < 0)
1972 goto inc_error;
1973 if (num_pols == 0)
1974 return NULL;
1975 if (num_xfrms <= 0)
1976 goto make_dummy_bundle;
1977 }
1978
1979 new_xdst = xfrm_resolve_and_create_bundle(pols, num_pols, fl, family, dst_orig);
1980 if (IS_ERR(new_xdst)) {
1981 err = PTR_ERR(new_xdst);
1982 if (err != -EAGAIN)
1983 goto error;
1984 if (oldflo == NULL)
1985 goto make_dummy_bundle;
1986 dst_hold(&xdst->u.dst);
1987 return oldflo;
Timo Teräsd809ec82010-07-12 21:29:42 +00001988 } else if (new_xdst == NULL) {
1989 num_xfrms = 0;
1990 if (oldflo == NULL)
1991 goto make_dummy_bundle;
1992 xdst->num_xfrms = 0;
1993 dst_hold(&xdst->u.dst);
1994 return oldflo;
Timo Teräs80c802f2010-04-07 00:30:05 +00001995 }
1996
1997 /* Kill the previous bundle */
1998 if (xdst) {
1999 /* The policies were stolen for newly generated bundle */
2000 xdst->num_pols = 0;
2001 dst_free(&xdst->u.dst);
2002 }
2003
2004 /* Flow cache does not have reference, it dst_free()'s,
2005 * but we do need to return one reference for original caller */
2006 dst_hold(&new_xdst->u.dst);
2007 return &new_xdst->flo;
2008
2009make_dummy_bundle:
2010 /* We found policies, but there's no bundles to instantiate:
2011 * either because the policy blocks, has no transformations or
2012 * we could not build template (no xfrm_states).*/
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002013 xdst = xfrm_create_dummy_bundle(net, dst_orig, fl, num_xfrms, family);
Timo Teräs80c802f2010-04-07 00:30:05 +00002014 if (IS_ERR(xdst)) {
2015 xfrm_pols_put(pols, num_pols);
2016 return ERR_CAST(xdst);
2017 }
2018 xdst->num_pols = num_pols;
2019 xdst->num_xfrms = num_xfrms;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08002020 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002021
2022 dst_hold(&xdst->u.dst);
2023 return &xdst->flo;
2024
2025inc_error:
2026 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
2027error:
2028 if (xdst != NULL)
2029 dst_free(&xdst->u.dst);
2030 else
2031 xfrm_pols_put(pols, num_pols);
2032 return ERR_PTR(err);
2033}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
David S. Miller2774c132011-03-01 14:59:04 -08002035static struct dst_entry *make_blackhole(struct net *net, u16 family,
2036 struct dst_entry *dst_orig)
2037{
2038 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
2039 struct dst_entry *ret;
2040
2041 if (!afinfo) {
2042 dst_release(dst_orig);
Li RongQing433a1952012-09-17 22:40:10 +00002043 return ERR_PTR(-EINVAL);
David S. Miller2774c132011-03-01 14:59:04 -08002044 } else {
2045 ret = afinfo->blackhole_route(net, dst_orig);
2046 }
2047 xfrm_policy_put_afinfo(afinfo);
2048
2049 return ret;
2050}
2051
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052/* Main function: finds/creates a bundle for given flow.
2053 *
2054 * At the moment we eat a raw IP route. Mostly to speed up lookups
2055 * on interfaces with disabled IPsec.
2056 */
David S. Miller452edd52011-03-02 13:27:41 -08002057struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
2058 const struct flowi *fl,
2059 struct sock *sk, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060{
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002061 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
Timo Teräs80c802f2010-04-07 00:30:05 +00002062 struct flow_cache_object *flo;
2063 struct xfrm_dst *xdst;
David S. Miller452edd52011-03-02 13:27:41 -08002064 struct dst_entry *dst, *route;
Timo Teräs80c802f2010-04-07 00:30:05 +00002065 u16 family = dst_orig->ops->family;
Trent Jaegerdf718372005-12-13 23:12:27 -08002066 u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
Changli Gao4b021622010-04-27 21:20:22 +00002067 int i, err, num_pols, num_xfrms = 0, drop_pols = 0;
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002068
Timo Teräs80c802f2010-04-07 00:30:05 +00002069 dst = NULL;
2070 xdst = NULL;
2071 route = NULL;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002072
Thomas Graff7944fb2007-08-25 13:46:55 -07002073 if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002074 num_pols = 1;
2075 pols[0] = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
2076 err = xfrm_expand_policies(fl, family, pols,
2077 &num_pols, &num_xfrms);
2078 if (err < 0)
Herbert Xu75b8c132007-12-11 04:38:08 -08002079 goto dropdst;
Timo Teräs80c802f2010-04-07 00:30:05 +00002080
2081 if (num_pols) {
2082 if (num_xfrms <= 0) {
2083 drop_pols = num_pols;
2084 goto no_transform;
2085 }
2086
2087 xdst = xfrm_resolve_and_create_bundle(
2088 pols, num_pols, fl,
2089 family, dst_orig);
2090 if (IS_ERR(xdst)) {
2091 xfrm_pols_put(pols, num_pols);
2092 err = PTR_ERR(xdst);
2093 goto dropdst;
Timo Teräsd809ec82010-07-12 21:29:42 +00002094 } else if (xdst == NULL) {
2095 num_xfrms = 0;
2096 drop_pols = num_pols;
2097 goto no_transform;
Timo Teräs80c802f2010-04-07 00:30:05 +00002098 }
2099
Timo Teräs80c802f2010-04-07 00:30:05 +00002100 route = xdst->route;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002101 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
Timo Teräs80c802f2010-04-07 00:30:05 +00002104 if (xdst == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 /* To accelerate a bit... */
David S. Miller2518c7c2006-08-24 04:45:07 -07002106 if ((dst_orig->flags & DST_NOXFRM) ||
Alexey Dobriyan52479b62008-11-25 17:35:18 -08002107 !net->xfrm.policy_count[XFRM_POLICY_OUT])
Herbert Xu8b7817f2007-12-12 10:44:43 -08002108 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109
Timo Teräs80c802f2010-04-07 00:30:05 +00002110 flo = flow_cache_lookup(net, fl, family, dir,
2111 xfrm_bundle_lookup, dst_orig);
2112 if (flo == NULL)
2113 goto nopol;
Timo Teräsfe1a5f02010-04-07 00:30:04 +00002114 if (IS_ERR(flo)) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002115 err = PTR_ERR(flo);
Herbert Xu75b8c132007-12-11 04:38:08 -08002116 goto dropdst;
Masahide NAKAMURAd66e37a2008-01-07 21:46:15 -08002117 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002118 xdst = container_of(flo, struct xfrm_dst, flo);
2119
2120 num_pols = xdst->num_pols;
2121 num_xfrms = xdst->num_xfrms;
Weilong Chen3e94c2d2013-12-24 09:43:47 +08002122 memcpy(pols, xdst->pols, sizeof(struct xfrm_policy *) * num_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002123 route = xdst->route;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 }
2125
Timo Teräs80c802f2010-04-07 00:30:05 +00002126 dst = &xdst->u.dst;
2127 if (route == NULL && num_xfrms > 0) {
2128 /* The only case when xfrm_bundle_lookup() returns a
2129 * bundle with null route, is when the template could
2130 * not be resolved. It means policies are there, but
2131 * bundle could not be created, since we don't yet
2132 * have the xfrm_state's. We need to wait for KM to
2133 * negotiate new SA's or bail out with error.*/
2134 if (net->xfrm.sysctl_larval_drop) {
Timo Teräs80c802f2010-04-07 00:30:05 +00002135 dst_release(dst);
Timo Terasa1aa3482010-05-15 23:49:26 -07002136 xfrm_pols_put(pols, drop_pols);
Timo Teräs80c802f2010-04-07 00:30:05 +00002137 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
David S. Miller2774c132011-03-01 14:59:04 -08002138
David S. Miller452edd52011-03-02 13:27:41 -08002139 return make_blackhole(net, family, dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002140 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002141
Steffen Klassert5b8ef342013-08-27 13:43:30 +02002142 err = -EAGAIN;
Timo Teräs80c802f2010-04-07 00:30:05 +00002143
2144 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
2145 goto error;
2146 }
2147
2148no_transform:
2149 if (num_pols == 0)
Herbert Xu8b7817f2007-12-12 10:44:43 -08002150 goto nopol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
Timo Teräs80c802f2010-04-07 00:30:05 +00002152 if ((flags & XFRM_LOOKUP_ICMP) &&
2153 !(pols[0]->flags & XFRM_POLICY_ICMP)) {
2154 err = -ENOENT;
Herbert Xu8b7817f2007-12-12 10:44:43 -08002155 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002156 }
Herbert Xu8b7817f2007-12-12 10:44:43 -08002157
Timo Teräs80c802f2010-04-07 00:30:05 +00002158 for (i = 0; i < num_pols; i++)
2159 pols[i]->curlft.use_time = get_seconds();
Herbert Xu8b7817f2007-12-12 10:44:43 -08002160
Timo Teräs80c802f2010-04-07 00:30:05 +00002161 if (num_xfrms < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 /* Prohibit the flow */
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002163 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLBLOCK);
Patrick McHardye104411b2005-09-08 15:11:55 -07002164 err = -EPERM;
2165 goto error;
Timo Teräs80c802f2010-04-07 00:30:05 +00002166 } else if (num_xfrms > 0) {
2167 /* Flow transformed */
Timo Teräs80c802f2010-04-07 00:30:05 +00002168 dst_release(dst_orig);
2169 } else {
2170 /* Flow passes untransformed */
2171 dst_release(dst);
David S. Miller452edd52011-03-02 13:27:41 -08002172 dst = dst_orig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002174ok:
2175 xfrm_pols_put(pols, drop_pols);
Gao feng0c183372012-05-26 01:30:53 +00002176 if (dst && dst->xfrm &&
2177 dst->xfrm->props.mode == XFRM_MODE_TUNNEL)
2178 dst->flags |= DST_XFRM_TUNNEL;
David S. Miller452edd52011-03-02 13:27:41 -08002179 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
Timo Teräs80c802f2010-04-07 00:30:05 +00002181nopol:
David S. Miller452edd52011-03-02 13:27:41 -08002182 if (!(flags & XFRM_LOOKUP_ICMP)) {
2183 dst = dst_orig;
Timo Teräs80c802f2010-04-07 00:30:05 +00002184 goto ok;
David S. Miller452edd52011-03-02 13:27:41 -08002185 }
Timo Teräs80c802f2010-04-07 00:30:05 +00002186 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187error:
Timo Teräs80c802f2010-04-07 00:30:05 +00002188 dst_release(dst);
Herbert Xu75b8c132007-12-11 04:38:08 -08002189dropdst:
2190 dst_release(dst_orig);
Timo Teräs80c802f2010-04-07 00:30:05 +00002191 xfrm_pols_put(pols, drop_pols);
David S. Miller452edd52011-03-02 13:27:41 -08002192 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193}
2194EXPORT_SYMBOL(xfrm_lookup);
2195
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002196static inline int
David S. Miller8f029de2011-02-22 17:59:59 -08002197xfrm_secpath_reject(int idx, struct sk_buff *skb, const struct flowi *fl)
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002198{
2199 struct xfrm_state *x;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002200
2201 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
2202 return 0;
2203 x = skb->sp->xvec[idx];
2204 if (!x->type->reject)
2205 return 0;
Herbert Xu1ecafed2007-10-09 13:24:07 -07002206 return x->type->reject(x, skb, fl);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002207}
2208
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209/* When skb is transformed back to its "native" form, we have to
2210 * check policy restrictions. At the moment we make this in maximally
2211 * stupid way. Shame on me. :-) Of course, connected sockets must
2212 * have policy cached at them.
2213 */
2214
2215static inline int
David S. Miller7db454b2011-02-24 01:43:01 -05002216xfrm_state_ok(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 unsigned short family)
2218{
2219 if (xfrm_state_kern(x))
Kazunori MIYAZAWA928ba412007-02-13 12:57:16 -08002220 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 return x->id.proto == tmpl->id.proto &&
2222 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
2223 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
2224 x->props.mode == tmpl->mode &&
Herbert Xuc5d18e92008-04-22 00:46:42 -07002225 (tmpl->allalgs || (tmpl->aalgos & (1<<x->props.aalgo)) ||
Masahide NAKAMURAf3bd4842006-08-23 18:00:48 -07002226 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002227 !(x->props.mode != XFRM_MODE_TRANSPORT &&
2228 xfrm_state_addr_cmp(tmpl, x, family));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229}
2230
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002231/*
2232 * 0 or more than 0 is returned when validation is succeeded (either bypass
2233 * because of optional transport mode, or next index of the mathced secpath
2234 * state with the template.
2235 * -1 is returned when no matching template is found.
2236 * Otherwise "-2 - errored_index" is returned.
2237 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238static inline int
David S. Miller22cccb72011-02-24 01:43:33 -05002239xfrm_policy_ok(const struct xfrm_tmpl *tmpl, const struct sec_path *sp, int start,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 unsigned short family)
2241{
2242 int idx = start;
2243
2244 if (tmpl->optional) {
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -07002245 if (tmpl->mode == XFRM_MODE_TRANSPORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 return start;
2247 } else
2248 start = -1;
2249 for (; idx < sp->len; idx++) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002250 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 return ++idx;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002252 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
2253 if (start == -1)
2254 start = -2-idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 break;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 }
2258 return start;
2259}
2260
Herbert Xud5422ef2007-12-12 10:44:16 -08002261int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
2262 unsigned int family, int reverse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263{
2264 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002265 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266
2267 if (unlikely(afinfo == NULL))
2268 return -EAFNOSUPPORT;
2269
Herbert Xud5422ef2007-12-12 10:44:16 -08002270 afinfo->decode_session(skb, fl, reverse);
David S. Miller1d28f422011-03-12 00:29:39 -05002271 err = security_xfrm_decode_session(skb, &fl->flowi_secid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 xfrm_policy_put_afinfo(afinfo);
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002273 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274}
Herbert Xud5422ef2007-12-12 10:44:16 -08002275EXPORT_SYMBOL(__xfrm_decode_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276
David S. Miller9a7386e2011-02-24 01:44:12 -05002277static inline int secpath_has_nontransport(const struct sec_path *sp, int k, int *idxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278{
2279 for (; k < sp->len; k++) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002280 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002281 *idxp = k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 return 1;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 }
2285
2286 return 0;
2287}
2288
YOSHIFUJI Hideakia716c112007-02-09 23:25:29 +09002289int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 unsigned short family)
2291{
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002292 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 struct xfrm_policy *pol;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002294 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2295 int npols = 0;
2296 int xfrm_nr;
2297 int pi;
Herbert Xud5422ef2007-12-12 10:44:16 -08002298 int reverse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 struct flowi fl;
Herbert Xud5422ef2007-12-12 10:44:16 -08002300 u8 fl_dir;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002301 int xerr_idx = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302
Herbert Xud5422ef2007-12-12 10:44:16 -08002303 reverse = dir & ~XFRM_POLICY_MASK;
2304 dir &= XFRM_POLICY_MASK;
2305 fl_dir = policy_to_flow_dir(dir);
2306
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002307 if (__xfrm_decode_session(skb, &fl, family, reverse) < 0) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002308 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002310 }
2311
Patrick McHardyeb9c7eb2006-01-06 23:06:30 -08002312 nf_nat_decode_session(skb, &fl, family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313
2314 /* First, check used SA against their selectors. */
2315 if (skb->sp) {
2316 int i;
2317
Weilong Chen9b7a7872013-12-24 09:43:46 +08002318 for (i = skb->sp->len-1; i >= 0; i--) {
Herbert Xudbe5b4a2006-04-01 00:54:16 -08002319 struct xfrm_state *x = skb->sp->xvec[i];
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002320 if (!xfrm_selector_match(&x->sel, &fl, family)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002321 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 }
2325 }
2326
2327 pol = NULL;
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002328 if (sk && sk->sk_policy[dir]) {
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07002329 pol = xfrm_sk_policy_lookup(sk, dir, &fl);
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002330 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002331 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002332 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002333 }
Venkat Yekkirala3bccfbc2006-10-05 15:42:35 -05002334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335
Timo Teräsfe1a5f02010-04-07 00:30:04 +00002336 if (!pol) {
2337 struct flow_cache_object *flo;
2338
2339 flo = flow_cache_lookup(net, &fl, family, fl_dir,
2340 xfrm_policy_lookup, NULL);
2341 if (IS_ERR_OR_NULL(flo))
2342 pol = ERR_CAST(flo);
2343 else
2344 pol = container_of(flo, struct xfrm_policy, flo);
2345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002347 if (IS_ERR(pol)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002348 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002349 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002350 }
James Morris134b0fc2006-10-05 15:42:27 -05002351
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002352 if (!pol) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002353 if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002354 xfrm_secpath_reject(xerr_idx, skb, &fl);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002355 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOPOLS);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002356 return 0;
2357 }
2358 return 1;
2359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
James Morris9d729f72007-03-04 16:12:44 -08002361 pol->curlft.use_time = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002363 pols[0] = pol;
Weilong Chen02d08922013-12-24 09:43:48 +08002364 npols++;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002365#ifdef CONFIG_XFRM_SUB_POLICY
2366 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
Alexey Dobriyanf6e1e252008-11-25 17:35:44 -08002367 pols[1] = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN,
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002368 &fl, family,
2369 XFRM_POLICY_IN);
2370 if (pols[1]) {
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002371 if (IS_ERR(pols[1])) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002372 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
James Morris134b0fc2006-10-05 15:42:27 -05002373 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002374 }
James Morris9d729f72007-03-04 16:12:44 -08002375 pols[1]->curlft.use_time = get_seconds();
Weilong Chen02d08922013-12-24 09:43:48 +08002376 npols++;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002377 }
2378 }
2379#endif
2380
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 if (pol->action == XFRM_POLICY_ALLOW) {
2382 struct sec_path *sp;
2383 static struct sec_path dummy;
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002384 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002385 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002386 struct xfrm_tmpl **tpp = tp;
2387 int ti = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 int i, k;
2389
2390 if ((sp = skb->sp) == NULL)
2391 sp = &dummy;
2392
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002393 for (pi = 0; pi < npols; pi++) {
2394 if (pols[pi] != pol &&
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002395 pols[pi]->action != XFRM_POLICY_ALLOW) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002396 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002397 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002398 }
2399 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002400 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002401 goto reject_error;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002402 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002403 for (i = 0; i < pols[pi]->xfrm_nr; i++)
2404 tpp[ti++] = &pols[pi]->xfrm_vec[i];
2405 }
2406 xfrm_nr = ti;
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002407 if (npols > 1) {
Fan Du283bc9f2013-11-07 17:47:50 +08002408 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family, net);
Masahide NAKAMURA41a49cc2006-08-23 22:48:31 -07002409 tpp = stp;
2410 }
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002411
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 /* For each tunnel xfrm, find the first matching tmpl.
2413 * For each tmpl before that, find corresponding xfrm.
2414 * Order is _important_. Later we will implement
2415 * some barriers, but at the moment barriers
2416 * are implied between each two transformations.
2417 */
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002418 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
2419 k = xfrm_policy_ok(tpp[i], sp, k, family);
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002420 if (k < 0) {
James Morrisd1d9fac2006-09-01 00:32:12 -07002421 if (k < -1)
2422 /* "-2 - errored_index" returned */
2423 xerr_idx = -(2+k);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002424 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 goto reject;
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 }
2428
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002429 if (secpath_has_nontransport(sp, k, &xerr_idx)) {
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002430 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 goto reject;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002434 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 return 1;
2436 }
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002437 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438
2439reject:
Masahide NAKAMURAdf0ba922006-08-23 20:41:00 -07002440 xfrm_secpath_reject(xerr_idx, skb, &fl);
Masahide NAKAMURA4e81bb82006-08-23 22:43:30 -07002441reject_error:
2442 xfrm_pols_put(pols, npols);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 return 0;
2444}
2445EXPORT_SYMBOL(__xfrm_policy_check);
2446
2447int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
2448{
Alexey Dobriyan99a66652008-11-25 17:36:13 -08002449 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 struct flowi fl;
Eric Dumazetadf30902009-06-02 05:19:30 +00002451 struct dst_entry *dst;
Eric Dumazet73137142011-03-15 15:26:43 -07002452 int res = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002454 if (xfrm_decode_session(skb, &fl, family) < 0) {
jamal72032fd2010-02-18 03:35:07 +00002455 XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 return 0;
Masahide NAKAMURA0aa64772007-12-20 20:43:36 -08002457 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458
Eric Dumazetfafeeb62010-06-01 10:04:49 +00002459 skb_dst_force(skb);
Eric Dumazetadf30902009-06-02 05:19:30 +00002460
David S. Miller452edd52011-03-02 13:27:41 -08002461 dst = xfrm_lookup(net, skb_dst(skb), &fl, NULL, 0);
2462 if (IS_ERR(dst)) {
Eric Dumazet73137142011-03-15 15:26:43 -07002463 res = 0;
David S. Miller452edd52011-03-02 13:27:41 -08002464 dst = NULL;
2465 }
Eric Dumazetadf30902009-06-02 05:19:30 +00002466 skb_dst_set(skb, dst);
2467 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468}
2469EXPORT_SYMBOL(__xfrm_route_forward);
2470
David S. Millerd49c73c2006-08-13 18:55:53 -07002471/* Optimize later using cookies and generation ids. */
2472
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
2474{
David S. Millerd49c73c2006-08-13 18:55:53 -07002475 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
David S. Millerf5b0a872012-07-19 12:31:33 -07002476 * to DST_OBSOLETE_FORCE_CHK to force all XFRM destinations to
2477 * get validated by dst_ops->check on every use. We do this
2478 * because when a normal route referenced by an XFRM dst is
2479 * obsoleted we do not go looking around for all parent
2480 * referencing XFRM dsts so that we can invalidate them. It
2481 * is just too much work. Instead we make the checks here on
2482 * every use. For example:
David S. Millerd49c73c2006-08-13 18:55:53 -07002483 *
2484 * XFRM dst A --> IPv4 dst X
2485 *
2486 * X is the "xdst->route" of A (X is also the "dst->path" of A
2487 * in this example). If X is marked obsolete, "A" will not
2488 * notice. That's what we are validating here via the
2489 * stale_bundle() check.
2490 *
2491 * When a policy's bundle is pruned, we dst_free() the XFRM
David S. Millerf5b0a872012-07-19 12:31:33 -07002492 * dst which causes it's ->obsolete field to be set to
2493 * DST_OBSOLETE_DEAD. If an XFRM dst has been pruned like
2494 * this, we want to force a new route lookup.
David S. Miller399c1802005-12-19 14:23:23 -08002495 */
David S. Millerd49c73c2006-08-13 18:55:53 -07002496 if (dst->obsolete < 0 && !stale_bundle(dst))
2497 return dst;
2498
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 return NULL;
2500}
2501
2502static int stale_bundle(struct dst_entry *dst)
2503{
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002504 return !xfrm_bundle_ok((struct xfrm_dst *)dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505}
2506
Herbert Xuaabc9762005-05-03 16:27:10 -07002507void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002510 dst->dev = dev_net(dev)->loopback_dev;
Daniel Lezcanode3cb742007-09-25 19:16:28 -07002511 dev_hold(dst->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 dev_put(dev);
2513 }
2514}
Herbert Xuaabc9762005-05-03 16:27:10 -07002515EXPORT_SYMBOL(xfrm_dst_ifdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516
2517static void xfrm_link_failure(struct sk_buff *skb)
2518{
2519 /* Impossible. Such dst must be popped before reaches point of failure. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520}
2521
2522static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
2523{
2524 if (dst) {
2525 if (dst->obsolete) {
2526 dst_release(dst);
2527 dst = NULL;
2528 }
2529 }
2530 return dst;
2531}
2532
Paul Mooree4c17212013-05-29 07:36:25 +00002533void xfrm_garbage_collect(struct net *net)
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002534{
Fan Duca925cf2014-01-18 09:55:27 +08002535 flow_cache_flush(net);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002536}
Paul Mooree4c17212013-05-29 07:36:25 +00002537EXPORT_SYMBOL(xfrm_garbage_collect);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002538
2539static void xfrm_garbage_collect_deferred(struct net *net)
2540{
Fan Duca925cf2014-01-18 09:55:27 +08002541 flow_cache_flush_deferred(net);
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002542}
2543
Herbert Xu25ee3282007-12-11 09:32:34 -08002544static void xfrm_init_pmtu(struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545{
2546 do {
2547 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2548 u32 pmtu, route_mtu_cached;
2549
2550 pmtu = dst_mtu(dst->child);
2551 xdst->child_mtu_cached = pmtu;
2552
2553 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
2554
2555 route_mtu_cached = dst_mtu(xdst->route);
2556 xdst->route_mtu_cached = route_mtu_cached;
2557
2558 if (pmtu > route_mtu_cached)
2559 pmtu = route_mtu_cached;
2560
David S. Millerdefb3512010-12-08 21:16:57 -08002561 dst_metric_set(dst, RTAX_MTU, pmtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 } while ((dst = dst->next));
2563}
2564
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565/* Check that the bundle accepts the flow and its components are
2566 * still valid.
2567 */
2568
Steffen Klassert12fdb4d2011-06-29 23:18:20 +00002569static int xfrm_bundle_ok(struct xfrm_dst *first)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570{
2571 struct dst_entry *dst = &first->u.dst;
2572 struct xfrm_dst *last;
2573 u32 mtu;
2574
Hideaki YOSHIFUJI92d63dec2005-05-26 12:58:04 -07002575 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 (dst->dev && !netif_running(dst->dev)))
2577 return 0;
2578
Steffen Klasserta0073fe2013-02-05 12:52:55 +01002579 if (dst->flags & DST_XFRM_QUEUE)
2580 return 1;
2581
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 last = NULL;
2583
2584 do {
2585 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2586
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 if (dst->xfrm->km.state != XFRM_STATE_VALID)
2588 return 0;
Timo Teräs80c802f2010-04-07 00:30:05 +00002589 if (xdst->xfrm_genid != dst->xfrm->genid)
2590 return 0;
Timo Teräsb1312c82010-06-24 14:35:00 -07002591 if (xdst->num_pols > 0 &&
2592 xdst->policy_genid != atomic_read(&xdst->pols[0]->genid))
David S. Miller9d4a7062006-08-24 03:18:09 -07002593 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594
2595 mtu = dst_mtu(dst->child);
2596 if (xdst->child_mtu_cached != mtu) {
2597 last = xdst;
2598 xdst->child_mtu_cached = mtu;
2599 }
2600
Hideaki YOSHIFUJI92d63dec2005-05-26 12:58:04 -07002601 if (!dst_check(xdst->route, xdst->route_cookie))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 return 0;
2603 mtu = dst_mtu(xdst->route);
2604 if (xdst->route_mtu_cached != mtu) {
2605 last = xdst;
2606 xdst->route_mtu_cached = mtu;
2607 }
2608
2609 dst = dst->child;
2610 } while (dst->xfrm);
2611
2612 if (likely(!last))
2613 return 1;
2614
2615 mtu = last->child_mtu_cached;
2616 for (;;) {
2617 dst = &last->u.dst;
2618
2619 mtu = xfrm_state_mtu(dst->xfrm, mtu);
2620 if (mtu > last->route_mtu_cached)
2621 mtu = last->route_mtu_cached;
David S. Millerdefb3512010-12-08 21:16:57 -08002622 dst_metric_set(dst, RTAX_MTU, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623
2624 if (last == first)
2625 break;
2626
Patrick McHardybd0bf072007-07-18 01:55:52 -07002627 last = (struct xfrm_dst *)last->u.dst.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 last->child_mtu_cached = mtu;
2629 }
2630
2631 return 1;
2632}
2633
David S. Miller0dbaee32010-12-13 12:52:14 -08002634static unsigned int xfrm_default_advmss(const struct dst_entry *dst)
2635{
2636 return dst_metric_advmss(dst->path);
2637}
2638
Steffen Klassertebb762f2011-11-23 02:12:51 +00002639static unsigned int xfrm_mtu(const struct dst_entry *dst)
David S. Millerd33e4552010-12-14 13:01:14 -08002640{
Steffen Klassert618f9bc2011-11-23 02:13:31 +00002641 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
2642
2643 return mtu ? : dst_mtu(dst->path);
David S. Millerd33e4552010-12-14 13:01:14 -08002644}
2645
David S. Millerf894cbf2012-07-02 21:52:24 -07002646static struct neighbour *xfrm_neigh_lookup(const struct dst_entry *dst,
2647 struct sk_buff *skb,
2648 const void *daddr)
David S. Millerd3aaeb32011-07-18 00:40:17 -07002649{
David S. Millerf894cbf2012-07-02 21:52:24 -07002650 return dst->path->ops->neigh_lookup(dst, skb, daddr);
David S. Millerd3aaeb32011-07-18 00:40:17 -07002651}
2652
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
2654{
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002655 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 int err = 0;
2657 if (unlikely(afinfo == NULL))
2658 return -EINVAL;
2659 if (unlikely(afinfo->family >= NPROTO))
2660 return -EAFNOSUPPORT;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002661 spin_lock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
2663 err = -ENOBUFS;
2664 else {
2665 struct dst_ops *dst_ops = afinfo->dst_ops;
2666 if (likely(dst_ops->kmem_cachep == NULL))
2667 dst_ops->kmem_cachep = xfrm_dst_cache;
2668 if (likely(dst_ops->check == NULL))
2669 dst_ops->check = xfrm_dst_check;
David S. Miller0dbaee32010-12-13 12:52:14 -08002670 if (likely(dst_ops->default_advmss == NULL))
2671 dst_ops->default_advmss = xfrm_default_advmss;
Steffen Klassertebb762f2011-11-23 02:12:51 +00002672 if (likely(dst_ops->mtu == NULL))
2673 dst_ops->mtu = xfrm_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 if (likely(dst_ops->negative_advice == NULL))
2675 dst_ops->negative_advice = xfrm_negative_advice;
2676 if (likely(dst_ops->link_failure == NULL))
2677 dst_ops->link_failure = xfrm_link_failure;
David S. Millerd3aaeb32011-07-18 00:40:17 -07002678 if (likely(dst_ops->neigh_lookup == NULL))
2679 dst_ops->neigh_lookup = xfrm_neigh_lookup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 if (likely(afinfo->garbage_collect == NULL))
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002681 afinfo->garbage_collect = xfrm_garbage_collect_deferred;
Priyanka Jain418a99a2012-08-12 21:22:29 +00002682 rcu_assign_pointer(xfrm_policy_afinfo[afinfo->family], afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02002684 spin_unlock(&xfrm_policy_afinfo_lock);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002685
2686 rtnl_lock();
2687 for_each_net(net) {
2688 struct dst_ops *xfrm_dst_ops;
2689
2690 switch (afinfo->family) {
2691 case AF_INET:
2692 xfrm_dst_ops = &net->xfrm.xfrm4_dst_ops;
2693 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002694#if IS_ENABLED(CONFIG_IPV6)
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002695 case AF_INET6:
2696 xfrm_dst_ops = &net->xfrm.xfrm6_dst_ops;
2697 break;
2698#endif
2699 default:
2700 BUG();
2701 }
2702 *xfrm_dst_ops = *afinfo->dst_ops;
2703 }
2704 rtnl_unlock();
2705
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 return err;
2707}
2708EXPORT_SYMBOL(xfrm_policy_register_afinfo);
2709
2710int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
2711{
2712 int err = 0;
2713 if (unlikely(afinfo == NULL))
2714 return -EINVAL;
2715 if (unlikely(afinfo->family >= NPROTO))
2716 return -EAFNOSUPPORT;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002717 spin_lock(&xfrm_policy_afinfo_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
2719 if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
2720 err = -EINVAL;
Eric Dumazetef8531b2012-08-19 12:31:48 +02002721 else
2722 RCU_INIT_POINTER(xfrm_policy_afinfo[afinfo->family],
2723 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 }
Eric Dumazetef8531b2012-08-19 12:31:48 +02002725 spin_unlock(&xfrm_policy_afinfo_lock);
2726 if (!err) {
2727 struct dst_ops *dst_ops = afinfo->dst_ops;
2728
2729 synchronize_rcu();
2730
2731 dst_ops->kmem_cachep = NULL;
2732 dst_ops->check = NULL;
2733 dst_ops->negative_advice = NULL;
2734 dst_ops->link_failure = NULL;
2735 afinfo->garbage_collect = NULL;
2736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 return err;
2738}
2739EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
2740
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002741static void __net_init xfrm_dst_ops_init(struct net *net)
2742{
2743 struct xfrm_policy_afinfo *afinfo;
2744
Eric Dumazetef8531b2012-08-19 12:31:48 +02002745 rcu_read_lock();
2746 afinfo = rcu_dereference(xfrm_policy_afinfo[AF_INET]);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002747 if (afinfo)
2748 net->xfrm.xfrm4_dst_ops = *afinfo->dst_ops;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002749#if IS_ENABLED(CONFIG_IPV6)
Eric Dumazetef8531b2012-08-19 12:31:48 +02002750 afinfo = rcu_dereference(xfrm_policy_afinfo[AF_INET6]);
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002751 if (afinfo)
2752 net->xfrm.xfrm6_dst_ops = *afinfo->dst_ops;
2753#endif
Priyanka Jain418a99a2012-08-12 21:22:29 +00002754 rcu_read_unlock();
Herbert Xu546be242006-05-27 23:03:58 -07002755}
2756
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
2758{
Jiri Pirko351638e2013-05-28 01:30:21 +00002759 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02002760
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 switch (event) {
2762 case NETDEV_DOWN:
Steffen Klassertc0ed1c12011-12-21 16:48:08 -05002763 xfrm_garbage_collect(dev_net(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 }
2765 return NOTIFY_DONE;
2766}
2767
2768static struct notifier_block xfrm_dev_notifier = {
Alexey Dobriyand5917a32008-10-31 00:41:59 -07002769 .notifier_call = xfrm_dev_event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770};
2771
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002772#ifdef CONFIG_XFRM_STATISTICS
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002773static int __net_init xfrm_statistics_init(struct net *net)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002774{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002775 int rv;
WANG Cong698365f2014-05-05 15:55:55 -07002776 net->mib.xfrm_statistics = alloc_percpu(struct linux_xfrm_mib);
2777 if (!net->mib.xfrm_statistics)
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002778 return -ENOMEM;
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002779 rv = xfrm_proc_init(net);
2780 if (rv < 0)
WANG Cong698365f2014-05-05 15:55:55 -07002781 free_percpu(net->mib.xfrm_statistics);
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002782 return rv;
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002783}
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002784
2785static void xfrm_statistics_fini(struct net *net)
2786{
Alexey Dobriyanc68cd1a2008-11-25 18:00:14 -08002787 xfrm_proc_fini(net);
WANG Cong698365f2014-05-05 15:55:55 -07002788 free_percpu(net->mib.xfrm_statistics);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002789}
2790#else
2791static int __net_init xfrm_statistics_init(struct net *net)
2792{
2793 return 0;
2794}
2795
2796static void xfrm_statistics_fini(struct net *net)
2797{
2798}
Masahide NAKAMURA558f82e2007-12-20 20:42:57 -08002799#endif
2800
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002801static int __net_init xfrm_policy_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802{
David S. Miller2518c7c2006-08-24 04:45:07 -07002803 unsigned int hmask, sz;
2804 int dir;
2805
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002806 if (net_eq(net, &init_net))
2807 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 sizeof(struct xfrm_dst),
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002809 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002810 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811
David S. Miller2518c7c2006-08-24 04:45:07 -07002812 hmask = 8 - 1;
2813 sz = (hmask+1) * sizeof(struct hlist_head);
2814
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002815 net->xfrm.policy_byidx = xfrm_hash_alloc(sz);
2816 if (!net->xfrm.policy_byidx)
2817 goto out_byidx;
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08002818 net->xfrm.policy_idx_hmask = hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -07002819
2820 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
2821 struct xfrm_policy_hash *htab;
2822
Alexey Dobriyandc2caba2008-11-25 17:24:15 -08002823 net->xfrm.policy_count[dir] = 0;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002824 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
David S. Miller2518c7c2006-08-24 04:45:07 -07002825
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002826 htab = &net->xfrm.policy_bydst[dir];
David S. Miller44e36b42006-08-24 04:50:50 -07002827 htab->table = xfrm_hash_alloc(sz);
David S. Miller2518c7c2006-08-24 04:45:07 -07002828 if (!htab->table)
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002829 goto out_bydst;
2830 htab->hmask = hmask;
David S. Miller2518c7c2006-08-24 04:45:07 -07002831 }
2832
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08002833 INIT_LIST_HEAD(&net->xfrm.policy_all);
Alexey Dobriyan66caf622008-11-25 17:28:57 -08002834 INIT_WORK(&net->xfrm.policy_hash_work, xfrm_hash_resize);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002835 if (net_eq(net, &init_net))
2836 register_netdevice_notifier(&xfrm_dev_notifier);
2837 return 0;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002838
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002839out_bydst:
2840 for (dir--; dir >= 0; dir--) {
2841 struct xfrm_policy_hash *htab;
2842
2843 htab = &net->xfrm.policy_bydst[dir];
2844 xfrm_hash_free(htab->table, sz);
2845 }
2846 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002847out_byidx:
2848 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849}
2850
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002851static void xfrm_policy_fini(struct net *net)
2852{
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002853 unsigned int sz;
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002854 int dir;
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002855
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002856 flush_work(&net->xfrm.policy_hash_work);
2857#ifdef CONFIG_XFRM_SUB_POLICY
Tetsuo Handa2e710292014-04-22 21:48:30 +09002858 xfrm_policy_flush(net, XFRM_POLICY_TYPE_SUB, false);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002859#endif
Tetsuo Handa2e710292014-04-22 21:48:30 +09002860 xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, false);
Alexey Dobriyan7c2776e2008-11-25 17:57:44 -08002861
Alexey Dobriyanadfcf0b2008-11-25 17:22:11 -08002862 WARN_ON(!list_empty(&net->xfrm.policy_all));
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002863
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002864 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002865 struct xfrm_policy_hash *htab;
2866
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002867 WARN_ON(!hlist_empty(&net->xfrm.policy_inexact[dir]));
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002868
2869 htab = &net->xfrm.policy_bydst[dir];
Michal Kubecek5b653b22013-01-18 16:03:48 +01002870 sz = (htab->hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyana35f6c52008-11-25 17:23:48 -08002871 WARN_ON(!hlist_empty(htab->table));
2872 xfrm_hash_free(htab->table, sz);
Alexey Dobriyan8b18f8e2008-11-25 17:23:26 -08002873 }
2874
Alexey Dobriyan8100bea2008-11-25 17:22:58 -08002875 sz = (net->xfrm.policy_idx_hmask + 1) * sizeof(struct hlist_head);
Alexey Dobriyan93b851c2008-11-25 17:22:35 -08002876 WARN_ON(!hlist_empty(net->xfrm.policy_byidx));
2877 xfrm_hash_free(net->xfrm.policy_byidx, sz);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002878}
2879
2880static int __net_init xfrm_net_init(struct net *net)
2881{
2882 int rv;
2883
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002884 rv = xfrm_statistics_init(net);
2885 if (rv < 0)
2886 goto out_statistics;
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002887 rv = xfrm_state_init(net);
2888 if (rv < 0)
2889 goto out_state;
2890 rv = xfrm_policy_init(net);
2891 if (rv < 0)
2892 goto out_policy;
Alexey Dobriyand7c75442010-01-24 22:47:53 -08002893 xfrm_dst_ops_init(net);
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08002894 rv = xfrm_sysctl_init(net);
2895 if (rv < 0)
2896 goto out_sysctl;
Steffen Klassert4a93f502014-03-12 09:43:17 +01002897 rv = flow_cache_init(net);
2898 if (rv < 0)
2899 goto out;
Fan Du283bc9f2013-11-07 17:47:50 +08002900
2901 /* Initialize the per-net locks here */
2902 spin_lock_init(&net->xfrm.xfrm_state_lock);
2903 rwlock_init(&net->xfrm.xfrm_policy_lock);
Fan Du283bc9f2013-11-07 17:47:50 +08002904 mutex_init(&net->xfrm.xfrm_cfg_mutex);
2905
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002906 return 0;
2907
Steffen Klassert4a93f502014-03-12 09:43:17 +01002908out:
2909 xfrm_sysctl_fini(net);
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08002910out_sysctl:
2911 xfrm_policy_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002912out_policy:
2913 xfrm_state_fini(net);
2914out_state:
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002915 xfrm_statistics_fini(net);
2916out_statistics:
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002917 return rv;
2918}
2919
2920static void __net_exit xfrm_net_exit(struct net *net)
2921{
Steffen Klassert4a93f502014-03-12 09:43:17 +01002922 flow_cache_fini(net);
Alexey Dobriyanb27aead2008-11-25 18:00:48 -08002923 xfrm_sysctl_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002924 xfrm_policy_fini(net);
2925 xfrm_state_fini(net);
Alexey Dobriyan59c99402008-11-25 17:59:52 -08002926 xfrm_statistics_fini(net);
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002927}
2928
2929static struct pernet_operations __net_initdata xfrm_net_ops = {
2930 .init = xfrm_net_init,
2931 .exit = xfrm_net_exit,
2932};
2933
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934void __init xfrm_init(void)
2935{
Alexey Dobriyand62ddc22008-11-25 17:14:31 -08002936 register_pernet_subsys(&xfrm_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 xfrm_input_init();
2938}
2939
Joy Lattenab5f5e82007-09-17 11:51:22 -07002940#ifdef CONFIG_AUDITSYSCALL
Ilpo Järvinen1486cbd72008-01-12 03:20:03 -08002941static void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
2942 struct audit_buffer *audit_buf)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002943{
Paul Moore875179f2007-12-01 23:27:18 +11002944 struct xfrm_sec_ctx *ctx = xp->security;
2945 struct xfrm_selector *sel = &xp->selector;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002946
Paul Moore875179f2007-12-01 23:27:18 +11002947 if (ctx)
2948 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
2949 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
2950
Weilong Chen9b7a7872013-12-24 09:43:46 +08002951 switch (sel->family) {
Joy Lattenab5f5e82007-09-17 11:51:22 -07002952 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -07002953 audit_log_format(audit_buf, " src=%pI4", &sel->saddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11002954 if (sel->prefixlen_s != 32)
2955 audit_log_format(audit_buf, " src_prefixlen=%d",
2956 sel->prefixlen_s);
Harvey Harrison21454aa2008-10-31 00:54:56 -07002957 audit_log_format(audit_buf, " dst=%pI4", &sel->daddr.a4);
Paul Moore875179f2007-12-01 23:27:18 +11002958 if (sel->prefixlen_d != 32)
2959 audit_log_format(audit_buf, " dst_prefixlen=%d",
2960 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002961 break;
2962 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002963 audit_log_format(audit_buf, " src=%pI6", sel->saddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11002964 if (sel->prefixlen_s != 128)
2965 audit_log_format(audit_buf, " src_prefixlen=%d",
2966 sel->prefixlen_s);
Harvey Harrison5b095d9892008-10-29 12:52:50 -07002967 audit_log_format(audit_buf, " dst=%pI6", sel->daddr.a6);
Paul Moore875179f2007-12-01 23:27:18 +11002968 if (sel->prefixlen_d != 128)
2969 audit_log_format(audit_buf, " dst_prefixlen=%d",
2970 sel->prefixlen_d);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002971 break;
2972 }
2973}
2974
Tetsuo Handa2e710292014-04-22 21:48:30 +09002975void xfrm_audit_policy_add(struct xfrm_policy *xp, int result, bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002976{
2977 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002978
Paul Mooreafeb14b2007-12-21 14:58:11 -08002979 audit_buf = xfrm_audit_start("SPD-add");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002980 if (audit_buf == NULL)
2981 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09002982 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002983 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002984 xfrm_audit_common_policyinfo(xp, audit_buf);
2985 audit_log_end(audit_buf);
2986}
2987EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
2988
Paul Moore68277ac2007-12-20 20:49:33 -08002989void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
Tetsuo Handa2e710292014-04-22 21:48:30 +09002990 bool task_valid)
Joy Lattenab5f5e82007-09-17 11:51:22 -07002991{
2992 struct audit_buffer *audit_buf;
Joy Lattenab5f5e82007-09-17 11:51:22 -07002993
Paul Mooreafeb14b2007-12-21 14:58:11 -08002994 audit_buf = xfrm_audit_start("SPD-delete");
Joy Lattenab5f5e82007-09-17 11:51:22 -07002995 if (audit_buf == NULL)
2996 return;
Tetsuo Handa2e710292014-04-22 21:48:30 +09002997 xfrm_audit_helper_usrinfo(task_valid, audit_buf);
Paul Mooreafeb14b2007-12-21 14:58:11 -08002998 audit_log_format(audit_buf, " res=%u", result);
Joy Lattenab5f5e82007-09-17 11:51:22 -07002999 xfrm_audit_common_policyinfo(xp, audit_buf);
3000 audit_log_end(audit_buf);
3001}
3002EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
3003#endif
3004
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003005#ifdef CONFIG_XFRM_MIGRATE
David S. Millerbc9b35a2012-05-15 15:04:57 -04003006static bool xfrm_migrate_selector_match(const struct xfrm_selector *sel_cmp,
3007 const struct xfrm_selector *sel_tgt)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003008{
3009 if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
3010 if (sel_tgt->family == sel_cmp->family &&
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003011 xfrm_addr_equal(&sel_tgt->daddr, &sel_cmp->daddr,
3012 sel_cmp->family) &&
3013 xfrm_addr_equal(&sel_tgt->saddr, &sel_cmp->saddr,
3014 sel_cmp->family) &&
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003015 sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
3016 sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04003017 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003018 }
3019 } else {
3020 if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
David S. Millerbc9b35a2012-05-15 15:04:57 -04003021 return true;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003022 }
3023 }
David S. Millerbc9b35a2012-05-15 15:04:57 -04003024 return false;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003025}
3026
Weilong Chen3e94c2d2013-12-24 09:43:47 +08003027static struct xfrm_policy *xfrm_migrate_policy_find(const struct xfrm_selector *sel,
3028 u8 dir, u8 type, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003029{
3030 struct xfrm_policy *pol, *ret = NULL;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003031 struct hlist_head *chain;
3032 u32 priority = ~0U;
3033
Fan Du283bc9f2013-11-07 17:47:50 +08003034 read_lock_bh(&net->xfrm.xfrm_policy_lock); /*FIXME*/
Fan Du8d549c42013-11-07 17:47:49 +08003035 chain = policy_hash_direct(net, &sel->daddr, &sel->saddr, sel->family, dir);
Sasha Levinb67bfe02013-02-27 17:06:00 -08003036 hlist_for_each_entry(pol, chain, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003037 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
3038 pol->type == type) {
3039 ret = pol;
3040 priority = ret->priority;
3041 break;
3042 }
3043 }
Fan Du8d549c42013-11-07 17:47:49 +08003044 chain = &net->xfrm.policy_inexact[dir];
Sasha Levinb67bfe02013-02-27 17:06:00 -08003045 hlist_for_each_entry(pol, chain, bydst) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003046 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
3047 pol->type == type &&
3048 pol->priority < priority) {
3049 ret = pol;
3050 break;
3051 }
3052 }
3053
3054 if (ret)
3055 xfrm_pol_hold(ret);
3056
Fan Du283bc9f2013-11-07 17:47:50 +08003057 read_unlock_bh(&net->xfrm.xfrm_policy_lock);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003058
3059 return ret;
3060}
3061
David S. Millerdd701752011-02-24 00:21:08 -05003062static int migrate_tmpl_match(const struct xfrm_migrate *m, const struct xfrm_tmpl *t)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003063{
3064 int match = 0;
3065
3066 if (t->mode == m->mode && t->id.proto == m->proto &&
3067 (m->reqid == 0 || t->reqid == m->reqid)) {
3068 switch (t->mode) {
3069 case XFRM_MODE_TUNNEL:
3070 case XFRM_MODE_BEET:
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003071 if (xfrm_addr_equal(&t->id.daddr, &m->old_daddr,
3072 m->old_family) &&
3073 xfrm_addr_equal(&t->saddr, &m->old_saddr,
3074 m->old_family)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003075 match = 1;
3076 }
3077 break;
3078 case XFRM_MODE_TRANSPORT:
3079 /* in case of transport mode, template does not store
3080 any IP addresses, hence we just compare mode and
3081 protocol */
3082 match = 1;
3083 break;
3084 default:
3085 break;
3086 }
3087 }
3088 return match;
3089}
3090
3091/* update endpoint address(es) of template(s) */
3092static int xfrm_policy_migrate(struct xfrm_policy *pol,
3093 struct xfrm_migrate *m, int num_migrate)
3094{
3095 struct xfrm_migrate *mp;
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003096 int i, j, n = 0;
3097
3098 write_lock_bh(&pol->lock);
Herbert Xu12a169e2008-10-01 07:03:24 -07003099 if (unlikely(pol->walk.dead)) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003100 /* target policy has been deleted */
3101 write_unlock_bh(&pol->lock);
3102 return -ENOENT;
3103 }
3104
3105 for (i = 0; i < pol->xfrm_nr; i++) {
3106 for (j = 0, mp = m; j < num_migrate; j++, mp++) {
3107 if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
3108 continue;
3109 n++;
Herbert Xu1bfcb102007-10-17 21:31:50 -07003110 if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
3111 pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003112 continue;
3113 /* update endpoints */
3114 memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
3115 sizeof(pol->xfrm_vec[i].id.daddr));
3116 memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
3117 sizeof(pol->xfrm_vec[i].saddr));
3118 pol->xfrm_vec[i].encap_family = mp->new_family;
3119 /* flush bundles */
Timo Teräs80c802f2010-04-07 00:30:05 +00003120 atomic_inc(&pol->genid);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003121 }
3122 }
3123
3124 write_unlock_bh(&pol->lock);
3125
3126 if (!n)
3127 return -ENODATA;
3128
3129 return 0;
3130}
3131
David S. Millerdd701752011-02-24 00:21:08 -05003132static int xfrm_migrate_check(const struct xfrm_migrate *m, int num_migrate)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003133{
3134 int i, j;
3135
3136 if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
3137 return -EINVAL;
3138
3139 for (i = 0; i < num_migrate; i++) {
YOSHIFUJI Hideaki / 吉藤英明70e94e62013-01-29 12:48:50 +00003140 if (xfrm_addr_equal(&m[i].old_daddr, &m[i].new_daddr,
3141 m[i].old_family) &&
3142 xfrm_addr_equal(&m[i].old_saddr, &m[i].new_saddr,
3143 m[i].old_family))
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003144 return -EINVAL;
3145 if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
3146 xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
3147 return -EINVAL;
3148
3149 /* check if there is any duplicated entry */
3150 for (j = i + 1; j < num_migrate; j++) {
3151 if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
3152 sizeof(m[i].old_daddr)) &&
3153 !memcmp(&m[i].old_saddr, &m[j].old_saddr,
3154 sizeof(m[i].old_saddr)) &&
3155 m[i].proto == m[j].proto &&
3156 m[i].mode == m[j].mode &&
3157 m[i].reqid == m[j].reqid &&
3158 m[i].old_family == m[j].old_family)
3159 return -EINVAL;
3160 }
3161 }
3162
3163 return 0;
3164}
3165
David S. Millerb4b7c0b2011-02-24 00:35:06 -05003166int xfrm_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003167 struct xfrm_migrate *m, int num_migrate,
Fan Du8d549c42013-11-07 17:47:49 +08003168 struct xfrm_kmaddress *k, struct net *net)
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003169{
3170 int i, err, nx_cur = 0, nx_new = 0;
3171 struct xfrm_policy *pol = NULL;
3172 struct xfrm_state *x, *xc;
3173 struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
3174 struct xfrm_state *x_new[XFRM_MAX_DEPTH];
3175 struct xfrm_migrate *mp;
3176
3177 if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
3178 goto out;
3179
3180 /* Stage 1 - find policy */
Fan Du8d549c42013-11-07 17:47:49 +08003181 if ((pol = xfrm_migrate_policy_find(sel, dir, type, net)) == NULL) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003182 err = -ENOENT;
3183 goto out;
3184 }
3185
3186 /* Stage 2 - find and update state(s) */
3187 for (i = 0, mp = m; i < num_migrate; i++, mp++) {
Fan Du283bc9f2013-11-07 17:47:50 +08003188 if ((x = xfrm_migrate_state_find(mp, net))) {
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003189 x_cur[nx_cur] = x;
3190 nx_cur++;
3191 if ((xc = xfrm_state_migrate(x, mp))) {
3192 x_new[nx_new] = xc;
3193 nx_new++;
3194 } else {
3195 err = -ENODATA;
3196 goto restore_state;
3197 }
3198 }
3199 }
3200
3201 /* Stage 3 - update policy */
3202 if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
3203 goto restore_state;
3204
3205 /* Stage 4 - delete old state(s) */
3206 if (nx_cur) {
3207 xfrm_states_put(x_cur, nx_cur);
3208 xfrm_states_delete(x_cur, nx_cur);
3209 }
3210
3211 /* Stage 5 - announce */
Arnaud Ebalard13c1d182008-10-05 13:33:42 -07003212 km_migrate(sel, dir, type, m, num_migrate, k);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003213
3214 xfrm_pol_put(pol);
3215
3216 return 0;
3217out:
3218 return err;
3219
3220restore_state:
3221 if (pol)
3222 xfrm_pol_put(pol);
3223 if (nx_cur)
3224 xfrm_states_put(x_cur, nx_cur);
3225 if (nx_new)
3226 xfrm_states_delete(x_new, nx_new);
3227
3228 return err;
3229}
David S. Millere610e672007-02-08 13:29:15 -08003230EXPORT_SYMBOL(xfrm_migrate);
Shinta Sugimoto80c9aba2007-02-08 13:11:42 -08003231#endif