Add winbind plugin from Andrew Bartlet.
[mpls-ppp.git] / pppd / sys-NeXT.c
blobbf819f6af5293884cdbfa00548a975f06f39e4d3
1 /*
2 * sys-next.c - System-dependent procedures for setting up
3 * PPP interfaces on NeXT 3.2/3.3 systems
5 * Copyright (c) 1994 Philippe-Andre Prindeville.
6 * All rights reserved.
8 * Copyright (c) 1984-2000 Carnegie Mellon University. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in
19 * the documentation and/or other materials provided with the
20 * distribution.
22 * 3. The name "Carnegie Mellon University" must not be used to
23 * endorse or promote products derived from this software without
24 * prior written permission. For permission or any legal
25 * details, please contact
26 * Office of Technology Transfer
27 * Carnegie Mellon University
28 * 5000 Forbes Avenue
29 * Pittsburgh, PA 15213-3890
30 * (412) 268-4387, fax: (412) 268-7395
31 * tech-transfer@andrew.cmu.edu
33 * 4. Redistributions of any form whatsoever must retain the following
34 * acknowledgment:
35 * "This product includes software developed by Computing Services
36 * at Carnegie Mellon University (http://www.cmu.edu/computing/)."
38 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
39 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
40 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
41 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
42 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
43 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
44 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
47 #define RCSID "$Id: sys-NeXT.c,v 1.21 2002/12/04 23:03:32 paulus Exp $"
49 #include <stdio.h>
50 #include <termios.h>
51 #include <utmp.h>
52 #include <unistd.h>
53 #include <stdlib.h>
54 #include <libc.h>
55 #include <strings.h>
56 #include <sys/types.h>
57 #include <sys/file.h>
58 #include <sys/socket.h>
59 #include <sys/ioctl.h>
60 #include <sys/time.h>
61 #include <sys/errno.h>
62 #include <sys/stat.h>
63 #include <sys/fcntl.h>
65 #include <net/if.h>
66 #include <net/ppp_defs.h>
67 #include <net/if_ppp.h>
68 #include <netdb.h>
69 #include <netinet/in.h>
70 #include <netinet/in_systm.h>
71 #include <netinet/in_var.h>
72 #if !(NS_TARGET >= 40)
73 /* XXX get an error "duplicate member ip_v under 4.1 GAMMA */
74 #include <netinet/ip.h>
75 #endif /* NS_TARGET */
76 #include <netinet/if_ether.h>
77 #include <net/route.h>
78 #include <netinet/in.h>
80 #include <netinfo/ni.h>
82 #include "pppd.h"
84 static const char rcsid[] = RCSID;
86 static int initdisc = -1; /* Initial TTY discipline */
87 static int initfdflags = -1; /* Initial file descriptor flags for fd */
88 static int ppp_fd = -1; /* fd which is set to PPP discipline */
89 static int loop_slave = -1;
90 static int loop_master;
91 static char loop_name[20];
93 static fd_set in_fds; /* set of fds that wait_input waits for */
94 static int max_in_fd; /* highest fd set in in_fds */
96 extern int errno;
98 static int restore_term; /* 1 => we've munged the terminal */
99 static struct termios inittermios; /* Initial TTY termios */
101 static int sockfd; /* socket for doing interface ioctls */
102 static int pppdev; /* +++ */
104 #if defined(i386) && defined(HAS_BROKEN_IOCTL)
105 #define ioctl myioctl
106 #endif
108 static int if_is_up; /* the interface is currently up */
109 static u_int32_t default_route_gateway; /* gateway addr for default route */
110 static u_int32_t proxy_arp_addr; /* remote addr for proxy arp */
112 /* Prototypes for procedures local to this file. */
113 static int translate_speed __P((int));
114 static int baud_rate_of __P((int));
115 static int dodefaultroute __P((u_int32_t, int));
116 static int get_ether_addr __P((u_int32_t, struct sockaddr *));
117 static int ether_by_host __P((char *, struct ether_addr *));
121 * sys_init - System-dependent initialization.
123 void
124 sys_init()
126 openlog("pppd", LOG_PID | LOG_NDELAY, LOG_PPP);
127 setlogmask(LOG_UPTO(LOG_INFO));
129 /* Get an internet socket for doing socket ioctl's on. */
130 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
131 fatal("Couldn't create IP socket: %m");
133 if((pppdev = open("/dev/ppp0", O_RDWR, O_NONBLOCK)) == NULL)
134 fatal("Couldn't open /dev/ppp0: %m");
136 FD_ZERO(&in_fds);
137 max_in_fd = 0;
141 * sys_cleanup - restore any system state we modified before exiting:
142 * mark the interface down, delete default route and/or proxy arp entry.
143 * This should call die() because it's called from die().
145 void
146 sys_cleanup()
148 struct ifreq ifr;
150 if (if_is_up) {
151 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
152 if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) >= 0
153 && ((ifr.ifr_flags & IFF_UP) != 0)) {
154 ifr.ifr_flags &= ~IFF_UP;
155 ioctl(sockfd, SIOCSIFFLAGS, &ifr);
159 if (default_route_gateway)
160 cifdefaultroute(0, 0, default_route_gateway);
161 if (proxy_arp_addr)
162 cifproxyarp(0, proxy_arp_addr);
164 close(pppdev);
168 * ppp_available - check whether the system has any ppp interfaces
169 * (in fact we check whether we can do an ioctl on ppp0).
172 ppp_available()
174 int s, ok;
175 struct ifreq ifr;
176 extern char *no_ppp_msg;
178 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
179 return 1; /* can't tell - maybe we're not root */
181 strlcpy(ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
182 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
183 close(s);
185 no_ppp_msg = "\
186 This system lacks kernel support for PPP. To include PPP support\n\
187 in the kernel, please follow the steps detailed in the README.NeXT\n\
188 file in the ppp-2.2 distribution.\n";
190 return ok;
194 * establish_ppp - Turn the serial port into a ppp interface.
197 establish_ppp(fd)
198 int fd;
200 int pppdisc = PPPDISC;
201 int x;
203 if (ioctl(fd, TIOCGETD, &initdisc) < 0)
204 fatal("ioctl(TIOCGETD): %m");
205 if (ioctl(fd, TIOCSETD, &pppdisc) < 0)
206 fatal("ioctl(establish TIOCSETD): %m");
209 * Find out which interface we were given.
211 if (ioctl(fd, PPPIOCGUNIT, &ifunit) < 0)
212 fatal("ioctl(PPPIOCGUNIT): %m");
215 * Enable debug in the driver if requested.
217 if (kdebugflag) {
218 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
219 warn("ioctl(PPPIOCGFLAGS): %m");
220 } else {
221 x |= (kdebugflag & 0xFF) * SC_DEBUG;
222 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
223 warn("ioctl(PPPIOCSFLAGS): %m");
228 * Set device for non-blocking reads so PPPD can poll for
229 * input from the kernel.
231 if ((initfdflags = fcntl(fd, F_GETFL)) == -1
232 || fcntl(fd, F_SETFL, initfdflags | O_NONBLOCK) == -1) {
233 warn("Couldn't set device to non-blocking mode: %m");
236 return fd;
241 * disestablish_ppp - Restore the serial port to normal operation.
242 * This shouldn't call die() because it's called from die().
244 void
245 disestablish_ppp(fd)
246 int fd;
248 /* Reset non-blocking mode on fd. */
249 if (initfdflags != -1 && fcntl(fd, F_SETFL, initfdflags) < 0)
250 warn("Couldn't restore device fd flags: %m");
251 initfdflags = -1;
253 /* Restore old line discipline. */
254 if (initdisc >= 0 && ioctl(fd, TIOCSETD, &initdisc) < 0)
255 error("ioctl(TIOCSETD): %m");
256 initdisc = -1;
260 * Check whether the link seems not to be 8-bit clean.
262 void
263 clean_check()
265 int x;
266 char *s;
268 if (ioctl(ttyfd, PPPIOCGFLAGS, (caddr_t) &x) == 0) {
269 s = NULL;
270 switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP)) {
271 case SC_RCV_B7_0:
272 s = "bit 7 set to 1";
273 break;
274 case SC_RCV_B7_1:
275 s = "bit 7 set to 0";
276 break;
277 case SC_RCV_EVNP:
278 s = "odd parity";
279 break;
280 case SC_RCV_ODDP:
281 s = "even parity";
282 break;
284 if (s != NULL) {
285 warn("Serial link is not 8-bit clean:");
286 warn("All received characters had %s", s);
292 * List of valid speeds.
294 struct speed {
295 int speed_int, speed_val;
296 } speeds[] = {
297 #ifdef B50
298 { 50, B50 },
299 #endif
300 #ifdef B75
301 { 75, B75 },
302 #endif
303 #ifdef B110
304 { 110, B110 },
305 #endif
306 #ifdef B134
307 { 134, B134 },
308 #endif
309 #ifdef B150
310 { 150, B150 },
311 #endif
312 #ifdef B200
313 { 200, B200 },
314 #endif
315 #ifdef B300
316 { 300, B300 },
317 #endif
318 #ifdef B600
319 { 600, B600 },
320 #endif
321 #ifdef B1200
322 { 1200, B1200 },
323 #endif
324 #ifdef B1800
325 { 1800, B1800 },
326 #endif
327 #ifdef B2000
328 { 2000, B2000 },
329 #endif
330 #ifdef B2400
331 { 2400, B2400 },
332 #endif
333 #ifdef B3600
334 { 3600, B3600 },
335 #endif
336 #ifdef B4800
337 { 4800, B4800 },
338 #endif
339 #ifdef B7200
340 { 7200, B7200 },
341 #endif
342 #ifdef B9600
343 { 9600, B9600 },
344 #endif
345 #ifdef B19200
346 { 19200, B19200 },
347 #endif
348 #ifdef B38400
349 { 38400, B38400 },
350 #endif
351 #ifdef EXTA
352 { 19200, EXTA },
353 #endif
354 #ifdef EXTB
355 { 38400, EXTB },
356 #endif
357 #ifdef B14400
358 { 14400, B14400 },
359 #endif
360 #ifdef B28800
361 { 28800, B28800 },
362 #endif
363 #ifdef B43200
364 { 43200, B43200 },
365 #endif
366 #ifdef B57600
367 { 57600, B57600 },
368 #endif
370 #ifndef B115200
371 #warning Defining B115200
372 #define B115200 20
373 #endif
375 #ifdef B115200
376 { 115200, B115200 },
377 #endif
378 { 0, 0 }
382 * Translate from bits/second to a speed_t.
385 translate_speed(bps)
386 int bps;
388 struct speed *speedp;
390 if (bps == 0)
391 return 0;
392 for (speedp = speeds; speedp->speed_int; speedp++)
393 if (bps == speedp->speed_int)
394 return speedp->speed_val;
395 warn("speed %d not supported", bps);
396 return 0;
400 * Translate from a speed_t to bits/second.
402 static int
403 baud_rate_of(speed)
404 int speed;
406 struct speed *speedp;
408 if (speed == 0)
409 return 0;
410 for (speedp = speeds; speedp->speed_int; speedp++)
411 if (speed == speedp->speed_val)
412 return speedp->speed_int;
413 return 0;
418 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
419 * at the requested speed, etc. If `local' is true, set CLOCAL
420 * regardless of whether the modem option was specified.
422 void
423 set_up_tty(fd, local)
424 int fd, local;
426 int speed, x, modembits;
427 struct termios tios;
429 if (tcgetattr(fd, &tios) < 0)
430 fatal("tcgetattr: %m");
432 if (!restore_term)
433 inittermios = tios;
435 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
437 tios.c_cflag |= CS8 | CREAD | HUPCL;
438 if (local || !modem)
439 tios.c_cflag |= CLOCAL;
441 tios.c_iflag = IGNBRK | IGNPAR;
442 tios.c_oflag = 0;
443 tios.c_lflag = 0;
444 tios.c_cc[VMIN] = 1;
445 tios.c_cc[VTIME] = 0;
447 if (crtscts == -2) {
448 tios.c_iflag |= IXON | IXOFF;
449 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
450 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
453 speed = translate_speed(inspeed);
454 if (speed) {
455 cfsetospeed(&tios, speed);
456 cfsetispeed(&tios, speed);
457 } else {
458 speed = cfgetospeed(&tios);
460 * We can't proceed if the serial port speed is B0,
461 * since that implies that the serial port is disabled.
463 if (speed == B0)
464 fatal("Baud rate for %s is 0; need explicit baud rate",
465 devnam);
468 if (modem) {
469 modembits = TIOCM_RTS | TIOCM_CTS;
470 if (ioctl(fd, (crtscts ? TIOCMBIS : TIOCMBIC), &modembits) < 0)
471 error("ioctl: TIOCMBIS/BIC: %m");
474 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0)
475 fatal("tcsetattr: %m");
477 baud_rate = inspeed = baud_rate_of(speed);
478 restore_term = 1;
482 * restore_tty - restore the terminal to the saved settings.
484 void
485 restore_tty(fd)
486 int fd;
488 if (restore_term) {
489 if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
490 if (errno != ENXIO)
491 warn("tcsetattr: %m");
492 restore_term = 0;
497 * setdtr - control the DTR line on the serial port.
498 * This is called from die(), so it shouldn't call die().
500 * The write hack is to get NXFax to recognize that there is
501 * activity on the port. Not using the write nukes
502 * NXFax's capability to determine port usage.
505 void
506 setdtr(fd, on)
507 int fd, on;
509 int modembits = TIOCM_DTR;
511 if (!on)
513 write(fd, " ", 1);
514 sleep(1);
517 /* ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits); */
518 ioctl(fd, (on? TIOCSDTR: TIOCCDTR), 0);
523 * output - Output PPP packet.
525 void
526 output(unit, p, len)
527 int unit;
528 u_char *p;
529 int len;
531 if (debug)
532 dbglog("sent %P", p, len);
534 if (write(ttyfd, p, len) < 0) {
535 if (errno == EWOULDBLOCK || errno == ENOBUFS
536 || errno == ENXIO || errno == EIO) {
537 warn("write: warning: %m");
538 } else {
539 fatal("write: %m");
546 * wait_input - wait until there is data available,
547 * for the length of time specified by *timo (indefinite
548 * if timo is NULL).
550 void
551 wait_input(timo)
552 struct timeval *timo;
554 fd_set ready;
555 int n;
557 ready = in_fds;
558 n = select(max_in_fd + 1, &ready, NULL, &ready, timo);
559 if (n < 0 && errno != EINTR)
560 fatal("select: %m");
565 * add_fd - add an fd to the set that wait_input waits for.
567 void add_fd(fd)
568 int fd;
570 FD_SET(fd, &in_fds);
571 if (fd > max_in_fd)
572 max_in_fd = fd;
576 * remove_fd - remove an fd from the set that wait_input waits for.
578 void remove_fd(fd)
579 int fd;
581 FD_CLR(fd, &in_fds);
585 * read_packet - get a PPP packet from the serial device.
588 read_packet(buf)
589 u_char *buf;
591 int len;
593 if ((len = read(ttyfd, buf, PPP_MTU + PPP_HDRLEN)) < 0) {
594 if (errno == EWOULDBLOCK || errno == EINTR) {
595 SYSDEBUG(("read: %m"));
596 return -1;
598 fatal("read: %m");
600 return len;
605 * ppp_send_config - configure the transmit characteristics of
606 * the ppp interface.
608 void
609 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
610 int unit, mtu;
611 u_int32_t asyncmap;
612 int pcomp, accomp;
614 u_int x;
615 struct ifreq ifr;
617 strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
618 ifr.ifr_mtu = mtu;
619 if (ioctl(sockfd, SIOCSIFMTU, (caddr_t) &ifr) < 0)
620 fatal("ioctl(SIOCSIFMTU): %m");
622 if (ioctl(ttyfd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0)
623 fatal("ioctl(PPPIOCSASYNCMAP): %m");
625 if (ioctl(ttyfd, PPPIOCGFLAGS, (caddr_t) &x) < 0)
626 fatal("ioctl(PPPIOCGFLAGS): %m");
628 x = pcomp? x | SC_COMP_PROT: x &~ SC_COMP_PROT;
629 x = accomp? x | SC_COMP_AC: x &~ SC_COMP_AC;
630 if (ioctl(ttyfd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
631 fatal("ioctl(PPPIOCSFLAGS): %m");
636 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
638 void
639 ppp_set_xaccm(unit, accm)
640 int unit;
641 ext_accm accm;
643 if (ioctl(ttyfd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY)
644 warn("ioctl(PPPIOCSXASYNCMAP): %m");
649 * ppp_recv_config - configure the receive-side characteristics of
650 * the ppp interface.
652 void
653 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
654 int unit, mru;
655 u_int32_t asyncmap;
656 int pcomp, accomp;
658 int x;
660 if (ioctl(ttyfd, PPPIOCSMRU, (caddr_t) &mru) < 0)
661 fatal("ioctl(PPPIOCSMRU): %m");
662 if (ioctl(ttyfd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0)
663 fatal("ioctl(PPPIOCSRASYNCMAP): %m");
664 if (ioctl(ttyfd, PPPIOCGFLAGS, (caddr_t) &x) < 0)
665 fatal("ioctl(PPPIOCGFLAGS): %m");
666 x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC;
667 if (ioctl(ttyfd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
668 fatal("ioctl(PPPIOCSFLAGS): %m");
672 * ccp_test - ask kernel whether a given compression method
673 * is acceptable for use.
676 ccp_test(unit, opt_ptr, opt_len, for_transmit)
677 int unit, opt_len, for_transmit;
678 u_char *opt_ptr;
680 struct ppp_option_data data;
682 data.ptr = opt_ptr;
683 data.length = opt_len;
684 data.transmit = for_transmit;
685 if (ioctl(ttyfd, PPPIOCSCOMPRESS, (caddr_t) &data) >= 0)
686 return 1;
687 return (errno == ENOBUFS)? 0: -1;
691 * ccp_flags_set - inform kernel about the current state of CCP.
693 void
694 ccp_flags_set(unit, isopen, isup)
695 int unit, isopen, isup;
697 int x;
699 if (ioctl(ttyfd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
700 error("ioctl(PPPIOCGFLAGS): %m");
701 return;
703 x = isopen? x | SC_CCP_OPEN: x &~ SC_CCP_OPEN;
704 x = isup? x | SC_CCP_UP: x &~ SC_CCP_UP;
705 if (ioctl(ttyfd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
706 error("ioctl(PPPIOCSFLAGS): %m");
710 * ccp_fatal_error - returns 1 if decompression was disabled as a
711 * result of an error detected after decompression of a packet,
712 * 0 otherwise. This is necessary because of patent nonsense.
715 ccp_fatal_error(unit)
716 int unit;
718 int x;
720 if (ioctl(ttyfd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
721 error("ioctl(PPPIOCGFLAGS): %m");
722 return 0;
724 return x & SC_DC_FERROR;
728 * sifvjcomp - config tcp header compression
731 sifvjcomp(u, vjcomp, cidcomp, maxcid)
732 int u, vjcomp, cidcomp, maxcid;
734 u_int x;
736 if (ioctl(ttyfd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
737 error("ioctl(PPIOCGFLAGS): %m");
738 return 0;
740 x = vjcomp ? x | SC_COMP_TCP: x &~ SC_COMP_TCP;
741 x = cidcomp? x & ~SC_NO_TCP_CCID: x | SC_NO_TCP_CCID;
742 if (ioctl(ttyfd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
743 error("ioctl(PPPIOCSFLAGS): %m");
744 return 0;
746 if (ioctl(ttyfd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0) {
747 error("ioctl(PPPIOCSFLAGS): %m");
748 return 0;
750 return 1;
754 * sifup - Config the interface up and enable IP packets to pass.
756 #ifndef SC_ENABLE_IP
757 #define SC_ENABLE_IP 0x100 /* compat for old versions of kernel code */
758 #endif
761 sifup(u)
762 int u;
764 struct ifreq ifr;
765 u_int x;
766 struct npioctl npi;
768 strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
769 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
770 error("ioctl (SIOCGIFFLAGS): %m");
771 return 0;
773 ifr.ifr_flags |= IFF_UP;
774 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
775 error("ioctl(SIOCSIFFLAGS): %m");
776 return 0;
778 if_is_up = 1;
779 npi.protocol = PPP_IP;
780 npi.mode = NPMODE_PASS;
781 if (ioctl(ttyfd, PPPIOCSNPMODE, &npi) < 0) {
782 if (errno != ENOTTY) {
783 error("ioctl(PPPIOCSNPMODE): %m");
784 return 0;
786 /* for backwards compatibility */
787 if (ioctl(ttyfd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
788 error("ioctl (PPPIOCGFLAGS): %m");
789 return 0;
791 x |= SC_ENABLE_IP;
792 if (ioctl(ttyfd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
793 error("ioctl(PPPIOCSFLAGS): %m");
794 return 0;
797 return 1;
801 * sifdown - Config the interface down and disable IP.
804 sifdown(u)
805 int u;
807 struct ifreq ifr;
808 u_int x;
809 int rv;
810 struct npioctl npi;
812 rv = 1;
813 npi.protocol = PPP_IP;
814 npi.mode = NPMODE_ERROR;
815 ioctl(ttyfd, PPPIOCSNPMODE, (caddr_t) &npi);
816 /* ignore errors, because ttyfd might have been closed by now. */
819 strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
820 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
821 error("ioctl (SIOCGIFFLAGS): %m");
822 rv = 0;
823 } else {
824 ifr.ifr_flags &= ~IFF_UP;
825 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
826 error("ioctl(SIOCSIFFLAGS): %m");
827 rv = 0;
828 } else
829 if_is_up = 0;
831 return rv;
835 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
836 * if it exists.
838 #define SET_SA_FAMILY(addr, family) \
839 BZERO((char *) &(addr), sizeof(addr)); \
840 addr.sa_family = (family);
843 * sifaddr - Config the interface IP addresses and netmask.
846 sifaddr(u, o, h, m)
847 int u;
848 u_int32_t o, h, m;
850 int ret;
851 struct ifreq ifr;
853 ret = 1;
854 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
855 SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
856 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
857 if (ioctl(sockfd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
858 error("ioctl(SIOCAIFADDR): %m");
859 ret = 0;
861 ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = h;
862 if (ioctl(sockfd, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
863 error("ioctl(SIOCSIFDSTADDR): %m");
864 ret = 0;
866 if (m != 0) {
867 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = m;
868 info("Setting interface mask to %s\n", ip_ntoa(m));
869 if (ioctl(sockfd, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
870 error("ioctl(SIOCSIFNETMASK): %m");
871 ret = 0;
874 return ret;
878 * cifaddr - Clear the interface IP addresses, and delete routes
879 * through the interface if possible.
881 * N.B.: under NextStep, you can't *delete* an address on an interface,
882 * so we change it to 0.0.0.0... A real hack. But it simplifies
883 * reconnection on the server side.
886 cifaddr(u, o, h)
887 int u;
888 u_int32_t o, h;
890 struct rtentry rt;
892 #if 1
893 h = o = 0L;
894 (void) sifaddr(u, o, h, 0L);
895 #endif
896 SET_SA_FAMILY(rt.rt_dst, AF_INET);
897 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = h;
898 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
899 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = o;
900 rt.rt_flags = RTF_HOST;
901 if (ioctl(sockfd, SIOCDELRT, (caddr_t) &rt) < 0) {
902 error("ioctl(SIOCDELRT): %m");
903 return 0;
905 return 1;
909 * sifdefaultroute - assign a default route through the address given.
912 sifdefaultroute(u, l, g)
913 int u;
914 u_int32_t l, g;
916 return dodefaultroute(g, 's');
920 * cifdefaultroute - delete a default route through the address given.
923 cifdefaultroute(u, l, g)
924 int u;
925 u_int32_t l, g;
927 return dodefaultroute(g, 'c');
931 * dodefaultroute - talk to a routing socket to add/delete a default route.
934 dodefaultroute(g, cmd)
935 u_int32_t g;
936 int cmd;
938 struct rtentry rt;
940 SET_SA_FAMILY(rt.rt_dst, AF_INET);
941 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = 0L;
942 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
943 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
944 rt.rt_flags = RTF_GATEWAY;
945 if (ioctl(sockfd, (cmd == 's') ? SIOCADDRT : SIOCDELRT, &rt) < 0) {
946 error("%cifdefaultroute: ioctl(%s): %m", cmd,
947 (cmd == 's') ? "SIOCADDRT" : "SIOCDELRT");
948 return 0;
950 default_route_gateway = (cmd == 's')? g: 0;
951 return 1;
955 * sifproxyarp - Make a proxy ARP entry for the peer.
958 sifproxyarp(unit, hisaddr)
959 int unit;
960 u_int32_t hisaddr;
962 struct arpreq arpreq;
964 BZERO(&arpreq, sizeof(arpreq));
967 * Get the hardware address of an interface on the same subnet
968 * as our local address.
970 if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) {
971 error("Cannot determine ethernet address for proxy ARP");
972 return 0;
975 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
976 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
977 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
978 if (ioctl(sockfd, SIOCSARP, (caddr_t)&arpreq) < 0) {
979 error("ioctl(SIOCSARP): %m");
980 return 0;
983 proxy_arp_addr = hisaddr;
984 return 1;
988 * cifproxyarp - Delete the proxy ARP entry for the peer.
991 cifproxyarp(unit, hisaddr)
992 int unit;
993 u_int32_t hisaddr;
995 struct arpreq arpreq;
997 BZERO(&arpreq, sizeof(arpreq));
998 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
999 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1000 if (ioctl(sockfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
1001 warn("ioctl(SIOCDARP): %m");
1002 return 0;
1004 proxy_arp_addr = 0;
1005 return 1;
1009 * get_ether_addr - get the hardware address of an interface on the
1010 * the same subnet as ipaddr.
1012 #define MAX_IFS 32
1015 get_ether_addr(ipaddr, hwaddr)
1016 u_int32_t ipaddr;
1017 struct sockaddr *hwaddr;
1019 struct ifreq *ifr, *ifend, *ifp;
1020 u_int32_t ina, mask;
1021 struct ether_addr dla;
1022 struct ifreq ifreq;
1023 struct ifconf ifc;
1024 struct ifreq ifs[MAX_IFS];
1025 struct hostent *hostent;
1027 ifc.ifc_len = sizeof(ifs);
1028 ifc.ifc_req = ifs;
1029 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1030 error("ioctl(SIOCGIFCONF): %m");
1031 return 0;
1035 * Scan through looking for an interface with an Internet
1036 * address on the same subnet as `ipaddr'.
1038 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1039 for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1040 ((char *)&ifr->ifr_addr + sizeof(struct sockaddr))) {
1041 if (ifr->ifr_addr.sa_family == AF_INET) {
1042 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1043 strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1045 * Check that the interface is up, and not point-to-point
1046 * or loopback.
1048 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1049 continue;
1050 if ((ifreq.ifr_flags &
1051 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
1052 != (IFF_UP|IFF_BROADCAST))
1053 continue;
1055 * Get its netmask and check that it's on the right subnet.
1057 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1058 continue;
1059 mask = ((struct sockaddr_in*)&ifreq.ifr_addr)->sin_addr.s_addr;
1060 if ((ipaddr & mask) != (ina & mask))
1061 continue;
1063 break;
1067 if (ifr >= ifend)
1068 return 0;
1069 info("found interface %s for proxy arp", ifr->ifr_name);
1072 * Get the hostname and look for an entry using the ethers database.
1073 * Under NeXTStep this is the best we can do for now.
1075 if ((hostent = gethostbyaddr((char*)&ina, sizeof(ina), AF_INET)) == NULL)
1076 return 0;
1078 if (ether_by_host(hostent->h_name, &dla)) {
1079 info("Add entry for %s in /etc/ethers", hostent->h_name);
1080 return 0; /* it's not there */
1082 hwaddr->sa_family = AF_UNSPEC;
1083 BCOPY(&dla, hwaddr->sa_data, sizeof(dla));
1084 return 1;
1087 static int
1088 ether_by_host(hostname, etherptr)
1089 char *hostname;
1090 struct ether_addr *etherptr;
1092 struct ether_addr *thisptr;
1093 void *conn;
1094 ni_id root;
1095 ni_namelist val;
1096 char path[256];
1098 if (!ether_hostton(hostname, etherptr))
1099 return 0;
1101 * We shall now try and
1102 * find the address in the
1103 * top domain of netinfo.
1105 slprintf(path, sizeof(path), "/machines/%s", hostname);
1107 if (ni_open((void *)0, "/", &conn)
1108 || ni_root(conn, &root)
1109 || ni_pathsearch(conn, &root, path)
1110 || ni_lookupprop(conn, &root, "en_address", &val))
1111 return 1;
1114 * Now we can convert the returned string into an ethernet address.
1116 strlcpy(path, val.ni_namelist_val[0], sizeof(path));
1117 ni_free(conn);
1118 if ((thisptr = (struct ether_addr*)ether_aton(path)) == NULL)
1119 return 1;
1120 BCOPY(thisptr, etherptr, sizeof(struct ether_addr));
1121 return 0;
1127 * Return user specified netmask, modified by any mask we might determine
1128 * for address `addr' (in network byte order).
1129 * Here we scan through the system's list of interfaces, looking for
1130 * any non-point-to-point interfaces which might appear to be on the same
1131 * network as `addr'. If we find any, we OR in their netmask to the
1132 * user-specified netmask.
1134 u_int32_t
1135 GetMask(addr)
1136 u_int32_t addr;
1138 u_int32_t mask, nmask, ina;
1139 struct ifreq *ifr, *ifend, ifreq;
1140 struct ifconf ifc;
1141 struct ifreq ifs[MAX_IFS];
1143 addr = ntohl(addr);
1144 if (IN_CLASSA(addr)) /* determine network mask for address class */
1145 nmask = IN_CLASSA_NET;
1146 else if (IN_CLASSB(addr))
1147 nmask = IN_CLASSB_NET;
1148 else
1149 nmask = IN_CLASSC_NET;
1150 /* class D nets are disallowed by bad_ip_adrs */
1151 mask = netmask | htonl(nmask);
1154 * Scan through the system's network interfaces.
1156 ifc.ifc_len = sizeof(ifs);
1157 ifc.ifc_req = ifs;
1158 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1159 warn("ioctl(SIOCGIFCONF): %m");
1160 return mask;
1162 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1163 for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1164 ((char *)&ifr->ifr_addr + sizeof(struct sockaddr))) {
1166 * Check the interface's internet address.
1168 if (ifr->ifr_addr.sa_family != AF_INET)
1169 continue;
1170 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1171 if ((ntohl(ina) & nmask) != (addr & nmask))
1172 continue;
1174 * Check that the interface is up, and not point-to-point or loopback.
1176 strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1177 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1178 continue;
1179 if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
1180 != IFF_UP)
1181 continue;
1183 * Get its netmask and OR it into our mask.
1185 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1186 continue;
1187 mask |= ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr;
1190 return mask;
1194 * have_route_to - determine if the system has any route to
1195 * a given IP address.
1196 * For demand mode to work properly, we have to ignore routes
1197 * through our own interface.
1199 int have_route_to(u_int32_t addr)
1201 return -1;
1204 #if 0
1206 * daemon - Detach us from the terminal session.
1209 daemon(nochdir, noclose)
1210 int nochdir, noclose;
1212 int pid;
1214 if ((pid = fork()) < 0)
1215 return -1;
1216 if (pid != 0)
1217 exit(0); /* parent dies */
1218 (void)setsid();
1219 if (!nochdir)
1220 chdir("/");
1221 if (!noclose) {
1222 fclose(stdin); /* don't need stdin, stdout, stderr */
1223 fclose(stdout);
1224 fclose(stderr);
1226 return 0;
1228 #endif
1230 char *
1231 strdup(s)
1232 const char *s;
1234 char *d = malloc(strlen(s) + 1);
1236 if (d) strcpy(d, s);
1237 return d;
1241 * This logwtmp() implementation is subject to the following copyright:
1243 * Copyright (c) 1988 The Regents of the University of California.
1244 * All rights reserved.
1246 * Redistribution and use in source and binary forms are permitted
1247 * provided that the above copyright notice and this paragraph are
1248 * duplicated in all such forms and that any documentation,
1249 * advertising materials, and other materials related to such
1250 * distribution and use acknowledge that the software was developed
1251 * by the University of California, Berkeley. The name of the
1252 * University may not be used to endorse or promote products derived
1253 * from this software without specific prior written permission.
1254 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1255 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1256 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1259 #define WTMPFILE "/usr/adm/wtmp"
1261 void
1262 logwtmp(line, name, host)
1263 const char *line, *name, *host;
1265 int fd;
1266 struct stat buf;
1267 struct utmp ut;
1269 if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0)
1270 return;
1271 if (!fstat(fd, &buf)) {
1272 strncpy(ut.ut_line, line, sizeof(ut.ut_line));
1273 strncpy(ut.ut_name, name, sizeof(ut.ut_name));
1274 strncpy(ut.ut_host, host, sizeof(ut.ut_host));
1275 (void)time(&ut.ut_time);
1276 if (write(fd, (char *)&ut, sizeof(struct utmp)) != sizeof(struct utmp))
1277 (void)ftruncate(fd, buf.st_size);
1279 close(fd);
1282 #if 0
1284 * Routines for locking and unlocking the serial device, moved here
1285 * from chat.c.
1288 #define LOCK_PREFIX "/usr/spool/uucp/LCK/LCK.."
1290 static char *lock_file;
1293 * lock - create a lock file for the named device
1296 lock(dev)
1297 char *dev;
1299 int fd, pid, n;
1300 char *p;
1301 size_t l;
1303 if ((p = strrchr(dev, '/')) != NULL)
1304 dev = p + 1;
1305 l = strlen(LOCK_PREFIX) + strlen(dev) + 1;
1306 lock_file = malloc(l);
1307 if (lock_file == NULL)
1308 novm("lock file name");
1309 slprintf(lock_file, l, "%s%s", LOCK_PREFIX, dev);
1311 while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
1312 if (errno == EEXIST
1313 && (fd = open(lock_file, O_RDONLY, 0)) >= 0) {
1314 /* Read the lock file to find out who has the device locked */
1315 n = read(fd, &pid, sizeof(pid));
1316 if (n <= 0) {
1317 error("Can't read pid from lock file %s", lock_file);
1318 close(fd);
1319 } else {
1320 if (kill(pid, 0) == -1 && errno == ESRCH) {
1321 /* pid no longer exists - remove the lock file */
1322 if (unlink(lock_file) == 0) {
1323 close(fd);
1324 notice("Removed stale lock on %s (pid %d)",
1325 dev, pid);
1326 continue;
1327 } else
1328 warn("Couldn't remove stale lock on %s", dev);
1329 } else
1330 notice("Device %s is locked by pid %d",
1331 dev, pid);
1333 close(fd);
1334 } else
1335 error("Can't create lock file %s: %m", lock_file);
1336 free(lock_file);
1337 lock_file = NULL;
1338 return -1;
1341 pid = getpid();
1342 write(fd, &pid, sizeof pid);
1344 close(fd);
1345 return 0;
1349 * unlock - remove our lockfile
1351 void
1352 unlock()
1354 if (lock_file) {
1355 unlink(lock_file);
1356 free(lock_file);
1357 lock_file = NULL;
1360 #endif
1362 #if defined(i386) && defined(HAS_BROKEN_IOCTL)
1364 ioctl(fd, cmd, c)
1365 int fd, cmd;
1366 caddr_t c;
1368 #undef ioctl
1369 int ret;
1371 #ifdef DEBUGIOCTL
1372 int serrno;
1373 u_char let, code, size;
1375 size = (cmd >> 16) & IOCPARM_MASK;
1376 let = (cmd >> 8);
1377 code = cmd;
1379 if (let == 't' && (75 <= code && code <= 90))
1380 info("ioctl(%d, 0x%x ('%c', %d, %d), 0x%x)\n", fd, cmd,
1381 let, code, size, c);
1382 #endif
1384 ret = ioctl(fd, cmd, c);
1386 #ifdef DEBUGIOCTL
1387 serrno = errno;
1388 if (ret == -1)
1389 info("ioctl('%c', %d, %d) errno = %d (%m)\n",
1390 let, code, size, errno);
1391 if (let == 't' && (75 <= code && code <= 90) && (cmd & IOC_OUT)) {
1392 int i, len = ((cmd >> 16) & IOCPARM_MASK);
1393 for (i = 0; i < len / 4; ++i)
1394 info("word[%d] @ 0x%06x = 0x%x\n",
1395 i, &((int *) c)[i],((int *)c)[i]);
1397 errno = serrno;
1398 #endif
1400 if (ret == -1 && errno == EPERM)
1401 errno = ret = 0;
1402 return ret;
1404 #endif /* HAS_BROKEN_IOCTL */
1407 #if defined(FIXSIGS) && (defined (hppa) || defined(sparc))
1410 * These redefinitions of Posix functions are necessary
1411 * because HPPA systems have an OS bug that causes
1412 * sigaction to core dump:
1414 * AlainF 9-Nov-1994 HACK FOR HP-PA/NEXTSTEP
1415 * sigaction(3) seems broken in the HP-PA NeXTSTEP 3.2
1416 * Posix lib. This causes pppd to SIGBUS at the expiration
1417 * of the first timeout (_sigtramp seems to invoke
1418 * the SIGALRM handler at an unreasonably low address).
1419 * All calls so sigaction(3) have been changed to calls
1420 * to sigvec(2) and sigprocmask(SIG_BLOCK,...) to
1421 * sigblock(2).
1422 * This is kind of a hack, especially since there are
1423 * other routines of the Posix lib still used, but
1424 * it worked for me.
1426 * Dave Hess <David-Hess@net.tamu.edu> noted that 3.3 Sparc seems to
1427 * have the same bug. Thus this fix has been enabled for SPARC also.
1432 int sigemptyset(sigset_t *mask)
1434 *mask = 0;
1437 sigaddset(sigset_t *mask, int which_sig)
1439 *mask |= sigmask(which_sig);
1443 int sigaction(int sig, const struct sigaction *act, struct sigaction *oact)
1445 struct sigvec sv;
1446 static int in = 0;
1448 sv.sv_handler = act->sa_handler;
1449 sv.sv_mask = act->sa_mask;
1450 sv.sv_flags = 0;
1452 if (!in)
1454 in = 1;
1455 warn("PPPD: Inside modified HP and SPARC sigaction\n");
1458 return sigvec(sig, &sv, NULL);
1461 #endif
1465 * Code following is added for 2.3 compatibility
1469 * get_idle_time - return how long the link has been idle.
1472 get_idle_time(u, ip)
1473 int u;
1474 struct ppp_idle *ip;
1476 return (ioctl(ttyfd, PPPIOCGIDLE, ip) >= 0);
1480 * get_ppp_stats - return statistics for the link.
1483 get_ppp_stats(u, stats)
1484 int u;
1485 struct pppd_stats *stats;
1487 struct ifpppstatsreq req;
1489 memset (&req, 0, sizeof (req));
1490 strlcpy(req.ifr_name, interface, sizeof(req.ifr_name));
1491 if (ioctl(sockfd, SIOCGPPPSTATS, &req) < 0) {
1492 error("Couldn't get PPP statistics: %m");
1493 return 0;
1495 stats->bytes_in = req.stats.p.ppp_ibytes;
1496 stats->bytes_out = req.stats.p.ppp_obytes;
1497 return 1;
1502 * get_loop_output - read characters from the loopback, form them
1503 * into frames, and detect when we want to bring the real link up.
1504 * Return value is 1 if we need to bring up the link, 0 otherwise.
1507 get_loop_output()
1510 #if 0
1511 int rv = 0;
1512 int n;
1514 while ((n = read(loop_master, inbuf, sizeof(inbuf))) >= 0) {
1515 if (loop_chars(inbuf, n))
1516 rv = 1;
1519 if (n == 0)
1520 fatal("eof on loopback");
1521 if (errno != EWOULDBLOCK)
1522 fatal("read from loopback: %m");
1524 return rv;
1525 #endif
1527 return 0;
1531 * sifnpmode - Set the mode for handling packets for a given NP.
1534 sifnpmode(u, proto, mode)
1535 int u;
1536 int proto;
1537 enum NPmode mode;
1539 struct npioctl npi;
1541 npi.protocol = proto;
1542 npi.mode = mode;
1543 if (ioctl(ttyfd, PPPIOCSNPMODE, &npi) < 0) {
1544 error("ioctl(set NP %d mode to %d): %m", proto, mode);
1545 return 0;
1547 return 1;
1552 * open_ppp_loopback - open the device we use for getting
1553 * packets in demand mode, and connect it to a ppp interface.
1554 * Here we use a pty.
1557 open_ppp_loopback()
1560 #if 0
1561 int flags;
1562 struct termios tios;
1563 int pppdisc = PPPDISC;
1565 fatal("open_ppp_loopback called!");
1567 if (openpty(&loop_master, &loop_slave, loop_name, NULL, NULL) < 0)
1568 fatal("No free pty for loopback");
1569 SYSDEBUG(("using %s for loopback", loop_name));
1571 if (tcgetattr(loop_slave, &tios) == 0) {
1572 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB);
1573 tios.c_cflag |= CS8 | CREAD;
1574 tios.c_iflag = IGNPAR;
1575 tios.c_oflag = 0;
1576 tios.c_lflag = 0;
1577 if (tcsetattr(loop_slave, TCSAFLUSH, &tios) < 0)
1578 warn("couldn't set attributes on loopback: %m");
1581 if ((flags = fcntl(loop_master, F_GETFL)) != -1)
1582 if (fcntl(loop_master, F_SETFL, flags | O_NONBLOCK) == -1)
1583 warn("couldn't set loopback to nonblock: %m");
1585 ttyfd = loop_slave;
1586 if (ioctl(ttyfd, TIOCSETD, &pppdisc) < 0)
1587 fatal("ioctl(TIOCSETD): %m");
1590 * Find out which interface we were given.
1592 if (ioctl(ttyfd, PPPIOCGUNIT, &ifunit) < 0)
1593 fatal("ioctl(PPPIOCGUNIT): %m");
1596 * Enable debug in the driver if requested.
1598 if (kdebugflag) {
1599 if (ioctl(ttyfd, PPPIOCGFLAGS, (caddr_t) &flags) < 0) {
1600 warn("ioctl (PPPIOCGFLAGS): %m");
1601 } else {
1602 flags |= (kdebugflag & 0xFF) * SC_DEBUG;
1603 if (ioctl(ttyfd, PPPIOCSFLAGS, (caddr_t) &flags) < 0)
1604 warn("ioctl(PPPIOCSFLAGS): %m");
1608 return loop_master;
1609 #endif
1614 * restore_loop - reattach the ppp unit to the loopback.
1616 void
1617 restore_loop()
1619 int x;
1622 * Transfer the ppp interface back to the loopback.
1624 if (ioctl(ttyfd, PPPIOCXFERUNIT, 0) < 0)
1625 fatal("ioctl(transfer ppp unit): %m");
1626 x = PPPDISC;
1627 if (ioctl(loop_slave, TIOCSETD, &x) < 0)
1628 fatal("ioctl(TIOCSETD): %m");
1631 * Check that we got the same unit again.
1633 if (ioctl(loop_slave, PPPIOCGUNIT, &x) < 0)
1634 fatal("ioctl(PPPIOCGUNIT): %m");
1635 if (x != ifunit)
1636 fatal("transfer_ppp failed: wanted unit %d, got %d",
1637 ifunit, x);
1638 ttyfd = loop_slave;
1643 * Use the hostid as part of the random number seed.
1646 get_host_seed()
1648 return gethostid();
1653 * sys_check_options - check the options that the user specified
1656 sys_check_options()
1659 * We don't support demand dialing yet.
1661 if (demand)
1663 option_error("PPP-2.3 for NeXTSTEP does not yet support demand dialing");
1664 return 0;
1666 return 1;
1671 * sys_close - Clean up in a child process before execing.
1673 void
1674 sys_close()
1676 close(sockfd);
1677 if (loop_slave >= 0) {
1678 close(loop_slave);
1679 close(loop_master);
1681 closelog();
1684 #if 0
1686 * wait_loop_output - wait until there is data available on the
1687 * loopback, for the length of time specified by *timo (indefinite
1688 * if timo is NULL).
1690 void wait_loop_output(timo)
1691 struct timeval *timo;
1693 fd_set ready;
1694 int n;
1696 FD_ZERO(&ready);
1697 FD_SET(loop_master, &ready);
1698 n = select(loop_master + 1, &ready, NULL, &ready, timo);
1699 if (n < 0 && errno != EINTR)
1700 fatal("select: %m");
1704 * wait_time - wait for a given length of time or until a
1705 * signal is received.
1707 void wait_time(timo)
1708 struct timeval *timo;
1710 int n;
1712 n = select(0, NULL, NULL, NULL, timo);
1713 if (n < 0 && errno != EINTR)
1714 fatal("select: %m");
1716 #endif