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