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