Fix the Frame Relay dissector to call subdissectors regardless of
[obnox/wireshark/wip.git] / packet-bootp.c
1 /* packet-bootp.c
2  * Routines for BOOTP/DHCP packet disassembly
3  * Gilbert Ramirez <gram@xiexie.org>
4  *
5  * $Id: packet-bootp.c,v 1.44 2001/01/03 22:49:06 guy Exp $
6  *
7  * The information used comes from:
8  * RFC  951: Bootstrap Protocol
9  * RFC 1542: Clarifications and Extensions for the Bootstrap Protocol
10  * RFC 2131: Dynamic Host Configuration Protocol
11  * RFC 2132: DHCP Options and BOOTP Vendor Extensions
12  * RFC 2489: Procedure for Defining New DHCP Options
13  * BOOTP and DHCP Parameters
14  *     http://www.isi.edu/in-notes/iana/assignments/bootp-dhcp-parameters
15  *
16  * Ethereal - Network traffic analyzer
17  * By Gerald Combs <gerald@zing.org>
18  * Copyright 1998 Gerald Combs
19  *
20  * 
21  * This program is free software; you can redistribute it and/or
22  * modify it under the terms of the GNU General Public License
23  * as published by the Free Software Foundation; either version 2
24  * of the License, or (at your option) any later version.
25  * 
26  * This program is distributed in the hope that it will be useful,
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29  * GNU General Public License for more details.
30  * 
31  * You should have received a copy of the GNU General Public License
32  * along with this program; if not, write to the Free Software
33  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
34  */
35
36 #ifdef HAVE_CONFIG_H
37 # include "config.h"
38 #endif
39
40 #ifdef HAVE_SYS_TYPES_H
41 # include <sys/types.h>
42 #endif
43
44 #include <string.h>
45 #include <glib.h>
46 #include "packet.h"
47 #include "packet-arp.h"
48
49 static int proto_bootp = -1;
50 static int hf_bootp_type = -1;
51 static int hf_bootp_hw_type = -1;
52 static int hf_bootp_hw_len = -1;
53 static int hf_bootp_hops = -1;
54 static int hf_bootp_id = -1;
55 static int hf_bootp_secs = -1;
56 static int hf_bootp_flag = -1;
57 static int hf_bootp_ip_client = -1;
58 static int hf_bootp_ip_your = -1;
59 static int hf_bootp_ip_server = -1;
60 static int hf_bootp_ip_relay = -1;
61 static int hf_bootp_hw_addr = -1;
62 static int hf_bootp_server = -1;
63 static int hf_bootp_file = -1;
64 static int hf_bootp_cookie = -1;
65 static int hf_bootp_dhcp = -1;
66
67 static guint ett_bootp = -1;
68 static guint ett_bootp_option = -1;
69
70 #define UDP_PORT_BOOTPS  67
71
72 enum field_type { none, ipv4, string, toggle, yes_no, special, opaque,
73         time_in_secs,
74         val_u_byte, val_u_short, val_u_long,
75         val_s_long };
76
77 struct opt_info {
78         char    *text;
79         enum field_type ftype;
80 };
81
82 #define NUM_OPT_INFOS 128
83 #define NUM_O63_SUBOPTS 11
84
85 static int dissect_netware_ip_suboption(proto_tree *v_tree, tvbuff_t *tvb,
86     int optp);
87
88 static const char *
89 get_dhcp_type(guint8 byte)
90 {
91         static const char       *opt53_text[] = {
92                 "Unknown Message Type",
93                 "Discover",
94                 "Offer",
95                 "Request",
96                 "Decline",
97                 "ACK",
98                 "NAK",
99                 "Release",
100                 "Inform"
101         };
102         int i;
103
104         if (byte > 0 && byte < (sizeof opt53_text / sizeof opt53_text[0]))
105                 i = byte;
106         else
107                 i = 0;
108         return opt53_text[i];
109 }
110
111 /* Returns the number of bytes consumed by this option. */
112 static int
113 bootp_option(tvbuff_t *tvb, proto_tree *bp_tree, int voff, int eoff)
114 {
115         char                    *text;
116         enum field_type         ftype;
117         u_char                  code = tvb_get_guint8(tvb, voff);
118         int                     vlen;
119         u_char                  byte;
120         int                     i,optp, consumed;
121         u_long                  time_secs;
122         proto_tree              *v_tree;
123         proto_item              *vti;
124
125         static const value_string nbnt_vals[] = {
126             {0x1,   "B-node" },
127             {0x2,   "P-node" },
128             {0x4,   "M-node" },
129             {0x8,   "H-node" },
130             {0,     NULL     } };
131
132         static struct opt_info opt[] = {
133                 /*   0 */ { "Padding",                                                          none },
134                 /*   1 */ { "Subnet Mask",                                                      ipv4 },
135                 /*   2 */ { "Time Offset",                                                      val_s_long },
136                 /*   3 */ { "Router",                                                           ipv4 },
137                 /*   4 */ { "Time Server",                                                      ipv4 },
138                 /*   5 */ { "Name Server",                                                      ipv4 },
139                 /*   6 */ { "Domain Name Server",                                       ipv4 },
140                 /*   7 */ { "Log Server",                                                       ipv4 },
141                 /*   8 */ { "Cookie Server",                                            ipv4 },
142                 /*   9 */ { "LPR Server",                                                       ipv4 },
143                 /*  10 */ { "Impress Server",                                           ipv4 },
144                 /*  11 */ { "Resource Location Server",                         ipv4 },
145                 /*  12 */ { "Host Name",                                                        string },
146                 /*  13 */ { "Boot File Size",                                           val_u_short },
147                 /*  14 */ { "Merit Dump File",                                          string },
148                 /*  15 */ { "Domain Name",                                                      string },
149                 /*  16 */ { "Swap Server",                                                      ipv4 },
150                 /*  17 */ { "Root Path",                                                        string },
151                 /*  18 */ { "Extensions Path",                                          string },
152                 /*  19 */ { "IP Forwarding",                                            toggle },
153                 /*  20 */ { "Non-Local Source Routing",                         toggle },
154                 /*  21 */ { "Policy Filter",                                            special },
155                 /*  22 */ { "Maximum Datagram Reassembly Size",         val_u_short },
156                 /*  23 */ { "Default IP Time-to-Live",                          val_u_byte },
157                 /*  24 */ { "Path MTU Aging Timeout",                           time_in_secs },
158                 /*  25 */ { "Path MTU Plateau Table",                           val_u_short },
159                 /*  26 */ { "Interface MTU",                                            val_u_short },
160                 /*  27 */ { "All Subnets are Local",                            yes_no },
161                 /*  28 */ { "Broadcast Address",                                        ipv4 },
162                 /*  29 */ { "Perform Mask Discovery",                           toggle },
163                 /*  30 */ { "Mask Supplier",                                            yes_no },
164                 /*  31 */ { "Perform Router Discover",                          toggle },
165                 /*  32 */ { "Router Solicitation Address",                      ipv4 },
166                 /*  33 */ { "Static Route",                                                     special },
167                 /*  34 */ { "Trailer Encapsulation",                            toggle },
168                 /*  35 */ { "ARP Cache Timeout",                                        time_in_secs },
169                 /*  36 */ { "Ethernet Encapsulation",                           toggle },
170                 /*  37 */ { "TCP Default TTL",                                          val_u_byte },
171                 /*  38 */ { "TCP Keepalive Interval",                           time_in_secs },
172                 /*  39 */ { "TCP Keepalive Garbage",                            toggle },
173                 /*  40 */ { "Network Information Service Domain",       string },
174                 /*  41 */ { "Network Information Service Servers",      ipv4 },
175                 /*  42 */ { "Network Time Protocol Servers",            ipv4 },
176                 /*  43 */ { "Vendor-Specific Information",                      special },
177                 /*  44 */ { "NetBIOS over TCP/IP Name Server",          ipv4 },
178                 /*  45 */ { "NetBIOS over TCP/IP Datagram Distribution Name Server", ipv4 },
179                 /*  46 */ { "NetBIOS over TCP/IP Node Type",            special },
180                 /*  47 */ { "NetBIOS over TCP/IP Scope",                        string },
181                 /*  48 */ { "X Window System Font Server",                      ipv4 },
182                 /*  49 */ { "X Window System Display Manager",          ipv4 },
183                 /*  50 */ { "Requested IP Address",                                     ipv4 },
184                 /*  51 */ { "IP Address Lease Time",                            time_in_secs },
185                 /*  52 */ { "Option Overload",                                          special },
186                 /*  53 */ { "DHCP Message Type",                                        special },
187                 /*  54 */ { "Server Identifier",                                        ipv4 },
188                 /*  55 */ { "Parameter Request List",                           special },
189                 /*  56 */ { "Message",                                                          string },
190                 /*  57 */ { "Maximum DHCP Message Size",                        val_u_short },
191                 /*  58 */ { "Renewal Time Value",                                       time_in_secs },
192                 /*  59 */ { "Rebinding Time Value",                                     time_in_secs },
193                 /*  60 */ { "Vendor class identifier",                          opaque },
194                 /*  61 */ { "Client identifier",                                        special },
195                 /*  62 */ { "Novell/Netware IP domain",                                 string },
196                 /*  63 */ { "Novell Options",   special },
197                 /*  64 */ { "Network Information Service+ Domain",      string },
198                 /*  65 */ { "Network Information Service+ Servers",     ipv4 },
199                 /*  66 */ { "TFTP Server Name",                                         string },
200                 /*  67 */ { "Bootfile name",                                            string },
201                 /*  68 */ { "Mobile IP Home Agent",                                     ipv4 },
202                 /*  69 */ { "SMTP Server",                                                      ipv4 },
203                 /*  70 */ { "POP3 Server",                                                      ipv4 },
204                 /*  71 */ { "NNTP Server",                                                      ipv4 },
205                 /*  72 */ { "Default WWW Server",                                       ipv4 },
206                 /*  73 */ { "Default Finger Server",                            ipv4 },
207                 /*  74 */ { "Default IRC Server",                                       ipv4 },
208                 /*  75 */ { "StreetTalk Server",                                        ipv4 },
209                 /*  76 */ { "StreetTalk Directory Assistance Server", ipv4 },
210                 /*  77 */ { "User Class Information",                           opaque },
211                 /*  78 */ { "Directory Agent Information",                      opaque },
212                 /*  79 */ { "Service Location Agent Scope",                     opaque },
213                 /*  80 */ { "Naming Authority",                                         opaque },
214                 /*  81 */ { "Client Fully Qualified Domain Name",       opaque },
215                 /*  82 */ { "Agent Circuit ID",                                         opaque },
216                 /*  83 */ { "Agent Remote ID",                                          opaque },
217                 /*  84 */ { "Agent Subnet Mask",                                        opaque },
218                 /*  85 */ { "Novell Directory Services Servers",        opaque },
219                 /*  86 */ { "Novell Directory Services Tree Name",      opaque },
220                 /*  87 */ { "Novell Directory Services Context",        opaque },
221                 /*  88 */ { "IEEE 1003.1 POSIX Timezone",                       opaque },
222                 /*  89 */ { "Fully Qualified Domain Name",                      opaque },
223                 /*  90 */ { "Authentication",                                           opaque },
224                 /*  91 */ { "Vines TCP/IP Server Option",                       opaque },
225                 /*  92 */ { "Server Selection Option",                          opaque },
226                 /*  93 */ { "Client System Architecture",                       opaque },
227                 /*  94 */ { "Client Network Device Interface",          opaque },
228                 /*  95 */ { "Lightweight Directory Access Protocol",    opaque },
229                 /*  96 */ { "IPv6 Transitions",                                         opaque },
230                 /*  97 */ { "UUID/GUID-based Client Identifier",        opaque },
231                 /*  98 */ { "Open Group's User Authentication",         opaque },
232                 /*  99 */ { "Unassigned",                                                       opaque },
233                 /* 100 */ { "Printer Name",                                                     opaque },
234                 /* 101 */ { "MDHCP multicast address",                          opaque },
235                 /* 102 */ { "Removed/unassigned",                                       opaque },
236                 /* 103 */ { "Removed/unassigned",                                       opaque },
237                 /* 104 */ { "Removed/unassigned",                                       opaque },
238                 /* 105 */ { "Removed/unassigned",                                       opaque },
239                 /* 106 */ { "Removed/unassigned",                                       opaque },
240                 /* 107 */ { "Removed/unassigned",                                       opaque },
241                 /* 108 */ { "Swap Path Option",                                         opaque },
242                 /* 109 */ { "Unassigned",                                                       opaque },
243                 /* 110 */ { "IPX Compability",                                          opaque },
244                 /* 111 */ { "Unassigned",                                                       opaque },
245                 /* 112 */ { "Netinfo Parent Server Address",            opaque },
246                 /* 113 */ { "Netinfo Parent Server Tag",                        opaque },
247                 /* 114 */ { "URL",                                                                      opaque },
248                 /* 115 */ { "DHCP Failover Protocol",                           opaque },
249                 /* 116 */ { "DHCP Auto-Configuration",                          opaque },
250                 /* 117 */ { "Unassigned",                                                       opaque },
251                 /* 118 */ { "Unassigned",                                                       opaque },
252                 /* 119 */ { "Unassigned",                                                       opaque },
253                 /* 120 */ { "Unassigned",                                                       opaque },
254                 /* 121 */ { "Unassigned",                                                       opaque },
255                 /* 122 */ { "Unassigned",                                                       opaque },
256                 /* 123 */ { "Unassigned",                                                       opaque },
257                 /* 124 */ { "Unassigned",                                                       opaque },
258                 /* 125 */ { "Unassigned",                                                       opaque },
259                 /* 126 */ { "Extension",                                                        opaque },
260                 /* 127 */ { "Extension",                                                        opaque }
261         };
262
263         /* Options whose length isn't "vlen + 2". */
264         switch (code) {
265
266         case 0:         /* Padding */
267                 /* check how much padding we have */
268                 for (i = voff + 1; i < eoff; i++ ) {
269                         if (tvb_get_guint8(tvb, i) != 0) {
270                                 break;
271                         }
272                 }
273                 i = i - voff;
274                 if (bp_tree != NULL)
275                         proto_tree_add_text(bp_tree, tvb, voff, i, "Padding");
276                 consumed = i;
277                 return consumed;
278                 break;
279
280         case 255:       /* End Option */
281                 if (bp_tree != NULL)
282                         proto_tree_add_text(bp_tree, tvb, voff, 1, "End Option");
283                 consumed = 1;
284                 return consumed;
285         }
286
287         /*
288          * Get the length of the option, and the number of bytes it
289          * consumes (the length doesn't include the option code or
290          * length bytes).
291          */
292         vlen = tvb_get_guint8(tvb, voff+1);
293         consumed = vlen + 2;
294         if (bp_tree == NULL) {
295                 /* Don't put anything in the protocol tree. */
296                 return consumed;
297         }
298
299         text = opt[code].text;
300         /* Special cases */
301         switch (code) {
302
303         case 21:        /* Policy Filter */
304                 if (vlen == 8) {
305                         /* one IP address pair */
306                         proto_tree_add_text(bp_tree, tvb, voff, consumed,
307                                 "Option %d: %s = %s/%s", code, text,
308                                 ip_to_str(tvb_get_ptr(tvb, voff+2, 4)),
309                                 ip_to_str(tvb_get_ptr(tvb, voff+6, 4)));
310                 } else {
311                         /* > 1 IP address pair. Let's make a sub-tree */
312                         vti = proto_tree_add_text(bp_tree, tvb, voff,
313                                 consumed, "Option %d: %s", code, text);
314                         v_tree = proto_item_add_subtree(vti, ett_bootp_option);
315                         for (i = voff + 2; i < voff + consumed; i += 8) {
316                                 proto_tree_add_text(v_tree, tvb, i, 8, "IP Address/Mask: %s/%s",
317                                         ip_to_str(tvb_get_ptr(tvb, i, 4)),
318                                         ip_to_str(tvb_get_ptr(tvb, i+4, 4)));
319                         }
320                 }
321                 break;
322
323         case 33:        /* Static Route */
324                 if (vlen == 8) {
325                         /* one IP address pair */
326                         proto_tree_add_text(bp_tree, tvb, voff, consumed,
327                                 "Option %d: %s = %s/%s", code, text,
328                                 ip_to_str(tvb_get_ptr(tvb, voff+2, 4)),
329                                 ip_to_str(tvb_get_ptr(tvb, voff+6, 4)));
330                 } else {
331                         /* > 1 IP address pair. Let's make a sub-tree */
332                         vti = proto_tree_add_text(bp_tree, tvb, voff,
333                                 consumed, "Option %d: %s", code, text);
334                         v_tree = proto_item_add_subtree(vti, ett_bootp_option);
335                         for (i = voff + 2; i < voff + consumed; i += 8) {
336                                 proto_tree_add_text(v_tree, tvb, i, 8,
337                                         "Destination IP Address/Router: %s/%s",
338                                         ip_to_str(tvb_get_ptr(tvb, i, 4)),
339                                         ip_to_str(tvb_get_ptr(tvb, i+4, 4)));
340                         }
341                 }
342                 break;
343
344         case 43:        /* Vendor-Specific Info */
345                 proto_tree_add_text(bp_tree, tvb, voff, consumed,
346                                 "Option %d: %s", code, text);
347                 break;
348
349         case 46:        /* NetBIOS-over-TCP/IP Node Type */
350                 byte = tvb_get_guint8(tvb, voff+2);
351                 proto_tree_add_text(bp_tree, tvb, voff, consumed,
352                                 "Option %d: %s = %s", code, text,
353                                 val_to_str(byte, nbnt_vals,
354                                     "Unknown (0x%02x)"));
355                 break;
356                                 
357         case 53:        /* DHCP Message Type */
358                 proto_tree_add_text(bp_tree, tvb, voff, 3, "Option %d: %s = DHCP %s",
359                         code, text, get_dhcp_type(tvb_get_guint8(tvb, voff+2)));
360                 break;
361
362         case 55:        /* Parameter Request List */
363                 vti = proto_tree_add_text(bp_tree, tvb, voff,
364                         vlen + 2, "Option %d: %s", code, text);
365                 v_tree = proto_item_add_subtree(vti, ett_bootp_option);
366                 for (i = 0; i < vlen; i++) {
367                         byte = tvb_get_guint8(tvb, voff+2+i);
368                         if (byte < NUM_OPT_INFOS) {
369                                 proto_tree_add_text(v_tree, tvb, voff+2+i, 1, "%d = %s",
370                                                 byte, opt[byte].text);
371                         } else {
372                                 proto_tree_add_text(vti, tvb, voff+2+i, 1,
373                                         "Unknown Option Code: %d", byte);
374                         }
375                 }
376                 break;
377
378         case 61:        /* Client Identifier */
379                 /* We *MAY* use hwtype/hwaddr. If we have 7 bytes, I'll
380                    guess that the first is the hwtype, and the last 6
381                    are the hw addr */
382                 if (vlen == 7) {
383                         guint8 htype;
384
385                         vti = proto_tree_add_text(bp_tree, tvb, voff,
386                                 consumed, "Option %d: %s", code, text);
387                         v_tree = proto_item_add_subtree(vti, ett_bootp_option);
388                         htype = tvb_get_guint8(tvb, voff+2);
389                         proto_tree_add_text(v_tree, tvb, voff+2, 1,
390                                 "Hardware type: %s",
391                                 arphrdtype_to_str(htype,
392                                         "Unknown (0x%02x)"));
393                         proto_tree_add_text(v_tree, tvb, voff+3, 6,
394                                 "Client hardware address: %s",
395                                 arphrdaddr_to_str(tvb_get_ptr(tvb, voff+3, 6),
396                                         6, htype));
397                 } else {
398                         /* otherwise, it's opaque data */
399                         proto_tree_add_text(bp_tree, tvb, voff, consumed,
400                                 "Option %d: %s (%d bytes)", code, text, vlen);
401                 }
402                 break;
403
404         case 63:        /* NetWare/IP options */
405                 vti = proto_tree_add_text(bp_tree, tvb, voff,
406                     consumed, "Option %d: %s", code, text);
407                 v_tree = proto_item_add_subtree(vti, ett_bootp_option);
408
409                 optp = voff+2;
410                 while (optp < voff+consumed)
411                         optp = dissect_netware_ip_suboption(v_tree, tvb, optp);
412                 break;
413
414         default:        /* not special */
415                 break;
416         }
417
418         /* Normal cases */
419         if (code < NUM_OPT_INFOS) {
420                 text = opt[code].text;
421                 ftype = opt[code].ftype;
422
423                 switch (ftype) {
424
425                 case special:
426                         return consumed;
427
428                 case ipv4:
429                         if (vlen == 4) {
430                                 /* one IP address */
431                                 proto_tree_add_text(bp_tree, tvb, voff, consumed,
432                                         "Option %d: %s = %s", code, text,
433                                         ip_to_str(tvb_get_ptr(tvb, voff+2, 4)));
434                         } else {
435                                 /* > 1 IP addresses. Let's make a sub-tree */
436                                 vti = proto_tree_add_text(bp_tree, tvb, voff,
437                                         consumed, "Option %d: %s", code, text);
438                                 v_tree = proto_item_add_subtree(vti, ett_bootp_option);
439                                 for (i = voff + 2; i < voff + consumed; i += 4) {
440                                         proto_tree_add_text(v_tree, tvb, i, 4, "IP Address: %s",
441                                                 ip_to_str(tvb_get_ptr(tvb, i, 4)));
442                                 }
443                         }
444                         break;
445
446                 case string:
447                         /* Fix for non null-terminated string supplied by
448                          * John Lines <John.Lines@aeat.co.uk>
449                          */
450                         proto_tree_add_text(bp_tree, tvb, voff, consumed,
451                                         "Option %d: %s = %.*s", code, text, vlen,
452                                         tvb_get_ptr(tvb, voff+2, consumed-2));
453                         break;
454
455                 case opaque:
456                         proto_tree_add_text(bp_tree, tvb, voff, consumed,
457                                         "Option %d: %s (%d bytes)",
458                                         code, text, vlen);
459                         break;
460
461                 case val_u_short:
462                         if (vlen == 2) {
463                                 /* one u_short */
464                                 proto_tree_add_text(bp_tree, tvb, voff, consumed,
465                                                 "Option %d: %s = %d", code, text,
466                                                 tvb_get_ntohs(tvb, voff+2));
467                         } else {
468                                 /* > 1 u_short */
469                                 vti = proto_tree_add_text(bp_tree, tvb, voff,
470                                         consumed, "Option %d: %s", code, text);
471                                 v_tree = proto_item_add_subtree(vti, ett_bootp_option);
472                                 for (i = voff + 2; i < voff + consumed; i += 2) {
473                                         proto_tree_add_text(v_tree, tvb, i, 4, "Value: %d",
474                                                 tvb_get_ntohs(tvb, i));
475                                 }
476                         }
477                         break;
478
479                 case val_u_long:
480                         proto_tree_add_text(bp_tree, tvb, voff, consumed,
481                                         "Option %d: %s = %d", code, text,
482                                         tvb_get_ntohl(tvb, voff+2));
483                         break;
484
485                 case val_u_byte:
486                         proto_tree_add_text(bp_tree, tvb, voff, consumed,
487                                         "Option %d: %s = %d", code, text,
488                                         tvb_get_guint8(tvb, voff+2));
489                         break;
490
491                 case toggle:
492                         i = tvb_get_guint8(tvb, voff+2);
493                         if (i != 0 && i != 1) {
494                                 proto_tree_add_text(bp_tree, tvb, voff, consumed,
495                                                 "Option %d: %s = Invalid Value %d", code, text,
496                                                 i);
497                         } else {
498                                 proto_tree_add_text(bp_tree, tvb, voff, consumed,
499                                                 "Option %d: %s = %s", code, text,
500                                                 i == 0 ? "Disabled" : "Enabled");
501                         }
502                         break;
503
504                 case yes_no:
505                         i = tvb_get_guint8(tvb, voff+2);
506                         if (i != 0 && i != 1) {
507                                 proto_tree_add_text(bp_tree, tvb, voff, consumed,
508                                                 "Option %d: %s = Invalid Value %d", code, text,
509                                                 i);
510                         } else {
511                                 proto_tree_add_text(bp_tree, tvb, voff, consumed,
512                                                 "Option %d: %s = %s", code, text,
513                                                 i == 0 ? "No" : "Yes");
514                         }
515                         break;
516
517                 case time_in_secs:
518                         time_secs = tvb_get_ntohl(tvb, voff+2);
519                         proto_tree_add_text(bp_tree, tvb, voff, consumed,
520                                 "Option %d: %s = %s", code, text,
521                                 ((time_secs == 0xffffffff) ?
522                                     "infinity" :
523                                     time_secs_to_str(time_secs)));
524                         break;
525
526                 default:
527                         proto_tree_add_text(bp_tree, tvb, voff, consumed,
528                                         "Option %d: %s (%d bytes)", code, text, vlen);
529                 }
530         } else {
531                 proto_tree_add_text(bp_tree, tvb, voff, consumed,
532                                 "Unknown Option Code: %d (%d bytes)", code, vlen);
533         }
534
535         return consumed;
536 }
537
538 static int
539 dissect_netware_ip_suboption(proto_tree *v_tree, tvbuff_t *tvb, int optp)
540 {
541         guint8 subopt;
542         guint8 subopt_len;
543         int slask;
544         proto_tree *o63_v_tree;
545         proto_item *vti;
546
547         struct o63_opt_info { 
548                 char    *truet;
549                 char    *falset;
550                 enum field_type ft;
551         };
552
553         static struct o63_opt_info o63_opt[]= {
554                 /* 0 */ {"","",none},
555                 /* 1 */ {"NWIP does not exist on subnet","",string},
556                 /* 2 */ {"NWIP exist in options area","",string},
557                 /* 3 */ {"NWIP exists in sname/file","",string},
558                 /* 4 */ {"NWIP exists, but too big","",string},
559                 /* 5 */ {"Broadcast for nearest Netware server","Do NOT Broadcast for nearest Netware server",yes_no}, 
560                 /* 6 */ {"Preferred DSS server","",ipv4},
561                 /* 7 */ {"Nearest NWIP server","",ipv4},
562                 /* 8 */ {"Autoretries","",val_u_short},
563                 /* 9 */ {"Autoretry delay, secs ","",val_u_short},
564                 /* 10*/ {"Support NetWare/IP v1.1","Do NOT support NetWare/IP v1.1",yes_no},
565                 /* 11*/ {"Primary DSS ", "" , special}
566         };
567                 
568         subopt = tvb_get_guint8(tvb, optp);
569         if (subopt > NUM_O63_SUBOPTS) {
570                 proto_tree_add_text(v_tree, tvb,optp,1,"Unknown suboption %d", subopt);
571                 optp++;
572         } else {
573                 switch (o63_opt[subopt].ft) {
574
575                 case string:
576                         proto_tree_add_text(v_tree, tvb, optp, 2, "Suboption %d: %s", subopt, o63_opt[subopt].truet);
577                         optp+=2;
578                         break;
579
580                 case yes_no:
581                         if (tvb_get_guint8(tvb, optp+2)==1) {
582                                 proto_tree_add_text(v_tree, tvb, optp, 3, "Suboption %d: %s", subopt, o63_opt[subopt].truet);
583                         } else {
584                                 proto_tree_add_text(v_tree, tvb, optp, 3, "Suboption %d: %s" , subopt, o63_opt[subopt].falset);
585                         }
586                         optp+=3;
587                         break;
588
589                 case special:   
590                         proto_tree_add_text(v_tree, tvb, optp, 6,
591                             "Suboption %d: %s = %s" ,
592                             subopt, o63_opt[subopt].truet,
593                             ip_to_str(tvb_get_ptr(tvb, optp+2, 4)));
594                         optp=optp+6;
595                         break;
596
597                 case val_u_short:
598                         proto_tree_add_text(v_tree, tvb, optp, 3, "Suboption %d: %s = %u",
599                             subopt, o63_opt[subopt].truet,
600                             tvb_get_guint8(tvb, optp+2));       /* XXX - 1 byte? */
601                         optp+=3;
602                         break;
603                                                         
604                 case ipv4:
605                         subopt_len = tvb_get_guint8(tvb, optp+1);
606                         if (subopt_len == 4) {
607                                 /* one IP address */
608                                 proto_tree_add_text(v_tree, tvb, optp, 6,
609                                     "Suboption %d : %s = %s",
610                                     subopt, o63_opt[subopt].truet,
611                                     ip_to_str(tvb_get_ptr(tvb, optp+2, 4)));
612                                 optp=optp+6;
613                         } else {
614                                 /* > 1 IP addresses. Let's make a sub-tree */
615                                 vti = proto_tree_add_text(v_tree, tvb, optp,
616                                     subopt_len+2, "Suboption %d: %s",
617                                     subopt, o63_opt[subopt].truet);
618                                 o63_v_tree = proto_item_add_subtree(vti, ett_bootp_option);
619                                 for (slask = optp + 2 ; slask < optp+subopt_len; slask += 4) {
620                                         proto_tree_add_text(o63_v_tree, tvb, slask, 4, "IP Address: %s",
621                                             ip_to_str(tvb_get_ptr(tvb, slask, 4)));
622                                 }
623                                 optp=slask;
624                         }
625                         break;
626                 default:
627                         proto_tree_add_text(v_tree, tvb,optp,1,"Unknown suboption %d", subopt);
628                         optp++;
629                         break;
630                 }
631         }
632         return optp;
633 }
634
635 #define BOOTREQUEST     1
636 #define BOOTREPLY       2
637
638 static const value_string op_vals[] = {
639         { BOOTREQUEST,  "Boot Request" },
640         { BOOTREPLY,    "Boot Reply" },
641         { 0,            NULL }
642 };
643
644 static void
645 dissect_bootp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
646 {
647         proto_tree      *bp_tree = NULL;
648         proto_item      *ti;
649         guint8          op;
650         guint8          htype, hlen;
651         guint8          *haddr;
652         int             voff, eoff; /* vender offset, end offset */
653         guint32         ip_addr;
654         gboolean        is_dhcp = FALSE;
655         const char      *dhcp_type;
656
657         CHECK_DISPLAY_AS_DATA(proto_bootp, tvb, pinfo, tree);
658
659         pinfo->current_proto = "BOOTP/DHCP";
660
661         if (check_col(pinfo->fd, COL_PROTOCOL))
662                 col_set_str(pinfo->fd, COL_PROTOCOL, "BOOTP");
663         if (check_col(pinfo->fd, COL_INFO)) {
664                 /*
665                  * In case we throw an exception fetching the opcode, etc.
666                  */
667                 col_clear(pinfo->fd, COL_INFO);
668         }
669
670         op = tvb_get_guint8(tvb, 0);
671         htype = tvb_get_guint8(tvb, 1);
672         hlen = tvb_get_guint8(tvb, 2);
673         if (check_col(pinfo->fd, COL_INFO)) {
674                 switch (op) {
675
676                 case BOOTREQUEST:
677                         col_add_fstr(pinfo->fd, COL_INFO, "Boot Request from %s",
678                                 arphrdaddr_to_str(tvb_get_ptr(tvb, 28, hlen),
679                                         hlen, htype));
680                         break;
681
682                 case BOOTREPLY:
683                         col_set_str(pinfo->fd, COL_INFO, "Boot Reply");
684                         break;
685
686                 default:
687                         col_add_fstr(pinfo->fd, COL_INFO, "Unknown BOOTP message type (%u)",
688                             op);
689                         break;
690                 }
691         }
692
693         if (tree) {
694                 ti = proto_tree_add_item(tree, proto_bootp, tvb, 0,
695                     tvb_length(tvb), FALSE);
696                 bp_tree = proto_item_add_subtree(ti, ett_bootp);
697
698                 proto_tree_add_uint(bp_tree, hf_bootp_type, tvb, 
699                                            0, 1,
700                                            op);
701                 proto_tree_add_uint_format(bp_tree, hf_bootp_hw_type, tvb,
702                                            1, 1,
703                                            htype,
704                                            "Hardware type: %s",
705                                            arphrdtype_to_str(htype,
706                                                              "Unknown (0x%02x)"));
707                 proto_tree_add_uint(bp_tree, hf_bootp_hw_len, tvb,
708                                     2, 1, hlen);
709                 proto_tree_add_item(bp_tree, hf_bootp_hops, tvb,
710                                     3, 1, FALSE);
711                 proto_tree_add_item(bp_tree, hf_bootp_id, tvb,
712                                     4, 4, FALSE);
713                 proto_tree_add_item(bp_tree, hf_bootp_secs, tvb,
714                                     8, 2, FALSE);
715                 proto_tree_add_uint(bp_tree, hf_bootp_flag, tvb,
716                                     10, 2, tvb_get_ntohs(tvb, 10) & 0x8000);
717
718                 proto_tree_add_item(bp_tree, hf_bootp_ip_client, tvb, 
719                                     12, 4, FALSE);
720                 proto_tree_add_item(bp_tree, hf_bootp_ip_your, tvb, 
721                                     16, 4, FALSE);
722                 proto_tree_add_item(bp_tree, hf_bootp_ip_server, tvb,
723                                     20, 4, FALSE);
724                 proto_tree_add_item(bp_tree, hf_bootp_ip_relay, tvb,
725                                     24, 4, FALSE);
726
727                 if (hlen > 0) {
728                         haddr = tvb_get_ptr(tvb, 28, hlen);
729                         proto_tree_add_bytes_format(bp_tree, hf_bootp_hw_addr, tvb, 
730                                                    28, hlen,
731                                                    haddr,
732                                                    "Client hardware address: %s",
733                                                    arphrdaddr_to_str(haddr,
734                                                                      hlen,
735                                                                      htype));
736                 }
737                 else {
738                         proto_tree_add_text(bp_tree,  tvb,
739                                                    28, 0, "Client address not given");
740                 }
741
742                 /* The server host name is optional */
743                 if (tvb_get_guint8(tvb, 44) != '\0') {
744                         proto_tree_add_item(bp_tree, hf_bootp_server, tvb,
745                                                    44, 64, FALSE);
746                 }
747                 else {
748                         proto_tree_add_string_format(bp_tree, hf_bootp_server, tvb,
749                                                    44, 64,
750                                                    tvb_get_ptr(tvb, 44, 1),
751                                                    "Server host name not given");
752                 }
753
754                 /* Boot file */
755                 if (tvb_get_guint8(tvb, 108) != '\0') {
756                         proto_tree_add_item(bp_tree, hf_bootp_file, tvb,
757                                                    108, 128, FALSE);
758                 }
759                 else {
760                         proto_tree_add_string_format(bp_tree, hf_bootp_file, tvb,
761                                                    108, 128,
762                                                    tvb_get_ptr(tvb, 108, 1),
763                                                    "Boot file name not given");
764                 }
765
766                 tvb_memcpy(tvb, (void *)&ip_addr, 236, sizeof(ip_addr));
767                 if (tvb_get_ntohl(tvb, 236) == 0x63825363) {
768                         proto_tree_add_ipv4_format(bp_tree, hf_bootp_cookie, tvb,
769                                             236, 4, ip_addr,
770                                             "Magic cookie: (OK)");
771                 }
772                 else {
773                         proto_tree_add_ipv4(bp_tree, hf_bootp_cookie, tvb,
774                                             236, 4, ip_addr);
775                 }
776         }
777
778         voff = 240;
779         eoff = tvb_reported_length(tvb);
780         while (voff < eoff) {
781                 /* Handle the DHCP option specially here, so that we
782                    can flag DHCP packets as such. */
783                 if (!is_dhcp && tvb_get_guint8(tvb, voff) == 53) {
784                         /*
785                          * We haven't already seen a DHCP Type option, and
786                          * this is a DHCP Type option, so flag this packet
787                          * as DHCP.
788                          */
789                         dhcp_type = get_dhcp_type(tvb_get_guint8(tvb, voff+2));
790                         if (check_col(pinfo->fd, COL_PROTOCOL))
791                                 col_set_str(pinfo->fd, COL_PROTOCOL, "DHCP");
792                         if (check_col(pinfo->fd, COL_INFO))
793                                 col_add_fstr(pinfo->fd, COL_INFO, "DHCP %-8s - Transaction ID 0x%x",
794                                     dhcp_type, tvb_get_ntohl(tvb, 4));
795                         if (tree)
796                                 proto_tree_add_boolean_hidden(bp_tree, hf_bootp_dhcp,
797                                     tvb, 0, 0, 1);
798                         is_dhcp = TRUE;
799                 }
800                 voff += bootp_option(tvb, bp_tree, voff, eoff);
801         }
802 }
803
804 void
805 proto_register_bootp(void)
806 {
807   static hf_register_info hf[] = {
808     { &hf_bootp_dhcp,
809       { "Frame is DHCP",                "bootp.dhcp",    FT_BOOLEAN,
810         BASE_NONE,                      NULL,            0x0,
811         "" }},                            
812                       
813     { &hf_bootp_type,
814       { "Message type",                 "bootp.type",    FT_UINT8,
815          BASE_DEC,                      VALS(op_vals),   0x0,
816         "" }},
817
818     { &hf_bootp_hw_type,
819       { "Hardware type",                "bootp.hw.type", FT_UINT8,
820         BASE_HEX,                       NULL,            0x0,
821         "" }},
822
823     { &hf_bootp_hw_len,
824       { "Hardware address length",      "bootp.hw.len",  FT_UINT8,
825         BASE_DEC,                       NULL,            0x0,
826         "" }},
827
828     { &hf_bootp_hops,
829       { "Hops",                         "bootp.hops",    FT_UINT8,
830         BASE_DEC,                       NULL,            0x0,
831         "" }},
832
833     { &hf_bootp_id,
834       { "Transaction ID",               "bootp.id",      FT_UINT32,
835         BASE_HEX,                        NULL,           0x0,
836         "" }},
837
838     { &hf_bootp_secs,
839       { "Seconds elapsed",              "bootp.secs",    FT_UINT16,
840         BASE_DEC,                        NULL,           0x0,
841         "" }},
842
843     { &hf_bootp_flag,
844       { "Broadcast flag",               "bootp.flag",    FT_UINT16,
845         BASE_HEX,                       NULL,            0x0,
846         "" }},
847
848     { &hf_bootp_ip_client,
849       { "Client IP address",            "bootp.ip.client",FT_IPv4,
850         BASE_NONE,                      NULL,             0x0,
851         "" }},
852
853     { &hf_bootp_ip_your,
854       { "Your (client) IP address",     "bootp.ip.your",  FT_IPv4,
855         BASE_NONE,                      NULL,             0x0,
856         "" }},
857
858     { &hf_bootp_ip_server,
859       { "Next server IP address",       "bootp.ip.server",FT_IPv4,
860         BASE_NONE,                      NULL,             0x0,
861         "" }},
862
863     { &hf_bootp_ip_relay,
864       { "Relay agent IP address",       "bootp.ip.relay", FT_IPv4,
865         BASE_NONE,                      NULL,             0x0,
866         "" }},
867
868     { &hf_bootp_hw_addr,
869       { "Client hardware address",      "bootp.hw.addr", FT_BYTES,
870         BASE_NONE,                      NULL,            0x0,
871         "" }},
872
873     { &hf_bootp_server,
874       { "Server host name",             "bootp.server",  FT_STRING,
875         BASE_NONE,                      NULL,            0x0,
876         "" }},
877
878     { &hf_bootp_file,
879       { "Boot file name",               "bootp.file",    FT_STRING,
880         BASE_NONE,                      NULL,            0x0,
881         "" }},
882
883     { &hf_bootp_cookie,
884       { "Magic cookie",                 "bootp.cookie",  FT_IPv4,
885          BASE_NONE,                     NULL,            0x0,
886         "" }},
887   };
888   static gint *ett[] = {
889     &ett_bootp,
890     &ett_bootp_option,
891   };
892   
893   proto_bootp = proto_register_protocol("Bootstrap Protocol", "BOOTP/DHCP",
894                                         "bootp");
895   proto_register_field_array(proto_bootp, hf, array_length(hf));
896   proto_register_subtree_array(ett, array_length(ett));
897 }
898
899 void
900 proto_reg_handoff_bootp(void)
901 {
902   dissector_add("udp.port", UDP_PORT_BOOTPS, dissect_bootp);
903 }