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,
62 u64 rx_cookie_discards;
67 /* Describes a session. Contains information to determine incoming
68 * packets and transmit outgoing ones.
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,
76 unsigned recv_seq:1; /* expect receive packets with
77 * sequence numbers? */
78 unsigned send_seq:1; /* send packets with sequence
80 unsigned lns_mode:1; /* behave as LNS? LAC enables
81 * sequence numbers under
83 int debug; /* bitmask of debug message
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
99 int magic; /* should be
100 * L2TP_SESSION_MAGIC */
102 struct l2tp_tunnel *tunnel; /* back pointer to tunnel
110 u16 offset; /* offset from end of L2TP header
111 to beginning of data */
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 */
121 char name[32]; /* for logging */
122 unsigned data_seq:2; /* data sequencing level
123 * 0 => none, 1 => IP only,
126 unsigned recv_seq:1; /* expect receive packets with
127 * sequence numbers? */
128 unsigned send_seq:1; /* send packets with sequence
130 unsigned lns_mode:1; /* behave as LNS? LAC enables
131 * sequence numbers under
133 int debug; /* bitmask of debug message
135 int reorder_timeout; /* configured reorder timeout
139 enum l2tp_pwtype pwtype;
140 struct l2tp_stats stats;
141 struct hlist_node global_hlist; /* Global hash list node */
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);
149 uint8_t priv[0]; /* private data */
152 /* Describes the tunnel. It contains info to track all the associated
153 * sessions so incoming packets can be sorted out
155 struct l2tp_tunnel_cfg {
156 int debug; /* bitmask of debug message
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,
168 int version; /* 2=>L2TPv2, 3=>L2TPv3 */
170 char name[20]; /* for logging */
171 int debug; /* bitmask of debug message
173 struct l2tp_stats stats;
175 struct list_head list; /* Keep a list of all tunnels */
176 struct net *l2tp_net; /* the net we belong to */
180 int (*recv_payload_hook)(struct sk_buff *skb);
181 void (*old_sk_destruct)(struct sock *);
182 struct sock *sock; /* Parent socket */
185 uint8_t priv[0]; /* private data */
188 static inline void *l2tp_tunnel_priv(struct l2tp_tunnel *tunnel)
190 return &tunnel->priv[0];
193 static inline void *l2tp_session_priv(struct l2tp_session *session)
195 return &session->priv[0];
198 static inline struct l2tp_tunnel *l2tp_sock_to_tunnel(struct sock *sk)
200 struct l2tp_tunnel *tunnel;
206 tunnel = (struct l2tp_tunnel *)(sk->sk_user_data);
207 if (tunnel == NULL) {
212 BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
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);
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);
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);
237 /* Tunnel reference counts. Incremented per session that is added to
240 static inline void l2tp_tunnel_inc_refcount_1(struct l2tp_tunnel *tunnel)
242 atomic_inc(&tunnel->ref_count);
245 static inline void l2tp_tunnel_dec_refcount_1(struct l2tp_tunnel *tunnel)
247 if (atomic_dec_and_test(&tunnel->ref_count))
248 l2tp_tunnel_free(tunnel);
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); \
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); \
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)
264 /* Session reference counts. Incremented when code obtains a reference
267 static inline void l2tp_session_inc_refcount_1(struct l2tp_session *session)
269 atomic_inc(&session->ref_count);
272 static inline void l2tp_session_dec_refcount_1(struct l2tp_session *session)
274 if (atomic_dec_and_test(&session->ref_count))
275 l2tp_session_free(session);
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); \
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); \
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)
292 #endif /* _L2TP_CORE_H_ */