2 * Routines for Appletalk packet disassembly (DDP, currently).
4 * $Id: packet-atalk.c,v 1.37 2000/05/30 03:35:51 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"
41 static int proto_ddp = -1;
42 static int hf_ddp_hopcount = -1;
43 static int hf_ddp_len = -1;
44 static int hf_ddp_checksum = -1;
45 static int hf_ddp_dst_net = -1;
46 static int hf_ddp_src_net = -1;
47 static int hf_ddp_dst_node = -1;
48 static int hf_ddp_src_node = -1;
49 static int hf_ddp_dst_socket = -1;
50 static int hf_ddp_src_socket = -1;
51 static int hf_ddp_type = -1;
53 static int proto_nbp = -1;
54 static int hf_nbp_op = -1;
55 static int hf_nbp_info = -1;
56 static int hf_nbp_count = -1;
57 static int hf_nbp_tid = -1;
59 static int hf_nbp_node_net = -1;
60 static int hf_nbp_node_port = -1;
61 static int hf_nbp_node_node = -1;
62 static int hf_nbp_node_enum = -1;
63 static int hf_nbp_node_object = -1;
64 static int hf_nbp_node_type = -1;
65 static int hf_nbp_node_zone = -1;
67 static int proto_rtmp = -1;
68 static int hf_rtmp_tuple_net = -1;
69 static int hf_rtmp_tuple_dist = -1;
70 static int hf_rtmp_net = -1;
71 static int hf_rtmp_node_len = -1;
72 static int hf_rtmp_node = -1;
74 static gint ett_nbp = -1;
75 static gint ett_nbp_info = -1;
76 static gint ett_nbp_node = -1;
77 static gint ett_rtmp = -1;
78 static gint ett_rtmp_tuple = -1;
79 static gint ett_ddp = -1;
80 static gint ett_pstring = -1;
82 static dissector_table_t ddp_dissector_table;
84 /* P = Padding, H = Hops, L = Len */
85 #if BYTE_ORDER == BIG_ENDIAN
86 /* PPHHHHLL LLLLLLLL */
87 # define ddp_hops(x) ( ( x >> 10) & 0x3C )
88 # define ddp_len(x) ( x & 0x03ff )
90 /* LLLLLLLL PPHHHHLL*/
91 # define ddp_hops(x) ( x & 0x3C )
92 # define ddp_len(x) ( ntohs(x) & 0x03ff )
94 typedef struct _e_ddp {
95 guint16 hops_len; /* combines pad, hops, and len */
96 guint16 sum,dnet,snet;
102 #define DDP_HEADER_SIZE 13
105 atalk_addr_to_str(const struct atalk_ddp_addr *addrp)
107 static gchar str[3][14];
110 if (cur == &str[0][0]) {
112 } else if (cur == &str[1][0]) {
118 sprintf(cur, "%u.%u:%u", addrp->net, addrp->node, addrp->port);
122 static const value_string op_vals[] = {
123 {DDP_RTMPDATA, "AppleTalk Routing Table response or data" },
124 {DDP_NBP, "AppleTalk Name Binding Protocol packet"},
125 {DDP_ATP, "AppleTalk Transaction Protocol packet"},
126 {DDP_AEP, "AppleTalk Echo Protocol packet"},
127 {DDP_RTMPREQ, "AppleTalk Routing Table request"},
128 {DDP_ZIP, "AppleTalk Zone Information Protocol packet"},
129 {DDP_ADSP, "AppleTalk Data Stream Protocol"},
130 {DDP_EIGRP, "Cisco EIGRP for AppleTalk"},
135 #define NBP_FORWARD 4
138 static const value_string nbp_op_vals[] = {
139 {NBP_LOOKUP, "lookup"},
140 {NBP_FORWARD, "forward request"},
141 {NBP_REPLY, "reply"},
145 int dissect_pascal_string(const u_char *pd, int offset, frame_data *fd,
146 proto_tree *tree, int hf_index)
150 if ( ! BYTES_ARE_IN_FRAME(offset,1) ) {
151 dissect_data(pd,offset,fd,tree);
156 if ( ! BYTES_ARE_IN_FRAME(offset,len) ) {
157 dissect_data(pd,offset,fd,tree);
168 tmp = g_malloc( len+1 );
169 memcpy(tmp, &pd[offset], len);
171 item = proto_tree_add_item(tree, hf_index, NullTVB, offset-1, len+1, tmp);
173 subtree = proto_item_add_subtree(item, ett_pstring);
174 proto_tree_add_text(subtree, NullTVB, offset-1, 1, "Length: %d", len);
175 proto_tree_add_text(subtree, NullTVB, offset, len, "Data: %s", tmp);
185 dissect_rtmp_request(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
186 dissect_data(pd, offset, fd, tree);
191 dissect_rtmp_data(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
192 proto_tree *rtmp_tree;
195 guint8 nodelen,nodelen_bits;
196 guint16 node; /* might be more than 8 bits */
199 if (!BYTES_ARE_IN_FRAME(offset, 3)) {
200 dissect_data(pd, offset, fd, tree);
204 net = pntohs(&pd[offset]);
205 nodelen_bits = pd[offset+2];
206 if ( nodelen_bits <= 8 ) {
210 node = pntohs(&pd[offset]);
214 if (check_col(fd, COL_PROTOCOL))
215 col_add_str(fd, COL_PROTOCOL, "RTMP");
217 if (check_col(fd, COL_INFO))
218 col_add_fstr(fd, COL_INFO, "Net: %d Node Len: %d Node: %d",
219 net, nodelen_bits, node);
222 ti = proto_tree_add_item(tree, proto_rtmp, NullTVB, offset, END_OF_FRAME, NULL);
223 rtmp_tree = proto_item_add_subtree(ti, ett_rtmp);
225 proto_tree_add_item(rtmp_tree, hf_rtmp_net, NullTVB, offset, 2, net);
226 proto_tree_add_item(rtmp_tree, hf_rtmp_node_len, NullTVB, offset+2, 1, nodelen_bits);
227 proto_tree_add_item(rtmp_tree, hf_rtmp_node, NullTVB, offset+3, nodelen, nodelen);
228 offset += 3 + nodelen;
231 while ( BYTES_ARE_IN_FRAME(offset, 1) )
233 proto_tree *tuple_item, *tuple_tree;
234 guint16 tuple_net, tuple_net2;
235 guint8 tuple_dist, tuple_dist2;
237 if ( ! BYTES_ARE_IN_FRAME(offset, 3) )
239 dissect_data(pd,offset,fd,rtmp_tree);
243 tuple_net = pntohs(&pd[offset]);
244 tuple_dist = pd[offset+2];
246 tuple_item = proto_tree_add_text(rtmp_tree, NullTVB, offset, 3,
247 "Tuple %d: Net: %d Dist: %d",
248 i, tuple_net, tuple_dist);
249 tuple_tree = proto_item_add_subtree(tuple_item, ett_rtmp_tuple);
251 proto_tree_add_item(tuple_tree, hf_rtmp_tuple_net, NullTVB, offset, 2,
253 proto_tree_add_item(tuple_tree, hf_rtmp_tuple_dist, NullTVB, offset+2, 1,
256 if ( tuple_dist == 0 || tuple_dist & 0x80 ) /* phase 1/2 */
258 if ( ! BYTES_ARE_IN_FRAME(offset+3, 3) )
260 dissect_data(pd,offset,fd,rtmp_tree);
264 tuple_net2 = pntohs(&pd[offset+3]);
265 tuple_dist2 = pd[offset+5];
267 proto_tree_add_item(tuple_tree, hf_rtmp_tuple_net, NullTVB, offset, 2,
269 proto_tree_add_item(tuple_tree, hf_rtmp_tuple_dist, NullTVB, offset+2, 1,
272 proto_item_set_len(tuple_item, 6);
275 else /* screwy gatorbox/etc. */
288 dissect_nbp(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
289 proto_tree *nbp_tree;
290 proto_tree *nbp_info_tree;
291 proto_item *ti, *info_item;
295 if (!BYTES_ARE_IN_FRAME(offset, 2)) {
296 dissect_data(pd, offset, fd, tree);
300 op = pd[offset] >> 4;
301 count = pd[offset] & 0x0F;
303 if (check_col(fd, COL_PROTOCOL))
304 col_add_str(fd, COL_PROTOCOL, "NBP");
306 if (check_col(fd, COL_INFO))
307 col_add_fstr(fd, COL_INFO, "Op: %s Count: %d",
308 val_to_str(op, nbp_op_vals, "unknown (%1x)"), count);
311 ti = proto_tree_add_item(tree, proto_nbp, NullTVB, offset, END_OF_FRAME, NULL);
312 nbp_tree = proto_item_add_subtree(ti, ett_nbp);
314 info_item = proto_tree_add_uint_format(nbp_tree, hf_nbp_info, NullTVB, offset, 1,
316 "Info: 0x%01X Operation: %s Count: %d", pd[offset],
317 val_to_str(op, nbp_op_vals, "unknown"),
319 nbp_info_tree = proto_item_add_subtree(info_item, ett_nbp_info);
320 proto_tree_add_item(nbp_info_tree, hf_nbp_op, NullTVB, offset, 1, pd[offset]);
321 proto_tree_add_item(nbp_info_tree, hf_nbp_count, NullTVB, offset, 1, pd[offset]);
322 proto_tree_add_item(nbp_tree, hf_nbp_tid, NullTVB, offset+1, 1, pd[offset+1]);
325 for (i=0; i<count; i++) {
326 struct atalk_ddp_addr addr;
327 proto_tree *node_item,*node_tree;
328 int soffset = offset;
330 if ( !BYTES_ARE_IN_FRAME(offset, 6) ) {
331 dissect_data(pd,offset,fd,nbp_tree);
335 node_item = proto_tree_add_text(nbp_tree, NullTVB, offset, 4,
337 node_tree = proto_item_add_subtree(node_item, ett_nbp_node);
339 addr.net = pntohs(&pd[offset]);
340 addr.node = pd[offset+2];
341 addr.port = pd[offset+3];
343 /* note, this is probably wrong, I need to look at my info at work
344 tomorrow to straighten it out */
346 proto_tree_add_item(node_tree, hf_nbp_node_net, NullTVB, offset, 2, addr.net);
348 proto_tree_add_item(node_tree, hf_nbp_node_node, NullTVB, offset, 1, addr.node);
350 proto_tree_add_item(node_tree, hf_nbp_node_port, NullTVB, offset, 1, addr.port);
352 proto_tree_add_item(node_tree, hf_nbp_node_enum, NullTVB, offset, 1, pd[offset]);
355 offset = dissect_pascal_string(pd,offset,fd,node_tree,hf_nbp_node_object);
356 offset = dissect_pascal_string(pd,offset,fd,node_tree,hf_nbp_node_type);
357 offset = dissect_pascal_string(pd,offset,fd,node_tree,hf_nbp_node_zone);
359 proto_item_set_len(node_item, offset-soffset);
367 dissect_ddp(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
369 proto_tree *ddp_tree;
371 static struct atalk_ddp_addr src, dst;
373 if (!BYTES_ARE_IN_FRAME(offset, DDP_HEADER_SIZE)) {
374 dissect_data(pd, offset, fd, tree);
378 memcpy(&ddp, &pd[offset], sizeof(e_ddp));
379 ddp.dnet=ntohs(ddp.dnet);
380 ddp.snet=ntohs(ddp.snet);
381 ddp.sum=ntohs(ddp.sum);
384 src.node = ddp.snode;
385 src.port = ddp.sport;
387 dst.node = ddp.dnode;
388 dst.port = ddp.dport;
389 SET_ADDRESS(&pi.net_src, AT_ATALK, sizeof src, (guint8 *)&src);
390 SET_ADDRESS(&pi.src, AT_ATALK, sizeof src, (guint8 *)&src);
391 SET_ADDRESS(&pi.net_dst, AT_ATALK, sizeof dst, (guint8 *)&dst);
392 SET_ADDRESS(&pi.dst, AT_ATALK, sizeof dst, (guint8 *)&dst);
394 if (check_col(fd, COL_PROTOCOL))
395 col_add_str(fd, COL_PROTOCOL, "DDP");
396 if (check_col(fd, COL_INFO))
397 col_add_str(fd, COL_INFO,
398 val_to_str(ddp.type, op_vals, "Unknown DDP protocol (%02x)"));
401 ti = proto_tree_add_item(tree, proto_ddp, NullTVB, offset, DDP_HEADER_SIZE, NULL);
402 ddp_tree = proto_item_add_subtree(ti, ett_ddp);
403 proto_tree_add_item(ddp_tree, hf_ddp_hopcount, NullTVB, offset, 1,
404 ddp_hops(ddp.hops_len));
405 proto_tree_add_item(ddp_tree, hf_ddp_len, NullTVB, offset, 2,
406 ddp_len(ddp.hops_len));
407 proto_tree_add_item(ddp_tree, hf_ddp_checksum, NullTVB, offset + 2, 2, ddp.sum);
408 proto_tree_add_item(ddp_tree, hf_ddp_dst_net, NullTVB, offset + 4, 2, ddp.dnet);
409 proto_tree_add_item(ddp_tree, hf_ddp_src_net, NullTVB, offset + 6, 2, ddp.snet);
410 proto_tree_add_item(ddp_tree, hf_ddp_dst_node, NullTVB, offset + 8, 1, ddp.dnode);
411 proto_tree_add_item(ddp_tree, hf_ddp_src_node, NullTVB, offset + 9, 1, ddp.snode);
412 proto_tree_add_item(ddp_tree, hf_ddp_dst_socket, NullTVB, offset + 10, 1, ddp.dport);
413 proto_tree_add_item(ddp_tree, hf_ddp_src_socket, NullTVB, offset + 11, 1, ddp.sport);
414 proto_tree_add_item(ddp_tree, hf_ddp_type, NullTVB, offset + 12, 1, ddp.type);
417 offset += DDP_HEADER_SIZE;
419 if (!dissector_try_port(ddp_dissector_table, ddp.type, pd, offset, fd, tree))
420 dissect_data(pd, offset, fd, tree);
424 proto_register_atalk(void)
426 static hf_register_info hf_ddp[] = {
428 { "Hop count", "ddp.hopcount", FT_UINT8, BASE_DEC, NULL, 0x0,
432 { "Datagram length", "ddp.len", FT_UINT16, BASE_DEC, NULL, 0x0,
436 { "Checksum", "ddp.checksum", FT_UINT16, BASE_DEC, NULL, 0x0,
440 { "Destination Net", "ddp.dst.net", FT_UINT16, BASE_DEC, NULL, 0x0,
444 { "Source Net", "ddp.src.net", FT_UINT16, BASE_DEC, NULL, 0x0,
448 { "Destination Node", "ddp.dst.node", FT_UINT8, BASE_DEC, NULL, 0x0,
452 { "Source Node", "ddp.src.node", FT_UINT8, BASE_DEC, NULL, 0x0,
455 { &hf_ddp_dst_socket,
456 { "Destination Socket", "ddp.dst.socket", FT_UINT8, BASE_DEC, NULL, 0x0,
459 { &hf_ddp_src_socket,
460 { "Source Socket", "ddp.src.socket", FT_UINT8, BASE_DEC, NULL, 0x0,
464 { "Protocol type", "ddp.type", FT_UINT8, BASE_DEC, VALS(op_vals), 0x0,
468 static hf_register_info hf_nbp[] = {
470 { "Operation", "nbp.op", FT_UINT8, BASE_DEC,
471 VALS(nbp_op_vals), 0xF0, "Operation" }},
473 { "Info", "nbp.info", FT_UINT8, BASE_HEX,
474 NULL, 0x0, "Info" }},
476 { "Count", "nbp.count", FT_UINT8, BASE_DEC,
477 NULL, 0x0F, "Count" }},
479 { "Network", "nbp.net", FT_UINT16, BASE_DEC,
480 NULL, 0x0, "Network" }},
482 { "Node", "nbp.node", FT_UINT8, BASE_DEC,
483 NULL, 0x0, "Node" }},
485 { "Port", "nbp.port", FT_UINT8, BASE_DEC,
486 NULL, 0x0, "Port" }},
488 { "Enumerator", "nbp.enum", FT_UINT8, BASE_DEC,
489 NULL, 0x0, "Enumerator" }},
490 { &hf_nbp_node_object,
491 { "Object", "nbp.object", FT_STRING, BASE_DEC,
492 NULL, 0x0, "Object" }},
494 { "Type", "nbp.type", FT_STRING, BASE_DEC,
495 NULL, 0x0, "Type" }},
497 { "Zone", "nbp.zone", FT_STRING, BASE_DEC,
498 NULL, 0x0, "Zone" }},
500 { "Transaction ID", "nbp.tid", FT_UINT8, BASE_DEC,
501 NULL, 0x0, "Transaction ID" }}
504 static hf_register_info hf_rtmp[] = {
506 { "Net", "rtmp.net", FT_UINT16, BASE_DEC,
509 { "Node", "nbp.nodeid", FT_UINT8, BASE_DEC,
510 NULL, 0x0, "Node" }},
512 { "Node Length", "nbp.nodeid.length", FT_UINT8, BASE_DEC,
513 NULL, 0x0, "Node Length" }},
514 { &hf_rtmp_tuple_net,
515 { "Net", "rtmp.tuple.net", FT_UINT16, BASE_DEC,
517 { &hf_rtmp_tuple_dist,
518 { "Distance", "rtmp.tuple.dist", FT_UINT16, BASE_DEC,
519 NULL, 0x0, "Distance" }}
523 static gint *ett[] = {
533 proto_ddp = proto_register_protocol("Datagram Delivery Protocol", "ddp");
534 proto_register_field_array(proto_ddp, hf_ddp, array_length(hf_ddp));
536 proto_nbp = proto_register_protocol("Name Binding Protocol", "nbp");
537 proto_register_field_array(proto_nbp, hf_nbp, array_length(hf_nbp));
539 proto_rtmp = proto_register_protocol("Routing Table", "rtmp");
540 proto_register_field_array(proto_rtmp, hf_rtmp, array_length(hf_rtmp));
542 proto_register_subtree_array(ett, array_length(ett));
544 /* subdissector code */
545 ddp_dissector_table = register_dissector_table("ddp.type");
549 proto_reg_handoff_atalk(void)
551 dissector_add("ethertype", ETHERTYPE_ATALK, dissect_ddp);
552 dissector_add("ppp.protocol", PPP_AT, dissect_ddp);
553 dissector_add("ddp.type", DDP_NBP, dissect_nbp);
554 dissector_add("ddp.type", DDP_RTMPREQ, dissect_rtmp_request);
555 dissector_add("ddp.type", DDP_RTMPDATA, dissect_rtmp_data);