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>
55 (sizeof(*(su)) - sizeof((su)->sun_path) + strlen((su)->sun_path))
58 static const char * const dhcpcd_types[] =
59 { "link", "ipv4", "ra", "dhcp6", NULL };
62 dhcpcd_command_fd(DHCPCD_CONNECTION *con,
63 int fd, const char *cmd, char **buffer)
70 /* Each command is \n terminated.
71 * Each argument is NULL seperated.
72 * We may need to send a space one day, so the API
73 * in this function may need to be improved */
74 len = strlen(cmd) + 1;
75 if (con->terminate_commands)
77 if (len > sizeof(buf)) {
81 strlcpy(buf, cmd, sizeof(buf));
83 while ((p = strchr(p, ' ')) != NULL)
85 if (con->terminate_commands) {
90 if (write(fd, buf, len) == -1)
95 bytes = read(fd, buf, sizeof(size_t));
96 if (bytes == 0 || bytes == -1)
98 memcpy(&len, buf, sizeof(size_t));
99 nbuf = realloc(*buffer, len + 1);
103 bytes = read(fd, *buffer, len);
104 if (bytes != -1 && (size_t)bytes < len)
105 *buffer[bytes] = '\0';
110 dhcpcd_command(DHCPCD_CONNECTION *con, const char *cmd, char **buffer)
113 return dhcpcd_command_fd(con, con->command_fd, cmd, buffer);
117 dhcpcd_realloc(DHCPCD_CONNECTION *con, size_t len)
120 if (con->buflen < len) {
123 nbuf = realloc(con->buf, len);
133 dhcpcd_command_arg(DHCPCD_CONNECTION *con, const char *cmd, const char *arg,
138 cmdlen = strlen(cmd);
139 len = cmdlen + strlen(arg) + 2;
140 if (!dhcpcd_realloc(con, len))
142 strlcpy(con->buf, cmd, con->buflen);
143 con->buf[cmdlen] = ' ';
144 strlcpy(con->buf + cmdlen + 1, arg, con->buflen - 1 - cmdlen);
146 return dhcpcd_command_fd(con, con->command_fd, con->buf, buffer);
155 struct sockaddr_un sun;
157 fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
161 memset(&sun, 0, sizeof(sun));
162 sun.sun_family = AF_UNIX;
163 strlcpy(sun.sun_path, DHCPCD_SOCKET, sizeof(sun.sun_path));
164 len = (socklen_t)SUN_LEN(&sun);
165 if (connect(fd, (struct sockaddr *)&sun, len) == 0)
172 get_value(const char *data, size_t len, const char *var)
181 while (data + vlen + 1 < end) {
182 if (strncmp(data, var, vlen) == 0) {
186 data += strlen(data) + 1;
188 if (p != NULL && *p != '\0')
194 dhcpcd_get_value(const DHCPCD_IF *i, const char *var)
198 return get_value(i->data, i->data_len, var);
202 dhcpcd_get_prefix_value(const DHCPCD_IF *i, const char *prefix, const char *var)
209 l = strlcpy(p, prefix, plen);
210 if (l >= sizeof(pvar)) {
216 if (strlcpy(p, var, plen) >= plen) {
220 return dhcpcd_get_value(i, pvar);
223 static bool strtobool(const char *var)
229 return (*var == '0' || *var == '\0' ||
230 strcmp(var, "false") == 0 ||
231 strcmp(var, "no") == 0) ? false : true;
235 get_status(DHCPCD_CONNECTION *con)
241 if (con->command_fd == -1 || con->listen_fd == -1)
244 status = "disconnected";
245 for (i = con->interfaces; i; i = i->next) {
247 if (strcmp(i->type, "link")) {
248 status = "connected";
251 status = "connecting";
258 update_status(DHCPCD_CONNECTION *con)
263 nstatus = get_status(con);
264 if (con->status == NULL || strcmp(nstatus, con->status)) {
265 con->status = nstatus;
267 con->status_cb(con, con->status, con->status_context);
272 dhcpcd_interfaces(DHCPCD_CONNECTION *con)
276 return con->interfaces;
280 dhcpcd_get_if(DHCPCD_CONNECTION *con, const char *ifname, const char *type)
285 for (i = con->interfaces; i; i = i->next)
286 if (strcmp(i->ifname, ifname) == 0 &&
287 strcmp(i->type, type) == 0)
293 dhcpcd_new_if(DHCPCD_CONNECTION *con, char *data, size_t len)
295 const char *ifname, *ifclass, *reason, *type, *order, *flags;
296 char *orderdup, *o, *p;
297 DHCPCD_IF *e, *i, *l, *n, *nl;
300 ifname = get_value(data, len, "interface=");
301 if (ifname == NULL || *ifname == '\0') {
305 reason = get_value(data, len, "reason=");
306 if (reason == NULL || *reason == '\0') {
310 ifclass = get_value(data, len, "ifclass=");
311 /* Skip pseudo interfaces */
312 if (ifclass && *ifclass != '\0') {
316 if (strcmp(reason, "RECONFIGURE") == 0) {
320 order = get_value(data, len, "interface_order=");
321 if (order == NULL || *order == '\0') {
326 if (strcmp(reason, "PREINIT") == 0 ||
327 strcmp(reason, "UNKNOWN") == 0 ||
328 strcmp(reason, "CARRIER") == 0 ||
329 strcmp(reason, "NOCARRIER") == 0 ||
330 strcmp(reason, "DEPARTED") == 0 ||
331 strcmp(reason, "STOPPED") == 0)
333 else if (strcmp(reason, "ROUTERADVERT") == 0)
335 else if (reason[strlen(reason) - 1] == '6')
341 /* Remove all instances on carrier drop */
342 if (strcmp(reason, "NOCARRIER") == 0 ||
343 strcmp(reason, "DEPARTED") == 0 ||
344 strcmp(reason, "STOPPED") == 0)
347 for (e = con->interfaces; e; e = n) {
349 if (strcmp(e->ifname, ifname) == 0) {
350 if (strcmp(e->type, type) == 0)
356 con->interfaces = e->next;
362 } else if (strcmp(type, "link")) {
363 /* If link is down, ignore it */
364 e = dhcpcd_get_if(con, ifname, "link");
365 if (e && strcmp(e->reason, "NOCARRIER") == 0)
369 orderdup = strdup(order);
370 if (orderdup == NULL)
373 /* Find our pointer */
376 for (e = con->interfaces; e; e = e->next) {
377 if (strcmp(e->ifname, ifname) == 0 &&
378 strcmp(e->type, type) == 0)
387 i = malloc(sizeof(*i));
400 /* Now fill out our interface structure */
407 flags = dhcpcd_get_value(i, "ifflags=");
409 i->flags = (unsigned int)strtoul(flags, NULL, 0);
412 if (strcmp(reason, "CARRIER") == 0)
415 i->up = strtobool(dhcpcd_get_value(i, "if_up="));
416 i->wireless = strtobool(dhcpcd_get_value(i, "ifwireless="));
417 i->ssid = dhcpcd_get_value(i, i->up ? "new_ssid=" : "old_ssid=");
422 while ((o = strsep(&p, " ")) != NULL) {
423 for (ti = 0; dhcpcd_types[ti]; ti++) {
425 for (e = con->interfaces; e; e = e->next) {
426 if (strcmp(e->ifname, o) == 0 &&
427 strcmp(e->type, dhcpcd_types[ti]) == 0)
436 con->interfaces = e->next;
447 /* Free any stragglers */
448 while (con->interfaces) {
449 e = con->interfaces->next;
450 free(con->interfaces->data);
451 free(con->interfaces);
460 dhcpcd_read_if(DHCPCD_CONNECTION *con, int fd)
462 char sbuf[sizeof(size_t)], *rbuf;
467 bytes = read(fd, sbuf, sizeof(sbuf));
468 if (bytes == 0 || bytes == -1) {
472 memcpy(&len, sbuf, sizeof(len));
473 rbuf = malloc(len + 1);
476 bytes = read(fd, rbuf, len);
477 if (bytes == 0 || bytes == -1) {
482 if ((size_t)bytes != len) {
489 i = dhcpcd_new_if(con, rbuf, len);
496 dhcpcd_dispatchif(DHCPCD_IF *i)
501 i->con->if_cb(i, i->con->if_context);
502 dhcpcd_wpa_if_event(i);
506 dhcpcd_dispatch(DHCPCD_CONNECTION *con)
511 i = dhcpcd_read_if(con, con->listen_fd);
513 dhcpcd_dispatchif(i);
515 /* Have to call update_status last as it could
516 * cause the interface to be destroyed. */
523 DHCPCD_CONNECTION *con;
525 con = calloc(1, sizeof(*con));
526 con->command_fd = con->listen_fd = -1;
531 /* Good enough for our needs */
533 strverscmp(const char *s1, const char *s2)
535 int s1maj, s1min, s1part;
536 int s2maj, s2min, s2part;
540 if (sscanf(s1, "%d.%d.%d", &s1maj, &s1min, &s1part) < 1)
543 if (sscanf(s2, "%d.%d.%d", &s2maj, &s2min, &s2part) < 1)
551 return s1part - s2part;
556 dhcpcd_open(DHCPCD_CONNECTION *con)
564 if (con->listen_fd != -1)
565 return con->listen_fd;
566 con->command_fd = dhcpcd_connect();
567 if (con->command_fd == -1) {
572 con->terminate_commands = false;
573 if (dhcpcd_command(con, "--version", &con->version) <= 0)
575 con->terminate_commands =
576 strverscmp(con->version, "6.4.1") >= 0 ? true : false;
578 if (dhcpcd_command(con, "--getconfigfile", &con->cffile) <= 0)
581 con->listen_fd = dhcpcd_connect();
582 if (con->listen_fd == -1) {
583 close(con->command_fd);
584 con->command_fd = -1;
588 dhcpcd_command_fd(con, con->listen_fd, "--listen", NULL);
590 dhcpcd_command_fd(con, con->command_fd, "--getinterfaces", NULL);
591 bytes = read(con->command_fd, cmd, sizeof(nifs));
592 if (bytes != sizeof(nifs)) {
593 close(con->command_fd);
594 con->command_fd = -1;
595 close(con->listen_fd);
598 memcpy(&nifs, cmd, sizeof(nifs));
599 /* We don't dispatch each interface here as that
600 * causes too much notification spam when the GUI starts */
601 for (n = 0; n < nifs; n++) {
602 i = dhcpcd_read_if(con, con->command_fd);
604 dhcpcd_wpa_if_event(i);
609 return con->listen_fd;
613 dhcpcd_get_fd(DHCPCD_CONNECTION *con)
617 return con->listen_fd;
621 dhcpcd_status(DHCPCD_CONNECTION *con)
629 dhcpcd_version(DHCPCD_CONNECTION *con)
637 dhcpcd_cffile(DHCPCD_CONNECTION *con)
645 dhcpcd_set_if_callback(DHCPCD_CONNECTION *con,
646 void (*cb)(DHCPCD_IF *, void *), void *ctx)
651 con->if_context = ctx;
655 dhcpcd_set_status_callback(DHCPCD_CONNECTION *con,
656 void (*cb)(DHCPCD_CONNECTION *, const char *, void *), void *ctx)
661 con->status_context = ctx;
665 dhcpcd_close(DHCPCD_CONNECTION *con)
671 /* Shut down WPA listeners as they aren't much good without dhcpcd.
672 * They'll be restarted anyway when dhcpcd comes back up. */
673 for (wpa = con->wpa; wpa; wpa = wpa->next)
674 dhcpcd_wpa_close(con->wpa);
676 if (con->command_fd != -1) {
677 shutdown(con->command_fd, SHUT_RDWR);
678 con->command_fd = -1;
680 if (con->listen_fd != -1) {
681 shutdown(con->listen_fd, SHUT_RDWR);
701 dhcpcd_if_connection(DHCPCD_IF *i)
709 dhcpcd_if_message(const DHCPCD_IF *i)
711 const char *ip, *iplen, *pfx;
713 const char *reason = NULL;
718 /* Don't report non SLAAC configurations */
719 if (strcmp(i->type, "ra") == 0 && i->up &&
720 dhcpcd_get_value(i, "ra1_prefix=") == NULL)
724 if (strcmp(i->reason, "EXPIRE") == 0)
725 reason = _("Expired");
726 else if (strcmp(i->reason, "CARRIER") == 0) {
729 reason = _("Associated with");
731 reason = _("Cable plugged in");
732 } else if (strcmp(i->reason, "NOCARRIER") == 0) {
735 reason = _("Disassociated from");
738 reason = _("Not associated");
740 reason = _("Cable unplugged");
741 } else if (strcmp(i->reason, "UNKNOWN") == 0)
742 reason = _("Unknown link state");
743 else if (strcmp(i->reason, "FAIL") == 0)
744 reason = _("Automatic configuration not possible");
745 else if (strcmp(i->reason, "3RDPARTY") == 0)
746 reason = _("Waiting for 3rd Party configuration");
748 if (reason == NULL) {
750 reason = _("Configured");
751 else if (strcmp(i->type, "ra") == 0)
752 reason = "Expired RA";
757 pfx = i->up ? "new_" : "old_";
758 if ((ip = dhcpcd_get_prefix_value(i, pfx, "ip_address=")))
759 iplen = dhcpcd_get_prefix_value(i, pfx, "subnet_cidr=");
760 else if ((ip = dhcpcd_get_value(i, "ra1_prefix=")))
762 else if ((ip = dhcpcd_get_prefix_value(i, pfx,
763 "dhcp6_ia_na1_ia_addr1=")))
770 len = strlen(i->ifname) + strlen(reason) + 3;
771 if (showssid && i->ssid)
772 len += strlen(i->ssid) + 1;
774 len += strlen(ip) + 1;
776 len += strlen(iplen) + 1;
777 msg = p = malloc(len);
780 p += snprintf(msg, len, "%s: %s", i->ifname, reason);
782 p += snprintf(p, len - (size_t)(p - msg), " %s", i->ssid);
784 p += snprintf(p, len - (size_t)(p - msg), " %s/%s", ip, iplen);
786 p += snprintf(p, len - (size_t)(p - msg), " %s", ip);
791 dhcpcd_free(DHCPCD_CONNECTION *con)
798 while (con->interfaces) {
799 nif = con->interfaces->next;
800 free(con->interfaces->data);
801 free(con->interfaces);
802 con->interfaces = nif;
805 nwpa = con->wpa->next;
806 dhcpcd_wpa_close(con->wpa);
810 while (con->wi_history) {
811 nh = con->wi_history->next;
812 free(con->wi_history);
813 con->wi_history = nh;