Skip to content

Commit 56ccdf8

Browse files
TaeheeYoogregkh
authored andcommitted
bonding: Add struct bond_ipesc to manage SA
[ Upstream commit 9a56055 ] bonding has been supporting ipsec offload. When SA is added, bonding just passes SA to its own active real interface. But it doesn't manage SA. So, when events(add/del real interface, active real interface change, etc) occur, bonding can't handle that well because It doesn't manage SA. So some problems(panic, UAF, refcnt leak)occur. In order to make it stable, it should manage SA. That's the reason why struct bond_ipsec is added. When a new SA is added to bonding interface, it is stored in the bond_ipsec list. And the SA is passed to a current active real interface. If events occur, it uses bond_ipsec data to handle these events. bond->ipsec_list is protected by bond->ipsec_lock. If a current active real interface is changed, the following logic works. 1. delete all SAs from old active real interface 2. Add all SAs to the new active real interface. 3. If a new active real interface doesn't support ipsec offload or SA's option, it sets real_dev to NULL. Fixes: 18cb261 ("bonding: support hardware encryption offload to slaves") Signed-off-by: Taehee Yoo <ap420073@gmail.com> Signed-off-by: David S. Miller <davem@davemloft.net> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent b3bd1f5 commit 56ccdf8

2 files changed

Lines changed: 127 additions & 21 deletions

File tree

‎drivers/net/bonding/bond_main.c‎

Lines changed: 119 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@ static int bond_vlan_rx_kill_vid(struct net_device *bond_dev,
385385
static int bond_ipsec_add_sa(struct xfrm_state *xs)
386386
{
387387
struct net_device *bond_dev = xs->xso.dev;
388+
struct bond_ipsec *ipsec;
388389
struct bonding *bond;
389390
struct slave *slave;
390391
int err;
@@ -400,9 +401,6 @@ static int bond_ipsec_add_sa(struct xfrm_state *xs)
400401
return -ENODEV;
401402
}
402403

403-
xs->xso.real_dev = slave->dev;
404-
bond->xs = xs;
405-
406404
if (!slave->dev->xfrmdev_ops ||
407405
!slave->dev->xfrmdev_ops->xdo_dev_state_add ||
408406
netif_is_bond_master(slave->dev)) {
@@ -411,18 +409,71 @@ static int bond_ipsec_add_sa(struct xfrm_state *xs)
411409
return -EINVAL;
412410
}
413411

412+
ipsec = kmalloc(sizeof(*ipsec), GFP_ATOMIC);
413+
if (!ipsec) {
414+
rcu_read_unlock();
415+
return -ENOMEM;
416+
}
417+
xs->xso.real_dev = slave->dev;
418+
414419
err = slave->dev->xfrmdev_ops->xdo_dev_state_add(xs);
420+
if (!err) {
421+
ipsec->xs = xs;
422+
INIT_LIST_HEAD(&ipsec->list);
423+
spin_lock_bh(&bond->ipsec_lock);
424+
list_add(&ipsec->list, &bond->ipsec_list);
425+
spin_unlock_bh(&bond->ipsec_lock);
426+
} else {
427+
kfree(ipsec);
428+
}
415429
rcu_read_unlock();
416430
return err;
417431
}
418432

