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