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.7 1994/05/30 06:10:07 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 * read_packet - get a PPP packet from the serial device.
165 if ((len = read(fd, buf, MTU + DLLHEADERLEN)) < 0) {
166 if (errno == EWOULDBLOCK) {
167 MAINDEBUG((LOG_DEBUG, "read(fd): EWOULDBLOCK"));
170 syslog(LOG_ERR, "read(fd): %m");
178 * ppp_send_config - configure the transmit characteristics of
182 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
190 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
192 if (ioctl(s, SIOCSIFMTU, (caddr_t) &ifr) < 0) {
193 syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m");
197 if (ioctl(fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0) {
198 syslog(LOG_ERR, "ioctl(PPPIOCSASYNCMAP): %m");
202 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
203 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
206 x = pcomp? x | SC_COMP_PROT: x &~ SC_COMP_PROT;
207 x = accomp? x | SC_COMP_AC: x &~ SC_COMP_AC;
208 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
209 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
216 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
219 ppp_set_xaccm(unit, accm)
223 if (ioctl(fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY)
224 syslog(LOG_WARNING, "ioctl(set extended ACCM): %m");
229 * ppp_recv_config - configure the receive-side characteristics of
233 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
240 if (ioctl(fd, PPPIOCSMRU, (caddr_t) &mru) < 0) {
241 syslog(LOG_ERR, "ioctl(PPPIOCSMRU): %m");
244 if (ioctl(fd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0) {
245 syslog(LOG_ERR, "ioctl(PPPIOCSRASYNCMAP): %m");
248 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
249 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
252 x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC;
253 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
254 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
260 * sifvjcomp - config tcp header compression
263 sifvjcomp(u, vjcomp, cidcomp, maxcid)
264 int u, vjcomp, cidcomp, maxcid;
268 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
269 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
272 x = vjcomp ? x | SC_COMP_TCP: x &~ SC_COMP_TCP;
273 x = cidcomp? x & ~SC_NO_TCP_CCID: x | SC_NO_TCP_CCID;
274 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
275 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
278 if (ioctl(fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0) {
279 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
286 * sifup - Config the interface up and enable IP packets to pass.
295 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
296 if (ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
297 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
300 ifr.ifr_flags |= IFF_UP;
301 if (ioctl(s, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
302 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
305 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
306 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
310 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
311 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
318 * sifdown - Config the interface down and disable IP.
329 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
330 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
334 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
335 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
339 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
340 if (ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
341 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
344 ifr.ifr_flags &= ~IFF_UP;
345 if (ioctl(s, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
346 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
354 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
357 #define SET_SA_FAMILY(addr, family) \
358 BZERO((char *) &(addr), sizeof(addr)); \
359 addr.sa_family = (family); \
360 addr.sa_len = sizeof(addr);
363 * sifaddr - Config the interface IP addresses and netmask.
370 struct ifaliasreq ifra;
372 strncpy(ifra.ifra_name, ifname, sizeof(ifra.ifra_name));
373 SET_SA_FAMILY(ifra.ifra_addr, AF_INET);
374 ((struct sockaddr_in *) &ifra.ifra_addr)->sin_addr.s_addr = o;
375 SET_SA_FAMILY(ifra.ifra_broadaddr, AF_INET);
376 ((struct sockaddr_in *) &ifra.ifra_broadaddr)->sin_addr.s_addr = h;
378 SET_SA_FAMILY(ifra.ifra_mask, AF_INET);
379 ((struct sockaddr_in *) &ifra.ifra_mask)->sin_addr.s_addr = m;
381 BZERO(&ifra.ifra_mask, sizeof(ifra.ifra_mask));
382 if (ioctl(s, SIOCAIFADDR, (caddr_t) &ifra) < 0) {
383 if (errno != EEXIST) {
384 syslog(LOG_ERR, "ioctl(SIOCAIFADDR): %m");
387 syslog(LOG_WARNING, "ioctl(SIOCAIFADDR): Address already exists");
393 * cifaddr - Clear the interface IP addresses, and delete routes
394 * through the interface if possible.
401 struct ifaliasreq ifra;
403 strncpy(ifra.ifra_name, ifname, sizeof(ifra.ifra_name));
404 SET_SA_FAMILY(ifra.ifra_addr, AF_INET);
405 ((struct sockaddr_in *) &ifra.ifra_addr)->sin_addr.s_addr = o;
406 SET_SA_FAMILY(ifra.ifra_broadaddr, AF_INET);
407 ((struct sockaddr_in *) &ifra.ifra_broadaddr)->sin_addr.s_addr = h;
408 BZERO(&ifra.ifra_mask, sizeof(ifra.ifra_mask));
409 if (ioctl(s, SIOCDIFADDR, (caddr_t) &ifra) < 0) {
410 syslog(LOG_WARNING, "ioctl(SIOCDIFADDR): %m");
418 * sifdefaultroute - assign a default route through the address given.
421 sifdefaultroute(u, g)
425 return dodefaultroute(g, 's');
429 * cifdefaultroute - delete a default route through the address given.
432 cifdefaultroute(u, g)
436 return dodefaultroute(g, 'c');
440 * dodefaultroute - talk to a routing socket to add/delete a default route.
443 dodefaultroute(g, cmd)
449 struct rt_msghdr hdr;
450 struct sockaddr_in dst;
451 struct sockaddr_in gway;
452 struct sockaddr_in mask;
455 if ((routes = socket(PF_ROUTE, SOCK_RAW, AF_INET)) < 0) {
456 syslog(LOG_ERR, "%cifdefaultroute: opening routing socket: %m", cmd);
460 memset(&rtmsg, 0, sizeof(rtmsg));
461 rtmsg.hdr.rtm_type = cmd == 's'? RTM_ADD: RTM_DELETE;
462 rtmsg.hdr.rtm_flags = RTF_UP | RTF_GATEWAY;
463 rtmsg.hdr.rtm_version = RTM_VERSION;
464 rtmsg.hdr.rtm_seq = ++rtm_seq;
465 rtmsg.hdr.rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
466 rtmsg.dst.sin_len = sizeof(rtmsg.dst);
467 rtmsg.dst.sin_family = AF_INET;
468 rtmsg.gway.sin_len = sizeof(rtmsg.gway);
469 rtmsg.gway.sin_family = AF_INET;
470 rtmsg.gway.sin_addr.s_addr = g;
471 rtmsg.mask.sin_len = sizeof(rtmsg.dst);
472 rtmsg.mask.sin_family = AF_INET;
474 rtmsg.hdr.rtm_msglen = sizeof(rtmsg);
475 if (write(routes, &rtmsg, sizeof(rtmsg)) < 0) {
476 syslog(LOG_ERR, "%s default route: %m", cmd=='s'? "add": "delete");
488 * sifproxyarp - Make a proxy ARP entry for the peer.
491 struct rt_msghdr hdr;
492 struct sockaddr_inarp dst;
493 struct sockaddr_dl hwa;
497 static int arpmsg_valid;
500 sifproxyarp(unit, hisaddr)
508 * Get the hardware address of an interface on the same subnet
509 * as our local address.
511 memset(&arpmsg, 0, sizeof(arpmsg));
512 if (!get_ether_addr(hisaddr, &arpmsg.hwa)) {
513 syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
517 if ((routes = socket(PF_ROUTE, SOCK_RAW, AF_INET)) < 0) {
518 syslog(LOG_ERR, "sifproxyarp: opening routing socket: %m");
522 arpmsg.hdr.rtm_type = RTM_ADD;
523 arpmsg.hdr.rtm_flags = RTF_ANNOUNCE | RTF_HOST | RTF_STATIC;
524 arpmsg.hdr.rtm_version = RTM_VERSION;
525 arpmsg.hdr.rtm_seq = ++rtm_seq;
526 arpmsg.hdr.rtm_addrs = RTA_DST | RTA_GATEWAY;
527 arpmsg.hdr.rtm_inits = RTV_EXPIRE;
528 arpmsg.dst.sin_len = sizeof(struct sockaddr_inarp);
529 arpmsg.dst.sin_family = AF_INET;
530 arpmsg.dst.sin_addr.s_addr = hisaddr;
531 arpmsg.dst.sin_other = SIN_PROXY;
533 arpmsg.hdr.rtm_msglen = (char *) &arpmsg.hwa - (char *) &arpmsg
534 + arpmsg.hwa.sdl_len;
535 if (write(routes, &arpmsg, arpmsg.hdr.rtm_msglen) < 0) {
536 syslog(LOG_ERR, "add proxy arp entry: %m");
547 * cifproxyarp - Delete the proxy ARP entry for the peer.
550 cifproxyarp(unit, hisaddr)
560 arpmsg.hdr.rtm_type = RTM_DELETE;
561 arpmsg.hdr.rtm_seq = ++rtm_seq;
563 if ((routes = socket(PF_ROUTE, SOCK_RAW, AF_INET)) < 0) {
564 syslog(LOG_ERR, "sifproxyarp: opening routing socket: %m");
568 if (write(routes, &arpmsg, arpmsg.hdr.rtm_msglen) < 0) {
569 syslog(LOG_ERR, "delete proxy arp entry: %m");
578 #else /* RTM_VERSION */
581 * sifproxyarp - Make a proxy ARP entry for the peer.
584 sifproxyarp(unit, hisaddr)
588 struct arpreq arpreq;
590 struct sockaddr_dl sdl;
594 BZERO(&arpreq, sizeof(arpreq));
597 * Get the hardware address of an interface on the same subnet
598 * as our local address.
600 if (!get_ether_addr(hisaddr, &dls.sdl)) {
601 syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
605 arpreq.arp_ha.sa_len = sizeof(struct sockaddr);
606 arpreq.arp_ha.sa_family = AF_UNSPEC;
607 BCOPY(LLADDR(&dls.sdl), arpreq.arp_ha.sa_data, dls.sdl.sdl_alen);
608 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
609 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
610 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
611 if (ioctl(s, SIOCSARP, (caddr_t)&arpreq) < 0) {
612 syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
620 * cifproxyarp - Delete the proxy ARP entry for the peer.
623 cifproxyarp(unit, hisaddr)
627 struct arpreq arpreq;
629 BZERO(&arpreq, sizeof(arpreq));
630 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
631 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
632 if (ioctl(s, SIOCDARP, (caddr_t)&arpreq) < 0) {
633 syslog(LOG_WARNING, "ioctl(SIOCDARP): %m");
638 #endif /* RTM_VERSION */
642 * get_ether_addr - get the hardware address of an interface on the
643 * the same subnet as ipaddr.
648 get_ether_addr(ipaddr, hwaddr)
650 struct sockaddr_dl *hwaddr;
652 struct ifreq *ifr, *ifend, *ifp;
654 struct sockaddr_dl *dla;
657 struct ifreq ifs[MAX_IFS];
659 ifc.ifc_len = sizeof(ifs);
661 if (ioctl(s, SIOCGIFCONF, &ifc) < 0) {
662 syslog(LOG_ERR, "ioctl(SIOCGIFCONF): %m");
667 * Scan through looking for an interface with an Internet
668 * address on the same subnet as `ipaddr'.
670 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
671 for (ifr = ifc.ifc_req; ifr < ifend; ) {
672 if (ifr->ifr_addr.sa_family == AF_INET) {
673 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
674 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
676 * Check that the interface is up, and not point-to-point
679 if (ioctl(s, SIOCGIFFLAGS, &ifreq) < 0)
681 if ((ifreq.ifr_flags &
682 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
683 != (IFF_UP|IFF_BROADCAST))
686 * Get its netmask and check that it's on the right subnet.
688 if (ioctl(s, SIOCGIFNETMASK, &ifreq) < 0)
690 mask = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
691 if ((ipaddr & mask) != (ina & mask))
696 ifr = (struct ifreq *) ((char *)&ifr->ifr_addr + ifr->ifr_addr.sa_len);
701 syslog(LOG_INFO, "found interface %s for proxy arp", ifr->ifr_name);
704 * Now scan through again looking for a link-level address
705 * for this interface.
708 for (ifr = ifc.ifc_req; ifr < ifend; ) {
709 if (strcmp(ifp->ifr_name, ifr->ifr_name) == 0
710 && ifr->ifr_addr.sa_family == AF_LINK) {
712 * Found the link-level address - copy it out
714 dla = (struct sockaddr_dl *) &ifr->ifr_addr;
715 BCOPY(dla, hwaddr, dla->sdl_len);
718 ifr = (struct ifreq *) ((char *)&ifr->ifr_addr + ifr->ifr_addr.sa_len);
726 * ppp_available - check whether the system has any ppp interfaces
727 * (in fact we check whether we can do an ioctl on ppp0).
735 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
736 return 1; /* can't tell - maybe we're not root */
738 strncpy(ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
739 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;