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