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