1/*
2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000
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#include <sys/cdefs.h>
22#ifndef lint
23__RCSID("$NetBSD: print-ap1394.c,v 1.7 2023/08/17 20:19:40 christos Exp $");
24#endif
25
26/* \summary: Apple IP-over-IEEE 1394 printer */
27
28#ifdef HAVE_CONFIG_H
29#include <config.h>
30#endif
31
32#include "netdissect-stdinc.h"
33
34#define ND_LONGJMP_FROM_TCHECK
35#include "netdissect.h"
36#include "extract.h"
37#include "addrtoname.h"
38#include "ethertype.h"
39
40/*
41 * Structure of a header for Apple's IP-over-IEEE 1384 BPF header.
42 */
43#define FIREWIRE_EUI64_LEN	8
44struct firewire_header {
45	nd_byte     firewire_dhost[FIREWIRE_EUI64_LEN];
46	nd_byte     firewire_shost[FIREWIRE_EUI64_LEN];
47	nd_uint16_t firewire_type;
48};
49
50/*
51 * Length of that header; note that some compilers may pad
52 * "struct firewire_header" to a multiple of 4 bytes, for example, so
53 * "sizeof (struct firewire_header)" may not give the right answer.
54 */
55#define FIREWIRE_HDRLEN		18
56
57static const char *
58fwaddr_string(netdissect_options *ndo, const u_char *addr)
59{
60	return GET_LINKADDR_STRING(addr, LINKADDR_IEEE1394, FIREWIRE_EUI64_LEN);
61}
62
63static void
64ap1394_hdr_print(netdissect_options *ndo, const u_char *bp, u_int length)
65{
66	const struct firewire_header *fp;
67	uint16_t firewire_type;
68
69	fp = (const struct firewire_header *)bp;
70
71	ND_PRINT("%s > %s",
72		     fwaddr_string(ndo, fp->firewire_shost),
73		     fwaddr_string(ndo, fp->firewire_dhost));
74
75	firewire_type = GET_BE_U_2(fp->firewire_type);
76	if (!ndo->ndo_qflag) {
77		ND_PRINT(", ethertype %s (0x%04x)",
78			       tok2str(ethertype_values,"Unknown", firewire_type),
79                               firewire_type);
80        } else {
81                ND_PRINT(", %s", tok2str(ethertype_values,"Unknown Ethertype (0x%04x)", firewire_type));
82        }
83
84	ND_PRINT(", length %u: ", length);
85}
86
87/*
88 * This is the top level routine of the printer.  'p' points
89 * to the ether header of the packet, 'h->ts' is the timestamp,
90 * 'h->len' is the length of the packet off the wire, and 'h->caplen'
91 * is the number of bytes actually captured.
92 */
93void
94ap1394_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h, const u_char *p)
95{
96	u_int length = h->len;
97	u_int caplen = h->caplen;
98	const struct firewire_header *fp;
99	u_short ether_type;
100	struct lladdr_info src, dst;
101
102	ndo->ndo_protocol = "ap1394";
103	ND_TCHECK_LEN(p, FIREWIRE_HDRLEN);
104	ndo->ndo_ll_hdr_len += FIREWIRE_HDRLEN;
105
106	if (ndo->ndo_eflag)
107		ap1394_hdr_print(ndo, p, length);
108
109	length -= FIREWIRE_HDRLEN;
110	caplen -= FIREWIRE_HDRLEN;
111	fp = (const struct firewire_header *)p;
112	p += FIREWIRE_HDRLEN;
113
114	ether_type = GET_BE_U_2(fp->firewire_type);
115	src.addr = fp->firewire_shost;
116	src.addr_string = fwaddr_string;
117	dst.addr = fp->firewire_dhost;
118	dst.addr_string = fwaddr_string;
119	if (ethertype_print(ndo, ether_type, p, length, caplen, &src, &dst) == 0) {
120		/* ether_type not known, print raw packet */
121		if (!ndo->ndo_eflag)
122			ap1394_hdr_print(ndo, (const u_char *)fp, length + FIREWIRE_HDRLEN);
123
124		if (!ndo->ndo_suppress_default_print)
125			ND_DEFAULTPRINT(p, caplen);
126	}
127}
128