X-Git-Url: http://git.openwrt.org/?p=project%2Fnetifd.git;a=blobdiff_plain;f=system-linux.c;h=8b9189d27a2b974b043195a894999e5ef793b85e;hp=bb834a2d6614a8f4f278c01683379da0ea619b90;hb=268149290fcd8a52f377b21226990d3bf0e9c226;hpb=ca3f00d45e72c4b28b2ce39afc9543fa2f225020 diff --git a/system-linux.c b/system-linux.c index bb834a2..8b9189d 100644 --- a/system-linux.c +++ b/system-linux.c @@ -87,6 +87,31 @@ static void handler_rtnl_event(struct uloop_fd *u, unsigned int events) nl_recvmsgs(sock_rtnl_event, nl_cb_rtnl_event); } +static void system_set_sysctl(const char *path, const char *val) +{ + int fd; + + fd = open(path, O_WRONLY); + if (fd < 0) + return; + + write(fd, val, strlen(val)); + close(fd); +} + +static void system_set_dev_sysctl(const char *path, const char *device, const char *val) +{ + static char buf[256]; + + snprintf(buf, sizeof(buf), path, val); + system_set_sysctl(buf, val); +} + +static void system_set_disable_ipv6(struct device *dev, const char *val) +{ + system_set_dev_sysctl("/proc/sys/net/ipv6/conf/%s/disable_ipv6", dev->ifname, val); +} + // Evaluate netlink messages static int cb_rtnl_event(struct nl_msg *msg, void *arg) { @@ -138,11 +163,13 @@ static int system_bridge_if(const char *bridge, struct device *dev, int cmd, voi int system_bridge_addif(struct device *bridge, struct device *dev) { + system_set_disable_ipv6(dev, "1"); return system_bridge_if(bridge->ifname, dev, SIOCBRADDIF, NULL); } int system_bridge_delif(struct device *bridge, struct device *dev) { + system_set_disable_ipv6(dev, "0"); return system_bridge_if(bridge->ifname, dev, SIOCBRDELIF, NULL); } @@ -202,14 +229,150 @@ static int system_if_flags(const char *ifname, unsigned add, unsigned rem) return ioctl(sock_ioctl, SIOCSIFFLAGS, &ifr); } +struct clear_data { + struct nl_msg *msg; + struct device *dev; + int type; + int size; + int af; +}; + + +static bool check_ifaddr(struct nlmsghdr *hdr, int ifindex) +{ + struct ifaddrmsg *ifa = NLMSG_DATA(hdr); + + return ifa->ifa_index == ifindex; +} + +static bool check_route(struct nlmsghdr *hdr, int ifindex) +{ + struct nlattr *tb[__RTA_MAX]; + + nlmsg_parse(hdr, sizeof(struct rtmsg), tb, __RTA_MAX - 1, NULL); + if (!tb[RTA_OIF]) + return false; + + return *(int *)RTA_DATA(tb[RTA_OIF]) == ifindex; +} + +static int cb_clear_event(struct nl_msg *msg, void *arg) +{ + struct clear_data *clr = arg; + struct nlmsghdr *hdr = nlmsg_hdr(msg); + bool (*cb)(struct nlmsghdr *, int ifindex); + int type; + + switch(clr->type) { + case RTM_GETADDR: + type = RTM_DELADDR; + if (hdr->nlmsg_type != RTM_NEWADDR) + return NL_SKIP; + + cb = check_ifaddr; + break; + case RTM_GETROUTE: + type = RTM_DELROUTE; + if (hdr->nlmsg_type != RTM_NEWROUTE) + return NL_SKIP; + + cb = check_route; + break; + default: + return NL_SKIP; + } + + if (!cb(hdr, clr->dev->ifindex)) + return NL_SKIP; + + 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; + + if (!nl_send_auto_complete(sock_rtnl, clr->msg)) + nl_wait_for_ack(sock_rtnl); + + return NL_SKIP; +} + +static int +cb_finish_event(struct nl_msg *msg, void *arg) +{ + int *pending = arg; + *pending = 0; + return NL_STOP; +} + +static int +error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err, void *arg) +{ + int *pending = arg; + *pending = err->error; + return NL_STOP; +} + +static void +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 rtmsg rtm = { + .rtm_family = af, + .rtm_flags = RTM_F_CLONED, + }; + int flags = NLM_F_DUMP; + int pending = 1; + + clr.af = af; + clr.dev = dev; + clr.type = type; + switch (type) { + case RTM_GETADDR: + clr.size = sizeof(struct rtgenmsg); + break; + case RTM_GETROUTE: + clr.size = sizeof(struct rtmsg); + break; + default: + return; + } + + if (!cb) + return; + + clr.msg = nlmsg_alloc_simple(type, flags); + if (!clr.msg) + goto out; + + nlmsg_append(clr.msg, &rtm, clr.size, 0); + nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, cb_clear_event, &clr); + 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); + while (pending > 0) + nl_recvmsgs(sock_rtnl, cb); + + nlmsg_free(clr.msg); +out: + nl_cb_put(cb); +} + /* * Clear bridge (membership) state and bring down device */ void system_if_clear_state(struct device *dev) { - char buf[256]; + static char buf[256]; char *bridge; + if (dev->external) + return; + dev->ifindex = system_if_resolve(dev); if (!dev->ifindex) return; @@ -227,6 +390,12 @@ void system_if_clear_state(struct device *dev) D(SYSTEM, "Remove device '%s' from bridge '%s'\n", dev->ifname, bridge); system_bridge_if(bridge, dev, SIOCBRDELIF, NULL); } + + system_if_clear_entries(dev, RTM_GETROUTE, AF_INET); + system_if_clear_entries(dev, RTM_GETADDR, AF_INET); + system_if_clear_entries(dev, RTM_GETROUTE, AF_INET6); + system_if_clear_entries(dev, RTM_GETADDR, AF_INET6); + system_set_disable_ipv6(dev, "0"); } static inline unsigned long @@ -250,6 +419,9 @@ int system_bridge_addbr(struct device *bridge, struct bridge_config *cfg) args[1] = sec_to_jiffies(cfg->forward_delay); system_bridge_if(bridge->ifname, NULL, SIOCDEVPRIVATE, &args); + system_set_dev_sysctl("/sys/devices/virtual/net/%s/bridge/multicast_snooping", + bridge->ifname, cfg->igmp_snoop ? "1" : "0"); + if (cfg->flags & BRIDGE_OPT_AGEING_TIME) { args[0] = BRCTL_SET_AGEING_TIME; args[1] = sec_to_jiffies(cfg->ageing_time); @@ -301,9 +473,32 @@ int system_vlan_del(struct device *dev) return system_vlan(dev, -1); } -int system_if_up(struct device *dev) +static void +system_if_apply_settings(struct device *dev) { + struct ifreq ifr; + + memset(&ifr, 0, sizeof(ifr)); + strncpy(ifr.ifr_name, dev->ifname, sizeof(ifr.ifr_name)); + if (dev->flags & DEV_OPT_MTU) { + ifr.ifr_mtu = dev->mtu; + ioctl(sock_ioctl, SIOCSIFMTU, &ifr); + } + if (dev->flags & DEV_OPT_TXQUEUELEN) { + ifr.ifr_qlen = dev->txqueuelen; + ioctl(sock_ioctl, SIOCSIFTXQLEN, &ifr); + } + if (dev->flags & DEV_OPT_MACADDR) { + memcpy(&ifr.ifr_hwaddr, dev->macaddr, sizeof(dev->macaddr)); + ioctl(sock_ioctl, SIOCSIFHWADDR, &ifr); + } + dev->ifindex = system_if_resolve(dev); +} + +int system_if_up(struct device *dev) +{ + system_if_apply_settings(dev); return system_if_flags(dev->ifname, IFF_UP, 0); } @@ -370,7 +565,17 @@ static int system_addr(struct device *dev, struct device_addr *addr, int cmd) .ifa_index = dev->ifindex, }; - struct nl_msg *msg = nlmsg_alloc_simple(cmd, 0); + struct nl_msg *msg; + + dev = addr->device; + if (dev) { + if (!dev->ifindex) + return -1; + + ifa.ifa_index = dev->ifindex; + } + + msg = nlmsg_alloc_simple(cmd, 0); if (!msg) return -1; @@ -394,6 +599,7 @@ static int system_rt(struct device *dev, struct device_route *route, int cmd) int alen = ((route->flags & DEVADDR_FAMILY) == DEVADDR_INET4) ? 4 : 16; bool have_gw; unsigned int flags = 0; + int ifindex = dev->ifindex; if (alen == 4) have_gw = !!route->nexthop.in.s_addr; @@ -414,11 +620,20 @@ static int system_rt(struct device *dev, struct device_route *route, int cmd) .rtm_scope = scope, .rtm_type = (cmd == RTM_DELROUTE) ? 0: RTN_UNICAST, }; + struct nl_msg *msg; if (cmd == RTM_NEWROUTE) flags |= NLM_F_CREATE | NLM_F_REPLACE; - struct nl_msg *msg = nlmsg_alloc_simple(cmd, flags); + dev = route->device; + if (dev) { + if (!dev->ifindex) + return -1; + + ifindex = dev->ifindex; + } + + msg = nlmsg_alloc_simple(cmd, flags); if (!msg) return -1; @@ -431,7 +646,7 @@ static int system_rt(struct device *dev, struct device_route *route, int cmd) nla_put(msg, RTA_GATEWAY, alen, &route->nexthop); if (route->flags & DEVADDR_DEVICE) - nla_put_u32(msg, RTA_OIF, dev->ifindex); + nla_put_u32(msg, RTA_OIF, ifindex); return system_rtnl_call(msg); }