]> git.ozlabs.org Git - ppp.git/blob - pppd/sys-bsd.c
fix up prototypes; let sysdep stuff make device non-blocking
[ppp.git] / 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  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms are permitted
9  * provided that the above copyright notice and this paragraph are
10  * duplicated in all such forms and that any documentation,
11  * advertising materials, and other materials related to such
12  * distribution and use acknowledge that the software was developed
13  * by Carnegie Mellon University.  The name of the
14  * University may not be used to endorse or promote products derived
15  * from this software without specific prior written permission.
16  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19  */
20
21 #ifndef lint
22 static char rcsid[] = "$Id: sys-bsd.c,v 1.21 1995/08/16 01:40:23 paulus Exp $";
23 #endif
24
25 /*
26  * TODO:
27  */
28
29 #include <stdio.h>
30 #include <syslog.h>
31 #include <string.h>
32 #include <stdlib.h>
33 #include <errno.h>
34 #include <fcntl.h>
35 #include <termios.h>
36 #include <sys/ioctl.h>
37 #include <sys/types.h>
38 #include <sys/socket.h>
39 #include <sys/time.h>
40 #include <sys/stat.h>
41
42 #include <net/if.h>
43 #include <net/ppp_defs.h>
44 #include <net/if_ppp.h>
45 #include <net/route.h>
46 #include <net/if_dl.h>
47 #include <netinet/in.h>
48
49 #if RTM_VERSION >= 3
50 #include <netinet/if_ether.h>
51 #endif
52
53 #include "pppd.h"
54
55 static int initdisc = -1;       /* Initial TTY discipline */
56 static int rtm_seq;
57
58 static int      restore_term;   /* 1 => we've munged the terminal */
59 static struct termios inittermios; /* Initial TTY termios */
60 static struct winsize wsinfo;   /* Initial window size info */
61
62 static char *lock_file;         /* name of lock file created */
63
64 int sockfd;                     /* socket for doing interface ioctls */
65
66 /*
67  * sys_init - System-dependent initialization.
68  */
69 void
70 sys_init()
71 {
72     openlog("pppd", LOG_PID | LOG_NDELAY, LOG_PPP);
73     setlogmask(LOG_UPTO(LOG_INFO));
74     if (debug)
75         setlogmask(LOG_UPTO(LOG_DEBUG));
76
77     /* Get an internet socket for doing socket ioctl's on. */
78     if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
79         syslog(LOG_ERR, "Couldn't create IP socket: %m");
80         die(1);
81     }
82 }
83
84 /*
85  * note_debug_level - note a change in the debug level.
86  */
87 void
88 note_debug_level()
89 {
90     if (debug) {
91         syslog(LOG_INFO, "Debug turned ON, Level %d", debug);
92         setlogmask(LOG_UPTO(LOG_DEBUG));
93     } else {
94         setlogmask(LOG_UPTO(LOG_WARNING));
95     }
96 }
97
98 /*
99  * ppp_available - check whether the system has any ppp interfaces
100  * (in fact we check whether we can do an ioctl on ppp0).
101  */
102 int
103 ppp_available()
104 {
105     int s, ok;
106     struct ifreq ifr;
107     extern char *no_ppp_msg;
108
109     if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
110         return 1;               /* can't tell */
111
112     strncpy(ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
113     ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
114     close(s);
115
116     no_ppp_msg = "\
117 This system lacks kernel support for PPP.  To include PPP support\n\
118 in the kernel, please follow the steps detailed in the README.bsd\n\
119 file in the ppp-2.2 distribution.\n";
120     return ok;
121 }
122
123 /*
124  * establish_ppp - Turn the serial port into a ppp interface.
125  */
126 void
127 establish_ppp()
128 {
129     int pppdisc = PPPDISC;
130     int x;
131
132     if (ioctl(fd, TIOCGETD, &initdisc) < 0) {
133         syslog(LOG_ERR, "ioctl(TIOCGETD): %m");
134         die(1);
135     }
136     if (ioctl(fd, TIOCSETD, &pppdisc) < 0) {
137         syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
138         die(1);
139     }
140
141     /*
142      * Find out which interface we were given.
143      */
144     if (ioctl(fd, PPPIOCGUNIT, &ifunit) < 0) {  
145         syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m");
146         die(1);
147     }
148
149     /*
150      * Enable debug in the driver if requested.
151      */
152     if (kdebugflag) {
153         if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
154             syslog(LOG_WARNING, "ioctl (PPPIOCGFLAGS): %m");
155         } else {
156             x |= (kdebugflag & 0xFF) * SC_DEBUG;
157             if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
158                 syslog(LOG_WARNING, "ioctl(PPPIOCSFLAGS): %m");
159         }
160     }
161 }
162
163
164 /*
165  * disestablish_ppp - Restore the serial port to normal operation.
166  * This shouldn't call die() because it's called from die().
167  */
168 void
169 disestablish_ppp()
170 {
171     int x;
172     char *s;
173
174     if (initdisc >= 0) {
175         /*
176          * Check whether the link seems not to be 8-bit clean.
177          */
178         if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) == 0) {
179             s = NULL;
180             switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP)) {
181             case SC_RCV_B7_0:
182                 s = "bit 7 set to 1";
183                 break;
184             case SC_RCV_B7_1:
185                 s = "bit 7 set to 0";
186                 break;
187             case SC_RCV_EVNP:
188                 s = "odd parity";
189                 break;
190             case SC_RCV_ODDP:
191                 s = "even parity";
192                 break;
193             }
194             if (s != NULL) {
195                 syslog(LOG_WARNING, "Serial link is not 8-bit clean:");
196                 syslog(LOG_WARNING, "All received characters had %s", s);
197             }
198         }
199         if (ioctl(fd, TIOCSETD, &initdisc) < 0)
200             syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
201     }
202 }
203
204
205 /*
206  * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
207  * at the requested speed, etc.  If `local' is true, set CLOCAL
208  * regardless of whether the modem option was specified.
209  *
210  * For *BSD, we assume that speed_t values numerically equal bits/second.
211  */
212 set_up_tty(fd, local)
213     int fd, local;
214 {
215     struct termios tios;
216
217     if (tcgetattr(fd, &tios) < 0) {
218         syslog(LOG_ERR, "tcgetattr: %m");
219         die(1);
220     }
221
222     if (!restore_term) {
223         inittermios = tios;
224         ioctl(fd, TIOCGWINSZ, &wsinfo);
225     }
226
227     tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
228     if (crtscts > 0)
229         tios.c_cflag |= CRTSCTS;
230     else if (crtscts < 0)
231         tios.c_cflag &= ~CRTSCTS;
232
233     tios.c_cflag |= CS8 | CREAD | HUPCL;
234     if (local || !modem)
235         tios.c_cflag |= CLOCAL;
236     tios.c_iflag = IGNBRK | IGNPAR;
237     tios.c_oflag = 0;
238     tios.c_lflag = 0;
239     tios.c_cc[VMIN] = 1;
240     tios.c_cc[VTIME] = 0;
241
242     if (crtscts == 2) {
243         tios.c_iflag |= IXOFF;
244         tios.c_cc[VSTOP] = 0x13;        /* DC3 = XOFF = ^S */
245         tios.c_cc[VSTART] = 0x11;       /* DC1 = XON  = ^Q */
246     }
247
248     if (inspeed) {
249         cfsetospeed(&tios, inspeed);
250         cfsetispeed(&tios, inspeed);
251     } else {
252         inspeed = cfgetospeed(&tios);
253         /*
254          * We can't proceed if the serial port speed is 0,
255          * since that implies that the serial port is disabled.
256          */
257         if (inspeed == 0) {
258             syslog(LOG_ERR, "Baud rate for %s is 0; need explicit baud rate",
259                    devnam);
260             die(1);
261         }
262     }
263     baud_rate = inspeed;
264
265     if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
266         syslog(LOG_ERR, "tcsetattr: %m");
267         die(1);
268     }
269
270     restore_term = 1;
271 }
272
273 /*
274  * restore_tty - restore the terminal to the saved settings.
275  */
276 void
277 restore_tty()
278 {
279     if (restore_term) {
280         if (!default_device) {
281             /*
282              * Turn off echoing, because otherwise we can get into
283              * a loop with the tty and the modem echoing to each other.
284              * We presume we are the sole user of this tty device, so
285              * when we close it, it will revert to its defaults anyway.
286              */
287             inittermios.c_lflag &= ~(ECHO | ECHONL);
288         }
289         if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
290             if (errno != ENXIO)
291                 syslog(LOG_WARNING, "tcsetattr: %m");
292         ioctl(fd, TIOCSWINSZ, &wsinfo);
293         restore_term = 0;
294     }
295 }
296
297 /*
298  * setdtr - control the DTR line on the serial port.
299  * This is called from die(), so it shouldn't call die().
300  */
301 setdtr(fd, on)
302 int fd, on;
303 {
304     int modembits = TIOCM_DTR;
305
306     ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
307 }
308
309
310 /*
311  * output - Output PPP packet.
312  */
313 void
314 output(unit, p, len)
315     int unit;
316     u_char *p;
317     int len;
318 {
319     if (unit != 0)
320         MAINDEBUG((LOG_WARNING, "output: unit != 0!"));
321     if (debug)
322         log_packet(p, len, "sent ");
323
324     if (write(fd, p, len) < 0) {
325         syslog(LOG_ERR, "write: %m");
326         die(1);
327     }
328 }
329
330
331 /*
332  * wait_input - wait until there is data available on fd,
333  * for the length of time specified by *timo (indefinite
334  * if timo is NULL).
335  */
336 wait_input(timo)
337     struct timeval *timo;
338 {
339     fd_set ready;
340     int n;
341
342     FD_ZERO(&ready);
343     FD_SET(fd, &ready);
344     n = select(fd+1, &ready, NULL, &ready, timo);
345     if (n < 0 && errno != EINTR) {
346         syslog(LOG_ERR, "select: %m");
347         die(1);
348     }
349 }
350
351
352 /*
353  * read_packet - get a PPP packet from the serial device.
354  */
355 int
356 read_packet(buf)
357     u_char *buf;
358 {
359     int len;
360
361     if ((len = read(fd, buf, PPP_MTU + PPP_HDRLEN)) < 0) {
362         if (errno == EWOULDBLOCK || errno == EINTR)
363             return -1;
364         syslog(LOG_ERR, "read(fd): %m");
365         die(1);
366     }
367     return len;
368 }
369
370
371 /*
372  * ppp_send_config - configure the transmit characteristics of
373  * the ppp interface.
374  */
375 void
376 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
377     int unit, mtu;
378     u_int32_t asyncmap;
379     int pcomp, accomp;
380 {
381     u_int x;
382     struct ifreq ifr;
383
384     strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
385     ifr.ifr_mtu = mtu;
386     if (ioctl(sockfd, SIOCSIFMTU, (caddr_t) &ifr) < 0) {
387         syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m");
388         quit();
389     }
390
391     if (ioctl(fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0) {
392         syslog(LOG_ERR, "ioctl(PPPIOCSASYNCMAP): %m");
393         quit();
394     }
395
396     if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
397         syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
398         quit();
399     }
400     x = pcomp? x | SC_COMP_PROT: x &~ SC_COMP_PROT;
401     x = accomp? x | SC_COMP_AC: x &~ SC_COMP_AC;
402     if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
403         syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
404         quit();
405     }
406 }
407
408
409 /*
410  * ppp_set_xaccm - set the extended transmit ACCM for the interface.
411  */
412 void
413 ppp_set_xaccm(unit, accm)
414     int unit;
415     ext_accm accm;
416 {
417     if (ioctl(fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY)
418         syslog(LOG_WARNING, "ioctl(set extended ACCM): %m");
419 }
420
421
422 /*
423  * ppp_recv_config - configure the receive-side characteristics of
424  * the ppp interface.
425  */
426 void
427 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
428     int unit, mru;
429     u_int32_t asyncmap;
430     int pcomp, accomp;
431 {
432     int x;
433
434     if (ioctl(fd, PPPIOCSMRU, (caddr_t) &mru) < 0) {
435         syslog(LOG_ERR, "ioctl(PPPIOCSMRU): %m");
436         quit();
437     }
438     if (ioctl(fd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0) {
439         syslog(LOG_ERR, "ioctl(PPPIOCSRASYNCMAP): %m");
440         quit();
441     }
442     if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
443         syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
444         quit();
445     }
446     x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC;
447     if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
448         syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
449         quit();
450     }
451 }
452
453 /*
454  * ccp_test - ask kernel whether a given compression method
455  * is acceptable for use.
456  */
457 ccp_test(unit, opt_ptr, opt_len, for_transmit)
458     int unit, opt_len, for_transmit;
459     u_char *opt_ptr;
460 {
461     struct ppp_option_data data;
462
463     data.ptr = opt_ptr;
464     data.length = opt_len;
465     data.transmit = for_transmit;
466     return ioctl(fd, PPPIOCSCOMPRESS, (caddr_t) &data) >= 0;
467 }
468
469 /*
470  * ccp_flags_set - inform kernel about the current state of CCP.
471  */
472 void
473 ccp_flags_set(unit, isopen, isup)
474     int unit, isopen, isup;
475 {
476     int x;
477
478     if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
479         syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
480         return;
481     }
482     x = isopen? x | SC_CCP_OPEN: x &~ SC_CCP_OPEN;
483     x = isup? x | SC_CCP_UP: x &~ SC_CCP_UP;
484     if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
485         syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
486 }
487
488 /*
489  * ccp_fatal_error - returns 1 if decompression was disabled as a
490  * result of an error detected after decompression of a packet,
491  * 0 otherwise.  This is necessary because of patent nonsense.
492  */
493 int
494 ccp_fatal_error(unit)
495     int unit;
496 {
497     int x;
498
499     if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
500         syslog(LOG_ERR, "ioctl(PPPIOCGFLAGS): %m");
501         return 0;
502     }
503     return x & SC_DC_FERROR;
504 }
505
506 /*
507  * sifvjcomp - config tcp header compression
508  */
509 int
510 sifvjcomp(u, vjcomp, cidcomp, maxcid)
511     int u, vjcomp, cidcomp, maxcid;
512 {
513     u_int x;
514
515     if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
516         syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
517         return 0;
518     }
519     x = vjcomp ? x | SC_COMP_TCP: x &~ SC_COMP_TCP;
520     x = cidcomp? x & ~SC_NO_TCP_CCID: x | SC_NO_TCP_CCID;
521     if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
522         syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
523         return 0;
524     }
525     if (ioctl(fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0) {
526         syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
527         return 0;
528     }
529     return 1;
530 }
531
532 /*
533  * sifup - Config the interface up and enable IP packets to pass.
534  */
535 #ifndef SC_ENABLE_IP
536 #define SC_ENABLE_IP    0x100   /* compat for old versions of kernel code */
537 #endif
538
539 int
540 sifup(u)
541     int u;
542 {
543     struct ifreq ifr;
544     u_int x;
545     struct npioctl npi;
546
547     strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
548     if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
549         syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
550         return 0;
551     }
552     ifr.ifr_flags |= IFF_UP;
553     if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
554         syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
555         return 0;
556     }
557     npi.protocol = PPP_IP;
558     npi.mode = NPMODE_PASS;
559     if (ioctl(fd, PPPIOCSNPMODE, &npi) < 0) {
560         if (errno != ENOTTY) {
561             syslog(LOG_ERR, "ioctl(PPPIOCSNPMODE): %m");
562             return 0;
563         }
564         /* for backwards compatibility */
565         if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
566             syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
567             return 0;
568         }
569         x |= SC_ENABLE_IP;
570         if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
571             syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
572             return 0;
573         }
574     }
575     return 1;
576 }
577
578 /*
579  * sifdown - Config the interface down and disable IP.
580  */
581 int
582 sifdown(u)
583     int u;
584 {
585     struct ifreq ifr;
586     u_int x;
587     int rv;
588     struct npioctl npi;
589
590     rv = 1;
591     npi.protocol = PPP_IP;
592     npi.mode = NPMODE_ERROR;
593     if (ioctl(fd, PPPIOCSNPMODE, (caddr_t) &npi) < 0) {
594         if (errno != ENOTTY) {
595             syslog(LOG_ERR, "ioctl(PPPIOCSNPMODE): %m");
596             rv = 0;
597         } else {
598             /* backwards compatibility */
599             if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
600                 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
601                 rv = 0;
602             } else {
603                 x &= ~SC_ENABLE_IP;
604                 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
605                     syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
606                     rv = 0;
607                 }
608             }
609         }
610     }
611
612     strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
613     if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
614         syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
615         rv = 0;
616     } else {
617         ifr.ifr_flags &= ~IFF_UP;
618         if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
619             syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
620             rv = 0;
621         }
622     }
623     return rv;
624 }
625
626 /*
627  * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
628  * if it exists.
629  */
630 #define SET_SA_FAMILY(addr, family)             \
631     BZERO((char *) &(addr), sizeof(addr));      \
632     addr.sa_family = (family);                  \
633     addr.sa_len = sizeof(addr);
634
635 /*
636  * sifaddr - Config the interface IP addresses and netmask.
637  */
638 int
639 sifaddr(u, o, h, m)
640     int u;
641     u_int32_t o, h, m;
642 {
643     struct ifaliasreq ifra;
644
645     strncpy(ifra.ifra_name, ifname, sizeof(ifra.ifra_name));
646     SET_SA_FAMILY(ifra.ifra_addr, AF_INET);
647     ((struct sockaddr_in *) &ifra.ifra_addr)->sin_addr.s_addr = o;
648     SET_SA_FAMILY(ifra.ifra_broadaddr, AF_INET);
649     ((struct sockaddr_in *) &ifra.ifra_broadaddr)->sin_addr.s_addr = h;
650     if (m != 0) {
651         SET_SA_FAMILY(ifra.ifra_mask, AF_INET);
652         ((struct sockaddr_in *) &ifra.ifra_mask)->sin_addr.s_addr = m;
653     } else
654         BZERO(&ifra.ifra_mask, sizeof(ifra.ifra_mask));
655     if (ioctl(sockfd, SIOCAIFADDR, (caddr_t) &ifra) < 0) {
656         if (errno != EEXIST) {
657             syslog(LOG_ERR, "ioctl(SIOCAIFADDR): %m");
658             return 0;
659         }
660         syslog(LOG_WARNING, "ioctl(SIOCAIFADDR): Address already exists");
661     }
662     return 1;
663 }
664
665 /*
666  * cifaddr - Clear the interface IP addresses, and delete routes
667  * through the interface if possible.
668  */
669 int
670 cifaddr(u, o, h)
671     int u;
672     u_int32_t o, h;
673 {
674     struct ifaliasreq ifra;
675
676     strncpy(ifra.ifra_name, ifname, sizeof(ifra.ifra_name));
677     SET_SA_FAMILY(ifra.ifra_addr, AF_INET);
678     ((struct sockaddr_in *) &ifra.ifra_addr)->sin_addr.s_addr = o;
679     SET_SA_FAMILY(ifra.ifra_broadaddr, AF_INET);
680     ((struct sockaddr_in *) &ifra.ifra_broadaddr)->sin_addr.s_addr = h;
681     BZERO(&ifra.ifra_mask, sizeof(ifra.ifra_mask));
682     if (ioctl(sockfd, SIOCDIFADDR, (caddr_t) &ifra) < 0) {
683         syslog(LOG_WARNING, "ioctl(SIOCDIFADDR): %m");
684         return 0;
685     }
686     return 1;
687 }
688
689 /*
690  * sifdefaultroute - assign a default route through the address given.
691  */
692 int
693 sifdefaultroute(u, g)
694     int u;
695     u_int32_t g;
696 {
697     return dodefaultroute(g, 's');
698 }
699
700 /*
701  * cifdefaultroute - delete a default route through the address given.
702  */
703 int
704 cifdefaultroute(u, g)
705     int u;
706     u_int32_t g;
707 {
708     return dodefaultroute(g, 'c');
709 }
710
711 /*
712  * dodefaultroute - talk to a routing socket to add/delete a default route.
713  */
714 int
715 dodefaultroute(g, cmd)
716     u_int32_t g;
717     int cmd;
718 {
719     int routes;
720     struct {
721         struct rt_msghdr        hdr;
722         struct sockaddr_in      dst;
723         struct sockaddr_in      gway;
724         struct sockaddr_in      mask;
725     } rtmsg;
726
727     if ((routes = socket(PF_ROUTE, SOCK_RAW, AF_INET)) < 0) {
728         syslog(LOG_ERR, "%cifdefaultroute: opening routing socket: %m", cmd);
729         return 0;
730     }
731
732     memset(&rtmsg, 0, sizeof(rtmsg));
733     rtmsg.hdr.rtm_type = cmd == 's'? RTM_ADD: RTM_DELETE;
734     rtmsg.hdr.rtm_flags = RTF_UP | RTF_GATEWAY;
735     rtmsg.hdr.rtm_version = RTM_VERSION;
736     rtmsg.hdr.rtm_seq = ++rtm_seq;
737     rtmsg.hdr.rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
738     rtmsg.dst.sin_len = sizeof(rtmsg.dst);
739     rtmsg.dst.sin_family = AF_INET;
740     rtmsg.gway.sin_len = sizeof(rtmsg.gway);
741     rtmsg.gway.sin_family = AF_INET;
742     rtmsg.gway.sin_addr.s_addr = g;
743     rtmsg.mask.sin_len = sizeof(rtmsg.dst);
744     rtmsg.mask.sin_family = AF_INET;
745
746     rtmsg.hdr.rtm_msglen = sizeof(rtmsg);
747     if (write(routes, &rtmsg, sizeof(rtmsg)) < 0) {
748         syslog(LOG_ERR, "%s default route: %m", cmd=='s'? "add": "delete");
749         close(routes);
750         return 0;
751     }
752
753     close(routes);
754     return 1;
755 }
756
757 #if RTM_VERSION >= 3
758
759 /*
760  * sifproxyarp - Make a proxy ARP entry for the peer.
761  */
762 static struct {
763     struct rt_msghdr            hdr;
764     struct sockaddr_inarp       dst;
765     struct sockaddr_dl          hwa;
766     char                        extra[128];
767 } arpmsg;
768
769 static int arpmsg_valid;
770
771 int
772 sifproxyarp(unit, hisaddr)
773     int unit;
774     u_int32_t hisaddr;
775 {
776     int routes;
777     int l;
778
779     /*
780      * Get the hardware address of an interface on the same subnet
781      * as our local address.
782      */
783     memset(&arpmsg, 0, sizeof(arpmsg));
784     if (!get_ether_addr(hisaddr, &arpmsg.hwa)) {
785         syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
786         return 0;
787     }
788
789     if ((routes = socket(PF_ROUTE, SOCK_RAW, AF_INET)) < 0) {
790         syslog(LOG_ERR, "sifproxyarp: opening routing socket: %m");
791         return 0;
792     }
793
794     arpmsg.hdr.rtm_type = RTM_ADD;
795     arpmsg.hdr.rtm_flags = RTF_ANNOUNCE | RTF_HOST | RTF_STATIC;
796     arpmsg.hdr.rtm_version = RTM_VERSION;
797     arpmsg.hdr.rtm_seq = ++rtm_seq;
798     arpmsg.hdr.rtm_addrs = RTA_DST | RTA_GATEWAY;
799     arpmsg.hdr.rtm_inits = RTV_EXPIRE;
800     arpmsg.dst.sin_len = sizeof(struct sockaddr_inarp);
801     arpmsg.dst.sin_family = AF_INET;
802     arpmsg.dst.sin_addr.s_addr = hisaddr;
803     arpmsg.dst.sin_other = SIN_PROXY;
804
805     arpmsg.hdr.rtm_msglen = (char *) &arpmsg.hwa - (char *) &arpmsg
806         + arpmsg.hwa.sdl_len;
807     if (write(routes, &arpmsg, arpmsg.hdr.rtm_msglen) < 0) {
808         syslog(LOG_ERR, "add proxy arp entry: %m");
809         close(routes);
810         return 0;
811     }
812
813     close(routes);
814     arpmsg_valid = 1;
815     return 1;
816 }
817
818 /*
819  * cifproxyarp - Delete the proxy ARP entry for the peer.
820  */
821 int
822 cifproxyarp(unit, hisaddr)
823     int unit;
824     u_int32_t hisaddr;
825 {
826     int routes;
827
828     if (!arpmsg_valid)
829         return 0;
830     arpmsg_valid = 0;
831
832     arpmsg.hdr.rtm_type = RTM_DELETE;
833     arpmsg.hdr.rtm_seq = ++rtm_seq;
834
835     if ((routes = socket(PF_ROUTE, SOCK_RAW, AF_INET)) < 0) {
836         syslog(LOG_ERR, "sifproxyarp: opening routing socket: %m");
837         return 0;
838     }
839
840     if (write(routes, &arpmsg, arpmsg.hdr.rtm_msglen) < 0) {
841         syslog(LOG_ERR, "delete proxy arp entry: %m");
842         close(routes);
843         return 0;
844     }
845
846     close(routes);
847     return 1;
848 }
849
850 #else   /* RTM_VERSION */
851
852 /*
853  * sifproxyarp - Make a proxy ARP entry for the peer.
854  */
855 int
856 sifproxyarp(unit, hisaddr)
857     int unit;
858     u_int32_t hisaddr;
859 {
860     struct arpreq arpreq;
861     struct {
862         struct sockaddr_dl      sdl;
863         char                    space[128];
864     } dls;
865
866     BZERO(&arpreq, sizeof(arpreq));
867
868     /*
869      * Get the hardware address of an interface on the same subnet
870      * as our local address.
871      */
872     if (!get_ether_addr(hisaddr, &dls.sdl)) {
873         syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
874         return 0;
875     }
876
877     arpreq.arp_ha.sa_len = sizeof(struct sockaddr);
878     arpreq.arp_ha.sa_family = AF_UNSPEC;
879     BCOPY(LLADDR(&dls.sdl), arpreq.arp_ha.sa_data, dls.sdl.sdl_alen);
880     SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
881     ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
882     arpreq.arp_flags = ATF_PERM | ATF_PUBL;
883     if (ioctl(sockfd, SIOCSARP, (caddr_t)&arpreq) < 0) {
884         syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
885         return 0;
886     }
887
888     return 1;
889 }
890
891 /*
892  * cifproxyarp - Delete the proxy ARP entry for the peer.
893  */
894 int
895 cifproxyarp(unit, hisaddr)
896     int unit;
897     u_int32_t hisaddr;
898 {
899     struct arpreq arpreq;
900
901     BZERO(&arpreq, sizeof(arpreq));
902     SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
903     ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
904     if (ioctl(sockfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
905         syslog(LOG_WARNING, "ioctl(SIOCDARP): %m");
906         return 0;
907     }
908     return 1;
909 }
910 #endif  /* RTM_VERSION */
911
912
913 /*
914  * get_ether_addr - get the hardware address of an interface on the
915  * the same subnet as ipaddr.
916  */
917 #define MAX_IFS         32
918
919 int
920 get_ether_addr(ipaddr, hwaddr)
921     u_int32_t ipaddr;
922     struct sockaddr_dl *hwaddr;
923 {
924     struct ifreq *ifr, *ifend, *ifp;
925     u_int32_t ina, mask;
926     struct sockaddr_dl *dla;
927     struct ifreq ifreq;
928     struct ifconf ifc;
929     struct ifreq ifs[MAX_IFS];
930
931     ifc.ifc_len = sizeof(ifs);
932     ifc.ifc_req = ifs;
933     if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
934         syslog(LOG_ERR, "ioctl(SIOCGIFCONF): %m");
935         return 0;
936     }
937
938     /*
939      * Scan through looking for an interface with an Internet
940      * address on the same subnet as `ipaddr'.
941      */
942     ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
943     for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
944                 ((char *)&ifr->ifr_addr + ifr->ifr_addr.sa_len)) {
945         if (ifr->ifr_addr.sa_family == AF_INET) {
946             ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
947             strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
948             /*
949              * Check that the interface is up, and not point-to-point
950              * or loopback.
951              */
952             if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
953                 continue;
954             if ((ifreq.ifr_flags &
955                  (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
956                  != (IFF_UP|IFF_BROADCAST))
957                 continue;
958             /*
959              * Get its netmask and check that it's on the right subnet.
960              */
961             if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
962                 continue;
963             mask = ((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr;
964             if ((ipaddr & mask) != (ina & mask))
965                 continue;
966
967             break;
968         }
969     }
970
971     if (ifr >= ifend)
972         return 0;
973     syslog(LOG_INFO, "found interface %s for proxy arp", ifr->ifr_name);
974
975     /*
976      * Now scan through again looking for a link-level address
977      * for this interface.
978      */
979     ifp = ifr;
980     for (ifr = ifc.ifc_req; ifr < ifend; ) {
981         if (strcmp(ifp->ifr_name, ifr->ifr_name) == 0
982             && ifr->ifr_addr.sa_family == AF_LINK) {
983             /*
984              * Found the link-level address - copy it out
985              */
986             dla = (struct sockaddr_dl *) &ifr->ifr_addr;
987             BCOPY(dla, hwaddr, dla->sdl_len);
988             return 1;
989         }
990         ifr = (struct ifreq *) ((char *)&ifr->ifr_addr + ifr->ifr_addr.sa_len);
991     }
992
993     return 0;
994 }
995
996 /*
997  * Return user specified netmask, modified by any mask we might determine
998  * for address `addr' (in network byte order).
999  * Here we scan through the system's list of interfaces, looking for
1000  * any non-point-to-point interfaces which might appear to be on the same
1001  * network as `addr'.  If we find any, we OR in their netmask to the
1002  * user-specified netmask.
1003  */
1004 u_int32_t
1005 GetMask(addr)
1006     u_int32_t addr;
1007 {
1008     u_int32_t mask, nmask, ina;
1009     struct ifreq *ifr, *ifend, ifreq;
1010     struct ifconf ifc;
1011     struct ifreq ifs[MAX_IFS];
1012
1013     addr = ntohl(addr);
1014     if (IN_CLASSA(addr))        /* determine network mask for address class */
1015         nmask = IN_CLASSA_NET;
1016     else if (IN_CLASSB(addr))
1017         nmask = IN_CLASSB_NET;
1018     else
1019         nmask = IN_CLASSC_NET;
1020     /* class D nets are disallowed by bad_ip_adrs */
1021     mask = netmask | htonl(nmask);
1022
1023     /*
1024      * Scan through the system's network interfaces.
1025      */
1026     ifc.ifc_len = sizeof(ifs);
1027     ifc.ifc_req = ifs;
1028     if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1029         syslog(LOG_WARNING, "ioctl(SIOCGIFCONF): %m");
1030         return mask;
1031     }
1032     ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1033     for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1034                 ((char *)&ifr->ifr_addr + ifr->ifr_addr.sa_len)) {
1035         /*
1036          * Check the interface's internet address.
1037          */
1038         if (ifr->ifr_addr.sa_family != AF_INET)
1039             continue;
1040         ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1041         if ((ntohl(ina) & nmask) != (addr & nmask))
1042             continue;
1043         /*
1044          * Check that the interface is up, and not point-to-point or loopback.
1045          */
1046         strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1047         if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1048             continue;
1049         if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
1050             != IFF_UP)
1051             continue;
1052         /*
1053          * Get its netmask and OR it into our mask.
1054          */
1055         if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1056             continue;
1057         mask |= ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr;
1058     }
1059
1060     return mask;
1061 }
1062
1063 /*
1064  * lock - create a lock file for the named lock device
1065  */
1066 #define LOCK_PREFIX     "/var/spool/lock/LCK.."
1067
1068 int
1069 lock(dev)
1070     char *dev;
1071 {
1072     char hdb_lock_buffer[12];
1073     int fd, pid, n;
1074     char *p;
1075
1076     if ((p = strrchr(dev, '/')) != NULL)
1077         dev = p + 1;
1078     lock_file = malloc(strlen(LOCK_PREFIX) + strlen(dev) + 1);
1079     if (lock_file == NULL)
1080         novm("lock file name");
1081     strcat(strcpy(lock_file, LOCK_PREFIX), dev);
1082
1083     while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
1084         if (errno == EEXIST
1085             && (fd = open(lock_file, O_RDONLY, 0)) >= 0) {
1086             /* Read the lock file to find out who has the device locked */
1087             n = read(fd, hdb_lock_buffer, 11);
1088             if (n > 0) {
1089                 hdb_lock_buffer[n] = 0;
1090                 pid = atoi(hdb_lock_buffer);
1091             }
1092             if (n <= 0) {
1093                 syslog(LOG_ERR, "Can't read pid from lock file %s", lock_file);
1094                 close(fd);
1095             } else {
1096                 if (kill(pid, 0) == -1 && errno == ESRCH) {
1097                     /* pid no longer exists - remove the lock file */
1098                     if (unlink(lock_file) == 0) {
1099                         close(fd);
1100                         syslog(LOG_NOTICE, "Removed stale lock on %s (pid %d)",
1101                                dev, pid);
1102                         continue;
1103                     } else
1104                         syslog(LOG_WARNING, "Couldn't remove stale lock on %s",
1105                                dev);
1106                 } else
1107                     syslog(LOG_NOTICE, "Device %s is locked by pid %d",
1108                            dev, pid);
1109             }
1110             close(fd);
1111         } else
1112             syslog(LOG_ERR, "Can't create lock file %s: %m", lock_file);
1113         free(lock_file);
1114         lock_file = NULL;
1115         return -1;
1116     }
1117
1118     sprintf(hdb_lock_buffer, "%10d\n", getpid());
1119     write(fd, hdb_lock_buffer, 11);
1120
1121     close(fd);
1122     return 0;
1123 }
1124
1125 /*
1126  * unlock - remove our lockfile
1127  */
1128 unlock()
1129 {
1130     if (lock_file) {
1131         unlink(lock_file);
1132         free(lock_file);
1133         lock_file = NULL;
1134     }
1135 }