ac6b1250862f7ac0dfbe2fdc5e47ee17acca7a91
[platform/upstream/connman.git] / gdhcp / common.c
1 /*
2  *  DHCP library with GLib integration
3  *
4  *  Copyright (C) 2007-2013  Intel Corporation. All rights reserved.
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License version 2 as
8  *  published by the Free Software Foundation.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  *
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <errno.h>
28 #include <unistd.h>
29 #include <sys/ioctl.h>
30 #include <stdint.h>
31 #include <string.h>
32 #include <endian.h>
33 #include <net/if_arp.h>
34 #include <linux/if.h>
35 #include <netpacket/packet.h>
36 #include <net/ethernet.h>
37 #include <arpa/inet.h>
38
39 #include "gdhcp.h"
40 #include "common.h"
41
42 static const DHCPOption client_options[] = {
43         { OPTION_IP,                    0x01 }, /* subnet-mask */
44         { OPTION_IP | OPTION_LIST,      0x03 }, /* routers */
45         { OPTION_IP | OPTION_LIST,      0x06 }, /* domain-name-servers */
46         { OPTION_STRING,                0x0c }, /* hostname */
47         { OPTION_STRING,                0x0f }, /* domain-name */
48         { OPTION_IP | OPTION_LIST,      0x2a }, /* ntp-servers */
49         { OPTION_U32,                   0x33 }, /* dhcp-lease-time */
50         /* Options below will not be exposed to user */
51         { OPTION_IP,                    0x32 }, /* requested-ip */
52         { OPTION_U8,                    0x35 }, /* message-type */
53         { OPTION_U32,                   0x36 }, /* server-id */
54         { OPTION_U16,                   0x39 }, /* max-size */
55         { OPTION_STRING,                0x3c }, /* vendor */
56         { OPTION_STRING,                0x3d }, /* client-id */
57         { OPTION_STRING,                0xfc }, /* UNOFFICIAL proxy-pac */
58         { OPTION_UNKNOWN,               0x00 },
59 };
60
61 GDHCPOptionType dhcp_get_code_type(uint8_t code)
62 {
63         int i;
64
65         for (i = 0; client_options[i].code; i++) {
66                 if (client_options[i].code == code)
67                         return client_options[i].type;
68         }
69
70         return OPTION_UNKNOWN;
71 }
72
73 uint8_t *dhcp_get_option(struct dhcp_packet *packet, int code)
74 {
75         int len, rem;
76         uint8_t *optionptr;
77         uint8_t overload = 0;
78
79         /* option bytes: [code][len][data1][data2]..[dataLEN] */
80         optionptr = packet->options;
81         rem = sizeof(packet->options);
82
83         while (1) {
84                 if (rem <= 0)
85                         /* Bad packet, malformed option field */
86                         return NULL;
87
88                 if (optionptr[OPT_CODE] == DHCP_PADDING) {
89                         rem--;
90                         optionptr++;
91
92                         continue;
93                 }
94
95                 if (optionptr[OPT_CODE] == DHCP_END) {
96                         if (overload & FILE_FIELD) {
97                                 overload &= ~FILE_FIELD;
98
99                                 optionptr = packet->file;
100                                 rem = sizeof(packet->file);
101
102                                 continue;
103                         } else if (overload & SNAME_FIELD) {
104                                 overload &= ~SNAME_FIELD;
105
106                                 optionptr = packet->sname;
107                                 rem = sizeof(packet->sname);
108
109                                 continue;
110                         }
111
112                         break;
113                 }
114
115                 len = 2 + optionptr[OPT_LEN];
116
117                 rem -= len;
118                 if (rem < 0)
119                         continue; /* complain and return NULL */
120
121                 if (optionptr[OPT_CODE] == code)
122                         return optionptr + OPT_DATA;
123
124                 if (optionptr[OPT_CODE] == DHCP_OPTION_OVERLOAD)
125                         overload |= optionptr[OPT_DATA];
126
127                 optionptr += len;
128         }
129
130         return NULL;
131 }
132
133 int dhcp_end_option(uint8_t *optionptr)
134 {
135         int i = 0;
136
137         while (optionptr[i] != DHCP_END) {
138                 if (optionptr[i] != DHCP_PADDING)
139                         i += optionptr[i + OPT_LEN] + OPT_DATA - 1;
140
141                 i++;
142         }
143
144         return i;
145 }
146
147 /* get a rough idea of how long an option will be */
148 static const uint8_t len_of_option_as_string[] = {
149         [OPTION_IP] = sizeof("255.255.255.255 "),
150         [OPTION_STRING] = 1,
151         [OPTION_U8] = sizeof("255 "),
152         [OPTION_U16] = sizeof("65535 "),
153         [OPTION_U32] = sizeof("4294967295 "),
154 };
155
156 static int sprint_nip(char *dest, const char *pre, const uint8_t *ip)
157 {
158         return sprintf(dest, "%s%u.%u.%u.%u", pre, ip[0], ip[1], ip[2], ip[3]);
159 }
160
161 /* Create "opt_value1 option_value2 ..." string */
162 char *malloc_option_value_string(uint8_t *option, GDHCPOptionType type)
163 {
164         unsigned upper_length;
165         int len, optlen;
166         char *dest, *ret;
167
168         len = option[OPT_LEN - OPT_DATA];
169         type &= OPTION_TYPE_MASK;
170         optlen = dhcp_option_lengths[type];
171         if (optlen == 0)
172                 return NULL;
173         upper_length = len_of_option_as_string[type] *
174                         ((unsigned)len / (unsigned)optlen);
175         dest = ret = g_malloc(upper_length + 1);
176         if (ret == NULL)
177                 return NULL;
178
179         while (len >= optlen) {
180                 switch (type) {
181                 case OPTION_IP:
182                         dest += sprint_nip(dest, "", option);
183                         break;
184                 case OPTION_U16: {
185                         uint16_t val_u16 = get_be16(option);
186                         dest += sprintf(dest, "%u", val_u16);
187                         break;
188                 }
189                 case OPTION_U32: {
190                         uint32_t val_u32 = get_be32(option);
191                         dest += sprintf(dest, "%u", val_u32);
192                         break;
193                 }
194                 case OPTION_STRING:
195                         memcpy(dest, option, len);
196                         dest[len] = '\0';
197                         return ret;
198                 default:
199                         break;
200                 }
201                 option += optlen;
202                 len -= optlen;
203                 if (len <= 0)
204                         break;
205                 *dest++ = ' ';
206                 *dest = '\0';
207         }
208
209         return ret;
210 }
211
212 uint8_t *dhcpv6_get_option(struct dhcpv6_packet *packet, uint16_t pkt_len,
213                         int code, uint16_t *option_len, int *option_count)
214 {
215         int rem, count = 0;
216         uint8_t *optionptr, *found = NULL;
217         uint16_t opt_code, opt_len, len;
218
219         optionptr = packet->options;
220         rem = pkt_len - 1 - 3;
221
222         if (rem <= 0)
223                 goto bad_packet;
224
225         while (1) {
226                 opt_code = optionptr[0] << 8 | optionptr[1];
227                 opt_len = len = optionptr[2] << 8 | optionptr[3];
228                 len += 2 + 2; /* skip code and len */
229
230                 if (len < 4)
231                         goto bad_packet;
232
233                 rem -= len;
234                 if (rem < 0)
235                         break;
236
237                 if (opt_code == code) {
238                         if (option_len)
239                                 *option_len = opt_len;
240                         found = optionptr + 2 + 2;
241                         count++;
242                 }
243
244                 if (rem == 0)
245                         break;
246
247                 optionptr += len;
248         }
249
250         if (option_count)
251                 *option_count = count;
252
253         return found;
254
255 bad_packet:
256         if (option_len)
257                 *option_len = 0;
258         if (option_count)
259                 *option_count = 0;
260         return NULL;
261 }
262
263 uint8_t *dhcpv6_get_sub_option(unsigned char *option, uint16_t max_len,
264                         uint16_t *option_code, uint16_t *option_len)
265 {
266         int rem;
267         uint16_t code, len;
268
269         rem = max_len - 2 - 2;
270
271         if (rem <= 0)
272                 /* Bad option */
273                 return NULL;
274
275         code = option[0] << 8 | option[1];
276         len = option[2] << 8 | option[3];
277
278         rem -= len;
279         if (rem < 0)
280                 return NULL;
281
282         *option_code = code;
283         *option_len = len;
284
285         return &option[4];
286 }
287
288 /*
289  * Add an option (supplied in binary form) to the options.
290  * Option format: [code][len][data1][data2]..[dataLEN]
291  */
292 void dhcp_add_binary_option(struct dhcp_packet *packet, uint8_t *addopt)
293 {
294         unsigned len;
295         uint8_t *optionptr = packet->options;
296         unsigned end = dhcp_end_option(optionptr);
297
298         len = OPT_DATA + addopt[OPT_LEN];
299
300         /* end position + (option code/length + addopt length) + end option */
301         if (end + len + 1 >= DHCP_OPTIONS_BUFSIZE)
302                 /* option did not fit into the packet */
303                 return;
304
305         memcpy(optionptr + end, addopt, len);
306
307         optionptr[end + len] = DHCP_END;
308 }
309
310 /*
311  * Add an option (supplied in binary form) to the options.
312  * Option format: [code][len][data1][data2]..[dataLEN]
313  */
314 void dhcpv6_add_binary_option(struct dhcpv6_packet *packet, uint16_t max_len,
315                                 uint16_t *pkt_len, uint8_t *addopt)
316 {
317         unsigned len;
318         uint8_t *optionptr = packet->options;
319
320         len = 2 + 2 + (addopt[2] << 8 | addopt[3]);
321
322         /* end position + (option code/length + addopt length) */
323         if (*pkt_len + len >= max_len)
324                 /* option did not fit into the packet */
325                 return;
326
327         memcpy(optionptr + *pkt_len, addopt, len);
328         *pkt_len += len;
329 }
330
331 static GDHCPOptionType check_option(uint8_t code, uint8_t data_len)
332 {
333         GDHCPOptionType type = dhcp_get_code_type(code);
334         uint8_t len;
335
336         if (type == OPTION_UNKNOWN)
337                 return type;
338
339         len = dhcp_option_lengths[type & OPTION_TYPE_MASK];
340         if (len != data_len) {
341                 printf("Invalid option len %d (expecting %d) for code 0x%x\n",
342                         data_len, len, code);
343                 return OPTION_UNKNOWN;
344         }
345
346         return type;
347 }
348
349 void dhcp_add_option_uint32(struct dhcp_packet *packet, uint8_t code,
350                                                         uint32_t data)
351 {
352         uint8_t option[6];
353
354         if (check_option(code, sizeof(data)) == OPTION_UNKNOWN)
355                 return;
356
357         option[OPT_CODE] = code;
358         option[OPT_LEN] = sizeof(data);
359         put_be32(data, option + OPT_DATA);
360
361         dhcp_add_binary_option(packet, option);
362
363         return;
364 }
365
366 void dhcp_add_option_uint16(struct dhcp_packet *packet, uint8_t code,
367                                                         uint16_t data)
368 {
369         uint8_t option[6];
370
371         if (check_option(code, sizeof(data)) == OPTION_UNKNOWN)
372                 return;
373
374         option[OPT_CODE] = code;
375         option[OPT_LEN] = sizeof(data);
376         put_be16(data, option + OPT_DATA);
377
378         dhcp_add_binary_option(packet, option);
379
380         return;
381 }
382
383 void dhcp_add_option_uint8(struct dhcp_packet *packet, uint8_t code,
384                                                         uint8_t data)
385 {
386         uint8_t option[6];
387
388         if (check_option(code, sizeof(data)) == OPTION_UNKNOWN)
389                 return;
390
391         option[OPT_CODE] = code;
392         option[OPT_LEN] = sizeof(data);
393         option[OPT_DATA] = data;
394
395         dhcp_add_binary_option(packet, option);
396
397         return;
398 }
399
400 void dhcp_init_header(struct dhcp_packet *packet, char type)
401 {
402         memset(packet, 0, sizeof(*packet));
403
404         packet->op = BOOTREQUEST;
405
406         switch (type) {
407         case DHCPOFFER:
408         case DHCPACK:
409         case DHCPNAK:
410                 packet->op = BOOTREPLY;
411         }
412
413         packet->htype = 1;
414         packet->hlen = 6;
415         packet->cookie = htonl(DHCP_MAGIC);
416         packet->options[0] = DHCP_END;
417
418         dhcp_add_option_uint8(packet, DHCP_MESSAGE_TYPE, type);
419 }
420
421 void dhcpv6_init_header(struct dhcpv6_packet *packet, uint8_t type)
422 {
423         int id;
424
425         memset(packet, 0, sizeof(*packet));
426
427         packet->message = type;
428
429         id = random();
430
431         packet->transaction_id[0] = (id >> 16) & 0xff;
432         packet->transaction_id[1] = (id >> 8) & 0xff;
433         packet->transaction_id[2] = id & 0xff;
434 }
435
436 int dhcp_recv_l3_packet(struct dhcp_packet *packet, int fd)
437 {
438         int n;
439
440         memset(packet, 0, sizeof(*packet));
441
442         n = read(fd, packet, sizeof(*packet));
443         if (n < 0)
444                 return -errno;
445
446         if (packet->cookie != htonl(DHCP_MAGIC))
447                 return -EPROTO;
448
449         return n;
450 }
451
452 int dhcpv6_recv_l3_packet(struct dhcpv6_packet **packet, unsigned char *buf,
453                         int buf_len, int fd)
454 {
455         int n;
456
457         n = read(fd, buf, buf_len);
458         if (n < 0)
459                 return -errno;
460
461         *packet = (struct dhcpv6_packet *)buf;
462
463         return n;
464 }
465
466 /* TODO: Use glib checksum */
467 uint16_t dhcp_checksum(void *addr, int count)
468 {
469         /*
470          * Compute Internet Checksum for "count" bytes
471          * beginning at location "addr".
472          */
473         int32_t sum = 0;
474         uint16_t *source = (uint16_t *) addr;
475
476         while (count > 1)  {
477                 /*  This is the inner loop */
478                 sum += *source++;
479                 count -= 2;
480         }
481
482         /*  Add left-over byte, if any */
483         if (count > 0) {
484                 /* Make sure that the left-over byte is added correctly both
485                  * with little and big endian hosts */
486                 uint16_t tmp = 0;
487                 *(uint8_t *) &tmp = *(uint8_t *) source;
488                 sum += tmp;
489         }
490         /*  Fold 32-bit sum to 16 bits */
491         while (sum >> 16)
492                 sum = (sum & 0xffff) + (sum >> 16);
493
494         return ~sum;
495 }
496
497 #define IN6ADDR_ALL_DHCP_RELAY_AGENTS_AND_SERVERS_MC_INIT \
498         { { { 0xff,0x02,0,0,0,0,0,0,0,0,0,0,0,0x1,0,0x2 } } } /* ff02::1:2 */
499 static const struct in6_addr in6addr_all_dhcp_relay_agents_and_servers_mc =
500         IN6ADDR_ALL_DHCP_RELAY_AGENTS_AND_SERVERS_MC_INIT;
501
502 /* from netinet/in.h */
503 struct in6_pktinfo {
504         struct in6_addr ipi6_addr;  /* src/dst IPv6 address */
505         unsigned int ipi6_ifindex;  /* send/recv interface index */
506 };
507
508 int dhcpv6_send_packet(int index, struct dhcpv6_packet *dhcp_pkt, int len)
509 {
510         struct msghdr m;
511         struct iovec v;
512         struct in6_pktinfo *pktinfo;
513         struct cmsghdr *cmsg;
514         int fd, ret;
515         struct sockaddr_in6 dst;
516         void *control_buf;
517         size_t control_buf_len;
518
519         fd = socket(PF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
520         if (fd < 0)
521                 return -errno;
522
523         memset(&dst, 0, sizeof(dst));
524         dst.sin6_family = AF_INET6;
525         dst.sin6_port = htons(DHCPV6_SERVER_PORT);
526
527         dst.sin6_addr = in6addr_all_dhcp_relay_agents_and_servers_mc;
528
529         control_buf_len = CMSG_SPACE(sizeof(struct in6_pktinfo));
530         control_buf = g_try_malloc0(control_buf_len);
531         if (!control_buf) {
532                 close(fd);
533                 return -ENOMEM;
534         }
535
536         memset(&m, 0, sizeof(m));
537         memset(&v, 0, sizeof(v));
538
539         m.msg_name = &dst;
540         m.msg_namelen = sizeof(dst);
541
542         v.iov_base = (char *)dhcp_pkt;
543         v.iov_len = len;
544         m.msg_iov = &v;
545         m.msg_iovlen = 1;
546
547         m.msg_control = control_buf;
548         m.msg_controllen = control_buf_len;
549         cmsg = CMSG_FIRSTHDR(&m);
550         cmsg->cmsg_level = IPPROTO_IPV6;
551         cmsg->cmsg_type = IPV6_PKTINFO;
552         cmsg->cmsg_len = CMSG_LEN(sizeof(*pktinfo));
553
554         pktinfo = (struct in6_pktinfo *)CMSG_DATA(cmsg);
555         memset(pktinfo, 0, sizeof(*pktinfo));
556         pktinfo->ipi6_ifindex = index;
557         m.msg_controllen = cmsg->cmsg_len;
558
559         ret = sendmsg(fd, &m, 0);
560         if (ret < 0) {
561                 char *msg = "DHCPv6 msg send failed";
562
563                 if (errno == EADDRNOTAVAIL) {
564                         char *str = g_strdup_printf("%s (index %d)",
565                                         msg, index);
566                         perror(str);
567                         g_free(str);
568                 } else
569                         perror(msg);
570         }
571
572         g_free(control_buf);
573         close(fd);
574
575         return ret;
576 }
577
578 int dhcp_send_raw_packet(struct dhcp_packet *dhcp_pkt,
579                         uint32_t source_ip, int source_port,
580                         uint32_t dest_ip, int dest_port,
581                         const uint8_t *dest_arp, int ifindex, bool bcast)
582 {
583         struct sockaddr_ll dest;
584         struct ip_udp_dhcp_packet packet;
585         int fd, n;
586
587         enum {
588                 IP_UPD_DHCP_SIZE = sizeof(struct ip_udp_dhcp_packet) -
589                                                 EXTEND_FOR_BUGGY_SERVERS,
590                 UPD_DHCP_SIZE = IP_UPD_DHCP_SIZE -
591                                 offsetof(struct ip_udp_dhcp_packet, udp),
592         };
593
594         fd = socket(PF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC, htons(ETH_P_IP));
595         if (fd < 0)
596                 return -errno;
597
598         if (bcast)
599                 dhcp_pkt->flags |= htons(BROADCAST_FLAG);
600
601         memset(&dest, 0, sizeof(dest));
602         memset(&packet, 0, sizeof(packet));
603         packet.data = *dhcp_pkt;
604
605         dest.sll_family = AF_PACKET;
606         dest.sll_protocol = htons(ETH_P_IP);
607         dest.sll_ifindex = ifindex;
608         dest.sll_halen = 6;
609         memcpy(dest.sll_addr, dest_arp, 6);
610         if (bind(fd, (struct sockaddr *)&dest, sizeof(dest)) < 0) {
611                 close(fd);
612                 return -errno;
613         }
614
615         packet.ip.protocol = IPPROTO_UDP;
616         packet.ip.saddr = source_ip;
617         packet.ip.daddr = dest_ip;
618         packet.udp.source = htons(source_port);
619         packet.udp.dest = htons(dest_port);
620         /* size, excluding IP header: */
621         packet.udp.len = htons(UPD_DHCP_SIZE);
622         /* for UDP checksumming, ip.len is set to UDP packet len */
623         packet.ip.tot_len = packet.udp.len;
624         packet.udp.check = dhcp_checksum(&packet, IP_UPD_DHCP_SIZE);
625         /* but for sending, it is set to IP packet len */
626         packet.ip.tot_len = htons(IP_UPD_DHCP_SIZE);
627         packet.ip.ihl = sizeof(packet.ip) >> 2;
628         packet.ip.version = IPVERSION;
629         packet.ip.ttl = IPDEFTTL;
630         packet.ip.check = dhcp_checksum(&packet.ip, sizeof(packet.ip));
631
632         /*
633          * Currently we send full-sized DHCP packets (zero padded).
634          * If you need to change this: last byte of the packet is
635          * packet.data.options[dhcp_end_option(packet.data.options)]
636          */
637         n = sendto(fd, &packet, IP_UPD_DHCP_SIZE, 0,
638                         (struct sockaddr *) &dest, sizeof(dest));
639         close(fd);
640
641         if (n < 0)
642                 return -errno;
643
644         return n;
645 }
646
647 int dhcp_send_kernel_packet(struct dhcp_packet *dhcp_pkt,
648                                 uint32_t source_ip, int source_port,
649                                 uint32_t dest_ip, int dest_port)
650 {
651         struct sockaddr_in client;
652         int fd, n, opt = 1;
653
654         enum {
655                 DHCP_SIZE = sizeof(struct dhcp_packet) -
656                                         EXTEND_FOR_BUGGY_SERVERS,
657         };
658
659         fd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
660         if (fd < 0)
661                 return -errno;
662
663         setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
664
665         memset(&client, 0, sizeof(client));
666         client.sin_family = AF_INET;
667         client.sin_port = htons(source_port);
668         client.sin_addr.s_addr = htonl(source_ip);
669         if (bind(fd, (struct sockaddr *) &client, sizeof(client)) < 0) {
670                 close(fd);
671                 return -errno;
672         }
673
674         memset(&client, 0, sizeof(client));
675         client.sin_family = AF_INET;
676         client.sin_port = htons(dest_port);
677         client.sin_addr.s_addr = htonl(dest_ip);
678         if (connect(fd, (struct sockaddr *) &client, sizeof(client)) < 0) {
679                 close(fd);
680                 return -errno;
681         }
682
683         n = write(fd, dhcp_pkt, DHCP_SIZE);
684
685         close(fd);
686
687         if (n < 0)
688                 return -errno;
689
690         return n;
691 }
692
693 int dhcp_l3_socket(int port, const char *interface, int family)
694 {
695         int fd, opt = 1, len;
696         struct sockaddr_in addr4;
697         struct sockaddr_in6 addr6;
698         struct sockaddr *addr;
699
700         fd = socket(family, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
701         if (fd < 0)
702                 return -errno;
703
704         setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
705
706         if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
707                                 interface, strlen(interface) + 1) < 0) {
708                 close(fd);
709                 return -1;
710         }
711
712         if (family == AF_INET) {
713                 memset(&addr4, 0, sizeof(addr4));
714                 addr4.sin_family = family;
715                 addr4.sin_port = htons(port);
716                 addr = (struct sockaddr *)&addr4;
717                 len = sizeof(addr4);
718         } else if (family == AF_INET6) {
719                 memset(&addr6, 0, sizeof(addr6));
720                 addr6.sin6_family = family;
721                 addr6.sin6_port = htons(port);
722                 addr = (struct sockaddr *)&addr6;
723                 len = sizeof(addr6);
724         } else {
725                 close(fd);
726                 return -EINVAL;
727         }
728
729         if (bind(fd, addr, len) != 0) {
730                 close(fd);
731                 return -1;
732         }
733
734         return fd;
735 }
736
737 char *get_interface_name(int index)
738 {
739         struct ifreq ifr;
740         int sk, err;
741
742         if (index < 0)
743                 return NULL;
744
745         sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
746         if (sk < 0) {
747                 perror("Open socket error");
748                 return NULL;
749         }
750
751         memset(&ifr, 0, sizeof(ifr));
752         ifr.ifr_ifindex = index;
753
754         err = ioctl(sk, SIOCGIFNAME, &ifr);
755         if (err < 0) {
756                 perror("Get interface name error");
757                 close(sk);
758                 return NULL;
759         }
760
761         close(sk);
762
763         return g_strdup(ifr.ifr_name);
764 }
765
766 bool interface_is_up(int index)
767 {
768         int sk, err;
769         struct ifreq ifr;
770         bool ret = false;
771
772         sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
773         if (sk < 0) {
774                 perror("Open socket error");
775                 return false;
776         }
777
778         memset(&ifr, 0, sizeof(ifr));
779         ifr.ifr_ifindex = index;
780
781         err = ioctl(sk, SIOCGIFNAME, &ifr);
782         if (err < 0) {
783                 perror("Get interface name error");
784                 goto done;
785         }
786
787         err = ioctl(sk, SIOCGIFFLAGS, &ifr);
788         if (err < 0) {
789                 perror("Get interface flags error");
790                 goto done;
791         }
792
793         if (ifr.ifr_flags & IFF_UP)
794                 ret = true;
795
796 done:
797         close(sk);
798
799         return ret;
800 }