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