1 /* $OpenBSD: l2tp.h,v 1.14 2021/03/29 03:54:39 yasuoka 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 #ifndef L2TP_H 29 #define L2TP_H 1 30 /*@file 31 * header file for the L2TP module 32 */ 33 /* $Id: l2tp.h,v 1.14 2021/03/29 03:54:39 yasuoka Exp $ */ 34 35 /************************************************************************ 36 * Protocol Constants 37 ************************************************************************/ 38 39 #define L2TP_RFC2661_VERSION 1 40 #define L2TP_RFC2661_REVISION 0 41 #define L2TP_AVP_MAXSIZ 1024 42 43 /* Header */ 44 45 #define L2TP_HEADER_FLAG_TOM 0x8000 46 #define L2TP_HEADER_FLAG_LENGTH 0x4000 47 #define L2TP_HEADER_FLAG_SEQUENCE 0x0800 48 #define L2TP_HEADER_FLAG_OFFSET 0x0200 49 #define L2TP_HEADER_FLAG_PRIORITY 0x0100 50 #define L2TP_HEADER_FLAG_VERSION_MASK 0x000f 51 #define L2TP_HEADER_VERSION_RFC2661 0x02 52 53 /* AVP Attribute Types */ 54 55 /* RFC 2661 */ 56 #define L2TP_AVP_TYPE_MESSAGE_TYPE 0 57 #define L2TP_AVP_TYPE_RESULT_CODE 1 58 #define L2TP_AVP_TYPE_PROTOCOL_VERSION 2 59 #define L2TP_AVP_TYPE_FRAMING_CAPABILITIES 3 60 #define L2TP_AVP_TYPE_BEARER_CAPABILITIES 4 61 #define L2TP_AVP_TYPE_TIE_BREAKER 5 62 #define L2TP_AVP_TYPE_FIRMWARE_REVISION 6 63 #define L2TP_AVP_TYPE_HOST_NAME 7 64 #define L2TP_AVP_TYPE_VENDOR_NAME 8 65 #define L2TP_AVP_TYPE_ASSINGED_TUNNEL_ID 9 66 #define L2TP_AVP_TYPE_RECV_WINDOW_SIZE 10 67 #define L2TP_AVP_TYPE_CHALLENGE 11 68 #define L2TP_AVP_TYPE_CAUSE_CODE 12 69 #define L2TP_AVP_TYPE_CHALLENGE_RESPONSE 13 70 #define L2TP_AVP_TYPE_ASSIGNED_SESSION_ID 14 71 #define L2TP_AVP_TYPE_CALL_SERIAL_NUMBER 15 72 #define L2TP_AVP_TYPE_MINIMUM_BPS 16 73 #define L2TP_AVP_TYPE_MAXIMUM_BPS 17 74 #define L2TP_AVP_TYPE_BEARER_TYPE 18 75 #define L2TP_AVP_TYPE_FRAMING_TYPE 19 76 #define L2TP_AVP_TYPE_CALLED_NUMBER 21 77 #define L2TP_AVP_TYPE_CALLING_NUMBER 22 78 #define L2TP_AVP_TYPE_SUB_ADDRESS 23 79 #define L2TP_AVP_TYPE_TX_CONNECT_SPEED 24 80 81 #define L2TP_AVP_TYPE_PHYSICAL_CHANNEL_ID 25 82 #define L2TP_AVP_TYPE_INITIAL_RECV_LCP_CONFREQ 26 83 #define L2TP_AVP_TYPE_LAST_SENT_LCP_CONFREQ 27 84 #define L2TP_AVP_TYPE_LAST_RECV_LCP_CONFREQ 28 85 #define L2TP_AVP_TYPE_PROXY_AUTHEN_TYPE 29 86 #define L2TP_AVP_TYPE_PROXY_AUTHEN_NAME 30 87 #define L2TP_AVP_TYPE_PROXY_AUTHEN_CHALLENGE 31 88 #define L2TP_AVP_TYPE_PROXY_AUTHEN_ID 32 89 #define L2TP_AVP_TYPE_PROXY_AUTHEN_RESPONSE 33 90 #define L2TP_AVP_TYPE_CALL_ERRORS 34 91 #define L2TP_AVP_TYPE_ACCM 35 92 #define L2TP_AVP_TYPE_RANDOM_VECTOR 36 93 #define L2TP_AVP_TYPE_PRIVATE_GROUP_ID 37 94 #define L2TP_AVP_TYPE_RX_CONNECT_SPEED 38 95 #define L2TP_AVP_TYPE_SEQUENCING_REQUIRED 39 96 97 98 /* RFC 3301 */ 99 #define L2TP_AVP_TYPE_TX_MINIMUM 40 100 #define L2TP_AVP_TYPE_CALLING_SUB_ADDRESS 44 101 102 /* RFC 3145 */ 103 #define L2TP_AVP_TYPE_PPP_DISCONNECT_CAUSE_CODE 46 104 105 /* RFC 3308 */ 106 #define L2TP_AVP_TYPE_CCDS 47 107 #define L2TP_AVP_TYPE_SDS 48 108 109 /* RFC 3437 */ 110 #define L2TP_AVP_TYPE_LCP_WANT_OPTIONS 49 111 #define L2TP_AVP_TYPE_LCP_ALLOW_OPTIONS 50 112 #define L2TP_AVP_TYPE_LNS_LAST_SENT_LCP_CONFREQ 51 113 #define L2TP_AVP_TYPE_LNS_LAST_RECV_LCP_CONFREQ 52 114 115 /* RFC 3573 */ 116 #define L2TP_AVP_TYPE_MODEM_ON_HOLD_CAPABLE 53 117 #define L2TP_AVP_TYPE_MODEM_ON_HOLD_STATUS 54 118 119 /* RFC 3817 */ 120 #define L2TP_AVP_TYPE_PPPOE_RELAY 55 121 #define L2TP_AVP_TYPE_PPPOE_RELAY_RESP_CAP 56 122 #define L2TP_AVP_TYPE_PPPOE_RELAY_FORW_CAP 57 123 124 /* No RFC yet */ 125 #define L2TP_AVP_TYPE_EXTENDED_VENDOR_ID 58 126 #define L2TP_AVP_TYPE_PSEUDOWIRE_CAP_LIST 62 127 #define L2TP_AVP_TYPE_LOCAL_SESSION_ID 63 128 #define L2TP_AVP_TYPE_REMOTE_SESSION_ID 64 129 #define L2TP_AVP_TYPE_ASSIGNED_COOKIE 65 130 #define L2TP_AVP_TYPE_REMOTE_END_ID 66 131 #define L2TP_AVP_TYPE_APPLICATION_CODE 67 132 #define L2TP_AVP_TYPE_PSEUDOWIRE_TYPE 68 133 #define L2TP_AVP_TYPE_L2_SPECIFIC_SUBLAYER 69 134 #define L2TP_AVP_TYPE_DATA_SEQUENCING 70 135 #define L2TP_AVP_TYPE_CIRCUIT_STATUS 71 136 #define L2TP_AVP_TYPE_PREFERRED_LANGUAGE 72 137 #define L2TP_AVP_TYPE_CTRL_MSG_AUTH_NONCE 73 138 /* #define L2TP_AVP_TYPE_TX_CONNECT_SPEED 74 */ 139 /* #define L2TP_AVP_TYPE_RX_CONNECT_SPEED 75 */ 140 #define L2TP_AVP_TYPE_FAILOVER_CAPABILITY 76 141 #define L2TP_AVP_TYPE_TUNNEL_RECOVERY 77 142 #define L2TP_AVP_TYPE_SUGGESTED_CTRL_SEQUENCE 78 143 #define L2TP_AVP_TYPE_FAILOVER_SESSION_STATE 79 144 145 /* RFC 4045 */ 146 #define L2TP_AVP_TYPE_MULTICAST_CAPABILITY 80 147 #define L2TP_AVP_TYPE_NEW_OUTGOING_SESSIONS 81 148 #define L2TP_AVP_TYPE_NEW_OUTGOING_SESSIONS_ACK 82 149 #define L2TP_AVP_TYPE_WITHDRAW_OUTGOING_SESSIONS 83 150 #define L2TP_AVP_TYPE_MULTICAST_PACKETS_PRIORITY 84 151 152 /* Control Message Type */ 153 154 #define L2TP_AVP_MESSAGE_TYPE_SCCRQ 1 155 #define L2TP_AVP_MESSAGE_TYPE_SCCRP 2 156 #define L2TP_AVP_MESSAGE_TYPE_SCCCN 3 157 #define L2TP_AVP_MESSAGE_TYPE_StopCCN 4 158 #define L2TP_AVP_MESSAGE_TYPE_HELLO 6 159 #define L2TP_AVP_MESSAGE_TYPE_OCRQ 7 160 #define L2TP_AVP_MESSAGE_TYPE_OCRP 8 161 #define L2TP_AVP_MESSAGE_TYPE_OCCN 9 162 #define L2TP_AVP_MESSAGE_TYPE_ICRQ 10 163 #define L2TP_AVP_MESSAGE_TYPE_ICRP 11 164 #define L2TP_AVP_MESSAGE_TYPE_ICCN 12 165 #define L2TP_AVP_MESSAGE_TYPE_CDN 14 166 167 #define L2TP_FRAMING_CAP_FLAGS_SYNC 0x00000001 168 #define L2TP_FRAMING_CAP_FLAGS_ASYNC 0x00000002 169 #define L2TP_BEARER_CAP_FLAGS_DIGITAL 0x00000001 170 #define L2TP_BEARER_CAP_FLAGS_ANALOG 0x00000002 171 172 /* 173 * Constants on pp.19-22 of RFC2661 174 * macro names may be inappropriate. 175 */ 176 #define L2TP_STOP_CCN_RCODE_GENERAL 1 177 #define L2TP_STOP_CCN_RCODE_GENERAL_ERROR 2 178 #define L2TP_STOP_CCN_RCODE_ALREADY_EXISTS 3 179 #define L2TP_STOP_CCN_RCODE_UNAUTHORIZED 4 180 #define L2TP_STOP_CCN_RCODE_BAD_PROTOCOL_VERSION 5 181 #define L2TP_STOP_CCN_RCODE_SHUTTING_DOWN 6 182 #define L2TP_STOP_CCN_RCODE_FSM_ERROR 7 183 184 #define L2TP_CDN_RCODE_LOST_CARRIER 1 185 #define L2TP_CDN_RCODE_ERROR_CODE 2 186 #define L2TP_CDN_RCODE_ADMINISTRATIVE_REASON 3 187 #define L2TP_CDN_RCODE_TEMP_NOT_AVALIABLE 4 188 #define L2TP_CDN_RCODE_PERM_NOT_AVALIABLE 5 189 #define L2TP_CDN_RCODE_INVALID_DESTINATION 6 190 #define L2TP_CDN_RCODE_NO_CARRIER 7 191 #define L2TP_CDN_RCODE_BUSY 8 192 #define L2TP_CDN_RCODE_NO_DIALTONE 9 193 #define L2TP_CDN_RCODE_CALL_TIMEOUT_BY_LAC 10 194 #define L2TP_CDN_RCODE_NO_FRAMING_DETECTED 11 195 196 #define L2TP_ECODE_NO_CONTROL_CONNECTION 1 197 #define L2TP_ECODE_WRONG_LENGTH 2 198 #define L2TP_ECODE_INVALID_MESSAGE 3 199 #define L2TP_ECODE_NO_RESOURCE 4 200 #define L2TP_ECODE_INVALID_SESSION_ID 5 201 #define L2TP_ECODE_GENERIC_ERROR 6 202 #define L2TP_ECODE_TRY_ANOTHER 7 203 #define L2TP_ECODE_UNKNOWN_MANDATORY_AVP 8 204 205 /* Proxy Authen Type */ 206 #define L2TP_AUTH_TYPE_RESERVED 0 207 #define L2TP_AUTH_TYPE_TEXUAL 1 208 #define L2TP_AUTH_TYPE_PPP_CHAP 2 209 #define L2TP_AUTH_TYPE_PPP_PAP 3 210 #define L2TP_AUTH_TYPE_NO_AUTH 4 211 #define L2TP_AUTH_TYPE_MS_CHAP_V1 5 212 213 /************************************************************************ 214 * Implementation Specific Constants 215 ************************************************************************/ 216 #include "l2tp_conf.h" 217 218 #define L2TPD_TUNNEL_HASH_SIZ 127 219 #define L2TPD_SND_BUFSIZ 2048 220 #define L2TPD_DEFAULT_SEND_WINSZ 4 221 #define L2TPD_DEFAULT_LAYER2_LABEL "L2TP" 222 #define L2TPD_DIALIN_LAYER2_LABEL "DialIn" 223 #define L2TPD_CONFIG_BUFSIZ 65535 224 #define L2TP_CTRL_WINDOW_SIZE 8 225 #ifndef L2TPD_VENDOR_NAME 226 #define L2TPD_VENDOR_NAME "" 227 #endif 228 #define L2TPD_DEFAULT_UDP_PORT 1701 229 230 /** maximum number of addresses we will listen on */ 231 #ifndef L2TP_NLISTENER 232 #define L2TP_NLISTENER 6 233 #endif 234 235 /* 236 * state of daemon 237 */ 238 #define L2TPD_STATE_INIT 0 239 #define L2TPD_STATE_RUNNING 1 240 #define L2TPD_STATE_SHUTTING_DOWN 2 241 #define L2TPD_STATE_STOPPED 3 242 243 /* 244 * state of a control connection 245 */ 246 #define L2TP_CTRL_STATE_IDLE 0 247 #define L2TP_CTRL_STATE_WAIT_CTL_CONN 1 248 #define L2TP_CTRL_STATE_WAIT_CTL_REPLY 2 249 #define L2TP_CTRL_STATE_ESTABLISHED 3 250 #define L2TP_CTRL_STATE_CLEANUP_WAIT 4 251 252 /* 253 * state of a call 254 */ 255 #define L2TP_CALL_STATE_IDLE 0 256 #define L2TP_CALL_STATE_WAIT_CONN 1 257 #define L2TP_CALL_STATE_ESTABLISHED 2 258 #define L2TP_CALL_STATE_CLEANUP_WAIT 3 259 260 /* 261 * timeout 262 */ 263 #define L2TP_CTRL_CTRL_PKT_TIMEOUT 12 264 /** wait time for the first call */ 265 #define L2TP_CTRL_WAIT_CALL_TIMEOUT 16 266 #define L2TP_CTRL_CLEANUP_WAIT_TIME 3 267 #define L2TP_CTRL_DEFAULT_HELLO_INTERVAL 60 268 #define L2TP_CTRL_DEFAULT_HELLO_TIMEOUT 30 269 270 #define L2TPD_SHUTDOWN_TIMEOUT 5 271 272 /** returns whether an L2TP daemon is stopped */ 273 #define l2tpd_is_stopped(l2tpd) \ 274 (((l2tpd)->state != L2TPD_STATE_SHUTTING_DOWN && \ 275 (l2tpd)->state != L2TPD_STATE_RUNNING)? 1 : 0) 276 277 /** returns whether an L2TP daemon is going to shutdown */ 278 #define l2tpd_is_shutting_down(l2tpd) \ 279 (((l2tpd)->state == L2TPD_STATE_SHUTTING_DOWN)? 1 : 0) 280 281 /** macro to retrieve a physical layer label from l2tp_ctrl */ 282 #define L2TP_CTRL_LISTENER_TUN_NAME(ctrl) \ 283 ((l2tpd_listener *)slist_get(&(ctrl)->l2tpd->listener, \ 284 (ctrl)->listener_index))->tun_name 285 286 #define L2TP_CTRL_CONF(ctrl) \ 287 ((l2tpd_listener *)slist_get(&(ctrl)->l2tpd->listener, \ 288 (ctrl)->listener_index))->conf 289 290 #define L2TP_CALL_DELAY_LIMIT 64 291 292 /** datatype represents L2TP daemon */ 293 struct _l2tpd; 294 295 typedef struct _l2tpd_listener { 296 /* configuration */ 297 struct l2tp_conf *conf; 298 /** event context */ 299 struct event ev_sock; 300 /** L2TPD itself */ 301 struct _l2tpd *self; 302 /** index number */ 303 uint16_t index; 304 /** enable/disable */ 305 uint16_t enabled; 306 /** listening socket */ 307 int sock; 308 /** listening socket address for UDP packets */ 309 union { 310 struct sockaddr_in sin4; 311 struct sockaddr_in6 sin6; 312 } bind; 313 /** tunnel name */ 314 char tun_name[L2TP_NAME_LEN]; 315 } l2tpd_listener; 316 317 /** datatype represents L2TP daemon */ 318 typedef struct _l2tpd { 319 /** timeout event context */ 320 struct event ev_timeout; 321 /** instance ID */ 322 u_int id; 323 /** listener list */ 324 slist listener; 325 /** state */ 326 int state; 327 /** mappings from tunnel ID to {@link ::_l2tp_ctrl L2TP control} */ 328 hash_table *ctrl_map; 329 /** unique and free Session-ID list */ 330 slist free_session_id_list; 331 332 /** flags */ 333 uint32_t 334 purge_ipsec_sa:1; 335 } l2tpd; 336 337 /** datatype represents L2TP control connection */ 338 typedef struct _l2tp_ctrl { 339 struct event ev_timeout; 340 /** ID */ 341 u_int id; 342 /** parent L2TPD */ 343 l2tpd *l2tpd; 344 /** listener index number */ 345 uint16_t listener_index; 346 /** state */ 347 int state; 348 /** tunnel Id */ 349 int tunnel_id; 350 /** window size */ 351 int winsz; 352 /** peer's tunnel Id */ 353 int peer_tunnel_id; 354 /** peer's window size */ 355 int peer_winsz; 356 /** next acknowledgement number */ 357 uint16_t snd_una; 358 /** next send sequence number */ 359 uint16_t snd_nxt; 360 /** last send sequence number */ 361 uint16_t snd_last; 362 /** last send ack number */ 363 uint16_t snd_lastnr; 364 /** receive sequence number */ 365 uint16_t rcv_nxt; 366 /** peer's IP address */ 367 struct sockaddr_storage peer; 368 /** my IP address */ 369 struct sockaddr_storage sock; 370 /** IPSEC NAT-T SA cookie */ 371 void *sa_cookie; 372 373 /** list of L2TP calls */ 374 slist call_list; 375 /* 376 * Note about send window: 377 * pos == lim when buffer is full. 378 * pos == -1, lim == 0 when buffer is empty. 379 */ 380 /** bytes available in send buffer. it is a list of length #winsz */ 381 bytebuffer **snd_buffers; 382 int snd_buffercnt; 383 /** sending buffer for ZLB */ 384 bytebuffer *zlb_buffer; 385 386 /** the time when last control message sent */ 387 time_t last_snd_ctrl; 388 /** the time when last packet received */ 389 time_t last_rcv; 390 391 /** 392 * If we are on active close and have not sent a StopCCN message yet, 393 * active_closing has the result code to be in the StopCCN message. 394 */ 395 int active_closing; 396 397 /** 398 * delay between transition to idle state and sending HELLO in seconds. 399 */ 400 int hello_interval; 401 /** HELLO timeout */ 402 int hello_timeout; 403 /** time when the last HELLO packet was sent */ 404 time_t hello_io_time; 405 /** number of calls established */ 406 int ncalls; 407 408 int /** use sequence number in L2TP Data Message? */ 409 data_use_seq:1, 410 /** waiting to acknowledge HELLO? */ 411 hello_wait_ack:1; 412 413 } l2tp_ctrl; 414 415 /** 416 * datatype represents a L2TP call 417 */ 418 typedef struct _l2tp_call { 419 /** ID */ 420 u_int id; 421 /** state */ 422 int state; 423 /** parent control connection */ 424 l2tp_ctrl *ctrl; 425 /** bound {@link ::_npppd_ppp ppp} */ 426 void *ppp; 427 /** session ID */ 428 uint16_t session_id; 429 /** peer's session ID */ 430 uint16_t peer_session_id; 431 /** next sequence number */ 432 uint16_t snd_nxt; 433 /** receiving sequence number */ 434 uint16_t rcv_nxt; 435 /** calling number */ 436 char calling_number[32]; 437 438 uint32_t /** Sequencing required */ 439 seq_required:1, 440 /** Use sequencing in the data connection */ 441 use_seq:1; 442 } l2tp_call; 443 444 #ifdef __cplusplus 445 extern "C" { 446 #endif 447 448 l2tp_call *l2tp_call_create (void); 449 int l2tp_call_init (l2tp_call *, l2tp_ctrl *); 450 void l2tp_call_destroy (l2tp_call *, int); 451 void l2tp_call_admin_disconnect(l2tp_call *); 452 void l2tp_call_drop (l2tp_call *); 453 int l2tp_call_recv_packet (l2tp_ctrl *, l2tp_call *, int, u_char *, int); 454 void l2tp_call_ppp_input (l2tp_call *, u_char *, int, int); 455 456 void l2tp_ctrl_destroy (l2tp_ctrl *); 457 l2tp_ctrl *l2tp_ctrl_create (void); 458 void l2tp_ctrl_input (l2tpd *, int, struct sockaddr *, struct sockaddr *, void *, u_char *, int); 459 int l2tp_ctrl_send(l2tp_ctrl *, const void *, int); 460 int l2tp_ctrl_send_packet(l2tp_ctrl *, int, bytebuffer *); 461 int l2tp_ctrl_stop (l2tp_ctrl *, int); 462 bytebuffer *l2tp_ctrl_prepare_snd_buffer (l2tp_ctrl *, int); 463 void l2tp_ctrl_log (l2tp_ctrl *, int, const char *, ...) __attribute__((__format__ (__printf__, 3, 4))); 464 int l2tpd_init (l2tpd *); 465 void l2tpd_uninit (l2tpd *); 466 int l2tpd_assign_call (l2tpd *, l2tp_call *); 467 void l2tpd_release_call (l2tpd *, l2tp_call *); 468 int l2tpd_start (l2tpd *); 469 void l2tpd_stop (l2tpd *); 470 void l2tpd_stop_immediatly (l2tpd *); 471 l2tp_ctrl *l2tpd_get_ctrl (l2tpd *, u_int); 472 void l2tpd_add_ctrl (l2tpd *, l2tp_ctrl *); 473 void l2tpd_ctrl_finished_notify(l2tpd *); 474 void l2tpd_remove_ctrl (l2tpd *, u_int); 475 int l2tpd_add_listener (l2tpd *, int, struct l2tp_conf *, struct sockaddr *); 476 void l2tpd_log (l2tpd *, int, const char *, ...) __attribute__((__format__ (__printf__, 3, 4))); 477 int l2tpd_reload(l2tpd *, struct l2tp_confs *); 478 void l2tpd_log_access_deny(l2tpd *, const char *, struct sockaddr *); 479 #ifdef __cplusplus 480 } 481 #endif 482 #endif 483