diff --git a/include/NetworkManager.h b/include/NetworkManager.h index 0c91022175..496612033d 100644 --- a/include/NetworkManager.h +++ b/include/NetworkManager.h @@ -56,6 +56,7 @@ #define NM_DBUS_INTERFACE_DEVICE_GENERIC NM_DBUS_INTERFACE_DEVICE ".Generic" #define NM_DBUS_INTERFACE_DEVICE_VETH NM_DBUS_INTERFACE_DEVICE ".Veth" #define NM_DBUS_INTERFACE_DEVICE_TUN NM_DBUS_INTERFACE_DEVICE ".Tun" +#define NM_DBUS_INTERFACE_DEVICE_MACVLAN NM_DBUS_INTERFACE_DEVICE ".Macvlan" #define NM_DBUS_IFACE_SETTINGS "org.freedesktop.NetworkManager.Settings" diff --git a/introspection/Makefile.am b/introspection/Makefile.am index 68f6965c28..0f337ce57f 100644 --- a/introspection/Makefile.am +++ b/introspection/Makefile.am @@ -18,6 +18,7 @@ EXTRA_DIST = \ nm-device-generic.xml \ nm-device-veth.xml \ nm-device-tun.xml \ + nm-device-macvlan.xml \ nm-device.xml \ nm-ip4-config.xml \ nm-ip6-config.xml \ diff --git a/introspection/nm-device-macvlan.xml b/introspection/nm-device-macvlan.xml new file mode 100644 index 0000000000..a387bf5647 --- /dev/null +++ b/introspection/nm-device-macvlan.xml @@ -0,0 +1,33 @@ + + + + + + + + The object path of the parent device. + + + + + + The macvlan mode, one of "private", "vepa", "bridge", or "passthru". + + + + + + Whether the device is blocked from going into promiscuous mode. + + + + + + + A dictionary mapping property names to variant boxed values + + + + + + diff --git a/src/Makefile.am b/src/Makefile.am index c67b79456d..c50612b967 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -78,6 +78,8 @@ nm_sources = \ devices/nm-device-generic.h \ devices/nm-device-infiniband.c \ devices/nm-device-infiniband.h \ + devices/nm-device-macvlan.c \ + devices/nm-device-macvlan.h \ devices/nm-device-modem.c \ devices/nm-device-modem.h \ devices/nm-device-olpc-mesh.c \ @@ -313,6 +315,7 @@ glue_sources = \ nm-device-generic-glue.h \ nm-device-glue.h \ nm-device-infiniband-glue.h \ + nm-device-macvlan-glue.h \ nm-device-modem-glue.h \ nm-device-olpc-mesh-glue.h \ nm-device-tun-glue.h \ diff --git a/src/devices/nm-device-macvlan.c b/src/devices/nm-device-macvlan.c new file mode 100644 index 0000000000..8ebd0fff1f --- /dev/null +++ b/src/devices/nm-device-macvlan.c @@ -0,0 +1,182 @@ +/* -*- 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 2013 Red Hat, Inc. + */ + +#include "config.h" + +#include + +#include "nm-device-macvlan.h" +#include "nm-dbus-manager.h" +#include "nm-logging.h" +#include "nm-manager.h" +#include "nm-platform.h" + +#include "nm-device-macvlan-glue.h" + +G_DEFINE_TYPE (NMDeviceMacvlan, nm_device_macvlan, NM_TYPE_DEVICE_GENERIC) + +#define NM_DEVICE_MACVLAN_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_DEVICE_MACVLAN, NMDeviceMacvlanPrivate)) + +typedef struct { + NMDevice *parent; + NMPlatformMacvlanProperties props; +} NMDeviceMacvlanPrivate; + +enum { + PROP_0, + PROP_PARENT, + PROP_MODE, + PROP_NO_PROMISC, + + LAST_PROP +}; + +/**************************************************************/ + +/**************************************************************/ + +static void +link_changed (NMDevice *device) +{ + NMDeviceMacvlanPrivate *priv = NM_DEVICE_MACVLAN_GET_PRIVATE (device); + GObject *object = G_OBJECT (device); + NMPlatformMacvlanProperties props; + + if (!nm_platform_macvlan_get_properties (nm_device_get_ifindex (device), &props)) { + nm_log_warn (LOGD_HW, "(%s): could not read macvlan properties", + nm_device_get_iface (device)); + return; + } + + g_object_freeze_notify (object); + + if (priv->props.parent_ifindex != props.parent_ifindex) { + g_object_notify (object, NM_DEVICE_MACVLAN_PARENT); + if (priv->parent) + g_object_remove_weak_pointer (G_OBJECT (priv->parent), (gpointer *) &priv->parent); + priv->parent = nm_manager_get_device_by_ifindex (nm_manager_get (), props.parent_ifindex); + if (priv->parent) + g_object_add_weak_pointer (G_OBJECT (priv->parent), (gpointer *) &priv->parent); + } + if (g_strcmp0 (priv->props.mode, props.mode) != 0) + g_object_notify (object, NM_DEVICE_MACVLAN_MODE); + if (priv->props.no_promisc != props.no_promisc) + g_object_notify (object, NM_DEVICE_MACVLAN_NO_PROMISC); + + memcpy (&priv->props, &props, sizeof (NMPlatformMacvlanProperties)); + + g_object_thaw_notify (object); +} + +/**************************************************************/ + +NMDevice * +nm_device_macvlan_new (const char *udi, + const char *iface, + const char *driver) +{ + g_return_val_if_fail (udi != NULL, NULL); + + return (NMDevice *) g_object_new (NM_TYPE_DEVICE_MACVLAN, + NM_DEVICE_UDI, udi, + NM_DEVICE_IFACE, iface, + NM_DEVICE_DRIVER, driver, + NM_DEVICE_TYPE_DESC, "Macvlan", + NM_DEVICE_DEVICE_TYPE, NM_DEVICE_TYPE_GENERIC, + NULL); +} + +static void +nm_device_macvlan_init (NMDeviceMacvlan *self) +{ +} + +static void +constructed (GObject *object) +{ + link_changed (NM_DEVICE (object)); + + G_OBJECT_CLASS (nm_device_macvlan_parent_class)->constructed (object); +} + +static void +get_property (GObject *object, guint prop_id, + GValue *value, GParamSpec *pspec) +{ + NMDeviceMacvlanPrivate *priv = NM_DEVICE_MACVLAN_GET_PRIVATE (object); + + switch (prop_id) { + case PROP_PARENT: + g_value_set_boxed (value, priv->parent ? nm_device_get_path (priv->parent) : "/"); + break; + case PROP_MODE: + g_value_set_string (value, priv->props.mode); + break; + case PROP_NO_PROMISC: + g_value_set_boolean (value, priv->props.no_promisc); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +nm_device_macvlan_class_init (NMDeviceMacvlanClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + NMDeviceClass *device_class = NM_DEVICE_CLASS (klass); + + g_type_class_add_private (klass, sizeof (NMDeviceMacvlanPrivate)); + + object_class->constructed = constructed; + object_class->get_property = get_property; + + device_class->link_changed = link_changed; + + /* properties */ + g_object_class_install_property + (object_class, PROP_PARENT, + g_param_spec_boxed (NM_DEVICE_MACVLAN_PARENT, + "Parent", + "Parent device", + DBUS_TYPE_G_OBJECT_PATH, + G_PARAM_READABLE)); + + g_object_class_install_property + (object_class, PROP_MODE, + g_param_spec_string (NM_DEVICE_MACVLAN_MODE, + "Mode", + "Mode: 'private', 'vepa', 'bridge', or 'passthru'", + NULL, + G_PARAM_READABLE)); + + g_object_class_install_property + (object_class, PROP_NO_PROMISC, + g_param_spec_boolean (NM_DEVICE_MACVLAN_NO_PROMISC, + "No-promisc", + "No promiscuous mode", + FALSE, + G_PARAM_READABLE)); + + nm_dbus_manager_register_exported_type (nm_dbus_manager_get (), + G_TYPE_FROM_CLASS (klass), + &dbus_glib_nm_device_macvlan_object_info); +} diff --git a/src/devices/nm-device-macvlan.h b/src/devices/nm-device-macvlan.h new file mode 100644 index 0000000000..be6253f152 --- /dev/null +++ b/src/devices/nm-device-macvlan.h @@ -0,0 +1,58 @@ +/* -*- 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 2013 Red Hat, Inc. + */ + +#ifndef NM_DEVICE_MACVLAN_H +#define NM_DEVICE_MACVLAN_H + +#include + +#include "nm-device-generic.h" + +G_BEGIN_DECLS + +#define NM_TYPE_DEVICE_MACVLAN (nm_device_macvlan_get_type ()) +#define NM_DEVICE_MACVLAN(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_DEVICE_MACVLAN, NMDeviceMacvlan)) +#define NM_DEVICE_MACVLAN_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), NM_TYPE_DEVICE_MACVLAN, NMDeviceMacvlanClass)) +#define NM_IS_DEVICE_MACVLAN(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), NM_TYPE_DEVICE_MACVLAN)) +#define NM_IS_DEVICE_MACVLAN_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), NM_TYPE_DEVICE_MACVLAN)) +#define NM_DEVICE_MACVLAN_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), NM_TYPE_DEVICE_MACVLAN, NMDeviceMacvlanClass)) + +#define NM_DEVICE_MACVLAN_PARENT "parent" +#define NM_DEVICE_MACVLAN_MODE "mode" +#define NM_DEVICE_MACVLAN_NO_PROMISC "no-promisc" + +typedef struct { + NMDeviceGeneric parent; +} NMDeviceMacvlan; + +typedef struct { + NMDeviceGenericClass parent; + +} NMDeviceMacvlanClass; + +GType nm_device_macvlan_get_type (void); + +NMDevice *nm_device_macvlan_new (const char *udi, + const char *iface, + const char *driver); + +G_END_DECLS + +#endif /* NM_DEVICE_MACVLAN_H */ diff --git a/src/nm-manager.c b/src/nm-manager.c index f5b4df6c14..625341d67d 100644 --- a/src/nm-manager.c +++ b/src/nm-manager.c @@ -54,6 +54,7 @@ #include "nm-device-generic.h" #include "nm-device-veth.h" #include "nm-device-tun.h" +#include "nm-device-macvlan.h" #include "nm-system.h" #include "nm-setting-bluetooth.h" #include "nm-setting-connection.h" @@ -2297,6 +2298,10 @@ udev_device_added_cb (NMUdevManager *udev_mgr, case NM_LINK_TYPE_TAP: device = nm_device_tun_new (sysfs_path, iface, driver); break; + case NM_LINK_TYPE_MACVLAN: + case NM_LINK_TYPE_MACVTAP: + device = nm_device_macvlan_new (sysfs_path, iface, driver); + break; default: device = nm_device_generic_new (sysfs_path, iface, driver); diff --git a/src/platform/nm-fake-platform.c b/src/platform/nm-fake-platform.c index 796715d60d..2a18168c4e 100644 --- a/src/platform/nm-fake-platform.c +++ b/src/platform/nm-fake-platform.c @@ -570,6 +570,12 @@ tun_get_properties (NMPlatform *platform, int ifindex, NMPlatformTunProperties * return FALSE; } +static gboolean +macvlan_get_properties (NMPlatform *platform, int ifindex, NMPlatformMacvlanProperties *props) +{ + return FALSE; +} + /******************************************************************/ static GArray * @@ -1029,6 +1035,7 @@ nm_fake_platform_class_init (NMFakePlatformClass *klass) platform_class->veth_get_properties = veth_get_properties; platform_class->tun_get_properties = tun_get_properties; + platform_class->macvlan_get_properties = macvlan_get_properties; platform_class->ip4_address_get_all = ip4_address_get_all; platform_class->ip6_address_get_all = ip6_address_get_all; diff --git a/src/platform/nm-linux-platform.c b/src/platform/nm-linux-platform.c index 0738e9f842..d4643ffa23 100644 --- a/src/platform/nm-linux-platform.c +++ b/src/platform/nm-linux-platform.c @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -259,6 +260,88 @@ delete_kernel_object (struct nl_sock *sock, struct nl_object *object) } } +/* nm_rtnl_link_parse_info_data(): Re-fetches a link from the kernel + * and parses its IFLA_INFO_DATA using a caller-provided parser. + * + * Code is stolen from rtnl_link_get_kernel(), nl_pickup(), and link_msg_parser(). + */ + +typedef int (*NMNLInfoDataParser) (struct nlattr *info_data, gpointer parser_data); + +typedef struct { + NMNLInfoDataParser parser; + gpointer parser_data; +} NMNLInfoDataClosure; + +static struct nla_policy info_data_link_policy[IFLA_MAX + 1] = { + [IFLA_LINKINFO] = { .type = NLA_NESTED }, +}; + +static struct nla_policy info_data_link_info_policy[IFLA_INFO_MAX + 1] = { + [IFLA_INFO_DATA] = { .type = NLA_NESTED }, +}; + +static int +info_data_parser (struct nl_msg *msg, void *arg) +{ + NMNLInfoDataClosure *closure = arg; + struct nlmsghdr *n = nlmsg_hdr (msg); + struct nlattr *tb[IFLA_MAX + 1]; + struct nlattr *li[IFLA_INFO_MAX + 1]; + int err; + + if (!nlmsg_valid_hdr (n, sizeof (struct ifinfomsg))) + return -NLE_MSG_TOOSHORT; + + err = nlmsg_parse (n, sizeof (struct ifinfomsg), tb, IFLA_MAX, info_data_link_policy); + if (err < 0) + return err; + + if (!tb[IFLA_LINKINFO]) + return -NLE_MISSING_ATTR; + + err = nla_parse_nested (li, IFLA_INFO_MAX, tb[IFLA_LINKINFO], info_data_link_info_policy); + if (err < 0) + return err; + + if (!li[IFLA_INFO_DATA]) + return -NLE_MISSING_ATTR; + + return closure->parser (li[IFLA_INFO_DATA], closure->parser_data); +} + +static int +nm_rtnl_link_parse_info_data (struct nl_sock *sk, int ifindex, + NMNLInfoDataParser parser, gpointer parser_data) +{ + NMNLInfoDataClosure data = { .parser = parser, .parser_data = parser_data }; + struct nl_msg *msg = NULL; + struct nl_cb *cb; + int err; + + err = rtnl_link_build_get_request (ifindex, NULL, &msg); + if (err < 0) + return err; + + err = nl_send_auto (sk, msg); + nlmsg_free (msg); + if (err < 0) + return err; + + cb = nl_cb_clone (nl_socket_get_cb (sk)); + if (cb == NULL) + return -NLE_NOMEM; + nl_cb_set (cb, NL_CB_VALID, NL_CB_CUSTOM, info_data_parser, &data); + + err = nl_recvmsgs (sk, cb); + nl_cb_put (cb); + if (err < 0) + return err; + + nl_wait_for_ack (sk); + return 0; +} + /******************************************************************/ /* Object type specific utilities */ @@ -272,6 +355,10 @@ type_to_string (NMLinkType type) return "dummy"; case NM_LINK_TYPE_IFB: return "ifb"; + case NM_LINK_TYPE_MACVLAN: + return "macvlan"; + case NM_LINK_TYPE_MACVTAP: + return "macvtap"; case NM_LINK_TYPE_TAP: return "tap"; case NM_LINK_TYPE_TUN: @@ -333,6 +420,10 @@ link_extract_type (struct rtnl_link *rtnllink, const char **out_name) return_type (NM_LINK_TYPE_DUMMY, "dummy"); else if (!strcmp (type, "ifb")) return_type (NM_LINK_TYPE_IFB, "ifb"); + else if (!strcmp (type, "macvlan")) + return_type (NM_LINK_TYPE_MACVLAN, "macvlan"); + else if (!strcmp (type, "macvtap")) + return_type (NM_LINK_TYPE_MACVTAP, "macvtap"); else if (!strcmp (type, "tun")) { NMPlatformTunProperties props; @@ -1488,6 +1579,63 @@ tun_get_properties (NMPlatform *platform, int ifindex, NMPlatformTunProperties * return TRUE; } +static const struct nla_policy macvlan_info_policy[IFLA_MACVLAN_MAX + 1] = { + [IFLA_MACVLAN_MODE] = { .type = NLA_U32 }, + [IFLA_MACVLAN_FLAGS] = { .type = NLA_U16 }, +}; + +static int +macvlan_info_data_parser (struct nlattr *info_data, gpointer parser_data) +{ + NMPlatformMacvlanProperties *props = parser_data; + struct nlattr *tb[IFLA_MACVLAN_MAX + 1]; + int err; + + err = nla_parse_nested (tb, IFLA_MACVLAN_MAX, info_data, + (struct nla_policy *) macvlan_info_policy); + if (err < 0) + return err; + + switch (nla_get_u32 (tb[IFLA_MACVLAN_MODE])) { + case MACVLAN_MODE_PRIVATE: + props->mode = "private"; + break; + case MACVLAN_MODE_VEPA: + props->mode = "vepa"; + break; + case MACVLAN_MODE_BRIDGE: + props->mode = "bridge"; + break; + case MACVLAN_MODE_PASSTHRU: + props->mode = "passthru"; + break; + default: + return -NLE_PARSE_ERR; + } + + props->no_promisc = !!(nla_get_u16 (tb[IFLA_MACVLAN_FLAGS]) & MACVLAN_FLAG_NOPROMISC); + + return 0; +} + +static gboolean +macvlan_get_properties (NMPlatform *platform, int ifindex, NMPlatformMacvlanProperties *props) +{ + NMLinuxPlatformPrivate *priv = NM_LINUX_PLATFORM_GET_PRIVATE (platform); + auto_nl_object struct rtnl_link *rtnllink; + int err; + + rtnllink = link_get (platform, ifindex); + if (!rtnllink) + return FALSE; + + props->parent_ifindex = rtnl_link_get_link (rtnllink); + + err = nm_rtnl_link_parse_info_data (priv->nlh, ifindex, + macvlan_info_data_parser, props); + return (err == 0); +} + /******************************************************************/ static int @@ -1972,6 +2120,7 @@ nm_linux_platform_class_init (NMLinuxPlatformClass *klass) platform_class->veth_get_properties = veth_get_properties; platform_class->tun_get_properties = tun_get_properties; + platform_class->macvlan_get_properties = macvlan_get_properties; platform_class->ip4_address_get_all = ip4_address_get_all; platform_class->ip6_address_get_all = ip6_address_get_all; diff --git a/src/platform/nm-platform.c b/src/platform/nm-platform.c index b93b115f3e..f030b85f9d 100644 --- a/src/platform/nm-platform.c +++ b/src/platform/nm-platform.c @@ -923,6 +923,17 @@ nm_platform_tun_get_properties (int ifindex, NMPlatformTunProperties *props) return klass->tun_get_properties (platform, ifindex, props); } +gboolean +nm_platform_macvlan_get_properties (int ifindex, NMPlatformMacvlanProperties *props) +{ + reset_error (); + + g_return_val_if_fail (ifindex > 0, FALSE); + g_return_val_if_fail (props != NULL, FALSE); + + return klass->macvlan_get_properties (platform, ifindex, props); +} + /******************************************************************/ GArray * diff --git a/src/platform/nm-platform.h b/src/platform/nm-platform.h index 0866f3cdc6..826856b9ed 100644 --- a/src/platform/nm-platform.h +++ b/src/platform/nm-platform.h @@ -59,6 +59,8 @@ typedef enum { NM_LINK_TYPE_DUMMY, NM_LINK_TYPE_IFB, NM_LINK_TYPE_LOOPBACK, + NM_LINK_TYPE_MACVLAN, + NM_LINK_TYPE_MACVTAP, NM_LINK_TYPE_TAP, NM_LINK_TYPE_TUN, NM_LINK_TYPE_VETH, @@ -126,6 +128,12 @@ typedef struct { gboolean multi_queue; } NMPlatformTunProperties; +typedef struct { + int parent_ifindex; + const char *mode; + gboolean no_promisc; +} NMPlatformMacvlanProperties; + /******************************************************************/ /* NMPlatform abstract class and its implementations provide a layer between @@ -209,6 +217,7 @@ typedef struct { gboolean (*veth_get_properties) (NMPlatform *, int ifindex, NMPlatformVethProperties *properties); gboolean (*tun_get_properties) (NMPlatform *, int ifindex, NMPlatformTunProperties *properties); + gboolean (*macvlan_get_properties) (NMPlatform *, int ifindex, NMPlatformMacvlanProperties *props); GArray * (*ip4_address_get_all) (NMPlatform *, int ifindex); GArray * (*ip6_address_get_all) (NMPlatform *, int ifindex); @@ -319,6 +328,7 @@ gboolean nm_platform_vlan_set_egress_map (int ifindex, int from, int to); gboolean nm_platform_veth_get_properties (int ifindex, NMPlatformVethProperties *properties); gboolean nm_platform_tun_get_properties (int ifindex, NMPlatformTunProperties *properties); +gboolean nm_platform_macvlan_get_properties (int ifindex, NMPlatformMacvlanProperties *props); GArray *nm_platform_ip4_address_get_all (int ifindex); GArray *nm_platform_ip6_address_get_all (int ifindex); diff --git a/src/platform/tests/platform.c b/src/platform/tests/platform.c index b62c768818..6661fe1abc 100644 --- a/src/platform/tests/platform.c +++ b/src/platform/tests/platform.c @@ -384,6 +384,22 @@ do_tun_get_properties (char **argv) return TRUE; } +static gboolean +do_macvlan_get_properties (char **argv) +{ + int ifindex = parse_ifindex (*argv++); + NMPlatformMacvlanProperties props; + + if (!nm_platform_macvlan_get_properties (ifindex, &props)) + return FALSE; + + printf ("parent: %d\n", props.parent_ifindex); + printf ("mode: %s\n", props.mode); + printf ("no-promisc: "); + print_boolean (props.no_promisc); + return TRUE; +} + static gboolean do_ip4_address_get_all (char **argv) { @@ -672,6 +688,8 @@ static const command_t commands[] = { "" }, { "tun-get-properties", "get tun/tap properties", do_tun_get_properties, 1, "" }, + { "macvlan-get-properties", "get macvlan properties", do_macvlan_get_properties, 1, + "" }, { "ip4-address-get-all", "print all IPv4 addresses", do_ip4_address_get_all, 1, "" }, { "ip6-address-get-all", "print all IPv6 addresses", do_ip6_address_get_all, 1, "" }, { "ip4-address-add", "add IPv4 address", do_ip4_address_add, 2, "
/" },