2 * Routines for Appletalk packet disassembly (DDP, currently).
4 * $Id: packet-atalk.c,v 1.48 2001/01/03 06:55:27 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 CHECK_DISPLAY_AS_DATA(proto_rtmp, tvb, pinfo, tree);
203 pinfo->current_proto = "RTMP";
205 function = tvb_get_guint8(tvb, 0);
207 if (check_col(pinfo->fd, COL_PROTOCOL))
208 col_set_str(pinfo->fd, COL_PROTOCOL, "RTMP");
210 if (check_col(pinfo->fd, COL_INFO))
211 col_add_fstr(pinfo->fd, COL_INFO, "%s",
212 val_to_str(function, rtmp_function_vals, "Unknown function (%02)"));
215 ti = proto_tree_add_item(tree, proto_rtmp, tvb, 0, 1, FALSE);
216 rtmp_tree = proto_item_add_subtree(ti, ett_rtmp);
218 proto_tree_add_uint(rtmp_tree, hf_rtmp_function, tvb, 0, 1, function);
223 dissect_rtmp_data(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) {
224 proto_tree *rtmp_tree;
228 guint8 nodelen,nodelen_bits;
229 guint16 node; /* might be more than 8 bits */
232 CHECK_DISPLAY_AS_DATA(proto_rtmp, tvb, pinfo, tree);
234 pinfo->current_proto = "RTMP";
236 net = tvb_get_ntohs(tvb, offset);
237 nodelen_bits = tvb_get_guint8(tvb, offset+2);
238 if ( nodelen_bits <= 8 ) {
239 node = tvb_get_guint8(tvb, offset)+1;
242 node = tvb_get_ntohs(tvb, offset);
246 if (check_col(pinfo->fd, COL_PROTOCOL))
247 col_set_str(pinfo->fd, COL_PROTOCOL, "RTMP");
249 if (check_col(pinfo->fd, COL_INFO))
250 col_add_fstr(pinfo->fd, COL_INFO, "Net: %u Node Len: %u Node: %u",
251 net, nodelen_bits, node);
254 ti = proto_tree_add_item(tree, proto_rtmp, tvb, offset,
255 tvb_length_remaining(tvb, offset), FALSE);
256 rtmp_tree = proto_item_add_subtree(ti, ett_rtmp);
258 proto_tree_add_uint(rtmp_tree, hf_rtmp_net, tvb, offset, 2, net);
259 proto_tree_add_uint(rtmp_tree, hf_rtmp_node_len, tvb, offset+2, 1,
261 proto_tree_add_uint(rtmp_tree, hf_rtmp_node, tvb, offset+3, nodelen,
263 offset += 3 + nodelen;
266 while (tvb_offset_exists(tvb, offset)) {
267 proto_tree *tuple_item, *tuple_tree;
270 guint16 tuple_range_end;
272 tuple_net = tvb_get_ntohs(tvb, offset);
273 tuple_dist = tvb_get_guint8(tvb, offset+2);
275 if (tuple_dist & 0x80) {
276 tuple_range_end = tvb_get_ntohs(tvb, offset+3);
277 tuple_item = proto_tree_add_text(rtmp_tree, tvb, offset, 6,
278 "Tuple %d: Range Start: %u Dist: %u Range End: %u",
279 i, tuple_net, tuple_dist&0x7F, tuple_range_end);
281 tuple_item = proto_tree_add_text(rtmp_tree, tvb, offset, 3,
282 "Tuple %d: Net: %u Dist: %u",
283 i, tuple_net, tuple_dist);
285 tuple_tree = proto_item_add_subtree(tuple_item, ett_rtmp_tuple);
287 if (tuple_dist & 0x80) {
288 proto_tree_add_uint(tuple_tree, hf_rtmp_tuple_range_start, tvb, offset, 2,
291 proto_tree_add_uint(tuple_tree, hf_rtmp_tuple_net, tvb, offset, 2,
294 proto_tree_add_uint(tuple_tree, hf_rtmp_tuple_dist, tvb, offset+2, 1,
297 if (tuple_dist & 0x80) {
299 * Extended network tuple.
301 proto_tree_add_item(tuple_tree, hf_rtmp_tuple_range_end, tvb, offset+3, 2,
313 dissect_nbp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) {
314 proto_tree *nbp_tree;
315 proto_tree *nbp_info_tree;
316 proto_item *ti, *info_item;
322 CHECK_DISPLAY_AS_DATA(proto_nbp, tvb, pinfo, tree);
324 pinfo->current_proto = "NBP";
326 info = tvb_get_guint8(tvb, offset);
330 if (check_col(pinfo->fd, COL_PROTOCOL))
331 col_set_str(pinfo->fd, COL_PROTOCOL, "NBP");
333 if (check_col(pinfo->fd, COL_INFO))
334 col_add_fstr(pinfo->fd, COL_INFO, "Op: %s Count: %u",
335 val_to_str(op, nbp_op_vals, "Unknown (0x%01x)"), count);
338 ti = proto_tree_add_item(tree, proto_nbp, tvb, offset, END_OF_FRAME, FALSE);
339 nbp_tree = proto_item_add_subtree(ti, ett_nbp);
341 info_item = proto_tree_add_uint_format(nbp_tree, hf_nbp_info, tvb, offset, 1,
343 "Info: 0x%01X Operation: %s Count: %u", info,
344 val_to_str(op, nbp_op_vals, "Unknown (0x%01X)"),
346 nbp_info_tree = proto_item_add_subtree(info_item, ett_nbp_info);
347 proto_tree_add_uint(nbp_info_tree, hf_nbp_op, tvb, offset, 1, info);
348 proto_tree_add_uint(nbp_info_tree, hf_nbp_count, tvb, offset, 1, info);
349 proto_tree_add_item(nbp_tree, hf_nbp_tid, tvb, offset+1, 1, FALSE);
352 for (i=0; i<count; i++) {
353 proto_tree *node_item,*node_tree;
354 int soffset = offset;
356 node_item = proto_tree_add_text(nbp_tree, tvb, offset, 4,
358 node_tree = proto_item_add_subtree(node_item, ett_nbp_node);
360 proto_tree_add_item(node_tree, hf_nbp_node_net, tvb, offset, 2, FALSE);
362 proto_tree_add_item(node_tree, hf_nbp_node_node, tvb, offset, 1, FALSE);
364 proto_tree_add_item(node_tree, hf_nbp_node_port, tvb, offset, 1, FALSE);
366 proto_tree_add_item(node_tree, hf_nbp_node_enum, tvb, offset, 1, FALSE);
369 offset = dissect_pascal_string(tvb, offset, node_tree, hf_nbp_node_object);
370 offset = dissect_pascal_string(tvb, offset, node_tree, hf_nbp_node_type);
371 offset = dissect_pascal_string(tvb, offset, node_tree, hf_nbp_node_zone);
373 proto_item_set_len(node_item, offset-soffset);
381 dissect_ddp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) {
383 proto_tree *ddp_tree;
385 static struct atalk_ddp_addr src, dst;
388 CHECK_DISPLAY_AS_DATA(proto_ddp, tvb, pinfo, tree);
390 pinfo->current_proto = "DDP";
392 tvb_memcpy(tvb, (guint8 *)&ddp, 0, sizeof(e_ddp));
393 ddp.dnet=ntohs(ddp.dnet);
394 ddp.snet=ntohs(ddp.snet);
395 ddp.sum=ntohs(ddp.sum);
396 ddp.hops_len=ntohs(ddp.hops_len);
399 src.node = ddp.snode;
400 src.port = ddp.sport;
402 dst.node = ddp.dnode;
403 dst.port = ddp.dport;
404 SET_ADDRESS(&pinfo->net_src, AT_ATALK, sizeof src, (guint8 *)&src);
405 SET_ADDRESS(&pinfo->src, AT_ATALK, sizeof src, (guint8 *)&src);
406 SET_ADDRESS(&pinfo->net_dst, AT_ATALK, sizeof dst, (guint8 *)&dst);
407 SET_ADDRESS(&pinfo->dst, AT_ATALK, sizeof dst, (guint8 *)&dst);
409 if (check_col(pinfo->fd, COL_PROTOCOL))
410 col_set_str(pinfo->fd, COL_PROTOCOL, "DDP");
411 if (check_col(pinfo->fd, COL_INFO))
412 col_add_str(pinfo->fd, COL_INFO,
413 val_to_str(ddp.type, op_vals, "Unknown DDP protocol (%02x)"));
416 ti = proto_tree_add_item(tree, proto_ddp, tvb, 0, DDP_HEADER_SIZE,
418 ddp_tree = proto_item_add_subtree(ti, ett_ddp);
419 proto_tree_add_uint(ddp_tree, hf_ddp_hopcount, tvb, 0, 1,
420 ddp_hops(ddp.hops_len));
421 proto_tree_add_uint(ddp_tree, hf_ddp_len, tvb, 0, 2,
422 ddp_len(ddp.hops_len));
423 proto_tree_add_uint(ddp_tree, hf_ddp_checksum, tvb, 2, 2,
425 proto_tree_add_uint(ddp_tree, hf_ddp_dst_net, tvb, 4, 2,
427 proto_tree_add_uint(ddp_tree, hf_ddp_src_net, tvb, 6, 2,
429 proto_tree_add_uint(ddp_tree, hf_ddp_dst_node, tvb, 8, 1,
431 proto_tree_add_uint(ddp_tree, hf_ddp_src_node, tvb, 9, 1,
433 proto_tree_add_uint(ddp_tree, hf_ddp_dst_socket, tvb, 10, 1,
435 proto_tree_add_uint(ddp_tree, hf_ddp_src_socket, tvb, 11, 1,
437 proto_tree_add_uint(ddp_tree, hf_ddp_type, tvb, 12, 1,
441 new_tvb = tvb_new_subset(tvb, DDP_HEADER_SIZE, -1, -1);
443 if (!dissector_try_port(ddp_dissector_table, ddp.type, new_tvb, pinfo, tree))
444 dissect_data(new_tvb, 0, pinfo, tree);
448 proto_register_atalk(void)
450 static hf_register_info hf_ddp[] = {
452 { "Hop count", "ddp.hopcount", FT_UINT8, BASE_DEC, NULL, 0x0,
456 { "Datagram length", "ddp.len", FT_UINT16, BASE_DEC, NULL, 0x0,
460 { "Checksum", "ddp.checksum", FT_UINT16, BASE_DEC, NULL, 0x0,
464 { "Destination Net", "ddp.dst.net", FT_UINT16, BASE_DEC, NULL, 0x0,
468 { "Source Net", "ddp.src.net", FT_UINT16, BASE_DEC, NULL, 0x0,
472 { "Destination Node", "ddp.dst.node", FT_UINT8, BASE_DEC, NULL, 0x0,
476 { "Source Node", "ddp.src.node", FT_UINT8, BASE_DEC, NULL, 0x0,
479 { &hf_ddp_dst_socket,
480 { "Destination Socket", "ddp.dst.socket", FT_UINT8, BASE_DEC, NULL, 0x0,
483 { &hf_ddp_src_socket,
484 { "Source Socket", "ddp.src.socket", FT_UINT8, BASE_DEC, NULL, 0x0,
488 { "Protocol type", "ddp.type", FT_UINT8, BASE_DEC, VALS(op_vals), 0x0,
492 static hf_register_info hf_nbp[] = {
494 { "Operation", "nbp.op", FT_UINT8, BASE_DEC,
495 VALS(nbp_op_vals), 0xF0, "Operation" }},
497 { "Info", "nbp.info", FT_UINT8, BASE_HEX,
498 NULL, 0x0, "Info" }},
500 { "Count", "nbp.count", FT_UINT8, BASE_DEC,
501 NULL, 0x0F, "Count" }},
503 { "Network", "nbp.net", FT_UINT16, BASE_DEC,
504 NULL, 0x0, "Network" }},
506 { "Node", "nbp.node", FT_UINT8, BASE_DEC,
507 NULL, 0x0, "Node" }},
509 { "Port", "nbp.port", FT_UINT8, BASE_DEC,
510 NULL, 0x0, "Port" }},
512 { "Enumerator", "nbp.enum", FT_UINT8, BASE_DEC,
513 NULL, 0x0, "Enumerator" }},
514 { &hf_nbp_node_object,
515 { "Object", "nbp.object", FT_STRING, BASE_DEC,
516 NULL, 0x0, "Object" }},
518 { "Type", "nbp.type", FT_STRING, BASE_DEC,
519 NULL, 0x0, "Type" }},
521 { "Zone", "nbp.zone", FT_STRING, BASE_DEC,
522 NULL, 0x0, "Zone" }},
524 { "Transaction ID", "nbp.tid", FT_UINT8, BASE_DEC,
525 NULL, 0x0, "Transaction ID" }}
528 static hf_register_info hf_rtmp[] = {
530 { "Net", "rtmp.net", FT_UINT16, BASE_DEC,
533 { "Node", "nbp.nodeid", FT_UINT8, BASE_DEC,
534 NULL, 0x0, "Node" }},
536 { "Node Length", "nbp.nodeid.length", FT_UINT8, BASE_DEC,
537 NULL, 0x0, "Node Length" }},
538 { &hf_rtmp_tuple_net,
539 { "Net", "rtmp.tuple.net", FT_UINT16, BASE_DEC,
541 { &hf_rtmp_tuple_range_start,
542 { "Range Start", "rtmp.tuple.range_start", FT_UINT16, BASE_DEC,
543 NULL, 0x0, "Range Start" }},
544 { &hf_rtmp_tuple_range_end,
545 { "Range End", "rtmp.tuple.range_end", FT_UINT16, BASE_DEC,
546 NULL, 0x0, "Range End" }},
547 { &hf_rtmp_tuple_dist,
548 { "Distance", "rtmp.tuple.dist", FT_UINT16, BASE_DEC,
549 NULL, 0x0, "Distance" }},
551 { "Function", "rtmp.function", FT_UINT8, BASE_DEC,
552 VALS(rtmp_function_vals), 0x0, "Request Function" }}
556 static gint *ett[] = {
566 proto_ddp = proto_register_protocol("Datagram Delivery Protocol", "DDP", "ddp");
567 proto_register_field_array(proto_ddp, hf_ddp, array_length(hf_ddp));
569 proto_nbp = proto_register_protocol("Name Binding Protocol", "NBP", "nbp");
570 proto_register_field_array(proto_nbp, hf_nbp, array_length(hf_nbp));
572 proto_rtmp = proto_register_protocol("Routing Table Maintenance Protocol",
574 proto_register_field_array(proto_rtmp, hf_rtmp, array_length(hf_rtmp));
576 proto_register_subtree_array(ett, array_length(ett));
578 /* subdissector code */
579 ddp_dissector_table = register_dissector_table("ddp.type");
583 proto_reg_handoff_atalk(void)
585 dissector_add("ethertype", ETHERTYPE_ATALK, dissect_ddp);
586 dissector_add("ppp.protocol", PPP_AT, dissect_ddp);
587 dissector_add("null.type", BSD_AF_APPLETALK, dissect_ddp);
588 dissector_add("ddp.type", DDP_NBP, dissect_nbp);
589 dissector_add("ddp.type", DDP_RTMPREQ, dissect_rtmp_request);
590 dissector_add("ddp.type", DDP_RTMPDATA, dissect_rtmp_data);