static int dhcp6_address_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { _cleanup_link_unref_ Link *link = userdata; int r; assert(link); r = sd_netlink_message_get_errno(m); if (r < 0 && r != -EEXIST) { if (link->rtnl_extended_attrs) { log_link_warning(link, "Could not set extended netlink attributes, reverting to fallback mechanism"); link->rtnl_extended_attrs = false; dhcp6_lease_address_acquired(link->dhcp6_client, link); return 1; } log_link_error_errno(link, r, "Could not set DHCPv6 address: %m"); link_enter_failed(link); } else if (r >= 0) manager_rtnl_process_address(rtnl, m, link->manager); return 1; }
int link_routing_policy_rule_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { _cleanup_link_unref_ Link *link = userdata; int r; assert(rtnl); assert(m); assert(link); assert(link->ifname); assert(link->routing_policy_rule_messages > 0); link->routing_policy_rule_messages--; if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) return 1; r = sd_netlink_message_get_errno(m); if (r < 0 && r != -EEXIST) log_link_warning_errno(link, r, "Could not add routing policy rule: %m"); if (link->routing_policy_rule_messages == 0) { log_link_debug(link, "Routing policy rule configured"); link->routing_policy_rules_configured = true; link_check_ready(link); } return 1; }
static int set_brvlan_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) { int r; assert(link); r = sd_netlink_message_get_errno(m); if (r < 0 && r != -EEXIST) log_link_error_errno(link, r, "Could not add VLAN to bridge port: %m"); return 1; }
static int set_fdb_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { Link *link = userdata; int r; assert(link); r = sd_netlink_message_get_errno(m); if (r < 0 && r != -EEXIST) log_link_error_errno(link, r, "Could not add FDB entry: %m"); return 1; }
static int pipe_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { int *counter = userdata; int r; (*counter)--; r = sd_netlink_message_get_errno(m); log_info_errno(r, "%d left in pipe. got reply: %m", *counter); assert_se(r >= 0); return 1; }
/* callback for bridge netdev's parameter set */ static int netdev_bridge_set_handler(sd_netlink *rtnl, sd_netlink_message *m, NetDev *netdev) { int r; assert(netdev); assert(m); r = sd_netlink_message_get_errno(m); if (r < 0) { log_netdev_warning_errno(netdev, r, "Bridge parameters could not be set: %m"); return 1; } log_netdev_debug(netdev, "Bridge parameters set success"); return 1; }
/* callback for brige netdev's parameter set */ static int netdev_bridge_set_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { _cleanup_netdev_unref_ NetDev *netdev = userdata; int r; assert(netdev); assert(m); r = sd_netlink_message_get_errno(m); if (r < 0) { log_netdev_warning_errno(netdev, r, "Bridge parameters could not be set: %m"); return 1; } log_netdev_debug(netdev, "Bridge parametres set success"); return 1; }
static int dhcp4_address_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { _cleanup_link_unref_ Link *link = userdata; int r; assert(link); r = sd_netlink_message_get_errno(m); if (r < 0 && r != -EEXIST) { log_link_error_errno(link, r, "Could not set DHCPv4 address: %m"); link_enter_failed(link); } else if (r >= 0) manager_rtnl_process_address(rtnl, m, link->manager); link_set_dhcp_routes(link); return 1; }
static int route_expire_callback(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { Link *link = userdata; int r; assert(rtnl); assert(m); assert(link); assert(link->ifname); if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) return 1; r = sd_netlink_message_get_errno(m); if (r < 0 && r != -EEXIST) log_link_warning_errno(link, r, "could not remove route: %m"); return 1; }
static int routing_policy_rule_remove_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { _cleanup_link_unref_ Link *link = userdata; int r; assert(m); assert(link); assert(link->ifname); link->routing_policy_rule_remove_messages--; if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) return 1; r = sd_netlink_message_get_errno(m); if (r < 0) log_link_warning_errno(link, r, "Could not drop routing policy rule: %m"); return 1; }
/* callback for geneve netdev's created without a backing Link */ static int geneve_netdev_create_handler(sd_netlink *rtnl, sd_netlink_message *m, NetDev *netdev) { int r; assert(netdev); assert(netdev->state != _NETDEV_STATE_INVALID); r = sd_netlink_message_get_errno(m); if (r == -EEXIST) log_netdev_info(netdev, "Geneve netdev exists, using existing without changing its parameters"); else if (r < 0) { log_netdev_warning_errno(netdev, r, "Geneve netdev could not be created: %m"); netdev_drop(netdev); return 1; } log_netdev_debug(netdev, "Geneve created"); return 1; }
static int ipv4ll_route_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { _cleanup_link_unref_ Link *link = userdata; int r; assert(link); assert(!link->ipv4ll_route); r = sd_netlink_message_get_errno(m); if (r < 0 && r != -EEXIST) { log_link_error_errno(link, r, "could not set ipv4ll route: %m"); link_enter_failed(link); } link->ipv4ll_route = true; if (link->ipv4ll_address == true) link_check_ready(link); return 1; }
static int dhcp4_route_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) { int r; assert(link); assert(link->dhcp4_messages > 0); link->dhcp4_messages--; r = sd_netlink_message_get_errno(m); if (r < 0 && r != -EEXIST) { log_link_error_errno(link, r, "Could not set DHCPv4 route: %m"); link_enter_failed(link); } if (link->dhcp4_messages == 0) { link->dhcp4_configured = true; link_check_ready(link); } return 1; }
static int ipv4ll_address_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { _cleanup_link_unref_ Link *link = userdata; int r; assert(link); assert(!link->ipv4ll_address); r = sd_netlink_message_get_errno(m); if (r < 0 && r != -EEXIST) { log_link_error(link, "could not set ipv4ll address: %s", strerror(-r)); link_enter_failed(link); } else if (r >= 0) link_rtnl_process_address(rtnl, m, link->manager); link->ipv4ll_address = true; if (link->ipv4ll_route == true) link_client_handler(link); return 1; }
static int ndisc_netlink_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { _cleanup_link_unref_ Link *link = userdata; int r; assert(link); assert(link->ndisc_messages > 0); link->ndisc_messages--; r = sd_netlink_message_get_errno(m); if (r < 0 && r != -EEXIST) { log_link_error_errno(link, r, "Could not set NDisc route or address: %m"); link_enter_failed(link); } if (link->ndisc_messages == 0) { link->ndisc_configured = true; link_check_ready(link); } return 1; }
static int dhcp4_route_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { _cleanup_link_unref_ Link *link = userdata; int r; assert(link); assert(link->dhcp4_messages); link->dhcp4_messages --; r = sd_netlink_message_get_errno(m); if (r < 0 && r != -EEXIST) { log_link_error_errno(link, r, "Could not set DHCPv4 route: %m"); link_enter_failed(link); } if (!link->dhcp4_messages) { link->dhcp4_configured = true; link_check_ready(link); } return 1; }
static int dhcp4_address_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) { int r; assert(link); r = sd_netlink_message_get_errno(m); if (r < 0 && r != -EEXIST) { log_link_error_errno(link, r, "Could not set DHCPv4 address: %m"); link_enter_failed(link); } else if (r >= 0) manager_rtnl_process_address(rtnl, m, link->manager); link_set_dhcp_routes(link); /* Add back static routes since kernel removes while DHCPv4 address is removed from when lease expires */ link_request_set_routes(link); if (link->dhcp4_messages == 0) { link->dhcp4_configured = true; link_check_ready(link); } return 1; }
int local_addresses(sd_netlink *context, int ifindex, int af, struct local_address **ret) { _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL; _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; _cleanup_free_ struct local_address *list = NULL; size_t n_list = 0, n_allocated = 0; sd_netlink_message *m; int r; assert(ret); if (context) rtnl = sd_netlink_ref(context); else { r = sd_netlink_open(&rtnl); if (r < 0) return r; } r = sd_rtnl_message_new_addr(rtnl, &req, RTM_GETADDR, 0, af); if (r < 0) return r; r = sd_netlink_call(rtnl, req, 0, &reply); if (r < 0) return r; for (m = reply; m; m = sd_netlink_message_next(m)) { struct local_address *a; unsigned char flags; uint16_t type; int ifi, family; r = sd_netlink_message_get_errno(m); if (r < 0) return r; r = sd_netlink_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 && IN_SET(a->scope, RT_SCOPE_HOST, RT_SCOPE_NOWHERE)) continue; switch (family) { case AF_INET: r = sd_netlink_message_read_in_addr(m, IFA_LOCAL, &a->address.in); if (r < 0) { r = sd_netlink_message_read_in_addr(m, IFA_ADDRESS, &a->address.in); if (r < 0) continue; } break; case AF_INET6: r = sd_netlink_message_read_in6_addr(m, IFA_LOCAL, &a->address.in6); if (r < 0) { r = sd_netlink_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++; }; qsort_safe(list, n_list, sizeof(struct local_address), address_compare); *ret = TAKE_PTR(list); return (int) n_list; }
static void test_message(sd_netlink *rtnl) { _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL; assert_se(rtnl_message_new_synthetic_error(rtnl, -ETIMEDOUT, 1, &m) >= 0); assert_se(sd_netlink_message_get_errno(m) == -ETIMEDOUT); }
static int get_gateway_description( sd_netlink *rtnl, sd_hwdb *hwdb, int ifindex, int family, union in_addr_union *gateway, char **gateway_description) { _cleanup_netlink_message_unref_ sd_netlink_message *req = NULL, *reply = NULL; sd_netlink_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_netlink_message_request_dump(req, true); if (r < 0) return r; r = sd_netlink_call(rtnl, req, 0, &reply); if (r < 0) return r; for (m = reply; m; m = sd_netlink_message_next(m)) { union in_addr_union gw = {}; struct ether_addr mac = {}; uint16_t type; int ifi, fam; r = sd_netlink_message_get_errno(m); if (r < 0) { log_error_errno(r, "got error: %m"); continue; } r = sd_netlink_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_netlink_message_read_in_addr(m, NDA_DST, &gw.in); if (r < 0) continue; break; case AF_INET6: r = sd_netlink_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_netlink_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_netlink *context, int ifindex, int af, struct local_address **ret) { _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL; _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; _cleanup_free_ struct local_address *list = NULL; sd_netlink_message *m = NULL; size_t n_list = 0, n_allocated = 0; int r; assert(ret); if (context) rtnl = sd_netlink_ref(context); else { r = sd_netlink_open(&rtnl); 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_netlink_message_request_dump(req, true); if (r < 0) return r; r = sd_netlink_call(rtnl, req, 0, &reply); if (r < 0) return r; for (m = reply; m; m = sd_netlink_message_next(m)) { struct local_address *a; uint16_t type; unsigned char dst_len, src_len; uint32_t ifi; int family; r = sd_netlink_message_get_errno(m); if (r < 0) return r; r = sd_netlink_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_netlink_message_read_u32(m, RTA_OIF, &ifi); if (r == -ENODATA) /* Not all routes have an RTA_OIF attribute (for example nexthop ones) */ continue; 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_netlink_message_read_in_addr(m, RTA_GATEWAY, &a->address.in); if (r < 0) continue; break; case AF_INET6: r = sd_netlink_message_read_in6_addr(m, RTA_GATEWAY, &a->address.in6); if (r < 0) continue; break; default: continue; } sd_netlink_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 = TAKE_PTR(list); return (int) n_list; }