Branch Coverage

libwireguard.c
Criterion Covered Total %
branch 113 458 24.6


line true false branch
135 5 2 if (size)
138 0 2 if (size > 8192)
179 5 0 return len >= (int)sizeof(struct nlmsghdr) &&
180 5 1 nlh->nlmsg_len >= sizeof(struct nlmsghdr) &&
5 0 nlh->nlmsg_len >= sizeof(struct nlmsghdr) &&
197 5 0 return nlh->nlmsg_seq && seq ? nlh->nlmsg_seq == seq : true;
5 0 return nlh->nlmsg_seq && seq ? nlh->nlmsg_seq == seq : true;
5 0 return nlh->nlmsg_seq && seq ? nlh->nlmsg_seq == seq : true;
202 5 0 return nlh->nlmsg_pid && portid ? nlh->nlmsg_pid == portid : true;
5 0 return nlh->nlmsg_pid && portid ? nlh->nlmsg_pid == portid : true;
5 0 return nlh->nlmsg_pid && portid ? nlh->nlmsg_pid == portid : true;
222 56 0 return len >= (int)sizeof(struct nlattr) &&
223 56 3 attr->nla_len >= sizeof(struct nlattr) &&
56 0 attr->nla_len >= sizeof(struct nlattr) &&
234 0 0 if (mnl_attr_get_type(attr) > max) {
247 0 0 if (attr_len < exp_len) {
253 0 0 if (attr_len > 0) {
259 0 0 if (attr_len == 0) {
263 0 0 if (attr_data[attr_len-1] != '\0') {
269 0 0 if (attr_len == 0) {
276 0 0 if (attr_len == 0)
279 0 0 if (attr_len < MNL_ATTR_HDRLEN) {
288 0 0 if (exp_len && attr_len > exp_len) {
0 0 if (exp_len && attr_len > exp_len) {
307 0 0 if (type >= MNL_TYPE_MAX) {
321 55 2 mnl_attr_for_each(attr, nlh, offset)
322 0 55 if ((ret = cb(attr, data)) <= MNL_CB_STOP)
333 1 1 mnl_attr_for_each_nested(attr, nested)
334 0 1 if ((ret = cb(attr, data)) <= MNL_CB_STOP)
378 4 0 if (pad > 0)
409 0 0 if (nlh->nlmsg_len + MNL_ATTR_HDRLEN + MNL_ALIGN(len) > buflen)
436 0 0 if (nlh->nlmsg_len + MNL_ATTR_HDRLEN > buflen)
460 0 2 if (nlh->nlmsg_len < mnl_nlmsg_size(sizeof(struct nlmsgerr))) {
465 2 0 if (err->error < 0)
470 0 2 return err->error == 0 ? MNL_CB_STOP : MNL_CB_ERROR;
494 5 1 while (mnl_nlmsg_ok(nlh, len)) {
496 0 5 if (!mnl_nlmsg_portid_ok(nlh, portid)) {
501 0 5 if (!mnl_nlmsg_seq_ok(nlh, seq)) {
506 0 5 if (nlh->nlmsg_flags & NLM_F_DUMP_INTR) {
511 2 3 if (nlh->nlmsg_type >= NLMSG_MIN_TYPE) {
512 2 0 if (cb_data){
514 0 2 if (ret <= MNL_CB_STOP)
517 0 3 } else if (nlh->nlmsg_type < cb_ctl_array_len) {
518 0 0 if (cb_ctl_array && cb_ctl_array[nlh->nlmsg_type]) {
0 0 if (cb_ctl_array && cb_ctl_array[nlh->nlmsg_type]) {
520 0 0 if (ret <= MNL_CB_STOP)
523 3 0 } else if (default_cb_array[nlh->nlmsg_type]) {
525 3 0 if (ret <= MNL_CB_STOP)
563 0 3 if (nl == NULL)
567 0 3 if (nl->fd == -1) {
590 0 3 if (ret < 0)
595 0 3 if (ret < 0)
598 0 3 if (addr_len != sizeof(nl->addr)) {
602 0 3 if (nl->addr.nl_family != AF_NETLINK) {
638 0 4 if (ret == -1)
641 0 4 if (msg.msg_flags & MSG_TRUNC) {
645 0 4 if (msg.msg_namelen != sizeof(struct sockaddr_nl)) {
713 0 0 if (nlh->nlmsg_len < mnl_nlmsg_size(sizeof(struct nlmsgerr))) {
718 0 0 if (err->error < 0)
723 0 0 return err->error == 0 ? MNL_CB_STOP : MNL_CB_ERROR;
729 0 0 if (nlh->nlmsg_flags & NLM_F_MULTI && nlh->nlmsg_len == mnl_nlmsg_size(sizeof(int))) {
0 0 if (nlh->nlmsg_flags & NLM_F_MULTI && nlh->nlmsg_len == mnl_nlmsg_size(sizeof(int))) {
732 0 0 if (error < 0)
737 0 0 return error == 0 ? MNL_CB_STOP : MNL_CB_ERROR;
756 0 0 if (err <= 0)
760 0 0 } while (err > 0);
770 0 0 if (mnl_attr_type_valid(attr, CTRL_ATTR_MAX) < 0)
786 0 0 if (!tb[CTRL_ATTR_FAMILY_ID])
799 0 0 if (!nlg)
805 0 0 if (!nlg->buf)
809 0 0 if (!nlg->nl) {
814 0 0 if (mnl_socket_bind(nlg->nl, 0, MNL_SOCKET_AUTOPID) < 0) {
825 0 0 if (mnlg_socket_send(nlg, nlh) < 0) {
831 0 0 if (mnlg_socket_recv_run(nlg, get_family_id_cb, &nlg->id) < 0) {
832 0 0 errno = errno == ENOENT ? EPROTONOSUPPORT : errno;
833 0 0 err = errno ? -errno : -ENOSYS;
872 0 0 if (len == 1)
875 0 0 if (len >= list->cap - list->len) {
879 0 0 if (new_cap < list->len +len + 1)
882 0 0 if (!new_buffer)
902 1 0 if (mnl_attr_get_type(attr) == IFLA_INFO_KIND && !strcmp(WG_GENL_NAME, mnl_attr_get_str(attr)))
0 1 if (mnl_attr_get_type(attr) == IFLA_INFO_KIND && !strcmp(WG_GENL_NAME, mnl_attr_get_str(attr)))
911 1 54 if (mnl_attr_get_type(attr) == IFLA_LINKINFO)
913 2 52 else if (mnl_attr_get_type(attr) == IFLA_IFNAME)
925 0 2 if (ret != MNL_CB_OK)
927 2 0 if (interface.name && interface.is_wireguard)
0 2 if (interface.name && interface.is_wireguard)
929 0 2 if (ret < 0)
931 2 0 if (nlh->nlmsg_type != NLMSG_DONE)
949 0 1 if (!rtnl_buffer)
953 0 1 if (!nl) {
958 0 1 if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
973 0 1 if (mnl_socket_sendto(nl, rtnl_buffer, message_len) < 0) {
979 0 2 if ((len = mnl_socket_recvfrom(nl, rtnl_buffer, mnl_ideal_socket_buffer_size())) < 0) {
983 0 2 if ((len = mnl_cb_run(rtnl_buffer, len, seq, portid, read_devices_cb, list)) < 0) {
989 0 0 if (errno != EINTR) {
994 1 1 if (len == MNL_CB_OK + 1)
1000 1 0 if (nl)
1016 0 2 if (!rtnl_buffer) {
1022 0 2 if (!nl) {
1027 0 2 if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
1033 2 0 nlh->nlmsg_type = add ? RTM_NEWLINK : RTM_DELLINK;
1034 2 0 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | (add ? NLM_F_CREATE | NLM_F_EXCL : 0);
1043 0 2 if (mnl_socket_sendto(nl, rtnl_buffer, nlh->nlmsg_len) < 0) {
1047 0 2 if ((len = mnl_socket_recvfrom(nl, rtnl_buffer, mnl_ideal_socket_buffer_size())) < 0) {
1051 2 0 if (mnl_cb_run(rtnl_buffer, len, nlh->nlmsg_seq, mnl_socket_get_portid(nl), NULL, NULL) < 0) {
1059 2 0 if (nl)
1074 0 0 if (!nlg)
1081 0 0 if (!peer) {
1084 0 0 if (dev->flags & WGDEVICE_HAS_PRIVATE_KEY)
1086 0 0 if (dev->flags & WGDEVICE_HAS_LISTEN_PORT)
1088 0 0 if (dev->flags & WGDEVICE_HAS_FWMARK)
1090 0 0 if (dev->flags & WGDEVICE_REPLACE_PEERS)
1092 0 0 if (flags)
1095 0 0 if (!dev->first_peer)
1099 0 0 for (peer = peer ? peer : dev->first_peer; peer; peer = peer->next_peer) {
0 0 for (peer = peer ? peer : dev->first_peer; peer; peer = peer->next_peer) {
1103 0 0 if (!peer_nest)
1105 0 0 if (!mnl_attr_put_check(nlh, mnl_ideal_socket_buffer_size(), WGPEER_A_PUBLIC_KEY, sizeof(peer->public_key), peer->public_key))
1107 0 0 if (peer->flags & WGPEER_REMOVE_ME)
1109 0 0 if (!allowedip) {
1110 0 0 if (peer->flags & WGPEER_REPLACE_ALLOWEDIPS)
1112 0 0 if (peer->flags & WGPEER_HAS_PRESHARED_KEY) {
1113 0 0 if (!mnl_attr_put_check(nlh, mnl_ideal_socket_buffer_size(), WGPEER_A_PRESHARED_KEY, sizeof(peer->preshared_key), peer->preshared_key))
1116 0 0 if (peer->endpoint.addr.sa_family == AF_INET) {
1117 0 0 if (!mnl_attr_put_check(nlh, mnl_ideal_socket_buffer_size(), WGPEER_A_ENDPOINT, sizeof(peer->endpoint.addr4), &peer->endpoint.addr4))
1119 0 0 } else if (peer->endpoint.addr.sa_family == AF_INET6) {
1120 0 0 if (!mnl_attr_put_check(nlh, mnl_ideal_socket_buffer_size(), WGPEER_A_ENDPOINT, sizeof(peer->endpoint.addr6), &peer->endpoint.addr6))
1123 0 0 if (peer->flags & WGPEER_HAS_PERSISTENT_KEEPALIVE_INTERVAL) {
1124 0 0 if (!mnl_attr_put_u16_check(nlh, mnl_ideal_socket_buffer_size(), WGPEER_A_PERSISTENT_KEEPALIVE_INTERVAL, peer->persistent_keepalive_interval))
1128 0 0 if (flags) {
1129 0 0 if (!mnl_attr_put_u32_check(nlh, mnl_ideal_socket_buffer_size(), WGPEER_A_FLAGS, flags))
1132 0 0 if (peer->first_allowedip) {
1133 0 0 if (!allowedip)
1136 0 0 if (!allowedips_nest)
1138 0 0 for (; allowedip; allowedip = allowedip->next_allowedip) {
1140 0 0 if (!allowedip_nest)
1142 0 0 if (!mnl_attr_put_u16_check(nlh, mnl_ideal_socket_buffer_size(), WGALLOWEDIP_A_FAMILY, allowedip->family))
1144 0 0 if (allowedip->family == AF_INET) {
1145 0 0 if (!mnl_attr_put_check(nlh, mnl_ideal_socket_buffer_size(), WGALLOWEDIP_A_IPADDR, sizeof(allowedip->ip4), &allowedip->ip4))
1147 0 0 } else if (allowedip->family == AF_INET6) {
1148 0 0 if (!mnl_attr_put_check(nlh, mnl_ideal_socket_buffer_size(), WGALLOWEDIP_A_IPADDR, sizeof(allowedip->ip6), &allowedip->ip6))
1151 0 0 if (!mnl_attr_put_u8_check(nlh, mnl_ideal_socket_buffer_size(), WGALLOWEDIP_A_CIDR_MASK, allowedip->cidr))
1167 0 0 if (allowedip_nest)
1169 0 0 if (allowedips_nest)
1175 0 0 if (peer_nest)
1180 0 0 if (mnlg_socket_send(nlg, nlh) < 0) {
1185 0 0 if (mnlg_socket_recv_run(nlg, NULL, NULL) < 0) {
1186 0 0 ret = errno ? -errno : -EINVAL;
1189 0 0 if (peer)
1206 0 0 if (!mnl_attr_validate(attr, MNL_TYPE_U16))
1210 0 0 if (mnl_attr_get_payload_len(attr) == sizeof(allowedip->ip4))
1212 0 0 else if (mnl_attr_get_payload_len(attr) == sizeof(allowedip->ip6))
1216 0 0 if (!mnl_attr_validate(attr, MNL_TYPE_U8))
1230 0 0 if (!new_allowedip)
1232 0 0 if (!peer->first_allowedip)
1239 0 0 if (!ret)
1241 0 0 if (!((new_allowedip->family == AF_INET && new_allowedip->cidr <= 32) || (new_allowedip->family == AF_INET6 && new_allowedip->cidr <= 128))) {
0 0 if (!((new_allowedip->family == AF_INET && new_allowedip->cidr <= 32) || (new_allowedip->family == AF_INET6 && new_allowedip->cidr <= 128))) {
0 0 if (!((new_allowedip->family == AF_INET && new_allowedip->cidr <= 32) || (new_allowedip->family == AF_INET6 && new_allowedip->cidr <= 128))) {
0 0 if (!((new_allowedip->family == AF_INET && new_allowedip->cidr <= 32) || (new_allowedip->family == AF_INET6 && new_allowedip->cidr <= 128))) {
1253 0 0 for (i = 0; i < sizeof(wg_key); ++i) {
1268 0 0 if (mnl_attr_get_payload_len(attr) == sizeof(peer->public_key)) {
1274 0 0 if (mnl_attr_get_payload_len(attr) == sizeof(peer->preshared_key)) {
1276 0 0 if (!wg_key_is_zero(peer->preshared_key))
1283 0 0 if (mnl_attr_get_payload_len(attr) < sizeof(*addr))
1286 0 0 if (addr->sa_family == AF_INET && mnl_attr_get_payload_len(attr) == sizeof(peer->endpoint.addr4))
0 0 if (addr->sa_family == AF_INET && mnl_attr_get_payload_len(attr) == sizeof(peer->endpoint.addr4))
1288 0 0 else if (addr->sa_family == AF_INET6 && mnl_attr_get_payload_len(attr) == sizeof(peer->endpoint.addr6))
0 0 else if (addr->sa_family == AF_INET6 && mnl_attr_get_payload_len(attr) == sizeof(peer->endpoint.addr6))
1293 0 0 if (!mnl_attr_validate(attr, MNL_TYPE_U16))
1297 0 0 if (mnl_attr_get_payload_len(attr) == sizeof(peer->last_handshake_time))
1301 0 0 if (!mnl_attr_validate(attr, MNL_TYPE_U64))
1305 0 0 if (!mnl_attr_validate(attr, MNL_TYPE_U64))
1321 0 0 if (!new_peer)
1323 0 0 if (!device->first_peer)
1330 0 0 if (!ret)
1332 0 0 if (!(new_peer->flags & WGPEER_HAS_PUBLIC_KEY)) {
1347 0 0 if (!mnl_attr_validate(attr, MNL_TYPE_U32))
1351 0 0 if (!mnl_attr_validate(attr, MNL_TYPE_STRING)) {
1357 0 0 if (mnl_attr_get_payload_len(attr) == sizeof(device->private_key)) {
1363 0 0 if (mnl_attr_get_payload_len(attr) == sizeof(device->public_key)) {
1369 0 0 if (!mnl_attr_validate(attr, MNL_TYPE_U16))
1373 0 0 if (!mnl_attr_validate(attr, MNL_TYPE_U32))
1392 0 0 while (peer && peer->next_peer) {
0 0 while (peer && peer->next_peer) {
1393 0 0 if (memcmp(peer->public_key, peer->next_peer->public_key, sizeof(wg_key))) {
1397 0 0 if (!peer->first_allowedip) {
1418 0 0 if (!*device)
1422 0 0 if (!nlg) {
1430 0 0 if (mnlg_socket_send(nlg, nlh) < 0) {
1435 0 0 if (mnlg_socket_recv_run(nlg, read_device_cb, *device) < 0) {
1436 0 0 ret = errno ? -errno : -EINVAL;
1442 0 0 if (nlg)
1444 0 0 if (ret) {
1446 0 0 if (ret == -EINTR)
1461 0 1 if (errno) {
1465 0 1 return list.buffer ?: strdup("\0");
1483 0 0 if (!dev)
1485 0 0 for (peer = dev->first_peer, np = peer ? peer->next_peer : NULL; peer; peer = np, np = peer ? peer->next_peer : NULL) {
0 0 for (peer = dev->first_peer, np = peer ? peer->next_peer : NULL; peer; peer = np, np = peer ? peer->next_peer : NULL) {
0 0 for (peer = dev->first_peer, np = peer ? peer->next_peer : NULL; peer; peer = np, np = peer ? peer->next_peer : NULL) {
1486 0 0 for (allowedip = peer->first_allowedip, na = allowedip ? allowedip->next_allowedip : NULL; allowedip; allowedip = na, na = allowedip ? allowedip->next_allowedip : NULL)
0 0 for (allowedip = peer->first_allowedip, na = allowedip ? allowedip->next_allowedip : NULL; allowedip; allowedip = na, na = allowedip ? allowedip->next_allowedip : NULL)
0 0 for (allowedip = peer->first_allowedip, na = allowedip ? allowedip->next_allowedip : NULL; allowedip; allowedip = na, na = allowedip ? allowedip->next_allowedip : NULL)
1498 0 0 for (i = 0; i < 4; ++i)
1511 0 0 for (i = 0; i < 32 / 3; ++i)
1523 0 0 for (i = 0; i < 4; ++i)
1540 0 0 if (strlen(base64) != sizeof(wg_key_b64_string) - 1 || base64[sizeof(wg_key_b64_string) - 2] != '=') {
0 0 if (strlen(base64) != sizeof(wg_key_b64_string) - 1 || base64[sizeof(wg_key_b64_string) - 2] != '=') {
1545 0 0 for (i = 0; i < 32 / 3; ++i) {
1573 195744 12234 for (i = 0; i < 16; ++i) {
1574 12234 183510 o[(i + 1) % 16] += (i == 15 ? 38 : 1) * (o[i] >> 16);
1584 32704 2044 for (i = 0; i < 16; ++i) {
1604 4 2 for (j = 0; j < 2; ++j) {
1606 56 4 for (i = 1; i < 15; ++i) {
1615 32 2 for (i = 0; i < 16; ++i) {
1629 32640 2040 for (i = 0; i < 16; ++i)
1637 32640 2040 for (i = 0; i < 16; ++i)
1646 97824 6114 for (i = 0; i < 16; ++i) {
1647 1565184 97824 for (j = 0; j < 16; ++j)
1650 91710 6114 for (i = 0; i < 15; ++i)
1665 508 2 for (a = 253; a >= 0; --a) {
1667 506 2 if (a != 2 && a != 4)
504 2 if (a != 2 && a != 4)
1690 510 2 for (i = 254; i >= 0; --i) {
1749 0 4 assert(fd >= 0);
1750 4 4 for (i = 0; i < sizeof(wg_key); i += ret) {
1752 0 4 assert(ret > 0);