There is no string value assigned to the "ip.fragment.error",
[obnox/wireshark/wip.git] / packet-ip.c
1 /* packet-ip.c
2  * Routines for IP and miscellaneous IP protocol packet disassembly
3  *
4  * $Id: packet-ip.c,v 1.134 2001/06/05 05:54:14 guy Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@zing.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * 
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  * 
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  * 
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #ifdef HAVE_SYS_TYPES_H
31 # include <sys/types.h>
32 #endif
33
34 #ifdef HAVE_NETINET_IN_H
35 # include <netinet/in.h>
36 #endif
37
38 #include <stdio.h>
39 #include <string.h>
40 #include <glib.h>
41
42 #ifdef NEED_SNPRINTF_H
43 # include "snprintf.h"
44 #endif
45
46 #include "packet.h"
47 #include "resolv.h"
48 #include "ipproto.h"
49 #include "prefs.h"
50 #include "etypes.h"
51 #include "greproto.h"
52 #include "ppptypes.h"
53 #include "llcsaps.h"
54 #include "aftypes.h"
55 #include "packet-ip.h"
56 #include "packet-ipsec.h"
57 #include "in_cksum.h"
58 #include "nlpid.h"
59
60 static void dissect_icmp(tvbuff_t *, packet_info *, proto_tree *);
61
62 /* Decode the old IPv4 TOS field as the DiffServ DS Field */
63 static gboolean g_ip_dscp_actif = TRUE;
64
65 /* Defragment fragmented IP datagrams */
66 static gboolean ip_defragment = FALSE;
67
68 /* Place IP summary in proto tree */
69 static gboolean ip_summary_in_tree = TRUE;
70
71 static int proto_ip = -1;
72 static int hf_ip_version = -1;
73 static int hf_ip_hdr_len = -1;
74 static int hf_ip_dsfield = -1;
75 static int hf_ip_dsfield_dscp = -1;
76 static int hf_ip_dsfield_ect = -1;
77 static int hf_ip_dsfield_ce = -1;
78 static int hf_ip_tos = -1;
79 static int hf_ip_tos_precedence = -1;
80 static int hf_ip_tos_delay = -1;
81 static int hf_ip_tos_throughput = -1;
82 static int hf_ip_tos_reliability = -1;
83 static int hf_ip_tos_cost = -1;
84 static int hf_ip_len = -1;
85 static int hf_ip_id = -1;
86 static int hf_ip_dst = -1;
87 static int hf_ip_src = -1;
88 static int hf_ip_addr = -1;
89 static int hf_ip_flags = -1;
90 static int hf_ip_flags_df = -1;
91 static int hf_ip_flags_mf = -1;
92 static int hf_ip_frag_offset = -1;
93 static int hf_ip_ttl = -1;
94 static int hf_ip_proto = -1;
95 static int hf_ip_checksum = -1;
96 static int hf_ip_checksum_bad = -1;
97 static int hf_ip_fragments = -1;
98 static int hf_ip_fragment = -1;
99 static int hf_ip_fragment_overlap = -1;
100 static int hf_ip_fragment_overlap_conflict = -1;
101 static int hf_ip_fragment_multiple_tails = -1;
102 static int hf_ip_fragment_too_long_fragment = -1;
103 static int hf_ip_fragment_error = -1;
104
105 static gint ett_ip = -1;
106 static gint ett_ip_dsfield = -1;
107 static gint ett_ip_tos = -1;
108 static gint ett_ip_off = -1;
109 static gint ett_ip_options = -1;
110 static gint ett_ip_option_sec = -1;
111 static gint ett_ip_option_route = -1;
112 static gint ett_ip_option_timestamp = -1;
113 static gint ett_ip_fragments = -1;
114 static gint ett_ip_fragment  = -1;
115
116 /* Used by IPv6 as well, so not static */
117 dissector_table_t ip_dissector_table;
118
119 static int proto_icmp = -1;
120 static int hf_icmp_type = -1;
121 static int hf_icmp_code = -1;
122 static int hf_icmp_checksum = -1;
123 static int hf_icmp_checksum_bad = -1;
124
125 static gint ett_icmp = -1;
126
127 /* ICMP definitions */
128
129 #define ICMP_ECHOREPLY     0
130 #define ICMP_UNREACH       3
131 #define ICMP_SOURCEQUENCH  4
132 #define ICMP_REDIRECT      5
133 #define ICMP_ECHO          8
134 #define ICMP_RTRADVERT     9
135 #define ICMP_RTRSOLICIT   10
136 #define ICMP_TIMXCEED     11
137 #define ICMP_PARAMPROB    12
138 #define ICMP_TSTAMP       13
139 #define ICMP_TSTAMPREPLY  14
140 #define ICMP_IREQ         15
141 #define ICMP_IREQREPLY    16
142 #define ICMP_MASKREQ      17
143 #define ICMP_MASKREPLY    18
144
145 /* ICMP UNREACHABLE */
146
147 #define ICMP_NET_UNREACH        0       /* Network Unreachable */
148 #define ICMP_HOST_UNREACH       1       /* Host Unreachable */
149 #define ICMP_PROT_UNREACH       2       /* Protocol Unreachable */
150 #define ICMP_PORT_UNREACH       3       /* Port Unreachable */
151 #define ICMP_FRAG_NEEDED        4       /* Fragmentation Needed/DF set */
152 #define ICMP_SR_FAILED          5       /* Source Route failed */
153 #define ICMP_NET_UNKNOWN        6
154 #define ICMP_HOST_UNKNOWN       7
155 #define ICMP_HOST_ISOLATED      8
156 #define ICMP_NET_ANO            9
157 #define ICMP_HOST_ANO           10
158 #define ICMP_NET_UNR_TOS        11
159 #define ICMP_HOST_UNR_TOS       12
160 #define ICMP_PKT_FILTERED       13      /* Packet filtered */
161 #define ICMP_PREC_VIOLATION     14      /* Precedence violation */
162 #define ICMP_PREC_CUTOFF        15      /* Precedence cut off */
163
164
165 /* IP structs and definitions */
166
167 typedef struct _e_ip 
168    {
169    guint8  ip_v_hl; /* combines ip_v and ip_hl */
170    guint8  ip_tos;
171    guint16 ip_len;
172    guint16 ip_id;
173    guint16 ip_off;
174    guint8  ip_ttl;
175    guint8  ip_p;
176    guint16 ip_sum;
177    guint32 ip_src;
178    guint32 ip_dst;
179    } e_ip;
180
181 /* Offsets of fields within an IP header. */
182 #define IPH_V_HL        0
183 #define IPH_TOS         1
184 #define IPH_LEN         2
185 #define IPH_ID          4
186 #define IPH_TTL         6
187 #define IPH_OFF         8
188 #define IPH_P           9
189 #define IPH_SUM         10
190 #define IPH_SRC         12
191 #define IPH_DST         16
192
193 /* Minimum IP header length. */
194 #define IPH_MIN_LEN     20
195
196 /* IP flags. */
197 #define IP_CE           0x8000          /* Flag: "Congestion"           */
198 #define IP_DF           0x4000          /* Flag: "Don't Fragment"       */
199 #define IP_MF           0x2000          /* Flag: "More Fragments"       */
200 #define IP_OFFSET       0x1FFF          /* "Fragment Offset" part       */
201
202 /* Differentiated Services Field. See RFCs 2474, 2597 and 2598. */
203 #define IPDSFIELD_DSCP_MASK     0xFC
204 #define IPDSFIELD_ECN_MASK     0x03
205 #define IPDSFIELD_DSCP_SHIFT    2
206 #define IPDSFIELD_DSCP(dsfield) (((dsfield)&IPDSFIELD_DSCP_MASK)>>IPDSFIELD_DSCP_SHIFT)
207 #define IPDSFIELD_ECN(dsfield)  ((dsfield)&IPDSFIELD_ECN_MASK)
208 #define IPDSFIELD_DSCP_DEFAULT  0x00
209 #define IPDSFIELD_DSCP_CS1      0x08
210 #define IPDSFIELD_DSCP_CS2      0x10
211 #define IPDSFIELD_DSCP_CS3      0x18
212 #define IPDSFIELD_DSCP_CS4      0x20
213 #define IPDSFIELD_DSCP_CS5      0x28
214 #define IPDSFIELD_DSCP_CS6      0x30
215 #define IPDSFIELD_DSCP_CS7      0x38
216 #define IPDSFIELD_DSCP_AF11     0x0A
217 #define IPDSFIELD_DSCP_AF12     0x0C
218 #define IPDSFIELD_DSCP_AF13     0x0E
219 #define IPDSFIELD_DSCP_AF21     0x12
220 #define IPDSFIELD_DSCP_AF22     0x14
221 #define IPDSFIELD_DSCP_AF23     0x16
222 #define IPDSFIELD_DSCP_AF31     0x1A
223 #define IPDSFIELD_DSCP_AF32     0x1C
224 #define IPDSFIELD_DSCP_AF33     0x1E
225 #define IPDSFIELD_DSCP_AF41     0x22
226 #define IPDSFIELD_DSCP_AF42     0x24
227 #define IPDSFIELD_DSCP_AF43     0x26
228 #define IPDSFIELD_DSCP_EF       0x2E
229 #define IPDSFIELD_ECT_MASK      0x02
230 #define IPDSFIELD_CE_MASK       0x01
231
232 /* IP TOS, superseded by the DS Field, RFC 2474. */
233 #define IPTOS_TOS_MASK    0x1E
234 #define IPTOS_TOS(tos)    ((tos) & IPTOS_TOS_MASK)
235 #define IPTOS_NONE        0x00
236 #define IPTOS_LOWCOST     0x02
237 #define IPTOS_RELIABILITY 0x04
238 #define IPTOS_THROUGHPUT  0x08
239 #define IPTOS_LOWDELAY    0x10
240 #define IPTOS_SECURITY    0x1E
241
242 #define IPTOS_PREC_MASK         0xE0
243 #define IPTOS_PREC_SHIFT        5
244 #define IPTOS_PREC(tos)         (((tos)&IPTOS_PREC_MASK)>>IPTOS_PREC_SHIFT)
245 #define IPTOS_PREC_NETCONTROL           7
246 #define IPTOS_PREC_INTERNETCONTROL      6
247 #define IPTOS_PREC_CRITIC_ECP           5
248 #define IPTOS_PREC_FLASHOVERRIDE        4
249 #define IPTOS_PREC_FLASH                3
250 #define IPTOS_PREC_IMMEDIATE            2
251 #define IPTOS_PREC_PRIORITY             1
252 #define IPTOS_PREC_ROUTINE              0
253
254 /* IP options */
255 #define IPOPT_COPY              0x80
256
257 #define IPOPT_CONTROL           0x00
258 #define IPOPT_RESERVED1         0x20
259 #define IPOPT_MEASUREMENT       0x40
260 #define IPOPT_RESERVED2         0x60
261
262 #define IPOPT_END       (0 |IPOPT_CONTROL)
263 #define IPOPT_NOOP      (1 |IPOPT_CONTROL)
264 #define IPOPT_SEC       (2 |IPOPT_CONTROL|IPOPT_COPY)
265 #define IPOPT_LSRR      (3 |IPOPT_CONTROL|IPOPT_COPY)
266 #define IPOPT_TIMESTAMP (4 |IPOPT_MEASUREMENT)
267 #define IPOPT_RR        (7 |IPOPT_CONTROL)
268 #define IPOPT_SID       (8 |IPOPT_CONTROL|IPOPT_COPY)
269 #define IPOPT_SSRR      (9 |IPOPT_CONTROL|IPOPT_COPY)
270 #define IPOPT_RA        (20|IPOPT_CONTROL|IPOPT_COPY)
271
272 /* IP option lengths */
273 #define IPOLEN_SEC      11
274 #define IPOLEN_LSRR_MIN 3
275 #define IPOLEN_TIMESTAMP_MIN 5
276 #define IPOLEN_RR_MIN   3
277 #define IPOLEN_SID      4
278 #define IPOLEN_SSRR_MIN 3
279 #define IPOLEN_RA       4
280
281 #define IPSEC_UNCLASSIFIED      0x0000
282 #define IPSEC_CONFIDENTIAL      0xF135
283 #define IPSEC_EFTO              0x789A
284 #define IPSEC_MMMM              0xBC4D
285 #define IPSEC_RESTRICTED        0xAF13
286 #define IPSEC_SECRET            0xD788
287 #define IPSEC_TOPSECRET         0x6BC5
288 #define IPSEC_RESERVED1         0x35E2
289 #define IPSEC_RESERVED2         0x9AF1
290 #define IPSEC_RESERVED3         0x4D78
291 #define IPSEC_RESERVED4         0x24BD
292 #define IPSEC_RESERVED5         0x135E
293 #define IPSEC_RESERVED6         0x89AF
294 #define IPSEC_RESERVED7         0xC4D6
295 #define IPSEC_RESERVED8         0xE26B
296
297 #define IPOPT_TS_TSONLY         0               /* timestamps only */
298 #define IPOPT_TS_TSANDADDR      1               /* timestamps and addresses */
299 #define IPOPT_TS_PRESPEC        3               /* specified modules only */
300
301 /*
302  * defragmentation of IPv4
303  */
304 static GHashTable *ip_fragment_table=NULL;
305
306 typedef struct _ip_fragment_key {
307         guint32 srcip;
308         guint32 dstip;
309         guint32 id;
310 } ip_fragment_key;
311
312 /* make sure that all flags that are set in a fragment entry is also set for
313  * the flags field of ipfd_head !!!
314  */
315 /* only in ipfd_head: packet is defragmented */
316 #define IPD_DEFRAGMENTED        0x0001
317 /* there are overlapping fragments */
318 #define IPD_OVERLAP             0x0002
319 /* overlapping fragments contain different data */ 
320 #define IPD_OVERLAPCONFLICT     0x0004  
321 /* more than one fragment which indicates end-of data */
322 #define IPD_MULTIPLETAILS       0x0008
323 /* fragment contains data past the end of the datagram */
324 #define IPD_TOOLONGFRAGMENT     0x0010
325 typedef struct _ip_fragment_data {
326         struct _ip_fragment_data *next;
327         guint32 frame;
328         guint32 offset;
329         guint32 len;
330         guint32 datalen; /*Only valid in first item of list */
331         guint32 flags;
332         unsigned char *data;
333 } ip_fragment_data;
334
335 #define LINK_FRAG(ipfd_head,ipfd)                                       \
336         {       ip_fragment_data *ipfd_i;                               \
337                 /* add fragment to list, keep list sorted */            \
338                 for(ipfd_i=ipfd_head;ipfd_i->next;ipfd_i=ipfd_i->next){ \
339                         if( (ipfd->offset) < (ipfd_i->next->offset) )   \
340                                 break;                                  \
341                 }                                                       \
342                 ipfd->next=ipfd_i->next;                                \
343                 ipfd_i->next=ipfd;                                      \
344         }
345
346
347 static gint
348 ip_fragment_equal(gconstpointer k1, gconstpointer k2)
349 {
350         ip_fragment_key* key1 = (ip_fragment_key*) k1;
351         ip_fragment_key* key2 = (ip_fragment_key*) k2;
352
353         return ( ( (key1->srcip == key2->srcip) &&
354                    (key1->dstip == key2->dstip) &&
355                    (key1->id    == key2->id) 
356                  ) ?
357                  TRUE : FALSE);
358 }
359
360 static guint
361 ip_fragment_hash(gconstpointer k)
362 {
363         ip_fragment_key* key = (ip_fragment_key*) k;
364
365         return (key->srcip ^ key->dstip ^ key->id);
366 }
367
368 static gboolean
369 free_all_fragments(gpointer key, gpointer value, gpointer user_data)
370 {
371         ip_fragment_data *ipfd_head;
372         ip_fragment_data *ipfd_i;
373
374         ipfd_head=value;
375         while(ipfd_head){
376                 ipfd_i=ipfd_head->next;
377                 if(ipfd_head->data){
378                         g_free(ipfd_head->data);
379                 }
380                 g_free(ipfd_head);
381                 ipfd_head=ipfd_i;
382         }
383
384         g_free(key);
385         return TRUE;
386 }
387
388 static void
389 ip_defragment_init(void)
390 {
391         if( ip_fragment_table != NULL ){
392                 /* The fragment hash table exists.
393                  * 
394                  * Remove all entries and free all memory.
395                  */
396                 g_hash_table_foreach_remove(ip_fragment_table,free_all_fragments,NULL);
397         } else {
398                 /* The fragment table does not exist. Create it */
399                 ip_fragment_table = g_hash_table_new(ip_fragment_hash,
400                                 ip_fragment_equal);
401         }
402 }
403
404 /* This function adds a new fragment to the fragment hash table
405  * If this is the first fragment seen in for this ip packet,
406  * a new entry is created in the hash table, otherwise
407  * This fragment is just added to the linked list of 
408  * fragments for this packet.
409  * The list of fragments for a specific ip-packet is kept sorted for
410  * easier handling.
411  * tvb had better contain an IPv4 packet, or BAD things will probably happen.
412  *
413  * Returns a pointer to the head of the fragment data list if we have all the
414  * fragments, NULL otherwise.
415  */
416 static ip_fragment_data *
417 ip_fragment_add(tvbuff_t *tvb, int offset, packet_info *pinfo, guint32 id,
418                 guint32 floff)
419 {
420         ip_fragment_key *ipfk;
421         ip_fragment_data *ipfd_head;
422         ip_fragment_data *ipfd;
423         ip_fragment_data *ipfd_i;
424         guint32 frag_offset;
425         guint32 max,dfpos;
426
427
428         /* create key to search hash with */
429         ipfk=g_malloc(sizeof(ip_fragment_key));
430         memcpy(&ipfk->srcip, pinfo->src.data, sizeof(ipfk->srcip));
431         memcpy(&ipfk->dstip, pinfo->dst.data, sizeof(ipfk->dstip));
432         ipfk->id    = id;
433
434         ipfd_head=g_hash_table_lookup(ip_fragment_table, ipfk);
435
436
437         /* have we already seen this frame ?*/
438         if( pinfo->fd->flags.visited ){
439                 g_free(ipfk);
440                 if (ipfd_head != NULL && ipfd_head->flags & IPD_DEFRAGMENTED) {
441                         return ipfd_head;
442                 } else {
443                         return NULL;
444                 }
445         }
446
447
448
449         frag_offset = (floff & IP_OFFSET)*8;
450
451         if (ipfd_head==NULL){
452                 /* not found, this must be the first snooped fragment for this
453                  * ip packet. Create list-head.
454                  */
455                 ipfd_head=g_malloc(sizeof(ip_fragment_data));
456                 /* head/first structure in list only holds no other data than
457                  * 'datalen' then we dont have to change the head of the list
458                  * even if we want to keep it sorted
459                  */
460                 ipfd_head->next=NULL;
461                 ipfd_head->datalen=0;
462                 ipfd_head->offset=0;
463                 ipfd_head->len=0;
464                 ipfd_head->flags=0;
465                 ipfd_head->data=NULL;
466                 g_hash_table_insert(ip_fragment_table, ipfk, ipfd_head);
467         } else {
468                 /* this was not the first fragment, so we can throw ipfk 
469                  * away, only used for the first fragment, 3 lines up
470                  */
471                 g_free(ipfk);
472         }
473
474
475         /* create new ipfd describing this fragment */
476         ipfd = g_malloc(sizeof(ip_fragment_data));
477         ipfd->next = NULL;
478         ipfd->flags = 0;
479         ipfd->frame = pinfo->fd->num;
480         ipfd->offset = frag_offset;
481         ipfd->len  = pinfo->iplen;
482         ipfd->len  -= (pinfo->iphdrlen*4);
483         ipfd->data = NULL;
484
485
486
487         if( !(floff&IP_MF) ){  
488                 /* this is a tail fragment */
489                 if (ipfd_head->datalen) {
490                         /* ok we have already seen other tails for this packet
491                          * it might be a duplicate.
492                          */
493                         if (ipfd_head->datalen != (ipfd->offset + ipfd->len) ){
494                                 /* Oops, this tail indicates a different packet
495                                  * len than the previous ones. Somethings wrong
496                                  */
497                                 ipfd->flags      |= IPD_MULTIPLETAILS;
498                                 ipfd_head->flags |= IPD_MULTIPLETAILS;
499                         }
500                 } else {
501                         /* this was the first tail fragment, now we know the
502                          * length of the packet
503                          */
504                         ipfd_head->datalen = ipfd->offset + ipfd->len;
505                 }
506         }
507
508
509
510
511         /* If the packet is already defragmented, this MUST be an overlap.
512          * The entire defragmented packet is in ipfd_head->data
513          * Even if we have previously defragmented this packet, we still check
514          * check it. Someone might play overlap and TTL games.
515          */
516         if (ipfd_head->flags & IPD_DEFRAGMENTED) {
517                 ipfd->flags      |= IPD_OVERLAP;
518                 ipfd_head->flags |= IPD_OVERLAP;
519                 /* make sure its not too long */
520                 if (ipfd->offset + ipfd->len > ipfd_head->datalen) {
521                         ipfd->flags      |= IPD_TOOLONGFRAGMENT;
522                         ipfd_head->flags |= IPD_TOOLONGFRAGMENT;
523                         LINK_FRAG(ipfd_head,ipfd);
524                         return (ipfd_head);
525                 }
526                 /* make sure it doesnt conflict with previous data */
527                 if ( memcmp(ipfd_head->data+ipfd->offset,
528                         tvb_get_ptr(tvb,offset,ipfd->len),ipfd->len) ){
529                         ipfd->flags      |= IPD_OVERLAPCONFLICT;
530                         ipfd_head->flags |= IPD_OVERLAPCONFLICT;
531                         LINK_FRAG(ipfd_head,ipfd);
532                         return (ipfd_head);
533                 }
534                 /* it was just an overlap, link it and return */
535                 LINK_FRAG(ipfd_head,ipfd);
536                 return (ipfd_head);
537         }
538
539
540
541         /* If we have reached this point, the packet is not defragmented yet.
542          * Save all payload in a buffer until we can defragment.
543          */
544         ipfd->data = g_malloc(ipfd->len);
545         tvb_memcpy(tvb, ipfd->data, offset, ipfd->len);
546         LINK_FRAG(ipfd_head,ipfd);
547
548
549         if( !(ipfd_head->datalen) ){
550                 /* if we dont know the datalen, there are still missing
551                  * packets. Cheaper than the check below.
552                  */
553                 return NULL;
554         }
555
556
557         /* check if we have received the entire fragment
558          * this is easy since the list is sorted and the head is faked.
559          */
560         max = 0;
561         for (ipfd_i=ipfd_head->next;ipfd_i;ipfd_i=ipfd_i->next) {
562                 if ( ((ipfd_i->offset)<=max) && 
563                     ((ipfd_i->offset+ipfd_i->len)>max) ){
564                         max = ipfd_i->offset+ipfd_i->len;
565                 }
566         }
567
568         if (max < (ipfd_head->datalen)) {
569                 /* we have not received all packets yet */
570                 return NULL;
571         }
572
573
574         if (max > (ipfd_head->datalen)) {
575                 /* oops, too long fragment detected */
576                 ipfd->flags      |= IPD_TOOLONGFRAGMENT;
577                 ipfd_head->flags |= IPD_TOOLONGFRAGMENT;
578         }
579
580
581         /* we have received an entire packet, defragment it and
582          * free all fragments 
583          */
584         ipfd_head->data = g_malloc(max);
585
586         /* add all data fragments */
587         for (dfpos=0,ipfd_i=ipfd_head;ipfd_i;ipfd_i=ipfd_i->next) {
588                 if (ipfd_i->len) {
589                         if (ipfd_i->offset < dfpos) {
590                                 ipfd_i->flags    |= IPD_OVERLAP;
591                                 ipfd_head->flags |= IPD_OVERLAP;
592                                 if ( memcmp(ipfd_head->data+ipfd_i->offset,
593                                         ipfd_i->data,
594                                         MIN(ipfd_i->len,(dfpos-ipfd_i->offset))
595                                         ) ){
596                                         ipfd_i->flags    |= IPD_OVERLAPCONFLICT;
597                                         ipfd_head->flags |= IPD_OVERLAPCONFLICT;
598                                 }
599                         }
600                         memcpy(ipfd_head->data+ipfd_i->offset,ipfd_i->data,ipfd_i->len);
601                         g_free(ipfd_i->data);
602                         ipfd_i->data=NULL;
603
604                         dfpos=MAX(dfpos,(ipfd_i->offset+ipfd_i->len));
605                 }
606         }
607
608         /* mark this packet as defragmented.
609            allows us to skip any trailing fragments */
610         ipfd_head->flags |= IPD_DEFRAGMENTED;
611
612         return ipfd_head;
613 }
614
615
616
617 void
618 capture_ip(const u_char *pd, int offset, packet_counts *ld) {
619   if (!BYTES_ARE_IN_FRAME(offset, IPH_MIN_LEN)) {
620     ld->other++;
621     return;
622   }
623   switch (pd[offset + 9]) {
624     case IP_PROTO_SCTP:
625       ld->sctp++;
626       break;
627     case IP_PROTO_TCP:
628       ld->tcp++;
629       break;
630     case IP_PROTO_UDP:
631       ld->udp++;
632       break;
633     case IP_PROTO_ICMP:
634       ld->icmp++;
635       break;
636     case IP_PROTO_OSPF:
637       ld->ospf++;
638       break;
639     case IP_PROTO_GRE:
640       ld->gre++;
641       break;
642     case IP_PROTO_VINES:
643       ld->vines++;
644       break;
645     default:
646       ld->other++;
647   }
648 }
649
650 static void
651 dissect_ipopt_security(const ip_tcp_opt *optp, tvbuff_t *tvb, int offset,
652                         guint optlen, frame_data *fd, proto_tree *opt_tree)
653 {
654   proto_tree *field_tree = NULL;
655   proto_item *tf;
656   guint      val;
657   static const value_string secl_vals[] = {
658     {IPSEC_UNCLASSIFIED, "Unclassified"},
659     {IPSEC_CONFIDENTIAL, "Confidential"},
660     {IPSEC_EFTO,         "EFTO"        },
661     {IPSEC_MMMM,         "MMMM"        },
662     {IPSEC_RESTRICTED,   "Restricted"  },
663     {IPSEC_SECRET,       "Secret"      },
664     {IPSEC_TOPSECRET,    "Top secret"  },
665     {IPSEC_RESERVED1,    "Reserved"    },
666     {IPSEC_RESERVED2,    "Reserved"    },
667     {IPSEC_RESERVED3,    "Reserved"    },
668     {IPSEC_RESERVED4,    "Reserved"    },
669     {IPSEC_RESERVED5,    "Reserved"    },
670     {IPSEC_RESERVED6,    "Reserved"    },
671     {IPSEC_RESERVED7,    "Reserved"    },
672     {IPSEC_RESERVED8,    "Reserved"    },
673     {0,                  NULL          } };
674
675   tf = proto_tree_add_text(opt_tree, tvb, offset,      optlen, "%s:", optp->name);
676   field_tree = proto_item_add_subtree(tf, *optp->subtree_index);
677   offset += 2;
678
679   val = tvb_get_ntohs(tvb, offset);
680   proto_tree_add_text(field_tree, tvb, offset,       2,
681               "Security: %s", val_to_str(val, secl_vals, "Unknown (0x%x)"));
682   offset += 2;
683
684   val = tvb_get_ntohs(tvb, offset);
685   proto_tree_add_text(field_tree, tvb, offset,         2,
686               "Compartments: %u", val);
687   offset += 2;
688
689   proto_tree_add_text(field_tree, tvb, offset,         2,
690               "Handling restrictions: %c%c",
691               tvb_get_guint8(tvb, offset),
692               tvb_get_guint8(tvb, offset + 1));
693   offset += 2;
694
695   proto_tree_add_text(field_tree, tvb, offset,         3,
696               "Transmission control code: %c%c%c",
697               tvb_get_guint8(tvb, offset),
698               tvb_get_guint8(tvb, offset + 1),
699               tvb_get_guint8(tvb, offset + 2));
700 }
701
702 static void
703 dissect_ipopt_route(const ip_tcp_opt *optp, tvbuff_t *tvb, int offset,
704                         guint optlen, frame_data *fd, proto_tree *opt_tree)
705 {
706   proto_tree *field_tree = NULL;
707   proto_item *tf;
708   int ptr;
709   int optoffset = 0;
710   struct in_addr addr;
711
712   tf = proto_tree_add_text(opt_tree, tvb, offset,      optlen, "%s (%u bytes)",
713                                 optp->name, optlen);
714   field_tree = proto_item_add_subtree(tf, *optp->subtree_index);
715
716   optoffset += 2;       /* skip past type and length */
717   optlen -= 2;          /* subtract size of type and length */
718
719   ptr = tvb_get_guint8(tvb, offset + optoffset);
720   proto_tree_add_text(field_tree, tvb, offset + optoffset, 1,
721               "Pointer: %d%s", ptr,
722               ((ptr < 4) ? " (points before first address)" :
723                ((ptr & 3) ? " (points to middle of address)" : "")));
724   optoffset++;
725   optlen--;
726   ptr--;        /* ptr is 1-origin */
727
728   while (optlen > 0) {
729     if (optlen < 4) {
730       proto_tree_add_text(field_tree, tvb, offset,      optlen,
731         "(suboption would go past end of option)");
732       break;
733     }
734
735     /* Avoids alignment problems on many architectures. */
736     tvb_memcpy(tvb, (guint8 *)&addr, offset + optoffset, sizeof(addr));
737
738     proto_tree_add_text(field_tree, tvb, offset + optoffset, 4,
739               "%s%s",
740               ((addr.s_addr == 0) ? "-" : (char *)get_hostname(addr.s_addr)),
741               ((optoffset == ptr) ? " <- (current)" : ""));
742     optoffset += 4;
743     optlen -= 4;
744   }
745 }
746
747 static void
748 dissect_ipopt_sid(const ip_tcp_opt *optp, tvbuff_t *tvb, int offset,
749                         guint optlen, frame_data *fd, proto_tree *opt_tree)
750 {
751   proto_tree_add_text(opt_tree, tvb, offset,      optlen,
752     "%s: %u", optp->name, tvb_get_ntohs(tvb, offset + 2));
753   return;
754 }
755
756 static void
757 dissect_ipopt_timestamp(const ip_tcp_opt *optp, tvbuff_t *tvb,
758     int offset, guint optlen, frame_data *fd, proto_tree *opt_tree)
759 {
760   proto_tree *field_tree = NULL;
761   proto_item *tf;
762   int        ptr;
763   int        optoffset = 0;
764   int        flg;
765   static const value_string flag_vals[] = {
766     {IPOPT_TS_TSONLY,    "Time stamps only"                      },
767     {IPOPT_TS_TSANDADDR, "Time stamp and address"                },
768     {IPOPT_TS_PRESPEC,   "Time stamps for prespecified addresses"},
769     {0,                  NULL                                    } };
770   struct in_addr addr;
771   guint ts;
772
773   tf = proto_tree_add_text(opt_tree, tvb, offset,      optlen, "%s:", optp->name);
774   field_tree = proto_item_add_subtree(tf, *optp->subtree_index);
775
776   optoffset += 2;       /* skip past type and length */
777   optlen -= 2;          /* subtract size of type and length */
778
779   ptr = tvb_get_guint8(tvb, offset + optoffset);
780   proto_tree_add_text(field_tree, tvb, offset + optoffset, 1,
781               "Pointer: %d%s", ptr,
782               ((ptr < 5) ? " (points before first address)" :
783                (((ptr - 1) & 3) ? " (points to middle of address)" : "")));
784   optoffset++;
785   optlen--;
786   ptr--;        /* ptr is 1-origin */
787
788   flg = tvb_get_guint8(tvb, offset + optoffset);
789   proto_tree_add_text(field_tree, tvb, offset + optoffset,   1,
790         "Overflow: %u", flg >> 4);
791   flg &= 0xF;
792   proto_tree_add_text(field_tree, tvb, offset + optoffset, 1,
793         "Flag: %s", val_to_str(flg, flag_vals, "Unknown (0x%x)"));
794   optoffset++;
795   optlen--;
796
797   while (optlen > 0) {
798     if (flg == IPOPT_TS_TSANDADDR) {
799       if (optlen < 8) {
800         proto_tree_add_text(field_tree, tvb, offset + optoffset, optlen,
801           "(suboption would go past end of option)");
802         break;
803       }
804       tvb_memcpy(tvb, (char *)&addr, offset + optoffset, sizeof(addr));
805       ts = tvb_get_ntohl(tvb, offset + optoffset + 4);
806       optlen -= 8;
807       proto_tree_add_text(field_tree, tvb, offset + optoffset,      8,
808           "Address = %s, time stamp = %u",
809           ((addr.s_addr == 0) ? "-" :  (char *)get_hostname(addr.s_addr)),
810           ts);
811       optoffset += 8;
812     } else {
813       if (optlen < 4) {
814         proto_tree_add_text(field_tree, tvb, offset + optoffset, optlen,
815           "(suboption would go past end of option)");
816         break;
817       }
818       ts = tvb_get_ntohl(tvb, offset + optoffset);
819       optlen -= 4;
820       proto_tree_add_text(field_tree, tvb, offset + optoffset, 4,
821           "Time stamp = %u", ts);
822       optoffset += 4;
823     }
824   }
825 }
826
827 static void
828 dissect_ipopt_ra(const ip_tcp_opt *optp, tvbuff_t *tvb, int offset,
829                 guint optlen, frame_data *fd, proto_tree *opt_tree)
830 {
831   /* Router-Alert, as defined by RFC2113 */
832   int opt = tvb_get_ntohs(tvb, offset + 2);
833   static const value_string ra_opts[] = { 
834         {0, "Every router examines packet"},
835         {0, NULL}
836   };
837   
838   proto_tree_add_text(opt_tree, tvb, offset,      optlen,
839     "%s: %s", optp->name, val_to_str(opt, ra_opts, "Unknown (%d)"));
840   return;
841 }
842
843 static const ip_tcp_opt ipopts[] = {
844   {
845     IPOPT_END,
846     "EOL",
847     NULL,
848     NO_LENGTH,
849     0,
850     NULL,
851   },
852   {
853     IPOPT_NOOP,
854     "NOP",
855     NULL,
856     NO_LENGTH,
857     0,
858     NULL,
859   },
860   {
861     IPOPT_SEC,
862     "Security",
863     &ett_ip_option_sec,
864     FIXED_LENGTH,
865     IPOLEN_SEC,
866     dissect_ipopt_security
867   },
868   {
869     IPOPT_SSRR,
870     "Strict source route",
871     &ett_ip_option_route,
872     VARIABLE_LENGTH,
873     IPOLEN_SSRR_MIN,
874     dissect_ipopt_route
875   },
876   {
877     IPOPT_LSRR,
878     "Loose source route",
879     &ett_ip_option_route,
880     VARIABLE_LENGTH,
881     IPOLEN_LSRR_MIN,
882     dissect_ipopt_route
883   },
884   {
885     IPOPT_RR,
886     "Record route",
887     &ett_ip_option_route,
888     VARIABLE_LENGTH,
889     IPOLEN_RR_MIN,
890     dissect_ipopt_route
891   },
892   {
893     IPOPT_SID,
894     "Stream identifier",
895     NULL,
896     FIXED_LENGTH,
897     IPOLEN_SID,
898     dissect_ipopt_sid
899   },
900   {
901     IPOPT_TIMESTAMP,
902     "Time stamp",
903     &ett_ip_option_timestamp,
904     VARIABLE_LENGTH,
905     IPOLEN_TIMESTAMP_MIN,
906     dissect_ipopt_timestamp
907   },
908   {
909     IPOPT_RA,
910     "Router Alert",
911     NULL,
912     FIXED_LENGTH,
913     IPOLEN_RA,
914     dissect_ipopt_ra
915   },
916 };
917
918 #define N_IP_OPTS       (sizeof ipopts / sizeof ipopts[0])
919
920 /* Dissect the IP or TCP options in a packet. */
921 void
922 dissect_ip_tcp_options(tvbuff_t *tvb, int offset, guint length,
923                         const ip_tcp_opt *opttab, int nopts, int eol,
924                         frame_data *fd, proto_tree *opt_tree)
925 {
926   u_char            opt;
927   const ip_tcp_opt *optp;
928   opt_len_type      len_type;
929   int               optlen;
930   char             *name;
931   char              name_str[7+1+1+2+2+1+1];    /* "Unknown (0x%02x)" */
932   void            (*dissect)(const struct ip_tcp_opt *, tvbuff_t *,
933                                 int, guint, frame_data *, proto_tree *);
934   guint             len;
935
936   while (length > 0) {
937     opt = tvb_get_guint8(tvb, offset);
938     for (optp = &opttab[0]; optp < &opttab[nopts]; optp++) {
939       if (optp->optcode == opt)
940         break;
941     }
942     if (optp == &opttab[nopts]) {
943       /* We assume that the only NO_LENGTH options are EOL and NOP options,
944          so that we can treat unknown options as VARIABLE_LENGTH with a
945          minimum of 2, and at least be able to move on to the next option
946          by using the length in the option. */
947       optp = NULL;      /* indicate that we don't know this option */
948       len_type = VARIABLE_LENGTH;
949       optlen = 2;
950       snprintf(name_str, sizeof name_str, "Unknown (0x%02x)", opt);
951       name = name_str;
952       dissect = NULL;
953     } else {
954       len_type = optp->len_type;
955       optlen = optp->optlen;
956       name = optp->name;
957       dissect = optp->dissect;
958     }
959     --length;      /* account for type byte */
960     if (len_type != NO_LENGTH) {
961       /* Option has a length. Is it in the packet? */
962       if (length == 0) {
963         /* Bogus - packet must at least include option code byte and
964            length byte! */
965         proto_tree_add_text(opt_tree, tvb, offset,      1,
966               "%s (length byte past end of options)", name);
967         return;
968       }
969       len = tvb_get_guint8(tvb, offset + 1);  /* total including type, len */
970       --length;    /* account for length byte */
971       if (len < 2) {
972         /* Bogus - option length is too short to include option code and
973            option length. */
974         proto_tree_add_text(opt_tree, tvb, offset,      2,
975               "%s (with too-short option length = %u byte%s)", name,
976               len, plurality(len, "", "s"));
977         return;
978       } else if (len - 2 > length) {
979         /* Bogus - option goes past the end of the header. */
980         proto_tree_add_text(opt_tree, tvb, offset,      length,
981               "%s (option length = %u byte%s says option goes past end of options)",
982               name, len, plurality(len, "", "s"));
983         return;
984       } else if (len_type == FIXED_LENGTH && len != optlen) {
985         /* Bogus - option length isn't what it's supposed to be for this
986            option. */
987         proto_tree_add_text(opt_tree, tvb, offset,      len,
988               "%s (with option length = %u byte%s; should be %u)", name,
989               len, plurality(len, "", "s"), optlen);
990         return;
991       } else if (len_type == VARIABLE_LENGTH && len < optlen) {
992         /* Bogus - option length is less than what it's supposed to be for
993            this option. */
994         proto_tree_add_text(opt_tree, tvb, offset,      len,
995               "%s (with option length = %u byte%s; should be >= %u)", name,
996               len, plurality(len, "", "s"), optlen);
997         return;
998       } else {
999         if (optp == NULL) {
1000           proto_tree_add_text(opt_tree, tvb, offset,    len, "%s (%u byte%s)",
1001                                 name, len, plurality(len, "", "s"));
1002         } else {
1003           if (dissect != NULL) {
1004             /* Option has a dissector. */
1005             (*dissect)(optp, tvb, offset,          len, fd, opt_tree);
1006           } else {
1007             /* Option has no data, hence no dissector. */
1008             proto_tree_add_text(opt_tree, tvb, offset,  len, "%s", name);
1009           }
1010         }
1011         len -= 2;       /* subtract size of type and length */
1012         offset += 2 + len;
1013       }
1014       length -= len;
1015     } else {
1016       proto_tree_add_text(opt_tree, tvb, offset,      1, "%s", name);
1017       offset += 1;
1018     }
1019     if (opt == eol)
1020       break;
1021   }
1022 }
1023
1024 static const value_string dscp_vals[] = {
1025                   { IPDSFIELD_DSCP_DEFAULT, "Default"               },
1026                   { IPDSFIELD_DSCP_CS1,     "Class Selector 1"      },
1027                   { IPDSFIELD_DSCP_CS2,     "Class Selector 2"      },
1028                   { IPDSFIELD_DSCP_CS3,     "Class Selector 3"      },
1029                   { IPDSFIELD_DSCP_CS4,     "Class Selector 4"      },
1030                   { IPDSFIELD_DSCP_CS5,     "Class Selector 5"      },
1031                   { IPDSFIELD_DSCP_CS6,     "Class Selector 6"      },
1032                   { IPDSFIELD_DSCP_CS7,     "Class Selector 7"      },
1033                   { IPDSFIELD_DSCP_AF11,    "Assured Forwarding 11" },
1034                   { IPDSFIELD_DSCP_AF12,    "Assured Forwarding 12" },
1035                   { IPDSFIELD_DSCP_AF13,    "Assured Forwarding 13" },
1036                   { IPDSFIELD_DSCP_AF21,    "Assured Forwarding 21" },
1037                   { IPDSFIELD_DSCP_AF22,    "Assured Forwarding 22" },
1038                   { IPDSFIELD_DSCP_AF23,    "Assured Forwarding 23" },
1039                   { IPDSFIELD_DSCP_AF31,    "Assured Forwarding 31" },
1040                   { IPDSFIELD_DSCP_AF32,    "Assured Forwarding 32" },
1041                   { IPDSFIELD_DSCP_AF33,    "Assured Forwarding 33" },
1042                   { IPDSFIELD_DSCP_AF41,    "Assured Forwarding 41" },
1043                   { IPDSFIELD_DSCP_AF42,    "Assured Forwarding 42" },
1044                   { IPDSFIELD_DSCP_AF43,    "Assured Forwarding 43" },
1045                   { IPDSFIELD_DSCP_EF,      "Expedited Forwarding"  },
1046                   { 0,                      NULL                    } };
1047
1048 static const value_string precedence_vals[] = {
1049                   { IPTOS_PREC_ROUTINE,         "routine"              },
1050                   { IPTOS_PREC_PRIORITY,        "priority"             },
1051                   { IPTOS_PREC_IMMEDIATE,       "immediate"            },
1052                   { IPTOS_PREC_FLASH,           "flash"                },
1053                   { IPTOS_PREC_FLASHOVERRIDE,   "flash override"       },
1054                   { IPTOS_PREC_CRITIC_ECP,      "CRITIC/ECP"           },
1055                   { IPTOS_PREC_INTERNETCONTROL, "internetwork control" },
1056                   { IPTOS_PREC_NETCONTROL,      "network control"      },
1057                   { 0,                          NULL                   } };
1058
1059 static const value_string iptos_vals[] = {
1060         { IPTOS_NONE,           "None" },
1061         { IPTOS_LOWCOST,        "Minimize cost" },
1062         { IPTOS_RELIABILITY,    "Maximize reliability" },
1063         { IPTOS_THROUGHPUT,     "Maximize throughput" },
1064         { IPTOS_LOWDELAY,       "Minimize delay" },
1065         { IPTOS_SECURITY,       "Maximize security" },
1066         { 0,                    NULL }
1067 };
1068
1069 static const true_false_string tos_set_low = {
1070   "Low",
1071   "Normal"
1072 };
1073
1074 static const true_false_string tos_set_high = {
1075   "High",
1076   "Normal"
1077 };
1078
1079 static const true_false_string flags_set_truth = {
1080   "Set",
1081   "Not set"
1082 };
1083
1084 static guint16 ip_checksum(const guint8 *ptr, int len)
1085 {
1086         vec_t cksum_vec[1];
1087
1088         cksum_vec[0].ptr = ptr;
1089         cksum_vec[0].len = len;
1090         return in_cksum(&cksum_vec[0], 1);
1091 }
1092
1093 static void
1094 dissect_ip(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1095 {
1096   e_ip       iph;
1097   proto_tree *ip_tree = NULL, *field_tree;
1098   proto_item *ti, *tf;
1099   int        offset = 0;
1100   guint      hlen, optlen, len, payload_len, reported_payload_len, padding;
1101   guint16    flags;
1102   guint8     nxt;
1103   guint16    ipsum;
1104   ip_fragment_data *ipfd_head;
1105   tvbuff_t   *next_tvb;
1106   packet_info save_pi;
1107   gboolean must_restore_pi = FALSE;
1108   gboolean update_col_info = TRUE;
1109
1110   if (check_col(pinfo->fd, COL_PROTOCOL))
1111     col_set_str(pinfo->fd, COL_PROTOCOL, "IP");
1112   if (check_col(pinfo->fd, COL_INFO))
1113     col_clear(pinfo->fd, COL_INFO);
1114
1115   /* Avoids alignment problems on many architectures. */
1116   tvb_memcpy(tvb, (guint8 *)&iph, offset, sizeof(e_ip));
1117   iph.ip_len = ntohs(iph.ip_len);
1118   iph.ip_id  = ntohs(iph.ip_id);
1119   iph.ip_off = ntohs(iph.ip_off);
1120   iph.ip_sum = ntohs(iph.ip_sum);
1121
1122   /* Length of payload handed to us. */
1123   reported_payload_len = tvb_reported_length(tvb);
1124   payload_len = tvb_length(tvb);
1125
1126   /* Length of IP datagram. */
1127   len = iph.ip_len;
1128
1129   if (len < reported_payload_len) {
1130     /* Adjust the length of this tvbuff to include only the IP datagram.
1131        Our caller may use that to determine how much of its packet
1132        was padding. */
1133     tvb_set_reported_length(tvb, len);
1134
1135     /* Shrink the total payload by the amount of padding. */
1136     padding = reported_payload_len - len;
1137     if (pinfo->len >= padding)
1138       pinfo->len -= padding;
1139
1140     /* Shrink the captured payload by the amount of padding in the
1141        captured payload (which may be less than the amount of padding,
1142        as the padding may not have been captured). */
1143     if (len < payload_len) {
1144       padding = payload_len - len;
1145       if (pinfo->captured_len >= padding)
1146         pinfo->captured_len -= padding;
1147     }
1148   }
1149
1150   hlen = lo_nibble(iph.ip_v_hl) * 4;    /* IP header length, in bytes */
1151  
1152   if (tree) {
1153     if (ip_summary_in_tree && hlen >= IPH_MIN_LEN) {
1154       ti = proto_tree_add_protocol_format(tree, proto_ip, tvb, offset, hlen,
1155                 "Internet Protocol, Src Addr: %s (%s), Dst Addr: %s (%s)",
1156                 get_hostname(iph.ip_src), ip_to_str((guint8 *) &iph.ip_src),
1157                 get_hostname(iph.ip_dst), ip_to_str((guint8 *) &iph.ip_dst));
1158     } else {
1159       ti = proto_tree_add_item(tree, proto_ip, tvb, offset, hlen, FALSE);
1160     }
1161     ip_tree = proto_item_add_subtree(ti, ett_ip);
1162   }
1163
1164   if (hlen < IPH_MIN_LEN) {
1165     if (check_col(pinfo->fd, COL_INFO))
1166       col_add_fstr(pinfo->fd, COL_INFO, "Bogus IP header length (%u, must be at least %u)",
1167        hlen, IPH_MIN_LEN);
1168     if (tree) {
1169       proto_tree_add_uint_format(ip_tree, hf_ip_hdr_len, tvb, offset, 1, hlen,
1170        "Header length: %u bytes (bogus, must be at least %u)", hlen,
1171        IPH_MIN_LEN);
1172     }
1173     return;
1174   }
1175
1176   /*
1177    * Compute the checksum of the IP header.
1178    */
1179   ipsum = ip_checksum(tvb_get_ptr(tvb, offset, hlen), hlen);
1180
1181   if (tree) {
1182     proto_tree_add_uint(ip_tree, hf_ip_version, tvb, offset, 1, hi_nibble(iph.ip_v_hl));
1183     proto_tree_add_uint_format(ip_tree, hf_ip_hdr_len, tvb, offset, 1, hlen,
1184         "Header length: %u bytes", hlen);
1185
1186     if (g_ip_dscp_actif) {
1187       tf = proto_tree_add_uint_format(ip_tree, hf_ip_dsfield, tvb, offset + 1, 1, iph.ip_tos,
1188            "Differentiated Services Field: 0x%02x (DSCP 0x%02x: %s; ECN: 0x%02x)", iph.ip_tos,
1189            IPDSFIELD_DSCP(iph.ip_tos), val_to_str(IPDSFIELD_DSCP(iph.ip_tos), dscp_vals,
1190            "Unknown DSCP"),IPDSFIELD_ECN(iph.ip_tos));
1191
1192       field_tree = proto_item_add_subtree(tf, ett_ip_dsfield);
1193       proto_tree_add_uint(field_tree, hf_ip_dsfield_dscp, tvb, offset + 1, 1, iph.ip_tos);
1194       proto_tree_add_uint(field_tree, hf_ip_dsfield_ect, tvb, offset + 1, 1, iph.ip_tos);
1195       proto_tree_add_uint(field_tree, hf_ip_dsfield_ce, tvb, offset + 1, 1, iph.ip_tos);
1196     } else {
1197       tf = proto_tree_add_uint_format(ip_tree, hf_ip_tos, tvb, offset + 1, 1, iph.ip_tos,
1198           "Type of service: 0x%02x (%s)", iph.ip_tos,
1199           val_to_str( IPTOS_TOS(iph.ip_tos), iptos_vals, "Unknown") );
1200
1201       field_tree = proto_item_add_subtree(tf, ett_ip_tos);
1202       proto_tree_add_uint(field_tree, hf_ip_tos_precedence, tvb, offset + 1, 1, iph.ip_tos);
1203       proto_tree_add_boolean(field_tree, hf_ip_tos_delay, tvb, offset + 1, 1, iph.ip_tos);
1204       proto_tree_add_boolean(field_tree, hf_ip_tos_throughput, tvb, offset + 1, 1, iph.ip_tos);
1205       proto_tree_add_boolean(field_tree, hf_ip_tos_reliability, tvb, offset + 1, 1, iph.ip_tos);
1206       proto_tree_add_boolean(field_tree, hf_ip_tos_cost, tvb, offset + 1, 1, iph.ip_tos);
1207     }
1208     proto_tree_add_uint(ip_tree, hf_ip_len, tvb, offset +  2, 2, iph.ip_len);
1209     proto_tree_add_uint(ip_tree, hf_ip_id, tvb, offset +  4, 2, iph.ip_id);
1210
1211     flags = (iph.ip_off & (IP_DF|IP_MF)) >> 12;
1212     tf = proto_tree_add_uint(ip_tree, hf_ip_flags, tvb, offset +  6, 1, flags);
1213     field_tree = proto_item_add_subtree(tf, ett_ip_off);
1214     proto_tree_add_boolean(field_tree, hf_ip_flags_df, tvb, offset + 6, 1, flags),
1215     proto_tree_add_boolean(field_tree, hf_ip_flags_mf, tvb, offset + 6, 1, flags),
1216
1217     proto_tree_add_uint(ip_tree, hf_ip_frag_offset, tvb, offset +  6, 2,
1218       (iph.ip_off & IP_OFFSET)*8);
1219
1220     proto_tree_add_uint(ip_tree, hf_ip_ttl, tvb, offset +  8, 1, iph.ip_ttl);
1221     proto_tree_add_uint_format(ip_tree, hf_ip_proto, tvb, offset +  9, 1, iph.ip_p,
1222         "Protocol: %s (0x%02x)", ipprotostr(iph.ip_p), iph.ip_p);
1223
1224     if (ipsum == 0) {
1225         proto_tree_add_uint_format(ip_tree, hf_ip_checksum, tvb, offset + 10, 2, iph.ip_sum,
1226               "Header checksum: 0x%04x (correct)", iph.ip_sum);
1227     }
1228     else {
1229         proto_tree_add_item_hidden(ip_tree, hf_ip_checksum_bad, tvb, offset + 10, 2, TRUE);
1230         proto_tree_add_uint_format(ip_tree, hf_ip_checksum, tvb, offset + 10, 2, iph.ip_sum,
1231           "Header checksum: 0x%04x (incorrect, should be 0x%04x)", iph.ip_sum,
1232           in_cksum_shouldbe(iph.ip_sum, ipsum));
1233     }
1234
1235     proto_tree_add_ipv4(ip_tree, hf_ip_src, tvb, offset + 12, 4, iph.ip_src);
1236     proto_tree_add_ipv4(ip_tree, hf_ip_dst, tvb, offset + 16, 4, iph.ip_dst);
1237     proto_tree_add_ipv4_hidden(ip_tree, hf_ip_addr, tvb, offset + 12, 4, iph.ip_src);
1238     proto_tree_add_ipv4_hidden(ip_tree, hf_ip_addr, tvb, offset + 16, 4, iph.ip_dst);
1239
1240     /* Decode IP options, if any. */
1241     if (hlen > sizeof (e_ip)) {
1242       /* There's more than just the fixed-length header.  Decode the
1243          options. */
1244       optlen = hlen - sizeof (e_ip);    /* length of options, in bytes */
1245       tf = proto_tree_add_text(ip_tree, tvb, offset +  20, optlen,
1246         "Options: (%u bytes)", optlen);
1247       field_tree = proto_item_add_subtree(tf, ett_ip_options);
1248       dissect_ip_tcp_options(tvb, offset + 20, optlen,
1249          ipopts, N_IP_OPTS, IPOPT_END, pinfo->fd, field_tree);
1250     }
1251   }
1252
1253   pinfo->ipproto = iph.ip_p;
1254
1255   pinfo->iplen = iph.ip_len;
1256
1257   pinfo->iphdrlen = lo_nibble(iph.ip_v_hl);
1258
1259   SET_ADDRESS(&pinfo->net_src, AT_IPv4, 4, tvb_get_ptr(tvb, offset + IPH_SRC, 4));
1260   SET_ADDRESS(&pinfo->src, AT_IPv4, 4, tvb_get_ptr(tvb, offset + IPH_SRC, 4));
1261   SET_ADDRESS(&pinfo->net_dst, AT_IPv4, 4, tvb_get_ptr(tvb, offset + IPH_DST, 4));
1262   SET_ADDRESS(&pinfo->dst, AT_IPv4, 4, tvb_get_ptr(tvb, offset + IPH_DST, 4));
1263
1264   /* Skip over header + options */
1265   offset += hlen;
1266   nxt = iph.ip_p;       /* XXX - what if this isn't the same for all fragments? */
1267
1268   /* If ip_defragment is on and this is a fragment, then just add the fragment
1269    * to the hashtable.
1270    */
1271   if (ip_defragment && (iph.ip_off & (IP_MF|IP_OFFSET))) {
1272     /* We're reassembling, and this is part of a fragmented datagram.
1273        Add the fragment to the hash table if the checksum is ok
1274        and the frame isn't truncated. */
1275     if ((ipsum==0) && (tvb_reported_length(tvb) <= tvb_length(tvb))) {
1276       ipfd_head = ip_fragment_add(tvb, offset, pinfo, iph.ip_id, iph.ip_off);
1277     } else {
1278       ipfd_head=NULL;
1279     }
1280
1281     if (ipfd_head != NULL) {
1282       ip_fragment_data *ipfd;
1283       proto_tree *ft=NULL;
1284       proto_item *fi=NULL;
1285
1286       /* OK, we have the complete reassembled payload. */
1287       /* show all fragments */
1288       fi = proto_tree_add_item(ip_tree, hf_ip_fragments, 
1289                 tvb, 0, 0, FALSE);
1290       ft = proto_item_add_subtree(fi, ett_ip_fragments);
1291       for (ipfd=ipfd_head->next; ipfd; ipfd=ipfd->next){
1292         if (ipfd->flags & (IPD_OVERLAP|IPD_OVERLAPCONFLICT
1293                           |IPD_MULTIPLETAILS|IPD_TOOLONGFRAGMENT) ) {
1294           /* this fragment has some flags set, create a subtree 
1295            * for it and display the flags.
1296            */
1297           proto_tree *fet=NULL;
1298           proto_item *fei=NULL;
1299           int hf;
1300
1301           if (ipfd->flags & (IPD_OVERLAPCONFLICT
1302                       |IPD_MULTIPLETAILS|IPD_TOOLONGFRAGMENT) ) {
1303             hf = hf_ip_fragment_error;
1304           } else {
1305             hf = hf_ip_fragment;
1306           }
1307           fei = proto_tree_add_none_format(ft, hf, 
1308                    tvb, 0, 0,
1309                    "Frame:%d payload:%d-%d",
1310                    ipfd->frame,
1311                    ipfd->offset,
1312                    ipfd->offset+ipfd->len-1
1313           );
1314           fet = proto_item_add_subtree(fei, ett_ip_fragment);
1315           if (ipfd->flags&IPD_OVERLAP) {
1316             proto_tree_add_boolean(fet, 
1317                  hf_ip_fragment_overlap, tvb, 0, 0, 
1318                  TRUE);
1319           }
1320           if (ipfd->flags&IPD_OVERLAPCONFLICT) {
1321             proto_tree_add_boolean(fet, 
1322                  hf_ip_fragment_overlap_conflict, tvb, 0, 0, 
1323                  TRUE);
1324           }
1325           if (ipfd->flags&IPD_MULTIPLETAILS) {
1326             proto_tree_add_boolean(fet, 
1327                  hf_ip_fragment_multiple_tails, tvb, 0, 0, 
1328                  TRUE);
1329           }
1330           if (ipfd->flags&IPD_TOOLONGFRAGMENT) {
1331             proto_tree_add_boolean(fet, 
1332                  hf_ip_fragment_too_long_fragment, tvb, 0, 0, 
1333                  TRUE);
1334           }
1335         } else {
1336           /* nothing of interest for this fragment */
1337           proto_tree_add_none_format(ft, hf_ip_fragment, 
1338                    tvb, 0, 0,
1339                    "Frame:%d payload:%d-%d",
1340                    ipfd->frame,
1341                    ipfd->offset,
1342                    ipfd->offset+ipfd->len-1
1343           );
1344         }
1345       }
1346       if (ipfd_head->flags & (IPD_OVERLAPCONFLICT
1347                         |IPD_MULTIPLETAILS|IPD_TOOLONGFRAGMENT) ) {
1348         if (check_col(pinfo->fd, COL_INFO)) {
1349           col_set_str(pinfo->fd, COL_INFO, "[Illegal fragments]");
1350           update_col_info = FALSE;
1351         }
1352       }
1353
1354       /* Allocate a new tvbuff, referring to the reassembled payload. */
1355       next_tvb = tvb_new_real_data(ipfd_head->data, ipfd_head->datalen,
1356         ipfd_head->datalen, "Reassembled");
1357
1358       /* Add the tvbuff to the list of tvbuffs to which the tvbuff we
1359          were handed refers, so it'll get cleaned up when that tvbuff
1360          is cleaned up. */
1361       tvb_set_child_real_data_tvbuff(tvb, next_tvb);
1362
1363       /* Add the defragmented data to the data source list. */
1364       pinfo->fd->data_src = g_slist_append(pinfo->fd->data_src, next_tvb);
1365
1366       /* It's not fragmented. */
1367       pinfo->fragmented = FALSE;
1368
1369       /* Save the current value of "pi", and adjust certain fields to
1370          reflect the new tvbuff. */
1371       save_pi = pi;
1372       pi.compat_top_tvb = next_tvb;
1373       pi.len = tvb_reported_length(next_tvb);
1374       pi.captured_len = tvb_length(next_tvb);
1375       must_restore_pi = TRUE;
1376     } else {
1377       /* We don't have the complete reassembled payload. */
1378       next_tvb = NULL;
1379     }
1380   } else {
1381     /* If this is the first fragment, dissect its contents, otherwise
1382        just show it as a fragment.
1383
1384        XXX - if we eventually don't save the reassembled contents of all
1385        fragmented datagrams, we may want to always reassemble. */
1386     if (iph.ip_off & IP_OFFSET) {
1387       /* Not the first fragment - don't dissect it. */
1388       next_tvb = NULL;
1389     } else {
1390       /* First fragment, or not fragmented.  Dissect what we have here. */
1391
1392       /* Get a tvbuff for the payload. */
1393       next_tvb = tvb_new_subset(tvb, offset, -1, -1);
1394
1395       /*
1396        * If this is the first fragment, but not the only fragment,
1397        * tell the next protocol that.
1398        */
1399       if (iph.ip_off & IP_MF)
1400         pinfo->fragmented = TRUE;
1401       else
1402         pinfo->fragmented = FALSE;
1403     }
1404   }
1405
1406   if (next_tvb == NULL) {
1407     /* Just show this as a fragment. */
1408     if (check_col(pinfo->fd, COL_INFO))
1409       col_add_fstr(pinfo->fd, COL_INFO, "Fragmented IP protocol (proto=%s 0x%02x, off=%u)",
1410         ipprotostr(iph.ip_p), iph.ip_p, (iph.ip_off & IP_OFFSET) * 8);
1411     dissect_data(tvb, offset, pinfo, tree);
1412
1413     /* As we haven't reassembled anything, we haven't changed "pi", so
1414        we don't have to restore it. */
1415     return;
1416   }
1417
1418   /* Hand off to the next protocol.
1419
1420      XXX - setting the columns only after trying various dissectors means
1421      that if one of those dissectors throws an exception, the frame won't
1422      even be labelled as an IP frame; ideally, if a frame being dissected
1423      throws an exception, it'll be labelled as a mangled frame of the
1424      type in question. */
1425   if (!dissector_try_port(ip_dissector_table, nxt, next_tvb, pinfo, tree)) {
1426     /* Unknown protocol */
1427     if (update_col_info) {
1428       if (check_col(pinfo->fd, COL_INFO))
1429         col_add_fstr(pinfo->fd, COL_INFO, "%s (0x%02x)", ipprotostr(iph.ip_p), iph.ip_p);
1430     }
1431     dissect_data(next_tvb, 0, pinfo, tree);
1432   }
1433
1434   if (must_restore_pi)
1435     pi = save_pi;
1436 }
1437
1438
1439 static const gchar *unreach_str[] = {"Network unreachable",
1440                                      "Host unreachable",
1441                                      "Protocol unreachable",
1442                                      "Port unreachable",
1443                                      "Fragmentation needed",
1444                                      "Source route failed",
1445                                      "Destination network unknown",
1446                                      "Destination host unknown",
1447                                      "Source host isolated",
1448                                      "Network administratively prohibited",
1449                                      "Host administratively prohibited",
1450                                      "Network unreachable for TOS",
1451                                      "Host unreachable for TOS",
1452                                      "Communication administratively filtered",
1453                                      "Host precedence violation",
1454                                      "Precedence cutoff in effect"};
1455                                      
1456 #define N_UNREACH       (sizeof unreach_str / sizeof unreach_str[0])
1457
1458 static const gchar *redir_str[] = {"Redirect for network",
1459                                    "Redirect for host",
1460                                    "Redirect for TOS and network",
1461                                    "Redirect for TOS and host"};
1462
1463 #define N_REDIRECT      (sizeof redir_str / sizeof redir_str[0])
1464
1465 static const gchar *ttl_str[] = {"TTL equals 0 during transit",
1466                                  "TTL equals 0 during reassembly"};
1467                                  
1468 #define N_TIMXCEED      (sizeof ttl_str / sizeof ttl_str[0])
1469
1470 static const gchar *par_str[] = {"IP header bad", "Required option missing"};
1471
1472 #define N_PARAMPROB     (sizeof par_str / sizeof par_str[0])
1473
1474 static void
1475 dissect_icmp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1476 {
1477   proto_tree *icmp_tree;
1478   proto_item *ti;
1479   guint8     icmp_type;
1480   guint8     icmp_code;
1481   guint      length, reported_length;
1482   guint16    cksum, computed_cksum;
1483   gchar      type_str[64], code_str[64] = "";
1484   guint8     num_addrs = 0;
1485   guint8     addr_entry_size = 0;
1486   int        i;
1487
1488   if (check_col(pinfo->fd, COL_PROTOCOL))
1489     col_set_str(pinfo->fd, COL_PROTOCOL, "ICMP");
1490   if (check_col(pinfo->fd, COL_INFO))
1491     col_clear(pinfo->fd, COL_INFO);
1492
1493   /* To do: check for runts, errs, etc. */
1494   icmp_type = tvb_get_guint8(tvb, 0);
1495   icmp_code = tvb_get_guint8(tvb, 1);
1496   cksum = tvb_get_ntohs(tvb, 2);
1497
1498   switch (icmp_type) {
1499     case ICMP_ECHOREPLY:
1500       strcpy(type_str, "Echo (ping) reply");
1501       break;
1502     case ICMP_UNREACH:
1503       strcpy(type_str, "Destination unreachable");
1504       if (icmp_code < N_UNREACH) {
1505         sprintf(code_str, "(%s)", unreach_str[icmp_code]);
1506       } else {
1507         strcpy(code_str, "(Unknown - error?)");
1508       }
1509       break;
1510     case ICMP_SOURCEQUENCH:
1511       strcpy(type_str, "Source quench (flow control)");
1512       break;
1513     case ICMP_REDIRECT:
1514       strcpy(type_str, "Redirect");
1515       if (icmp_code < N_REDIRECT) {
1516         sprintf(code_str, "(%s)", redir_str[icmp_code]);
1517       } else {
1518         strcpy(code_str, "(Unknown - error?)");
1519       }
1520       break;
1521     case ICMP_ECHO:
1522       strcpy(type_str, "Echo (ping) request");
1523       break;
1524     case ICMP_RTRADVERT:
1525       strcpy(type_str, "Router advertisement");
1526       break;
1527     case ICMP_RTRSOLICIT:
1528       strcpy(type_str, "Router solicitation");
1529       break;
1530     case ICMP_TIMXCEED:
1531       strcpy(type_str, "Time-to-live exceeded");
1532       if (icmp_code < N_TIMXCEED) {
1533         sprintf(code_str, "(%s)", ttl_str[icmp_code]);
1534       } else {
1535         strcpy(code_str, "(Unknown - error?)");
1536       }
1537       break;
1538     case ICMP_PARAMPROB:
1539       strcpy(type_str, "Parameter problem");
1540       if (icmp_code < N_PARAMPROB) {
1541         sprintf(code_str, "(%s)", par_str[icmp_code]);
1542       } else {
1543         strcpy(code_str, "(Unknown - error?)");
1544       }
1545       break;
1546     case ICMP_TSTAMP:
1547       strcpy(type_str, "Timestamp request");
1548       break;
1549     case ICMP_TSTAMPREPLY:
1550       strcpy(type_str, "Timestamp reply");
1551       break;
1552     case ICMP_IREQ:
1553       strcpy(type_str, "Information request");
1554       break;
1555     case ICMP_IREQREPLY:
1556       strcpy(type_str, "Information reply");
1557       break;
1558     case ICMP_MASKREQ:
1559       strcpy(type_str, "Address mask request");
1560       break;
1561     case ICMP_MASKREPLY:
1562       strcpy(type_str, "Address mask reply");
1563       break;
1564     default:
1565       strcpy(type_str, "Unknown ICMP (obsolete or malformed?)");
1566   }
1567
1568   if (check_col(pinfo->fd, COL_INFO))
1569     col_add_str(pinfo->fd, COL_INFO, type_str);
1570
1571   if (tree) {
1572     length = tvb_length(tvb);
1573     reported_length = tvb_reported_length(tvb);
1574     ti = proto_tree_add_item(tree, proto_icmp, tvb, 0, length, FALSE);
1575     icmp_tree = proto_item_add_subtree(ti, ett_icmp);
1576     proto_tree_add_uint_format(icmp_tree, hf_icmp_type, tvb, 0, 1, 
1577                                icmp_type,
1578                                "Type: %u (%s)",
1579                                icmp_type, type_str);
1580     proto_tree_add_uint_format(icmp_tree, hf_icmp_code, tvb, 1, 1, 
1581                                icmp_code,
1582                                "Code: %u %s",
1583                                icmp_code, code_str);
1584
1585     if (!pinfo->fragmented && length >= reported_length) {
1586       /* The packet isn't part of a fragmented datagram and isn't
1587          truncated, so we can checksum it. */
1588
1589       computed_cksum = ip_checksum(tvb_get_ptr(tvb, 0, reported_length),
1590                                      reported_length);
1591       if (computed_cksum == 0) {
1592         proto_tree_add_uint_format(icmp_tree, hf_icmp_checksum, tvb, 2, 2,
1593                           cksum,
1594                           "Checksum: 0x%04x (correct)", cksum);
1595       } else {
1596         proto_tree_add_item_hidden(icmp_tree, hf_icmp_checksum_bad,
1597                           tvb, 2, 2, TRUE);
1598         proto_tree_add_uint_format(icmp_tree, hf_icmp_checksum, tvb, 2, 2,
1599                   cksum,
1600                   "Checksum: 0x%04x (incorrect, should be 0x%04x)",
1601                   cksum, in_cksum_shouldbe(cksum, computed_cksum));
1602       }
1603     } else {
1604       proto_tree_add_uint(icmp_tree, hf_icmp_checksum, tvb, 2, 2, cksum);
1605     }
1606
1607     /* Decode the second 4 bytes of the packet. */
1608     switch (icmp_type) {
1609       case ICMP_ECHOREPLY:
1610       case ICMP_ECHO:
1611       case ICMP_TSTAMP:
1612       case ICMP_TSTAMPREPLY:
1613       case ICMP_IREQ:
1614       case ICMP_IREQREPLY:
1615       case ICMP_MASKREQ:
1616       case ICMP_MASKREPLY:
1617         proto_tree_add_text(icmp_tree, tvb, 4, 2, "Identifier: 0x%04x",
1618           tvb_get_ntohs(tvb, 4));
1619         proto_tree_add_text(icmp_tree, tvb, 6, 2, "Sequence number: %02x:%02x",
1620           tvb_get_guint8(tvb, 6), tvb_get_guint8(tvb, 7));
1621         break;
1622
1623        case ICMP_UNREACH:
1624          switch (icmp_code) {
1625            case ICMP_FRAG_NEEDED:
1626                  proto_tree_add_text(icmp_tree, tvb, 6, 2, "MTU of next hop: %u",
1627                    tvb_get_ntohs(tvb, 6));
1628                  break;
1629            }
1630          break;
1631
1632       case ICMP_RTRADVERT:
1633         num_addrs = tvb_get_guint8(tvb, 4);
1634         proto_tree_add_text(icmp_tree, tvb, 4, 1, "Number of addresses: %u",
1635           num_addrs);
1636         addr_entry_size = tvb_get_guint8(tvb, 5);
1637         proto_tree_add_text(icmp_tree, tvb, 5, 1, "Address entry size: %u",
1638           addr_entry_size);
1639         proto_tree_add_text(icmp_tree, tvb, 6, 2, "Lifetime: %s",
1640           time_secs_to_str(tvb_get_ntohs(tvb, 6)));
1641         break;
1642
1643       case ICMP_PARAMPROB:
1644         proto_tree_add_text(icmp_tree, tvb, 4, 1, "Pointer: %u",
1645           tvb_get_guint8(tvb, 4));
1646         break;
1647
1648       case ICMP_REDIRECT:
1649         proto_tree_add_text(icmp_tree, tvb, 4, 4, "Gateway address: %s",
1650           ip_to_str(tvb_get_ptr(tvb, 4, 4)));
1651         break;
1652     }
1653
1654     /* Decode the additional information in the packet.  */
1655     switch (icmp_type) {
1656       case ICMP_UNREACH:
1657       case ICMP_TIMXCEED:
1658       case ICMP_PARAMPROB:
1659       case ICMP_SOURCEQUENCH:
1660       case ICMP_REDIRECT:
1661         /* Decode the IP header and first 64 bits of data from the
1662            original datagram.
1663
1664            XXX - for now, just display it as data; not all dissection
1665            routines can handle a short packet without exploding. */
1666         dissect_data(tvb, 8, pinfo, icmp_tree);
1667         break;
1668
1669       case ICMP_ECHOREPLY:
1670       case ICMP_ECHO:
1671         dissect_data(tvb, 8, pinfo, icmp_tree);
1672         break;
1673
1674       case ICMP_RTRADVERT:
1675         if (addr_entry_size == 2) {
1676           for (i = 0; i < num_addrs; i++) {
1677             proto_tree_add_text(icmp_tree, tvb, 8 + (i*8), 4,
1678               "Router address: %s",
1679               ip_to_str(tvb_get_ptr(tvb, 8 + (i*8), 4)));
1680             proto_tree_add_text(icmp_tree, tvb, 12 + (i*8), 4,
1681               "Preference level: %u", tvb_get_ntohl(tvb, 12 + (i*8)));
1682           }
1683         } else
1684           dissect_data(tvb, 8, pinfo, icmp_tree);
1685         break;
1686
1687       case ICMP_TSTAMP:
1688       case ICMP_TSTAMPREPLY:
1689         proto_tree_add_text(icmp_tree, tvb, 8, 4, "Originate timestamp: %u",
1690           tvb_get_ntohl(tvb, 8));
1691         proto_tree_add_text(icmp_tree, tvb, 12, 4, "Receive timestamp: %u",
1692           tvb_get_ntohl(tvb, 12));
1693         proto_tree_add_text(icmp_tree, tvb, 16, 4, "Transmit timestamp: %u",
1694           tvb_get_ntohl(tvb, 16));
1695         break;
1696
1697     case ICMP_MASKREQ:
1698     case ICMP_MASKREPLY:
1699         proto_tree_add_text(icmp_tree, tvb, 8, 4, "Address mask: %s (0x%08x)",
1700           ip_to_str(tvb_get_ptr(tvb, 8, 4)), tvb_get_ntohl(tvb, 8));
1701         break;
1702     }
1703   }
1704 }
1705
1706 void
1707 proto_register_ip(void)
1708 {
1709         static hf_register_info hf[] = {
1710
1711                 { &hf_ip_version,
1712                 { "Version",            "ip.version", FT_UINT8, BASE_DEC, NULL, 0x0,
1713                         "" }},
1714
1715                 { &hf_ip_hdr_len,
1716                 { "Header Length",      "ip.hdr_len", FT_UINT8, BASE_DEC, NULL, 0x0,
1717                         "" }},
1718
1719                 { &hf_ip_dsfield,
1720                 { "Differentiated Services field",      "ip.dsfield", FT_UINT8, BASE_DEC, NULL, 0x0,
1721                         "" }},
1722
1723                 { &hf_ip_dsfield_dscp,
1724                 { "Differentiated Services Codepoint",  "ip.dsfield.dscp", FT_UINT8, BASE_HEX,
1725                         VALS(dscp_vals), IPDSFIELD_DSCP_MASK,
1726                         "" }},
1727
1728                 { &hf_ip_dsfield_ect,
1729                 { "ECN-Capable Transport (ECT)",        "ip.dsfield.ect", FT_UINT8, BASE_DEC, NULL,
1730                         IPDSFIELD_ECT_MASK,
1731                         "" }},
1732
1733                 { &hf_ip_dsfield_ce,
1734                 { "ECN-CE",     "ip.dsfield.ce", FT_UINT8, BASE_DEC, NULL,
1735                         IPDSFIELD_CE_MASK,
1736                         "" }},
1737
1738                 { &hf_ip_tos,
1739                 { "Type of Service",    "ip.tos", FT_UINT8, BASE_DEC, NULL, 0x0,
1740                         "" }},
1741
1742                 { &hf_ip_tos_precedence,
1743                 { "Precedence",         "ip.tos.precedence", FT_UINT8, BASE_DEC, VALS(precedence_vals),
1744                         IPTOS_PREC_MASK,
1745                         "" }},
1746
1747                 { &hf_ip_tos_delay,
1748                 { "Delay",              "ip.tos.delay", FT_BOOLEAN, 8, TFS(&tos_set_low),
1749                         IPTOS_LOWDELAY,
1750                         "" }},
1751
1752                 { &hf_ip_tos_throughput,
1753                 { "Throughput",         "ip.tos.throughput", FT_BOOLEAN, 8, TFS(&tos_set_high),
1754                         IPTOS_THROUGHPUT,
1755                         "" }},
1756
1757                 { &hf_ip_tos_reliability,
1758                 { "Reliability",        "ip.tos.reliability", FT_BOOLEAN, 8, TFS(&tos_set_high),
1759                         IPTOS_RELIABILITY,
1760                         "" }},
1761
1762                 { &hf_ip_tos_cost,
1763                 { "Cost",               "ip.tos.cost", FT_BOOLEAN, 8, TFS(&tos_set_low),
1764                         IPTOS_LOWCOST,
1765                         "" }},
1766
1767                 { &hf_ip_len,
1768                 { "Total Length",       "ip.len", FT_UINT16, BASE_DEC, NULL, 0x0,
1769                         "" }},
1770
1771                 { &hf_ip_id,
1772                 { "Identification",     "ip.id", FT_UINT16, BASE_HEX, NULL, 0x0,
1773                         "" }},
1774
1775                 { &hf_ip_dst,
1776                 { "Destination",        "ip.dst", FT_IPv4, BASE_NONE, NULL, 0x0,
1777                         "" }},
1778
1779                 { &hf_ip_src,
1780                 { "Source",             "ip.src", FT_IPv4, BASE_NONE, NULL, 0x0,
1781                         "" }},
1782
1783                 { &hf_ip_addr,
1784                 { "Source or Destination Address", "ip.addr", FT_IPv4, BASE_NONE, NULL, 0x0,
1785                         "" }},
1786
1787                 { &hf_ip_flags,
1788                 { "Flags",              "ip.flags", FT_UINT8, BASE_HEX, NULL, 0x0,
1789                         "" }},
1790
1791                 { &hf_ip_flags_df,
1792                 { "Don't fragment",     "ip.flags.df", FT_BOOLEAN, 4, TFS(&flags_set_truth), IP_DF>>12,
1793                         "" }},
1794
1795                 { &hf_ip_flags_mf,
1796                 { "More fragments",     "ip.flags.mf", FT_BOOLEAN, 4, TFS(&flags_set_truth), IP_MF>>12,
1797                         "" }},
1798
1799                 { &hf_ip_frag_offset,
1800                 { "Fragment offset",    "ip.frag_offset", FT_UINT16, BASE_DEC, NULL, 0x0,
1801                         "" }},
1802
1803                 { &hf_ip_ttl,
1804                 { "Time to live",       "ip.ttl", FT_UINT8, BASE_DEC, NULL, 0x0,
1805                         "" }},
1806
1807                 { &hf_ip_proto,
1808                 { "Protocol",           "ip.proto", FT_UINT8, BASE_HEX, NULL, 0x0,
1809                         "" }},
1810
1811                 { &hf_ip_checksum,
1812                 { "Header checksum",    "ip.checksum", FT_UINT16, BASE_HEX, NULL, 0x0,
1813                         "" }},
1814
1815                 { &hf_ip_checksum_bad,
1816                 { "Bad Header checksum",        "ip.checksum_bad", FT_BOOLEAN, BASE_NONE, NULL, 0x0,
1817                         "" }},
1818
1819                 { &hf_ip_fragment_overlap,
1820                 { "Fragment overlap",   "ip.fragment.overlap", FT_BOOLEAN, BASE_NONE, NULL, 0x0,
1821                         "Fragment overlaps with other fragments" }},
1822
1823                 { &hf_ip_fragment_overlap_conflict,
1824                 { "Conflicting data in fragment overlap",       "ip.fragment.overlap.conflict", FT_BOOLEAN, BASE_NONE, NULL, 0x0,
1825                         "Overlapping fragments contained conflicting data" }},
1826
1827                 { &hf_ip_fragment_multiple_tails,
1828                 { "Multiple tail fragments found",      "ip.fragment.multipletails", FT_BOOLEAN, BASE_NONE, NULL, 0x0,
1829                         "Several tails were found when defragmenting the packet" }},
1830
1831                 { &hf_ip_fragment_too_long_fragment,
1832                 { "Fragment too long",  "ip.fragment.toolongfragment", FT_BOOLEAN, BASE_NONE, NULL, 0x0,
1833                         "Fragment contained data past end of packet" }},
1834
1835                 { &hf_ip_fragment_error,
1836                 { "Defragmentation error", "ip.fragment.error", FT_NONE, BASE_NONE, NULL, 0x0,
1837                         "Defragmentation error due to illegal fragments" }},
1838
1839                 { &hf_ip_fragment,
1840                 { "IP Fragment", "ip.fragment", FT_NONE, BASE_NONE, NULL, 0x0,
1841                         "IP Fragment" }},
1842
1843                 { &hf_ip_fragments,
1844                 { "IP Fragments", "ip.fragments", FT_NONE, BASE_NONE, NULL, 0x0,
1845                         "IP Fragments" }},
1846         };
1847         static gint *ett[] = {
1848                 &ett_ip,
1849                 &ett_ip_dsfield,
1850                 &ett_ip_tos,
1851                 &ett_ip_off,
1852                 &ett_ip_options,
1853                 &ett_ip_option_sec,
1854                 &ett_ip_option_route,
1855                 &ett_ip_option_timestamp,
1856                 &ett_ip_fragments,
1857                 &ett_ip_fragment,
1858         };
1859         module_t *ip_module;
1860
1861         proto_ip = proto_register_protocol("Internet Protocol", "IP", "ip");
1862         proto_register_field_array(proto_ip, hf, array_length(hf));
1863         proto_register_subtree_array(ett, array_length(ett));
1864
1865         /* subdissector code */
1866         ip_dissector_table = register_dissector_table("ip.proto");
1867
1868         /* Register a configuration option for decoding TOS as DSCP */
1869         ip_module = prefs_register_protocol(proto_ip, NULL);
1870         prefs_register_bool_preference(ip_module, "decode_tos_as_diffserv",
1871             "Decode IPv4 TOS field as DiffServ field",
1872             "Whether the IPv4 type-of-service field should be decoded as a Differentiated Services field",
1873             &g_ip_dscp_actif);
1874         prefs_register_bool_preference(ip_module, "defragment",
1875                 "Reassemble fragmented IP datagrams",
1876                 "Whether fragmented IP datagrams should be reassembled",
1877                 &ip_defragment);
1878         prefs_register_bool_preference(ip_module, "ip_summary_in_tree",
1879             "Show IP summary in protocol tree",
1880             "Whether the IP summary line should be shown in the protocol tree",
1881             &ip_summary_in_tree);
1882
1883         register_dissector("ip", dissect_ip, proto_ip);
1884         register_init_routine(ip_defragment_init);
1885 }
1886
1887 void
1888 proto_reg_handoff_ip(void)
1889 {
1890         dissector_add("ethertype", ETHERTYPE_IP, dissect_ip, proto_ip);
1891         dissector_add("ppp.protocol", PPP_IP, dissect_ip, proto_ip);
1892         dissector_add("ppp.protocol", ETHERTYPE_IP, dissect_ip, proto_ip);
1893         dissector_add("gre.proto", ETHERTYPE_IP, dissect_ip, proto_ip);
1894         dissector_add("gre.proto", GRE_WCCP, dissect_ip, proto_ip);
1895         dissector_add("llc.dsap", SAP_IP, dissect_ip, proto_ip);
1896         dissector_add("ip.proto", IP_PROTO_IPIP, dissect_ip, proto_ip);
1897         dissector_add("null.type", BSD_AF_INET, dissect_ip, proto_ip);
1898         dissector_add("chdlctype", ETHERTYPE_IP, dissect_ip, proto_ip);
1899         dissector_add("fr.ietf", NLPID_IP, dissect_ip, proto_ip);
1900 }
1901
1902 void
1903 proto_register_icmp(void)
1904 {
1905   static hf_register_info hf[] = {
1906     
1907     { &hf_icmp_type,
1908       { "Type",         "icmp.type",            FT_UINT8, BASE_DEC,     NULL, 0x0,
1909         "" }},
1910
1911     { &hf_icmp_code,
1912       { "Code",         "icmp.code",            FT_UINT8, BASE_HEX,     NULL, 0x0,
1913         "" }},    
1914
1915     { &hf_icmp_checksum,
1916       { "Checksum",     "icmp.checksum",        FT_UINT16, BASE_HEX,    NULL, 0x0,
1917         "" }},
1918
1919     { &hf_icmp_checksum_bad,
1920       { "Bad Checksum", "icmp.checksum_bad",    FT_BOOLEAN, BASE_NONE,  NULL, 0x0,
1921         "" }},
1922   };
1923   static gint *ett[] = {
1924     &ett_icmp,
1925   };
1926   
1927   proto_icmp = proto_register_protocol("Internet Control Message Protocol", 
1928                                        "ICMP", "icmp");
1929   proto_register_field_array(proto_icmp, hf, array_length(hf));
1930   proto_register_subtree_array(ett, array_length(ett));
1931 }
1932
1933 void
1934 proto_reg_handoff_icmp(void)
1935 {
1936   dissector_add("ip.proto", IP_PROTO_ICMP, dissect_icmp, proto_icmp);
1937 }