X-Git-Url: http://git.archive.openwrt.org/?p=project%2Fnetifd.git;a=blobdiff_plain;f=system-linux.c;h=1fcf22fc0fe8042e9c423ddfe64450b48f95182a;hp=44332a2b88d79af2b487b12a966673f9e9bff547;hb=a12081aa986c4cc878bf0480a5d0ffb986aed0c5;hpb=fb0e4138070d7c2ce723af5780e763af3a1353d8 diff --git a/system-linux.c b/system-linux.c index 44332a2..1fcf22f 100644 --- a/system-linux.c +++ b/system-linux.c @@ -14,6 +14,7 @@ #include #include #include +#include #include #include @@ -24,67 +25,95 @@ #include "device.h" #include "system.h" +struct event_socket { + struct uloop_fd uloop; + struct nl_sock *sock; + struct nl_cb *cb; +}; + static int sock_ioctl = -1; static struct nl_sock *sock_rtnl = NULL; -static struct nl_sock *sock_rtnl_event = NULL; -static void handler_rtnl_event(struct uloop_fd *u, unsigned int events); static int cb_rtnl_event(struct nl_msg *msg, void *arg); -static struct uloop_fd rtnl_event = {.cb = handler_rtnl_event}; -static struct nl_cb *nl_cb_rtnl_event; +static void handle_hotplug_event(struct uloop_fd *u, unsigned int events); -int system_init(void) +static void +handler_nl_event(struct uloop_fd *u, unsigned int events) { - sock_ioctl = socket(AF_LOCAL, SOCK_DGRAM, 0); - fcntl(sock_ioctl, F_SETFD, fcntl(sock_ioctl, F_GETFD) | FD_CLOEXEC); + struct event_socket *ev = container_of(u, struct event_socket, uloop); + nl_recvmsgs(ev->sock, ev->cb); +} - // Prepare socket for routing / address control - sock_rtnl = nl_socket_alloc(); - if (!sock_rtnl) - return -1; +static struct nl_sock * +create_socket(int protocol, int groups) +{ + struct nl_sock *sock; - if (nl_connect(sock_rtnl, NETLINK_ROUTE)) - goto error_free_sock; + sock = nl_socket_alloc(); + if (!sock) + return NULL; - // Prepare socket for link events - nl_cb_rtnl_event = nl_cb_alloc(NL_CB_DEFAULT); - if (!nl_cb_rtnl_event) - goto error_free_sock; + if (groups) + nl_join_groups(sock, groups); - nl_cb_set(nl_cb_rtnl_event, NL_CB_VALID, NL_CB_CUSTOM, - cb_rtnl_event, NULL); + if (nl_connect(sock, protocol)) + return NULL; - sock_rtnl_event = nl_socket_alloc(); - if (!sock_rtnl_event) - goto error_free_cb; + return sock; +} - if (nl_connect(sock_rtnl_event, NETLINK_ROUTE)) - goto error_free_event; +static bool +create_raw_event_socket(struct event_socket *ev, int protocol, int groups, + uloop_fd_handler cb) +{ + ev->sock = create_socket(protocol, groups); + if (!ev->sock) + return false; - // Receive network link events form kernel - nl_socket_add_membership(sock_rtnl_event, RTNLGRP_LINK); + ev->uloop.fd = nl_socket_get_fd(ev->sock); + ev->uloop.cb = cb; + uloop_fd_add(&ev->uloop, ULOOP_READ | ULOOP_EDGE_TRIGGER); + return true; +} - rtnl_event.fd = nl_socket_get_fd(sock_rtnl_event); - uloop_fd_add(&rtnl_event, ULOOP_READ | ULOOP_EDGE_TRIGGER); +static bool +create_event_socket(struct event_socket *ev, int protocol, + int (*cb)(struct nl_msg *msg, void *arg)) +{ + // Prepare socket for link events + ev->cb = nl_cb_alloc(NL_CB_DEFAULT); + if (!ev->cb) + return false; - return 0; + nl_cb_set(ev->cb, NL_CB_VALID, NL_CB_CUSTOM, cb, NULL); -error_free_event: - nl_socket_free(sock_rtnl_event); - sock_rtnl_event = NULL; -error_free_cb: - nl_cb_put(nl_cb_rtnl_event); - nl_cb_rtnl_event = NULL; -error_free_sock: - nl_socket_free(sock_rtnl); - sock_rtnl = NULL; - return -1; + return create_raw_event_socket(ev, protocol, 0, handler_nl_event); } -// If socket is ready for reading parse netlink events -static void handler_rtnl_event(struct uloop_fd *u, unsigned int events) +int system_init(void) { - nl_recvmsgs(sock_rtnl_event, nl_cb_rtnl_event); + static struct event_socket rtnl_event; + static struct event_socket hotplug_event; + + sock_ioctl = socket(AF_LOCAL, SOCK_DGRAM, 0); + fcntl(sock_ioctl, F_SETFD, fcntl(sock_ioctl, F_GETFD) | FD_CLOEXEC); + + // Prepare socket for routing / address control + sock_rtnl = create_socket(NETLINK_ROUTE, 0); + if (!sock_rtnl) + return -1; + + if (!create_event_socket(&rtnl_event, NETLINK_ROUTE, cb_rtnl_event)) + return -1; + + if (!create_raw_event_socket(&hotplug_event, NETLINK_KOBJECT_UEVENT, 1, + handle_hotplug_event)) + return -1; + + // Receive network link events form kernel + nl_socket_add_membership(rtnl_event.sock, RTNLGRP_LINK); + + return 0; } static void system_set_sysctl(const char *path, const char *val) @@ -101,7 +130,7 @@ static void system_set_sysctl(const char *path, const char *val) static void system_set_dev_sysctl(const char *path, const char *device, const char *val) { - char buf[256]; + static char buf[256]; snprintf(buf, sizeof(buf), path, val); system_set_sysctl(buf, val); @@ -131,12 +160,78 @@ static int cb_rtnl_event(struct nl_msg *msg, void *arg) goto out; dev->ifindex = ifi->ifi_index; - device_set_present(dev, (nh->nlmsg_type == RTM_NEWLINK)); + /* TODO: parse link status */ out: return 0; } +static void +handle_hotplug_msg(char *data, int size) +{ + const char *subsystem = NULL, *interface = NULL; + char *cur, *end, *sep; + struct device *dev; + int skip; + bool add; + + if (!strncmp(data, "add@", 4)) + add = true; + else if (!strncmp(data, "remove@", 7)) + add = false; + else + return; + + skip = strlen(data) + 1; + end = data + size; + + for (cur = data + skip; cur < end; cur += skip) { + skip = strlen(cur) + 1; + + sep = strchr(cur, '='); + if (!sep) + continue; + + *sep = 0; + if (!strcmp(cur, "INTERFACE")) + interface = sep + 1; + else if (!strcmp(cur, "SUBSYSTEM")) { + subsystem = sep + 1; + if (strcmp(subsystem, "net") != 0) + return; + } + if (subsystem && interface) + goto found; + } + return; + +found: + dev = device_get(interface, false); + if (!dev) + return; + + if (dev->type != &simple_device_type) + return; + + device_set_present(dev, add); +} + +static void +handle_hotplug_event(struct uloop_fd *u, unsigned int events) +{ + struct event_socket *ev = container_of(u, struct event_socket, uloop); + struct sockaddr_nl nla; + unsigned char *buf = NULL; + int size; + + while ((size = nl_recv(ev->sock, &nla, &buf, NULL)) > 0) { + if (nla.nl_pid == 0) + handle_hotplug_msg((char *) buf, size); + + free(buf); + } +} + static int system_rtnl_call(struct nl_msg *msg) { int s = -(nl_send_auto_complete(sock_rtnl, msg) @@ -367,7 +462,7 @@ out: */ void system_if_clear_state(struct device *dev) { - char buf[256]; + static char buf[256]; char *bridge; if (dev->external) @@ -509,11 +604,105 @@ int system_if_down(struct device *dev) int system_if_check(struct device *dev) { - device_set_present(dev, (system_if_resolve(dev) >= 0)); + device_set_present(dev, (system_if_resolve(dev) > 0)); return 0; } -int system_if_dump_stats(struct device *dev, struct blob_buf *b) +struct device * +system_if_get_parent(struct device *dev) +{ + char buf[64], *devname; + int ifindex, iflink, len; + FILE *f; + + snprintf(buf, sizeof(buf), "/sys/class/net/%s/iflink", dev->ifname); + f = fopen(buf, "r"); + if (!f) + return NULL; + + len = fread(buf, 1, sizeof(buf) - 1, f); + fclose(f); + + if (len <= 0) + return NULL; + + buf[len] = 0; + iflink = strtoul(buf, NULL, 0); + ifindex = system_if_resolve(dev); + if (!iflink || iflink == ifindex) + return NULL; + + devname = if_indextoname(iflink, buf); + if (!devname) + return NULL; + + return device_get(devname, true); +} + +static bool +read_string_file(int dir_fd, const char *file, char *buf, int len) +{ + bool ret = false; + char *c; + int fd; + + fd = openat(dir_fd, file, O_RDONLY); + if (fd < 0) + return false; + +retry: + len = read(fd, buf, len - 1); + if (len < 0) { + if (errno == EINTR) + goto retry; + } else if (len > 0) { + buf[len] = 0; + + c = strchr(buf, '\n'); + if (c) + *c = 0; + + ret = true; + } + + close(fd); + + return ret; +} + +static bool +read_int_file(int dir_fd, const char *file, int *val) +{ + char buf[64]; + bool ret = false; + + ret = read_string_file(dir_fd, file, buf, sizeof(buf)); + if (ret) + *val = strtoul(buf, NULL, 0); + + return ret; +} + +int +system_if_dump_info(struct device *dev, struct blob_buf *b) +{ + char buf[64]; + int dir_fd, val = 0; + + snprintf(buf, sizeof(buf), "/sys/class/net/%s", dev->ifname); + dir_fd = open(buf, O_DIRECTORY); + + if (read_int_file(dir_fd, "carrier", &val)) + blobmsg_add_u8(b, "link", !!val); + if (read_string_file(dir_fd, "address", buf, sizeof(buf))) + blobmsg_add_string(b, "macaddr", buf); + + close(dir_fd); + return 0; +} + +int +system_if_dump_stats(struct device *dev, struct blob_buf *b) { const char *const counters[] = { "collisions", "rx_frame_errors", "tx_compressed", @@ -527,30 +716,16 @@ int system_if_dump_stats(struct device *dev, struct blob_buf *b) }; char buf[64]; int stats_dir; - int i, fd, len; + int i, val = 0; snprintf(buf, sizeof(buf), "/sys/class/net/%s/statistics", dev->ifname); stats_dir = open(buf, O_DIRECTORY); if (stats_dir < 0) return -1; - for (i = 0; i < ARRAY_SIZE(counters); i++) { - fd = openat(stats_dir, counters[i], O_RDONLY); - if (fd < 0) - continue; - -retry: - len = read(fd, buf, sizeof(buf)); - if (len < 0) { - if (errno == EINTR) - goto retry; - continue; - } - - buf[len] = 0; - blobmsg_add_u32(b, counters[i], strtoul(buf, NULL, 0)); - close(fd); - } + for (i = 0; i < ARRAY_SIZE(counters); i++) + if (read_int_file(stats_dir, counters[i], &val)) + blobmsg_add_u32(b, counters[i], val); close(stats_dir); return 0;