Ejemplo n.º 1
0
static void test_address_get(sd_rtnl *rtnl, int ifindex) {
        sd_rtnl_message *m;
        sd_rtnl_message *r;
        struct in_addr in_data;

        assert_se(sd_rtnl_message_new_addr(rtnl, &m, RTM_GETADDR, ifindex, AF_INET) >= 0);
        assert_se(m);

        assert_se(sd_rtnl_call(rtnl, m, -1, &r) == 1);

        assert_se(sd_rtnl_message_read_in_addr(r, IFA_LOCAL, &in_data) == 0);
        assert_se(sd_rtnl_message_read_in_addr(r, IFA_ADDRESS, &in_data) == 0);
        assert_se(sd_rtnl_message_read_in_addr(r, IFA_LABEL, &in_data) == 0);
        assert_se(sd_rtnl_message_read_in_addr(r, IFA_CACHEINFO, &in_data) == 0);

        assert_se(sd_rtnl_flush(rtnl) >= 0);
        assert_se((m = sd_rtnl_message_unref(m)) == NULL);
        assert_se((r = sd_rtnl_message_unref(r)) == NULL);

}
Ejemplo n.º 2
0
static void test_route(void) {
        _cleanup_rtnl_message_unref_ sd_rtnl_message *req;
        struct in_addr addr, addr_data;
        uint32_t index = 2, u32_data;
        int r;

        r = sd_rtnl_message_new_route(NULL, &req, RTM_NEWROUTE, AF_INET, RTPROT_STATIC);
        if (r < 0) {
                log_error_errno(r, "Could not create RTM_NEWROUTE message: %m");
                return;
        }

        addr.s_addr = htonl(INADDR_LOOPBACK);

        r = sd_rtnl_message_append_in_addr(req, RTA_GATEWAY, &addr);
        if (r < 0) {
                log_error_errno(r, "Could not append RTA_GATEWAY attribute: %m");
                return;
        }

        r = sd_rtnl_message_append_u32(req, RTA_OIF, index);
        if (r < 0) {
                log_error_errno(r, "Could not append RTA_OIF attribute: %m");
                return;
        }

        assert_se(sd_rtnl_message_rewind(req) >= 0);

        assert_se(sd_rtnl_message_read_in_addr(req, RTA_GATEWAY, &addr_data) >= 0);
        assert_se(addr_data.s_addr == addr.s_addr);

        assert_se(sd_rtnl_message_read_u32(req, RTA_OIF, &u32_data) >= 0);
        assert_se(u32_data == index);

        assert_se((req = sd_rtnl_message_unref(req)) == NULL);
}
int local_addresses(sd_rtnl *context, int ifindex, int af, struct local_address **ret) {
        _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL, *reply = NULL;
        _cleanup_rtnl_unref_ sd_rtnl *rtnl = NULL;
        _cleanup_free_ struct local_address *list = NULL;
        size_t n_list = 0, n_allocated = 0;
        sd_rtnl_message *m;
        int r;

        assert(ret);

        if (context)
                rtnl = sd_rtnl_ref(context);
        else {
                r = sd_rtnl_open(&rtnl, 0);
                if (r < 0)
                        return r;
        }

        r = sd_rtnl_message_new_addr(rtnl, &req, RTM_GETADDR, 0, af);
        if (r < 0)
                return r;

        r = sd_rtnl_call(rtnl, req, 0, &reply);
        if (r < 0)
                return r;

        for (m = reply; m; m = sd_rtnl_message_next(m)) {
                struct local_address *a;
                unsigned char flags;
                uint16_t type;
                int ifi, family;

                r = sd_rtnl_message_get_errno(m);
                if (r < 0)
                        return r;

                r = sd_rtnl_message_get_type(m, &type);
                if (r < 0)
                        return r;
                if (type != RTM_NEWADDR)
                        continue;

                r = sd_rtnl_message_addr_get_ifindex(m, &ifi);
                if (r < 0)
                        return r;
                if (ifindex > 0 && ifi != ifindex)
                        continue;

                r = sd_rtnl_message_addr_get_family(m, &family);
                if (r < 0)
                        return r;
                if (af != AF_UNSPEC && af != family)
                        continue;

                r = sd_rtnl_message_addr_get_flags(m, &flags);
                if (r < 0)
                        return r;
                if (flags & IFA_F_DEPRECATED)
                        continue;

                if (!GREEDY_REALLOC0(list, n_allocated, n_list+1))
                        return -ENOMEM;

                a = list + n_list;

                r = sd_rtnl_message_addr_get_scope(m, &a->scope);
                if (r < 0)
                        return r;

                if (ifindex == 0 && (a->scope == RT_SCOPE_HOST || a->scope == RT_SCOPE_NOWHERE))
                        continue;

                switch (family) {

                case AF_INET:
                        r = sd_rtnl_message_read_in_addr(m, IFA_LOCAL, &a->address.in);
                        if (r < 0) {
                                r = sd_rtnl_message_read_in_addr(m, IFA_ADDRESS, &a->address.in);
                                if (r < 0)
                                        continue;
                        }
                        break;

                case AF_INET6:
                        r = sd_rtnl_message_read_in6_addr(m, IFA_LOCAL, &a->address.in6);
                        if (r < 0) {
                                r = sd_rtnl_message_read_in6_addr(m, IFA_ADDRESS, &a->address.in6);
                                if (r < 0)
                                        continue;
                        }
                        break;

                default:
                        continue;
                }

                a->ifindex = ifi;
                a->family = family;

                n_list++;
        };

        if (n_list > 0)
                qsort(list, n_list, sizeof(struct local_address), address_compare);

        *ret = list;
        list = NULL;

        return (int) n_list;
}
static int get_gateway_description(
                sd_rtnl *rtnl,
                sd_hwdb *hwdb,
                int ifindex,
                int family,
                union in_addr_union *gateway,
                char **gateway_description) {
        _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL, *reply = NULL;
        sd_rtnl_message *m;
        int r;

        assert(rtnl);
        assert(ifindex >= 0);
        assert(family == AF_INET || family == AF_INET6);
        assert(gateway);
        assert(gateway_description);

        r = sd_rtnl_message_new_neigh(rtnl, &req, RTM_GETNEIGH, ifindex, family);
        if (r < 0)
                return r;

        r = sd_rtnl_message_request_dump(req, true);
        if (r < 0)
                return r;

        r = sd_rtnl_call(rtnl, req, 0, &reply);
        if (r < 0)
                return r;

        for (m = reply; m; m = sd_rtnl_message_next(m)) {
                union in_addr_union gw = {};
                struct ether_addr mac = {};
                uint16_t type;
                int ifi, fam;

                r = sd_rtnl_message_get_errno(m);
                if (r < 0) {
                        log_error_errno(r, "got error: %m");
                        continue;
                }

                r = sd_rtnl_message_get_type(m, &type);
                if (r < 0) {
                        log_error_errno(r, "could not get type: %m");
                        continue;
                }

                if (type != RTM_NEWNEIGH) {
                        log_error("type is not RTM_NEWNEIGH");
                        continue;
                }

                r = sd_rtnl_message_neigh_get_family(m, &fam);
                if (r < 0) {
                        log_error_errno(r, "could not get family: %m");
                        continue;
                }

                if (fam != family) {
                        log_error("family is not correct");
                        continue;
                }

                r = sd_rtnl_message_neigh_get_ifindex(m, &ifi);
                if (r < 0) {
                        log_error_errno(r, "could not get ifindex: %m");
                        continue;
                }

                if (ifindex > 0 && ifi != ifindex)
                        continue;

                switch (fam) {
                case AF_INET:
                        r = sd_rtnl_message_read_in_addr(m, NDA_DST, &gw.in);
                        if (r < 0)
                                continue;

                        break;
                case AF_INET6:
                        r = sd_rtnl_message_read_in6_addr(m, NDA_DST, &gw.in6);
                        if (r < 0)
                                continue;

                        break;
                default:
                        continue;
                }

                if (!in_addr_equal(fam, &gw, gateway))
                        continue;

                r = sd_rtnl_message_read_ether_addr(m, NDA_LLADDR, &mac);
                if (r < 0)
                        continue;

                r = ieee_oui(hwdb, &mac, gateway_description);
                if (r < 0)
                        continue;

                return 0;
        }

        return -ENODATA;
}
int local_gateways(sd_rtnl *context, int ifindex, int af, struct local_address **ret) {
        _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL, *reply = NULL;
        _cleanup_rtnl_unref_ sd_rtnl *rtnl = NULL;
        _cleanup_free_ struct local_address *list = NULL;
        sd_rtnl_message *m = NULL;
        size_t n_list = 0, n_allocated = 0;
        int r;

        assert(ret);

        if (context)
                rtnl = sd_rtnl_ref(context);
        else {
                r = sd_rtnl_open(&rtnl, 0);
                if (r < 0)
                        return r;
        }

        r = sd_rtnl_message_new_route(rtnl, &req, RTM_GETROUTE, af, RTPROT_UNSPEC);
        if (r < 0)
                return r;

        r = sd_rtnl_message_request_dump(req, true);
        if (r < 0)
                return r;

        r = sd_rtnl_call(rtnl, req, 0, &reply);
        if (r < 0)
                return r;

        for (m = reply; m; m = sd_rtnl_message_next(m)) {
                struct local_address *a;
                uint16_t type;
                unsigned char dst_len, src_len;
                uint32_t ifi;
                int family;

                r = sd_rtnl_message_get_errno(m);
                if (r < 0)
                        return r;

                r = sd_rtnl_message_get_type(m, &type);
                if (r < 0)
                        return r;
                if (type != RTM_NEWROUTE)
                        continue;

                /* We only care for default routes */
                r = sd_rtnl_message_route_get_dst_prefixlen(m, &dst_len);
                if (r < 0)
                        return r;
                if (dst_len != 0)
                        continue;

                r = sd_rtnl_message_route_get_src_prefixlen(m, &src_len);
                if (r < 0)
                        return r;
                if (src_len != 0)
                        continue;

                r = sd_rtnl_message_read_u32(m, RTA_OIF, &ifi);
                if (r < 0)
                        return r;
                if (ifindex > 0 && (int) ifi != ifindex)
                        continue;

                r = sd_rtnl_message_route_get_family(m, &family);
                if (r < 0)
                        return r;
                if (af != AF_UNSPEC && af != family)
                        continue;

                if (!GREEDY_REALLOC0(list, n_allocated, n_list + 1))
                        return -ENOMEM;

                a = list + n_list;

                switch (family) {
                case AF_INET:
                        r = sd_rtnl_message_read_in_addr(m, RTA_GATEWAY, &a->address.in);
                        if (r < 0)
                                continue;

                        break;
                case AF_INET6:
                        r = sd_rtnl_message_read_in6_addr(m, RTA_GATEWAY, &a->address.in6);
                        if (r < 0)
                                continue;

                        break;
                default:
                        continue;
                }

                sd_rtnl_message_read_u32(m, RTA_PRIORITY, &a->metric);

                a->ifindex = ifi;
                a->family = family;

                n_list++;
        }

        if (n_list > 0)
                qsort(list, n_list, sizeof(struct local_address), address_compare);

        *ret = list;
        list = NULL;

        return (int) n_list;
}