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