2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
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
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.
21 * Format and print AppleTalk packets.
25 static const char rcsid[] =
26 "@(#) $Header: print-atalk.c,v 1.48 97/05/28 12:50:58 leres Exp $ (LBL)";
29 #include <sys/param.h>
31 #include <sys/socket.h>
39 #include <netinet/in.h>
40 #include <netinet/in_systm.h>
41 #include <netinet/ip.h>
42 #include <netinet/ip_var.h>
43 #include <net/ethernet.h>
44 #include <netinet/udp.h>
45 #include <netinet/udp_var.h>
46 #include <netinet/tcp.h>
47 #include <netinet/tcpip.h>
53 #include "interface.h"
54 #include "addrtoname.h"
55 #include "ethertype.h"
56 #include "extract.h" /* must come after interface.h */
57 #include "appletalk.h"
60 static struct tok type2str[] = {
62 { ddpRTMPrequest, "rtmpReq" },
80 static char tstr[] = "[|atalk]";
82 static void atp_print(const struct atATP *, u_int);
83 static void atp_bitmap_print(u_char);
84 static void nbp_print(const struct atNBP *, u_int, u_short, u_char, u_char);
85 static const char *print_cstring(const char *, const u_char *);
86 static const struct atNBPtuple *nbp_tuple_print(const struct atNBPtuple *,
88 u_short, u_char, u_char);
89 static const struct atNBPtuple *nbp_name_print(const struct atNBPtuple *,
91 static const char *ataddr_string(u_short, u_char);
92 static void ddp_print(const u_char *, u_int, int, u_short, u_char, u_char);
93 static const char *ddpskt_string(int);
96 * Print AppleTalk Datagram Delivery Protocol packets.
99 atalk_print(register const u_char *bp, u_int length)
101 register const struct LAP *lp;
102 register const struct atDDP *dp;
103 register const struct atShortDDP *sdp;
107 lp = (struct LAP *)bp;
109 length -= sizeof(*lp);
112 static struct LAP lp_ = {0, 0, lapDDP};
119 if (length < ddpSSize) {
120 (void)printf(" [|sddp %d]", length);
123 sdp = (const struct atShortDDP *)bp;
125 ataddr_string(0, lp->src), ddpskt_string(sdp->srcSkt));
127 ataddr_string(0, lp->dst), ddpskt_string(sdp->dstSkt));
130 ddp_print(bp, length, sdp->type, 0, lp->src, sdp->srcSkt);
134 if (length < ddpSize) {
135 (void)printf(" [|ddp %d]", length);
138 dp = (const struct atDDP *)bp;
139 snet = EXTRACT_16BITS(&dp->srcNet);
140 printf("%s.%s", ataddr_string(snet, dp->srcNode),
141 ddpskt_string(dp->srcSkt));
143 ataddr_string(EXTRACT_16BITS(&dp->dstNet), dp->dstNode),
144 ddpskt_string(dp->dstSkt));
147 ddp_print(bp, length, dp->type, snet, dp->srcNode, dp->srcSkt);
152 klap_print(bp, length);
157 printf("%d > %d at-lap#%d %d",
158 lp->src, lp->dst, lp->type, length);
163 /* XXX should probably pass in the snap header and do checks like arp_print() */
165 aarp_print(register const u_char *bp, u_int length)
167 register const struct aarp *ap;
169 #define AT(member) ataddr_string((ap->member[1]<<8)|ap->member[2],ap->member[3])
172 ap = (const struct aarp *)bp;
173 if (ntohs(ap->htype) == 1 && ntohs(ap->ptype) == ETHERTYPE_ATALK &&
174 ap->halen == 6 && ap->palen == 4 )
175 switch (ntohs(ap->op)) {
177 case 1: /* request */
178 (void)printf("who-has %s tell %s",
179 AT(pdaddr), AT(psaddr));
182 case 2: /* response */
183 (void)printf("reply %s is-at %s",
184 AT(psaddr), etheraddr_string(ap->hsaddr));
187 case 3: /* probe (oy!) */
188 (void)printf("probe %s tell %s",
189 AT(pdaddr), AT(psaddr));
192 (void)printf("len %d op %d htype %d ptype %#x halen %d palen %d",
193 length, ap->op, ap->htype, ap->ptype, ap->halen, ap->palen );
197 ddp_print(register const u_char *bp, register u_int length, register int t,
198 register u_short snet, register u_char snode, u_char skt)
204 nbp_print((const struct atNBP *)bp, length, snet, snode, skt);
208 atp_print((const struct atATP *)bp, length);
212 (void)printf(" at-%s %d", tok2str(type2str, NULL, t), length);
218 atp_print(register const struct atATP *ap, u_int length)
223 if ((const u_char *)(ap + 1) > snapend) {
224 /* Just bail if we don't have the whole chunk. */
228 length -= sizeof(*ap);
229 switch (ap->control & 0xc0) {
232 (void)printf(" atp-req%s %d",
233 ap->control & atpXO? " " : "*",
234 EXTRACT_16BITS(&ap->transID));
236 atp_bitmap_print(ap->bitmap);
239 (void)printf(" [len=%d]", length);
241 switch (ap->control & (atpEOM|atpSTS)) {
243 (void)printf(" [EOM]");
246 (void)printf(" [STS]");
249 (void)printf(" [EOM,STS]");
255 (void)printf(" atp-resp%s%d:%d (%d)",
256 ap->control & atpEOM? "*" : " ",
257 EXTRACT_16BITS(&ap->transID), ap->bitmap, length);
258 switch (ap->control & (atpXO|atpSTS)) {
260 (void)printf(" [XO]");
263 (void)printf(" [STS]");
266 (void)printf(" [XO,STS]");
272 (void)printf(" atp-rel %d", EXTRACT_16BITS(&ap->transID));
274 atp_bitmap_print(ap->bitmap);
276 /* length should be zero */
278 (void)printf(" [len=%d]", length);
280 /* there shouldn't be any control flags */
281 if (ap->control & (atpXO|atpEOM|atpSTS)) {
283 if (ap->control & atpXO) {
284 (void)printf("%cXO", c);
287 if (ap->control & atpEOM) {
288 (void)printf("%cEOM", c);
291 if (ap->control & atpSTS) {
292 (void)printf("%cSTS", c);
300 (void)printf(" atp-0x%x %d (%d)", ap->control,
301 EXTRACT_16BITS(&ap->transID), length);
304 data = EXTRACT_32BITS(&ap->userData);
306 (void)printf(" 0x%x", data);
310 atp_bitmap_print(register u_char bm)
316 * The '& 0xff' below is needed for compilers that want to sign
317 * extend a u_char, which is the case with the Ultrix compiler.
318 * (gcc is smart enough to eliminate it, at least on the Sparc).
320 if ((bm + 1) & (bm & 0xff)) {
322 for (i = 0; bm; ++i) {
324 (void)printf("%c%d", c, i);
334 (void)printf("<0-%d>", i - 1);
341 nbp_print(register const struct atNBP *np, u_int length, register u_short snet,
342 register u_char snode, register u_char skt)
344 register const struct atNBPtuple *tp =
345 (struct atNBPtuple *)((u_char *)np + nbpHeaderSize);
349 length -= nbpHeaderSize;
351 /* must be room for at least one tuple */
352 (void)printf(" truncated-nbp %d", length + nbpHeaderSize);
355 /* ep points to end of available data */
357 if ((const u_char *)tp > ep) {
361 switch (i = np->control & 0xf0) {
365 (void)printf(i == nbpLkUp? " nbp-lkup %d:":" nbp-brRq %d:",
367 if ((const u_char *)(tp + 1) > ep) {
371 (void)nbp_name_print(tp, ep);
373 * look for anomalies: the spec says there can only
374 * be one tuple, the address must match the source
375 * address and the enumerator should be zero.
377 if ((np->control & 0xf) != 1)
378 (void)printf(" [ntup=%d]", np->control & 0xf);
380 (void)printf(" [enum=%d]", tp->enumerator);
381 if (EXTRACT_16BITS(&tp->net) != snet ||
382 tp->node != snode || tp->skt != skt)
383 (void)printf(" [addr=%s.%d]",
384 ataddr_string(EXTRACT_16BITS(&tp->net),
389 (void)printf(" nbp-reply %d:", np->id);
391 /* print each of the tuples in the reply */
392 for (i = np->control & 0xf; --i >= 0 && tp; )
393 tp = nbp_tuple_print(tp, ep, snet, snode, skt);
397 (void)printf(" nbp-0x%x %d (%d)", np->control, np->id,
403 /* print a counted string */
405 print_cstring(register const char *cp, register const u_char *ep)
407 register u_int length;
409 if (cp >= (const char *)ep) {
415 /* Spec says string can be at most 32 bytes long */
417 (void)printf("[len=%u]", length);
420 while ((int)--length >= 0) {
421 if (cp >= (char *)ep) {
430 static const struct atNBPtuple *
431 nbp_tuple_print(register const struct atNBPtuple *tp,
432 register const u_char *ep,
433 register u_short snet, register u_char snode,
436 register const struct atNBPtuple *tpn;
438 if ((const u_char *)(tp + 1) > ep) {
442 tpn = nbp_name_print(tp, ep);
444 /* if the enumerator isn't 1, print it */
445 if (tp->enumerator != 1)
446 (void)printf("(%d)", tp->enumerator);
448 /* if the socket doesn't match the src socket, print it */
450 (void)printf(" %d", tp->skt);
452 /* if the address doesn't match the src address, it's an anomaly */
453 if (EXTRACT_16BITS(&tp->net) != snet || tp->node != snode)
454 (void)printf(" [addr=%s]",
455 ataddr_string(EXTRACT_16BITS(&tp->net), tp->node));
460 static const struct atNBPtuple *
461 nbp_name_print(const struct atNBPtuple *tp, register const u_char *ep)
463 register const char *cp = (const char *)tp + nbpTupleSize;
469 if ((cp = print_cstring(cp, ep)) != NULL) {
472 if ((cp = print_cstring(cp, ep)) != NULL) {
475 if ((cp = print_cstring(cp, ep)) != NULL)
479 return ((const struct atNBPtuple *)cp);
483 #define HASHNAMESIZE 4096
488 struct hnamemem *nxt;
491 static struct hnamemem hnametable[HASHNAMESIZE];
494 ataddr_string(u_short atnet, u_char athost)
496 register struct hnamemem *tp, *tp2;
497 register int i = (atnet << 8) | athost;
499 static int first = 1;
503 * if this is the first call, see if there's an AppleTalk
504 * number to name map file.
506 if (first && (first = 0, !nflag)
507 && (fp = fopen("/etc/atalk.names", "r"))) {
511 while (fgets(line, sizeof(line), fp)) {
512 if (line[0] == '\n' || line[0] == 0 || line[0] == '#')
514 if (sscanf(line, "%d.%d %s", &i1, &i2, nambuf) == 3)
515 /* got a hostname. */
517 else if (sscanf(line, "%d %s", &i1, nambuf) == 2)
519 i2 = (i1 << 8) | 255;
523 for (tp = &hnametable[i2 & (HASHNAMESIZE-1)];
524 tp->nxt; tp = tp->nxt)
527 tp->nxt = newhnamemem();
528 tp->name = savestr(nambuf);
533 for (tp = &hnametable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
537 /* didn't have the node name -- see if we've got the net name */
539 for (tp2 = &hnametable[i & (HASHNAMESIZE-1)]; tp2->nxt; tp2 = tp2->nxt)
540 if (tp2->addr == i) {
541 tp->addr = (atnet << 8) | athost;
542 tp->nxt = newhnamemem();
543 (void)sprintf(nambuf, "%s.%d", tp2->name, athost);
544 tp->name = savestr(nambuf);
548 tp->addr = (atnet << 8) | athost;
549 tp->nxt = newhnamemem();
551 (void)sprintf(nambuf, "%d.%d", atnet, athost);
553 (void)sprintf(nambuf, "%d", atnet);
554 tp->name = savestr(nambuf);
559 static struct tok skt2str[] = {
560 { rtmpSkt, "rtmp" }, /* routing table maintenance */
561 { nbpSkt, "nis" }, /* name info socket */
562 { echoSkt, "echo" }, /* AppleTalk echo protocol */
563 { zipSkt, "zip" }, /* zone info protocol */
568 ddpskt_string(register int skt)
573 (void)sprintf(buf, "%d", skt);
576 return (tok2str(skt2str, "%d", skt));