From c54ba49e82eac8218c64c25e191654df0f3862b7 Mon Sep 17 00:00:00 2001
From: Martine Lenders <m.lenders@fu-berlin.de>
Date: Tue, 23 Oct 2018 19:34:46 +0200
Subject: [PATCH] gnrc_ipv6_ext: move ipv6_ext_rh (partly) to GNRC

---
 Makefile.dep                                  |  6 +-
 sys/Makefile                                  |  3 -
 sys/include/net/gnrc/ipv6/ext.h               |  4 +
 sys/include/net/gnrc/ipv6/ext/rh.h            | 73 +++++++++++++++++++
 sys/include/net/gnrc/rpl/srh.h                |  6 +-
 sys/include/net/ipv6/ext/rh.h                 | 31 ++------
 sys/net/gnrc/Makefile                         |  3 +
 .../network_layer/ipv6/ext/gnrc_ipv6_ext.c    | 19 +++--
 .../network_layer/ipv6/ext/rh/Makefile        |  2 +-
 .../ipv6/ext/rh/gnrc_ipv6_ext_rh.c}           |  6 +-
 sys/net/gnrc/routing/rpl/srh/gnrc_rpl_srh.c   | 12 +--
 11 files changed, 110 insertions(+), 55 deletions(-)
 create mode 100644 sys/include/net/gnrc/ipv6/ext/rh.h
 rename sys/net/{ => gnrc}/network_layer/ipv6/ext/rh/Makefile (57%)
 rename sys/net/{network_layer/ipv6/ext/rh/ipv6_ext_rh.c => gnrc/network_layer/ipv6/ext/rh/gnrc_ipv6_ext_rh.c} (81%)

diff --git a/Makefile.dep b/Makefile.dep
index 9b01c11117..e0f0641169 100644
--- a/Makefile.dep
+++ b/Makefile.dep
@@ -245,11 +245,11 @@ ifneq (,$(filter gnrc_icmpv6,$(USEMODULE)))
 endif
 
 ifneq (,$(filter gnrc_rpl_srh,$(USEMODULE)))
-  USEMODULE += ipv6_ext_rh
+  USEMODULE += gnrc_ipv6_ext_rh
 endif
 
-ifneq (,$(filter ipv6_ext_rh,$(USEMODULE)))
-  USEMODULE += ipv6_ext
+ifneq (,$(filter gnrc_ipv6_ext_rh,$(USEMODULE)))
+  USEMODULE += gnrc_ipv6_ext
 endif
 
 ifneq (,$(filter gnrc_ipv6_ext,$(USEMODULE)))
diff --git a/sys/Makefile b/sys/Makefile
index bab6d94783..58631e23da 100644
--- a/sys/Makefile
+++ b/sys/Makefile
@@ -46,9 +46,6 @@ endif
 ifneq (,$(filter ipv6_ext_rh,$(USEMODULE)))
   DIRS += net/network_layer/ipv6/ext/rh
 endif
-ifneq (,$(filter ipv6_ext,$(USEMODULE)))
-  DIRS += net/network_layer/ipv6/ext
-endif
 ifneq (,$(filter ipv6_hdr,$(USEMODULE)))
   DIRS += net/network_layer/ipv6/hdr
 endif
diff --git a/sys/include/net/gnrc/ipv6/ext.h b/sys/include/net/gnrc/ipv6/ext.h
index 84fdf17d83..e772e7f7eb 100644
--- a/sys/include/net/gnrc/ipv6/ext.h
+++ b/sys/include/net/gnrc/ipv6/ext.h
@@ -31,6 +31,10 @@
 #include "net/gnrc/pkt.h"
 #include "net/ipv6/ext.h"
 
