1 /* $OpenBSD: pipex_local.h,v 1.41 2021/01/04 12:21:38 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 */ 61 62 #ifdef PIPEX_MPPE 63 /* mppe rc4 key */ 64 struct pipex_mppe { 65 int16_t stateless:1, /* [I] key change mode */ 66 resetreq:1, /* [N] */ 67 reserved:14; 68 int16_t keylenbits; /* [I] key length */ 69 int16_t keylen; /* [I] */ 70 uint16_t coher_cnt; /* [N] cohency counter */ 71 struct rc4_ctx rc4ctx; /* [N] */ 72 u_char master_key[PIPEX_MPPE_KEYLEN]; /* [N] master key of MPPE */ 73 u_char session_key[PIPEX_MPPE_KEYLEN]; /* [N] session key of MPPE */ 74 u_char (*old_session_keys)[PIPEX_MPPE_KEYLEN]; 75 /* [N] old session keys */ 76 }; 77 #endif /* PIPEX_MPPE */ 78 79 #ifdef PIPEX_PPPOE 80 struct pipex_pppoe_session { 81 u_int over_ifidx; /* [I] ether interface */ 82 }; 83 #endif /* PIPEX_PPPOE */ 84 85 #ifdef PIPEX_PPTP 86 struct pipex_pptp_session { 87 /* sequence number gap between pipex and userland */ 88 int32_t snd_gap; /* [N] gap of our sequence */ 89 int32_t rcv_gap; /* [N] gap of peer's sequence */ 90 int32_t ul_snd_una; /* [N] userland send acked seq */ 91 92 uint32_t snd_nxt; /* [N] send next */ 93 uint32_t rcv_nxt; /* [N] receive next */ 94 uint32_t snd_una; /* [N] send acked sequence */ 95 uint32_t rcv_acked; /* [N] recv acked sequence */ 96 97 int winsz; /* [I] windows size */ 98 int maxwinsz; /* [I] max windows size */ 99 int peer_maxwinsz; /* [I] peer's max windows size */ 100 }; 101 #endif /* PIPEX_PPTP */ 102 103 #ifdef PIPEX_L2TP 104 /* 105 * L2TP Packet headers 106 * 107 * +----+---+----+---+----+--------+ 108 * |IPv4|UDP|L2TP|PPP|IPv4|Data....| 109 * +----+---+----+---+----+--------+ 110 * 111 * Session Data 112 * 113 * IPv4 IP_SRC <-- required for encap. 114 * IP_DST <-- required for encap. 115 * 116 * UDP SPort <-- required for encap. 117 * DPort <-- required for encap. 118 * 119 * L2TP FLAGS <-- only handle TYPE=0 (data) 120 * Tunnel ID <-- ID per tunnel(NOT a key: differed from RFC) 121 * Session ID <-- ID per PPP session(KEY to look up session) 122 * Ns(SEND SEQ) <-- sequence number of packet to send(opt.) 123 * Nr(RECV SEQ) <-- sequence number of packet to recv(opt.) 124 * 125 * - Recv Session lookup key is (Tunnnel ID, Session ID) in RFC. 126 * - BUT (Session ID) in PIPEX. SESSION ID MUST BE UNIQ. 127 * 128 * - We must update (Ns, Nr) of data channel. and we must adjust (Ns, Nr) 129 * in packets from/to userland. 130 */ 131 struct pipex_l2tp_session { 132 /* KEYS for session lookup (host byte order) */ 133 uint16_t tunnel_id; /* [I] our tunnel-id */ 134 uint16_t peer_tunnel_id; /* [I] peer's tunnel-id */ 135 136 uint32_t option_flags; /* [I] protocol options */ 137 138 int16_t ns_gap; /* [N] gap between userland and pipex */ 139 int16_t nr_gap; /* [N] gap between userland and pipex */ 140 uint16_t ul_ns_una; /* [N] unacked sequence number (userland) */ 141 142 uint16_t ns_nxt; /* [N] next sequence number to send */ 143 uint16_t ns_una; /* [N] unacked sequence number to send */ 144 145 uint16_t nr_nxt; /* [N] next sequence number to recv */ 146 uint16_t nr_acked; /* [N] acked sequence number to recv */ 147 uint32_t ipsecflowinfo; /* [N] IPsec SA flow id for NAT-T */ 148 }; 149 #endif /* PIPEX_L2TP */ 150 151 /* pppac ip-extension sessoin table */ 152 struct pipex_session { 153 struct radix_node ps4_rn[2]; 154 /* [N] tree glue, and other values */ 155 struct radix_node ps6_rn[2]; 156 /* [N] tree glue, and other values */ 157 LIST_ENTRY(pipex_session) session_list; /* [N] all session chain */ 158 LIST_ENTRY(pipex_session) state_list; /* [N] state list chain */ 159 LIST_ENTRY(pipex_session) id_chain; /* [N] id hash chain */ 160 LIST_ENTRY(pipex_session) peer_addr_chain; 161 /* [N] peer's address hash chain */ 162 uint16_t state; /* [N] pipex session state */ 163 #define PIPEX_STATE_INITIAL 0x0000 164 #define PIPEX_STATE_OPENED 0x0001 165 #define PIPEX_STATE_CLOSE_WAIT 0x0002 166 #define PIPEX_STATE_CLOSE_WAIT2 0x0003 167 #define PIPEX_STATE_CLOSED 0x0004 168 169 uint16_t ip_forward:1, /* [N] {en|dis}ableIP forwarding */ 170 ip6_forward:1, /* [I] {en|dis}able IPv6 forwarding */ 171 is_multicast:1, /* [I] virtual entry for multicast */ 172 is_pppx:1, /* [I] interface is point2point(pppx) */ 173 reserved:12; 174 uint16_t protocol; /* [I] tunnel protocol (PK) */ 175 uint16_t session_id; /* [I] session-id (PK) */ 176 uint16_t peer_session_id; /* [I] peer's session-id */ 177 uint16_t peer_mru; /* [I] peer's MRU */ 178 uint32_t timeout_sec; /* [I] idle timeout */ 179 int ppp_id; /* [I] PPP id */ 180 181 struct sockaddr_in ip_address; /* [I] remote address (AK) */ 182 struct sockaddr_in ip_netmask; /* [I] remote address mask (AK) */ 183 struct sockaddr_in6 ip6_address; /* [I] remote IPv6 address */ 184 int ip6_prefixlen; /* [I] remote IPv6 prefixlen */ 185 186 u_int ifindex; /* [N] interface index */ 187 void *ownersc; /* [I] owner context */ 188 189 uint32_t ppp_flags; /* [I] configure flags */ 190 #ifdef PIPEX_MPPE 191 int ccp_id; /* [N] CCP packet id */ 192 struct pipex_mppe 193 mppe_recv, /* MPPE context for incoming */ 194 mppe_send; /* MPPE context for outgoing */ 195 #endif /*PIPEXMPPE */ 196 struct pipex_statistics stat; /* [N] statistics */ 197 union { 198 #ifdef PIPEX_PPPOE 199 struct pipex_pppoe_session pppoe; /* context for PPPoE */ 200 #endif /* PIPEX_PPPOE */ 201 #ifdef PIPEX_PPTP 202 struct pipex_pptp_session pptp; /* context for PPTP */ 203 #endif /* PIPEX_PPTP */ 204 #ifdef PIPEX_L2TP 205 struct pipex_l2tp_session l2tp; 206 #endif 207 char _proto_unknown[0]; 208 } proto; 209 union { 210 struct sockaddr sa; 211 struct sockaddr_in sin4; 212 struct sockaddr_in6 sin6; 213 struct sockaddr_dl sdl; 214 } peer, local; /* [I] */ 215 }; 216 217 /* gre header */ 218 struct pipex_gre_header { 219 uint16_t flags; /* flags and version*/ 220 #define PIPEX_GRE_KFLAG 0x2000 /* keys present */ 221 #define PIPEX_GRE_SFLAG 0x1000 /* seq present */ 222 #define PIPEX_GRE_AFLAG 0x0080 /* ack present */ 223 #define PIPEX_GRE_VER 0x0001 /* gre version code */ 224 #define PIPEX_GRE_VERMASK 0x0007 /* gre version mask */ 225 #define PIPEX_GRE_UNUSEDFLAGS 0xcf78 /* unused at pptp. set 0 in rfc2637 */ 226 227 uint16_t type; 228 #define PIPEX_GRE_PROTO_PPP 0x880b /* gre/ppp */ 229 230 uint16_t len; /* length not include gre header */ 231 uint16_t call_id; /* call_id */ 232 } __packed; 233 234 /* pppoe header */ 235 struct pipex_pppoe_header { 236 uint8_t vertype; /* version and type */ 237 #define PIPEX_PPPOE_VERTYPE 0x11 /* version and type code */ 238 239 uint8_t code; /* code */ 240 #define PIPEX_PPPOE_CODE_SESSION 0x00 /* code session */ 241 242 uint16_t session_id; /* session id */ 243 uint16_t length; /* length */ 244 } __packed; 245 246 /* l2tp header */ 247 struct pipex_l2tp_header { 248 uint16_t flagsver; 249 #define PIPEX_L2TP_FLAG_MASK 0xfff0 250 #define PIPEX_L2TP_FLAG_TYPE 0x8000 251 #define PIPEX_L2TP_FLAG_LENGTH 0x4000 252 #define PIPEX_L2TP_FLAG_SEQUENCE 0x0800 253 #define PIPEX_L2TP_FLAG_OFFSET 0x0200 254 #define PIPEX_L2TP_FLAG_PRIORITY 0x0100 255 #define PIPEX_L2TP_VER_MASK 0x000f 256 #define PIPEX_L2TP_VER 2 257 uint16_t length; /* optional */ 258 uint16_t tunnel_id; 259 uint16_t session_id; 260 /* can be followed by option header */ 261 } __packed; 262 263 /* l2tp option header */ 264 struct pipex_l2tp_seq_header { 265 uint16_t ns; 266 uint16_t nr; 267 } __packed; 268 269 struct pipex_l2tp_offset_header { 270 uint16_t offset_size; 271 /* uint8_t offset_pad[] */ 272 } __packed; 273 274 #ifdef PIPEX_DEBUG 275 #define PIPEX_DBG(a) if (pipex_debug & 1) pipex_session_log a 276 /* #define PIPEX_MPPE_DBG(a) if (pipex_debug & 1) pipex_session_log a */ 277 #define PIPEX_MPPE_DBG(a) 278 #else 279 #define PIPEX_DBG(a) 280 #define PIPEX_MPPE_DBG(a) 281 #endif /* PIPEX_DEBUG */ 282 283 LIST_HEAD(pipex_hash_head, pipex_session); 284 285 extern struct pipex_hash_head pipex_session_list; 286 extern struct pipex_hash_head pipex_close_wait_list; 287 extern struct pipex_hash_head pipex_peer_addr_hashtable[]; 288 extern struct pipex_hash_head pipex_id_hashtable[]; 289 extern struct pool pipex_session_pool; 290 291 292 #define PIPEX_ID_HASHTABLE(key) \ 293 (&pipex_id_hashtable[(key) & PIPEX_HASH_MASK]) 294 #define PIPEX_PEER_ADDR_HASHTABLE(key) \ 295 (&pipex_peer_addr_hashtable[(key) & PIPEX_HASH_MASK]) 296 297 #define GETCHAR(c, cp) do { \ 298 (c) = *(cp)++; \ 299 } while (0) 300 301 #define PUTCHAR(s, cp) do { \ 302 *(cp)++ = (u_char)(s); \ 303 } while (0) 304 305 #define GETSHORT(s, cp) do { \ 306 (s) = *(cp)++ << 8; \ 307 (s) |= *(cp)++; \ 308 } while (0) 309 310 #define PUTSHORT(s, cp) do { \ 311 *(cp)++ = (u_char) ((s) >> 8); \ 312 *(cp)++ = (u_char) (s); \ 313 } while (0) 314 315 #define GETLONG(l, cp) do { \ 316 (l) = *(cp)++ << 8; \ 317 (l) |= *(cp)++; (l) <<= 8; \ 318 (l) |= *(cp)++; (l) <<= 8; \ 319 (l) |= *(cp)++; \ 320 } while (0) 321 322 #define PUTLONG(l, cp) do { \ 323 *(cp)++ = (u_char) ((l) >> 24); \ 324 *(cp)++ = (u_char) ((l) >> 16); \ 325 *(cp)++ = (u_char) ((l) >> 8); \ 326 *(cp)++ = (u_char) (l); \ 327 } while (0) 328 329 #define PIPEX_PULLUP(m0, l) \ 330 if ((m0)->m_len < (l)) { \ 331 if ((m0)->m_pkthdr.len < (l)) { \ 332 PIPEX_DBG((NULL, LOG_DEBUG, \ 333 "<%s> received packet is too short.", \ 334 __func__)); \ 335 m_freem(m0); \ 336 (m0) = NULL; \ 337 } else { \ 338 (m0) = m_pullup((m0), (l)); \ 339 KASSERT((m0) != NULL); \ 340 } \ 341 } 342 #define PIPEX_SEEK_NEXTHDR(ptr, len, t) \ 343 ((t) (((char *)ptr) + len)) 344 #define SEQ32_LT(a,b) ((int32_t)((a) - (b)) < 0) 345 #define SEQ32_LE(a,b) ((int32_t)((a) - (b)) <= 0) 346 #define SEQ32_GT(a,b) ((int32_t)((a) - (b)) > 0) 347 #define SEQ32_GE(a,b) ((int32_t)((a) - (b)) >= 0) 348 #define SEQ32_SUB(a,b) ((int32_t)((a) - (b))) 349 350 #define SEQ16_LT(a,b) ((int16_t)((a) - (b)) < 0) 351 #define SEQ16_LE(a,b) ((int16_t)((a) - (b)) <= 0) 352 #define SEQ16_GT(a,b) ((int16_t)((a) - (b)) > 0) 353 #define SEQ16_GE(a,b) ((int16_t)((a) - (b)) >= 0) 354 #define SEQ16_SUB(a,b) ((int16_t)((a) - (b))) 355 356 #define pipex_session_is_acfc_accepted(s) \ 357 (((s)->ppp_flags & PIPEX_PPP_ACFC_ACCEPTED)? 1 : 0) 358 #define pipex_session_is_pfc_accepted(s) \ 359 (((s)->ppp_flags & PIPEX_PPP_PFC_ACCEPTED)? 1 : 0) 360 #define pipex_session_is_acfc_enabled(s) \ 361 (((s)->ppp_flags & PIPEX_PPP_ACFC_ENABLED)? 1 : 0) 362 #define pipex_session_is_pfc_enabled(s) \ 363 (((s)->ppp_flags & PIPEX_PPP_PFC_ENABLED)? 1 : 0) 364 #define pipex_session_has_acf(s) \ 365 (((s)->ppp_flags & PIPEX_PPP_HAS_ACF)? 1 : 0) 366 #define pipex_session_is_mppe_accepted(s) \ 367 (((s)->ppp_flags & PIPEX_PPP_MPPE_ACCEPTED)? 1 : 0) 368 #define pipex_session_is_mppe_enabled(s) \ 369 (((s)->ppp_flags & PIPEX_PPP_MPPE_ENABLED)? 1 : 0) 370 #define pipex_session_is_mppe_required(s) \ 371 (((s)->ppp_flags & PIPEX_PPP_MPPE_REQUIRED)? 1 : 0) 372 #define pipex_mppe_rc4_keybits(r) ((r)->keylen << 3) 373 #define pipex_session_is_l2tp_data_sequencing_on(s) \ 374 (((s)->proto.l2tp.option_flags & PIPEX_L2TP_USE_SEQUENCING) ? 1 : 0) 375 376 #define PIPEX_IPGRE_HDRLEN (sizeof(struct ip) + sizeof(struct pipex_gre_header)) 377 #define PIPEX_TCP_OPTLEN 40 378 #define PIPEX_L2TP_MINLEN 8 379 380 void pipex_destroy_all_sessions (void *); 381 int pipex_init_session(struct pipex_session **, 382 struct pipex_session_req *); 383 void pipex_rele_session(struct pipex_session *); 384 int pipex_link_session(struct pipex_session *, 385 struct ifnet *, void *); 386 void pipex_unlink_session(struct pipex_session *); 387 int pipex_config_session (struct pipex_session_config_req *, 388 void *); 389 int pipex_get_stat (struct pipex_session_stat_req *, 390 void *); 391 int pipex_get_closed (struct pipex_session_list_req *, 392 void *); 393 struct pipex_session *pipex_lookup_by_ip_address (struct in_addr); 394 struct pipex_session *pipex_lookup_by_session_id (int, int); 395 void pipex_ip_output (struct mbuf *, struct pipex_session *); 396 void pipex_ppp_output (struct mbuf *, struct pipex_session *, int); 397 int pipex_ppp_proto (struct mbuf *, struct pipex_session *, int, int *); 398 void pipex_ppp_input (struct mbuf *, struct pipex_session *, int); 399 void pipex_ip_input (struct mbuf *, struct pipex_session *); 400 #ifdef INET6 401 void pipex_ip6_input (struct mbuf *, struct pipex_session *); 402 #endif 403 struct mbuf *pipex_common_input(struct pipex_session *, 404 struct mbuf *, int, int); 405 406 #ifdef PIPEX_PPPOE 407 void pipex_pppoe_output (struct mbuf *, struct pipex_session *); 408 #endif 409 410 #ifdef PIPEX_PPTP 411 void pipex_pptp_output (struct mbuf *, struct pipex_session *, int, int); 412 struct pipex_session *pipex_pptp_userland_lookup_session(struct mbuf *, struct sockaddr *); 413 #endif 414 415 #ifdef PIPEX_L2TP 416 void pipex_l2tp_output (struct mbuf *, struct pipex_session *); 417 #endif 418 419 #ifdef PIPEX_MPPE 420 void pipex_mppe_init (struct pipex_mppe *, int, int, u_char *, int); 421 void GetNewKeyFromSHA (u_char *, u_char *, int, u_char *); 422 void pipex_mppe_reduce_key (struct pipex_mppe *); 423 void mppe_key_change (struct pipex_mppe *); 424 void pipex_mppe_input (struct mbuf *, struct pipex_session *); 425 void pipex_mppe_output (struct mbuf *, struct pipex_session *, uint16_t); 426 void pipex_ccp_input (struct mbuf *, struct pipex_session *); 427 int pipex_ccp_output (struct pipex_session *, int, int); 428 #endif 429 430 struct mbuf *adjust_tcp_mss (struct mbuf *, int); 431 struct mbuf *ip_is_idle_packet (struct mbuf *, int *); 432 void pipex_session_log (struct pipex_session *, int, const char *, ...) __attribute__((__format__(__printf__,3,4))); 433 uint32_t pipex_sockaddr_hash_key(struct sockaddr *); 434 int pipex_sockaddr_compar_addr(struct sockaddr *, struct sockaddr *); 435 int pipex_ppp_enqueue (struct mbuf *, struct pipex_session *, struct mbuf_queue *); 436 void pipex_timer_start (void); 437 void pipex_timer_stop (void); 438 void pipex_timer (void *); 439