2 * Copyright (c) 2012 Ed Maste. All rights reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "route-table.h"
21 #include <sys/socket.h>
22 #include <sys/types.h>
25 #include <net/route.h>
26 #include <net/if_dl.h>
27 #include <netinet/in.h>
32 #include "ovs-router.h"
37 VLOG_DEFINE_THIS_MODULE(route_table_bsd);
40 route_table_fallback_lookup(ovs_be32 ip, char name[], ovs_be32 *gw)
47 struct rt_msghdr *rtm = &rtmsg.rtm;
48 struct sockaddr_dl *ifp = NULL;
49 struct sockaddr_in *sin;
52 int i, len, namelen, rtsock;
53 const pid_t pid = getpid();
56 rtsock = socket(PF_ROUTE, SOCK_RAW, 0);
60 memset(&rtmsg, 0, sizeof(rtmsg));
62 rtm->rtm_msglen = sizeof(struct rt_msghdr) + sizeof(struct sockaddr_in);
63 rtm->rtm_version = RTM_VERSION;
64 rtm->rtm_type = RTM_GET;
65 rtm->rtm_addrs = RTA_DST | RTA_IFP;
68 sin = (struct sockaddr_in *)(rtm + 1);
69 sin->sin_len = len = sizeof(struct sockaddr_in);
70 sin->sin_family = AF_INET;
71 sin->sin_addr.s_addr = ip;
73 if ((write(rtsock, (char *)&rtmsg, rtm->rtm_msglen)) < 0) {
78 VLOG_DBG("looking route up for " IP_FMT " pid %" PRIuMAX,
79 IP_ARGS(ip), (uintmax_t)pid);
81 len = read(rtsock, (char *)&rtmsg, sizeof(rtmsg));
83 VLOG_DBG("got rtmsg pid %" PRIuMAX " seq %d",
84 (uintmax_t)rtmsg.rtm.rtm_pid,
87 } while (len > 0 && (rtmsg.rtm.rtm_seq != seq ||
88 rtmsg.rtm.rtm_pid != pid));
97 sa = (struct sockaddr *)(rtm + 1);
98 for (i = 1; i; i <<= 1) {
99 if (rtm->rtm_addrs & i) {
100 if (i == RTA_IFP && sa->sa_family == AF_LINK &&
101 ALIGNED_CAST(struct sockaddr_dl *, sa)->sdl_nlen) {
102 ifp = ALIGNED_CAST(struct sockaddr_dl *, sa);
103 namelen = ifp->sdl_nlen;
104 if (namelen > IFNAMSIZ - 1)
105 namelen = IFNAMSIZ - 1;
106 memcpy(name, ifp->sdl_data, namelen);
107 name[namelen] = '\0';
108 VLOG_DBG("got ifp %s", name);
110 } else if (i == RTA_GATEWAY && sa->sa_family == AF_INET) {
111 const struct sockaddr_in *sin_dst =
112 ALIGNED_CAST(struct sockaddr_in *, sa);
114 *gw = sin_dst->sin_addr.s_addr;
115 VLOG_DBG("got gateway " IP_FMT, IP_ARGS(*gw));
117 #if defined(__FreeBSD__)
118 sa = (struct sockaddr *)((char *)sa + SA_SIZE(sa));
119 #elif defined(__NetBSD__)
120 sa = (struct sockaddr *)((char *)sa + RT_ROUNDUP(sa->sa_len));
130 route_table_get_change_seq(void)
136 route_table_init(void)
142 route_table_run(void)
147 route_table_wait(void)