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