5 * Copyright (C) 2007-2009 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32 #include <sys/ioctl.h>
33 #include <sys/socket.h>
34 #include <arpa/inet.h>
35 #include <net/route.h>
36 #include <net/ethernet.h>
37 #include <linux/if_arp.h>
38 #include <linux/wireless.h>
42 int connman_inet_ifindex(const char *name)
50 sk = socket(PF_INET, SOCK_DGRAM, 0);
54 memset(&ifr, 0, sizeof(ifr));
55 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
57 err = ioctl(sk, SIOCGIFINDEX, &ifr);
64 return ifr.ifr_ifindex;
67 char *connman_inet_ifname(int index)
75 sk = socket(PF_INET, SOCK_DGRAM, 0);
79 memset(&ifr, 0, sizeof(ifr));
80 ifr.ifr_ifindex = index;
82 err = ioctl(sk, SIOCGIFNAME, &ifr);
89 return strdup(ifr.ifr_name);
92 int connman_inet_ifup(int index)
97 sk = socket(PF_INET, SOCK_DGRAM, 0);
101 memset(&ifr, 0, sizeof(ifr));
102 ifr.ifr_ifindex = index;
104 if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
109 if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
114 if (ifr.ifr_flags & IFF_UP) {
119 ifr.ifr_flags |= IFF_UP;
121 if (ioctl(sk, SIOCSIFFLAGS, &ifr) < 0) {
134 int connman_inet_ifdown(int index)
139 sk = socket(PF_INET, SOCK_DGRAM, 0);
143 memset(&ifr, 0, sizeof(ifr));
144 ifr.ifr_ifindex = index;
146 if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
151 if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
156 if (!(ifr.ifr_flags & IFF_UP)) {
161 ifr.ifr_flags &= ~IFF_UP;
163 if (ioctl(sk, SIOCSIFFLAGS, &ifr) < 0)
174 static unsigned short index2type(int index)
182 sk = socket(PF_INET, SOCK_DGRAM, 0);
186 memset(&ifr, 0, sizeof(ifr));
187 ifr.ifr_ifindex = index;
189 err = ioctl(sk, SIOCGIFNAME, &ifr);
192 err = ioctl(sk, SIOCGIFHWADDR, &ifr);
199 return ifr.ifr_hwaddr.sa_family;
202 static char *index2addr(int index)
205 struct ether_addr *eth;
212 sk = socket(PF_INET, SOCK_DGRAM, 0);
216 memset(&ifr, 0, sizeof(ifr));
217 ifr.ifr_ifindex = index;
219 err = ioctl(sk, SIOCGIFNAME, &ifr);
222 err = ioctl(sk, SIOCGIFHWADDR, &ifr);
233 eth = (void *) &ifr.ifr_hwaddr.sa_data;
234 snprintf(str, 18, "%02X:%02X:%02X:%02X:%02X:%02X",
235 eth->ether_addr_octet[0],
236 eth->ether_addr_octet[1],
237 eth->ether_addr_octet[2],
238 eth->ether_addr_octet[3],
239 eth->ether_addr_octet[4],
240 eth->ether_addr_octet[5]);
245 static char *index2ident(int index, const char *prefix)
248 struct ether_addr *eth;
255 sk = socket(PF_INET, SOCK_DGRAM, 0);
259 memset(&ifr, 0, sizeof(ifr));
260 ifr.ifr_ifindex = index;
262 err = ioctl(sk, SIOCGIFNAME, &ifr);
265 err = ioctl(sk, SIOCGIFHWADDR, &ifr);
272 len = prefix ? strlen(prefix) + 18 : 18;
278 eth = (void *) &ifr.ifr_hwaddr.sa_data;
279 snprintf(str, len, "%s%02x%02x%02x%02x%02x%02x",
280 prefix ? prefix : "",
281 eth->ether_addr_octet[0],
282 eth->ether_addr_octet[1],
283 eth->ether_addr_octet[2],
284 eth->ether_addr_octet[3],
285 eth->ether_addr_octet[4],
286 eth->ether_addr_octet[5]);
291 struct connman_device *connman_inet_create_device(int index)
293 enum connman_device_type devtype = CONNMAN_DEVICE_TYPE_UNKNOWN;
294 enum connman_device_mode mode = CONNMAN_DEVICE_MODE_UNKNOWN;
295 struct connman_device *device;
296 unsigned short type = index2type(index);
297 char *addr, *name, *devname, *ident = NULL;
302 devname = connman_inet_ifname(index);
306 __connman_udev_get_devtype(devname);
308 if (type == ARPHRD_ETHER) {
309 char bridge_path[PATH_MAX], wimax_path[PATH_MAX];
314 snprintf(bridge_path, PATH_MAX,
315 "/sys/class/net/%s/bridge", devname);
316 snprintf(wimax_path, PATH_MAX,
317 "/sys/class/net/%s/wimax", devname);
319 memset(&iwr, 0, sizeof(iwr));
320 strncpy(iwr.ifr_ifrn.ifrn_name, devname, IFNAMSIZ);
322 sk = socket(PF_INET, SOCK_DGRAM, 0);
324 if (g_str_has_prefix(devname, "vmnet") == TRUE ||
325 g_str_has_prefix(devname, "vboxnet") == TRUE) {
326 connman_info("Ignoring network interface %s", devname);
327 devtype = CONNMAN_DEVICE_TYPE_UNKNOWN;
328 } else if (g_str_has_prefix(devname, "bnep") == TRUE)
329 devtype = CONNMAN_DEVICE_TYPE_UNKNOWN;
330 else if (g_str_has_prefix(devname, "wmx") == TRUE)
331 devtype = CONNMAN_DEVICE_TYPE_UNKNOWN;
332 else if (stat(wimax_path, &st) == 0 && (st.st_mode & S_IFDIR))
333 devtype = CONNMAN_DEVICE_TYPE_UNKNOWN;
334 else if (stat(bridge_path, &st) == 0 && (st.st_mode & S_IFDIR))
335 devtype = CONNMAN_DEVICE_TYPE_UNKNOWN;
336 else if (ioctl(sk, SIOCGIWNAME, &iwr) == 0)
337 devtype = CONNMAN_DEVICE_TYPE_WIFI;
339 devtype = CONNMAN_DEVICE_TYPE_ETHERNET;
342 } else if (type == ARPHRD_NONE) {
343 if (g_str_has_prefix(devname, "hso") == TRUE)
344 devtype = CONNMAN_DEVICE_TYPE_HSO;
348 case CONNMAN_DEVICE_TYPE_UNKNOWN:
351 case CONNMAN_DEVICE_TYPE_ETHERNET:
352 case CONNMAN_DEVICE_TYPE_WIFI:
353 case CONNMAN_DEVICE_TYPE_WIMAX:
354 name = index2ident(index, "");
355 addr = index2addr(index);
357 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
358 case CONNMAN_DEVICE_TYPE_GPS:
359 case CONNMAN_DEVICE_TYPE_HSO:
360 case CONNMAN_DEVICE_TYPE_NOZOMI:
361 case CONNMAN_DEVICE_TYPE_HUAWEI:
362 case CONNMAN_DEVICE_TYPE_NOVATEL:
363 case CONNMAN_DEVICE_TYPE_VENDOR:
364 name = strdup(devname);
369 device = connman_device_create(name, devtype);
370 if (device == NULL) {
378 case CONNMAN_DEVICE_TYPE_UNKNOWN:
379 case CONNMAN_DEVICE_TYPE_VENDOR:
380 case CONNMAN_DEVICE_TYPE_NOZOMI:
381 case CONNMAN_DEVICE_TYPE_HUAWEI:
382 case CONNMAN_DEVICE_TYPE_NOVATEL:
383 case CONNMAN_DEVICE_TYPE_GPS:
384 mode = CONNMAN_DEVICE_MODE_UNKNOWN;
386 case CONNMAN_DEVICE_TYPE_ETHERNET:
387 mode = CONNMAN_DEVICE_MODE_TRANSPORT_IP;
388 ident = index2ident(index, NULL);
390 case CONNMAN_DEVICE_TYPE_WIFI:
391 case CONNMAN_DEVICE_TYPE_WIMAX:
392 mode = CONNMAN_DEVICE_MODE_NETWORK_SINGLE;
393 ident = index2ident(index, NULL);
395 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
396 mode = CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE;
398 case CONNMAN_DEVICE_TYPE_HSO:
399 mode = CONNMAN_DEVICE_MODE_NETWORK_SINGLE;
403 connman_device_set_mode(device, mode);
405 connman_device_set_index(device, index);
406 connman_device_set_interface(device, devname);
409 connman_device_set_ident(device, ident);
413 connman_device_set_string(device, "Address", addr);
422 int connman_inet_set_address(int index, struct in_addr address,
423 struct in_addr netmask, struct in_addr broadcast)
426 struct sockaddr_in *addr;
429 sk = socket(PF_INET, SOCK_DGRAM, 0);
433 memset(&ifr, 0, sizeof(ifr));
434 ifr.ifr_ifindex = index;
436 if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
441 DBG("ifname %s", ifr.ifr_name);
443 addr = (struct sockaddr_in *) &ifr.ifr_addr;
444 addr->sin_family = AF_INET;
445 addr->sin_addr = address;
447 err = ioctl(sk, SIOCSIFADDR, &ifr);
450 DBG("address setting failed (%s)", strerror(errno));
452 addr = (struct sockaddr_in *) &ifr.ifr_netmask;
453 addr->sin_family = AF_INET;
454 addr->sin_addr = netmask;
456 err = ioctl(sk, SIOCSIFNETMASK, &ifr);
459 DBG("netmask setting failed (%s)", strerror(errno));
461 addr = (struct sockaddr_in *) &ifr.ifr_broadaddr;
462 addr->sin_family = AF_INET;
463 addr->sin_addr = broadcast;
465 err = ioctl(sk, SIOCSIFBRDADDR, &ifr);
468 DBG("broadcast setting failed (%s)", strerror(errno));
475 int connman_inet_clear_address(int index)
478 struct sockaddr_in *addr;
481 sk = socket(PF_INET, SOCK_DGRAM, 0);
485 memset(&ifr, 0, sizeof(ifr));
486 ifr.ifr_ifindex = index;
488 if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
493 DBG("ifname %s", ifr.ifr_name);
495 addr = (struct sockaddr_in *) &ifr.ifr_addr;
496 addr->sin_family = AF_INET;
497 addr->sin_addr.s_addr = INADDR_ANY;
499 //err = ioctl(sk, SIOCDIFADDR, &ifr);
500 err = ioctl(sk, SIOCSIFADDR, &ifr);
504 if (err < 0 && errno != EADDRNOTAVAIL) {
505 DBG("address removal failed (%s)", strerror(errno));
512 int connman_inet_set_gateway(int index, struct in_addr gateway)
516 struct sockaddr_in *addr;
519 sk = socket(PF_INET, SOCK_DGRAM, 0);
523 memset(&ifr, 0, sizeof(ifr));
524 ifr.ifr_ifindex = index;
526 if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
531 DBG("ifname %s", ifr.ifr_name);
533 memset(&rt, 0, sizeof(rt));
534 rt.rt_flags = RTF_UP | RTF_HOST;
536 addr = (struct sockaddr_in *) &rt.rt_dst;
537 addr->sin_family = AF_INET;
538 addr->sin_addr = gateway;
540 addr = (struct sockaddr_in *) &rt.rt_gateway;
541 addr->sin_family = AF_INET;
542 addr->sin_addr.s_addr = INADDR_ANY;
544 addr = (struct sockaddr_in *) &rt.rt_genmask;
545 addr->sin_family = AF_INET;
546 addr->sin_addr.s_addr = INADDR_ANY;
548 rt.rt_dev = ifr.ifr_name;
550 err = ioctl(sk, SIOCADDRT, &rt);
552 connman_error("Setting host gateway route failed (%s)",
555 memset(&rt, 0, sizeof(rt));
556 rt.rt_flags = RTF_UP | RTF_GATEWAY;
558 addr = (struct sockaddr_in *) &rt.rt_dst;
559 addr->sin_family = AF_INET;
560 addr->sin_addr.s_addr = INADDR_ANY;
562 addr = (struct sockaddr_in *) &rt.rt_gateway;
563 addr->sin_family = AF_INET;
564 addr->sin_addr = gateway;
566 addr = (struct sockaddr_in *) &rt.rt_genmask;
567 addr->sin_family = AF_INET;
568 addr->sin_addr.s_addr = INADDR_ANY;
570 err = ioctl(sk, SIOCADDRT, &rt);
572 connman_error("Setting default route failed (%s)",