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