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