xref: /openbmc/linux/net/l2tp/l2tp_core.h (revision 01e28b92)
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 
151f5cd2a0SGuillaume Nault #include <net/dst.h>
161f5cd2a0SGuillaume Nault #include <net/sock.h>
171f5cd2a0SGuillaume Nault 
18d6a61ec9SGuillaume Nault #ifdef CONFIG_XFRM
19d6a61ec9SGuillaume Nault #include <net/xfrm.h>
20d6a61ec9SGuillaume Nault #endif
21d6a61ec9SGuillaume Nault 
22fd558d18SJames Chapman /* Just some random numbers */
23fd558d18SJames Chapman #define L2TP_TUNNEL_MAGIC	0x42114DDA
24fd558d18SJames Chapman #define L2TP_SESSION_MAGIC	0x0C04EB7D
25fd558d18SJames Chapman 
26f7faffa3SJames Chapman /* Per tunnel, session hash table size */
27fd558d18SJames Chapman #define L2TP_HASH_BITS	4
28fd558d18SJames Chapman #define L2TP_HASH_SIZE	(1 << L2TP_HASH_BITS)
29fd558d18SJames Chapman 
30f7faffa3SJames Chapman /* System-wide, session hash table size */
31f7faffa3SJames Chapman #define L2TP_HASH_BITS_2	8
32f7faffa3SJames Chapman #define L2TP_HASH_SIZE_2	(1 << L2TP_HASH_BITS_2)
33f7faffa3SJames Chapman 
34fd558d18SJames Chapman struct sk_buff;
35fd558d18SJames Chapman 
36fd558d18SJames Chapman struct l2tp_stats {
377b7c0719STom Parkin 	atomic_long_t		tx_packets;
387b7c0719STom Parkin 	atomic_long_t		tx_bytes;
397b7c0719STom Parkin 	atomic_long_t		tx_errors;
407b7c0719STom Parkin 	atomic_long_t		rx_packets;
417b7c0719STom Parkin 	atomic_long_t		rx_bytes;
427b7c0719STom Parkin 	atomic_long_t		rx_seq_discards;
437b7c0719STom Parkin 	atomic_long_t		rx_oos_packets;
447b7c0719STom Parkin 	atomic_long_t		rx_errors;
457b7c0719STom Parkin 	atomic_long_t		rx_cookie_discards;
46fd558d18SJames Chapman };
47fd558d18SJames Chapman 
48fd558d18SJames Chapman struct l2tp_tunnel;
49fd558d18SJames Chapman 
50fd558d18SJames Chapman /* Describes a session. Contains information to determine incoming
51fd558d18SJames Chapman  * packets and transmit outgoing ones.
52fd558d18SJames Chapman  */
53fd558d18SJames Chapman struct l2tp_session_cfg {
54f7faffa3SJames Chapman 	enum l2tp_pwtype	pw_type;
5595c96174SEric Dumazet 	unsigned int		recv_seq:1;	/* expect receive packets with
56fd558d18SJames Chapman 						 * sequence numbers? */
5795c96174SEric Dumazet 	unsigned int		send_seq:1;	/* send packets with sequence
58fd558d18SJames Chapman 						 * numbers? */
5995c96174SEric Dumazet 	unsigned int		lns_mode:1;	/* behave as LNS? LAC enables
60fd558d18SJames Chapman 						 * sequence numbers under
61fd558d18SJames Chapman 						 * control of LNS. */
62fd558d18SJames Chapman 	int			debug;		/* bitmask of debug message
63fd558d18SJames Chapman 						 * categories */
64f7faffa3SJames Chapman 	u16			l2specific_type; /* Layer 2 specific type */
65f7faffa3SJames Chapman 	u8			cookie[8];	/* optional cookie */
66f7faffa3SJames Chapman 	int			cookie_len;	/* 0, 4 or 8 bytes */
67f7faffa3SJames Chapman 	u8			peer_cookie[8];	/* peer's cookie */
68f7faffa3SJames Chapman 	int			peer_cookie_len; /* 0, 4 or 8 bytes */
69fd558d18SJames Chapman 	int			reorder_timeout; /* configured reorder timeout
70fd558d18SJames Chapman 						  * (in jiffies) */
71309795f4SJames Chapman 	char			*ifname;
72fd558d18SJames Chapman };
73fd558d18SJames Chapman 
74fd558d18SJames Chapman struct l2tp_session {
75fd558d18SJames Chapman 	int			magic;		/* should be
76fd558d18SJames Chapman 						 * L2TP_SESSION_MAGIC */
77b228a940SGuillaume Nault 	long			dead;
78fd558d18SJames Chapman 
79fd558d18SJames Chapman 	struct l2tp_tunnel	*tunnel;	/* back pointer to tunnel
80fd558d18SJames Chapman 						 * context */
81fd558d18SJames Chapman 	u32			session_id;
82fd558d18SJames Chapman 	u32			peer_session_id;
83f7faffa3SJames Chapman 	u8			cookie[8];
84f7faffa3SJames Chapman 	int			cookie_len;
85f7faffa3SJames Chapman 	u8			peer_cookie[8];
86f7faffa3SJames Chapman 	int			peer_cookie_len;
87f7faffa3SJames Chapman 	u16			l2specific_type;
88f7faffa3SJames Chapman 	u16			hdr_len;
89f7faffa3SJames Chapman 	u32			nr;		/* session NR state (receive) */
90f7faffa3SJames Chapman 	u32			ns;		/* session NR state (send) */
91fd558d18SJames Chapman 	struct sk_buff_head	reorder_q;	/* receive reorder queue */
928a1631d5SJames Chapman 	u32			nr_max;		/* max NR. Depends on tunnel */
938a1631d5SJames Chapman 	u32			nr_window_size;	/* NR window size */
94a0dbd822SJames Chapman 	u32			nr_oos;		/* NR of last OOS packet */
95a0dbd822SJames Chapman 	int			nr_oos_count;	/* For OOS recovery */
96a0dbd822SJames Chapman 	int			nr_oos_count_max;
97fd558d18SJames Chapman 	struct hlist_node	hlist;		/* Hash list node */
98f00c854cSReshetova, Elena 	refcount_t		ref_count;
99fd558d18SJames Chapman 
100fd558d18SJames Chapman 	char			name[32];	/* for logging */
101309795f4SJames Chapman 	char			ifname[IFNAMSIZ];
10295c96174SEric Dumazet 	unsigned int		recv_seq:1;	/* expect receive packets with
103fd558d18SJames Chapman 						 * sequence numbers? */
10495c96174SEric Dumazet 	unsigned int		send_seq:1;	/* send packets with sequence
105fd558d18SJames Chapman 						 * numbers? */
10695c96174SEric Dumazet 	unsigned int		lns_mode:1;	/* behave as LNS? LAC enables
107fd558d18SJames Chapman 						 * sequence numbers under
108fd558d18SJames Chapman 						 * control of LNS. */
109fd558d18SJames Chapman 	int			debug;		/* bitmask of debug message
110fd558d18SJames Chapman 						 * categories */
111fd558d18SJames Chapman 	int			reorder_timeout; /* configured reorder timeout
112fd558d18SJames Chapman 						  * (in jiffies) */
11338d40b3fSJames Chapman 	int			reorder_skip;	/* set if skip to next nr */
114f7faffa3SJames Chapman 	enum l2tp_pwtype	pwtype;
115fd558d18SJames Chapman 	struct l2tp_stats	stats;
116f7faffa3SJames Chapman 	struct hlist_node	global_hlist;	/* Global hash list node */
117fd558d18SJames Chapman 
118f7faffa3SJames Chapman 	int (*build_header)(struct l2tp_session *session, void *buf);
119fd558d18SJames Chapman 	void (*recv_skb)(struct l2tp_session *session, struct sk_buff *skb, int data_len);
120fd558d18SJames Chapman 	void (*session_close)(struct l2tp_session *session);
1219dd79945SJavier Martinez Canillas #if IS_ENABLED(CONFIG_L2TP_DEBUGFS)
1220ad66140SJames Chapman 	void (*show)(struct seq_file *m, void *priv);
1230ad66140SJames Chapman #endif
124fd558d18SJames Chapman 	uint8_t			priv[0];	/* private data */
125fd558d18SJames Chapman };
126fd558d18SJames Chapman 
127fd558d18SJames Chapman /* Describes the tunnel. It contains info to track all the associated
128fd558d18SJames Chapman  * sessions so incoming packets can be sorted out
129fd558d18SJames Chapman  */
130fd558d18SJames Chapman struct l2tp_tunnel_cfg {
131fd558d18SJames Chapman 	int			debug;		/* bitmask of debug message
132fd558d18SJames Chapman 						 * categories */
1330d76751fSJames Chapman 	enum l2tp_encap_type	encap;
134789a4a2cSJames Chapman 
135789a4a2cSJames Chapman 	/* Used only for kernel-created sockets */
136789a4a2cSJames Chapman 	struct in_addr		local_ip;
137789a4a2cSJames Chapman 	struct in_addr		peer_ip;
138f9bac8dfSChris Elston #if IS_ENABLED(CONFIG_IPV6)
139f9bac8dfSChris Elston 	struct in6_addr		*local_ip6;
140f9bac8dfSChris Elston 	struct in6_addr		*peer_ip6;
141f9bac8dfSChris Elston #endif
142789a4a2cSJames Chapman 	u16			local_udp_port;
143789a4a2cSJames Chapman 	u16			peer_udp_port;
1446b649feaSTom Herbert 	unsigned int		use_udp_checksums:1,
1456b649feaSTom Herbert 				udp6_zero_tx_checksums:1,
1466b649feaSTom Herbert 				udp6_zero_rx_checksums:1;
147fd558d18SJames Chapman };
148fd558d18SJames Chapman 
149fd558d18SJames Chapman struct l2tp_tunnel {
150fd558d18SJames Chapman 	int			magic;		/* Should be L2TP_TUNNEL_MAGIC */
15162b982eeSSabrina Dubroca 
15262b982eeSSabrina Dubroca 	unsigned long		dead;
15362b982eeSSabrina Dubroca 
15499469c32Sxeb@mail.ru 	struct rcu_head rcu;
155fd558d18SJames Chapman 	rwlock_t		hlist_lock;	/* protect session_hlist */
156f3c66d4eSGuillaume Nault 	bool			acpt_newsess;	/* Indicates whether this
157f3c66d4eSGuillaume Nault 						 * tunnel accepts new sessions.
158f3c66d4eSGuillaume Nault 						 * Protected by hlist_lock.
159f3c66d4eSGuillaume Nault 						 */
160fd558d18SJames Chapman 	struct hlist_head	session_hlist[L2TP_HASH_SIZE];
161fd558d18SJames Chapman 						/* hashed list of sessions,
162fd558d18SJames Chapman 						 * hashed by id */
163fd558d18SJames Chapman 	u32			tunnel_id;
164fd558d18SJames Chapman 	u32			peer_tunnel_id;
165fd558d18SJames Chapman 	int			version;	/* 2=>L2TPv2, 3=>L2TPv3 */
166fd558d18SJames Chapman 
167fd558d18SJames Chapman 	char			name[20];	/* for logging */
168fd558d18SJames Chapman 	int			debug;		/* bitmask of debug message
169fd558d18SJames Chapman 						 * categories */
1700d76751fSJames Chapman 	enum l2tp_encap_type	encap;
171fd558d18SJames Chapman 	struct l2tp_stats	stats;
172fd558d18SJames Chapman 
173fd558d18SJames Chapman 	struct list_head	list;		/* Keep a list of all tunnels */
174fd558d18SJames Chapman 	struct net		*l2tp_net;	/* the net we belong to */
175fd558d18SJames Chapman 
176fbea9e07SReshetova, Elena 	refcount_t		ref_count;
177fd558d18SJames Chapman 	void (*old_sk_destruct)(struct sock *);
178fd558d18SJames Chapman 	struct sock		*sock;		/* Parent socket */
17980d84ef3STom Parkin 	int			fd;		/* Parent fd, if tunnel socket
18080d84ef3STom Parkin 						 * was created by userspace */
181fd558d18SJames Chapman 
182f8ccac0eSTom Parkin 	struct work_struct	del_work;
183fd558d18SJames Chapman };
184fd558d18SJames Chapman 
185309795f4SJames Chapman struct l2tp_nl_cmd_ops {
186f026bc29SGuillaume Nault 	int (*session_create)(struct net *net, struct l2tp_tunnel *tunnel,
187f026bc29SGuillaume Nault 			      u32 session_id, u32 peer_session_id,
188f026bc29SGuillaume Nault 			      struct l2tp_session_cfg *cfg);
189309795f4SJames Chapman 	int (*session_delete)(struct l2tp_session *session);
190309795f4SJames Chapman };
191309795f4SJames Chapman 
192fd558d18SJames Chapman static inline void *l2tp_session_priv(struct l2tp_session *session)
193fd558d18SJames Chapman {
194fd558d18SJames Chapman 	return &session->priv[0];
195fd558d18SJames Chapman }
196fd558d18SJames Chapman 
19754652eb1SGuillaume Nault struct l2tp_tunnel *l2tp_tunnel_get(const struct net *net, u32 tunnel_id);
1985846c131SGuillaume Nault struct l2tp_tunnel *l2tp_tunnel_get_nth(const struct net *net, int nth);
19901e28b92SGuillaume Nault struct l2tp_session *l2tp_tunnel_get_session(struct l2tp_tunnel *tunnel,
20001e28b92SGuillaume Nault 					     u32 session_id);
2015846c131SGuillaume Nault 
202d00fa9adSJames Chapman void l2tp_tunnel_free(struct l2tp_tunnel *tunnel);
20354652eb1SGuillaume Nault 
20401e28b92SGuillaume Nault struct l2tp_session *l2tp_session_get(const struct net *net, u32 session_id);
205a4346210SGuillaume Nault struct l2tp_session *l2tp_session_get_nth(struct l2tp_tunnel *tunnel, int nth);
2069aaef50cSGuillaume Nault struct l2tp_session *l2tp_session_get_by_ifname(const struct net *net,
207a4346210SGuillaume Nault 						const char *ifname);
208fd558d18SJames Chapman 
209c1b1203dSJoe Perches int l2tp_tunnel_create(struct net *net, int fd, int version, u32 tunnel_id,
210c1b1203dSJoe Perches 		       u32 peer_tunnel_id, struct l2tp_tunnel_cfg *cfg,
211c1b1203dSJoe Perches 		       struct l2tp_tunnel **tunnelp);
2126b9f3423SGuillaume Nault int l2tp_tunnel_register(struct l2tp_tunnel *tunnel, struct net *net,
2136b9f3423SGuillaume Nault 			 struct l2tp_tunnel_cfg *cfg);
2146b9f3423SGuillaume Nault 
21562b982eeSSabrina Dubroca void l2tp_tunnel_delete(struct l2tp_tunnel *tunnel);
216c1b1203dSJoe Perches struct l2tp_session *l2tp_session_create(int priv_size,
217c1b1203dSJoe Perches 					 struct l2tp_tunnel *tunnel,
218c1b1203dSJoe Perches 					 u32 session_id, u32 peer_session_id,
219c1b1203dSJoe Perches 					 struct l2tp_session_cfg *cfg);
2203953ae7bSGuillaume Nault int l2tp_session_register(struct l2tp_session *session,
2213953ae7bSGuillaume Nault 			  struct l2tp_tunnel *tunnel);
2223953ae7bSGuillaume Nault 
223c1b1203dSJoe Perches void __l2tp_session_unhash(struct l2tp_session *session);
224c1b1203dSJoe Perches int l2tp_session_delete(struct l2tp_session *session);
225c1b1203dSJoe Perches void l2tp_session_free(struct l2tp_session *session);
226c1b1203dSJoe Perches void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
227c1b1203dSJoe Perches 		      unsigned char *ptr, unsigned char *optr, u16 hdrflags,
2282b139e6bSGuillaume Nault 		      int length);
229c1b1203dSJoe Perches int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb);
230bb5016eaSGuillaume Nault void l2tp_session_set_header_len(struct l2tp_session *session, int version);
231fd558d18SJames Chapman 
232c1b1203dSJoe Perches int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb,
233c1b1203dSJoe Perches 		  int hdr_len);
234fd558d18SJames Chapman 
235c1b1203dSJoe Perches int l2tp_nl_register_ops(enum l2tp_pwtype pw_type,
236c1b1203dSJoe Perches 			 const struct l2tp_nl_cmd_ops *ops);
237c1b1203dSJoe Perches void l2tp_nl_unregister_ops(enum l2tp_pwtype pw_type);
23872fb96e7SEric Dumazet int l2tp_ioctl(struct sock *sk, int cmd, unsigned long arg);
239309795f4SJames Chapman 
24054652eb1SGuillaume Nault static inline void l2tp_tunnel_inc_refcount(struct l2tp_tunnel *tunnel)
24154652eb1SGuillaume Nault {
24254652eb1SGuillaume Nault 	refcount_inc(&tunnel->ref_count);
24354652eb1SGuillaume Nault }
24454652eb1SGuillaume Nault 
24554652eb1SGuillaume Nault static inline void l2tp_tunnel_dec_refcount(struct l2tp_tunnel *tunnel)
24654652eb1SGuillaume Nault {
24754652eb1SGuillaume Nault 	if (refcount_dec_and_test(&tunnel->ref_count))
248d00fa9adSJames Chapman 		l2tp_tunnel_free(tunnel);
24954652eb1SGuillaume Nault }
25054652eb1SGuillaume Nault 
251fd558d18SJames Chapman /* Session reference counts. Incremented when code obtains a reference
252fd558d18SJames Chapman  * to a session.
253fd558d18SJames Chapman  */
2549ff672baSGuillaume Nault static inline void l2tp_session_inc_refcount(struct l2tp_session *session)
255fd558d18SJames Chapman {
256f00c854cSReshetova, Elena 	refcount_inc(&session->ref_count);
257fd558d18SJames Chapman }
258fd558d18SJames Chapman 
2599ff672baSGuillaume Nault static inline void l2tp_session_dec_refcount(struct l2tp_session *session)
260fd558d18SJames Chapman {
261f00c854cSReshetova, Elena 	if (refcount_dec_and_test(&session->ref_count))
262fd558d18SJames Chapman 		l2tp_session_free(session);
263fd558d18SJames Chapman }
264fd558d18SJames Chapman 
26562e7b6a5SLorenzo Bianconi static inline int l2tp_get_l2specific_len(struct l2tp_session *session)
26662e7b6a5SLorenzo Bianconi {
26762e7b6a5SLorenzo Bianconi 	switch (session->l2specific_type) {
26862e7b6a5SLorenzo Bianconi 	case L2TP_L2SPECTYPE_DEFAULT:
26962e7b6a5SLorenzo Bianconi 		return 4;
27062e7b6a5SLorenzo Bianconi 	case L2TP_L2SPECTYPE_NONE:
27162e7b6a5SLorenzo Bianconi 	default:
27262e7b6a5SLorenzo Bianconi 		return 0;
27362e7b6a5SLorenzo Bianconi 	}
27462e7b6a5SLorenzo Bianconi }
27562e7b6a5SLorenzo Bianconi 
2761f5cd2a0SGuillaume Nault static inline u32 l2tp_tunnel_dst_mtu(const struct l2tp_tunnel *tunnel)
2771f5cd2a0SGuillaume Nault {
2781f5cd2a0SGuillaume Nault 	struct dst_entry *dst;
2791f5cd2a0SGuillaume Nault 	u32 mtu;
2801f5cd2a0SGuillaume Nault 
2811f5cd2a0SGuillaume Nault 	dst = sk_dst_get(tunnel->sock);
2821f5cd2a0SGuillaume Nault 	if (!dst)
2831f5cd2a0SGuillaume Nault 		return 0;
2841f5cd2a0SGuillaume Nault 
2851f5cd2a0SGuillaume Nault 	mtu = dst_mtu(dst);
2861f5cd2a0SGuillaume Nault 	dst_release(dst);
2871f5cd2a0SGuillaume Nault 
2881f5cd2a0SGuillaume Nault 	return mtu;
2891f5cd2a0SGuillaume Nault }
2901f5cd2a0SGuillaume Nault 
291d6a61ec9SGuillaume Nault #ifdef CONFIG_XFRM
292d6a61ec9SGuillaume Nault static inline bool l2tp_tunnel_uses_xfrm(const struct l2tp_tunnel *tunnel)
293d6a61ec9SGuillaume Nault {
294d6a61ec9SGuillaume Nault 	struct sock *sk = tunnel->sock;
295d6a61ec9SGuillaume Nault 
296d6a61ec9SGuillaume Nault 	return sk && (rcu_access_pointer(sk->sk_policy[0]) ||
297d6a61ec9SGuillaume Nault 		      rcu_access_pointer(sk->sk_policy[1]));
298d6a61ec9SGuillaume Nault }
299d6a61ec9SGuillaume Nault #else
300d6a61ec9SGuillaume Nault static inline bool l2tp_tunnel_uses_xfrm(const struct l2tp_tunnel *tunnel)
301d6a61ec9SGuillaume Nault {
302d6a61ec9SGuillaume Nault 	return false;
303d6a61ec9SGuillaume Nault }
304d6a61ec9SGuillaume Nault #endif
305d6a61ec9SGuillaume Nault 
306a4ca44faSJoe Perches #define l2tp_printk(ptr, type, func, fmt, ...)				\
307a4ca44faSJoe Perches do {									\
308a4ca44faSJoe Perches 	if (((ptr)->debug) & (type))					\
309a4ca44faSJoe Perches 		func(fmt, ##__VA_ARGS__);				\
310a4ca44faSJoe Perches } while (0)
311a4ca44faSJoe Perches 
312a4ca44faSJoe Perches #define l2tp_warn(ptr, type, fmt, ...)					\
313a4ca44faSJoe Perches 	l2tp_printk(ptr, type, pr_warn, fmt, ##__VA_ARGS__)
314a4ca44faSJoe Perches #define l2tp_info(ptr, type, fmt, ...)					\
315a4ca44faSJoe Perches 	l2tp_printk(ptr, type, pr_info, fmt, ##__VA_ARGS__)
316a4ca44faSJoe Perches #define l2tp_dbg(ptr, type, fmt, ...)					\
317a4ca44faSJoe Perches 	l2tp_printk(ptr, type, pr_debug, fmt, ##__VA_ARGS__)
318a4ca44faSJoe Perches 
319f1f39f91Sstephen hemminger #define MODULE_ALIAS_L2TP_PWTYPE(type) \
320f1f39f91Sstephen hemminger 	MODULE_ALIAS("net-l2tp-type-" __stringify(type))
321f1f39f91Sstephen hemminger 
322fd558d18SJames Chapman #endif /* _L2TP_CORE_H_ */
323