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