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 #define L2TP_HASH_BITS 4
19 #define L2TP_HASH_SIZE (1 << L2TP_HASH_BITS)
21 /* Debug message categories for the DEBUG socket option */
23 L2TP_MSG_DEBUG = (1 << 0), /* verbose debug (if
25 L2TP_MSG_CONTROL = (1 << 1), /* userspace - kernel
27 L2TP_MSG_SEQ = (1 << 2), /* sequence numbers */
28 L2TP_MSG_DATA = (1 << 3), /* data packets */
46 /* Describes a session. Contains information to determine incoming
47 * packets and transmit outgoing ones.
49 struct l2tp_session_cfg {
50 unsigned data_seq:2; /* data sequencing level
51 * 0 => none, 1 => IP only,
54 unsigned recv_seq:1; /* expect receive packets with
55 * sequence numbers? */
56 unsigned send_seq:1; /* send packets with sequence
58 unsigned lns_mode:1; /* behave as LNS? LAC enables
59 * sequence numbers under
61 int debug; /* bitmask of debug message
63 int offset; /* offset to payload */
64 int reorder_timeout; /* configured reorder timeout
72 int magic; /* should be
73 * L2TP_SESSION_MAGIC */
75 struct l2tp_tunnel *tunnel; /* back pointer to tunnel
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 */
85 char name[32]; /* for logging */
86 unsigned data_seq:2; /* data sequencing level
87 * 0 => none, 1 => IP only,
90 unsigned recv_seq:1; /* expect receive packets with
91 * sequence numbers? */
92 unsigned send_seq:1; /* send packets with sequence
94 unsigned lns_mode:1; /* behave as LNS? LAC enables
95 * sequence numbers under
97 int debug; /* bitmask of debug message
99 int reorder_timeout; /* configured reorder timeout
104 struct l2tp_stats stats;
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);
111 uint8_t priv[0]; /* private data */
114 /* Describes the tunnel. It contains info to track all the associated
115 * sessions so incoming packets can be sorted out
117 struct l2tp_tunnel_cfg {
118 int debug; /* bitmask of debug message
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,
130 int version; /* 2=>L2TPv2, 3=>L2TPv3 */
132 char name[20]; /* for logging */
133 int debug; /* bitmask of debug message
136 struct l2tp_stats stats;
138 struct list_head list; /* Keep a list of all tunnels */
139 struct net *l2tp_net; /* the net we belong to */
143 int (*recv_payload_hook)(struct sk_buff *skb);
144 void (*old_sk_destruct)(struct sock *);
145 struct sock *sock; /* Parent socket */
148 uint8_t priv[0]; /* private data */
151 static inline void *l2tp_tunnel_priv(struct l2tp_tunnel *tunnel)
153 return &tunnel->priv[0];
156 static inline void *l2tp_session_priv(struct l2tp_session *session)
158 return &session->priv[0];
161 static inline struct l2tp_tunnel *l2tp_sock_to_tunnel(struct sock *sk)
163 struct l2tp_tunnel *tunnel;
169 tunnel = (struct l2tp_tunnel *)(sk->sk_user_data);
170 if (tunnel == NULL) {
175 BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);
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);
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);
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);
199 /* Tunnel reference counts. Incremented per session that is added to
202 static inline void l2tp_tunnel_inc_refcount_1(struct l2tp_tunnel *tunnel)
204 atomic_inc(&tunnel->ref_count);
207 static inline void l2tp_tunnel_dec_refcount_1(struct l2tp_tunnel *tunnel)
209 if (atomic_dec_and_test(&tunnel->ref_count))
210 l2tp_tunnel_free(tunnel);
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); \
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); \
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)
226 /* Session reference counts. Incremented when code obtains a reference
229 static inline void l2tp_session_inc_refcount_1(struct l2tp_session *session)
231 atomic_inc(&session->ref_count);
234 static inline void l2tp_session_dec_refcount_1(struct l2tp_session *session)
236 if (atomic_dec_and_test(&session->ref_count))
237 l2tp_session_free(session);
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); \
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); \
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)
254 #endif /* _L2TP_CORE_H_ */