add a system_init function for system control
[project/netifd.git] / system-linux.c
1 #include <sys/socket.h>
2 #include <sys/ioctl.h>
3
4 #include <linux/rtnetlink.h>
5 #include <linux/sockios.h>
6 #include <linux/if_vlan.h>
7
8 #include <stddef.h>
9 #include <string.h>
10 #include <fcntl.h>
11 #include <errno.h>
12
13 #include <netlink/msg.h>
14
15 #include "netifd.h"
16 #include "device.h"
17 #include "system.h"
18
19 static int sock_ioctl = -1;
20 static struct nl_sock *sock_rtnl = NULL;
21
22 int system_init(void)
23 {
24         sock_ioctl = socket(AF_LOCAL, SOCK_DGRAM, 0);
25         fcntl(sock_ioctl, F_SETFD, fcntl(sock_ioctl, F_GETFD) | FD_CLOEXEC);
26
27         if ((sock_rtnl = nl_socket_alloc())) {
28                 if (nl_connect(sock_rtnl, NETLINK_ROUTE)) {
29                         nl_socket_free(sock_rtnl);
30                         sock_rtnl = NULL;
31                 }
32         }
33
34         return -(sock_ioctl < 0 || !sock_rtnl);
35 }
36
37 static int system_rtnl_call(struct nl_msg *msg)
38 {
39         return -(nl_send_auto_complete(sock_rtnl, msg)
40                         || nl_wait_for_ack(sock_rtnl));
41 }
42
43 int system_bridge_addbr(struct device *bridge)
44 {
45         return ioctl(sock_ioctl, SIOCBRADDBR, bridge->ifname);
46 }
47
48 int system_bridge_delbr(struct device *bridge)
49 {
50         return ioctl(sock_ioctl, SIOCBRDELBR, bridge->ifname);
51 }
52
53 static int system_bridge_if(struct device *bridge, struct device *dev, int cmd)
54 {
55         struct ifreq ifr;
56         ifr.ifr_ifindex = dev->ifindex;
57         strncpy(ifr.ifr_name, bridge->ifname, sizeof(ifr.ifr_name));
58         return ioctl(sock_ioctl, cmd, &ifr);
59 }
60
61 int system_bridge_addif(struct device *bridge, struct device *dev)
62 {
63         return system_bridge_if(bridge, dev, SIOCBRADDIF);
64 }
65
66 int system_bridge_delif(struct device *bridge, struct device *dev)
67 {
68         return system_bridge_if(bridge, dev, SIOCBRDELIF);
69 }
70
71 static int system_vlan(struct device *dev, int id)
72 {
73         struct vlan_ioctl_args ifr = {
74                 .cmd = (id == 0) ? DEL_VLAN_CMD : ADD_VLAN_CMD,
75                 .u = {.VID = id},
76         };
77         strncpy(ifr.device1, dev->ifname, sizeof(ifr.device1));
78         return ioctl(sock_ioctl, SIOCSIFVLAN, &ifr);
79 }
80
81 int system_vlan_add(struct device *dev, int id)
82 {
83         return system_vlan(dev, id);
84 }
85
86 int system_vlan_del(struct device *dev)
87 {
88         return system_vlan(dev, 0);
89 }
90
91 static int system_if_flags(struct device *dev, unsigned add, unsigned rem)
92 {
93         struct ifreq ifr;
94         strncpy(ifr.ifr_name, dev->ifname, sizeof(ifr.ifr_name));
95         ioctl(sock_ioctl, SIOCGIFFLAGS, &ifr);
96         ifr.ifr_flags |= add;
97         ifr.ifr_flags &= ~rem;
98         return ioctl(sock_ioctl, SIOCSIFFLAGS, &ifr);
99 }
100
101 int system_if_up(struct device *dev)
102 {
103         return system_if_flags(dev, IFF_UP, 0);
104 }
105
106 int system_if_down(struct device *dev)
107 {
108         return system_if_flags(dev, 0, IFF_UP);
109 }
110
111 int system_if_check(struct device *dev)
112 {
113         struct ifreq ifr;
114         strncpy(ifr.ifr_name, dev->ifname, sizeof(ifr.ifr_name));
115         if (ioctl(sock_ioctl, SIOCGIFINDEX, &ifr))
116                 return -1;
117
118         dev->ifindex = ifr.ifr_ifindex;
119
120         /* if (!strcmp(dev->ifname, "eth0"))
121                 device_set_present(dev, true); */
122         return 0;
123 }
124
125 static int system_addr(struct device *dev, struct device_addr *addr, int cmd)
126 {
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,
132         };
133
134         struct nl_msg *msg = nlmsg_alloc_simple(cmd, 0);
135         if (!msg)
136                 return -1;
137
138         nlmsg_append(msg, &ifa, sizeof(ifa), 0);
139         nla_put(msg, IFA_ADDRESS, alen, &addr->addr);
140         return system_rtnl_call(msg);
141 }
142
143 int system_add_address(struct device *dev, struct device_addr *addr)
144 {
145         return system_addr(dev, addr, RTM_NEWADDR);
146 }
147
148 int system_del_address(struct device *dev, struct device_addr *addr)
149 {
150         return system_addr(dev, addr, RTM_DELADDR);
151 }
152
153 static int system_rt(struct device *dev, struct device_route *route, int cmd)
154 {
155         int alen = ((route->flags & DEVADDR_FAMILY) == DEVADDR_INET4) ? 4 : 16;
156         bool have_gw;
157
158         if (alen == 4)
159                 have_gw = !!route->nexthop.in.s_addr;
160         else
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];
165
166         unsigned char scope = (cmd == RTM_DELROUTE) ? RT_SCOPE_NOWHERE :
167                         (have_gw) ? RT_SCOPE_UNIVERSE : RT_SCOPE_LINK;
168
169         struct rtmsg rtm = {
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,
174                 .rtm_scope = scope,
175                 .rtm_type = (cmd == RTM_DELROUTE) ? 0: RTN_UNICAST,
176         };
177
178         struct nl_msg *msg = nlmsg_alloc_simple(cmd, 0);
179         if (!msg)
180                 return -1;
181
182         nlmsg_append(msg, &rtm, sizeof(rtm), 0);
183
184         if (route->mask)
185                 nla_put(msg, RTA_DST, alen, &route->addr);
186
187         if (have_gw)
188                 nla_put(msg, RTA_GATEWAY, alen, &route->nexthop);
189
190         if (route->flags & DEVADDR_DEVICE)
191                 nla_put_u32(msg, RTA_OIF, dev->ifindex);
192
193         return system_rtnl_call(msg);
194 }
195
196 int system_add_route(struct device *dev, struct device_route *route)
197 {
198         return system_rt(dev, route, RTM_NEWROUTE);
199 }
200
201 int system_del_route(struct device *dev, struct device_route *route)
202 {
203         return system_rt(dev, route, RTM_DELROUTE);
204 }