xref: /openbmc/linux/net/l2tp/l2tp_core.h (revision f9bac8df)
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 {
39fd558d18SJames Chapman 	u64			tx_packets;
40fd558d18SJames Chapman 	u64			tx_bytes;
41fd558d18SJames Chapman 	u64			tx_errors;
42fd558d18SJames Chapman 	u64			rx_packets;
43fd558d18SJames Chapman 	u64			rx_bytes;
44fd558d18SJames Chapman 	u64			rx_seq_discards;
45fd558d18SJames Chapman 	u64			rx_oos_packets;
46fd558d18SJames Chapman 	u64			rx_errors;
47f7faffa3SJames Chapman 	u64			rx_cookie_discards;
485de7aee5SJames Chapman 	struct u64_stats_sync	syncp;
49fd558d18SJames Chapman };
50fd558d18SJames Chapman 
51fd558d18SJames Chapman struct l2tp_tunnel;
52fd558d18SJames Chapman 
53fd558d18SJames Chapman /* Describes a session. Contains information to determine incoming
54fd558d18SJames Chapman  * packets and transmit outgoing ones.
55fd558d18SJames Chapman  */
56fd558d18SJames Chapman struct l2tp_session_cfg {
57f7faffa3SJames Chapman 	enum l2tp_pwtype	pw_type;
5895c96174SEric Dumazet 	unsigned int		data_seq:2;	/* data sequencing level
59fd558d18SJames Chapman 						 * 0 => none, 1 => IP only,
60fd558d18SJames Chapman 						 * 2 => all
61fd558d18SJames Chapman 						 */
6295c96174SEric Dumazet 	unsigned int		recv_seq:1;	/* expect receive packets with
63fd558d18SJames Chapman 						 * sequence numbers? */
6495c96174SEric Dumazet 	unsigned int		send_seq:1;	/* send packets with sequence
65fd558d18SJames Chapman 						 * numbers? */
6695c96174SEric Dumazet 	unsigned int		lns_mode:1;	/* behave as LNS? LAC enables
67fd558d18SJames Chapman 						 * sequence numbers under
68fd558d18SJames Chapman 						 * control of LNS. */
69fd558d18SJames Chapman 	int			debug;		/* bitmask of debug message
70fd558d18SJames Chapman 						 * categories */
71309795f4SJames Chapman 	u16			vlan_id;	/* VLAN pseudowire only */
72f7faffa3SJames Chapman 	u16			offset;		/* offset to payload */
73f7faffa3SJames Chapman 	u16			l2specific_len;	/* Layer 2 specific length */
74f7faffa3SJames Chapman 	u16			l2specific_type; /* Layer 2 specific type */
75f7faffa3SJames Chapman 	u8			cookie[8];	/* optional cookie */
76f7faffa3SJames Chapman 	int			cookie_len;	/* 0, 4 or 8 bytes */
77f7faffa3SJames Chapman 	u8			peer_cookie[8];	/* peer's cookie */
78f7faffa3SJames Chapman 	int			peer_cookie_len; /* 0, 4 or 8 bytes */
79fd558d18SJames Chapman 	int			reorder_timeout; /* configured reorder timeout
80fd558d18SJames Chapman 						  * (in jiffies) */
81fd558d18SJames Chapman 	int			mtu;
82fd558d18SJames Chapman 	int			mru;
83309795f4SJames Chapman 	char			*ifname;
84fd558d18SJames Chapman };
85fd558d18SJames Chapman 
86fd558d18SJames Chapman struct l2tp_session {
87fd558d18SJames Chapman 	int			magic;		/* should be
88fd558d18SJames Chapman 						 * L2TP_SESSION_MAGIC */
89fd558d18SJames Chapman 
90fd558d18SJames Chapman 	struct l2tp_tunnel	*tunnel;	/* back pointer to tunnel
91fd558d18SJames Chapman 						 * context */
92fd558d18SJames Chapman 	u32			session_id;
93fd558d18SJames Chapman 	u32			peer_session_id;
94f7faffa3SJames Chapman 	u8			cookie[8];
95f7faffa3SJames Chapman 	int			cookie_len;
96f7faffa3SJames Chapman 	u8			peer_cookie[8];
97f7faffa3SJames Chapman 	int			peer_cookie_len;
98f7faffa3SJames Chapman 	u16			offset;		/* offset from end of L2TP header
99f7faffa3SJames Chapman 						   to beginning of data */
100f7faffa3SJames Chapman 	u16			l2specific_len;
101f7faffa3SJames Chapman 	u16			l2specific_type;
102f7faffa3SJames Chapman 	u16			hdr_len;
103f7faffa3SJames Chapman 	u32			nr;		/* session NR state (receive) */
104f7faffa3SJames Chapman 	u32			ns;		/* session NR state (send) */
105fd558d18SJames Chapman 	struct sk_buff_head	reorder_q;	/* receive reorder queue */
106fd558d18SJames Chapman 	struct hlist_node	hlist;		/* Hash list node */
107fd558d18SJames Chapman 	atomic_t		ref_count;
108fd558d18SJames Chapman 
109fd558d18SJames Chapman 	char			name[32];	/* for logging */
110309795f4SJames Chapman 	char			ifname[IFNAMSIZ];
11195c96174SEric Dumazet 	unsigned int		data_seq:2;	/* data sequencing level
112fd558d18SJames Chapman 						 * 0 => none, 1 => IP only,
113fd558d18SJames Chapman 						 * 2 => all
114fd558d18SJames Chapman 						 */
11595c96174SEric Dumazet 	unsigned int		recv_seq:1;	/* expect receive packets with
116fd558d18SJames Chapman 						 * sequence numbers? */
11795c96174SEric Dumazet 	unsigned int		send_seq:1;	/* send packets with sequence
118fd558d18SJames Chapman 						 * numbers? */
11995c96174SEric Dumazet 	unsigned int		lns_mode:1;	/* behave as LNS? LAC enables
120fd558d18SJames Chapman 						 * sequence numbers under
121fd558d18SJames Chapman 						 * control of LNS. */
122fd558d18SJames Chapman 	int			debug;		/* bitmask of debug message
123fd558d18SJames Chapman 						 * categories */
124fd558d18SJames Chapman 	int			reorder_timeout; /* configured reorder timeout
125fd558d18SJames Chapman 						  * (in jiffies) */
126fd558d18SJames Chapman 	int			mtu;
127fd558d18SJames Chapman 	int			mru;
128f7faffa3SJames Chapman 	enum l2tp_pwtype	pwtype;
129fd558d18SJames Chapman 	struct l2tp_stats	stats;
130f7faffa3SJames Chapman 	struct hlist_node	global_hlist;	/* Global hash list node */
131fd558d18SJames Chapman 
132f7faffa3SJames Chapman 	int (*build_header)(struct l2tp_session *session, void *buf);
133fd558d18SJames Chapman 	void (*recv_skb)(struct l2tp_session *session, struct sk_buff *skb, int data_len);
134fd558d18SJames Chapman 	void (*session_close)(struct l2tp_session *session);
135fd558d18SJames Chapman 	void (*ref)(struct l2tp_session *session);
136fd558d18SJames Chapman 	void (*deref)(struct l2tp_session *session);
137f66ef2d0SDavid S. Miller #if defined(CONFIG_L2TP_DEBUGFS) || defined(CONFIG_L2TP_DEBUGFS_MODULE)
1380ad66140SJames Chapman 	void (*show)(struct seq_file *m, void *priv);
1390ad66140SJames Chapman #endif
140fd558d18SJames Chapman 	uint8_t			priv[0];	/* private data */
141fd558d18SJames Chapman };
142fd558d18SJames Chapman 
143fd558d18SJames Chapman /* Describes the tunnel. It contains info to track all the associated
144fd558d18SJames Chapman  * sessions so incoming packets can be sorted out
145fd558d18SJames Chapman  */
146fd558d18SJames Chapman struct l2tp_tunnel_cfg {
147fd558d18SJames Chapman 	int			debug;		/* bitmask of debug message
148fd558d18SJames Chapman 						 * categories */
1490d76751fSJames Chapman 	enum l2tp_encap_type	encap;
150789a4a2cSJames Chapman 
151789a4a2cSJames Chapman 	/* Used only for kernel-created sockets */
152789a4a2cSJames Chapman 	struct in_addr		local_ip;
153789a4a2cSJames Chapman 	struct in_addr		peer_ip;
154f9bac8dfSChris Elston #if IS_ENABLED(CONFIG_IPV6)
155f9bac8dfSChris Elston 	struct in6_addr		*local_ip6;
156f9bac8dfSChris Elston 	struct in6_addr		*peer_ip6;
157f9bac8dfSChris Elston #endif
158789a4a2cSJames Chapman 	u16			local_udp_port;
159789a4a2cSJames Chapman 	u16			peer_udp_port;
1607bddd0dbSEric Dumazet 	unsigned int		use_udp_checksums:1;
161fd558d18SJames Chapman };
162fd558d18SJames Chapman 
163fd558d18SJames Chapman struct l2tp_tunnel {
164fd558d18SJames Chapman 	int			magic;		/* Should be L2TP_TUNNEL_MAGIC */
165fd558d18SJames Chapman 	rwlock_t		hlist_lock;	/* protect session_hlist */
166fd558d18SJames Chapman 	struct hlist_head	session_hlist[L2TP_HASH_SIZE];
167fd558d18SJames Chapman 						/* hashed list of sessions,
168fd558d18SJames Chapman 						 * hashed by id */
169fd558d18SJames Chapman 	u32			tunnel_id;
170fd558d18SJames Chapman 	u32			peer_tunnel_id;
171fd558d18SJames Chapman 	int			version;	/* 2=>L2TPv2, 3=>L2TPv3 */
172fd558d18SJames Chapman 
173fd558d18SJames Chapman 	char			name[20];	/* for logging */
174fd558d18SJames Chapman 	int			debug;		/* bitmask of debug message
175fd558d18SJames Chapman 						 * categories */
1760d76751fSJames Chapman 	enum l2tp_encap_type	encap;
177fd558d18SJames Chapman 	struct l2tp_stats	stats;
178fd558d18SJames Chapman 
179fd558d18SJames Chapman 	struct list_head	list;		/* Keep a list of all tunnels */
180fd558d18SJames Chapman 	struct net		*l2tp_net;	/* the net we belong to */
181fd558d18SJames Chapman 
182fd558d18SJames Chapman 	atomic_t		ref_count;
1830ad66140SJames Chapman #ifdef CONFIG_DEBUG_FS
1840ad66140SJames Chapman 	void (*show)(struct seq_file *m, void *arg);
1850ad66140SJames Chapman #endif
186fd558d18SJames Chapman 	int (*recv_payload_hook)(struct sk_buff *skb);
187fd558d18SJames Chapman 	void (*old_sk_destruct)(struct sock *);
188fd558d18SJames Chapman 	struct sock		*sock;		/* Parent socket */
189fd558d18SJames Chapman 	int			fd;
190fd558d18SJames Chapman 
191fd558d18SJames Chapman 	uint8_t			priv[0];	/* private data */
192fd558d18SJames Chapman };
193fd558d18SJames Chapman 
194309795f4SJames Chapman struct l2tp_nl_cmd_ops {
195309795f4SJames Chapman 	int (*session_create)(struct net *net, u32 tunnel_id, u32 session_id, u32 peer_session_id, struct l2tp_session_cfg *cfg);
196309795f4SJames Chapman 	int (*session_delete)(struct l2tp_session *session);
197309795f4SJames Chapman };
198309795f4SJames Chapman 
199fd558d18SJames Chapman static inline void *l2tp_tunnel_priv(struct l2tp_tunnel *tunnel)
200fd558d18SJames Chapman {
201fd558d18SJames Chapman 	return &tunnel->priv[0];
202fd558d18SJames Chapman }
203fd558d18SJames Chapman 
204fd558d18SJames Chapman static inline void *l2tp_session_priv(struct l2tp_session *session)
205fd558d18SJames Chapman {
206fd558d18SJames Chapman 	return &session->priv[0];
207fd558d18SJames Chapman }
208fd558d18SJames Chapman 
209fd558d18SJames Chapman static inline struct l2tp_tunnel *l2tp_sock_to_tunnel(struct sock *sk)
210fd558d18SJames Chapman {
211fd558d18SJames Chapman 	struct l2tp_tunnel *tunnel;
212fd558d18SJames Chapman 
213fd558d18SJames Chapman 	if (sk == NULL)
214fd558d18SJames Chapman 		return NULL;
215fd558d18SJames Chapman 
216fd558d18SJames Chapman 	sock_hold(sk);
217fd558d18SJames Chapman 	tunnel = (struct l2tp_tunnel *)(sk->sk_user_data);
218fd558d18SJames Chapman 	if (tunnel == NULL) {
219fd558d18SJames Chapman 		sock_put(sk);
220fd558d18SJames Chapman 		goto out;
221fd558d18SJames Chapman 	}
222fd558d18SJames Chapman 
223fd558d18SJames Chapman 	BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
224fd558d18SJames Chapman 
225fd558d18SJames Chapman out:
226fd558d18SJames Chapman 	return tunnel;
227fd558d18SJames Chapman }
228fd558d18SJames Chapman 
229f7faffa3SJames Chapman extern struct l2tp_session *l2tp_session_find(struct net *net, struct l2tp_tunnel *tunnel, u32 session_id);
230fd558d18SJames Chapman extern struct l2tp_session *l2tp_session_find_nth(struct l2tp_tunnel *tunnel, int nth);
231309795f4SJames Chapman extern struct l2tp_session *l2tp_session_find_by_ifname(struct net *net, char *ifname);
232fd558d18SJames Chapman extern struct l2tp_tunnel *l2tp_tunnel_find(struct net *net, u32 tunnel_id);
233fd558d18SJames Chapman extern struct l2tp_tunnel *l2tp_tunnel_find_nth(struct net *net, int nth);
234fd558d18SJames Chapman 
235fd558d18SJames Chapman extern int l2tp_tunnel_create(struct net *net, int fd, int version, u32 tunnel_id, u32 peer_tunnel_id, struct l2tp_tunnel_cfg *cfg, struct l2tp_tunnel **tunnelp);
236309795f4SJames Chapman extern int l2tp_tunnel_delete(struct l2tp_tunnel *tunnel);
237fd558d18SJames Chapman extern struct l2tp_session *l2tp_session_create(int priv_size, struct l2tp_tunnel *tunnel, u32 session_id, u32 peer_session_id, struct l2tp_session_cfg *cfg);
238309795f4SJames Chapman extern int l2tp_session_delete(struct l2tp_session *session);
239fd558d18SJames Chapman extern void l2tp_session_free(struct l2tp_session *session);
240f7faffa3SJames Chapman extern void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb, unsigned char *ptr, unsigned char *optr, u16 hdrflags, int length, int (*payload_hook)(struct sk_buff *skb));
241fd558d18SJames Chapman extern int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb);
242fd558d18SJames Chapman 
243fd558d18SJames Chapman extern int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len);
244fd558d18SJames Chapman 
245309795f4SJames Chapman extern int l2tp_nl_register_ops(enum l2tp_pwtype pw_type, const struct l2tp_nl_cmd_ops *ops);
246309795f4SJames Chapman extern void l2tp_nl_unregister_ops(enum l2tp_pwtype pw_type);
247309795f4SJames Chapman 
248fd558d18SJames Chapman /* Session reference counts. Incremented when code obtains a reference
249fd558d18SJames Chapman  * to a session.
250fd558d18SJames Chapman  */
251fd558d18SJames Chapman static inline void l2tp_session_inc_refcount_1(struct l2tp_session *session)
252fd558d18SJames Chapman {
253fd558d18SJames Chapman 	atomic_inc(&session->ref_count);
254fd558d18SJames Chapman }
255fd558d18SJames Chapman 
256fd558d18SJames Chapman static inline void l2tp_session_dec_refcount_1(struct l2tp_session *session)
257fd558d18SJames Chapman {
258fd558d18SJames Chapman 	if (atomic_dec_and_test(&session->ref_count))
259fd558d18SJames Chapman 		l2tp_session_free(session);
260fd558d18SJames Chapman }
261fd558d18SJames Chapman 
262fd558d18SJames Chapman #ifdef L2TP_REFCNT_DEBUG
263fd558d18SJames Chapman #define l2tp_session_inc_refcount(_s) do { \
264fd558d18SJames Chapman 		printk(KERN_DEBUG "l2tp_session_inc_refcount: %s:%d %s: cnt=%d\n", __func__, __LINE__, (_s)->name, atomic_read(&_s->ref_count)); \
265fd558d18SJames Chapman 		l2tp_session_inc_refcount_1(_s);				\
266fd558d18SJames Chapman 	} while (0)
267fd558d18SJames Chapman #define l2tp_session_dec_refcount(_s) do { \
268fd558d18SJames Chapman 		printk(KERN_DEBUG "l2tp_session_dec_refcount: %s:%d %s: cnt=%d\n", __func__, __LINE__, (_s)->name, atomic_read(&_s->ref_count)); \
269fd558d18SJames Chapman 		l2tp_session_dec_refcount_1(_s);				\
270fd558d18SJames Chapman 	} while (0)
271fd558d18SJames Chapman #else
272fd558d18SJames Chapman #define l2tp_session_inc_refcount(s) l2tp_session_inc_refcount_1(s)
273fd558d18SJames Chapman #define l2tp_session_dec_refcount(s) l2tp_session_dec_refcount_1(s)
274fd558d18SJames Chapman #endif
275fd558d18SJames Chapman 
276fd558d18SJames Chapman #endif /* _L2TP_CORE_H_ */
277