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 struct rcu_head rcu; 71 72 struct l2tp_tunnel *tunnel; /* back pointer to tunnel context */ 73 u32 session_id; 74 u32 peer_session_id; 75 u8 cookie[8]; 76 int cookie_len; 77 u8 peer_cookie[8]; 78 int peer_cookie_len; 79 u16 l2specific_type; 80 u16 hdr_len; 81 u32 nr; /* session NR state (receive) */ 82 u32 ns; /* session NR state (send) */ 83 struct sk_buff_head reorder_q; /* receive reorder queue */ 84 u32 nr_max; /* max NR. Depends on tunnel */ 85 u32 nr_window_size; /* NR window size */ 86 u32 nr_oos; /* NR of last OOS packet */ 87 int nr_oos_count; /* for OOS recovery */ 88 int nr_oos_count_max; 89 struct list_head list; /* per-tunnel list node */ 90 refcount_t ref_count; 91 struct hlist_node hlist; /* per-net session hlist */ 92 unsigned long hlist_key; /* key for session hlist */ 93 struct l2tp_session_coll_list *coll_list; /* session collision list */ 94 struct list_head clist; /* for coll_list */ 95 96 char name[L2TP_SESSION_NAME_MAX]; /* for logging */ 97 char ifname[IFNAMSIZ]; 98 unsigned int recv_seq:1; /* expect receive packets with sequence numbers? */ 99 unsigned int send_seq:1; /* send packets with sequence numbers? */ 100 unsigned int lns_mode:1; /* behave as LNS? 101 * LAC enables sequence numbers under LNS control. 102 */ 103 int reorder_timeout; /* configured reorder timeout (in jiffies) */ 104 int reorder_skip; /* set if skip to next nr */ 105 enum l2tp_pwtype pwtype; 106 struct l2tp_stats stats; 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 unlisting 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 enum l2tp_encap_type encap; 136 137 /* Used only for kernel-created sockets */ 138 struct in_addr local_ip; 139 struct in_addr peer_ip; 140 #if IS_ENABLED(CONFIG_IPV6) 141 struct in6_addr *local_ip6; 142 struct in6_addr *peer_ip6; 143 #endif 144 u16 local_udp_port; 145 u16 peer_udp_port; 146 unsigned int use_udp_checksums:1, 147 udp6_zero_tx_checksums:1, 148 udp6_zero_rx_checksums:1; 149 }; 150 151 /* Represents a tunnel instance. 152 * Tracks runtime state including IO statistics. 153 * Holds the tunnel socket (either passed from userspace or directly created by the kernel). 154 * Maintains a list of sessions belonging to the tunnel instance. 155 * Is linked into a per-net list of tunnels. 156 */ 157 #define L2TP_TUNNEL_NAME_MAX 20 158 struct l2tp_tunnel { 159 int magic; /* Should be L2TP_TUNNEL_MAGIC */ 160 161 unsigned long dead; 162 163 struct rcu_head rcu; 164 spinlock_t list_lock; /* write-protection for session_list */ 165 bool acpt_newsess; /* indicates whether this tunnel accepts 166 * new sessions. Protected by list_lock. 167 */ 168 struct list_head session_list; /* list of sessions */ 169 u32 tunnel_id; 170 u32 peer_tunnel_id; 171 int version; /* 2=>L2TPv2, 3=>L2TPv3 */ 172 173 char name[L2TP_TUNNEL_NAME_MAX]; /* for logging */ 174 enum l2tp_encap_type encap; 175 struct l2tp_stats stats; 176 177 struct net *l2tp_net; /* the net we belong to */ 178 179 refcount_t ref_count; 180 void (*old_sk_destruct)(struct sock *sk); 181 struct sock *sock; /* parent socket */ 182 int fd; /* parent fd, if tunnel socket was created 183 * by userspace 184 */ 185 186 struct work_struct del_work; 187 }; 188 189 /* Pseudowire ops callbacks for use with the l2tp genetlink interface */ 190 struct l2tp_nl_cmd_ops { 191 /* The pseudowire session create callback is responsible for creating a session 192 * instance for a specific pseudowire type. 193 * It must call l2tp_session_create and l2tp_session_register to register the 194 * session instance, as well as carry out any pseudowire-specific initialisation. 195 * It must return >= 0 on success, or an appropriate negative errno value on failure. 196 */ 197 int (*session_create)(struct net *net, struct l2tp_tunnel *tunnel, 198 u32 session_id, u32 peer_session_id, 199 struct l2tp_session_cfg *cfg); 200 201 /* The pseudowire session delete callback is responsible for initiating the deletion 202 * of a session instance. 203 * It must call l2tp_session_delete, as well as carry out any pseudowire-specific 204 * teardown actions. 205 */ 206 void (*session_delete)(struct l2tp_session *session); 207 }; 208 209 static inline void *l2tp_session_priv(struct l2tp_session *session) 210 { 211 return &session->priv[0]; 212 } 213 214 /* Tunnel and session refcounts */ 215 void l2tp_tunnel_inc_refcount(struct l2tp_tunnel *tunnel); 216 void l2tp_tunnel_dec_refcount(struct l2tp_tunnel *tunnel); 217 void l2tp_session_inc_refcount(struct l2tp_session *session); 218 void l2tp_session_dec_refcount(struct l2tp_session *session); 219 220 /* Tunnel and session lookup. 221 * These functions take a reference on the instances they return, so 222 * the caller must ensure that the reference is dropped appropriately. 223 */ 224 struct l2tp_tunnel *l2tp_tunnel_get(const struct net *net, u32 tunnel_id); 225 struct l2tp_tunnel *l2tp_tunnel_get_nth(const struct net *net, int nth); 226 227 struct l2tp_session *l2tp_v3_session_get(const struct net *net, struct sock *sk, u32 session_id); 228 struct l2tp_session *l2tp_v2_session_get(const struct net *net, u16 tunnel_id, u16 session_id); 229 struct l2tp_session *l2tp_session_get(const struct net *net, struct sock *sk, int pver, 230 u32 tunnel_id, u32 session_id); 231 struct l2tp_session *l2tp_session_get_nth(struct l2tp_tunnel *tunnel, int nth); 232 struct l2tp_session *l2tp_session_get_by_ifname(const struct net *net, 233 const char *ifname); 234 235 /* Tunnel and session lifetime management. 236 * Creation of a new instance is a two-step process: create, then register. 237 * Destruction is triggered using the *_delete functions, and completes asynchronously. 238 */ 239 int l2tp_tunnel_create(int fd, int version, u32 tunnel_id, 240 u32 peer_tunnel_id, struct l2tp_tunnel_cfg *cfg, 241 struct l2tp_tunnel **tunnelp); 242 int l2tp_tunnel_register(struct l2tp_tunnel *tunnel, struct net *net, 243 struct l2tp_tunnel_cfg *cfg); 244 void l2tp_tunnel_delete(struct l2tp_tunnel *tunnel); 245 246 struct l2tp_session *l2tp_session_create(int priv_size, 247 struct l2tp_tunnel *tunnel, 248 u32 session_id, u32 peer_session_id, 249 struct l2tp_session_cfg *cfg); 250 int l2tp_session_register(struct l2tp_session *session, 251 struct l2tp_tunnel *tunnel); 252 void l2tp_session_delete(struct l2tp_session *session); 253 254 /* Receive path helpers. If data sequencing is enabled for the session these 255 * functions handle queuing and reordering prior to passing packets to the 256 * pseudowire code to be passed to userspace. 257 */ 258 void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb, 259 unsigned char *ptr, unsigned char *optr, u16 hdrflags, 260 int length); 261 int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb); 262 263 /* Transmit path helpers for sending packets over the tunnel socket. */ 264 void l2tp_session_set_header_len(struct l2tp_session *session, int version, 265 enum l2tp_encap_type encap); 266 int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb); 267 268 /* Pseudowire management. 269 * Pseudowires should register with l2tp core on module init, and unregister 270 * on module exit. 271 */ 272 int l2tp_nl_register_ops(enum l2tp_pwtype pw_type, const struct l2tp_nl_cmd_ops *ops); 273 void l2tp_nl_unregister_ops(enum l2tp_pwtype pw_type); 274 275 /* IOCTL helper for IP encap modules. */ 276 int l2tp_ioctl(struct sock *sk, int cmd, int *karg); 277 278 /* Extract the tunnel structure from a socket's sk_user_data pointer, 279 * validating the tunnel magic feather. 280 */ 281 struct l2tp_tunnel *l2tp_sk_to_tunnel(struct sock *sk); 282 283 static inline int l2tp_get_l2specific_len(struct l2tp_session *session) 284 { 285 switch (session->l2specific_type) { 286 case L2TP_L2SPECTYPE_DEFAULT: 287 return 4; 288 case L2TP_L2SPECTYPE_NONE: 289 default: 290 return 0; 291 } 292 } 293 294 static inline u32 l2tp_tunnel_dst_mtu(const struct l2tp_tunnel *tunnel) 295 { 296 struct dst_entry *dst; 297 u32 mtu; 298 299 dst = sk_dst_get(tunnel->sock); 300 if (!dst) 301 return 0; 302 303 mtu = dst_mtu(dst); 304 dst_release(dst); 305 306 return mtu; 307 } 308 309 #ifdef CONFIG_XFRM 310 static inline bool l2tp_tunnel_uses_xfrm(const struct l2tp_tunnel *tunnel) 311 { 312 struct sock *sk = tunnel->sock; 313 314 return sk && (rcu_access_pointer(sk->sk_policy[0]) || 315 rcu_access_pointer(sk->sk_policy[1])); 316 } 317 #else 318 static inline bool l2tp_tunnel_uses_xfrm(const struct l2tp_tunnel *tunnel) 319 { 320 return false; 321 } 322 #endif 323 324 static inline int l2tp_v3_ensure_opt_in_linear(struct l2tp_session *session, struct sk_buff *skb, 325 unsigned char **ptr, unsigned char **optr) 326 { 327 int opt_len = session->peer_cookie_len + l2tp_get_l2specific_len(session); 328 329 if (opt_len > 0) { 330 int off = *ptr - *optr; 331 332 if (!pskb_may_pull(skb, off + opt_len)) 333 return -1; 334 335 if (skb->data != *optr) { 336 *optr = skb->data; 337 *ptr = skb->data + off; 338 } 339 } 340 341 return 0; 342 } 343 344 #define MODULE_ALIAS_L2TP_PWTYPE(type) \ 345 MODULE_ALIAS("net-l2tp-type-" __stringify(type)) 346 347 #endif /* _L2TP_CORE_H_ */ 348
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.