xref: /openbsd/usr.sbin/npppd/pptp/pptp.h (revision 963e0d12)
1 /*	$OpenBSD: pptp.h,v 1.12 2024/02/26 08:29:37 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 PPTP_H
29 #define PPTP_H	1
30 
31 /*
32  * PPTP protocol constants
33  */
34 #define	PPTP_MES_TYPE_CTRL			1
35 #define	PPTP_MAGIC_COOKIE			0x1a2b3c4d
36 #define	PPTP_RFC_2637_VERSION			0x0100
37 
38 #ifndef	PPTP_MAX_CALL
39 #define	PPTP_MAX_CALL				8192
40 #endif
41 
42 /* Start-Control-Connection-Request */
43 #define	PPTP_CTRL_MES_CODE_SCCRQ	1
44 
45 /* Start-Control-Connection-Reply */
46 #define	PPTP_CTRL_MES_CODE_SCCRP	2
47 
48 /* Stop-Control-Connection-Request */
49 #define	PPTP_CTRL_MES_CODE_StopCCRQ	3
50 
51 /* Stop-Control-Connection-Reply */
52 #define	PPTP_CTRL_MES_CODE_StopCCRP	4
53 
54 /* Echo-Request */
55 #define	PPTP_CTRL_MES_CODE_ECHO_RQ	5
56 
57 /* Echo-Reply */
58 #define	PPTP_CTRL_MES_CODE_ECHO_RP	6
59 
60 /* Outgoing-Call-Request */
61 #define	PPTP_CTRL_MES_CODE_OCRQ		7
62 
63 /* Outgoing-Call-Reply */
64 #define	PPTP_CTRL_MES_CODE_OCRP		8
65 
66 /* Incoming-Call-Request */
67 #define	PPTP_CTRL_MES_CODE_ICRQ		9
68 
69 /* Incoming-Call-Reply */
70 #define	PPTP_CTRL_MES_CODE_ICRP		10
71 
72 /* Incoming-Call-Connected */
73 #define	PPTP_CTRL_MES_CODE_ICCN		11
74 
75 /* Call-Clear-Request */
76 #define	PPTP_CTRL_MES_CODE_CCR		12
77 
78 /* Call-Disconnect-Notify */
79 #define	PPTP_CTRL_MES_CODE_CDN		13
80 
81 /* Set-Link-Info */
82 #define	PPTP_CTRL_MES_CODE_SLI		15
83 
84 
85 #define	PPTP_CTRL_FRAMING_ASYNC		1
86 #define	PPTP_CTRL_FRAMING_SYNC		2
87 
88 #define	PPTP_CTRL_BEARER_ANALOG		1
89 #define	PPTP_CTRL_BEARER_DIGITAL	2
90 
91 
92 /* Result Code: Start-Control-Connection-Reply */
93 #define PPTP_SCCRP_RESULT_SUCCESS		1
94 #define PPTP_SCCRP_RESULT_GENERIC_ERROR		2
95 #define PPTP_SCCRP_RESULT_CHANNEL_EXISTS	3
96 #define PPTP_SCCRP_RESULT_NOT_AUTHORIZIZED	4
97 #define PPTP_SCCRP_RESULT_BAD_PROTOCOL_VERSION	5
98 
99 /* General Error Code (RFC 2637 2.16 pp.36) */
100 #define PPTP_ERROR_NONE				0
101 #define PPTP_ERROR_NOT_CONNECTED		1
102 #define PPTP_ERROR_BAD_FORMAT			2
103 #define PPTP_ERROR_NO_RESOURCE			3
104 #define PPTP_ERROR_BAD_CALL			4
105 #define PPTP_ERROR_PAC_ERROR			5
106 
107 /* Result Code: Outgoing-Call-Reply */
108 #define PPTP_OCRP_RESULT_CONNECTED		1
109 #define PPTP_OCRP_RESULT_GENERIC_ERROR		2
110 #define PPTP_OCRP_RESULT_NO_CARRIER		3
111 #define PPTP_OCRP_RESULT_BUSY			4
112 #define PPTP_OCRP_RESULT_NO_DIALTONE		5
113 #define PPTP_OCRP_RESULT_TIMEOUT		6
114 #define PPTP_OCRP_RESULT_DO_NOT_ACCEPT		7
115 
116 /* Result Code: Echo-Reply */
117 #define PPTP_ECHO_RP_RESULT_OK			1
118 #define PPTP_ECHO_RP_RESULT_GENERIC_ERROR	2
119 
120 /* Reason code of the Stop-Control-Connection-Request */
121 #define	PPTP_StopCCRQ_REASON_NONE			1
122 #define	PPTP_StopCCRQ_REASON_STOP_PROTOCOL		2
123 #define	PPTP_StopCCRQ_REASON_STOP_LOCAL_SHUTDOWN	3
124 
125 /* Result code of the Stop-Control-Connection-Response */
126 #define	PPTP_StopCCRP_RESULT_OK			1
127 #define	PPTP_StopCCRP_RESULT_GENERIC_ERROR	2
128 
129 #define	PPTP_CDN_RESULT_LOST_CARRIER		1
130 #define	PPTP_CDN_RESULT_GENRIC_ERROR		2
131 #define	PPTP_CDN_RESULT_ADMIN_SHUTDOWN		3
132 #define	PPTP_CDN_RESULT_REQUEST			4
133 
134 /* Default TCP port number */
135 #define	PPTPD_DEFAULT_TCP_PORT			1723
136 
137 
138 #define	PPTP_GRE_PROTOCOL_TYPE			0x880b
139 #define	PPTP_GRE_VERSION			1
140 
141 /*
142  * Constants of the NPPPD implementation
143  */
144 #include "pptp_conf.h"
145 
146 /* pptpd status */
147 #define	PPTPD_STATE_INIT 		0
148 #define	PPTPD_STATE_RUNNING 		1
149 #define	PPTPD_STATE_SHUTTING_DOWN 	2
150 #define	PPTPD_STATE_STOPPED 		3
151 
152 #define	PPTPD_CONFIG_BUFSIZ		65535
153 
154 #define	PPTP_BACKLOG	32
155 #define PPTP_BUFSIZ	1024
156 
157 #define PPTPD_DEFAULT_LAYER2_LABEL		"PPTP"
158 
159 /* pptp control state code */
160 #define PPTP_CTRL_STATE_IDLE			0
161 #define PPTP_CTRL_STATE_WAIT_CTRL_REPLY		1
162 #define PPTP_CTRL_STATE_ESTABLISHED		2
163 #define PPTP_CTRL_STATE_WAIT_STOP_REPLY		3
164 #define PPTP_CTRL_STATE_DISPOSING		4
165 
166 #ifndef	PPTPD_DEFAULT_VENDOR_NAME
167 #define	PPTPD_DEFAULT_VENDOR_NAME		""
168 #endif
169 
170 #ifndef	PPTP_CALL_DEFAULT_MAXWINSZ
171 #define	PPTP_CALL_DEFAULT_MAXWINSZ		64
172 #endif
173 
174 /* Connection speed that notified by OCRP */
175 /* XXX: currently we use fixed value */
176 #ifndef	PPTP_CALL_CONNECT_SPEED
177 #define	PPTP_CALL_CONNECT_SPEED			10000000
178 #endif
179 
180 /* Initial packet processing delay that notified by OCRP */
181 #ifndef	PPTP_CALL_INITIAL_PPD
182 #define PPTP_CALL_INITIAL_PPD			0
183 #endif
184 
185 /**
186  * PPTP_CALL_DELAY_LIMIT indicates how many sequence number can be rewinded
187  * by reordering.
188  */
189 #define	PPTP_CALL_DELAY_LIMIT			64
190 
191 /* pptp call state machine */
192 #define	PPTP_CALL_STATE_IDLE			0
193 #define	PPTP_CALL_STATE_WAIT_CONN		1
194 #define	PPTP_CALL_STATE_ESTABLISHED		2
195 #define	PPTP_CALL_STATE_CLEANUP_WAIT		3
196 
197 /* Timeout */
198 #define PPTPD_SHUTDOWN_TIMEOUT			5
199 
200 #define	PPTPD_IDLE_TIMEOUT			60
201 
202 #define	PPTP_CALL_CLEANUP_WAIT_TIME		3
203 
204 #define PPTP_CTRL_DEFAULT_ECHO_INTERVAL		60
205 #define PPTP_CTRL_DEFAULT_ECHO_TIMEOUT		60
206 #define	PPTP_CTRL_StopCCRP_WAIT_TIME		3
207 
208 /* MAXIMUM bindable IP addresses */
209 #ifndef	PPTP_NLISTENER
210 #define	PPTP_NLISTENER				6
211 #endif
212 
213 /* Utility macro */
214 #define	pptpd_is_stopped(pptpd)					\
215 	(((pptpd)->state != PPTPD_STATE_SHUTTING_DOWN &&	\
216 	    (pptpd)->state != PPTPD_STATE_RUNNING)? 1 : 0)
217 
218 #define	pptpd_is_shutting_down(pptpd)				\
219 	(((pptpd)->state == PPTPD_STATE_SHUTTING_DOWN)? 1 : 0)
220 
221 /*
222  * types
223  */
224 struct _pptpd;
225 
226 typedef struct _pptpd_listener {
227 	struct event ev_sock_gre; /* GRE event context */
228 	struct _pptpd	*self;
229 	uint16_t	index;
230 	int		sock; /* listing socket */
231 	int		sock_gre; /* GRE socket */
232 	struct sockaddr_in bind_sin;	/* listing TCP address */
233 	struct sockaddr_in bind_sin_gre; /* listing GRE address */
234 	char	tun_name[PPTP_NAME_LEN];
235 	struct pptp_conf *conf;
236 } pptpd_listener;
237 
238 typedef struct _pptpd {
239 	unsigned	id;
240 	slist listener;		/* list of listeners */
241 	int state;
242 	struct event ev_timer; /* timer event context */
243 	slist  ctrl_list;	/* list of PPTP controls */
244 
245 	slist call_free_list;	/* Free call lists */
246 	hash_table *call_id_map; /* table to map between callid and call */
247 	/* ipv4 networks that is permitted to connect */
248 
249 	uint32_t		/* flags */
250 	    initialized:1;
251 } pptpd;
252 
253 #define pptp_ctrl_sock_gre(ctrl)	\
254 	((pptpd_listener *)slist_get(&(ctrl)->pptpd->listener,\
255 	    (ctrl)->listener_index))->sock_gre
256 
257 /* get listner's physical layer label from pptp_ctrl */
258 #define	PPTP_CTRL_LISTENER_TUN_NAME(ctrl)	\
259 	((pptpd_listener *)slist_get(&(ctrl)->pptpd->listener,\
260 	    (ctrl)->listener_index))->tun_name
261 
262 #define	PPTP_CTRL_CONF(ctrl)					\
263 	((pptpd_listener *)slist_get(&(ctrl)->pptpd->listener,	\
264 	    (ctrl)->listener_index))->conf
265 
266 typedef struct _pptp_ctrl {
267 	pptpd		*pptpd;	/* parents */
268 	uint16_t	listener_index;
269 	unsigned 	id;
270 	int		state;
271 
272 	int		sock;
273 	struct sockaddr_storage peer;
274 	struct sockaddr_storage our;
275 	struct event	ev_sock;
276 	struct event	ev_timer;
277 
278 	int echo_interval; /* periods between idle state to ECHO transmit */
279 	int echo_timeout;
280 
281 	int		send_ready; /* ready to send */
282 	bytebuffer	*recv_buf;
283 	bytebuffer	*send_buf;
284 
285 	slist		call_list;
286 
287 	time_t	last_snd_ctrl;	/* timestamp of latest ctrl message sent */
288 	time_t	last_rcv_ctrl;	/* timestamp of latest ctrl message received */
289 	uint32_t	echo_seq; /* identifier of Echo Request */
290 
291 	uint16_t	/* flags : processing I/O events */
292 			on_io_event:1,
293 			reserved:15;
294 } pptp_ctrl;
295 
296 typedef struct _pptp_call {
297 	pptp_ctrl	*ctrl; /* parent */
298 	unsigned	id;
299 
300 	int		ifidx; /* receive interface index */
301 
302 	int		state;
303 
304 	unsigned	peers_call_id;
305 	void		*ppp;
306 
307 	uint32_t	snd_una;	/* next ack notification */
308 	uint32_t	snd_nxt;	/* next transmit sequence # */
309 
310 	uint32_t	rcv_nxt;	/* received sequence # */
311 	uint32_t	rcv_acked;	/* latest acked received sequence # */
312 
313 	int		winsz;		/* current window size */
314 	int		maxwinsz;	/* maximum window size */
315 	int		peers_maxwinsz;
316 
317 	time_t		last_io;
318 } pptp_call;
319 
320 
321 /*
322  * function prototypes
323  */
324 #ifdef __cplusplus
325 extern "C" {
326 #endif
327 
328 int   pptpd_init (pptpd *);
329 void  pptpd_uninit (pptpd *);
330 int   pptpd_assign_call (pptpd *, pptp_call *);
331 void  pptpd_release_call (pptpd *, pptp_call *);
332 int   pptpd_start (pptpd *);
333 void  pptpd_stop (pptpd *);
334 void pptpd_stop_immediatly (pptpd *);
335 void  pptpd_ctrl_finished_notify(pptpd *, pptp_ctrl *);
336 int  pptpd_add_listener(pptpd *, int, struct pptp_conf *, struct sockaddr *);
337 
338 pptp_ctrl  *pptp_ctrl_create (void);
339 int        pptp_ctrl_init (pptp_ctrl *);
340 int        pptp_ctrl_start (pptp_ctrl *);
341 void       pptp_ctrl_stop (pptp_ctrl *, int);
342 void       pptp_ctrl_destroy (pptp_ctrl *);
343 void       pptp_ctrl_output (pptp_ctrl *, u_char *, int);
344 
345 pptp_call  *pptp_call_create (void);
346 int        pptp_call_init (pptp_call *, pptp_ctrl *);
347 int        pptp_call_start (pptp_call *);
348 int        pptp_call_stop (pptp_call *);
349 void       pptp_call_destroy (pptp_call *);
350 void       pptp_call_input (pptp_call *, int, u_char *, int);
351 void       pptp_call_gre_input (pptp_call *, uint32_t, uint32_t, int, u_char *, int);
352 void       pptp_call_disconnect(pptp_call *, int, int, const char *);
353 int        pptpd_reload(pptpd *, struct pptp_confs *);
354 
355 #ifdef __cplusplus
356 }
357 #endif
358 #endif
359