From c9ea543134f3fc111443d5756a76bd68261d3d72 Mon Sep 17 00:00:00 2001
From: KK <ykk@kk-alien>
Date: Thu, 13 Jan 2011 17:42:03 -0800
Subject: [PATCH] State invalid packets

Flow structure to indicate if packet is valid
Allows intentional invalid packet to be dropped
---
 src/include/flow.hh |   15 +++++++++++----
 src/lib/flow.cc     |   24 ++++++++++++++++--------
 2 files changed, 27 insertions(+), 12 deletions(-)

diff --git a/src/include/flow.hh b/src/include/flow.hh
index f787d77..4f7c712 100644
--- a/src/include/flow.hh
+++ b/src/include/flow.hh
@@ -31,6 +31,9 @@ namespace vigil {
 class Buffer;
 
 struct Flow {
+  /** Validity of flow
+   */
+  bool invalid;
   /** Input switch port. 
    */
   uint16_t in_port;       
@@ -74,6 +77,7 @@ struct Flow {
   /** Empty constructor
    */
   Flow() :
+    invalid(false),
     in_port(0), dl_vlan(0), dl_vlan_pcp(0), 
     dl_src(), dl_dst(), dl_type(0),
     nw_src(0), nw_dst(0), 
@@ -81,23 +85,26 @@ struct Flow {
     tp_src(0), tp_dst(0), cookie(0) { }
   /** Copy constructor
    */
-  Flow(const Flow& flow_, uint64_t cookie_=0);
+  Flow(const Flow& flow_, uint64_t cookie_=0, bool invalid_=false);
   /** Constructor from packet
    */
   Flow(uint16_t in_port_, const Buffer&, uint64_t cookie_=0);
   /** Constructor from ofp_match
    */
-  Flow(const ofp_match& match, uint64_t cookie_=0);
+  Flow(const ofp_match& match, uint64_t cookie_=0, 
+       bool invalid_=false);
   /** Constructor from ofp_match
    */
-  Flow(const ofp_match* match, uint64_t cookie_=0);
+  Flow(const ofp_match* match, uint64_t cookie_=0,
+       bool invalid_=false);
   /** Detail constructor
    */
   Flow(uint16_t in_port_, uint16_t dl_vlan_, uint8_t dl_vlan_pcp_,
        ethernetaddr dl_src_, ethernetaddr dl_dst_, uint16_t dl_type_, 
        uint32_t nw_src_, uint32_t nw_dst_, uint8_t nw_proto_,
        uint16_t tp_src_, uint16_t tp_dst_, uint8_t nw_tos_=0, 
-       uint64_t cookie_=0) :
+       uint64_t cookie_=0, bool invalid_=false) :
+    invalid(invalid_),
     in_port(in_port_), dl_vlan(dl_vlan_), dl_vlan_pcp(dl_vlan_pcp_), 
     dl_src(dl_src_), dl_dst(dl_dst_), dl_type(dl_type_),
     nw_src(nw_src_), nw_dst(nw_dst_), 
diff --git a/src/lib/flow.cc b/src/lib/flow.cc
index f5c9ecf..76056f6 100644
--- a/src/lib/flow.cc
+++ b/src/lib/flow.cc
@@ -100,8 +100,9 @@ pull_vlan(Buffer& b)
     return b.try_pull<vlan_header>();
 }
 
-  Flow::Flow(const ofp_match& match, uint64_t cookie_) 
-    : in_port(match.in_port), dl_vlan(match.dl_vlan), 
+  Flow::Flow(const ofp_match& match, uint64_t cookie_, bool invalid_) 
+    : invalid(invalid_),
+      in_port(match.in_port), dl_vlan(match.dl_vlan), 
       dl_vlan_pcp(match.dl_vlan_pcp), 
       dl_src(), dl_dst(), dl_type(match.dl_type),
       nw_src(match.nw_src), nw_dst(match.nw_dst), 
@@ -112,8 +113,9 @@ pull_vlan(Buffer& b)
     memcpy(dl_dst.octet, match.dl_dst, ethernetaddr::LEN);
 }
 
-  Flow::Flow(const ofp_match* match, uint64_t cookie_) 
-    : in_port(match->in_port), dl_vlan(match->dl_vlan), 
+  Flow::Flow(const ofp_match* match, uint64_t cookie_, bool invalid_) 
+    : invalid(invalid_),
+      in_port(match->in_port), dl_vlan(match->dl_vlan), 
       dl_vlan_pcp(match->dl_vlan_pcp), 
       dl_src(), dl_dst(), dl_type(match->dl_type),
       nw_src(match->nw_src), nw_dst(match->nw_dst), 
@@ -144,7 +146,8 @@ const of_match Flow::get_exact_match() const
     return om;
 }
 
-  Flow::Flow(const Flow& flow, uint64_t cookie_):
+Flow::Flow(const Flow& flow, uint64_t cookie_, bool invalid_):
+    invalid(invalid_),
     in_port(flow.in_port), dl_vlan(flow.dl_vlan), dl_vlan_pcp(flow.dl_vlan_pcp), 
     dl_src(flow.dl_src), dl_dst(flow.dl_dst), dl_type(flow.dl_type),
     nw_src(flow.nw_src), nw_dst(flow.nw_dst), 
@@ -158,6 +161,7 @@ Flow::Flow(uint16_t in_port_, const Buffer& buffer, uint64_t cookie_)
       nw_src(0), nw_dst(0), nw_proto(0), nw_tos(0),
       tp_src(0), tp_dst(0), cookie(cookie_)
 {
+    invalid = false;
     dl_vlan = htons(OFP_VLAN_NONE);
 
     Nonowning_buffer b(buffer);
@@ -214,7 +218,8 @@ Flow::Flow(uint16_t in_port_, const Buffer& buffer, uint64_t cookie_)
                         } else {
                             /* Avoid tricking other code into thinking that
                              * this packet has an L4 header. */
-                            nw_proto = 0;
+			    log.err("Invalid TCP packet, though IP protocol number if TCP");
+			    invalid = true;
                         }
                     } else if (nw_proto == ip_::proto::UDP) {
                         const udp_header *udp = pull_udp(b);
@@ -224,7 +229,8 @@ Flow::Flow(uint16_t in_port_, const Buffer& buffer, uint64_t cookie_)
                         } else {
                             /* Avoid tricking other code into thinking that
                              * this packet has an L4 header. */
-                            nw_proto = 0;
+			    log.err("Invalid UDP packet, though IP protocol number if UDP");
+                            invalid = true;
                         }
                     } else if (nw_proto == ip_::proto::ICMP) {
                         const icmp_header *icmp = pull_icmp(b);
@@ -234,7 +240,8 @@ Flow::Flow(uint16_t in_port_, const Buffer& buffer, uint64_t cookie_)
                         } else {
                             /* Avoid tricking other code into thinking that
                              * this packet has an L4 header. */
-                            nw_proto = 0;
+			    log.err("Invalid ICMP packet, though IP protocol number if ICMP");
+                            invalid = true;
                         }
                     }
                 }
@@ -256,6 +263,7 @@ end:
         log.err("Packet length %zu less than minimum Ethernet packet %d: %s",
                  buffer.size(), ETH_HEADER_LEN,
                  to_string().c_str());
+	invalid = true;
     }
 }
 
-- 
1.7.0.4

