1 /*
2 Copyright (c) 2007, 2008 by Juliusz Chroboczek
3 
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10 
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 THE SOFTWARE.
21 */
22 
23 #include <string.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <errno.h>
27 #include <assert.h>
28 #include <sys/time.h>
29 #include <sys/ioctl.h>
30 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <net/if.h>
33 #include <arpa/inet.h>
34 
35 #include "babeld.h"
36 #include "util.h"
37 #include "kernel.h"
38 #include "interface.h"
39 #include "neighbour.h"
40 #include "message.h"
41 #include "route.h"
42 #include "configuration.h"
43 #include "local.h"
44 #include "xroute.h"
45 
46 #define MIN_MTU 512
47 
48 struct interface *interfaces = NULL;
49 
50 static struct interface *
last_interface(void)51 last_interface(void)
52 {
53     struct interface *ifp = interfaces;
54 
55     if(!ifp)
56         return NULL;
57 
58     while(ifp->next)
59         ifp = ifp->next;
60 
61     return ifp;
62 }
63 
64 struct interface *
add_interface(char * ifname,struct interface_conf * if_conf)65 add_interface(char *ifname, struct interface_conf *if_conf)
66 {
67     struct interface *ifp;
68 
69     FOR_ALL_INTERFACES(ifp) {
70         if(strcmp(ifp->name, ifname) == 0) {
71             if(if_conf)
72                 fprintf(stderr,
73                         "Warning: attempting to add existing interface (%s), "
74                         "new configuration ignored.\n", ifname);
75             return ifp;
76         }
77     }
78 
79     ifp = calloc(1, sizeof(struct interface));
80     if(ifp == NULL)
81         return NULL;
82 
83     strncpy(ifp->name, ifname, IF_NAMESIZE);
84     ifp->conf = if_conf ? if_conf : default_interface_conf;
85     ifp->hello_seqno = (random() & 0xFFFF);
86 
87     if(interfaces == NULL)
88         interfaces = ifp;
89     else
90         last_interface()->next = ifp;
91 
92     local_notify_interface(ifp, LOCAL_ADD);
93 
94     return ifp;
95 }
96 
97 int
flush_interface(char * ifname)98 flush_interface(char *ifname)
99 {
100     struct interface *ifp, *prev;
101 
102     prev = NULL;
103     ifp = interfaces;
104     while(ifp) {
105         if(strcmp(ifp->name, ifname) == 0)
106             break;
107         prev = ifp;
108         ifp = ifp->next;
109     }
110 
111     if(ifp == NULL)
112         return 0;
113 
114     interface_updown(ifp, 0);
115     if(prev)
116         prev->next = ifp->next;
117     else
118         interfaces = ifp->next;
119 
120     if(ifp->conf != NULL && ifp->conf != default_interface_conf)
121         flush_ifconf(ifp->conf);
122 
123     local_notify_interface(ifp, LOCAL_FLUSH);
124 
125     free(ifp->ipv4);
126     free(ifp);
127 
128     return 1;
129 }
130 
131 /* This should be no more than half the hello interval, so that hellos
132    aren't sent late.  The result is in milliseconds. */
133 unsigned
jitter(struct buffered * buf,int urgent)134 jitter(struct buffered *buf, int urgent)
135 {
136     unsigned interval = buf->flush_interval;
137     if(urgent)
138         interval = MIN(interval, 20);
139     else
140         interval = MIN(interval, 2000);
141     return roughly(interval / 2);
142 }
143 
144 unsigned
update_jitter(struct interface * ifp,int urgent)145 update_jitter(struct interface *ifp, int urgent)
146 {
147     unsigned interval = ifp->hello_interval;
148     if(urgent)
149         interval = MIN(interval, 100);
150     else
151         interval = MIN(interval, 4000);
152     return roughly(interval);
153 }
154 
155 void
set_timeout(struct timeval * timeout,int msecs)156 set_timeout(struct timeval *timeout, int msecs)
157 {
158     timeval_add_msec(timeout, &now, roughly(msecs));
159 }
160 
161 static int
check_interface_ipv4(struct interface * ifp)162 check_interface_ipv4(struct interface *ifp)
163 {
164     unsigned char ipv4[4];
165     int rc;
166 
167     if(ifp->ifindex > 0)
168         rc = kernel_interface_ipv4(ifp->name, ifp->ifindex, ipv4);
169     else
170         rc = 0;
171 
172     if(rc > 0) {
173         if(!ifp->ipv4 || memcmp(ipv4, ifp->ipv4, 4) != 0) {
174             debugf("Noticed IPv4 change for %s.\n", ifp->name);
175             flush_interface_routes(ifp, 0);
176             if(!ifp->ipv4)
177                 ifp->ipv4 = malloc(4);
178             if(ifp->ipv4)
179                 memcpy(ifp->ipv4, ipv4, 4);
180             local_notify_interface(ifp, LOCAL_CHANGE);
181             return 1;
182         }
183     } else {
184         if(ifp->ipv4) {
185             debugf("Noticed IPv4 change for %s.\n", ifp->name);
186             flush_interface_routes(ifp, 0);
187             free(ifp->ipv4);
188             ifp->ipv4 = NULL;
189             local_notify_interface(ifp, LOCAL_CHANGE);
190             return 1;
191         }
192     }
193     return 0;
194 }
195 
196 static int
check_interface_channel(struct interface * ifp)197 check_interface_channel(struct interface *ifp)
198 {
199     int channel = IF_CONF(ifp, channel);
200     int rc = 1;
201 
202     if(channel == IF_CHANNEL_UNKNOWN) {
203         /* IF_WIRELESS merely means that we know for sure that the
204            interface is wireless, so check unconditionally. */
205         channel = kernel_interface_channel(ifp->name, ifp->ifindex);
206         if(channel < 0) {
207             if((ifp->flags & IF_WIRELESS))
208                 rc = -1;
209             channel = (ifp->flags & IF_WIRELESS) ?
210                 IF_CHANNEL_INTERFERING : IF_CHANNEL_NONINTERFERING;
211         }
212     }
213 
214     if(ifp->channel != channel) {
215         ifp->channel = channel;
216         return rc;
217     }
218     return 0;
219 }
220 
221 static int
check_link_local_addresses(struct interface * ifp)222 check_link_local_addresses(struct interface *ifp)
223 {
224     struct kernel_route ll[32];
225     int rc, i;
226 
227     rc = kernel_addresses(ifp->ifindex, 1, ll, 32);
228     if(rc <= 0) {
229         if(rc < 0)
230             perror("kernel_addresses(link local)");
231         else
232             fprintf(stderr, "Interface %s has no link-local address.\n",
233                     ifp->name);
234         if(ifp->ll) {
235             free(ifp->ll);
236             ifp->numll = 0;
237             ifp->ll = NULL;
238         }
239         local_notify_interface(ifp, LOCAL_CHANGE);
240         /* Most probably DAD hasn't finished yet.  Reschedule us
241            real soon. */
242         schedule_interfaces_check(2000, 0);
243         return -1;
244     } else {
245         int changed;
246         if(rc == ifp->numll) {
247             changed = 0;
248             for(i = 0; i < rc; i++) {
249                 if(memcmp(ifp->ll[i], ll[i].prefix, 16) != 0) {
250                     changed = 1;
251                     break;
252                 }
253             }
254         } else {
255             changed = 1;
256         }
257 
258         if(changed) {
259             free(ifp->ll);
260             ifp->numll = 0;
261             ifp->ll = malloc(16 * rc);
262             if(ifp->ll == NULL) {
263                 perror("malloc(ll)");
264             } else {
265                 for(i = 0; i < rc; i++)
266                     memcpy(ifp->ll[i], ll[i].prefix, 16);
267                 ifp->numll = rc;
268             }
269             local_notify_interface(ifp, LOCAL_CHANGE);
270         }
271     }
272 
273     return 0;
274 }
275 
276 int
interface_updown(struct interface * ifp,int up)277 interface_updown(struct interface *ifp, int up)
278 {
279     int mtu, rc, type;
280     struct ipv6_mreq mreq;
281 
282     if((!!up) == if_up(ifp))
283         return 0;
284 
285     if(up) {
286         ifp->flags |= IF_UP;
287         if(ifp->ifindex <= 0) {
288             fprintf(stderr,
289                     "Upping unknown interface %s.\n", ifp->name);
290             goto fail;
291         }
292 
293         rc = kernel_setup_interface(1, ifp->name, ifp->ifindex);
294         if(rc < 0) {
295             fprintf(stderr, "kernel_setup_interface(%s, %u) failed.\n",
296                     ifp->name, ifp->ifindex);
297             goto fail;
298         }
299 
300         memset(&ifp->buf.sin6, 0, sizeof(ifp->buf.sin6));
301         ifp->buf.sin6.sin6_family = AF_INET6;
302         memcpy(&ifp->buf.sin6.sin6_addr, protocol_group, 16);
303         ifp->buf.sin6.sin6_port = htons(protocol_port);
304         ifp->buf.sin6.sin6_scope_id = ifp->ifindex;
305 
306         mtu = kernel_interface_mtu(ifp->name, ifp->ifindex);
307         if(mtu < 0) {
308             fprintf(stderr,
309                     "Warning: couldn't get MTU of interface %s (%u), "
310                     "using 1280\n",
311                     ifp->name, ifp->ifindex);
312             mtu = 1280;
313         }
314 
315         /* We need to be able to fit at least a router-ID and an update,
316            up to 116 bytes, and that's not counting sub-TLVs or crypto keys.
317            In IPv6, the minimum MTU is 1280, and every host must be able
318            to reassemble up to 1500 bytes.  In IPv4, every host must be
319            able to reassemble up to 576 bytes.  At any rate, the Babel spec
320            says that every node must be able to parse packets of size 512. */
321         if(mtu < MIN_MTU) {
322             fprintf(stderr,
323                     "Suspiciously low MTU %d on interface %s (%u), using %d.\n",
324                     mtu, ifp->name, ifp->ifindex, MIN_MTU);
325             mtu = 512;
326         }
327 
328         if(ifp->buf.buf)
329             free(ifp->buf.buf);
330 
331         /* 40 for IPv6 header, 8 for UDP header, 12 for good luck. */
332         ifp->buf.size = mtu - sizeof(packet_header) - 60;
333         ifp->buf.buf = malloc(ifp->buf.size);
334         if(ifp->buf.buf == NULL) {
335             fprintf(stderr, "Couldn't allocate sendbuf.\n");
336             ifp->buf.size = 0;
337             goto fail;
338         }
339         ifp->buf.hello = -1;
340 
341         rc = resize_receive_buffer(mtu);
342         if(rc < 0)
343             fprintf(stderr, "Warning: couldn't resize "
344                     "receive buffer for interface %s (%u) (%d bytes).\n",
345                     ifp->name, ifp->ifindex, mtu);
346 
347         type = IF_CONF(ifp, type);
348         if(type == IF_TYPE_DEFAULT) {
349             if(all_wireless) {
350                 type = IF_TYPE_WIRELESS;
351             } else {
352                 rc = kernel_interface_wireless(ifp->name, ifp->ifindex);
353                 if(rc < 0) {
354                     fprintf(stderr,
355                             "Warning: couldn't determine whether %s (%u) "
356                             "is a wireless interface.\n",
357                             ifp->name, ifp->ifindex);
358                 } else if(rc) {
359                     type = IF_TYPE_WIRELESS;
360                 }
361             }
362         }
363 
364         /* Type is CONFIG_TYPE_AUTO if interface is not known to be
365            wireless, so provide sane defaults for that case. */
366 
367         if(type == IF_TYPE_WIRELESS)
368             ifp->flags |= IF_WIRELESS;
369         else
370             ifp->flags &= ~IF_WIRELESS;
371 
372         ifp->cost = IF_CONF(ifp, cost);
373         if(ifp->cost <= 0)
374             ifp->cost = type == IF_TYPE_WIRELESS ? 256 : 96;
375 
376         if(IF_CONF(ifp, split_horizon) == CONFIG_YES)
377             ifp->flags |= IF_SPLIT_HORIZON;
378         else if(IF_CONF(ifp, split_horizon) == CONFIG_NO)
379             ifp->flags &= ~IF_SPLIT_HORIZON;
380         else if(type == IF_TYPE_WIRED)
381             ifp->flags |= IF_SPLIT_HORIZON;
382         else
383             ifp->flags &= ~IF_SPLIT_HORIZON;
384 
385         if(IF_CONF(ifp, lq) == CONFIG_YES)
386             ifp->flags |= IF_LQ;
387         else if(IF_CONF(ifp, lq) == CONFIG_NO)
388             ifp->flags &= ~IF_LQ;
389         else if(type == IF_TYPE_WIRELESS)
390             ifp->flags |= IF_LQ;
391         else
392             ifp->flags &= ~IF_LQ;
393 
394         if(IF_CONF(ifp, faraway) == CONFIG_YES)
395             ifp->flags |= IF_FARAWAY;
396 
397         if(IF_CONF(ifp, unicast) == CONFIG_YES)
398             ifp->flags |= IF_UNICAST;
399 
400         if(IF_CONF(ifp, hello_interval) > 0)
401             ifp->hello_interval = IF_CONF(ifp, hello_interval);
402         else if(type == IF_TYPE_WIRELESS)
403             ifp->hello_interval = default_wireless_hello_interval;
404         else
405             ifp->hello_interval = default_wired_hello_interval;
406 
407         ifp->update_interval =
408             IF_CONF(ifp, update_interval) > 0 ?
409             IF_CONF(ifp, update_interval) :
410             ifp->hello_interval * 4;
411 
412         /* This must be no more than half the Hello interval, or else
413            Hellos will arrive late. */
414         ifp->buf.flush_interval = ifp->hello_interval / 2;
415 
416         ifp->rtt_decay =
417             IF_CONF(ifp, rtt_decay) > 0 ?
418             IF_CONF(ifp, rtt_decay) : 42;
419 
420         ifp->rtt_min =
421             IF_CONF(ifp, rtt_min) > 0 ?
422             IF_CONF(ifp, rtt_min) : 10000;
423         ifp->rtt_max =
424             IF_CONF(ifp, rtt_max) > 0 ?
425             IF_CONF(ifp, rtt_max) : 120000;
426         if(ifp->rtt_max <= ifp->rtt_min) {
427             fprintf(stderr,
428                     "Uh, rtt-max is less than or equal to rtt-min (%u <= %u). "
429                     "Setting it to %u.\n", ifp->rtt_max, ifp->rtt_min,
430                     ifp->rtt_min + 10000);
431             ifp->rtt_max = ifp->rtt_min + 10000;
432         }
433         ifp->max_rtt_penalty = IF_CONF(ifp, max_rtt_penalty);
434         if(ifp->max_rtt_penalty == 0 && type == IF_TYPE_TUNNEL)
435             ifp->max_rtt_penalty = 96;
436 
437         if(IF_CONF(ifp, enable_timestamps) == CONFIG_YES)
438             ifp->flags |= IF_TIMESTAMPS;
439         else if(IF_CONF(ifp, enable_timestamps) == CONFIG_NO)
440             ifp->flags &= ~IF_TIMESTAMPS;
441         else if(type == IF_TYPE_TUNNEL)
442             ifp->flags |= IF_TIMESTAMPS;
443         else
444             ifp->flags &= ~IF_TIMESTAMPS;
445         if(ifp->max_rtt_penalty > 0 && !(ifp->flags & IF_TIMESTAMPS))
446             fprintf(stderr,
447                     "Warning: max_rtt_penalty is set "
448                     "but timestamps are disabled on interface %s.\n",
449                     ifp->name);
450 
451         if(IF_CONF(ifp, rfc6126) == CONFIG_YES)
452             ifp->flags |= IF_RFC6126;
453         else
454             ifp->flags &= ~IF_RFC6126;
455 
456         rc = check_link_local_addresses(ifp);
457         if(rc < 0) {
458             goto fail;
459         }
460         memset(&mreq, 0, sizeof(mreq));
461         memcpy(&mreq.ipv6mr_multiaddr, protocol_group, 16);
462         mreq.ipv6mr_interface = ifp->ifindex;
463         rc = setsockopt(protocol_socket, IPPROTO_IPV6, IPV6_JOIN_GROUP,
464                         (char*)&mreq, sizeof(mreq));
465         if(rc < 0) {
466             perror("setsockopt(IPV6_JOIN_GROUP)");
467             goto fail;
468         }
469 
470         rc = check_interface_channel(ifp);
471         if(rc < 0)
472             fprintf(stderr,
473                     "Warning: couldn't determine channel of interface %s.\n",
474                     ifp->name);
475         update_interface_metric(ifp);
476         rc = check_interface_ipv4(ifp);
477 
478         debugf("Upped interface %s (cost=%d, channel=%d%s).\n",
479                ifp->name,
480                ifp->cost,
481                ifp->channel,
482                ifp->ipv4 ? ", IPv4" : "");
483 
484         set_timeout(&ifp->hello_timeout, ifp->hello_interval);
485         set_timeout(&ifp->update_timeout, ifp->update_interval);
486         send_hello(ifp);
487         if(rc > 0)
488             send_update(ifp, 0, NULL, 0, NULL, 0);
489         send_multicast_request(ifp, NULL, 0, NULL, 0);
490     } else {
491         ifp->flags &= ~IF_UP;
492         flush_interface_routes(ifp, 0);
493         ifp->buf.len = 0;
494         ifp->buf.size = 0;
495         free(ifp->buf.buf);
496         ifp->num_buffered_updates = 0;
497         ifp->update_bufsize = 0;
498         if(ifp->buffered_updates)
499             free(ifp->buffered_updates);
500         ifp->buffered_updates = NULL;
501         ifp->buf.buf = NULL;
502         if(ifp->ifindex > 0) {
503             memset(&mreq, 0, sizeof(mreq));
504             memcpy(&mreq.ipv6mr_multiaddr, protocol_group, 16);
505             mreq.ipv6mr_interface = ifp->ifindex;
506             rc = setsockopt(protocol_socket, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
507                             (char*)&mreq, sizeof(mreq));
508             if(rc < 0)
509                 perror("setsockopt(IPV6_LEAVE_GROUP)");
510             kernel_setup_interface(0, ifp->name, ifp->ifindex);
511         }
512         if(ifp->ll)
513             free(ifp->ll);
514         ifp->ll = NULL;
515         ifp->numll = 0;
516     }
517 
518     local_notify_interface(ifp, LOCAL_CHANGE);
519 
520     return 1;
521 
522  fail:
523     assert(up);
524     interface_updown(ifp, 0);
525     local_notify_interface(ifp, LOCAL_CHANGE);
526     return -1;
527 }
528 
529 int
interface_ll_address(struct interface * ifp,const unsigned char * address)530 interface_ll_address(struct interface *ifp, const unsigned char *address)
531 {
532     int i;
533 
534     if(!if_up(ifp))
535         return 0;
536 
537     for(i = 0; i < ifp->numll; i++)
538         if(memcmp(ifp->ll[i], address, 16) == 0)
539             return 1;
540 
541     return 0;
542 }
543 
544 void
check_interfaces(void)545 check_interfaces(void)
546 {
547     struct interface *ifp;
548     int rc, ifindex_changed = 0;
549     unsigned int ifindex;
550 
551     FOR_ALL_INTERFACES(ifp) {
552         ifindex = if_nametoindex(ifp->name);
553         if(ifindex != ifp->ifindex) {
554             debugf("Noticed ifindex change for %s.\n", ifp->name);
555             interface_updown(ifp, 0);
556             ifp->ifindex = ifindex;
557             ifindex_changed = 1;
558         }
559 
560         if(ifp->ifindex > 0)
561             rc = kernel_interface_operational(ifp->name, ifp->ifindex);
562         else
563             rc = 0;
564         if((rc > 0) != if_up(ifp)) {
565             debugf("Noticed status change for %s.\n", ifp->name);
566             interface_updown(ifp, rc > 0);
567         }
568 
569         if(if_up(ifp)) {
570             /* Bother, said Pooh.  We should probably check for a change
571                in IPv4 addresses at this point. */
572             check_link_local_addresses(ifp);
573             check_interface_channel(ifp);
574             rc = check_interface_ipv4(ifp);
575             if(rc > 0) {
576                 send_multicast_request(ifp, NULL, 0, NULL, 0);
577                 send_update(ifp, 0, NULL, 0, NULL, 0);
578             }
579         }
580     }
581 
582     if(ifindex_changed)
583         renumber_filters();
584 }
585