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.2 1993/12/14 05:41:11 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; /* Initial TTY discipline */
49 * establish_ppp - Turn the serial port into a ppp interface.
54 int pppdisc = PPPDISC;
56 if (ioctl(fd, TIOCGETD, &initdisc) < 0) {
57 syslog(LOG_ERR, "ioctl(TIOCGETD): %m");
60 if (ioctl(fd, TIOCSETD, &pppdisc) < 0) {
61 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
66 * Find out which interface we were given.
68 if (ioctl(fd, PPPIOCGUNIT, &ifunit) < 0) {
69 syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m");
76 * disestablish_ppp - Restore the serial port to normal operation.
77 * This shouldn't call die() because it's called from die().
82 if (ioctl(fd, TIOCSETD, &initdisc) < 0)
83 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
88 * output - Output PPP packet.
97 MAINDEBUG((LOG_WARNING, "output: unit != 0!"));
99 if (write(fd, p, len) < 0) {
100 syslog(LOG_ERR, "write: %m");
107 * read_packet - get a PPP packet from the serial device.
115 if ((len = read(fd, buf, MTU + DLLHEADERLEN)) < 0) {
116 if (errno == EWOULDBLOCK) {
117 MAINDEBUG((LOG_DEBUG, "read(fd): EWOULDBLOCK"));
120 syslog(LOG_ERR, "read(fd): %m");
128 * ppp_send_config - configure the transmit characteristics of
132 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
140 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
142 if (ioctl(s, SIOCSIFMTU, (caddr_t) &ifr) < 0) {
143 syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m");
147 if (ioctl(fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0) {
148 syslog(LOG_ERR, "ioctl(PPPIOCSASYNCMAP): %m");
152 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
153 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
156 x = pcomp? x | SC_COMP_PROT: x &~ SC_COMP_PROT;
157 x = accomp? x | SC_COMP_AC: x &~ SC_COMP_AC;
158 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
159 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
165 * ppp_recv_config - configure the receive-side characteristics of
166 * the ppp interface. At present this does nothing.
169 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
175 if (ioctl(fd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0) {
176 syslog(LOG_ERR, "ioctl(PPPIOCSRASYNCMAP): %m");
180 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
181 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
184 x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC;
185 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
186 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
193 * sifvjcomp - config tcp header compression
196 sifvjcomp(u, vjcomp, cidcomp)
200 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
201 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
204 x = vjcomp ? x | SC_COMP_TCP: x &~ SC_COMP_TCP;
205 x = cidcomp? x & ~SC_NO_TCP_CCID: x | SC_NO_TCP_CCID;
206 if(ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
207 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
214 * sifup - Config the interface up.
220 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
221 if (ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
222 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
225 ifr.ifr_flags |= IFF_UP;
226 if (ioctl(s, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
227 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
234 * sifdown - Config the interface down.
240 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
241 if (ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
242 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
245 ifr.ifr_flags &= ~IFF_UP;
246 if (ioctl(s, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
247 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
254 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
257 #define SET_SA_FAMILY(addr, family) \
258 BZERO((char *) &(addr), sizeof(addr)); \
259 addr.sa_family = (family); \
260 addr.sa_len = sizeof(addr);
263 * sifaddr - Config the interface IP addresses and netmask.
268 struct ifaliasreq ifra;
270 strncpy(ifra.ifra_name, ifname, sizeof(ifra.ifra_name));
271 SET_SA_FAMILY(ifra.ifra_addr, AF_INET);
272 ((struct sockaddr_in *) &ifra.ifra_addr)->sin_addr.s_addr = o;
273 SET_SA_FAMILY(ifra.ifra_broadaddr, AF_INET);
274 ((struct sockaddr_in *) &ifra.ifra_broadaddr)->sin_addr.s_addr = h;
276 SET_SA_FAMILY(ifra.ifra_mask, AF_INET);
277 ((struct sockaddr_in *) &ifra.ifra_mask)->sin_addr.s_addr = m;
279 BZERO(&ifra.ifra_mask, sizeof(ifra.ifra_mask));
280 if (ioctl(s, SIOCAIFADDR, (caddr_t) &ifra) < 0) {
281 if (errno != EEXIST) {
282 syslog(LOG_ERR, "ioctl(SIOCAIFADDR): %m");
285 syslog(LOG_WARNING, "ioctl(SIOCAIFADDR): Address already exists");
291 * cifaddr - Clear the interface IP addresses, and delete routes
292 * through the interface if possible.
297 struct ifaliasreq ifra;
299 strncpy(ifra.ifra_name, ifname, sizeof(ifra.ifra_name));
300 SET_SA_FAMILY(ifra.ifra_addr, AF_INET);
301 ((struct sockaddr_in *) &ifra.ifra_addr)->sin_addr.s_addr = o;
302 SET_SA_FAMILY(ifra.ifra_broadaddr, AF_INET);
303 ((struct sockaddr_in *) &ifra.ifra_broadaddr)->sin_addr.s_addr = h;
304 BZERO(&ifra.ifra_mask, sizeof(ifra.ifra_mask));
305 if (ioctl(s, SIOCDIFADDR, (caddr_t) &ifra) < 0) {
306 syslog(LOG_WARNING, "ioctl(SIOCDIFADDR): %m");
313 * sifdefaultroute - assign a default route through the address given.
316 sifdefaultroute(u, g)
320 SET_SA_FAMILY(rt.rt_dst, AF_INET);
321 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
322 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
323 rt.rt_flags = RTF_GATEWAY;
324 if (ioctl(s, SIOCADDRT, &rt) < 0) {
325 syslog(LOG_ERR, "default route ioctl(SIOCADDRT): %m");
332 * cifdefaultroute - delete a default route through the address given.
335 cifdefaultroute(u, g)
339 SET_SA_FAMILY(rt.rt_dst, AF_INET);
340 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
341 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
342 rt.rt_flags = RTF_GATEWAY;
343 if (ioctl(s, SIOCDELRT, &rt) < 0)
344 syslog(LOG_WARNING, "default route ioctl(SIOCDELRT): %m");
348 * sifproxyarp - Make a proxy ARP entry for the peer.
351 sifproxyarp(unit, hisaddr)
355 struct arpreq arpreq;
357 BZERO(&arpreq, sizeof(arpreq));
360 * Get the hardware address of an interface on the same subnet
361 * as our local address.
363 if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) {
364 syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
368 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
369 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
370 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
371 if (ioctl(s, SIOCSARP, (caddr_t)&arpreq) < 0) {
372 syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
380 * cifproxyarp - Delete the proxy ARP entry for the peer.
383 cifproxyarp(unit, hisaddr)
387 struct arpreq arpreq;
389 BZERO(&arpreq, sizeof(arpreq));
390 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
391 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
392 if (ioctl(s, SIOCDARP, (caddr_t)&arpreq) < 0) {
393 syslog(LOG_WARNING, "ioctl(SIOCDARP): %m");
400 * get_ether_addr - get the hardware address of an interface on the
401 * the same subnet as ipaddr.
406 get_ether_addr(ipaddr, hwaddr)
408 struct sockaddr *hwaddr;
410 struct ifreq *ifr, *ifend, *ifp;
412 struct sockaddr_dl *dla;
415 struct ifreq ifs[MAX_IFS];
417 ifc.ifc_len = sizeof(ifs);
419 if (ioctl(s, SIOCGIFCONF, &ifc) < 0) {
420 syslog(LOG_ERR, "ioctl(SIOCGIFCONF): %m");
425 * Scan through looking for an interface with an Internet
426 * address on the same subnet as `ipaddr'.
428 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
429 for (ifr = ifc.ifc_req; ifr < ifend; ) {
430 if (ifr->ifr_addr.sa_family == AF_INET) {
431 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
432 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
434 * Check that the interface is up, and not point-to-point
437 if (ioctl(s, SIOCGIFFLAGS, &ifreq) < 0)
439 if ((ifreq.ifr_flags &
440 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
441 != (IFF_UP|IFF_BROADCAST))
444 * Get its netmask and check that it's on the right subnet.
446 if (ioctl(s, SIOCGIFNETMASK, &ifreq) < 0)
448 mask = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
449 if ((ipaddr & mask) != (ina & mask))
454 ifr = (struct ifreq *) ((char *)&ifr->ifr_addr + ifr->ifr_addr.sa_len);
459 syslog(LOG_INFO, "found interface %s for proxy arp", ifr->ifr_name);
462 * Now scan through again looking for a link-level address
463 * for this interface.
466 for (ifr = ifc.ifc_req; ifr < ifend; ) {
467 if (strcmp(ifp->ifr_name, ifr->ifr_name) == 0
468 && ifr->ifr_addr.sa_family == AF_LINK) {
470 * Found the link-level address - copy it out
472 dla = (struct sockaddr_dl *)&ifr->ifr_addr;
473 hwaddr->sa_len = sizeof(struct sockaddr);
474 hwaddr->sa_family = AF_UNSPEC;
475 BCOPY(LLADDR(dla), hwaddr->sa_data, dla->sdl_alen);
478 ifr = (struct ifreq *) ((char *)&ifr->ifr_addr + ifr->ifr_addr.sa_len);
485 * ppp_available - check whether the system has any ppp interfaces
486 * (in fact we check whether we can do an ioctl on ppp0).
495 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
496 return 1; /* can't tell - maybe we're not root */
498 strncpy(ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
499 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;