]> The Tcpdump Group git mirrors - tcpdump/blob - print-udp.c
- enable build outside of the tree
[tcpdump] / print-udp.c
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
22 #ifndef lint
23 static const char rcsid[] =
24 "@(#) $Header: /tcpdump/master/tcpdump/print-udp.c,v 1.63 1999-10-30 05:11:21 itojun 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 #ifdef NOERROR
39 #undef NOERROR /* Solaris sucks */
40 #endif
41 #ifdef T_UNSPEC
42 #undef T_UNSPEC /* SINIX does too */
43 #endif
44 #include <arpa/nameser.h>
45 #ifdef SEGSIZE
46 #undef SEGSIZE
47 #endif
48 #include <arpa/tftp.h>
49
50 #include <rpc/rpc.h>
51
52 #include <stdio.h>
53
54 #ifdef INET6
55 #include <netinet/ip6.h>
56 #endif
57
58 #include "interface.h"
59 #include "addrtoname.h"
60 #include "appletalk.h"
61
62 #include "nfsv2.h"
63 #include "bootp.h"
64
65 struct rtcphdr {
66 u_short rh_flags; /* T:2 P:1 CNT:5 PT:8 */
67 u_short rh_len; /* length of message (in words) */
68 u_int rh_ssrc; /* synchronization src id */
69 };
70
71 typedef struct {
72 u_int upper; /* more significant 32 bits */
73 u_int lower; /* less significant 32 bits */
74 } ntp64;
75
76 /*
77 * Sender report.
78 */
79 struct rtcp_sr {
80 ntp64 sr_ntp; /* 64-bit ntp timestamp */
81 u_int sr_ts; /* reference media timestamp */
82 u_int sr_np; /* no. packets sent */
83 u_int sr_nb; /* no. bytes sent */
84 };
85
86 /*
87 * Receiver report.
88 * Time stamps are middle 32-bits of ntp timestamp.
89 */
90 struct rtcp_rr {
91 u_int rr_srcid; /* sender being reported */
92 u_int rr_nl; /* no. packets lost */
93 u_int rr_ls; /* extended last seq number received */
94 u_int rr_dv; /* jitter (delay variance) */
95 u_int rr_lsr; /* orig. ts from last rr from this src */
96 u_int rr_dlsr; /* time from recpt of last rr to xmit time */
97 };
98
99 /*XXX*/
100 #define RTCP_PT_SR 200
101 #define RTCP_PT_RR 201
102 #define RTCP_PT_SDES 202
103 #define RTCP_SDES_CNAME 1
104 #define RTCP_SDES_NAME 2
105 #define RTCP_SDES_EMAIL 3
106 #define RTCP_SDES_PHONE 4
107 #define RTCP_SDES_LOC 5
108 #define RTCP_SDES_TOOL 6
109 #define RTCP_SDES_NOTE 7
110 #define RTCP_SDES_PRIV 8
111 #define RTCP_PT_BYE 203
112 #define RTCP_PT_APP 204
113
114 static void
115 vat_print(const void *hdr, u_int len, register const struct udphdr *up)
116 {
117 /* vat/vt audio */
118 u_int ts = *(u_short *)hdr;
119 if ((ts & 0xf060) != 0) {
120 /* probably vt */
121 (void)printf(" udp/vt %u %d / %d",
122 (u_int32_t)(ntohs(up->uh_ulen) - sizeof(*up)),
123 ts & 0x3ff, ts >> 10);
124 } else {
125 /* probably vat */
126 u_int i0 = ntohl(((u_int *)hdr)[0]);
127 u_int i1 = ntohl(((u_int *)hdr)[1]);
128 printf(" udp/vat %u c%d %u%s",
129 (u_int32_t)(ntohs(up->uh_ulen) - sizeof(*up) - 8),
130 i0 & 0xffff,
131 i1, i0 & 0x800000? "*" : "");
132 /* audio format */
133 if (i0 & 0x1f0000)
134 printf(" f%d", (i0 >> 16) & 0x1f);
135 if (i0 & 0x3f000000)
136 printf(" s%d", (i0 >> 24) & 0x3f);
137 }
138 }
139
140 static void
141 rtp_print(const void *hdr, u_int len, register const struct udphdr *up)
142 {
143 /* rtp v1 or v2 */
144 u_int *ip = (u_int *)hdr;
145 u_int hasopt, hasext, contype, hasmarker;
146 u_int i0 = ntohl(((u_int *)hdr)[0]);
147 u_int i1 = ntohl(((u_int *)hdr)[1]);
148 u_int dlen = ntohs(up->uh_ulen) - sizeof(*up) - 8;
149 const char * ptype;
150
151 ip += 2;
152 len >>= 2;
153 len -= 2;
154 hasopt = 0;
155 hasext = 0;
156 if ((i0 >> 30) == 1) {
157 /* rtp v1 */
158 hasopt = i0 & 0x800000;
159 contype = (i0 >> 16) & 0x3f;
160 hasmarker = i0 & 0x400000;
161 ptype = "rtpv1";
162 } else {
163 /* rtp v2 */
164 hasext = i0 & 0x10000000;
165 contype = (i0 >> 16) & 0x7f;
166 hasmarker = i0 & 0x800000;
167 dlen -= 4;
168 ptype = "rtp";
169 ip += 1;
170 len -= 1;
171 }
172 printf(" udp/%s %d c%d %s%s %d %u",
173 ptype,
174 dlen,
175 contype,
176 (hasopt || hasext)? "+" : "",
177 hasmarker? "*" : "",
178 i0 & 0xffff,
179 i1);
180 if (vflag) {
181 printf(" %u", i1);
182 if (hasopt) {
183 u_int i2, optlen;
184 do {
185 i2 = ip[0];
186 optlen = (i2 >> 16) & 0xff;
187 if (optlen == 0 || optlen > len) {
188 printf(" !opt");
189 return;
190 }
191 ip += optlen;
192 len -= optlen;
193 } while ((int)i2 >= 0);
194 }
195 if (hasext) {
196 u_int i2, extlen;
197 i2 = ip[0];
198 extlen = (i2 & 0xffff) + 1;
199 if (extlen > len) {
200 printf(" !ext");
201 return;
202 }
203 ip += extlen;
204 }
205 if (contype == 0x1f) /*XXX H.261 */
206 printf(" 0x%04x", ip[0] >> 16);
207 }
208 }
209
210 static const u_char *
211 rtcp_print(const u_char *hdr, const u_char *ep)
212 {
213 /* rtp v2 control (rtcp) */
214 struct rtcp_rr *rr = 0;
215 struct rtcp_sr *sr;
216 struct rtcphdr *rh = (struct rtcphdr *)hdr;
217 u_int len;
218 u_short flags;
219 int cnt;
220 double ts, dts;
221 if ((u_char *)(rh + 1) > ep) {
222 printf(" [|rtcp]");
223 return (ep);
224 }
225 len = (ntohs(rh->rh_len) + 1) * 4;
226 flags = ntohs(rh->rh_flags);
227 cnt = (flags >> 8) & 0x1f;
228 switch (flags & 0xff) {
229 case RTCP_PT_SR:
230 sr = (struct rtcp_sr *)(rh + 1);
231 printf(" sr");
232 if (len != cnt * sizeof(*rr) + sizeof(*sr) + sizeof(*rh))
233 printf(" [%d]", len);
234 if (vflag)
235 printf(" %u", (u_int32_t)ntohl(rh->rh_ssrc));
236 if ((u_char *)(sr + 1) > ep) {
237 printf(" [|rtcp]");
238 return (ep);
239 }
240 ts = (double)((u_int32_t)ntohl(sr->sr_ntp.upper)) +
241 ((double)((u_int32_t)ntohl(sr->sr_ntp.lower)) /
242 4294967296.0);
243 printf(" @%.2f %u %up %ub", ts, (u_int32_t)ntohl(sr->sr_ts),
244 (u_int32_t)ntohl(sr->sr_np), (u_int32_t)ntohl(sr->sr_nb));
245 rr = (struct rtcp_rr *)(sr + 1);
246 break;
247 case RTCP_PT_RR:
248 printf(" rr");
249 if (len != cnt * sizeof(*rr) + sizeof(*rh))
250 printf(" [%d]", len);
251 rr = (struct rtcp_rr *)(rh + 1);
252 if (vflag)
253 printf(" %u", (u_int32_t)ntohl(rh->rh_ssrc));
254 break;
255 case RTCP_PT_SDES:
256 printf(" sdes %d", len);
257 if (vflag)
258 printf(" %u", (u_int32_t)ntohl(rh->rh_ssrc));
259 cnt = 0;
260 break;
261 case RTCP_PT_BYE:
262 printf(" bye %d", len);
263 if (vflag)
264 printf(" %u", (u_int32_t)ntohl(rh->rh_ssrc));
265 cnt = 0;
266 break;
267 default:
268 printf(" type-0x%x %d", flags & 0xff, len);
269 cnt = 0;
270 break;
271 }
272 if (cnt > 1)
273 printf(" c%d", cnt);
274 while (--cnt >= 0) {
275 if ((u_char *)(rr + 1) > ep) {
276 printf(" [|rtcp]");
277 return (ep);
278 }
279 if (vflag)
280 printf(" %u", (u_int32_t)ntohl(rr->rr_srcid));
281 ts = (double)((u_int32_t)ntohl(rr->rr_lsr)) / 65536.;
282 dts = (double)((u_int32_t)ntohl(rr->rr_dlsr)) / 65536.;
283 printf(" %ul %us %uj @%.2f+%.2f",
284 (u_int32_t)ntohl(rr->rr_nl) & 0x00ffffff,
285 (u_int32_t)ntohl(rr->rr_ls),
286 (u_int32_t)ntohl(rr->rr_dv), ts, dts);
287 }
288 return (hdr + len);
289 }
290
291 /* XXX probably should use getservbyname() and cache answers */
292 #define TFTP_PORT 69 /*XXX*/
293 #define KERBEROS_PORT 88 /*XXX*/
294 #define SUNRPC_PORT 111 /*XXX*/
295 #define SNMP_PORT 161 /*XXX*/
296 #define NTP_PORT 123 /*XXX*/
297 #define SNMPTRAP_PORT 162 /*XXX*/
298 #define ISAKMP_PORT 500 /*XXX*/
299 #define RIP_PORT 520 /*XXX*/
300 #define KERBEROS_SEC_PORT 750 /*XXX*/
301 #define L2TP_PORT 1701 /*XXX*/
302 #define ISAKMP_PORT_USER1 7500 /*??? - nonstandard*/
303 #define ISAKMP_PORT_USER2 8500 /*??? - nonstandard*/
304
305 #ifdef INET6
306 #define RIPNG_PORT 521 /*XXX*/
307 #define DHCP6_SERV_PORT 546 /*XXX*/
308 #define DHCP6_CLI_PORT 547 /*XXX*/
309 #endif
310
311 void
312 udp_print(register const u_char *bp, u_int length, register const u_char *bp2)
313 {
314 register const struct udphdr *up;
315 register const struct ip *ip;
316 register const u_char *cp;
317 register const u_char *ep = bp + length;
318 u_short sport, dport, ulen;
319 #ifdef INET6
320 register const struct ip6_hdr *ip6;
321 #endif
322
323 if (ep > snapend)
324 ep = snapend;
325 up = (struct udphdr *)bp;
326 ip = (struct ip *)bp2;
327 #ifdef INET6
328 if (ip->ip_v == 6)
329 ip6 = (struct ip6_hdr *)bp2;
330 else
331 ip6 = NULL;
332 #endif /*INET6*/
333 cp = (u_char *)(up + 1);
334 if (cp > snapend) {
335 printf("[|udp]");
336 return;
337 }
338 if (length < sizeof(struct udphdr)) {
339 (void)printf(" truncated-udp %d", length);
340 return;
341 }
342 length -= sizeof(struct udphdr);
343
344 sport = ntohs(up->uh_sport);
345 dport = ntohs(up->uh_dport);
346 ulen = ntohs(up->uh_ulen);
347 if (packettype) {
348 register struct rpc_msg *rp;
349 enum msg_type direction;
350
351 switch (packettype) {
352
353 case PT_VAT:
354 (void)printf("%s.%s > %s.%s:",
355 ipaddr_string(&ip->ip_src),
356 udpport_string(sport),
357 ipaddr_string(&ip->ip_dst),
358 udpport_string(dport));
359 vat_print((void *)(up + 1), length, up);
360 break;
361
362 case PT_WB:
363 (void)printf("%s.%s > %s.%s:",
364 ipaddr_string(&ip->ip_src),
365 udpport_string(sport),
366 ipaddr_string(&ip->ip_dst),
367 udpport_string(dport));
368 wb_print((void *)(up + 1), length);
369 break;
370
371 case PT_RPC:
372 rp = (struct rpc_msg *)(up + 1);
373 direction = (enum msg_type)ntohl(rp->rm_direction);
374 if (direction == CALL)
375 sunrpcrequest_print((u_char *)rp, length,
376 (u_char *)ip);
377 else
378 nfsreply_print((u_char *)rp, length,
379 (u_char *)ip); /*XXX*/
380 break;
381
382 case PT_RTP:
383 (void)printf("%s.%s > %s.%s:",
384 ipaddr_string(&ip->ip_src),
385 udpport_string(sport),
386 ipaddr_string(&ip->ip_dst),
387 udpport_string(dport));
388 rtp_print((void *)(up + 1), length, up);
389 break;
390
391 case PT_RTCP:
392 (void)printf("%s.%s > %s.%s:",
393 ipaddr_string(&ip->ip_src),
394 udpport_string(sport),
395 ipaddr_string(&ip->ip_dst),
396 udpport_string(dport));
397 while (cp < ep)
398 cp = rtcp_print(cp, ep);
399 break;
400
401 case PT_SNMP:
402 (void)printf("%s.%s > %s.%s:",
403 ipaddr_string(&ip->ip_src),
404 udpport_string(sport),
405 ipaddr_string(&ip->ip_dst),
406 udpport_string(dport));
407 snmp_print((const u_char *)(up + 1), length);
408 break;
409 }
410 return;
411 }
412
413 if (!qflag) {
414 register struct rpc_msg *rp;
415 enum msg_type direction;
416
417 rp = (struct rpc_msg *)(up + 1);
418 if (TTEST(rp->rm_direction)) {
419 direction = (enum msg_type)ntohl(rp->rm_direction);
420 if (dport == NFS_PORT && direction == CALL) {
421 nfsreq_print((u_char *)rp, length,
422 (u_char *)ip);
423 return;
424 }
425 if (sport == NFS_PORT && direction == REPLY) {
426 nfsreply_print((u_char *)rp, length,
427 (u_char *)ip);
428 return;
429 }
430 #ifdef notdef
431 if (dport == SUNRPC_PORT && direction == CALL) {
432 sunrpcrequest_print((u_char *)rp, length, (u_char *)ip);
433 return;
434 }
435 #endif
436 }
437 if (TTEST(((struct LAP *)cp)->type) &&
438 ((struct LAP *)cp)->type == lapDDP &&
439 (atalk_port(sport) || atalk_port(dport))) {
440 if (vflag)
441 fputs("kip ", stdout);
442 atalk_print(cp, length);
443 return;
444 }
445 }
446 #if 0
447 (void)printf("%s.%s > %s.%s:",
448 ipaddr_string(&ip->ip_src), udpport_string(sport),
449 ipaddr_string(&ip->ip_dst), udpport_string(dport));
450 #else
451 #ifdef INET6
452 if (ip6) {
453 if (ip6->ip6_nxt == IPPROTO_UDP) {
454 (void)printf("%s.%s > %s.%s: ",
455 ip6addr_string(&ip6->ip6_src),
456 udpport_string(sport),
457 ip6addr_string(&ip6->ip6_dst),
458 udpport_string(dport));
459 } else {
460 (void)printf("%s > %s: ",
461 udpport_string(sport), udpport_string(dport));
462 }
463 } else
464 #endif /*INET6*/
465 {
466 if (ip->ip_p == IPPROTO_UDP) {
467 (void)printf("%s.%s > %s.%s: ",
468 ipaddr_string(&ip->ip_src),
469 udpport_string(sport),
470 ipaddr_string(&ip->ip_dst),
471 udpport_string(dport));
472 } else {
473 (void)printf("%s > %s: ",
474 udpport_string(sport), udpport_string(dport));
475 }
476 }
477 #endif
478
479 if (!qflag) {
480 #define ISPORT(p) (dport == (p) || sport == (p))
481 if (ISPORT(NAMESERVER_PORT))
482 ns_print((const u_char *)(up + 1), length);
483 else if (ISPORT(TFTP_PORT))
484 tftp_print((const u_char *)(up + 1), length);
485 else if (ISPORT(IPPORT_BOOTPC) || ISPORT(IPPORT_BOOTPS))
486 bootp_print((const u_char *)(up + 1), length,
487 sport, dport);
488 else if (ISPORT(RIP_PORT))
489 rip_print((const u_char *)(up + 1), length);
490 else if (ISPORT(ISAKMP_PORT))
491 isakmp_print((const u_char *)(up + 1), length, bp2);
492 #if 1 /*???*/
493 else if (ISPORT(ISAKMP_PORT_USER1) || ISPORT(ISAKMP_PORT_USER2))
494 isakmp_print((const u_char *)(up + 1), length, bp2);
495 #endif
496 else if (ISPORT(SNMP_PORT) || ISPORT(SNMPTRAP_PORT))
497 snmp_print((const u_char *)(up + 1), length);
498 else if (ISPORT(NTP_PORT))
499 ntp_print((const u_char *)(up + 1), length);
500 else if (ISPORT(KERBEROS_PORT) || ISPORT(KERBEROS_SEC_PORT))
501 krb_print((const void *)(up + 1), length);
502 else if (ISPORT(L2TP_PORT))
503 l2tp_print((const u_char *)(up + 1), length);
504 else if (dport == 3456)
505 vat_print((const void *)(up + 1), length, up);
506 #ifdef INET6
507 else if (ISPORT(RIPNG_PORT))
508 ripng_print((const u_char *)(up + 1), length);
509 else if (ISPORT(DHCP6_SERV_PORT) || ISPORT(DHCP6_CLI_PORT)) {
510 dhcp6_print((const u_char *)(up + 1), length,
511 sport, dport);
512 }
513 #endif /*INET6*/
514 /*
515 * Kludge in test for whiteboard packets.
516 */
517 else if (dport == 4567)
518 wb_print((const void *)(up + 1), length);
519 else
520 (void)printf(" udp %u",
521 (u_int32_t)(ulen - sizeof(*up)));
522 #undef ISPORT
523 } else
524 (void)printf(" udp %u", (u_int32_t)(ulen - sizeof(*up)));
525 }