2 * sys-bsd.c - System-dependent procedures for setting up
3 * PPP interfaces on bsd-4.4-ish systems (including 386BSD, NetBSD, etc.)
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-bsd.c,v 1.6 1994/05/24 11:27:56 paulus Exp $";
30 #include <sys/ioctl.h>
31 #include <sys/types.h>
32 #include <sys/socket.h>
34 #include <sys/errno.h>
37 #include <net/if_ppp.h>
38 #include <net/route.h>
39 #include <net/if_dl.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.
290 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
291 if (ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
292 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
295 ifr.ifr_flags |= IFF_UP;
296 if (ioctl(s, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
297 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
300 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
301 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
305 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
306 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
313 * sifdown - Config the interface down and disable IP.
324 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
325 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
329 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
330 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
334 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
335 if (ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
336 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
339 ifr.ifr_flags &= ~IFF_UP;
340 if (ioctl(s, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
341 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
349 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
352 #define SET_SA_FAMILY(addr, family) \
353 BZERO((char *) &(addr), sizeof(addr)); \
354 addr.sa_family = (family); \
355 addr.sa_len = sizeof(addr);
358 * sifaddr - Config the interface IP addresses and netmask.
365 struct ifaliasreq ifra;
367 strncpy(ifra.ifra_name, ifname, sizeof(ifra.ifra_name));
368 SET_SA_FAMILY(ifra.ifra_addr, AF_INET);
369 ((struct sockaddr_in *) &ifra.ifra_addr)->sin_addr.s_addr = o;
370 SET_SA_FAMILY(ifra.ifra_broadaddr, AF_INET);
371 ((struct sockaddr_in *) &ifra.ifra_broadaddr)->sin_addr.s_addr = h;
373 SET_SA_FAMILY(ifra.ifra_mask, AF_INET);
374 ((struct sockaddr_in *) &ifra.ifra_mask)->sin_addr.s_addr = m;
376 BZERO(&ifra.ifra_mask, sizeof(ifra.ifra_mask));
377 if (ioctl(s, SIOCAIFADDR, (caddr_t) &ifra) < 0) {
378 if (errno != EEXIST) {
379 syslog(LOG_ERR, "ioctl(SIOCAIFADDR): %m");
382 syslog(LOG_WARNING, "ioctl(SIOCAIFADDR): Address already exists");
388 * cifaddr - Clear the interface IP addresses, and delete routes
389 * through the interface if possible.
396 struct ifaliasreq ifra;
398 strncpy(ifra.ifra_name, ifname, sizeof(ifra.ifra_name));
399 SET_SA_FAMILY(ifra.ifra_addr, AF_INET);
400 ((struct sockaddr_in *) &ifra.ifra_addr)->sin_addr.s_addr = o;
401 SET_SA_FAMILY(ifra.ifra_broadaddr, AF_INET);
402 ((struct sockaddr_in *) &ifra.ifra_broadaddr)->sin_addr.s_addr = h;
403 BZERO(&ifra.ifra_mask, sizeof(ifra.ifra_mask));
404 if (ioctl(s, SIOCDIFADDR, (caddr_t) &ifra) < 0) {
405 syslog(LOG_WARNING, "ioctl(SIOCDIFADDR): %m");
412 * sifdefaultroute - assign a default route through the address given.
415 sifdefaultroute(u, g)
421 SET_SA_FAMILY(rt.rt_dst, AF_INET);
422 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
423 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
424 rt.rt_flags = RTF_GATEWAY;
425 if (ioctl(s, SIOCADDRT, &rt) < 0) {
426 syslog(LOG_ERR, "default route ioctl(SIOCADDRT): %m");
433 * cifdefaultroute - delete a default route through the address given.
436 cifdefaultroute(u, g)
442 SET_SA_FAMILY(rt.rt_dst, AF_INET);
443 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
444 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
445 rt.rt_flags = RTF_GATEWAY;
446 if (ioctl(s, SIOCDELRT, &rt) < 0)
447 syslog(LOG_WARNING, "default route ioctl(SIOCDELRT): %m");
451 * sifproxyarp - Make a proxy ARP entry for the peer.
454 sifproxyarp(unit, hisaddr)
458 struct arpreq arpreq;
460 BZERO(&arpreq, sizeof(arpreq));
463 * Get the hardware address of an interface on the same subnet
464 * as our local address.
466 if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) {
467 syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
471 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
472 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
473 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
474 if (ioctl(s, SIOCSARP, (caddr_t)&arpreq) < 0) {
475 syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
483 * cifproxyarp - Delete the proxy ARP entry for the peer.
486 cifproxyarp(unit, hisaddr)
490 struct arpreq arpreq;
492 BZERO(&arpreq, sizeof(arpreq));
493 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
494 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
495 if (ioctl(s, SIOCDARP, (caddr_t)&arpreq) < 0) {
496 syslog(LOG_WARNING, "ioctl(SIOCDARP): %m");
503 * get_ether_addr - get the hardware address of an interface on the
504 * the same subnet as ipaddr.
509 get_ether_addr(ipaddr, hwaddr)
511 struct sockaddr *hwaddr;
513 struct ifreq *ifr, *ifend, *ifp;
515 struct sockaddr_dl *dla;
518 struct ifreq ifs[MAX_IFS];
520 ifc.ifc_len = sizeof(ifs);
522 if (ioctl(s, SIOCGIFCONF, &ifc) < 0) {
523 syslog(LOG_ERR, "ioctl(SIOCGIFCONF): %m");
528 * Scan through looking for an interface with an Internet
529 * address on the same subnet as `ipaddr'.
531 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
532 for (ifr = ifc.ifc_req; ifr < ifend; ) {
533 if (ifr->ifr_addr.sa_family == AF_INET) {
534 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
535 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
537 * Check that the interface is up, and not point-to-point
540 if (ioctl(s, SIOCGIFFLAGS, &ifreq) < 0)
542 if ((ifreq.ifr_flags &
543 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
544 != (IFF_UP|IFF_BROADCAST))
547 * Get its netmask and check that it's on the right subnet.
549 if (ioctl(s, SIOCGIFNETMASK, &ifreq) < 0)
551 mask = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
552 if ((ipaddr & mask) != (ina & mask))
557 ifr = (struct ifreq *) ((char *)&ifr->ifr_addr + ifr->ifr_addr.sa_len);
562 syslog(LOG_INFO, "found interface %s for proxy arp", ifr->ifr_name);
565 * Now scan through again looking for a link-level address
566 * for this interface.
569 for (ifr = ifc.ifc_req; ifr < ifend; ) {
570 if (strcmp(ifp->ifr_name, ifr->ifr_name) == 0
571 && ifr->ifr_addr.sa_family == AF_LINK) {
573 * Found the link-level address - copy it out
575 dla = (struct sockaddr_dl *)&ifr->ifr_addr;
576 hwaddr->sa_len = sizeof(struct sockaddr);
577 hwaddr->sa_family = AF_UNSPEC;
578 BCOPY(LLADDR(dla), hwaddr->sa_data, dla->sdl_alen);
581 ifr = (struct ifreq *) ((char *)&ifr->ifr_addr + ifr->ifr_addr.sa_len);
589 * ppp_available - check whether the system has any ppp interfaces
590 * (in fact we check whether we can do an ioctl on ppp0).
598 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
599 return 1; /* can't tell - maybe we're not root */
601 strncpy(ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
602 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;