]> The Tcpdump Group git mirrors - libpcap/blob - pcap-septel.c
From Gilbert Hoyek <[email protected]>: support for capturing SS7
[libpcap] / pcap-septel.c
1 /*
2 * pcap-septel.c: Packet capture interface for Intel/Septel card.
3 *
4 * The functionality of this code attempts to mimic that of pcap-linux as much
5 * as possible. This code is compiled in several different ways depending on
6 * whether SEPTEL_ONLY and HAVE_SEPTEL_API are defined. If HAVE_SEPTEL_API is
7 * not defined it should not get compiled in, otherwise if SEPTEL_ONLY is
8 * defined then the 'septel_' function calls are renamed to 'pcap_'
9 * equivalents. If SEPTEL_ONLY is not defined then nothing is altered - the
10 * septel_ functions will be called as required from their
11 * pcap-linux/equivalents.
12 *
13 * Authors: Gilbert HOYEK (gil_hoyek@hotmail.com), Elias M. KHOURY
14 * (+961 3 485243)
15 */
16
17 #ifndef lint
18 static const char rcsid[] _U_ =
19 "@(#) $Header: /tcpdump/master/libpcap/pcap-septel.c,v 1.1 2005-06-20 21:27:10 guy Exp $";
20 #endif
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include <sys/param.h>
27
28 #include <stdlib.h>
29 #include <string.h>
30 #include <errno.h>
31
32 #include "pcap-int.h"
33
34 #include <ctype.h>
35 #include <netinet/in.h>
36 #include <sys/mman.h>
37 #include <sys/socket.h>
38 #include <sys/types.h>
39 #include <unistd.h>
40
41 #ifdef HAVE_SEPTEL_API
42 #include <msg.h>
43 #include <ss7_inc.h>
44 #include <sysgct.h>
45 #include <pack.h>
46 #include <system.h>
47 #endif /* HAVE_SEPTEL_API */
48
49 #ifdef SEPTEL_ONLY
50 /* This code is required when compiling for a DAG device only. */
51 #include "pcap-septel.h"
52
53 /* Replace dag function names with pcap equivalent. */
54 #define septel_open_live pcap_open_live
55 #define septel_platform_finddevs pcap_platform_finddevs
56 #endif /* SEPTEL_ONLY */
57
58 static int septel_setfilter(pcap_t *p, struct bpf_program *fp);
59 static int septel_stats(pcap_t *p, struct pcap_stat *ps);
60 static int septel_set_datalink(pcap_t *p, int dlt);
61 static int septel_get_datalink(pcap_t *p);
62 static int septel_setnonblock(pcap_t *p, int nonblock, char *errbuf);
63
64 static void septel_platform_close(pcap_t *p) {
65
66 }
67
68
69
70 /*
71 * Read at most max_packets from the capture queue and call the callback
72 * for each of them. Returns the number of packets handled, -1 if an
73 * error occured, or -2 if we were told to break out of the loop.
74 */
75 static int septel_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user) {
76
77 HDR *h;
78 MSG *m;
79 int processed = 0 ;
80 int t = 0 ;
81
82 /* identifier for the message queue of the module(upe) from which we are capturing
83 * packets.These IDs are defined in system.txt . By default it is set to 0x2d
84 * so change it to 0xdd for technical reason and therefore the module id for upe becomes:
85 * LOCAL 0xdd * upe - Example user part task */
86 unsigned int id = 0xdd;
87
88 /* process the packets */
89 do {
90
91 unsigned short packet_len = 0;
92 int caplen = 0;
93 int counter = 0;
94 struct pcap_pkthdr pcap_header;
95
96 u_char *dp ;
97 dp = malloc(320); /* 320 = size of param area */
98
99 /*
100 * Has "pcap_breakloop()" been called?
101 */
102 loop:
103 if (p->break_loop) {
104 /*
105 * Yes - clear the flag that indicates that
106 * it has, and return -2 to indicate that
107 * we were told to break out of the loop.
108 */
109 p->break_loop = 0;
110 return -2;
111 }
112
113 /*repeat until a packet is read
114 *a NULL message means :
115 * when no packet is in queue or all packets in queue already read */
116 do {
117 /* receive packet in non-blocking mode
118 * GCT_grab is defined in the septel library software */
119 h = GCT_grab(id);
120
121 m = (MSG*)h;
122 /* a couter is added here to avoid an infinite loop
123 * that will cause our capture program GUI to freeze while waiting for a packet*/
124 counter++ ;
125
126 }
127 while ((m == NULL)&& (counter< 100)) ;
128
129 if (m != NULL) {
130
131 t = h->type ;
132
133 /* catch only messages with type = 0xcf00 or 0x8f01 corrsponding to ss7 messages*/
134 if ((t != 0xcf00) && (t != 0x8f01)) {
135 relm(h);
136 goto loop ;
137 }
138
139
140 dp = get_param(m);/* get pointer to MSG parameter area (m->param) */
141 packet_len = m->len;
142 caplen = p->snapshot ;
143
144
145 if (caplen > packet_len) {
146
147 caplen = packet_len;
148 }
149 /* Run the packet filter if there is one. */
150 if ((p->fcode.bf_insns == NULL) || bpf_filter(p->fcode.bf_insns, dp, packet_len, caplen)) {
151
152
153 /* get a time stamp , consisting of :
154 *
155 * pcap_header.ts.tv_sec:
156 * ----------------------
157 * a UNIX format time-in-seconds when he packet was captured,
158 * i.e. the number of seconds since Epoch time (January 1,1970, 00:00:00 GMT)
159 *
160 * pcap_header.ts.tv_usec :
161 * ------------------------
162 * the number of microseconds since that second
163 * when the packet was captured
164 */
165
166 (void)gettimeofday(&pcap_header.ts, NULL);
167
168 /* Fill in our own header data */
169 pcap_header.caplen = caplen;
170 pcap_header.len = packet_len;
171
172 /* Count the packet. */
173 p->md.stat.ps_recv++;
174
175 /* Call the user supplied callback function */
176 callback(user, &pcap_header, dp);
177
178 processed++ ;
179
180 }
181 /* after being processed the packet must be
182 *released in order to receive another one */
183 relm(h);
184 }else
185 processed++;
186
187 }
188
189 while (processed < cnt ) ;
190
191 return processed ;
192 }
193
194 /*
195 * Get a handle for a live capture from the given Septel device. Always pass a NULL device
196 * The promisc flag is ignored because Septel cards have built-in tracing.
197 * The to_ms parameter is also ignored as it is
198 * not supported in hardware.
199 *
200 * See also pcap(3).
201 */
202 pcap_t *septel_open_live(const char *device, int snaplen, int promisc, int to_ms, char *ebuf) {
203 pcap_t *handle;
204
205 handle = malloc(sizeof(*handle));
206 if (handle == NULL) {
207 snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc %s: %s", device, pcap_strerror(errno));
208 return NULL;
209 }
210
211 /* Initialize some components of the pcap structure. */
212
213 memset(handle, 0, sizeof(*handle));
214
215 handle->snapshot = snaplen;
216
217 if ((handle->linktype = septel_get_datalink(handle)) < 0) {
218 snprintf(ebuf, PCAP_ERRBUF_SIZE, "septel_get_linktype %s: unknown linktype\n", device);
219 goto fail;
220 }
221
222 handle->bufsize = 0;
223
224
225 /*
226 * "select()" and "poll()" don't work on Septel queues
227 */
228 handle->selectable_fd = -1;
229
230 handle->read_op = septel_read;
231 handle->setfilter_op = septel_setfilter;
232 handle->set_datalink_op = septel_set_datalink;
233 handle->getnonblock_op = pcap_getnonblock_fd;
234 handle->setnonblock_op = septel_setnonblock;
235 handle->stats_op = septel_stats;
236 handle->close_op = septel_platform_close;
237
238 return handle;
239
240 fail:
241 if (handle != NULL) {
242 free(handle);
243 }
244
245 return NULL;
246 }
247
248 static int septel_stats(pcap_t *p, struct pcap_stat *ps) {
249 /*p->md.stat.ps_recv = 0;*/
250 /*p->md.stat.ps_drop = 0;*/
251
252 *ps = p->md.stat;
253
254 return 0;
255 }
256
257
258 int
259 septel_platform_finddevs(pcap_if_t **devlistp, char *errbuf)
260 {
261 unsigned char *p;
262 const char description[512]= "Intel/Septel device";
263 char name[512]="septel" ;
264 int ret = 0;
265 pcap_add_if(devlistp,name,0,description,errbuf);
266
267 return (ret);
268 }
269
270
271 /*
272 * Installs the given bpf filter program in the given pcap structure. There is
273 * no attempt to store the filter in kernel memory as that is not supported
274 * with SEPTEL cards.
275 */
276 static int septel_setfilter(pcap_t *p, struct bpf_program *fp) {
277 if (!p)
278 return -1;
279 if (!fp) {
280 strncpy(p->errbuf, "setfilter: No filter specified",
281 sizeof(p->errbuf));
282 return -1;
283 }
284
285 /* Make our private copy of the filter */
286
287 if (install_bpf_program(p, fp) < 0) {
288 snprintf(p->errbuf, sizeof(p->errbuf),
289 "malloc: %s", pcap_strerror(errno));
290 return -1;
291 }
292
293 p->md.use_bpf = 0;
294
295
296
297 return (0);
298 }
299
300
301 static int
302 septel_set_datalink(pcap_t *p, int dlt)
303 {
304 return (0);
305 }
306
307 static int
308 septel_setnonblock(pcap_t *p, int nonblock, char *errbuf)
309 {
310 return (0);
311 }
312
313 static int
314 septel_get_datalink(pcap_t *p)
315 {
316 int linktype = -1;
317 return DLT_MTP2;
318 }