1 /* 2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996 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 #ifndef lint 23 static char rcsid[] = 24 "@(#) $Header: print-udp.c,v 1.55 96/07/23 14:17:28 leres Exp $ (LBL)"; 25 #endif 26 27 #include <sys/param.h> 28 #include <sys/time.h> 29 #include <sys/socket.h> 30 31 #include <netinet/in.h> 32 #include <netinet/in_systm.h> 33 #include <netinet/ip.h> 34 #include <netinet/ip_var.h> 35 #include <netinet/udp.h> 36 #include <netinet/udp_var.h> 37 38 #undef NOERROR /* Solaris sucks */ 39 #undef T_UNSPEC /* SINIX does too */ 40 #include <arpa/nameser.h> 41 #include <arpa/tftp.h> 42 43 #include <rpc/rpc.h> 44 45 #include <stdio.h> 46 47 #include "interface.h" 48 #include "addrtoname.h" 49 #include "appletalk.h" 50 51 #include "nfsv2.h" 52 #include "bootp.h" 53 54 struct rtcphdr { 55 u_short rh_flags; /* T:2 P:1 CNT:5 PT:8 */ 56 u_short rh_len; /* length of message (in bytes) */ 57 u_int rh_ssrc; /* synchronization src id */ 58 }; 59 60 typedef struct { 61 u_int upper; /* more significant 32 bits */ 62 u_int lower; /* less significant 32 bits */ 63 } ntp64; 64 65 /* 66 * Sender report. 67 */ 68 struct rtcp_sr { 69 ntp64 sr_ntp; /* 64-bit ntp timestamp */ 70 u_int sr_ts; /* reference media timestamp */ 71 u_int sr_np; /* no. packets sent */ 72 u_int sr_nb; /* no. bytes sent */ 73 }; 74 75 /* 76 * Receiver report. 77 * Time stamps are middle 32-bits of ntp timestamp. 78 */ 79 struct rtcp_rr { 80 u_int rr_srcid; /* sender being reported */ 81 u_int rr_nr; /* no. packets received */ 82 u_int rr_np; /* no. packets predicted */ 83 u_int rr_dv; /* jitter (delay variance) */ 84 u_int rr_lsr; /* orig. ts from last rr from this src */ 85 u_int rr_dlsr; /* time from recpt of last rr to xmit time */ 86 }; 87 88 /*XXX*/ 89 #define RTCP_PT_SR 0 90 #define RTCP_PT_RR 1 91 #define RTCP_PT_SDES 2 92 #define RTCP_SDES_CNAME 1 93 #define RTCP_SDES_NAME 2 94 #define RTCP_SDES_EMAIL 3 95 #define RTCP_SDES_PHONE 4 96 #define RTCP_SDES_LOC 5 97 #define RTCP_SDES_TOOL 6 98 #define RTCP_SDES_TXT 7 99 #define RTCP_PT_BYE 3 100 #define RTCP_PT_APP 4 101 102 static void 103 vat_print(const void *hdr, u_int len, register const struct udphdr *up) 104 { 105 /* vat/vt audio */ 106 u_int ts = *(u_short *)hdr; 107 if ((ts & 0xf060) != 0) { 108 /* probably vt */ 109 (void)printf(" udp/vt %u %d / %d", 110 (u_int32_t)(ntohs(up->uh_ulen) - sizeof(*up)), 111 ts & 0x3ff, ts >> 10); 112 } else { 113 /* probably vat */ 114 u_int i0 = ntohl(((u_int *)hdr)[0]); 115 u_int i1 = ntohl(((u_int *)hdr)[1]); 116 printf(" udp/vat %u c%d %u%s", 117 (u_int32_t)(ntohs(up->uh_ulen) - sizeof(*up) - 8), 118 i0 & 0xffff, 119 i1, i0 & 0x800000? "*" : ""); 120 /* audio format */ 121 if (i0 & 0x1f0000) 122 printf(" f%d", (i0 >> 16) & 0x1f); 123 if (i0 & 0x3f000000) 124 printf(" s%d", (i0 >> 24) & 0x3f); 125 } 126 } 127 128 static void 129 rtp_print(const void *hdr, u_int len, register const struct udphdr *up) 130 { 131 /* rtp v1 or v2 */ 132 u_int *ip = (u_int *)hdr; 133 u_int hasopt, contype, hasmarker; 134 u_int i0 = ntohl(((u_int *)hdr)[0]); 135 u_int i1 = ntohl(((u_int *)hdr)[1]); 136 u_int dlen = ntohs(up->uh_ulen) - sizeof(*up) - 8; 137 const char* ptype; 138 139 ip += 2; 140 len >>= 2; 141 len -= 2; 142 if ((i0 >> 30) == 1) { 143 /* rtp v1 */ 144 hasopt = i0 & 0x800000; 145 contype = (i0 >> 16) & 0x3f; 146 hasmarker = i0 & 0x400000; 147 ptype = "rtpv1"; 148 } else { /*XXX*/ 149 /* rtp v2 */ 150 hasopt = i0 & 0x20000000; 151 contype = (i0 >> 16) & 0x7f; 152 hasmarker = i0 & 0x800000; 153 dlen -= 4; 154 ptype = "rtp"; 155 ip += 1; 156 len -= 1; 157 } 158 printf(" udp/%s %d c%d %s%s %d", 159 ptype, 160 dlen, 161 contype, 162 hasopt? "+" : "", 163 hasmarker? "*" : "", 164 i0 & 0xffff); 165 if (vflag) { 166 if (hasopt) { 167 u_int i2, optlen; 168 do { 169 i2 = ip[0]; 170 optlen = (i2 >> 16) & 0xff; 171 if (optlen == 0 || optlen > len) { 172 printf(" !opt"); 173 return; 174 } 175 ip += optlen; 176 } while ((int)i2 >= 0); 177 } 178 if (contype == 0x1f) 179 printf(" 0x%04x", ip[0] >> 16); 180 printf(" %u", i1); 181 } 182 } 183 184 static const u_char* 185 rtcp_print(const u_char *hdr) 186 { 187 /* rtp v2 control (rtcp) */ 188 struct rtcp_rr* rr = 0; 189 struct rtcp_sr* sr; 190 struct rtcphdr* rh = (struct rtcphdr*)hdr; 191 u_int len = (ntohs(rh->rh_len) + 1) * 4; 192 u_short flags = ntohs(rh->rh_flags); 193 int cnt = (flags >> 8) & 0x1f; 194 double ts, dts, jitter; 195 if (vflag) 196 printf(" %u", (u_int32_t)ntohl(rh->rh_ssrc)); 197 switch (flags & 0xff) { 198 case RTCP_PT_SR: 199 sr = (struct rtcp_sr*)(rh + 1); 200 printf(" sr"); 201 if (len != cnt * sizeof(*rr) + sizeof(*sr) + sizeof(*rh)) 202 printf(" [%d]", len); 203 ts = (double)((u_int32_t)ntohl(sr->sr_ts)) / 65536.; 204 printf(" @%.2f %up %ub", ts, (u_int32_t)ntohl(sr->sr_np), 205 (u_int32_t)ntohl(sr->sr_nb)); 206 rr = (struct rtcp_rr*)(sr + 1); 207 break; 208 case RTCP_PT_RR: 209 printf(" rr"); 210 if (len != cnt * sizeof(*rr) + sizeof(*rh)) 211 printf(" [%d]", len); 212 rr = (struct rtcp_rr*)(rh + 1); 213 break; 214 case RTCP_PT_SDES: 215 printf(" sdes %d", len); 216 cnt = 0; 217 break; 218 case RTCP_PT_BYE: 219 printf(" bye %d", len); 220 cnt = 0; 221 break; 222 default: 223 printf(" type-0x%x %d", flags & 0xff, len); 224 cnt = 0; 225 break; 226 } 227 if (cnt > 1) 228 printf(" c%d", cnt); 229 while (--cnt >= 0) { 230 if ((u_char*)(rr + 1) > snapend) { 231 printf(" [|rtcp]"); 232 return (snapend); 233 } 234 if (vflag) 235 printf(" %u", (u_int32_t)ntohl(rr->rr_srcid)); 236 ts = (double)((u_int32_t)ntohl(rr->rr_lsr)) / 65536.; 237 dts = (double)((u_int32_t)ntohl(rr->rr_dlsr)) / 65536.; 238 jitter = (double)((u_int32_t)ntohl(rr->rr_dv)) / 65536.; 239 printf(" %ur %ue %.2fj @%.2f+%.2f", 240 (u_int32_t)ntohl(rr->rr_nr), 241 (u_int32_t)ntohl(rr->rr_np), 242 jitter, ts, dts); 243 } 244 return (hdr + len); 245 } 246 247 /* XXX probably should use getservbyname() and cache answers */ 248 #define TFTP_PORT 69 /*XXX*/ 249 #define KERBEROS_PORT 88 /*XXX*/ 250 #define SUNRPC_PORT 111 /*XXX*/ 251 #define SNMP_PORT 161 /*XXX*/ 252 #define NTP_PORT 123 /*XXX*/ 253 #define SNMPTRAP_PORT 162 /*XXX*/ 254 #define RIP_PORT 520 /*XXX*/ 255 #define KERBEROS_SEC_PORT 750 /*XXX*/ 256 257 void 258 udp_print(register const u_char *bp, u_int length, register const u_char *bp2) 259 { 260 register const struct udphdr *up; 261 register const struct ip *ip; 262 register const u_char *cp; 263 u_short sport, dport, ulen; 264 265 up = (struct udphdr *)bp; 266 ip = (struct ip *)bp2; 267 cp = (u_char *)(up + 1); 268 if (cp > snapend) { 269 printf("[|udp]"); 270 return; 271 } 272 if (length < sizeof(struct udphdr)) { 273 (void)printf(" truncated-udp %d", length); 274 return; 275 } 276 length -= sizeof(struct udphdr); 277 278 sport = ntohs(up->uh_sport); 279 dport = ntohs(up->uh_dport); 280 ulen = ntohs(up->uh_ulen); 281 if (packettype) { 282 register struct rpc_msg *rp; 283 enum msg_type direction; 284 285 switch (packettype) { 286 287 case PT_VAT: 288 (void)printf("%s.%s > %s.%s:", 289 ipaddr_string(&ip->ip_src), 290 udpport_string(sport), 291 ipaddr_string(&ip->ip_dst), 292 udpport_string(dport)); 293 vat_print((void *)(up + 1), length, up); 294 break; 295 296 case PT_WB: 297 (void)printf("%s.%s > %s.%s:", 298 ipaddr_string(&ip->ip_src), 299 udpport_string(sport), 300 ipaddr_string(&ip->ip_dst), 301 udpport_string(dport)); 302 wb_print((void *)(up + 1), length); 303 break; 304 305 case PT_RPC: 306 rp = (struct rpc_msg *)(up + 1); 307 direction = (enum msg_type)ntohl(rp->rm_direction); 308 if (direction == CALL) 309 sunrpcrequest_print((u_char *)rp, length, 310 (u_char *)ip); 311 else 312 nfsreply_print((u_char *)rp, length, 313 (u_char *)ip); /*XXX*/ 314 break; 315 316 case PT_RTP: 317 (void)printf("%s.%s > %s.%s:", 318 ipaddr_string(&ip->ip_src), 319 udpport_string(sport), 320 ipaddr_string(&ip->ip_dst), 321 udpport_string(dport)); 322 rtp_print((void *)(up + 1), length, up); 323 break; 324 325 case PT_RTCP: 326 (void)printf("%s.%s > %s.%s:", 327 ipaddr_string(&ip->ip_src), 328 udpport_string(sport), 329 ipaddr_string(&ip->ip_dst), 330 udpport_string(dport)); 331 while (cp < snapend) 332 cp = rtcp_print(cp); 333 break; 334 } 335 return; 336 } 337 338 if (! qflag) { 339 register struct rpc_msg *rp; 340 enum msg_type direction; 341 342 rp = (struct rpc_msg *)(up + 1); 343 TCHECK(rp->rm_direction); 344 direction = (enum msg_type)ntohl(rp->rm_direction); 345 if (dport == NFS_PORT && direction == CALL) { 346 nfsreq_print((u_char *)rp, length, (u_char *)ip); 347 return; 348 } 349 else if (sport == NFS_PORT && direction == REPLY) { 350 nfsreply_print((u_char *)rp, length, (u_char *)ip); 351 return; 352 } 353 #ifdef notdef 354 else if (dport == SUNRPC_PORT && direction == CALL) { 355 sunrpcrequest_print((u_char *)rp, length, (u_char *)ip); 356 return; 357 } 358 #endif 359 else { 360 TCHECK2(cp[0], 1); 361 if (((struct LAP *)cp)->type == lapDDP && 362 (atalk_port(sport) || atalk_port(dport))) { 363 if (vflag) 364 fputs("kip ", stdout); 365 atalk_print(cp, length); 366 return; 367 } 368 } 369 } 370 (void)printf("%s.%s > %s.%s:", 371 ipaddr_string(&ip->ip_src), udpport_string(sport), 372 ipaddr_string(&ip->ip_dst), udpport_string(dport)); 373 374 if (!qflag) { 375 #define ISPORT(p) (dport == (p) || sport == (p)) 376 if (ISPORT(NAMESERVER_PORT)) 377 ns_print((const u_char *)(up + 1), length); 378 else if (ISPORT(TFTP_PORT)) 379 tftp_print((const u_char *)(up + 1), length); 380 else if (ISPORT(IPPORT_BOOTPC) || ISPORT(IPPORT_BOOTPS)) 381 bootp_print((const u_char *)(up + 1), length, 382 sport, dport); 383 else if (ISPORT(RIP_PORT)) 384 rip_print((const u_char *)(up + 1), length); 385 else if (ISPORT(SNMP_PORT) || ISPORT(SNMPTRAP_PORT)) 386 snmp_print((const u_char *)(up + 1), length); 387 else if (ISPORT(NTP_PORT)) 388 ntp_print((const u_char *)(up + 1), length); 389 else if (ISPORT(KERBEROS_PORT) || ISPORT(KERBEROS_SEC_PORT)) 390 krb_print((const void *)(up + 1), length); 391 else if (dport == 3456) 392 vat_print((const void *)(up + 1), length, up); 393 /* 394 * Kludge in test for whiteboard packets. 395 */ 396 else if (dport == 4567) 397 wb_print((const void *)(up + 1), length); 398 else 399 (void)printf(" udp %u", 400 (u_int32_t)(ulen - sizeof(*up))); 401 #undef ISPORT 402 } else 403 (void)printf(" udp %u", (u_int32_t)(ulen - sizeof(*up))); 404 return; 405 trunc: 406 fputs("[|udp]", stdout); 407 } 408