mcr | f555c16 | 1999-10-07 23:47:09 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 1989, 1990, 1991, 1993, 1994, 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 const char rcsid[] = |
itojun | a909932 | 2000-06-18 17:41:35 +0000 | [diff] [blame^] | 24 | "@(#) $Header: /tcpdump/master/tcpdump/print-rip.c,v 1.41 2000-06-18 17:41:35 itojun Exp $ (LBL)"; |
fenner | b9ac23c | 1999-11-21 09:36:43 +0000 | [diff] [blame] | 25 | #endif |
| 26 | |
| 27 | #ifdef HAVE_CONFIG_H |
| 28 | #include "config.h" |
mcr | f555c16 | 1999-10-07 23:47:09 +0000 | [diff] [blame] | 29 | #endif |
| 30 | |
| 31 | #include <sys/param.h> |
| 32 | #include <sys/time.h> |
| 33 | #include <sys/socket.h> |
| 34 | |
| 35 | #include <netinet/in.h> |
| 36 | #include <netinet/in_systm.h> |
| 37 | #include <netinet/ip.h> |
itojun | c9d84d1 | 1999-10-30 05:11:06 +0000 | [diff] [blame] | 38 | #include <netinet/ip_var.h> |
mcr | f555c16 | 1999-10-07 23:47:09 +0000 | [diff] [blame] | 39 | #include <netinet/udp.h> |
itojun | c9d84d1 | 1999-10-30 05:11:06 +0000 | [diff] [blame] | 40 | #include <netinet/udp_var.h> |
mcr | f555c16 | 1999-10-07 23:47:09 +0000 | [diff] [blame] | 41 | |
| 42 | #include <stdio.h> |
| 43 | |
| 44 | #include "interface.h" |
| 45 | #include "addrtoname.h" |
| 46 | #include "extract.h" /* must come after interface.h */ |
| 47 | |
| 48 | struct rip { |
| 49 | u_char rip_cmd; /* request/response */ |
| 50 | u_char rip_vers; /* protocol version # */ |
| 51 | u_short rip_zero2; /* unused */ |
| 52 | }; |
| 53 | #define RIPCMD_REQUEST 1 /* want info */ |
| 54 | #define RIPCMD_RESPONSE 2 /* responding to request */ |
| 55 | #define RIPCMD_TRACEON 3 /* turn tracing on */ |
| 56 | #define RIPCMD_TRACEOFF 4 /* turn it off */ |
| 57 | #define RIPCMD_POLL 5 /* want info from everybody */ |
| 58 | #define RIPCMD_POLLENTRY 6 /* poll for entry */ |
| 59 | |
itojun | a909932 | 2000-06-18 17:41:35 +0000 | [diff] [blame^] | 60 | #define RIP_AUTHLEN 16 |
| 61 | |
mcr | f555c16 | 1999-10-07 23:47:09 +0000 | [diff] [blame] | 62 | struct rip_netinfo { |
| 63 | u_short rip_family; |
| 64 | u_short rip_tag; |
| 65 | u_int32_t rip_dest; |
| 66 | u_int32_t rip_dest_mask; |
| 67 | u_int32_t rip_router; |
| 68 | u_int32_t rip_metric; /* cost of route */ |
| 69 | }; |
| 70 | |
| 71 | static void |
itojun | a909932 | 2000-06-18 17:41:35 +0000 | [diff] [blame^] | 72 | rip_printblk(const u_char *cp, const u_char *ep) |
mcr | f555c16 | 1999-10-07 23:47:09 +0000 | [diff] [blame] | 73 | { |
itojun | a909932 | 2000-06-18 17:41:35 +0000 | [diff] [blame^] | 74 | for (; cp < ep; cp += 2) |
| 75 | printf(" %04x", EXTRACT_16BITS(cp)); |
| 76 | return; |
| 77 | } |
mcr | f555c16 | 1999-10-07 23:47:09 +0000 | [diff] [blame] | 78 | |
itojun | a909932 | 2000-06-18 17:41:35 +0000 | [diff] [blame^] | 79 | static void |
| 80 | rip_entry_print_v1(register int vers, register const struct rip_netinfo *ni) |
| 81 | { |
| 82 | register u_short family; |
mcr | f555c16 | 1999-10-07 23:47:09 +0000 | [diff] [blame] | 83 | |
itojun | a909932 | 2000-06-18 17:41:35 +0000 | [diff] [blame^] | 84 | /* RFC 1058 */ |
| 85 | family = EXTRACT_16BITS(&ni->rip_family); |
| 86 | if (family != AF_INET) { |
| 87 | printf(" [family %d:", family); |
| 88 | rip_printblk((u_char *)&ni->rip_tag, |
| 89 | (u_char *)&ni->rip_metric + |
| 90 | sizeof(ni->rip_metric)); |
mcr | f555c16 | 1999-10-07 23:47:09 +0000 | [diff] [blame] | 91 | printf("]"); |
itojun | a909932 | 2000-06-18 17:41:35 +0000 | [diff] [blame^] | 92 | return; |
| 93 | } |
| 94 | if (ni->rip_tag || ni->rip_dest_mask || ni->rip_router) { |
| 95 | /* MBZ fields not zero */ |
| 96 | printf(" ["); |
| 97 | rip_printblk((u_char *)&ni->rip_family, |
| 98 | (u_char *)&ni->rip_metric + |
| 99 | sizeof(ni->rip_metric)); |
| 100 | printf("]"); |
| 101 | return; |
| 102 | } |
| 103 | printf(" {%s}(%d)", ipaddr_string(&ni->rip_dest), |
| 104 | EXTRACT_32BITS(&ni->rip_metric)); |
| 105 | } |
| 106 | |
| 107 | static void |
| 108 | rip_entry_print_v2(register int vers, register const struct rip_netinfo *ni) |
| 109 | { |
| 110 | register u_char *p; |
| 111 | register u_short family; |
| 112 | char buf[RIP_AUTHLEN]; |
| 113 | |
| 114 | /* RFC 1723 */ |
| 115 | family = EXTRACT_16BITS(&ni->rip_family); |
| 116 | if (family == 0xFFFF) { |
| 117 | if (EXTRACT_16BITS(&ni->rip_tag) == 2) { |
| 118 | memcpy(buf, &ni->rip_dest, sizeof(buf)); |
| 119 | buf[sizeof(buf)-1] = '\0'; |
| 120 | for (p = buf; *p; p++) { |
| 121 | if (!isprint(*p)) |
| 122 | break; |
| 123 | } |
| 124 | if (!*p) { |
| 125 | printf(" [password %s]", buf); |
| 126 | } else { |
| 127 | printf(" [password: "); |
| 128 | rip_printblk((u_char *)&ni->rip_dest, |
| 129 | (u_char *)&ni->rip_metric + |
| 130 | sizeof(ni->rip_metric)); |
| 131 | printf("]"); |
| 132 | } |
| 133 | } else { |
| 134 | printf(" [auth %d:", |
| 135 | EXTRACT_16BITS(&ni->rip_tag)); |
| 136 | rip_printblk((u_char *)&ni->rip_dest, |
| 137 | (u_char *)&ni->rip_metric + |
| 138 | sizeof(ni->rip_metric)); |
| 139 | printf("]"); |
| 140 | } |
| 141 | } else if (family != AF_INET) { |
| 142 | printf(" [family %d:", family); |
| 143 | rip_printblk((u_char *)&ni->rip_tag, |
| 144 | (u_char *)&ni->rip_metric + |
| 145 | sizeof(ni->rip_metric)); |
| 146 | printf("]"); |
| 147 | return; |
| 148 | } else { /* AF_INET */ |
mcr | f555c16 | 1999-10-07 23:47:09 +0000 | [diff] [blame] | 149 | printf(" {%s", ipaddr_string(&ni->rip_dest)); |
| 150 | if (ni->rip_dest_mask) |
| 151 | printf("/%s", ipaddr_string(&ni->rip_dest_mask)); |
| 152 | if (ni->rip_router) |
| 153 | printf("->%s", ipaddr_string(&ni->rip_router)); |
| 154 | if (ni->rip_tag) |
| 155 | printf(" tag %04x", EXTRACT_16BITS(&ni->rip_tag)); |
itojun | a909932 | 2000-06-18 17:41:35 +0000 | [diff] [blame^] | 156 | printf("}(%d)", EXTRACT_32BITS(&ni->rip_metric)); |
mcr | f555c16 | 1999-10-07 23:47:09 +0000 | [diff] [blame] | 157 | } |
mcr | f555c16 | 1999-10-07 23:47:09 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | void |
| 161 | rip_print(const u_char *dat, u_int length) |
| 162 | { |
| 163 | register const struct rip *rp; |
| 164 | register const struct rip_netinfo *ni; |
| 165 | register int i, j, trunc; |
| 166 | |
| 167 | i = min(length, snapend - dat) - sizeof(*rp); |
fenner | a2392b0 | 1999-11-22 04:24:28 +0000 | [diff] [blame] | 168 | if (i < 0) { |
| 169 | printf(" [|rip]"); |
mcr | f555c16 | 1999-10-07 23:47:09 +0000 | [diff] [blame] | 170 | return; |
fenner | a2392b0 | 1999-11-22 04:24:28 +0000 | [diff] [blame] | 171 | } |
mcr | f555c16 | 1999-10-07 23:47:09 +0000 | [diff] [blame] | 172 | |
| 173 | rp = (struct rip *)dat; |
mcr | f555c16 | 1999-10-07 23:47:09 +0000 | [diff] [blame] | 174 | switch (rp->rip_vers) { |
itojun | a909932 | 2000-06-18 17:41:35 +0000 | [diff] [blame^] | 175 | case 0: |
| 176 | /* RFC 1058 */ |
| 177 | printf(" RIPv0: "); |
| 178 | rip_printblk((u_char *)&ni->rip_family, |
| 179 | (u_char *)&ni->rip_metric + |
| 180 | sizeof(ni->rip_metric)); |
mcr | f555c16 | 1999-10-07 23:47:09 +0000 | [diff] [blame] | 181 | break; |
mcr | f555c16 | 1999-10-07 23:47:09 +0000 | [diff] [blame] | 182 | default: |
itojun | a909932 | 2000-06-18 17:41:35 +0000 | [diff] [blame^] | 183 | switch (rp->rip_cmd) { |
| 184 | case RIPCMD_REQUEST: |
| 185 | printf(" RIPv%d-req %d", rp->rip_vers, length); |
| 186 | break; |
| 187 | case RIPCMD_RESPONSE: |
| 188 | j = length / sizeof(*ni); |
| 189 | if (j * sizeof(*ni) != length - 4) |
| 190 | printf(" RIPv%d-resp [items %d] [%d]:", |
| 191 | rp->rip_vers, j, length); |
| 192 | else |
| 193 | printf(" RIPv%d-resp [items %d]:", |
| 194 | rp->rip_vers, j); |
| 195 | trunc = (i / sizeof(*ni)) != j; |
| 196 | ni = (struct rip_netinfo *)(rp + 1); |
| 197 | for (; (i -= sizeof(*ni)) >= 0; ++ni) { |
| 198 | if (rp->rip_vers == 1) |
| 199 | rip_entry_print_v1(rp->rip_vers, ni); |
| 200 | else |
| 201 | rip_entry_print_v2(rp->rip_vers, ni); |
| 202 | } |
| 203 | if (trunc) |
| 204 | printf("[|rip]"); |
| 205 | break; |
| 206 | case RIPCMD_TRACEON: |
| 207 | printf(" RIPv%d-traceon %d: \"", rp->rip_vers, length); |
| 208 | (void)fn_print((const u_char *)(rp + 1), snapend); |
| 209 | fputs("\"\n", stdout); |
| 210 | break; |
| 211 | case RIPCMD_TRACEOFF: |
| 212 | printf(" RIPv%d-traceoff %d", rp->rip_vers, length); |
| 213 | break; |
| 214 | case RIPCMD_POLL: |
| 215 | printf(" RIPv%d-poll %d", rp->rip_vers, length); |
| 216 | break; |
| 217 | case RIPCMD_POLLENTRY: |
| 218 | printf(" RIPv%d-pollentry %d", rp->rip_vers, length); |
| 219 | break; |
| 220 | default: |
| 221 | printf(" RIPv%d-#%d %d", rp->rip_vers, rp->rip_cmd, |
| 222 | length); |
| 223 | break; |
| 224 | } |
mcr | f555c16 | 1999-10-07 23:47:09 +0000 | [diff] [blame] | 225 | } |
| 226 | } |