]> git.ozlabs.org Git - ppp.git/blob - pppd/sys-linux.c
changed s to sockfd and added lock() and unlock();
[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 /*
22  * TODO:
23  */
24
25 #include <stdio.h>
26 #include <syslog.h>
27 #include <string.h>
28 #include <time.h>
29 #include <memory.h>
30 #include <utmp.h>
31 #include <sys/ioctl.h>
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <sys/time.h>
35 #include <sys/errno.h>
36 #include <mntent.h>
37
38 #include <net/if.h>
39 #include <net/ppp_defs.h>
40 #include <net/if_arp.h>
41 #include <linux/ppp.h>
42 #include <linux/route.h>
43 #include <linux/if_ether.h>
44 #include <netinet/in.h>
45 #include <signal.h>
46
47 #include "pppd.h"
48 #include "fsm.h"
49 #include "ipcp.h"
50
51 static int initdisc = -1;       /* Initial TTY discipline */
52 static int prev_kdebugflag = 0;
53
54 static int restore_term;                /* 1 => we've munged the terminal */
55 static struct termios inittermios;      /* Initial TTY termios */
56
57 int sockfd;                     /* socket for doing interface ioctls */
58
59 static int driver_version = 0;
60 static int driver_modification = 0;
61 static int driver_patch = 0;
62
63 static char *lock_file;
64
65 #define MAX_IFS         32
66
67 /*
68  * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
69  * if it exists.
70  */
71
72 #define SET_SA_FAMILY(addr, family)                     \
73     memset ((char *) &(addr), '\0', sizeof(addr));      \
74     addr.sa_family = (family);
75
76 /*
77  * sys_init - System-dependent initialization.
78  */
79
80 void sys_init(void)
81 {
82     openlog("pppd", LOG_PID | LOG_NDELAY, LOG_PPP);
83     setlogmask(LOG_UPTO(LOG_INFO));
84     if (debug)
85         setlogmask(LOG_UPTO(LOG_DEBUG));
86
87     /* Get an internet socket for doing socket ioctl's on. */
88     if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
89         syslog(LOG_ERR, "Couldn't create IP socket: %m");
90         die(1);
91     }
92 }
93
94 /*
95  * ccp_fatal_error - returns 1 if decompression was disabled as a
96  * result of an error detected after decompression of a packet,
97  * 0 otherwise.  This is necessary because of patent nonsense.
98  */
99 int ccp_fatal_error (int unit)
100 {
101     int x;
102
103     if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
104         syslog(LOG_ERR, "ioctl(PPPIOCGFLAGS): %m");
105         return 0;
106     }
107     return x & SC_DC_FERROR;
108 }
109
110 /*
111  * note_debug_level - note a change in the debug level.
112  */
113
114 void note_debug_level(void)
115 {
116     if (debug) {
117         syslog(LOG_INFO, "Debug turned ON, Level %d", debug);
118         setlogmask(LOG_UPTO(LOG_DEBUG));
119     } else {
120         setlogmask(LOG_UPTO(LOG_WARNING));
121     }
122 }
123
124 /*
125  * set_kdebugflag - Define the debugging level for the kernel
126  */
127
128 int set_kdebugflag (int requested_level)
129 {
130     if (ioctl(fd, PPPIOCGDEBUG, &prev_kdebugflag) < 0) {
131         syslog(LOG_ERR, "ioctl(PPPIOCGDEBUG): %m");
132         return (0);
133     }
134
135     if (prev_kdebugflag != requested_level) {
136         if (ioctl(fd, PPPIOCSDEBUG, &requested_level) < 0) {
137             syslog (LOG_ERR, "ioctl(PPPIOCSDEBUG): %m");
138             return (0);
139         }
140         syslog(LOG_INFO, "set kernel debugging level to %d", requested_level);
141     }
142     return (1);
143 }
144
145 /*
146  * establish_ppp - Turn the serial port into a ppp interface.
147  */
148
149 void establish_ppp (void)
150 {
151     int pppdisc = N_PPP;
152     int sig     = SIGIO;
153
154     if (ioctl(fd, TIOCEXCL, 0) < 0) {
155         syslog (LOG_WARNING, "ioctl(TIOCEXCL): %m");
156     }
157
158     if (ioctl(fd, TIOCGETD, &initdisc) < 0) {
159         syslog(LOG_ERR, "ioctl(TIOCGETD): %m");
160         die (1);
161     }
162     
163     if (ioctl(fd, TIOCSETD, &pppdisc) < 0) {
164         syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
165         die (1);
166     }
167     if (ioctl(fd, PPPIOCGUNIT, &ifunit) < 0) {
168         syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m");
169         die (1);
170     }
171
172     set_kdebugflag (kdebugflag);
173
174     syslog (LOG_NOTICE, "Using version %d.%d.%d of PPP driver",
175             driver_version, driver_modification, driver_patch);
176 }
177
178 /*
179  * disestablish_ppp - Restore the serial port to normal operation.
180  * This shouldn't call die() because it's called from die().
181  */
182
183 void disestablish_ppp(void)
184 {
185     int x;
186     char *s;
187
188     if (initdisc >= 0) {
189         set_kdebugflag (prev_kdebugflag);
190         /*
191          * Check whether the link seems not to be 8-bit clean.
192          */
193         if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) == 0) {
194             s = NULL;
195             switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP)) {
196             case SC_RCV_B7_0:
197                 s = "bit 7 set to 1";
198                 break;
199             case SC_RCV_B7_1:
200                 s = "bit 7 set to 0";
201                 break;
202             case SC_RCV_EVNP:
203                 s = "odd parity";
204                 break;
205             case SC_RCV_ODDP:
206                 s = "even parity";
207                 break;
208             }
209             if (s != NULL) {
210                 syslog(LOG_WARNING, "Serial link is not 8-bit clean:");
211                 syslog(LOG_WARNING, "All received characters had %s", s);
212             }
213         }
214
215         if (ioctl(fd, TIOCSETD, &initdisc) < 0)
216             syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
217
218         if (ioctl(fd, TIOCNXCL, 0) < 0)
219             syslog (LOG_WARNING, "ioctl(TIOCNXCL): %m");
220
221         initdisc = -1;
222     }
223 }
224
225 /*
226  * List of valid speeds.
227  */
228 struct speed {
229     unsigned int speed_int;
230     int          speed_val;
231 } speeds[] = {
232 #ifdef B50
233     { 50U, B50 },
234 #endif
235 #ifdef B75
236     { 75U, B75 },
237 #endif
238 #ifdef B110
239     { 110U, B110 },
240 #endif
241 #ifdef B134
242     { 134U, B134 },
243 #endif
244 #ifdef B150
245     { 150U, B150 },
246 #endif
247 #ifdef B200
248     { 200U, B200 },
249 #endif
250 #ifdef B300
251     { 300U, B300 },
252 #endif
253 #ifdef B600
254     { 600U, B600 },
255 #endif
256 #ifdef B1200
257     { 1200U, B1200 },
258 #endif
259 #ifdef B1800
260     { 1800U, B1800 },
261 #endif
262 #ifdef B2000
263     { 2000U, B2000 },
264 #endif
265 #ifdef B2400
266     { 2400U, B2400 },
267 #endif
268 #ifdef B3600
269     { 3600U, B3600 },
270 #endif
271 #ifdef B4800
272     { 4800U, B4800 },
273 #endif
274 #ifdef B7200
275     { 7200U, B7200 },
276 #endif
277 #ifdef B9600
278     { 9600U, B9600 },
279 #endif
280 #ifdef B19200
281     { 19200U, B19200 },
282 #endif
283 #ifdef B38400
284     { 38400U, B38400 },
285 #endif
286 #ifdef EXTA
287     { 19200U, EXTA },
288 #endif
289 #ifdef EXTB
290     { 38400U, EXTB },
291 #endif
292 #ifdef B57600
293     { 57600U, B57600 },
294 #endif
295 #ifdef B115200
296     { 115200U, B115200 },
297 #endif
298     { 0U, 0 }
299 };
300
301 /*
302  * Translate from bits/second to a speed_t.
303  */
304 int translate_speed (unsigned int bps)
305 {
306     struct speed *speedp;
307
308     if (bps == 0U)
309         return 0;
310     for (speedp = speeds; speedp->speed_int; speedp++)
311         if (bps == speedp->speed_int)
312             return speedp->speed_val;
313     syslog(LOG_WARNING, "speed %u not supported", bps);
314     return 0;
315 }
316
317 /*
318  * Translate from a speed_t to bits/second.
319  */
320
321 unsigned int baud_rate_of (int speed)
322 {
323     struct speed *speedp;
324
325     if (speed == 0)
326         return 0;
327     for (speedp = speeds; speedp->speed_int; speedp++)
328         if (speed == speedp->speed_val)
329             return speedp->speed_int;
330     return 0;
331 }
332
333 /*
334  * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
335  * at the requested speed, etc.  If `local' is true, set CLOCAL
336  * regardless of whether the modem option was specified.
337  */
338
339 void set_up_tty (int fd, int local)
340 {
341     unsigned int speed;
342     int x;
343     struct termios tios;
344
345     if (tcgetattr(fd, &tios) < 0) {
346         syslog(LOG_ERR, "tcgetattr: %m");
347         die(1);
348     }
349
350     if (!restore_term)
351         inittermios = tios;
352
353     tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL | CRTSCTS);
354     if (crtscts == 1)
355         tios.c_cflag |= CRTSCTS;
356
357     tios.c_cflag |= CS8 | CREAD | HUPCL;
358     if (local || !modem)
359         tios.c_cflag |= CLOCAL;
360     tios.c_iflag = IGNBRK | IGNPAR;
361     tios.c_oflag = 0;
362     tios.c_lflag = 0;
363     tios.c_cc[VMIN] = 1;
364     tios.c_cc[VTIME] = 0;
365
366     if (crtscts == 2) {
367         tios.c_iflag |= IXOFF;
368         tios.c_cc[VSTOP] = 0x13;        /* DC3 = XOFF = ^S */
369         tios.c_cc[VSTART] = 0x11;       /* DC1 = XON  = ^Q */
370     }
371
372     speed = translate_speed(inspeed);
373     if (speed) {
374         cfsetospeed(&tios, speed);
375         cfsetispeed(&tios, speed);
376     } else {
377         speed = cfgetospeed(&tios);
378         /*
379          * We can't proceed if the serial port speed is B0,
380          * since that implies that the serial port is disabled.
381          */
382         if (speed == B0) {
383             syslog(LOG_ERR, "Baud rate for %s is 0; need explicit baud rate",
384                    devnam);
385             die(1);
386         }
387     }
388
389     if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
390         syslog(LOG_ERR, "tcsetattr: %m");
391         die(1);
392     }
393
394     baud_rate = baud_rate_of(speed);
395     restore_term = TRUE;
396 }
397
398 /*
399  * setdtr - control the DTR line on the serial port.
400  * This is called from die(), so it shouldn't call die().
401  */
402 void setdtr (int fd, int on)
403 {
404     int modembits = TIOCM_DTR;
405
406     ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
407 }
408
409 /*
410  * restore_tty - restore the terminal to the saved settings.
411  */
412 void restore_tty(void)
413 {
414     if (restore_term) {
415         if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
416             if (errno != ENXIO)
417                 syslog(LOG_WARNING, "tcsetattr: %m");
418         restore_term = 0;
419     }
420 }
421
422 /*
423  * output - Output PPP packet.
424  */
425
426 void output (int unit, unsigned char *p, int len)
427 {
428     if (unit != 0)
429         MAINDEBUG((LOG_WARNING, "output: unit != 0!"));
430
431     if (debug)
432         log_packet(p, len, "sent ");
433     
434     if (write(fd, p, len) < 0) {
435         syslog(LOG_ERR, "write: %m");
436         die(1);
437     }
438 }
439
440 /*
441  * wait_input - wait until there is data available on fd,
442  * for the length of time specified by *timo (indefinite
443  * if timo is NULL).
444  */
445
446 void wait_input (struct timeval *timo)
447 {
448     fd_set ready;
449     int n;
450
451     FD_ZERO(&ready);
452     FD_SET(fd, &ready);
453     n = select(fd+1, &ready, NULL, &ready, timo);
454     if (n < 0 && errno != EINTR) {
455         syslog(LOG_ERR, "select: %m");
456         die(1);
457     }
458 }
459
460
461 /*
462  * read_packet - get a PPP packet from the serial device.
463  */
464
465 int read_packet (unsigned char *buf)
466 {
467     int len;
468   
469     len = read(fd, buf, PPP_MTU + PPP_HDRLEN);
470     if (len < 0) {
471         if (errno == EWOULDBLOCK) {
472             return -1;
473         }
474         syslog(LOG_ERR, "read(fd): %m");
475         die(1);
476     }
477     return len;
478 }
479
480 /*
481  * ppp_send_config - configure the transmit characteristics of
482  * the ppp interface.
483  */
484 void ppp_send_config (int unit,int mtu,u_int32_t asyncmap,int pcomp,int accomp)
485 {
486     u_int x;
487     struct ifreq ifr;
488   
489     MAINDEBUG ((LOG_DEBUG, "send_config: mtu = %d\n", mtu));
490     strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
491     ifr.ifr_mtu = mtu;
492     if (ioctl(sockfd, SIOCSIFMTU, (caddr_t) &ifr) < 0) {
493         syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m");
494         quit();
495     }
496
497     MAINDEBUG ((LOG_DEBUG, "send_config: asyncmap = %lx\n", asyncmap));
498     if (ioctl(fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0) {
499         syslog(LOG_ERR, "ioctl(PPPIOCSASYNCMAP): %m");
500         quit();
501     }
502     
503     if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
504         syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
505         quit();
506     }
507
508     x = pcomp  ? x | SC_COMP_PROT : x & ~SC_COMP_PROT;
509     x = accomp ? x | SC_COMP_AC   : x & ~SC_COMP_AC;
510
511     MAINDEBUG ((LOG_DEBUG, "send_config: flags = %x\n", x));
512     if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
513         syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
514         quit();
515     }
516 }
517
518 /*
519  * ppp_set_xaccm - set the extended transmit ACCM for the interface.
520  */
521 void ppp_set_xaccm (int unit, ext_accm accm)
522 {
523     MAINDEBUG ((LOG_DEBUG, "set_xaccm: %08lx %08lx %08lx %08lx\n",
524                 accm[0], accm[1], accm[2], accm[3]));
525     if (ioctl(fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY)
526         syslog(LOG_WARNING, "ioctl(set extended ACCM): %m");
527 }
528
529 /*
530  * ppp_recv_config - configure the receive-side characteristics of
531  * the ppp interface.
532  */
533 void ppp_recv_config (int unit,int mru,u_int32_t asyncmap,int pcomp,int accomp)
534 {
535     u_int x;
536
537     MAINDEBUG ((LOG_DEBUG, "recv_config: mru = %d\n", mru));
538     if (ioctl(fd, PPPIOCSMRU, (caddr_t) &mru) < 0)
539         syslog(LOG_ERR, "ioctl(PPPIOCSMRU): %m");
540
541     MAINDEBUG ((LOG_DEBUG, "recv_config: asyncmap = %lx\n", asyncmap));
542     if (ioctl(fd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0) {
543         syslog(LOG_ERR, "ioctl(PPPIOCSRASYNCMAP): %m");
544         quit();
545     }
546   
547     if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
548         syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
549         quit();
550     }
551
552     x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC;
553     MAINDEBUG ((LOG_DEBUG, "recv_config: flags = %x\n", x));
554     if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
555         syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
556         quit();
557     }
558 }
559
560 /*
561  * ccp_test - ask kernel whether a given compression method
562  * is acceptable for use.
563  */
564 int ccp_test (int unit, u_char *opt_ptr, int opt_len, int for_transmit)
565 {
566     struct ppp_option_data data;
567
568     data.ptr = opt_ptr;
569     data.length = opt_len;
570     data.transmit = for_transmit;
571     return ioctl(fd, PPPIOCSCOMPRESS, (caddr_t) &data) >= 0;
572 }
573
574 /*
575  * ccp_flags_set - inform kernel about the current state of CCP.
576  */
577 void ccp_flags_set (int unit, int isopen, int isup)
578 {
579     int x;
580
581     if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
582         syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
583         return;
584     }
585     x = isopen? x | SC_CCP_OPEN: x &~ SC_CCP_OPEN;
586     x = isup? x | SC_CCP_UP: x &~ SC_CCP_UP;
587     if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
588         syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
589 }
590
591 /*
592  * sifvjcomp - config tcp header compression
593  */
594
595 int sifvjcomp (int u, int vjcomp, int cidcomp, int maxcid)
596 {
597     u_int x;
598
599     if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
600         syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
601         return 0;
602     }
603
604     x = vjcomp  ? x | SC_COMP_TCP     : x &~ SC_COMP_TCP;
605     x = cidcomp ? x & ~SC_NO_TCP_CCID : x | SC_NO_TCP_CCID;
606
607     if(ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
608         syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
609         return 0;
610     }
611
612     if (vjcomp) {
613         if (ioctl (fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0) {
614             syslog (LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
615             return 0;
616         }
617     }
618
619     return 1;
620 }
621
622 /*
623  * sifup - Config the interface up and enable IP packets to pass.
624  */
625
626 int sifup (int u)
627 {
628     struct ifreq ifr;
629
630     strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
631     if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
632         syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
633         return 0;
634     }
635
636     ifr.ifr_flags |= (IFF_UP | IFF_POINTOPOINT);
637     if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
638         syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
639         return 0;
640     }
641     return 1;
642 }
643
644 /*
645  * sifdown - Config the interface down and disable IP.
646  */
647
648 int sifdown (int u)
649 {
650     struct ifreq ifr;
651
652     strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
653     if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
654         syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
655         return 0;
656     }
657
658     ifr.ifr_flags &= ~IFF_UP;
659     ifr.ifr_flags |= IFF_POINTOPOINT;
660     if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
661         syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
662         return 0;
663     }
664     return 1;
665 }
666
667 /*
668  * sifaddr - Config the interface IP addresses and netmask.
669  */
670
671 int sifaddr (int unit, int our_adr, int his_adr, int net_mask)
672 {
673     struct ifreq   ifr; 
674     struct rtentry rt;
675     
676     SET_SA_FAMILY (ifr.ifr_addr,    AF_INET); 
677     SET_SA_FAMILY (ifr.ifr_dstaddr, AF_INET); 
678     SET_SA_FAMILY (ifr.ifr_netmask, AF_INET); 
679
680     strncpy (ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
681 /*
682  *  Set our IP address
683  */
684     ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = our_adr;
685     if (ioctl(sockfd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
686         if (errno != EEXIST)
687             syslog (LOG_ERR, "ioctl(SIOCAIFADDR): %m");
688         else
689             syslog (LOG_WARNING, "ioctl(SIOCAIFADDR): Address already exists");
690         return (0);
691     } 
692 /*
693  *  Set the gateway address
694  */
695     ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = his_adr;
696     if (ioctl(sockfd, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
697         syslog (LOG_ERR, "ioctl(SIOCSIFDSTADDR): %m"); 
698         return (0);
699     } 
700 /*
701  *  Set the netmask
702  */
703     if (net_mask != 0) {
704         ((struct sockaddr_in *) &ifr.ifr_netmask)->sin_addr.s_addr = net_mask;
705         if (ioctl(sockfd, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
706             syslog (LOG_ERR, "ioctl(SIOCSIFNETMASK): %m"); 
707             return (0);
708         } 
709     }
710 /*
711  *  Add the device route
712  */
713     memset (&rt, '\0', sizeof (rt));
714
715     SET_SA_FAMILY (rt.rt_dst,     AF_INET);
716     SET_SA_FAMILY (rt.rt_gateway, AF_INET);
717     rt.rt_dev = ifname;  /* MJC */
718
719     ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = 0;
720     ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr     = his_adr;
721     rt.rt_flags = RTF_UP | RTF_HOST;
722
723     if (ioctl(sockfd, SIOCADDRT, &rt) < 0) {
724         syslog (LOG_ERR, "ioctl(SIOCADDRT) device route: %m");
725         return (0);
726     }
727     return 1;
728 }
729
730 /*
731  * cifaddr - Clear the interface IP addresses, and delete routes
732  * through the interface if possible.
733  */
734
735 int cifaddr (int unit, int our_adr, int his_adr)
736 {
737     struct rtentry rt;
738 /*
739  *  Delete the route through the device
740  */
741     memset (&rt, '\0', sizeof (rt));
742
743     SET_SA_FAMILY (rt.rt_dst,     AF_INET);
744     SET_SA_FAMILY (rt.rt_gateway, AF_INET);
745     rt.rt_dev = ifname;  /* MJC */
746
747     ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = 0;
748     ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr     = his_adr;
749     rt.rt_flags = RTF_UP | RTF_HOST;
750
751     if (ioctl(sockfd, SIOCDELRT, &rt) < 0) {
752         syslog (LOG_ERR, "ioctl(SIOCDELRT) device route: %m");
753         return (0);
754     }
755     return 1;
756 }
757
758 /*
759  * path_to_route - determine the path to the proc file system data
760  */
761
762 FILE *route_fd = (FILE *) 0;
763 static char route_buffer [100];
764
765 static char *path_to_route (void);
766 static int open_route_table (void);
767 static void close_route_table (void);
768 static int read_route_table (struct rtentry *rt);
769 static int defaultroute_exists (void);
770
771 /*
772  * path_to_route - find the path to the route tables in the proc file system
773  */
774
775 static char *path_to_route (void)
776 {
777     struct mntent *mntent;
778     FILE *fp;
779
780     fp = fopen (MOUNTED, "r");
781     if (fp != 0) {
782         while ((mntent = getmntent (fp)) != 0) {
783             if (strcmp (mntent->mnt_type, MNTTYPE_IGNORE) == 0)
784                 continue;
785
786             if (strcmp (mntent->mnt_type, "proc") == 0) {
787                 strncpy (route_buffer, mntent->mnt_dir,
788                          sizeof (route_buffer)-10);
789                 route_buffer [sizeof (route_buffer)-10] = '\0';
790                 strcat (route_buffer, "/net/route");
791
792                 fclose (fp);
793                 return (route_buffer);
794             }
795         }
796         fclose (fp);
797     }
798     syslog (LOG_ERR, "proc file system not mounted");
799     return 0;
800 }
801
802 /*
803  * open_route_table - open the interface to the route table
804  */
805
806 static int open_route_table (void)
807 {
808     char *path;
809
810     if (route_fd != (FILE *) 0)
811         close_route_table();
812
813     path = path_to_route();
814     if (path == NULL)
815         return 0;
816
817     route_fd = fopen (path, "r");
818     if (route_fd == (FILE *) 0) {
819         syslog (LOG_ERR, "can not open %s: %m", path);
820         return 0;
821     }
822
823     /* read and discard the header line. */
824     if (fgets (route_buffer, sizeof (route_buffer), route_fd) == (char *) 0) {
825         close_route_table();
826         return 0;
827     }
828     return 1;
829 }
830
831 /*
832  * close_route_table - close the interface to the route table
833  */
834
835 static void close_route_table (void)
836 {
837     if (route_fd != (FILE *) 0) {
838         fclose (route_fd);
839         route_fd = (FILE *) 0;
840     }
841 }
842
843 /*
844  * read_route_table - read the next entry from the route table
845  */
846
847 static int read_route_table (struct rtentry *rt)
848 {
849     static char delims[] = " \t\n";
850     char *dev_ptr, *ptr, *dst_ptr, *gw_ptr, *flag_ptr;
851
852     if (fgets (route_buffer, sizeof (route_buffer), route_fd) == (char *) 0)
853         return 0;
854
855     memset (rt, '\0', sizeof (struct rtentry));
856
857     dev_ptr  = strtok (route_buffer, delims); /* interface name */
858     dst_ptr  = strtok (NULL,         delims); /* destination address */
859     gw_ptr   = strtok (NULL,         delims); /* gateway */
860     flag_ptr = strtok (NULL,         delims); /* flags */
861
862     ((struct sockaddr_in *) &rt->rt_dst)->sin_addr.s_addr =
863       strtoul (dst_ptr, NULL, 16); /* network order */
864
865     ((struct sockaddr_in *) &rt->rt_gateway)->sin_addr.s_addr =
866       strtoul (gw_ptr, NULL, 16);  /* network order */
867
868     rt->rt_flags = (short) strtoul (flag_ptr, NULL, 16);
869     rt->rt_dev   = dev_ptr;
870
871     return 1;
872 }
873
874 /*
875  * defaultroute_exists - determine if there is a default route
876  */
877
878 static int defaultroute_exists (void)
879 {
880     struct rtentry rt;
881     int    result = 0;
882
883     if (!open_route_table())
884         return 0;
885
886     while (read_route_table(&rt) != 0) {
887         if (rt.rt_flags & RTF_UP == 0)
888             continue;
889
890         if (((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr == 0L) {
891             syslog (LOG_ERR,
892                     "ppp not replacing existing default route to %s[%s]",
893                     rt.rt_dev,
894                     inet_ntoa (((struct sockaddr_in *) &rt.rt_gateway)->
895                                sin_addr.s_addr));
896             result = 1;
897             break;
898         }
899     }
900     close_route_table();
901     return result;
902 }
903
904 /*
905  * sifdefaultroute - assign a default route through the address given.
906  */
907
908 int sifdefaultroute (int unit, int gateway)
909 {
910     struct rtentry rt;
911
912     if (defaultroute_exists())
913         return 0;
914
915     memset (&rt, '\0', sizeof (rt));
916     SET_SA_FAMILY (rt.rt_dst,     AF_INET);
917     SET_SA_FAMILY (rt.rt_gateway, AF_INET);
918     ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = gateway;
919     
920     rt.rt_flags = RTF_UP | RTF_GATEWAY;
921     if (ioctl(sockfd, SIOCADDRT, &rt) < 0) {
922         syslog (LOG_ERR, "default route ioctl(SIOCADDRT): %m");
923         return 0;
924     }
925     return 1;
926 }
927
928 /*
929  * cifdefaultroute - delete a default route through the address given.
930  */
931
932 int cifdefaultroute (int unit, int gateway)
933 {
934     struct rtentry rt;
935   
936     SET_SA_FAMILY (rt.rt_dst,     AF_INET);
937     SET_SA_FAMILY (rt.rt_gateway, AF_INET);
938     ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = gateway;
939     
940     rt.rt_flags = RTF_UP | RTF_GATEWAY;
941     if (ioctl(sockfd, SIOCDELRT, &rt) < 0) {
942         syslog (LOG_ERR, "default route ioctl(SIOCDELRT): %m");
943         return 0;
944     }
945     return 1;
946 }
947
948 /*
949  * sifproxyarp - Make a proxy ARP entry for the peer.
950  */
951
952 int sifproxyarp (int unit, u_int32_t his_adr)
953 {
954     struct arpreq arpreq;
955
956     memset (&arpreq, '\0', sizeof(arpreq));
957 /*
958  * Get the hardware address of an interface on the same subnet
959  * as our local address.
960  */
961     if (!get_ether_addr(his_adr, &arpreq.arp_ha)) {
962         syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
963         return 0;
964     }
965     
966     SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
967     ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = his_adr;
968     arpreq.arp_flags = ATF_PERM | ATF_PUBL;
969     
970     if (ioctl(sockfd, SIOCSARP, (caddr_t)&arpreq) < 0) {
971         syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
972         return 0;
973     }
974     return 1;
975 }
976
977 /*
978  * cifproxyarp - Delete the proxy ARP entry for the peer.
979  */
980
981 int cifproxyarp (int unit, u_int32_t his_adr)
982 {
983     struct arpreq arpreq;
984   
985     memset (&arpreq, '\0', sizeof(arpreq));
986     SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
987     
988     ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = his_adr;
989     if (ioctl(sockfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
990         syslog(LOG_WARNING, "ioctl(SIOCDARP): %m");
991         return 0;
992     }
993     return 1;
994 }
995      
996 /*
997  * get_ether_addr - get the hardware address of an interface on the
998  * the same subnet as ipaddr.
999  */
1000
1001 int get_ether_addr (u_int32_t ipaddr, struct sockaddr *hwaddr)
1002 {
1003     struct ifreq *ifr, *ifend, *ifp;
1004     int i;
1005     u_int32_t ina, mask;
1006     struct sockaddr_dl *dla;
1007     struct ifreq ifreq;
1008     struct ifconf ifc;
1009     struct ifreq ifs[MAX_IFS];
1010     
1011     ifc.ifc_len = sizeof(ifs);
1012     ifc.ifc_req = ifs;
1013     if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1014         syslog(LOG_ERR, "ioctl(SIOCGIFCONF): %m");
1015         return 0;
1016     }
1017     MAINDEBUG ((LOG_DEBUG, "proxy arp: scanning %d interfaces for IP %s",
1018                 ifc.ifc_len / sizeof(struct ifreq), ip_ntoa(ipaddr)));
1019 /*
1020  * Scan through looking for an interface with an Internet
1021  * address on the same subnet as `ipaddr'.
1022  */
1023     ifend = ifs + (ifc.ifc_len / sizeof(struct ifreq));
1024     for (ifr = ifc.ifc_req; ifr < ifend; ifr++) {
1025         if (ifr->ifr_addr.sa_family == AF_INET) {
1026             ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1027             strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1028             MAINDEBUG ((LOG_DEBUG, "proxy arp: examining interface %s",
1029                         ifreq.ifr_name));
1030 /*
1031  * Check that the interface is up, and not point-to-point
1032  * or loopback.
1033  */
1034             if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1035                 continue;
1036             if ((ifreq.ifr_flags &
1037                  (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
1038                 != (IFF_UP|IFF_BROADCAST))
1039                 continue;
1040 /*
1041  * Get its netmask and check that it's on the right subnet.
1042  */
1043             if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1044                 continue;
1045             mask = ((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr;
1046             MAINDEBUG ((LOG_DEBUG, "proxy arp: interface addr %s mask %lx",
1047                         ip_ntoa(ina), ntohl(mask)));
1048             if (((ipaddr ^ ina) & mask) != 0)
1049                 continue;
1050             break;
1051         }
1052     }
1053     
1054     if (ifr >= ifend)
1055         return 0;
1056
1057     syslog(LOG_INFO, "found interface %s for proxy arp", ifreq.ifr_name);
1058 /*
1059  * Now get the hardware address.
1060  */
1061     if (ioctl (s, SIOCGIFHWADDR, &ifreq) < 0) {
1062         syslog(LOG_ERR, "SIOCGIFHWADDR(%s): %m", ifreq.ifr_name);
1063         return 0;
1064     }
1065
1066     /* Use the proper family for token ring support. */
1067     if (ifreq.ifr_hwaddr.sa_family == 0)
1068         hwaddr->sa_family = ARPHRD_ETHER;
1069     else
1070         hwaddr->sa_family = ifreq.ifr_hwaddr.sa_family;
1071
1072     /* Well, ethernet has the longest address so far . . . . */
1073     memcpy (&hwaddr->sa_data, &ifreq.ifr_hwaddr.sa_data, ETH_ALEN);
1074
1075     MAINDEBUG ((LOG_DEBUG,
1076                 "proxy arp: found hwaddr %02x:%02x:%02x:%02x:%02x:%02x",
1077                 (int) ((unsigned char *) &hwaddr->sa_data)[0],
1078                 (int) ((unsigned char *) &hwaddr->sa_data)[1],
1079                 (int) ((unsigned char *) &hwaddr->sa_data)[2],
1080                 (int) ((unsigned char *) &hwaddr->sa_data)[3],
1081                 (int) ((unsigned char *) &hwaddr->sa_data)[4],
1082                 (int) ((unsigned char *) &hwaddr->sa_data)[5]));
1083     return 1;
1084 }
1085
1086 /*
1087  * Internal routine to decode the version.modification.patch level
1088  */
1089
1090 static void decode_version (char *buf, int *version,
1091                             int *modification, int *patch)
1092 {
1093
1094   *version      = (int) strtoul (buf, &buf, 10);
1095   *modification = 0;
1096   *patch        = 0;
1097
1098   if (*buf == '.') {
1099       ++buf;
1100       *modification = (int) strtoul (buf, &buf, 10);
1101       if (*buf == '.') {
1102           ++buf;
1103           *patch = (int) strtoul (buf, &buf, 10);
1104       }
1105   }
1106
1107   if (*buf != '\0') {
1108       *version      =
1109       *modification =
1110       *patch        = 0;
1111   }
1112 }
1113
1114 /*
1115  * ppp_available - check whether the system has any ppp interfaces
1116  * (in fact we check whether we can do an ioctl on ppp0).
1117  */
1118
1119 int ppp_available(void)
1120 {
1121     int s, ok;
1122     struct ifreq ifr;
1123     int my_version, my_modification, my_patch;
1124 /*
1125  * Open a socket for doing the ioctl operations.
1126  */    
1127     s = socket(AF_INET, SOCK_DGRAM, 0);
1128     if (s < 0)
1129         return 0;
1130     
1131     strncpy (ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
1132     ok = ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
1133 /*
1134  * Ensure that the hardware address is for PPP and not something else
1135  */
1136     if (ok != 0)
1137         ok = ioctl (s, SIOCGIFHWADDR, (caddr_t) &ifr) >= 0;
1138
1139     if (ok != 0 && ifr.ifr_hwaddr.sa_family != ARPHRD_PPP)
1140         ok = 0;
1141 /*
1142  *  This is the PPP device. Validate the version of the driver at this
1143  *  point to ensure that this program will work with the driver.
1144  */
1145     if (ok != 0) {
1146         char   abBuffer [1024];
1147         int    size;
1148
1149         ifr.ifr_data = abBuffer;
1150         size = ioctl (s, SIOCGPPPVER, (caddr_t) &ifr);
1151         ok   = size >= 0;
1152
1153         if (ok != 0) {
1154             decode_version (abBuffer,
1155                             &driver_version,
1156                             &driver_modification,
1157                             &driver_patch);
1158         }
1159     }
1160
1161     if (ok == 0) {
1162         driver_version      =
1163         driver_modification =
1164         driver_patch        = 0;
1165     }
1166 /*
1167  * Validate the version of the driver against the version that we used.
1168  */
1169     decode_version (PPP_VERSION,
1170                     &my_version,
1171                     &my_modification,
1172                     &my_patch);
1173
1174     /* The version numbers must match */
1175     if (driver_version != my_version)
1176         ok = 0;
1177       
1178     /* The modification levels must be legal */
1179     if (driver_modification < my_modification)
1180         ok = 0;
1181
1182     close(s);
1183
1184     if (ok == 0) {
1185         fprintf(stderr, "Sorry - PPP driver version %d.%d.%d is out of date\n",
1186                 driver_version, driver_modification, driver_patch);
1187         exit (1); /* avoid confusing error message 'ppp is not supported'. */
1188     }
1189
1190     return ok;
1191 }
1192
1193 int logwtmp (char *line, char *name, char *host)
1194 {
1195     struct utmp ut;
1196
1197     memset (&ut, 0, sizeof (ut));
1198     (void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
1199     (void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
1200     (void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
1201     (void)time(&ut.ut_time);
1202         
1203     pututline (&ut);            /* Write the line to the proper place */
1204     endutent();                 /* Indicate operation is complete */
1205 }
1206
1207 /*
1208  * Code for locking/unlocking the serial device.
1209  * This code is derived from chat.c.
1210  */
1211
1212 #define LOCKPREFIX      "/usr/spool/uucp/LCK.."
1213
1214 /*
1215  * lock - create a lock file for the named device
1216  */
1217 int
1218 lock(dev)
1219     char *dev;
1220 {
1221     char hdb_lock_buffer[12];
1222     int fd, pid, n;
1223     char *p;
1224
1225     if ((p = strrchr(dev, '/')) != NULL)
1226         dev = p + 1;
1227     lock_file = malloc(strlen(LOCK_PREFIX) + strlen(dev) + 1);
1228     if (lock_file == NULL)
1229         novm("lock file name");
1230     strcat(strcpy(lock_file, LOCK_PREFIX), dev);
1231
1232     while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
1233         if (errno == EEXIST
1234             && (fd = open(lock_file, O_RDONLY, 0)) >= 0) {
1235             /* Read the lock file to find out who has the device locked */
1236             n = read(fd, hdb_lock_buffer, 11);
1237             if (n > 0) {
1238                 hdb_lock_buffer[n] = 0;
1239                 pid = atoi(hdb_lock_buffer);
1240             }
1241             if (n <= 0) {
1242                 syslog(LOG_ERR, "Can't read pid from lock file %s", lock_file);
1243                 close(fd);
1244             } else {
1245                 if (kill(pid, 0) == -1 && errno == ESRCH) {
1246                     /* pid no longer exists - remove the lock file */
1247                     if (unlink(lock_file) == 0) {
1248                         close(fd);
1249                         syslog(LOG_NOTICE, "Removed stale lock on %s (pid %d)",
1250                                dev, pid);
1251                         continue;
1252                     } else
1253                         syslog(LOG_WARNING, "Couldn't remove stale lock on %s",
1254                                dev);
1255                 } else
1256                     syslog(LOG_NOTICE, "Device %s is locked by pid %d",
1257                            dev, pid);
1258             }
1259             close(fd);
1260         } else
1261             syslog(LOG_ERR, "Can't create lock file %s: %m", lock_file);
1262         free(lock_file);
1263         lock_file = NULL;
1264         return -1;
1265     }
1266
1267     sprintf(hdb_lock_buffer, "%10d\n", getpid());
1268     write(fd, hdb_lock_buffer, 11);
1269
1270     close(fd);
1271     return 0;
1272 }
1273
1274 /*
1275  * unlock - remove our lockfile
1276  */
1277 unlock()
1278 {
1279     if (lock_file) {
1280         unlink(lock_file);
1281         free(lock_file);
1282         lock_file = NULL;
1283     }
1284 }