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.9 1994/08/22 00:41:00 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_test - ask kernel whether a given compression method
282 * is acceptable for use.
284 ccp_test(unit, opt_ptr, opt_len, for_transmit)
285 int unit, opt_len, for_transmit;
288 struct ppp_comp_data data;
291 data.length = opt_len;
292 data.transmit = for_transmit;
293 return ioctl(fd, PPPIOCSCOMPRESS, (caddr_t) &data) >= 0;
297 * ccp_flags_set - inform kernel about the current state of CCP.
300 ccp_flags_set(unit, isopen, isup)
301 int unit, isopen, isup;
305 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
306 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
309 x = isopen? x | SC_CCP_OPEN: x &~ SC_CCP_OPEN;
310 x = isup? x | SC_CCP_UP: x &~ SC_CCP_UP;
311 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
312 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
316 * sifvjcomp - config tcp header compression
319 sifvjcomp(u, vjcomp, cidcomp, maxcid)
320 int u, vjcomp, cidcomp, maxcid;
324 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
325 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
328 x = vjcomp ? x | SC_COMP_TCP: x &~ SC_COMP_TCP;
329 x = cidcomp? x & ~SC_NO_TCP_CCID: x | SC_NO_TCP_CCID;
330 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
331 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
334 if (ioctl(fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0) {
335 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
342 * sifup - Config the interface up and enable IP packets to pass.
351 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
352 if (ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
353 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
356 ifr.ifr_flags |= IFF_UP;
357 if (ioctl(s, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
358 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
361 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
362 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
366 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
367 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
374 * sifdown - Config the interface down and disable IP.
385 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
386 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
390 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
391 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
395 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
396 if (ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
397 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
400 ifr.ifr_flags &= ~IFF_UP;
401 if (ioctl(s, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
402 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
410 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
413 #define SET_SA_FAMILY(addr, family) \
414 BZERO((char *) &(addr), sizeof(addr)); \
415 addr.sa_family = (family); \
416 addr.sa_len = sizeof(addr);
419 * sifaddr - Config the interface IP addresses and netmask.
426 struct ifaliasreq ifra;
428 strncpy(ifra.ifra_name, ifname, sizeof(ifra.ifra_name));
429 SET_SA_FAMILY(ifra.ifra_addr, AF_INET);
430 ((struct sockaddr_in *) &ifra.ifra_addr)->sin_addr.s_addr = o;
431 SET_SA_FAMILY(ifra.ifra_broadaddr, AF_INET);
432 ((struct sockaddr_in *) &ifra.ifra_broadaddr)->sin_addr.s_addr = h;
434 SET_SA_FAMILY(ifra.ifra_mask, AF_INET);
435 ((struct sockaddr_in *) &ifra.ifra_mask)->sin_addr.s_addr = m;
437 BZERO(&ifra.ifra_mask, sizeof(ifra.ifra_mask));
438 if (ioctl(s, SIOCAIFADDR, (caddr_t) &ifra) < 0) {
439 if (errno != EEXIST) {
440 syslog(LOG_ERR, "ioctl(SIOCAIFADDR): %m");
443 syslog(LOG_WARNING, "ioctl(SIOCAIFADDR): Address already exists");
449 * cifaddr - Clear the interface IP addresses, and delete routes
450 * through the interface if possible.
457 struct ifaliasreq ifra;
459 strncpy(ifra.ifra_name, ifname, sizeof(ifra.ifra_name));
460 SET_SA_FAMILY(ifra.ifra_addr, AF_INET);
461 ((struct sockaddr_in *) &ifra.ifra_addr)->sin_addr.s_addr = o;
462 SET_SA_FAMILY(ifra.ifra_broadaddr, AF_INET);
463 ((struct sockaddr_in *) &ifra.ifra_broadaddr)->sin_addr.s_addr = h;
464 BZERO(&ifra.ifra_mask, sizeof(ifra.ifra_mask));
465 if (ioctl(s, SIOCDIFADDR, (caddr_t) &ifra) < 0) {
466 syslog(LOG_WARNING, "ioctl(SIOCDIFADDR): %m");
473 * sifdefaultroute - assign a default route through the address given.
476 sifdefaultroute(u, g)
480 return dodefaultroute(g, 's');
484 * cifdefaultroute - delete a default route through the address given.
487 cifdefaultroute(u, g)
491 return dodefaultroute(g, 'c');
495 * dodefaultroute - talk to a routing socket to add/delete a default route.
498 dodefaultroute(g, cmd)
504 struct rt_msghdr hdr;
505 struct sockaddr_in dst;
506 struct sockaddr_in gway;
507 struct sockaddr_in mask;
510 if ((routes = socket(PF_ROUTE, SOCK_RAW, AF_INET)) < 0) {
511 syslog(LOG_ERR, "%cifdefaultroute: opening routing socket: %m", cmd);
515 memset(&rtmsg, 0, sizeof(rtmsg));
516 rtmsg.hdr.rtm_type = cmd == 's'? RTM_ADD: RTM_DELETE;
517 rtmsg.hdr.rtm_flags = RTF_UP | RTF_GATEWAY;
518 rtmsg.hdr.rtm_version = RTM_VERSION;
519 rtmsg.hdr.rtm_seq = ++rtm_seq;
520 rtmsg.hdr.rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
521 rtmsg.dst.sin_len = sizeof(rtmsg.dst);
522 rtmsg.dst.sin_family = AF_INET;
523 rtmsg.gway.sin_len = sizeof(rtmsg.gway);
524 rtmsg.gway.sin_family = AF_INET;
525 rtmsg.gway.sin_addr.s_addr = g;
526 rtmsg.mask.sin_len = sizeof(rtmsg.dst);
527 rtmsg.mask.sin_family = AF_INET;
529 rtmsg.hdr.rtm_msglen = sizeof(rtmsg);
530 if (write(routes, &rtmsg, sizeof(rtmsg)) < 0) {
531 syslog(LOG_ERR, "%s default route: %m", cmd=='s'? "add": "delete");
543 * sifproxyarp - Make a proxy ARP entry for the peer.
546 struct rt_msghdr hdr;
547 struct sockaddr_inarp dst;
548 struct sockaddr_dl hwa;
552 static int arpmsg_valid;
555 sifproxyarp(unit, hisaddr)
563 * Get the hardware address of an interface on the same subnet
564 * as our local address.
566 memset(&arpmsg, 0, sizeof(arpmsg));
567 if (!get_ether_addr(hisaddr, &arpmsg.hwa)) {
568 syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
572 if ((routes = socket(PF_ROUTE, SOCK_RAW, AF_INET)) < 0) {
573 syslog(LOG_ERR, "sifproxyarp: opening routing socket: %m");
577 arpmsg.hdr.rtm_type = RTM_ADD;
578 arpmsg.hdr.rtm_flags = RTF_ANNOUNCE | RTF_HOST | RTF_STATIC;
579 arpmsg.hdr.rtm_version = RTM_VERSION;
580 arpmsg.hdr.rtm_seq = ++rtm_seq;
581 arpmsg.hdr.rtm_addrs = RTA_DST | RTA_GATEWAY;
582 arpmsg.hdr.rtm_inits = RTV_EXPIRE;
583 arpmsg.dst.sin_len = sizeof(struct sockaddr_inarp);
584 arpmsg.dst.sin_family = AF_INET;
585 arpmsg.dst.sin_addr.s_addr = hisaddr;
586 arpmsg.dst.sin_other = SIN_PROXY;
588 arpmsg.hdr.rtm_msglen = (char *) &arpmsg.hwa - (char *) &arpmsg
589 + arpmsg.hwa.sdl_len;
590 if (write(routes, &arpmsg, arpmsg.hdr.rtm_msglen) < 0) {
591 syslog(LOG_ERR, "add proxy arp entry: %m");
602 * cifproxyarp - Delete the proxy ARP entry for the peer.
605 cifproxyarp(unit, hisaddr)
615 arpmsg.hdr.rtm_type = RTM_DELETE;
616 arpmsg.hdr.rtm_seq = ++rtm_seq;
618 if ((routes = socket(PF_ROUTE, SOCK_RAW, AF_INET)) < 0) {
619 syslog(LOG_ERR, "sifproxyarp: opening routing socket: %m");
623 if (write(routes, &arpmsg, arpmsg.hdr.rtm_msglen) < 0) {
624 syslog(LOG_ERR, "delete proxy arp entry: %m");
633 #else /* RTM_VERSION */
636 * sifproxyarp - Make a proxy ARP entry for the peer.
639 sifproxyarp(unit, hisaddr)
643 struct arpreq arpreq;
645 struct sockaddr_dl sdl;
649 BZERO(&arpreq, sizeof(arpreq));
652 * Get the hardware address of an interface on the same subnet
653 * as our local address.
655 if (!get_ether_addr(hisaddr, &dls.sdl)) {
656 syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
660 arpreq.arp_ha.sa_len = sizeof(struct sockaddr);
661 arpreq.arp_ha.sa_family = AF_UNSPEC;
662 BCOPY(LLADDR(&dls.sdl), arpreq.arp_ha.sa_data, dls.sdl.sdl_alen);
663 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
664 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
665 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
666 if (ioctl(s, SIOCSARP, (caddr_t)&arpreq) < 0) {
667 syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
675 * cifproxyarp - Delete the proxy ARP entry for the peer.
678 cifproxyarp(unit, hisaddr)
682 struct arpreq arpreq;
684 BZERO(&arpreq, sizeof(arpreq));
685 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
686 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
687 if (ioctl(s, SIOCDARP, (caddr_t)&arpreq) < 0) {
688 syslog(LOG_WARNING, "ioctl(SIOCDARP): %m");
693 #endif /* RTM_VERSION */
697 * get_ether_addr - get the hardware address of an interface on the
698 * the same subnet as ipaddr.
703 get_ether_addr(ipaddr, hwaddr)
705 struct sockaddr_dl *hwaddr;
707 struct ifreq *ifr, *ifend, *ifp;
709 struct sockaddr_dl *dla;
712 struct ifreq ifs[MAX_IFS];
714 ifc.ifc_len = sizeof(ifs);
716 if (ioctl(s, SIOCGIFCONF, &ifc) < 0) {
717 syslog(LOG_ERR, "ioctl(SIOCGIFCONF): %m");
722 * Scan through looking for an interface with an Internet
723 * address on the same subnet as `ipaddr'.
725 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
726 for (ifr = ifc.ifc_req; ifr < ifend; ) {
727 if (ifr->ifr_addr.sa_family == AF_INET) {
728 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
729 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
731 * Check that the interface is up, and not point-to-point
734 if (ioctl(s, SIOCGIFFLAGS, &ifreq) < 0)
736 if ((ifreq.ifr_flags &
737 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
738 != (IFF_UP|IFF_BROADCAST))
741 * Get its netmask and check that it's on the right subnet.
743 if (ioctl(s, SIOCGIFNETMASK, &ifreq) < 0)
745 mask = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
746 if ((ipaddr & mask) != (ina & mask))
751 ifr = (struct ifreq *) ((char *)&ifr->ifr_addr + ifr->ifr_addr.sa_len);
756 syslog(LOG_INFO, "found interface %s for proxy arp", ifr->ifr_name);
759 * Now scan through again looking for a link-level address
760 * for this interface.
763 for (ifr = ifc.ifc_req; ifr < ifend; ) {
764 if (strcmp(ifp->ifr_name, ifr->ifr_name) == 0
765 && ifr->ifr_addr.sa_family == AF_LINK) {
767 * Found the link-level address - copy it out
769 dla = (struct sockaddr_dl *) &ifr->ifr_addr;
770 BCOPY(dla, hwaddr, dla->sdl_len);
773 ifr = (struct ifreq *) ((char *)&ifr->ifr_addr + ifr->ifr_addr.sa_len);
781 * ppp_available - check whether the system has any ppp interfaces
782 * (in fact we check whether we can do an ioctl on ppp0).
790 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
791 return 1; /* can't tell - maybe we're not root */
793 strncpy(ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
794 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;