1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /* L2TP internal definitions.
3  *
4  * Copyright (c) 2008,2009 Katalix Systems Ltd
5  */
6 #include <linux/refcount.h>
7 
8 #ifndef _L2TP_CORE_H_
9 #define _L2TP_CORE_H_
10 
11 #include <net/dst.h>
12 #include <net/sock.h>
13 
14 #ifdef CONFIG_XFRM
15 #include <net/xfrm.h>
16 #endif
17 
18 /* Random numbers used for internal consistency checks of tunnel and session structures */
19 #define L2TP_TUNNEL_MAGIC	0x42114DDA
20 #define L2TP_SESSION_MAGIC	0x0C04EB7D
21 
22 /* Per tunnel session hash table size */
23 #define L2TP_HASH_BITS	4
24 #define L2TP_HASH_SIZE	BIT(L2TP_HASH_BITS)
25 
26 /* System-wide session hash table size */
27 #define L2TP_HASH_BITS_2	8
28 #define L2TP_HASH_SIZE_2	BIT(L2TP_HASH_BITS_2)
29 
30 struct sk_buff;
31 
32 struct l2tp_stats {
33 	atomic_long_t		tx_packets;
34 	atomic_long_t		tx_bytes;
35 	atomic_long_t		tx_errors;
36 	atomic_long_t		rx_packets;
37 	atomic_long_t		rx_bytes;
38 	atomic_long_t		rx_seq_discards;
39 	atomic_long_t		rx_oos_packets;
40 	atomic_long_t		rx_errors;
41 	atomic_long_t		rx_cookie_discards;
42 };
43 
44 struct l2tp_tunnel;
45 
46 /* L2TP session configuration */
47 struct l2tp_session_cfg {
48 	enum l2tp_pwtype	pw_type;
49 	unsigned int		recv_seq:1;	/* expect receive packets with sequence numbers? */
50 	unsigned int		send_seq:1;	/* send packets with sequence numbers? */
51 	unsigned int		lns_mode:1;	/* behave as LNS?
52 						 * LAC enables sequence numbers under LNS control.
53 						 */
54 	u16			l2specific_type; /* Layer 2 specific type */
55 	u8			cookie[8];	/* optional cookie */
56 	int			cookie_len;	/* 0, 4 or 8 bytes */
57 	u8			peer_cookie[8];	/* peer's cookie */
58 	int			peer_cookie_len; /* 0, 4 or 8 bytes */
59 	int			reorder_timeout; /* configured reorder timeout (in jiffies) */
60 	char			*ifname;
61 };
62 
63 /* Represents a session (pseudowire) instance.
64  * Tracks runtime state including cookies, dataplane packet sequencing, and IO statistics.
65  * Is linked into a per-tunnel session hashlist; and in the case of an L2TPv3 session into
66  * an additional per-net ("global") hashlist.
67  */
68 #define L2TP_SESSION_NAME_MAX 32
69 struct l2tp_session {
70 	int			magic;		/* should be L2TP_SESSION_MAGIC */
71 	long			dead;
72 
73 	struct l2tp_tunnel	*tunnel;	/* back pointer to tunnel context */
74 	u32			session_id;
75 	u32			peer_session_id;
76 	u8			cookie[8];
77 	int			cookie_len;
78 	u8			peer_cookie[8];
79 	int			peer_cookie_len;
80 	u16			l2specific_type;
81 	u16			hdr_len;
82 	u32			nr;		/* session NR state (receive) */
83 	u32			ns;		/* session NR state (send) */
84 	struct sk_buff_head	reorder_q;	/* receive reorder queue */
85 	u32			nr_max;		/* max NR. Depends on tunnel */
86 	u32			nr_window_size;	/* NR window size */
87 	u32			nr_oos;		/* NR of last OOS packet */
88 	int			nr_oos_count;	/* for OOS recovery */
89 	int			nr_oos_count_max;
90 	struct hlist_node	hlist;		/* hash list node */
91 	refcount_t		ref_count;
92 
93 	char			name[L2TP_SESSION_NAME_MAX]; /* for logging */
94 	char			ifname[IFNAMSIZ];
95 	unsigned int		recv_seq:1;	/* expect receive packets with sequence numbers? */
96 	unsigned int		send_seq:1;	/* send packets with sequence numbers? */
97 	unsigned int		lns_mode:1;	/* behave as LNS?
98 						 * LAC enables sequence numbers under LNS control.
99 						 */
100 	int			reorder_timeout; /* configured reorder timeout (in jiffies) */
101 	int			reorder_skip;	/* set if skip to next nr */
102 	enum l2tp_pwtype	pwtype;
103 	struct l2tp_stats	stats;
104 	struct hlist_node	global_hlist;	/* global hash list node */
105 
106 	/* Session receive handler for data packets.
107 	 * Each pseudowire implementation should implement this callback in order to
108 	 * handle incoming packets.  Packets are passed to the pseudowire handler after
109 	 * reordering, if data sequence numbers are enabled for the session.
110 	 */
111 	void (*recv_skb)(struct l2tp_session *session, struct sk_buff *skb, int data_len);
112 
113 	/* Session close handler.
114 	 * Each pseudowire implementation may implement this callback in order to carry
115 	 * out pseudowire-specific shutdown actions.
116 	 * The callback is called by core after unhashing the session and purging its
117 	 * reorder queue.
118 	 */
119 	void (*session_close)(struct l2tp_session *session);
120 
121 	/* Session show handler.
122 	 * Pseudowire-specific implementation of debugfs session rendering.
123 	 * The callback is called by l2tp_debugfs.c after rendering core session
124 	 * information.
125 	 */
126 	void (*show)(struct seq_file *m, void *priv);
127 
128 	u8			priv[];		/* private data */
129 };
130 
131 /* L2TP tunnel configuration */
132 struct l2tp_tunnel_cfg {
133 	enum l2tp_encap_type	encap;
134 
135 	/* Used only for kernel-created sockets */
136 	struct in_addr		local_ip;
137 	struct in_addr		peer_ip;
138 #if IS_ENABLED(CONFIG_IPV6)
139 	struct in6_addr		*local_ip6;
140 	struct in6_addr		*peer_ip6;
141 #endif
142 	u16			local_udp_port;
143 	u16			peer_udp_port;
144 	unsigned int		use_udp_checksums:1,
145 				udp6_zero_tx_checksums:1,
146 				udp6_zero_rx_checksums:1;
147 };
148 
149 /* Represents a tunnel instance.
150  * Tracks runtime state including IO statistics.
151  * Holds the tunnel socket (either passed from userspace or directly created by the kernel).
152  * Maintains a hashlist of sessions belonging to the tunnel instance.
153  * Is linked into a per-net list of tunnels.
154  */
155 #define L2TP_TUNNEL_NAME_MAX 20
156 struct l2tp_tunnel {
157 	int			magic;		/* Should be L2TP_TUNNEL_MAGIC */
158 
159 	unsigned long		dead;
160 
161 	struct rcu_head rcu;
162 	rwlock_t		hlist_lock;	/* protect session_hlist */
163 	bool			acpt_newsess;	/* indicates whether this tunnel accepts
164 						 * new sessions. Protected by hlist_lock.
165 						 */
166 	struct hlist_head	session_hlist[L2TP_HASH_SIZE];
167 						/* hashed list of sessions, hashed by id */
168 	u32			tunnel_id;
169 	u32			peer_tunnel_id;
170 	int			version;	/* 2=>L2TPv2, 3=>L2TPv3 */
171 
172 	char			name[L2TP_TUNNEL_NAME_MAX]; /* for logging */
173 	enum l2tp_encap_type	encap;
174 	struct l2tp_stats	stats;
175 
176 	struct list_head	list;		/* list node on per-namespace list of tunnels */
177 	struct net		*l2tp_net;	/* the net we belong to */
178 
179 	refcount_t		ref_count;
180 	void (*old_sk_destruct)(struct sock *sk);
181 	struct sock		*sock;		/* parent socket */
182 	int			fd;		/* parent fd, if tunnel socket was created
183 						 * by userspace
184 						 */
185 
186 	struct work_struct	del_work;
187 };
188 
189 /* Pseudowire ops callbacks for use with the l2tp genetlink interface */
190 struct l2tp_nl_cmd_ops {
191 	/* The pseudowire session create callback is responsible for creating a session
192 	 * instance for a specific pseudowire type.
193 	 * It must call l2tp_session_create and l2tp_session_register to register the
194 	 * session instance, as well as carry out any pseudowire-specific initialisation.
195 	 * It must return >= 0 on success, or an appropriate negative errno value on failure.
196 	 */
197 	int (*session_create)(struct net *net, struct l2tp_tunnel *tunnel,
198 			      u32 session_id, u32 peer_session_id,
199 			      struct l2tp_session_cfg *cfg);
200 
201 	/* The pseudowire session delete callback is responsible for initiating the deletion
202 	 * of a session instance.
203 	 * It must call l2tp_session_delete, as well as carry out any pseudowire-specific
204 	 * teardown actions.
205 	 */
206 	void (*session_delete)(struct l2tp_session *session);
207 };
208 
l2tp_session_priv(struct l2tp_session * session)209 static inline void *l2tp_session_priv(struct l2tp_session *session)
210 {
211 	return &session->priv[0];
212 }
213 
214 /* Tunnel and session refcounts */
215 void l2tp_tunnel_inc_refcount(struct l2tp_tunnel *tunnel);
216 void l2tp_tunnel_dec_refcount(struct l2tp_tunnel *tunnel);
217 void l2tp_session_inc_refcount(struct l2tp_session *session);
218 void l2tp_session_dec_refcount(struct l2tp_session *session);
219 
220 /* Tunnel and session lookup.
221  * These functions take a reference on the instances they return, so
222  * the caller must ensure that the reference is dropped appropriately.
223  */
224 struct l2tp_tunnel *l2tp_tunnel_get(const struct net *net, u32 tunnel_id);
225 struct l2tp_tunnel *l2tp_tunnel_get_nth(const struct net *net, int nth);
226 struct l2tp_session *l2tp_tunnel_get_session(struct l2tp_tunnel *tunnel,
227 					     u32 session_id);
228 
229 struct l2tp_session *l2tp_session_get(const struct net *net, u32 session_id);
230 struct l2tp_session *l2tp_session_get_nth(struct l2tp_tunnel *tunnel, int nth);
231 struct l2tp_session *l2tp_session_get_by_ifname(const struct net *net,
232 						const char *ifname);
233 
234 /* Tunnel and session lifetime management.
235  * Creation of a new instance is a two-step process: create, then register.
236  * Destruction is triggered using the *_delete functions, and completes asynchronously.
237  */
238 int l2tp_tunnel_create(int fd, int version, u32 tunnel_id,
239 		       u32 peer_tunnel_id, struct l2tp_tunnel_cfg *cfg,
240 		       struct l2tp_tunnel **tunnelp);
241 int l2tp_tunnel_register(struct l2tp_tunnel *tunnel, struct net *net,
242 			 struct l2tp_tunnel_cfg *cfg);
243 void l2tp_tunnel_delete(struct l2tp_tunnel *tunnel);
244 
245 struct l2tp_session *l2tp_session_create(int priv_size,
246 					 struct l2tp_tunnel *tunnel,
247 					 u32 session_id, u32 peer_session_id,
248 					 struct l2tp_session_cfg *cfg);
249 int l2tp_session_register(struct l2tp_session *session,
250 			  struct l2tp_tunnel *tunnel);
251 void l2tp_session_delete(struct l2tp_session *session);
252 
253 /* Receive path helpers.  If data sequencing is enabled for the session these
254  * functions handle queuing and reordering prior to passing packets to the
255  * pseudowire code to be passed to userspace.
256  */
257 void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
258 		      unsigned char *ptr, unsigned char *optr, u16 hdrflags,
259 		      int length);
260 int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb);
261 
262 /* Transmit path helpers for sending packets over the tunnel socket. */
263 void l2tp_session_set_header_len(struct l2tp_session *session, int version);
264 int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb);
265 
266 /* Pseudowire management.
267  * Pseudowires should register with l2tp core on module init, and unregister
268  * on module exit.
269  */
270 int l2tp_nl_register_ops(enum l2tp_pwtype pw_type, const struct l2tp_nl_cmd_ops *ops);
271 void l2tp_nl_unregister_ops(enum l2tp_pwtype pw_type);
272 
273 /* IOCTL helper for IP encap modules. */
274 int l2tp_ioctl(struct sock *sk, int cmd, unsigned long arg);
275 
276 /* Extract the tunnel structure from a socket's sk_user_data pointer,
277  * validating the tunnel magic feather.
278  */
279 struct l2tp_tunnel *l2tp_sk_to_tunnel(struct sock *sk);
280 
l2tp_get_l2specific_len(struct l2tp_session * session)281 static inline int l2tp_get_l2specific_len(struct l2tp_session *session)
282 {
283 	switch (session->l2specific_type) {
284 	case L2TP_L2SPECTYPE_DEFAULT:
285 		return 4;
286 	case L2TP_L2SPECTYPE_NONE:
287 	default:
288 		return 0;
289 	}
290 }
291 
l2tp_tunnel_dst_mtu(const struct l2tp_tunnel * tunnel)292 static inline u32 l2tp_tunnel_dst_mtu(const struct l2tp_tunnel *tunnel)
293 {
294 	struct dst_entry *dst;
295 	u32 mtu;
296 
297 	dst = sk_dst_get(tunnel->sock);
298 	if (!dst)
299 		return 0;
300 
301 	mtu = dst_mtu(dst);
302 	dst_release(dst);
303 
304 	return mtu;
305 }
306 
307 #ifdef CONFIG_XFRM
l2tp_tunnel_uses_xfrm(const struct l2tp_tunnel * tunnel)308 static inline bool l2tp_tunnel_uses_xfrm(const struct l2tp_tunnel *tunnel)
309 {
310 	struct sock *sk = tunnel->sock;
311 
312 	return sk && (rcu_access_pointer(sk->sk_policy[0]) ||
313 		      rcu_access_pointer(sk->sk_policy[1]));
314 }
315 #else
l2tp_tunnel_uses_xfrm(const struct l2tp_tunnel * tunnel)316 static inline bool l2tp_tunnel_uses_xfrm(const struct l2tp_tunnel *tunnel)
317 {
318 	return false;
319 }
320 #endif
321 
l2tp_v3_ensure_opt_in_linear(struct l2tp_session * session,struct sk_buff * skb,unsigned char ** ptr,unsigned char ** optr)322 static inline int l2tp_v3_ensure_opt_in_linear(struct l2tp_session *session, struct sk_buff *skb,
323 					       unsigned char **ptr, unsigned char **optr)
324 {
325 	int opt_len = session->peer_cookie_len + l2tp_get_l2specific_len(session);
326 
327 	if (opt_len > 0) {
328 		int off = *ptr - *optr;
329 
330 		if (!pskb_may_pull(skb, off + opt_len))
331 			return -1;
332 
333 		if (skb->data != *optr) {
334 			*optr = skb->data;
335 			*ptr = skb->data + off;
336 		}
337 	}
338 
339 	return 0;
340 }
341 
342 #define MODULE_ALIAS_L2TP_PWTYPE(type) \
343 	MODULE_ALIAS("net-l2tp-type-" __stringify(type))
344 
345 #endif /* _L2TP_CORE_H_ */
346