xref: /openbmc/linux/net/l2tp/l2tp_core.h (revision 62e7b6a5)
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  */
10fbea9e07SReshetova, Elena #include <linux/refcount.h>
11fd558d18SJames Chapman 
12fd558d18SJames Chapman #ifndef _L2TP_CORE_H_
13fd558d18SJames Chapman #define _L2TP_CORE_H_
14fd558d18SJames Chapman 
15fd558d18SJames Chapman /* Just some random numbers */
16fd558d18SJames Chapman #define L2TP_TUNNEL_MAGIC	0x42114DDA
17fd558d18SJames Chapman #define L2TP_SESSION_MAGIC	0x0C04EB7D
18fd558d18SJames Chapman 
19f7faffa3SJames Chapman /* Per tunnel, session hash table size */
20fd558d18SJames Chapman #define L2TP_HASH_BITS	4
21fd558d18SJames Chapman #define L2TP_HASH_SIZE	(1 << L2TP_HASH_BITS)
22fd558d18SJames Chapman 
23f7faffa3SJames Chapman /* System-wide, session hash table size */
24f7faffa3SJames Chapman #define L2TP_HASH_BITS_2	8
25f7faffa3SJames Chapman #define L2TP_HASH_SIZE_2	(1 << L2TP_HASH_BITS_2)
26f7faffa3SJames Chapman 
27fd558d18SJames Chapman struct sk_buff;
28fd558d18SJames Chapman 
29fd558d18SJames Chapman struct l2tp_stats {
307b7c0719STom Parkin 	atomic_long_t		tx_packets;
317b7c0719STom Parkin 	atomic_long_t		tx_bytes;
327b7c0719STom Parkin 	atomic_long_t		tx_errors;
337b7c0719STom Parkin 	atomic_long_t		rx_packets;
347b7c0719STom Parkin 	atomic_long_t		rx_bytes;
357b7c0719STom Parkin 	atomic_long_t		rx_seq_discards;
367b7c0719STom Parkin 	atomic_long_t		rx_oos_packets;
377b7c0719STom Parkin 	atomic_long_t		rx_errors;
387b7c0719STom Parkin 	atomic_long_t		rx_cookie_discards;
39fd558d18SJames Chapman };
40fd558d18SJames Chapman 
41fd558d18SJames Chapman struct l2tp_tunnel;
42fd558d18SJames Chapman 
43fd558d18SJames Chapman /* Describes a session. Contains information to determine incoming
44fd558d18SJames Chapman  * packets and transmit outgoing ones.
45fd558d18SJames Chapman  */
46fd558d18SJames Chapman struct l2tp_session_cfg {
47f7faffa3SJames Chapman 	enum l2tp_pwtype	pw_type;
4895c96174SEric Dumazet 	unsigned int		data_seq:2;	/* data sequencing level
49fd558d18SJames Chapman 						 * 0 => none, 1 => IP only,
50fd558d18SJames Chapman 						 * 2 => all
51fd558d18SJames Chapman 						 */
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 */
61309795f4SJames Chapman 	u16			vlan_id;	/* VLAN pseudowire only */
62f7faffa3SJames Chapman 	u16			l2specific_len;	/* Layer 2 specific length */
63f7faffa3SJames Chapman 	u16			l2specific_type; /* Layer 2 specific type */
64f7faffa3SJames Chapman 	u8			cookie[8];	/* optional cookie */
65f7faffa3SJames Chapman 	int			cookie_len;	/* 0, 4 or 8 bytes */
66f7faffa3SJames Chapman 	u8			peer_cookie[8];	/* peer's cookie */
67f7faffa3SJames Chapman 	int			peer_cookie_len; /* 0, 4 or 8 bytes */
68fd558d18SJames Chapman 	int			reorder_timeout; /* configured reorder timeout
69fd558d18SJames Chapman 						  * (in jiffies) */
70fd558d18SJames Chapman 	int			mtu;
71fd558d18SJames Chapman 	int			mru;
72309795f4SJames Chapman 	char			*ifname;
73fd558d18SJames Chapman };
74fd558d18SJames Chapman 
75fd558d18SJames Chapman struct l2tp_session {
76fd558d18SJames Chapman 	int			magic;		/* should be
77fd558d18SJames Chapman 						 * L2TP_SESSION_MAGIC */
78b228a940SGuillaume Nault 	long			dead;
79fd558d18SJames Chapman 
80fd558d18SJames Chapman 	struct l2tp_tunnel	*tunnel;	/* back pointer to tunnel
81fd558d18SJames Chapman 						 * context */
82fd558d18SJames Chapman 	u32			session_id;
83fd558d18SJames Chapman 	u32			peer_session_id;
84f7faffa3SJames Chapman 	u8			cookie[8];
85f7faffa3SJames Chapman 	int			cookie_len;
86f7faffa3SJames Chapman 	u8			peer_cookie[8];
87f7faffa3SJames Chapman 	int			peer_cookie_len;
88f7faffa3SJames Chapman 	u16			l2specific_len;
89f7faffa3SJames Chapman 	u16			l2specific_type;
90f7faffa3SJames Chapman 	u16			hdr_len;
91f7faffa3SJames Chapman 	u32			nr;		/* session NR state (receive) */
92f7faffa3SJames Chapman 	u32			ns;		/* session NR state (send) */
93fd558d18SJames Chapman 	struct sk_buff_head	reorder_q;	/* receive reorder queue */
948a1631d5SJames Chapman 	u32			nr_max;		/* max NR. Depends on tunnel */
958a1631d5SJames Chapman 	u32			nr_window_size;	/* NR window size */
96a0dbd822SJames Chapman 	u32			nr_oos;		/* NR of last OOS packet */
97a0dbd822SJames Chapman 	int			nr_oos_count;	/* For OOS recovery */
98a0dbd822SJames Chapman 	int			nr_oos_count_max;
99fd558d18SJames Chapman 	struct hlist_node	hlist;		/* Hash list node */
100f00c854cSReshetova, Elena 	refcount_t		ref_count;
101fd558d18SJames Chapman 
102fd558d18SJames Chapman 	char			name[32];	/* for logging */
103309795f4SJames Chapman 	char			ifname[IFNAMSIZ];
10495c96174SEric Dumazet 	unsigned int		data_seq:2;	/* data sequencing level
105fd558d18SJames Chapman 						 * 0 => none, 1 => IP only,
106fd558d18SJames Chapman 						 * 2 => all
107fd558d18SJames Chapman 						 */
10895c96174SEric Dumazet 	unsigned int		recv_seq:1;	/* expect receive packets with
109fd558d18SJames Chapman 						 * sequence numbers? */
11095c96174SEric Dumazet 	unsigned int		send_seq:1;	/* send packets with sequence
111fd558d18SJames Chapman 						 * numbers? */
11295c96174SEric Dumazet 	unsigned int		lns_mode:1;	/* behave as LNS? LAC enables
113fd558d18SJames Chapman 						 * sequence numbers under
114fd558d18SJames Chapman 						 * control of LNS. */
115fd558d18SJames Chapman 	int			debug;		/* bitmask of debug message
116fd558d18SJames Chapman 						 * categories */
117fd558d18SJames Chapman 	int			reorder_timeout; /* configured reorder timeout
118fd558d18SJames Chapman 						  * (in jiffies) */
11938d40b3fSJames Chapman 	int			reorder_skip;	/* set if skip to next nr */
120fd558d18SJames Chapman 	int			mtu;
121fd558d18SJames Chapman 	int			mru;
122f7faffa3SJames Chapman 	enum l2tp_pwtype	pwtype;
123fd558d18SJames Chapman 	struct l2tp_stats	stats;
124f7faffa3SJames Chapman 	struct hlist_node	global_hlist;	/* Global hash list node */
125fd558d18SJames Chapman 
126f7faffa3SJames Chapman 	int (*build_header)(struct l2tp_session *session, void *buf);
127fd558d18SJames Chapman 	void (*recv_skb)(struct l2tp_session *session, struct sk_buff *skb, int data_len);
128fd558d18SJames Chapman 	void (*session_close)(struct l2tp_session *session);
1299dd79945SJavier Martinez Canillas #if IS_ENABLED(CONFIG_L2TP_DEBUGFS)
1300ad66140SJames Chapman 	void (*show)(struct seq_file *m, void *priv);
1310ad66140SJames Chapman #endif
132fd558d18SJames Chapman 	uint8_t			priv[0];	/* private data */
133fd558d18SJames Chapman };
134fd558d18SJames Chapman 
135fd558d18SJames Chapman /* Describes the tunnel. It contains info to track all the associated
136fd558d18SJames Chapman  * sessions so incoming packets can be sorted out
137fd558d18SJames Chapman  */
138fd558d18SJames Chapman struct l2tp_tunnel_cfg {
139fd558d18SJames Chapman 	int			debug;		/* bitmask of debug message
140fd558d18SJames Chapman 						 * categories */
1410d76751fSJames Chapman 	enum l2tp_encap_type	encap;
142789a4a2cSJames Chapman 
143789a4a2cSJames Chapman 	/* Used only for kernel-created sockets */
144789a4a2cSJames Chapman 	struct in_addr		local_ip;
145789a4a2cSJames Chapman 	struct in_addr		peer_ip;
146f9bac8dfSChris Elston #if IS_ENABLED(CONFIG_IPV6)
147f9bac8dfSChris Elston 	struct in6_addr		*local_ip6;
148f9bac8dfSChris Elston 	struct in6_addr		*peer_ip6;
149f9bac8dfSChris Elston #endif
150789a4a2cSJames Chapman 	u16			local_udp_port;
151789a4a2cSJames Chapman 	u16			peer_udp_port;
1526b649feaSTom Herbert 	unsigned int		use_udp_checksums:1,
1536b649feaSTom Herbert 				udp6_zero_tx_checksums:1,
1546b649feaSTom Herbert 				udp6_zero_rx_checksums:1;
155fd558d18SJames Chapman };
156fd558d18SJames Chapman 
157fd558d18SJames Chapman struct l2tp_tunnel {
158fd558d18SJames Chapman 	int			magic;		/* Should be L2TP_TUNNEL_MAGIC */
15962b982eeSSabrina Dubroca 
16062b982eeSSabrina Dubroca 	unsigned long		dead;
16162b982eeSSabrina Dubroca 
16299469c32Sxeb@mail.ru 	struct rcu_head rcu;
163fd558d18SJames Chapman 	rwlock_t		hlist_lock;	/* protect session_hlist */
164f3c66d4eSGuillaume Nault 	bool			acpt_newsess;	/* Indicates whether this
165f3c66d4eSGuillaume Nault 						 * tunnel accepts new sessions.
166f3c66d4eSGuillaume Nault 						 * Protected by hlist_lock.
167f3c66d4eSGuillaume Nault 						 */
168fd558d18SJames Chapman 	struct hlist_head	session_hlist[L2TP_HASH_SIZE];
169fd558d18SJames Chapman 						/* hashed list of sessions,
170fd558d18SJames Chapman 						 * hashed by id */
171fd558d18SJames Chapman 	u32			tunnel_id;
172fd558d18SJames Chapman 	u32			peer_tunnel_id;
173fd558d18SJames Chapman 	int			version;	/* 2=>L2TPv2, 3=>L2TPv3 */
174fd558d18SJames Chapman 
175fd558d18SJames Chapman 	char			name[20];	/* for logging */
176fd558d18SJames Chapman 	int			debug;		/* bitmask of debug message
177fd558d18SJames Chapman 						 * categories */
1780d76751fSJames Chapman 	enum l2tp_encap_type	encap;
179fd558d18SJames Chapman 	struct l2tp_stats	stats;
180fd558d18SJames Chapman 
181fd558d18SJames Chapman 	struct list_head	list;		/* Keep a list of all tunnels */
182fd558d18SJames Chapman 	struct net		*l2tp_net;	/* the net we belong to */
183fd558d18SJames Chapman 
184fbea9e07SReshetova, Elena 	refcount_t		ref_count;
1850ad66140SJames Chapman #ifdef CONFIG_DEBUG_FS
1860ad66140SJames Chapman 	void (*show)(struct seq_file *m, void *arg);
1870ad66140SJames Chapman #endif
188fd558d18SJames Chapman 	int (*recv_payload_hook)(struct sk_buff *skb);
189fd558d18SJames Chapman 	void (*old_sk_destruct)(struct sock *);
190fd558d18SJames Chapman 	struct sock		*sock;		/* Parent socket */
19180d84ef3STom Parkin 	int			fd;		/* Parent fd, if tunnel socket
19280d84ef3STom Parkin 						 * was created by userspace */
193e18503f4SFrançois Cachereul #if IS_ENABLED(CONFIG_IPV6)
194e18503f4SFrançois Cachereul 	bool			v4mapped;
195e18503f4SFrançois Cachereul #endif
196fd558d18SJames Chapman 
197f8ccac0eSTom Parkin 	struct work_struct	del_work;
198f8ccac0eSTom Parkin 
199fd558d18SJames Chapman 	uint8_t			priv[0];	/* private data */
200fd558d18SJames Chapman };
201fd558d18SJames Chapman 
202309795f4SJames Chapman struct l2tp_nl_cmd_ops {
203f026bc29SGuillaume Nault 	int (*session_create)(struct net *net, struct l2tp_tunnel *tunnel,
204f026bc29SGuillaume Nault 			      u32 session_id, u32 peer_session_id,
205f026bc29SGuillaume Nault 			      struct l2tp_session_cfg *cfg);
206309795f4SJames Chapman 	int (*session_delete)(struct l2tp_session *session);
207309795f4SJames Chapman };
208309795f4SJames Chapman 
209fd558d18SJames Chapman static inline void *l2tp_tunnel_priv(struct l2tp_tunnel *tunnel)
210fd558d18SJames Chapman {
211fd558d18SJames Chapman 	return &tunnel->priv[0];
212fd558d18SJames Chapman }
213fd558d18SJames Chapman 
214fd558d18SJames Chapman static inline void *l2tp_session_priv(struct l2tp_session *session)
215fd558d18SJames Chapman {
216fd558d18SJames Chapman 	return &session->priv[0];
217fd558d18SJames Chapman }
218fd558d18SJames Chapman 
219fd558d18SJames Chapman static inline struct l2tp_tunnel *l2tp_sock_to_tunnel(struct sock *sk)
220fd558d18SJames Chapman {
221fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel;
222fd558d18SJames Chapman 
223fd558d18SJames Chapman 	if (sk == NULL)
224fd558d18SJames Chapman 		return NULL;
225fd558d18SJames Chapman 
226fd558d18SJames Chapman 	sock_hold(sk);
227fd558d18SJames Chapman 	tunnel = (struct l2tp_tunnel *)(sk->sk_user_data);
228fd558d18SJames Chapman 	if (tunnel == NULL) {
229fd558d18SJames Chapman 		sock_put(sk);
230fd558d18SJames Chapman 		goto out;
231fd558d18SJames Chapman 	}
232fd558d18SJames Chapman 
233fd558d18SJames Chapman 	BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
234fd558d18SJames Chapman 
235fd558d18SJames Chapman out:
236fd558d18SJames Chapman 	return tunnel;
237fd558d18SJames Chapman }
238fd558d18SJames Chapman 
23954652eb1SGuillaume Nault struct l2tp_tunnel *l2tp_tunnel_get(const struct net *net, u32 tunnel_id);
24054652eb1SGuillaume Nault 
2419aaef50cSGuillaume Nault struct l2tp_session *l2tp_session_get(const struct net *net,
24261b9a047SGuillaume Nault 				      struct l2tp_tunnel *tunnel,
243a4346210SGuillaume Nault 				      u32 session_id);
244a4346210SGuillaume Nault struct l2tp_session *l2tp_session_get_nth(struct l2tp_tunnel *tunnel, int nth);
2459aaef50cSGuillaume Nault struct l2tp_session *l2tp_session_get_by_ifname(const struct net *net,
246a4346210SGuillaume Nault 						const char *ifname);
2472f858b92SGuillaume Nault struct l2tp_tunnel *l2tp_tunnel_find(const struct net *net, u32 tunnel_id);
2482f858b92SGuillaume Nault struct l2tp_tunnel *l2tp_tunnel_find_nth(const struct net *net, int nth);
249fd558d18SJames Chapman 
250c1b1203dSJoe Perches int l2tp_tunnel_create(struct net *net, int fd, int version, u32 tunnel_id,
251c1b1203dSJoe Perches 		       u32 peer_tunnel_id, struct l2tp_tunnel_cfg *cfg,
252c1b1203dSJoe Perches 		       struct l2tp_tunnel **tunnelp);
253c1b1203dSJoe Perches void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel);
25462b982eeSSabrina Dubroca void l2tp_tunnel_delete(struct l2tp_tunnel *tunnel);
255c1b1203dSJoe Perches struct l2tp_session *l2tp_session_create(int priv_size,
256c1b1203dSJoe Perches 					 struct l2tp_tunnel *tunnel,
257c1b1203dSJoe Perches 					 u32 session_id, u32 peer_session_id,
258c1b1203dSJoe Perches 					 struct l2tp_session_cfg *cfg);
2593953ae7bSGuillaume Nault int l2tp_session_register(struct l2tp_session *session,
2603953ae7bSGuillaume Nault 			  struct l2tp_tunnel *tunnel);
2613953ae7bSGuillaume Nault 
262c1b1203dSJoe Perches void __l2tp_session_unhash(struct l2tp_session *session);
263c1b1203dSJoe Perches int l2tp_session_delete(struct l2tp_session *session);
264c1b1203dSJoe Perches void l2tp_session_free(struct l2tp_session *session);
265c1b1203dSJoe Perches void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
266c1b1203dSJoe Perches 		      unsigned char *ptr, unsigned char *optr, u16 hdrflags,
267c1b1203dSJoe Perches 		      int length, int (*payload_hook)(struct sk_buff *skb));
268c1b1203dSJoe Perches int l2tp_session_queue_purge(struct l2tp_session *session);
269c1b1203dSJoe Perches int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb);
270bb5016eaSGuillaume Nault void l2tp_session_set_header_len(struct l2tp_session *session, int version);
271fd558d18SJames Chapman 
272c1b1203dSJoe Perches int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb,
273c1b1203dSJoe Perches 		  int hdr_len);
274fd558d18SJames Chapman 
275c1b1203dSJoe Perches int l2tp_nl_register_ops(enum l2tp_pwtype pw_type,
276c1b1203dSJoe Perches 			 const struct l2tp_nl_cmd_ops *ops);
277c1b1203dSJoe Perches void l2tp_nl_unregister_ops(enum l2tp_pwtype pw_type);
27872fb96e7SEric Dumazet int l2tp_ioctl(struct sock *sk, int cmd, unsigned long arg);
279309795f4SJames Chapman 
28054652eb1SGuillaume Nault static inline void l2tp_tunnel_inc_refcount(struct l2tp_tunnel *tunnel)
28154652eb1SGuillaume Nault {
28254652eb1SGuillaume Nault 	refcount_inc(&tunnel->ref_count);
28354652eb1SGuillaume Nault }
28454652eb1SGuillaume Nault 
28554652eb1SGuillaume Nault static inline void l2tp_tunnel_dec_refcount(struct l2tp_tunnel *tunnel)
28654652eb1SGuillaume Nault {
28754652eb1SGuillaume Nault 	if (refcount_dec_and_test(&tunnel->ref_count))
28854652eb1SGuillaume Nault 		kfree_rcu(tunnel, rcu);
28954652eb1SGuillaume Nault }
29054652eb1SGuillaume Nault 
291fd558d18SJames Chapman /* Session reference counts. Incremented when code obtains a reference
292fd558d18SJames Chapman  * to a session.
293fd558d18SJames Chapman  */
2949ff672baSGuillaume Nault static inline void l2tp_session_inc_refcount(struct l2tp_session *session)
295fd558d18SJames Chapman {
296f00c854cSReshetova, Elena 	refcount_inc(&session->ref_count);
297fd558d18SJames Chapman }
298fd558d18SJames Chapman 
2999ff672baSGuillaume Nault static inline void l2tp_session_dec_refcount(struct l2tp_session *session)
300fd558d18SJames Chapman {
301f00c854cSReshetova, Elena 	if (refcount_dec_and_test(&session->ref_count))
302fd558d18SJames Chapman 		l2tp_session_free(session);
303fd558d18SJames Chapman }
304fd558d18SJames Chapman 
30562e7b6a5SLorenzo Bianconi static inline int l2tp_get_l2specific_len(struct l2tp_session *session)
30662e7b6a5SLorenzo Bianconi {
30762e7b6a5SLorenzo Bianconi 	switch (session->l2specific_type) {
30862e7b6a5SLorenzo Bianconi 	case L2TP_L2SPECTYPE_DEFAULT:
30962e7b6a5SLorenzo Bianconi 		return 4;
31062e7b6a5SLorenzo Bianconi 	case L2TP_L2SPECTYPE_NONE:
31162e7b6a5SLorenzo Bianconi 	default:
31262e7b6a5SLorenzo Bianconi 		return 0;
31362e7b6a5SLorenzo Bianconi 	}
31462e7b6a5SLorenzo Bianconi }
31562e7b6a5SLorenzo Bianconi 
316a4ca44faSJoe Perches #define l2tp_printk(ptr, type, func, fmt, ...)				\
317a4ca44faSJoe Perches do {									\
318a4ca44faSJoe Perches 	if (((ptr)->debug) & (type))					\
319a4ca44faSJoe Perches 		func(fmt, ##__VA_ARGS__);				\
320a4ca44faSJoe Perches } while (0)
321a4ca44faSJoe Perches 
322a4ca44faSJoe Perches #define l2tp_warn(ptr, type, fmt, ...)					\
323a4ca44faSJoe Perches 	l2tp_printk(ptr, type, pr_warn, fmt, ##__VA_ARGS__)
324a4ca44faSJoe Perches #define l2tp_info(ptr, type, fmt, ...)					\
325a4ca44faSJoe Perches 	l2tp_printk(ptr, type, pr_info, fmt, ##__VA_ARGS__)
326a4ca44faSJoe Perches #define l2tp_dbg(ptr, type, fmt, ...)					\
327a4ca44faSJoe Perches 	l2tp_printk(ptr, type, pr_debug, fmt, ##__VA_ARGS__)
328a4ca44faSJoe Perches 
329f1f39f91Sstephen hemminger #define MODULE_ALIAS_L2TP_PWTYPE(type) \
330f1f39f91Sstephen hemminger 	MODULE_ALIAS("net-l2tp-type-" __stringify(type))
331f1f39f91Sstephen hemminger 
332fd558d18SJames Chapman #endif /* _L2TP_CORE_H_ */
333