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>
25 #include <linux/fib_rules.h>
29 static struct uloop_fd rtnl_sock;
30 static unsigned int rtnl_seq, rtnl_dump_seq;
31 int route_table = 16800;
33 static void rtnl_flush(void)
37 fd = open("/proc/sys/net/ipv4/route/flush", O_WRONLY);
46 RULE_F_ADD = (1 << 0),
47 RULE_F_DEFGW_WORKAROUND = (1 << 1),
50 static int get_route_table(struct relayd_interface *rif)
55 return local_route_table;
59 rtnl_rule_request(struct relayd_interface *rif, int flags)
70 char ifname[IFNAMSIZ + 1];
74 .rtm_family = AF_INET,
75 .rtm_table = RT_TABLE_UNSPEC,
76 .rtm_scope = RT_SCOPE_UNIVERSE,
77 .rtm_protocol = RTPROT_BOOT,
80 .rta_type = FRA_TABLE,
81 .rta_len = sizeof(req.table),
84 const char *ifname = "lo";
85 int padding = sizeof(req.dev.ifname);
90 if (!(flags & RULE_F_DEFGW_WORKAROUND)) {
91 req.dev.rta.rta_type = FRA_IFNAME;
92 padding -= strlen(ifname) + 1;
93 strcpy(req.dev.ifname, ifname);
94 req.dev.rta.rta_len = sizeof(req.dev.rta) + strlen(ifname) + 1;
96 req.dev.rta.rta_type = FRA_PRIORITY;
97 req.dev.rta.rta_len = sizeof(req.dev.rta) + sizeof(uint32_t);
98 padding -= sizeof(uint32_t);
99 *((uint32_t *) &req.dev.ifname) = 1;
101 req.table.table = get_route_table(rif);
102 req.nl.nlmsg_len = sizeof(req) - padding;
104 req.nl.nlmsg_flags = NLM_F_REQUEST;
105 if (flags & RULE_F_ADD) {
106 req.nl.nlmsg_type = RTM_NEWRULE;
107 req.nl.nlmsg_flags |= NLM_F_CREATE | NLM_F_EXCL;
109 req.rt.rtm_type = RTN_UNICAST;
111 req.nl.nlmsg_type = RTM_DELRULE;
112 req.rt.rtm_type = RTN_UNSPEC;
115 send(rtnl_sock.fd, &req, req.nl.nlmsg_len, 0);
124 static struct rtnl_addr *
125 rtnl_add_addr(struct rtnl_addr *addr, int *len, int type, const uint8_t *ipaddr)
127 addr->rta.rta_type = type;
128 memcpy(addr->ipaddr, ipaddr, 4);
129 *len += sizeof(*addr);
134 rtnl_route_request(struct relayd_interface *rif, struct relayd_host *host,
135 struct relayd_route *route, bool add)
148 struct rtnl_addr addr[3];
151 .rtm_family = AF_INET,
153 .rtm_table = RT_TABLE_MAIN,
156 .rta_type = RTA_TABLE,
157 .rta_len = sizeof(req.table),
161 .rta_len = sizeof(req.dev),
163 .addr[0].rta.rta_len = sizeof(struct rtnl_addr),
164 .addr[1].rta.rta_len = sizeof(struct rtnl_addr),
165 .addr[2].rta.rta_len = sizeof(struct rtnl_addr),
167 int pktlen = sizeof(req) - sizeof(req.addr);
168 struct rtnl_addr *addr = &req.addr[0];
169 const char *ifname = "loopback";
171 req.dev.ifindex = host->rif->sll.sll_ifindex;
172 req.table.table = get_route_table(rif);
174 req.nl.nlmsg_flags = NLM_F_REQUEST;
176 req.nl.nlmsg_type = RTM_NEWROUTE;
177 req.nl.nlmsg_flags |= NLM_F_CREATE | NLM_F_REPLACE;
179 req.rt.rtm_protocol = RTPROT_BOOT;
181 req.rt.rtm_scope = RT_SCOPE_UNIVERSE;
183 req.rt.rtm_scope = RT_SCOPE_LINK;
185 req.rt.rtm_type = RTN_UNICAST;
187 req.nl.nlmsg_type = RTM_DELROUTE;
188 req.rt.rtm_scope = RT_SCOPE_NOWHERE;
192 ifname = rif->ifname;
195 DPRINTF(2, "%s: add route to "IP_FMT"/%d via "IP_FMT" (%s)\n", ifname,
196 IP_BUF(route->dest), route->mask, IP_BUF(host->ipaddr),
199 req.rt.rtm_dst_len = route->mask;
201 addr = rtnl_add_addr(addr, &pktlen, RTA_DST, route->dest);
202 addr = rtnl_add_addr(addr, &pktlen, RTA_GATEWAY, host->ipaddr);
204 DPRINTF(2, "%s: add host route to "IP_FMT" (%s)\n", ifname,
205 IP_BUF(host->ipaddr), host->rif->ifname);
206 addr = rtnl_add_addr(addr, &pktlen, RTA_DST, host->ipaddr);
207 req.rt.rtm_dst_len = 32;
212 addr = rtnl_add_addr(addr, &pktlen, RTA_PREFSRC, local_addr);
214 req.nl.nlmsg_len = pktlen;
216 rtnl_rule_request(rif, RULE_F_DEFGW_WORKAROUND | RULE_F_ADD);
217 send(rtnl_sock.fd, &req, pktlen, 0);
219 rtnl_rule_request(rif, RULE_F_DEFGW_WORKAROUND);
224 rtnl_route_set(struct relayd_host *host, struct relayd_route *route, bool add)
226 struct relayd_interface *rif;
228 list_for_each_entry(rif, &interfaces, list) {
229 if (rif == host->rif)
232 rtnl_route_request(rif, host, route, add);
234 if (local_route_table)
235 rtnl_route_request(NULL, host, route, add);
238 void relayd_add_interface_routes(struct relayd_interface *rif)
240 rif->rt_table = route_table++;
241 rtnl_rule_request(rif, RULE_F_ADD);
244 void relayd_del_interface_routes(struct relayd_interface *rif)
246 rtnl_rule_request(rif, 0);
251 ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
254 static void rtnl_parse_newneigh(struct nlmsghdr *h)
256 struct relayd_interface *rif = NULL;
257 struct ndmsg *r = NLMSG_DATA(h);
258 const uint8_t *lladdr = NULL;
259 const uint8_t *ipaddr = NULL;
263 if (r->ndm_family != AF_INET)
266 list_for_each_entry(rif, &interfaces, list) {
267 if (rif->sll.sll_ifindex == r->ndm_ifindex)
268 goto found_interface;
273 len = h->nlmsg_len - NLMSG_LENGTH(sizeof(*r));
274 for (rta = NDA_RTA(r); RTA_OK(rta, len); rta = RTA_NEXT(rta, len)) {
275 switch(rta->rta_type) {
277 lladdr = RTA_DATA(rta);
280 ipaddr = RTA_DATA(rta);
287 if (!lladdr || !ipaddr || (r->ndm_state & (NUD_INCOMPLETE|NUD_FAILED)))
290 if (!memcmp(lladdr, "\x00\x00\x00\x00\x00\x00", ETH_ALEN))
293 DPRINTF(1, "%s: Found ARP cache entry for host "IP_FMT" ("MAC_FMT")\n",
294 rif->ifname, IP_BUF(ipaddr), MAC_BUF(lladdr));
295 relayd_refresh_host(rif, lladdr, ipaddr);
298 static void rtnl_parse_packet(void *data, int len)
302 for (h = data; NLMSG_OK(h, len); h = NLMSG_NEXT(h, len)) {
303 if (h->nlmsg_type == NLMSG_DONE ||
304 h->nlmsg_type == NLMSG_ERROR)
307 if (h->nlmsg_seq != rtnl_dump_seq)
310 if (h->nlmsg_type == RTM_NEWNEIGH)
311 rtnl_parse_newneigh(h);
315 static void rtnl_cb(struct uloop_fd *fd, unsigned int events)
317 struct sockaddr_nl nladdr;
318 static uint8_t buf[16384];
321 .iov_len = sizeof(buf),
323 struct msghdr msg = {
325 .msg_namelen = sizeof(nladdr),
333 len = recvmsg(rtnl_sock.fd, &msg, 0);
344 if (nladdr.nl_pid != 0)
347 rtnl_parse_packet(buf, len);
351 static void rtnl_dump_request(int nlmsg_type)
358 .nlmsg_len = sizeof(req),
359 .nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST,
362 .g.rtgen_family = AF_INET,
364 req.nlh.nlmsg_type = nlmsg_type;
365 req.nlh.nlmsg_seq = rtnl_seq;
366 send(rtnl_sock.fd, &req, sizeof(req), 0);
370 int relayd_rtnl_init(void)
372 struct sockaddr_nl snl_local;
374 rtnl_sock.fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
375 if (rtnl_sock.fd < 0) {
376 perror("socket(AF_NETLINK)");
380 snl_local.nl_family = AF_NETLINK;
382 if (bind(rtnl_sock.fd, (struct sockaddr *) &snl_local, sizeof(struct sockaddr_nl)) < 0) {
388 rtnl_sock.cb = rtnl_cb;
389 uloop_fd_add(&rtnl_sock, ULOOP_READ | ULOOP_EDGE_TRIGGER);
391 rtnl_seq = time(NULL);
392 rtnl_dump_seq = rtnl_seq;
393 rtnl_dump_request(RTM_GETNEIGH);
394 rtnl_rule_request(NULL, RULE_F_ADD);
399 void relayd_rtnl_done(void)
401 rtnl_rule_request(NULL, 0);
402 uloop_fd_delete(&rtnl_sock);