167 lines
4.1 KiB
C
167 lines
4.1 KiB
C
/*
|
|
* 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 "network-inador-private.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;
|
|
}
|