]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/tcpdump/print-atalk.c
This commit was generated by cvs2svn to compensate for changes in r126209,
[FreeBSD/FreeBSD.git] / contrib / tcpdump / print-atalk.c
1 /*
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.
20  *
21  * Format and print AppleTalk packets.
22  *
23  * $FreeBSD$
24  */
25
26 #ifndef lint
27 static const char rcsid[] =
28     "@(#) $Header: /tcpdump/master/tcpdump/print-atalk.c,v 1.70.2.1 2002/02/05 10:04:18 guy Exp $ (LBL)";
29 #endif
30
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
34
35 #include <sys/param.h>
36 #include <sys/time.h>
37 #include <sys/socket.h>
38
39 #include <netinet/in.h>
40
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <netdb.h>              /* for MAXHOSTNAMELEN on some platforms */
45 #include <pcap.h>
46
47 #include "interface.h"
48 #include "addrtoname.h"
49 #include "ethertype.h"
50 #include "extract.h"                    /* must come after interface.h */
51 #include "appletalk.h"
52
53 static struct tok type2str[] = {
54         { ddpRTMP,              "rtmp" },
55         { ddpRTMPrequest,       "rtmpReq" },
56         { ddpECHO,              "echo" },
57         { ddpIP,                "IP" },
58         { ddpARP,               "ARP" },
59         { ddpKLAP,              "KLAP" },
60         { 0,                    NULL }
61 };
62
63 struct aarp {
64         u_int16_t       htype, ptype;
65         u_int8_t        halen, palen;
66         u_int16_t       op;
67         u_int8_t        hsaddr[6];
68         u_int8_t        psaddr[4];
69         u_int8_t        hdaddr[6];
70         u_int8_t        pdaddr[4];
71 };
72
73 static char tstr[] = "[|atalk]";
74
75 static void atp_print(const struct atATP *, u_int);
76 static void atp_bitmap_print(u_char);
77 static void nbp_print(const struct atNBP *, u_int, u_short, u_char, u_char);
78 static const char *print_cstring(const char *, const u_char *);
79 static const struct atNBPtuple *nbp_tuple_print(const struct atNBPtuple *,
80                                                 const u_char *,
81                                                 u_short, u_char, u_char);
82 static const struct atNBPtuple *nbp_name_print(const struct atNBPtuple *,
83                                                const u_char *);
84 static const char *ataddr_string(u_short, u_char);
85 static void ddp_print(const u_char *, u_int, int, u_short, u_char, u_char);
86 static const char *ddpskt_string(int);
87
88 /*
89  * Print LLAP packets received on a physical LocalTalk interface.
90  */
91 void
92 ltalk_if_print(u_char *user, const struct pcap_pkthdr *h, const u_char *p)
93 {
94         snapend = p + h->caplen;
95         ++infodelay;
96         ts_print(&h->ts);
97         llap_print(p, h->caplen);
98         if(xflag)
99                 default_print(p, h->caplen);
100         putchar('\n');
101         --infodelay;
102         if (infoprint)
103                 info(0);
104 }
105
106 /*
107  * Print AppleTalk LLAP packets.
108  */
109 void
110 llap_print(register const u_char *bp, u_int length)
111 {
112         register const struct LAP *lp;
113         register const struct atDDP *dp;
114         register const struct atShortDDP *sdp;
115         u_short snet;
116
117 #if 0
118         /*
119          * Our packet is on a 4-byte boundary, as we're either called
120          * directly from a top-level link-layer printer (ltalk_if_print)
121          * or from the UDP printer.  The LLAP+DDP header is a multiple
122          * of 4 bytes in length, so the DDP payload is also on a 4-byte
123          * boundary, and we don't need to align it before calling
124          * "ddp_print()".
125          */
126         lp = (const struct LAP *)bp;
127         bp += sizeof(*lp);
128         length -= sizeof(*lp);
129 #else
130         {
131                 static struct LAP lp_ = {0, 0, lapDDP};
132                 lp = &lp_;
133         }
134 #endif
135         switch (lp->type) {
136
137         case lapShortDDP:
138                 if (length < ddpSSize) {
139                         (void)printf(" [|sddp %d]", length);
140                         return;
141                 }
142                 sdp = (const struct atShortDDP *)bp;
143                 printf("%s.%s",
144                     ataddr_string(0, lp->src), ddpskt_string(sdp->srcSkt));
145                 printf(" > %s.%s:",
146                     ataddr_string(0, lp->dst), ddpskt_string(sdp->dstSkt));
147                 bp += ddpSSize;
148                 length -= ddpSSize;
149                 ddp_print(bp, length, sdp->type, 0, lp->src, sdp->srcSkt);
150                 break;
151
152         case lapDDP:
153                 if (length < ddpSize) {
154                         (void)printf(" [|ddp %d]", length);
155                         return;
156                 }
157                 dp = (const struct atDDP *)bp;
158                 snet = EXTRACT_16BITS(&dp->srcNet);
159                 printf("%s.%s", ataddr_string(snet, dp->srcNode),
160                     ddpskt_string(dp->srcSkt));
161                 printf(" > %s.%s:",
162                     ataddr_string(EXTRACT_16BITS(&dp->dstNet), dp->dstNode),
163                     ddpskt_string(dp->dstSkt));
164                 bp += ddpSize;
165                 length -= ddpSize;
166                 ddp_print(bp, length, dp->type, snet, dp->srcNode, dp->srcSkt);
167                 break;
168
169 #ifdef notdef
170         case lapKLAP:
171                 klap_print(bp, length);
172                 break;
173 #endif
174
175         default:
176                 printf("%d > %d at-lap#%d %d",
177                     lp->src, lp->dst, lp->type, length);
178                 break;
179         }
180 }
181
182 /*
183  * Print EtherTalk/TokenTalk packets (or FDDITalk, or whatever it's called
184  * when it runs over FDDI; yes, I've seen FDDI captures with AppleTalk
185  * packets in them).
186  */
187 void
188 atalk_print(register const u_char *bp, u_int length)
189 {
190         register const struct atDDP *dp;
191         u_short snet;
192
193         if (length < ddpSize) {
194                 (void)printf(" [|ddp %d]", length);
195                 return;
196         }
197         dp = (const struct atDDP *)bp;
198         snet = EXTRACT_16BITS(&dp->srcNet);
199         printf("%s.%s", ataddr_string(snet, dp->srcNode),
200                ddpskt_string(dp->srcSkt));
201         printf(" > %s.%s:",
202                ataddr_string(EXTRACT_16BITS(&dp->dstNet), dp->dstNode),
203                ddpskt_string(dp->dstSkt));
204         bp += ddpSize;
205         length -= ddpSize;
206 #ifdef LBL_ALIGN
207         if ((long)bp & 3) {
208                 static u_char *abuf = NULL;
209
210                 if (abuf == NULL) {
211                         abuf = (u_char *)malloc(snaplen);
212                         if (abuf == NULL)
213                                 error("atalk_print: malloc");
214                 }
215                 memcpy((char *)abuf, (char *)bp, min(length, snaplen));
216                 snapend += abuf - (u_char *)bp;
217                 packetp = abuf;
218                 bp = abuf;
219         }
220 #endif
221         ddp_print(bp, length, dp->type, snet, dp->srcNode, dp->srcSkt);
222 }
223
224 /* XXX should probably pass in the snap header and do checks like arp_print() */
225 void
226 aarp_print(register const u_char *bp, u_int length)
227 {
228         register const struct aarp *ap;
229
230 #define AT(member) ataddr_string((ap->member[1]<<8)|ap->member[2],ap->member[3])
231
232         printf("aarp ");
233         ap = (const struct aarp *)bp;
234         if (ntohs(ap->htype) == 1 && ntohs(ap->ptype) == ETHERTYPE_ATALK &&
235             ap->halen == 6 && ap->palen == 4 )
236                 switch (ntohs(ap->op)) {
237
238                 case 1:                         /* request */
239                         (void)printf("who-has %s tell %s",
240                             AT(pdaddr), AT(psaddr));
241                         return;
242
243                 case 2:                         /* response */
244                         (void)printf("reply %s is-at %s",
245                             AT(psaddr), etheraddr_string(ap->hsaddr));
246                         return;
247
248                 case 3:                         /* probe (oy!) */
249                         (void)printf("probe %s tell %s",
250                             AT(pdaddr), AT(psaddr));
251                         return;
252                 }
253         (void)printf("len %u op %u htype %u ptype %#x halen %u palen %u",
254             length, ntohs(ap->op), ntohs(ap->htype), ntohs(ap->ptype),
255             ap->halen, ap->palen);
256 }
257
258 /*
259  * Print AppleTalk Datagram Delivery Protocol packets.
260  */
261 static void
262 ddp_print(register const u_char *bp, register u_int length, register int t,
263           register u_short snet, register u_char snode, u_char skt)
264 {
265
266         switch (t) {
267
268         case ddpNBP:
269                 nbp_print((const struct atNBP *)bp, length, snet, snode, skt);
270                 break;
271
272         case ddpATP:
273                 atp_print((const struct atATP *)bp, length);
274                 break;
275
276         default:
277                 (void)printf(" at-%s %d", tok2str(type2str, NULL, t), length);
278                 break;
279         }
280 }
281
282 static void
283 atp_print(register const struct atATP *ap, u_int length)
284 {
285         char c;
286         u_int32_t data;
287
288         if ((const u_char *)(ap + 1) > snapend) {
289                 /* Just bail if we don't have the whole chunk. */
290                 fputs(tstr, stdout);
291                 return;
292         }
293         length -= sizeof(*ap);
294         switch (ap->control & 0xc0) {
295
296         case atpReqCode:
297                 (void)printf(" atp-req%s %d",
298                              ap->control & atpXO? " " : "*",
299                              EXTRACT_16BITS(&ap->transID));
300
301                 atp_bitmap_print(ap->bitmap);
302
303                 if (length != 0)
304                         (void)printf(" [len=%d]", length);
305
306                 switch (ap->control & (atpEOM|atpSTS)) {
307                 case atpEOM:
308                         (void)printf(" [EOM]");
309                         break;
310                 case atpSTS:
311                         (void)printf(" [STS]");
312                         break;
313                 case atpEOM|atpSTS:
314                         (void)printf(" [EOM,STS]");
315                         break;
316                 }
317                 break;
318
319         case atpRspCode:
320                 (void)printf(" atp-resp%s%d:%d (%d)",
321                              ap->control & atpEOM? "*" : " ",
322                              EXTRACT_16BITS(&ap->transID), ap->bitmap, length);
323                 switch (ap->control & (atpXO|atpSTS)) {
324                 case atpXO:
325                         (void)printf(" [XO]");
326                         break;
327                 case atpSTS:
328                         (void)printf(" [STS]");
329                         break;
330                 case atpXO|atpSTS:
331                         (void)printf(" [XO,STS]");
332                         break;
333                 }
334                 break;
335
336         case atpRelCode:
337                 (void)printf(" atp-rel  %d", EXTRACT_16BITS(&ap->transID));
338
339                 atp_bitmap_print(ap->bitmap);
340
341                 /* length should be zero */
342                 if (length)
343                         (void)printf(" [len=%d]", length);
344
345                 /* there shouldn't be any control flags */
346                 if (ap->control & (atpXO|atpEOM|atpSTS)) {
347                         c = '[';
348                         if (ap->control & atpXO) {
349                                 (void)printf("%cXO", c);
350                                 c = ',';
351                         }
352                         if (ap->control & atpEOM) {
353                                 (void)printf("%cEOM", c);
354                                 c = ',';
355                         }
356                         if (ap->control & atpSTS) {
357                                 (void)printf("%cSTS", c);
358                                 c = ',';
359                         }
360                         (void)printf("]");
361                 }
362                 break;
363
364         default:
365                 (void)printf(" atp-0x%x  %d (%d)", ap->control,
366                              EXTRACT_16BITS(&ap->transID), length);
367                 break;
368         }
369         data = EXTRACT_32BITS(&ap->userData);
370         if (data != 0)
371                 (void)printf(" 0x%x", data);
372 }
373
374 static void
375 atp_bitmap_print(register u_char bm)
376 {
377         register char c;
378         register int i;
379
380         /*
381          * The '& 0xff' below is needed for compilers that want to sign
382          * extend a u_char, which is the case with the Ultrix compiler.
383          * (gcc is smart enough to eliminate it, at least on the Sparc).
384          */
385         if ((bm + 1) & (bm & 0xff)) {
386                 c = '<';
387                 for (i = 0; bm; ++i) {
388                         if (bm & 1) {
389                                 (void)printf("%c%d", c, i);
390                                 c = ',';
391                         }
392                         bm >>= 1;
393                 }
394                 (void)printf(">");
395         } else {
396                 for (i = 0; bm; ++i)
397                         bm >>= 1;
398                 if (i > 1)
399                         (void)printf("<0-%d>", i - 1);
400                 else
401                         (void)printf("<0>");
402         }
403 }
404
405 static void
406 nbp_print(register const struct atNBP *np, u_int length, register u_short snet,
407           register u_char snode, register u_char skt)
408 {
409         register const struct atNBPtuple *tp =
410                 (const struct atNBPtuple *)((u_char *)np + nbpHeaderSize);
411         int i;
412         const u_char *ep;
413
414         if (length < nbpHeaderSize) {
415                 (void)printf(" truncated-nbp %d", length);
416                 return;
417         }
418
419         length -= nbpHeaderSize;
420         if (length < 8) {
421                 /* must be room for at least one tuple */
422                 (void)printf(" truncated-nbp %d", length + nbpHeaderSize);
423                 return;
424         }
425         /* ep points to end of available data */
426         ep = snapend;
427         if ((const u_char *)tp > ep) {
428                 fputs(tstr, stdout);
429                 return;
430         }
431         switch (i = np->control & 0xf0) {
432
433         case nbpBrRq:
434         case nbpLkUp:
435                 (void)printf(i == nbpLkUp? " nbp-lkup %d:":" nbp-brRq %d:",
436                              np->id);
437                 if ((const u_char *)(tp + 1) > ep) {
438                         fputs(tstr, stdout);
439                         return;
440                 }
441                 (void)nbp_name_print(tp, ep);
442                 /*
443                  * look for anomalies: the spec says there can only
444                  * be one tuple, the address must match the source
445                  * address and the enumerator should be zero.
446                  */
447                 if ((np->control & 0xf) != 1)
448                         (void)printf(" [ntup=%d]", np->control & 0xf);
449                 if (tp->enumerator)
450                         (void)printf(" [enum=%d]", tp->enumerator);
451                 if (EXTRACT_16BITS(&tp->net) != snet ||
452                     tp->node != snode || tp->skt != skt)
453                         (void)printf(" [addr=%s.%d]",
454                             ataddr_string(EXTRACT_16BITS(&tp->net),
455                             tp->node), tp->skt);
456                 break;
457
458         case nbpLkUpReply:
459                 (void)printf(" nbp-reply %d:", np->id);
460
461                 /* print each of the tuples in the reply */
462                 for (i = np->control & 0xf; --i >= 0 && tp; )
463                         tp = nbp_tuple_print(tp, ep, snet, snode, skt);
464                 break;
465
466         default:
467                 (void)printf(" nbp-0x%x  %d (%d)", np->control, np->id,
468                                 length);
469                 break;
470         }
471 }
472
473 /* print a counted string */
474 static const char *
475 print_cstring(register const char *cp, register const u_char *ep)
476 {
477         register u_int length;
478
479         if (cp >= (const char *)ep) {
480                 fputs(tstr, stdout);
481                 return (0);
482         }
483         length = *cp++;
484
485         /* Spec says string can be at most 32 bytes long */
486         if (length > 32) {
487                 (void)printf("[len=%u]", length);
488                 return (0);
489         }
490         while ((int)--length >= 0) {
491                 if (cp >= (const char *)ep) {
492                         fputs(tstr, stdout);
493                         return (0);
494                 }
495                 putchar(*cp++);
496         }
497         return (cp);
498 }
499
500 static const struct atNBPtuple *
501 nbp_tuple_print(register const struct atNBPtuple *tp,
502                 register const u_char *ep,
503                 register u_short snet, register u_char snode,
504                 register u_char skt)
505 {
506         register const struct atNBPtuple *tpn;
507
508         if ((const u_char *)(tp + 1) > ep) {
509                 fputs(tstr, stdout);
510                 return 0;
511         }
512         tpn = nbp_name_print(tp, ep);
513
514         /* if the enumerator isn't 1, print it */
515         if (tp->enumerator != 1)
516                 (void)printf("(%d)", tp->enumerator);
517
518         /* if the socket doesn't match the src socket, print it */
519         if (tp->skt != skt)
520                 (void)printf(" %d", tp->skt);
521
522         /* if the address doesn't match the src address, it's an anomaly */
523         if (EXTRACT_16BITS(&tp->net) != snet || tp->node != snode)
524                 (void)printf(" [addr=%s]",
525                     ataddr_string(EXTRACT_16BITS(&tp->net), tp->node));
526
527         return (tpn);
528 }
529
530 static const struct atNBPtuple *
531 nbp_name_print(const struct atNBPtuple *tp, register const u_char *ep)
532 {
533         register const char *cp = (const char *)tp + nbpTupleSize;
534
535         putchar(' ');
536
537         /* Object */
538         putchar('"');
539         if ((cp = print_cstring(cp, ep)) != NULL) {
540                 /* Type */
541                 putchar(':');
542                 if ((cp = print_cstring(cp, ep)) != NULL) {
543                         /* Zone */
544                         putchar('@');
545                         if ((cp = print_cstring(cp, ep)) != NULL)
546                                 putchar('"');
547                 }
548         }
549         return ((const struct atNBPtuple *)cp);
550 }
551
552
553 #define HASHNAMESIZE 4096
554
555 struct hnamemem {
556         int addr;
557         char *name;
558         struct hnamemem *nxt;
559 };
560
561 static struct hnamemem hnametable[HASHNAMESIZE];
562
563 static const char *
564 ataddr_string(u_short atnet, u_char athost)
565 {
566         register struct hnamemem *tp, *tp2;
567         register int i = (atnet << 8) | athost;
568         char nambuf[MAXHOSTNAMELEN + 20];
569         static int first = 1;
570         FILE *fp;
571
572         /*
573          * if this is the first call, see if there's an AppleTalk
574          * number to name map file.
575          */
576         if (first && (first = 0, !nflag)
577             && (fp = fopen("/etc/atalk.names", "r"))) {
578                 char line[256];
579                 int i1, i2, i3;
580
581                 while (fgets(line, sizeof(line), fp)) {
582                         if (line[0] == '\n' || line[0] == 0 || line[0] == '#')
583                                 continue;
584                         if (sscanf(line, "%d.%d.%d %256s", &i1, &i2, &i3,
585                                      nambuf) == 4)
586                                 /* got a hostname. */
587                                 i3 |= ((i1 << 8) | i2) << 8;
588                         else if (sscanf(line, "%d.%d %256s", &i1, &i2,
589                                         nambuf) == 3)
590                                 /* got a net name */
591                                 i3 = (((i1 << 8) | i2) << 8) | 255;
592                         else
593                                 continue;
594
595                         for (tp = &hnametable[i2 & (HASHNAMESIZE-1)];
596                              tp->nxt; tp = tp->nxt)
597                                 ;
598                         tp->addr = i2;
599                         tp->nxt = newhnamemem();
600                         tp->name = strdup(nambuf);
601                 }
602                 fclose(fp);
603         }
604
605         for (tp = &hnametable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
606                 if (tp->addr == i)
607                         return (tp->name);
608
609         /* didn't have the node name -- see if we've got the net name */
610         i |= 255;
611         for (tp2 = &hnametable[i & (HASHNAMESIZE-1)]; tp2->nxt; tp2 = tp2->nxt)
612                 if (tp2->addr == i) {
613                         tp->addr = (atnet << 8) | athost;
614                         tp->nxt = newhnamemem();
615                         (void)snprintf(nambuf, sizeof(nambuf), "%s.%d",
616                             tp2->name, athost);
617                         tp->name = strdup(nambuf);
618                         return (tp->name);
619                 }
620
621         tp->addr = (atnet << 8) | athost;
622         tp->nxt = newhnamemem();
623         if (athost != 255)
624                 (void)snprintf(nambuf, sizeof(nambuf), "%d.%d.%d",
625                     atnet >> 8, atnet & 0xff, athost);
626         else
627                 (void)snprintf(nambuf, sizeof(nambuf), "%d.%d", atnet >> 8,
628                     atnet & 0xff);
629         tp->name = strdup(nambuf);
630
631         return (tp->name);
632 }
633
634 static struct tok skt2str[] = {
635         { rtmpSkt,      "rtmp" },       /* routing table maintenance */
636         { nbpSkt,       "nis" },        /* name info socket */
637         { echoSkt,      "echo" },       /* AppleTalk echo protocol */
638         { zipSkt,       "zip" },        /* zone info protocol */
639         { 0,            NULL }
640 };
641
642 static const char *
643 ddpskt_string(register int skt)
644 {
645         static char buf[8];
646
647         if (nflag) {
648                 (void)snprintf(buf, sizeof(buf), "%d", skt);
649                 return (buf);
650         }
651         return (tok2str(skt2str, "%d", skt));
652 }