xref: /freebsd/contrib/libpcap/pcap-septel.c (revision 1669d8afc64812c8d2d1d147ae1fd42ff441e1b1)
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.2.2 2005/06/21 01:03:23 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 Septel 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_setnonblock(pcap_t *p, int nonblock, char *errbuf);
61 
62 static void septel_platform_close(pcap_t *p) {
63 
64 }
65 
66 
67 
68 /*
69  *  Read at most max_packets from the capture queue and call the callback
70  *  for each of them. Returns the number of packets handled, -1 if an
71  *  error occured, or -2 if we were told to break out of the loop.
72  */
73 static int septel_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user) {
74 
75   HDR *h;
76   MSG *m;
77   int processed = 0 ;
78   int t = 0 ;
79 
80   /* identifier for the message queue of the module(upe) from which we are capturing
81    * packets.These IDs are defined in system.txt . By default it is set to 0x2d
82    * so change it to 0xdd for technical reason and therefore the module id for upe becomes:
83    * LOCAL        0xdd           * upe - Example user part task */
84   unsigned int id = 0xdd;
85 
86   /* process the packets */
87   do  {
88 
89     unsigned short packet_len = 0;
90     int caplen = 0;
91     int counter = 0;
92     struct pcap_pkthdr   pcap_header;
93     u_char *dp ;
94 
95     /*
96      * Has "pcap_breakloop()" been called?
97      */
98 loop:
99     if (p->break_loop) {
100       /*
101        * Yes - clear the flag that indicates that
102        * it has, and return -2 to indicate that
103        * we were told to break out of the loop.
104        */
105       p->break_loop = 0;
106       return -2;
107     }
108 
109     /*repeat until a packet is read
110      *a NULL message means :
111      * when no packet is in queue or all packets in queue already read */
112     do  {
113       /* receive packet in non-blocking mode
114        * GCT_grab is defined in the septel library software */
115       h = GCT_grab(id);
116 
117       m = (MSG*)h;
118       /* a couter is added here to avoid an infinite loop
119        * that will cause our capture program GUI to freeze while waiting
120        * for a packet*/
121       counter++ ;
122 
123     }
124     while  ((m == NULL)&& (counter< 100)) ;
125 
126     if (m != NULL) {
127 
128       t = h->type ;
129 
130       /* catch only messages with type = 0xcf00 or 0x8f01 corrsponding to ss7 messages*/
131       /* XXX = why not use API_MSG_TX_REQ for 0xcf00 and API_MSG_RX_IND
132        * for 0x8f01? */
133       if ((t != 0xcf00) && (t != 0x8f01)) {
134         relm(h);
135         goto loop ;
136       }
137 
138       /* XXX - is API_MSG_RX_IND for an MTP2 or MTP3 message? */
139       dp = get_param(m);/* get pointer to MSG parameter area (m->param) */
140       packet_len = m->len;
141       caplen =  p->snapshot ;
142 
143 
144       if (caplen > packet_len) {
145 
146         caplen = packet_len;
147       }
148       /* Run the packet filter if there is one. */
149       if ((p->fcode.bf_insns == NULL) || bpf_filter(p->fcode.bf_insns, dp, packet_len, caplen)) {
150 
151 
152         /*  get a time stamp , consisting of :
153          *
154          *  pcap_header.ts.tv_sec:
155          *  ----------------------
156          *   a UNIX format time-in-seconds when he packet was captured,
157          *   i.e. the number of seconds since Epoch time (January 1,1970, 00:00:00 GMT)
158          *
159          *  pcap_header.ts.tv_usec :
160          *  ------------------------
161          *   the number of microseconds since that second
162          *   when the packet was captured
163          */
164 
165         (void)gettimeofday(&pcap_header.ts, NULL);
166 
167         /* Fill in our own header data */
168         pcap_header.caplen = caplen;
169         pcap_header.len = packet_len;
170 
171         /* Count the packet. */
172         p->md.stat.ps_recv++;
173 
174         /* Call the user supplied callback function */
175         callback(user, &pcap_header, dp);
176 
177         processed++ ;
178 
179       }
180       /* after being processed the packet must be
181        *released in order to receive another one */
182       relm(h);
183     }else
184       processed++;
185 
186   }
187   while (processed < cnt) ;
188 
189   return processed ;
190 }
191 
192 
193 static int
194 septel_inject(pcap_t *handle, const void *buf _U_, size_t size _U_)
195 {
196   strlcpy(handle->errbuf, "Sending packets isn't supported on Septel cards",
197           PCAP_ERRBUF_SIZE);
198   return (-1);
199 }
200 
201 /*
202  *  Get a handle for a live capture from the given Septel device.  Always pass a NULL device
203  *  The promisc flag is ignored because Septel cards have built-in tracing.
204  *  The to_ms parameter is also ignored as it is
205  *  not supported in hardware.
206  *
207  *  See also pcap(3).
208  */
209 pcap_t *septel_open_live(const char *device, int snaplen, int promisc, int to_ms, char *ebuf) {
210   pcap_t *handle;
211 
212   handle = malloc(sizeof(*handle));
213   if (handle == NULL) {
214     snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc %s: %s", device, pcap_strerror(errno));
215     return NULL;
216   }
217 
218   /* Initialize some components of the pcap structure. */
219 
220   memset(handle, 0, sizeof(*handle));
221 
222   handle->snapshot = snaplen;
223 
224   handle->linktype = DLT_MTP2;
225 
226   handle->bufsize = 0;
227 
228   /*
229    * "select()" and "poll()" don't work on Septel queues
230    */
231   handle->selectable_fd = -1;
232 
233   handle->read_op = septel_read;
234   handle->inject_op = septel_inject;
235   handle->setfilter_op = septel_setfilter;
236   handle->set_datalink_op = NULL; /* can't change data link type */
237   handle->getnonblock_op = pcap_getnonblock_fd;
238   handle->setnonblock_op = septel_setnonblock;
239   handle->stats_op = septel_stats;
240   handle->close_op = septel_platform_close;
241 
242   return handle;
243 
244 fail:
245   if (handle != NULL) {
246     free(handle);
247   }
248 
249   return NULL;
250 }
251 
252 static int septel_stats(pcap_t *p, struct pcap_stat *ps) {
253   /*p->md.stat.ps_recv = 0;*/
254   /*p->md.stat.ps_drop = 0;*/
255 
256   *ps = p->md.stat;
257 
258   return 0;
259 }
260 
261 
262 int
263 septel_platform_finddevs(pcap_if_t **devlistp, char *errbuf)
264 {
265 unsigned char *p;
266   const char description[512]= "Intel/Septel device";
267   char name[512]="septel" ;
268   int ret = 0;
269   pcap_add_if(devlistp,name,0,description,errbuf);
270 
271   return (ret);
272 }
273 
274 
275 /*
276  * Installs the given bpf filter program in the given pcap structure.  There is
277  * no attempt to store the filter in kernel memory as that is not supported
278  * with Septel cards.
279  */
280 static int septel_setfilter(pcap_t *p, struct bpf_program *fp) {
281   if (!p)
282     return -1;
283   if (!fp) {
284     strncpy(p->errbuf, "setfilter: No filter specified",
285 	    sizeof(p->errbuf));
286     return -1;
287   }
288 
289   /* Make our private copy of the filter */
290 
291   if (install_bpf_program(p, fp) < 0) {
292     snprintf(p->errbuf, sizeof(p->errbuf),
293 	     "malloc: %s", pcap_strerror(errno));
294     return -1;
295   }
296 
297   p->md.use_bpf = 0;
298 
299   return (0);
300 }
301 
302 
303 static int
304 septel_setnonblock(pcap_t *p, int nonblock, char *errbuf)
305 {
306   return (0);
307 }
308