l2tp_core.h 7.8 KB

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