2 * Copyright (C) 2010 Felix Fietkau <nbd@openwrt.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License v2 as published by
6 * the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18 #include <sys/socket.h>
26 #include <linux/fib_rules.h>
30 static struct uloop_fd rtnl_sock;
31 static unsigned int rtnl_seq, rtnl_dump_seq;
32 int route_table = 16800;
34 static void rtnl_flush(void)
38 fd = open("/proc/sys/net/ipv4/route/flush", O_WRONLY);
47 RULE_F_ADD = (1 << 0),
48 RULE_F_DEFGW_WORKAROUND = (1 << 1),
51 static int get_route_table(struct relayd_interface *rif)
56 return local_route_table;
60 rtnl_rule_request(struct relayd_interface *rif, int flags)
71 char ifname[IFNAMSIZ + 1];
75 .rtm_family = AF_INET,
76 .rtm_table = RT_TABLE_UNSPEC,
77 .rtm_scope = RT_SCOPE_UNIVERSE,
78 .rtm_protocol = RTPROT_BOOT,
81 .rta_type = FRA_TABLE,
82 .rta_len = sizeof(req.table),
85 const char *ifname = "lo";
86 int padding = sizeof(req.dev.ifname);
91 if (!(flags & RULE_F_DEFGW_WORKAROUND)) {
92 req.dev.rta.rta_type = FRA_IFNAME;
93 padding -= strlen(ifname) + 1;
94 strcpy(req.dev.ifname, ifname);
95 req.dev.rta.rta_len = sizeof(req.dev.rta) + strlen(ifname) + 1;
98 req.dev.rta.rta_type = FRA_PRIORITY;
99 req.dev.rta.rta_len = sizeof(req.dev.rta) + sizeof(uint32_t);
100 padding -= sizeof(uint32_t);
101 memcpy(&req.dev.ifname, &val, sizeof(val));
103 req.table.table = get_route_table(rif);
104 req.nl.nlmsg_len = sizeof(req) - padding;
106 req.nl.nlmsg_flags = NLM_F_REQUEST;
107 if (flags & RULE_F_ADD) {
108 req.nl.nlmsg_type = RTM_NEWRULE;
109 req.nl.nlmsg_flags |= NLM_F_CREATE | NLM_F_EXCL;
111 req.rt.rtm_type = RTN_UNICAST;
113 req.nl.nlmsg_type = RTM_DELRULE;
114 req.rt.rtm_type = RTN_UNSPEC;
117 send(rtnl_sock.fd, &req, req.nl.nlmsg_len, 0);
126 static struct rtnl_addr *
127 rtnl_add_addr(struct rtnl_addr *addr, int *len, int type, const uint8_t *ipaddr)
129 addr->rta.rta_type = type;
130 memcpy(addr->ipaddr, ipaddr, 4);
131 *len += sizeof(*addr);
136 rtnl_route_request(struct relayd_interface *rif, struct relayd_host *host,
137 struct relayd_route *route, bool add)
150 struct rtnl_addr addr[3];
153 .rtm_family = AF_INET,
155 .rtm_table = RT_TABLE_MAIN,
158 .rta_type = RTA_TABLE,
159 .rta_len = sizeof(req.table),
163 .rta_len = sizeof(req.dev),
165 .addr[0].rta.rta_len = sizeof(struct rtnl_addr),
166 .addr[1].rta.rta_len = sizeof(struct rtnl_addr),
167 .addr[2].rta.rta_len = sizeof(struct rtnl_addr),
169 int pktlen = sizeof(req) - sizeof(req.addr);
170 struct rtnl_addr *addr = &req.addr[0];
171 const char *ifname = "loopback";
173 req.dev.ifindex = host->rif->sll.sll_ifindex;
174 req.table.table = get_route_table(rif);
176 req.nl.nlmsg_flags = NLM_F_REQUEST;
178 req.nl.nlmsg_type = RTM_NEWROUTE;
179 req.nl.nlmsg_flags |= NLM_F_CREATE | NLM_F_REPLACE;
181 req.rt.rtm_protocol = RTPROT_BOOT;
183 req.rt.rtm_scope = RT_SCOPE_UNIVERSE;
185 req.rt.rtm_scope = RT_SCOPE_LINK;
187 req.rt.rtm_type = RTN_UNICAST;
189 req.nl.nlmsg_type = RTM_DELROUTE;
190 req.rt.rtm_scope = RT_SCOPE_NOWHERE;
194 ifname = rif->ifname;
197 DPRINTF(2, "%s: add route to "IP_FMT"/%d via "IP_FMT" (%s)\n", ifname,
198 IP_BUF(route->dest), route->mask, IP_BUF(host->ipaddr),
201 req.rt.rtm_dst_len = route->mask;
203 addr = rtnl_add_addr(addr, &pktlen, RTA_DST, route->dest);
204 addr = rtnl_add_addr(addr, &pktlen, RTA_GATEWAY, host->ipaddr);
206 DPRINTF(2, "%s: add host route to "IP_FMT" (%s)\n", ifname,
207 IP_BUF(host->ipaddr), host->rif->ifname);
208 addr = rtnl_add_addr(addr, &pktlen, RTA_DST, host->ipaddr);
209 req.rt.rtm_dst_len = 32;
214 addr = rtnl_add_addr(addr, &pktlen, RTA_PREFSRC, local_addr);
216 req.nl.nlmsg_len = pktlen;
218 rtnl_rule_request(rif, RULE_F_DEFGW_WORKAROUND | RULE_F_ADD);
219 send(rtnl_sock.fd, &req, pktlen, 0);
221 rtnl_rule_request(rif, RULE_F_DEFGW_WORKAROUND);
226 rtnl_route_set(struct relayd_host *host, struct relayd_route *route, bool add)
228 struct relayd_interface *rif;
230 list_for_each_entry(rif, &interfaces, list) {
231 if (rif == host->rif)
234 rtnl_route_request(rif, host, route, add);
236 if (local_route_table)
237 rtnl_route_request(NULL, host, route, add);
240 void relayd_add_interface_routes(struct relayd_interface *rif)
242 rif->rt_table = route_table++;
243 rtnl_rule_request(rif, RULE_F_ADD);
246 void relayd_del_interface_routes(struct relayd_interface *rif)
248 rtnl_rule_request(rif, 0);
253 ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
256 static void rtnl_parse_newneigh(struct nlmsghdr *h)
258 struct relayd_interface *rif = NULL;
259 struct ndmsg *r = NLMSG_DATA(h);
260 const uint8_t *lladdr = NULL;
261 const uint8_t *ipaddr = NULL;
265 if (r->ndm_family != AF_INET)
268 list_for_each_entry(rif, &interfaces, list) {
269 if (rif->sll.sll_ifindex == r->ndm_ifindex)
270 goto found_interface;
275 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(*r));
276 for (rta = NDA_RTA(r); RTA_OK(rta, len); rta = RTA_NEXT(rta, len)) {
277 switch(rta->rta_type) {
279 lladdr = RTA_DATA(rta);
282 ipaddr = RTA_DATA(rta);
289 if (!lladdr || !ipaddr || (r->ndm_state & (NUD_INCOMPLETE|NUD_FAILED)))
292 if (!memcmp(lladdr, "\x00\x00\x00\x00\x00\x00", ETH_ALEN))
295 DPRINTF(1, "%s: Found ARP cache entry for host "IP_FMT" ("MAC_FMT")\n",
296 rif->ifname, IP_BUF(ipaddr), MAC_BUF(lladdr));
297 relayd_refresh_host(rif, lladdr, ipaddr);
300 static void rtnl_parse_packet(void *data, int len)
304 for (h = data; NLMSG_OK(h, len); h = NLMSG_NEXT(h, len)) {
305 if (h->nlmsg_type == NLMSG_DONE ||
306 h->nlmsg_type == NLMSG_ERROR)
309 if (h->nlmsg_seq != rtnl_dump_seq)
312 if (h->nlmsg_type == RTM_NEWNEIGH)
313 rtnl_parse_newneigh(h);
317 static void rtnl_cb(struct uloop_fd *fd, unsigned int events)
319 struct sockaddr_nl nladdr;
320 static uint8_t buf[16384];
323 .iov_len = sizeof(buf),
325 struct msghdr msg = {
327 .msg_namelen = sizeof(nladdr),
335 len = recvmsg(rtnl_sock.fd, &msg, 0);
346 if (nladdr.nl_pid != 0)
349 rtnl_parse_packet(buf, len);
353 static void rtnl_dump_request(int nlmsg_type)
360 .nlmsg_len = sizeof(req),
361 .nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST,
364 .g.rtgen_family = AF_INET,
366 req.nlh.nlmsg_type = nlmsg_type;
367 req.nlh.nlmsg_seq = rtnl_seq;
368 send(rtnl_sock.fd, &req, sizeof(req), 0);
372 int relayd_rtnl_init(void)
374 struct sockaddr_nl snl_local;
376 rtnl_sock.fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
377 if (rtnl_sock.fd < 0) {
378 perror("socket(AF_NETLINK)");
382 snl_local.nl_family = AF_NETLINK;
384 if (bind(rtnl_sock.fd, (struct sockaddr *) &snl_local, sizeof(struct sockaddr_nl)) < 0) {
390 rtnl_sock.cb = rtnl_cb;
391 uloop_fd_add(&rtnl_sock, ULOOP_READ | ULOOP_EDGE_TRIGGER);
393 rtnl_seq = time(NULL);
394 rtnl_dump_seq = rtnl_seq;
395 rtnl_dump_request(RTM_GETNEIGH);
396 rtnl_rule_request(NULL, RULE_F_ADD);
401 void relayd_rtnl_done(void)
403 rtnl_rule_request(NULL, 0);
404 uloop_fd_delete(&rtnl_sock);