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