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_route_request(struct relayd_interface
*rif
, struct relayd_host
*host
,
47 struct relayd_route
*route
, bool add
)
70 .rtm_family
= AF_INET
,
72 .rtm_table
= RT_TABLE_MAIN
,
75 .rta_type
= RTA_TABLE
,
76 .rta_len
= sizeof(req
.table
),
79 .rta_len
= sizeof(req
.dst
),
83 .rta_len
= sizeof(req
.dev
),
86 .rta_type
= RTA_GATEWAY
,
87 .rta_len
= sizeof(req
.gw
),
90 int pktlen
= sizeof(req
);
92 req
.dev
.ifindex
= host
->rif
->sll
.sll_ifindex
;
93 req
.table
.table
= rif
->rt_table
;
95 req
.nl
.nlmsg_flags
= NLM_F_REQUEST
;
97 req
.nl
.nlmsg_type
= RTM_NEWROUTE
;
98 req
.nl
.nlmsg_flags
|= NLM_F_CREATE
| NLM_F_REPLACE
;
100 req
.rt
.rtm_protocol
= RTPROT_BOOT
;
102 req
.rt
.rtm_scope
= RT_SCOPE_UNIVERSE
;
104 req
.rt
.rtm_scope
= RT_SCOPE_LINK
;
106 req
.rt
.rtm_type
= RTN_UNICAST
;
108 req
.nl
.nlmsg_type
= RTM_DELROUTE
;
109 req
.rt
.rtm_scope
= RT_SCOPE_NOWHERE
;
112 req
.dst
.rta
.rta_type
= RTA_DST
;
114 DPRINTF(2, "%s: add route to "IP_FMT
"/%d via "IP_FMT
"\n",
115 host
->rif
->ifname
, IP_BUF(route
->dest
), route
->mask
, IP_BUF(host
->ipaddr
));
117 /* No DST -> reuse the DST attribute as the gateway attribute */
118 memcpy(req
.dst
.ipaddr
, host
->ipaddr
, sizeof(req
.dst
.ipaddr
));
119 req
.dst
.rta
.rta_type
= RTA_GATEWAY
;
120 pktlen
-= sizeof(req
.gw
);
121 req
.rt
.rtm_dst_len
= 0;
123 memcpy(req
.gw
.ipaddr
, host
->ipaddr
, sizeof(req
.gw
.ipaddr
));
124 memcpy(req
.dst
.ipaddr
, route
->dest
, sizeof(req
.dst
.ipaddr
));
125 req
.rt
.rtm_dst_len
= route
->mask
;
128 pktlen
-= sizeof(req
.gw
);
129 req
.rt
.rtm_dst_len
= 32;
130 memcpy(req
.dst
.ipaddr
, host
->ipaddr
, sizeof(req
.dst
.ipaddr
));
133 req
.nl
.nlmsg_len
= pktlen
;
134 send(rtnl_sock
.fd
, &req
, pktlen
, 0);
138 static void rtnl_rule_request(struct relayd_interface
*rif
, bool add
)
149 char ifname
[IFNAMSIZ
+ 1];
153 .rtm_family
= AF_INET
,
154 .rtm_table
= RT_TABLE_UNSPEC
,
155 .rtm_scope
= RT_SCOPE_UNIVERSE
,
156 .rtm_protocol
= RTPROT_BOOT
,
159 .rta_type
= FRA_TABLE
,
160 .rta_len
= sizeof(req
.table
),
163 .rta_type
= FRA_IFNAME
,
167 int padding
= sizeof(req
.dev
.ifname
);
168 padding
-= strlen(rif
->ifname
) + 1;
170 strcpy(req
.dev
.ifname
, rif
->ifname
);
171 req
.dev
.rta
.rta_len
= sizeof(req
.dev
.rta
) + strlen(rif
->ifname
) + 1;
172 req
.table
.table
= rif
->rt_table
;
173 req
.nl
.nlmsg_len
= sizeof(req
) - padding
;
175 req
.nl
.nlmsg_flags
= NLM_F_REQUEST
;
177 req
.nl
.nlmsg_type
= RTM_NEWRULE
;
178 req
.nl
.nlmsg_flags
|= NLM_F_CREATE
| NLM_F_EXCL
;
180 req
.rt
.rtm_type
= RTN_UNICAST
;
182 req
.nl
.nlmsg_type
= RTM_DELRULE
;
183 req
.rt
.rtm_type
= RTN_UNSPEC
;
186 send(rtnl_sock
.fd
, &req
, req
.nl
.nlmsg_len
, 0);
191 rtnl_route_set(struct relayd_host
*host
, struct relayd_route
*route
, bool add
)
193 struct relayd_interface
*rif
;
195 list_for_each_entry(rif
, &interfaces
, list
) {
196 if (rif
== host
->rif
)
199 rtnl_route_request(rif
, host
, route
, add
);
203 void relayd_add_interface_routes(struct relayd_interface
*rif
)
205 rif
->rt_table
= route_table
++;
206 rtnl_rule_request(rif
, true);
209 void relayd_del_interface_routes(struct relayd_interface
*rif
)
211 rtnl_rule_request(rif
, false);
216 ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
219 static void rtnl_parse_newneigh(struct nlmsghdr
*h
)
221 struct relayd_interface
*rif
= NULL
;
222 struct ndmsg
*r
= NLMSG_DATA(h
);
223 const uint8_t *lladdr
= NULL
;
224 const uint8_t *ipaddr
= NULL
;
228 if (r
->ndm_family
!= AF_INET
)
231 list_for_each_entry(rif
, &interfaces
, list
) {
232 if (rif
->sll
.sll_ifindex
== r
->ndm_ifindex
)
233 goto found_interface
;
238 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(*r
));
239 for (rta
= NDA_RTA(r
); RTA_OK(rta
, len
); rta
= RTA_NEXT(rta
, len
)) {
240 switch(rta
->rta_type
) {
242 lladdr
= RTA_DATA(rta
);
245 ipaddr
= RTA_DATA(rta
);
252 if (!lladdr
|| !ipaddr
|| (r
->ndm_state
& (NUD_INCOMPLETE
|NUD_FAILED
)))
255 if (!memcmp(lladdr
, "\x00\x00\x00\x00\x00\x00", ETH_ALEN
))
258 DPRINTF(1, "%s: Found ARP cache entry for host "IP_FMT
" ("MAC_FMT
")\n",
259 rif
->ifname
, IP_BUF(ipaddr
), MAC_BUF(lladdr
));
260 relayd_refresh_host(rif
, lladdr
, ipaddr
);
263 static void rtnl_parse_packet(void *data
, int len
)
267 for (h
= data
; NLMSG_OK(h
, len
); h
= NLMSG_NEXT(h
, len
)) {
268 if (h
->nlmsg_type
== NLMSG_DONE
||
269 h
->nlmsg_type
== NLMSG_ERROR
)
272 if (h
->nlmsg_seq
!= rtnl_dump_seq
)
275 if (h
->nlmsg_type
== RTM_NEWNEIGH
)
276 rtnl_parse_newneigh(h
);
280 static void rtnl_cb(struct uloop_fd
*fd
, unsigned int events
)
282 struct sockaddr_nl nladdr
;
283 static uint8_t buf
[16384];
286 .iov_len
= sizeof(buf
),
288 struct msghdr msg
= {
290 .msg_namelen
= sizeof(nladdr
),
298 len
= recvmsg(rtnl_sock
.fd
, &msg
, 0);
309 if (nladdr
.nl_pid
!= 0)
312 rtnl_parse_packet(buf
, len
);
316 int relayd_rtnl_init(void)
318 struct sockaddr_nl snl_local
;
324 .nlmsg_len
= sizeof(req
),
325 .nlmsg_type
= RTM_GETNEIGH
,
326 .nlmsg_flags
= NLM_F_ROOT
|NLM_F_MATCH
|NLM_F_REQUEST
,
329 .g
.rtgen_family
= AF_INET
,
332 rtnl_sock
.fd
= socket(AF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
333 if (rtnl_sock
.fd
< 0) {
334 perror("socket(AF_NETLINK)");
338 snl_local
.nl_family
= AF_NETLINK
;
340 if (bind(rtnl_sock
.fd
, (struct sockaddr
*) &snl_local
, sizeof(struct sockaddr_nl
)) < 0) {
346 rtnl_sock
.cb
= rtnl_cb
;
347 uloop_fd_add(&rtnl_sock
, ULOOP_READ
| ULOOP_EDGE_TRIGGER
);
349 rtnl_seq
= time(NULL
);
350 rtnl_dump_seq
= rtnl_seq
;
351 req
.nlh
.nlmsg_seq
= rtnl_seq
;
352 send(rtnl_sock
.fd
, &req
, sizeof(req
), 0);
357 void relayd_rtnl_done(void)
359 uloop_fd_delete(&rtnl_sock
);