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 rtnl_rule_request(struct relayd_interface
*rif
, bool add
, bool defgw_workaround
)
57 char ifname
[IFNAMSIZ
+ 1];
61 .rtm_family
= AF_INET
,
62 .rtm_table
= RT_TABLE_UNSPEC
,
63 .rtm_scope
= RT_SCOPE_UNIVERSE
,
64 .rtm_protocol
= RTPROT_BOOT
,
67 .rta_type
= FRA_TABLE
,
68 .rta_len
= sizeof(req
.table
),
72 int padding
= sizeof(req
.dev
.ifname
);
74 if (!defgw_workaround
) {
75 req
.dev
.rta
.rta_type
= FRA_IFNAME
;
76 padding
-= strlen(rif
->ifname
) + 1;
77 strcpy(req
.dev
.ifname
, rif
->ifname
);
78 req
.dev
.rta
.rta_len
= sizeof(req
.dev
.rta
) + strlen(rif
->ifname
) + 1;
80 req
.dev
.rta
.rta_type
= FRA_PRIORITY
;
81 req
.dev
.rta
.rta_len
= sizeof(req
.dev
.rta
) + sizeof(uint32_t);
82 padding
-= sizeof(uint32_t);
83 *((uint32_t *) &req
.dev
.ifname
) = 1;
85 req
.table
.table
= rif
->rt_table
;
86 req
.nl
.nlmsg_len
= sizeof(req
) - padding
;
88 req
.nl
.nlmsg_flags
= NLM_F_REQUEST
;
90 req
.nl
.nlmsg_type
= RTM_NEWRULE
;
91 req
.nl
.nlmsg_flags
|= NLM_F_CREATE
| NLM_F_EXCL
;
93 req
.rt
.rtm_type
= RTN_UNICAST
;
95 req
.nl
.nlmsg_type
= RTM_DELRULE
;
96 req
.rt
.rtm_type
= RTN_UNSPEC
;
99 send(rtnl_sock
.fd
, &req
, req
.nl
.nlmsg_len
, 0);
105 rtnl_route_request(struct relayd_interface
*rif
, struct relayd_host
*host
,
106 struct relayd_route
*route
, bool add
)
129 .rtm_family
= AF_INET
,
131 .rtm_table
= RT_TABLE_MAIN
,
134 .rta_type
= RTA_TABLE
,
135 .rta_len
= sizeof(req
.table
),
138 .rta_len
= sizeof(req
.dst
),
142 .rta_len
= sizeof(req
.dev
),
145 .rta_type
= RTA_GATEWAY
,
146 .rta_len
= sizeof(req
.gw
),
149 int pktlen
= sizeof(req
);
151 req
.dev
.ifindex
= host
->rif
->sll
.sll_ifindex
;
152 req
.table
.table
= rif
->rt_table
;
154 req
.nl
.nlmsg_flags
= NLM_F_REQUEST
;
156 req
.nl
.nlmsg_type
= RTM_NEWROUTE
;
157 req
.nl
.nlmsg_flags
|= NLM_F_CREATE
| NLM_F_REPLACE
;
159 req
.rt
.rtm_protocol
= RTPROT_BOOT
;
161 req
.rt
.rtm_scope
= RT_SCOPE_UNIVERSE
;
163 req
.rt
.rtm_scope
= RT_SCOPE_LINK
;
165 req
.rt
.rtm_type
= RTN_UNICAST
;
167 req
.nl
.nlmsg_type
= RTM_DELROUTE
;
168 req
.rt
.rtm_scope
= RT_SCOPE_NOWHERE
;
171 req
.dst
.rta
.rta_type
= RTA_DST
;
173 DPRINTF(2, "%s: add route to "IP_FMT
"/%d via "IP_FMT
"\n",
174 host
->rif
->ifname
, IP_BUF(route
->dest
), route
->mask
, IP_BUF(host
->ipaddr
));
176 /* No DST -> reuse the DST attribute as the gateway attribute */
177 memcpy(req
.dst
.ipaddr
, host
->ipaddr
, sizeof(req
.dst
.ipaddr
));
178 req
.dst
.rta
.rta_type
= RTA_GATEWAY
;
179 pktlen
-= sizeof(req
.gw
);
180 req
.rt
.rtm_dst_len
= 0;
182 memcpy(req
.gw
.ipaddr
, host
->ipaddr
, sizeof(req
.gw
.ipaddr
));
183 memcpy(req
.dst
.ipaddr
, route
->dest
, sizeof(req
.dst
.ipaddr
));
184 req
.rt
.rtm_dst_len
= route
->mask
;
187 pktlen
-= sizeof(req
.gw
);
188 req
.rt
.rtm_dst_len
= 32;
189 memcpy(req
.dst
.ipaddr
, host
->ipaddr
, sizeof(req
.dst
.ipaddr
));
192 req
.nl
.nlmsg_len
= pktlen
;
194 rtnl_rule_request(rif
, true, true);
195 send(rtnl_sock
.fd
, &req
, pktlen
, 0);
197 rtnl_rule_request(rif
, false, true);
202 rtnl_route_set(struct relayd_host
*host
, struct relayd_route
*route
, bool add
)
204 struct relayd_interface
*rif
;
206 list_for_each_entry(rif
, &interfaces
, list
) {
207 if (rif
== host
->rif
)
210 rtnl_route_request(rif
, host
, route
, add
);
214 void relayd_add_interface_routes(struct relayd_interface
*rif
)
216 rif
->rt_table
= route_table
++;
217 rtnl_rule_request(rif
, true, false);
220 void relayd_del_interface_routes(struct relayd_interface
*rif
)
222 rtnl_rule_request(rif
, false, false);
227 ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
230 static void rtnl_parse_newneigh(struct nlmsghdr
*h
)
232 struct relayd_interface
*rif
= NULL
;
233 struct ndmsg
*r
= NLMSG_DATA(h
);
234 const uint8_t *lladdr
= NULL
;
235 const uint8_t *ipaddr
= NULL
;
239 if (r
->ndm_family
!= AF_INET
)
242 list_for_each_entry(rif
, &interfaces
, list
) {
243 if (rif
->sll
.sll_ifindex
== r
->ndm_ifindex
)
244 goto found_interface
;
249 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(*r
));
250 for (rta
= NDA_RTA(r
); RTA_OK(rta
, len
); rta
= RTA_NEXT(rta
, len
)) {
251 switch(rta
->rta_type
) {
253 lladdr
= RTA_DATA(rta
);
256 ipaddr
= RTA_DATA(rta
);
263 if (!lladdr
|| !ipaddr
|| (r
->ndm_state
& (NUD_INCOMPLETE
|NUD_FAILED
)))
266 if (!memcmp(lladdr
, "\x00\x00\x00\x00\x00\x00", ETH_ALEN
))
269 DPRINTF(1, "%s: Found ARP cache entry for host "IP_FMT
" ("MAC_FMT
")\n",
270 rif
->ifname
, IP_BUF(ipaddr
), MAC_BUF(lladdr
));
271 relayd_refresh_host(rif
, lladdr
, ipaddr
);
274 static void rtnl_parse_packet(void *data
, int len
)
278 for (h
= data
; NLMSG_OK(h
, len
); h
= NLMSG_NEXT(h
, len
)) {
279 if (h
->nlmsg_type
== NLMSG_DONE
||
280 h
->nlmsg_type
== NLMSG_ERROR
)
283 if (h
->nlmsg_seq
!= rtnl_dump_seq
)
286 if (h
->nlmsg_type
== RTM_NEWNEIGH
)
287 rtnl_parse_newneigh(h
);
291 static void rtnl_cb(struct uloop_fd
*fd
, unsigned int events
)
293 struct sockaddr_nl nladdr
;
294 static uint8_t buf
[16384];
297 .iov_len
= sizeof(buf
),
299 struct msghdr msg
= {
301 .msg_namelen
= sizeof(nladdr
),
309 len
= recvmsg(rtnl_sock
.fd
, &msg
, 0);
320 if (nladdr
.nl_pid
!= 0)
323 rtnl_parse_packet(buf
, len
);
327 int relayd_rtnl_init(void)
329 struct sockaddr_nl snl_local
;
335 .nlmsg_len
= sizeof(req
),
336 .nlmsg_type
= RTM_GETNEIGH
,
337 .nlmsg_flags
= NLM_F_ROOT
|NLM_F_MATCH
|NLM_F_REQUEST
,
340 .g
.rtgen_family
= AF_INET
,
343 rtnl_sock
.fd
= socket(AF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
344 if (rtnl_sock
.fd
< 0) {
345 perror("socket(AF_NETLINK)");
349 snl_local
.nl_family
= AF_NETLINK
;
351 if (bind(rtnl_sock
.fd
, (struct sockaddr
*) &snl_local
, sizeof(struct sockaddr_nl
)) < 0) {
357 rtnl_sock
.cb
= rtnl_cb
;
358 uloop_fd_add(&rtnl_sock
, ULOOP_READ
| ULOOP_EDGE_TRIGGER
);
360 rtnl_seq
= time(NULL
);
361 rtnl_dump_seq
= rtnl_seq
;
362 req
.nlh
.nlmsg_seq
= rtnl_seq
;
363 send(rtnl_sock
.fd
, &req
, sizeof(req
), 0);
368 void relayd_rtnl_done(void)
370 uloop_fd_delete(&rtnl_sock
);