2 * sys-str.c - System-dependent procedures for setting up
3 * PPP interfaces on systems which use the STREAMS ppp interface.
5 * Copyright (c) 1989 Carnegie Mellon University.
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.
34 #include <sys/types.h>
35 #include <sys/socket.h>
36 #include <sys/sockio.h>
38 #include <sys/stream.h>
39 #include <sys/stropts.h>
42 #include <net/route.h>
43 #include <net/if_arp.h>
44 #include <netinet/in.h>
48 #include <net/ppp_str.h>
51 #define ifr_mtu ifr_metric
54 #define MAXMODULES 10 /* max number of module names to save */
55 static struct modlist {
56 char modname[FMNAMESZ+1];
57 } str_modules[MAXMODULES];
58 static int str_module_count = 0;
59 static int pushed_ppp;
60 static int closed_stdio;
62 static int restore_term; /* 1 => we've munged the terminal */
63 static struct termios inittermios; /* Initial TTY termios */
66 * sys_init - System-dependent initialization.
71 openlog("pppd", LOG_PID | LOG_NDELAY, LOG_PPP);
72 setlogmask(LOG_UPTO(LOG_INFO));
74 setlogmask(LOG_UPTO(LOG_DEBUG));
78 * note_debug_level - note a change in the debug level.
84 syslog(LOG_INFO, "Debug turned ON, Level %d", debug);
85 setlogmask(LOG_UPTO(LOG_DEBUG));
87 setlogmask(LOG_UPTO(LOG_WARNING));
92 * daemon - Detach us from the terminal session.
95 daemon(nochdir, noclose)
100 if ((pid = fork()) < 0)
103 exit(0); /* parent dies */
108 fclose(stdin); /* don't need stdin, stdout, stderr */
117 * ppp_available - check if this kernel supports PPP.
124 fd = open("/dev/tty", O_RDONLY, 0);
126 return 1; /* can't find out - assume we have ppp */
127 ret = ioctl(fd, I_FIND, "pppasync") >= 0;
134 * establish_ppp - Turn the serial port into a ppp interface.
139 /* go through and save the name of all the modules, then pop em */
141 if (ioctl(fd, I_LOOK, str_modules[str_module_count].modname) < 0 ||
142 ioctl(fd, I_POP, 0) < 0)
144 MAINDEBUG((LOG_DEBUG, "popped stream module : %s",
145 str_modules[str_module_count].modname));
149 /* now push the async/fcs module */
150 if (ioctl(fd, I_PUSH, "pppasync") < 0) {
151 syslog(LOG_ERR, "ioctl(I_PUSH, ppp_async): %m");
154 /* push the compress module */
155 if (ioctl(fd, I_PUSH, "pppcomp") < 0) {
156 syslog(LOG_WARNING, "ioctl(I_PUSH, ppp_comp): %m");
158 /* finally, push the ppp_if module that actually handles the */
159 /* network interface */
160 if (ioctl(fd, I_PUSH, "pppif") < 0) {
161 syslog(LOG_ERR, "ioctl(I_PUSH, ppp_if): %m");
165 /* read mode, message non-discard mode */
166 if (ioctl(fd, I_SRDOPT, RMSGN) < 0) {
167 syslog(LOG_ERR, "ioctl(I_SRDOPT, RMSGN): %m");
171 * Find out which interface we were given.
172 * (ppp_if handles this ioctl)
174 if (ioctl(fd, SIOCGETU, &ifunit) < 0) {
175 syslog(LOG_ERR, "ioctl(SIOCGETU): %m");
179 /* Set debug flags in driver */
180 if (ioctl(fd, SIOCSIFDEBUG, &kdebugflag) < 0) {
181 syslog(LOG_ERR, "ioctl(SIOCSIFDEBUG): %m");
184 /* close stdin, stdout, stderr if they might refer to the device */
185 if (default_device && !closed_stdio) {
188 for (i = 0; i <= 2; ++i)
189 if (i != fd && i != s)
196 * disestablish_ppp - Restore the serial port to normal operation.
197 * It attempts to reconstruct the stream with the previously popped
198 * modules. This shouldn't call die() because it's called from die().
207 /* we can't push or pop modules after the stream has hung up */
208 str_module_count = 0;
209 restore_term = 0; /* nor can we fix up terminal settings */
215 * Check whether the link seems not to be 8-bit clean.
217 if (ioctl(fd, SIOCGIFDEBUG, (caddr_t) &flags) == 0) {
219 switch (~flags & PAI_FLAGS_HIBITS) {
221 s = "bit 7 set to 1";
224 s = "bit 7 set to 0";
226 case PAI_FLAGS_PAR_EVEN:
229 case PAI_FLAGS_PAR_ODD:
234 syslog(LOG_WARNING, "Serial link is not 8-bit clean:");
235 syslog(LOG_WARNING, "All received characters had %s", s);
240 while (ioctl(fd, I_POP, 0) == 0) /* pop any we pushed */
244 for (; str_module_count > 0; str_module_count--) {
245 if (ioctl(fd, I_PUSH, str_modules[str_module_count-1].modname)) {
247 syslog(LOG_WARNING, "str_restore: couldn't push module %s: %m",
248 str_modules[str_module_count-1].modname);
250 MAINDEBUG((LOG_INFO, "str_restore: pushed module %s",
251 str_modules[str_module_count-1].modname));
258 * List of valid speeds.
261 int speed_int, speed_val;
333 * Translate from bits/second to a speed_t.
339 struct speed *speedp;
343 for (speedp = speeds; speedp->speed_int; speedp++)
344 if (bps == speedp->speed_int)
345 return speedp->speed_val;
346 syslog(LOG_WARNING, "speed %d not supported", bps);
351 * Translate from a speed_t to bits/second.
357 struct speed *speedp;
361 for (speedp = speeds; speedp->speed_int; speedp++)
362 if (speed == speedp->speed_val)
363 return speedp->speed_int;
368 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
369 * at the requested speed, etc. If `local' is true, set CLOCAL
370 * regardless of whether the modem option was specified.
372 set_up_tty(fd, local)
378 if (tcgetattr(fd, &tios) < 0) {
379 syslog(LOG_ERR, "tcgetattr: %m");
386 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL | CRTSCTS);
388 tios.c_cflag |= CRTSCTS;
390 tios.c_cflag |= CS8 | CREAD | HUPCL;
392 tios.c_cflag |= CLOCAL;
393 tios.c_iflag = IGNBRK | IGNPAR;
397 tios.c_cc[VTIME] = 0;
400 tios.c_iflag |= IXOFF;
401 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
402 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
405 speed = translate_speed(inspeed);
407 cfsetospeed(&tios, speed);
408 cfsetispeed(&tios, speed);
410 speed = cfgetospeed(&tios);
412 * We can't proceed if the serial port speed is B0,
413 * since that implies that the serial port is disabled.
416 syslog(LOG_ERR, "Baud rate for %s is 0; need explicit baud rate",
422 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
423 syslog(LOG_ERR, "tcsetattr: %m");
427 baud_rate = inspeed = baud_rate_of(speed);
432 * restore_tty - restore the terminal to the saved settings.
438 if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
440 syslog(LOG_WARNING, "tcsetattr: %m");
446 * setdtr - control the DTR line on the serial port.
447 * This is called from die(), so it shouldn't call die().
452 int modembits = TIOCM_DTR;
454 ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
459 * output - Output PPP packet.
470 MAINDEBUG((LOG_WARNING, "output: unit != 0!"));
472 log_packet(p, len, "sent ");
475 str.buf = (caddr_t) p;
476 if (putmsg(fd, NULL, &str, 0) < 0) {
477 if (errno != ENXIO) {
478 syslog(LOG_ERR, "putmsg: %m");
485 * wait_input - wait for input, for a length of time specified in *timo.
488 struct timeval *timo;
493 t = timo == NULL? -1: timo->tv_sec * 1000 + timo->tv_usec / 1000;
495 pfd.events = POLLIN | POLLPRI | POLLHUP;
496 if (poll(&pfd, 1, t) < 0 && errno != EINTR) {
497 syslog(LOG_ERR, "poll: %m");
503 * read_packet - get a PPP packet from the serial device.
509 struct strbuf str, ctl;
511 unsigned char ctlbuf[16];
513 str.maxlen = MTU+DLLHEADERLEN;
514 str.buf = (caddr_t) buf;
515 ctl.maxlen = sizeof(ctlbuf);
516 ctl.buf = (caddr_t) ctlbuf;
518 len = getmsg(fd, &ctl, &str, &i);
520 if (errno == EAGAIN || errno == EWOULDBLOCK) {
523 syslog(LOG_ERR, "getmsg(fd) %m");
527 MAINDEBUG((LOG_DEBUG, "getmsg returned 0x%x",len));
529 syslog(LOG_NOTICE, "got ctrl msg len %d %x %x\n", ctl.len,
530 ctlbuf[0], ctlbuf[1]);
533 MAINDEBUG((LOG_DEBUG, "getmsg short return length %d", str.len));
542 * ppp_send_config - configure the transmit characteristics of
546 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
554 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
556 if (ioctl(s, SIOCSIFMTU, (caddr_t) &ifr) < 0) {
557 syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m");
561 if(ioctl(fd, SIOCSIFASYNCMAP, (caddr_t) &asyncmap) < 0) {
562 syslog(LOG_ERR, "ioctl(SIOCSIFASYNCMAP): %m");
567 if(ioctl(fd, SIOCSIFCOMPPROT, &c) < 0) {
568 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPPROT): %m");
573 if(ioctl(fd, SIOCSIFCOMPAC, &c) < 0) {
574 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPAC): %m");
581 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
584 ppp_set_xaccm(unit, accm)
588 if (ioctl(fd, SIOCSIFXASYNCMAP, accm) < 0 && errno != ENOTTY)
589 syslog(LOG_WARNING, "ioctl(set extended ACCM): %m");
594 * ppp_recv_config - configure the receive-side characteristics of
598 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
605 if (ioctl(fd, SIOCSIFMRU, &mru) < 0) {
606 syslog(LOG_ERR, "ioctl(SIOCSIFMRU): %m");
609 if (ioctl(fd, SIOCSIFRASYNCMAP, (caddr_t) &asyncmap) < 0) {
610 syslog(LOG_ERR, "ioctl(SIOCSIFRASYNCMAP): %m");
613 c = 2 + (pcomp? 1: 0);
614 if(ioctl(fd, SIOCSIFCOMPPROT, &c) < 0) {
615 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPPROT): %m");
618 c = 2 + (accomp? 1: 0);
619 if (ioctl(fd, SIOCSIFCOMPAC, &c) < 0) {
620 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPAC): %m");
625 * ccp_test - ask kernel whether a given compression method
626 * is acceptable for use.
628 ccp_test(unit, opt_ptr, opt_len, for_transmit)
629 int unit, opt_len, for_transmit;
632 struct ppp_option_data data;
634 if ((unsigned) opt_len > MAX_PPP_OPTION)
635 opt_len = MAX_PPP_OPTION;
636 data.length = opt_len;
637 data.transmit = for_transmit;
638 BCOPY(opt_ptr, data.opt_data, opt_len);
639 return ioctl(fd, SIOCSCOMPRESS, (caddr_t) &data) >= 0;
643 * ccp_flags_set - inform kernel about the current state of CCP.
646 ccp_flags_set(unit, isopen, isup)
647 int unit, isopen, isup;
651 x = (isopen? 1: 0) + (isup? 2: 0);
652 if (ioctl(fd, SIOCSIFCOMP, (caddr_t) &x) < 0 && errno != ENOTTY)
653 syslog(LOG_ERR, "ioctl (SIOCSIFCOMP): %m");
657 * sifvjcomp - config tcp header compression
660 sifvjcomp(u, vjcomp, cidcomp, maxcid)
661 int u, vjcomp, cidcomp, maxcid;
665 x = (vjcomp? 1: 0) + (cidcomp? 0: 2) + (maxcid << 4);
666 if (ioctl(fd, SIOCSIFVJCOMP, (caddr_t) &x) < 0) {
667 syslog(LOG_ERR, "ioctl(SIOCSIFVJCOMP): %m");
674 * sifup - Config the interface up.
682 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
683 if (ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
684 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
687 ifr.ifr_flags |= IFF_UP;
688 if (ioctl(s, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
689 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
696 * sifdown - Config the interface down.
703 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
704 if (ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
705 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
708 ifr.ifr_flags &= ~IFF_UP;
709 if (ioctl(s, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
710 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
717 * SET_SA_FAMILY - initialize a struct sockaddr, setting the sa_family field.
719 #define SET_SA_FAMILY(addr, family) \
720 BZERO((char *) &(addr), sizeof(addr)); \
721 addr.sa_family = (family);
724 * sifaddr - Config the interface IP addresses and netmask.
735 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
736 SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
737 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
738 if (ioctl(s, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
739 syslog(LOG_ERR, "ioctl(SIOCSIFADDR): %m");
742 ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = h;
743 if (ioctl(s, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
744 syslog(LOG_ERR, "ioctl(SIOCSIFDSTADDR): %m");
748 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = m;
749 syslog(LOG_INFO, "Setting interface mask to %s\n", ip_ntoa(m));
750 if (ioctl(s, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
751 syslog(LOG_ERR, "ioctl(SIOCSIFNETMASK): %m");
759 * cifaddr - Clear the interface IP addresses, and delete routes
760 * through the interface if possible.
769 SET_SA_FAMILY(rt.rt_dst, AF_INET);
770 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = h;
771 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
772 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = o;
773 rt.rt_flags = RTF_HOST;
774 if (ioctl(s, SIOCDELRT, (caddr_t) &rt) < 0) {
775 syslog(LOG_ERR, "ioctl(SIOCDELRT): %m");
782 * sifdefaultroute - assign a default route through the address given.
785 sifdefaultroute(u, g)
791 SET_SA_FAMILY(rt.rt_dst, AF_INET);
792 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
793 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
794 rt.rt_flags = RTF_GATEWAY;
795 if (ioctl(s, SIOCADDRT, &rt) < 0) {
796 syslog(LOG_ERR, "default route ioctl(SIOCADDRT): %m");
803 * cifdefaultroute - delete a default route through the address given.
806 cifdefaultroute(u, g)
812 SET_SA_FAMILY(rt.rt_dst, AF_INET);
813 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
814 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
815 rt.rt_flags = RTF_GATEWAY;
816 if (ioctl(s, SIOCDELRT, &rt) < 0) {
817 syslog(LOG_ERR, "default route ioctl(SIOCDELRT): %m");
824 * sifproxyarp - Make a proxy ARP entry for the peer.
827 sifproxyarp(unit, hisaddr)
831 struct arpreq arpreq;
833 BZERO(&arpreq, sizeof(arpreq));
836 * Get the hardware address of an interface on the same subnet
837 * as our local address.
839 if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) {
840 syslog(LOG_WARNING, "Cannot determine ethernet address for proxy ARP");
844 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
845 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
846 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
847 if (ioctl(s, SIOCSARP, (caddr_t)&arpreq) < 0) {
848 syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
856 * cifproxyarp - Delete the proxy ARP entry for the peer.
859 cifproxyarp(unit, hisaddr)
863 struct arpreq arpreq;
865 BZERO(&arpreq, sizeof(arpreq));
866 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
867 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
868 if (ioctl(s, SIOCDARP, (caddr_t)&arpreq) < 0) {
869 syslog(LOG_ERR, "ioctl(SIOCDARP): %m");
876 * get_ether_addr - get the hardware address of an interface on the
877 * the same subnet as ipaddr. Code borrowed from myetheraddr.c
878 * in the cslip-2.6 distribution, which is subject to the following
879 * copyright notice (which also applies to logwtmp below):
881 * Copyright (c) 1990, 1992 The Regents of the University of California.
882 * All rights reserved.
884 * Redistribution and use in source and binary forms, with or without
885 * modification, are permitted provided that: (1) source code distributions
886 * retain the above copyright notice and this paragraph in its entirety, (2)
887 * distributions including binary code include the above copyright notice and
888 * this paragraph in its entirety in the documentation or other materials
889 * provided with the distribution, and (3) all advertising materials mentioning
890 * features or use of this software display the following acknowledgement:
891 * ``This product includes software developed by the University of California,
892 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
893 * the University nor the names of its contributors may be used to endorse
894 * or promote products derived from this software without specific prior
895 * written permission.
896 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
897 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
898 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
904 #include <arpa/inet.h>
906 /* XXX SunOS 4.1 defines this and 3.5 doesn't... */
912 #include <netinet/in_var.h>
914 #include <netinet/if_ether.h>
916 /* Cast a struct sockaddr to a structaddr_in */
917 #define SATOSIN(sa) ((struct sockaddr_in *)(sa))
919 /* Determine if "bits" is set in "flag" */
920 #define ALLSET(flag, bits) (((flag) & (bits)) == (bits))
922 static struct nlist nl[] = {
931 get_ether_addr(ipaddr, hwaddr)
933 struct sockaddr *hwaddr;
936 register struct ifnet *ifp;
937 register struct arpcom *ac;
938 struct arpcom arpcom;
941 register struct ifaddr *ifa;
942 register struct in_ifaddr *in;
950 /* Open kernel memory for reading */
951 kd = kvm_open(0, 0, 0, O_RDONLY, NULL);
953 syslog(LOG_ERR, "kvm_open: %m");
958 if (kvm_nlist(kd, nl) != 0) {
959 syslog(LOG_ERR, "kvm_nlist failed");
970 if (kvm_read(kd, nl[N_IFNET].n_value, (char *)&addr, sizeof(addr))
972 syslog(LOG_ERR, "error reading ifnet addr");
975 for ( ; addr; addr = (uint32)ifp->if_next) {
976 if (kvm_read(kd, addr, (char *)ac, sizeof(*ac)) != sizeof(*ac)) {
977 syslog(LOG_ERR, "error reading ifnet");
981 /* Only look at configured, broadcast interfaces */
982 if (!ALLSET(ifp->if_flags, IFF_UP | IFF_BROADCAST))
985 /* This probably can't happen... */
986 if (ifp->if_addrlist == 0)
990 /* Get interface ip address */
992 if (kvm_read(kd, (uint32)ifp->if_addrlist, (char *)&ifaddr,
993 sizeof(ifaddr)) != sizeof(ifaddr)) {
994 syslog(LOG_ERR, "error reading ifaddr");
997 inp = &SATOSIN(&ifa->ifa_addr)->sin_addr;
999 inp = &SATOSIN(&ifp->if_addr)->sin_addr;
1002 /* Check if this interface on the right subnet */
1004 mask = in->ia_subnetmask;
1006 mask = ifp->if_subnetmask;
1008 if ((ipaddr & mask) != (inp->s_addr & mask))
1011 /* Copy out the local ethernet address */
1012 hwaddr->sa_family = AF_UNSPEC;
1013 BCOPY((caddr_t) &arpcom.ac_enaddr, hwaddr->sa_data,
1014 sizeof(arpcom.ac_enaddr));
1015 return 1; /* success! */
1018 /* couldn't find one */
1022 #define WTMPFILE "/usr/adm/wtmp"
1025 logwtmp(line, name, host)
1026 char *line, *name, *host;
1032 if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0)
1034 if (!fstat(fd, &buf)) {
1035 (void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
1036 (void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
1037 (void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
1038 (void)time(&ut.ut_time);
1039 if (write(fd, (char *)&ut, sizeof(struct utmp)) != sizeof(struct utmp))
1040 (void)ftruncate(fd, buf.st_size);