If the capture child process exits unexpectedly, give more information
[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.42 2000/11/19 08:53:55 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, const u_char *pd,
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(const u_char *pd, proto_tree *bp_tree, int voff, int eoff)
114 {
115         char                    *text;
116         enum field_type         ftype;
117         u_char                  code = pd[voff];
118         int                     vlen = pd[voff+1];
119         u_char                  byte;
120         int                     i,optp, consumed = vlen + 2;
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 (pd[i] != 0) {
270                                 break;
271                         }
272                 }
273                 i = i - voff;
274                 if (bp_tree != NULL)
275                         proto_tree_add_text(bp_tree, NullTVB, 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, NullTVB, voff, 1, "End Option");
283                 consumed = 1;
284                 return consumed;
285         }
286
287         if (bp_tree == NULL) {
288                 /* Don't put anything in the protocol tree. */
289                 return consumed;
290         }
291
292         text = opt[code].text;
293         /* Special cases */
294         switch (code) {
295
296         case 21:        /* Policy Filter */
297                 if (vlen == 8) {
298                         /* one IP address pair */
299                         proto_tree_add_text(bp_tree, NullTVB, voff, consumed,
300                                 "Option %d: %s = %s/%s", code, text,
301                                 ip_to_str((guint8*)&pd[voff+2]),
302                                 ip_to_str((guint8*)&pd[voff+6]));
303                 } else {
304                         /* > 1 IP address pair. Let's make a sub-tree */
305                         vti = proto_tree_add_text(bp_tree, NullTVB, voff,
306                                 consumed, "Option %d: %s", code, text);
307                         v_tree = proto_item_add_subtree(vti, ett_bootp_option);
308                         for (i = voff + 2; i < voff + consumed; i += 8) {
309                                 proto_tree_add_text(v_tree, NullTVB, i, 8, "IP Address/Mask: %s/%s",
310                                         ip_to_str((guint8*)&pd[i]),
311                                         ip_to_str((guint8*)&pd[i+4]));
312                         }
313                 }
314                 break;
315
316         case 33:        /* Static Route */
317                 if (vlen == 8) {
318                         /* one IP address pair */
319                         proto_tree_add_text(bp_tree, NullTVB, voff, consumed,
320                                 "Option %d: %s = %s/%s", code, text,
321                                 ip_to_str((guint8*)&pd[voff+2]),
322                                 ip_to_str((guint8*)&pd[voff+6]));
323                 } else {
324                         /* > 1 IP address pair. Let's make a sub-tree */
325                         vti = proto_tree_add_text(bp_tree, NullTVB, voff,
326                                 consumed, "Option %d: %s", code, text);
327                         v_tree = proto_item_add_subtree(vti, ett_bootp_option);
328                         for (i = voff + 2; i < voff + consumed; i += 8) {
329                                 proto_tree_add_text(v_tree, NullTVB, i, 8,
330                                         "Destination IP Address/Router: %s/%s",
331                                         ip_to_str((guint8*)&pd[i]),
332                                         ip_to_str((guint8*)&pd[i+4]));
333                         }
334                 }
335                 break;
336
337         case 43:        /* Vendor-Specific Info */
338                 proto_tree_add_text(bp_tree, NullTVB, voff, consumed,
339                                 "Option %d: %s", code, text);
340                 break;
341
342         case 46:        /* NetBIOS-over-TCP/IP Node Type */
343                 byte = pd[voff+2];
344                 proto_tree_add_text(bp_tree, NullTVB, voff, consumed,
345                                 "Option %d: %s = %s", code, text,
346                                 val_to_str(byte, nbnt_vals,
347                                     "Unknown (0x%02x)"));
348                 break;
349                                 
350         case 53:        /* DHCP Message Type */
351                 proto_tree_add_text(bp_tree, NullTVB, voff, 3, "Option %d: %s = DHCP %s",
352                         code, text, get_dhcp_type(pd[voff+2]));
353                 break;
354
355         case 55:        /* Parameter Request List */
356                 vti = proto_tree_add_text(bp_tree, NullTVB, voff,
357                         vlen + 2, "Option %d: %s", code, text);
358                 v_tree = proto_item_add_subtree(vti, ett_bootp_option);
359                 for (i = 0; i < vlen; i++) {
360                         byte = pd[voff+2+i];
361                         if (byte < NUM_OPT_INFOS) {
362                                 proto_tree_add_text(v_tree, NullTVB, voff+2+i, 1, "%d = %s",
363                                                 byte, opt[byte].text);
364                         } else {
365                                 proto_tree_add_text(vti, NullTVB, voff+2+i, 1,
366                                         "Unknown Option Code: %d", byte);
367                         }
368                 }
369                 break;
370
371         case 61:        /* Client Identifier */
372                 /* We *MAY* use hwtype/hwaddr. If we have 7 bytes, I'll
373                    guess that the first is the hwtype, and the last 6
374                    are the hw addr */
375                 if (vlen == 7) {
376                         vti = proto_tree_add_text(bp_tree, NullTVB, voff,
377                                 consumed, "Option %d: %s", code, text);
378                         v_tree = proto_item_add_subtree(vti, ett_bootp_option);
379                         proto_tree_add_text(v_tree, NullTVB, voff+2, 1,
380                                 "Hardware type: %s",
381                                 arphrdtype_to_str(pd[voff+2],
382                                         "Unknown (0x%02x)"));
383                         proto_tree_add_text(v_tree, NullTVB, voff+3, 6,
384                                 "Client hardware address: %s",
385                                 arphrdaddr_to_str((guint8*)&pd[voff+3],
386                                         6, pd[voff+2]));
387                 } else {
388                         /* otherwise, it's opaque data */
389                         proto_tree_add_text(bp_tree, NullTVB, voff, consumed,
390                                 "Option %d: %s (%d bytes)", code, text, vlen);
391                 }
392                 break;
393
394         case 63:        /* NetWare/IP options */
395                 vti = proto_tree_add_text(bp_tree, NullTVB, voff,
396                     consumed, "Option %d: %s", code, text);
397                 v_tree = proto_item_add_subtree(vti, ett_bootp_option);
398
399                 optp = voff+2;
400                 while (optp < voff+consumed)
401                         optp = dissect_netware_ip_suboption(v_tree, pd, optp);
402                 break;
403
404         default:        /* not special */
405                 break;
406         }
407
408         /* Normal cases */
409         if (code < NUM_OPT_INFOS) {
410                 text = opt[code].text;
411                 ftype = opt[code].ftype;
412
413                 switch (ftype) {
414
415                 case special:
416                         return consumed;
417
418                 case ipv4:
419                         if (vlen == 4) {
420                                 /* one IP address */
421                                 proto_tree_add_text(bp_tree, NullTVB, voff, consumed,
422                                         "Option %d: %s = %s", code, text,
423                                         ip_to_str((guint8*)&pd[voff+2]));
424                         } else {
425                                 /* > 1 IP addresses. Let's make a sub-tree */
426                                 vti = proto_tree_add_text(bp_tree, NullTVB, voff,
427                                         consumed, "Option %d: %s", code, text);
428                                 v_tree = proto_item_add_subtree(vti, ett_bootp_option);
429                                 for (i = voff + 2; i < voff + consumed; i += 4) {
430                                         proto_tree_add_text(v_tree, NullTVB, i, 4, "IP Address: %s",
431                                                 ip_to_str((guint8*)&pd[i]));
432                                 }
433                         }
434                         break;
435
436                 case string:
437                         /* Fix for non null-terminated string supplied by
438                          * John Lines <John.Lines@aeat.co.uk>
439                          */
440                         proto_tree_add_text(bp_tree, NullTVB, voff, consumed,
441                                         "Option %d: %s = %.*s", code, text, vlen, &pd[voff+2]);
442                         break;
443
444                 case opaque:
445                         proto_tree_add_text(bp_tree, NullTVB, voff, consumed,
446                                         "Option %d: %s (%d bytes)",
447                                         code, text, vlen);
448                         break;
449
450                 case val_u_short:
451                         if (vlen == 2) {
452                                 /* one u_short */
453                                 proto_tree_add_text(bp_tree, NullTVB, voff, consumed,
454                                                 "Option %d: %s = %d", code, text,
455                                                 pntohs(&pd[voff+2]));
456                         } else {
457                                 /* > 1 u_short */
458                                 vti = proto_tree_add_text(bp_tree, NullTVB, voff,
459                                         consumed, "Option %d: %s", code, text);
460                                 v_tree = proto_item_add_subtree(vti, ett_bootp_option);
461                                 for (i = voff + 2; i < voff + consumed; i += 2) {
462                                         proto_tree_add_text(v_tree, NullTVB, i, 4, "Value: %d",
463                                                 pntohs(&pd[i]));
464                                 }
465                         }
466                         break;
467
468                 case val_u_long:
469                         proto_tree_add_text(bp_tree, NullTVB, voff, consumed,
470                                         "Option %d: %s = %d", code, text,
471                                         pntohl(&pd[voff+2]));
472                         break;
473
474                 case val_u_byte:
475                         proto_tree_add_text(bp_tree, NullTVB, voff, consumed,
476                                         "Option %d: %s = %d", code, text, pd[voff+2]);
477                         break;
478
479                 case toggle:
480                         i = pd[voff+2];
481                         if (i != 0 && i != 1) {
482                                 proto_tree_add_text(bp_tree, NullTVB, voff, consumed,
483                                                 "Option %d: %s = Invalid Value %d", code, text,
484                                                 pd[voff+2]);
485                         } else {
486                                 proto_tree_add_text(bp_tree, NullTVB, voff, consumed,
487                                                 "Option %d: %s = %s", code, text,
488                                                 pd[voff+2] == 0 ? "Disabled" : "Enabled");
489                         }
490                         break;
491
492                 case yes_no:
493                         i = pd[voff+2];
494                         if (i != 0 && i != 1) {
495                                 proto_tree_add_text(bp_tree, NullTVB, voff, consumed,
496                                                 "Option %d: %s = Invalid Value %d", code, text,
497                                                 pd[voff+2]);
498                         } else {
499                                 proto_tree_add_text(bp_tree, NullTVB, voff, consumed,
500                                                 "Option %d: %s = %s", code, text,
501                                                 pd[voff+2] == 0 ? "No" : "Yes");
502                         }
503                         break;
504
505                 case time_in_secs:
506                         time_secs = pntohl(&pd[voff+2]);
507                         proto_tree_add_text(bp_tree, NullTVB, voff, consumed,
508                                 "Option %d: %s = %s", code, text,
509                                 ((time_secs == 0xffffffff) ?
510                                     "infinity" :
511                                     time_secs_to_str(time_secs)));
512                         break;
513
514                 default:
515                         proto_tree_add_text(bp_tree, NullTVB, voff, consumed,
516                                         "Option %d: %s (%d bytes)", code, text, vlen);
517                 }
518         } else {
519                 proto_tree_add_text(bp_tree, NullTVB, voff, consumed,
520                                 "Unknown Option Code: %d (%d bytes)", code, vlen);
521         }
522
523         return consumed;
524 }
525
526 static int
527 dissect_netware_ip_suboption(proto_tree *v_tree, const u_char *pd, int optp)
528 {
529         int slask;
530         proto_tree *o63_v_tree;
531         proto_item *vti;
532
533         struct o63_opt_info { 
534                 char    *truet;
535                 char    *falset;
536                 enum field_type ft;
537         };
538
539         static struct o63_opt_info o63_opt[]= {
540                 /* 0 */ {"","",none},
541                 /* 1 */ {"NWIP does not exist on subnet","",string},
542                 /* 2 */ {"NWIP exist in options area","",string},
543                 /* 3 */ {"NWIP exists in sname/file","",string},
544                 /* 4 */ {"NWIP exists, but too big","",string},
545                 /* 5 */ {"Broadcast for nearest Netware server","Do NOT Broadcast for nearest Netware server",yes_no}, 
546                 /* 6 */ {"Preferred DSS server","",ipv4},
547                 /* 7 */ {"Nearest NWIP server","",ipv4},
548                 /* 8 */ {"Autoretries","",val_u_short},
549                 /* 9 */ {"Autoretry delay, secs ","",val_u_short},
550                 /* 10*/ {"Support NetWare/IP v1.1","Do NOT support NetWare/IP v1.1",yes_no},
551                 /* 11*/ {"Primary DSS ", "" , special}
552         };
553                 
554         if (pd[optp] > NUM_O63_SUBOPTS) {
555                 proto_tree_add_text(v_tree, NullTVB,optp,1,"Unknown suboption %d", pd[optp]);
556                 optp++;
557         } else {
558                 switch (o63_opt[pd[optp]].ft) {
559
560                 case string:
561                         proto_tree_add_text(v_tree, NullTVB, optp, 2, "Suboption %d: %s", pd[optp], o63_opt[pd[optp]].truet);
562                         optp+=2;
563                         break;
564
565                 case yes_no:
566                         if (pd[optp+2]==1) {
567                                 proto_tree_add_text(v_tree, NullTVB, optp, 3, "Suboption %d: %s", pd[optp], o63_opt[pd[optp]].truet);
568                         } else {
569                                 proto_tree_add_text(v_tree, NullTVB, optp, 3, "Suboption %d: %s" , pd[optp], o63_opt[pd[optp]].falset);
570                         }
571                         optp+=3;
572                         break;
573
574                 case special:   
575                         proto_tree_add_text(v_tree, NullTVB, optp, 6,
576                             "Suboption %d: %s = %s" ,
577                             pd[optp], o63_opt[pd[optp]].truet,
578                             ip_to_str((guint8*)&pd[optp+2]));
579                         optp=optp+6;
580                         break;
581
582                 case val_u_short:
583                         proto_tree_add_text(v_tree, NullTVB, optp, 3, "Suboption %d: %s = %d",pd[optp], o63_opt[pd[optp]].truet, pd[optp+2]);
584                         optp+=3;
585                         break;
586                                                         
587                 case ipv4:
588                         if (pd[optp+1] == 4) {
589                                 /* one IP address */
590                                 proto_tree_add_text(v_tree, NullTVB, optp, 6,
591                                     "Suboption %d : %s = %s",
592                                     pd[optp], o63_opt[pd[optp]].truet,
593                                     ip_to_str((guint8*)&pd[optp+2]));
594                                 optp=optp+6;
595                         } else {
596                                 /* > 1 IP addresses. Let's make a sub-tree */
597                                 vti = proto_tree_add_text(v_tree, NullTVB, optp,
598                                     pd[optp+1]+2, "Suboption %d: %s",
599                                     pd[optp], o63_opt[pd[optp]].truet);
600                                 o63_v_tree = proto_item_add_subtree(vti, ett_bootp_option);
601                                 for (slask = optp + 2 ; slask < optp+pd[optp+1]; slask += 4) {
602                                         proto_tree_add_text(o63_v_tree, NullTVB, slask, 4, "IP Address: %s",
603                                         ip_to_str((guint8*)&pd[slask]));
604                                 }
605                                 optp=slask;
606                         }
607                         break;
608                 default:
609                         proto_tree_add_text(v_tree, NullTVB,optp,1,"Unknown suboption %d", pd[optp]);
610                         optp++;
611                         break;
612                 }
613         }
614         return optp;
615 }
616
617 static void
618 dissect_bootp(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
619 {
620         proto_tree      *bp_tree = NULL;
621         proto_item      *ti;
622         int             voff, eoff; /* vender offset, end offset */
623         guint32         ip_addr;
624         const char      *dhcp_type;
625
626         OLD_CHECK_DISPLAY_AS_DATA(proto_bootp, pd, offset, fd, tree);
627
628         dhcp_type = NULL;
629
630         if (check_col(fd, COL_PROTOCOL))
631                 col_set_str(fd, COL_PROTOCOL, "BOOTP");
632
633         if (check_col(fd, COL_INFO)) {
634                 if (pd[offset] == 1) {
635                         col_add_fstr(fd, COL_INFO, "Boot Request from %s",
636                                 arphrdaddr_to_str((guint8*)&pd[offset+28],
637                                         pd[offset+2], pd[offset+1]));
638                 }
639                 else {
640                         col_set_str(fd, COL_INFO, "Boot Reply");
641                 }
642         }
643
644         if (tree) {
645                 ti = proto_tree_add_item(tree, proto_bootp, NullTVB, offset, END_OF_FRAME, FALSE);
646                 bp_tree = proto_item_add_subtree(ti, ett_bootp);
647
648                 proto_tree_add_uint_format(bp_tree, hf_bootp_type, NullTVB, 
649                                            offset, 1,
650                                            pd[offset], 
651                                            pd[offset] == 1 ?
652                                            "Boot Request" : "Boot Reply");
653                 proto_tree_add_uint_format(bp_tree, hf_bootp_hw_type, NullTVB,
654                                            offset + 1, 1,
655                                            pd[offset+1],
656                                            "Hardware type: %s",
657                                            arphrdtype_to_str(pd[offset+1],
658                                                              "Unknown (0x%02x)"));
659                 proto_tree_add_uint(bp_tree, hf_bootp_hw_len, NullTVB,
660                                     offset + 2, 1, pd[offset+2]);
661                 proto_tree_add_uint(bp_tree, hf_bootp_hops, NullTVB,
662                                     offset + 3, 1, pd[offset+3]);
663                 proto_tree_add_uint(bp_tree, hf_bootp_id, NullTVB,
664                                    offset + 4, 4, pntohl(&pd[offset+4]));
665                 proto_tree_add_uint(bp_tree, hf_bootp_secs, NullTVB,
666                                     offset + 8, 2, pntohs(&pd[offset+8]));
667                 proto_tree_add_uint(bp_tree, hf_bootp_flag, NullTVB,
668                                     offset + 10, 2, pntohs(&pd[offset+10]) & 0x8000);
669
670                 memcpy(&ip_addr, &pd[offset+12], sizeof(ip_addr));
671                 proto_tree_add_ipv4(bp_tree, hf_bootp_ip_client, NullTVB, 
672                                     offset + 12, 4, ip_addr);
673                 memcpy(&ip_addr, &pd[offset+16], sizeof(ip_addr));
674                 proto_tree_add_ipv4(bp_tree, hf_bootp_ip_your, NullTVB, 
675                                     offset + 16, 4, ip_addr);
676                 memcpy(&ip_addr, &pd[offset+20], sizeof(ip_addr));
677                 proto_tree_add_ipv4(bp_tree, hf_bootp_ip_server, NullTVB,
678                                     offset + 20, 4, ip_addr);
679                 memcpy(&ip_addr, &pd[offset+24], sizeof(ip_addr));
680                 proto_tree_add_ipv4(bp_tree, hf_bootp_ip_relay, NullTVB,
681                                     offset + 24, 4, ip_addr);
682
683                 if (pd[offset+2] > 0) {
684                         proto_tree_add_bytes_format(bp_tree, hf_bootp_hw_addr, NullTVB, 
685                                                    offset + 28, pd[offset+2],
686                                                    &pd[offset+28],
687                                                    "Client hardware address: %s",
688                                                    arphrdaddr_to_str((guint8*)&pd[offset+28],
689                                                                      pd[offset+2], pd[offset+1]));
690                 }
691                 else {
692                         proto_tree_add_text(bp_tree,  NullTVB, 
693                                                    offset + 28, 0, "Client address not given");
694                 }
695
696                 /* The server host name is optional */
697                 if (pd[offset+44]) {
698                         proto_tree_add_string_format(bp_tree, hf_bootp_server, NullTVB,
699                                                    offset + 44, 64,
700                                                    &pd[offset+44],
701                                                    "Server host name: %s",
702                                                    &pd[offset+44]);
703                 }
704                 else {
705                         proto_tree_add_string_format(bp_tree, hf_bootp_server, NullTVB,
706                                                    offset + 44, 64,
707                                                    &pd[offset+44],
708                                                    "Server host name not given");
709                 }
710
711                 /* Boot file */
712                 if (pd[offset+108]) {
713                         proto_tree_add_string_format(bp_tree, hf_bootp_file, NullTVB,
714                                                    offset + 108, 128,
715                                                    &pd[offset+108],
716                                                    "Boot file name: %s",
717                                                    &pd[offset+108]);
718                 }
719                 else {
720                         proto_tree_add_string_format(bp_tree, hf_bootp_file, NullTVB,
721                                                    offset + 108, 128,
722                                                    &pd[offset+108],
723                                                    "Boot file name not given");
724                 }
725
726                 memcpy(&ip_addr, &pd[offset + 236], sizeof(ip_addr));
727                 if (pntohl(&pd[offset+236]) == 0x63825363) {
728                         proto_tree_add_ipv4_format(bp_tree, hf_bootp_cookie, NullTVB,
729                                             offset + 236, 4, ip_addr,
730                                             "Magic cookie: (OK)");
731                 }
732                 else {
733                         proto_tree_add_ipv4(bp_tree, hf_bootp_cookie, NullTVB,
734                                             offset + 236, 4, ip_addr);
735                 }
736         }
737
738         voff = offset+240;
739         eoff = pi.captured_len;
740         while (voff < eoff) {
741                 /* Handle the DHCP option specially here, so that we
742                    can flag DHCP packets as such. */
743                 if (pd[voff] == 53)
744                         dhcp_type = get_dhcp_type(pd[voff+2]);
745                 voff += bootp_option(pd, bp_tree, voff, eoff);
746         }
747         if (dhcp_type != NULL ) {
748                 if (check_col(fd, COL_PROTOCOL))
749                         col_set_str(fd, COL_PROTOCOL, "DHCP");
750                 if (check_col(fd, COL_INFO))
751                         col_add_fstr(fd, COL_INFO, "DHCP %-8s - Transaction ID 0x%x",
752                             dhcp_type, pntohl(&pd[offset+4]));
753                 if (tree)
754                         proto_tree_add_boolean_hidden(bp_tree, hf_bootp_dhcp,
755                             NullTVB, 0, 0, 1);
756         }
757 }
758
759 void
760 proto_register_bootp(void)
761 {
762   static hf_register_info hf[] = {
763     { &hf_bootp_dhcp,
764       { "Frame is DHCP",                "bootp.dhcp",    FT_BOOLEAN,  BASE_NONE, NULL, 0x0,
765         "" }},                            
766                       
767     { &hf_bootp_type,
768       { "Message type",                 "bootp.type",    FT_UINT8,  BASE_NONE, NULL, 0x0,
769         "" }},
770
771     { &hf_bootp_hw_type,
772       { "Hardware type",                "bootp.hw.type", FT_UINT8,  BASE_HEX, NULL, 0x0,
773         "" }},
774
775     { &hf_bootp_hw_len,
776       { "Hardware address length",      "bootp.hw.len",  FT_UINT8,  BASE_DEC, NULL, 0x0,
777         "" }},
778
779     { &hf_bootp_hops,
780       { "Hops",                         "bootp.hops",    FT_UINT8,  BASE_DEC, NULL, 0x0,
781         "" }},
782
783     { &hf_bootp_id,
784       { "Transaction ID",               "bootp.id",      FT_UINT32, BASE_HEX, NULL, 0x0,
785         "" }},
786
787     { &hf_bootp_secs,
788       { "Seconds elapsed",              "bootp.secs",    FT_UINT16, BASE_DEC, NULL, 0x0,
789         "" }},
790
791     { &hf_bootp_flag,
792       { "Broadcast flag",               "bootp.flag",    FT_UINT16, BASE_HEX, NULL, 0x0,
793         "" }},
794
795     { &hf_bootp_ip_client,
796       { "Client IP address",            "bootp.ip.client",FT_IPv4,  BASE_NONE, NULL, 0x0,
797         "" }},
798
799     { &hf_bootp_ip_your,
800       { "Your (client) IP address",     "bootp.ip.your",  FT_IPv4,  BASE_NONE, NULL, 0x0,
801         "" }},
802
803     { &hf_bootp_ip_server,
804       { "Next server IP address",       "bootp.ip.server",FT_IPv4,  BASE_NONE, NULL, 0x0,
805         "" }},
806
807     { &hf_bootp_ip_relay,
808       { "Relay agent IP address",       "bootp.ip.relay", FT_IPv4,  BASE_NONE, NULL, 0x0,
809         "" }},
810
811     { &hf_bootp_hw_addr,
812       { "Client hardware address",      "bootp.hw.addr", FT_BYTES,  BASE_NONE, NULL, 0x0,
813         "" }},
814
815     { &hf_bootp_server,
816       { "Server host name",             "bootp.server",  FT_STRING, BASE_NONE, NULL, 0x0,
817         "" }},
818
819     { &hf_bootp_file,
820       { "Boot file name",               "bootp.file",    FT_STRING, BASE_NONE, NULL, 0x0,
821         "" }},
822
823     { &hf_bootp_cookie,
824       { "Magic cookie",                 "bootp.cookie",  FT_IPv4,   BASE_NONE, NULL, 0x0,
825         "" }},
826   };
827   static gint *ett[] = {
828     &ett_bootp,
829     &ett_bootp_option,
830   };
831   
832   proto_bootp = proto_register_protocol("Bootstrap Protocol", "bootp");
833   proto_register_field_array(proto_bootp, hf, array_length(hf));
834   proto_register_subtree_array(ett, array_length(ett));
835 }
836
837 void
838 proto_reg_handoff_bootp(void)
839 {
840   old_dissector_add("udp.port", UDP_PORT_BOOTPS, dissect_bootp);
841 }