1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright (C) 2011-2019  B.A.T.M.A.N. contributors:
3  *
4  * Antonio Quartulli
5  */
6 
7 #ifndef _NET_BATMAN_ADV_DISTRIBUTED_ARP_TABLE_H_
8 #define _NET_BATMAN_ADV_DISTRIBUTED_ARP_TABLE_H_
9 
10 #include "main.h"
11 
12 #include <linux/compiler.h>
13 #include <linux/netdevice.h>
14 #include <linux/types.h>
15 #include <uapi/linux/batadv_packet.h>
16 
17 #include "originator.h"
18 
19 struct netlink_callback;
20 struct seq_file;
21 struct sk_buff;
22 
23 #ifdef CONFIG_BATMAN_ADV_DAT
24 
25 /* BATADV_DAT_ADDR_MAX - maximum address value in the DHT space */
26 #define BATADV_DAT_ADDR_MAX ((batadv_dat_addr_t)~(batadv_dat_addr_t)0)
27 
28 void batadv_dat_status_update(struct net_device *net_dev);
29 bool batadv_dat_snoop_outgoing_arp_request(struct batadv_priv *bat_priv,
30 					   struct sk_buff *skb);
31 bool batadv_dat_snoop_incoming_arp_request(struct batadv_priv *bat_priv,
32 					   struct sk_buff *skb, int hdr_size);
33 void batadv_dat_snoop_outgoing_arp_reply(struct batadv_priv *bat_priv,
34 					 struct sk_buff *skb);
35 bool batadv_dat_snoop_incoming_arp_reply(struct batadv_priv *bat_priv,
36 					 struct sk_buff *skb, int hdr_size);
37 void batadv_dat_snoop_outgoing_dhcp_ack(struct batadv_priv *bat_priv,
38 					struct sk_buff *skb,
39 					__be16 proto,
40 					unsigned short vid);
41 void batadv_dat_snoop_incoming_dhcp_ack(struct batadv_priv *bat_priv,
42 					struct sk_buff *skb, int hdr_size);
43 bool batadv_dat_drop_broadcast_packet(struct batadv_priv *bat_priv,
44 				      struct batadv_forw_packet *forw_packet);
45 
46 /**
47  * batadv_dat_init_orig_node_addr() - assign a DAT address to the orig_node
48  * @orig_node: the node to assign the DAT address to
49  */
50 static inline void
51 batadv_dat_init_orig_node_addr(struct batadv_orig_node *orig_node)
52 {
53 	u32 addr;
54 
55 	addr = batadv_choose_orig(orig_node->orig, BATADV_DAT_ADDR_MAX);
56 	orig_node->dat_addr = (batadv_dat_addr_t)addr;
57 }
58 
59 /**
60  * batadv_dat_init_own_addr() - assign a DAT address to the node itself
61  * @bat_priv: the bat priv with all the soft interface information
62  * @primary_if: a pointer to the primary interface
63  */
64 static inline void
65 batadv_dat_init_own_addr(struct batadv_priv *bat_priv,
66 			 struct batadv_hard_iface *primary_if)
67 {
68 	u32 addr;
69 
70 	addr = batadv_choose_orig(primary_if->net_dev->dev_addr,
71 				  BATADV_DAT_ADDR_MAX);
72 
73 	bat_priv->dat.addr = (batadv_dat_addr_t)addr;
74 }
75 
76 int batadv_dat_init(struct batadv_priv *bat_priv);
77 void batadv_dat_free(struct batadv_priv *bat_priv);
78 int batadv_dat_cache_seq_print_text(struct seq_file *seq, void *offset);
79 int batadv_dat_cache_dump(struct sk_buff *msg, struct netlink_callback *cb);
80 
81 /**
82  * batadv_dat_inc_counter() - increment the correct DAT packet counter
83  * @bat_priv: the bat priv with all the soft interface information
84  * @subtype: the 4addr subtype of the packet to be counted
85  *
86  * Updates the ethtool statistics for the received packet if it is a DAT subtype
87  */
88 static inline void batadv_dat_inc_counter(struct batadv_priv *bat_priv,
89 					  u8 subtype)
90 {
91 	switch (subtype) {
92 	case BATADV_P_DAT_DHT_GET:
93 		batadv_inc_counter(bat_priv,
94 				   BATADV_CNT_DAT_GET_RX);
95 		break;
96 	case BATADV_P_DAT_DHT_PUT:
97 		batadv_inc_counter(bat_priv,
98 				   BATADV_CNT_DAT_PUT_RX);
99 		break;
100 	}
101 }
102 
103 #else
104 
105 static inline void batadv_dat_status_update(struct net_device *net_dev)
106 {
107 }
108 
109 static inline bool
110 batadv_dat_snoop_outgoing_arp_request(struct batadv_priv *bat_priv,
111 				      struct sk_buff *skb)
112 {
113 	return false;
114 }
115 
116 static inline bool
117 batadv_dat_snoop_incoming_arp_request(struct batadv_priv *bat_priv,
118 				      struct sk_buff *skb, int hdr_size)
119 {
120 	return false;
121 }
122 
123 static inline bool
124 batadv_dat_snoop_outgoing_arp_reply(struct batadv_priv *bat_priv,
125 				    struct sk_buff *skb)
126 {
127 	return false;
128 }
129 
130 static inline bool
131 batadv_dat_snoop_incoming_arp_reply(struct batadv_priv *bat_priv,
132 				    struct sk_buff *skb, int hdr_size)
133 {
134 	return false;
135 }
136 
137 static inline void
138 batadv_dat_snoop_outgoing_dhcp_ack(struct batadv_priv *bat_priv,
139 				   struct sk_buff *skb, __be16 proto,
140 				   unsigned short vid)
141 {
142 }
143 
144 static inline void
145 batadv_dat_snoop_incoming_dhcp_ack(struct batadv_priv *bat_priv,
146 				   struct sk_buff *skb, int hdr_size)
147 {
148 }
149 
150 static inline bool
151 batadv_dat_drop_broadcast_packet(struct batadv_priv *bat_priv,
152 				 struct batadv_forw_packet *forw_packet)
153 {
154 	return false;
155 }
156 
157 static inline void
158 batadv_dat_init_orig_node_addr(struct batadv_orig_node *orig_node)
159 {
160 }
161 
162 static inline void batadv_dat_init_own_addr(struct batadv_priv *bat_priv,
163 					    struct batadv_hard_iface *iface)
164 {
165 }
166 
167 static inline void batadv_arp_change_timeout(struct net_device *soft_iface,
168 					     const char *name)
169 {
170 }
171 
172 static inline int batadv_dat_init(struct batadv_priv *bat_priv)
173 {
174 	return 0;
175 }
176 
177 static inline void batadv_dat_free(struct batadv_priv *bat_priv)
178 {
179 }
180 
181 static inline int
182 batadv_dat_cache_dump(struct sk_buff *msg, struct netlink_callback *cb)
183 {
184 	return -EOPNOTSUPP;
185 }
186 
187 static inline void batadv_dat_inc_counter(struct batadv_priv *bat_priv,
188 					  u8 subtype)
189 {
190 }
191 
192 #endif /* CONFIG_BATMAN_ADV_DAT */
193 
194 #endif /* _NET_BATMAN_ADV_DISTRIBUTED_ARP_TABLE_H_ */
195