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 struct buffered_update {
24     unsigned char id[8];
25     unsigned char prefix[16];
26     unsigned char src_prefix[16];
27     unsigned char plen;
28     unsigned char src_plen;
29     unsigned char pad[2];
30 };
31 
32 #define IF_TYPE_DEFAULT 0
33 #define IF_TYPE_WIRED 1
34 #define IF_TYPE_WIRELESS 2
35 #define IF_TYPE_TUNNEL 3
36 
37 /* If you modify this structure, also modify the merge_ifconf function. */
38 
39 struct interface_conf {
40     char *ifname;
41     unsigned hello_interval;
42     unsigned update_interval;
43     unsigned short cost;
44     char type;
45     char split_horizon;
46     char lq;
47     char faraway;
48     char unicast;
49     char enable_timestamps;
50     char rfc6126;
51     int channel;
52     unsigned int rtt_decay;
53     unsigned int rtt_min;
54     unsigned int rtt_max;
55     unsigned int max_rtt_penalty;
56     struct interface_conf *next;
57 };
58 
59 #define CONFIG_DEFAULT 0
60 #define CONFIG_NO 1
61 #define CONFIG_YES 2
62 
63 /* Interface is up. */
64 #define IF_UP (1 << 0)
65 /* Interface known to be wireless, unknown otherwise. */
66 #define IF_WIRELESS (1<<1)
67 /* Apply split horizon. */
68 #define IF_SPLIT_HORIZON (1 << 2)
69 /* Perform link-quality estimation. */
70 #define IF_LQ (1 << 3)
71 /* Nodes on the far end don't interfere with nodes on the near end. */
72 #define IF_FARAWAY (1 << 4)
73 /* Send most TLVs over unicast. */
74 #define IF_UNICAST (1 << 5)
75 /* Send timestamps in Hello and IHU. */
76 #define IF_TIMESTAMPS (1 << 6)
77 /* Remain compatible with RFC 6126. */
78 #define IF_RFC6126 (1 << 7)
79 /* Use Babel over DTLS on this interface. */
80 #define IF_DTLS (1 << 9)
81 
82 /* Only INTERFERING can appear on the wire. */
83 #define IF_CHANNEL_UNKNOWN 0
84 #define IF_CHANNEL_INTERFERING 255
85 #define IF_CHANNEL_NONINTERFERING -2
86 
87 struct buffered {
88     struct sockaddr_in6 sin6;
89     unsigned char *buf;
90     int len;
91     int size;
92     int flush_interval;
93     struct timeval timeout;
94     char have_id;
95     char have_nh;
96     char have_prefix;
97     unsigned char id[8];
98     unsigned char nh[4];
99     unsigned char prefix[16];
100     /* Relative position of the Hello message in the send buffer, or
101        (-1) if there is none. */
102     int hello;
103 };
104 
105 struct interface {
106     struct interface *next;
107     struct interface_conf *conf;
108     unsigned int ifindex;
109     unsigned short flags;
110     unsigned short cost;
111     int channel;
112     struct timeval hello_timeout;
113     struct timeval update_timeout;
114     struct timeval update_flush_timeout;
115     char name[IF_NAMESIZE];
116     unsigned char *ipv4;
117     int numll;
118     unsigned char (*ll)[16];
119     struct buffered buf;
120     struct buffered_update *buffered_updates;
121     int num_buffered_updates;
122     int update_bufsize;
123     time_t last_update_time;
124     unsigned short hello_seqno;
125     unsigned hello_interval;
126     unsigned update_interval;
127     /* A higher value means we forget old RTT samples faster. Must be
128        between 1 and 256, inclusive. */
129     unsigned int rtt_decay;
130     /* Parameters for computing the cost associated to RTT. */
131     unsigned int rtt_min;
132     unsigned int rtt_max;
133     unsigned int max_rtt_penalty;
134 };
135 
136 #define IF_CONF(_ifp, _field) \
137     ((_ifp)->conf ? (_ifp)->conf->_field : 0)
138 
139 extern struct interface *interfaces;
140 
141 #define FOR_ALL_INTERFACES(_ifp) for(_ifp = interfaces; _ifp; _ifp = _ifp->next)
142 
143 static inline int
if_up(struct interface * ifp)144 if_up(struct interface *ifp)
145 {
146     return !!(ifp->flags & IF_UP);
147 }
148 
149 struct interface *add_interface(char *ifname, struct interface_conf *if_conf);
150 int flush_interface(char *ifname);
151 unsigned jitter(struct buffered *buf, int urgent);
152 unsigned update_jitter(struct interface *ifp, int urgent);
153 void set_timeout(struct timeval *timeout, int msecs);
154 int interface_updown(struct interface *ifp, int up);
155 int interface_ll_address(struct interface *ifp, const unsigned char *address);
156 void check_interfaces(void);
157