+#ifdef MODULE_GNRC_IPV6_EXT_RH
+#include "net/gnrc/ipv6/ext/rh.h"
+#endif
+
 #ifdef __cplusplus
 extern "C" {
 #endif
diff --git a/sys/include/net/gnrc/ipv6/ext/rh.h b/sys/include/net/gnrc/ipv6/ext/rh.h
new file mode 100644
index 0000000000..e657eb0726
--- /dev/null
+++ b/sys/include/net/gnrc/ipv6/ext/rh.h
@@ -0,0 +1,73 @@
+/*
+ * Copyright (C) 2018 Freie Universität Berlin
+ *
+ * This file is subject to the terms and conditions of the GNU Lesser
+ * General Public License v2.1. See the file LICENSE in the top level
+ * directory for more details.
+ */
+
+/**
+ * @defgroup    net_gnrc_ipv6_ext_rh Support for IPv6 routing header extension
+ * @ingroup     net_gnrc_ipv6_ext
+ * @brief       GNRC implementation of IPv6 routing header extension.
+ * @{
+ *
+ * @file
+ * @brief   GNRC routing extension header definitions.
+ *
+ * @author  Martine Lenders <m.lenders@fu-berlin.de>
+ */
+#ifndef NET_GNRC_IPV6_EXT_RH_H
+#define NET_GNRC_IPV6_EXT_RH_H
+
+#include "net/ipv6/hdr.h"
+
+#include "net/ipv6/ext/rh.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+enum {
+    /**
+     * @brief   An error occurred during routing header processing
+     */
+    GNRC_IPV6_EXT_RH_ERROR = 0,
+    /**
+     * @brief   The routing header was successfully processed and this node
+     *          is the destination (i.e. ipv6_ext_rh_t::seg_left == 0)
+     */
+    GNRC_IPV6_EXT_RH_OK,
+    /**
+     * @brief   The routing header was successfully processed and the packet
+     *          was forwarded to another node or should be forwarded to another
+     *          node.
+     *
+     * When @ref gnrc_ipv6_ext_rh_process() returns this value, the packet was
+     * already forwarded to another node. Implementations for specific routing
+     * header types should leave the forwarding to the calling @ref
+     * gnrc_ipv6_ext_rh_process() and should return @ref
+     * GNRC_IPV6_EXT_RH_FORWARDED if they want the packet to be forwarded. They
+     * should however set ipv6_hdr_t::dst accordingly.
+     */
+    GNRC_IPV6_EXT_RH_FORWARDED,
+};
+
+/**
+ * @brief   Process the routing header of an IPv6 packet.
+ *
+ * @param[in, out] ipv6     An IPv6 packet.
+ * @param[in] ext           A routing header of @p ipv6.
+ *
+ * @return  @ref GNRC_IPV6_EXT_RH_OK, on success
+ * @return  @ref GNRC_IPV6_EXT_RH_FORWARDED, when @p pkt was forwarded
+ * @return  @ref GNRC_IPV6_EXT_RH_ERROR, on error
+ */
+int gnrc_ipv6_ext_rh_process(ipv6_hdr_t *ipv6, ipv6_ext_rh_t *ext);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* NET_GNRC_IPV6_EXT_RH_H */
+/** @} */
diff --git a/sys/include/net/gnrc/rpl/srh.h b/sys/include/net/gnrc/rpl/srh.h
index 5078198d4d..e4639696c5 100644
--- a/sys/include/net/gnrc/rpl/srh.h
+++ b/sys/include/net/gnrc/rpl/srh.h
@@ -60,9 +60,9 @@ typedef struct __attribute__((packed)) {
  * @param[in,out] ipv6  The IPv6 header of the incoming packet.
  * @param[in] rh        A RPL source routing header.
  *
- * @return  EXT_RH_CODE_ERROR
- * @return  EXT_RH_CODE_FORWARD
- * @return  EXT_RH_CODE_OK
+ * @return  @ref GNRC_IPV6_EXT_RH_OK, on success
+ * @return  @ref GNRC_IPV6_EXT_RH_FORWARDED, when @p pkt *should be* forwarded
+ * @return  @ref GNRC_IPV6_EXT_RH_ERROR, on error
  */
 int gnrc_rpl_srh_process(ipv6_hdr_t *ipv6, gnrc_rpl_srh_t *rh);
 
diff --git a/sys/include/net/ipv6/ext/rh.h b/sys/include/net/ipv6/ext/rh.h
index 39848d6c8c..882b57b392 100644
--- a/sys/include/net/ipv6/ext/rh.h
+++ b/sys/include/net/ipv6/ext/rh.h
@@ -1,5 +1,6 @@
 /*
- * Copyright (C) 2015 Martine Lenders <mlenders@inf.fu-berlin.de>
+ * Copyright (C) 2015 Cenk Gündoğan <cnkgndgn@gmail.com>
+ * Copyright (C) 2018 Freie Universität Berlin
  *
  * This file is subject to the terms and conditions of the GNU Lesser
  * General Public License v2.1. See the file LICENSE in the top level
@@ -9,13 +10,14 @@
 /**
  * @defgroup    net_ipv6_ext_rh IPv6 routing header extension
  * @ingroup     net_ipv6_ext
- * @brief       Implementation of IPv6 routing header extension.
+ * @brief       Definitions for IPv6 routing header extension.
  * @{
  *
  * @file
  * @brief   Routing extension header definitions.
  *
- * @author  Martine Lenders <mlenders@inf.fu-berlin.de>
+ * @author  Cenk Gündoğan <cnkgndgn@gmail.com>
+ * @author  Martine Lenders <m.lenders@fu-berlin.de>
  */
 #ifndef NET_IPV6_EXT_RH_H
 #define NET_IPV6_EXT_RH_H
@@ -30,17 +32,6 @@
 extern "C" {
 #endif
 
-/**
- * @name Return codes for routing header processing
- * @{
- */
-#define EXT_RH_CODE_ERROR       (-1)
-#define EXT_RH_CODE_FORWARD     (0)
-#define EXT_RH_CODE_OK          (1)
-/**
- * @}
- */
-
 /**
  * @brief   IPv6 routing extension header.
  *
@@ -55,18 +46,6 @@ typedef struct __attribute__((packed)) {
     uint8_t seg_left;   /**< number of route segments remaining */
 } ipv6_ext_rh_t;
 
-/**
- * @brief   Process the routing header of an IPv6 packet.
- *
- * @param[in, out] ipv6     An IPv6 packet.
- * @param[in] ext           A routing header of @p ipv6.
- *
- * @return  EXT_RH_CODE_ERROR
- * @return  EXT_RH_CODE_FORWARD
- * @return  EXT_RH_CODE_OK
- */
-int ipv6_ext_rh_process(ipv6_hdr_t *ipv6, ipv6_ext_rh_t *ext);
-
 #ifdef __cplusplus
 }
 #endif
