\section{Firewall} \subsection{Software Firewall} A software firewall is inspecting data that goes in and out of the device. It has to be installed on each device in the network. Therefore, can only protect one device at a time. Looking at already existing solutions for linux and other operating systems, rules and settings can be identified that need to be implemented for this firewall. \subsection{UFW (Uncomplicated Firewall)} To see how a firewall works, UFW was analyzed. A look at the table provides following information: \begin{figure}[H] \begin{center} \includegraphics[width=0.6\textwidth]{ufw} \caption{UFW} \label{fig:UFW} \end{center} \end{figure} A destination port on the device, the action of the firewall and the IP-Address from where the request originated can be setup. Also the protocol that the rule applies to, can be chosen with TCP or UDP. \newpage \subsection{Parameter} After analyzing existing solutions following firewall parameters were implemented: \lstset{style=c++} \begin{lstlisting} typedef enum firewall_targets : uint8_t { TARGET_REJECT = 0, TARGET_DROP = 1, TARGET_ACCEPT = 2, } firewall_target_t; typedef enum firewall_protocols : uint8_t { PROTOCOL_TCP = 0, PROTOCOL_UDP = 1, PROTOCOL_ALL = 255, } firewall_protocol_t; static const uint8_t IPV4ADDRESS_LENGTH = 16; typedef struct firewall_rules { uint8_t key; char ip[IPV4ADDRESS_LENGTH]; uint32_t port_from; uint32_t port_to; firewall_protocol_t protocol; firewall_target_t target; struct firewall_rules *next; } firewall_rule_t; \end{lstlisting} A port can be a maximum of 65565, therefore cannot be of type \verb|uint16_t| but \verb|uint32_t|. Target as well as protocol are enums for the available options. To block a range of ports, there is a \verb|port_from| and \verb|port_to|. The firewall will store all the rules as linked list to dynamically add and remove rules. \newpage \subsection{IwIP} lwIP is a small independent implementation of the TCP/IP protocol suite and is used in the esp-core for network communication. This is the place the firewall need to check for the incoming traffic in order to drop, reject or pass packets based on the rules. \cite[cf.][]{lwip} \subsubsection{Analysing} First step is to analyze the code to find out where the packets are getting handled. Looking in \verb|~/.platformio/packages/framework-espidf/components/lwip/lwip/src/core/ipv4| there is a function: \verb|err_t ip4_input(struct pbuf *p, struct netif *inp)| to consume all incoming packages. Simply placing a logger can quickly show that this is the place to put the firewall filter. \begin{verbatim} if (ip4_addr4_16_val(iphdr->src) == 211) { ESP_LOGI("PROTO", "%3" U16_F, (u16_t)IPH_PROTO(iphdr)); ESP_LOGI("IP", "% " U16_F "% " U16_F "% " U16_F "% " U16_F, ip4_addr1_16_val(iphdr->src), ip4_addr2_16_val(iphdr->src), ip4_addr3_16_val(iphdr->src), ip4_addr4_16_val(iphdr->src)); } \end{verbatim} Following output can be seen when sending a ping from the machine with IP-Address \verb|10.93.0.211| to the esp. ICMP is therefore marked with protocol 1. \begin{verbatim} I (x) PROTO: 1 I (x) IP: 10 93 0 211 \end{verbatim} Sending a UDP or TCP package to the ESP (IP-Address: \verb|10.93.0.246|) can be done by executing the python code in the repository. \begin{verbatim} python3 tester.py -i 10.93.0.246 -p 80 -t TCP python3 tester.py -i 10.93.0.246 -p 22 -t UPD \end{verbatim} Following output can be registered. \begin{verbatim} I (x) PROTO: 6 I (x) IP: 10 93 0 211 I (x) PROTO: 17 I (x) IP: 10 93 0 211 \end{verbatim} Looking at the printed protocols this means 6 == "TCP" and 17 == "UDP". \subsubsection{Using Hook} After learning the protocols that need to be filtered and looked out for, a hook needs to be registered in order to filter the packets based on our rules. To register a hook \verb|LWIP_HOOK_IP4_INPUT| needs to be set as \verb|build_flag| in the \verb|platformio.ini| file to overwrite it in LwIP. This is an easy way of testing if it works as expected, but should be written in a function for any other use-case. \begin{verbatim} build_flags = '-DLWIP_HOOK_IP4_INPUT(pbuf, input_netif)=({ESP_LOGI("HOOK","TEST");0;})' \end{verbatim} \subsection{Benchmark}