+/*
+ * netifd - network interface daemon
+ * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2
+ * as published by the Free Software Foundation
+ *
+ * This program 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.
+ */
#define _GNU_SOURCE
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/syscall.h>
+#include <net/if.h>
+#include <net/if_arp.h>
+
+#include <arpa/inet.h>
+#include <netinet/in.h>
+
#include <linux/rtnetlink.h>
#include <linux/sockios.h>
+#include <linux/ip.h>
#include <linux/if_vlan.h>
#include <linux/if_bridge.h>
+#include <linux/if_tunnel.h>
#include <linux/ethtool.h>
-#include <net/if_arp.h>
#include <unistd.h>
#include <string.h>
static void system_set_dev_sysctl(const char *path, const char *device, const char *val)
{
- snprintf(dev_buf, sizeof(dev_buf), path, val);
+ snprintf(dev_buf, sizeof(dev_buf), path, device);
system_set_sysctl(dev_buf, val);
}
if (dev->type != &simple_device_type)
return;
+ if (add && system_if_force_external(dev->ifname))
+ return;
+
device_set_present(dev, add);
}
static int system_rtnl_call(struct nl_msg *msg)
{
- int s = -(nl_send_auto_complete(sock_rtnl, msg)
- || nl_wait_for_ack(sock_rtnl));
+ int ret;
+
+ ret = nl_send_auto_complete(sock_rtnl, msg);
nlmsg_free(msg);
- return s;
+
+ if (ret < 0)
+ return ret;
+
+ return nl_wait_for_ack(sock_rtnl);
}
int system_bridge_delbr(struct device *bridge)
static int system_bridge_if(const char *bridge, struct device *dev, int cmd, void *data)
{
struct ifreq ifr;
+
+ memset(&ifr, 0, sizeof(ifr));
if (dev)
ifr.ifr_ifindex = dev->ifindex;
else
static int system_if_flags(const char *ifname, unsigned add, unsigned rem)
{
struct ifreq ifr;
+
+ memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
ioctl(sock_ioctl, SIOCGIFFLAGS, &ifr);
ifr.ifr_flags |= add;
}
}
-static void
+void
system_if_apply_settings(struct device *dev, struct device_settings *s)
{
struct ifreq ifr;
if (ioctl(sock_ioctl, SIOCSIFTXQLEN, &ifr) < 0)
s->flags &= ~DEV_OPT_TXQUEUELEN;
}
- if (s->flags & DEV_OPT_MACADDR) {
+ if ((s->flags & DEV_OPT_MACADDR) && !dev->external) {
ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
memcpy(&ifr.ifr_hwaddr.sa_data, s->macaddr, sizeof(s->macaddr));
if (ioctl(sock_ioctl, SIOCSIFHWADDR, &ifr) < 0)
}
}
+bool
+system_if_force_external(const char *ifname)
+{
+ char buf[64];
+ struct stat s;
+
+ snprintf(buf, sizeof(buf), "/sys/class/net/%s/phy80211", ifname);
+ return stat(buf, &s) == 0;
+}
+
int
system_if_dump_info(struct device *dev, struct blob_buf *b)
{
nlmsg_append(msg, &ifa, sizeof(ifa), 0);
nla_put(msg, IFA_LOCAL, alen, &addr->addr);
- if (v4)
- nla_put_u32(msg, IFA_BROADCAST, addr->broadcast);
+ if (v4) {
+ if (addr->broadcast)
+ nla_put_u32(msg, IFA_BROADCAST, addr->broadcast);
+ if (addr->point_to_point)
+ nla_put_u32(msg, IFA_ADDRESS, addr->point_to_point);
+ }
return system_rtnl_call(msg);
}
.rtm_family = (alen == 4) ? AF_INET : AF_INET6,
.rtm_dst_len = route->mask,
.rtm_table = RT_TABLE_MAIN,
- .rtm_protocol = RTPROT_BOOT,
+ .rtm_protocol = (route->flags & DEVADDR_KERNEL) ? RTPROT_KERNEL : RTPROT_STATIC,
.rtm_scope = scope,
.rtm_type = (cmd == RTM_DELROUTE) ? 0: RTN_UNICAST,
};
if (have_gw)
nla_put(msg, RTA_GATEWAY, alen, &route->nexthop);
- if (route->flags & DEVADDR_DEVICE)
- nla_put_u32(msg, RTA_OIF, ifindex);
+ nla_put_u32(msg, RTA_OIF, ifindex);
return system_rtnl_call(msg);
}
return 0;
}
+
+#ifndef IP_DF
+#define IP_DF 0x4000
+#endif
+
+static void tunnel_parm_init(struct ip_tunnel_parm *p)
+{
+ memset(p, 0, sizeof(*p));
+ p->iph.version = 4;
+ p->iph.ihl = 5;
+ p->iph.frag_off = htons(IP_DF);
+}
+
+static int tunnel_ioctl(const char *name, int cmd, void *p)
+{
+ struct ifreq ifr;
+
+ memset(&ifr, 0, sizeof(ifr));
+ strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
+ ifr.ifr_ifru.ifru_data = p;
+ return ioctl(sock_ioctl, cmd, &ifr);
+}
+
+int system_del_ip_tunnel(const char *name)
+{
+ struct ip_tunnel_parm p;
+
+ tunnel_parm_init(&p);
+ return tunnel_ioctl(name, SIOCDELTUNNEL, &p);
+}
+
+static int parse_ipaddr(struct blob_attr *attr, __be32 *addr)
+{
+ if (!attr)
+ return 1;
+
+ return inet_pton(AF_INET, blobmsg_data(attr), (void *) addr);
+}
+
+
+int system_add_ip_tunnel(const char *name, struct blob_attr *attr)
+{
+ struct blob_attr *tb[__TUNNEL_ATTR_MAX];
+ struct blob_attr *cur;
+ struct ip_tunnel_parm p;
+ const char *base, *str;
+ bool is_sit;
+
+ system_del_ip_tunnel(name);
+
+ tunnel_parm_init(&p);
+
+ blobmsg_parse(tunnel_attr_list.params, __TUNNEL_ATTR_MAX, tb,
+ blob_data(attr), blob_len(attr));
+
+ if (!(cur = tb[TUNNEL_ATTR_TYPE]))
+ return -EINVAL;
+ str = blobmsg_data(cur);
+ is_sit = !strcmp(str, "sit");
+
+ if (is_sit) {
+ p.iph.protocol = IPPROTO_IPV6;
+ base = "sit0";
+ } else
+ return -EINVAL;
+
+ if (!parse_ipaddr(tb[TUNNEL_ATTR_LOCAL], &p.iph.saddr))
+ return -EINVAL;
+
+ if (!parse_ipaddr(tb[TUNNEL_ATTR_REMOTE], &p.iph.daddr))
+ return -EINVAL;
+
+ if ((cur = tb[TUNNEL_ATTR_TTL])) {
+ unsigned int val = blobmsg_get_u32(cur);
+
+ if (val > 255)
+ return -EINVAL;
+
+ p.iph.ttl = val;
+ }
+
+ strncpy(p.name, name, sizeof(p.name));
+ if (tunnel_ioctl(base, SIOCADDTUNNEL, &p) < 0)
+ return -1;
+
+#ifdef SIOCADD6RD
+ cur = tb[TUNNEL_ATTR_6RD_PREFIX];
+ if (cur && is_sit) {
+ unsigned int mask;
+ struct ip_tunnel_6rd p6;
+
+ memset(&p6, 0, sizeof(p6));
+
+ if (!parse_ip_and_netmask(AF_INET6, blobmsg_data(cur),
+ &p6.prefix, &mask) || mask > 128)
+ return -EINVAL;
+ p6.prefixlen = mask;
+
+ if ((cur = tb[TUNNEL_ATTR_6RD_RELAY_PREFIX])) {
+ if (!parse_ip_and_netmask(AF_INET, blobmsg_data(cur),
+ &p6.relay_prefix, &mask) || mask > 32)
+ return -EINVAL;
+ p6.relay_prefixlen = mask;
+ }
+
+ if (tunnel_ioctl(name, SIOCADD6RD, &p6) < 0) {
+ system_del_ip_tunnel(name);
+ return -1;
+ }
+ }
+#endif
+
+ return 0;
+}