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