6039e3f75bb775a4c14a8025e26b94b736ffafe6
[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 "util.h"
33
34 static int pid;
35 static unsigned int register_count = 0;
36
37 bool
38 ovs_router_lookup(ovs_be32 ip, char name[], ovs_be32 *gw)
39 {
40     struct {
41         struct rt_msghdr rtm;
42         char space[512];
43     } rtmsg;
44
45     struct rt_msghdr *rtm = &rtmsg.rtm;
46     struct sockaddr_dl *ifp = NULL;
47     struct sockaddr_in *sin;
48     struct sockaddr *sa;
49     static int seq;
50     int i, len, namelen, rtsock;
51
52     rtsock = socket(PF_ROUTE, SOCK_RAW, 0);
53     if (rtsock < 0)
54         return false;
55
56     memset(&rtmsg, 0, sizeof(rtmsg));
57
58     rtm->rtm_msglen = sizeof(struct rt_msghdr) + sizeof(struct sockaddr_in);
59     rtm->rtm_version = RTM_VERSION;
60     rtm->rtm_type = RTM_GET;
61     rtm->rtm_addrs = RTA_DST | RTA_IFP;
62     rtm->rtm_seq = ++seq;
63
64     sin = (struct sockaddr_in *)(rtm + 1);
65     sin->sin_len = len = sizeof(struct sockaddr_in);
66     sin->sin_family = AF_INET;
67     sin->sin_addr.s_addr = ip;
68
69     if ((write(rtsock, (char *)&rtmsg, rtm->rtm_msglen)) < 0) {
70         close(rtsock);
71         return false;
72     }
73
74     do {
75         len = read(rtsock, (char *)&rtmsg, sizeof(rtmsg));
76     } while (len > 0 && (rtmsg.rtm.rtm_seq != seq ||
77         rtmsg.rtm.rtm_pid != pid));
78
79     close(rtsock);
80
81     if (len < 0) {
82         return false;
83     }
84
85     sa = (struct sockaddr *)(rtm + 1);
86     for (i = 1; i; i <<= 1) {
87         if (rtm->rtm_addrs & i) {
88             if (i == RTA_IFP && sa->sa_family == AF_LINK &&
89               ALIGNED_CAST(struct sockaddr_dl *, sa)->sdl_nlen) {
90                 ifp = ALIGNED_CAST(struct sockaddr_dl *, sa);
91                 namelen = ifp->sdl_nlen;
92                 if (namelen > IFNAMSIZ - 1)
93                     namelen = IFNAMSIZ - 1;
94                 memcpy(name, ifp->sdl_data, namelen);
95                 name[namelen] = '\0';
96                 *gw = 0;
97                 return true;
98             }
99 #if defined(__FreeBSD__)
100             sa = (struct sockaddr *)((char *)sa + SA_SIZE(sa));
101 #elif defined(__NetBSD__)
102             sa = (struct sockaddr *)((char *)sa + RT_ROUNDUP(sa->sa_len));
103 #else
104 #error unimplemented
105 #endif
106         }
107     }
108     return false;
109 }
110
111 uint64_t
112 route_table_get_change_seq(void)
113 {
114     return 0;
115 }
116
117 void
118 route_table_register(void)
119 {
120     if (!register_count)
121     {
122         pid = getpid();
123     }
124
125     register_count++;
126 }
127
128 void
129 route_table_unregister(void)
130 {
131     register_count--;
132 }
133
134 void
135 route_table_run(void)
136 {
137 }
138
139 void
140 route_table_wait(void)
141 {
142 }