Remove a bunch of duplicate semicolons.
[obnox/wireshark/wip.git] / packet-bootp.c
1 /* packet-bootp.c
2  * Routines for BOOTP/DHCP packet disassembly
3  * Gilbert Ramirez <gram@alumni.rice.edu>
4  *
5  * $Id: packet-bootp.c,v 1.75 2003/09/02 22:47:57 guy Exp $
6  *
7  * The information used comes from:
8  * RFC  951: Bootstrap Protocol
9  * RFC 1497: BOOTP extensions
10  * RFC 1542: Clarifications and Extensions for the Bootstrap Protocol
11  * RFC 2131: Dynamic Host Configuration Protocol
12  * RFC 2132: DHCP Options and BOOTP Vendor Extensions
13  * RFC 2489: Procedure for Defining New DHCP Options
14  * RFC 3046: DHCP Relay Agent Information Option
15  * RFC 3118: Authentication for DHCP Messages
16  * RFC 3203: DHCP reconfigure extension
17  * BOOTP and DHCP Parameters
18  *     http://www.iana.org/assignments/bootp-dhcp-parameters
19  *
20  * Ethereal - Network traffic analyzer
21  * By Gerald Combs <gerald@ethereal.com>
22  * Copyright 1998 Gerald Combs
23  *
24  * This program is free software; you can redistribute it and/or
25  * modify it under the terms of the GNU General Public License
26  * as published by the Free Software Foundation; either version 2
27  * of the License, or (at your option) any later version.
28  *
29  * This program is distributed in the hope that it will be useful,
30  * but WITHOUT ANY WARRANTY; without even the implied warranty of
31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32  * GNU General Public License for more details.
33  *
34  * You should have received a copy of the GNU General Public License
35  * along with this program; if not, write to the Free Software
36  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
37  */
38
39 #ifdef HAVE_CONFIG_H
40 # include "config.h"
41 #endif
42
43 #include <string.h>
44 #include <glib.h>
45 #include <epan/int-64bit.h>
46 #include <epan/packet.h>
47 #include "packet-arp.h"
48
49 #include "tap.h"
50  
51 static int bootp_dhcp_tap = -1;
52 static int proto_bootp = -1;
53 static int hf_bootp_type = -1;
54 static int hf_bootp_hw_type = -1;
55 static int hf_bootp_hw_len = -1;
56 static int hf_bootp_hops = -1;
57 static int hf_bootp_id = -1;
58 static int hf_bootp_secs = -1;
59 static int hf_bootp_flags = -1;
60 static int hf_bootp_flags_broadcast = -1;
61 static int hf_bootp_flags_reserved = -1;
62 static int hf_bootp_ip_client = -1;
63 static int hf_bootp_ip_your = -1;
64 static int hf_bootp_ip_server = -1;
65 static int hf_bootp_ip_relay = -1;
66 static int hf_bootp_hw_addr = -1;
67 static int hf_bootp_server = -1;
68 static int hf_bootp_file = -1;
69 static int hf_bootp_cookie = -1;
70 static int hf_bootp_vendor = -1;
71 static int hf_bootp_dhcp = -1;
72
73 static guint ett_bootp = -1;
74 static guint ett_bootp_flags = -1;
75 static guint ett_bootp_option = -1;
76
77 #define UDP_PORT_BOOTPS  67
78 #define UDP_PORT_BOOTPC  68
79
80 #define BOOTP_BC        0x8000
81 #define BOOTP_MBZ       0x7FFF
82
83 enum field_type { none, ipv4, string, toggle, yes_no, special, opaque,
84         time_in_secs,
85         val_u_byte, val_u_short, val_u_le_short, val_u_long,
86         val_s_long };
87
88 struct opt_info {
89         char    *text;
90         enum field_type ftype;
91 };
92
93 static const true_false_string flag_set_broadcast = {
94   "Broadcast",
95   "Unicast"
96 };
97
98 #define NUM_OPT_INFOS 211
99 #define NUM_O63_SUBOPTS 11
100
101 static int dissect_vendor_pxeclient_suboption(proto_tree *v_tree, tvbuff_t *tvb,
102     int optp);
103 static int dissect_netware_ip_suboption(proto_tree *v_tree, tvbuff_t *tvb,
104     int optp);
105 static int bootp_dhcp_decode_agent_info(proto_tree *v_tree, tvbuff_t *tvb,
106     int optp);
107
108 static const char *
109 get_dhcp_type(guint8 byte)
110 {
111         static const char       *opt53_text[] = {
112                 "Unknown Message Type",
113                 "Discover",
114                 "Offer",
115                 "Request",
116                 "Decline",
117                 "ACK",
118                 "NAK",
119                 "Release",
120                 "Inform",
121                 "Force Renew"
122         };
123         int i;
124
125         if (byte > 0 && byte < (sizeof opt53_text / sizeof opt53_text[0]))
126                 i = byte;
127         else
128                 i = 0;
129         return opt53_text[i];
130 }
131
132 /* DHCP Authentication protocols */
133 #define AUTHEN_PROTO_CONFIG_TOKEN       0
134 #define AUTHEN_PROTO_DELAYED_AUTHEN     1
135
136 /* DHCP Authentication algorithms for delayed authentication */
137 #define AUTHEN_DELAYED_ALGO_HMAC_MD5    1
138
139 /* DHCP Authentication Replay Detection Methods */
140 #define AUTHEN_RDM_MONOTONIC_COUNTER    0x00
141
142 /* DHCP Option Overload (option code 52) */
143 #define OPT_OVERLOAD_FILE               1
144 #define OPT_OVERLOAD_SNAME              2
145 #define OPT_OVERLOAD_BOTH               3
146
147 /* Server name and boot file offsets and lengths */
148 #define SERVER_NAME_OFFSET              44
149 #define SERVER_NAME_LEN                 64
150 #define FILE_NAME_OFFSET                108
151 #define FILE_NAME_LEN                   128
152 #define VENDOR_INFO_OFFSET              236
153
154 /* Returns the number of bytes consumed by this option. */
155 static int
156 bootp_option(tvbuff_t *tvb, proto_tree *bp_tree, int voff, int eoff,
157     gboolean first_pass, gboolean *at_end, const char **dhcp_type_p,
158     const guint8 **vendor_class_id_p)
159 {
160         char                    *text;
161         enum field_type         ftype;
162         guchar                  code = tvb_get_guint8(tvb, voff);
163         int                     vlen;
164         guchar                  byte;
165         int                     i,optp, consumed;
166         gulong                  time_secs;
167         proto_tree              *v_tree, *o52tree;
168         proto_item              *vti;
169         guint8                  protocol;
170         guint8                  algorithm;
171         guint8                  rdm;
172         int                     o52voff, o52eoff;
173         gboolean                o52at_end;
174
175         static const value_string nbnt_vals[] = {
176             {0x1,   "B-node" },
177             {0x2,   "P-node" },
178             {0x4,   "M-node" },
179             {0x8,   "H-node" },
180             {0,     NULL     } };
181
182         static const value_string authen_protocol_vals[] = {
183             {AUTHEN_PROTO_CONFIG_TOKEN,   "configuration token" },
184             {AUTHEN_PROTO_DELAYED_AUTHEN, "delayed authentication" },
185             {0,                           NULL     } };
186
187         static const value_string authen_da_algo_vals[] = {
188             {AUTHEN_DELAYED_ALGO_HMAC_MD5, "HMAC_MD5" },
189             {0,                            NULL     } };
190
191         static const value_string authen_rdm_vals[] = {
192             {AUTHEN_RDM_MONOTONIC_COUNTER, "Monotonically-increasing counter" },
193             {0,                            NULL     } };
194
195         static const value_string opt_overload_vals[] = {
196             { OPT_OVERLOAD_FILE,  "Boot file name holds options",                },
197             { OPT_OVERLOAD_SNAME, "Server host name holds options",              },
198             { OPT_OVERLOAD_BOTH,  "Boot file and server host names hold options" },
199             { 0,                  NULL                                           } };
200
201         static struct opt_info opt[] = {
202                 /*   0 */ { "Padding",                                  none },
203                 /*   1 */ { "Subnet Mask",                              ipv4 },
204                 /*   2 */ { "Time Offset",                              val_s_long },
205                 /*   3 */ { "Router",                                   ipv4 },
206                 /*   4 */ { "Time Server",                              ipv4 },
207                 /*   5 */ { "Name Server",                              ipv4 },
208                 /*   6 */ { "Domain Name Server",                       ipv4 },
209                 /*   7 */ { "Log Server",                               ipv4 },
210                 /*   8 */ { "Cookie Server",                            ipv4 },
211                 /*   9 */ { "LPR Server",                               ipv4 },
212                 /*  10 */ { "Impress Server",                           ipv4 },
213                 /*  11 */ { "Resource Location Server",                 ipv4 },
214                 /*  12 */ { "Host Name",                                string },
215                 /*  13 */ { "Boot File Size",                           val_u_short },
216                 /*  14 */ { "Merit Dump File",                          string },
217                 /*  15 */ { "Domain Name",                              string },
218                 /*  16 */ { "Swap Server",                              ipv4 },
219                 /*  17 */ { "Root Path",                                string },
220                 /*  18 */ { "Extensions Path",                          string },
221                 /*  19 */ { "IP Forwarding",                            toggle },
222                 /*  20 */ { "Non-Local Source Routing",                 toggle },
223                 /*  21 */ { "Policy Filter",                            special },
224                 /*  22 */ { "Maximum Datagram Reassembly Size",         val_u_short },
225                 /*  23 */ { "Default IP Time-to-Live",                  val_u_byte },
226                 /*  24 */ { "Path MTU Aging Timeout",                   time_in_secs },
227                 /*  25 */ { "Path MTU Plateau Table",                   val_u_short },
228                 /*  26 */ { "Interface MTU",                            val_u_short },
229                 /*  27 */ { "All Subnets are Local",                    yes_no },
230                 /*  28 */ { "Broadcast Address",                        ipv4 },
231                 /*  29 */ { "Perform Mask Discovery",                   toggle },
232                 /*  30 */ { "Mask Supplier",                            yes_no },
233                 /*  31 */ { "Perform Router Discover",                  toggle },
234                 /*  32 */ { "Router Solicitation Address",              ipv4 },
235                 /*  33 */ { "Static Route",                             special },
236                 /*  34 */ { "Trailer Encapsulation",                    toggle },
237                 /*  35 */ { "ARP Cache Timeout",                        time_in_secs },
238                 /*  36 */ { "Ethernet Encapsulation",                   toggle },
239                 /*  37 */ { "TCP Default TTL",                          val_u_byte },
240                 /*  38 */ { "TCP Keepalive Interval",                   time_in_secs },
241                 /*  39 */ { "TCP Keepalive Garbage",                    toggle },
242                 /*  40 */ { "Network Information Service Domain",       string },
243                 /*  41 */ { "Network Information Service Servers",      ipv4 },
244                 /*  42 */ { "Network Time Protocol Servers",            ipv4 },
245                 /*  43 */ { "Vendor-Specific Information",              special },
246                 /*  44 */ { "NetBIOS over TCP/IP Name Server",          ipv4 },
247                 /*  45 */ { "NetBIOS over TCP/IP Datagram Distribution Name Server", ipv4 },
248                 /*  46 */ { "NetBIOS over TCP/IP Node Type",            special },
249                 /*  47 */ { "NetBIOS over TCP/IP Scope",                string },
250                 /*  48 */ { "X Window System Font Server",              ipv4 },
251                 /*  49 */ { "X Window System Display Manager",          ipv4 },
252                 /*  50 */ { "Requested IP Address",                     ipv4 },
253                 /*  51 */ { "IP Address Lease Time",                    time_in_secs },
254                 /*  52 */ { "Option Overload",                          special },
255                 /*  53 */ { "DHCP Message Type",                        special },
256                 /*  54 */ { "Server Identifier",                        ipv4 },
257                 /*  55 */ { "Parameter Request List",                   special },
258                 /*  56 */ { "Message",                                  string },
259                 /*  57 */ { "Maximum DHCP Message Size",                val_u_short },
260                 /*  58 */ { "Renewal Time Value",                       time_in_secs },
261                 /*  59 */ { "Rebinding Time Value",                     time_in_secs },
262                 /*  60 */ { "Vendor class identifier",                  special },
263                 /*  61 */ { "Client identifier",                        special },
264                 /*  62 */ { "Novell/Netware IP domain",                 string },
265                 /*  63 */ { "Novell Options",                           special },
266                 /*  64 */ { "Network Information Service+ Domain",      string },
267                 /*  65 */ { "Network Information Service+ Servers",     ipv4 },
268                 /*  66 */ { "TFTP Server Name",                         string },
269                 /*  67 */ { "Bootfile name",                            string },
270                 /*  68 */ { "Mobile IP Home Agent",                     ipv4 },
271                 /*  69 */ { "SMTP Server",                              ipv4 },
272                 /*  70 */ { "POP3 Server",                              ipv4 },
273                 /*  71 */ { "NNTP Server",                              ipv4 },
274                 /*  72 */ { "Default WWW Server",                       ipv4 },
275                 /*  73 */ { "Default Finger Server",                    ipv4 },
276                 /*  74 */ { "Default IRC Server",                       ipv4 },
277                 /*  75 */ { "StreetTalk Server",                        ipv4 },
278                 /*  76 */ { "StreetTalk Directory Assistance Server",   ipv4 },
279                 /*  77 */ { "User Class Information",                   opaque },
280                 /*  78 */ { "Directory Agent Information",              opaque },
281                 /*  79 */ { "Service Location Agent Scope",             opaque },
282                 /*  80 */ { "Naming Authority",                         opaque },
283                 /*  81 */ { "Client Fully Qualified Domain Name",       opaque },
284                 /*  82 */ { "Agent Information Option",                 special },
285                 /*  83 */ { "Unassigned",                               opaque },
286                 /*  84 */ { "Unassigned",                               opaque },
287                 /*  85 */ { "Novell Directory Services Servers",        opaque },
288                 /*  86 */ { "Novell Directory Services Tree Name",      opaque },
289                 /*  87 */ { "Novell Directory Services Context",        opaque },
290                 /*  88 */ { "IEEE 1003.1 POSIX Timezone",               opaque },
291                 /*  89 */ { "Fully Qualified Domain Name",              opaque },
292                 /*  90 */ { "Authentication",                           special },
293                 /*  91 */ { "Vines TCP/IP Server Option",               opaque },
294                 /*  92 */ { "Server Selection Option",                  opaque },
295                 /*  93 */ { "Client System Architecture",               opaque },
296                 /*  94 */ { "Client Network Device Interface",          opaque },
297                 /*  95 */ { "Lightweight Directory Access Protocol",    opaque },
298                 /*  96 */ { "IPv6 Transitions",                         opaque },
299                 /*  97 */ { "UUID/GUID-based Client Identifier",        opaque },
300                 /*  98 */ { "Open Group's User Authentication",         opaque },
301                 /*  99 */ { "Unassigned",                               opaque },
302                 /* 100 */ { "Printer Name",                             opaque },
303                 /* 101 */ { "MDHCP multicast address",                  opaque },
304                 /* 102 */ { "Removed/unassigned",                       opaque },
305                 /* 103 */ { "Removed/unassigned",                       opaque },
306                 /* 104 */ { "Removed/unassigned",                       opaque },
307                 /* 105 */ { "Removed/unassigned",                       opaque },
308                 /* 106 */ { "Removed/unassigned",                       opaque },
309                 /* 107 */ { "Removed/unassigned",                       opaque },
310                 /* 108 */ { "Swap Path Option",                         opaque },
311                 /* 109 */ { "Unassigned",                               opaque },
312                 /* 110 */ { "IPX Compability",                          opaque },
313                 /* 111 */ { "Unassigned",                               opaque },
314                 /* 112 */ { "NetInfo Parent Server Address",            ipv4 },
315                 /* 113 */ { "NetInfo Parent Server Tag",                string },
316                 /* 114 */ { "URL",                                      opaque },
317                 /* 115 */ { "DHCP Failover Protocol",                   opaque },
318                 /* 116 */ { "DHCP Auto-Configuration",                  opaque },
319                 /* 117 */ { "Unassigned",                               opaque },
320                 /* 118 */ { "Unassigned",                               opaque },
321                 /* 119 */ { "Unassigned",                               opaque },
322                 /* 120 */ { "Unassigned",                               opaque },
323                 /* 121 */ { "Unassigned",                               opaque },
324                 /* 122 */ { "Unassigned",                               opaque },
325                 /* 123 */ { "Unassigned",                               opaque },
326                 /* 124 */ { "Unassigned",                               opaque },
327                 /* 125 */ { "Unassigned",                               opaque },
328                 /* 126 */ { "Extension",                                opaque },
329                 /* 127 */ { "Extension",                                opaque },
330                 /* 128 */ { "Private",                                  opaque },
331                 /* 129 */ { "Private",                                  opaque },
332                 /* 130 */ { "Private",                                  opaque },
333                 /* 131 */ { "Private",                                  opaque },
334                 /* 132 */ { "Private",                                  opaque },
335                 /* 133 */ { "Private",                                  opaque },
336                 /* 134 */ { "Private",                                  opaque },
337                 /* 135 */ { "Private",                                  opaque },
338                 /* 136 */ { "Private",                                  opaque },
339                 /* 137 */ { "Private",                                  opaque },
340                 /* 138 */ { "Private",                                  opaque },
341                 /* 139 */ { "Private",                                  opaque },
342                 /* 140 */ { "Private",                                  opaque },
343                 /* 141 */ { "Private",                                  opaque },
344                 /* 142 */ { "Private",                                  opaque },
345                 /* 143 */ { "Private",                                  opaque },
346                 /* 144 */ { "Private",                                  opaque },
347                 /* 145 */ { "Private",                                  opaque },
348                 /* 146 */ { "Private",                                  opaque },
349                 /* 147 */ { "Private",                                  opaque },
350                 /* 148 */ { "Private",                                  opaque },
351                 /* 149 */ { "Private",                                  opaque },
352                 /* 150 */ { "Private",                                  opaque },
353                 /* 151 */ { "Private",                                  opaque },
354                 /* 152 */ { "Private",                                  opaque },
355                 /* 153 */ { "Private",                                  opaque },
356                 /* 154 */ { "Private",                                  opaque },
357                 /* 155 */ { "Private",                                  opaque },
358                 /* 156 */ { "Private",                                  opaque },
359                 /* 157 */ { "Private",                                  opaque },
360                 /* 158 */ { "Private",                                  opaque },
361                 /* 159 */ { "Private",                                  opaque },
362                 /* 160 */ { "Private",                                  opaque },
363                 /* 161 */ { "Private",                                  opaque },
364                 /* 162 */ { "Private",                                  opaque },
365                 /* 163 */ { "Private",                                  opaque },
366                 /* 164 */ { "Private",                                  opaque },
367                 /* 165 */ { "Private",                                  opaque },
368                 /* 166 */ { "Private",                                  opaque },
369                 /* 167 */ { "Private",                                  opaque },
370                 /* 168 */ { "Private",                                  opaque },
371                 /* 169 */ { "Private",                                  opaque },
372                 /* 170 */ { "Private",                                  opaque },
373                 /* 171 */ { "Private",                                  opaque },
374                 /* 172 */ { "Private",                                  opaque },
375                 /* 173 */ { "Private",                                  opaque },
376                 /* 174 */ { "Private",                                  opaque },
377                 /* 175 */ { "Private",                                  opaque },
378                 /* 176 */ { "Private",                                  opaque },
379                 /* 177 */ { "Private",                                  opaque },
380                 /* 178 */ { "Private",                                  opaque },
381                 /* 179 */ { "Private",                                  opaque },
382                 /* 180 */ { "Private",                                  opaque },
383                 /* 181 */ { "Private",                                  opaque },
384                 /* 182 */ { "Private",                                  opaque },
385                 /* 183 */ { "Private",                                  opaque },
386                 /* 184 */ { "Private",                                  opaque },
387                 /* 185 */ { "Private",                                  opaque },
388                 /* 186 */ { "Private",                                  opaque },
389                 /* 187 */ { "Private",                                  opaque },
390                 /* 188 */ { "Private",                                  opaque },
391                 /* 189 */ { "Private",                                  opaque },
392                 /* 190 */ { "Private",                                  opaque },
393                 /* 191 */ { "Private",                                  opaque },
394                 /* 192 */ { "Private",                                  opaque },
395                 /* 193 */ { "Private",                                  opaque },
396                 /* 194 */ { "Private",                                  opaque },
397                 /* 195 */ { "Private",                                  opaque },
398                 /* 196 */ { "Private",                                  opaque },
399                 /* 197 */ { "Private",                                  opaque },
400                 /* 198 */ { "Private",                                  opaque },
401                 /* 199 */ { "Private",                                  opaque },
402                 /* 200 */ { "Private",                                  opaque },
403                 /* 201 */ { "Private",                                  opaque },
404                 /* 202 */ { "Private",                                  opaque },
405                 /* 203 */ { "Private",                                  opaque },
406                 /* 204 */ { "Private",                                  opaque },
407                 /* 205 */ { "Private",                                  opaque },
408                 /* 206 */ { "Private",                                  opaque },
409                 /* 207 */ { "Private",                                  opaque },
410                 /* 208 */ { "Private",                                  opaque },
411                 /* 209 */ { "Private",                                  opaque },
412                 /* 210 */ { "Authentication",                           special }
413         };
414
415         /* Options whose length isn't "vlen + 2". */
416         switch (code) {
417
418         case 0:         /* Padding */
419                 /* check how much padding we have */
420                 for (i = voff + 1; i < eoff; i++ ) {
421                         if (tvb_get_guint8(tvb, i) != 0) {
422                                 break;
423                         }
424                 }
425                 i = i - voff;
426                 if (!first_pass) {
427                         if (bp_tree != NULL) {
428                                 proto_tree_add_text(bp_tree, tvb, voff, i,
429                                     "Padding");
430                         }
431                 }
432                 consumed = i;
433                 return consumed;
434                 break;
435
436         case 255:       /* End Option */
437                 if (!first_pass) {
438                         if (bp_tree != NULL) {
439                                 proto_tree_add_text(bp_tree, tvb, voff, 1,
440                                     "End Option");
441                         }
442                 }
443                 *at_end = TRUE;
444                 consumed = 1;
445                 return consumed;
446         }
447
448         /*
449          * Get the length of the option, and the number of bytes it
450          * consumes (the length doesn't include the option code or
451          * length bytes).
452          *
453          * On the first pass, check first whether we have the length
454          * byte, so that we don't throw an exception; if we throw an
455          * exception in the first pass, which is only checking for options
456          * whose values we need in order to properly dissect the packet
457          * on the second pass, we won't actually dissect the options, so
458          * you won't be able to see which option had the problem.
459          */
460         if (first_pass) {
461                 if (!tvb_bytes_exist(tvb, voff+1, 1)) {
462                         /*
463                          * We don't have the length byte; just return 1
464                          * as the number of bytes we consumed, to count
465                          * the code byte.
466                          */
467                         return 1;
468                 }
469         }
470         vlen = tvb_get_guint8(tvb, voff+1);
471         consumed = vlen + 2;
472
473         /*
474          * In the first pass, we don't put anything into the protocol
475          * tree; we just check for some options we have to look at
476          * in order to properly process the packet:
477          *
478          *      53 (DHCP message type) - if this is present, this is DHCP
479          *
480          *      60 (Vendor class identifier) - we need this in order to
481          *         interpret the vendor-specific info
482          *
483          * We also check, before fetching anything, to make sure we
484          * have the entire item we're fetching, so that we don't throw
485          * an exception.
486          */
487         if (first_pass) {
488                 if (tvb_bytes_exist(tvb, voff+2, consumed-2)) {
489                         switch (code) {
490
491                         case 53:
492                                 *dhcp_type_p =
493                                     get_dhcp_type(tvb_get_guint8(tvb, voff+2));
494                                 break;
495
496                         case 60:
497                                 *vendor_class_id_p =
498                                     tvb_get_ptr(tvb, voff+2, consumed-2);
499                                 break;
500                         }
501                 }
502
503                 /*
504                  * We don't do anything else here.
505                  */
506                 return consumed;
507         }
508
509         /*
510          * This is the second pass - if there's a protocol tree to be
511          * built, we put stuff into it, otherwise we just return.
512          */
513         if (bp_tree == NULL) {
514                 /* Don't put anything in the protocol tree. */
515                 return consumed;
516         }
517
518         text = opt[code].text;
519         /* Special cases */
520         switch (code) {
521
522         case 21:        /* Policy Filter */
523                 if (vlen == 8) {
524                         /* one IP address pair */
525                         proto_tree_add_text(bp_tree, tvb, voff, consumed,
526                                 "Option %d: %s = %s/%s", code, text,
527                                 ip_to_str(tvb_get_ptr(tvb, voff+2, 4)),
528                                 ip_to_str(tvb_get_ptr(tvb, voff+6, 4)));
529                 } else {
530                         /* > 1 IP address pair. Let's make a sub-tree */
531                         vti = proto_tree_add_text(bp_tree, tvb, voff,
532                                 consumed, "Option %d: %s", code, text);
533                         v_tree = proto_item_add_subtree(vti, ett_bootp_option);
534                         for (i = voff + 2; i < voff + consumed; i += 8) {
535                                 proto_tree_add_text(v_tree, tvb, i, 8, "IP Address/Mask: %s/%s",
536                                         ip_to_str(tvb_get_ptr(tvb, i, 4)),
537                                         ip_to_str(tvb_get_ptr(tvb, i+4, 4)));
538                         }
539                 }
540                 break;
541
542         case 33:        /* Static Route */
543                 if (vlen == 8) {
544                         /* one IP address pair */
545                         proto_tree_add_text(bp_tree, tvb, voff, consumed,
546                                 "Option %d: %s = %s/%s", code, text,
547                                 ip_to_str(tvb_get_ptr(tvb, voff+2, 4)),
548                                 ip_to_str(tvb_get_ptr(tvb, voff+6, 4)));
549                 } else {
550                         /* > 1 IP address pair. Let's make a sub-tree */
551                         vti = proto_tree_add_text(bp_tree, tvb, voff,
552                                 consumed, "Option %d: %s", code, text);
553                         v_tree = proto_item_add_subtree(vti, ett_bootp_option);
554                         for (i = voff + 2; i < voff + consumed; i += 8) {
555                                 proto_tree_add_text(v_tree, tvb, i, 8,
556                                         "Destination IP Address/Router: %s/%s",
557                                         ip_to_str(tvb_get_ptr(tvb, i, 4)),
558                                         ip_to_str(tvb_get_ptr(tvb, i+4, 4)));
559                         }
560                 }
561                 break;
562
563         case 43:        /* Vendor-Specific Info */
564                 /* PXE protocol 2.1 as described in the intel specs */
565                 if (*vendor_class_id_p != NULL &&
566                     strncmp(*vendor_class_id_p, "PXEClient", strlen("PXEClient")) == 0) {
567                         vti = proto_tree_add_text(bp_tree, tvb, voff,
568                                 consumed, "Option %d: %s (PXEClient)", code, text);
569                         v_tree = proto_item_add_subtree(vti, ett_bootp_option);
570
571                         optp = voff+2;
572                         while (optp < voff+consumed) {
573                                 optp = dissect_vendor_pxeclient_suboption(v_tree,
574                                         tvb, optp);
575                         }
576                 } else {
577                         proto_tree_add_text(bp_tree, tvb, voff, consumed,
578                                 "Option %d: %s (%d bytes)", code, text, vlen);
579                 }
580                 break;
581
582         case 46:        /* NetBIOS-over-TCP/IP Node Type */
583                 byte = tvb_get_guint8(tvb, voff+2);
584                 proto_tree_add_text(bp_tree, tvb, voff, consumed,
585                                 "Option %d: %s = %s", code, text,
586                                 val_to_str(byte, nbnt_vals,
587                                     "Unknown (0x%02x)"));
588                 break;
589
590         case 52:        /* Option Overload */
591                 byte = tvb_get_guint8(tvb, voff+2);
592                 vti = proto_tree_add_text(bp_tree, tvb, voff, consumed,
593                         "Option %d: %s = %s", code, text,
594                         val_to_str(byte, opt_overload_vals,
595                             "Unknown (0x%02x)"));
596                             
597                 /* Just in case we find an option 52 in sname or file */
598                 if (voff > VENDOR_INFO_OFFSET && byte >= 1 && byte <= 3) {
599                         o52tree = proto_item_add_subtree(vti, ett_bootp_option);
600                         if (byte == 1 || byte == 3) {   /* 'file' */
601                                 vti = proto_tree_add_text (o52tree, tvb,
602                                         FILE_NAME_OFFSET, FILE_NAME_LEN,
603                                         "Boot file name option overload");
604                                 v_tree = proto_item_add_subtree(vti, ett_bootp_option);
605                                 o52voff = FILE_NAME_OFFSET;
606                                 o52eoff = FILE_NAME_OFFSET + FILE_NAME_LEN;
607                                 o52at_end = FALSE;
608                                 while (o52voff < o52eoff && !o52at_end) {
609                                         o52voff += bootp_option(tvb, v_tree, o52voff,
610                                                 o52eoff, FALSE, &o52at_end,
611                                                 dhcp_type_p, vendor_class_id_p);
612                                 }
613                         }
614                         if (byte == 2 || byte == 3) {   /* 'sname' */
615                                 vti = proto_tree_add_text (o52tree, tvb,
616                                         SERVER_NAME_OFFSET, SERVER_NAME_LEN, 
617                                         "Server host name option overload");
618                                 v_tree = proto_item_add_subtree(vti, ett_bootp_option);
619                                 o52voff = SERVER_NAME_OFFSET;
620                                 o52eoff = SERVER_NAME_OFFSET + SERVER_NAME_LEN;
621                                 o52at_end = FALSE;
622                                 while (o52voff < o52eoff && !o52at_end) {
623                                         o52voff += bootp_option(tvb, v_tree, o52voff,
624                                                 o52eoff, FALSE, &o52at_end,
625                                                 dhcp_type_p, vendor_class_id_p);
626                                 }
627                         }
628                 }
629
630 /*              protocol = tvb_get_guint8(tvb, voff+2);
631                 proto_tree_add_text(v_tree, tvb, voff+2, 1, "Protocol: %s (%u)",
632                                     val_to_str(protocol, authen_protocol_vals, "Unknown"),
633                                     protocol); */
634                 break;
635         case 53:        /* DHCP Message Type */
636                 proto_tree_add_text(bp_tree, tvb, voff, 3, "Option %d: %s = DHCP %s",
637                         code, text, get_dhcp_type(tvb_get_guint8(tvb, voff+2)));
638                 break;
639
640         case 55:        /* Parameter Request List */
641                 vti = proto_tree_add_text(bp_tree, tvb, voff,
642                         vlen + 2, "Option %d: %s", code, text);
643                 v_tree = proto_item_add_subtree(vti, ett_bootp_option);
644                 for (i = 0; i < vlen; i++) {
645                         byte = tvb_get_guint8(tvb, voff+2+i);
646                         if (byte < NUM_OPT_INFOS) {
647                                 proto_tree_add_text(v_tree, tvb, voff+2+i, 1, "%d = %s",
648                                                 byte, opt[byte].text);
649                         } else {
650                                 proto_tree_add_text(vti, tvb, voff+2+i, 1,
651                                         "Unknown Option Code: %d", byte);
652                         }
653                 }
654                 break;
655
656         case 60:        /* Vendor class identifier */
657                 proto_tree_add_text(bp_tree, tvb, voff, consumed,
658                         "Option %d: %s = \"%.*s\"", code, text, vlen,
659                         tvb_get_ptr(tvb, voff+2, consumed-2));
660                 break;
661
662         case 61:        /* Client Identifier */
663                 /* We *MAY* use hwtype/hwaddr. If we have 7 bytes, I'll
664                    guess that the first is the hwtype, and the last 6
665                    are the hw addr */
666                 if (vlen == 7) {
667                         guint8 htype;
668
669                         vti = proto_tree_add_text(bp_tree, tvb, voff,
670                                 consumed, "Option %d: %s", code, text);
671                         v_tree = proto_item_add_subtree(vti, ett_bootp_option);
672                         htype = tvb_get_guint8(tvb, voff+2);
673                         proto_tree_add_text(v_tree, tvb, voff+2, 1,
674                                 "Hardware type: %s",
675                                 arphrdtype_to_str(htype,
676                                         "Unknown (0x%02x)"));
677                         proto_tree_add_text(v_tree, tvb, voff+3, 6,
678                                 "Client hardware address: %s",
679                                 arphrdaddr_to_str(tvb_get_ptr(tvb, voff+3, 6),
680                                         6, htype));
681                 } else {
682                         /* otherwise, it's opaque data */
683                         proto_tree_add_text(bp_tree, tvb, voff, consumed,
684                                 "Option %d: %s (%d bytes)", code, text, vlen);
685                 }
686                 break;
687
688         case 63:        /* NetWare/IP options */
689                 vti = proto_tree_add_text(bp_tree, tvb, voff,
690                     consumed, "Option %d: %s", code, text);
691                 v_tree = proto_item_add_subtree(vti, ett_bootp_option);
692
693                 optp = voff+2;
694                 while (optp < voff+consumed)
695                         optp = dissect_netware_ip_suboption(v_tree, tvb, optp);
696                 break;
697
698         case 82:        /* Relay Agent Information Option */
699                 vti = proto_tree_add_text(bp_tree, tvb, voff, consumed,
700                                           "Option %d: %s (%d bytes)",
701                                           code, text, vlen);
702                 v_tree = proto_item_add_subtree(vti, ett_bootp_option);
703                 optp = voff+2;
704                 while (optp < voff+consumed) {
705                         optp = bootp_dhcp_decode_agent_info(v_tree, tvb, optp);
706                 }
707                 break;
708
709         case 90:        /* DHCP Authentication */
710         case 210:       /* Was this used for authentication at one time? */
711                 vti = proto_tree_add_text(bp_tree, tvb, voff,
712                         vlen + 2, "Option %d: %s", code, text);
713                 v_tree = proto_item_add_subtree(vti, ett_bootp_option);
714
715                 protocol = tvb_get_guint8(tvb, voff+2);
716                 proto_tree_add_text(v_tree, tvb, voff+2, 1, "Protocol: %s (%u)",
717                                     val_to_str(protocol, authen_protocol_vals, "Unknown"),
718                                     protocol);
719
720                 algorithm = tvb_get_guint8(tvb, voff+3);
721                 switch (protocol) {
722
723                 case AUTHEN_PROTO_DELAYED_AUTHEN:
724                         proto_tree_add_text(v_tree, tvb, voff+3, 1,
725                                     "Algorithm: %s (%u)",
726                                     val_to_str(algorithm, authen_da_algo_vals, "Unknown"),
727                                     algorithm);
728                         break;
729
730                 default:
731                         proto_tree_add_text(v_tree, tvb, voff+3, 1,
732                                     "Algorithm: %u", algorithm);
733                         break;
734                 }
735
736                 rdm = tvb_get_guint8(tvb, voff+4);
737                 proto_tree_add_text(v_tree, tvb, voff+4, 1,
738                                     "Replay Detection Method: %s (%u)",
739                                     val_to_str(rdm, authen_rdm_vals, "Unknown"),
740                                     rdm);
741
742                 switch (rdm) {
743
744                 case AUTHEN_RDM_MONOTONIC_COUNTER:
745                         proto_tree_add_text(v_tree, tvb, voff+5, 8,
746                                     "Replay Detection Value: %s",
747                                     u64toh(tvb_get_ptr(tvb, voff+5, 8)));
748                         break;
749
750                 default:
751                         proto_tree_add_text(v_tree, tvb, voff+5, 8,
752                                     "Replay Detection Value: %s",
753                                     tvb_bytes_to_str(tvb, voff+5, 8));
754                         break;
755                 }
756
757                 switch (protocol) {
758
759                 case AUTHEN_PROTO_DELAYED_AUTHEN:
760                         switch (algorithm) {
761
762                         case AUTHEN_DELAYED_ALGO_HMAC_MD5:
763                                 proto_tree_add_text(v_tree, tvb, voff+13, 4,
764                                         "Secret ID: 0x%08x",
765                                         tvb_get_ntohl(tvb, voff+13));
766                                 proto_tree_add_text(v_tree, tvb, voff+17, 16,
767                                         "HMAC MD5 Hash: %s",
768                                         tvb_bytes_to_str(tvb, voff+17, 16));
769                                 break;
770
771                         default:
772                                 proto_tree_add_text(v_tree, tvb, voff+13, vlen-11,
773                                         "Authentication Information: %s",
774                                         tvb_bytes_to_str(tvb, voff+17, vlen-11));
775                                 break;
776                         }
777                         break;
778
779                 default:
780                         proto_tree_add_text(v_tree, tvb, voff+13, vlen-11,
781                                 "Authentication Information: %s",
782                                 tvb_bytes_to_str(tvb, voff+17, vlen-11));
783                         break;
784                 }
785                 break;
786
787         default:        /* not special */
788                 break;
789         }
790
791         /* Normal cases */
792         if (code < NUM_OPT_INFOS) {
793                 text = opt[code].text;
794                 ftype = opt[code].ftype;
795
796                 switch (ftype) {
797
798                 case special:
799                         return consumed;
800
801                 case ipv4:
802                         if (vlen == 4) {
803                                 /* one IP address */
804                                 proto_tree_add_text(bp_tree, tvb, voff, consumed,
805                                         "Option %d: %s = %s", code, text,
806                                         ip_to_str(tvb_get_ptr(tvb, voff+2, 4)));
807                         } else {
808                                 /* > 1 IP addresses. Let's make a sub-tree */
809                                 vti = proto_tree_add_text(bp_tree, tvb, voff,
810                                         consumed, "Option %d: %s", code, text);
811                                 v_tree = proto_item_add_subtree(vti, ett_bootp_option);
812                                 for (i = voff + 2; i < voff + consumed; i += 4) {
813                                         proto_tree_add_text(v_tree, tvb, i, 4, "IP Address: %s",
814                                                 ip_to_str(tvb_get_ptr(tvb, i, 4)));
815                                 }
816                         }
817                         break;
818
819                 case string:
820                         /* Fix for non null-terminated string supplied by
821                          * John Lines <John.Lines@aeat.co.uk>
822                          */
823                         proto_tree_add_text(bp_tree, tvb, voff, consumed,
824                                         "Option %d: %s = \"%.*s\"", code, text, vlen,
825                                         tvb_get_ptr(tvb, voff+2, consumed-2));
826                         break;
827
828                 case opaque:
829                         proto_tree_add_text(bp_tree, tvb, voff, consumed,
830                                         "Option %d: %s (%d bytes)",
831                                         code, text, vlen);
832                         break;
833
834                 case val_u_short:
835                         if (vlen == 2) {
836                                 /* one gushort */
837                                 proto_tree_add_text(bp_tree, tvb, voff, consumed,
838                                                 "Option %d: %s = %d", code, text,
839                                                 tvb_get_ntohs(tvb, voff+2));
840                         } else {
841                                 /* > 1 gushort */
842                                 vti = proto_tree_add_text(bp_tree, tvb, voff,
843                                         consumed, "Option %d: %s", code, text);
844                                 v_tree = proto_item_add_subtree(vti, ett_bootp_option);
845                                 for (i = voff + 2; i < voff + consumed; i += 2) {
846                                         proto_tree_add_text(v_tree, tvb, i, 4, "Value: %d",
847                                                 tvb_get_ntohs(tvb, i));
848                                 }
849                         }
850                         break;
851
852                 case val_u_long:
853                         proto_tree_add_text(bp_tree, tvb, voff, consumed,
854                                         "Option %d: %s = %d", code, text,
855                                         tvb_get_ntohl(tvb, voff+2));
856                         break;
857
858                 case val_u_byte:
859                         proto_tree_add_text(bp_tree, tvb, voff, consumed,
860                                         "Option %d: %s = %d", code, text,
861                                         tvb_get_guint8(tvb, voff+2));
862                         break;
863
864                 case toggle:
865                         i = tvb_get_guint8(tvb, voff+2);
866                         if (i != 0 && i != 1) {
867                                 proto_tree_add_text(bp_tree, tvb, voff, consumed,
868                                                 "Option %d: %s = Invalid Value %d", code, text,
869                                                 i);
870                         } else {
871                                 proto_tree_add_text(bp_tree, tvb, voff, consumed,
872                                                 "Option %d: %s = %s", code, text,
873                                                 i == 0 ? "Disabled" : "Enabled");
874                         }
875                         break;
876
877                 case yes_no:
878                         i = tvb_get_guint8(tvb, voff+2);
879                         if (i != 0 && i != 1) {
880                                 proto_tree_add_text(bp_tree, tvb, voff, consumed,
881                                                 "Option %d: %s = Invalid Value %d", code, text,
882                                                 i);
883                         } else {
884                                 proto_tree_add_text(bp_tree, tvb, voff, consumed,
885                                                 "Option %d: %s = %s", code, text,
886                                                 i == 0 ? "No" : "Yes");
887                         }
888                         break;
889
890                 case time_in_secs:
891                         time_secs = tvb_get_ntohl(tvb, voff+2);
892                         proto_tree_add_text(bp_tree, tvb, voff, consumed,
893                                 "Option %d: %s = %s", code, text,
894                                 ((time_secs == 0xffffffff) ?
895                                     "infinity" :
896                                     time_secs_to_str(time_secs)));
897                         break;
898
899                 default:
900                         proto_tree_add_text(bp_tree, tvb, voff, consumed,
901                                         "Option %d: %s (%d bytes)", code, text, vlen);
902                 }
903         } else {
904                 proto_tree_add_text(bp_tree, tvb, voff, consumed,
905                                 "Unknown Option Code: %d (%d bytes)", code, vlen);
906         }
907
908         return consumed;
909 }
910
911 static int
912 bootp_dhcp_decode_agent_info(proto_tree *v_tree, tvbuff_t *tvb, int optp)
913 {
914         guint8 subopt;
915         guint8 subopt_len;
916
917         subopt = tvb_get_guint8(tvb, optp);
918         subopt_len = tvb_get_guint8(tvb, optp+1);
919         switch (subopt) {
920         case 1:
921                 proto_tree_add_text(v_tree, tvb, optp, subopt_len + 2,
922                                     "Agent Circuit ID (%d bytes)", subopt_len);
923                 break;
924         case 2:
925                 proto_tree_add_text(v_tree, tvb, optp, subopt_len + 2,
926                                     "Agent Remote ID (%d bytes)", subopt_len);
927                 break;
928         default:
929                 proto_tree_add_text(v_tree, tvb, optp, subopt_len + 2,
930                                     "Unknown agent option: %d", subopt);
931                 break;
932         }
933         optp += (subopt_len + 2);
934         return optp;
935 }
936
937 static int
938 dissect_vendor_pxeclient_suboption(proto_tree *v_tree, tvbuff_t *tvb, int optp)
939 {
940         guint8 subopt;
941         guint8 subopt_len;
942         int slask;
943         proto_tree *o43pxeclient_v_tree;
944         proto_item *vti;
945
946         struct o43pxeclient_opt_info {
947                 char    *text;
948                 enum field_type ft;
949         };
950
951         static struct o43pxeclient_opt_info o43pxeclient_opt[]= {
952                 /* 0 */ {"nop", special},       /* dummy */
953                 /* 1 */ {"PXE mtftp IP", ipv4},
954                 /* 2 */ {"PXE mtftp client port", val_u_le_short},
955                 /* 3 */ {"PXE mtftp server port",val_u_le_short},
956                 /* 4 */ {"PXE mtftp timeout", val_u_byte},
957                 /* 5 */ {"PXE mtftp delay", val_u_byte},
958                 /* 6 */ {"PXE discovery control", val_u_byte},
959                         /*
960                          * Correct: b0 (lsb): disable broadcast discovery
961                          *      b1: disable multicast discovery
962                          *      b2: only use/accept servers in boot servers
963                          *      b3: download bootfile without prompt/menu/disc
964                          */
965                 /* 7 */ {"PXE multicast address", ipv4},
966                 /* 8 */ {"PXE boot servers", special},
967                 /* 9 */ {"PXE boot menu", special},
968                 /* 10 */ {"PXE menu prompt", special},
969                 /* 11 */ {"PXE multicast address alloc", special},
970                 /* 12 */ {"PXE credential types", special},
971                 /* 71 {"PXE boot item", special} */
972                 /* 255 {"PXE end options", special} */
973         };
974 #define NUM_O43PXECLIENT_SUBOPTS (12)
975
976         subopt = tvb_get_guint8(tvb, optp);
977
978         if (subopt == 0 ) {
979                 proto_tree_add_text(v_tree, tvb, optp, 1, "Padding");
980                 return (optp+1);
981         } else if (subopt == 255) {     /* End Option */
982                 proto_tree_add_text(v_tree, tvb, optp, 1, "End PXEClient option");
983                 /* Make sure we skip any junk left this option */
984                 return (optp+255);
985         }
986
987         subopt_len = tvb_get_guint8(tvb, optp+1);
988
989         if ( subopt == 71 ) {   /* 71 {"PXE boot item", special} */
990                 /* case special */
991                 /* I may need to decode that properly one day */
992                 proto_tree_add_text(v_tree, tvb, optp, subopt_len+2,
993                         "Suboption %d: %s (%d byte%s)" ,
994                         subopt, "PXE boot item",
995                         subopt_len, (subopt_len != 1)?"s":"");
996         } else if ( (subopt < 1 ) || (subopt > NUM_O43PXECLIENT_SUBOPTS) ) {
997                 proto_tree_add_text(v_tree, tvb, optp, subopt_len+2,
998                         "Unknown suboption %d (%d byte%s)", subopt, subopt_len,
999                         (subopt_len != 1)?"s":"");
1000         } else {
1001                 switch (o43pxeclient_opt[subopt].ft) {
1002
1003 /* XXX          case string:
1004                         proto_tree_add_text(v_tree, tvb, optp, subopt_len+2,
1005                                 "Suboption %d: %s", subopt, o43pxeclient_opt[subopt].text);
1006                         break;
1007    XXX */
1008                 case special:
1009                         /* I may need to decode that properly one day */
1010                         proto_tree_add_text(v_tree, tvb, optp, subopt_len+2,
1011                                 "Suboption %d: %s (%d byte%s)" ,
1012                                 subopt, o43pxeclient_opt[subopt].text,
1013                                 subopt_len, (subopt_len != 1)?"s":"");
1014                         break;
1015
1016                 case val_u_le_short:
1017                         proto_tree_add_text(v_tree, tvb, optp, 4, "Suboption %d: %s = %u",
1018                             subopt, o43pxeclient_opt[subopt].text,
1019                             tvb_get_letohs(tvb, optp+2));
1020                         break;
1021
1022                 case val_u_byte:
1023                         proto_tree_add_text(v_tree, tvb, optp, 3, "Suboption %d: %s = %u",
1024                             subopt, o43pxeclient_opt[subopt].text,
1025                             tvb_get_guint8(tvb, optp+2));
1026                         break;
1027
1028                 case ipv4:
1029                         if (subopt_len == 4) {
1030                                 /* one IP address */
1031                                 proto_tree_add_text(v_tree, tvb, optp, 6,
1032                                     "Suboption %d : %s = %s",
1033                                     subopt, o43pxeclient_opt[subopt].text,
1034                                     ip_to_str(tvb_get_ptr(tvb, optp+2, 4)));
1035                         } else {
1036                                 /* > 1 IP addresses. Let's make a sub-tree */
1037                                 vti = proto_tree_add_text(v_tree, tvb, optp,
1038                                     subopt_len+2, "Suboption %d: %s",
1039                                     subopt, o43pxeclient_opt[subopt].text);
1040                                 o43pxeclient_v_tree = proto_item_add_subtree(vti, ett_bootp_option);
1041                                 for (slask = optp + 2 ; slask < optp+subopt_len; slask += 4) {
1042                                         proto_tree_add_text(o43pxeclient_v_tree, tvb, slask, 4, "IP Address: %s",
1043                                             ip_to_str(tvb_get_ptr(tvb, slask, 4)));
1044                                 }
1045                         }
1046                         break;
1047                 default:
1048                         proto_tree_add_text(v_tree, tvb, optp, subopt_len+2,"ERROR, please report: Unknown subopt type handler %d", subopt);
1049                         break;
1050                 }
1051         }
1052         optp += (subopt_len + 2);
1053         return optp;
1054 }
1055
1056 static int
1057 dissect_netware_ip_suboption(proto_tree *v_tree, tvbuff_t *tvb, int optp)
1058 {
1059         guint8 subopt;
1060         guint8 subopt_len;
1061         int slask;
1062         proto_tree *o63_v_tree;
1063         proto_item *vti;
1064
1065         struct o63_opt_info {
1066                 char    *truet;
1067                 char    *falset;
1068                 enum field_type ft;
1069         };
1070
1071         static struct o63_opt_info o63_opt[]= {
1072                 /* 0 */ {"","",none},
1073                 /* 1 */ {"NWIP does not exist on subnet","",string},
1074                 /* 2 */ {"NWIP exist in options area","",string},
1075                 /* 3 */ {"NWIP exists in sname/file","",string},
1076                 /* 4 */ {"NWIP exists, but too big","",string},
1077                 /* 5 */ {"Broadcast for nearest Netware server","Do NOT Broadcast for nearest Netware server",yes_no},
1078                 /* 6 */ {"Preferred DSS server","",ipv4},
1079                 /* 7 */ {"Nearest NWIP server","",ipv4},
1080                 /* 8 */ {"Autoretries","",val_u_short},
1081                 /* 9 */ {"Autoretry delay, secs ","",val_u_short},
1082                 /* 10*/ {"Support NetWare/IP v1.1","Do NOT support NetWare/IP v1.1",yes_no},
1083                 /* 11*/ {"Primary DSS ", "" , special}
1084         };
1085
1086         subopt = tvb_get_guint8(tvb, optp);
1087         if (subopt > NUM_O63_SUBOPTS) {
1088                 proto_tree_add_text(v_tree, tvb,optp,1,"Unknown suboption %d", subopt);
1089                 optp++;
1090         } else {
1091                 switch (o63_opt[subopt].ft) {
1092
1093                 case string:
1094                         proto_tree_add_text(v_tree, tvb, optp, 2, "Suboption %d: %s", subopt, o63_opt[subopt].truet);
1095                         optp+=2;
1096                         break;
1097
1098                 case yes_no:
1099                         if (tvb_get_guint8(tvb, optp+2)==1) {
1100                                 proto_tree_add_text(v_tree, tvb, optp, 3, "Suboption %d: %s", subopt, o63_opt[subopt].truet);
1101                         } else {
1102                                 proto_tree_add_text(v_tree, tvb, optp, 3, "Suboption %d: %s" , subopt, o63_opt[subopt].falset);
1103                         }
1104                         optp+=3;
1105                         break;
1106
1107                 case special:
1108                         proto_tree_add_text(v_tree, tvb, optp, 6,
1109                             "Suboption %d: %s = %s" ,
1110                             subopt, o63_opt[subopt].truet,
1111                             ip_to_str(tvb_get_ptr(tvb, optp+2, 4)));
1112                         optp=optp+6;
1113                         break;
1114
1115                 case val_u_short:
1116                         proto_tree_add_text(v_tree, tvb, optp, 3, "Suboption %d: %s = %u",
1117                             subopt, o63_opt[subopt].truet,
1118                             tvb_get_guint8(tvb, optp+2));       /* XXX - 1 byte? */
1119                         optp+=3;
1120                         break;
1121
1122                 case ipv4:
1123                         subopt_len = tvb_get_guint8(tvb, optp+1);
1124                         if (subopt_len == 4) {
1125                                 /* one IP address */
1126                                 proto_tree_add_text(v_tree, tvb, optp, 6,
1127                                     "Suboption %d : %s = %s",
1128                                     subopt, o63_opt[subopt].truet,
1129                                     ip_to_str(tvb_get_ptr(tvb, optp+2, 4)));
1130                                 optp=optp+6;
1131                         } else {
1132                                 /* > 1 IP addresses. Let's make a sub-tree */
1133                                 vti = proto_tree_add_text(v_tree, tvb, optp,
1134                                     subopt_len+2, "Suboption %d: %s",
1135                                     subopt, o63_opt[subopt].truet);
1136                                 o63_v_tree = proto_item_add_subtree(vti, ett_bootp_option);
1137                                 for (slask = optp + 2 ; slask < optp+subopt_len; slask += 4) {
1138                                         proto_tree_add_text(o63_v_tree, tvb, slask, 4, "IP Address: %s",
1139                                             ip_to_str(tvb_get_ptr(tvb, slask, 4)));
1140                                 }
1141                                 optp=slask;
1142                         }
1143                         break;
1144                 default:
1145                         proto_tree_add_text(v_tree, tvb,optp,1,"Unknown suboption %d", subopt);
1146                         optp++;
1147                         break;
1148                 }
1149         }
1150         return optp;
1151 }
1152
1153 #define BOOTREQUEST     1
1154 #define BOOTREPLY       2
1155
1156 static const value_string op_vals[] = {
1157         { BOOTREQUEST,  "Boot Request" },
1158         { BOOTREPLY,    "Boot Reply" },
1159         { 0,            NULL }
1160 };
1161
1162 static void
1163 dissect_bootp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1164 {
1165         proto_tree      *bp_tree = NULL;
1166         proto_item      *ti;
1167         proto_tree      *flag_tree = NULL;
1168         proto_item      *fi;
1169         guint8          op;
1170         guint8          htype, hlen;
1171         const guint8    *haddr;
1172         int             voff, eoff, tmpvoff; /* vendor offset, end offset */
1173         guint32         ip_addr;
1174         gboolean        at_end;
1175         const char      *dhcp_type = NULL;
1176         const guint8    *vendor_class_id = NULL;
1177         guint16         flags;
1178
1179         if (check_col(pinfo->cinfo, COL_PROTOCOL))
1180                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "BOOTP");
1181         if (check_col(pinfo->cinfo, COL_INFO)) {
1182                 /*
1183                  * In case we throw an exception fetching the opcode, etc.
1184                  */
1185                 col_clear(pinfo->cinfo, COL_INFO);
1186         }
1187
1188         op = tvb_get_guint8(tvb, 0);
1189         htype = tvb_get_guint8(tvb, 1);
1190         hlen = tvb_get_guint8(tvb, 2);
1191         if (check_col(pinfo->cinfo, COL_INFO)) {
1192                 switch (op) {
1193
1194                 case BOOTREQUEST:
1195                         col_add_fstr(pinfo->cinfo, COL_INFO, "Boot Request from %s",
1196                                 arphrdaddr_to_str(tvb_get_ptr(tvb, 28, hlen),
1197                                         hlen, htype));
1198                         break;
1199
1200                 case BOOTREPLY:
1201                         col_set_str(pinfo->cinfo, COL_INFO, "Boot Reply");
1202                         break;
1203
1204                 default:
1205                         col_add_fstr(pinfo->cinfo, COL_INFO, "Unknown BOOTP message type (%u)",
1206                             op);
1207                         break;
1208                 }
1209         }
1210
1211         if (tree) {
1212                 ti = proto_tree_add_item(tree, proto_bootp, tvb, 0, -1, FALSE);
1213                 bp_tree = proto_item_add_subtree(ti, ett_bootp);
1214
1215                 proto_tree_add_uint(bp_tree, hf_bootp_type, tvb,
1216                                            0, 1,
1217                                            op);
1218                 proto_tree_add_uint_format(bp_tree, hf_bootp_hw_type, tvb,
1219                                            1, 1,
1220                                            htype,
1221                                            "Hardware type: %s",
1222                                            arphrdtype_to_str(htype,
1223                                                              "Unknown (0x%02x)"));
1224                 proto_tree_add_uint(bp_tree, hf_bootp_hw_len, tvb,
1225                                     2, 1, hlen);
1226                 proto_tree_add_item(bp_tree, hf_bootp_hops, tvb,
1227                                     3, 1, FALSE);
1228                 proto_tree_add_item(bp_tree, hf_bootp_id, tvb,
1229                                     4, 4, FALSE);
1230                 proto_tree_add_item(bp_tree, hf_bootp_secs, tvb,
1231                                     8, 2, FALSE);
1232                 flags = tvb_get_ntohs(tvb, 10);
1233                 fi = proto_tree_add_uint(bp_tree, hf_bootp_flags, tvb,
1234                                     10, 2, flags);
1235                 proto_item_append_text(fi, " (%s)",
1236                     (flags & BOOTP_BC) ? "Broadcast" : "Unicast");
1237                 flag_tree = proto_item_add_subtree(fi, ett_bootp_flags);
1238                 proto_tree_add_boolean(flag_tree, hf_bootp_flags_broadcast, tvb,
1239                                     10, 2, flags);
1240                 proto_tree_add_uint(flag_tree, hf_bootp_flags_reserved, tvb,
1241                                     10, 2, flags);
1242                 proto_tree_add_item(bp_tree, hf_bootp_ip_client, tvb,
1243                                     12, 4, FALSE);
1244                 proto_tree_add_item(bp_tree, hf_bootp_ip_your, tvb,
1245                                     16, 4, FALSE);
1246                 proto_tree_add_item(bp_tree, hf_bootp_ip_server, tvb,
1247                                     20, 4, FALSE);
1248                 proto_tree_add_item(bp_tree, hf_bootp_ip_relay, tvb,
1249                                     24, 4, FALSE);
1250
1251                 if (hlen > 0) {
1252                         haddr = tvb_get_ptr(tvb, 28, hlen);
1253                         proto_tree_add_bytes_format(bp_tree, hf_bootp_hw_addr, tvb,
1254                                                    28, hlen,
1255                                                    haddr,
1256                                                    "Client hardware address: %s",
1257                                                    arphrdaddr_to_str(haddr,
1258                                                                      hlen,
1259                                                                      htype));
1260                 }
1261                 else {
1262                         proto_tree_add_text(bp_tree,  tvb,
1263                                                    28, 0, "Client address not given");
1264                 }
1265
1266                 /* The server host name is optional */
1267                 if (tvb_get_guint8(tvb, 44) != '\0') {
1268                         proto_tree_add_item(bp_tree, hf_bootp_server, tvb,
1269                                                    SERVER_NAME_OFFSET, 
1270                                                    SERVER_NAME_LEN, FALSE);
1271                 }
1272                 else {
1273                         proto_tree_add_string_format(bp_tree, hf_bootp_server, tvb,
1274                                                    SERVER_NAME_OFFSET, 
1275                                                    SERVER_NAME_LEN,
1276                                                    tvb_get_ptr(tvb, SERVER_NAME_OFFSET, 1),
1277                                                    "Server host name not given");
1278                 }
1279
1280                 /* Boot file */
1281                 if (tvb_get_guint8(tvb, 108) != '\0') {
1282                         proto_tree_add_item(bp_tree, hf_bootp_file, tvb,
1283                                                    FILE_NAME_OFFSET,
1284                                                    FILE_NAME_LEN, FALSE);
1285                 }
1286                 else {
1287                         proto_tree_add_string_format(bp_tree, hf_bootp_file, tvb,
1288                                                    FILE_NAME_OFFSET,
1289                                                    FILE_NAME_LEN,
1290                                                    tvb_get_ptr(tvb, FILE_NAME_OFFSET, 1),
1291                                                    "Boot file name not given");
1292                 }
1293         }
1294
1295         voff = VENDOR_INFO_OFFSET;
1296
1297         /* rfc2132 says it SHOULD exist, not that it MUST exist */
1298         if (tvb_bytes_exist(tvb, voff, 4)) {
1299                 if (tvb_get_ntohl(tvb, voff) == 0x63825363) {
1300                         if (tree) {
1301                                 tvb_memcpy(tvb, (void *)&ip_addr, voff, sizeof(ip_addr));
1302                                 proto_tree_add_ipv4_format(bp_tree, hf_bootp_cookie, tvb,
1303                                     voff, 4, ip_addr,
1304                                     "Magic cookie: (OK)");
1305                         }
1306                         voff += 4;
1307                 }
1308                 else {
1309                         if (tree) {
1310                                 proto_tree_add_text(bp_tree,  tvb,
1311                                         voff, 64, "Bootp vendor specific options");
1312                         }
1313                         voff += 64;
1314                 }
1315         }
1316
1317         eoff = tvb_reported_length(tvb);
1318
1319         /*
1320          * In the first pass, we just look for the DHCP message type
1321          * and Vendor class identifier options.
1322          */
1323         tmpvoff = voff;
1324         at_end = FALSE;
1325         while (tmpvoff < eoff && !at_end) {
1326                 tmpvoff += bootp_option(tvb, 0, tmpvoff, eoff, TRUE, &at_end,
1327                     &dhcp_type, &vendor_class_id);
1328         }
1329
1330         /*
1331          * If there was a DHCP message type option, flag this packet
1332          * as DHCP.
1333          */
1334         if (dhcp_type != NULL) {
1335                 /*
1336                  * Yes, this is a DHCP packet, and "dhcp_type" is the
1337                  * packet type.
1338                  */
1339                 if (check_col(pinfo->cinfo, COL_PROTOCOL))
1340                         col_set_str(pinfo->cinfo, COL_PROTOCOL, "DHCP");
1341                 if (check_col(pinfo->cinfo, COL_INFO))
1342                         col_add_fstr(pinfo->cinfo, COL_INFO, "DHCP %-8s - Transaction ID 0x%x",
1343                             dhcp_type, tvb_get_ntohl(tvb, 4));
1344                 if (tree)
1345                         proto_tree_add_boolean_hidden(bp_tree, hf_bootp_dhcp,
1346                             tvb, 0, 0, 1);
1347                 tap_queue_packet( bootp_dhcp_tap, pinfo, (gpointer) dhcp_type);
1348         }
1349
1350         /*
1351          * If we're not building the protocol tree, we don't need to
1352          * make a second pass.
1353          */
1354         if (tree == NULL)
1355                 return;
1356
1357         /*
1358          * OK, now build the protocol tree.
1359          */
1360         at_end = FALSE;
1361         while (voff < eoff && !at_end) {
1362                 voff += bootp_option(tvb, bp_tree, voff, eoff, FALSE, &at_end,
1363                     &dhcp_type, &vendor_class_id);
1364         }
1365         if (voff < eoff) {
1366                 /*
1367                  * Padding after the end option.
1368                  */
1369                 proto_tree_add_text(bp_tree, tvb, voff, eoff - voff, "Padding");
1370         }
1371 }
1372
1373 void
1374 proto_register_bootp(void)
1375 {
1376   static hf_register_info hf[] = {
1377     { &hf_bootp_dhcp,
1378       { "Frame is DHCP",                "bootp.dhcp",    FT_BOOLEAN,
1379         BASE_NONE,                      NULL,            0x0,
1380         "", HFILL }},
1381
1382     { &hf_bootp_type,
1383       { "Message type",                 "bootp.type",    FT_UINT8,
1384          BASE_DEC,                      VALS(op_vals),   0x0,
1385         "", HFILL }},
1386
1387     { &hf_bootp_hw_type,
1388       { "Hardware type",                "bootp.hw.type", FT_UINT8,
1389         BASE_HEX,                       NULL,            0x0,
1390         "", HFILL }},
1391
1392     { &hf_bootp_hw_len,
1393       { "Hardware address length",      "bootp.hw.len",  FT_UINT8,
1394         BASE_DEC,                       NULL,            0x0,
1395         "", HFILL }},
1396
1397     { &hf_bootp_hops,
1398       { "Hops",                         "bootp.hops",    FT_UINT8,
1399         BASE_DEC,                       NULL,            0x0,
1400         "", HFILL }},
1401
1402     { &hf_bootp_id,
1403       { "Transaction ID",               "bootp.id",      FT_UINT32,
1404         BASE_HEX,                        NULL,           0x0,
1405         "", HFILL }},
1406
1407     { &hf_bootp_secs,
1408       { "Seconds elapsed",              "bootp.secs",    FT_UINT16,
1409         BASE_DEC,                        NULL,           0x0,
1410         "", HFILL }},
1411
1412     { &hf_bootp_flags,
1413       { "Bootp flags",                  "bootp.flags",   FT_UINT16,
1414         BASE_HEX,                       NULL,            0x0,
1415         "", HFILL }},
1416
1417     { &hf_bootp_flags_broadcast,
1418       { "Broadcast flag",               "bootp.flags.bc", FT_BOOLEAN,
1419         16,                     TFS(&flag_set_broadcast), BOOTP_BC,
1420         "", HFILL }},
1421
1422     { &hf_bootp_flags_reserved,
1423       { "Reserved flags",               "bootp.flags.reserved", FT_UINT16,
1424         BASE_HEX,                       NULL,           BOOTP_MBZ,
1425         "", HFILL }},
1426
1427     { &hf_bootp_ip_client,
1428       { "Client IP address",            "bootp.ip.client",FT_IPv4,
1429         BASE_NONE,                      NULL,             0x0,
1430         "", HFILL }},
1431
1432     { &hf_bootp_ip_your,
1433       { "Your (client) IP address",     "bootp.ip.your",  FT_IPv4,
1434         BASE_NONE,                      NULL,             0x0,
1435         "", HFILL }},
1436
1437     { &hf_bootp_ip_server,
1438       { "Next server IP address",       "bootp.ip.server",FT_IPv4,
1439         BASE_NONE,                      NULL,             0x0,
1440         "", HFILL }},
1441
1442     { &hf_bootp_ip_relay,
1443       { "Relay agent IP address",       "bootp.ip.relay", FT_IPv4,
1444         BASE_NONE,                      NULL,             0x0,
1445         "", HFILL }},
1446
1447     { &hf_bootp_hw_addr,
1448       { "Client hardware address",      "bootp.hw.addr", FT_BYTES,
1449         BASE_NONE,                      NULL,            0x0,
1450         "", HFILL }},
1451
1452     { &hf_bootp_server,
1453       { "Server host name",             "bootp.server",  FT_STRING,
1454         BASE_NONE,                      NULL,            0x0,
1455         "", HFILL }},
1456
1457     { &hf_bootp_file,
1458       { "Boot file name",               "bootp.file",    FT_STRING,
1459         BASE_NONE,                      NULL,            0x0,
1460         "", HFILL }},
1461
1462     { &hf_bootp_cookie,
1463       { "Magic cookie",                 "bootp.cookie",  FT_IPv4,
1464          BASE_NONE,                     NULL,            0x0,
1465         "", HFILL }},
1466
1467     { &hf_bootp_vendor,
1468       { "Bootp Vendor Options",         "bootp.vendor", FT_BYTES,
1469         BASE_NONE,                      NULL,            0x0,
1470         "", HFILL }},
1471   };
1472   static gint *ett[] = {
1473     &ett_bootp,
1474     &ett_bootp_flags,
1475     &ett_bootp_option,
1476   };
1477
1478   proto_bootp = proto_register_protocol("Bootstrap Protocol", "BOOTP/DHCP",
1479                                         "bootp");
1480   proto_register_field_array(proto_bootp, hf, array_length(hf));
1481   proto_register_subtree_array(ett, array_length(ett));
1482   bootp_dhcp_tap = register_tap("bootp");
1483 }
1484
1485 void
1486 proto_reg_handoff_bootp(void)
1487 {
1488   dissector_handle_t bootp_handle;
1489
1490   bootp_handle = create_dissector_handle(dissect_bootp, proto_bootp);
1491   dissector_add("udp.port", UDP_PORT_BOOTPS, bootp_handle);
1492   dissector_add("udp.port", UDP_PORT_BOOTPC, bootp_handle);
1493 }