2 * sys-str.c - System-dependent procedures for setting up
3 * PPP interfaces on systems which use the STREAMS ppp interface.
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.
29 #include <sys/ioctl.h>
30 #include <sys/types.h>
31 #include <sys/socket.h>
33 #include <sys/stream.h>
34 #include <sys/stropts.h>
37 #include <net/route.h>
38 #include <net/if_arp.h>
39 #include <netinet/in.h>
43 #include <net/ppp_str.h>
46 #define ifr_mtu ifr_metric
49 #define MAXMODULES 10 /* max number of module names to save */
50 static struct modlist {
51 char modname[FMNAMESZ+1];
52 } str_modules[MAXMODULES];
53 static int str_module_count = 0;
55 extern int hungup; /* has the physical layer been disconnected? */
58 * ppp_available - check if this kernel supports PPP.
65 fd = open("/dev/tty", O_RDONLY, 0);
67 return 1; /* can't find out - assume we have ppp */
68 ret = ioctl(fd, I_FIND, "pppasync") >= 0;
75 * establish_ppp - Turn the serial port into a ppp interface.
80 /* go through and save the name of all the modules, then pop em */
82 if (ioctl(fd, I_LOOK, str_modules[str_module_count].modname) < 0 ||
83 ioctl(fd, I_POP, 0) < 0)
85 MAINDEBUG((LOG_DEBUG, "popped stream module : %s",
86 str_modules[str_module_count].modname));
90 MAINDEBUG((LOG_INFO, "about to push modules..."));
92 /* now push the async/fcs module */
93 if (ioctl(fd, I_PUSH, "pppasync") < 0) {
94 syslog(LOG_ERR, "ioctl(I_PUSH, ppp_async): %m");
97 /* finally, push the ppp_if module that actually handles the */
98 /* network interface */
99 if (ioctl(fd, I_PUSH, "pppif") < 0) {
100 syslog(LOG_ERR, "ioctl(I_PUSH, ppp_if): %m");
103 if (ioctl(fd, I_SETSIG, S_INPUT) < 0) {
104 syslog(LOG_ERR, "ioctl(I_SETSIG, S_INPUT): %m");
107 /* read mode, message non-discard mode */
108 if (ioctl(fd, I_SRDOPT, RMSGN) < 0) {
109 syslog(LOG_ERR, "ioctl(I_SRDOPT, RMSGN): %m");
112 /* Flush any waiting messages, or we'll never get SIGPOLL */
113 if (ioctl(fd, I_FLUSH, FLUSHRW) < 0) {
114 syslog(LOG_ERR, "ioctl(I_FLUSH, FLUSHRW): %m");
118 * Find out which interface we were given.
119 * (ppp_if handles this ioctl)
121 if (ioctl(fd, SIOCGETU, &ifunit) < 0) {
122 syslog(LOG_ERR, "ioctl(SIOCGETU): %m");
126 /* if debug, set debug flags in driver */
128 int flags = debug ? 0x3 : 0;
129 if (ioctl(fd, SIOCSIFDEBUG, &flags) < 0) {
130 syslog(LOG_ERR, "ioctl(SIOCSIFDEBUG): %m");
134 MAINDEBUG((LOG_INFO, "done pushing modules, ifunit %d", ifunit));
138 * disestablish_ppp - Restore the serial port to normal operation.
139 * It attempts to reconstruct the stream with the previously popped
140 * modules. This shouldn't call die() because it's called from die().
148 /* we can't push or pop modules after the stream has hung up */
149 str_module_count = 0;
153 while (ioctl(fd, I_POP, 0) == 0) /* pop any we pushed */
156 for (; str_module_count > 0; str_module_count--) {
157 if (ioctl(fd, I_PUSH, str_modules[str_module_count-1].modname)) {
158 syslog(LOG_ERR, "str_restore: couldn't push module %s: %m",
159 str_modules[str_module_count-1].modname);
161 MAINDEBUG((LOG_INFO, "str_restore: pushed module %s",
162 str_modules[str_module_count-1].modname));
169 * output - Output PPP packet.
180 MAINDEBUG((LOG_WARNING, "output: unit != 0!"));
183 str.buf = (caddr_t) p;
184 if(putmsg(fd, NULL, &str, 0) < 0) {
185 syslog(LOG_ERR, "putmsg: %m");
192 * read_packet - get a PPP packet from the serial device.
201 str.maxlen = MTU+DLLHEADERLEN;
202 str.buf = (caddr_t) buf;
204 len = getmsg(fd, NULL, &str, &i);
206 if (errno == EAGAIN || errno == EWOULDBLOCK) {
209 syslog(LOG_ERR, "getmsg(fd) %m");
213 MAINDEBUG((LOG_DEBUG, "getmsg returned 0x%x",len));
216 MAINDEBUG((LOG_DEBUG, "getmsg short return length %d", str.len));
225 * ppp_send_config - configure the transmit characteristics of
229 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
237 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
239 if (ioctl(s, SIOCSIFMTU, (caddr_t) &ifr) < 0) {
240 syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m");
244 if(ioctl(fd, SIOCSIFASYNCMAP, (caddr_t) &asyncmap) < 0) {
245 syslog(LOG_ERR, "ioctl(SIOCSIFASYNCMAP): %m");
250 if(ioctl(fd, SIOCSIFCOMPPROT, &c) < 0) {
251 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPPROT): %m");
256 if(ioctl(fd, SIOCSIFCOMPAC, &c) < 0) {
257 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPAC): %m");
263 * ppp_recv_config - configure the receive-side characteristics of
264 * the ppp interface. At present this just sets the MRU.
267 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
274 if (ioctl(fd, SIOCSIFMRU, &mru) < 0) {
275 syslog(LOG_ERR, "ioctl(SIOCSIFMRU): %m");
279 if(ioctl(fd, SIOCSIFRASYNCMAP, (caddr_t) &asyncmap) < 0) {
280 syslog(LOG_ERR, "ioctl(SIOCSIFRASYNCMAP): %m");
284 if(ioctl(fd, SIOCSIFRCOMPAC, &c) < 0) {
285 syslog(LOG_ERR, "ioctl(SIOCSIFRCOMPAC): %m");
292 * sifvjcomp - config tcp header compression
295 sifvjcomp(u, vjcomp, cidcomp)
299 if(ioctl(fd, SIOCSIFVJCOMP, (caddr_t) &x) < 0) {
300 syslog(LOG_ERR, "ioctl(SIOCSIFVJCOMP): %m");
307 * sifup - Config the interface up.
314 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
315 if (ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
316 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
319 ifr.ifr_flags |= IFF_UP;
320 if (ioctl(s, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
321 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
328 * sifdown - Config the interface down.
334 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
335 if (ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
336 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
339 ifr.ifr_flags &= ~IFF_UP;
340 if (ioctl(s, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
341 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
348 * SET_SA_FAMILY - initialize a struct sockaddr, setting the sa_family field.
350 #define SET_SA_FAMILY(addr, family) \
351 BZERO((char *) &(addr), sizeof(addr)); \
352 addr.sa_family = (family);
355 * sifaddr - Config the interface IP addresses and netmask.
364 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
365 SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
366 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
367 if (ioctl(s, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
368 syslog(LOG_ERR, "ioctl(SIOCSIFADDR): %m");
371 ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = h;
372 if (ioctl(s, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
373 syslog(LOG_ERR, "ioctl(SIOCSIFDSTADDR): %m");
377 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = m;
378 syslog(LOG_INFO, "Setting interface mask to %s\n", ip_ntoa(m));
379 if (ioctl(s, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
380 syslog(LOG_ERR, "ioctl(SIOCSIFNETMASK): %m");
388 * cifaddr - Clear the interface IP addresses, and delete routes
389 * through the interface if possible.
396 SET_SA_FAMILY(rt.rt_dst, AF_INET);
397 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = h;
398 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
399 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = o;
400 rt.rt_flags = RTF_HOST;
401 if (ioctl(s, SIOCDELRT, (caddr_t) &rt) < 0) {
402 syslog(LOG_ERR, "ioctl(SIOCDELRT): %m");
409 * sifdefaultroute - assign a default route through the address given.
412 sifdefaultroute(u, g)
416 SET_SA_FAMILY(rt.rt_dst, AF_INET);
417 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
418 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
419 rt.rt_flags = RTF_GATEWAY;
420 if (ioctl(s, SIOCADDRT, &rt) < 0) {
421 syslog(LOG_ERR, "default route ioctl(SIOCADDRT): %m");
428 * cifdefaultroute - delete a default route through the address given.
431 cifdefaultroute(u, g)
435 SET_SA_FAMILY(rt.rt_dst, AF_INET);
436 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
437 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
438 rt.rt_flags = RTF_GATEWAY;
439 if (ioctl(s, SIOCDELRT, &rt) < 0) {
440 syslog(LOG_ERR, "default route ioctl(SIOCDELRT): %m");
447 * sifproxyarp - Make a proxy ARP entry for the peer.
450 sifproxyarp(unit, hisaddr)
454 struct arpreq arpreq;
456 BZERO(&arpreq, sizeof(arpreq));
459 * Get the hardware address of an interface on the same subnet
460 * as our local address.
462 if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) {
463 syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
467 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
468 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
469 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
470 if (ioctl(s, SIOCSARP, (caddr_t)&arpreq) < 0) {
471 syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
479 * cifproxyarp - Delete the proxy ARP entry for the peer.
482 cifproxyarp(unit, hisaddr)
486 struct arpreq arpreq;
488 BZERO(&arpreq, sizeof(arpreq));
489 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
490 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
491 if (ioctl(s, SIOCDARP, (caddr_t)&arpreq) < 0) {
492 syslog(LOG_ERR, "ioctl(SIOCDARP): %m");
499 * get_ether_addr - get the hardware address of an interface on the
500 * the same subnet as ipaddr. Code borrowed from myetheraddr.c
501 * in the cslip-2.6 distribution, which is subject to the following
504 * Copyright (c) 1990, 1992 The Regents of the University of California.
505 * All rights reserved.
507 * Redistribution and use in source and binary forms, with or without
508 * modification, are permitted provided that: (1) source code distributions
509 * retain the above copyright notice and this paragraph in its entirety, (2)
510 * distributions including binary code include the above copyright notice and
511 * this paragraph in its entirety in the documentation or other materials
512 * provided with the distribution, and (3) all advertising materials mentioning
513 * features or use of this software display the following acknowledgement:
514 * ``This product includes software developed by the University of California,
515 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
516 * the University nor the names of its contributors may be used to endorse
517 * or promote products derived from this software without specific prior
518 * written permission.
519 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
520 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
521 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
527 #include <arpa/inet.h>
529 /* XXX SunOS 4.1 defines this and 3.5 doesn't... */
535 #include <netinet/in_var.h>
537 #include <netinet/if_ether.h>
539 /* Cast a struct sockaddr to a structaddr_in */
540 #define SATOSIN(sa) ((struct sockaddr_in *)(sa))
542 /* Determine if "bits" is set in "flag" */
543 #define ALLSET(flag, bits) (((flag) & (bits)) == (bits))
545 static struct nlist nl[] = {
554 get_ether_addr(ipaddr, hwaddr)
556 struct sockaddr *hwaddr;
559 register struct ifnet *ifp;
560 register struct arpcom *ac;
561 struct arpcom arpcom;
564 register struct ifaddr *ifa;
565 register struct in_ifaddr *in;
573 /* Open kernel memory for reading */
574 kd = kvm_open(0, 0, 0, O_RDONLY, NULL);
576 syslog(LOG_ERR, "kvm_open: %m");
581 if (kvm_nlist(kd, nl) != 0) {
582 syslog(LOG_ERR, "kvm_nlist failed");
593 if (kvm_read(kd, nl[N_IFNET].n_value, (char *)&addr, sizeof(addr))
595 syslog(LOG_ERR, "error reading ifnet addr");
598 for ( ; addr; addr = (u_long)ifp->if_next) {
599 if (kvm_read(kd, addr, (char *)ac, sizeof(*ac)) != sizeof(*ac)) {
600 syslog(LOG_ERR, "error reading ifnet");
604 /* Only look at configured, broadcast interfaces */
605 if (!ALLSET(ifp->if_flags, IFF_UP | IFF_BROADCAST))
608 /* This probably can't happen... */
609 if (ifp->if_addrlist == 0)
613 /* Get interface ip address */
615 if (kvm_read(kd, (u_long)ifp->if_addrlist, (char *)&ifaddr,
616 sizeof(ifaddr)) != sizeof(ifaddr)) {
617 syslog(LOG_ERR, "error reading ifaddr");
620 inp = &SATOSIN(&ifa->ifa_addr)->sin_addr;
622 inp = &SATOSIN(&ifp->if_addr)->sin_addr;
625 /* Check if this interface on the right subnet */
627 mask = in->ia_subnetmask;
629 mask = ifp->if_subnetmask;
631 if ((ipaddr & mask) != (inp->s_addr & mask))
634 /* Copy out the local ethernet address */
635 hwaddr->sa_family = AF_UNSPEC;
636 BCOPY((caddr_t) &arpcom.ac_enaddr, hwaddr->sa_data,
637 sizeof(arpcom.ac_enaddr));
638 return 1; /* success! */
641 /* couldn't find one */