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