diff options
author | Alfredo Matos <alfredo.matos@caixamagica.pt> | 2011-07-29 11:38:46 +0100 |
---|---|---|
committer | Dan Williams <dcbw@redhat.com> | 2011-07-30 15:47:26 -0500 |
commit | a6c65568313183c5b56b36934e07a1d675d4217b (patch) | |
tree | 07154f62b1641334103a6abe4904abb3c4914477 | |
parent | 73ddc1e3072cfc7a9ff21dc719c8bb17a89203ce (diff) | |
download | NetworkManager-a6c65568313183c5b56b36934e07a1d675d4217b.tar.gz |
core: add libnl-2 support with libnl-1 compatibility
-rw-r--r-- | configure.ac | 30 | ||||
-rw-r--r-- | src/Makefile.am | 2 | ||||
-rw-r--r-- | src/backends/NetworkManagerGeneric.c | 6 | ||||
-rw-r--r-- | src/ip6-manager/nm-ip6-manager.c | 14 | ||||
-rw-r--r-- | src/nm-device.c | 6 | ||||
-rw-r--r-- | src/nm-netlink-compat.c | 92 | ||||
-rw-r--r-- | src/nm-netlink-compat.h | 162 | ||||
-rw-r--r-- | src/nm-netlink-monitor.c | 77 | ||||
-rw-r--r-- | src/nm-netlink-monitor.h | 4 | ||||
-rw-r--r-- | src/nm-netlink-utils.c | 21 | ||||
-rw-r--r-- | src/nm-system.c | 50 |
11 files changed, 387 insertions, 77 deletions
diff --git a/configure.ac b/configure.ac index 2a33ebd9cb..608765b616 100644 --- a/configure.ac +++ b/configure.ac @@ -312,10 +312,34 @@ else fi AC_DEFINE_UNQUOTED(NO_CONSOLEKIT, $no_ck, [Define to disable use of ConsoleKit]) -PKG_CHECK_MODULES(LIBNL, libnl-1 >= 1.0-pre8) +have_libnl="no" +PKG_CHECK_MODULES(LIBNL1, libnl-1 >= 1.0-pre8, [have_libnl1=yes], [have_libnl1=no]) +if (test "${have_libnl1}" = "yes"); then + AC_DEFINE(HAVE_LIBNL1, 1, [Define if you require libnl-1 legacy support]) + LIBNL_CFLAGS="$LIBNL1_CFLAGS" + LIBNL_LIBS="$LIBNL1_LIBS" + libnl_version="1" + have_libnl="yes" +fi + +PKG_CHECK_MODULES(LIBNL2, libnl-2.0, [have_libnl2=yes], [have_libnl2=no]) +if (test "${have_libnl2}" = "yes"); then + AC_DEFINE(HAVE_LIBNL2, 1, [Define if you require specific libnl-2 support]) + LIBNL_CFLAGS="$LIBNL2_CFLAGS" + LIBNL_LIBS="$LIBNL2_LIBS" + libnl_version="2" + have_libnl="yes" +fi + +if (test "${have_libnl}" = "no"); then + AC_MSG_ERROR([libnl development header are required]) +fi AC_SUBST(LIBNL_CFLAGS) AC_SUBST(LIBNL_LIBS) -NM_LIBNL_CHECK + +if (test "${libnl_version}" = "1"); then + NM_LIBNL_CHECK +fi PKG_CHECK_MODULES(UUID, uuid) AC_SUBST(UUID_CFLAGS) @@ -737,6 +761,8 @@ else echo PPP support: no fi +echo libnl version: ${libnl_version} + echo echo Building documentation: ${with_docs} echo Building tests: ${with_tests} diff --git a/src/Makefile.am b/src/Makefile.am index 11529a7b96..c8caefb0f1 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -161,6 +161,8 @@ NetworkManager_SOURCES = \ nm-netlink-monitor.h \ nm-netlink-utils.c \ nm-netlink-utils.h \ + nm-netlink-compat.h \ + nm-netlink-compat.c \ nm-activation-request.c \ nm-activation-request.h \ nm-properties-changed-signal.c \ diff --git a/src/backends/NetworkManagerGeneric.c b/src/backends/NetworkManagerGeneric.c index 90a41bba97..2ef8583c03 100644 --- a/src/backends/NetworkManagerGeneric.c +++ b/src/backends/NetworkManagerGeneric.c @@ -34,7 +34,9 @@ #include "nm-system.h" #include "NetworkManagerUtils.h" #include "nm-logging.h" +#include "nm-netlink-compat.h" #include "nm-netlink-monitor.h" +#include "nm-netlink-utils.h" /* Because of a bug in libnl, rtnl.h should be included before route.h */ #include <netlink/route/rtnl.h> @@ -50,7 +52,7 @@ */ void nm_generic_enable_loopback (void) { - struct nl_handle * nlh = NULL; + struct nl_sock * nlh = NULL; struct rtnl_addr * addr = NULL; struct nl_addr * nl_addr = NULL; guint32 binaddr = 0; @@ -92,7 +94,7 @@ void nm_generic_enable_loopback (void) if ((err = rtnl_addr_add (nlh, addr, 0)) < 0) { if (err != -EEXIST) { - nm_log_warn (LOGD_CORE, "error %d returned from rtnl_addr_add():\n%s", err, nl_geterror()); + nm_log_warn (LOGD_CORE, "error %d returned from rtnl_addr_add():\n%s", err, nl_geterror(err)); } } out: diff --git a/src/ip6-manager/nm-ip6-manager.c b/src/ip6-manager/nm-ip6-manager.c index 4a5dee587d..60045234ca 100644 --- a/src/ip6-manager/nm-ip6-manager.c +++ b/src/ip6-manager/nm-ip6-manager.c @@ -27,6 +27,8 @@ #include "nm-ip6-manager.h" #include "nm-netlink-monitor.h" +#include "nm-netlink-utils.h" +#include "nm-netlink-compat.h" #include "NetworkManagerUtils.h" #include "nm-marshal.h" #include "nm-logging.h" @@ -44,7 +46,7 @@ typedef struct { NMNetlinkMonitor *monitor; GHashTable *devices; - struct nl_handle *nlh; + struct nl_sock *nlh; struct nl_cache *addr_cache, *route_cache; guint netlink_id; @@ -553,7 +555,7 @@ process_addr (NMIP6Manager *manager, struct nl_msg *msg) } old_size = nl_cache_nitems (priv->addr_cache); - nl_cache_include (priv->addr_cache, (struct nl_object *)rtnladdr, NULL); + nl_cache_include (priv->addr_cache, (struct nl_object *)rtnladdr, NULL, NULL); rtnl_addr_put (rtnladdr); /* The kernel will re-notify us of automatically-added addresses @@ -593,7 +595,7 @@ process_route (NMIP6Manager *manager, struct nl_msg *msg) } old_size = nl_cache_nitems (priv->route_cache); - nl_cache_include (priv->route_cache, (struct nl_object *)rtnlroute, NULL); + nl_cache_include (priv->route_cache, (struct nl_object *)rtnlroute, NULL, NULL); rtnl_route_put (rtnlroute); /* As above in process_addr */ @@ -1248,7 +1250,7 @@ nm_ip6_manager_get_ip6_config (NMIP6Manager *manager, int ifindex) nm_ip6_route_set_dest (ip6route, dest); nm_ip6_route_set_prefix (ip6route, rtnl_route_get_dst_len (rtnlroute)); nm_ip6_route_set_next_hop (ip6route, gateway); - metric = rtnl_route_get_metric (rtnlroute, 1); + rtnl_route_get_metric(rtnlroute, 1, &metric); if (metric != UINT_MAX) nm_ip6_route_set_metric (ip6route, metric); nm_ip6_config_take_route (config, ip6route); @@ -1344,8 +1346,8 @@ nm_ip6_manager_init (NMIP6Manager *manager) G_CALLBACK (netlink_notification), manager); priv->nlh = nm_netlink_get_default_handle (); - priv->addr_cache = rtnl_addr_alloc_cache (priv->nlh); - priv->route_cache = rtnl_route_alloc_cache (priv->nlh); + rtnl_addr_alloc_cache(priv->nlh, &priv->addr_cache); + rtnl_route_alloc_cache (priv->nlh, NETLINK_ROUTE, NL_AUTO_PROVIDE, &priv->route_cache); } static void diff --git a/src/nm-device.c b/src/nm-device.c index 6a2db4804b..fbb0b06144 100644 --- a/src/nm-device.c +++ b/src/nm-device.c @@ -47,6 +47,8 @@ #include "nm-utils.h" #include "nm-logging.h" #include "nm-netlink-monitor.h" +#include "nm-netlink-utils.h" +#include "nm-netlink-compat.h" #include "nm-setting-ip4-config.h" #include "nm-setting-ip6-config.h" #include "nm-setting-connection.h" @@ -1679,10 +1681,10 @@ dhcp6_start (NMDevice *self, */ err = nm_system_set_ip6_route (priv->ip_iface ? priv->ip_ifindex : priv->ifindex, &dest, 8, NULL, 256, 0, RTPROT_BOOT, RT_TABLE_LOCAL, NULL); - if (err && (nl_get_errno () != EEXIST)) { + if (err && (err != -NLE_EXIST)) { nm_log_err (LOGD_DEVICE | LOGD_IP6, "(%s): failed to add IPv6 multicast route: %s", - priv->ip_iface ? priv->ip_iface : priv->iface, nl_geterror ()); + priv->ip_iface ? priv->ip_iface : priv->iface, nl_geterror (err)); } s_ip6 = (NMSettingIP6Config *) nm_connection_get_setting (connection, NM_TYPE_SETTING_IP6_CONFIG); diff --git a/src/nm-netlink-compat.c b/src/nm-netlink-compat.c new file mode 100644 index 0000000000..38bbf88ddb --- /dev/null +++ b/src/nm-netlink-compat.c @@ -0,0 +1,92 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ +/* NetworkManager -- Network link manager + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Copyright (C) 2011 Caixa Magica Software. + */ + +#include <config.h> +#include <glib.h> + +#include "nm-netlink-compat.h" + +#ifndef HAVE_LIBNL1 +struct rtnl_nexthop * nm_netlink_get_nh(struct rtnl_route * route) { + int hops; + + hops = rtnl_route_get_nnexthops (route); + g_return_val_if_fail(hops > 0, NULL); + + return rtnl_route_nexthop_n (route, 0); +} + +int rtnl_route_get_oif(struct rtnl_route * route) { + struct rtnl_nexthop * nh; + nh = nm_netlink_get_nh(route); + g_return_val_if_fail(nh, -NLE_OBJ_NOTFOUND); + return rtnl_route_nh_get_ifindex (nh); +} + +int rtnl_route_set_oif(struct rtnl_route * route, int ifindex) { + struct rtnl_nexthop * nh; + + nh = rtnl_route_nh_alloc(); + rtnl_route_nh_set_ifindex(nh, ifindex); + rtnl_route_add_nexthop(route, nh); + + return 0; +} + +struct nl_addr * rtnl_route_get_gateway(struct rtnl_route * route) { + struct rtnl_nexthop * nh; + + nh = nm_netlink_get_nh(route); + g_return_val_if_fail(nh, NULL); + return rtnl_route_nh_get_gateway(nh); +} + +int rtnl_route_set_gateway(struct rtnl_route * route, struct nl_addr * gw_addr) { + struct rtnl_nexthop * nh; + + nh = nm_netlink_get_nh(route); + g_return_val_if_fail(nh, -NLE_OBJ_NOTFOUND); + + rtnl_route_nh_set_gateway(nh, gw_addr); + return 0; +} + +int rtnl_route_get_dst_len(struct rtnl_route * rtnlroute) { + struct nl_addr * dst; + + dst = rtnl_route_get_dst(rtnlroute); + return nl_addr_get_prefixlen(dst); +} +#endif + +#ifdef HAVE_LIBNL1 +int nl_compat_error(int err) { + err = abs(err); + + if(err==EEXIST) + err = NLE_EXIST; + else if(err==ENOENT) + err = NLE_OBJ_NOTFOUND; + else if(err==ERANGE) + err = NLE_RANGE; + + return -err; +} +#endif diff --git a/src/nm-netlink-compat.h b/src/nm-netlink-compat.h new file mode 100644 index 0000000000..6101b307a4 --- /dev/null +++ b/src/nm-netlink-compat.h @@ -0,0 +1,162 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ +/* NetworkManager -- Network link manager + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Copyright (C) 2011 Caixa Magica Software. + */ +#ifndef NM_NETLINK_COMPAT_H +#define NM_NETLINK_COMPAT_H + +#include <errno.h> + +#include <netlink/netlink.h> +#include <netlink/route/rtnl.h> +#include <netlink/route/link.h> +#include <netlink/route/route.h> +#include <netlink/route/addr.h> + + +#include <netlink/netlink.h> +#include <netlink/cache.h> +#include <netlink/utils.h> +#include <netlink/data.h> +#include <netlink/route/rtnl.h> +#include <netlink/route/route.h> +#include <netlink/route/link.h> +#include <netlink/route/nexthop.h> + +#include <config.h> + +/* libnl-1 API compatibility for libnl-2/3*/ +#ifndef HAVE_LIBNL1 + +struct rtnl_nexthop * nm_netlink_get_nh(struct rtnl_route *); +int rtnl_route_get_oif(struct rtnl_route *); +int rtnl_route_set_oif(struct rtnl_route *, int); +int rtnl_route_set_gateway(struct rtnl_route *, struct nl_addr *); +int rtnl_route_get_dst_len(struct rtnl_route *); +struct nl_addr * rtnl_route_get_gateway(struct rtnl_route *); +#endif + + +/* libnl-1.0 compat functions */ +#ifdef HAVE_LIBNL1 + +#define nl_sock nl_handle + +/* libnl-1.0 functions with similar prototypes */ +#define nl_socket_alloc nl_handle_alloc +#define nl_socket_alloc_cb nl_handle_alloc_cb +#define nl_socket_free nl_handle_destroy +#define nl_socket_set_passcred nl_set_passcred +#define nl_socket_disable_seq_check nl_disable_sequence_check +#define rtnl_route_set_priority rtnl_route_set_prio + +/* auxiliary functions */ +int nl_compat_error(int); + +/* libnl-1.0 functions with modified prototypes in libnl-2/3*/ +static inline const char * __nl_geterror(int err) +{ + /* err is set, can be parsed */ + return nl_geterror(); +} +#define nl_geterror __nl_geterror + +static inline int __rtnl_addr_alloc_cache(struct nl_sock *h, struct nl_cache **cache) { + *cache = rtnl_addr_alloc_cache(h); + if(!*cache) + return -ENOMEM; + return 0; +} +#define rtnl_addr_alloc_cache __rtnl_addr_alloc_cache + +static inline int __rtnl_route_alloc_cache(struct nl_sock *h, int family, int flags, struct nl_cache **cache) { + *cache = rtnl_route_alloc_cache(h); + if(!*cache) + return -ENOMEM; + return 0; +} +#define rtnl_route_alloc_cache __rtnl_route_alloc_cache + +static inline int __rtnl_link_alloc_cache(struct nl_sock *h, struct nl_cache **cache) { + *cache = rtnl_link_alloc_cache (h); + if(!*cache) + return -ENOMEM; + return 0; +} +#define rtnl_link_alloc_cache __rtnl_link_alloc_cache + +static inline int __rtnl_route_get_metric(struct rtnl_route * route, int metric, unsigned int *value) { + *value = rtnl_route_get_metric(route, metric); + return 0; +} +#define rtnl_route_get_metric __rtnl_route_get_metric + +static inline int __rtnl_addr_add(struct nl_sock * h, struct rtnl_addr * addr, int flags) { + return nl_compat_error(rtnl_addr_add(h,addr,flags)); +} +#define rtnl_addr_add __rtnl_addr_add + +static inline int rtnl_route_delete(struct nl_sock * h, struct rtnl_route * route, int flags) { + return nl_compat_error(rtnl_route_del(h, route, flags)); +} +#define rtnl_route_del rtnl_route_delete + +static inline int __rtnl_link_change(struct nl_sock * h, struct rtnl_link *old, struct rtnl_link * tmpl, int flags) { + return nl_compat_error(rtnl_link_change(h, old, tmpl,flags)); +} +#define rtnl_link_change __rtnl_link_change + +static inline int __nl_cache_include(struct nl_cache * cache, struct nl_object * obj, change_func_t cb, void * data) +{ + return nl_cache_include(cache, obj, cb); +} +#define nl_cache_include __nl_cache_include + +#define NLE_SUCCESS 0 +#define NLE_FAILURE 1 +#define NLE_INTR 2 +#define NLE_BAD_SOCK 3 +#define NLE_AGAIN 4 +#define NLE_NOMEM 5 +#define NLE_EXIST 6 +#define NLE_INVAL 7 +#define NLE_RANGE 8 +#define NLE_MSGSIZE 9 +#define NLE_OPNOTSUPP 10 +#define NLE_AF_NOSUPPORT 11 +#define NLE_OBJ_NOTFOUND 12 +#define NLE_NOATTR 13 +#define NLE_MISSING_ATTR 14 +#define NLE_AF_MISMATCH 15 +#define NLE_SEQ_MISMATCH 16 +#define NLE_MSG_OVERFLOW 17 +#define NLE_MSG_TRUNC 18 +#define NLE_NOADDR 19 +#define NLE_SRCRT_NOSUPPORT 20 +#define NLE_MSG_TOOSHORT 21 +#define NLE_MSGTYPE_NOSUPPORT 22 +#define NLE_OBJ_MISMATCH 23 +#define NLE_NOCACHE 24 +#define NLE_BUSY 25 +#define NLE_PROTO_MISMATCH 26 +#define NLE_NOACCESS 27 +#define NLE_PERM 28 +#define NLE_PKTLOC_FILE 29 +#endif + +#endif /* NM_NETLINK_COMPAT_H */ diff --git a/src/nm-netlink-monitor.c b/src/nm-netlink-monitor.c index 13b3ab9850..978e87d6aa 100644 --- a/src/nm-netlink-monitor.c +++ b/src/nm-netlink-monitor.c @@ -45,6 +45,7 @@ #include <glib.h> #include <glib/gi18n.h> +#include "nm-netlink-compat.h" #include "nm-netlink-monitor.h" #include "nm-logging.h" @@ -58,12 +59,12 @@ typedef struct { /* Async event listener connection */ - struct nl_handle *nlh_event; + struct nl_sock *nlh_event; GIOChannel * io_channel; guint event_id; /* Sync/blocking request/response connection */ - struct nl_handle *nlh_sync; + struct nl_sock *nlh_sync; struct nl_cache * link_cache; guint request_status_id; @@ -98,7 +99,7 @@ link_msg_handler (struct nl_object *obj, void *arg) error = g_error_new (NM_NETLINK_MONITOR_ERROR, NM_NETLINK_MONITOR_ERROR_BAD_ALLOC, _("error processing netlink message: %s"), - nl_geterror ()); + nl_geterror (ENOMEM)); g_signal_emit (self, signals[ERROR], 0, error); g_error_free (error); return; @@ -130,7 +131,7 @@ link_msg_handler (struct nl_object *obj, void *arg) static int event_msg_recv (struct nl_msg *msg, void *arg) { - struct nl_handle *nlh = arg; + struct nl_sock *nlh = arg; struct nlmsghdr *hdr = nlmsg_hdr (msg); struct ucred *creds = nlmsg_get_creds (msg); const struct sockaddr_nl *snl; @@ -195,6 +196,7 @@ event_handler (GIOChannel *channel, NMNetlinkMonitor *self = (NMNetlinkMonitor *) user_data; NMNetlinkMonitorPrivate *priv; GError *error = NULL; + int err; g_return_val_if_fail (NM_IS_NETLINK_MONITOR (self), TRUE); @@ -225,11 +227,11 @@ event_handler (GIOChannel *channel, g_return_val_if_fail (!(io_condition & ~EVENT_CONDITIONS), FALSE); /* Process the netlink messages */ - if (nl_recvmsgs_default (priv->nlh_event) < 0) { + if ((err = nl_recvmsgs_default (priv->nlh_event)) < 0) { error = g_error_new (NM_NETLINK_MONITOR_ERROR, NM_NETLINK_MONITOR_ERROR_PROCESSING_MESSAGE, _("error processing netlink message: %s"), - nl_geterror ()); + nl_geterror (err)); g_signal_emit (self, signals[ERROR], 0, error); g_error_free (error); } @@ -238,32 +240,34 @@ event_handler (GIOChannel *channel, } static gboolean -nlh_setup (struct nl_handle *nlh, +nlh_setup (struct nl_sock *nlh, nl_recvmsg_msg_cb_t valid_func, gpointer cb_data, GError **error) { + int err; + nl_socket_modify_cb (nlh, NL_CB_MSG_IN, NL_CB_CUSTOM, event_msg_recv, cb_data); if (valid_func) nl_socket_modify_cb (nlh, NL_CB_VALID, NL_CB_CUSTOM, valid_func, cb_data); - if (nl_connect (nlh, NETLINK_ROUTE) < 0) { + if ((err = nl_connect (nlh, NETLINK_ROUTE)) < 0) { g_set_error (error, NM_NETLINK_MONITOR_ERROR, NM_NETLINK_MONITOR_ERROR_NETLINK_CONNECT, _("unable to connect to netlink for monitoring link status: %s"), - nl_geterror ()); + nl_geterror (err)); return FALSE; } /* Enable unix socket peer credentials which we use for verifying that the * sender of the message is actually the kernel. */ - if (nl_set_passcred (nlh, 1) < 0) { + if (nl_socket_set_passcred (nlh, 1) < 0) { g_set_error (error, NM_NETLINK_MONITOR_ERROR, NM_NETLINK_MONITOR_ERROR_NETLINK_CONNECT, _("unable to enable netlink handle credential passing: %s"), - nl_geterror ()); + nl_geterror (err)); return FALSE; } @@ -283,20 +287,20 @@ event_connection_setup (NMNetlinkMonitor *self, GError **error) /* Set up the event listener connection */ cb = nl_cb_alloc (NL_CB_DEFAULT); - priv->nlh_event = nl_handle_alloc_cb (cb); + priv->nlh_event = nl_socket_alloc_cb (cb); nl_cb_put (cb); if (!priv->nlh_event) { g_set_error (error, NM_NETLINK_MONITOR_ERROR, NM_NETLINK_MONITOR_ERROR_NETLINK_ALLOC_HANDLE, _("unable to allocate netlink handle for monitoring link status: %s"), - nl_geterror ()); + nl_geterror (ENOMEM)); goto error; } if (!nlh_setup (priv->nlh_event, event_msg_ready, self, error)) goto error; - nl_disable_sequence_check (priv->nlh_event); + nl_socket_disable_seq_check (priv->nlh_event); /* Subscribe to the LINK group for internal carrier signals */ if (!nm_netlink_monitor_subscribe (self, RTNLGRP_LINK, error)) @@ -327,7 +331,7 @@ error: nm_netlink_monitor_close_connection (self); if (priv->nlh_event) { - nl_handle_destroy (priv->nlh_event); + nl_socket_free (priv->nlh_event); priv->nlh_event = NULL; } @@ -342,16 +346,17 @@ sync_connection_setup (NMNetlinkMonitor *self, GError **error) #ifdef LIBNL_NEEDS_ADDR_CACHING_WORKAROUND struct nl_cache *addr_cache; #endif + int err; /* Set up the event listener connection */ cb = nl_cb_alloc (NL_CB_DEFAULT); - priv->nlh_sync = nl_handle_alloc_cb (cb); + priv->nlh_sync = nl_socket_alloc_cb (cb); nl_cb_put (cb); if (!priv->nlh_sync) { g_set_error (error, NM_NETLINK_MONITOR_ERROR, NM_NETLINK_MONITOR_ERROR_NETLINK_ALLOC_HANDLE, _("unable to allocate netlink handle for monitoring link status: %s"), - nl_geterror ()); + nl_geterror (ENOMEM)); goto error; } @@ -365,16 +370,18 @@ sync_connection_setup (NMNetlinkMonitor *self, GError **error) * result, most addresses will not compare as equal even to * themselves, busting caching. */ - addr_cache = rtnl_addr_alloc_cache (priv->nlh_sync); + rtnl_addr_alloc_cache (priv->nlh_sync, &addr_cache); nl_cache_get_ops (addr_cache)->co_obj_ops->oo_id_attrs &= ~0x80; nl_cache_free (addr_cache); #endif - if ((priv->link_cache = rtnl_link_alloc_cache (priv->nlh_sync)) == NULL) { + err = rtnl_link_alloc_cache (priv->nlh_sync, &priv->link_cache); + + if (err) { g_set_error (error, NM_NETLINK_MONITOR_ERROR, NM_NETLINK_MONITOR_ERROR_NETLINK_ALLOC_LINK_CACHE, _("unable to allocate netlink link cache for monitoring link status: %s"), - nl_geterror ()); + nl_geterror (err)); goto error; } nl_cache_mngt_provide (priv->link_cache); @@ -388,7 +395,7 @@ error: } if (priv->nlh_sync) { - nl_handle_destroy (priv->nlh_sync); + nl_socket_free (priv->nlh_sync); priv->nlh_sync = NULL; } @@ -483,7 +490,7 @@ gboolean nm_netlink_monitor_subscribe (NMNetlinkMonitor *self, int group, GError **error) { NMNetlinkMonitorPrivate *priv; - int subs; + int subs, err; g_return_val_if_fail (NM_IS_NETLINK_MONITOR (self), FALSE); @@ -496,11 +503,11 @@ nm_netlink_monitor_subscribe (NMNetlinkMonitor *self, int group, GError **error) subs = get_subs (self, group) + 1; if (subs == 1) { - if (nl_socket_add_membership (priv->nlh_event, group) < 0) { + if ((err = nl_socket_add_membership (priv->nlh_event, group)) < 0) { g_set_error (error, NM_NETLINK_MONITOR_ERROR, NM_NETLINK_MONITOR_ERROR_NETLINK_JOIN_GROUP, _("unable to join netlink group: %s"), - nl_geterror ()); + nl_geterror (err)); return FALSE; } } @@ -558,14 +565,15 @@ deferred_emit_carrier_state (gpointer user_data) { NMNetlinkMonitor *self = NM_NETLINK_MONITOR (user_data); NMNetlinkMonitorPrivate *priv = NM_NETLINK_MONITOR_GET_PRIVATE (self); + int err; priv->request_status_id = 0; /* Update the link cache with latest state, and if there are no errors * emit the link states for all the interfaces in the cache. */ - if (nl_cache_refill (priv->nlh_sync, priv->link_cache)) { - nm_log_err (LOGD_HW, "error updating link cache: %s", nl_geterror ()); + if ((err = nl_cache_refill (priv->nlh_sync, priv->link_cache)) != 0) { + nm_log_err (LOGD_HW, "error updating link cache: %s", nl_geterror (err)); } else nl_cache_foreach_filter (priv->link_cache, NULL, link_msg_handler, self); @@ -614,6 +622,7 @@ nm_netlink_monitor_get_flags_sync (NMNetlinkMonitor *self, NMNetlinkMonitorPrivate *priv; GetFlagsInfo info; struct rtnl_link *filter; + int err; g_return_val_if_fail (self != NULL, FALSE); g_return_val_if_fail (NM_IS_NETLINK_MONITOR (self), FALSE); @@ -622,12 +631,12 @@ nm_netlink_monitor_get_flags_sync (NMNetlinkMonitor *self, priv = NM_NETLINK_MONITOR_GET_PRIVATE (self); /* Update the link cache with the latest information */ - if (nl_cache_refill (priv->nlh_sync, priv->link_cache)) { + if ((err = nl_cache_refill (priv->nlh_sync, priv->link_cache)) != 0) { g_set_error (error, NM_NETLINK_MONITOR_ERROR, NM_NETLINK_MONITOR_ERROR_LINK_CACHE_UPDATE, _("error updating link cache: %s"), - nl_geterror ()); + nl_geterror (err)); return FALSE; } @@ -640,7 +649,7 @@ nm_netlink_monitor_get_flags_sync (NMNetlinkMonitor *self, NM_NETLINK_MONITOR_ERROR, NM_NETLINK_MONITOR_ERROR_LINK_CACHE_UPDATE, _("error updating link cache: %s"), - nl_geterror ()); + nl_geterror (err)); return FALSE; } @@ -651,7 +660,7 @@ nm_netlink_monitor_get_flags_sync (NMNetlinkMonitor *self, NM_NETLINK_MONITOR_ERROR, NM_NETLINK_MONITOR_ERROR_BAD_ALLOC, _("error processing netlink message: %s"), - nl_geterror ()); + nl_geterror (err)); return FALSE; } rtnl_link_set_ifindex (filter, ifindex); @@ -678,11 +687,11 @@ nm_netlink_monitor_get_flags_sync (NMNetlinkMonitor *self, /***************************************************************/ -struct nl_handle * +struct nl_sock * nm_netlink_get_default_handle (void) { NMNetlinkMonitor *self; - struct nl_handle *nlh; + struct nl_sock *nlh; self = nm_netlink_monitor_get (); nlh = NM_NETLINK_MONITOR_GET_PRIVATE (self)->nlh_sync; @@ -796,12 +805,12 @@ finalize (GObject *object) } if (priv->nlh_event) { - nl_handle_destroy (priv->nlh_event); + nl_socket_free (priv->nlh_event); priv->nlh_event = NULL; } if (priv->nlh_sync) { - nl_handle_destroy (priv->nlh_sync); + nl_socket_free (priv->nlh_sync); priv->nlh_sync = NULL; } diff --git a/src/nm-netlink-monitor.h b/src/nm-netlink-monitor.h index 8b57a312d7..38ccd350df 100644 --- a/src/nm-netlink-monitor.h +++ b/src/nm-netlink-monitor.h @@ -90,10 +90,12 @@ gboolean nm_netlink_monitor_get_flags_sync (NMNetlinkMonitor *monitor guint32 *ifflags, GError **error); +#include "nm-netlink-compat.h" + /* Generic utility functions */ int nm_netlink_iface_to_index (const char *iface); char * nm_netlink_index_to_iface (int idx); struct rtnl_link *nm_netlink_index_to_rtnl_link (int idx); -struct nl_handle *nm_netlink_get_default_handle (void); +struct nl_sock *nm_netlink_get_default_handle (void); #endif /* NM_NETLINK_MONITOR_H */ diff --git a/src/nm-netlink-utils.c b/src/nm-netlink-utils.c index ef7ebed7ba..dfb726546b 100644 --- a/src/nm-netlink-utils.c +++ b/src/nm-netlink-utils.c @@ -21,6 +21,7 @@ #include "logging/nm-logging.h" #include "nm-netlink-utils.h" #include "nm-netlink-monitor.h" +#include "nm-netlink-compat.h" #include <arpa/inet.h> #include <netinet/in.h> @@ -28,6 +29,8 @@ #include <netlink/addr.h> #include <netlink/route/addr.h> +#include <errno.h> + typedef struct { int ifindex; int family; @@ -86,7 +89,7 @@ nm_netlink_find_address (int ifindex, void *addr, /* struct in_addr or struct in6_addr */ int prefix) { - struct nl_handle *nlh = NULL; + struct nl_sock *nlh = NULL; struct nl_cache *cache = NULL; FindAddrInfo info; @@ -109,7 +112,7 @@ nm_netlink_find_address (int ifindex, nlh = nm_netlink_get_default_handle (); if (nlh) { - cache = rtnl_addr_alloc_cache (nlh); + rtnl_addr_alloc_cache(nlh, &cache); if (cache) { nl_cache_mngt_provide (cache); nl_cache_foreach (cache, find_one_address, &info); @@ -152,7 +155,7 @@ nm_netlink_route_new (int ifindex, else if (prop == NMNL_PROP_SCOPE && value != RT_SCOPE_NOWHERE) rtnl_route_set_scope (route, value); else if (prop == NMNL_PROP_PRIO && value > 0) - rtnl_route_set_prio (route, value); + rtnl_route_set_priority (route, value); prop = va_arg (var_args, NmNlProp); } @@ -170,14 +173,15 @@ nm_netlink_route_new (int ifindex, gboolean nm_netlink_route_delete (struct rtnl_route *route) { - struct nl_handle *nlh; - int err; + struct nl_sock *nlh; + int err=0; g_return_val_if_fail (route != NULL, FALSE); nlh = nm_netlink_get_default_handle (); - err = rtnl_route_del (nlh, route, 0); - return err == 0 ? TRUE : FALSE; + err = rtnl_route_delete (nlh, route, 0); + + return ((err < 0) && (err != -NLE_RANGE)) ? FALSE: TRUE; } @@ -321,10 +325,11 @@ nm_netlink_foreach_route (int ifindex, info.user_data = user_data; info.iface = nm_netlink_index_to_iface (ifindex); - cache = rtnl_route_alloc_cache (nm_netlink_get_default_handle ()); + rtnl_route_alloc_cache (nm_netlink_get_default_handle (), family, NL_AUTO_PROVIDE, &cache); g_return_val_if_fail (cache != NULL, NULL); nl_cache_foreach (cache, foreach_route_cb, &info); nl_cache_free (cache); return info.out_route; } + diff --git a/src/nm-system.c b/src/nm-system.c index 21cd8ce972..4f6236db3c 100644 --- a/src/nm-system.c +++ b/src/nm-system.c @@ -49,6 +49,7 @@ #include "nm-logging.h" #include "nm-netlink-monitor.h" #include "nm-netlink-utils.h" +#include "nm-netlink-compat.h" #include <netlink/route/addr.h> #include <netlink/route/route.h> @@ -92,7 +93,7 @@ nm_system_device_set_ip4_route (int ifindex, guint32 metric, int mss) { - struct nl_handle *nlh; + struct nl_sock *nlh; struct rtnl_route *route; struct nl_addr *dest_addr; struct nl_addr *gw_addr = NULL; @@ -160,7 +161,7 @@ nm_system_device_set_ip4_route (int ifindex, if (err) { nm_log_err (LOGD_DEVICE | LOGD_IP4, "(%s): failed to set IPv4 route: %s", - iface, nl_geterror ()); + iface, nl_geterror (err)); rtnl_route_put (route); route = NULL; } @@ -174,7 +175,7 @@ sync_addresses (int ifindex, struct rtnl_addr **addrs, int num_addrs) { - struct nl_handle *nlh; + struct nl_sock *nlh; struct nl_cache *addr_cache; struct rtnl_addr *filter_addr, *match_addr; struct nl_object *match; @@ -193,7 +194,8 @@ sync_addresses (int ifindex, if (!nlh) return FALSE; - addr_cache = rtnl_addr_alloc_cache (nlh); + rtnl_addr_alloc_cache(nlh, &addr_cache); + if (!addr_cache) return FALSE; @@ -266,7 +268,7 @@ sync_addresses (int ifindex, err = rtnl_addr_delete (nlh, match_addr, 0); if (err < 0) { nm_log_err (log_domain, "(%s): error %d returned from rtnl_addr_delete(): %s", - iface, err, nl_geterror ()); + iface, err, nl_geterror (err)); } } @@ -299,10 +301,10 @@ sync_addresses (int ifindex, } err = rtnl_addr_add (nlh, addrs[i], 0); - if (err < 0 && (nl_get_errno () != EEXIST)) { + if (err < 0 && (err != -NLE_EXIST)) { nm_log_err (log_domain, "(%s): error %d returned from rtnl_addr_add():\n%s", - iface, err, nl_geterror ()); + iface, err, nl_geterror (err)); } rtnl_addr_put (addrs[i]); @@ -482,7 +484,7 @@ nm_system_set_ip6_route (int ifindex, int table, struct rtnl_route **out_route) { - struct nl_handle *nlh; + struct nl_sock *nlh; struct rtnl_route *route; struct nl_addr *dest_addr; struct nl_addr *gw_addr = NULL; @@ -634,7 +636,7 @@ nm_system_apply_ip6_config (int ifindex, if (err) { nm_log_err (LOGD_DEVICE | LOGD_IP6, "(%s): failed to set IPv6 route: %s", - iface, nl_geterror ()); + iface, nl_geterror (err)); } } } @@ -663,8 +665,9 @@ nm_system_iface_set_up (int ifindex, gboolean *no_firmware) { struct rtnl_link *request = NULL, *old = NULL; - struct nl_handle *nlh; + struct nl_sock *nlh; gboolean success = FALSE; + int err; g_return_val_if_fail (ifindex > 0, FALSE); if (no_firmware) @@ -682,10 +685,13 @@ nm_system_iface_set_up (int ifindex, if (old) { nlh = nm_netlink_get_default_handle (); if (nlh) { - if (rtnl_link_change (nlh, old, request, 0) == 0) + err = rtnl_link_change (nlh, old, request, 0); + if (err == 0) { success = TRUE; - else if ((nl_get_errno () == ENOENT) && no_firmware && up) - *no_firmware = TRUE; + } else { + if ((err == -NLE_OBJ_NOTFOUND) && no_firmware && up) + *no_firmware = TRUE; + } } } @@ -738,7 +744,7 @@ nm_system_iface_set_mtu (int ifindex, guint32 mtu) struct rtnl_link *old; struct rtnl_link *new; gboolean success = FALSE; - struct nl_handle *nlh; + struct nl_sock *nlh; const char *iface; int err; @@ -785,7 +791,7 @@ nm_system_iface_set_mac (int ifindex, const struct ether_addr *mac) { struct rtnl_link *old, *new; gboolean success = FALSE; - struct nl_handle *nlh; + struct nl_sock *nlh; const char *iface; struct nl_addr *addr = NULL; int err; @@ -826,7 +832,7 @@ nm_system_iface_set_mac (int ifindex, const struct ether_addr *mac) static struct rtnl_route * add_ip4_route_to_gateway (int ifindex, guint32 gw, guint32 mss) { - struct nl_handle *nlh; + struct nl_sock *nlh; struct rtnl_route *route = NULL; struct nl_addr *gw_addr = NULL; const char *iface; @@ -872,7 +878,7 @@ static int replace_default_ip4_route (int ifindex, guint32 gw, guint32 mss) { struct rtnl_route *route = NULL; - struct nl_handle *nlh; + struct nl_sock *nlh; struct nl_addr *dst_addr = NULL; guint32 dst = 0; struct nl_addr *gw_addr = NULL; @@ -934,7 +940,7 @@ nm_system_replace_default_ip4_route_vpn (int ifindex, guint32 parent_mss) { struct rtnl_route *gw_route = NULL; - struct nl_handle *nlh; + struct nl_sock *nlh; gboolean success = FALSE; int err; const char *iface; @@ -1023,7 +1029,7 @@ nm_system_replace_default_ip4_route (int ifindex, guint32 gw, guint32 mss) static struct rtnl_route * add_ip6_route_to_gateway (int ifindex, const struct in6_addr *gw) { - struct nl_handle *nlh; + struct nl_sock *nlh; struct rtnl_route *route = NULL; struct nl_addr *gw_addr = NULL; const char *iface; @@ -1069,7 +1075,7 @@ static int replace_default_ip6_route (int ifindex, const struct in6_addr *gw) { struct rtnl_route *route = NULL; - struct nl_handle *nlh; + struct nl_sock *nlh; struct nl_addr *gw_addr = NULL; const char *iface; int err = -1; @@ -1266,14 +1272,14 @@ nm_system_device_set_priority (int ifindex, NMIP4Config *config, int priority) { - struct nl_handle *nlh; + struct nl_sock *nlh; struct rtnl_route *found; found = nm_netlink_foreach_route (ifindex, AF_INET, RT_SCOPE_LINK, FALSE, find_route, config); if (found) { nlh = nm_netlink_get_default_handle (); nm_netlink_route_delete (found); - rtnl_route_set_prio (found, priority); + rtnl_route_set_priority (found, priority); rtnl_route_add (nlh, found, 0); rtnl_route_put (found); } |