lib/route-table-*: Fix non-Linux builds
[cascardo/ovs.git] / lib / route-table-bsd.c
1 /*
2  * Copyright (c) 2012 Ed Maste. All rights reserved.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <config.h>
18
19 #include "route-table.h"
20
21 #include <sys/socket.h>
22 #include <sys/types.h>
23
24 #include <net/if.h>
25 #include <net/route.h>
26 #include <net/if_dl.h>
27 #include <netinet/in.h>
28
29 #include <string.h>
30 #include <unistd.h>
31
32 #include "ovs-router.h"
33 #include "util.h"
34
35 static int pid;
36 static unsigned int register_count = 0;
37
38 bool
39 ovs_router_lookup(ovs_be32 ip, char name[], ovs_be32 *gw)
40 {
41     struct {
42         struct rt_msghdr rtm;
43         char space[512];
44     } rtmsg;
45
46     struct rt_msghdr *rtm = &rtmsg.rtm;
47     struct sockaddr_dl *ifp = NULL;
48     struct sockaddr_in *sin;
49     struct sockaddr *sa;
50     static int seq;
51     int i, len, namelen, rtsock;
52
53     rtsock = socket(PF_ROUTE, SOCK_RAW, 0);
54     if (rtsock < 0)
55         return false;
56
57     memset(&rtmsg, 0, sizeof(rtmsg));
58
59     rtm->rtm_msglen = sizeof(struct rt_msghdr) + sizeof(struct sockaddr_in);
60     rtm->rtm_version = RTM_VERSION;
61     rtm->rtm_type = RTM_GET;
62     rtm->rtm_addrs = RTA_DST | RTA_IFP;
63     rtm->rtm_seq = ++seq;
64
65     sin = (struct sockaddr_in *)(rtm + 1);
66     sin->sin_len = len = sizeof(struct sockaddr_in);
67     sin->sin_family = AF_INET;
68     sin->sin_addr.s_addr = ip;
69
70     if ((write(rtsock, (char *)&rtmsg, rtm->rtm_msglen)) < 0) {
71         close(rtsock);
72         return false;
73     }
74
75     do {
76         len = read(rtsock, (char *)&rtmsg, sizeof(rtmsg));
77     } while (len > 0 && (rtmsg.rtm.rtm_seq != seq ||
78         rtmsg.rtm.rtm_pid != pid));
79
80     close(rtsock);
81
82     if (len < 0) {
83         return false;
84     }
85
86     sa = (struct sockaddr *)(rtm + 1);
87     for (i = 1; i; i <<= 1) {
88         if (rtm->rtm_addrs & i) {
89             if (i == RTA_IFP && sa->sa_family == AF_LINK &&
90               ALIGNED_CAST(struct sockaddr_dl *, sa)->sdl_nlen) {
91                 ifp = ALIGNED_CAST(struct sockaddr_dl *, sa);
92                 namelen = ifp->sdl_nlen;
93                 if (namelen > IFNAMSIZ - 1)
94                     namelen = IFNAMSIZ - 1;
95                 memcpy(name, ifp->sdl_data, namelen);
96                 name[namelen] = '\0';
97                 *gw = 0;
98                 return true;
99             }
100 #if defined(__FreeBSD__)
101             sa = (struct sockaddr *)((char *)sa + SA_SIZE(sa));
102 #elif defined(__NetBSD__)
103             sa = (struct sockaddr *)((char *)sa + RT_ROUNDUP(sa->sa_len));
104 #else
105 #error unimplemented
106 #endif
107         }
108     }
109     return false;
110 }
111
112 uint64_t
113 route_table_get_change_seq(void)
114 {
115     return 0;
116 }
117
118 void
119 route_table_register(void)
120 {
121     if (!register_count)
122     {
123         pid = getpid();
124     }
125
126     register_count++;
127 }
128
129 void
130 route_table_unregister(void)
131 {
132     register_count--;
133 }
134
135 void
136 route_table_run(void)
137 {
138 }
139
140 void
141 route_table_wait(void)
142 {
143 }
144
145 void
146 ovs_router_init(void)
147 {
148 }