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