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