xref: /openbmc/linux/net/l2tp/l2tp_core.h (revision af71b090)
1d2912cb1SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
2fd558d18SJames Chapman /*
3fd558d18SJames Chapman  * L2TP internal definitions.
4fd558d18SJames Chapman  *
5fd558d18SJames Chapman  * Copyright (c) 2008,2009 Katalix Systems Ltd
6fd558d18SJames Chapman  */
7fbea9e07SReshetova, Elena #include <linux/refcount.h>
8fd558d18SJames Chapman 
9fd558d18SJames Chapman #ifndef _L2TP_CORE_H_
10fd558d18SJames Chapman #define _L2TP_CORE_H_
11fd558d18SJames Chapman 
121f5cd2a0SGuillaume Nault #include <net/dst.h>
131f5cd2a0SGuillaume Nault #include <net/sock.h>
141f5cd2a0SGuillaume Nault 
15d6a61ec9SGuillaume Nault #ifdef CONFIG_XFRM
16d6a61ec9SGuillaume Nault #include <net/xfrm.h>
17d6a61ec9SGuillaume Nault #endif
18d6a61ec9SGuillaume Nault 
19fd558d18SJames Chapman /* Just some random numbers */
20fd558d18SJames Chapman #define L2TP_TUNNEL_MAGIC	0x42114DDA
21fd558d18SJames Chapman #define L2TP_SESSION_MAGIC	0x0C04EB7D
22fd558d18SJames Chapman 
23f7faffa3SJames Chapman /* Per tunnel, session hash table size */
24fd558d18SJames Chapman #define L2TP_HASH_BITS	4
25fd558d18SJames Chapman #define L2TP_HASH_SIZE	(1 << L2TP_HASH_BITS)
26fd558d18SJames Chapman 
27f7faffa3SJames Chapman /* System-wide, session hash table size */
28f7faffa3SJames Chapman #define L2TP_HASH_BITS_2	8
29f7faffa3SJames Chapman #define L2TP_HASH_SIZE_2	(1 << L2TP_HASH_BITS_2)
30f7faffa3SJames Chapman 
31fd558d18SJames Chapman struct sk_buff;
32fd558d18SJames Chapman 
33fd558d18SJames Chapman struct l2tp_stats {
347b7c0719STom Parkin 	atomic_long_t		tx_packets;
357b7c0719STom Parkin 	atomic_long_t		tx_bytes;
367b7c0719STom Parkin 	atomic_long_t		tx_errors;
377b7c0719STom Parkin 	atomic_long_t		rx_packets;
387b7c0719STom Parkin 	atomic_long_t		rx_bytes;
397b7c0719STom Parkin 	atomic_long_t		rx_seq_discards;
407b7c0719STom Parkin 	atomic_long_t		rx_oos_packets;
417b7c0719STom Parkin 	atomic_long_t		rx_errors;
427b7c0719STom Parkin 	atomic_long_t		rx_cookie_discards;
43fd558d18SJames Chapman };
44fd558d18SJames Chapman 
45fd558d18SJames Chapman struct l2tp_tunnel;
46fd558d18SJames Chapman 
47fd558d18SJames Chapman /* Describes a session. Contains information to determine incoming
48fd558d18SJames Chapman  * packets and transmit outgoing ones.
49fd558d18SJames Chapman  */
50fd558d18SJames Chapman struct l2tp_session_cfg {
51f7faffa3SJames Chapman 	enum l2tp_pwtype	pw_type;
5295c96174SEric Dumazet 	unsigned int		recv_seq:1;	/* expect receive packets with
53fd558d18SJames Chapman 						 * sequence numbers? */
5495c96174SEric Dumazet 	unsigned int		send_seq:1;	/* send packets with sequence
55fd558d18SJames Chapman 						 * numbers? */
5695c96174SEric Dumazet 	unsigned int		lns_mode:1;	/* behave as LNS? LAC enables
57fd558d18SJames Chapman 						 * sequence numbers under
58fd558d18SJames Chapman 						 * control of LNS. */
59fd558d18SJames Chapman 	int			debug;		/* bitmask of debug message
60fd558d18SJames Chapman 						 * categories */
61f7faffa3SJames Chapman 	u16			l2specific_type; /* Layer 2 specific type */
62f7faffa3SJames Chapman 	u8			cookie[8];	/* optional cookie */
63f7faffa3SJames Chapman 	int			cookie_len;	/* 0, 4 or 8 bytes */
64f7faffa3SJames Chapman 	u8			peer_cookie[8];	/* peer's cookie */
65f7faffa3SJames Chapman 	int			peer_cookie_len; /* 0, 4 or 8 bytes */
66fd558d18SJames Chapman 	int			reorder_timeout; /* configured reorder timeout
67fd558d18SJames Chapman 						  * (in jiffies) */
68309795f4SJames Chapman 	char			*ifname;
69fd558d18SJames Chapman };
70fd558d18SJames Chapman 
71fd558d18SJames Chapman struct l2tp_session {
72fd558d18SJames Chapman 	int			magic;		/* should be
73fd558d18SJames Chapman 						 * L2TP_SESSION_MAGIC */
74b228a940SGuillaume Nault 	long			dead;
75fd558d18SJames Chapman 
76fd558d18SJames Chapman 	struct l2tp_tunnel	*tunnel;	/* back pointer to tunnel
77fd558d18SJames Chapman 						 * context */
78fd558d18SJames Chapman 	u32			session_id;
79fd558d18SJames Chapman 	u32			peer_session_id;
80f7faffa3SJames Chapman 	u8			cookie[8];
81f7faffa3SJames Chapman 	int			cookie_len;
82f7faffa3SJames Chapman 	u8			peer_cookie[8];
83f7faffa3SJames Chapman 	int			peer_cookie_len;
84f7faffa3SJames Chapman 	u16			l2specific_type;
85f7faffa3SJames Chapman 	u16			hdr_len;
86f7faffa3SJames Chapman 	u32			nr;		/* session NR state (receive) */
87f7faffa3SJames Chapman 	u32			ns;		/* session NR state (send) */
88fd558d18SJames Chapman 	struct sk_buff_head	reorder_q;	/* receive reorder queue */
898a1631d5SJames Chapman 	u32			nr_max;		/* max NR. Depends on tunnel */
908a1631d5SJames Chapman 	u32			nr_window_size;	/* NR window size */
91a0dbd822SJames Chapman 	u32			nr_oos;		/* NR of last OOS packet */
92a0dbd822SJames Chapman 	int			nr_oos_count;	/* For OOS recovery */
93a0dbd822SJames Chapman 	int			nr_oos_count_max;
94fd558d18SJames Chapman 	struct hlist_node	hlist;		/* Hash list node */
95f00c854cSReshetova, Elena 	refcount_t		ref_count;
96fd558d18SJames Chapman 
97fd558d18SJames Chapman 	char			name[32];	/* for logging */
98309795f4SJames Chapman 	char			ifname[IFNAMSIZ];
9995c96174SEric Dumazet 	unsigned int		recv_seq:1;	/* expect receive packets with
100fd558d18SJames Chapman 						 * sequence numbers? */
10195c96174SEric Dumazet 	unsigned int		send_seq:1;	/* send packets with sequence
102fd558d18SJames Chapman 						 * numbers? */
10395c96174SEric Dumazet 	unsigned int		lns_mode:1;	/* behave as LNS? LAC enables
104fd558d18SJames Chapman 						 * sequence numbers under
105fd558d18SJames Chapman 						 * control of LNS. */
106fd558d18SJames Chapman 	int			debug;		/* bitmask of debug message
107fd558d18SJames Chapman 						 * categories */
108fd558d18SJames Chapman 	int			reorder_timeout; /* configured reorder timeout
109fd558d18SJames Chapman 						  * (in jiffies) */
11038d40b3fSJames Chapman 	int			reorder_skip;	/* set if skip to next nr */
111f7faffa3SJames Chapman 	enum l2tp_pwtype	pwtype;
112fd558d18SJames Chapman 	struct l2tp_stats	stats;
113f7faffa3SJames Chapman 	struct hlist_node	global_hlist;	/* Global hash list node */
114fd558d18SJames Chapman 
115f7faffa3SJames Chapman 	int (*build_header)(struct l2tp_session *session, void *buf);
116fd558d18SJames Chapman 	void (*recv_skb)(struct l2tp_session *session, struct sk_buff *skb, int data_len);
117fd558d18SJames Chapman 	void (*session_close)(struct l2tp_session *session);
1180ad66140SJames Chapman 	void (*show)(struct seq_file *m, void *priv);
119af71b090SGustavo A. R. Silva 	u8			priv[];	/* private data */
120fd558d18SJames Chapman };
121fd558d18SJames Chapman 
122fd558d18SJames Chapman /* Describes the tunnel. It contains info to track all the associated
123fd558d18SJames Chapman  * sessions so incoming packets can be sorted out
124fd558d18SJames Chapman  */
125fd558d18SJames Chapman struct l2tp_tunnel_cfg {
126fd558d18SJames Chapman 	int			debug;		/* bitmask of debug message
127fd558d18SJames Chapman 						 * categories */
1280d76751fSJames Chapman 	enum l2tp_encap_type	encap;
129789a4a2cSJames Chapman 
130789a4a2cSJames Chapman 	/* Used only for kernel-created sockets */
131789a4a2cSJames Chapman 	struct in_addr		local_ip;
132789a4a2cSJames Chapman 	struct in_addr		peer_ip;
133f9bac8dfSChris Elston #if IS_ENABLED(CONFIG_IPV6)
134f9bac8dfSChris Elston 	struct in6_addr		*local_ip6;
135f9bac8dfSChris Elston 	struct in6_addr		*peer_ip6;
136f9bac8dfSChris Elston #endif
137789a4a2cSJames Chapman 	u16			local_udp_port;
138789a4a2cSJames Chapman 	u16			peer_udp_port;
1396b649feaSTom Herbert 	unsigned int		use_udp_checksums:1,
1406b649feaSTom Herbert 				udp6_zero_tx_checksums:1,
1416b649feaSTom Herbert 				udp6_zero_rx_checksums:1;
142fd558d18SJames Chapman };
143fd558d18SJames Chapman 
144fd558d18SJames Chapman struct l2tp_tunnel {
145fd558d18SJames Chapman 	int			magic;		/* Should be L2TP_TUNNEL_MAGIC */
14662b982eeSSabrina Dubroca 
14762b982eeSSabrina Dubroca 	unsigned long		dead;
14862b982eeSSabrina Dubroca 
14999469c32Sxeb@mail.ru 	struct rcu_head rcu;
150fd558d18SJames Chapman 	rwlock_t		hlist_lock;	/* protect session_hlist */
151f3c66d4eSGuillaume Nault 	bool			acpt_newsess;	/* Indicates whether this
152f3c66d4eSGuillaume Nault 						 * tunnel accepts new sessions.
153f3c66d4eSGuillaume Nault 						 * Protected by hlist_lock.
154f3c66d4eSGuillaume Nault 						 */
155fd558d18SJames Chapman 	struct hlist_head	session_hlist[L2TP_HASH_SIZE];
156fd558d18SJames Chapman 						/* hashed list of sessions,
157fd558d18SJames Chapman 						 * hashed by id */
158fd558d18SJames Chapman 	u32			tunnel_id;
159fd558d18SJames Chapman 	u32			peer_tunnel_id;
160fd558d18SJames Chapman 	int			version;	/* 2=>L2TPv2, 3=>L2TPv3 */
161fd558d18SJames Chapman 
162fd558d18SJames Chapman 	char			name[20];	/* for logging */
163fd558d18SJames Chapman 	int			debug;		/* bitmask of debug message
164fd558d18SJames Chapman 						 * categories */
1650d76751fSJames Chapman 	enum l2tp_encap_type	encap;
166fd558d18SJames Chapman 	struct l2tp_stats	stats;
167fd558d18SJames Chapman 
168fd558d18SJames Chapman 	struct list_head	list;		/* Keep a list of all tunnels */
169fd558d18SJames Chapman 	struct net		*l2tp_net;	/* the net we belong to */
170fd558d18SJames Chapman 
171fbea9e07SReshetova, Elena 	refcount_t		ref_count;
172fd558d18SJames Chapman 	void (*old_sk_destruct)(struct sock *);
173fd558d18SJames Chapman 	struct sock		*sock;		/* Parent socket */
17480d84ef3STom Parkin 	int			fd;		/* Parent fd, if tunnel socket
17580d84ef3STom Parkin 						 * was created by userspace */
176fd558d18SJames Chapman 
177f8ccac0eSTom Parkin 	struct work_struct	del_work;
178fd558d18SJames Chapman };
179fd558d18SJames Chapman 
180309795f4SJames Chapman struct l2tp_nl_cmd_ops {
181f026bc29SGuillaume Nault 	int (*session_create)(struct net *net, struct l2tp_tunnel *tunnel,
182f026bc29SGuillaume Nault 			      u32 session_id, u32 peer_session_id,
183f026bc29SGuillaume Nault 			      struct l2tp_session_cfg *cfg);
184309795f4SJames Chapman 	int (*session_delete)(struct l2tp_session *session);
185309795f4SJames Chapman };
186309795f4SJames Chapman 
187fd558d18SJames Chapman static inline void *l2tp_session_priv(struct l2tp_session *session)
188fd558d18SJames Chapman {
189fd558d18SJames Chapman 	return &session->priv[0];
190fd558d18SJames Chapman }
191fd558d18SJames Chapman 
19254652eb1SGuillaume Nault struct l2tp_tunnel *l2tp_tunnel_get(const struct net *net, u32 tunnel_id);
1935846c131SGuillaume Nault struct l2tp_tunnel *l2tp_tunnel_get_nth(const struct net *net, int nth);
19401e28b92SGuillaume Nault struct l2tp_session *l2tp_tunnel_get_session(struct l2tp_tunnel *tunnel,
19501e28b92SGuillaume Nault 					     u32 session_id);
1965846c131SGuillaume Nault 
197d00fa9adSJames Chapman void l2tp_tunnel_free(struct l2tp_tunnel *tunnel);
19854652eb1SGuillaume Nault 
19901e28b92SGuillaume Nault struct l2tp_session *l2tp_session_get(const struct net *net, u32 session_id);
200a4346210SGuillaume Nault struct l2tp_session *l2tp_session_get_nth(struct l2tp_tunnel *tunnel, int nth);
2019aaef50cSGuillaume Nault struct l2tp_session *l2tp_session_get_by_ifname(const struct net *net,
202a4346210SGuillaume Nault 						const char *ifname);
203fd558d18SJames Chapman 
204c1b1203dSJoe Perches int l2tp_tunnel_create(struct net *net, int fd, int version, u32 tunnel_id,
205c1b1203dSJoe Perches 		       u32 peer_tunnel_id, struct l2tp_tunnel_cfg *cfg,
206c1b1203dSJoe Perches 		       struct l2tp_tunnel **tunnelp);
2076b9f3423SGuillaume Nault int l2tp_tunnel_register(struct l2tp_tunnel *tunnel, struct net *net,
2086b9f3423SGuillaume Nault 			 struct l2tp_tunnel_cfg *cfg);
2096b9f3423SGuillaume Nault 
21062b982eeSSabrina Dubroca void l2tp_tunnel_delete(struct l2tp_tunnel *tunnel);
211c1b1203dSJoe Perches struct l2tp_session *l2tp_session_create(int priv_size,
212c1b1203dSJoe Perches 					 struct l2tp_tunnel *tunnel,
213c1b1203dSJoe Perches 					 u32 session_id, u32 peer_session_id,
214c1b1203dSJoe Perches 					 struct l2tp_session_cfg *cfg);
2153953ae7bSGuillaume Nault int l2tp_session_register(struct l2tp_session *session,
2163953ae7bSGuillaume Nault 			  struct l2tp_tunnel *tunnel);
2173953ae7bSGuillaume Nault 
218c1b1203dSJoe Perches void __l2tp_session_unhash(struct l2tp_session *session);
219c1b1203dSJoe Perches int l2tp_session_delete(struct l2tp_session *session);
220c1b1203dSJoe Perches void l2tp_session_free(struct l2tp_session *session);
221c1b1203dSJoe Perches void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
222c1b1203dSJoe Perches 		      unsigned char *ptr, unsigned char *optr, u16 hdrflags,
2232b139e6bSGuillaume Nault 		      int length);
224c1b1203dSJoe Perches int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb);
225bb5016eaSGuillaume Nault void l2tp_session_set_header_len(struct l2tp_session *session, int version);
226fd558d18SJames Chapman 
227c1b1203dSJoe Perches int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb,
228c1b1203dSJoe Perches 		  int hdr_len);
229fd558d18SJames Chapman 
230c1b1203dSJoe Perches int l2tp_nl_register_ops(enum l2tp_pwtype pw_type,
231c1b1203dSJoe Perches 			 const struct l2tp_nl_cmd_ops *ops);
232c1b1203dSJoe Perches void l2tp_nl_unregister_ops(enum l2tp_pwtype pw_type);
23372fb96e7SEric Dumazet int l2tp_ioctl(struct sock *sk, int cmd, unsigned long arg);
234309795f4SJames Chapman 
23554652eb1SGuillaume Nault static inline void l2tp_tunnel_inc_refcount(struct l2tp_tunnel *tunnel)
23654652eb1SGuillaume Nault {
23754652eb1SGuillaume Nault 	refcount_inc(&tunnel->ref_count);
23854652eb1SGuillaume Nault }
23954652eb1SGuillaume Nault 
24054652eb1SGuillaume Nault static inline void l2tp_tunnel_dec_refcount(struct l2tp_tunnel *tunnel)
24154652eb1SGuillaume Nault {
24254652eb1SGuillaume Nault 	if (refcount_dec_and_test(&tunnel->ref_count))
243d00fa9adSJames Chapman 		l2tp_tunnel_free(tunnel);
24454652eb1SGuillaume Nault }
24554652eb1SGuillaume Nault 
246fd558d18SJames Chapman /* Session reference counts. Incremented when code obtains a reference
247fd558d18SJames Chapman  * to a session.
248fd558d18SJames Chapman  */
2499ff672baSGuillaume Nault static inline void l2tp_session_inc_refcount(struct l2tp_session *session)
250fd558d18SJames Chapman {
251f00c854cSReshetova, Elena 	refcount_inc(&session->ref_count);
252fd558d18SJames Chapman }
253fd558d18SJames Chapman 
2549ff672baSGuillaume Nault static inline void l2tp_session_dec_refcount(struct l2tp_session *session)
255fd558d18SJames Chapman {
256f00c854cSReshetova, Elena 	if (refcount_dec_and_test(&session->ref_count))
257fd558d18SJames Chapman 		l2tp_session_free(session);
258fd558d18SJames Chapman }
259fd558d18SJames Chapman 
26062e7b6a5SLorenzo Bianconi static inline int l2tp_get_l2specific_len(struct l2tp_session *session)
26162e7b6a5SLorenzo Bianconi {
26262e7b6a5SLorenzo Bianconi 	switch (session->l2specific_type) {
26362e7b6a5SLorenzo Bianconi 	case L2TP_L2SPECTYPE_DEFAULT:
26462e7b6a5SLorenzo Bianconi 		return 4;
26562e7b6a5SLorenzo Bianconi 	case L2TP_L2SPECTYPE_NONE:
26662e7b6a5SLorenzo Bianconi 	default:
26762e7b6a5SLorenzo Bianconi 		return 0;
26862e7b6a5SLorenzo Bianconi 	}
26962e7b6a5SLorenzo Bianconi }
27062e7b6a5SLorenzo Bianconi 
2711f5cd2a0SGuillaume Nault static inline u32 l2tp_tunnel_dst_mtu(const struct l2tp_tunnel *tunnel)
2721f5cd2a0SGuillaume Nault {
2731f5cd2a0SGuillaume Nault 	struct dst_entry *dst;
2741f5cd2a0SGuillaume Nault 	u32 mtu;
2751f5cd2a0SGuillaume Nault 
2761f5cd2a0SGuillaume Nault 	dst = sk_dst_get(tunnel->sock);
2771f5cd2a0SGuillaume Nault 	if (!dst)
2781f5cd2a0SGuillaume Nault 		return 0;
2791f5cd2a0SGuillaume Nault 
2801f5cd2a0SGuillaume Nault 	mtu = dst_mtu(dst);
2811f5cd2a0SGuillaume Nault 	dst_release(dst);
2821f5cd2a0SGuillaume Nault 
2831f5cd2a0SGuillaume Nault 	return mtu;
2841f5cd2a0SGuillaume Nault }
2851f5cd2a0SGuillaume Nault 
286d6a61ec9SGuillaume Nault #ifdef CONFIG_XFRM
287d6a61ec9SGuillaume Nault static inline bool l2tp_tunnel_uses_xfrm(const struct l2tp_tunnel *tunnel)
288d6a61ec9SGuillaume Nault {
289d6a61ec9SGuillaume Nault 	struct sock *sk = tunnel->sock;
290d6a61ec9SGuillaume Nault 
291d6a61ec9SGuillaume Nault 	return sk && (rcu_access_pointer(sk->sk_policy[0]) ||
292d6a61ec9SGuillaume Nault 		      rcu_access_pointer(sk->sk_policy[1]));
293d6a61ec9SGuillaume Nault }
294d6a61ec9SGuillaume Nault #else
295d6a61ec9SGuillaume Nault static inline bool l2tp_tunnel_uses_xfrm(const struct l2tp_tunnel *tunnel)
296d6a61ec9SGuillaume Nault {
297d6a61ec9SGuillaume Nault 	return false;
298d6a61ec9SGuillaume Nault }
299d6a61ec9SGuillaume Nault #endif
300d6a61ec9SGuillaume Nault 
3014522a70dSJacob Wen static inline int l2tp_v3_ensure_opt_in_linear(struct l2tp_session *session, struct sk_buff *skb,
3024522a70dSJacob Wen 					       unsigned char **ptr, unsigned char **optr)
3034522a70dSJacob Wen {
3044522a70dSJacob Wen 	int opt_len = session->peer_cookie_len + l2tp_get_l2specific_len(session);
3054522a70dSJacob Wen 
3064522a70dSJacob Wen 	if (opt_len > 0) {
3074522a70dSJacob Wen 		int off = *ptr - *optr;
3084522a70dSJacob Wen 
3094522a70dSJacob Wen 		if (!pskb_may_pull(skb, off + opt_len))
3104522a70dSJacob Wen 			return -1;
3114522a70dSJacob Wen 
3124522a70dSJacob Wen 		if (skb->data != *optr) {
3134522a70dSJacob Wen 			*optr = skb->data;
3144522a70dSJacob Wen 			*ptr = skb->data + off;
3154522a70dSJacob Wen 		}
3164522a70dSJacob Wen 	}
3174522a70dSJacob Wen 
3184522a70dSJacob Wen 	return 0;
3194522a70dSJacob Wen }
3204522a70dSJacob Wen 
321a4ca44faSJoe Perches #define l2tp_printk(ptr, type, func, fmt, ...)				\
322a4ca44faSJoe Perches do {									\
323a4ca44faSJoe Perches 	if (((ptr)->debug) & (type))					\
324a4ca44faSJoe Perches 		func(fmt, ##__VA_ARGS__);				\
325a4ca44faSJoe Perches } while (0)
326a4ca44faSJoe Perches 
327a4ca44faSJoe Perches #define l2tp_warn(ptr, type, fmt, ...)					\
328a4ca44faSJoe Perches 	l2tp_printk(ptr, type, pr_warn, fmt, ##__VA_ARGS__)
329a4ca44faSJoe Perches #define l2tp_info(ptr, type, fmt, ...)					\
330a4ca44faSJoe Perches 	l2tp_printk(ptr, type, pr_info, fmt, ##__VA_ARGS__)
331a4ca44faSJoe Perches #define l2tp_dbg(ptr, type, fmt, ...)					\
332a4ca44faSJoe Perches 	l2tp_printk(ptr, type, pr_debug, fmt, ##__VA_ARGS__)
333a4ca44faSJoe Perches 
334f1f39f91Sstephen hemminger #define MODULE_ALIAS_L2TP_PWTYPE(type) \
335f1f39f91Sstephen hemminger 	MODULE_ALIAS("net-l2tp-type-" __stringify(type))
336f1f39f91Sstephen hemminger 
337fd558d18SJames Chapman #endif /* _L2TP_CORE_H_ */
338