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.1 1994/04/18 04:11:11 paulus Exp $";
30 #include <sys/ioctl.h>
31 #include <sys/types.h>
32 #include <sys/socket.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 */
49 * establish_ppp - Turn the serial port into a ppp interface.
54 int pppdisc = PPPDISC;
56 if (ioctl(fd, TIOCGETD, &initdisc) < 0) {
57 syslog(LOG_ERR, "ioctl(TIOCGETD): %m");
60 if (ioctl(fd, TIOCSETD, &pppdisc) < 0) {
61 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
66 * Find out which interface we were given.
68 if (ioctl(fd, PPPIOCGUNIT, &ifunit) < 0) {
69 syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m");
74 * Enable debug in the driver if requested.
77 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
78 syslog(LOG_WARNING, "ioctl (PPPIOCGFLAGS): %m");
80 x |= (kdebugflag & 0xFF) * SC_DEBUG;
81 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
82 syslog(LOG_WARNING, "ioctl(PPPIOCSFLAGS): %m");
89 * disestablish_ppp - Restore the serial port to normal operation.
90 * This shouldn't call die() because it's called from die().
100 * Check whether the link seems not to be 8-bit clean.
102 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) == 0) {
104 switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP)) {
106 s = "bit 7 set to 1";
109 s = "bit 7 set to 0";
119 syslog(LOG_WARNING, "Serial link is not 8-bit clean:");
120 syslog(LOG_WARNING, "All received characters had %s", s);
123 if (ioctl(fd, TIOCSETD, &initdisc) < 0)
124 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
130 * output - Output PPP packet.
139 MAINDEBUG((LOG_WARNING, "output: unit != 0!"));
141 log_packet(p, len, "sent ");
143 if (write(fd, p, len) < 0) {
144 syslog(LOG_ERR, "write: %m");
151 * read_packet - get a PPP packet from the serial device.
159 if ((len = read(fd, buf, MTU + DLLHEADERLEN)) < 0) {
160 if (errno == EWOULDBLOCK) {
161 MAINDEBUG((LOG_DEBUG, "read(fd): EWOULDBLOCK"));
164 syslog(LOG_ERR, "read(fd): %m");
172 * ppp_send_config - configure the transmit characteristics of
176 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
184 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
186 if (ioctl(s, SIOCSIFMTU, (caddr_t) &ifr) < 0) {
187 syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m");
191 if (ioctl(fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0) {
192 syslog(LOG_ERR, "ioctl(PPPIOCSASYNCMAP): %m");
196 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
197 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
200 x = pcomp? x | SC_COMP_PROT: x &~ SC_COMP_PROT;
201 x = accomp? x | SC_COMP_AC: x &~ SC_COMP_AC;
202 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
203 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
210 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
213 ppp_set_xaccm(unit, accm)
217 if (ioctl(fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY)
218 syslog(LOG_WARNING, "ioctl(set extended ACCM): %m");
223 * ppp_recv_config - configure the receive-side characteristics of
227 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
234 if (ioctl(fd, PPPIOCSMRU, (caddr_t) &mru) < 0) {
235 syslog(LOG_ERR, "ioctl(PPPIOCSMRU): %m");
238 if (ioctl(fd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0) {
239 syslog(LOG_ERR, "ioctl(PPPIOCSRASYNCMAP): %m");
242 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
243 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
246 x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC;
247 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
248 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
254 * sifvjcomp - config tcp header compression
257 sifvjcomp(u, vjcomp, cidcomp, maxcid)
258 int u, vjcomp, cidcomp, maxcid;
262 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
263 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
266 x = vjcomp ? x | SC_COMP_TCP: x &~ SC_COMP_TCP;
267 x = cidcomp? x & ~SC_NO_TCP_CCID: x | SC_NO_TCP_CCID;
268 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
269 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
272 if (ioctl(fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0) {
273 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
280 * sifup - Config the interface up and enable IP packets to pass.
288 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
289 if (ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
290 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
293 ifr.ifr_flags |= IFF_UP;
294 if (ioctl(s, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
295 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
298 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
299 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
303 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
304 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
311 * sifdown - Config the interface down and disable IP.
321 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
322 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
326 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
327 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
331 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
332 if (ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
333 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
336 ifr.ifr_flags &= ~IFF_UP;
337 if (ioctl(s, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
338 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
346 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
349 #define SET_SA_FAMILY(addr, family) \
350 BZERO((char *) &(addr), sizeof(addr)); \
351 addr.sa_family = (family);
354 * sifaddr - Config the interface IP addresses and netmask.
363 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
364 SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
365 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
366 if (ioctl(s, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
367 syslog(LOG_ERR, "ioctl(SIOCSIFADDR): %m");
370 ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = h;
371 if (ioctl(s, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
372 syslog(LOG_ERR, "ioctl(SIOCSIFDSTADDR): %m");
376 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = m;
377 syslog(LOG_INFO, "Setting interface mask to %s\n", ip_ntoa(m));
378 if (ioctl(s, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
379 syslog(LOG_ERR, "ioctl(SIOCSIFNETMASK): %m");
387 * cifaddr - Clear the interface IP addresses, and delete routes
388 * through the interface if possible.
395 SET_SA_FAMILY(rt.rt_dst, AF_INET);
396 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = h;
397 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
398 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = o;
399 rt.rt_flags = RTF_HOST;
400 if (ioctl(s, SIOCDELRT, (caddr_t) &rt) < 0) {
401 syslog(LOG_ERR, "ioctl(SIOCDELRT): %m");
408 * sifdefaultroute - assign a default route through the address given.
411 sifdefaultroute(u, g)
415 SET_SA_FAMILY(rt.rt_dst, AF_INET);
416 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
417 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
418 rt.rt_flags = RTF_GATEWAY;
419 if (ioctl(s, SIOCADDRT, &rt) < 0) {
420 syslog(LOG_ERR, "default route ioctl(SIOCADDRT): %m");
427 * cifdefaultroute - delete a default route through the address given.
430 cifdefaultroute(u, g)
434 SET_SA_FAMILY(rt.rt_dst, AF_INET);
435 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
436 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
437 rt.rt_flags = RTF_GATEWAY;
438 if (ioctl(s, SIOCDELRT, &rt) < 0)
439 syslog(LOG_WARNING, "default route ioctl(SIOCDELRT): %m");
443 * sifproxyarp - Make a proxy ARP entry for the peer.
446 sifproxyarp(unit, hisaddr)
450 struct arpreq arpreq;
452 BZERO(&arpreq, sizeof(arpreq));
455 * Get the hardware address of an interface on the same subnet
456 * as our local address.
458 if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) {
459 syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
463 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
464 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
465 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
466 if (ioctl(s, SIOCSARP, (caddr_t)&arpreq) < 0) {
467 syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
475 * cifproxyarp - Delete the proxy ARP entry for the peer.
478 cifproxyarp(unit, hisaddr)
482 struct arpreq arpreq;
484 BZERO(&arpreq, sizeof(arpreq));
485 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
486 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
487 if (ioctl(s, SIOCDARP, (caddr_t)&arpreq) < 0) {
488 syslog(LOG_WARNING, "ioctl(SIOCDARP): %m");
495 * get_ether_addr - get the hardware address of an interface on the
496 * the same subnet as ipaddr.
501 get_ether_addr(ipaddr, hwaddr)
503 struct sockaddr *hwaddr;
505 struct ifreq *ifr, *ifend, *ifp;
507 struct sockaddr_dl *dla;
510 struct ifreq ifs[MAX_IFS];
512 ifc.ifc_len = sizeof(ifs);
514 if (ioctl(s, SIOCGIFCONF, &ifc) < 0) {
515 syslog(LOG_ERR, "ioctl(SIOCGIFCONF): %m");
520 * Scan through looking for an interface with an Internet
521 * address on the same subnet as `ipaddr'.
523 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
524 for (ifr = ifc.ifc_req; ifr < ifend; ) {
525 if (ifr->ifr_addr.sa_family == AF_INET) {
526 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
527 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
529 * Check that the interface is up, and not point-to-point
532 if (ioctl(s, SIOCGIFFLAGS, &ifreq) < 0)
534 if ((ifreq.ifr_flags &
535 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
536 != (IFF_UP|IFF_BROADCAST))
539 * Get its netmask and check that it's on the right subnet.
541 if (ioctl(s, SIOCGIFNETMASK, &ifreq) < 0)
543 mask = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
544 if ((ipaddr & mask) != (ina & mask))
549 ifr = (struct ifreq *) ((char *)&ifr->ifr_addr + sizeof(struct sockaddr)
555 syslog(LOG_DEBUG, "found interface %s for proxy arp", ifr->ifr_name);
558 * Now scan through again looking for a link-level address
559 * for this interface.
562 for (ifr = ifc.ifc_req; ifr < ifend; ) {
563 if (strcmp(ifp->ifr_name, ifr->ifr_name) == 0
564 && ifr->ifr_addr.sa_family == AF_DLI) {
565 /* && ifr->ifr_addr.sa_family == AF_LINK) { Per! Kolla !!! ROHACK */
567 * Found the link-level address - copy it out
569 dla = (struct sockaddr_dl *)&ifr->ifr_addr;
570 hwaddr->sa_family = AF_UNSPEC;
571 BCOPY(dla, hwaddr->sa_data, sizeof(hwaddr->sa_data));
574 ifr = (struct ifreq *) ((char *)&ifr->ifr_addr + sizeof(struct sockaddr)
583 * ppp_available - check whether the system has any ppp interfaces
584 * (in fact we check whether we can do an ioctl on ppp0).
593 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
594 return 1; /* can't tell - maybe we're not root */
596 strncpy(ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
597 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
605 Seems like strdup() is not part of string package in Ultrix.
606 If I understood the man-page on the sun this should work.
611 char *strdup( in ) char *in;
614 if(! (dup = (char *) malloc( strlen( in ) +1 ))) return NULL;
615 (void) strcpy( dup, in );