Move the rtnl code to a separate source file
[project/relayd.git] / route.c
1 /*
2  *   Copyright (C) 2010 Felix Fietkau <nbd@openwrt.org>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 #include <sys/socket.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <errno.h>
22 #include <unistd.h>
23
24 #include "relayd.h"
25
26 static struct uloop_fd rtnl_sock;
27 static unsigned int rtnl_seq, rtnl_dump_seq;
28
29 static void rtnl_route_set(struct relayd_host *host, bool add)
30 {
31         static struct {
32                 struct nlmsghdr nl;
33                 struct rtmsg rt;
34                 struct {
35                         struct rtattr rta;
36                         uint8_t ipaddr[4];
37                 } __packed dst;
38                 struct {
39                         struct rtattr rta;
40                         int ifindex;
41                 } __packed dev;
42         } __packed req;
43
44         memset(&req, 0, sizeof(req));
45
46         req.nl.nlmsg_len = sizeof(req);
47         req.rt.rtm_family = AF_INET;
48         req.rt.rtm_dst_len = 32;
49
50         req.dst.rta.rta_type = RTA_DST;
51         req.dst.rta.rta_len = sizeof(req.dst);
52         memcpy(req.dst.ipaddr, host->ipaddr, sizeof(req.dst.ipaddr));
53
54         req.dev.rta.rta_type = RTA_OIF;
55         req.dev.rta.rta_len = sizeof(req.dev);
56         req.dev.ifindex = host->rif->sll.sll_ifindex;
57
58         req.nl.nlmsg_flags = NLM_F_REQUEST;
59         req.rt.rtm_table = RT_TABLE_MAIN;
60         if (add) {
61                 req.nl.nlmsg_type = RTM_NEWROUTE;
62                 req.nl.nlmsg_flags |= NLM_F_CREATE | NLM_F_REPLACE;
63
64                 req.rt.rtm_protocol = RTPROT_BOOT;
65                 req.rt.rtm_scope = RT_SCOPE_LINK;
66                 req.rt.rtm_type = RTN_UNICAST;
67         } else {
68                 req.nl.nlmsg_type = RTM_DELROUTE;
69                 req.rt.rtm_scope = RT_SCOPE_NOWHERE;
70         }
71
72         send(rtnl_sock.fd, &req, sizeof(req), 0);
73 }
74
75 void relayd_add_route(struct relayd_host *host)
76 {
77         rtnl_route_set(host, true);
78 }
79
80 void relayd_del_route(struct relayd_host *host)
81 {
82         rtnl_route_set(host, false);
83 }
84
85 #ifndef NDA_RTA
86 #define NDA_RTA(r) \
87     ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
88 #endif
89
90 static void rtnl_parse_newneigh(struct nlmsghdr *h)
91 {
92         struct relayd_interface *rif = NULL;
93         struct ndmsg *r = NLMSG_DATA(h);
94         const uint8_t *lladdr = NULL;
95         const uint8_t *ipaddr = NULL;
96         struct rtattr *rta;
97         int len;
98
99         if (r->ndm_family != AF_INET)
100                 return;
101
102         list_for_each_entry(rif, &interfaces, list) {
103                 if (rif->sll.sll_ifindex == r->ndm_ifindex)
104                         goto found_interface;
105         }
106         return;
107
108 found_interface:
109         len = h->nlmsg_len - NLMSG_LENGTH(sizeof(*r));
110         for (rta = NDA_RTA(r); RTA_OK(rta, len); rta = RTA_NEXT(rta, len)) {
111                 switch(rta->rta_type) {
112                 case NDA_LLADDR:
113                         lladdr = RTA_DATA(rta);
114                         break;
115                 case NDA_DST:
116                         ipaddr = RTA_DATA(rta);
117                         break;
118                 default:
119                         break;
120                 }
121         }
122
123         if (!lladdr || !ipaddr || (r->ndm_state & (NUD_INCOMPLETE|NUD_FAILED)))
124                 return;
125
126         if (!memcmp(lladdr, "\x00\x00\x00\x00\x00\x00", ETH_ALEN))
127                 return;
128
129         DPRINTF(1, "%s: Found ARP cache entry for host "IP_FMT" ("MAC_FMT")\n",
130                 rif->ifname, IP_BUF(ipaddr), MAC_BUF(lladdr));
131         relayd_refresh_host(rif, lladdr, ipaddr);
132 }
133
134 static void rtnl_parse_packet(void *data, int len)
135 {
136         struct nlmsghdr *h;
137
138         for (h = data; NLMSG_OK(h, len); h = NLMSG_NEXT(h, len)) {
139                 if (h->nlmsg_type == NLMSG_DONE ||
140                     h->nlmsg_type == NLMSG_ERROR)
141                         return;
142
143                 if (h->nlmsg_seq != rtnl_dump_seq)
144                         continue;
145
146                 if (h->nlmsg_type == RTM_NEWNEIGH)
147                         rtnl_parse_newneigh(h);
148         }
149 }
150
151 static void rtnl_cb(struct uloop_fd *fd, unsigned int events)
152 {
153         struct sockaddr_nl nladdr;
154         static uint8_t buf[16384];
155         struct iovec iov = {
156                 .iov_base = buf,
157                 .iov_len = sizeof(buf),
158         };
159         struct msghdr msg = {
160                 .msg_name = &nladdr,
161                 .msg_namelen = sizeof(nladdr),
162                 .msg_iov = &iov,
163                 .msg_iovlen = 1,
164         };
165
166         do {
167                 int len;
168
169                 len = recvmsg(rtnl_sock.fd, &msg, 0);
170                 if (len < 0) {
171                         if (errno == EINTR)
172                                 continue;
173
174                         return;
175                 }
176
177                 if (!len)
178                         break;
179
180                 if (nladdr.nl_pid != 0)
181                         continue;
182
183                 rtnl_parse_packet(buf, len);
184         } while (1);
185 }
186
187 int relayd_rtnl_init(void)
188 {
189         struct sockaddr_nl snl_local;
190         static struct {
191                 struct nlmsghdr nlh;
192                 struct rtgenmsg g;
193         } req = {
194                 .nlh = {
195                         .nlmsg_len = sizeof(req),
196                         .nlmsg_type = RTM_GETNEIGH,
197                         .nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST,
198                         .nlmsg_pid = 0,
199                 },
200                 .g.rtgen_family = AF_INET,
201         };
202
203         rtnl_sock.fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
204         if (rtnl_sock.fd < 0) {
205                 perror("socket(AF_NETLINK)");
206                 return -1;
207         }
208
209         snl_local.nl_family = AF_NETLINK;
210
211         if (bind(rtnl_sock.fd, (struct sockaddr *) &snl_local, sizeof(struct sockaddr_nl)) < 0) {
212                 perror("bind");
213                 close(rtnl_sock.fd);
214                 return -1;
215         }
216
217         rtnl_sock.cb = rtnl_cb;
218         uloop_fd_add(&rtnl_sock, ULOOP_READ | ULOOP_EDGE_TRIGGER);
219
220         rtnl_seq = time(NULL);
221         rtnl_dump_seq = rtnl_seq;
222         req.nlh.nlmsg_seq = rtnl_seq;
223         send(rtnl_sock.fd, &req, sizeof(req), 0);
224
225         return 0;
226 }
227
228 void relayd_rtnl_done(void)
229 {
230         uloop_fd_delete(&rtnl_sock);
231         close(rtnl_sock.fd);
232 }