]> Pileus Git - ~andy/linux/blob - net/l2tp/l2tp_core.h
l2tp: Add L2TPv3 protocol support
[~andy/linux] / net / l2tp / l2tp_core.h
1 /*
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
18 /* Per tunnel, session hash table size */
19 #define L2TP_HASH_BITS  4
20 #define L2TP_HASH_SIZE  (1 << L2TP_HASH_BITS)
21
22 /* 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
26 /* Debug message categories for the DEBUG socket option */
27 enum {
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
36 enum 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
46 enum l2tp_l2spec_type {
47         L2TP_L2SPECTYPE_NONE,
48         L2TP_L2SPECTYPE_DEFAULT,
49 };
50
51 struct sk_buff;
52
53 struct l2tp_stats {
54         u64                     tx_packets;
55         u64                     tx_bytes;
56         u64                     tx_errors;
57         u64                     rx_packets;
58         u64                     rx_bytes;
59         u64                     rx_seq_discards;
60         u64                     rx_oos_packets;
61         u64                     rx_errors;
62         u64                     rx_cookie_discards;
63 };
64
65 struct l2tp_tunnel;
66
67 /* Describes a session. Contains information to determine incoming
68  * packets and transmit outgoing ones.
69  */
70 struct l2tp_session_cfg {
71         enum l2tp_pwtype        pw_type;
72         unsigned                data_seq:2;     /* data sequencing level
73                                                  * 0 => none, 1 => IP only,
74                                                  * 2 => all
75                                                  */
76         unsigned                recv_seq:1;     /* expect receive packets with
77                                                  * sequence numbers? */
78         unsigned                send_seq:1;     /* send packets with sequence
79                                                  * numbers? */
80         unsigned                lns_mode:1;     /* behave as LNS? LAC enables
81                                                  * sequence numbers under
82                                                  * control of LNS. */
83         int                     debug;          /* bitmask of debug message
84                                                  * categories */
85         u16                     offset;         /* offset to payload */
86         u16                     l2specific_len; /* Layer 2 specific length */
87         u16                     l2specific_type; /* Layer 2 specific type */
88         u8                      cookie[8];      /* optional cookie */
89         int                     cookie_len;     /* 0, 4 or 8 bytes */
90         u8                      peer_cookie[8]; /* peer's cookie */
91         int                     peer_cookie_len; /* 0, 4 or 8 bytes */
92         int                     reorder_timeout; /* configured reorder timeout
93                                                   * (in jiffies) */
94         int                     mtu;
95         int                     mru;
96 };
97
98 struct l2tp_session {
99         int                     magic;          /* should be
100                                                  * L2TP_SESSION_MAGIC */
101
102         struct l2tp_tunnel      *tunnel;        /* back pointer to tunnel
103                                                  * context */
104         u32                     session_id;
105         u32                     peer_session_id;
106         u8                      cookie[8];
107         int                     cookie_len;
108         u8                      peer_cookie[8];
109         int                     peer_cookie_len;
110         u16                     offset;         /* offset from end of L2TP header
111                                                    to beginning of data */
112         u16                     l2specific_len;
113         u16                     l2specific_type;
114         u16                     hdr_len;
115         u32                     nr;             /* session NR state (receive) */
116         u32                     ns;             /* session NR state (send) */
117         struct sk_buff_head     reorder_q;      /* receive reorder queue */
118         struct hlist_node       hlist;          /* Hash list node */
119         atomic_t                ref_count;
120
121         char                    name[32];       /* for logging */
122         unsigned                data_seq:2;     /* data sequencing level
123                                                  * 0 => none, 1 => IP only,
124                                                  * 2 => all
125                                                  */
126         unsigned                recv_seq:1;     /* expect receive packets with
127                                                  * sequence numbers? */
128         unsigned                send_seq:1;     /* send packets with sequence
129                                                  * numbers? */
130         unsigned                lns_mode:1;     /* behave as LNS? LAC enables
131                                                  * sequence numbers under
132                                                  * control of LNS. */
133         int                     debug;          /* bitmask of debug message
134                                                  * categories */
135         int                     reorder_timeout; /* configured reorder timeout
136                                                   * (in jiffies) */
137         int                     mtu;
138         int                     mru;
139         enum l2tp_pwtype        pwtype;
140         struct l2tp_stats       stats;
141         struct hlist_node       global_hlist;   /* Global hash list node */
142
143         int (*build_header)(struct l2tp_session *session, void *buf);
144         void (*recv_skb)(struct l2tp_session *session, struct sk_buff *skb, int data_len);
145         void (*session_close)(struct l2tp_session *session);
146         void (*ref)(struct l2tp_session *session);
147         void (*deref)(struct l2tp_session *session);
148
149         uint8_t                 priv[0];        /* private data */
150 };
151
152 /* Describes the tunnel. It contains info to track all the associated
153  * sessions so incoming packets can be sorted out
154  */
155 struct l2tp_tunnel_cfg {
156         int                     debug;          /* bitmask of debug message
157                                                  * categories */
158 };
159
160 struct l2tp_tunnel {
161         int                     magic;          /* Should be L2TP_TUNNEL_MAGIC */
162         rwlock_t                hlist_lock;     /* protect session_hlist */
163         struct hlist_head       session_hlist[L2TP_HASH_SIZE];
164                                                 /* hashed list of sessions,
165                                                  * hashed by id */
166         u32                     tunnel_id;
167         u32                     peer_tunnel_id;
168         int                     version;        /* 2=>L2TPv2, 3=>L2TPv3 */
169
170         char                    name[20];       /* for logging */
171         int                     debug;          /* bitmask of debug message
172                                                  * categories */
173         struct l2tp_stats       stats;
174
175         struct list_head        list;           /* Keep a list of all tunnels */
176         struct net              *l2tp_net;      /* the net we belong to */
177
178         atomic_t                ref_count;
179
180         int (*recv_payload_hook)(struct sk_buff *skb);
181         void (*old_sk_destruct)(struct sock *);
182         struct sock             *sock;          /* Parent socket */
183         int                     fd;
184
185         uint8_t                 priv[0];        /* private data */
186 };
187
188 static inline void *l2tp_tunnel_priv(struct l2tp_tunnel *tunnel)
189 {
190         return &tunnel->priv[0];
191 }
192
193 static inline void *l2tp_session_priv(struct l2tp_session *session)
194 {
195         return &session->priv[0];
196 }
197
198 static inline struct l2tp_tunnel *l2tp_sock_to_tunnel(struct sock *sk)
199 {
200         struct l2tp_tunnel *tunnel;
201
202         if (sk == NULL)
203                 return NULL;
204
205         sock_hold(sk);
206         tunnel = (struct l2tp_tunnel *)(sk->sk_user_data);
207         if (tunnel == NULL) {
208                 sock_put(sk);
209                 goto out;
210         }
211
212         BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
213
214 out:
215         return tunnel;
216 }
217
218 extern struct l2tp_session *l2tp_session_find(struct net *net, struct l2tp_tunnel *tunnel, u32 session_id);
219 extern struct l2tp_session *l2tp_session_find_nth(struct l2tp_tunnel *tunnel, int nth);
220 extern struct l2tp_tunnel *l2tp_tunnel_find(struct net *net, u32 tunnel_id);
221 extern struct l2tp_tunnel *l2tp_tunnel_find_nth(struct net *net, int nth);
222
223 extern 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);
224 extern 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);
225 extern void l2tp_tunnel_free(struct l2tp_tunnel *tunnel);
226 extern void l2tp_session_free(struct l2tp_session *session);
227 extern 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));
228 extern int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb, int (*payload_hook)(struct sk_buff *skb));
229 extern int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb);
230
231 extern int l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb, size_t data_len);
232 extern int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len);
233 extern void l2tp_tunnel_destruct(struct sock *sk);
234 extern void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel);
235 extern void l2tp_session_set_header_len(struct l2tp_session *session, int version);
236
237 /* Tunnel reference counts. Incremented per session that is added to
238  * the tunnel.
239  */
240 static inline void l2tp_tunnel_inc_refcount_1(struct l2tp_tunnel *tunnel)
241 {
242         atomic_inc(&tunnel->ref_count);
243 }
244
245 static inline void l2tp_tunnel_dec_refcount_1(struct l2tp_tunnel *tunnel)
246 {
247         if (atomic_dec_and_test(&tunnel->ref_count))
248                 l2tp_tunnel_free(tunnel);
249 }
250 #ifdef L2TP_REFCNT_DEBUG
251 #define l2tp_tunnel_inc_refcount(_t) do { \
252                 printk(KERN_DEBUG "l2tp_tunnel_inc_refcount: %s:%d %s: cnt=%d\n", __func__, __LINE__, (_t)->name, atomic_read(&_t->ref_count)); \
253                 l2tp_tunnel_inc_refcount_1(_t);                         \
254         } while (0)
255 #define l2tp_tunnel_dec_refcount(_t) do { \
256                 printk(KERN_DEBUG "l2tp_tunnel_dec_refcount: %s:%d %s: cnt=%d\n", __func__, __LINE__, (_t)->name, atomic_read(&_t->ref_count)); \
257                 l2tp_tunnel_dec_refcount_1(_t);                         \
258         } while (0)
259 #else
260 #define l2tp_tunnel_inc_refcount(t) l2tp_tunnel_inc_refcount_1(t)
261 #define l2tp_tunnel_dec_refcount(t) l2tp_tunnel_dec_refcount_1(t)
262 #endif
263
264 /* Session reference counts. Incremented when code obtains a reference
265  * to a session.
266  */
267 static inline void l2tp_session_inc_refcount_1(struct l2tp_session *session)
268 {
269         atomic_inc(&session->ref_count);
270 }
271
272 static inline void l2tp_session_dec_refcount_1(struct l2tp_session *session)
273 {
274         if (atomic_dec_and_test(&session->ref_count))
275                 l2tp_session_free(session);
276 }
277
278 #ifdef L2TP_REFCNT_DEBUG
279 #define l2tp_session_inc_refcount(_s) do { \
280                 printk(KERN_DEBUG "l2tp_session_inc_refcount: %s:%d %s: cnt=%d\n", __func__, __LINE__, (_s)->name, atomic_read(&_s->ref_count)); \
281                 l2tp_session_inc_refcount_1(_s);                                \
282         } while (0)
283 #define l2tp_session_dec_refcount(_s) do { \
284                 printk(KERN_DEBUG "l2tp_session_dec_refcount: %s:%d %s: cnt=%d\n", __func__, __LINE__, (_s)->name, atomic_read(&_s->ref_count)); \
285                 l2tp_session_dec_refcount_1(_s);                                \
286         } while (0)
287 #else
288 #define l2tp_session_inc_refcount(s) l2tp_session_inc_refcount_1(s)
289 #define l2tp_session_dec_refcount(s) l2tp_session_dec_refcount_1(s)
290 #endif
291
292 #endif /* _L2TP_CORE_H_ */