diff --git a/sys/net/gnrc/Makefile b/sys/net/gnrc/Makefile
index 372b1e3b73..eeae57137e 100644
--- a/sys/net/gnrc/Makefile
+++ b/sys/net/gnrc/Makefile
@@ -13,6 +13,9 @@ endif
 ifneq (,$(filter gnrc_ipv6_ext,$(USEMODULE)))
   DIRS += network_layer/ipv6/ext
 endif
+ifneq (,$(filter gnrc_ipv6_ext_rh,$(USEMODULE)))
+  DIRS += network_layer/ipv6/ext/rh
+endif
 ifneq (,$(filter gnrc_ipv6_hdr,$(USEMODULE)))
   DIRS += network_layer/ipv6/hdr
 endif
diff --git a/sys/net/gnrc/network_layer/ipv6/ext/gnrc_ipv6_ext.c b/sys/net/gnrc/network_layer/ipv6/ext/gnrc_ipv6_ext.c
index 9458869bfc..9e2a802b17 100644
--- a/sys/net/gnrc/network_layer/ipv6/ext/gnrc_ipv6_ext.c
+++ b/sys/net/gnrc/network_layer/ipv6/ext/gnrc_ipv6_ext.c
@@ -18,14 +18,14 @@
 #include "net/gnrc/netif.h"
 #include "net/gnrc/pktbuf.h"
 #include "net/gnrc/ipv6.h"
+#include "net/gnrc/ipv6/ext/rh.h"
 
 #include "net/gnrc/ipv6/ext.h"
 
 #define ENABLE_DEBUG    (0)
 #include "debug.h"
 
