6cea299e1a6aabb7457456c1fc68fcca5cb12add
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
)
75 char ifname
[IFNAMSIZ
+ 1];
79 .rtm_family
= AF_INET
,
80 .rtm_table
= RT_TABLE_UNSPEC
,
81 .rtm_scope
= RT_SCOPE_UNIVERSE
,
82 .rtm_protocol
= RTPROT_BOOT
,
85 .rta
.rta_type
= FRA_PRIORITY
,
86 .rta
.rta_len
= sizeof(req
.prio
),
90 .rta_type
= FRA_TABLE
,
91 .rta_len
= sizeof(req
.table
),
94 const char *ifname
= "lo";
95 int padding
= sizeof(req
.dev
.ifname
);
100 if (!(flags
& RULE_F_DEFGW_WORKAROUND
)) {
101 req
.dev
.rta
.rta_type
= FRA_IFNAME
;
102 padding
-= strlen(ifname
) + 1;
103 strcpy(req
.dev
.ifname
, ifname
);
104 req
.dev
.rta
.rta_len
= sizeof(req
.dev
.rta
) + strlen(ifname
) + 1;
106 padding
= sizeof(req
.dev
);
109 req
.table
.table
= get_route_table(rif
);
110 req
.nl
.nlmsg_len
= sizeof(req
) - padding
;
112 req
.nl
.nlmsg_flags
= NLM_F_REQUEST
;
113 if (flags
& RULE_F_ADD
) {
114 req
.nl
.nlmsg_type
= RTM_NEWRULE
;
115 req
.nl
.nlmsg_flags
|= NLM_F_CREATE
| NLM_F_EXCL
;
117 req
.rt
.rtm_type
= RTN_UNICAST
;
119 req
.nl
.nlmsg_type
= RTM_DELRULE
;
120 req
.rt
.rtm_type
= RTN_UNSPEC
;
123 send(rtnl_sock
.fd
, &req
, req
.nl
.nlmsg_len
, 0);
132 static struct rtnl_addr
*
133 rtnl_add_addr(struct rtnl_addr
*addr
, int *len
, int type
, const uint8_t *ipaddr
)
135 addr
->rta
.rta_type
= type
;
136 memcpy(addr
->ipaddr
, ipaddr
, 4);
137 *len
+= sizeof(*addr
);
142 rtnl_route_request(struct relayd_interface
*rif
, struct relayd_host
*host
,
143 struct relayd_route
*route
, bool add
)
156 struct rtnl_addr addr
[3];
159 .rtm_family
= AF_INET
,
161 .rtm_table
= RT_TABLE_MAIN
,
164 .rta_type
= RTA_TABLE
,
165 .rta_len
= sizeof(req
.table
),
169 .rta_len
= sizeof(req
.dev
),
171 .addr
[0].rta
.rta_len
= sizeof(struct rtnl_addr
),
172 .addr
[1].rta
.rta_len
= sizeof(struct rtnl_addr
),
173 .addr
[2].rta
.rta_len
= sizeof(struct rtnl_addr
),
175 int pktlen
= sizeof(req
) - sizeof(req
.addr
);
176 struct rtnl_addr
*addr
= &req
.addr
[0];
177 const char *ifname
= "loopback";
179 req
.dev
.ifindex
= host
->rif
->sll
.sll_ifindex
;
180 req
.table
.table
= get_route_table(rif
);
182 req
.nl
.nlmsg_flags
= NLM_F_REQUEST
;
184 req
.nl
.nlmsg_type
= RTM_NEWROUTE
;
185 req
.nl
.nlmsg_flags
|= NLM_F_CREATE
| NLM_F_REPLACE
;
187 req
.rt
.rtm_protocol
= RTPROT_BOOT
;
189 req
.rt
.rtm_scope
= RT_SCOPE_UNIVERSE
;
191 req
.rt
.rtm_scope
= RT_SCOPE_LINK
;
193 req
.rt
.rtm_type
= RTN_UNICAST
;
195 req
.nl
.nlmsg_type
= RTM_DELROUTE
;
196 req
.rt
.rtm_scope
= RT_SCOPE_NOWHERE
;
200 ifname
= rif
->ifname
;
203 DPRINTF(2, "%s: add route to "IP_FMT
"/%d via "IP_FMT
" (%s)\n", ifname
,
204 IP_BUF(route
->dest
), route
->mask
, IP_BUF(host
->ipaddr
),
207 req
.rt
.rtm_dst_len
= route
->mask
;
209 addr
= rtnl_add_addr(addr
, &pktlen
, RTA_DST
, route
->dest
);
210 addr
= rtnl_add_addr(addr
, &pktlen
, RTA_GATEWAY
, host
->ipaddr
);
212 DPRINTF(2, "%s: add host route to "IP_FMT
" (%s)\n", ifname
,
213 IP_BUF(host
->ipaddr
), host
->rif
->ifname
);
214 addr
= rtnl_add_addr(addr
, &pktlen
, RTA_DST
, host
->ipaddr
);
215 req
.rt
.rtm_dst_len
= 32;
220 addr
= rtnl_add_addr(addr
, &pktlen
, RTA_PREFSRC
, local_addr
);
222 req
.nl
.nlmsg_len
= pktlen
;
224 rtnl_rule_request(rif
, RULE_F_DEFGW_WORKAROUND
| RULE_F_ADD
);
225 send(rtnl_sock
.fd
, &req
, pktlen
, 0);
227 rtnl_rule_request(rif
, RULE_F_DEFGW_WORKAROUND
);
232 rtnl_route_set(struct relayd_host
*host
, struct relayd_route
*route
, bool add
)
234 struct relayd_interface
*rif
;
236 list_for_each_entry(rif
, &interfaces
, list
) {
237 if (rif
== host
->rif
)
240 rtnl_route_request(rif
, host
, route
, add
);
242 if (local_route_table
)
243 rtnl_route_request(NULL
, host
, route
, add
);
246 void relayd_add_interface_routes(struct relayd_interface
*rif
)
248 rif
->rt_table
= route_table
++;
249 rtnl_rule_request(rif
, RULE_F_ADD
);
252 void relayd_del_interface_routes(struct relayd_interface
*rif
)
254 rtnl_rule_request(rif
, 0);
259 ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
262 static void rtnl_parse_newneigh(struct nlmsghdr
*h
)
264 struct relayd_interface
*rif
= NULL
;
265 struct ndmsg
*r
= NLMSG_DATA(h
);
266 const uint8_t *lladdr
= NULL
;
267 const uint8_t *ipaddr
= NULL
;
271 if (r
->ndm_family
!= AF_INET
)
274 list_for_each_entry(rif
, &interfaces
, list
) {
275 if (rif
->sll
.sll_ifindex
== r
->ndm_ifindex
)
276 goto found_interface
;
281 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(*r
));
282 for (rta
= NDA_RTA(r
); RTA_OK(rta
, len
); rta
= RTA_NEXT(rta
, len
)) {
283 switch(rta
->rta_type
) {
285 lladdr
= RTA_DATA(rta
);
288 ipaddr
= RTA_DATA(rta
);
295 if (!lladdr
|| !ipaddr
|| (r
->ndm_state
& (NUD_INCOMPLETE
|NUD_FAILED
)))
298 if (!memcmp(lladdr
, "\x00\x00\x00\x00\x00\x00", ETH_ALEN
))
301 DPRINTF(1, "%s: Found ARP cache entry for host "IP_FMT
" ("MAC_FMT
")\n",
302 rif
->ifname
, IP_BUF(ipaddr
), MAC_BUF(lladdr
));
303 relayd_refresh_host(rif
, lladdr
, ipaddr
);
306 static void rtnl_parse_packet(void *data
, int len
)
310 for (h
= data
; NLMSG_OK(h
, len
); h
= NLMSG_NEXT(h
, len
)) {
311 if (h
->nlmsg_type
== NLMSG_DONE
||
312 h
->nlmsg_type
== NLMSG_ERROR
)
315 if (h
->nlmsg_seq
!= rtnl_dump_seq
)
318 if (h
->nlmsg_type
== RTM_NEWNEIGH
)
319 rtnl_parse_newneigh(h
);
323 static void rtnl_cb(struct uloop_fd
*fd
, unsigned int events
)
325 struct sockaddr_nl nladdr
;
326 static uint8_t buf
[16384];
329 .iov_len
= sizeof(buf
),
331 struct msghdr msg
= {
333 .msg_namelen
= sizeof(nladdr
),
341 len
= recvmsg(rtnl_sock
.fd
, &msg
, 0);
352 if (nladdr
.nl_pid
!= 0)
355 rtnl_parse_packet(buf
, len
);
359 static void rtnl_dump_request(int nlmsg_type
)
366 .nlmsg_len
= sizeof(req
),
367 .nlmsg_flags
= NLM_F_ROOT
|NLM_F_MATCH
|NLM_F_REQUEST
,
370 .g
.rtgen_family
= AF_INET
,
372 req
.nlh
.nlmsg_type
= nlmsg_type
;
373 req
.nlh
.nlmsg_seq
= rtnl_seq
;
374 send(rtnl_sock
.fd
, &req
, sizeof(req
), 0);
378 int relayd_rtnl_init(void)
380 struct sockaddr_nl snl_local
= {};
382 rtnl_sock
.fd
= socket(AF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
383 if (rtnl_sock
.fd
< 0) {
384 perror("socket(AF_NETLINK)");
388 snl_local
.nl_family
= AF_NETLINK
;
390 if (bind(rtnl_sock
.fd
, (struct sockaddr
*) &snl_local
, sizeof(struct sockaddr_nl
)) < 0) {
396 rtnl_sock
.cb
= rtnl_cb
;
397 uloop_fd_add(&rtnl_sock
, ULOOP_READ
| ULOOP_EDGE_TRIGGER
);
399 rtnl_seq
= time(NULL
);
400 rtnl_dump_seq
= rtnl_seq
;
401 rtnl_dump_request(RTM_GETNEIGH
);
402 rtnl_rule_request(NULL
, RULE_F_ADD
);
407 void relayd_rtnl_done(void)
409 rtnl_rule_request(NULL
, 0);
410 uloop_fd_delete(&rtnl_sock
);