blob: 6788391ab90c57bc3dfcc53f03200e09466683a7 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
2 * Copyright 2008, The Android Open Source Project
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 <stdio.h>
18#include <stdlib.h>
19#include <unistd.h>
20#include <string.h>
21#include <errno.h>
22
23#include <sys/socket.h>
24#include <sys/select.h>
25#include <sys/types.h>
26#include <netinet/in.h>
27#include <arpa/inet.h>
Dmitry Shmidt7d05a802011-01-24 17:10:30 -080028#include <net/if.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080029
30#include <linux/if.h>
Szymon Jakubczakc88e09c2010-06-09 16:11:09 -040031#include <linux/if_ether.h>
32#include <linux/if_arp.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080033#include <linux/sockios.h>
34#include <linux/route.h>
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +053035#include <linux/ipv6_route.h>
36#include <netdb.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080037#include <linux/wireless.h>
38
39#ifdef ANDROID
40#define LOG_TAG "NetUtils"
41#include <cutils/log.h>
42#include <cutils/properties.h>
43#else
44#include <stdio.h>
45#include <string.h>
46#define LOGD printf
47#define LOGW printf
48#endif
49
50static int ifc_ctl_sock = -1;
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +053051static int ifc_ctl_sock6 = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080052void printerr(char *fmt, ...);
53
Szymon Jakubczakc88e09c2010-06-09 16:11:09 -040054static const char *ipaddr_to_string(in_addr_t addr)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080055{
56 struct in_addr in_addr;
57
58 in_addr.s_addr = addr;
59 return inet_ntoa(in_addr);
60}
61
62int ifc_init(void)
63{
64 if (ifc_ctl_sock == -1) {
65 ifc_ctl_sock = socket(AF_INET, SOCK_DGRAM, 0);
66 if (ifc_ctl_sock < 0) {
67 printerr("socket() failed: %s\n", strerror(errno));
68 }
69 }
70 return ifc_ctl_sock < 0 ? -1 : 0;
71}
72
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +053073int ifc_init6(void)
74{
75 if (ifc_ctl_sock6 == -1) {
76 ifc_ctl_sock6 = socket(AF_INET6, SOCK_DGRAM, 0);
77 if (ifc_ctl_sock6 < 0) {
78 printerr("socket() failed: %s\n", strerror(errno));
79 }
80 }
81 return ifc_ctl_sock6 < 0 ? -1 : 0;
82}
83
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080084void ifc_close(void)
85{
86 if (ifc_ctl_sock != -1) {
87 (void)close(ifc_ctl_sock);
88 ifc_ctl_sock = -1;
89 }
90}
91
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +053092void ifc_close6(void)
93{
94 if (ifc_ctl_sock6 != -1) {
95 (void)close(ifc_ctl_sock6);
96 ifc_ctl_sock6 = -1;
97 }
98}
99
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800100static void ifc_init_ifr(const char *name, struct ifreq *ifr)
101{
102 memset(ifr, 0, sizeof(struct ifreq));
103 strncpy(ifr->ifr_name, name, IFNAMSIZ);
104 ifr->ifr_name[IFNAMSIZ - 1] = 0;
105}
106
107int ifc_get_hwaddr(const char *name, void *ptr)
108{
109 int r;
110 struct ifreq ifr;
111 ifc_init_ifr(name, &ifr);
112
113 r = ioctl(ifc_ctl_sock, SIOCGIFHWADDR, &ifr);
114 if(r < 0) return -1;
115
Szymon Jakubczakc88e09c2010-06-09 16:11:09 -0400116 memcpy(ptr, &ifr.ifr_hwaddr.sa_data, ETH_ALEN);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800117 return 0;
118}
119
120int ifc_get_ifindex(const char *name, int *if_indexp)
121{
122 int r;
123 struct ifreq ifr;
124 ifc_init_ifr(name, &ifr);
125
126 r = ioctl(ifc_ctl_sock, SIOCGIFINDEX, &ifr);
127 if(r < 0) return -1;
128
129 *if_indexp = ifr.ifr_ifindex;
Dmitry Shmidt7d05a802011-01-24 17:10:30 -0800130 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800131}
132
133static int ifc_set_flags(const char *name, unsigned set, unsigned clr)
134{
135 struct ifreq ifr;
136 ifc_init_ifr(name, &ifr);
137
138 if(ioctl(ifc_ctl_sock, SIOCGIFFLAGS, &ifr) < 0) return -1;
139 ifr.ifr_flags = (ifr.ifr_flags & (~clr)) | set;
140 return ioctl(ifc_ctl_sock, SIOCSIFFLAGS, &ifr);
141}
142
143int ifc_up(const char *name)
144{
145 return ifc_set_flags(name, IFF_UP, 0);
146}
147
148int ifc_down(const char *name)
149{
150 return ifc_set_flags(name, 0, IFF_UP);
151}
152
153static void init_sockaddr_in(struct sockaddr *sa, in_addr_t addr)
154{
155 struct sockaddr_in *sin = (struct sockaddr_in *) sa;
156 sin->sin_family = AF_INET;
157 sin->sin_port = 0;
158 sin->sin_addr.s_addr = addr;
159}
160
161int ifc_set_addr(const char *name, in_addr_t addr)
162{
163 struct ifreq ifr;
164
165 ifc_init_ifr(name, &ifr);
166 init_sockaddr_in(&ifr.ifr_addr, addr);
Dmitry Shmidt7d05a802011-01-24 17:10:30 -0800167
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800168 return ioctl(ifc_ctl_sock, SIOCSIFADDR, &ifr);
169}
170
Szymon Jakubczakc88e09c2010-06-09 16:11:09 -0400171int ifc_set_hwaddr(const char *name, const void *ptr)
172{
173 int r;
174 struct ifreq ifr;
175 ifc_init_ifr(name, &ifr);
176
177 ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
178 memcpy(&ifr.ifr_hwaddr.sa_data, ptr, ETH_ALEN);
179 return ioctl(ifc_ctl_sock, SIOCSIFHWADDR, &ifr);
180}
181
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800182int ifc_set_mask(const char *name, in_addr_t mask)
183{
184 struct ifreq ifr;
185
186 ifc_init_ifr(name, &ifr);
187 init_sockaddr_in(&ifr.ifr_addr, mask);
Dmitry Shmidt7d05a802011-01-24 17:10:30 -0800188
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800189 return ioctl(ifc_ctl_sock, SIOCSIFNETMASK, &ifr);
190}
191
Dmitry Shmidt9092b912011-01-27 14:16:20 -0800192int ifc_get_addr(const char *name, in_addr_t *addr)
193{
194 struct ifreq ifr;
195 int ret = 0;
196
197 ifc_init_ifr(name, &ifr);
198 if (addr != NULL) {
199 ret = ioctl(ifc_ctl_sock, SIOCGIFADDR, &ifr);
200 if (ret < 0) {
201 *addr = 0;
202 } else {
203 *addr = ((struct sockaddr_in*) &ifr.ifr_addr)->sin_addr.s_addr;
204 }
205 }
206 return ret;
207}
208
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800209int ifc_get_info(const char *name, in_addr_t *addr, in_addr_t *mask, unsigned *flags)
210{
211 struct ifreq ifr;
212 ifc_init_ifr(name, &ifr);
213
214 if (addr != NULL) {
215 if(ioctl(ifc_ctl_sock, SIOCGIFADDR, &ifr) < 0) {
216 *addr = 0;
217 } else {
218 *addr = ((struct sockaddr_in*) &ifr.ifr_addr)->sin_addr.s_addr;
219 }
220 }
Dmitry Shmidt7d05a802011-01-24 17:10:30 -0800221
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800222 if (mask != NULL) {
223 if(ioctl(ifc_ctl_sock, SIOCGIFNETMASK, &ifr) < 0) {
224 *mask = 0;
225 } else {
226 *mask = ((struct sockaddr_in*) &ifr.ifr_addr)->sin_addr.s_addr;
227 }
228 }
229
230 if (flags != NULL) {
231 if(ioctl(ifc_ctl_sock, SIOCGIFFLAGS, &ifr) < 0) {
232 *flags = 0;
233 } else {
234 *flags = ifr.ifr_flags;
235 }
236 }
237
238 return 0;
239}
240
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530241in_addr_t get_ipv4_netmask(int prefix_length)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800242{
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530243 in_addr_t mask = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800244
Stan Chesnutt36f93f02010-12-16 10:59:48 -0800245 // C99 (6.5.7): shifts of 32 bits have undefined results
246 if (prefix_length == 0) {
247 return 0;
248 }
249
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530250 mask = ~mask << (32 - prefix_length);
251 mask = htonl(mask);
252
253 return mask;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800254}
255
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530256int ifc_add_ipv4_route(const char *ifname, struct in_addr dst, int prefix_length,
257 struct in_addr gw)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800258{
259 struct rtentry rt;
260 int result;
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530261 in_addr_t netmask;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800262
263 memset(&rt, 0, sizeof(rt));
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530264
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800265 rt.rt_dst.sa_family = AF_INET;
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530266 rt.rt_dev = (void*) ifname;
267
268 netmask = get_ipv4_netmask(prefix_length);
269 init_sockaddr_in(&rt.rt_genmask, netmask);
270 init_sockaddr_in(&rt.rt_dst, dst.s_addr);
271 rt.rt_flags = RTF_UP;
272
273 if (prefix_length == 32) {
274 rt.rt_flags |= RTF_HOST;
275 }
276
277 if (gw.s_addr != 0) {
278 rt.rt_flags |= RTF_GATEWAY;
279 init_sockaddr_in(&rt.rt_gateway, gw.s_addr);
280 }
281
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800282 ifc_init();
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530283
284 if (ifc_ctl_sock < 0) {
285 return -errno;
286 }
287
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800288 result = ioctl(ifc_ctl_sock, SIOCADDRT, &rt);
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530289 if (result < 0) {
290 if (errno == EEXIST) {
291 result = 0;
292 } else {
293 result = -errno;
294 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800295 }
296 ifc_close();
297 return result;
298}
299
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530300int ifc_create_default_route(const char *name, in_addr_t gw)
301{
302 struct in_addr in_dst, in_gw;
303
304 in_dst.s_addr = 0;
305 in_gw.s_addr = gw;
306
307 return ifc_add_ipv4_route(name, in_dst, 0, in_gw);
308}
309
310int ifc_add_host_route(const char *name, in_addr_t dst)
311{
312 struct in_addr in_dst, in_gw;
313
314 in_dst.s_addr = dst;
315 in_gw.s_addr = 0;
316
317 return ifc_add_ipv4_route(name, in_dst, 32, in_gw);
318}
319
Mike Lockwoodfeb63e92009-07-10 17:21:17 -0400320int ifc_enable(const char *ifname)
321{
322 int result;
323
324 ifc_init();
325 result = ifc_up(ifname);
326 ifc_close();
327 return result;
328}
329
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800330int ifc_disable(const char *ifname)
331{
Dmitry Shmidt9092b912011-01-27 14:16:20 -0800332 unsigned addr, count;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800333 int result;
334
335 ifc_init();
336 result = ifc_down(ifname);
Dmitry Shmidt9092b912011-01-27 14:16:20 -0800337
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800338 ifc_set_addr(ifname, 0);
Dmitry Shmidt9092b912011-01-27 14:16:20 -0800339 for (count=0, addr=1;((addr != 0) && (count < 255)); count++) {
340 if (ifc_get_addr(ifname, &addr) < 0)
341 break;
342 if (addr)
343 ifc_set_addr(ifname, 0);
344 }
345
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800346 ifc_close();
347 return result;
348}
349
350int ifc_reset_connections(const char *ifname)
351{
352#ifdef HAVE_ANDROID_OS
353 int result;
354 in_addr_t myaddr;
355 struct ifreq ifr;
356
357 ifc_init();
358 ifc_get_info(ifname, &myaddr, NULL, NULL);
359 ifc_init_ifr(ifname, &ifr);
360 init_sockaddr_in(&ifr.ifr_addr, myaddr);
361 result = ioctl(ifc_ctl_sock, SIOCKILLADDR, &ifr);
362 ifc_close();
Dmitry Shmidt7d05a802011-01-24 17:10:30 -0800363
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800364 return result;
365#else
366 return 0;
367#endif
368}
369
370/*
371 * Remove the routes associated with the named interface.
372 */
373int ifc_remove_host_routes(const char *name)
374{
375 char ifname[64];
376 in_addr_t dest, gway, mask;
377 int flags, refcnt, use, metric, mtu, win, irtt;
378 struct rtentry rt;
379 FILE *fp;
380 struct in_addr addr;
381
382 fp = fopen("/proc/net/route", "r");
383 if (fp == NULL)
384 return -1;
385 /* Skip the header line */
386 if (fscanf(fp, "%*[^\n]\n") < 0) {
387 fclose(fp);
388 return -1;
389 }
390 ifc_init();
391 for (;;) {
392 int nread = fscanf(fp, "%63s%X%X%X%d%d%d%X%d%d%d\n",
393 ifname, &dest, &gway, &flags, &refcnt, &use, &metric, &mask,
394 &mtu, &win, &irtt);
395 if (nread != 11) {
396 break;
397 }
398 if ((flags & (RTF_UP|RTF_HOST)) != (RTF_UP|RTF_HOST)
399 || strcmp(ifname, name) != 0) {
400 continue;
401 }
402 memset(&rt, 0, sizeof(rt));
403 rt.rt_dev = (void *)name;
404 init_sockaddr_in(&rt.rt_dst, dest);
405 init_sockaddr_in(&rt.rt_gateway, gway);
406 init_sockaddr_in(&rt.rt_genmask, mask);
407 addr.s_addr = dest;
408 if (ioctl(ifc_ctl_sock, SIOCDELRT, &rt) < 0) {
409 LOGD("failed to remove route for %s to %s: %s",
410 ifname, inet_ntoa(addr), strerror(errno));
411 }
412 }
413 fclose(fp);
414 ifc_close();
415 return 0;
416}
417
418/*
419 * Return the address of the default gateway
420 *
421 * TODO: factor out common code from this and remove_host_routes()
422 * so that we only scan /proc/net/route in one place.
423 */
424int ifc_get_default_route(const char *ifname)
425{
426 char name[64];
427 in_addr_t dest, gway, mask;
428 int flags, refcnt, use, metric, mtu, win, irtt;
429 int result;
430 FILE *fp;
431
432 fp = fopen("/proc/net/route", "r");
433 if (fp == NULL)
434 return 0;
435 /* Skip the header line */
436 if (fscanf(fp, "%*[^\n]\n") < 0) {
437 fclose(fp);
438 return 0;
439 }
440 ifc_init();
441 result = 0;
442 for (;;) {
443 int nread = fscanf(fp, "%63s%X%X%X%d%d%d%X%d%d%d\n",
444 name, &dest, &gway, &flags, &refcnt, &use, &metric, &mask,
445 &mtu, &win, &irtt);
446 if (nread != 11) {
447 break;
448 }
449 if ((flags & (RTF_UP|RTF_GATEWAY)) == (RTF_UP|RTF_GATEWAY)
450 && dest == 0
451 && strcmp(ifname, name) == 0) {
452 result = gway;
453 break;
454 }
455 }
456 fclose(fp);
457 ifc_close();
458 return result;
459}
460
461/*
462 * Sets the specified gateway as the default route for the named interface.
463 */
464int ifc_set_default_route(const char *ifname, in_addr_t gateway)
465{
466 struct in_addr addr;
467 int result;
468
469 ifc_init();
470 addr.s_addr = gateway;
471 if ((result = ifc_create_default_route(ifname, gateway)) < 0) {
472 LOGD("failed to add %s as default route for %s: %s",
473 inet_ntoa(addr), ifname, strerror(errno));
474 }
475 ifc_close();
476 return result;
477}
478
479/*
480 * Removes the default route for the named interface.
481 */
482int ifc_remove_default_route(const char *ifname)
483{
484 struct rtentry rt;
485 int result;
486
487 ifc_init();
488 memset(&rt, 0, sizeof(rt));
489 rt.rt_dev = (void *)ifname;
490 rt.rt_flags = RTF_UP|RTF_GATEWAY;
491 init_sockaddr_in(&rt.rt_dst, 0);
492 if ((result = ioctl(ifc_ctl_sock, SIOCDELRT, &rt)) < 0) {
493 LOGD("failed to remove default route for %s: %s", ifname, strerror(errno));
494 }
495 ifc_close();
496 return result;
497}
498
499int
500ifc_configure(const char *ifname,
501 in_addr_t address,
502 in_addr_t netmask,
503 in_addr_t gateway,
504 in_addr_t dns1,
505 in_addr_t dns2) {
506
507 char dns_prop_name[PROPERTY_KEY_MAX];
508
509 ifc_init();
510
511 if (ifc_up(ifname)) {
512 printerr("failed to turn on interface %s: %s\n", ifname, strerror(errno));
513 ifc_close();
514 return -1;
515 }
516 if (ifc_set_addr(ifname, address)) {
517 printerr("failed to set ipaddr %s: %s\n", ipaddr_to_string(address), strerror(errno));
518 ifc_close();
519 return -1;
520 }
521 if (ifc_set_mask(ifname, netmask)) {
522 printerr("failed to set netmask %s: %s\n", ipaddr_to_string(netmask), strerror(errno));
523 ifc_close();
524 return -1;
525 }
526 if (ifc_create_default_route(ifname, gateway)) {
527 printerr("failed to set default route %s: %s\n", ipaddr_to_string(gateway), strerror(errno));
528 ifc_close();
529 return -1;
530 }
531
532 ifc_close();
533
Szymon Jakubczakc88e09c2010-06-09 16:11:09 -0400534 snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns1", ifname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800535 property_set(dns_prop_name, dns1 ? ipaddr_to_string(dns1) : "");
Szymon Jakubczakc88e09c2010-06-09 16:11:09 -0400536 snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns2", ifname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800537 property_set(dns_prop_name, dns2 ? ipaddr_to_string(dns2) : "");
538
539 return 0;
540}
Banavathu, Srinivas Naik8984bb92010-08-11 01:23:54 +0530541
542int ifc_add_ipv6_route(const char *ifname, struct in6_addr dst, int prefix_length,
543 struct in6_addr gw)
544{
545 struct in6_rtmsg rtmsg;
546 int result;
547 int ifindex;
548
549 memset(&rtmsg, 0, sizeof(rtmsg));
550
551 ifindex = if_nametoindex(ifname);
552 if (ifindex == 0) {
553 printerr("if_nametoindex() failed: interface %s\n", ifname);
554 return -ENXIO;
555 }
556
557 rtmsg.rtmsg_ifindex = ifindex;
558 rtmsg.rtmsg_dst = dst;
559 rtmsg.rtmsg_dst_len = prefix_length;
560 rtmsg.rtmsg_flags = RTF_UP;
561
562 if (prefix_length == 128) {
563 rtmsg.rtmsg_flags |= RTF_HOST;
564 }
565
566 if (memcmp(&gw, &in6addr_any, sizeof(in6addr_any))) {
567 rtmsg.rtmsg_flags |= RTF_GATEWAY;
568 rtmsg.rtmsg_gateway = gw;
569 }
570
571 ifc_init6();
572
573 if (ifc_ctl_sock6 < 0) {
574 return -errno;
575 }
576
577 result = ioctl(ifc_ctl_sock6, SIOCADDRT, &rtmsg);
578 if (result < 0) {
579 if (errno == EEXIST) {
580 result = 0;
581 } else {
582 result = -errno;
583 }
584 }
585 ifc_close6();
586 return result;
587}
588
589int ifc_add_route(const char *ifname, const char *dst, int prefix_length,
590 const char *gw)
591{
592 int ret = 0;
593 struct sockaddr_in ipv4_dst, ipv4_gw;
594 struct sockaddr_in6 ipv6_dst, ipv6_gw;
595 struct addrinfo hints, *addr_ai, *gw_ai;
596
597 memset(&hints, 0, sizeof(hints));
598 hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
599 hints.ai_flags = AI_NUMERICHOST;
600
601 ret = getaddrinfo(dst, NULL, &hints, &addr_ai);
602
603 if (ret != 0) {
604 printerr("getaddrinfo failed: invalid address %s\n", dst);
605 return -EINVAL;
606 }
607
608 if (gw == NULL) {
609 if (addr_ai->ai_family == AF_INET6) {
610 gw = "::";
611 } else if (addr_ai->ai_family == AF_INET) {
612 gw = "0.0.0.0";
613 }
614 }
615
616 ret = getaddrinfo(gw, NULL, &hints, &gw_ai);
617 if (ret != 0) {
618 printerr("getaddrinfo failed: invalid gateway %s\n", gw);
619 freeaddrinfo(addr_ai);
620 return -EINVAL;
621 }
622
623 if (addr_ai->ai_family != gw_ai->ai_family) {
624 printerr("ifc_add_route: different address families: %s and %s\n", dst, gw);
625 freeaddrinfo(addr_ai);
626 freeaddrinfo(gw_ai);
627 return -EINVAL;
628 }
629
630 if (addr_ai->ai_family == AF_INET6) {
631 memcpy(&ipv6_dst, addr_ai->ai_addr, sizeof(struct sockaddr_in6));
632 memcpy(&ipv6_gw, gw_ai->ai_addr, sizeof(struct sockaddr_in6));
633 ret = ifc_add_ipv6_route(ifname, ipv6_dst.sin6_addr, prefix_length,
634 ipv6_gw.sin6_addr);
635 } else if (addr_ai->ai_family == AF_INET) {
636 memcpy(&ipv4_dst, addr_ai->ai_addr, sizeof(struct sockaddr_in));
637 memcpy(&ipv4_gw, gw_ai->ai_addr, sizeof(struct sockaddr_in));
638 ret = ifc_add_ipv4_route(ifname, ipv4_dst.sin_addr, prefix_length,
639 ipv4_gw.sin_addr);
640 } else {
641 printerr("ifc_add_route: getaddrinfo returned un supported address family %d\n",
642 addr_ai->ai_family);
643 ret = -EAFNOSUPPORT;
644 }
645
646 freeaddrinfo(addr_ai);
647 freeaddrinfo(gw_ai);
648 return ret;
649}