1/*
2 * Copyright (c) 2003, 2014 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23/*
24 * pppd.h - PPP daemon global declarations.
25 *
26 * Copyright (c) 1984-2000 Carnegie Mellon University. All rights reserved.
27 *
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
30 * are met:
31 *
32 * 1. Redistributions of source code must retain the above copyright
33 *    notice, this list of conditions and the following disclaimer.
34 *
35 * 2. Redistributions in binary form must reproduce the above copyright
36 *    notice, this list of conditions and the following disclaimer in
37 *    the documentation and/or other materials provided with the
38 *    distribution.
39 *
40 * 3. The name "Carnegie Mellon University" must not be used to
41 *    endorse or promote products derived from this software without
42 *    prior written permission. For permission or any legal
43 *    details, please contact
44 *      Office of Technology Transfer
45 *      Carnegie Mellon University
46 *      5000 Forbes Avenue
47 *      Pittsburgh, PA  15213-3890
48 *      (412) 268-4387, fax: (412) 268-7395
49 *      tech-transfer@andrew.cmu.edu
50 *
51 * 4. Redistributions of any form whatsoever must retain the following
52 *    acknowledgment:
53 *    "This product includes software developed by Computing Services
54 *     at Carnegie Mellon University (http://www.cmu.edu/computing/)."
55 *
56 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
57 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
58 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
59 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
60 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
61 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
62 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
63 *
64 * $Id: pppd.h,v 1.35.12.1 2006/04/17 18:37:15 callie Exp $
65 */
66
67/*
68 * TODO:
69 */
70
71#ifndef __PPPD_H__
72#define __PPPD_H__
73
74#include <stdio.h>		/* for FILE */
75#include <limits.h>		/* for NGROUPS_MAX */
76#include <sys/param.h>		/* for MAXPATHLEN and BSD4_4, if defined */
77#include <sys/types.h>		/* for u_int32_t, if defined */
78#include <sys/time.h>		/* for struct timeval */
79#include <netinet/in.h>		/* for struct in_addr */
80#ifdef __APPLE__
81#include <ppp_defs.h>
82#else
83#include <net/ppp_defs.h>
84#endif
85#include "patchlevel.h"
86#ifdef __APPLE__
87#include "../../PPP_VERSION.h"
88#include <SystemConfiguration/SystemConfiguration.h>
89#include <dns_sd.h>
90#include "scnc_main.h"
91#include "fsm.h"
92#endif
93
94#if defined(__STDC__)
95#include <stdarg.h>
96#define __V(x)	x
97#else
98#include <varargs.h>
99#define __V(x)	(va_alist) va_dcl
100#define const
101#define volatile
102#endif
103
104#include <sys/kern_event.h>
105
106#ifdef INET6
107#include "eui64.h"
108#endif
109
110/*
111 * Limits.
112 */
113
114#define NUM_PPP		1	/* One PPP interface supported (per process) */
115#define MAXWORDLEN	1024	/* max length of word in file (incl null) */
116#define MAXARGS		1	/* max # args to a command */
117#define MAXNAMELEN	256	/* max length of hostname or name for auth */
118#define MAXSECRETLEN	256	/* max length of password or secret */
119
120/*
121 * Option descriptor structure.
122 */
123
124#ifdef __APPLE__
125// bool is defined in headers as 4 bytes
126#undef bool
127#endif
128
129typedef unsigned char	bool;
130
131enum opt_type {
132	o_special_noarg = 0,
133	o_special = 1,
134	o_bool,
135	o_int,
136	o_uint32,
137	o_string,
138	o_wild,
139	o_special_cfarg
140};
141
142typedef struct {
143	char	*name;		/* name of the option */
144	enum opt_type type;
145	void	*addr;
146	char	*description;
147	unsigned int flags;
148	void	*addr2;
149	int	upper_limit;
150	int	lower_limit;
151	const char *source;
152	short int priority;
153	short int winner;
154#ifdef __APPLE__
155	void	*addr3;
156	char **other_source;
157	int	nb_other_source;
158#endif
159} option_t;
160
161/* Values for flags */
162#define OPT_VALUE	0xff	/* mask for presupplied value */
163#define OPT_HEX		0x100	/* int option is in hex */
164#define OPT_NOARG	0x200	/* option doesn't take argument */
165#define OPT_OR		0x400	/* OR in argument to value */
166#define OPT_INC		0x800	/* increment value */
167#define OPT_A2OR	0x800	/* for o_bool, OR arg to *(u_char *)addr2 */
168#define OPT_PRIV	0x1000	/* privileged option */
169#define OPT_STATIC	0x2000	/* string option goes into static array */
170#define OPT_LLIMIT	0x4000	/* check value against lower limit */
171#define OPT_ULIMIT	0x8000	/* check value against upper limit */
172#define OPT_LIMITS	(OPT_LLIMIT|OPT_ULIMIT)
173#define OPT_ZEROOK	0x10000	/* 0 value is OK even if not within limits */
174#define OPT_HIDE	0x10000	/* for o_string, print value as ?????? */
175#define OPT_A2LIST	0x10000 /* for o_special, keep list of values */
176#define OPT_A2CLRB	0x10000 /* o_bool, clr val bits in *(u_char *)addr2 */
177#define OPT_NOINCR	0x20000	/* value mustn't be increased */
178#define OPT_ZEROINF	0x40000	/* with OPT_NOINCR, 0 == infinity */
179#define OPT_PRIO	0x80000	/* process option priorities for this option */
180#define OPT_PRIOSUB	0x100000 /* subsidiary member of priority group */
181#define OPT_ALIAS	0x200000 /* option is alias for previous option */
182#define OPT_A2COPY	0x400000 /* addr2 -> second location to rcv value */
183#define OPT_ENABLE	0x800000 /* use *addr2 as enable for option */
184#define OPT_A2CLR	0x1000000 /* clear *(bool *)addr2 */
185#define OPT_PRIVFIX	0x2000000 /* user can't override if set by root */
186#define OPT_INITONLY	0x4000000 /* option can only be set in init phase */
187#define OPT_DEVEQUIV	0x8000000 /* equiv to device name */
188#define OPT_DEVNAM	(OPT_INITONLY | OPT_DEVEQUIV)
189#define OPT_A2PRINTER	0x10000000 /* *addr2 is a fn for printing option */
190#define OPT_A2STRVAL	0x20000000 /* *addr2 points to current string value */
191#define OPT_NOPRINT	0x40000000 /* don't print this option at all */
192
193#define OPT_VAL(x)	((x) & OPT_VALUE)
194
195/* Values for priority */
196#define OPRIO_DEFAULT	0	/* a default value */
197#define OPRIO_CFGFILE	1	/* value from a configuration file */
198#define OPRIO_CMDLINE	2	/* value from the command line */
199#define OPRIO_SECFILE	3	/* value from options in a secrets file */
200#define OPRIO_ROOT	100	/* added to priority if OPT_PRIVFIX && root */
201
202#ifndef GIDSET_TYPE
203#define GIDSET_TYPE	gid_t
204#endif
205
206/* Structure representing a list of permitted IP addresses. */
207struct permitted_ip {
208    int		permit;		/* 1 = permit, 0 = forbid */
209    u_int32_t	base;		/* match if (addr & mask) == base */
210    u_int32_t	mask;		/* base and mask are in network byte order */
211};
212
213/*
214 * Unfortunately, the linux kernel driver uses a different structure
215 * for statistics from the rest of the ports.
216 * This structure serves as a common representation for the bits
217 * pppd needs.
218 */
219struct pppd_stats {
220    unsigned int	bytes_in;
221    unsigned int	bytes_out;
222    unsigned int	pkts_in;
223    unsigned int	pkts_out;
224};
225
226/* Used for storing a sequence of words.  Usually malloced. */
227struct wordlist {
228    struct wordlist	*next;
229    char		*word;
230};
231
232/* An endpoint discriminator, used with multilink. */
233#define MAX_ENDP_LEN	20	/* maximum length of discriminator value */
234struct epdisc {
235    unsigned char	class;
236    unsigned char	length;
237    unsigned char	value[MAX_ENDP_LEN];
238};
239
240/* values for epdisc.class */
241#define EPD_NULL	0	/* null discriminator, no data */
242#define EPD_LOCAL	1
243#define EPD_IP		2
244#define EPD_MAC		3
245#define EPD_MAGIC	4
246#define EPD_PHONENUM	5
247
248typedef void (*notify_func) __P((void *, uintptr_t));
249
250struct notifier {
251    struct notifier *next;
252    notify_func	    func;
253    void	    *arg;
254};
255
256/*
257 * Global variables.
258 */
259
260extern int	hungup;		/* Physical layer has disconnected */
261#ifdef __APPLE__
262extern int do_modem_hungup;			/* need to finish disconnection */
263#endif
264extern int	ifunit;		/* Interface unit number */
265extern char	ifname[];	/* Interface name */
266extern char	hostname[];	/* Our hostname */
267#ifdef __APPLE__
268extern u_char	outpacket_buf[PPP_MRU+PPP_HDRLEN]; /* Buffer for outgoing packets */
269#else
270extern u_char	outpacket_buf[]; /* Buffer for outgoing packets */
271#endif
272extern int	phase;		/* Current state of link - see values below */
273extern int	baud_rate;	/* Current link speed in bits/sec */
274extern char	*progname;	/* Name of this program */
275extern int	redirect_stderr;/* Connector's stderr should go to file */
276extern char	peer_authname[];/* Authenticated name of peer */
277extern int	auth_done[NUM_PPP]; /* Methods actually used for auth */
278extern int	privileged;	/* We were run by real-uid root */
279extern int	need_holdoff;	/* Need holdoff period after link terminates */
280extern char	**script_env;	/* Environment variables for scripts */
281extern int	detached;	/* Have detached from controlling tty */
282extern GIDSET_TYPE groups[NGROUPS_MAX];	/* groups the user is in */
283extern int	ngroups;	/* How many groups valid in groups */
284extern struct pppd_stats link_stats; /* byte/packet counts etc. for link */
285extern int	link_stats_valid; /* set if link_stats is valid */
286extern unsigned	link_connect_time; /* time the link was up for */
287extern int	using_pty;	/* using pty as device (notty or pty opt.) */
288extern int	log_to_fd;	/* logging to this fd as well as syslog */
289extern bool	log_default;	/* log_to_fd is default (stdout) */
290extern char	*no_ppp_msg;	/* message to print if ppp not in kernel */
291extern volatile int status;	/* exit status for pppd */
292#ifdef __APPLE__
293extern bool	controlled ;	/* Is pppd controlled by the PPPController ?  */
294extern FILE 	*controlfile;	/* file descriptor for options and control */
295extern int 	controlfd;	/* file descriptor for options and control */
296extern int 	statusfd ;	/* file descriptor status update */
297extern volatile int devstatus;	/* exit device status for pppd */
298extern char	username[MAXNAMELEN];/* Our name for authenticating ourselves */
299#endif
300extern bool	devnam_fixed;	/* can no longer change devnam */
301extern int	unsuccess;	/* # unsuccessful connection attempts */
302extern int	do_callback;	/* set if we want to do callback next */
303extern int	doing_callback;	/* set if this is a callback */
304extern int	error_count;	/* # of times error() has been called */
305extern char	ppp_devnam[MAXPATHLEN];
306extern char     remote_number[MAXNAMELEN]; /* Remote telephone number, if avail. */
307extern int      ppp_session_number; /* Session number (eg PPPoE session) */
308
309extern int	listen_time;	/* time to listen first (ms) */
310extern struct notifier *pidchange;   /* for notifications of pid changing */
311extern struct notifier *phasechange; /* for notifications of phase changes */
312extern struct notifier *exitnotify;  /* for notification that we're exiting */
313extern struct notifier *sigreceived; /* notification of received signal */
314extern struct notifier *ip_up_notifier; /* IPCP has come up */
315extern struct notifier *ip_down_notifier; /* IPCP has gone down */
316extern struct notifier *auth_up_notifier; /* peer has authenticated */
317extern struct notifier *link_down_notifier; /* link has gone down */
318extern struct notifier *fork_notifier;	/* we are a new child process */
319extern struct notifier *protocolsready_notifier;	/* all protocols ready */
320extern struct notifier *acspdhcpready_notifier;	/* acsp/dhcp info ready */
321
322#ifdef __APPLE__
323extern int ip_src_address_filter; /* ip source address filter */
324extern u_char inpacket_buf[PPP_MRU+PPP_HDRLEN]; /* buffer for incoming packet */
325extern uid_t	connector_uid;	/* uid for connect script */
326extern uid_t	disconnector_uid;	/*uid for disconnect script */
327extern char	*terminal_script;/* Script to etablish connection once modem is connected */
328extern char	*altconnect_script;/* alternate script to establish physical link */
329extern char	*altconnect_data;/* alternate connect data top pipe to the script */
330extern int altconnect_data_len;/* alternate connect data length */
331extern char	*connect_data;/* connect data top pipe to the script */
332extern int connect_data_len;/* connect data length */
333extern char	*disconnect_data;/* disconnect data top pipe to the script */
334extern int disconnect_data_len;/* disconnect data length */
335extern char	*terminal_data;/* terminal data top pipe to the script */
336extern int terminal_data_len;/* terminal data length */
337extern int 	pty_delay;	/* timeout to wait for the pty command */
338extern char 	*device;	 /* device we are using (can be use as a generic device container) */
339extern char 	*remoteaddress; /* remoteaddress we are connecting to (can be use as a generic address container) */
340extern char 	*altremoteaddress; /* alternate remoteaddress we are connecting to */
341extern char 	*ifscope; /* interface to establish over */
342extern int 	redialcount;	/* number of time to redial */
343extern int 	redialtimer;	/* delay in seconds to wait before to redial */
344extern bool 	redialalternate; /* do we redial alternate number */
345extern int  	redialingcount;  /* current redialing count */
346extern bool  	redialingalternate;  /* currently redialing main or alternate number */
347extern int 	busycode;	/* busy error code that triggers the redial */
348extern bool hasbusystate;	/* change phase to report busy state */
349extern int 	cancelcode;	/* cancel error code for connectors*/
350extern int (*start_link_hook) __P((void));
351extern int (*change_password_hook) __P((u_char *msg));
352extern int (*retry_password_hook) __P((u_char *msg));
353extern int (*link_up_hook) __P((void));
354extern bool link_up_done;
355extern void (*wait_input_hook) __P((void));
356extern int 	extraconnecttime;	/* give some extra connection time to the connection sequence */
357extern int    retry_pre_start_link_check;
358
359extern int (*terminal_window_hook) __P((char *, int, int));
360
361extern struct notifier *auth_start_notify;
362extern struct notifier *auth_withpeer_fail_notify;
363extern struct notifier *auth_withpeer_success_notify;
364extern struct notifier *auth_peer_success_notify;
365/* struct send with auth_peer_success_notify */
366struct auth_peer_success_info {
367    int protocol;
368    int protocol_flavor;
369    char *name;
370    int namelen;
371};
372
373extern struct notifier *lcp_up_notify;
374extern struct notifier *lcp_down_notify;
375extern struct notifier *lcp_lowerup_notify;
376extern struct notifier *lcp_lowerdown_notify;
377
378extern struct notifier *lcp_timeremaining_notify;
379/* struct send with lcp_timeremaining_notify */
380struct lcp_timeremaining_info {
381    int	time;	/* time remaining in seconds */
382    u_char *text;	/* optional text sent by the server */
383    int textlen;/* len of the text */
384};
385
386extern struct notifier *ip_up_notify;
387extern struct notifier *ip_down_notify;
388extern struct notifier *network_probe_start_notify;
389extern struct notifier *network_probe_stop_notify;
390
391extern struct notifier *initscript_started_notify;
392extern struct notifier *initscript_finished_notify;
393extern struct notifier *connectscript_started_notify;
394extern struct notifier *connectscript_finished_notify;
395extern struct notifier *terminalscript_started_notify;
396extern struct notifier *terminalscript_finished_notify;
397
398extern struct notifier *connect_started_notify;
399extern struct notifier *connect_success_notify;
400extern struct notifier *connect_fail_notify;
401extern struct notifier *disconnect_started_notify;
402extern struct notifier *disconnect_done_notify;
403
404extern struct notifier *stop_notify;
405extern struct notifier *cont_notify;
406
407extern struct notifier *system_inited_notify;
408
409extern struct notifier *network_probed_notify;
410
411extern int wait_underlying_interface_up;
412extern int lcp_echo_interval;
413extern int lcp_echo_fails;
414extern int lcp_echo_fails_slow;
415extern int lcp_echo_interval_slow;
416extern int lcp_echos_hastened;
417void lcp_echo_restart __P((int));
418
419#endif
420
421/* Values for do_callback and doing_callback */
422#define CALLBACK_DIALIN		1	/* we are expecting the call back */
423#define CALLBACK_DIALOUT	2	/* we are dialling out to call back */
424
425/*
426 * Variables set by command-line options.
427 */
428
429extern int	debug;		/* Debug flag */
430extern int	kdebugflag;	/* Tell kernel to print debug messages */
431extern int	default_device;	/* Using /dev/tty or equivalent */
432extern char	devnam[MAXPATHLEN];	/* Device name */
433extern int	crtscts;	/* Use hardware flow control */
434extern bool	modem;		/* Use modem control lines */
435extern int	inspeed;	/* Input/Output speed requested */
436extern u_int32_t netmask;	/* IP netmask to set on interface */
437extern bool	lockflag;	/* Create lock file to lock the serial dev */
438extern bool	nodetach;	/* Don't detach from controlling tty */
439extern bool	updetach;	/* Detach from controlling tty when link up */
440extern char	*initializer;	/* Script to initialize physical link */
441extern char	*connect_script; /* Script to establish physical link */
442extern char	*disconnect_script; /* Script to disestablish physical link */
443extern char	*welcomer;	/* Script to welcome client after connection */
444extern char	*ptycommand;	/* Command to run on other side of pty */
445extern int	maxconnect;	/* Maximum connect time (seconds) */
446extern char	user[MAXNAMELEN];/* Our name for authenticating ourselves */
447extern char	passwd[MAXSECRETLEN];	/* Password for PAP or CHAP */
448extern bool	auth_required;	/* Peer is required to authenticate */
449extern bool	persist;	/* Reopen link after it goes down */
450extern bool	uselogin;	/* Use /etc/passwd for checking PAP */
451extern char	our_name[MAXNAMELEN];/* Our name for authentication purposes */
452extern char	remote_name[MAXNAMELEN]; /* Peer's name for authentication */
453extern bool	explicit_remote;/* remote_name specified with remotename opt */
454extern bool	demand;		/* Do dial-on-demand */
455extern char	*ipparam;	/* Extra parameter for ip up/down scripts */
456extern bool	cryptpap;	/* Others' PAP passwords are encrypted */
457extern int	idle_time_limit;/* Shut down link if idle for this long */
458#ifdef __APPLE__
459#define PASSWDFROM_UNKNOWN		0	/* password comes from an unknown location, can't save it back */
460#define PASSWDFROM_KEYCHAIN		1	/* password comes from system keychain */
461#define PASSWDFROM_USERKEYCHAIN 2	/* password comes from user keychain */
462#define PASSWDFROM_PREFS		3	/* password comes from system prefs */
463extern int	passwdfrom;					/* where does the password com from, so we can save a new one back if needed */
464extern char	passwdkey[MAXSECRETLEN];	/* keychain key where the password is located, when itcomes from the keychain  */
465extern char	new_passwd[MAXSECRETLEN];	/* new password for protocol supporting changing password */
466extern bool    	noidlerecv;     /* Disconnect if idle only for outgoing traffic */
467extern bool    	noidlesend;     /* Disconnect if idle only for incoming traffic */
468extern int	tokencard;		/* Token card authentication. default is just name/password */
469extern bool	holdfirst;	/* apply holdoff timer when starting pppd, useful to delay dialondemand */
470#endif
471extern int	holdoff;	/* Dead time before restarting */
472extern bool	holdoff_specified; /* true if user gave a holdoff value */
473extern bool	notty;		/* Stdin/out is not a tty */
474extern char	*pty_socket;	/* Socket to connect to pty */
475extern char	*record_file;	/* File to record chars sent/received */
476extern bool	sync_serial;	/* Device is synchronous serial device */
477extern int	maxfail;	/* Max # of unsuccessful connection attempts */
478extern char	linkname[MAXPATHLEN]; /* logical name for link */
479extern bool	tune_kernel;	/* May alter kernel settings as necessary */
480extern int	connect_delay;	/* Time to delay after connect script */
481extern int	max_data_rate;	/* max bytes/sec through charshunt */
482extern int	req_unit;	/* interface unit number to use */
483extern bool	multilink;	/* enable multilink operation */
484extern bool	noendpoint;	/* don't send or accept endpt. discrim. */
485extern char	*bundle_name;	/* bundle name for multilink */
486extern bool	dump_options;	/* print out option values */
487extern bool	dryrun;		/* check everything, print options, exit */
488
489#ifdef MAXOCTETS
490extern unsigned int maxoctets;	     /* Maximum octetes per session (in bytes) */
491extern int       maxoctets_dir;      /* Direction :
492				      0 - in+out (default)
493				      1 - in
494				      2 - out
495				      3 - max(in,out) */
496extern int       maxoctets_timeout;  /* Timeout for check of octets limit */
497#define PPP_OCTETS_DIRECTION_SUM        0
498#define PPP_OCTETS_DIRECTION_IN         1
499#define PPP_OCTETS_DIRECTION_OUT        2
500#define PPP_OCTETS_DIRECTION_MAXOVERAL  3
501/* same as previos, but little different on RADIUS side */
502#define PPP_OCTETS_DIRECTION_MAXSESSION 4
503#endif
504
505#ifdef PPP_FILTER
506extern struct	bpf_program pass_filter;   /* Filter for pkts to pass */
507extern struct	bpf_program active_filter; /* Filter for link-active pkts */
508#endif
509
510#ifdef MSLANMAN
511extern bool	ms_lanman;	/* Use LanMan password instead of NT */
512				/* Has meaning only with MS-CHAP challenges */
513#endif
514
515/* Values for auth_pending, auth_done */
516#define PAP_WITHPEER	0x1
517#define PAP_PEER	0x2
518#define CHAP_WITHPEER	0x4
519#define CHAP_PEER	0x8
520#define EAP_WITHPEER	0x10
521#define EAP_PEER	0x20
522
523/* Values for auth_done only */
524#define CHAP_MD5_WITHPEER	0x40
525#define CHAP_MD5_PEER		0x80
526#ifdef CHAPMS
527#define CHAP_MS_SHIFT		8	/* LSB position for MS auths */
528#define CHAP_MS_WITHPEER	0x100
529#define CHAP_MS_PEER		0x200
530#define CHAP_MS2_WITHPEER	0x400
531#define CHAP_MS2_PEER		0x800
532#endif
533
534extern char *current_option;	/* the name of the option being parsed */
535extern int  privileged_option;	/* set iff the current option came from root */
536extern char *option_source;	/* string saying where the option came from */
537extern int  option_priority;	/* priority of current options */
538
539/*
540 * Values for phase.
541 */
542#define PHASE_DEAD		0
543#define PHASE_INITIALIZE	1
544#define PHASE_SERIALCONN	2
545#define PHASE_DORMANT		3
546#define PHASE_ESTABLISH		4
547#define PHASE_AUTHENTICATE	5
548#define PHASE_CALLBACK		6
549#define PHASE_NETWORK		7
550#define PHASE_RUNNING		8
551#define PHASE_TERMINATE		9
552#define PHASE_DISCONNECT	10
553#define PHASE_HOLDOFF		11
554#ifdef __APPLE__
555#define PHASE_ONHOLD		12
556#define PHASE_WAITONBUSY	13
557#define PHASE_WAITING		14
558#endif
559
560/*
561 * The following struct gives the addresses of procedures to call
562 * for a particular protocol.
563 */
564struct protent {
565    u_short protocol;		/* PPP protocol number */
566    /* Initialization procedure */
567    void (*init) __P((int unit));
568    /* Process a received packet */
569    void (*input) __P((int unit, u_char *pkt, int len));
570    /* Process a received protocol-reject */
571    void (*protrej) __P((int unit));
572    /* Lower layer has come up */
573    void (*lowerup) __P((int unit));
574    /* Lower layer has gone down */
575    void (*lowerdown) __P((int unit));
576    /* Open the protocol */
577    void (*open) __P((int unit));
578    /* Close the protocol */
579    void (*close) __P((int unit, char *reason));
580    /* Print a packet in readable form */
581    int  (*printpkt) __P((u_char *pkt, int len,
582			  void (*printer) __P((void *, char *, ...)),
583			  void *arg));
584    /* Process a received data packet */
585    void (*datainput) __P((int unit, u_char *pkt, int len));
586    bool enabled_flag;		/* 0 iff protocol is disabled */
587    char *name;			/* Text name of protocol */
588    char *data_name;		/* Text name of corresponding data protocol */
589    option_t *options;		/* List of command-line options */
590    /* Check requested options, assign defaults */
591    void (*check_options) __P((void));
592    /* Configure interface for demand-dial */
593    int  (*demand_conf) __P((int unit));
594    /* Say whether to bring up link for this pkt */
595    int  (*active_pkt) __P((u_char *pkt, int len));
596#ifdef __APPLE__
597    /* connection is on hold */
598    void  (*hold) __P((int unit));
599    /* connection resumes */
600    void  (*cont) __P((int unit));
601    int (*state) __P((int unit));
602    /* Print a data packet in readable form */
603    int  (*printdatapkt) __P((u_char *pkt, int len,
604			  void (*printer) __P((void *, char *, ...)),
605			  void *arg));
606#endif
607};
608
609/* Table of pointers to supported protocols */
610extern struct protent *protocols[];
611
612/*
613 * This struct contains pointers to a set of procedures for
614 * doing operations on a "channel".  A channel provides a way
615 * to send and receive PPP packets - the canonical example is
616 * a serial port device in PPP line discipline (or equivalently
617 * with PPP STREAMS modules pushed onto it).
618 */
619struct channel {
620	/* set of options for this channel */
621	option_t *options;
622	/* find and process a per-channel options file */
623	void (*process_extra_options) __P((void));
624	/* check all the options that have been given */
625	void (*check_options) __P((void));
626	/* get the channel ready to do PPP, return a file descriptor */
627#ifdef __APPLE__
628	int  (*connect) __P((int *));
629#else
630	int  (*connect) __P((void));
631#endif
632	/* we're finished with the channel */
633	void (*disconnect) __P((void));
634	/* put the channel into PPP `mode' */
635	int  (*establish_ppp) __P((int));
636	/* take the channel out of PPP `mode', restore loopback if demand */
637	void (*disestablish_ppp) __P((int));
638	/* set the transmit-side PPP parameters of the channel */
639	void (*send_config) __P((int, u_int32_t, int, int));
640	/* set the receive-side PPP parameters of the channel */
641	void (*recv_config) __P((int, u_int32_t, int, int));
642	/* cleanup on error or normal exit */
643	void (*cleanup) __P((void));
644	/* close the device, called in children after fork */
645	void (*close) __P((void));
646#ifdef __APPLE__
647	void (*wait_input) __P((void));
648	/* before start_link_hook, check reachability of server amongst other things */
649	int (*pre_start_link_check) __P((void));
650#endif
651};
652
653extern struct channel *the_channel;
654
655/*
656 * Prototypes.
657 */
658
659/* Procedures exported from main.c. */
660void set_ifunit __P((int));	/* set stuff that depends on ifunit */
661void detach __P((void));	/* Detach from controlling tty */
662void die __P((int)) __attribute__ ((noreturn));		/* Cleanup and exit */
663void quit __P((void)) __attribute__ ((noreturn));		/* like die(1) */
664void novm __P((char *)) __attribute__ ((noreturn));	/* Say we ran out of memory, and die */
665void timeout __P((void (*func)(void *), void *arg, int s, int us));
666				/* Call func(arg) after s.us seconds */
667void untimeout __P((void (*func)(void *), void *arg));
668				/* Cancel call to func(arg) */
669void record_child __P((int, char *, void (*) (void *), void *));
670pid_t safe_fork __P((void));	/* Fork & close stuff in child */
671#ifdef __APPLE__
672int  device_script __P((char *cmd, int in, int out, int dont_wait, uid_t program_uid, char *pipe_args, int pipe_args_len));
673				/* Run `cmd' with given stdin and stdout */
674#else
675int  device_script __P((char *cmd, int in, int out, int dont_wait));
676				/* Run `cmd' with given stdin and stdout */
677#endif
678pid_t run_program __P((char *prog, char **args, int must_exist,
679		       void (*done)(void *), void *arg));
680				/* Run program prog with args in child */
681void reopen_log __P((void));	/* (re)open the connection to syslog */
682void update_link_stats __P((int)); /* Get stats at link termination */
683void script_setenv __P((char *, char *, int));	/* set script env var */
684void script_unsetenv __P((char *));		/* unset script env var */
685void new_phase __P((int));	/* signal start of new phase */
686void add_notifier __P((struct notifier **, notify_func, void *));
687void add_notifier_last __P((struct notifier **, notify_func, void *));
688void remove_notifier __P((struct notifier **, notify_func, void *));
689void notify __P((struct notifier *, int));
690void notify_with_ptr __P((struct notifier *, uintptr_t));
691int  ppp_send_config __P((int, int, u_int32_t, int, int));
692int  ppp_recv_config __P((int, int, u_int32_t, int, int));
693
694/* Procedures exported from tty.c. */
695void tty_init __P((void));
696
697/* Procedures exported from utils.c. */
698void log_packet __P((u_char *, int, char *, int));
699				/* Format a packet and log it with syslog */
700void print_string __P((char *, int,  void (*) (void *, char *, ...),
701		void *));	/* Format a string for output */
702int slprintf __P((char *, int, char *, ...));		/* sprintf++ */
703int vslprintf __P((char *, int, char *, va_list));	/* vsprintf++ */
704#ifdef NO_SRTLXXX
705size_t strlcpy __P((char *, const char *, size_t));	/* safe strcpy */
706size_t strlcat __P((char *, const char *, size_t));	/* safe strncpy */
707#endif
708void dbglog __P((char *, ...));	/* log a debug message */
709void info __P((char *, ...));	/* log an informational message */
710void notice __P((char *, ...));	/* log a notice-level message */
711void warning __P((char *, ...));	/* log a warning message */
712void error __P((char *, ...));	/* log an error message */
713void fatal __P((char *, ...)) __attribute__ ((noreturn));	/* log an error message and die(1) */
714void init_pr_log __P((char *, int));	/* initialize for using pr_log */
715void pr_log __P((void *, char *, ...));	/* printer fn, output to syslog */
716void end_pr_log __P((void));	/* finish up after using pr_log */
717void dump_packet __P((const char *, u_char *, int));
718				/* dump packet to debug log if interesting */
719ssize_t complete_read __P((int, void *, size_t));
720				/* read a complete buffer */
721#ifdef __APPLE__
722void log_vpn_interface_address_event (const char                  *location,
723									  struct kern_event_msg *ev_msg,
724									  int                    wait_interface_timeout,
725									  u_char                  *interface,
726									  struct in_addr        *our_address);
727int check_vpn_interface_or_service_unrecoverable (SCDynamicStoreRef dynamicStoreRef,
728												  const char                  *location,
729												  struct kern_event_msg *ev_msg,
730												  char                  *interface_buf);
731int check_vpn_interface_address_change (int                    transport_down,
732                                        struct kern_event_msg *ev_msg,
733                                        char                  *interface_buf,
734										int                    interface_media,
735                                        struct in_addr        *our_address);
736int check_vpn_interface_alternate (int                    transport_down,
737                                   struct kern_event_msg *ev_msg,
738                                   char                  *interface_buf);
739
740
741/* -----------------------------------------------------------------------------
742 NAT Port-Mapping apis and data-structures
743 ----------------------------------------------------------------------------- */
744#define PUBLIC_NAT_PORT_MAPPING_TIMEOUT 20
745#define GOOG_DNS_PROBE_ADDR_A           0x08080808
746#define GOOG_DNS_PROBE                  0
747#define PEER_ADDR_PROBE                 1
748#define ALT_PEER_ADDR_PROBE             2
749#define MAX_PROBE_ADDRS                 3
750
751typedef void (*link_failure_func) __P((void));
752typedef void (*probe_disconnect_func) __P((fsm *));
753
754typedef struct ppp_session {
755	int                 valid;
756	char               *sd_name;
757	char               *interface_name;
758	u_int32_t           interface_name_siz;
759	struct in_addr      interface_address;
760	int                 nat_mapping_timer_blocked;
761	int                 nat_mapping_timer_running;
762	mdns_nat_mapping_t  nat_mapping[MDNS_NAT_MAPPING_MAX];
763	u_int32_t           nat_mapping_cnt;
764	link_failure_func   failure_func;
765	int                 probe_timer_running;
766	struct sockaddr_in  probe_addrs[MAX_PROBE_ADDRS];
767	int                 probe_fds[MAX_PROBE_ADDRS]; 	// descriptors for probing link
768	int                 probe_success;
769	int                 probe_tries;
770	int                 probe_ntransmit;
771	int                 opt_noipsec;
772} ppp_session_t;
773
774extern ppp_session_t *session;
775
776#define PPP_SESSION_INITIALIZER() {0, NULL, NULL, 0, {0}, 0, 0, {{0},{0},{0},{0}}, 0, NULL, 0, {{0},{0},{0}}, {-1,-1, -1}, 0, 0, 0}
777
778void ppp_session_clear __P((ppp_session_t *));
779int ppp_variable_echo_is_off __P((void));
780void ppp_variable_echo_start __P((void));
781void ppp_variable_echo_stop __P((void));
782void ppp_auxiliary_probe_init __P((void));
783void ppp_auxiliary_probe_stop __P((void));
784void ppp_auxiliary_probe_check __P((int, probe_disconnect_func, fsm *));
785void ppp_process_auxiliary_probe_input __P((void));
786
787void l2tp_set_nat_port_mapping __P((void));
788void l2tp_clear_nat_port_mapping __P((void));
789void pptp_set_nat_port_mapping __P((void));
790void pptp_clear_nat_port_mapping __P((void));
791void ppp_process_nat_port_mapping_events __P((void));
792void ppp_start_public_nat_port_mapping_timer __P((void));
793void ppp_stop_public_nat_port_mapping_timer __P((void));
794void ppp_block_public_nat_port_mapping_timer __P((void));
795void ppp_unblock_public_nat_port_mapping_timer __P((void));
796#endif /* __APPLE__ */
797
798/* Procedures exported from auth.c */
799void link_required __P((int));	  /* we are starting to use the link */
800void link_terminated __P((int));  /* we are finished with the link */
801void link_down __P((int));	  /* the LCP layer has left the Opened state */
802void link_established __P((int)); /* the link is up; authenticate now */
803void start_networks __P((int));   /* start all the network control protos */
804void continue_networks __P((int)); /* start network [ip, etc] control protos */
805void np_up __P((int, int));	  /* a network protocol has come up */
806void np_down __P((int, int));	  /* a network protocol has gone down */
807void np_finished __P((int, int)); /* a network protocol no longer needs link */
808void auth_peer_fail __P((int, int));
809				/* peer failed to authenticate itself */
810void auth_peer_success __P((int, int, int, u_char *, int));
811				/* peer successfully authenticated itself */
812void auth_withpeer_fail __P((int, int));
813				/* we failed to authenticate ourselves */
814#ifdef __APPLE__
815int unexpected_network_packet __P((int, int));
816void auth_withpeer_cancelled __P((int, int));
817				/* authentication cancelled by user */
818#endif
819void auth_withpeer_success __P((int, int, int));
820				/* we successfully authenticated ourselves */
821void auth_check_options __P((void));
822				/* check authentication options supplied */
823void auth_reset __P((int));	/* check what secrets we have */
824int  check_passwd __P((int, u_char *, int, u_char *, int, char **));
825				/* Check peer-supplied username/password */
826int  get_secret __P((int, u_char *, u_char *, u_char *, int *, int));
827				/* get "secret" for chap */
828int  get_srp_secret __P((int unit, char *client, char *server, char *secret,
829    int am_server));
830int  auth_ip_addr __P((int, u_int32_t));
831				/* check if IP address is authorized */
832int  auth_number __P((void));	/* check if remote number is authorized */
833int  bad_ip_adrs __P((u_int32_t));
834				/* check if IP address is unreasonable */
835#ifdef __APPLE__
836void check_idle __P((void *));
837#endif
838
839/* Procedures exported from demand.c */
840void demand_conf __P((void));	/* config interface(s) for demand-dial */
841void demand_block __P((void));	/* set all NPs to queue up packets */
842void demand_unblock __P((void)); /* set all NPs to pass packets */
843void demand_discard __P((void)); /* set all NPs to discard packets */
844void demand_rexmit __P((int));	/* retransmit saved frames for an NP */
845int  loop_chars __P((unsigned char *, int)); /* process chars from loopback */
846int  loop_frame __P((unsigned char *, int)); /* should we bring link up? */
847
848/* Procedures exported from multilink.c */
849void mp_check_options __P((void)); /* Check multilink-related options */
850int  mp_join_bundle __P((void));  /* join our link to an appropriate bundle */
851char *epdisc_to_str __P((struct epdisc *)); /* string from endpoint discrim. */
852int  str_to_epdisc __P((struct epdisc *, char *)); /* endpt disc. from str */
853
854/* Procedures exported from sys-*.c */
855void sys_init __P((void));	/* Do system-dependent initialization */
856void sys_cleanup __P((void));	/* Restore system state before exiting */
857int  sys_check_options __P((void)); /* Check options specified */
858void sys_close __P((void));	/* Clean up in a child before execing */
859int  ppp_available __P((void));	/* Test whether ppp kernel support exists */
860int  get_pty __P((int *, int *, char *, int));	/* Get pty master/slave */
861int  open_ppp_loopback __P((void)); /* Open loopback for demand-dialling */
862int  tty_establish_ppp __P((int));  /* Turn serial port into a ppp interface */
863void tty_disestablish_ppp __P((int)); /* Restore port to normal operation */
864void generic_disestablish_ppp __P((int dev_fd)); /* Restore device setting */
865int  generic_establish_ppp __P((int dev_fd, UInt8 *delegate)); /* Make a ppp interface */
866void make_new_bundle __P((int, int, int, int)); /* Create new bundle */
867int  bundle_attach __P((int));	/* Attach link to existing bundle */
868void cfg_bundle __P((int, int, int, int)); /* Configure existing bundle */
869void clean_check __P((void));	/* Check if line was 8-bit clean */
870void set_up_tty __P((int, int)); /* Set up port's speed, parameters, etc. */
871void restore_tty __P((int));	/* Restore port's original parameters */
872void setdtr __P((int, int));	/* Raise or lower port's DTR line */
873void output __P((int, u_char *, int)); /* Output a PPP packet */
874void wait_input __P((struct timeval *));
875				/* Wait for input, with timeout */
876void add_fd __P((int));		/* Add fd to set to wait for */
877void remove_fd __P((int));	/* Remove fd from set to wait for */
878#ifdef __APPLE__
879void sys_runloop __P((void));	/* Do system-dependent runloop action */
880int save_new_password(); /* save new password to the keychain */
881void sys_statusnotify(); /* send status notification to the controller */
882void sys_reinit();			/* reinit after pid has changed */
883void sys_install_options(void);		/* install system specific options, before sys_init */
884int sys_check_controller(void);
885int sys_setup_security_session(void);
886int sys_loadplugin(char *arg);
887void sys_publish_remoteaddress(char *addr);
888int getabsolutetime(struct timeval *timenow);
889bool is_ready_fd(int fd);	/* check if fd is ready (out of select) */
890void set_up_tty_local __P((int, int)); /* Set up port's 'local' parameters only. */
891void ppp_hold __P((int unit));	/* stop ppp traffic on this link */
892void ppp_cont __P((int unit));	/* resume ppp traffic on this link */
893void auth_hold(int unit);
894void auth_cont(int unit);
895void option_change_idle();
896void set_server_peer(struct in_addr ppp_server); /* set the remote server peer address */
897void set_network_signature(char *, char *, char *, char *); /* set the network signature */
898int wait_input_fd(int fd, int delay);
899void closeallfrom(int from);
900void options_close __P((void));	/* close options stuff */
901void sys_install(void);
902void sys_uninstall(void);
903#ifdef INET6
904int ether_to_eui64(eui64_t *p_eui64);
905#endif
906void generic_send_config __P((int, u_int32_t, int, int));
907				/* Configure i/f transmit parameters */
908void generic_recv_config __P((int, u_int32_t, int, int));
909				/* Configure i/f receive parameters */
910void sys_log(int priority, const char *message, ...) __attribute__((format(__printf__, 2, 0)));
911#else
912#define sys_log syslog
913#endif
914int  read_packet __P((u_char *)); /* Read PPP packet */
915int  get_loop_output __P((void)); /* Read pkts from loopback */
916void tty_send_config __P((int, u_int32_t, int, int));
917				/* Configure i/f transmit parameters */
918void tty_set_xaccm __P((ext_accm));
919				/* Set extended transmit ACCM */
920void tty_recv_config __P((int, u_int32_t, int, int));
921				/* Configure i/f receive parameters */
922int  ccp_test __P((int, u_char *, int, int));
923				/* Test support for compression scheme */
924void ccp_flags_set __P((int, int, int));
925				/* Set kernel CCP state */
926int  ccp_fatal_error __P((int)); /* Test for fatal decomp error in kernel */
927int  get_idle_time __P((int, struct ppp_idle *));
928				/* Find out how long link has been idle */
929int  get_ppp_stats __P((int, struct pppd_stats *));
930				/* Return link statistics */
931void netif_set_mtu __P((int, int)); /* Set PPP interface MTU */
932int  netif_get_mtu __P((int));      /* Get PPP interface MTU */
933int  sifvjcomp __P((int, int, int, int));
934				/* Configure VJ TCP header compression */
935int  sifup __P((int));		/* Configure i/f up for one protocol */
936int  sifnpmode __P((int u, int proto, enum NPmode mode));
937				/* Set mode for handling packets for proto */
938int  sifdown __P((int));	/* Configure i/f down for one protocol */
939int  sifaddr __P((int, u_int32_t, u_int32_t, u_int32_t));
940				/* Configure IPv4 addresses for i/f */
941int  uifaddr __P((int, u_int32_t, u_int32_t, u_int32_t));
942				/* Update IPv4 addresses for i/f */
943int  cifaddr __P((int, u_int32_t, u_int32_t));
944				/* Reset i/f IP addresses */
945#ifdef INET6
946int  sif6addr __P((int, eui64_t, eui64_t));
947				/* Configure IPv6 addresses for i/f */
948int  cif6addr __P((int, eui64_t, eui64_t));
949				/* Remove an IPv6 address from i/f */
950#endif
951#ifdef __APPLE__
952int sifroute __P((int, u_int32_t, u_int32_t, u_int32_t));
953				/* set the route for the interface */
954int cifroute __P((void));
955				/* clear the route for the interface */
956int  sifnpafmode __P((int u, int proto, enum NPAFmode mode));
957				/* Set mode for filtering addresses for proto */
958int sifdns(u_int32_t dns1, u_int32_t dns2);
959int sifwins(u_int32_t wins1, u_int32_t wins2);
960#endif
961int  sifdefaultroute __P((int, u_int32_t, u_int32_t));
962				/* Create default route through i/f */
963int  cifdefaultroute __P((int, u_int32_t, u_int32_t));
964				/* Delete default route through i/f */
965int  sifproxyarp __P((int, u_int32_t));
966				/* Add proxy ARP entry for peer */
967int  cifproxyarp __P((int, u_int32_t));
968				/* Delete proxy ARP entry for peer */
969u_int32_t GetMask __P((u_int32_t)); /* Get appropriate netmask for address */
970int  lock __P((char *));	/* Create lock file for device */
971int  relock __P((int));		/* Rewrite lock file with new pid */
972void unlock __P((void));	/* Delete previously-created lock file */
973void logwtmp __P((const char *, const char *, const char *));
974				/* Write entry to wtmp file */
975int  get_host_seed __P((void));	/* Get host-dependent random number seed */
976int  have_route_to __P((u_int32_t)); /* Check if route to addr exists */
977#ifdef PPP_FILTER
978int  set_filters __P((struct bpf_program *pass, struct bpf_program *active));
979				/* Set filter programs in kernel */
980#endif
981#ifdef IPX_CHANGE
982int  sipxfaddr __P((int, unsigned long, unsigned char *));
983int  cipxfaddr __P((int));
984#endif
985int  get_if_hwaddr __P((u_char *addr, char *name));
986char *get_first_ethernet __P((void));
987
988/* Procedures exported from options.c */
989int setipaddr __P((char *, char **, int)); /* Set local/remote ip addresses */
990int  parse_args __P((int argc, char **argv));
991				/* Parse options from arguments given */
992#ifdef __APPLE__
993int options_from_controller __P(());
994#endif
995int  options_from_file __P((char *filename, int must_exist, int check_prot,
996			    int privileged));
997				/* Parse options from an options file */
998int  options_from_user __P((void)); /* Parse options from user's .ppprc */
999int  options_for_tty __P((void)); /* Parse options from /etc/ppp/options.tty */
1000int  options_from_list __P((struct wordlist *, int privileged));
1001				/* Parse options from a wordlist */
1002int  getword __P((FILE *f, char *word, int *newlinep, char *filename));
1003				/* Read a word from a file */
1004void option_error __P((char *fmt, ...));
1005				/* Print an error message about an option */
1006int int_option __P((char *, int *));
1007				/* Simplified number_option for decimal ints */
1008void add_options __P((option_t *)); /* Add extra options */
1009void check_options __P((void));	/* check values after all options parsed */
1010int  override_value __P((const char *, int, const char *));
1011				/* override value if permitted by priority */
1012void print_options __P((void (*) __P((void *, char *, ...)), void *));
1013				/* print out values of all options */
1014
1015int parse_dotted_ip __P((char *, u_int32_t *));
1016
1017/*
1018 * Hooks to enable plugins to change various things.
1019 */
1020extern int (*new_phase_hook) __P((int));
1021extern int (*idle_time_hook) __P((struct ppp_idle *));
1022extern int (*holdoff_hook) __P((void));
1023extern int (*pap_check_hook) __P((void));
1024extern int (*pap_auth_hook) __P((char *user, char *passwd, char **msgp,
1025				 struct wordlist **paddrs,
1026				 struct wordlist **popts));
1027extern void (*pap_logout_hook) __P((void));
1028extern int (*pap_passwd_hook) __P((char *user, char *passwd));
1029extern int (*allowed_address_hook) __P((u_int32_t addr));
1030extern void (*ip_up_hook) __P((void));
1031extern void (*ip_down_hook) __P((void));
1032extern void (*ip_choose_hook) __P((u_int32_t *));
1033#ifdef __APPLE__
1034extern void (*ipdata_input_hook) __P((int, u_char *, int, u_int32_t, u_int32_t));
1035extern void (*ipdata_up_hook) __P((int, u_int32_t, u_int32_t));
1036extern void (*ipdata_down_hook) __P((int));
1037extern int (*ipdata_print_hook) __P((u_char *, int, void (*) __P((void *, char *, ...)), void *));
1038#endif
1039
1040extern int (*chap_check_hook) __P((void));
1041extern int (*chap_passwd_hook) __P((char *user, char *passwd));
1042
1043/* Let a plugin snoop sent and received packets.  Useful for L2TP */
1044extern void (*snoop_recv_hook) __P((unsigned char *p, int len));
1045extern void (*snoop_send_hook) __P((unsigned char *p, int len));
1046
1047#ifdef __APPLE__
1048/* Hook for access control list to verify if user should have access */
1049extern int (*acl_hook) __P((u_char *user, int len));
1050#endif
1051
1052
1053/*
1054 * Inline versions of get/put char/short/long.
1055 * Pointer is advanced; we assume that both arguments
1056 * are lvalues and will already be in registers.
1057 * cp MUST be u_char *.
1058 */
1059#define GETCHAR(c, cp) { \
1060	(c) = *(cp)++; \
1061}
1062#define PUTCHAR(c, cp) { \
1063	*(cp)++ = (u_char) (c); \
1064}
1065
1066
1067#define GETSHORT(s, cp) { \
1068	(s) = *(cp)++ << 8; \
1069	(s) |= *(cp)++; \
1070}
1071#define PUTSHORT(s, cp) { \
1072	*(cp)++ = (u_char) ((s) >> 8); \
1073	*(cp)++ = (u_char) (s); \
1074}
1075
1076#define GETLONG(l, cp) { \
1077	(l) = *(cp)++ << 8; \
1078	(l) |= *(cp)++; (l) <<= 8; \
1079	(l) |= *(cp)++; (l) <<= 8; \
1080	(l) |= *(cp)++; \
1081}
1082#define PUTLONG(l, cp) { \
1083	*(cp)++ = (u_char) ((l) >> 24); \
1084	*(cp)++ = (u_char) ((l) >> 16); \
1085	*(cp)++ = (u_char) ((l) >> 8); \
1086	*(cp)++ = (u_char) (l); \
1087}
1088
1089#define INCPTR(n, cp)	((cp) += (n))
1090#define DECPTR(n, cp)	((cp) -= (n))
1091
1092/*
1093 * System dependent definitions for user-level 4.3BSD UNIX implementation.
1094 */
1095
1096#define TIMEOUT(r, f, t)	timeout((r), (f), (t), 0)
1097#define UNTIMEOUT(r, f)		untimeout((r), (f))
1098
1099#define BCOPY(s, d, l)		memcpy(d, s, l)
1100#define BZERO(s, n)		memset(s, 0, n)
1101#define	BCMP(s1, s2, l)		memcmp(s1, s2, l)
1102
1103#define PRINTMSG(m, l)		{ info("Remote message: %0.*v", l, m); }
1104
1105/*
1106 * MAKEHEADER - Add Header fields to a packet.
1107 */
1108#define MAKEHEADER(p, t) { \
1109    PUTCHAR(PPP_ALLSTATIONS, p); \
1110    PUTCHAR(PPP_UI, p); \
1111    PUTSHORT(t, p); }
1112
1113/*
1114 * Exit status values.
1115 */
1116#define EXIT_OK			0
1117#define EXIT_FATAL_ERROR	1
1118#define EXIT_OPTION_ERROR	2
1119#define EXIT_NOT_ROOT		3
1120#define EXIT_NO_KERNEL_SUPPORT	4
1121#define EXIT_USER_REQUEST	5
1122#define EXIT_LOCK_FAILED	6
1123#define EXIT_OPEN_FAILED	7
1124#define EXIT_CONNECT_FAILED	8
1125#define EXIT_PTYCMD_FAILED	9
1126#define EXIT_NEGOTIATION_FAILED	10
1127#define EXIT_PEER_AUTH_FAILED	11
1128#define EXIT_IDLE_TIMEOUT	12
1129#define EXIT_CONNECT_TIME	13
1130#define EXIT_CALLBACK		14
1131#define EXIT_PEER_DEAD		15
1132#define EXIT_HANGUP		16
1133#define EXIT_LOOPBACK		17
1134#define EXIT_INIT_FAILED	18
1135#define EXIT_AUTH_TOPEER_FAILED	19
1136#ifdef __APPLE__
1137#define EXIT_TERMINAL_FAILED	20
1138#define EXIT_DEVICE_ERROR	21
1139#endif
1140#ifdef MAXOCTETS
1141#ifdef __APPLE__
1142#define EXIT_TRAFFIC_LIMIT	22
1143#else
1144#define EXIT_TRAFFIC_LIMIT	20
1145#define EXIT_CNID_AUTH_FAILED	21
1146#endif
1147#endif
1148#ifdef __APPLE__
1149#define EXIT_PEER_NOT_AUTHORIZED	23
1150#define EXIT_CNID_AUTH_FAILED	24
1151#define EXIT_PEER_UNREACHABLE 25
1152#endif
1153
1154/*
1155 * Debug macros.  Slightly useful for finding bugs in pppd, not particularly
1156 * useful for finding out why your connection isn't being established.
1157 */
1158#ifdef DEBUGALL
1159#define DEBUGMAIN	1
1160#define DEBUGFSM	1
1161#define DEBUGLCP	1
1162#define DEBUGIPCP	1
1163#define DEBUGIPV6CP	1
1164#define DEBUGACSCP	1
1165#define DEBUGUPAP	1
1166#define DEBUGCHAP	1
1167#define DEBUGEAP	1
1168#endif
1169
1170#ifdef __APPLE__
1171#ifndef LOG_PPP
1172#define LOG_PPP LOG_RAS
1173#endif
1174#endif
1175
1176#ifndef LOG_PPP			/* we use LOG_LOCAL2 for syslog by default */
1177#if defined(DEBUGMAIN) || defined(DEBUGFSM) || defined(DEBUGSYS) \
1178  || defined(DEBUGLCP) || defined(DEBUGIPCP) || defined(DEBUGUPAP) \
1179  || defined(DEBUGCHAP) || defined(DEBUG) || defined(DEBUGIPV6CP) \
1180  || defined(DEBUGEAP) || defined(DEBUGACSCP)
1181#define LOG_PPP LOG_LOCAL2
1182#else
1183#define LOG_PPP LOG_DAEMON
1184#endif
1185#endif /* LOG_PPP */
1186
1187#ifdef DEBUGMAIN
1188#define MAINDEBUG(x)	if (debug) dbglog x
1189#else
1190#define MAINDEBUG(x)
1191#endif
1192
1193#ifdef DEBUGSYS
1194#define SYSDEBUG(x)	if (debug) dbglog x
1195#else
1196#define SYSDEBUG(x)
1197#endif
1198
1199#ifdef DEBUGFSM
1200#define FSMDEBUG(x)	if (debug) dbglog x
1201#else
1202#define FSMDEBUG(x)
1203#endif
1204
1205#ifdef DEBUGLCP
1206#define LCPDEBUG(x)	if (debug) dbglog x
1207#else
1208#define LCPDEBUG(x)
1209#endif
1210
1211#ifdef DEBUGIPCP
1212#define IPCPDEBUG(x)	if (debug) dbglog x
1213#else
1214#define IPCPDEBUG(x)
1215#endif
1216
1217#ifdef DEBUGACSCP
1218#define ACSCPDEBUG(x)	if (debug) dbglog x
1219#else
1220#define ACSCPDEBUG(x)
1221#endif
1222
1223#ifdef DEBUGIPV6CP
1224#define IPV6CPDEBUG(x)  if (debug) dbglog x
1225#else
1226#define IPV6CPDEBUG(x)
1227#endif
1228
1229#ifdef DEBUGUPAP
1230#define UPAPDEBUG(x)	if (debug) dbglog x
1231#else
1232#define UPAPDEBUG(x)
1233#endif
1234
1235#ifdef DEBUGCHAP
1236#define CHAPDEBUG(x)	if (debug) dbglog x
1237#else
1238#define CHAPDEBUG(x)
1239#endif
1240
1241#ifdef DEBUGEAP
1242#define EAPDEBUG(x)	if (debug) dbglog x
1243#else
1244#define EAPDEBUG(x)
1245#endif
1246
1247#ifdef DEBUGIPXCP
1248#define IPXCPDEBUG(x)	if (debug) dbglog x
1249#else
1250#define IPXCPDEBUG(x)
1251#endif
1252
1253#ifndef SIGTYPE
1254#if defined(sun) || defined(SYSV) || defined(POSIX_SOURCE)
1255#define SIGTYPE void
1256#else
1257#define SIGTYPE int
1258#endif /* defined(sun) || defined(SYSV) || defined(POSIX_SOURCE) */
1259#endif /* SIGTYPE */
1260
1261#ifndef MIN
1262#define MIN(a, b)	((a) < (b)? (a): (b))
1263#endif
1264#ifndef MAX
1265#define MAX(a, b)	((a) > (b)? (a): (b))
1266#endif
1267
1268#ifndef offsetof
1269#define offsetof(type, member) ((size_t) &((type *)0)->member)
1270#endif
1271
1272#ifdef __APPLE__
1273/* Reachability macros... should ideally be in a SystemConfiguration header file. */
1274
1275/* if the connection is reachable as-is (e.g. via ppp, airport, or ethernet). */
1276#define REACHABLE_NOW ((flags & kSCNetworkReachabilityFlagsReachable) && \
1277                        ! ((flags & kSCNetworkReachabilityFlagsTransientConnection) && \
1278                            (flags & kSCNetworkReachabilityFlagsConnectionRequired)))
1279
1280/*
1281 * if the connection is not currently reachable but will be when needed. i.e. It becomes reachable
1282 * automatically via a dialup-modem (using PPP).
1283 */
1284#define REACHABLE_AUTOMATICALLY_VIA_SCNC ((flags & kSCNetworkReachabilityFlagsReachable) && \
1285                                                (flags & kSCNetworkReachabilityFlagsTransientConnection) && \
1286                                                    (flags & kSCNetworkReachabilityFlagsConnectionRequired) && \
1287                                                        (flags & kSCNetworkReachabilityFlagsConnectionAutomatic))
1288
1289/*
1290 * if the connection is not currently reachable but will be when needed. i.e. It becomes reachable
1291 * automatically via the iphone EDGE (using PPP).
1292 */
1293#if TARGET_OS_EMBEDDED
1294/* currently works for iphone build only (because kSCNetworkReachabilityFlagsIsWWAN is defined). */
1295#define REACHABLE_AUTOMATICALLY_VIA_WWAN ((flags & kSCNetworkReachabilityFlagsReachable) && \
1296                                            (flags & kSCNetworkReachabilityFlagsTransientConnection) && \
1297                                                (flags & kSCNetworkReachabilityFlagsConnectionRequired) && \
1298                                                    (flags & kSCNetworkReachabilityFlagsIsWWAN))
1299#else
1300/* currently doesn't work for non-iphone builds (because kSCNetworkReachabilityFlagsIsWWAN is undefined). */
1301#define REACHABLE_AUTOMATICALLY_VIA_WWAN 0
1302#endif /* TARGET_OS_EMBEDDED */
1303
1304/*
1305 * if connection is automatic without user intervention. currently two cases (see the macros above);
1306 * the dialup modem types, and the iphone EDGE.
1307 */
1308#define REACHABLE_AUTOMATICALLY_WITHOUT_USER (!(flags & kSCNetworkReachabilityFlagsInterventionRequired) && \
1309					      (REACHABLE_AUTOMATICALLY_VIA_SCNC || REACHABLE_AUTOMATICALLY_VIA_WWAN))
1310
1311#define PPPD_WWAN_INTERFACE_TIMEOUT 40 // give 40 seconds for cell interface to come up <rdar://problem/7941304>
1312
1313/* try to handle as many types of dns delimiters as possible */
1314#define GET_SPLITDNS_DELIM(data, delim) do {	\
1315		if (strstr(data, ",")) {				\
1316			delim = ",";						\
1317		} else if (strstr(data, ";")) {			\
1318			delim = ";";						\
1319		} else if (strstr(data, "\n")) {		\
1320			delim = "\n";						\
1321		} else if (strstr(data, "\r")) {		\
1322			delim = "\r";						\
1323		} else if (strstr(data, " ")) {			\
1324			delim = " ";						\
1325		} else {								\
1326			delim = "\0";						\
1327		}										\
1328	} while(0)
1329
1330/* Wcast-align fix - cast away alignment warning when buffer is aligned */
1331#define ALIGNED_CAST(type)	(type)(void *)
1332
1333/* Define keys for AirPort Setup and State interface dictionaries */
1334#define SC_AIRPORT_POWERENABLED_KEY		CFSTR("PowerEnabled")
1335#define SC_AIRPORT_POWERSTATUS_KEY		CFSTR("Power Status")
1336
1337#endif /* __APPLE__ */
1338
1339#endif /* __PPP_H__ */
1340