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[] = |
hannes | c603bd1 | 2002-12-22 02:01:49 +0000 | [diff] [blame] | 24 | "@(#) $Header: /tcpdump/master/tcpdump/print-rip.c,v 1.55 2002-12-22 02:01:49 hannes 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 | |
risso | 3d93249 | 2002-08-01 08:52:55 +0000 | [diff] [blame] | 31 | #include <tcpdump-stdinc.h> |
mcr | f555c16 | 1999-10-07 23:47:09 +0000 | [diff] [blame] | 32 | |
| 33 | #include <stdio.h> |
itojun | febfc92 | 2000-10-03 04:19:07 +0000 | [diff] [blame] | 34 | #include <string.h> |
mcr | f555c16 | 1999-10-07 23:47:09 +0000 | [diff] [blame] | 35 | |
| 36 | #include "interface.h" |
| 37 | #include "addrtoname.h" |
| 38 | #include "extract.h" /* must come after interface.h */ |
| 39 | |
| 40 | struct rip { |
itojun | 10afb02 | 2002-11-09 17:19:16 +0000 | [diff] [blame] | 41 | u_int8_t rip_cmd; /* request/response */ |
| 42 | u_int8_t rip_vers; /* protocol version # */ |
hannes | c603bd1 | 2002-12-22 02:01:49 +0000 | [diff] [blame] | 43 | u_int8_t unused[2]; /* unused */ |
guy | fcc82f4 | 2002-12-11 07:13:49 +0000 | [diff] [blame] | 44 | }; |
hannes | c603bd1 | 2002-12-22 02:01:49 +0000 | [diff] [blame] | 45 | |
mcr | f555c16 | 1999-10-07 23:47:09 +0000 | [diff] [blame] | 46 | #define RIPCMD_REQUEST 1 /* want info */ |
| 47 | #define RIPCMD_RESPONSE 2 /* responding to request */ |
| 48 | #define RIPCMD_TRACEON 3 /* turn tracing on */ |
| 49 | #define RIPCMD_TRACEOFF 4 /* turn it off */ |
| 50 | #define RIPCMD_POLL 5 /* want info from everybody */ |
| 51 | #define RIPCMD_POLLENTRY 6 /* poll for entry */ |
| 52 | |
hannes | c603bd1 | 2002-12-22 02:01:49 +0000 | [diff] [blame] | 53 | static const struct tok rip_cmd_values[] = { |
| 54 | { RIPCMD_REQUEST, "Request" }, |
| 55 | { RIPCMD_RESPONSE, "Response" }, |
| 56 | { RIPCMD_TRACEON, "Trace on" }, |
| 57 | { RIPCMD_TRACEOFF, "Trace off" }, |
| 58 | { RIPCMD_POLL, "Poll" }, |
| 59 | { RIPCMD_POLLENTRY, "Poll Entry" }, |
| 60 | { 0, NULL} |
| 61 | }; |
| 62 | |
| 63 | #define RIP_AUTHLEN 16 |
| 64 | #define RIP_ROUTELEN 20 |
| 65 | |
| 66 | /* |
| 67 | * rfc 1723 |
| 68 | * |
| 69 | * 0 1 2 3 3 |
| 70 | * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
| 71 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 72 | * | Command (1) | Version (1) | unused | |
| 73 | * +---------------+---------------+-------------------------------+ |
| 74 | * | Address Family Identifier (2) | Route Tag (2) | |
| 75 | * +-------------------------------+-------------------------------+ |
| 76 | * | IP Address (4) | |
| 77 | * +---------------------------------------------------------------+ |
| 78 | * | Subnet Mask (4) | |
| 79 | * +---------------------------------------------------------------+ |
| 80 | * | Next Hop (4) | |
| 81 | * +---------------------------------------------------------------+ |
| 82 | * | Metric (4) | |
| 83 | * +---------------------------------------------------------------+ |
| 84 | * |
| 85 | */ |
itojun | a909932 | 2000-06-18 17:41:35 +0000 | [diff] [blame] | 86 | |
mcr | f555c16 | 1999-10-07 23:47:09 +0000 | [diff] [blame] | 87 | struct rip_netinfo { |
itojun | 10afb02 | 2002-11-09 17:19:16 +0000 | [diff] [blame] | 88 | u_int16_t rip_family; |
| 89 | u_int16_t rip_tag; |
mcr | f555c16 | 1999-10-07 23:47:09 +0000 | [diff] [blame] | 90 | u_int32_t rip_dest; |
| 91 | u_int32_t rip_dest_mask; |
| 92 | u_int32_t rip_router; |
| 93 | u_int32_t rip_metric; /* cost of route */ |
guy | fcc82f4 | 2002-12-11 07:13:49 +0000 | [diff] [blame] | 94 | }; |
mcr | f555c16 | 1999-10-07 23:47:09 +0000 | [diff] [blame] | 95 | |
| 96 | static void |
guy | c422d3a | 2002-09-05 21:25:34 +0000 | [diff] [blame] | 97 | rip_entry_print_v1(register const struct rip_netinfo *ni) |
itojun | a909932 | 2000-06-18 17:41:35 +0000 | [diff] [blame] | 98 | { |
| 99 | register u_short family; |
mcr | f555c16 | 1999-10-07 23:47:09 +0000 | [diff] [blame] | 100 | |
itojun | a909932 | 2000-06-18 17:41:35 +0000 | [diff] [blame] | 101 | /* RFC 1058 */ |
| 102 | family = EXTRACT_16BITS(&ni->rip_family); |
| 103 | if (family != AF_INET) { |
hannes | c603bd1 | 2002-12-22 02:01:49 +0000 | [diff] [blame] | 104 | printf("\n\t AFI: %u:", family); |
| 105 | print_unknown_data((u_int8_t *)&ni->rip_family,"\n\t ",RIP_ROUTELEN); |
itojun | a909932 | 2000-06-18 17:41:35 +0000 | [diff] [blame] | 106 | return; |
| 107 | } |
guy | fcc82f4 | 2002-12-11 07:13:49 +0000 | [diff] [blame] | 108 | if (EXTRACT_16BITS(&ni->rip_tag) || |
| 109 | EXTRACT_32BITS(&ni->rip_dest_mask) || |
| 110 | EXTRACT_32BITS(&ni->rip_router)) { |
itojun | a909932 | 2000-06-18 17:41:35 +0000 | [diff] [blame] | 111 | /* MBZ fields not zero */ |
hannes | c603bd1 | 2002-12-22 02:01:49 +0000 | [diff] [blame] | 112 | print_unknown_data((u_int8_t *)&ni->rip_family,"\n\t ",RIP_ROUTELEN); |
itojun | a909932 | 2000-06-18 17:41:35 +0000 | [diff] [blame] | 113 | return; |
hannes | c603bd1 | 2002-12-22 02:01:49 +0000 | [diff] [blame] | 114 | } /* AF_INET */ |
| 115 | printf("\n\t %s, metric: %u", |
| 116 | ipaddr_string(&ni->rip_dest), |
itojun | a909932 | 2000-06-18 17:41:35 +0000 | [diff] [blame] | 117 | EXTRACT_32BITS(&ni->rip_metric)); |
| 118 | } |
| 119 | |
| 120 | static void |
guy | c422d3a | 2002-09-05 21:25:34 +0000 | [diff] [blame] | 121 | rip_entry_print_v2(register const struct rip_netinfo *ni) |
itojun | a909932 | 2000-06-18 17:41:35 +0000 | [diff] [blame] | 122 | { |
| 123 | register u_char *p; |
| 124 | register u_short family; |
guy | 09a08a4 | 2000-09-24 07:59:35 +0000 | [diff] [blame] | 125 | u_char buf[RIP_AUTHLEN]; |
itojun | a909932 | 2000-06-18 17:41:35 +0000 | [diff] [blame] | 126 | |
itojun | a909932 | 2000-06-18 17:41:35 +0000 | [diff] [blame] | 127 | family = EXTRACT_16BITS(&ni->rip_family); |
hannes | c603bd1 | 2002-12-22 02:01:49 +0000 | [diff] [blame] | 128 | if (family == 0xFFFF) { /* 16 bytes authentication ? */ |
| 129 | if (EXTRACT_16BITS(&ni->rip_tag) == 2) { /* simple text authentication ? */ |
itojun | a909932 | 2000-06-18 17:41:35 +0000 | [diff] [blame] | 130 | memcpy(buf, &ni->rip_dest, sizeof(buf)); |
| 131 | buf[sizeof(buf)-1] = '\0'; |
| 132 | for (p = buf; *p; p++) { |
| 133 | if (!isprint(*p)) |
| 134 | break; |
| 135 | } |
hannes | c603bd1 | 2002-12-22 02:01:49 +0000 | [diff] [blame] | 136 | printf("\n\t Simple Text Authentication data: %s", buf); |
| 137 | } else { |
| 138 | printf("\n\t Unknown (%u) Authentication data:", |
itojun | a909932 | 2000-06-18 17:41:35 +0000 | [diff] [blame] | 139 | EXTRACT_16BITS(&ni->rip_tag)); |
hannes | c603bd1 | 2002-12-22 02:01:49 +0000 | [diff] [blame] | 140 | print_unknown_data((u_int8_t *)&ni->rip_dest,"\n\t ",RIP_AUTHLEN); |
itojun | a909932 | 2000-06-18 17:41:35 +0000 | [diff] [blame] | 141 | } |
| 142 | } else if (family != AF_INET) { |
hannes | c603bd1 | 2002-12-22 02:01:49 +0000 | [diff] [blame] | 143 | printf("\n\t AFI: %u", family); |
| 144 | print_unknown_data((u_int8_t *)&ni->rip_tag,"\n\t ",RIP_ROUTELEN-2); |
itojun | a909932 | 2000-06-18 17:41:35 +0000 | [diff] [blame] | 145 | return; |
| 146 | } else { /* AF_INET */ |
hannes | c603bd1 | 2002-12-22 02:01:49 +0000 | [diff] [blame] | 147 | printf("\n\t AFI: IPv4: %15s/%-2d, tag 0x%04x, metric: %u, next-hop: ", |
| 148 | ipaddr_string(&ni->rip_dest), |
| 149 | mask2plen(EXTRACT_32BITS(&ni->rip_dest_mask)), |
| 150 | EXTRACT_16BITS(&ni->rip_tag), |
| 151 | EXTRACT_32BITS(&ni->rip_metric)); |
guy | fcc82f4 | 2002-12-11 07:13:49 +0000 | [diff] [blame] | 152 | if (EXTRACT_32BITS(&ni->rip_router)) |
hannes | c603bd1 | 2002-12-22 02:01:49 +0000 | [diff] [blame] | 153 | printf("%s", ipaddr_string(&ni->rip_router)); |
| 154 | else |
| 155 | printf("self"); |
mcr | f555c16 | 1999-10-07 23:47:09 +0000 | [diff] [blame] | 156 | } |
mcr | f555c16 | 1999-10-07 23:47:09 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | void |
| 160 | rip_print(const u_char *dat, u_int length) |
| 161 | { |
| 162 | register const struct rip *rp; |
| 163 | register const struct rip_netinfo *ni; |
guy | edb0e92 | 2002-09-05 00:00:07 +0000 | [diff] [blame] | 164 | register u_int i, j; |
| 165 | register int trunc; |
mcr | f555c16 | 1999-10-07 23:47:09 +0000 | [diff] [blame] | 166 | |
guy | edb0e92 | 2002-09-05 00:00:07 +0000 | [diff] [blame] | 167 | if (snapend < dat) { |
fenner | a2392b0 | 1999-11-22 04:24:28 +0000 | [diff] [blame] | 168 | printf(" [|rip]"); |
mcr | f555c16 | 1999-10-07 23:47:09 +0000 | [diff] [blame] | 169 | return; |
fenner | a2392b0 | 1999-11-22 04:24:28 +0000 | [diff] [blame] | 170 | } |
guy | edb0e92 | 2002-09-05 00:00:07 +0000 | [diff] [blame] | 171 | i = snapend - dat; |
| 172 | if (i > length) |
| 173 | i = length; |
| 174 | if (i < sizeof(*rp)) { |
| 175 | printf(" [|rip]"); |
| 176 | return; |
| 177 | } |
| 178 | i -= sizeof(*rp); |
mcr | f555c16 | 1999-10-07 23:47:09 +0000 | [diff] [blame] | 179 | |
| 180 | rp = (struct rip *)dat; |
hannes | c603bd1 | 2002-12-22 02:01:49 +0000 | [diff] [blame] | 181 | |
| 182 | printf("%sRIPv%u", |
| 183 | (vflag >= 1) ? "\n\t" : "", |
| 184 | rp->rip_vers); |
| 185 | |
mcr | f555c16 | 1999-10-07 23:47:09 +0000 | [diff] [blame] | 186 | switch (rp->rip_vers) { |
itojun | a909932 | 2000-06-18 17:41:35 +0000 | [diff] [blame] | 187 | case 0: |
guy | 09a08a4 | 2000-09-24 07:59:35 +0000 | [diff] [blame] | 188 | /* |
| 189 | * RFC 1058. |
| 190 | * |
| 191 | * XXX - RFC 1058 says |
| 192 | * |
| 193 | * 0 Datagrams whose version number is zero are to be ignored. |
| 194 | * These are from a previous version of the protocol, whose |
| 195 | * packet format was machine-specific. |
| 196 | * |
hannes | c603bd1 | 2002-12-22 02:01:49 +0000 | [diff] [blame] | 197 | * so perhaps we should just dump the packet, in hex. |
itojun | 3733a8b | 2001-01-28 08:14:55 +0000 | [diff] [blame] | 198 | */ |
hannes | c603bd1 | 2002-12-22 02:01:49 +0000 | [diff] [blame] | 199 | print_unknown_data((u_int8_t *)&rp->rip_cmd,"\n\t",length); |
mcr | f555c16 | 1999-10-07 23:47:09 +0000 | [diff] [blame] | 200 | break; |
mcr | f555c16 | 1999-10-07 23:47:09 +0000 | [diff] [blame] | 201 | default: |
hannes | c603bd1 | 2002-12-22 02:01:49 +0000 | [diff] [blame] | 202 | /* dump version and lets see if we know the commands name*/ |
| 203 | printf(", %s, length: %u", |
| 204 | tok2str(rip_cmd_values, |
| 205 | "unknown command (%u)", |
| 206 | rp->rip_cmd), |
| 207 | length); |
| 208 | |
| 209 | if (vflag < 1) |
| 210 | return; |
| 211 | |
itojun | a909932 | 2000-06-18 17:41:35 +0000 | [diff] [blame] | 212 | switch (rp->rip_cmd) { |
itojun | a909932 | 2000-06-18 17:41:35 +0000 | [diff] [blame] | 213 | case RIPCMD_RESPONSE: |
| 214 | j = length / sizeof(*ni); |
hannes | c603bd1 | 2002-12-22 02:01:49 +0000 | [diff] [blame] | 215 | printf(", routes: %u",j); |
itojun | a909932 | 2000-06-18 17:41:35 +0000 | [diff] [blame] | 216 | trunc = (i / sizeof(*ni)) != j; |
| 217 | ni = (struct rip_netinfo *)(rp + 1); |
guy | edb0e92 | 2002-09-05 00:00:07 +0000 | [diff] [blame] | 218 | for (; i >= sizeof(*ni); ++ni) { |
itojun | a909932 | 2000-06-18 17:41:35 +0000 | [diff] [blame] | 219 | if (rp->rip_vers == 1) |
guy | c422d3a | 2002-09-05 21:25:34 +0000 | [diff] [blame] | 220 | rip_entry_print_v1(ni); |
hannes | c603bd1 | 2002-12-22 02:01:49 +0000 | [diff] [blame] | 221 | else if (rp->rip_vers == 2) |
guy | c422d3a | 2002-09-05 21:25:34 +0000 | [diff] [blame] | 222 | rip_entry_print_v2(ni); |
hannes | c603bd1 | 2002-12-22 02:01:49 +0000 | [diff] [blame] | 223 | else |
| 224 | break; |
guy | edb0e92 | 2002-09-05 00:00:07 +0000 | [diff] [blame] | 225 | i -= sizeof(*ni); |
itojun | a909932 | 2000-06-18 17:41:35 +0000 | [diff] [blame] | 226 | } |
| 227 | if (trunc) |
| 228 | printf("[|rip]"); |
| 229 | break; |
hannes | c603bd1 | 2002-12-22 02:01:49 +0000 | [diff] [blame] | 230 | |
| 231 | case RIPCMD_REQUEST: |
itojun | a909932 | 2000-06-18 17:41:35 +0000 | [diff] [blame] | 232 | case RIPCMD_TRACEOFF: |
itojun | a909932 | 2000-06-18 17:41:35 +0000 | [diff] [blame] | 233 | case RIPCMD_POLL: |
itojun | a909932 | 2000-06-18 17:41:35 +0000 | [diff] [blame] | 234 | case RIPCMD_POLLENTRY: |
itojun | a909932 | 2000-06-18 17:41:35 +0000 | [diff] [blame] | 235 | break; |
hannes | c603bd1 | 2002-12-22 02:01:49 +0000 | [diff] [blame] | 236 | |
| 237 | case RIPCMD_TRACEON: |
| 238 | /* fall through */ |
| 239 | default: |
| 240 | if (vflag <= 1) { |
| 241 | if(!print_unknown_data((u_int8_t *)rp,"\n\t",length)) |
| 242 | return; |
| 243 | } |
| 244 | break; |
| 245 | } |
| 246 | /* do we want to see an additionally hexdump ? */ |
| 247 | if (vflag> 1) { |
| 248 | if(!print_unknown_data((u_int8_t *)rp,"\n\t",length)) |
| 249 | return; |
| 250 | } |
| 251 | } |
mcr | f555c16 | 1999-10-07 23:47:09 +0000 | [diff] [blame] | 252 | } |
hannes | c603bd1 | 2002-12-22 02:01:49 +0000 | [diff] [blame] | 253 | |
| 254 | |