Skip to content

Commit 9a56055

Browse files
TaeheeYoodavem330
authored andcommitted
bonding: Add struct bond_ipesc to manage SA
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>
1 parent b121693 commit 9a56055

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
@@ -401,6 +401,7 @@ static int bond_vlan_rx_kill_vid(struct net_device *bond_dev,
401401
static int bond_ipsec_add_sa(struct xfrm_state *xs)
402402
{
403403
struct net_device *bond_dev = xs->xso.dev;
404+
struct bond_ipsec *ipsec;
404405
struct bonding *bond;
405406
struct slave *slave;
406407
int err;
@@ -416,9 +417,6 @@ static int bond_ipsec_add_sa(struct xfrm_state *xs)
416417
return -ENODEV;
417418
}
418419

419-
xs->xso.real_dev = slave->dev;
420-
bond->xs = xs;
421-
422420
if (!slave->dev->xfrmdev_ops ||
423421
!slave->dev->xfrmdev_ops->xdo_dev_state_add ||
424422
netif_is_bond_master(slave->dev)) {
@@ -427,18 +425,71 @@ static int bond_ipsec_add_sa(struct xfrm_state *xs)
427425
return -EINVAL;
428426
}
429427

428+
ipsec = kmalloc(sizeof(*ipsec), GFP_ATOMIC);
429+
if (!ipsec) {
430+
rcu_read_unlock();
431+
return -ENOMEM;
432+
}
433+
xs->xso.real_dev = slave->dev;
434+
430435
err = slave->dev->xfrmdev_ops->xdo_dev_state_add(xs);
436+
if (!err) {
437+
ipsec->xs = xs;
438+
INIT_LIST_HEAD(&ipsec->list);
439+
spin_lock_bh(&bond->ipsec_lock);
440+
list_add(&ipsec->list, &bond->ipsec_list);
441+
spin_unlock_bh(&bond->ipsec_lock);
442+
} else {
443+
kfree(ipsec);
444+
}
431445
rcu_read_unlock();
432446
return err;
433447
}
434448

