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