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 #ifndef lint 22 static const char rcsid[] = 23 "@(#) $Header: /tcpdump/master/tcpdump/print-sll.c,v 1.3 2000/12/23 20:49:34 guy Exp $ (LBL)"; 24 #endif 25 26 #ifdef HAVE_CONFIG_H 27 #include "config.h" 28 #endif 29 30 #include <sys/param.h> 31 #include <sys/time.h> 32 #include <sys/socket.h> 33 34 struct mbuf; 35 struct rtentry; 36 37 #include <netinet/in.h> 38 39 #include <stdio.h> 40 #include <string.h> 41 #include <pcap.h> 42 43 #include "interface.h" 44 #include "addrtoname.h" 45 #include "ethertype.h" 46 47 #include "ether.h" 48 #include "sll.h" 49 50 const u_char *packetp; 51 const u_char *snapend; 52 53 static inline void 54 sll_print(register const struct sll_header *sllp, u_int length) 55 { 56 u_short halen; 57 58 switch (ntohs(sllp->sll_pkttype)) { 59 60 case LINUX_SLL_HOST: 61 (void)printf("< "); 62 break; 63 64 case LINUX_SLL_BROADCAST: 65 (void)printf("B "); 66 break; 67 68 case LINUX_SLL_MULTICAST: 69 (void)printf("M "); 70 break; 71 72 case LINUX_SLL_OTHERHOST: 73 (void)printf("P "); 74 break; 75 76 case LINUX_SLL_OUTGOING: 77 (void)printf("> "); 78 break; 79 80 default: 81 (void)printf("? "); 82 break; 83 } 84 85 /* 86 * XXX - check the link-layer address type value? 87 * For now, we just assume 6 means Ethernet. 88 * XXX - print others as strings of hex? 89 */ 90 halen = ntohs(sllp->sll_halen); 91 if (halen == 6) 92 (void)printf("%s ", etheraddr_string(sllp->sll_addr)); 93 94 if (!qflag) 95 (void)printf("%s ", etherproto_string(sllp->sll_protocol)); 96 (void)printf("%d: ", length); 97 } 98 99 /* 100 * This is the top level routine of the printer. 'p' is the points 101 * to the ether header of the packet, 'h->tv' is the timestamp, 102 * 'h->length' is the length of the packet off the wire, and 'h->caplen' 103 * is the number of bytes actually captured. 104 */ 105 void 106 sll_if_print(u_char *user, const struct pcap_pkthdr *h, const u_char *p) 107 { 108 u_int caplen = h->caplen; 109 u_int length = h->len; 110 register const struct sll_header *sllp; 111 u_short pkttype; 112 struct ether_header ehdr; 113 u_short ether_type; 114 u_short extracted_ethertype; 115 116 ts_print(&h->ts); 117 118 if (caplen < SLL_HDR_LEN) { 119 /* 120 * XXX - this "can't happen" because "pcap-linux.c" always 121 * adds this many bytes of header to every packet in a 122 * cooked socket capture. 123 */ 124 printf("[|sll]"); 125 goto out; 126 } 127 128 sllp = (const struct sll_header *)p; 129 130 /* 131 * Fake up an Ethernet header for the benefit of printers that 132 * insist on "packetp" pointing to an Ethernet header. 133 */ 134 pkttype = ntohs(sllp->sll_pkttype); 135 136 /* The source address is in the packet header */ 137 memcpy(ehdr.ether_shost, sllp->sll_addr, ETHER_ADDR_LEN); 138 139 if (pkttype != LINUX_SLL_OUTGOING) { 140 /* 141 * We received this packet. 142 * 143 * We don't know the destination address, so 144 * we fake it - all 0's except that the 145 * bottommost bit of the bottommost octet 146 * is set for a unicast packet, all 0's except 147 * that the bottommost bit of the uppermost 148 * octet is set for a multicast packet, all 149 * 1's for a broadcast packet. 150 */ 151 if (pkttype == LINUX_SLL_BROADCAST) 152 memset(ehdr.ether_dhost, 0xFF, ETHER_ADDR_LEN); 153 else { 154 memset(ehdr.ether_dhost, 0, ETHER_ADDR_LEN); 155 if (pkttype == LINUX_SLL_MULTICAST) 156 ehdr.ether_dhost[0] = 1; 157 else 158 ehdr.ether_dhost[ETHER_ADDR_LEN-1] = 1; 159 } 160 } else { 161 /* 162 * We sent this packet; we don't know whether it's 163 * broadcast, multicast, or unicast, so just make 164 * the destination address all 0's. 165 */ 166 memset(ehdr.ether_dhost, 0, ETHER_ADDR_LEN); 167 } 168 169 if (eflag) 170 sll_print(sllp, length); 171 172 /* 173 * Some printers want to get back at the ethernet addresses, 174 * and/or check that they're not walking off the end of the packet. 175 * Rather than pass them all the way down, we set these globals. 176 */ 177 snapend = p + caplen; 178 /* 179 * Actually, the only printers that use packetp are print-arp.c 180 * and print-bootp.c, and they assume that packetp points to an 181 * Ethernet header. The right thing to do is to fix them to know 182 * which link type is in use when they excavate. XXX 183 */ 184 packetp = (u_char *)&ehdr; 185 186 length -= SLL_HDR_LEN; 187 caplen -= SLL_HDR_LEN; 188 p += SLL_HDR_LEN; 189 190 ether_type = ntohs(sllp->sll_protocol); 191 192 /* 193 * Is it (gag) an 802.3 encapsulation, or some non-Ethernet 194 * packet type? 195 */ 196 extracted_ethertype = 0; 197 if (ether_type <= ETHERMTU) { 198 /* 199 * Yes - what type is it? 200 */ 201 switch (ether_type) { 202 203 case LINUX_SLL_P_802_2: 204 /* 205 * 802.2. 206 * Try to print the LLC-layer header & higher layers. 207 */ 208 if (llc_print(p, length, caplen, ESRC(&ehdr), 209 EDST(&ehdr), &extracted_ethertype) == 0) 210 goto unknown; /* unknown LLC type */ 211 break; 212 213 default: 214 unknown: 215 /* ether_type not known, print raw packet */ 216 if (!eflag) 217 sll_print(sllp, length + SLL_HDR_LEN); 218 if (extracted_ethertype) { 219 printf("(LLC %s) ", 220 etherproto_string(htons(extracted_ethertype))); 221 } 222 if (!xflag && !qflag) 223 default_print(p, caplen); 224 break; 225 } 226 } else if (ether_encap_print(ether_type, p, length, caplen, 227 &extracted_ethertype) == 0) { 228 /* ether_type not known, print raw packet */ 229 if (!eflag) 230 sll_print(sllp, length + SLL_HDR_LEN); 231 if (!xflag && !qflag) 232 default_print(p, caplen); 233 } 234 if (xflag) 235 default_print(p, caplen); 236 out: 237 putchar('\n'); 238 } 239