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