]> git.ozlabs.org Git - ppp.git/blob - pppd/sys-linux.c
add log level arg for log_packet
[ppp.git] / pppd / sys-linux.c
1 /*
2  * sys-linux.c - System-dependent procedures for setting up
3  * PPP interfaces on Linux systems
4  *
5  * Copyright (c) 1989 Carnegie Mellon University.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms are permitted
9  * provided that the above copyright notice and this paragraph are
10  * duplicated in all such forms and that any documentation,
11  * advertising materials, and other materials related to such
12  * distribution and use acknowledge that the software was developed
13  * by Carnegie Mellon University.  The name of the
14  * University may not be used to endorse or promote products derived
15  * from this software without specific prior written permission.
16  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19  */
20
21 #include <sys/ioctl.h>
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <sys/time.h>
25 #include <sys/errno.h>
26 #include <sys/file.h>
27 #include <sys/stat.h>
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <syslog.h>
32 #include <string.h>
33 #include <time.h>
34 #include <memory.h>
35 #include <utmp.h>
36 #include <mntent.h>
37 #include <signal.h>
38 #include <fcntl.h>
39 #include <ctype.h>
40
41 /* This is in netdevice.h. However, this compile will fail miserably if
42    you attempt to include netdevice.h because it has so many references
43    to __memcpy functions which it should not attempt to do. So, since I
44    really don't use it, but it must be defined, define it now. */
45
46 #ifndef MAX_ADDR_LEN
47 #define MAX_ADDR_LEN 7
48 #endif
49
50 #include <linux/version.h>
51 #include <net/if.h>
52 #include <linux/ppp_defs.h>
53 #include <net/if_arp.h>
54 #include <linux/if_ppp.h>
55 #include <net/route.h>
56 #include <linux/if_ether.h>
57 #include <netinet/in.h>
58
59 #include "pppd.h"
60 #include "fsm.h"
61 #include "ipcp.h"
62
63 #ifndef RTF_DEFAULT  /* Normally in <linux/route.h> from <net/route.h> */
64 #define RTF_DEFAULT  0
65 #endif
66
67 #ifdef IPX_CHANGE
68 #include "ipxcp.h"
69 #endif
70
71 #ifdef LOCKLIB
72 #include <sys/locks.h>
73 #endif
74
75 #define ok_error(num) ((num)==EIO)
76
77 static int tty_disc = N_TTY;    /* The TTY discipline */
78 static int ppp_disc = N_PPP;    /* The PPP discpline */
79 static int initfdflags = -1;    /* Initial file descriptor flags for fd */
80 static int ppp_fd = -1;         /* fd which is set to PPP discipline */
81 static int sock_fd = -1;        /* socket for doing interface ioctls */
82 static int slave_fd = -1;
83 static int master_fd = -1;
84
85 static int has_proxy_arp       = 0;
86 static int driver_version      = 0;
87 static int driver_modification = 0;
88 static int driver_patch        = 0;
89 static int restore_term        = 0;     /* 1 => we've munged the terminal */
90 static struct termios inittermios;      /* Initial TTY termios */
91
92 static char loop_name[20];
93 static unsigned char inbuf[512]; /* buffer for chars read from loopback */
94
95 static int      if_is_up;       /* Interface has been marked up */
96 static u_int32_t default_route_gateway; /* Gateway for default route added */
97 static u_int32_t proxy_arp_addr;        /* Addr for proxy arp entry added */
98
99 static char *lock_file;
100
101 #define MAX_IFS         100
102
103 #define FLAGS_GOOD (IFF_UP          | IFF_BROADCAST)
104 #define FLAGS_MASK (IFF_UP          | IFF_BROADCAST | \
105                     IFF_POINTOPOINT | IFF_LOOPBACK  | IFF_NOARP)
106
107 /* Prototypes for procedures local to this file. */
108 static int get_flags (void);
109 static void set_flags (int flags);
110 static int translate_speed (int bps);
111 static int baud_rate_of (int speed);
112 static char *path_to_route (void);
113 static void close_route_table (void);
114 static int open_route_table (void);
115 static int read_route_table (struct rtentry *rt);
116 static int defaultroute_exists (struct rtentry *rt);
117 static int get_ether_addr (u_int32_t ipaddr, struct sockaddr *hwaddr,
118                            char *name);
119 static void decode_version (char *buf, int *version, int *mod, int *patch);
120
121 extern u_char   inpacket_buf[]; /* borrowed from main.c */
122
123 /*
124  * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
125  * if it exists.
126  */
127
128 #define SET_SA_FAMILY(addr, family)                     \
129     memset ((char *) &(addr), '\0', sizeof(addr));      \
130     addr.sa_family = (family);
131
132 /*
133  * Determine if the PPP connection should still be present.
134  */
135
136 extern int hungup;
137 #define still_ppp() (hungup == 0)
138
139 #ifndef LOCK_PREFIX
140 #define LOCK_PREFIX     "/var/lock/LCK.."
141 #endif
142
143 /********************************************************************
144  *
145  * Functions to read and set the flags value in the device driver
146  */
147
148 static void set_ppp_fd (int new_fd)
149   {    
150     SYSDEBUG ((LOG_DEBUG, "setting ppp_fd to %d\n", ppp_fd));
151     ppp_fd = new_fd;
152   }
153
154 /********************************************************************
155  *
156  * Functions to read and set the flags value in the device driver
157  */
158
159 static int get_flags (void)
160   {    
161     int flags;
162
163     if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &flags) < 0)
164       {
165         if ( ok_error (errno) )
166           {
167             flags = 0;
168           }
169         else
170           {
171             syslog(LOG_ERR, "ioctl(PPPIOCGFLAGS): %m");
172             quit();
173           }
174       }
175
176     SYSDEBUG ((LOG_DEBUG, "get flags = %x\n", flags));
177     return flags;
178   }
179
180 /********************************************************************/
181
182 static void set_flags (int flags)
183   {    
184     SYSDEBUG ((LOG_DEBUG, "set flags = %x\n", flags));
185
186     if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &flags) < 0)
187       {
188         if (! ok_error (errno) )
189           {
190             syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS, %x): %m(%d)", flags, errno);
191             quit();
192           }
193       }
194   }
195
196 /********************************************************************
197  *
198  * sys_init - System-dependent initialization.
199  */
200
201 void sys_init(void)
202   {
203     openlog("pppd", LOG_PID | LOG_NDELAY, LOG_PPP);
204     setlogmask(LOG_UPTO(LOG_INFO));
205     if (debug)
206       {
207         setlogmask(LOG_UPTO(LOG_DEBUG));
208       }
209     
210     /* Get an internet socket for doing socket ioctls. */
211     sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
212     if (sock_fd < 0)
213       {
214         if ( ! ok_error ( errno ))
215           {
216             syslog(LOG_ERR, "Couldn't create IP socket: %m(%d)", errno);
217             die(1);
218           }
219       }
220   }
221
222 /********************************************************************
223  *
224  * sys_cleanup - restore any system state we modified before exiting:
225  * mark the interface down, delete default route and/or proxy arp entry.
226  * This should call die() because it's called from die().
227  */
228
229 void sys_cleanup(void)
230   {
231     struct ifreq ifr;
232 /*
233  * Take down the device
234  */
235     if (if_is_up)
236       {
237         sifdown(0);
238       }
239 /*
240  * Delete any routes through the device.
241  */
242     if (default_route_gateway != 0)
243       {
244         cifdefaultroute(0, 0, default_route_gateway);
245       }
246
247     if (has_proxy_arp)
248       {
249         cifproxyarp(0, proxy_arp_addr);
250       }
251   }
252
253 /********************************************************************
254  *
255  * sys_close - Clean up in a child process before execing.
256  */
257 void
258 sys_close(void)
259   {
260     close(sock_fd);
261     sock_fd = -1;
262     closelog();
263   }
264
265 /********************************************************************
266  *
267  * note_debug_level - note a change in the debug level.
268  */
269
270 void note_debug_level (void)
271   {
272     if (debug)
273       {
274         SYSDEBUG ((LOG_INFO, "Debug turned ON, Level %d", debug));
275         setlogmask(LOG_UPTO(LOG_DEBUG));
276       }
277     else
278       {
279         setlogmask(LOG_UPTO(LOG_WARNING));
280       }
281   }
282
283 /********************************************************************
284  *
285  * set_kdebugflag - Define the debugging level for the kernel
286  */
287
288 int set_kdebugflag (int requested_level)
289   {
290     if (ioctl(ppp_fd, PPPIOCSDEBUG, &requested_level) < 0)
291       {
292         if ( ! ok_error (errno) )
293           {
294             syslog (LOG_ERR, "ioctl(PPPIOCSDEBUG): %m");
295           }
296         return (0);
297       }
298     SYSDEBUG ((LOG_INFO, "set kernel debugging level to %d",
299                 requested_level));
300     return (1);
301   }
302
303 /********************************************************************
304  *
305  * establish_ppp - Turn the serial port into a ppp interface.
306  */
307
308 void establish_ppp (int tty_fd)
309   {
310     int x;
311 /*
312  * The current PPP device will be the tty file.
313  */
314     set_ppp_fd (tty_fd);
315 /*
316  * Ensure that the tty device is in exclusive mode.
317  */
318     if (ioctl(tty_fd, TIOCEXCL, 0) < 0)
319       {
320         if ( ! ok_error ( errno ))
321           {
322             syslog (LOG_WARNING, "ioctl(TIOCEXCL): %m");
323           }
324       }
325 /*
326  * Demand mode - prime the old ppp device to relinquish the unit.
327  */
328     if (demand && ioctl(slave_fd, PPPIOCXFERUNIT, 0) < 0)
329       {
330         syslog(LOG_ERR, "ioctl(transfer ppp unit): %m(%d)", errno);
331         die(1);
332       }
333 /*
334  * Set the current tty to the PPP discpline
335  */
336     if (ioctl(ppp_fd, TIOCSETD, &ppp_disc) < 0)
337       {
338         if ( ! ok_error (errno) )
339           {
340             syslog(LOG_ERR, "ioctl(TIOCSETD): %m(%d)", errno);
341             die(1);
342           }
343       }
344 /*
345  * Find out which interface we were given.
346  */
347     if (ioctl(ppp_fd, PPPIOCGUNIT, &x) < 0)
348       { 
349         if ( ! ok_error (errno))
350           {
351             syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m(%d)", errno);
352             die(1);
353           }
354       }
355 /*
356  * Check that we got the same unit again.
357  */
358     if (demand)
359       {
360         if (x != ifunit)
361           {
362             syslog(LOG_ERR, "transfer_ppp failed: wanted unit %d, got %d",
363                    ifunit, x);
364             die(1);
365           }
366       }
367
368     ifunit = x;
369 /*
370  * Enable debug in the driver if requested.
371  */
372     if (!demand)
373       set_kdebugflag (kdebugflag);
374
375     set_flags (get_flags() & ~(SC_RCV_B7_0 | SC_RCV_B7_1 |
376                                SC_RCV_EVNP | SC_RCV_ODDP));
377
378     SYSDEBUG ((LOG_NOTICE, "Using version %d.%d.%d of PPP driver",
379             driver_version, driver_modification, driver_patch));
380 /*
381  * Fetch the initial file flags and reset blocking mode on the file.
382  */
383     initfdflags = fcntl(ppp_fd, F_GETFL);
384
385     if (initfdflags == -1 ||
386         fcntl(ppp_fd, F_SETFL, initfdflags | O_NONBLOCK) == -1)
387       {
388         if ( ! ok_error (errno))
389           {
390             syslog(LOG_WARNING,
391                    "Couldn't set device to non-blocking mode: %m");
392           }
393       }
394   }
395
396 /********************************************************************
397  *
398  * disestablish_ppp - Restore the serial port to normal operation.
399  * This shouldn't call die() because it's called from die().
400  */
401
402 void disestablish_ppp(int tty_fd)
403   {
404     int x;
405     char *s;
406
407 /*
408  * Attempt to restore the previous tty settings
409  */
410     if (!hungup)
411       {
412 /*
413  * Restore the previous line discipline
414  */
415         if (ioctl(tty_fd, TIOCSETD, &tty_disc) < 0)
416           {
417             if ( ! ok_error (errno))
418               {
419                 syslog(LOG_ERR, "ioctl(TIOCSETD, N_TTY): %m");
420               }
421           }
422         
423         if (ioctl(tty_fd, TIOCNXCL, 0) < 0)
424           {
425             if ( ! ok_error (errno))
426               {
427                 syslog (LOG_WARNING, "ioctl(TIOCNXCL): %m(%d)", errno);
428               }
429           }
430
431         /* Reset non-blocking mode on fd. */
432         if (initfdflags != -1 && fcntl(tty_fd, F_SETFL, initfdflags) < 0)
433           {
434             if ( ! ok_error (errno))
435               {
436                 syslog (LOG_WARNING,
437                         "Couldn't restore device fd flags: %m");
438               }
439           }
440       }
441     initfdflags = -1;
442   }
443
444 /********************************************************************
445  *
446  * clean_check - Fetch the flags for the device and generate
447  * appropriate error messages.
448  */
449 void clean_check(void)
450   {
451     int x;
452     char *s;
453
454     if (still_ppp())
455       {
456         if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) == 0)
457           {
458             s = NULL;
459             switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP))
460               {
461               case SC_RCV_B7_0:
462               case SC_RCV_B7_0 | SC_RCV_EVNP:
463               case SC_RCV_B7_0 | SC_RCV_ODDP:
464               case SC_RCV_B7_0 | SC_RCV_ODDP | SC_RCV_EVNP:
465                 s = "all had bit 7 set to 1";
466                 break;
467                 
468               case SC_RCV_B7_1:
469               case SC_RCV_B7_1 | SC_RCV_EVNP:
470               case SC_RCV_B7_1 | SC_RCV_ODDP:
471               case SC_RCV_B7_1 | SC_RCV_ODDP | SC_RCV_EVNP:
472                 s = "all had bit 7 set to 0";
473                 break;
474                 
475               case SC_RCV_EVNP:
476                 s = "all had odd parity";
477                 break;
478                 
479               case SC_RCV_ODDP:
480                 s = "all had even parity";
481                 break;
482               }
483             
484             if (s != NULL)
485               {
486                 syslog(LOG_WARNING, "Receive serial link is not"
487                        " 8-bit clean:");
488                 syslog(LOG_WARNING, "Problem: %s", s);
489               }
490           }
491       }
492   }
493         
494
495 /*
496  * List of valid speeds.
497  */
498
499 struct speed {
500     int speed_int, speed_val;
501 } speeds[] = {
502 #ifdef B50
503     { 50, B50 },
504 #endif
505 #ifdef B75
506     { 75, B75 },
507 #endif
508 #ifdef B110
509     { 110, B110 },
510 #endif
511 #ifdef B134
512     { 134, B134 },
513 #endif
514 #ifdef B150
515     { 150, B150 },
516 #endif
517 #ifdef B200
518     { 200, B200 },
519 #endif
520 #ifdef B300
521     { 300, B300 },
522 #endif
523 #ifdef B600
524     { 600, B600 },
525 #endif
526 #ifdef B1200
527     { 1200, B1200 },
528 #endif
529 #ifdef B1800
530     { 1800, B1800 },
531 #endif
532 #ifdef B2000
533     { 2000, B2000 },
534 #endif
535 #ifdef B2400
536     { 2400, B2400 },
537 #endif
538 #ifdef B3600
539     { 3600, B3600 },
540 #endif
541 #ifdef B4800
542     { 4800, B4800 },
543 #endif
544 #ifdef B7200
545     { 7200, B7200 },
546 #endif
547 #ifdef B9600
548     { 9600, B9600 },
549 #endif
550 #ifdef B19200
551     { 19200, B19200 },
552 #endif
553 #ifdef B38400
554     { 38400, B38400 },
555 #endif
556 #ifdef B57600
557     { 57600, B57600 },
558 #endif
559 #ifdef B115200
560     { 115200, B115200 },
561 #endif
562 #ifdef EXTA
563     { 19200, EXTA },
564 #endif
565 #ifdef EXTB
566     { 38400, EXTB },
567 #endif
568     { 0, 0 }
569 };
570
571 /********************************************************************
572  *
573  * Translate from bits/second to a speed_t.
574  */
575
576 static int translate_speed (int bps)
577   {
578     struct speed *speedp;
579
580     if (bps != 0)
581       {
582         for (speedp = speeds; speedp->speed_int; speedp++)
583           {
584             if (bps == speedp->speed_int)
585               {
586                 return speedp->speed_val;
587               }
588           }
589         syslog(LOG_WARNING, "speed %d not supported", bps);
590       }
591     return 0;
592   }
593
594 /********************************************************************
595  *
596  * Translate from a speed_t to bits/second.
597  */
598
599 static int baud_rate_of (int speed)
600   {
601     struct speed *speedp;
602     
603     if (speed != 0)
604       {
605         for (speedp = speeds; speedp->speed_int; speedp++)
606           {
607             if (speed == speedp->speed_val)
608               {
609                 return speedp->speed_int;
610               }
611           }
612       }
613     return 0;
614   }
615
616 /********************************************************************
617  *
618  * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
619  * at the requested speed, etc.  If `local' is true, set CLOCAL
620  * regardless of whether the modem option was specified.
621  */
622
623 void set_up_tty (int tty_fd, int local)
624   {
625     int speed, x;
626     struct termios tios;
627     
628     if (tcgetattr(tty_fd, &tios) < 0)
629       {
630         syslog(LOG_ERR, "tcgetattr: %m(%d)", errno);
631         die(1);
632       }
633     
634     if (!restore_term)
635       {
636         inittermios = tios;
637       }
638     
639     tios.c_cflag     &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
640     tios.c_cflag     |= CS8 | CREAD | HUPCL;
641
642     tios.c_iflag      = IGNBRK | IGNPAR;
643     tios.c_oflag      = 0;
644     tios.c_lflag      = 0;
645     tios.c_cc[VMIN]   = 1;
646     tios.c_cc[VTIME]  = 0;
647     
648     if (local || !modem)
649       {
650         tios.c_cflag ^= (CLOCAL | HUPCL);
651       }
652
653     switch (crtscts)
654       {
655     case 1:
656         tios.c_cflag |= CRTSCTS;
657         break;
658
659     case -2:
660         tios.c_iflag     |= IXON | IXOFF;
661         tios.c_cc[VSTOP]  = 0x13;       /* DC3 = XOFF = ^S */
662         tios.c_cc[VSTART] = 0x11;       /* DC1 = XON  = ^Q */
663         break;
664
665     case -1:
666         tios.c_cflag &= ~CRTSCTS;
667         break;
668
669     default:
670         break;
671       }
672     
673     speed = translate_speed(inspeed);
674     if (speed)
675       {
676         cfsetospeed (&tios, speed);
677         cfsetispeed (&tios, speed);
678       }
679 /*
680  * We can't proceed if the serial port speed is B0,
681  * since that implies that the serial port is disabled.
682  */
683     else
684       {
685         speed = cfgetospeed(&tios);
686         if (speed == B0)
687           {
688             syslog(LOG_ERR, "Baud rate for %s is 0; need explicit baud rate",
689                    devnam);
690             die (1);
691           }
692       }
693
694     if (tcsetattr(tty_fd, TCSAFLUSH, &tios) < 0)
695       {
696         syslog(LOG_ERR, "tcsetattr: %m");
697         die(1);
698       }
699     
700     baud_rate    = baud_rate_of(speed);
701     restore_term = TRUE;
702   }
703
704 /********************************************************************
705  *
706  * setdtr - control the DTR line on the serial port.
707  * This is called from die(), so it shouldn't call die().
708  */
709
710 void setdtr (int tty_fd, int on)
711   {
712     int modembits = TIOCM_DTR;
713
714     ioctl(tty_fd, (on ? TIOCMBIS : TIOCMBIC), &modembits);
715   }
716
717 /********************************************************************
718  *
719  * restore_tty - restore the terminal to the saved settings.
720  */
721
722 void restore_tty (int tty_fd)
723   {
724     if (restore_term)
725       {
726         restore_term = 0;
727 /*
728  * Turn off echoing, because otherwise we can get into
729  * a loop with the tty and the modem echoing to each other.
730  * We presume we are the sole user of this tty device, so
731  * when we close it, it will revert to its defaults anyway.
732  */
733         if (!default_device)
734           {
735             inittermios.c_lflag &= ~(ECHO | ECHONL);
736           }
737         
738         if (tcsetattr(tty_fd, TCSAFLUSH, &inittermios) < 0)
739           {
740             if (! ok_error (errno))
741               {
742                 syslog(LOG_WARNING, "tcsetattr: %m");
743               }
744           }
745       }
746   }
747
748 /********************************************************************
749  *
750  * output - Output PPP packet.
751  */
752
753 void output (int unit, unsigned char *p, int len)
754   {
755     if (debug)
756       {
757         log_packet(p, len, "sent ", LOG_DEBUG);
758       }
759     
760     if (write(ppp_fd, p, len) < 0)
761       {
762         if (errno == EWOULDBLOCK || errno == ENOBUFS
763             || errno == ENXIO || errno == EIO)
764           {
765             syslog(LOG_WARNING, "write: warning: %m(%d)", errno);
766           } 
767         else
768           {
769             syslog(LOG_ERR, "write: %m(%d)", errno);
770             die(1);
771           }
772       }
773   }
774
775 /********************************************************************
776  *
777  * wait_input - wait until there is data available on ppp_fd,
778  * for the length of time specified by *timo (indefinite
779  * if timo is NULL).
780  */
781
782 void wait_input (struct timeval *timo)
783   {
784     fd_set ready;
785     int n;
786     
787     FD_ZERO(&ready);
788     FD_SET(ppp_fd, &ready);
789
790     n = select(ppp_fd + 1, &ready, NULL, &ready, timo);
791     if (n < 0 && errno != EINTR)
792       {
793         syslog(LOG_ERR, "select: %m(%d)", errno);
794         die(1);
795       }
796   }
797
798 /********************************************************************
799  *
800  * wait_loop_output - wait until there is data available on the
801  * loopback, for the length of time specified by *timo (indefinite
802  * if timo is NULL).
803  */
804 void wait_loop_output(timo)
805     struct timeval *timo;
806   {
807     fd_set ready;
808     int n;
809
810     FD_ZERO(&ready);
811     FD_SET(master_fd, &ready);
812     n = select(master_fd + 1, &ready, NULL, &ready, timo);
813     if (n < 0 && errno != EINTR)
814       {
815         syslog(LOG_ERR, "select: %m(%d)", errno);
816         die(1);
817       }
818   }
819
820 /********************************************************************
821  *
822  * wait_time - wait for a given length of time or until a
823  * signal is received.
824  */
825
826 void wait_time(timo)
827     struct timeval *timo;
828 {
829     int n;
830
831     n = select(0, NULL, NULL, NULL, timo);
832     if (n < 0 && errno != EINTR) {
833         syslog(LOG_ERR, "select: %m(%d)", errno);
834         die(1);
835     }
836 }
837
838 /********************************************************************
839  *
840  * read_packet - get a PPP packet from the serial device.
841  */
842
843 int read_packet (unsigned char *buf)
844   {
845     int len;
846   
847     len = read(ppp_fd, buf, PPP_MTU + PPP_HDRLEN);
848     if (len < 0)
849       {
850         if (errno == EWOULDBLOCK)
851           {
852             return -1;
853           }
854         syslog(LOG_ERR, "read: %m(%d)", errno);
855         die(1);
856       }
857     return len;
858   }
859
860 /********************************************************************
861  *
862  * get_loop_output - get outgoing packets from the ppp device,
863  * and detect when we want to bring the real link up.
864  * Return value is 1 if we need to bring up the link, 0 otherwise.
865  */
866 int
867 get_loop_output(void)
868   {
869     int rv = 0;
870     int n  = read(master_fd, inbuf, sizeof(inbuf));
871
872     while (n > 0)
873       {
874         if (loop_chars(inbuf, n))
875           {
876             rv = 1;
877           }
878         n = read(master_fd, inbuf, sizeof(inbuf));
879       }
880
881     if (n == 0)
882       {
883         syslog(LOG_ERR, "eof on loopback");
884         die(1);
885       }
886
887     if (errno != EWOULDBLOCK)
888       {
889         syslog(LOG_ERR, "read from loopback: %m(%d)", errno);
890         die(1);
891       }
892     
893     return rv;
894   }
895
896 /********************************************************************
897  *
898  * ppp_send_config - configure the transmit characteristics of
899  * the ppp interface.
900  */
901
902 void ppp_send_config (int unit,int mtu,u_int32_t asyncmap,int pcomp,int accomp)
903   {
904     u_int x;
905     struct ifreq ifr;
906   
907     SYSDEBUG ((LOG_DEBUG, "send_config: mtu = %d\n", mtu));
908 /*
909  * Ensure that the link is still up.
910  */
911     if (still_ppp())
912       {
913 /*
914  * Set the MTU and other parameters for the ppp device
915  */
916         memset (&ifr, '\0', sizeof (ifr));
917         strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
918         ifr.ifr_mtu = mtu;
919         
920         if (ioctl(sock_fd, SIOCSIFMTU, (caddr_t) &ifr) < 0)
921           {
922             syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m(%d)", errno);
923             quit();
924           }
925         
926         SYSDEBUG ((LOG_DEBUG, "send_config: asyncmap = %lx\n", asyncmap));
927         if (ioctl(ppp_fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0)
928           {
929             syslog(LOG_ERR, "ioctl(PPPIOCSASYNCMAP): %m(%d)", errno);
930             quit();
931           }
932     
933         x = get_flags();
934         x = pcomp  ? x | SC_COMP_PROT : x & ~SC_COMP_PROT;
935         x = accomp ? x | SC_COMP_AC   : x & ~SC_COMP_AC;
936         set_flags(x);
937       }
938   }
939
940 /********************************************************************
941  *
942  * ppp_set_xaccm - set the extended transmit ACCM for the interface.
943  */
944
945 void ppp_set_xaccm (int unit, ext_accm accm)
946   {
947     SYSDEBUG ((LOG_DEBUG, "set_xaccm: %08lx %08lx %08lx %08lx\n",
948                 accm[0], accm[1], accm[2], accm[3]));
949
950     if (ioctl(ppp_fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY)
951       {
952         if ( ! ok_error (errno))
953           {
954             syslog(LOG_WARNING, "ioctl(set extended ACCM): %m(%d)", errno);
955           }
956       }
957   }
958
959 /********************************************************************
960  *
961  * ppp_recv_config - configure the receive-side characteristics of
962  * the ppp interface.
963  */
964
965 void ppp_recv_config (int unit,int mru,u_int32_t asyncmap,int pcomp,int accomp)
966   {
967     u_int x;
968
969     SYSDEBUG ((LOG_DEBUG, "recv_config: mru = %d\n", mru));
970 /*
971  * If we were called because the link has gone down then there is nothing
972  * which may be done. Just return without incident.
973  */
974     if (!still_ppp())
975       {
976         return;
977       }
978 /*
979  * Set the receiver parameters
980  */
981     if (ioctl(ppp_fd, PPPIOCSMRU, (caddr_t) &mru) < 0)
982       {
983         if ( ! ok_error (errno))
984           {
985             syslog(LOG_ERR, "ioctl(PPPIOCSMRU): %m(%d)", errno);
986           }
987       }
988
989     SYSDEBUG ((LOG_DEBUG, "recv_config: asyncmap = %lx\n", asyncmap));
990     if (ioctl(ppp_fd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0)
991       {
992         syslog(LOG_ERR, "ioctl(PPPIOCSRASYNCMAP): %m(%d)", errno);
993         quit();
994       }
995
996     x = get_flags();
997     x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC;
998     set_flags (x);
999   }
1000
1001 /********************************************************************
1002  *
1003  * ccp_test - ask kernel whether a given compression method
1004  * is acceptable for use.
1005  */
1006
1007 int ccp_test (int unit, u_char *opt_ptr, int opt_len, int for_transmit)
1008   {
1009     struct ppp_option_data data;
1010
1011     memset (&data, '\0', sizeof (data));
1012     data.ptr      = opt_ptr;
1013     data.length   = opt_len;
1014     data.transmit = for_transmit;
1015
1016     if (ioctl(ppp_fd, PPPIOCSCOMPRESS, (caddr_t) &data) >= 0)
1017       {
1018         return 1;
1019       }
1020
1021     return (errno == ENOBUFS)? 0: -1;
1022   }
1023
1024 /********************************************************************
1025  *
1026  * ccp_flags_set - inform kernel about the current state of CCP.
1027  */
1028
1029 void ccp_flags_set (int unit, int isopen, int isup)
1030   {
1031     if (still_ppp())
1032       {
1033         int x = get_flags();
1034         x = isopen? x | SC_CCP_OPEN : x &~ SC_CCP_OPEN;
1035         x = isup?   x | SC_CCP_UP   : x &~ SC_CCP_UP;
1036         set_flags (x);
1037       }
1038   }
1039
1040 /********************************************************************
1041  *
1042  * get_idle_time - return how long the link has been idle.
1043  */
1044 int
1045 get_idle_time(u, ip)
1046     int u;
1047     struct ppp_idle *ip;
1048 {
1049     return ioctl(ppp_fd, PPPIOCGIDLE, ip) >= 0;
1050
1051
1052 /********************************************************************
1053  *
1054  * ccp_fatal_error - returns 1 if decompression was disabled as a
1055  * result of an error detected after decompression of a packet,
1056  * 0 otherwise.  This is necessary because of patent nonsense.
1057  */
1058
1059 int ccp_fatal_error (int unit)
1060   {
1061     int x = get_flags();
1062
1063     return x & SC_DC_FERROR;
1064   }
1065
1066 /*
1067  * path_to_route - determine the path to the proc file system data
1068  */
1069
1070 FILE *route_fd = (FILE *) 0;
1071 static char route_buffer [512];
1072
1073 static char *path_to_route (void);
1074 static int open_route_table (void);
1075 static void close_route_table (void);
1076 static int read_route_table (struct rtentry *rt);
1077
1078 /********************************************************************
1079  *
1080  * path_to_procfs - find the path to the proc file system mount point
1081  */
1082
1083 static int path_to_procfs (void)
1084   {
1085     struct mntent *mntent;
1086     FILE *fp;
1087
1088     fp = fopen (MOUNTED, "r");
1089     if (fp != 0)
1090       {
1091         mntent = getmntent (fp);
1092         while (mntent != (struct mntent *) 0)
1093           {
1094             if (strcmp (mntent->mnt_type, MNTTYPE_IGNORE) != 0)
1095               {
1096                 if (strcmp (mntent->mnt_type, "proc") == 0)
1097                   {
1098                     strncpy (route_buffer, mntent->mnt_dir,
1099                              sizeof (route_buffer)-10);
1100                     route_buffer [sizeof (route_buffer)-10] = '\0';
1101                     fclose (fp);
1102                     return 1;
1103                   }
1104               }
1105             mntent = getmntent (fp);
1106           }
1107         fclose (fp);
1108       }
1109     return 0;
1110   }
1111
1112 /********************************************************************
1113  *
1114  * path_to_route - find the path to the route tables in the proc file system
1115  */
1116
1117 static char *path_to_route (void)
1118   {
1119     if (! path_to_procfs())
1120       {
1121         syslog (LOG_ERR, "proc file system not mounted");
1122         return 0;
1123       }
1124     strcat (route_buffer, "/net/route");
1125     return (route_buffer);
1126   }
1127
1128 /********************************************************************
1129  *
1130  * close_route_table - close the interface to the route table
1131  */
1132
1133 static void close_route_table (void)
1134   {
1135     if (route_fd != (FILE *) 0)
1136       {
1137         fclose (route_fd);
1138         route_fd = (FILE *) 0;
1139       }
1140   }
1141
1142 /********************************************************************
1143  *
1144  * open_route_table - open the interface to the route table
1145  */
1146
1147 static int open_route_table (void)
1148   {
1149     char *path;
1150
1151     close_route_table();
1152
1153     path = path_to_route();
1154     if (path == NULL)
1155       {
1156         return 0;
1157       }
1158
1159     route_fd = fopen (path, "r");
1160     if (route_fd == (FILE *) 0)
1161       {
1162         syslog (LOG_ERR, "can not open %s: %m(%d)", path, errno);
1163         return 0;
1164       }
1165     return 1;
1166   }
1167
1168 /********************************************************************
1169  *
1170  * read_route_table - read the next entry from the route table
1171  */
1172
1173 static int read_route_table (struct rtentry *rt)
1174   {
1175     static char delims[] = " \t\n";
1176     char *dev_ptr, *ptr, *dst_ptr, *gw_ptr, *flag_ptr;
1177         
1178     memset (rt, '\0', sizeof (struct rtentry));
1179
1180     for (;;)
1181       {
1182         if (fgets (route_buffer, sizeof (route_buffer), route_fd) ==
1183             (char *) 0)
1184           {
1185             return 0;
1186           }
1187
1188         dev_ptr  = strtok (route_buffer, delims); /* interface name */
1189         dst_ptr  = strtok (NULL,         delims); /* destination address */
1190         gw_ptr   = strtok (NULL,         delims); /* gateway */
1191         flag_ptr = strtok (NULL,         delims); /* flags */
1192     
1193         if (flag_ptr == (char *) 0) /* assume that we failed, somewhere. */
1194           {
1195             return 0;
1196           }
1197         
1198         /* Discard that stupid header line which should never
1199          * have been there in the first place !! */
1200         if (isxdigit (*dst_ptr) && isxdigit (*gw_ptr) && isxdigit (*flag_ptr))
1201           {
1202             break;
1203           }
1204       }
1205
1206     ((struct sockaddr_in *) &rt->rt_dst)->sin_addr.s_addr =
1207       strtoul (dst_ptr, NULL, 16);
1208
1209     ((struct sockaddr_in *) &rt->rt_gateway)->sin_addr.s_addr =
1210       strtoul (gw_ptr, NULL, 16);
1211
1212     rt->rt_flags = (short) strtoul (flag_ptr, NULL, 16);
1213     rt->rt_dev   = dev_ptr;
1214
1215     return 1;
1216   }
1217
1218 /********************************************************************
1219  *
1220  * defaultroute_exists - determine if there is a default route
1221  */
1222
1223 static int defaultroute_exists (struct rtentry *rt)
1224   {
1225     int    result = 0;
1226
1227     if (!open_route_table())
1228       {
1229         return 0;
1230       }
1231
1232     while (read_route_table(rt) != 0)
1233       {
1234         if ((rt->rt_flags & RTF_UP) == 0)
1235           {
1236             continue;
1237           }
1238
1239         if (((struct sockaddr_in *) (&rt->rt_dst))->sin_addr.s_addr == 0L)
1240           {
1241             result = 1;
1242             break;
1243           }
1244       }
1245
1246     close_route_table();
1247     return result;
1248   }
1249
1250 /********************************************************************
1251  *
1252  * sifdefaultroute - assign a default route through the address given.
1253  */
1254
1255 int sifdefaultroute (int unit, u_int32_t ouraddr, u_int32_t gateway)
1256   {
1257     struct rtentry rt;
1258
1259     if (defaultroute_exists(&rt))
1260       {
1261         u_int32_t old_gateway = ((struct sockaddr_in *) (&rt.rt_gateway))->
1262                                 sin_addr.s_addr;
1263
1264         if (old_gateway != gateway)
1265           {
1266             syslog (LOG_ERR,
1267                     "ppp not replacing existing default route to %s[%s]",
1268                     rt.rt_dev,
1269                     inet_ntoa (old_gateway));
1270           }
1271         return 0;
1272       }
1273
1274     memset (&rt, '\0', sizeof (rt));
1275     SET_SA_FAMILY (rt.rt_dst,     AF_INET);
1276     SET_SA_FAMILY (rt.rt_gateway, AF_INET);
1277
1278 #if LINUX_VERSION_CODE > 0x020100
1279     SET_SA_FAMILY (rt.rt_genmask, AF_INET);
1280     ((struct sockaddr_in *) &rt.rt_genmask)->sin_addr.s_addr = 0L;
1281 #endif
1282
1283     ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = gateway;
1284     
1285     rt.rt_flags = RTF_UP | RTF_GATEWAY | RTF_DEFAULT;
1286     if (ioctl(sock_fd, SIOCADDRT, &rt) < 0)
1287       {
1288         if ( ! ok_error ( errno ))
1289           {
1290             syslog (LOG_ERR, "default route ioctl(SIOCADDRT): %m(%d)", errno);
1291           }
1292         return 0;
1293       }
1294
1295     default_route_gateway = gateway;
1296     return 1;
1297   }
1298
1299 /********************************************************************
1300  *
1301  * cifdefaultroute - delete a default route through the address given.
1302  */
1303
1304 int cifdefaultroute (int unit, u_int32_t ouraddr, u_int32_t gateway)
1305   {
1306     struct rtentry rt;
1307
1308     default_route_gateway = 0;
1309
1310     memset (&rt, '\0', sizeof (rt));
1311     SET_SA_FAMILY (rt.rt_dst,     AF_INET);
1312     SET_SA_FAMILY (rt.rt_gateway, AF_INET);
1313
1314 #if LINUX_VERSION_CODE > 0x020100
1315     SET_SA_FAMILY (rt.rt_genmask, AF_INET);
1316     ((struct sockaddr_in *) &rt.rt_genmask)->sin_addr.s_addr = 0L;
1317 #endif
1318
1319     ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = gateway;
1320     
1321     rt.rt_flags = RTF_UP | RTF_GATEWAY | RTF_DEFAULT;
1322     if (ioctl(sock_fd, SIOCDELRT, &rt) < 0 && errno != ESRCH)
1323       {
1324         if (still_ppp())
1325           {
1326             if ( ! ok_error ( errno ))
1327               {
1328                 syslog (LOG_ERR,
1329                         "default route ioctl(SIOCDELRT): %m(%d)", errno);
1330               }
1331             return 0;
1332           }
1333       }
1334
1335     return 1;
1336   }
1337
1338 /********************************************************************
1339  *
1340  * sifproxyarp - Make a proxy ARP entry for the peer.
1341  */
1342
1343 int sifproxyarp (int unit, u_int32_t his_adr)
1344   {
1345     struct arpreq arpreq;
1346
1347     if (has_proxy_arp == 0)
1348       {
1349         memset (&arpreq, '\0', sizeof(arpreq));
1350     
1351         SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1352         ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = his_adr;
1353         arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1354 /*
1355  * Get the hardware address of an interface on the same subnet
1356  * as our local address.
1357  */
1358         if (!get_ether_addr(his_adr, &arpreq.arp_ha, arpreq.arp_dev))
1359           {
1360             syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
1361             return 0;
1362           }
1363         
1364         if (ioctl(sock_fd, SIOCSARP, (caddr_t)&arpreq) < 0)
1365           {
1366             if ( ! ok_error ( errno ))
1367               {
1368                 syslog(LOG_ERR, "ioctl(SIOCSARP): %m(%d)", errno);
1369               }
1370             return 0;
1371           }
1372       }
1373
1374     proxy_arp_addr = his_adr;
1375     has_proxy_arp = 1;
1376     return 1;
1377   }
1378
1379 /********************************************************************
1380  *
1381  * cifproxyarp - Delete the proxy ARP entry for the peer.
1382  */
1383
1384 int cifproxyarp (int unit, u_int32_t his_adr)
1385   {
1386     struct arpreq arpreq;
1387
1388     if (has_proxy_arp == 1)
1389       {
1390         memset (&arpreq, '\0', sizeof(arpreq));
1391         SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1392         ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = his_adr;
1393         arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1394
1395         if (ioctl(sock_fd, SIOCDARP, (caddr_t)&arpreq) < 0)
1396           {
1397             if ( ! ok_error ( errno ))
1398               {
1399                 syslog(LOG_WARNING, "ioctl(SIOCDARP): %m(%d)", errno);
1400               }
1401             return 0;
1402           }
1403       }
1404     has_proxy_arp = 0;
1405     return 1;
1406   }
1407      
1408 /********************************************************************
1409  *
1410  * get_ether_addr - get the hardware address of an interface on the
1411  * the same subnet as ipaddr.
1412  */
1413
1414 static int get_ether_addr (u_int32_t ipaddr,
1415                            struct sockaddr *hwaddr,
1416                            char *name)
1417   {
1418     struct ifreq *ifr, *ifend, *ifp;
1419     int i;
1420     u_int32_t ina, mask;
1421     struct ifreq ifreq;
1422     struct ifconf ifc;
1423     struct ifreq ifs[MAX_IFS];
1424     
1425     ifc.ifc_len = sizeof(ifs);
1426     ifc.ifc_req = ifs;
1427     if (ioctl(sock_fd, SIOCGIFCONF, &ifc) < 0)
1428       {
1429         if ( ! ok_error ( errno ))
1430           {
1431             syslog(LOG_ERR, "ioctl(SIOCGIFCONF): %m(%d)", errno);
1432           }
1433         return 0;
1434       }
1435
1436     SYSDEBUG ((LOG_DEBUG, "proxy arp: scanning %d interfaces for IP %s",
1437                 ifc.ifc_len / sizeof(struct ifreq), ip_ntoa(ipaddr)));
1438 /*
1439  * Scan through looking for an interface with an Internet
1440  * address on the same subnet as `ipaddr'.
1441  */
1442     ifend = ifs + (ifc.ifc_len / sizeof(struct ifreq));
1443     for (ifr = ifc.ifc_req; ifr < ifend; ifr++)
1444       {
1445         if (ifr->ifr_addr.sa_family == AF_INET)
1446           {
1447             ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1448             strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1449             SYSDEBUG ((LOG_DEBUG, "proxy arp: examining interface %s",
1450                         ifreq.ifr_name));
1451 /*
1452  * Check that the interface is up, and not point-to-point
1453  * nor loopback.
1454  */
1455             if (ioctl(sock_fd, SIOCGIFFLAGS, &ifreq) < 0)
1456               {
1457                 continue;
1458               }
1459
1460             if (((ifreq.ifr_flags ^ FLAGS_GOOD) & FLAGS_MASK) != 0)
1461               {
1462                 continue;
1463               }
1464 /*
1465  * Get its netmask and check that it's on the right subnet.
1466  */
1467             if (ioctl(sock_fd, SIOCGIFNETMASK, &ifreq) < 0)
1468               {
1469                 continue;
1470               }
1471
1472             mask = ((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr;
1473             SYSDEBUG ((LOG_DEBUG, "proxy arp: interface addr %s mask %lx",
1474                         ip_ntoa(ina), ntohl(mask)));
1475
1476             if (((ipaddr ^ ina) & mask) != 0)
1477               {
1478                 continue;
1479               }
1480             break;
1481           }
1482       }
1483     
1484     if (ifr >= ifend)
1485       {
1486         return 0;
1487       }
1488
1489     memcpy (name, ifreq.ifr_name, sizeof(ifreq.ifr_name));
1490     syslog(LOG_INFO, "found interface %s for proxy arp", name);
1491 /*
1492  * Now get the hardware address.
1493  */
1494     memset (&ifreq.ifr_hwaddr, 0, sizeof (struct sockaddr));
1495     if (ioctl (sock_fd, SIOCGIFHWADDR, &ifreq) < 0)
1496       {
1497         syslog(LOG_ERR, "SIOCGIFHWADDR(%s): %m(%d)", ifreq.ifr_name, errno);
1498         return 0;
1499       }
1500
1501     memcpy (hwaddr,
1502             &ifreq.ifr_hwaddr,
1503             sizeof (struct sockaddr));
1504
1505     SYSDEBUG ((LOG_DEBUG,
1506            "proxy arp: found hwaddr %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
1507                 (int) ((unsigned char *) &hwaddr->sa_data)[0],
1508                 (int) ((unsigned char *) &hwaddr->sa_data)[1],
1509                 (int) ((unsigned char *) &hwaddr->sa_data)[2],
1510                 (int) ((unsigned char *) &hwaddr->sa_data)[3],
1511                 (int) ((unsigned char *) &hwaddr->sa_data)[4],
1512                 (int) ((unsigned char *) &hwaddr->sa_data)[5],
1513                 (int) ((unsigned char *) &hwaddr->sa_data)[6],
1514                 (int) ((unsigned char *) &hwaddr->sa_data)[7]));
1515     return 1;
1516   }
1517
1518 /********************************************************************
1519  *
1520  * Return user specified netmask, modified by any mask we might determine
1521  * for address `addr' (in network byte order).
1522  * Here we scan through the system's list of interfaces, looking for
1523  * any non-point-to-point interfaces which might appear to be on the same
1524  * network as `addr'.  If we find any, we OR in their netmask to the
1525  * user-specified netmask.
1526  */
1527
1528 u_int32_t GetMask (u_int32_t addr)
1529   {
1530     u_int32_t mask, nmask, ina;
1531     struct ifreq *ifr, *ifend, ifreq;
1532     struct ifconf ifc;
1533     struct ifreq ifs[MAX_IFS];
1534
1535     addr = ntohl(addr);
1536     
1537     if (IN_CLASSA(addr))        /* determine network mask for address class */
1538       {
1539         nmask = IN_CLASSA_NET;
1540       }
1541     else
1542       {
1543         if (IN_CLASSB(addr))
1544           {
1545             nmask = IN_CLASSB_NET;
1546           }
1547         else
1548           {
1549             nmask = IN_CLASSC_NET;
1550           }
1551       }
1552     
1553     /* class D nets are disallowed by bad_ip_adrs */
1554     mask = netmask | htonl(nmask);
1555 /*
1556  * Scan through the system's network interfaces.
1557  */
1558     ifc.ifc_len = sizeof(ifs);
1559     ifc.ifc_req = ifs;
1560     if (ioctl(sock_fd, SIOCGIFCONF, &ifc) < 0)
1561       {
1562         if ( ! ok_error ( errno ))
1563           {
1564             syslog(LOG_WARNING, "ioctl(SIOCGIFCONF): %m(%d)", errno);
1565           }
1566         return mask;
1567       }
1568     
1569     ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1570     for (ifr = ifc.ifc_req; ifr < ifend; ifr++)
1571       {
1572 /*
1573  * Check the interface's internet address.
1574  */
1575         if (ifr->ifr_addr.sa_family != AF_INET)
1576           {
1577             continue;
1578           }
1579         ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1580         if (((ntohl(ina) ^ addr) & nmask) != 0)
1581           {
1582             continue;
1583           }
1584 /*
1585  * Check that the interface is up, and not point-to-point nor loopback.
1586  */
1587         strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1588         if (ioctl(sock_fd, SIOCGIFFLAGS, &ifreq) < 0)
1589           {
1590             continue;
1591           }
1592         
1593         if (((ifreq.ifr_flags ^ FLAGS_GOOD) & FLAGS_MASK) != 0)
1594           {
1595             continue;
1596           }
1597 /*
1598  * Get its netmask and OR it into our mask.
1599  */
1600         if (ioctl(sock_fd, SIOCGIFNETMASK, &ifreq) < 0)
1601           {
1602             continue;
1603           }
1604         mask |= ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr;
1605         break;
1606       }
1607     return mask;
1608   }
1609
1610 /********************************************************************
1611  *
1612  * Internal routine to decode the version.modification.patch level
1613  */
1614
1615 static void decode_version (char *buf, int *version,
1616                             int *modification, int *patch)
1617   {
1618     *version      = (int) strtoul (buf, &buf, 10);
1619     *modification = 0;
1620     *patch        = 0;
1621     
1622     if (*buf == '.')
1623       {
1624         ++buf;
1625         *modification = (int) strtoul (buf, &buf, 10);
1626         if (*buf == '.')
1627           {
1628             ++buf;
1629             *patch = (int) strtoul (buf, &buf, 10);
1630           }
1631       }
1632     
1633     if (*buf != '\0')
1634       {
1635         *version      =
1636         *modification =
1637         *patch        = 0;
1638       }
1639   }
1640
1641 /********************************************************************
1642  *
1643  * Procedure to determine if the PPP line dicipline is registered.
1644  */
1645
1646 int
1647 ppp_registered(void)
1648   {
1649     int local_fd;
1650     int init_disc = -1;
1651     int initfdflags;
1652
1653     local_fd = open(devnam, O_NONBLOCK | O_RDWR, 0);
1654     if (local_fd < 0)
1655       {
1656         syslog(LOG_ERR, "Failed to open %s: %m(%d)", devnam, errno);
1657         return 0;
1658       }
1659
1660     initfdflags = fcntl(local_fd, F_GETFL);
1661     if (initfdflags == -1)
1662       {
1663         syslog(LOG_ERR, "Couldn't get device fd flags: %m(%d)", errno);
1664         close (local_fd);
1665         return 0;
1666       }
1667
1668     initfdflags &= ~O_NONBLOCK;
1669     fcntl(local_fd, F_SETFL, initfdflags);
1670 /*
1671  * Read the initial line dicipline and try to put the device into the
1672  * PPP dicipline.
1673  */
1674     if (ioctl(local_fd, TIOCGETD, &init_disc) < 0)
1675       {
1676         syslog(LOG_ERR, "ioctl(TIOCGETD): %m(%d)", errno);
1677         close (local_fd);
1678         return 0;
1679       }
1680     
1681     if (ioctl(local_fd, TIOCSETD, &ppp_disc) < 0)
1682       {
1683         syslog(LOG_ERR, "ioctl(TIOCSETD): %m(%d)", errno);
1684         close (local_fd);
1685         return 0;
1686       }
1687     
1688     if (ioctl(local_fd, TIOCSETD, &init_disc) < 0)
1689       {
1690         syslog(LOG_ERR, "ioctl(TIOCSETD): %m(%d)", errno);
1691         close (local_fd);
1692         return 0;
1693       }
1694     
1695     close (local_fd);
1696     return 1;
1697   }
1698
1699 /********************************************************************
1700  *
1701  * ppp_available - check whether the system has any ppp interfaces
1702  * (in fact we check whether we can do an ioctl on ppp0).
1703  */
1704
1705 int ppp_available(void)
1706   {
1707     int s, ok;
1708     struct ifreq ifr;
1709     int    size;
1710     int    my_version, my_modification, my_patch;
1711     extern char *no_ppp_msg;
1712 /*
1713  * Open a socket for doing the ioctl operations.
1714  */    
1715     s = socket(AF_INET, SOCK_DGRAM, 0);
1716     if (s < 0)
1717       {
1718         return 0;
1719       }
1720     
1721     strncpy (ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
1722     ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
1723 /*
1724  * If the device did not exist then attempt to create one by putting the
1725  * current tty into the PPP discipline. If this works then obtain the
1726  * flags for the device again.
1727  */
1728     if (!ok)
1729       {
1730         if (ppp_registered())
1731           {
1732             strncpy (ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
1733             ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
1734           }
1735       }
1736 /*
1737  * Ensure that the hardware address is for PPP and not something else
1738  */
1739     if (ok)
1740       {
1741         ok = ioctl (s, SIOCGIFHWADDR, (caddr_t) &ifr) >= 0;
1742       }
1743
1744     if (ok && ((ifr.ifr_hwaddr.sa_family & ~0xFF) != ARPHRD_PPP))
1745       {
1746         ok = 0;
1747       }
1748
1749     if (!ok)
1750       {
1751         no_ppp_msg = 
1752           "This system lacks kernel support for PPP. To include PPP support\n"
1753           "in the kernel, please follow the steps detailed in the "
1754           "README.linux\nfile in the ppp-2.3 distribution.\n";
1755       }
1756 /*
1757  *  This is the PPP device. Validate the version of the driver at this
1758  *  point to ensure that this program will work with the driver.
1759  */
1760     else
1761       {
1762         char   abBuffer [1024];
1763
1764         ifr.ifr_data = abBuffer;
1765         size = ioctl (s, SIOCGPPPVER, (caddr_t) &ifr);
1766         ok   = size >= 0;
1767
1768         if (ok)
1769           {
1770             decode_version (abBuffer,
1771                             &driver_version,
1772                             &driver_modification,
1773                             &driver_patch);
1774           }
1775     
1776         if (!ok)
1777           {
1778             driver_version      =
1779             driver_modification =
1780             driver_patch        = 0;
1781           }
1782 /*
1783  * Validate the version of the driver against the version that we used.
1784  */
1785         decode_version (PPP_VERSION,
1786                         &my_version,
1787                         &my_modification,
1788                         &my_patch);
1789
1790         /* The version numbers must match */
1791         if (driver_version != my_version)
1792           {
1793             ok = 0;
1794           }
1795       
1796         /* The modification levels must be legal */
1797         if (driver_modification < my_modification)
1798           {
1799             ok = 0;
1800           }
1801
1802         close (s);
1803         if (!ok)
1804           {
1805             sprintf (route_buffer,
1806                      "Sorry - PPP driver version %d.%d.%d is out of date\n",
1807                      driver_version, driver_modification, driver_patch);
1808
1809             no_ppp_msg = route_buffer;
1810           }
1811       }
1812     return ok;
1813   }
1814
1815 /********************************************************************
1816  *
1817  * Update the wtmp file with the appropriate user name and tty device.
1818  */
1819
1820 void logwtmp (const char *line, const char *name, const char *host)
1821   {
1822     int    wtmp;
1823     struct utmp ut, *utp;
1824     pid_t  mypid = getpid();
1825 /*
1826  * Update the signon database for users.
1827  * Christoph Lameter: Copied from poeigl-1.36 Jan 3, 1996
1828  */
1829     utmpname(_PATH_UTMP);
1830     setutent();
1831     while ((utp = getutent()) && (utp->ut_pid != mypid))
1832         /* nothing */;
1833
1834     /* Is this call really necessary? There is another one after the 'put' */
1835     endutent();
1836     
1837     if (utp)
1838       {
1839         memcpy(&ut, utp, sizeof(ut));
1840       }
1841     else
1842       {
1843         /* some gettys/telnetds don't initialize utmp... */
1844         memset(&ut, 0, sizeof(ut));
1845       }
1846
1847     if (ut.ut_id[0] == 0)
1848       {
1849         strncpy(ut.ut_id, line + 3, sizeof(ut.ut_id));
1850       }
1851         
1852     strncpy(ut.ut_user, name, sizeof(ut.ut_user));
1853     strncpy(ut.ut_line, line, sizeof(ut.ut_line));
1854
1855     time(&ut.ut_time);
1856
1857     ut.ut_type = USER_PROCESS;
1858     ut.ut_pid  = mypid;
1859
1860     /* Insert the host name if one is supplied */
1861     if (*host)
1862       {
1863         strncpy (ut.ut_host, host, sizeof(ut.ut_host));
1864       }
1865
1866     /* Insert the IP address of the remote system if IP is enabled */
1867     if (ipcp_protent.enabled_flag && ipcp_hisoptions[0].neg_addr)
1868       {
1869         memcpy  (&ut.ut_addr, (char *) &ipcp_hisoptions[0].hisaddr,
1870                  sizeof(ut.ut_addr));
1871       }
1872         
1873     /* CL: Makes sure that the logout works */
1874     if (*host == 0 && *name==0)
1875       {
1876         ut.ut_host[0]=0;
1877       }
1878
1879     pututline(&ut);
1880     endutent();
1881 /*
1882  * Update the wtmp file.
1883  */
1884     wtmp = open(_PATH_WTMP, O_APPEND|O_WRONLY);
1885     if (wtmp >= 0)
1886       {
1887         flock(wtmp, LOCK_EX);
1888
1889         /* we really should check for error on the write for a full disk! */
1890         write (wtmp, (char *)&ut, sizeof(ut));
1891         close (wtmp);
1892
1893         flock(wtmp, LOCK_UN);
1894       }
1895   }
1896
1897 /********************************************************************
1898  * Code for locking/unlocking the serial device.
1899  * This code is derived from chat.c.
1900  */
1901
1902 /*
1903  * lock - create a lock file for the named device
1904  */
1905
1906 int lock (char *dev)
1907   {
1908 #ifdef LOCKLIB
1909     int result;
1910     lock_file = malloc(strlen(dev) + 1);
1911     if (lock_file == NULL)
1912       {
1913         novm("lock file name");
1914       }
1915     strcpy (lock_file, dev);
1916     result = mklock (dev, (void *) 0);
1917
1918     if (result > 0)
1919       {
1920         syslog (LOG_NOTICE, "Device %s is locked by pid %d", dev, result);
1921         free (lock_file);
1922         lock_file = NULL;
1923         result = -1;
1924       }
1925     else
1926       {
1927         if (result < 0)
1928           {
1929             syslog (LOG_ERR, "Can't create lock file %s", lock_file);
1930             free (lock_file);
1931             lock_file = NULL;
1932             result = -1;
1933           }
1934       }
1935     return (result);
1936 #else
1937     char hdb_lock_buffer[12];
1938     int fd, n;
1939     int pid = getpid();
1940     char *p;
1941
1942     p = strrchr(dev, '/');
1943     if (p != NULL)
1944       {
1945         dev = ++p;
1946       }
1947
1948     lock_file = malloc(strlen(LOCK_PREFIX) + strlen(dev) + 1);
1949     if (lock_file == NULL)
1950       {
1951         novm("lock file name");
1952       }
1953
1954     strcpy (lock_file, LOCK_PREFIX);
1955     strcat (lock_file, dev);
1956 /*
1957  * Attempt to create the lock file at this point.
1958  */
1959     while (1)
1960       {
1961         fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644);
1962         if (fd >= 0)
1963           {
1964             pid = getpid();
1965 #ifndef PID_BINARY
1966             sprintf (hdb_lock_buffer, "%010d\n", pid);
1967             write (fd, hdb_lock_buffer, 11);
1968 #else
1969             write(fd, &pid, sizeof (pid));
1970 #endif
1971             close(fd);
1972             return 0;
1973           }
1974 /*
1975  * If the file exists then check to see if the pid is stale
1976  */
1977         if (errno == EEXIST)
1978           {
1979             fd = open(lock_file, O_RDONLY, 0);
1980             if (fd < 0)
1981               {
1982                 if (errno == ENOENT) /* This is just a timing problem. */
1983                   {
1984                     continue;
1985                   }
1986                 break;
1987               }
1988
1989             /* Read the lock file to find out who has the device locked */
1990             n = read (fd, hdb_lock_buffer, 11);
1991             close (fd);
1992             if (n < 0)
1993               {
1994                 syslog(LOG_ERR, "Can't read pid from lock file %s", lock_file);
1995                 break;
1996               }
1997
1998             /* See the process still exists. */
1999             if (n > 0)
2000               {
2001 #ifndef PID_BINARY
2002                 hdb_lock_buffer[n] = '\0';
2003                 sscanf (hdb_lock_buffer, " %d", &pid);
2004 #else
2005                 pid = ((int *) hdb_lock_buffer)[0];
2006 #endif
2007                 if (pid == 0 || pid == getpid()
2008                     || (kill(pid, 0) == -1 && errno == ESRCH))
2009                   {
2010                     n = 0;
2011                   }
2012               }
2013
2014             /* If the process does not exist then try to remove the lock */
2015             if (n == 0 && unlink (lock_file) == 0)
2016               {
2017                 syslog (LOG_NOTICE, "Removed stale lock on %s (pid %d)",
2018                         dev, pid);
2019                 continue;
2020               }
2021
2022             syslog (LOG_NOTICE, "Device %s is locked by pid %d", dev, pid);
2023             break;
2024           }
2025
2026         syslog(LOG_ERR, "Can't create lock file %s: %m(%d)", lock_file, errno);
2027         break;
2028       }
2029
2030     free(lock_file);
2031     lock_file = NULL;
2032     return -1;
2033 #endif
2034 }
2035
2036
2037 /********************************************************************
2038  *
2039  * unlock - remove our lockfile
2040  */
2041
2042 void unlock(void)
2043   {
2044     if (lock_file)
2045       {
2046 #ifdef LOCKLIB
2047         (void) rmlock (lock_file, (void *) 0);
2048 #else
2049         unlink(lock_file);
2050 #endif
2051         free(lock_file);
2052         lock_file = NULL;
2053       }
2054   }
2055
2056 /********************************************************************
2057  *
2058  * sifvjcomp - config tcp header compression
2059  */
2060
2061 int sifvjcomp (int u, int vjcomp, int cidcomp, int maxcid)
2062   {
2063     u_int x = get_flags();
2064
2065     if (vjcomp)
2066       {
2067         if (ioctl (ppp_fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0)
2068           {
2069             if (! ok_error (errno))
2070               {
2071                 syslog (LOG_ERR, "ioctl(PPPIOCSFLAGS): %m(%d)", errno);
2072               }
2073             vjcomp = 0;
2074           }
2075       }
2076
2077     x = vjcomp  ? x | SC_COMP_TCP     : x &~ SC_COMP_TCP;
2078     x = cidcomp ? x & ~SC_NO_TCP_CCID : x | SC_NO_TCP_CCID;
2079     set_flags (x);
2080
2081     return 1;
2082   }
2083
2084 /********************************************************************
2085  *
2086  * sifup - Config the interface up and enable IP packets to pass.
2087  */
2088
2089 int sifup (int u)
2090   {
2091     struct ifreq ifr;
2092
2093     memset (&ifr, '\0', sizeof (ifr));
2094     strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
2095     if (ioctl(sock_fd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0)
2096       {
2097         if (! ok_error (errno))
2098           {
2099             syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m(%d)", errno);
2100           }
2101         return 0;
2102       }
2103
2104     ifr.ifr_flags |= (IFF_UP | IFF_POINTOPOINT);
2105     if (ioctl(sock_fd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0)
2106       {
2107         if (! ok_error (errno))
2108           {
2109             syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m(%d)", errno);
2110           }
2111         return 0;
2112       }
2113     if_is_up = 1;
2114     return 1;
2115   }
2116
2117 /********************************************************************
2118  *
2119  * sifdown - Config the interface down and disable IP.
2120  */
2121
2122 int sifdown (int u)
2123   {
2124     struct ifreq ifr;
2125
2126     if_is_up = 0;
2127
2128     memset (&ifr, '\0', sizeof (ifr));
2129     strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
2130     if (ioctl(sock_fd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0)
2131       {
2132         if (! ok_error (errno))
2133           {
2134             syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m(%d)", errno);
2135           }
2136         return 0;
2137       }
2138
2139     ifr.ifr_flags &= ~IFF_UP;
2140     ifr.ifr_flags |= IFF_POINTOPOINT;
2141     if (ioctl(sock_fd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0)
2142       {
2143         if (! ok_error (errno))
2144           {
2145             syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m(%d)", errno);
2146           }
2147         return 0;
2148       }
2149     return 1;
2150   }
2151
2152 /********************************************************************
2153  *
2154  * sifaddr - Config the interface IP addresses and netmask.
2155  */
2156
2157 int sifaddr (int unit, u_int32_t our_adr, u_int32_t his_adr,
2158              u_int32_t net_mask)
2159   {
2160     struct ifreq   ifr; 
2161     struct rtentry rt;
2162     
2163     memset (&ifr, '\0', sizeof (ifr));
2164     memset (&rt,  '\0', sizeof (rt));
2165     
2166     SET_SA_FAMILY (ifr.ifr_addr,    AF_INET); 
2167     SET_SA_FAMILY (ifr.ifr_dstaddr, AF_INET); 
2168     SET_SA_FAMILY (ifr.ifr_netmask, AF_INET); 
2169
2170     strncpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
2171 /*
2172  *  Set our IP address
2173  */
2174     ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = our_adr;
2175     if (ioctl(sock_fd, SIOCSIFADDR, (caddr_t) &ifr) < 0)
2176       {
2177         if (errno != EEXIST)
2178           {
2179             if (! ok_error (errno))
2180               {
2181                 syslog (LOG_ERR, "ioctl(SIOCAIFADDR): %m(%d)", errno);
2182               }
2183           }
2184         else
2185           {
2186             syslog (LOG_WARNING, "ioctl(SIOCAIFADDR): Address already exists");
2187           }
2188         return (0);
2189       } 
2190 /*
2191  *  Set the gateway address
2192  */
2193     ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = his_adr;
2194     if (ioctl(sock_fd, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0)
2195       {
2196         if (! ok_error (errno))
2197           {
2198             syslog (LOG_ERR, "ioctl(SIOCSIFDSTADDR): %m(%d)", errno); 
2199           }
2200         return (0);
2201       } 
2202 /*
2203  *  Set the netmask
2204  */
2205     if (net_mask != 0)
2206       {
2207         ((struct sockaddr_in *) &ifr.ifr_netmask)->sin_addr.s_addr = net_mask;
2208         if (ioctl(sock_fd, SIOCSIFNETMASK, (caddr_t) &ifr) < 0)
2209           {
2210             if (! ok_error (errno))
2211               {
2212                 syslog (LOG_ERR, "ioctl(SIOCSIFNETMASK): %m(%d)", errno); 
2213               }
2214             return (0);
2215           } 
2216       }
2217 /*
2218  *  Add the device route
2219  */
2220 #if LINUX_VERSION_CODE < 0x020100+16            /* 2.1.16 */
2221     SET_SA_FAMILY (rt.rt_dst,     AF_INET);
2222     SET_SA_FAMILY (rt.rt_gateway, AF_INET);
2223     rt.rt_dev = ifname;
2224
2225     ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = 0L;
2226     ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr     = his_adr;
2227     rt.rt_flags = RTF_UP | RTF_HOST;
2228
2229 #if LINUX_VERSION_CODE > 0x020100
2230     SET_SA_FAMILY (rt.rt_genmask, AF_INET);
2231     ((struct sockaddr_in *) &rt.rt_genmask)->sin_addr.s_addr = -1L;
2232 #endif
2233
2234     if (ioctl(sock_fd, SIOCADDRT, &rt) < 0)
2235       {
2236         if (! ok_error (errno))
2237           {
2238             syslog (LOG_ERR, "ioctl(SIOCADDRT) device route: %m(%d)", errno);
2239           }
2240         return (0);
2241       }
2242 #endif
2243     return 1;
2244   }
2245
2246 /********************************************************************
2247  *
2248  * cifaddr - Clear the interface IP addresses, and delete routes
2249  * through the interface if possible.
2250  */
2251
2252 int cifaddr (int unit, u_int32_t our_adr, u_int32_t his_adr)
2253   {
2254 #if LINUX_VERSION_CODE < 0x020100+16            /* 2.1.16 */
2255     struct rtentry rt;
2256 /*
2257  *  Delete the route through the device
2258  */
2259     memset (&rt, '\0', sizeof (rt));
2260
2261     SET_SA_FAMILY (rt.rt_dst,     AF_INET);
2262     SET_SA_FAMILY (rt.rt_gateway, AF_INET);
2263     rt.rt_dev = ifname;
2264
2265     ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = 0;
2266     ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr     = his_adr;
2267     rt.rt_flags = RTF_UP | RTF_HOST;
2268
2269 #if LINUX_VERSION_CODE > 0x020100
2270     SET_SA_FAMILY (rt.rt_genmask, AF_INET);
2271     ((struct sockaddr_in *) &rt.rt_genmask)->sin_addr.s_addr = -1L;
2272 #endif
2273
2274     if (ioctl(sock_fd, SIOCDELRT, &rt) < 0 && errno != ESRCH)
2275       {
2276         if (still_ppp() && ! ok_error (errno))
2277           {
2278             syslog (LOG_ERR, "ioctl(SIOCDELRT) device route: %m(%d)", errno);
2279           }
2280         return (0);
2281       }
2282 #endif
2283     return 1;
2284   }
2285
2286 /********************************************************************
2287  *
2288  * open_loopback - open the device we use for getting packets
2289  * in demand mode.  Under Linux, we use our existing fd
2290  * to the ppp driver.
2291  */
2292 void
2293 open_ppp_loopback(void)
2294   {
2295     int flags, i;
2296     struct termios tios;
2297
2298     master_fd = -1;
2299     for (i = 0; i < 64; ++i) {
2300       sprintf(loop_name, "/dev/pty%c%x", 'p' + i / 16, i % 16);
2301       master_fd = open(loop_name, O_RDWR, 0);
2302       if (master_fd >= 0)
2303         break;
2304     }
2305     if (master_fd < 0) {
2306       syslog(LOG_ERR, "No free pty for loopback");
2307       die(1);
2308     }
2309     SYSDEBUG((LOG_DEBUG, "using %s for loopback", loop_name));
2310     loop_name[5] = 't';
2311     slave_fd = open(loop_name, O_RDWR, 0);
2312     if (slave_fd < 0) {
2313       syslog(LOG_ERR, "Couldn't open %s for loopback: %m", loop_name);
2314       die(1);
2315     }
2316
2317     set_ppp_fd(slave_fd);
2318
2319     if (tcgetattr(ppp_fd, &tios) == 0)
2320       {
2321         tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB);
2322         tios.c_cflag |= CS8 | CREAD;
2323         tios.c_iflag  = IGNPAR | CLOCAL;
2324         tios.c_oflag  = 0;
2325         tios.c_lflag  = 0;
2326         if (tcsetattr(ppp_fd, TCSAFLUSH, &tios) < 0)
2327           {
2328             syslog(LOG_WARNING, "couldn't set attributes on loopback: %m(%d)", errno);
2329           }
2330       }
2331
2332     flags = fcntl(master_fd, F_GETFL);
2333     if (flags == -1 ||
2334         fcntl(master_fd, F_SETFL, flags | O_NONBLOCK) == -1)
2335       {
2336         syslog(LOG_WARNING, "couldn't set master loopback to nonblock: %m(%d)", errno);
2337       }
2338
2339     flags = fcntl(ppp_fd, F_GETFL);
2340     if (flags == -1 ||
2341         fcntl(ppp_fd, F_SETFL, flags | O_NONBLOCK) == -1)
2342       {
2343         syslog(LOG_WARNING, "couldn't set slave loopback to nonblock: %m(%d)", errno);
2344       }
2345
2346     if (ioctl(ppp_fd, TIOCSETD, &ppp_disc) < 0)
2347       {
2348         syslog(LOG_ERR, "ioctl(TIOCSETD): %m(%d)", errno);
2349         die(1);
2350       }
2351 /*
2352  * Find out which interface we were given.
2353  */
2354     if (ioctl(ppp_fd, PPPIOCGUNIT, &ifunit) < 0)
2355       { 
2356         syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m(%d)", errno);
2357         die(1);
2358       }
2359 /*
2360  * Enable debug in the driver if requested.
2361  */
2362     set_kdebugflag (kdebugflag);
2363   }
2364
2365 /********************************************************************
2366  *
2367  * restore_loop - reattach the ppp unit to the loopback.
2368  *
2369  * The kernel ppp driver automatically reattaches the ppp unit to
2370  * the loopback if the serial port is set to a line discipline other
2371  * than ppp, or if it detects a modem hangup.  The former will happen
2372  * in disestablish_ppp if the latter hasn't already happened, so we
2373  * shouldn't need to do anything.
2374  *
2375  * Just to be sure, set the real serial port to the normal discipline.
2376  */
2377
2378 void
2379 restore_loop(void)
2380   {
2381     if (ppp_fd != slave_fd)
2382       {
2383         (void) ioctl(ppp_fd, TIOCSETD, &tty_disc);
2384         set_ppp_fd(slave_fd);
2385       }
2386   }
2387
2388 /********************************************************************
2389  *
2390  * sifnpmode - Set the mode for handling packets for a given NP.
2391  */
2392
2393 int
2394 sifnpmode(u, proto, mode)
2395     int u;
2396     int proto;
2397     enum NPmode mode;
2398 {
2399     struct npioctl npi;
2400
2401     npi.protocol = proto;
2402     npi.mode     = mode;
2403     if (ioctl(ppp_fd, PPPIOCSNPMODE, (caddr_t) &npi) < 0)
2404       {
2405         if (! ok_error (errno))
2406           {
2407             syslog(LOG_ERR, "ioctl(PPPIOCSNPMODE, %d, %d): %m(%d)",
2408                    proto, mode, errno);
2409             syslog(LOG_ERR, "ppp_fd=%d slave_fd=%d\n", ppp_fd, slave_fd);
2410           }
2411         return 0;
2412       }
2413     return 1;
2414   }
2415
2416 \f
2417 #include <linux/ipx.h>
2418
2419 /********************************************************************
2420  *
2421  * sipxfaddr - Config the interface IPX networknumber
2422  */
2423
2424 int sipxfaddr (int unit, unsigned long int network, unsigned char * node )
2425   {
2426     int    result = 1;
2427
2428 #ifdef IPX_CHANGE
2429     int    skfd; 
2430     struct sockaddr_ipx  ipx_addr;
2431     struct ifreq         ifr;
2432     struct sockaddr_ipx *sipx = (struct sockaddr_ipx *) &ifr.ifr_addr;
2433
2434     skfd = socket (AF_IPX, SOCK_DGRAM, 0);
2435     if (skfd < 0)
2436       { 
2437         if (! ok_error (errno))
2438           {
2439             syslog (LOG_DEBUG, "socket(AF_IPX): %m(%d)", errno);
2440           }
2441         result = 0;
2442       }
2443     else
2444       {
2445         memset (&ifr, '\0', sizeof (ifr));
2446         strcpy (ifr.ifr_name, ifname);
2447
2448         memcpy (sipx->sipx_node, node, IPX_NODE_LEN);
2449         sipx->sipx_family  = AF_IPX;
2450         sipx->sipx_port    = 0;
2451         sipx->sipx_network = htonl (network);
2452         sipx->sipx_type    = IPX_FRAME_ETHERII;
2453         sipx->sipx_action  = IPX_CRTITF;
2454 /*
2455  *  Set the IPX device
2456  */
2457         if (ioctl(skfd, SIOCSIFADDR, (caddr_t) &ifr) < 0)
2458           {
2459             result = 0;
2460             if (errno != EEXIST)
2461               {
2462                 if (! ok_error (errno))
2463                   {
2464                     syslog (LOG_DEBUG,
2465                             "ioctl(SIOCAIFADDR, CRTITF): %m(%d)", errno);
2466                   }
2467               }
2468             else
2469               {
2470                 syslog (LOG_WARNING,
2471                         "ioctl(SIOCAIFADDR, CRTITF): Address already exists");
2472               }
2473           }
2474         close (skfd);
2475       }
2476 #endif
2477     return result;
2478   }
2479
2480 /********************************************************************
2481  *
2482  * cipxfaddr - Clear the information for the IPX network. The IPX routes
2483  *             are removed and the device is no longer able to pass IPX
2484  *             frames.
2485  */
2486
2487 int cipxfaddr (int unit)
2488   {
2489     int    result = 1;
2490
2491 #ifdef IPX_CHANGE
2492     int    skfd; 
2493     struct sockaddr_ipx  ipx_addr;
2494     struct ifreq         ifr;
2495     struct sockaddr_ipx *sipx = (struct sockaddr_ipx *) &ifr.ifr_addr;
2496
2497     skfd = socket (AF_IPX, SOCK_DGRAM, 0);
2498     if (skfd < 0)
2499       { 
2500         if (! ok_error (errno))
2501           {
2502             syslog (LOG_DEBUG, "socket(AF_IPX): %m(%d)", errno);
2503           }
2504         result = 0;
2505       }
2506     else
2507       {
2508         memset (&ifr, '\0', sizeof (ifr));
2509         strcpy (ifr.ifr_name, ifname);
2510
2511         sipx->sipx_type    = IPX_FRAME_ETHERII;
2512         sipx->sipx_action  = IPX_DLTITF;
2513         sipx->sipx_family  = AF_IPX;
2514 /*
2515  *  Set the IPX device
2516  */
2517         if (ioctl(skfd, SIOCSIFADDR, (caddr_t) &ifr) < 0)
2518           {
2519             if (! ok_error (errno))
2520               {
2521                 syslog (LOG_INFO,
2522                         "ioctl(SIOCAIFADDR, IPX_DLTITF): %m(%d)", errno);
2523               }
2524             result = 0;
2525           }
2526         close (skfd);
2527       }
2528 #endif
2529     return result;
2530   }
2531
2532 /*
2533  * daemon - Detach us from controlling terminal session.
2534  */
2535 int
2536 daemon(nochdir, noclose)
2537     int nochdir, noclose;
2538 {
2539     int pid;
2540
2541     if ((pid = fork()) < 0)
2542         return -1;
2543     if (pid != 0)
2544         exit(0);                /* parent dies */
2545     setsid();
2546     if (!nochdir)
2547         chdir("/");
2548     if (!noclose) {
2549         fclose(stdin);          /* don't need stdin, stdout, stderr */
2550         fclose(stdout);
2551         fclose(stderr);
2552     }
2553     return 0;
2554 }
2555
2556 /********************************************************************
2557  *
2558  * sys_check_options - check the options that the user specified
2559  */
2560
2561 void
2562 sys_check_options(void)
2563   {
2564 #ifdef IPX_CHANGE
2565     struct stat stat_buf;
2566 /*
2567  * Disable the IPX protocol if the support is not present in the kernel.
2568  * If we disable it then ensure that IP support is enabled.
2569  */
2570     while (ipxcp_protent.enabled_flag)
2571       {
2572         if (path_to_procfs())
2573           {
2574             strcat (route_buffer, "/net/ipx_interface");
2575             if (lstat (route_buffer, &stat_buf) >= 0)
2576               {
2577                 break;
2578               }
2579           }
2580         syslog (LOG_ERR, "IPX support is not present in the kernel\n");
2581         ipxcp_protent.enabled_flag = 0;
2582         ipcp_protent.enabled_flag  = 1;
2583         break;
2584       }
2585 #endif
2586   }