xref: /openbsd-src/usr.sbin/tcpdump/print-ripng.c (revision e5157e49389faebcb42b7237d55fbf096d9c2523)
1 /*	$OpenBSD: print-ripng.c,v 1.3 2014/08/14 12:44:44 mpi Exp $	*/
2 
3 /*
4  * Copyright (c) 1989, 1990, 1991, 1993, 1994
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that: (1) source code distributions
9  * retain the above copyright notice and this paragraph in its entirety, (2)
10  * distributions including binary code include the above copyright notice and
11  * this paragraph in its entirety in the documentation or other materials
12  * provided with the distribution, and (3) all advertising materials mentioning
13  * features or use of this software display the following acknowledgement:
14  * ``This product includes software developed by the University of California,
15  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
16  * the University nor the names of its contributors may be used to endorse
17  * or promote products derived from this software without specific prior
18  * written permission.
19  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
20  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
21  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22  */
23 
24 #ifdef INET6
25 
26 #include <sys/param.h>
27 #include <sys/time.h>
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 
31 #include <netinet/in.h>
32 #include <netinet/ip.h>
33 #include <netinet/ip_var.h>
34 #include <netinet/udp.h>
35 #include <netinet/udp_var.h>
36 
37 #include <errno.h>
38 #include <stdio.h>
39 
40 #include <netinet/ip6.h>
41 
42 #include "route6d.h"
43 #include "interface.h"
44 #include "addrtoname.h"
45 
46 static int
47 rip6_entry_print(register const struct netinfo6 *ni, int metric)
48 {
49 	int l;
50 	l = printf("%s/%d", ip6addr_string(&ni->rip6_dest), ni->rip6_plen);
51 	if (ni->rip6_tag)
52 		l += printf(" [%d]", ntohs(ni->rip6_tag));
53 	if (metric)
54 		l += printf(" (%d)", ni->rip6_metric);
55 	return l;
56 }
57 
58 void
59 ripng_print(const u_char *dat, int length)
60 {
61 	register const struct rip6 *rp = (struct rip6 *)dat;
62 	register const struct netinfo6 *ni;
63 	register int amt = snapend - dat;
64 	register int i = min(length, amt) -
65 			 (sizeof(struct rip6) - sizeof(struct netinfo6));
66 	int j;
67 	int trunc;
68 
69 	if (i < 0)
70 		return;
71 
72 	switch (rp->rip6_cmd) {
73 
74 	case RIP6_REQUEST:
75 		j = length / sizeof(*ni);
76 		if (j == 1
77 		    &&  rp->rip6_nets->rip6_metric == HOPCNT_INFINITY6
78 		    &&  IN6_IS_ADDR_UNSPECIFIED(&rp->rip6_nets->rip6_dest)) {
79 			printf(" ripng-req dump");
80 			break;
81 		}
82 		if (j * sizeof(*ni) != length - 4)
83 			printf(" ripng-req %d[%d]:", j, length);
84 		else
85 			printf(" ripng-req %d:", j);
86 		trunc = ((i / sizeof(*ni)) * sizeof(*ni) != i);
87 		for (ni = rp->rip6_nets; (i -= sizeof(*ni)) >= 0; ++ni) {
88 			if (vflag)
89 				printf("\n\t");
90 			else
91 				printf(" ");
92 			rip6_entry_print(ni, 0);
93 		}
94 		break;
95 	case RIP6_RESPONSE:
96 		j = length / sizeof(*ni);
97 		if (j * sizeof(*ni) != length - 4)
98 			printf(" ripng-resp %d[%d]:", j, length);
99 		else
100 			printf(" ripng-resp %d:", j);
101 		trunc = ((i / sizeof(*ni)) * sizeof(*ni) != i);
102 		for (ni = rp->rip6_nets; (i -= sizeof(*ni)) >= 0; ++ni) {
103 			if (vflag)
104 				printf("\n\t");
105 			else
106 				printf(" ");
107 			rip6_entry_print(ni, ni->rip6_metric);
108 		}
109 		if (trunc)
110 			printf("[|rip]");
111 		break;
112 	default:
113 		printf(" ripng-%d ?? %d", rp->rip6_cmd, length);
114 		break;
115 	}
116 	if (rp->rip6_vers != RIP6_VERSION)
117 		printf(" [vers %d]", rp->rip6_vers);
118 }
119 #endif /* INET6 */
120