/* * printpolicy(sock) * * Queries the kernel for the current address selection policy using * the open socket sock, and prints the result. Returns EXIT_FAILURE * if the table cannot be obtained, or EXIT_SUCCESS if the table is * obtained and printed successfully. */ static int printpolicy(int sock) { ip6_asp_t policy[KERN_POLICY_SIZE]; ip6_asp_t *policy_ptr = policy; int count, policy_index; char prefixstr[INET6_ADDRSTRLEN + sizeof ("/128")]; if ((count = strioctl(sock, SIOCGIP6ADDRPOLICY, policy_ptr, KERN_POLICY_SIZE * sizeof (ip6_asp_t))) < 0) { perror("SIOCGIP6ADDRPOLICY"); return (EXIT_FAILURE); } if (count > KERN_POLICY_SIZE) { policy_ptr = malloc(count * sizeof (ip6_asp_t)); if (policy_ptr == NULL) { perror("malloc"); return (EXIT_FAILURE); } if ((count = strioctl(sock, SIOCGIP6ADDRPOLICY, policy_ptr, count * sizeof (ip6_asp_t))) < 0) { perror("SIOCGIP6ADDRPOLICY"); return (EXIT_FAILURE); } } if (count == 0) { /* * There should always at least be a default entry in the * policy table, so the minimum acceptable value of * policy_count is 1. */ (void) fprintf(stderr, gettext("%s: ERROR: " "IPv6 address selection policy is empty.\n"), myname); return (EXIT_FAILURE); } /* * The format printed here must also be parsable by parseconf(), * since we expect users to be able to redirect this output to * a usable configuration file if need be. */ (void) printf("# Prefix " " Precedence Label\n"); for (policy_index = 0; policy_index < count; policy_index++) { (void) snprintf(prefixstr, sizeof (prefixstr), "%s/%d", inet_ntop(AF_INET6, &policy_ptr[policy_index].ip6_asp_prefix, prefixstr, sizeof (prefixstr)), ip_mask_to_plen_v6(&policy_ptr[policy_index].ip6_asp_mask)); (void) printf("%-45s %10d %s\n", prefixstr, policy_ptr[policy_index].ip6_asp_precedence, policy_ptr[policy_index].ip6_asp_label); } if (policy_ptr != policy) free(policy_ptr); return (EXIT_SUCCESS); }
/* * Push and configure the buffer module. Returns -1 for error, otherwise 0. */ int pcap_conf_bufmod(pcap_t *p, int snaplen) { struct timeval to; bpf_u_int32 ss, chunksize; /* Non-standard call to get the data nicely buffered. */ if (ioctl(p->fd, I_PUSH, "bufmod") != 0) { pcap_stream_err("I_PUSH bufmod", errno, p->errbuf); return (-1); } ss = snaplen; if (ss > 0 && strioctl(p->fd, SBIOCSSNAP, sizeof(ss), (char *)&ss) != 0) { pcap_stream_err("SBIOCSSNAP", errno, p->errbuf); return (-1); } if (p->opt.immediate) { /* Set the timeout to zero, for immediate delivery. */ to.tv_sec = 0; to.tv_usec = 0; if (strioctl(p->fd, SBIOCSTIME, sizeof(to), (char *)&to) != 0) { pcap_stream_err("SBIOCSTIME", errno, p->errbuf); return (-1); } } else { /* Set up the bufmod timeout. */ if (p->opt.timeout != 0) { to.tv_sec = p->opt.timeout / 1000; to.tv_usec = (p->opt.timeout * 1000) % 1000000; if (strioctl(p->fd, SBIOCSTIME, sizeof(to), (char *)&to) != 0) { pcap_stream_err("SBIOCSTIME", errno, p->errbuf); return (-1); } } /* Set the chunk length. */ chunksize = CHUNKSIZE; if (strioctl(p->fd, SBIOCSCHUNK, sizeof(chunksize), (char *)&chunksize) != 0) { pcap_stream_err("SBIOCSCHUNKP", errno, p->errbuf); return (-1); } } return (0); }
/* ARGSUSED */ static int cnioctl(dev_t dev, int cmd, intptr_t arg, int flag, struct cred *cred, int *rvalp) { if (rconsvp == NULL) return (0); /* * In wc, VT_SET_CONSUSER which comes from minor node 0 * has two sources -- either /dev/console or /dev/vt/0 . * We need a way to differentiate them, so here we * change VT_SET_CONSUSER to a private VT_RESET_CONSUSER * ioctl. */ if (cmd == VT_SET_CONSUSER) cmd = VT_RESET_CONSUSER; if ((cmd & _CNIOC_MASK) == _CNIOC) return (cnprivateioc(dev, cmd, arg, flag, cred, rvalp)); if (rconsvp->v_stream != NULL) return (strioctl(rconsvp, cmd, arg, flag, U_TO_K, cred, rvalp)); return (cdev_ioctl(rconsdev, cmd, arg, flag, cred, rvalp)); }
/* * Open the sppptun driver. */ static void open_tunnel_dev(void) { struct ppptun_peer ptp; tunfd = open(tunnam, O_RDWR); if (tunfd == -1) { early_error(tunnam); } /* * Tell the device driver that I'm a daemon handling inbound * connections, not a PPP session. */ (void) memset(&ptp, '\0', sizeof (ptp)); ptp.ptp_style = PTS_PPPOE; ptp.ptp_flags = PTPF_DAEMON; (void) memcpy(ptp.ptp_address.pta_pppoe.ptma_mac, ether_bcast, sizeof (ptp.ptp_address.pta_pppoe.ptma_mac)); if (strioctl(tunfd, PPPTUN_SPEER, &ptp, sizeof (ptp), sizeof (ptp)) < 0) { myperror("PPPTUN_SPEER"); exit(1); } }
/********************************************************************** *%FUNCTION: openInterface *%ARGUMENTS: * ifname -- name of interface * type -- Ethernet frame type * hwaddr -- if non-NULL, set to the hardware address *%RETURNS: * A raw socket for talking to the Ethernet card. Exits on error. *%DESCRIPTION: * Opens a raw Ethernet socket ***********************************************************************/ int openInterface(char const *ifname, UINT16_t type, unsigned char *hwaddr) { int fd; long buf[MAXDLBUF]; union DL_primitives *dlp; char base_dev[PATH_MAX]; int ppa; if(strlen(ifname) > PATH_MAX) { fatallog("openInterface: interface name too long"); } ppa = atoi(&ifname[strlen(ifname)-1]); strncpy(base_dev, ifname, PATH_MAX); base_dev[strlen(base_dev)-1] = '\0'; /* rearranged order of DLPI code - delphys 20010803 */ dlp = (union DL_primitives*) buf; if (( fd = open(base_dev, O_RDWR)) < 0) { /* Give a more helpful message for the common error case */ if (errno == EPERM) { fatallog("Cannot create raw socket -- pppoe must be run as root."); } fatallog("open(%s): %m", base_dev); } /* rearranged order of DLPI code - delphys 20010803 */ dlattachreq(fd, ppa); dlokack(fd, (char *)buf); dlbindreq(fd, type, 0, DL_CLDLS, 0, 0); dlbindack(fd, (char *)buf); dlinforeq(fd); dlinfoack(fd, (char *)buf); dl_abssaplen = ABS(dlp->info_ack.dl_sap_length); dl_saplen = dlp->info_ack.dl_sap_length; if (ETHERADDRL != (dlp->info_ack.dl_addr_length - dl_abssaplen)) fatallog("invalid destination physical address length"); dl_addrlen = dl_abssaplen + ETHERADDRL; /* ethernet address retrieved as part of DL_INFO_ACK - delphys 20010803 */ memcpy(hwaddr, (u_char*)((char*)(dlp) + (int)(dlp->info_ack.dl_addr_offset)), ETHERADDRL); if ( strioctl(fd, DLIOCRAW, -1, 0, NULL) < 0 ) { fatallog("DLIOCRAW: %m"); } if (ioctl(fd, I_FLUSH, FLUSHR) < 0) fatallog("I_FLUSH: %m"); return fd; }
bool PPPStats::ifIsUp() { bool is_up; struct ifreq ifr; #if defined(__svr4__ ) usleep(1000000); // Needed for Solaris ?! #endif #ifdef STREAMS if ((t = open("/dev/ppp", O_RDONLY)) < 0) { perror("pppstats: Couldn't open /dev/ppp: "); return false; } if (!strioctl(t, PPPIO_ATTACH, (char*)&unit, sizeof(int), 0)) { fprintf(stderr, "pppstats: ppp%d is not available\n", unit); ::close(t); return false; } // TODO: close t somewhere again #endif if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { perror("Couldn't create IP socket"); return false; } strlcpy(ifr.ifr_name, unitName, sizeof(ifr.ifr_name)); if(ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) { if (errno) fprintf(stderr, "Couldn't find interface %s: %s\n", unitName, strerror(errno)); ::close(s); s = 0; return 0; } if ((ifr.ifr_flags & (IFF_UP|IFF_RUNNING)) != 0) { is_up = true; kdDebug() << "Interface is up" << endl; } else { is_up = false; ::close(s); s = 0; kdDebug() << "Interface is down" << endl; } return is_up; }
/* * sys_init - System-dependent initialization. */ void sys_init() { int x; openlog("pppd", LOG_PID | LOG_NDELAY, LOG_PPP); setlogmask(LOG_UPTO(LOG_INFO)); if (debug) setlogmask(LOG_UPTO(LOG_DEBUG)); /* Get an internet socket for doing socket ioctl's on. */ if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) fatal("Couldn't create IP socket: %m"); if (default_device) tty_sid = getsid((pid_t)0); /* * Open the ppp device. */ pppfd = open("/dev/streams/ppp", O_RDWR | O_NONBLOCK, 0); if (pppfd < 0) fatal("Can't open /dev/streams/ppp: %m"); if (kdebugflag) { x = PPPDBG_LOG + PPPDBG_DRIVER; strioctl(pppfd, PPPIO_DEBUG, &x, sizeof(int), 0); } /* Assign a new PPA and get its unit number. */ if (strioctl(pppfd, PPPIO_NEWPPA, &ifunit, 0, sizeof(int)) < 0) fatal("Can't create new PPP interface: %m"); /* * Open the ppp device again and push the if_ppp module on it. */ iffd = open("/dev/streams/ppp", O_RDWR, 0); if (iffd < 0) fatal("Can't open /dev/streams/ppp (2): %m"); if (kdebugflag) { x = PPPDBG_LOG + PPPDBG_DRIVER; strioctl(iffd, PPPIO_DEBUG, &x, sizeof(int), 0); } if (strioctl(iffd, PPPIO_ATTACH, &ifunit, sizeof(int), 0) < 0) fatal("Couldn't attach ppp interface to device: %m"); if (ioctl(iffd, I_PUSH, "if_ppp") < 0) fatal("Can't push ppp interface module: %m"); if (kdebugflag) { x = PPPDBG_LOG + PPPDBG_IF; strioctl(iffd, PPPIO_DEBUG, &x, sizeof(int), 0); } if (strioctl(iffd, PPPIO_NEWPPA, &ifunit, sizeof(int), 0) < 0) fatal("Couldn't create ppp interface unit: %m"); x = PPP_IP; if (strioctl(iffd, PPPIO_BIND, &x, sizeof(int), 0) < 0) fatal("Couldn't bind ppp interface to IP SAP: %m"); n_pollfds = 0; }
/* * setpolicy(sock, new_policy, count) * * Sends an SIOCSIP6ADDRPOLICY ioctl to the kernel to set the address * selection policy table pointed to by new_policy. count should be * the number of entries in the table; sock should be an open INET6 * socket. Returns EXIT_FAILURE or EXIT_SUCCESS. */ static int setpolicy(int sock, ip6_asp_t *new_policy, int count) { if (strioctl(sock, SIOCSIP6ADDRPOLICY, new_policy, count * sizeof (ip6_asp_t)) < 0) { perror("SIOCSIP6ADDRPOLICY"); return (EXIT_FAILURE); } return (EXIT_SUCCESS); }
/* * sys_init - System-dependent initialization. */ void sys_init() { int x; /* Get an internet socket for doing socket ioctl's on. */ if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) fatal("Couldn't create IP socket: %m"); /* * We may want to send a SIGHUP to the session leader associated * with our controlling terminal later. Because SunOS doesn't * have getsid(), we make do with sending the signal to our * parent process. */ parent_pid = getppid(); /* * Open the ppp device. */ pppfd = open("/dev/ppp", O_RDWR | O_NONBLOCK, 0); if (pppfd < 0) fatal("Can't open /dev/ppp: %m"); if (kdebugflag) { x = PPPDBG_LOG + PPPDBG_DRIVER; strioctl(pppfd, PPPIO_DEBUG, &x, sizeof(int), 0); } /* Assign a new PPA and get its unit number. */ if (strioctl(pppfd, PPPIO_NEWPPA, &ifunit, 0, sizeof(int)) < 0) fatal("Can't create new PPP interface: %m"); /* * Open the ppp device again and push the if_ppp module on it. */ iffd = open("/dev/ppp", O_RDWR, 0); if (iffd < 0) fatal("Can't open /dev/ppp (2): %m"); if (kdebugflag) { x = PPPDBG_LOG + PPPDBG_DRIVER; strioctl(iffd, PPPIO_DEBUG, &x, sizeof(int), 0); } if (strioctl(iffd, PPPIO_ATTACH, &ifunit, sizeof(int), 0) < 0) fatal("Couldn't attach ppp interface to device: %m"); if (ioctl(iffd, I_PUSH, "if_ppp") < 0) fatal("Can't push ppp interface module: %m"); if (kdebugflag) { x = PPPDBG_LOG + PPPDBG_IF; strioctl(iffd, PPPIO_DEBUG, &x, sizeof(int), 0); } if (strioctl(iffd, PPPIO_NEWPPA, &ifunit, sizeof(int), 0) < 0) fatal("Couldn't create ppp interface unit: %m"); x = PPP_IP; if (strioctl(iffd, PPPIO_BIND, &x, sizeof(int), 0) < 0) fatal("Couldn't bind ppp interface to IP SAP: %m"); n_pollfds = 0; }
/* * Push and configure the buffer module. Returns -1 for error, otherwise 0. */ int pcap_conf_bufmod(pcap_t *p, int snaplen, int timeout) { int retv = 0; bpf_u_int32 ss, chunksize; /* Non-standard call to get the data nicely buffered. */ if (ioctl(p->fd, I_PUSH, "bufmod") != 0) { pcap_stream_err("I_PUSH bufmod", errno, p->errbuf); retv = -1; } ss = snaplen; if (ss > 0 && strioctl(p->fd, SBIOCSSNAP, sizeof(ss), (char *)&ss) != 0) { pcap_stream_err("SBIOCSSNAP", errno, p->errbuf); retv = -1; } /* Set up the bufmod timeout. */ if (timeout != 0) { struct timeval to; to.tv_sec = timeout / 1000; to.tv_usec = (timeout * 1000) % 1000000; if (strioctl(p->fd, SBIOCSTIME, sizeof(to), (char *)&to) != 0) { pcap_stream_err("SBIOCSTIME", errno, p->errbuf); retv = -1; } } /* Set the chunk length. */ chunksize = CHUNKSIZE; if (strioctl(p->fd, SBIOCSCHUNK, sizeof(chunksize), (char *)&chunksize) != 0) { pcap_stream_err("SBIOCSCHUNKP", errno, p->errbuf); retv = -1; } return (retv); }
static void get_ppp_stats(struct ppp_stats *curp) { if (strioctl(s, PPPIO_GETSTAT, curp, 0, sizeof(*curp)) < 0) { fprintf(stderr, "%s: ", progname); if (errno == EINVAL) fprintf(stderr, "kernel support missing\n"); else perror("couldn't get PPP statistics"); exit(1); } }
/* kludge alert! */ #undef __GLIBC__ #endif #include <sys/socket.h> /* *BSD, Linux, NeXT, Ultrix etc. */ #ifndef HAVE_NET_IF_PPP_H #ifdef HAVE_LINUX_IF_PPP_H #include <linux/if.h> #include <linux/if_ppp.h> #endif #else #include <net/if.h> #include <net/if_ppp.h> #endif #else /* STREAMS */ #include <sys/socket.h> #include <sys/stropts.h> /* SVR4, Solaris 2, SunOS 4, OSF/1, etc. */ #include <net/ppp_defs.h> #include <net/pppio.h> #include <net/if.h> #include <sys/sockio.h> #endif /* STREAMS */ #include <qtimer.h> #include <kdebug.h> PPPStats::PPPStats() { clear(); timer = new QTimer; ppp_connection_time = new QTime; setUnit(0); connect(timer, SIGNAL(timeout()), SLOT(timerClick())); } PPPStats::~PPPStats() { stop(); delete timer; } void PPPStats::clear() { ibytes = 0; ipackets = 0; ibytes_last = 0; obytes_last = 0; compressedin = 0; uncompressedin = 0; errorin = 0; obytes = 0; opackets = 0; compressed = 0; packetsunc = 0; packetsoutunc = 0; ioStatus = BytesNone; } void PPPStats::timerClick() { enum IOStatus newStatus; doStats(); if((ibytes != ibytes_last) && (obytes != obytes_last)) newStatus = BytesBoth; else if(ibytes != ibytes_last) newStatus = BytesIn; else if(obytes != obytes_last) newStatus = BytesOut; else newStatus = BytesNone; if(newStatus != ioStatus) emit statsChanged(ioStatus = newStatus); //baud_speed = (double)((ibytes - ibytes_last) /1024.0 ) * 5; ibytes_last = ibytes; obytes_last = obytes; } void PPPStats::setUnit(int u) { unit = u; sprintf(unitName, "ppp%d", unit); } void PPPStats::start() { timer->start(PPP_STATS_INTERVAL); } void PPPStats::stop() { emit statsChanged(BytesNone); timer->stop(); } bool PPPStats::ifIsUp() { bool is_up; struct ifreq ifr; #if defined(__svr4__ ) usleep(1000000); // Needed for Solaris ?! #endif #ifdef STREAMS if ((t = open("/dev/ppp", O_RDONLY)) < 0) { perror("pppstats: Couldn't open /dev/ppp: "); return false; } if (!strioctl(t, PPPIO_ATTACH, (char*)&unit, sizeof(int), 0)) { fprintf(stderr, "pppstats: ppp%d is not available\n", unit); ::close(t); return false; } // TODO: close t somewhere again #endif if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { perror("Couldn't create IP socket"); return false; } strlcpy(ifr.ifr_name, unitName, sizeof(ifr.ifr_name)); if(ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) { if (errno) fprintf(stderr, "Couldn't find interface %s: %s\n", unitName, strerror(errno)); ::close(s); s = 0; return 0; } if ((ifr.ifr_flags & (IFF_UP|IFF_RUNNING)) != 0) { is_up = true; kdDebug() << "Interface is up" << endl; } else { is_up = false; ::close(s); s = 0; kdDebug() << "Interface is down" << endl; } return is_up; } bool PPPStats::initStats() { struct sockaddr_in *sinp; struct ifreq ifr; clear(); strlcpy(ifr.ifr_name, unitName, sizeof(ifr.ifr_name)); if (ioctl(s, SIOCGIFADDR, &ifr) < 0) {} sinp = (struct sockaddr_in*)&ifr.ifr_addr; if(sinp->sin_addr.s_addr) local_ip_address = inet_ntoa(sinp->sin_addr); else local_ip_address = ""; kdDebug() << "Local IP: " << local_ip_address << endl; if (ioctl(s, SIOCGIFDSTADDR, &ifr) < 0) ; sinp = (struct sockaddr_in*)&ifr.ifr_dstaddr; if(sinp->sin_addr.s_addr) remote_ip_address = inet_ntoa(sinp->sin_addr); else remote_ip_address = ""; kdDebug() << "Remote IP: " << remote_ip_address << endl; return true; } bool PPPStats::doStats() { struct ppp_stats cur; if(! get_ppp_stats(&cur)) { return false; } // "in" "pack" "comp" "uncomp" "err" // IN PACK VJCOMP VJUNC VJERR ibytes = cur.p.ppp_ibytes; // bytes received ipackets = cur.p.ppp_ipackets; // packets recieved compressedin = cur.vj.vjs_compressedin; // inbound compressed packets uncompressedin = cur.vj.vjs_uncompressedin; // inbound uncompressed packets errorin = cur.vj.vjs_errorin; //receive errors // "out" "pack" "comp" "uncomp" "ip" // OUT PACK JCOMP VJUNC NON-VJ obytes = cur.p.ppp_obytes; // raw bytes sent opackets = cur.p.ppp_opackets; // packets sent compressed = cur.vj.vjs_compressed; //outbound compressed packets // outbound packets - outbound compressed packets packetsunc = cur.vj.vjs_packets - cur.vj.vjs_compressed; // packets sent - oubount compressed packetsoutunc = cur.p.ppp_opackets - cur.vj.vjs_packets; return true; } #ifndef STREAMS bool PPPStats::get_ppp_stats(struct ppp_stats *curp) { struct ifpppstatsreq req; if(s==0) return false; #ifdef __linux__ req.stats_ptr = (caddr_t) &req.stats; sprintf(req.ifr__name, "ppp%d", unit); #else sprintf(req.ifr_name, "ppp%d", unit); #endif if (ioctl(s, SIOCGPPPSTATS, &req) < 0) { if (errno == ENOTTY) fprintf(stderr, "pppstats: kernel support missing\n"); else perror("ioctl(SIOCGPPPSTATS)"); return false; } *curp = req.stats; return true; } #else /* STREAMS */ bool PPPStats::get_ppp_stats( struct ppp_stats *curp) { if (!strioctl(t, PPPIO_GETSTAT, (char*)curp, 0, sizeof(*curp))) { if (errno == EINVAL) fprintf(stderr, "pppstats: kernel support missing\n"); else perror("pppstats: Couldn't get statistics"); return false; } return true; }
void rpc_poptimod(vnode_t *vp) { int error, isfound, ret; error = strioctl(vp, I_FIND, (intptr_t)"timod", 0, K_TO_K, kcred, &isfound); if (error) { RPCLOG(1, "rpc_poptimod: I_FIND strioctl error %d\n", error); return; } if (isfound) { /* * Pop timod module */ error = strioctl(vp, I_POP, 0, 0, K_TO_K, kcred, &ret); if (error) { RPCLOG(1, "rpc_poptimod: I_POP strioctl error %d\n", error); return; } } }
int pcap_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf) { #ifdef HAVE_SOLARIS int fd; union { u_int nunits; char pad[516]; /* XXX - must be at least 513; is 516 in "atmgetunits" */ } buf; char baname[2+1+1]; u_int i; #endif /* * Get the list of regular interfaces first. */ if (pcap_findalldevs_interfaces(devlistp, errbuf, is_dlpi_interface) == -1) return (-1); /* failure */ #ifdef HAVE_SOLARIS /* * We may have to do special magic to get ATM devices. */ if ((fd = open("/dev/ba", O_RDWR)) < 0) { /* * We couldn't open the "ba" device. * For now, just give up; perhaps we should * return an error if the problem is neither * a "that device doesn't exist" error (ENOENT, * ENXIO, etc.) or a "you're not allowed to do * that" error (EPERM, EACCES). */ return (0); } if (strioctl(fd, A_GET_UNITS, sizeof(buf), (char *)&buf) < 0) { pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "A_GET_UNITS: %s", pcap_strerror(errno)); return (-1); } for (i = 0; i < buf.nunits; i++) { pcap_snprintf(baname, sizeof baname, "ba%u", i); if (add_dev(devlistp, baname, 0, NULL, errbuf) == NULL) return (-1); } #endif return (0); }
static void get_ppp_cstats(ppp_comp_stats *csp) { if (strioctl(s, PPPIO_GETCSTAT, csp, 0, sizeof(*csp)) < 0) { fprintf(stderr, "%s: ", progname); if (errno == ENOTTY) { fprintf(stderr, "no kernel compression support\n"); if (zflag) exit(1); rflag = 0; } else { perror("couldn't get PPP compression statistics"); exit(1); } } }
static int eth_match_ppa(eth_t *e, const char *device) { char *p, dev[16], buf[256]; int len, ppa; strlcpy(buf, "dl_ifnames", sizeof(buf)); if ((len = strioctl(e->fd, ND_GET, sizeof(buf), buf)) < 0) return (-1); for (p = buf; p < buf + len; p += strlen(p) + 1) { ppa = -1; if (sscanf(p, "%s (PPA %d)\n", dev, &ppa) != 2) break; if (strcmp(dev, device) == 0) break; } return (ppa); }
static int link_match_ppa(link_t *handle, const char *device) { char *p; char dev[16]; char buf[256]; int len; int ppa; strlcpy(buf, "dl_ifnames", sizeof(buf)); if ((len = strioctl(handle->fd, ND_GET, sizeof(buf), buf)) < 0) return -1; for (p = buf; p < buf + len; p += strlen(p) + 1) { ppa = -1; if (sscanf(p, "%s (PPA %d)\n", dev, &ppa) != 2) break; if (strcmp(dev, device) == 0) break; } return ppa; }
pcap_t * pcap_open_live(char *device, int snaplen, int promisc, int to_ms, char *ebuf) { register char *cp; char *eos; register pcap_t *p; register int ppa; register dl_info_ack_t *infop; #ifdef HAVE_SYS_BUFMOD_H bpf_u_int32 ss, flag; #ifdef HAVE_SOLARIS register char *release; bpf_u_int32 osmajor, osminor, osmicro; #endif #endif bpf_u_int32 buf[MAXDLBUF]; char dname[100]; #ifndef HAVE_DEV_DLPI char dname2[100]; #endif p = (pcap_t *)malloc(sizeof(*p)); if (p == NULL) { strcpy(ebuf, pcap_strerror(errno)); return (NULL); } memset(p, 0, sizeof(*p)); /* ** Determine device and ppa */ cp = strpbrk(device, "0123456789"); if (cp == NULL) { sprintf(ebuf, "%s missing unit number", device); goto bad; } ppa = strtol(cp, &eos, 10); if (*eos != '\0') { sprintf(ebuf, "%s bad unit number", device); goto bad; } if (*device == '/') strcpy(dname, device); else sprintf(dname, "%s/%s", PCAP_DEV_PREFIX, device); #ifdef HAVE_DEV_DLPI /* Map network device to /dev/dlpi unit */ cp = "/dev/dlpi"; if ((p->fd = open(cp, O_RDWR)) < 0) { sprintf(ebuf, "%s: %s", cp, pcap_strerror(errno)); goto bad; } /* Map network interface to /dev/dlpi unit */ ppa = get_dlpi_ppa(p->fd, dname, ppa, ebuf); if (ppa < 0) goto bad; #else /* Try device without unit number */ strcpy(dname2, dname); cp = strchr(dname, *cp); *cp = '\0'; if ((p->fd = open(dname, O_RDWR)) < 0) { if (errno != ENOENT) { sprintf(ebuf, "%s: %s", dname, pcap_strerror(errno)); goto bad; } /* Try again with unit number */ if ((p->fd = open(dname2, O_RDWR)) < 0) { sprintf(ebuf, "%s: %s", dname2, pcap_strerror(errno)); goto bad; } /* XXX Assume unit zero */ ppa = 0; } #endif p->snapshot = snaplen; /* ** Attach if "style 2" provider */ if (dlinforeq(p->fd, ebuf) < 0 || dlinfoack(p->fd, (char *)buf, ebuf) < 0) goto bad; infop = &((union DL_primitives *)buf)->info_ack; if (infop->dl_provider_style == DL_STYLE2 && (dlattachreq(p->fd, ppa, ebuf) < 0 || dlokack(p->fd, "attach", (char *)buf, ebuf) < 0)) goto bad; /* ** Bind (defer if using HP-UX 9 or HP-UX 10.20, totally skip if ** using SINIX) */ #if !defined(HAVE_HPUX9) && !defined(HAVE_HPUX10_20) && !defined(sinix) if (dlbindreq(p->fd, 0, ebuf) < 0 || dlbindack(p->fd, (char *)buf, ebuf) < 0) goto bad; #endif if (promisc) { /* ** Enable promiscuous */ if (dlpromisconreq(p->fd, DL_PROMISC_PHYS, ebuf) < 0 || dlokack(p->fd, "promisc_phys", (char *)buf, ebuf) < 0) goto bad; /* ** Try to enable multicast (you would have thought ** promiscuous would be sufficient). (Skip if using ** HP-UX or SINIX) */ #if !defined(__hpux) && !defined(sinix) if (dlpromisconreq(p->fd, DL_PROMISC_MULTI, ebuf) < 0 || dlokack(p->fd, "promisc_multi", (char *)buf, ebuf) < 0) fprintf(stderr, "WARNING: DL_PROMISC_MULTI failed (%s)\n", ebuf); #endif } /* ** Try to enable sap (when not in promiscuous mode when using ** using HP-UX and never under SINIX) */ #ifndef sinix if ( #ifdef __hpux !promisc && #endif (dlpromisconreq(p->fd, DL_PROMISC_SAP, ebuf) < 0 || dlokack(p->fd, "promisc_sap", (char *)buf, ebuf) < 0)) { /* Not fatal if promisc since the DL_PROMISC_PHYS worked */ if (promisc) fprintf(stderr, "WARNING: DL_PROMISC_SAP failed (%s)\n", ebuf); else goto bad; } #endif /* ** HP-UX 9 and HP-UX 10.20 must bind after setting promiscuous ** options) */ #if defined(HAVE_HPUX9) || defined(HAVE_HPUX10_20) if (dlbindreq(p->fd, 0, ebuf) < 0 || dlbindack(p->fd, (char *)buf, ebuf) < 0) goto bad; #endif /* ** Determine link type */ if (dlinforeq(p->fd, ebuf) < 0 || dlinfoack(p->fd, (char *)buf, ebuf) < 0) goto bad; infop = &((union DL_primitives *)buf)->info_ack; switch (infop->dl_mac_type) { case DL_CSMACD: case DL_ETHER: p->linktype = DLT_EN10MB; p->offset = 2; break; case DL_FDDI: p->linktype = DLT_FDDI; p->offset = 3; break; default: sprintf(ebuf, "unknown mac type 0x%lu", infop->dl_mac_type); goto bad; } #ifdef DLIOCRAW /* ** This is a non standard SunOS hack to get the ethernet header. */ if (strioctl(p->fd, DLIOCRAW, 0, NULL) < 0) { sprintf(ebuf, "DLIOCRAW: %s", pcap_strerror(errno)); goto bad; } #endif #ifdef HAVE_SYS_BUFMOD_H /* ** Another non standard call to get the data nicely buffered */ if (ioctl(p->fd, I_PUSH, "bufmod") != 0) { sprintf(ebuf, "I_PUSH bufmod: %s", pcap_strerror(errno)); goto bad; } /* ** Now that the bufmod is pushed lets configure it. ** ** There is a bug in bufmod(7). When dealing with messages of ** less than snaplen size it strips data from the beginning not ** the end. ** ** This bug is supposed to be fixed in 5.3.2. Also, there is a ** patch available. Ask for bugid 1149065. */ ss = snaplen; #ifdef HAVE_SOLARIS release = get_release(&osmajor, &osminor, &osmicro); if (osmajor == 5 && (osminor <= 2 || (osminor == 3 && osmicro < 2)) && getenv("BUFMOD_FIXED") == NULL) { fprintf(stderr, "WARNING: bufmod is broken in SunOS %s; ignoring snaplen.\n", release); ss = 0; } #endif if (ss > 0 && strioctl(p->fd, SBIOCSSNAP, sizeof(ss), (char *)&ss) != 0) { sprintf(ebuf, "SBIOCSSNAP: %s", pcap_strerror(errno)); goto bad; } /* ** Set up the bufmod flags */ if (strioctl(p->fd, SBIOCGFLAGS, sizeof(flag), (char *)&flag) < 0) { sprintf(ebuf, "SBIOCGFLAGS: %s", pcap_strerror(errno)); goto bad; } flag |= SB_NO_DROPS; if (strioctl(p->fd, SBIOCSFLAGS, sizeof(flag), (char *)&flag) != 0) { sprintf(ebuf, "SBIOCSFLAGS: %s", pcap_strerror(errno)); goto bad; } /* ** Set up the bufmod timeout */ if (to_ms != 0) { struct timeval to; to.tv_sec = to_ms / 1000; to.tv_usec = (to_ms * 1000) % 1000000; if (strioctl(p->fd, SBIOCSTIME, sizeof(to), (char *)&to) != 0) { sprintf(ebuf, "SBIOCSTIME: %s", pcap_strerror(errno)); goto bad; } } #endif /* ** As the last operation flush the read side. */ if (ioctl(p->fd, I_FLUSH, FLUSHR) != 0) { sprintf(ebuf, "FLUSHR: %s", pcap_strerror(errno)); goto bad; } /* Allocate data buffer */ p->bufsize = MAXDLBUF * sizeof(bpf_u_int32); p->buffer = (u_char *)malloc(p->bufsize + p->offset); return (p); bad: free(p); return (NULL); }
eth_t * eth_open(const char *device) { union DL_primitives *dlp; uint32_t buf[8192]; char *p, dev[16]; eth_t *e; int ppa; if ((e = calloc(1, sizeof(*e))) == NULL) return (NULL); #ifdef HAVE_SYS_DLPIHDR_H if ((e->fd = open("/dev/streams/dlb", O_RDWR)) < 0) return (eth_close(e)); if ((ppa = eth_match_ppa(e, device)) < 0) { errno = ESRCH; return (eth_close(e)); } #else e->fd = -1; snprintf(dev, sizeof(dev), "/dev/%s", device); if ((p = strpbrk(dev, "0123456789")) == NULL) { errno = EINVAL; return (eth_close(e)); } ppa = atoi(p); *p = '\0'; if ((e->fd = open(dev, O_RDWR)) < 0) { snprintf(dev, sizeof(dev), "/dev/%s", device); if ((e->fd = open(dev, O_RDWR)) < 0) return (eth_close(e)); } #endif dlp = (union DL_primitives *)buf; dlp->info_req.dl_primitive = DL_INFO_REQ; if (dlpi_msg(e->fd, dlp, DL_INFO_REQ_SIZE, RS_HIPRI, DL_INFO_ACK, DL_INFO_ACK_SIZE, sizeof(buf)) < 0) return (eth_close(e)); e->sap_len = dlp->info_ack.dl_sap_length; if (dlp->info_ack.dl_provider_style == DL_STYLE2) { dlp->attach_req.dl_primitive = DL_ATTACH_REQ; dlp->attach_req.dl_ppa = ppa; if (dlpi_msg(e->fd, dlp, DL_ATTACH_REQ_SIZE, 0, DL_OK_ACK, DL_OK_ACK_SIZE, sizeof(buf)) < 0) return (eth_close(e)); } memset(&dlp->bind_req, 0, DL_BIND_REQ_SIZE); dlp->bind_req.dl_primitive = DL_BIND_REQ; #ifdef DL_HP_RAWDLS dlp->bind_req.dl_sap = 24; /* from HP-UX DLPI programmers guide */ dlp->bind_req.dl_service_mode = DL_HP_RAWDLS; #else dlp->bind_req.dl_sap = DL_ETHER; dlp->bind_req.dl_service_mode = DL_CLDLS; #endif if (dlpi_msg(e->fd, dlp, DL_BIND_REQ_SIZE, 0, DL_BIND_ACK, DL_BIND_ACK_SIZE, sizeof(buf)) < 0) return (eth_close(e)); #ifdef DLIOCRAW if (strioctl(e->fd, DLIOCRAW, 0, NULL) < 0) return (eth_close(e)); #endif return (e); }
int t_kopen(file_t *fp, dev_t rdev, int flags, TIUSER **tiptr, cred_t *cr) { int madefp = 0; struct T_info_ack inforeq; int retval; vnode_t *vp; struct strioctl strioc; int error; TIUSER *ntiptr; int rtries = 0; /* * Special case for install: miniroot needs to be able to access files * via NFS as though it were always in the global zone. */ if (nfs_global_client_only != 0) cr = kcred; KTLILOG(2, "t_kopen: fp %x, ", fp); KTLILOG(2, "rdev %x, ", rdev); KTLILOG(2, "flags %x\n", flags); *tiptr = NULL; error = 0; retval = 0; if (fp == NULL) { if (rdev == 0 || rdev == NODEV) { KTLILOG(1, "t_kopen: null device\n", 0); return (EINVAL); } /* * allocate a file pointer, but * no file descripter. */ if ((error = falloc(NULL, flags, &fp, NULL)) != 0) { KTLILOG(1, "t_kopen: falloc: %d\n", error); return (error); } /* Install proper cred in file */ if (cr != fp->f_cred) { crhold(cr); crfree(fp->f_cred); fp->f_cred = cr; } vp = makespecvp(rdev, VCHR); /* * this will call the streams open for us. * Want to retry if error is EAGAIN, the streams open routine * might fail due to temporarely out of memory. */ do { if ((error = VOP_OPEN(&vp, flags, cr)) == EAGAIN) { (void) delay(hz); } } while (error == EAGAIN && ++rtries < 5); if (error) { KTLILOG(1, "t_kopen: VOP_OPEN: %d\n", error); unfalloc(fp); VN_RELE(vp); return (error); } /* * fp is completely initialized so drop the write lock. * I actually don't need any locking on fp in here since * there is no fd pointing at it. However, since I could * call closef if there is an error and closef requires * the fp read locked, I will acquire the read lock here * and make sure I release it before I leave this routine. */ fp->f_vnode = vp; mutex_exit(&fp->f_tlock); madefp = 1; } else { vp = fp->f_vnode; } if (vp->v_stream == NULL) { if (madefp) (void) closef(fp); KTLILOG(1, "t_kopen: not a streams device\n", 0); return (ENOSTR); } /* * allocate a new transport structure */ ntiptr = kmem_alloc(TIUSERSZ, KM_SLEEP); ntiptr->fp = fp; ntiptr->flags = madefp ? MADE_FP : 0; KTLILOG(2, "t_kopen: vp %x, ", vp); KTLILOG(2, "stp %x\n", vp->v_stream); /* * see if TIMOD is already pushed */ error = strioctl(vp, I_FIND, (intptr_t)"timod", 0, K_TO_K, cr, &retval); if (error) { kmem_free(ntiptr, TIUSERSZ); if (madefp) (void) closef(fp); KTLILOG(1, "t_kopen: strioctl(I_FIND, timod): %d\n", error); return (error); } if (retval == 0) { tryagain: error = strioctl(vp, I_PUSH, (intptr_t)"timod", 0, K_TO_K, cr, &retval); if (error) { switch (error) { case ENOSPC: case EAGAIN: case ENOSR: /* * This probably means the master file * should be tuned. */ cmn_err(CE_WARN, "t_kopen: I_PUSH of timod failed, error %d\n", error); (void) delay(hz); error = 0; goto tryagain; default: kmem_free(ntiptr, TIUSERSZ); if (madefp) (void) closef(fp); KTLILOG(1, "t_kopen: I_PUSH (timod): %d", error); return (error); } } } inforeq.PRIM_type = T_INFO_REQ; strioc.ic_cmd = TI_GETINFO; strioc.ic_timout = 0; strioc.ic_dp = (char *)&inforeq; strioc.ic_len = (int)sizeof (struct T_info_req); error = strdoioctl(vp->v_stream, &strioc, FNATIVE, K_TO_K, cr, &retval); if (error) { kmem_free(ntiptr, TIUSERSZ); if (madefp) (void) closef(fp); KTLILOG(1, "t_kopen: strdoioctl(T_INFO_REQ): %d\n", error); return (error); } if (retval) { if ((retval & 0xff) == TSYSERR) error = (retval >> 8) & 0xff; else error = t_tlitosyserr(retval & 0xff); kmem_free(ntiptr, TIUSERSZ); if (madefp) (void) closef(fp); KTLILOG(1, "t_kopen: strdoioctl(T_INFO_REQ): retval: 0x%x\n", retval); return (error); }
/* * link_open -- Open the specified link-level device * * Inputs: * * device The name of the device to open * * Returns: * * A pointer to a link handle structure. */ static link_t * link_open(const char *device) { union DL_primitives *dlp; unsigned char buf[MAXDLBUF]; char *p; char dev[16]; link_t *handle; int ppa; handle = Malloc(sizeof(*handle)); memset(handle, '\0', sizeof(*handle)); #ifdef HAVE_SYS_DLPIHDR_H if ((handle->fd = open("/dev/streams/dlb", O_RDWR)) < 0) { free(handle); return NULL; } if ((ppa = link_match_ppa(handle, device)) < 0) { link_close(handle); return NULL; } #else handle->fd = -1; snprintf(dev, sizeof(dev), "/dev/%s", device); if ((p = strpbrk(dev, "0123456789")) == NULL) { link_close(handle); return NULL; } ppa = atoi(p); *p = '\0'; if ((handle->fd = open(dev, O_RDWR)) < 0) { snprintf(dev, sizeof(dev), "/dev/%s", device); if ((handle->fd = open(dev, O_RDWR)) < 0) { link_close(handle); return NULL; } } #endif memset(&(handle->ifr), 0, sizeof(struct ifreq)); strlcpy(handle->ifr.ifr_name, device, sizeof(handle->ifr.ifr_name)); dlp = (union DL_primitives *)buf; dlp->info_req.dl_primitive = DL_INFO_REQ; if (dlpi_msg(handle->fd, dlp, DL_INFO_REQ_SIZE, RS_HIPRI, DL_INFO_ACK, DL_INFO_ACK_SIZE, sizeof(buf)) < 0) { link_close(handle); return NULL; } handle->sap_first = (dlp->info_ack.dl_sap_length > 0); if (dlp->info_ack.dl_provider_style == DL_STYLE2) { dlp->attach_req.dl_primitive = DL_ATTACH_REQ; dlp->attach_req.dl_ppa = ppa; if (dlpi_msg(handle->fd, dlp, DL_ATTACH_REQ_SIZE, 0, DL_OK_ACK, DL_OK_ACK_SIZE, sizeof(buf)) < 0) { link_close(handle); return NULL; } } memset(&dlp->bind_req, 0, DL_BIND_REQ_SIZE); dlp->bind_req.dl_primitive = DL_BIND_REQ; #ifdef DL_HP_RAWDLS dlp->bind_req.dl_sap = 24; /* from HP-UX DLPI programmers guide */ dlp->bind_req.dl_service_mode = DL_HP_RAWDLS; #else dlp->bind_req.dl_sap = DL_ETHER; dlp->bind_req.dl_service_mode = DL_CLDLS; #endif if (dlpi_msg(handle->fd, dlp, DL_BIND_REQ_SIZE, 0, DL_BIND_ACK, DL_BIND_ACK_SIZE, sizeof(buf)) < 0) { link_close(handle); return NULL; } #ifdef DLIOCRAW if (strioctl(handle->fd, DLIOCRAW, 0, NULL) < 0) { link_close(handle); return NULL; } #endif return (handle); }
pcap_t * pcap_open_live(const char *device, int snaplen, int promisc, int to_ms, char *ebuf) { register char *cp; register pcap_t *p; int ppa; #ifdef HAVE_SOLARIS int isatm = 0; #endif register dl_info_ack_t *infop; #ifdef HAVE_SYS_BUFMOD_H bpf_u_int32 ss, chunksize; #ifdef HAVE_SOLARIS register char *release; bpf_u_int32 osmajor, osminor, osmicro; #endif #endif bpf_u_int32 buf[MAXDLBUF]; char dname[100]; #ifndef HAVE_DEV_DLPI char dname2[100]; #endif p = (pcap_t *)malloc(sizeof(*p)); if (p == NULL) { strlcpy(ebuf, pcap_strerror(errno), PCAP_ERRBUF_SIZE); return (NULL); } memset(p, 0, sizeof(*p)); p->fd = -1; /* indicate that it hasn't been opened yet */ p->send_fd = -1; #ifdef HAVE_DEV_DLPI /* ** Remove any "/dev/" on the front of the device. */ cp = strrchr(device, '/'); if (cp == NULL) strlcpy(dname, device, sizeof(dname)); else strlcpy(dname, cp + 1, sizeof(dname)); /* * Split the device name into a device type name and a unit number; * chop off the unit number, so "dname" is just a device type name. */ cp = split_dname(dname, &ppa, ebuf); if (cp == NULL) goto bad; *cp = '\0'; /* * Use "/dev/dlpi" as the device. * * XXX - HP's DLPI Programmer's Guide for HP-UX 11.00 says that * the "dl_mjr_num" field is for the "major number of interface * driver"; that's the major of "/dev/dlpi" on the system on * which I tried this, but there may be DLPI devices that * use a different driver, in which case we may need to * search "/dev" for the appropriate device with that major * device number, rather than hardwiring "/dev/dlpi". */ cp = "/dev/dlpi"; if ((p->fd = open(cp, O_RDWR)) < 0) { snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s: %s", cp, pcap_strerror(errno)); goto bad; } #ifdef DL_HP_RAWDLS /* * XXX - HP-UX 10.20 and 11.xx don't appear to support sending and * receiving packets on the same descriptor - you need separate * descriptors for sending and receiving, bound to different SAPs. * * If the open fails, we just leave -1 in "p->send_fd" and reject * attempts to send packets, just as if, in pcap-bpf.c, we fail * to open the BPF device for reading and writing, we just try * to open it for reading only and, if that succeeds, just let * the send attempts fail. */ p->send_fd = open(cp, O_RDWR); #endif /* * Get a table of all PPAs for that device, and search that * table for the specified device type name and unit number. */ ppa = get_dlpi_ppa(p->fd, dname, ppa, ebuf); if (ppa < 0) goto bad; #else /* * If the device name begins with "/", assume it begins with * the pathname of the directory containing the device to open; * otherwise, concatenate the device directory name and the * device name. */ if (*device == '/') strlcpy(dname, device, sizeof(dname)); else snprintf(dname, sizeof(dname), "%s/%s", PCAP_DEV_PREFIX, device); /* * Get the unit number, and a pointer to the end of the device * type name. */ cp = split_dname(dname, &ppa, ebuf); if (cp == NULL) goto bad; /* * Make a copy of the device pathname, and then remove the unit * number from the device pathname. */ strlcpy(dname2, dname, sizeof(dname)); *cp = '\0'; /* Try device without unit number */ if ((p->fd = open(dname, O_RDWR)) < 0) { if (errno != ENOENT) { snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s: %s", dname, pcap_strerror(errno)); goto bad; } /* Try again with unit number */ if ((p->fd = open(dname2, O_RDWR)) < 0) { if (errno == ENOENT) { /* * We just report "No DLPI device found" * with the device name, so people don't * get confused and think, for example, * that if they can't capture on "lo0" * on Solaris the fix is to change libpcap * (or the application that uses it) to * look for something other than "/dev/lo0", * as the fix is to look for an operating * system other than Solaris - you just * *can't* capture on a loopback interface * on Solaris, the lack of a DLPI device * for the loopback interface is just a * symptom of that inability. */ snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s: No DLPI device found", device); } else { snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s: %s", dname2, pcap_strerror(errno)); } goto bad; } /* XXX Assume unit zero */ ppa = 0; } #endif p->snapshot = snaplen; /* ** Attach if "style 2" provider */ if (dlinforeq(p->fd, ebuf) < 0 || dlinfoack(p->fd, (char *)buf, ebuf) < 0) goto bad; infop = &((union DL_primitives *)buf)->info_ack; #ifdef HAVE_SOLARIS if (infop->dl_mac_type == DL_IPATM) isatm = 1; #endif if (infop->dl_provider_style == DL_STYLE2) { if (dl_doattach(p->fd, ppa, ebuf) < 0) goto bad; #ifdef DL_HP_RAWDLS if (p->send_fd >= 0) { if (dl_doattach(p->send_fd, ppa, ebuf) < 0) goto bad; } #endif } /* ** Bind (defer if using HP-UX 9 or HP-UX 10.20 or later, totally ** skip if using SINIX) */ #if !defined(HAVE_HPUX9) && !defined(HAVE_HPUX10_20_OR_LATER) && !defined(sinix) #ifdef _AIX /* ** AIX. ** According to IBM's AIX Support Line, the dl_sap value ** should not be less than 0x600 (1536) for standard Ethernet. ** However, we seem to get DL_BADADDR - "DLSAP addr in improper ** format or invalid" - errors if we use 1537 on the "tr0" ** device, which, given that its name starts with "tr" and that ** it's IBM, probably means a Token Ring device. (Perhaps we ** need to use 1537 on "/dev/dlpi/en" because that device is for ** D/I/X Ethernet, the "SAP" is actually an Ethernet type, and ** it rejects invalid Ethernet types.) ** ** So if 1537 fails, we try 2, as Hyung Sik Yoon of IBM Korea ** says that works on Token Ring (he says that 0 does *not* ** work; perhaps that's considered an invalid LLC SAP value - I ** assume the SAP value in a DLPI bind is an LLC SAP for network ** types that use 802.2 LLC). */ if ((dlbindreq(p->fd, 1537, ebuf) < 0 && dlbindreq(p->fd, 2, ebuf) < 0) || dlbindack(p->fd, (char *)buf, ebuf, NULL) < 0) goto bad; #elif defined(DL_HP_RAWDLS) /* ** HP-UX 10.0x and 10.1x. */ if (dl_dohpuxbind(p->fd, ebuf) < 0) goto bad; if (p->send_fd >= 0) { /* ** XXX - if this fails, just close send_fd and ** set it to -1, so that you can't send but can ** still receive? */ if (dl_dohpuxbind(p->send_fd, ebuf) < 0) goto bad; } #else /* neither AIX nor HP-UX */ /* ** Not Sinix, and neither AIX nor HP-UX - Solaris, and any other ** OS using DLPI. **/ if (dlbindreq(p->fd, 0, ebuf) < 0 || dlbindack(p->fd, (char *)buf, ebuf, NULL) < 0) goto bad; #endif /* AIX vs. HP-UX vs. other */ #endif /* !HP-UX 9 and !HP-UX 10.20 or later and !SINIX */ #ifdef HAVE_SOLARIS if (isatm) { /* ** Have to turn on some special ATM promiscuous mode ** for SunATM. ** Do *NOT* turn regular promiscuous mode on; it doesn't ** help, and may break things. */ if (strioctl(p->fd, A_PROMISCON_REQ, 0, NULL) < 0) { snprintf(ebuf, PCAP_ERRBUF_SIZE, "A_PROMISCON_REQ: %s", pcap_strerror(errno)); goto bad; } } else #endif if (promisc) { /* ** Enable promiscuous (not necessary on send FD) */ if (dlpromisconreq(p->fd, DL_PROMISC_PHYS, ebuf) < 0 || dlokack(p->fd, "promisc_phys", (char *)buf, ebuf) < 0) goto bad; /* ** Try to enable multicast (you would have thought ** promiscuous would be sufficient). (Skip if using ** HP-UX or SINIX) (Not necessary on send FD) */ #if !defined(__hpux) && !defined(sinix) if (dlpromisconreq(p->fd, DL_PROMISC_MULTI, ebuf) < 0 || dlokack(p->fd, "promisc_multi", (char *)buf, ebuf) < 0) fprintf(stderr, "WARNING: DL_PROMISC_MULTI failed (%s)\n", ebuf); #endif } /* ** Try to enable SAP promiscuity (when not in promiscuous mode ** when using HP-UX, when not doing SunATM on Solaris, and never ** under SINIX) (Not necessary on send FD) */ #ifndef sinix if ( #ifdef __hpux !promisc && #endif #ifdef HAVE_SOLARIS !isatm && #endif (dlpromisconreq(p->fd, DL_PROMISC_SAP, ebuf) < 0 || dlokack(p->fd, "promisc_sap", (char *)buf, ebuf) < 0)) { /* Not fatal if promisc since the DL_PROMISC_PHYS worked */ if (promisc) fprintf(stderr, "WARNING: DL_PROMISC_SAP failed (%s)\n", ebuf); else goto bad; } #endif /* sinix */ /* ** HP-UX 9, and HP-UX 10.20 or later, must bind after setting ** promiscuous options. */ #if defined(HAVE_HPUX9) || defined(HAVE_HPUX10_20_OR_LATER) if (dl_dohpuxbind(p->fd, ebuf) < 0) goto bad; /* ** We don't set promiscuous mode on the send FD, but we'll defer ** binding it anyway, just to keep the HP-UX 9/10.20 or later ** code together. */ if (p->send_fd >= 0) { /* ** XXX - if this fails, just close send_fd and ** set it to -1, so that you can't send but can ** still receive? */ if (dl_dohpuxbind(p->send_fd, ebuf) < 0) goto bad; } #endif /* ** Determine link type ** XXX - get SAP length and address length as well, for use ** when sending packets. */ if (dlinforeq(p->fd, ebuf) < 0 || dlinfoack(p->fd, (char *)buf, ebuf) < 0) goto bad; infop = &((union DL_primitives *)buf)->info_ack; switch (infop->dl_mac_type) { case DL_CSMACD: case DL_ETHER: p->linktype = DLT_EN10MB; p->offset = 2; /* * This is (presumably) a real Ethernet capture; give it a * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so * that an application can let you choose it, in case you're * capturing DOCSIS traffic that a Cisco Cable Modem * Termination System is putting out onto an Ethernet (it * doesn't put an Ethernet header onto the wire, it puts raw * DOCSIS frames out on the wire inside the low-level * Ethernet framing). */ p->dlt_list = (u_int *) malloc(sizeof(u_int) * 2); /* * If that fails, just leave the list empty. */ if (p->dlt_list != NULL) { p->dlt_list[0] = DLT_EN10MB; p->dlt_list[1] = DLT_DOCSIS; p->dlt_count = 2; } break; case DL_FDDI: p->linktype = DLT_FDDI; p->offset = 3; break; case DL_TPR: /* * XXX - what about DL_TPB? Is that Token Bus? */ p->linktype = DLT_IEEE802; p->offset = 2; break; #ifdef HAVE_SOLARIS case DL_IPATM: p->linktype = DLT_SUNATM; p->offset = 0; /* works for LANE and LLC encapsulation */ break; #endif default: snprintf(ebuf, PCAP_ERRBUF_SIZE, "unknown mac type %lu", (unsigned long)infop->dl_mac_type); goto bad; } #ifdef DLIOCRAW /* ** This is a non standard SunOS hack to get the full raw link-layer ** header. */ if (strioctl(p->fd, DLIOCRAW, 0, NULL) < 0) { snprintf(ebuf, PCAP_ERRBUF_SIZE, "DLIOCRAW: %s", pcap_strerror(errno)); goto bad; } #endif #ifdef HAVE_SYS_BUFMOD_H /* ** Another non standard call to get the data nicely buffered */ if (ioctl(p->fd, I_PUSH, "bufmod") != 0) { snprintf(ebuf, PCAP_ERRBUF_SIZE, "I_PUSH bufmod: %s", pcap_strerror(errno)); goto bad; } /* ** Now that the bufmod is pushed lets configure it. ** ** There is a bug in bufmod(7). When dealing with messages of ** less than snaplen size it strips data from the beginning not ** the end. ** ** This bug is supposed to be fixed in 5.3.2. Also, there is a ** patch available. Ask for bugid 1149065. */ ss = snaplen; #ifdef HAVE_SOLARIS release = get_release(&osmajor, &osminor, &osmicro); if (osmajor == 5 && (osminor <= 2 || (osminor == 3 && osmicro < 2)) && getenv("BUFMOD_FIXED") == NULL) { fprintf(stderr, "WARNING: bufmod is broken in SunOS %s; ignoring snaplen.\n", release); ss = 0; } #endif if (ss > 0 && strioctl(p->fd, SBIOCSSNAP, sizeof(ss), (char *)&ss) != 0) { snprintf(ebuf, PCAP_ERRBUF_SIZE, "SBIOCSSNAP: %s", pcap_strerror(errno)); goto bad; } /* ** Set up the bufmod timeout */ if (to_ms != 0) { struct timeval to; to.tv_sec = to_ms / 1000; to.tv_usec = (to_ms * 1000) % 1000000; if (strioctl(p->fd, SBIOCSTIME, sizeof(to), (char *)&to) != 0) { snprintf(ebuf, PCAP_ERRBUF_SIZE, "SBIOCSTIME: %s", pcap_strerror(errno)); goto bad; } } /* ** Set the chunk length. */ chunksize = CHUNKSIZE; if (strioctl(p->fd, SBIOCSCHUNK, sizeof(chunksize), (char *)&chunksize) != 0) { snprintf(ebuf, PCAP_ERRBUF_SIZE, "SBIOCSCHUNKP: %s", pcap_strerror(errno)); goto bad; } #endif /* ** As the last operation flush the read side. */ if (ioctl(p->fd, I_FLUSH, FLUSHR) != 0) { snprintf(ebuf, PCAP_ERRBUF_SIZE, "FLUSHR: %s", pcap_strerror(errno)); goto bad; } /* Allocate data buffer */ p->bufsize = PKTBUFSIZE; p->buffer = (u_char *)malloc(p->bufsize + p->offset); if (p->buffer == NULL) { strlcpy(ebuf, pcap_strerror(errno), PCAP_ERRBUF_SIZE); goto bad; } /* * "p->fd" is an FD for a STREAMS device, so "select()" and * "poll()" should work on it. */ p->selectable_fd = p->fd; p->read_op = pcap_read_dlpi; p->inject_op = pcap_inject_dlpi; p->setfilter_op = install_bpf_program; /* no kernel filtering */ p->setdirection_op = NULL; /* Not implemented.*/ p->set_datalink_op = NULL; /* can't change data link type */ p->getnonblock_op = pcap_getnonblock_fd; p->setnonblock_op = pcap_setnonblock_fd; p->stats_op = pcap_stats_dlpi; p->close_op = pcap_close_dlpi; return (p); bad: if (p->fd >= 0) close(p->fd); if (p->send_fd >= 0) close(p->send_fd); /* * Get rid of any link-layer type list we allocated. */ if (p->dlt_list != NULL) free(p->dlt_list); free(p); return (NULL); }
int main(int argc, char *argv[]) { int c; #ifdef STREAMS char *dev; #endif interface = "ppp0"; if ((progname = strrchr(argv[0], '/')) == NULL) progname = argv[0]; else ++progname; while ((c = getopt(argc, argv, "advrzc:w:")) != -1) { switch (c) { case 'a': ++aflag; break; case 'd': ++dflag; break; case 'v': ++vflag; break; case 'r': ++rflag; break; case 'z': ++zflag; break; case 'c': count = atoi(optarg); if (count <= 0) usage(); break; case 'w': interval = atoi(optarg); if (interval <= 0) usage(); break; default: usage(); } } argc -= optind; argv += optind; if (!interval && count) interval = 5; if (interval && !count) infinite = 1; if (!interval && !count) count = 1; if (aflag) dflag = 0; if (argc > 1) usage(); if (argc > 0) interface = argv[0]; if (sscanf(interface, "ppp%d", &unit) != 1) { fprintf(stderr, "%s: invalid interface '%s' specified\n", progname, interface); } #ifndef STREAMS { struct ifreq ifr; s = socket(AF_INET, SOCK_DGRAM, 0); if (s < 0) { fprintf(stderr, "%s: ", progname); perror("couldn't create IP socket"); exit(1); } #ifdef _linux_ #undef ifr_name #define ifr_name ifr_ifrn.ifrn_name #endif strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name)); if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) { fprintf(stderr, "%s: nonexistent interface '%s' specified\n", progname, interface); exit(1); } } #else /* STREAMS */ #ifdef __osf__ dev = "/dev/streams/ppp"; #else dev = "/dev/ppp"; #endif if ((s = open(dev, O_RDONLY)) < 0) { fprintf(stderr, "%s: couldn't open ", progname); perror(dev); exit(1); } if (strioctl(s, PPPIO_ATTACH, &unit, sizeof(int), 0) < 0) { fprintf(stderr, "%s: ppp%d is not available\n", progname, unit); exit(1); } #endif /* STREAMS */ intpr(); exit(0); }
static int pcap_activate_dlpi(pcap_t *p) { #ifdef DL_HP_RAWDLS struct pcap_dlpi *pd = p->priv; #endif int status = 0; int retv; register char *cp; int ppa; #ifdef HAVE_SOLARIS int isatm = 0; #endif register dl_info_ack_t *infop; #ifdef HAVE_SYS_BUFMOD_H bpf_u_int32 ss; #ifdef HAVE_SOLARIS register char *release; bpf_u_int32 osmajor, osminor, osmicro; #endif #endif bpf_u_int32 buf[MAXDLBUF]; char dname[100]; #ifndef HAVE_DEV_DLPI char dname2[100]; #endif #ifdef HAVE_DEV_DLPI /* ** Remove any "/dev/" on the front of the device. */ cp = strrchr(p->opt.source, '/'); if (cp == NULL) strlcpy(dname, p->opt.source, sizeof(dname)); else strlcpy(dname, cp + 1, sizeof(dname)); /* * Split the device name into a device type name and a unit number; * chop off the unit number, so "dname" is just a device type name. */ cp = split_dname(dname, &ppa, p->errbuf); if (cp == NULL) { status = PCAP_ERROR_NO_SUCH_DEVICE; goto bad; } *cp = '\0'; /* * Use "/dev/dlpi" as the device. * * XXX - HP's DLPI Programmer's Guide for HP-UX 11.00 says that * the "dl_mjr_num" field is for the "major number of interface * driver"; that's the major of "/dev/dlpi" on the system on * which I tried this, but there may be DLPI devices that * use a different driver, in which case we may need to * search "/dev" for the appropriate device with that major * device number, rather than hardwiring "/dev/dlpi". */ cp = "/dev/dlpi"; if ((p->fd = open(cp, O_RDWR)) < 0) { if (errno == EPERM || errno == EACCES) status = PCAP_ERROR_PERM_DENIED; else status = PCAP_ERROR; snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "%s: %s", cp, pcap_strerror(errno)); goto bad; } #ifdef DL_HP_RAWDLS /* * XXX - HP-UX 10.20 and 11.xx don't appear to support sending and * receiving packets on the same descriptor - you need separate * descriptors for sending and receiving, bound to different SAPs. * * If the open fails, we just leave -1 in "pd->send_fd" and reject * attempts to send packets, just as if, in pcap-bpf.c, we fail * to open the BPF device for reading and writing, we just try * to open it for reading only and, if that succeeds, just let * the send attempts fail. */ pd->send_fd = open(cp, O_RDWR); #endif /* * Get a table of all PPAs for that device, and search that * table for the specified device type name and unit number. */ ppa = get_dlpi_ppa(p->fd, dname, ppa, p->errbuf); if (ppa < 0) { status = ppa; goto bad; } #else /* * If the device name begins with "/", assume it begins with * the pathname of the directory containing the device to open; * otherwise, concatenate the device directory name and the * device name. */ if (*p->opt.source == '/') strlcpy(dname, p->opt.source, sizeof(dname)); else snprintf(dname, sizeof(dname), "%s/%s", PCAP_DEV_PREFIX, p->opt.source); /* * Get the unit number, and a pointer to the end of the device * type name. */ cp = split_dname(dname, &ppa, p->errbuf); if (cp == NULL) { status = PCAP_ERROR_NO_SUCH_DEVICE; goto bad; } /* * Make a copy of the device pathname, and then remove the unit * number from the device pathname. */ strlcpy(dname2, dname, sizeof(dname)); *cp = '\0'; /* Try device without unit number */ if ((p->fd = open(dname, O_RDWR)) < 0) { if (errno != ENOENT) { if (errno == EPERM || errno == EACCES) status = PCAP_ERROR_PERM_DENIED; else status = PCAP_ERROR; snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "%s: %s", dname, pcap_strerror(errno)); goto bad; } /* Try again with unit number */ if ((p->fd = open(dname2, O_RDWR)) < 0) { if (errno == ENOENT) { status = PCAP_ERROR_NO_SUCH_DEVICE; /* * We provide an error message even * for this error, for diagnostic * purposes (so that, for example, * the app can show the message if the * user requests it). * * In it, we just report "No DLPI device * found" with the device name, so people * don't get confused and think, for example, * that if they can't capture on "lo0" * on Solaris the fix is to change libpcap * (or the application that uses it) to * look for something other than "/dev/lo0", * as the fix is to look for an operating * system other than Solaris - you just * *can't* capture on a loopback interface * on Solaris, the lack of a DLPI device * for the loopback interface is just a * symptom of that inability. */ snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "%s: No DLPI device found", p->opt.source); } else { if (errno == EPERM || errno == EACCES) status = PCAP_ERROR_PERM_DENIED; else status = PCAP_ERROR; snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "%s: %s", dname2, pcap_strerror(errno)); } goto bad; } /* XXX Assume unit zero */ ppa = 0; } #endif /* ** Attach if "style 2" provider */ if (dlinforeq(p->fd, p->errbuf) < 0 || dlinfoack(p->fd, (char *)buf, p->errbuf) < 0) { status = PCAP_ERROR; goto bad; } infop = &(MAKE_DL_PRIMITIVES(buf))->info_ack; #ifdef HAVE_SOLARIS if (infop->dl_mac_type == DL_IPATM) isatm = 1; #endif if (infop->dl_provider_style == DL_STYLE2) { retv = dl_doattach(p->fd, ppa, p->errbuf); if (retv < 0) { status = retv; goto bad; } #ifdef DL_HP_RAWDLS if (pd->send_fd >= 0) { retv = dl_doattach(pd->send_fd, ppa, p->errbuf); if (retv < 0) { status = retv; goto bad; } } #endif } if (p->opt.rfmon) { /* * This device exists, but we don't support monitor mode * any platforms that support DLPI. */ status = PCAP_ERROR_RFMON_NOTSUP; goto bad; } #ifdef HAVE_DLPI_PASSIVE /* * Enable Passive mode to be able to capture on aggregated link. * Not supported in all Solaris versions. */ dlpassive(p->fd, p->errbuf); #endif /* ** Bind (defer if using HP-UX 9 or HP-UX 10.20 or later, totally ** skip if using SINIX) */ #if !defined(HAVE_HPUX9) && !defined(HAVE_HPUX10_20_OR_LATER) && !defined(sinix) #ifdef _AIX /* ** AIX. ** According to IBM's AIX Support Line, the dl_sap value ** should not be less than 0x600 (1536) for standard Ethernet. ** However, we seem to get DL_BADADDR - "DLSAP addr in improper ** format or invalid" - errors if we use 1537 on the "tr0" ** device, which, given that its name starts with "tr" and that ** it's IBM, probably means a Token Ring device. (Perhaps we ** need to use 1537 on "/dev/dlpi/en" because that device is for ** D/I/X Ethernet, the "SAP" is actually an Ethernet type, and ** it rejects invalid Ethernet types.) ** ** So if 1537 fails, we try 2, as Hyung Sik Yoon of IBM Korea ** says that works on Token Ring (he says that 0 does *not* ** work; perhaps that's considered an invalid LLC SAP value - I ** assume the SAP value in a DLPI bind is an LLC SAP for network ** types that use 802.2 LLC). */ if ((dlbindreq(p->fd, 1537, p->errbuf) < 0 && dlbindreq(p->fd, 2, p->errbuf) < 0) || dlbindack(p->fd, (char *)buf, p->errbuf, NULL) < 0) { status = PCAP_ERROR; goto bad; } #elif defined(DL_HP_RAWDLS) /* ** HP-UX 10.0x and 10.1x. */ if (dl_dohpuxbind(p->fd, p->errbuf) < 0) { status = PCAP_ERROR; goto bad; } if (pd->send_fd >= 0) { /* ** XXX - if this fails, just close send_fd and ** set it to -1, so that you can't send but can ** still receive? */ if (dl_dohpuxbind(pd->send_fd, p->errbuf) < 0) { status = PCAP_ERROR; goto bad; } } #else /* neither AIX nor HP-UX */ /* ** Not Sinix, and neither AIX nor HP-UX - Solaris, and any other ** OS using DLPI. **/ if (dlbindreq(p->fd, 0, p->errbuf) < 0 || dlbindack(p->fd, (char *)buf, p->errbuf, NULL) < 0) { status = PCAP_ERROR; goto bad; } #endif /* AIX vs. HP-UX vs. other */ #endif /* !HP-UX 9 and !HP-UX 10.20 or later and !SINIX */ #ifdef HAVE_SOLARIS if (isatm) { /* ** Have to turn on some special ATM promiscuous mode ** for SunATM. ** Do *NOT* turn regular promiscuous mode on; it doesn't ** help, and may break things. */ if (strioctl(p->fd, A_PROMISCON_REQ, 0, NULL) < 0) { status = PCAP_ERROR; snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "A_PROMISCON_REQ: %s", pcap_strerror(errno)); goto bad; } } else #endif if (p->opt.promisc) { /* ** Enable promiscuous (not necessary on send FD) */ retv = dlpromiscon(p, DL_PROMISC_PHYS); if (retv < 0) { if (retv == PCAP_ERROR_PERM_DENIED) status = PCAP_ERROR_PROMISC_PERM_DENIED; else status = retv; goto bad; } /* ** Try to enable multicast (you would have thought ** promiscuous would be sufficient). (Skip if using ** HP-UX or SINIX) (Not necessary on send FD) */ #if !defined(__hpux) && !defined(sinix) retv = dlpromiscon(p, DL_PROMISC_MULTI); if (retv < 0) status = PCAP_WARNING; #endif } /* ** Try to enable SAP promiscuity (when not in promiscuous mode ** when using HP-UX, when not doing SunATM on Solaris, and never ** under SINIX) (Not necessary on send FD) */ #ifndef sinix #if defined(__hpux) /* HP-UX - only do this when not in promiscuous mode */ if (!p->opt.promisc) { #elif defined(HAVE_SOLARIS) /* Solaris - don't do this on SunATM devices */ if (!isatm) { #else /* Everything else (except for SINIX) - always do this */ { #endif retv = dlpromiscon(p, DL_PROMISC_SAP); if (retv < 0) { if (p->opt.promisc) { /* * Not fatal, since the DL_PROMISC_PHYS mode * worked. * * Report it as a warning, however. */ status = PCAP_WARNING; } else { /* * Fatal. */ status = retv; goto bad; } } } #endif /* sinix */ /* ** HP-UX 9, and HP-UX 10.20 or later, must bind after setting ** promiscuous options. */ #if defined(HAVE_HPUX9) || defined(HAVE_HPUX10_20_OR_LATER) if (dl_dohpuxbind(p->fd, p->errbuf) < 0) { status = PCAP_ERROR; goto bad; } /* ** We don't set promiscuous mode on the send FD, but we'll defer ** binding it anyway, just to keep the HP-UX 9/10.20 or later ** code together. */ if (pd->send_fd >= 0) { /* ** XXX - if this fails, just close send_fd and ** set it to -1, so that you can't send but can ** still receive? */ if (dl_dohpuxbind(pd->send_fd, p->errbuf) < 0) { status = PCAP_ERROR; goto bad; } } #endif /* ** Determine link type ** XXX - get SAP length and address length as well, for use ** when sending packets. */ if (dlinforeq(p->fd, p->errbuf) < 0 || dlinfoack(p->fd, (char *)buf, p->errbuf) < 0) { status = PCAP_ERROR; goto bad; } infop = &(MAKE_DL_PRIMITIVES(buf))->info_ack; if (pcap_process_mactype(p, infop->dl_mac_type) != 0) { status = PCAP_ERROR; goto bad; } #ifdef DLIOCRAW /* ** This is a non standard SunOS hack to get the full raw link-layer ** header. */ if (strioctl(p->fd, DLIOCRAW, 0, NULL) < 0) { status = PCAP_ERROR; snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "DLIOCRAW: %s", pcap_strerror(errno)); goto bad; } #endif #ifdef HAVE_SYS_BUFMOD_H ss = p->snapshot; /* ** There is a bug in bufmod(7). When dealing with messages of ** less than snaplen size it strips data from the beginning not ** the end. ** ** This bug is fixed in 5.3.2. Also, there is a patch available. ** Ask for bugid 1149065. */ #ifdef HAVE_SOLARIS release = get_release(&osmajor, &osminor, &osmicro); if (osmajor == 5 && (osminor <= 2 || (osminor == 3 && osmicro < 2)) && getenv("BUFMOD_FIXED") == NULL) { snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "WARNING: bufmod is broken in SunOS %s; ignoring snaplen.", release); ss = 0; status = PCAP_WARNING; } #endif /* Push and configure bufmod. */ if (pcap_conf_bufmod(p, ss) != 0) { status = PCAP_ERROR; goto bad; } #endif /* ** As the last operation flush the read side. */ if (ioctl(p->fd, I_FLUSH, FLUSHR) != 0) { status = PCAP_ERROR; snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "FLUSHR: %s", pcap_strerror(errno)); goto bad; } /* Allocate data buffer. */ if (pcap_alloc_databuf(p) != 0) { status = PCAP_ERROR; goto bad; } /* * Success. * * "p->fd" is an FD for a STREAMS device, so "select()" and * "poll()" should work on it. */ p->selectable_fd = p->fd; p->read_op = pcap_read_dlpi; p->inject_op = pcap_inject_dlpi; p->setfilter_op = install_bpf_program; /* no kernel filtering */ p->setdirection_op = NULL; /* Not implemented.*/ p->set_datalink_op = NULL; /* can't change data link type */ p->getnonblock_op = pcap_getnonblock_fd; p->setnonblock_op = pcap_setnonblock_fd; p->stats_op = pcap_stats_dlpi; p->cleanup_op = pcap_cleanup_dlpi; return (status); bad: pcap_cleanup_dlpi(p); return (status); } /* * Split a device name into a device type name and a unit number; * return the a pointer to the beginning of the unit number, which * is the end of the device type name, and set "*unitp" to the unit * number. * * Returns NULL on error, and fills "ebuf" with an error message. */ static char * split_dname(char *device, int *unitp, char *ebuf) { char *cp; char *eos; long unit; /* * Look for a number at the end of the device name string. */ cp = device + strlen(device) - 1; if (*cp < '0' || *cp > '9') { snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s missing unit number", device); return (NULL); } /* Digits at end of string are unit number */ while (cp-1 >= device && *(cp-1) >= '0' && *(cp-1) <= '9') cp--; errno = 0; unit = strtol(cp, &eos, 10); if (*eos != '\0') { snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s bad unit number", device); return (NULL); } if (errno == ERANGE || unit > INT_MAX) { snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s unit number too large", device); return (NULL); } if (unit < 0) { snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s unit number is negative", device); return (NULL); } *unitp = (int)unit; return (cp); } static int dl_doattach(int fd, int ppa, char *ebuf) { dl_attach_req_t req; bpf_u_int32 buf[MAXDLBUF]; int err; req.dl_primitive = DL_ATTACH_REQ; req.dl_ppa = ppa; if (send_request(fd, (char *)&req, sizeof(req), "attach", ebuf) < 0) return (PCAP_ERROR); err = dlokack(fd, "attach", (char *)buf, ebuf); if (err < 0) return (err); return (0); }
int openInterface(char const *ifname, UINT16_t type, unsigned char *hwaddr) { int fd; long buf[MAXDLBUF]; union DL_primitives *dlp; char base_dev[PATH_MAX]; int ppa; if(strlen(ifname) > PATH_MAX) { rp_fatal("socket: Interface name too long"); } if (strlen(ifname) < 2) { rp_fatal("socket: Interface name too short"); } ppa = atoi(&ifname[strlen(ifname)-1]); strncpy(base_dev, ifname, PATH_MAX); base_dev[strlen(base_dev)-1] = '\0'; dlp = (union DL_primitives*) buf; if ( (fd = open(base_dev, O_RDWR)) < 0) { if (errno == EPERM) { rp_fatal("Cannot create raw socket -- pppoe must be run as root."); } if (errno == ENOENT) { char ifname[512]; snprintf(ifname, sizeof(ifname), "/dev/%s", base_dev); if ((fd = open(ifname, O_RDWR)) < 0) { if (errno == EPERM) { rp_fatal("Cannot create raw socket -- pppoe must be run as root."); } } } } if (fd < 0) { fatalSys("socket"); } dlattachreq(fd, ppa); dlokack(fd, (char *)buf); dlbindreq(fd, type, 0, DL_CLDLS, 0, 0); dlbindack(fd, (char *)buf); dlinforeq(fd); dlinfoack(fd, (char *)buf); dl_abssaplen = ABS(dlp->info_ack.dl_sap_length); dl_saplen = dlp->info_ack.dl_sap_length; if (ETHERADDRL != (dlp->info_ack.dl_addr_length - dl_abssaplen)) fatalSys("invalid destination physical address length"); dl_addrlen = dl_abssaplen + ETHERADDRL; memcpy(hwaddr, (u_char*)((char*)(dlp) + (int)(dlp->info_ack.dl_addr_offset)), ETHERADDRL); if ( strioctl(fd, DLIOCRAW, -1, 0, NULL) < 0 ) { fatalSys("DLIOCRAW"); } if (ioctl(fd, I_FLUSH, FLUSHR) < 0) fatalSys("I_FLUSH"); return fd; }
int libnet_open_link(libnet_t *l) { register char *cp; char *eos; register int ppa; register dl_info_ack_t *infop; bpf_u_int32 buf[MAXDLBUF]; char dname[100]; #ifndef HAVE_DEV_DLPI char dname2[100]; #endif if (l == NULL) { return (-1); } /* * Determine device and ppa */ cp = strpbrk(l->device, "0123456789"); if (cp == NULL) { sprintf(l->err_buf, "%s missing unit number", l->device); goto bad; } ppa = strtol(cp, &eos, 10); if (*eos != '\0') { snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, "libnet_open_link: %s bad unit number", l->device); goto bad; } if (*(l->device) == '/') { memset(&dname, 0, sizeof(dname)); strncpy(dname, l->device, sizeof(dname) - 1); } else { sprintf(dname, "%s/%s", DLPI_DEV_PREFIX, l->device); } #ifdef HAVE_DEV_DLPI /* * Map network device to /dev/dlpi unit */ cp = "/dev/dlpi"; l->fd = open(cp, O_RDWR); if (l->fd == -1) { snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, "libnet_open_link: %s: %s", cp, strerror(errno)); goto bad; } /* * Map network interface to /dev/dlpi unit */ ppa = get_dlpi_ppa(l->fd, dname, ppa, l->err_buf); if (ppa < 0) { goto bad; } #else /* * Try device without unit number */ strcpy(dname2, dname); cp = strchr(dname, *cp); *cp = '\0'; l->fd = open(dname, O_RDWR); if (l->fd == -1) { if (errno != ENOENT) { snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, "libnet_open_link: %s: %s", dname, strerror(errno)); goto bad; } /* * Try again with unit number */ l->fd = open(dname2, O_RDWR); if (l->fd == -1) { snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, "libnet_open_link: %s: %s", dname2, strerror(errno)); goto bad; } cp = dname2; while (*cp && !isdigit((int)*cp)) cp++; if (*cp) ppa = atoi(cp); else /* * XXX Assume unit zero */ ppa = 0; } #endif /* * Attach if "style 2" provider */ if (dlinforeq(l->fd, l->err_buf) < 0 || dlinfoack(l->fd, (char *)buf, l->err_buf) < 0) { goto bad; } infop = &((union DL_primitives *)buf)->info_ack; if (infop->dl_provider_style == DL_STYLE2 && (dlattachreq(l->fd, ppa, l->err_buf) < 0 || dlokack(l->fd, "attach", (char *)buf, l->err_buf) < 0)) { goto bad; } /* * Bind HP-UX 9 and HP-UX 10.20 */ #if defined(HAVE_HPUX9) || defined(HAVE_HPUX10_20) || defined(HAVE_HPUX11) || defined(HAVE_SOLARIS) if (dlbindreq(l->fd, 0, l->err_buf) < 0 || dlbindack(l->fd, (char *)buf, l->err_buf) < 0) { goto bad; } #endif /* * Determine link type */ if (dlinforeq(l->fd, l->err_buf) < 0 || dlinfoack(l->fd, (char *)buf, l->err_buf) < 0) { goto bad; } infop = &((union DL_primitives *)buf)->info_ack; switch (infop->dl_mac_type) { case DL_CSMACD: case DL_ETHER: l->link_type = DLT_EN10MB; l->link_offset = 0xe; break; case DL_FDDI: l->link_type = DLT_FDDI; l->link_offset = 0x15; break; default: sprintf(l->err_buf, "libnet_open_link: unknown mac type 0x%lu", (unsigned long) infop->dl_mac_type); goto bad; } #ifdef DLIOCRAW /* * This is a non standard SunOS hack to get the ethernet header. */ if (strioctl(l->fd, DLIOCRAW, 0, NULL) < 0) { sprintf(l->err_buf, "libnet_open_link: DLIOCRAW: %s", strerror(errno)); goto bad; } #endif return (1); bad: if (l->fd > 0) { close(l->fd); /* this can fail ok */ } return (-1); }
/* * Dispatch a message from the tunnel driver. It could be an actual * PPPoE message or just an event notification. */ static void handle_input(uint32_t *ctrlbuf, int ctrllen, uint32_t *databuf, int datalen) { poep_t *poep = (poep_t *)databuf; union ppptun_name ptn; int retv; struct strbuf ctrl; struct strbuf data; void *srvp; boolean_t launch; struct ppptun_control *ptc; if (ctrllen != sizeof (*ptc)) { logdbg("bogus %d byte control message from driver", ctrllen); return; } ptc = (struct ppptun_control *)ctrlbuf; /* Switch out on event notifications. */ switch (ptc->ptc_action) { case PTCA_TEST: logdbg("test reply for discriminator %X", ptc->ptc_discrim); return; case PTCA_CONTROL: break; case PTCA_DISCONNECT: logdbg("session %d disconnected on %s; send PADT", ptc->ptc_rsessid, ptc->ptc_name); poep = poe_mkheader(pkt_output, POECODE_PADT, ptc->ptc_rsessid); ptc->ptc_action = PTCA_CONTROL; ctrl.len = sizeof (*ptc); ctrl.buf = (caddr_t)ptc; data.len = poe_length(poep) + sizeof (*poep); data.buf = (caddr_t)poep; if (putmsg(tunfd, &ctrl, &data, 0) < 0) { logerr("putmsg PADT: %s", mystrerror(errno)); } else { output_packets++; } return; case PTCA_UNPLUMB: logdbg("%s unplumbed", ptc->ptc_name); return; default: logdbg("unexpected code %d from driver", ptc->ptc_action); return; } /* Only PPPoE control messages get here. */ input_packets++; if (datalen < sizeof (*poep)) { logdbg("incomplete PPPoE message from %s/%s", ehost(&ptc->ptc_address), ptc->ptc_name); return; } /* Server handles only PADI and PADR; all others are ignored. */ if (poep->poep_code == POECODE_PADI) { padi_packets++; } else if (poep->poep_code == POECODE_PADR) { padr_packets++; } else { loginfo("unexpected %s from %s", poe_codename(poep->poep_code), ehost(&ptc->ptc_address)); return; } logdbg("Recv from %s/%s: %s", ehost(&ptc->ptc_address), ptc->ptc_name, poe_codename(poep->poep_code)); /* Parse out service and formulate template reply. */ retv = locate_service(poep, datalen, ptc->ptc_name, &ptc->ptc_address, pkt_output, &srvp); /* Continue formulating reply */ launch = B_FALSE; if (retv != 1) { /* Ignore initiation if we don't offer a service. */ if (retv <= 0 && poep->poep_code == POECODE_PADI) { logdbg("no services; no reply"); return; } if (retv == 0) (void) poe_add_str((poep_t *)pkt_output, POETT_NAMERR, "No such service."); } else { /* Exactly one service chosen; if it's PADR, then we start. */ if (poep->poep_code == POECODE_PADR) { launch = B_TRUE; } } poep = (poep_t *)pkt_output; /* Select control interface for output. */ (void) strncpy(ptn.ptn_name, ptc->ptc_name, sizeof (ptn.ptn_name)); if (strioctl(tunfd, PPPTUN_SCTL, &ptn, sizeof (ptn), 0) < 0) { logerr("PPPTUN_SCTL %s: %s", ptn.ptn_name, mystrerror(errno)); return; } /* Launch the PPP service */ if (launch && launch_service(tunfd, poep, srvp, ptc)) sessions_started++; /* Send the reply. */ ctrl.len = sizeof (*ptc); ctrl.buf = (caddr_t)ptc; data.len = poe_length(poep) + sizeof (*poep); data.buf = (caddr_t)poep; if (putmsg(tunfd, &ctrl, &data, 0) < 0) { logerr("putmsg %s: %s", ptc->ptc_name, mystrerror(errno)); } else { output_packets++; logdbg("Send to %s/%s: %s", ehost(&ptc->ptc_address), ptc->ptc_name, poe_codename(poep->poep_code)); } }