xref: /openbmc/linux/net/l2tp/l2tp_core.h (revision e9697e2e)
1 /*
2  * L2TP internal definitions.
3  *
4  * Copyright (c) 2008,2009 Katalix Systems Ltd
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10 #include <linux/refcount.h>
11 
12 #ifndef _L2TP_CORE_H_
13 #define _L2TP_CORE_H_
14 
15 #include <net/dst.h>
16 #include <net/sock.h>
17 
18 /* Just some random numbers */
19 #define L2TP_TUNNEL_MAGIC	0x42114DDA
20 #define L2TP_SESSION_MAGIC	0x0C04EB7D
21 
22 /* Per tunnel, session hash table size */
23 #define L2TP_HASH_BITS	4
24 #define L2TP_HASH_SIZE	(1 << L2TP_HASH_BITS)
25 
26 /* System-wide, session hash table size */
27 #define L2TP_HASH_BITS_2	8
28 #define L2TP_HASH_SIZE_2	(1 << L2TP_HASH_BITS_2)
29 
30 struct sk_buff;
31 
32 struct l2tp_stats {
33 	atomic_long_t		tx_packets;
34 	atomic_long_t		tx_bytes;
35 	atomic_long_t		tx_errors;
36 	atomic_long_t		rx_packets;
37 	atomic_long_t		rx_bytes;
38 	atomic_long_t		rx_seq_discards;
39 	atomic_long_t		rx_oos_packets;
40 	atomic_long_t		rx_errors;
41 	atomic_long_t		rx_cookie_discards;
42 };
43 
44 struct l2tp_tunnel;
45 
46 /* Describes a session. Contains information to determine incoming
47  * packets and transmit outgoing ones.
48  */
49 struct l2tp_session_cfg {
50 	enum l2tp_pwtype	pw_type;
51 	unsigned int		recv_seq:1;	/* expect receive packets with
52 						 * sequence numbers? */
53 	unsigned int		send_seq:1;	/* send packets with sequence
54 						 * numbers? */
55 	unsigned int		lns_mode:1;	/* behave as LNS? LAC enables
56 						 * sequence numbers under
57 						 * control of LNS. */
58 	int			debug;		/* bitmask of debug message
59 						 * categories */
60 	u16			l2specific_type; /* Layer 2 specific type */
61 	u8			cookie[8];	/* optional cookie */
62 	int			cookie_len;	/* 0, 4 or 8 bytes */
63 	u8			peer_cookie[8];	/* peer's cookie */
64 	int			peer_cookie_len; /* 0, 4 or 8 bytes */
65 	int			reorder_timeout; /* configured reorder timeout
66 						  * (in jiffies) */
67 	char			*ifname;
68 };
69 
70 struct l2tp_session {
71 	int			magic;		/* should be
72 						 * L2TP_SESSION_MAGIC */
73 	long			dead;
74 
75 	struct l2tp_tunnel	*tunnel;	/* back pointer to tunnel
76 						 * context */
77 	u32			session_id;
78 	u32			peer_session_id;
79 	u8			cookie[8];
80 	int			cookie_len;
81 	u8			peer_cookie[8];
82 	int			peer_cookie_len;
83 	u16			l2specific_type;
84 	u16			hdr_len;
85 	u32			nr;		/* session NR state (receive) */
86 	u32			ns;		/* session NR state (send) */
87 	struct sk_buff_head	reorder_q;	/* receive reorder queue */
88 	u32			nr_max;		/* max NR. Depends on tunnel */
89 	u32			nr_window_size;	/* NR window size */
90 	u32			nr_oos;		/* NR of last OOS packet */
91 	int			nr_oos_count;	/* For OOS recovery */
92 	int			nr_oos_count_max;
93 	struct hlist_node	hlist;		/* Hash list node */
94 	refcount_t		ref_count;
95 
96 	char			name[32];	/* for logging */
97 	char			ifname[IFNAMSIZ];
98 	unsigned int		recv_seq:1;	/* expect receive packets with
99 						 * sequence numbers? */
100 	unsigned int		send_seq:1;	/* send packets with sequence
101 						 * numbers? */
102 	unsigned int		lns_mode:1;	/* behave as LNS? LAC enables
103 						 * sequence numbers under
104 						 * control of LNS. */
105 	int			debug;		/* bitmask of debug message
106 						 * categories */
107 	int			reorder_timeout; /* configured reorder timeout
108 						  * (in jiffies) */
109 	int			reorder_skip;	/* set if skip to next nr */
110 	enum l2tp_pwtype	pwtype;
111 	struct l2tp_stats	stats;
112 	struct hlist_node	global_hlist;	/* Global hash list node */
113 
114 	int (*build_header)(struct l2tp_session *session, void *buf);
115 	void (*recv_skb)(struct l2tp_session *session, struct sk_buff *skb, int data_len);
116 	void (*session_close)(struct l2tp_session *session);
117 #if IS_ENABLED(CONFIG_L2TP_DEBUGFS)
118 	void (*show)(struct seq_file *m, void *priv);
119 #endif
120 	uint8_t			priv[0];	/* private data */
121 };
122 
123 /* Describes the tunnel. It contains info to track all the associated
124  * sessions so incoming packets can be sorted out
125  */
126 struct l2tp_tunnel_cfg {
127 	int			debug;		/* bitmask of debug message
128 						 * categories */
129 	enum l2tp_encap_type	encap;
130 
131 	/* Used only for kernel-created sockets */
132 	struct in_addr		local_ip;
133 	struct in_addr		peer_ip;
134 #if IS_ENABLED(CONFIG_IPV6)
135 	struct in6_addr		*local_ip6;
136 	struct in6_addr		*peer_ip6;
137 #endif
138 	u16			local_udp_port;
139 	u16			peer_udp_port;
140 	unsigned int		use_udp_checksums:1,
141 				udp6_zero_tx_checksums:1,
142 				udp6_zero_rx_checksums:1;
143 };
144 
145 struct l2tp_tunnel {
146 	int			magic;		/* Should be L2TP_TUNNEL_MAGIC */
147 
148 	unsigned long		dead;
149 
150 	struct rcu_head rcu;
151 	rwlock_t		hlist_lock;	/* protect session_hlist */
152 	bool			acpt_newsess;	/* Indicates whether this
153 						 * tunnel accepts new sessions.
154 						 * Protected by hlist_lock.
155 						 */
156 	struct hlist_head	session_hlist[L2TP_HASH_SIZE];
157 						/* hashed list of sessions,
158 						 * hashed by id */
159 	u32			tunnel_id;
160 	u32			peer_tunnel_id;
161 	int			version;	/* 2=>L2TPv2, 3=>L2TPv3 */
162 
163 	char			name[20];	/* for logging */
164 	int			debug;		/* bitmask of debug message
165 						 * categories */
166 	enum l2tp_encap_type	encap;
167 	struct l2tp_stats	stats;
168 
169 	struct list_head	list;		/* Keep a list of all tunnels */
170 	struct net		*l2tp_net;	/* the net we belong to */
171 
172 	refcount_t		ref_count;
173 	void (*old_sk_destruct)(struct sock *);
174 	struct sock		*sock;		/* Parent socket */
175 	int			fd;		/* Parent fd, if tunnel socket
176 						 * was created by userspace */
177 
178 	struct work_struct	del_work;
179 };
180 
181 struct l2tp_nl_cmd_ops {
182 	int (*session_create)(struct net *net, struct l2tp_tunnel *tunnel,
183 			      u32 session_id, u32 peer_session_id,
184 			      struct l2tp_session_cfg *cfg);
185 	int (*session_delete)(struct l2tp_session *session);
186 };
187 
188 static inline void *l2tp_session_priv(struct l2tp_session *session)
189 {
190 	return &session->priv[0];
191 }
192 
193 struct l2tp_tunnel *l2tp_tunnel_get(const struct net *net, u32 tunnel_id);
194 struct l2tp_tunnel *l2tp_tunnel_get_nth(const struct net *net, int nth);
195 
196 void l2tp_tunnel_free(struct l2tp_tunnel *tunnel);
197 
198 struct l2tp_session *l2tp_session_get(const struct net *net,
199 				      struct l2tp_tunnel *tunnel,
200 				      u32 session_id);
201 struct l2tp_session *l2tp_session_get_nth(struct l2tp_tunnel *tunnel, int nth);
202 struct l2tp_session *l2tp_session_get_by_ifname(const struct net *net,
203 						const char *ifname);
204 
205 int l2tp_tunnel_create(struct net *net, int fd, int version, u32 tunnel_id,
206 		       u32 peer_tunnel_id, struct l2tp_tunnel_cfg *cfg,
207 		       struct l2tp_tunnel **tunnelp);
208 int l2tp_tunnel_register(struct l2tp_tunnel *tunnel, struct net *net,
209 			 struct l2tp_tunnel_cfg *cfg);
210 
211 void l2tp_tunnel_delete(struct l2tp_tunnel *tunnel);
212 struct l2tp_session *l2tp_session_create(int priv_size,
213 					 struct l2tp_tunnel *tunnel,
214 					 u32 session_id, u32 peer_session_id,
215 					 struct l2tp_session_cfg *cfg);
216 int l2tp_session_register(struct l2tp_session *session,
217 			  struct l2tp_tunnel *tunnel);
218 
219 void __l2tp_session_unhash(struct l2tp_session *session);
220 int l2tp_session_delete(struct l2tp_session *session);
221 void l2tp_session_free(struct l2tp_session *session);
222 void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
223 		      unsigned char *ptr, unsigned char *optr, u16 hdrflags,
224 		      int length);
225 int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb);
226 void l2tp_session_set_header_len(struct l2tp_session *session, int version);
227 
228 int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb,
229 		  int hdr_len);
230 
231 int l2tp_nl_register_ops(enum l2tp_pwtype pw_type,
232 			 const struct l2tp_nl_cmd_ops *ops);
233 void l2tp_nl_unregister_ops(enum l2tp_pwtype pw_type);
234 int l2tp_ioctl(struct sock *sk, int cmd, unsigned long arg);
235 
236 static inline void l2tp_tunnel_inc_refcount(struct l2tp_tunnel *tunnel)
237 {
238 	refcount_inc(&tunnel->ref_count);
239 }
240 
241 static inline void l2tp_tunnel_dec_refcount(struct l2tp_tunnel *tunnel)
242 {
243 	if (refcount_dec_and_test(&tunnel->ref_count))
244 		l2tp_tunnel_free(tunnel);
245 }
246 
247 /* Session reference counts. Incremented when code obtains a reference
248  * to a session.
249  */
250 static inline void l2tp_session_inc_refcount(struct l2tp_session *session)
251 {
252 	refcount_inc(&session->ref_count);
253 }
254 
255 static inline void l2tp_session_dec_refcount(struct l2tp_session *session)
256 {
257 	if (refcount_dec_and_test(&session->ref_count))
258 		l2tp_session_free(session);
259 }
260 
261 static inline int l2tp_get_l2specific_len(struct l2tp_session *session)
262 {
263 	switch (session->l2specific_type) {
264 	case L2TP_L2SPECTYPE_DEFAULT:
265 		return 4;
266 	case L2TP_L2SPECTYPE_NONE:
267 	default:
268 		return 0;
269 	}
270 }
271 
272 static inline u32 l2tp_tunnel_dst_mtu(const struct l2tp_tunnel *tunnel)
273 {
274 	struct dst_entry *dst;
275 	u32 mtu;
276 
277 	dst = sk_dst_get(tunnel->sock);
278 	if (!dst)
279 		return 0;
280 
281 	mtu = dst_mtu(dst);
282 	dst_release(dst);
283 
284 	return mtu;
285 }
286 
287 #define l2tp_printk(ptr, type, func, fmt, ...)				\
288 do {									\
289 	if (((ptr)->debug) & (type))					\
290 		func(fmt, ##__VA_ARGS__);				\
291 } while (0)
292 
293 #define l2tp_warn(ptr, type, fmt, ...)					\
294 	l2tp_printk(ptr, type, pr_warn, fmt, ##__VA_ARGS__)
295 #define l2tp_info(ptr, type, fmt, ...)					\
296 	l2tp_printk(ptr, type, pr_info, fmt, ##__VA_ARGS__)
297 #define l2tp_dbg(ptr, type, fmt, ...)					\
298 	l2tp_printk(ptr, type, pr_debug, fmt, ##__VA_ARGS__)
299 
300 #define MODULE_ALIAS_L2TP_PWTYPE(type) \
301 	MODULE_ALIAS("net-l2tp-type-" __stringify(type))
302 
303 #endif /* _L2TP_CORE_H_ */
304