NetworkInador/lib/interfaces.c

804 lines
22 KiB
C
Raw Permalink Normal View History

2018-08-07 15:28:22 -05:00
/*
* interfaces.c
* This file is part of Network-inador
*
2019-12-31 11:05:50 -06:00
* Copyright (C) 2019, 2020 - Félix Arreola Rodríguez
2018-08-07 15:28:22 -05:00
*
* 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 <string.h>
2019-12-31 11:05:50 -06:00
#include <netlink/socket.h>
#include <netlink/msg.h>
2018-08-07 15:28:22 -05:00
2021-09-04 23:26:39 -05:00
#include <linux/if_arp.h>
2018-08-07 15:28:22 -05:00
#include <stdarg.h>
2018-08-07 15:28:22 -05:00
#include "network-inador-private.h"
2020-01-02 12:53:53 -06:00
#include "interfaces.h"
#include "routes.h"
2020-01-02 12:53:53 -06:00
#include "ip-address.h"
#include "wireless_if.h"
//#include "manager.h"
2021-09-04 23:26:39 -05:00
#include "network-inador-link-types.h"
2018-08-07 15:28:22 -05:00
2019-12-31 11:05:50 -06:00
static int _interfaces_receive_message_interface (struct nl_msg *msg, void *arg, int first_time);
2018-08-07 15:28:22 -05:00
2019-12-31 11:05:50 -06:00
static int _interfaces_list_first_time (struct nl_msg *msg, void *arg) {
return _interfaces_receive_message_interface (msg, arg, TRUE);
}
2018-08-07 15:28:22 -05:00
2019-12-31 11:05:50 -06:00
int interface_receive_message_newlink (struct nl_msg *msg, void *arg) {
return _interfaces_receive_message_interface (msg, arg, FALSE);
2018-08-07 15:28:22 -05:00
}
2021-09-04 23:26:39 -05:00
static uint32_t interfaces_check_link_type (Interface *iface) {
int g;
/* Revisar primero el INFO_KIND */
for (g = 0; g < (sizeof (linktypes) / sizeof (linktypes[0])); g++) {
if (linktypes[g].rtnl_type == NULL) continue;
if (strcmp (iface->rtnl_type, linktypes[g].rtnl_type) == 0) {
return linktypes[g].link_type;
2021-09-04 23:26:39 -05:00
}
}
if (iface->arp_type == ARPHRD_LOOPBACK)
return NI_LINK_TYPE_LOOPBACK;
else if (iface->arp_type == ARPHRD_INFINIBAND)
return NI_LINK_TYPE_INFINIBAND;
else if (iface->arp_type == ARPHRD_SIT)
return NI_LINK_TYPE_SIT;
else if (iface->arp_type == ARPHRD_TUNNEL6)
return NI_LINK_TYPE_IP6TNL;
else if (iface->arp_type == ARPHRD_PPP)
return NI_LINK_TYPE_PPP;
else if (iface->arp_type == ARPHRD_IEEE802154)
return NI_LINK_TYPE_WPAN;
else if (iface->arp_type == ARPHRD_6LOWPAN)
return NI_LINK_TYPE_6LOWPAN;
if (iface->is_wireless) {
return NI_LINK_TYPE_WIFI;
}
if (iface->arp_type == ARPHRD_ETHER) {
if (strncmp (iface->name, "rmnet", 5) == 0 ||
strncmp (iface->name, "rev_rmnet", 9) == 0 ||
strncmp (iface->name, "ccmni", 5) == 0)
return NI_LINK_TYPE_WWAN_NET;
return NI_LINK_TYPE_ETHERNET;
}
return NI_LINK_TYPE_UNKNOWN;
}
2019-12-31 11:05:50 -06:00
static int _interfaces_receive_message_interface (struct nl_msg *msg, void *arg, int first_time) {
struct nlmsghdr *reply;
struct ifinfomsg *iface_msg;
int remaining;
struct nlattr *attr;
NetworkInadorHandle *handle = (NetworkInadorHandle *) arg;
2021-09-04 23:26:39 -05:00
int was_new = 0, was_update = 0;
uint32_t new_flags;
2019-12-31 11:05:50 -06:00
Interface *iface;
uint32_t u32data;
2021-09-04 23:26:39 -05:00
char temp_name[128];
2018-08-07 15:28:22 -05:00
2019-12-31 11:05:50 -06:00
reply = nlmsg_hdr (msg);
2018-08-07 15:28:22 -05:00
2019-12-31 11:05:50 -06:00
if (reply->nlmsg_type != RTM_NEWLINK) return NL_SKIP;
2018-08-07 15:28:22 -05:00
2019-12-31 11:05:50 -06:00
iface_msg = nlmsg_data (reply);
2018-08-07 15:28:22 -05:00
2019-12-31 11:05:50 -06:00
iface = _interfaces_locate_by_index (handle->interfaces, iface_msg->ifi_index);
2018-08-07 15:28:22 -05:00
2019-12-31 11:05:50 -06:00
if (iface == NULL) {
/* Crear esta interfaz */
2021-09-04 23:26:39 -05:00
iface = malloc (sizeof (Interface));
memset (iface, 0, sizeof (Interface));
2022-01-02 23:07:39 -06:00
iface->handle = handle;
2018-08-07 15:28:22 -05:00
handle->interfaces = f_list_append (handle->interfaces, iface);
2018-08-23 13:31:27 -05:00
was_new = 1;
2018-08-07 15:28:22 -05:00
}
2019-12-31 11:05:50 -06:00
if (iface_msg->ifi_family == AF_BRIDGE) {
/* Tenemos un evento especial, se está agregando una interfaz a un bridge */
2019-12-31 11:05:50 -06:00
nlmsg_for_each_attr(attr, reply, sizeof (struct ifinfomsg), remaining) {
if (nla_type (attr) == IFLA_MASTER) {
if (nla_len (attr) != 4) {
/* Tamaño incorrecto para el nuevo master */
return NL_SKIP;
}
u32data = nla_get_u32 (attr);
iface->master_index = u32data;
}
}
//printf ("Interface %d agregada a la interfaz %d (bridge)\n", iface->index, iface->master_index);
was_update = 1;
//manager_send_event_interface_update (handle, iface);
2019-12-31 11:05:50 -06:00
return NL_SKIP;
}
2021-09-04 23:26:39 -05:00
iface->arp_type = iface_msg->ifi_type;
new_flags = iface_msg->ifi_flags;
2019-12-31 11:05:50 -06:00
iface->index = iface_msg->ifi_index;
2018-08-07 15:28:22 -05:00
2019-12-31 11:05:50 -06:00
nlmsg_for_each_attr(attr, reply, sizeof (struct ifinfomsg), remaining) {
switch (nla_type (attr)) {
//nla_len (Attr);
2018-08-07 15:28:22 -05:00
case IFLA_IFNAME:
// Actualizar el nombre de la interfaz */
2021-09-04 23:26:39 -05:00
strncpy (temp_name, nla_data (attr), IFNAMSIZ);
if (strcmp (temp_name, iface->name) != 0) {
was_update = 1;
strncpy (iface->name, nla_data (attr), IFNAMSIZ);
}
2018-08-07 15:28:22 -05:00
break;
case IFLA_ADDRESS:
2019-12-31 11:05:50 -06:00
if (nla_len (attr) > ETHER_ADDR_LEN) {
printf ("----- Warning, address es mayor que ETHER_ADDR_LEN\n");
continue;
}
memcpy (iface->real_hw, nla_data (attr), nla_len (attr));
2018-08-07 15:28:22 -05:00
//printf ("Interface %d has hw addr: %02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx\n", iface->ifi_index, new->real_hw[0], new->real_hw[1], new->real_hw[2], new->real_hw[3], new->real_hw[4], new->real_hw[5]);
break;
case IFLA_MASTER:
2019-12-31 11:05:50 -06:00
if (nla_len (attr) != 4) {
/* Tamaño incorrecto para el nuevo master */
continue;
}
2019-12-31 11:05:50 -06:00
if (first_time == FALSE) continue;
u32data = nla_get_u32 (attr);
iface->master_index = u32data;
2021-09-04 23:26:39 -05:00
//printf ("Interface %d has master: %d\n", iface->index, iface->master_index);
break;
case IFLA_MTU:
2019-12-31 11:05:50 -06:00
if (nla_len (attr) != 4) {
/* Tamaño incorrecto para el mtu */
continue;
2018-08-17 01:16:01 -05:00
}
2019-12-31 11:05:50 -06:00
u32data = nla_get_u32 (attr);
2021-09-04 23:26:39 -05:00
if (iface->mtu != u32data) {
iface->mtu = u32data;
was_update = 1;
}
2019-12-31 11:05:50 -06:00
//printf ("Interface %d has mtu: %u\n", iface->ifi_index, new->mtu);
2018-08-17 01:16:01 -05:00
break;
case IFLA_LINKINFO:
{
printf ("La interfaz tiene linkinfo");
2019-12-31 11:05:50 -06:00
struct nlattr *sub_attr;
int sub_remaining;
2018-08-17 01:16:01 -05:00
2019-12-31 11:05:50 -06:00
nla_for_each_nested(sub_attr, attr, sub_remaining) {
2021-09-04 23:26:39 -05:00
printf ("\tLINK_INFO attr: %i\n", nla_type (sub_attr));
2019-12-31 11:05:50 -06:00
switch (nla_type (sub_attr)) {
case IFLA_INFO_KIND:
printf ("IFLA_INFO_KIND: %s\n", nla_data (sub_attr));
2021-09-04 23:26:39 -05:00
strncpy (iface->rtnl_type, nla_data (sub_attr), sizeof (iface->rtnl_type) - 1);
iface->rtnl_type[sizeof (iface->rtnl_type) - 1] = 0;
2019-12-31 11:05:50 -06:00
break;
2018-08-17 01:16:01 -05:00
}
}
}
break;
2019-12-31 11:05:50 -06:00
case IFLA_LINK:
/* Corresponde a la interfaz real de una vlan */
if (nla_len (attr) != 4) {
/* Tamaño incorrecto para el vlan parent */
continue;
}
u32data = nla_get_u32 (attr);
iface->vlan_parent = u32data;
//printf ("Interface %d: tiene como padre a %d\n", iface->index, iface->vlan_parent);
break;
case IFLA_PROTINFO:
printf ("OoOoO Interface %i tiene IFLA_PROTINFO oOoOo: %i\n", iface->index, IFLA_PROTINFO);
break;
case IFLA_AF_SPEC:
{
struct nlattr *sub_family;
int sub_family_len;
sub_family = nla_data (attr);
remaining = nla_len (attr);
while (remaining > sizeof (sub_family)) {
sub_family_len = sub_family->nla_len;
if (sub_family_len > remaining) {
printf ("Los sub atributos se acabaron prematuramente\n");
break;
}
printf ("Interface %d, IFLA_AF_SPEC, sub attributo type: %i, tamaño: %i\n", iface->index, sub_family->nla_type, sub_family_len);
if (sub_family->nla_type == AF_INET6) {
struct nlattr *sub_attr;
int sub_remaining;
nla_for_each_nested(sub_attr, sub_family, sub_remaining) {
printf ("\tIFLA_AF_SPEC, family: %i attr: %i, len: %i\n", sub_family->nla_type, nla_type (sub_attr), sub_attr->nla_len);
unsigned char *ttt;
switch (nla_type (sub_attr)) {
case IFLA_INET6_FLAGS:
iface->inet6_data.i6_flags = nla_get_u32 (sub_attr);
break;
case IFLA_INET6_CONF:
ttt = nla_data (sub_attr);
printf ("Size of data: %i, sizeof struct: %i\n", nla_len (sub_attr), sizeof (iface->inet6_data.i6_conf));
memcpy (iface->inet6_data.i6_conf, ttt, sizeof (iface->inet6_data.i6_conf));
break;
case IFLA_INET6_CACHEINFO:
memcpy (&iface->inet6_data.i6_cacheinfo, nla_data (sub_attr), sizeof (iface->inet6_data.i6_cacheinfo));
break;
case IFLA_INET6_ADDR_GEN_MODE:
iface->inet6_data.i6_addr_gen_mode = nla_get_u8 (sub_attr);
break;
}
}
}
/* Avanzar a la siguiente familia presente */
remaining -= RTA_ALIGN (sub_family_len);
sub_family = (struct nlattr *) (((char *) sub_family) + RTA_ALIGN (sub_family_len));
}
}
break;
/*default:
printf ("RTA Attribute \"%hu\" no procesado\n", nla_type (attr));*/
2018-08-07 15:28:22 -05:00
}
}
2021-09-04 23:26:39 -05:00
if (iface->flags != new_flags) {
iface->flags = new_flags;
route_ask_delayed_delroute (handle);
2021-09-04 23:26:39 -05:00
was_update = 1;
}
if (was_new) {
//printf ("+++++ Interfaz nueva creada: %s\n", iface->name);
//wireless_interface_check (handle, iface);
2021-09-04 23:26:39 -05:00
iface->link_type = interfaces_check_link_type (iface);
//manager_send_event_interface_add (handle, iface);
2021-09-04 23:26:39 -05:00
} else if (was_update) {
//manager_send_event_interface_update (handle, iface);
}
2019-12-31 11:05:50 -06:00
return NL_SKIP;
}
Interface * _interfaces_locate_by_index (FList *list, int index) {
2019-12-31 11:05:50 -06:00
Interface *iface;
FList *g;
2019-12-31 11:05:50 -06:00
for (g = list; g != NULL; g = g->next) {
iface = (Interface *) g->data;
if (iface->index == index) {
return iface;
}
}
2019-12-31 11:05:50 -06:00
return NULL;
2018-08-07 15:28:22 -05:00
}
Interface * _interfaces_locate_by_name (FList *list, const char *name) {
2021-12-28 23:21:31 -06:00
Interface *iface;
FList *g;
2021-12-28 23:21:31 -06:00
for (g = list; g != NULL; g = g->next) {
iface = (Interface *) g->data;
if (strcmp (iface->name, name) == 0) {
return iface;
}
}
return NULL;
}
2021-08-07 19:42:55 -05:00
int _interfaces_wait_ack_or_error (struct nl_msg *msg, void *arg) {
2019-12-31 11:05:50 -06:00
int *ret = (int *) arg;
struct nlmsgerr *l_err;
struct nlmsghdr *reply;
reply = nlmsg_hdr (msg);
if (reply->nlmsg_type == NLMSG_ERROR) {
l_err = nlmsg_data (reply);
*ret = l_err->error;
}
return NL_SKIP;
}
2021-08-07 19:42:55 -05:00
int _interfaces_wait_error (struct sockaddr_nl *nla, struct nlmsgerr *l_err, void *arg) {
2019-12-31 11:05:50 -06:00
int *ret = (int *) arg;
*ret = l_err->error;
return NL_SKIP;
}
int interface_receive_message_dellink (struct nl_msg *msg, void *arg) {
NetworkInadorHandle *handle = (NetworkInadorHandle *) arg;
Interface *iface;
struct ifinfomsg *iface_msg;
int remaining;
uint32_t u32data;
struct nlmsghdr *reply;
struct nlattr *attr;
reply = nlmsg_hdr (msg);
2021-09-04 23:26:39 -05:00
if (reply->nlmsg_type != RTM_DELLINK) return NL_SKIP;
2019-12-31 11:05:50 -06:00
iface_msg = nlmsg_data (reply);
iface = _interfaces_locate_by_index (handle->interfaces, iface_msg->ifi_index);
if (iface == NULL) {
2018-08-07 15:28:22 -05:00
printf ("Error, solicitaron eliminar interfaz que ya no existe\n");
2019-12-31 11:05:50 -06:00
return NL_SKIP;
2018-08-07 15:28:22 -05:00
}
2019-12-31 11:05:50 -06:00
if (iface_msg->ifi_family == AF_BRIDGE) {
/* Tenemos un evento especial, se está eliminando una interfaz de un bridge */
nlmsg_for_each_attr(attr, reply, sizeof (struct ifinfomsg), remaining) {
if (nla_type (attr) == IFLA_MASTER) {
if (nla_len (attr) != 4) {
/* Tamaño incorrecto para el nuevo master */
return NL_SKIP;
}
u32data = nla_get_u32 (attr);
iface->master_index = 0;
}
}
//printf ("Interface %d se sacó de su bridge\n", iface->index);
2019-12-31 11:05:50 -06:00
/* Generar EVENTO AQUI */
//manager_send_event_interface_update (handle, iface);
2019-12-31 11:05:50 -06:00
return NL_SKIP;
}
route_ask_delayed_delroute (handle);
//printf ("----- Interfaz eliminada: %s\n", iface->name);
2021-09-04 23:26:39 -05:00
handle->interfaces = f_list_remove (handle->interfaces, iface);
2018-08-07 15:28:22 -05:00
2020-01-02 12:53:53 -06:00
/* Antes de eliminar la interfaz, eliminar la lista ligada de todas las direcciones IP */
f_list_free_full (iface->address, free);
2021-09-04 23:26:39 -05:00
//manager_send_event_interface_del (handle, iface->index);
2020-01-02 12:53:53 -06:00
2021-09-04 23:26:39 -05:00
free (iface);
2019-12-31 11:05:50 -06:00
}
int interfaces_change_mac_address (NetworkInadorHandle *handle, int index, void *new_mac) {
/* ETHER_ADDR_LEN */
struct nl_msg * msg;
struct ifinfomsg iface_hdr;
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");
2018-08-07 15:28:22 -05:00
2019-12-31 11:05:50 -06:00
return -1;
2018-08-07 15:28:22 -05:00
}
2019-12-31 11:05:50 -06:00
iface_hdr.ifi_family = AF_UNSPEC;
2021-09-04 23:26:39 -05:00
iface_hdr.ifi_type = iface->arp_type;
2019-12-31 11:05:50 -06:00
iface_hdr.ifi_index = iface->index;
iface_hdr.ifi_flags = iface->flags;
iface_hdr.ifi_change = 0xFFFFFFFF;
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);
2018-08-07 15:28:22 -05:00
2019-12-31 11:05:50 -06:00
return -1;
}
ret = nla_put (msg, IFLA_ADDRESS, ETHER_ADDR_LEN, new_mac);
if (ret != 0) {
nlmsg_free (msg);
2018-08-07 15:28:22 -05:00
2019-12-31 11:05:50 -06:00
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) {
2019-12-31 11:05:50 -06:00
return -1;
2018-08-07 15:28:22 -05:00
}
2019-12-31 11:05:50 -06:00
return 0;
2018-08-07 15:28:22 -05:00
}
2019-12-31 11:05:50 -06:00
int interfaces_change_mtu (NetworkInadorHandle *handle, int index, uint32_t new_mtu) {
/* ETHER_ADDR_LEN */
struct nl_msg * msg;
struct ifinfomsg iface_hdr;
int ret, error;
2018-08-07 15:28:22 -05:00
Interface *iface;
2019-12-31 11:05:50 -06:00
iface = _interfaces_locate_by_index (handle->interfaces, index);
2018-08-07 15:28:22 -05:00
2019-12-31 11:05:50 -06:00
if (iface == NULL) {
//printf ("Error, solicitaron operación sobre interfaz que no existe\n");
2019-12-31 11:05:50 -06:00
return -1;
}
2018-08-07 15:28:22 -05:00
2019-12-31 11:05:50 -06:00
iface_hdr.ifi_family = AF_UNSPEC;
2021-09-04 23:26:39 -05:00
iface_hdr.ifi_type = iface->arp_type;
2019-12-31 11:05:50 -06:00
iface_hdr.ifi_index = iface->index;
iface_hdr.ifi_flags = iface->flags;
iface_hdr.ifi_change = 0xFFFFFFFF;
2018-08-07 15:28:22 -05:00
2019-12-31 11:05:50 -06:00
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;
2018-08-07 15:28:22 -05:00
}
2019-12-31 11:05:50 -06:00
ret = nla_put (msg, IFLA_MTU, sizeof (new_mtu), &new_mtu);
2018-08-07 15:28:22 -05:00
2019-12-31 11:05:50 -06:00
if (ret != 0) {
nlmsg_free (msg);
2018-08-07 15:28:22 -05:00
2019-12-31 11:05:50 -06:00
return -1;
2018-08-07 15:28:22 -05:00
}
2019-12-31 11:05:50 -06:00
nl_complete_msg (handle->nl_sock_route, msg);
ret = nl_send (handle->nl_sock_route, msg);
nlmsg_free (msg);
if (ret <= 0) {
return -1;
}
2019-12-31 11:05:50 -06:00
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);
2018-08-07 15:28:22 -05:00
if (ret <= 0 || error < 0) {
2019-12-31 11:05:50 -06:00
return -1;
2018-08-07 15:28:22 -05:00
}
2019-12-31 11:05:50 -06:00
return 0;
2018-08-07 15:28:22 -05:00
}
2019-12-31 11:05:50 -06:00
static int _interfaces_change_admin_up (NetworkInadorHandle *handle, int index, int up) {
struct nl_msg * msg;
struct ifinfomsg iface_hdr;
int ret, error;
2018-08-07 15:28:22 -05:00
Interface *iface;
2019-12-31 11:05:50 -06:00
iface = _interfaces_locate_by_index (handle->interfaces, index);
2018-08-07 15:28:22 -05:00
if (iface == NULL) {
printf ("Error, solicitaron operación sobre interfaz que no existe\n");
2019-12-31 11:05:50 -06:00
return -1;
2018-08-07 15:28:22 -05:00
}
2019-12-31 11:05:50 -06:00
if (up && (iface->flags & IFF_UP)) {
/* Ningún cambio necesario, ya está activa */
return 0;
} else if (up == FALSE && ((iface->flags & IFF_UP) == 0)) {
/* Ningún cambio necesario, ya está desactivada */
return 0;
}
iface_hdr.ifi_family = AF_UNSPEC;
2021-09-04 23:26:39 -05:00
iface_hdr.ifi_type = iface->arp_type;
2019-12-31 11:05:50 -06:00
iface_hdr.ifi_index = iface->index;
if (up) {
iface_hdr.ifi_flags = iface->flags | IFF_UP;
} else {
iface_hdr.ifi_flags = iface->flags & ~IFF_UP;
}
iface_hdr.ifi_change = 0xFFFFFFFF;
2018-08-21 22:48:20 -05:00
2019-12-31 11:05:50 -06:00
msg = nlmsg_alloc_simple (RTM_NEWLINK, NLM_F_REQUEST);
ret = nlmsg_append (msg, &iface_hdr, sizeof (iface_hdr), NLMSG_ALIGNTO);
2018-08-07 15:28:22 -05:00
2019-12-31 11:05:50 -06:00
if (ret != 0) {
nlmsg_free (msg);
2018-08-07 15:28:22 -05:00
2019-12-31 11:05:50 -06:00
return -1;
2018-08-07 15:28:22 -05:00
}
2019-12-31 11:05:50 -06:00
nl_complete_msg (handle->nl_sock_route, msg);
ret = nl_send (handle->nl_sock_route, msg);
nlmsg_free (msg);
if (ret <= 0) {
return -1;
}
2019-12-31 11:05:50 -06:00
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);
2018-08-07 15:28:22 -05:00
2019-12-31 11:05:50 -06:00
nl_recvmsgs_default (handle->nl_sock_route);
if (ret <= 0 || error < 0) {
2019-12-31 11:05:50 -06:00
return -1;
2018-08-07 15:28:22 -05:00
}
2019-12-31 11:05:50 -06:00
return 0;
2018-08-07 15:28:22 -05:00
}
2019-12-31 11:05:50 -06:00
int interfaces_change_set_up (NetworkInadorHandle *handle, int index) {
return _interfaces_change_admin_up (handle, index, TRUE);
}
2019-12-31 11:05:50 -06:00
int interfaces_change_set_down (NetworkInadorHandle *handle, int index) {
return _interfaces_change_admin_up (handle, index, FALSE);
}
2019-12-31 11:05:50 -06:00
int interfaces_change_name (NetworkInadorHandle *handle, int index, char * new_name) {
/* IFNAMSIZ */
struct nl_msg * msg;
struct ifinfomsg iface_hdr;
int ret, error;
Interface *iface;
2019-12-31 11:05:50 -06:00
iface = _interfaces_locate_by_index (handle->interfaces, index);
if (iface == NULL) {
printf ("Error, solicitaron operación sobre interfaz que no existe\n");
2019-12-31 11:05:50 -06:00
return -1;
}
2018-08-21 22:48:20 -05:00
2019-12-31 11:05:50 -06:00
if (strlen (new_name) > IFNAMSIZ) {
return -1;
2018-08-21 22:48:20 -05:00
}
2019-12-31 11:05:50 -06:00
iface_hdr.ifi_family = AF_UNSPEC;
2021-09-04 23:26:39 -05:00
iface_hdr.ifi_type = iface->arp_type;
2019-12-31 11:05:50 -06:00
iface_hdr.ifi_index = iface->index;
iface_hdr.ifi_flags = iface->flags;
iface_hdr.ifi_change = 0xFFFFFFFF;
2019-12-31 11:05:50 -06:00
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;
}
2019-12-31 11:05:50 -06:00
ret = nla_put (msg, IFLA_IFNAME, strlen (new_name) + 1, new_name);
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);
ret = nl_recvmsgs_default (handle->nl_sock_route);
if (ret <= 0 || error < 0) {
2019-12-31 11:05:50 -06:00
return -1;
}
return 0;
}
#if 0
int interfaces_set_sysctl_ipv6_params (NetworkInadorHandle *handle, int index, int param, ...) {
uint32_t new_sysctl[DEVCONF_MAX];
Interface *iface;
va_list va_list_params;
uint32_t value;
struct nl_msg * msg, *msg2, *msg3;
struct ifinfomsg iface_hdr;
int ret, error;
iface = _interfaces_locate_by_index (handle->interfaces, index);
if (iface == NULL) {
printf ("Error, solicitaron operación sobre interfaz que no existe\n");
return -1;
}
/* Copiar los sysctl previos para preservarlos */
memcpy (new_sysctl, iface->inet6_data.i6_conf, sizeof (new_sysctl));
va_start (va_list_params, param);
while (param != -1) {
value = va_arg (va_list_params, uint32_t);
if (param < DEVCONF_MAX) {
new_sysctl[param] = value;
}
param = va_arg (va_list_params, int);
}
va_end (va_list_params);
/* Preparar el mensaje de configurar la interfaz */
memset (&iface_hdr, 0, sizeof (iface_hdr));
#define LINK_ATTR_AF_SPEC (1 << 27)
iface_hdr.ifi_family = AF_UNSPEC;
iface_hdr.ifi_index = iface->index;
iface_hdr.ifi_change = LINK_ATTR_AF_SPEC;
msg = nlmsg_alloc_simple (RTM_SETLINK, NLM_F_REQUEST);
ret = nlmsg_append (msg, &iface_hdr, sizeof (iface_hdr), NLMSG_ALIGNTO);
if (ret != 0) {
nlmsg_free (msg);
return -1;
}
/* FIXME: Aquí el nesting doble o triple */
msg2 = nlmsg_alloc_simple (RTM_NEWLINK, NLM_F_REQUEST);
msg3 = nlmsg_alloc_simple (RTM_NEWLINK, NLM_F_REQUEST);
nla_put (msg3, IFLA_INET6_CONF, sizeof (new_sysctl), new_sysctl);
nla_put_nested (msg2, AF_INET6 | NLA_F_NESTED, msg3);
nla_put_nested (msg, IFLA_AF_SPEC | NLA_F_NESTED, msg2);
nl_complete_msg (handle->nl_sock_route, msg);
ret = nl_send (handle->nl_sock_route, msg);
nlmsg_free (msg);
nlmsg_free (msg2);
nlmsg_free (msg3);
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;
}
#endif
2019-12-31 11:05:50 -06:00
void interfaces_init (NetworkInadorHandle *handle) {
/* Si es la primera vez que nos llaman, descargar una primera lista de interfaces */
struct nl_msg * msg;
struct rtgenmsg rt_hdr = {
.rtgen_family = AF_PACKET,
};
int ret;
msg = nlmsg_alloc_simple (RTM_GETLINK, NLM_F_REQUEST | NLM_F_DUMP);
ret = nlmsg_append (msg, &rt_hdr, sizeof (rt_hdr), NLMSG_ALIGNTO);
if (ret != 0) {
return;
2018-08-07 15:28:22 -05:00
}
2019-12-31 11:05:50 -06:00
nl_complete_msg (handle->nl_sock_route, msg);
ret = nl_send (handle->nl_sock_route, msg);
nlmsg_free (msg);
nl_socket_modify_cb (handle->nl_sock_route, NL_CB_VALID, NL_CB_CUSTOM, _interfaces_list_first_time, handle);
nl_recvmsgs_default (handle->nl_sock_route);
2020-01-02 12:53:53 -06:00
ip_address_init (handle);
2018-08-07 15:28:22 -05:00
}
2022-02-06 23:24:09 -06:00
void interfaces_clean_up (NetworkInadorHandle *handle) {
FList *g;
2022-02-06 23:24:09 -06:00
Interface *iface;
for (g = handle->interfaces; g != NULL; g = g->next) {
iface = (Interface *) g->data;
/* Antes de eliminar la interfaz, eliminar la lista ligada de todas las direcciones IP */
f_list_free_full (iface->address, free);
2022-02-06 23:24:09 -06:00
iface->address = NULL;
}
f_list_free_full (handle->interfaces, free);
2022-02-06 23:24:09 -06:00
handle->interfaces = NULL;
}