Skip to content

Commit 4cf476c

Browse files
tomparkindavem330
authored andcommitted
ppp: add PPPIOCBRIDGECHAN and PPPIOCUNBRIDGECHAN ioctls
This new ioctl pair allows two ppp channels to be bridged together: frames arriving in one channel are transmitted in the other channel and vice versa. The practical use for this is primarily to support the L2TP Access Concentrator use-case. The end-user session is presented as a ppp channel (typically PPPoE, although it could be e.g. PPPoA, or even PPP over a serial link) and is switched into a PPPoL2TP session for transmission to the LNS. At the LNS the PPP session is terminated in the ISP's network. When a PPP channel is bridged to another it takes a reference on the other's struct ppp_file. This reference is dropped when the channels are unbridged, which can occur either explicitly on userspace calling the PPPIOCUNBRIDGECHAN ioctl, or implicitly when either channel in the bridge is unregistered. In order to implement the channel bridge, struct channel is extended with a new field, 'bridge', which points to the other struct channel making up the bridge. This pointer is RCU protected to avoid adding another lock to the data path. To guard against concurrent writes to the pointer, the existing struct channel lock 'upl' coverage is extended rather than adding a new lock. The 'upl' lock is used to protect the existing unit pointer. Since the bridge effectively replaces the unit (they're mutually exclusive for a channel) it makes coding easier to use the same lock to cover them both. Signed-off-by: Tom Parkin <tparkin@katalix.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 51e1368 commit 4cf476c

File tree

2 files changed

+151
-3
lines changed

2 files changed

+151
-3
lines changed

drivers/net/ppp/ppp_generic.c

Lines changed: 149 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,8 @@ struct channel {
174174
struct ppp *ppp; /* ppp unit we're connected to */
175175
struct net *chan_net; /* the net channel belongs to */
176176
struct list_head clist; /* link in list of channels per unit */
177-
rwlock_t upl; /* protects `ppp' */
177+
rwlock_t upl; /* protects `ppp' and 'bridge' */
178+
struct channel __rcu *bridge; /* "bridged" ppp channel */
178179
#ifdef CONFIG_PPP_MULTILINK
179180
u8 avail; /* flag used in multilink stuff */
180181
u8 had_frag; /* >= 1 fragments have been sent */
@@ -606,6 +607,83 @@ static struct bpf_prog *compat_ppp_get_filter(struct sock_fprog32 __user *p)
606607
#endif
607608
#endif
608609

610+
/* Bridge one PPP channel to another.
611+
* When two channels are bridged, ppp_input on one channel is redirected to
612+
* the other's ops->start_xmit handler.
613+
* In order to safely bridge channels we must reject channels which are already
614+
* part of a bridge instance, or which form part of an existing unit.
615+
* Once successfully bridged, each channel holds a reference on the other
616+
* to prevent it being freed while the bridge is extant.
617+
*/
618+
static int ppp_bridge_channels(struct channel *pch, struct channel *pchb)
619+
{
620+
write_lock_bh(&pch->upl);
621+
if (pch->ppp ||
622+
rcu_dereference_protected(pch->bridge, lockdep_is_held(&pch->upl))) {
623+
write_unlock_bh(&pch->upl);
624+
return -EALREADY;
625+
}
626+
rcu_assign_pointer(pch->bridge, pchb);
627+
write_unlock_bh(&pch->upl);
628+
629+
write_lock_bh(&pchb->upl);
630+
if (pchb->ppp ||
631+
rcu_dereference_protected(pchb->bridge, lockdep_is_held(&pchb->upl))) {
632+
write_unlock_bh(&pchb->upl);
633+
goto err_unset;
634+
}
635+
rcu_assign_pointer(pchb->bridge, pch);
636+
write_unlock_bh(&pchb->upl);
637+
638+
refcount_inc(&pch->file.refcnt);
639+
refcount_inc(&pchb->file.refcnt);
640+
641+
return 0;
642+
643+
err_unset:
644+
write_lock_bh(&pch->upl);
645+
RCU_INIT_POINTER(pch->bridge, NULL);
646+
write_unlock_bh(&pch->upl);
647+
synchronize_rcu();
648+
return -EALREADY;
649+
}
650+
651+
static int ppp_unbridge_channels(struct channel *pch)
652+
{
653+
struct channel *pchb, *pchbb;
654+
655+
write_lock_bh(&pch->upl);
656+
pchb = rcu_dereference_protected(pch->bridge, lockdep_is_held(&pch->upl));
657+
if (!pchb) {
658+
write_unlock_bh(&pch->upl);
659+
return -EINVAL;
660+
}
661+
RCU_INIT_POINTER(pch->bridge, NULL);
662+
write_unlock_bh(&pch->upl);
663+
664+
/* Only modify pchb if phcb->bridge points back to pch.
665+
* If not, it implies that there has been a race unbridging (and possibly
666+
* even rebridging) pchb. We should leave pchb alone to avoid either a
667+
* refcount underflow, or breaking another established bridge instance.
668+
*/
669+
write_lock_bh(&pchb->upl);
670+
pchbb = rcu_dereference_protected(pchb->bridge, lockdep_is_held(&pchb->upl));
671+
if (pchbb == pch)
672+
RCU_INIT_POINTER(pchb->bridge, NULL);
673+
write_unlock_bh(&pchb->upl);
674+
675+
synchronize_rcu();
676+
677+
if (pchbb == pch)
678+
if (refcount_dec_and_test(&pch->file.refcnt))
679+
ppp_destroy_channel(pch);
680+
681+
if (refcount_dec_and_test(&pchb->file.refcnt))
682+
ppp_destroy_channel(pchb);
683+
684+
return 0;
685+
}
686+
609687
static long ppp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
610688
{
611689
struct ppp_file *pf;
@@ -641,8 +719,9 @@ static long ppp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
641719
}
642720

643721
if (pf->kind == CHANNEL) {
644-
struct channel *pch;
722+
struct channel *pch, *pchb;
645723
struct ppp_channel *chan;
724+
struct ppp_net *pn;
646725

647726
pch = PF_TO_CHANNEL(pf);
648727

@@ -657,6 +736,31 @@ static long ppp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
657736
err = ppp_disconnect_channel(pch);
658737
break;
659738

739+
case PPPIOCBRIDGECHAN:
740+
if (get_user(unit, p))
741+
break;
742+
err = -ENXIO;
743+
pn = ppp_pernet(current->nsproxy->net_ns);
744+
spin_lock_bh(&pn->all_channels_lock);
745+
pchb = ppp_find_channel(pn, unit);
746+
/* Hold a reference to prevent pchb being freed while
747+
* we establish the bridge.
748+
*/
749+
if (pchb)
750+
refcount_inc(&pchb->file.refcnt);
751+
spin_unlock_bh(&pn->all_channels_lock);
752+
if (!pchb)
753+
break;
754+
err = ppp_bridge_channels(pch, pchb);
755+
/* Drop earlier refcount now bridge establishment is complete */
756+
if (refcount_dec_and_test(&pchb->file.refcnt))
757+
ppp_destroy_channel(pchb);
758+
break;
759+
760+
case PPPIOCUNBRIDGECHAN:
761+
err = ppp_unbridge_channels(pch);
762+
break;
763+
660764
default:
661765
down_read(&pch->chan_sem);
662766
chan = pch->chan;
@@ -2089,6 +2193,40 @@ static bool ppp_decompress_proto(struct sk_buff *skb)
20892193
return pskb_may_pull(skb, 2);
20902194
}
20912195

2196+
/* Attempt to handle a frame via. a bridged channel, if one exists.
2197+
* If the channel is bridged, the frame is consumed by the bridge.
2198+
* If not, the caller must handle the frame by normal recv mechanisms.
2199+
* Returns true if the frame is consumed, false otherwise.
2200+
*/
2201+
static bool ppp_channel_bridge_input(struct channel *pch, struct sk_buff *skb)
2202+
{
2203+
struct channel *pchb;
2204+
2205+
rcu_read_lock();
2206+
pchb = rcu_dereference(pch->bridge);
2207+
if (!pchb)
2208+
goto out_rcu;
2209+
2210+
spin_lock(&pchb->downl);
2211+
if (!pchb->chan) {
2212+
/* channel got unregistered */
2213+
kfree_skb(skb);
2214+
goto outl;
2215+
}
2216+
2217+
skb_scrub_packet(skb, !net_eq(pch->chan_net, pchb->chan_net));
2218+
if (!pchb->chan->ops->start_xmit(pchb->chan, skb))
2219+
kfree_skb(skb);
2220+
2221+
outl:
2222+
spin_unlock(&pchb->downl);
2223+
out_rcu:
2224+
rcu_read_unlock();
2225+
2226+
/* If pchb is set then we've consumed the packet */
2227+
return !!pchb;
2228+
}
2229+
20922230
void
20932231
ppp_input(struct ppp_channel *chan, struct sk_buff *skb)
20942232
{
@@ -2100,6 +2238,10 @@ ppp_input(struct ppp_channel *chan, struct sk_buff *skb)
21002238
return;
21012239
}
21022240

2241+
/* If the channel is bridged, transmit via. bridge */
2242+
if (ppp_channel_bridge_input(pch, skb))
2243+
return;
2244+
21032245
read_lock_bh(&pch->upl);
21042246
if (!ppp_decompress_proto(skb)) {
21052247
kfree_skb(skb);
@@ -2796,8 +2938,11 @@ ppp_unregister_channel(struct ppp_channel *chan)
27962938
list_del(&pch->list);
27972939
spin_unlock_bh(&pn->all_channels_lock);
27982940

2941+
ppp_unbridge_channels(pch);
2942+
27992943
pch->file.dead = 1;
28002944
wake_up_interruptible(&pch->file.rwait);
2945+
28012946
if (refcount_dec_and_test(&pch->file.refcnt))
28022947
ppp_destroy_channel(pch);
28032948
}
@@ -3270,7 +3415,8 @@ ppp_connect_channel(struct channel *pch, int unit)
32703415
goto out;
32713416
write_lock_bh(&pch->upl);
32723417
ret = -EINVAL;
3273-
if (pch->ppp)
3418+
if (pch->ppp ||
3419+
rcu_dereference_protected(pch->bridge, lockdep_is_held(&pch->upl)))
32743420
goto outl;
32753421

32763422
ppp_lock(ppp);

include/uapi/linux/ppp-ioctl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ struct pppol2tp_ioc_stats {
115115
#define PPPIOCATTCHAN _IOW('t', 56, int) /* attach to ppp channel */
116116
#define PPPIOCGCHAN _IOR('t', 55, int) /* get ppp channel number */
117117
#define PPPIOCGL2TPSTATS _IOR('t', 54, struct pppol2tp_ioc_stats)
118+
#define PPPIOCBRIDGECHAN _IOW('t', 53, int) /* bridge one channel to another */
119+
#define PPPIOCUNBRIDGECHAN _IO('t', 54) /* unbridge channel */
118120

119121
#define SIOCGPPPSTATS (SIOCDEVPRIVATE + 0)
120122
#define SIOCGPPPVER (SIOCDEVPRIVATE + 1) /* NEVER change this!! */

0 commit comments

Comments
 (0)