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