1 /* $OpenBSD: pipex_local.h,v 1.43 2021/07/27 09:29:09 mvs Exp $ */ 2 3 /* 4 * Copyright (c) 2009 Internet Initiative Japan Inc. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #define PIPEX_PPTP 1 30 #define PIPEX_L2TP 1 31 #define PIPEX_PPPOE 1 32 #define PIPEX_MPPE 1 33 34 #define PIPEX_REWIND_LIMIT 64 35 36 #define PIPEX_ENABLED 0x0001 37 38 /* compile time option constants */ 39 #ifndef PIPEX_MAX_SESSION 40 #define PIPEX_MAX_SESSION 512 41 #endif 42 #define PIPEX_HASH_DIV 8 43 #define PIPEX_HASH_SIZE (PIPEX_MAX_SESSION/PIPEX_HASH_DIV) 44 #define PIPEX_HASH_MASK (PIPEX_HASH_SIZE-1) 45 #define PIPEX_CLOSE_TIMEOUT 30 46 #define PIPEX_PPPMINLEN 5 47 /* minimum PPP header length is 1 and minimum ppp payload length is 4 */ 48 49 #ifndef NNBY /* usually defined on the <sys/types.h> */ 50 #define NNBY 8 /* number of bits of a byte */ 51 #endif 52 53 #define PIPEX_MPPE_NOLDKEY 64 /* should be power of two */ 54 #define PIPEX_MPPE_OLDKEYMASK (PIPEX_MPPE_NOLDKEY - 1) 55 56 /* 57 * Locks used to protect struct members: 58 * I immutable after creation 59 * N net lock 60 * s this pipex_session' `pxs_mtx' 61 * m this pipex_mppe' `pxm_mtx' 62 */ 63 64 #ifdef PIPEX_MPPE 65 /* mppe rc4 key */ 66 struct pipex_mppe { 67 struct mutex pxm_mtx; 68 int16_t stateless:1, /* [I] key change mode */ 69 resetreq:1, /* [m] */ 70 reserved:14; 71 int16_t keylenbits; /* [I] key length */ 72 int16_t keylen; /* [I] */ 73 uint16_t coher_cnt; /* [m] cohency counter */ 74 struct rc4_ctx rc4ctx; /* [m] */ 75 u_char master_key[PIPEX_MPPE_KEYLEN]; /* [m] master key of MPPE */ 76 u_char session_key[PIPEX_MPPE_KEYLEN]; /* [m] session key of MPPE */ 77 u_char (*old_session_keys)[PIPEX_MPPE_KEYLEN]; 78 /* [m] old session keys */ 79 }; 80 #endif /* PIPEX_MPPE */ 81 82 #ifdef PIPEX_PPPOE 83 struct pipex_pppoe_session { 84 u_int over_ifidx; /* [I] ether interface */ 85 }; 86 #endif /* PIPEX_PPPOE */ 87 88 #ifdef PIPEX_PPTP 89 struct pipex_pptp_session { 90 /* sequence number gap between pipex and userland */ 91 int32_t snd_gap; /* [N] gap of our sequence */ 92 int32_t rcv_gap; /* [N] gap of peer's sequence */ 93 int32_t ul_snd_una; /* [N] userland send acked seq */ 94 95 uint32_t snd_nxt; /* [N] send next */ 96 uint32_t rcv_nxt; /* [N] receive next */ 97 uint32_t snd_una; /* [N] send acked sequence */ 98 uint32_t rcv_acked; /* [N] recv acked sequence */ 99 100 int winsz; /* [I] windows size */ 101 int maxwinsz; /* [I] max windows size */ 102 int peer_maxwinsz; /* [I] peer's max windows size */ 103 }; 104 #endif /* PIPEX_PPTP */ 105 106 #ifdef PIPEX_L2TP 107 /* 108 * L2TP Packet headers 109 * 110 * +----+---+----+---+----+--------+ 111 * |IPv4|UDP|L2TP|PPP|IPv4|Data....| 112 * +----+---+----+---+----+--------+ 113 * 114 * Session Data 115 * 116 * IPv4 IP_SRC <-- required for encap. 117 * IP_DST <-- required for encap. 118 * 119 * UDP SPort <-- required for encap. 120 * DPort <-- required for encap. 121 * 122 * L2TP FLAGS <-- only handle TYPE=0 (data) 123 * Tunnel ID <-- ID per tunnel(NOT a key: differed from RFC) 124 * Session ID <-- ID per PPP session(KEY to look up session) 125 * Ns(SEND SEQ) <-- sequence number of packet to send(opt.) 126 * Nr(RECV SEQ) <-- sequence number of packet to recv(opt.) 127 * 128 * - Recv Session lookup key is (Tunnnel ID, Session ID) in RFC. 129 * - BUT (Session ID) in PIPEX. SESSION ID MUST BE UNIQ. 130 * 131 * - We must update (Ns, Nr) of data channel. and we must adjust (Ns, Nr) 132 * in packets from/to userland. 133 */ 134 struct pipex_l2tp_session { 135 /* KEYS for session lookup (host byte order) */ 136 uint16_t tunnel_id; /* [I] our tunnel-id */ 137 uint16_t peer_tunnel_id; /* [I] peer's tunnel-id */ 138 139 uint32_t option_flags; /* [I] protocol options */ 140 141 int16_t ns_gap; /* [N] gap between userland and pipex */ 142 int16_t nr_gap; /* [N] gap between userland and pipex */ 143 uint16_t ul_ns_una; /* [N] unacked sequence number (userland) */ 144 145 uint16_t ns_nxt; /* [N] next sequence number to send */ 146 uint16_t ns_una; /* [N] unacked sequence number to send */ 147 148 uint16_t nr_nxt; /* [N] next sequence number to recv */ 149 uint16_t nr_acked; /* [N] acked sequence number to recv */ 150 uint32_t ipsecflowinfo; /* [N] IPsec SA flow id for NAT-T */ 151 }; 152 #endif /* PIPEX_L2TP */ 153 154 struct cpumem; 155 156 /* pppac ip-extension sessoin table */ 157 struct pipex_session { 158 struct radix_node ps4_rn[2]; 159 /* [N] tree glue, and other values */ 160 struct radix_node ps6_rn[2]; 161 /* [N] tree glue, and other values */ 162 struct mutex pxs_mtx; 163 164 LIST_ENTRY(pipex_session) session_list; /* [N] all session chain */ 165 LIST_ENTRY(pipex_session) state_list; /* [N] state list chain */ 166 LIST_ENTRY(pipex_session) id_chain; /* [N] id hash chain */ 167 LIST_ENTRY(pipex_session) peer_addr_chain; 168 /* [N] peer's address hash chain */ 169 uint16_t state; /* [N] pipex session state */ 170 #define PIPEX_STATE_INITIAL 0x0000 171 #define PIPEX_STATE_OPENED 0x0001 172 #define PIPEX_STATE_CLOSE_WAIT 0x0002 173 #define PIPEX_STATE_CLOSE_WAIT2 0x0003 174 #define PIPEX_STATE_CLOSED 0x0004 175 176 uint32_t idle_time; /* [N] idle time in seconds */ 177 uint16_t ip_forward:1, /* [N] {en|dis}ableIP forwarding */ 178 ip6_forward:1, /* [I] {en|dis}able IPv6 forwarding */ 179 is_multicast:1, /* [I] virtual entry for multicast */ 180 is_pppx:1, /* [I] interface is point2point(pppx) */ 181 reserved:12; 182 uint16_t protocol; /* [I] tunnel protocol (PK) */ 183 uint16_t session_id; /* [I] session-id (PK) */ 184 uint16_t peer_session_id; /* [I] peer's session-id */ 185 uint16_t peer_mru; /* [I] peer's MRU */ 186 uint32_t timeout_sec; /* [I] idle timeout */ 187 int ppp_id; /* [I] PPP id */ 188 189 struct sockaddr_in ip_address; /* [I] remote address (AK) */ 190 struct sockaddr_in ip_netmask; /* [I] remote address mask (AK) */ 191 struct sockaddr_in6 ip6_address; /* [I] remote IPv6 address */ 192 int ip6_prefixlen; /* [I] remote IPv6 prefixlen */ 193 194 u_int ifindex; /* [N] interface index */ 195 void *ownersc; /* [I] owner context */ 196 197 uint32_t ppp_flags; /* [I] configure flags */ 198 #ifdef PIPEX_MPPE 199 int ccp_id; /* [s] CCP packet id */ 200 struct pipex_mppe 201 mppe_recv, /* MPPE context for incoming */ 202 mppe_send; /* MPPE context for outgoing */ 203 #endif /*PIPEXMPPE */ 204 205 struct cpumem *stat_counters; 206 207 union { 208 #ifdef PIPEX_PPPOE 209 struct pipex_pppoe_session pppoe; /* context for PPPoE */ 210 #endif /* PIPEX_PPPOE */ 211 #ifdef PIPEX_PPTP 212 struct pipex_pptp_session pptp; /* context for PPTP */ 213 #endif /* PIPEX_PPTP */ 214 #ifdef PIPEX_L2TP 215 struct pipex_l2tp_session l2tp; 216 #endif 217 char _proto_unknown[0]; 218 } proto; 219 union { 220 struct sockaddr sa; 221 struct sockaddr_in sin4; 222 struct sockaddr_in6 sin6; 223 struct sockaddr_dl sdl; 224 } peer, local; /* [I] */ 225 }; 226 227 enum pipex_counters { 228 pxc_ipackets, /* packets received from tunnel */ 229 pxc_ierrors, /* error packets received from tunnel */ 230 pxc_ibytes, /* number of received bytes from tunnel */ 231 pxc_opackets, /* packets sent to tunnel */ 232 pxc_oerrors, /* error packets on sending to tunnel */ 233 pxc_obytes, /* number of sent bytes to tunnel */ 234 pxc_ncounters 235 }; 236 237 /* gre header */ 238 struct pipex_gre_header { 239 uint16_t flags; /* flags and version*/ 240 #define PIPEX_GRE_KFLAG 0x2000 /* keys present */ 241 #define PIPEX_GRE_SFLAG 0x1000 /* seq present */ 242 #define PIPEX_GRE_AFLAG 0x0080 /* ack present */ 243 #define PIPEX_GRE_VER 0x0001 /* gre version code */ 244 #define PIPEX_GRE_VERMASK 0x0007 /* gre version mask */ 245 #define PIPEX_GRE_UNUSEDFLAGS 0xcf78 /* unused at pptp. set 0 in rfc2637 */ 246 247 uint16_t type; 248 #define PIPEX_GRE_PROTO_PPP 0x880b /* gre/ppp */ 249 250 uint16_t len; /* length not include gre header */ 251 uint16_t call_id; /* call_id */ 252 } __packed; 253 254 /* pppoe header */ 255 struct pipex_pppoe_header { 256 uint8_t vertype; /* version and type */ 257 #define PIPEX_PPPOE_VERTYPE 0x11 /* version and type code */ 258 259 uint8_t code; /* code */ 260 #define PIPEX_PPPOE_CODE_SESSION 0x00 /* code session */ 261 262 uint16_t session_id; /* session id */ 263 uint16_t length; /* length */ 264 } __packed; 265 266 /* l2tp header */ 267 struct pipex_l2tp_header { 268 uint16_t flagsver; 269 #define PIPEX_L2TP_FLAG_MASK 0xfff0 270 #define PIPEX_L2TP_FLAG_TYPE 0x8000 271 #define PIPEX_L2TP_FLAG_LENGTH 0x4000 272 #define PIPEX_L2TP_FLAG_SEQUENCE 0x0800 273 #define PIPEX_L2TP_FLAG_OFFSET 0x0200 274 #define PIPEX_L2TP_FLAG_PRIORITY 0x0100 275 #define PIPEX_L2TP_VER_MASK 0x000f 276 #define PIPEX_L2TP_VER 2 277 uint16_t length; /* optional */ 278 uint16_t tunnel_id; 279 uint16_t session_id; 280 /* can be followed by option header */ 281 } __packed; 282 283 /* l2tp option header */ 284 struct pipex_l2tp_seq_header { 285 uint16_t ns; 286 uint16_t nr; 287 } __packed; 288 289 struct pipex_l2tp_offset_header { 290 uint16_t offset_size; 291 /* uint8_t offset_pad[] */ 292 } __packed; 293 294 #ifdef PIPEX_DEBUG 295 #define PIPEX_DBG(a) if (pipex_debug & 1) pipex_session_log a 296 /* #define PIPEX_MPPE_DBG(a) if (pipex_debug & 1) pipex_session_log a */ 297 #define PIPEX_MPPE_DBG(a) 298 #else 299 #define PIPEX_DBG(a) 300 #define PIPEX_MPPE_DBG(a) 301 #endif /* PIPEX_DEBUG */ 302 303 LIST_HEAD(pipex_hash_head, pipex_session); 304 305 extern struct pipex_hash_head pipex_session_list; 306 extern struct pipex_hash_head pipex_close_wait_list; 307 extern struct pipex_hash_head pipex_peer_addr_hashtable[]; 308 extern struct pipex_hash_head pipex_id_hashtable[]; 309 extern struct pool pipex_session_pool; 310 311 312 #define PIPEX_ID_HASHTABLE(key) \ 313 (&pipex_id_hashtable[(key) & PIPEX_HASH_MASK]) 314 #define PIPEX_PEER_ADDR_HASHTABLE(key) \ 315 (&pipex_peer_addr_hashtable[(key) & PIPEX_HASH_MASK]) 316 317 #define GETCHAR(c, cp) do { \ 318 (c) = *(cp)++; \ 319 } while (0) 320 321 #define PUTCHAR(s, cp) do { \ 322 *(cp)++ = (u_char)(s); \ 323 } while (0) 324 325 #define GETSHORT(s, cp) do { \ 326 (s) = *(cp)++ << 8; \ 327 (s) |= *(cp)++; \ 328 } while (0) 329 330 #define PUTSHORT(s, cp) do { \ 331 *(cp)++ = (u_char) ((s) >> 8); \ 332 *(cp)++ = (u_char) (s); \ 333 } while (0) 334 335 #define GETLONG(l, cp) do { \ 336 (l) = *(cp)++ << 8; \ 337 (l) |= *(cp)++; (l) <<= 8; \ 338 (l) |= *(cp)++; (l) <<= 8; \ 339 (l) |= *(cp)++; \ 340 } while (0) 341 342 #define PUTLONG(l, cp) do { \ 343 *(cp)++ = (u_char) ((l) >> 24); \ 344 *(cp)++ = (u_char) ((l) >> 16); \ 345 *(cp)++ = (u_char) ((l) >> 8); \ 346 *(cp)++ = (u_char) (l); \ 347 } while (0) 348 349 #define PIPEX_PULLUP(m0, l) \ 350 if ((m0)->m_len < (l)) { \ 351 if ((m0)->m_pkthdr.len < (l)) { \ 352 PIPEX_DBG((NULL, LOG_DEBUG, \ 353 "<%s> received packet is too short.", \ 354 __func__)); \ 355 m_freem(m0); \ 356 (m0) = NULL; \ 357 } else { \ 358 (m0) = m_pullup((m0), (l)); \ 359 KASSERT((m0) != NULL); \ 360 } \ 361 } 362 #define PIPEX_SEEK_NEXTHDR(ptr, len, t) \ 363 ((t) (((char *)ptr) + len)) 364 #define SEQ32_LT(a,b) ((int32_t)((a) - (b)) < 0) 365 #define SEQ32_LE(a,b) ((int32_t)((a) - (b)) <= 0) 366 #define SEQ32_GT(a,b) ((int32_t)((a) - (b)) > 0) 367 #define SEQ32_GE(a,b) ((int32_t)((a) - (b)) >= 0) 368 #define SEQ32_SUB(a,b) ((int32_t)((a) - (b))) 369 370 #define SEQ16_LT(a,b) ((int16_t)((a) - (b)) < 0) 371 #define SEQ16_LE(a,b) ((int16_t)((a) - (b)) <= 0) 372 #define SEQ16_GT(a,b) ((int16_t)((a) - (b)) > 0) 373 #define SEQ16_GE(a,b) ((int16_t)((a) - (b)) >= 0) 374 #define SEQ16_SUB(a,b) ((int16_t)((a) - (b))) 375 376 #define pipex_session_is_acfc_accepted(s) \ 377 (((s)->ppp_flags & PIPEX_PPP_ACFC_ACCEPTED)? 1 : 0) 378 #define pipex_session_is_pfc_accepted(s) \ 379 (((s)->ppp_flags & PIPEX_PPP_PFC_ACCEPTED)? 1 : 0) 380 #define pipex_session_is_acfc_enabled(s) \ 381 (((s)->ppp_flags & PIPEX_PPP_ACFC_ENABLED)? 1 : 0) 382 #define pipex_session_is_pfc_enabled(s) \ 383 (((s)->ppp_flags & PIPEX_PPP_PFC_ENABLED)? 1 : 0) 384 #define pipex_session_has_acf(s) \ 385 (((s)->ppp_flags & PIPEX_PPP_HAS_ACF)? 1 : 0) 386 #define pipex_session_is_mppe_accepted(s) \ 387 (((s)->ppp_flags & PIPEX_PPP_MPPE_ACCEPTED)? 1 : 0) 388 #define pipex_session_is_mppe_enabled(s) \ 389 (((s)->ppp_flags & PIPEX_PPP_MPPE_ENABLED)? 1 : 0) 390 #define pipex_session_is_mppe_required(s) \ 391 (((s)->ppp_flags & PIPEX_PPP_MPPE_REQUIRED)? 1 : 0) 392 #define pipex_mppe_rc4_keybits(r) ((r)->keylen << 3) 393 #define pipex_session_is_l2tp_data_sequencing_on(s) \ 394 (((s)->proto.l2tp.option_flags & PIPEX_L2TP_USE_SEQUENCING) ? 1 : 0) 395 396 #define PIPEX_IPGRE_HDRLEN (sizeof(struct ip) + sizeof(struct pipex_gre_header)) 397 #define PIPEX_TCP_OPTLEN 40 398 #define PIPEX_L2TP_MINLEN 8 399 400 void pipex_destroy_all_sessions (void *); 401 int pipex_init_session(struct pipex_session **, 402 struct pipex_session_req *); 403 void pipex_rele_session(struct pipex_session *); 404 int pipex_link_session(struct pipex_session *, 405 struct ifnet *, void *); 406 void pipex_unlink_session(struct pipex_session *); 407 void pipex_export_session_stats(struct pipex_session *, 408 struct pipex_statistics *); 409 int pipex_config_session (struct pipex_session_config_req *, 410 void *); 411 int pipex_get_stat (struct pipex_session_stat_req *, 412 void *); 413 int pipex_get_closed (struct pipex_session_list_req *, 414 void *); 415 struct pipex_session *pipex_lookup_by_ip_address (struct in_addr); 416 struct pipex_session *pipex_lookup_by_session_id (int, int); 417 void pipex_ip_output (struct mbuf *, struct pipex_session *); 418 void pipex_ppp_output (struct mbuf *, struct pipex_session *, int); 419 int pipex_ppp_proto (struct mbuf *, struct pipex_session *, int, int *); 420 void pipex_ppp_input (struct mbuf *, struct pipex_session *, int); 421 void pipex_ip_input (struct mbuf *, struct pipex_session *); 422 #ifdef INET6 423 void pipex_ip6_input (struct mbuf *, struct pipex_session *); 424 #endif 425 struct mbuf *pipex_common_input(struct pipex_session *, 426 struct mbuf *, int, int); 427 428 #ifdef PIPEX_PPPOE 429 void pipex_pppoe_output (struct mbuf *, struct pipex_session *); 430 #endif 431 432 #ifdef PIPEX_PPTP 433 void pipex_pptp_output (struct mbuf *, struct pipex_session *, int, int); 434 struct pipex_session *pipex_pptp_userland_lookup_session(struct mbuf *, struct sockaddr *); 435 #endif 436 437 #ifdef PIPEX_L2TP 438 void pipex_l2tp_output (struct mbuf *, struct pipex_session *); 439 #endif 440 441 #ifdef PIPEX_MPPE 442 void pipex_mppe_init (struct pipex_mppe *, int, int, u_char *, int); 443 void GetNewKeyFromSHA (u_char *, u_char *, int, u_char *); 444 void pipex_mppe_reduce_key (struct pipex_mppe *); 445 void mppe_key_change (struct pipex_mppe *); 446 void pipex_mppe_input (struct mbuf *, struct pipex_session *); 447 void pipex_mppe_output (struct mbuf *, struct pipex_session *, uint16_t); 448 void pipex_ccp_input (struct mbuf *, struct pipex_session *); 449 int pipex_ccp_output (struct pipex_session *, int, int); 450 #endif 451 452 struct mbuf *adjust_tcp_mss (struct mbuf *, int); 453 struct mbuf *ip_is_idle_packet (struct mbuf *, int *); 454 void pipex_session_log (struct pipex_session *, int, const char *, ...) __attribute__((__format__(__printf__,3,4))); 455 uint32_t pipex_sockaddr_hash_key(struct sockaddr *); 456 int pipex_sockaddr_compar_addr(struct sockaddr *, struct sockaddr *); 457 int pipex_ppp_enqueue (struct mbuf *, struct pipex_session *, struct mbuf_queue *); 458 void pipex_timer_start (void); 459 void pipex_timer_stop (void); 460 void pipex_timer (void *); 461