Skip to content

Commit 3305f9a

Browse files
committed
Merge branch 'l2tp-rework-pppol2tp-ioctl-handling'
Guillaume Nault says: ==================== l2tp: rework pppol2tp ioctl handling The current ioctl() handling code can be simplified. It tests for non-relevant conditions and uselessly holds sockets. Once useless code is removed, it becomes even simpler to let pppol2tp_ioctl() handle commands directly, rather than dispatch them to pppol2tp_tunnel_ioctl() or pppol2tp_session_ioctl(). That is the approach taken by this series. Patch #1 and #2 define helper functions aimed at simplifying the rest of the patch set. Patch #3 drops useless tests in pppol2p_ioctl() and avoid holding a refcount on the socket. Patches #4, #5 and torvalds#6 are the core of the series. They let pppol2tp_ioctl() handle all ioctls and drop the tunnel and session specific functions. Then patch torvalds#6 brings a little bit of consolidation. Finally, patch torvalds#7 takes advantage of the simplified code to make pppol2tp sockets compatible with dev_ioctl(). Certainly not a killer feature, but it is trivial and it is always nice to see l2tp getting better integration with the rest of the stack. ==================== Signed-off-by: David S. Miller <davem@davemloft.net>
2 parents 8a8982d + 4f5f85e commit 3305f9a

File tree

7 files changed

+133
-199
lines changed

7 files changed

+133
-199
lines changed

include/uapi/linux/ppp-ioctl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ struct ppp_option_data {
6868
struct pppol2tp_ioc_stats {
6969
__u16 tunnel_id; /* redundant */
7070
__u16 session_id; /* if zero, get tunnel stats */
71-
__u32 using_ipsec:1; /* valid only for session_id == 0 */
71+
__u32 using_ipsec:1;
7272
__aligned_u64 tx_packets;
7373
__aligned_u64 tx_bytes;
7474
__aligned_u64 tx_errors;

net/l2tp/l2tp_core.c

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -203,44 +203,44 @@ struct l2tp_tunnel *l2tp_tunnel_get_nth(const struct net *net, int nth)
203203
}
204204
EXPORT_SYMBOL_GPL(l2tp_tunnel_get_nth);
205205

206-
/* Lookup a session. A new reference is held on the returned session. */
207-
struct l2tp_session *l2tp_session_get(const struct net *net,
208-
struct l2tp_tunnel *tunnel,
209-
u32 session_id)
206+
struct l2tp_session *l2tp_tunnel_get_session(struct l2tp_tunnel *tunnel,
207+
u32 session_id)
210208
{
211209
struct hlist_head *session_list;
212210
struct l2tp_session *session;
213211

214-
if (!tunnel) {
215-
struct l2tp_net *pn = l2tp_pernet(net);
216-
217-
session_list = l2tp_session_id_hash_2(pn, session_id);
212+
session_list = l2tp_session_id_hash(tunnel, session_id);
218213

219-
rcu_read_lock_bh();
220-
hlist_for_each_entry_rcu(session, session_list, global_hlist) {
221-
if (session->session_id == session_id) {
222-
l2tp_session_inc_refcount(session);
223-
rcu_read_unlock_bh();
214+
read_lock_bh(&tunnel->hlist_lock);
215+
hlist_for_each_entry(session, session_list, hlist)
216+
if (session->session_id == session_id) {
217+
l2tp_session_inc_refcount(session);
218+
read_unlock_bh(&tunnel->hlist_lock);
224219

225-
return session;
226-
}
220+
return session;
227221
}
228-
rcu_read_unlock_bh();
222+
read_unlock_bh(&tunnel->hlist_lock);
229223

230-
return NULL;
231-
}
224+
return NULL;
225+
}
226+
EXPORT_SYMBOL_GPL(l2tp_tunnel_get_session);
232227

233-
session_list = l2tp_session_id_hash(tunnel, session_id);
234-
read_lock_bh(&tunnel->hlist_lock);
235-
hlist_for_each_entry(session, session_list, hlist) {
228+
struct l2tp_session *l2tp_session_get(const struct net *net, u32 session_id)
229+
{
230+
struct hlist_head *session_list;
231+
struct l2tp_session *session;
232+
233+
session_list = l2tp_session_id_hash_2(l2tp_pernet(net), session_id);
234+
235+
rcu_read_lock_bh();
236+
hlist_for_each_entry_rcu(session, session_list, global_hlist)
236237
if (session->session_id == session_id) {
237238
l2tp_session_inc_refcount(session);
238-
read_unlock_bh(&tunnel->hlist_lock);
239+
rcu_read_unlock_bh();
239240

240241
return session;
241242
}
242-
}
243-
read_unlock_bh(&tunnel->hlist_lock);
243+
rcu_read_unlock_bh();
244244

245245
return NULL;
246246
}
@@ -872,7 +872,7 @@ static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb)
872872
}
873873

