]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - usr.sbin/pppd/sys-bsd.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / usr.sbin / pppd / sys-bsd.c
1 /*
2  * sys-bsd.c - System-dependent procedures for setting up
3  * PPP interfaces on bsd-4.4-ish systems (including 386BSD, NetBSD, etc.)
4  *
5  * Copyright (c) 1989 Carnegie Mellon University.
6  * Copyright (c) 1995 The Australian National University.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms are permitted
10  * provided that the above copyright notice and this paragraph are
11  * duplicated in all such forms and that any documentation,
12  * advertising materials, and other materials related to such
13  * distribution and use acknowledge that the software was developed
14  * by Carnegie Mellon University and The Australian National University.
15  * The names of the Universities may not be used to endorse or promote
16  * products derived from this software without specific prior written
17  * permission.
18  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
20  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21  */
22
23 #ifndef lint
24 static char rcsid[] = "$FreeBSD$";
25 #endif
26 /*      $NetBSD: sys-bsd.c,v 1.1.1.3 1997/09/26 18:53:04 christos Exp $ */
27
28 /*
29  * TODO:
30  */
31
32 #include <stdio.h>
33 #include <syslog.h>
34 #include <string.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <termios.h>
40 #include <signal.h>
41 #include <sys/ioctl.h>
42 #include <sys/types.h>
43 #include <sys/socket.h>
44 #include <sys/time.h>
45 #include <sys/stat.h>
46 #include <sys/param.h>
47 #include <sys/module.h>
48 #ifdef NetBSD1_2
49 #include <util.h>
50 #endif
51 #ifdef PPP_FILTER
52 #include <net/bpf.h>
53 #endif
54
55 #include <net/if.h>
56 #include <net/ppp_defs.h>
57 #include <net/if_ppp.h>
58 #include <net/route.h>
59 #include <net/if_dl.h>
60 #include <netinet/in.h>
61 #include <net/if_var.h>
62 #include <netinet6/in6_var.h>
63 #include <netinet6/nd6.h>
64 #include <ifaddrs.h>
65
66 #ifdef IPX_CHANGE
67 #include <netipx/ipx.h>
68 #endif
69
70 #if RTM_VERSION >= 3
71 #include <sys/param.h>
72 #if defined(NetBSD) && (NetBSD >= 199703)
73 #include <netinet/if_inarp.h>
74 #else   /* NetBSD 1.2D or later */
75 #ifdef __FreeBSD__
76 #include <netinet/if_ether.h>
77 #else
78 #include <net/if_ether.h>
79 #endif
80 #endif
81 #endif
82
83 #include <ifaddrs.h>
84
85 #include "pppd.h"
86 #include "fsm.h"
87 #include "ipcp.h"
88
89 static int initdisc = -1;       /* Initial TTY discipline for ppp_fd */
90 static int initfdflags = -1;    /* Initial file descriptor flags for ppp_fd */
91 static int ppp_fd = -1;         /* fd which is set to PPP discipline */
92 static int rtm_seq;
93
94 static int restore_term;        /* 1 => we've munged the terminal */
95 static struct termios inittermios; /* Initial TTY termios */
96 static struct winsize wsinfo;   /* Initial window size info */
97
98 static char *lock_file;         /* name of lock file created */
99
100 static int loop_slave = -1;
101 static int loop_master;
102 static char loop_name[20];
103
104 static unsigned char inbuf[512]; /* buffer for chars read from loopback */
105
106 static int sockfd;              /* socket for doing interface ioctls */
107 #ifdef INET6
108 static int sock6_fd = -1;       /* socket for doing ipv6 interface ioctls */
109 #endif /* INET6 */
110
111 static int if_is_up;            /* the interface is currently up */
112 static u_int32_t ifaddrs[2];    /* local and remote addresses we set */
113 static u_int32_t default_route_gateway; /* gateway addr for default route */
114 static u_int32_t proxy_arp_addr;        /* remote addr for proxy arp */
115
116 /* Prototypes for procedures local to this file. */
117 static int dodefaultroute __P((u_int32_t, int));
118 static int get_ether_addr __P((u_int32_t, struct sockaddr_dl *));
119
120
121 /*
122  * sys_init - System-dependent initialization.
123  */
124 void
125 sys_init()
126 {
127     /* Get an internet socket for doing socket ioctl's on. */
128     if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
129         syslog(LOG_ERR, "Couldn't create IP socket: %m");
130         die(1);
131     }
132
133 #ifdef INET6
134     if ((sock6_fd = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
135         /* check it at runtime */
136         sock6_fd = -1;
137     }
138 #endif
139 }
140
141 /*
142  * sys_cleanup - restore any system state we modified before exiting:
143  * mark the interface down, delete default route and/or proxy arp entry.
144  * This should call die() because it's called from die().
145  */
146 void
147 sys_cleanup()
148 {
149     struct ifreq ifr;
150
151     if (if_is_up) {
152         strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
153         if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) >= 0
154             && ((ifr.ifr_flags & IFF_UP) != 0)) {
155             ifr.ifr_flags &= ~IFF_UP;
156             ioctl(sockfd, SIOCSIFFLAGS, &ifr);
157         }
158     }
159     if (ifaddrs[0] != 0)
160         cifaddr(0, ifaddrs[0], ifaddrs[1]);
161     if (default_route_gateway)
162         cifdefaultroute(0, 0, default_route_gateway);
163     if (proxy_arp_addr)
164         cifproxyarp(0, proxy_arp_addr);
165 }
166
167 /*
168  * sys_close - Clean up in a child process before execing.
169  */
170 void
171 sys_close()
172 {
173     if (sockfd >= 0)
174         close(sockfd);
175 #ifdef INET6
176     if (sock6_fd >= 0)
177         close(sock6_fd);
178 #endif
179     if (loop_slave >= 0) {
180         close(loop_slave);
181         close(loop_master);
182     }
183 }
184
185 /*
186  * sys_check_options - check the options that the user specified
187  */
188 void
189 sys_check_options()
190 {
191 }
192
193 /*
194  * ppp_available - check whether the system has the ppp module loaded
195  * or compiled in. If it doesn't, and we're actually root (not just SUID
196  * root) try loading it before giving up.
197  */
198 int
199 ppp_available()
200 {
201     const char *modname = "if_ppp";
202     extern char *no_ppp_msg;
203
204     if (modfind(modname) != -1) {
205         return 1;
206     }
207
208     if (getuid() == 0 && kldload(modname) != -1)
209         return 1;
210
211     no_ppp_msg = "\
212 This system lacks kernel support for PPP.  To include PPP support\n\
213 in the kernel, please add \"device ppp\" to your kernel config or \n\
214 load the if_ppp module.\n";
215
216     return 0;
217 }
218
219 /*
220  * establish_ppp - Turn the serial port into a ppp interface.
221  */
222 void
223 establish_ppp(fd)
224     int fd;
225 {
226     int pppdisc = PPPDISC;
227     int x;
228
229     if (demand) {
230         /*
231          * Demand mode - prime the old ppp device to relinquish the unit.
232          */
233         if (ioctl(ppp_fd, PPPIOCXFERUNIT, 0) < 0) {
234             syslog(LOG_ERR, "ioctl(transfer ppp unit): %m");
235             die(1);
236         }
237     }
238
239     /*
240      * Save the old line discipline of fd, and set it to PPP.
241      */
242     if (ioctl(fd, TIOCGETD, &initdisc) < 0) {
243         syslog(LOG_ERR, "ioctl(TIOCGETD): %m");
244         die(1);
245     }
246     if (ioctl(fd, TIOCSETD, &pppdisc) < 0) {
247         syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
248         die(1);
249     }
250
251     if (!demand) {
252         /*
253          * Find out which interface we were given.
254          */
255         if (ioctl(fd, PPPIOCGUNIT, &ifunit) < 0) {      
256             syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m");
257             die(1);
258         }
259     } else {
260         /*
261          * Check that we got the same unit again.
262          */
263         if (ioctl(fd, PPPIOCGUNIT, &x) < 0) {   
264             syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m");
265             die(1);
266         }
267         if (x != ifunit) {
268             syslog(LOG_ERR, "transfer_ppp failed: wanted unit %d, got %d",
269                    ifunit, x);
270             die(1);
271         }
272         x = TTYDISC;
273         ioctl(loop_slave, TIOCSETD, &x);
274     }
275
276     ppp_fd = fd;
277
278     /*
279      * Enable debug in the driver if requested.
280      */
281     if (kdebugflag) {
282         if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
283             syslog(LOG_WARNING, "ioctl (PPPIOCGFLAGS): %m");
284         } else {
285             x |= (kdebugflag & 0xFF) * SC_DEBUG;
286             if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
287                 syslog(LOG_WARNING, "ioctl(PPPIOCSFLAGS): %m");
288         }
289     }
290
291     /*
292      * Set device for non-blocking reads.
293      */
294     if ((initfdflags = fcntl(fd, F_GETFL)) == -1
295         || fcntl(fd, F_SETFL, initfdflags | O_NONBLOCK) == -1) {
296         syslog(LOG_WARNING, "Couldn't set device to non-blocking mode: %m");
297     }
298 }
299
300 /*
301  * restore_loop - reattach the ppp unit to the loopback.
302  */
303 void
304 restore_loop()
305 {
306     int x;
307
308     /*
309      * Transfer the ppp interface back to the loopback.
310      */
311     if (ioctl(ppp_fd, PPPIOCXFERUNIT, 0) < 0) {
312         syslog(LOG_ERR, "ioctl(transfer ppp unit): %m");
313         die(1);
314     }
315     x = PPPDISC;
316     if (ioctl(loop_slave, TIOCSETD, &x) < 0) {
317         syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
318         die(1);
319     }
320
321     /*
322      * Check that we got the same unit again.
323      */
324     if (ioctl(loop_slave, PPPIOCGUNIT, &x) < 0) {       
325         syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m");
326         die(1);
327     }
328     if (x != ifunit) {
329         syslog(LOG_ERR, "transfer_ppp failed: wanted unit %d, got %d",
330                ifunit, x);
331         die(1);
332     }
333     ppp_fd = loop_slave;
334 }
335
336
337 /*
338  * disestablish_ppp - Restore the serial port to normal operation.
339  * This shouldn't call die() because it's called from die().
340  */
341 void
342 disestablish_ppp(fd)
343     int fd;
344 {
345     /* Reset non-blocking mode on fd. */
346     if (initfdflags != -1 && fcntl(fd, F_SETFL, initfdflags) < 0)
347         syslog(LOG_WARNING, "Couldn't restore device fd flags: %m");
348     initfdflags = -1;
349
350     /* Restore old line discipline. */
351     if (initdisc >= 0 && ioctl(fd, TIOCSETD, &initdisc) < 0)
352         syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
353     initdisc = -1;
354
355     if (fd == ppp_fd)
356         ppp_fd = -1;
357 }
358
359 /*
360  * Check whether the link seems not to be 8-bit clean.
361  */
362 void
363 clean_check()
364 {
365     int x;
366     char *s;
367
368     if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) == 0) {
369         s = NULL;
370         switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP)) {
371         case SC_RCV_B7_0:
372             s = "bit 7 set to 1";
373             break;
374         case SC_RCV_B7_1:
375             s = "bit 7 set to 0";
376             break;
377         case SC_RCV_EVNP:
378             s = "odd parity";
379             break;
380         case SC_RCV_ODDP:
381             s = "even parity";
382             break;
383         }
384         if (s != NULL) {
385             syslog(LOG_WARNING, "Serial link is not 8-bit clean:");
386             syslog(LOG_WARNING, "All received characters had %s", s);
387         }
388     }
389 }
390
391 /*
392  * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
393  * at the requested speed, etc.  If `local' is true, set CLOCAL
394  * regardless of whether the modem option was specified.
395  *
396  * For *BSD, we assume that speed_t values numerically equal bits/second.
397  */
398 void
399 set_up_tty(fd, local)
400     int fd, local;
401 {
402     struct termios tios;
403
404     if (tcgetattr(fd, &tios) < 0) {
405         syslog(LOG_ERR, "tcgetattr: %m");
406         die(1);
407     }
408
409     if (!restore_term) {
410         inittermios = tios;
411         ioctl(fd, TIOCGWINSZ, &wsinfo);
412     }
413
414     tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
415     if (crtscts > 0 && !local)
416         tios.c_cflag |= CRTSCTS;
417     else if (crtscts < 0)
418         tios.c_cflag &= ~CRTSCTS;
419
420     tios.c_cflag |= CS8 | CREAD | HUPCL;
421     if (local || !modem)
422         tios.c_cflag |= CLOCAL;
423     tios.c_iflag = IGNBRK | IGNPAR;
424     tios.c_oflag = 0;
425     tios.c_lflag = 0;
426     tios.c_cc[VMIN] = 1;
427     tios.c_cc[VTIME] = 0;
428
429     if (crtscts == -2) {
430         tios.c_iflag |= IXON | IXOFF;
431         tios.c_cc[VSTOP] = 0x13;        /* DC3 = XOFF = ^S */
432         tios.c_cc[VSTART] = 0x11;       /* DC1 = XON  = ^Q */
433     }
434
435     if (inspeed) {
436         cfsetospeed(&tios, inspeed);
437         cfsetispeed(&tios, inspeed);
438     } else {
439         inspeed = cfgetospeed(&tios);
440         /*
441          * We can't proceed if the serial port speed is 0,
442          * since that implies that the serial port is disabled.
443          */
444         if (inspeed == 0) {
445             syslog(LOG_ERR, "Baud rate for %s is 0; need explicit baud rate",
446                    devnam);
447             die(1);
448         }
449     }
450     baud_rate = inspeed;
451
452     if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
453         syslog(LOG_ERR, "tcsetattr: %m");
454         die(1);
455     }
456
457     restore_term = 1;
458 }
459
460 /*
461  * restore_tty - restore the terminal to the saved settings.
462  */
463 void
464 restore_tty(fd)
465     int fd;
466 {
467     if (restore_term) {
468         if (!default_device) {
469             /*
470              * Turn off echoing, because otherwise we can get into
471              * a loop with the tty and the modem echoing to each other.
472              * We presume we are the sole user of this tty device, so
473              * when we close it, it will revert to its defaults anyway.
474              */
475             inittermios.c_lflag &= ~(ECHO | ECHONL);
476         }
477         if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
478             if (errno != ENXIO)
479                 syslog(LOG_WARNING, "tcsetattr: %m");
480         ioctl(fd, TIOCSWINSZ, &wsinfo);
481         restore_term = 0;
482     }
483 }
484
485 /*
486  * setdtr - control the DTR line on the serial port.
487  * This is called from die(), so it shouldn't call die().
488  */
489 void
490 setdtr(fd, on)
491 int fd, on;
492 {
493     int modembits = TIOCM_DTR;
494
495     ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
496 }
497
498 #ifdef INET6
499 /*
500  * sif6addr - Config the interface with an IPv6 link-local address
501  */
502 int
503 sif6addr(int unit, eui64_t our_eui64, eui64_t his_eui64)
504 {
505     int ifindex;
506     struct in6_aliasreq addreq6;
507
508     if (sock6_fd < 0) {
509         syslog(LOG_ERR, "No IPv6 socket available");
510         die(1);
511         /*NOTREACHED*/
512     }
513
514     /* actually, this part is not kame local - RFC2553 conformant */
515     ifindex = if_nametoindex(ifname);
516     if (ifindex == 0) {
517         syslog(LOG_ERR, "sifaddr6: no interface %s", ifname);
518         return 0;
519     }
520
521     memset(&addreq6, 0, sizeof(addreq6));
522     strlcpy(addreq6.ifra_name, ifname, sizeof(addreq6.ifra_name));
523
524     /* my addr */
525     addreq6.ifra_addr.sin6_family = AF_INET6;
526     addreq6.ifra_addr.sin6_len = sizeof(struct sockaddr_in6);
527     addreq6.ifra_addr.sin6_addr.s6_addr[0] = 0xfe;
528     addreq6.ifra_addr.sin6_addr.s6_addr[1] = 0x80;
529     memcpy(&addreq6.ifra_addr.sin6_addr.s6_addr[8], &our_eui64,
530         sizeof(our_eui64));
531     /* KAME ifindex hack */
532     *(u_int16_t *)&addreq6.ifra_addr.sin6_addr.s6_addr[2] = htons(ifindex);
533
534     /* his addr */
535     addreq6.ifra_dstaddr.sin6_family = AF_INET6;
536     addreq6.ifra_dstaddr.sin6_len = sizeof(struct sockaddr_in6);
537     addreq6.ifra_dstaddr.sin6_addr.s6_addr[0] = 0xfe;
538     addreq6.ifra_dstaddr.sin6_addr.s6_addr[1] = 0x80;
539     memcpy(&addreq6.ifra_dstaddr.sin6_addr.s6_addr[8], &his_eui64,
540         sizeof(our_eui64));
541     /* KAME ifindex hack */
542     *(u_int16_t *)&addreq6.ifra_dstaddr.sin6_addr.s6_addr[2] = htons(ifindex);
543
544     /* prefix mask: 128bit */
545     addreq6.ifra_prefixmask.sin6_family = AF_INET6;
546     addreq6.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
547     memset(&addreq6.ifra_prefixmask.sin6_addr, 0xff,
548         sizeof(addreq6.ifra_prefixmask.sin6_addr));
549
550     /* address lifetime (infty) */
551     addreq6.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
552     addreq6.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
553
554     if (ioctl(sock6_fd, SIOCAIFADDR_IN6, &addreq6) < 0) {
555         syslog(LOG_ERR, "sif6addr: ioctl(SIOCAIFADDR_IN6): %m");
556         return 0;
557     }
558
559     return 1;
560 }
561
562
563 /*
564  * cif6addr - Remove IPv6 address from interface
565  */
566 int
567 cif6addr(int unit, eui64_t our_eui64, eui64_t his_eui64)
568 {
569     int ifindex;
570     struct in6_ifreq delreq6;
571
572     if (sock6_fd < 0) {
573         syslog(LOG_ERR, "No IPv6 socket available");
574         die(1);
575         /*NOTREACHED*/
576     }
577
578     /* actually, this part is not kame local - RFC2553 conformant */
579     ifindex = if_nametoindex(ifname);
580     if (ifindex == 0) {
581         syslog(LOG_ERR, "cifaddr6: no interface %s", ifname);
582         return 0;
583     }
584
585     memset(&delreq6, 0, sizeof(delreq6));
586     strlcpy(delreq6.ifr_name, ifname, sizeof(delreq6.ifr_name));
587
588     /* my addr */
589     delreq6.ifr_ifru.ifru_addr.sin6_family = AF_INET6;
590     delreq6.ifr_ifru.ifru_addr.sin6_len = sizeof(struct sockaddr_in6);
591     delreq6.ifr_ifru.ifru_addr.sin6_addr.s6_addr[0] = 0xfe;
592     delreq6.ifr_ifru.ifru_addr.sin6_addr.s6_addr[1] = 0x80;
593     memcpy(&delreq6.ifr_ifru.ifru_addr.sin6_addr.s6_addr[8], &our_eui64,
594         sizeof(our_eui64));
595     /* KAME ifindex hack */
596     *(u_int16_t *)&delreq6.ifr_ifru.ifru_addr.sin6_addr.s6_addr[2] =
597         htons(ifindex);
598
599     if (ioctl(sock6_fd, SIOCDIFADDR_IN6, &delreq6) < 0) {
600         syslog(LOG_ERR, "cif6addr: ioctl(SIOCDIFADDR_IN6): %m");
601         return 0;
602     }
603
604     return 1;
605 }
606 #endif /* INET6 */
607
608 /*
609  * open_ppp_loopback - open the device we use for getting
610  * packets in demand mode, and connect it to a ppp interface.
611  * Here we use a pty.
612  */
613 void
614 open_ppp_loopback()
615 {
616     int flags;
617     struct termios tios;
618     int pppdisc = PPPDISC;
619
620     if (openpty(&loop_master, &loop_slave, loop_name, NULL, NULL) < 0) {
621         syslog(LOG_ERR, "No free pty for loopback");
622         die(1);
623     }
624     SYSDEBUG((LOG_DEBUG, "using %s for loopback", loop_name));
625
626     if (tcgetattr(loop_slave, &tios) == 0) {
627         tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB);
628         tios.c_cflag |= CS8 | CREAD;
629         tios.c_iflag = IGNPAR;
630         tios.c_oflag = 0;
631         tios.c_lflag = 0;
632         if (tcsetattr(loop_slave, TCSAFLUSH, &tios) < 0)
633             syslog(LOG_WARNING, "couldn't set attributes on loopback: %m");
634     }
635
636     if ((flags = fcntl(loop_master, F_GETFL)) != -1) 
637         if (fcntl(loop_master, F_SETFL, flags | O_NONBLOCK) == -1)
638             syslog(LOG_WARNING, "couldn't set loopback to nonblock: %m");
639
640     ppp_fd = loop_slave;
641     if (ioctl(ppp_fd, TIOCSETD, &pppdisc) < 0) {
642         syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
643         die(1);
644     }
645
646     /*
647      * Find out which interface we were given.
648      */
649     if (ioctl(ppp_fd, PPPIOCGUNIT, &ifunit) < 0) {      
650         syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m");
651         die(1);
652     }
653
654     /*
655      * Enable debug in the driver if requested.
656      */
657     if (kdebugflag) {
658         if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &flags) < 0) {
659             syslog(LOG_WARNING, "ioctl (PPPIOCGFLAGS): %m");
660         } else {
661             flags |= (kdebugflag & 0xFF) * SC_DEBUG;
662             if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &flags) < 0)
663                 syslog(LOG_WARNING, "ioctl(PPPIOCSFLAGS): %m");
664         }
665     }
666
667 }
668
669
670 /*
671  * output - Output PPP packet.
672  */
673 void
674 output(unit, p, len)
675     int unit;
676     u_char *p;
677     int len;
678 {
679     if (debug)
680         log_packet(p, len, "sent ", LOG_DEBUG);
681
682     if (write(ttyfd, p, len) < 0) {
683         if (errno != EIO)
684             syslog(LOG_ERR, "write: %m");
685     }
686 }
687
688
689 /*
690  * wait_input - wait until there is data available on ttyfd,
691  * for the length of time specified by *timo (indefinite
692  * if timo is NULL).
693  */
694 void
695 wait_input(timo)
696     struct timeval *timo;
697 {
698     fd_set ready;
699     int n;
700
701     if (ttyfd >= FD_SETSIZE) {
702             syslog(LOG_ERR, "descriptor too big");
703             die(1);
704     }
705     FD_ZERO(&ready);
706     FD_SET(ttyfd, &ready);
707     n = select(ttyfd+1, &ready, NULL, &ready, timo);
708     if (n < 0 && errno != EINTR) {
709         syslog(LOG_ERR, "select: %m");
710         die(1);
711     }
712 }
713
714
715 /*
716  * wait_loop_output - wait until there is data available on the
717  * loopback, for the length of time specified by *timo (indefinite
718  * if timo is NULL).
719  */
720 void
721 wait_loop_output(timo)
722     struct timeval *timo;
723 {
724     fd_set ready;
725     int n;
726
727     if (loop_master >= FD_SETSIZE) {
728             syslog(LOG_ERR, "descriptor too big");
729             die(1);
730     }
731     FD_ZERO(&ready);
732     FD_SET(loop_master, &ready);
733     n = select(loop_master + 1, &ready, NULL, &ready, timo);
734     if (n < 0 && errno != EINTR) {
735         syslog(LOG_ERR, "select: %m");
736         die(1);
737     }
738 }
739
740
741 /*
742  * wait_time - wait for a given length of time or until a
743  * signal is received.
744  */
745 void
746 wait_time(timo)
747     struct timeval *timo;
748 {
749     int n;
750
751     n = select(0, NULL, NULL, NULL, timo);
752     if (n < 0 && errno != EINTR) {
753         syslog(LOG_ERR, "select: %m");
754         die(1);
755     }
756 }
757
758
759 /*
760  * read_packet - get a PPP packet from the serial device.
761  */
762 int
763 read_packet(buf)
764     u_char *buf;
765 {
766     int len;
767
768     if ((len = read(ttyfd, buf, PPP_MTU + PPP_HDRLEN)) < 0) {
769         if (errno == EWOULDBLOCK || errno == EINTR)
770             return -1;
771         syslog(LOG_ERR, "read: %m");
772         die(1);
773     }
774     return len;
775 }
776
777
778 /*
779  * get_loop_output - read characters from the loopback, form them
780  * into frames, and detect when we want to bring the real link up.
781  * Return value is 1 if we need to bring up the link, 0 otherwise.
782  */
783 int
784 get_loop_output()
785 {
786     int rv = 0;
787     int n;
788
789     while ((n = read(loop_master, inbuf, sizeof(inbuf))) >= 0) {
790         if (loop_chars(inbuf, n))
791             rv = 1;
792     }
793
794     if (n == 0) {
795         syslog(LOG_ERR, "eof on loopback");
796         die(1);
797     } else if (errno != EWOULDBLOCK){
798         syslog(LOG_ERR, "read from loopback: %m");
799         die(1);
800     }
801
802     return rv;
803 }
804
805
806 /*
807  * ppp_send_config - configure the transmit characteristics of
808  * the ppp interface.
809  */
810 void
811 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
812     int unit, mtu;
813     u_int32_t asyncmap;
814     int pcomp, accomp;
815 {
816     u_int x;
817     struct ifreq ifr;
818
819     strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
820     ifr.ifr_mtu = mtu;
821     if (ioctl(sockfd, SIOCSIFMTU, (caddr_t) &ifr) < 0) {
822         syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m");
823         quit();
824     }
825
826     if (ioctl(ppp_fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0) {
827         syslog(LOG_ERR, "ioctl(PPPIOCSASYNCMAP): %m");
828         quit();
829     }
830
831     if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
832         syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
833         quit();
834     }
835     x = pcomp? x | SC_COMP_PROT: x &~ SC_COMP_PROT;
836     x = accomp? x | SC_COMP_AC: x &~ SC_COMP_AC;
837     if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
838         syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
839         quit();
840     }
841 }
842
843
844 /*
845  * ppp_set_xaccm - set the extended transmit ACCM for the interface.
846  */
847 void
848 ppp_set_xaccm(unit, accm)
849     int unit;
850     ext_accm accm;
851 {
852     if (ioctl(ppp_fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY)
853         syslog(LOG_WARNING, "ioctl(set extended ACCM): %m");
854 }
855
856
857 /*
858  * ppp_recv_config - configure the receive-side characteristics of
859  * the ppp interface.
860  */
861 void
862 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
863     int unit, mru;
864     u_int32_t asyncmap;
865     int pcomp, accomp;
866 {
867     int x;
868
869     if (ioctl(ppp_fd, PPPIOCSMRU, (caddr_t) &mru) < 0) {
870         syslog(LOG_ERR, "ioctl(PPPIOCSMRU): %m");
871         quit();
872     }
873     if (ioctl(ppp_fd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0) {
874         syslog(LOG_ERR, "ioctl(PPPIOCSRASYNCMAP): %m");
875         quit();
876     }
877     if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
878         syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
879         quit();
880     }
881     x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC;
882     if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
883         syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
884         quit();
885     }
886 }
887
888 /*
889  * ccp_test - ask kernel whether a given compression method
890  * is acceptable for use.  Returns 1 if the method and parameters
891  * are OK, 0 if the method is known but the parameters are not OK
892  * (e.g. code size should be reduced), or -1 if the method is unknown.
893  */
894 int
895 ccp_test(unit, opt_ptr, opt_len, for_transmit)
896     int unit, opt_len, for_transmit;
897     u_char *opt_ptr;
898 {
899     struct ppp_option_data data;
900
901     data.ptr = opt_ptr;
902     data.length = opt_len;
903     data.transmit = for_transmit;
904     if (ioctl(ttyfd, PPPIOCSCOMPRESS, (caddr_t) &data) >= 0)
905         return 1;
906     return (errno == ENOBUFS)? 0: -1;
907 }
908
909 /*
910  * ccp_flags_set - inform kernel about the current state of CCP.
911  */
912 void
913 ccp_flags_set(unit, isopen, isup)
914     int unit, isopen, isup;
915 {
916     int x;
917
918     if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
919         syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
920         return;
921     }
922     x = isopen? x | SC_CCP_OPEN: x &~ SC_CCP_OPEN;
923     x = isup? x | SC_CCP_UP: x &~ SC_CCP_UP;
924     if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
925         syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
926 }
927
928 /*
929  * ccp_fatal_error - returns 1 if decompression was disabled as a
930  * result of an error detected after decompression of a packet,
931  * 0 otherwise.  This is necessary because of patent nonsense.
932  */
933 int
934 ccp_fatal_error(unit)
935     int unit;
936 {
937     int x;
938
939     if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
940         syslog(LOG_ERR, "ioctl(PPPIOCGFLAGS): %m");
941         return 0;
942     }
943     return x & SC_DC_FERROR;
944 }
945
946 /*
947  * get_idle_time - return how long the link has been idle.
948  */
949 int
950 get_idle_time(u, ip)
951     int u;
952     struct ppp_idle *ip;
953 {
954     return ioctl(ppp_fd, PPPIOCGIDLE, ip) >= 0;
955 }
956
957
958 #ifdef PPP_FILTER
959 /*
960  * set_filters - transfer the pass and active filters to the kernel.
961  */
962 int
963 set_filters(pass, active)
964     struct bpf_program *pass, *active;
965 {
966     int ret = 1;
967
968     if (pass->bf_len > 0) {
969         if (ioctl(ppp_fd, PPPIOCSPASS, pass) < 0) {
970             syslog(LOG_ERR, "Couldn't set pass-filter in kernel: %m");
971             ret = 0;
972         }
973     }
974     if (active->bf_len > 0) {
975         if (ioctl(ppp_fd, PPPIOCSACTIVE, active) < 0) {
976             syslog(LOG_ERR, "Couldn't set active-filter in kernel: %m");
977             ret = 0;
978         }
979     }
980     return ret;
981 }
982 #endif
983
984 /*
985  * sifvjcomp - config tcp header compression
986  */
987 int
988 sifvjcomp(u, vjcomp, cidcomp, maxcid)
989     int u, vjcomp, cidcomp, maxcid;
990 {
991     u_int x;
992
993     if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
994         syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
995         return 0;
996     }
997     x = vjcomp ? x | SC_COMP_TCP: x &~ SC_COMP_TCP;
998     x = cidcomp? x & ~SC_NO_TCP_CCID: x | SC_NO_TCP_CCID;
999     if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
1000         syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
1001         return 0;
1002     }
1003     if (vjcomp && ioctl(ppp_fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0) {
1004         syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
1005         return 0;
1006     }
1007     return 1;
1008 }
1009
1010 /*
1011  * sifup - Config the interface up and enable IP packets to pass.
1012  */
1013 int
1014 sifup(u)
1015     int u;
1016 {
1017     struct ifreq ifr;
1018
1019     strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
1020     if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
1021         syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
1022         return 0;
1023     }
1024     ifr.ifr_flags |= IFF_UP;
1025     if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
1026         syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
1027         return 0;
1028     }
1029     if_is_up = 1;
1030     return 1;
1031 }
1032
1033 /*
1034  * sifnpmode - Set the mode for handling packets for a given NP.
1035  */
1036 int
1037 sifnpmode(u, proto, mode)
1038     int u;
1039     int proto;
1040     enum NPmode mode;
1041 {
1042     struct npioctl npi;
1043
1044     npi.protocol = proto;
1045     npi.mode = mode;
1046     if (ioctl(ppp_fd, PPPIOCSNPMODE, &npi) < 0) {
1047         syslog(LOG_ERR, "ioctl(set NP %d mode to %d): %m", proto, mode);
1048         return 0;
1049     }
1050     return 1;
1051 }
1052
1053 /*
1054  * sifdown - Config the interface down and disable IP.
1055  */
1056 int
1057 sifdown(u)
1058     int u;
1059 {
1060     struct ifreq ifr;
1061     int rv;
1062     struct npioctl npi;
1063
1064     rv = 1;
1065     npi.protocol = PPP_IP;
1066     npi.mode = NPMODE_ERROR;
1067     ioctl(ppp_fd, PPPIOCSNPMODE, (caddr_t) &npi);
1068     /* ignore errors, because ppp_fd might have been closed by now. */
1069
1070     strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
1071     if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
1072         syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
1073         rv = 0;
1074     } else {
1075         ifr.ifr_flags &= ~IFF_UP;
1076         if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
1077             syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
1078             rv = 0;
1079         } else
1080             if_is_up = 0;
1081     }
1082     return rv;
1083 }
1084
1085 /*
1086  * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
1087  * if it exists.
1088  */
1089 #define SET_SA_FAMILY(addr, family)             \
1090     BZERO((char *) &(addr), sizeof(addr));      \
1091     addr.sa_family = (family);                  \
1092     addr.sa_len = sizeof(addr);
1093
1094 /*
1095  * sifaddr - Config the interface IP addresses and netmask.
1096  */
1097 int
1098 sifaddr(u, o, h, m)
1099     int u;
1100     u_int32_t o, h, m;
1101 {
1102     struct ifaliasreq ifra;
1103     struct ifreq ifr;
1104
1105     strncpy(ifra.ifra_name, ifname, sizeof(ifra.ifra_name));
1106     SET_SA_FAMILY(ifra.ifra_addr, AF_INET);
1107     ((struct sockaddr_in *) &ifra.ifra_addr)->sin_addr.s_addr = o;
1108     SET_SA_FAMILY(ifra.ifra_broadaddr, AF_INET);
1109     ((struct sockaddr_in *) &ifra.ifra_broadaddr)->sin_addr.s_addr = h;
1110     if (m != 0) {
1111         SET_SA_FAMILY(ifra.ifra_mask, AF_INET);
1112         ((struct sockaddr_in *) &ifra.ifra_mask)->sin_addr.s_addr = m;
1113     } else
1114         BZERO(&ifra.ifra_mask, sizeof(ifra.ifra_mask));
1115     BZERO(&ifr, sizeof(ifr));
1116     strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
1117     if (ioctl(sockfd, SIOCDIFADDR, (caddr_t) &ifr) < 0) {
1118         if (errno != EADDRNOTAVAIL)
1119             syslog(LOG_WARNING, "Couldn't remove interface address: %m");
1120     }
1121     if (ioctl(sockfd, SIOCAIFADDR, (caddr_t) &ifra) < 0) {
1122         if (errno != EEXIST) {
1123             syslog(LOG_ERR, "Couldn't set interface address: %m");
1124             return 0;
1125         }
1126         syslog(LOG_WARNING,
1127                "Couldn't set interface address: Address %s already exists",
1128                 ip_ntoa(o));
1129     }
1130     ifaddrs[0] = o;
1131     ifaddrs[1] = h;
1132     return 1;
1133 }
1134
1135 /*
1136  * cifaddr - Clear the interface IP addresses, and delete routes
1137  * through the interface if possible.
1138  */
1139 int
1140 cifaddr(u, o, h)
1141     int u;
1142     u_int32_t o, h;
1143 {
1144     struct ifaliasreq ifra;
1145
1146     ifaddrs[0] = 0;
1147     strncpy(ifra.ifra_name, ifname, sizeof(ifra.ifra_name));
1148     SET_SA_FAMILY(ifra.ifra_addr, AF_INET);
1149     ((struct sockaddr_in *) &ifra.ifra_addr)->sin_addr.s_addr = o;
1150     SET_SA_FAMILY(ifra.ifra_broadaddr, AF_INET);
1151     ((struct sockaddr_in *) &ifra.ifra_broadaddr)->sin_addr.s_addr = h;
1152     BZERO(&ifra.ifra_mask, sizeof(ifra.ifra_mask));
1153     if (ioctl(sockfd, SIOCDIFADDR, (caddr_t) &ifra) < 0) {
1154         if (errno != EADDRNOTAVAIL)
1155             syslog(LOG_WARNING, "Couldn't delete interface address: %m");
1156         return 0;
1157     }
1158     return 1;
1159 }
1160
1161 /*
1162  * sifdefaultroute - assign a default route through the address given.
1163  */
1164 int
1165 sifdefaultroute(u, l, g)
1166     int u;
1167     u_int32_t l, g;
1168 {
1169     return dodefaultroute(g, 's');
1170 }
1171
1172 /*
1173  * cifdefaultroute - delete a default route through the address given.
1174  */
1175 int
1176 cifdefaultroute(u, l, g)
1177     int u;
1178     u_int32_t l, g;
1179 {
1180     return dodefaultroute(g, 'c');
1181 }
1182
1183 /*
1184  * dodefaultroute - talk to a routing socket to add/delete a default route.
1185  */
1186 static int
1187 dodefaultroute(g, cmd)
1188     u_int32_t g;
1189     int cmd;
1190 {
1191     int routes;
1192     struct {
1193         struct rt_msghdr        hdr;
1194         struct sockaddr_in      dst;
1195         struct sockaddr_in      gway;
1196         struct sockaddr_in      mask;
1197     } rtmsg;
1198
1199     if ((routes = socket(PF_ROUTE, SOCK_RAW, AF_INET)) < 0) {
1200         syslog(LOG_ERR, "Couldn't %s default route: socket: %m",
1201                cmd=='s'? "add": "delete");
1202         return 0;
1203     }
1204
1205     memset(&rtmsg, 0, sizeof(rtmsg));
1206     rtmsg.hdr.rtm_type = cmd == 's'? RTM_ADD: RTM_DELETE;
1207     rtmsg.hdr.rtm_flags = RTF_UP | RTF_GATEWAY | RTF_STATIC;
1208     rtmsg.hdr.rtm_version = RTM_VERSION;
1209     rtmsg.hdr.rtm_seq = ++rtm_seq;
1210     rtmsg.hdr.rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
1211     rtmsg.dst.sin_len = sizeof(rtmsg.dst);
1212     rtmsg.dst.sin_family = AF_INET;
1213     rtmsg.gway.sin_len = sizeof(rtmsg.gway);
1214     rtmsg.gway.sin_family = AF_INET;
1215     rtmsg.gway.sin_addr.s_addr = g;
1216     rtmsg.mask.sin_len = sizeof(rtmsg.dst);
1217     rtmsg.mask.sin_family = AF_INET;
1218
1219     rtmsg.hdr.rtm_msglen = sizeof(rtmsg);
1220     if (write(routes, &rtmsg, sizeof(rtmsg)) < 0) {
1221         syslog(LOG_ERR, "Couldn't %s default route: %m",
1222                cmd=='s'? "add": "delete");
1223         close(routes);
1224         return 0;
1225     }
1226
1227     close(routes);
1228     default_route_gateway = (cmd == 's')? g: 0;
1229     return 1;
1230 }
1231
1232 #if RTM_VERSION >= 3
1233
1234 /*
1235  * sifproxyarp - Make a proxy ARP entry for the peer.
1236  */
1237 static struct {
1238     struct rt_msghdr            hdr;
1239     struct sockaddr_inarp       dst;
1240     struct sockaddr_dl          hwa;
1241     char                        extra[128];
1242 } arpmsg;
1243
1244 static int arpmsg_valid;
1245
1246 int
1247 sifproxyarp(unit, hisaddr)
1248     int unit;
1249     u_int32_t hisaddr;
1250 {
1251     int routes;
1252
1253     /*
1254      * Get the hardware address of an interface on the same subnet
1255      * as our local address.
1256      */
1257     memset(&arpmsg, 0, sizeof(arpmsg));
1258     if (!get_ether_addr(hisaddr, &arpmsg.hwa)) {
1259         syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
1260         return 0;
1261     }
1262
1263     if ((routes = socket(PF_ROUTE, SOCK_RAW, AF_INET)) < 0) {
1264         syslog(LOG_ERR, "Couldn't add proxy arp entry: socket: %m");
1265         return 0;
1266     }
1267
1268     arpmsg.hdr.rtm_type = RTM_ADD;
1269     arpmsg.hdr.rtm_flags = RTF_ANNOUNCE | RTF_HOST | RTF_STATIC;
1270     arpmsg.hdr.rtm_version = RTM_VERSION;
1271     arpmsg.hdr.rtm_seq = ++rtm_seq;
1272     arpmsg.hdr.rtm_addrs = RTA_DST | RTA_GATEWAY;
1273     arpmsg.hdr.rtm_inits = RTV_EXPIRE;
1274     arpmsg.dst.sin_len = sizeof(struct sockaddr_inarp);
1275     arpmsg.dst.sin_family = AF_INET;
1276     arpmsg.dst.sin_addr.s_addr = hisaddr;
1277     arpmsg.dst.sin_other = SIN_PROXY;
1278
1279     arpmsg.hdr.rtm_msglen = (char *) &arpmsg.hwa - (char *) &arpmsg
1280         + arpmsg.hwa.sdl_len;
1281     if (write(routes, &arpmsg, arpmsg.hdr.rtm_msglen) < 0) {
1282         syslog(LOG_ERR, "Couldn't add proxy arp entry: %m");
1283         close(routes);
1284         return 0;
1285     }
1286
1287     close(routes);
1288     arpmsg_valid = 1;
1289     proxy_arp_addr = hisaddr;
1290     return 1;
1291 }
1292
1293 /*
1294  * cifproxyarp - Delete the proxy ARP entry for the peer.
1295  */
1296 int
1297 cifproxyarp(unit, hisaddr)
1298     int unit;
1299     u_int32_t hisaddr;
1300 {
1301     int routes;
1302
1303     if (!arpmsg_valid)
1304         return 0;
1305     arpmsg_valid = 0;
1306
1307     arpmsg.hdr.rtm_type = RTM_DELETE;
1308     arpmsg.hdr.rtm_seq = ++rtm_seq;
1309
1310     if ((routes = socket(PF_ROUTE, SOCK_RAW, AF_INET)) < 0) {
1311         syslog(LOG_ERR, "Couldn't delete proxy arp entry: socket: %m");
1312         return 0;
1313     }
1314
1315     if (write(routes, &arpmsg, arpmsg.hdr.rtm_msglen) < 0) {
1316         syslog(LOG_ERR, "Couldn't delete proxy arp entry: %m");
1317         close(routes);
1318         return 0;
1319     }
1320
1321     close(routes);
1322     proxy_arp_addr = 0;
1323     return 1;
1324 }
1325
1326 #else   /* RTM_VERSION */
1327
1328 /*
1329  * sifproxyarp - Make a proxy ARP entry for the peer.
1330  */
1331 int
1332 sifproxyarp(unit, hisaddr)
1333     int unit;
1334     u_int32_t hisaddr;
1335 {
1336     struct arpreq arpreq;
1337     struct {
1338         struct sockaddr_dl      sdl;
1339         char                    space[128];
1340     } dls;
1341
1342     BZERO(&arpreq, sizeof(arpreq));
1343
1344     /*
1345      * Get the hardware address of an interface on the same subnet
1346      * as our local address.
1347      */
1348     if (!get_ether_addr(hisaddr, &dls.sdl)) {
1349         syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
1350         return 0;
1351     }
1352
1353     arpreq.arp_ha.sa_len = sizeof(struct sockaddr);
1354     arpreq.arp_ha.sa_family = AF_UNSPEC;
1355     BCOPY(LLADDR(&dls.sdl), arpreq.arp_ha.sa_data, dls.sdl.sdl_alen);
1356     SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1357     ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1358     arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1359     if (ioctl(sockfd, SIOCSARP, (caddr_t)&arpreq) < 0) {
1360         syslog(LOG_ERR, "Couldn't add proxy arp entry: %m");
1361         return 0;
1362     }
1363
1364     proxy_arp_addr = hisaddr;
1365     return 1;
1366 }
1367
1368 /*
1369  * cifproxyarp - Delete the proxy ARP entry for the peer.
1370  */
1371 int
1372 cifproxyarp(unit, hisaddr)
1373     int unit;
1374     u_int32_t hisaddr;
1375 {
1376     struct arpreq arpreq;
1377
1378     BZERO(&arpreq, sizeof(arpreq));
1379     SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1380     ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1381     if (ioctl(sockfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
1382         syslog(LOG_WARNING, "Couldn't delete proxy arp entry: %m");
1383         return 0;
1384     }
1385     proxy_arp_addr = 0;
1386     return 1;
1387 }
1388 #endif  /* RTM_VERSION */
1389
1390 #ifdef IPX_CHANGE
1391 /********************************************************************
1392  *
1393  * sipxfaddr - Config the interface IPX networknumber
1394  */
1395
1396 int
1397 sipxfaddr (int unit, unsigned long int network, unsigned char * node )
1398   {
1399     int    result = 1;
1400
1401     int    skfd; 
1402     struct sockaddr_ipx  ipx_addr;
1403     struct ifreq         ifr;
1404     struct sockaddr_ipx *sipx = (struct sockaddr_ipx *) &ifr.ifr_addr;
1405     union ipx_net_u net;
1406
1407     skfd = socket (AF_IPX, SOCK_DGRAM, 0);
1408     if (skfd < 0)
1409       { 
1410         syslog (LOG_DEBUG, "socket(AF_IPX): %m(%d)", errno);
1411         result = 0;
1412       }
1413     else
1414       {
1415         memset (&ifr, '\0', sizeof (ifr));
1416         strcpy (ifr.ifr_name, ifname);
1417
1418         memcpy (sipx->sipx_addr.x_host.c_host, node, 6);
1419         sipx->sipx_len     = sizeof(sipx);
1420         sipx->sipx_family  = AF_IPX;
1421         sipx->sipx_port    = 0;
1422         memset(&net, 0, sizeof(net));
1423         net.long_e = htonl (network);
1424         sipx->sipx_addr.x_net = net.net_e;
1425 /*
1426  *  Set the IPX device
1427  */
1428         if (ioctl(skfd, SIOCSIFADDR, (caddr_t) &ifr) < 0)
1429           {
1430             result = 0;
1431             if (errno != EEXIST)
1432               {
1433                 syslog (LOG_DEBUG,
1434                             "ioctl(SIOCAIFADDR, CRTITF): %m(%d)", errno);
1435               }
1436             else
1437               {
1438                 syslog (LOG_WARNING,
1439                         "ioctl(SIOCAIFADDR, CRTITF): Address already exists");
1440               }
1441           }
1442         close (skfd);
1443       }
1444     return result;
1445   }
1446
1447 /********************************************************************
1448  *
1449  * cipxfaddr - Clear the information for the IPX network. The IPX routes
1450  *             are removed and the device is no longer able to pass IPX
1451  *             frames.
1452  */
1453
1454 int cipxfaddr (int unit)
1455   {
1456     int    result = 1;
1457
1458     int    skfd; 
1459     struct sockaddr_ipx  ipx_addr;
1460     struct ifreq         ifr;
1461     struct sockaddr_ipx *sipx = (struct sockaddr_ipx *) &ifr.ifr_addr;
1462
1463     skfd = socket (AF_IPX, SOCK_DGRAM, 0);
1464     if (skfd < 0)
1465       { 
1466         syslog (LOG_DEBUG, "socket(AF_IPX): %m(%d)", errno);
1467         result = 0;
1468       }
1469     else
1470       {
1471         memset (&ifr, '\0', sizeof (ifr));
1472         strcpy (ifr.ifr_name, ifname);
1473
1474         sipx->sipx_len     = sizeof(sipx);
1475         sipx->sipx_family  = AF_IPX;
1476 /*
1477  *  Set the IPX device
1478  */
1479         if (ioctl(skfd, SIOCSIFADDR, (caddr_t) &ifr) < 0)
1480           {
1481             syslog (LOG_INFO,
1482                         "ioctl(SIOCAIFADDR, IPX_DLTITF): %m(%d)", errno);
1483             result = 0;
1484           }
1485         close (skfd);
1486       }
1487     return result;
1488   }
1489 #endif
1490
1491 /*
1492  * get_ether_addr - get the hardware address of an interface on the
1493  * the same subnet as ipaddr.
1494  */
1495 static int
1496 get_ether_addr(ipaddr, hwaddr)
1497     u_int32_t ipaddr;
1498     struct sockaddr_dl *hwaddr;
1499 {
1500     u_int32_t ina, mask;
1501     struct sockaddr_dl *dla;
1502     struct ifaddrs *ifap, *ifa, *ifp;
1503
1504     /*
1505      * Scan through looking for an interface with an Internet
1506      * address on the same subnet as `ipaddr'.
1507      */
1508     if (getifaddrs(&ifap) != 0) {
1509         syslog(LOG_ERR, "getifaddrs: %m");
1510         return 0;
1511     }
1512     for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
1513         if (ifa->ifa_addr->sa_family != AF_INET)
1514             continue;
1515         ina = ((struct sockaddr_in *) ifa->ifa_addr)->sin_addr.s_addr;
1516         /*
1517          * Check that the interface is up, and not point-to-point
1518          * or loopback.
1519          */
1520         if ((ifa->ifa_flags &
1521             (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
1522             != (IFF_UP|IFF_BROADCAST))
1523                 continue;
1524         /*
1525          * Get its netmask and check that it's on the right subnet.
1526          */
1527         mask = ((struct sockaddr_in *) ifa->ifa_netmask)->sin_addr.s_addr;
1528         if ((ipaddr & mask) != (ina & mask))
1529                 continue;
1530         break;
1531     }
1532     if (!ifa) {
1533         freeifaddrs(ifap);
1534         return 0;
1535     }
1536     syslog(LOG_INFO, "found interface %s for proxy arp", ifa->ifa_name);
1537
1538     /*
1539      * Now scan through again looking for a link-level address
1540      * for this interface.
1541      */
1542     ifp = ifa;
1543     for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
1544         if (strcmp(ifp->ifa_name, ifa->ifa_name) != 0)
1545             continue;
1546         if (ifa->ifa_addr->sa_family != AF_LINK)
1547             continue;
1548         /*
1549          * Found the link-level address - copy it out
1550          */
1551         dla = (struct sockaddr_dl *) ifa->ifa_addr;
1552         BCOPY(dla, hwaddr, dla->sdl_len);
1553         freeifaddrs(ifap);
1554         return 1;
1555     }
1556
1557     freeifaddrs(ifap);
1558     return 0;
1559 }
1560
1561 /*
1562  * Return user specified netmask, modified by any mask we might determine
1563  * for address `addr' (in network byte order).
1564  * Here we scan through the system's list of interfaces, looking for
1565  * any non-point-to-point interfaces which might appear to be on the same
1566  * network as `addr'.  If we find any, we OR in their netmask to the
1567  * user-specified netmask.
1568  */
1569 u_int32_t
1570 GetMask(addr)
1571     u_int32_t addr;
1572 {
1573     u_int32_t mask, nmask, ina;
1574     struct ifaddrs *ifap, *ifa;
1575
1576     addr = ntohl(addr);
1577     if (IN_CLASSA(addr))        /* determine network mask for address class */
1578         nmask = IN_CLASSA_NET;
1579     else if (IN_CLASSB(addr))
1580         nmask = IN_CLASSB_NET;
1581     else
1582         nmask = IN_CLASSC_NET;
1583     /* class D nets are disallowed by bad_ip_adrs */
1584     mask = netmask | htonl(nmask);
1585
1586     /*
1587      * Scan through the system's network interfaces.
1588      */
1589     if (getifaddrs(&ifap) != 0) {
1590         syslog(LOG_WARNING, "getifaddrs: %m");
1591         return mask;
1592     }
1593     for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
1594         /*
1595          * Check the interface's internet address.
1596          */
1597         if (ifa->ifa_addr->sa_family != AF_INET)
1598             continue;
1599         ina = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
1600         if ((ntohl(ina) & nmask) != (addr & nmask))
1601             continue;
1602         /*
1603          * Check that the interface is up, and not point-to-point or loopback.
1604          */
1605         if ((ifa->ifa_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK)) != IFF_UP)
1606             continue;
1607         /*
1608          * Get its netmask and OR it into our mask.
1609          */
1610         mask |= ((struct sockaddr_in *)&ifa->ifa_netmask)->sin_addr.s_addr;
1611     }
1612
1613     freeifaddrs(ifap);
1614     return mask;
1615 }
1616
1617 /*
1618  * Use the hostid as part of the random number seed.
1619  */
1620 int
1621 get_host_seed()
1622 {
1623     return gethostid();
1624 }
1625
1626 /*
1627  * lock - create a lock file for the named lock device
1628  */
1629 #define LOCK_PREFIX     "/var/spool/lock/LCK.."
1630
1631 int
1632 lock(dev)
1633     char *dev;
1634 {
1635     char hdb_lock_buffer[12];
1636     int fd, pid, n;
1637     char *p;
1638
1639     if ((p = strrchr(dev, '/')) != NULL)
1640         dev = p + 1;
1641     lock_file = malloc(strlen(LOCK_PREFIX) + strlen(dev) + 1);
1642     if (lock_file == NULL)
1643         novm("lock file name");
1644     strcat(strcpy(lock_file, LOCK_PREFIX), dev);
1645
1646     while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
1647         if (errno == EEXIST
1648             && (fd = open(lock_file, O_RDONLY, 0)) >= 0) {
1649             /* Read the lock file to find out who has the device locked */
1650             n = read(fd, hdb_lock_buffer, 11);
1651             if (n <= 0) {
1652                 syslog(LOG_ERR, "Can't read pid from lock file %s", lock_file);
1653                 close(fd);
1654             } else {
1655                 hdb_lock_buffer[n] = 0;
1656                 pid = atoi(hdb_lock_buffer);
1657                 if (kill(pid, 0) == -1 && errno == ESRCH) {
1658                     /* pid no longer exists - remove the lock file */
1659                     if (unlink(lock_file) == 0) {
1660                         close(fd);
1661                         syslog(LOG_NOTICE, "Removed stale lock on %s (pid %d)",
1662                                dev, pid);
1663                         continue;
1664                     } else
1665                         syslog(LOG_WARNING, "Couldn't remove stale lock on %s",
1666                                dev);
1667                 } else
1668                     syslog(LOG_NOTICE, "Device %s is locked by pid %d",
1669                            dev, pid);
1670             }
1671             close(fd);
1672         } else
1673             syslog(LOG_ERR, "Can't create lock file %s: %m", lock_file);
1674         free(lock_file);
1675         lock_file = NULL;
1676         return -1;
1677     }
1678
1679     sprintf(hdb_lock_buffer, "%10d\n", getpid());
1680     write(fd, hdb_lock_buffer, 11);
1681
1682     close(fd);
1683     return 0;
1684 }
1685
1686 /*
1687  * unlock - remove our lockfile
1688  */
1689 void
1690 unlock()
1691 {
1692     if (lock_file) {
1693         unlink(lock_file);
1694         free(lock_file);
1695         lock_file = NULL;
1696     }
1697 }