1 /* 2 * Copyright (c) 1989, 1990, 1991, 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 22 #include <sys/cdefs.h> 23 #ifndef lint 24 __RCSID("$NetBSD: print-sl.c,v 1.11 2024/09/02 16:15:33 christos Exp $"); 25 #endif 26 27 /* \summary: Compressed Serial Line Internet Protocol printer */ 28 29 #include <config.h> 30 31 #include "netdissect-stdinc.h" 32 33 #define ND_LONGJMP_FROM_TCHECK 34 #include "netdissect.h" 35 #include "extract.h" 36 37 #include "ip.h" 38 #include "tcp.h" 39 #include "slcompress.h" 40 41 /* 42 * definitions of the pseudo- link-level header attached to slip 43 * packets grabbed by the packet filter (bpf) traffic monitor. 44 */ 45 #define SLIP_HDRLEN 16 46 47 #define SLX_DIR 0 48 #define SLX_CHDR 1 49 50 #define SLIPDIR_IN 0 51 #define SLIPDIR_OUT 1 52 53 54 static u_int lastlen[2][256]; 55 static u_int lastconn = 255; 56 57 static void sliplink_print(netdissect_options *, const u_char *, const struct ip *, u_int); 58 static void compressed_sl_print(netdissect_options *, const u_char *, const struct ip *, u_int, int); 59 60 void 61 sl_if_print(netdissect_options *ndo, 62 const struct pcap_pkthdr *h, const u_char *p) 63 { 64 u_int length = h->len; 65 const struct ip *ip; 66 67 ndo->ndo_protocol = "slip"; 68 ND_TCHECK_LEN(p, SLIP_HDRLEN); 69 ndo->ndo_ll_hdr_len += SLIP_HDRLEN; 70 71 length -= SLIP_HDRLEN; 72 73 ip = (const struct ip *)(p + SLIP_HDRLEN); 74 75 if (ndo->ndo_eflag) 76 sliplink_print(ndo, p, ip, length); 77 78 switch (IP_V(ip)) { 79 case 4: 80 ip_print(ndo, (const u_char *)ip, length); 81 break; 82 case 6: 83 ip6_print(ndo, (const u_char *)ip, length); 84 break; 85 default: 86 ND_PRINT("ip v%u", IP_V(ip)); 87 } 88 } 89 90 void 91 sl_bsdos_if_print(netdissect_options *ndo, 92 const struct pcap_pkthdr *h, const u_char *p) 93 { 94 u_int length = h->len; 95 const struct ip *ip; 96 97 ndo->ndo_protocol = "slip_bsdos"; 98 ND_TCHECK_LEN(p, SLIP_HDRLEN); 99 ndo->ndo_ll_hdr_len += SLIP_HDRLEN; 100 101 length -= SLIP_HDRLEN; 102 103 ip = (const struct ip *)(p + SLIP_HDRLEN); 104 105 #ifdef notdef 106 if (ndo->ndo_eflag) 107 sliplink_print(ndo, p, ip, length); 108 #endif 109 110 ip_print(ndo, (const u_char *)ip, length); 111 } 112 113 static void 114 sliplink_print(netdissect_options *ndo, 115 const u_char *p, const struct ip *ip, 116 u_int length) 117 { 118 int dir; 119 u_int hlen; 120 121 dir = GET_U_1(p + SLX_DIR); 122 switch (dir) { 123 124 case SLIPDIR_IN: 125 ND_PRINT("I "); 126 break; 127 128 case SLIPDIR_OUT: 129 ND_PRINT("O "); 130 break; 131 132 default: 133 ND_PRINT("Invalid direction %d ", dir); 134 dir = -1; 135 break; 136 } 137 switch (GET_U_1(p + SLX_CHDR) & 0xf0) { 138 139 case TYPE_IP: 140 ND_PRINT("ip %u: ", length + SLIP_HDRLEN); 141 break; 142 143 case TYPE_UNCOMPRESSED_TCP: 144 /* 145 * The connection id is stored in the IP protocol field. 146 * Get it from the link layer since sl_uncompress_tcp() 147 * has restored the IP header copy to IPPROTO_TCP. 148 */ 149 lastconn = GET_U_1(((const struct ip *)(p + SLX_CHDR))->ip_p); 150 ND_PRINT("utcp %u: ", lastconn); 151 if (dir == -1) { 152 /* Direction is bogus, don't use it */ 153 return; 154 } 155 ND_TCHECK_SIZE(ip); 156 hlen = IP_HL(ip); 157 ND_TCHECK_SIZE((const struct tcphdr *)&((const int *)ip)[hlen]); 158 hlen += TH_OFF((const struct tcphdr *)&((const int *)ip)[hlen]); 159 lastlen[dir][lastconn] = length - (hlen << 2); 160 break; 161 162 default: 163 if (dir == -1) { 164 /* Direction is bogus, don't use it */ 165 return; 166 } 167 if (GET_U_1(p + SLX_CHDR) & TYPE_COMPRESSED_TCP) { 168 compressed_sl_print(ndo, p + SLX_CHDR, ip, length, dir); 169 ND_PRINT(": "); 170 } else 171 ND_PRINT("slip-%u!: ", GET_U_1(p + SLX_CHDR)); 172 } 173 } 174 175 static const u_char * 176 print_sl_change(netdissect_options *ndo, 177 const char *str, const u_char *cp) 178 { 179 u_int i; 180 181 if ((i = GET_U_1(cp)) == 0) { 182 cp++; 183 i = GET_BE_U_2(cp); 184 cp += 2; 185 } 186 ND_PRINT(" %s%u", str, i); 187 return (cp); 188 } 189 190 static const u_char * 191 print_sl_winchange(netdissect_options *ndo, 192 const u_char *cp) 193 { 194 int16_t i; 195 196 if ((i = GET_U_1(cp)) == 0) { 197 cp++; 198 i = GET_BE_S_2(cp); 199 cp += 2; 200 } 201 if (i >= 0) 202 ND_PRINT(" W+%d", i); 203 else 204 ND_PRINT(" W%d", i); 205 return (cp); 206 } 207 208 static void 209 compressed_sl_print(netdissect_options *ndo, 210 const u_char *chdr, const struct ip *ip, 211 u_int length, int dir) 212 { 213 const u_char *cp = chdr; 214 u_int flags, hlen; 215 216 flags = GET_U_1(cp); 217 cp++; 218 if (flags & NEW_C) { 219 lastconn = GET_U_1(cp); 220 cp++; 221 ND_PRINT("ctcp %u", lastconn); 222 } else 223 ND_PRINT("ctcp *"); 224 225 /* skip tcp checksum */ 226 cp += 2; 227 228 switch (flags & SPECIALS_MASK) { 229 case SPECIAL_I: 230 ND_PRINT(" *SA+%u", lastlen[dir][lastconn]); 231 break; 232 233 case SPECIAL_D: 234 ND_PRINT(" *S+%u", lastlen[dir][lastconn]); 235 break; 236 237 default: 238 if (flags & NEW_U) 239 cp = print_sl_change(ndo, "U=", cp); 240 if (flags & NEW_W) 241 cp = print_sl_winchange(ndo, cp); 242 if (flags & NEW_A) 243 cp = print_sl_change(ndo, "A+", cp); 244 if (flags & NEW_S) 245 cp = print_sl_change(ndo, "S+", cp); 246 break; 247 } 248 if (flags & NEW_I) 249 cp = print_sl_change(ndo, "I+", cp); 250 251 /* 252 * 'hlen' is the length of the uncompressed TCP/IP header (in words). 253 * 'cp - chdr' is the length of the compressed header. 254 * 'length - hlen' is the amount of data in the packet. 255 */ 256 ND_TCHECK_SIZE(ip); 257 hlen = IP_HL(ip); 258 ND_TCHECK_SIZE((const struct tcphdr *)&((const int32_t *)ip)[hlen]); 259 hlen += TH_OFF((const struct tcphdr *)&((const int32_t *)ip)[hlen]); 260 lastlen[dir][lastconn] = length - (hlen << 2); 261 ND_PRINT(" %u (%ld)", lastlen[dir][lastconn], (long)(cp - chdr)); 262 } 263