2 * Kernel routing table readup by getmsg(2)
3 * Copyright (C) 1999 Michael Handler
5 * This file is part of GNU Zebra.
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Zebra; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
29 #include "zebra/rib.h"
30 #include "zebra/zserv.h"
32 #include <sys/stream.h>
33 #include <sys/tihdr.h>
35 /* Solaris defines these in both <netinet/in.h> and <inet/in.h>, sigh */
37 #include <sys/tiuser.h>
39 #define T_CURRENT MI_T_CURRENT
40 #endif /* T_CURRENT */
42 #define IRE_CACHE 0x0020 /* Cached Route entry */
43 #endif /* IRE_CACHE */
44 #ifndef IRE_HOST_REDIRECT
45 #define IRE_HOST_REDIRECT 0x0200 /* Host route entry from redirects */
46 #endif /* IRE_HOST_REDIRECT */
47 #ifndef IRE_CACHETABLE
48 #define IRE_CACHETABLE (IRE_CACHE | IRE_BROADCAST | IRE_LOCAL | \
50 #endif /* IRE_CACHETABLE */
58 #include <inet/common.h>
60 #include <inet/mib2.h>
62 /* device to read IP routing table from */
63 #ifndef _PATH_GETMSG_ROUTE
64 #define _PATH_GETMSG_ROUTE "/dev/ip"
65 #endif /* _PATH_GETMSG_ROUTE */
67 #define RT_BUFSIZ 8192
70 handle_route_entry (mib2_ipRouteEntry_t
*routeEntry
)
72 struct prefix_ipv4 prefix
;
73 struct in_addr tmpaddr
, gateway
;
74 u_char zebra_flags
= 0;
76 if (routeEntry
->ipRouteInfo
.re_ire_type
& IRE_CACHETABLE
)
79 if (routeEntry
->ipRouteInfo
.re_ire_type
& IRE_HOST_REDIRECT
)
80 zebra_flags
|= ZEBRA_FLAG_SELFROUTE
;
82 prefix
.family
= AF_INET
;
84 tmpaddr
.s_addr
= routeEntry
->ipRouteDest
;
85 prefix
.prefix
= tmpaddr
;
87 tmpaddr
.s_addr
= routeEntry
->ipRouteMask
;
88 prefix
.prefixlen
= ip_masklen (tmpaddr
);
90 gateway
.s_addr
= routeEntry
->ipRouteNextHop
;
92 rib_add_ipv4 (ZEBRA_ROUTE_KERNEL
, zebra_flags
, &prefix
,
93 &gateway
, 0, 0, 0, 0);
99 char storage
[RT_BUFSIZ
];
101 struct T_optmgmt_req
*TLIreq
= (struct T_optmgmt_req
*) storage
;
102 struct T_optmgmt_ack
*TLIack
= (struct T_optmgmt_ack
*) storage
;
103 struct T_error_ack
*TLIerr
= (struct T_error_ack
*) storage
;
105 struct opthdr
*MIB2hdr
;
107 mib2_ipRouteEntry_t
*routeEntry
, *lastRouteEntry
;
109 struct strbuf msgdata
;
110 int flags
, dev
, retval
, process
;
112 if ((dev
= open (_PATH_GETMSG_ROUTE
, O_RDWR
)) == -1) {
113 zlog_warn ("can't open %s: %s", _PATH_GETMSG_ROUTE
,
114 safe_strerror (errno
));
118 TLIreq
->PRIM_type
= T_OPTMGMT_REQ
;
119 TLIreq
->OPT_offset
= sizeof (struct T_optmgmt_req
);
120 TLIreq
->OPT_length
= sizeof (struct opthdr
);
121 TLIreq
->MGMT_flags
= T_CURRENT
;
123 MIB2hdr
= (struct opthdr
*) &TLIreq
[1];
125 MIB2hdr
->level
= MIB2_IP
;
129 msgdata
.buf
= storage
;
130 msgdata
.len
= sizeof (struct T_optmgmt_req
) + sizeof (struct opthdr
);
134 if (putmsg (dev
, &msgdata
, NULL
, flags
) == -1) {
135 zlog_warn ("putmsg failed: %s", safe_strerror (errno
));
139 MIB2hdr
= (struct opthdr
*) &TLIack
[1];
140 msgdata
.maxlen
= sizeof (storage
);
144 retval
= getmsg (dev
, &msgdata
, NULL
, &flags
);
147 zlog_warn ("getmsg(ctl) failed: %s", safe_strerror (errno
));
151 /* This is normal loop termination */
153 msgdata
.len
>= sizeof (struct T_optmgmt_ack
) &&
154 TLIack
->PRIM_type
== T_OPTMGMT_ACK
&&
155 TLIack
->MGMT_flags
== T_SUCCESS
&&
159 if (msgdata
.len
>= sizeof (struct T_error_ack
) &&
160 TLIerr
->PRIM_type
== T_ERROR_ACK
) {
161 zlog_warn ("getmsg(ctl) returned T_ERROR_ACK: %s",
162 safe_strerror ((TLIerr
->TLI_error
== TSYSERR
)
163 ? TLIerr
->UNIX_error
: EPROTO
));
167 /* should dump more debugging info to the log statement,
168 like what GateD does in this instance, but not
170 if (retval
!= MOREDATA
||
171 msgdata
.len
< sizeof (struct T_optmgmt_ack
) ||
172 TLIack
->PRIM_type
!= T_OPTMGMT_ACK
||
173 TLIack
->MGMT_flags
!= T_SUCCESS
) {
175 zlog_warn ("getmsg(ctl) returned bizarreness");
179 /* MIB2_IP_21 is the the pseudo-MIB2 ipRouteTable
180 entry, see <inet/mib2.h>. "This isn't the MIB data
181 you're looking for." */
182 process
= (MIB2hdr
->level
== MIB2_IP
&&
183 MIB2hdr
->name
== MIB2_IP_21
) ? 1 : 0;
185 /* getmsg writes the data buffer out completely, not
186 to the closest smaller multiple. Unless reassembling
187 data structures across buffer boundaries is your idea
188 of a good time, set maxlen to the closest smaller
189 multiple of the size of the datastructure you're
191 msgdata
.maxlen
= sizeof (storage
) - (sizeof (storage
)
192 % sizeof (mib2_ipRouteEntry_t
));
198 retval
= getmsg (dev
, NULL
, &msgdata
, &flags
);
201 zlog_warn ("getmsg(data) failed: %s",
202 safe_strerror (errno
));
206 if (!(retval
== 0 || retval
== MOREDATA
)) {
207 zlog_warn ("getmsg(data) returned %d", retval
);
213 sizeof (mib2_ipRouteEntry_t
) != 0) {
214 zlog_warn ("getmsg(data) returned "
215 "msgdata.len = %d (%% sizeof (mib2_ipRouteEntry_t) != 0)", msgdata
.len
);
219 routeEntry
= (mib2_ipRouteEntry_t
*)
221 lastRouteEntry
= (mib2_ipRouteEntry_t
*)
222 (msgdata
.buf
+ msgdata
.len
);
224 handle_route_entry (routeEntry
);
225 } while (++routeEntry
< lastRouteEntry
);
227 } while (retval
== MOREDATA
);