]> git.meshlink.io Git - catta/blob - iface.c
add packet scheduler
[catta] / iface.c
1 #include <string.h>
2 #include <sys/socket.h>
3 #include <asm/types.h>
4 #include <linux/netlink.h>
5 #include <linux/rtnetlink.h>
6 #include <errno.h>
7 #include <net/if.h>
8
9 #include "iface.h"
10 #include "netlink.h"
11 #include "dns.h"
12 #include "socket.h"
13
14 static void update_address_rr(flxInterfaceMonitor *m, flxInterfaceAddress *a, int remove) {
15     g_assert(m);
16     g_assert(a);
17
18     if (!flx_address_is_relevant(a) || remove) {
19         if (a->rr_id >= 0) {
20             flx_server_remove(m->server, a->rr_id);
21             a->rr_id = -1;
22         }
23     } else {
24         if (a->rr_id < 0) {
25             a->rr_id = flx_server_get_next_id(m->server);
26             flx_server_add_address(m->server, a->rr_id, a->interface->index, AF_UNSPEC, FALSE, m->server->hostname, &a->address);
27         }
28     }
29 }
30
31 static void update_interface_rr(flxInterfaceMonitor *m, flxInterface *i, int remove) {
32     flxInterfaceAddress *a;
33     g_assert(m);
34     g_assert(i);
35
36     for (a = i->addresses; a; a = a->address_next)
37         update_address_rr(m, a, remove);
38 }
39
40 static void free_address(flxInterfaceMonitor *m, flxInterfaceAddress *a) {
41     g_assert(m);
42     g_assert(a);
43     g_assert(a->interface);
44
45     if (a->address.family == AF_INET)
46         a->interface->n_ipv4_addrs --;
47     else if (a->address.family == AF_INET6)
48         a->interface->n_ipv6_addrs --;
49
50     FLX_LLIST_REMOVE(flxInterfaceAddress, address, a->interface->addresses, a);
51
52     flx_server_remove(m->server, a->rr_id);
53     
54     g_free(a);
55 }
56
57 static void free_interface(flxInterfaceMonitor *m, flxInterface *i) {
58     g_assert(m);
59     g_assert(i);
60
61     if (i->ipv4_scheduler)
62         flx_packet_scheduler_free(i->ipv4_scheduler);
63     if (i->ipv6_scheduler)
64         flx_packet_scheduler_free(i->ipv6_scheduler);
65     
66     while (i->addresses)
67         free_address(m, i->addresses);
68
69     if (i->ipv4_cache)
70         flx_cache_free(i->ipv4_cache);
71     if (i->ipv6_cache)
72         flx_cache_free(i->ipv6_cache);
73     
74     g_assert(i->n_ipv6_addrs == 0);
75     g_assert(i->n_ipv4_addrs == 0);
76
77     FLX_LLIST_REMOVE(flxInterface, interface, m->interfaces, i);
78     g_hash_table_remove(m->hash_table, &i->index);
79
80     flx_cache_free(i->ipv4_cache);
81     flx_cache_free(i->ipv6_cache);
82     
83     g_free(i->name);
84     g_free(i);
85 }
86
87 static flxInterfaceAddress* get_address(flxInterfaceMonitor *m, flxInterface *i, const flxAddress *raddr) {
88     flxInterfaceAddress *ia;
89     
90     g_assert(m);
91     g_assert(i);
92     g_assert(raddr);
93
94     for (ia = i->addresses; ia; ia = ia->address_next)
95         if (flx_address_cmp(&ia->address, raddr) == 0)
96             return ia;
97
98     return NULL;
99 }
100
101 static int netlink_list_items(flxNetlink *nl, guint16 type, guint *ret_seq) {
102     struct nlmsghdr *n;
103     struct rtgenmsg *gen;
104     guint8 req[1024];
105     
106     memset(&req, 0, sizeof(req));
107     n = (struct nlmsghdr*) req;
108     n->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg));
109     n->nlmsg_type = type;
110     n->nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
111     n->nlmsg_pid = 0;
112
113     gen = NLMSG_DATA(n);
114     memset(gen, 0, sizeof(struct rtgenmsg));
115     gen->rtgen_family = AF_UNSPEC;
116
117     return flx_netlink_send(nl, n, ret_seq);
118 }
119
120 static void callback(flxNetlink *nl, struct nlmsghdr *n, gpointer userdata) {
121     flxInterfaceMonitor *m = userdata;
122     
123     g_assert(m);
124     g_assert(n);
125     g_assert(m->netlink == nl);
126
127     if (n->nlmsg_type == RTM_NEWLINK) {
128         struct ifinfomsg *ifinfomsg = NLMSG_DATA(n);
129         flxInterface *i;
130         struct rtattr *a = NULL;
131         size_t l;
132         int changed;
133
134         if (ifinfomsg->ifi_family != AF_UNSPEC)
135             return;
136
137         if ((i = (flxInterface*) flx_interface_monitor_get_interface(m, ifinfomsg->ifi_index)))
138             changed = 1;
139         else {
140             i = g_new(flxInterface, 1);
141             i->monitor = m;
142             i->name = NULL;
143             i->index = ifinfomsg->ifi_index;
144             i->addresses = NULL;
145             i->n_ipv4_addrs = i->n_ipv6_addrs = 0;
146             FLX_LLIST_PREPEND(flxInterface, interface, m->interfaces, i);
147             g_hash_table_insert(m->hash_table, &i->index, i);
148             i->mtu = 1500;
149             i->ipv4_cache = flx_cache_new(m->server, i);
150             i->ipv6_cache = flx_cache_new(m->server, i);
151             i->ipv4_scheduler = flx_packet_scheduler_new(m->server, i, AF_INET);
152             i->ipv6_scheduler = flx_packet_scheduler_new(m->server, i, AF_INET6);
153             
154             changed = 0;
155         }
156         
157         i->flags = ifinfomsg->ifi_flags;
158
159         l = NLMSG_PAYLOAD(n, sizeof(struct ifinfomsg));
160         a = IFLA_RTA(ifinfomsg);
161
162         while (RTA_OK(a, l)) {
163             switch(a->rta_type) {
164                 case IFLA_IFNAME:
165                     g_free(i->name);
166                     i->name = g_strndup(RTA_DATA(a), RTA_PAYLOAD(a));
167                     break;
168
169                 case IFLA_MTU:
170                     g_assert(RTA_PAYLOAD(a) == sizeof(unsigned int));
171                     i->mtu = *((unsigned int*) RTA_DATA(a));
172                     break;
173                     
174                 default:
175                     ;
176             }
177
178             a = RTA_NEXT(a, l);
179         }
180
181         update_interface_rr(m, i, 0);
182     } else if (n->nlmsg_type == RTM_DELLINK) {
183         struct ifinfomsg *ifinfomsg = NLMSG_DATA(n);
184         flxInterface *i;
185
186         if (ifinfomsg->ifi_family != AF_UNSPEC)
187             return;
188         
189         if (!(i = (flxInterface*) flx_interface_monitor_get_interface(m, ifinfomsg->ifi_index)))
190             return;
191
192         update_interface_rr(m, i, 1);
193         free_interface(m, i);
194         
195     } else if (n->nlmsg_type == RTM_NEWADDR || n->nlmsg_type == RTM_DELADDR) {
196
197         struct ifaddrmsg *ifaddrmsg = NLMSG_DATA(n);
198         flxInterface *i;
199         struct rtattr *a = NULL;
200         size_t l;
201         int changed;
202         flxAddress raddr;
203         int raddr_valid = 0;
204
205         if (ifaddrmsg->ifa_family != AF_INET && ifaddrmsg->ifa_family != AF_INET6)
206             return;
207
208         if (!(i = (flxInterface*) flx_interface_monitor_get_interface(m, ifaddrmsg->ifa_index)))
209             return;
210
211         raddr.family = ifaddrmsg->ifa_family;
212
213         l = NLMSG_PAYLOAD(n, sizeof(struct ifinfomsg));
214         a = IFA_RTA(ifaddrmsg);
215
216         while (RTA_OK(a, l)) {
217             switch(a->rta_type) {
218                 case IFA_ADDRESS:
219                     if ((raddr.family == AF_INET6 && RTA_PAYLOAD(a) != 16) ||
220                         (raddr.family == AF_INET && RTA_PAYLOAD(a) != 4))
221                         return;
222
223                     memcpy(raddr.data, RTA_DATA(a), RTA_PAYLOAD(a));
224                     raddr_valid = 1;
225
226                     break;
227                     
228                 default:
229                     ;
230             }
231
232             a = RTA_NEXT(a, l);
233         }
234
235
236         if (!raddr_valid)
237             return;
238
239         if (n->nlmsg_type == RTM_NEWADDR) {
240             flxInterfaceAddress *addr;
241             
242             if ((addr = get_address(m, i, &raddr)))
243                 changed = 1;
244             else {
245                 addr = g_new(flxInterfaceAddress, 1);
246                 addr->address = raddr;
247                 addr->interface = i;
248
249                 if (raddr.family == AF_INET)
250                     i->n_ipv4_addrs++;
251                 else if (raddr.family == AF_INET6)
252                     i->n_ipv6_addrs++;
253
254                 addr->rr_id = -1;
255
256                 FLX_LLIST_PREPEND(flxInterfaceAddress, address, i->addresses, addr);
257                 
258                 changed = 0;
259             }
260             
261             addr->flags = ifaddrmsg->ifa_flags;
262             addr->scope = ifaddrmsg->ifa_scope;
263
264             update_address_rr(m, addr, 0);
265         } else {
266             flxInterfaceAddress *addr;
267             
268             if (!(addr = get_address(m, i, &raddr)))
269                 return;
270
271             update_address_rr(m, addr, 1);
272             free_address(m, addr);
273         }
274                 
275     } else if (n->nlmsg_type == NLMSG_DONE) {
276
277         if (m->list == LIST_IFACE) {
278             m->list = LIST_DONE;
279             
280             if (netlink_list_items(m->netlink, RTM_GETADDR, &m->query_addr_seq) < 0) {
281                 g_warning("NETLINK: Failed to list addrs: %s", strerror(errno));
282             } else
283                 m->list = LIST_ADDR;
284         } else
285             m->list = LIST_DONE;
286         
287     } else if (n->nlmsg_type == NLMSG_ERROR && (n->nlmsg_seq == m->query_link_seq || n->nlmsg_seq == m->query_addr_seq)) {
288         struct nlmsgerr *e = NLMSG_DATA (n);
289                     
290         if (e->error)
291             g_warning("NETLINK: Failed to browse: %s", strerror(-e->error));
292     }
293 }
294
295 flxInterfaceMonitor *flx_interface_monitor_new(flxServer *s) {
296     flxInterfaceMonitor *m = NULL;
297
298     m = g_new0(flxInterfaceMonitor, 1);
299     m->server = s;
300     if (!(m->netlink = flx_netlink_new(s->context, RTMGRP_LINK|RTMGRP_IPV4_IFADDR|RTMGRP_IPV6_IFADDR, callback, m)))
301         goto fail;
302
303     m->hash_table = g_hash_table_new(g_int_hash, g_int_equal);
304     m->interfaces = NULL;
305
306     if (netlink_list_items(m->netlink, RTM_GETLINK, &m->query_link_seq) < 0)
307         goto fail;
308
309     m->list = LIST_IFACE;
310     
311     return m;
312
313 fail:
314     flx_interface_monitor_free(m);
315     return NULL;
316 }
317
318 void flx_interface_monitor_free(flxInterfaceMonitor *m) {
319     g_assert(m);
320
321     if (m->netlink)
322         flx_netlink_free(m->netlink);
323
324     if (m->hash_table)
325         g_hash_table_destroy(m->hash_table);
326
327     g_free(m);
328 }
329
330
331 flxInterface* flx_interface_monitor_get_interface(flxInterfaceMonitor *m, gint index) {
332     g_assert(m);
333     g_assert(index > 0);
334
335     return g_hash_table_lookup(m->hash_table, &index);
336 }
337
338 flxInterface* flx_interface_monitor_get_first(flxInterfaceMonitor *m) {
339     g_assert(m);
340     return m->interfaces;
341 }
342
343 int flx_interface_is_relevant(flxInterface *i) {
344     g_assert(i);
345
346     return
347         (i->flags & IFF_UP) &&
348         (i->flags & IFF_RUNNING) &&
349         !(i->flags & IFF_LOOPBACK);
350 }
351
352 int flx_address_is_relevant(flxInterfaceAddress *a) {
353     g_assert(a);
354
355     return
356         a->scope == RT_SCOPE_UNIVERSE &&
357         flx_interface_is_relevant(a->interface);
358 }
359
360 void flx_interface_send_packet(flxInterface *i, guchar protocol, flxDnsPacket *p) {
361     g_assert(i);
362     g_assert(p);
363
364     if (!flx_interface_is_relevant(i))
365         return;
366     
367     if ((protocol == AF_INET || protocol == AF_UNSPEC) && i->n_ipv4_addrs > 0) {
368         g_message("sending on '%s':IPv4", i->name);
369         flx_send_dns_packet_ipv4(i->monitor->server->fd_ipv4, i->index, p);
370     }
371
372     if ((protocol == AF_INET6 || protocol == AF_UNSPEC) && i->n_ipv6_addrs > 0) {
373         g_message("sending on '%s':IPv6", i->name);
374         flx_send_dns_packet_ipv6(i->monitor->server->fd_ipv6, i->index, p);
375     }
376 }
377
378 void flx_interface_post_query(flxInterface *i, guchar protocol, flxKey *k) {
379     g_assert(i);
380     g_assert(k);
381
382     if (!flx_interface_is_relevant(i))
383         return;
384
385     if ((protocol == AF_INET || protocol == AF_UNSPEC) && i->n_ipv4_addrs > 0)
386         flx_packet_scheduler_post_query(i->ipv4_scheduler, k);
387
388     if ((protocol == AF_INET6 || protocol == AF_UNSPEC) && i->n_ipv6_addrs > 0)
389         flx_packet_scheduler_post_query(i->ipv6_scheduler, k);
390 }
391
392 void flx_interface_post_response(flxInterface *i, guchar protocol, flxRecord *rr) {
393     g_assert(i);
394     g_assert(rr);
395
396     if (!flx_interface_is_relevant(i))
397         return;
398
399     if ((protocol == AF_INET || protocol == AF_UNSPEC) && i->n_ipv4_addrs > 0)
400         flx_packet_scheduler_post_response(i->ipv4_scheduler, rr);
401
402     if ((protocol == AF_INET6 || protocol == AF_UNSPEC) && i->n_ipv6_addrs > 0)
403         flx_packet_scheduler_post_response(i->ipv6_scheduler, rr);
404 }
405
406 void flx_dump_caches(flxServer *s, FILE *f) {
407     flxInterface *i;
408     g_assert(s);
409
410     for (i = flx_interface_monitor_get_first(s->monitor); i; i = i->interface_next) {
411         if (!flx_interface_is_relevant(i))
412             continue;
413         
414         if (i->n_ipv4_addrs > 0) {
415             fprintf(f, ";;; INTERFACE %s; IPv4 ;;;\n", i->name);
416             flx_cache_dump(i->ipv4_cache, f);
417         }
418
419         if (i->n_ipv6_addrs > 0) {
420             fprintf(f, ";;; INTERFACE %s; IPv6 ;;;\n", i->name);
421             flx_cache_dump(i->ipv6_cache, f);
422         }
423     }
424 }