]> git.ozlabs.org Git - ppp.git/blob - pppd/sys-bsd.c
leave tty with echo turned off if not default device
[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.18 1995/05/01 00:23:53 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 (!default_device) {
273             /*
274              * Turn off echoing, because otherwise we can get into
275              * a loop with the tty and the modem echoing to each other.
276              * We presume we are the sole user of this tty device, so
277              * when we close it, it will revert to its defaults anyway.
278              */
279             inittermios.c_lflag &= ~(ECHO | ECHONL);
280         }
281         if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
282             if (errno != ENXIO)
283                 syslog(LOG_WARNING, "tcsetattr: %m");
284         restore_term = 0;
285     }
286 }
287
288 /*
289  * setdtr - control the DTR line on the serial port.
290  * This is called from die(), so it shouldn't call die().
291  */
292 setdtr(fd, on)
293 int fd, on;
294 {
295     int modembits = TIOCM_DTR;
296
297     ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
298 }
299
300
301 /*
302  * output - Output PPP packet.
303  */
304 void
305 output(unit, p, len)
306     int unit;
307     u_char *p;
308     int len;
309 {
310     if (unit != 0)
311         MAINDEBUG((LOG_WARNING, "output: unit != 0!"));
312     if (debug)
313         log_packet(p, len, "sent ");
314
315     if (write(fd, p, len) < 0) {
316         syslog(LOG_ERR, "write: %m");
317         die(1);
318     }
319 }
320
321
322 /*
323  * wait_input - wait until there is data available on fd,
324  * for the length of time specified by *timo (indefinite
325  * if timo is NULL).
326  */
327 wait_input(timo)
328     struct timeval *timo;
329 {
330     fd_set ready;
331     int n;
332
333     FD_ZERO(&ready);
334     FD_SET(fd, &ready);
335     n = select(fd+1, &ready, NULL, &ready, timo);
336     if (n < 0 && errno != EINTR) {
337         syslog(LOG_ERR, "select: %m");
338         die(1);
339     }
340 }
341
342
343 /*
344  * read_packet - get a PPP packet from the serial device.
345  */
346 int
347 read_packet(buf)
348     u_char *buf;
349 {
350     int len;
351
352     if ((len = read(fd, buf, PPP_MTU + PPP_HDRLEN)) < 0) {
353         if (errno == EWOULDBLOCK || errno == EINTR) {
354             MAINDEBUG((LOG_DEBUG, "read(fd): %m"));
355             return -1;
356         }
357         syslog(LOG_ERR, "read(fd): %m");
358         die(1);
359     }
360     return len;
361 }
362
363
364 /*
365  * ppp_send_config - configure the transmit characteristics of
366  * the ppp interface.
367  */
368 void
369 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
370     int unit, mtu;
371     u_int32_t asyncmap;
372     int pcomp, accomp;
373 {
374     u_int x;
375     struct ifreq ifr;
376
377     strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
378     ifr.ifr_mtu = mtu;
379     if (ioctl(sockfd, SIOCSIFMTU, (caddr_t) &ifr) < 0) {
380         syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m");
381         quit();
382     }
383
384     if (ioctl(fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0) {
385         syslog(LOG_ERR, "ioctl(PPPIOCSASYNCMAP): %m");
386         quit();
387     }
388
389     if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
390         syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
391         quit();
392     }
393     x = pcomp? x | SC_COMP_PROT: x &~ SC_COMP_PROT;
394     x = accomp? x | SC_COMP_AC: x &~ SC_COMP_AC;
395     if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
396         syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
397         quit();
398     }
399 }
400
401
402 /*
403  * ppp_set_xaccm - set the extended transmit ACCM for the interface.
404  */
405 void
406 ppp_set_xaccm(unit, accm)
407     int unit;
408     ext_accm accm;
409 {
410     if (ioctl(fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY)
411         syslog(LOG_WARNING, "ioctl(set extended ACCM): %m");
412 }
413
414
415 /*
416  * ppp_recv_config - configure the receive-side characteristics of
417  * the ppp interface.
418  */
419 void
420 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
421     int unit, mru;
422     u_int32_t asyncmap;
423     int pcomp, accomp;
424 {
425     int x;
426
427     if (ioctl(fd, PPPIOCSMRU, (caddr_t) &mru) < 0) {
428         syslog(LOG_ERR, "ioctl(PPPIOCSMRU): %m");
429         quit();
430     }
431     if (ioctl(fd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0) {
432         syslog(LOG_ERR, "ioctl(PPPIOCSRASYNCMAP): %m");
433         quit();
434     }
435     if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
436         syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
437         quit();
438     }
439     x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC;
440     if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
441         syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
442         quit();
443     }
444 }
445
446 /*
447  * ccp_test - ask kernel whether a given compression method
448  * is acceptable for use.
449  */
450 ccp_test(unit, opt_ptr, opt_len, for_transmit)
451     int unit, opt_len, for_transmit;
452     u_char *opt_ptr;
453 {
454     struct ppp_option_data data;
455
456     data.ptr = opt_ptr;
457     data.length = opt_len;
458     data.transmit = for_transmit;
459     return ioctl(fd, PPPIOCSCOMPRESS, (caddr_t) &data) >= 0;
460 }
461
462 /*
463  * ccp_flags_set - inform kernel about the current state of CCP.
464  */
465 void
466 ccp_flags_set(unit, isopen, isup)
467     int unit, isopen, isup;
468 {
469     int x;
470
471     if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
472         syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
473         return;
474     }
475     x = isopen? x | SC_CCP_OPEN: x &~ SC_CCP_OPEN;
476     x = isup? x | SC_CCP_UP: x &~ SC_CCP_UP;
477     if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
478         syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
479 }
480
481 /*
482  * ccp_fatal_error - returns 1 if decompression was disabled as a
483  * result of an error detected after decompression of a packet,
484  * 0 otherwise.  This is necessary because of patent nonsense.
485  */
486 int
487 ccp_fatal_error(unit)
488     int unit;
489 {
490     int x;
491
492     if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
493         syslog(LOG_ERR, "ioctl(PPPIOCGFLAGS): %m");
494         return 0;
495     }
496     return x & SC_DC_FERROR;
497 }
498
499 /*
500  * sifvjcomp - config tcp header compression
501  */
502 int
503 sifvjcomp(u, vjcomp, cidcomp, maxcid)
504     int u, vjcomp, cidcomp, maxcid;
505 {
506     u_int x;
507
508     if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
509         syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
510         return 0;
511     }
512     x = vjcomp ? x | SC_COMP_TCP: x &~ SC_COMP_TCP;
513     x = cidcomp? x & ~SC_NO_TCP_CCID: x | SC_NO_TCP_CCID;
514     if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
515         syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
516         return 0;
517     }
518     if (ioctl(fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0) {
519         syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
520         return 0;
521     }
522     return 1;
523 }
524
525 /*
526  * sifup - Config the interface up and enable IP packets to pass.
527  */
528 #ifndef SC_ENABLE_IP
529 #define SC_ENABLE_IP    0x100   /* compat for old versions of kernel code */
530 #endif
531
532 int
533 sifup(u)
534     int u;
535 {
536     struct ifreq ifr;
537     u_int x;
538     struct npioctl npi;
539
540     strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
541     if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
542         syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
543         return 0;
544     }
545     ifr.ifr_flags |= IFF_UP;
546     if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
547         syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
548         return 0;
549     }
550     npi.protocol = PPP_IP;
551     npi.mode = NPMODE_PASS;
552     if (ioctl(fd, PPPIOCSNPMODE, &npi) < 0) {
553         if (errno != ENOTTY) {
554             syslog(LOG_ERR, "ioctl(PPPIOCSNPMODE): %m");
555             return 0;
556         }
557         /* for backwards compatibility */
558         if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
559             syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
560             return 0;
561         }
562         x |= SC_ENABLE_IP;
563         if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
564             syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
565             return 0;
566         }
567     }
568     return 1;
569 }
570
571 /*
572  * sifdown - Config the interface down and disable IP.
573  */
574 int
575 sifdown(u)
576     int u;
577 {
578     struct ifreq ifr;
579     u_int x;
580     int rv;
581     struct npioctl npi;
582
583     rv = 1;
584     npi.protocol = PPP_IP;
585     npi.mode = NPMODE_ERROR;
586     if (ioctl(fd, PPPIOCSNPMODE, (caddr_t) &npi) < 0) {
587         if (errno != ENOTTY) {
588             syslog(LOG_ERR, "ioctl(PPPIOCSNPMODE): %m");
589             rv = 0;
590         } else {
591             /* backwards compatibility */
592             if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
593                 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
594                 rv = 0;
595             } else {
596                 x &= ~SC_ENABLE_IP;
597                 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
598                     syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
599                     rv = 0;
600                 }
601             }
602         }
603     }
604
605     strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
606     if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
607         syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
608         rv = 0;
609     } else {
610         ifr.ifr_flags &= ~IFF_UP;
611         if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
612             syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
613             rv = 0;
614         }
615     }
616     return rv;
617 }
618
619 /*
620  * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
621  * if it exists.
622  */
623 #define SET_SA_FAMILY(addr, family)             \
624     BZERO((char *) &(addr), sizeof(addr));      \
625     addr.sa_family = (family);                  \
626     addr.sa_len = sizeof(addr);
627
628 /*
629  * sifaddr - Config the interface IP addresses and netmask.
630  */
631 int
632 sifaddr(u, o, h, m)
633     int u;
634     u_int32_t o, h, m;
635 {
636     struct ifaliasreq ifra;
637
638     strncpy(ifra.ifra_name, ifname, sizeof(ifra.ifra_name));
639     SET_SA_FAMILY(ifra.ifra_addr, AF_INET);
640     ((struct sockaddr_in *) &ifra.ifra_addr)->sin_addr.s_addr = o;
641     SET_SA_FAMILY(ifra.ifra_broadaddr, AF_INET);
642     ((struct sockaddr_in *) &ifra.ifra_broadaddr)->sin_addr.s_addr = h;
643     if (m != 0) {
644         SET_SA_FAMILY(ifra.ifra_mask, AF_INET);
645         ((struct sockaddr_in *) &ifra.ifra_mask)->sin_addr.s_addr = m;
646     } else
647         BZERO(&ifra.ifra_mask, sizeof(ifra.ifra_mask));
648     if (ioctl(sockfd, SIOCAIFADDR, (caddr_t) &ifra) < 0) {
649         if (errno != EEXIST) {
650             syslog(LOG_ERR, "ioctl(SIOCAIFADDR): %m");
651             return 0;
652         }
653         syslog(LOG_WARNING, "ioctl(SIOCAIFADDR): Address already exists");
654     }
655     return 1;
656 }
657
658 /*
659  * cifaddr - Clear the interface IP addresses, and delete routes
660  * through the interface if possible.
661  */
662 int
663 cifaddr(u, o, h)
664     int u;
665     u_int32_t o, h;
666 {
667     struct ifaliasreq ifra;
668
669     strncpy(ifra.ifra_name, ifname, sizeof(ifra.ifra_name));
670     SET_SA_FAMILY(ifra.ifra_addr, AF_INET);
671     ((struct sockaddr_in *) &ifra.ifra_addr)->sin_addr.s_addr = o;
672     SET_SA_FAMILY(ifra.ifra_broadaddr, AF_INET);
673     ((struct sockaddr_in *) &ifra.ifra_broadaddr)->sin_addr.s_addr = h;
674     BZERO(&ifra.ifra_mask, sizeof(ifra.ifra_mask));
675     if (ioctl(sockfd, SIOCDIFADDR, (caddr_t) &ifra) < 0) {
676         syslog(LOG_WARNING, "ioctl(SIOCDIFADDR): %m");
677         return 0;
678     }
679     return 1;
680 }
681
682 /*
683  * sifdefaultroute - assign a default route through the address given.
684  */
685 int
686 sifdefaultroute(u, g)
687     int u;
688     u_int32_t g;
689 {
690     return dodefaultroute(g, 's');
691 }
692
693 /*
694  * cifdefaultroute - delete a default route through the address given.
695  */
696 int
697 cifdefaultroute(u, g)
698     int u;
699     u_int32_t g;
700 {
701     return dodefaultroute(g, 'c');
702 }
703
704 /*
705  * dodefaultroute - talk to a routing socket to add/delete a default route.
706  */
707 int
708 dodefaultroute(g, cmd)
709     u_int32_t g;
710     int cmd;
711 {
712     int routes;
713     struct {
714         struct rt_msghdr        hdr;
715         struct sockaddr_in      dst;
716         struct sockaddr_in      gway;
717         struct sockaddr_in      mask;
718     } rtmsg;
719
720     if ((routes = socket(PF_ROUTE, SOCK_RAW, AF_INET)) < 0) {
721         syslog(LOG_ERR, "%cifdefaultroute: opening routing socket: %m", cmd);
722         return 0;
723     }
724
725     memset(&rtmsg, 0, sizeof(rtmsg));
726     rtmsg.hdr.rtm_type = cmd == 's'? RTM_ADD: RTM_DELETE;
727     rtmsg.hdr.rtm_flags = RTF_UP | RTF_GATEWAY;
728     rtmsg.hdr.rtm_version = RTM_VERSION;
729     rtmsg.hdr.rtm_seq = ++rtm_seq;
730     rtmsg.hdr.rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
731     rtmsg.dst.sin_len = sizeof(rtmsg.dst);
732     rtmsg.dst.sin_family = AF_INET;
733     rtmsg.gway.sin_len = sizeof(rtmsg.gway);
734     rtmsg.gway.sin_family = AF_INET;
735     rtmsg.gway.sin_addr.s_addr = g;
736     rtmsg.mask.sin_len = sizeof(rtmsg.dst);
737     rtmsg.mask.sin_family = AF_INET;
738
739     rtmsg.hdr.rtm_msglen = sizeof(rtmsg);
740     if (write(routes, &rtmsg, sizeof(rtmsg)) < 0) {
741         syslog(LOG_ERR, "%s default route: %m", cmd=='s'? "add": "delete");
742         close(routes);
743         return 0;
744     }
745
746     close(routes);
747     return 1;
748 }
749
750 #if RTM_VERSION >= 3
751
752 /*
753  * sifproxyarp - Make a proxy ARP entry for the peer.
754  */
755 static struct {
756     struct rt_msghdr            hdr;
757     struct sockaddr_inarp       dst;
758     struct sockaddr_dl          hwa;
759     char                        extra[128];
760 } arpmsg;
761
762 static int arpmsg_valid;
763
764 int
765 sifproxyarp(unit, hisaddr)
766     int unit;
767     u_int32_t hisaddr;
768 {
769     int routes;
770     int l;
771
772     /*
773      * Get the hardware address of an interface on the same subnet
774      * as our local address.
775      */
776     memset(&arpmsg, 0, sizeof(arpmsg));
777     if (!get_ether_addr(hisaddr, &arpmsg.hwa)) {
778         syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
779         return 0;
780     }
781
782     if ((routes = socket(PF_ROUTE, SOCK_RAW, AF_INET)) < 0) {
783         syslog(LOG_ERR, "sifproxyarp: opening routing socket: %m");
784         return 0;
785     }
786
787     arpmsg.hdr.rtm_type = RTM_ADD;
788     arpmsg.hdr.rtm_flags = RTF_ANNOUNCE | RTF_HOST | RTF_STATIC;
789     arpmsg.hdr.rtm_version = RTM_VERSION;
790     arpmsg.hdr.rtm_seq = ++rtm_seq;
791     arpmsg.hdr.rtm_addrs = RTA_DST | RTA_GATEWAY;
792     arpmsg.hdr.rtm_inits = RTV_EXPIRE;
793     arpmsg.dst.sin_len = sizeof(struct sockaddr_inarp);
794     arpmsg.dst.sin_family = AF_INET;
795     arpmsg.dst.sin_addr.s_addr = hisaddr;
796     arpmsg.dst.sin_other = SIN_PROXY;
797
798     arpmsg.hdr.rtm_msglen = (char *) &arpmsg.hwa - (char *) &arpmsg
799         + arpmsg.hwa.sdl_len;
800     if (write(routes, &arpmsg, arpmsg.hdr.rtm_msglen) < 0) {
801         syslog(LOG_ERR, "add proxy arp entry: %m");
802         close(routes);
803         return 0;
804     }
805
806     close(routes);
807     arpmsg_valid = 1;
808     return 1;
809 }
810
811 /*
812  * cifproxyarp - Delete the proxy ARP entry for the peer.
813  */
814 int
815 cifproxyarp(unit, hisaddr)
816     int unit;
817     u_int32_t hisaddr;
818 {
819     int routes;
820
821     if (!arpmsg_valid)
822         return 0;
823     arpmsg_valid = 0;
824
825     arpmsg.hdr.rtm_type = RTM_DELETE;
826     arpmsg.hdr.rtm_seq = ++rtm_seq;
827
828     if ((routes = socket(PF_ROUTE, SOCK_RAW, AF_INET)) < 0) {
829         syslog(LOG_ERR, "sifproxyarp: opening routing socket: %m");
830         return 0;
831     }
832
833     if (write(routes, &arpmsg, arpmsg.hdr.rtm_msglen) < 0) {
834         syslog(LOG_ERR, "delete proxy arp entry: %m");
835         close(routes);
836         return 0;
837     }
838
839     close(routes);
840     return 1;
841 }
842
843 #else   /* RTM_VERSION */
844
845 /*
846  * sifproxyarp - Make a proxy ARP entry for the peer.
847  */
848 int
849 sifproxyarp(unit, hisaddr)
850     int unit;
851     u_int32_t hisaddr;
852 {
853     struct arpreq arpreq;
854     struct {
855         struct sockaddr_dl      sdl;
856         char                    space[128];
857     } dls;
858
859     BZERO(&arpreq, sizeof(arpreq));
860
861     /*
862      * Get the hardware address of an interface on the same subnet
863      * as our local address.
864      */
865     if (!get_ether_addr(hisaddr, &dls.sdl)) {
866         syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
867         return 0;
868     }
869
870     arpreq.arp_ha.sa_len = sizeof(struct sockaddr);
871     arpreq.arp_ha.sa_family = AF_UNSPEC;
872     BCOPY(LLADDR(&dls.sdl), arpreq.arp_ha.sa_data, dls.sdl.sdl_alen);
873     SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
874     ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
875     arpreq.arp_flags = ATF_PERM | ATF_PUBL;
876     if (ioctl(sockfd, SIOCSARP, (caddr_t)&arpreq) < 0) {
877         syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
878         return 0;
879     }
880
881     return 1;
882 }
883
884 /*
885  * cifproxyarp - Delete the proxy ARP entry for the peer.
886  */
887 int
888 cifproxyarp(unit, hisaddr)
889     int unit;
890     u_int32_t hisaddr;
891 {
892     struct arpreq arpreq;
893
894     BZERO(&arpreq, sizeof(arpreq));
895     SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
896     ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
897     if (ioctl(sockfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
898         syslog(LOG_WARNING, "ioctl(SIOCDARP): %m");
899         return 0;
900     }
901     return 1;
902 }
903 #endif  /* RTM_VERSION */
904
905
906 /*
907  * get_ether_addr - get the hardware address of an interface on the
908  * the same subnet as ipaddr.
909  */
910 #define MAX_IFS         32
911
912 int
913 get_ether_addr(ipaddr, hwaddr)
914     u_int32_t ipaddr;
915     struct sockaddr_dl *hwaddr;
916 {
917     struct ifreq *ifr, *ifend, *ifp;
918     u_int32_t ina, mask;
919     struct sockaddr_dl *dla;
920     struct ifreq ifreq;
921     struct ifconf ifc;
922     struct ifreq ifs[MAX_IFS];
923
924     ifc.ifc_len = sizeof(ifs);
925     ifc.ifc_req = ifs;
926     if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
927         syslog(LOG_ERR, "ioctl(SIOCGIFCONF): %m");
928         return 0;
929     }
930
931     /*
932      * Scan through looking for an interface with an Internet
933      * address on the same subnet as `ipaddr'.
934      */
935     ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
936     for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
937                 ((char *)&ifr->ifr_addr + ifr->ifr_addr.sa_len)) {
938         if (ifr->ifr_addr.sa_family == AF_INET) {
939             ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
940             strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
941             /*
942              * Check that the interface is up, and not point-to-point
943              * or loopback.
944              */
945             if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
946                 continue;
947             if ((ifreq.ifr_flags &
948                  (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
949                  != (IFF_UP|IFF_BROADCAST))
950                 continue;
951             /*
952              * Get its netmask and check that it's on the right subnet.
953              */
954             if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
955                 continue;
956             mask = ((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr;
957             if ((ipaddr & mask) != (ina & mask))
958                 continue;
959
960             break;
961         }
962     }
963
964     if (ifr >= ifend)
965         return 0;
966     syslog(LOG_INFO, "found interface %s for proxy arp", ifr->ifr_name);
967
968     /*
969      * Now scan through again looking for a link-level address
970      * for this interface.
971      */
972     ifp = ifr;
973     for (ifr = ifc.ifc_req; ifr < ifend; ) {
974         if (strcmp(ifp->ifr_name, ifr->ifr_name) == 0
975             && ifr->ifr_addr.sa_family == AF_LINK) {
976             /*
977              * Found the link-level address - copy it out
978              */
979             dla = (struct sockaddr_dl *) &ifr->ifr_addr;
980             BCOPY(dla, hwaddr, dla->sdl_len);
981             return 1;
982         }
983         ifr = (struct ifreq *) ((char *)&ifr->ifr_addr + ifr->ifr_addr.sa_len);
984     }
985
986     return 0;
987 }
988
989 /*
990  * Return user specified netmask, modified by any mask we might determine
991  * for address `addr' (in network byte order).
992  * Here we scan through the system's list of interfaces, looking for
993  * any non-point-to-point interfaces which might appear to be on the same
994  * network as `addr'.  If we find any, we OR in their netmask to the
995  * user-specified netmask.
996  */
997 u_int32_t
998 GetMask(addr)
999     u_int32_t addr;
1000 {
1001     u_int32_t mask, nmask, ina;
1002     struct ifreq *ifr, *ifend, ifreq;
1003     struct ifconf ifc;
1004     struct ifreq ifs[MAX_IFS];
1005
1006     addr = ntohl(addr);
1007     if (IN_CLASSA(addr))        /* determine network mask for address class */
1008         nmask = IN_CLASSA_NET;
1009     else if (IN_CLASSB(addr))
1010         nmask = IN_CLASSB_NET;
1011     else
1012         nmask = IN_CLASSC_NET;
1013     /* class D nets are disallowed by bad_ip_adrs */
1014     mask = netmask | htonl(nmask);
1015
1016     /*
1017      * Scan through the system's network interfaces.
1018      */
1019     ifc.ifc_len = sizeof(ifs);
1020     ifc.ifc_req = ifs;
1021     if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1022         syslog(LOG_WARNING, "ioctl(SIOCGIFCONF): %m");
1023         return mask;
1024     }
1025     ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1026     for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1027                 ((char *)&ifr->ifr_addr + ifr->ifr_addr.sa_len)) {
1028         /*
1029          * Check the interface's internet address.
1030          */
1031         if (ifr->ifr_addr.sa_family != AF_INET)
1032             continue;
1033         ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1034         if ((ntohl(ina) & nmask) != (addr & nmask))
1035             continue;
1036         /*
1037          * Check that the interface is up, and not point-to-point or loopback.
1038          */
1039         strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1040         if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1041             continue;
1042         if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
1043             != IFF_UP)
1044             continue;
1045         /*
1046          * Get its netmask and OR it into our mask.
1047          */
1048         if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1049             continue;
1050         mask |= ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr;
1051     }
1052
1053     return mask;
1054 }
1055
1056 /*
1057  * lock - create a lock file for the named lock device
1058  */
1059 #define LOCK_PREFIX     "/var/spool/lock/LCK.."
1060
1061 int
1062 lock(dev)
1063     char *dev;
1064 {
1065     char hdb_lock_buffer[12];
1066     int fd, pid, n;
1067     char *p;
1068
1069     if ((p = strrchr(dev, '/')) != NULL)
1070         dev = p + 1;
1071     lock_file = malloc(strlen(LOCK_PREFIX) + strlen(dev) + 1);
1072     if (lock_file == NULL)
1073         novm("lock file name");
1074     strcat(strcpy(lock_file, LOCK_PREFIX), dev);
1075
1076     while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
1077         if (errno == EEXIST
1078             && (fd = open(lock_file, O_RDONLY, 0)) >= 0) {
1079             /* Read the lock file to find out who has the device locked */
1080             n = read(fd, hdb_lock_buffer, 11);
1081             if (n > 0) {
1082                 hdb_lock_buffer[n] = 0;
1083                 pid = atoi(hdb_lock_buffer);
1084             }
1085             if (n <= 0) {
1086                 syslog(LOG_ERR, "Can't read pid from lock file %s", lock_file);
1087                 close(fd);
1088             } else {
1089                 if (kill(pid, 0) == -1 && errno == ESRCH) {
1090                     /* pid no longer exists - remove the lock file */
1091                     if (unlink(lock_file) == 0) {
1092                         close(fd);
1093                         syslog(LOG_NOTICE, "Removed stale lock on %s (pid %d)",
1094                                dev, pid);
1095                         continue;
1096                     } else
1097                         syslog(LOG_WARNING, "Couldn't remove stale lock on %s",
1098                                dev);
1099                 } else
1100                     syslog(LOG_NOTICE, "Device %s is locked by pid %d",
1101                            dev, pid);
1102             }
1103             close(fd);
1104         } else
1105             syslog(LOG_ERR, "Can't create lock file %s: %m", lock_file);
1106         free(lock_file);
1107         lock_file = NULL;
1108         return -1;
1109     }
1110
1111     sprintf(hdb_lock_buffer, "%10d\n", getpid());
1112     write(fd, hdb_lock_buffer, 11);
1113
1114     close(fd);
1115     return 0;
1116 }
1117
1118 /*
1119  * unlock - remove our lockfile
1120  */
1121 unlock()
1122 {
1123     if (lock_file) {
1124         unlink(lock_file);
1125         free(lock_file);
1126         lock_file = NULL;
1127     }
1128 }