X-Git-Url: http://git.archive.openwrt.org/?p=project%2Fnetifd.git;a=blobdiff_plain;f=system-linux.c;h=af252ea64e21c68f71ccd8793bcdf4f1731aa0ec;hp=6345b7f22bc80c232a689a5a7547116fba84ccff;hb=b0b11b2295fbb8399949139d82156e123e005902;hpb=414195df0ba6046cee387517d13d77e2f4d92fec diff --git a/system-linux.c b/system-linux.c index 6345b7f..af252ea 100644 --- a/system-linux.c +++ b/system-linux.c @@ -1,3 +1,16 @@ +/* + * netifd - network interface daemon + * Copyright (C) 2012 Felix Fietkau + * + * 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 @@ -5,12 +18,19 @@ #include #include +#include +#include + +#include +#include + #include #include +#include #include #include +#include #include -#include #include #include @@ -236,10 +256,15 @@ handle_hotplug_event(struct uloop_fd *u, unsigned int events) 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) @@ -832,8 +857,12 @@ static int system_addr(struct device *dev, struct device_addr *addr, int cmd) 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); } @@ -888,14 +917,13 @@ static int system_rt(struct device *dev, struct device_route *route, int cmd) if (route->mask) nla_put(msg, RTA_DST, alen, &route->addr); - if (route->metric >= 0) + if (route->metric > 0) nla_put_u32(msg, RTA_PRIORITY, route->metric); 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); } @@ -942,3 +970,87 @@ time_t system_get_rtime(void) 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; + int cmd = SIOCADDTUNNEL; + + 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)); + + cur = tb[TUNNEL_ATTR_TYPE]; + if (!cur) + return -EINVAL; + + str = blobmsg_data(cur); + if (!strcmp(str, "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)); + return tunnel_ioctl(base, cmd, &p); +}