There's no need to keep a "FILE *" for the file being printed to in a
[obnox/wireshark/wip.git] / packet-iapp.c
1 /* packet-iapp.c
2  * Routines for IAPP dissection
3  * Copyright 2002, Alfred Arnold <aarnold@elsa.de>
4  *
5  * $Id: packet-iapp.c,v 1.6 2002/08/28 21:00:16 jmayer Exp $
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@ethereal.com>
9  * Copyright 1998 Gerald Combs
10  *
11  * is a dissector file; if you just copied this from README.developer,
12  * don't bother with the "Copied from" - you don't even need to put
13  * in a "Copied from" if you copied an existing dissector, especially
14  * if the bulk of the code in the new dissector is your code)
15  *
16  * This program is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU General Public License
18  * as published by the Free Software Foundation; either version 2
19  * of the License, or (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29  */
30
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <ctype.h>
39
40 #include <glib.h>
41
42 #include <epan/packet.h>
43 #include "oui.h"
44
45 /* Initialize the protocol and registered fields */
46 static int proto_iapp = -1;
47 static int hf_iapp_version = -1;
48 static int hf_iapp_type = -1;
49
50 /* Initialize the subtree pointers */
51 static gint ett_iapp = -1;
52 static gint ett_iapp_pdu = -1;
53 static gint ett_iapp_cap = -1;
54 static gint ett_iapp_auth = -1;
55
56 #define UDP_PORT_IAPP     2313
57
58 #define IAPP_ANNOUNCE_REQUEST  0
59 #define IAPP_ANNOUNCE_RESPONSE 1
60 #define IAPP_HANDOVER_REQUEST 2
61 #define IAPP_HANDOVER_RESPONSE 3
62
63 #define IAPP_PDU_SSID 0
64 #define IAPP_PDU_BSSID 1
65 #define IAPP_PDU_OLDBSSID 2
66 #define IAPP_PDU_MSADDR 3
67 #define IAPP_PDU_CAPABILITY 4
68 #define IAPP_PDU_ANNOUNCEINT 5
69 #define IAPP_PDU_HOTIMEOUT 6
70 #define IAPP_PDU_MESSAGEID 7
71 #define IAPP_PDU_PHYTYPE 0x10
72 #define IAPP_PDU_REGDOMAIN 0x11
73 #define IAPP_PDU_CHANNEL 0x12
74 #define IAPP_PDU_BEACONINT 0x13
75 #define IAPP_PDU_OUIIDENT 0x80
76 #define IAPP_PDU_AUTHINFO 0x81
77
78 #define IAPP_CAP_FORWARDING 0x40
79 #define IAPP_CAP_WEP 0x20
80
81 #define IAPP_PHY_PROP 0x00
82 #define IAPP_PHY_FHSS 0x01
83 #define IAPP_PHY_DSSS 0x02
84 #define IAPP_PHY_IR 0x03
85 #define IAPP_PHY_OFDM 0x04
86
87 #define IAPP_DOM_FCC 0x10
88 #define IAPP_DOM_IC 0x20
89 #define IAPP_DOM_ETSI 0x30
90 #define IAPP_DOM_SPAIN 0x31
91 #define IAPP_DOM_FRANCE 0x32
92 #define IAPP_DOM_MKK 0x40
93
94 #define IAPP_AUTH_STATUS 0x01
95 #define IAPP_AUTH_USERNAME 0x02
96 #define IAPP_AUTH_PROVNAME 0x03
97 #define IAPP_AUTH_RXPKTS 0x04
98 #define IAPP_AUTH_TXPKTS 0x05
99 #define IAPP_AUTH_RXBYTES 0x06
100 #define IAPP_AUTH_TXBYTES 0x07
101 #define IAPP_AUTH_LOGINTIME 0x08
102 #define IAPP_AUTH_TIMELIMIT 0x09
103 #define IAPP_AUTH_VOLLIMIT 0x0a
104 #define IAPP_AUTH_ACCCYCLE 0x0b
105 #define IAPP_AUTH_RXGWORDS 0x0c
106 #define IAPP_AUTH_TXGWORDS 0x0d
107 #define IAPP_AUTH_IPADDR 0x0e
108 #define IAPP_AUTH_TRAILER 0xff
109
110
111 typedef struct _e_iapphdr {
112         guint8 ia_version;
113         guint8 ia_type;
114 } e_iapphdr;
115
116 typedef struct _e_pduhdr {
117         guint8 pdu_type;
118         guint8 pdu_len_h;
119         guint8 pdu_len_l;
120 } e_pduhdr;
121
122 static value_string iapp_vals[] = {
123   {IAPP_ANNOUNCE_REQUEST, "Announce Request"},
124   {IAPP_ANNOUNCE_RESPONSE, "Announce Response"},
125   {IAPP_HANDOVER_REQUEST, "Handover Request"},
126   {IAPP_HANDOVER_RESPONSE, "Handover Response"},
127   {0, NULL}};
128
129 static value_string iapp_pdu_type_vals[] = {
130   {IAPP_PDU_SSID, "Network Name"},
131   {IAPP_PDU_BSSID, "BSSID"},
132   {IAPP_PDU_OLDBSSID, "Old BSSID"},
133   {IAPP_PDU_MSADDR, "Mobile Station Address"},
134   {IAPP_PDU_CAPABILITY, "Capabilities"},
135   {IAPP_PDU_ANNOUNCEINT, "Announce Interval"},
136   {IAPP_PDU_HOTIMEOUT, "Handover Timeout"},
137   {IAPP_PDU_MESSAGEID, "Message ID"},
138   {IAPP_PDU_PHYTYPE, "PHY Type"},
139   {IAPP_PDU_REGDOMAIN, "Regulatory Domain"},
140   {IAPP_PDU_CHANNEL, "Radio Channel"},
141   {IAPP_PDU_BEACONINT, "Beacon Interval"},
142   {IAPP_PDU_OUIIDENT, "OUI Identifier"},
143   {IAPP_PDU_AUTHINFO, "ELSA Authentication Info"},
144   {0, NULL}};
145
146 static value_string iapp_cap_vals[] = {
147   {IAPP_CAP_FORWARDING, "Forwarding"},
148   {IAPP_CAP_WEP, "WEP"},
149   {0, NULL}};
150
151 static value_string iapp_phy_vals[] = {
152   {IAPP_PHY_PROP, "Proprietary"},
153   {IAPP_PHY_FHSS, "FHSS"},
154   {IAPP_PHY_DSSS, "DSSS"},
155   {IAPP_PHY_IR, "Infrared"},
156   {IAPP_PHY_OFDM, "OFDM"},
157   {0, NULL}};
158
159 static value_string iapp_dom_vals[] = {
160   {IAPP_DOM_FCC, "FCC (USA)"},
161   {IAPP_DOM_IC, "IC (Canada)"},
162   {IAPP_DOM_ETSI, "ETSI (Europe)"},
163   {IAPP_DOM_SPAIN, "Spain"},
164   {IAPP_DOM_FRANCE, "France"},
165   {IAPP_DOM_MKK, "MKK (Japan)"},
166   {0, NULL}};
167
168 static value_string iapp_auth_type_vals[] = {
169   {IAPP_AUTH_STATUS, "Status"},
170   {IAPP_AUTH_USERNAME, "User Name"},
171   {IAPP_AUTH_PROVNAME, "Provider Name"},
172   {IAPP_AUTH_RXPKTS, "Received Packets"},
173   {IAPP_AUTH_TXPKTS, "Transmitted Packets"},
174   {IAPP_AUTH_RXBYTES, "Received Octets"},
175   {IAPP_AUTH_TXBYTES, "Transmitted Octets"},
176   {IAPP_AUTH_LOGINTIME, "Session Time"},
177   {IAPP_AUTH_TIMELIMIT, "Time Limit"},
178   {IAPP_AUTH_VOLLIMIT, "Volume Limit"},
179   {IAPP_AUTH_ACCCYCLE, "Accounting Cycle"},
180   {IAPP_AUTH_TRAILER, "Authenticator"},
181   {IAPP_AUTH_RXGWORDS, "Received Gigawords"},
182   {IAPP_AUTH_TXGWORDS, "Transmitted Gigawords"},
183   {IAPP_AUTH_IPADDR, "Client IP Address"},
184   {0, NULL}};
185
186
187 static gchar textbuffer[2000];
188
189 static gchar*
190 iaconvertbufftostr(gchar *dest, tvbuff_t *tvb, int offset, int length)
191 {
192 /*converts the raw buffer into printable text */
193         guint32 i;
194         guint32 totlen=0;
195         const guint8 *pd = tvb_get_ptr(tvb, offset, length);
196
197         dest[0]='"';
198         dest[1]=0;
199         totlen=1;
200         for (i=0; i < (guint32)length; i++)
201         {
202                 if( isalnum((int)pd[i])||ispunct((int)pd[i])
203                                 ||((int)pd[i]==' '))            {
204                         dest[totlen]=(gchar)pd[i];
205                         totlen++;
206                 }
207                 else
208                 {
209                         sprintf(&(dest[totlen]), "\\%03o", pd[i]);
210                         totlen=totlen+strlen(&(dest[totlen]));
211                 }
212         }
213         dest[totlen]='"';
214         dest[totlen+1]=0;
215         return dest;
216 }
217
218 /* dissect a capability bit field */
219
220 static void dissect_caps(proto_item *pitem, tvbuff_t *tvb, int offset)
221 {
222         proto_tree *captree;
223         int bit, val, z, thisbit;
224         gchar *strval, bitval[20];
225
226         captree = proto_item_add_subtree(pitem, ett_iapp_cap);
227         val = tvb_get_guint8(tvb, offset + 3);
228
229         bitval[8] = '\0';
230         for (bit = 7; bit >= 0; bit--)
231         {
232                 strval = match_strval(1 << bit, iapp_cap_vals);
233                 if (strval)
234                 {
235                         thisbit = (val & (1 << bit)) ? 1 : 0;
236                         for (z = 0; z < 7; z++)
237                                 if (z == 7 - bit)
238                                         bitval[z] = thisbit + '0';
239                                 else
240                                         bitval[z] = '.';
241                         proto_tree_add_text(captree, tvb, offset + 3, 1, "%s %s: %s",
242                                 bitval, strval, thisbit ? "Yes" : "No");
243                 }
244         }
245 }
246
247 static gchar*
248 authval_to_str(int type, int len, tvbuff_t *tvb, int offset)
249 {
250         gchar *run;
251         int z, val;
252
253         run = textbuffer;
254         run += sprintf(run, "Value: ");
255
256         switch (type)
257         {
258                 case IAPP_AUTH_STATUS:
259                         strcpy(textbuffer, tvb_get_guint8(tvb, offset + 3) ? "Authenticated" : "Not authenticated");
260                         break;
261                 case IAPP_AUTH_USERNAME:
262                 case IAPP_AUTH_PROVNAME:
263                         iaconvertbufftostr(run, tvb, offset + 3, len);
264                         break;
265                 case IAPP_AUTH_RXPKTS:
266                 case IAPP_AUTH_TXPKTS:
267                 case IAPP_AUTH_RXBYTES:
268                 case IAPP_AUTH_TXBYTES:
269                 case IAPP_AUTH_RXGWORDS:
270                 case IAPP_AUTH_TXGWORDS:
271                 case IAPP_AUTH_VOLLIMIT:
272                         val = tvb_get_ntohl(tvb, offset + 3);
273                         run += sprintf(run, "%d", val);
274                         break;
275                 case IAPP_AUTH_LOGINTIME:
276                 case IAPP_AUTH_TIMELIMIT:
277                 case IAPP_AUTH_ACCCYCLE:
278                         val = tvb_get_ntohl(tvb, offset + 3);
279                         run += sprintf(run, "%d seconds", val);
280                         break;
281                 case IAPP_AUTH_IPADDR:
282                         run += sprintf(run, "%d.%d.%d.%d",
283                                 tvb_get_guint8(tvb, offset + 3),
284                                 tvb_get_guint8(tvb, offset + 4),
285                                 tvb_get_guint8(tvb, offset + 5),
286                                 tvb_get_guint8(tvb, offset + 6));
287                         break;
288                 case IAPP_AUTH_TRAILER:
289                         for (z = 0; z < len; z++)
290                         run += sprintf(run, " %02x", tvb_get_guint8(tvb, offset + 3 + z));
291                         break;
292         }
293
294         return textbuffer;
295 }
296
297 /* dissect authentication info */
298
299 static void dissect_authinfo(proto_item *pitem, tvbuff_t *tvb, int offset, int sumlen)
300 {
301         proto_tree *authtree;
302         e_pduhdr pduhdr;
303         gchar *authstrval, *valstr;
304         int len;
305
306         authtree = proto_item_add_subtree(pitem, ett_iapp_auth);
307
308         while (sumlen > 0)
309         {
310                 tvb_memcpy(tvb, (guint8 *)&pduhdr, offset, sizeof(e_pduhdr));
311                 len = (((int)pduhdr.pdu_len_h) << 8) + pduhdr.pdu_len_l;
312
313                 authstrval = val_to_str(pduhdr.pdu_type, iapp_auth_type_vals,
314                         "Unknown PDU Type");
315                 valstr = authval_to_str(pduhdr.pdu_type, len, tvb, offset);
316                 proto_tree_add_text(authtree, tvb, offset, len + 3, "%s(%d) %s",
317                         authstrval, pduhdr.pdu_type, valstr);
318
319                 sumlen -= (len + 3);
320                 offset += (len + 3);
321         }
322 }
323
324 /* get displayable values of PDU contents */
325
326 static int is_fhss = 0;
327
328 static gchar*
329 pduval_to_str(int type, int len, tvbuff_t *tvb, int offset)
330 {
331         gchar *run;
332         const guint8 *mac;
333         int z, val;
334         gchar *strval;
335
336         run = textbuffer;
337         run += sprintf(run, "Value: ");
338
339         switch (type)
340         {
341                 case IAPP_PDU_SSID:
342                         iaconvertbufftostr(run, tvb, offset + 3, len);
343                         break;
344                 case IAPP_PDU_BSSID:
345                 case IAPP_PDU_OLDBSSID:
346                 case IAPP_PDU_MSADDR:
347                         mac = tvb_get_ptr(tvb, offset + 3, len);
348                         for (z = 0; z < len; z++)
349                                 run += sprintf(run, "%s%02x", z ? ":" : "", mac[z]);
350                         break;
351                 case IAPP_PDU_CAPABILITY:
352                 {
353                         int mask, first = 1;
354
355                         val = tvb_get_guint8(tvb, offset + 3);
356                         run += sprintf(run, "%02x (", val);
357                         for (mask = 0x80; mask; mask >>= 1)
358                                 if (val & mask)
359                                 {
360                                         strval = match_strval(mask, iapp_cap_vals);
361                                         if (strval)
362                                         {
363                                                 if (!first)
364                                                         run += sprintf(run, " ");
365                                                 run += sprintf(run, strval);
366                                         }
367                                 }
368                         run += sprintf(run, ")");
369                         break;
370                 }
371                 case IAPP_PDU_ANNOUNCEINT:
372                         val = tvb_get_ntohs(tvb, offset + 3);
373                         run += sprintf(run, "%d seconds", val);
374                         break;
375                 case IAPP_PDU_HOTIMEOUT:
376                 case IAPP_PDU_BEACONINT:
377                         val = tvb_get_ntohs(tvb, offset + 3);
378                         run += sprintf(run, "%d Kus", val);
379                         break;
380                 case IAPP_PDU_MESSAGEID:
381                         val = tvb_get_ntohs(tvb, offset + 3);
382                         run += sprintf(run, "%d", val);
383                         break;
384                 case IAPP_PDU_PHYTYPE:
385                         val = tvb_get_guint8(tvb, offset + 3);
386                         strval = val_to_str(val, iapp_phy_vals, "Unknown");
387                         run += sprintf(run, strval);
388                         is_fhss = (val == IAPP_PHY_FHSS);
389                         break;
390                 case IAPP_PDU_REGDOMAIN:
391                         val = tvb_get_guint8(tvb, offset + 3);
392                         strval = val_to_str(val, iapp_dom_vals, "Unknown");
393                         run += sprintf(run, strval);
394                         break;
395                 case IAPP_PDU_CHANNEL:
396                         val = tvb_get_guint8(tvb, offset + 3);
397                         if (is_fhss)
398                                 run += sprintf(run, "Pattern set %d, sequence %d",
399                                                 ((val >> 6) & 3) + 1, (val & 31) + 1);
400                         else
401                                 run += sprintf(run, "%d", val);
402                         break;
403                 case IAPP_PDU_OUIIDENT:
404                         for (val = z = 0; z < 3; z++)
405                                 val = (val << 8) | tvb_get_guint8(tvb, offset + 3 + z);
406                         strval = val_to_str(val, oui_vals, "Unknown");
407                         run += sprintf(run, strval);
408                         break;
409         }
410
411         return textbuffer;
412 }
413
414 /* code to dissect a list of PDUs */
415
416 static void
417 dissect_pdus(tvbuff_t *tvb, int offset, proto_tree *pdutree, int pdulen)
418 {
419         e_pduhdr pduhdr;
420         int len;
421         gchar *pdustrval, *valstr;
422         proto_item *ti;
423
424         if (!pdulen)
425         {
426                 proto_tree_add_text(pdutree, tvb, offset, 0, "No PDUs found");
427                 return;
428         }
429
430         while (pdulen > 0)
431         {
432                 tvb_memcpy(tvb, (guint8 *)&pduhdr, offset, sizeof(e_pduhdr));
433                 len = (((int)pduhdr.pdu_len_h) << 8) + pduhdr.pdu_len_l;
434
435                 pdustrval = val_to_str(pduhdr.pdu_type, iapp_pdu_type_vals,
436                         "Unknown PDU Type");
437                 valstr = pduval_to_str(pduhdr.pdu_type, len, tvb, offset);
438                 ti = proto_tree_add_text(pdutree, tvb, offset, len + 3, "%s(%d) %s",
439                         pdustrval, pduhdr.pdu_type, valstr);
440
441                 if (pduhdr.pdu_type == IAPP_PDU_CAPABILITY)
442                         dissect_caps(ti, tvb, offset);
443
444                 if (pduhdr.pdu_type == IAPP_PDU_AUTHINFO)
445                         dissect_authinfo(ti, tvb, offset + 3, len);
446
447                 pdulen -= (len + 3);
448                 offset += (len + 3);
449         }
450 }
451
452 /* code to dissect an IAPP packet */
453 static void
454 dissect_iapp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
455 {
456         proto_item *ti, *pdutf;
457         proto_tree *iapp_tree, *pdutree;
458         e_iapphdr ih;
459         int ia_version;
460         int ia_type;
461         gchar *codestrval;
462
463         if (check_col(pinfo->cinfo, COL_PROTOCOL))
464                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "IAPP");
465
466         if (check_col(pinfo->cinfo, COL_INFO))
467                 col_clear(pinfo->cinfo, COL_INFO);
468
469         tvb_memcpy(tvb, (guint8 *)&ih, 0, sizeof(e_iapphdr));
470
471         ia_version = (int)ih.ia_version;
472         ia_type = (int)ih.ia_type;
473         codestrval = val_to_str(ia_type, iapp_vals,  "Unknown Packet");
474         if (check_col(pinfo->cinfo, COL_INFO))
475         {
476                 col_add_fstr(pinfo->cinfo, COL_INFO, "%s(%d) (version=%d)",
477                              codestrval, ia_type, ia_version);
478         }
479
480         if (tree)
481         {
482                 ti = proto_tree_add_item(tree, proto_iapp, tvb, 0, -1, FALSE);
483                 iapp_tree = proto_item_add_subtree(ti, ett_iapp);
484
485                 /* common header for all IAPP frames */
486
487                 proto_tree_add_uint(iapp_tree, hf_iapp_version, tvb, 0, 1,
488                         ih.ia_version);
489                 proto_tree_add_uint_format(iapp_tree, hf_iapp_type, tvb, 1, 1,
490                         ih.ia_type, "Type: %s(%d)", codestrval, ia_type);
491
492                 pdutf = proto_tree_add_text(iapp_tree, tvb, 2, -1,
493                                 "Protocol data units");
494                 pdutree = proto_item_add_subtree(pdutf, ett_iapp_pdu);
495
496                 if (pdutree)
497                 {
498                         dissect_pdus(tvb, 2, pdutree,
499                                 tvb_length_remaining(tvb, 2));
500                 }
501         }
502 }
503
504
505 /* Register the protocol with Ethereal */
506
507 /* this format is require because a script is used to build the C function
508    that calls all the protocol registration.
509 */
510
511 void
512 proto_register_iapp(void)
513 {
514
515 /* Setup list of header fields  See Section 1.6.1 for details*/
516         static hf_register_info hf[] = {
517                 { &hf_iapp_version,
518                         { "Version", "iapp.version", FT_UINT8, BASE_DEC, NULL, 0x00, "", HFILL }
519                 },
520                 { &hf_iapp_type,
521                         { "type", "iapp.type", FT_UINT8, BASE_DEC, NULL, 0x00, "", HFILL }
522                 },
523         };
524
525 /* Setup protocol subtree array */
526         static gint *ett[] = {
527                 &ett_iapp,
528                 &ett_iapp_pdu,
529                 &ett_iapp_cap,
530                 &ett_iapp_auth
531         };
532
533 /* Register the protocol name and description */
534         proto_iapp = proto_register_protocol("Inter-Access-Point Protocol",
535             "IAPP", "iapp");
536
537 /* Required function calls to register the header fields and subtrees used */
538         proto_register_field_array(proto_iapp, hf, array_length(hf));
539         proto_register_subtree_array(ett, array_length(ett));
540 }
541
542
543 /* If this dissector uses sub-dissector registration add a registration routine.
544    This format is required because a script is used to find these routines and
545    create the code that calls these routines.
546 */
547 void
548 proto_reg_handoff_iapp(void)
549 {
550         dissector_handle_t iapp_handle;
551
552         iapp_handle = create_dissector_handle(dissect_iapp, proto_iapp);
553         dissector_add("udp.port", UDP_PORT_IAPP, iapp_handle);
554 }