xref: /netbsd-src/external/bsd/ppp/dist/pppd/pppd.h (revision f12839c5f795a8def46f685de6698463dbd213a9)
1 /*	$NetBSD: pppd.h,v 1.7 2025/01/08 19:59:39 christos Exp $	*/
2 
3 /*
4  * pppd.h - PPP daemon global declarations.
5  *
6  * Copyright (c) 1984-2000 Carnegie Mellon University. All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. The name "Carnegie Mellon University" must not be used to
21  *    endorse or promote products derived from this software without
22  *    prior written permission. For permission or any legal
23  *    details, please contact
24  *      Office of Technology Transfer
25  *      Carnegie Mellon University
26  *      5000 Forbes Avenue
27  *      Pittsburgh, PA  15213-3890
28  *      (412) 268-4387, fax: (412) 268-7395
29  *      tech-transfer@andrew.cmu.edu
30  *
31  * 4. Redistributions of any form whatsoever must retain the following
32  *    acknowledgment:
33  *    "This product includes software developed by Computing Services
34  *     at Carnegie Mellon University (http://www.cmu.edu/computing/)."
35  *
36  * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
37  * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
38  * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
39  * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
40  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
41  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
42  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
43  */
44 
45 #ifndef PPP_PPPD_H
46 #define PPP_PPPD_H
47 
48 #include <stdarg.h>
49 #include <stdbool.h>
50 #include <stddef.h>
51 #include <stdint.h>
52 #include <sys/types.h>
53 
54 #include "pppdconf.h"
55 
56 #ifdef __cplusplus
57 extern "C" {
58 #endif
59 
60 /*
61  * Limits
62  */
63 #define NUM_PPP		1	/* One PPP interface supported (per process) */
64 #define MAXWORDLEN	1024	/* max length of word in file (incl null) */
65 #define MAXARGS		1	/* max # args to a command */
66 #define MAXNAMELEN	256	/* max length of hostname or name for auth */
67 #define MAXSECRETLEN	256	/* max length of password or secret */
68 
69 
70 /*
71  * Values for phase.
72  */
73 typedef enum ppp_phase
74 {
75     PHASE_DEAD,
76     PHASE_INITIALIZE,
77     PHASE_SERIALCONN,
78     PHASE_DORMANT,
79     PHASE_ESTABLISH,
80     PHASE_AUTHENTICATE,
81     PHASE_CALLBACK,
82     PHASE_NETWORK,
83     PHASE_RUNNING,
84     PHASE_TERMINATE,
85     PHASE_DISCONNECT,
86     PHASE_HOLDOFF,
87     PHASE_MASTER,
88 } ppp_phase_t;
89 
90 /*
91  * Values for exit codes
92  */
93 typedef enum ppp_exit_code
94 {
95     EXIT_OK                 = 0,
96     EXIT_FATAL_ERROR        = 1,
97     EXIT_OPTION_ERROR       = 2,
98     EXIT_NOT_ROOT           = 3,
99     EXIT_NO_KERNEL_SUPPORT  = 4,
100     EXIT_USER_REQUEST       = 5,
101     EXIT_LOCK_FAILED        = 6,
102     EXIT_OPEN_FAILED        = 7,
103     EXIT_CONNECT_FAILED     = 8,
104     EXIT_PTYCMD_FAILED      = 9,
105     EXIT_NEGOTIATION_FAILED = 10,
106     EXIT_PEER_AUTH_FAILED   = 11,
107     EXIT_IDLE_TIMEOUT       = 12,
108     EXIT_CONNECT_TIME       = 13,
109     EXIT_CALLBACK           = 14,
110     EXIT_PEER_DEAD          = 15,
111     EXIT_HANGUP             = 16,
112     EXIT_LOOPBACK           = 17,
113     EXIT_INIT_FAILED        = 18,
114     EXIT_AUTH_TOPEER_FAILED = 19,
115     EXIT_TRAFFIC_LIMIT      = 20,
116     EXIT_CNID_AUTH_FAILED   = 21
117 } ppp_exit_code_t;
118 
119 /*
120  * Type of notifier callbacks
121  */
122 typedef enum
123 {
124     NF_PID_CHANGE,
125     NF_PHASE_CHANGE,
126     NF_EXIT,
127     NF_SIGNALED,
128     NF_IP_UP,
129     NF_IP_DOWN,
130     NF_IPV6_UP,
131     NF_IPV6_DOWN,
132     NF_AUTH_UP,
133     NF_LINK_DOWN,
134     NF_FORK,
135     NF_MAX_NOTIFY
136 } ppp_notify_t;
137 
138 typedef enum
139 {
140     PPP_DIR_LOG,
141     PPP_DIR_RUNTIME,
142     PPP_DIR_CONF,
143     PPP_DIR_PLUGIN,
144 } ppp_path_t;
145 
146 /*
147  * Unfortunately, the linux kernel driver uses a different structure
148  * for statistics from the rest of the ports.
149  * This structure serves as a common representation for the bits
150  * pppd needs.
151  */
152 struct pppd_stats
153 {
154     uint64_t		bytes_in;
155     uint64_t		bytes_out;
156     unsigned int	pkts_in;
157     unsigned int	pkts_out;
158 };
159 typedef struct pppd_stats ppp_link_stats_st;
160 
161 /*
162  * Used for storing a sequence of words.  Usually malloced.
163  */
164 struct wordlist {
165     struct wordlist	*next;
166     char		*word;
167 };
168 
169 struct option;
170 typedef void (*printer_func)(void *, char *, ...);
171 
172 /*
173  * The following struct gives the addresses of procedures to call for a particular protocol.
174  */
175 struct protent {
176     /* PPP protocol number */
177     unsigned short protocol;
178     /* Initialization procedure */
179     void (*init)(int unit);
180     /* Process a received packet */
181     void (*input)(int unit, unsigned char *pkt, int len);
182     /* Process a received protocol-reject */
183     void (*protrej)(int unit);
184     /* Lower layer has come up */
185     void (*lowerup)(int unit);
186     /* Lower layer has gone down */
187     void (*lowerdown)(int unit);
188     /* Open the protocol */
189     void (*open)(int unit);
190     /* Close the protocol */
191     void (*close)(int unit, char *reason);
192     /* Print a packet in readable form */
193     int  (*printpkt)(unsigned char *pkt, int len, printer_func printer, void *arg);
194     /* Process a received data packet */
195     void (*datainput)(int unit, unsigned char *pkt, int len);
196     /* 0 iff protocol is disabled */
197     bool enabled_flag;
198     /* Text name of protocol */
199     char *name;
200     /* Text name of corresponding data protocol */
201     char *data_name;
202     /* List of command-line options */
203     struct option *options;
204     /* Check requested options, assign defaults */
205     void (*check_options)(void);
206     /* Configure interface for demand-dial */
207     int  (*demand_conf)(int unit);
208     /* Say whether to bring up link for this pkt */
209     int  (*active_pkt)(unsigned char *pkt, int len);
210 };
211 
212 /* Table of pointers to supported protocols */
213 extern struct protent *protocols[];
214 
215 
216 /*
217  * This struct contains pointers to a set of procedures for doing operations on a "channel".
218  * A channel provides a way to send and receive PPP packets - the canonical example is a serial
219  * port device in PPP line discipline (or equivalently with PPP STREAMS modules pushed onto it).
220  */
221 struct channel {
222 	/* set of options for this channel */
223 	struct option *options;
224 	/* find and process a per-channel options file */
225 	void (*process_extra_options)(void);
226 	/* check all the options that have been given */
227 	void (*check_options)(void);
228 	/* get the channel ready to do PPP, return a file descriptor */
229 	int  (*connect)(void);
230 	/* we're finished with the channel */
231 	void (*disconnect)(void);
232 	/* put the channel into PPP `mode' */
233 	int  (*establish_ppp)(int);
234 	/* take the channel out of PPP `mode', restore loopback if demand */
235 	void (*disestablish_ppp)(int);
236 	/* set the transmit-side PPP parameters of the channel */
237 	void (*send_config)(int, uint32_t, int, int);
238 	/* set the receive-side PPP parameters of the channel */
239 	void (*recv_config)(int, uint32_t, int, int);
240 	/* cleanup on error or normal exit */
241 	void (*cleanup)(void);
242 	/* close the device, called in children after fork */
243 	void (*close)(void);
244 };
245 
246 extern struct channel *the_channel;
247 
248 
249 /*
250  * Functions for string formatting and debugging
251  */
252 
253 /* Is debug enabled */
254 bool debug_on(void);
255 
256 /* Safe sprintf++ */
257 int slprintf(char *, int, const char *, ...);
258 
259 /* vsprintf++ */
260 int vslprintf(char *, int, const char *, va_list);
261 
262 /* safe strcpy */
263 size_t strlcpy(char *, const char *, size_t);
264 
265 /* safe strncpy */
266 size_t strlcat(char *, const char *, size_t);
267 
268 /* log a debug message */
269 void dbglog(const char *, ...);
270 
271 /* log an informational message */
272 void info(const char *, ...);
273 
274 /* log a notice-level message */
275 void notice(const char *, ...);
276 
277 /* log a warning message */
278 void warn(const char *, ...);
279 
280 /* log an error message */
281 void error(const char *, ...);
282 
283 /* log an error message and die(1) */
284 void fatal(const char *, ...);
285 
286 /* Say we ran out of memory, and die */
287 void novm(const char *);
288 
289 /* Format a packet and log it with syslog */
290 void log_packet(unsigned char *, int, char *, int);
291 
292 /* dump packet to debug log if interesting */
293 void dump_packet(const char *, unsigned char *, int);
294 
295 /* initialize for using pr_log */
296 void init_pr_log(const char *, int);
297 
298 /* printer fn, output to syslog */
299 void pr_log(void *, char *, ...);
300 
301 /* finish up after using pr_log */
302 void end_pr_log(void);
303 
304 /*
305  * Get the current exist status of pppd
306  */
307 ppp_exit_code_t ppp_status(void);
308 
309 /*
310  * Set the exit status
311  */
312 void ppp_set_status(ppp_exit_code_t code);
313 
314 /*
315  * Configure the session's maximum number of octets
316  */
317 void ppp_set_session_limit(unsigned int octets);
318 
319 /*
320  * Which direction to limit the number of octets
321  */
322 void ppp_set_session_limit_dir(unsigned int direction);
323 
324 /*
325  * Get the current link stats, returns true when valid and false if otherwise
326  */
327 bool ppp_get_link_stats(ppp_link_stats_st *stats);
328 
329 /*
330  * Get pppd's notion of time
331  */
332 struct timeval;
333 int ppp_get_time(struct timeval *);
334 
335 /*
336  * Schedule a callback in s.us seconds from now
337  */
338 typedef void (*ppp_timer_cb)(void *arg);
339 void ppp_timeout(ppp_timer_cb func, void *arg, int s, int us);
340 
341 /*
342  * Cancel any pending timer callbacks
343  */
344 void ppp_untimeout(void (*func)(void *), void *arg);
345 
346 /*
347  * Clean up in a child before execing
348  */
349 void ppp_sys_close(void);
350 
351 /*
352  * Fork & close stuff in child
353  */
354 pid_t ppp_safe_fork(int, int, int);
355 
356 /*
357  * Get the current hostname
358  */
359 const char *ppp_hostname(void);
360 
361 /*
362  * Is pppd using pty as a device (opposed to notty or pty opt).
363  */
364 bool ppp_using_pty(void);
365 
366 /*
367  * Device is synchronous serial device
368  */
369 bool ppp_sync_serial(void);
370 
371 /*
372  * Modem mode
373  */
374 bool ppp_get_modem(void);
375 
376 /*
377  * Control the mode of the tty terminal
378  */
379 void ppp_set_modem(bool on);
380 
381 /*
382  * Set the current session number, e.g. for PPPoE
383  */
384 void ppp_set_session_number(int number);
385 
386 /*
387  * Set the current session number, e.g. for PPPoE
388  */
389 int ppp_get_session_number(void);
390 
391 /*
392  * Check if pppd got signaled, returns 0 if not signaled, returns -1 on failure, and the signal number when signaled.
393  */
394 bool ppp_signaled(int sig);
395 
396 /*
397  * Maximum connect time in seconds
398  */
399 int ppp_get_max_connect_time(void);
400 
401 /*
402  * Set the maximum connect time in seconds
403  */
404 void ppp_set_max_connect_time(unsigned int max);
405 
406 /*
407  * Get the link idle time before shutting the link down
408  */
409 int ppp_get_max_idle_time(void);
410 
411 /*
412  * Set the link idle time before shutting the link down
413  */
414 void ppp_set_max_idle_time(unsigned int idle);
415 
416 /*
417  * Get the duration the link was up (uptime)
418  */
419 int ppp_get_link_uptime(void);
420 
421 /*
422  * Get the ipparam configured with pppd
423  */
424 const char *ppp_ipparam(void);
425 
426 /*
427  * check if IP address is unreasonable
428  */
429 bool ppp_bad_ip_addr(uint32_t);
430 
431 /*
432  * Expose an environment variable to scripts
433  */
434 void ppp_script_setenv(char *, char *, int);
435 
436 /*
437  * Unexpose an environment variable to scripts
438  */
439 void ppp_script_unsetenv(char *);
440 
441 /*
442  * Test whether ppp kernel support exists
443  */
444 int ppp_check_kernel_support(void);
445 
446 /*
447  * Restore device setting
448  */
449 void ppp_generic_disestablish(int dev_fd);
450 
451 /*
452  * Set the interface MTU
453  */
454 void ppp_set_mtu(int, int);
455 
456 /*
457  * Get the interface MTU
458  */
459 int  ppp_get_mtu(int);
460 
461 /*
462  * Make a ppp interface
463  */
464 int ppp_generic_establish(int dev_fd);
465 
466 /*
467  * Get the peer's authentication name
468  */
469 const char *ppp_peer_authname(char *buf, size_t bufsz);
470 
471 /*
472  * Get the remote name
473  */
474 const char *ppp_remote_name(void);
475 
476 /*
477  * Get the remote number (if set), otherwise return NULL
478  */
479 const char *ppp_get_remote_number(void);
480 
481 /*
482  * Set the remote number, typically it's a MAC address
483  */
484 void ppp_set_remote_number(const char *buf);
485 
486 /*
487  * Get the current interface unit for the pppX device
488  */
489 int ppp_ifunit(void);
490 
491 /*
492  * Get the current interface name
493  */
494 const char *ppp_ifname(void);
495 
496 /*
497  * Get the current interface name
498  */
499 int ppp_get_ifname(char *buf, size_t bufsz);
500 
501 /*
502  * Set the current interface name, ifname is a \0 terminated string
503  */
504 void ppp_set_ifname(const char *ifname);
505 
506 /*
507  * Set the original devnam (prior to any renaming, etc).
508  */
509 int ppp_set_pppdevnam(const char *name);
510 
511 /*
512  * Get the original devnam (prior to any renaming, etc).
513  */
514 const char *ppp_pppdevnam(void);
515 
516 /*
517  * Get the current devnam, e.g. /dev/ttyS0, /dev/ptmx
518  */
519 const char *ppp_devnam(void);
520 
521 /*
522  * Set the device name
523  */
524 int ppp_set_devnam(const char *name);
525 
526 /*
527  * Definition for the notify callback function
528  *   ctx - contextual argument provided with the registration
529  *   arg - anything passed by the notification, e.g. phase, pid, etc
530  */
531 typedef void (ppp_notify_fn)(void *ctx, int arg);
532 
533 /*
534  * Add a callback notification for when a given event has occured
535  */
536 void ppp_add_notify(ppp_notify_t type, ppp_notify_fn *func, void *ctx);
537 
538 /*
539  * Remove a callback notification previously registered
540  */
541 void ppp_del_notify(ppp_notify_t type, ppp_notify_fn *func, void *ctx);
542 
543 /*
544  * Get the path prefix in which a file is installed
545  */
546 int ppp_get_path(ppp_path_t type, char *buf, size_t bufsz);
547 
548 /*
549  * Get the file with path prefix
550  */
551 int ppp_get_filepath(ppp_path_t type, const char *name, char *buf, size_t bufsz);
552 
553 /*
554  * Check if pppd is to re-open link after it goes down
555  */
556 bool ppp_persist(void);
557 
558 /*
559  * Hooks to enable plugins to hook into various parts of the code
560  */
561 
562 struct ppp_idle; /* Declared in <linux/ppp_defs.h> */
563 extern int (*idle_time_hook)(struct ppp_idle *);
564 extern int (*new_phase_hook)(int);
565 extern int (*holdoff_hook)(void);
566 extern int  (*allowed_address_hook)(uint32_t addr);
567 extern void (*snoop_recv_hook)(unsigned char *p, int len);
568 extern void (*snoop_send_hook)(unsigned char *p, int len);
569 
570 #ifdef __cplusplus
571 }
572 #endif
573 
574 #endif /* PPP_PPPD_H */
575