]>
git.proxmox.com Git - mirror_frr.git/blob - zebra/rtread_getmsg.c
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
30 #include "zebra/rib.h"
31 #include "zebra/zserv.h"
33 #include <sys/stream.h>
34 #include <sys/tihdr.h>
36 /* Solaris defines these in both <netinet/in.h> and <inet/in.h>, sigh */
38 #include <sys/tiuser.h>
40 #define T_CURRENT MI_T_CURRENT
41 #endif /* T_CURRENT */
43 #define IRE_CACHE 0x0020 /* Cached Route entry */
44 #endif /* IRE_CACHE */
45 #ifndef IRE_HOST_REDIRECT
46 #define IRE_HOST_REDIRECT 0x0200 /* Host route entry from redirects */
47 #endif /* IRE_HOST_REDIRECT */
48 #ifndef IRE_CACHETABLE
49 #define IRE_CACHETABLE (IRE_CACHE | IRE_BROADCAST | IRE_LOCAL | \
51 #endif /* IRE_CACHETABLE */
59 #include <inet/common.h>
61 #include <inet/mib2.h>
63 /* device to read IP routing table from */
64 #ifndef _PATH_GETMSG_ROUTE
65 #define _PATH_GETMSG_ROUTE "/dev/ip"
66 #endif /* _PATH_GETMSG_ROUTE */
68 #define RT_BUFSIZ 8192
71 handle_route_entry (mib2_ipRouteEntry_t
*routeEntry
)
73 struct prefix_ipv4 prefix
;
74 struct in_addr tmpaddr
, gateway
;
75 u_char zebra_flags
= 0;
77 if (routeEntry
->ipRouteInfo
.re_ire_type
& IRE_CACHETABLE
)
80 if (routeEntry
->ipRouteInfo
.re_ire_type
& IRE_HOST_REDIRECT
)
81 zebra_flags
|= ZEBRA_FLAG_SELFROUTE
;
83 prefix
.family
= AF_INET
;
85 tmpaddr
.s_addr
= routeEntry
->ipRouteDest
;
86 prefix
.prefix
= tmpaddr
;
88 tmpaddr
.s_addr
= routeEntry
->ipRouteMask
;
89 prefix
.prefixlen
= ip_masklen (tmpaddr
);
91 gateway
.s_addr
= routeEntry
->ipRouteNextHop
;
93 rib_add_ipv4 (ZEBRA_ROUTE_KERNEL
, 0, zebra_flags
, &prefix
,
94 &gateway
, NULL
, 0, VRF_DEFAULT
, 0, 0, 0, SAFI_UNICAST
);
98 route_read (struct zebra_ns
*zns
)
100 char storage
[RT_BUFSIZ
];
102 struct T_optmgmt_req
*TLIreq
= (struct T_optmgmt_req
*) storage
;
103 struct T_optmgmt_ack
*TLIack
= (struct T_optmgmt_ack
*) storage
;
104 struct T_error_ack
*TLIerr
= (struct T_error_ack
*) storage
;
106 struct opthdr
*MIB2hdr
;
108 mib2_ipRouteEntry_t
*routeEntry
, *lastRouteEntry
;
110 struct strbuf msgdata
;
111 int flags
, dev
, retval
, process
;
113 if ((dev
= open (_PATH_GETMSG_ROUTE
, O_RDWR
)) == -1) {
114 zlog_warn ("can't open %s: %s", _PATH_GETMSG_ROUTE
,
115 safe_strerror (errno
));
119 TLIreq
->PRIM_type
= T_OPTMGMT_REQ
;
120 TLIreq
->OPT_offset
= sizeof (struct T_optmgmt_req
);
121 TLIreq
->OPT_length
= sizeof (struct opthdr
);
122 TLIreq
->MGMT_flags
= T_CURRENT
;
124 MIB2hdr
= (struct opthdr
*) &TLIreq
[1];
126 MIB2hdr
->level
= MIB2_IP
;
130 msgdata
.buf
= storage
;
131 msgdata
.len
= sizeof (struct T_optmgmt_req
) + sizeof (struct opthdr
);
135 if (putmsg (dev
, &msgdata
, NULL
, flags
) == -1) {
136 zlog_warn ("putmsg failed: %s", safe_strerror (errno
));
140 MIB2hdr
= (struct opthdr
*) &TLIack
[1];
141 msgdata
.maxlen
= sizeof (storage
);
145 retval
= getmsg (dev
, &msgdata
, NULL
, &flags
);
148 zlog_warn ("getmsg(ctl) failed: %s", safe_strerror (errno
));
152 /* This is normal loop termination */
154 msgdata
.len
>= sizeof (struct T_optmgmt_ack
) &&
155 TLIack
->PRIM_type
== T_OPTMGMT_ACK
&&
156 TLIack
->MGMT_flags
== T_SUCCESS
&&
160 if (msgdata
.len
>= sizeof (struct T_error_ack
) &&
161 TLIerr
->PRIM_type
== T_ERROR_ACK
) {
162 zlog_warn ("getmsg(ctl) returned T_ERROR_ACK: %s",
163 safe_strerror ((TLIerr
->TLI_error
== TSYSERR
)
164 ? TLIerr
->UNIX_error
: EPROTO
));
168 /* should dump more debugging info to the log statement,
169 like what GateD does in this instance, but not
171 if (retval
!= MOREDATA
||
172 msgdata
.len
< sizeof (struct T_optmgmt_ack
) ||
173 TLIack
->PRIM_type
!= T_OPTMGMT_ACK
||
174 TLIack
->MGMT_flags
!= T_SUCCESS
) {
176 zlog_warn ("getmsg(ctl) returned bizarreness");
180 /* MIB2_IP_21 is the the pseudo-MIB2 ipRouteTable
181 entry, see <inet/mib2.h>. "This isn't the MIB data
182 you're looking for." */
183 process
= (MIB2hdr
->level
== MIB2_IP
&&
184 MIB2hdr
->name
== MIB2_IP_21
) ? 1 : 0;
186 /* getmsg writes the data buffer out completely, not
187 to the closest smaller multiple. Unless reassembling
188 data structures across buffer boundaries is your idea
189 of a good time, set maxlen to the closest smaller
190 multiple of the size of the datastructure you're
192 msgdata
.maxlen
= sizeof (storage
) - (sizeof (storage
)
193 % sizeof (mib2_ipRouteEntry_t
));
199 retval
= getmsg (dev
, NULL
, &msgdata
, &flags
);
202 zlog_warn ("getmsg(data) failed: %s",
203 safe_strerror (errno
));
207 if (!(retval
== 0 || retval
== MOREDATA
)) {
208 zlog_warn ("getmsg(data) returned %d", retval
);
214 sizeof (mib2_ipRouteEntry_t
) != 0) {
215 zlog_warn ("getmsg(data) returned "
216 "msgdata.len = %d (%% sizeof (mib2_ipRouteEntry_t) != 0)", msgdata
.len
);
220 routeEntry
= (mib2_ipRouteEntry_t
*)
222 lastRouteEntry
= (mib2_ipRouteEntry_t
*)
223 (msgdata
.buf
+ msgdata
.len
);
225 handle_route_entry (routeEntry
);
226 } while (++routeEntry
< lastRouteEntry
);
228 } while (retval
== MOREDATA
);