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.8 1994/08/09 06:30:38 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>
43 #include <netinet/if_ether.h>
49 static int initdisc = -1; /* Initial TTY discipline */
50 extern int kdebugflag;
54 * establish_ppp - Turn the serial port into a ppp interface.
59 int pppdisc = PPPDISC;
62 if (ioctl(fd, TIOCGETD, &initdisc) < 0) {
63 syslog(LOG_ERR, "ioctl(TIOCGETD): %m");
66 if (ioctl(fd, TIOCSETD, &pppdisc) < 0) {
67 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
72 * Find out which interface we were given.
74 if (ioctl(fd, PPPIOCGUNIT, &ifunit) < 0) {
75 syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m");
80 * Enable debug in the driver if requested.
83 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
84 syslog(LOG_WARNING, "ioctl (PPPIOCGFLAGS): %m");
86 x |= (kdebugflag & 0xFF) * SC_DEBUG;
87 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
88 syslog(LOG_WARNING, "ioctl(PPPIOCSFLAGS): %m");
95 * disestablish_ppp - Restore the serial port to normal operation.
96 * This shouldn't call die() because it's called from die().
106 * Check whether the link seems not to be 8-bit clean.
108 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) == 0) {
110 switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP)) {
112 s = "bit 7 set to 1";
115 s = "bit 7 set to 0";
125 syslog(LOG_WARNING, "Serial link is not 8-bit clean:");
126 syslog(LOG_WARNING, "All received characters had %s", s);
129 if (ioctl(fd, TIOCSETD, &initdisc) < 0)
130 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
136 * output - Output PPP packet.
145 MAINDEBUG((LOG_WARNING, "output: unit != 0!"));
147 log_packet(p, len, "sent ");
149 if (write(fd, p, len) < 0) {
150 syslog(LOG_ERR, "write: %m");
157 * wait_input - wait until there is data available on fd,
158 * for the length of time specified by *timo (indefinite
162 struct timeval *timo;
169 n = select(fd+1, &ready, NULL, &ready, timo);
170 if (n < 0 && errno != EINTR) {
171 syslog(LOG_ERR, "select: %m");
178 * read_packet - get a PPP packet from the serial device.
186 if ((len = read(fd, buf, MTU + DLLHEADERLEN)) < 0) {
187 if (errno == EWOULDBLOCK) {
188 MAINDEBUG((LOG_DEBUG, "read(fd): EWOULDBLOCK"));
191 syslog(LOG_ERR, "read(fd): %m");
199 * ppp_send_config - configure the transmit characteristics of
203 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
211 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
213 if (ioctl(s, SIOCSIFMTU, (caddr_t) &ifr) < 0) {
214 syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m");
218 if (ioctl(fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0) {
219 syslog(LOG_ERR, "ioctl(PPPIOCSASYNCMAP): %m");
223 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
224 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
227 x = pcomp? x | SC_COMP_PROT: x &~ SC_COMP_PROT;
228 x = accomp? x | SC_COMP_AC: x &~ SC_COMP_AC;
229 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
230 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
237 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
240 ppp_set_xaccm(unit, accm)
244 if (ioctl(fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY)
245 syslog(LOG_WARNING, "ioctl(set extended ACCM): %m");
250 * ppp_recv_config - configure the receive-side characteristics of
254 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
261 if (ioctl(fd, PPPIOCSMRU, (caddr_t) &mru) < 0) {
262 syslog(LOG_ERR, "ioctl(PPPIOCSMRU): %m");
265 if (ioctl(fd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0) {
266 syslog(LOG_ERR, "ioctl(PPPIOCSRASYNCMAP): %m");
269 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
270 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
273 x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC;
274 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
275 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
281 * ccp_flags_set - inform kernel about the current state of CCP.
284 ccp_flags_set(unit, isopen, isup)
285 int unit, isopen, isup;
289 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
290 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
293 x = isopen? x | SC_CCP_OPEN: x &~ SC_CCP_OPEN;
294 x = isup? x | SC_CCP_UP: x &~ SC_CCP_UP;
295 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
296 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
300 * sifvjcomp - config tcp header compression
303 sifvjcomp(u, vjcomp, cidcomp, maxcid)
304 int u, vjcomp, cidcomp, maxcid;
308 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
309 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
312 x = vjcomp ? x | SC_COMP_TCP: x &~ SC_COMP_TCP;
313 x = cidcomp? x & ~SC_NO_TCP_CCID: x | SC_NO_TCP_CCID;
314 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
315 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
318 if (ioctl(fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0) {
319 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
326 * sifup - Config the interface up and enable IP packets to pass.
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");
345 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
346 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
350 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
351 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
358 * sifdown - Config the interface down and disable IP.
369 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
370 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
374 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
375 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
379 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
380 if (ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
381 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
384 ifr.ifr_flags &= ~IFF_UP;
385 if (ioctl(s, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
386 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
394 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
397 #define SET_SA_FAMILY(addr, family) \
398 BZERO((char *) &(addr), sizeof(addr)); \
399 addr.sa_family = (family); \
400 addr.sa_len = sizeof(addr);
403 * sifaddr - Config the interface IP addresses and netmask.
410 struct ifaliasreq ifra;
412 strncpy(ifra.ifra_name, ifname, sizeof(ifra.ifra_name));
413 SET_SA_FAMILY(ifra.ifra_addr, AF_INET);
414 ((struct sockaddr_in *) &ifra.ifra_addr)->sin_addr.s_addr = o;
415 SET_SA_FAMILY(ifra.ifra_broadaddr, AF_INET);
416 ((struct sockaddr_in *) &ifra.ifra_broadaddr)->sin_addr.s_addr = h;
418 SET_SA_FAMILY(ifra.ifra_mask, AF_INET);
419 ((struct sockaddr_in *) &ifra.ifra_mask)->sin_addr.s_addr = m;
421 BZERO(&ifra.ifra_mask, sizeof(ifra.ifra_mask));
422 if (ioctl(s, SIOCAIFADDR, (caddr_t) &ifra) < 0) {
423 if (errno != EEXIST) {
424 syslog(LOG_ERR, "ioctl(SIOCAIFADDR): %m");
427 syslog(LOG_WARNING, "ioctl(SIOCAIFADDR): Address already exists");
433 * cifaddr - Clear the interface IP addresses, and delete routes
434 * through the interface if possible.
441 struct ifaliasreq ifra;
443 strncpy(ifra.ifra_name, ifname, sizeof(ifra.ifra_name));
444 SET_SA_FAMILY(ifra.ifra_addr, AF_INET);
445 ((struct sockaddr_in *) &ifra.ifra_addr)->sin_addr.s_addr = o;
446 SET_SA_FAMILY(ifra.ifra_broadaddr, AF_INET);
447 ((struct sockaddr_in *) &ifra.ifra_broadaddr)->sin_addr.s_addr = h;
448 BZERO(&ifra.ifra_mask, sizeof(ifra.ifra_mask));
449 if (ioctl(s, SIOCDIFADDR, (caddr_t) &ifra) < 0) {
450 syslog(LOG_WARNING, "ioctl(SIOCDIFADDR): %m");
457 * sifdefaultroute - assign a default route through the address given.
460 sifdefaultroute(u, g)
464 return dodefaultroute(g, 's');
468 * cifdefaultroute - delete a default route through the address given.
471 cifdefaultroute(u, g)
475 return dodefaultroute(g, 'c');
479 * dodefaultroute - talk to a routing socket to add/delete a default route.
482 dodefaultroute(g, cmd)
488 struct rt_msghdr hdr;
489 struct sockaddr_in dst;
490 struct sockaddr_in gway;
491 struct sockaddr_in mask;
494 if ((routes = socket(PF_ROUTE, SOCK_RAW, AF_INET)) < 0) {
495 syslog(LOG_ERR, "%cifdefaultroute: opening routing socket: %m", cmd);
499 memset(&rtmsg, 0, sizeof(rtmsg));
500 rtmsg.hdr.rtm_type = cmd == 's'? RTM_ADD: RTM_DELETE;
501 rtmsg.hdr.rtm_flags = RTF_UP | RTF_GATEWAY;
502 rtmsg.hdr.rtm_version = RTM_VERSION;
503 rtmsg.hdr.rtm_seq = ++rtm_seq;
504 rtmsg.hdr.rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
505 rtmsg.dst.sin_len = sizeof(rtmsg.dst);
506 rtmsg.dst.sin_family = AF_INET;
507 rtmsg.gway.sin_len = sizeof(rtmsg.gway);
508 rtmsg.gway.sin_family = AF_INET;
509 rtmsg.gway.sin_addr.s_addr = g;
510 rtmsg.mask.sin_len = sizeof(rtmsg.dst);
511 rtmsg.mask.sin_family = AF_INET;
513 rtmsg.hdr.rtm_msglen = sizeof(rtmsg);
514 if (write(routes, &rtmsg, sizeof(rtmsg)) < 0) {
515 syslog(LOG_ERR, "%s default route: %m", cmd=='s'? "add": "delete");
527 * sifproxyarp - Make a proxy ARP entry for the peer.
530 struct rt_msghdr hdr;
531 struct sockaddr_inarp dst;
532 struct sockaddr_dl hwa;
536 static int arpmsg_valid;
539 sifproxyarp(unit, hisaddr)
547 * Get the hardware address of an interface on the same subnet
548 * as our local address.
550 memset(&arpmsg, 0, sizeof(arpmsg));
551 if (!get_ether_addr(hisaddr, &arpmsg.hwa)) {
552 syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
556 if ((routes = socket(PF_ROUTE, SOCK_RAW, AF_INET)) < 0) {
557 syslog(LOG_ERR, "sifproxyarp: opening routing socket: %m");
561 arpmsg.hdr.rtm_type = RTM_ADD;
562 arpmsg.hdr.rtm_flags = RTF_ANNOUNCE | RTF_HOST | RTF_STATIC;
563 arpmsg.hdr.rtm_version = RTM_VERSION;
564 arpmsg.hdr.rtm_seq = ++rtm_seq;
565 arpmsg.hdr.rtm_addrs = RTA_DST | RTA_GATEWAY;
566 arpmsg.hdr.rtm_inits = RTV_EXPIRE;
567 arpmsg.dst.sin_len = sizeof(struct sockaddr_inarp);
568 arpmsg.dst.sin_family = AF_INET;
569 arpmsg.dst.sin_addr.s_addr = hisaddr;
570 arpmsg.dst.sin_other = SIN_PROXY;
572 arpmsg.hdr.rtm_msglen = (char *) &arpmsg.hwa - (char *) &arpmsg
573 + arpmsg.hwa.sdl_len;
574 if (write(routes, &arpmsg, arpmsg.hdr.rtm_msglen) < 0) {
575 syslog(LOG_ERR, "add proxy arp entry: %m");
586 * cifproxyarp - Delete the proxy ARP entry for the peer.
589 cifproxyarp(unit, hisaddr)
599 arpmsg.hdr.rtm_type = RTM_DELETE;
600 arpmsg.hdr.rtm_seq = ++rtm_seq;
602 if ((routes = socket(PF_ROUTE, SOCK_RAW, AF_INET)) < 0) {
603 syslog(LOG_ERR, "sifproxyarp: opening routing socket: %m");
607 if (write(routes, &arpmsg, arpmsg.hdr.rtm_msglen) < 0) {
608 syslog(LOG_ERR, "delete proxy arp entry: %m");
617 #else /* RTM_VERSION */
620 * sifproxyarp - Make a proxy ARP entry for the peer.
623 sifproxyarp(unit, hisaddr)
627 struct arpreq arpreq;
629 struct sockaddr_dl sdl;
633 BZERO(&arpreq, sizeof(arpreq));
636 * Get the hardware address of an interface on the same subnet
637 * as our local address.
639 if (!get_ether_addr(hisaddr, &dls.sdl)) {
640 syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
644 arpreq.arp_ha.sa_len = sizeof(struct sockaddr);
645 arpreq.arp_ha.sa_family = AF_UNSPEC;
646 BCOPY(LLADDR(&dls.sdl), arpreq.arp_ha.sa_data, dls.sdl.sdl_alen);
647 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
648 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
649 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
650 if (ioctl(s, SIOCSARP, (caddr_t)&arpreq) < 0) {
651 syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
659 * cifproxyarp - Delete the proxy ARP entry for the peer.
662 cifproxyarp(unit, hisaddr)
666 struct arpreq arpreq;
668 BZERO(&arpreq, sizeof(arpreq));
669 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
670 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
671 if (ioctl(s, SIOCDARP, (caddr_t)&arpreq) < 0) {
672 syslog(LOG_WARNING, "ioctl(SIOCDARP): %m");
677 #endif /* RTM_VERSION */
681 * get_ether_addr - get the hardware address of an interface on the
682 * the same subnet as ipaddr.
687 get_ether_addr(ipaddr, hwaddr)
689 struct sockaddr_dl *hwaddr;
691 struct ifreq *ifr, *ifend, *ifp;
693 struct sockaddr_dl *dla;
696 struct ifreq ifs[MAX_IFS];
698 ifc.ifc_len = sizeof(ifs);
700 if (ioctl(s, SIOCGIFCONF, &ifc) < 0) {
701 syslog(LOG_ERR, "ioctl(SIOCGIFCONF): %m");
706 * Scan through looking for an interface with an Internet
707 * address on the same subnet as `ipaddr'.
709 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
710 for (ifr = ifc.ifc_req; ifr < ifend; ) {
711 if (ifr->ifr_addr.sa_family == AF_INET) {
712 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
713 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
715 * Check that the interface is up, and not point-to-point
718 if (ioctl(s, SIOCGIFFLAGS, &ifreq) < 0)
720 if ((ifreq.ifr_flags &
721 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
722 != (IFF_UP|IFF_BROADCAST))
725 * Get its netmask and check that it's on the right subnet.
727 if (ioctl(s, SIOCGIFNETMASK, &ifreq) < 0)
729 mask = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
730 if ((ipaddr & mask) != (ina & mask))
735 ifr = (struct ifreq *) ((char *)&ifr->ifr_addr + ifr->ifr_addr.sa_len);
740 syslog(LOG_INFO, "found interface %s for proxy arp", ifr->ifr_name);
743 * Now scan through again looking for a link-level address
744 * for this interface.
747 for (ifr = ifc.ifc_req; ifr < ifend; ) {
748 if (strcmp(ifp->ifr_name, ifr->ifr_name) == 0
749 && ifr->ifr_addr.sa_family == AF_LINK) {
751 * Found the link-level address - copy it out
753 dla = (struct sockaddr_dl *) &ifr->ifr_addr;
754 BCOPY(dla, hwaddr, dla->sdl_len);
757 ifr = (struct ifreq *) ((char *)&ifr->ifr_addr + ifr->ifr_addr.sa_len);
765 * ppp_available - check whether the system has any ppp interfaces
766 * (in fact we check whether we can do an ioctl on ppp0).
774 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
775 return 1; /* can't tell - maybe we're not root */
777 strncpy(ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
778 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;