l2tp_core.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. #include <linux/refcount.h>
  11. #ifndef _L2TP_CORE_H_
  12. #define _L2TP_CORE_H_
  13. #include <net/dst.h>
  14. #include <net/sock.h>
  15. #ifdef CONFIG_XFRM
  16. #include <net/xfrm.h>
  17. #endif
  18. /* Just some random numbers */
  19. #define L2TP_TUNNEL_MAGIC 0x42114DDA
  20. #define L2TP_SESSION_MAGIC 0x0C04EB7D
  21. /* Per tunnel, session hash table size */
  22. #define L2TP_HASH_BITS 4
  23. #define L2TP_HASH_SIZE (1 << L2TP_HASH_BITS)
  24. /* System-wide, session hash table size */
  25. #define L2TP_HASH_BITS_2 8
  26. #define L2TP_HASH_SIZE_2 (1 << L2TP_HASH_BITS_2)
  27. struct sk_buff;
  28. struct l2tp_stats {
  29. atomic_long_t tx_packets;
  30. atomic_long_t tx_bytes;
  31. atomic_long_t tx_errors;
  32. atomic_long_t rx_packets;
  33. atomic_long_t rx_bytes;
  34. atomic_long_t rx_seq_discards;
  35. atomic_long_t rx_oos_packets;
  36. atomic_long_t rx_errors;
  37. atomic_long_t rx_cookie_discards;
  38. };
  39. struct l2tp_tunnel;
  40. /* Describes a session. Contains information to determine incoming
  41. * packets and transmit outgoing ones.
  42. */
  43. struct l2tp_session_cfg {
  44. enum l2tp_pwtype pw_type;
  45. unsigned int recv_seq:1; /* expect receive packets with
  46. * sequence numbers? */
  47. unsigned int send_seq:1; /* send packets with sequence
  48. * numbers? */
  49. unsigned int lns_mode:1; /* behave as LNS? LAC enables
  50. * sequence numbers under
  51. * control of LNS. */
  52. int debug; /* bitmask of debug message
  53. * categories */
  54. u16 l2specific_type; /* Layer 2 specific type */
  55. u8 cookie[8]; /* optional cookie */
  56. int cookie_len; /* 0, 4 or 8 bytes */
  57. u8 peer_cookie[8]; /* peer's cookie */
  58. int peer_cookie_len; /* 0, 4 or 8 bytes */
  59. int reorder_timeout; /* configured reorder timeout
  60. * (in jiffies) */
  61. char *ifname;
  62. };
  63. struct l2tp_session {
  64. int magic; /* should be
  65. * L2TP_SESSION_MAGIC */
  66. long dead;
  67. struct l2tp_tunnel *tunnel; /* back pointer to tunnel
  68. * context */
  69. u32 session_id;
  70. u32 peer_session_id;
  71. u8 cookie[8];
  72. int cookie_len;
  73. u8 peer_cookie[8];
  74. int peer_cookie_len;
  75. u16 l2specific_type;
  76. u16 hdr_len;
  77. u32 nr; /* session NR state (receive) */
  78. u32 ns; /* session NR state (send) */
  79. struct sk_buff_head reorder_q; /* receive reorder queue */
  80. u32 nr_max; /* max NR. Depends on tunnel */
  81. u32 nr_window_size; /* NR window size */
  82. u32 nr_oos; /* NR of last OOS packet */
  83. int nr_oos_count; /* For OOS recovery */
  84. int nr_oos_count_max;
  85. struct hlist_node hlist; /* Hash list node */
  86. refcount_t ref_count;
  87. char name[32]; /* for logging */
  88. char ifname[IFNAMSIZ];
  89. unsigned int recv_seq:1; /* expect receive packets with
  90. * sequence numbers? */
  91. unsigned int send_seq:1; /* send packets with sequence
  92. * numbers? */
  93. unsigned int lns_mode:1; /* behave as LNS? LAC enables
  94. * sequence numbers under
  95. * control of LNS. */
  96. int debug; /* bitmask of debug message
  97. * categories */
  98. int reorder_timeout; /* configured reorder timeout
  99. * (in jiffies) */
  100. int reorder_skip; /* set if skip to next nr */
  101. enum l2tp_pwtype pwtype;
  102. struct l2tp_stats stats;
  103. struct hlist_node global_hlist; /* Global hash list node */
  104. int (*build_header)(struct l2tp_session *session, void *buf);
  105. void (*recv_skb)(struct l2tp_session *session, struct sk_buff *skb, int data_len);
  106. void (*session_close)(struct l2tp_session *session);
  107. void (*show)(struct seq_file *m, void *priv);
  108. uint8_t priv[0]; /* private data */
  109. };
  110. /* Describes the tunnel. It contains info to track all the associated
  111. * sessions so incoming packets can be sorted out
  112. */
  113. struct l2tp_tunnel_cfg {
  114. int debug; /* bitmask of debug message
  115. * categories */
  116. enum l2tp_encap_type encap;
  117. /* Used only for kernel-created sockets */
  118. struct in_addr local_ip;
  119. struct in_addr peer_ip;
  120. #if IS_ENABLED(CONFIG_IPV6)
  121. struct in6_addr *local_ip6;
  122. struct in6_addr *peer_ip6;
  123. #endif
  124. u16 local_udp_port;
  125. u16 peer_udp_port;
  126. unsigned int use_udp_checksums:1,
  127. udp6_zero_tx_checksums:1,
  128. udp6_zero_rx_checksums:1;
  129. };
  130. struct l2tp_tunnel {
  131. int magic; /* Should be L2TP_TUNNEL_MAGIC */
  132. unsigned long dead;
  133. struct rcu_head rcu;
  134. rwlock_t hlist_lock; /* protect session_hlist */
  135. bool acpt_newsess; /* Indicates whether this
  136. * tunnel accepts new sessions.
  137. * Protected by hlist_lock.
  138. */
  139. struct hlist_head session_hlist[L2TP_HASH_SIZE];
  140. /* hashed list of sessions,
  141. * hashed by id */
  142. u32 tunnel_id;
  143. u32 peer_tunnel_id;
  144. int version; /* 2=>L2TPv2, 3=>L2TPv3 */
  145. char name[20]; /* for logging */
  146. int debug; /* bitmask of debug message
  147. * categories */
  148. enum l2tp_encap_type encap;
  149. struct l2tp_stats stats;
  150. struct list_head list; /* Keep a list of all tunnels */
  151. struct net *l2tp_net; /* the net we belong to */
  152. refcount_t ref_count;
  153. void (*old_sk_destruct)(struct sock *);
  154. struct sock *sock; /* Parent socket */
  155. int fd; /* Parent fd, if tunnel socket
  156. * was created by userspace */
  157. struct work_struct del_work;
  158. };
  159. struct l2tp_nl_cmd_ops {
  160. int (*session_create)(struct net *net, struct l2tp_tunnel *tunnel,
  161. u32 session_id, u32 peer_session_id,
  162. struct l2tp_session_cfg *cfg);
  163. int (*session_delete)(struct l2tp_session *session);
  164. };
  165. static inline void *l2tp_session_priv(struct l2tp_session *session)
  166. {
  167. return &session->priv[0];
  168. }
  169. struct l2tp_tunnel *l2tp_tunnel_get(const struct net *net, u32 tunnel_id);
  170. struct l2tp_tunnel *l2tp_tunnel_get_nth(const struct net *net, int nth);
  171. struct l2tp_session *l2tp_tunnel_get_session(struct l2tp_tunnel *tunnel,
  172. u32 session_id);
  173. void l2tp_tunnel_free(struct l2tp_tunnel *tunnel);
  174. struct l2tp_session *l2tp_session_get(const struct net *net, u32 session_id);
  175. struct l2tp_session *l2tp_session_get_nth(struct l2tp_tunnel *tunnel, int nth);
  176. struct l2tp_session *l2tp_session_get_by_ifname(const struct net *net,
  177. const char *ifname);
  178. int l2tp_tunnel_create(struct net *net, int fd, int version, u32 tunnel_id,
  179. u32 peer_tunnel_id, struct l2tp_tunnel_cfg *cfg,
  180. struct l2tp_tunnel **tunnelp);
  181. int l2tp_tunnel_register(struct l2tp_tunnel *tunnel, struct net *net,
  182. struct l2tp_tunnel_cfg *cfg);
  183. void l2tp_tunnel_delete(struct l2tp_tunnel *tunnel);
  184. struct l2tp_session *l2tp_session_create(int priv_size,
  185. struct l2tp_tunnel *tunnel,
  186. u32 session_id, u32 peer_session_id,
  187. struct l2tp_session_cfg *cfg);
  188. int l2tp_session_register(struct l2tp_session *session,
  189. struct l2tp_tunnel *tunnel);
  190. void __l2tp_session_unhash(struct l2tp_session *session);
  191. int l2tp_session_delete(struct l2tp_session *session);
  192. void l2tp_session_free(struct l2tp_session *session);
  193. void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
  194. unsigned char *ptr, unsigned char *optr, u16 hdrflags,
  195. int length);
  196. int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb);
  197. void l2tp_session_set_header_len(struct l2tp_session *session, int version);
  198. int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb,
  199. int hdr_len);
  200. int l2tp_nl_register_ops(enum l2tp_pwtype pw_type,
  201. const struct l2tp_nl_cmd_ops *ops);
  202. void l2tp_nl_unregister_ops(enum l2tp_pwtype pw_type);
  203. int l2tp_ioctl(struct sock *sk, int cmd, unsigned long arg);
  204. static inline void l2tp_tunnel_inc_refcount(struct l2tp_tunnel *tunnel)
  205. {
  206. refcount_inc(&tunnel->ref_count);
  207. }
  208. static inline void l2tp_tunnel_dec_refcount(struct l2tp_tunnel *tunnel)
  209. {
  210. if (refcount_dec_and_test(&tunnel->ref_count))
  211. l2tp_tunnel_free(tunnel);
  212. }
  213. /* Session reference counts. Incremented when code obtains a reference
  214. * to a session.
  215. */
  216. static inline void l2tp_session_inc_refcount(struct l2tp_session *session)
  217. {
  218. refcount_inc(&session->ref_count);
  219. }
  220. static inline void l2tp_session_dec_refcount(struct l2tp_session *session)
  221. {
  222. if (refcount_dec_and_test(&session->ref_count))
  223. l2tp_session_free(session);
  224. }
  225. static inline int l2tp_get_l2specific_len(struct l2tp_session *session)
  226. {
  227. switch (session->l2specific_type) {
  228. case L2TP_L2SPECTYPE_DEFAULT:
  229. return 4;
  230. case L2TP_L2SPECTYPE_NONE:
  231. default:
  232. return 0;
  233. }
  234. }
  235. static inline u32 l2tp_tunnel_dst_mtu(const struct l2tp_tunnel *tunnel)
  236. {
  237. struct dst_entry *dst;
  238. u32 mtu;
  239. dst = sk_dst_get(tunnel->sock);
  240. if (!dst)
  241. return 0;
  242. mtu = dst_mtu(dst);
  243. dst_release(dst);
  244. return mtu;
  245. }
  246. #ifdef CONFIG_XFRM
  247. static inline bool l2tp_tunnel_uses_xfrm(const struct l2tp_tunnel *tunnel)
  248. {
  249. struct sock *sk = tunnel->sock;
  250. return sk && (rcu_access_pointer(sk->sk_policy[0]) ||
  251. rcu_access_pointer(sk->sk_policy[1]));
  252. }
  253. #else
  254. static inline bool l2tp_tunnel_uses_xfrm(const struct l2tp_tunnel *tunnel)
  255. {
  256. return false;
  257. }
  258. #endif
  259. static inline int l2tp_v3_ensure_opt_in_linear(struct l2tp_session *session, struct sk_buff *skb,
  260. unsigned char **ptr, unsigned char **optr)
  261. {
  262. int opt_len = session->peer_cookie_len + l2tp_get_l2specific_len(session);
  263. if (opt_len > 0) {
  264. int off = *ptr - *optr;
  265. if (!pskb_may_pull(skb, off + opt_len))
  266. return -1;
  267. if (skb->data != *optr) {
  268. *optr = skb->data;
  269. *ptr = skb->data + off;
  270. }
  271. }
  272. return 0;
  273. }
  274. #define l2tp_printk(ptr, type, func, fmt, ...) \
  275. do { \
  276. if (((ptr)->debug) & (type)) \
  277. func(fmt, ##__VA_ARGS__); \
  278. } while (0)
  279. #define l2tp_warn(ptr, type, fmt, ...) \
  280. l2tp_printk(ptr, type, pr_warn, fmt, ##__VA_ARGS__)
  281. #define l2tp_info(ptr, type, fmt, ...) \
  282. l2tp_printk(ptr, type, pr_info, fmt, ##__VA_ARGS__)
  283. #define l2tp_dbg(ptr, type, fmt, ...) \
  284. l2tp_printk(ptr, type, pr_debug, fmt, ##__VA_ARGS__)
  285. #define MODULE_ALIAS_L2TP_PWTYPE(type) \
  286. MODULE_ALIAS("net-l2tp-type-" __stringify(type))
  287. #endif /* _L2TP_CORE_H_ */