l2tp_core.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /* L2TP internal definitions.
  3. *
  4. * Copyright (c) 2008,2009 Katalix Systems Ltd
  5. */
  6. #include <linux/refcount.h>
  7. #ifndef _L2TP_CORE_H_
  8. #define _L2TP_CORE_H_
  9. #include <net/dst.h>
  10. #include <net/sock.h>
  11. #ifdef CONFIG_XFRM
  12. #include <net/xfrm.h>
  13. #endif
  14. /* Random numbers used for internal consistency checks of tunnel and session structures */
  15. #define L2TP_SESSION_MAGIC 0x0C04EB7D
  16. struct sk_buff;
  17. struct l2tp_stats {
  18. atomic_long_t tx_packets;
  19. atomic_long_t tx_bytes;
  20. atomic_long_t tx_errors;
  21. atomic_long_t rx_packets;
  22. atomic_long_t rx_bytes;
  23. atomic_long_t rx_seq_discards;
  24. atomic_long_t rx_oos_packets;
  25. atomic_long_t rx_errors;
  26. atomic_long_t rx_cookie_discards;
  27. atomic_long_t rx_invalid;
  28. };
  29. struct l2tp_tunnel;
  30. /* L2TP session configuration */
  31. struct l2tp_session_cfg {
  32. enum l2tp_pwtype pw_type;
  33. unsigned int recv_seq:1; /* expect receive packets with sequence numbers? */
  34. unsigned int send_seq:1; /* send packets with sequence numbers? */
  35. unsigned int lns_mode:1; /* behave as LNS?
  36. * LAC enables sequence numbers under LNS control.
  37. */
  38. u16 l2specific_type; /* Layer 2 specific type */
  39. u8 cookie[8]; /* optional cookie */
  40. int cookie_len; /* 0, 4 or 8 bytes */
  41. u8 peer_cookie[8]; /* peer's cookie */
  42. int peer_cookie_len; /* 0, 4 or 8 bytes */
  43. int reorder_timeout; /* configured reorder timeout (in jiffies) */
  44. char *ifname;
  45. };
  46. struct l2tp_session_coll_list {
  47. spinlock_t lock; /* for access to list */
  48. struct list_head list;
  49. refcount_t ref_count;
  50. };
  51. /* Represents a session (pseudowire) instance.
  52. * Tracks runtime state including cookies, dataplane packet sequencing, and IO statistics.
  53. * Is linked into a per-tunnel session list and a per-net ("global") IDR tree.
  54. */
  55. #define L2TP_SESSION_NAME_MAX 32
  56. struct l2tp_session {
  57. int magic; /* should be L2TP_SESSION_MAGIC */
  58. long dead;
  59. struct rcu_head rcu;
  60. struct l2tp_tunnel *tunnel; /* back pointer to tunnel context */
  61. u32 session_id;
  62. u32 peer_session_id;
  63. u8 cookie[8];
  64. int cookie_len;
  65. u8 peer_cookie[8];
  66. int peer_cookie_len;
  67. u16 l2specific_type;
  68. u16 hdr_len;
  69. u32 nr; /* session NR state (receive) */
  70. u32 ns; /* session NR state (send) */
  71. struct sk_buff_head reorder_q; /* receive reorder queue */
  72. u32 nr_max; /* max NR. Depends on tunnel */
  73. u32 nr_window_size; /* NR window size */
  74. u32 nr_oos; /* NR of last OOS packet */
  75. int nr_oos_count; /* for OOS recovery */
  76. int nr_oos_count_max;
  77. struct list_head list; /* per-tunnel list node */
  78. refcount_t ref_count;
  79. struct hlist_node hlist; /* per-net session hlist */
  80. unsigned long hlist_key; /* key for session hlist */
  81. struct l2tp_session_coll_list *coll_list; /* session collision list */
  82. struct list_head clist; /* for coll_list */
  83. char name[L2TP_SESSION_NAME_MAX]; /* for logging */
  84. char ifname[IFNAMSIZ];
  85. unsigned int recv_seq:1; /* expect receive packets with sequence numbers? */
  86. unsigned int send_seq:1; /* send packets with sequence numbers? */
  87. unsigned int lns_mode:1; /* behave as LNS?
  88. * LAC enables sequence numbers under LNS control.
  89. */
  90. int reorder_timeout; /* configured reorder timeout (in jiffies) */
  91. int reorder_skip; /* set if skip to next nr */
  92. enum l2tp_pwtype pwtype;
  93. struct l2tp_stats stats;
  94. struct work_struct del_work;
  95. /* Session receive handler for data packets.
  96. * Each pseudowire implementation should implement this callback in order to
  97. * handle incoming packets. Packets are passed to the pseudowire handler after
  98. * reordering, if data sequence numbers are enabled for the session.
  99. */
  100. void (*recv_skb)(struct l2tp_session *session, struct sk_buff *skb, int data_len);
  101. /* Session close handler.
  102. * Each pseudowire implementation may implement this callback in order to carry
  103. * out pseudowire-specific shutdown actions.
  104. * The callback is called by core after unlisting the session and purging its
  105. * reorder queue.
  106. */
  107. void (*session_close)(struct l2tp_session *session);
  108. /* Session show handler.
  109. * Pseudowire-specific implementation of debugfs session rendering.
  110. * The callback is called by l2tp_debugfs.c after rendering core session
  111. * information.
  112. */
  113. void (*show)(struct seq_file *m, void *priv);
  114. u8 priv[]; /* private data */
  115. };
  116. /* L2TP tunnel configuration */
  117. struct l2tp_tunnel_cfg {
  118. enum l2tp_encap_type encap;
  119. /* Used only for kernel-created sockets */
  120. struct in_addr local_ip;
  121. struct in_addr peer_ip;
  122. #if IS_ENABLED(CONFIG_IPV6)
  123. struct in6_addr *local_ip6;
  124. struct in6_addr *peer_ip6;
  125. #endif
  126. u16 local_udp_port;
  127. u16 peer_udp_port;
  128. unsigned int use_udp_checksums:1,
  129. udp6_zero_tx_checksums:1,
  130. udp6_zero_rx_checksums:1;
  131. };
  132. /* Represents a tunnel instance.
  133. * Tracks runtime state including IO statistics.
  134. * Holds the tunnel socket (either passed from userspace or directly created by the kernel).
  135. * Maintains a list of sessions belonging to the tunnel instance.
  136. * Is linked into a per-net list of tunnels.
  137. */
  138. #define L2TP_TUNNEL_NAME_MAX 20
  139. struct l2tp_tunnel {
  140. unsigned long dead;
  141. struct rcu_head rcu;
  142. spinlock_t list_lock; /* write-protection for session_list */
  143. bool acpt_newsess; /* indicates whether this tunnel accepts
  144. * new sessions. Protected by list_lock.
  145. */
  146. struct list_head session_list; /* list of sessions */
  147. u32 tunnel_id;
  148. u32 peer_tunnel_id;
  149. int version; /* 2=>L2TPv2, 3=>L2TPv3 */
  150. char name[L2TP_TUNNEL_NAME_MAX]; /* for logging */
  151. enum l2tp_encap_type encap;
  152. struct l2tp_stats stats;
  153. struct net *l2tp_net; /* the net we belong to */
  154. refcount_t ref_count;
  155. struct sock *sock; /* parent socket */
  156. int fd; /* parent fd, if tunnel socket was created
  157. * by userspace
  158. */
  159. struct work_struct del_work;
  160. };
  161. /* Pseudowire ops callbacks for use with the l2tp genetlink interface */
  162. struct l2tp_nl_cmd_ops {
  163. /* The pseudowire session create callback is responsible for creating a session
  164. * instance for a specific pseudowire type.
  165. * It must call l2tp_session_create and l2tp_session_register to register the
  166. * session instance, as well as carry out any pseudowire-specific initialisation.
  167. * It must return >= 0 on success, or an appropriate negative errno value on failure.
  168. */
  169. int (*session_create)(struct net *net, struct l2tp_tunnel *tunnel,
  170. u32 session_id, u32 peer_session_id,
  171. struct l2tp_session_cfg *cfg);
  172. /* The pseudowire session delete callback is responsible for initiating the deletion
  173. * of a session instance.
  174. * It must call l2tp_session_delete, as well as carry out any pseudowire-specific
  175. * teardown actions.
  176. */
  177. void (*session_delete)(struct l2tp_session *session);
  178. };
  179. static inline void *l2tp_session_priv(struct l2tp_session *session)
  180. {
  181. return &session->priv[0];
  182. }
  183. /* Tunnel and session refcounts */
  184. void l2tp_tunnel_put(struct l2tp_tunnel *tunnel);
  185. void l2tp_session_put(struct l2tp_session *session);
  186. /* Tunnel and session lookup.
  187. * These functions take a reference on the instances they return, so
  188. * the caller must ensure that the reference is dropped appropriately.
  189. */
  190. struct l2tp_tunnel *l2tp_tunnel_get(const struct net *net, u32 tunnel_id);
  191. struct l2tp_tunnel *l2tp_tunnel_get_next(const struct net *net, unsigned long *key);
  192. struct l2tp_session *l2tp_v3_session_get(const struct net *net, struct sock *sk, u32 session_id);
  193. struct l2tp_session *l2tp_v2_session_get(const struct net *net, u16 tunnel_id, u16 session_id);
  194. struct l2tp_session *l2tp_session_get(const struct net *net, struct sock *sk, int pver,
  195. u32 tunnel_id, u32 session_id);
  196. struct l2tp_session *l2tp_session_get_next(const struct net *net, struct sock *sk, int pver,
  197. u32 tunnel_id, unsigned long *key);
  198. struct l2tp_session *l2tp_session_get_by_ifname(const struct net *net,
  199. const char *ifname);
  200. /* Tunnel and session lifetime management.
  201. * Creation of a new instance is a two-step process: create, then register.
  202. * Destruction is triggered using the *_delete functions, and completes asynchronously.
  203. */
  204. int l2tp_tunnel_create(int fd, int version, u32 tunnel_id,
  205. u32 peer_tunnel_id, struct l2tp_tunnel_cfg *cfg,
  206. struct l2tp_tunnel **tunnelp);
  207. int l2tp_tunnel_register(struct l2tp_tunnel *tunnel, struct net *net,
  208. struct l2tp_tunnel_cfg *cfg);
  209. void l2tp_tunnel_delete(struct l2tp_tunnel *tunnel);
  210. struct l2tp_session *l2tp_session_create(int priv_size,
  211. struct l2tp_tunnel *tunnel,
  212. u32 session_id, u32 peer_session_id,
  213. struct l2tp_session_cfg *cfg);
  214. int l2tp_session_register(struct l2tp_session *session,
  215. struct l2tp_tunnel *tunnel);
  216. void l2tp_session_delete(struct l2tp_session *session);
  217. /* Receive path helpers. If data sequencing is enabled for the session these
  218. * functions handle queuing and reordering prior to passing packets to the
  219. * pseudowire code to be passed to userspace.
  220. */
  221. void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
  222. unsigned char *ptr, unsigned char *optr, u16 hdrflags,
  223. int length);
  224. int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb);
  225. /* Transmit path helpers for sending packets over the tunnel socket. */
  226. void l2tp_session_set_header_len(struct l2tp_session *session, int version,
  227. enum l2tp_encap_type encap);
  228. int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb);
  229. /* Pseudowire management.
  230. * Pseudowires should register with l2tp core on module init, and unregister
  231. * on module exit.
  232. */
  233. int l2tp_nl_register_ops(enum l2tp_pwtype pw_type, const struct l2tp_nl_cmd_ops *ops);
  234. void l2tp_nl_unregister_ops(enum l2tp_pwtype pw_type);
  235. /* IOCTL helper for IP encap modules. */
  236. int l2tp_ioctl(struct sock *sk, int cmd, int *karg);
  237. struct l2tp_tunnel *l2tp_sk_to_tunnel(const struct sock *sk);
  238. static inline int l2tp_get_l2specific_len(struct l2tp_session *session)
  239. {
  240. switch (session->l2specific_type) {
  241. case L2TP_L2SPECTYPE_DEFAULT:
  242. return 4;
  243. case L2TP_L2SPECTYPE_NONE:
  244. default:
  245. return 0;
  246. }
  247. }
  248. static inline u32 l2tp_tunnel_dst_mtu(const struct l2tp_tunnel *tunnel)
  249. {
  250. struct dst_entry *dst;
  251. u32 mtu;
  252. dst = sk_dst_get(tunnel->sock);
  253. if (!dst)
  254. return 0;
  255. mtu = dst_mtu(dst);
  256. dst_release(dst);
  257. return mtu;
  258. }
  259. #ifdef CONFIG_XFRM
  260. static inline bool l2tp_tunnel_uses_xfrm(const struct l2tp_tunnel *tunnel)
  261. {
  262. struct sock *sk = tunnel->sock;
  263. return sk && (rcu_access_pointer(sk->sk_policy[0]) ||
  264. rcu_access_pointer(sk->sk_policy[1]));
  265. }
  266. #else
  267. static inline bool l2tp_tunnel_uses_xfrm(const struct l2tp_tunnel *tunnel)
  268. {
  269. return false;
  270. }
  271. #endif
  272. static inline int l2tp_v3_ensure_opt_in_linear(struct l2tp_session *session, struct sk_buff *skb,
  273. unsigned char **ptr, unsigned char **optr)
  274. {
  275. int opt_len = session->peer_cookie_len + l2tp_get_l2specific_len(session);
  276. if (opt_len > 0) {
  277. int off = *ptr - *optr;
  278. if (!pskb_may_pull(skb, off + opt_len))
  279. return -1;
  280. if (skb->data != *optr) {
  281. *optr = skb->data;
  282. *ptr = skb->data + off;
  283. }
  284. }
  285. return 0;
  286. }
  287. #define MODULE_ALIAS_L2TP_PWTYPE(type) \
  288. MODULE_ALIAS("net-l2tp-type-" __stringify(type))
  289. #endif /* _L2TP_CORE_H_ */