2 * Copyright (C) 1999 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GNU Zebra; see the file COPYING. If not, write to the Free
18 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26 #include <net-snmp/net-snmp-config.h>
27 #include <net-snmp/net-snmp-includes.h>
31 #include <snmp_impl.h>
41 #include "zebra/rib.h"
42 #include "zebra/zserv.h"
44 #define IPFWMIB 1,3,6,1,2,1,4,24
47 #define IPFORWARDDEST 1
48 #define IPFORWARDMASK 2
49 #define IPFORWARDPOLICY 3
50 #define IPFORWARDNEXTHOP 4
51 #define IPFORWARDIFINDEX 5
52 #define IPFORWARDTYPE 6
53 #define IPFORWARDPROTO 7
54 #define IPFORWARDAGE 8
55 #define IPFORWARDINFO 9
56 #define IPFORWARDNEXTHOPAS 10
57 #define IPFORWARDMETRIC1 11
58 #define IPFORWARDMETRIC2 12
59 #define IPFORWARDMETRIC3 13
60 #define IPFORWARDMETRIC4 14
61 #define IPFORWARDMETRIC5 15
63 /* ipCidrRouteTable */
64 #define IPCIDRROUTEDEST 1
65 #define IPCIDRROUTEMASK 2
66 #define IPCIDRROUTETOS 3
67 #define IPCIDRROUTENEXTHOP 4
68 #define IPCIDRROUTEIFINDEX 5
69 #define IPCIDRROUTETYPE 6
70 #define IPCIDRROUTEPROTO 7
71 #define IPCIDRROUTEAGE 8
72 #define IPCIDRROUTEINFO 9
73 #define IPCIDRROUTENEXTHOPAS 10
74 #define IPCIDRROUTEMETRIC1 11
75 #define IPCIDRROUTEMETRIC2 12
76 #define IPCIDRROUTEMETRIC3 13
77 #define IPCIDRROUTEMETRIC4 14
78 #define IPCIDRROUTEMETRIC5 15
79 #define IPCIDRROUTESTATUS 16
81 #define INTEGER32 ASN_INTEGER
82 #define GAUGE32 ASN_GAUGE
83 #define ENUMERATION ASN_INTEGER
84 #define ROWSTATUS ASN_INTEGER
85 #define IPADDRESS ASN_IPADDRESS
86 #define OBJECTIDENTIFIER ASN_OBJECT_ID
88 extern struct zebra_t zebrad
;
90 oid ipfw_oid
[] = { IPFWMIB
};
93 static u_char
* ipFwNumber (struct variable
*, oid
[], size_t *,
94 int, size_t *, WriteMethod
**);
95 static u_char
* ipFwTable (struct variable
*, oid
[], size_t *,
96 int, size_t *, WriteMethod
**);
97 static u_char
* ipCidrNumber (struct variable
*, oid
[], size_t *,
98 int, size_t *, WriteMethod
**);
99 static u_char
* ipCidrTable (struct variable
*, oid
[], size_t *,
100 int, size_t *, WriteMethod
**);
102 struct variable zebra_variables
[] =
104 {0, GAUGE32
, RONLY
, ipFwNumber
, 1, {1}},
105 {IPFORWARDDEST
, IPADDRESS
, RONLY
, ipFwTable
, 3, {2, 1, 1}},
106 {IPFORWARDMASK
, IPADDRESS
, RONLY
, ipFwTable
, 3, {2, 1, 2}},
107 {IPFORWARDPOLICY
, INTEGER32
, RONLY
, ipFwTable
, 3, {2, 1, 3}},
108 {IPFORWARDNEXTHOP
, IPADDRESS
, RONLY
, ipFwTable
, 3, {2, 1, 4}},
109 {IPFORWARDIFINDEX
, INTEGER32
, RONLY
, ipFwTable
, 3, {2, 1, 5}},
110 {IPFORWARDTYPE
, ENUMERATION
, RONLY
, ipFwTable
, 3, {2, 1, 6}},
111 {IPFORWARDPROTO
, ENUMERATION
, RONLY
, ipFwTable
, 3, {2, 1, 7}},
112 {IPFORWARDAGE
, INTEGER32
, RONLY
, ipFwTable
, 3, {2, 1, 8}},
113 {IPFORWARDINFO
, OBJECTIDENTIFIER
, RONLY
, ipFwTable
, 3, {2, 1, 9}},
114 {IPFORWARDNEXTHOPAS
, INTEGER32
, RONLY
, ipFwTable
, 3, {2, 1, 10}},
115 {IPFORWARDMETRIC1
, INTEGER32
, RONLY
, ipFwTable
, 3, {2, 1, 11}},
116 {IPFORWARDMETRIC2
, INTEGER32
, RONLY
, ipFwTable
, 3, {2, 1, 12}},
117 {IPFORWARDMETRIC3
, INTEGER32
, RONLY
, ipFwTable
, 3, {2, 1, 13}},
118 {IPFORWARDMETRIC4
, INTEGER32
, RONLY
, ipFwTable
, 3, {2, 1, 14}},
119 {IPFORWARDMETRIC5
, INTEGER32
, RONLY
, ipFwTable
, 3, {2, 1, 15}},
120 {0, GAUGE32
, RONLY
, ipCidrNumber
, 1, {3}},
121 {IPCIDRROUTEDEST
, IPADDRESS
, RONLY
, ipCidrTable
, 3, {4, 1, 1}},
122 {IPCIDRROUTEMASK
, IPADDRESS
, RONLY
, ipCidrTable
, 3, {4, 1, 2}},
123 {IPCIDRROUTETOS
, INTEGER32
, RONLY
, ipCidrTable
, 3, {4, 1, 3}},
124 {IPCIDRROUTENEXTHOP
, IPADDRESS
, RONLY
, ipCidrTable
, 3, {4, 1, 4}},
125 {IPCIDRROUTEIFINDEX
, INTEGER32
, RONLY
, ipCidrTable
, 3, {4, 1, 5}},
126 {IPCIDRROUTETYPE
, ENUMERATION
, RONLY
, ipCidrTable
, 3, {4, 1, 6}},
127 {IPCIDRROUTEPROTO
, ENUMERATION
, RONLY
, ipCidrTable
, 3, {4, 1, 7}},
128 {IPCIDRROUTEAGE
, INTEGER32
, RONLY
, ipCidrTable
, 3, {4, 1, 8}},
129 {IPCIDRROUTEINFO
, OBJECTIDENTIFIER
, RONLY
, ipCidrTable
, 3, {4, 1, 9}},
130 {IPCIDRROUTENEXTHOPAS
, INTEGER32
, RONLY
, ipCidrTable
, 3, {4, 1, 10}},
131 {IPCIDRROUTEMETRIC1
, INTEGER32
, RONLY
, ipCidrTable
, 3, {4, 1, 11}},
132 {IPCIDRROUTEMETRIC2
, INTEGER32
, RONLY
, ipCidrTable
, 3, {4, 1, 12}},
133 {IPCIDRROUTEMETRIC3
, INTEGER32
, RONLY
, ipCidrTable
, 3, {4, 1, 13}},
134 {IPCIDRROUTEMETRIC4
, INTEGER32
, RONLY
, ipCidrTable
, 3, {4, 1, 14}},
135 {IPCIDRROUTEMETRIC5
, INTEGER32
, RONLY
, ipCidrTable
, 3, {4, 1, 15}},
136 {IPCIDRROUTESTATUS
, ROWSTATUS
, RONLY
, ipCidrTable
, 3, {4, 1, 16}}
141 ipFwNumber (struct variable
*v
, oid objid
[], size_t *objid_len
,
142 int exact
, size_t *val_len
, WriteMethod
**write_method
)
145 struct route_table
*table
;
146 struct route_node
*rn
;
149 if (smux_header_generic(v
, objid
, objid_len
, exact
, val_len
, write_method
) == MATCH_FAILED
)
152 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
156 /* Return number of routing entries. */
158 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
159 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
162 return (u_char
*)&result
;
166 ipCidrNumber (struct variable
*v
, oid objid
[], size_t *objid_len
,
167 int exact
, size_t *val_len
, WriteMethod
**write_method
)
170 struct route_table
*table
;
171 struct route_node
*rn
;
174 if (smux_header_generic(v
, objid
, objid_len
, exact
, val_len
, write_method
) == MATCH_FAILED
)
177 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
181 /* Return number of routing entries. */
183 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
184 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
187 return (u_char
*)&result
;
191 in_addr_cmp(u_char
*p1
, u_char
*p2
)
207 in_addr_add(u_char
*p
, int num
)
213 for (i
= 3; 0 <= i
; i
--) {
215 if (*p
+ num
> 255) {
224 /* ip + num > 0xffffffff */
232 proto_trans(int type
)
236 case ZEBRA_ROUTE_SYSTEM
:
237 return 1; /* other */
238 case ZEBRA_ROUTE_KERNEL
:
239 return 1; /* other */
240 case ZEBRA_ROUTE_CONNECT
:
241 return 2; /* local interface */
242 case ZEBRA_ROUTE_STATIC
:
243 return 3; /* static route */
244 case ZEBRA_ROUTE_RIP
:
246 case ZEBRA_ROUTE_RIPNG
:
247 return 1; /* shouldn't happen */
248 case ZEBRA_ROUTE_OSPF
:
249 return 13; /* ospf */
250 case ZEBRA_ROUTE_OSPF6
:
251 return 1; /* shouldn't happen */
252 case ZEBRA_ROUTE_BGP
:
255 return 1; /* other */
260 check_replace(struct route_node
*np2
, struct rib
*rib2
,
261 struct route_node
**np
, struct rib
**rib
)
272 if (in_addr_cmp(&(*np
)->p
.u
.prefix
, &np2
->p
.u
.prefix
) < 0)
274 if (in_addr_cmp(&(*np
)->p
.u
.prefix
, &np2
->p
.u
.prefix
) > 0)
281 proto
= proto_trans((*rib
)->type
);
282 proto2
= proto_trans(rib2
->type
);
293 if (in_addr_cmp((u_char
*)&(*rib
)->nexthop
->gate
.ipv4
,
294 (u_char
*)&rib2
->nexthop
->gate
.ipv4
) <= 0)
303 get_fwtable_route_node(struct variable
*v
, oid objid
[], size_t *objid_len
,
304 int exact
, struct route_node
**np
, struct rib
**rib
)
307 struct route_table
*table
;
308 struct route_node
*np2
;
312 struct in_addr nexthop
;
316 /* Init index variables */
318 pnt
= (u_char
*) &dest
;
319 for (i
= 0; i
< 4; i
++)
322 pnt
= (u_char
*) &nexthop
;
323 for (i
= 0; i
< 4; i
++)
329 /* Init return variables */
334 /* Short circuit exact matches of wrong length */
336 if (exact
&& (*objid_len
!= (unsigned) v
->namelen
+ 10))
339 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
343 /* Get INDEX information out of OID.
344 * ipForwardDest, ipForwardProto, ipForwardPolicy, ipForwardNextHop
347 if (*objid_len
> (unsigned) v
->namelen
)
348 oid2in_addr (objid
+ v
->namelen
, MIN(4, *objid_len
- v
->namelen
), &dest
);
350 if (*objid_len
> (unsigned) v
->namelen
+ 4)
351 proto
= objid
[v
->namelen
+ 4];
353 if (*objid_len
> (unsigned) v
->namelen
+ 5)
354 policy
= objid
[v
->namelen
+ 5];
356 if (*objid_len
> (unsigned) v
->namelen
+ 6)
357 oid2in_addr (objid
+ v
->namelen
+ 6, MIN(4, *objid_len
- v
->namelen
- 6),
360 /* Apply GETNEXT on not exact search */
362 if (!exact
&& (*objid_len
>= (unsigned) v
->namelen
+ 10))
364 if (! in_addr_add((u_char
*) &nexthop
, 1))
368 /* For exact: search matching entry in rib table. */
372 if (policy
) /* Not supported (yet?) */
374 for (*np
= route_top (table
); *np
; *np
= route_next (*np
))
376 if (!in_addr_cmp(&(*np
)->p
.u
.prefix
, (u_char
*)&dest
))
378 for (*rib
= (*np
)->info
; *rib
; *rib
= (*rib
)->next
)
380 if (!in_addr_cmp((u_char
*)&(*rib
)->nexthop
->gate
.ipv4
,
382 if (proto
== proto_trans((*rib
)->type
))
390 /* Search next best entry */
392 for (np2
= route_top (table
); np2
; np2
= route_next (np2
))
395 /* Check destination first */
396 if (in_addr_cmp(&np2
->p
.u
.prefix
, (u_char
*)&dest
) > 0)
397 for (rib2
= np2
->info
; rib2
; rib2
= rib2
->next
)
398 check_replace(np2
, rib2
, np
, rib
);
400 if (in_addr_cmp(&np2
->p
.u
.prefix
, (u_char
*)&dest
) == 0)
401 { /* have to look at each rib individually */
402 for (rib2
= np2
->info
; rib2
; rib2
= rib2
->next
)
406 proto2
= proto_trans(rib2
->type
);
409 if ((policy
< policy2
)
410 || ((policy
== policy2
) && (proto
< proto2
))
411 || ((policy
== policy2
) && (proto
== proto2
)
412 && (in_addr_cmp((u_char
*)&rib2
->nexthop
->gate
.ipv4
,
413 (u_char
*) &nexthop
) >= 0)
415 check_replace(np2
, rib2
, np
, rib
);
424 proto
= proto_trans((*rib
)->type
);
426 *objid_len
= v
->namelen
+ 10;
427 pnt
= (u_char
*) &(*np
)->p
.u
.prefix
;
428 for (i
= 0; i
< 4; i
++)
429 objid
[v
->namelen
+ i
] = *pnt
++;
431 objid
[v
->namelen
+ 4] = proto
;
432 objid
[v
->namelen
+ 5] = policy
;
435 struct nexthop
*nexthop
;
437 nexthop
= (*rib
)->nexthop
;
440 pnt
= (u_char
*) &nexthop
->gate
.ipv4
;
441 for (i
= 0; i
< 4; i
++)
442 objid
[i
+ v
->namelen
+ 6] = *pnt
++;
450 ipFwTable (struct variable
*v
, oid objid
[], size_t *objid_len
,
451 int exact
, size_t *val_len
, WriteMethod
**write_method
)
453 struct route_node
*np
;
456 static int resarr
[2];
457 static struct in_addr netmask
;
458 struct nexthop
*nexthop
;
460 get_fwtable_route_node(v
, objid
, objid_len
, exact
, &np
, &rib
);
464 nexthop
= rib
->nexthop
;
472 return &np
->p
.u
.prefix
;
475 masklen2ip(np
->p
.prefixlen
, &netmask
);
477 return (u_char
*)&netmask
;
479 case IPFORWARDPOLICY
:
481 *val_len
= sizeof(int);
482 return (u_char
*)&result
;
484 case IPFORWARDNEXTHOP
:
486 return (u_char
*)&nexthop
->gate
.ipv4
;
488 case IPFORWARDIFINDEX
:
489 *val_len
= sizeof(int);
490 return (u_char
*)&nexthop
->ifindex
;
493 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
494 || nexthop
->type
== NEXTHOP_TYPE_IFNAME
)
498 *val_len
= sizeof(int);
499 return (u_char
*)&result
;
502 result
= proto_trans(rib
->type
);
503 *val_len
= sizeof(int);
504 return (u_char
*)&result
;
508 *val_len
= sizeof(int);
509 return (u_char
*)&result
;
514 *val_len
= 2 * sizeof(int);
515 return (u_char
*)resarr
;
517 case IPFORWARDNEXTHOPAS
:
519 *val_len
= sizeof(int);
520 return (u_char
*)&result
;
522 case IPFORWARDMETRIC1
:
524 *val_len
= sizeof(int);
525 return (u_char
*)&result
;
527 case IPFORWARDMETRIC2
:
529 *val_len
= sizeof(int);
530 return (u_char
*)&result
;
532 case IPFORWARDMETRIC3
:
534 *val_len
= sizeof(int);
535 return (u_char
*)&result
;
537 case IPFORWARDMETRIC4
:
539 *val_len
= sizeof(int);
540 return (u_char
*)&result
;
542 case IPFORWARDMETRIC5
:
544 *val_len
= sizeof(int);
545 return (u_char
*)&result
;
555 ipCidrTable (struct variable
*v
, oid objid
[], size_t *objid_len
,
556 int exact
, size_t *val_len
, WriteMethod
**write_method
)
560 case IPCIDRROUTEDEST
:
572 smux_init (zebrad
.master
);
573 REGISTER_MIB("mibII/ipforward", zebra_variables
, variable
, ipfw_oid
);
575 #endif /* HAVE_SNMP */