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