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