struct event_socket {
struct uloop_fd uloop;
struct nl_sock *sock;
+ int bufsize;
};
static int sock_ioctl = -1;
handler_nl_event(struct uloop_fd *u, unsigned int events)
{
struct event_socket *ev = container_of(u, struct event_socket, uloop);
- nl_recvmsgs_default(ev->sock);
+ int err;
+ socklen_t errlen = sizeof(err);
+
+ if (!u->error) {
+ nl_recvmsgs_default(ev->sock);
+ return;
+ }
+
+ if (getsockopt(u->fd, SOL_SOCKET, SO_ERROR, (void *)&err, &errlen))
+ goto abort;
+
+ switch(err) {
+ case ENOBUFS:
+ // Increase rx buffer size on netlink socket
+ ev->bufsize *= 2;
+ if (nl_socket_set_buffer_size(ev->sock, ev->bufsize, 0))
+ goto abort;
+
+ // Request full dump since some info got dropped
+ struct rtgenmsg msg = { .rtgen_family = AF_UNSPEC };
+ nl_send_simple(ev->sock, RTM_GETLINK, NLM_F_DUMP, &msg, sizeof(msg));
+ break;
+
+ default:
+ goto abort;
+ }
+ u->error = false;
+ return;
+
+abort:
+ uloop_fd_delete(&ev->uloop);
+ return;
}
static struct nl_sock *
static bool
create_raw_event_socket(struct event_socket *ev, int protocol, int groups,
- uloop_fd_handler cb)
+ uloop_fd_handler cb, int flags)
{
ev->sock = create_socket(protocol, groups);
if (!ev->sock)
ev->uloop.fd = nl_socket_get_fd(ev->sock);
ev->uloop.cb = cb;
- if (uloop_fd_add(&ev->uloop, ULOOP_READ))
+ if (uloop_fd_add(&ev->uloop, ULOOP_READ|flags))
return false;
return true;
create_event_socket(struct event_socket *ev, int protocol,
int (*cb)(struct nl_msg *msg, void *arg))
{
- if (!create_raw_event_socket(ev, protocol, 0, handler_nl_event))
+ if (!create_raw_event_socket(ev, protocol, 0, handler_nl_event, ULOOP_ERROR_CB))
return false;
// Install the valid custom callback handler
nl_socket_disable_seq_check(ev->sock);
// Increase rx buffer size to 65K on event sockets
- if (nl_socket_set_buffer_size(ev->sock, 65535, 0))
+ ev->bufsize = 65535;
+ if (nl_socket_set_buffer_size(ev->sock, ev->bufsize, 0))
return false;
return true;
return -1;
if (!create_raw_event_socket(&hotplug_event, NETLINK_KOBJECT_UEVENT, 1,
- handle_hotplug_event))
+ handle_hotplug_event, 0))
return -1;
// Receive network link events form kernel
if (fd < 0)
return;
- write(fd, val, strlen(val));
+ if (write(fd, val, strlen(val))) {}
close(fd);
}
hdr->nlmsg_type = type;
hdr->nlmsg_flags = NLM_F_REQUEST;
- if (!nl_send_auto_complete(sock_rtnl, clr->msg))
- nl_wait_for_ack(sock_rtnl);
+ nl_socket_disable_auto_ack(sock_rtnl);
+ nl_send_auto_complete(sock_rtnl, clr->msg);
+ nl_socket_enable_auto_ack(sock_rtnl);
return NL_SKIP;
}
struct rtmsg rtm = {
.rtm_family = (alen == 4) ? AF_INET : AF_INET6,
.rtm_dst_len = route->mask,
+ .rtm_src_len = route->sourcemask,
.rtm_table = (table < 256) ? table : RT_TABLE_UNSPEC,
.rtm_protocol = (route->flags & DEVADDR_KERNEL) ? RTPROT_KERNEL : RTPROT_STATIC,
.rtm_scope = scope,
if (route->mask)
nla_put(msg, RTA_DST, alen, &route->addr);
+ if (route->sourcemask)
+ nla_put(msg, RTA_SRC, alen, &route->source);
+
if (route->metric > 0)
nla_put_u32(msg, RTA_PRIORITY, route->metric);
if (fd < 0)
continue;
- write(fd, "-1", 2);
+ if (write(fd, "-1", 2)) {}
close(fd);
}
return 0;