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.6 1994/09/21 06:47:37 paulus Exp $";
33 #include <sys/types.h>
35 #include <sys/socket.h>
36 #include <sys/ioctl.h>
38 #include <sys/errno.h>
42 #include <net/ppp_defs.h>
43 #include <net/if_ppp.h>
44 #include <net/route.h>
45 #include <netinet/in.h>
49 static int initdisc = -1; /* Initial TTY discipline */
51 static int restore_term; /* 1 => we've munged the terminal */
52 static struct termios inittermios; /* Initial TTY termios */
55 * sys_init - System-dependent initialization.
60 openlog("pppd", LOG_PID);
62 setlogmask(LOG_UPTO(LOG_DEBUG));
66 * note_debug_level - note a change in the debug level.
72 syslog(LOG_INFO, "Debug turned ON, Level %d", debug);
77 * establish_ppp - Turn the serial port into a ppp interface.
82 int pppdisc = PPPDISC;
85 if (ioctl(fd, TIOCGETD, &initdisc) < 0) {
86 syslog(LOG_ERR, "ioctl(TIOCGETD): %m");
89 if (ioctl(fd, TIOCSETD, &pppdisc) < 0) {
90 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
95 * Find out which interface we were given.
97 if (ioctl(fd, PPPIOCGUNIT, &ifunit) < 0) {
98 syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m");
103 * Enable debug in the driver if requested.
106 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
107 syslog(LOG_WARNING, "ioctl (PPPIOCGFLAGS): %m");
109 x |= (kdebugflag & 0xFF) * SC_DEBUG;
110 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
111 syslog(LOG_WARNING, "ioctl(PPPIOCSFLAGS): %m");
118 * disestablish_ppp - Restore the serial port to normal operation.
119 * This shouldn't call die() because it's called from die().
129 * Check whether the link seems not to be 8-bit clean.
131 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) == 0) {
133 switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP)) {
135 s = "bit 7 set to 1";
138 s = "bit 7 set to 0";
148 syslog(LOG_WARNING, "Serial link is not 8-bit clean:");
149 syslog(LOG_WARNING, "All received characters had %s", s);
152 if (ioctl(fd, TIOCSETD, &initdisc) < 0)
153 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
159 * List of valid speeds.
162 int speed_int, speed_val;
234 * Translate from bits/second to a speed_t.
240 struct speed *speedp;
244 for (speedp = speeds; speedp->speed_int; speedp++)
245 if (bps == speedp->speed_int)
246 return speedp->speed_val;
247 syslog(LOG_WARNING, "speed %d not supported", bps);
252 * Translate from a speed_t to bits/second.
258 struct speed *speedp;
262 for (speedp = speeds; speedp->speed_int; speedp++)
263 if (speed == speedp->speed_val)
264 return speedp->speed_int;
269 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
270 * at the requested speed, etc. If `local' is true, set CLOCAL
271 * regardless of whether the modem option was specified.
273 set_up_tty(fd, local)
279 if (tcgetattr(fd, &tios) < 0) {
280 syslog(LOG_ERR, "tcgetattr: %m");
288 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL | CRTSCTS);
290 tios.c_cflag |= CRTSCTS;
292 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
295 tios.c_cflag |= CS8 | CREAD | HUPCL;
297 tios.c_cflag |= CLOCAL;
298 tios.c_iflag = IGNBRK | IGNPAR;
302 tios.c_cc[VTIME] = 0;
305 tios.c_iflag |= IXOFF;
306 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
307 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
310 speed = translate_speed(inspeed);
312 cfsetospeed(&tios, speed);
313 cfsetispeed(&tios, speed);
315 speed = cfgetospeed(&tios);
318 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
319 syslog(LOG_ERR, "tcsetattr: %m");
324 if (ioctl(fd, (crtscts || modem)? TIOCMODEM: TIOCNMODEM, &x) < 0)
325 syslog(LOG_WARNING, "TIOC(N)MODEM: %m");
326 if (ioctl(fd, (local || !modem)? TIOCNCAR: TIOCCAR) < 0)
327 syslog(LOG_WARNING, "TIOC(N)CAR: %m");
329 baud_rate = inspeed = baud_rate_of(speed);
334 * restore_tty - restore the terminal to the saved settings.
340 if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
342 syslog(LOG_WARNING, "tcsetattr: %m");
343 restore_term = FALSE;
348 * setdtr - control the DTR line on the serial port.
349 * This is called from die(), so it shouldn't call die().
354 int modembits = TIOCM_DTR;
356 ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
361 * output - Output PPP packet.
370 MAINDEBUG((LOG_WARNING, "output: unit != 0!"));
372 log_packet(p, len, "sent ");
374 if (write(fd, p, len) < 0) {
375 syslog(LOG_ERR, "write: %m");
382 * wait_input - wait until there is data available on fd,
383 * for the length of time specified by *timo (indefinite
387 struct timeval *timo;
394 n = select(fd+1, &ready, NULL, &ready, timo);
395 if (n < 0 && errno != EINTR) {
396 syslog(LOG_ERR, "select: %m");
403 * read_packet - get a PPP packet from the serial device.
411 if ((len = read(fd, buf, PPP_MTU + PPP_HDRLEN)) < 0) {
412 if (errno == EWOULDBLOCK) {
413 MAINDEBUG((LOG_DEBUG, "read(fd): EWOULDBLOCK"));
416 syslog(LOG_ERR, "read(fd): %m");
424 * ppp_send_config - configure the transmit characteristics of
428 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
436 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
438 if (ioctl(s, SIOCSIFMTU, (caddr_t) &ifr) < 0) {
439 syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m");
443 if (ioctl(fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0) {
444 syslog(LOG_ERR, "ioctl(PPPIOCSASYNCMAP): %m");
448 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
449 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
452 x = pcomp? x | SC_COMP_PROT: x &~ SC_COMP_PROT;
453 x = accomp? x | SC_COMP_AC: x &~ SC_COMP_AC;
454 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
455 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
462 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
465 ppp_set_xaccm(unit, accm)
469 if (ioctl(fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY)
470 syslog(LOG_WARNING, "ioctl(set extended ACCM): %m");
475 * ppp_recv_config - configure the receive-side characteristics of
479 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
486 if (ioctl(fd, PPPIOCSMRU, (caddr_t) &mru) < 0) {
487 syslog(LOG_ERR, "ioctl(PPPIOCSMRU): %m");
490 if (ioctl(fd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0) {
491 syslog(LOG_ERR, "ioctl(PPPIOCSRASYNCMAP): %m");
494 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
495 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
498 x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC;
499 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
500 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
506 * ccp_flags_set - inform kernel about the current state of CCP.
509 ccp_flags_set(unit, isopen, isup)
510 int unit, isopen, isup;
514 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
515 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
518 x = isopen? x | SC_CCP_OPEN: x &~ SC_CCP_OPEN;
519 x = isup? x | SC_CCP_UP: x &~ SC_CCP_UP;
520 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
521 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
525 * sifvjcomp - config tcp header compression
528 sifvjcomp(u, vjcomp, cidcomp, maxcid)
529 int u, vjcomp, cidcomp, maxcid;
533 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
534 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
537 x = vjcomp ? x | SC_COMP_TCP: x &~ SC_COMP_TCP;
538 x = cidcomp? x & ~SC_NO_TCP_CCID: x | SC_NO_TCP_CCID;
539 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
540 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
543 if (ioctl(fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0) {
544 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
551 * sifup - Config the interface up and enable IP packets to pass.
559 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
560 if (ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
561 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
564 ifr.ifr_flags |= IFF_UP;
565 if (ioctl(s, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
566 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
569 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
570 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
574 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
575 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
582 * sifdown - Config the interface down and disable IP.
592 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
593 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
597 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
598 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
602 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
603 if (ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
604 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
607 ifr.ifr_flags &= ~IFF_UP;
608 if (ioctl(s, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
609 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
617 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
620 #define SET_SA_FAMILY(addr, family) \
621 BZERO((char *) &(addr), sizeof(addr)); \
622 addr.sa_family = (family);
625 * sifaddr - Config the interface IP addresses and netmask.
634 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
635 SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
636 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
637 if (ioctl(s, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
638 syslog(LOG_ERR, "ioctl(SIOCSIFADDR): %m");
641 ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = h;
642 if (ioctl(s, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
643 syslog(LOG_ERR, "ioctl(SIOCSIFDSTADDR): %m");
647 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = m;
648 syslog(LOG_INFO, "Setting interface mask to %s\n", ip_ntoa(m));
649 if (ioctl(s, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
650 syslog(LOG_ERR, "ioctl(SIOCSIFNETMASK): %m");
658 * cifaddr - Clear the interface IP addresses, and delete routes
659 * through the interface if possible.
666 SET_SA_FAMILY(rt.rt_dst, AF_INET);
667 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = h;
668 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
669 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = o;
670 rt.rt_flags = RTF_HOST;
671 if (ioctl(s, SIOCDELRT, (caddr_t) &rt) < 0) {
672 syslog(LOG_ERR, "ioctl(SIOCDELRT): %m");
679 * sifdefaultroute - assign a default route through the address given.
682 sifdefaultroute(u, g)
686 SET_SA_FAMILY(rt.rt_dst, AF_INET);
687 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
688 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
689 rt.rt_flags = RTF_GATEWAY;
690 if (ioctl(s, SIOCADDRT, &rt) < 0) {
691 syslog(LOG_ERR, "default route ioctl(SIOCADDRT): %m");
698 * cifdefaultroute - delete a default route through the address given.
701 cifdefaultroute(u, g)
705 SET_SA_FAMILY(rt.rt_dst, AF_INET);
706 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
707 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
708 rt.rt_flags = RTF_GATEWAY;
709 if (ioctl(s, SIOCDELRT, &rt) < 0)
710 syslog(LOG_WARNING, "default route ioctl(SIOCDELRT): %m");
714 * sifproxyarp - Make a proxy ARP entry for the peer.
717 sifproxyarp(unit, hisaddr)
721 struct arpreq arpreq;
723 BZERO(&arpreq, sizeof(arpreq));
726 * Get the hardware address of an interface on the same subnet
727 * as our local address.
729 if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) {
730 syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
734 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
735 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
736 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
737 if (ioctl(s, SIOCSARP, (caddr_t)&arpreq) < 0) {
738 syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
746 * cifproxyarp - Delete the proxy ARP entry for the peer.
749 cifproxyarp(unit, hisaddr)
753 struct arpreq arpreq;
755 BZERO(&arpreq, sizeof(arpreq));
756 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
757 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
758 if (ioctl(s, SIOCDARP, (caddr_t)&arpreq) < 0) {
759 syslog(LOG_WARNING, "ioctl(SIOCDARP): %m");
766 * get_ether_addr - get the hardware address of an interface on the
767 * the same subnet as ipaddr.
772 get_ether_addr(ipaddr, hwaddr)
774 struct sockaddr *hwaddr;
776 struct ifreq *ifr, *ifend, *ifp;
778 struct sockaddr_dl *dla;
781 struct ifreq ifs[MAX_IFS];
783 ifc.ifc_len = sizeof(ifs);
785 if (ioctl(s, SIOCGIFCONF, &ifc) < 0) {
786 syslog(LOG_ERR, "ioctl(SIOCGIFCONF): %m");
791 * Scan through looking for an interface with an Internet
792 * address on the same subnet as `ipaddr'.
794 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
795 for (ifr = ifc.ifc_req; ifr < ifend; ) {
796 if (ifr->ifr_addr.sa_family == AF_INET) {
797 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
798 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
800 * Check that the interface is up, and not point-to-point
803 if (ioctl(s, SIOCGIFFLAGS, &ifreq) < 0)
805 if ((ifreq.ifr_flags &
806 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
807 != (IFF_UP|IFF_BROADCAST))
810 * Get its netmask and check that it's on the right subnet.
812 if (ioctl(s, SIOCGIFNETMASK, &ifreq) < 0)
814 mask = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
815 if ((ipaddr & mask) != (ina & mask))
820 ifr = (struct ifreq *) ((char *)&ifr->ifr_addr + sizeof(struct sockaddr)
826 syslog(LOG_DEBUG, "found interface %s for proxy arp", ifr->ifr_name);
829 * Now scan through again looking for a link-level address
830 * for this interface.
833 for (ifr = ifc.ifc_req; ifr < ifend; ) {
834 if (strcmp(ifp->ifr_name, ifr->ifr_name) == 0
835 && ifr->ifr_addr.sa_family == AF_DLI) {
836 /* && ifr->ifr_addr.sa_family == AF_LINK) { Per! Kolla !!! ROHACK */
838 * Found the link-level address - copy it out
840 dla = (struct sockaddr_dl *)&ifr->ifr_addr;
841 hwaddr->sa_family = AF_UNSPEC;
842 BCOPY(dla, hwaddr->sa_data, sizeof(hwaddr->sa_data));
845 ifr = (struct ifreq *) ((char *)&ifr->ifr_addr + sizeof(struct sockaddr)
854 * ppp_available - check whether the system has any ppp interfaces
855 * (in fact we check whether we can do an ioctl on ppp0).
864 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
865 return 1; /* can't tell - maybe we're not root */
867 strncpy(ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
868 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
876 Seems like strdup() is not part of string package in Ultrix.
877 If I understood the man-page on the sun this should work.
882 char *strdup( in ) char *in;
885 if(! (dup = (char *) malloc( strlen( in ) +1 ))) return NULL;
886 (void) strcpy( dup, in );
891 * This logwtmp() implementation is subject to the following copyright:
893 * Copyright (c) 1988 The Regents of the University of California.
894 * All rights reserved.
896 * Redistribution and use in source and binary forms are permitted
897 * provided that the above copyright notice and this paragraph are
898 * duplicated in all such forms and that any documentation,
899 * advertising materials, and other materials related to such
900 * distribution and use acknowledge that the software was developed
901 * by the University of California, Berkeley. The name of the
902 * University may not be used to endorse or promote products derived
903 * from this software without specific prior written permission.
904 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
905 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
906 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
909 #define WTMPFILE "/usr/adm/wtmp"
912 logwtmp(line, name, host)
913 char *line, *name, *host;
919 if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0)
921 if (!fstat(fd, &buf)) {
922 (void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
923 (void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
924 (void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
925 (void)time(&ut.ut_time);
926 if (write(fd, (char *)&ut, sizeof(struct utmp)) != sizeof(struct utmp))
927 (void)ftruncate(fd, buf.st_size);