2 * sys-ultrix.c - System-dependent procedures for setting up
3 * PPP interfaces on Ultrix systems.
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.
22 static char rcsid[] = "$Id: sys-ultrix.c,v 1.5 1994/09/01 00:39:09 paulus Exp $";
33 #include <sys/types.h>
35 #include <sys/socket.h>
36 #include <sys/ioctl.h>
38 #include <sys/errno.h>
43 #include <net/if_ppp.h>
44 #include <net/route.h>
45 #include <netinet/in.h>
50 static int initdisc = -1; /* Initial TTY discipline */
52 static int restore_term; /* 1 => we've munged the terminal */
53 static struct termios inittermios; /* Initial TTY termios */
56 * sys_init - System-dependent initialization.
61 openlog("pppd", LOG_PID);
63 setlogmask(LOG_UPTO(LOG_DEBUG));
67 * note_debug_level - note a change in the debug level.
73 syslog(LOG_INFO, "Debug turned ON, Level %d", debug);
78 * establish_ppp - Turn the serial port into a ppp interface.
83 int pppdisc = PPPDISC;
86 if (ioctl(fd, TIOCGETD, &initdisc) < 0) {
87 syslog(LOG_ERR, "ioctl(TIOCGETD): %m");
90 if (ioctl(fd, TIOCSETD, &pppdisc) < 0) {
91 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
96 * Find out which interface we were given.
98 if (ioctl(fd, PPPIOCGUNIT, &ifunit) < 0) {
99 syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m");
104 * Enable debug in the driver if requested.
107 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
108 syslog(LOG_WARNING, "ioctl (PPPIOCGFLAGS): %m");
110 x |= (kdebugflag & 0xFF) * SC_DEBUG;
111 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
112 syslog(LOG_WARNING, "ioctl(PPPIOCSFLAGS): %m");
119 * disestablish_ppp - Restore the serial port to normal operation.
120 * This shouldn't call die() because it's called from die().
130 * Check whether the link seems not to be 8-bit clean.
132 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) == 0) {
134 switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP)) {
136 s = "bit 7 set to 1";
139 s = "bit 7 set to 0";
149 syslog(LOG_WARNING, "Serial link is not 8-bit clean:");
150 syslog(LOG_WARNING, "All received characters had %s", s);
153 if (ioctl(fd, TIOCSETD, &initdisc) < 0)
154 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
160 * List of valid speeds.
163 int speed_int, speed_val;
235 * Translate from bits/second to a speed_t.
241 struct speed *speedp;
245 for (speedp = speeds; speedp->speed_int; speedp++)
246 if (bps == speedp->speed_int)
247 return speedp->speed_val;
248 syslog(LOG_WARNING, "speed %d not supported", bps);
253 * Translate from a speed_t to bits/second.
259 struct speed *speedp;
263 for (speedp = speeds; speedp->speed_int; speedp++)
264 if (speed == speedp->speed_val)
265 return speedp->speed_int;
270 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
271 * at the requested speed, etc. If `local' is true, set CLOCAL
272 * regardless of whether the modem option was specified.
274 set_up_tty(fd, local)
280 if (tcgetattr(fd, &tios) < 0) {
281 syslog(LOG_ERR, "tcgetattr: %m");
289 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL | CRTSCTS);
291 tios.c_cflag |= CRTSCTS;
293 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
296 tios.c_cflag |= CS8 | CREAD | HUPCL;
298 tios.c_cflag |= CLOCAL;
299 tios.c_iflag = IGNBRK | IGNPAR;
303 tios.c_cc[VTIME] = 0;
306 tios.c_iflag |= IXOFF;
307 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
308 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
311 speed = translate_speed(inspeed);
313 cfsetospeed(&tios, speed);
314 cfsetispeed(&tios, speed);
316 speed = cfgetospeed(&tios);
319 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
320 syslog(LOG_ERR, "tcsetattr: %m");
325 if (ioctl(fd, (crtscts || modem)? TIOCMODEM: TIOCNMODEM, &x) < 0)
326 syslog(LOG_WARNING, "TIOC(N)MODEM: %m");
327 if (ioctl(fd, (local || !modem)? TIOCNCAR: TIOCCAR) < 0)
328 syslog(LOG_WARNING, "TIOC(N)CAR: %m");
330 baud_rate = inspeed = baud_rate_of(speed);
335 * restore_tty - restore the terminal to the saved settings.
341 if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
343 syslog(LOG_WARNING, "tcsetattr: %m");
344 restore_term = FALSE;
349 * setdtr - control the DTR line on the serial port.
350 * This is called from die(), so it shouldn't call die().
355 int modembits = TIOCM_DTR;
357 ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
362 * output - Output PPP packet.
371 MAINDEBUG((LOG_WARNING, "output: unit != 0!"));
373 log_packet(p, len, "sent ");
375 if (write(fd, p, len) < 0) {
376 syslog(LOG_ERR, "write: %m");
383 * wait_input - wait until there is data available on fd,
384 * for the length of time specified by *timo (indefinite
388 struct timeval *timo;
395 n = select(fd+1, &ready, NULL, &ready, timo);
396 if (n < 0 && errno != EINTR) {
397 syslog(LOG_ERR, "select: %m");
404 * read_packet - get a PPP packet from the serial device.
412 if ((len = read(fd, buf, MTU + DLLHEADERLEN)) < 0) {
413 if (errno == EWOULDBLOCK) {
414 MAINDEBUG((LOG_DEBUG, "read(fd): EWOULDBLOCK"));
417 syslog(LOG_ERR, "read(fd): %m");
425 * ppp_send_config - configure the transmit characteristics of
429 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
437 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
439 if (ioctl(s, SIOCSIFMTU, (caddr_t) &ifr) < 0) {
440 syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m");
444 if (ioctl(fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0) {
445 syslog(LOG_ERR, "ioctl(PPPIOCSASYNCMAP): %m");
449 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
450 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
453 x = pcomp? x | SC_COMP_PROT: x &~ SC_COMP_PROT;
454 x = accomp? x | SC_COMP_AC: x &~ SC_COMP_AC;
455 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
456 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
463 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
466 ppp_set_xaccm(unit, accm)
470 if (ioctl(fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY)
471 syslog(LOG_WARNING, "ioctl(set extended ACCM): %m");
476 * ppp_recv_config - configure the receive-side characteristics of
480 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
487 if (ioctl(fd, PPPIOCSMRU, (caddr_t) &mru) < 0) {
488 syslog(LOG_ERR, "ioctl(PPPIOCSMRU): %m");
491 if (ioctl(fd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0) {
492 syslog(LOG_ERR, "ioctl(PPPIOCSRASYNCMAP): %m");
495 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
496 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
499 x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC;
500 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
501 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
507 * ccp_flags_set - inform kernel about the current state of CCP.
510 ccp_flags_set(unit, isopen, isup)
511 int unit, isopen, isup;
515 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
516 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
519 x = isopen? x | SC_CCP_OPEN: x &~ SC_CCP_OPEN;
520 x = isup? x | SC_CCP_UP: x &~ SC_CCP_UP;
521 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
522 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
526 * sifvjcomp - config tcp header compression
529 sifvjcomp(u, vjcomp, cidcomp, maxcid)
530 int u, vjcomp, cidcomp, maxcid;
534 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
535 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
538 x = vjcomp ? x | SC_COMP_TCP: x &~ SC_COMP_TCP;
539 x = cidcomp? x & ~SC_NO_TCP_CCID: x | SC_NO_TCP_CCID;
540 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
541 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
544 if (ioctl(fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0) {
545 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
552 * sifup - Config the interface up and enable IP packets to pass.
560 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
561 if (ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
562 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
565 ifr.ifr_flags |= IFF_UP;
566 if (ioctl(s, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
567 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
570 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
571 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
575 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
576 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
583 * sifdown - Config the interface down and disable IP.
593 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
594 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
598 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
599 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
603 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
604 if (ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
605 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
608 ifr.ifr_flags &= ~IFF_UP;
609 if (ioctl(s, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
610 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
618 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
621 #define SET_SA_FAMILY(addr, family) \
622 BZERO((char *) &(addr), sizeof(addr)); \
623 addr.sa_family = (family);
626 * sifaddr - Config the interface IP addresses and netmask.
635 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
636 SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
637 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
638 if (ioctl(s, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
639 syslog(LOG_ERR, "ioctl(SIOCSIFADDR): %m");
642 ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = h;
643 if (ioctl(s, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
644 syslog(LOG_ERR, "ioctl(SIOCSIFDSTADDR): %m");
648 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = m;
649 syslog(LOG_INFO, "Setting interface mask to %s\n", ip_ntoa(m));
650 if (ioctl(s, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
651 syslog(LOG_ERR, "ioctl(SIOCSIFNETMASK): %m");
659 * cifaddr - Clear the interface IP addresses, and delete routes
660 * through the interface if possible.
667 SET_SA_FAMILY(rt.rt_dst, AF_INET);
668 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = h;
669 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
670 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = o;
671 rt.rt_flags = RTF_HOST;
672 if (ioctl(s, SIOCDELRT, (caddr_t) &rt) < 0) {
673 syslog(LOG_ERR, "ioctl(SIOCDELRT): %m");
680 * sifdefaultroute - assign a default route through the address given.
683 sifdefaultroute(u, g)
687 SET_SA_FAMILY(rt.rt_dst, AF_INET);
688 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
689 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
690 rt.rt_flags = RTF_GATEWAY;
691 if (ioctl(s, SIOCADDRT, &rt) < 0) {
692 syslog(LOG_ERR, "default route ioctl(SIOCADDRT): %m");
699 * cifdefaultroute - delete a default route through the address given.
702 cifdefaultroute(u, g)
706 SET_SA_FAMILY(rt.rt_dst, AF_INET);
707 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
708 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
709 rt.rt_flags = RTF_GATEWAY;
710 if (ioctl(s, SIOCDELRT, &rt) < 0)
711 syslog(LOG_WARNING, "default route ioctl(SIOCDELRT): %m");
715 * sifproxyarp - Make a proxy ARP entry for the peer.
718 sifproxyarp(unit, hisaddr)
722 struct arpreq arpreq;
724 BZERO(&arpreq, sizeof(arpreq));
727 * Get the hardware address of an interface on the same subnet
728 * as our local address.
730 if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) {
731 syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
735 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
736 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
737 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
738 if (ioctl(s, SIOCSARP, (caddr_t)&arpreq) < 0) {
739 syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
747 * cifproxyarp - Delete the proxy ARP entry for the peer.
750 cifproxyarp(unit, hisaddr)
754 struct arpreq arpreq;
756 BZERO(&arpreq, sizeof(arpreq));
757 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
758 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
759 if (ioctl(s, SIOCDARP, (caddr_t)&arpreq) < 0) {
760 syslog(LOG_WARNING, "ioctl(SIOCDARP): %m");
767 * get_ether_addr - get the hardware address of an interface on the
768 * the same subnet as ipaddr.
773 get_ether_addr(ipaddr, hwaddr)
775 struct sockaddr *hwaddr;
777 struct ifreq *ifr, *ifend, *ifp;
779 struct sockaddr_dl *dla;
782 struct ifreq ifs[MAX_IFS];
784 ifc.ifc_len = sizeof(ifs);
786 if (ioctl(s, SIOCGIFCONF, &ifc) < 0) {
787 syslog(LOG_ERR, "ioctl(SIOCGIFCONF): %m");
792 * Scan through looking for an interface with an Internet
793 * address on the same subnet as `ipaddr'.
795 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
796 for (ifr = ifc.ifc_req; ifr < ifend; ) {
797 if (ifr->ifr_addr.sa_family == AF_INET) {
798 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
799 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
801 * Check that the interface is up, and not point-to-point
804 if (ioctl(s, SIOCGIFFLAGS, &ifreq) < 0)
806 if ((ifreq.ifr_flags &
807 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
808 != (IFF_UP|IFF_BROADCAST))
811 * Get its netmask and check that it's on the right subnet.
813 if (ioctl(s, SIOCGIFNETMASK, &ifreq) < 0)
815 mask = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
816 if ((ipaddr & mask) != (ina & mask))
821 ifr = (struct ifreq *) ((char *)&ifr->ifr_addr + sizeof(struct sockaddr)
827 syslog(LOG_DEBUG, "found interface %s for proxy arp", ifr->ifr_name);
830 * Now scan through again looking for a link-level address
831 * for this interface.
834 for (ifr = ifc.ifc_req; ifr < ifend; ) {
835 if (strcmp(ifp->ifr_name, ifr->ifr_name) == 0
836 && ifr->ifr_addr.sa_family == AF_DLI) {
837 /* && ifr->ifr_addr.sa_family == AF_LINK) { Per! Kolla !!! ROHACK */
839 * Found the link-level address - copy it out
841 dla = (struct sockaddr_dl *)&ifr->ifr_addr;
842 hwaddr->sa_family = AF_UNSPEC;
843 BCOPY(dla, hwaddr->sa_data, sizeof(hwaddr->sa_data));
846 ifr = (struct ifreq *) ((char *)&ifr->ifr_addr + sizeof(struct sockaddr)
855 * ppp_available - check whether the system has any ppp interfaces
856 * (in fact we check whether we can do an ioctl on ppp0).
865 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
866 return 1; /* can't tell - maybe we're not root */
868 strncpy(ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
869 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
877 Seems like strdup() is not part of string package in Ultrix.
878 If I understood the man-page on the sun this should work.
883 char *strdup( in ) char *in;
886 if(! (dup = (char *) malloc( strlen( in ) +1 ))) return NULL;
887 (void) strcpy( dup, in );
892 * This logwtmp() implementation is subject to the following copyright:
894 * Copyright (c) 1988 The Regents of the University of California.
895 * All rights reserved.
897 * Redistribution and use in source and binary forms are permitted
898 * provided that the above copyright notice and this paragraph are
899 * duplicated in all such forms and that any documentation,
900 * advertising materials, and other materials related to such
901 * distribution and use acknowledge that the software was developed
902 * by the University of California, Berkeley. The name of the
903 * University may not be used to endorse or promote products derived
904 * from this software without specific prior written permission.
905 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
906 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
907 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
910 #define WTMPFILE "/usr/adm/wtmp"
913 logwtmp(line, name, host)
914 char *line, *name, *host;
920 if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0)
922 if (!fstat(fd, &buf)) {
923 (void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
924 (void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
925 (void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
926 (void)time(&ut.ut_time);
927 if (write(fd, (char *)&ut, sizeof(struct utmp)) != sizeof(struct utmp))
928 (void)ftruncate(fd, buf.st_size);