NetworkInador/src/bridge.c

300 lines
7.5 KiB
C
Raw Normal View History

2021-08-07 19:42:55 -05:00
/*
* bridge.c
* This file is part of Network-inador
*
* Copyright (C) 2018 - 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 "common.h"
#include "interfaces.h"
int interfaces_bridge_create (NetworkInadorHandle *handle, const char *name) {
struct nl_msg * msg;
struct ifinfomsg iface_hdr;
struct nlattr *info;
int ret, error, len;
const char *type = "bridge";
if (name != NULL) {
/* Validar la longitud del nombre, por seguridad */
len = strlen (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 (name != NULL) {
ret = nla_put (msg, IFLA_IFNAME, strlen (name) + 1, name);
if (ret != 0) {
nlmsg_free (msg);
return -1;
}
}
2021-08-07 19:42:55 -05:00
info = nla_nest_start (msg, IFLA_LINKINFO);
if (info == NULL) {
nlmsg_free (msg);
return -1;
}
ret = nla_put_string (msg, IFLA_INFO_KIND, type);
if (ret != 0) {
nla_nest_end (msg, info);
nlmsg_free (msg);
return -1;
}
nla_nest_end (msg, info);
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;
}
int interfaces_bridge_set_master_interface (NetworkInadorHandle *handle, int master_index, int index) {
struct nl_msg * msg;
struct ifinfomsg iface_hdr;
struct nlattr *info;
int ret, error;
Interface *master_iface, *iface;
master_iface = _interfaces_locate_by_index (handle->interfaces, master_index);
iface = _interfaces_locate_by_index (handle->interfaces, index);
if (master_iface == NULL || iface == NULL) {
printf ("Error, solicitaron operación sobre interfaz que no existe\n");
return -1;
}
memset (&iface_hdr, 0, sizeof (iface_hdr));
iface_hdr.ifi_family = AF_UNSPEC;
iface_hdr.ifi_index = iface->index;
msg = nlmsg_alloc_simple (RTM_NEWLINK, NLM_F_REQUEST);
ret = nlmsg_append (msg, &iface_hdr, sizeof (iface_hdr), NLMSG_ALIGNTO);
if (ret != 0) {
nlmsg_free (msg);
return -1;
}
ret = nla_put_u32 (msg, IFLA_MASTER, master_iface->index);
if (ret != 0) {
nlmsg_free (msg);
return -1;
}
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;
}
int interfaces_bridge_remove_master_interface (NetworkInadorHandle *handle, int index) {
struct nl_msg * msg;
struct ifinfomsg iface_hdr;
struct nlattr *info;
int ret, error;
Interface *iface;
iface = _interfaces_locate_by_index (handle->interfaces, index);
if (iface == NULL) {
printf ("Error, solicitaron operación sobre interfaz que no existe\n");
return -1;
}
memset (&iface_hdr, 0, sizeof (iface_hdr));
iface_hdr.ifi_family = AF_UNSPEC;
iface_hdr.ifi_index = iface->index;
msg = nlmsg_alloc_simple (RTM_NEWLINK, NLM_F_REQUEST);
ret = nlmsg_append (msg, &iface_hdr, sizeof (iface_hdr), NLMSG_ALIGNTO);
if (ret != 0) {
nlmsg_free (msg);
return -1;
}
ret = nla_put_u32 (msg, IFLA_MASTER, 0);
if (ret != 0) {
nlmsg_free (msg);
return -1;
}
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;
}
int interfaces_bridge_delete (NetworkInadorHandle *handle, int index) {
struct nl_msg * msg;
struct ifinfomsg iface_hdr;
struct nlattr *info;
int ret, error, len;
Interface *iface;
iface = _interfaces_locate_by_index (handle->interfaces, index);
if (iface == NULL) {
printf ("Error, solicitaron operación sobre interfaz que no existe\n");
return -1;
}
memset (&iface_hdr, 0, sizeof (iface_hdr));
iface_hdr.ifi_family = AF_UNSPEC;
iface_hdr.ifi_index = iface->index;
msg = nlmsg_alloc_simple (RTM_DELLINK, NLM_F_REQUEST | NLM_F_EXCL);
ret = nlmsg_append (msg, &iface_hdr, sizeof (iface_hdr), NLMSG_ALIGNTO);
if (ret != 0) {
nlmsg_free (msg);
return -1;
}
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;
}