From dfffe4b963d96404ab9fb04bb172ad60d370e6ef Mon Sep 17 00:00:00 2001 From: Ruslan Mstoi Date: Mon, 24 Apr 2017 16:10:52 +0300 Subject: [PATCH] drivers: slip: escape SLIP special characters in headers too Otherwise they are send as is and stripped by tunslip6, resulting in malformed packets. Jira: ZEP-2037 Change-Id: I1267b9ac956f3cd7aa3456ce20447ef97281d7a8 Signed-off-by: Ruslan Mstoi --- drivers/slip/slip.c | 57 ++++++++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 24 deletions(-) diff --git a/drivers/slip/slip.c b/drivers/slip/slip.c index fdd2d8cfd61..52c277b19e4 100644 --- a/drivers/slip/slip.c +++ b/drivers/slip/slip.c @@ -125,6 +125,37 @@ static inline void slip_writeb(unsigned char c) uart_pipe_send(&buf[0], 1); } +/** + * @brief Write byte to SLIP, escape if it is END or ESC character + * + * @param c a byte to write + */ +static void slip_writeb_esc(unsigned char c) +{ + switch (c) { + case SLIP_END: + /* If it's the same code as an END character, + * we send a special two character code so as + * not to make the receiver think we sent + * an END. + */ + slip_writeb(SLIP_ESC); + slip_writeb(SLIP_ESC_END); + break; + case SLIP_ESC: + /* If it's the same code as an ESC character, + * we send a special two character code so as + * not to make the receiver think we sent + * an ESC. + */ + slip_writeb(SLIP_ESC); + slip_writeb(SLIP_ESC_ESC); + break; + default: + slip_writeb(c); + } +} + static int slip_send(struct net_if *iface, struct net_pkt *pkt) { struct net_buf *frag; @@ -154,7 +185,7 @@ static int slip_send(struct net_if *iface, struct net_pkt *pkt) /* This writes ethernet header */ if (!send_header_once && ll_reserve) { for (i = 0; i < ll_reserve; i++) { - slip_writeb(*ptr++); + slip_writeb_esc(*ptr++); } } @@ -174,29 +205,7 @@ static int slip_send(struct net_if *iface, struct net_pkt *pkt) for (i = 0; i < frag->len; ++i) { c = *ptr++; - - switch (c) { - case SLIP_END: - /* If it's the same code as an END character, - * we send a special two character code so as - * not to make the receiver think we sent - * an END. - */ - slip_writeb(SLIP_ESC); - slip_writeb(SLIP_ESC_END); - break; - case SLIP_ESC: - /* If it's the same code as an ESC character, - * we send a special two character code so as - * not to make the receiver think we sent - * an ESC. - */ - slip_writeb(SLIP_ESC); - slip_writeb(SLIP_ESC_ESC); - break; - default: - slip_writeb(c); - } + slip_writeb_esc(c); } #if defined(CONFIG_SLIP_DEBUG)