]> git.ozlabs.org Git - ppp.git/blob - pppd/sys-str.c
use ccp_test, open in silent mode if we don't want any compression.
[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 <fcntl.h>
29 #include <string.h>
30 #include <time.h>
31 #include <utmp.h>
32 #include <poll.h>
33 #include <sys/ioctl.h>
34 #include <sys/types.h>
35 #include <sys/socket.h>
36 #include <sys/time.h>
37 #include <sys/stream.h>
38 #include <sys/stropts.h>
39
40 #include <net/if.h>
41 #include <net/route.h>
42 #include <net/if_arp.h>
43 #include <netinet/in.h>
44
45 #include "pppd.h"
46 #include "ppp.h"
47 #include <net/ppp_str.h>
48
49 #ifndef ifr_mtu
50 #define ifr_mtu         ifr_metric
51 #endif
52
53 #define MAXMODULES      10      /* max number of module names to save */
54 static struct   modlist {
55     char        modname[FMNAMESZ+1];
56 } str_modules[MAXMODULES];
57 static int      str_module_count = 0;
58 static int      pushed_ppp;
59 static int      closed_stdio;
60
61 extern int      hungup;         /* has the physical layer been disconnected? */
62 extern int      kdebugflag;
63 extern int      default_device;
64 extern int      restore_term;
65
66 #define PAI_FLAGS_B7_0          0x100
67 #define PAI_FLAGS_B7_1          0x200
68 #define PAI_FLAGS_PAR_EVEN      0x400
69 #define PAI_FLAGS_PAR_ODD       0x800
70 #define PAI_FLAGS_HIBITS        0xF00
71
72 /*
73  * daemon - Detach us from the terminal session.
74  */
75 int
76 daemon(nochdir, noclose)
77     int nochdir, noclose;
78 {
79     int pid;
80
81     if ((pid = fork()) < 0)
82         return -1;
83     if (pid != 0)
84         exit(0);                /* parent dies */
85     setsid();
86     if (!nochdir)
87         chdir("/");
88     if (!noclose) {
89         fclose(stdin);          /* don't need stdin, stdout, stderr */
90         fclose(stdout);
91         fclose(stderr);
92     }
93     return 0;
94 }
95
96
97 /*
98  * ppp_available - check if this kernel supports PPP.
99  */
100 int
101 ppp_available()
102 {
103     int fd, ret;
104
105     fd = open("/dev/tty", O_RDONLY, 0);
106     if (fd < 0)
107         return 1;               /* can't find out - assume we have ppp */
108     ret = ioctl(fd, I_FIND, "pppasync") >= 0;
109     close(fd);
110     return ret;
111 }
112
113
114 /*
115  * establish_ppp - Turn the serial port into a ppp interface.
116  */
117 void
118 establish_ppp()
119 {
120     /* go through and save the name of all the modules, then pop em */
121     for (;;) { 
122         if (ioctl(fd, I_LOOK, str_modules[str_module_count].modname) < 0 ||
123             ioctl(fd, I_POP, 0) < 0)
124             break;
125         MAINDEBUG((LOG_DEBUG, "popped stream module : %s",
126                    str_modules[str_module_count].modname));
127         str_module_count++;
128     }
129
130     /* now push the async/fcs module */
131     if (ioctl(fd, I_PUSH, "pppasync") < 0) {
132         syslog(LOG_ERR, "ioctl(I_PUSH, ppp_async): %m");
133         die(1);
134     }
135     /* finally, push the ppp_if module that actually handles the */
136     /* network interface */ 
137     if (ioctl(fd, I_PUSH, "pppif") < 0) {
138         syslog(LOG_ERR, "ioctl(I_PUSH, ppp_if): %m");
139         die(1);
140     }
141     pushed_ppp = 1;
142 #if 0
143     if (ioctl(fd, I_SETSIG, S_INPUT) < 0) {
144         syslog(LOG_ERR, "ioctl(I_SETSIG, S_INPUT): %m");
145         die(1);
146     }
147 #endif
148     /* read mode, message non-discard mode */
149     if (ioctl(fd, I_SRDOPT, RMSGN) < 0) {
150         syslog(LOG_ERR, "ioctl(I_SRDOPT, RMSGN): %m");
151         die(1);
152     }
153 #if 0
154     /* Flush any waiting messages, or we'll never get SIGPOLL */
155     if (ioctl(fd, I_FLUSH, FLUSHRW) < 0) {
156         syslog(LOG_ERR, "ioctl(I_FLUSH, FLUSHRW): %m");
157         die(1);
158     }
159 #endif
160     /*
161      * Find out which interface we were given.
162      * (ppp_if handles this ioctl)
163      */
164     if (ioctl(fd, SIOCGETU, &ifunit) < 0) {
165         syslog(LOG_ERR, "ioctl(SIOCGETU): %m");
166         die(1);
167     }
168
169     /* Set debug flags in driver */
170     if (ioctl(fd, SIOCSIFDEBUG, &kdebugflag) < 0) {
171         syslog(LOG_ERR, "ioctl(SIOCSIFDEBUG): %m");
172     }
173
174     /* close stdin, stdout, stderr if they might refer to the device */
175     if (default_device && !closed_stdio) {
176         int i;
177
178         for (i = 0; i <= 2; ++i)
179             if (i != fd && i != s)
180                 close(i);
181         closed_stdio = 1;
182     }
183 }
184
185 /*
186  * disestablish_ppp - Restore the serial port to normal operation.
187  * It attempts to reconstruct the stream with the previously popped
188  * modules.  This shouldn't call die() because it's called from die().
189  */
190 void
191 disestablish_ppp()
192 {
193     int flags;
194     char *s;
195
196     if (hungup) {
197         /* we can't push or pop modules after the stream has hung up */
198         str_module_count = 0;
199         restore_term = 0;       /* nor can we fix up terminal settings */
200         return;
201     }
202
203     if (pushed_ppp) {
204         /*
205          * Check whether the link seems not to be 8-bit clean.
206          */
207         if (ioctl(fd, SIOCGIFDEBUG, (caddr_t) &flags) == 0) {
208             s = NULL;
209             switch (~flags & PAI_FLAGS_HIBITS) {
210             case PAI_FLAGS_B7_0:
211                 s = "bit 7 set to 1";
212                 break;
213             case PAI_FLAGS_B7_1:
214                 s = "bit 7 set to 0";
215                 break;
216             case PAI_FLAGS_PAR_EVEN:
217                 s = "odd parity";
218                 break;
219             case PAI_FLAGS_PAR_ODD:
220                 s = "even parity";
221                 break;
222             }
223             if (s != NULL) {
224                 syslog(LOG_WARNING, "Serial link is not 8-bit clean:");
225                 syslog(LOG_WARNING, "All received characters had %s", s);
226             }
227         }
228     }
229
230     while (ioctl(fd, I_POP, 0) == 0)    /* pop any we pushed */
231         ;
232     pushed_ppp = 0;
233   
234     for (; str_module_count > 0; str_module_count--) {
235         if (ioctl(fd, I_PUSH, str_modules[str_module_count-1].modname)) {
236             if (errno != ENXIO)
237                 syslog(LOG_WARNING, "str_restore: couldn't push module %s: %m",
238                        str_modules[str_module_count-1].modname);
239         } else {
240             MAINDEBUG((LOG_INFO, "str_restore: pushed module %s",
241                        str_modules[str_module_count-1].modname));
242         }
243     }
244 }
245
246
247 /*
248  * output - Output PPP packet.
249  */
250 void
251 output(unit, p, len)
252     int unit;
253     u_char *p;
254     int len;
255 {
256     struct strbuf       str;
257
258     if (unit != 0)
259         MAINDEBUG((LOG_WARNING, "output: unit != 0!"));
260     if (debug)
261         log_packet(p, len, "sent ");
262
263     str.len = len;
264     str.buf = (caddr_t) p;
265     if (putmsg(fd, NULL, &str, 0) < 0) {
266         if (errno != ENXIO) {
267             syslog(LOG_ERR, "putmsg: %m");
268             die(1);
269         }
270     }
271 }
272
273
274 wait_input(timo)
275     struct timeval *timo;
276 {
277     int t;
278     struct pollfd pfd;
279
280     t = timo == NULL? -1: timo->tv_sec * 1000 + timo->tv_usec / 1000;
281     pfd.fd = fd;
282     pfd.events = POLLIN | POLLPRI | POLLHUP;
283     if (poll(&pfd, 1, t) < 0 && errno != EINTR) {
284         syslog(LOG_ERR, "poll: %m");
285         die(1);
286     }
287 }
288
289 /*
290  * read_packet - get a PPP packet from the serial device.
291  */
292 int
293 read_packet(buf)
294     u_char *buf;
295 {
296     struct strbuf str, ctl;
297     int len, i;
298     unsigned char ctlbuf[16];
299
300     str.maxlen = MTU+DLLHEADERLEN;
301     str.buf = (caddr_t) buf;
302     ctl.maxlen = sizeof(ctlbuf);
303     ctl.buf = (caddr_t) ctlbuf;
304     i = 0;
305     len = getmsg(fd, &ctl, &str, &i);
306     if (len < 0) {
307         if (errno == EAGAIN || errno == EWOULDBLOCK) {
308             return -1;
309         }
310         syslog(LOG_ERR, "getmsg(fd) %m");
311         die(1);
312     }
313     if (len) 
314         MAINDEBUG((LOG_DEBUG, "getmsg returned 0x%x",len));
315     if (ctl.len > 0)
316         syslog(LOG_NOTICE, "got ctrl msg len %d %x %x\n", ctl.len,
317                ctlbuf[0], ctlbuf[1]);
318
319     if (str.len < 0) {
320         MAINDEBUG((LOG_DEBUG, "getmsg short return length %d", str.len));
321         return -1;
322     }
323
324     return str.len;
325 }
326
327
328 /*
329  * ppp_send_config - configure the transmit characteristics of
330  * the ppp interface.
331  */
332 void
333 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
334     int unit, mtu;
335     u_long asyncmap;
336     int pcomp, accomp;
337 {
338     char c;
339     struct ifreq ifr;
340
341     strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
342     ifr.ifr_mtu = mtu;
343     if (ioctl(s, SIOCSIFMTU, (caddr_t) &ifr) < 0) {
344         syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m");
345         quit();
346     }
347
348     if(ioctl(fd, SIOCSIFASYNCMAP, (caddr_t) &asyncmap) < 0) {
349         syslog(LOG_ERR, "ioctl(SIOCSIFASYNCMAP): %m");
350         quit();
351     }
352
353     c = (pcomp? 1: 0);
354     if(ioctl(fd, SIOCSIFCOMPPROT, &c) < 0) {
355         syslog(LOG_ERR, "ioctl(SIOCSIFCOMPPROT): %m");
356         quit();
357     }
358
359     c = (accomp? 1: 0);
360     if(ioctl(fd, SIOCSIFCOMPAC, &c) < 0) {
361         syslog(LOG_ERR, "ioctl(SIOCSIFCOMPAC): %m");
362         quit();
363     }
364 }
365
366
367 /*
368  * ppp_set_xaccm - set the extended transmit ACCM for the interface.
369  */
370 void
371 ppp_set_xaccm(unit, accm)
372     int unit;
373     ext_accm accm;
374 {
375     if (ioctl(fd, SIOCSIFXASYNCMAP, accm) < 0 && errno != ENOTTY)
376         syslog(LOG_WARNING, "ioctl(set extended ACCM): %m");
377 }
378
379
380 /*
381  * ppp_recv_config - configure the receive-side characteristics of
382  * the ppp interface.
383  */
384 void
385 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
386     int unit, mru;
387     u_long asyncmap;
388     int pcomp, accomp;
389 {
390     char c;
391
392     if (ioctl(fd, SIOCSIFMRU, &mru) < 0) {
393         syslog(LOG_ERR, "ioctl(SIOCSIFMRU): %m");
394     }
395
396     if (ioctl(fd, SIOCSIFRASYNCMAP, (caddr_t) &asyncmap) < 0) {
397         syslog(LOG_ERR, "ioctl(SIOCSIFRASYNCMAP): %m");
398     }
399
400     c = 2 + (pcomp? 1: 0);
401     if(ioctl(fd, SIOCSIFCOMPPROT, &c) < 0) {
402         syslog(LOG_ERR, "ioctl(SIOCSIFCOMPPROT): %m");
403     }
404
405     c = 2 + (accomp? 1: 0);
406     if (ioctl(fd, SIOCSIFCOMPAC, &c) < 0) {
407         syslog(LOG_ERR, "ioctl(SIOCSIFCOMPAC): %m");
408     }
409 }
410
411 /*
412  * ccp_flags_set - inform kernel about the current state of CCP.
413  */
414 void
415 ccp_flags_set(unit, isopen, isup)
416     int unit, isopen, isup;
417 {
418     int x;
419
420 #if 0
421     x = (isopen? 1: 0) + (isup? 2: 0);
422     if (ioctl(fd, SIOCSIFCOMP, (caddr_t) &x) < 0)
423         syslog(LOG_ERR, "ioctl (SIOCSIFCOMP): %m");
424 #endif
425 }
426
427 /*
428  * sifvjcomp - config tcp header compression
429  */
430 int
431 sifvjcomp(u, vjcomp, cidcomp, maxcid)
432     int u, vjcomp, cidcomp, maxcid;
433 {
434     char x;
435
436     x = (vjcomp? 1: 0) + (cidcomp? 0: 2) + (maxcid << 4);
437     if (ioctl(fd, SIOCSIFVJCOMP, (caddr_t) &x) < 0) {
438         syslog(LOG_ERR, "ioctl(SIOCSIFVJCOMP): %m");
439         return 0;
440     }
441     return 1;
442 }
443
444 /*
445  * sifup - Config the interface up.
446  */
447 int
448 sifup(u)
449     int u;
450 {
451     struct ifreq ifr;
452
453     strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
454     if (ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
455         syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
456         return 0;
457     }
458     ifr.ifr_flags |= IFF_UP;
459     if (ioctl(s, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
460         syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
461         return 0;
462     }
463     return 1;
464 }
465
466 /*
467  * sifdown - Config the interface down.
468  */
469 int
470 sifdown(u)
471     int u;
472 {
473     struct ifreq ifr;
474     strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
475     if (ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
476         syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
477         return 0;
478     }
479     ifr.ifr_flags &= ~IFF_UP;
480     if (ioctl(s, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
481         syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
482         return 0;
483     }
484     return 1;
485 }
486
487 /*
488  * SET_SA_FAMILY - initialize a struct sockaddr, setting the sa_family field.
489  */
490 #define SET_SA_FAMILY(addr, family)             \
491     BZERO((char *) &(addr), sizeof(addr));      \
492     addr.sa_family = (family);
493
494 /*
495  * sifaddr - Config the interface IP addresses and netmask.
496  */
497 int
498 sifaddr(u, o, h, m)
499     int u;
500     u_long o, h, m;
501 {
502     int ret;
503     struct ifreq ifr;
504
505     ret = 1;
506     strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
507     SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
508     ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
509     if (ioctl(s, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
510         syslog(LOG_ERR, "ioctl(SIOCSIFADDR): %m");
511         ret = 0;
512     }
513     ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = h;
514     if (ioctl(s, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
515         syslog(LOG_ERR, "ioctl(SIOCSIFDSTADDR): %m");
516         ret = 0;
517     }
518     if (m != 0) {
519         ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = m;
520         syslog(LOG_INFO, "Setting interface mask to %s\n", ip_ntoa(m));
521         if (ioctl(s, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
522             syslog(LOG_ERR, "ioctl(SIOCSIFNETMASK): %m");
523             ret = 0;
524         }
525     }
526     return ret;
527 }
528
529 /*
530  * cifaddr - Clear the interface IP addresses, and delete routes
531  * through the interface if possible.
532  */
533 int
534 cifaddr(u, o, h)
535     int u;
536     u_long o, h;
537 {
538     struct rtentry rt;
539
540     SET_SA_FAMILY(rt.rt_dst, AF_INET);
541     ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = h;
542     SET_SA_FAMILY(rt.rt_gateway, AF_INET);
543     ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = o;
544     rt.rt_flags = RTF_HOST;
545     if (ioctl(s, SIOCDELRT, (caddr_t) &rt) < 0) {
546         syslog(LOG_ERR, "ioctl(SIOCDELRT): %m");
547         return 0;
548     }
549     return 1;
550 }
551
552 /*
553  * sifdefaultroute - assign a default route through the address given.
554  */
555 int
556 sifdefaultroute(u, g)
557     int u;
558     u_long g;
559 {
560     struct rtentry rt;
561
562     SET_SA_FAMILY(rt.rt_dst, AF_INET);
563     SET_SA_FAMILY(rt.rt_gateway, AF_INET);
564     ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
565     rt.rt_flags = RTF_GATEWAY;
566     if (ioctl(s, SIOCADDRT, &rt) < 0) {
567         syslog(LOG_ERR, "default route ioctl(SIOCADDRT): %m");
568         return 0;
569     }
570     return 1;
571 }
572
573 /*
574  * cifdefaultroute - delete a default route through the address given.
575  */
576 int
577 cifdefaultroute(u, g)
578     int u;
579     u_long g;
580 {
581     struct rtentry rt;
582
583     SET_SA_FAMILY(rt.rt_dst, AF_INET);
584     SET_SA_FAMILY(rt.rt_gateway, AF_INET);
585     ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
586     rt.rt_flags = RTF_GATEWAY;
587     if (ioctl(s, SIOCDELRT, &rt) < 0) {
588         syslog(LOG_ERR, "default route ioctl(SIOCDELRT): %m");
589         return 0;
590     }
591     return 1;
592 }
593
594 /*
595  * sifproxyarp - Make a proxy ARP entry for the peer.
596  */
597 int
598 sifproxyarp(unit, hisaddr)
599     int unit;
600     u_long hisaddr;
601 {
602     struct arpreq arpreq;
603
604     BZERO(&arpreq, sizeof(arpreq));
605
606     /*
607      * Get the hardware address of an interface on the same subnet
608      * as our local address.
609      */
610     if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) {
611         syslog(LOG_WARNING, "Cannot determine ethernet address for proxy ARP");
612         return 0;
613     }
614
615     SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
616     ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
617     arpreq.arp_flags = ATF_PERM | ATF_PUBL;
618     if (ioctl(s, SIOCSARP, (caddr_t)&arpreq) < 0) {
619         syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
620         return 0;
621     }
622
623     return 1;
624 }
625
626 /*
627  * cifproxyarp - Delete the proxy ARP entry for the peer.
628  */
629 int
630 cifproxyarp(unit, hisaddr)
631     int unit;
632     u_long hisaddr;
633 {
634     struct arpreq arpreq;
635
636     BZERO(&arpreq, sizeof(arpreq));
637     SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
638     ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
639     if (ioctl(s, SIOCDARP, (caddr_t)&arpreq) < 0) {
640         syslog(LOG_ERR, "ioctl(SIOCDARP): %m");
641         return 0;
642     }
643     return 1;
644 }
645
646 /*
647  * get_ether_addr - get the hardware address of an interface on the
648  * the same subnet as ipaddr.  Code borrowed from myetheraddr.c
649  * in the cslip-2.6 distribution, which is subject to the following
650  * copyright notice (which also applies to logwtmp below):
651  *
652  * Copyright (c) 1990, 1992 The Regents of the University of California.
653  * All rights reserved.
654  *
655  * Redistribution and use in source and binary forms, with or without
656  * modification, are permitted provided that: (1) source code distributions
657  * retain the above copyright notice and this paragraph in its entirety, (2)
658  * distributions including binary code include the above copyright notice and
659  * this paragraph in its entirety in the documentation or other materials
660  * provided with the distribution, and (3) all advertising materials mentioning
661  * features or use of this software display the following acknowledgement:
662  * ``This product includes software developed by the University of California,
663  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
664  * the University nor the names of its contributors may be used to endorse
665  * or promote products derived from this software without specific prior
666  * written permission.
667  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
668  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
669  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
670  */
671
672 #include <fcntl.h>
673 #include <nlist.h>
674 #include <kvm.h>
675 #include <arpa/inet.h>
676
677 /* XXX SunOS 4.1 defines this and 3.5 doesn't... */
678 #ifdef _nlist_h
679 #define SUNOS4
680 #endif
681
682 #ifdef SUNOS4
683 #include <netinet/in_var.h>
684 #endif
685 #include <netinet/if_ether.h>
686
687 /* Cast a struct sockaddr to a structaddr_in */
688 #define SATOSIN(sa) ((struct sockaddr_in *)(sa))
689
690 /* Determine if "bits" is set in "flag" */
691 #define ALLSET(flag, bits) (((flag) & (bits)) == (bits))
692
693 static struct nlist nl[] = {
694 #define N_IFNET 0
695         { "_ifnet" },
696         { 0 }
697 };
698
699 static void kread();
700
701 int
702 get_ether_addr(ipaddr, hwaddr)
703     u_long ipaddr;
704     struct sockaddr *hwaddr;
705 {
706     register kvm_t *kd;
707     register struct ifnet *ifp;
708     register struct arpcom *ac;
709     struct arpcom arpcom;
710     struct in_addr *inp;
711 #ifdef SUNOS4
712     register struct ifaddr *ifa;
713     register struct in_ifaddr *in;
714     union {
715         struct ifaddr ifa;
716         struct in_ifaddr in;
717     } ifaddr;
718 #endif
719     u_long addr, mask;
720
721     /* Open kernel memory for reading */
722     kd = kvm_open(0, 0, 0, O_RDONLY, NULL);
723     if (kd == 0) {
724         syslog(LOG_ERR, "kvm_open: %m");
725         return 0;
726     }
727
728     /* Fetch namelist */
729     if (kvm_nlist(kd, nl) != 0) {
730         syslog(LOG_ERR, "kvm_nlist failed");
731         return 0;
732     }
733
734     ac = &arpcom;
735     ifp = &arpcom.ac_if;
736 #ifdef SUNOS4
737     ifa = &ifaddr.ifa;
738     in = &ifaddr.in;
739 #endif
740
741     if (kvm_read(kd, nl[N_IFNET].n_value, (char *)&addr, sizeof(addr))
742         != sizeof(addr)) {
743         syslog(LOG_ERR, "error reading ifnet addr");
744         return 0;
745     }
746     for ( ; addr; addr = (u_long)ifp->if_next) {
747         if (kvm_read(kd, addr, (char *)ac, sizeof(*ac)) != sizeof(*ac)) {
748             syslog(LOG_ERR, "error reading ifnet");
749             return 0;
750         }
751
752         /* Only look at configured, broadcast interfaces */
753         if (!ALLSET(ifp->if_flags, IFF_UP | IFF_BROADCAST))
754             continue;
755 #ifdef SUNOS4
756         /* This probably can't happen... */
757         if (ifp->if_addrlist == 0)
758             continue;
759 #endif
760
761         /* Get interface ip address */
762 #ifdef SUNOS4
763         if (kvm_read(kd, (u_long)ifp->if_addrlist, (char *)&ifaddr,
764                      sizeof(ifaddr)) != sizeof(ifaddr)) {
765             syslog(LOG_ERR, "error reading ifaddr");
766             return 0;
767         }
768         inp = &SATOSIN(&ifa->ifa_addr)->sin_addr;
769 #else
770         inp = &SATOSIN(&ifp->if_addr)->sin_addr;
771 #endif
772
773         /* Check if this interface on the right subnet */
774 #ifdef SUNOS4
775         mask = in->ia_subnetmask;
776 #else
777         mask = ifp->if_subnetmask;
778 #endif
779         if ((ipaddr & mask) != (inp->s_addr & mask))
780             continue;
781
782         /* Copy out the local ethernet address */
783         hwaddr->sa_family = AF_UNSPEC;
784         BCOPY((caddr_t) &arpcom.ac_enaddr, hwaddr->sa_data,
785               sizeof(arpcom.ac_enaddr));
786         return 1;               /* success! */
787     }
788
789     /* couldn't find one */
790     return 0;
791 }
792
793 #define WTMPFILE        "/usr/adm/wtmp"
794
795 int
796 logwtmp(line, name, host)
797     char *line, *name, *host;
798 {
799     int fd;
800     struct stat buf;
801     struct utmp ut;
802
803     if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0)
804         return;
805     if (!fstat(fd, &buf)) {
806         (void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
807         (void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
808         (void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
809         (void)time(&ut.ut_time);
810         if (write(fd, (char *)&ut, sizeof(struct utmp)) != sizeof(struct utmp))
811             (void)ftruncate(fd, buf.st_size);
812     }
813     close(fd);
814 }