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