Agrego interfaces veth.

master
Félix Arreola Rodríguez 2024-01-07 19:16:40 -06:00
parent 050fa2893a
commit a4f9598531
5 changed files with 200 additions and 1 deletions

View File

@ -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 \

View File

@ -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);
}

View File

@ -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 };

166
src/veth.c 100644
View File

@ -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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netlink/socket.h>
#include <netlink/msg.h>
#include <net/ethernet.h>
#include <net/if_arp.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <linux/veth.h>
#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;
}

29
src/veth.h 100644
View File

@ -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__ */