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