3 #include <sys/socket.h>
6 #include <sys/syscall.h>
9 #include <net/if_arp.h>
11 #include <arpa/inet.h>
12 #include <netinet/in.h>
14 #include <linux/rtnetlink.h>
15 #include <linux/sockios.h>
17 #include <linux/if_vlan.h>
18 #include <linux/if_bridge.h>
19 #include <linux/if_tunnel.h>
20 #include <linux/ethtool.h>
28 #include <netlink/msg.h>
29 #include <netlink/attr.h>
30 #include <netlink/socket.h>
31 #include <libubox/uloop.h>
38 struct uloop_fd uloop;
43 static int sock_ioctl = -1;
44 static struct nl_sock *sock_rtnl = NULL;
46 static int cb_rtnl_event(struct nl_msg *msg, void *arg);
47 static void handle_hotplug_event(struct uloop_fd *u, unsigned int events);
49 static char dev_buf[256];
52 handler_nl_event(struct uloop_fd *u, unsigned int events)
54 struct event_socket *ev = container_of(u, struct event_socket, uloop);
55 nl_recvmsgs(ev->sock, ev->cb);
58 static struct nl_sock *
59 create_socket(int protocol, int groups)
63 sock = nl_socket_alloc();
68 nl_join_groups(sock, groups);
70 if (nl_connect(sock, protocol))
77 create_raw_event_socket(struct event_socket *ev, int protocol, int groups,
80 ev->sock = create_socket(protocol, groups);
84 ev->uloop.fd = nl_socket_get_fd(ev->sock);
86 uloop_fd_add(&ev->uloop, ULOOP_READ | ULOOP_EDGE_TRIGGER);
91 create_event_socket(struct event_socket *ev, int protocol,
92 int (*cb)(struct nl_msg *msg, void *arg))
94 // Prepare socket for link events
95 ev->cb = nl_cb_alloc(NL_CB_DEFAULT);
99 nl_cb_set(ev->cb, NL_CB_VALID, NL_CB_CUSTOM, cb, NULL);
101 return create_raw_event_socket(ev, protocol, 0, handler_nl_event);
104 int system_init(void)
106 static struct event_socket rtnl_event;
107 static struct event_socket hotplug_event;
109 sock_ioctl = socket(AF_LOCAL, SOCK_DGRAM, 0);
110 fcntl(sock_ioctl, F_SETFD, fcntl(sock_ioctl, F_GETFD) | FD_CLOEXEC);
112 // Prepare socket for routing / address control
113 sock_rtnl = create_socket(NETLINK_ROUTE, 0);
117 if (!create_event_socket(&rtnl_event, NETLINK_ROUTE, cb_rtnl_event))
120 if (!create_raw_event_socket(&hotplug_event, NETLINK_KOBJECT_UEVENT, 1,
121 handle_hotplug_event))
124 // Receive network link events form kernel
125 nl_socket_add_membership(rtnl_event.sock, RTNLGRP_LINK);
130 static void system_set_sysctl(const char *path, const char *val)
134 fd = open(path, O_WRONLY);
138 write(fd, val, strlen(val));
142 static void system_set_dev_sysctl(const char *path, const char *device, const char *val)
144 snprintf(dev_buf, sizeof(dev_buf), path, val);
145 system_set_sysctl(dev_buf, val);
148 static void system_set_disable_ipv6(struct device *dev, const char *val)
150 system_set_dev_sysctl("/proc/sys/net/ipv6/conf/%s/disable_ipv6", dev->ifname, val);
153 // Evaluate netlink messages
154 static int cb_rtnl_event(struct nl_msg *msg, void *arg)
156 struct nlmsghdr *nh = nlmsg_hdr(msg);
157 struct ifinfomsg *ifi = NLMSG_DATA(nh);
158 struct nlattr *nla[__IFLA_MAX];
160 if (nh->nlmsg_type != RTM_DELLINK && nh->nlmsg_type != RTM_NEWLINK)
163 nlmsg_parse(nh, sizeof(*ifi), nla, __IFLA_MAX - 1, NULL);
164 if (!nla[IFLA_IFNAME])
167 struct device *dev = device_get(RTA_DATA(nla[IFLA_IFNAME]), false);
171 dev->ifindex = ifi->ifi_index;
172 /* TODO: parse link status */
179 handle_hotplug_msg(char *data, int size)
181 const char *subsystem = NULL, *interface = NULL;
182 char *cur, *end, *sep;
187 if (!strncmp(data, "add@", 4))
189 else if (!strncmp(data, "remove@", 7))
194 skip = strlen(data) + 1;
197 for (cur = data + skip; cur < end; cur += skip) {
198 skip = strlen(cur) + 1;
200 sep = strchr(cur, '=');
205 if (!strcmp(cur, "INTERFACE"))
207 else if (!strcmp(cur, "SUBSYSTEM")) {
209 if (strcmp(subsystem, "net") != 0)
212 if (subsystem && interface)
218 dev = device_get(interface, false);
222 if (dev->type != &simple_device_type)
225 device_set_present(dev, add);
229 handle_hotplug_event(struct uloop_fd *u, unsigned int events)
231 struct event_socket *ev = container_of(u, struct event_socket, uloop);
232 struct sockaddr_nl nla;
233 unsigned char *buf = NULL;
236 while ((size = nl_recv(ev->sock, &nla, &buf, NULL)) > 0) {
238 handle_hotplug_msg((char *) buf, size);
244 static int system_rtnl_call(struct nl_msg *msg)
246 int s = -(nl_send_auto_complete(sock_rtnl, msg)
247 || nl_wait_for_ack(sock_rtnl));
252 int system_bridge_delbr(struct device *bridge)
254 return ioctl(sock_ioctl, SIOCBRDELBR, bridge->ifname);
257 static int system_bridge_if(const char *bridge, struct device *dev, int cmd, void *data)
261 ifr.ifr_ifindex = dev->ifindex;
264 strncpy(ifr.ifr_name, bridge, sizeof(ifr.ifr_name));
265 return ioctl(sock_ioctl, cmd, &ifr);
268 static bool system_is_bridge(const char *name, char *buf, int buflen)
272 snprintf(buf, buflen, "/sys/devices/virtual/net/%s/bridge", name);
273 if (stat(buf, &st) < 0)
279 static char *system_get_bridge(const char *name, char *buf, int buflen)
285 snprintf(buf, buflen, "/sys/devices/virtual/net/*/brif/%s/bridge", name);
286 if (glob(buf, GLOB_NOSORT, NULL, &gl) < 0)
289 if (gl.gl_pathc == 0)
292 len = readlink(gl.gl_pathv[0], buf, buflen);
297 path = strrchr(buf, '/');
304 int system_bridge_addif(struct device *bridge, struct device *dev)
308 system_set_disable_ipv6(dev, "1");
309 oldbr = system_get_bridge(dev->ifname, dev_buf, sizeof(dev_buf));
310 if (oldbr && !strcmp(oldbr, bridge->ifname))
313 return system_bridge_if(bridge->ifname, dev, SIOCBRADDIF, NULL);
316 int system_bridge_delif(struct device *bridge, struct device *dev)
318 system_set_disable_ipv6(dev, "0");
319 return system_bridge_if(bridge->ifname, dev, SIOCBRDELIF, NULL);
322 static int system_if_resolve(struct device *dev)
325 strncpy(ifr.ifr_name, dev->ifname, sizeof(ifr.ifr_name));
326 if (!ioctl(sock_ioctl, SIOCGIFINDEX, &ifr))
327 return ifr.ifr_ifindex;
332 static int system_if_flags(const char *ifname, unsigned add, unsigned rem)
335 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
336 ioctl(sock_ioctl, SIOCGIFFLAGS, &ifr);
337 ifr.ifr_flags |= add;
338 ifr.ifr_flags &= ~rem;
339 return ioctl(sock_ioctl, SIOCSIFFLAGS, &ifr);
351 static bool check_ifaddr(struct nlmsghdr *hdr, int ifindex)
353 struct ifaddrmsg *ifa = NLMSG_DATA(hdr);
355 return ifa->ifa_index == ifindex;
358 static bool check_route(struct nlmsghdr *hdr, int ifindex)
360 struct nlattr *tb[__RTA_MAX];
362 nlmsg_parse(hdr, sizeof(struct rtmsg), tb, __RTA_MAX - 1, NULL);
366 return *(int *)RTA_DATA(tb[RTA_OIF]) == ifindex;
369 static int cb_clear_event(struct nl_msg *msg, void *arg)
371 struct clear_data *clr = arg;
372 struct nlmsghdr *hdr = nlmsg_hdr(msg);
373 bool (*cb)(struct nlmsghdr *, int ifindex);
379 if (hdr->nlmsg_type != RTM_NEWADDR)
386 if (hdr->nlmsg_type != RTM_NEWROUTE)
395 if (!cb(hdr, clr->dev->ifindex))
398 D(SYSTEM, "Remove %s from device %s\n",
399 type == RTM_DELADDR ? "an address" : "a route",
401 memcpy(nlmsg_hdr(clr->msg), hdr, hdr->nlmsg_len);
402 hdr = nlmsg_hdr(clr->msg);
403 hdr->nlmsg_type = type;
404 hdr->nlmsg_flags = NLM_F_REQUEST;
406 if (!nl_send_auto_complete(sock_rtnl, clr->msg))
407 nl_wait_for_ack(sock_rtnl);
413 cb_finish_event(struct nl_msg *msg, void *arg)
421 error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err, void *arg)
424 *pending = err->error;
429 system_if_clear_entries(struct device *dev, int type, int af)
431 struct clear_data clr;
432 struct nl_cb *cb = nl_cb_alloc(NL_CB_DEFAULT);
435 .rtm_flags = RTM_F_CLONED,
437 int flags = NLM_F_DUMP;
445 clr.size = sizeof(struct rtgenmsg);
448 clr.size = sizeof(struct rtmsg);
457 clr.msg = nlmsg_alloc_simple(type, flags);
461 nlmsg_append(clr.msg, &rtm, clr.size, 0);
462 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, cb_clear_event, &clr);
463 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, cb_finish_event, &pending);
464 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &pending);
466 nl_send_auto_complete(sock_rtnl, clr.msg);
468 nl_recvmsgs(sock_rtnl, cb);
476 * Clear bridge (membership) state and bring down device
478 void system_if_clear_state(struct device *dev)
480 static char buf[256];
486 dev->ifindex = system_if_resolve(dev);
490 system_if_flags(dev->ifname, 0, IFF_UP);
492 if (system_is_bridge(dev->ifname, buf, sizeof(buf))) {
493 D(SYSTEM, "Delete existing bridge named '%s'\n", dev->ifname);
494 system_bridge_delbr(dev);
498 bridge = system_get_bridge(dev->ifname, buf, sizeof(buf));
500 D(SYSTEM, "Remove device '%s' from bridge '%s'\n", dev->ifname, bridge);
501 system_bridge_if(bridge, dev, SIOCBRDELIF, NULL);
504 system_if_clear_entries(dev, RTM_GETROUTE, AF_INET);
505 system_if_clear_entries(dev, RTM_GETADDR, AF_INET);
506 system_if_clear_entries(dev, RTM_GETROUTE, AF_INET6);
507 system_if_clear_entries(dev, RTM_GETADDR, AF_INET6);
508 system_set_disable_ipv6(dev, "0");
511 static inline unsigned long
512 sec_to_jiffies(int val)
514 return (unsigned long) val * 100;
517 int system_bridge_addbr(struct device *bridge, struct bridge_config *cfg)
519 unsigned long args[4] = {};
521 if (ioctl(sock_ioctl, SIOCBRADDBR, bridge->ifname) < 0)
524 args[0] = BRCTL_SET_BRIDGE_STP_STATE;
525 args[1] = !!cfg->stp;
526 system_bridge_if(bridge->ifname, NULL, SIOCDEVPRIVATE, &args);
528 args[0] = BRCTL_SET_BRIDGE_FORWARD_DELAY;
529 args[1] = sec_to_jiffies(cfg->forward_delay);
530 system_bridge_if(bridge->ifname, NULL, SIOCDEVPRIVATE, &args);
532 system_set_dev_sysctl("/sys/devices/virtual/net/%s/bridge/multicast_snooping",
533 bridge->ifname, cfg->igmp_snoop ? "1" : "0");
535 if (cfg->flags & BRIDGE_OPT_AGEING_TIME) {
536 args[0] = BRCTL_SET_AGEING_TIME;
537 args[1] = sec_to_jiffies(cfg->ageing_time);
538 system_bridge_if(bridge->ifname, NULL, SIOCDEVPRIVATE, &args);
541 if (cfg->flags & BRIDGE_OPT_HELLO_TIME) {
542 args[0] = BRCTL_SET_BRIDGE_HELLO_TIME;
543 args[1] = sec_to_jiffies(cfg->hello_time);
544 system_bridge_if(bridge->ifname, NULL, SIOCDEVPRIVATE, &args);
547 if (cfg->flags & BRIDGE_OPT_MAX_AGE) {
548 args[0] = BRCTL_SET_BRIDGE_MAX_AGE;
549 args[1] = sec_to_jiffies(cfg->max_age);
550 system_bridge_if(bridge->ifname, NULL, SIOCDEVPRIVATE, &args);
556 static int system_vlan(struct device *dev, int id)
558 struct vlan_ioctl_args ifr = {
559 .cmd = SET_VLAN_NAME_TYPE_CMD,
560 .u.name_type = VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD,
563 ioctl(sock_ioctl, SIOCSIFVLAN, &ifr);
566 ifr.cmd = DEL_VLAN_CMD;
569 ifr.cmd = ADD_VLAN_CMD;
572 strncpy(ifr.device1, dev->ifname, sizeof(ifr.device1));
573 return ioctl(sock_ioctl, SIOCSIFVLAN, &ifr);
576 int system_vlan_add(struct device *dev, int id)
578 return system_vlan(dev, id);
581 int system_vlan_del(struct device *dev)
583 return system_vlan(dev, -1);
587 system_if_get_settings(struct device *dev, struct device_settings *s)
591 memset(&ifr, 0, sizeof(ifr));
592 strncpy(ifr.ifr_name, dev->ifname, sizeof(ifr.ifr_name));
594 if (ioctl(sock_ioctl, SIOCGIFMTU, &ifr) == 0) {
595 s->mtu = ifr.ifr_mtu;
596 s->flags |= DEV_OPT_MTU;
599 if (ioctl(sock_ioctl, SIOCGIFTXQLEN, &ifr) == 0) {
600 s->txqueuelen = ifr.ifr_qlen;
601 s->flags |= DEV_OPT_TXQUEUELEN;
604 if (ioctl(sock_ioctl, SIOCGIFHWADDR, &ifr) == 0) {
605 memcpy(s->macaddr, &ifr.ifr_hwaddr.sa_data, sizeof(s->macaddr));
606 s->flags |= DEV_OPT_MACADDR;
611 system_if_apply_settings(struct device *dev, struct device_settings *s)
615 memset(&ifr, 0, sizeof(ifr));
616 strncpy(ifr.ifr_name, dev->ifname, sizeof(ifr.ifr_name));
617 if (s->flags & DEV_OPT_MTU) {
618 ifr.ifr_mtu = s->mtu;
619 if (ioctl(sock_ioctl, SIOCSIFMTU, &ifr) < 0)
620 s->flags &= ~DEV_OPT_MTU;
622 if (s->flags & DEV_OPT_TXQUEUELEN) {
623 ifr.ifr_qlen = s->txqueuelen;
624 if (ioctl(sock_ioctl, SIOCSIFTXQLEN, &ifr) < 0)
625 s->flags &= ~DEV_OPT_TXQUEUELEN;
627 if (s->flags & DEV_OPT_MACADDR) {
628 ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
629 memcpy(&ifr.ifr_hwaddr.sa_data, s->macaddr, sizeof(s->macaddr));
630 if (ioctl(sock_ioctl, SIOCSIFHWADDR, &ifr) < 0)
631 s->flags &= ~DEV_OPT_MACADDR;
635 int system_if_up(struct device *dev)
637 system_if_get_settings(dev, &dev->orig_settings);
638 system_if_apply_settings(dev, &dev->settings);
639 dev->ifindex = system_if_resolve(dev);
640 return system_if_flags(dev->ifname, IFF_UP, 0);
643 int system_if_down(struct device *dev)
645 int ret = system_if_flags(dev->ifname, 0, IFF_UP);
646 dev->orig_settings.flags &= dev->settings.flags;
647 system_if_apply_settings(dev, &dev->orig_settings);
651 int system_if_check(struct device *dev)
653 device_set_present(dev, (system_if_resolve(dev) > 0));
658 system_if_get_parent(struct device *dev)
660 char buf[64], *devname;
661 int ifindex, iflink, len;
664 snprintf(buf, sizeof(buf), "/sys/class/net/%s/iflink", dev->ifname);
669 len = fread(buf, 1, sizeof(buf) - 1, f);
676 iflink = strtoul(buf, NULL, 0);
677 ifindex = system_if_resolve(dev);
678 if (!iflink || iflink == ifindex)
681 devname = if_indextoname(iflink, buf);
685 return device_get(devname, true);
689 read_string_file(int dir_fd, const char *file, char *buf, int len)
695 fd = openat(dir_fd, file, O_RDONLY);
700 len = read(fd, buf, len - 1);
704 } else if (len > 0) {
707 c = strchr(buf, '\n');
720 read_int_file(int dir_fd, const char *file, int *val)
725 ret = read_string_file(dir_fd, file, buf, sizeof(buf));
727 *val = strtoul(buf, NULL, 0);
732 /* Assume advertised flags == supported flags */
733 static const struct {
736 } ethtool_link_modes[] = {
737 { ADVERTISED_10baseT_Half, "10H" },
738 { ADVERTISED_10baseT_Full, "10F" },
739 { ADVERTISED_100baseT_Half, "100H" },
740 { ADVERTISED_100baseT_Full, "100F" },
741 { ADVERTISED_1000baseT_Half, "1000H" },
742 { ADVERTISED_1000baseT_Full, "1000F" },
745 static void system_add_link_modes(struct blob_buf *b, __u32 mask)
748 for (i = 0; i < ARRAY_SIZE(ethtool_link_modes); i++) {
749 if (mask & ethtool_link_modes[i].mask)
750 blobmsg_add_string(b, NULL, ethtool_link_modes[i].name);
755 system_if_dump_info(struct device *dev, struct blob_buf *b)
757 struct ethtool_cmd ecmd;
763 snprintf(buf, sizeof(buf), "/sys/class/net/%s", dev->ifname);
764 dir_fd = open(buf, O_DIRECTORY);
766 if (read_int_file(dir_fd, "carrier", &val))
767 blobmsg_add_u8(b, "link", !!val);
769 memset(&ecmd, 0, sizeof(ecmd));
770 memset(&ifr, 0, sizeof(ifr));
771 strcpy(ifr.ifr_name, dev->ifname);
772 ifr.ifr_data = (caddr_t) &ecmd;
773 ecmd.cmd = ETHTOOL_GSET;
775 if (ioctl(sock_ioctl, SIOCETHTOOL, &ifr) == 0) {
776 c = blobmsg_open_array(b, "link-advertising");
777 system_add_link_modes(b, ecmd.advertising);
778 blobmsg_close_array(b, c);
780 c = blobmsg_open_array(b, "link-supported");
781 system_add_link_modes(b, ecmd.supported);
782 blobmsg_close_array(b, c);
784 s = blobmsg_alloc_string_buffer(b, "speed", 8);
785 snprintf(s, 8, "%d%c", ethtool_cmd_speed(&ecmd),
786 ecmd.duplex == DUPLEX_HALF ? 'H' : 'F');
787 blobmsg_add_string_buffer(b);
795 system_if_dump_stats(struct device *dev, struct blob_buf *b)
797 const char *const counters[] = {
798 "collisions", "rx_frame_errors", "tx_compressed",
799 "multicast", "rx_length_errors", "tx_dropped",
800 "rx_bytes", "rx_missed_errors", "tx_errors",
801 "rx_compressed", "rx_over_errors", "tx_fifo_errors",
802 "rx_crc_errors", "rx_packets", "tx_heartbeat_errors",
803 "rx_dropped", "tx_aborted_errors", "tx_packets",
804 "rx_errors", "tx_bytes", "tx_window_errors",
805 "rx_fifo_errors", "tx_carrier_errors",
811 snprintf(buf, sizeof(buf), "/sys/class/net/%s/statistics", dev->ifname);
812 stats_dir = open(buf, O_DIRECTORY);
816 for (i = 0; i < ARRAY_SIZE(counters); i++)
817 if (read_int_file(stats_dir, counters[i], &val))
818 blobmsg_add_u32(b, counters[i], val);
824 static int system_addr(struct device *dev, struct device_addr *addr, int cmd)
826 bool v4 = ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET4);
827 int alen = v4 ? 4 : 16;
828 struct ifaddrmsg ifa = {
829 .ifa_family = (alen == 4) ? AF_INET : AF_INET6,
830 .ifa_prefixlen = addr->mask,
831 .ifa_index = dev->ifindex,
836 msg = nlmsg_alloc_simple(cmd, 0);
840 nlmsg_append(msg, &ifa, sizeof(ifa), 0);
841 nla_put(msg, IFA_LOCAL, alen, &addr->addr);
843 nla_put_u32(msg, IFA_BROADCAST, addr->broadcast);
845 return system_rtnl_call(msg);
848 int system_add_address(struct device *dev, struct device_addr *addr)
850 return system_addr(dev, addr, RTM_NEWADDR);
853 int system_del_address(struct device *dev, struct device_addr *addr)
855 return system_addr(dev, addr, RTM_DELADDR);
858 static int system_rt(struct device *dev, struct device_route *route, int cmd)
860 int alen = ((route->flags & DEVADDR_FAMILY) == DEVADDR_INET4) ? 4 : 16;
862 unsigned int flags = 0;
863 int ifindex = dev->ifindex;
866 have_gw = !!route->nexthop.in.s_addr;
868 have_gw = route->nexthop.in6.s6_addr32[0] ||
869 route->nexthop.in6.s6_addr32[1] ||
870 route->nexthop.in6.s6_addr32[2] ||
871 route->nexthop.in6.s6_addr32[3];
873 unsigned char scope = (cmd == RTM_DELROUTE) ? RT_SCOPE_NOWHERE :
874 (have_gw) ? RT_SCOPE_UNIVERSE : RT_SCOPE_LINK;
877 .rtm_family = (alen == 4) ? AF_INET : AF_INET6,
878 .rtm_dst_len = route->mask,
879 .rtm_table = RT_TABLE_MAIN,
880 .rtm_protocol = RTPROT_BOOT,
882 .rtm_type = (cmd == RTM_DELROUTE) ? 0: RTN_UNICAST,
886 if (cmd == RTM_NEWROUTE)
887 flags |= NLM_F_CREATE | NLM_F_REPLACE;
889 msg = nlmsg_alloc_simple(cmd, flags);
893 nlmsg_append(msg, &rtm, sizeof(rtm), 0);
896 nla_put(msg, RTA_DST, alen, &route->addr);
898 if (route->metric > 0)
899 nla_put_u32(msg, RTA_PRIORITY, route->metric);
902 nla_put(msg, RTA_GATEWAY, alen, &route->nexthop);
904 nla_put_u32(msg, RTA_OIF, ifindex);
906 return system_rtnl_call(msg);
909 int system_add_route(struct device *dev, struct device_route *route)
911 return system_rt(dev, route, RTM_NEWROUTE);
914 int system_del_route(struct device *dev, struct device_route *route)
916 return system_rt(dev, route, RTM_DELROUTE);
919 int system_flush_routes(void)
921 const char *names[] = {
922 "/proc/sys/net/ipv4/route/flush",
923 "/proc/sys/net/ipv6/route/flush"
927 for (i = 0; i < ARRAY_SIZE(names); i++) {
928 fd = open(names[i], O_WRONLY);
938 time_t system_get_rtime(void)
943 if (syscall(__NR_clock_gettime, CLOCK_MONOTONIC, &ts) == 0)
946 if (gettimeofday(&tv, NULL) == 0)
956 static void tunnel_parm_init(struct ip_tunnel_parm *p)
958 memset(p, 0, sizeof(*p));
961 p->iph.frag_off = htons(IP_DF);
964 static int tunnel_ioctl(const char *name, int cmd, void *p)
968 memset(&ifr, 0, sizeof(ifr));
969 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
970 ifr.ifr_ifru.ifru_data = p;
971 return ioctl(sock_ioctl, cmd, &ifr);
974 int system_del_ip_tunnel(const char *name)
976 struct ip_tunnel_parm p;
978 tunnel_parm_init(&p);
979 return tunnel_ioctl(name, SIOCDELTUNNEL, &p);
982 static int parse_ipaddr(struct blob_attr *attr, __be32 *addr)
987 return inet_pton(AF_INET, blobmsg_data(attr), (void *) addr);
991 int system_add_ip_tunnel(const char *name, struct blob_attr *attr)
993 struct blob_attr *tb[__TUNNEL_ATTR_MAX];
994 struct blob_attr *cur;
995 struct ip_tunnel_parm p;
996 const char *base, *str;
997 int cmd = SIOCADDTUNNEL;
999 system_del_ip_tunnel(name);
1001 tunnel_parm_init(&p);
1003 blobmsg_parse(tunnel_attr_list.params, __TUNNEL_ATTR_MAX, tb,
1004 blob_data(attr), blob_len(attr));
1006 cur = tb[TUNNEL_ATTR_TYPE];
1010 str = blobmsg_data(cur);
1011 if (!strcmp(str, "sit")) {
1012 p.iph.protocol = IPPROTO_IPV6;
1017 if (!parse_ipaddr(tb[TUNNEL_ATTR_LOCAL], &p.iph.saddr))
1020 if (!parse_ipaddr(tb[TUNNEL_ATTR_REMOTE], &p.iph.daddr))
1023 if ((cur = tb[TUNNEL_ATTR_TTL])) {
1024 unsigned int val = blobmsg_get_u32(cur);
1032 strncpy(p.name, name, sizeof(p.name));
1033 return tunnel_ioctl(base, cmd, &p);