874874
/* Find the session context */
875-
session = l2tp_session_get(tunnel->l2tp_net, tunnel, session_id);
875+
session = l2tp_tunnel_get_session(tunnel, session_id);
876876
if (!session || !session->recv_skb) {
877877
if (session)
878878
l2tp_session_dec_refcount(session);

net/l2tp/l2tp_core.h

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
#include <net/dst.h>
1616
#include <net/sock.h>
1717

18+
#ifdef CONFIG_XFRM
19+
#include <net/xfrm.h>
20+
#endif
21+
1822
/* Just some random numbers */
1923
#define L2TP_TUNNEL_MAGIC 0x42114DDA
2024
#define L2TP_SESSION_MAGIC 0x0C04EB7D
@@ -192,12 +196,12 @@ static inline void *l2tp_session_priv(struct l2tp_session *session)
192196

193197
struct l2tp_tunnel *l2tp_tunnel_get(const struct net *net, u32 tunnel_id);
194198
struct l2tp_tunnel *l2tp_tunnel_get_nth(const struct net *net, int nth);
199+
struct l2tp_session *l2tp_tunnel_get_session(struct l2tp_tunnel *tunnel,
200+
u32 session_id);
195201

196202
void l2tp_tunnel_free(struct l2tp_tunnel *tunnel);
197203

198-
struct l2tp_session *l2tp_session_get(const struct net *net,
199-
struct l2tp_tunnel *tunnel,
200-
u32 session_id);
204+
struct l2tp_session *l2tp_session_get(const struct net *net, u32 session_id);
201205
struct l2tp_session *l2tp_session_get_nth(struct l2tp_tunnel *tunnel, int nth);
202206
struct l2tp_session *l2tp_session_get_by_ifname(const struct net *net,
203207
const char *ifname);
@@ -284,6 +288,21 @@ static inline u32 l2tp_tunnel_dst_mtu(const struct l2tp_tunnel *tunnel)
284288
return mtu;
285289
}
286290

291+
#ifdef CONFIG_XFRM
292+
static inline bool l2tp_tunnel_uses_xfrm(const struct l2tp_tunnel *tunnel)
293+
{
294+
struct sock *sk = tunnel->sock;
295+
296+
return sk && (rcu_access_pointer(sk->sk_policy[0]) ||
297+
rcu_access_pointer(sk->sk_policy[1]));
298+
}
299+
#else
300+
static inline bool l2tp_tunnel_uses_xfrm(const struct l2tp_tunnel *tunnel)
301+
{
302+
return false;
303+
}
304+
#endif
305+
287306
#define l2tp_printk(ptr, type, func, fmt, ...) \
288307
do { \
289308
if (((ptr)->debug) & (type)) \

net/l2tp/l2tp_ip.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ static int l2tp_ip_recv(struct sk_buff *skb)
144144
}
145145

146146
/* Ok, this is a data packet. Lookup the session. */
147-
session = l2tp_session_get(net, NULL, session_id);
147+
session = l2tp_session_get(net, session_id);
148148
if (!session)
149149
goto discard;
150150

net/l2tp/l2tp_ip6.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ static int l2tp_ip6_recv(struct sk_buff *skb)
157157
}
158158

159159
/* Ok, this is a data packet. Lookup the session. */
160-
session = l2tp_session_get(net, NULL, session_id);
160+
session = l2tp_session_get(net, session_id);
161161
if (!session)
162162
goto discard;
163163

net/l2tp/l2tp_netlink.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ static struct l2tp_session *l2tp_nl_session_get(struct genl_info *info)
6666
session_id = nla_get_u32(info->attrs[L2TP_ATTR_SESSION_ID]);
6767
tunnel = l2tp_tunnel_get(net, tunnel_id);
6868
if (tunnel) {
69-
session = l2tp_session_get(net, tunnel, session_id);
69+
session = l2tp_tunnel_get_session(tunnel, session_id);
7070
l2tp_tunnel_dec_refcount(tunnel);
7171
}
7272
}
@@ -627,7 +627,7 @@ static int l2tp_nl_cmd_session_create(struct sk_buff *skb, struct genl_info *inf
627627
&cfg);
628628

629629
if (ret >= 0) {
630-
session = l2tp_session_get(net, tunnel, session_id);
630+
session = l2tp_tunnel_get_session(tunnel, session_id);
631631
if (session) {
632632
ret = l2tp_session_notify(&l2tp_nl_family, info, session,
633633
L2TP_CMD_SESSION_CREATE);
@@ -710,9 +710,6 @@ static int l2tp_nl_session_send(struct sk_buff *skb, u32 portid, u32 seq, int fl
710710
void *hdr;
711711
struct nlattr *nest;
712712
struct l2tp_tunnel *tunnel = session->tunnel;
713-
struct sock *sk = NULL;
714-
715-
sk = tunnel->sock;
716713

717714
hdr = genlmsg_put(skb, portid, seq, &l2tp_nl_family, flags, cmd);
718715
if (!hdr)
@@ -738,10 +735,8 @@ static int l2tp_nl_session_send(struct sk_buff *skb, u32 portid, u32 seq, int fl
738735
nla_put_u8(skb, L2TP_ATTR_RECV_SEQ, session->recv_seq) ||
739736
nla_put_u8(skb, L2TP_ATTR_SEND_SEQ, session->send_seq) ||
740737
nla_put_u8(skb, L2TP_ATTR_LNS_MODE, session->lns_mode) ||
741-
#ifdef CONFIG_XFRM
742-
(((sk) && (sk->sk_policy[0] || sk->sk_policy[1])) &&
738+
(l2tp_tunnel_uses_xfrm(tunnel) &&
743739
nla_put_u8(skb, L2TP_ATTR_USING_IPSEC, 1)) ||
744-
#endif
745740
(session->reorder_timeout &&
746741
nla_put_msecs(skb, L2TP_ATTR_RECV_TIMEOUT,
747742
session->reorder_timeout, L2TP_ATTR_PAD)))

0 commit comments

Comments
 (0)