From a4f95985318974c6bbdb265b5083f40063d055d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20Arreola=20Rodr=C3=ADguez?= Date: Sun, 7 Jan 2024 19:16:40 -0600 Subject: [PATCH] Agrego interfaces veth. --- src/Makefile.am | 1 + src/dhcp_client.c | 4 +- src/main.c | 1 + src/veth.c | 166 ++++++++++++++++++++++++++++++++++++++++++++++ src/veth.h | 29 ++++++++ 5 files changed, 200 insertions(+), 1 deletion(-) create mode 100644 src/veth.c create mode 100644 src/veth.h diff --git a/src/Makefile.am b/src/Makefile.am index fe614bf..9a8157c 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -9,6 +9,7 @@ network_inador_SOURCES = main.c \ interfaces.c interfaces.h \ ip-address.c ip-address.h \ bridge.c bridge.h \ + veth.c veth.h \ manager.c manager.h \ dhcp_client.c dhcp_client.h \ routes.c routes.h \ diff --git a/src/dhcp_client.c b/src/dhcp_client.c index 743a4bd..27eed6e 100644 --- a/src/dhcp_client.c +++ b/src/dhcp_client.c @@ -97,6 +97,7 @@ void interfaces_dhcp_client_killed_cb (GPid pid, gint status, gpointer data) { } interfaces_dhcp_clear_info (&iface->dhcpc); + /* FIXME: ¿Debemos limpiar la información de DNS si no la movimos? */ resolv_manager_clear_dhcp_nameservers (iface->handle, iface); /* Enviar actualización de estado aquí */ @@ -345,7 +346,7 @@ void interfaces_dhcp_client_internal_feed_from_client (NetworkInadorHandle *hand dns_changed = 1; } - if (dns_changed) { + if (dns_changed && (iface->dhcpc.flags & DHCP_CLIENT_FLAG_DONT_ADD_DNS_INFO) == 0) { resolv_manager_process_dhcp_nameservers (handle, iface); } @@ -372,6 +373,7 @@ void interfaces_dhcp_client_internal_feed_from_client (NetworkInadorHandle *hand /* Cuando entramos a SELECTING, debemos limpiar los DNS del resolv.conf */ interfaces_dhcp_clear_info (&iface->dhcpc); + /* FIXME: ¿Debemos limpiar la información DNS si no la movimos? */ resolv_manager_clear_dhcp_nameservers (handle, iface); } diff --git a/src/main.c b/src/main.c index 27bda6c..8cebd2d 100644 --- a/src/main.c +++ b/src/main.c @@ -47,6 +47,7 @@ #include "dhcp_client.h" #include "routes.h" #include "resolv_manager.h" +#include "veth.h" /* Usados para salir en caso de una señal */ static int sigterm_pipe_fds[2] = { -1, -1 }; diff --git a/src/veth.c b/src/veth.c new file mode 100644 index 0000000..ac93fca --- /dev/null +++ b/src/veth.c @@ -0,0 +1,166 @@ +/* + * veth.c + * This file is part of Network-inador + * + * Copyright (C) 2023 - Félix Arreola Rodríguez + * + * Network-inador 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. + * + * Network-inador 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 Network-inador; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301 USA + */ + +#include +#include +#include + +#include +#include + +#include +#include + +#include +#include +#include +#include +#include + +#include "common.h" +#include "interfaces.h" + +int interfaces_veth_create (NetworkInadorHandle *handle, const char *link_name, const char *peer_name) { + struct nl_msg * msg; + struct ifinfomsg iface_hdr, peer_hdr; + struct nlattr *info, *data, *peer; + int ret, error, len; + const char *type = "veth"; + + if (link_name != NULL) { + /* Validar la longitud del nombre, por seguridad */ + len = strlen (link_name) + 1; + if (len == 1) { + /* Nombre muy corto */ + return -1; + } + if (len > IFNAMSIZ) { + /* Nombre muy largo */ + return -1; + } + } + + if (peer_name != NULL) { + /* Validar la longitud del nombre, por seguridad */ + len = strlen (peer_name) + 1; + if (len == 1) { + /* Nombre muy corto */ + return -1; + } + if (len > IFNAMSIZ) { + /* Nombre muy largo */ + return -1; + } + } + + memset (&iface_hdr, 0, sizeof (iface_hdr)); + iface_hdr.ifi_family = AF_UNSPEC; + iface_hdr.ifi_index = 0; + + msg = nlmsg_alloc_simple (RTM_NEWLINK, NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL); + ret = nlmsg_append (msg, &iface_hdr, sizeof (iface_hdr), NLMSG_ALIGNTO); + + if (ret != 0) { + nlmsg_free (msg); + + return -1; + } + + /* Anexar el nombre, si es que especificaron uno */ + if (link_name != NULL) { + ret = nla_put (msg, IFLA_IFNAME, strlen (link_name) + 1, link_name); + + if (ret != 0) { + nlmsg_free (msg); + + return -1; + } + } + + info = nla_nest_start (msg, IFLA_LINKINFO); + + if (info == NULL) { + nlmsg_free (msg); + + return -1; + } + + //ret = nla_put_string (msg, IFLA_INFO_KIND, type); + ret = nla_put (msg, IFLA_INFO_KIND, strlen (type), type); + + if (ret != 0) { + nla_nest_end (msg, info); + nlmsg_free (msg); + + return -1; + } + + /* Ver cómo anexar IFLA_INFO_DATA */ + data = nla_nest_start (msg, IFLA_INFO_DATA); + + /* Vaciar la estructura del peer */ + memset (&peer_hdr, 0, sizeof (peer_hdr)); + peer_hdr.ifi_family = AF_UNSPEC; + peer_hdr.ifi_index = 0; + + peer = nla_nest_start (msg, VETH_INFO_PEER); + nlmsg_append (msg, &peer_hdr, sizeof (peer_hdr), 0); + + if (peer_name != NULL) { + ret = nla_put (msg, IFLA_IFNAME, strlen (peer_name) + 1, peer_name); + + if (ret != 0) { + nlmsg_free (msg); + + return -1; + } + } + + nla_nest_end (msg, peer); /* Cerrar el VETH_INFO_PEER */ + + nla_nest_end (msg, data); /* Cerrar el IFLA_INFO_DATA */ + + nla_nest_end (msg, info); /* Cerrar el IFLA_LINKINFO */ + + nl_complete_msg (handle->nl_sock_route, msg); + + ret = nl_send (handle->nl_sock_route, msg); + + nlmsg_free (msg); + if (ret <= 0) { + return -1; + } + + error = 0; + nl_socket_modify_cb (handle->nl_sock_route, NL_CB_VALID, NL_CB_CUSTOM, _interfaces_wait_ack_or_error, &error); + nl_socket_modify_cb (handle->nl_sock_route, NL_CB_INVALID, NL_CB_CUSTOM, _interfaces_wait_ack_or_error, &error); + nl_socket_modify_cb (handle->nl_sock_route, NL_CB_ACK, NL_CB_CUSTOM, _interfaces_wait_ack_or_error, &error); + nl_socket_modify_err_cb (handle->nl_sock_route, NL_CB_CUSTOM, _interfaces_wait_error, &error); + + nl_recvmsgs_default (handle->nl_sock_route); + + if (ret <= 0 || error < 0) { + return -1; + } + + return 0; +} diff --git a/src/veth.h b/src/veth.h new file mode 100644 index 0000000..1d0f9c5 --- /dev/null +++ b/src/veth.h @@ -0,0 +1,29 @@ +/* + * veth.h + * This file is part of Network-inador + * + * Copyright (C) 2023 - Félix Arreola Rodríguez + * + * Network-inador 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. + * + * Network-inador 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 Network-inador; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301 USA + */ + +#ifndef __VETH_H__ +#define __VETH_H__ + +int interfaces_veth_create (NetworkInadorHandle *handle, const char *link, const char *peer); + +#endif /* __VETH_H__ */ +