]> git.ozlabs.org Git - ppp.git/blob - pppd/sys-str.c
stuff moved here from main.c; use uint32 for IP addresses;
[ppp.git] / pppd / sys-str.c
1 /*
2  * sys-str.c - System-dependent procedures for setting up
3  * PPP interfaces on systems which use the STREAMS ppp interface.
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 /*
22  * TODO:
23  */
24
25 #include <stdio.h>
26 #include <errno.h>
27 #include <syslog.h>
28 #include <termios.h>
29 #include <fcntl.h>
30 #include <string.h>
31 #include <time.h>
32 #include <utmp.h>
33 #include <poll.h>
34 #include <sys/types.h>
35 #include <sys/socket.h>
36 #include <sys/sockio.h>
37 #include <sys/time.h>
38 #include <sys/stream.h>
39 #include <sys/stropts.h>
40
41 #include <net/if.h>
42 #include <net/route.h>
43 #include <net/if_arp.h>
44 #include <netinet/in.h>
45
46 #include "pppd.h"
47 #include "ppp.h"
48 #include <net/ppp_str.h>
49
50 #ifndef ifr_mtu
51 #define ifr_mtu         ifr_metric
52 #endif
53
54 #define MAXMODULES      10      /* max number of module names to save */
55 static struct   modlist {
56     char        modname[FMNAMESZ+1];
57 } str_modules[MAXMODULES];
58 static int      str_module_count = 0;
59 static int      pushed_ppp;
60 static int      closed_stdio;
61
62 static int      restore_term;   /* 1 => we've munged the terminal */
63 static struct termios inittermios; /* Initial TTY termios */
64
65 /*
66  * sys_init - System-dependent initialization.
67  */
68 void
69 sys_init()
70 {
71     openlog("pppd", LOG_PID | LOG_NDELAY, LOG_PPP);
72     setlogmask(LOG_UPTO(LOG_INFO));
73     if (debug)
74         setlogmask(LOG_UPTO(LOG_DEBUG));
75 }
76
77 /*
78  * note_debug_level - note a change in the debug level.
79  */
80 void
81 note_debug_level()
82 {
83     if (debug) {
84         syslog(LOG_INFO, "Debug turned ON, Level %d", debug);
85         setlogmask(LOG_UPTO(LOG_DEBUG));
86     } else {
87         setlogmask(LOG_UPTO(LOG_WARNING));
88     }
89 }
90
91 /*
92  * daemon - Detach us from the terminal session.
93  */
94 int
95 daemon(nochdir, noclose)
96     int nochdir, noclose;
97 {
98     int pid;
99
100     if ((pid = fork()) < 0)
101         return -1;
102     if (pid != 0)
103         exit(0);                /* parent dies */
104     setsid();
105     if (!nochdir)
106         chdir("/");
107     if (!noclose) {
108         fclose(stdin);          /* don't need stdin, stdout, stderr */
109         fclose(stdout);
110         fclose(stderr);
111     }
112     return 0;
113 }
114
115
116 /*
117  * ppp_available - check if this kernel supports PPP.
118  */
119 int
120 ppp_available()
121 {
122     int fd, ret;
123
124     fd = open("/dev/tty", O_RDONLY, 0);
125     if (fd < 0)
126         return 1;               /* can't find out - assume we have ppp */
127     ret = ioctl(fd, I_FIND, "pppasync") >= 0;
128     close(fd);
129     return ret;
130 }
131
132
133 /*
134  * establish_ppp - Turn the serial port into a ppp interface.
135  */
136 void
137 establish_ppp()
138 {
139     /* go through and save the name of all the modules, then pop em */
140     for (;;) { 
141         if (ioctl(fd, I_LOOK, str_modules[str_module_count].modname) < 0 ||
142             ioctl(fd, I_POP, 0) < 0)
143             break;
144         MAINDEBUG((LOG_DEBUG, "popped stream module : %s",
145                    str_modules[str_module_count].modname));
146         str_module_count++;
147     }
148
149     /* now push the async/fcs module */
150     if (ioctl(fd, I_PUSH, "pppasync") < 0) {
151         syslog(LOG_ERR, "ioctl(I_PUSH, ppp_async): %m");
152         die(1);
153     }
154     /* push the compress module */
155     if (ioctl(fd, I_PUSH, "pppcomp") < 0) {
156         syslog(LOG_WARNING, "ioctl(I_PUSH, ppp_comp): %m");
157     }
158     /* finally, push the ppp_if module that actually handles the */
159     /* network interface */ 
160     if (ioctl(fd, I_PUSH, "pppif") < 0) {
161         syslog(LOG_ERR, "ioctl(I_PUSH, ppp_if): %m");
162         die(1);
163     }
164     pushed_ppp = 1;
165     /* read mode, message non-discard mode */
166     if (ioctl(fd, I_SRDOPT, RMSGN) < 0) {
167         syslog(LOG_ERR, "ioctl(I_SRDOPT, RMSGN): %m");
168         die(1);
169     }
170     /*
171      * Find out which interface we were given.
172      * (ppp_if handles this ioctl)
173      */
174     if (ioctl(fd, SIOCGETU, &ifunit) < 0) {
175         syslog(LOG_ERR, "ioctl(SIOCGETU): %m");
176         die(1);
177     }
178
179     /* Set debug flags in driver */
180     if (ioctl(fd, SIOCSIFDEBUG, &kdebugflag) < 0) {
181         syslog(LOG_ERR, "ioctl(SIOCSIFDEBUG): %m");
182     }
183
184     /* close stdin, stdout, stderr if they might refer to the device */
185     if (default_device && !closed_stdio) {
186         int i;
187
188         for (i = 0; i <= 2; ++i)
189             if (i != fd && i != s)
190                 close(i);
191         closed_stdio = 1;
192     }
193 }
194
195 /*
196  * disestablish_ppp - Restore the serial port to normal operation.
197  * It attempts to reconstruct the stream with the previously popped
198  * modules.  This shouldn't call die() because it's called from die().
199  */
200 void
201 disestablish_ppp()
202 {
203     int flags;
204     char *s;
205
206     if (hungup) {
207         /* we can't push or pop modules after the stream has hung up */
208         str_module_count = 0;
209         restore_term = 0;       /* nor can we fix up terminal settings */
210         return;
211     }
212
213     if (pushed_ppp) {
214         /*
215          * Check whether the link seems not to be 8-bit clean.
216          */
217         if (ioctl(fd, SIOCGIFDEBUG, (caddr_t) &flags) == 0) {
218             s = NULL;
219             switch (~flags & PAI_FLAGS_HIBITS) {
220             case PAI_FLAGS_B7_0:
221                 s = "bit 7 set to 1";
222                 break;
223             case PAI_FLAGS_B7_1:
224                 s = "bit 7 set to 0";
225                 break;
226             case PAI_FLAGS_PAR_EVEN:
227                 s = "odd parity";
228                 break;
229             case PAI_FLAGS_PAR_ODD:
230                 s = "even parity";
231                 break;
232             }
233             if (s != NULL) {
234                 syslog(LOG_WARNING, "Serial link is not 8-bit clean:");
235                 syslog(LOG_WARNING, "All received characters had %s", s);
236             }
237         }
238     }
239
240     while (ioctl(fd, I_POP, 0) == 0)    /* pop any we pushed */
241         ;
242     pushed_ppp = 0;
243   
244     for (; str_module_count > 0; str_module_count--) {
245         if (ioctl(fd, I_PUSH, str_modules[str_module_count-1].modname)) {
246             if (errno != ENXIO)
247                 syslog(LOG_WARNING, "str_restore: couldn't push module %s: %m",
248                        str_modules[str_module_count-1].modname);
249         } else {
250             MAINDEBUG((LOG_INFO, "str_restore: pushed module %s",
251                        str_modules[str_module_count-1].modname));
252         }
253     }
254 }
255
256
257 /*
258  * List of valid speeds.
259  */
260 struct speed {
261     int speed_int, speed_val;
262 } speeds[] = {
263 #ifdef B50
264     { 50, B50 },
265 #endif
266 #ifdef B75
267     { 75, B75 },
268 #endif
269 #ifdef B110
270     { 110, B110 },
271 #endif
272 #ifdef B134
273     { 134, B134 },
274 #endif
275 #ifdef B150
276     { 150, B150 },
277 #endif
278 #ifdef B200
279     { 200, B200 },
280 #endif
281 #ifdef B300
282     { 300, B300 },
283 #endif
284 #ifdef B600
285     { 600, B600 },
286 #endif
287 #ifdef B1200
288     { 1200, B1200 },
289 #endif
290 #ifdef B1800
291     { 1800, B1800 },
292 #endif
293 #ifdef B2000
294     { 2000, B2000 },
295 #endif
296 #ifdef B2400
297     { 2400, B2400 },
298 #endif
299 #ifdef B3600
300     { 3600, B3600 },
301 #endif
302 #ifdef B4800
303     { 4800, B4800 },
304 #endif
305 #ifdef B7200
306     { 7200, B7200 },
307 #endif
308 #ifdef B9600
309     { 9600, B9600 },
310 #endif
311 #ifdef B19200
312     { 19200, B19200 },
313 #endif
314 #ifdef B38400
315     { 38400, B38400 },
316 #endif
317 #ifdef EXTA
318     { 19200, EXTA },
319 #endif
320 #ifdef EXTB
321     { 38400, EXTB },
322 #endif
323 #ifdef B57600
324     { 57600, B57600 },
325 #endif
326 #ifdef B115200
327     { 115200, B115200 },
328 #endif
329     { 0, 0 }
330 };
331
332 /*
333  * Translate from bits/second to a speed_t.
334  */
335 int
336 translate_speed(bps)
337     int bps;
338 {
339     struct speed *speedp;
340
341     if (bps == 0)
342         return 0;
343     for (speedp = speeds; speedp->speed_int; speedp++)
344         if (bps == speedp->speed_int)
345             return speedp->speed_val;
346     syslog(LOG_WARNING, "speed %d not supported", bps);
347     return 0;
348 }
349
350 /*
351  * Translate from a speed_t to bits/second.
352  */
353 int
354 baud_rate_of(speed)
355     int speed;
356 {
357     struct speed *speedp;
358
359     if (speed == 0)
360         return 0;
361     for (speedp = speeds; speedp->speed_int; speedp++)
362         if (speed == speedp->speed_val)
363             return speedp->speed_int;
364     return 0;
365 }
366
367 /*
368  * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
369  * at the requested speed, etc.  If `local' is true, set CLOCAL
370  * regardless of whether the modem option was specified.
371  */
372 set_up_tty(fd, local)
373     int fd, local;
374 {
375     int speed;
376     struct termios tios;
377
378     if (tcgetattr(fd, &tios) < 0) {
379         syslog(LOG_ERR, "tcgetattr: %m");
380         die(1);
381     }
382
383     if (!restore_term)
384         inittermios = tios;
385
386     tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL | CRTSCTS);
387     if (crtscts == 1)
388         tios.c_cflag |= CRTSCTS;
389
390     tios.c_cflag |= CS8 | CREAD | HUPCL;
391     if (local || !modem)
392         tios.c_cflag |= CLOCAL;
393     tios.c_iflag = IGNBRK | IGNPAR;
394     tios.c_oflag = 0;
395     tios.c_lflag = 0;
396     tios.c_cc[VMIN] = 1;
397     tios.c_cc[VTIME] = 0;
398
399     if (crtscts == 2) {
400         tios.c_iflag |= IXOFF;
401         tios.c_cc[VSTOP] = 0x13;        /* DC3 = XOFF = ^S */
402         tios.c_cc[VSTART] = 0x11;       /* DC1 = XON  = ^Q */
403     }
404
405     speed = translate_speed(inspeed);
406     if (speed) {
407         cfsetospeed(&tios, speed);
408         cfsetispeed(&tios, speed);
409     } else {
410         speed = cfgetospeed(&tios);
411         /*
412          * We can't proceed if the serial port speed is B0,
413          * since that implies that the serial port is disabled.
414          */
415         if (speed == B0) {
416             syslog(LOG_ERR, "Baud rate for %s is 0; need explicit baud rate",
417                    devnam);
418             die(1);
419         }
420     }
421
422     if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
423         syslog(LOG_ERR, "tcsetattr: %m");
424         die(1);
425     }
426
427     baud_rate = inspeed = baud_rate_of(speed);
428     restore_term = 1;
429 }
430
431 /*
432  * restore_tty - restore the terminal to the saved settings.
433  */
434 void
435 restore_tty()
436 {
437     if (restore_term) {
438         if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
439             if (errno != ENXIO)
440                 syslog(LOG_WARNING, "tcsetattr: %m");
441         restore_term = 0;
442     }
443 }
444
445 /*
446  * setdtr - control the DTR line on the serial port.
447  * This is called from die(), so it shouldn't call die().
448  */
449 setdtr(fd, on)
450 int fd, on;
451 {
452     int modembits = TIOCM_DTR;
453
454     ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
455 }
456
457
458 /*
459  * output - Output PPP packet.
460  */
461 void
462 output(unit, p, len)
463     int unit;
464     u_char *p;
465     int len;
466 {
467     struct strbuf       str;
468
469     if (unit != 0)
470         MAINDEBUG((LOG_WARNING, "output: unit != 0!"));
471     if (debug)
472         log_packet(p, len, "sent ");
473
474     str.len = len;
475     str.buf = (caddr_t) p;
476     if (putmsg(fd, NULL, &str, 0) < 0) {
477         if (errno != ENXIO) {
478             syslog(LOG_ERR, "putmsg: %m");
479             die(1);
480         }
481     }
482 }
483
484 /*
485  * wait_input - wait for input, for a length of time specified in *timo.
486  */
487 wait_input(timo)
488     struct timeval *timo;
489 {
490     int t;
491     struct pollfd pfd;
492
493     t = timo == NULL? -1: timo->tv_sec * 1000 + timo->tv_usec / 1000;
494     pfd.fd = fd;
495     pfd.events = POLLIN | POLLPRI | POLLHUP;
496     if (poll(&pfd, 1, t) < 0 && errno != EINTR) {
497         syslog(LOG_ERR, "poll: %m");
498         die(1);
499     }
500 }
501
502 /*
503  * read_packet - get a PPP packet from the serial device.
504  */
505 int
506 read_packet(buf)
507     u_char *buf;
508 {
509     struct strbuf str, ctl;
510     int len, i;
511     unsigned char ctlbuf[16];
512
513     str.maxlen = MTU+DLLHEADERLEN;
514     str.buf = (caddr_t) buf;
515     ctl.maxlen = sizeof(ctlbuf);
516     ctl.buf = (caddr_t) ctlbuf;
517     i = 0;
518     len = getmsg(fd, &ctl, &str, &i);
519     if (len < 0) {
520         if (errno == EAGAIN || errno == EWOULDBLOCK) {
521             return -1;
522         }
523         syslog(LOG_ERR, "getmsg(fd) %m");
524         die(1);
525     }
526     if (len) 
527         MAINDEBUG((LOG_DEBUG, "getmsg returned 0x%x",len));
528     if (ctl.len > 0)
529         syslog(LOG_NOTICE, "got ctrl msg len %d %x %x\n", ctl.len,
530                ctlbuf[0], ctlbuf[1]);
531
532     if (str.len < 0) {
533         MAINDEBUG((LOG_DEBUG, "getmsg short return length %d", str.len));
534         return -1;
535     }
536
537     return str.len;
538 }
539
540
541 /*
542  * ppp_send_config - configure the transmit characteristics of
543  * the ppp interface.
544  */
545 void
546 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
547     int unit, mtu;
548     uint32 asyncmap;
549     int pcomp, accomp;
550 {
551     char c;
552     struct ifreq ifr;
553
554     strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
555     ifr.ifr_mtu = mtu;
556     if (ioctl(s, SIOCSIFMTU, (caddr_t) &ifr) < 0) {
557         syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m");
558         quit();
559     }
560
561     if(ioctl(fd, SIOCSIFASYNCMAP, (caddr_t) &asyncmap) < 0) {
562         syslog(LOG_ERR, "ioctl(SIOCSIFASYNCMAP): %m");
563         quit();
564     }
565
566     c = (pcomp? 1: 0);
567     if(ioctl(fd, SIOCSIFCOMPPROT, &c) < 0) {
568         syslog(LOG_ERR, "ioctl(SIOCSIFCOMPPROT): %m");
569         quit();
570     }
571
572     c = (accomp? 1: 0);
573     if(ioctl(fd, SIOCSIFCOMPAC, &c) < 0) {
574         syslog(LOG_ERR, "ioctl(SIOCSIFCOMPAC): %m");
575         quit();
576     }
577 }
578
579
580 /*
581  * ppp_set_xaccm - set the extended transmit ACCM for the interface.
582  */
583 void
584 ppp_set_xaccm(unit, accm)
585     int unit;
586     ext_accm accm;
587 {
588     if (ioctl(fd, SIOCSIFXASYNCMAP, accm) < 0 && errno != ENOTTY)
589         syslog(LOG_WARNING, "ioctl(set extended ACCM): %m");
590 }
591
592
593 /*
594  * ppp_recv_config - configure the receive-side characteristics of
595  * the ppp interface.
596  */
597 void
598 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
599     int unit, mru;
600     uint32 asyncmap;
601     int pcomp, accomp;
602 {
603     char c;
604
605     if (ioctl(fd, SIOCSIFMRU, &mru) < 0) {
606         syslog(LOG_ERR, "ioctl(SIOCSIFMRU): %m");
607     }
608
609     if (ioctl(fd, SIOCSIFRASYNCMAP, (caddr_t) &asyncmap) < 0) {
610         syslog(LOG_ERR, "ioctl(SIOCSIFRASYNCMAP): %m");
611     }
612
613     c = 2 + (pcomp? 1: 0);
614     if(ioctl(fd, SIOCSIFCOMPPROT, &c) < 0) {
615         syslog(LOG_ERR, "ioctl(SIOCSIFCOMPPROT): %m");
616     }
617
618     c = 2 + (accomp? 1: 0);
619     if (ioctl(fd, SIOCSIFCOMPAC, &c) < 0) {
620         syslog(LOG_ERR, "ioctl(SIOCSIFCOMPAC): %m");
621     }
622 }
623
624 /*
625  * ccp_test - ask kernel whether a given compression method
626  * is acceptable for use.
627  */
628 ccp_test(unit, opt_ptr, opt_len, for_transmit)
629     int unit, opt_len, for_transmit;
630     u_char *opt_ptr;
631 {
632     struct ppp_option_data data;
633
634     if ((unsigned) opt_len > MAX_PPP_OPTION)
635         opt_len = MAX_PPP_OPTION;
636     data.length = opt_len;
637     data.transmit = for_transmit;
638     BCOPY(opt_ptr, data.opt_data, opt_len);
639     return ioctl(fd, SIOCSCOMPRESS, (caddr_t) &data) >= 0;
640 }
641
642 /*
643  * ccp_flags_set - inform kernel about the current state of CCP.
644  */
645 void
646 ccp_flags_set(unit, isopen, isup)
647     int unit, isopen, isup;
648 {
649     int x;
650
651     x = (isopen? 1: 0) + (isup? 2: 0);
652     if (ioctl(fd, SIOCSIFCOMP, (caddr_t) &x) < 0 && errno != ENOTTY)
653         syslog(LOG_ERR, "ioctl (SIOCSIFCOMP): %m");
654 }
655
656 /*
657  * sifvjcomp - config tcp header compression
658  */
659 int
660 sifvjcomp(u, vjcomp, cidcomp, maxcid)
661     int u, vjcomp, cidcomp, maxcid;
662 {
663     char x;
664
665     x = (vjcomp? 1: 0) + (cidcomp? 0: 2) + (maxcid << 4);
666     if (ioctl(fd, SIOCSIFVJCOMP, (caddr_t) &x) < 0) {
667         syslog(LOG_ERR, "ioctl(SIOCSIFVJCOMP): %m");
668         return 0;
669     }
670     return 1;
671 }
672
673 /*
674  * sifup - Config the interface up.
675  */
676 int
677 sifup(u)
678     int u;
679 {
680     struct ifreq ifr;
681
682     strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
683     if (ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
684         syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
685         return 0;
686     }
687     ifr.ifr_flags |= IFF_UP;
688     if (ioctl(s, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
689         syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
690         return 0;
691     }
692     return 1;
693 }
694
695 /*
696  * sifdown - Config the interface down.
697  */
698 int
699 sifdown(u)
700     int u;
701 {
702     struct ifreq ifr;
703     strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
704     if (ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
705         syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
706         return 0;
707     }
708     ifr.ifr_flags &= ~IFF_UP;
709     if (ioctl(s, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
710         syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
711         return 0;
712     }
713     return 1;
714 }
715
716 /*
717  * SET_SA_FAMILY - initialize a struct sockaddr, setting the sa_family field.
718  */
719 #define SET_SA_FAMILY(addr, family)             \
720     BZERO((char *) &(addr), sizeof(addr));      \
721     addr.sa_family = (family);
722
723 /*
724  * sifaddr - Config the interface IP addresses and netmask.
725  */
726 int
727 sifaddr(u, o, h, m)
728     int u;
729     uint32 o, h, m;
730 {
731     int ret;
732     struct ifreq ifr;
733
734     ret = 1;
735     strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
736     SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
737     ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
738     if (ioctl(s, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
739         syslog(LOG_ERR, "ioctl(SIOCSIFADDR): %m");
740         ret = 0;
741     }
742     ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = h;
743     if (ioctl(s, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
744         syslog(LOG_ERR, "ioctl(SIOCSIFDSTADDR): %m");
745         ret = 0;
746     }
747     if (m != 0) {
748         ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = m;
749         syslog(LOG_INFO, "Setting interface mask to %s\n", ip_ntoa(m));
750         if (ioctl(s, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
751             syslog(LOG_ERR, "ioctl(SIOCSIFNETMASK): %m");
752             ret = 0;
753         }
754     }
755     return ret;
756 }
757
758 /*
759  * cifaddr - Clear the interface IP addresses, and delete routes
760  * through the interface if possible.
761  */
762 int
763 cifaddr(u, o, h)
764     int u;
765     uint32 o, h;
766 {
767     struct rtentry rt;
768
769     SET_SA_FAMILY(rt.rt_dst, AF_INET);
770     ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = h;
771     SET_SA_FAMILY(rt.rt_gateway, AF_INET);
772     ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = o;
773     rt.rt_flags = RTF_HOST;
774     if (ioctl(s, SIOCDELRT, (caddr_t) &rt) < 0) {
775         syslog(LOG_ERR, "ioctl(SIOCDELRT): %m");
776         return 0;
777     }
778     return 1;
779 }
780
781 /*
782  * sifdefaultroute - assign a default route through the address given.
783  */
784 int
785 sifdefaultroute(u, g)
786     int u;
787     uint32 g;
788 {
789     struct rtentry rt;
790
791     SET_SA_FAMILY(rt.rt_dst, AF_INET);
792     SET_SA_FAMILY(rt.rt_gateway, AF_INET);
793     ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
794     rt.rt_flags = RTF_GATEWAY;
795     if (ioctl(s, SIOCADDRT, &rt) < 0) {
796         syslog(LOG_ERR, "default route ioctl(SIOCADDRT): %m");
797         return 0;
798     }
799     return 1;
800 }
801
802 /*
803  * cifdefaultroute - delete a default route through the address given.
804  */
805 int
806 cifdefaultroute(u, g)
807     int u;
808     uint32 g;
809 {
810     struct rtentry rt;
811
812     SET_SA_FAMILY(rt.rt_dst, AF_INET);
813     SET_SA_FAMILY(rt.rt_gateway, AF_INET);
814     ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
815     rt.rt_flags = RTF_GATEWAY;
816     if (ioctl(s, SIOCDELRT, &rt) < 0) {
817         syslog(LOG_ERR, "default route ioctl(SIOCDELRT): %m");
818         return 0;
819     }
820     return 1;
821 }
822
823 /*
824  * sifproxyarp - Make a proxy ARP entry for the peer.
825  */
826 int
827 sifproxyarp(unit, hisaddr)
828     int unit;
829     uint32 hisaddr;
830 {
831     struct arpreq arpreq;
832
833     BZERO(&arpreq, sizeof(arpreq));
834
835     /*
836      * Get the hardware address of an interface on the same subnet
837      * as our local address.
838      */
839     if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) {
840         syslog(LOG_WARNING, "Cannot determine ethernet address for proxy ARP");
841         return 0;
842     }
843
844     SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
845     ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
846     arpreq.arp_flags = ATF_PERM | ATF_PUBL;
847     if (ioctl(s, SIOCSARP, (caddr_t)&arpreq) < 0) {
848         syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
849         return 0;
850     }
851
852     return 1;
853 }
854
855 /*
856  * cifproxyarp - Delete the proxy ARP entry for the peer.
857  */
858 int
859 cifproxyarp(unit, hisaddr)
860     int unit;
861     uint32 hisaddr;
862 {
863     struct arpreq arpreq;
864
865     BZERO(&arpreq, sizeof(arpreq));
866     SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
867     ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
868     if (ioctl(s, SIOCDARP, (caddr_t)&arpreq) < 0) {
869         syslog(LOG_ERR, "ioctl(SIOCDARP): %m");
870         return 0;
871     }
872     return 1;
873 }
874
875 /*
876  * get_ether_addr - get the hardware address of an interface on the
877  * the same subnet as ipaddr.  Code borrowed from myetheraddr.c
878  * in the cslip-2.6 distribution, which is subject to the following
879  * copyright notice (which also applies to logwtmp below):
880  *
881  * Copyright (c) 1990, 1992 The Regents of the University of California.
882  * All rights reserved.
883  *
884  * Redistribution and use in source and binary forms, with or without
885  * modification, are permitted provided that: (1) source code distributions
886  * retain the above copyright notice and this paragraph in its entirety, (2)
887  * distributions including binary code include the above copyright notice and
888  * this paragraph in its entirety in the documentation or other materials
889  * provided with the distribution, and (3) all advertising materials mentioning
890  * features or use of this software display the following acknowledgement:
891  * ``This product includes software developed by the University of California,
892  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
893  * the University nor the names of its contributors may be used to endorse
894  * or promote products derived from this software without specific prior
895  * written permission.
896  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
897  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
898  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
899  */
900
901 #include <fcntl.h>
902 #include <nlist.h>
903 #include <kvm.h>
904 #include <arpa/inet.h>
905
906 /* XXX SunOS 4.1 defines this and 3.5 doesn't... */
907 #ifdef _nlist_h
908 #define SUNOS4
909 #endif
910
911 #ifdef SUNOS4
912 #include <netinet/in_var.h>
913 #endif
914 #include <netinet/if_ether.h>
915
916 /* Cast a struct sockaddr to a structaddr_in */
917 #define SATOSIN(sa) ((struct sockaddr_in *)(sa))
918
919 /* Determine if "bits" is set in "flag" */
920 #define ALLSET(flag, bits) (((flag) & (bits)) == (bits))
921
922 static struct nlist nl[] = {
923 #define N_IFNET 0
924         { "_ifnet" },
925         { 0 }
926 };
927
928 static void kread();
929
930 int
931 get_ether_addr(ipaddr, hwaddr)
932     uint32 ipaddr;
933     struct sockaddr *hwaddr;
934 {
935     register kvm_t *kd;
936     register struct ifnet *ifp;
937     register struct arpcom *ac;
938     struct arpcom arpcom;
939     struct in_addr *inp;
940 #ifdef SUNOS4
941     register struct ifaddr *ifa;
942     register struct in_ifaddr *in;
943     union {
944         struct ifaddr ifa;
945         struct in_ifaddr in;
946     } ifaddr;
947 #endif
948     uint32 addr, mask;
949
950     /* Open kernel memory for reading */
951     kd = kvm_open(0, 0, 0, O_RDONLY, NULL);
952     if (kd == 0) {
953         syslog(LOG_ERR, "kvm_open: %m");
954         return 0;
955     }
956
957     /* Fetch namelist */
958     if (kvm_nlist(kd, nl) != 0) {
959         syslog(LOG_ERR, "kvm_nlist failed");
960         return 0;
961     }
962
963     ac = &arpcom;
964     ifp = &arpcom.ac_if;
965 #ifdef SUNOS4
966     ifa = &ifaddr.ifa;
967     in = &ifaddr.in;
968 #endif
969
970     if (kvm_read(kd, nl[N_IFNET].n_value, (char *)&addr, sizeof(addr))
971         != sizeof(addr)) {
972         syslog(LOG_ERR, "error reading ifnet addr");
973         return 0;
974     }
975     for ( ; addr; addr = (uint32)ifp->if_next) {
976         if (kvm_read(kd, addr, (char *)ac, sizeof(*ac)) != sizeof(*ac)) {
977             syslog(LOG_ERR, "error reading ifnet");
978             return 0;
979         }
980
981         /* Only look at configured, broadcast interfaces */
982         if (!ALLSET(ifp->if_flags, IFF_UP | IFF_BROADCAST))
983             continue;
984 #ifdef SUNOS4
985         /* This probably can't happen... */
986         if (ifp->if_addrlist == 0)
987             continue;
988 #endif
989
990         /* Get interface ip address */
991 #ifdef SUNOS4
992         if (kvm_read(kd, (uint32)ifp->if_addrlist, (char *)&ifaddr,
993                      sizeof(ifaddr)) != sizeof(ifaddr)) {
994             syslog(LOG_ERR, "error reading ifaddr");
995             return 0;
996         }
997         inp = &SATOSIN(&ifa->ifa_addr)->sin_addr;
998 #else
999         inp = &SATOSIN(&ifp->if_addr)->sin_addr;
1000 #endif
1001
1002         /* Check if this interface on the right subnet */
1003 #ifdef SUNOS4
1004         mask = in->ia_subnetmask;
1005 #else
1006         mask = ifp->if_subnetmask;
1007 #endif
1008         if ((ipaddr & mask) != (inp->s_addr & mask))
1009             continue;
1010
1011         /* Copy out the local ethernet address */
1012         hwaddr->sa_family = AF_UNSPEC;
1013         BCOPY((caddr_t) &arpcom.ac_enaddr, hwaddr->sa_data,
1014               sizeof(arpcom.ac_enaddr));
1015         return 1;               /* success! */
1016     }
1017
1018     /* couldn't find one */
1019     return 0;
1020 }
1021
1022 #define WTMPFILE        "/usr/adm/wtmp"
1023
1024 int
1025 logwtmp(line, name, host)
1026     char *line, *name, *host;
1027 {
1028     int fd;
1029     struct stat buf;
1030     struct utmp ut;
1031
1032     if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0)
1033         return;
1034     if (!fstat(fd, &buf)) {
1035         (void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
1036         (void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
1037         (void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
1038         (void)time(&ut.ut_time);
1039         if (write(fd, (char *)&ut, sizeof(struct utmp)) != sizeof(struct utmp))
1040             (void)ftruncate(fd, buf.st_size);
1041     }
1042     close(fd);
1043 }