433+
static void bond_ipsec_add_sa_all(struct bonding *bond)
434+
{
435+
struct net_device *bond_dev = bond->dev;
436+
struct bond_ipsec *ipsec;
437+
struct slave *slave;
438+
439+
rcu_read_lock();
440+
slave = rcu_dereference(bond->curr_active_slave);
441+
if (!slave)
442+
goto out;
443+
444+
if (!slave->dev->xfrmdev_ops ||
445+
!slave->dev->xfrmdev_ops->xdo_dev_state_add ||
446+
netif_is_bond_master(slave->dev)) {
447+
spin_lock_bh(&bond->ipsec_lock);
448+
if (!list_empty(&bond->ipsec_list))
449+
slave_warn(bond_dev, slave->dev,
450+
"%s: no slave xdo_dev_state_add\n",
451+
__func__);
452+
spin_unlock_bh(&bond->ipsec_lock);
453+
goto out;
454+
}
455+
456+
spin_lock_bh(&bond->ipsec_lock);
457+
list_for_each_entry(ipsec, &bond->ipsec_list, list) {
458+
ipsec->xs->xso.real_dev = slave->dev;
459+
if (slave->dev->xfrmdev_ops->xdo_dev_state_add(ipsec->xs)) {
460+
slave_warn(bond_dev, slave->dev, "%s: failed to add SA\n", __func__);
461+
ipsec->xs->xso.real_dev = NULL;
462+
}
463+
}
464+
spin_unlock_bh(&bond->ipsec_lock);
465+
out:
466+
rcu_read_unlock();
467+
}
468+
419469
/**
420470
* bond_ipsec_del_sa - clear out this specific SA
421471
* @xs: pointer to transformer state struct
422472
**/
423473
static void bond_ipsec_del_sa(struct xfrm_state *xs)
424474
{
425475
struct net_device *bond_dev = xs->xso.dev;
476+
struct bond_ipsec *ipsec;
426477
struct bonding *bond;
427478
struct slave *slave;
428479

@@ -436,7 +487,10 @@ static void bond_ipsec_del_sa(struct xfrm_state *xs)
436487
if (!slave)
437488
goto out;
438489

439-
xs->xso.real_dev = slave->dev;
490+
if (!xs->xso.real_dev)
491+
goto out;
492+
493+
WARN_ON(xs->xso.real_dev != slave->dev);
440494

441495
if (!slave->dev->xfrmdev_ops ||
442496
!slave->dev->xfrmdev_ops->xdo_dev_state_delete ||
@@ -447,6 +501,48 @@ static void bond_ipsec_del_sa(struct xfrm_state *xs)
447501

448502
slave->dev->xfrmdev_ops->xdo_dev_state_delete(xs);
449503
out:
504+
spin_lock_bh(&bond->ipsec_lock);
505+
list_for_each_entry(ipsec, &bond->ipsec_list, list) {
506+
if (ipsec->xs == xs) {
507+
list_del(&ipsec->list);
508+
kfree(ipsec);
509+
break;
510+
}
511+
}
512+
spin_unlock_bh(&bond->ipsec_lock);
513+
rcu_read_unlock();
514+
}
515+
516+
static void bond_ipsec_del_sa_all(struct bonding *bond)
517+
{
518+
struct net_device *bond_dev = bond->dev;
519+
struct bond_ipsec *ipsec;
520+
struct slave *slave;
521+
522+
rcu_read_lock();
523+
slave = rcu_dereference(bond->curr_active_slave);
524+
if (!slave) {
525+
rcu_read_unlock();
526+
return;
527+
}
528+
529+
spin_lock_bh(&bond->ipsec_lock);
530+
list_for_each_entry(ipsec, &bond->ipsec_list, list) {
531+
if (!ipsec->xs->xso.real_dev)
532+
continue;
533+
534+
if (!slave->dev->xfrmdev_ops ||
535+
!slave->dev->xfrmdev_ops->xdo_dev_state_delete ||
536+
netif_is_bond_master(slave->dev)) {
537+
slave_warn(bond_dev, slave->dev,
538+
"%s: no slave xdo_dev_state_delete\n",
539+
__func__);
540+
} else {
541+
slave->dev->xfrmdev_ops->xdo_dev_state_delete(ipsec->xs);
542+
}
543+
ipsec->xs->xso.real_dev = NULL;
544+
}
545+
spin_unlock_bh(&bond->ipsec_lock);
450546
rcu_read_unlock();
451547
}
452548

@@ -458,22 +554,27 @@ static void bond_ipsec_del_sa(struct xfrm_state *xs)
458554
static bool bond_ipsec_offload_ok(struct sk_buff *skb, struct xfrm_state *xs)
459555
{
460556
struct net_device *bond_dev = xs->xso.dev;
461-
struct bonding *bond = netdev_priv(bond_dev);
462-
struct slave *curr_active = rcu_dereference(bond->curr_active_slave);
463-
struct net_device *slave_dev = curr_active->dev;
557+
struct net_device *real_dev;
558+
struct slave *curr_active;
559+
struct bonding *bond;
560+
561+
bond = netdev_priv(bond_dev);
562+
curr_active = rcu_dereference(bond->curr_active_slave);
563+
real_dev = curr_active->dev;
464564

465565
if (BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP)
466566
return true;
467567

468-
if (!slave_dev->xfrmdev_ops ||
469-
!slave_dev->xfrmdev_ops->xdo_dev_offload_ok ||
470-
netif_is_bond_master(slave_dev)) {
471-
slave_warn(bond_dev, slave_dev, "%s: no slave xdo_dev_offload_ok\n", __func__);
568+
if (!xs->xso.real_dev)
569+
return false;
570+
571+
if (!real_dev->xfrmdev_ops ||
572+
!real_dev->xfrmdev_ops->xdo_dev_offload_ok ||
573+
netif_is_bond_master(real_dev)) {
472574
return false;
473575
}
474576

475-
xs->xso.real_dev = slave_dev;
476-
return slave_dev->xfrmdev_ops->xdo_dev_offload_ok(skb, xs);
577+
return real_dev->xfrmdev_ops->xdo_dev_offload_ok(skb, xs);
477578
}
478579

479580
static const struct xfrmdev_ops bond_xfrmdev_ops = {
@@ -990,8 +1091,7 @@ void bond_change_active_slave(struct bonding *bond, struct slave *new_active)
9901091
return;
9911092

9921093
#ifdef CONFIG_XFRM_OFFLOAD
993-
if (old_active && bond->xs)
994-
bond_ipsec_del_sa(bond->xs);
1094+
bond_ipsec_del_sa_all(bond);
9951095
#endif /* CONFIG_XFRM_OFFLOAD */
9961096

9971097
if (new_active) {
@@ -1067,10 +1167,7 @@ void bond_change_active_slave(struct bonding *bond, struct slave *new_active)
10671167
}
10681168

10691169
#ifdef CONFIG_XFRM_OFFLOAD
1070-
if (new_active && bond->xs) {
1071-
xfrm_dev_state_flush(dev_net(bond->dev), bond->dev, true);
1072-
bond_ipsec_add_sa(bond->xs);
1073-
}
1170+
bond_ipsec_add_sa_all(bond);
10741171
#endif /* CONFIG_XFRM_OFFLOAD */
10751172

10761173
/* resend IGMP joins since active slave has changed or
@@ -3309,6 +3406,7 @@ static int bond_master_netdev_event(unsigned long event,
33093406
return bond_event_changename(event_bond);
33103407
case NETDEV_UNREGISTER:
33113408
bond_remove_proc_entry(event_bond);
3409+
xfrm_dev_state_flush(dev_net(bond_dev), bond_dev, true);
33123410
break;
33133411
case NETDEV_REGISTER:
33143412
bond_create_proc_entry(event_bond);
@@ -4742,7 +4840,8 @@ void bond_setup(struct net_device *bond_dev)
47424840
#ifdef CONFIG_XFRM_OFFLOAD
47434841
/* set up xfrm device ops (only supported in active-backup right now) */
47444842
bond_dev->xfrmdev_ops = &bond_xfrmdev_ops;
4745-
bond->xs = NULL;
4843+
INIT_LIST_HEAD(&bond->ipsec_list);
4844+
spin_lock_init(&bond->ipsec_lock);
47464845
#endif /* CONFIG_XFRM_OFFLOAD */
47474846

47484847
/* don't acquire bond device's netif_tx_lock when transmitting */

‎include/net/bonding.h‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,11 @@ struct bond_up_slave {
199199
*/
200200
#define BOND_LINK_NOCHANGE -1
201201

202+
struct bond_ipsec {
203+
struct list_head list;
204+
struct xfrm_state *xs;
205+
};
206+
202207
/*
203208
* Here are the locking policies for the two bonding locks:
204209
* Get rcu_read_lock when reading or RTNL when writing slave list.
@@ -247,7 +252,9 @@ struct bonding {
247252
#endif /* CONFIG_DEBUG_FS */
248253
struct rtnl_link_stats64 bond_stats;
249254
#ifdef CONFIG_XFRM_OFFLOAD
250-
struct xfrm_state *xs;
255+
struct list_head ipsec_list;
256+
/* protecting ipsec_list */
257+
spinlock_t ipsec_lock;
251258
#endif /* CONFIG_XFRM_OFFLOAD */
252259
};
253260

0 commit comments

Comments
 (0)