xref: /openbmc/linux/net/l2tp/l2tp_core.h (revision bb5016ea)
1fd558d18SJames Chapman /*
2fd558d18SJames Chapman  * L2TP internal definitions.
3fd558d18SJames Chapman  *
4fd558d18SJames Chapman  * Copyright (c) 2008,2009 Katalix Systems Ltd
5fd558d18SJames Chapman  *
6fd558d18SJames Chapman  * This program is free software; you can redistribute it and/or modify
7fd558d18SJames Chapman  * it under the terms of the GNU General Public License version 2 as
8fd558d18SJames Chapman  * published by the Free Software Foundation.
9fd558d18SJames Chapman  */
10fd558d18SJames Chapman 
11fd558d18SJames Chapman #ifndef _L2TP_CORE_H_
12fd558d18SJames Chapman #define _L2TP_CORE_H_
13fd558d18SJames Chapman 
14fd558d18SJames Chapman /* Just some random numbers */
15fd558d18SJames Chapman #define L2TP_TUNNEL_MAGIC	0x42114DDA
16fd558d18SJames Chapman #define L2TP_SESSION_MAGIC	0x0C04EB7D
17fd558d18SJames Chapman 
18f7faffa3SJames Chapman /* Per tunnel, session hash table size */
19fd558d18SJames Chapman #define L2TP_HASH_BITS	4
20fd558d18SJames Chapman #define L2TP_HASH_SIZE	(1 << L2TP_HASH_BITS)
21fd558d18SJames Chapman 
22f7faffa3SJames Chapman /* System-wide, session hash table size */
23f7faffa3SJames Chapman #define L2TP_HASH_BITS_2	8
24f7faffa3SJames Chapman #define L2TP_HASH_SIZE_2	(1 << L2TP_HASH_BITS_2)
25f7faffa3SJames Chapman 
26fd558d18SJames Chapman /* Debug message categories for the DEBUG socket option */
27fd558d18SJames Chapman enum {
28fd558d18SJames Chapman 	L2TP_MSG_DEBUG		= (1 << 0),	/* verbose debug (if
29fd558d18SJames Chapman 						 * compiled in) */
30fd558d18SJames Chapman 	L2TP_MSG_CONTROL	= (1 << 1),	/* userspace - kernel
31fd558d18SJames Chapman 						 * interface */
32fd558d18SJames Chapman 	L2TP_MSG_SEQ		= (1 << 2),	/* sequence numbers */
33fd558d18SJames Chapman 	L2TP_MSG_DATA		= (1 << 3),	/* data packets */
34fd558d18SJames Chapman };
35fd558d18SJames Chapman 
36fd558d18SJames Chapman struct sk_buff;
37fd558d18SJames Chapman 
38fd558d18SJames Chapman struct l2tp_stats {
397b7c0719STom Parkin 	atomic_long_t		tx_packets;
407b7c0719STom Parkin 	atomic_long_t		tx_bytes;
417b7c0719STom Parkin 	atomic_long_t		tx_errors;
427b7c0719STom Parkin 	atomic_long_t		rx_packets;
437b7c0719STom Parkin 	atomic_long_t		rx_bytes;
447b7c0719STom Parkin 	atomic_long_t		rx_seq_discards;
457b7c0719STom Parkin 	atomic_long_t		rx_oos_packets;
467b7c0719STom Parkin 	atomic_long_t		rx_errors;
477b7c0719STom Parkin 	atomic_long_t		rx_cookie_discards;
48fd558d18SJames Chapman };
49fd558d18SJames Chapman 
50fd558d18SJames Chapman struct l2tp_tunnel;
51fd558d18SJames Chapman 
52fd558d18SJames Chapman /* Describes a session. Contains information to determine incoming
53fd558d18SJames Chapman  * packets and transmit outgoing ones.
54fd558d18SJames Chapman  */
55fd558d18SJames Chapman struct l2tp_session_cfg {
56f7faffa3SJames Chapman 	enum l2tp_pwtype	pw_type;
5795c96174SEric Dumazet 	unsigned int		data_seq:2;	/* data sequencing level
58fd558d18SJames Chapman 						 * 0 => none, 1 => IP only,
59fd558d18SJames Chapman 						 * 2 => all
60fd558d18SJames Chapman 						 */
6195c96174SEric Dumazet 	unsigned int		recv_seq:1;	/* expect receive packets with
62fd558d18SJames Chapman 						 * sequence numbers? */
6395c96174SEric Dumazet 	unsigned int		send_seq:1;	/* send packets with sequence
64fd558d18SJames Chapman 						 * numbers? */
6595c96174SEric Dumazet 	unsigned int		lns_mode:1;	/* behave as LNS? LAC enables
66fd558d18SJames Chapman 						 * sequence numbers under
67fd558d18SJames Chapman 						 * control of LNS. */
68fd558d18SJames Chapman 	int			debug;		/* bitmask of debug message
69fd558d18SJames Chapman 						 * categories */
70309795f4SJames Chapman 	u16			vlan_id;	/* VLAN pseudowire only */
71f7faffa3SJames Chapman 	u16			offset;		/* offset to payload */
72f7faffa3SJames Chapman 	u16			l2specific_len;	/* Layer 2 specific length */
73f7faffa3SJames Chapman 	u16			l2specific_type; /* Layer 2 specific type */
74f7faffa3SJames Chapman 	u8			cookie[8];	/* optional cookie */
75f7faffa3SJames Chapman 	int			cookie_len;	/* 0, 4 or 8 bytes */
76f7faffa3SJames Chapman 	u8			peer_cookie[8];	/* peer's cookie */
77f7faffa3SJames Chapman 	int			peer_cookie_len; /* 0, 4 or 8 bytes */
78fd558d18SJames Chapman 	int			reorder_timeout; /* configured reorder timeout
79fd558d18SJames Chapman 						  * (in jiffies) */
80fd558d18SJames Chapman 	int			mtu;
81fd558d18SJames Chapman 	int			mru;
82309795f4SJames Chapman 	char			*ifname;
83fd558d18SJames Chapman };
84fd558d18SJames Chapman 
85fd558d18SJames Chapman struct l2tp_session {
86fd558d18SJames Chapman 	int			magic;		/* should be
87fd558d18SJames Chapman 						 * L2TP_SESSION_MAGIC */
88fd558d18SJames Chapman 
89fd558d18SJames Chapman 	struct l2tp_tunnel	*tunnel;	/* back pointer to tunnel
90fd558d18SJames Chapman 						 * context */
91fd558d18SJames Chapman 	u32			session_id;
92fd558d18SJames Chapman 	u32			peer_session_id;
93f7faffa3SJames Chapman 	u8			cookie[8];
94f7faffa3SJames Chapman 	int			cookie_len;
95f7faffa3SJames Chapman 	u8			peer_cookie[8];
96f7faffa3SJames Chapman 	int			peer_cookie_len;
97f7faffa3SJames Chapman 	u16			offset;		/* offset from end of L2TP header
98f7faffa3SJames Chapman 						   to beginning of data */
99f7faffa3SJames Chapman 	u16			l2specific_len;
100f7faffa3SJames Chapman 	u16			l2specific_type;
101f7faffa3SJames Chapman 	u16			hdr_len;
102f7faffa3SJames Chapman 	u32			nr;		/* session NR state (receive) */
103f7faffa3SJames Chapman 	u32			ns;		/* session NR state (send) */
104fd558d18SJames Chapman 	struct sk_buff_head	reorder_q;	/* receive reorder queue */
1058a1631d5SJames Chapman 	u32			nr_max;		/* max NR. Depends on tunnel */
1068a1631d5SJames Chapman 	u32			nr_window_size;	/* NR window size */
107a0dbd822SJames Chapman 	u32			nr_oos;		/* NR of last OOS packet */
108a0dbd822SJames Chapman 	int			nr_oos_count;	/* For OOS recovery */
109a0dbd822SJames Chapman 	int			nr_oos_count_max;
110fd558d18SJames Chapman 	struct hlist_node	hlist;		/* Hash list node */
111fd558d18SJames Chapman 	atomic_t		ref_count;
112fd558d18SJames Chapman 
113fd558d18SJames Chapman 	char			name[32];	/* for logging */
114309795f4SJames Chapman 	char			ifname[IFNAMSIZ];
11595c96174SEric Dumazet 	unsigned int		data_seq:2;	/* data sequencing level
116fd558d18SJames Chapman 						 * 0 => none, 1 => IP only,
117fd558d18SJames Chapman 						 * 2 => all
118fd558d18SJames Chapman 						 */
11995c96174SEric Dumazet 	unsigned int		recv_seq:1;	/* expect receive packets with
120fd558d18SJames Chapman 						 * sequence numbers? */
12195c96174SEric Dumazet 	unsigned int		send_seq:1;	/* send packets with sequence
122fd558d18SJames Chapman 						 * numbers? */
12395c96174SEric Dumazet 	unsigned int		lns_mode:1;	/* behave as LNS? LAC enables
124fd558d18SJames Chapman 						 * sequence numbers under
125fd558d18SJames Chapman 						 * control of LNS. */
126fd558d18SJames Chapman 	int			debug;		/* bitmask of debug message
127fd558d18SJames Chapman 						 * categories */
128fd558d18SJames Chapman 	int			reorder_timeout; /* configured reorder timeout
129fd558d18SJames Chapman 						  * (in jiffies) */
13038d40b3fSJames Chapman 	int			reorder_skip;	/* set if skip to next nr */
131fd558d18SJames Chapman 	int			mtu;
132fd558d18SJames Chapman 	int			mru;
133f7faffa3SJames Chapman 	enum l2tp_pwtype	pwtype;
134fd558d18SJames Chapman 	struct l2tp_stats	stats;
135f7faffa3SJames Chapman 	struct hlist_node	global_hlist;	/* Global hash list node */
136fd558d18SJames Chapman 
137f7faffa3SJames Chapman 	int (*build_header)(struct l2tp_session *session, void *buf);
138fd558d18SJames Chapman 	void (*recv_skb)(struct l2tp_session *session, struct sk_buff *skb, int data_len);
139fd558d18SJames Chapman 	void (*session_close)(struct l2tp_session *session);
140fd558d18SJames Chapman 	void (*ref)(struct l2tp_session *session);
141fd558d18SJames Chapman 	void (*deref)(struct l2tp_session *session);
142f66ef2d0SDavid S. Miller #if defined(CONFIG_L2TP_DEBUGFS) || defined(CONFIG_L2TP_DEBUGFS_MODULE)
1430ad66140SJames Chapman 	void (*show)(struct seq_file *m, void *priv);
1440ad66140SJames Chapman #endif
145fd558d18SJames Chapman 	uint8_t			priv[0];	/* private data */
146fd558d18SJames Chapman };
147fd558d18SJames Chapman 
148fd558d18SJames Chapman /* Describes the tunnel. It contains info to track all the associated
149fd558d18SJames Chapman  * sessions so incoming packets can be sorted out
150fd558d18SJames Chapman  */
151fd558d18SJames Chapman struct l2tp_tunnel_cfg {
152fd558d18SJames Chapman 	int			debug;		/* bitmask of debug message
153fd558d18SJames Chapman 						 * categories */
1540d76751fSJames Chapman 	enum l2tp_encap_type	encap;
155789a4a2cSJames Chapman 
156789a4a2cSJames Chapman 	/* Used only for kernel-created sockets */
157789a4a2cSJames Chapman 	struct in_addr		local_ip;
158789a4a2cSJames Chapman 	struct in_addr		peer_ip;
159f9bac8dfSChris Elston #if IS_ENABLED(CONFIG_IPV6)
160f9bac8dfSChris Elston 	struct in6_addr		*local_ip6;
161f9bac8dfSChris Elston 	struct in6_addr		*peer_ip6;
162f9bac8dfSChris Elston #endif
163789a4a2cSJames Chapman 	u16			local_udp_port;
164789a4a2cSJames Chapman 	u16			peer_udp_port;
1657bddd0dbSEric Dumazet 	unsigned int		use_udp_checksums:1;
166fd558d18SJames Chapman };
167fd558d18SJames Chapman 
168fd558d18SJames Chapman struct l2tp_tunnel {
169fd558d18SJames Chapman 	int			magic;		/* Should be L2TP_TUNNEL_MAGIC */
17099469c32Sxeb@mail.ru 	struct rcu_head rcu;
171fd558d18SJames Chapman 	rwlock_t		hlist_lock;	/* protect session_hlist */
172fd558d18SJames Chapman 	struct hlist_head	session_hlist[L2TP_HASH_SIZE];
173fd558d18SJames Chapman 						/* hashed list of sessions,
174fd558d18SJames Chapman 						 * hashed by id */
175fd558d18SJames Chapman 	u32			tunnel_id;
176fd558d18SJames Chapman 	u32			peer_tunnel_id;
177fd558d18SJames Chapman 	int			version;	/* 2=>L2TPv2, 3=>L2TPv3 */
178fd558d18SJames Chapman 
179fd558d18SJames Chapman 	char			name[20];	/* for logging */
180fd558d18SJames Chapman 	int			debug;		/* bitmask of debug message
181fd558d18SJames Chapman 						 * categories */
1820d76751fSJames Chapman 	enum l2tp_encap_type	encap;
183fd558d18SJames Chapman 	struct l2tp_stats	stats;
184fd558d18SJames Chapman 
185fd558d18SJames Chapman 	struct list_head	list;		/* Keep a list of all tunnels */
186fd558d18SJames Chapman 	struct net		*l2tp_net;	/* the net we belong to */
187fd558d18SJames Chapman 
188fd558d18SJames Chapman 	atomic_t		ref_count;
1890ad66140SJames Chapman #ifdef CONFIG_DEBUG_FS
1900ad66140SJames Chapman 	void (*show)(struct seq_file *m, void *arg);
1910ad66140SJames Chapman #endif
192fd558d18SJames Chapman 	int (*recv_payload_hook)(struct sk_buff *skb);
193fd558d18SJames Chapman 	void (*old_sk_destruct)(struct sock *);
194fd558d18SJames Chapman 	struct sock		*sock;		/* Parent socket */
19580d84ef3STom Parkin 	int			fd;		/* Parent fd, if tunnel socket
19680d84ef3STom Parkin 						 * was created by userspace */
197e18503f4SFrançois Cachereul #if IS_ENABLED(CONFIG_IPV6)
198e18503f4SFrançois Cachereul 	bool			v4mapped;
199e18503f4SFrançois Cachereul #endif
200fd558d18SJames Chapman 
201f8ccac0eSTom Parkin 	struct work_struct	del_work;
202f8ccac0eSTom Parkin 
203fd558d18SJames Chapman 	uint8_t			priv[0];	/* private data */
204fd558d18SJames Chapman };
205fd558d18SJames Chapman 
206309795f4SJames Chapman struct l2tp_nl_cmd_ops {
207309795f4SJames Chapman 	int (*session_create)(struct net *net, u32 tunnel_id, u32 session_id, u32 peer_session_id, struct l2tp_session_cfg *cfg);
208309795f4SJames Chapman 	int (*session_delete)(struct l2tp_session *session);
209309795f4SJames Chapman };
210309795f4SJames Chapman 
211fd558d18SJames Chapman static inline void *l2tp_tunnel_priv(struct l2tp_tunnel *tunnel)
212fd558d18SJames Chapman {
213fd558d18SJames Chapman 	return &tunnel->priv[0];
214fd558d18SJames Chapman }
215fd558d18SJames Chapman 
216fd558d18SJames Chapman static inline void *l2tp_session_priv(struct l2tp_session *session)
217fd558d18SJames Chapman {
218fd558d18SJames Chapman 	return &session->priv[0];
219fd558d18SJames Chapman }
220fd558d18SJames Chapman 
221fd558d18SJames Chapman static inline struct l2tp_tunnel *l2tp_sock_to_tunnel(struct sock *sk)
222fd558d18SJames Chapman {
223fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel;
224fd558d18SJames Chapman 
225fd558d18SJames Chapman 	if (sk == NULL)
226fd558d18SJames Chapman 		return NULL;
227fd558d18SJames Chapman 
228fd558d18SJames Chapman 	sock_hold(sk);
229fd558d18SJames Chapman 	tunnel = (struct l2tp_tunnel *)(sk->sk_user_data);
230fd558d18SJames Chapman 	if (tunnel == NULL) {
231fd558d18SJames Chapman 		sock_put(sk);
232fd558d18SJames Chapman 		goto out;
233fd558d18SJames Chapman 	}
234fd558d18SJames Chapman 
235fd558d18SJames Chapman 	BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
236fd558d18SJames Chapman 
237fd558d18SJames Chapman out:
238fd558d18SJames Chapman 	return tunnel;
239fd558d18SJames Chapman }
240fd558d18SJames Chapman 
241c1b1203dSJoe Perches struct l2tp_session *l2tp_session_find(struct net *net,
242c1b1203dSJoe Perches 				       struct l2tp_tunnel *tunnel,
243c1b1203dSJoe Perches 				       u32 session_id);
244c1b1203dSJoe Perches struct l2tp_session *l2tp_session_find_nth(struct l2tp_tunnel *tunnel, int nth);
245c1b1203dSJoe Perches struct l2tp_session *l2tp_session_find_by_ifname(struct net *net, char *ifname);
246c1b1203dSJoe Perches struct l2tp_tunnel *l2tp_tunnel_find(struct net *net, u32 tunnel_id);
247c1b1203dSJoe Perches struct l2tp_tunnel *l2tp_tunnel_find_nth(struct net *net, int nth);
248fd558d18SJames Chapman 
249c1b1203dSJoe Perches int l2tp_tunnel_create(struct net *net, int fd, int version, u32 tunnel_id,
250c1b1203dSJoe Perches 		       u32 peer_tunnel_id, struct l2tp_tunnel_cfg *cfg,
251c1b1203dSJoe Perches 		       struct l2tp_tunnel **tunnelp);
252c1b1203dSJoe Perches void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel);
253c1b1203dSJoe Perches int l2tp_tunnel_delete(struct l2tp_tunnel *tunnel);
254c1b1203dSJoe Perches struct l2tp_session *l2tp_session_create(int priv_size,
255c1b1203dSJoe Perches 					 struct l2tp_tunnel *tunnel,
256c1b1203dSJoe Perches 					 u32 session_id, u32 peer_session_id,
257c1b1203dSJoe Perches 					 struct l2tp_session_cfg *cfg);
258c1b1203dSJoe Perches void __l2tp_session_unhash(struct l2tp_session *session);
259c1b1203dSJoe Perches int l2tp_session_delete(struct l2tp_session *session);
260c1b1203dSJoe Perches void l2tp_session_free(struct l2tp_session *session);
261c1b1203dSJoe Perches void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
262c1b1203dSJoe Perches 		      unsigned char *ptr, unsigned char *optr, u16 hdrflags,
263c1b1203dSJoe Perches 		      int length, int (*payload_hook)(struct sk_buff *skb));
264c1b1203dSJoe Perches int l2tp_session_queue_purge(struct l2tp_session *session);
265c1b1203dSJoe Perches int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb);
266bb5016eaSGuillaume Nault void l2tp_session_set_header_len(struct l2tp_session *session, int version);
267fd558d18SJames Chapman 
268c1b1203dSJoe Perches int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb,
269c1b1203dSJoe Perches 		  int hdr_len);
270fd558d18SJames Chapman 
271c1b1203dSJoe Perches int l2tp_nl_register_ops(enum l2tp_pwtype pw_type,
272c1b1203dSJoe Perches 			 const struct l2tp_nl_cmd_ops *ops);
273c1b1203dSJoe Perches void l2tp_nl_unregister_ops(enum l2tp_pwtype pw_type);
274309795f4SJames Chapman 
275fd558d18SJames Chapman /* Session reference counts. Incremented when code obtains a reference
276fd558d18SJames Chapman  * to a session.
277fd558d18SJames Chapman  */
278fd558d18SJames Chapman static inline void l2tp_session_inc_refcount_1(struct l2tp_session *session)
279fd558d18SJames Chapman {
280fd558d18SJames Chapman 	atomic_inc(&session->ref_count);
281fd558d18SJames Chapman }
282fd558d18SJames Chapman 
283fd558d18SJames Chapman static inline void l2tp_session_dec_refcount_1(struct l2tp_session *session)
284fd558d18SJames Chapman {
285fd558d18SJames Chapman 	if (atomic_dec_and_test(&session->ref_count))
286fd558d18SJames Chapman 		l2tp_session_free(session);
287fd558d18SJames Chapman }
288fd558d18SJames Chapman 
289fd558d18SJames Chapman #ifdef L2TP_REFCNT_DEBUG
290a4ca44faSJoe Perches #define l2tp_session_inc_refcount(_s)					\
291a4ca44faSJoe Perches do {									\
292a4ca44faSJoe Perches 	pr_debug("l2tp_session_inc_refcount: %s:%d %s: cnt=%d\n",	\
293a4ca44faSJoe Perches 		 __func__, __LINE__, (_s)->name,			\
294a4ca44faSJoe Perches 		 atomic_read(&_s->ref_count));				\
295fd558d18SJames Chapman 	l2tp_session_inc_refcount_1(_s);				\
296fd558d18SJames Chapman } while (0)
297a4ca44faSJoe Perches #define l2tp_session_dec_refcount(_s)					\
298a4ca44faSJoe Perches do {									\
299a4ca44faSJoe Perches 	pr_debug("l2tp_session_dec_refcount: %s:%d %s: cnt=%d\n",	\
300a4ca44faSJoe Perches 		 __func__, __LINE__, (_s)->name,			\
301a4ca44faSJoe Perches 		 atomic_read(&_s->ref_count));				\
302fd558d18SJames Chapman 	l2tp_session_dec_refcount_1(_s);				\
303fd558d18SJames Chapman } while (0)
304fd558d18SJames Chapman #else
305fd558d18SJames Chapman #define l2tp_session_inc_refcount(s) l2tp_session_inc_refcount_1(s)
306fd558d18SJames Chapman #define l2tp_session_dec_refcount(s) l2tp_session_dec_refcount_1(s)
307fd558d18SJames Chapman #endif
308fd558d18SJames Chapman 
309a4ca44faSJoe Perches #define l2tp_printk(ptr, type, func, fmt, ...)				\
310a4ca44faSJoe Perches do {									\
311a4ca44faSJoe Perches 	if (((ptr)->debug) & (type))					\
312a4ca44faSJoe Perches 		func(fmt, ##__VA_ARGS__);				\
313a4ca44faSJoe Perches } while (0)
314a4ca44faSJoe Perches 
315a4ca44faSJoe Perches #define l2tp_warn(ptr, type, fmt, ...)					\
316a4ca44faSJoe Perches 	l2tp_printk(ptr, type, pr_warn, fmt, ##__VA_ARGS__)
317a4ca44faSJoe Perches #define l2tp_info(ptr, type, fmt, ...)					\
318a4ca44faSJoe Perches 	l2tp_printk(ptr, type, pr_info, fmt, ##__VA_ARGS__)
319a4ca44faSJoe Perches #define l2tp_dbg(ptr, type, fmt, ...)					\
320a4ca44faSJoe Perches 	l2tp_printk(ptr, type, pr_debug, fmt, ##__VA_ARGS__)
321a4ca44faSJoe Perches 
322fd558d18SJames Chapman #endif /* _L2TP_CORE_H_ */
323