-#ifdef MODULE_GNRC_RPL_SRH
-
+#ifdef MODULE_GNRC_IPV6_EXT_RH
 enum gnrc_ipv6_ext_demux_status {
     GNRC_IPV6_EXT_OK,
     GNRC_IPV6_EXT_FORWARDED,
@@ -65,13 +65,13 @@ static enum gnrc_ipv6_ext_demux_status _handle_rh(gnrc_pktsnip_t *current, gnrc_
         hdr = ipv6->data;
     }
 
-    switch (ipv6_ext_rh_process(hdr, (ipv6_ext_rh_t *)ext)) {
-        case EXT_RH_CODE_ERROR:
+    switch (gnrc_ipv6_ext_rh_process(hdr, (ipv6_ext_rh_t *)ext)) {
+        case GNRC_IPV6_EXT_RH_ERROR:
             /* TODO: send ICMPv6 error codes */
             gnrc_pktbuf_release(pkt);
             return GNRC_IPV6_EXT_ERROR;
 
-        case EXT_RH_CODE_FORWARD:
+        case GNRC_IPV6_EXT_RH_FORWARDED:
             /* forward packet */
             if (!gnrc_netapi_dispatch_receive(GNRC_NETTYPE_IPV6, GNRC_NETREG_DEMUX_CTX_ALL, pkt)) {
                 DEBUG("ipv6: could not dispatch packet to the ipv6 thread\n");
@@ -79,7 +79,7 @@ static enum gnrc_ipv6_ext_demux_status _handle_rh(gnrc_pktsnip_t *current, gnrc_
             }
             return GNRC_IPV6_EXT_FORWARDED;
 
-        case EXT_RH_CODE_OK:
+        case GNRC_IPV6_EXT_RH_OK:
             /* this should not happen since we checked seg_left early */
             gnrc_pktbuf_release(pkt);
             return GNRC_IPV6_EXT_ERROR;
@@ -87,8 +87,7 @@ static enum gnrc_ipv6_ext_demux_status _handle_rh(gnrc_pktsnip_t *current, gnrc_
 
     return GNRC_IPV6_EXT_OK;
 }
-
-#endif
+#endif  /* MODULE_GNRC_IPV6_EXT_RH */
 
 /**
  * @brief marks IPv6 extension header if needed.
@@ -183,7 +182,7 @@ void gnrc_ipv6_ext_demux(gnrc_netif_t *netif,
 
         switch (nh) {
             case PROTNUM_IPV6_EXT_RH:
-#ifdef MODULE_GNRC_RPL_SRH
+#ifdef MODULE_GNRC_IPV6_EXT_RH
                 /* if current != pkt, size is already checked */
                 if (current == pkt && !_has_valid_size(pkt, nh)) {
                     DEBUG("ipv6_ext: invalid size\n");
@@ -215,7 +214,7 @@ void gnrc_ipv6_ext_demux(gnrc_netif_t *netif,
                 }
 
                 break;
-#endif
+#endif  /* MODULE_GNRC_IPV6_EXT_RH */
 
             case PROTNUM_IPV6_EXT_HOPOPT:
             case PROTNUM_IPV6_EXT_DST:
diff --git a/sys/net/network_layer/ipv6/ext/rh/Makefile b/sys/net/gnrc/network_layer/ipv6/ext/rh/Makefile
similarity index 57%
rename from sys/net/network_layer/ipv6/ext/rh/Makefile
rename to sys/net/gnrc/network_layer/ipv6/ext/rh/Makefile
index aeefa0b6a9..6620668583 100644
--- a/sys/net/network_layer/ipv6/ext/rh/Makefile
+++ b/sys/net/gnrc/network_layer/ipv6/ext/rh/Makefile
@@ -1,3 +1,3 @@
-MODULE = ipv6_ext_rh
+MODULE = gnrc_ipv6_ext_rh
 
 include $(RIOTBASE)/Makefile.base
diff --git a/sys/net/network_layer/ipv6/ext/rh/ipv6_ext_rh.c b/sys/net/gnrc/network_layer/ipv6/ext/rh/gnrc_ipv6_ext_rh.c
similarity index 81%
rename from sys/net/network_layer/ipv6/ext/rh/ipv6_ext_rh.c
rename to sys/net/gnrc/network_layer/ipv6/ext/rh/gnrc_ipv6_ext_rh.c
index 187a5c8056..e00cf2d03f 100644
--- a/sys/net/network_layer/ipv6/ext/rh/ipv6_ext_rh.c
+++ b/sys/net/gnrc/network_layer/ipv6/ext/rh/gnrc_ipv6_ext_rh.c
@@ -16,10 +16,10 @@
 
 #include "net/protnum.h"
 #include "net/ipv6/ext.h"
-#include "net/ipv6/ext/rh.h"
+#include "net/gnrc/ipv6/ext/rh.h"
 #include "net/gnrc/rpl/srh.h"
 
-int ipv6_ext_rh_process(ipv6_hdr_t *hdr, ipv6_ext_rh_t *ext)
+int gnrc_ipv6_ext_rh_process(ipv6_hdr_t *hdr, ipv6_ext_rh_t *ext)
 {
     (void) hdr;
 
@@ -32,7 +32,7 @@ int ipv6_ext_rh_process(ipv6_hdr_t *hdr, ipv6_ext_rh_t *ext)
         default:
             break;
     }
-    return EXT_RH_CODE_ERROR;
+    return GNRC_IPV6_EXT_RH_ERROR;
 }
 
 /** @} */
diff --git a/sys/net/gnrc/routing/rpl/srh/gnrc_rpl_srh.c b/sys/net/gnrc/routing/rpl/srh/gnrc_rpl_srh.c
index c1e5deaa26..11ece16655 100644
--- a/sys/net/gnrc/routing/rpl/srh/gnrc_rpl_srh.c
+++ b/sys/net/gnrc/routing/rpl/srh/gnrc_rpl_srh.c
@@ -14,7 +14,7 @@
 
 #include <string.h>
 #include "net/gnrc/netif/internal.h"
-#include "net/ipv6/ext/rh.h"
+#include "net/gnrc/ipv6/ext/rh.h"
 #include "net/gnrc/rpl/srh.h"
 
 #define ENABLE_DEBUG    (0)
@@ -29,7 +29,7 @@ static char addr_str[IPV6_ADDR_MAX_STR_LEN];
 int gnrc_rpl_srh_process(ipv6_hdr_t *ipv6, gnrc_rpl_srh_t *rh)
 {
     if (rh->seg_left == 0) {
-        return EXT_RH_CODE_OK;
+        return GNRC_IPV6_EXT_RH_OK;
     }
 
     uint8_t n = (((rh->len * 8) - GNRC_RPL_SRH_PADDING(rh->pad_resv) -
@@ -45,7 +45,7 @@ int gnrc_rpl_srh_process(ipv6_hdr_t *ipv6, gnrc_rpl_srh_t *rh)
     if (rh->seg_left > n) {
         DEBUG("RPL SRH: number of segments left > number of addresses - discard\n");
         /* TODO ICMP Parameter Problem - Code 0 */
-        return EXT_RH_CODE_ERROR;
+        return GNRC_IPV6_EXT_RH_ERROR;
     }
 
     rh->seg_left--;
@@ -58,7 +58,7 @@ int gnrc_rpl_srh_process(ipv6_hdr_t *ipv6, gnrc_rpl_srh_t *rh)
     if (ipv6_addr_is_multicast(&ipv6->dst) || ipv6_addr_is_multicast(&addr)) {
         DEBUG("RPL SRH: found a multicast address - discard\n");
         /* TODO discard the packet */
-        return EXT_RH_CODE_ERROR;
+        return GNRC_IPV6_EXT_RH_ERROR;
     }
 
     /* check if multiple addresses of my interface exist */
@@ -75,7 +75,7 @@ int gnrc_rpl_srh_process(ipv6_hdr_t *ipv6, gnrc_rpl_srh_t *rh)
             if (found && ((k - found_pos) > 1)) {
                 DEBUG("RPL SRH: found multiple addresses that belong to me - discard\n");
                 /* TODO send an ICMP Parameter Problem (Code 0) and discard the packet */
-                return EXT_RH_CODE_ERROR;
+                return GNRC_IPV6_EXT_RH_ERROR;
             }
             found_pos = k;
             found = true;
@@ -89,7 +89,7 @@ int gnrc_rpl_srh_process(ipv6_hdr_t *ipv6, gnrc_rpl_srh_t *rh)
 
     ipv6->dst = addr;
 
-    return EXT_RH_CODE_FORWARD;
+    return GNRC_IPV6_EXT_RH_FORWARDED;
 }
 
 /** @} */
-- 
GitLab