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