blob: 9d7d69d3d790189f01ca5d430980b98d021d2ac4 [file] [log] [blame]
The Android Open Source Project2949f582009-03-03 19:30:46 -08001/*
2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
16 * written permission.
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
The Android Open Source Project2949f582009-03-03 19:30:46 -080020 */
21
Elliott Hughese2e3bd12017-05-15 10:59:29 -070022/* \summary: AppleTalk printer */
23
The Android Open Source Project2949f582009-03-03 19:30:46 -080024#ifdef HAVE_CONFIG_H
25#include "config.h"
26#endif
27
Elliott Hughese2e3bd12017-05-15 10:59:29 -070028#include <netdissect-stdinc.h>
The Android Open Source Project2949f582009-03-03 19:30:46 -080029
30#include <stdio.h>
The Android Open Source Project2949f582009-03-03 19:30:46 -080031#include <string.h>
The Android Open Source Project2949f582009-03-03 19:30:46 -080032
Elliott Hughese2e3bd12017-05-15 10:59:29 -070033#include "netdissect.h"
The Android Open Source Project2949f582009-03-03 19:30:46 -080034#include "addrtoname.h"
35#include "ethertype.h"
Elliott Hughese2e3bd12017-05-15 10:59:29 -070036#include "extract.h"
The Android Open Source Project2949f582009-03-03 19:30:46 -080037#include "appletalk.h"
38
Elliott Hughes892a68b2015-10-19 14:43:53 -070039static const char tstr[] = "[|atalk]";
40
JP Abgrall53f17a92014-02-12 14:02:41 -080041static const struct tok type2str[] = {
The Android Open Source Project2949f582009-03-03 19:30:46 -080042 { ddpRTMP, "rtmp" },
43 { ddpRTMPrequest, "rtmpReq" },
44 { ddpECHO, "echo" },
45 { ddpIP, "IP" },
46 { ddpARP, "ARP" },
47 { ddpKLAP, "KLAP" },
48 { 0, NULL }
49};
50
51struct aarp {
Elliott Hughes892a68b2015-10-19 14:43:53 -070052 uint16_t htype, ptype;
53 uint8_t halen, palen;
54 uint16_t op;
55 uint8_t hsaddr[6];
56 uint8_t psaddr[4];
57 uint8_t hdaddr[6];
58 uint8_t pdaddr[4];
The Android Open Source Project2949f582009-03-03 19:30:46 -080059};
60
Elliott Hughes892a68b2015-10-19 14:43:53 -070061static void atp_print(netdissect_options *, const struct atATP *, u_int);
62static void atp_bitmap_print(netdissect_options *, u_char);
63static void nbp_print(netdissect_options *, const struct atNBP *, u_int, u_short, u_char, u_char);
64static const struct atNBPtuple *nbp_tuple_print(netdissect_options *ndo, const struct atNBPtuple *,
The Android Open Source Project2949f582009-03-03 19:30:46 -080065 const u_char *,
66 u_short, u_char, u_char);
Elliott Hughes892a68b2015-10-19 14:43:53 -070067static const struct atNBPtuple *nbp_name_print(netdissect_options *, const struct atNBPtuple *,
The Android Open Source Project2949f582009-03-03 19:30:46 -080068 const u_char *);
Elliott Hughes892a68b2015-10-19 14:43:53 -070069static const char *ataddr_string(netdissect_options *, u_short, u_char);
70static void ddp_print(netdissect_options *, const u_char *, u_int, int, u_short, u_char, u_char);
71static const char *ddpskt_string(netdissect_options *, int);
The Android Open Source Project2949f582009-03-03 19:30:46 -080072
73/*
74 * Print LLAP packets received on a physical LocalTalk interface.
75 */
76u_int
Elliott Hughes892a68b2015-10-19 14:43:53 -070077ltalk_if_print(netdissect_options *ndo,
78 const struct pcap_pkthdr *h, const u_char *p)
The Android Open Source Project2949f582009-03-03 19:30:46 -080079{
Elliott Hughese2e3bd12017-05-15 10:59:29 -070080 u_int hdrlen;
81
82 hdrlen = llap_print(ndo, p, h->len);
83 if (hdrlen == 0) {
84 /* Cut short by the snapshot length. */
85 return (h->caplen);
86 }
87 return (hdrlen);
The Android Open Source Project2949f582009-03-03 19:30:46 -080088}
89
90/*
91 * Print AppleTalk LLAP packets.
92 */
93u_int
Elliott Hughes892a68b2015-10-19 14:43:53 -070094llap_print(netdissect_options *ndo,
95 register const u_char *bp, u_int length)
The Android Open Source Project2949f582009-03-03 19:30:46 -080096{
97 register const struct LAP *lp;
98 register const struct atDDP *dp;
99 register const struct atShortDDP *sdp;
100 u_short snet;
101 u_int hdrlen;
102
JP Abgrall53f17a92014-02-12 14:02:41 -0800103 if (length < sizeof(*lp)) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700104 ND_PRINT((ndo, " [|llap %u]", length));
JP Abgrall53f17a92014-02-12 14:02:41 -0800105 return (length);
106 }
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700107 if (!ND_TTEST2(*bp, sizeof(*lp))) {
108 ND_PRINT((ndo, " [|llap]"));
109 return (0); /* cut short by the snapshot length */
110 }
The Android Open Source Project2949f582009-03-03 19:30:46 -0800111 lp = (const struct LAP *)bp;
112 bp += sizeof(*lp);
113 length -= sizeof(*lp);
114 hdrlen = sizeof(*lp);
115 switch (lp->type) {
116
117 case lapShortDDP:
118 if (length < ddpSSize) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700119 ND_PRINT((ndo, " [|sddp %u]", length));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800120 return (length);
121 }
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700122 if (!ND_TTEST2(*bp, ddpSSize)) {
123 ND_PRINT((ndo, " [|sddp]"));
124 return (0); /* cut short by the snapshot length */
125 }
The Android Open Source Project2949f582009-03-03 19:30:46 -0800126 sdp = (const struct atShortDDP *)bp;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700127 ND_PRINT((ndo, "%s.%s",
128 ataddr_string(ndo, 0, lp->src), ddpskt_string(ndo, sdp->srcSkt)));
129 ND_PRINT((ndo, " > %s.%s:",
130 ataddr_string(ndo, 0, lp->dst), ddpskt_string(ndo, sdp->dstSkt)));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800131 bp += ddpSSize;
132 length -= ddpSSize;
133 hdrlen += ddpSSize;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700134 ddp_print(ndo, bp, length, sdp->type, 0, lp->src, sdp->srcSkt);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800135 break;
136
137 case lapDDP:
138 if (length < ddpSize) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700139 ND_PRINT((ndo, " [|ddp %u]", length));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800140 return (length);
141 }
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700142 if (!ND_TTEST2(*bp, ddpSize)) {
143 ND_PRINT((ndo, " [|ddp]"));
144 return (0); /* cut short by the snapshot length */
145 }
The Android Open Source Project2949f582009-03-03 19:30:46 -0800146 dp = (const struct atDDP *)bp;
147 snet = EXTRACT_16BITS(&dp->srcNet);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700148 ND_PRINT((ndo, "%s.%s", ataddr_string(ndo, snet, dp->srcNode),
149 ddpskt_string(ndo, dp->srcSkt)));
150 ND_PRINT((ndo, " > %s.%s:",
151 ataddr_string(ndo, EXTRACT_16BITS(&dp->dstNet), dp->dstNode),
152 ddpskt_string(ndo, dp->dstSkt)));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800153 bp += ddpSize;
154 length -= ddpSize;
155 hdrlen += ddpSize;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700156 ddp_print(ndo, bp, length, dp->type, snet, dp->srcNode, dp->srcSkt);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800157 break;
158
159#ifdef notdef
160 case lapKLAP:
161 klap_print(bp, length);
162 break;
163#endif
164
165 default:
Elliott Hughes892a68b2015-10-19 14:43:53 -0700166 ND_PRINT((ndo, "%d > %d at-lap#%d %u",
167 lp->src, lp->dst, lp->type, length));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800168 break;
169 }
170 return (hdrlen);
171}
172
173/*
174 * Print EtherTalk/TokenTalk packets (or FDDITalk, or whatever it's called
175 * when it runs over FDDI; yes, I've seen FDDI captures with AppleTalk
176 * packets in them).
177 */
178void
Elliott Hughes892a68b2015-10-19 14:43:53 -0700179atalk_print(netdissect_options *ndo,
180 register const u_char *bp, u_int length)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800181{
182 register const struct atDDP *dp;
183 u_short snet;
184
Elliott Hughes892a68b2015-10-19 14:43:53 -0700185 if(!ndo->ndo_eflag)
186 ND_PRINT((ndo, "AT "));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800187
188 if (length < ddpSize) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700189 ND_PRINT((ndo, " [|ddp %u]", length));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800190 return;
191 }
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700192 if (!ND_TTEST2(*bp, ddpSize)) {
193 ND_PRINT((ndo, " [|ddp]"));
194 return;
195 }
The Android Open Source Project2949f582009-03-03 19:30:46 -0800196 dp = (const struct atDDP *)bp;
197 snet = EXTRACT_16BITS(&dp->srcNet);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700198 ND_PRINT((ndo, "%s.%s", ataddr_string(ndo, snet, dp->srcNode),
199 ddpskt_string(ndo, dp->srcSkt)));
200 ND_PRINT((ndo, " > %s.%s: ",
201 ataddr_string(ndo, EXTRACT_16BITS(&dp->dstNet), dp->dstNode),
202 ddpskt_string(ndo, dp->dstSkt)));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800203 bp += ddpSize;
204 length -= ddpSize;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700205 ddp_print(ndo, bp, length, dp->type, snet, dp->srcNode, dp->srcSkt);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800206}
207
208/* XXX should probably pass in the snap header and do checks like arp_print() */
209void
Elliott Hughes892a68b2015-10-19 14:43:53 -0700210aarp_print(netdissect_options *ndo,
211 register const u_char *bp, u_int length)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800212{
213 register const struct aarp *ap;
214
Elliott Hughes892a68b2015-10-19 14:43:53 -0700215#define AT(member) ataddr_string(ndo, (ap->member[1]<<8)|ap->member[2],ap->member[3])
The Android Open Source Project2949f582009-03-03 19:30:46 -0800216
Elliott Hughes892a68b2015-10-19 14:43:53 -0700217 ND_PRINT((ndo, "aarp "));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800218 ap = (const struct aarp *)bp;
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700219 if (!ND_TTEST(*ap)) {
220 /* Just bail if we don't have the whole chunk. */
221 ND_PRINT((ndo, " [|aarp]"));
222 return;
223 }
224 if (length < sizeof(*ap)) {
225 ND_PRINT((ndo, " [|aarp %u]", length));
226 return;
227 }
The Android Open Source Project2949f582009-03-03 19:30:46 -0800228 if (EXTRACT_16BITS(&ap->htype) == 1 &&
229 EXTRACT_16BITS(&ap->ptype) == ETHERTYPE_ATALK &&
230 ap->halen == 6 && ap->palen == 4 )
231 switch (EXTRACT_16BITS(&ap->op)) {
232
233 case 1: /* request */
Elliott Hughes892a68b2015-10-19 14:43:53 -0700234 ND_PRINT((ndo, "who-has %s tell %s", AT(pdaddr), AT(psaddr)));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800235 return;
236
237 case 2: /* response */
Elliott Hughes892a68b2015-10-19 14:43:53 -0700238 ND_PRINT((ndo, "reply %s is-at %s", AT(psaddr), etheraddr_string(ndo, ap->hsaddr)));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800239 return;
240
241 case 3: /* probe (oy!) */
Elliott Hughes892a68b2015-10-19 14:43:53 -0700242 ND_PRINT((ndo, "probe %s tell %s", AT(pdaddr), AT(psaddr)));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800243 return;
244 }
Elliott Hughes892a68b2015-10-19 14:43:53 -0700245 ND_PRINT((ndo, "len %u op %u htype %u ptype %#x halen %u palen %u",
The Android Open Source Project2949f582009-03-03 19:30:46 -0800246 length, EXTRACT_16BITS(&ap->op), EXTRACT_16BITS(&ap->htype),
Elliott Hughes892a68b2015-10-19 14:43:53 -0700247 EXTRACT_16BITS(&ap->ptype), ap->halen, ap->palen));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800248}
249
250/*
251 * Print AppleTalk Datagram Delivery Protocol packets.
252 */
253static void
Elliott Hughes892a68b2015-10-19 14:43:53 -0700254ddp_print(netdissect_options *ndo,
255 register const u_char *bp, register u_int length, register int t,
256 register u_short snet, register u_char snode, u_char skt)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800257{
258
259 switch (t) {
260
261 case ddpNBP:
Elliott Hughes892a68b2015-10-19 14:43:53 -0700262 nbp_print(ndo, (const struct atNBP *)bp, length, snet, snode, skt);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800263 break;
264
265 case ddpATP:
Elliott Hughes892a68b2015-10-19 14:43:53 -0700266 atp_print(ndo, (const struct atATP *)bp, length);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800267 break;
268
269 case ddpEIGRP:
Elliott Hughes892a68b2015-10-19 14:43:53 -0700270 eigrp_print(ndo, bp, length);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800271 break;
272
273 default:
Elliott Hughes892a68b2015-10-19 14:43:53 -0700274 ND_PRINT((ndo, " at-%s %d", tok2str(type2str, NULL, t), length));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800275 break;
276 }
277}
278
279static void
Elliott Hughes892a68b2015-10-19 14:43:53 -0700280atp_print(netdissect_options *ndo,
281 register const struct atATP *ap, u_int length)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800282{
283 char c;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700284 uint32_t data;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800285
Elliott Hughes892a68b2015-10-19 14:43:53 -0700286 if ((const u_char *)(ap + 1) > ndo->ndo_snapend) {
The Android Open Source Project2949f582009-03-03 19:30:46 -0800287 /* Just bail if we don't have the whole chunk. */
Elliott Hughes892a68b2015-10-19 14:43:53 -0700288 ND_PRINT((ndo, "%s", tstr));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800289 return;
290 }
JP Abgrall53f17a92014-02-12 14:02:41 -0800291 if (length < sizeof(*ap)) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700292 ND_PRINT((ndo, " [|atp %u]", length));
JP Abgrall53f17a92014-02-12 14:02:41 -0800293 return;
294 }
The Android Open Source Project2949f582009-03-03 19:30:46 -0800295 length -= sizeof(*ap);
296 switch (ap->control & 0xc0) {
297
298 case atpReqCode:
Elliott Hughes892a68b2015-10-19 14:43:53 -0700299 ND_PRINT((ndo, " atp-req%s %d",
The Android Open Source Project2949f582009-03-03 19:30:46 -0800300 ap->control & atpXO? " " : "*",
Elliott Hughes892a68b2015-10-19 14:43:53 -0700301 EXTRACT_16BITS(&ap->transID)));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800302
Elliott Hughes892a68b2015-10-19 14:43:53 -0700303 atp_bitmap_print(ndo, ap->bitmap);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800304
305 if (length != 0)
Elliott Hughes892a68b2015-10-19 14:43:53 -0700306 ND_PRINT((ndo, " [len=%u]", length));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800307
308 switch (ap->control & (atpEOM|atpSTS)) {
309 case atpEOM:
Elliott Hughes892a68b2015-10-19 14:43:53 -0700310 ND_PRINT((ndo, " [EOM]"));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800311 break;
312 case atpSTS:
Elliott Hughes892a68b2015-10-19 14:43:53 -0700313 ND_PRINT((ndo, " [STS]"));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800314 break;
315 case atpEOM|atpSTS:
Elliott Hughes892a68b2015-10-19 14:43:53 -0700316 ND_PRINT((ndo, " [EOM,STS]"));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800317 break;
318 }
319 break;
320
321 case atpRspCode:
Elliott Hughes892a68b2015-10-19 14:43:53 -0700322 ND_PRINT((ndo, " atp-resp%s%d:%d (%u)",
The Android Open Source Project2949f582009-03-03 19:30:46 -0800323 ap->control & atpEOM? "*" : " ",
Elliott Hughes892a68b2015-10-19 14:43:53 -0700324 EXTRACT_16BITS(&ap->transID), ap->bitmap, length));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800325 switch (ap->control & (atpXO|atpSTS)) {
326 case atpXO:
Elliott Hughes892a68b2015-10-19 14:43:53 -0700327 ND_PRINT((ndo, " [XO]"));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800328 break;
329 case atpSTS:
Elliott Hughes892a68b2015-10-19 14:43:53 -0700330 ND_PRINT((ndo, " [STS]"));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800331 break;
332 case atpXO|atpSTS:
Elliott Hughes892a68b2015-10-19 14:43:53 -0700333 ND_PRINT((ndo, " [XO,STS]"));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800334 break;
335 }
336 break;
337
338 case atpRelCode:
Elliott Hughes892a68b2015-10-19 14:43:53 -0700339 ND_PRINT((ndo, " atp-rel %d", EXTRACT_16BITS(&ap->transID)));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800340
Elliott Hughes892a68b2015-10-19 14:43:53 -0700341 atp_bitmap_print(ndo, ap->bitmap);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800342
343 /* length should be zero */
344 if (length)
Elliott Hughes892a68b2015-10-19 14:43:53 -0700345 ND_PRINT((ndo, " [len=%u]", length));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800346
347 /* there shouldn't be any control flags */
348 if (ap->control & (atpXO|atpEOM|atpSTS)) {
349 c = '[';
350 if (ap->control & atpXO) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700351 ND_PRINT((ndo, "%cXO", c));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800352 c = ',';
353 }
354 if (ap->control & atpEOM) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700355 ND_PRINT((ndo, "%cEOM", c));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800356 c = ',';
357 }
358 if (ap->control & atpSTS) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700359 ND_PRINT((ndo, "%cSTS", c));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800360 c = ',';
361 }
Elliott Hughes892a68b2015-10-19 14:43:53 -0700362 ND_PRINT((ndo, "]"));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800363 }
364 break;
365
366 default:
Elliott Hughes892a68b2015-10-19 14:43:53 -0700367 ND_PRINT((ndo, " atp-0x%x %d (%u)", ap->control,
368 EXTRACT_16BITS(&ap->transID), length));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800369 break;
370 }
371 data = EXTRACT_32BITS(&ap->userData);
372 if (data != 0)
Elliott Hughes892a68b2015-10-19 14:43:53 -0700373 ND_PRINT((ndo, " 0x%x", data));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800374}
375
376static void
Elliott Hughes892a68b2015-10-19 14:43:53 -0700377atp_bitmap_print(netdissect_options *ndo,
378 register u_char bm)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800379{
380 register char c;
381 register int i;
382
383 /*
384 * The '& 0xff' below is needed for compilers that want to sign
385 * extend a u_char, which is the case with the Ultrix compiler.
386 * (gcc is smart enough to eliminate it, at least on the Sparc).
387 */
388 if ((bm + 1) & (bm & 0xff)) {
389 c = '<';
390 for (i = 0; bm; ++i) {
391 if (bm & 1) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700392 ND_PRINT((ndo, "%c%d", c, i));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800393 c = ',';
394 }
395 bm >>= 1;
396 }
Elliott Hughes892a68b2015-10-19 14:43:53 -0700397 ND_PRINT((ndo, ">"));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800398 } else {
399 for (i = 0; bm; ++i)
400 bm >>= 1;
401 if (i > 1)
Elliott Hughes892a68b2015-10-19 14:43:53 -0700402 ND_PRINT((ndo, "<0-%d>", i - 1));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800403 else
Elliott Hughes892a68b2015-10-19 14:43:53 -0700404 ND_PRINT((ndo, "<0>"));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800405 }
406}
407
408static void
Elliott Hughes892a68b2015-10-19 14:43:53 -0700409nbp_print(netdissect_options *ndo,
410 register const struct atNBP *np, u_int length, register u_short snet,
411 register u_char snode, register u_char skt)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800412{
413 register const struct atNBPtuple *tp =
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700414 (const struct atNBPtuple *)((const u_char *)np + nbpHeaderSize);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800415 int i;
416 const u_char *ep;
417
418 if (length < nbpHeaderSize) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700419 ND_PRINT((ndo, " truncated-nbp %u", length));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800420 return;
421 }
422
423 length -= nbpHeaderSize;
424 if (length < 8) {
425 /* must be room for at least one tuple */
Elliott Hughes892a68b2015-10-19 14:43:53 -0700426 ND_PRINT((ndo, " truncated-nbp %u", length + nbpHeaderSize));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800427 return;
428 }
429 /* ep points to end of available data */
Elliott Hughes892a68b2015-10-19 14:43:53 -0700430 ep = ndo->ndo_snapend;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800431 if ((const u_char *)tp > ep) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700432 ND_PRINT((ndo, "%s", tstr));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800433 return;
434 }
435 switch (i = np->control & 0xf0) {
436
437 case nbpBrRq:
438 case nbpLkUp:
Elliott Hughes892a68b2015-10-19 14:43:53 -0700439 ND_PRINT((ndo, i == nbpLkUp? " nbp-lkup %d:":" nbp-brRq %d:", np->id));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800440 if ((const u_char *)(tp + 1) > ep) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700441 ND_PRINT((ndo, "%s", tstr));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800442 return;
443 }
Elliott Hughes892a68b2015-10-19 14:43:53 -0700444 (void)nbp_name_print(ndo, tp, ep);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800445 /*
446 * look for anomalies: the spec says there can only
447 * be one tuple, the address must match the source
448 * address and the enumerator should be zero.
449 */
450 if ((np->control & 0xf) != 1)
Elliott Hughes892a68b2015-10-19 14:43:53 -0700451 ND_PRINT((ndo, " [ntup=%d]", np->control & 0xf));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800452 if (tp->enumerator)
Elliott Hughes892a68b2015-10-19 14:43:53 -0700453 ND_PRINT((ndo, " [enum=%d]", tp->enumerator));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800454 if (EXTRACT_16BITS(&tp->net) != snet ||
455 tp->node != snode || tp->skt != skt)
Elliott Hughes892a68b2015-10-19 14:43:53 -0700456 ND_PRINT((ndo, " [addr=%s.%d]",
457 ataddr_string(ndo, EXTRACT_16BITS(&tp->net),
458 tp->node), tp->skt));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800459 break;
460
461 case nbpLkUpReply:
Elliott Hughes892a68b2015-10-19 14:43:53 -0700462 ND_PRINT((ndo, " nbp-reply %d:", np->id));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800463
464 /* print each of the tuples in the reply */
465 for (i = np->control & 0xf; --i >= 0 && tp; )
Elliott Hughes892a68b2015-10-19 14:43:53 -0700466 tp = nbp_tuple_print(ndo, tp, ep, snet, snode, skt);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800467 break;
468
469 default:
Elliott Hughes892a68b2015-10-19 14:43:53 -0700470 ND_PRINT((ndo, " nbp-0x%x %d (%u)", np->control, np->id, length));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800471 break;
472 }
473}
474
475/* print a counted string */
476static const char *
Elliott Hughes892a68b2015-10-19 14:43:53 -0700477print_cstring(netdissect_options *ndo,
478 register const char *cp, register const u_char *ep)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800479{
480 register u_int length;
481
482 if (cp >= (const char *)ep) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700483 ND_PRINT((ndo, "%s", tstr));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800484 return (0);
485 }
486 length = *cp++;
487
488 /* Spec says string can be at most 32 bytes long */
489 if (length > 32) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700490 ND_PRINT((ndo, "[len=%u]", length));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800491 return (0);
492 }
493 while ((int)--length >= 0) {
494 if (cp >= (const char *)ep) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700495 ND_PRINT((ndo, "%s", tstr));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800496 return (0);
497 }
Elliott Hughes892a68b2015-10-19 14:43:53 -0700498 ND_PRINT((ndo, "%c", *cp++));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800499 }
500 return (cp);
501}
502
503static const struct atNBPtuple *
Elliott Hughes892a68b2015-10-19 14:43:53 -0700504nbp_tuple_print(netdissect_options *ndo,
505 register const struct atNBPtuple *tp, register const u_char *ep,
506 register u_short snet, register u_char snode, register u_char skt)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800507{
508 register const struct atNBPtuple *tpn;
509
510 if ((const u_char *)(tp + 1) > ep) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700511 ND_PRINT((ndo, "%s", tstr));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800512 return 0;
513 }
Elliott Hughes892a68b2015-10-19 14:43:53 -0700514 tpn = nbp_name_print(ndo, tp, ep);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800515
516 /* if the enumerator isn't 1, print it */
517 if (tp->enumerator != 1)
Elliott Hughes892a68b2015-10-19 14:43:53 -0700518 ND_PRINT((ndo, "(%d)", tp->enumerator));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800519
520 /* if the socket doesn't match the src socket, print it */
521 if (tp->skt != skt)
Elliott Hughes892a68b2015-10-19 14:43:53 -0700522 ND_PRINT((ndo, " %d", tp->skt));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800523
524 /* if the address doesn't match the src address, it's an anomaly */
525 if (EXTRACT_16BITS(&tp->net) != snet || tp->node != snode)
Elliott Hughes892a68b2015-10-19 14:43:53 -0700526 ND_PRINT((ndo, " [addr=%s]",
527 ataddr_string(ndo, EXTRACT_16BITS(&tp->net), tp->node)));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800528
529 return (tpn);
530}
531
532static const struct atNBPtuple *
Elliott Hughes892a68b2015-10-19 14:43:53 -0700533nbp_name_print(netdissect_options *ndo,
534 const struct atNBPtuple *tp, register const u_char *ep)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800535{
536 register const char *cp = (const char *)tp + nbpTupleSize;
537
Elliott Hughes892a68b2015-10-19 14:43:53 -0700538 ND_PRINT((ndo, " "));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800539
540 /* Object */
Elliott Hughes892a68b2015-10-19 14:43:53 -0700541 ND_PRINT((ndo, "\""));
542 if ((cp = print_cstring(ndo, cp, ep)) != NULL) {
The Android Open Source Project2949f582009-03-03 19:30:46 -0800543 /* Type */
Elliott Hughes892a68b2015-10-19 14:43:53 -0700544 ND_PRINT((ndo, ":"));
545 if ((cp = print_cstring(ndo, cp, ep)) != NULL) {
The Android Open Source Project2949f582009-03-03 19:30:46 -0800546 /* Zone */
Elliott Hughes892a68b2015-10-19 14:43:53 -0700547 ND_PRINT((ndo, "@"));
548 if ((cp = print_cstring(ndo, cp, ep)) != NULL)
549 ND_PRINT((ndo, "\""));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800550 }
551 }
552 return ((const struct atNBPtuple *)cp);
553}
554
555
556#define HASHNAMESIZE 4096
557
558struct hnamemem {
559 int addr;
560 char *name;
561 struct hnamemem *nxt;
562};
563
564static struct hnamemem hnametable[HASHNAMESIZE];
565
566static const char *
Elliott Hughes892a68b2015-10-19 14:43:53 -0700567ataddr_string(netdissect_options *ndo,
568 u_short atnet, u_char athost)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800569{
570 register struct hnamemem *tp, *tp2;
571 register int i = (atnet << 8) | athost;
JP Abgrall53f17a92014-02-12 14:02:41 -0800572 char nambuf[256+1];
The Android Open Source Project2949f582009-03-03 19:30:46 -0800573 static int first = 1;
574 FILE *fp;
575
576 /*
577 * if this is the first call, see if there's an AppleTalk
578 * number to name map file.
579 */
Elliott Hughes892a68b2015-10-19 14:43:53 -0700580 if (first && (first = 0, !ndo->ndo_nflag)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800581 && (fp = fopen("/etc/atalk.names", "r"))) {
582 char line[256];
JP Abgrall53f17a92014-02-12 14:02:41 -0800583 int i1, i2;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800584
585 while (fgets(line, sizeof(line), fp)) {
586 if (line[0] == '\n' || line[0] == 0 || line[0] == '#')
587 continue;
JP Abgrall53f17a92014-02-12 14:02:41 -0800588 if (sscanf(line, "%d.%d %256s", &i1, &i2, nambuf) == 3)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800589 /* got a hostname. */
JP Abgrall53f17a92014-02-12 14:02:41 -0800590 i2 |= (i1 << 8);
591 else if (sscanf(line, "%d %256s", &i1, nambuf) == 2)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800592 /* got a net name */
JP Abgrall53f17a92014-02-12 14:02:41 -0800593 i2 = (i1 << 8) | 255;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800594 else
595 continue;
596
JP Abgrall53f17a92014-02-12 14:02:41 -0800597 for (tp = &hnametable[i2 & (HASHNAMESIZE-1)];
The Android Open Source Project2949f582009-03-03 19:30:46 -0800598 tp->nxt; tp = tp->nxt)
599 ;
JP Abgrall53f17a92014-02-12 14:02:41 -0800600 tp->addr = i2;
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700601 tp->nxt = newhnamemem(ndo);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800602 tp->name = strdup(nambuf);
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700603 if (tp->name == NULL)
604 (*ndo->ndo_error)(ndo,
605 "ataddr_string: strdup(nambuf)");
The Android Open Source Project2949f582009-03-03 19:30:46 -0800606 }
607 fclose(fp);
608 }
609
610 for (tp = &hnametable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
611 if (tp->addr == i)
612 return (tp->name);
613
614 /* didn't have the node name -- see if we've got the net name */
615 i |= 255;
616 for (tp2 = &hnametable[i & (HASHNAMESIZE-1)]; tp2->nxt; tp2 = tp2->nxt)
617 if (tp2->addr == i) {
618 tp->addr = (atnet << 8) | athost;
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700619 tp->nxt = newhnamemem(ndo);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800620 (void)snprintf(nambuf, sizeof(nambuf), "%s.%d",
621 tp2->name, athost);
622 tp->name = strdup(nambuf);
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700623 if (tp->name == NULL)
624 (*ndo->ndo_error)(ndo,
625 "ataddr_string: strdup(nambuf)");
The Android Open Source Project2949f582009-03-03 19:30:46 -0800626 return (tp->name);
627 }
628
629 tp->addr = (atnet << 8) | athost;
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700630 tp->nxt = newhnamemem(ndo);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800631 if (athost != 255)
JP Abgrall53f17a92014-02-12 14:02:41 -0800632 (void)snprintf(nambuf, sizeof(nambuf), "%d.%d", atnet, athost);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800633 else
JP Abgrall53f17a92014-02-12 14:02:41 -0800634 (void)snprintf(nambuf, sizeof(nambuf), "%d", atnet);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800635 tp->name = strdup(nambuf);
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700636 if (tp->name == NULL)
637 (*ndo->ndo_error)(ndo, "ataddr_string: strdup(nambuf)");
The Android Open Source Project2949f582009-03-03 19:30:46 -0800638
639 return (tp->name);
640}
641
JP Abgrall53f17a92014-02-12 14:02:41 -0800642static const struct tok skt2str[] = {
The Android Open Source Project2949f582009-03-03 19:30:46 -0800643 { rtmpSkt, "rtmp" }, /* routing table maintenance */
644 { nbpSkt, "nis" }, /* name info socket */
645 { echoSkt, "echo" }, /* AppleTalk echo protocol */
646 { zipSkt, "zip" }, /* zone info protocol */
647 { 0, NULL }
648};
649
650static const char *
Elliott Hughes892a68b2015-10-19 14:43:53 -0700651ddpskt_string(netdissect_options *ndo,
652 register int skt)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800653{
654 static char buf[8];
655
Elliott Hughes892a68b2015-10-19 14:43:53 -0700656 if (ndo->ndo_nflag) {
The Android Open Source Project2949f582009-03-03 19:30:46 -0800657 (void)snprintf(buf, sizeof(buf), "%d", skt);
658 return (buf);
659 }
660 return (tok2str(skt2str, "%d", skt));
661}