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