Fix the rechallenge behaviour. Previously, once it sent a rechallenge,
[mpls-ppp.git] / pppd / sys-ultrix.c
blob69458943ff0ac6b05957cbbde565298dbf86d508
1 /*
2 * sys-ultrix.c - System-dependent procedures for setting up
3 * PPP interfaces on Ultrix systems.
5 * Copyright (c) 1995-2002 Paul Mackerras. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. The name(s) of the authors of this software must not be used to
15 * endorse or promote products derived from this software without
16 * prior written permission.
18 * 3. Redistributions of any form whatsoever must retain the following
19 * acknowledgment:
20 * "This product includes software developed by Paul Mackerras
21 * <paulus@samba.org>".
23 * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
24 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
25 * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
26 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
27 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
28 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
29 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
31 * Derived from main.c and pppd.h, which are:
33 * Copyright (c) 1984-2000 Carnegie Mellon University. All rights reserved.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in
44 * the documentation and/or other materials provided with the
45 * distribution.
47 * 3. The name "Carnegie Mellon University" must not be used to
48 * endorse or promote products derived from this software without
49 * prior written permission. For permission or any legal
50 * details, please contact
51 * Office of Technology Transfer
52 * Carnegie Mellon University
53 * 5000 Forbes Avenue
54 * Pittsburgh, PA 15213-3890
55 * (412) 268-4387, fax: (412) 268-7395
56 * tech-transfer@andrew.cmu.edu
58 * 4. Redistributions of any form whatsoever must retain the following
59 * acknowledgment:
60 * "This product includes software developed by Computing Services
61 * at Carnegie Mellon University (http://www.cmu.edu/computing/)."
63 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
64 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
65 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
66 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
67 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
68 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
69 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
72 #define RCSID "$Id: sys-ultrix.c,v 1.35 2004/11/04 10:02:26 paulus Exp $"
75 * TODO:
78 #include <stdio.h>
79 #include <stdlib.h>
80 #include <string.h>
81 #include <stdlib.h>
82 #include <errno.h>
83 #include <fcntl.h>
84 #include <termios.h>
85 #include <utmp.h>
86 #include <sys/types.h>
87 #include <sys/file.h>
88 #include <sys/socket.h>
89 #include <sys/ioctl.h>
90 #include <sys/time.h>
91 #include <sys/errno.h>
92 #include <sys/stat.h>
94 #include <net/if.h>
95 #include <net/ppp_defs.h>
96 #include <net/if_ppp.h>
97 #include <net/route.h>
98 #include <netinet/in.h>
100 #include "pppd.h"
102 static const char rcsid[] = RCSID;
104 static int initdisc = -1; /* Initial TTY discipline for ppp_fd */
105 static int initfdflags = -1; /* Initial file descriptor flags for ppp_fd */
106 static int ppp_fd = -1; /* fd which is set to PPP discipline */
108 static int restore_term; /* 1 => we've munged the terminal */
109 static struct termios inittermios; /* Initial TTY termios */
110 static struct winsize wsinfo; /* Initial window size info */
112 static char *lock_file; /* name of lock file created */
114 static int loop_slave = -1;
115 static int loop_master;
116 static char loop_name[20];
118 static unsigned char inbuf[512]; /* buffer for chars read from loopback */
120 static int sockfd; /* socket for doing interface ioctls */
122 static fd_set in_fds; /* set of fds that wait_input waits for */
123 static int max_in_fd; /* highest fd set in in_fds */
125 static int if_is_up; /* the interface is currently up */
126 static u_int32_t ifaddrs[2]; /* local and remote addresses */
127 static u_int32_t default_route_gateway; /* gateway addr for default route */
128 static u_int32_t proxy_arp_addr; /* remote addr for proxy arp */
130 /* Prototypes for procedures local to this file. */
131 static int translate_speed __P((int));
132 static int baud_rate_of __P((int));
133 static int get_ether_addr __P((u_int32_t, struct sockaddr *));
137 * sys_init - System-dependent initialization.
139 void
140 sys_init()
142 /* Get an internet socket for doing socket ioctl's on. */
143 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
144 fatal("Couldn't create IP socket: %m");
146 FD_ZERO(&in_fds);
147 max_in_fd = 0;
151 * sys_cleanup - restore any system state we modified before exiting:
152 * mark the interface down, delete default route and/or proxy arp entry.
153 * This shouldn't call die() because it's called from die().
155 void
156 sys_cleanup()
158 struct ifreq ifr;
160 if (if_is_up) {
161 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
162 if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) >= 0
163 && ((ifr.ifr_flags & IFF_UP) != 0)) {
164 ifr.ifr_flags &= ~IFF_UP;
165 ioctl(sockfd, SIOCSIFFLAGS, &ifr);
168 if (ifaddrs[0])
169 cifaddr(0, ifaddrs[0], ifaddrs[1]);
170 if (default_route_gateway)
171 cifdefaultroute(0, 0, default_route_gateway);
172 if (proxy_arp_addr)
173 cifproxyarp(0, proxy_arp_addr);
177 * sys_close - Clean up in a child process before execing.
179 void
180 sys_close()
182 close(sockfd);
183 if (loop_slave >= 0) {
184 close(loop_slave);
185 close(loop_master);
190 * sys_check_options - check the options that the user specified
193 sys_check_options()
195 if (demand) {
196 option_error("Sorry - demand-dialling is not supported under Ultrix\n");
197 return 0;
199 return 1;
202 #if 0
204 * daemon - Detach us from the terminal session.
207 daemon(nochdir, noclose)
208 int nochdir, noclose;
210 int pid;
212 if ((pid = fork()) < 0)
213 return -1;
214 if (pid != 0)
215 exit(0); /* parent dies */
216 setsid();
217 if (!nochdir)
218 chdir("/");
219 if (!noclose) {
220 fclose(stdin); /* don't need stdin, stdout, stderr */
221 fclose(stdout);
222 fclose(stderr);
224 return 0;
226 #endif
229 * ppp_available - check whether the system has any ppp interfaces
230 * (in fact we check whether we can do an ioctl on ppp0).
233 ppp_available()
235 int s, ok;
236 struct ifreq ifr;
237 extern char *no_ppp_msg;
239 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
240 return 1; /* can't tell */
242 strlcpy(ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
243 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
244 close(s);
246 no_ppp_msg = "\
247 This system lacks kernel support for PPP. To include PPP support\n\
248 in the kernel, please follow the steps detailed in the README.ultrix\n\
249 file in the ppp-2.2 distribution.\n";
250 return ok;
254 * establish_ppp - Turn the serial port into a ppp interface.
257 establish_ppp(fd)
258 int fd;
260 int pppdisc = PPPDISC;
261 int x;
264 * Save the old line discipline of fd, and set it to PPP.
266 if (ioctl(fd, TIOCGETD, &initdisc) < 0)
267 fatal("ioctl(TIOCGETD): %m");
268 if (ioctl(fd, TIOCSETD, &pppdisc) < 0)
269 fatal("ioctl(TIOCSETD): %m");
272 * Find out which interface we were given.
274 if (ioctl(fd, PPPIOCGUNIT, &ifunit) < 0) {
275 fatal("ioctl(PPPIOCGUNIT): %m");
277 ppp_fd = fd;
280 * Enable debug in the driver if requested.
282 if (kdebugflag) {
283 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
284 warn("ioctl (PPPIOCGFLAGS): %m");
285 } else {
286 x |= (kdebugflag & 0xFF) * SC_DEBUG;
287 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
288 warn("ioctl(PPPIOCSFLAGS): %m");
293 * Set device for non-blocking reads.
295 if ((initfdflags = fcntl(fd, F_GETFL)) == -1
296 || fcntl(fd, F_SETFL, initfdflags | O_NONBLOCK) == -1) {
297 warn("Couldn't set device to non-blocking mode: %m");
300 return fd;
304 * restore_loop - reattach the ppp unit to the loopback.
306 void
307 restore_loop()
312 * disestablish_ppp - Restore the serial port to normal operation.
313 * This shouldn't call die() because it's called from die().
315 void
316 disestablish_ppp(fd)
317 int fd;
319 /* Reset non-blocking mode on fd. */
320 if (initfdflags != -1 && fcntl(fd, F_SETFL, initfdflags) < 0)
321 warn("Couldn't restore device fd flags: %m");
322 initfdflags = -1;
324 /* Restore old line discipline. */
325 if (initdisc >= 0 && ioctl(fd, TIOCSETD, &initdisc) < 0)
326 error("ioctl(TIOCSETD): %m");
327 initdisc = -1;
329 if (fd == ppp_fd)
330 ppp_fd = -1;
334 * Check whether the link seems not to be 8-bit clean.
336 void
337 clean_check()
339 int x;
340 char *s;
342 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) == 0) {
343 s = NULL;
344 switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP)) {
345 case SC_RCV_B7_0:
346 s = "bit 7 set to 1";
347 break;
348 case SC_RCV_B7_1:
349 s = "bit 7 set to 0";
350 break;
351 case SC_RCV_EVNP:
352 s = "odd parity";
353 break;
354 case SC_RCV_ODDP:
355 s = "even parity";
356 break;
358 if (s != NULL) {
359 warn("Serial link is not 8-bit clean:");
360 warn("All received characters had %s", s);
367 * List of valid speeds.
369 struct speed {
370 int speed_int, speed_val;
371 } speeds[] = {
372 #ifdef B50
373 { 50, B50 },
374 #endif
375 #ifdef B75
376 { 75, B75 },
377 #endif
378 #ifdef B110
379 { 110, B110 },
380 #endif
381 #ifdef B134
382 { 134, B134 },
383 #endif
384 #ifdef B150
385 { 150, B150 },
386 #endif
387 #ifdef B200
388 { 200, B200 },
389 #endif
390 #ifdef B300
391 { 300, B300 },
392 #endif
393 #ifdef B600
394 { 600, B600 },
395 #endif
396 #ifdef B1200
397 { 1200, B1200 },
398 #endif
399 #ifdef B1800
400 { 1800, B1800 },
401 #endif
402 #ifdef B2000
403 { 2000, B2000 },
404 #endif
405 #ifdef B2400
406 { 2400, B2400 },
407 #endif
408 #ifdef B3600
409 { 3600, B3600 },
410 #endif
411 #ifdef B4800
412 { 4800, B4800 },
413 #endif
414 #ifdef B7200
415 { 7200, B7200 },
416 #endif
417 #ifdef B9600
418 { 9600, B9600 },
419 #endif
420 #ifdef B19200
421 { 19200, B19200 },
422 #endif
423 #ifdef B38400
424 { 38400, B38400 },
425 #endif
426 #ifdef EXTA
427 { 19200, EXTA },
428 #endif
429 #ifdef EXTB
430 { 38400, EXTB },
431 #endif
432 #ifdef B57600
433 { 57600, B57600 },
434 #endif
435 #ifdef B115200
436 { 115200, B115200 },
437 #endif
438 { 0, 0 }
442 * Translate from bits/second to a speed_t.
445 translate_speed(bps)
446 int bps;
448 struct speed *speedp;
450 if (bps == 0)
451 return 0;
452 for (speedp = speeds; speedp->speed_int; speedp++)
453 if (bps == speedp->speed_int)
454 return speedp->speed_val;
455 warn("speed %d not supported", bps);
456 return 0;
460 * Translate from a speed_t to bits/second.
463 baud_rate_of(speed)
464 int speed;
466 struct speed *speedp;
468 if (speed == 0)
469 return 0;
470 for (speedp = speeds; speedp->speed_int; speedp++)
471 if (speed == speedp->speed_val)
472 return speedp->speed_int;
473 return 0;
477 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
478 * at the requested speed, etc. If `local' is true, set CLOCAL
479 * regardless of whether the modem option was specified.
481 void
482 set_up_tty(fd, local)
483 int fd, local;
485 int speed, x;
486 struct termios tios;
488 if (tcgetattr(fd, &tios) < 0)
489 fatal("tcgetattr: %m");
491 if (!restore_term) {
492 inittermios = tios;
493 ioctl(fd, TIOCGWINSZ, &wsinfo);
496 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
497 #ifdef CRTSCTS
498 if (crtscts > 0 && !local)
499 tios.c_cflag |= CRTSCTS;
500 else if (crtscts < 0)
501 tios.c_cflag &= ~CRTSCTS;
502 #endif /* CRTSCTS */
504 tios.c_cflag |= CS8 | CREAD | HUPCL;
505 if (local || !modem)
506 tios.c_cflag |= CLOCAL;
507 tios.c_iflag = IGNBRK | IGNPAR;
508 tios.c_oflag = 0;
509 tios.c_lflag = 0;
510 tios.c_cc[VMIN] = 1;
511 tios.c_cc[VTIME] = 0;
513 if (crtscts == -2) {
514 tios.c_iflag |= IXON | IXOFF;
515 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
516 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
519 speed = translate_speed(inspeed);
520 if (speed) {
521 cfsetospeed(&tios, speed);
522 cfsetispeed(&tios, speed);
523 } else {
524 speed = cfgetospeed(&tios);
526 * We can't proceed if the serial port speed is B0,
527 * since that implies that the serial port is disabled.
529 if (speed == B0)
530 fatal("Baud rate for %s is 0; need explicit baud rate", devnam);
533 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0)
534 fatal("tcsetattr: %m");
536 x = 0;
537 if (ioctl(fd, (crtscts > 0 || modem)? TIOCMODEM: TIOCNMODEM, &x) < 0)
538 warn("TIOC(N)MODEM: %m");
539 if (ioctl(fd, (local || !modem)? TIOCNCAR: TIOCCAR) < 0)
540 warn("TIOC(N)CAR: %m");
542 baud_rate = inspeed = baud_rate_of(speed);
543 restore_term = 1;
547 * restore_tty - restore the terminal to the saved settings.
549 void
550 restore_tty(fd)
551 int fd;
553 if (restore_term) {
554 if (!default_device) {
556 * Turn off echoing, because otherwise we can get into
557 * a loop with the tty and the modem echoing to each other.
558 * We presume we are the sole user of this tty device, so
559 * when we close it, it will revert to its defaults anyway.
561 inittermios.c_lflag &= ~(ECHO | ECHONL);
563 if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
564 if (errno != ENXIO)
565 warn("tcsetattr: %m");
566 ioctl(fd, TIOCSWINSZ, &wsinfo);
567 restore_term = 0;
572 * setdtr - control the DTR line on the serial port.
573 * This is called from die(), so it shouldn't call die().
575 void
576 setdtr(fd, on)
577 int fd, on;
579 int modembits = TIOCM_DTR;
581 ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
586 * open_ppp_loopback - open the device we use for getting
587 * packets in demand mode, and connect it to a ppp interface.
588 * Here we use would use a pty, except that Ultrix ptys are brain-dead.
591 open_ppp_loopback()
593 fatal("open_ppp_loopback called!");
594 /* return loop_master; */
599 * output - Output PPP packet.
601 void
602 output(unit, p, len)
603 int unit;
604 u_char *p;
605 int len;
607 if (debug)
608 dbglog("sent %P", p, len);
610 if (write(ttyfd, p, len) < 0) {
611 if (errno != EIO)
612 error("write: %m");
618 * wait_input - wait until there is data available,
619 * for the length of time specified by *timo (indefinite
620 * if timo is NULL).
622 void
623 wait_input(timo)
624 struct timeval *timo;
626 fd_set ready;
627 int n;
629 ready = in_fds;
630 n = select(ttyfd+1, &ready, NULL, &ready, timo);
631 if (n < 0 && errno != EINTR)
632 fatal("select: %m");
637 * add_fd - add an fd to the set that wait_input waits for.
639 void add_fd(fd)
640 int fd;
642 FD_SET(fd, &in_fds);
643 if (fd > max_in_fd)
644 max_in_fd = fd;
648 * remove_fd - remove an fd from the set that wait_input waits for.
650 void remove_fd(fd)
651 int fd;
653 FD_CLR(fd, &in_fds);
656 #if 0
658 * wait_loop_output - wait until there is data available on the
659 * loopback, for the length of time specified by *timo (indefinite
660 * if timo is NULL).
662 void
663 wait_loop_output(timo)
664 struct timeval *timo;
666 wait_time(timo);
671 * wait_time - wait for a given length of time or until a
672 * signal is received.
674 void
675 wait_time(timo)
676 struct timeval *timo;
678 int n;
680 n = select(0, NULL, NULL, NULL, timo);
681 if (n < 0 && errno != EINTR)
682 fatal("select: %m");
684 #endif
687 * read_packet - get a PPP packet from the serial device.
690 read_packet(buf)
691 u_char *buf;
693 int len;
695 if ((len = read(ttyfd, buf, PPP_MTU + PPP_HDRLEN)) < 0) {
696 if (errno == EWOULDBLOCK || errno == EINTR)
697 return -1;
698 fatal("read(fd): %m");
700 return len;
705 * get_loop_output - read characters from the loopback, form them
706 * into frames, and detect when we want to bring the real link up.
707 * Return value is 1 if we need to bring up the link, 0 otherwise.
710 get_loop_output()
712 return 0;
717 * ppp_send_config - configure the transmit characteristics of
718 * the ppp interface.
720 void
721 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
722 int unit, mtu;
723 u_int32_t asyncmap;
724 int pcomp, accomp;
726 u_int x;
728 if (ioctl(ppp_fd, PPPIOCSMTU, &mtu) < 0)
729 fatal("ioctl(PPPIOCSMTU): %m");
731 if (ioctl(ppp_fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0)
732 fatal("ioctl(PPPIOCSASYNCMAP): %m");
734 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0)
735 fatal("ioctl (PPPIOCGFLAGS): %m");
736 x = pcomp? x | SC_COMP_PROT: x &~ SC_COMP_PROT;
737 x = accomp? x | SC_COMP_AC: x &~ SC_COMP_AC;
738 if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
739 fatal("ioctl(PPPIOCSFLAGS): %m");
744 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
746 void
747 ppp_set_xaccm(unit, accm)
748 int unit;
749 ext_accm accm;
751 if (ioctl(ppp_fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY)
752 warn("ioctl(set extended ACCM): %m");
757 * ppp_recv_config - configure the receive-side characteristics of
758 * the ppp interface.
760 void
761 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
762 int unit, mru;
763 u_int32_t asyncmap;
764 int pcomp, accomp;
766 int x;
768 if (ioctl(ppp_fd, PPPIOCSMRU, (caddr_t) &mru) < 0)
769 fatal("ioctl(PPPIOCSMRU): %m");
770 if (ioctl(ppp_fd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0)
771 fatal("ioctl(PPPIOCSRASYNCMAP): %m");
772 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0)
773 fatal("ioctl (PPPIOCGFLAGS): %m");
774 x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC;
775 if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
776 fatal("ioctl(PPPIOCSFLAGS): %m");
780 * ccp_test - ask kernel whether a given compression method
781 * is acceptable for use. Returns 1 if the method and parameters
782 * are OK, 0 if the method is known but the parameters are not OK
783 * (e.g. code size should be reduced), or -1 if the method is unknown.
786 ccp_test(unit, opt_ptr, opt_len, for_transmit)
787 int unit, opt_len, for_transmit;
788 u_char *opt_ptr;
790 struct ppp_option_data data;
792 data.ptr = opt_ptr;
793 data.length = opt_len;
794 data.transmit = for_transmit;
795 if (ioctl(ttyfd, PPPIOCSCOMPRESS, (caddr_t) &data) >= 0)
796 return 1;
797 return (errno == ENOBUFS)? 0: -1;
801 * ccp_flags_set - inform kernel about the current state of CCP.
803 void
804 ccp_flags_set(unit, isopen, isup)
805 int unit, isopen, isup;
807 int x;
809 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
810 error("ioctl (PPPIOCGFLAGS): %m");
811 return;
813 x = isopen? x | SC_CCP_OPEN: x &~ SC_CCP_OPEN;
814 x = isup? x | SC_CCP_UP: x &~ SC_CCP_UP;
815 if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
816 error("ioctl(PPPIOCSFLAGS): %m");
820 * ccp_fatal_error - returns 1 if decompression was disabled as a
821 * result of an error detected after decompression of a packet,
822 * 0 otherwise. This is necessary because of patent nonsense.
825 ccp_fatal_error(unit)
826 int unit;
828 int x;
830 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
831 error("ioctl(PPPIOCGFLAGS): %m");
832 return 0;
834 return x & SC_DC_FERROR;
838 * get_idle_time - return how long the link has been idle.
841 get_idle_time(u, ip)
842 int u;
843 struct ppp_idle *ip;
845 return ioctl(ppp_fd, PPPIOCGIDLE, ip) >= 0;
849 * get_ppp_stats - return statistics for the link.
852 get_ppp_stats(u, stats)
853 int u;
854 struct pppd_stats *stats;
856 struct ifpppstatsreq req;
858 memset (&req, 0, sizeof (req));
859 strlcpy(req.ifr_name, interface, sizeof(req.ifr_name));
860 if (ioctl(sockfd, SIOCGPPPSTATS, &req) < 0) {
861 error("Couldn't get PPP statistics: %m");
862 return 0;
864 stats->bytes_in = req.stats.p.ppp_ibytes;
865 stats->bytes_out = req.stats.p.ppp_obytes;
866 return 1;
871 * sifvjcomp - config tcp header compression
874 sifvjcomp(u, vjcomp, cidcomp, maxcid)
875 int u, vjcomp, cidcomp, maxcid;
877 u_int x;
879 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
880 error("ioctl (PPPIOCGFLAGS): %m");
881 return 0;
883 x = vjcomp ? x | SC_COMP_TCP: x &~ SC_COMP_TCP;
884 x = cidcomp? x & ~SC_NO_TCP_CCID: x | SC_NO_TCP_CCID;
885 if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
886 error("ioctl(PPPIOCSFLAGS): %m");
887 return 0;
889 if (ioctl(ppp_fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0) {
890 error("ioctl(PPPIOCSFLAGS): %m");
891 return 0;
893 return 1;
897 * sifup - Config the interface up and enable IP packets to pass.
900 sifup(u)
901 int u;
903 struct ifreq ifr;
905 strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
906 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
907 error("ioctl (SIOCGIFFLAGS): %m");
908 return 0;
910 ifr.ifr_flags |= IFF_UP;
911 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
912 error("ioctl(SIOCSIFFLAGS): %m");
913 return 0;
915 if_is_up = 1;
916 return 1;
920 * sifnpmode - Set the mode for handling packets for a given NP.
923 sifnpmode(u, proto, mode)
924 int u;
925 int proto;
926 enum NPmode mode;
928 struct npioctl npi;
930 npi.protocol = proto;
931 npi.mode = mode;
932 if (ioctl(ppp_fd, PPPIOCSNPMODE, &npi) < 0) {
933 error("ioctl(set NP %d mode to %d): %m", proto, mode);
934 return 0;
936 return 1;
940 * sifdown - Config the interface down and disable IP.
943 sifdown(u)
944 int u;
946 struct ifreq ifr;
947 int rv;
948 struct npioctl npi;
950 rv = 1;
951 npi.protocol = PPP_IP;
952 npi.mode = NPMODE_ERROR;
953 ioctl(ppp_fd, PPPIOCSNPMODE, (caddr_t) &npi);
954 /* ignore errors, because ppp_fd might have been closed by now. */
956 strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
957 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
958 error("ioctl (SIOCGIFFLAGS): %m");
959 rv = 0;
960 } else {
961 ifr.ifr_flags &= ~IFF_UP;
962 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
963 error("ioctl(SIOCSIFFLAGS): %m");
964 rv = 0;
965 } else
966 if_is_up = 0;
968 return rv;
972 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
973 * if it exists.
975 #define SET_SA_FAMILY(addr, family) \
976 BZERO((char *) &(addr), sizeof(addr)); \
977 addr.sa_family = (family);
980 * sifaddr - Config the interface IP addresses and netmask.
983 sifaddr(u, o, h, m)
984 int u;
985 u_int32_t o, h, m;
987 int ret;
988 struct ifreq ifr;
990 ret = 1;
991 strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
992 SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
993 if (m != 0) {
994 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = m;
995 info("Setting interface mask to %s\n", ip_ntoa(m));
996 if (ioctl(sockfd, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
997 error("ioctl(SIOCSIFNETMASK): %m");
998 ret = 0;
1001 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
1002 if (ioctl(sockfd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
1003 error("ioctl(SIOCSIFADDR): %m");
1004 ret = 0;
1006 ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = h;
1007 if (ioctl(sockfd, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
1008 error("ioctl(SIOCSIFDSTADDR): %m");
1009 ret = 0;
1011 ifaddrs[0] = o;
1012 ifaddrs[1] = h;
1013 return ret;
1017 * cifaddr - Clear the interface IP addresses, and delete routes
1018 * through the interface if possible.
1021 cifaddr(u, o, h)
1022 int u;
1023 u_int32_t o, h;
1025 struct rtentry rt;
1027 ifaddrs[0] = 0;
1028 BZERO(&rt, sizeof(rt));
1029 SET_SA_FAMILY(rt.rt_dst, AF_INET);
1030 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = h;
1031 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
1032 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = o;
1033 rt.rt_flags = RTF_HOST;
1034 if (ioctl(sockfd, SIOCDELRT, (caddr_t) &rt) < 0) {
1035 error("ioctl(SIOCDELRT): %m");
1036 return 0;
1038 return 1;
1042 * sifdefaultroute - assign a default route through the address given.
1045 sifdefaultroute(u, l, g)
1046 int u;
1047 u_int32_t l, g;
1049 struct rtentry rt;
1051 BZERO(&rt, sizeof(rt));
1052 SET_SA_FAMILY(rt.rt_dst, AF_INET);
1053 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
1054 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
1055 rt.rt_flags = RTF_GATEWAY;
1056 if (ioctl(sockfd, SIOCADDRT, &rt) < 0) {
1057 error("default route ioctl(SIOCADDRT): %m");
1058 return 0;
1060 default_route_gateway = g;
1061 return 1;
1065 * cifdefaultroute - delete a default route through the address given.
1068 cifdefaultroute(u, l, g)
1069 int u;
1070 u_int32_t l, g;
1072 struct rtentry rt;
1074 BZERO(&rt, sizeof(rt));
1075 SET_SA_FAMILY(rt.rt_dst, AF_INET);
1076 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
1077 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
1078 rt.rt_flags = RTF_GATEWAY;
1079 if (ioctl(sockfd, SIOCDELRT, &rt) < 0)
1080 warn("default route ioctl(SIOCDELRT): %m");
1081 default_route_gateway = 0;
1082 return 1;
1086 * sifproxyarp - Make a proxy ARP entry for the peer.
1089 sifproxyarp(unit, hisaddr)
1090 int unit;
1091 u_int32_t hisaddr;
1093 struct arpreq arpreq;
1095 BZERO(&arpreq, sizeof(arpreq));
1098 * Get the hardware address of an interface on the same subnet
1099 * as our local address.
1101 if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) {
1102 error("Cannot determine ethernet address for proxy ARP");
1103 return 0;
1106 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1107 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1108 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1109 if (ioctl(sockfd, SIOCSARP, (caddr_t)&arpreq) < 0) {
1110 error("Couldn't add proxy arp entry: %m");
1111 return 0;
1114 proxy_arp_addr = hisaddr;
1115 return 1;
1119 * cifproxyarp - Delete the proxy ARP entry for the peer.
1122 cifproxyarp(unit, hisaddr)
1123 int unit;
1124 u_int32_t hisaddr;
1126 struct arpreq arpreq;
1128 BZERO(&arpreq, sizeof(arpreq));
1129 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1130 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1131 if (ioctl(sockfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
1132 warn("Couldn't delete proxy arp entry: %m");
1133 return 0;
1135 proxy_arp_addr = 0;
1136 return 1;
1140 * get_ether_addr - get the hardware address of an interface on the
1141 * the same subnet as ipaddr.
1143 #define MAX_IFS 32
1145 static int
1146 get_ether_addr(ipaddr, hwaddr)
1147 u_int32_t ipaddr;
1148 struct sockaddr *hwaddr;
1150 struct ifreq *ifr, *ifend;
1151 u_int32_t ina, mask;
1152 struct ifreq ifreq;
1153 struct ifconf ifc;
1154 struct ifreq ifs[MAX_IFS];
1155 struct ifdevea ifdevea;
1157 ifc.ifc_len = sizeof(ifs);
1158 ifc.ifc_req = ifs;
1159 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1160 error("ioctl(SIOCGIFCONF): %m");
1161 return 0;
1165 * Scan through looking for an interface with an Internet
1166 * address on the same subnet as `ipaddr'.
1168 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1169 for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1170 ((char *)&ifr->ifr_addr + sizeof(struct sockaddr))) {
1171 if (ifr->ifr_addr.sa_family == AF_INET) {
1172 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1173 strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1175 * Check that the interface is up, and not point-to-point
1176 * or loopback.
1178 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1179 continue;
1180 if ((ifreq.ifr_flags &
1181 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
1182 != (IFF_UP|IFF_BROADCAST))
1183 continue;
1185 * Get its netmask and check that it's on the right subnet.
1187 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1188 continue;
1189 mask = ((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr;
1190 if ((ipaddr & mask) != (ina & mask))
1191 continue;
1193 break;
1197 if (ifr >= ifend)
1198 return 0;
1199 info("found interface %s for proxy arp", ifr->ifr_name);
1202 * Grab the physical address for this interface.
1204 strlcpy(ifdevea.ifr_name, ifr->ifr_name, sizeof(ifdevea.ifr_name));
1205 if (ioctl(sockfd, SIOCRPHYSADDR, &ifdevea) < 0) {
1206 error("Couldn't get h/w address for %s: %m", ifr->ifr_name);
1207 return 0;
1210 hwaddr->sa_family = AF_UNSPEC;
1211 BCOPY(ifdevea.current_pa, hwaddr->sa_data, 6);
1212 return 1;
1216 * Return user specified netmask, modified by any mask we might determine
1217 * for address `addr' (in network byte order).
1218 * Here we scan through the system's list of interfaces, looking for
1219 * any non-point-to-point interfaces which might appear to be on the same
1220 * network as `addr'. If we find any, we OR in their netmask to the
1221 * user-specified netmask.
1223 u_int32_t
1224 GetMask(addr)
1225 u_int32_t addr;
1227 u_int32_t mask, nmask, ina;
1228 struct ifreq *ifr, *ifend, ifreq;
1229 struct ifconf ifc;
1230 struct ifreq ifs[MAX_IFS];
1232 addr = ntohl(addr);
1233 if (IN_CLASSA(addr)) /* determine network mask for address class */
1234 nmask = IN_CLASSA_NET;
1235 else if (IN_CLASSB(addr))
1236 nmask = IN_CLASSB_NET;
1237 else
1238 nmask = IN_CLASSC_NET;
1239 /* class D nets are disallowed by bad_ip_adrs */
1240 mask = netmask | htonl(nmask);
1243 * Scan through the system's network interfaces.
1245 ifc.ifc_len = sizeof(ifs);
1246 ifc.ifc_req = ifs;
1247 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1248 warn("ioctl(SIOCGIFCONF): %m");
1249 return mask;
1251 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1252 for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1253 ((char *)&ifr->ifr_addr + sizeof(struct sockaddr))) {
1255 * Check the interface's internet address.
1257 if (ifr->ifr_addr.sa_family != AF_INET)
1258 continue;
1259 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1260 if ((ntohl(ina) & nmask) != (addr & nmask))
1261 continue;
1263 * Check that the interface is up, and not point-to-point or loopback.
1265 strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1266 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1267 continue;
1268 if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
1269 != IFF_UP)
1270 continue;
1272 * Get its netmask and OR it into our mask.
1274 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1275 continue;
1276 mask |= ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr;
1279 return mask;
1283 * have_route_to - determine if the system has any route to
1284 * a given IP address.
1285 * For demand mode to work properly, we have to ignore routes
1286 * through our own interface.
1288 int have_route_to(u_int32_t addr)
1290 return -1;
1295 * Use the hostid as part of the random number seed.
1298 get_host_seed()
1300 return gethostid();
1305 Seems like strdup() is not part of string package in Ultrix.
1306 If I understood the man-page on the sun this should work.
1308 Robert Olsson
1311 char *strdup( in ) char *in;
1313 char* dup;
1314 if(! (dup = (char *) malloc( strlen( in ) +1 ))) return NULL;
1315 (void) strcpy( dup, in );
1316 return dup;
1320 * This logwtmp() implementation is subject to the following copyright:
1322 * Copyright (c) 1988 The Regents of the University of California.
1323 * All rights reserved.
1325 * Redistribution and use in source and binary forms are permitted
1326 * provided that the above copyright notice and this paragraph are
1327 * duplicated in all such forms and that any documentation,
1328 * advertising materials, and other materials related to such
1329 * distribution and use acknowledge that the software was developed
1330 * by the University of California, Berkeley. The name of the
1331 * University may not be used to endorse or promote products derived
1332 * from this software without specific prior written permission.
1333 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1334 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1335 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1338 #define WTMPFILE "/usr/adm/wtmp"
1340 void
1341 logwtmp(line, name, host)
1342 const char *line, *name, *host;
1344 int fd;
1345 struct stat buf;
1346 struct utmp ut;
1348 if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0)
1349 return;
1350 if (!fstat(fd, &buf)) {
1351 strncpy(ut.ut_line, line, sizeof(ut.ut_line));
1352 strncpy(ut.ut_name, name, sizeof(ut.ut_name));
1353 strncpy(ut.ut_host, host, sizeof(ut.ut_host));
1354 (void)time(&ut.ut_time);
1355 if (write(fd, (char *)&ut, sizeof(struct utmp)) != sizeof(struct utmp))
1356 (void)ftruncate(fd, buf.st_size);
1358 close(fd);
1361 #if 0
1363 * Routines for locking and unlocking the serial device, moved here
1364 * from chat.c.
1367 #define LOCK_PREFIX "/usr/spool/uucp/LCK.."
1370 * lock - create a lock file for the named device
1373 lock(dev)
1374 char *dev;
1376 int fd, pid, n;
1377 char *p;
1378 size_t l;
1380 if ((p = strrchr(dev, '/')) != NULL)
1381 dev = p + 1;
1382 l = strlen(LOCK_PREFIX) + strlen(dev) + 1;
1383 lock_file = malloc(l);
1384 if (lock_file == NULL)
1385 novm("lock file name");
1386 slprintf(lock_file, l, "%s%s, LOCK_PREFIX, dev);
1388 while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
1389 if (errno == EEXIST
1390 && (fd = open(lock_file, O_RDONLY, 0)) >= 0) {
1391 /* Read the lock file to find out who has the device locked */
1392 n = read(fd, &pid, sizeof(pid));
1393 if (n <= 0) {
1394 error("Can't read pid from lock file %s", lock_file);
1395 close(fd);
1396 } else {
1397 if (kill(pid, 0) == -1 && errno == ESRCH) {
1398 /* pid no longer exists - remove the lock file */
1399 if (unlink(lock_file) == 0) {
1400 close(fd);
1401 notice("Removed stale lock on %s (pid %d)",
1402 dev, pid);
1403 continue;
1404 } else
1405 warn("Couldn't remove stale lock on %s",
1406 dev);
1407 } else
1408 notice("Device %s is locked by pid %d",
1409 dev, pid);
1411 close(fd);
1412 } else
1413 error("Can't create lock file %s: %m", lock_file);
1414 free(lock_file);
1415 lock_file = NULL;
1416 return -1;
1419 pid = getpid();
1420 write(fd, &pid, sizeof pid);
1422 close(fd);
1423 return 0;
1427 * unlock - remove our lockfile
1429 void
1430 unlock()
1432 if (lock_file) {
1433 unlink(lock_file);
1434 free(lock_file);
1435 lock_file = NULL;
1438 #endif