#include <net/if.h>
#include <net/if_arp.h>
+#include <limits.h>
#include <arpa/inet.h>
#include <netinet/ether.h>
#include <netinet/in.h>
#include <linux/veth.h>
#include <linux/version.h>
+#include <sched.h>
+
#ifndef RTN_FAILED_POLICY
#define RTN_FAILED_POLICY 12
#endif
#include "netifd.h"
#include "device.h"
#include "system.h"
+#include "utils.h"
struct event_socket {
struct uloop_fd uloop;
if (groups)
nl_join_groups(sock, groups);
- if (nl_connect(sock, protocol))
+ if (nl_connect(sock, protocol)) {
+ nl_socket_free(sock);
return NULL;
+ }
return sock;
}
struct clear_data *clr = arg;
struct nlmsghdr *hdr = nlmsg_hdr(msg);
bool (*cb)(struct nlmsghdr *, int ifindex);
- int type;
+ int type, ret;
switch(clr->type) {
case RTM_GETADDR:
D(SYSTEM, "Remove %s from device %s\n",
type == RTM_DELADDR ? "an address" : "a route",
clr->dev->ifname);
+
memcpy(nlmsg_hdr(clr->msg), hdr, hdr->nlmsg_len);
hdr = nlmsg_hdr(clr->msg);
hdr->nlmsg_type = type;
hdr->nlmsg_flags = NLM_F_REQUEST;
nl_socket_disable_auto_ack(sock_rtnl);
- nl_send_auto_complete(sock_rtnl, clr->msg);
+ ret = nl_send_auto_complete(sock_rtnl, clr->msg);
+ if (ret < 0) {
+ if (type == RTM_DELRULE)
+ D(SYSTEM, "Error deleting a rule: %d\n", ret);
+ else
+ D(SYSTEM, "Error deleting %s from device '%s': %d\n",
+ type == RTM_DELADDR ? "an address" : "a route",
+ clr->dev->ifname, ret);
+ }
+
nl_socket_enable_auto_ack(sock_rtnl);
return NL_SKIP;
system_if_clear_entries(struct device *dev, int type, int af)
{
struct clear_data clr;
- struct nl_cb *cb = nl_cb_alloc(NL_CB_DEFAULT);
+ struct nl_cb *cb;
struct rtmsg rtm = {
.rtm_family = af,
.rtm_flags = RTM_F_CLONED,
return;
}
+ cb = nl_cb_alloc(NL_CB_DEFAULT);
if (!cb)
return;
nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, cb_finish_event, &pending);
nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &pending);
- nl_send_auto_complete(sock_rtnl, clr.msg);
+ if (nl_send_auto_complete(sock_rtnl, clr.msg) < 0)
+ goto free;
+
while (pending > 0)
nl_recvmsgs(sock_rtnl, cb);
+free:
nlmsg_free(clr.msg);
out:
nl_cb_put(cb);
return -ENOMEM;
}
+int system_link_netns_move(struct device *dev, int netns_fd, const char *target_ifname)
+{
+ struct nl_msg *msg;
+ struct ifinfomsg iim = {
+ .ifi_family = AF_UNSPEC,
+ };
+
+ if (!dev)
+ return -1;
+
+ iim.ifi_index = system_if_resolve(dev);
+ msg = nlmsg_alloc_simple(RTM_NEWLINK, NLM_F_REQUEST);
+
+ if (!msg)
+ return -1;
+
+ nlmsg_append(msg, &iim, sizeof(iim), 0);
+ if (target_ifname)
+ nla_put_string(msg, IFLA_IFNAME, target_ifname);
+
+ nla_put_u32(msg, IFLA_NET_NS_FD, netns_fd);
+ return system_rtnl_call(msg);
+}
+
static int system_link_del(const char *ifname)
{
struct nl_msg *msg;
return system_link_del(macvlan->ifname);
}
+int system_netns_open(const pid_t target_ns)
+{
+ char pid_net_path[PATH_MAX];
+
+ snprintf(pid_net_path, sizeof(pid_net_path), "/proc/%u/ns/net", target_ns);
+
+ return open(pid_net_path, O_RDONLY);
+}
+
+int system_netns_set(int netns_fd)
+{
+ return setns(netns_fd, CLONE_NEWNET);
+}
+
int system_veth_add(struct device *veth, struct veth_config *cfg)
{
struct nl_msg *msg;
int system_vlandev_add(struct device *vlandev, struct device *dev, struct vlandev_config *cfg)
{
struct nl_msg *msg;
- struct nlattr *linkinfo, *data;
+ struct nlattr *linkinfo, *data, *qos;
struct ifinfomsg iim = { .ifi_family = AF_UNSPEC };
+ struct vlan_qos_mapping *elem;
+ struct ifla_vlan_qos_mapping nl_qos_map;
int rv;
msg = nlmsg_alloc_simple(RTM_NEWLINK, NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL);
netifd_log_message(L_WARNING, "%s Your kernel is older than linux 3.10.0, 802.1ad is not supported defaulting to 802.1q", vlandev->type->name);
#endif
+ if (!(qos = nla_nest_start(msg, IFLA_VLAN_INGRESS_QOS)))
+ goto nla_put_failure;
+
+ vlist_simple_for_each_element(&cfg->ingress_qos_mapping_list, elem, node) {
+ nl_qos_map.from = elem->from;
+ nl_qos_map.to = elem->to;
+ nla_put(msg, IFLA_VLAN_QOS_MAPPING, sizeof(nl_qos_map), &nl_qos_map);
+ }
+ nla_nest_end(msg, qos);
+
+ if (!(qos = nla_nest_start(msg, IFLA_VLAN_EGRESS_QOS)))
+ goto nla_put_failure;
+
+ vlist_simple_for_each_element(&cfg->egress_qos_mapping_list, elem, node) {
+ nl_qos_map.from = elem->from;
+ nl_qos_map.to = elem->to;
+ nla_put(msg, IFLA_VLAN_QOS_MAPPING, sizeof(nl_qos_map), &nl_qos_map);
+ }
+ nla_nest_end(msg, qos);
+
nla_nest_end(msg, data);
nla_nest_end(msg, linkinfo);
nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, cb_if_check_ack, &chk);
nl_cb_err(cb, NL_CB_CUSTOM, cb_if_check_error, &chk);
- nl_send_auto_complete(sock_rtnl, msg);
+ ret = nl_send_auto_complete(sock_rtnl, msg);
+ if (ret < 0)
+ goto free;
+
while (chk.pending > 0)
nl_recvmsgs(sock_rtnl, cb);
struct timespec ts;
struct timeval tv;
- if (syscall(__NR_clock_gettime, CLOCK_MONOTONIC, &ts) == 0)
+ if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0)
return ts.tv_sec;
if (gettimeofday(&tv, NULL) == 0)
struct blob_attr *cur;
int ret = 0;
- nlm = nlmsg_alloc_simple(RTM_NEWLINK, NLM_F_REQUEST | NLM_F_ACK | NLM_F_CREATE | NLM_F_EXCL);
+ nlm = nlmsg_alloc_simple(RTM_NEWLINK, NLM_F_REQUEST | NLM_F_REPLACE | NLM_F_CREATE);
if (!nlm)
return -1;
if (!strcmp(str, "greip") || !strcmp(str, "gretapip") ||
!strcmp(str, "greip6") || !strcmp(str, "gretapip6") ||
!strcmp(str, "vtiip") || !strcmp(str, "vtiip6") ||
- !strcmp(str, "vxlan") || !strcmp(str, "vxlan6"))
+ !strcmp(str, "vxlan") || !strcmp(str, "vxlan6") ||
+ !strcmp(str, "xfrm"))
return system_link_del(name);
else
return tunnel_ioctl(name, SIOCDELTUNNEL, NULL);