xref: /openbmc/linux/net/l2tp/l2tp_core.h (revision dbf82f3f)
1d2912cb1SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
220dcb110STom Parkin /* L2TP internal definitions.
3fd558d18SJames Chapman  *
4fd558d18SJames Chapman  * Copyright (c) 2008,2009 Katalix Systems Ltd
5fd558d18SJames Chapman  */
6fbea9e07SReshetova, Elena #include <linux/refcount.h>
7fd558d18SJames Chapman 
8fd558d18SJames Chapman #ifndef _L2TP_CORE_H_
9fd558d18SJames Chapman #define _L2TP_CORE_H_
10fd558d18SJames Chapman 
111f5cd2a0SGuillaume Nault #include <net/dst.h>
121f5cd2a0SGuillaume Nault #include <net/sock.h>
131f5cd2a0SGuillaume Nault 
14d6a61ec9SGuillaume Nault #ifdef CONFIG_XFRM
15d6a61ec9SGuillaume Nault #include <net/xfrm.h>
16d6a61ec9SGuillaume Nault #endif
17d6a61ec9SGuillaume Nault 
18fd558d18SJames Chapman /* Just some random numbers */
19fd558d18SJames Chapman #define L2TP_TUNNEL_MAGIC	0x42114DDA
20fd558d18SJames Chapman #define L2TP_SESSION_MAGIC	0x0C04EB7D
21fd558d18SJames Chapman 
22f7faffa3SJames Chapman /* Per tunnel, session hash table size */
23fd558d18SJames Chapman #define L2TP_HASH_BITS	4
24dbf82f3fSTom Parkin #define L2TP_HASH_SIZE	BIT(L2TP_HASH_BITS)
25fd558d18SJames Chapman 
26f7faffa3SJames Chapman /* System-wide, session hash table size */
27f7faffa3SJames Chapman #define L2TP_HASH_BITS_2	8
28dbf82f3fSTom Parkin #define L2TP_HASH_SIZE_2	BIT(L2TP_HASH_BITS_2)
29f7faffa3SJames Chapman 
30fd558d18SJames Chapman struct sk_buff;
31fd558d18SJames Chapman 
32fd558d18SJames Chapman struct l2tp_stats {
337b7c0719STom Parkin 	atomic_long_t		tx_packets;
347b7c0719STom Parkin 	atomic_long_t		tx_bytes;
357b7c0719STom Parkin 	atomic_long_t		tx_errors;
367b7c0719STom Parkin 	atomic_long_t		rx_packets;
377b7c0719STom Parkin 	atomic_long_t		rx_bytes;
387b7c0719STom Parkin 	atomic_long_t		rx_seq_discards;
397b7c0719STom Parkin 	atomic_long_t		rx_oos_packets;
407b7c0719STom Parkin 	atomic_long_t		rx_errors;
417b7c0719STom Parkin 	atomic_long_t		rx_cookie_discards;
42fd558d18SJames Chapman };
43fd558d18SJames Chapman 
44fd558d18SJames Chapman struct l2tp_tunnel;
45fd558d18SJames Chapman 
46fd558d18SJames Chapman /* Describes a session. Contains information to determine incoming
47fd558d18SJames Chapman  * packets and transmit outgoing ones.
48fd558d18SJames Chapman  */
49fd558d18SJames Chapman struct l2tp_session_cfg {
50f7faffa3SJames Chapman 	enum l2tp_pwtype	pw_type;
5120dcb110STom Parkin 	unsigned int		recv_seq:1;	/* expect receive packets with sequence numbers? */
5220dcb110STom Parkin 	unsigned int		send_seq:1;	/* send packets with sequence numbers? */
5320dcb110STom Parkin 	unsigned int		lns_mode:1;	/* behave as LNS?
5420dcb110STom Parkin 						 * LAC enables sequence numbers under LNS control.
5520dcb110STom Parkin 						 */
5620dcb110STom Parkin 	int			debug;		/* bitmask of debug message categories */
57f7faffa3SJames Chapman 	u16			l2specific_type; /* Layer 2 specific type */
58f7faffa3SJames Chapman 	u8			cookie[8];	/* optional cookie */
59f7faffa3SJames Chapman 	int			cookie_len;	/* 0, 4 or 8 bytes */
60f7faffa3SJames Chapman 	u8			peer_cookie[8];	/* peer's cookie */
61f7faffa3SJames Chapman 	int			peer_cookie_len; /* 0, 4 or 8 bytes */
6220dcb110STom Parkin 	int			reorder_timeout; /* configured reorder timeout (in jiffies) */
63309795f4SJames Chapman 	char			*ifname;
64fd558d18SJames Chapman };
65fd558d18SJames Chapman 
66fd558d18SJames Chapman struct l2tp_session {
6720dcb110STom Parkin 	int			magic;		/* should be L2TP_SESSION_MAGIC */
68b228a940SGuillaume Nault 	long			dead;
69fd558d18SJames Chapman 
7020dcb110STom Parkin 	struct l2tp_tunnel	*tunnel;	/* back pointer to tunnel context */
71fd558d18SJames Chapman 	u32			session_id;
72fd558d18SJames Chapman 	u32			peer_session_id;
73f7faffa3SJames Chapman 	u8			cookie[8];
74f7faffa3SJames Chapman 	int			cookie_len;
75f7faffa3SJames Chapman 	u8			peer_cookie[8];
76f7faffa3SJames Chapman 	int			peer_cookie_len;
77f7faffa3SJames Chapman 	u16			l2specific_type;
78f7faffa3SJames Chapman 	u16			hdr_len;
79f7faffa3SJames Chapman 	u32			nr;		/* session NR state (receive) */
80f7faffa3SJames Chapman 	u32			ns;		/* session NR state (send) */
81fd558d18SJames Chapman 	struct sk_buff_head	reorder_q;	/* receive reorder queue */
828a1631d5SJames Chapman 	u32			nr_max;		/* max NR. Depends on tunnel */
838a1631d5SJames Chapman 	u32			nr_window_size;	/* NR window size */
84a0dbd822SJames Chapman 	u32			nr_oos;		/* NR of last OOS packet */
8520dcb110STom Parkin 	int			nr_oos_count;	/* for OOS recovery */
86a0dbd822SJames Chapman 	int			nr_oos_count_max;
8720dcb110STom Parkin 	struct hlist_node	hlist;		/* hash list node */
88f00c854cSReshetova, Elena 	refcount_t		ref_count;
89fd558d18SJames Chapman 
90fd558d18SJames Chapman 	char			name[32];	/* for logging */
91309795f4SJames Chapman 	char			ifname[IFNAMSIZ];
9220dcb110STom Parkin 	unsigned int		recv_seq:1;	/* expect receive packets with sequence numbers? */
9320dcb110STom Parkin 	unsigned int		send_seq:1;	/* send packets with sequence numbers? */
9420dcb110STom Parkin 	unsigned int		lns_mode:1;	/* behave as LNS?
9520dcb110STom Parkin 						 * LAC enables sequence numbers under LNS control.
9620dcb110STom Parkin 						 */
9720dcb110STom Parkin 	int			debug;		/* bitmask of debug message categories */
9820dcb110STom Parkin 	int			reorder_timeout; /* configured reorder timeout (in jiffies) */
9938d40b3fSJames Chapman 	int			reorder_skip;	/* set if skip to next nr */
100f7faffa3SJames Chapman 	enum l2tp_pwtype	pwtype;
101fd558d18SJames Chapman 	struct l2tp_stats	stats;
10220dcb110STom Parkin 	struct hlist_node	global_hlist;	/* global hash list node */
103fd558d18SJames Chapman 
104f7faffa3SJames Chapman 	int (*build_header)(struct l2tp_session *session, void *buf);
105fd558d18SJames Chapman 	void (*recv_skb)(struct l2tp_session *session, struct sk_buff *skb, int data_len);
106fd558d18SJames Chapman 	void (*session_close)(struct l2tp_session *session);
1070ad66140SJames Chapman 	void (*show)(struct seq_file *m, void *priv);
108af71b090SGustavo A. R. Silva 	u8			priv[];		/* private data */
109fd558d18SJames Chapman };
110fd558d18SJames Chapman 
111fd558d18SJames Chapman /* Describes the tunnel. It contains info to track all the associated
112fd558d18SJames Chapman  * sessions so incoming packets can be sorted out
113fd558d18SJames Chapman  */
114fd558d18SJames Chapman struct l2tp_tunnel_cfg {
11520dcb110STom Parkin 	int			debug;		/* bitmask of debug message categories */
1160d76751fSJames Chapman 	enum l2tp_encap_type	encap;
117789a4a2cSJames Chapman 
118789a4a2cSJames Chapman 	/* Used only for kernel-created sockets */
119789a4a2cSJames Chapman 	struct in_addr		local_ip;
120789a4a2cSJames Chapman 	struct in_addr		peer_ip;
121f9bac8dfSChris Elston #if IS_ENABLED(CONFIG_IPV6)
122f9bac8dfSChris Elston 	struct in6_addr		*local_ip6;
123f9bac8dfSChris Elston 	struct in6_addr		*peer_ip6;
124f9bac8dfSChris Elston #endif
125789a4a2cSJames Chapman 	u16			local_udp_port;
126789a4a2cSJames Chapman 	u16			peer_udp_port;
1276b649feaSTom Herbert 	unsigned int		use_udp_checksums:1,
1286b649feaSTom Herbert 				udp6_zero_tx_checksums:1,
1296b649feaSTom Herbert 				udp6_zero_rx_checksums:1;
130fd558d18SJames Chapman };
131fd558d18SJames Chapman 
132fd558d18SJames Chapman struct l2tp_tunnel {
133fd558d18SJames Chapman 	int			magic;		/* Should be L2TP_TUNNEL_MAGIC */
13462b982eeSSabrina Dubroca 
13562b982eeSSabrina Dubroca 	unsigned long		dead;
13662b982eeSSabrina Dubroca 
13799469c32Sxeb@mail.ru 	struct rcu_head rcu;
138fd558d18SJames Chapman 	rwlock_t		hlist_lock;	/* protect session_hlist */
13920dcb110STom Parkin 	bool			acpt_newsess;	/* indicates whether this tunnel accepts
14020dcb110STom Parkin 						 * new sessions. Protected by hlist_lock.
141f3c66d4eSGuillaume Nault 						 */
142fd558d18SJames Chapman 	struct hlist_head	session_hlist[L2TP_HASH_SIZE];
14320dcb110STom Parkin 						/* hashed list of sessions, hashed by id */
144fd558d18SJames Chapman 	u32			tunnel_id;
145fd558d18SJames Chapman 	u32			peer_tunnel_id;
146fd558d18SJames Chapman 	int			version;	/* 2=>L2TPv2, 3=>L2TPv3 */
147fd558d18SJames Chapman 
148fd558d18SJames Chapman 	char			name[20];	/* for logging */
14920dcb110STom Parkin 	int			debug;		/* bitmask of debug message categories */
1500d76751fSJames Chapman 	enum l2tp_encap_type	encap;
151fd558d18SJames Chapman 	struct l2tp_stats	stats;
152fd558d18SJames Chapman 
15320dcb110STom Parkin 	struct list_head	list;		/* list node on per-namespace list of tunnels */
154fd558d18SJames Chapman 	struct net		*l2tp_net;	/* the net we belong to */
155fd558d18SJames Chapman 
156fbea9e07SReshetova, Elena 	refcount_t		ref_count;
157bef04d16STom Parkin 	void (*old_sk_destruct)(struct sock *sk);
15820dcb110STom Parkin 	struct sock		*sock;		/* parent socket */
15920dcb110STom Parkin 	int			fd;		/* parent fd, if tunnel socket was created
16020dcb110STom Parkin 						 * by userspace
16120dcb110STom Parkin 						 */
162fd558d18SJames Chapman 
163f8ccac0eSTom Parkin 	struct work_struct	del_work;
164fd558d18SJames Chapman };
165fd558d18SJames Chapman 
166309795f4SJames Chapman struct l2tp_nl_cmd_ops {
167f026bc29SGuillaume Nault 	int (*session_create)(struct net *net, struct l2tp_tunnel *tunnel,
168f026bc29SGuillaume Nault 			      u32 session_id, u32 peer_session_id,
169f026bc29SGuillaume Nault 			      struct l2tp_session_cfg *cfg);
170309795f4SJames Chapman 	int (*session_delete)(struct l2tp_session *session);
171309795f4SJames Chapman };
172309795f4SJames Chapman 
173fd558d18SJames Chapman static inline void *l2tp_session_priv(struct l2tp_session *session)
174fd558d18SJames Chapman {
175fd558d18SJames Chapman 	return &session->priv[0];
176fd558d18SJames Chapman }
177fd558d18SJames Chapman 
17854652eb1SGuillaume Nault struct l2tp_tunnel *l2tp_tunnel_get(const struct net *net, u32 tunnel_id);
1795846c131SGuillaume Nault struct l2tp_tunnel *l2tp_tunnel_get_nth(const struct net *net, int nth);
18001e28b92SGuillaume Nault struct l2tp_session *l2tp_tunnel_get_session(struct l2tp_tunnel *tunnel,
18101e28b92SGuillaume Nault 					     u32 session_id);
1825846c131SGuillaume Nault 
183d00fa9adSJames Chapman void l2tp_tunnel_free(struct l2tp_tunnel *tunnel);
18454652eb1SGuillaume Nault 
18501e28b92SGuillaume Nault struct l2tp_session *l2tp_session_get(const struct net *net, u32 session_id);
186a4346210SGuillaume Nault struct l2tp_session *l2tp_session_get_nth(struct l2tp_tunnel *tunnel, int nth);
1879aaef50cSGuillaume Nault struct l2tp_session *l2tp_session_get_by_ifname(const struct net *net,
188a4346210SGuillaume Nault 						const char *ifname);
189fd558d18SJames Chapman 
190c1b1203dSJoe Perches int l2tp_tunnel_create(struct net *net, int fd, int version, u32 tunnel_id,
191c1b1203dSJoe Perches 		       u32 peer_tunnel_id, struct l2tp_tunnel_cfg *cfg,
192c1b1203dSJoe Perches 		       struct l2tp_tunnel **tunnelp);
1936b9f3423SGuillaume Nault int l2tp_tunnel_register(struct l2tp_tunnel *tunnel, struct net *net,
1946b9f3423SGuillaume Nault 			 struct l2tp_tunnel_cfg *cfg);
1956b9f3423SGuillaume Nault 
19662b982eeSSabrina Dubroca void l2tp_tunnel_delete(struct l2tp_tunnel *tunnel);
197c1b1203dSJoe Perches struct l2tp_session *l2tp_session_create(int priv_size,
198c1b1203dSJoe Perches 					 struct l2tp_tunnel *tunnel,
199c1b1203dSJoe Perches 					 u32 session_id, u32 peer_session_id,
200c1b1203dSJoe Perches 					 struct l2tp_session_cfg *cfg);
2013953ae7bSGuillaume Nault int l2tp_session_register(struct l2tp_session *session,
2023953ae7bSGuillaume Nault 			  struct l2tp_tunnel *tunnel);
2033953ae7bSGuillaume Nault 
204c1b1203dSJoe Perches void __l2tp_session_unhash(struct l2tp_session *session);
205c1b1203dSJoe Perches int l2tp_session_delete(struct l2tp_session *session);
206c1b1203dSJoe Perches void l2tp_session_free(struct l2tp_session *session);
207c1b1203dSJoe Perches void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
208c1b1203dSJoe Perches 		      unsigned char *ptr, unsigned char *optr, u16 hdrflags,
2092b139e6bSGuillaume Nault 		      int length);
210c1b1203dSJoe Perches int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb);
211bb5016eaSGuillaume Nault void l2tp_session_set_header_len(struct l2tp_session *session, int version);
212fd558d18SJames Chapman 
213c1b1203dSJoe Perches int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb,
214c1b1203dSJoe Perches 		  int hdr_len);
215fd558d18SJames Chapman 
216c1b1203dSJoe Perches int l2tp_nl_register_ops(enum l2tp_pwtype pw_type,
217c1b1203dSJoe Perches 			 const struct l2tp_nl_cmd_ops *ops);
218c1b1203dSJoe Perches void l2tp_nl_unregister_ops(enum l2tp_pwtype pw_type);
21972fb96e7SEric Dumazet int l2tp_ioctl(struct sock *sk, int cmd, unsigned long arg);
220309795f4SJames Chapman 
22154652eb1SGuillaume Nault static inline void l2tp_tunnel_inc_refcount(struct l2tp_tunnel *tunnel)
22254652eb1SGuillaume Nault {
22354652eb1SGuillaume Nault 	refcount_inc(&tunnel->ref_count);
22454652eb1SGuillaume Nault }
22554652eb1SGuillaume Nault 
22654652eb1SGuillaume Nault static inline void l2tp_tunnel_dec_refcount(struct l2tp_tunnel *tunnel)
22754652eb1SGuillaume Nault {
22854652eb1SGuillaume Nault 	if (refcount_dec_and_test(&tunnel->ref_count))
229d00fa9adSJames Chapman 		l2tp_tunnel_free(tunnel);
23054652eb1SGuillaume Nault }
23154652eb1SGuillaume Nault 
232fd558d18SJames Chapman /* Session reference counts. Incremented when code obtains a reference
233fd558d18SJames Chapman  * to a session.
234fd558d18SJames Chapman  */
2359ff672baSGuillaume Nault static inline void l2tp_session_inc_refcount(struct l2tp_session *session)
236fd558d18SJames Chapman {
237f00c854cSReshetova, Elena 	refcount_inc(&session->ref_count);
238fd558d18SJames Chapman }
239fd558d18SJames Chapman 
2409ff672baSGuillaume Nault static inline void l2tp_session_dec_refcount(struct l2tp_session *session)
241fd558d18SJames Chapman {
242f00c854cSReshetova, Elena 	if (refcount_dec_and_test(&session->ref_count))
243fd558d18SJames Chapman 		l2tp_session_free(session);
244fd558d18SJames Chapman }
245fd558d18SJames Chapman 
24662e7b6a5SLorenzo Bianconi static inline int l2tp_get_l2specific_len(struct l2tp_session *session)
24762e7b6a5SLorenzo Bianconi {
24862e7b6a5SLorenzo Bianconi 	switch (session->l2specific_type) {
24962e7b6a5SLorenzo Bianconi 	case L2TP_L2SPECTYPE_DEFAULT:
25062e7b6a5SLorenzo Bianconi 		return 4;
25162e7b6a5SLorenzo Bianconi 	case L2TP_L2SPECTYPE_NONE:
25262e7b6a5SLorenzo Bianconi 	default:
25362e7b6a5SLorenzo Bianconi 		return 0;
25462e7b6a5SLorenzo Bianconi 	}
25562e7b6a5SLorenzo Bianconi }
25662e7b6a5SLorenzo Bianconi 
2571f5cd2a0SGuillaume Nault static inline u32 l2tp_tunnel_dst_mtu(const struct l2tp_tunnel *tunnel)
2581f5cd2a0SGuillaume Nault {
2591f5cd2a0SGuillaume Nault 	struct dst_entry *dst;
2601f5cd2a0SGuillaume Nault 	u32 mtu;
2611f5cd2a0SGuillaume Nault 
2621f5cd2a0SGuillaume Nault 	dst = sk_dst_get(tunnel->sock);
2631f5cd2a0SGuillaume Nault 	if (!dst)
2641f5cd2a0SGuillaume Nault 		return 0;
2651f5cd2a0SGuillaume Nault 
2661f5cd2a0SGuillaume Nault 	mtu = dst_mtu(dst);
2671f5cd2a0SGuillaume Nault 	dst_release(dst);
2681f5cd2a0SGuillaume Nault 
2691f5cd2a0SGuillaume Nault 	return mtu;
2701f5cd2a0SGuillaume Nault }
2711f5cd2a0SGuillaume Nault 
272d6a61ec9SGuillaume Nault #ifdef CONFIG_XFRM
273d6a61ec9SGuillaume Nault static inline bool l2tp_tunnel_uses_xfrm(const struct l2tp_tunnel *tunnel)
274d6a61ec9SGuillaume Nault {
275d6a61ec9SGuillaume Nault 	struct sock *sk = tunnel->sock;
276d6a61ec9SGuillaume Nault 
277d6a61ec9SGuillaume Nault 	return sk && (rcu_access_pointer(sk->sk_policy[0]) ||
278d6a61ec9SGuillaume Nault 		      rcu_access_pointer(sk->sk_policy[1]));
279d6a61ec9SGuillaume Nault }
280d6a61ec9SGuillaume Nault #else
281d6a61ec9SGuillaume Nault static inline bool l2tp_tunnel_uses_xfrm(const struct l2tp_tunnel *tunnel)
282d6a61ec9SGuillaume Nault {
283d6a61ec9SGuillaume Nault 	return false;
284d6a61ec9SGuillaume Nault }
285d6a61ec9SGuillaume Nault #endif
286d6a61ec9SGuillaume Nault 
2874522a70dSJacob Wen static inline int l2tp_v3_ensure_opt_in_linear(struct l2tp_session *session, struct sk_buff *skb,
2884522a70dSJacob Wen 					       unsigned char **ptr, unsigned char **optr)
2894522a70dSJacob Wen {
2904522a70dSJacob Wen 	int opt_len = session->peer_cookie_len + l2tp_get_l2specific_len(session);
2914522a70dSJacob Wen 
2924522a70dSJacob Wen 	if (opt_len > 0) {
2934522a70dSJacob Wen 		int off = *ptr - *optr;
2944522a70dSJacob Wen 
2954522a70dSJacob Wen 		if (!pskb_may_pull(skb, off + opt_len))
2964522a70dSJacob Wen 			return -1;
2974522a70dSJacob Wen 
2984522a70dSJacob Wen 		if (skb->data != *optr) {
2994522a70dSJacob Wen 			*optr = skb->data;
3004522a70dSJacob Wen 			*ptr = skb->data + off;
3014522a70dSJacob Wen 		}
3024522a70dSJacob Wen 	}
3034522a70dSJacob Wen 
3044522a70dSJacob Wen 	return 0;
3054522a70dSJacob Wen }
3064522a70dSJacob Wen 
307a4ca44faSJoe Perches #define l2tp_printk(ptr, type, func, fmt, ...)				\
308a4ca44faSJoe Perches do {									\
309a4ca44faSJoe Perches 	if (((ptr)->debug) & (type))					\
310a4ca44faSJoe Perches 		func(fmt, ##__VA_ARGS__);				\
311a4ca44faSJoe Perches } while (0)
312a4ca44faSJoe Perches 
313a4ca44faSJoe Perches #define l2tp_warn(ptr, type, fmt, ...)					\
314a4ca44faSJoe Perches 	l2tp_printk(ptr, type, pr_warn, fmt, ##__VA_ARGS__)
315a4ca44faSJoe Perches #define l2tp_info(ptr, type, fmt, ...)					\
316a4ca44faSJoe Perches 	l2tp_printk(ptr, type, pr_info, fmt, ##__VA_ARGS__)
317a4ca44faSJoe Perches #define l2tp_dbg(ptr, type, fmt, ...)					\
318a4ca44faSJoe Perches 	l2tp_printk(ptr, type, pr_debug, fmt, ##__VA_ARGS__)
319a4ca44faSJoe Perches 
320f1f39f91Sstephen hemminger #define MODULE_ALIAS_L2TP_PWTYPE(type) \
321f1f39f91Sstephen hemminger 	MODULE_ALIAS("net-l2tp-type-" __stringify(type))
322f1f39f91Sstephen hemminger 
323fd558d18SJames Chapman #endif /* _L2TP_CORE_H_ */
324