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 if (type == ARPHRD_ETHER) {
307 char bridge_path[PATH_MAX], wimax_path[PATH_MAX];
312 snprintf(bridge_path, PATH_MAX,
313 "/sys/class/net/%s/bridge", devname);
314 snprintf(wimax_path, PATH_MAX,
315 "/sys/class/net/%s/wimax", devname);
317 memset(&iwr, 0, sizeof(iwr));
318 strncpy(iwr.ifr_ifrn.ifrn_name, devname, IFNAMSIZ);
320 sk = socket(PF_INET, SOCK_DGRAM, 0);
322 if (g_str_has_prefix(devname, "vmnet") == TRUE ||
323 g_str_has_prefix(devname, "vboxnet") == TRUE) {
324 connman_info("Ignoring network interface %s", devname);
325 devtype = CONNMAN_DEVICE_TYPE_UNKNOWN;
326 } else if (g_str_has_prefix(devname, "bnep") == TRUE)
327 devtype = CONNMAN_DEVICE_TYPE_UNKNOWN;
328 else if (g_str_has_prefix(devname, "wmx") == TRUE)
329 devtype = CONNMAN_DEVICE_TYPE_UNKNOWN;
330 else if (stat(wimax_path, &st) == 0 && (st.st_mode & S_IFDIR))
331 devtype = CONNMAN_DEVICE_TYPE_UNKNOWN;
332 else if (stat(bridge_path, &st) == 0 && (st.st_mode & S_IFDIR))
333 devtype = CONNMAN_DEVICE_TYPE_UNKNOWN;
334 else if (ioctl(sk, SIOCGIWNAME, &iwr) == 0)
335 devtype = CONNMAN_DEVICE_TYPE_WIFI;
337 devtype = CONNMAN_DEVICE_TYPE_ETHERNET;
340 } else if (type == ARPHRD_NONE) {
341 if (g_str_has_prefix(devname, "hso") == TRUE)
342 devtype = CONNMAN_DEVICE_TYPE_HSO;
346 case CONNMAN_DEVICE_TYPE_UNKNOWN:
349 case CONNMAN_DEVICE_TYPE_ETHERNET:
350 case CONNMAN_DEVICE_TYPE_WIFI:
351 case CONNMAN_DEVICE_TYPE_WIMAX:
352 name = index2ident(index, "");
353 addr = index2addr(index);
355 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
356 case CONNMAN_DEVICE_TYPE_GPS:
357 case CONNMAN_DEVICE_TYPE_HSO:
358 case CONNMAN_DEVICE_TYPE_NOZOMI:
359 case CONNMAN_DEVICE_TYPE_HUAWEI:
360 case CONNMAN_DEVICE_TYPE_NOVATEL:
361 case CONNMAN_DEVICE_TYPE_VENDOR:
362 name = strdup(devname);
367 device = connman_device_create(name, devtype);
368 if (device == NULL) {
376 case CONNMAN_DEVICE_TYPE_UNKNOWN:
377 case CONNMAN_DEVICE_TYPE_VENDOR:
378 case CONNMAN_DEVICE_TYPE_NOZOMI:
379 case CONNMAN_DEVICE_TYPE_HUAWEI:
380 case CONNMAN_DEVICE_TYPE_NOVATEL:
381 case CONNMAN_DEVICE_TYPE_GPS:
382 mode = CONNMAN_DEVICE_MODE_UNKNOWN;
384 case CONNMAN_DEVICE_TYPE_ETHERNET:
385 mode = CONNMAN_DEVICE_MODE_TRANSPORT_IP;
386 ident = index2ident(index, NULL);
388 case CONNMAN_DEVICE_TYPE_WIFI:
389 case CONNMAN_DEVICE_TYPE_WIMAX:
390 mode = CONNMAN_DEVICE_MODE_NETWORK_SINGLE;
391 ident = index2ident(index, NULL);
393 case CONNMAN_DEVICE_TYPE_BLUETOOTH:
394 mode = CONNMAN_DEVICE_MODE_NETWORK_MULTIPLE;
396 case CONNMAN_DEVICE_TYPE_HSO:
397 mode = CONNMAN_DEVICE_MODE_NETWORK_SINGLE;
398 connman_device_set_policy(device, CONNMAN_DEVICE_POLICY_MANUAL);
402 connman_device_set_mode(device, mode);
404 connman_device_set_index(device, index);
405 connman_device_set_interface(device, devname);
408 connman_device_set_ident(device, ident);
412 connman_device_set_string(device, "Address", addr);
421 int connman_inet_set_address(int index, struct in_addr address,
422 struct in_addr netmask, struct in_addr broadcast)
425 struct sockaddr_in *addr;
428 sk = socket(PF_INET, SOCK_DGRAM, 0);
432 memset(&ifr, 0, sizeof(ifr));
433 ifr.ifr_ifindex = index;
435 if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
440 DBG("ifname %s", ifr.ifr_name);
442 addr = (struct sockaddr_in *) &ifr.ifr_addr;
443 addr->sin_family = AF_INET;
444 addr->sin_addr = address;
446 err = ioctl(sk, SIOCSIFADDR, &ifr);
449 DBG("address setting failed (%s)", strerror(errno));
451 addr = (struct sockaddr_in *) &ifr.ifr_netmask;
452 addr->sin_family = AF_INET;
453 addr->sin_addr = netmask;
455 err = ioctl(sk, SIOCSIFNETMASK, &ifr);
458 DBG("netmask setting failed (%s)", strerror(errno));
460 addr = (struct sockaddr_in *) &ifr.ifr_broadaddr;
461 addr->sin_family = AF_INET;
462 addr->sin_addr = broadcast;
464 err = ioctl(sk, SIOCSIFBRDADDR, &ifr);
467 DBG("broadcast setting failed (%s)", strerror(errno));
474 int connman_inet_clear_address(int index)
477 struct sockaddr_in *addr;
480 sk = socket(PF_INET, SOCK_DGRAM, 0);
484 memset(&ifr, 0, sizeof(ifr));
485 ifr.ifr_ifindex = index;
487 if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
492 DBG("ifname %s", ifr.ifr_name);
494 addr = (struct sockaddr_in *) &ifr.ifr_addr;
495 addr->sin_family = AF_INET;
496 addr->sin_addr.s_addr = INADDR_ANY;
498 //err = ioctl(sk, SIOCDIFADDR, &ifr);
499 err = ioctl(sk, SIOCSIFADDR, &ifr);
503 if (err < 0 && errno != EADDRNOTAVAIL) {
504 DBG("address removal failed (%s)", strerror(errno));
511 int connman_inet_set_gateway(int index, struct in_addr gateway)
515 struct sockaddr_in *addr;
518 sk = socket(PF_INET, SOCK_DGRAM, 0);
522 memset(&ifr, 0, sizeof(ifr));
523 ifr.ifr_ifindex = index;
525 if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
530 DBG("ifname %s", ifr.ifr_name);
532 memset(&rt, 0, sizeof(rt));
533 rt.rt_flags = RTF_UP | RTF_HOST;
535 addr = (struct sockaddr_in *) &rt.rt_dst;
536 addr->sin_family = AF_INET;
537 addr->sin_addr = gateway;
539 addr = (struct sockaddr_in *) &rt.rt_gateway;
540 addr->sin_family = AF_INET;
541 addr->sin_addr.s_addr = INADDR_ANY;
543 addr = (struct sockaddr_in *) &rt.rt_genmask;
544 addr->sin_family = AF_INET;
545 addr->sin_addr.s_addr = INADDR_ANY;
547 rt.rt_dev = ifr.ifr_name;
549 err = ioctl(sk, SIOCADDRT, &rt);
551 connman_error("Setting host gateway route failed (%s)",
554 memset(&rt, 0, sizeof(rt));
555 rt.rt_flags = RTF_UP | RTF_GATEWAY;
557 addr = (struct sockaddr_in *) &rt.rt_dst;
558 addr->sin_family = AF_INET;
559 addr->sin_addr.s_addr = INADDR_ANY;
561 addr = (struct sockaddr_in *) &rt.rt_gateway;
562 addr->sin_family = AF_INET;
563 addr->sin_addr = gateway;
565 addr = (struct sockaddr_in *) &rt.rt_genmask;
566 addr->sin_family = AF_INET;
567 addr->sin_addr.s_addr = INADDR_ANY;
569 err = ioctl(sk, SIOCADDRT, &rt);
571 connman_error("Setting default route failed (%s)",