Commit | Line | Data |
---|---|---|
d6f94d91 LL |
1 | /* Copyright (C) 2011-2016 B.A.T.M.A.N. contributors: |
2 | * | |
3 | * Linus Lüssing, Marek Lindner | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or | |
6 | * modify it under the terms of version 2 of the GNU General Public | |
7 | * License as published by the Free Software Foundation. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, but | |
10 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
12 | * General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program; if not, see <http://www.gnu.org/licenses/>. | |
16 | */ | |
17 | ||
18 | #include "bat_v_elp.h" | |
19 | #include "main.h" | |
20 | ||
21 | #include <linux/atomic.h> | |
22 | #include <linux/byteorder/generic.h> | |
23 | #include <linux/errno.h> | |
24 | #include <linux/etherdevice.h> | |
25 | #include <linux/fs.h> | |
26 | #include <linux/if_ether.h> | |
27 | #include <linux/jiffies.h> | |
28 | #include <linux/kernel.h> | |
29 | #include <linux/netdevice.h> | |
30 | #include <linux/random.h> | |
31 | #include <linux/rculist.h> | |
32 | #include <linux/rcupdate.h> | |
33 | #include <linux/skbuff.h> | |
34 | #include <linux/stddef.h> | |
35 | #include <linux/string.h> | |
36 | #include <linux/types.h> | |
37 | #include <linux/workqueue.h> | |
38 | ||
39 | #include "bat_algo.h" | |
9323158e | 40 | #include "bat_v_ogm.h" |
d6f94d91 | 41 | #include "hard-interface.h" |
162bd64c | 42 | #include "originator.h" |
d6f94d91 | 43 | #include "packet.h" |
162bd64c | 44 | #include "routing.h" |
d6f94d91 LL |
45 | #include "send.h" |
46 | ||
47 | /** | |
48 | * batadv_v_elp_start_timer - restart timer for ELP periodic work | |
49 | * @hard_iface: the interface for which the timer has to be reset | |
50 | */ | |
51 | static void batadv_v_elp_start_timer(struct batadv_hard_iface *hard_iface) | |
52 | { | |
53 | unsigned int msecs; | |
54 | ||
55 | msecs = atomic_read(&hard_iface->bat_v.elp_interval) - BATADV_JITTER; | |
56 | msecs += prandom_u32() % (2 * BATADV_JITTER); | |
57 | ||
58 | queue_delayed_work(batadv_event_workqueue, &hard_iface->bat_v.elp_wq, | |
59 | msecs_to_jiffies(msecs)); | |
60 | } | |
61 | ||
62 | /** | |
63 | * batadv_v_elp_periodic_work - ELP periodic task per interface | |
64 | * @work: work queue item | |
65 | * | |
66 | * Emits broadcast ELP message in regular intervals. | |
67 | */ | |
68 | static void batadv_v_elp_periodic_work(struct work_struct *work) | |
69 | { | |
70 | struct batadv_hard_iface *hard_iface; | |
71 | struct batadv_hard_iface_bat_v *bat_v; | |
72 | struct batadv_elp_packet *elp_packet; | |
73 | struct batadv_priv *bat_priv; | |
74 | struct sk_buff *skb; | |
75 | u32 elp_interval; | |
76 | ||
77 | bat_v = container_of(work, struct batadv_hard_iface_bat_v, elp_wq.work); | |
78 | hard_iface = container_of(bat_v, struct batadv_hard_iface, bat_v); | |
79 | bat_priv = netdev_priv(hard_iface->soft_iface); | |
80 | ||
81 | if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING) | |
82 | goto out; | |
83 | ||
84 | /* we are in the process of shutting this interface down */ | |
85 | if ((hard_iface->if_status == BATADV_IF_NOT_IN_USE) || | |
86 | (hard_iface->if_status == BATADV_IF_TO_BE_REMOVED)) | |
87 | goto out; | |
88 | ||
89 | /* the interface was enabled but may not be ready yet */ | |
90 | if (hard_iface->if_status != BATADV_IF_ACTIVE) | |
91 | goto restart_timer; | |
92 | ||
93 | skb = skb_copy(hard_iface->bat_v.elp_skb, GFP_ATOMIC); | |
94 | if (!skb) | |
95 | goto restart_timer; | |
96 | ||
97 | elp_packet = (struct batadv_elp_packet *)skb->data; | |
98 | elp_packet->seqno = htonl(atomic_read(&hard_iface->bat_v.elp_seqno)); | |
99 | elp_interval = atomic_read(&hard_iface->bat_v.elp_interval); | |
100 | elp_packet->elp_interval = htonl(elp_interval); | |
101 | ||
102 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, | |
103 | "Sending broadcast ELP packet on interface %s, seqno %u\n", | |
104 | hard_iface->net_dev->name, | |
105 | atomic_read(&hard_iface->bat_v.elp_seqno)); | |
106 | ||
107 | batadv_send_skb_packet(skb, hard_iface, batadv_broadcast_addr); | |
108 | ||
109 | atomic_inc(&hard_iface->bat_v.elp_seqno); | |
110 | ||
111 | restart_timer: | |
112 | batadv_v_elp_start_timer(hard_iface); | |
113 | out: | |
114 | return; | |
115 | } | |
116 | ||
117 | /** | |
118 | * batadv_v_elp_iface_enable - setup the ELP interface private resources | |
119 | * @hard_iface: interface for which the data has to be prepared | |
120 | * | |
121 | * Return: 0 on success or a -ENOMEM in case of failure. | |
122 | */ | |
123 | int batadv_v_elp_iface_enable(struct batadv_hard_iface *hard_iface) | |
124 | { | |
125 | struct batadv_elp_packet *elp_packet; | |
126 | unsigned char *elp_buff; | |
127 | u32 random_seqno; | |
128 | size_t size; | |
129 | int res = -ENOMEM; | |
130 | ||
131 | size = ETH_HLEN + NET_IP_ALIGN + BATADV_ELP_HLEN; | |
132 | hard_iface->bat_v.elp_skb = dev_alloc_skb(size); | |
133 | if (!hard_iface->bat_v.elp_skb) | |
134 | goto out; | |
135 | ||
136 | skb_reserve(hard_iface->bat_v.elp_skb, ETH_HLEN + NET_IP_ALIGN); | |
137 | elp_buff = skb_push(hard_iface->bat_v.elp_skb, BATADV_ELP_HLEN); | |
138 | elp_packet = (struct batadv_elp_packet *)elp_buff; | |
139 | memset(elp_packet, 0, BATADV_ELP_HLEN); | |
140 | ||
141 | elp_packet->packet_type = BATADV_ELP; | |
142 | elp_packet->version = BATADV_COMPAT_VERSION; | |
143 | ||
144 | /* randomize initial seqno to avoid collision */ | |
145 | get_random_bytes(&random_seqno, sizeof(random_seqno)); | |
146 | atomic_set(&hard_iface->bat_v.elp_seqno, random_seqno); | |
147 | atomic_set(&hard_iface->bat_v.elp_interval, 500); | |
148 | ||
149 | INIT_DELAYED_WORK(&hard_iface->bat_v.elp_wq, | |
150 | batadv_v_elp_periodic_work); | |
151 | batadv_v_elp_start_timer(hard_iface); | |
152 | res = 0; | |
153 | ||
154 | out: | |
155 | return res; | |
156 | } | |
157 | ||
158 | /** | |
159 | * batadv_v_elp_iface_disable - release ELP interface private resources | |
160 | * @hard_iface: interface for which the resources have to be released | |
161 | */ | |
162 | void batadv_v_elp_iface_disable(struct batadv_hard_iface *hard_iface) | |
163 | { | |
164 | cancel_delayed_work_sync(&hard_iface->bat_v.elp_wq); | |
165 | ||
166 | dev_kfree_skb(hard_iface->bat_v.elp_skb); | |
167 | hard_iface->bat_v.elp_skb = NULL; | |
168 | } | |
169 | ||
170 | /** | |
171 | * batadv_v_elp_primary_iface_set - change internal data to reflect the new | |
172 | * primary interface | |
173 | * @primary_iface: the new primary interface | |
174 | */ | |
175 | void batadv_v_elp_primary_iface_set(struct batadv_hard_iface *primary_iface) | |
176 | { | |
177 | struct batadv_hard_iface *hard_iface; | |
178 | struct batadv_elp_packet *elp_packet; | |
179 | struct sk_buff *skb; | |
180 | ||
181 | /* update orig field of every elp iface belonging to this mesh */ | |
182 | rcu_read_lock(); | |
183 | list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) { | |
184 | if (primary_iface->soft_iface != hard_iface->soft_iface) | |
185 | continue; | |
186 | ||
187 | if (!hard_iface->bat_v.elp_skb) | |
188 | continue; | |
189 | ||
190 | skb = hard_iface->bat_v.elp_skb; | |
191 | elp_packet = (struct batadv_elp_packet *)skb->data; | |
192 | ether_addr_copy(elp_packet->orig, | |
193 | primary_iface->net_dev->dev_addr); | |
194 | } | |
195 | rcu_read_unlock(); | |
196 | } | |
162bd64c | 197 | |
162bd64c LL |
198 | /** |
199 | * batadv_v_elp_neigh_update - update an ELP neighbour node | |
200 | * @bat_priv: the bat priv with all the soft interface information | |
201 | * @neigh_addr: the neighbour interface address | |
202 | * @if_incoming: the interface the packet was received through | |
203 | * @elp_packet: the received ELP packet | |
204 | * | |
205 | * Updates the ELP neighbour node state with the data received within the new | |
206 | * ELP packet. | |
207 | */ | |
208 | static void batadv_v_elp_neigh_update(struct batadv_priv *bat_priv, | |
209 | u8 *neigh_addr, | |
210 | struct batadv_hard_iface *if_incoming, | |
211 | struct batadv_elp_packet *elp_packet) | |
212 | ||
213 | { | |
214 | struct batadv_neigh_node *neigh; | |
215 | struct batadv_orig_node *orig_neigh; | |
216 | struct batadv_hardif_neigh_node *hardif_neigh; | |
217 | s32 seqno_diff; | |
218 | s32 elp_latest_seqno; | |
219 | ||
220 | orig_neigh = batadv_v_ogm_orig_get(bat_priv, elp_packet->orig); | |
221 | if (!orig_neigh) | |
222 | return; | |
223 | ||
224 | neigh = batadv_neigh_node_new(orig_neigh, if_incoming, neigh_addr); | |
225 | if (!neigh) | |
226 | goto orig_free; | |
227 | ||
228 | hardif_neigh = batadv_hardif_neigh_get(if_incoming, neigh_addr); | |
229 | if (!hardif_neigh) | |
230 | goto neigh_free; | |
231 | ||
232 | elp_latest_seqno = hardif_neigh->bat_v.elp_latest_seqno; | |
233 | seqno_diff = ntohl(elp_packet->seqno) - elp_latest_seqno; | |
234 | ||
235 | /* known or older sequence numbers are ignored. However always adopt | |
236 | * if the router seems to have been restarted. | |
237 | */ | |
238 | if (seqno_diff < 1 && seqno_diff > -BATADV_ELP_MAX_AGE) | |
239 | goto hardif_free; | |
240 | ||
241 | neigh->last_seen = jiffies; | |
242 | hardif_neigh->last_seen = jiffies; | |
243 | hardif_neigh->bat_v.elp_latest_seqno = ntohl(elp_packet->seqno); | |
244 | hardif_neigh->bat_v.elp_interval = ntohl(elp_packet->elp_interval); | |
245 | ||
246 | hardif_free: | |
247 | if (hardif_neigh) | |
248 | batadv_hardif_neigh_put(hardif_neigh); | |
249 | neigh_free: | |
250 | if (neigh) | |
251 | batadv_neigh_node_put(neigh); | |
252 | orig_free: | |
253 | if (orig_neigh) | |
254 | batadv_orig_node_put(orig_neigh); | |
255 | } | |
256 | ||
257 | /** | |
258 | * batadv_v_elp_packet_recv - main ELP packet handler | |
259 | * @skb: the received packet | |
260 | * @if_incoming: the interface this packet was received through | |
261 | * | |
262 | * Return: NET_RX_SUCCESS and consumes the skb if the packet was peoperly | |
263 | * processed or NET_RX_DROP in case of failure. | |
264 | */ | |
265 | int batadv_v_elp_packet_recv(struct sk_buff *skb, | |
266 | struct batadv_hard_iface *if_incoming) | |
267 | { | |
268 | struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface); | |
269 | struct batadv_elp_packet *elp_packet; | |
270 | struct batadv_hard_iface *primary_if; | |
271 | struct ethhdr *ethhdr = (struct ethhdr *)skb_mac_header(skb); | |
272 | bool ret; | |
273 | ||
274 | ret = batadv_check_management_packet(skb, if_incoming, BATADV_ELP_HLEN); | |
275 | if (!ret) | |
276 | return NET_RX_DROP; | |
277 | ||
278 | if (batadv_is_my_mac(bat_priv, ethhdr->h_source)) | |
279 | return NET_RX_DROP; | |
280 | ||
281 | /* did we receive a B.A.T.M.A.N. V ELP packet on an interface | |
282 | * that does not have B.A.T.M.A.N. V ELP enabled ? | |
283 | */ | |
284 | if (strcmp(bat_priv->bat_algo_ops->name, "BATMAN_V") != 0) | |
285 | return NET_RX_DROP; | |
286 | ||
287 | elp_packet = (struct batadv_elp_packet *)skb->data; | |
288 | ||
289 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, | |
290 | "Received ELP packet from %pM seqno %u ORIG: %pM\n", | |
291 | ethhdr->h_source, ntohl(elp_packet->seqno), | |
292 | elp_packet->orig); | |
293 | ||
294 | primary_if = batadv_primary_if_get_selected(bat_priv); | |
295 | if (!primary_if) | |
296 | goto out; | |
297 | ||
298 | batadv_v_elp_neigh_update(bat_priv, ethhdr->h_source, if_incoming, | |
299 | elp_packet); | |
300 | ||
301 | out: | |
302 | if (primary_if) | |
303 | batadv_hardif_put(primary_if); | |
304 | consume_skb(skb); | |
305 | return NET_RX_SUCCESS; | |
306 | } |