]> git.ozlabs.org Git - ppp.git/blob - pppd/sys-ultrix.c
add a few defs
[ppp.git] / pppd / sys-ultrix.c
1 /*
2  * sys-ultrix.c - System-dependent procedures for setting up
3  * PPP interfaces on Ultrix systems.
4  *
5  * Copyright (c) 1989 Carnegie Mellon University.
6  * Copyright (c) 1995 The Australian National University.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms are permitted
10  * provided that the above copyright notice and this paragraph are
11  * duplicated in all such forms and that any documentation,
12  * advertising materials, and other materials related to such
13  * distribution and use acknowledge that the software was developed
14  * by Carnegie Mellon University and The Australian National University.
15  * The names of the Universities may not be used to endorse or promote
16  * products derived from this software without specific prior written
17  * permission.
18  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
20  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21  */
22
23 #ifndef lint
24 static char rcsid[] = "$Id: sys-ultrix.c,v 1.21 1997/04/30 05:59:38 paulus Exp $";
25 #endif
26
27 /*
28  * TODO:
29  */
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <syslog.h>
34 #include <string.h>
35 #include <stdlib.h>
36 #include <errno.h>
37 #include <fcntl.h>
38 #include <termios.h>
39 #include <utmp.h>
40 #include <sys/types.h>
41 #include <sys/file.h>
42 #include <sys/socket.h>
43 #include <sys/ioctl.h>
44 #include <sys/time.h>
45 #include <sys/errno.h>
46 #include <sys/stat.h>
47
48 #include <net/if.h>
49 #include <net/ppp_defs.h>
50 #include <net/if_ppp.h>
51 #include <net/route.h>
52 #include <netinet/in.h>
53
54 #include "pppd.h"
55
56 static int initdisc = -1;       /* Initial TTY discipline for ppp_fd */
57 static int initfdflags = -1;    /* Initial file descriptor flags for ppp_fd */
58 static int ppp_fd = -1;         /* fd which is set to PPP discipline */
59
60 static int restore_term;        /* 1 => we've munged the terminal */
61 static struct termios inittermios; /* Initial TTY termios */
62 static struct winsize wsinfo;   /* Initial window size info */
63
64 static char *lock_file;         /* name of lock file created */
65
66 static int loop_slave = -1;
67 static int loop_master;
68 static char loop_name[20];
69
70 static unsigned char inbuf[512]; /* buffer for chars read from loopback */
71
72 static int sockfd;              /* socket for doing interface ioctls */
73
74 static int if_is_up;            /* the interface is currently up */
75 static u_int32_t ifaddrs[2];    /* local and remote addresses */
76 static u_int32_t default_route_gateway; /* gateway addr for default route */
77 static u_int32_t proxy_arp_addr;        /* remote addr for proxy arp */
78
79 /* Prototypes for procedures local to this file. */
80 static int translate_speed __P((int));
81 static int baud_rate_of __P((int));
82 static int get_ether_addr __P((u_int32_t, struct sockaddr *));
83
84
85 /*
86  * sys_init - System-dependent initialization.
87  */
88 void
89 sys_init()
90 {
91     /* Get an internet socket for doing socket ioctl's on. */
92     if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
93         syslog(LOG_ERR, "Couldn't create IP socket: %m");
94         die(1);
95     }
96 }
97
98 /*
99  * sys_cleanup - restore any system state we modified before exiting:
100  * mark the interface down, delete default route and/or proxy arp entry.
101  * This should call die() because it's called from die().
102  */
103 void
104 sys_cleanup()
105 {
106     struct ifreq ifr;
107
108     if (if_is_up) {
109         strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
110         if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) >= 0
111             && ((ifr.ifr_flags & IFF_UP) != 0)) {
112             ifr.ifr_flags &= ~IFF_UP;
113             ioctl(sockfd, SIOCSIFFLAGS, &ifr);
114         }
115     }
116     if (ifaddrs[0])
117         cifaddr(0, ifaddrs[0], ifaddrs[1]);
118     if (default_route_gateway)
119         cifdefaultroute(0, 0, default_route_gateway);
120     if (proxy_arp_addr)
121         cifproxyarp(0, proxy_arp_addr);
122 }
123
124 /*
125  * sys_close - Clean up in a child process before execing.
126  */
127 void
128 sys_close()
129 {
130     close(sockfd);
131     if (loop_slave >= 0) {
132         close(loop_slave);
133         close(loop_master);
134     }
135 }
136
137 /*
138  * sys_check_options - check the options that the user specified
139  */
140 void
141 sys_check_options()
142 {
143     if (demand) {
144         option_error("Sorry - demand-dialling is not supported under Ultrix\n");
145         exit(1);
146     }
147 }
148
149
150 /*
151  * daemon - Detach us from the terminal session.
152  */
153 int
154 daemon(nochdir, noclose)
155     int nochdir, noclose;
156 {
157     int pid;
158
159     if ((pid = fork()) < 0)
160         return -1;
161     if (pid != 0)
162         exit(0);                /* parent dies */
163     setsid();
164     if (!nochdir)
165         chdir("/");
166     if (!noclose) {
167         fclose(stdin);          /* don't need stdin, stdout, stderr */
168         fclose(stdout);
169         fclose(stderr);
170     }
171     return 0;
172 }
173
174 /*
175  * ppp_available - check whether the system has any ppp interfaces
176  * (in fact we check whether we can do an ioctl on ppp0).
177  */
178 int
179 ppp_available()
180 {
181     int s, ok;
182     struct ifreq ifr;
183     extern char *no_ppp_msg;
184
185     if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
186         return 1;               /* can't tell */
187
188     strncpy(ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
189     ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
190     close(s);
191
192     no_ppp_msg = "\
193 This system lacks kernel support for PPP.  To include PPP support\n\
194 in the kernel, please follow the steps detailed in the README.ultrix\n\
195 file in the ppp-2.2 distribution.\n";
196     return ok;
197 }
198
199 /*
200  * establish_ppp - Turn the serial port into a ppp interface.
201  */
202 void
203 establish_ppp(fd)
204     int fd;
205 {
206     int pppdisc = PPPDISC;
207     int x;
208
209     /*
210      * Save the old line discipline of fd, and set it to PPP.
211      */
212     if (ioctl(fd, TIOCGETD, &initdisc) < 0) {
213         syslog(LOG_ERR, "ioctl(TIOCGETD): %m");
214         die(1);
215     }
216     if (ioctl(fd, TIOCSETD, &pppdisc) < 0) {
217         syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
218         die(1);
219     }
220
221     /*
222      * Find out which interface we were given.
223      */
224     if (ioctl(fd, PPPIOCGUNIT, &ifunit) < 0) {  
225         syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m");
226         die(1);
227     }
228
229     ppp_fd = fd;
230
231     /*
232      * Enable debug in the driver if requested.
233      */
234     if (kdebugflag) {
235         if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
236             syslog(LOG_WARNING, "ioctl (PPPIOCGFLAGS): %m");
237         } else {
238             x |= (kdebugflag & 0xFF) * SC_DEBUG;
239             if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
240                 syslog(LOG_WARNING, "ioctl(PPPIOCSFLAGS): %m");
241         }
242     }
243
244     /*
245      * Set device for non-blocking reads.
246      */
247     if ((initfdflags = fcntl(fd, F_GETFL)) == -1
248         || fcntl(fd, F_SETFL, initfdflags | O_NONBLOCK) == -1) {
249         syslog(LOG_WARNING, "Couldn't set device to non-blocking mode: %m");
250     }
251 }
252
253 /*
254  * restore_loop - reattach the ppp unit to the loopback.
255  */
256 void
257 restore_loop()
258 {
259 }
260
261 /*
262  * disestablish_ppp - Restore the serial port to normal operation.
263  * This shouldn't call die() because it's called from die().
264  */
265 void
266 disestablish_ppp(fd)
267      int fd;
268 {
269     /* Reset non-blocking mode on fd. */
270     if (initfdflags != -1 && fcntl(fd, F_SETFL, initfdflags) < 0)
271         syslog(LOG_WARNING, "Couldn't restore device fd flags: %m");
272     initfdflags = -1;
273
274     /* Restore old line discipline. */
275     if (initdisc >= 0 && ioctl(fd, TIOCSETD, &initdisc) < 0)
276         syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
277     initdisc = -1;
278
279     if (fd == ppp_fd)
280         ppp_fd = -1;
281 }
282
283 /*
284  * Check whether the link seems not to be 8-bit clean.
285  */
286 void
287 clean_check()
288 {
289     int x;
290     char *s;
291
292     if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) == 0) {
293         s = NULL;
294         switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP)) {
295         case SC_RCV_B7_0:
296             s = "bit 7 set to 1";
297             break;
298         case SC_RCV_B7_1:
299             s = "bit 7 set to 0";
300             break;
301         case SC_RCV_EVNP:
302             s = "odd parity";
303             break;
304         case SC_RCV_ODDP:
305             s = "even parity";
306             break;
307         }
308         if (s != NULL) {
309             syslog(LOG_WARNING, "Serial link is not 8-bit clean:");
310             syslog(LOG_WARNING, "All received characters had %s", s);
311         }
312     }
313 }
314
315
316 /*
317  * List of valid speeds.
318  */
319 struct speed {
320     int speed_int, speed_val;
321 } speeds[] = {
322 #ifdef B50
323     { 50, B50 },
324 #endif
325 #ifdef B75
326     { 75, B75 },
327 #endif
328 #ifdef B110
329     { 110, B110 },
330 #endif
331 #ifdef B134
332     { 134, B134 },
333 #endif
334 #ifdef B150
335     { 150, B150 },
336 #endif
337 #ifdef B200
338     { 200, B200 },
339 #endif
340 #ifdef B300
341     { 300, B300 },
342 #endif
343 #ifdef B600
344     { 600, B600 },
345 #endif
346 #ifdef B1200
347     { 1200, B1200 },
348 #endif
349 #ifdef B1800
350     { 1800, B1800 },
351 #endif
352 #ifdef B2000
353     { 2000, B2000 },
354 #endif
355 #ifdef B2400
356     { 2400, B2400 },
357 #endif
358 #ifdef B3600
359     { 3600, B3600 },
360 #endif
361 #ifdef B4800
362     { 4800, B4800 },
363 #endif
364 #ifdef B7200
365     { 7200, B7200 },
366 #endif
367 #ifdef B9600
368     { 9600, B9600 },
369 #endif
370 #ifdef B19200
371     { 19200, B19200 },
372 #endif
373 #ifdef B38400
374     { 38400, B38400 },
375 #endif
376 #ifdef EXTA
377     { 19200, EXTA },
378 #endif
379 #ifdef EXTB
380     { 38400, EXTB },
381 #endif
382 #ifdef B57600
383     { 57600, B57600 },
384 #endif
385 #ifdef B115200
386     { 115200, B115200 },
387 #endif
388     { 0, 0 }
389 };
390
391 /*
392  * Translate from bits/second to a speed_t.
393  */
394 int
395 translate_speed(bps)
396     int bps;
397 {
398     struct speed *speedp;
399
400     if (bps == 0)
401         return 0;
402     for (speedp = speeds; speedp->speed_int; speedp++)
403         if (bps == speedp->speed_int)
404             return speedp->speed_val;
405     syslog(LOG_WARNING, "speed %d not supported", bps);
406     return 0;
407 }
408
409 /*
410  * Translate from a speed_t to bits/second.
411  */
412 int
413 baud_rate_of(speed)
414     int speed;
415 {
416     struct speed *speedp;
417
418     if (speed == 0)
419         return 0;
420     for (speedp = speeds; speedp->speed_int; speedp++)
421         if (speed == speedp->speed_val)
422             return speedp->speed_int;
423     return 0;
424 }
425
426 /*
427  * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
428  * at the requested speed, etc.  If `local' is true, set CLOCAL
429  * regardless of whether the modem option was specified.
430  */
431 void
432 set_up_tty(fd, local)
433     int fd, local;
434 {
435     int speed, x;
436     struct termios tios;
437
438     if (tcgetattr(fd, &tios) < 0) {
439         syslog(LOG_ERR, "tcgetattr: %m");
440         die(1);
441     }
442
443     if (!restore_term) {
444         inittermios = tios;
445         ioctl(fd, TIOCGWINSZ, &wsinfo);
446     }
447
448     tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
449 #ifdef CRTSCTS
450     if (crtscts > 0 && !local)
451         tios.c_cflag |= CRTSCTS;
452     else if (crtscts < 0)
453         tios.c_cflag &= ~CRTSCTS;
454 #endif  /* CRTSCTS */
455
456     tios.c_cflag |= CS8 | CREAD | HUPCL;
457     if (local || !modem)
458         tios.c_cflag |= CLOCAL;
459     tios.c_iflag = IGNBRK | IGNPAR;
460     tios.c_oflag = 0;
461     tios.c_lflag = 0;
462     tios.c_cc[VMIN] = 1;
463     tios.c_cc[VTIME] = 0;
464
465     if (crtscts == -2) {
466         tios.c_iflag |= IXON | IXOFF;
467         tios.c_cc[VSTOP] = 0x13;        /* DC3 = XOFF = ^S */
468         tios.c_cc[VSTART] = 0x11;       /* DC1 = XON  = ^Q */
469     }
470
471     speed = translate_speed(inspeed);
472     if (speed) {
473         cfsetospeed(&tios, speed);
474         cfsetispeed(&tios, speed);
475     } else {
476         speed = cfgetospeed(&tios);
477         /*
478          * We can't proceed if the serial port speed is B0,
479          * since that implies that the serial port is disabled.
480          */
481         if (speed == B0) {
482             syslog(LOG_ERR, "Baud rate for %s is 0; need explicit baud rate",
483                    devnam);
484             die(1);
485         }
486     }
487
488     if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
489         syslog(LOG_ERR, "tcsetattr: %m");
490         die(1);
491     }
492
493     x = 0;
494     if (ioctl(fd, (crtscts > 0 || modem)? TIOCMODEM: TIOCNMODEM, &x) < 0)
495         syslog(LOG_WARNING, "TIOC(N)MODEM: %m");
496     if (ioctl(fd, (local || !modem)? TIOCNCAR: TIOCCAR) < 0)
497         syslog(LOG_WARNING, "TIOC(N)CAR: %m");
498
499     baud_rate = inspeed = baud_rate_of(speed);
500     restore_term = TRUE;
501 }
502
503 /*
504  * restore_tty - restore the terminal to the saved settings.
505  */
506 void
507 restore_tty(fd)
508     int fd;
509 {
510     if (restore_term) {
511         if (!default_device) {
512             /*
513              * Turn off echoing, because otherwise we can get into
514              * a loop with the tty and the modem echoing to each other.
515              * We presume we are the sole user of this tty device, so
516              * when we close it, it will revert to its defaults anyway.
517              */
518             inittermios.c_lflag &= ~(ECHO | ECHONL);
519         }
520         if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
521             if (errno != ENXIO)
522                 syslog(LOG_WARNING, "tcsetattr: %m");
523         ioctl(fd, TIOCSWINSZ, &wsinfo);
524         restore_term = FALSE;
525     }
526 }
527
528 /*
529  * setdtr - control the DTR line on the serial port.
530  * This is called from die(), so it shouldn't call die().
531  */
532 void
533 setdtr(fd, on)
534 int fd, on;
535 {
536     int modembits = TIOCM_DTR;
537
538     ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
539 }
540
541
542 /*
543  * open_ppp_loopback - open the device we use for getting
544  * packets in demand mode, and connect it to a ppp interface.
545  * Here we use would use a pty, except that Ultrix ptys are brain-dead.
546  */
547 void
548 open_ppp_loopback()
549 {
550     syslog(LOG_ERR, "open_ppp_loopback called!");
551     die(1);
552 }
553
554
555 /*
556  * output - Output PPP packet.
557  */
558 void
559 output(unit, p, len)
560     int unit;
561     u_char *p;
562     int len;
563 {
564     if (debug)
565         log_packet(p, len, "sent ", LOG_DEBUG);
566
567     if (write(ttyfd, p, len) < 0) {
568         if (errno != EIO)
569             syslog(LOG_ERR, "write: %m");
570     }
571 }
572
573
574 /*
575  * wait_input - wait until there is data available on ttyfd,
576  * for the length of time specified by *timo (indefinite
577  * if timo is NULL).
578  */
579 void
580 wait_input(timo)
581     struct timeval *timo;
582 {
583     fd_set ready;
584     int n;
585
586     FD_ZERO(&ready);
587     FD_SET(ttyfd, &ready);
588     n = select(ttyfd+1, &ready, NULL, &ready, timo);
589     if (n < 0 && errno != EINTR) {
590         syslog(LOG_ERR, "select: %m");
591         die(1);
592     }
593 }
594
595
596 /*
597  * wait_loop_output - wait until there is data available on the
598  * loopback, for the length of time specified by *timo (indefinite
599  * if timo is NULL).
600  */
601 void
602 wait_loop_output(timo)
603     struct timeval *timo;
604 {
605     wait_time(timo);
606 }
607
608
609 /*
610  * wait_time - wait for a given length of time or until a
611  * signal is received.
612  */
613 void
614 wait_time(timo)
615     struct timeval *timo;
616 {
617     int n;
618
619     n = select(0, NULL, NULL, NULL, timo);
620     if (n < 0 && errno != EINTR) {
621         syslog(LOG_ERR, "select: %m");
622         die(1);
623     }
624 }
625
626
627 /*
628  * read_packet - get a PPP packet from the serial device.
629  */
630 int
631 read_packet(buf)
632     u_char *buf;
633 {
634     int len;
635
636     if ((len = read(ttyfd, buf, PPP_MTU + PPP_HDRLEN)) < 0) {
637         if (errno == EWOULDBLOCK || errno == EINTR)
638             return -1;
639         syslog(LOG_ERR, "read(fd): %m");
640         die(1);
641     }
642     return len;
643 }
644
645
646 /*
647  * get_loop_output - read characters from the loopback, form them
648  * into frames, and detect when we want to bring the real link up.
649  * Return value is 1 if we need to bring up the link, 0 otherwise.
650  */
651 int
652 get_loop_output()
653 {
654     return 0;
655 }
656
657
658 /*
659  * ppp_send_config - configure the transmit characteristics of
660  * the ppp interface.
661  */
662 void
663 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
664     int unit, mtu;
665     u_int32_t asyncmap;
666     int pcomp, accomp;
667 {
668     u_int x;
669
670     if (ioctl(ppp_fd, PPPIOCSMTU, &mtu) < 0) {
671         syslog(LOG_ERR, "ioctl(PPPIOCSMTU): %m");
672         quit();
673     }
674
675     if (ioctl(ppp_fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0) {
676         syslog(LOG_ERR, "ioctl(PPPIOCSASYNCMAP): %m");
677         quit();
678     }
679
680     if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
681         syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
682         quit();
683     }
684     x = pcomp? x | SC_COMP_PROT: x &~ SC_COMP_PROT;
685     x = accomp? x | SC_COMP_AC: x &~ SC_COMP_AC;
686     if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
687         syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
688         quit();
689     }
690 }
691
692
693 /*
694  * ppp_set_xaccm - set the extended transmit ACCM for the interface.
695  */
696 void
697 ppp_set_xaccm(unit, accm)
698     int unit;
699     ext_accm accm;
700 {
701     if (ioctl(ppp_fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY)
702         syslog(LOG_WARNING, "ioctl(set extended ACCM): %m");
703 }
704
705
706 /*
707  * ppp_recv_config - configure the receive-side characteristics of
708  * the ppp interface.
709  */
710 void
711 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
712     int unit, mru;
713     u_int32_t asyncmap;
714     int pcomp, accomp;
715 {
716     int x;
717
718     if (ioctl(ppp_fd, PPPIOCSMRU, (caddr_t) &mru) < 0) {
719         syslog(LOG_ERR, "ioctl(PPPIOCSMRU): %m");
720         quit();
721     }
722     if (ioctl(ppp_fd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0) {
723         syslog(LOG_ERR, "ioctl(PPPIOCSRASYNCMAP): %m");
724         quit();
725     }
726     if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
727         syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
728         quit();
729     }
730     x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC;
731     if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
732         syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
733         quit();
734     }
735 }
736
737 /*
738  * ccp_test - ask kernel whether a given compression method
739  * is acceptable for use.  Returns 1 if the method and parameters
740  * are OK, 0 if the method is known but the parameters are not OK
741  * (e.g. code size should be reduced), or -1 if the method is unknown.
742  */
743 int
744 ccp_test(unit, opt_ptr, opt_len, for_transmit)
745     int unit, opt_len, for_transmit;
746     u_char *opt_ptr;
747 {
748     struct ppp_option_data data;
749
750     data.ptr = opt_ptr;
751     data.length = opt_len;
752     data.transmit = for_transmit;
753     if (ioctl(ttyfd, PPPIOCSCOMPRESS, (caddr_t) &data) >= 0)
754         return 1;
755     return (errno == ENOBUFS)? 0: -1;
756 }
757
758 /*
759  * ccp_flags_set - inform kernel about the current state of CCP.
760  */
761 void
762 ccp_flags_set(unit, isopen, isup)
763     int unit, isopen, isup;
764 {
765     int x;
766
767     if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
768         syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
769         return;
770     }
771     x = isopen? x | SC_CCP_OPEN: x &~ SC_CCP_OPEN;
772     x = isup? x | SC_CCP_UP: x &~ SC_CCP_UP;
773     if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
774         syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
775 }
776
777 /*
778  * ccp_fatal_error - returns 1 if decompression was disabled as a
779  * result of an error detected after decompression of a packet,
780  * 0 otherwise.  This is necessary because of patent nonsense.
781  */
782 int
783 ccp_fatal_error(unit)
784     int unit;
785 {
786     int x;
787
788     if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
789         syslog(LOG_ERR, "ioctl(PPPIOCGFLAGS): %m");
790         return 0;
791     }
792     return x & SC_DC_FERROR;
793 }
794
795 /*
796  * get_idle_time - return how long the link has been idle.
797  */
798 int
799 get_idle_time(u, ip)
800     int u;
801     struct ppp_idle *ip;
802 {
803     return ioctl(ppp_fd, PPPIOCGIDLE, ip) >= 0;
804 }
805
806
807 /*
808  * sifvjcomp - config tcp header compression
809  */
810 int
811 sifvjcomp(u, vjcomp, cidcomp, maxcid)
812     int u, vjcomp, cidcomp, maxcid;
813 {
814     u_int x;
815
816     if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
817         syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
818         return 0;
819     }
820     x = vjcomp ? x | SC_COMP_TCP: x &~ SC_COMP_TCP;
821     x = cidcomp? x & ~SC_NO_TCP_CCID: x | SC_NO_TCP_CCID;
822     if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
823         syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
824         return 0;
825     }
826     if (ioctl(ppp_fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0) {
827         syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
828         return 0;
829     }
830     return 1;
831 }
832
833 /*
834  * sifup - Config the interface up and enable IP packets to pass.
835  */
836 int
837 sifup(u)
838     int u;
839 {
840     struct ifreq ifr;
841
842     strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
843     if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
844         syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
845         return 0;
846     }
847     ifr.ifr_flags |= IFF_UP;
848     if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
849         syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
850         return 0;
851     }
852     if_is_up = 1;
853     return 1;
854 }
855
856 /*
857  * sifnpmode - Set the mode for handling packets for a given NP.
858  */
859 int
860 sifnpmode(u, proto, mode)
861     int u;
862     int proto;
863     enum NPmode mode;
864 {
865     struct npioctl npi;
866
867     npi.protocol = proto;
868     npi.mode = mode;
869     if (ioctl(ppp_fd, PPPIOCSNPMODE, &npi) < 0) {
870         syslog(LOG_ERR, "ioctl(set NP %d mode to %d): %m", proto, mode);
871         return 0;
872     }
873     return 1;
874 }
875
876 /*
877  * sifdown - Config the interface down and disable IP.
878  */
879 int
880 sifdown(u)
881     int u;
882 {
883     struct ifreq ifr;
884     int rv;
885     struct npioctl npi;
886
887     rv = 1;
888     npi.protocol = PPP_IP;
889     npi.mode = NPMODE_ERROR;
890     ioctl(ppp_fd, PPPIOCSNPMODE, (caddr_t) &npi);
891     /* ignore errors, because ppp_fd might have been closed by now. */
892
893     strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
894     if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
895         syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
896         rv = 0;
897     } else {
898         ifr.ifr_flags &= ~IFF_UP;
899         if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
900             syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
901             rv = 0;
902         } else
903             if_is_up = 0;
904     }
905     return rv;
906 }
907
908 /*
909  * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
910  * if it exists.
911  */
912 #define SET_SA_FAMILY(addr, family)             \
913     BZERO((char *) &(addr), sizeof(addr));      \
914     addr.sa_family = (family); 
915
916 /*
917  * sifaddr - Config the interface IP addresses and netmask.
918  */
919 int
920 sifaddr(u, o, h, m)
921     int u;
922     u_int32_t o, h, m;
923 {
924     int ret;
925     struct ifreq ifr;
926
927     ret = 1;
928     strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
929     SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
930     if (m != 0) {
931         ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = m;
932         syslog(LOG_INFO, "Setting interface mask to %s\n", ip_ntoa(m));
933         if (ioctl(sockfd, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
934             syslog(LOG_ERR, "ioctl(SIOCSIFNETMASK): %m");
935             ret = 0;
936         }
937     }
938     ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
939     if (ioctl(sockfd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
940         syslog(LOG_ERR, "ioctl(SIOCSIFADDR): %m");
941         ret = 0;
942     }
943     ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = h;
944     if (ioctl(sockfd, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
945         syslog(LOG_ERR, "ioctl(SIOCSIFDSTADDR): %m");
946         ret = 0;
947     }
948     ifaddrs[0] = o;
949     ifaddrs[1] = h;
950     return ret;
951 }
952
953 /*
954  * cifaddr - Clear the interface IP addresses, and delete routes
955  * through the interface if possible.
956  */
957 int
958 cifaddr(u, o, h)
959     int u;
960     u_int32_t o, h;
961 {
962     struct rtentry rt;
963
964     ifaddrs[0] = 0;
965     BZERO(&rt, sizeof(rt));
966     SET_SA_FAMILY(rt.rt_dst, AF_INET);
967     ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = h;
968     SET_SA_FAMILY(rt.rt_gateway, AF_INET);
969     ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = o;
970     rt.rt_flags = RTF_HOST;
971     if (ioctl(sockfd, SIOCDELRT, (caddr_t) &rt) < 0) {
972         syslog(LOG_ERR, "ioctl(SIOCDELRT): %m");
973         return 0;
974     }
975     return 1;
976 }
977
978 /*
979  * sifdefaultroute - assign a default route through the address given.
980  */
981 int
982 sifdefaultroute(u, l, g)
983     int u;
984     u_int32_t l, g;
985 {
986     struct rtentry rt;
987
988     BZERO(&rt, sizeof(rt));
989     SET_SA_FAMILY(rt.rt_dst, AF_INET);
990     SET_SA_FAMILY(rt.rt_gateway, AF_INET);
991     ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
992     rt.rt_flags = RTF_GATEWAY;
993     if (ioctl(sockfd, SIOCADDRT, &rt) < 0) {
994         syslog(LOG_ERR, "default route ioctl(SIOCADDRT): %m");
995         return 0;
996     }
997     default_route_gateway = g;
998     return 1;
999 }
1000
1001 /*
1002  * cifdefaultroute - delete a default route through the address given.
1003  */
1004 int
1005 cifdefaultroute(u, l, g)
1006     int u;
1007     u_int32_t l, g;
1008 {
1009     struct rtentry rt;
1010
1011     BZERO(&rt, sizeof(rt));
1012     SET_SA_FAMILY(rt.rt_dst, AF_INET);
1013     SET_SA_FAMILY(rt.rt_gateway, AF_INET);
1014     ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
1015     rt.rt_flags = RTF_GATEWAY;
1016     if (ioctl(sockfd, SIOCDELRT, &rt) < 0)
1017         syslog(LOG_WARNING, "default route ioctl(SIOCDELRT): %m");
1018     default_route_gateway = 0;
1019     return 1;
1020 }
1021
1022 /*
1023  * sifproxyarp - Make a proxy ARP entry for the peer.
1024  */
1025 int
1026 sifproxyarp(unit, hisaddr)
1027     int unit;
1028     u_int32_t hisaddr;
1029 {
1030     struct arpreq arpreq;
1031
1032     BZERO(&arpreq, sizeof(arpreq));
1033
1034     /*
1035      * Get the hardware address of an interface on the same subnet
1036      * as our local address.
1037      */
1038     if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) {
1039         syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
1040         return 0;
1041     }
1042
1043     SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1044     ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1045     arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1046     if (ioctl(sockfd, SIOCSARP, (caddr_t)&arpreq) < 0) {
1047         syslog(LOG_ERR, "Couldn't add proxy arp entry: %m");
1048         return 0;
1049     }
1050
1051     proxy_arp_addr = hisaddr;
1052     return 1;
1053 }
1054
1055 /*
1056  * cifproxyarp - Delete the proxy ARP entry for the peer.
1057  */
1058 int
1059 cifproxyarp(unit, hisaddr)
1060     int unit;
1061     u_int32_t hisaddr;
1062 {
1063     struct arpreq arpreq;
1064
1065     BZERO(&arpreq, sizeof(arpreq));
1066     SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1067     ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1068     if (ioctl(sockfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
1069         syslog(LOG_WARNING, "Couldn't delete proxy arp entry: %m");
1070         return 0;
1071     }
1072     proxy_arp_addr = 0;
1073     return 1;
1074 }
1075
1076 /*
1077  * get_ether_addr - get the hardware address of an interface on the
1078  * the same subnet as ipaddr.
1079  */
1080 #define MAX_IFS         32
1081
1082 static int
1083 get_ether_addr(ipaddr, hwaddr)
1084     u_int32_t ipaddr;
1085     struct sockaddr *hwaddr;
1086 {
1087     struct ifreq *ifr, *ifend;
1088     u_int32_t ina, mask;
1089     struct ifreq ifreq;
1090     struct ifconf ifc;
1091     struct ifreq ifs[MAX_IFS];
1092     struct ifdevea ifdevea;
1093
1094     ifc.ifc_len = sizeof(ifs);
1095     ifc.ifc_req = ifs;
1096     if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1097         syslog(LOG_ERR, "ioctl(SIOCGIFCONF): %m");
1098         return 0;
1099     }
1100
1101     /*
1102      * Scan through looking for an interface with an Internet
1103      * address on the same subnet as `ipaddr'.
1104      */
1105     ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1106     for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1107             ((char *)&ifr->ifr_addr + sizeof(struct sockaddr))) {
1108         if (ifr->ifr_addr.sa_family == AF_INET) {
1109             ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1110             strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1111             /*
1112              * Check that the interface is up, and not point-to-point
1113              * or loopback.
1114              */
1115             if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1116                 continue;
1117             if ((ifreq.ifr_flags &
1118                  (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
1119                  != (IFF_UP|IFF_BROADCAST))
1120                 continue;
1121             /*
1122              * Get its netmask and check that it's on the right subnet.
1123              */
1124             if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1125                 continue;
1126             mask = ((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr;
1127             if ((ipaddr & mask) != (ina & mask))
1128                 continue;
1129
1130             break;
1131         }
1132     }
1133
1134     if (ifr >= ifend)
1135         return 0;
1136     syslog(LOG_INFO, "found interface %s for proxy arp", ifr->ifr_name);
1137
1138     /*
1139      * Grab the physical address for this interface.
1140      */
1141     strncpy(ifdevea.ifr_name, ifr->ifr_name, sizeof(ifdevea.ifr_name));
1142     if (ioctl(sockfd, SIOCRPHYSADDR, &ifdevea) < 0) {
1143         syslog(LOG_ERR, "Couldn't get h/w address for %s: %m", ifr->ifr_name);
1144         return 0;
1145     }
1146
1147     hwaddr->sa_family = AF_UNSPEC;
1148     BCOPY(ifdevea.current_pa, hwaddr->sa_data, 6);
1149     return 1;
1150 }
1151
1152 /*
1153  * Return user specified netmask, modified by any mask we might determine
1154  * for address `addr' (in network byte order).
1155  * Here we scan through the system's list of interfaces, looking for
1156  * any non-point-to-point interfaces which might appear to be on the same
1157  * network as `addr'.  If we find any, we OR in their netmask to the
1158  * user-specified netmask.
1159  */
1160 u_int32_t
1161 GetMask(addr)
1162     u_int32_t addr;
1163 {
1164     u_int32_t mask, nmask, ina;
1165     struct ifreq *ifr, *ifend, ifreq;
1166     struct ifconf ifc;
1167     struct ifreq ifs[MAX_IFS];
1168
1169     addr = ntohl(addr);
1170     if (IN_CLASSA(addr))        /* determine network mask for address class */
1171         nmask = IN_CLASSA_NET;
1172     else if (IN_CLASSB(addr))
1173         nmask = IN_CLASSB_NET;
1174     else
1175         nmask = IN_CLASSC_NET;
1176     /* class D nets are disallowed by bad_ip_adrs */
1177     mask = netmask | htonl(nmask);
1178
1179     /*
1180      * Scan through the system's network interfaces.
1181      */
1182     ifc.ifc_len = sizeof(ifs);
1183     ifc.ifc_req = ifs;
1184     if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1185         syslog(LOG_WARNING, "ioctl(SIOCGIFCONF): %m");
1186         return mask;
1187     }
1188     ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1189     for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1190                 ((char *)&ifr->ifr_addr + sizeof(struct sockaddr))) {
1191         /*
1192          * Check the interface's internet address.
1193          */
1194         if (ifr->ifr_addr.sa_family != AF_INET)
1195             continue;
1196         ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1197         if ((ntohl(ina) & nmask) != (addr & nmask))
1198             continue;
1199         /*
1200          * Check that the interface is up, and not point-to-point or loopback.
1201          */
1202         strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1203         if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1204             continue;
1205         if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
1206             != IFF_UP)
1207             continue;
1208         /*
1209          * Get its netmask and OR it into our mask.
1210          */
1211         if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1212             continue;
1213         mask |= ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr;
1214     }
1215
1216     return mask;
1217 }
1218
1219
1220 /*
1221   Seems like strdup() is not part of string package in Ultrix.
1222   If I understood the man-page on the sun this should work.
1223
1224   Robert Olsson
1225 */
1226
1227 char *strdup( in ) char *in;
1228 {
1229   char* dup;
1230   if(! (dup = (char *) malloc( strlen( in ) +1 ))) return NULL;
1231   (void) strcpy( dup, in );
1232   return dup;
1233 }
1234
1235 /*
1236  * This logwtmp() implementation is subject to the following copyright:
1237  *
1238  * Copyright (c) 1988 The Regents of the University of California.
1239  * All rights reserved.
1240  *
1241  * Redistribution and use in source and binary forms are permitted
1242  * provided that the above copyright notice and this paragraph are
1243  * duplicated in all such forms and that any documentation,
1244  * advertising materials, and other materials related to such
1245  * distribution and use acknowledge that the software was developed
1246  * by the University of California, Berkeley.  The name of the
1247  * University may not be used to endorse or promote products derived
1248  * from this software without specific prior written permission.
1249  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1250  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1251  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1252  */
1253
1254 #define WTMPFILE        "/usr/adm/wtmp"
1255
1256 void
1257 logwtmp(line, name, host)
1258     const char *line, *name, *host;
1259 {
1260     int fd;
1261     struct stat buf;
1262     struct utmp ut;
1263
1264     if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0)
1265         return;
1266     if (!fstat(fd, &buf)) {
1267         (void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
1268         (void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
1269         (void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
1270         (void)time(&ut.ut_time);
1271         if (write(fd, (char *)&ut, sizeof(struct utmp)) != sizeof(struct utmp))
1272             (void)ftruncate(fd, buf.st_size);
1273     }
1274     close(fd);
1275 }
1276
1277 /*
1278  * Routines for locking and unlocking the serial device, moved here
1279  * from chat.c.
1280  */
1281
1282 #define LOCK_PREFIX     "/usr/spool/uucp/LCK.."
1283
1284 /*
1285  * lock - create a lock file for the named device
1286  */
1287 int
1288 lock(dev)
1289     char *dev;
1290 {
1291     int fd, pid, n;
1292     char *p;
1293
1294     if ((p = strrchr(dev, '/')) != NULL)
1295         dev = p + 1;
1296     lock_file = malloc(strlen(LOCK_PREFIX) + strlen(dev) + 1);
1297     if (lock_file == NULL)
1298         novm("lock file name");
1299     strcat(strcpy(lock_file, LOCK_PREFIX), dev);
1300
1301     while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
1302         if (errno == EEXIST
1303             && (fd = open(lock_file, O_RDONLY, 0)) >= 0) {
1304             /* Read the lock file to find out who has the device locked */
1305             n = read(fd, &pid, sizeof(pid));
1306             if (n <= 0) {
1307                 syslog(LOG_ERR, "Can't read pid from lock file %s", lock_file);
1308                 close(fd);
1309             } else {
1310                 if (kill(pid, 0) == -1 && errno == ESRCH) {
1311                     /* pid no longer exists - remove the lock file */
1312                     if (unlink(lock_file) == 0) {
1313                         close(fd);
1314                         syslog(LOG_NOTICE, "Removed stale lock on %s (pid %d)",
1315                                dev, pid);
1316                         continue;
1317                     } else
1318                         syslog(LOG_WARNING, "Couldn't remove stale lock on %s",
1319                                dev);
1320                 } else
1321                     syslog(LOG_NOTICE, "Device %s is locked by pid %d",
1322                            dev, pid);
1323             }
1324             close(fd);
1325         } else
1326             syslog(LOG_ERR, "Can't create lock file %s: %m", lock_file);
1327         free(lock_file);
1328         lock_file = NULL;
1329         return -1;
1330     }
1331
1332     pid = getpid();
1333     write(fd, &pid, sizeof pid);
1334
1335     close(fd);
1336     return 0;
1337 }
1338
1339 /*
1340  * unlock - remove our lockfile
1341  */
1342 void
1343 unlock()
1344 {
1345     if (lock_file) {
1346         unlink(lock_file);
1347         free(lock_file);
1348         lock_file = NULL;
1349     }
1350 }