1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) B.A.T.M.A.N. contributors:
7 #include "gateway_common.h"
10 #include <linux/atomic.h>
11 #include <linux/byteorder/generic.h>
12 #include <linux/stddef.h>
13 #include <linux/types.h>
14 #include <uapi/linux/batadv_packet.h>
15 #include <uapi/linux/batman_adv.h>
17 #include "gateway_client.h"
21 * batadv_gw_tvlv_container_update() - update the gw tvlv container after
22 * gateway setting change
23 * @bat_priv: the bat priv with all the soft interface information
25 void batadv_gw_tvlv_container_update(struct batadv_priv
*bat_priv
)
27 struct batadv_tvlv_gateway_data gw
;
31 gw_mode
= atomic_read(&bat_priv
->gw
.mode
);
34 case BATADV_GW_MODE_OFF
:
35 case BATADV_GW_MODE_CLIENT
:
36 batadv_tvlv_container_unregister(bat_priv
, BATADV_TVLV_GW
, 1);
38 case BATADV_GW_MODE_SERVER
:
39 down
= atomic_read(&bat_priv
->gw
.bandwidth_down
);
40 up
= atomic_read(&bat_priv
->gw
.bandwidth_up
);
41 gw
.bandwidth_down
= htonl(down
);
42 gw
.bandwidth_up
= htonl(up
);
43 batadv_tvlv_container_register(bat_priv
, BATADV_TVLV_GW
, 1,
50 * batadv_gw_tvlv_ogm_handler_v1() - process incoming gateway tvlv container
51 * @bat_priv: the bat priv with all the soft interface information
52 * @orig: the orig_node of the ogm
53 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
54 * @tvlv_value: tvlv buffer containing the gateway data
55 * @tvlv_value_len: tvlv buffer length
57 static void batadv_gw_tvlv_ogm_handler_v1(struct batadv_priv
*bat_priv
,
58 struct batadv_orig_node
*orig
,
60 void *tvlv_value
, u16 tvlv_value_len
)
62 struct batadv_tvlv_gateway_data gateway
, *gateway_ptr
;
64 /* only fetch the tvlv value if the handler wasn't called via the
65 * CIFNOTFND flag and if there is data to fetch
67 if (flags
& BATADV_TVLV_HANDLER_OGM_CIFNOTFND
||
68 tvlv_value_len
< sizeof(gateway
)) {
69 gateway
.bandwidth_down
= 0;
70 gateway
.bandwidth_up
= 0;
72 gateway_ptr
= tvlv_value
;
73 gateway
.bandwidth_down
= gateway_ptr
->bandwidth_down
;
74 gateway
.bandwidth_up
= gateway_ptr
->bandwidth_up
;
75 if (gateway
.bandwidth_down
== 0 ||
76 gateway
.bandwidth_up
== 0) {
77 gateway
.bandwidth_down
= 0;
78 gateway
.bandwidth_up
= 0;
82 batadv_gw_node_update(bat_priv
, orig
, &gateway
);
84 /* restart gateway selection */
85 if (gateway
.bandwidth_down
!= 0 &&
86 atomic_read(&bat_priv
->gw
.mode
) == BATADV_GW_MODE_CLIENT
)
87 batadv_gw_check_election(bat_priv
, orig
);
91 * batadv_gw_init() - initialise the gateway handling internals
92 * @bat_priv: the bat priv with all the soft interface information
94 void batadv_gw_init(struct batadv_priv
*bat_priv
)
96 if (bat_priv
->algo_ops
->gw
.init_sel_class
)
97 bat_priv
->algo_ops
->gw
.init_sel_class(bat_priv
);
99 atomic_set(&bat_priv
->gw
.sel_class
, 1);
101 batadv_tvlv_handler_register(bat_priv
, batadv_gw_tvlv_ogm_handler_v1
,
102 NULL
, NULL
, BATADV_TVLV_GW
, 1,
103 BATADV_TVLV_HANDLER_OGM_CIFNOTFND
);
107 * batadv_gw_free() - free the gateway handling internals
108 * @bat_priv: the bat priv with all the soft interface information
110 void batadv_gw_free(struct batadv_priv
*bat_priv
)
112 batadv_tvlv_container_unregister(bat_priv
, BATADV_TVLV_GW
, 1);
113 batadv_tvlv_handler_unregister(bat_priv
, BATADV_TVLV_GW
, 1);