1 /*
2 * 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
13 *
14 */
15
16 #include <linux/err.h>
17 #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>
24 #include <linux/netfilter.h>
25 #include <linux/module.h>
26 #include <linux/cache.h>
27 #include <linux/audit.h>
28 #include <net/dst.h>
29 #include <net/flow.h>
30 #include <net/xfrm.h>
31 #include <net/ip.h>
32 #ifdef CONFIG_XFRM_STATISTICS
33 #include <net/snmp.h>
34 #endif
35
36 #include "xfrm_hash.h"
37
38 #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
42 DEFINE_MUTEX(xfrm_cfg_mutex);
43 EXPORT_SYMBOL(xfrm_cfg_mutex);
44
45 static DEFINE_SPINLOCK(xfrm_policy_sk_bundle_lock);
46 static struct dst_entry *xfrm_policy_sk_bundles;
47 static DEFINE_RWLOCK(xfrm_policy_lock);
48
49 static DEFINE_SPINLOCK(xfrm_policy_afinfo_lock);
50 static struct xfrm_policy_afinfo __rcu *xfrm_policy_afinfo[NPROTO]
51 __read_mostly;
52
53 static struct kmem_cache *xfrm_dst_cache __read_mostly;
54
55 static void xfrm_init_pmtu(struct dst_entry *dst);
56 static int stale_bundle(struct dst_entry *dst);
57 static int xfrm_bundle_ok(struct xfrm_dst *xdst);
58 static void xfrm_policy_queue_process(unsigned long arg);
59
60 static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
61 int dir);
62
63 static inline bool
__xfrm4_selector_match(const struct xfrm_selector * sel,const struct flowi * fl)64 __xfrm4_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
65 {
66 const struct flowi4 *fl4 = &fl->u.ip4;
67
68 return addr4_match(fl4->daddr, sel->daddr.a4, sel->prefixlen_d) &&
69 addr4_match(fl4->saddr, sel->saddr.a4, sel->prefixlen_s) &&
70 !((xfrm_flowi_dport(fl, &fl4->uli) ^ sel->dport) & sel->dport_mask) &&
71 !((xfrm_flowi_sport(fl, &fl4->uli) ^ sel->sport) & sel->sport_mask) &&
72 (fl4->flowi4_proto == sel->proto || !sel->proto) &&
73 (fl4->flowi4_oif == sel->ifindex || !sel->ifindex);
74 }
75
76 static inline bool
__xfrm6_selector_match(const struct xfrm_selector * sel,const struct flowi * fl)77 __xfrm6_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
78 {
79 const struct flowi6 *fl6 = &fl->u.ip6;
80
81 return addr_match(&fl6->daddr, &sel->daddr, sel->prefixlen_d) &&
82 addr_match(&fl6->saddr, &sel->saddr, sel->prefixlen_s) &&
83 !((xfrm_flowi_dport(fl, &fl6->uli) ^ sel->dport) & sel->dport_mask) &&
84 !((xfrm_flowi_sport(fl, &fl6->uli) ^ sel->sport) & sel->sport_mask) &&
85 (fl6->flowi6_proto == sel->proto || !sel->proto) &&
86 (fl6->flowi6_oif == sel->ifindex || !sel->ifindex);
87 }
88
xfrm_selector_match(const struct xfrm_selector * sel,const struct flowi * fl,unsigned short family)89 bool xfrm_selector_match(const struct xfrm_selector *sel, const struct flowi *fl,
90 unsigned short family)
91 {
92 switch (family) {
93 case AF_INET:
94 return __xfrm4_selector_match(sel, fl);
95 case AF_INET6:
96 return __xfrm6_selector_match(sel, fl);
97 }
98 return false;
99 }
100
xfrm_policy_get_afinfo(unsigned short family)101 static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
102 {
103 struct xfrm_policy_afinfo *afinfo;
104
105 if (unlikely(family >= NPROTO))
106 return NULL;
107 rcu_read_lock();
108 afinfo = rcu_dereference(xfrm_policy_afinfo[family]);
109 if (unlikely(!afinfo))
110 rcu_read_unlock();
111 return afinfo;
112 }
113
xfrm_policy_put_afinfo(struct xfrm_policy_afinfo * afinfo)114 static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
115 {
116 rcu_read_unlock();
117 }
118
__xfrm_dst_lookup(struct net * net,int tos,const xfrm_address_t * saddr,const xfrm_address_t * daddr,int family,u32 mark)119 static inline struct dst_entry *__xfrm_dst_lookup(struct net *net, int tos,
120 const xfrm_address_t *saddr,
121 const xfrm_address_t *daddr,
122 int family, u32 mark)
123 {
124 struct xfrm_policy_afinfo *afinfo;
125 struct dst_entry *dst;
126
127 afinfo = xfrm_policy_get_afinfo(family);
128 if (unlikely(afinfo == NULL))
129 return ERR_PTR(-EAFNOSUPPORT);
130
131 dst = afinfo->dst_lookup(net, tos, saddr, daddr, mark);
132
133 xfrm_policy_put_afinfo(afinfo);
134
135 return dst;
136 }
137
xfrm_dst_lookup(struct xfrm_state * x,int tos,xfrm_address_t * prev_saddr,xfrm_address_t * prev_daddr,int family,u32 mark)138 static inline struct dst_entry *xfrm_dst_lookup(struct xfrm_state *x, int tos,
139 xfrm_address_t *prev_saddr,
140 xfrm_address_t *prev_daddr,
141 int family, u32 mark)
142 {
143 struct net *net = xs_net(x);
144 xfrm_address_t *saddr = &x->props.saddr;
145 xfrm_address_t *daddr = &x->id.daddr;
146 struct dst_entry *dst;
147
148 if (x->type->flags & XFRM_TYPE_LOCAL_COADDR) {
149 saddr = x->coaddr;
150 daddr = prev_daddr;
151 }
152 if (x->type->flags & XFRM_TYPE_REMOTE_COADDR) {
153 saddr = prev_saddr;
154 daddr = x->coaddr;
155 }
156
157 dst = __xfrm_dst_lookup(net, tos, saddr, daddr, family, mark);
158
159 if (!IS_ERR(dst)) {
160 if (prev_saddr != saddr)
161 memcpy(prev_saddr, saddr, sizeof(*prev_saddr));
162 if (prev_daddr != daddr)
163 memcpy(prev_daddr, daddr, sizeof(*prev_daddr));
164 }
165
166 return dst;
167 }
168
make_jiffies(long secs)169 static inline unsigned long make_jiffies(long secs)
170 {
171 if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
172 return MAX_SCHEDULE_TIMEOUT-1;
173 else
174 return secs*HZ;
175 }
176
xfrm_policy_timer(unsigned long data)177 static void xfrm_policy_timer(unsigned long data)
178 {
179 struct xfrm_policy *xp = (struct xfrm_policy*)data;
180 unsigned long now = get_seconds();
181 long next = LONG_MAX;
182 int warn = 0;
183 int dir;
184
185 read_lock(&xp->lock);
186
187 if (unlikely(xp->walk.dead))
188 goto out;
189
190 dir = xfrm_policy_id2dir(xp->index);
191
192 if (xp->lft.hard_add_expires_seconds) {
193 long tmo = xp->lft.hard_add_expires_seconds +
194 xp->curlft.add_time - now;
195 if (tmo <= 0)
196 goto expired;
197 if (tmo < next)
198 next = tmo;
199 }
200 if (xp->lft.hard_use_expires_seconds) {
201 long tmo = xp->lft.hard_use_expires_seconds +
202 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
203 if (tmo <= 0)
204 goto expired;
205 if (tmo < next)
206 next = tmo;
207 }
208 if (xp->lft.soft_add_expires_seconds) {
209 long tmo = xp->lft.soft_add_expires_seconds +
210 xp->curlft.add_time - now;
211 if (tmo <= 0) {
212 warn = 1;
213 tmo = XFRM_KM_TIMEOUT;
214 }
215 if (tmo < next)
216 next = tmo;
217 }
218 if (xp->lft.soft_use_expires_seconds) {
219 long tmo = xp->lft.soft_use_expires_seconds +
220 (xp->curlft.use_time ? : xp->curlft.add_time) - now;
221 if (tmo <= 0) {
222 warn = 1;
223 tmo = XFRM_KM_TIMEOUT;
224 }
225 if (tmo < next)
226 next = tmo;
227 }
228
229 if (warn)
230 km_policy_expired(xp, dir, 0, 0);
231 if (next != LONG_MAX &&
232 !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
233 xfrm_pol_hold(xp);
234
235 out:
236 read_unlock(&xp->lock);
237 xfrm_pol_put(xp);
238 return;
239
240 expired:
241 read_unlock(&xp->lock);
242 if (!xfrm_policy_delete(xp, dir))
243 km_policy_expired(xp, dir, 1, 0);
244 xfrm_pol_put(xp);
245 }
246
xfrm_policy_flo_get(struct flow_cache_object * flo)247 static struct flow_cache_object *xfrm_policy_flo_get(struct flow_cache_object *flo)
248 {
249 struct xfrm_policy *pol = container_of(flo, struct xfrm_policy, flo);
250
251 if (unlikely(pol->walk.dead))
252 flo = NULL;
253 else
254 xfrm_pol_hold(pol);
255
256 return flo;
257 }
258
xfrm_policy_flo_check(struct flow_cache_object * flo)259 static int xfrm_policy_flo_check(struct flow_cache_object *flo)
260 {
261 struct xfrm_policy *pol = container_of(flo, struct xfrm_policy, flo);
262
263 return !pol->walk.dead;
264 }
265
xfrm_policy_flo_delete(struct flow_cache_object * flo)266 static void xfrm_policy_flo_delete(struct flow_cache_object *flo)
267 {
268 xfrm_pol_put(container_of(flo, struct xfrm_policy, flo));
269 }
270
271 static const struct flow_cache_ops xfrm_policy_fc_ops = {
272 .get = xfrm_policy_flo_get,
273 .check = xfrm_policy_flo_check,
274 .delete = xfrm_policy_flo_delete,
275 };
276
277 /* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
278 * SPD calls.
279 */
280
xfrm_policy_alloc(struct net * net,gfp_t gfp)281 struct xfrm_policy *xfrm_policy_alloc(struct net *net, gfp_t gfp)
282 {
283 struct xfrm_policy *policy;
284
285 policy = kzalloc(sizeof(struct xfrm_policy), gfp);
286
287 if (policy) {
288 write_pnet(&policy->xp_net, net);
289 INIT_LIST_HEAD(&policy->walk.all);
290 INIT_HLIST_NODE(&policy->bydst);
291 INIT_HLIST_NODE(&policy->byidx);
292 rwlock_init(&policy->lock);
293 atomic_set(&policy->refcnt, 1);
294 skb_queue_head_init(&policy->polq.hold_queue);
295 setup_timer(&policy->timer, xfrm_policy_timer,
296 (unsigned long)policy);
297 setup_timer(&policy->polq.hold_timer, xfrm_policy_queue_process,
298 (unsigned long)policy);
299 policy->flo.ops = &xfrm_policy_fc_ops;
300 }
301 return policy;
302 }
303 EXPORT_SYMBOL(xfrm_policy_alloc);
304
305 /* Destroy xfrm_policy: descendant resources must be released to this moment. */
306
xfrm_policy_destroy(struct xfrm_policy * policy)307 void xfrm_policy_destroy(struct xfrm_policy *policy)
308 {
309 BUG_ON(!policy->walk.dead);
310
311 if (del_timer(&policy->timer))
312 BUG();
313
314 security_xfrm_policy_free(policy->security);
315 kfree(policy);
316 }
317 EXPORT_SYMBOL(xfrm_policy_destroy);
318
xfrm_queue_purge(struct sk_buff_head * list)319 static void xfrm_queue_purge(struct sk_buff_head *list)
320 {
321 struct sk_buff *skb;
322
323 while ((skb = skb_dequeue(list)) != NULL) {
324 dev_put(skb->dev);
325 kfree_skb(skb);
326 }
327 }
328
329 /* Rule must be locked. Release descentant resources, announce
330 * entry dead. The rule must be unlinked from lists to the moment.
331 */
332
xfrm_policy_kill(struct xfrm_policy * policy)333 static void xfrm_policy_kill(struct xfrm_policy *policy)
334 {
335 policy->walk.dead = 1;
336
337 atomic_inc(&policy->genid);
338
339 del_timer(&policy->polq.hold_timer);
340 xfrm_queue_purge(&policy->polq.hold_queue);
341
342 if (del_timer(&policy->timer))
343 xfrm_pol_put(policy);
344
345 xfrm_pol_put(policy);
346 }
347
348 static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024;
349
idx_hash(struct net * net,u32 index)350 static inline unsigned int idx_hash(struct net *net, u32 index)
351 {
352 return __idx_hash(index, net->xfrm.policy_idx_hmask);
353 }
354
policy_hash_bysel(struct net * net,const struct xfrm_selector * sel,unsigned short family,int dir)355 static struct hlist_head *policy_hash_bysel(struct net *net,
356 const struct xfrm_selector *sel,
357 unsigned short family, int dir)
358 {
359 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
360 unsigned int hash = __sel_hash(sel, family, hmask);
361
362 return (hash == hmask + 1 ?
363 &net->xfrm.policy_inexact[dir] :
364 net->xfrm.policy_bydst[dir].table + hash);
365 }
366
policy_hash_direct(struct net * net,const xfrm_address_t * daddr,const xfrm_address_t * saddr,unsigned short family,int dir)367 static struct hlist_head *policy_hash_direct(struct net *net,
368 const xfrm_address_t *daddr,
369 const xfrm_address_t *saddr,
370 unsigned short family, int dir)
371 {
372 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
373 unsigned int hash = __addr_hash(daddr, saddr, family, hmask);
374
375 return net->xfrm.policy_bydst[dir].table + hash;
376 }
377
xfrm_dst_hash_transfer(struct hlist_head * list,struct hlist_head * ndsttable,unsigned int nhashmask)378 static void xfrm_dst_hash_transfer(struct hlist_head *list,
379 struct hlist_head *ndsttable,
380 unsigned int nhashmask)
381 {
382 struct hlist_node *tmp, *entry0 = NULL;
383 struct xfrm_policy *pol;
384 unsigned int h0 = 0;
385
386 redo:
387 hlist_for_each_entry_safe(pol, tmp, list, bydst) {
388 unsigned int h;
389
390 h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
391 pol->family, nhashmask);
392 if (!entry0) {
393 hlist_del(&pol->bydst);
394 hlist_add_head(&pol->bydst, ndsttable+h);
395 h0 = h;
396 } else {
397 if (h != h0)
398 continue;
399 hlist_del(&pol->bydst);
400 hlist_add_after(entry0, &pol->bydst);
401 }
402 entry0 = &pol->bydst;
403 }
404 if (!hlist_empty(list)) {
405 entry0 = NULL;
406 goto redo;
407 }
408 }
409
xfrm_idx_hash_transfer(struct hlist_head * list,struct hlist_head * nidxtable,unsigned int nhashmask)410 static void xfrm_idx_hash_transfer(struct hlist_head *list,
411 struct hlist_head *nidxtable,
412 unsigned int nhashmask)
413 {
414 struct hlist_node *tmp;
415 struct xfrm_policy *pol;
416
417 hlist_for_each_entry_safe(pol, tmp, list, byidx) {
418 unsigned int h;
419
420 h = __idx_hash(pol->index, nhashmask);
421 hlist_add_head(&pol->byidx, nidxtable+h);
422 }
423 }
424
xfrm_new_hash_mask(unsigned int old_hmask)425 static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
426 {
427 return ((old_hmask + 1) << 1) - 1;
428 }
429
xfrm_bydst_resize(struct net * net,int dir)430 static void xfrm_bydst_resize(struct net *net, int dir)
431 {
432 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
433 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
434 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
435 struct hlist_head *odst = net->xfrm.policy_bydst[dir].table;
436 struct hlist_head *ndst = xfrm_hash_alloc(nsize);
437 int i;
438
439 if (!ndst)
440 return;
441
442 write_lock_bh(&xfrm_policy_lock);
443
444 for (i = hmask; i >= 0; i--)
445 xfrm_dst_hash_transfer(odst + i, ndst, nhashmask);
446
447 net->xfrm.policy_bydst[dir].table = ndst;
448 net->xfrm.policy_bydst[dir].hmask = nhashmask;
449
450 write_unlock_bh(&xfrm_policy_lock);
451
452 xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
453 }
454
xfrm_byidx_resize(struct net * net,int total)455 static void xfrm_byidx_resize(struct net *net, int total)
456 {
457 unsigned int hmask = net->xfrm.policy_idx_hmask;
458 unsigned int nhashmask = xfrm_new_hash_mask(hmask);
459 unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
460 struct hlist_head *oidx = net->xfrm.policy_byidx;
461 struct hlist_head *nidx = xfrm_hash_alloc(nsize);
462 int i;
463
464 if (!nidx)
465 return;
466
467 write_lock_bh(&xfrm_policy_lock);
468
469 for (i = hmask; i >= 0; i--)
470 xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
471
472 net->xfrm.policy_byidx = nidx;
473 net->xfrm.policy_idx_hmask = nhashmask;
474
475 write_unlock_bh(&xfrm_policy_lock);
476
477 xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
478 }
479
xfrm_bydst_should_resize(struct net * net,int dir,int * total)480 static inline int xfrm_bydst_should_resize(struct net *net, int dir, int *total)
481 {
482 unsigned int cnt = net->xfrm.policy_count[dir];
483 unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
484
485 if (total)
486 *total += cnt;
487
488 if ((hmask + 1) < xfrm_policy_hashmax &&
489 cnt > hmask)
490 return 1;
491
492 return 0;
493 }
494
xfrm_byidx_should_resize(struct net * net,int total)495 static inline int xfrm_byidx_should_resize(struct net *net, int total)
496 {
497 unsigned int hmask = net->xfrm.policy_idx_hmask;
498
499 if ((hmask + 1) < xfrm_policy_hashmax &&
500 total > hmask)
501 return 1;
502
503 return 0;
504 }
505
xfrm_spd_getinfo(struct net * net,struct xfrmk_spdinfo * si)506 void xfrm_spd_getinfo(struct net *net, struct xfrmk_spdinfo *si)
507 {
508 read_lock_bh(&xfrm_policy_lock);
509 si->incnt = net->xfrm.policy_count[XFRM_POLICY_IN];
510 si->outcnt = net->xfrm.policy_count[XFRM_POLICY_OUT];
511 si->fwdcnt = net->xfrm.policy_count[XFRM_POLICY_FWD];
512 si->inscnt = net->xfrm.policy_count[XFRM_POLICY_IN+XFRM_POLICY_MAX];
513 si->outscnt = net->xfrm.policy_count[XFRM_POLICY_OUT+XFRM_POLICY_MAX];
514 si->fwdscnt = net->xfrm.policy_count[XFRM_POLICY_FWD+XFRM_POLICY_MAX];
515 si->spdhcnt = net->xfrm.policy_idx_hmask;
516 si->spdhmcnt = xfrm_policy_hashmax;
517 read_unlock_bh(&xfrm_policy_lock);
518 }
519 EXPORT_SYMBOL(xfrm_spd_getinfo);
520
521 static DEFINE_MUTEX(hash_resize_mutex);
xfrm_hash_resize(struct work_struct * work)522 static void xfrm_hash_resize(struct work_struct *work)
523 {
524 struct net *net = container_of(work, struct net, xfrm.policy_hash_work);
525 int dir, total;
526
527 mutex_lock(&hash_resize_mutex);
528
529 total = 0;
530 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
531 if (xfrm_bydst_should_resize(net, dir, &total))
532 xfrm_bydst_resize(net, dir);
533 }
534 if (xfrm_byidx_should_resize(net, total))
535 xfrm_byidx_resize(net, total);
536
537 mutex_unlock(&hash_resize_mutex);
538 }
539
540 /* Generate new index... KAME seems to generate them ordered by cost
541 * of an absolute inpredictability of ordering of rules. This will not pass. */
xfrm_gen_index(struct net * net,int dir)542 static u32 xfrm_gen_index(struct net *net, int dir)
543 {
544 static u32 idx_generator;
545
546 for (;;) {
547 struct hlist_head *list;
548 struct xfrm_policy *p;
549 u32 idx;
550 int found;
551
552 idx = (idx_generator | dir);
553 idx_generator += 8;
554 if (idx == 0)
555 idx = 8;
556 list = net->xfrm.policy_byidx + idx_hash(net, idx);
557 found = 0;
558 hlist_for_each_entry(p, list, byidx) {
559 if (p->index == idx) {
560 found = 1;
561 break;
562 }
563 }
564 if (!found)
565 return idx;
566 }
567 }
568
selector_cmp(struct xfrm_selector * s1,struct xfrm_selector * s2)569 static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
570 {
571 u32 *p1 = (u32 *) s1;
572 u32 *p2 = (u32 *) s2;
573 int len = sizeof(struct xfrm_selector) / sizeof(u32);
574 int i;
575
576 for (i = 0; i < len; i++) {
577 if (p1[i] != p2[i])
578 return 1;
579 }
580
581 return 0;
582 }
583
xfrm_policy_requeue(struct xfrm_policy * old,struct xfrm_policy * new)584 static void xfrm_policy_requeue(struct xfrm_policy *old,
585 struct xfrm_policy *new)
586 {
587 struct xfrm_policy_queue *pq = &old->polq;
588 struct sk_buff_head list;
589
590 __skb_queue_head_init(&list);
591
592 spin_lock_bh(&pq->hold_queue.lock);
593 skb_queue_splice_init(&pq->hold_queue, &list);
594 del_timer(&pq->hold_timer);
595 spin_unlock_bh(&pq->hold_queue.lock);
596
597 if (skb_queue_empty(&list))
598 return;
599
600 pq = &new->polq;
601
602 spin_lock_bh(&pq->hold_queue.lock);
603 skb_queue_splice(&list, &pq->hold_queue);
604 pq->timeout = XFRM_QUEUE_TMO_MIN;
605 mod_timer(&pq->hold_timer, jiffies);
606 spin_unlock_bh(&pq->hold_queue.lock);
607 }
608
xfrm_policy_mark_match(struct xfrm_policy * policy,struct xfrm_policy * pol)609 static 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
xfrm_policy_insert(int dir,struct xfrm_policy * policy,int excl)624 int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
625 {
626 struct net *net = xp_net(policy);
627 struct xfrm_policy *pol;
628 struct xfrm_policy *delpol;
629 struct hlist_head *chain;
630 struct hlist_node *newpos;
631
632 write_lock_bh(&xfrm_policy_lock);
633 chain = policy_hash_bysel(net, &policy->selector, policy->family, dir);
634 delpol = NULL;
635 newpos = NULL;
636 hlist_for_each_entry(pol, chain, bydst) {
637 if (pol->type == policy->type &&
638 !selector_cmp(&pol->selector, &policy->selector) &&
639 xfrm_policy_mark_match(policy, pol) &&
640 xfrm_sec_ctx_match(pol->security, policy->security) &&
641 !WARN_ON(delpol)) {
642 if (excl) {
643 write_unlock_bh(&xfrm_policy_lock);
644 return -EEXIST;
645 }
646 delpol = pol;
647 if (policy->priority > pol->priority)
648 continue;
649 } else if (policy->priority >= pol->priority) {
650 newpos = &pol->bydst;
651 continue;
652 }
653 if (delpol)
654 break;
655 }
656 if (newpos)
657 hlist_add_after(newpos, &policy->bydst);
658 else
659 hlist_add_head(&policy->bydst, chain);
660 xfrm_pol_hold(policy);
661 net->xfrm.policy_count[dir]++;
662 atomic_inc(&flow_cache_genid);
663 rt_genid_bump(net);
664 if (delpol) {
665 xfrm_policy_requeue(delpol, policy);
666 __xfrm_policy_unlink(delpol, dir);
667 }
668 policy->index = delpol ? delpol->index : xfrm_gen_index(net, dir);
669 hlist_add_head(&policy->byidx, net->xfrm.policy_byidx+idx_hash(net, policy->index));
670 policy->curlft.add_time = get_seconds();
671 policy->curlft.use_time = 0;
672 if (!mod_timer(&policy->timer, jiffies + HZ))
673 xfrm_pol_hold(policy);
674 list_add(&policy->walk.all, &net->xfrm.policy_all);
675 write_unlock_bh(&xfrm_policy_lock);
676
677 if (delpol)
678 xfrm_policy_kill(delpol);
679 else if (xfrm_bydst_should_resize(net, dir, NULL))
680 schedule_work(&net->xfrm.policy_hash_work);
681
682 return 0;
683 }
684 EXPORT_SYMBOL(xfrm_policy_insert);
685
xfrm_policy_bysel_ctx(struct net * net,u32 mark,u8 type,int dir,struct xfrm_selector * sel,struct xfrm_sec_ctx * ctx,int delete,int * err)686 struct xfrm_policy *xfrm_policy_bysel_ctx(struct net *net, u32 mark, u8 type,
687 int dir, struct xfrm_selector *sel,
688 struct xfrm_sec_ctx *ctx, int delete,
689 int *err)
690 {
691 struct xfrm_policy *pol, *ret;
692 struct hlist_head *chain;
693
694 *err = 0;
695 write_lock_bh(&xfrm_policy_lock);
696 chain = policy_hash_bysel(net, sel, sel->family, dir);
697 ret = NULL;
698 hlist_for_each_entry(pol, chain, bydst) {
699 if (pol->type == type &&
700 (mark & pol->mark.m) == pol->mark.v &&
701 !selector_cmp(sel, &pol->selector) &&
702 xfrm_sec_ctx_match(ctx, pol->security)) {
703 xfrm_pol_hold(pol);
704 if (delete) {
705 *err = security_xfrm_policy_delete(
706 pol->security);
707 if (*err) {
708 write_unlock_bh(&xfrm_policy_lock);
709 return pol;
710 }
711 __xfrm_policy_unlink(pol, dir);
712 }
713 ret = pol;
714 break;
715 }
716 }
717 write_unlock_bh(&xfrm_policy_lock);
718
719 if (ret && delete)
720 xfrm_policy_kill(ret);
721 return ret;
722 }
723 EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
724
xfrm_policy_byid(struct net * net,u32 mark,u8 type,int dir,u32 id,int delete,int * err)725 struct xfrm_policy *xfrm_policy_byid(struct net *net, u32 mark, u8 type,
726 int dir, u32 id, int delete, int *err)
727 {
728 struct xfrm_policy *pol, *ret;
729 struct hlist_head *chain;
730
731 *err = -ENOENT;
732 if (xfrm_policy_id2dir(id) != dir)
733 return NULL;
734
735 *err = 0;
736 write_lock_bh(&xfrm_policy_lock);
737 chain = net->xfrm.policy_byidx + idx_hash(net, id);
738 ret = NULL;
739 hlist_for_each_entry(pol, chain, byidx) {
740 if (pol->type == type && pol->index == id &&
741 (mark & pol->mark.m) == pol->mark.v) {
742 xfrm_pol_hold(pol);
743 if (delete) {
744 *err = security_xfrm_policy_delete(
745 pol->security);
746 if (*err) {
747 write_unlock_bh(&xfrm_policy_lock);
748 return pol;
749 }
750 __xfrm_policy_unlink(pol, dir);
751 }
752 ret = pol;
753 break;
754 }
755 }
756 write_unlock_bh(&xfrm_policy_lock);
757
758 if (ret && delete)
759 xfrm_policy_kill(ret);
760 return ret;
761 }
762 EXPORT_SYMBOL(xfrm_policy_byid);
763
764 #ifdef CONFIG_SECURITY_NETWORK_XFRM
765 static inline int
xfrm_policy_flush_secctx_check(struct net * net,u8 type,struct xfrm_audit * audit_info)766 xfrm_policy_flush_secctx_check(struct net *net, u8 type, struct xfrm_audit *audit_info)
767 {
768 int dir, err = 0;
769
770 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
771 struct xfrm_policy *pol;
772 int i;
773
774 hlist_for_each_entry(pol,
775 &net->xfrm.policy_inexact[dir], bydst) {
776 if (pol->type != type)
777 continue;
778 err = security_xfrm_policy_delete(pol->security);
779 if (err) {
780 xfrm_audit_policy_delete(pol, 0,
781 audit_info->loginuid,
782 audit_info->sessionid,
783 audit_info->secid);
784 return err;
785 }
786 }
787 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
788 hlist_for_each_entry(pol,
789 net->xfrm.policy_bydst[dir].table + i,
790 bydst) {
791 if (pol->type != type)
792 continue;
793 err = security_xfrm_policy_delete(
794 pol->security);
795 if (err) {
796 xfrm_audit_policy_delete(pol, 0,
797 audit_info->loginuid,
798 audit_info->sessionid,
799 audit_info->secid);
800 return err;
801 }
802 }
803 }
804 }
805 return err;
806 }
807 #else
808 static inline int
xfrm_policy_flush_secctx_check(struct net * net,u8 type,struct xfrm_audit * audit_info)809 xfrm_policy_flush_secctx_check(struct net *net, u8 type, struct xfrm_audit *audit_info)
810 {
811 return 0;
812 }
813 #endif
814
xfrm_policy_flush(struct net * net,u8 type,struct xfrm_audit * audit_info)815 int xfrm_policy_flush(struct net *net, u8 type, struct xfrm_audit *audit_info)
816 {
817 int dir, err = 0, cnt = 0;
818
819 write_lock_bh(&xfrm_policy_lock);
820
821 err = xfrm_policy_flush_secctx_check(net, type, audit_info);
822 if (err)
823 goto out;
824
825 for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
826 struct xfrm_policy *pol;
827 int i;
828
829 again1:
830 hlist_for_each_entry(pol,
831 &net->xfrm.policy_inexact[dir], bydst) {
832 if (pol->type != type)
833 continue;
834 __xfrm_policy_unlink(pol, dir);
835 write_unlock_bh(&xfrm_policy_lock);
836 cnt++;
837
838 xfrm_audit_policy_delete(pol, 1, audit_info->loginuid,
839 audit_info->sessionid,
840 audit_info->secid);
841
842 xfrm_policy_kill(pol);
843
844 write_lock_bh(&xfrm_policy_lock);
845 goto again1;
846 }
847
848 for (i = net->xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
849 again2:
850 hlist_for_each_entry(pol,
851 net->xfrm.policy_bydst[dir].table + i,
852 bydst) {
853 if (pol->type != type)
854 continue;
855 __xfrm_policy_unlink(pol, dir);
856 write_unlock_bh(&xfrm_policy_lock);
857 cnt++;
858
859 xfrm_audit_policy_delete(pol, 1,
860 audit_info->loginuid,
861 audit_info->sessionid,
862 audit_info->secid);
863 xfrm_policy_kill(pol);
864
865 write_lock_bh(&xfrm_policy_lock);
866 goto again2;
867 }
868 }
869
870 }
871 if (!cnt)
872 err = -ESRCH;
873 out:
874 write_unlock_bh(&xfrm_policy_lock);
875 return err;
876 }
877 EXPORT_SYMBOL(xfrm_policy_flush);
878
xfrm_policy_walk(struct net * net,struct xfrm_policy_walk * walk,int (* func)(struct xfrm_policy *,int,int,void *),void * data)879 int xfrm_policy_walk(struct net *net, struct xfrm_policy_walk *walk,
880 int (*func)(struct xfrm_policy *, int, int, void*),
881 void *data)
882 {
883 struct xfrm_policy *pol;
884 struct xfrm_policy_walk_entry *x;
885 int error = 0;
886
887 if (walk->type >= XFRM_POLICY_TYPE_MAX &&
888 walk->type != XFRM_POLICY_TYPE_ANY)
889 return -EINVAL;
890
891 if (list_empty(&walk->walk.all) && walk->seq != 0)
892 return 0;
893
894 write_lock_bh(&xfrm_policy_lock);
895 if (list_empty(&walk->walk.all))
896 x = list_first_entry(&net->xfrm.policy_all, struct xfrm_policy_walk_entry, all);
897 else
898 x = list_entry(&walk->walk.all, struct xfrm_policy_walk_entry, all);
899 list_for_each_entry_from(x, &net->xfrm.policy_all, all) {
900 if (x->dead)
901 continue;
902 pol = container_of(x, struct xfrm_policy, walk);
903 if (walk->type != XFRM_POLICY_TYPE_ANY &&
904 walk->type != pol->type)
905 continue;
906 error = func(pol, xfrm_policy_id2dir(pol->index),
907 walk->seq, data);
908 if (error) {
909 list_move_tail(&walk->walk.all, &x->all);
910 goto out;
911 }
912 walk->seq++;
913 }
914 if (walk->seq == 0) {
915 error = -ENOENT;
916 goto out;
917 }
918 list_del_init(&walk->walk.all);
919 out:
920 write_unlock_bh(&xfrm_policy_lock);
921 return error;
922 }
923 EXPORT_SYMBOL(xfrm_policy_walk);
924
xfrm_policy_walk_init(struct xfrm_policy_walk * walk,u8 type)925 void xfrm_policy_walk_init(struct xfrm_policy_walk *walk, u8 type)
926 {
927 INIT_LIST_HEAD(&walk->walk.all);
928 walk->walk.dead = 1;
929 walk->type = type;
930 walk->seq = 0;
931 }
932 EXPORT_SYMBOL(xfrm_policy_walk_init);
933
xfrm_policy_walk_done(struct xfrm_policy_walk * walk)934 void xfrm_policy_walk_done(struct xfrm_policy_walk *walk)
935 {
936 if (list_empty(&walk->walk.all))
937 return;
938
939 write_lock_bh(&xfrm_policy_lock);
940 list_del(&walk->walk.all);
941 write_unlock_bh(&xfrm_policy_lock);
942 }
943 EXPORT_SYMBOL(xfrm_policy_walk_done);
944
945 /*
946 * Find policy to apply to this flow.
947 *
948 * Returns 0 if policy found, else an -errno.
949 */
xfrm_policy_match(const struct xfrm_policy * pol,const struct flowi * fl,u8 type,u16 family,int dir)950 static int xfrm_policy_match(const struct xfrm_policy *pol,
951 const struct flowi *fl,
952 u8 type, u16 family, int dir)
953 {
954 const struct xfrm_selector *sel = &pol->selector;
955 int ret = -ESRCH;
956 bool match;
957
958 if (pol->family != family ||
959 (fl->flowi_mark & pol->mark.m) != pol->mark.v ||
960 pol->type != type)
961 return ret;
962
963 match = xfrm_selector_match(sel, fl, family);
964 if (match)
965 ret = security_xfrm_policy_lookup(pol->security, fl->flowi_secid,
966 dir);
967
968 return ret;
969 }
970
xfrm_policy_lookup_bytype(struct net * net,u8 type,const struct flowi * fl,u16 family,u8 dir)971 static struct xfrm_policy *xfrm_policy_lookup_bytype(struct net *net, u8 type,
972 const struct flowi *fl,
973 u16 family, u8 dir)
974 {
975 int err;
976 struct xfrm_policy *pol, *ret;
977 const xfrm_address_t *daddr, *saddr;
978 struct hlist_head *chain;
979 u32 priority = ~0U;
980
981 daddr = xfrm_flowi_daddr(fl, family);
982 saddr = xfrm_flowi_saddr(fl, family);
983 if (unlikely(!daddr || !saddr))
984 return NULL;
985
986 read_lock_bh(&xfrm_policy_lock);
987 chain = policy_hash_direct(net, daddr, saddr, family, dir);
988 ret = NULL;
989 hlist_for_each_entry(pol, chain, bydst) {
990 err = xfrm_policy_match(pol, fl, type, family, dir);
991 if (err) {
992 if (err == -ESRCH)
993 continue;
994 else {
995 ret = ERR_PTR(err);
996 goto fail;
997 }
998 } else {
999 ret = pol;
1000 priority = ret->priority;
1001 break;
1002 }
1003 }
1004 chain = &net->xfrm.policy_inexact[dir];
1005 hlist_for_each_entry(pol, chain, bydst) {
1006 err = xfrm_policy_match(pol, fl, type, family, dir);
1007 if (err) {
1008 if (err == -ESRCH)
1009 continue;
1010 else {
1011 ret = ERR_PTR(err);
1012 goto fail;
1013 }
1014 } else if (pol->priority < priority) {
1015 ret = pol;
1016 break;
1017 }
1018 }
1019 if (ret)
1020 xfrm_pol_hold(ret);
1021 fail:
1022 read_unlock_bh(&xfrm_policy_lock);
1023
1024 return ret;
1025 }
1026
1027 static struct xfrm_policy *
__xfrm_policy_lookup(struct net * net,const struct flowi * fl,u16 family,u8 dir)1028 __xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir)
1029 {
1030 #ifdef CONFIG_XFRM_SUB_POLICY
1031 struct xfrm_policy *pol;
1032
1033 pol = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_SUB, fl, family, dir);
1034 if (pol != NULL)
1035 return pol;
1036 #endif
1037 return xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN, fl, family, dir);
1038 }
1039
flow_to_policy_dir(int dir)1040 static int flow_to_policy_dir(int dir)
1041 {
1042 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
1043 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1044 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1045 return dir;
1046
1047 switch (dir) {
1048 default:
1049 case FLOW_DIR_IN:
1050 return XFRM_POLICY_IN;
1051 case FLOW_DIR_OUT:
1052 return XFRM_POLICY_OUT;
1053 case FLOW_DIR_FWD:
1054 return XFRM_POLICY_FWD;
1055 }
1056 }
1057
1058 static struct flow_cache_object *
xfrm_policy_lookup(struct net * net,const struct flowi * fl,u16 family,u8 dir,struct flow_cache_object * old_obj,void * ctx)1059 xfrm_policy_lookup(struct net *net, const struct flowi *fl, u16 family,
1060 u8 dir, struct flow_cache_object *old_obj, void *ctx)
1061 {
1062 struct xfrm_policy *pol;
1063
1064 if (old_obj)
1065 xfrm_pol_put(container_of(old_obj, struct xfrm_policy, flo));
1066
1067 pol = __xfrm_policy_lookup(net, fl, family, flow_to_policy_dir(dir));
1068 if (IS_ERR_OR_NULL(pol))
1069 return ERR_CAST(pol);
1070
1071 /* Resolver returns two references:
1072 * one for cache and one for caller of flow_cache_lookup() */
1073 xfrm_pol_hold(pol);
1074
1075 return &pol->flo;
1076 }
1077
policy_to_flow_dir(int dir)1078 static inline int policy_to_flow_dir(int dir)
1079 {
1080 if (XFRM_POLICY_IN == FLOW_DIR_IN &&
1081 XFRM_POLICY_OUT == FLOW_DIR_OUT &&
1082 XFRM_POLICY_FWD == FLOW_DIR_FWD)
1083 return dir;
1084 switch (dir) {
1085 default:
1086 case XFRM_POLICY_IN:
1087 return FLOW_DIR_IN;
1088 case XFRM_POLICY_OUT:
1089 return FLOW_DIR_OUT;
1090 case XFRM_POLICY_FWD:
1091 return FLOW_DIR_FWD;
1092 }
1093 }
1094
xfrm_sk_policy_lookup(struct sock * sk,int dir,const struct flowi * fl)1095 static struct xfrm_policy *xfrm_sk_policy_lookup(struct sock *sk, int dir,
1096 const struct flowi *fl)
1097 {
1098 struct xfrm_policy *pol;
1099
1100 read_lock_bh(&xfrm_policy_lock);
1101 if ((pol = sk->sk_policy[dir]) != NULL) {
1102 bool match = xfrm_selector_match(&pol->selector, fl,
1103 sk->sk_family);
1104 int err = 0;
1105
1106 if (match) {
1107 if ((sk->sk_mark & pol->mark.m) != pol->mark.v) {
1108 pol = NULL;
1109 goto out;
1110 }
1111 err = security_xfrm_policy_lookup(pol->security,
1112 fl->flowi_secid,
1113 policy_to_flow_dir(dir));
1114 if (!err)
1115 xfrm_pol_hold(pol);
1116 else if (err == -ESRCH)
1117 pol = NULL;
1118 else
1119 pol = ERR_PTR(err);
1120 } else
1121 pol = NULL;
1122 }
1123 out:
1124 read_unlock_bh(&xfrm_policy_lock);
1125 return pol;
1126 }
1127
__xfrm_policy_link(struct xfrm_policy * pol,int dir)1128 static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
1129 {
1130 struct net *net = xp_net(pol);
1131 struct hlist_head *chain = policy_hash_bysel(net, &pol->selector,
1132 pol->family, dir);
1133
1134 list_add(&pol->walk.all, &net->xfrm.policy_all);
1135 hlist_add_head(&pol->bydst, chain);
1136 hlist_add_head(&pol->byidx, net->xfrm.policy_byidx+idx_hash(net, pol->index));
1137 net->xfrm.policy_count[dir]++;
1138 xfrm_pol_hold(pol);
1139
1140 if (xfrm_bydst_should_resize(net, dir, NULL))
1141 schedule_work(&net->xfrm.policy_hash_work);
1142 }
1143
__xfrm_policy_unlink(struct xfrm_policy * pol,int dir)1144 static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
1145 int dir)
1146 {
1147 struct net *net = xp_net(pol);
1148
1149 if (hlist_unhashed(&pol->bydst))
1150 return NULL;
1151
1152 hlist_del(&pol->bydst);
1153 hlist_del(&pol->byidx);
1154 list_del(&pol->walk.all);
1155 net->xfrm.policy_count[dir]--;
1156
1157 return pol;
1158 }
1159
xfrm_policy_delete(struct xfrm_policy * pol,int dir)1160 int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
1161 {
1162 write_lock_bh(&xfrm_policy_lock);
1163 pol = __xfrm_policy_unlink(pol, dir);
1164 write_unlock_bh(&xfrm_policy_lock);
1165 if (pol) {
1166 xfrm_policy_kill(pol);
1167 return 0;
1168 }
1169 return -ENOENT;
1170 }
1171 EXPORT_SYMBOL(xfrm_policy_delete);
1172
xfrm_sk_policy_insert(struct sock * sk,int dir,struct xfrm_policy * pol)1173 int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
1174 {
1175 struct net *net = xp_net(pol);
1176 struct xfrm_policy *old_pol;
1177
1178 #ifdef CONFIG_XFRM_SUB_POLICY
1179 if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
1180 return -EINVAL;
1181 #endif
1182
1183 write_lock_bh(&xfrm_policy_lock);
1184 old_pol = sk->sk_policy[dir];
1185 sk->sk_policy[dir] = pol;
1186 if (pol) {
1187 pol->curlft.add_time = get_seconds();
1188 pol->index = xfrm_gen_index(net, XFRM_POLICY_MAX+dir);
1189 __xfrm_policy_link(pol, XFRM_POLICY_MAX+dir);
1190 }
1191 if (old_pol) {
1192 if (pol)
1193 xfrm_policy_requeue(old_pol, pol);
1194
1195 /* Unlinking succeeds always. This is the only function
1196 * allowed to delete or replace socket policy.
1197 */
1198 __xfrm_policy_unlink(old_pol, XFRM_POLICY_MAX+dir);
1199 }
1200 write_unlock_bh(&xfrm_policy_lock);
1201
1202 if (old_pol) {
1203 xfrm_policy_kill(old_pol);
1204 }
1205 return 0;
1206 }
1207
clone_policy(const struct xfrm_policy * old,int dir)1208 static struct xfrm_policy *clone_policy(const struct xfrm_policy *old, int dir)
1209 {
1210 struct xfrm_policy *newp = xfrm_policy_alloc(xp_net(old), GFP_ATOMIC);
1211
1212 if (newp) {
1213 newp->selector = old->selector;
1214 if (security_xfrm_policy_clone(old->security,
1215 &newp->security)) {
1216 kfree(newp);
1217 return NULL; /* ENOMEM */
1218 }
1219 newp->lft = old->lft;
1220 newp->curlft = old->curlft;
1221 newp->mark = old->mark;
1222 newp->action = old->action;
1223 newp->flags = old->flags;
1224 newp->xfrm_nr = old->xfrm_nr;
1225 newp->index = old->index;
1226 newp->type = old->type;
1227 memcpy(newp->xfrm_vec, old->xfrm_vec,
1228 newp->xfrm_nr*sizeof(struct xfrm_tmpl));
1229 write_lock_bh(&xfrm_policy_lock);
1230 __xfrm_policy_link(newp, XFRM_POLICY_MAX+dir);
1231 write_unlock_bh(&xfrm_policy_lock);
1232 xfrm_pol_put(newp);
1233 }
1234 return newp;
1235 }
1236
__xfrm_sk_clone_policy(struct sock * sk)1237 int __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
1250 static int
xfrm_get_saddr(struct net * net,xfrm_address_t * local,xfrm_address_t * remote,unsigned short family,u32 mark)1251 xfrm_get_saddr(struct net *net, xfrm_address_t *local, xfrm_address_t *remote,
1252 unsigned short family, u32 mark)
1253 {
1254 int err;
1255 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1256
1257 if (unlikely(afinfo == NULL))
1258 return -EINVAL;
1259 err = afinfo->get_saddr(net, local, remote, mark);
1260 xfrm_policy_put_afinfo(afinfo);
1261 return err;
1262 }
1263
1264 /* Resolve list of templates for the flow, given policy. */
1265
1266 static int
xfrm_tmpl_resolve_one(struct xfrm_policy * policy,const struct flowi * fl,struct xfrm_state ** xfrm,unsigned short family)1267 xfrm_tmpl_resolve_one(struct xfrm_policy *policy, const struct flowi *fl,
1268 struct xfrm_state **xfrm, unsigned short family)
1269 {
1270 struct net *net = xp_net(policy);
1271 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);
1275 xfrm_address_t tmp;
1276
1277 for (nx=0, i = 0; i < policy->xfrm_nr; i++) {
1278 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
1283 if (tmpl->mode == XFRM_MODE_TUNNEL ||
1284 tmpl->mode == XFRM_MODE_BEET) {
1285 remote = &tmpl->id.daddr;
1286 local = &tmpl->saddr;
1287 if (xfrm_addr_any(local, tmpl->encap_family)) {
1288 error = xfrm_get_saddr(net, &tmp, remote, tmpl->encap_family, 0);
1289 if (error)
1290 goto fail;
1291 local = &tmp;
1292 }
1293 }
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);
1307 }
1308 else if (error == -ESRCH)
1309 error = -EAGAIN;
1310
1311 if (!tmpl->optional)
1312 goto fail;
1313 }
1314 return nx;
1315
1316 fail:
1317 for (nx--; nx>=0; nx--)
1318 xfrm_state_put(xfrm[nx]);
1319 return error;
1320 }
1321
1322 static int
xfrm_tmpl_resolve(struct xfrm_policy ** pols,int npols,const struct flowi * fl,struct xfrm_state ** xfrm,unsigned short family)1323 xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, const struct flowi *fl,
1324 struct xfrm_state **xfrm, unsigned short family)
1325 {
1326 struct xfrm_state *tp[XFRM_MAX_DEPTH];
1327 struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
1328 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 }
1338
1339 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
1340 if (ret < 0) {
1341 error = ret;
1342 goto fail;
1343 } else
1344 cnx += ret;
1345 }
1346
1347 /* found states are sorted for outbound processing */
1348 if (npols > 1)
1349 xfrm_state_sort(xfrm, tpp, cnx, family);
1350
1351 return cnx;
1352
1353 fail:
1354 for (cnx--; cnx>=0; cnx--)
1355 xfrm_state_put(tpp[cnx]);
1356 return error;
1357
1358 }
1359
1360 /* Check that the bundle accepts the flow and its components are
1361 * still valid.
1362 */
1363
xfrm_get_tos(const struct flowi * fl,int family)1364 static inline int xfrm_get_tos(const struct flowi *fl, int family)
1365 {
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
xfrm_bundle_flo_get(struct flow_cache_object * flo)1379 static 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;
1390 } else if (dst->flags & DST_XFRM_QUEUE) {
1391 return NULL;
1392 } else {
1393 /* Real bundle */
1394 if (stale_bundle(dst))
1395 return NULL;
1396 }
1397
1398 dst_hold(dst);
1399 return flo;
1400 }
1401
xfrm_bundle_flo_check(struct flow_cache_object * flo)1402 static 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
xfrm_bundle_flo_delete(struct flow_cache_object * flo)1415 static 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
1423 static 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
xfrm_alloc_dst(struct net * net,int family)1429 static inline struct xfrm_dst *xfrm_alloc_dst(struct net *net, int family)
1430 {
1431 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
1432 struct dst_ops *dst_ops;
1433 struct xfrm_dst *xdst;
1434
1435 if (!afinfo)
1436 return ERR_PTR(-EINVAL);
1437
1438 switch (family) {
1439 case AF_INET:
1440 dst_ops = &net->xfrm.xfrm4_dst_ops;
1441 break;
1442 #if IS_ENABLED(CONFIG_IPV6)
1443 case AF_INET6:
1444 dst_ops = &net->xfrm.xfrm6_dst_ops;
1445 break;
1446 #endif
1447 default:
1448 BUG();
1449 }
1450 xdst = dst_alloc(dst_ops, NULL, 0, DST_OBSOLETE_NONE, 0);
1451
1452 if (likely(xdst)) {
1453 struct dst_entry *dst = &xdst->u.dst;
1454
1455 memset(dst + 1, 0, sizeof(*xdst) - sizeof(*dst));
1456 xdst->flo.ops = &xfrm_bundle_fc_ops;
1457 if (afinfo->init_dst)
1458 afinfo->init_dst(net, xdst);
1459 } else
1460 xdst = ERR_PTR(-ENOBUFS);
1461
1462 xfrm_policy_put_afinfo(afinfo);
1463
1464 return xdst;
1465 }
1466
xfrm_init_path(struct xfrm_dst * path,struct dst_entry * dst,int nfheader_len)1467 static 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
xfrm_fill_dst(struct xfrm_dst * xdst,struct net_device * dev,const struct flowi * fl)1484 static inline int xfrm_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
1485 const struct flowi *fl)
1486 {
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
1494 err = afinfo->fill_dst(xdst, dev, fl);
1495
1496 xfrm_policy_put_afinfo(afinfo);
1497
1498 return err;
1499 }
1500
1501
1502 /* Allocate chain of dst_entry's, attach known xfrm's, calculate
1503 * all the metrics... Shortly, bundle a bundle.
1504 */
1505
xfrm_bundle_create(struct xfrm_policy * policy,struct xfrm_state ** xfrm,int nx,const struct flowi * fl,struct dst_entry * dst)1506 static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy,
1507 struct xfrm_state **xfrm, int nx,
1508 const struct flowi *fl,
1509 struct dst_entry *dst)
1510 {
1511 struct net *net = xp_net(policy);
1512 unsigned long now = jiffies;
1513 struct net_device *dev;
1514 struct xfrm_mode *inner_mode;
1515 struct dst_entry *dst_prev = NULL;
1516 struct dst_entry *dst0 = NULL;
1517 int i = 0;
1518 int err;
1519 int header_len = 0;
1520 int nfheader_len = 0;
1521 int trailer_len = 0;
1522 int tos;
1523 int family = policy->selector.family;
1524 xfrm_address_t saddr, daddr;
1525
1526 xfrm_flowi_addr_get(fl, &saddr, &daddr, family);
1527
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++) {
1536 struct xfrm_dst *xdst = xfrm_alloc_dst(net, family);
1537 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
1545 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
1556 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;
1564 dst_copy_metrics(dst1, dst);
1565
1566 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
1567 family = xfrm[i]->props.family;
1568 dst = xfrm_dst_lookup(xfrm[i], tos, &saddr, &daddr,
1569 family,
1570 xfrm[i]->props.output_mark);
1571 err = PTR_ERR(dst);
1572 if (IS_ERR(dst))
1573 goto put_states;
1574 } else
1575 dst_hold(dst);
1576
1577 dst1->xfrm = xfrm[i];
1578 xdst->xfrm_genid = xfrm[i]->genid;
1579
1580 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
1581 dst1->flags |= DST_HOST;
1582 dst1->lastuse = now;
1583
1584 dst1->input = dst_discard;
1585 dst1->output = inner_mode->afinfo->output;
1586
1587 dst1->next = dst_prev;
1588 dst_prev = dst1;
1589
1590 header_len += xfrm[i]->props.header_len;
1591 if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT)
1592 nfheader_len += xfrm[i]->props.header_len;
1593 trailer_len += xfrm[i]->props.trailer_len;
1594 }
1595
1596 dst_prev->child = dst;
1597 dst0->path = dst;
1598
1599 err = -ENODEV;
1600 dev = dst->dev;
1601 if (!dev)
1602 goto free_dst;
1603
1604 xfrm_init_path((struct xfrm_dst *)dst0, dst, nfheader_len);
1605 xfrm_init_pmtu(dst_prev);
1606
1607 for (dst_prev = dst0; dst_prev != dst; dst_prev = dst_prev->child) {
1608 struct xfrm_dst *xdst = (struct xfrm_dst *)dst_prev;
1609
1610 err = xfrm_fill_dst(xdst, dev, fl);
1611 if (err)
1612 goto free_dst;
1613
1614 dst_prev->header_len = header_len;
1615 dst_prev->trailer_len = trailer_len;
1616 header_len -= xdst->u.dst.xfrm->props.header_len;
1617 trailer_len -= xdst->u.dst.xfrm->props.trailer_len;
1618 }
1619
1620 out:
1621 return dst0;
1622
1623 put_states:
1624 for (; i < nx; i++)
1625 xfrm_state_put(xfrm[i]);
1626 free_dst:
1627 if (dst0)
1628 dst_free(dst0);
1629 dst0 = ERR_PTR(err);
1630 goto out;
1631 }
1632
1633 static int inline
xfrm_dst_alloc_copy(void ** target,const void * src,int size)1634 xfrm_dst_alloc_copy(void **target, const void *src, int size)
1635 {
1636 if (!*target) {
1637 *target = kmalloc(size, GFP_ATOMIC);
1638 if (!*target)
1639 return -ENOMEM;
1640 }
1641 memcpy(*target, src, size);
1642 return 0;
1643 }
1644
1645 static int inline
xfrm_dst_update_parent(struct dst_entry * dst,const struct xfrm_selector * sel)1646 xfrm_dst_update_parent(struct dst_entry *dst, const struct xfrm_selector *sel)
1647 {
1648 #ifdef CONFIG_XFRM_SUB_POLICY
1649 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1650 return xfrm_dst_alloc_copy((void **)&(xdst->partner),
1651 sel, sizeof(*sel));
1652 #else
1653 return 0;
1654 #endif
1655 }
1656
1657 static int inline
xfrm_dst_update_origin(struct dst_entry * dst,const struct flowi * fl)1658 xfrm_dst_update_origin(struct dst_entry *dst, const struct flowi *fl)
1659 {
1660 #ifdef CONFIG_XFRM_SUB_POLICY
1661 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
1662 return xfrm_dst_alloc_copy((void **)&(xdst->origin), fl, sizeof(*fl));
1663 #else
1664 return 0;
1665 #endif
1666 }
1667
xfrm_expand_policies(const struct flowi * fl,u16 family,struct xfrm_policy ** pols,int * num_pols,int * num_xfrms)1668 static int xfrm_expand_policies(const struct flowi *fl, u16 family,
1669 struct xfrm_policy **pols,
1670 int *num_pols, int *num_xfrms)
1671 {
1672 int i;
1673
1674 if (*num_pols == 0 || !pols[0]) {
1675 *num_pols = 0;
1676 *num_xfrms = 0;
1677 return 0;
1678 }
1679 if (IS_ERR(pols[0]))
1680 return PTR_ERR(pols[0]);
1681
1682 *num_xfrms = pols[0]->xfrm_nr;
1683
1684 #ifdef CONFIG_XFRM_SUB_POLICY
1685 if (pols[0] && pols[0]->action == XFRM_POLICY_ALLOW &&
1686 pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
1687 pols[1] = xfrm_policy_lookup_bytype(xp_net(pols[0]),
1688 XFRM_POLICY_TYPE_MAIN,
1689 fl, family,
1690 XFRM_POLICY_OUT);
1691 if (pols[1]) {
1692 if (IS_ERR(pols[1])) {
1693 xfrm_pols_put(pols, *num_pols);
1694 return PTR_ERR(pols[1]);
1695 }
1696 (*num_pols) ++;
1697 (*num_xfrms) += pols[1]->xfrm_nr;
1698 }
1699 }
1700 #endif
1701 for (i = 0; i < *num_pols; i++) {
1702 if (pols[i]->action != XFRM_POLICY_ALLOW) {
1703 *num_xfrms = -1;
1704 break;
1705 }
1706 }
1707
1708 return 0;
1709
1710 }
1711
1712 static struct xfrm_dst *
xfrm_resolve_and_create_bundle(struct xfrm_policy ** pols,int num_pols,const struct flowi * fl,u16 family,struct dst_entry * dst_orig)1713 xfrm_resolve_and_create_bundle(struct xfrm_policy **pols, int num_pols,
1714 const struct flowi *fl, u16 family,
1715 struct dst_entry *dst_orig)
1716 {
1717 struct net *net = xp_net(pols[0]);
1718 struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
1719 struct dst_entry *dst;
1720 struct xfrm_dst *xdst;
1721 int err;
1722
1723 /* Try to instantiate a bundle */
1724 err = xfrm_tmpl_resolve(pols, num_pols, fl, xfrm, family);
1725 if (err <= 0) {
1726 if (err != 0 && err != -EAGAIN)
1727 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
1728 return ERR_PTR(err);
1729 }
1730
1731 dst = xfrm_bundle_create(pols[0], xfrm, err, fl, dst_orig);
1732 if (IS_ERR(dst)) {
1733 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLEGENERROR);
1734 return ERR_CAST(dst);
1735 }
1736
1737 xdst = (struct xfrm_dst *)dst;
1738 xdst->num_xfrms = err;
1739 if (num_pols > 1)
1740 err = xfrm_dst_update_parent(dst, &pols[1]->selector);
1741 else
1742 err = xfrm_dst_update_origin(dst, fl);
1743 if (unlikely(err)) {
1744 dst_free(dst);
1745 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLECHECKERROR);
1746 return ERR_PTR(err);
1747 }
1748
1749 xdst->num_pols = num_pols;
1750 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy*) * num_pols);
1751 xdst->policy_genid = atomic_read(&pols[0]->genid);
1752
1753 return xdst;
1754 }
1755
xfrm_policy_queue_process(unsigned long arg)1756 static void xfrm_policy_queue_process(unsigned long arg)
1757 {
1758 int err = 0;
1759 struct sk_buff *skb;
1760 struct sock *sk;
1761 struct dst_entry *dst;
1762 struct net_device *dev;
1763 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);
1770 dst = skb_dst(skb);
1771 sk = skb->sk;
1772 xfrm_decode_session(skb, &fl, dst->ops->family);
1773 spin_unlock(&pq->hold_queue.lock);
1774
1775 dst_hold(dst->path);
1776 dst = xfrm_lookup(xp_net(pol), dst->path, &fl,
1777 sk, 0);
1778 if (IS_ERR(dst))
1779 goto purge_queue;
1780
1781 if (dst->flags & DST_XFRM_QUEUE) {
1782 dst_release(dst);
1783
1784 if (pq->timeout >= XFRM_QUEUE_TMO_MAX)
1785 goto purge_queue;
1786
1787 pq->timeout = pq->timeout << 1;
1788 mod_timer(&pq->hold_timer, jiffies + pq->timeout);
1789 return;
1790 }
1791
1792 dst_release(dst);
1793
1794 __skb_queue_head_init(&list);
1795
1796 spin_lock(&pq->hold_queue.lock);
1797 pq->timeout = 0;
1798 skb_queue_splice_init(&pq->hold_queue, &list);
1799 spin_unlock(&pq->hold_queue.lock);
1800
1801 while (!skb_queue_empty(&list)) {
1802 skb = __skb_dequeue(&list);
1803
1804 xfrm_decode_session(skb, &fl, skb_dst(skb)->ops->family);
1805 dst_hold(skb_dst(skb)->path);
1806 dst = xfrm_lookup(xp_net(pol), skb_dst(skb)->path,
1807 &fl, skb->sk, 0);
1808 if (IS_ERR(dst)) {
1809 dev_put(skb->dev);
1810 kfree_skb(skb);
1811 continue;
1812 }
1813
1814 nf_reset(skb);
1815 skb_dst_drop(skb);
1816 skb_dst_set(skb, dst);
1817
1818 dev = skb->dev;
1819 err = dst_output(skb);
1820 dev_put(dev);
1821 }
1822
1823 return;
1824
1825 purge_queue:
1826 pq->timeout = 0;
1827 xfrm_queue_purge(&pq->hold_queue);
1828 }
1829
xdst_queue_output(struct sk_buff * skb)1830 static int xdst_queue_output(struct sk_buff *skb)
1831 {
1832 unsigned long sched_next;
1833 struct dst_entry *dst = skb_dst(skb);
1834 struct xfrm_dst *xdst = (struct xfrm_dst *) dst;
1835 struct xfrm_policy_queue *pq = &xdst->pols[0]->polq;
1836
1837 if (pq->hold_queue.qlen > XFRM_MAX_QUEUE_LEN) {
1838 kfree_skb(skb);
1839 return -EAGAIN;
1840 }
1841
1842 skb_dst_force(skb);
1843 dev_hold(skb->dev);
1844
1845 spin_lock_bh(&pq->hold_queue.lock);
1846
1847 if (!pq->timeout)
1848 pq->timeout = XFRM_QUEUE_TMO_MIN;
1849
1850 sched_next = jiffies + pq->timeout;
1851
1852 if (del_timer(&pq->hold_timer)) {
1853 if (time_before(pq->hold_timer.expires, sched_next))
1854 sched_next = pq->hold_timer.expires;
1855 }
1856
1857 __skb_queue_tail(&pq->hold_queue, skb);
1858 mod_timer(&pq->hold_timer, sched_next);
1859
1860 spin_unlock_bh(&pq->hold_queue.lock);
1861
1862 return 0;
1863 }
1864
xfrm_create_dummy_bundle(struct net * net,struct dst_entry * dst,const struct flowi * fl,int num_xfrms,u16 family)1865 static struct xfrm_dst *xfrm_create_dummy_bundle(struct net *net,
1866 struct dst_entry *dst,
1867 const struct flowi *fl,
1868 int num_xfrms,
1869 u16 family)
1870 {
1871 int err;
1872 struct net_device *dev;
1873 struct dst_entry *dst1;
1874 struct xfrm_dst *xdst;
1875
1876 xdst = xfrm_alloc_dst(net, family);
1877 if (IS_ERR(xdst))
1878 return xdst;
1879
1880 if (net->xfrm.sysctl_larval_drop || num_xfrms <= 0 ||
1881 (fl->flowi_flags & FLOWI_FLAG_CAN_SLEEP))
1882 return xdst;
1883
1884 dst1 = &xdst->u.dst;
1885 dst_hold(dst);
1886 xdst->route = dst;
1887
1888 dst_copy_metrics(dst1, dst);
1889
1890 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
1891 dst1->flags |= DST_HOST | DST_XFRM_QUEUE;
1892 dst1->lastuse = jiffies;
1893
1894 dst1->input = dst_discard;
1895 dst1->output = xdst_queue_output;
1896
1897 dst_hold(dst);
1898 dst1->child = dst;
1899 dst1->path = dst;
1900
1901 xfrm_init_path((struct xfrm_dst *)dst1, dst, 0);
1902
1903 err = -ENODEV;
1904 dev = dst->dev;
1905 if (!dev)
1906 goto free_dst;
1907
1908 err = xfrm_fill_dst(xdst, dev, fl);
1909 if (err)
1910 goto free_dst;
1911
1912 out:
1913 return xdst;
1914
1915 free_dst:
1916 dst_release(dst1);
1917 xdst = ERR_PTR(err);
1918 goto out;
1919 }
1920
1921 static struct flow_cache_object *
xfrm_bundle_lookup(struct net * net,const struct flowi * fl,u16 family,u8 dir,struct flow_cache_object * oldflo,void * ctx)1922 xfrm_bundle_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir,
1923 struct flow_cache_object *oldflo, void *ctx)
1924 {
1925 struct dst_entry *dst_orig = (struct dst_entry *)ctx;
1926 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
1927 struct xfrm_dst *xdst, *new_xdst;
1928 int num_pols = 0, num_xfrms = 0, i, err, pol_dead;
1929
1930 /* Check if the policies from old bundle are usable */
1931 xdst = NULL;
1932 if (oldflo) {
1933 xdst = container_of(oldflo, struct xfrm_dst, flo);
1934 num_pols = xdst->num_pols;
1935 num_xfrms = xdst->num_xfrms;
1936 pol_dead = 0;
1937 for (i = 0; i < num_pols; i++) {
1938 pols[i] = xdst->pols[i];
1939 pol_dead |= pols[i]->walk.dead;
1940 }
1941 if (pol_dead) {
1942 dst_free(&xdst->u.dst);
1943 xdst = NULL;
1944 num_pols = 0;
1945 num_xfrms = 0;
1946 oldflo = NULL;
1947 }
1948 }
1949
1950 /* Resolve policies to use if we couldn't get them from
1951 * previous cache entry */
1952 if (xdst == NULL) {
1953 num_pols = 1;
1954 pols[0] = __xfrm_policy_lookup(net, fl, family,
1955 flow_to_policy_dir(dir));
1956 err = xfrm_expand_policies(fl, family, pols,
1957 &num_pols, &num_xfrms);
1958 if (err < 0)
1959 goto inc_error;
1960 if (num_pols == 0)
1961 return NULL;
1962 if (num_xfrms <= 0)
1963 goto make_dummy_bundle;
1964 }
1965
1966 new_xdst = xfrm_resolve_and_create_bundle(pols, num_pols, fl, family, dst_orig);
1967 if (IS_ERR(new_xdst)) {
1968 err = PTR_ERR(new_xdst);
1969 if (err != -EAGAIN)
1970 goto error;
1971 if (oldflo == NULL)
1972 goto make_dummy_bundle;
1973 dst_hold(&xdst->u.dst);
1974 return oldflo;
1975 } else if (new_xdst == NULL) {
1976 num_xfrms = 0;
1977 if (oldflo == NULL)
1978 goto make_dummy_bundle;
1979 xdst->num_xfrms = 0;
1980 dst_hold(&xdst->u.dst);
1981 return oldflo;
1982 }
1983
1984 /* Kill the previous bundle */
1985 if (xdst) {
1986 /* The policies were stolen for newly generated bundle */
1987 xdst->num_pols = 0;
1988 dst_free(&xdst->u.dst);
1989 }
1990
1991 /* Flow cache does not have reference, it dst_free()'s,
1992 * but we do need to return one reference for original caller */
1993 dst_hold(&new_xdst->u.dst);
1994 return &new_xdst->flo;
1995
1996 make_dummy_bundle:
1997 /* We found policies, but there's no bundles to instantiate:
1998 * either because the policy blocks, has no transformations or
1999 * we could not build template (no xfrm_states).*/
2000 xdst = xfrm_create_dummy_bundle(net, dst_orig, fl, num_xfrms, family);
2001 if (IS_ERR(xdst)) {
2002 xfrm_pols_put(pols, num_pols);
2003 return ERR_CAST(xdst);
2004 }
2005 xdst->num_pols = num_pols;
2006 xdst->num_xfrms = num_xfrms;
2007 memcpy(xdst->pols, pols, sizeof(struct xfrm_policy*) * num_pols);
2008
2009 dst_hold(&xdst->u.dst);
2010 return &xdst->flo;
2011
2012 inc_error:
2013 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
2014 error:
2015 if (xdst != NULL)
2016 dst_free(&xdst->u.dst);
2017 else
2018 xfrm_pols_put(pols, num_pols);
2019 return ERR_PTR(err);
2020 }
2021
make_blackhole(struct net * net,u16 family,struct dst_entry * dst_orig)2022 static struct dst_entry *make_blackhole(struct net *net, u16 family,
2023 struct dst_entry *dst_orig)
2024 {
2025 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
2026 struct dst_entry *ret;
2027
2028 if (!afinfo) {
2029 dst_release(dst_orig);
2030 return ERR_PTR(-EINVAL);
2031 } else {
2032 ret = afinfo->blackhole_route(net, dst_orig);
2033 }
2034 xfrm_policy_put_afinfo(afinfo);
2035
2036 return ret;
2037 }
2038
2039 /* Main function: finds/creates a bundle for given flow.
2040 *
2041 * At the moment we eat a raw IP route. Mostly to speed up lookups
2042 * on interfaces with disabled IPsec.
2043 */
xfrm_lookup(struct net * net,struct dst_entry * dst_orig,const struct flowi * fl,struct sock * sk,int flags)2044 struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
2045 const struct flowi *fl,
2046 struct sock *sk, int flags)
2047 {
2048 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2049 struct flow_cache_object *flo;
2050 struct xfrm_dst *xdst;
2051 struct dst_entry *dst, *route;
2052 u16 family = dst_orig->ops->family;
2053 u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
2054 int i, err, num_pols, num_xfrms = 0, drop_pols = 0;
2055
2056 restart:
2057 dst = NULL;
2058 xdst = NULL;
2059 route = NULL;
2060
2061 if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
2062 num_pols = 1;
2063 pols[0] = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
2064 err = xfrm_expand_policies(fl, family, pols,
2065 &num_pols, &num_xfrms);
2066 if (err < 0)
2067 goto dropdst;
2068
2069 if (num_pols) {
2070 if (num_xfrms <= 0) {
2071 drop_pols = num_pols;
2072 goto no_transform;
2073 }
2074
2075 xdst = xfrm_resolve_and_create_bundle(
2076 pols, num_pols, fl,
2077 family, dst_orig);
2078 if (IS_ERR(xdst)) {
2079 xfrm_pols_put(pols, num_pols);
2080 err = PTR_ERR(xdst);
2081 goto dropdst;
2082 } else if (xdst == NULL) {
2083 num_xfrms = 0;
2084 drop_pols = num_pols;
2085 goto no_transform;
2086 }
2087
2088 dst_hold(&xdst->u.dst);
2089
2090 spin_lock_bh(&xfrm_policy_sk_bundle_lock);
2091 xdst->u.dst.next = xfrm_policy_sk_bundles;
2092 xfrm_policy_sk_bundles = &xdst->u.dst;
2093 spin_unlock_bh(&xfrm_policy_sk_bundle_lock);
2094
2095 route = xdst->route;
2096 }
2097 }
2098
2099 if (xdst == NULL) {
2100 /* To accelerate a bit... */
2101 if ((dst_orig->flags & DST_NOXFRM) ||
2102 !net->xfrm.policy_count[XFRM_POLICY_OUT])
2103 goto nopol;
2104
2105 flo = flow_cache_lookup(net, fl, family, dir,
2106 xfrm_bundle_lookup, dst_orig);
2107 if (flo == NULL)
2108 goto nopol;
2109 if (IS_ERR(flo)) {
2110 err = PTR_ERR(flo);
2111 goto dropdst;
2112 }
2113 xdst = container_of(flo, struct xfrm_dst, flo);
2114
2115 num_pols = xdst->num_pols;
2116 num_xfrms = xdst->num_xfrms;
2117 memcpy(pols, xdst->pols, sizeof(struct xfrm_policy*) * num_pols);
2118 route = xdst->route;
2119 }
2120
2121 dst = &xdst->u.dst;
2122 if (route == NULL && num_xfrms > 0) {
2123 /* The only case when xfrm_bundle_lookup() returns a
2124 * bundle with null route, is when the template could
2125 * not be resolved. It means policies are there, but
2126 * bundle could not be created, since we don't yet
2127 * have the xfrm_state's. We need to wait for KM to
2128 * negotiate new SA's or bail out with error.*/
2129 if (net->xfrm.sysctl_larval_drop) {
2130 /* EREMOTE tells the caller to generate
2131 * a one-shot blackhole route. */
2132 dst_release(dst);
2133 xfrm_pols_put(pols, drop_pols);
2134 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
2135
2136 return make_blackhole(net, family, dst_orig);
2137 }
2138 if (fl->flowi_flags & FLOWI_FLAG_CAN_SLEEP) {
2139 DECLARE_WAITQUEUE(wait, current);
2140
2141 add_wait_queue(&net->xfrm.km_waitq, &wait);
2142 set_current_state(TASK_INTERRUPTIBLE);
2143 schedule();
2144 set_current_state(TASK_RUNNING);
2145 remove_wait_queue(&net->xfrm.km_waitq, &wait);
2146
2147 if (!signal_pending(current)) {
2148 dst_release(dst);
2149 goto restart;
2150 }
2151
2152 err = -ERESTART;
2153 } else
2154 err = -EAGAIN;
2155
2156 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
2157 goto error;
2158 }
2159
2160 no_transform:
2161 if (num_pols == 0)
2162 goto nopol;
2163
2164 if ((flags & XFRM_LOOKUP_ICMP) &&
2165 !(pols[0]->flags & XFRM_POLICY_ICMP)) {
2166 err = -ENOENT;
2167 goto error;
2168 }
2169
2170 for (i = 0; i < num_pols; i++)
2171 pols[i]->curlft.use_time = get_seconds();
2172
2173 if (num_xfrms < 0) {
2174 /* Prohibit the flow */
2175 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLBLOCK);
2176 err = -EPERM;
2177 goto error;
2178 } else if (num_xfrms > 0) {
2179 /* Flow transformed */
2180 dst_release(dst_orig);
2181 } else {
2182 /* Flow passes untransformed */
2183 dst_release(dst);
2184 dst = dst_orig;
2185 }
2186 ok:
2187 xfrm_pols_put(pols, drop_pols);
2188 if (dst && dst->xfrm &&
2189 dst->xfrm->props.mode == XFRM_MODE_TUNNEL)
2190 dst->flags |= DST_XFRM_TUNNEL;
2191 return dst;
2192
2193 nopol:
2194 if (!(flags & XFRM_LOOKUP_ICMP)) {
2195 dst = dst_orig;
2196 goto ok;
2197 }
2198 err = -ENOENT;
2199 error:
2200 dst_release(dst);
2201 dropdst:
2202 dst_release(dst_orig);
2203 xfrm_pols_put(pols, drop_pols);
2204 return ERR_PTR(err);
2205 }
2206 EXPORT_SYMBOL(xfrm_lookup);
2207
2208 static inline int
xfrm_secpath_reject(int idx,struct sk_buff * skb,const struct flowi * fl)2209 xfrm_secpath_reject(int idx, struct sk_buff *skb, const struct flowi *fl)
2210 {
2211 struct xfrm_state *x;
2212
2213 if (!skb->sp || idx < 0 || idx >= skb->sp->len)
2214 return 0;
2215 x = skb->sp->xvec[idx];
2216 if (!x->type->reject)
2217 return 0;
2218 return x->type->reject(x, skb, fl);
2219 }
2220
2221 /* When skb is transformed back to its "native" form, we have to
2222 * check policy restrictions. At the moment we make this in maximally
2223 * stupid way. Shame on me. :-) Of course, connected sockets must
2224 * have policy cached at them.
2225 */
2226
2227 static inline int
xfrm_state_ok(const struct xfrm_tmpl * tmpl,const struct xfrm_state * x,unsigned short family)2228 xfrm_state_ok(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x,
2229 unsigned short family)
2230 {
2231 if (xfrm_state_kern(x))
2232 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
2233 return x->id.proto == tmpl->id.proto &&
2234 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
2235 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
2236 x->props.mode == tmpl->mode &&
2237 (tmpl->allalgs || (tmpl->aalgos & (1<<x->props.aalgo)) ||
2238 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
2239 !(x->props.mode != XFRM_MODE_TRANSPORT &&
2240 xfrm_state_addr_cmp(tmpl, x, family));
2241 }
2242
2243 /*
2244 * 0 or more than 0 is returned when validation is succeeded (either bypass
2245 * because of optional transport mode, or next index of the mathced secpath
2246 * state with the template.
2247 * -1 is returned when no matching template is found.
2248 * Otherwise "-2 - errored_index" is returned.
2249 */
2250 static inline int
xfrm_policy_ok(const struct xfrm_tmpl * tmpl,const struct sec_path * sp,int start,unsigned short family)2251 xfrm_policy_ok(const struct xfrm_tmpl *tmpl, const struct sec_path *sp, int start,
2252 unsigned short family)
2253 {
2254 int idx = start;
2255
2256 if (tmpl->optional) {
2257 if (tmpl->mode == XFRM_MODE_TRANSPORT)
2258 return start;
2259 } else
2260 start = -1;
2261 for (; idx < sp->len; idx++) {
2262 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
2263 return ++idx;
2264 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
2265 if (start == -1)
2266 start = -2-idx;
2267 break;
2268 }
2269 }
2270 return start;
2271 }
2272
__xfrm_decode_session(struct sk_buff * skb,struct flowi * fl,unsigned int family,int reverse)2273 int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
2274 unsigned int family, int reverse)
2275 {
2276 struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
2277 int err;
2278
2279 if (unlikely(afinfo == NULL))
2280 return -EAFNOSUPPORT;
2281
2282 afinfo->decode_session(skb, fl, reverse);
2283 err = security_xfrm_decode_session(skb, &fl->flowi_secid);
2284 xfrm_policy_put_afinfo(afinfo);
2285 return err;
2286 }
2287 EXPORT_SYMBOL(__xfrm_decode_session);
2288
secpath_has_nontransport(const struct sec_path * sp,int k,int * idxp)2289 static inline int secpath_has_nontransport(const struct sec_path *sp, int k, int *idxp)
2290 {
2291 for (; k < sp->len; k++) {
2292 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
2293 *idxp = k;
2294 return 1;
2295 }
2296 }
2297
2298 return 0;
2299 }
2300
__xfrm_policy_check(struct sock * sk,int dir,struct sk_buff * skb,unsigned short family)2301 int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
2302 unsigned short family)
2303 {
2304 struct net *net = dev_net(skb->dev);
2305 struct xfrm_policy *pol;
2306 struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
2307 int npols = 0;
2308 int xfrm_nr;
2309 int pi;
2310 int reverse;
2311 struct flowi fl;
2312 u8 fl_dir;
2313 int xerr_idx = -1;
2314
2315 reverse = dir & ~XFRM_POLICY_MASK;
2316 dir &= XFRM_POLICY_MASK;
2317 fl_dir = policy_to_flow_dir(dir);
2318
2319 if (__xfrm_decode_session(skb, &fl, family, reverse) < 0) {
2320 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
2321 return 0;
2322 }
2323
2324 nf_nat_decode_session(skb, &fl, family);
2325
2326 /* First, check used SA against their selectors. */
2327 if (skb->sp) {
2328 int i;
2329
2330 for (i=skb->sp->len-1; i>=0; i--) {
2331 struct xfrm_state *x = skb->sp->xvec[i];
2332 if (!xfrm_selector_match(&x->sel, &fl, family)) {
2333 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
2334 return 0;
2335 }
2336 }
2337 }
2338
2339 pol = NULL;
2340 if (sk && sk->sk_policy[dir]) {
2341 pol = xfrm_sk_policy_lookup(sk, dir, &fl);
2342 if (IS_ERR(pol)) {
2343 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
2344 return 0;
2345 }
2346 }
2347
2348 if (!pol) {
2349 struct flow_cache_object *flo;
2350
2351 flo = flow_cache_lookup(net, &fl, family, fl_dir,
2352 xfrm_policy_lookup, NULL);
2353 if (IS_ERR_OR_NULL(flo))
2354 pol = ERR_CAST(flo);
2355 else
2356 pol = container_of(flo, struct xfrm_policy, flo);
2357 }
2358
2359 if (IS_ERR(pol)) {
2360 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
2361 return 0;
2362 }
2363
2364 if (!pol) {
2365 if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
2366 xfrm_secpath_reject(xerr_idx, skb, &fl);
2367 XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOPOLS);
2368 return 0;
2369 }
2370 return 1;
2371 }
2372
2373 pol->curlft.use_time = get_seconds();
2374
2375 pols[0] = pol;
2376 npols ++;
2377 #ifdef CONFIG_XFRM_SUB_POLICY
2378 if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
2379 pols[1] = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN,
2380 &fl, family,
2381 XFRM_POLICY_IN);
2382 if (pols[1]) {
2383 if (IS_ERR(pols[1])) {
2384 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
2385 return 0;
2386 }
2387 pols[1]->curlft.use_time = get_seconds();
2388 npols ++;
2389 }
2390 }
2391 #endif
2392
2393 if (pol->action == XFRM_POLICY_ALLOW) {
2394 struct sec_path *sp;
2395 static struct sec_path dummy;
2396 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
2397 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
2398 struct xfrm_tmpl **tpp = tp;
2399 int ti = 0;
2400 int i, k;
2401
2402 if ((sp = skb->sp) == NULL)
2403 sp = &dummy;
2404
2405 for (pi = 0; pi < npols; pi++) {
2406 if (pols[pi] != pol &&
2407 pols[pi]->action != XFRM_POLICY_ALLOW) {
2408 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
2409 goto reject;
2410 }
2411 if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH) {
2412 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
2413 goto reject_error;
2414 }
2415 for (i = 0; i < pols[pi]->xfrm_nr; i++)
2416 tpp[ti++] = &pols[pi]->xfrm_vec[i];
2417 }
2418 xfrm_nr = ti;
2419 if (npols > 1) {
2420 xfrm_tmpl_sort(stp, tpp, xfrm_nr, family);
2421 tpp = stp;
2422 }
2423
2424 /* For each tunnel xfrm, find the first matching tmpl.
2425 * For each tmpl before that, find corresponding xfrm.
2426 * Order is _important_. Later we will implement
2427 * some barriers, but at the moment barriers
2428 * are implied between each two transformations.
2429 */
2430 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
2431 k = xfrm_policy_ok(tpp[i], sp, k, family);
2432 if (k < 0) {
2433 if (k < -1)
2434 /* "-2 - errored_index" returned */
2435 xerr_idx = -(2+k);
2436 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
2437 goto reject;
2438 }
2439 }
2440
2441 if (secpath_has_nontransport(sp, k, &xerr_idx)) {
2442 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
2443 goto reject;
2444 }
2445
2446 xfrm_pols_put(pols, npols);
2447 return 1;
2448 }
2449 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
2450
2451 reject:
2452 xfrm_secpath_reject(xerr_idx, skb, &fl);
2453 reject_error:
2454 xfrm_pols_put(pols, npols);
2455 return 0;
2456 }
2457 EXPORT_SYMBOL(__xfrm_policy_check);
2458
__xfrm_route_forward(struct sk_buff * skb,unsigned short family)2459 int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
2460 {
2461 struct net *net = dev_net(skb->dev);
2462 struct flowi fl;
2463 struct dst_entry *dst;
2464 int res = 1;
2465
2466 if (xfrm_decode_session(skb, &fl, family) < 0) {
2467 XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
2468 return 0;
2469 }
2470
2471 skb_dst_force(skb);
2472
2473 dst = xfrm_lookup(net, skb_dst(skb), &fl, NULL, 0);
2474 if (IS_ERR(dst)) {
2475 res = 0;
2476 dst = NULL;
2477 }
2478 skb_dst_set(skb, dst);
2479 return res;
2480 }
2481 EXPORT_SYMBOL(__xfrm_route_forward);
2482
2483 /* Optimize later using cookies and generation ids. */
2484
xfrm_dst_check(struct dst_entry * dst,u32 cookie)2485 static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
2486 {
2487 /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
2488 * to DST_OBSOLETE_FORCE_CHK to force all XFRM destinations to
2489 * get validated by dst_ops->check on every use. We do this
2490 * because when a normal route referenced by an XFRM dst is
2491 * obsoleted we do not go looking around for all parent
2492 * referencing XFRM dsts so that we can invalidate them. It
2493 * is just too much work. Instead we make the checks here on
2494 * every use. For example:
2495 *
2496 * XFRM dst A --> IPv4 dst X
2497 *
2498 * X is the "xdst->route" of A (X is also the "dst->path" of A
2499 * in this example). If X is marked obsolete, "A" will not
2500 * notice. That's what we are validating here via the
2501 * stale_bundle() check.
2502 *
2503 * When a policy's bundle is pruned, we dst_free() the XFRM
2504 * dst which causes it's ->obsolete field to be set to
2505 * DST_OBSOLETE_DEAD. If an XFRM dst has been pruned like
2506 * this, we want to force a new route lookup.
2507 */
2508 if (dst->obsolete < 0 && !stale_bundle(dst))
2509 return dst;
2510
2511 return NULL;
2512 }
2513
stale_bundle(struct dst_entry * dst)2514 static int stale_bundle(struct dst_entry *dst)
2515 {
2516 return !xfrm_bundle_ok((struct xfrm_dst *)dst);
2517 }
2518
xfrm_dst_ifdown(struct dst_entry * dst,struct net_device * dev)2519 void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
2520 {
2521 while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
2522 dst->dev = dev_net(dev)->loopback_dev;
2523 dev_hold(dst->dev);
2524 dev_put(dev);
2525 }
2526 }
2527 EXPORT_SYMBOL(xfrm_dst_ifdown);
2528
xfrm_link_failure(struct sk_buff * skb)2529 static void xfrm_link_failure(struct sk_buff *skb)
2530 {
2531 /* Impossible. Such dst must be popped before reaches point of failure. */
2532 }
2533
xfrm_negative_advice(struct dst_entry * dst)2534 static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
2535 {
2536 if (dst) {
2537 if (dst->obsolete) {
2538 dst_release(dst);
2539 dst = NULL;
2540 }
2541 }
2542 return dst;
2543 }
2544
__xfrm_garbage_collect(struct net * net)2545 static void __xfrm_garbage_collect(struct net *net)
2546 {
2547 struct dst_entry *head, *next;
2548
2549 spin_lock_bh(&xfrm_policy_sk_bundle_lock);
2550 head = xfrm_policy_sk_bundles;
2551 xfrm_policy_sk_bundles = NULL;
2552 spin_unlock_bh(&xfrm_policy_sk_bundle_lock);
2553
2554 while (head) {
2555 next = head->next;
2556 dst_free(head);
2557 head = next;
2558 }
2559 }
2560
xfrm_garbage_collect(struct net * net)2561 void xfrm_garbage_collect(struct net *net)
2562 {
2563 flow_cache_flush();
2564 __xfrm_garbage_collect(net);
2565 }
2566 EXPORT_SYMBOL(xfrm_garbage_collect);
2567
xfrm_garbage_collect_deferred(struct net * net)2568 static void xfrm_garbage_collect_deferred(struct net *net)
2569 {
2570 flow_cache_flush_deferred();
2571 __xfrm_garbage_collect(net);
2572 }
2573
xfrm_init_pmtu(struct dst_entry * dst)2574 static void xfrm_init_pmtu(struct dst_entry *dst)
2575 {
2576 do {
2577 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2578 u32 pmtu, route_mtu_cached;
2579
2580 pmtu = dst_mtu(dst->child);
2581 xdst->child_mtu_cached = pmtu;
2582
2583 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
2584
2585 route_mtu_cached = dst_mtu(xdst->route);
2586 xdst->route_mtu_cached = route_mtu_cached;
2587
2588 if (pmtu > route_mtu_cached)
2589 pmtu = route_mtu_cached;
2590
2591 dst_metric_set(dst, RTAX_MTU, pmtu);
2592 } while ((dst = dst->next));
2593 }
2594
2595 /* Check that the bundle accepts the flow and its components are
2596 * still valid.
2597 */
2598
xfrm_bundle_ok(struct xfrm_dst * first)2599 static int xfrm_bundle_ok(struct xfrm_dst *first)
2600 {
2601 struct dst_entry *dst = &first->u.dst;
2602 struct xfrm_dst *last;
2603 u32 mtu;
2604
2605 if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
2606 (dst->dev && !netif_running(dst->dev)))
2607 return 0;
2608
2609 if (dst->flags & DST_XFRM_QUEUE)
2610 return 1;
2611
2612 last = NULL;
2613
2614 do {
2615 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
2616
2617 if (dst->xfrm->km.state != XFRM_STATE_VALID)
2618 return 0;
2619 if (xdst->xfrm_genid != dst->xfrm->genid)
2620 return 0;
2621 if (xdst->num_pols > 0 &&
2622 xdst->policy_genid != atomic_read(&xdst->pols[0]->genid))
2623 return 0;
2624
2625 mtu = dst_mtu(dst->child);
2626 if (xdst->child_mtu_cached != mtu) {
2627 last = xdst;
2628 xdst->child_mtu_cached = mtu;
2629 }
2630
2631 if (!dst_check(xdst->route, xdst->route_cookie))
2632 return 0;
2633 mtu = dst_mtu(xdst->route);
2634 if (xdst->route_mtu_cached != mtu) {
2635 last = xdst;
2636 xdst->route_mtu_cached = mtu;
2637 }
2638
2639 dst = dst->child;
2640 } while (dst->xfrm);
2641
2642 if (likely(!last))
2643 return 1;
2644
2645 mtu = last->child_mtu_cached;
2646 for (;;) {
2647 dst = &last->u.dst;
2648
2649 mtu = xfrm_state_mtu(dst->xfrm, mtu);
2650 if (mtu > last->route_mtu_cached)
2651 mtu = last->route_mtu_cached;
2652 dst_metric_set(dst, RTAX_MTU, mtu);
2653
2654 if (last == first)
2655 break;
2656
2657 last = (struct xfrm_dst *)last->u.dst.next;
2658 last->child_mtu_cached = mtu;
2659 }
2660
2661 return 1;
2662 }
2663
xfrm_default_advmss(const struct dst_entry * dst)2664 static unsigned int xfrm_default_advmss(const struct dst_entry *dst)
2665 {
2666 return dst_metric_advmss(dst->path);
2667 }
2668
xfrm_mtu(const struct dst_entry * dst)2669 static unsigned int xfrm_mtu(const struct dst_entry *dst)
2670 {
2671 unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
2672
2673 return mtu ? : dst_mtu(dst->path);
2674 }
2675
xfrm_neigh_lookup(const struct dst_entry * dst,struct sk_buff * skb,const void * daddr)2676 static struct neighbour *xfrm_neigh_lookup(const struct dst_entry *dst,
2677 struct sk_buff *skb,
2678 const void *daddr)
2679 {
2680 return dst->path->ops->neigh_lookup(dst, skb, daddr);
2681 }
2682
xfrm_policy_register_afinfo(struct xfrm_policy_afinfo * afinfo)2683 int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
2684 {
2685 struct net *net;
2686 int err = 0;
2687 if (unlikely(afinfo == NULL))
2688 return -EINVAL;
2689 if (unlikely(afinfo->family >= NPROTO))
2690 return -EAFNOSUPPORT;
2691 spin_lock(&xfrm_policy_afinfo_lock);
2692 if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
2693 err = -ENOBUFS;
2694 else {
2695 struct dst_ops *dst_ops = afinfo->dst_ops;
2696 if (likely(dst_ops->kmem_cachep == NULL))
2697 dst_ops->kmem_cachep = xfrm_dst_cache;
2698 if (likely(dst_ops->check == NULL))
2699 dst_ops->check = xfrm_dst_check;
2700 if (likely(dst_ops->default_advmss == NULL))
2701 dst_ops->default_advmss = xfrm_default_advmss;
2702 if (likely(dst_ops->mtu == NULL))
2703 dst_ops->mtu = xfrm_mtu;
2704 if (likely(dst_ops->negative_advice == NULL))
2705 dst_ops->negative_advice = xfrm_negative_advice;
2706 if (likely(dst_ops->link_failure == NULL))
2707 dst_ops->link_failure = xfrm_link_failure;
2708 if (likely(dst_ops->neigh_lookup == NULL))
2709 dst_ops->neigh_lookup = xfrm_neigh_lookup;
2710 if (likely(afinfo->garbage_collect == NULL))
2711 afinfo->garbage_collect = xfrm_garbage_collect_deferred;
2712 rcu_assign_pointer(xfrm_policy_afinfo[afinfo->family], afinfo);
2713 }
2714 spin_unlock(&xfrm_policy_afinfo_lock);
2715
2716 rtnl_lock();
2717 for_each_net(net) {
2718 struct dst_ops *xfrm_dst_ops;
2719
2720 switch (afinfo->family) {
2721 case AF_INET:
2722 xfrm_dst_ops = &net->xfrm.xfrm4_dst_ops;
2723 break;
2724 #if IS_ENABLED(CONFIG_IPV6)
2725 case AF_INET6:
2726 xfrm_dst_ops = &net->xfrm.xfrm6_dst_ops;
2727 break;
2728 #endif
2729 default:
2730 BUG();
2731 }
2732 *xfrm_dst_ops = *afinfo->dst_ops;
2733 }
2734 rtnl_unlock();
2735
2736 return err;
2737 }
2738 EXPORT_SYMBOL(xfrm_policy_register_afinfo);
2739
xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo * afinfo)2740 int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
2741 {
2742 int err = 0;
2743 if (unlikely(afinfo == NULL))
2744 return -EINVAL;
2745 if (unlikely(afinfo->family >= NPROTO))
2746 return -EAFNOSUPPORT;
2747 spin_lock(&xfrm_policy_afinfo_lock);
2748 if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
2749 if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
2750 err = -EINVAL;
2751 else
2752 RCU_INIT_POINTER(xfrm_policy_afinfo[afinfo->family],
2753 NULL);
2754 }
2755 spin_unlock(&xfrm_policy_afinfo_lock);
2756 if (!err) {
2757 struct dst_ops *dst_ops = afinfo->dst_ops;
2758
2759 synchronize_rcu();
2760
2761 dst_ops->kmem_cachep = NULL;
2762 dst_ops->check = NULL;
2763 dst_ops->negative_advice = NULL;
2764 dst_ops->link_failure = NULL;
2765 afinfo->garbage_collect = NULL;
2766 }
2767 return err;
2768 }
2769 EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
2770
xfrm_dst_ops_init(struct net * net)2771 static void __net_init xfrm_dst_ops_init(struct net *net)
2772 {
2773 struct xfrm_policy_afinfo *afinfo;
2774
2775 rcu_read_lock();
2776 afinfo = rcu_dereference(xfrm_policy_afinfo[AF_INET]);
2777 if (afinfo)
2778 net->xfrm.xfrm4_dst_ops = *afinfo->dst_ops;
2779 #if IS_ENABLED(CONFIG_IPV6)
2780 afinfo = rcu_dereference(xfrm_policy_afinfo[AF_INET6]);
2781 if (afinfo)
2782 net->xfrm.xfrm6_dst_ops = *afinfo->dst_ops;
2783 #endif
2784 rcu_read_unlock();
2785 }
2786
xfrm_dev_event(struct notifier_block * this,unsigned long event,void * ptr)2787 static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
2788 {
2789 struct net_device *dev = ptr;
2790
2791 switch (event) {
2792 case NETDEV_DOWN:
2793 xfrm_garbage_collect(dev_net(dev));
2794 }
2795 return NOTIFY_DONE;
2796 }
2797
2798 static struct notifier_block xfrm_dev_notifier = {
2799 .notifier_call = xfrm_dev_event,
2800 };
2801
2802 #ifdef CONFIG_XFRM_STATISTICS
xfrm_statistics_init(struct net * net)2803 static int __net_init xfrm_statistics_init(struct net *net)
2804 {
2805 int rv;
2806
2807 if (snmp_mib_init((void __percpu **)net->mib.xfrm_statistics,
2808 sizeof(struct linux_xfrm_mib),
2809 __alignof__(struct linux_xfrm_mib)) < 0)
2810 return -ENOMEM;
2811 rv = xfrm_proc_init(net);
2812 if (rv < 0)
2813 snmp_mib_free((void __percpu **)net->mib.xfrm_statistics);
2814 return rv;
2815 }
2816
xfrm_statistics_fini(struct net * net)2817 static void xfrm_statistics_fini(struct net *net)
2818 {
2819 xfrm_proc_fini(net);
2820 snmp_mib_free((void __percpu **)net->mib.xfrm_statistics);
2821 }
2822 #else
xfrm_statistics_init(struct net * net)2823 static int __net_init xfrm_statistics_init(struct net *net)
2824 {
2825 return 0;
2826 }
2827
xfrm_statistics_fini(struct net * net)2828 static void xfrm_statistics_fini(struct net *net)
2829 {
2830 }
2831 #endif
2832
xfrm_policy_init(struct net * net)2833 static int __net_init xfrm_policy_init(struct net *net)
2834 {
2835 unsigned int hmask, sz;
2836 int dir;
2837
2838 if (net_eq(net, &init_net))
2839 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
2840 sizeof(struct xfrm_dst),
2841 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
2842 NULL);
2843
2844 hmask = 8 - 1;
2845 sz = (hmask+1) * sizeof(struct hlist_head);
2846
2847 net->xfrm.policy_byidx = xfrm_hash_alloc(sz);
2848 if (!net->xfrm.policy_byidx)
2849 goto out_byidx;
2850 net->xfrm.policy_idx_hmask = hmask;
2851
2852 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
2853 struct xfrm_policy_hash *htab;
2854
2855 net->xfrm.policy_count[dir] = 0;
2856 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
2857
2858 htab = &net->xfrm.policy_bydst[dir];
2859 htab->table = xfrm_hash_alloc(sz);
2860 if (!htab->table)
2861 goto out_bydst;
2862 htab->hmask = hmask;
2863 }
2864
2865 INIT_LIST_HEAD(&net->xfrm.policy_all);
2866 INIT_WORK(&net->xfrm.policy_hash_work, xfrm_hash_resize);
2867 if (net_eq(net, &init_net))
2868 register_netdevice_notifier(&xfrm_dev_notifier);
2869 return 0;
2870
2871 out_bydst:
2872 for (dir--; dir >= 0; dir--) {
2873 struct xfrm_policy_hash *htab;
2874
2875 htab = &net->xfrm.policy_bydst[dir];
2876 xfrm_hash_free(htab->table, sz);
2877 }
2878 xfrm_hash_free(net->xfrm.policy_byidx, sz);
2879 out_byidx:
2880 return -ENOMEM;
2881 }
2882
xfrm_policy_fini(struct net * net)2883 static void xfrm_policy_fini(struct net *net)
2884 {
2885 struct xfrm_audit audit_info;
2886 unsigned int sz;
2887 int dir;
2888
2889 flush_work(&net->xfrm.policy_hash_work);
2890 #ifdef CONFIG_XFRM_SUB_POLICY
2891 audit_info.loginuid = INVALID_UID;
2892 audit_info.sessionid = -1;
2893 audit_info.secid = 0;
2894 xfrm_policy_flush(net, XFRM_POLICY_TYPE_SUB, &audit_info);
2895 #endif
2896 audit_info.loginuid = INVALID_UID;
2897 audit_info.sessionid = -1;
2898 audit_info.secid = 0;
2899 xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, &audit_info);
2900
2901 WARN_ON(!list_empty(&net->xfrm.policy_all));
2902
2903 for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
2904 struct xfrm_policy_hash *htab;
2905
2906 WARN_ON(!hlist_empty(&net->xfrm.policy_inexact[dir]));
2907
2908 htab = &net->xfrm.policy_bydst[dir];
2909 sz = (htab->hmask + 1) * sizeof(struct hlist_head);
2910 WARN_ON(!hlist_empty(htab->table));
2911 xfrm_hash_free(htab->table, sz);
2912 }
2913
2914 sz = (net->xfrm.policy_idx_hmask + 1) * sizeof(struct hlist_head);
2915 WARN_ON(!hlist_empty(net->xfrm.policy_byidx));
2916 xfrm_hash_free(net->xfrm.policy_byidx, sz);
2917 }
2918
xfrm_net_init(struct net * net)2919 static int __net_init xfrm_net_init(struct net *net)
2920 {
2921 int rv;
2922
2923 rv = xfrm_statistics_init(net);
2924 if (rv < 0)
2925 goto out_statistics;
2926 rv = xfrm_state_init(net);
2927 if (rv < 0)
2928 goto out_state;
2929 rv = xfrm_policy_init(net);
2930 if (rv < 0)
2931 goto out_policy;
2932 xfrm_dst_ops_init(net);
2933 rv = xfrm_sysctl_init(net);
2934 if (rv < 0)
2935 goto out_sysctl;
2936 return 0;
2937
2938 out_sysctl:
2939 xfrm_policy_fini(net);
2940 out_policy:
2941 xfrm_state_fini(net);
2942 out_state:
2943 xfrm_statistics_fini(net);
2944 out_statistics:
2945 return rv;
2946 }
2947
xfrm_net_exit(struct net * net)2948 static void __net_exit xfrm_net_exit(struct net *net)
2949 {
2950 xfrm_sysctl_fini(net);
2951 xfrm_policy_fini(net);
2952 xfrm_state_fini(net);
2953 xfrm_statistics_fini(net);
2954 }
2955
2956 static struct pernet_operations __net_initdata xfrm_net_ops = {
2957 .init = xfrm_net_init,
2958 .exit = xfrm_net_exit,
2959 };
2960
xfrm_init(void)2961 void __init xfrm_init(void)
2962 {
2963 register_pernet_subsys(&xfrm_net_ops);
2964 xfrm_input_init();
2965 }
2966
2967 #ifdef CONFIG_AUDITSYSCALL
xfrm_audit_common_policyinfo(struct xfrm_policy * xp,struct audit_buffer * audit_buf)2968 static void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
2969 struct audit_buffer *audit_buf)
2970 {
2971 struct xfrm_sec_ctx *ctx = xp->security;
2972 struct xfrm_selector *sel = &xp->selector;
2973
2974 if (ctx)
2975 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
2976 ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
2977
2978 switch(sel->family) {
2979 case AF_INET:
2980 audit_log_format(audit_buf, " src=%pI4", &sel->saddr.a4);
2981 if (sel->prefixlen_s != 32)
2982 audit_log_format(audit_buf, " src_prefixlen=%d",
2983 sel->prefixlen_s);
2984 audit_log_format(audit_buf, " dst=%pI4", &sel->daddr.a4);
2985 if (sel->prefixlen_d != 32)
2986 audit_log_format(audit_buf, " dst_prefixlen=%d",
2987 sel->prefixlen_d);
2988 break;
2989 case AF_INET6:
2990 audit_log_format(audit_buf, " src=%pI6", sel->saddr.a6);
2991 if (sel->prefixlen_s != 128)
2992 audit_log_format(audit_buf, " src_prefixlen=%d",
2993 sel->prefixlen_s);
2994 audit_log_format(audit_buf, " dst=%pI6", sel->daddr.a6);
2995 if (sel->prefixlen_d != 128)
2996 audit_log_format(audit_buf, " dst_prefixlen=%d",
2997 sel->prefixlen_d);
2998 break;
2999 }
3000 }
3001
xfrm_audit_policy_add(struct xfrm_policy * xp,int result,kuid_t auid,u32 sessionid,u32 secid)3002 void xfrm_audit_policy_add(struct xfrm_policy *xp, int result,
3003 kuid_t auid, u32 sessionid, u32 secid)
3004 {
3005 struct audit_buffer *audit_buf;
3006
3007 audit_buf = xfrm_audit_start("SPD-add");
3008 if (audit_buf == NULL)
3009 return;
3010 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
3011 audit_log_format(audit_buf, " res=%u", result);
3012 xfrm_audit_common_policyinfo(xp, audit_buf);
3013 audit_log_end(audit_buf);
3014 }
3015 EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
3016
xfrm_audit_policy_delete(struct xfrm_policy * xp,int result,kuid_t auid,u32 sessionid,u32 secid)3017 void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
3018 kuid_t auid, u32 sessionid, u32 secid)
3019 {
3020 struct audit_buffer *audit_buf;
3021
3022 audit_buf = xfrm_audit_start("SPD-delete");
3023 if (audit_buf == NULL)
3024 return;
3025 xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
3026 audit_log_format(audit_buf, " res=%u", result);
3027 xfrm_audit_common_policyinfo(xp, audit_buf);
3028 audit_log_end(audit_buf);
3029 }
3030 EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
3031 #endif
3032
3033 #ifdef CONFIG_XFRM_MIGRATE
xfrm_migrate_selector_match(const struct xfrm_selector * sel_cmp,const struct xfrm_selector * sel_tgt)3034 static bool xfrm_migrate_selector_match(const struct xfrm_selector *sel_cmp,
3035 const struct xfrm_selector *sel_tgt)
3036 {
3037 if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
3038 if (sel_tgt->family == sel_cmp->family &&
3039 xfrm_addr_equal(&sel_tgt->daddr, &sel_cmp->daddr,
3040 sel_cmp->family) &&
3041 xfrm_addr_equal(&sel_tgt->saddr, &sel_cmp->saddr,
3042 sel_cmp->family) &&
3043 sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
3044 sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
3045 return true;
3046 }
3047 } else {
3048 if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
3049 return true;
3050 }
3051 }
3052 return false;
3053 }
3054
xfrm_migrate_policy_find(const struct xfrm_selector * sel,u8 dir,u8 type)3055 static struct xfrm_policy * xfrm_migrate_policy_find(const struct xfrm_selector *sel,
3056 u8 dir, u8 type)
3057 {
3058 struct xfrm_policy *pol, *ret = NULL;
3059 struct hlist_head *chain;
3060 u32 priority = ~0U;
3061
3062 read_lock_bh(&xfrm_policy_lock);
3063 chain = policy_hash_direct(&init_net, &sel->daddr, &sel->saddr, sel->family, dir);
3064 hlist_for_each_entry(pol, chain, bydst) {
3065 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
3066 pol->type == type) {
3067 ret = pol;
3068 priority = ret->priority;
3069 break;
3070 }
3071 }
3072 chain = &init_net.xfrm.policy_inexact[dir];
3073 hlist_for_each_entry(pol, chain, bydst) {
3074 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
3075 pol->type == type &&
3076 pol->priority < priority) {
3077 ret = pol;
3078 break;
3079 }
3080 }
3081
3082 if (ret)
3083 xfrm_pol_hold(ret);
3084
3085 read_unlock_bh(&xfrm_policy_lock);
3086
3087 return ret;
3088 }
3089
migrate_tmpl_match(const struct xfrm_migrate * m,const struct xfrm_tmpl * t)3090 static int migrate_tmpl_match(const struct xfrm_migrate *m, const struct xfrm_tmpl *t)
3091 {
3092 int match = 0;
3093
3094 if (t->mode == m->mode && t->id.proto == m->proto &&
3095 (m->reqid == 0 || t->reqid == m->reqid)) {
3096 switch (t->mode) {
3097 case XFRM_MODE_TUNNEL:
3098 case XFRM_MODE_BEET:
3099 if (xfrm_addr_equal(&t->id.daddr, &m->old_daddr,
3100 m->old_family) &&
3101 xfrm_addr_equal(&t->saddr, &m->old_saddr,
3102 m->old_family)) {
3103 match = 1;
3104 }
3105 break;
3106 case XFRM_MODE_TRANSPORT:
3107 /* in case of transport mode, template does not store
3108 any IP addresses, hence we just compare mode and
3109 protocol */
3110 match = 1;
3111 break;
3112 default:
3113 break;
3114 }
3115 }
3116 return match;
3117 }
3118
3119 /* update endpoint address(es) of template(s) */
xfrm_policy_migrate(struct xfrm_policy * pol,struct xfrm_migrate * m,int num_migrate)3120 static int xfrm_policy_migrate(struct xfrm_policy *pol,
3121 struct xfrm_migrate *m, int num_migrate)
3122 {
3123 struct xfrm_migrate *mp;
3124 int i, j, n = 0;
3125
3126 write_lock_bh(&pol->lock);
3127 if (unlikely(pol->walk.dead)) {
3128 /* target policy has been deleted */
3129 write_unlock_bh(&pol->lock);
3130 return -ENOENT;
3131 }
3132
3133 for (i = 0; i < pol->xfrm_nr; i++) {
3134 for (j = 0, mp = m; j < num_migrate; j++, mp++) {
3135 if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
3136 continue;
3137 n++;
3138 if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
3139 pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
3140 continue;
3141 /* update endpoints */
3142 memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
3143 sizeof(pol->xfrm_vec[i].id.daddr));
3144 memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
3145 sizeof(pol->xfrm_vec[i].saddr));
3146 pol->xfrm_vec[i].encap_family = mp->new_family;
3147 /* flush bundles */
3148 atomic_inc(&pol->genid);
3149 }
3150 }
3151
3152 write_unlock_bh(&pol->lock);
3153
3154 if (!n)
3155 return -ENODATA;
3156
3157 return 0;
3158 }
3159
xfrm_migrate_check(const struct xfrm_migrate * m,int num_migrate)3160 static int xfrm_migrate_check(const struct xfrm_migrate *m, int num_migrate)
3161 {
3162 int i, j;
3163
3164 if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
3165 return -EINVAL;
3166
3167 for (i = 0; i < num_migrate; i++) {
3168 if (xfrm_addr_equal(&m[i].old_daddr, &m[i].new_daddr,
3169 m[i].old_family) &&
3170 xfrm_addr_equal(&m[i].old_saddr, &m[i].new_saddr,
3171 m[i].old_family))
3172 return -EINVAL;
3173 if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
3174 xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
3175 return -EINVAL;
3176
3177 /* check if there is any duplicated entry */
3178 for (j = i + 1; j < num_migrate; j++) {
3179 if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
3180 sizeof(m[i].old_daddr)) &&
3181 !memcmp(&m[i].old_saddr, &m[j].old_saddr,
3182 sizeof(m[i].old_saddr)) &&
3183 m[i].proto == m[j].proto &&
3184 m[i].mode == m[j].mode &&
3185 m[i].reqid == m[j].reqid &&
3186 m[i].old_family == m[j].old_family)
3187 return -EINVAL;
3188 }
3189 }
3190
3191 return 0;
3192 }
3193
xfrm_migrate(const struct xfrm_selector * sel,u8 dir,u8 type,struct xfrm_migrate * m,int num_migrate,struct xfrm_kmaddress * k)3194 int xfrm_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
3195 struct xfrm_migrate *m, int num_migrate,
3196 struct xfrm_kmaddress *k)
3197 {
3198 int i, err, nx_cur = 0, nx_new = 0;
3199 struct xfrm_policy *pol = NULL;
3200 struct xfrm_state *x, *xc;
3201 struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
3202 struct xfrm_state *x_new[XFRM_MAX_DEPTH];
3203 struct xfrm_migrate *mp;
3204
3205 if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
3206 goto out;
3207
3208 /* Stage 1 - find policy */
3209 if ((pol = xfrm_migrate_policy_find(sel, dir, type)) == NULL) {
3210 err = -ENOENT;
3211 goto out;
3212 }
3213
3214 /* Stage 2 - find and update state(s) */
3215 for (i = 0, mp = m; i < num_migrate; i++, mp++) {
3216 if ((x = xfrm_migrate_state_find(mp))) {
3217 x_cur[nx_cur] = x;
3218 nx_cur++;
3219 if ((xc = xfrm_state_migrate(x, mp))) {
3220 x_new[nx_new] = xc;
3221 nx_new++;
3222 } else {
3223 err = -ENODATA;
3224 goto restore_state;
3225 }
3226 }
3227 }
3228
3229 /* Stage 3 - update policy */
3230 if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
3231 goto restore_state;
3232
3233 /* Stage 4 - delete old state(s) */
3234 if (nx_cur) {
3235 xfrm_states_put(x_cur, nx_cur);
3236 xfrm_states_delete(x_cur, nx_cur);
3237 }
3238
3239 /* Stage 5 - announce */
3240 km_migrate(sel, dir, type, m, num_migrate, k);
3241
3242 xfrm_pol_put(pol);
3243
3244 return 0;
3245 out:
3246 return err;
3247
3248 restore_state:
3249 if (pol)
3250 xfrm_pol_put(pol);
3251 if (nx_cur)
3252 xfrm_states_put(x_cur, nx_cur);
3253 if (nx_new)
3254 xfrm_states_delete(x_new, nx_new);
3255
3256 return err;
3257 }
3258 EXPORT_SYMBOL(xfrm_migrate);
3259 #endif
3260