* Handles incoming packets related to handshake */
(id int)
| 268 | /* Handles incoming packets related to handshake |
| 269 | */ |
| 270 | func (device *Device) RoutineHandshake(id int) { |
| 271 | defer func() { |
| 272 | device.Log.Verbosef("Routine: handshake worker %d - stopped", id) |
| 273 | device.queue.encryption.wg.Done() |
| 274 | }() |
| 275 | device.Log.Verbosef("Routine: handshake worker %d - started", id) |
| 276 | |
| 277 | for elem := range device.queue.handshake.c { |
| 278 | |
| 279 | // handle cookie fields and ratelimiting |
| 280 | |
| 281 | switch elem.msgType { |
| 282 | |
| 283 | case MessageCookieReplyType: |
| 284 | |
| 285 | // unmarshal packet |
| 286 | |
| 287 | var reply MessageCookieReply |
| 288 | err := reply.unmarshal(elem.packet) |
| 289 | if err != nil { |
| 290 | device.Log.Verbosef("Failed to decode cookie reply") |
| 291 | goto skip |
| 292 | } |
| 293 | |
| 294 | // lookup peer from index |
| 295 | |
| 296 | entry := device.indexTable.Lookup(reply.Receiver) |
| 297 | |
| 298 | if entry.peer == nil { |
| 299 | goto skip |
| 300 | } |
| 301 | |
| 302 | // consume reply |
| 303 | |
| 304 | if peer := entry.peer; peer.isRunning.Load() { |
| 305 | device.Log.Verbosef("Receiving cookie response from %s", elem.endpoint.DstToString()) |
| 306 | if !peer.cookieGenerator.ConsumeReply(&reply) { |
| 307 | device.Log.Verbosef("Could not decrypt invalid cookie response") |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | goto skip |
| 312 | |
| 313 | case MessageInitiationType, MessageResponseType: |
| 314 | |
| 315 | // check mac fields and maybe ratelimit |
| 316 | |
| 317 | if !device.cookieChecker.CheckMAC1(elem.packet) { |
| 318 | device.Log.Verbosef("Received packet with invalid mac1") |
| 319 | goto skip |
| 320 | } |
| 321 | |
| 322 | // endpoints destination address is the source of the datagram |
| 323 | |
| 324 | if device.IsUnderLoad() { |
| 325 | |
| 326 | // verify MAC2 field |
| 327 |
no test coverage detected