2 * Routines for Appletalk packet disassembly (DDP, currently).
4 * $Id: packet-atalk.c,v 1.51 2001/03/15 09:11:00 guy Exp $
6 * Simon Wilkinson <sxw@dcs.ed.ac.uk>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 #ifdef HAVE_SYS_TYPES_H
28 # include <sys/types.h>
31 #ifdef HAVE_NETINET_IN_H
32 # include <netinet/in.h>
37 #include "packet-atalk.h"
42 static int proto_ddp = -1;
43 static int hf_ddp_hopcount = -1;
44 static int hf_ddp_len = -1;
45 static int hf_ddp_checksum = -1;
46 static int hf_ddp_dst_net = -1;
47 static int hf_ddp_src_net = -1;
48 static int hf_ddp_dst_node = -1;
49 static int hf_ddp_src_node = -1;
50 static int hf_ddp_dst_socket = -1;
51 static int hf_ddp_src_socket = -1;
52 static int hf_ddp_type = -1;
54 static int proto_nbp = -1;
55 static int hf_nbp_op = -1;
56 static int hf_nbp_info = -1;
57 static int hf_nbp_count = -1;
58 static int hf_nbp_tid = -1;
60 static int hf_nbp_node_net = -1;
61 static int hf_nbp_node_port = -1;
62 static int hf_nbp_node_node = -1;
63 static int hf_nbp_node_enum = -1;
64 static int hf_nbp_node_object = -1;
65 static int hf_nbp_node_type = -1;
66 static int hf_nbp_node_zone = -1;
68 static int proto_rtmp = -1;
69 static int hf_rtmp_net = -1;
70 static int hf_rtmp_node_len = -1;
71 static int hf_rtmp_node = -1;
72 static int hf_rtmp_tuple_net = -1;
73 static int hf_rtmp_tuple_range_start = -1;
74 static int hf_rtmp_tuple_range_end = -1;
75 static int hf_rtmp_tuple_dist = -1;
76 static int hf_rtmp_function = -1;
78 static gint ett_nbp = -1;
79 static gint ett_nbp_info = -1;
80 static gint ett_nbp_node = -1;
81 static gint ett_rtmp = -1;
82 static gint ett_rtmp_tuple = -1;
83 static gint ett_ddp = -1;
84 static gint ett_pstring = -1;
86 static dissector_table_t ddp_dissector_table;
89 * P = Padding, H = Hops, L = Len
93 * Assumes the argument is in host byte order.
95 #define ddp_hops(x) ( ( x >> 10) & 0x3C )
96 #define ddp_len(x) ( x & 0x03ff )
97 typedef struct _e_ddp {
98 guint16 hops_len; /* combines pad, hops, and len */
99 guint16 sum,dnet,snet;
105 #define DDP_HEADER_SIZE 13
108 atalk_addr_to_str(const struct atalk_ddp_addr *addrp)
110 static gchar str[3][14];
113 if (cur == &str[0][0]) {
115 } else if (cur == &str[1][0]) {
121 sprintf(cur, "%u.%u:%u", addrp->net, addrp->node, addrp->port);
125 static const value_string op_vals[] = {
126 {DDP_RTMPDATA, "AppleTalk Routing Table response or data" },
127 {DDP_NBP, "AppleTalk Name Binding Protocol packet"},
128 {DDP_ATP, "AppleTalk Transaction Protocol packet"},
129 {DDP_AEP, "AppleTalk Echo Protocol packet"},
130 {DDP_RTMPREQ, "AppleTalk Routing Table request"},
131 {DDP_ZIP, "AppleTalk Zone Information Protocol packet"},
132 {DDP_ADSP, "AppleTalk Data Stream Protocol"},
133 {DDP_EIGRP, "Cisco EIGRP for AppleTalk"},
137 static const value_string rtmp_function_vals[] = {
139 {2, "Route Data Request (split horizon processed)"},
140 {3, "Route Data Request (no split horizon processing)"},
145 #define NBP_FORWARD 4
148 static const value_string nbp_op_vals[] = {
149 {NBP_LOOKUP, "lookup"},
150 {NBP_FORWARD, "forward request"},
151 {NBP_REPLY, "reply"},
156 * XXX - do this with an FT_UINT_STRING?
157 * Unfortunately, you can't extract from an FT_UINT_STRING the string,
158 * which we'd want to do in order to put it into the "Data:" portion.
160 int dissect_pascal_string(tvbuff_t *tvb, int offset, proto_tree *tree,
165 len = tvb_get_guint8(tvb, offset);
175 * XXX - if we could do this inside the protocol tree
176 * code, we could perhaps avoid allocating and freeing
177 * this string buffer.
179 tmp = g_malloc( len+1 );
180 tvb_memcpy(tvb, tmp, offset, len);
182 item = proto_tree_add_string(tree, hf_index, tvb, offset-1, len+1, tmp);
184 subtree = proto_item_add_subtree(item, ett_pstring);
185 proto_tree_add_text(subtree, tvb, offset-1, 1, "Length: %d", len);
186 proto_tree_add_text(subtree, tvb, offset, len, "Data: %s", tmp);
196 dissect_rtmp_request(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) {
197 proto_tree *rtmp_tree;
201 if (check_col(pinfo->fd, COL_PROTOCOL))
202 col_set_str(pinfo->fd, COL_PROTOCOL, "RTMP");
203 if (check_col(pinfo->fd, COL_INFO))
204 col_clear(pinfo->fd, COL_INFO);
206 function = tvb_get_guint8(tvb, 0);
208 if (check_col(pinfo->fd, COL_INFO))
209 col_add_fstr(pinfo->fd, COL_INFO, "%s",
210 val_to_str(function, rtmp_function_vals, "Unknown function (%02)"));
213 ti = proto_tree_add_item(tree, proto_rtmp, tvb, 0, 1, FALSE);
214 rtmp_tree = proto_item_add_subtree(ti, ett_rtmp);
216 proto_tree_add_uint(rtmp_tree, hf_rtmp_function, tvb, 0, 1, function);
221 dissect_rtmp_data(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) {
222 proto_tree *rtmp_tree;
226 guint8 nodelen,nodelen_bits;
227 guint16 node; /* might be more than 8 bits */
230 if (check_col(pinfo->fd, COL_PROTOCOL))
231 col_set_str(pinfo->fd, COL_PROTOCOL, "RTMP");
232 if (check_col(pinfo->fd, COL_INFO))
233 col_clear(pinfo->fd, COL_INFO);
235 net = tvb_get_ntohs(tvb, offset);
236 nodelen_bits = tvb_get_guint8(tvb, offset+2);
237 if ( nodelen_bits <= 8 ) {
238 node = tvb_get_guint8(tvb, offset)+1;
241 node = tvb_get_ntohs(tvb, offset);
245 if (check_col(pinfo->fd, COL_INFO))
246 col_add_fstr(pinfo->fd, COL_INFO, "Net: %u Node Len: %u Node: %u",
247 net, nodelen_bits, node);
250 ti = proto_tree_add_item(tree, proto_rtmp, tvb, offset,
251 tvb_length_remaining(tvb, offset), FALSE);
252 rtmp_tree = proto_item_add_subtree(ti, ett_rtmp);
254 proto_tree_add_uint(rtmp_tree, hf_rtmp_net, tvb, offset, 2, net);
255 proto_tree_add_uint(rtmp_tree, hf_rtmp_node_len, tvb, offset+2, 1,
257 proto_tree_add_uint(rtmp_tree, hf_rtmp_node, tvb, offset+3, nodelen,
259 offset += 3 + nodelen;
262 while (tvb_offset_exists(tvb, offset)) {
263 proto_tree *tuple_item, *tuple_tree;
266 guint16 tuple_range_end;
268 tuple_net = tvb_get_ntohs(tvb, offset);
269 tuple_dist = tvb_get_guint8(tvb, offset+2);
271 if (tuple_dist & 0x80) {
272 tuple_range_end = tvb_get_ntohs(tvb, offset+3);
273 tuple_item = proto_tree_add_text(rtmp_tree, tvb, offset, 6,
274 "Tuple %d: Range Start: %u Dist: %u Range End: %u",
275 i, tuple_net, tuple_dist&0x7F, tuple_range_end);
277 tuple_item = proto_tree_add_text(rtmp_tree, tvb, offset, 3,
278 "Tuple %d: Net: %u Dist: %u",
279 i, tuple_net, tuple_dist);
281 tuple_tree = proto_item_add_subtree(tuple_item, ett_rtmp_tuple);
283 if (tuple_dist & 0x80) {
284 proto_tree_add_uint(tuple_tree, hf_rtmp_tuple_range_start, tvb, offset, 2,
287 proto_tree_add_uint(tuple_tree, hf_rtmp_tuple_net, tvb, offset, 2,
290 proto_tree_add_uint(tuple_tree, hf_rtmp_tuple_dist, tvb, offset+2, 1,
293 if (tuple_dist & 0x80) {
295 * Extended network tuple.
297 proto_tree_add_item(tuple_tree, hf_rtmp_tuple_range_end, tvb, offset+3, 2,
309 dissect_nbp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) {
310 proto_tree *nbp_tree;
311 proto_tree *nbp_info_tree;
312 proto_item *ti, *info_item;
318 if (check_col(pinfo->fd, COL_PROTOCOL))
319 col_set_str(pinfo->fd, COL_PROTOCOL, "NBP");
320 if (check_col(pinfo->fd, COL_INFO))
321 col_clear(pinfo->fd, COL_INFO);
323 info = tvb_get_guint8(tvb, offset);
327 if (check_col(pinfo->fd, COL_INFO))
328 col_add_fstr(pinfo->fd, COL_INFO, "Op: %s Count: %u",
329 val_to_str(op, nbp_op_vals, "Unknown (0x%01x)"), count);
332 ti = proto_tree_add_item(tree, proto_nbp, tvb, offset, END_OF_FRAME, FALSE);
333 nbp_tree = proto_item_add_subtree(ti, ett_nbp);
335 info_item = proto_tree_add_uint_format(nbp_tree, hf_nbp_info, tvb, offset, 1,
337 "Info: 0x%01X Operation: %s Count: %u", info,
338 val_to_str(op, nbp_op_vals, "Unknown (0x%01X)"),
340 nbp_info_tree = proto_item_add_subtree(info_item, ett_nbp_info);
341 proto_tree_add_uint(nbp_info_tree, hf_nbp_op, tvb, offset, 1, info);
342 proto_tree_add_uint(nbp_info_tree, hf_nbp_count, tvb, offset, 1, info);
343 proto_tree_add_item(nbp_tree, hf_nbp_tid, tvb, offset+1, 1, FALSE);
346 for (i=0; i<count; i++) {
347 proto_tree *node_item,*node_tree;
348 int soffset = offset;
350 node_item = proto_tree_add_text(nbp_tree, tvb, offset, 4,
352 node_tree = proto_item_add_subtree(node_item, ett_nbp_node);
354 proto_tree_add_item(node_tree, hf_nbp_node_net, tvb, offset, 2, FALSE);
356 proto_tree_add_item(node_tree, hf_nbp_node_node, tvb, offset, 1, FALSE);
358 proto_tree_add_item(node_tree, hf_nbp_node_port, tvb, offset, 1, FALSE);
360 proto_tree_add_item(node_tree, hf_nbp_node_enum, tvb, offset, 1, FALSE);
363 offset = dissect_pascal_string(tvb, offset, node_tree, hf_nbp_node_object);
364 offset = dissect_pascal_string(tvb, offset, node_tree, hf_nbp_node_type);
365 offset = dissect_pascal_string(tvb, offset, node_tree, hf_nbp_node_zone);
367 proto_item_set_len(node_item, offset-soffset);
375 dissect_ddp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) {
377 proto_tree *ddp_tree;
379 static struct atalk_ddp_addr src, dst;
382 if (check_col(pinfo->fd, COL_PROTOCOL))
383 col_set_str(pinfo->fd, COL_PROTOCOL, "DDP");
384 if (check_col(pinfo->fd, COL_INFO))
385 col_clear(pinfo->fd, COL_INFO);
387 tvb_memcpy(tvb, (guint8 *)&ddp, 0, sizeof(e_ddp));
388 ddp.dnet=ntohs(ddp.dnet);
389 ddp.snet=ntohs(ddp.snet);
390 ddp.sum=ntohs(ddp.sum);
391 ddp.hops_len=ntohs(ddp.hops_len);
394 src.node = ddp.snode;
395 src.port = ddp.sport;
397 dst.node = ddp.dnode;
398 dst.port = ddp.dport;
399 SET_ADDRESS(&pinfo->net_src, AT_ATALK, sizeof src, (guint8 *)&src);
400 SET_ADDRESS(&pinfo->src, AT_ATALK, sizeof src, (guint8 *)&src);
401 SET_ADDRESS(&pinfo->net_dst, AT_ATALK, sizeof dst, (guint8 *)&dst);
402 SET_ADDRESS(&pinfo->dst, AT_ATALK, sizeof dst, (guint8 *)&dst);
404 if (check_col(pinfo->fd, COL_INFO))
405 col_add_str(pinfo->fd, COL_INFO,
406 val_to_str(ddp.type, op_vals, "Unknown DDP protocol (%02x)"));
409 ti = proto_tree_add_item(tree, proto_ddp, tvb, 0, DDP_HEADER_SIZE,
411 ddp_tree = proto_item_add_subtree(ti, ett_ddp);
412 proto_tree_add_uint(ddp_tree, hf_ddp_hopcount, tvb, 0, 1,
413 ddp_hops(ddp.hops_len));
414 proto_tree_add_uint(ddp_tree, hf_ddp_len, tvb, 0, 2,
415 ddp_len(ddp.hops_len));
416 proto_tree_add_uint(ddp_tree, hf_ddp_checksum, tvb, 2, 2,
418 proto_tree_add_uint(ddp_tree, hf_ddp_dst_net, tvb, 4, 2,
420 proto_tree_add_uint(ddp_tree, hf_ddp_src_net, tvb, 6, 2,
422 proto_tree_add_uint(ddp_tree, hf_ddp_dst_node, tvb, 8, 1,
424 proto_tree_add_uint(ddp_tree, hf_ddp_src_node, tvb, 9, 1,
426 proto_tree_add_uint(ddp_tree, hf_ddp_dst_socket, tvb, 10, 1,
428 proto_tree_add_uint(ddp_tree, hf_ddp_src_socket, tvb, 11, 1,
430 proto_tree_add_uint(ddp_tree, hf_ddp_type, tvb, 12, 1,
434 new_tvb = tvb_new_subset(tvb, DDP_HEADER_SIZE, -1, -1);
436 if (!dissector_try_port(ddp_dissector_table, ddp.type, new_tvb, pinfo, tree))
437 dissect_data(new_tvb, 0, pinfo, tree);
441 proto_register_atalk(void)
443 static hf_register_info hf_ddp[] = {
445 { "Hop count", "ddp.hopcount", FT_UINT8, BASE_DEC, NULL, 0x0,
449 { "Datagram length", "ddp.len", FT_UINT16, BASE_DEC, NULL, 0x0,
453 { "Checksum", "ddp.checksum", FT_UINT16, BASE_DEC, NULL, 0x0,
457 { "Destination Net", "ddp.dst.net", FT_UINT16, BASE_DEC, NULL, 0x0,
461 { "Source Net", "ddp.src.net", FT_UINT16, BASE_DEC, NULL, 0x0,
465 { "Destination Node", "ddp.dst.node", FT_UINT8, BASE_DEC, NULL, 0x0,
469 { "Source Node", "ddp.src.node", FT_UINT8, BASE_DEC, NULL, 0x0,
472 { &hf_ddp_dst_socket,
473 { "Destination Socket", "ddp.dst.socket", FT_UINT8, BASE_DEC, NULL, 0x0,
476 { &hf_ddp_src_socket,
477 { "Source Socket", "ddp.src.socket", FT_UINT8, BASE_DEC, NULL, 0x0,
481 { "Protocol type", "ddp.type", FT_UINT8, BASE_DEC, VALS(op_vals), 0x0,
485 static hf_register_info hf_nbp[] = {
487 { "Operation", "nbp.op", FT_UINT8, BASE_DEC,
488 VALS(nbp_op_vals), 0xF0, "Operation" }},
490 { "Info", "nbp.info", FT_UINT8, BASE_HEX,
491 NULL, 0x0, "Info" }},
493 { "Count", "nbp.count", FT_UINT8, BASE_DEC,
494 NULL, 0x0F, "Count" }},
496 { "Network", "nbp.net", FT_UINT16, BASE_DEC,
497 NULL, 0x0, "Network" }},
499 { "Node", "nbp.node", FT_UINT8, BASE_DEC,
500 NULL, 0x0, "Node" }},
502 { "Port", "nbp.port", FT_UINT8, BASE_DEC,
503 NULL, 0x0, "Port" }},
505 { "Enumerator", "nbp.enum", FT_UINT8, BASE_DEC,
506 NULL, 0x0, "Enumerator" }},
507 { &hf_nbp_node_object,
508 { "Object", "nbp.object", FT_STRING, BASE_DEC,
509 NULL, 0x0, "Object" }},
511 { "Type", "nbp.type", FT_STRING, BASE_DEC,
512 NULL, 0x0, "Type" }},
514 { "Zone", "nbp.zone", FT_STRING, BASE_DEC,
515 NULL, 0x0, "Zone" }},
517 { "Transaction ID", "nbp.tid", FT_UINT8, BASE_DEC,
518 NULL, 0x0, "Transaction ID" }}
521 static hf_register_info hf_rtmp[] = {
523 { "Net", "rtmp.net", FT_UINT16, BASE_DEC,
526 { "Node", "nbp.nodeid", FT_UINT8, BASE_DEC,
527 NULL, 0x0, "Node" }},
529 { "Node Length", "nbp.nodeid.length", FT_UINT8, BASE_DEC,
530 NULL, 0x0, "Node Length" }},
531 { &hf_rtmp_tuple_net,
532 { "Net", "rtmp.tuple.net", FT_UINT16, BASE_DEC,
534 { &hf_rtmp_tuple_range_start,
535 { "Range Start", "rtmp.tuple.range_start", FT_UINT16, BASE_DEC,
536 NULL, 0x0, "Range Start" }},
537 { &hf_rtmp_tuple_range_end,
538 { "Range End", "rtmp.tuple.range_end", FT_UINT16, BASE_DEC,
539 NULL, 0x0, "Range End" }},
540 { &hf_rtmp_tuple_dist,
541 { "Distance", "rtmp.tuple.dist", FT_UINT16, BASE_DEC,
542 NULL, 0x0, "Distance" }},
544 { "Function", "rtmp.function", FT_UINT8, BASE_DEC,
545 VALS(rtmp_function_vals), 0x0, "Request Function" }}
549 static gint *ett[] = {
559 proto_ddp = proto_register_protocol("Datagram Delivery Protocol", "DDP", "ddp");
560 proto_register_field_array(proto_ddp, hf_ddp, array_length(hf_ddp));
562 proto_nbp = proto_register_protocol("Name Binding Protocol", "NBP", "nbp");
563 proto_register_field_array(proto_nbp, hf_nbp, array_length(hf_nbp));
565 proto_rtmp = proto_register_protocol("Routing Table Maintenance Protocol",
567 proto_register_field_array(proto_rtmp, hf_rtmp, array_length(hf_rtmp));
569 proto_register_subtree_array(ett, array_length(ett));
571 /* subdissector code */
572 ddp_dissector_table = register_dissector_table("ddp.type");
576 proto_reg_handoff_atalk(void)
578 dissector_add("ethertype", ETHERTYPE_ATALK, dissect_ddp, proto_ddp);
579 dissector_add("chdlctype", ETHERTYPE_ATALK, dissect_ddp, proto_ddp);
580 dissector_add("ppp.protocol", PPP_AT, dissect_ddp, proto_ddp);
581 dissector_add("null.type", BSD_AF_APPLETALK, dissect_ddp, proto_ddp);
582 dissector_add("ddp.type", DDP_NBP, dissect_nbp, proto_nbp);
583 dissector_add("ddp.type", DDP_RTMPREQ, dissect_rtmp_request, proto_rtmp);
584 dissector_add("ddp.type", DDP_RTMPDATA, dissect_rtmp_data, proto_rtmp);