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.5 1994/05/09 02:33:41 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.
289 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
290 if (ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
291 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
294 ifr.ifr_flags |= IFF_UP;
295 if (ioctl(s, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
296 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
299 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
300 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
304 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
305 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
312 * sifdown - Config the interface down and disable IP.
322 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
323 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
327 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
328 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
332 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
333 if (ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
334 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
337 ifr.ifr_flags &= ~IFF_UP;
338 if (ioctl(s, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
339 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
347 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
350 #define SET_SA_FAMILY(addr, family) \
351 BZERO((char *) &(addr), sizeof(addr)); \
352 addr.sa_family = (family); \
353 addr.sa_len = sizeof(addr);
356 * sifaddr - Config the interface IP addresses and netmask.
361 struct ifaliasreq ifra;
363 strncpy(ifra.ifra_name, ifname, sizeof(ifra.ifra_name));
364 SET_SA_FAMILY(ifra.ifra_addr, AF_INET);
365 ((struct sockaddr_in *) &ifra.ifra_addr)->sin_addr.s_addr = o;
366 SET_SA_FAMILY(ifra.ifra_broadaddr, AF_INET);
367 ((struct sockaddr_in *) &ifra.ifra_broadaddr)->sin_addr.s_addr = h;
369 SET_SA_FAMILY(ifra.ifra_mask, AF_INET);
370 ((struct sockaddr_in *) &ifra.ifra_mask)->sin_addr.s_addr = m;
372 BZERO(&ifra.ifra_mask, sizeof(ifra.ifra_mask));
373 if (ioctl(s, SIOCAIFADDR, (caddr_t) &ifra) < 0) {
374 if (errno != EEXIST) {
375 syslog(LOG_ERR, "ioctl(SIOCAIFADDR): %m");
378 syslog(LOG_WARNING, "ioctl(SIOCAIFADDR): Address already exists");
384 * cifaddr - Clear the interface IP addresses, and delete routes
385 * through the interface if possible.
390 struct ifaliasreq ifra;
392 strncpy(ifra.ifra_name, ifname, sizeof(ifra.ifra_name));
393 SET_SA_FAMILY(ifra.ifra_addr, AF_INET);
394 ((struct sockaddr_in *) &ifra.ifra_addr)->sin_addr.s_addr = o;
395 SET_SA_FAMILY(ifra.ifra_broadaddr, AF_INET);
396 ((struct sockaddr_in *) &ifra.ifra_broadaddr)->sin_addr.s_addr = h;
397 BZERO(&ifra.ifra_mask, sizeof(ifra.ifra_mask));
398 if (ioctl(s, SIOCDIFADDR, (caddr_t) &ifra) < 0) {
399 syslog(LOG_WARNING, "ioctl(SIOCDIFADDR): %m");
406 * sifdefaultroute - assign a default route through the address given.
409 sifdefaultroute(u, g)
413 SET_SA_FAMILY(rt.rt_dst, AF_INET);
414 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
415 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
416 rt.rt_flags = RTF_GATEWAY;
417 if (ioctl(s, SIOCADDRT, &rt) < 0) {
418 syslog(LOG_ERR, "default route ioctl(SIOCADDRT): %m");
425 * cifdefaultroute - delete a default route through the address given.
428 cifdefaultroute(u, g)
432 SET_SA_FAMILY(rt.rt_dst, AF_INET);
433 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
434 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
435 rt.rt_flags = RTF_GATEWAY;
436 if (ioctl(s, SIOCDELRT, &rt) < 0)
437 syslog(LOG_WARNING, "default route ioctl(SIOCDELRT): %m");
441 * sifproxyarp - Make a proxy ARP entry for the peer.
444 sifproxyarp(unit, hisaddr)
448 struct arpreq arpreq;
450 BZERO(&arpreq, sizeof(arpreq));
453 * Get the hardware address of an interface on the same subnet
454 * as our local address.
456 if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) {
457 syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
461 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
462 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
463 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
464 if (ioctl(s, SIOCSARP, (caddr_t)&arpreq) < 0) {
465 syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
473 * cifproxyarp - Delete the proxy ARP entry for the peer.
476 cifproxyarp(unit, hisaddr)
480 struct arpreq arpreq;
482 BZERO(&arpreq, sizeof(arpreq));
483 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
484 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
485 if (ioctl(s, SIOCDARP, (caddr_t)&arpreq) < 0) {
486 syslog(LOG_WARNING, "ioctl(SIOCDARP): %m");
493 * get_ether_addr - get the hardware address of an interface on the
494 * the same subnet as ipaddr.
499 get_ether_addr(ipaddr, hwaddr)
501 struct sockaddr *hwaddr;
503 struct ifreq *ifr, *ifend, *ifp;
505 struct sockaddr_dl *dla;
508 struct ifreq ifs[MAX_IFS];
510 ifc.ifc_len = sizeof(ifs);
512 if (ioctl(s, SIOCGIFCONF, &ifc) < 0) {
513 syslog(LOG_ERR, "ioctl(SIOCGIFCONF): %m");
518 * Scan through looking for an interface with an Internet
519 * address on the same subnet as `ipaddr'.
521 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
522 for (ifr = ifc.ifc_req; ifr < ifend; ) {
523 if (ifr->ifr_addr.sa_family == AF_INET) {
524 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
525 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
527 * Check that the interface is up, and not point-to-point
530 if (ioctl(s, SIOCGIFFLAGS, &ifreq) < 0)
532 if ((ifreq.ifr_flags &
533 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
534 != (IFF_UP|IFF_BROADCAST))
537 * Get its netmask and check that it's on the right subnet.
539 if (ioctl(s, SIOCGIFNETMASK, &ifreq) < 0)
541 mask = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
542 if ((ipaddr & mask) != (ina & mask))
547 ifr = (struct ifreq *) ((char *)&ifr->ifr_addr + ifr->ifr_addr.sa_len);
552 syslog(LOG_INFO, "found interface %s for proxy arp", ifr->ifr_name);
555 * Now scan through again looking for a link-level address
556 * for this interface.
559 for (ifr = ifc.ifc_req; ifr < ifend; ) {
560 if (strcmp(ifp->ifr_name, ifr->ifr_name) == 0
561 && ifr->ifr_addr.sa_family == AF_LINK) {
563 * Found the link-level address - copy it out
565 dla = (struct sockaddr_dl *)&ifr->ifr_addr;
566 hwaddr->sa_len = sizeof(struct sockaddr);
567 hwaddr->sa_family = AF_UNSPEC;
568 BCOPY(LLADDR(dla), hwaddr->sa_data, dla->sdl_alen);
571 ifr = (struct ifreq *) ((char *)&ifr->ifr_addr + ifr->ifr_addr.sa_len);
579 * ppp_available - check whether the system has any ppp interfaces
580 * (in fact we check whether we can do an ioctl on ppp0).
588 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
589 return 1; /* can't tell - maybe we're not root */
591 strncpy(ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
592 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;