]> The Tcpdump Group git mirrors - tcpdump/blob - print-udp.c
add code for printing MakeDir and StoreStatus. Also change date
[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.64 1999-11-17 05:45:58 assar 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 #define RX_PORT_LOW 7000 /*XXX*/
305 #define RX_PORT_HIGH 7009 /*XXX*/
306
307 #ifdef INET6
308 #define RIPNG_PORT 521 /*XXX*/
309 #define DHCP6_SERV_PORT 546 /*XXX*/
310 #define DHCP6_CLI_PORT 547 /*XXX*/
311 #endif
312
313 void
314 udp_print(register const u_char *bp, u_int length, register const u_char *bp2)
315 {
316 register const struct udphdr *up;
317 register const struct ip *ip;
318 register const u_char *cp;
319 register const u_char *ep = bp + length;
320 u_short sport, dport, ulen;
321 #ifdef INET6
322 register const struct ip6_hdr *ip6;
323 #endif
324
325 if (ep > snapend)
326 ep = snapend;
327 up = (struct udphdr *)bp;
328 ip = (struct ip *)bp2;
329 #ifdef INET6
330 if (ip->ip_v == 6)
331 ip6 = (struct ip6_hdr *)bp2;
332 else
333 ip6 = NULL;
334 #endif /*INET6*/
335 cp = (u_char *)(up + 1);
336 if (cp > snapend) {
337 printf("[|udp]");
338 return;
339 }
340 if (length < sizeof(struct udphdr)) {
341 (void)printf(" truncated-udp %d", length);
342 return;
343 }
344 length -= sizeof(struct udphdr);
345
346 sport = ntohs(up->uh_sport);
347 dport = ntohs(up->uh_dport);
348 ulen = ntohs(up->uh_ulen);
349 if (packettype) {
350 register struct rpc_msg *rp;
351 enum msg_type direction;
352
353 switch (packettype) {
354
355 case PT_VAT:
356 (void)printf("%s.%s > %s.%s:",
357 ipaddr_string(&ip->ip_src),
358 udpport_string(sport),
359 ipaddr_string(&ip->ip_dst),
360 udpport_string(dport));
361 vat_print((void *)(up + 1), length, up);
362 break;
363
364 case PT_WB:
365 (void)printf("%s.%s > %s.%s:",
366 ipaddr_string(&ip->ip_src),
367 udpport_string(sport),
368 ipaddr_string(&ip->ip_dst),
369 udpport_string(dport));
370 wb_print((void *)(up + 1), length);
371 break;
372
373 case PT_RPC:
374 rp = (struct rpc_msg *)(up + 1);
375 direction = (enum msg_type)ntohl(rp->rm_direction);
376 if (direction == CALL)
377 sunrpcrequest_print((u_char *)rp, length,
378 (u_char *)ip);
379 else
380 nfsreply_print((u_char *)rp, length,
381 (u_char *)ip); /*XXX*/
382 break;
383
384 case PT_RTP:
385 (void)printf("%s.%s > %s.%s:",
386 ipaddr_string(&ip->ip_src),
387 udpport_string(sport),
388 ipaddr_string(&ip->ip_dst),
389 udpport_string(dport));
390 rtp_print((void *)(up + 1), length, up);
391 break;
392
393 case PT_RTCP:
394 (void)printf("%s.%s > %s.%s:",
395 ipaddr_string(&ip->ip_src),
396 udpport_string(sport),
397 ipaddr_string(&ip->ip_dst),
398 udpport_string(dport));
399 while (cp < ep)
400 cp = rtcp_print(cp, ep);
401 break;
402
403 case PT_SNMP:
404 (void)printf("%s.%s > %s.%s:",
405 ipaddr_string(&ip->ip_src),
406 udpport_string(sport),
407 ipaddr_string(&ip->ip_dst),
408 udpport_string(dport));
409 snmp_print((const u_char *)(up + 1), length);
410 break;
411 }
412 return;
413 }
414
415 if (!qflag) {
416 register struct rpc_msg *rp;
417 enum msg_type direction;
418
419 rp = (struct rpc_msg *)(up + 1);
420 if (TTEST(rp->rm_direction)) {
421 direction = (enum msg_type)ntohl(rp->rm_direction);
422 if (dport == NFS_PORT && direction == CALL) {
423 nfsreq_print((u_char *)rp, length,
424 (u_char *)ip);
425 return;
426 }
427 if (sport == NFS_PORT && direction == REPLY) {
428 nfsreply_print((u_char *)rp, length,
429 (u_char *)ip);
430 return;
431 }
432 #ifdef notdef
433 if (dport == SUNRPC_PORT && direction == CALL) {
434 sunrpcrequest_print((u_char *)rp, length, (u_char *)ip);
435 return;
436 }
437 #endif
438 }
439 if (TTEST(((struct LAP *)cp)->type) &&
440 ((struct LAP *)cp)->type == lapDDP &&
441 (atalk_port(sport) || atalk_port(dport))) {
442 if (vflag)
443 fputs("kip ", stdout);
444 atalk_print(cp, length);
445 return;
446 }
447 }
448 #if 0
449 (void)printf("%s.%s > %s.%s:",
450 ipaddr_string(&ip->ip_src), udpport_string(sport),
451 ipaddr_string(&ip->ip_dst), udpport_string(dport));
452 #else
453 #ifdef INET6
454 if (ip6) {
455 if (ip6->ip6_nxt == IPPROTO_UDP) {
456 (void)printf("%s.%s > %s.%s: ",
457 ip6addr_string(&ip6->ip6_src),
458 udpport_string(sport),
459 ip6addr_string(&ip6->ip6_dst),
460 udpport_string(dport));
461 } else {
462 (void)printf("%s > %s: ",
463 udpport_string(sport), udpport_string(dport));
464 }
465 } else
466 #endif /*INET6*/
467 {
468 if (ip->ip_p == IPPROTO_UDP) {
469 (void)printf("%s.%s > %s.%s: ",
470 ipaddr_string(&ip->ip_src),
471 udpport_string(sport),
472 ipaddr_string(&ip->ip_dst),
473 udpport_string(dport));
474 } else {
475 (void)printf("%s > %s: ",
476 udpport_string(sport), udpport_string(dport));
477 }
478 }
479 #endif
480
481 if (!qflag) {
482 #define ISPORT(p) (dport == (p) || sport == (p))
483 if (ISPORT(NAMESERVER_PORT))
484 ns_print((const u_char *)(up + 1), length);
485 else if (ISPORT(TFTP_PORT))
486 tftp_print((const u_char *)(up + 1), length);
487 else if (ISPORT(IPPORT_BOOTPC) || ISPORT(IPPORT_BOOTPS))
488 bootp_print((const u_char *)(up + 1), length,
489 sport, dport);
490 else if (ISPORT(RIP_PORT))
491 rip_print((const u_char *)(up + 1), length);
492 else if (ISPORT(ISAKMP_PORT))
493 isakmp_print((const u_char *)(up + 1), length, bp2);
494 #if 1 /*???*/
495 else if (ISPORT(ISAKMP_PORT_USER1) || ISPORT(ISAKMP_PORT_USER2))
496 isakmp_print((const u_char *)(up + 1), length, bp2);
497 #endif
498 else if (ISPORT(SNMP_PORT) || ISPORT(SNMPTRAP_PORT))
499 snmp_print((const u_char *)(up + 1), length);
500 else if (ISPORT(NTP_PORT))
501 ntp_print((const u_char *)(up + 1), length);
502 else if (ISPORT(KERBEROS_PORT) || ISPORT(KERBEROS_SEC_PORT))
503 krb_print((const void *)(up + 1), length);
504 else if (ISPORT(L2TP_PORT))
505 l2tp_print((const u_char *)(up + 1), length);
506 else if (dport == 3456)
507 vat_print((const void *)(up + 1), length, up);
508 /*
509 * Since there are 10 possible ports to check, I think
510 * a <> test would be more efficient
511 */
512 else if ((sport >= RX_PORT_LOW && sport <= RX_PORT_HIGH) ||
513 (dport >= RX_PORT_LOW && dport <= RX_PORT_HIGH))
514 rx_print((const void *)(up + 1), length, sport, dport,
515 (u_char *) ip);
516 #ifdef INET6
517 else if (ISPORT(RIPNG_PORT))
518 ripng_print((const u_char *)(up + 1), length);
519 else if (ISPORT(DHCP6_SERV_PORT) || ISPORT(DHCP6_CLI_PORT)) {
520 dhcp6_print((const u_char *)(up + 1), length,
521 sport, dport);
522 }
523 #endif /*INET6*/
524 /*
525 * Kludge in test for whiteboard packets.
526 */
527 else if (dport == 4567)
528 wb_print((const void *)(up + 1), length);
529 else
530 (void)printf(" udp %u",
531 (u_int32_t)(ulen - sizeof(*up)));
532 #undef ISPORT
533 } else
534 (void)printf(" udp %u", (u_int32_t)(ulen - sizeof(*up)));
535 }