1 #include <sys/socket.h>
4 #include <linux/rtnetlink.h>
5 #include <linux/sockios.h>
6 #include <linux/if_vlan.h>
13 #include <netlink/msg.h>
19 static int sock_ioctl = -1;
20 static struct nl_sock *sock_rtnl = NULL;
24 sock_ioctl = socket(AF_LOCAL, SOCK_DGRAM, 0);
25 fcntl(sock_ioctl, F_SETFD, fcntl(sock_ioctl, F_GETFD) | FD_CLOEXEC);
27 if ((sock_rtnl = nl_socket_alloc())) {
28 if (nl_connect(sock_rtnl, NETLINK_ROUTE)) {
29 nl_socket_free(sock_rtnl);
34 return -(sock_ioctl < 0 || !sock_rtnl);
37 static int system_rtnl_call(struct nl_msg *msg)
39 return -(nl_send_auto_complete(sock_rtnl, msg)
40 || nl_wait_for_ack(sock_rtnl));
43 int system_bridge_addbr(struct device *bridge)
45 return ioctl(sock_ioctl, SIOCBRADDBR, bridge->ifname);
48 int system_bridge_delbr(struct device *bridge)
50 return ioctl(sock_ioctl, SIOCBRDELBR, bridge->ifname);
53 static int system_bridge_if(struct device *bridge, struct device *dev, int cmd)
56 ifr.ifr_ifindex = dev->ifindex;
57 strncpy(ifr.ifr_name, bridge->ifname, sizeof(ifr.ifr_name));
58 return ioctl(sock_ioctl, cmd, &ifr);
61 int system_bridge_addif(struct device *bridge, struct device *dev)
63 return system_bridge_if(bridge, dev, SIOCBRADDIF);
66 int system_bridge_delif(struct device *bridge, struct device *dev)
68 return system_bridge_if(bridge, dev, SIOCBRDELIF);
71 static int system_vlan(struct device *dev, int id)
73 struct vlan_ioctl_args ifr = {
74 .cmd = (id == 0) ? DEL_VLAN_CMD : ADD_VLAN_CMD,
77 strncpy(ifr.device1, dev->ifname, sizeof(ifr.device1));
78 return ioctl(sock_ioctl, SIOCSIFVLAN, &ifr);
81 int system_vlan_add(struct device *dev, int id)
83 return system_vlan(dev, id);
86 int system_vlan_del(struct device *dev)
88 return system_vlan(dev, 0);
91 static int system_if_flags(struct device *dev, unsigned add, unsigned rem)
94 strncpy(ifr.ifr_name, dev->ifname, sizeof(ifr.ifr_name));
95 ioctl(sock_ioctl, SIOCGIFFLAGS, &ifr);
97 ifr.ifr_flags &= ~rem;
98 return ioctl(sock_ioctl, SIOCSIFFLAGS, &ifr);
101 int system_if_up(struct device *dev)
103 return system_if_flags(dev, IFF_UP, 0);
106 int system_if_down(struct device *dev)
108 return system_if_flags(dev, 0, IFF_UP);
111 int system_if_check(struct device *dev)
114 strncpy(ifr.ifr_name, dev->ifname, sizeof(ifr.ifr_name));
115 if (ioctl(sock_ioctl, SIOCGIFINDEX, &ifr))
118 dev->ifindex = ifr.ifr_ifindex;
120 /* if (!strcmp(dev->ifname, "eth0"))
121 device_set_present(dev, true); */
125 static int system_addr(struct device *dev, struct device_addr *addr, int cmd)
127 int alen = ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET4) ? 4 : 16;
128 struct ifaddrmsg ifa = {
129 .ifa_family = (alen == 4) ? AF_INET : AF_INET6,
130 .ifa_prefixlen = addr->mask,
131 .ifa_index = dev->ifindex,
134 struct nl_msg *msg = nlmsg_alloc_simple(cmd, 0);
138 nlmsg_append(msg, &ifa, sizeof(ifa), 0);
139 nla_put(msg, IFA_ADDRESS, alen, &addr->addr);
140 return system_rtnl_call(msg);
143 int system_add_address(struct device *dev, struct device_addr *addr)
145 return system_addr(dev, addr, RTM_NEWADDR);
148 int system_del_address(struct device *dev, struct device_addr *addr)
150 return system_addr(dev, addr, RTM_DELADDR);
153 static int system_rt(struct device *dev, struct device_route *route, int cmd)
155 int alen = ((route->flags & DEVADDR_FAMILY) == DEVADDR_INET4) ? 4 : 16;
159 have_gw = !!route->nexthop.in.s_addr;
161 have_gw = route->nexthop.in6.s6_addr32[0] ||
162 route->nexthop.in6.s6_addr32[1] ||
163 route->nexthop.in6.s6_addr32[2] ||
164 route->nexthop.in6.s6_addr32[3];
166 unsigned char scope = (cmd == RTM_DELROUTE) ? RT_SCOPE_NOWHERE :
167 (have_gw) ? RT_SCOPE_UNIVERSE : RT_SCOPE_LINK;
170 .rtm_family = (alen == 4) ? AF_INET : AF_INET6,
171 .rtm_dst_len = route->mask,
172 .rtm_table = RT_TABLE_MAIN,
173 .rtm_protocol = RTPROT_BOOT,
175 .rtm_type = (cmd == RTM_DELROUTE) ? 0: RTN_UNICAST,
178 struct nl_msg *msg = nlmsg_alloc_simple(cmd, 0);
182 nlmsg_append(msg, &rtm, sizeof(rtm), 0);
185 nla_put(msg, RTA_DST, alen, &route->addr);
188 nla_put(msg, RTA_GATEWAY, alen, &route->nexthop);
190 if (route->flags & DEVADDR_DEVICE)
191 nla_put_u32(msg, RTA_OIF, dev->ifindex);
193 return system_rtnl_call(msg);
196 int system_add_route(struct device *dev, struct device_route *route)
198 return system_rt(dev, route, RTM_NEWROUTE);
201 int system_del_route(struct device *dev, struct device_route *route)
203 return system_rt(dev, route, RTM_DELROUTE);