1dbb651f747f47d805fce4a5e9150f52bb79921a
[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 static void __init 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
35 static int system_rtnl_call(struct nl_msg *msg)
36 {
37         return -!!(!sock_rtnl || nl_send_auto_complete(sock_rtnl, msg)
38                                         || nl_wait_for_ack(sock_rtnl));
39 }
40
41 int system_bridge_addbr(struct device *bridge)
42 {
43         return ioctl(sock_ioctl, SIOCBRADDBR, bridge->ifname);
44 }
45
46 int system_bridge_delbr(struct device *bridge)
47 {
48         return ioctl(sock_ioctl, SIOCBRDELBR, bridge->ifname);
49 }
50
51 static int system_bridge_if(struct device *bridge, struct device *dev, int cmd)
52 {
53         struct ifreq ifr;
54         ifr.ifr_ifindex = dev->ifindex;
55         strncpy(ifr.ifr_name, bridge->ifname, sizeof(ifr.ifr_name));
56         return ioctl(sock_ioctl, cmd, &ifr);
57 }
58
59 int system_bridge_addif(struct device *bridge, struct device *dev)
60 {
61         return system_bridge_if(bridge, dev, SIOCBRADDIF);
62 }
63
64 int system_bridge_delif(struct device *bridge, struct device *dev)
65 {
66         return system_bridge_if(bridge, dev, SIOCBRDELIF);
67 }
68
69 static int system_vlan(struct device *dev, int id)
70 {
71         struct vlan_ioctl_args ifr = {
72                 .cmd = (id == 0) ? DEL_VLAN_CMD : ADD_VLAN_CMD,
73                 .u = {.VID = id},
74         };
75         strncpy(ifr.device1, dev->ifname, sizeof(ifr.device1));
76         return ioctl(sock_ioctl, SIOCSIFVLAN, &ifr);
77 }
78
79 int system_vlan_add(struct device *dev, int id)
80 {
81         return system_vlan(dev, id);
82 }
83
84 int system_vlan_del(struct device *dev)
85 {
86         return system_vlan(dev, 0);
87 }
88
89 static int system_if_flags(struct device *dev, unsigned add, unsigned rem)
90 {
91         struct ifreq ifr;
92         strncpy(ifr.ifr_name, dev->ifname, sizeof(ifr.ifr_name));
93         ioctl(sock_ioctl, SIOCGIFFLAGS, &ifr);
94         ifr.ifr_flags |= add;
95         ifr.ifr_flags &= ~rem;
96         return ioctl(sock_ioctl, SIOCSIFFLAGS, &ifr);
97 }
98
99 int system_if_up(struct device *dev)
100 {
101         return system_if_flags(dev, IFF_UP, 0);
102 }
103
104 int system_if_down(struct device *dev)
105 {
106         return system_if_flags(dev, 0, IFF_UP);
107 }
108
109 int system_if_check(struct device *dev)
110 {
111         struct ifreq ifr;
112         strncpy(ifr.ifr_name, dev->ifname, sizeof(ifr.ifr_name));
113         if (ioctl(sock_ioctl, SIOCGIFINDEX, &ifr))
114                 return -1;
115
116         dev->ifindex = ifr.ifr_ifindex;
117
118         /* if (!strcmp(dev->ifname, "eth0"))
119                 device_set_present(dev, true); */
120         return 0;
121 }
122
123 static int system_addr(struct device *dev, struct device_addr *addr, int cmd)
124 {
125         int alen = ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET4) ? 4 : 16;
126         struct ifaddrmsg ifa = {
127                 .ifa_family = (alen == 4) ? AF_INET : AF_INET6,
128                 .ifa_prefixlen = addr->mask,
129                 .ifa_index = dev->ifindex,
130         };
131
132         struct nl_msg *msg = nlmsg_alloc_simple(cmd, 0);
133         if (!msg)
134                 return -1;
135
136         nlmsg_append(msg, &ifa, sizeof(ifa), 0);
137         nla_put(msg, IFA_ADDRESS, alen, &addr->addr);
138         return system_rtnl_call(msg);
139 }
140
141 int system_add_address(struct device *dev, struct device_addr *addr)
142 {
143         return system_addr(dev, addr, RTM_NEWADDR);
144 }
145
146 int system_del_address(struct device *dev, struct device_addr *addr)
147 {
148         return system_addr(dev, addr, RTM_DELADDR);
149 }
150
151 static int system_rt(struct device *dev, struct device_route *route, int cmd)
152 {
153         int alen = ((route->flags & DEVADDR_FAMILY) == DEVADDR_INET4) ? 4 : 16;
154         bool have_gw;
155
156         if (alen == 4)
157                 have_gw = !!route->nexthop.in.s_addr;
158         else
159                 have_gw = route->nexthop.in6.s6_addr32[0] ||
160                         route->nexthop.in6.s6_addr32[1] ||
161                         route->nexthop.in6.s6_addr32[2] ||
162                         route->nexthop.in6.s6_addr32[3];
163
164         unsigned char scope = (cmd == RTM_DELROUTE) ? RT_SCOPE_NOWHERE :
165                         (have_gw) ? RT_SCOPE_UNIVERSE : RT_SCOPE_LINK;
166
167         struct rtmsg rtm = {
168                 .rtm_family = (alen == 4) ? AF_INET : AF_INET6,
169                 .rtm_dst_len = route->mask,
170                 .rtm_table = RT_TABLE_MAIN,
171                 .rtm_protocol = RTPROT_BOOT,
172                 .rtm_scope = scope,
173                 .rtm_type = (cmd == RTM_DELROUTE) ? 0: RTN_UNICAST,
174         };
175
176         struct nl_msg *msg = nlmsg_alloc_simple(cmd, 0);
177         if (!msg)
178                 return -1;
179
180         nlmsg_append(msg, &rtm, sizeof(rtm), 0);
181
182         if (route->mask)
183                 nla_put(msg, RTA_DST, alen, &route->addr);
184
185         if (have_gw)
186                 nla_put(msg, RTA_GATEWAY, alen, &route->nexthop);
187
188         if (route->flags & DEVADDR_DEVICE)
189                 nla_put_u32(msg, RTA_OIF, dev->ifindex);
190
191         return system_rtnl_call(msg);
192 }
193
194 int system_add_route(struct device *dev, struct device_route *route)
195 {
196         return system_rt(dev, route, RTM_NEWROUTE);
197 }
198
199 int system_del_route(struct device *dev, struct device_route *route)
200 {
201         return system_rt(dev, route, RTM_DELROUTE);
202 }