1d2912cb1SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
220dcb110STom Parkin /* L2TP internal definitions.
3fd558d18SJames Chapman *
4fd558d18SJames Chapman * Copyright (c) 2008,2009 Katalix Systems Ltd
5fd558d18SJames Chapman */
6fbea9e07SReshetova, Elena #include <linux/refcount.h>
7fd558d18SJames Chapman
8fd558d18SJames Chapman #ifndef _L2TP_CORE_H_
9fd558d18SJames Chapman #define _L2TP_CORE_H_
10fd558d18SJames Chapman
111f5cd2a0SGuillaume Nault #include <net/dst.h>
121f5cd2a0SGuillaume Nault #include <net/sock.h>
131f5cd2a0SGuillaume Nault
14d6a61ec9SGuillaume Nault #ifdef CONFIG_XFRM
15d6a61ec9SGuillaume Nault #include <net/xfrm.h>
16d6a61ec9SGuillaume Nault #endif
17d6a61ec9SGuillaume Nault
18340bb1acSTom Parkin /* Random numbers used for internal consistency checks of tunnel and session structures */
19fd558d18SJames Chapman #define L2TP_TUNNEL_MAGIC 0x42114DDA
20fd558d18SJames Chapman #define L2TP_SESSION_MAGIC 0x0C04EB7D
21fd558d18SJames Chapman
22340bb1acSTom Parkin /* Per tunnel session hash table size */
23fd558d18SJames Chapman #define L2TP_HASH_BITS 4
24dbf82f3fSTom Parkin #define L2TP_HASH_SIZE BIT(L2TP_HASH_BITS)
25fd558d18SJames Chapman
26340bb1acSTom Parkin /* System-wide session hash table size */
27f7faffa3SJames Chapman #define L2TP_HASH_BITS_2 8
28dbf82f3fSTom Parkin #define L2TP_HASH_SIZE_2 BIT(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;
423e59e885SMatthias Schiffer atomic_long_t rx_invalid;
43fd558d18SJames Chapman };
44fd558d18SJames Chapman
45fd558d18SJames Chapman struct l2tp_tunnel;
46fd558d18SJames Chapman
47340bb1acSTom Parkin /* L2TP session configuration */
48fd558d18SJames Chapman struct l2tp_session_cfg {
49f7faffa3SJames Chapman enum l2tp_pwtype pw_type;
5020dcb110STom Parkin unsigned int recv_seq:1; /* expect receive packets with sequence numbers? */
5120dcb110STom Parkin unsigned int send_seq:1; /* send packets with sequence numbers? */
5220dcb110STom Parkin unsigned int lns_mode:1; /* behave as LNS?
5320dcb110STom Parkin * LAC enables sequence numbers under LNS control.
5420dcb110STom Parkin */
55f7faffa3SJames Chapman u16 l2specific_type; /* Layer 2 specific type */
56f7faffa3SJames Chapman u8 cookie[8]; /* optional cookie */
57f7faffa3SJames Chapman int cookie_len; /* 0, 4 or 8 bytes */
58f7faffa3SJames Chapman u8 peer_cookie[8]; /* peer's cookie */
59f7faffa3SJames Chapman int peer_cookie_len; /* 0, 4 or 8 bytes */
6020dcb110STom Parkin int reorder_timeout; /* configured reorder timeout (in jiffies) */
61309795f4SJames Chapman char *ifname;
62fd558d18SJames Chapman };
63fd558d18SJames Chapman
64340bb1acSTom Parkin /* Represents a session (pseudowire) instance.
65340bb1acSTom Parkin * Tracks runtime state including cookies, dataplane packet sequencing, and IO statistics.
66340bb1acSTom Parkin * Is linked into a per-tunnel session hashlist; and in the case of an L2TPv3 session into
67340bb1acSTom Parkin * an additional per-net ("global") hashlist.
68340bb1acSTom Parkin */
692a03dd8eSTom Parkin #define L2TP_SESSION_NAME_MAX 32
70fd558d18SJames Chapman struct l2tp_session {
7120dcb110STom Parkin int magic; /* should be L2TP_SESSION_MAGIC */
72b228a940SGuillaume Nault long dead;
73fd558d18SJames Chapman
7420dcb110STom Parkin struct l2tp_tunnel *tunnel; /* back pointer to tunnel context */
75fd558d18SJames Chapman u32 session_id;
76fd558d18SJames Chapman u32 peer_session_id;
77f7faffa3SJames Chapman u8 cookie[8];
78f7faffa3SJames Chapman int cookie_len;
79f7faffa3SJames Chapman u8 peer_cookie[8];
80f7faffa3SJames Chapman int peer_cookie_len;
81f7faffa3SJames Chapman u16 l2specific_type;
82f7faffa3SJames Chapman u16 hdr_len;
83f7faffa3SJames Chapman u32 nr; /* session NR state (receive) */
84f7faffa3SJames Chapman u32 ns; /* session NR state (send) */
85fd558d18SJames Chapman struct sk_buff_head reorder_q; /* receive reorder queue */
868a1631d5SJames Chapman u32 nr_max; /* max NR. Depends on tunnel */
878a1631d5SJames Chapman u32 nr_window_size; /* NR window size */
88a0dbd822SJames Chapman u32 nr_oos; /* NR of last OOS packet */
8920dcb110STom Parkin int nr_oos_count; /* for OOS recovery */
90a0dbd822SJames Chapman int nr_oos_count_max;
9120dcb110STom Parkin struct hlist_node hlist; /* hash list node */
92f00c854cSReshetova, Elena refcount_t ref_count;
93fd558d18SJames Chapman
942a03dd8eSTom Parkin char name[L2TP_SESSION_NAME_MAX]; /* for logging */
95309795f4SJames Chapman char ifname[IFNAMSIZ];
9620dcb110STom Parkin unsigned int recv_seq:1; /* expect receive packets with sequence numbers? */
9720dcb110STom Parkin unsigned int send_seq:1; /* send packets with sequence numbers? */
9820dcb110STom Parkin unsigned int lns_mode:1; /* behave as LNS?
9920dcb110STom Parkin * LAC enables sequence numbers under LNS control.
10020dcb110STom Parkin */
10120dcb110STom Parkin int reorder_timeout; /* configured reorder timeout (in jiffies) */
10238d40b3fSJames Chapman int reorder_skip; /* set if skip to next nr */
103f7faffa3SJames Chapman enum l2tp_pwtype pwtype;
104fd558d18SJames Chapman struct l2tp_stats stats;
10520dcb110STom Parkin struct hlist_node global_hlist; /* global hash list node */
106fd558d18SJames Chapman
107340bb1acSTom Parkin /* Session receive handler for data packets.
108340bb1acSTom Parkin * Each pseudowire implementation should implement this callback in order to
109340bb1acSTom Parkin * handle incoming packets. Packets are passed to the pseudowire handler after
110340bb1acSTom Parkin * reordering, if data sequence numbers are enabled for the session.
111340bb1acSTom Parkin */
112fd558d18SJames Chapman void (*recv_skb)(struct l2tp_session *session, struct sk_buff *skb, int data_len);
113340bb1acSTom Parkin
114340bb1acSTom Parkin /* Session close handler.
115340bb1acSTom Parkin * Each pseudowire implementation may implement this callback in order to carry
116340bb1acSTom Parkin * out pseudowire-specific shutdown actions.
117340bb1acSTom Parkin * The callback is called by core after unhashing the session and purging its
118340bb1acSTom Parkin * reorder queue.
119340bb1acSTom Parkin */
120fd558d18SJames Chapman void (*session_close)(struct l2tp_session *session);
121340bb1acSTom Parkin
122340bb1acSTom Parkin /* Session show handler.
123340bb1acSTom Parkin * Pseudowire-specific implementation of debugfs session rendering.
124340bb1acSTom Parkin * The callback is called by l2tp_debugfs.c after rendering core session
125340bb1acSTom Parkin * information.
126340bb1acSTom Parkin */
1270ad66140SJames Chapman void (*show)(struct seq_file *m, void *priv);
128340bb1acSTom Parkin
129af71b090SGustavo A. R. Silva u8 priv[]; /* private data */
130fd558d18SJames Chapman };
131fd558d18SJames Chapman
132340bb1acSTom Parkin /* L2TP tunnel configuration */
133fd558d18SJames Chapman struct l2tp_tunnel_cfg {
1340d76751fSJames Chapman enum l2tp_encap_type encap;
135789a4a2cSJames Chapman
136789a4a2cSJames Chapman /* Used only for kernel-created sockets */
137789a4a2cSJames Chapman struct in_addr local_ip;
138789a4a2cSJames Chapman struct in_addr peer_ip;
139f9bac8dfSChris Elston #if IS_ENABLED(CONFIG_IPV6)
140f9bac8dfSChris Elston struct in6_addr *local_ip6;
141f9bac8dfSChris Elston struct in6_addr *peer_ip6;
142f9bac8dfSChris Elston #endif
143789a4a2cSJames Chapman u16 local_udp_port;
144789a4a2cSJames Chapman u16 peer_udp_port;
1456b649feaSTom Herbert unsigned int use_udp_checksums:1,
1466b649feaSTom Herbert udp6_zero_tx_checksums:1,
1476b649feaSTom Herbert udp6_zero_rx_checksums:1;
148fd558d18SJames Chapman };
149fd558d18SJames Chapman
150340bb1acSTom Parkin /* Represents a tunnel instance.
151340bb1acSTom Parkin * Tracks runtime state including IO statistics.
152340bb1acSTom Parkin * Holds the tunnel socket (either passed from userspace or directly created by the kernel).
153340bb1acSTom Parkin * Maintains a hashlist of sessions belonging to the tunnel instance.
154340bb1acSTom Parkin * Is linked into a per-net list of tunnels.
155340bb1acSTom Parkin */
1562a03dd8eSTom Parkin #define L2TP_TUNNEL_NAME_MAX 20
157fd558d18SJames Chapman struct l2tp_tunnel {
158fd558d18SJames Chapman int magic; /* Should be L2TP_TUNNEL_MAGIC */
15962b982eeSSabrina Dubroca
16062b982eeSSabrina Dubroca unsigned long dead;
16162b982eeSSabrina Dubroca
16299469c32Sxeb@mail.ru struct rcu_head rcu;
16307b8ca37STom Parkin spinlock_t hlist_lock; /* write-protection for session_hlist */
16420dcb110STom Parkin bool acpt_newsess; /* indicates whether this tunnel accepts
16520dcb110STom Parkin * new sessions. Protected by hlist_lock.
166f3c66d4eSGuillaume Nault */
167fd558d18SJames Chapman struct hlist_head session_hlist[L2TP_HASH_SIZE];
16820dcb110STom Parkin /* hashed list of sessions, hashed by id */
169fd558d18SJames Chapman u32 tunnel_id;
170fd558d18SJames Chapman u32 peer_tunnel_id;
171fd558d18SJames Chapman int version; /* 2=>L2TPv2, 3=>L2TPv3 */
172fd558d18SJames Chapman
1732a03dd8eSTom Parkin char name[L2TP_TUNNEL_NAME_MAX]; /* for logging */
1740d76751fSJames Chapman enum l2tp_encap_type encap;
175fd558d18SJames Chapman struct l2tp_stats stats;
176fd558d18SJames Chapman
17720dcb110STom Parkin struct list_head list; /* list node on per-namespace list of tunnels */
178fd558d18SJames Chapman struct net *l2tp_net; /* the net we belong to */
179fd558d18SJames Chapman
180fbea9e07SReshetova, Elena refcount_t ref_count;
181bef04d16STom Parkin void (*old_sk_destruct)(struct sock *sk);
18220dcb110STom Parkin struct sock *sock; /* parent socket */
18320dcb110STom Parkin int fd; /* parent fd, if tunnel socket was created
18420dcb110STom Parkin * by userspace
18520dcb110STom Parkin */
186fd558d18SJames Chapman
187f8ccac0eSTom Parkin struct work_struct del_work;
188fd558d18SJames Chapman };
189fd558d18SJames Chapman
190340bb1acSTom Parkin /* Pseudowire ops callbacks for use with the l2tp genetlink interface */
191309795f4SJames Chapman struct l2tp_nl_cmd_ops {
192340bb1acSTom Parkin /* The pseudowire session create callback is responsible for creating a session
193340bb1acSTom Parkin * instance for a specific pseudowire type.
194340bb1acSTom Parkin * It must call l2tp_session_create and l2tp_session_register to register the
195340bb1acSTom Parkin * session instance, as well as carry out any pseudowire-specific initialisation.
196340bb1acSTom Parkin * It must return >= 0 on success, or an appropriate negative errno value on failure.
197340bb1acSTom Parkin */
198f026bc29SGuillaume Nault int (*session_create)(struct net *net, struct l2tp_tunnel *tunnel,
199f026bc29SGuillaume Nault u32 session_id, u32 peer_session_id,
200f026bc29SGuillaume Nault struct l2tp_session_cfg *cfg);
201340bb1acSTom Parkin
202340bb1acSTom Parkin /* The pseudowire session delete callback is responsible for initiating the deletion
203340bb1acSTom Parkin * of a session instance.
204340bb1acSTom Parkin * It must call l2tp_session_delete, as well as carry out any pseudowire-specific
205340bb1acSTom Parkin * teardown actions.
206340bb1acSTom Parkin */
207628703f5STom Parkin void (*session_delete)(struct l2tp_session *session);
208309795f4SJames Chapman };
209309795f4SJames Chapman
l2tp_session_priv(struct l2tp_session * session)210fd558d18SJames Chapman static inline void *l2tp_session_priv(struct l2tp_session *session)
211fd558d18SJames Chapman {
212fd558d18SJames Chapman return &session->priv[0];
213fd558d18SJames Chapman }
214fd558d18SJames Chapman
215340bb1acSTom Parkin /* Tunnel and session refcounts */
21652016e25STom Parkin void l2tp_tunnel_inc_refcount(struct l2tp_tunnel *tunnel);
21752016e25STom Parkin void l2tp_tunnel_dec_refcount(struct l2tp_tunnel *tunnel);
21852016e25STom Parkin void l2tp_session_inc_refcount(struct l2tp_session *session);
21952016e25STom Parkin void l2tp_session_dec_refcount(struct l2tp_session *session);
22052016e25STom Parkin
221340bb1acSTom Parkin /* Tunnel and session lookup.
222340bb1acSTom Parkin * These functions take a reference on the instances they return, so
223340bb1acSTom Parkin * the caller must ensure that the reference is dropped appropriately.
224340bb1acSTom Parkin */
22554652eb1SGuillaume Nault struct l2tp_tunnel *l2tp_tunnel_get(const struct net *net, u32 tunnel_id);
2265846c131SGuillaume Nault struct l2tp_tunnel *l2tp_tunnel_get_nth(const struct net *net, int nth);
22701e28b92SGuillaume Nault struct l2tp_session *l2tp_tunnel_get_session(struct l2tp_tunnel *tunnel,
22801e28b92SGuillaume Nault u32 session_id);
2295846c131SGuillaume Nault
23001e28b92SGuillaume Nault struct l2tp_session *l2tp_session_get(const struct net *net, u32 session_id);
231a4346210SGuillaume Nault struct l2tp_session *l2tp_session_get_nth(struct l2tp_tunnel *tunnel, int nth);
2329aaef50cSGuillaume Nault struct l2tp_session *l2tp_session_get_by_ifname(const struct net *net,
233a4346210SGuillaume Nault const char *ifname);
234fd558d18SJames Chapman
235340bb1acSTom Parkin /* Tunnel and session lifetime management.
236340bb1acSTom Parkin * Creation of a new instance is a two-step process: create, then register.
237340bb1acSTom Parkin * Destruction is triggered using the *_delete functions, and completes asynchronously.
238340bb1acSTom Parkin */
239c9ccd4c6STom Parkin int l2tp_tunnel_create(int fd, int version, u32 tunnel_id,
240c1b1203dSJoe Perches u32 peer_tunnel_id, struct l2tp_tunnel_cfg *cfg,
241c1b1203dSJoe Perches struct l2tp_tunnel **tunnelp);
2426b9f3423SGuillaume Nault int l2tp_tunnel_register(struct l2tp_tunnel *tunnel, struct net *net,
2436b9f3423SGuillaume Nault struct l2tp_tunnel_cfg *cfg);
24462b982eeSSabrina Dubroca void l2tp_tunnel_delete(struct l2tp_tunnel *tunnel);
245340bb1acSTom Parkin
246c1b1203dSJoe Perches struct l2tp_session *l2tp_session_create(int priv_size,
247c1b1203dSJoe Perches struct l2tp_tunnel *tunnel,
248c1b1203dSJoe Perches u32 session_id, u32 peer_session_id,
249c1b1203dSJoe Perches struct l2tp_session_cfg *cfg);
2503953ae7bSGuillaume Nault int l2tp_session_register(struct l2tp_session *session,
2513953ae7bSGuillaume Nault struct l2tp_tunnel *tunnel);
252628703f5STom Parkin void l2tp_session_delete(struct l2tp_session *session);
253340bb1acSTom Parkin
254340bb1acSTom Parkin /* Receive path helpers. If data sequencing is enabled for the session these
255340bb1acSTom Parkin * functions handle queuing and reordering prior to passing packets to the
256340bb1acSTom Parkin * pseudowire code to be passed to userspace.
257340bb1acSTom Parkin */
258c1b1203dSJoe Perches void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
259c1b1203dSJoe Perches unsigned char *ptr, unsigned char *optr, u16 hdrflags,
2602b139e6bSGuillaume Nault int length);
261c1b1203dSJoe Perches int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb);
262fd558d18SJames Chapman
263340bb1acSTom Parkin /* Transmit path helpers for sending packets over the tunnel socket. */
264340bb1acSTom Parkin void l2tp_session_set_header_len(struct l2tp_session *session, int version);
265efe05278STom Parkin int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb);
266fd558d18SJames Chapman
267340bb1acSTom Parkin /* Pseudowire management.
268340bb1acSTom Parkin * Pseudowires should register with l2tp core on module init, and unregister
269340bb1acSTom Parkin * on module exit.
270340bb1acSTom Parkin */
271340bb1acSTom Parkin int l2tp_nl_register_ops(enum l2tp_pwtype pw_type, const struct l2tp_nl_cmd_ops *ops);
272c1b1203dSJoe Perches void l2tp_nl_unregister_ops(enum l2tp_pwtype pw_type);
273340bb1acSTom Parkin
274340bb1acSTom Parkin /* IOCTL helper for IP encap modules. */
275*e1d001faSBreno Leitao int l2tp_ioctl(struct sock *sk, int cmd, int *karg);
276309795f4SJames Chapman
27745faeff1STom Parkin /* Extract the tunnel structure from a socket's sk_user_data pointer,
27845faeff1STom Parkin * validating the tunnel magic feather.
27945faeff1STom Parkin */
28045faeff1STom Parkin struct l2tp_tunnel *l2tp_sk_to_tunnel(struct sock *sk);
28145faeff1STom Parkin
l2tp_get_l2specific_len(struct l2tp_session * session)28262e7b6a5SLorenzo Bianconi static inline int l2tp_get_l2specific_len(struct l2tp_session *session)
28362e7b6a5SLorenzo Bianconi {
28462e7b6a5SLorenzo Bianconi switch (session->l2specific_type) {
28562e7b6a5SLorenzo Bianconi case L2TP_L2SPECTYPE_DEFAULT:
28662e7b6a5SLorenzo Bianconi return 4;
28762e7b6a5SLorenzo Bianconi case L2TP_L2SPECTYPE_NONE:
28862e7b6a5SLorenzo Bianconi default:
28962e7b6a5SLorenzo Bianconi return 0;
29062e7b6a5SLorenzo Bianconi }
29162e7b6a5SLorenzo Bianconi }
29262e7b6a5SLorenzo Bianconi
l2tp_tunnel_dst_mtu(const struct l2tp_tunnel * tunnel)2931f5cd2a0SGuillaume Nault static inline u32 l2tp_tunnel_dst_mtu(const struct l2tp_tunnel *tunnel)
2941f5cd2a0SGuillaume Nault {
2951f5cd2a0SGuillaume Nault struct dst_entry *dst;
2961f5cd2a0SGuillaume Nault u32 mtu;
2971f5cd2a0SGuillaume Nault
2981f5cd2a0SGuillaume Nault dst = sk_dst_get(tunnel->sock);
2991f5cd2a0SGuillaume Nault if (!dst)
3001f5cd2a0SGuillaume Nault return 0;
3011f5cd2a0SGuillaume Nault
3021f5cd2a0SGuillaume Nault mtu = dst_mtu(dst);
3031f5cd2a0SGuillaume Nault dst_release(dst);
3041f5cd2a0SGuillaume Nault
3051f5cd2a0SGuillaume Nault return mtu;
3061f5cd2a0SGuillaume Nault }
3071f5cd2a0SGuillaume Nault
308d6a61ec9SGuillaume Nault #ifdef CONFIG_XFRM
l2tp_tunnel_uses_xfrm(const struct l2tp_tunnel * tunnel)309d6a61ec9SGuillaume Nault static inline bool l2tp_tunnel_uses_xfrm(const struct l2tp_tunnel *tunnel)
310d6a61ec9SGuillaume Nault {
311d6a61ec9SGuillaume Nault struct sock *sk = tunnel->sock;
312d6a61ec9SGuillaume Nault
313d6a61ec9SGuillaume Nault return sk && (rcu_access_pointer(sk->sk_policy[0]) ||
314d6a61ec9SGuillaume Nault rcu_access_pointer(sk->sk_policy[1]));
315d6a61ec9SGuillaume Nault }
316d6a61ec9SGuillaume Nault #else
l2tp_tunnel_uses_xfrm(const struct l2tp_tunnel * tunnel)317d6a61ec9SGuillaume Nault static inline bool l2tp_tunnel_uses_xfrm(const struct l2tp_tunnel *tunnel)
318d6a61ec9SGuillaume Nault {
319d6a61ec9SGuillaume Nault return false;
320d6a61ec9SGuillaume Nault }
321d6a61ec9SGuillaume Nault #endif
322d6a61ec9SGuillaume Nault
l2tp_v3_ensure_opt_in_linear(struct l2tp_session * session,struct sk_buff * skb,unsigned char ** ptr,unsigned char ** optr)3234522a70dSJacob Wen static inline int l2tp_v3_ensure_opt_in_linear(struct l2tp_session *session, struct sk_buff *skb,
3244522a70dSJacob Wen unsigned char **ptr, unsigned char **optr)
3254522a70dSJacob Wen {
3264522a70dSJacob Wen int opt_len = session->peer_cookie_len + l2tp_get_l2specific_len(session);
3274522a70dSJacob Wen
3284522a70dSJacob Wen if (opt_len > 0) {
3294522a70dSJacob Wen int off = *ptr - *optr;
3304522a70dSJacob Wen
3314522a70dSJacob Wen if (!pskb_may_pull(skb, off + opt_len))
3324522a70dSJacob Wen return -1;
3334522a70dSJacob Wen
3344522a70dSJacob Wen if (skb->data != *optr) {
3354522a70dSJacob Wen *optr = skb->data;
3364522a70dSJacob Wen *ptr = skb->data + off;
3374522a70dSJacob Wen }
3384522a70dSJacob Wen }
3394522a70dSJacob Wen
3404522a70dSJacob Wen return 0;
3414522a70dSJacob Wen }
3424522a70dSJacob Wen
343f1f39f91Sstephen hemminger #define MODULE_ALIAS_L2TP_PWTYPE(type) \
344f1f39f91Sstephen hemminger MODULE_ALIAS("net-l2tp-type-" __stringify(type))
345f1f39f91Sstephen hemminger
346fd558d18SJames Chapman #endif /* _L2TP_CORE_H_ */
347