blob: d2395984645e610bd74969f1a572d41f297fbdf5 [file] [log] [blame]
James Chapmanfd558d12010-04-02 06:18:33 +00001/*
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
11#ifndef _L2TP_CORE_H_
12#define _L2TP_CORE_H_
13
14/* Just some random numbers */
15#define L2TP_TUNNEL_MAGIC 0x42114DDA
16#define L2TP_SESSION_MAGIC 0x0C04EB7D
17
James Chapmanf7faffa2010-04-02 06:18:49 +000018/* Per tunnel, session hash table size */
James Chapmanfd558d12010-04-02 06:18:33 +000019#define L2TP_HASH_BITS 4
20#define L2TP_HASH_SIZE (1 << L2TP_HASH_BITS)
21
James Chapmanf7faffa2010-04-02 06:18:49 +000022/* System-wide, session hash table size */
23#define L2TP_HASH_BITS_2 8
24#define L2TP_HASH_SIZE_2 (1 << L2TP_HASH_BITS_2)
25
James Chapmanfd558d12010-04-02 06:18:33 +000026/* Debug message categories for the DEBUG socket option */
27enum {
28 L2TP_MSG_DEBUG = (1 << 0), /* verbose debug (if
29 * compiled in) */
30 L2TP_MSG_CONTROL = (1 << 1), /* userspace - kernel
31 * interface */
32 L2TP_MSG_SEQ = (1 << 2), /* sequence numbers */
33 L2TP_MSG_DATA = (1 << 3), /* data packets */
34};
35
James Chapmanf7faffa2010-04-02 06:18:49 +000036enum l2tp_pwtype {
37 L2TP_PWTYPE_NONE = 0x0000,
38 L2TP_PWTYPE_ETH_VLAN = 0x0004,
39 L2TP_PWTYPE_ETH = 0x0005,
40 L2TP_PWTYPE_PPP = 0x0007,
41 L2TP_PWTYPE_PPP_AC = 0x0008,
42 L2TP_PWTYPE_IP = 0x000b,
43 __L2TP_PWTYPE_MAX
44};
45
46enum l2tp_l2spec_type {
47 L2TP_L2SPECTYPE_NONE,
48 L2TP_L2SPECTYPE_DEFAULT,
49};
50
James Chapman0d767512010-04-02 06:19:00 +000051enum l2tp_encap_type {
52 L2TP_ENCAPTYPE_UDP,
53 L2TP_ENCAPTYPE_IP,
54};
55
James Chapmanfd558d12010-04-02 06:18:33 +000056struct sk_buff;
57
58struct l2tp_stats {
59 u64 tx_packets;
60 u64 tx_bytes;
61 u64 tx_errors;
62 u64 rx_packets;
63 u64 rx_bytes;
64 u64 rx_seq_discards;
65 u64 rx_oos_packets;
66 u64 rx_errors;
James Chapmanf7faffa2010-04-02 06:18:49 +000067 u64 rx_cookie_discards;
James Chapmanfd558d12010-04-02 06:18:33 +000068};
69
70struct l2tp_tunnel;
71
72/* Describes a session. Contains information to determine incoming
73 * packets and transmit outgoing ones.
74 */
75struct l2tp_session_cfg {
James Chapmanf7faffa2010-04-02 06:18:49 +000076 enum l2tp_pwtype pw_type;
James Chapmanfd558d12010-04-02 06:18:33 +000077 unsigned data_seq:2; /* data sequencing level
78 * 0 => none, 1 => IP only,
79 * 2 => all
80 */
81 unsigned recv_seq:1; /* expect receive packets with
82 * sequence numbers? */
83 unsigned send_seq:1; /* send packets with sequence
84 * numbers? */
85 unsigned lns_mode:1; /* behave as LNS? LAC enables
86 * sequence numbers under
87 * control of LNS. */
88 int debug; /* bitmask of debug message
89 * categories */
James Chapmanf7faffa2010-04-02 06:18:49 +000090 u16 offset; /* offset to payload */
91 u16 l2specific_len; /* Layer 2 specific length */
92 u16 l2specific_type; /* Layer 2 specific type */
93 u8 cookie[8]; /* optional cookie */
94 int cookie_len; /* 0, 4 or 8 bytes */
95 u8 peer_cookie[8]; /* peer's cookie */
96 int peer_cookie_len; /* 0, 4 or 8 bytes */
James Chapmanfd558d12010-04-02 06:18:33 +000097 int reorder_timeout; /* configured reorder timeout
98 * (in jiffies) */
99 int mtu;
100 int mru;
James Chapmanfd558d12010-04-02 06:18:33 +0000101};
102
103struct l2tp_session {
104 int magic; /* should be
105 * L2TP_SESSION_MAGIC */
106
107 struct l2tp_tunnel *tunnel; /* back pointer to tunnel
108 * context */
109 u32 session_id;
110 u32 peer_session_id;
James Chapmanf7faffa2010-04-02 06:18:49 +0000111 u8 cookie[8];
112 int cookie_len;
113 u8 peer_cookie[8];
114 int peer_cookie_len;
115 u16 offset; /* offset from end of L2TP header
116 to beginning of data */
117 u16 l2specific_len;
118 u16 l2specific_type;
119 u16 hdr_len;
120 u32 nr; /* session NR state (receive) */
121 u32 ns; /* session NR state (send) */
James Chapmanfd558d12010-04-02 06:18:33 +0000122 struct sk_buff_head reorder_q; /* receive reorder queue */
123 struct hlist_node hlist; /* Hash list node */
124 atomic_t ref_count;
125
126 char name[32]; /* for logging */
127 unsigned data_seq:2; /* data sequencing level
128 * 0 => none, 1 => IP only,
129 * 2 => all
130 */
131 unsigned recv_seq:1; /* expect receive packets with
132 * sequence numbers? */
133 unsigned send_seq:1; /* send packets with sequence
134 * numbers? */
135 unsigned lns_mode:1; /* behave as LNS? LAC enables
136 * sequence numbers under
137 * control of LNS. */
138 int debug; /* bitmask of debug message
139 * categories */
140 int reorder_timeout; /* configured reorder timeout
141 * (in jiffies) */
142 int mtu;
143 int mru;
James Chapmanf7faffa2010-04-02 06:18:49 +0000144 enum l2tp_pwtype pwtype;
James Chapmanfd558d12010-04-02 06:18:33 +0000145 struct l2tp_stats stats;
James Chapmanf7faffa2010-04-02 06:18:49 +0000146 struct hlist_node global_hlist; /* Global hash list node */
James Chapmanfd558d12010-04-02 06:18:33 +0000147
James Chapmanf7faffa2010-04-02 06:18:49 +0000148 int (*build_header)(struct l2tp_session *session, void *buf);
James Chapmanfd558d12010-04-02 06:18:33 +0000149 void (*recv_skb)(struct l2tp_session *session, struct sk_buff *skb, int data_len);
150 void (*session_close)(struct l2tp_session *session);
151 void (*ref)(struct l2tp_session *session);
152 void (*deref)(struct l2tp_session *session);
153
154 uint8_t priv[0]; /* private data */
155};
156
157/* Describes the tunnel. It contains info to track all the associated
158 * sessions so incoming packets can be sorted out
159 */
160struct l2tp_tunnel_cfg {
161 int debug; /* bitmask of debug message
162 * categories */
James Chapman0d767512010-04-02 06:19:00 +0000163 enum l2tp_encap_type encap;
James Chapmanfd558d12010-04-02 06:18:33 +0000164};
165
166struct l2tp_tunnel {
167 int magic; /* Should be L2TP_TUNNEL_MAGIC */
168 rwlock_t hlist_lock; /* protect session_hlist */
169 struct hlist_head session_hlist[L2TP_HASH_SIZE];
170 /* hashed list of sessions,
171 * hashed by id */
172 u32 tunnel_id;
173 u32 peer_tunnel_id;
174 int version; /* 2=>L2TPv2, 3=>L2TPv3 */
175
176 char name[20]; /* for logging */
177 int debug; /* bitmask of debug message
178 * categories */
James Chapman0d767512010-04-02 06:19:00 +0000179 enum l2tp_encap_type encap;
James Chapmanfd558d12010-04-02 06:18:33 +0000180 struct l2tp_stats stats;
181
182 struct list_head list; /* Keep a list of all tunnels */
183 struct net *l2tp_net; /* the net we belong to */
184
185 atomic_t ref_count;
186
187 int (*recv_payload_hook)(struct sk_buff *skb);
188 void (*old_sk_destruct)(struct sock *);
189 struct sock *sock; /* Parent socket */
190 int fd;
191
192 uint8_t priv[0]; /* private data */
193};
194
195static inline void *l2tp_tunnel_priv(struct l2tp_tunnel *tunnel)
196{
197 return &tunnel->priv[0];
198}
199
200static inline void *l2tp_session_priv(struct l2tp_session *session)
201{
202 return &session->priv[0];
203}
204
205static inline struct l2tp_tunnel *l2tp_sock_to_tunnel(struct sock *sk)
206{
207 struct l2tp_tunnel *tunnel;
208
209 if (sk == NULL)
210 return NULL;
211
212 sock_hold(sk);
213 tunnel = (struct l2tp_tunnel *)(sk->sk_user_data);
214 if (tunnel == NULL) {
215 sock_put(sk);
216 goto out;
217 }
218
219 BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
220
221out:
222 return tunnel;
223}
224
James Chapmanf7faffa2010-04-02 06:18:49 +0000225extern struct l2tp_session *l2tp_session_find(struct net *net, struct l2tp_tunnel *tunnel, u32 session_id);
James Chapmanfd558d12010-04-02 06:18:33 +0000226extern struct l2tp_session *l2tp_session_find_nth(struct l2tp_tunnel *tunnel, int nth);
227extern struct l2tp_tunnel *l2tp_tunnel_find(struct net *net, u32 tunnel_id);
228extern struct l2tp_tunnel *l2tp_tunnel_find_nth(struct net *net, int nth);
229
230extern int l2tp_tunnel_create(struct net *net, int fd, int version, u32 tunnel_id, u32 peer_tunnel_id, struct l2tp_tunnel_cfg *cfg, struct l2tp_tunnel **tunnelp);
231extern struct l2tp_session *l2tp_session_create(int priv_size, struct l2tp_tunnel *tunnel, u32 session_id, u32 peer_session_id, struct l2tp_session_cfg *cfg);
232extern void l2tp_tunnel_free(struct l2tp_tunnel *tunnel);
233extern void l2tp_session_free(struct l2tp_session *session);
James Chapmanf7faffa2010-04-02 06:18:49 +0000234extern void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb, unsigned char *ptr, unsigned char *optr, u16 hdrflags, int length, int (*payload_hook)(struct sk_buff *skb));
James Chapmanfd558d12010-04-02 06:18:33 +0000235extern int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb, int (*payload_hook)(struct sk_buff *skb));
236extern int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb);
237
James Chapmanfd558d12010-04-02 06:18:33 +0000238extern int l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb, size_t data_len);
239extern int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len);
240extern void l2tp_tunnel_destruct(struct sock *sk);
241extern void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel);
James Chapmanf7faffa2010-04-02 06:18:49 +0000242extern void l2tp_session_set_header_len(struct l2tp_session *session, int version);
James Chapmanfd558d12010-04-02 06:18:33 +0000243
244/* Tunnel reference counts. Incremented per session that is added to
245 * the tunnel.
246 */
247static inline void l2tp_tunnel_inc_refcount_1(struct l2tp_tunnel *tunnel)
248{
249 atomic_inc(&tunnel->ref_count);
250}
251
252static inline void l2tp_tunnel_dec_refcount_1(struct l2tp_tunnel *tunnel)
253{
254 if (atomic_dec_and_test(&tunnel->ref_count))
255 l2tp_tunnel_free(tunnel);
256}
257#ifdef L2TP_REFCNT_DEBUG
258#define l2tp_tunnel_inc_refcount(_t) do { \
259 printk(KERN_DEBUG "l2tp_tunnel_inc_refcount: %s:%d %s: cnt=%d\n", __func__, __LINE__, (_t)->name, atomic_read(&_t->ref_count)); \
260 l2tp_tunnel_inc_refcount_1(_t); \
261 } while (0)
262#define l2tp_tunnel_dec_refcount(_t) do { \
263 printk(KERN_DEBUG "l2tp_tunnel_dec_refcount: %s:%d %s: cnt=%d\n", __func__, __LINE__, (_t)->name, atomic_read(&_t->ref_count)); \
264 l2tp_tunnel_dec_refcount_1(_t); \
265 } while (0)
266#else
267#define l2tp_tunnel_inc_refcount(t) l2tp_tunnel_inc_refcount_1(t)
268#define l2tp_tunnel_dec_refcount(t) l2tp_tunnel_dec_refcount_1(t)
269#endif
270
271/* Session reference counts. Incremented when code obtains a reference
272 * to a session.
273 */
274static inline void l2tp_session_inc_refcount_1(struct l2tp_session *session)
275{
276 atomic_inc(&session->ref_count);
277}
278
279static inline void l2tp_session_dec_refcount_1(struct l2tp_session *session)
280{
281 if (atomic_dec_and_test(&session->ref_count))
282 l2tp_session_free(session);
283}
284
285#ifdef L2TP_REFCNT_DEBUG
286#define l2tp_session_inc_refcount(_s) do { \
287 printk(KERN_DEBUG "l2tp_session_inc_refcount: %s:%d %s: cnt=%d\n", __func__, __LINE__, (_s)->name, atomic_read(&_s->ref_count)); \
288 l2tp_session_inc_refcount_1(_s); \
289 } while (0)
290#define l2tp_session_dec_refcount(_s) do { \
291 printk(KERN_DEBUG "l2tp_session_dec_refcount: %s:%d %s: cnt=%d\n", __func__, __LINE__, (_s)->name, atomic_read(&_s->ref_count)); \
292 l2tp_session_dec_refcount_1(_s); \
293 } while (0)
294#else
295#define l2tp_session_inc_refcount(s) l2tp_session_inc_refcount_1(s)
296#define l2tp_session_dec_refcount(s) l2tp_session_dec_refcount_1(s)
297#endif
298
299#endif /* _L2TP_CORE_H_ */