remove the need for C99 math (closes: #1579)
[openwrt/svn-archive/archive.git] / net / olsrd / patches / 220-olsrd-fixes-jhay-bsd.patch
1 diff -Nur olsrd-0.4.10.orig/src/bsd/kernel_routes.c olsrd-0.4.10/src/bsd/kernel_routes.c
2 --- olsrd-0.4.10.orig/src/bsd/kernel_routes.c 2005-02-27 11:43:38.000000000 +0100
3 +++ olsrd-0.4.10/src/bsd/kernel_routes.c 2006-12-12 08:59:26.000000000 +0100
4 @@ -170,12 +170,103 @@
5 return add_del_route(dest, 0);
6 }
7
8 -int olsr_ioctl_add_route6(struct rt_entry *dest)
9 +static int add_del_route6(struct rt_entry *dest, int add)
10 {
11 + struct rt_msghdr *rtm;
12 + unsigned char buff[512];
13 + unsigned char *walker;
14 + struct sockaddr_in6 sin6;
15 + struct sockaddr_dl sdl;
16 + int step, step_dl;
17 + int len;
18 + char Str1[40], Str2[40];
19 +
20 + inet_ntop(AF_INET6, &dest->rt_dst.v6, Str1, 40);
21 + inet_ntop(AF_INET6, &dest->rt_router.v6, Str2, 40);
22 +
23 + OLSR_PRINTF(1, "%s IPv6 route to %s/%d via %s.\n",
24 + (add != 0) ? "Adding" : "Removing", Str1, dest->rt_mask.v6, Str2)
25 +
26 + memset(buff, 0, sizeof (buff));
27 + memset(&sin6, 0, sizeof (sin6));
28 + memset(&sdl, 0, sizeof (sdl));
29 +
30 + sin6.sin6_len = sizeof (sin6);
31 + sin6.sin6_family = AF_INET6;
32 + sdl.sdl_len = sizeof (sdl);
33 + sdl.sdl_family = AF_LINK;
34 +
35 + step = 1 + ((sizeof (struct sockaddr_in6) - 1) | 3);
36 + step_dl = 1 + ((sizeof (struct sockaddr_dl) - 1) | 3);
37 +
38 + rtm = (struct rt_msghdr *)buff;
39 + rtm->rtm_version = RTM_VERSION;
40 + rtm->rtm_type = (add != 0) ? RTM_ADD : RTM_DELETE;
41 + rtm->rtm_index = 0;
42 + rtm->rtm_flags = dest->rt_flags;
43 + rtm->rtm_addrs = RTA_DST | RTA_GATEWAY;
44 + rtm->rtm_seq = ++seq;
45 +
46 + walker = buff + sizeof (struct rt_msghdr);
47 +
48 + memcpy(&sin6.sin6_addr.s6_addr, &dest->rt_dst.v6, sizeof(struct in6_addr));
49 +
50 + memcpy(walker, &sin6, sizeof (sin6));
51 + walker += step;
52 +
53 + if ((rtm->rtm_flags & RTF_GATEWAY) != 0)
54 + {
55 + memcpy(&sin6.sin6_addr.s6_addr, &dest->rt_router.v6, sizeof(struct in6_addr));
56 +
57 + memcpy(walker, &sin6, sizeof (sin6));
58 + walker += step;
59 + }
60 +
61 + // the host is directly reachable, so add the output interface's address
62 +
63 + else
64 + {
65 + memcpy(&sin6.sin6_addr.s6_addr, &dest->rt_if->int6_addr.sin6_addr.s6_addr,
66 + sizeof(struct in6_addr));
67 +
68 + memcpy(walker, &sin6, sizeof (sin6));
69 + walker += step;
70 + rtm->rtm_flags |= RTF_LLINFO;
71 + }
72 +
73 + if ((rtm->rtm_flags & RTF_HOST) == 0)
74 + {
75 + olsr_prefix_to_netmask((union olsr_ip_addr *)&sin6.sin6_addr, dest->rt_mask.v6);
76 + memcpy(walker, &sin6, sizeof (sin6));
77 + walker += step;
78 + rtm->rtm_addrs |= RTA_NETMASK;
79 + }
80 +
81 + if ((rtm->rtm_flags & RTF_GATEWAY) != 0)
82 + {
83 + strcpy(&sdl.sdl_data[0], dest->rt_if->int_name);
84 + sdl.sdl_nlen = (u_char)strlen(dest->rt_if->int_name);
85 + memcpy(walker, &sdl, sizeof (sdl));
86 + walker += step_dl;
87 + rtm->rtm_addrs |= RTA_IFP;
88 + }
89 +
90 + rtm->rtm_msglen = (unsigned short)(walker - buff);
91 +
92 + len = write(rts, buff, rtm->rtm_msglen);
93 +
94 + if (len < rtm->rtm_msglen)
95 + fprintf(stderr, "cannot write to routing socket: %s\n", strerror(errno));
96 +
97 return 0;
98 }
99
100 +int olsr_ioctl_add_route6(struct rt_entry *dest)
101 +{
102 + return add_del_route6(dest, 1);
103 +}
104 +
105 int olsr_ioctl_del_route6(struct rt_entry *dest)
106 {
107 - return 0;
108 + return add_del_route6(dest, 0);
109 }
110 diff -Nur olsrd-0.4.10.orig/src/bsd/net.c olsrd-0.4.10/src/bsd/net.c
111 --- olsrd-0.4.10.orig/src/bsd/net.c 2005-08-28 21:30:29.000000000 +0200
112 +++ olsrd-0.4.10/src/bsd/net.c 2006-12-12 08:59:30.000000000 +0100
113 @@ -61,8 +61,10 @@
114 #endif
115
116 #ifdef __FreeBSD__
117 +#include <ifaddrs.h>
118 #include <net/if_var.h>
119 #include <net/ethernet.h>
120 +#include <netinet/in_var.h>
121 #ifndef FBSD_NO_80211
122 #include <net80211/ieee80211.h>
123 #include <net80211/ieee80211_ioctl.h>
124 @@ -71,8 +73,8 @@
125 #endif
126 #endif
127
128 -#ifdef SPOOF
129 #include <net/if_dl.h>
130 +#ifdef SPOOF
131 #include <libnet.h>
132 #endif /* SPOOF */
133
134 @@ -172,6 +174,17 @@
135 name = "net.inet6.icmp6.rediraccept";
136
137 ignore_redir = set_sysctl_int(name, 0);
138 +#elif defined __FreeBSD__
139 + if (olsr_cnf->ip_version == AF_INET)
140 + {
141 + name = "net.inet.icmp.drop_redirect";
142 + ignore_redir = set_sysctl_int(name, 1);
143 + }
144 + else
145 + {
146 + name = "net.inet6.icmp6.rediraccept";
147 + ignore_redir = set_sysctl_int(name, 0);
148 + }
149 #else
150 if (olsr_cnf->ip_version == AF_INET)
151 name = "net.inet.icmp.drop_redirect";
152 @@ -243,6 +256,12 @@
153 else
154 name = "net.inet6.icmp6.rediraccept";
155
156 +#elif defined __FreeBSD__
157 + if (olsr_cnf->ip_version == AF_INET)
158 + name = "net.inet.icmp.drop_redirect";
159 +
160 + else
161 + name = "net.inet6.icmp6.rediraccept";
162 #else
163 if (olsr_cnf->ip_version == AF_INET)
164 name = "net.inet.icmp.drop_redirect";
165 @@ -335,7 +354,6 @@
166 return (-1);
167 }
168
169 -#ifdef SPOOF
170 if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &on, sizeof(on)) < 0)
171 {
172 perror("SO_REUSEPORT failed");
173 @@ -347,7 +365,6 @@
174 perror("IP_RECVIF failed");
175 return (-1);
176 }
177 -#endif /* SPOOF */
178
179 for (on = bufspace; ; on -= 1024)
180 {
181 @@ -406,6 +423,18 @@
182 return (-1);
183 }
184
185 + if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &on, sizeof(on)) < 0)
186 + {
187 + perror("SO_REUSEPORT failed");
188 + return (-1);
189 + }
190 +
191 + if (setsockopt(sock, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on, sizeof(on)) < 0)
192 + {
193 + perror("IPV6_RECVPKTINFO failed");
194 + return (-1);
195 + }
196 +
197 if (bind(sock, (struct sockaddr *)sin, sizeof (*sin)) < 0)
198 {
199 perror("bind");
200 @@ -425,31 +454,16 @@
201 int
202 join_mcast(struct interface *ifs, int sock)
203 {
204 - /* See linux/in6.h */
205 + /* See netinet6/in6.h */
206
207 struct ipv6_mreq mcastreq;
208
209 COPY_IP(&mcastreq.ipv6mr_multiaddr, &ifs->int6_multaddr.sin6_addr);
210 mcastreq.ipv6mr_interface = ifs->if_index;
211
212 -#if 0
213 OLSR_PRINTF(3, "Interface %s joining multicast %s...", ifs->int_name, olsr_ip_to_string((union olsr_ip_addr *)&ifs->int6_multaddr.sin6_addr))
214 - /* Send multicast */
215 - if(setsockopt(sock,
216 - IPPROTO_IPV6,
217 - IPV6_ADD_MEMBERSHIP,
218 - (char *)&mcastreq,
219 - sizeof(struct ipv6_mreq))
220 - < 0)
221 - {
222 - perror("Join multicast");
223 - return -1;
224 - }
225 -#else
226 -#warning implement IPV6_ADD_MEMBERSHIP
227 -#endif
228
229 - /* Old libc fix */
230 + /* rfc 3493 */
231 #ifdef IPV6_JOIN_GROUP
232 /* Join reciever group */
233 if(setsockopt(sock,
234 @@ -458,8 +472,8 @@
235 (char *)&mcastreq,
236 sizeof(struct ipv6_mreq))
237 < 0)
238 -#else
239 - /* Join reciever group */
240 +#else /* rfc 2133, obsoleted */
241 + /* Join receiver group */
242 if(setsockopt(sock,
243 IPPROTO_IPV6,
244 IPV6_ADD_MEMBERSHIP,
245 @@ -494,6 +508,70 @@
246
247 int get_ipv6_address(char *ifname, struct sockaddr_in6 *saddr6, int scope_in)
248 {
249 + struct ifaddrs *ifap, *ifa;
250 + const struct sockaddr_in6 *sin6 = NULL;
251 + struct in6_ifreq ifr6;
252 + int found = 0;
253 + int s6;
254 + u_int32_t flags6;
255 +
256 + if (getifaddrs(&ifap) != 0)
257 + {
258 + OLSR_PRINTF(3, "get_ipv6_address: getifaddrs() failed.\n")
259 + return 0;
260 + }
261 +
262 + for (ifa = ifap; ifa; ifa = ifa->ifa_next)
263 + {
264 + if (ifa->ifa_addr->sa_family == AF_INET6 &&
265 + strcmp(ifa->ifa_name, ifname) == 0)
266 + {
267 + sin6 = (const struct sockaddr_in6 *)ifa->ifa_addr;
268 + if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
269 + continue;
270 + strncpy(ifr6.ifr_name, ifname, sizeof(ifname));
271 + if ((s6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
272 + {
273 + OLSR_PRINTF(3, "socket(AF_INET6,SOCK_DGRAM)");
274 + break;
275 + }
276 + ifr6.ifr_addr = *sin6;
277 + if (ioctl(s6, SIOCGIFAFLAG_IN6, &ifr6) < 0)
278 + {
279 + OLSR_PRINTF(3, "ioctl(SIOCGIFAFLAG_IN6)");
280 + close(s6);
281 + break;
282 + }
283 + close(s6);
284 + flags6 = ifr6.ifr_ifru.ifru_flags6;
285 + if ((flags6 & IN6_IFF_ANYCAST) != 0)
286 + continue;
287 + if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))
288 + {
289 + if (scope_in)
290 + {
291 + memcpy(&saddr6->sin6_addr, &sin6->sin6_addr,
292 + sizeof(struct in6_addr));
293 + found = 1;
294 + break;
295 + }
296 + }
297 + else
298 + {
299 + if (scope_in == 0)
300 + {
301 + memcpy(&saddr6->sin6_addr, &sin6->sin6_addr,
302 + sizeof(struct in6_addr));
303 + found = 1;
304 + break;
305 + }
306 + }
307 + }
308 + }
309 + freeifaddrs(ifap);
310 + if (found)
311 + return 1;
312 +
313 return 0;
314 }
315
316 @@ -621,16 +699,19 @@
317 struct sockaddr *from,
318 socklen_t *fromlen)
319 {
320 -#if SPOOF
321 struct msghdr mhdr;
322 struct iovec iov;
323 struct cmsghdr *cm;
324 struct sockaddr_dl *sdl;
325 struct sockaddr_in *sin = (struct sockaddr_in *) from; //XXX
326 + struct sockaddr_in6 *sin6;
327 + struct in6_addr *iaddr6;
328 + struct in6_pktinfo *pkti;
329 + struct interface *ifc;
330 + char addrstr[INET6_ADDRSTRLEN];
331 + char iname[IFNAMSIZ];
332 unsigned char chdr[4096];
333 int count;
334 - struct interface *ifc;
335 - char iname[32];
336
337 bzero(&mhdr, sizeof(mhdr));
338 bzero(&iov, sizeof(iov));
339 @@ -653,35 +734,45 @@
340
341 /* this needs to get communicated back to caller */
342 *fromlen = mhdr.msg_namelen;
343 -
344 - cm = (struct cmsghdr *) chdr;
345 - sdl = (struct sockaddr_dl *) CMSG_DATA (cm);
346 - bzero (iname, sizeof (iname));
347 - memcpy (iname, sdl->sdl_data, sdl->sdl_nlen);
348 + if (olsr_cnf->ip_version == AF_INET6)
349 + {
350 + for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(&mhdr); cm;
351 + cm = (struct cmsghdr *)CMSG_NXTHDR(&mhdr, cm))
352 + {
353 + if (cm->cmsg_level == IPPROTO_IPV6 && cm->cmsg_type == IPV6_PKTINFO)
354 + {
355 + pkti = (struct in6_pktinfo *) CMSG_DATA(cm);
356 + iaddr6 = &pkti->ipi6_addr;
357 + if_indextoname(pkti->ipi6_ifindex, iname);
358 + }
359 + }
360 + }
361 + else
362 + {
363 + cm = (struct cmsghdr *) chdr;
364 + sdl = (struct sockaddr_dl *) CMSG_DATA (cm);
365 + bzero (iname, sizeof (iname));
366 + memcpy (iname, sdl->sdl_data, sdl->sdl_nlen);
367 + }
368
369 ifc = if_ifwithsock (s);
370
371 + sin6 = (struct sockaddr_in6 *)from;
372 + OLSR_PRINTF (4, "%d bytes from %s, socket associated %s really received on %s\n",
373 + count,
374 + (olsr_cnf->ip_version == AF_INET6) ?
375 + inet_ntop(AF_INET6, (char *)&sin6->sin6_addr, addrstr,
376 + INET6_ADDRSTRLEN):
377 + inet_ntoa (sin->sin_addr),
378 + ifc->int_name,
379 + iname);
380 +
381 if (strcmp (ifc->int_name, iname) != 0)
382 {
383 return (0);
384 }
385
386 - OLSR_PRINTF (2, "%d bytes from %s, socket associated %s really received on %s\n",
387 - count,
388 - inet_ntoa (sin->sin_addr),
389 - ifc->int_name,
390 - iname);
391 -
392 return (count);
393 -
394 -#else /* SPOOF */
395 - return recvfrom(s,
396 - buf,
397 - len,
398 - 0,
399 - from,
400 - fromlen);
401 -#endif /* SPOOF */
402 }
403
404 /**
405 diff -Nur olsrd-0.4.10.orig/src/net_olsr.c olsrd-0.4.10/src/net_olsr.c
406 --- olsrd-0.4.10.orig/src/net_olsr.c 2005-12-29 19:37:16.000000000 +0100
407 +++ olsrd-0.4.10/src/net_olsr.c 2006-12-12 08:59:35.000000000 +0100
408 @@ -526,7 +526,7 @@
409
410 for(;p > 0; p -= 8)
411 {
412 - adr->v6.s6_addr[i] = (p < 8) ? 0xff ^ (0xff << p) : 0xff;
413 + adr->v6.s6_addr[i] = (p < 8) ? 0xff ^ (0xff >> p) : 0xff;
414 i++;
415 }
416
417 @@ -564,7 +564,7 @@
418 {
419 for(tmp = adr->v6.s6_addr[i];
420 tmp > 0;
421 - tmp = tmp >> 1)
422 + tmp = (tmp << 1) & 0xff)
423 prefix++;
424 }
425 }