3 * Copyright (c) 2003 Markus Friedl. All rights reserved.
5 * SPDX-License-Identifier: BSD-2-Clause
10 #include <epan/packet.h>
11 #include <epan/capture_dissectors.h>
12 #include <epan/aftypes.h>
13 #include <wsutil/pint.h>
15 void proto_register_enc(void);
16 void proto_reg_handoff_enc(void);
18 static dissector_handle_t enc_handle
;
19 static capture_dissector_handle_t enc_cap_handle
;
22 /* The header in OpenBSD Encapsulating Interface files. */
29 #define BSD_ENC_HDRLEN 12
31 #define BSD_ENC_M_CONF 0x00000400 /* payload encrypted */
32 #define BSD_ENC_M_AUTH 0x00000800 /* payload authenticated */
33 #define BSD_ENC_M_COMP 0x00001000 /* payload compressed */
34 #define BSD_ENC_M_AUTH_AH 0x00002000 /* header authenticated */
36 #define BSD_ENC_M_RESERVED 0xFFFFC3FF /* Reserved/unused flags */
38 static dissector_table_t enc_dissector_table
;
43 static int hf_enc_spi
;
44 static int hf_enc_flags
;
45 static int hf_enc_flags_payload_enc
;
46 static int hf_enc_flags_payload_auth
;
47 static int hf_enc_flags_payload_compress
;
48 static int hf_enc_flags_header_auth
;
49 static int hf_enc_flags_reserved
;
52 static int ett_enc_flag
;
55 capture_enc(const unsigned char *pd
, int offset _U_
, int len
, capture_packet_info_t
*cpinfo
, const union wtap_pseudo_header
*pseudo_header
)
59 if (!BYTES_ARE_IN_FRAME(0, len
, BSD_ENC_HDRLEN
))
62 memcpy((char *)&af
, (const char *)&pd
[0], sizeof(af
));
63 if ((af
& 0xFFFF0000) != 0) {
65 * BSD AF_ types will always have the upper 16 bits as 0, so if any
66 * of them are non-zero, the af field must be byte-swapped, and
67 * will, at least in DLT_ENC headers, always have at least one of
68 * the lower 16 bits not being 0 (it won't be AF_UNSPEC, which is
69 * 0), so if the af field is byte-swapped, at least one of the
70 * upper 16 bits will be 0.
72 af
= GUINT32_SWAP_LE_BE(af
);
74 return try_capture_dissector("enc", af
, pd
, BSD_ENC_HDRLEN
, len
, cpinfo
, pseudo_header
);
77 static const value_string af_vals
[] = {
78 { BSD_AF_INET
, "IPv4" },
79 { BSD_AF_INET6_BSD
, "IPv6" },
84 dissect_enc(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*tree
, void* data _U_
)
87 unsigned writer_encoding
;
92 static int * const flags
[] = {
93 &hf_enc_flags_payload_enc
,
94 &hf_enc_flags_payload_auth
,
95 &hf_enc_flags_payload_compress
,
96 &hf_enc_flags_header_auth
,
97 &hf_enc_flags_reserved
,
101 col_set_str(pinfo
->cinfo
, COL_PROTOCOL
, "ENC");
104 * Initially assume the file was written by a host with our byte order.
106 writer_encoding
= ENC_HOST_ENDIAN
;
107 ench
.af
= tvb_get_h_uint32(tvb
, 0);
108 if ((ench
.af
& 0xFFFF0000) != 0) {
110 * BSD AF_ types will always have the upper 16 bits as 0, so if any
111 * of them are non-zero, the af field must be byte-swapped, and
112 * will, at least in DLT_ENC headers, always have at least one of
113 * the lower 16 bits not being 0 (it won't be AF_UNSPEC, which is
114 * 0), so if the af field is byte-swapped, at least one of the
115 * upper 16 bits will be 0.
117 ench
.af
= GUINT32_SWAP_LE_BE(ench
.af
);
120 * It was written by a host with the *opposite* byte order.
122 writer_encoding
= ENC_ANTI_HOST_ENDIAN
;
124 ench
.spi
= tvb_get_ntohl(tvb
, 4);
127 ti
= proto_tree_add_protocol_format(tree
, proto_enc
, tvb
, 0,
129 "Enc %s, SPI 0x%8.8x",
130 val_to_str(ench
.af
, af_vals
, "unknown (%u)"),
132 enc_tree
= proto_item_add_subtree(ti
, ett_enc
);
134 proto_tree_add_item(enc_tree
, hf_enc_af
, tvb
, 0, 4, writer_encoding
);
135 proto_tree_add_item(enc_tree
, hf_enc_spi
, tvb
, 4, 4, ENC_BIG_ENDIAN
);
136 proto_tree_add_bitmask(enc_tree
, tvb
, 8, hf_enc_flags
, ett_enc_flag
, flags
, writer_encoding
);
139 /* Set the tvbuff for the payload after the header */
140 next_tvb
= tvb_new_subset_remaining(tvb
, BSD_ENC_HDRLEN
);
141 if (!dissector_try_uint(enc_dissector_table
, ench
.af
, next_tvb
, pinfo
, tree
))
142 call_data_dissector(next_tvb
, pinfo
, tree
);
144 return tvb_captured_length(tvb
);
148 proto_register_enc(void)
150 static hf_register_info hf
[] = {
152 { "Address Family", "enc.af", FT_UINT32
, BASE_DEC
, VALS(af_vals
), 0x0,
153 "Protocol (IPv4 vs IPv6)", HFILL
}},
155 { "SPI", "enc.spi", FT_UINT32
, BASE_HEX
, NULL
, 0x0,
156 "Security Parameter Index", HFILL
}},
158 { "Flags", "enc.flags", FT_UINT32
, BASE_HEX
, NULL
, 0x0,
159 "ENC flags", HFILL
}},
160 { &hf_enc_flags_payload_enc
,
161 { "Payload encrypted", "enc.flags.payload_enc", FT_BOOLEAN
, 32, NULL
, BSD_ENC_M_CONF
,
163 { &hf_enc_flags_payload_auth
,
164 { "Payload authenticated", "enc.flags.payload_auth", FT_BOOLEAN
, 32, NULL
, BSD_ENC_M_AUTH
,
166 { &hf_enc_flags_payload_compress
,
167 { "Payload compressed", "enc.flags.payload_compress", FT_BOOLEAN
, 32, NULL
, BSD_ENC_M_COMP
,
169 { &hf_enc_flags_header_auth
,
170 { "Header authenticated", "enc.flags.header_auth", FT_BOOLEAN
, 32, NULL
, BSD_ENC_M_AUTH_AH
,
172 { &hf_enc_flags_reserved
,
173 { "Reserved", "enc.flags.reserved", FT_UINT32
, BASE_HEX
, NULL
, BSD_ENC_M_RESERVED
,
182 proto_enc
= proto_register_protocol("OpenBSD Encapsulating device",
184 proto_register_field_array(proto_enc
, hf
, array_length(hf
));
185 proto_register_subtree_array(ett
, array_length(ett
));
187 enc_dissector_table
= register_dissector_table("enc", "OpenBSD Encapsulating device", proto_enc
, FT_UINT32
, BASE_DEC
);
188 register_capture_dissector_table("enc", "ENC");
190 enc_handle
= register_dissector("enc", dissect_enc
, proto_enc
);
191 enc_cap_handle
= register_capture_dissector("enc", capture_enc
, proto_enc
);
195 proto_reg_handoff_enc(void)
197 dissector_add_uint("wtap_encap", WTAP_ENCAP_ENC
, enc_handle
);
198 capture_dissector_add_uint("wtap_encap", WTAP_ENCAP_ENC
, enc_cap_handle
);
202 * Editor modelines - https://www.wireshark.org/tools/modelines.html
207 * indent-tabs-mode: nil
210 * ex: set shiftwidth=2 tabstop=8 expandtab:
211 * :indentSize=2:tabSize=8:noTabs=true: