]>
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
29 #include "zebra/rib.h"
31 #include <sys/stream.h>
32 #include <sys/tihdr.h>
34 /* Solaris defines these in both <netinet/in.h> and <inet/in.h>, sigh */
36 #include <sys/tiuser.h>
38 #define T_CURRENT MI_T_CURRENT
39 #endif /* T_CURRENT */
41 #define IRE_CACHE 0x0020 /* Cached Route entry */
42 #endif /* IRE_CACHE */
43 #ifndef IRE_HOST_REDIRECT
44 #define IRE_HOST_REDIRECT 0x0200 /* Host route entry from redirects */
45 #endif /* IRE_HOST_REDIRECT */
46 #ifndef IRE_CACHETABLE
47 #define IRE_CACHETABLE (IRE_CACHE | IRE_BROADCAST | IRE_LOCAL | \
49 #endif /* IRE_CACHETABLE */
57 #include <inet/common.h>
59 #include <inet/mib2.h>
61 /* device to read IP routing table from */
62 #ifndef _PATH_GETMSG_ROUTE
63 #define _PATH_GETMSG_ROUTE "/dev/ip"
64 #endif /* _PATH_GETMSG_ROUTE */
66 #define RT_BUFSIZ 8192
68 void handle_route_entry (mib2_ipRouteEntry_t
*routeEntry
)
70 struct prefix_ipv4 prefix
;
71 struct in_addr tmpaddr
, gateway
;
72 u_char zebra_flags
= 0;
74 if (routeEntry
->ipRouteInfo
.re_ire_type
& IRE_CACHETABLE
)
77 if (routeEntry
->ipRouteInfo
.re_ire_type
& IRE_HOST_REDIRECT
)
78 zebra_flags
|= ZEBRA_FLAG_SELFROUTE
;
80 prefix
.family
= AF_INET
;
82 tmpaddr
.s_addr
= routeEntry
->ipRouteDest
;
83 prefix
.prefix
= tmpaddr
;
85 tmpaddr
.s_addr
= routeEntry
->ipRouteMask
;
86 prefix
.prefixlen
= ip_masklen (tmpaddr
);
88 gateway
.s_addr
= routeEntry
->ipRouteNextHop
;
90 rib_add_ipv4 (ZEBRA_ROUTE_KERNEL
, zebra_flags
, &prefix
,
91 &gateway
, 0, 0, 0, 0);
96 char storage
[RT_BUFSIZ
];
98 struct T_optmgmt_req
*TLIreq
= (struct T_optmgmt_req
*) storage
;
99 struct T_optmgmt_ack
*TLIack
= (struct T_optmgmt_ack
*) storage
;
100 struct T_error_ack
*TLIerr
= (struct T_error_ack
*) storage
;
102 struct opthdr
*MIB2hdr
;
104 mib2_ipRouteEntry_t
*routeEntry
, *lastRouteEntry
;
106 struct strbuf msgdata
;
107 int flags
, dev
, retval
, process
;
109 if ((dev
= open (_PATH_GETMSG_ROUTE
, O_RDWR
)) == -1) {
110 zlog_warn ("can't open %s: %s", _PATH_GETMSG_ROUTE
,
115 TLIreq
->PRIM_type
= T_OPTMGMT_REQ
;
116 TLIreq
->OPT_offset
= sizeof (struct T_optmgmt_req
);
117 TLIreq
->OPT_length
= sizeof (struct opthdr
);
118 TLIreq
->MGMT_flags
= T_CURRENT
;
120 MIB2hdr
= (struct opthdr
*) &TLIreq
[1];
122 MIB2hdr
->level
= MIB2_IP
;
126 msgdata
.buf
= storage
;
127 msgdata
.len
= sizeof (struct T_optmgmt_req
) + sizeof (struct opthdr
);
131 if (putmsg (dev
, &msgdata
, NULL
, flags
) == -1) {
132 zlog_warn ("putmsg failed: %s", strerror (errno
));
136 MIB2hdr
= (struct opthdr
*) &TLIack
[1];
137 msgdata
.maxlen
= sizeof (storage
);
141 retval
= getmsg (dev
, &msgdata
, NULL
, &flags
);
144 zlog_warn ("getmsg(ctl) failed: %s", strerror (errno
));
148 /* This is normal loop termination */
150 msgdata
.len
>= sizeof (struct T_optmgmt_ack
) &&
151 TLIack
->PRIM_type
== T_OPTMGMT_ACK
&&
152 TLIack
->MGMT_flags
== T_SUCCESS
&&
156 if (msgdata
.len
>= sizeof (struct T_error_ack
) &&
157 TLIerr
->PRIM_type
== T_ERROR_ACK
) {
158 zlog_warn ("getmsg(ctl) returned T_ERROR_ACK: %s",
159 strerror ((TLIerr
->TLI_error
== TSYSERR
)
160 ? TLIerr
->UNIX_error
: EPROTO
));
164 /* should dump more debugging info to the log statement,
165 like what GateD does in this instance, but not
167 if (retval
!= MOREDATA
||
168 msgdata
.len
< sizeof (struct T_optmgmt_ack
) ||
169 TLIack
->PRIM_type
!= T_OPTMGMT_ACK
||
170 TLIack
->MGMT_flags
!= T_SUCCESS
) {
172 zlog_warn ("getmsg(ctl) returned bizarreness");
176 /* MIB2_IP_21 is the the pseudo-MIB2 ipRouteTable
177 entry, see <inet/mib2.h>. "This isn't the MIB data
178 you're looking for." */
179 process
= (MIB2hdr
->level
== MIB2_IP
&&
180 MIB2hdr
->name
== MIB2_IP_21
) ? 1 : 0;
182 /* getmsg writes the data buffer out completely, not
183 to the closest smaller multiple. Unless reassembling
184 data structures across buffer boundaries is your idea
185 of a good time, set maxlen to the closest smaller
186 multiple of the size of the datastructure you're
188 msgdata
.maxlen
= sizeof (storage
) - (sizeof (storage
)
189 % sizeof (mib2_ipRouteEntry_t
));
195 retval
= getmsg (dev
, NULL
, &msgdata
, &flags
);
198 zlog_warn ("getmsg(data) failed: %s",
203 if (!(retval
== 0 || retval
== MOREDATA
)) {
204 zlog_warn ("getmsg(data) returned %d", retval
);
210 sizeof (mib2_ipRouteEntry_t
) != 0) {
211 zlog_warn ("getmsg(data) returned "
212 "msgdata.len = %d (%% sizeof (mib2_ipRouteEntry_t) != 0)", msgdata
.len
);
216 routeEntry
= (mib2_ipRouteEntry_t
*)
218 lastRouteEntry
= (mib2_ipRouteEntry_t
*)
219 (msgdata
.buf
+ msgdata
.len
);
221 handle_route_entry (routeEntry
);
222 } while (++routeEntry
< lastRouteEntry
);
224 } while (retval
== MOREDATA
);