l2tp: Split pppol2tp patch into separate l2tp and ppp parts
[pandora-kernel.git] / 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 #define L2TP_HASH_BITS  4
19 #define L2TP_HASH_SIZE  (1 << L2TP_HASH_BITS)
20
21 /* Debug message categories for the DEBUG socket option */
22 enum {
23         L2TP_MSG_DEBUG          = (1 << 0),     /* verbose debug (if
24                                                  * compiled in) */
25         L2TP_MSG_CONTROL        = (1 << 1),     /* userspace - kernel
26                                                  * interface */
27         L2TP_MSG_SEQ            = (1 << 2),     /* sequence numbers */
28         L2TP_MSG_DATA           = (1 << 3),     /* data packets */
29 };
30
31 struct sk_buff;
32
33 struct l2tp_stats {
34         u64                     tx_packets;
35         u64                     tx_bytes;
36         u64                     tx_errors;
37         u64                     rx_packets;
38         u64                     rx_bytes;
39         u64                     rx_seq_discards;
40         u64                     rx_oos_packets;
41         u64                     rx_errors;
42 };
43
44 struct l2tp_tunnel;
45
46 /* Describes a session. Contains information to determine incoming
47  * packets and transmit outgoing ones.
48  */
49 struct l2tp_session_cfg {
50         unsigned                data_seq:2;     /* data sequencing level
51                                                  * 0 => none, 1 => IP only,
52                                                  * 2 => all
53                                                  */
54         unsigned                recv_seq:1;     /* expect receive packets with
55                                                  * sequence numbers? */
56         unsigned                send_seq:1;     /* send packets with sequence
57                                                  * numbers? */
58         unsigned                lns_mode:1;     /* behave as LNS? LAC enables
59                                                  * sequence numbers under
60                                                  * control of LNS. */
61         int                     debug;          /* bitmask of debug message
62                                                  * categories */
63         int                     offset;         /* offset to payload */
64         int                     reorder_timeout; /* configured reorder timeout
65                                                   * (in jiffies) */
66         int                     mtu;
67         int                     mru;
68         int                     hdr_len;
69 };
70
71 struct l2tp_session {
72         int                     magic;          /* should be
73                                                  * L2TP_SESSION_MAGIC */
74
75         struct l2tp_tunnel      *tunnel;        /* back pointer to tunnel
76                                                  * context */
77         u32                     session_id;
78         u32                     peer_session_id;
79         u16                     nr;             /* session NR state (receive) */
80         u16                     ns;             /* session NR state (send) */
81         struct sk_buff_head     reorder_q;      /* receive reorder queue */
82         struct hlist_node       hlist;          /* Hash list node */
83         atomic_t                ref_count;
84
85         char                    name[32];       /* for logging */
86         unsigned                data_seq:2;     /* data sequencing level
87                                                  * 0 => none, 1 => IP only,
88                                                  * 2 => all
89                                                  */
90         unsigned                recv_seq:1;     /* expect receive packets with
91                                                  * sequence numbers? */
92         unsigned                send_seq:1;     /* send packets with sequence
93                                                  * numbers? */
94         unsigned                lns_mode:1;     /* behave as LNS? LAC enables
95                                                  * sequence numbers under
96                                                  * control of LNS. */
97         int                     debug;          /* bitmask of debug message
98                                                  * categories */
99         int                     reorder_timeout; /* configured reorder timeout
100                                                   * (in jiffies) */
101         int                     mtu;
102         int                     mru;
103         int                     hdr_len;
104         struct l2tp_stats       stats;
105
106         void (*recv_skb)(struct l2tp_session *session, struct sk_buff *skb, int data_len);
107         void (*session_close)(struct l2tp_session *session);
108         void (*ref)(struct l2tp_session *session);
109         void (*deref)(struct l2tp_session *session);
110
111         uint8_t                 priv[0];        /* private data */
112 };
113
114 /* Describes the tunnel. It contains info to track all the associated
115  * sessions so incoming packets can be sorted out
116  */
117 struct l2tp_tunnel_cfg {
118         int                     debug;          /* bitmask of debug message
119                                                  * categories */
120 };
121
122 struct l2tp_tunnel {
123         int                     magic;          /* Should be L2TP_TUNNEL_MAGIC */
124         rwlock_t                hlist_lock;     /* protect session_hlist */
125         struct hlist_head       session_hlist[L2TP_HASH_SIZE];
126                                                 /* hashed list of sessions,
127                                                  * hashed by id */
128         u32                     tunnel_id;
129         u32                     peer_tunnel_id;
130         int                     version;        /* 2=>L2TPv2, 3=>L2TPv3 */
131
132         char                    name[20];       /* for logging */
133         int                     debug;          /* bitmask of debug message
134                                                  * categories */
135         int                     hdr_len;
136         struct l2tp_stats       stats;
137
138         struct list_head        list;           /* Keep a list of all tunnels */
139         struct net              *l2tp_net;      /* the net we belong to */
140
141         atomic_t                ref_count;
142
143         int (*recv_payload_hook)(struct sk_buff *skb);
144         void (*old_sk_destruct)(struct sock *);
145         struct sock             *sock;          /* Parent socket */
146         int                     fd;
147
148         uint8_t                 priv[0];        /* private data */
149 };
150
151 static inline void *l2tp_tunnel_priv(struct l2tp_tunnel *tunnel)
152 {
153         return &tunnel->priv[0];
154 }
155
156 static inline void *l2tp_session_priv(struct l2tp_session *session)
157 {
158         return &session->priv[0];
159 }
160
161 static inline struct l2tp_tunnel *l2tp_sock_to_tunnel(struct sock *sk)
162 {
163         struct l2tp_tunnel *tunnel;
164
165         if (sk == NULL)
166                 return NULL;
167
168         sock_hold(sk);
169         tunnel = (struct l2tp_tunnel *)(sk->sk_user_data);
170         if (tunnel == NULL) {
171                 sock_put(sk);
172                 goto out;
173         }
174
175         BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
176
177 out:
178         return tunnel;
179 }
180
181 extern struct l2tp_session *l2tp_session_find(struct l2tp_tunnel *tunnel, u32 session_id);
182 extern struct l2tp_session *l2tp_session_find_nth(struct l2tp_tunnel *tunnel, int nth);
183 extern struct l2tp_tunnel *l2tp_tunnel_find(struct net *net, u32 tunnel_id);
184 extern struct l2tp_tunnel *l2tp_tunnel_find_nth(struct net *net, int nth);
185
186 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);
187 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);
188 extern void l2tp_tunnel_free(struct l2tp_tunnel *tunnel);
189 extern void l2tp_session_free(struct l2tp_session *session);
190 extern int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb, int (*payload_hook)(struct sk_buff *skb));
191 extern int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb);
192
193 extern void l2tp_build_l2tp_header(struct l2tp_session *session, void *buf);
194 extern int l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb, size_t data_len);
195 extern int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len);
196 extern void l2tp_tunnel_destruct(struct sock *sk);
197 extern void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel);
198
199 /* Tunnel reference counts. Incremented per session that is added to
200  * the tunnel.
201  */
202 static inline void l2tp_tunnel_inc_refcount_1(struct l2tp_tunnel *tunnel)
203 {
204         atomic_inc(&tunnel->ref_count);
205 }
206
207 static inline void l2tp_tunnel_dec_refcount_1(struct l2tp_tunnel *tunnel)
208 {
209         if (atomic_dec_and_test(&tunnel->ref_count))
210                 l2tp_tunnel_free(tunnel);
211 }
212 #ifdef L2TP_REFCNT_DEBUG
213 #define l2tp_tunnel_inc_refcount(_t) do { \
214                 printk(KERN_DEBUG "l2tp_tunnel_inc_refcount: %s:%d %s: cnt=%d\n", __func__, __LINE__, (_t)->name, atomic_read(&_t->ref_count)); \
215                 l2tp_tunnel_inc_refcount_1(_t);                         \
216         } while (0)
217 #define l2tp_tunnel_dec_refcount(_t) do { \
218                 printk(KERN_DEBUG "l2tp_tunnel_dec_refcount: %s:%d %s: cnt=%d\n", __func__, __LINE__, (_t)->name, atomic_read(&_t->ref_count)); \
219                 l2tp_tunnel_dec_refcount_1(_t);                         \
220         } while (0)
221 #else
222 #define l2tp_tunnel_inc_refcount(t) l2tp_tunnel_inc_refcount_1(t)
223 #define l2tp_tunnel_dec_refcount(t) l2tp_tunnel_dec_refcount_1(t)
224 #endif
225
226 /* Session reference counts. Incremented when code obtains a reference
227  * to a session.
228  */
229 static inline void l2tp_session_inc_refcount_1(struct l2tp_session *session)
230 {
231         atomic_inc(&session->ref_count);
232 }
233
234 static inline void l2tp_session_dec_refcount_1(struct l2tp_session *session)
235 {
236         if (atomic_dec_and_test(&session->ref_count))
237                 l2tp_session_free(session);
238 }
239
240 #ifdef L2TP_REFCNT_DEBUG
241 #define l2tp_session_inc_refcount(_s) do { \
242                 printk(KERN_DEBUG "l2tp_session_inc_refcount: %s:%d %s: cnt=%d\n", __func__, __LINE__, (_s)->name, atomic_read(&_s->ref_count)); \
243                 l2tp_session_inc_refcount_1(_s);                                \
244         } while (0)
245 #define l2tp_session_dec_refcount(_s) do { \
246                 printk(KERN_DEBUG "l2tp_session_dec_refcount: %s:%d %s: cnt=%d\n", __func__, __LINE__, (_s)->name, atomic_read(&_s->ref_count)); \
247                 l2tp_session_dec_refcount_1(_s);                                \
248         } while (0)
249 #else
250 #define l2tp_session_inc_refcount(s) l2tp_session_inc_refcount_1(s)
251 #define l2tp_session_dec_refcount(s) l2tp_session_dec_refcount_1(s)
252 #endif
253
254 #endif /* _L2TP_CORE_H_ */