]>
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"
34 #include "zebra/zebra_pbr.h"
36 /* Thank you, Solaris, for polluting application symbol namespace. */
38 #undef hook_unregister
40 #include <sys/stream.h>
41 #include <sys/tihdr.h>
43 /* Solaris defines these in both <netinet/in.h> and <inet/in.h>, sigh */
45 #include <sys/tiuser.h>
47 #define T_CURRENT MI_T_CURRENT
48 #endif /* T_CURRENT */
50 #define IRE_CACHE 0x0020 /* Cached Route entry */
51 #endif /* IRE_CACHE */
52 #ifndef IRE_HOST_REDIRECT
53 #define IRE_HOST_REDIRECT 0x0200 /* Host route entry from redirects */
54 #endif /* IRE_HOST_REDIRECT */
55 #ifndef IRE_CACHETABLE
56 #define IRE_CACHETABLE (IRE_CACHE | IRE_BROADCAST | IRE_LOCAL | IRE_LOOPBACK)
57 #endif /* IRE_CACHETABLE */
65 #include <inet/common.h>
67 #include <inet/mib2.h>
69 /* device to read IP routing table from */
70 #ifndef _PATH_GETMSG_ROUTE
71 #define _PATH_GETMSG_ROUTE "/dev/ip"
72 #endif /* _PATH_GETMSG_ROUTE */
74 #define RT_BUFSIZ 8192
76 static void handle_route_entry(mib2_ipRouteEntry_t
*routeEntry
)
79 struct in_addr tmpaddr
;
81 uint8_t zebra_flags
= 0;
83 if (routeEntry
->ipRouteInfo
.re_ire_type
& IRE_CACHETABLE
)
86 if (routeEntry
->ipRouteInfo
.re_ire_type
& IRE_HOST_REDIRECT
)
87 zebra_flags
|= ZEBRA_FLAG_SELFROUTE
;
89 prefix
.family
= AF_INET
;
91 tmpaddr
.s_addr
= routeEntry
->ipRouteDest
;
92 prefix
.u
.prefix4
= tmpaddr
;
94 tmpaddr
.s_addr
= routeEntry
->ipRouteMask
;
95 prefix
.prefixlen
= ip_masklen(tmpaddr
);
97 memset(&nh
, 0, sizeof(nh
));
98 nh
.vrf_id
= VRF_DEFAULT
;
99 nh
.type
= NEXTHOP_TYPE_IPV4
;
100 nh
.gate
.ipv4
.s_addr
= routeEntry
->ipRouteNextHop
;
102 rib_add(AFI_IP
, SAFI_UNICAST
, VRF_DEFAULT
, ZEBRA_ROUTE_KERNEL
, 0,
103 zebra_flags
, &prefix
, NULL
, &nh
, 0, 0, 0, 0, 0);
106 void route_read(struct zebra_ns
*zns
)
108 char storage
[RT_BUFSIZ
];
110 struct T_optmgmt_req
*TLIreq
= (struct T_optmgmt_req
*)storage
;
111 struct T_optmgmt_ack
*TLIack
= (struct T_optmgmt_ack
*)storage
;
112 struct T_error_ack
*TLIerr
= (struct T_error_ack
*)storage
;
114 struct opthdr
*MIB2hdr
;
116 mib2_ipRouteEntry_t
*routeEntry
, *lastRouteEntry
;
118 struct strbuf msgdata
;
119 int flags
, dev
, retval
, process
;
121 if ((dev
= open(_PATH_GETMSG_ROUTE
, O_RDWR
)) == -1) {
122 zlog_warn("can't open %s: %s", _PATH_GETMSG_ROUTE
,
123 safe_strerror(errno
));
127 TLIreq
->PRIM_type
= T_OPTMGMT_REQ
;
128 TLIreq
->OPT_offset
= sizeof(struct T_optmgmt_req
);
129 TLIreq
->OPT_length
= sizeof(struct opthdr
);
130 TLIreq
->MGMT_flags
= T_CURRENT
;
132 MIB2hdr
= (struct opthdr
*)&TLIreq
[1];
134 MIB2hdr
->level
= MIB2_IP
;
138 msgdata
.buf
= storage
;
139 msgdata
.len
= sizeof(struct T_optmgmt_req
) + sizeof(struct opthdr
);
143 if (putmsg(dev
, &msgdata
, NULL
, flags
) == -1) {
144 zlog_warn("putmsg failed: %s", safe_strerror(errno
));
148 MIB2hdr
= (struct opthdr
*)&TLIack
[1];
149 msgdata
.maxlen
= sizeof(storage
);
153 retval
= getmsg(dev
, &msgdata
, NULL
, &flags
);
156 zlog_warn("getmsg(ctl) failed: %s",
157 safe_strerror(errno
));
161 /* This is normal loop termination */
163 && (size_t)msgdata
.len
>= sizeof(struct T_optmgmt_ack
)
164 && TLIack
->PRIM_type
== T_OPTMGMT_ACK
165 && TLIack
->MGMT_flags
== T_SUCCESS
&& MIB2hdr
->len
== 0)
168 if ((size_t)msgdata
.len
>= sizeof(struct T_error_ack
)
169 && TLIerr
->PRIM_type
== T_ERROR_ACK
) {
170 zlog_warn("getmsg(ctl) returned T_ERROR_ACK: %s",
171 safe_strerror((TLIerr
->TLI_error
== TSYSERR
)
177 /* should dump more debugging info to the log statement,
178 like what GateD does in this instance, but not
180 if (retval
!= MOREDATA
181 || (size_t)msgdata
.len
< sizeof(struct T_optmgmt_ack
)
182 || TLIack
->PRIM_type
!= T_OPTMGMT_ACK
183 || TLIack
->MGMT_flags
!= T_SUCCESS
) {
185 zlog_warn("getmsg(ctl) returned bizarreness");
189 /* MIB2_IP_21 is the the pseudo-MIB2 ipRouteTable
190 entry, see <inet/mib2.h>. "This isn't the MIB data
191 you're looking for." */
192 process
= (MIB2hdr
->level
== MIB2_IP
193 && MIB2hdr
->name
== MIB2_IP_21
)
197 /* getmsg writes the data buffer out completely, not
198 to the closest smaller multiple. Unless reassembling
199 data structures across buffer boundaries is your idea
200 of a good time, set maxlen to the closest smaller
201 multiple of the size of the datastructure you're
205 - (sizeof(storage
) % sizeof(mib2_ipRouteEntry_t
));
211 retval
= getmsg(dev
, NULL
, &msgdata
, &flags
);
214 zlog_warn("getmsg(data) failed: %s",
215 safe_strerror(errno
));
219 if (!(retval
== 0 || retval
== MOREDATA
)) {
220 zlog_warn("getmsg(data) returned %d", retval
);
225 if (msgdata
.len
% sizeof(mib2_ipRouteEntry_t
)
228 "getmsg(data) returned "
229 "msgdata.len = %d (%% sizeof (mib2_ipRouteEntry_t) != 0)",
234 routeEntry
= (mib2_ipRouteEntry_t
*)msgdata
.buf
;
236 (mib2_ipRouteEntry_t
*)(msgdata
.buf
239 handle_route_entry(routeEntry
);
240 } while (++routeEntry
< lastRouteEntry
);
242 } while (retval
== MOREDATA
);
249 /* Only implemented for netlink method */
250 void macfdb_read(struct zebra_ns
*zns
)
254 void macfdb_read_for_bridge(struct zebra_ns
*zns
, struct interface
*ifp
,
255 struct interface
*br_if
)
259 void neigh_read(struct zebra_ns
*zns
)
263 void neigh_read_for_vlan(struct zebra_ns
*zns
, struct interface
*vlan_if
)
267 void kernel_read_pbr_rules(struct zebra_ns
*zns
)