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>
27 static struct uloop_fd rtnl_sock
;
28 static unsigned int rtnl_seq
, rtnl_dump_seq
;
30 static void rtnl_flush(void)
34 fd
= open("/proc/sys/net/ipv4/route/flush", O_WRONLY
);
42 static void rtnl_route_set(struct relayd_host
*host
, bool add
)
57 .nlmsg_len
= sizeof(req
),
60 .rtm_family
= AF_INET
,
62 .rtm_table
= RT_TABLE_MAIN
,
66 .rta_len
= sizeof(req
.dst
),
70 .rta_len
= sizeof(req
.dev
),
74 memcpy(req
.dst
.ipaddr
, host
->ipaddr
, sizeof(req
.dst
.ipaddr
));
75 req
.dev
.ifindex
= host
->rif
->sll
.sll_ifindex
;
77 req
.nl
.nlmsg_flags
= NLM_F_REQUEST
;
79 req
.nl
.nlmsg_type
= RTM_NEWROUTE
;
80 req
.nl
.nlmsg_flags
|= NLM_F_CREATE
| NLM_F_REPLACE
;
82 req
.rt
.rtm_protocol
= RTPROT_BOOT
;
83 req
.rt
.rtm_scope
= RT_SCOPE_LINK
;
84 req
.rt
.rtm_type
= RTN_UNICAST
;
86 req
.nl
.nlmsg_type
= RTM_DELROUTE
;
87 req
.rt
.rtm_scope
= RT_SCOPE_NOWHERE
;
90 send(rtnl_sock
.fd
, &req
, sizeof(req
), 0);
94 void relayd_add_route(struct relayd_host
*host
)
96 rtnl_route_set(host
, true);
99 void relayd_del_route(struct relayd_host
*host
)
101 rtnl_route_set(host
, false);
106 ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
109 static void rtnl_parse_newneigh(struct nlmsghdr
*h
)
111 struct relayd_interface
*rif
= NULL
;
112 struct ndmsg
*r
= NLMSG_DATA(h
);
113 const uint8_t *lladdr
= NULL
;
114 const uint8_t *ipaddr
= NULL
;
118 if (r
->ndm_family
!= AF_INET
)
121 list_for_each_entry(rif
, &interfaces
, list
) {
122 if (rif
->sll
.sll_ifindex
== r
->ndm_ifindex
)
123 goto found_interface
;
128 len
= h
->nlmsg_len
- NLMSG_LENGTH(sizeof(*r
));
129 for (rta
= NDA_RTA(r
); RTA_OK(rta
, len
); rta
= RTA_NEXT(rta
, len
)) {
130 switch(rta
->rta_type
) {
132 lladdr
= RTA_DATA(rta
);
135 ipaddr
= RTA_DATA(rta
);
142 if (!lladdr
|| !ipaddr
|| (r
->ndm_state
& (NUD_INCOMPLETE
|NUD_FAILED
)))
145 if (!memcmp(lladdr
, "\x00\x00\x00\x00\x00\x00", ETH_ALEN
))
148 DPRINTF(1, "%s: Found ARP cache entry for host "IP_FMT
" ("MAC_FMT
")\n",
149 rif
->ifname
, IP_BUF(ipaddr
), MAC_BUF(lladdr
));
150 relayd_refresh_host(rif
, lladdr
, ipaddr
);
153 static void rtnl_parse_packet(void *data
, int len
)
157 for (h
= data
; NLMSG_OK(h
, len
); h
= NLMSG_NEXT(h
, len
)) {
158 if (h
->nlmsg_type
== NLMSG_DONE
||
159 h
->nlmsg_type
== NLMSG_ERROR
)
162 if (h
->nlmsg_seq
!= rtnl_dump_seq
)
165 if (h
->nlmsg_type
== RTM_NEWNEIGH
)
166 rtnl_parse_newneigh(h
);
170 static void rtnl_cb(struct uloop_fd
*fd
, unsigned int events
)
172 struct sockaddr_nl nladdr
;
173 static uint8_t buf
[16384];
176 .iov_len
= sizeof(buf
),
178 struct msghdr msg
= {
180 .msg_namelen
= sizeof(nladdr
),
188 len
= recvmsg(rtnl_sock
.fd
, &msg
, 0);
199 if (nladdr
.nl_pid
!= 0)
202 rtnl_parse_packet(buf
, len
);
206 int relayd_rtnl_init(void)
208 struct sockaddr_nl snl_local
;
214 .nlmsg_len
= sizeof(req
),
215 .nlmsg_type
= RTM_GETNEIGH
,
216 .nlmsg_flags
= NLM_F_ROOT
|NLM_F_MATCH
|NLM_F_REQUEST
,
219 .g
.rtgen_family
= AF_INET
,
222 rtnl_sock
.fd
= socket(AF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
223 if (rtnl_sock
.fd
< 0) {
224 perror("socket(AF_NETLINK)");
228 snl_local
.nl_family
= AF_NETLINK
;
230 if (bind(rtnl_sock
.fd
, (struct sockaddr
*) &snl_local
, sizeof(struct sockaddr_nl
)) < 0) {
236 rtnl_sock
.cb
= rtnl_cb
;
237 uloop_fd_add(&rtnl_sock
, ULOOP_READ
| ULOOP_EDGE_TRIGGER
);
239 rtnl_seq
= time(NULL
);
240 rtnl_dump_seq
= rtnl_seq
;
241 req
.nlh
.nlmsg_seq
= rtnl_seq
;
242 send(rtnl_sock
.fd
, &req
, sizeof(req
), 0);
247 void relayd_rtnl_done(void)
249 uloop_fd_delete(&rtnl_sock
);