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.
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
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
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
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
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 $"
56 #include <sys/types.h>
58 #include <sys/socket.h>
59 #include <sys/ioctl.h>
61 #include <sys/errno.h>
63 #include <sys/fcntl.h>
66 #include <net/ppp_defs.h>
67 #include <net/if_ppp.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>
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 */
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
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.
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");
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().
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
);
162 cifproxyarp(0, proxy_arp_addr
);
168 * ppp_available - check whether the system has any ppp interfaces
169 * (in fact we check whether we can do an ioctl on ppp0).
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;
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";
194 * establish_ppp - Turn the serial port into a ppp interface.
200 int pppdisc
= PPPDISC
;
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.
218 if (ioctl(fd
, PPPIOCGFLAGS
, (caddr_t
) &x
) < 0) {
219 warn("ioctl(PPPIOCGFLAGS): %m");
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");
241 * disestablish_ppp - Restore the serial port to normal operation.
242 * This shouldn't call die() because it's called from die().
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");
253 /* Restore old line discipline. */
254 if (initdisc
>= 0 && ioctl(fd
, TIOCSETD
, &initdisc
) < 0)
255 error("ioctl(TIOCSETD): %m");
260 * Check whether the link seems not to be 8-bit clean.
268 if (ioctl(ttyfd
, PPPIOCGFLAGS
, (caddr_t
) &x
) == 0) {
270 switch (~x
& (SC_RCV_B7_0
|SC_RCV_B7_1
|SC_RCV_EVNP
|SC_RCV_ODDP
)) {
272 s
= "bit 7 set to 1";
275 s
= "bit 7 set to 0";
285 warn("Serial link is not 8-bit clean:");
286 warn("All received characters had %s", s
);
292 * List of valid speeds.
295 int speed_int
, speed_val
;
371 #warning Defining B115200
382 * Translate from bits/second to a speed_t.
388 struct speed
*speedp
;
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
);
400 * Translate from a speed_t to bits/second.
406 struct speed
*speedp
;
410 for (speedp
= speeds
; speedp
->speed_int
; speedp
++)
411 if (speed
== speedp
->speed_val
)
412 return speedp
->speed_int
;
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.
423 set_up_tty(fd
, local
)
426 int speed
, x
, modembits
;
429 if (tcgetattr(fd
, &tios
) < 0)
430 fatal("tcgetattr: %m");
435 tios
.c_cflag
&= ~(CSIZE
| CSTOPB
| PARENB
| CLOCAL
);
437 tios
.c_cflag
|= CS8
| CREAD
| HUPCL
;
439 tios
.c_cflag
|= CLOCAL
;
441 tios
.c_iflag
= IGNBRK
| IGNPAR
;
445 tios
.c_cc
[VTIME
] = 0;
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
);
455 cfsetospeed(&tios
, speed
);
456 cfsetispeed(&tios
, speed
);
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.
464 fatal("Baud rate for %s is 0; need explicit baud rate",
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
);
482 * restore_tty - restore the terminal to the saved settings.
489 if (tcsetattr(fd
, TCSAFLUSH
, &inittermios
) < 0)
491 warn("tcsetattr: %m");
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.
509 int modembits
= TIOCM_DTR
;
517 /* ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits); */
518 ioctl(fd
, (on
? TIOCSDTR
: TIOCCDTR
), 0);
523 * output - Output PPP packet.
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");
546 * wait_input - wait until there is data available,
547 * for the length of time specified by *timo (indefinite
552 struct timeval
*timo
;
558 n
= select(max_in_fd
+ 1, &ready
, NULL
, &ready
, timo
);
559 if (n
< 0 && errno
!= EINTR
)
565 * add_fd - add an fd to the set that wait_input waits for.
576 * remove_fd - remove an fd from the set that wait_input waits for.
585 * read_packet - get a PPP packet from the serial device.
593 if ((len
= read(ttyfd
, buf
, PPP_MTU
+ PPP_HDRLEN
)) < 0) {
594 if (errno
== EWOULDBLOCK
|| errno
== EINTR
) {
595 SYSDEBUG(("read: %m"));
605 * ppp_send_config - configure the transmit characteristics of
609 ppp_send_config(unit
, mtu
, asyncmap
, pcomp
, accomp
)
617 strlcpy(ifr
.ifr_name
, ifname
, sizeof (ifr
.ifr_name
));
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.
639 ppp_set_xaccm(unit
, 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
653 ppp_recv_config(unit
, mru
, asyncmap
, pcomp
, accomp
)
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
;
680 struct ppp_option_data data
;
683 data
.length
= opt_len
;
684 data
.transmit
= for_transmit
;
685 if (ioctl(ttyfd
, PPPIOCSCOMPRESS
, (caddr_t
) &data
) >= 0)
687 return (errno
== ENOBUFS
)? 0: -1;
691 * ccp_flags_set - inform kernel about the current state of CCP.
694 ccp_flags_set(unit
, isopen
, isup
)
695 int unit
, isopen
, isup
;
699 if (ioctl(ttyfd
, PPPIOCGFLAGS
, (caddr_t
) &x
) < 0) {
700 error("ioctl(PPPIOCGFLAGS): %m");
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
)
720 if (ioctl(ttyfd
, PPPIOCGFLAGS
, (caddr_t
) &x
) < 0) {
721 error("ioctl(PPPIOCGFLAGS): %m");
724 return x
& SC_DC_FERROR
;
728 * sifvjcomp - config tcp header compression
731 sifvjcomp(u
, vjcomp
, cidcomp
, maxcid
)
732 int u
, vjcomp
, cidcomp
, maxcid
;
736 if (ioctl(ttyfd
, PPPIOCGFLAGS
, (caddr_t
) &x
) < 0) {
737 error("ioctl(PPIOCGFLAGS): %m");
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");
746 if (ioctl(ttyfd
, PPPIOCSMAXCID
, (caddr_t
) &maxcid
) < 0) {
747 error("ioctl(PPPIOCSFLAGS): %m");
754 * sifup - Config the interface up and enable IP packets to pass.
757 #define SC_ENABLE_IP 0x100 /* compat for old versions of kernel code */
768 strlcpy(ifr
.ifr_name
, ifname
, sizeof (ifr
.ifr_name
));
769 if (ioctl(sockfd
, SIOCGIFFLAGS
, (caddr_t
) &ifr
) < 0) {
770 error("ioctl (SIOCGIFFLAGS): %m");
773 ifr
.ifr_flags
|= IFF_UP
;
774 if (ioctl(sockfd
, SIOCSIFFLAGS
, (caddr_t
) &ifr
) < 0) {
775 error("ioctl(SIOCSIFFLAGS): %m");
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");
786 /* for backwards compatibility */
787 if (ioctl(ttyfd
, PPPIOCGFLAGS
, (caddr_t
) &x
) < 0) {
788 error("ioctl (PPPIOCGFLAGS): %m");
792 if (ioctl(ttyfd
, PPPIOCSFLAGS
, (caddr_t
) &x
) < 0) {
793 error("ioctl(PPPIOCSFLAGS): %m");
801 * sifdown - Config the interface down and disable IP.
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");
824 ifr
.ifr_flags
&= ~IFF_UP
;
825 if (ioctl(sockfd
, SIOCSIFFLAGS
, (caddr_t
) &ifr
) < 0) {
826 error("ioctl(SIOCSIFFLAGS): %m");
835 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
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.
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");
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");
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");
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.
894 (void) sifaddr(u
, o
, h
, 0L);
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");
909 * sifdefaultroute - assign a default route through the address given.
912 sifdefaultroute(u
, l
, g
)
916 return dodefaultroute(g
, 's');
920 * cifdefaultroute - delete a default route through the address given.
923 cifdefaultroute(u
, l
, g
)
927 return dodefaultroute(g
, 'c');
931 * dodefaultroute - talk to a routing socket to add/delete a default route.
934 dodefaultroute(g
, cmd
)
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");
950 default_route_gateway
= (cmd
== 's')? g
: 0;
955 * sifproxyarp - Make a proxy ARP entry for the peer.
958 sifproxyarp(unit
, 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");
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");
983 proxy_arp_addr
= hisaddr
;
988 * cifproxyarp - Delete the proxy ARP entry for the peer.
991 cifproxyarp(unit
, 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");
1009 * get_ether_addr - get the hardware address of an interface on the
1010 * the same subnet as ipaddr.
1015 get_ether_addr(ipaddr
, hwaddr
)
1017 struct sockaddr
*hwaddr
;
1019 struct ifreq
*ifr
, *ifend
, *ifp
;
1020 u_int32_t ina
, mask
;
1021 struct ether_addr dla
;
1024 struct ifreq ifs
[MAX_IFS
];
1025 struct hostent
*hostent
;
1027 ifc
.ifc_len
= sizeof(ifs
);
1029 if (ioctl(sockfd
, SIOCGIFCONF
, &ifc
) < 0) {
1030 error("ioctl(SIOCGIFCONF): %m");
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
1048 if (ioctl(sockfd
, SIOCGIFFLAGS
, &ifreq
) < 0)
1050 if ((ifreq
.ifr_flags
&
1051 (IFF_UP
|IFF_BROADCAST
|IFF_POINTOPOINT
|IFF_LOOPBACK
|IFF_NOARP
))
1052 != (IFF_UP
|IFF_BROADCAST
))
1055 * Get its netmask and check that it's on the right subnet.
1057 if (ioctl(sockfd
, SIOCGIFNETMASK
, &ifreq
) < 0)
1059 mask
= ((struct sockaddr_in
*)&ifreq
.ifr_addr
)->sin_addr
.s_addr
;
1060 if ((ipaddr
& mask
) != (ina
& mask
))
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
)
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
));
1088 ether_by_host(hostname
, etherptr
)
1090 struct ether_addr
*etherptr
;
1092 struct ether_addr
*thisptr
;
1098 if (!ether_hostton(hostname
, etherptr
))
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
))
1114 * Now we can convert the returned string into an ethernet address.
1116 strlcpy(path
, val
.ni_namelist_val
[0], sizeof(path
));
1118 if ((thisptr
= (struct ether_addr
*)ether_aton(path
)) == NULL
)
1120 BCOPY(thisptr
, etherptr
, sizeof(struct ether_addr
));
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.
1138 u_int32_t mask
, nmask
, ina
;
1139 struct ifreq
*ifr
, *ifend
, ifreq
;
1141 struct ifreq ifs
[MAX_IFS
];
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
;
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
);
1158 if (ioctl(sockfd
, SIOCGIFCONF
, &ifc
) < 0) {
1159 warn("ioctl(SIOCGIFCONF): %m");
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
)
1170 ina
= ((struct sockaddr_in
*) &ifr
->ifr_addr
)->sin_addr
.s_addr
;
1171 if ((ntohl(ina
) & nmask
) != (addr
& nmask
))
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)
1179 if ((ifreq
.ifr_flags
& (IFF_UP
|IFF_POINTOPOINT
|IFF_LOOPBACK
))
1183 * Get its netmask and OR it into our mask.
1185 if (ioctl(sockfd
, SIOCGIFNETMASK
, &ifreq
) < 0)
1187 mask
|= ((struct sockaddr_in
*)&ifreq
.ifr_addr
)->sin_addr
.s_addr
;
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
)
1206 * daemon - Detach us from the terminal session.
1209 daemon(nochdir
, noclose
)
1210 int nochdir
, noclose
;
1214 if ((pid
= fork()) < 0)
1217 exit(0); /* parent dies */
1222 fclose(stdin
); /* don't need stdin, stdout, stderr */
1234 char *d
= malloc(strlen(s
) + 1);
1236 if (d
) strcpy(d
, s
);
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"
1262 logwtmp(line
, name
, host
)
1263 const char *line
, *name
, *host
;
1269 if ((fd
= open(WTMPFILE
, O_WRONLY
|O_APPEND
, 0)) < 0)
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
);
1284 * Routines for locking and unlocking the serial device, moved here
1288 #define LOCK_PREFIX "/usr/spool/uucp/LCK/LCK.."
1290 static char *lock_file
;
1293 * lock - create a lock file for the named device
1303 if ((p
= strrchr(dev
, '/')) != NULL
)
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) {
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
));
1317 error("Can't read pid from lock file %s", lock_file
);
1320 if (kill(pid
, 0) == -1 && errno
== ESRCH
) {
1321 /* pid no longer exists - remove the lock file */
1322 if (unlink(lock_file
) == 0) {
1324 notice("Removed stale lock on %s (pid %d)",
1328 warn("Couldn't remove stale lock on %s", dev
);
1330 notice("Device %s is locked by pid %d",
1335 error("Can't create lock file %s: %m", lock_file
);
1342 write(fd
, &pid
, sizeof pid
);
1349 * unlock - remove our lockfile
1362 #if defined(i386) && defined(HAS_BROKEN_IOCTL)
1373 u_char let
, code
, size
;
1375 size
= (cmd
>> 16) & IOCPARM_MASK
;
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
);
1384 ret
= ioctl(fd
, cmd
, c
);
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
]);
1400 if (ret
== -1 && errno
== EPERM
)
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
1422 * This is kind of a hack, especially since there are
1423 * other routines of the Posix lib still used, but
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
)
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
)
1448 sv
.sv_handler
= act
->sa_handler
;
1449 sv
.sv_mask
= act
->sa_mask
;
1455 warn("PPPD: Inside modified HP and SPARC sigaction\n");
1458 return sigvec(sig
, &sv
, NULL
);
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
)
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
)
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");
1495 stats
->bytes_in
= req
.stats
.p
.ppp_ibytes
;
1496 stats
->bytes_out
= req
.stats
.p
.ppp_obytes
;
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.
1514 while ((n
= read(loop_master
, inbuf
, sizeof(inbuf
))) >= 0) {
1515 if (loop_chars(inbuf
, n
))
1520 fatal("eof on loopback");
1521 if (errno
!= EWOULDBLOCK
)
1522 fatal("read from loopback: %m");
1531 * sifnpmode - Set the mode for handling packets for a given NP.
1534 sifnpmode(u
, proto
, mode
)
1541 npi
.protocol
= proto
;
1543 if (ioctl(ttyfd
, PPPIOCSNPMODE
, &npi
) < 0) {
1544 error("ioctl(set NP %d mode to %d): %m", proto
, mode
);
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.
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
;
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");
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.
1599 if (ioctl(ttyfd
, PPPIOCGFLAGS
, (caddr_t
) &flags
) < 0) {
1600 warn("ioctl (PPPIOCGFLAGS): %m");
1602 flags
|= (kdebugflag
& 0xFF) * SC_DEBUG
;
1603 if (ioctl(ttyfd
, PPPIOCSFLAGS
, (caddr_t
) &flags
) < 0)
1604 warn("ioctl(PPPIOCSFLAGS): %m");
1614 * restore_loop - reattach the ppp unit to the loopback.
1622 * Transfer the ppp interface back to the loopback.
1624 if (ioctl(ttyfd
, PPPIOCXFERUNIT
, 0) < 0)
1625 fatal("ioctl(transfer ppp unit): %m");
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");
1636 fatal("transfer_ppp failed: wanted unit %d, got %d",
1643 * Use the hostid as part of the random number seed.
1653 * sys_check_options - check the options that the user specified
1659 * We don't support demand dialing yet.
1663 option_error("PPP-2.3 for NeXTSTEP does not yet support demand dialing");
1671 * sys_close - Clean up in a child process before execing.
1677 if (loop_slave
>= 0) {
1686 * wait_loop_output - wait until there is data available on the
1687 * loopback, for the length of time specified by *timo (indefinite
1690 void wait_loop_output(timo
)
1691 struct timeval
*timo
;
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
;
1712 n
= select(0, NULL
, NULL
, NULL
, timo
);
1713 if (n
< 0 && errno
!= EINTR
)
1714 fatal("select: %m");