Skip to content

Commit c82910e

Browse files
Jon Paul Maloydavem330
authored andcommitted
tipc: clean up neigbor discovery message reception
The function tipc_disc_rcv(), which is handling received neighbor discovery messages, is perceived as messy, and it is hard to verify its correctness by code inspection. The fact that the task it is set to resolve is fairly complex does not make the situation better. In this commit we try to take a more systematic approach to the problem. We define a decision machine which takes three state flags as input, and produces three action flags as output. We then walk through all permutations of the state flags, and for each of them we describe verbally what is going on, plus that we set zero or more of the action flags. The action flags indicate what should be done once the decision machine has finished its job, while the last part of the function deals with performing those actions. Signed-off-by: Jon Maloy <jon.maloy@ericsson.com> Reviewed-by: Ying Xue <ying.xue@windriver.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 38504c2 commit c82910e

1 file changed

Lines changed: 111 additions & 108 deletions

File tree

‎net/tipc/discover.c‎

Lines changed: 111 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* net/tipc/discover.c
33
*
4-
* Copyright (c) 2003-2006, Ericsson AB
4+
* Copyright (c) 2003-2006, 2014, Ericsson AB
55
* Copyright (c) 2005-2006, 2010-2011, Wind River Systems
66
* All rights reserved.
77
*
@@ -106,147 +106,150 @@ static void disc_dupl_alert(struct tipc_bearer *b_ptr, u32 node_addr,
106106
}
107107

