A Framework For Modeling and Verifying Iot Communication Protocols
A Framework For Modeling and Verifying Iot Communication Protocols
A Framework For Modeling and Verifying Iot Communication Protocols
1 Introduction
Most of the applications in IoT need a reliable network and use existing Internet
to communicate with the cloud/servers and with other nodes. Hence it is com-
mon to use the existing TCP/IP stack. The underlying physical, DLL, network
and transport layer of this backbone is used. However the TCP/IP stack has a
larger footprint and memory requirement. UDP, a lighter transport layer proto-
col is used in some cases where the reliability has to be built in the application
layer protocol. Other communication requirements of an IoT system which need
3
to be possessed by the application layer protocol are: low bandwidth, low mem-
ory consumption, small code foot-print, self recovery from loss of connection,
resource discovery, light-weight, low message overhead, low power consumption,
authentication, security requirements, appropriate Quality of Service(QoS).
Following is brief description of some of the application protocols highlighting
the features and properties which we verify in this paper.
2.1 MQTT
MQTT [9] is a publish-subscribe protocol designed for constrained devices con-
nected over unreliable, low bandwidth networks. It gives flexibility to connect
multiple servers to multiple Clients. The protocol has low message overhead
which makes it bandwidth efficient and can be easily implemented on a low
powered device. Significant features offered by MQTT are explained below:
1. QoS: A Quality of Services(QoS) for a message is decided between the sender
and the receiver. MQTT offers three levels of QoS: QoS0 is a fire and forget
kind of message delivery, where the sender sends a publish message only once
and does not wait for any acknowledgement. QoS1 guarantees delivery at
least once by seeking for an acknowledgement for every message published.
If the sender does not receive an acknowledgement within a defined time
due to loss of either publish or its acknowledgement message, it transmits
a duplicate publish message. QoS2 guarantees exactly once delivery where
neither loss nor duplication of any message is allowed. It is achieved by a
two step acknowledgement process.
2. Subscribe: A client can subscribe to a topic by sending subscribe message
with desired QoS and receiving an acknowledgement. Similarly it can unsub-
scribe from any topic. The QoS with which the data is received is minimum
of the sending and receiving QoS for that topic.
3. Keep-alive: To differentiate between connection drop and inactivity on a
channel, a fixed time called keep-alive is configured. If there is no exchange
of application messages for keep-alive time, then the client sends a ping
request and server has to acknowledge. On failure of reception of the ping
packets at server or client side, appropriate error handling is triggered.
4. Persistent Session: Persistent session can be maintained in lossy networks
with frequent disruption by storing the session state. If a client attempts
to reconnect as a persistent session, all the previous configurations of the
session are restored including any old subscriptions and keep-alive informa-
tion. Upon re-connection, the client and server resume communication by
first sending packets for all unacknowledged messages, thus guaranteeing
required QoS.
5. Retain Message: A publish message marked as ”retain” is stored by the
server. If any subscriber goes offline and reconnects, then all the retained
messages are sent to the subscriber upon re-connection with required QoS.
In case of a new subscription, if a retained message for a particular topic is
already stored, the server sends this information to the subscriber immedi-
ately after the subscription request is sent.
4
6. Will Message: With ”will” message, a client can inform all other subscribing
clients that it is offline or transmit any other message to its subscribers after
loss of connection. When connection between publisher and server is lost,
the server transmits the will message to all the clients which subscribe to
any topic being published by the publisher.
7. Authentication: MQTT also offers user-password feature for authentication.
Implementations can use these fields or provide any other external authen-
tication mechanisms. The applications can also use TLS for data encryp-
tion [9].
2.2 MQTT-SN
MQTT-SN is another data centric protocol and is based on MQTT with adapta-
tions to suit the wireless communication environment. Unlike MQTT, MQTT-SN
does not require an underlying network like TCP/IP making it a low complex-
ity, light weight protocol. Some of the significant differences between MQTT and
MQTT-SN include
2.3 CoAP
CoAP is a specialized web transfer protocol based on REST architecture, fulfill-
ing Machine to Machine(M2M) requirements in constrained environments. CoAP
has low header overhead, parsing complexity, and has uri based addressing. Like
HTTP it has request-response model and it also supports publish-subscribe ar-
chitecture in extended mode. It is stateless HTTP mapping, allowing proxies to
be built providing access to CoAP resources via HTTP. Following are significant
features of CoAP:
1. Layered Architecture: CoAP implements a request-response model with asyn-
chronous message exchanges at lower layer. In the two layer approach, CoAP’s
messaging layer is used to deal with UDP and the asynchronous nature of the
interactions, and the request-response interactions on top of it for method
and response codes. CoAP defines four types of messages: confirmable, non-
confirmable, acknowledgement, reset. The basic exchanges of the four types
of messages are somewhat orthogonal to the request-response interactions.
A Request can be carried in confirmable and non- confirmable message, and
a response can be carried in these as well or as piggybacked in acknowledge-
ment messages.
2. Message Correlation and Deduplication: A message and its acknowledgement
are correlated through a message ID and request-response are correlated
through token numbers. A recipient of a message can deduplicate a message
by using the message ID and should process any request only once.
3. Unicast/multicast requests: CoAP can communicate in both unicast and
multicast modes. For discovering resources and services in the network,
CoAP uses multicast request. After a connection is established with a server,
unicast mode is used. A multicast request is characterized by being trans-
ported in a CoAP non-confirmable message that is addressed to an IP mul-
ticast address instead of a CoAP endpoint.
4. Reliability: CoAP uses UDP for transport layer which by default does not
support reliability mechanisms. CoAP uses a layer of messages that supports
optional reliability. A QoS of ”at least once” is supported in CoAP. To avoid
congestion in network, CoAP end point re-transmits with an exponential
back-off mechanism.
5. Proxying and Caching:The protocol supports caching of responses in order
to efficiently fulfil requests. Caching helps to limit network traffic, improve
performance, help sleeping devices and also for security. Simple caching is en-
abled using freshness and validity information carried with CoAP responses.
A cache could be located in an endpoint or an intermediary called proxy.
Freshness and Validation model: A max-age option in a response indicates
that the response is to be considered not fresh after its age is greater than
the specified time. To avoid caching a server can set max-age option to 0. A
proxy can validate a stored response after its max-age is over by communi-
cating with the server to update the ”freshness” and reuse the response
6. Resource Discovery: Like MQTT-SN, CoAP uses multicast requests to dis-
cover services and resources in the network. A CoAP node an send a multi-
6
3 Event-B
Rodin
ProB
The common abstract model implements the commonalities among various pro-
tocols as mentioned in Table 1. Figure 2 is a diagrammatic representation of the
abstract model.
9
Machine
Transmit/Receive Messages, Error Event
Events in the Machine
Attacker, Time Tracking
Communication
Channel
1st Refinement
MultiCast/ UniCast
BroadCast
2nd Refinement
Channel
Conversation
3rd Refinement
4th Refinement
Message Message Timer Timeout
Send Acknowlege Increment Detection
From the common abstract model, the decomposed machines are refined further
to add details specific to a protocol. Some of the features which are not used
in the protocol need not be used or refined. For example there is no broadcast
or multicast support in MQTT protocol. Hence this module does not need any
refinement in MQTT model. The contexts from the abstract model are extended
to add detailed attributes. Channel variables and internal buffers are introduced
to track the dynamic behaviour of the channel that include messages in channel,
topics subscribed, payload counters, send and receive buffers, timers, configura-
tion settings, etc. Following is a detailed description of MQTT protocol model
created from the abstract model. We then briefly describe the other two protocol
models (MQTT-SN and CoAP) which follow similar procedures.
Publish
QoS0
1st Refinement
Message Message
Send Receive
2nd Refinement
Client Server Client Server
Send Send Receive Receive
Message
Send
1st Refinement
2nd Refinement
Publish
QoS2 Orig
Publish
QoS2 Dup
Publish
QoS2 Rcv
Publish
QoS2 Orig
. . . . Publish Qos2
Release Rcv
Subscribe
Send
Subscribe
Receive
3rd Refinement
. . . Publish
QoS2 Rcv
Publish
QoS2 Rcv . . . . . . . . . . .
Client Server
To track if the correct message is delivered with required QoS and time,
the ”Payload” is implemented as a counter with a range of 0 to 9. The range
of the counter can be extended to any number without affecting our model.
This method of using counter as payload allows us to uniquely identify every
message transmitted. By keeping a track of how many times the message with
a given payload value is received, we can verify interesting properties related to
QoS, message ordering, retained message and persistent sessions. Figure 7 is an
example of the QoS0 Publish event transmitted by an MQTT client. The guards
ensure that a message of type publish with QoS0 is transmitted on the channel
which is already established. In the actions, the channel is populated with a
new message carrying unique payload, client timer is initialized, direction of
the message is set, PayloadCounter is incremented and Timer-increment event
is triggered.
ProB is used for validating our model through simulation of events and check-
ing LTL properties for common abstract model. Accuracy of the model can be
obtained by executing different runs and observing the sequence of events and
variable values in each of these events. ProB also reports any invariant violation
or error in events which is then corrected in the model. Model validation is also
done by writing invariants and seeing that these invariants are satisfied through
the refinements.
15
Following are some of the significant properties that are verified through the
model by writing them as invariants that have to be satisfied for all the events
in protocol specific models. Properties 1 to 7 are verified in MQTT and MQTT-
SN models and 8 to 11 are verified in CoAP model.
A property required to be satisfied by the model has been expressed as an
invariant. The property invariant contains two parts : Well-definedness expres-
sions and the actual property to be proved. For example, in the invariant for
Duplicate Channel Property, for every quantified variable, there is a range de-
fined to which it belongs: ∀chnl · (chnl ∈ Server establishedChannel), followed
by well-definedness conditions for the projection function:
chnl ∈ dom(Channel Server) and the actual property to be verified is written at
the end of the equation: (Channel ClientID(ch) = Channel ClientID(chnl) ⇒
(ch = chnl). We omit the well-definedness conditions and state only the actual
property to be proved.
For every property listed, the property is stated in English language as writ-
ten in the protocol specification followed by the property written in our Event-B
model. The variable names given in our model are self explanatory and are not
16
2. Timers validity: The time tracker variable is always greater than or equal to
any local time counter.
∀pc·((pc1 ∈ 0 · ·9
(4)
⇒ (time ≥ RcvT Range(pc1) ∧ (time ≥ SendT Range(pc1)))
3. Message Ordering: If both client and server make sure that no more than
one message is ”in-flight” at any one time (by not sending a message until its
predecessor has been acknowledged), then no QoS1 message will be received
after any later one. For example a subscriber might receive them in the
order 1, 2, 3, 3, 4 but not 1, 2, 3, 2, 3, 4. Setting an in-flight window of 1 also
means that order will be preserved even if the publisher sends a sequence
of messages with different QoS levels on the same topic. Refer to Sec. 4.6
in [9].
∀ch·∀pc1 · ∀pc2 · ((pc1 ∈ 0 · ·9 ∧ pc2 ∈ 0 · ·9 ∧ ch ∈ establishChannel
∧ (pc1 ∈ Client MsgSentQoS2 (ch) ∨ pc1 ∈ Client MsgSentQoS1 (ch))
∧ (pc2 ∈ Client MsgSentQoS2 (ch) ∨ pc2 ∈ Client MsgSentQoS1 (ch))
(5)
∧ (time > SendT Range(pc2) + Response Timeout)
∧ pc1 6= pc2 ∧ (SendT Range(pc1) < SendT Range(pc2))
⇒ (RcvT Range(pc1) ≤ RcvT Range(pc2))
– QoS2: Publish from client to server with QoS = 2. If client sends a packet
with QoS = 2 to the server, exactly one copy of the packet should be received
at the server side even in case of loss or duplicate transmission of messages.
This is the highest quality of service and increases overhead. Refer to Sec.
4.3.3 in [9].
∀ch · ∀pc·((pc ∈ 0 · ·9 ∧ ch ∈ establishChannel
∧ (pc ∈ Client MsgSentQoS2 (ch))
(8)
∧ (time > (SendT Range(pc) + Response Timeout)))
⇒ (Server MsgReceived 2 (pc) = 1))
7. QoS of a message from Client1 to Client2: The effective QoS of any message
received by the subscriber depends on the QoS with which the publishing
client transmits this message and the QoS set by the subscriber while sub-
scribing for the given topic. The effective QoS with which message is delivered
18
is the minimum of the two QoS. Below is an example of the message which
is transmitted with QoS1 but was subscribed with QoS2. In this case the
message is received by the subscriber with the effective QoS of 1. The below
property states, if a message which is transmitted from Client1 on channel
ch with QoS1 (ATLEASTONCE) and if the cleint2 has subscribed to this
topic on channel chnl with QoS2 (EAXACTONCE) then Client2 will receive
this message from the Server1 on channel chnl within a configured amount
of time, at least once. Refer to Sec. 4.3 in [9].
Similarly all combinations of the QoS at publisher and subscriber end have
been verified.
Our validated models of MQTT, MQTT-SN and CoAP have together discharged
1840 proof obligations, of which 88% proof obligations were automatically dis-
charged through AtlierB, SMT, PP and ML provers. The proof obligations in-
clude well-definedness of predicates and expressions in invariants, guards, ac-
tions, variant and witnesses of all the events, feasibility checks, variable re-
use check, guard strengthening and witness feasibility in refinements, variant
checks for natural number and decreasing variants for convergent and antici-
pated events, theorems in axioms and invariant preservation for refinements and
invariants used for verification of required properties. About 30% of proofs dis-
charged in the models are for verification of properties written as invariants.
Table 2 gives a summary of the properties verified.
6 Related Work
Communication protocols for IoT have been used for over a decade now, but
there has been no attempt to provide formal semantics for these protocols. A
recent paper shows that there are scenarios where MQTT has failed to adhere
to the QoS requirement [16]. However the paper is limited to partial model of
MQTT protocol for QoS properties. In another work, a protocol used for IoT
- Zigbee is verified for properties related to connection establishment proper-
ties [17] using Event-B. In [19] and [20], the authors give methods to evaluate
performance of MQTT protocol with regards to different QoS levels used and
compare with other IoT protocol CoAP. In [18] the author again tests connection
properties using passive testing for XMPP protocol in IoT.
We differ from the above mentioned approaches by proposing a framework
comprising of a common model for IoT protocols which can be used to build
models of different IoT protocols. These models verify properties required for
IoT like connection establishment, persistent sessions, retained-message trans-
mission, will messages, message ordering, proxying, caching and QoS and provide
proof obligations for these properties through automatic proof discharge and in-
teractive proof discharge methods.
In this paper we have proposed a framework using Event-B to model IoT pro-
tocols. We then have used this framework and went on to model some of the
widely used IoT protocols viz., MQTT, MQTT-SN and CoAP. Through simula-
tion and proof obligation discharge in Rodin, we have formally verified that the
properties related to QoS, persistent session, will, retain messages, resource dis-
covery, two layered request-response architecture, caching, proxying and message
deduplication. We show that the protocols work as intended in an uninterrupted
network as well as with an intruder which consumes messages in the network.
The three protocols modeled in this paper implement simple mechanisms to
provide reliable message transfer over a lossy network. They are also able to re-
duce overhead by providing features like persistent connections, retain messages,
caching and proxying which are essential for IoT systems. Our work is a stepping
stone towards providing formal semantics of IoT protocols and behaviour of IoT
systems.
Future research would focus on modeling the other aspects of protocols like
security, user authentication, encryption and different attacker modules. We
would also like to move verification of more properties from the concrete pro-
tocol models to the common abstract model. We would like to further compare
other protocols for IoT like AMQP and XMPP by modeling them using our
framework. It would also be interesting to integrate the protocol model into an
existing model of IoT system and verify the properties required at the system
level.
21
References
1. Event-B, http://www.Event-B.org/
2. Abrial J.R., Modeling in Event-B: System and Software Engineering, Cambridge
University Press(2010)
3. Evans, N., Butler, M.: A proposal for records in Event-B. In: International Sympo-
sium on Formal Methods, pp. 221-235. Springer, Berlin Heidelberg (2006)
4. Rodin Tool, http://wiki.Event-B.org/index.php/Rodin Platform
5. Rodin Hand Book, https://www3.hhu.de/stups/handbook/rodin/current/pdf/rodin-
doc.pdf
6. ProB Tool, https://www3.hhu.de/stups/prob/index.php/Main Page
7. Gartner newsroom, http://www.gartner.com/newsroom/id/3165317
8. Karagiannis, V., Chatzimisios, P., Vazquez-Gallego, F., Alonso-Zarate J.: A survey
on application layer protocols for the internet of things. In: Transaction on IoT and
Cloud Computing, 3(1):11-7 (2015)
9. MQTT Version 3.1.1 Specification, http://docs.oasis-
open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html
10. MQTT-SN Version 1.2 Specification, http://mqtt.org/new/wp-
content/uploads/2009/06/MQTT-SN spec v1.2.pdf
11. The Constrained Application Protocol (CoAP) Specification RFC7252,
https://tools.ietf.org/html/rfc7252
12. Extensible Messaging and Presence Protocol (XMPP) Core RFC6120,
http://xmpp.org/rfcs/rfc6120.html
13. Advanced Messaging Queuing Protocol Version 1.0 Specification,
http://docs.oasis-open.org/amqp/core/v1.0/amqp-core-complete-v1.0.pdf
14. Pascal, C., and Renato, S.: Event-B model decomposition, DEPLOY Plenary Tech-
nical Workshop (2009)
15. Salehi Fathabadi A., Butler M., Rezazadeh A.: A Systematic Approach to Atom-
icity Decomposition in Event-B. In: 10th International Conference, SEFM. Thessa-
loniki, Greece (2012)
16. Aziz, B.: A Formal Model and Analysis of the MQ Telemetry Transport Protocol.
In: Ninth International Conference, Availability, Reliability and Security (ARES),
pp. 59-68. Fribourg (2014)
17. Gawanmeh, A.: Embedding and Verification of ZigBee Protocol Stack in Event-B.
In: Procedia Computer Science, Volume 5, pp. 736-741. ISSN 1877-0509, (2011)
18. Che, X., Maag, S.: A Passive Testing Approach for Protocols in Internet of Things.
In: Green Computing and Communications (GreenCom), IEEE and Internet of
Things (iThings/CPSCom), IEEE International Conference on and IEEE Cyber,
Physical and Social Computing, pp. 678-684. IEEE Press (2013)
19. Lee, S., Kim, H., Hong, D. K., Ju, H.: Correlation analysis of MQTT loss and delay
according to QoS level. In: The International Conference on Information Network-
ing(ICOIN), pp. 714-717. IEEE (2013)
20. Thangavel, D., Ma, X., Valera, A., Tan, H.X., Tan, C.K.: Performance evaluation
of MQTT and CoAP via a common middleware. In: IEEE Ninth International Con-
ference, Intelligent Sensors, Sensor Networks and Information Processing (ISSNIP),
pp. 1-6. IEEE Press (2014)