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