int main(int argc,char *argv[]) { if(argc < 3) return usage(argc, argv); // set and check device name char* tmp=argv[1]; if(strcmp(tmp,"1") && strcmp(tmp,"2")) return usage(argc, argv); char devname[12]; sprintf(devname, "/dev/schar%s",tmp); printf("devname %s\n",devname); int commandnum = atoi( argv[2] ); printf("commandnum=%d\n",commandnum); int adrnum = 0; if (argc > 3){ adrnum = atoi( argv[3] ); printf("adrnum=%02x\n",adrnum); } // open device eth_open(devname); eth_register_mac(); int e=write_command(commandnum,adrnum); // close device eth_close(); return e; }
int main() { bold("This sends and empty payload, length should be 64\n"); printf("Opening eth0\n"); eth_iface_t * iface = eth_open( "eth0" ); printf("Sending empty payload (1 byte) to 00:11:22:33:44:55\n"); mac_addr_t dst; mac_str_addr("00:11:22:33:44:55", dst); uint8_t byte = 1; printf("With type IPV4_OVER_ETH_TYPE\n"); int bytes_sent = eth_send( iface, dst, 0x0800, &byte, 1 ); printf(BOLDCYAN "bytes_sent = %d\n" RESET, bytes_sent); bold("Now we try to send 128 bits, for exmple: \n"); printf(" Now we try 128 bytes to 00:11:22:33:44:55\n"); int i; unsigned char word[128]; for( i=0; i < 128; i++ ) word[i] = i; bytes_sent = eth_send( iface, dst, 0x0800, word, 128 ); printf(BOLDCYAN "bytes_sent = %d\n" RESET, bytes_sent); return 0; }
void open_devices(void) { i = intf_open(); if ( i == NULL ) { perror("intf open error"); exit(-1); } strncpy(ie.intf_name, iface, 60); if ( intf_get(i, &ie) == -1 ) { perror("intf get error"); exit(-1); } mha = ie.intf_link_addr; if ( addr_ntop(&mha, mhw, 32) == NULL ){ exit(-1); } mad = ie.intf_addr; if ( addr_ntop(&mad, mip, 32) == NULL ){ exit(-1); } rmslash(mip); e = eth_open(iface); if ( e == NULL ) { perror("eth open error"); exit(-1); } p = pcap_open_live(iface, 20000, 1, 500, ebuf); if ( p == NULL ) { perror(ebuf); exit(-1); } }
int main(int argc, char **argv, char **env) { float speed; #ifndef WITH_SERIAL_PTY set_conio_terminal_mode(); #endif Verilated::commandArgs(argc, argv); dut = new Vdut; Verilated::traceEverOn(true); tfp = new VerilatedVcdC; dut->trace(tfp, 99); tfp->open("dut.vcd"); struct sim s; sim_init(&s); #ifdef WITH_SERIAL_PTY console_init(&s); console_open(&s); #endif #ifdef WITH_ETH eth_init(&s, "/dev/net/tap0", "tap0"); // XXX get this from /tmp/simethernet eth_open(&s); #endif s.run = true; while(s.run) { sim_tick(&s); if(SYS_CLK) { #ifdef WITH_SERIAL if(console_service(&s) != 0) s.run = false; #endif #ifdef WITH_ETH ethernet_service(&s); #endif } } s.end = clock(); speed = (s.tick/2)/((s.end-s.start)/CLOCKS_PER_SEC); printf("average speed: %3.3f MHz\n\r", speed/1000000); tfp->close(); #ifdef WITH_SERIAL_PTY console_close(&s); #endif #ifdef WITH_ETH eth_close(&s); #endif exit(0); }
int _intf_get_noalias(intf_t *intf, struct intf_entry *entry) { struct lifreq lifr; /* Get interface index. */ entry->intf_index = if_nametoindex(entry->intf_name); if (entry->intf_index == 0) return (-1); strlcpy(lifr.lifr_name, entry->intf_name, sizeof(lifr.lifr_name)); /* Get interface flags. */ if (ioctl(intf->fd, SIOCGLIFFLAGS, &lifr) < 0) return (-1); entry->intf_flags = intf_iff_to_flags(lifr.lifr_flags); _intf_set_type(entry); /* Get interface MTU. */ #ifdef SIOCSIFMTU if (ioctl(intf->fd, SIOCGLIFMTU, &lifr) < 0) #endif return (-1); entry->intf_mtu = lifr.lifr_mtu; entry->intf_addr.addr_type = entry->intf_dst_addr.addr_type = entry->intf_link_addr.addr_type = ADDR_TYPE_NONE; /* Get primary interface address. */ if (ioctl(intf->fd, SIOCGLIFADDR, &lifr) == 0) { addr_ston((struct sockaddr *)&lifr.lifr_addr, &entry->intf_addr); if (ioctl(intf->fd, SIOCGLIFNETMASK, &lifr) < 0) return (-1); addr_stob((struct sockaddr *)&lifr.lifr_addr, &entry->intf_addr.addr_bits); } /* Get other addresses. */ if (entry->intf_type == INTF_TYPE_TUN) { if (ioctl(intf->fd, SIOCGLIFDSTADDR, &lifr) == 0) { if (addr_ston((struct sockaddr *)&lifr.lifr_addr, &entry->intf_dst_addr) < 0) return (-1); } } else if (entry->intf_type == INTF_TYPE_ETH) { eth_t *eth; if ((eth = eth_open(entry->intf_name)) != NULL) { if (!eth_get(eth, &entry->intf_link_addr.addr_eth)) { entry->intf_link_addr.addr_type = ADDR_TYPE_ETH; entry->intf_link_addr.addr_bits = ETH_ADDR_BITS; } eth_close(eth); } } return (0); }
static void arpd_init(char *dev, int naddresses, char **addresses) { struct bpf_program fcode; char filter[1024], ebuf[PCAP_ERRBUF_SIZE], *dst; intf_t *intf; dst = arpd_expandips(naddresses, addresses); if ((arpd_arp = arp_open()) == NULL) err(1, "arp_open"); if ((intf = intf_open()) == NULL) err(1, "intf_open"); if (dev == NULL) { if ((dev = pcap_lookupdev(ebuf)) == NULL) errx(1, "pcap_lookupdev: %s", ebuf); } arpd_ifent.intf_len = sizeof(arpd_ifent); strncpy(arpd_ifent.intf_name, dev, sizeof(arpd_ifent.intf_name) - 1); arpd_ifent.intf_name[sizeof(arpd_ifent.intf_name) - 1] = '\0'; if (intf_get(intf, &arpd_ifent) < 0) err(1, "intf_get"); if (arpd_ifent.intf_addr.addr_type != ADDR_TYPE_IP || arpd_ifent.intf_link_addr.addr_type != ADDR_TYPE_ETH) errx(1, "bad interface configuration: not IP or Ethernet"); arpd_ifent.intf_addr.addr_bits = IP_ADDR_BITS; snprintf(filter, sizeof(filter), "arp %s%s%s and not ether src %s", dst ? "and (" : "", dst ? dst : "", dst ? ")" : "", addr_ntoa(&arpd_ifent.intf_link_addr)); if ((arpd_pcap = pcap_open_live(dev, 128, 0, 500, ebuf)) == NULL) errx(1, "pcap_open_live: %s", ebuf); if (pcap_compile(arpd_pcap, &fcode, filter, 1, 0) < 0 || pcap_setfilter(arpd_pcap, &fcode) < 0) errx(1, "bad pcap filter: %s", pcap_geterr(arpd_pcap)); #if defined(BSD) && defined(BIOCIMMEDIATE) { int on = 1; if (ioctl(pcap_fileno(arpd_pcap), BIOCIMMEDIATE, &on) < 0) err(1, "BIOCIMMEDIATE"); } #endif if ((arpd_eth = eth_open(dev)) == NULL) errx(1, "eth_open"); #ifndef LOG_PERROR #define LOG_PERROR 0 #endif openlog("arpd", LOG_PERROR|LOG_PID|LOG_CONS, LOG_DAEMON); syslog(LOG_INFO, "listening on %s: %s", dev, filter); }
void packet_listen(char * in_device, char * out_device, char * filter) { struct bpf_program fp; bpf_u_int32 localnet, netmask; /* net addr holders */ char errorbuf[PCAP_ERRBUF_SIZE]; /* buffer to put error strings in */ fprintf( stderr, "Starting tap from %s to %s using filter:\n\t%s\n", in_device, out_device, filter ); pd = pcap_open_live(in_device, snaplen?snaplen:65535, 1, 500, errorbuf); if(pd == NULL) { fatal("start_sniffing(): interface %s open: %s\n", in_device, errorbuf); } if(pcap_compile(pd, &fp, filter, 1, netmask) < 0) { fatal("start_sniffing() FSM compilation failed: \n\t%s\n" "PCAP command: %s\n", pcap_geterr(pd), filter); } /* set the pcap filter */ if(pcap_setfilter(pd, &fp) < 0) { fatal("start_sniffing() setfilter: \n\t%s\n", pcap_geterr(pd)); } /* get data link type */ datalink = pcap_datalink(pd); if(datalink < 0) { fatal("OpenPcap() datalink grab: \n\t%s\n", pcap_geterr(pd)); } if((eth_retrans = eth_open(out_device)) == NULL) fatal("init_retrans() eth_open failed\n"); /* Read all packets on the device. Continue until cnt packets read */ if(pcap_loop(pd, count, (pcap_handler) packet_retrans, NULL) < 0) { fatal("pcap_loop: %s", pcap_geterr(pd)); shandler( 0 ); } return; }
/** * Inner sendpacket_open() method for using libdnet */ static sendpacket_t * sendpacket_open_libdnet(const char *device, char *errbuf) { eth_t *ldnet; sendpacket_t *sp; assert(device); assert(errbuf); dbg(1, "sendpacket: using Libdnet"); if ((ldnet = eth_open(device)) == NULL) return NULL; sp = (sendpacket_t *)safe_malloc(sizeof(sendpacket_t)); strlcpy(sp->device, device, sizeof(sp->device)); sp->handle.ldnet = ldnet; sp->handle_type = SP_TYPE_LIBDNET; return sp; }
int main(int argc,char *argv[]) { int n; char *tmp; char devname[12]; // check arguments if(argc != 2) return usage(argc, argv); // set and check device name tmp=argv[1]; if(strcmp(tmp,"1") && strcmp(tmp,"2")) return usage(argc, argv); sprintf(devname, "/dev/schar%s",tmp); printf("devname %s\n",devname); // open device eth_open(devname); eth_register_mac(); while(1){ // never ending loop, Ctrl-C to stop // receive data n=eth_read(0); if (n<=6) continue; printf(" nread %d \n",n); printf(" nrdat %d \n",nrdat); printf("Dump: \""); dumphex(n,rpkt); printf("\"\n"); printf("Use Ctrl-C to stop.\n"); } // close device eth_close(); return 0; }
static int Active_Open (const char* dev) { if ( dev && strcasecmp(dev, "ip") ) { s_link = eth_open(dev); if ( !s_link ) FatalError("%s: can't open %s!\n", "Active response", dev); s_send = Active_SendEth; } else { s_ipnet = ip_open(); if ( !s_ipnet ) FatalError("%s: can't open ip!\n", "Active response"); s_send = Active_SendIp; } return ( s_link || s_ipnet ) ? 0 : -1; }
/** * Respond2 initialization function * * @param data argument passed to the resp keyword * @param otn pointer to an OptTreeNode structure * @param protocol Snort rule protocol (IP/TCP/UDP) * * @return void function */ static void Respond2Init(char *data, OptTreeNode *otn, int protocol) { static int setup = 0; RespondData *rd = NULL; if (!(protocol & (IPPROTO_ICMP | IPPROTO_TCP | IPPROTO_UDP))) FatalError("%s: %s(%d): Can't respond to IP protocol rules.\n", MODNAME, file_name, file_line); rd = (RespondData *)SnortAlloc(sizeof(RespondData)); if (!setup) { SetLinkInfo(); /* setup link-layer pointer arithmetic info */ SetRespAttempts(&config); /* configure # of TCP attempts */ SetRespCacheRows(&config); /* configure # of rows in cache */ SetRespCacheMemcap(&config); /* configure response cache memcap */ if ((respcache_init(&respcache, &config)) != 0) FatalError("%s: Unable to allocate hash table memory.\n", MODNAME); /* Open raw socket or network device before Snort drops privileges */ if (link_offset) { if (config.ethdev == NULL) /* open link-layer device */ { if ((config.ethdev = eth_open(pv.respond2_ethdev)) == NULL) FatalError("%s: Unable to open link-layer device: %s.\n", MODNAME, pv.respond2_ethdev); } DEBUG_WRAP( DebugMessage(DEBUG_PLUGIN, "%s: using link-layer " "injection on interface %s\n", MODNAME, pv.respond2_ethdev); DebugMessage(DEBUG_PLUGIN, "%s: link_offset = %d\n", MODNAME, link_offset); ); }
static int l2_packet_init_libdnet(struct l2_packet_data *l2) { eth_addr_t own_addr; l2->eth = eth_open(l2->ifname); if (!l2->eth) { printf("Failed to open interface '%s'.\n", l2->ifname); perror("eth_open"); return -1; } if (eth_get(l2->eth, &own_addr) < 0) { printf("Failed to get own hw address from interface '%s'.\n", l2->ifname); perror("eth_get"); eth_close(l2->eth); l2->eth = NULL; return -1; } os_memcpy(l2->own_addr, own_addr.data, ETH_ALEN); return 0; }
static int l2_packet_init_libdnet(struct l2_packet_data *l2) { eth_addr_t own_addr; l2->eth = eth_open(l2->ifname); if (!l2->eth) { wpa_printf(MSG_ERROR, "Failed to open interface '%s' - eth_open: %s", l2->ifname, strerror(errno)); return -1; } if (eth_get(l2->eth, &own_addr) < 0) { wpa_printf(MSG_ERROR, "Failed to get own hw address from interface '%s' - eth_get: %s", l2->ifname, strerror(errno)); eth_close(l2->eth); l2->eth = NULL; return -1; } os_memcpy(l2->own_addr, own_addr.data, ETH_ALEN); return 0; }
target_context_t *target_open(const char *port, const char *netif) { target_context_t *tc; assert(port); tc = zmalloc(sizeof (target_context_t)); #ifndef WIN32 tc->portfd = -1; #else tc->portfd = INVALID_HANDLE_VALUE; #endif tc->sockfd = -1; tc->medium = TM_SERIAL; tc->mtu = SERIAL_MTU; tc->write = target_write_serial; tc->portfd = open_port(port); if(tc->portfd < 0) goto failure; #if defined(WIN32) if(tc->portfd == INVALID_HANDLE_VALUE) goto failure; #endif #ifndef WIN32 if (netif && (eth_open(tc, netif) < 0)) goto failure; #endif return tc; failure: target_close(tc); return NULL; }
/*********************************************************** * open_socket() * what: opens an ethernet socket * params: * none * returns: * socket - a socket descriptor **********************************************************/ int open_socket_C(const char *dev) { #ifdef _LINUX_ // Open socket for WRITE socketw = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL)); if (socketw == -1) { perror("socket():"); exit(1); } // retrieve ethernet interface index strncpy(ifr.ifr_name, dev, IFNAMSIZ); if (ioctl(socketw, SIOCGIFINDEX, &ifr) == -1) { perror(dev); exit(1); } ifindex = ifr.ifr_ifindex; // retrieve corresponding MAC if (ioctl(socketw, SIOCGIFHWADDR, &ifr) == -1) { perror("GET_HWADDR"); exit(1); } if (strcmp(dev,"lo") != 0) { int i; for (i=0; i<ETH_ALEN; i++) host_mac[i] = ifr.ifr_hwaddr.sa_data[i]; } // prepare sockaddr_ll socket_address.sll_family = AF_PACKET; socket_address.sll_protocol = htons(ETH_P_ALL); socket_address.sll_ifindex = ifindex; socket_address.sll_hatype = 0;//ARPHRD_ETHER; socket_address.sll_pkttype = 0;//PACKET_OTHERHOST; socket_address.sll_halen = ETH_ALEN; socket_address.sll_addr[0] = neuflow_mac[0]; socket_address.sll_addr[1] = neuflow_mac[1]; socket_address.sll_addr[2] = neuflow_mac[2]; socket_address.sll_addr[3] = neuflow_mac[3]; socket_address.sll_addr[4] = neuflow_mac[4]; socket_address.sll_addr[5] = neuflow_mac[5]; socket_address.sll_addr[6] = 0x00; socket_address.sll_addr[7] = 0x00; // size of socket socklen = sizeof(socket_address); #else dev = "en0"; socketw = eth_open(dev); if (socketw == NULL){ perror("socketw():"); printf("Couldn't open device: %s\n", dev); exit(1); } #endif // open socket for RD (pcap), with a filter char errbuf[20]; int wait_time = 0; #ifdef _LINUX_ wait_time = -1; #else wait_time = 1000; #endif socketr = pcap_open_live(dev, 3000, 1, wait_time, errbuf); if (socketr == NULL) { fprintf(stderr, "Couldn't open device %s: %s\n", dev, errbuf); exit(2); } // set up a filter char filter_exp[] = "ether src 01:02:03:04:05:06"; struct bpf_program fp; if (pcap_compile(socketr, &fp, filter_exp, 0, 0) == -1) { fprintf(stderr, "Couldn't parse filter %s: %s\n", filter_exp, pcap_geterr(socketr)); exit(2); } if (pcap_setfilter(socketr, &fp) == -1) { fprintf(stderr, "Couldn't install filter %s: %s\n", filter_exp, pcap_geterr(socketr)); exit(2); } // Message printf("# libetherflow: using dev %s\n", dev); #ifdef _LINUX_ // set buffer sizes int sockbufsize = 64*1024*1024; unsigned int size = sizeof(int); int realbufsize = 0; int set_res = setsockopt(socketw, SOL_SOCKET, SO_RCVBUFFORCE, (int *)&sockbufsize, sizeof(int)); int get_res = getsockopt(socketw, SOL_SOCKET, SO_RCVBUF, &realbufsize, &size); if ((set_res < 0)||(get_res < 0)) { perror("set/get sockopt"); close(socketw); exit(1); } printf("# libetherflow: set rx buffer size to %dMB\n", realbufsize/(1024*1024)); set_res = setsockopt(socketw, SOL_SOCKET, SO_SNDBUFFORCE, (int *)&sockbufsize, sizeof(int)); get_res = getsockopt(socketw, SOL_SOCKET, SO_SNDBUF, &realbufsize, &size); if ((set_res < 0)||(get_res < 0)) { perror("set/get sockopt"); close(socketw); exit(1); } printf("# libetherflow: set tx buffer size to %dMB\n", realbufsize/(1024*1024)); #endif return 0; }
static int _intf_get_noalias(intf_t *intf, struct intf_entry *entry) { struct ifreq ifr; strlcpy(ifr.ifr_name, entry->intf_name, sizeof(ifr.ifr_name)); /* Get interface flags. */ if (ioctl(intf->fd, SIOCGIFFLAGS, &ifr) < 0) return (-1); entry->intf_flags = intf_iff_to_flags(ifr.ifr_flags); _intf_set_type(entry); /* Get interface MTU. */ #ifdef SIOCGIFMTU if (ioctl(intf->fd, SIOCGIFMTU, &ifr) < 0) #endif return (-1); entry->intf_mtu = ifr.ifr_mtu; entry->intf_addr.addr_type = entry->intf_dst_addr.addr_type = entry->intf_link_addr.addr_type = ADDR_TYPE_NONE; /* Get primary interface address. */ if (ioctl(intf->fd, SIOCGIFADDR, &ifr) == 0) { addr_ston(&ifr.ifr_addr, &entry->intf_addr); if (ioctl(intf->fd, SIOCGIFNETMASK, &ifr) < 0) return (-1); addr_stob(&ifr.ifr_addr, &entry->intf_addr.addr_bits); } /* Get other addresses. */ if (entry->intf_type == INTF_TYPE_TUN) { if (ioctl(intf->fd, SIOCGIFDSTADDR, &ifr) == 0) { if (addr_ston(&ifr.ifr_addr, &entry->intf_dst_addr) < 0) return (-1); } } else if (entry->intf_type == INTF_TYPE_ETH) { #if defined(SIOCGIFHWADDR) if (ioctl(intf->fd, SIOCGIFHWADDR, &ifr) < 0) return (-1); if (addr_ston(&ifr.ifr_addr, &entry->intf_link_addr) < 0) return (-1); #elif defined(SIOCRPHYSADDR) /* Tru64 */ struct ifdevea *ifd = (struct ifdevea *)𝔦 /* XXX */ if (ioctl(intf->fd, SIOCRPHYSADDR, ifd) < 0) return (-1); addr_pack(&entry->intf_link_addr, ADDR_TYPE_ETH, ETH_ADDR_BITS, ifd->current_pa, ETH_ADDR_LEN); #else eth_t *eth; if ((eth = eth_open(entry->intf_name)) != NULL) { if (!eth_get(eth, &entry->intf_link_addr.addr_eth)) { entry->intf_link_addr.addr_type = ADDR_TYPE_ETH; entry->intf_link_addr.addr_bits = ETH_ADDR_BITS; } eth_close(eth); } #endif } return (0); }
int intf_set(intf_t *intf, const struct intf_entry *entry) { struct ifreq ifr; struct intf_entry *orig; struct addr bcast; u_char buf[BUFSIZ]; orig = (struct intf_entry *)buf; orig->intf_len = sizeof(buf); strcpy(orig->intf_name, entry->intf_name); if (intf_get(intf, orig) < 0) return (-1); /* Delete any existing aliases. */ if (_intf_delete_aliases(intf, orig) < 0) return (-1); /* Delete any existing addrs. */ if (_intf_delete_addrs(intf, orig) < 0) return (-1); memset(&ifr, 0, sizeof(ifr)); strlcpy(ifr.ifr_name, entry->intf_name, sizeof(ifr.ifr_name)); /* Set interface MTU. */ if (entry->intf_mtu != 0) { ifr.ifr_mtu = entry->intf_mtu; #ifdef SIOCSIFMTU if (ioctl(intf->fd, SIOCSIFMTU, &ifr) < 0) #endif return (-1); } /* Set interface address. */ if (entry->intf_addr.addr_type == ADDR_TYPE_IP) { #ifdef BSD /* XXX - why must this happen before SIOCSIFADDR? */ if (addr_btos(entry->intf_addr.addr_bits, &ifr.ifr_addr) == 0) { if (ioctl(intf->fd, SIOCSIFNETMASK, &ifr) < 0) return (-1); } #endif if (addr_ntos(&entry->intf_addr, &ifr.ifr_addr) < 0) return (-1); if (ioctl(intf->fd, SIOCSIFADDR, &ifr) < 0 && errno != EEXIST) return (-1); if (addr_btos(entry->intf_addr.addr_bits, &ifr.ifr_addr) == 0 #ifdef __linux__ && entry->intf_addr.addr_ip != 0 #endif ) { if (ioctl(intf->fd, SIOCSIFNETMASK, &ifr) < 0) return (-1); } if (addr_bcast(&entry->intf_addr, &bcast) == 0) { if (addr_ntos(&bcast, &ifr.ifr_broadaddr) == 0) { /* XXX - ignore error from non-broadcast ifs */ ioctl(intf->fd, SIOCSIFBRDADDR, &ifr); } } } /* Set link-level address. */ if (entry->intf_link_addr.addr_type == ADDR_TYPE_ETH && addr_cmp(&entry->intf_link_addr, &orig->intf_link_addr) != 0) { #if defined(SIOCSIFHWADDR) if (addr_ntos(&entry->intf_link_addr, &ifr.ifr_hwaddr) < 0) return (-1); if (ioctl(intf->fd, SIOCSIFHWADDR, &ifr) < 0) return (-1); #elif defined (SIOCSIFLLADDR) memcpy(ifr.ifr_addr.sa_data, &entry->intf_link_addr.addr_eth, ETH_ADDR_LEN); ifr.ifr_addr.sa_len = ETH_ADDR_LEN; if (ioctl(intf->fd, SIOCSIFLLADDR, &ifr) < 0) return (-1); #else eth_t *eth; if ((eth = eth_open(entry->intf_name)) == NULL) return (-1); if (eth_set(eth, &entry->intf_link_addr.addr_eth) < 0) { eth_close(eth); return (-1); } eth_close(eth); #endif } /* Set point-to-point destination. */ if (entry->intf_dst_addr.addr_type == ADDR_TYPE_IP) { if (addr_ntos(&entry->intf_dst_addr, &ifr.ifr_dstaddr) < 0) return (-1); if (ioctl(intf->fd, SIOCSIFDSTADDR, &ifr) < 0 && errno != EEXIST) return (-1); } /* Add aliases. */ if (_intf_add_aliases(intf, entry) < 0) return (-1); /* Set interface flags. */ if (ioctl(intf->fd, SIOCGIFFLAGS, &ifr) < 0) return (-1); ifr.ifr_flags = intf_flags_to_iff(entry->intf_flags, ifr.ifr_flags); if (ioctl(intf->fd, SIOCSIFFLAGS, &ifr) < 0) return (-1); return (0); }
static int ipq_daq_initialize ( const DAQ_Config_t* cfg, void** handle, char* errBuf, size_t errMax) { int status; if(cfg->name && *(cfg->name)) { snprintf(errBuf, errMax, "The ipq DAQ module does not support interface or readback mode!"); return DAQ_ERROR_INVAL; } IpqImpl* impl = calloc(1, sizeof(*impl)); if ( !impl ) { snprintf(errBuf, errMax, "%s: failed to allocate the ipq context!", __FUNCTION__); return DAQ_ERROR_NOMEM; } if ( ipq_daq_get_setup(impl, cfg, errBuf, errMax) != DAQ_SUCCESS ) { ipq_daq_shutdown(impl); return DAQ_ERROR; } impl->buf = malloc(MSG_BUF_SIZE); if ( !impl->buf ) { snprintf(errBuf, errMax, "%s: failed to allocate the ipq buffer!", __FUNCTION__); ipq_daq_shutdown(impl); return DAQ_ERROR_NOMEM; } // remember to also link in, eg: // iptables -A OUTPUT -p icmp -j QUEUE impl->ipqh = ipq_create_handle(0, impl->proto); if ( !impl->ipqh ) { snprintf(errBuf, errMax, "%s: ipq_create_handle error %s\n", __FUNCTION__, ipq_errstr()); ipq_daq_shutdown(impl); return DAQ_ERROR; } // copy both packet metadata and packet payload // paket payload is limited to IP_MAXPACKET status = ipq_set_mode(impl->ipqh, IPQ_COPY_PACKET, IP_MAXPACKET); if ( status < 0 ) { snprintf(errBuf, errMax, "%s: ipq_set_mode error %s\n", __FUNCTION__, ipq_errstr()); ipq_daq_shutdown(impl); return DAQ_ERROR; } if ( impl->device && strcasecmp(impl->device, "ip") ) { impl->link = eth_open(impl->device); if ( !impl->link ) { snprintf(errBuf, errMax, "%s: can't open %s!\n", __FUNCTION__, impl->device); ipq_daq_shutdown(impl); return DAQ_ERROR; } } else { impl->net = ip_open(); if ( !impl->net ) { snprintf(errBuf, errMax, "%s: can't open ip!\n", __FUNCTION__); ipq_daq_shutdown(impl); return DAQ_ERROR; } } impl->state = DAQ_STATE_INITIALIZED; *handle = impl; return DAQ_SUCCESS; }
static void fragroute_init(const char *dst, const char *outfile) { struct arp_entry arpent; struct intf_entry ifent; struct route_entry rtent; #ifdef WIN32 WSADATA wsdata; if (WSAStartup(MAKEWORD(2, 2), &wsdata) != 0) err(1, "couldn't initialize Winsock"); SetConsoleCtrlHandler(fragroute_signal, TRUE); #else signal(SIGINT, fragroute_signal); signal(SIGTERM, fragroute_signal); #endif if (addr_aton(dst, &ctx.dst) < 0) err(1, "destination address invalid"); if (ctx.dst.addr_bits != IP_ADDR_BITS) errx(1, "only /32 destinations supported at this time"); pkt_init(128); event_init(); // event_sigcb = fragroute_close; if ((ctx.arp = arp_open()) == NULL || (ctx.intf = intf_open()) == NULL || (ctx.route = route_open()) == NULL) err(1, "couldn't open kernel networking interfaces"); /* Find outgoing interface, addresses, and MTU. */ ifent.intf_len = sizeof(ifent); if (intf_get_dst(ctx.intf, &ifent, &ctx.dst) < 0) err(1, "couldn't determine outgoing interface"); memcpy(&ctx.src, &ifent.intf_addr, sizeof(ctx.src)); ctx.src.addr_bits = IP_ADDR_BITS; memcpy(&ctx.smac, &ifent.intf_link_addr, sizeof(ctx.smac)); ctx.mtu = ifent.intf_mtu; /* Open outgoing interface for sending. */ if ((ctx.eth = eth_open(ifent.intf_name)) == NULL) err(1, "couldn't open %s for sending", ifent.intf_name); /* Find destination MAC address. */ memcpy(&arpent.arp_pa, &ctx.dst, sizeof(arpent.arp_pa)); if (arp_get(ctx.arp, &arpent) < 0) { memcpy(&rtent.route_dst, &ctx.dst, sizeof(rtent.route_dst)); if (route_get(ctx.route, &rtent) < 0) err(1, "no route to %s", addr_ntoa(&rtent.route_dst)); memcpy(&arpent.arp_pa, &rtent.route_gw, sizeof(arpent.arp_pa)); if (arp_get(ctx.arp, &arpent) < 0) err(1, "no ARP entry for %s", addr_ntoa(&arpent.arp_pa)); } memcpy(&ctx.dmac, &arpent.arp_ha, sizeof(ctx.dmac)); /* Setup our tunnel. */ if ((ctx.tun = tun_open(&ctx.src, &ctx.dst, ctx.mtu)) == NULL) err(1, "couldn't initialize tunnel interface"); ctx.dfile = NULL; if (outfile && ((ctx.dfile = pcap_dump_open(ctx.tun->pcap, outfile)) == NULL)) err(1, "couldn't open pcap dump file"); tun_register(ctx.tun, fragroute_process, &ctx); }
static int nfq_daq_initialize ( const DAQ_Config_t* cfg, void** handle, char* errBuf, size_t errMax) { if(cfg->name && *(cfg->name)) { snprintf(errBuf, errMax, "The nfq DAQ module does not support interface or readback mode!"); return DAQ_ERROR_INVAL; } // setup internal stuff NfqImpl *impl = calloc(1, sizeof(*impl)); if ( !impl ) { snprintf(errBuf, errMax, "%s: failed to allocate nfq context\n", __FUNCTION__); return DAQ_ERROR_NOMEM; } if ( nfq_daq_get_setup(impl, cfg, errBuf, errMax) != DAQ_SUCCESS ) { nfq_daq_shutdown(impl); return DAQ_ERROR; } if ( (impl->buf = malloc(MSG_BUF_SIZE)) == NULL ) { snprintf(errBuf, errMax, "%s: failed to allocate nfq buffer\n", __FUNCTION__); nfq_daq_shutdown(impl); return DAQ_ERROR_NOMEM; } // setup input stuff // 1. get a new q handle if ( !(impl->nf_handle = nfq_open()) ) { snprintf(errBuf, errMax, "%s: failed to get handle for nfq\n", __FUNCTION__); nfq_daq_shutdown(impl); return DAQ_ERROR; } // 2. now use the new q handle to rip the rug out from other // nfq users / handles? actually that doesn't seem to // happen which is good, but then why is this *supposed* // to be necessary? especially since we haven't bound to // a qid yet, and that is exclusive anyway. if ( (IP4(impl) && nfq_unbind_pf(impl->nf_handle, PF_INET) < 0) || (IP6(impl) && nfq_unbind_pf(impl->nf_handle, PF_INET6) < 0) ) { snprintf(errBuf, errMax, "%s: failed to unbind protocols for nfq\n", __FUNCTION__); //nfq_daq_shutdown(impl); //return DAQ_ERROR; } // 3. select protocols for the q handle // this is necessary but insufficient because we still // must configure iptables externally, eg: // // iptables -A OUTPUT -p icmp -j NFQUEUE [--queue-num <#>] // (# defaults to 0). // // :( iptables rules should be managed automatically to avoid // queueing packets to nowhere or waiting for packets that // will never come. (ie this bind should take the -p, -s, // etc args you can pass to iptables and create the dang // rule!) if ( (IP4(impl) && nfq_bind_pf(impl->nf_handle, PF_INET) < 0) || (IP6(impl) && nfq_bind_pf(impl->nf_handle, PF_INET6) < 0) ) { snprintf(errBuf, errMax, "%s: failed to bind protocols for nfq\n", __FUNCTION__); nfq_daq_shutdown(impl); return DAQ_ERROR; } // 4. bind to/allocate the specified nfqueue instance // (this is the puppy specified via iptables as in // above example.) // // ** there can be at most 1 nf_queue per qid if ( !(impl->nf_queue = nfq_create_queue( impl->nf_handle, impl->qid, daq_nfq_callback, impl)) ) { snprintf(errBuf, errMax, "%s: nf queue creation failed\n", __FUNCTION__); nfq_daq_shutdown(impl); return DAQ_ERROR; } // 5. configure copying for maximum overhead if ( nfq_set_mode(impl->nf_queue, NFQNL_COPY_PACKET, IP_MAXPACKET) < 0 ) { snprintf(errBuf, errMax, "%s: unable to set packet copy mode\n", __FUNCTION__); nfq_daq_shutdown(impl); return DAQ_ERROR; } // 6. set queue length (optional) if ( impl->qlen > 0 && nfq_set_queue_maxlen(impl->nf_queue, impl->qlen)) { snprintf(errBuf, errMax, "%s: unable to set queue length\n", __FUNCTION__); nfq_daq_shutdown(impl); return DAQ_ERROR; } // 7. get the q socket descriptor // (after getting not 1 but 2 handles!) impl->sock = nfq_fd(impl->nf_handle); // setup output stuff // we've got 2 handles and a socket descriptor but, incredibly, // no way to inject? if ( impl->device && strcasecmp(impl->device, "ip") ) { impl->link = eth_open(impl->device); if ( !impl->link ) { snprintf(errBuf, errMax, "%s: can't open %s!\n", __FUNCTION__, impl->device); nfq_daq_shutdown(impl); return DAQ_ERROR; } } else { impl->net = ip_open(); if ( !impl->net ) { snprintf(errBuf, errMax, "%s: can't open ip!\n", __FUNCTION__); nfq_daq_shutdown(impl); return DAQ_ERROR; } } impl->state = DAQ_STATE_INITIALIZED; *handle = impl; return DAQ_SUCCESS; }
static int _intf_get_noalias(intf_t *intf, struct intf_entry *entry) { struct ifreq ifr; #ifdef HAVE_GETKERNINFO int size; struct kinfo_ndd *nddp; void *end; #endif /* Get interface index. */ entry->intf_index = if_nametoindex(entry->intf_name); if (entry->intf_index == 0) return (-1); strlcpy(ifr.ifr_name, entry->intf_name, sizeof(ifr.ifr_name)); /* Get interface flags. */ if (ioctl(intf->fd, SIOCGIFFLAGS, &ifr) < 0) return (-1); entry->intf_flags = intf_iff_to_flags(ifr.ifr_flags); _intf_set_type(entry); /* Get interface MTU. */ #ifdef SIOCGIFMTU if (ioctl(intf->fd, SIOCGIFMTU, &ifr) < 0) #endif return (-1); entry->intf_mtu = ifr.ifr_mtu; entry->intf_addr.addr_type = entry->intf_dst_addr.addr_type = entry->intf_link_addr.addr_type = ADDR_TYPE_NONE; /* Get primary interface address. */ if (ioctl(intf->fd, SIOCGIFADDR, &ifr) == 0) { addr_ston(&ifr.ifr_addr, &entry->intf_addr); if (ioctl(intf->fd, SIOCGIFNETMASK, &ifr) < 0) return (-1); addr_stob(&ifr.ifr_addr, &entry->intf_addr.addr_bits); } /* Get other addresses. */ if (entry->intf_type == INTF_TYPE_TUN) { if (ioctl(intf->fd, SIOCGIFDSTADDR, &ifr) == 0) { if (addr_ston(&ifr.ifr_addr, &entry->intf_dst_addr) < 0) return (-1); } } else if (entry->intf_type == INTF_TYPE_ETH) { #if defined(HAVE_GETKERNINFO) /* AIX also defines SIOCGIFHWADDR, but it fails silently? * This is the method IBM recommends here: * http://www-01.ibm.com/support/knowledgecenter/ssw_aix_53/com.ibm.aix.progcomm/doc/progcomc/skt_sndother_ex.htm%23ssqinc2joyc?lang=en */ /* How many bytes will be returned? */ size = getkerninfo(KINFO_NDD, 0, 0, 0); if (size <= 0) { return -1; } nddp = (struct kinfo_ndd *)malloc(size); if (!nddp) { return -1; } /* Get all Network Device Driver (NDD) info */ if (getkerninfo(KINFO_NDD, nddp, &size, 0) < 0) { free(nddp); return -1; } /* Loop over the returned values until we find a match */ end = (void *)nddp + size; while ((void *)nddp < end) { if (!strcmp(nddp->ndd_alias, entry->intf_name) || !strcmp(nddp->ndd_name, entry->intf_name)) { addr_pack(&entry->intf_link_addr, ADDR_TYPE_ETH, ETH_ADDR_BITS, nddp->ndd_addr, ETH_ADDR_LEN); break; } else nddp++; } free(nddp); #elif defined(SIOCGIFHWADDR) if (ioctl(intf->fd, SIOCGIFHWADDR, &ifr) < 0) return (-1); if (addr_ston(&ifr.ifr_addr, &entry->intf_link_addr) < 0) return (-1); #elif defined(SIOCRPHYSADDR) /* Tru64 */ struct ifdevea *ifd = (struct ifdevea *)𝔦 /* XXX */ if (ioctl(intf->fd, SIOCRPHYSADDR, ifd) < 0) return (-1); addr_pack(&entry->intf_link_addr, ADDR_TYPE_ETH, ETH_ADDR_BITS, ifd->current_pa, ETH_ADDR_LEN); #else eth_t *eth; if ((eth = eth_open(entry->intf_name)) != NULL) { if (!eth_get(eth, &entry->intf_link_addr.addr_eth)) { entry->intf_link_addr.addr_type = ADDR_TYPE_ETH; entry->intf_link_addr.addr_bits = ETH_ADDR_BITS; } eth_close(eth); } #endif } return (0); }
ipv4_iface_t * ipv4_open( ) { eth_iface = eth_open( "eth0" ); ipv4_route_table_read ( FILENAME , ipv4_route_table_aux ); }
int _intf_get_noalias(intf_t *intf, struct intf_entry *entry) { struct lifreq lifr; int fd; /* Get interface index. */ entry->intf_index = if_nametoindex(entry->intf_name); if (entry->intf_index == 0) return (-1); strlcpy(lifr.lifr_name, entry->intf_name, sizeof(lifr.lifr_name)); /* Get interface flags. Here he also check whether we need to use fd or * fd6 in the rest of the function. Using the wrong address family in * the ioctls gives ENXIO on Solaris. */ if (ioctl(intf->fd, SIOCGLIFFLAGS, &lifr) >= 0) fd = intf->fd; else if (intf->fd6 != -1 && ioctl(intf->fd6, SIOCGLIFFLAGS, &lifr) >= 0) fd = intf->fd6; else return (-1); entry->intf_flags = intf_iff_to_flags(lifr.lifr_flags); _intf_set_type(entry); /* Get interface MTU. */ #ifdef SIOCGLIFMTU if (ioctl(fd, SIOCGLIFMTU, &lifr) < 0) #endif return (-1); entry->intf_mtu = lifr.lifr_mtu; entry->intf_addr.addr_type = entry->intf_dst_addr.addr_type = entry->intf_link_addr.addr_type = ADDR_TYPE_NONE; /* Get primary interface address. */ if (ioctl(fd, SIOCGLIFADDR, &lifr) == 0) { addr_ston((struct sockaddr *)&lifr.lifr_addr, &entry->intf_addr); if (ioctl(fd, SIOCGLIFNETMASK, &lifr) < 0) return (-1); addr_stob((struct sockaddr *)&lifr.lifr_addr, &entry->intf_addr.addr_bits); } /* Get other addresses. */ if (entry->intf_type == INTF_TYPE_TUN) { if (ioctl(fd, SIOCGLIFDSTADDR, &lifr) == 0) { if (addr_ston((struct sockaddr *)&lifr.lifr_addr, &entry->intf_dst_addr) < 0) return (-1); } } else if (entry->intf_type == INTF_TYPE_ETH) { eth_t *eth; if ((eth = eth_open(entry->intf_name)) != NULL) { if (!eth_get(eth, &entry->intf_link_addr.addr_eth)) { entry->intf_link_addr.addr_type = ADDR_TYPE_ETH; entry->intf_link_addr.addr_bits = ETH_ADDR_BITS; } eth_close(eth); } } return (0); }