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