]> git.ozlabs.org Git - ppp.git/blob - pppd/sys-ultrix.c
changed order of args to strlcpy/strlcat.
[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.28 1999/03/19 01:29:51 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(int fd)
590 {
591     FD_SET(fd, &in_fds);
592     if (fd > max_in_fd)
593         max_in_fd = fd;
594 }
595
596 /*
597  * remove_fd - remove an fd from the set that wait_input waits for.
598  */
599 void remove_fd(int fd)
600 {
601     FD_CLR(fd, &in_fds);
602 }
603
604 #if 0
605 /*
606  * wait_loop_output - wait until there is data available on the
607  * loopback, for the length of time specified by *timo (indefinite
608  * if timo is NULL).
609  */
610 void
611 wait_loop_output(timo)
612     struct timeval *timo;
613 {
614     wait_time(timo);
615 }
616
617
618 /*
619  * wait_time - wait for a given length of time or until a
620  * signal is received.
621  */
622 void
623 wait_time(timo)
624     struct timeval *timo;
625 {
626     int n;
627
628     n = select(0, NULL, NULL, NULL, timo);
629     if (n < 0 && errno != EINTR)
630         fatal("select: %m");
631 }
632 #endif
633
634 /*
635  * read_packet - get a PPP packet from the serial device.
636  */
637 int
638 read_packet(buf)
639     u_char *buf;
640 {
641     int len;
642
643     if ((len = read(ttyfd, buf, PPP_MTU + PPP_HDRLEN)) < 0) {
644         if (errno == EWOULDBLOCK || errno == EINTR)
645             return -1;
646         fatal("read(fd): %m");
647     }
648     return len;
649 }
650
651
652 /*
653  * get_loop_output - read characters from the loopback, form them
654  * into frames, and detect when we want to bring the real link up.
655  * Return value is 1 if we need to bring up the link, 0 otherwise.
656  */
657 int
658 get_loop_output()
659 {
660     return 0;
661 }
662
663
664 /*
665  * ppp_send_config - configure the transmit characteristics of
666  * the ppp interface.
667  */
668 void
669 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
670     int unit, mtu;
671     u_int32_t asyncmap;
672     int pcomp, accomp;
673 {
674     u_int x;
675
676     if (ioctl(ppp_fd, PPPIOCSMTU, &mtu) < 0)
677         fatal("ioctl(PPPIOCSMTU): %m");
678
679     if (ioctl(ppp_fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0)
680         fatal("ioctl(PPPIOCSASYNCMAP): %m");
681
682     if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0)
683         fatal("ioctl (PPPIOCGFLAGS): %m");
684     x = pcomp? x | SC_COMP_PROT: x &~ SC_COMP_PROT;
685     x = accomp? x | SC_COMP_AC: x &~ SC_COMP_AC;
686     if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
687         fatal("ioctl(PPPIOCSFLAGS): %m");
688 }
689
690
691 /*
692  * ppp_set_xaccm - set the extended transmit ACCM for the interface.
693  */
694 void
695 ppp_set_xaccm(unit, accm)
696     int unit;
697     ext_accm accm;
698 {
699     if (ioctl(ppp_fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY)
700         warn("ioctl(set extended ACCM): %m");
701 }
702
703
704 /*
705  * ppp_recv_config - configure the receive-side characteristics of
706  * the ppp interface.
707  */
708 void
709 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
710     int unit, mru;
711     u_int32_t asyncmap;
712     int pcomp, accomp;
713 {
714     int x;
715
716     if (ioctl(ppp_fd, PPPIOCSMRU, (caddr_t) &mru) < 0)
717         fatal("ioctl(PPPIOCSMRU): %m");
718     if (ioctl(ppp_fd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0)
719         fatal("ioctl(PPPIOCSRASYNCMAP): %m");
720     if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0)
721         fatal("ioctl (PPPIOCGFLAGS): %m");
722     x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC;
723     if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
724         fatal("ioctl(PPPIOCSFLAGS): %m");
725 }
726
727 /*
728  * ccp_test - ask kernel whether a given compression method
729  * is acceptable for use.  Returns 1 if the method and parameters
730  * are OK, 0 if the method is known but the parameters are not OK
731  * (e.g. code size should be reduced), or -1 if the method is unknown.
732  */
733 int
734 ccp_test(unit, opt_ptr, opt_len, for_transmit)
735     int unit, opt_len, for_transmit;
736     u_char *opt_ptr;
737 {
738     struct ppp_option_data data;
739
740     data.ptr = opt_ptr;
741     data.length = opt_len;
742     data.transmit = for_transmit;
743     if (ioctl(ttyfd, PPPIOCSCOMPRESS, (caddr_t) &data) >= 0)
744         return 1;
745     return (errno == ENOBUFS)? 0: -1;
746 }
747
748 /*
749  * ccp_flags_set - inform kernel about the current state of CCP.
750  */
751 void
752 ccp_flags_set(unit, isopen, isup)
753     int unit, isopen, isup;
754 {
755     int x;
756
757     if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
758         error("ioctl (PPPIOCGFLAGS): %m");
759         return;
760     }
761     x = isopen? x | SC_CCP_OPEN: x &~ SC_CCP_OPEN;
762     x = isup? x | SC_CCP_UP: x &~ SC_CCP_UP;
763     if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
764         error("ioctl(PPPIOCSFLAGS): %m");
765 }
766
767 /*
768  * ccp_fatal_error - returns 1 if decompression was disabled as a
769  * result of an error detected after decompression of a packet,
770  * 0 otherwise.  This is necessary because of patent nonsense.
771  */
772 int
773 ccp_fatal_error(unit)
774     int unit;
775 {
776     int x;
777
778     if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
779         error("ioctl(PPPIOCGFLAGS): %m");
780         return 0;
781     }
782     return x & SC_DC_FERROR;
783 }
784
785 /*
786  * get_idle_time - return how long the link has been idle.
787  */
788 int
789 get_idle_time(u, ip)
790     int u;
791     struct ppp_idle *ip;
792 {
793     return ioctl(ppp_fd, PPPIOCGIDLE, ip) >= 0;
794 }
795
796
797 /*
798  * sifvjcomp - config tcp header compression
799  */
800 int
801 sifvjcomp(u, vjcomp, cidcomp, maxcid)
802     int u, vjcomp, cidcomp, maxcid;
803 {
804     u_int x;
805
806     if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
807         error("ioctl (PPPIOCGFLAGS): %m");
808         return 0;
809     }
810     x = vjcomp ? x | SC_COMP_TCP: x &~ SC_COMP_TCP;
811     x = cidcomp? x & ~SC_NO_TCP_CCID: x | SC_NO_TCP_CCID;
812     if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
813         error("ioctl(PPPIOCSFLAGS): %m");
814         return 0;
815     }
816     if (ioctl(ppp_fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0) {
817         error("ioctl(PPPIOCSFLAGS): %m");
818         return 0;
819     }
820     return 1;
821 }
822
823 /*
824  * sifup - Config the interface up and enable IP packets to pass.
825  */
826 int
827 sifup(u)
828     int u;
829 {
830     struct ifreq ifr;
831
832     strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
833     if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
834         error("ioctl (SIOCGIFFLAGS): %m");
835         return 0;
836     }
837     ifr.ifr_flags |= IFF_UP;
838     if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
839         error("ioctl(SIOCSIFFLAGS): %m");
840         return 0;
841     }
842     if_is_up = 1;
843     return 1;
844 }
845
846 /*
847  * sifnpmode - Set the mode for handling packets for a given NP.
848  */
849 int
850 sifnpmode(u, proto, mode)
851     int u;
852     int proto;
853     enum NPmode mode;
854 {
855     struct npioctl npi;
856
857     npi.protocol = proto;
858     npi.mode = mode;
859     if (ioctl(ppp_fd, PPPIOCSNPMODE, &npi) < 0) {
860         error("ioctl(set NP %d mode to %d): %m", proto, mode);
861         return 0;
862     }
863     return 1;
864 }
865
866 /*
867  * sifdown - Config the interface down and disable IP.
868  */
869 int
870 sifdown(u)
871     int u;
872 {
873     struct ifreq ifr;
874     int rv;
875     struct npioctl npi;
876
877     rv = 1;
878     npi.protocol = PPP_IP;
879     npi.mode = NPMODE_ERROR;
880     ioctl(ppp_fd, PPPIOCSNPMODE, (caddr_t) &npi);
881     /* ignore errors, because ppp_fd might have been closed by now. */
882
883     strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
884     if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
885         error("ioctl (SIOCGIFFLAGS): %m");
886         rv = 0;
887     } else {
888         ifr.ifr_flags &= ~IFF_UP;
889         if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
890             error("ioctl(SIOCSIFFLAGS): %m");
891             rv = 0;
892         } else
893             if_is_up = 0;
894     }
895     return rv;
896 }
897
898 /*
899  * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
900  * if it exists.
901  */
902 #define SET_SA_FAMILY(addr, family)             \
903     BZERO((char *) &(addr), sizeof(addr));      \
904     addr.sa_family = (family); 
905
906 /*
907  * sifaddr - Config the interface IP addresses and netmask.
908  */
909 int
910 sifaddr(u, o, h, m)
911     int u;
912     u_int32_t o, h, m;
913 {
914     int ret;
915     struct ifreq ifr;
916
917     ret = 1;
918     strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
919     SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
920     if (m != 0) {
921         ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = m;
922         info("Setting interface mask to %s\n", ip_ntoa(m));
923         if (ioctl(sockfd, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
924             error("ioctl(SIOCSIFNETMASK): %m");
925             ret = 0;
926         }
927     }
928     ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
929     if (ioctl(sockfd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
930         error("ioctl(SIOCSIFADDR): %m");
931         ret = 0;
932     }
933     ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = h;
934     if (ioctl(sockfd, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
935         error("ioctl(SIOCSIFDSTADDR): %m");
936         ret = 0;
937     }
938     ifaddrs[0] = o;
939     ifaddrs[1] = h;
940     return ret;
941 }
942
943 /*
944  * cifaddr - Clear the interface IP addresses, and delete routes
945  * through the interface if possible.
946  */
947 int
948 cifaddr(u, o, h)
949     int u;
950     u_int32_t o, h;
951 {
952     struct rtentry rt;
953
954     ifaddrs[0] = 0;
955     BZERO(&rt, sizeof(rt));
956     SET_SA_FAMILY(rt.rt_dst, AF_INET);
957     ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = h;
958     SET_SA_FAMILY(rt.rt_gateway, AF_INET);
959     ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = o;
960     rt.rt_flags = RTF_HOST;
961     if (ioctl(sockfd, SIOCDELRT, (caddr_t) &rt) < 0) {
962         error("ioctl(SIOCDELRT): %m");
963         return 0;
964     }
965     return 1;
966 }
967
968 /*
969  * sifdefaultroute - assign a default route through the address given.
970  */
971 int
972 sifdefaultroute(u, l, g)
973     int u;
974     u_int32_t l, g;
975 {
976     struct rtentry rt;
977
978     BZERO(&rt, sizeof(rt));
979     SET_SA_FAMILY(rt.rt_dst, AF_INET);
980     SET_SA_FAMILY(rt.rt_gateway, AF_INET);
981     ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
982     rt.rt_flags = RTF_GATEWAY;
983     if (ioctl(sockfd, SIOCADDRT, &rt) < 0) {
984         error("default route ioctl(SIOCADDRT): %m");
985         return 0;
986     }
987     default_route_gateway = g;
988     return 1;
989 }
990
991 /*
992  * cifdefaultroute - delete a default route through the address given.
993  */
994 int
995 cifdefaultroute(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, SIOCDELRT, &rt) < 0)
1007         warn("default route ioctl(SIOCDELRT): %m");
1008     default_route_gateway = 0;
1009     return 1;
1010 }
1011
1012 /*
1013  * sifproxyarp - Make a proxy ARP entry for the peer.
1014  */
1015 int
1016 sifproxyarp(unit, hisaddr)
1017     int unit;
1018     u_int32_t hisaddr;
1019 {
1020     struct arpreq arpreq;
1021
1022     BZERO(&arpreq, sizeof(arpreq));
1023
1024     /*
1025      * Get the hardware address of an interface on the same subnet
1026      * as our local address.
1027      */
1028     if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) {
1029         error("Cannot determine ethernet address for proxy ARP");
1030         return 0;
1031     }
1032
1033     SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1034     ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1035     arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1036     if (ioctl(sockfd, SIOCSARP, (caddr_t)&arpreq) < 0) {
1037         error("Couldn't add proxy arp entry: %m");
1038         return 0;
1039     }
1040
1041     proxy_arp_addr = hisaddr;
1042     return 1;
1043 }
1044
1045 /*
1046  * cifproxyarp - Delete the proxy ARP entry for the peer.
1047  */
1048 int
1049 cifproxyarp(unit, hisaddr)
1050     int unit;
1051     u_int32_t hisaddr;
1052 {
1053     struct arpreq arpreq;
1054
1055     BZERO(&arpreq, sizeof(arpreq));
1056     SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1057     ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1058     if (ioctl(sockfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
1059         warn("Couldn't delete proxy arp entry: %m");
1060         return 0;
1061     }
1062     proxy_arp_addr = 0;
1063     return 1;
1064 }
1065
1066 /*
1067  * get_ether_addr - get the hardware address of an interface on the
1068  * the same subnet as ipaddr.
1069  */
1070 #define MAX_IFS         32
1071
1072 static int
1073 get_ether_addr(ipaddr, hwaddr)
1074     u_int32_t ipaddr;
1075     struct sockaddr *hwaddr;
1076 {
1077     struct ifreq *ifr, *ifend;
1078     u_int32_t ina, mask;
1079     struct ifreq ifreq;
1080     struct ifconf ifc;
1081     struct ifreq ifs[MAX_IFS];
1082     struct ifdevea ifdevea;
1083
1084     ifc.ifc_len = sizeof(ifs);
1085     ifc.ifc_req = ifs;
1086     if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1087         error("ioctl(SIOCGIFCONF): %m");
1088         return 0;
1089     }
1090
1091     /*
1092      * Scan through looking for an interface with an Internet
1093      * address on the same subnet as `ipaddr'.
1094      */
1095     ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1096     for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1097             ((char *)&ifr->ifr_addr + sizeof(struct sockaddr))) {
1098         if (ifr->ifr_addr.sa_family == AF_INET) {
1099             ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1100             strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1101             /*
1102              * Check that the interface is up, and not point-to-point
1103              * or loopback.
1104              */
1105             if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1106                 continue;
1107             if ((ifreq.ifr_flags &
1108                  (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
1109                  != (IFF_UP|IFF_BROADCAST))
1110                 continue;
1111             /*
1112              * Get its netmask and check that it's on the right subnet.
1113              */
1114             if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1115                 continue;
1116             mask = ((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr;
1117             if ((ipaddr & mask) != (ina & mask))
1118                 continue;
1119
1120             break;
1121         }
1122     }
1123
1124     if (ifr >= ifend)
1125         return 0;
1126     info("found interface %s for proxy arp", ifr->ifr_name);
1127
1128     /*
1129      * Grab the physical address for this interface.
1130      */
1131     strlcpy(ifdevea.ifr_name, ifr->ifr_name, sizeof(ifdevea.ifr_name));
1132     if (ioctl(sockfd, SIOCRPHYSADDR, &ifdevea) < 0) {
1133         error("Couldn't get h/w address for %s: %m", ifr->ifr_name);
1134         return 0;
1135     }
1136
1137     hwaddr->sa_family = AF_UNSPEC;
1138     BCOPY(ifdevea.current_pa, hwaddr->sa_data, 6);
1139     return 1;
1140 }
1141
1142 /*
1143  * Return user specified netmask, modified by any mask we might determine
1144  * for address `addr' (in network byte order).
1145  * Here we scan through the system's list of interfaces, looking for
1146  * any non-point-to-point interfaces which might appear to be on the same
1147  * network as `addr'.  If we find any, we OR in their netmask to the
1148  * user-specified netmask.
1149  */
1150 u_int32_t
1151 GetMask(addr)
1152     u_int32_t addr;
1153 {
1154     u_int32_t mask, nmask, ina;
1155     struct ifreq *ifr, *ifend, ifreq;
1156     struct ifconf ifc;
1157     struct ifreq ifs[MAX_IFS];
1158
1159     addr = ntohl(addr);
1160     if (IN_CLASSA(addr))        /* determine network mask for address class */
1161         nmask = IN_CLASSA_NET;
1162     else if (IN_CLASSB(addr))
1163         nmask = IN_CLASSB_NET;
1164     else
1165         nmask = IN_CLASSC_NET;
1166     /* class D nets are disallowed by bad_ip_adrs */
1167     mask = netmask | htonl(nmask);
1168
1169     /*
1170      * Scan through the system's network interfaces.
1171      */
1172     ifc.ifc_len = sizeof(ifs);
1173     ifc.ifc_req = ifs;
1174     if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1175         warn("ioctl(SIOCGIFCONF): %m");
1176         return mask;
1177     }
1178     ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1179     for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1180                 ((char *)&ifr->ifr_addr + sizeof(struct sockaddr))) {
1181         /*
1182          * Check the interface's internet address.
1183          */
1184         if (ifr->ifr_addr.sa_family != AF_INET)
1185             continue;
1186         ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1187         if ((ntohl(ina) & nmask) != (addr & nmask))
1188             continue;
1189         /*
1190          * Check that the interface is up, and not point-to-point or loopback.
1191          */
1192         strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1193         if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1194             continue;
1195         if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
1196             != IFF_UP)
1197             continue;
1198         /*
1199          * Get its netmask and OR it into our mask.
1200          */
1201         if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1202             continue;
1203         mask |= ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr;
1204     }
1205
1206     return mask;
1207 }
1208
1209 /*
1210  * have_route_to - determine if the system has any route to
1211  * a given IP address.
1212  * For demand mode to work properly, we have to ignore routes
1213  * through our own interface.
1214  */
1215 int have_route_to(u_int32_t addr)
1216 {
1217     return -1;
1218 }
1219
1220
1221 /*
1222  * Use the hostid as part of the random number seed.
1223  */
1224 int
1225 get_host_seed()
1226 {
1227     return gethostid();
1228 }
1229
1230
1231 /*
1232   Seems like strdup() is not part of string package in Ultrix.
1233   If I understood the man-page on the sun this should work.
1234
1235   Robert Olsson
1236 */
1237
1238 char *strdup( in ) char *in;
1239 {
1240   char* dup;
1241   if(! (dup = (char *) malloc( strlen( in ) +1 ))) return NULL;
1242   (void) strcpy( dup, in );
1243   return dup;
1244 }
1245
1246 /*
1247  * This logwtmp() implementation is subject to the following copyright:
1248  *
1249  * Copyright (c) 1988 The Regents of the University of California.
1250  * All rights reserved.
1251  *
1252  * Redistribution and use in source and binary forms are permitted
1253  * provided that the above copyright notice and this paragraph are
1254  * duplicated in all such forms and that any documentation,
1255  * advertising materials, and other materials related to such
1256  * distribution and use acknowledge that the software was developed
1257  * by the University of California, Berkeley.  The name of the
1258  * University may not be used to endorse or promote products derived
1259  * from this software without specific prior written permission.
1260  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1261  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1262  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1263  */
1264
1265 #define WTMPFILE        "/usr/adm/wtmp"
1266
1267 void
1268 logwtmp(line, name, host)
1269     const char *line, *name, *host;
1270 {
1271     int fd;
1272     struct stat buf;
1273     struct utmp ut;
1274
1275     if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0)
1276         return;
1277     if (!fstat(fd, &buf)) {
1278         strlcpy(ut.ut_line, line, sizeof(ut.ut_line));
1279         strlcpy(ut.ut_name, name, sizeof(ut.ut_name));
1280         strlcpy(ut.ut_host, host, sizeof(ut.ut_host));
1281         (void)time(&ut.ut_time);
1282         if (write(fd, (char *)&ut, sizeof(struct utmp)) != sizeof(struct utmp))
1283             (void)ftruncate(fd, buf.st_size);
1284     }
1285     close(fd);
1286 }
1287
1288 /*
1289  * Routines for locking and unlocking the serial device, moved here
1290  * from chat.c.
1291  */
1292
1293 #define LOCK_PREFIX     "/usr/spool/uucp/LCK.."
1294
1295 /*
1296  * lock - create a lock file for the named device
1297  */
1298 int
1299 lock(dev)
1300     char *dev;
1301 {
1302     int fd, pid, n;
1303     char *p;
1304     size_t l;
1305
1306     if ((p = strrchr(dev, '/')) != NULL)
1307         dev = p + 1;
1308     l = strlen(LOCK_PREFIX) + strlen(dev) + 1;
1309     lock_file = malloc(l);
1310     if (lock_file == NULL)
1311         novm("lock file name");
1312     slprintf(lock_file, l, "%s%s, LOCK_PREFIX, dev);
1313
1314     while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
1315         if (errno == EEXIST
1316             && (fd = open(lock_file, O_RDONLY, 0)) >= 0) {
1317             /* Read the lock file to find out who has the device locked */
1318             n = read(fd, &pid, sizeof(pid));
1319             if (n <= 0) {
1320                 error("Can't read pid from lock file %s", lock_file);
1321                 close(fd);
1322             } else {
1323                 if (kill(pid, 0) == -1 && errno == ESRCH) {
1324                     /* pid no longer exists - remove the lock file */
1325                     if (unlink(lock_file) == 0) {
1326                         close(fd);
1327                         notice("Removed stale lock on %s (pid %d)",
1328                                dev, pid);
1329                         continue;
1330                     } else
1331                         warn("Couldn't remove stale lock on %s",
1332                                dev);
1333                 } else
1334                     notice("Device %s is locked by pid %d",
1335                            dev, pid);
1336             }
1337             close(fd);
1338         } else
1339             error("Can't create lock file %s: %m", lock_file);
1340         free(lock_file);
1341         lock_file = NULL;
1342         return -1;
1343     }
1344
1345     pid = getpid();
1346     write(fd, &pid, sizeof pid);
1347
1348     close(fd);
1349     return 0;
1350 }
1351
1352 /*
1353  * unlock - remove our lockfile
1354  */
1355 void
1356 unlock()
1357 {
1358     if (lock_file) {
1359         unlink(lock_file);
1360         free(lock_file);
1361         lock_file = NULL;
1362     }
1363 }