2 * L2TP internal definitions.
4 * Copyright (c) 2008,2009 Katalix Systems Ltd
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.
14 /* Just some random numbers */
15 #define L2TP_TUNNEL_MAGIC 0x42114DDA
16 #define L2TP_SESSION_MAGIC 0x0C04EB7D
18 /* Per tunnel, session hash table size */
19 #define L2TP_HASH_BITS 4
20 #define L2TP_HASH_SIZE (1 << L2TP_HASH_BITS)
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)
26 /* Debug message categories for the DEBUG socket option */
28 L2TP_MSG_DEBUG = (1 << 0), /* verbose debug (if
30 L2TP_MSG_CONTROL = (1 << 1), /* userspace - kernel
32 L2TP_MSG_SEQ = (1 << 2), /* sequence numbers */
33 L2TP_MSG_DATA = (1 << 3), /* data packets */
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,
46 enum l2tp_l2spec_type {
48 L2TP_L2SPECTYPE_DEFAULT,
51 enum l2tp_encap_type {
67 u64 rx_cookie_discards;
72 /* Describes a session. Contains information to determine incoming
73 * packets and transmit outgoing ones.
75 struct l2tp_session_cfg {
76 enum l2tp_pwtype pw_type;
77 unsigned data_seq:2; /* data sequencing level
78 * 0 => none, 1 => IP only,
81 unsigned recv_seq:1; /* expect receive packets with
82 * sequence numbers? */
83 unsigned send_seq:1; /* send packets with sequence
85 unsigned lns_mode:1; /* behave as LNS? LAC enables
86 * sequence numbers under
88 int debug; /* bitmask of debug message
90 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 */
97 int reorder_timeout; /* configured reorder timeout
103 struct l2tp_session {
104 int magic; /* should be
105 * L2TP_SESSION_MAGIC */
107 struct l2tp_tunnel *tunnel; /* back pointer to tunnel
115 u16 offset; /* offset from end of L2TP header
116 to beginning of data */
120 u32 nr; /* session NR state (receive) */
121 u32 ns; /* session NR state (send) */
122 struct sk_buff_head reorder_q; /* receive reorder queue */
123 struct hlist_node hlist; /* Hash list node */
126 char name[32]; /* for logging */
127 unsigned data_seq:2; /* data sequencing level
128 * 0 => none, 1 => IP only,
131 unsigned recv_seq:1; /* expect receive packets with
132 * sequence numbers? */
133 unsigned send_seq:1; /* send packets with sequence
135 unsigned lns_mode:1; /* behave as LNS? LAC enables
136 * sequence numbers under
138 int debug; /* bitmask of debug message
140 int reorder_timeout; /* configured reorder timeout
144 enum l2tp_pwtype pwtype;
145 struct l2tp_stats stats;
146 struct hlist_node global_hlist; /* Global hash list node */
148 int (*build_header)(struct l2tp_session *session, void *buf);
149 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);
154 uint8_t priv[0]; /* private data */
157 /* Describes the tunnel. It contains info to track all the associated
158 * sessions so incoming packets can be sorted out
160 struct l2tp_tunnel_cfg {
161 int debug; /* bitmask of debug message
163 enum l2tp_encap_type encap;
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,
174 int version; /* 2=>L2TPv2, 3=>L2TPv3 */
176 char name[20]; /* for logging */
177 int debug; /* bitmask of debug message
179 enum l2tp_encap_type encap;
180 struct l2tp_stats stats;
182 struct list_head list; /* Keep a list of all tunnels */
183 struct net *l2tp_net; /* the net we belong to */
187 int (*recv_payload_hook)(struct sk_buff *skb);
188 void (*old_sk_destruct)(struct sock *);
189 struct sock *sock; /* Parent socket */
192 uint8_t priv[0]; /* private data */
195 static inline void *l2tp_tunnel_priv(struct l2tp_tunnel *tunnel)
197 return &tunnel->priv[0];
200 static inline void *l2tp_session_priv(struct l2tp_session *session)
202 return &session->priv[0];
205 static inline struct l2tp_tunnel *l2tp_sock_to_tunnel(struct sock *sk)
207 struct l2tp_tunnel *tunnel;
213 tunnel = (struct l2tp_tunnel *)(sk->sk_user_data);
214 if (tunnel == NULL) {
219 BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
225 extern struct l2tp_session *l2tp_session_find(struct net *net, struct l2tp_tunnel *tunnel, u32 session_id);
226 extern struct l2tp_session *l2tp_session_find_nth(struct l2tp_tunnel *tunnel, int nth);
227 extern struct l2tp_tunnel *l2tp_tunnel_find(struct net *net, u32 tunnel_id);
228 extern struct l2tp_tunnel *l2tp_tunnel_find_nth(struct net *net, int nth);
230 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);
231 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);
232 extern void l2tp_tunnel_free(struct l2tp_tunnel *tunnel);
233 extern void l2tp_session_free(struct l2tp_session *session);
234 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));
235 extern int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb, int (*payload_hook)(struct sk_buff *skb));
236 extern int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb);
238 extern int l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb, size_t data_len);
239 extern int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len);
240 extern void l2tp_tunnel_destruct(struct sock *sk);
241 extern void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel);
242 extern void l2tp_session_set_header_len(struct l2tp_session *session, int version);
244 /* Tunnel reference counts. Incremented per session that is added to
247 static inline void l2tp_tunnel_inc_refcount_1(struct l2tp_tunnel *tunnel)
249 atomic_inc(&tunnel->ref_count);
252 static inline void l2tp_tunnel_dec_refcount_1(struct l2tp_tunnel *tunnel)
254 if (atomic_dec_and_test(&tunnel->ref_count))
255 l2tp_tunnel_free(tunnel);
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); \
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); \
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)
271 /* Session reference counts. Incremented when code obtains a reference
274 static inline void l2tp_session_inc_refcount_1(struct l2tp_session *session)
276 atomic_inc(&session->ref_count);
279 static inline void l2tp_session_dec_refcount_1(struct l2tp_session *session)
281 if (atomic_dec_and_test(&session->ref_count))
282 l2tp_session_free(session);
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); \
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); \
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)
299 #endif /* _L2TP_CORE_H_ */