2 * System-dependent procedures for pppd under Solaris 2.
4 * Copyright (c) 1994 The Australian National University.
7 * Permission to use, copy, modify, and distribute this software and its
8 * documentation is hereby granted, provided that the above copyright
9 * notice appears in all copies. This software is provided without any
10 * warranty, express or implied. The Australian National University
11 * makes no representations about the suitability of this software for
14 * IN NO EVENT SHALL THE AUSTRALIAN NATIONAL UNIVERSITY BE LIABLE TO ANY
15 * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
16 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
17 * THE AUSTRALIAN NATIONAL UNIVERSITY HAVE BEEN ADVISED OF THE POSSIBILITY
20 * THE AUSTRALIAN NATIONAL UNIVERSITY SPECIFICALLY DISCLAIMS ANY WARRANTIES,
21 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
22 * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
23 * ON AN "AS IS" BASIS, AND THE AUSTRALIAN NATIONAL UNIVERSITY HAS NO
24 * OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
29 static char rcsid[] = "$Id: sys-svr4.c,v 1.1 1995/05/19 03:08:27 paulus Exp $";
39 #include <sys/types.h>
40 #include <sys/ioccom.h>
41 #include <sys/stream.h>
42 #include <sys/stropts.h>
43 #include <sys/socket.h>
44 #include <sys/sockio.h>
45 #include <sys/syslog.h>
46 #include <sys/systeminfo.h>
49 #include <net/ppp_defs.h>
50 #include <net/pppio.h>
51 #include <netinet/in.h>
56 static int fdmuxid = -1;
58 static int ipmuxid = -1;
60 static int restore_term;
61 static struct termios inittermios;
63 static int link_mtu, link_mru;
66 static int tty_nmodules;
67 static char tty_modules[NMODULES][FMNAMESZ+1];
70 * sys_init - System-dependent initialization.
75 openlog("pppd", LOG_PID | LOG_NDELAY, LOG_PPP);
76 setlogmask(LOG_UPTO(LOG_INFO));
78 setlogmask(LOG_UPTO(LOG_DEBUG));
80 ipfd = open("/dev/ip", O_RDWR, 0);
82 syslog(LOG_ERR, "Couldn't open IP device: %m");
88 * daemon - Detach us from controlling terminal session.
91 daemon(nochdir, noclose)
96 if ((pid = fork()) < 0)
99 exit(0); /* parent dies */
104 fclose(stdin); /* don't need stdin, stdout, stderr */
112 * note_debug_level - note a change in the debug level.
118 syslog(LOG_INFO, "Debug turned ON, Level %d", debug);
119 setlogmask(LOG_UPTO(LOG_DEBUG));
121 setlogmask(LOG_UPTO(LOG_WARNING));
126 * ppp_available - check whether the system has any ppp interfaces
131 return access("/dev/ppp", R_OK) >= 0;
135 * establish_ppp - Turn the serial port into a ppp interface.
142 pppfd = open("/dev/ppp", O_RDWR | O_NONBLOCK, 0);
144 syslog(LOG_ERR, "Can't open /dev/ppp: %m");
148 /* Assign a new PPA and get its unit number. */
149 if (strioctl(pppfd, PPPIO_NEWPPA, &ifunit, 0, sizeof(int)) < 0) {
150 syslog(LOG_ERR, "Can't create new PPP interface: %m");
154 /* Pop any existing modules off the tty stream. */
156 if (ioctl(fd, I_LOOK, tty_modules[i]) < 0
157 || ioctl(fd, I_POP, 0) < 0)
161 /* Push the async hdlc module and the compressor module. */
162 if (ioctl(fd, I_PUSH, "ppp_ahdl") < 0) {
163 syslog(LOG_ERR, "Couldn't push PPP Async HDLC module: %m");
166 if (ioctl(fd, I_PUSH, "ppp_comp") < 0) {
167 syslog(LOG_ERR, "Couldn't push PPP compression module: %m");
171 /* Link the serial port under the PPP multiplexor. */
172 if ((fdmuxid = ioctl(pppfd, I_LINK, fd)) < 0) {
173 syslog(LOG_ERR, "Can't link tty to PPP mux: %m");
179 * disestablish_ppp - Restore the serial port to normal operation.
180 * This shouldn't call die() because it's called from die().
188 if (ioctl(pppfd, I_UNLINK, fdmuxid) < 0) {
189 syslog(LOG_ERR, "Can't unlink tty from PPP mux: %m");
194 while (ioctl(fd, I_POP, 0) >= 0)
196 for (i = tty_nmodules - 1; i >= 0; --i)
197 if (ioctl(fd, I_PUSH, tty_modules[i]) < 0)
198 syslog(LOG_ERR, "Couldn't restore tty module %s: %m",
205 * List of valid speeds.
208 int speed_int, speed_val;
280 * Translate from bits/second to a speed_t.
286 struct speed *speedp;
290 for (speedp = speeds; speedp->speed_int; speedp++)
291 if (bps == speedp->speed_int)
292 return speedp->speed_val;
293 syslog(LOG_WARNING, "speed %d not supported", bps);
298 * Translate from a speed_t to bits/second.
304 struct speed *speedp;
308 for (speedp = speeds; speedp->speed_int; speedp++)
309 if (speed == speedp->speed_val)
310 return speedp->speed_int;
315 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
316 * at the requested speed, etc. If `local' is true, set CLOCAL
317 * regardless of whether the modem option was specified.
319 set_up_tty(fd, local)
325 if (tcgetattr(fd, &tios) < 0) {
326 syslog(LOG_ERR, "tcgetattr: %m");
333 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
335 tios.c_cflag |= CRTSCTS;
336 else if (crtscts < 0)
337 tios.c_cflag &= ~CRTSCTS;
339 tios.c_cflag |= CS8 | CREAD | HUPCL;
341 tios.c_cflag |= CLOCAL;
342 tios.c_iflag = IGNBRK | IGNPAR;
346 tios.c_cc[VTIME] = 0;
349 tios.c_iflag |= IXOFF;
350 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
351 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
354 speed = translate_speed(inspeed);
356 cfsetospeed(&tios, speed);
357 cfsetispeed(&tios, speed);
359 speed = cfgetospeed(&tios);
361 * We can't proceed if the serial port speed is 0,
362 * since that implies that the serial port is disabled.
365 syslog(LOG_ERR, "Baud rate for %s is 0; need explicit baud rate",
371 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
372 syslog(LOG_ERR, "tcsetattr: %m");
376 baud_rate = inspeed = baud_rate_of(speed);
381 * restore_tty - restore the terminal to the saved settings.
387 if (!default_device) {
389 * Turn off echoing, because otherwise we can get into
390 * a loop with the tty and the modem echoing to each other.
391 * We presume we are the sole user of this tty device, so
392 * when we close it, it will revert to its defaults anyway.
394 inittermios.c_lflag &= ~(ECHO | ECHONL);
396 if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
398 syslog(LOG_WARNING, "tcsetattr: %m");
404 * setdtr - control the DTR line on the serial port.
405 * This is called from die(), so it shouldn't call die().
410 int modembits = TIOCM_DTR;
412 ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
416 * output - Output PPP packet.
427 log_packet(p, len, "sent ");
430 data.buf = (caddr_t) p;
431 if (putmsg(pppfd, NULL, &data, 0) < 0) {
432 if (errno != ENXIO) {
433 syslog(LOG_ERR, "Couldn't send packet: %m");
440 * wait_input - wait until there is data available on fd,
441 * for the length of time specified by *timo (indefinite
445 struct timeval *timo;
450 t = timo == NULL? -1: timo->tv_sec * 1000 + timo->tv_usec / 1000;
452 pfd.events = POLLIN | POLLPRI | POLLHUP;
453 if (poll(&pfd, 1, t) < 0 && errno != EINTR) {
454 syslog(LOG_ERR, "poll: %m");
460 * read_packet - get a PPP packet from the serial device.
466 struct strbuf ctrl, data;
468 unsigned char ctrlbuf[sizeof(union DL_primitives)];
470 data.maxlen = PPP_MRU + PPP_HDRLEN;
471 data.buf = (caddr_t) buf;
472 ctrl.maxlen = sizeof(ctrlbuf);
473 ctrl.buf = (caddr_t) ctrlbuf;
475 len = getmsg(pppfd, &ctrl, &data, &flags);
477 if (errno = EAGAIN || errno == EINTR)
479 syslog(LOG_ERR, "Error reading packet: %m");
485 * Got a M_PROTO or M_PCPROTO message. Interpret it
486 * as a DLPI primitive.
494 * ppp_send_config - configure the transmit characteristics of
498 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
505 if (strioctl(pppfd, PPPIO_MTU, &mtu, sizeof(mtu), 0) < 0) {
506 syslog(LOG_ERR, "Couldn't set MTU: %m");
508 if (strioctl(pppfd, PPPIO_XACCM, &asyncmap, sizeof(asyncmap), 0) < 0) {
509 syslog(LOG_ERR, "Couldn't set transmit ACCM: %m");
511 cf[0] = (pcomp? COMP_PROT: 0) + (accomp? COMP_AC: 0);
512 cf[1] = COMP_PROT | COMP_AC;
513 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
514 syslog(LOG_ERR, "Couldn't set prot/AC compression: %m");
520 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
523 ppp_set_xaccm(unit, accm)
527 if (strioctl(pppfd, PPPIO_XACCM, accm, sizeof(ext_accm), 0) < 0) {
528 syslog(LOG_WARNING, "Couldn't set extended ACCM: %m");
533 * ppp_recv_config - configure the receive-side characteristics of
537 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
544 if (strioctl(pppfd, PPPIO_MRU, &mru, sizeof(mru), 0) < 0) {
545 syslog(LOG_ERR, "Couldn't set MRU: %m");
547 if (strioctl(pppfd, PPPIO_RACCM, &asyncmap, sizeof(asyncmap), 0) < 0) {
548 syslog(LOG_ERR, "Couldn't set receive ACCM: %m");
550 cf[0] = (pcomp? DECOMP_PROT: 0) + (accomp? DECOMP_AC: 0);
551 cf[1] = DECOMP_PROT | DECOMP_AC;
552 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
553 syslog(LOG_ERR, "Couldn't set prot/AC decompression: %m");
559 * ccp_test - ask kernel whether a given compression method
560 * is acceptable for use.
562 ccp_test(unit, opt_ptr, opt_len, for_transmit)
563 int unit, opt_len, for_transmit;
566 return strioctl(pppfd, (for_transmit? PPPIO_XCOMP: PPPIO_RCOMP),
567 opt_ptr, opt_len, 0) >= 0;
571 * ccp_flags_set - inform kernel about the current state of CCP.
574 ccp_flags_set(unit, isopen, isup)
575 int unit, isopen, isup;
579 cf[0] = (isopen? CCP_ISOPEN: 0) + (isup? CCP_ISUP: 0);
580 cf[1] = CCP_ISOPEN | CCP_ISUP | CCP_ERROR | CCP_FATALERROR;
581 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
582 syslog(LOG_ERR, "Couldn't set kernel CCP state: %m");
587 * ccp_fatal_error - returns 1 if decompression was disabled as a
588 * result of an error detected after decompression of a packet,
589 * 0 otherwise. This is necessary because of patent nonsense.
592 ccp_fatal_error(unit)
598 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
599 syslog(LOG_ERR, "Couldn't get compression flags: %m");
602 return cf[0] & CCP_FATALERROR;
606 * sifvjcomp - config tcp header compression
609 sifvjcomp(u, vjcomp, xcidcomp, xmaxcid)
610 int u, vjcomp, xcidcomp, xmaxcid;
616 maxcid[0] = xcidcomp;
617 maxcid[1] = 15; /* XXX should be rmaxcid */
618 if (strioctl(pppfd, PPPIO_VJINIT, maxcid, sizeof(maxcid), 0) < 0) {
619 syslog(LOG_ERR, "Couldn't initialize VJ compression: %m");
623 cf[0] = (vjcomp? COMP_VJC + DECOMP_VJC: 0) /* XXX this is wrong */
624 + (xcidcomp? COMP_VJCCID + DECOMP_VJCCID: 0);
625 cf[1] = COMP_VJC + DECOMP_VJC + COMP_VJCCID + DECOMP_VJCCID;
626 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
628 syslog(LOG_ERR, "Couldn't enable VJ compression: %m");
635 * sifup - Config the interface up and enable IP packets to pass.
644 syslog(LOG_DEBUG, "sifup: not plumbed!");
647 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
648 if (ioctl(ipfd, SIOCGIFFLAGS, &ifr) < 0) {
649 syslog(LOG_ERR, "Couldn't mark interface up (get): %m");
652 ifr.ifr_flags |= IFF_UP;
653 if (ioctl(ipfd, SIOCSIFFLAGS, &ifr) < 0) {
654 syslog(LOG_ERR, "Couldn't mark interface up (set): %m");
661 * sifdown - Config the interface down and disable IP.
671 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
672 if (ioctl(ipfd, SIOCGIFFLAGS, &ifr) < 0) {
673 syslog(LOG_ERR, "Couldn't mark interface down (get): %m");
676 ifr.ifr_flags &= ~IFF_UP;
677 if (ioctl(ipfd, SIOCSIFFLAGS, &ifr) < 0) {
678 syslog(LOG_ERR, "Couldn't mark interface down (set): %m");
685 * sifaddr - Config the interface IP addresses and netmask.
695 fd = open("/dev/ppp", O_RDWR, 0);
697 syslog(LOG_ERR, "Can't open /dev/ppp (2): %m");
700 if (ioctl(fd, I_PUSH, "ip") < 0) {
701 syslog(LOG_ERR, "Can't push IP module: %m");
705 ipmuxid = ioctl(ipfd, I_LINK, fd);
708 syslog(LOG_ERR, "Can't link PPP device to IP: %m");
712 memset(&ifr, 0, sizeof(ifr));
713 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
714 ifr.ifr_addr.sa_family = AF_INET;
715 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
716 if (ioctl(ipfd, SIOCSIFADDR, &ifr) < 0) {
717 syslog(LOG_ERR, "Couldn't set local IP address: %m");
719 ifr.ifr_dstaddr.sa_family = AF_INET;
720 ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = h;
721 if (ioctl(ipfd, SIOCSIFDSTADDR, &ifr) < 0) {
722 syslog(LOG_ERR, "Couldn't set remote IP address: %m");
724 ifr.ifr_addr.sa_family = AF_INET;
725 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = m;
726 if (ioctl(ipfd, SIOCSIFNETMASK, &ifr) < 0) {
727 syslog(LOG_ERR, "Couldn't set IP netmask: %m");
729 ifr.ifr_metric = link_mtu;
730 if (ioctl(ipfd, SIOCSIFMTU, &ifr) < 0) {
731 syslog(LOG_ERR, "Couldn't set IP MTU: %m");
738 * cifaddr - Clear the interface IP addresses, and delete routes
739 * through the interface if possible.
747 if (ioctl(ipfd, I_UNLINK, ipmuxid) < 0) {
748 syslog(LOG_ERR, "Can't remove ppp interface unit: %m");
757 * sifdefaultroute - assign a default route through the address given.
760 sifdefaultroute(u, g)
768 * cifdefaultroute - delete a default route through the address given.
771 cifdefaultroute(u, g)
779 * sifproxyarp - Make a proxy ARP entry for the peer.
782 sifproxyarp(unit, hisaddr)
790 * cifproxyarp - Delete the proxy ARP entry for the peer.
793 cifproxyarp(unit, hisaddr)
801 * get_ether_addr - get the hardware address of an interface on the
802 * the same subnet as ipaddr.
807 get_ether_addr(ipaddr, hwaddr)
809 struct sockaddr_dl *hwaddr;
814 * Return user specified netmask, modified by any mask we might determine
815 * for address `addr' (in network byte order).
816 * Here we scan through the system's list of interfaces, looking for
817 * any non-point-to-point interfaces which might appear to be on the same
818 * network as `addr'. If we find any, we OR in their netmask to the
819 * user-specified netmask.
825 u_int32_t mask, nmask, ina;
826 struct ifreq *ifr, *ifend, ifreq;
831 if (IN_CLASSA(addr)) /* determine network mask for address class */
832 nmask = IN_CLASSA_NET;
833 else if (IN_CLASSB(addr))
834 nmask = IN_CLASSB_NET;
836 nmask = IN_CLASSC_NET;
837 /* class D nets are disallowed by bad_ip_adrs */
838 mask = netmask | htonl(nmask);
841 * Scan through the system's network interfaces.
843 if (ioctl(ipfd, SIOCGIFNUM, &nif) < 0)
845 ifc.ifc_len = nif * sizeof(struct ifreq);
846 ifc.ifc_req = alloca(ifc.ifc_len);
847 if (ifc.ifc_req == 0)
849 if (ioctl(ipfd, SIOCGIFCONF, &ifc) < 0) {
850 syslog(LOG_WARNING, "Couldn't get system interface list: %m");
853 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
854 for (ifr = ifc.ifc_req; ifr < ifend; ++ifr) {
856 * Check the interface's internet address.
858 if (ifr->ifr_addr.sa_family != AF_INET)
860 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
861 if ((ntohl(ina) & nmask) != (addr & nmask))
864 * Check that the interface is up, and not point-to-point or loopback.
866 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
867 if (ioctl(ipfd, SIOCGIFFLAGS, &ifreq) < 0)
869 if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
873 * Get its netmask and OR it into our mask.
875 if (ioctl(ipfd, SIOCGIFNETMASK, &ifreq) < 0)
877 mask |= ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr;
884 * logwtmp - write an accounting record to the /var/adm/wtmp file.
887 logwtmp(line, name, host)
888 char *line, *name, *host;
893 * gethostid - return the serial number of this machine.
900 if (sysinfo(SI_HW_SERIAL, buf, sizeof(buf)) < 0) {
901 syslog(LOG_ERR, "sysinfo: %m");
904 return strtol(buf, NULL, 16);
908 strioctl(fd, cmd, ptr, ilen, olen)
909 int fd, cmd, ilen, olen;
918 if (ioctl(fd, I_STR, &str) == -1)
920 if (str.ic_len != olen)
921 syslog(LOG_DEBUG, "strioctl: expected %d bytes, got %d for cmd %x\n",
922 olen, str.ic_len, cmd);