18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/******************************************************************************
38c2ecf20Sopenharmony_ci*******************************************************************************
48c2ecf20Sopenharmony_ci**
58c2ecf20Sopenharmony_ci**  Copyright (C) Sistina Software, Inc.  1997-2003  All rights reserved.
68c2ecf20Sopenharmony_ci**  Copyright (C) 2004-2008 Red Hat, Inc.  All rights reserved.
78c2ecf20Sopenharmony_ci**
88c2ecf20Sopenharmony_ci**
98c2ecf20Sopenharmony_ci*******************************************************************************
108c2ecf20Sopenharmony_ci******************************************************************************/
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci/*
138c2ecf20Sopenharmony_ci * midcomms.c
148c2ecf20Sopenharmony_ci *
158c2ecf20Sopenharmony_ci * This is the appallingly named "mid-level" comms layer.
168c2ecf20Sopenharmony_ci *
178c2ecf20Sopenharmony_ci * Its purpose is to take packets from the "real" comms layer,
188c2ecf20Sopenharmony_ci * split them up into packets and pass them to the interested
198c2ecf20Sopenharmony_ci * part of the locking mechanism.
208c2ecf20Sopenharmony_ci *
218c2ecf20Sopenharmony_ci * It also takes messages from the locking layer, formats them
228c2ecf20Sopenharmony_ci * into packets and sends them to the comms layer.
238c2ecf20Sopenharmony_ci */
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci#include <asm/unaligned.h>
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci#include "dlm_internal.h"
288c2ecf20Sopenharmony_ci#include "lowcomms.h"
298c2ecf20Sopenharmony_ci#include "config.h"
308c2ecf20Sopenharmony_ci#include "lock.h"
318c2ecf20Sopenharmony_ci#include "midcomms.h"
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci/*
348c2ecf20Sopenharmony_ci * Called from the low-level comms layer to process a buffer of
358c2ecf20Sopenharmony_ci * commands.
368c2ecf20Sopenharmony_ci */
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ciint dlm_process_incoming_buffer(int nodeid, unsigned char *buf, int len)
398c2ecf20Sopenharmony_ci{
408c2ecf20Sopenharmony_ci	const unsigned char *ptr = buf;
418c2ecf20Sopenharmony_ci	const struct dlm_header *hd;
428c2ecf20Sopenharmony_ci	uint16_t msglen;
438c2ecf20Sopenharmony_ci	int ret = 0;
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci	while (len >= sizeof(struct dlm_header)) {
468c2ecf20Sopenharmony_ci		hd = (struct dlm_header *)ptr;
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci		/* no message should be more than this otherwise we
498c2ecf20Sopenharmony_ci		 * cannot deliver this message to upper layers
508c2ecf20Sopenharmony_ci		 */
518c2ecf20Sopenharmony_ci		msglen = get_unaligned_le16(&hd->h_length);
528c2ecf20Sopenharmony_ci		if (msglen > DEFAULT_BUFFER_SIZE ||
538c2ecf20Sopenharmony_ci		    msglen < sizeof(struct dlm_header)) {
548c2ecf20Sopenharmony_ci			log_print("received invalid length header: %u from node %d, will abort message parsing",
558c2ecf20Sopenharmony_ci				  msglen, nodeid);
568c2ecf20Sopenharmony_ci			return -EBADMSG;
578c2ecf20Sopenharmony_ci		}
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci		/* caller will take care that leftover
608c2ecf20Sopenharmony_ci		 * will be parsed next call with more data
618c2ecf20Sopenharmony_ci		 */
628c2ecf20Sopenharmony_ci		if (msglen > len)
638c2ecf20Sopenharmony_ci			break;
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci		switch (hd->h_cmd) {
668c2ecf20Sopenharmony_ci		case DLM_MSG:
678c2ecf20Sopenharmony_ci			if (msglen < sizeof(struct dlm_message)) {
688c2ecf20Sopenharmony_ci				log_print("dlm msg too small: %u, will skip this message",
698c2ecf20Sopenharmony_ci					  msglen);
708c2ecf20Sopenharmony_ci				goto skip;
718c2ecf20Sopenharmony_ci			}
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci			break;
748c2ecf20Sopenharmony_ci		case DLM_RCOM:
758c2ecf20Sopenharmony_ci			if (msglen < sizeof(struct dlm_rcom)) {
768c2ecf20Sopenharmony_ci				log_print("dlm rcom msg too small: %u, will skip this message",
778c2ecf20Sopenharmony_ci					  msglen);
788c2ecf20Sopenharmony_ci				goto skip;
798c2ecf20Sopenharmony_ci			}
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci			break;
828c2ecf20Sopenharmony_ci		default:
838c2ecf20Sopenharmony_ci			log_print("unsupported h_cmd received: %u, will skip this message",
848c2ecf20Sopenharmony_ci				  hd->h_cmd);
858c2ecf20Sopenharmony_ci			goto skip;
868c2ecf20Sopenharmony_ci		}
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci		/* for aligned memory access, we just copy current message
898c2ecf20Sopenharmony_ci		 * to begin of the buffer which contains already parsed buffer
908c2ecf20Sopenharmony_ci		 * data and should provide align access for upper layers
918c2ecf20Sopenharmony_ci		 * because the start address of the buffer has a aligned
928c2ecf20Sopenharmony_ci		 * address. This memmove can be removed when the upperlayer
938c2ecf20Sopenharmony_ci		 * is capable of unaligned memory access.
948c2ecf20Sopenharmony_ci		 */
958c2ecf20Sopenharmony_ci		memmove(buf, ptr, msglen);
968c2ecf20Sopenharmony_ci		dlm_receive_buffer((union dlm_packet *)buf, nodeid);
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ciskip:
998c2ecf20Sopenharmony_ci		ret += msglen;
1008c2ecf20Sopenharmony_ci		len -= msglen;
1018c2ecf20Sopenharmony_ci		ptr += msglen;
1028c2ecf20Sopenharmony_ci	}
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	return ret;
1058c2ecf20Sopenharmony_ci}
1068c2ecf20Sopenharmony_ci
107