18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci/* Copyright (C) 2007-2020  B.A.T.M.A.N. contributors:
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Marek Lindner, Simon Wunderlich
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#ifndef _NET_BATMAN_ADV_MAIN_H_
88c2ecf20Sopenharmony_ci#define _NET_BATMAN_ADV_MAIN_H_
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#define BATADV_DRIVER_AUTHOR "Marek Lindner <mareklindner@neomailbox.ch>, " \
118c2ecf20Sopenharmony_ci			     "Simon Wunderlich <sw@simonwunderlich.de>"
128c2ecf20Sopenharmony_ci#define BATADV_DRIVER_DESC   "B.A.T.M.A.N. advanced"
138c2ecf20Sopenharmony_ci#define BATADV_DRIVER_DEVICE "batman-adv"
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#ifndef BATADV_SOURCE_VERSION
168c2ecf20Sopenharmony_ci#define BATADV_SOURCE_VERSION "2020.4"
178c2ecf20Sopenharmony_ci#endif
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci/* B.A.T.M.A.N. parameters */
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#define BATADV_TQ_MAX_VALUE 255
228c2ecf20Sopenharmony_ci#define BATADV_THROUGHPUT_MAX_VALUE 0xFFFFFFFF
238c2ecf20Sopenharmony_ci#define BATADV_JITTER 20
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci/* Time To Live of broadcast messages */
268c2ecf20Sopenharmony_ci#define BATADV_TTL 50
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci/* maximum sequence number age of broadcast messages */
298c2ecf20Sopenharmony_ci#define BATADV_BCAST_MAX_AGE 64
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci/* purge originators after time in seconds if no valid packet comes in
328c2ecf20Sopenharmony_ci * -> TODO: check influence on BATADV_TQ_LOCAL_WINDOW_SIZE
338c2ecf20Sopenharmony_ci */
348c2ecf20Sopenharmony_ci#define BATADV_PURGE_TIMEOUT 200000 /* 200 seconds */
358c2ecf20Sopenharmony_ci#define BATADV_TT_LOCAL_TIMEOUT 600000 /* in milliseconds */
368c2ecf20Sopenharmony_ci#define BATADV_TT_CLIENT_ROAM_TIMEOUT 600000 /* in milliseconds */
378c2ecf20Sopenharmony_ci#define BATADV_TT_CLIENT_TEMP_TIMEOUT 600000 /* in milliseconds */
388c2ecf20Sopenharmony_ci#define BATADV_TT_WORK_PERIOD 5000 /* 5 seconds */
398c2ecf20Sopenharmony_ci#define BATADV_ORIG_WORK_PERIOD 1000 /* 1 second */
408c2ecf20Sopenharmony_ci#define BATADV_MCAST_WORK_PERIOD 500 /* 0.5 seconds */
418c2ecf20Sopenharmony_ci#define BATADV_DAT_ENTRY_TIMEOUT (5 * 60000) /* 5 mins in milliseconds */
428c2ecf20Sopenharmony_ci/* sliding packet range of received originator messages in sequence numbers
438c2ecf20Sopenharmony_ci * (should be a multiple of our word size)
448c2ecf20Sopenharmony_ci */
458c2ecf20Sopenharmony_ci#define BATADV_TQ_LOCAL_WINDOW_SIZE 64
468c2ecf20Sopenharmony_ci/* milliseconds we have to keep pending tt_req */
478c2ecf20Sopenharmony_ci#define BATADV_TT_REQUEST_TIMEOUT 3000
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci#define BATADV_TQ_GLOBAL_WINDOW_SIZE 5
508c2ecf20Sopenharmony_ci#define BATADV_TQ_LOCAL_BIDRECT_SEND_MINIMUM 1
518c2ecf20Sopenharmony_ci#define BATADV_TQ_LOCAL_BIDRECT_RECV_MINIMUM 1
528c2ecf20Sopenharmony_ci#define BATADV_TQ_TOTAL_BIDRECT_LIMIT 1
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci/* B.A.T.M.A.N. V */
558c2ecf20Sopenharmony_ci#define BATADV_THROUGHPUT_DEFAULT_VALUE 10 /* 1 Mbps */
568c2ecf20Sopenharmony_ci#define BATADV_ELP_PROBES_PER_NODE 2
578c2ecf20Sopenharmony_ci#define BATADV_ELP_MIN_PROBE_SIZE 200 /* bytes */
588c2ecf20Sopenharmony_ci#define BATADV_ELP_PROBE_MAX_TX_DIFF 100 /* milliseconds */
598c2ecf20Sopenharmony_ci#define BATADV_ELP_MAX_AGE 64
608c2ecf20Sopenharmony_ci#define BATADV_OGM_MAX_ORIGDIFF 5
618c2ecf20Sopenharmony_ci#define BATADV_OGM_MAX_AGE 64
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci/* number of OGMs sent with the last tt diff */
648c2ecf20Sopenharmony_ci#define BATADV_TT_OGM_APPEND_MAX 3
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci/* Time in which a client can roam at most ROAMING_MAX_COUNT times in
678c2ecf20Sopenharmony_ci * milliseconds
688c2ecf20Sopenharmony_ci */
698c2ecf20Sopenharmony_ci#define BATADV_ROAMING_MAX_TIME 20000
708c2ecf20Sopenharmony_ci#define BATADV_ROAMING_MAX_COUNT 5
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci#define BATADV_NO_FLAGS 0
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci#define BATADV_NULL_IFINDEX 0 /* dummy ifindex used to avoid iface checks */
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci#define BATADV_NO_MARK 0
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci/* default interface for multi interface operation. The default interface is
798c2ecf20Sopenharmony_ci * used for communication which originated locally (i.e. is not forwarded)
808c2ecf20Sopenharmony_ci * or where special forwarding is not desired/necessary.
818c2ecf20Sopenharmony_ci */
828c2ecf20Sopenharmony_ci#define BATADV_IF_DEFAULT	((struct batadv_hard_iface *)NULL)
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci#define BATADV_NUM_WORDS BITS_TO_LONGS(BATADV_TQ_LOCAL_WINDOW_SIZE)
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci#define BATADV_LOG_BUF_LEN 8192	  /* has to be a power of 2 */
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci/* number of packets to send for broadcasts on different interface types */
898c2ecf20Sopenharmony_ci#define BATADV_NUM_BCASTS_DEFAULT 1
908c2ecf20Sopenharmony_ci#define BATADV_NUM_BCASTS_WIRELESS 3
918c2ecf20Sopenharmony_ci#define BATADV_NUM_BCASTS_MAX 3
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci/* length of the single packet used by the TP meter */
948c2ecf20Sopenharmony_ci#define BATADV_TP_PACKET_LEN ETH_DATA_LEN
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci/* msecs after which an ARP_REQUEST is sent in broadcast as fallback */
978c2ecf20Sopenharmony_ci#define ARP_REQ_DELAY 250
988c2ecf20Sopenharmony_ci/* numbers of originator to contact for any PUT/GET DHT operation */
998c2ecf20Sopenharmony_ci#define BATADV_DAT_CANDIDATES_NUM 3
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci/* BATADV_TQ_SIMILARITY_THRESHOLD - TQ points that a secondary metric can differ
1028c2ecf20Sopenharmony_ci * at most from the primary one in order to be still considered acceptable
1038c2ecf20Sopenharmony_ci */
1048c2ecf20Sopenharmony_ci#define BATADV_TQ_SIMILARITY_THRESHOLD 50
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci/* should not be bigger than 512 bytes or change the size of
1078c2ecf20Sopenharmony_ci * forw_packet->direct_link_flags
1088c2ecf20Sopenharmony_ci */
1098c2ecf20Sopenharmony_ci#define BATADV_MAX_AGGREGATION_BYTES 512
1108c2ecf20Sopenharmony_ci#define BATADV_MAX_AGGREGATION_MS 100
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci#define BATADV_BLA_PERIOD_LENGTH	10000	/* 10 seconds */
1138c2ecf20Sopenharmony_ci#define BATADV_BLA_BACKBONE_TIMEOUT	(BATADV_BLA_PERIOD_LENGTH * 6)
1148c2ecf20Sopenharmony_ci#define BATADV_BLA_CLAIM_TIMEOUT	(BATADV_BLA_PERIOD_LENGTH * 10)
1158c2ecf20Sopenharmony_ci#define BATADV_BLA_WAIT_PERIODS		3
1168c2ecf20Sopenharmony_ci#define BATADV_BLA_LOOPDETECT_PERIODS	6
1178c2ecf20Sopenharmony_ci#define BATADV_BLA_LOOPDETECT_TIMEOUT	3000	/* 3 seconds */
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci#define BATADV_DUPLIST_SIZE		16
1208c2ecf20Sopenharmony_ci#define BATADV_DUPLIST_TIMEOUT		500	/* 500 ms */
1218c2ecf20Sopenharmony_ci/* don't reset again within 30 seconds */
1228c2ecf20Sopenharmony_ci#define BATADV_RESET_PROTECTION_MS 30000
1238c2ecf20Sopenharmony_ci#define BATADV_EXPECTED_SEQNO_RANGE	65536
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci#define BATADV_NC_NODE_TIMEOUT 10000 /* Milliseconds */
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci/**
1288c2ecf20Sopenharmony_ci * BATADV_TP_MAX_NUM - maximum number of simultaneously active tp sessions
1298c2ecf20Sopenharmony_ci */
1308c2ecf20Sopenharmony_ci#define BATADV_TP_MAX_NUM 5
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci/**
1338c2ecf20Sopenharmony_ci * enum batadv_mesh_state - State of a soft interface
1348c2ecf20Sopenharmony_ci */
1358c2ecf20Sopenharmony_cienum batadv_mesh_state {
1368c2ecf20Sopenharmony_ci	/** @BATADV_MESH_INACTIVE: soft interface is not yet running */
1378c2ecf20Sopenharmony_ci	BATADV_MESH_INACTIVE,
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	/** @BATADV_MESH_ACTIVE: interface is up and running */
1408c2ecf20Sopenharmony_ci	BATADV_MESH_ACTIVE,
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	/** @BATADV_MESH_DEACTIVATING: interface is getting shut down */
1438c2ecf20Sopenharmony_ci	BATADV_MESH_DEACTIVATING,
1448c2ecf20Sopenharmony_ci};
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci#define BATADV_BCAST_QUEUE_LEN		256
1478c2ecf20Sopenharmony_ci#define BATADV_BATMAN_QUEUE_LEN	256
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci/**
1508c2ecf20Sopenharmony_ci * enum batadv_uev_action - action type of uevent
1518c2ecf20Sopenharmony_ci */
1528c2ecf20Sopenharmony_cienum batadv_uev_action {
1538c2ecf20Sopenharmony_ci	/** @BATADV_UEV_ADD: gateway was selected (after none was selected) */
1548c2ecf20Sopenharmony_ci	BATADV_UEV_ADD = 0,
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	/**
1578c2ecf20Sopenharmony_ci	 * @BATADV_UEV_DEL: selected gateway was removed and none is selected
1588c2ecf20Sopenharmony_ci	 * anymore
1598c2ecf20Sopenharmony_ci	 */
1608c2ecf20Sopenharmony_ci	BATADV_UEV_DEL,
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	/**
1638c2ecf20Sopenharmony_ci	 * @BATADV_UEV_CHANGE: a different gateway was selected as based gateway
1648c2ecf20Sopenharmony_ci	 */
1658c2ecf20Sopenharmony_ci	BATADV_UEV_CHANGE,
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci	/**
1688c2ecf20Sopenharmony_ci	 * @BATADV_UEV_LOOPDETECT: loop was detected which cannot be handled by
1698c2ecf20Sopenharmony_ci	 * bridge loop avoidance
1708c2ecf20Sopenharmony_ci	 */
1718c2ecf20Sopenharmony_ci	BATADV_UEV_LOOPDETECT,
1728c2ecf20Sopenharmony_ci};
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci/**
1758c2ecf20Sopenharmony_ci * enum batadv_uev_type - Type of uevent
1768c2ecf20Sopenharmony_ci */
1778c2ecf20Sopenharmony_cienum batadv_uev_type {
1788c2ecf20Sopenharmony_ci	/** @BATADV_UEV_GW: selected gateway was modified */
1798c2ecf20Sopenharmony_ci	BATADV_UEV_GW = 0,
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	/** @BATADV_UEV_BLA: bridge loop avoidance event */
1828c2ecf20Sopenharmony_ci	BATADV_UEV_BLA,
1838c2ecf20Sopenharmony_ci};
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci#define BATADV_GW_THRESHOLD	50
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci/* Number of fragment chains for each orig_node */
1888c2ecf20Sopenharmony_ci#define BATADV_FRAG_BUFFER_COUNT 8
1898c2ecf20Sopenharmony_ci/* Maximum number of fragments for one packet */
1908c2ecf20Sopenharmony_ci#define BATADV_FRAG_MAX_FRAGMENTS 16
1918c2ecf20Sopenharmony_ci/* Maxumim size of each fragment */
1928c2ecf20Sopenharmony_ci#define BATADV_FRAG_MAX_FRAG_SIZE 1280
1938c2ecf20Sopenharmony_ci/* Time to keep fragments while waiting for rest of the fragments */
1948c2ecf20Sopenharmony_ci#define BATADV_FRAG_TIMEOUT 10000
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci#define BATADV_DAT_CANDIDATE_NOT_FOUND	0
1978c2ecf20Sopenharmony_ci#define BATADV_DAT_CANDIDATE_ORIG	1
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci/* Debug Messages */
2008c2ecf20Sopenharmony_ci#ifdef pr_fmt
2018c2ecf20Sopenharmony_ci#undef pr_fmt
2028c2ecf20Sopenharmony_ci#endif
2038c2ecf20Sopenharmony_ci/* Append 'batman-adv: ' before kernel messages */
2048c2ecf20Sopenharmony_ci#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci/* Kernel headers */
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci#include <linux/atomic.h>
2098c2ecf20Sopenharmony_ci#include <linux/compiler.h>
2108c2ecf20Sopenharmony_ci#include <linux/etherdevice.h>
2118c2ecf20Sopenharmony_ci#include <linux/if_vlan.h>
2128c2ecf20Sopenharmony_ci#include <linux/jiffies.h>
2138c2ecf20Sopenharmony_ci#include <linux/netdevice.h>
2148c2ecf20Sopenharmony_ci#include <linux/percpu.h>
2158c2ecf20Sopenharmony_ci#include <linux/seq_file.h>
2168c2ecf20Sopenharmony_ci#include <linux/skbuff.h>
2178c2ecf20Sopenharmony_ci#include <linux/types.h>
2188c2ecf20Sopenharmony_ci#include <uapi/linux/batadv_packet.h>
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci#include "types.h"
2218c2ecf20Sopenharmony_ci#include "main.h"
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci/**
2248c2ecf20Sopenharmony_ci * batadv_print_vid() - return printable version of vid information
2258c2ecf20Sopenharmony_ci * @vid: the VLAN identifier
2268c2ecf20Sopenharmony_ci *
2278c2ecf20Sopenharmony_ci * Return: -1 when no VLAN is used, VLAN id otherwise
2288c2ecf20Sopenharmony_ci */
2298c2ecf20Sopenharmony_cistatic inline int batadv_print_vid(unsigned short vid)
2308c2ecf20Sopenharmony_ci{
2318c2ecf20Sopenharmony_ci	if (vid & BATADV_VLAN_HAS_TAG)
2328c2ecf20Sopenharmony_ci		return (int)(vid & VLAN_VID_MASK);
2338c2ecf20Sopenharmony_ci	else
2348c2ecf20Sopenharmony_ci		return -1;
2358c2ecf20Sopenharmony_ci}
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ciextern struct list_head batadv_hardif_list;
2388c2ecf20Sopenharmony_ciextern unsigned int batadv_hardif_generation;
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ciextern unsigned char batadv_broadcast_addr[];
2418c2ecf20Sopenharmony_ciextern struct workqueue_struct *batadv_event_workqueue;
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ciint batadv_mesh_init(struct net_device *soft_iface);
2448c2ecf20Sopenharmony_civoid batadv_mesh_free(struct net_device *soft_iface);
2458c2ecf20Sopenharmony_cibool batadv_is_my_mac(struct batadv_priv *bat_priv, const u8 *addr);
2468c2ecf20Sopenharmony_cistruct batadv_hard_iface *
2478c2ecf20Sopenharmony_cibatadv_seq_print_text_primary_if_get(struct seq_file *seq);
2488c2ecf20Sopenharmony_ciint batadv_max_header_len(void);
2498c2ecf20Sopenharmony_civoid batadv_skb_set_priority(struct sk_buff *skb, int offset);
2508c2ecf20Sopenharmony_ciint batadv_batman_skb_recv(struct sk_buff *skb, struct net_device *dev,
2518c2ecf20Sopenharmony_ci			   struct packet_type *ptype,
2528c2ecf20Sopenharmony_ci			   struct net_device *orig_dev);
2538c2ecf20Sopenharmony_ciint
2548c2ecf20Sopenharmony_cibatadv_recv_handler_register(u8 packet_type,
2558c2ecf20Sopenharmony_ci			     int (*recv_handler)(struct sk_buff *,
2568c2ecf20Sopenharmony_ci						 struct batadv_hard_iface *));
2578c2ecf20Sopenharmony_civoid batadv_recv_handler_unregister(u8 packet_type);
2588c2ecf20Sopenharmony_ci__be32 batadv_skb_crc32(struct sk_buff *skb, u8 *payload_ptr);
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ci/**
2618c2ecf20Sopenharmony_ci * batadv_compare_eth() - Compare two not u16 aligned Ethernet addresses
2628c2ecf20Sopenharmony_ci * @data1: Pointer to a six-byte array containing the Ethernet address
2638c2ecf20Sopenharmony_ci * @data2: Pointer other six-byte array containing the Ethernet address
2648c2ecf20Sopenharmony_ci *
2658c2ecf20Sopenharmony_ci * note: can't use ether_addr_equal() as it requires aligned memory
2668c2ecf20Sopenharmony_ci *
2678c2ecf20Sopenharmony_ci * Return: true if they are the same ethernet addr
2688c2ecf20Sopenharmony_ci */
2698c2ecf20Sopenharmony_cistatic inline bool batadv_compare_eth(const void *data1, const void *data2)
2708c2ecf20Sopenharmony_ci{
2718c2ecf20Sopenharmony_ci	return ether_addr_equal_unaligned(data1, data2);
2728c2ecf20Sopenharmony_ci}
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci/**
2758c2ecf20Sopenharmony_ci * batadv_has_timed_out() - compares current time (jiffies) and timestamp +
2768c2ecf20Sopenharmony_ci *  timeout
2778c2ecf20Sopenharmony_ci * @timestamp:		base value to compare with (in jiffies)
2788c2ecf20Sopenharmony_ci * @timeout:		added to base value before comparing (in milliseconds)
2798c2ecf20Sopenharmony_ci *
2808c2ecf20Sopenharmony_ci * Return: true if current time is after timestamp + timeout
2818c2ecf20Sopenharmony_ci */
2828c2ecf20Sopenharmony_cistatic inline bool batadv_has_timed_out(unsigned long timestamp,
2838c2ecf20Sopenharmony_ci					unsigned int timeout)
2848c2ecf20Sopenharmony_ci{
2858c2ecf20Sopenharmony_ci	return time_is_before_jiffies(timestamp + msecs_to_jiffies(timeout));
2868c2ecf20Sopenharmony_ci}
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci/**
2898c2ecf20Sopenharmony_ci * batadv_atomic_dec_not_zero() - Decrease unless the number is 0
2908c2ecf20Sopenharmony_ci * @v: pointer of type atomic_t
2918c2ecf20Sopenharmony_ci *
2928c2ecf20Sopenharmony_ci * Return: non-zero if v was not 0, and zero otherwise.
2938c2ecf20Sopenharmony_ci */
2948c2ecf20Sopenharmony_ci#define batadv_atomic_dec_not_zero(v)	atomic_add_unless((v), -1, 0)
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci/**
2978c2ecf20Sopenharmony_ci * batadv_smallest_signed_int() - Returns the smallest signed integer in two's
2988c2ecf20Sopenharmony_ci *  complement with the sizeof x
2998c2ecf20Sopenharmony_ci * @x: type of integer
3008c2ecf20Sopenharmony_ci *
3018c2ecf20Sopenharmony_ci * Return: smallest signed integer of type
3028c2ecf20Sopenharmony_ci */
3038c2ecf20Sopenharmony_ci#define batadv_smallest_signed_int(x) (1u << (7u + 8u * (sizeof(x) - 1u)))
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci/**
3068c2ecf20Sopenharmony_ci * batadv_seq_before() - Checks if a sequence number x is a predecessor of y
3078c2ecf20Sopenharmony_ci * @x: potential predecessor of @y
3088c2ecf20Sopenharmony_ci * @y: value to compare @x against
3098c2ecf20Sopenharmony_ci *
3108c2ecf20Sopenharmony_ci * It handles overflows/underflows and can correctly check for a predecessor
3118c2ecf20Sopenharmony_ci * unless the variable sequence number has grown by more than
3128c2ecf20Sopenharmony_ci * 2**(bitwidth(x)-1)-1.
3138c2ecf20Sopenharmony_ci *
3148c2ecf20Sopenharmony_ci * This means that for a u8 with the maximum value 255, it would think:
3158c2ecf20Sopenharmony_ci *
3168c2ecf20Sopenharmony_ci * * when adding nothing - it is neither a predecessor nor a successor
3178c2ecf20Sopenharmony_ci * * before adding more than 127 to the starting value - it is a predecessor,
3188c2ecf20Sopenharmony_ci * * when adding 128 - it is neither a predecessor nor a successor,
3198c2ecf20Sopenharmony_ci * * after adding more than 127 to the starting value - it is a successor
3208c2ecf20Sopenharmony_ci *
3218c2ecf20Sopenharmony_ci * Return: true when x is a predecessor of y, false otherwise
3228c2ecf20Sopenharmony_ci */
3238c2ecf20Sopenharmony_ci#define batadv_seq_before(x, y) ({ \
3248c2ecf20Sopenharmony_ci	typeof(x)_d1 = (x); \
3258c2ecf20Sopenharmony_ci	typeof(y)_d2 = (y); \
3268c2ecf20Sopenharmony_ci	typeof(x)_dummy = (_d1 - _d2); \
3278c2ecf20Sopenharmony_ci	(void)(&_d1 == &_d2); \
3288c2ecf20Sopenharmony_ci	_dummy > batadv_smallest_signed_int(_dummy); \
3298c2ecf20Sopenharmony_ci})
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_ci/**
3328c2ecf20Sopenharmony_ci * batadv_seq_after() - Checks if a sequence number x is a successor of y
3338c2ecf20Sopenharmony_ci * @x: potential successor of @y
3348c2ecf20Sopenharmony_ci * @y: value to compare @x against
3358c2ecf20Sopenharmony_ci *
3368c2ecf20Sopenharmony_ci * It handles overflows/underflows and can correctly check for a successor
3378c2ecf20Sopenharmony_ci * unless the variable sequence number has grown by more than
3388c2ecf20Sopenharmony_ci * 2**(bitwidth(x)-1)-1.
3398c2ecf20Sopenharmony_ci *
3408c2ecf20Sopenharmony_ci * This means that for a u8 with the maximum value 255, it would think:
3418c2ecf20Sopenharmony_ci *
3428c2ecf20Sopenharmony_ci * * when adding nothing - it is neither a predecessor nor a successor
3438c2ecf20Sopenharmony_ci * * before adding more than 127 to the starting value - it is a predecessor,
3448c2ecf20Sopenharmony_ci * * when adding 128 - it is neither a predecessor nor a successor,
3458c2ecf20Sopenharmony_ci * * after adding more than 127 to the starting value - it is a successor
3468c2ecf20Sopenharmony_ci *
3478c2ecf20Sopenharmony_ci * Return: true when x is a successor of y, false otherwise
3488c2ecf20Sopenharmony_ci */
3498c2ecf20Sopenharmony_ci#define batadv_seq_after(x, y) batadv_seq_before(y, x)
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_ci/**
3528c2ecf20Sopenharmony_ci * batadv_add_counter() - Add to per cpu statistics counter of soft interface
3538c2ecf20Sopenharmony_ci * @bat_priv: the bat priv with all the soft interface information
3548c2ecf20Sopenharmony_ci * @idx: counter index which should be modified
3558c2ecf20Sopenharmony_ci * @count: value to increase counter by
3568c2ecf20Sopenharmony_ci *
3578c2ecf20Sopenharmony_ci * Stop preemption on local cpu while incrementing the counter
3588c2ecf20Sopenharmony_ci */
3598c2ecf20Sopenharmony_cistatic inline void batadv_add_counter(struct batadv_priv *bat_priv, size_t idx,
3608c2ecf20Sopenharmony_ci				      size_t count)
3618c2ecf20Sopenharmony_ci{
3628c2ecf20Sopenharmony_ci	this_cpu_add(bat_priv->bat_counters[idx], count);
3638c2ecf20Sopenharmony_ci}
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_ci/**
3668c2ecf20Sopenharmony_ci * batadv_inc_counter() - Increase per cpu statistics counter of soft interface
3678c2ecf20Sopenharmony_ci * @b: the bat priv with all the soft interface information
3688c2ecf20Sopenharmony_ci * @i: counter index which should be modified
3698c2ecf20Sopenharmony_ci */
3708c2ecf20Sopenharmony_ci#define batadv_inc_counter(b, i) batadv_add_counter(b, i, 1)
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ci/**
3738c2ecf20Sopenharmony_ci * BATADV_SKB_CB() - Get batadv_skb_cb from skb control buffer
3748c2ecf20Sopenharmony_ci * @__skb: skb holding the control buffer
3758c2ecf20Sopenharmony_ci *
3768c2ecf20Sopenharmony_ci * The members of the control buffer are defined in struct batadv_skb_cb in
3778c2ecf20Sopenharmony_ci * types.h. The macro is inspired by the similar macro TCP_SKB_CB() in tcp.h.
3788c2ecf20Sopenharmony_ci *
3798c2ecf20Sopenharmony_ci * Return: pointer to the batadv_skb_cb of the skb
3808c2ecf20Sopenharmony_ci */
3818c2ecf20Sopenharmony_ci#define BATADV_SKB_CB(__skb)       ((struct batadv_skb_cb *)&((__skb)->cb[0]))
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_ciunsigned short batadv_get_vid(struct sk_buff *skb, size_t header_len);
3848c2ecf20Sopenharmony_cibool batadv_vlan_ap_isola_get(struct batadv_priv *bat_priv, unsigned short vid);
3858c2ecf20Sopenharmony_ciint batadv_throw_uevent(struct batadv_priv *bat_priv, enum batadv_uev_type type,
3868c2ecf20Sopenharmony_ci			enum batadv_uev_action action, const char *data);
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci#endif /* _NET_BATMAN_ADV_MAIN_H_ */
389