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