]> git.ozlabs.org Git - ppp.git/blob - pppd/sys-NeXT.c
updated cpp defines
[ppp.git] / pppd / sys-NeXT.c
1 /*
2  * sys-next.c - System-dependent procedures for setting up
3  * PPP interfaces on NeXT 3.2/3.3  systems
4  *
5  * Copyright (c) 1989 Carnegie Mellon University.
6  * Copyright (c) 1994 Philippe-Andre Prindeville.
7  * Copyright (c) 1995 The Australian National University.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms are permitted
11  * provided that the above copyright notice and this paragraph are
12  * duplicated in all such forms and that any documentation,
13  * advertising materials, and other materials related to such
14  * distribution and use acknowledge that the software was developed
15  * by Carnegie Mellon University, Philippe-Andre Prindeville and
16  * The Australian National University.  The names of the developers
17  * may not be used to endorse or promote products derived from this
18  * software without specific prior written permission.
19  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
21  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22  */
23
24 #ifndef lint
25 static char rcsid[] = "$Id: sys-NeXT.c,v 1.6 1996/05/26 23:59:36 paulus Exp $";
26 #endif
27
28 #include <stdio.h>
29 #include <syslog.h>
30 #include <termios.h>
31 #include <utmp.h>
32 #include <unistd.h>
33 #include <stdlib.h>
34 #include <strings.h>
35 #include <sys/types.h>
36 #include <sys/file.h>
37 #include <sys/socket.h>
38 #include <sys/ioctl.h>
39 #include <sys/time.h>
40 #include <sys/errno.h>
41 #include <sys/stat.h>
42 #include <sys/fcntl.h>
43
44 #include <net/if.h>
45 #include <net/ppp_defs.h>
46 #include <net/if_ppp.h>
47 #include <netdb.h>
48 #include <netinet/in.h>
49 #include <netinet/in_systm.h>
50 #include <netinet/in_var.h>
51 #include <netinet/ip.h>
52 #include <netinet/if_ether.h>
53 #include <net/route.h>
54 #include <netinet/in.h>
55
56 #include <netinfo/ni.h>
57
58 #include "pppd.h"
59
60 static int initdisc = -1;       /* Initial TTY discipline for ppp_fd */
61 static int initfdflags = -1;    /* Initial file descriptor flags for ppp_fd */
62 static int ppp_fd = -1;         /* fd which is set to PPP discipline */
63
64 extern int errno;
65
66 static int restore_term;        /* 1 => we've munged the terminal */
67 static struct termios inittermios; /* Initial TTY termios */
68 static struct winsize wsinfo;   /* Initial window size info */
69
70 static char *lock_file;         /* name of lock file created */
71
72 static int loop_slave = -1;
73 static int loop_master;
74 static char loop_name[20];
75
76 static unsigned char inbuf[512]; /* buffer for chars read from loopback */
77
78 static int sockfd;              /* socket for doing interface ioctls */
79
80 #if defined(i386) && defined(HAS_BROKEN_IOCTL)
81 #define ioctl   myioctl
82 #endif
83
84 static int if_is_up;            /* the interface is currently up */
85 static u_int32_t ifaddrs[2];    /* local and remote addresses */
86 static u_int32_t default_route_gateway; /* gateway addr for default route */
87 static u_int32_t proxy_arp_addr;        /* remote addr for proxy arp */
88
89 /* Prototypes for procedures local to this file. */
90 static int translate_speed __P((int));
91 static int baud_rate_of __P((int));
92 static int dodefaultroute __P((u_int32_t, int));
93 static int get_ether_addr __P((u_int32_t, struct sockaddr *));
94 static int ether_by_host __P((char *, struct ether_addr *));
95
96
97 /*
98  * sys_init - System-dependent initialization.
99  */
100 void
101 sys_init()
102 {
103     /* Get an internet socket for doing socket ioctl's on. */
104     if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
105         syslog(LOG_ERR, "Couldn't create IP socket: %m");
106         die(1);
107     }
108 }
109
110 /*
111  * sys_cleanup - restore any system state we modified before exiting:
112  * mark the interface down, delete default route and/or proxy arp entry.
113  * This should call die() because it's called from die().
114  */
115 void
116 sys_cleanup()
117 {
118     struct ifreq ifr;
119
120     if (if_is_up) {
121         strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
122         if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) >= 0
123             && ((ifr.ifr_flags & IFF_UP) != 0)) {
124             ifr.ifr_flags &= ~IFF_UP;
125             ioctl(sockfd, SIOCSIFFLAGS, &ifr);
126         }
127     }
128     if (ifaddrs[0])
129         cifaddr(0, ifaddrs[0], ifaddrs[1]);
130     if (default_route_gateway)
131         cifdefaultroute(0, default_route_gateway);
132     if (proxy_arp_addr)
133         cifproxyarp(0, proxy_arp_addr);
134 }
135
136 /*
137  * sys_close - Clean up in a child process before execing.
138  */
139 void
140 sys_close()
141 {
142     close(sockfd);
143     if (loop_slave >= 0) {
144         close(loop_slave);
145         close(loop_master);
146     }
147 }
148
149 /*
150  * sys_check_options - check the options that the user specified
151  */
152 void
153 sys_check_options()
154 {
155 }
156
157
158 /*
159  * ppp_available - check whether the system has any ppp interfaces
160  * (in fact we check whether we can do an ioctl on ppp0).
161  */
162 int
163 ppp_available()
164 {
165     int s, ok;
166     struct ifreq ifr;
167
168     if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
169         return 1;               /* can't tell - maybe we're not root */
170
171     strncpy(ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
172     ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
173     close(s);
174
175     return ok;
176 }
177
178 /*
179  * establish_ppp - Turn the serial port into a ppp interface.
180  */
181 void
182 establish_ppp(fd)
183     int fd;
184 {
185     int pppdisc = PPPDISC;
186     int x;
187
188     if (demand) {
189         /*
190          * Demand mode - prime the old ppp device to relinquish the unit.
191          */
192         if (ioctl(ppp_fd, PPPIOCXFERUNIT, 0) < 0) {
193             syslog(LOG_ERR, "ioctl(transfer ppp unit): %m");
194             die(1);
195         }
196     }
197
198     /*
199      * Save the old line discipline of fd, and set it to PPP.
200      */
201     if (ioctl(fd, TIOCGETD, &initdisc) < 0) {
202         syslog(LOG_ERR, "ioctl(TIOCGETD): %m");
203         die(1);
204     }
205     if (ioctl(fd, TIOCSETD, &pppdisc) < 0) {
206         syslog(LOG_ERR, "ioctl(establish TIOCSETD): %m");
207         die(1);
208     }
209
210     if (!demand) {
211         /*
212          * Find out which interface we were given.
213          */
214         if (ioctl(fd, PPPIOCGUNIT, &ifunit) < 0) {      
215             syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m");
216             die(1);
217         }
218     } else {
219         /*
220          * Check that we got the same unit again.
221          */
222         if (ioctl(fd, PPPIOCGUNIT, &x) < 0) {   
223             syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m");
224             die(1);
225         }
226         if (x != ifunit) {
227             syslog(LOG_ERR, "transfer_ppp failed: wanted unit %d, got %d",
228                    ifunit, x);
229             die(1);
230         }
231         x = TTYDISC;
232         ioctl(loop_slave, TIOCSETD, &x);
233     }
234
235     ppp_fd = fd;
236
237     /*
238      * Enable debug in the driver if requested.
239      */
240     if (kdebugflag) {
241         if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
242             syslog(LOG_WARNING, "ioctl(PPPIOCGFLAGS): %m");
243         } else {
244             x |= (kdebugflag & 0xFF) * SC_DEBUG;
245             if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
246                 syslog(LOG_WARNING, "ioctl(PPPIOCSFLAGS): %m");
247         }
248     }
249
250     /*
251      * Set device for non-blocking reads.
252      */
253     if ((initfdflags = fcntl(fd, F_GETFL)) == -1
254         || fcntl(fd, F_SETFL, initfdflags | O_NONBLOCK) == -1) {
255         syslog(LOG_WARNING, "Couldn't set device to non-blocking mode: %m");
256     }
257 }
258
259 /*
260  * restore_loop - reattach the ppp unit to the loopback.
261  */
262 void
263 restore_loop()
264 {
265     int x;
266
267     /*
268      * Transfer the ppp interface back to the loopback.
269      */
270     if (ioctl(ppp_fd, PPPIOCXFERUNIT, 0) < 0) {
271         syslog(LOG_ERR, "ioctl(transfer ppp unit): %m");
272         die(1);
273     }
274     x = PPPDISC;
275     if (ioctl(loop_slave, TIOCSETD, &x) < 0) {
276         syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
277         die(1);
278     }
279
280     /*
281      * Check that we got the same unit again.
282      */
283     if (ioctl(loop_slave, PPPIOCGUNIT, &x) < 0) {       
284         syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m");
285         die(1);
286     }
287     if (x != ifunit) {
288         syslog(LOG_ERR, "transfer_ppp failed: wanted unit %d, got %d",
289                ifunit, x);
290         die(1);
291     }
292     ppp_fd = loop_slave;
293 }
294
295
296 /*
297  * disestablish_ppp - Restore the serial port to normal operation.
298  * This shouldn't call die() because it's called from die().
299  */
300 void
301 disestablish_ppp(fd)
302     int fd;
303 {
304     /* Reset non-blocking mode on fd. */
305     if (initfdflags != -1 && fcntl(fd, F_SETFL, initfdflags) < 0)
306         syslog(LOG_WARNING, "Couldn't restore device fd flags: %m");
307     initfdflags = -1;
308
309     /* Restore old line discipline. */
310     if (initdisc >= 0 && ioctl(fd, TIOCSETD, &initdisc) < 0)
311         syslog(LOG_ERR, "ioctl(disestablish TIOCSETD): %m");
312     initdisc = -1;
313
314     if (fd == ppp_fd)
315         ppp_fd = -1;
316 }
317
318 /*
319  * Check whether the link seems not to be 8-bit clean.
320  */
321 void
322 clean_check()
323 {
324     int x;
325     char *s;
326
327     if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) == 0) {
328         s = NULL;
329         switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP)) {
330         case SC_RCV_B7_0:
331             s = "bit 7 set to 1";
332             break;
333         case SC_RCV_B7_1:
334             s = "bit 7 set to 0";
335             break;
336         case SC_RCV_EVNP:
337             s = "odd parity";
338             break;
339         case SC_RCV_ODDP:
340             s = "even parity";
341             break;
342         }
343         if (s != NULL) {
344             syslog(LOG_WARNING, "Serial link is not 8-bit clean:");
345             syslog(LOG_WARNING, "All received characters had %s", s);
346         }
347     }
348 }
349
350 /*
351  * List of valid speeds.
352  */
353 struct speed {
354     int speed_int, speed_val;
355 } speeds[] = {
356 #ifdef B50
357     { 50, B50 },
358 #endif
359 #ifdef B75
360     { 75, B75 },
361 #endif
362 #ifdef B110
363     { 110, B110 },
364 #endif
365 #ifdef B134
366     { 134, B134 },
367 #endif
368 #ifdef B150
369     { 150, B150 },
370 #endif
371 #ifdef B200
372     { 200, B200 },
373 #endif
374 #ifdef B300
375     { 300, B300 },
376 #endif
377 #ifdef B600
378     { 600, B600 },
379 #endif
380 #ifdef B1200
381     { 1200, B1200 },
382 #endif
383 #ifdef B1800
384     { 1800, B1800 },
385 #endif
386 #ifdef B2000
387     { 2000, B2000 },
388 #endif
389 #ifdef B2400
390     { 2400, B2400 },
391 #endif
392 #ifdef B3600
393     { 3600, B3600 },
394 #endif
395 #ifdef B4800
396     { 4800, B4800 },
397 #endif
398 #ifdef B7200
399     { 7200, B7200 },
400 #endif
401 #ifdef B9600
402     { 9600, B9600 },
403 #endif
404 #ifdef B19200
405     { 19200, B19200 },
406 #endif
407 #ifdef B38400
408     { 38400, B38400 },
409 #endif
410 #ifdef EXTA
411     { 19200, EXTA },
412 #endif
413 #ifdef EXTB
414     { 38400, EXTB },
415 #endif
416 #ifdef B14400
417     { 14400, B14400 },
418 #endif
419 #ifdef B28800
420     { 28800, B28800 },
421 #endif
422 #ifdef B43200
423     { 43200, B43200 },
424 #endif
425 #ifdef B57600
426     { 57600, B57600 },
427 #endif
428 /*
429 #ifndef B115200
430 #warning Defining B115200
431 #define B115200 20
432 #endif
433 */
434 #ifdef B115200
435     { 115200, B115200 },
436 #endif
437     { 0, 0 }
438 };
439
440 /*
441  * Translate from bits/second to a speed_t.
442  */
443 int
444 translate_speed(bps)
445     int bps;
446 {
447     struct speed *speedp;
448
449     if (bps == 0)
450         return 0;
451     for (speedp = speeds; speedp->speed_int; speedp++)
452         if (bps == speedp->speed_int)
453             return speedp->speed_val;
454     syslog(LOG_WARNING, "speed %d not supported", bps);
455     return 0;
456 }
457
458 /*
459  * Translate from a speed_t to bits/second.
460  */
461 static int
462 baud_rate_of(speed)
463     int speed;
464 {
465     struct speed *speedp;
466
467     if (speed == 0)
468         return 0;
469     for (speedp = speeds; speedp->speed_int; speedp++)
470         if (speed == speedp->speed_val)
471             return speedp->speed_int;
472     return 0;
473 }
474
475
476 /*
477  * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
478  * at the requested speed, etc.  If `local' is true, set CLOCAL
479  * regardless of whether the modem option was specified.
480  */
481 void
482 set_up_tty(fd, local)
483     int fd, local;
484 {
485     int speed, x, modembits;
486     struct termios tios;
487
488     if (tcgetattr(fd, &tios) < 0) {
489         syslog(LOG_ERR, "tcgetattr: %m");
490         die(1);
491     }
492
493     if (!restore_term) {
494         inittermios = tios;
495         ioctl(fd, TIOCGWINSZ, &wsinfo);
496     }
497
498     tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
499     if (crtscts > 0 && !local)
500         tios.c_cflag |= CRTSCTS;
501     else if (crtscts < 0)
502         tios.c_cflag &= ~CRTSCTS;
503
504     tios.c_cflag |= CS8 | CREAD | HUPCL;
505     if (local || !modem)
506         tios.c_cflag |= CLOCAL;
507
508     tios.c_iflag = IGNBRK | IGNPAR;
509     tios.c_oflag = 0;
510     tios.c_lflag = 0;
511     tios.c_cc[VMIN] = 1;
512     tios.c_cc[VTIME] = 0;
513
514     if (crtscts == -2) {
515         tios.c_iflag |= IXON | IXOFF;
516         tios.c_cc[VSTOP] = 0x13;        /* DC3 = XOFF = ^S */
517         tios.c_cc[VSTART] = 0x11;       /* DC1 = XON  = ^Q */
518     }
519
520     speed = translate_speed(inspeed);
521     if (speed) {
522         cfsetospeed(&tios, speed);
523         cfsetispeed(&tios, speed);
524     } else {
525         speed = cfgetospeed(&tios);
526     }
527
528     if (modem) {
529       modembits = TIOCM_RTS | TIOCM_CTS;
530       if (ioctl(fd, (crtscts ? TIOCMBIS : TIOCMBIC), &modembits) < 0)
531         syslog(LOG_ERR, "ioctl: TIOCMBIS/BIC: %m");
532     }
533
534     if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
535         syslog(LOG_ERR, "tcsetattr: %m");
536         die(1);
537     }
538
539    baud_rate = inspeed = baud_rate_of(speed);
540    restore_term = 1;
541 }
542
543 /*
544  * restore_tty - restore the terminal to the saved settings.
545  */
546 void
547 restore_tty(fd)
548     int fd;
549 {
550     if (restore_term) {
551         if (!default_device) {
552             /*
553              * Turn off echoing, because otherwise we can get into
554              * a loop with the tty and the modem echoing to each other.
555              * We presume we are the sole user of this tty device, so
556              * when we close it, it will revert to its defaults anyway.
557              */
558             inittermios.c_lflag &= ~(ECHO | ECHONL);
559         }
560         if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
561             if (errno != ENXIO)
562                 syslog(LOG_WARNING, "tcsetattr: %m");
563         ioctl(fd, TIOCSWINSZ, &wsinfo);
564         restore_term = 0;
565     }
566 }
567
568 /*
569  * setdtr - control the DTR line on the serial port.
570  * This is called from die(), so it shouldn't call die().
571  *
572  * The write hack is to get NXFax to recognize that there is
573  * activity on the port.  Not using the write nukes
574  * NXFax's capability to determine port usage.
575  *
576  */
577 void
578 setdtr(fd, on)
579 int fd, on;
580 {
581     int modembits = TIOCM_DTR;
582
583     if (!on)
584       {
585         write(fd, " ", 1);
586         sleep(1);
587       }
588
589 /*    ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits); */
590     ioctl(fd, (on? TIOCSDTR: TIOCCDTR), 0);
591 }
592
593
594 /*
595  * open_ppp_loopback - open the device we use for getting
596  * packets in demand mode, and connect it to a ppp interface.
597  * Here we use a pty.
598  */
599 void
600 open_ppp_loopback()
601 {
602     int flags;
603     struct termios tios;
604     int pppdisc = PPPDISC;
605
606     if (openpty(&loop_master, &loop_slave, loop_name, NULL, NULL) < 0) {
607         syslog(LOG_ERR, "No free pty for loopback");
608         die(1);
609     }
610     SYSDEBUG((LOG_DEBUG, "using %s for loopback", loop_name));
611
612     if (tcgetattr(loop_slave, &tios) == 0) {
613         tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB);
614         tios.c_cflag |= CS8 | CREAD;
615         tios.c_iflag = IGNPAR;
616         tios.c_oflag = 0;
617         tios.c_lflag = 0;
618         if (tcsetattr(loop_slave, TCSAFLUSH, &tios) < 0)
619             syslog(LOG_WARNING, "couldn't set attributes on loopback: %m");
620     }
621
622     if ((flags = fcntl(loop_master, F_GETFL)) != -1) 
623         if (fcntl(loop_master, F_SETFL, flags | O_NONBLOCK) == -1)
624             syslog(LOG_WARNING, "couldn't set loopback to nonblock: %m");
625
626     ppp_fd = loop_slave;
627     if (ioctl(ppp_fd, TIOCSETD, &pppdisc) < 0) {
628         syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
629         die(1);
630     }
631
632     /*
633      * Find out which interface we were given.
634      */
635     if (ioctl(ppp_fd, PPPIOCGUNIT, &ifunit) < 0) {      
636         syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m");
637         die(1);
638     }
639
640     /*
641      * Enable debug in the driver if requested.
642      */
643     if (kdebugflag) {
644         if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &flags) < 0) {
645             syslog(LOG_WARNING, "ioctl (PPPIOCGFLAGS): %m");
646         } else {
647             flags |= (kdebugflag & 0xFF) * SC_DEBUG;
648             if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &flags) < 0)
649                 syslog(LOG_WARNING, "ioctl(PPPIOCSFLAGS): %m");
650         }
651     }
652
653 }
654
655
656 /*
657  * output - Output PPP packet.
658  */
659 void
660 output(unit, p, len)
661     int unit;
662     u_char *p;
663     int len;
664 {
665     if (debug)
666         log_packet(p, len, "sent ");
667
668     if (write(ttyfd, p, len) < 0) {
669         if (errno == EWOULDBLOCK || errno == ENOBUFS
670             || errno == ENXIO || errno == EIO) {
671             syslog(LOG_WARNING, "write: warning: %m");
672         } else {
673             syslog(LOG_ERR, "write: %m");
674             die(1);
675         }
676     }
677 }
678
679
680 /*
681  * wait_input - wait until there is data available on ttyfd,
682  * for the length of time specified by *timo (indefinite
683  * if timo is NULL).
684  */
685 void
686 wait_input(timo)
687     struct timeval *timo;
688 {
689     fd_set ready;
690     int n;
691
692     FD_ZERO(&ready);
693     FD_SET(ttyfd, &ready);
694     n = select(ttyfd+1, &ready, NULL, &ready, timo);
695     if (n < 0 && errno != EINTR) {
696         syslog(LOG_ERR, "select: %m");
697         die(1);
698     }
699 }
700
701
702 /*
703  * wait_loop_output - wait until there is data available on the
704  * loopback, for the length of time specified by *timo (indefinite
705  * if timo is NULL).
706  */
707 wait_loop_output(timo)
708     struct timeval *timo;
709 {
710     fd_set ready;
711     int n;
712
713     FD_ZERO(&ready);
714     FD_SET(loop_master, &ready);
715     n = select(loop_master + 1, &ready, NULL, &ready, timo);
716     if (n < 0 && errno != EINTR) {
717         syslog(LOG_ERR, "select: %m");
718         die(1);
719     }
720 }
721
722
723 /*
724  * wait_time - wait for a given length of time or until a
725  * signal is received.
726  */
727 wait_time(timo)
728     struct timeval *timo;
729 {
730     int n;
731
732     n = select(0, NULL, NULL, NULL, timo);
733     if (n < 0 && errno != EINTR) {
734         syslog(LOG_ERR, "select: %m");
735         die(1);
736     }
737 }
738
739
740 /*
741  * read_packet - get a PPP packet from the serial device.
742  */
743 int
744 read_packet(buf)
745     u_char *buf;
746 {
747     int len;
748
749     if ((len = read(ttyfd, buf, PPP_MTU + PPP_HDRLEN)) < 0) {
750         if (errno == EWOULDBLOCK || errno == EINTR)
751             return -1;
752         syslog(LOG_ERR, "read: %m");
753         die(1);
754     }
755     return len;
756 }
757
758
759 /*
760  * get_loop_output - read characters from the loopback, form them
761  * into frames, and detect when we want to bring the real link up.
762  * Return value is 1 if we need to bring up the link, 0 otherwise.
763  */
764 int
765 get_loop_output()
766 {
767     int rv = 0;
768     int n;
769
770     while ((n = read(loop_master, inbuf, sizeof(inbuf))) >= 0) {
771         if (loop_chars(inbuf, n))
772             rv = 1;
773     }
774
775     if (n == 0) {
776         syslog(LOG_ERR, "eof on loopback");
777         die(1);
778     } else if (errno != EWOULDBLOCK){
779         syslog(LOG_ERR, "read from loopback: %m");
780         die(1);
781     }
782
783     return rv;
784 }
785
786
787 /*
788  * ppp_send_config - configure the transmit characteristics of
789  * the ppp interface.
790  */
791 void
792 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
793     int unit, mtu;
794     u_int32_t asyncmap;
795     int pcomp, accomp;
796 {
797     u_int x;
798     struct ifreq ifr;
799
800     strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
801     ifr.ifr_mtu = mtu;
802     if (ioctl(sockfd, SIOCSIFMTU, (caddr_t) &ifr) < 0) {
803         syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m");
804         quit();
805     }
806
807     if (ioctl(ppp_fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0) {
808         syslog(LOG_ERR, "ioctl(PPPIOCSASYNCMAP): %m");
809         quit();
810     }
811
812     if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
813         syslog(LOG_ERR, "ioctl(PPPIOCGFLAGS): %m");
814         quit();
815     }
816     x = pcomp? x | SC_COMP_PROT: x &~ SC_COMP_PROT;
817     x = accomp? x | SC_COMP_AC: x &~ SC_COMP_AC;
818     if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
819         syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
820         quit();
821     }
822 }
823
824
825 /*
826  * ppp_set_xaccm - set the extended transmit ACCM for the interface.
827  */
828 void
829 ppp_set_xaccm(unit, accm)
830     int unit;
831     ext_accm accm;
832 {
833     if (ioctl(ppp_fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY)
834         syslog(LOG_WARNING, "ioctl(set extended ACCM): %m");
835 }
836
837
838 /*
839  * ppp_recv_config - configure the receive-side characteristics of
840  * the ppp interface.
841  */
842 void
843 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
844     int unit, mru;
845     u_int32_t asyncmap;
846     int pcomp, accomp;
847 {
848     int x;
849
850     if (ioctl(ppp_fd, PPPIOCSMRU, (caddr_t) &mru) < 0) {
851         syslog(LOG_ERR, "ioctl(PPPIOCSMRU): %m");
852         quit();
853     }
854     if (ioctl(ppp_fd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0) {
855         syslog(LOG_ERR, "ioctl(PPPIOCSRASYNCMAP): %m");
856         quit();
857     }
858     if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
859         syslog(LOG_ERR, "ioctl(PPPIOCGFLAGS): %m");
860         quit();
861     }
862     x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC;
863     if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
864         syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
865         quit();
866     }
867 }
868
869 /*
870  * ccp_test - ask kernel whether a given compression method
871  * is acceptable for use.  Returns 1 if the method and parameters
872  * are OK, 0 if the method is known but the parameters are not OK
873  * (e.g. code size should be reduced), or -1 if the method is unknown.
874  */
875 int
876 ccp_test(unit, opt_ptr, opt_len, for_transmit)
877     int unit, opt_len, for_transmit;
878     u_char *opt_ptr;
879 {
880     struct ppp_option_data data;
881
882     data.ptr = opt_ptr;
883     data.length = opt_len;
884     data.transmit = for_transmit;
885     if (ioctl(ttyfd, PPPIOCSCOMPRESS, (caddr_t) &data) >= 0)
886         return 1;
887     return (errno == ENOBUFS)? 0: -1;
888 }
889
890 /*
891  * ccp_flags_set - inform kernel about the current state of CCP.
892  */
893 void
894 ccp_flags_set(unit, isopen, isup)
895     int unit, isopen, isup;
896 {
897     int x;
898
899     if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
900         syslog(LOG_ERR, "ioctl(PPPIOCGFLAGS): %m");
901         return;
902     }
903     x = isopen? x | SC_CCP_OPEN: x &~ SC_CCP_OPEN;
904     x = isup? x | SC_CCP_UP: x &~ SC_CCP_UP;
905     if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
906         syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
907 }
908
909 /*
910  * ccp_fatal_error - returns 1 if decompression was disabled as a
911  * result of an error detected after decompression of a packet,
912  * 0 otherwise.  This is necessary because of patent nonsense.
913  */
914 int
915 ccp_fatal_error(unit)
916     int unit;
917 {
918     int x;
919
920     if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
921         syslog(LOG_ERR, "ioctl(PPPIOCGFLAGS): %m");
922         return 0;
923     }
924     return x & SC_DC_FERROR;
925 }
926
927 /*
928  * get_idle_time - return how long the link has been idle.
929  */
930 int
931 get_idle_time(u, ip)
932     int u;
933     struct ppp_idle *ip;
934 {
935     return ioctl(ppp_fd, PPPIOCGIDLE, ip) >= 0;
936 }
937
938
939 /*
940  * set_filters - transfer the pass and active filters to the kernel.
941  */
942 int
943 set_filters(pass, active)
944     struct bpf_program *pass, *active;
945 {
946     int ret = 1;
947
948     if (pass->bf_len > 0) {
949         if (ioctl(ppp_fd, PPPIOCSPASS, pass) < 0) {
950             syslog(LOG_ERR, "Couldn't set pass-filter in kernel: %m");
951             ret = 0;
952         }
953     }
954     if (active->bf_len > 0) {
955         if (ioctl(ppp_fd, PPPIOCSACTIVE, active) < 0) {
956             syslog(LOG_ERR, "Couldn't set active-filter in kernel: %m");
957             ret = 0;
958         }
959     }
960     return ret;
961 }
962
963
964 /*
965  * sifvjcomp - config tcp header compression
966  */
967 int
968 sifvjcomp(u, vjcomp, cidcomp, maxcid)
969     int u, vjcomp, cidcomp, maxcid;
970 {
971     u_int x;
972
973     if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
974         syslog(LOG_ERR, "ioctl(PPPIOCGFLAGS): %m");
975         return 0;
976     }
977     x = vjcomp ? x | SC_COMP_TCP: x &~ SC_COMP_TCP;
978     x = cidcomp? x & ~SC_NO_TCP_CCID: x | SC_NO_TCP_CCID;
979     if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
980         syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
981         return 0;
982     }
983     if (ioctl(ppp_fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0) {
984         syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
985         return 0;
986     }
987     return 1;
988 }
989
990 /*
991  * sifup - Config the interface up and enable IP packets to pass.
992  */
993 int
994 sifup(u)
995     int u;
996 {
997     struct ifreq ifr;
998
999     strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
1000     if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
1001         syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
1002         return 0;
1003     }
1004     ifr.ifr_flags |= IFF_UP;
1005     if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
1006         syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
1007         return 0;
1008     }
1009     if_is_up = 1;
1010     return 1;
1011 }
1012
1013 /*
1014  * sifnpmode - Set the mode for handling packets for a given NP.
1015  */
1016 int
1017 sifnpmode(u, proto, mode)
1018     int u;
1019     int proto;
1020     enum NPmode mode;
1021 {
1022     struct npioctl npi;
1023
1024     npi.protocol = proto;
1025     npi.mode = mode;
1026     if (ioctl(ppp_fd, PPPIOCSNPMODE, &npi) < 0) {
1027         syslog(LOG_ERR, "ioctl(set NP %d mode to %d): %m", proto, mode);
1028         return 0;
1029     }
1030     return 1;
1031 }
1032
1033 /*
1034  * sifdown - Config the interface down and disable IP.
1035  */
1036 int
1037 sifdown(u)
1038     int u;
1039 {
1040     struct ifreq ifr;
1041     int rv;
1042     struct npioctl npi;
1043
1044     rv = 1;
1045     npi.protocol = PPP_IP;
1046     npi.mode = NPMODE_ERROR;
1047     ioctl(ppp_fd, PPPIOCSNPMODE, (caddr_t) &npi);
1048     /* ignore errors, because ppp_fd might have been closed by now. */
1049
1050     strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
1051     if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
1052         syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
1053         rv = 0;
1054     } else {
1055         ifr.ifr_flags &= ~IFF_UP;
1056         if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
1057             syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
1058             rv = 0;
1059         } else
1060             if_is_up = 0;
1061     }
1062     return rv;
1063 }
1064
1065 /*
1066  * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
1067  * if it exists.
1068  */
1069 #define SET_SA_FAMILY(addr, family)             \
1070     BZERO((char *) &(addr), sizeof(addr));      \
1071     addr.sa_family = (family); 
1072
1073 /*
1074  * sifaddr - Config the interface IP addresses and netmask.
1075  */
1076 int
1077 sifaddr(u, o, h, m)
1078     int u;
1079     u_int32_t o, h, m;
1080 {
1081     int ret;
1082     struct ifreq ifr;
1083
1084     ret = 1;
1085     strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
1086     SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
1087     if (m != 0) {
1088         ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = m;
1089         syslog(LOG_INFO, "Setting interface mask to %s\n", ip_ntoa(m));
1090         if (ioctl(sockfd, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
1091             syslog(LOG_ERR, "ioctl(SIOCSIFNETMASK): %m");
1092             ret = 0;
1093         }
1094     }
1095     ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
1096     if (ioctl(sockfd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
1097         syslog(LOG_ERR, "ioctl(SIOCAIFADDR): %m");
1098         ret = 0;
1099     }
1100     ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = h;
1101     if (ioctl(sockfd, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
1102         syslog(LOG_ERR, "ioctl(SIOCSIFDSTADDR): %m");
1103         ret = 0;
1104     }
1105     ifaddrs[0] = o;
1106     ifaddrs[1] = h;
1107     return ret;
1108 }
1109
1110 /*
1111  * cifaddr - Clear the interface IP addresses, and delete routes
1112  * through the interface if possible.
1113  *
1114  * N.B.: under NextStep, you can't *delete* an address on an interface,
1115  * so we change it to 0.0.0.0...  A real hack.  But it simplifies
1116  * reconnection on the server side.
1117  */
1118 int
1119 cifaddr(u, o, h)
1120     int u;
1121     u_int32_t o, h;
1122 {
1123     struct rtentry rt;
1124
1125 #if 1
1126     h = o = 0L;
1127     (void) sifaddr(u, o, h, 0L);
1128 #endif
1129     ifaddrs[0] = 0;
1130     BZERO(&rt, sizeof(rt));
1131     SET_SA_FAMILY(rt.rt_dst, AF_INET);
1132     ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = h;
1133     SET_SA_FAMILY(rt.rt_gateway, AF_INET);
1134     ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = o;
1135     rt.rt_flags = RTF_HOST;
1136     if (ioctl(sockfd, SIOCDELRT, (caddr_t) &rt) < 0) {
1137         syslog(LOG_ERR, "ioctl(SIOCDELRT): %m");
1138         return 0;
1139     }
1140     return 1;
1141 }
1142
1143 /*
1144  * sifdefaultroute - assign a default route through the address given.
1145  */
1146 int
1147 sifdefaultroute(u, g)
1148     int u;
1149     u_int32_t g;
1150 {
1151     return dodefaultroute(g, 's');
1152 }
1153
1154 /*
1155  * cifdefaultroute - delete a default route through the address given.
1156  */
1157 int
1158 cifdefaultroute(u, g)
1159     int u;
1160     u_int32_t g;
1161 {
1162     return dodefaultroute(g, 'c');
1163 }
1164
1165 /*
1166  * dodefaultroute - talk to a routing socket to add/delete a default route.
1167  */
1168 int
1169 dodefaultroute(g, cmd)
1170     u_int32_t g;
1171     int cmd;
1172 {
1173     struct rtentry rt;
1174
1175     BZERO(&rt, sizeof(rt));
1176     SET_SA_FAMILY(rt.rt_dst, AF_INET);
1177     ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = 0L;
1178     SET_SA_FAMILY(rt.rt_gateway, AF_INET);
1179     ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
1180     rt.rt_flags = RTF_GATEWAY;
1181     if (ioctl(sockfd, (cmd == 's') ? SIOCADDRT : SIOCDELRT, &rt) < 0) {
1182         syslog(LOG_ERR, "%cifdefaultroute: ioctl(%s): %m", cmd,
1183                (cmd == 's') ? "SIOCADDRT" : "SIOCDELRT");
1184         return 0;
1185     }
1186     default_route_gateway = (cmd == 's')? g: 0;
1187     return 1;
1188 }
1189
1190 /*
1191  * sifproxyarp - Make a proxy ARP entry for the peer.
1192  */
1193 int
1194 sifproxyarp(unit, hisaddr)
1195     int unit;
1196     u_int32_t hisaddr;
1197 {
1198     struct arpreq arpreq;
1199
1200     BZERO(&arpreq, sizeof(arpreq));
1201
1202     /*
1203      * Get the hardware address of an interface on the same subnet
1204      * as our local address.
1205      */
1206     if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) {
1207         syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
1208         return 0;
1209     }
1210
1211     SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1212     ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1213     arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1214     if (ioctl(sockfd, SIOCSARP, (caddr_t)&arpreq) < 0) {
1215         syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
1216         return 0;
1217     }
1218
1219     proxy_arp_addr = hisaddr;
1220     return 1;
1221 }
1222
1223 /*
1224  * cifproxyarp - Delete the proxy ARP entry for the peer.
1225  */
1226 int
1227 cifproxyarp(unit, hisaddr)
1228     int unit;
1229     u_int32_t hisaddr;
1230 {
1231     struct arpreq arpreq;
1232
1233     BZERO(&arpreq, sizeof(arpreq));
1234     SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1235     ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1236     if (ioctl(sockfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
1237         syslog(LOG_WARNING, "ioctl(SIOCDARP): %m");
1238         return 0;
1239     }
1240     proxy_arp_addr = 0;
1241     return 1;
1242 }
1243
1244 /*
1245  * get_ether_addr - get the hardware address of an interface on the
1246  * the same subnet as ipaddr.
1247  */
1248 #define MAX_IFS         32
1249
1250 int
1251 get_ether_addr(ipaddr, hwaddr)
1252     u_int32_t ipaddr;
1253     struct sockaddr *hwaddr;
1254 {
1255     struct ifreq *ifr, *ifend, *ifp;
1256     u_int32_t ina, mask;
1257     struct ether_addr dla;
1258     struct ifreq ifreq;
1259     struct ifconf ifc;
1260     struct ifreq ifs[MAX_IFS];
1261     struct hostent *hostent;
1262
1263     ifc.ifc_len = sizeof(ifs);
1264     ifc.ifc_req = ifs;
1265     if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1266         syslog(LOG_ERR, "ioctl(SIOCGIFCONF): %m");
1267         return 0;
1268     }
1269
1270     /*
1271      * Scan through looking for an interface with an Internet
1272      * address on the same subnet as `ipaddr'.
1273      */
1274     ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1275     for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1276                 ((char *)&ifr->ifr_addr + sizeof(struct sockaddr))) {
1277         if (ifr->ifr_addr.sa_family == AF_INET) {
1278             ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1279             strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1280             /*
1281              * Check that the interface is up, and not point-to-point
1282              * or loopback.
1283              */
1284             if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1285                 continue;
1286             if ((ifreq.ifr_flags &
1287                  (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
1288                  != (IFF_UP|IFF_BROADCAST))
1289                 continue;
1290             /*
1291              * Get its netmask and check that it's on the right subnet.
1292              */
1293             if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1294                 continue;
1295             mask = ((struct sockaddr_in*)&ifreq.ifr_addr)->sin_addr.s_addr;
1296             if ((ipaddr & mask) != (ina & mask))
1297                 continue;
1298
1299             break;
1300         }
1301     }
1302
1303     if (ifr >= ifend)
1304         return 0;
1305     syslog(LOG_INFO, "found interface %s for proxy arp", ifr->ifr_name);
1306
1307     /*
1308      * Get the hostname and look for an entry using the ethers database.
1309      * Under NeXTStep this is the best we can do for now.
1310      */
1311     if ((hostent = gethostbyaddr((char*)&ina, sizeof(ina), AF_INET)) == NULL)
1312         return 0;
1313
1314     if (ether_by_host(hostent->h_name, &dla)) {
1315         syslog(LOG_INFO, "Add entry for %s in /etc/ethers", hostent->h_name);
1316         return 0;       /* it's not there */
1317     }
1318     hwaddr->sa_family = AF_UNSPEC;
1319     BCOPY(&dla, hwaddr->sa_data, sizeof(dla));
1320     return 1;
1321 }
1322
1323 static int
1324 ether_by_host(hostname, etherptr)
1325     char *hostname;
1326     struct ether_addr *etherptr;
1327 {
1328     struct ether_addr *thisptr;
1329     void *conn;
1330     ni_id root;
1331     ni_namelist val;
1332     char path[256];
1333
1334     if (!ether_hostton(hostname, etherptr))
1335         return 0;
1336     /*
1337      * We shall now try and
1338      * find the address in the
1339      * top domain of netinfo.
1340      */
1341     strcat(strcpy(path, "/machines/"), hostname);
1342
1343     if (ni_open((void *)0, "/", &conn)
1344      || ni_root(conn, &root)
1345      || ni_pathsearch(conn, &root, path)
1346      || ni_lookupprop(conn, &root, "en_address", &val))
1347         return 1;
1348
1349     /*
1350      * Now we can convert the returned string into an ethernet address.
1351      */
1352     strcpy(path, val.ni_namelist_val[0]);
1353     ni_free(conn);
1354     if ((thisptr = (struct ether_addr*)ether_aton(path)) == NULL)
1355         return 1;
1356     BCOPY(thisptr, etherptr, sizeof(struct ether_addr));
1357     return 0;
1358 }
1359
1360
1361
1362 /*
1363  * Return user specified netmask, modified by any mask we might determine
1364  * for address `addr' (in network byte order).
1365  * Here we scan through the system's list of interfaces, looking for
1366  * any non-point-to-point interfaces which might appear to be on the same
1367  * network as `addr'.  If we find any, we OR in their netmask to the
1368  * user-specified netmask.
1369  */
1370 u_int32_t
1371 GetMask(addr)
1372     u_int32_t addr;
1373 {
1374     u_int32_t mask, nmask, ina;
1375     struct ifreq *ifr, *ifend, ifreq;
1376     struct ifconf ifc;
1377     struct ifreq ifs[MAX_IFS];
1378
1379     addr = ntohl(addr);
1380     if (IN_CLASSA(addr))        /* determine network mask for address class */
1381         nmask = IN_CLASSA_NET;
1382     else if (IN_CLASSB(addr))
1383         nmask = IN_CLASSB_NET;
1384     else
1385         nmask = IN_CLASSC_NET;
1386     /* class D nets are disallowed by bad_ip_adrs */
1387     mask = netmask | htonl(nmask);
1388
1389     /*
1390      * Scan through the system's network interfaces.
1391      */
1392     ifc.ifc_len = sizeof(ifs);
1393     ifc.ifc_req = ifs;
1394     if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1395         syslog(LOG_WARNING, "ioctl(SIOCGIFCONF): %m");
1396         return mask;
1397     }
1398     ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1399     for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1400                 ((char *)&ifr->ifr_addr + sizeof(struct sockaddr))) {
1401         /*
1402          * Check the interface's internet address.
1403          */
1404         if (ifr->ifr_addr.sa_family != AF_INET)
1405             continue;
1406         ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1407         if ((ntohl(ina) & nmask) != (addr & nmask))
1408             continue;
1409         /*
1410          * Check that the interface is up, and not point-to-point or loopback.
1411          */
1412         strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1413         if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1414             continue;
1415         if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
1416             != IFF_UP)
1417             continue;
1418         /*
1419          * Get its netmask and OR it into our mask.
1420          */
1421         if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1422             continue;
1423         mask |= ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr;
1424     }
1425
1426     return mask;
1427 }
1428
1429
1430
1431 /*
1432  * daemon - Detach us from the terminal session.
1433  */
1434 int
1435 daemon(nochdir, noclose)
1436     int nochdir, noclose;
1437 {
1438     int pid;
1439
1440     if ((pid = fork()) < 0)
1441         return -1;
1442     if (pid != 0)
1443         exit(0);                /* parent dies */
1444     (void)setsid();
1445     if (!nochdir)
1446         chdir("/");
1447     if (!noclose) {
1448         fclose(stdin);          /* don't need stdin, stdout, stderr */
1449         fclose(stdout);
1450         fclose(stderr);
1451     }
1452     return 0;
1453 }
1454
1455
1456 char *
1457 strdup(s)
1458     const char *s;
1459 {
1460     char *d = malloc(strlen(s) + 1);
1461
1462     if (d) strcpy(d, s);
1463     return d;
1464 }
1465
1466 /*
1467  * This logwtmp() implementation is subject to the following copyright:
1468  *
1469  * Copyright (c) 1988 The Regents of the University of California.
1470  * All rights reserved.
1471  *
1472  * Redistribution and use in source and binary forms are permitted
1473  * provided that the above copyright notice and this paragraph are
1474  * duplicated in all such forms and that any documentation,
1475  * advertising materials, and other materials related to such
1476  * distribution and use acknowledge that the software was developed
1477  * by the University of California, Berkeley.  The name of the
1478  * University may not be used to endorse or promote products derived
1479  * from this software without specific prior written permission.
1480  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1481  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1482  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1483  */
1484
1485 #define WTMPFILE        "/usr/adm/wtmp"
1486
1487 int
1488 logwtmp(line, name, host)
1489     char *line, *name, *host;
1490 {
1491     int fd;
1492     struct stat buf;
1493     struct utmp ut;
1494
1495     if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0)
1496         return;
1497     if (!fstat(fd, &buf)) {
1498         (void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
1499         (void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
1500         (void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
1501         (void)time(&ut.ut_time);
1502         if (write(fd, (char *)&ut, sizeof(struct utmp)) != sizeof(struct utmp))
1503             (void)ftruncate(fd, buf.st_size);
1504     }
1505     close(fd);
1506 }
1507
1508 /*
1509  * Routines for locking and unlocking the serial device, moved here
1510  * from chat.c.
1511  */
1512
1513 #define LOCK_PREFIX     "/usr/spool/uucp/LCK/LCK.."
1514
1515 /*
1516  * lock - create a lock file for the named device
1517  */
1518 int
1519 lock(dev)
1520     char *dev;
1521 {
1522     int fd, pid, n;
1523     char *p;
1524
1525     if ((p = strrchr(dev, '/')) != NULL)
1526         dev = p + 1;
1527     lock_file = malloc(strlen(LOCK_PREFIX) + strlen(dev) + 1);
1528     if (lock_file == NULL)
1529         novm("lock file name");
1530     strcat(strcpy(lock_file, LOCK_PREFIX), dev);
1531
1532     while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
1533         if (errno == EEXIST
1534             && (fd = open(lock_file, O_RDONLY, 0)) >= 0) {
1535             /* Read the lock file to find out who has the device locked */
1536             n = read(fd, &pid, sizeof(pid));
1537             if (n <= 0) {
1538                 syslog(LOG_ERR, "Can't read pid from lock file %s", lock_file);
1539                 close(fd);
1540             } else {
1541                 if (kill(pid, 0) == -1 && errno == ESRCH) {
1542                     /* pid no longer exists - remove the lock file */
1543                     if (unlink(lock_file) == 0) {
1544                         close(fd);
1545                         syslog(LOG_NOTICE, "Removed stale lock on %s (pid %d)",
1546                                dev, pid);
1547                         continue;
1548                     } else
1549                         syslog(LOG_WARNING, "Couldn't remove stale lock on %s",
1550                                dev);
1551                 } else
1552                     syslog(LOG_NOTICE, "Device %s is locked by pid %d",
1553                            dev, pid);
1554             }
1555             close(fd);
1556         } else
1557             syslog(LOG_ERR, "Can't create lock file %s: %m", lock_file);
1558         free(lock_file);
1559         lock_file = NULL;
1560         return -1;
1561     }
1562
1563     pid = getpid();
1564     write(fd, &pid, sizeof pid);
1565
1566     close(fd);
1567     return 0;
1568 }
1569
1570 /*
1571  * unlock - remove our lockfile
1572  */
1573 void
1574 unlock()
1575 {
1576     if (lock_file) {
1577         unlink(lock_file);
1578         free(lock_file);
1579         lock_file = NULL;
1580     }
1581 }
1582
1583 #if defined(i386) && defined(HAS_BROKEN_IOCTL)
1584 int
1585 ioctl(fd, cmd, c)
1586     int fd, cmd;
1587     caddr_t c;
1588 {
1589 #undef  ioctl
1590     int ret;
1591
1592 #ifdef DEBUGIOCTL
1593     int serrno;
1594     u_char let, code, size;
1595
1596     size = (cmd >> 16) & IOCPARM_MASK;
1597     let = (cmd >> 8);
1598     code = cmd;
1599
1600     if (let == 't' && (75 <= code && code <= 90))
1601     syslog(LOG_INFO, "ioctl(%d, 0x%x ('%c', %d, %d), 0x%x)\n", fd, cmd,
1602            let, code, size, c);
1603 #endif
1604
1605     ret = ioctl(fd, cmd, c);
1606
1607 #ifdef DEBUGIOCTL
1608     serrno = errno;
1609     if (ret == -1)
1610         syslog(LOG_INFO, "ioctl('%c', %d, %d) errno = %d (%m)\n",
1611                 let, code, size, errno);
1612     if (let == 't' && (75 <= code && code <= 90) && (cmd & IOC_OUT)) {
1613         int i, len = ((cmd >> 16) & IOCPARM_MASK);
1614         for (i = 0; i < len / 4; ++i)
1615                 syslog(LOG_INFO, "word[%d] @ 0x%06x = 0x%x\n",
1616                        i, &((int *) c)[i],((int *)c)[i]);
1617     }
1618     errno = serrno;
1619 #endif
1620
1621     if (ret == -1 && errno == EPERM)
1622         errno = ret = 0;
1623     return ret;
1624 }
1625 #endif  /* HAS_BROKEN_IOCTL */
1626
1627
1628 #if defined(FIXSIGS) && (defined (hppa) || defined(sparc))
1629
1630 /*
1631  * These redefinitions of Posix functions are necessary
1632  * because HPPA systems have an OS bug that causes 
1633  * sigaction to core dump:
1634  *
1635  * AlainF 9-Nov-1994    HACK FOR HP-PA/NEXTSTEP
1636  *                      sigaction(3) seems broken in the HP-PA NeXTSTEP 3.2
1637  *                      Posix lib. This causes pppd to SIGBUS at the expiration
1638  *                      of the first timeout (_sigtramp seems to invoke
1639  *                      the SIGALRM handler at an unreasonably low address).
1640  *                      All calls so sigaction(3) have been changed to calls
1641  *                      to sigvec(2) and sigprocmask(SIG_BLOCK,...) to
1642  *                      sigblock(2).
1643  *                      This is kind of a hack, especially since there are
1644  *                      other routines of the Posix lib still used, but
1645  *                      it worked for me.
1646  *
1647  * Dave Hess <David-Hess@net.tamu.edu> noted that 3.3 Sparc seems to
1648  * have the same bug.  Thus this fix has been enabled for SPARC also.
1649  *
1650  *
1651  */
1652
1653 int sigemptyset(sigset_t *mask)
1654 {
1655   *mask = 0;
1656 }
1657
1658 sigaddset(sigset_t *mask, int which_sig)
1659 {
1660   *mask |= sigmask(which_sig);
1661 }
1662
1663
1664 int sigaction(int sig, const struct sigaction *act, struct sigaction *oact)
1665 {
1666    struct sigvec sv;
1667    static int in = 0;
1668
1669    sv.sv_handler = act->sa_handler;
1670    sv.sv_mask = act->sa_mask;
1671    sv.sv_flags = 0;
1672
1673    if (!in)
1674      {
1675        in = 1;
1676        syslog(LOG_WARNING, "PPPD: Inside modified HP and SPARC sigaction\n");
1677      }
1678
1679    return sigvec(sig, &sv, NULL);
1680 }
1681
1682 #endif