#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <linux/if_packet.h>
#include <netinet/if_ether.h>
#include <netinet/in.h>
#include <errno.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include <signal.h>
#include "cm_feature.h"
char buffer[2048];
static void print_ip(unsigned int ip)
{
unsigned char buf[32];
sprintf(buf, "%d.%d.%d.%d", (ip)&0xFF , (ip>>8)&0xFF, (ip>>16)&0xFF, (ip>>24)&0xFF);
printf("%s ", buf);
}
//===================packet printf================================
void print_pkt(char *buf, int len)
{
struct iphdr *p_iphdr;
struct tcphdr *p_tcphdr;
if( len < 14 + sizeof(struct iphdr))
{
printf("Not ip .\n");
return ;
}
if( *(unsigned short *)(buf + 12) == htons(0x0800) )
{
p_iphdr = (struct iphdr *)(buf + 14);
if( ntohs(p_iphdr->tot_len) + 14 > len)
{
printf("ip header wrong ? fragment ?.\n");
return ;
}
if( p_iphdr->protocol == 6)
{
p_tcphdr = (struct tcphdr *)( ((char *)p_iphdr) + p_iphdr->ihl*4 );
if( ((char *)p_tcphdr) + 20 > buf + len)
{
printf("TCP, bad header ? \n");
return ;
}
printf("> TCP sip:");
print_ip(p_iphdr->saddr);
printf(" dip:");
print_ip(p_iphdr->daddr);
printf(" sp:%u dp:%u \n", ntohs(p_tcphdr->source), ntohs(p_tcphdr->dest) );
}
else if(p_iphdr->protocol == 17 )
{
printf("> UDP sip:");
print_ip(p_iphdr->saddr);
printf(" dip:");
print_ip(p_iphdr->daddr);
printf(" \n");
}
else
{
printf("IP protocol:%d \n", p_iphdr->protocol );
}
}
else
{
printf("packet protocol: %x .\n", *(unsigned short *)(buf + 12) );
return ;
}
}
//====================================================
int set_interface_promisc(int sockfd, char *interface_name)
{
struct ifreq cfgreq;
strncpy( cfgreq.ifr_name, interface_name, IFNAMSIZ);
if( -1 == ioctl( sockfd, SIOCGIFFLAGS, &cfgreq))
{
return -1;
}
cfgreq.ifr_flags |= IFF_PROMISC;
if( -1 == ioctl( sockfd, SIOCSIFFLAGS, &cfgreq))
{
return -1;
}
return 0;
}
int set_interface_promisc2(int sockfd, int dev_id, char *interface_name)
{
struct packet_mreq mr;
memset(&mr, 0, sizeof(mr));
mr.mr_ifindex = dev_id;
mr.mr_type = PACKET_MR_PROMISC;
if (setsockopt(sockfd, SOL_PACKET,
PACKET_ADD_MEMBERSHIP, &mr, sizeof(mr)) == -1)
{
printf("> failed to set to promisc mode, errorno=%x", errno);
return -1;
}
return 0;
}
char ebuf[128];
int iface_get_id(int fd, const char *device, char *ebuf)
{
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
if (ioctl(fd, SIOCGIFINDEX, &ifr) == -1) {
//snprintf(ebuf, PCAP_ERRBUF_SIZE,
// "ioctl: %s", pcap_strerror(errno));
return -1;
}
return ifr.ifr_ifindex;
}
/*
* Bind the socket associated with FD to the given device.
*/
static int
iface_bind(int fd, int ifindex, char *ebuf)
{
struct sockaddr_ll sll;
int err;
socklen_t errlen = sizeof(err);
memset(&sll, 0, sizeof(sll));
sll.sll_family = AF_PACKET;
sll.sll_ifindex = ifindex;
sll.sll_protocol = htons(ETH_P_ALL);
if (bind(fd, (struct sockaddr *) &sll, sizeof(sll)) == -1) {
//snprintf(ebuf, PCAP_ERRBUF_SIZE,
// "bind: %s", pcap_strerror(errno));
return -1;
}
/* Any pending errors, e.g., network is down? */
if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &errlen) == -1) {
//snprintf(ebuf, PCAP_ERRBUF_SIZE,
// "getsockopt: %s", pcap_strerror(errno));
return -2;
}
if (err > 0) {
//snprintf(ebuf, PCAP_ERRBUF_SIZE,
// "bind: %s", pcap_strerror(err));
return -2;
}
return 0;
}
cm_dev_t *handle;
void sigHand_exit(int signo)
{
if( handle != NULL)
cmb_stop_session_feature(handle);
printf("+ exit app. \n");
exit(0);
}
int main()
{
int ret;
int sockfd;
struct ifreq recv_interface;
int nlen;
int i, counter=0;
int dev_id;
struct sigaction act;
//open r2 driver handle..
handle = cm_dev_open_live("/dev/cmb_np");
if(handle == NULL)
{
printf("> Failed to open driver. exit now. ");
return 0;
}
//enable flow capture feature. capture first 20 packets of each flow.
ret = cmb_start_session_feature(handle, 20);
if( ret != CM_SUCCESS)
{
printf("> Failed to enable this feature. exit now. \n");
return 0;
}
sockfd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL) );
if( sockfd == -1)
{
printf("> Failed to open PF_PACKET socket. \n");
return 0;
}
dev_id = iface_get_id(sockfd, "ath0", ebuf);
if( dev_id == -1)
{
return 0;
}
ret = iface_bind(sockfd, dev_id, ebuf);
if( ret < 0)
return 0;
ret = set_interface_promisc2(sockfd, dev_id, "ath0");
if( ret != 0)
{
printf("> failed to set ath0 in promisc mode.\n");
return 0;
}
act.sa_handler = sigHand_exit;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
sigaction(SIGINT, &act, NULL );
while(1)
{
//nlen = recvfrom(sockfd, buffer, sizeof(buffer), MSG_TRUNC, NULL, NULL );
nlen = recvfrom(sockfd, buffer, sizeof(buffer), MSG_TRUNC, NULL, NULL );
if( nlen <= 0)
{
printf("> socket closed. perror=%x \n", nlen);
break;
}
counter++;
/*
printf("packet : %d \n", counter);
for(i=0; (i<nlen) &&( i<14); i++)
{
printf(" %.2x", buffer[i]&0xFF);
}
printf("\n");
*/
print_pkt(buffer, nlen);
if( counter > 2000)
break;
}
cmb_stop_session_feature(handle);
return 0;
}
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <linux/if_packet.h>
#include <netinet/if_ether.h>
#include <netinet/in.h>
#include <errno.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include <signal.h>
#include "cm_feature.h"
char buffer[2048];
static void print_ip(unsigned int ip)
{
unsigned char buf[32];
sprintf(buf, "%d.%d.%d.%d", (ip)&0xFF , (ip>>8)&0xFF, (ip>>16)&0xFF, (ip>>24)&0xFF);
printf("%s ", buf);
}
//===================packet printf================================
void print_pkt(char *buf, int len)
{
struct iphdr *p_iphdr;
struct tcphdr *p_tcphdr;
if( len < 14 + sizeof(struct iphdr))
{
printf("Not ip .\n");
return ;
}
if( *(unsigned short *)(buf + 12) == htons(0x0800) )
{
p_iphdr = (struct iphdr *)(buf + 14);
if( ntohs(p_iphdr->tot_len) + 14 > len)
{
printf("ip header wrong ? fragment ?.\n");
return ;
}
if( p_iphdr->protocol == 6)
{
p_tcphdr = (struct tcphdr *)( ((char *)p_iphdr) + p_iphdr->ihl*4 );
if( ((char *)p_tcphdr) + 20 > buf + len)
{
printf("TCP, bad header ? \n");
return ;
}
printf("> TCP sip:");
print_ip(p_iphdr->saddr);
printf(" dip:");
print_ip(p_iphdr->daddr);
printf(" sp:%u dp:%u \n", ntohs(p_tcphdr->source), ntohs(p_tcphdr->dest) );
}
else if(p_iphdr->protocol == 17 )
{
printf("> UDP sip:");
print_ip(p_iphdr->saddr);
printf(" dip:");
print_ip(p_iphdr->daddr);
printf(" \n");
}
else
{
printf("IP protocol:%d \n", p_iphdr->protocol );
}
}
else
{
printf("packet protocol: %x .\n", *(unsigned short *)(buf + 12) );
return ;
}
}
//====================================================
int set_interface_promisc(int sockfd, char *interface_name)
{
struct ifreq cfgreq;
strncpy( cfgreq.ifr_name, interface_name, IFNAMSIZ);
if( -1 == ioctl( sockfd, SIOCGIFFLAGS, &cfgreq))
{
return -1;
}
cfgreq.ifr_flags |= IFF_PROMISC;
if( -1 == ioctl( sockfd, SIOCSIFFLAGS, &cfgreq))
{
return -1;
}
return 0;
}
int set_interface_promisc2(int sockfd, int dev_id, char *interface_name)
{
struct packet_mreq mr;
memset(&mr, 0, sizeof(mr));
mr.mr_ifindex = dev_id;
mr.mr_type = PACKET_MR_PROMISC;
if (setsockopt(sockfd, SOL_PACKET,
PACKET_ADD_MEMBERSHIP, &mr, sizeof(mr)) == -1)
{
printf("> failed to set to promisc mode, errorno=%x", errno);
return -1;
}
return 0;
}
char ebuf[128];
int iface_get_id(int fd, const char *device, char *ebuf)
{
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
if (ioctl(fd, SIOCGIFINDEX, &ifr) == -1) {
//snprintf(ebuf, PCAP_ERRBUF_SIZE,
// "ioctl: %s", pcap_strerror(errno));
return -1;
}
return ifr.ifr_ifindex;
}
/*
* Bind the socket associated with FD to the given device.
*/
static int
iface_bind(int fd, int ifindex, char *ebuf)
{
struct sockaddr_ll sll;
int err;
socklen_t errlen = sizeof(err);
memset(&sll, 0, sizeof(sll));
sll.sll_family = AF_PACKET;
sll.sll_ifindex = ifindex;
sll.sll_protocol = htons(ETH_P_ALL);
if (bind(fd, (struct sockaddr *) &sll, sizeof(sll)) == -1) {
//snprintf(ebuf, PCAP_ERRBUF_SIZE,
// "bind: %s", pcap_strerror(errno));
return -1;
}
/* Any pending errors, e.g., network is down? */
if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &errlen) == -1) {
//snprintf(ebuf, PCAP_ERRBUF_SIZE,
// "getsockopt: %s", pcap_strerror(errno));
return -2;
}
if (err > 0) {
//snprintf(ebuf, PCAP_ERRBUF_SIZE,
// "bind: %s", pcap_strerror(err));
return -2;
}
return 0;
}
cm_dev_t *handle;
void sigHand_exit(int signo)
{
if( handle != NULL)
cmb_stop_session_feature(handle);
printf("+ exit app. \n");
exit(0);
}
int main()
{
int ret;
int sockfd;
struct ifreq recv_interface;
int nlen;
int i, counter=0;
int dev_id;
struct sigaction act;
//open r2 driver handle..
handle = cm_dev_open_live("/dev/cmb_np");
if(handle == NULL)
{
printf("> Failed to open driver. exit now. ");
return 0;
}
//enable flow capture feature. capture first 20 packets of each flow.
ret = cmb_start_session_feature(handle, 20);
if( ret != CM_SUCCESS)
{
printf("> Failed to enable this feature. exit now. \n");
return 0;
}
sockfd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL) );
if( sockfd == -1)
{
printf("> Failed to open PF_PACKET socket. \n");
return 0;
}
dev_id = iface_get_id(sockfd, "ath0", ebuf);
if( dev_id == -1)
{
return 0;
}
ret = iface_bind(sockfd, dev_id, ebuf);
if( ret < 0)
return 0;
ret = set_interface_promisc2(sockfd, dev_id, "ath0");
if( ret != 0)
{
printf("> failed to set ath0 in promisc mode.\n");
return 0;
}
act.sa_handler = sigHand_exit;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
sigaction(SIGINT, &act, NULL );
while(1)
{
//nlen = recvfrom(sockfd, buffer, sizeof(buffer), MSG_TRUNC, NULL, NULL );
nlen = recvfrom(sockfd, buffer, sizeof(buffer), MSG_TRUNC, NULL, NULL );
if( nlen <= 0)
{
printf("> socket closed. perror=%x \n", nlen);
break;
}
counter++;
/*
printf("packet : %d \n", counter);
for(i=0; (i<nlen) &&( i<14); i++)
{
printf(" %.2x", buffer[i]&0xFF);
}
printf("\n");
*/
print_pkt(buffer, nlen);
if( counter > 2000)
break;
}
cmb_stop_session_feature(handle);
return 0;
}