pcap-septel.c revision 214518
140875Smsmith/* 240875Smsmith * pcap-septel.c: Packet capture interface for Intel/Septel card. 340875Smsmith * 440875Smsmith * The functionality of this code attempts to mimic that of pcap-linux as much 540875Smsmith * as possible. This code is compiled in several different ways depending on 640875Smsmith * whether SEPTEL_ONLY and HAVE_SEPTEL_API are defined. If HAVE_SEPTEL_API is 740875Smsmith * not defined it should not get compiled in, otherwise if SEPTEL_ONLY is 840875Smsmith * defined then the 'septel_' function calls are renamed to 'pcap_' 940875Smsmith * equivalents. If SEPTEL_ONLY is not defined then nothing is altered - the 1040875Smsmith * septel_ functions will be called as required from their 1140875Smsmith * pcap-linux/equivalents. 1240875Smsmith * 1340875Smsmith * Authors: Gilbert HOYEK (gil_hoyek@hotmail.com), Elias M. KHOURY 1440875Smsmith * (+961 3 485243) 1540875Smsmith */ 1640875Smsmith 1740875Smsmith#ifndef lint 1840875Smsmithstatic const char rcsid[] _U_ = 1940875Smsmith "@(#) $Header: /tcpdump/master/libpcap/pcap-septel.c,v 1.4 2008-04-14 20:40:58 guy Exp $"; 2040875Smsmith#endif 2140875Smsmith 2240875Smsmith#ifdef HAVE_CONFIG_H 2340875Smsmith#include "config.h" 2440875Smsmith#endif 2540875Smsmith 2640988Sjkh#include <sys/param.h> 2740875Smsmith 2840875Smsmith#include <stdlib.h> 2940875Smsmith#include <string.h> 3040875Smsmith#include <errno.h> 3140875Smsmith 3240875Smsmith#include "pcap-int.h" 3340984Sjkh 3440948Smsmith#include <ctype.h> 3540948Smsmith#include <netinet/in.h> 3640948Smsmith#include <sys/mman.h> 3740948Smsmith#include <sys/socket.h> 3840948Smsmith#include <sys/types.h> 3940948Smsmith#include <unistd.h> 4040948Smsmith 4140875Smsmith#ifdef HAVE_SEPTEL_API 4240875Smsmith#include <msg.h> 4340875Smsmith#include <ss7_inc.h> 4440875Smsmith#include <sysgct.h> 4540875Smsmith#include <pack.h> 4640875Smsmith#include <system.h> 4740875Smsmith#endif /* HAVE_SEPTEL_API */ 4840875Smsmith 4940875Smsmith#ifdef SEPTEL_ONLY 5040875Smsmith/* This code is required when compiling for a Septel device only. */ 5140875Smsmith#include "pcap-septel.h" 5240875Smsmith 5340875Smsmith/* Replace septel function names with pcap equivalent. */ 5440948Smsmith#define septel_create pcap_create 5540948Smsmith#define septel_platform_finddevs pcap_platform_finddevs 5640875Smsmith#endif /* SEPTEL_ONLY */ 5740875Smsmith 5840875Smsmithstatic int septel_setfilter(pcap_t *p, struct bpf_program *fp); 5940875Smsmithstatic int septel_stats(pcap_t *p, struct pcap_stat *ps); 6040875Smsmithstatic int septel_setnonblock(pcap_t *p, int nonblock, char *errbuf); 6140875Smsmith 6240875Smsmith/* 6340875Smsmith * Read at most max_packets from the capture queue and call the callback 6440875Smsmith * for each of them. Returns the number of packets handled, -1 if an 6540948Smsmith * error occured, or -2 if we were told to break out of the loop. 6640875Smsmith */ 6740875Smsmithstatic int septel_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user) { 6840875Smsmith 6940875Smsmith HDR *h; 7040875Smsmith MSG *m; 7140875Smsmith int processed = 0 ; 7240875Smsmith int t = 0 ; 7340875Smsmith 7440948Smsmith /* identifier for the message queue of the module(upe) from which we are capturing 7540948Smsmith * packets.These IDs are defined in system.txt . By default it is set to 0x2d 7640948Smsmith * so change it to 0xdd for technical reason and therefore the module id for upe becomes: 7740875Smsmith * LOCAL 0xdd * upe - Example user part task */ 7840948Smsmith unsigned int id = 0xdd; 7940948Smsmith 8040950Smsmith /* process the packets */ 8140950Smsmith do { 8240948Smsmith 8340950Smsmith unsigned short packet_len = 0; 8440950Smsmith int caplen = 0; 8540948Smsmith int counter = 0; 8640948Smsmith struct pcap_pkthdr pcap_header; 8740875Smsmith u_char *dp ; 8840875Smsmith 8940875Smsmith /* 9040875Smsmith * Has "pcap_breakloop()" been called? 9140875Smsmith */ 9240875Smsmithloop: 9340875Smsmith if (p->break_loop) { 9440875Smsmith /* 9540875Smsmith * Yes - clear the flag that indicates that 9640875Smsmith * it has, and return -2 to indicate that 9740875Smsmith * we were told to break out of the loop. 9840875Smsmith */ 9940875Smsmith p->break_loop = 0; 10040875Smsmith return -2; 10140875Smsmith } 10240875Smsmith 10340875Smsmith /*repeat until a packet is read 10440875Smsmith *a NULL message means : 10540875Smsmith * when no packet is in queue or all packets in queue already read */ 10640875Smsmith do { 10740875Smsmith /* receive packet in non-blocking mode 10840875Smsmith * GCT_grab is defined in the septel library software */ 10940988Sjkh h = GCT_grab(id); 11040988Sjkh 11140929Sjkh m = (MSG*)h; 11240875Smsmith /* a couter is added here to avoid an infinite loop 11340875Smsmith * that will cause our capture program GUI to freeze while waiting 11440875Smsmith * for a packet*/ 11540875Smsmith counter++ ; 11640875Smsmith 11740988Sjkh } 11840988Sjkh while ((m == NULL)&& (counter< 100)) ; 11940988Sjkh 12040988Sjkh if (m != NULL) { 12140988Sjkh 12240988Sjkh t = h->type ; 12340875Smsmith 12440875Smsmith /* catch only messages with type = 0xcf00 or 0x8f01 corrsponding to ss7 messages*/ 12540875Smsmith /* XXX = why not use API_MSG_TX_REQ for 0xcf00 and API_MSG_RX_IND 12640875Smsmith * for 0x8f01? */ 12740875Smsmith if ((t != 0xcf00) && (t != 0x8f01)) { 12840875Smsmith relm(h); 12940875Smsmith goto loop ; 13040875Smsmith } 13140875Smsmith 13240875Smsmith /* XXX - is API_MSG_RX_IND for an MTP2 or MTP3 message? */ 13340875Smsmith dp = get_param(m);/* get pointer to MSG parameter area (m->param) */ 13440948Smsmith packet_len = m->len; 13540948Smsmith caplen = p->snapshot ; 13640875Smsmith 13740875Smsmith 13840875Smsmith if (caplen > packet_len) { 139 140 caplen = packet_len; 141 } 142 /* Run the packet filter if there is one. */ 143 if ((p->fcode.bf_insns == NULL) || bpf_filter(p->fcode.bf_insns, dp, packet_len, caplen)) { 144 145 146 /* get a time stamp , consisting of : 147 * 148 * pcap_header.ts.tv_sec: 149 * ---------------------- 150 * a UNIX format time-in-seconds when he packet was captured, 151 * i.e. the number of seconds since Epoch time (January 1,1970, 00:00:00 GMT) 152 * 153 * pcap_header.ts.tv_usec : 154 * ------------------------ 155 * the number of microseconds since that second 156 * when the packet was captured 157 */ 158 159 (void)gettimeofday(&pcap_header.ts, NULL); 160 161 /* Fill in our own header data */ 162 pcap_header.caplen = caplen; 163 pcap_header.len = packet_len; 164 165 /* Count the packet. */ 166 p->md.stat.ps_recv++; 167 168 /* Call the user supplied callback function */ 169 callback(user, &pcap_header, dp); 170 171 processed++ ; 172 173 } 174 /* after being processed the packet must be 175 *released in order to receive another one */ 176 relm(h); 177 }else 178 processed++; 179 180 } 181 while (processed < cnt) ; 182 183 return processed ; 184} 185 186 187static int 188septel_inject(pcap_t *handle, const void *buf _U_, size_t size _U_) 189{ 190 strlcpy(handle->errbuf, "Sending packets isn't supported on Septel cards", 191 PCAP_ERRBUF_SIZE); 192 return (-1); 193} 194 195/* 196 * Activate a handle for a live capture from the given Septel device. Always pass a NULL device 197 * The promisc flag is ignored because Septel cards have built-in tracing. 198 * The timeout is also ignored as it is not supported in hardware. 199 * 200 * See also pcap(3). 201 */ 202static pcap_t *septel_activate(pcap_t* handle) { 203 /* Initialize some components of the pcap structure. */ 204 handle->linktype = DLT_MTP2; 205 206 handle->bufsize = 0; 207 208 /* 209 * "select()" and "poll()" don't work on Septel queues 210 */ 211 handle->selectable_fd = -1; 212 213 handle->read_op = septel_read; 214 handle->inject_op = septel_inject; 215 handle->setfilter_op = septel_setfilter; 216 handle->set_datalink_op = NULL; /* can't change data link type */ 217 handle->getnonblock_op = pcap_getnonblock_fd; 218 handle->setnonblock_op = septel_setnonblock; 219 handle->stats_op = septel_stats; 220 221 return 0; 222} 223 224pcap_t *septel_create(const char *device, char *ebuf) { 225 pcap_t *p; 226 227 p = pcap_create_common(device, ebuf); 228 if (p == NULL) 229 return NULL; 230 231 p->activate_op = septel_activate; 232 return p; 233} 234 235static int septel_stats(pcap_t *p, struct pcap_stat *ps) { 236 /*p->md.stat.ps_recv = 0;*/ 237 /*p->md.stat.ps_drop = 0;*/ 238 239 *ps = p->md.stat; 240 241 return 0; 242} 243 244 245int 246septel_platform_finddevs(pcap_if_t **devlistp, char *errbuf) 247{ 248unsigned char *p; 249 const char description[512]= "Intel/Septel device"; 250 char name[512]="septel" ; 251 int ret = 0; 252 pcap_add_if(devlistp,name,0,description,errbuf); 253 254 return (ret); 255} 256 257 258/* 259 * Installs the given bpf filter program in the given pcap structure. There is 260 * no attempt to store the filter in kernel memory as that is not supported 261 * with Septel cards. 262 */ 263static int septel_setfilter(pcap_t *p, struct bpf_program *fp) { 264 if (!p) 265 return -1; 266 if (!fp) { 267 strncpy(p->errbuf, "setfilter: No filter specified", 268 sizeof(p->errbuf)); 269 return -1; 270 } 271 272 /* Make our private copy of the filter */ 273 274 if (install_bpf_program(p, fp) < 0) { 275 snprintf(p->errbuf, sizeof(p->errbuf), 276 "malloc: %s", pcap_strerror(errno)); 277 return -1; 278 } 279 280 p->md.use_bpf = 0; 281 282 return (0); 283} 284 285 286static int 287septel_setnonblock(pcap_t *p, int nonblock, char *errbuf) 288{ 289 return (0); 290} 291