1 /* $OpenBSD: print-ripng.c,v 1.7 2018/10/22 16:12:45 kn 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 #include <sys/time.h>
25 #include <sys/types.h>
26 #include <sys/socket.h>
27
28 #include <netinet/in.h>
29 #include <netinet/ip.h>
30 #include <netinet/ip_var.h>
31 #include <netinet/udp.h>
32 #include <netinet/udp_var.h>
33
34 #include <errno.h>
35 #include <stdio.h>
36
37 #include <netinet/ip6.h>
38
39 #include "route6d.h"
40 #include "interface.h"
41 #include "addrtoname.h"
42
43 static int
rip6_entry_print(const struct netinfo6 * ni,int metric)44 rip6_entry_print(const struct netinfo6 *ni, int metric)
45 {
46 int l;
47 l = printf("%s/%d", ip6addr_string(&ni->rip6_dest), ni->rip6_plen);
48 if (ni->rip6_tag)
49 l += printf(" [%d]", ntohs(ni->rip6_tag));
50 if (metric)
51 l += printf(" (%d)", ni->rip6_metric);
52 return l;
53 }
54
55 void
ripng_print(const u_char * dat,int length)56 ripng_print(const u_char *dat, int length)
57 {
58 const struct rip6 *rp = (struct rip6 *)dat;
59 const struct netinfo6 *ni;
60 int amt = snapend - dat;
61 int i = min(length, amt) -
62 (sizeof(struct rip6) - sizeof(struct netinfo6));
63 int j;
64 int trunc;
65
66 if (i < 0)
67 return;
68
69 switch (rp->rip6_cmd) {
70
71 case RIP6_REQUEST:
72 j = length / sizeof(*ni);
73 if (j == 1
74 && rp->rip6_nets->rip6_metric == HOPCNT_INFINITY6
75 && IN6_IS_ADDR_UNSPECIFIED(&rp->rip6_nets->rip6_dest)) {
76 printf("ripng-req dump");
77 break;
78 }
79 if (j * sizeof(*ni) != length - 4)
80 printf("ripng-req %d[%d]:", j, length);
81 else
82 printf("ripng-req %d:", j);
83 trunc = ((i / sizeof(*ni)) * sizeof(*ni) != i);
84 for (ni = rp->rip6_nets; (i -= sizeof(*ni)) >= 0; ++ni) {
85 if (vflag)
86 printf("\n\t");
87 else
88 printf(" ");
89 rip6_entry_print(ni, 0);
90 }
91 break;
92 case RIP6_RESPONSE:
93 j = length / sizeof(*ni);
94 if (j * sizeof(*ni) != length - 4)
95 printf("ripng-resp %d[%d]:", j, length);
96 else
97 printf("ripng-resp %d:", j);
98 trunc = ((i / sizeof(*ni)) * sizeof(*ni) != i);
99 for (ni = rp->rip6_nets; (i -= sizeof(*ni)) >= 0; ++ni) {
100 if (vflag)
101 printf("\n\t");
102 else
103 printf(" ");
104 rip6_entry_print(ni, ni->rip6_metric);
105 }
106 if (trunc)
107 printf("[|rip]");
108 break;
109 default:
110 printf("ripng-%d ?? %d", rp->rip6_cmd, length);
111 break;
112 }
113 if (rp->rip6_vers != RIP6_VERSION)
114 printf(" [vers %d]", rp->rip6_vers);
115 }
116