1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* 3 * L2TP internal definitions. 4 * 5 * Copyright (c) 2008,2009 Katalix Systems Ltd 6 */ 7 #include <linux/refcount.h> 8 9 #ifndef _L2TP_CORE_H_ 10 #define _L2TP_CORE_H_ 11 12 #include <net/dst.h> 13 #include <net/sock.h> 14 15 #ifdef CONFIG_XFRM 16 #include <net/xfrm.h> 17 #endif 18 19 /* Just some random numbers */ 20 #define L2TP_TUNNEL_MAGIC 0x42114DDA 21 #define L2TP_SESSION_MAGIC 0x0C04EB7D 22 23 /* Per tunnel, session hash table size */ 24 #define L2TP_HASH_BITS 4 25 #define L2TP_HASH_SIZE (1 << L2TP_HASH_BITS) 26 27 /* System-wide, session hash table size */ 28 #define L2TP_HASH_BITS_2 8 29 #define L2TP_HASH_SIZE_2 (1 << L2TP_HASH_BITS_2) 30 31 struct sk_buff; 32 33 struct l2tp_stats { 34 atomic_long_t tx_packets; 35 atomic_long_t tx_bytes; 36 atomic_long_t tx_errors; 37 atomic_long_t rx_packets; 38 atomic_long_t rx_bytes; 39 atomic_long_t rx_seq_discards; 40 atomic_long_t rx_oos_packets; 41 atomic_long_t rx_errors; 42 atomic_long_t rx_cookie_discards; 43 }; 44 45 struct l2tp_tunnel; 46 47 /* Describes a session. Contains information to determine incoming 48 * packets and transmit outgoing ones. 49 */ 50 struct l2tp_session_cfg { 51 enum l2tp_pwtype pw_type; 52 unsigned int recv_seq:1; /* expect receive packets with 53 * sequence numbers? */ 54 unsigned int send_seq:1; /* send packets with sequence 55 * numbers? */ 56 unsigned int lns_mode:1; /* behave as LNS? LAC enables 57 * sequence numbers under 58 * control of LNS. */ 59 int debug; /* bitmask of debug message 60 * categories */ 61 u16 l2specific_type; /* Layer 2 specific type */ 62 u8 cookie[8]; /* optional cookie */ 63 int cookie_len; /* 0, 4 or 8 bytes */ 64 u8 peer_cookie[8]; /* peer's cookie */ 65 int peer_cookie_len; /* 0, 4 or 8 bytes */ 66 int reorder_timeout; /* configured reorder timeout 67 * (in jiffies) */ 68 char *ifname; 69 }; 70 71 struct l2tp_session { 72 int magic; /* should be 73 * L2TP_SESSION_MAGIC */ 74 long dead; 75 76 struct l2tp_tunnel *tunnel; /* back pointer to tunnel 77 * context */ 78 u32 session_id; 79 u32 peer_session_id; 80 u8 cookie[8]; 81 int cookie_len; 82 u8 peer_cookie[8]; 83 int peer_cookie_len; 84 u16 l2specific_type; 85 u16 hdr_len; 86 u32 nr; /* session NR state (receive) */ 87 u32 ns; /* session NR state (send) */ 88 struct sk_buff_head reorder_q; /* receive reorder queue */ 89 u32 nr_max; /* max NR. Depends on tunnel */ 90 u32 nr_window_size; /* NR window size */ 91 u32 nr_oos; /* NR of last OOS packet */ 92 int nr_oos_count; /* For OOS recovery */ 93 int nr_oos_count_max; 94 struct hlist_node hlist; /* Hash list node */ 95 refcount_t ref_count; 96 97 char name[32]; /* for logging */ 98 char ifname[IFNAMSIZ]; 99 unsigned int recv_seq:1; /* expect receive packets with 100 * sequence numbers? */ 101 unsigned int send_seq:1; /* send packets with sequence 102 * numbers? */ 103 unsigned int lns_mode:1; /* behave as LNS? LAC enables 104 * sequence numbers under 105 * control of LNS. */ 106 int debug; /* bitmask of debug message 107 * categories */ 108 int reorder_timeout; /* configured reorder timeout 109 * (in jiffies) */ 110 int reorder_skip; /* set if skip to next nr */ 111 enum l2tp_pwtype pwtype; 112 struct l2tp_stats stats; 113 struct hlist_node global_hlist; /* Global hash list node */ 114 115 int (*build_header)(struct l2tp_session *session, void *buf); 116 void (*recv_skb)(struct l2tp_session *session, struct sk_buff *skb, int data_len); 117 void (*session_close)(struct l2tp_session *session); 118 void (*show)(struct seq_file *m, void *priv); 119 u8 priv[]; /* private data */ 120 }; 121 122 /* Describes the tunnel. It contains info to track all the associated 123 * sessions so incoming packets can be sorted out 124 */ 125 struct l2tp_tunnel_cfg { 126 int debug; /* bitmask of debug message 127 * categories */ 128 enum l2tp_encap_type encap; 129 130 /* Used only for kernel-created sockets */ 131 struct in_addr local_ip; 132 struct in_addr peer_ip; 133 #if IS_ENABLED(CONFIG_IPV6) 134 struct in6_addr *local_ip6; 135 struct in6_addr *peer_ip6; 136 #endif 137 u16 local_udp_port; 138 u16 peer_udp_port; 139 unsigned int use_udp_checksums:1, 140 udp6_zero_tx_checksums:1, 141 udp6_zero_rx_checksums:1; 142 }; 143 144 struct l2tp_tunnel { 145 int magic; /* Should be L2TP_TUNNEL_MAGIC */ 146 147 unsigned long dead; 148 149 struct rcu_head rcu; 150 rwlock_t hlist_lock; /* protect session_hlist */ 151 bool acpt_newsess; /* Indicates whether this 152 * tunnel accepts new sessions. 153 * Protected by hlist_lock. 154 */ 155 struct hlist_head session_hlist[L2TP_HASH_SIZE]; 156 /* hashed list of sessions, 157 * hashed by id */ 158 u32 tunnel_id; 159 u32 peer_tunnel_id; 160 int version; /* 2=>L2TPv2, 3=>L2TPv3 */ 161 162 char name[20]; /* for logging */ 163 int debug; /* bitmask of debug message 164 * categories */ 165 enum l2tp_encap_type encap; 166 struct l2tp_stats stats; 167 168 struct list_head list; /* Keep a list of all tunnels */ 169 struct net *l2tp_net; /* the net we belong to */ 170 171 refcount_t ref_count; 172 void (*old_sk_destruct)(struct sock *); 173 struct sock *sock; /* Parent socket */ 174 int fd; /* Parent fd, if tunnel socket 175 * was created by userspace */ 176 177 struct work_struct del_work; 178 }; 179 180 struct l2tp_nl_cmd_ops { 181 int (*session_create)(struct net *net, struct l2tp_tunnel *tunnel, 182 u32 session_id, u32 peer_session_id, 183 struct l2tp_session_cfg *cfg); 184 int (*session_delete)(struct l2tp_session *session); 185 }; 186 187 static inline void *l2tp_session_priv(struct l2tp_session *session) 188 { 189 return &session->priv[0]; 190 } 191 192 struct l2tp_tunnel *l2tp_tunnel_get(const struct net *net, u32 tunnel_id); 193 struct l2tp_tunnel *l2tp_tunnel_get_nth(const struct net *net, int nth); 194 struct l2tp_session *l2tp_tunnel_get_session(struct l2tp_tunnel *tunnel, 195 u32 session_id); 196 197 void l2tp_tunnel_free(struct l2tp_tunnel *tunnel); 198 199 struct l2tp_session *l2tp_session_get(const struct net *net, u32 session_id); 200 struct l2tp_session *l2tp_session_get_nth(struct l2tp_tunnel *tunnel, int nth); 201 struct l2tp_session *l2tp_session_get_by_ifname(const struct net *net, 202 const char *ifname); 203 204 int l2tp_tunnel_create(struct net *net, 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 210 void l2tp_tunnel_delete(struct l2tp_tunnel *tunnel); 211 struct l2tp_session *l2tp_session_create(int priv_size, 212 struct l2tp_tunnel *tunnel, 213 u32 session_id, u32 peer_session_id, 214 struct l2tp_session_cfg *cfg); 215 int l2tp_session_register(struct l2tp_session *session, 216 struct l2tp_tunnel *tunnel); 217 218 void __l2tp_session_unhash(struct l2tp_session *session); 219 int l2tp_session_delete(struct l2tp_session *session); 220 void l2tp_session_free(struct l2tp_session *session); 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 void l2tp_session_set_header_len(struct l2tp_session *session, int version); 226 227 int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, 228 int hdr_len); 229 230 int l2tp_nl_register_ops(enum l2tp_pwtype pw_type, 231 const struct l2tp_nl_cmd_ops *ops); 232 void l2tp_nl_unregister_ops(enum l2tp_pwtype pw_type); 233 int l2tp_ioctl(struct sock *sk, int cmd, unsigned long arg); 234 235 static inline void l2tp_tunnel_inc_refcount(struct l2tp_tunnel *tunnel) 236 { 237 refcount_inc(&tunnel->ref_count); 238 } 239 240 static inline void l2tp_tunnel_dec_refcount(struct l2tp_tunnel *tunnel) 241 { 242 if (refcount_dec_and_test(&tunnel->ref_count)) 243 l2tp_tunnel_free(tunnel); 244 } 245 246 /* Session reference counts. Incremented when code obtains a reference 247 * to a session. 248 */ 249 static inline void l2tp_session_inc_refcount(struct l2tp_session *session) 250 { 251 refcount_inc(&session->ref_count); 252 } 253 254 static inline void l2tp_session_dec_refcount(struct l2tp_session *session) 255 { 256 if (refcount_dec_and_test(&session->ref_count)) 257 l2tp_session_free(session); 258 } 259 260 static inline int l2tp_get_l2specific_len(struct l2tp_session *session) 261 { 262 switch (session->l2specific_type) { 263 case L2TP_L2SPECTYPE_DEFAULT: 264 return 4; 265 case L2TP_L2SPECTYPE_NONE: 266 default: 267 return 0; 268 } 269 } 270 271 static inline u32 l2tp_tunnel_dst_mtu(const struct l2tp_tunnel *tunnel) 272 { 273 struct dst_entry *dst; 274 u32 mtu; 275 276 dst = sk_dst_get(tunnel->sock); 277 if (!dst) 278 return 0; 279 280 mtu = dst_mtu(dst); 281 dst_release(dst); 282 283 return mtu; 284 } 285 286 #ifdef CONFIG_XFRM 287 static inline bool l2tp_tunnel_uses_xfrm(const struct l2tp_tunnel *tunnel) 288 { 289 struct sock *sk = tunnel->sock; 290 291 return sk && (rcu_access_pointer(sk->sk_policy[0]) || 292 rcu_access_pointer(sk->sk_policy[1])); 293 } 294 #else 295 static inline bool l2tp_tunnel_uses_xfrm(const struct l2tp_tunnel *tunnel) 296 { 297 return false; 298 } 299 #endif 300 301 static inline int l2tp_v3_ensure_opt_in_linear(struct l2tp_session *session, struct sk_buff *skb, 302 unsigned char **ptr, unsigned char **optr) 303 { 304 int opt_len = session->peer_cookie_len + l2tp_get_l2specific_len(session); 305 306 if (opt_len > 0) { 307 int off = *ptr - *optr; 308 309 if (!pskb_may_pull(skb, off + opt_len)) 310 return -1; 311 312 if (skb->data != *optr) { 313 *optr = skb->data; 314 *ptr = skb->data + off; 315 } 316 } 317 318 return 0; 319 } 320 321 #define l2tp_printk(ptr, type, func, fmt, ...) \ 322 do { \ 323 if (((ptr)->debug) & (type)) \ 324 func(fmt, ##__VA_ARGS__); \ 325 } while (0) 326 327 #define l2tp_warn(ptr, type, fmt, ...) \ 328 l2tp_printk(ptr, type, pr_warn, fmt, ##__VA_ARGS__) 329 #define l2tp_info(ptr, type, fmt, ...) \ 330 l2tp_printk(ptr, type, pr_info, fmt, ##__VA_ARGS__) 331 #define l2tp_dbg(ptr, type, fmt, ...) \ 332 l2tp_printk(ptr, type, pr_debug, fmt, ##__VA_ARGS__) 333 334 #define MODULE_ALIAS_L2TP_PWTYPE(type) \ 335 MODULE_ALIAS("net-l2tp-type-" __stringify(type)) 336 337 #endif /* _L2TP_CORE_H_ */ 338