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.2 1994/05/06 04:48:21 paulus Exp $";
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <sys/ioctl.h>
34 #include <sys/errno.h>
38 #include <net/if_ppp.h>
39 #include <net/route.h>
40 #include <netinet/in.h>
45 static int initdisc = -1; /* Initial TTY discipline */
46 extern int kdebugflag;
49 * establish_ppp - Turn the serial port into a ppp interface.
54 int pppdisc = PPPDISC;
57 if (ioctl(fd, TIOCGETD, &initdisc) < 0) {
58 syslog(LOG_ERR, "ioctl(TIOCGETD): %m");
61 if (ioctl(fd, TIOCSETD, &pppdisc) < 0) {
62 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
67 * Find out which interface we were given.
69 if (ioctl(fd, PPPIOCGUNIT, &ifunit) < 0) {
70 syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m");
75 * Enable debug in the driver if requested.
78 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
79 syslog(LOG_WARNING, "ioctl (PPPIOCGFLAGS): %m");
81 x |= (kdebugflag & 0xFF) * SC_DEBUG;
82 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
83 syslog(LOG_WARNING, "ioctl(PPPIOCSFLAGS): %m");
90 * disestablish_ppp - Restore the serial port to normal operation.
91 * This shouldn't call die() because it's called from die().
101 * Check whether the link seems not to be 8-bit clean.
103 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) == 0) {
105 switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP)) {
107 s = "bit 7 set to 1";
110 s = "bit 7 set to 0";
120 syslog(LOG_WARNING, "Serial link is not 8-bit clean:");
121 syslog(LOG_WARNING, "All received characters had %s", s);
124 if (ioctl(fd, TIOCSETD, &initdisc) < 0)
125 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
131 * output - Output PPP packet.
140 MAINDEBUG((LOG_WARNING, "output: unit != 0!"));
142 log_packet(p, len, "sent ");
144 if (write(fd, p, len) < 0) {
145 syslog(LOG_ERR, "write: %m");
152 * read_packet - get a PPP packet from the serial device.
160 if ((len = read(fd, buf, MTU + DLLHEADERLEN)) < 0) {
161 if (errno == EWOULDBLOCK) {
162 MAINDEBUG((LOG_DEBUG, "read(fd): EWOULDBLOCK"));
165 syslog(LOG_ERR, "read(fd): %m");
173 * ppp_send_config - configure the transmit characteristics of
177 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
185 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
187 if (ioctl(s, SIOCSIFMTU, (caddr_t) &ifr) < 0) {
188 syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m");
192 if (ioctl(fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0) {
193 syslog(LOG_ERR, "ioctl(PPPIOCSASYNCMAP): %m");
197 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
198 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
201 x = pcomp? x | SC_COMP_PROT: x &~ SC_COMP_PROT;
202 x = accomp? x | SC_COMP_AC: x &~ SC_COMP_AC;
203 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
204 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
211 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
214 ppp_set_xaccm(unit, accm)
218 if (ioctl(fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY)
219 syslog(LOG_WARNING, "ioctl(set extended ACCM): %m");
224 * ppp_recv_config - configure the receive-side characteristics of
228 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
235 if (ioctl(fd, PPPIOCSMRU, (caddr_t) &mru) < 0) {
236 syslog(LOG_ERR, "ioctl(PPPIOCSMRU): %m");
239 if (ioctl(fd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0) {
240 syslog(LOG_ERR, "ioctl(PPPIOCSRASYNCMAP): %m");
243 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
244 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
247 x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC;
248 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
249 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
255 * sifvjcomp - config tcp header compression
258 sifvjcomp(u, vjcomp, cidcomp, maxcid)
259 int u, vjcomp, cidcomp, maxcid;
263 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
264 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
267 x = vjcomp ? x | SC_COMP_TCP: x &~ SC_COMP_TCP;
268 x = cidcomp? x & ~SC_NO_TCP_CCID: x | SC_NO_TCP_CCID;
269 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
270 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
273 if (ioctl(fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0) {
274 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
281 * sifup - Config the interface up and enable IP packets to pass.
289 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
290 if (ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
291 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
294 ifr.ifr_flags |= IFF_UP;
295 if (ioctl(s, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
296 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
299 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
300 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
304 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
305 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
312 * sifdown - Config the interface down and disable IP.
322 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
323 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
327 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
328 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
332 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
333 if (ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
334 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
337 ifr.ifr_flags &= ~IFF_UP;
338 if (ioctl(s, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
339 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
347 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
350 #define SET_SA_FAMILY(addr, family) \
351 BZERO((char *) &(addr), sizeof(addr)); \
352 addr.sa_family = (family);
355 * sifaddr - Config the interface IP addresses and netmask.
364 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
365 SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
366 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
367 if (ioctl(s, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
368 syslog(LOG_ERR, "ioctl(SIOCSIFADDR): %m");
371 ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = h;
372 if (ioctl(s, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
373 syslog(LOG_ERR, "ioctl(SIOCSIFDSTADDR): %m");
377 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = m;
378 syslog(LOG_INFO, "Setting interface mask to %s\n", ip_ntoa(m));
379 if (ioctl(s, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
380 syslog(LOG_ERR, "ioctl(SIOCSIFNETMASK): %m");
388 * cifaddr - Clear the interface IP addresses, and delete routes
389 * through the interface if possible.
396 SET_SA_FAMILY(rt.rt_dst, AF_INET);
397 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = h;
398 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
399 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = o;
400 rt.rt_flags = RTF_HOST;
401 if (ioctl(s, SIOCDELRT, (caddr_t) &rt) < 0) {
402 syslog(LOG_ERR, "ioctl(SIOCDELRT): %m");
409 * sifdefaultroute - assign a default route through the address given.
412 sifdefaultroute(u, g)
416 SET_SA_FAMILY(rt.rt_dst, AF_INET);
417 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
418 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
419 rt.rt_flags = RTF_GATEWAY;
420 if (ioctl(s, SIOCADDRT, &rt) < 0) {
421 syslog(LOG_ERR, "default route ioctl(SIOCADDRT): %m");
428 * cifdefaultroute - delete a default route through the address given.
431 cifdefaultroute(u, g)
435 SET_SA_FAMILY(rt.rt_dst, AF_INET);
436 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
437 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
438 rt.rt_flags = RTF_GATEWAY;
439 if (ioctl(s, SIOCDELRT, &rt) < 0)
440 syslog(LOG_WARNING, "default route ioctl(SIOCDELRT): %m");
444 * sifproxyarp - Make a proxy ARP entry for the peer.
447 sifproxyarp(unit, hisaddr)
451 struct arpreq arpreq;
453 BZERO(&arpreq, sizeof(arpreq));
456 * Get the hardware address of an interface on the same subnet
457 * as our local address.
459 if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) {
460 syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
464 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
465 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
466 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
467 if (ioctl(s, SIOCSARP, (caddr_t)&arpreq) < 0) {
468 syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
476 * cifproxyarp - Delete the proxy ARP entry for the peer.
479 cifproxyarp(unit, hisaddr)
483 struct arpreq arpreq;
485 BZERO(&arpreq, sizeof(arpreq));
486 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
487 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
488 if (ioctl(s, SIOCDARP, (caddr_t)&arpreq) < 0) {
489 syslog(LOG_WARNING, "ioctl(SIOCDARP): %m");
496 * get_ether_addr - get the hardware address of an interface on the
497 * the same subnet as ipaddr.
502 get_ether_addr(ipaddr, hwaddr)
504 struct sockaddr *hwaddr;
506 struct ifreq *ifr, *ifend, *ifp;
508 struct sockaddr_dl *dla;
511 struct ifreq ifs[MAX_IFS];
513 ifc.ifc_len = sizeof(ifs);
515 if (ioctl(s, SIOCGIFCONF, &ifc) < 0) {
516 syslog(LOG_ERR, "ioctl(SIOCGIFCONF): %m");
521 * Scan through looking for an interface with an Internet
522 * address on the same subnet as `ipaddr'.
524 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
525 for (ifr = ifc.ifc_req; ifr < ifend; ) {
526 if (ifr->ifr_addr.sa_family == AF_INET) {
527 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
528 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
530 * Check that the interface is up, and not point-to-point
533 if (ioctl(s, SIOCGIFFLAGS, &ifreq) < 0)
535 if ((ifreq.ifr_flags &
536 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
537 != (IFF_UP|IFF_BROADCAST))
540 * Get its netmask and check that it's on the right subnet.
542 if (ioctl(s, SIOCGIFNETMASK, &ifreq) < 0)
544 mask = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
545 if ((ipaddr & mask) != (ina & mask))
550 ifr = (struct ifreq *) ((char *)&ifr->ifr_addr + sizeof(struct sockaddr)
556 syslog(LOG_DEBUG, "found interface %s for proxy arp", ifr->ifr_name);
559 * Now scan through again looking for a link-level address
560 * for this interface.
563 for (ifr = ifc.ifc_req; ifr < ifend; ) {
564 if (strcmp(ifp->ifr_name, ifr->ifr_name) == 0
565 && ifr->ifr_addr.sa_family == AF_DLI) {
566 /* && ifr->ifr_addr.sa_family == AF_LINK) { Per! Kolla !!! ROHACK */
568 * Found the link-level address - copy it out
570 dla = (struct sockaddr_dl *)&ifr->ifr_addr;
571 hwaddr->sa_family = AF_UNSPEC;
572 BCOPY(dla, hwaddr->sa_data, sizeof(hwaddr->sa_data));
575 ifr = (struct ifreq *) ((char *)&ifr->ifr_addr + sizeof(struct sockaddr)
584 * ppp_available - check whether the system has any ppp interfaces
585 * (in fact we check whether we can do an ioctl on ppp0).
594 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
595 return 1; /* can't tell - maybe we're not root */
597 strncpy(ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
598 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
606 Seems like strdup() is not part of string package in Ultrix.
607 If I understood the man-page on the sun this should work.
612 char *strdup( in ) char *in;
615 if(! (dup = (char *) malloc( strlen( in ) +1 ))) return NULL;
616 (void) strcpy( dup, in );