449+
static void bond_ipsec_add_sa_all(struct bonding *bond)
450+
{
451+
struct net_device *bond_dev = bond->dev;
452+
struct bond_ipsec *ipsec;
453+
struct slave *slave;
454+
455+
rcu_read_lock();
456+
slave = rcu_dereference(bond->curr_active_slave);
457+
if (!slave)
458+
goto out;
459+
460+
if (!slave->dev->xfrmdev_ops ||
461+
!slave->dev->xfrmdev_ops->xdo_dev_state_add ||
462+
netif_is_bond_master(slave->dev)) {
463+
spin_lock_bh(&bond->ipsec_lock);
464+
if (!list_empty(&bond->ipsec_list))
465+
slave_warn(bond_dev, slave->dev,
466+
"%s: no slave xdo_dev_state_add\n",
467+
__func__);
468+
spin_unlock_bh(&bond->ipsec_lock);
469+
goto out;
470+
}
471+
472+
spin_lock_bh(&bond->ipsec_lock);
473+
list_for_each_entry(ipsec, &bond->ipsec_list, list) {
474+
ipsec->xs->xso.real_dev = slave->dev;
475+
if (slave->dev->xfrmdev_ops->xdo_dev_state_add(ipsec->xs)) {
476+
slave_warn(bond_dev, slave->dev, "%s: failed to add SA\n", __func__);
477+
ipsec->xs->xso.real_dev = NULL;
478+
}
479+
}
480+
spin_unlock_bh(&bond->ipsec_lock);
481+
out:
482+
rcu_read_unlock();
483+
}
484+
435485
/**
436486
* bond_ipsec_del_sa - clear out this specific SA
437487
* @xs: pointer to transformer state struct
438488
**/
439489
static void bond_ipsec_del_sa(struct xfrm_state *xs)
440490
{
441491
struct net_device *bond_dev = xs->xso.dev;
492+
struct bond_ipsec *ipsec;
442493
struct bonding *bond;
443494
struct slave *slave;
444495

@@ -452,7 +503,10 @@ static void bond_ipsec_del_sa(struct xfrm_state *xs)
452503
if (!slave)
453504
goto out;
454505

455-
xs->xso.real_dev = slave->dev;
506+
if (!xs->xso.real_dev)
507+
goto out;
508+
509+
WARN_ON(xs->xso.real_dev != slave->dev);
456510

457511
if (!slave->dev->xfrmdev_ops ||
458512
!slave->dev->xfrmdev_ops->xdo_dev_state_delete ||
@@ -463,6 +517,48 @@ static void bond_ipsec_del_sa(struct xfrm_state *xs)
463517

464518
slave->dev->xfrmdev_ops->xdo_dev_state_delete(xs);
465519
out:
520+
spin_lock_bh(&bond->ipsec_lock);
521+
list_for_each_entry(ipsec, &bond->ipsec_list, list) {
522+
if (ipsec->xs == xs) {
523+
list_del(&ipsec->list);
524+
kfree(ipsec);
525+
break;
526+
}
527+
}
528+
spin_unlock_bh(&bond->ipsec_lock);
529+
rcu_read_unlock();
530+
}
531+
532+
static void bond_ipsec_del_sa_all(struct bonding *bond)
533+
{
534+
struct net_device *bond_dev = bond->dev;
535+
struct bond_ipsec *ipsec;
536+
struct slave *slave;
537+
538+
rcu_read_lock();
539+
slave = rcu_dereference(bond->curr_active_slave);
540+
if (!slave) {
541+
rcu_read_unlock();
542+
return;
543+
}
544+
545+
spin_lock_bh(&bond->ipsec_lock);
546+
list_for_each_entry(ipsec, &bond->ipsec_list, list) {
547+
if (!ipsec->xs->xso.real_dev)
548+
continue;
549+
550+
if (!slave->dev->xfrmdev_ops ||
551+
!slave->dev->xfrmdev_ops->xdo_dev_state_delete ||
552+
netif_is_bond_master(slave->dev)) {
553+
slave_warn(bond_dev, slave->dev,
554+
"%s: no slave xdo_dev_state_delete\n",
555+
__func__);
556+
} else {
557+
slave->dev->xfrmdev_ops->xdo_dev_state_delete(ipsec->xs);
558+
}
559+
ipsec->xs->xso.real_dev = NULL;
560+
}
561+
spin_unlock_bh(&bond->ipsec_lock);
466562
rcu_read_unlock();
467563
}
468564

@@ -474,22 +570,27 @@ static void bond_ipsec_del_sa(struct xfrm_state *xs)
474570
static bool bond_ipsec_offload_ok(struct sk_buff *skb, struct xfrm_state *xs)
475571
{
476572
struct net_device *bond_dev = xs->xso.dev;
477-
struct bonding *bond = netdev_priv(bond_dev);
478-
struct slave *curr_active = rcu_dereference(bond->curr_active_slave);
479-
struct net_device *slave_dev = curr_active->dev;
573+
struct net_device *real_dev;
574+
struct slave *curr_active;
575+
struct bonding *bond;
576+
577+
bond = netdev_priv(bond_dev);
578+
curr_active = rcu_dereference(bond->curr_active_slave);
579+
real_dev = curr_active->dev;
480580

481581
if (BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP)
482582
return true;
483583

484-
if (!slave_dev->xfrmdev_ops ||
485-
!slave_dev->xfrmdev_ops->xdo_dev_offload_ok ||
486-
netif_is_bond_master(slave_dev)) {
487-
slave_warn(bond_dev, slave_dev, "%s: no slave xdo_dev_offload_ok\n", __func__);
584+
if (!xs->xso.real_dev)
585+
return false;
586+
587+
if (!real_dev->xfrmdev_ops ||
588+
!real_dev->xfrmdev_ops->xdo_dev_offload_ok ||
589+
netif_is_bond_master(real_dev)) {
488590
return false;
489591
}
490592

491-
xs->xso.real_dev = slave_dev;
492-
return slave_dev->xfrmdev_ops->xdo_dev_offload_ok(skb, xs);
593+
return real_dev->xfrmdev_ops->xdo_dev_offload_ok(skb, xs);
493594
}
494595

495596
static const struct xfrmdev_ops bond_xfrmdev_ops = {
@@ -1006,8 +1107,7 @@ void bond_change_active_slave(struct bonding *bond, struct slave *new_active)
10061107
return;
10071108

10081109
#ifdef CONFIG_XFRM_OFFLOAD
1009-
if (old_active && bond->xs)
1010-
bond_ipsec_del_sa(bond->xs);
1110+
bond_ipsec_del_sa_all(bond);
10111111
#endif /* CONFIG_XFRM_OFFLOAD */
10121112

10131113
if (new_active) {
@@ -1082,10 +1182,7 @@ void bond_change_active_slave(struct bonding *bond, struct slave *new_active)
10821182
}
10831183

10841184
#ifdef CONFIG_XFRM_OFFLOAD
1085-
if (new_active && bond->xs) {
1086-
xfrm_dev_state_flush(dev_net(bond->dev), bond->dev, true);
1087-
bond_ipsec_add_sa(bond->xs);
1088-
}
1185+
bond_ipsec_add_sa_all(bond);
10891186
#endif /* CONFIG_XFRM_OFFLOAD */
10901187

10911188
/* resend IGMP joins since active slave has changed or
@@ -3343,6 +3440,7 @@ static int bond_master_netdev_event(unsigned long event,
33433440
return bond_event_changename(event_bond);
33443441
case NETDEV_UNREGISTER:
33453442
bond_remove_proc_entry(event_bond);
3443+
xfrm_dev_state_flush(dev_net(bond_dev), bond_dev, true);
33463444
break;
33473445
case NETDEV_REGISTER:
33483446
bond_create_proc_entry(event_bond);
@@ -4910,7 +5008,8 @@ void bond_setup(struct net_device *bond_dev)
49105008
#ifdef CONFIG_XFRM_OFFLOAD
49115009
/* set up xfrm device ops (only supported in active-backup right now) */
49125010
bond_dev->xfrmdev_ops = &bond_xfrmdev_ops;
4913-
bond->xs = NULL;
5011+
INIT_LIST_HEAD(&bond->ipsec_list);
5012+
spin_lock_init(&bond->ipsec_lock);
49145013
#endif /* CONFIG_XFRM_OFFLOAD */
49155014

49165015
/* 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
@@ -201,6 +201,11 @@ struct bond_up_slave {
201201
*/
202202
#define BOND_LINK_NOCHANGE -1
203203

204+
struct bond_ipsec {
205+
struct list_head list;
206+
struct xfrm_state *xs;
207+
};
208+
204209
/*
205210
* Here are the locking policies for the two bonding locks:
206211
* Get rcu_read_lock when reading or RTNL when writing slave list.
@@ -249,7 +254,9 @@ struct bonding {
249254
#endif /* CONFIG_DEBUG_FS */
250255
struct rtnl_link_stats64 bond_stats;
251256
#ifdef CONFIG_XFRM_OFFLOAD
252-
struct xfrm_state *xs;
257+
struct list_head ipsec_list;
258+
/* protecting ipsec_list */
259+
spinlock_t ipsec_lock;
253260
#endif /* CONFIG_XFRM_OFFLOAD */
254261
};
255262

0 commit comments

Comments
 (0)