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),
51 rtnl_rule_request(struct relayd_interface
*rif
, int flags
)
62 char ifname
[IFNAMSIZ
+ 1];
66 .rtm_family
= AF_INET
,
67 .rtm_table
= RT_TABLE_UNSPEC
,
68 .rtm_scope
= RT_SCOPE_UNIVERSE
,
69 .rtm_protocol
= RTPROT_BOOT
,
72 .rta_type
= FRA_TABLE
,
73 .rta_len
= sizeof(req
.table
),
77 int padding
= sizeof(req
.dev
.ifname
);
79 if (!(flags
& RULE_F_DEFGW_WORKAROUND
)) {
80 req
.dev
.rta
.rta_type
= FRA_IFNAME
;
81 padding
-= strlen(rif
->ifname
) + 1;
82 strcpy(req
.dev
.ifname
, rif
->ifname
);
83 req
.dev
.rta
.rta_len
= sizeof(req
.dev
.rta
) + strlen(rif
->ifname
) + 1;
85 req
.dev
.rta
.rta_type
= FRA_PRIORITY
;
86 req
.dev
.rta
.rta_len
= sizeof(req
.dev
.rta
) + sizeof(uint32_t);
87 padding
-= sizeof(uint32_t);
88 *((uint32_t *) &req
.dev
.ifname
) = 1;
90 req
.table
.table
= rif
->rt_table
;
91 req
.nl
.nlmsg_len
= sizeof(req
) - padding
;
93 req
.nl
.nlmsg_flags
= NLM_F_REQUEST
;
94 if (flags
& RULE_F_ADD
) {
95 req
.nl
.nlmsg_type
= RTM_NEWRULE
;
96 req
.nl
.nlmsg_flags
|= NLM_F_CREATE
| NLM_F_EXCL
;
98 req
.rt
.rtm_type
= RTN_UNICAST
;
100 req
.nl
.nlmsg_type
= RTM_DELRULE
;
101 req
.rt
.rtm_type
= RTN_UNSPEC
;
104 send(rtnl_sock
.fd
, &req
, req
.nl
.nlmsg_len
, 0);
110 rtnl_route_request(struct relayd_interface
*rif
, struct relayd_host
*host
,
111 struct relayd_route
*route
, bool add
)
134 .rtm_family
= AF_INET
,
136 .rtm_table
= RT_TABLE_MAIN
,
139 .rta_type
= RTA_TABLE
,
140 .rta_len
= sizeof(req
.table
),
143 .rta_len
= sizeof(req
.dst
),
147 .rta_len
= sizeof(req
.dev
),
150 .rta_type
= RTA_GATEWAY
,
151 .rta_len
= sizeof(req
.gw
),
154 int pktlen
= sizeof(req
);
156 req
.dev
.ifindex
= host
->rif
->sll
.sll_ifindex
;
157 req
.table
.table
= rif
->rt_table
;
159 req
.nl
.nlmsg_flags
= NLM_F_REQUEST
;
161 req
.nl
.nlmsg_type
= RTM_NEWROUTE
;
162 req
.nl
.nlmsg_flags
|= NLM_F_CREATE
| NLM_F_REPLACE
;
164 req
.rt
.rtm_protocol
= RTPROT_BOOT
;
166 req
.rt
.rtm_scope
= RT_SCOPE_UNIVERSE
;
168 req
.rt
.rtm_scope
= RT_SCOPE_LINK
;
170 req
.rt
.rtm_type
= RTN_UNICAST
;
172 req
.nl
.nlmsg_type
= RTM_DELROUTE
;
173 req
.rt
.rtm_scope
= RT_SCOPE_NOWHERE
;
176 req
.dst
.rta
.rta_type
= RTA_DST
;
178 DPRINTF(2, "%s: add route to "IP_FMT
"/%d via "IP_FMT
"\n",
179 host
->rif
->ifname
, IP_BUF(route
->dest
), route
->mask
, IP_BUF(host
->ipaddr
));
181 /* No DST -> reuse the DST attribute as the gateway attribute */
182 memcpy(req
.dst
.ipaddr
, host
->ipaddr
, sizeof(req
.dst
.ipaddr
));
183 req
.dst
.rta
.rta_type
= RTA_GATEWAY
;
184 pktlen
-= sizeof(req
.gw
);
185 req
.rt
.rtm_dst_len
= 0;
187 memcpy(req
.gw
.ipaddr
, host
->ipaddr
, sizeof(req
.gw
.ipaddr
));
188 memcpy(req
.dst
.ipaddr
, route
->dest
, sizeof(req
.dst
.ipaddr
));
189 req
.rt
.rtm_dst_len
= route
->mask
;
192 pktlen
-= sizeof(req
.gw
);
193 req
.rt
.rtm_dst_len
= 32;
194 memcpy(req
.dst
.ipaddr
, host
->ipaddr
, sizeof(req
.dst
.ipaddr
));
197 req
.nl
.nlmsg_len
= pktlen
;
199 rtnl_rule_request(rif
, RULE_F_DEFGW_WORKAROUND
| RULE_F_ADD
);
200 send(rtnl_sock
.fd
, &req
, pktlen
, 0);
202 rtnl_rule_request(rif
, RULE_F_DEFGW_WORKAROUND
);
207 rtnl_route_set(struct relayd_host
*host
, struct relayd_route
*route
, bool add
)
209 struct relayd_interface
*rif
;
211 list_for_each_entry(rif
, &interfaces
, list
) {
212 if (rif
== host
->rif
)
215 rtnl_route_request(rif
, host
, route
, add
);
219 void relayd_add_interface_routes(struct relayd_interface
*rif
)
221 rif
->rt_table
= route_table
++;
222 rtnl_rule_request(rif
, RULE_F_ADD
);
225 void relayd_del_interface_routes(struct relayd_interface
*rif
)
227 rtnl_rule_request(rif
, 0);
232 ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
235 static void rtnl_parse_newneigh(struct nlmsghdr
*h
)
237 struct relayd_interface
*rif
= NULL
;
238 struct ndmsg
*r
= NLMSG_DATA(h
);
239 const uint8_t *lladdr
= NULL
;
240 const uint8_t *ipaddr
= NULL
;
244 if (r
->ndm_family
!= AF_INET
)
247 list_for_each_entry(rif
, &interfaces
, list
) {
248 if (rif
->sll
.sll_ifindex
== r
->ndm_ifindex
)
249 goto found_interface
;
254 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(*r
));
255 for (rta
= NDA_RTA(r
); RTA_OK(rta
, len
); rta
= RTA_NEXT(rta
, len
)) {
256 switch(rta
->rta_type
) {
258 lladdr
= RTA_DATA(rta
);
261 ipaddr
= RTA_DATA(rta
);
268 if (!lladdr
|| !ipaddr
|| (r
->ndm_state
& (NUD_INCOMPLETE
|NUD_FAILED
)))
271 if (!memcmp(lladdr
, "\x00\x00\x00\x00\x00\x00", ETH_ALEN
))
274 DPRINTF(1, "%s: Found ARP cache entry for host "IP_FMT
" ("MAC_FMT
")\n",
275 rif
->ifname
, IP_BUF(ipaddr
), MAC_BUF(lladdr
));
276 relayd_refresh_host(rif
, lladdr
, ipaddr
);
279 static void rtnl_parse_packet(void *data
, int len
)
283 for (h
= data
; NLMSG_OK(h
, len
); h
= NLMSG_NEXT(h
, len
)) {
284 if (h
->nlmsg_type
== NLMSG_DONE
||
285 h
->nlmsg_type
== NLMSG_ERROR
)
288 if (h
->nlmsg_seq
!= rtnl_dump_seq
)
291 if (h
->nlmsg_type
== RTM_NEWNEIGH
)
292 rtnl_parse_newneigh(h
);
296 static void rtnl_cb(struct uloop_fd
*fd
, unsigned int events
)
298 struct sockaddr_nl nladdr
;
299 static uint8_t buf
[16384];
302 .iov_len
= sizeof(buf
),
304 struct msghdr msg
= {
306 .msg_namelen
= sizeof(nladdr
),
314 len
= recvmsg(rtnl_sock
.fd
, &msg
, 0);
325 if (nladdr
.nl_pid
!= 0)
328 rtnl_parse_packet(buf
, len
);
332 static void rtnl_dump_request(int nlmsg_type
)
339 .nlmsg_len
= sizeof(req
),
340 .nlmsg_flags
= NLM_F_ROOT
|NLM_F_MATCH
|NLM_F_REQUEST
,
343 .g
.rtgen_family
= AF_INET
,
345 req
.nlh
.nlmsg_type
= nlmsg_type
;
346 req
.nlh
.nlmsg_seq
= rtnl_seq
;
347 send(rtnl_sock
.fd
, &req
, sizeof(req
), 0);
351 int relayd_rtnl_init(void)
353 struct sockaddr_nl snl_local
;
355 rtnl_sock
.fd
= socket(AF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
356 if (rtnl_sock
.fd
< 0) {
357 perror("socket(AF_NETLINK)");
361 snl_local
.nl_family
= AF_NETLINK
;
363 if (bind(rtnl_sock
.fd
, (struct sockaddr
*) &snl_local
, sizeof(struct sockaddr_nl
)) < 0) {
369 rtnl_sock
.cb
= rtnl_cb
;
370 uloop_fd_add(&rtnl_sock
, ULOOP_READ
| ULOOP_EDGE_TRIGGER
);
372 rtnl_seq
= time(NULL
);
373 rtnl_dump_seq
= rtnl_seq
;
374 rtnl_dump_request(RTM_GETNEIGH
);
379 void relayd_rtnl_done(void)
381 uloop_fd_delete(&rtnl_sock
);