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.4 1994/05/25 06:30:49 paulus Exp $";
31 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <sys/ioctl.h>
36 #include <sys/errno.h>
41 #include <net/if_ppp.h>
42 #include <net/route.h>
43 #include <netinet/in.h>
48 static int initdisc = -1; /* Initial TTY discipline */
49 extern int kdebugflag;
52 * establish_ppp - Turn the serial port into a ppp interface.
57 int pppdisc = PPPDISC;
60 if (ioctl(fd, TIOCGETD, &initdisc) < 0) {
61 syslog(LOG_ERR, "ioctl(TIOCGETD): %m");
64 if (ioctl(fd, TIOCSETD, &pppdisc) < 0) {
65 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
70 * Find out which interface we were given.
72 if (ioctl(fd, PPPIOCGUNIT, &ifunit) < 0) {
73 syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m");
78 * Enable debug in the driver if requested.
81 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
82 syslog(LOG_WARNING, "ioctl (PPPIOCGFLAGS): %m");
84 x |= (kdebugflag & 0xFF) * SC_DEBUG;
85 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
86 syslog(LOG_WARNING, "ioctl(PPPIOCSFLAGS): %m");
93 * disestablish_ppp - Restore the serial port to normal operation.
94 * This shouldn't call die() because it's called from die().
104 * Check whether the link seems not to be 8-bit clean.
106 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) == 0) {
108 switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP)) {
110 s = "bit 7 set to 1";
113 s = "bit 7 set to 0";
123 syslog(LOG_WARNING, "Serial link is not 8-bit clean:");
124 syslog(LOG_WARNING, "All received characters had %s", s);
127 if (ioctl(fd, TIOCSETD, &initdisc) < 0)
128 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
134 * output - Output PPP packet.
143 MAINDEBUG((LOG_WARNING, "output: unit != 0!"));
145 log_packet(p, len, "sent ");
147 if (write(fd, p, len) < 0) {
148 syslog(LOG_ERR, "write: %m");
155 * read_packet - get a PPP packet from the serial device.
163 if ((len = read(fd, buf, MTU + DLLHEADERLEN)) < 0) {
164 if (errno == EWOULDBLOCK) {
165 MAINDEBUG((LOG_DEBUG, "read(fd): EWOULDBLOCK"));
168 syslog(LOG_ERR, "read(fd): %m");
176 * ppp_send_config - configure the transmit characteristics of
180 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
188 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
190 if (ioctl(s, SIOCSIFMTU, (caddr_t) &ifr) < 0) {
191 syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m");
195 if (ioctl(fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0) {
196 syslog(LOG_ERR, "ioctl(PPPIOCSASYNCMAP): %m");
200 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
201 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
204 x = pcomp? x | SC_COMP_PROT: x &~ SC_COMP_PROT;
205 x = accomp? x | SC_COMP_AC: x &~ SC_COMP_AC;
206 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
207 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
214 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
217 ppp_set_xaccm(unit, accm)
221 if (ioctl(fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY)
222 syslog(LOG_WARNING, "ioctl(set extended ACCM): %m");
227 * ppp_recv_config - configure the receive-side characteristics of
231 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
238 if (ioctl(fd, PPPIOCSMRU, (caddr_t) &mru) < 0) {
239 syslog(LOG_ERR, "ioctl(PPPIOCSMRU): %m");
242 if (ioctl(fd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0) {
243 syslog(LOG_ERR, "ioctl(PPPIOCSRASYNCMAP): %m");
246 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
247 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
250 x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC;
251 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
252 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
258 * sifvjcomp - config tcp header compression
261 sifvjcomp(u, vjcomp, cidcomp, maxcid)
262 int u, vjcomp, cidcomp, maxcid;
266 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
267 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
270 x = vjcomp ? x | SC_COMP_TCP: x &~ SC_COMP_TCP;
271 x = cidcomp? x & ~SC_NO_TCP_CCID: x | SC_NO_TCP_CCID;
272 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
273 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
276 if (ioctl(fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0) {
277 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
284 * sifup - Config the interface up and enable IP packets to pass.
292 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
293 if (ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
294 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
297 ifr.ifr_flags |= IFF_UP;
298 if (ioctl(s, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
299 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
302 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
303 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
307 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
308 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
315 * sifdown - Config the interface down and disable IP.
325 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
326 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
330 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
331 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
335 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
336 if (ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
337 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
340 ifr.ifr_flags &= ~IFF_UP;
341 if (ioctl(s, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
342 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
350 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
353 #define SET_SA_FAMILY(addr, family) \
354 BZERO((char *) &(addr), sizeof(addr)); \
355 addr.sa_family = (family);
358 * sifaddr - Config the interface IP addresses and netmask.
367 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
368 SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
369 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
370 if (ioctl(s, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
371 syslog(LOG_ERR, "ioctl(SIOCSIFADDR): %m");
374 ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = h;
375 if (ioctl(s, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
376 syslog(LOG_ERR, "ioctl(SIOCSIFDSTADDR): %m");
380 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = m;
381 syslog(LOG_INFO, "Setting interface mask to %s\n", ip_ntoa(m));
382 if (ioctl(s, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
383 syslog(LOG_ERR, "ioctl(SIOCSIFNETMASK): %m");
391 * cifaddr - Clear the interface IP addresses, and delete routes
392 * through the interface if possible.
399 SET_SA_FAMILY(rt.rt_dst, AF_INET);
400 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = h;
401 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
402 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = o;
403 rt.rt_flags = RTF_HOST;
404 if (ioctl(s, SIOCDELRT, (caddr_t) &rt) < 0) {
405 syslog(LOG_ERR, "ioctl(SIOCDELRT): %m");
412 * sifdefaultroute - assign a default route through the address given.
415 sifdefaultroute(u, g)
419 SET_SA_FAMILY(rt.rt_dst, AF_INET);
420 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
421 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
422 rt.rt_flags = RTF_GATEWAY;
423 if (ioctl(s, SIOCADDRT, &rt) < 0) {
424 syslog(LOG_ERR, "default route ioctl(SIOCADDRT): %m");
431 * cifdefaultroute - delete a default route through the address given.
434 cifdefaultroute(u, g)
438 SET_SA_FAMILY(rt.rt_dst, AF_INET);
439 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
440 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
441 rt.rt_flags = RTF_GATEWAY;
442 if (ioctl(s, SIOCDELRT, &rt) < 0)
443 syslog(LOG_WARNING, "default route ioctl(SIOCDELRT): %m");
447 * sifproxyarp - Make a proxy ARP entry for the peer.
450 sifproxyarp(unit, hisaddr)
454 struct arpreq arpreq;
456 BZERO(&arpreq, sizeof(arpreq));
459 * Get the hardware address of an interface on the same subnet
460 * as our local address.
462 if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) {
463 syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
467 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
468 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
469 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
470 if (ioctl(s, SIOCSARP, (caddr_t)&arpreq) < 0) {
471 syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
479 * cifproxyarp - Delete the proxy ARP entry for the peer.
482 cifproxyarp(unit, hisaddr)
486 struct arpreq arpreq;
488 BZERO(&arpreq, sizeof(arpreq));
489 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
490 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
491 if (ioctl(s, SIOCDARP, (caddr_t)&arpreq) < 0) {
492 syslog(LOG_WARNING, "ioctl(SIOCDARP): %m");
499 * get_ether_addr - get the hardware address of an interface on the
500 * the same subnet as ipaddr.
505 get_ether_addr(ipaddr, hwaddr)
507 struct sockaddr *hwaddr;
509 struct ifreq *ifr, *ifend, *ifp;
511 struct sockaddr_dl *dla;
514 struct ifreq ifs[MAX_IFS];
516 ifc.ifc_len = sizeof(ifs);
518 if (ioctl(s, SIOCGIFCONF, &ifc) < 0) {
519 syslog(LOG_ERR, "ioctl(SIOCGIFCONF): %m");
524 * Scan through looking for an interface with an Internet
525 * address on the same subnet as `ipaddr'.
527 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
528 for (ifr = ifc.ifc_req; ifr < ifend; ) {
529 if (ifr->ifr_addr.sa_family == AF_INET) {
530 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
531 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
533 * Check that the interface is up, and not point-to-point
536 if (ioctl(s, SIOCGIFFLAGS, &ifreq) < 0)
538 if ((ifreq.ifr_flags &
539 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
540 != (IFF_UP|IFF_BROADCAST))
543 * Get its netmask and check that it's on the right subnet.
545 if (ioctl(s, SIOCGIFNETMASK, &ifreq) < 0)
547 mask = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
548 if ((ipaddr & mask) != (ina & mask))
553 ifr = (struct ifreq *) ((char *)&ifr->ifr_addr + sizeof(struct sockaddr)
559 syslog(LOG_DEBUG, "found interface %s for proxy arp", ifr->ifr_name);
562 * Now scan through again looking for a link-level address
563 * for this interface.
566 for (ifr = ifc.ifc_req; ifr < ifend; ) {
567 if (strcmp(ifp->ifr_name, ifr->ifr_name) == 0
568 && ifr->ifr_addr.sa_family == AF_DLI) {
569 /* && ifr->ifr_addr.sa_family == AF_LINK) { Per! Kolla !!! ROHACK */
571 * Found the link-level address - copy it out
573 dla = (struct sockaddr_dl *)&ifr->ifr_addr;
574 hwaddr->sa_family = AF_UNSPEC;
575 BCOPY(dla, hwaddr->sa_data, sizeof(hwaddr->sa_data));
578 ifr = (struct ifreq *) ((char *)&ifr->ifr_addr + sizeof(struct sockaddr)
587 * ppp_available - check whether the system has any ppp interfaces
588 * (in fact we check whether we can do an ioctl on ppp0).
597 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
598 return 1; /* can't tell - maybe we're not root */
600 strncpy(ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
601 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
609 Seems like strdup() is not part of string package in Ultrix.
610 If I understood the man-page on the sun this should work.
615 char *strdup( in ) char *in;
618 if(! (dup = (char *) malloc( strlen( in ) +1 ))) return NULL;
619 (void) strcpy( dup, in );
624 * This logwtmp() implementation is subject to the following copyright:
626 * Copyright (c) 1988 The Regents of the University of California.
627 * All rights reserved.
629 * Redistribution and use in source and binary forms are permitted
630 * provided that the above copyright notice and this paragraph are
631 * duplicated in all such forms and that any documentation,
632 * advertising materials, and other materials related to such
633 * distribution and use acknowledge that the software was developed
634 * by the University of California, Berkeley. The name of the
635 * University may not be used to endorse or promote products derived
636 * from this software without specific prior written permission.
637 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
638 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
639 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
642 #define WTMPFILE "/usr/adm/wtmp"
645 logwtmp(line, name, host)
646 char *line, *name, *host;
652 if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0)
654 if (!fstat(fd, &buf)) {
655 (void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
656 (void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
657 (void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
658 (void)time(&ut.ut_time);
659 if (write(fd, (char *)&ut, sizeof(struct utmp)) != sizeof(struct utmp))
660 (void)ftruncate(fd, buf.st_size);