]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - drivers/staging/batman-adv/soft-interface.c
Staging: batman-adv: 32bit sequence number and TTL for broadcasts
[mirror_ubuntu-jammy-kernel.git] / drivers / staging / batman-adv / soft-interface.c
1 /*
2 * Copyright (C) 2007-2010 B.A.T.M.A.N. contributors:
3 *
4 * Marek Lindner, Simon Wunderlich
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 * 02110-1301, USA
19 *
20 */
21
22 #include "main.h"
23 #include "soft-interface.h"
24 #include "hard-interface.h"
25 #include "send.h"
26 #include "translation-table.h"
27 #include "types.h"
28 #include "hash.h"
29 #include <linux/slab.h>
30 #include <linux/ethtool.h>
31 #include <linux/etherdevice.h>
32
33 static uint32_t bcast_seqno = 1; /* give own bcast messages seq numbers to avoid
34 * broadcast storms */
35 static int32_t skb_packets;
36 static int32_t skb_bad_packets;
37
38 unsigned char mainIfAddr[ETH_ALEN];
39 static int bat_get_settings(struct net_device *dev, struct ethtool_cmd *cmd);
40 static void bat_get_drvinfo(struct net_device *dev,
41 struct ethtool_drvinfo *info);
42 static u32 bat_get_msglevel(struct net_device *dev);
43 static void bat_set_msglevel(struct net_device *dev, u32 value);
44 static u32 bat_get_link(struct net_device *dev);
45 static u32 bat_get_rx_csum(struct net_device *dev);
46 static int bat_set_rx_csum(struct net_device *dev, u32 data);
47
48 static const struct ethtool_ops bat_ethtool_ops = {
49 .get_settings = bat_get_settings,
50 .get_drvinfo = bat_get_drvinfo,
51 .get_msglevel = bat_get_msglevel,
52 .set_msglevel = bat_set_msglevel,
53 .get_link = bat_get_link,
54 .get_rx_csum = bat_get_rx_csum,
55 .set_rx_csum = bat_set_rx_csum
56 };
57
58 void set_main_if_addr(uint8_t *addr)
59 {
60 memcpy(mainIfAddr, addr, ETH_ALEN);
61 }
62
63 int my_skb_push(struct sk_buff *skb, unsigned int len)
64 {
65 int result = 0;
66
67 skb_packets++;
68 if (skb_headroom(skb) < len) {
69 skb_bad_packets++;
70 result = pskb_expand_head(skb, len, 0, GFP_ATOMIC);
71
72 if (result < 0)
73 return result;
74 }
75
76 skb_push(skb, len);
77 return 0;
78 }
79
80 static int interface_open(struct net_device *dev)
81 {
82 netif_start_queue(dev);
83 return 0;
84 }
85
86 static int interface_release(struct net_device *dev)
87 {
88 netif_stop_queue(dev);
89 return 0;
90 }
91
92 static struct net_device_stats *interface_stats(struct net_device *dev)
93 {
94 struct bat_priv *priv = netdev_priv(dev);
95 return &priv->stats;
96 }
97
98 static int interface_set_mac_addr(struct net_device *dev, void *p)
99 {
100 struct sockaddr *addr = p;
101
102 if (!is_valid_ether_addr(addr->sa_data))
103 return -EADDRNOTAVAIL;
104
105 /* only modify hna-table if it has been initialised before */
106 if (atomic_read(&module_state) == MODULE_ACTIVE) {
107 hna_local_remove(dev->dev_addr, "mac address changed");
108 hna_local_add(addr->sa_data);
109 }
110
111 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
112
113 return 0;
114 }
115
116 static int interface_change_mtu(struct net_device *dev, int new_mtu)
117 {
118 /* check ranges */
119 if ((new_mtu < 68) || (new_mtu > hardif_min_mtu()))
120 return -EINVAL;
121
122 dev->mtu = new_mtu;
123
124 return 0;
125 }
126
127 int interface_tx(struct sk_buff *skb, struct net_device *dev)
128 {
129 struct unicast_packet *unicast_packet;
130 struct bcast_packet *bcast_packet;
131 struct orig_node *orig_node;
132 struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
133 struct bat_priv *priv = netdev_priv(dev);
134 struct batman_if *batman_if;
135 struct bat_priv *bat_priv;
136 uint8_t dstaddr[6];
137 int data_len = skb->len;
138 unsigned long flags;
139
140 if (atomic_read(&module_state) != MODULE_ACTIVE)
141 goto dropped;
142
143 /* FIXME: each batman_if will be attached to a softif */
144 bat_priv = netdev_priv(soft_device);
145
146 dev->trans_start = jiffies;
147 /* TODO: check this for locks */
148 hna_local_add(ethhdr->h_source);
149
150 /* ethernet packet should be broadcasted */
151 if (is_bcast(ethhdr->h_dest) || is_mcast(ethhdr->h_dest)) {
152
153 if (my_skb_push(skb, sizeof(struct bcast_packet)) < 0)
154 goto dropped;
155
156 bcast_packet = (struct bcast_packet *)skb->data;
157 bcast_packet->version = COMPAT_VERSION;
158 bcast_packet->ttl = TTL;
159
160 /* batman packet type: broadcast */
161 bcast_packet->packet_type = BAT_BCAST;
162
163 /* hw address of first interface is the orig mac because only
164 * this mac is known throughout the mesh */
165 memcpy(bcast_packet->orig, mainIfAddr, ETH_ALEN);
166
167 /* set broadcast sequence number */
168 bcast_packet->seqno = htonl(bcast_seqno);
169
170 /* broadcast packet. on success, increase seqno. */
171 if (add_bcast_packet_to_list(skb) == NETDEV_TX_OK)
172 bcast_seqno++;
173
174 /* a copy is stored in the bcast list, therefore removing
175 * the original skb. */
176 kfree_skb(skb);
177
178 /* unicast packet */
179 } else {
180 spin_lock_irqsave(&orig_hash_lock, flags);
181 /* get routing information */
182 orig_node = ((struct orig_node *)hash_find(orig_hash,
183 ethhdr->h_dest));
184
185 /* check for hna host */
186 if (!orig_node)
187 orig_node = transtable_search(ethhdr->h_dest);
188
189 if ((orig_node) &&
190 (orig_node->router)) {
191 struct neigh_node *router = orig_node->router;
192
193 if (my_skb_push(skb, sizeof(struct unicast_packet)) < 0)
194 goto unlock;
195
196 unicast_packet = (struct unicast_packet *)skb->data;
197
198 unicast_packet->version = COMPAT_VERSION;
199 /* batman packet type: unicast */
200 unicast_packet->packet_type = BAT_UNICAST;
201 /* set unicast ttl */
202 unicast_packet->ttl = TTL;
203 /* copy the destination for faster routing */
204 memcpy(unicast_packet->dest, orig_node->orig, ETH_ALEN);
205
206 /* net_dev won't be available when not active */
207 if (router->if_incoming->if_status != IF_ACTIVE)
208 goto unlock;
209
210 /* don't lock while sending the packets ... we therefore
211 * copy the required data before sending */
212
213 batman_if = router->if_incoming;
214 memcpy(dstaddr, router->addr, ETH_ALEN);
215 spin_unlock_irqrestore(&orig_hash_lock, flags);
216
217 send_skb_packet(skb, batman_if, dstaddr);
218 } else {
219 goto unlock;
220 }
221 }
222
223 priv->stats.tx_packets++;
224 priv->stats.tx_bytes += data_len;
225 goto end;
226
227 unlock:
228 spin_unlock_irqrestore(&orig_hash_lock, flags);
229 dropped:
230 priv->stats.tx_dropped++;
231 kfree_skb(skb);
232 end:
233 return NETDEV_TX_OK;
234 }
235
236 void interface_rx(struct sk_buff *skb, int hdr_size)
237 {
238 struct net_device *dev = soft_device;
239 struct bat_priv *priv = netdev_priv(dev);
240
241 /* check if enough space is available for pulling, and pull */
242 if (!pskb_may_pull(skb, hdr_size)) {
243 kfree_skb(skb);
244 return;
245 }
246 skb_pull_rcsum(skb, hdr_size);
247 /* skb_set_mac_header(skb, -sizeof(struct ethhdr));*/
248
249 skb->dev = dev;
250 skb->protocol = eth_type_trans(skb, dev);
251
252 /* should not be neccesary anymore as we use skb_pull_rcsum()
253 * TODO: please verify this and remove this TODO
254 * -- Dec 21st 2009, Simon Wunderlich */
255
256 /* skb->ip_summed = CHECKSUM_UNNECESSARY;*/
257
258 /* TODO: set skb->pkt_type to PACKET_BROADCAST, PACKET_MULTICAST,
259 * PACKET_OTHERHOST or PACKET_HOST */
260
261 priv->stats.rx_packets++;
262 priv->stats.rx_bytes += skb->len;
263
264 dev->last_rx = jiffies;
265
266 netif_rx(skb);
267 }
268
269 #ifdef HAVE_NET_DEVICE_OPS
270 static const struct net_device_ops bat_netdev_ops = {
271 .ndo_open = interface_open,
272 .ndo_stop = interface_release,
273 .ndo_get_stats = interface_stats,
274 .ndo_set_mac_address = interface_set_mac_addr,
275 .ndo_change_mtu = interface_change_mtu,
276 .ndo_start_xmit = interface_tx,
277 .ndo_validate_addr = eth_validate_addr
278 };
279 #endif
280
281 void interface_setup(struct net_device *dev)
282 {
283 struct bat_priv *priv = netdev_priv(dev);
284 char dev_addr[ETH_ALEN];
285
286 ether_setup(dev);
287
288 #ifdef HAVE_NET_DEVICE_OPS
289 dev->netdev_ops = &bat_netdev_ops;
290 #else
291 dev->open = interface_open;
292 dev->stop = interface_release;
293 dev->get_stats = interface_stats;
294 dev->set_mac_address = interface_set_mac_addr;
295 dev->change_mtu = interface_change_mtu;
296 dev->hard_start_xmit = interface_tx;
297 #endif
298 dev->destructor = free_netdev;
299
300 dev->mtu = hardif_min_mtu();
301 dev->hard_header_len = BAT_HEADER_LEN; /* reserve more space in the
302 * skbuff for our header */
303
304 /* generate random address */
305 random_ether_addr(dev_addr);
306 memcpy(dev->dev_addr, dev_addr, ETH_ALEN);
307
308 SET_ETHTOOL_OPS(dev, &bat_ethtool_ops);
309
310 memset(priv, 0, sizeof(struct bat_priv));
311 }
312
313 /* ethtool */
314 static int bat_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
315 {
316 cmd->supported = 0;
317 cmd->advertising = 0;
318 cmd->speed = SPEED_10;
319 cmd->duplex = DUPLEX_FULL;
320 cmd->port = PORT_TP;
321 cmd->phy_address = 0;
322 cmd->transceiver = XCVR_INTERNAL;
323 cmd->autoneg = AUTONEG_DISABLE;
324 cmd->maxtxpkt = 0;
325 cmd->maxrxpkt = 0;
326
327 return 0;
328 }
329
330 static void bat_get_drvinfo(struct net_device *dev,
331 struct ethtool_drvinfo *info)
332 {
333 strcpy(info->driver, "B.A.T.M.A.N. advanced");
334 strcpy(info->version, SOURCE_VERSION);
335 strcpy(info->fw_version, "N/A");
336 strcpy(info->bus_info, "batman");
337 }
338
339 static u32 bat_get_msglevel(struct net_device *dev)
340 {
341 return -EOPNOTSUPP;
342 }
343
344 static void bat_set_msglevel(struct net_device *dev, u32 value)
345 {
346 }
347
348 static u32 bat_get_link(struct net_device *dev)
349 {
350 return 1;
351 }
352
353 static u32 bat_get_rx_csum(struct net_device *dev)
354 {
355 return 0;
356 }
357
358 static int bat_set_rx_csum(struct net_device *dev, u32 data)
359 {
360 return -EOPNOTSUPP;
361 }