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