l2tp: capture more tx errors in data plane stats
[linux-2.6-block.git] / net / l2tp / l2tp_core.h
CommitLineData
d2912cb1 1/* SPDX-License-Identifier: GPL-2.0-only */
20dcb110 2/* L2TP internal definitions.
fd558d18
JC
3 *
4 * Copyright (c) 2008,2009 Katalix Systems Ltd
fd558d18 5 */
fbea9e07 6#include <linux/refcount.h>
fd558d18
JC
7
8#ifndef _L2TP_CORE_H_
9#define _L2TP_CORE_H_
10
1f5cd2a0
GN
11#include <net/dst.h>
12#include <net/sock.h>
13
d6a61ec9
GN
14#ifdef CONFIG_XFRM
15#include <net/xfrm.h>
16#endif
17
340bb1ac 18/* Random numbers used for internal consistency checks of tunnel and session structures */
fd558d18
JC
19#define L2TP_TUNNEL_MAGIC 0x42114DDA
20#define L2TP_SESSION_MAGIC 0x0C04EB7D
21
340bb1ac 22/* Per tunnel session hash table size */
fd558d18 23#define L2TP_HASH_BITS 4
dbf82f3f 24#define L2TP_HASH_SIZE BIT(L2TP_HASH_BITS)
fd558d18 25
340bb1ac 26/* System-wide session hash table size */
f7faffa3 27#define L2TP_HASH_BITS_2 8
dbf82f3f 28#define L2TP_HASH_SIZE_2 BIT(L2TP_HASH_BITS_2)
f7faffa3 29
fd558d18
JC
30struct sk_buff;
31
32struct l2tp_stats {
7b7c0719
TP
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;
fd558d18
JC
42};
43
44struct l2tp_tunnel;
45
340bb1ac 46/* L2TP session configuration */
fd558d18 47struct l2tp_session_cfg {
f7faffa3 48 enum l2tp_pwtype pw_type;
20dcb110
TP
49 unsigned int recv_seq:1; /* expect receive packets with sequence numbers? */
50 unsigned int send_seq:1; /* send packets with sequence numbers? */
51 unsigned int lns_mode:1; /* behave as LNS?
52 * LAC enables sequence numbers under LNS control.
53 */
f7faffa3
JC
54 u16 l2specific_type; /* Layer 2 specific type */
55 u8 cookie[8]; /* optional cookie */
56 int cookie_len; /* 0, 4 or 8 bytes */
57 u8 peer_cookie[8]; /* peer's cookie */
58 int peer_cookie_len; /* 0, 4 or 8 bytes */
20dcb110 59 int reorder_timeout; /* configured reorder timeout (in jiffies) */
309795f4 60 char *ifname;
fd558d18
JC
61};
62
340bb1ac
TP
63/* Represents a session (pseudowire) instance.
64 * Tracks runtime state including cookies, dataplane packet sequencing, and IO statistics.
65 * Is linked into a per-tunnel session hashlist; and in the case of an L2TPv3 session into
66 * an additional per-net ("global") hashlist.
67 */
2a03dd8e 68#define L2TP_SESSION_NAME_MAX 32
fd558d18 69struct l2tp_session {
20dcb110 70 int magic; /* should be L2TP_SESSION_MAGIC */
b228a940 71 long dead;
fd558d18 72
20dcb110 73 struct l2tp_tunnel *tunnel; /* back pointer to tunnel context */
fd558d18
JC
74 u32 session_id;
75 u32 peer_session_id;
f7faffa3
JC
76 u8 cookie[8];
77 int cookie_len;
78 u8 peer_cookie[8];
79 int peer_cookie_len;
f7faffa3
JC
80 u16 l2specific_type;
81 u16 hdr_len;
82 u32 nr; /* session NR state (receive) */
83 u32 ns; /* session NR state (send) */
fd558d18 84 struct sk_buff_head reorder_q; /* receive reorder queue */
8a1631d5
JC
85 u32 nr_max; /* max NR. Depends on tunnel */
86 u32 nr_window_size; /* NR window size */
a0dbd822 87 u32 nr_oos; /* NR of last OOS packet */
20dcb110 88 int nr_oos_count; /* for OOS recovery */
a0dbd822 89 int nr_oos_count_max;
20dcb110 90 struct hlist_node hlist; /* hash list node */
f00c854c 91 refcount_t ref_count;
fd558d18 92
2a03dd8e 93 char name[L2TP_SESSION_NAME_MAX]; /* for logging */
309795f4 94 char ifname[IFNAMSIZ];
20dcb110
TP
95 unsigned int recv_seq:1; /* expect receive packets with sequence numbers? */
96 unsigned int send_seq:1; /* send packets with sequence numbers? */
97 unsigned int lns_mode:1; /* behave as LNS?
98 * LAC enables sequence numbers under LNS control.
99 */
20dcb110 100 int reorder_timeout; /* configured reorder timeout (in jiffies) */
38d40b3f 101 int reorder_skip; /* set if skip to next nr */
f7faffa3 102 enum l2tp_pwtype pwtype;
fd558d18 103 struct l2tp_stats stats;
20dcb110 104 struct hlist_node global_hlist; /* global hash list node */
fd558d18 105
340bb1ac
TP
106 /* Session receive handler for data packets.
107 * Each pseudowire implementation should implement this callback in order to
108 * handle incoming packets. Packets are passed to the pseudowire handler after
109 * reordering, if data sequence numbers are enabled for the session.
110 */
fd558d18 111 void (*recv_skb)(struct l2tp_session *session, struct sk_buff *skb, int data_len);
340bb1ac
TP
112
113 /* Session close handler.
114 * Each pseudowire implementation may implement this callback in order to carry
115 * out pseudowire-specific shutdown actions.
116 * The callback is called by core after unhashing the session and purging its
117 * reorder queue.
118 */
fd558d18 119 void (*session_close)(struct l2tp_session *session);
340bb1ac
TP
120
121 /* Session show handler.
122 * Pseudowire-specific implementation of debugfs session rendering.
123 * The callback is called by l2tp_debugfs.c after rendering core session
124 * information.
125 */
0ad66140 126 void (*show)(struct seq_file *m, void *priv);
340bb1ac 127
20dcb110 128 u8 priv[]; /* private data */
fd558d18
JC
129};
130
340bb1ac 131/* L2TP tunnel configuration */
fd558d18 132struct l2tp_tunnel_cfg {
0d76751f 133 enum l2tp_encap_type encap;
789a4a2c
JC
134
135 /* Used only for kernel-created sockets */
136 struct in_addr local_ip;
137 struct in_addr peer_ip;
f9bac8df
CE
138#if IS_ENABLED(CONFIG_IPV6)
139 struct in6_addr *local_ip6;
140 struct in6_addr *peer_ip6;
141#endif
789a4a2c
JC
142 u16 local_udp_port;
143 u16 peer_udp_port;
6b649fea
TH
144 unsigned int use_udp_checksums:1,
145 udp6_zero_tx_checksums:1,
146 udp6_zero_rx_checksums:1;
fd558d18
JC
147};
148
340bb1ac
TP
149/* Represents a tunnel instance.
150 * Tracks runtime state including IO statistics.
151 * Holds the tunnel socket (either passed from userspace or directly created by the kernel).
152 * Maintains a hashlist of sessions belonging to the tunnel instance.
153 * Is linked into a per-net list of tunnels.
154 */
2a03dd8e 155#define L2TP_TUNNEL_NAME_MAX 20
fd558d18
JC
156struct l2tp_tunnel {
157 int magic; /* Should be L2TP_TUNNEL_MAGIC */
62b982ee
SD
158
159 unsigned long dead;
160
99469c32 161 struct rcu_head rcu;
fd558d18 162 rwlock_t hlist_lock; /* protect session_hlist */
20dcb110
TP
163 bool acpt_newsess; /* indicates whether this tunnel accepts
164 * new sessions. Protected by hlist_lock.
f3c66d4e 165 */
fd558d18 166 struct hlist_head session_hlist[L2TP_HASH_SIZE];
20dcb110 167 /* hashed list of sessions, hashed by id */
fd558d18
JC
168 u32 tunnel_id;
169 u32 peer_tunnel_id;
170 int version; /* 2=>L2TPv2, 3=>L2TPv3 */
171
2a03dd8e 172 char name[L2TP_TUNNEL_NAME_MAX]; /* for logging */
0d76751f 173 enum l2tp_encap_type encap;
fd558d18
JC
174 struct l2tp_stats stats;
175
20dcb110 176 struct list_head list; /* list node on per-namespace list of tunnels */
fd558d18
JC
177 struct net *l2tp_net; /* the net we belong to */
178
fbea9e07 179 refcount_t ref_count;
bef04d16 180 void (*old_sk_destruct)(struct sock *sk);
20dcb110
TP
181 struct sock *sock; /* parent socket */
182 int fd; /* parent fd, if tunnel socket was created
183 * by userspace
184 */
fd558d18 185
f8ccac0e 186 struct work_struct del_work;
fd558d18
JC
187};
188
340bb1ac 189/* Pseudowire ops callbacks for use with the l2tp genetlink interface */
309795f4 190struct l2tp_nl_cmd_ops {
340bb1ac
TP
191 /* The pseudowire session create callback is responsible for creating a session
192 * instance for a specific pseudowire type.
193 * It must call l2tp_session_create and l2tp_session_register to register the
194 * session instance, as well as carry out any pseudowire-specific initialisation.
195 * It must return >= 0 on success, or an appropriate negative errno value on failure.
196 */
f026bc29
GN
197 int (*session_create)(struct net *net, struct l2tp_tunnel *tunnel,
198 u32 session_id, u32 peer_session_id,
199 struct l2tp_session_cfg *cfg);
340bb1ac
TP
200
201 /* The pseudowire session delete callback is responsible for initiating the deletion
202 * of a session instance.
203 * It must call l2tp_session_delete, as well as carry out any pseudowire-specific
204 * teardown actions.
205 */
628703f5 206 void (*session_delete)(struct l2tp_session *session);
309795f4
JC
207};
208
fd558d18
JC
209static inline void *l2tp_session_priv(struct l2tp_session *session)
210{
211 return &session->priv[0];
212}
213
340bb1ac 214/* Tunnel and session refcounts */
52016e25
TP
215void l2tp_tunnel_inc_refcount(struct l2tp_tunnel *tunnel);
216void l2tp_tunnel_dec_refcount(struct l2tp_tunnel *tunnel);
217void l2tp_session_inc_refcount(struct l2tp_session *session);
218void l2tp_session_dec_refcount(struct l2tp_session *session);
219
340bb1ac
TP
220/* Tunnel and session lookup.
221 * These functions take a reference on the instances they return, so
222 * the caller must ensure that the reference is dropped appropriately.
223 */
54652eb1 224struct l2tp_tunnel *l2tp_tunnel_get(const struct net *net, u32 tunnel_id);
5846c131 225struct l2tp_tunnel *l2tp_tunnel_get_nth(const struct net *net, int nth);
01e28b92
GN
226struct l2tp_session *l2tp_tunnel_get_session(struct l2tp_tunnel *tunnel,
227 u32 session_id);
5846c131 228
01e28b92 229struct l2tp_session *l2tp_session_get(const struct net *net, u32 session_id);
a4346210 230struct l2tp_session *l2tp_session_get_nth(struct l2tp_tunnel *tunnel, int nth);
9aaef50c 231struct l2tp_session *l2tp_session_get_by_ifname(const struct net *net,
a4346210 232 const char *ifname);
c1b1203d 233
340bb1ac
TP
234/* Tunnel and session lifetime management.
235 * Creation of a new instance is a two-step process: create, then register.
236 * Destruction is triggered using the *_delete functions, and completes asynchronously.
237 */
c9ccd4c6 238int l2tp_tunnel_create(int fd, int version, u32 tunnel_id,
c1b1203d
JP
239 u32 peer_tunnel_id, struct l2tp_tunnel_cfg *cfg,
240 struct l2tp_tunnel **tunnelp);
6b9f3423
GN
241int l2tp_tunnel_register(struct l2tp_tunnel *tunnel, struct net *net,
242 struct l2tp_tunnel_cfg *cfg);
62b982ee 243void l2tp_tunnel_delete(struct l2tp_tunnel *tunnel);
340bb1ac 244
c1b1203d
JP
245struct l2tp_session *l2tp_session_create(int priv_size,
246 struct l2tp_tunnel *tunnel,
247 u32 session_id, u32 peer_session_id,
248 struct l2tp_session_cfg *cfg);
3953ae7b
GN
249int l2tp_session_register(struct l2tp_session *session,
250 struct l2tp_tunnel *tunnel);
628703f5 251void l2tp_session_delete(struct l2tp_session *session);
340bb1ac
TP
252
253/* Receive path helpers. If data sequencing is enabled for the session these
254 * functions handle queuing and reordering prior to passing packets to the
255 * pseudowire code to be passed to userspace.
256 */
c1b1203d
JP
257void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
258 unsigned char *ptr, unsigned char *optr, u16 hdrflags,
2b139e6b 259 int length);
c1b1203d
JP
260int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb);
261
340bb1ac
TP
262/* Transmit path helpers for sending packets over the tunnel socket. */
263void l2tp_session_set_header_len(struct l2tp_session *session, int version);
efe05278 264int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb);
c1b1203d 265
340bb1ac
TP
266/* Pseudowire management.
267 * Pseudowires should register with l2tp core on module init, and unregister
268 * on module exit.
269 */
270int l2tp_nl_register_ops(enum l2tp_pwtype pw_type, const struct l2tp_nl_cmd_ops *ops);
c1b1203d 271void l2tp_nl_unregister_ops(enum l2tp_pwtype pw_type);
340bb1ac
TP
272
273/* IOCTL helper for IP encap modules. */
72fb96e7 274int l2tp_ioctl(struct sock *sk, int cmd, unsigned long arg);
309795f4 275
62e7b6a5
LB
276static inline int l2tp_get_l2specific_len(struct l2tp_session *session)
277{
278 switch (session->l2specific_type) {
279 case L2TP_L2SPECTYPE_DEFAULT:
280 return 4;
281 case L2TP_L2SPECTYPE_NONE:
282 default:
283 return 0;
284 }
285}
286
1f5cd2a0
GN
287static inline u32 l2tp_tunnel_dst_mtu(const struct l2tp_tunnel *tunnel)
288{
289 struct dst_entry *dst;
290 u32 mtu;
291
292 dst = sk_dst_get(tunnel->sock);
293 if (!dst)
294 return 0;
295
296 mtu = dst_mtu(dst);
297 dst_release(dst);
298
299 return mtu;
300}
301
d6a61ec9
GN
302#ifdef CONFIG_XFRM
303static inline bool l2tp_tunnel_uses_xfrm(const struct l2tp_tunnel *tunnel)
304{
305 struct sock *sk = tunnel->sock;
306
307 return sk && (rcu_access_pointer(sk->sk_policy[0]) ||
308 rcu_access_pointer(sk->sk_policy[1]));
309}
310#else
311static inline bool l2tp_tunnel_uses_xfrm(const struct l2tp_tunnel *tunnel)
312{
313 return false;
314}
315#endif
316
4522a70d
JW
317static inline int l2tp_v3_ensure_opt_in_linear(struct l2tp_session *session, struct sk_buff *skb,
318 unsigned char **ptr, unsigned char **optr)
319{
320 int opt_len = session->peer_cookie_len + l2tp_get_l2specific_len(session);
321
322 if (opt_len > 0) {
323 int off = *ptr - *optr;
324
325 if (!pskb_may_pull(skb, off + opt_len))
326 return -1;
327
328 if (skb->data != *optr) {
329 *optr = skb->data;
330 *ptr = skb->data + off;
331 }
332 }
333
334 return 0;
335}
336
f1f39f91 337#define MODULE_ALIAS_L2TP_PWTYPE(type) \
338 MODULE_ALIAS("net-l2tp-type-" __stringify(type))
339
fd558d18 340#endif /* _L2TP_CORE_H_ */