Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Tom Parkin and committed by
David S. Miller
4cf476ce 51e13685

+151 -3
+149 -3
drivers/net/ppp/ppp_generic.c
··· 174 174 struct ppp *ppp; /* ppp unit we're connected to */ 175 175 struct net *chan_net; /* the net channel belongs to */ 176 176 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 */ 178 179 #ifdef CONFIG_PPP_MULTILINK 179 180 u8 avail; /* flag used in multilink stuff */ 180 181 u8 had_frag; /* >= 1 fragments have been sent */ ··· 607 606 #endif 608 607 #endif 609 608 609 + /* Bridge one PPP channel to another. 610 + * When two channels are bridged, ppp_input on one channel is redirected to 611 + * the other's ops->start_xmit handler. 612 + * In order to safely bridge channels we must reject channels which are already 613 + * part of a bridge instance, or which form part of an existing unit. 614 + * Once successfully bridged, each channel holds a reference on the other 615 + * to prevent it being freed while the bridge is extant. 616 + */ 617 + static int ppp_bridge_channels(struct channel *pch, struct channel *pchb) 618 + { 619 + write_lock_bh(&pch->upl); 620 + if (pch->ppp || 621 + rcu_dereference_protected(pch->bridge, lockdep_is_held(&pch->upl))) { 622 + write_unlock_bh(&pch->upl); 623 + return -EALREADY; 624 + } 625 + rcu_assign_pointer(pch->bridge, pchb); 626 + write_unlock_bh(&pch->upl); 627 + 628 + write_lock_bh(&pchb->upl); 629 + if (pchb->ppp || 630 + rcu_dereference_protected(pchb->bridge, lockdep_is_held(&pchb->upl))) { 631 + write_unlock_bh(&pchb->upl); 632 + goto err_unset; 633 + } 634 + rcu_assign_pointer(pchb->bridge, pch); 635 + write_unlock_bh(&pchb->upl); 636 + 637 + refcount_inc(&pch->file.refcnt); 638 + refcount_inc(&pchb->file.refcnt); 639 + 640 + return 0; 641 + 642 + err_unset: 643 + write_lock_bh(&pch->upl); 644 + RCU_INIT_POINTER(pch->bridge, NULL); 645 + write_unlock_bh(&pch->upl); 646 + synchronize_rcu(); 647 + return -EALREADY; 648 + } 649 + 650 + static int ppp_unbridge_channels(struct channel *pch) 651 + { 652 + struct channel *pchb, *pchbb; 653 + 654 + write_lock_bh(&pch->upl); 655 + pchb = rcu_dereference_protected(pch->bridge, lockdep_is_held(&pch->upl)); 656 + if (!pchb) { 657 + write_unlock_bh(&pch->upl); 658 + return -EINVAL; 659 + } 660 + RCU_INIT_POINTER(pch->bridge, NULL); 661 + write_unlock_bh(&pch->upl); 662 + 663 + /* Only modify pchb if phcb->bridge points back to pch. 664 + * If not, it implies that there has been a race unbridging (and possibly 665 + * even rebridging) pchb. We should leave pchb alone to avoid either a 666 + * refcount underflow, or breaking another established bridge instance. 667 + */ 668 + write_lock_bh(&pchb->upl); 669 + pchbb = rcu_dereference_protected(pchb->bridge, lockdep_is_held(&pchb->upl)); 670 + if (pchbb == pch) 671 + RCU_INIT_POINTER(pchb->bridge, NULL); 672 + write_unlock_bh(&pchb->upl); 673 + 674 + synchronize_rcu(); 675 + 676 + if (pchbb == pch) 677 + if (refcount_dec_and_test(&pch->file.refcnt)) 678 + ppp_destroy_channel(pch); 679 + 680 + if (refcount_dec_and_test(&pchb->file.refcnt)) 681 + ppp_destroy_channel(pchb); 682 + 683 + return 0; 684 + } 685 + 610 686 static long ppp_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 611 687 { 612 688 struct ppp_file *pf; ··· 719 641 } 720 642 721 643 if (pf->kind == CHANNEL) { 722 - struct channel *pch; 644 + struct channel *pch, *pchb; 723 645 struct ppp_channel *chan; 646 + struct ppp_net *pn; 724 647 725 648 pch = PF_TO_CHANNEL(pf); 726 649 ··· 734 655 735 656 case PPPIOCDISCONN: 736 657 err = ppp_disconnect_channel(pch); 658 + break; 659 + 660 + case PPPIOCBRIDGECHAN: 661 + if (get_user(unit, p)) 662 + break; 663 + err = -ENXIO; 664 + pn = ppp_pernet(current->nsproxy->net_ns); 665 + spin_lock_bh(&pn->all_channels_lock); 666 + pchb = ppp_find_channel(pn, unit); 667 + /* Hold a reference to prevent pchb being freed while 668 + * we establish the bridge. 669 + */ 670 + if (pchb) 671 + refcount_inc(&pchb->file.refcnt); 672 + spin_unlock_bh(&pn->all_channels_lock); 673 + if (!pchb) 674 + break; 675 + err = ppp_bridge_channels(pch, pchb); 676 + /* Drop earlier refcount now bridge establishment is complete */ 677 + if (refcount_dec_and_test(&pchb->file.refcnt)) 678 + ppp_destroy_channel(pchb); 679 + break; 680 + 681 + case PPPIOCUNBRIDGECHAN: 682 + err = ppp_unbridge_channels(pch); 737 683 break; 738 684 739 685 default: ··· 2193 2089 return pskb_may_pull(skb, 2); 2194 2090 } 2195 2091 2092 + /* Attempt to handle a frame via. a bridged channel, if one exists. 2093 + * If the channel is bridged, the frame is consumed by the bridge. 2094 + * If not, the caller must handle the frame by normal recv mechanisms. 2095 + * Returns true if the frame is consumed, false otherwise. 2096 + */ 2097 + static bool ppp_channel_bridge_input(struct channel *pch, struct sk_buff *skb) 2098 + { 2099 + struct channel *pchb; 2100 + 2101 + rcu_read_lock(); 2102 + pchb = rcu_dereference(pch->bridge); 2103 + if (!pchb) 2104 + goto out_rcu; 2105 + 2106 + spin_lock(&pchb->downl); 2107 + if (!pchb->chan) { 2108 + /* channel got unregistered */ 2109 + kfree_skb(skb); 2110 + goto outl; 2111 + } 2112 + 2113 + skb_scrub_packet(skb, !net_eq(pch->chan_net, pchb->chan_net)); 2114 + if (!pchb->chan->ops->start_xmit(pchb->chan, skb)) 2115 + kfree_skb(skb); 2116 + 2117 + outl: 2118 + spin_unlock(&pchb->downl); 2119 + out_rcu: 2120 + rcu_read_unlock(); 2121 + 2122 + /* If pchb is set then we've consumed the packet */ 2123 + return !!pchb; 2124 + } 2125 + 2196 2126 void 2197 2127 ppp_input(struct ppp_channel *chan, struct sk_buff *skb) 2198 2128 { ··· 2237 2099 kfree_skb(skb); 2238 2100 return; 2239 2101 } 2102 + 2103 + /* If the channel is bridged, transmit via. bridge */ 2104 + if (ppp_channel_bridge_input(pch, skb)) 2105 + return; 2240 2106 2241 2107 read_lock_bh(&pch->upl); 2242 2108 if (!ppp_decompress_proto(skb)) { ··· 2938 2796 list_del(&pch->list); 2939 2797 spin_unlock_bh(&pn->all_channels_lock); 2940 2798 2799 + ppp_unbridge_channels(pch); 2800 + 2941 2801 pch->file.dead = 1; 2942 2802 wake_up_interruptible(&pch->file.rwait); 2803 + 2943 2804 if (refcount_dec_and_test(&pch->file.refcnt)) 2944 2805 ppp_destroy_channel(pch); 2945 2806 } ··· 3415 3270 goto out; 3416 3271 write_lock_bh(&pch->upl); 3417 3272 ret = -EINVAL; 3418 - if (pch->ppp) 3273 + if (pch->ppp || 3274 + rcu_dereference_protected(pch->bridge, lockdep_is_held(&pch->upl))) 3419 3275 goto outl; 3420 3276 3421 3277 ppp_lock(ppp);
+2
include/uapi/linux/ppp-ioctl.h
··· 115 115 #define PPPIOCATTCHAN _IOW('t', 56, int) /* attach to ppp channel */ 116 116 #define PPPIOCGCHAN _IOR('t', 55, int) /* get ppp channel number */ 117 117 #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 */ 118 120 119 121 #define SIOCGPPPSTATS (SIOCDEVPRIVATE + 0) 120 122 #define SIOCGPPPVER (SIOCDEVPRIVATE + 1) /* NEVER change this!! */