]>
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
31 #include "lib_errors.h"
33 #include "zebra/rib.h"
35 #include "zebra/zebra_pbr.h"
36 #include "zebra/zebra_errors.h"
38 /* Thank you, Solaris, for polluting application symbol namespace. */
40 #undef hook_unregister
42 #include <sys/stream.h>
43 #include <sys/tihdr.h>
45 /* Solaris defines these in both <netinet/in.h> and <inet/in.h>, sigh */
47 #include <sys/tiuser.h>
49 #define T_CURRENT MI_T_CURRENT
50 #endif /* T_CURRENT */
52 #define IRE_CACHE 0x0020 /* Cached Route entry */
53 #endif /* IRE_CACHE */
54 #ifndef IRE_HOST_REDIRECT
55 #define IRE_HOST_REDIRECT 0x0200 /* Host route entry from redirects */
56 #endif /* IRE_HOST_REDIRECT */
57 #ifndef IRE_CACHETABLE
58 #define IRE_CACHETABLE (IRE_CACHE | IRE_BROADCAST | IRE_LOCAL | IRE_LOOPBACK)
59 #endif /* IRE_CACHETABLE */
67 #include <inet/common.h>
69 #include <inet/mib2.h>
71 /* device to read IP routing table from */
72 #ifndef _PATH_GETMSG_ROUTE
73 #define _PATH_GETMSG_ROUTE "/dev/ip"
74 #endif /* _PATH_GETMSG_ROUTE */
76 #define RT_BUFSIZ 8192
78 static void handle_route_entry(mib2_ipRouteEntry_t
*routeEntry
)
81 struct in_addr tmpaddr
;
83 uint8_t zebra_flags
= 0;
85 if (routeEntry
->ipRouteInfo
.re_ire_type
& IRE_CACHETABLE
)
88 if (routeEntry
->ipRouteInfo
.re_ire_type
& IRE_HOST_REDIRECT
)
89 zebra_flags
|= ZEBRA_FLAG_SELFROUTE
;
91 prefix
.family
= AF_INET
;
93 tmpaddr
.s_addr
= routeEntry
->ipRouteDest
;
94 prefix
.u
.prefix4
= tmpaddr
;
96 tmpaddr
.s_addr
= routeEntry
->ipRouteMask
;
97 prefix
.prefixlen
= ip_masklen(tmpaddr
);
99 memset(&nh
, 0, sizeof(nh
));
100 nh
.vrf_id
= VRF_DEFAULT
;
101 nh
.type
= NEXTHOP_TYPE_IPV4
;
102 nh
.gate
.ipv4
.s_addr
= routeEntry
->ipRouteNextHop
;
104 rib_add(AFI_IP
, SAFI_UNICAST
, VRF_DEFAULT
, ZEBRA_ROUTE_KERNEL
, 0,
105 zebra_flags
, &prefix
, NULL
, &nh
, 0, 0, 0, 0, 0, 0);
108 void route_read(struct zebra_ns
*zns
)
110 char storage
[RT_BUFSIZ
];
112 struct T_optmgmt_req
*TLIreq
= (struct T_optmgmt_req
*)storage
;
113 struct T_optmgmt_ack
*TLIack
= (struct T_optmgmt_ack
*)storage
;
114 struct T_error_ack
*TLIerr
= (struct T_error_ack
*)storage
;
116 struct opthdr
*MIB2hdr
;
118 mib2_ipRouteEntry_t
*routeEntry
, *lastRouteEntry
;
120 struct strbuf msgdata
;
121 int flags
, dev
, retval
, process
;
123 if ((dev
= open(_PATH_GETMSG_ROUTE
, O_RDWR
)) == -1) {
124 flog_err_sys(EC_LIB_SYSTEM_CALL
, "can't open %s: %s",
125 _PATH_GETMSG_ROUTE
, safe_strerror(errno
));
129 TLIreq
->PRIM_type
= T_OPTMGMT_REQ
;
130 TLIreq
->OPT_offset
= sizeof(struct T_optmgmt_req
);
131 TLIreq
->OPT_length
= sizeof(struct opthdr
);
132 TLIreq
->MGMT_flags
= T_CURRENT
;
134 MIB2hdr
= (struct opthdr
*)&TLIreq
[1];
136 MIB2hdr
->level
= MIB2_IP
;
140 msgdata
.buf
= storage
;
141 msgdata
.len
= sizeof(struct T_optmgmt_req
) + sizeof(struct opthdr
);
145 if (putmsg(dev
, &msgdata
, NULL
, flags
) == -1) {
146 flog_err_sys(EC_LIB_SOCKET
, "putmsg failed: %s",
147 safe_strerror(errno
));
151 MIB2hdr
= (struct opthdr
*)&TLIack
[1];
152 msgdata
.maxlen
= sizeof(storage
);
156 retval
= getmsg(dev
, &msgdata
, NULL
, &flags
);
159 flog_err_sys(EC_LIB_SYSTEM_CALL
,
160 "getmsg(ctl) failed: %s",
161 safe_strerror(errno
));
165 /* This is normal loop termination */
167 && (size_t)msgdata
.len
>= sizeof(struct T_optmgmt_ack
)
168 && TLIack
->PRIM_type
== T_OPTMGMT_ACK
169 && TLIack
->MGMT_flags
== T_SUCCESS
&& MIB2hdr
->len
== 0)
172 if ((size_t)msgdata
.len
>= sizeof(struct T_error_ack
)
173 && TLIerr
->PRIM_type
== T_ERROR_ACK
) {
174 zlog_debug("getmsg(ctl) returned T_ERROR_ACK: %s",
175 safe_strerror((TLIerr
->TLI_error
== TSYSERR
)
181 /* should dump more debugging info to the log statement,
182 like what GateD does in this instance, but not
184 if (retval
!= MOREDATA
185 || (size_t)msgdata
.len
< sizeof(struct T_optmgmt_ack
)
186 || TLIack
->PRIM_type
!= T_OPTMGMT_ACK
187 || TLIack
->MGMT_flags
!= T_SUCCESS
) {
189 zlog_debug("getmsg(ctl) returned bizarreness");
193 /* MIB2_IP_21 is the the pseudo-MIB2 ipRouteTable
194 entry, see <inet/mib2.h>. "This isn't the MIB data
195 you're looking for." */
196 process
= (MIB2hdr
->level
== MIB2_IP
197 && MIB2hdr
->name
== MIB2_IP_21
)
201 /* getmsg writes the data buffer out completely, not
202 to the closest smaller multiple. Unless reassembling
203 data structures across buffer boundaries is your idea
204 of a good time, set maxlen to the closest smaller
205 multiple of the size of the datastructure you're
209 - (sizeof(storage
) % sizeof(mib2_ipRouteEntry_t
));
215 retval
= getmsg(dev
, NULL
, &msgdata
, &flags
);
218 flog_err_sys(EC_LIB_SYSTEM_CALL
,
219 "getmsg(data) failed: %s",
220 safe_strerror(errno
));
224 if (!(retval
== 0 || retval
== MOREDATA
)) {
225 zlog_debug("getmsg(data) returned %d", retval
);
230 if (msgdata
.len
% sizeof(mib2_ipRouteEntry_t
)
233 "getmsg(data) returned msgdata.len = %d (%% sizeof(mib2_ipRouteEntry_t) != 0)",
238 routeEntry
= (mib2_ipRouteEntry_t
*)msgdata
.buf
;
240 (mib2_ipRouteEntry_t
*)(msgdata
.buf
243 handle_route_entry(routeEntry
);
244 } while (++routeEntry
< lastRouteEntry
);
246 } while (retval
== MOREDATA
);
253 /* Only implemented for netlink method */
254 void macfdb_read(struct zebra_ns
*zns
)
258 void macfdb_read_for_bridge(struct zebra_ns
*zns
, struct interface
*ifp
,
259 struct interface
*br_if
)
263 void macfdb_read_specific_mac(struct zebra_ns
*zns
, struct interface
*br_if
,
264 struct ethaddr
*mac
, vlanid_t vid
)
268 void neigh_read(struct zebra_ns
*zns
)
272 void neigh_read_for_vlan(struct zebra_ns
*zns
, struct interface
*vlan_if
)
276 void neigh_read_specific_ip(struct ipaddr
*ip
, struct interface
*vlan_if
)
280 void kernel_read_pbr_rules(struct zebra_ns
*zns
)