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