xref: /freebsd/contrib/libpcap/pcap-septel.c (revision afdbf109c6a661a729938f68211054a0a50d38ac)
1ee2dd488SSam Leffler /*
2ee2dd488SSam Leffler  * pcap-septel.c: Packet capture interface for Intel/Septel card.
3ee2dd488SSam Leffler  *
4ee2dd488SSam Leffler  * Authors: Gilbert HOYEK (gil_hoyek@hotmail.com), Elias M. KHOURY
5ee2dd488SSam Leffler  * (+961 3 485243)
6ee2dd488SSam Leffler  */
7ee2dd488SSam Leffler 
8b00ab754SHans Petter Selasky #include <config.h>
9ee2dd488SSam Leffler 
10ee2dd488SSam Leffler #include <sys/param.h>
11ee2dd488SSam Leffler 
12ee2dd488SSam Leffler #include <stdlib.h>
13ee2dd488SSam Leffler #include <string.h>
14ee2dd488SSam Leffler #include <errno.h>
15ee2dd488SSam Leffler 
16ee2dd488SSam Leffler #include "pcap-int.h"
17ee2dd488SSam Leffler 
18ee2dd488SSam Leffler #include <netinet/in.h>
19ee2dd488SSam Leffler #include <sys/mman.h>
20ee2dd488SSam Leffler #include <sys/socket.h>
21ee2dd488SSam Leffler #include <sys/types.h>
22ee2dd488SSam Leffler #include <unistd.h>
23ee2dd488SSam Leffler 
24ee2dd488SSam Leffler #include <msg.h>
25ee2dd488SSam Leffler #include <ss7_inc.h>
26ee2dd488SSam Leffler #include <sysgct.h>
27ee2dd488SSam Leffler #include <pack.h>
28ee2dd488SSam Leffler #include <system.h>
29ee2dd488SSam Leffler 
30ee2dd488SSam Leffler #include "pcap-septel.h"
31ee2dd488SSam Leffler 
32ee2dd488SSam Leffler static int septel_stats(pcap_t *p, struct pcap_stat *ps);
33b00ab754SHans Petter Selasky static int septel_getnonblock(pcap_t *p);
34b00ab754SHans Petter Selasky static int septel_setnonblock(pcap_t *p, int nonblock);
35ee2dd488SSam Leffler 
36ee2dd488SSam Leffler /*
37681ed54cSXin LI  * Private data for capturing on Septel devices.
38681ed54cSXin LI  */
39681ed54cSXin LI struct pcap_septel {
40681ed54cSXin LI 	struct pcap_stat stat;
41681ed54cSXin LI }
42681ed54cSXin LI 
43681ed54cSXin LI /*
44ee2dd488SSam Leffler  *  Read at most max_packets from the capture queue and call the callback
45ee2dd488SSam Leffler  *  for each of them. Returns the number of packets handled, -1 if an
466f9cba8fSJoseph Mingrone  *  error occurred, or -2 if we were told to break out of the loop.
47ee2dd488SSam Leffler  */
septel_read(pcap_t * p,int cnt,pcap_handler callback,u_char * user)48ee2dd488SSam Leffler static int septel_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user) {
49ee2dd488SSam Leffler 
50681ed54cSXin LI   struct pcap_septel *ps = p->priv;
51ee2dd488SSam Leffler   HDR *h;
52ee2dd488SSam Leffler   MSG *m;
53ee2dd488SSam Leffler   int processed = 0 ;
54ee2dd488SSam Leffler   int t = 0 ;
55ee2dd488SSam Leffler 
56ee2dd488SSam Leffler   /* identifier for the message queue of the module(upe) from which we are capturing
57ee2dd488SSam Leffler    * packets.These IDs are defined in system.txt . By default it is set to 0x2d
58ee2dd488SSam Leffler    * so change it to 0xdd for technical reason and therefore the module id for upe becomes:
59ee2dd488SSam Leffler    * LOCAL        0xdd           * upe - Example user part task */
60ee2dd488SSam Leffler   unsigned int id = 0xdd;
61ee2dd488SSam Leffler 
62ee2dd488SSam Leffler   /* process the packets */
63ee2dd488SSam Leffler   do  {
64ee2dd488SSam Leffler 
65ee2dd488SSam Leffler     unsigned short packet_len = 0;
66ee2dd488SSam Leffler     int caplen = 0;
67ee2dd488SSam Leffler     int counter = 0;
68ee2dd488SSam Leffler     struct pcap_pkthdr   pcap_header;
69ee2dd488SSam Leffler     u_char *dp ;
70ee2dd488SSam Leffler 
71ee2dd488SSam Leffler     /*
72ee2dd488SSam Leffler      * Has "pcap_breakloop()" been called?
73ee2dd488SSam Leffler      */
74ee2dd488SSam Leffler loop:
75ee2dd488SSam Leffler     if (p->break_loop) {
76ee2dd488SSam Leffler       /*
77ee2dd488SSam Leffler        * Yes - clear the flag that indicates that
78ee2dd488SSam Leffler        * it has, and return -2 to indicate that
79ee2dd488SSam Leffler        * we were told to break out of the loop.
80ee2dd488SSam Leffler        */
81ee2dd488SSam Leffler       p->break_loop = 0;
82ee2dd488SSam Leffler       return -2;
83ee2dd488SSam Leffler     }
84ee2dd488SSam Leffler 
85ee2dd488SSam Leffler     /*repeat until a packet is read
86ee2dd488SSam Leffler      *a NULL message means :
87ee2dd488SSam Leffler      * when no packet is in queue or all packets in queue already read */
88ee2dd488SSam Leffler     do  {
89ee2dd488SSam Leffler       /* receive packet in non-blocking mode
90ee2dd488SSam Leffler        * GCT_grab is defined in the septel library software */
91ee2dd488SSam Leffler       h = GCT_grab(id);
92ee2dd488SSam Leffler 
93ee2dd488SSam Leffler       m = (MSG*)h;
946f9cba8fSJoseph Mingrone       /* a counter is added here to avoid an infinite loop
95ee2dd488SSam Leffler        * that will cause our capture program GUI to freeze while waiting
96ee2dd488SSam Leffler        * for a packet*/
97ee2dd488SSam Leffler       counter++ ;
98ee2dd488SSam Leffler 
99ee2dd488SSam Leffler     }
100ee2dd488SSam Leffler     while  ((m == NULL)&& (counter< 100)) ;
101ee2dd488SSam Leffler 
102ee2dd488SSam Leffler     if (m != NULL) {
103ee2dd488SSam Leffler 
104ee2dd488SSam Leffler       t = h->type ;
105ee2dd488SSam Leffler 
1066f9cba8fSJoseph Mingrone       /* catch only messages with type = 0xcf00 or 0x8f01 corresponding to ss7 messages*/
107ee2dd488SSam Leffler       /* XXX = why not use API_MSG_TX_REQ for 0xcf00 and API_MSG_RX_IND
108ee2dd488SSam Leffler        * for 0x8f01? */
109ee2dd488SSam Leffler       if ((t != 0xcf00) && (t != 0x8f01)) {
110ee2dd488SSam Leffler         relm(h);
111ee2dd488SSam Leffler         goto loop ;
112ee2dd488SSam Leffler       }
113ee2dd488SSam Leffler 
114ee2dd488SSam Leffler       /* XXX - is API_MSG_RX_IND for an MTP2 or MTP3 message? */
115ee2dd488SSam Leffler       dp = get_param(m);/* get pointer to MSG parameter area (m->param) */
116ee2dd488SSam Leffler       packet_len = m->len;
117ee2dd488SSam Leffler       caplen =  p->snapshot ;
118ee2dd488SSam Leffler 
119ee2dd488SSam Leffler 
120ee2dd488SSam Leffler       if (caplen > packet_len) {
121ee2dd488SSam Leffler 
122ee2dd488SSam Leffler         caplen = packet_len;
123ee2dd488SSam Leffler       }
124ee2dd488SSam Leffler       /* Run the packet filter if there is one. */
125*afdbf109SJoseph Mingrone       if ((p->fcode.bf_insns == NULL) || pcapint_filter(p->fcode.bf_insns, dp, packet_len, caplen)) {
126ee2dd488SSam Leffler 
127ee2dd488SSam Leffler 
128ee2dd488SSam Leffler         /*  get a time stamp , consisting of :
129ee2dd488SSam Leffler          *
130ee2dd488SSam Leffler          *  pcap_header.ts.tv_sec:
131ee2dd488SSam Leffler          *  ----------------------
132ee2dd488SSam Leffler          *   a UNIX format time-in-seconds when he packet was captured,
133ee2dd488SSam Leffler          *   i.e. the number of seconds since Epoch time (January 1,1970, 00:00:00 GMT)
134ee2dd488SSam Leffler          *
135ee2dd488SSam Leffler          *  pcap_header.ts.tv_usec :
136ee2dd488SSam Leffler          *  ------------------------
137ee2dd488SSam Leffler          *   the number of microseconds since that second
138ee2dd488SSam Leffler          *   when the packet was captured
139ee2dd488SSam Leffler          */
140ee2dd488SSam Leffler 
141ee2dd488SSam Leffler         (void)gettimeofday(&pcap_header.ts, NULL);
142ee2dd488SSam Leffler 
143ee2dd488SSam Leffler         /* Fill in our own header data */
144ee2dd488SSam Leffler         pcap_header.caplen = caplen;
145ee2dd488SSam Leffler         pcap_header.len = packet_len;
146ee2dd488SSam Leffler 
147ee2dd488SSam Leffler         /* Count the packet. */
148681ed54cSXin LI         ps->stat.ps_recv++;
149ee2dd488SSam Leffler 
150ee2dd488SSam Leffler         /* Call the user supplied callback function */
151ee2dd488SSam Leffler         callback(user, &pcap_header, dp);
152ee2dd488SSam Leffler 
153ee2dd488SSam Leffler         processed++ ;
154ee2dd488SSam Leffler 
155ee2dd488SSam Leffler       }
156ee2dd488SSam Leffler       /* after being processed the packet must be
157ee2dd488SSam Leffler        *released in order to receive another one */
158ee2dd488SSam Leffler       relm(h);
159ee2dd488SSam Leffler     }else
160ee2dd488SSam Leffler       processed++;
161ee2dd488SSam Leffler 
162ee2dd488SSam Leffler   }
163ee2dd488SSam Leffler   while (processed < cnt) ;
164ee2dd488SSam Leffler 
165ee2dd488SSam Leffler   return processed ;
166ee2dd488SSam Leffler }
167ee2dd488SSam Leffler 
168ee2dd488SSam Leffler 
169ee2dd488SSam Leffler static int
septel_inject(pcap_t * handle,const void * buf _U_,int size _U_)1706f9cba8fSJoseph Mingrone septel_inject(pcap_t *handle, const void *buf _U_, int size _U_)
171ee2dd488SSam Leffler {
172*afdbf109SJoseph Mingrone   pcapint_strlcpy(handle->errbuf, "Sending packets isn't supported on Septel cards",
173ee2dd488SSam Leffler           PCAP_ERRBUF_SIZE);
174ee2dd488SSam Leffler   return (-1);
175ee2dd488SSam Leffler }
176ee2dd488SSam Leffler 
177ee2dd488SSam Leffler /*
178a8e07101SRui Paulo  *  Activate a handle for a live capture from the given Septel device.  Always pass a NULL device
179ee2dd488SSam Leffler  *  The promisc flag is ignored because Septel cards have built-in tracing.
180a8e07101SRui Paulo  *  The timeout is also ignored as it is not supported in hardware.
181ee2dd488SSam Leffler  *
182ee2dd488SSam Leffler  *  See also pcap(3).
183ee2dd488SSam Leffler  */
septel_activate(pcap_t * handle)184a8e07101SRui Paulo static pcap_t *septel_activate(pcap_t* handle) {
185ee2dd488SSam Leffler   /* Initialize some components of the pcap structure. */
186ee2dd488SSam Leffler   handle->linktype = DLT_MTP2;
187ee2dd488SSam Leffler 
188b00ab754SHans Petter Selasky   /*
189b00ab754SHans Petter Selasky    * Turn a negative snapshot value (invalid), a snapshot value of
190b00ab754SHans Petter Selasky    * 0 (unspecified), or a value bigger than the normal maximum
191b00ab754SHans Petter Selasky    * value, into the maximum allowed value.
192b00ab754SHans Petter Selasky    *
193b00ab754SHans Petter Selasky    * If some application really *needs* a bigger snapshot
194b00ab754SHans Petter Selasky    * length, we should just increase MAXIMUM_SNAPLEN.
195b00ab754SHans Petter Selasky    */
196b00ab754SHans Petter Selasky   if (handle->snapshot <= 0 || handle->snapshot > MAXIMUM_SNAPLEN)
197b00ab754SHans Petter Selasky     handle->snapshot = MAXIMUM_SNAPLEN;
198b00ab754SHans Petter Selasky 
199ee2dd488SSam Leffler   handle->bufsize = 0;
200ee2dd488SSam Leffler 
201ee2dd488SSam Leffler   /*
202ee2dd488SSam Leffler    * "select()" and "poll()" don't work on Septel queues
203ee2dd488SSam Leffler    */
204ee2dd488SSam Leffler   handle->selectable_fd = -1;
205ee2dd488SSam Leffler 
206ee2dd488SSam Leffler   handle->read_op = septel_read;
207ee2dd488SSam Leffler   handle->inject_op = septel_inject;
208*afdbf109SJoseph Mingrone   handle->setfilter_op = pcapint_install_bpf_program;
209ee2dd488SSam Leffler   handle->set_datalink_op = NULL; /* can't change data link type */
210b00ab754SHans Petter Selasky   handle->getnonblock_op = septel_getnonblock;
211ee2dd488SSam Leffler   handle->setnonblock_op = septel_setnonblock;
212ee2dd488SSam Leffler   handle->stats_op = septel_stats;
213ee2dd488SSam Leffler 
214a8e07101SRui Paulo   return 0;
215ee2dd488SSam Leffler }
216ee2dd488SSam Leffler 
septel_create(const char * device,char * ebuf,int * is_ours)217edc89b24SXin LI pcap_t *septel_create(const char *device, char *ebuf, int *is_ours) {
218edc89b24SXin LI 	const char *cp;
219a8e07101SRui Paulo 	pcap_t *p;
220a8e07101SRui Paulo 
221edc89b24SXin LI 	/* Does this look like the Septel device? */
222edc89b24SXin LI 	cp = strrchr(device, '/');
223edc89b24SXin LI 	if (cp == NULL)
224edc89b24SXin LI 		cp = device;
225edc89b24SXin LI 	if (strcmp(cp, "septel") != 0) {
226edc89b24SXin LI 		/* Nope, it's not "septel" */
227edc89b24SXin LI 		*is_ours = 0;
228edc89b24SXin LI 		return NULL;
229edc89b24SXin LI 	}
230edc89b24SXin LI 
231edc89b24SXin LI 	/* OK, it's probably ours. */
232edc89b24SXin LI 	*is_ours = 1;
233edc89b24SXin LI 
2346f9cba8fSJoseph Mingrone 	p = PCAP_CREATE_COMMON(ebuf, struct pcap_septel);
235a8e07101SRui Paulo 	if (p == NULL)
236ee2dd488SSam Leffler 		return NULL;
237a8e07101SRui Paulo 
238a8e07101SRui Paulo 	p->activate_op = septel_activate;
239b00ab754SHans Petter Selasky 	/*
240b00ab754SHans Petter Selasky 	 * Set these up front, so that, even if our client tries
241b00ab754SHans Petter Selasky 	 * to set non-blocking mode before we're activated, or
242b00ab754SHans Petter Selasky 	 * query the state of non-blocking mode, they get an error,
243b00ab754SHans Petter Selasky 	 * rather than having the non-blocking mode option set
244b00ab754SHans Petter Selasky 	 * for use later.
245b00ab754SHans Petter Selasky 	 */
246b00ab754SHans Petter Selasky 	p->getnonblock_op = septel_getnonblock;
247b00ab754SHans Petter Selasky 	p->setnonblock_op = septel_setnonblock;
248a8e07101SRui Paulo 	return p;
249ee2dd488SSam Leffler }
250ee2dd488SSam Leffler 
septel_stats(pcap_t * p,struct pcap_stat * ps)251ee2dd488SSam Leffler static int septel_stats(pcap_t *p, struct pcap_stat *ps) {
252681ed54cSXin LI   struct pcap_septel *handlep = p->priv;
253681ed54cSXin LI   /*handlep->stat.ps_recv = 0;*/
254681ed54cSXin LI   /*handlep->stat.ps_drop = 0;*/
255ee2dd488SSam Leffler 
256681ed54cSXin LI   *ps = handlep->stat;
257ee2dd488SSam Leffler 
258ee2dd488SSam Leffler   return 0;
259ee2dd488SSam Leffler }
260ee2dd488SSam Leffler 
261ee2dd488SSam Leffler 
262ee2dd488SSam Leffler int
septel_findalldevs(pcap_if_list_t * devlistp,char * errbuf)263b00ab754SHans Petter Selasky septel_findalldevs(pcap_if_list_t *devlistp, char *errbuf)
264ee2dd488SSam Leffler {
265b00ab754SHans Petter Selasky   /*
266b00ab754SHans Petter Selasky    * XXX - do the notions of "up", "running", or "connected" apply here?
267b00ab754SHans Petter Selasky    */
268*afdbf109SJoseph Mingrone   if (pcapint_add_dev(devlistp,"septel",0,"Intel/Septel device",errbuf) == NULL)
269b00ab754SHans Petter Selasky     return -1;
270b00ab754SHans Petter Selasky   return 0;
271ee2dd488SSam Leffler }
272ee2dd488SSam Leffler 
273ee2dd488SSam Leffler 
274ee2dd488SSam Leffler /*
275b00ab754SHans Petter Selasky  * We don't support non-blocking mode.  I'm not sure what we'd
276b00ab754SHans Petter Selasky  * do to support it and, given that we don't support select()/
277b00ab754SHans Petter Selasky  * poll()/epoll_wait()/kevent() etc., it probably doesn't
278b00ab754SHans Petter Selasky  * matter.
279b00ab754SHans Petter Selasky  */
280b00ab754SHans Petter Selasky static int
septel_getnonblock(pcap_t * p)281b00ab754SHans Petter Selasky septel_getnonblock(pcap_t *p)
282b00ab754SHans Petter Selasky {
283b00ab754SHans Petter Selasky   fprintf(p->errbuf, PCAP_ERRBUF_SIZE, "Non-blocking mode not supported on Septel devices");
284b00ab754SHans Petter Selasky   return (-1);
285b00ab754SHans Petter Selasky }
286ee2dd488SSam Leffler 
287ee2dd488SSam Leffler static int
septel_setnonblock(pcap_t * p,int nonblock _U_)288b00ab754SHans Petter Selasky septel_setnonblock(pcap_t *p, int nonblock _U_)
289ee2dd488SSam Leffler {
290b00ab754SHans Petter Selasky   fprintf(p->errbuf, PCAP_ERRBUF_SIZE, "Non-blocking mode not supported on Septel devices");
291681ed54cSXin LI   return (-1);
292ee2dd488SSam Leffler }
293ada6f083SXin LI 
294ada6f083SXin LI #ifdef SEPTEL_ONLY
295ada6f083SXin LI /*
296ada6f083SXin LI  * This libpcap build supports only Septel cards, not regular network
297ada6f083SXin LI  * interfaces.
298ada6f083SXin LI  */
299ada6f083SXin LI 
300ada6f083SXin LI /*
301ada6f083SXin LI  * There are no regular interfaces, just Septel interfaces.
302ada6f083SXin LI  */
303ada6f083SXin LI int
pcapint_platform_finddevs(pcap_if_list_t * devlistp,char * errbuf)304*afdbf109SJoseph Mingrone pcapint_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf)
305ada6f083SXin LI {
306ada6f083SXin LI   return (0);
307ada6f083SXin LI }
308ada6f083SXin LI 
309ada6f083SXin LI /*
310ada6f083SXin LI  * Attempts to open a regular interface fail.
311ada6f083SXin LI  */
312ada6f083SXin LI pcap_t *
pcapint_create_interface(const char * device,char * errbuf)313*afdbf109SJoseph Mingrone pcapint_create_interface(const char *device, char *errbuf)
314ada6f083SXin LI {
3156f9cba8fSJoseph Mingrone   snprintf(errbuf, PCAP_ERRBUF_SIZE,
316ada6f083SXin LI                 "This version of libpcap only supports Septel cards");
317ada6f083SXin LI   return (NULL);
318ada6f083SXin LI }
319b00ab754SHans Petter Selasky 
320b00ab754SHans Petter Selasky /*
321b00ab754SHans Petter Selasky  * Libpcap version string.
322b00ab754SHans Petter Selasky  */
323b00ab754SHans Petter Selasky const char *
pcap_lib_version(void)324b00ab754SHans Petter Selasky pcap_lib_version(void)
325b00ab754SHans Petter Selasky {
326b00ab754SHans Petter Selasky   return (PCAP_VERSION_STRING " (Septel-only)");
327b00ab754SHans Petter Selasky }
328ada6f083SXin LI #endif
329