This source file includes following definitions.
- l2tp_session_priv
- l2tp_tunnel_inc_refcount
- l2tp_tunnel_dec_refcount
- l2tp_session_inc_refcount
- l2tp_session_dec_refcount
- l2tp_get_l2specific_len
- l2tp_tunnel_dst_mtu
- l2tp_tunnel_uses_xfrm
- l2tp_tunnel_uses_xfrm
- l2tp_v3_ensure_opt_in_linear
1
2
3
4
5
6
7 #include <linux/refcount.h>
8
9 #ifndef _L2TP_CORE_H_
10 #define _L2TP_CORE_H_
11
12 #include <net/dst.h>
13 #include <net/sock.h>
14
15 #ifdef CONFIG_XFRM
16 #include <net/xfrm.h>
17 #endif
18
19
20 #define L2TP_TUNNEL_MAGIC 0x42114DDA
21 #define L2TP_SESSION_MAGIC 0x0C04EB7D
22
23
24 #define L2TP_HASH_BITS 4
25 #define L2TP_HASH_SIZE (1 << L2TP_HASH_BITS)
26
27
28 #define L2TP_HASH_BITS_2 8
29 #define L2TP_HASH_SIZE_2 (1 << L2TP_HASH_BITS_2)
30
31 struct sk_buff;
32
33 struct l2tp_stats {
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;
43 };
44
45 struct l2tp_tunnel;
46
47
48
49
50 struct l2tp_session_cfg {
51 enum l2tp_pwtype pw_type;
52 unsigned int recv_seq:1;
53
54 unsigned int send_seq:1;
55
56 unsigned int lns_mode:1;
57
58
59 int debug;
60
61 u16 l2specific_type;
62 u8 cookie[8];
63 int cookie_len;
64 u8 peer_cookie[8];
65 int peer_cookie_len;
66 int reorder_timeout;
67
68 char *ifname;
69 };
70
71 struct l2tp_session {
72 int magic;
73
74 long dead;
75
76 struct l2tp_tunnel *tunnel;
77
78 u32 session_id;
79 u32 peer_session_id;
80 u8 cookie[8];
81 int cookie_len;
82 u8 peer_cookie[8];
83 int peer_cookie_len;
84 u16 l2specific_type;
85 u16 hdr_len;
86 u32 nr;
87 u32 ns;
88 struct sk_buff_head reorder_q;
89 u32 nr_max;
90 u32 nr_window_size;
91 u32 nr_oos;
92 int nr_oos_count;
93 int nr_oos_count_max;
94 struct hlist_node hlist;
95 refcount_t ref_count;
96
97 char name[32];
98 char ifname[IFNAMSIZ];
99 unsigned int recv_seq:1;
100
101 unsigned int send_seq:1;
102
103 unsigned int lns_mode:1;
104
105
106 int debug;
107
108 int reorder_timeout;
109
110 int reorder_skip;
111 enum l2tp_pwtype pwtype;
112 struct l2tp_stats stats;
113 struct hlist_node global_hlist;
114
115 int (*build_header)(struct l2tp_session *session, void *buf);
116 void (*recv_skb)(struct l2tp_session *session, struct sk_buff *skb, int data_len);
117 void (*session_close)(struct l2tp_session *session);
118 void (*show)(struct seq_file *m, void *priv);
119 uint8_t priv[0];
120 };
121
122
123
124
125 struct l2tp_tunnel_cfg {
126 int debug;
127
128 enum l2tp_encap_type encap;
129
130
131 struct in_addr local_ip;
132 struct in_addr peer_ip;
133 #if IS_ENABLED(CONFIG_IPV6)
134 struct in6_addr *local_ip6;
135 struct in6_addr *peer_ip6;
136 #endif
137 u16 local_udp_port;
138 u16 peer_udp_port;
139 unsigned int use_udp_checksums:1,
140 udp6_zero_tx_checksums:1,
141 udp6_zero_rx_checksums:1;
142 };
143
144 struct l2tp_tunnel {
145 int magic;
146
147 unsigned long dead;
148
149 struct rcu_head rcu;
150 rwlock_t hlist_lock;
151 bool acpt_newsess;
152
153
154
155 struct hlist_head session_hlist[L2TP_HASH_SIZE];
156
157
158 u32 tunnel_id;
159 u32 peer_tunnel_id;
160 int version;
161
162 char name[20];
163 int debug;
164
165 enum l2tp_encap_type encap;
166 struct l2tp_stats stats;
167
168 struct list_head list;
169 struct net *l2tp_net;
170
171 refcount_t ref_count;
172 void (*old_sk_destruct)(struct sock *);
173 struct sock *sock;
174 int fd;
175
176
177 struct work_struct del_work;
178 };
179
180 struct l2tp_nl_cmd_ops {
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);
184 int (*session_delete)(struct l2tp_session *session);
185 };
186
187 static inline void *l2tp_session_priv(struct l2tp_session *session)
188 {
189 return &session->priv[0];
190 }
191
192 struct l2tp_tunnel *l2tp_tunnel_get(const struct net *net, u32 tunnel_id);
193 struct l2tp_tunnel *l2tp_tunnel_get_nth(const struct net *net, int nth);
194 struct l2tp_session *l2tp_tunnel_get_session(struct l2tp_tunnel *tunnel,
195 u32 session_id);
196
197 void l2tp_tunnel_free(struct l2tp_tunnel *tunnel);
198
199 struct l2tp_session *l2tp_session_get(const struct net *net, u32 session_id);
200 struct l2tp_session *l2tp_session_get_nth(struct l2tp_tunnel *tunnel, int nth);
201 struct l2tp_session *l2tp_session_get_by_ifname(const struct net *net,
202 const char *ifname);
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);
207 int l2tp_tunnel_register(struct l2tp_tunnel *tunnel, struct net *net,
208 struct l2tp_tunnel_cfg *cfg);
209
210 void l2tp_tunnel_delete(struct l2tp_tunnel *tunnel);
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);
215 int l2tp_session_register(struct l2tp_session *session,
216 struct l2tp_tunnel *tunnel);
217
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,
223 int length);
224 int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb);
225 void l2tp_session_set_header_len(struct l2tp_session *session, int version);
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);
233 int l2tp_ioctl(struct sock *sk, int cmd, unsigned long arg);
234
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))
243 l2tp_tunnel_free(tunnel);
244 }
245
246
247
248
249 static inline void l2tp_session_inc_refcount(struct l2tp_session *session)
250 {
251 refcount_inc(&session->ref_count);
252 }
253
254 static inline void l2tp_session_dec_refcount(struct l2tp_session *session)
255 {
256 if (refcount_dec_and_test(&session->ref_count))
257 l2tp_session_free(session);
258 }
259
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
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
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
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
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
334 #define MODULE_ALIAS_L2TP_PWTYPE(type) \
335 MODULE_ALIAS("net-l2tp-type-" __stringify(type))
336
337 #endif