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