]> git.ozlabs.org Git - ppp.git/blob - pppd/sys-ultrix.c
mods for other SVR4 systems
[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.19 1996/08/28 06:42:54 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, 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 ");
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, g)
983 {
984     struct rtentry rt;
985
986     BZERO(&rt, sizeof(rt));
987     SET_SA_FAMILY(rt.rt_dst, AF_INET);
988     SET_SA_FAMILY(rt.rt_gateway, AF_INET);
989     ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
990     rt.rt_flags = RTF_GATEWAY;
991     if (ioctl(sockfd, SIOCADDRT, &rt) < 0) {
992         syslog(LOG_ERR, "default route ioctl(SIOCADDRT): %m");
993         return 0;
994     }
995     default_route_gateway = g;
996     return 1;
997 }
998
999 /*
1000  * cifdefaultroute - delete a default route through the address given.
1001  */
1002 int
1003 cifdefaultroute(u, g)
1004 {
1005     struct rtentry rt;
1006
1007     BZERO(&rt, sizeof(rt));
1008     SET_SA_FAMILY(rt.rt_dst, AF_INET);
1009     SET_SA_FAMILY(rt.rt_gateway, AF_INET);
1010     ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
1011     rt.rt_flags = RTF_GATEWAY;
1012     if (ioctl(sockfd, SIOCDELRT, &rt) < 0)
1013         syslog(LOG_WARNING, "default route ioctl(SIOCDELRT): %m");
1014     default_route_gateway = 0;
1015     return 1;
1016 }
1017
1018 /*
1019  * sifproxyarp - Make a proxy ARP entry for the peer.
1020  */
1021 int
1022 sifproxyarp(unit, hisaddr)
1023     int unit;
1024     u_int32_t hisaddr;
1025 {
1026     struct arpreq arpreq;
1027
1028     BZERO(&arpreq, sizeof(arpreq));
1029
1030     /*
1031      * Get the hardware address of an interface on the same subnet
1032      * as our local address.
1033      */
1034     if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) {
1035         syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
1036         return 0;
1037     }
1038
1039     SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1040     ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1041     arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1042     if (ioctl(sockfd, SIOCSARP, (caddr_t)&arpreq) < 0) {
1043         syslog(LOG_ERR, "Couldn't add proxy arp entry: %m");
1044         return 0;
1045     }
1046
1047     proxy_arp_addr = hisaddr;
1048     return 1;
1049 }
1050
1051 /*
1052  * cifproxyarp - Delete the proxy ARP entry for the peer.
1053  */
1054 int
1055 cifproxyarp(unit, hisaddr)
1056     int unit;
1057     u_int32_t hisaddr;
1058 {
1059     struct arpreq arpreq;
1060
1061     BZERO(&arpreq, sizeof(arpreq));
1062     SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1063     ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1064     if (ioctl(sockfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
1065         syslog(LOG_WARNING, "Couldn't delete proxy arp entry: %m");
1066         return 0;
1067     }
1068     proxy_arp_addr = 0;
1069     return 1;
1070 }
1071
1072 /*
1073  * get_ether_addr - get the hardware address of an interface on the
1074  * the same subnet as ipaddr.
1075  */
1076 #define MAX_IFS         32
1077
1078 static int
1079 get_ether_addr(ipaddr, hwaddr)
1080     u_int32_t ipaddr;
1081     struct sockaddr *hwaddr;
1082 {
1083     struct ifreq *ifr, *ifend;
1084     u_int32_t ina, mask;
1085     struct ifreq ifreq;
1086     struct ifconf ifc;
1087     struct ifreq ifs[MAX_IFS];
1088     struct ifdevea ifdevea;
1089
1090     ifc.ifc_len = sizeof(ifs);
1091     ifc.ifc_req = ifs;
1092     if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1093         syslog(LOG_ERR, "ioctl(SIOCGIFCONF): %m");
1094         return 0;
1095     }
1096
1097     /*
1098      * Scan through looking for an interface with an Internet
1099      * address on the same subnet as `ipaddr'.
1100      */
1101     ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1102     for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1103             ((char *)&ifr->ifr_addr + sizeof(struct sockaddr))) {
1104         if (ifr->ifr_addr.sa_family == AF_INET) {
1105             ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1106             strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1107             /*
1108              * Check that the interface is up, and not point-to-point
1109              * or loopback.
1110              */
1111             if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1112                 continue;
1113             if ((ifreq.ifr_flags &
1114                  (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
1115                  != (IFF_UP|IFF_BROADCAST))
1116                 continue;
1117             /*
1118              * Get its netmask and check that it's on the right subnet.
1119              */
1120             if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1121                 continue;
1122             mask = ((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr;
1123             if ((ipaddr & mask) != (ina & mask))
1124                 continue;
1125
1126             break;
1127         }
1128     }
1129
1130     if (ifr >= ifend)
1131         return 0;
1132     syslog(LOG_INFO, "found interface %s for proxy arp", ifr->ifr_name);
1133
1134     /*
1135      * Grab the physical address for this interface.
1136      */
1137     strncpy(ifdevea.ifr_name, ifr->ifr_name, sizeof(ifdevea.ifr_name));
1138     if (ioctl(sockfd, SIOCRPHYSADDR, &ifdevea) < 0) {
1139         syslog(LOG_ERR, "Couldn't get h/w address for %s: %m", ifr->ifr_name);
1140         return 0;
1141     }
1142
1143     hwaddr->sa_family = AF_UNSPEC;
1144     BCOPY(ifdevea.current_pa, hwaddr->sa_data, 6);
1145     return 1;
1146 }
1147
1148 /*
1149  * Return user specified netmask, modified by any mask we might determine
1150  * for address `addr' (in network byte order).
1151  * Here we scan through the system's list of interfaces, looking for
1152  * any non-point-to-point interfaces which might appear to be on the same
1153  * network as `addr'.  If we find any, we OR in their netmask to the
1154  * user-specified netmask.
1155  */
1156 u_int32_t
1157 GetMask(addr)
1158     u_int32_t addr;
1159 {
1160     u_int32_t mask, nmask, ina;
1161     struct ifreq *ifr, *ifend, ifreq;
1162     struct ifconf ifc;
1163     struct ifreq ifs[MAX_IFS];
1164
1165     addr = ntohl(addr);
1166     if (IN_CLASSA(addr))        /* determine network mask for address class */
1167         nmask = IN_CLASSA_NET;
1168     else if (IN_CLASSB(addr))
1169         nmask = IN_CLASSB_NET;
1170     else
1171         nmask = IN_CLASSC_NET;
1172     /* class D nets are disallowed by bad_ip_adrs */
1173     mask = netmask | htonl(nmask);
1174
1175     /*
1176      * Scan through the system's network interfaces.
1177      */
1178     ifc.ifc_len = sizeof(ifs);
1179     ifc.ifc_req = ifs;
1180     if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1181         syslog(LOG_WARNING, "ioctl(SIOCGIFCONF): %m");
1182         return mask;
1183     }
1184     ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1185     for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1186                 ((char *)&ifr->ifr_addr + sizeof(struct sockaddr))) {
1187         /*
1188          * Check the interface's internet address.
1189          */
1190         if (ifr->ifr_addr.sa_family != AF_INET)
1191             continue;
1192         ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1193         if ((ntohl(ina) & nmask) != (addr & nmask))
1194             continue;
1195         /*
1196          * Check that the interface is up, and not point-to-point or loopback.
1197          */
1198         strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1199         if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1200             continue;
1201         if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
1202             != IFF_UP)
1203             continue;
1204         /*
1205          * Get its netmask and OR it into our mask.
1206          */
1207         if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1208             continue;
1209         mask |= ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr;
1210     }
1211
1212     return mask;
1213 }
1214
1215
1216 /*
1217   Seems like strdup() is not part of string package in Ultrix.
1218   If I understood the man-page on the sun this should work.
1219
1220   Robert Olsson
1221 */
1222
1223 char *strdup( in ) char *in;
1224 {
1225   char* dup;
1226   if(! (dup = (char *) malloc( strlen( in ) +1 ))) return NULL;
1227   (void) strcpy( dup, in );
1228   return dup;
1229 }
1230
1231 /*
1232  * This logwtmp() implementation is subject to the following copyright:
1233  *
1234  * Copyright (c) 1988 The Regents of the University of California.
1235  * All rights reserved.
1236  *
1237  * Redistribution and use in source and binary forms are permitted
1238  * provided that the above copyright notice and this paragraph are
1239  * duplicated in all such forms and that any documentation,
1240  * advertising materials, and other materials related to such
1241  * distribution and use acknowledge that the software was developed
1242  * by the University of California, Berkeley.  The name of the
1243  * University may not be used to endorse or promote products derived
1244  * from this software without specific prior written permission.
1245  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1246  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1247  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1248  */
1249
1250 #define WTMPFILE        "/usr/adm/wtmp"
1251
1252 int
1253 logwtmp(line, name, host)
1254     char *line, *name, *host;
1255 {
1256     int fd;
1257     struct stat buf;
1258     struct utmp ut;
1259
1260     if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0)
1261         return;
1262     if (!fstat(fd, &buf)) {
1263         (void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
1264         (void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
1265         (void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
1266         (void)time(&ut.ut_time);
1267         if (write(fd, (char *)&ut, sizeof(struct utmp)) != sizeof(struct utmp))
1268             (void)ftruncate(fd, buf.st_size);
1269     }
1270     close(fd);
1271 }
1272
1273 /*
1274  * Routines for locking and unlocking the serial device, moved here
1275  * from chat.c.
1276  */
1277
1278 #define LOCK_PREFIX     "/usr/spool/uucp/LCK.."
1279
1280 /*
1281  * lock - create a lock file for the named device
1282  */
1283 int
1284 lock(dev)
1285     char *dev;
1286 {
1287     int fd, pid, n;
1288     char *p;
1289
1290     if ((p = strrchr(dev, '/')) != NULL)
1291         dev = p + 1;
1292     lock_file = malloc(strlen(LOCK_PREFIX) + strlen(dev) + 1);
1293     if (lock_file == NULL)
1294         novm("lock file name");
1295     strcat(strcpy(lock_file, LOCK_PREFIX), dev);
1296
1297     while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
1298         if (errno == EEXIST
1299             && (fd = open(lock_file, O_RDONLY, 0)) >= 0) {
1300             /* Read the lock file to find out who has the device locked */
1301             n = read(fd, &pid, sizeof(pid));
1302             if (n <= 0) {
1303                 syslog(LOG_ERR, "Can't read pid from lock file %s", lock_file);
1304                 close(fd);
1305             } else {
1306                 if (kill(pid, 0) == -1 && errno == ESRCH) {
1307                     /* pid no longer exists - remove the lock file */
1308                     if (unlink(lock_file) == 0) {
1309                         close(fd);
1310                         syslog(LOG_NOTICE, "Removed stale lock on %s (pid %d)",
1311                                dev, pid);
1312                         continue;
1313                     } else
1314                         syslog(LOG_WARNING, "Couldn't remove stale lock on %s",
1315                                dev);
1316                 } else
1317                     syslog(LOG_NOTICE, "Device %s is locked by pid %d",
1318                            dev, pid);
1319             }
1320             close(fd);
1321         } else
1322             syslog(LOG_ERR, "Can't create lock file %s: %m", lock_file);
1323         free(lock_file);
1324         lock_file = NULL;
1325         return -1;
1326     }
1327
1328     pid = getpid();
1329     write(fd, &pid, sizeof pid);
1330
1331     close(fd);
1332     return 0;
1333 }
1334
1335 /*
1336  * unlock - remove our lockfile
1337  */
1338 void
1339 unlock()
1340 {
1341     if (lock_file) {
1342         unlink(lock_file);
1343         free(lock_file);
1344         lock_file = NULL;
1345     }
1346 }