108108
/**
109-
* tipc_disc_rcv - handle incoming link setup message (request or response)
109+
* tipc_disc_rcv - handle incoming discovery message (request or response)
110110
* @buf: buffer containing message
111-
* @b_ptr: bearer that message arrived on
111+
* @bearer: bearer that message arrived on
112112
*/
113-
void tipc_disc_rcv(struct sk_buff *buf, struct tipc_bearer *b_ptr)
113+
void tipc_disc_rcv(struct sk_buff *buf, struct tipc_bearer *bearer)
114114
{
115-
struct tipc_node *n_ptr;
115+
struct tipc_node *node;
116116
struct tipc_link *link;
117-
struct tipc_media_addr media_addr;
117+
struct tipc_media_addr maddr;
118118
struct sk_buff *rbuf;
119119
struct tipc_msg *msg = buf_msg(buf);
120-
u32 dest = msg_dest_domain(msg);
121-
u32 orig = msg_prevnode(msg);
120+
u32 ddom = msg_dest_domain(msg);
121+
u32 onode = msg_prevnode(msg);
122122
u32 net_id = msg_bc_netid(msg);
123-
u32 type = msg_type(msg);
123+
u32 mtyp = msg_type(msg);
124124
u32 signature = msg_node_sig(msg);
125-
int addr_mismatch;
126-
int link_fully_up;
127-
128-
media_addr.broadcast = 1;
129-
b_ptr->media->msg2addr(b_ptr, &media_addr, msg_media_addr(msg));
125+
bool addr_match = false;
126+
bool sign_match = false;
127+
bool link_up = false;
128+
bool accept_addr = false;
129+
bool accept_sign = false;
130+
bool respond = false;
131+
132+
bearer->media->msg2addr(bearer, &maddr, msg_media_addr(msg));
130133
kfree_skb(buf);
131134

132135
/* Ensure message from node is valid and communication is permitted */
133136
if (net_id != tipc_net_id)
134137
return;
135-
if (media_addr.broadcast)
138+
if (maddr.broadcast)
136139
return;
137-
if (!tipc_addr_domain_valid(dest))
140+
if (!tipc_addr_domain_valid(ddom))
138141
return;
139-
if (!tipc_addr_node_valid(orig))
142+
if (!tipc_addr_node_valid(onode))
140143
return;
141-
if (orig == tipc_own_addr) {
142-
if (memcmp(&media_addr, &b_ptr->addr, sizeof(media_addr)))
143-
disc_dupl_alert(b_ptr, tipc_own_addr, &media_addr);
144+
145+
if (in_own_node(onode)) {
146+
if (memcmp(&maddr, &bearer->addr, sizeof(maddr)))
147+
disc_dupl_alert(bearer, tipc_own_addr, &maddr);
144148
return;
145149
}
146-
if (!tipc_in_scope(dest, tipc_own_addr))
150+
if (!tipc_in_scope(ddom, tipc_own_addr))
147151
return;
148-
if (!tipc_in_scope(b_ptr->domain, orig))
152+
if (!tipc_in_scope(bearer->domain, onode))
149153
return;
150154

151-
/* Locate structure corresponding to requesting node */
152-
n_ptr = tipc_node_find(orig);
153-
if (!n_ptr) {
154-
n_ptr = tipc_node_create(orig);
155-
if (!n_ptr)
156-
return;
157-
}
158-
tipc_node_lock(n_ptr);
155+
/* Locate, or if necessary, create, node: */
156+
node = tipc_node_find(onode);
157+
if (!node)
158+
node = tipc_node_create(onode);
159+
if (!node)
160+
return;
159161

160-
/* Prepare to validate requesting node's signature and media address */
161-
link = n_ptr->links[b_ptr->identity];
162-
addr_mismatch = (link != NULL) &&
163-
memcmp(&link->media_addr, &media_addr, sizeof(media_addr));
162+
tipc_node_lock(node);
163+
link = node->links[bearer->identity];
164164

165-
/*
166-
* Ensure discovery message's signature is correct
167-
*
168-
* If signature is incorrect and there is no working link to the node,
169-
* accept the new signature but invalidate all existing links to the
170-
* node so they won't re-activate without a new discovery message.
171-
*
172-
* If signature is incorrect and the requested link to the node is
173-
* working, accept the new signature. (This is an instance of delayed
174-
* rediscovery, where a link endpoint was able to re-establish contact
175-
* with its peer endpoint on a node that rebooted before receiving a
176-
* discovery message from that node.)
177-
*
178-
* If signature is incorrect and there is a working link to the node
179-
* that is not the requested link, reject the request (must be from
180-
* a duplicate node).
181-
*/
182-
if (signature != n_ptr->signature) {
183-
if (n_ptr->working_links == 0) {
184-
struct tipc_link *curr_link;
185-
int i;
186-
187-
for (i = 0; i < MAX_BEARERS; i++) {
188-
curr_link = n_ptr->links[i];
189-
if (curr_link) {
190-
memset(&curr_link->media_addr, 0,
191-
sizeof(media_addr));
192-
tipc_link_reset(curr_link);
193-
}
194-
}
195-
addr_mismatch = (link != NULL);
196-
} else if (tipc_link_is_up(link) && !addr_mismatch) {
197-
/* delayed rediscovery */
198-
} else {
199-
disc_dupl_alert(b_ptr, orig, &media_addr);
200-
tipc_node_unlock(n_ptr);
201-
return;
202-
}
203-
n_ptr->signature = signature;
165+
/* Prepare to validate requesting node's signature and media address */
166+
sign_match = (signature == node->signature);
167+
addr_match = link && !memcmp(&link->media_addr, &maddr, sizeof(maddr));
168+
link_up = link && tipc_link_is_up(link);
169+
170+
171+
/* These three flags give us eight permutations: */
172+
173+
if (sign_match && addr_match && link_up) {
174+
/* All is fine. Do nothing. */
175+
} else if (sign_match && addr_match && !link_up) {
176+
/* Respond. The link will come up in due time */
177+
respond = true;
178+
} else if (sign_match && !addr_match && link_up) {
179+
/* Peer has changed i/f address without rebooting.
180+
* If so, the link will reset soon, and the next
181+
* discovery will be accepted. So we can ignore it.
182+
* It may also be an cloned or malicious peer having
183+
* chosen the same node address and signature as an
184+
* existing one.
185+
* Ignore requests until the link goes down, if ever.
186+
*/
187+
disc_dupl_alert(bearer, onode, &maddr);
188+
} else if (sign_match && !addr_match && !link_up) {
189+
/* Peer link has changed i/f address without rebooting.
190+
* It may also be a cloned or malicious peer; we can't
191+
* distinguish between the two.
192+
* The signature is correct, so we must accept.
193+
*/
194+
accept_addr = true;
195+
respond = true;
196+
} else if (!sign_match && addr_match && link_up) {
197+
/* Peer node rebooted. Two possibilities:
198+
* - Delayed re-discovery; this link endpoint has already
199+
* reset and re-established contact with the peer, before
200+
* receiving a discovery message from that node.
201+
* (The peer happened to receive one from this node first).
202+
* - The peer came back so fast that our side has not
203+
* discovered it yet. Probing from this side will soon
204+
* reset the link, since there can be no working link
205+
* endpoint at the peer end, and the link will re-establish.
206+
* Accept the signature, since it comes from a known peer.
207+
*/
208+
accept_sign = true;
209+
} else if (!sign_match && addr_match && !link_up) {
210+
/* The peer node has rebooted.
211+
* Accept signature, since it is a known peer.
212+
*/
213+
accept_sign = true;
214+
respond = true;
215+
} else if (!sign_match && !addr_match && link_up) {
216+
/* Peer rebooted with new address, or a new/duplicate peer.
217+
* Ignore until the link goes down, if ever.
218+
*/
219+
disc_dupl_alert(bearer, onode, &maddr);
220+
} else if (!sign_match && !addr_match && !link_up) {
221+
/* Peer rebooted with new address, or it is a new peer.
222+
* Accept signature and address.
223+
*/
224+
accept_sign = true;
225+
accept_addr = true;
226+
respond = true;
204227
}
205228

206-
/*
207-
* Ensure requesting node's media address is correct
208-
*
209-
* If media address doesn't match and the link is working, reject the
210-
* request (must be from a duplicate node).
211-
*
212-
* If media address doesn't match and the link is not working, accept
213-
* the new media address and reset the link to ensure it starts up
214-
* cleanly.
215-
*/
216-
if (addr_mismatch) {
217-
if (tipc_link_is_up(link)) {
218-
disc_dupl_alert(b_ptr, orig, &media_addr);
219-
tipc_node_unlock(n_ptr);
220-
return;
221-
} else {
222-
memcpy(&link->media_addr, &media_addr,
223-
sizeof(media_addr));
224-
tipc_link_reset(link);
225-
}
226-
}
229+
if (accept_sign)
230+
node->signature = signature;
227231

228-
/* Create a link endpoint for this bearer, if necessary */
229-
if (!link) {
230-
link = tipc_link_create(n_ptr, b_ptr, &media_addr);
231-
if (!link) {
232-
tipc_node_unlock(n_ptr);
233-
return;
232+
if (accept_addr) {
233+
if (!link)
234+
link = tipc_link_create(node, bearer, &maddr);
235+
if (link) {
236+
memcpy(&link->media_addr, &maddr, sizeof(maddr));
237+
tipc_link_reset(link);
238+
} else {
239+
respond = false;
234240
}
235241
}
236242

237-
/* Accept discovery message & send response, if necessary */
238-
link_fully_up = link_working_working(link);
239-
240-
if ((type == DSC_REQ_MSG) && !link_fully_up) {
243+
/* Send response, if necessary */
244+
if (respond && (mtyp == DSC_REQ_MSG)) {
241245
rbuf = tipc_buf_acquire(INT_H_SIZE);
242246
if (rbuf) {
243-
tipc_disc_init_msg(rbuf, DSC_RESP_MSG, b_ptr);
244-
tipc_bearer_send(b_ptr->identity, rbuf, &media_addr);
247+
tipc_disc_init_msg(rbuf, DSC_RESP_MSG, bearer);
248+
tipc_bearer_send(bearer->identity, rbuf, &maddr);
245249
kfree_skb(rbuf);
246250
}
247251
}
248-
249-
tipc_node_unlock(n_ptr);
252+
tipc_node_unlock(node);
250253
}
251254

252255
/**

0 commit comments

Comments
 (0)