3 * Copyright 2009-2014 Roy Marples <roy@marples.name>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/socket.h>
34 #include <arpa/inet.h>
60 (sizeof(*(su)) - sizeof((su)->sun_path) + strlen((su)->sun_path))
63 static const char * const dhcpcd_types[] =
64 { "link", "ipv4", "ra", "dhcp6", NULL };
67 dhcpcd_command_fd(DHCPCD_CONNECTION *con,
68 int fd, const char *cmd, char **buffer)
75 /* Each command is \n terminated.
76 * Each argument is NULL seperated.
77 * We may need to send a space one day, so the API
78 * in this function may need to be improved */
79 len = strlen(cmd) + 1;
80 if (con->terminate_commands)
82 if (len > sizeof(buf)) {
86 strlcpy(buf, cmd, sizeof(buf));
88 while ((p = strchr(p, ' ')) != NULL)
90 if (con->terminate_commands) {
95 if (write(fd, buf, len) == -1)
100 bytes = read(fd, buf, sizeof(size_t));
101 if (bytes == 0 || bytes == -1)
103 memcpy(&len, buf, sizeof(size_t));
104 nbuf = realloc(*buffer, len + 1);
108 bytes = read(fd, *buffer, len);
109 if (bytes != -1 && (size_t)bytes < len)
110 *buffer[bytes] = '\0';
115 dhcpcd_command(DHCPCD_CONNECTION *con, const char *cmd, char **buffer)
118 return dhcpcd_command_fd(con, con->command_fd, cmd, buffer);
122 dhcpcd_realloc(DHCPCD_CONNECTION *con, size_t len)
125 if (con->buflen < len) {
128 nbuf = realloc(con->buf, len);
138 dhcpcd_command_arg(DHCPCD_CONNECTION *con, const char *cmd, const char *arg,
143 cmdlen = strlen(cmd);
144 len = cmdlen + strlen(arg) + 2;
145 if (!dhcpcd_realloc(con, len))
147 strlcpy(con->buf, cmd, con->buflen);
148 con->buf[cmdlen] = ' ';
149 strlcpy(con->buf + cmdlen + 1, arg, con->buflen - 1 - cmdlen);
151 return dhcpcd_command_fd(con, con->command_fd, con->buf, buffer);
160 struct sockaddr_un sun;
162 fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
166 memset(&sun, 0, sizeof(sun));
167 sun.sun_family = AF_UNIX;
168 strlcpy(sun.sun_path, DHCPCD_SOCKET, sizeof(sun.sun_path));
169 len = (socklen_t)SUN_LEN(&sun);
170 if (connect(fd, (struct sockaddr *)&sun, len) == 0)
177 get_value(const char *data, size_t len, const char *var)
186 while (data + vlen + 1 < end) {
187 if (strncmp(data, var, vlen) == 0) {
191 data += strlen(data) + 1;
193 if (p != NULL && *p != '\0')
199 dhcpcd_get_value(const DHCPCD_IF *i, const char *var)
203 return get_value(i->data, i->data_len, var);
207 dhcpcd_get_prefix_value(const DHCPCD_IF *i, const char *prefix, const char *var)
214 l = strlcpy(p, prefix, plen);
215 if (l >= sizeof(pvar)) {
221 if (strlcpy(p, var, plen) >= plen) {
225 return dhcpcd_get_value(i, pvar);
228 static bool strtobool(const char *var)
234 return (*var == '0' || *var == '\0' ||
235 strcmp(var, "false") == 0 ||
236 strcmp(var, "no") == 0) ? false : true;
240 get_status(DHCPCD_CONNECTION *con)
246 if (con->command_fd == -1 || con->listen_fd == -1)
249 status = "disconnected";
250 for (i = con->interfaces; i; i = i->next) {
252 if (strcmp(i->type, "link")) {
253 status = "connected";
256 status = "connecting";
263 update_status(DHCPCD_CONNECTION *con)
268 nstatus = get_status(con);
269 if (con->status == NULL || strcmp(nstatus, con->status)) {
270 con->status = nstatus;
272 con->status_cb(con, con->status, con->status_context);
277 dhcpcd_interfaces(DHCPCD_CONNECTION *con)
281 return con->interfaces;
285 dhcpcd_get_if(DHCPCD_CONNECTION *con, const char *ifname, const char *type)
290 for (i = con->interfaces; i; i = i->next)
291 if (strcmp(i->ifname, ifname) == 0 &&
292 strcmp(i->type, type) == 0)
298 dhcpcd_new_if(DHCPCD_CONNECTION *con, char *data, size_t len)
300 const char *ifname, *ifclass, *reason, *type, *order, *flags;
301 char *orderdup, *o, *p;
302 DHCPCD_IF *e, *i, *l, *n, *nl;
305 ifname = get_value(data, len, "interface=");
306 if (ifname == NULL || *ifname == '\0') {
310 reason = get_value(data, len, "reason=");
311 if (reason == NULL || *reason == '\0') {
315 ifclass = get_value(data, len, "ifclass=");
316 /* Skip pseudo interfaces */
317 if (ifclass && *ifclass != '\0') {
321 if (strcmp(reason, "RECONFIGURE") == 0) {
325 order = get_value(data, len, "interface_order=");
326 if (order == NULL || *order == '\0') {
331 if (strcmp(reason, "PREINIT") == 0 ||
332 strcmp(reason, "UNKNOWN") == 0 ||
333 strcmp(reason, "CARRIER") == 0 ||
334 strcmp(reason, "NOCARRIER") == 0 ||
335 strcmp(reason, "DEPARTED") == 0 ||
336 strcmp(reason, "STOPPED") == 0)
338 else if (strcmp(reason, "ROUTERADVERT") == 0)
340 else if (reason[strlen(reason) - 1] == '6')
346 /* Remove all instances on carrier drop */
347 if (strcmp(reason, "NOCARRIER") == 0 ||
348 strcmp(reason, "DEPARTED") == 0 ||
349 strcmp(reason, "STOPPED") == 0)
352 for (e = con->interfaces; e; e = n) {
354 if (strcmp(e->ifname, ifname) == 0) {
355 if (strcmp(e->type, type) == 0)
361 con->interfaces = e->next;
367 } else if (strcmp(type, "link")) {
368 /* If link is down, ignore it */
369 e = dhcpcd_get_if(con, ifname, "link");
370 if (e && strcmp(e->reason, "NOCARRIER") == 0)
374 orderdup = strdup(order);
375 if (orderdup == NULL)
378 /* Find our pointer */
381 for (e = con->interfaces; e; e = e->next) {
382 if (strcmp(e->ifname, ifname) == 0 &&
383 strcmp(e->type, type) == 0)
392 i = malloc(sizeof(*i));
402 i->last_message = NULL;
406 /* Now fill out our interface structure */
413 flags = dhcpcd_get_value(i, "ifflags=");
415 i->flags = (unsigned int)strtoul(flags, NULL, 0);
418 if (strcmp(reason, "CARRIER") == 0)
421 i->up = strtobool(dhcpcd_get_value(i, "if_up="));
422 i->wireless = strtobool(dhcpcd_get_value(i, "ifwireless="));
423 i->ssid = dhcpcd_get_value(i, i->up ? "new_ssid=" : "old_ssid=");
428 while ((o = strsep(&p, " ")) != NULL) {
429 for (ti = 0; dhcpcd_types[ti]; ti++) {
431 for (e = con->interfaces; e; e = e->next) {
432 if (strcmp(e->ifname, o) == 0 &&
433 strcmp(e->type, dhcpcd_types[ti]) == 0)
442 con->interfaces = e->next;
453 /* Free any stragglers */
454 while (con->interfaces) {
455 e = con->interfaces->next;
456 free(con->interfaces->data);
457 free(con->interfaces->last_message);
458 free(con->interfaces);
467 dhcpcd_read_if(DHCPCD_CONNECTION *con, int fd)
469 char sbuf[sizeof(size_t)], *rbuf;
474 bytes = read(fd, sbuf, sizeof(sbuf));
475 if (bytes == 0 || bytes == -1) {
479 memcpy(&len, sbuf, sizeof(len));
480 rbuf = malloc(len + 1);
483 bytes = read(fd, rbuf, len);
484 if (bytes == 0 || bytes == -1) {
489 if ((size_t)bytes != len) {
496 i = dhcpcd_new_if(con, rbuf, len);
503 dhcpcd_dispatchif(DHCPCD_IF *i)
508 i->con->if_cb(i, i->con->if_context);
509 dhcpcd_wpa_if_event(i);
513 dhcpcd_dispatch(DHCPCD_CONNECTION *con)
518 i = dhcpcd_read_if(con, con->listen_fd);
520 dhcpcd_dispatchif(i);
522 /* Have to call update_status last as it could
523 * cause the interface to be destroyed. */
530 DHCPCD_CONNECTION *con;
532 con = calloc(1, sizeof(*con));
533 con->command_fd = con->listen_fd = -1;
538 /* Good enough for our needs */
540 strverscmp(const char *s1, const char *s2)
542 int s1maj, s1min, s1part;
543 int s2maj, s2min, s2part;
547 if (sscanf(s1, "%d.%d.%d", &s1maj, &s1min, &s1part) < 1)
550 if (sscanf(s2, "%d.%d.%d", &s2maj, &s2min, &s2part) < 1)
558 return s1part - s2part;
563 dhcpcd_open(DHCPCD_CONNECTION *con)
571 if (con->listen_fd != -1)
572 return con->listen_fd;
573 con->command_fd = dhcpcd_connect();
574 if (con->command_fd == -1) {
579 con->terminate_commands = false;
580 if (dhcpcd_command(con, "--version", &con->version) <= 0)
582 con->terminate_commands =
583 strverscmp(con->version, "6.4.1") >= 0 ? true : false;
585 if (dhcpcd_command(con, "--getconfigfile", &con->cffile) <= 0)
588 con->listen_fd = dhcpcd_connect();
589 if (con->listen_fd == -1) {
590 close(con->command_fd);
591 con->command_fd = -1;
595 dhcpcd_command_fd(con, con->listen_fd, "--listen", NULL);
597 dhcpcd_command_fd(con, con->command_fd, "--getinterfaces", NULL);
598 bytes = read(con->command_fd, cmd, sizeof(nifs));
599 if (bytes != sizeof(nifs)) {
600 close(con->command_fd);
601 con->command_fd = -1;
602 close(con->listen_fd);
605 memcpy(&nifs, cmd, sizeof(nifs));
606 /* We don't dispatch each interface here as that
607 * causes too much notification spam when the GUI starts */
608 for (n = 0; n < nifs; n++) {
609 i = dhcpcd_read_if(con, con->command_fd);
611 dhcpcd_wpa_if_event(i);
616 return con->listen_fd;
620 dhcpcd_get_fd(DHCPCD_CONNECTION *con)
624 return con->listen_fd;
628 dhcpcd_status(DHCPCD_CONNECTION *con)
636 dhcpcd_version(DHCPCD_CONNECTION *con)
644 dhcpcd_cffile(DHCPCD_CONNECTION *con)
652 dhcpcd_set_if_callback(DHCPCD_CONNECTION *con,
653 void (*cb)(DHCPCD_IF *, void *), void *ctx)
658 con->if_context = ctx;
662 dhcpcd_set_status_callback(DHCPCD_CONNECTION *con,
663 void (*cb)(DHCPCD_CONNECTION *, const char *, void *), void *ctx)
668 con->status_context = ctx;
672 dhcpcd_close(DHCPCD_CONNECTION *con)
678 /* Shut down WPA listeners as they aren't much good without dhcpcd.
679 * They'll be restarted anyway when dhcpcd comes back up. */
680 for (wpa = con->wpa; wpa; wpa = wpa->next)
681 dhcpcd_wpa_close(con->wpa);
683 if (con->command_fd != -1) {
684 shutdown(con->command_fd, SHUT_RDWR);
685 con->command_fd = -1;
687 if (con->listen_fd != -1) {
688 shutdown(con->listen_fd, SHUT_RDWR);
708 dhcpcd_if_connection(DHCPCD_IF *i)
716 dhcpcd_if_message(DHCPCD_IF *i, bool *new_msg)
718 const char *ip, *iplen, *pfx;
720 const char *reason = NULL;
725 /* Don't report non SLAAC configurations */
726 if (strcmp(i->type, "ra") == 0 && i->up &&
727 dhcpcd_get_value(i, "ra1_prefix=") == NULL)
731 if (strcmp(i->reason, "EXPIRE") == 0)
732 reason = _("Expired");
733 else if (strcmp(i->reason, "CARRIER") == 0) {
736 reason = _("Associated with");
738 reason = _("Cable plugged in");
739 } else if (strcmp(i->reason, "NOCARRIER") == 0) {
742 reason = _("Disassociated from");
745 reason = _("Not associated");
747 reason = _("Cable unplugged");
748 } else if (strcmp(i->reason, "UNKNOWN") == 0)
749 reason = _("Unknown link state");
750 else if (strcmp(i->reason, "FAIL") == 0)
751 reason = _("Automatic configuration not possible");
752 else if (strcmp(i->reason, "3RDPARTY") == 0)
753 reason = _("Waiting for 3rd Party configuration");
755 if (reason == NULL) {
757 reason = _("Configured");
758 else if (strcmp(i->type, "ra") == 0)
759 reason = "Expired RA";
764 pfx = i->up ? "new_" : "old_";
765 if ((ip = dhcpcd_get_prefix_value(i, pfx, "ip_address=")))
766 iplen = dhcpcd_get_prefix_value(i, pfx, "subnet_cidr=");
767 else if ((ip = dhcpcd_get_value(i, "ra1_prefix=")))
769 else if ((ip = dhcpcd_get_prefix_value(i, pfx,
770 "dhcp6_ia_na1_ia_addr1=")))
777 len = strlen(i->ifname) + strlen(reason) + 3;
778 if (showssid && i->ssid)
779 len += strlen(i->ssid) + 1;
781 len += strlen(ip) + 1;
783 len += strlen(iplen) + 1;
784 msg = p = malloc(len);
787 p += snprintf(msg, len, "%s: %s", i->ifname, reason);
789 p += snprintf(p, len - (size_t)(p - msg), " %s", i->ssid);
791 p += snprintf(p, len - (size_t)(p - msg), " %s/%s", ip, iplen);
793 p += snprintf(p, len - (size_t)(p - msg), " %s", ip);
796 if (i->last_message == NULL || strcmp(i->last_message, msg))
801 free(i->last_message);
802 i->last_message = strdup(msg);
808 dhcpcd_free(DHCPCD_CONNECTION *con)
815 while (con->interfaces) {
816 nif = con->interfaces->next;
817 free(con->interfaces->data);
818 free(con->interfaces->last_message);
819 free(con->interfaces);
820 con->interfaces = nif;
823 nwpa = con->wpa->next;
824 dhcpcd_wpa_close(con->wpa);
828 while (con->wi_history) {
829 nh = con->wi_history->next;
830 free(con->wi_history);
831 con->wi_history = nh;