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