xref: /openbmc/linux/net/l2tp/l2tp_core.h (revision 1f5cd2a0)
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 
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
24fd558d18SJames Chapman #define L2TP_HASH_SIZE	(1 << L2TP_HASH_BITS)
25fd558d18SJames Chapman 
26f7faffa3SJames Chapman /* System-wide, session hash table size */
27f7faffa3SJames Chapman #define L2TP_HASH_BITS_2	8
28f7faffa3SJames Chapman #define L2TP_HASH_SIZE_2	(1 << 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;
5195c96174SEric Dumazet 	unsigned int		recv_seq:1;	/* expect receive packets with
52fd558d18SJames Chapman 						 * sequence numbers? */
5395c96174SEric Dumazet 	unsigned int		send_seq:1;	/* send packets with sequence
54fd558d18SJames Chapman 						 * numbers? */
5595c96174SEric Dumazet 	unsigned int		lns_mode:1;	/* behave as LNS? LAC enables
56fd558d18SJames Chapman 						 * sequence numbers under
57fd558d18SJames Chapman 						 * control of LNS. */
58fd558d18SJames Chapman 	int			debug;		/* bitmask of debug message
59fd558d18SJames Chapman 						 * categories */
60f7faffa3SJames Chapman 	u16			l2specific_type; /* Layer 2 specific type */
61f7faffa3SJames Chapman 	u8			cookie[8];	/* optional cookie */
62f7faffa3SJames Chapman 	int			cookie_len;	/* 0, 4 or 8 bytes */
63f7faffa3SJames Chapman 	u8			peer_cookie[8];	/* peer's cookie */
64f7faffa3SJames Chapman 	int			peer_cookie_len; /* 0, 4 or 8 bytes */
65fd558d18SJames Chapman 	int			reorder_timeout; /* configured reorder timeout
66fd558d18SJames Chapman 						  * (in jiffies) */
67fd558d18SJames Chapman 	int			mtu;
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 */
111fd558d18SJames Chapman 	int			mtu;
112f7faffa3SJames Chapman 	enum l2tp_pwtype	pwtype;
113fd558d18SJames Chapman 	struct l2tp_stats	stats;
114f7faffa3SJames Chapman 	struct hlist_node	global_hlist;	/* Global hash list node */
115fd558d18SJames Chapman 
116f7faffa3SJames Chapman 	int (*build_header)(struct l2tp_session *session, void *buf);
117fd558d18SJames Chapman 	void (*recv_skb)(struct l2tp_session *session, struct sk_buff *skb, int data_len);
118fd558d18SJames Chapman 	void (*session_close)(struct l2tp_session *session);
1199dd79945SJavier Martinez Canillas #if IS_ENABLED(CONFIG_L2TP_DEBUGFS)
1200ad66140SJames Chapman 	void (*show)(struct seq_file *m, void *priv);
1210ad66140SJames Chapman #endif
122fd558d18SJames Chapman 	uint8_t			priv[0];	/* private data */
123fd558d18SJames Chapman };
124fd558d18SJames Chapman 
125fd558d18SJames Chapman /* Describes the tunnel. It contains info to track all the associated
126fd558d18SJames Chapman  * sessions so incoming packets can be sorted out
127fd558d18SJames Chapman  */
128fd558d18SJames Chapman struct l2tp_tunnel_cfg {
129fd558d18SJames Chapman 	int			debug;		/* bitmask of debug message
130fd558d18SJames Chapman 						 * categories */
1310d76751fSJames Chapman 	enum l2tp_encap_type	encap;
132789a4a2cSJames Chapman 
133789a4a2cSJames Chapman 	/* Used only for kernel-created sockets */
134789a4a2cSJames Chapman 	struct in_addr		local_ip;
135789a4a2cSJames Chapman 	struct in_addr		peer_ip;
136f9bac8dfSChris Elston #if IS_ENABLED(CONFIG_IPV6)
137f9bac8dfSChris Elston 	struct in6_addr		*local_ip6;
138f9bac8dfSChris Elston 	struct in6_addr		*peer_ip6;
139f9bac8dfSChris Elston #endif
140789a4a2cSJames Chapman 	u16			local_udp_port;
141789a4a2cSJames Chapman 	u16			peer_udp_port;
1426b649feaSTom Herbert 	unsigned int		use_udp_checksums:1,
1436b649feaSTom Herbert 				udp6_zero_tx_checksums:1,
1446b649feaSTom Herbert 				udp6_zero_rx_checksums:1;
145fd558d18SJames Chapman };
146fd558d18SJames Chapman 
147fd558d18SJames Chapman struct l2tp_tunnel {
148fd558d18SJames Chapman 	int			magic;		/* Should be L2TP_TUNNEL_MAGIC */
14962b982eeSSabrina Dubroca 
15062b982eeSSabrina Dubroca 	unsigned long		dead;
15162b982eeSSabrina Dubroca 
15299469c32Sxeb@mail.ru 	struct rcu_head rcu;
153fd558d18SJames Chapman 	rwlock_t		hlist_lock;	/* protect session_hlist */
154f3c66d4eSGuillaume Nault 	bool			acpt_newsess;	/* Indicates whether this
155f3c66d4eSGuillaume Nault 						 * tunnel accepts new sessions.
156f3c66d4eSGuillaume Nault 						 * Protected by hlist_lock.
157f3c66d4eSGuillaume Nault 						 */
158fd558d18SJames Chapman 	struct hlist_head	session_hlist[L2TP_HASH_SIZE];
159fd558d18SJames Chapman 						/* hashed list of sessions,
160fd558d18SJames Chapman 						 * hashed by id */
161fd558d18SJames Chapman 	u32			tunnel_id;
162fd558d18SJames Chapman 	u32			peer_tunnel_id;
163fd558d18SJames Chapman 	int			version;	/* 2=>L2TPv2, 3=>L2TPv3 */
164fd558d18SJames Chapman 
165fd558d18SJames Chapman 	char			name[20];	/* for logging */
166fd558d18SJames Chapman 	int			debug;		/* bitmask of debug message
167fd558d18SJames Chapman 						 * categories */
1680d76751fSJames Chapman 	enum l2tp_encap_type	encap;
169fd558d18SJames Chapman 	struct l2tp_stats	stats;
170fd558d18SJames Chapman 
171fd558d18SJames Chapman 	struct list_head	list;		/* Keep a list of all tunnels */
172fd558d18SJames Chapman 	struct net		*l2tp_net;	/* the net we belong to */
173fd558d18SJames Chapman 
174fbea9e07SReshetova, Elena 	refcount_t		ref_count;
175fd558d18SJames Chapman 	void (*old_sk_destruct)(struct sock *);
176fd558d18SJames Chapman 	struct sock		*sock;		/* Parent socket */
17780d84ef3STom Parkin 	int			fd;		/* Parent fd, if tunnel socket
17880d84ef3STom Parkin 						 * was created by userspace */
179fd558d18SJames Chapman 
180f8ccac0eSTom Parkin 	struct work_struct	del_work;
181fd558d18SJames Chapman };
182fd558d18SJames Chapman 
183309795f4SJames Chapman struct l2tp_nl_cmd_ops {
184f026bc29SGuillaume Nault 	int (*session_create)(struct net *net, struct l2tp_tunnel *tunnel,
185f026bc29SGuillaume Nault 			      u32 session_id, u32 peer_session_id,
186f026bc29SGuillaume Nault 			      struct l2tp_session_cfg *cfg);
187309795f4SJames Chapman 	int (*session_delete)(struct l2tp_session *session);
188309795f4SJames Chapman };
189309795f4SJames Chapman 
190fd558d18SJames Chapman static inline void *l2tp_session_priv(struct l2tp_session *session)
191fd558d18SJames Chapman {
192fd558d18SJames Chapman 	return &session->priv[0];
193fd558d18SJames Chapman }
194fd558d18SJames Chapman 
19554652eb1SGuillaume Nault struct l2tp_tunnel *l2tp_tunnel_get(const struct net *net, u32 tunnel_id);
1965846c131SGuillaume Nault struct l2tp_tunnel *l2tp_tunnel_get_nth(const struct net *net, int nth);
1975846c131SGuillaume Nault 
198d00fa9adSJames Chapman void l2tp_tunnel_free(struct l2tp_tunnel *tunnel);
19954652eb1SGuillaume Nault 
2009aaef50cSGuillaume Nault struct l2tp_session *l2tp_session_get(const struct net *net,
20161b9a047SGuillaume Nault 				      struct l2tp_tunnel *tunnel,
202a4346210SGuillaume Nault 				      u32 session_id);
203a4346210SGuillaume Nault struct l2tp_session *l2tp_session_get_nth(struct l2tp_tunnel *tunnel, int nth);
2049aaef50cSGuillaume Nault struct l2tp_session *l2tp_session_get_by_ifname(const struct net *net,
205a4346210SGuillaume Nault 						const char *ifname);
206fd558d18SJames Chapman 
207c1b1203dSJoe Perches int l2tp_tunnel_create(struct net *net, int fd, int version, u32 tunnel_id,
208c1b1203dSJoe Perches 		       u32 peer_tunnel_id, struct l2tp_tunnel_cfg *cfg,
209c1b1203dSJoe Perches 		       struct l2tp_tunnel **tunnelp);
2106b9f3423SGuillaume Nault int l2tp_tunnel_register(struct l2tp_tunnel *tunnel, struct net *net,
2116b9f3423SGuillaume Nault 			 struct l2tp_tunnel_cfg *cfg);
2126b9f3423SGuillaume Nault 
21362b982eeSSabrina Dubroca void l2tp_tunnel_delete(struct l2tp_tunnel *tunnel);
214c1b1203dSJoe Perches struct l2tp_session *l2tp_session_create(int priv_size,
215c1b1203dSJoe Perches 					 struct l2tp_tunnel *tunnel,
216c1b1203dSJoe Perches 					 u32 session_id, u32 peer_session_id,
217c1b1203dSJoe Perches 					 struct l2tp_session_cfg *cfg);
2183953ae7bSGuillaume Nault int l2tp_session_register(struct l2tp_session *session,
2193953ae7bSGuillaume Nault 			  struct l2tp_tunnel *tunnel);
2203953ae7bSGuillaume Nault 
221c1b1203dSJoe Perches void __l2tp_session_unhash(struct l2tp_session *session);
222c1b1203dSJoe Perches int l2tp_session_delete(struct l2tp_session *session);
223c1b1203dSJoe Perches void l2tp_session_free(struct l2tp_session *session);
224c1b1203dSJoe Perches void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
225c1b1203dSJoe Perches 		      unsigned char *ptr, unsigned char *optr, u16 hdrflags,
2262b139e6bSGuillaume Nault 		      int length);
227c1b1203dSJoe Perches int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb);
228bb5016eaSGuillaume Nault void l2tp_session_set_header_len(struct l2tp_session *session, int version);
229fd558d18SJames Chapman 
230c1b1203dSJoe Perches int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb,
231c1b1203dSJoe Perches 		  int hdr_len);
232fd558d18SJames Chapman 
233c1b1203dSJoe Perches int l2tp_nl_register_ops(enum l2tp_pwtype pw_type,
234c1b1203dSJoe Perches 			 const struct l2tp_nl_cmd_ops *ops);
235c1b1203dSJoe Perches void l2tp_nl_unregister_ops(enum l2tp_pwtype pw_type);
23672fb96e7SEric Dumazet int l2tp_ioctl(struct sock *sk, int cmd, unsigned long arg);
237309795f4SJames Chapman 
23854652eb1SGuillaume Nault static inline void l2tp_tunnel_inc_refcount(struct l2tp_tunnel *tunnel)
23954652eb1SGuillaume Nault {
24054652eb1SGuillaume Nault 	refcount_inc(&tunnel->ref_count);
24154652eb1SGuillaume Nault }
24254652eb1SGuillaume Nault 
24354652eb1SGuillaume Nault static inline void l2tp_tunnel_dec_refcount(struct l2tp_tunnel *tunnel)
24454652eb1SGuillaume Nault {
24554652eb1SGuillaume Nault 	if (refcount_dec_and_test(&tunnel->ref_count))
246d00fa9adSJames Chapman 		l2tp_tunnel_free(tunnel);
24754652eb1SGuillaume Nault }
24854652eb1SGuillaume Nault 
249fd558d18SJames Chapman /* Session reference counts. Incremented when code obtains a reference
250fd558d18SJames Chapman  * to a session.
251fd558d18SJames Chapman  */
2529ff672baSGuillaume Nault static inline void l2tp_session_inc_refcount(struct l2tp_session *session)
253fd558d18SJames Chapman {
254f00c854cSReshetova, Elena 	refcount_inc(&session->ref_count);
255fd558d18SJames Chapman }
256fd558d18SJames Chapman 
2579ff672baSGuillaume Nault static inline void l2tp_session_dec_refcount(struct l2tp_session *session)
258fd558d18SJames Chapman {
259f00c854cSReshetova, Elena 	if (refcount_dec_and_test(&session->ref_count))
260fd558d18SJames Chapman 		l2tp_session_free(session);
261fd558d18SJames Chapman }
262fd558d18SJames Chapman 
26362e7b6a5SLorenzo Bianconi static inline int l2tp_get_l2specific_len(struct l2tp_session *session)
26462e7b6a5SLorenzo Bianconi {
26562e7b6a5SLorenzo Bianconi 	switch (session->l2specific_type) {
26662e7b6a5SLorenzo Bianconi 	case L2TP_L2SPECTYPE_DEFAULT:
26762e7b6a5SLorenzo Bianconi 		return 4;
26862e7b6a5SLorenzo Bianconi 	case L2TP_L2SPECTYPE_NONE:
26962e7b6a5SLorenzo Bianconi 	default:
27062e7b6a5SLorenzo Bianconi 		return 0;
27162e7b6a5SLorenzo Bianconi 	}
27262e7b6a5SLorenzo Bianconi }
27362e7b6a5SLorenzo Bianconi 
2741f5cd2a0SGuillaume Nault static inline u32 l2tp_tunnel_dst_mtu(const struct l2tp_tunnel *tunnel)
2751f5cd2a0SGuillaume Nault {
2761f5cd2a0SGuillaume Nault 	struct dst_entry *dst;
2771f5cd2a0SGuillaume Nault 	u32 mtu;
2781f5cd2a0SGuillaume Nault 
2791f5cd2a0SGuillaume Nault 	dst = sk_dst_get(tunnel->sock);
2801f5cd2a0SGuillaume Nault 	if (!dst)
2811f5cd2a0SGuillaume Nault 		return 0;
2821f5cd2a0SGuillaume Nault 
2831f5cd2a0SGuillaume Nault 	mtu = dst_mtu(dst);
2841f5cd2a0SGuillaume Nault 	dst_release(dst);
2851f5cd2a0SGuillaume Nault 
2861f5cd2a0SGuillaume Nault 	return mtu;
2871f5cd2a0SGuillaume Nault }
2881f5cd2a0SGuillaume Nault 
289a4ca44faSJoe Perches #define l2tp_printk(ptr, type, func, fmt, ...)				\
290a4ca44faSJoe Perches do {									\
291a4ca44faSJoe Perches 	if (((ptr)->debug) & (type))					\
292a4ca44faSJoe Perches 		func(fmt, ##__VA_ARGS__);				\
293a4ca44faSJoe Perches } while (0)
294a4ca44faSJoe Perches 
295a4ca44faSJoe Perches #define l2tp_warn(ptr, type, fmt, ...)					\
296a4ca44faSJoe Perches 	l2tp_printk(ptr, type, pr_warn, fmt, ##__VA_ARGS__)
297a4ca44faSJoe Perches #define l2tp_info(ptr, type, fmt, ...)					\
298a4ca44faSJoe Perches 	l2tp_printk(ptr, type, pr_info, fmt, ##__VA_ARGS__)
299a4ca44faSJoe Perches #define l2tp_dbg(ptr, type, fmt, ...)					\
300a4ca44faSJoe Perches 	l2tp_printk(ptr, type, pr_debug, fmt, ##__VA_ARGS__)
301a4ca44faSJoe Perches 
302f1f39f91Sstephen hemminger #define MODULE_ALIAS_L2TP_PWTYPE(type) \
303f1f39f91Sstephen hemminger 	MODULE_ALIAS("net-l2tp-type-" __stringify(type))
304f1f39f91Sstephen hemminger 
305fd558d18SJames Chapman #endif /* _L2TP_CORE_H_ */
306