]>
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 along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32 #include "zebra/rib.h"
33 #include "zebra/zserv.h"
35 /* Thank you, Solaris, for polluting application symbol namespace. */
37 #undef hook_unregister
39 #include <sys/stream.h>
40 #include <sys/tihdr.h>
42 /* Solaris defines these in both <netinet/in.h> and <inet/in.h>, sigh */
44 #include <sys/tiuser.h>
46 #define T_CURRENT MI_T_CURRENT
47 #endif /* T_CURRENT */
49 #define IRE_CACHE 0x0020 /* Cached Route entry */
50 #endif /* IRE_CACHE */
51 #ifndef IRE_HOST_REDIRECT
52 #define IRE_HOST_REDIRECT 0x0200 /* Host route entry from redirects */
53 #endif /* IRE_HOST_REDIRECT */
54 #ifndef IRE_CACHETABLE
55 #define IRE_CACHETABLE (IRE_CACHE | IRE_BROADCAST | IRE_LOCAL | IRE_LOOPBACK)
56 #endif /* IRE_CACHETABLE */
64 #include <inet/common.h>
66 #include <inet/mib2.h>
68 /* device to read IP routing table from */
69 #ifndef _PATH_GETMSG_ROUTE
70 #define _PATH_GETMSG_ROUTE "/dev/ip"
71 #endif /* _PATH_GETMSG_ROUTE */
73 #define RT_BUFSIZ 8192
75 static void handle_route_entry(mib2_ipRouteEntry_t
*routeEntry
)
78 struct in_addr tmpaddr
;
80 u_char zebra_flags
= 0;
82 if (routeEntry
->ipRouteInfo
.re_ire_type
& IRE_CACHETABLE
)
85 if (routeEntry
->ipRouteInfo
.re_ire_type
& IRE_HOST_REDIRECT
)
86 zebra_flags
|= ZEBRA_FLAG_SELFROUTE
;
88 prefix
.family
= AF_INET
;
90 tmpaddr
.s_addr
= routeEntry
->ipRouteDest
;
91 prefix
.u
.prefix4
= tmpaddr
;
93 tmpaddr
.s_addr
= routeEntry
->ipRouteMask
;
94 prefix
.prefixlen
= ip_masklen(tmpaddr
);
96 memset(&nh
, 0, sizeof(nh
));
97 nh
.type
= NEXTHOP_TYPE_IPV4
;
98 nh
.gate
.ipv4
.s_addr
= routeEntry
->ipRouteNextHop
;
100 rib_add(AFI_IP
, SAFI_UNICAST
, VRF_DEFAULT
, ZEBRA_ROUTE_KERNEL
, 0,
101 zebra_flags
, &prefix
, NULL
, &nh
, 0, 0, 0, 0);
104 void route_read(struct zebra_ns
*zns
)
106 char storage
[RT_BUFSIZ
];
108 struct T_optmgmt_req
*TLIreq
= (struct T_optmgmt_req
*)storage
;
109 struct T_optmgmt_ack
*TLIack
= (struct T_optmgmt_ack
*)storage
;
110 struct T_error_ack
*TLIerr
= (struct T_error_ack
*)storage
;
112 struct opthdr
*MIB2hdr
;
114 mib2_ipRouteEntry_t
*routeEntry
, *lastRouteEntry
;
116 struct strbuf msgdata
;
117 int flags
, dev
, retval
, process
;
119 if ((dev
= open(_PATH_GETMSG_ROUTE
, O_RDWR
)) == -1) {
120 zlog_warn("can't open %s: %s", _PATH_GETMSG_ROUTE
,
121 safe_strerror(errno
));
125 TLIreq
->PRIM_type
= T_OPTMGMT_REQ
;
126 TLIreq
->OPT_offset
= sizeof(struct T_optmgmt_req
);
127 TLIreq
->OPT_length
= sizeof(struct opthdr
);
128 TLIreq
->MGMT_flags
= T_CURRENT
;
130 MIB2hdr
= (struct opthdr
*)&TLIreq
[1];
132 MIB2hdr
->level
= MIB2_IP
;
136 msgdata
.buf
= storage
;
137 msgdata
.len
= sizeof(struct T_optmgmt_req
) + sizeof(struct opthdr
);
141 if (putmsg(dev
, &msgdata
, NULL
, flags
) == -1) {
142 zlog_warn("putmsg failed: %s", safe_strerror(errno
));
146 MIB2hdr
= (struct opthdr
*)&TLIack
[1];
147 msgdata
.maxlen
= sizeof(storage
);
151 retval
= getmsg(dev
, &msgdata
, NULL
, &flags
);
154 zlog_warn("getmsg(ctl) failed: %s",
155 safe_strerror(errno
));
159 /* This is normal loop termination */
161 && (size_t)msgdata
.len
>= sizeof(struct T_optmgmt_ack
)
162 && TLIack
->PRIM_type
== T_OPTMGMT_ACK
163 && TLIack
->MGMT_flags
== T_SUCCESS
&& MIB2hdr
->len
== 0)
166 if ((size_t)msgdata
.len
>= sizeof(struct T_error_ack
)
167 && TLIerr
->PRIM_type
== T_ERROR_ACK
) {
168 zlog_warn("getmsg(ctl) returned T_ERROR_ACK: %s",
169 safe_strerror((TLIerr
->TLI_error
== TSYSERR
)
175 /* should dump more debugging info to the log statement,
176 like what GateD does in this instance, but not
178 if (retval
!= MOREDATA
179 || (size_t)msgdata
.len
< sizeof(struct T_optmgmt_ack
)
180 || TLIack
->PRIM_type
!= T_OPTMGMT_ACK
181 || TLIack
->MGMT_flags
!= T_SUCCESS
) {
183 zlog_warn("getmsg(ctl) returned bizarreness");
187 /* MIB2_IP_21 is the the pseudo-MIB2 ipRouteTable
188 entry, see <inet/mib2.h>. "This isn't the MIB data
189 you're looking for." */
190 process
= (MIB2hdr
->level
== MIB2_IP
191 && MIB2hdr
->name
== MIB2_IP_21
)
195 /* getmsg writes the data buffer out completely, not
196 to the closest smaller multiple. Unless reassembling
197 data structures across buffer boundaries is your idea
198 of a good time, set maxlen to the closest smaller
199 multiple of the size of the datastructure you're
203 - (sizeof(storage
) % sizeof(mib2_ipRouteEntry_t
));
209 retval
= getmsg(dev
, NULL
, &msgdata
, &flags
);
212 zlog_warn("getmsg(data) failed: %s",
213 safe_strerror(errno
));
217 if (!(retval
== 0 || retval
== MOREDATA
)) {
218 zlog_warn("getmsg(data) returned %d", retval
);
223 if (msgdata
.len
% sizeof(mib2_ipRouteEntry_t
)
226 "getmsg(data) returned "
227 "msgdata.len = %d (%% sizeof (mib2_ipRouteEntry_t) != 0)",
232 routeEntry
= (mib2_ipRouteEntry_t
*)msgdata
.buf
;
234 (mib2_ipRouteEntry_t
*)(msgdata
.buf
237 handle_route_entry(routeEntry
);
238 } while (++routeEntry
< lastRouteEntry
);
240 } while (retval
== MOREDATA
);
247 /* Only implemented for netlink method */
248 void macfdb_read(struct zebra_ns
*zns
)
252 void macfdb_read_for_bridge(struct zebra_ns
*zns
, struct interface
*ifp
,
253 struct interface
*br_if
)
257 void neigh_read(struct zebra_ns
*zns
)
261 void neigh_read_for_vlan(struct zebra_ns
*zns
, struct interface
*vlan_if
)