blob: 58ced4f9b5ef971dd61e3b94972817965a600bb0 [file] [log] [blame]
The Android Open Source Project2949f582009-03-03 19:30:46 -08001/*
2 * Copyright (c) 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 * Internet, ethernet, port, and protocol string to address
22 * and address to string conversion routines
23 */
The Android Open Source Project2949f582009-03-03 19:30:46 -080024
25#ifdef HAVE_CONFIG_H
Elliott Hughes820eced2021-08-20 18:00:50 -070026#include <config.h>
The Android Open Source Project2949f582009-03-03 19:30:46 -080027#endif
28
Elliott Hughes820eced2021-08-20 18:00:50 -070029#ifdef HAVE_CASPER
30#include <libcasper.h>
31#include <casper/cap_dns.h>
32#endif /* HAVE_CASPER */
The Android Open Source Project2949f582009-03-03 19:30:46 -080033
Elliott Hughes820eced2021-08-20 18:00:50 -070034#include "netdissect-stdinc.h"
The Android Open Source Project2949f582009-03-03 19:30:46 -080035
Elliott Hughes820eced2021-08-20 18:00:50 -070036#ifdef _WIN32
37 /*
38 * We have our own ether_ntohost(), reading from the system's
39 * Ethernet address file.
40 */
41 #include "missing/win_ether_ntohost.h"
42#else
43 #ifdef USE_ETHER_NTOHOST
44 #if defined(NET_ETHERNET_H_DECLARES_ETHER_NTOHOST)
45 /*
46 * OK, just include <net/ethernet.h>.
47 */
48 #include <net/ethernet.h>
49 #elif defined(NETINET_ETHER_H_DECLARES_ETHER_NTOHOST)
50 /*
51 * OK, just include <netinet/ether.h>
52 */
53 #include <netinet/ether.h>
54 #elif defined(SYS_ETHERNET_H_DECLARES_ETHER_NTOHOST)
55 /*
56 * OK, just include <sys/ethernet.h>
57 */
58 #include <sys/ethernet.h>
59 #elif defined(ARPA_INET_H_DECLARES_ETHER_NTOHOST)
60 /*
61 * OK, just include <arpa/inet.h>
62 */
63 #include <arpa/inet.h>
64 #elif defined(NETINET_IF_ETHER_H_DECLARES_ETHER_NTOHOST)
65 /*
66 * OK, include <netinet/if_ether.h>, after all the other stuff we
67 * need to include or define for its benefit.
68 */
69 #define NEED_NETINET_IF_ETHER_H
70 #else
71 /*
72 * We'll have to declare it ourselves.
73 * If <netinet/if_ether.h> defines struct ether_addr, include
74 * it. Otherwise, define it ourselves.
75 */
76 #ifdef HAVE_STRUCT_ETHER_ADDR
77 #define NEED_NETINET_IF_ETHER_H
78 #else /* HAVE_STRUCT_ETHER_ADDR */
79 struct ether_addr {
80 /* Beware FreeBSD calls this "octet". */
81 unsigned char ether_addr_octet[MAC_ADDR_LEN];
82 };
83 #endif /* HAVE_STRUCT_ETHER_ADDR */
84 #endif /* what declares ether_ntohost() */
The Android Open Source Project2949f582009-03-03 19:30:46 -080085
Elliott Hughes820eced2021-08-20 18:00:50 -070086 #ifdef NEED_NETINET_IF_ETHER_H
87 #include <net/if.h> /* Needed on some platforms */
88 #include <netinet/in.h> /* Needed on some platforms */
89 #include <netinet/if_ether.h>
90 #endif /* NEED_NETINET_IF_ETHER_H */
91
92 #ifndef HAVE_DECL_ETHER_NTOHOST
93 /*
94 * No header declares it, so declare it ourselves.
95 */
96 extern int ether_ntohost(char *, const struct ether_addr *);
97 #endif /* !defined(HAVE_DECL_ETHER_NTOHOST) */
98 #endif /* USE_ETHER_NTOHOST */
99#endif /* _WIN32 */
The Android Open Source Project2949f582009-03-03 19:30:46 -0800100
101#include <pcap.h>
102#include <pcap-namedb.h>
Elliott Hughes820eced2021-08-20 18:00:50 -0700103#ifndef HAVE_GETSERVENT
104#include <getservent.h>
105#endif
The Android Open Source Project2949f582009-03-03 19:30:46 -0800106#include <signal.h>
107#include <stdio.h>
108#include <string.h>
109#include <stdlib.h>
110
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700111#include "netdissect.h"
The Android Open Source Project2949f582009-03-03 19:30:46 -0800112#include "addrtoname.h"
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700113#include "addrtostr.h"
114#include "ethertype.h"
The Android Open Source Project2949f582009-03-03 19:30:46 -0800115#include "llc.h"
The Android Open Source Project2949f582009-03-03 19:30:46 -0800116#include "extract.h"
117#include "oui.h"
118
The Android Open Source Project2949f582009-03-03 19:30:46 -0800119/*
120 * hash tables for whatever-to-name translations
121 *
Elliott Hughes820eced2021-08-20 18:00:50 -0700122 * ndo_error() called on strdup(3) failure with S_ERR_ND_MEM_ALLOC status
The Android Open Source Project2949f582009-03-03 19:30:46 -0800123 */
124
125#define HASHNAMESIZE 4096
The Android Open Source Project2949f582009-03-03 19:30:46 -0800126
127struct hnamemem {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700128 uint32_t addr;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800129 const char *name;
130 struct hnamemem *nxt;
131};
132
JP Abgrall53f17a92014-02-12 14:02:41 -0800133static struct hnamemem hnametable[HASHNAMESIZE];
134static struct hnamemem tporttable[HASHNAMESIZE];
135static struct hnamemem uporttable[HASHNAMESIZE];
136static struct hnamemem eprototable[HASHNAMESIZE];
137static struct hnamemem dnaddrtable[HASHNAMESIZE];
138static struct hnamemem ipxsaptable[HASHNAMESIZE];
The Android Open Source Project2949f582009-03-03 19:30:46 -0800139
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700140#ifdef _WIN32
The Android Open Source Project2949f582009-03-03 19:30:46 -0800141/*
142 * fake gethostbyaddr for Win2k/XP
143 * gethostbyaddr() returns incorrect value when AF_INET6 is passed
144 * to 3rd argument.
145 *
146 * h_name in struct hostent is only valid.
147 */
148static struct hostent *
149win32_gethostbyaddr(const char *addr, int len, int type)
150{
151 static struct hostent host;
152 static char hostbuf[NI_MAXHOST];
153 char hname[NI_MAXHOST];
154 struct sockaddr_in6 addr6;
155
156 host.h_name = hostbuf;
157 switch (type) {
158 case AF_INET:
159 return gethostbyaddr(addr, len, type);
160 break;
161 case AF_INET6:
162 memset(&addr6, 0, sizeof(addr6));
163 addr6.sin6_family = AF_INET6;
164 memcpy(&addr6.sin6_addr, addr, len);
165 if (getnameinfo((struct sockaddr *)&addr6, sizeof(addr6),
166 hname, sizeof(hname), NULL, 0, 0)) {
167 return NULL;
168 } else {
Elliott Hughes820eced2021-08-20 18:00:50 -0700169 strlcpy(host.h_name, hname, NI_MAXHOST);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800170 return &host;
171 }
172 break;
173 default:
174 return NULL;
175 }
176}
177#define gethostbyaddr win32_gethostbyaddr
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700178#endif /* _WIN32 */
The Android Open Source Project2949f582009-03-03 19:30:46 -0800179
The Android Open Source Project2949f582009-03-03 19:30:46 -0800180struct h6namemem {
Elliott Hughes820eced2021-08-20 18:00:50 -0700181 nd_ipv6 addr;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800182 char *name;
183 struct h6namemem *nxt;
184};
185
JP Abgrall53f17a92014-02-12 14:02:41 -0800186static struct h6namemem h6nametable[HASHNAMESIZE];
The Android Open Source Project2949f582009-03-03 19:30:46 -0800187
188struct enamemem {
189 u_short e_addr0;
190 u_short e_addr1;
191 u_short e_addr2;
192 const char *e_name;
193 u_char *e_nsap; /* used only for nsaptable[] */
The Android Open Source Project2949f582009-03-03 19:30:46 -0800194 struct enamemem *e_nxt;
195};
196
JP Abgrall53f17a92014-02-12 14:02:41 -0800197static struct enamemem enametable[HASHNAMESIZE];
198static struct enamemem nsaptable[HASHNAMESIZE];
Elliott Hughescec480a2017-12-19 16:54:57 -0800199
200struct bsnamemem {
201 u_short bs_addr0;
202 u_short bs_addr1;
203 u_short bs_addr2;
204 const char *bs_name;
205 u_char *bs_bytes;
206 unsigned int bs_nbytes;
207 struct bsnamemem *bs_nxt;
208};
209
210static struct bsnamemem bytestringtable[HASHNAMESIZE];
The Android Open Source Project2949f582009-03-03 19:30:46 -0800211
212struct protoidmem {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700213 uint32_t p_oui;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800214 u_short p_proto;
215 const char *p_name;
216 struct protoidmem *p_nxt;
217};
218
JP Abgrall53f17a92014-02-12 14:02:41 -0800219static struct protoidmem protoidtable[HASHNAMESIZE];
The Android Open Source Project2949f582009-03-03 19:30:46 -0800220
221/*
222 * A faster replacement for inet_ntoa().
223 */
224const char *
Elliott Hughes892a68b2015-10-19 14:43:53 -0700225intoa(uint32_t addr)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800226{
Elliott Hughes820eced2021-08-20 18:00:50 -0700227 char *cp;
228 u_int byte;
229 int n;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800230 static char buf[sizeof(".xxx.xxx.xxx.xxx")];
231
Elliott Hughes820eced2021-08-20 18:00:50 -0700232 addr = ntohl(addr);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800233 cp = buf + sizeof(buf);
234 *--cp = '\0';
235
236 n = 4;
237 do {
238 byte = addr & 0xff;
Elliott Hughes820eced2021-08-20 18:00:50 -0700239 *--cp = (char)(byte % 10) + '0';
The Android Open Source Project2949f582009-03-03 19:30:46 -0800240 byte /= 10;
241 if (byte > 0) {
Elliott Hughes820eced2021-08-20 18:00:50 -0700242 *--cp = (char)(byte % 10) + '0';
The Android Open Source Project2949f582009-03-03 19:30:46 -0800243 byte /= 10;
244 if (byte > 0)
Elliott Hughes820eced2021-08-20 18:00:50 -0700245 *--cp = (char)byte + '0';
The Android Open Source Project2949f582009-03-03 19:30:46 -0800246 }
247 *--cp = '.';
248 addr >>= 8;
249 } while (--n > 0);
250
251 return cp + 1;
252}
253
Elliott Hughes892a68b2015-10-19 14:43:53 -0700254static uint32_t f_netmask;
255static uint32_t f_localnet;
Elliott Hughes820eced2021-08-20 18:00:50 -0700256#ifdef HAVE_CASPER
257extern cap_channel_t *capdns;
258#endif
The Android Open Source Project2949f582009-03-03 19:30:46 -0800259
260/*
261 * Return a name for the IP address pointed to by ap. This address
262 * is assumed to be in network byte order.
263 *
Elliott Hughes820eced2021-08-20 18:00:50 -0700264 * NOTE: ap is *NOT* necessarily part of the packet data, so you
265 * *CANNOT* use the ND_TCHECK_* or ND_TTEST_* macros on it. Furthermore,
The Android Open Source Project2949f582009-03-03 19:30:46 -0800266 * even in cases where it *is* part of the packet data, the caller
267 * would still have to check for a null return value, even if it's
268 * just printing the return value with "%s" - not all versions of
269 * printf print "(null)" with "%s" and a null pointer, some of them
270 * don't check for a null pointer and crash in that case.
271 *
272 * The callers of this routine should, before handing this routine
273 * a pointer to packet data, be sure that the data is present in
274 * the packet buffer. They should probably do those checks anyway,
275 * as other data at that layer might not be IP addresses, and it
276 * also needs to check whether they're present in the packet buffer.
277 */
278const char *
Elliott Hughes820eced2021-08-20 18:00:50 -0700279ipaddr_string(netdissect_options *ndo, const u_char *ap)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800280{
Elliott Hughes820eced2021-08-20 18:00:50 -0700281 struct hostent *hp;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700282 uint32_t addr;
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700283 struct hnamemem *p;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800284
285 memcpy(&addr, ap, sizeof(addr));
286 p = &hnametable[addr & (HASHNAMESIZE-1)];
287 for (; p->nxt; p = p->nxt) {
288 if (p->addr == addr)
289 return (p->name);
290 }
291 p->addr = addr;
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700292 p->nxt = newhnamemem(ndo);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800293
294 /*
295 * Print names unless:
296 * (1) -n was given.
297 * (2) Address is foreign and -f was given. (If -f was not
298 * given, f_netmask and f_localnet are 0 and the test
299 * evaluates to true)
300 */
Elliott Hughes892a68b2015-10-19 14:43:53 -0700301 if (!ndo->ndo_nflag &&
The Android Open Source Project2949f582009-03-03 19:30:46 -0800302 (addr & f_netmask) == f_localnet) {
Elliott Hughes820eced2021-08-20 18:00:50 -0700303#ifdef HAVE_CASPER
304 if (capdns != NULL) {
305 hp = cap_gethostbyaddr(capdns, (char *)&addr, 4,
306 AF_INET);
307 } else
308#endif
309 hp = gethostbyaddr((char *)&addr, 4, AF_INET);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800310 if (hp) {
311 char *dotp;
312
313 p->name = strdup(hp->h_name);
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700314 if (p->name == NULL)
Elliott Hughes820eced2021-08-20 18:00:50 -0700315 (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
316 "%s: strdup(hp->h_name)", __func__);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700317 if (ndo->ndo_Nflag) {
The Android Open Source Project2949f582009-03-03 19:30:46 -0800318 /* Remove domain qualifications */
319 dotp = strchr(p->name, '.');
320 if (dotp)
321 *dotp = '\0';
322 }
323 return (p->name);
324 }
325 }
326 p->name = strdup(intoa(addr));
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700327 if (p->name == NULL)
Elliott Hughes820eced2021-08-20 18:00:50 -0700328 (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
329 "%s: strdup(intoa(addr))", __func__);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800330 return (p->name);
331}
332
The Android Open Source Project2949f582009-03-03 19:30:46 -0800333/*
334 * Return a name for the IP6 address pointed to by ap. This address
335 * is assumed to be in network byte order.
336 */
337const char *
Elliott Hughes820eced2021-08-20 18:00:50 -0700338ip6addr_string(netdissect_options *ndo, const u_char *ap)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800339{
Elliott Hughes820eced2021-08-20 18:00:50 -0700340 struct hostent *hp;
JP Abgrall53f17a92014-02-12 14:02:41 -0800341 union {
Elliott Hughes820eced2021-08-20 18:00:50 -0700342 nd_ipv6 addr;
JP Abgrall53f17a92014-02-12 14:02:41 -0800343 struct for_hash_addr {
344 char fill[14];
Elliott Hughes892a68b2015-10-19 14:43:53 -0700345 uint16_t d;
JP Abgrall53f17a92014-02-12 14:02:41 -0800346 } addra;
347 } addr;
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700348 struct h6namemem *p;
Elliott Hughes820eced2021-08-20 18:00:50 -0700349 const char *cp;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800350 char ntop_buf[INET6_ADDRSTRLEN];
351
352 memcpy(&addr, ap, sizeof(addr));
JP Abgrall53f17a92014-02-12 14:02:41 -0800353 p = &h6nametable[addr.addra.d & (HASHNAMESIZE-1)];
The Android Open Source Project2949f582009-03-03 19:30:46 -0800354 for (; p->nxt; p = p->nxt) {
355 if (memcmp(&p->addr, &addr, sizeof(addr)) == 0)
356 return (p->name);
357 }
Elliott Hughes820eced2021-08-20 18:00:50 -0700358 memcpy(p->addr, addr.addr, sizeof(nd_ipv6));
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700359 p->nxt = newh6namemem(ndo);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800360
361 /*
362 * Do not print names if -n was given.
363 */
Elliott Hughes892a68b2015-10-19 14:43:53 -0700364 if (!ndo->ndo_nflag) {
Elliott Hughes820eced2021-08-20 18:00:50 -0700365#ifdef HAVE_CASPER
366 if (capdns != NULL) {
367 hp = cap_gethostbyaddr(capdns, (char *)&addr,
368 sizeof(addr), AF_INET6);
369 } else
370#endif
371 hp = gethostbyaddr((char *)&addr, sizeof(addr),
372 AF_INET6);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800373 if (hp) {
374 char *dotp;
375
376 p->name = strdup(hp->h_name);
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700377 if (p->name == NULL)
Elliott Hughes820eced2021-08-20 18:00:50 -0700378 (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
379 "%s: strdup(hp->h_name)", __func__);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700380 if (ndo->ndo_Nflag) {
The Android Open Source Project2949f582009-03-03 19:30:46 -0800381 /* Remove domain qualifications */
382 dotp = strchr(p->name, '.');
383 if (dotp)
384 *dotp = '\0';
385 }
386 return (p->name);
387 }
388 }
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700389 cp = addrtostr6(ap, ntop_buf, sizeof(ntop_buf));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800390 p->name = strdup(cp);
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700391 if (p->name == NULL)
Elliott Hughes820eced2021-08-20 18:00:50 -0700392 (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
393 "%s: strdup(cp)", __func__);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800394 return (p->name);
395}
The Android Open Source Project2949f582009-03-03 19:30:46 -0800396
Elliott Hughes820eced2021-08-20 18:00:50 -0700397static const char hex[16] = {
398 '0', '1', '2', '3', '4', '5', '6', '7',
399 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
400};
The Android Open Source Project2949f582009-03-03 19:30:46 -0800401
Elliott Hughes820eced2021-08-20 18:00:50 -0700402/*
403 * Convert an octet to two hex digits.
404 *
405 * Coverity appears either:
406 *
407 * not to believe the C standard when it asserts that a uint8_t is
408 * exactly 8 bits in size;
409 *
410 * not to believe that an unsigned type of exactly 8 bits has a value
411 * in the range of 0 to 255;
412 *
413 * not to believe that, for a range of unsigned values, if you shift
414 * one of those values right by 4 bits, the maximum result value is
415 * the maximum value shifted right by 4 bits, with no stray 1's shifted
416 * in;
417 *
418 * not to believe that 255 >> 4 is 15;
419 *
420 * so it gets upset that we're taking a "tainted" unsigned value, shifting
421 * it right 4 bits, and using it as an index into a 16-element array.
422 *
423 * So we do a stupid pointless masking of the result of the shift with
424 * 0xf, to hammer the point home to Coverity.
425 */
426static inline char *
427octet_to_hex(char *cp, uint8_t octet)
428{
429 *cp++ = hex[(octet >> 4) & 0xf];
430 *cp++ = hex[(octet >> 0) & 0xf];
431 return (cp);
432}
The Android Open Source Project2949f582009-03-03 19:30:46 -0800433
434/* Find the hash node that corresponds the ether address 'ep' */
435
Elliott Hughes820eced2021-08-20 18:00:50 -0700436static struct enamemem *
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700437lookup_emem(netdissect_options *ndo, const u_char *ep)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800438{
Elliott Hughes820eced2021-08-20 18:00:50 -0700439 u_int i, j, k;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800440 struct enamemem *tp;
441
442 k = (ep[0] << 8) | ep[1];
443 j = (ep[2] << 8) | ep[3];
444 i = (ep[4] << 8) | ep[5];
445
446 tp = &enametable[(i ^ j) & (HASHNAMESIZE-1)];
447 while (tp->e_nxt)
448 if (tp->e_addr0 == i &&
449 tp->e_addr1 == j &&
450 tp->e_addr2 == k)
451 return tp;
452 else
453 tp = tp->e_nxt;
Elliott Hughes820eced2021-08-20 18:00:50 -0700454 tp->e_addr0 = (u_short)i;
455 tp->e_addr1 = (u_short)j;
456 tp->e_addr2 = (u_short)k;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800457 tp->e_nxt = (struct enamemem *)calloc(1, sizeof(*tp));
458 if (tp->e_nxt == NULL)
Elliott Hughes820eced2021-08-20 18:00:50 -0700459 (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC, "%s: calloc", __func__);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800460
461 return tp;
462}
463
464/*
465 * Find the hash node that corresponds to the bytestring 'bs'
466 * with length 'nlen'
467 */
468
Elliott Hughes820eced2021-08-20 18:00:50 -0700469static struct bsnamemem *
470lookup_bytestring(netdissect_options *ndo, const u_char *bs,
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700471 const unsigned int nlen)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800472{
Elliott Hughescec480a2017-12-19 16:54:57 -0800473 struct bsnamemem *tp;
Elliott Hughes820eced2021-08-20 18:00:50 -0700474 u_int i, j, k;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800475
476 if (nlen >= 6) {
477 k = (bs[0] << 8) | bs[1];
478 j = (bs[2] << 8) | bs[3];
479 i = (bs[4] << 8) | bs[5];
480 } else if (nlen >= 4) {
481 k = (bs[0] << 8) | bs[1];
482 j = (bs[2] << 8) | bs[3];
483 i = 0;
484 } else
485 i = j = k = 0;
486
487 tp = &bytestringtable[(i ^ j) & (HASHNAMESIZE-1)];
Elliott Hughescec480a2017-12-19 16:54:57 -0800488 while (tp->bs_nxt)
489 if (nlen == tp->bs_nbytes &&
490 tp->bs_addr0 == i &&
491 tp->bs_addr1 == j &&
492 tp->bs_addr2 == k &&
493 memcmp((const char *)bs, (const char *)(tp->bs_bytes), nlen) == 0)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800494 return tp;
495 else
Elliott Hughescec480a2017-12-19 16:54:57 -0800496 tp = tp->bs_nxt;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800497
Elliott Hughes820eced2021-08-20 18:00:50 -0700498 tp->bs_addr0 = (u_short)i;
499 tp->bs_addr1 = (u_short)j;
500 tp->bs_addr2 = (u_short)k;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800501
Elliott Hughescec480a2017-12-19 16:54:57 -0800502 tp->bs_bytes = (u_char *) calloc(1, nlen);
503 if (tp->bs_bytes == NULL)
Elliott Hughes820eced2021-08-20 18:00:50 -0700504 (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
505 "%s: calloc", __func__);
JP Abgrall53f17a92014-02-12 14:02:41 -0800506
Elliott Hughescec480a2017-12-19 16:54:57 -0800507 memcpy(tp->bs_bytes, bs, nlen);
508 tp->bs_nbytes = nlen;
509 tp->bs_nxt = (struct bsnamemem *)calloc(1, sizeof(*tp));
510 if (tp->bs_nxt == NULL)
Elliott Hughes820eced2021-08-20 18:00:50 -0700511 (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
512 "%s: calloc", __func__);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800513
514 return tp;
515}
516
517/* Find the hash node that corresponds the NSAP 'nsap' */
518
Elliott Hughes820eced2021-08-20 18:00:50 -0700519static struct enamemem *
520lookup_nsap(netdissect_options *ndo, const u_char *nsap,
521 u_int nsap_length)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800522{
Elliott Hughes820eced2021-08-20 18:00:50 -0700523 u_int i, j, k;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800524 struct enamemem *tp;
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700525 const u_char *ensap;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800526
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700527 if (nsap_length > 6) {
528 ensap = nsap + nsap_length - 6;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800529 k = (ensap[0] << 8) | ensap[1];
530 j = (ensap[2] << 8) | ensap[3];
531 i = (ensap[4] << 8) | ensap[5];
532 }
533 else
534 i = j = k = 0;
535
536 tp = &nsaptable[(i ^ j) & (HASHNAMESIZE-1)];
537 while (tp->e_nxt)
Elliott Hughescec480a2017-12-19 16:54:57 -0800538 if (nsap_length == tp->e_nsap[0] &&
539 tp->e_addr0 == i &&
The Android Open Source Project2949f582009-03-03 19:30:46 -0800540 tp->e_addr1 == j &&
541 tp->e_addr2 == k &&
Elliott Hughescec480a2017-12-19 16:54:57 -0800542 memcmp((const char *)nsap,
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700543 (char *)&(tp->e_nsap[1]), nsap_length) == 0)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800544 return tp;
545 else
546 tp = tp->e_nxt;
Elliott Hughes820eced2021-08-20 18:00:50 -0700547 tp->e_addr0 = (u_short)i;
548 tp->e_addr1 = (u_short)j;
549 tp->e_addr2 = (u_short)k;
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700550 tp->e_nsap = (u_char *)malloc(nsap_length + 1);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800551 if (tp->e_nsap == NULL)
Elliott Hughes820eced2021-08-20 18:00:50 -0700552 (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC, "%s: malloc", __func__);
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700553 tp->e_nsap[0] = (u_char)nsap_length; /* guaranteed < ISONSAP_MAX_LENGTH */
554 memcpy((char *)&tp->e_nsap[1], (const char *)nsap, nsap_length);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800555 tp->e_nxt = (struct enamemem *)calloc(1, sizeof(*tp));
556 if (tp->e_nxt == NULL)
Elliott Hughes820eced2021-08-20 18:00:50 -0700557 (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC, "%s: calloc", __func__);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800558
559 return tp;
560}
561
562/* Find the hash node that corresponds the protoid 'pi'. */
563
Elliott Hughes820eced2021-08-20 18:00:50 -0700564static struct protoidmem *
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700565lookup_protoid(netdissect_options *ndo, const u_char *pi)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800566{
Elliott Hughes820eced2021-08-20 18:00:50 -0700567 u_int i, j;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800568 struct protoidmem *tp;
569
570 /* 5 octets won't be aligned */
571 i = (((pi[0] << 8) + pi[1]) << 8) + pi[2];
572 j = (pi[3] << 8) + pi[4];
573 /* XXX should be endian-insensitive, but do big-endian testing XXX */
574
575 tp = &protoidtable[(i ^ j) & (HASHNAMESIZE-1)];
576 while (tp->p_nxt)
577 if (tp->p_oui == i && tp->p_proto == j)
578 return tp;
579 else
580 tp = tp->p_nxt;
581 tp->p_oui = i;
Elliott Hughes820eced2021-08-20 18:00:50 -0700582 tp->p_proto = (u_short)j;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800583 tp->p_nxt = (struct protoidmem *)calloc(1, sizeof(*tp));
584 if (tp->p_nxt == NULL)
Elliott Hughes820eced2021-08-20 18:00:50 -0700585 (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC, "%s: calloc", __func__);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800586
587 return tp;
588}
589
590const char *
Elliott Hughes820eced2021-08-20 18:00:50 -0700591etheraddr_string(netdissect_options *ndo, const uint8_t *ep)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800592{
Elliott Hughes820eced2021-08-20 18:00:50 -0700593 int i;
594 char *cp;
595 struct enamemem *tp;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800596 int oui;
597 char buf[BUFSIZE];
598
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700599 tp = lookup_emem(ndo, ep);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800600 if (tp->e_name)
601 return (tp->e_name);
602#ifdef USE_ETHER_NTOHOST
Elliott Hughes892a68b2015-10-19 14:43:53 -0700603 if (!ndo->ndo_nflag) {
The Android Open Source Project2949f582009-03-03 19:30:46 -0800604 char buf2[BUFSIZE];
Elliott Hughes820eced2021-08-20 18:00:50 -0700605 /*
606 * This is a non-const copy of ep for ether_ntohost(), which
607 * has its second argument non-const in OpenBSD. Also saves a
608 * type cast.
609 */
610 struct ether_addr ea;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800611
Elliott Hughes820eced2021-08-20 18:00:50 -0700612 memcpy (&ea, ep, MAC_ADDR_LEN);
613 if (ether_ntohost(buf2, &ea) == 0) {
The Android Open Source Project2949f582009-03-03 19:30:46 -0800614 tp->e_name = strdup(buf2);
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700615 if (tp->e_name == NULL)
Elliott Hughes820eced2021-08-20 18:00:50 -0700616 (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
617 "%s: strdup(buf2)", __func__);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800618 return (tp->e_name);
619 }
620 }
621#endif
622 cp = buf;
Elliott Hughes820eced2021-08-20 18:00:50 -0700623 oui = EXTRACT_BE_U_3(ep);
624 cp = octet_to_hex(cp, *ep++);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800625 for (i = 5; --i >= 0;) {
626 *cp++ = ':';
Elliott Hughes820eced2021-08-20 18:00:50 -0700627 cp = octet_to_hex(cp, *ep++);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800628 }
629
Elliott Hughes892a68b2015-10-19 14:43:53 -0700630 if (!ndo->ndo_nflag) {
The Android Open Source Project2949f582009-03-03 19:30:46 -0800631 snprintf(cp, BUFSIZE - (2 + 5*3), " (oui %s)",
632 tok2str(oui_values, "Unknown", oui));
633 } else
634 *cp = '\0';
635 tp->e_name = strdup(buf);
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700636 if (tp->e_name == NULL)
Elliott Hughes820eced2021-08-20 18:00:50 -0700637 (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
638 "%s: strdup(buf)", __func__);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800639 return (tp->e_name);
640}
641
642const char *
Elliott Hughes820eced2021-08-20 18:00:50 -0700643le64addr_string(netdissect_options *ndo, const uint8_t *ep)
JP Abgrall53f17a92014-02-12 14:02:41 -0800644{
645 const unsigned int len = 8;
Elliott Hughes820eced2021-08-20 18:00:50 -0700646 u_int i;
647 char *cp;
648 struct bsnamemem *tp;
JP Abgrall53f17a92014-02-12 14:02:41 -0800649 char buf[BUFSIZE];
650
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700651 tp = lookup_bytestring(ndo, ep, len);
Elliott Hughescec480a2017-12-19 16:54:57 -0800652 if (tp->bs_name)
653 return (tp->bs_name);
JP Abgrall53f17a92014-02-12 14:02:41 -0800654
655 cp = buf;
656 for (i = len; i > 0 ; --i) {
Elliott Hughes820eced2021-08-20 18:00:50 -0700657 cp = octet_to_hex(cp, *(ep + i - 1));
JP Abgrall53f17a92014-02-12 14:02:41 -0800658 *cp++ = ':';
659 }
660 cp --;
661
662 *cp = '\0';
663
Elliott Hughescec480a2017-12-19 16:54:57 -0800664 tp->bs_name = strdup(buf);
665 if (tp->bs_name == NULL)
Elliott Hughes820eced2021-08-20 18:00:50 -0700666 (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
667 "%s: strdup(buf)", __func__);
JP Abgrall53f17a92014-02-12 14:02:41 -0800668
Elliott Hughescec480a2017-12-19 16:54:57 -0800669 return (tp->bs_name);
JP Abgrall53f17a92014-02-12 14:02:41 -0800670}
671
672const char *
Elliott Hughes820eced2021-08-20 18:00:50 -0700673linkaddr_string(netdissect_options *ndo, const uint8_t *ep,
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700674 const unsigned int type, const unsigned int len)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800675{
Elliott Hughes820eced2021-08-20 18:00:50 -0700676 u_int i;
677 char *cp;
678 struct bsnamemem *tp;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800679
JP Abgrall53f17a92014-02-12 14:02:41 -0800680 if (len == 0)
681 return ("<empty>");
682
Elliott Hughes820eced2021-08-20 18:00:50 -0700683 if (type == LINKADDR_ETHER && len == MAC_ADDR_LEN)
Elliott Hughes892a68b2015-10-19 14:43:53 -0700684 return (etheraddr_string(ndo, ep));
JP Abgrall53f17a92014-02-12 14:02:41 -0800685
686 if (type == LINKADDR_FRELAY)
Elliott Hughes892a68b2015-10-19 14:43:53 -0700687 return (q922_string(ndo, ep, len));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800688
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700689 tp = lookup_bytestring(ndo, ep, len);
Elliott Hughescec480a2017-12-19 16:54:57 -0800690 if (tp->bs_name)
691 return (tp->bs_name);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800692
Elliott Hughescec480a2017-12-19 16:54:57 -0800693 tp->bs_name = cp = (char *)malloc(len*3);
694 if (tp->bs_name == NULL)
Elliott Hughes820eced2021-08-20 18:00:50 -0700695 (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
696 "%s: malloc", __func__);
697 cp = octet_to_hex(cp, *ep++);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800698 for (i = len-1; i > 0 ; --i) {
699 *cp++ = ':';
Elliott Hughes820eced2021-08-20 18:00:50 -0700700 cp = octet_to_hex(cp, *ep++);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800701 }
702 *cp = '\0';
Elliott Hughescec480a2017-12-19 16:54:57 -0800703 return (tp->bs_name);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800704}
705
The Android Open Source Project2949f582009-03-03 19:30:46 -0800706#define ISONSAP_MAX_LENGTH 20
707const char *
Elliott Hughes820eced2021-08-20 18:00:50 -0700708isonsap_string(netdissect_options *ndo, const uint8_t *nsap,
709 u_int nsap_length)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800710{
Elliott Hughes820eced2021-08-20 18:00:50 -0700711 u_int nsap_idx;
712 char *cp;
713 struct enamemem *tp;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800714
715 if (nsap_length < 1 || nsap_length > ISONSAP_MAX_LENGTH)
716 return ("isonsap_string: illegal length");
717
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700718 tp = lookup_nsap(ndo, nsap, nsap_length);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800719 if (tp->e_name)
720 return tp->e_name;
721
722 tp->e_name = cp = (char *)malloc(sizeof("xx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xx"));
723 if (cp == NULL)
Elliott Hughes820eced2021-08-20 18:00:50 -0700724 (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
725 "%s: malloc", __func__);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800726
727 for (nsap_idx = 0; nsap_idx < nsap_length; nsap_idx++) {
Elliott Hughes820eced2021-08-20 18:00:50 -0700728 cp = octet_to_hex(cp, *nsap++);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800729 if (((nsap_idx & 1) == 0) &&
730 (nsap_idx + 1 < nsap_length)) {
Elliott Hughes820eced2021-08-20 18:00:50 -0700731 *cp++ = '.';
The Android Open Source Project2949f582009-03-03 19:30:46 -0800732 }
733 }
734 *cp = '\0';
735 return (tp->e_name);
736}
737
738const char *
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700739tcpport_string(netdissect_options *ndo, u_short port)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800740{
Elliott Hughes820eced2021-08-20 18:00:50 -0700741 struct hnamemem *tp;
742 uint32_t i = port;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800743 char buf[sizeof("00000")];
744
745 for (tp = &tporttable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
746 if (tp->addr == i)
747 return (tp->name);
748
749 tp->addr = i;
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700750 tp->nxt = newhnamemem(ndo);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800751
752 (void)snprintf(buf, sizeof(buf), "%u", i);
753 tp->name = strdup(buf);
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700754 if (tp->name == NULL)
Elliott Hughes820eced2021-08-20 18:00:50 -0700755 (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
756 "%s: strdup(buf)", __func__);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800757 return (tp->name);
758}
759
760const char *
Elliott Hughes820eced2021-08-20 18:00:50 -0700761udpport_string(netdissect_options *ndo, u_short port)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800762{
Elliott Hughes820eced2021-08-20 18:00:50 -0700763 struct hnamemem *tp;
764 uint32_t i = port;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800765 char buf[sizeof("00000")];
766
767 for (tp = &uporttable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
768 if (tp->addr == i)
769 return (tp->name);
770
771 tp->addr = i;
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700772 tp->nxt = newhnamemem(ndo);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800773
774 (void)snprintf(buf, sizeof(buf), "%u", i);
775 tp->name = strdup(buf);
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700776 if (tp->name == NULL)
Elliott Hughes820eced2021-08-20 18:00:50 -0700777 (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
778 "%s: strdup(buf)", __func__);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800779 return (tp->name);
780}
781
782const char *
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700783ipxsap_string(netdissect_options *ndo, u_short port)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800784{
Elliott Hughes820eced2021-08-20 18:00:50 -0700785 char *cp;
786 struct hnamemem *tp;
787 uint32_t i = port;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800788 char buf[sizeof("0000")];
789
790 for (tp = &ipxsaptable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
791 if (tp->addr == i)
792 return (tp->name);
793
794 tp->addr = i;
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700795 tp->nxt = newhnamemem(ndo);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800796
797 cp = buf;
Elliott Hughes820eced2021-08-20 18:00:50 -0700798 port = ntohs(port);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800799 *cp++ = hex[port >> 12 & 0xf];
800 *cp++ = hex[port >> 8 & 0xf];
801 *cp++ = hex[port >> 4 & 0xf];
802 *cp++ = hex[port & 0xf];
803 *cp++ = '\0';
804 tp->name = strdup(buf);
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700805 if (tp->name == NULL)
Elliott Hughes820eced2021-08-20 18:00:50 -0700806 (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
807 "%s: strdup(buf)", __func__);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800808 return (tp->name);
809}
810
811static void
Elliott Hughes892a68b2015-10-19 14:43:53 -0700812init_servarray(netdissect_options *ndo)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800813{
814 struct servent *sv;
Elliott Hughes820eced2021-08-20 18:00:50 -0700815 struct hnamemem *table;
816 int i;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800817 char buf[sizeof("0000000000")];
818
819 while ((sv = getservent()) != NULL) {
820 int port = ntohs(sv->s_port);
821 i = port & (HASHNAMESIZE-1);
822 if (strcmp(sv->s_proto, "tcp") == 0)
823 table = &tporttable[i];
824 else if (strcmp(sv->s_proto, "udp") == 0)
825 table = &uporttable[i];
826 else
827 continue;
828
829 while (table->name)
830 table = table->nxt;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700831 if (ndo->ndo_nflag) {
The Android Open Source Project2949f582009-03-03 19:30:46 -0800832 (void)snprintf(buf, sizeof(buf), "%d", port);
833 table->name = strdup(buf);
834 } else
835 table->name = strdup(sv->s_name);
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700836 if (table->name == NULL)
Elliott Hughes820eced2021-08-20 18:00:50 -0700837 (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
838 "%s: strdup", __func__);
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700839
The Android Open Source Project2949f582009-03-03 19:30:46 -0800840 table->addr = port;
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700841 table->nxt = newhnamemem(ndo);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800842 }
843 endservent();
844}
845
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700846static const struct eproto {
The Android Open Source Project2949f582009-03-03 19:30:46 -0800847 const char *s;
848 u_short p;
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700849} eproto_db[] = {
Elliott Hughes820eced2021-08-20 18:00:50 -0700850 { "aarp", ETHERTYPE_AARP },
851 { "arp", ETHERTYPE_ARP },
852 { "atalk", ETHERTYPE_ATALK },
853 { "decnet", ETHERTYPE_DN },
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700854 { "ip", ETHERTYPE_IP },
855 { "ip6", ETHERTYPE_IPV6 },
Elliott Hughes820eced2021-08-20 18:00:50 -0700856 { "lat", ETHERTYPE_LAT },
857 { "loopback", ETHERTYPE_LOOPBACK },
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700858 { "mopdl", ETHERTYPE_MOPDL },
859 { "moprc", ETHERTYPE_MOPRC },
Elliott Hughes820eced2021-08-20 18:00:50 -0700860 { "rarp", ETHERTYPE_REVARP },
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700861 { "sca", ETHERTYPE_SCA },
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700862 { (char *)0, 0 }
863};
The Android Open Source Project2949f582009-03-03 19:30:46 -0800864
865static void
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700866init_eprotoarray(netdissect_options *ndo)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800867{
Elliott Hughes820eced2021-08-20 18:00:50 -0700868 int i;
869 struct hnamemem *table;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800870
871 for (i = 0; eproto_db[i].s; i++) {
872 int j = htons(eproto_db[i].p) & (HASHNAMESIZE-1);
873 table = &eprototable[j];
874 while (table->name)
875 table = table->nxt;
876 table->name = eproto_db[i].s;
877 table->addr = htons(eproto_db[i].p);
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700878 table->nxt = newhnamemem(ndo);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800879 }
880}
881
JP Abgrall53f17a92014-02-12 14:02:41 -0800882static const struct protoidlist {
The Android Open Source Project2949f582009-03-03 19:30:46 -0800883 const u_char protoid[5];
884 const char *name;
885} protoidlist[] = {
886 {{ 0x00, 0x00, 0x0c, 0x01, 0x07 }, "CiscoMLS" },
887 {{ 0x00, 0x00, 0x0c, 0x20, 0x00 }, "CiscoCDP" },
888 {{ 0x00, 0x00, 0x0c, 0x20, 0x01 }, "CiscoCGMP" },
889 {{ 0x00, 0x00, 0x0c, 0x20, 0x03 }, "CiscoVTP" },
890 {{ 0x00, 0xe0, 0x2b, 0x00, 0xbb }, "ExtremeEDP" },
891 {{ 0x00, 0x00, 0x00, 0x00, 0x00 }, NULL }
892};
893
894/*
895 * SNAP proto IDs with org code 0:0:0 are actually encapsulated Ethernet
896 * types.
897 */
898static void
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700899init_protoidarray(netdissect_options *ndo)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800900{
Elliott Hughes820eced2021-08-20 18:00:50 -0700901 int i;
902 struct protoidmem *tp;
JP Abgrall53f17a92014-02-12 14:02:41 -0800903 const struct protoidlist *pl;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800904 u_char protoid[5];
905
906 protoid[0] = 0;
907 protoid[1] = 0;
908 protoid[2] = 0;
909 for (i = 0; eproto_db[i].s; i++) {
910 u_short etype = htons(eproto_db[i].p);
911
912 memcpy((char *)&protoid[3], (char *)&etype, 2);
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700913 tp = lookup_protoid(ndo, protoid);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800914 tp->p_name = strdup(eproto_db[i].s);
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700915 if (tp->p_name == NULL)
Elliott Hughes820eced2021-08-20 18:00:50 -0700916 (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
917 "%s: strdup(eproto_db[i].s)", __func__);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800918 }
919 /* Hardwire some SNAP proto ID names */
920 for (pl = protoidlist; pl->name != NULL; ++pl) {
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700921 tp = lookup_protoid(ndo, pl->protoid);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800922 /* Don't override existing name */
923 if (tp->p_name != NULL)
924 continue;
925
926 tp->p_name = pl->name;
927 }
928}
929
JP Abgrall53f17a92014-02-12 14:02:41 -0800930static const struct etherlist {
Elliott Hughes820eced2021-08-20 18:00:50 -0700931 const nd_mac_addr addr;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800932 const char *name;
933} etherlist[] = {
934 {{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, "Broadcast" },
935 {{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, NULL }
936};
937
938/*
939 * Initialize the ethers hash table. We take two different approaches
940 * depending on whether or not the system provides the ethers name
941 * service. If it does, we just wire in a few names at startup,
942 * and etheraddr_string() fills in the table on demand. If it doesn't,
943 * then we suck in the entire /etc/ethers file at startup. The idea
944 * is that parsing the local file will be fast, but spinning through
945 * all the ethers entries via NIS & next_etherent might be very slow.
946 *
947 * XXX pcap_next_etherent doesn't belong in the pcap interface, but
948 * since the pcap module already does name-to-address translation,
949 * it's already does most of the work for the ethernet address-to-name
950 * translation, so we just pcap_next_etherent as a convenience.
951 */
952static void
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700953init_etherarray(netdissect_options *ndo)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800954{
Elliott Hughes820eced2021-08-20 18:00:50 -0700955 const struct etherlist *el;
956 struct enamemem *tp;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800957#ifdef USE_ETHER_NTOHOST
958 char name[256];
959#else
Elliott Hughes820eced2021-08-20 18:00:50 -0700960 struct pcap_etherent *ep;
961 FILE *fp;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800962
963 /* Suck in entire ethers file */
964 fp = fopen(PCAP_ETHERS_FILE, "r");
965 if (fp != NULL) {
966 while ((ep = pcap_next_etherent(fp)) != NULL) {
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700967 tp = lookup_emem(ndo, ep->addr);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800968 tp->e_name = strdup(ep->name);
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700969 if (tp->e_name == NULL)
Elliott Hughes820eced2021-08-20 18:00:50 -0700970 (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
971 "%s: strdup(ep->addr)", __func__);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800972 }
973 (void)fclose(fp);
974 }
975#endif
976
977 /* Hardwire some ethernet names */
978 for (el = etherlist; el->name != NULL; ++el) {
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700979 tp = lookup_emem(ndo, el->addr);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800980 /* Don't override existing name */
981 if (tp->e_name != NULL)
982 continue;
983
984#ifdef USE_ETHER_NTOHOST
985 /*
986 * Use YP/NIS version of name if available.
The Android Open Source Project2949f582009-03-03 19:30:46 -0800987 */
Elliott Hughes820eced2021-08-20 18:00:50 -0700988 /* Same workaround as in etheraddr_string(). */
989 struct ether_addr ea;
990 memcpy (&ea, el->addr, MAC_ADDR_LEN);
991 if (ether_ntohost(name, &ea) == 0) {
The Android Open Source Project2949f582009-03-03 19:30:46 -0800992 tp->e_name = strdup(name);
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700993 if (tp->e_name == NULL)
Elliott Hughes820eced2021-08-20 18:00:50 -0700994 (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
995 "%s: strdup(name)", __func__);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800996 continue;
997 }
998#endif
999 tp->e_name = el->name;
1000 }
1001}
1002
Elliott Hughes820eced2021-08-20 18:00:50 -07001003static const struct ipxsap_ent {
1004 uint16_t v;
1005 const char *s;
1006} ipxsap_db[] = {
The Android Open Source Project2949f582009-03-03 19:30:46 -08001007 { 0x0000, "Unknown" },
1008 { 0x0001, "User" },
1009 { 0x0002, "User Group" },
1010 { 0x0003, "PrintQueue" },
1011 { 0x0004, "FileServer" },
1012 { 0x0005, "JobServer" },
1013 { 0x0006, "Gateway" },
1014 { 0x0007, "PrintServer" },
1015 { 0x0008, "ArchiveQueue" },
1016 { 0x0009, "ArchiveServer" },
1017 { 0x000a, "JobQueue" },
1018 { 0x000b, "Administration" },
1019 { 0x000F, "Novell TI-RPC" },
1020 { 0x0017, "Diagnostics" },
1021 { 0x0020, "NetBIOS" },
1022 { 0x0021, "NAS SNA Gateway" },
1023 { 0x0023, "NACS AsyncGateway" },
1024 { 0x0024, "RemoteBridge/RoutingService" },
1025 { 0x0026, "BridgeServer" },
1026 { 0x0027, "TCP/IP Gateway" },
1027 { 0x0028, "Point-to-point X.25 BridgeServer" },
1028 { 0x0029, "3270 Gateway" },
1029 { 0x002a, "CHI Corp" },
1030 { 0x002c, "PC Chalkboard" },
1031 { 0x002d, "TimeSynchServer" },
1032 { 0x002e, "ARCserve5.0/PalindromeBackup" },
1033 { 0x0045, "DI3270 Gateway" },
1034 { 0x0047, "AdvertisingPrintServer" },
1035 { 0x004a, "NetBlazerModems" },
1036 { 0x004b, "BtrieveVAP" },
1037 { 0x004c, "NetwareSQL" },
1038 { 0x004d, "XtreeNetwork" },
1039 { 0x0050, "BtrieveVAP4.11" },
1040 { 0x0052, "QuickLink" },
1041 { 0x0053, "PrintQueueUser" },
1042 { 0x0058, "Multipoint X.25 Router" },
1043 { 0x0060, "STLB/NLM" },
1044 { 0x0064, "ARCserve" },
1045 { 0x0066, "ARCserve3.0" },
1046 { 0x0072, "WAN CopyUtility" },
1047 { 0x007a, "TES-NetwareVMS" },
1048 { 0x0092, "WATCOM Debugger/EmeraldTapeBackupServer" },
1049 { 0x0095, "DDA OBGYN" },
1050 { 0x0098, "NetwareAccessServer" },
1051 { 0x009a, "Netware for VMS II/NamedPipeServer" },
1052 { 0x009b, "NetwareAccessServer" },
1053 { 0x009e, "PortableNetwareServer/SunLinkNVT" },
1054 { 0x00a1, "PowerchuteAPC UPS" },
1055 { 0x00aa, "LAWserve" },
1056 { 0x00ac, "CompaqIDA StatusMonitor" },
1057 { 0x0100, "PIPE STAIL" },
1058 { 0x0102, "LAN ProtectBindery" },
1059 { 0x0103, "OracleDataBaseServer" },
1060 { 0x0107, "Netware386/RSPX RemoteConsole" },
1061 { 0x010f, "NovellSNA Gateway" },
1062 { 0x0111, "TestServer" },
1063 { 0x0112, "HP PrintServer" },
1064 { 0x0114, "CSA MUX" },
1065 { 0x0115, "CSA LCA" },
1066 { 0x0116, "CSA CM" },
1067 { 0x0117, "CSA SMA" },
1068 { 0x0118, "CSA DBA" },
1069 { 0x0119, "CSA NMA" },
1070 { 0x011a, "CSA SSA" },
1071 { 0x011b, "CSA STATUS" },
1072 { 0x011e, "CSA APPC" },
1073 { 0x0126, "SNA TEST SSA Profile" },
1074 { 0x012a, "CSA TRACE" },
1075 { 0x012b, "NetwareSAA" },
1076 { 0x012e, "IKARUS VirusScan" },
1077 { 0x0130, "CommunicationsExecutive" },
1078 { 0x0133, "NNS DomainServer/NetwareNamingServicesDomain" },
1079 { 0x0135, "NetwareNamingServicesProfile" },
1080 { 0x0137, "Netware386 PrintQueue/NNS PrintQueue" },
1081 { 0x0141, "LAN SpoolServer" },
1082 { 0x0152, "IRMALAN Gateway" },
1083 { 0x0154, "NamedPipeServer" },
1084 { 0x0166, "NetWareManagement" },
1085 { 0x0168, "Intel PICKIT CommServer/Intel CAS TalkServer" },
1086 { 0x0173, "Compaq" },
1087 { 0x0174, "Compaq SNMP Agent" },
1088 { 0x0175, "Compaq" },
1089 { 0x0180, "XTreeServer/XTreeTools" },
1090 { 0x018A, "NASI ServicesBroadcastServer" },
1091 { 0x01b0, "GARP Gateway" },
1092 { 0x01b1, "Binfview" },
1093 { 0x01bf, "IntelLanDeskManager" },
1094 { 0x01ca, "AXTEC" },
1095 { 0x01cb, "ShivaNetModem/E" },
1096 { 0x01cc, "ShivaLanRover/E" },
1097 { 0x01cd, "ShivaLanRover/T" },
1098 { 0x01ce, "ShivaUniversal" },
1099 { 0x01d8, "CastelleFAXPressServer" },
1100 { 0x01da, "CastelleLANPressPrintServer" },
1101 { 0x01dc, "CastelleFAX/Xerox7033 FaxServer/ExcelLanFax" },
1102 { 0x01f0, "LEGATO" },
1103 { 0x01f5, "LEGATO" },
1104 { 0x0233, "NMS Agent/NetwareManagementAgent" },
1105 { 0x0237, "NMS IPX Discovery/LANternReadWriteChannel" },
1106 { 0x0238, "NMS IP Discovery/LANternTrapAlarmChannel" },
1107 { 0x023a, "LANtern" },
1108 { 0x023c, "MAVERICK" },
1109 { 0x023f, "NovellSMDR" },
1110 { 0x024e, "NetwareConnect" },
1111 { 0x024f, "NASI ServerBroadcast Cisco" },
1112 { 0x026a, "NMS ServiceConsole" },
1113 { 0x026b, "TimeSynchronizationServer Netware 4.x" },
1114 { 0x0278, "DirectoryServer Netware 4.x" },
1115 { 0x027b, "NetwareManagementAgent" },
1116 { 0x0280, "Novell File and Printer Sharing Service for PC" },
1117 { 0x0304, "NovellSAA Gateway" },
1118 { 0x0308, "COM/VERMED" },
1119 { 0x030a, "GalacticommWorldgroupServer" },
1120 { 0x030c, "IntelNetport2/HP JetDirect/HP Quicksilver" },
1121 { 0x0320, "AttachmateGateway" },
1122 { 0x0327, "MicrosoftDiagnostiocs" },
1123 { 0x0328, "WATCOM SQL Server" },
1124 { 0x0335, "MultiTechSystems MultisynchCommServer" },
1125 { 0x0343, "Xylogics RemoteAccessServer/LANModem" },
1126 { 0x0355, "ArcadaBackupExec" },
1127 { 0x0358, "MSLCD1" },
1128 { 0x0361, "NETINELO" },
1129 { 0x037e, "Powerchute UPS Monitoring" },
1130 { 0x037f, "ViruSafeNotify" },
1131 { 0x0386, "HP Bridge" },
1132 { 0x0387, "HP Hub" },
1133 { 0x0394, "NetWare SAA Gateway" },
1134 { 0x039b, "LotusNotes" },
1135 { 0x03b7, "CertusAntiVirus" },
1136 { 0x03c4, "ARCserve4.0" },
1137 { 0x03c7, "LANspool3.5" },
1138 { 0x03d7, "LexmarkPrinterServer" },
1139 { 0x03d8, "LexmarkXLE PrinterServer" },
1140 { 0x03dd, "BanyanENS NetwareClient" },
1141 { 0x03de, "GuptaSequelBaseServer/NetWareSQL" },
1142 { 0x03e1, "UnivelUnixware" },
1143 { 0x03e4, "UnivelUnixware" },
1144 { 0x03fc, "IntelNetport" },
1145 { 0x03fd, "PrintServerQueue" },
1146 { 0x040A, "ipnServer" },
1147 { 0x040D, "LVERRMAN" },
1148 { 0x040E, "LVLIC" },
1149 { 0x0414, "NET Silicon (DPI)/Kyocera" },
1150 { 0x0429, "SiteLockVirus" },
1151 { 0x0432, "UFHELPR???" },
1152 { 0x0433, "Synoptics281xAdvancedSNMPAgent" },
1153 { 0x0444, "MicrosoftNT SNA Server" },
1154 { 0x0448, "Oracle" },
1155 { 0x044c, "ARCserve5.01" },
1156 { 0x0457, "CanonGP55" },
1157 { 0x045a, "QMS Printers" },
1158 { 0x045b, "DellSCSI Array" },
1159 { 0x0491, "NetBlazerModems" },
1160 { 0x04ac, "OnTimeScheduler" },
1161 { 0x04b0, "CD-Net" },
1162 { 0x0513, "EmulexNQA" },
1163 { 0x0520, "SiteLockChecks" },
1164 { 0x0529, "SiteLockChecks" },
1165 { 0x052d, "CitrixOS2 AppServer" },
1166 { 0x0535, "Tektronix" },
1167 { 0x0536, "Milan" },
1168 { 0x055d, "Attachmate SNA gateway" },
1169 { 0x056b, "IBM8235 ModemServer" },
1170 { 0x056c, "ShivaLanRover/E PLUS" },
1171 { 0x056d, "ShivaLanRover/T PLUS" },
1172 { 0x0580, "McAfeeNetShield" },
1173 { 0x05B8, "NLM to workstation communication (Revelation Software)" },
1174 { 0x05BA, "CompatibleSystemsRouters" },
1175 { 0x05BE, "CheyenneHierarchicalStorageManager" },
1176 { 0x0606, "JCWatermarkImaging" },
1177 { 0x060c, "AXISNetworkPrinter" },
1178 { 0x0610, "AdaptecSCSIManagement" },
1179 { 0x0621, "IBM AntiVirus" },
1180 { 0x0640, "Windows95 RemoteRegistryService" },
1181 { 0x064e, "MicrosoftIIS" },
1182 { 0x067b, "Microsoft Win95/98 File and Print Sharing for NetWare" },
1183 { 0x067c, "Microsoft Win95/98 File and Print Sharing for NetWare" },
1184 { 0x076C, "Xerox" },
1185 { 0x079b, "ShivaLanRover/E 115" },
1186 { 0x079c, "ShivaLanRover/T 115" },
1187 { 0x07B4, "CubixWorldDesk" },
1188 { 0x07c2, "Quarterdeck IWare Connect V2.x NLM" },
1189 { 0x07c1, "Quarterdeck IWare Connect V3.x NLM" },
1190 { 0x0810, "ELAN License Server Demo" },
1191 { 0x0824, "ShivaLanRoverAccessSwitch/E" },
1192 { 0x086a, "ISSC Collector" },
1193 { 0x087f, "ISSC DAS AgentAIX" },
1194 { 0x0880, "Intel Netport PRO" },
1195 { 0x0881, "Intel Netport PRO" },
1196 { 0x0b29, "SiteLock" },
1197 { 0x0c29, "SiteLockApplications" },
1198 { 0x0c2c, "LicensingServer" },
1199 { 0x2101, "PerformanceTechnologyInstantInternet" },
1200 { 0x2380, "LAI SiteLock" },
1201 { 0x238c, "MeetingMaker" },
1202 { 0x4808, "SiteLockServer/SiteLockMetering" },
1203 { 0x5555, "SiteLockUser" },
1204 { 0x6312, "Tapeware" },
1205 { 0x6f00, "RabbitGateway" },
1206 { 0x7703, "MODEM" },
1207 { 0x8002, "NetPortPrinters" },
1208 { 0x8008, "WordPerfectNetworkVersion" },
1209 { 0x85BE, "Cisco EIGRP" },
1210 { 0x8888, "WordPerfectNetworkVersion/QuickNetworkManagement" },
1211 { 0x9000, "McAfeeNetShield" },
1212 { 0x9604, "CSA-NT_MON" },
1213 { 0xb6a8, "OceanIsleReachoutRemoteControl" },
1214 { 0xf11f, "SiteLockMetering" },
1215 { 0xf1ff, "SiteLock" },
1216 { 0xf503, "Microsoft SQL Server" },
1217 { 0xF905, "IBM TimeAndPlace" },
1218 { 0xfbfb, "TopCallIII FaxServer" },
1219 { 0xffff, "AnyService/Wildcard" },
1220 { 0, (char *)0 }
1221};
1222
1223static void
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001224init_ipxsaparray(netdissect_options *ndo)
The Android Open Source Project2949f582009-03-03 19:30:46 -08001225{
Elliott Hughes820eced2021-08-20 18:00:50 -07001226 int i;
1227 struct hnamemem *table;
The Android Open Source Project2949f582009-03-03 19:30:46 -08001228
1229 for (i = 0; ipxsap_db[i].s != NULL; i++) {
Elliott Hughes820eced2021-08-20 18:00:50 -07001230 u_int j = htons(ipxsap_db[i].v) & (HASHNAMESIZE-1);
The Android Open Source Project2949f582009-03-03 19:30:46 -08001231 table = &ipxsaptable[j];
1232 while (table->name)
1233 table = table->nxt;
1234 table->name = ipxsap_db[i].s;
1235 table->addr = htons(ipxsap_db[i].v);
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001236 table->nxt = newhnamemem(ndo);
The Android Open Source Project2949f582009-03-03 19:30:46 -08001237 }
1238}
1239
1240/*
1241 * Initialize the address to name translation machinery. We map all
Elliott Hughes892a68b2015-10-19 14:43:53 -07001242 * non-local IP addresses to numeric addresses if ndo->ndo_fflag is true
1243 * (i.e., to prevent blocking on the nameserver). localnet is the IP address
The Android Open Source Project2949f582009-03-03 19:30:46 -08001244 * of the local network. mask is its subnet mask.
1245 */
1246void
Elliott Hughes892a68b2015-10-19 14:43:53 -07001247init_addrtoname(netdissect_options *ndo, uint32_t localnet, uint32_t mask)
The Android Open Source Project2949f582009-03-03 19:30:46 -08001248{
Elliott Hughes892a68b2015-10-19 14:43:53 -07001249 if (ndo->ndo_fflag) {
The Android Open Source Project2949f582009-03-03 19:30:46 -08001250 f_localnet = localnet;
1251 f_netmask = mask;
1252 }
Elliott Hughes892a68b2015-10-19 14:43:53 -07001253 if (ndo->ndo_nflag)
The Android Open Source Project2949f582009-03-03 19:30:46 -08001254 /*
1255 * Simplest way to suppress names.
1256 */
1257 return;
1258
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001259 init_etherarray(ndo);
Elliott Hughes892a68b2015-10-19 14:43:53 -07001260 init_servarray(ndo);
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001261 init_eprotoarray(ndo);
1262 init_protoidarray(ndo);
1263 init_ipxsaparray(ndo);
The Android Open Source Project2949f582009-03-03 19:30:46 -08001264}
1265
1266const char *
Elliott Hughes892a68b2015-10-19 14:43:53 -07001267dnaddr_string(netdissect_options *ndo, u_short dnaddr)
The Android Open Source Project2949f582009-03-03 19:30:46 -08001268{
Elliott Hughes820eced2021-08-20 18:00:50 -07001269 struct hnamemem *tp;
The Android Open Source Project2949f582009-03-03 19:30:46 -08001270
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001271 for (tp = &dnaddrtable[dnaddr & (HASHNAMESIZE-1)]; tp->nxt != NULL;
The Android Open Source Project2949f582009-03-03 19:30:46 -08001272 tp = tp->nxt)
1273 if (tp->addr == dnaddr)
1274 return (tp->name);
1275
1276 tp->addr = dnaddr;
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001277 tp->nxt = newhnamemem(ndo);
Elliott Hughes820eced2021-08-20 18:00:50 -07001278 tp->name = dnnum_string(ndo, dnaddr);
The Android Open Source Project2949f582009-03-03 19:30:46 -08001279
1280 return(tp->name);
1281}
1282
1283/* Return a zero'ed hnamemem struct and cuts down on calloc() overhead */
1284struct hnamemem *
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001285newhnamemem(netdissect_options *ndo)
The Android Open Source Project2949f582009-03-03 19:30:46 -08001286{
Elliott Hughes820eced2021-08-20 18:00:50 -07001287 struct hnamemem *p;
The Android Open Source Project2949f582009-03-03 19:30:46 -08001288 static struct hnamemem *ptr = NULL;
1289 static u_int num = 0;
1290
1291 if (num <= 0) {
1292 num = 64;
1293 ptr = (struct hnamemem *)calloc(num, sizeof (*ptr));
1294 if (ptr == NULL)
Elliott Hughes820eced2021-08-20 18:00:50 -07001295 (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
1296 "%s: calloc", __func__);
The Android Open Source Project2949f582009-03-03 19:30:46 -08001297 }
1298 --num;
1299 p = ptr++;
1300 return (p);
1301}
1302
The Android Open Source Project2949f582009-03-03 19:30:46 -08001303/* Return a zero'ed h6namemem struct and cuts down on calloc() overhead */
1304struct h6namemem *
Elliott Hughese2e3bd12017-05-15 10:59:29 -07001305newh6namemem(netdissect_options *ndo)
The Android Open Source Project2949f582009-03-03 19:30:46 -08001306{
Elliott Hughes820eced2021-08-20 18:00:50 -07001307 struct h6namemem *p;
The Android Open Source Project2949f582009-03-03 19:30:46 -08001308 static struct h6namemem *ptr = NULL;
1309 static u_int num = 0;
1310
1311 if (num <= 0) {
1312 num = 64;
1313 ptr = (struct h6namemem *)calloc(num, sizeof (*ptr));
1314 if (ptr == NULL)
Elliott Hughes820eced2021-08-20 18:00:50 -07001315 (*ndo->ndo_error)(ndo, S_ERR_ND_MEM_ALLOC,
1316 "%s: calloc", __func__);
The Android Open Source Project2949f582009-03-03 19:30:46 -08001317 }
1318 --num;
1319 p = ptr++;
1320 return (p);
1321}
Elliott Hughes892a68b2015-10-19 14:43:53 -07001322
1323/* Represent TCI part of the 802.1Q 4-octet tag as text. */
1324const char *
1325ieee8021q_tci_string(const uint16_t tci)
1326{
1327 static char buf[128];
1328 snprintf(buf, sizeof(buf), "vlan %u, p %u%s",
1329 tci & 0xfff,
1330 tci >> 13,
1331 (tci & 0x1000) ? ", DEI" : "");
1332 return buf;
1333}