DESERT 3.5.1
Loading...
Searching...
No Matches
uwmll-module.cpp
Go to the documentation of this file.
1//
2// Copyright (c) 2017 Regents of the SIGNET lab, University of Padova.
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions
7// are met:
8// 1. Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// 2. Redistributions in binary form must reproduce the above copyright
11// notice, this list of conditions and the following disclaimer in the
12// documentation and/or other materials provided with the distribution.
13// 3. Neither the name of the University of Padova (SIGNET lab) nor the
14// names of its contributors may be used to endorse or promote products
15// derived from this software without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28//
29
30// This module has only slightly modification as respect to the mll module
31// contained in Miracle,
32// released under the same BSD copyright and implemented by
33// Erik Andersson, Emil Ljungdahl, Lars-Olof Moilanen (Karlstad University)
34
45#include "uwmll-module.h"
46#include <mmac-clmsg.h>
47#include <uwip-clmsg.h>
48#include <iostream>
49
53static class UWMllModuleClass : public TclClass
54{
55public:
60 : TclClass("Module/UW/MLL")
61 {
62 }
67 TclObject *
68 create(int, const char *const *)
69 {
70 return (new UWMllModule());
71 }
73
75 : seqno_(0)
76 , n_arp_pkt_drop(0)
77{
78 arptable_ = new UWARPTable();
79 bind("enable_addr_copy_", (int *) &enable_addr_copy);
80}
81
85
86int
88{
89 return Module::crLayCommand(m);
90}
91
92int
94{
95 return Module::crLayCommand(m);
96}
97
98int
100{
101 return Module::recvSyncClMsg(m);
102}
103
104int
105UWMllModule::command(int argc, const char *const *argv)
106{
107 Tcl &tcl = Tcl::instance();
108 if (argc == 2) {
109 if (strcasecmp(argv[1], "reset") == 0) {
110 arptable_->clear();
111 // FALL-THROUGH to give parents a chance to reset
112 } else if (strcasecmp(argv[1], "getArpPacketDrop") == 0) {
113 tcl.resultf("%d", getArpPktDropped());
114 return TCL_OK;
115 }
116 }
117 if (argc == 4) {
118 if (strcasecmp(argv[1], "addentry") == 0) {
119 nsaddr_t ipaddr = atoi(argv[2]);
120 UWARPEntry *e = new UWARPEntry(ipaddr);
121 e->macaddr_ = atoi(argv[3]);
122 e->up_ = 1;
124 return TCL_OK;
125 }
126 }
127 return Module::command(argc, argv);
128}
129
130void
132{
133 recv(p, -1);
134}
135
136void
137UWMllModule::recv(Packet *p, int idSrc)
138{
139 hdr_cmn *ch = HDR_CMN(p);
140 if (ch->direction() == hdr_cmn::UP) {
141 sendUp(p);
142 } else {
143 ch->direction() = hdr_cmn::DOWN;
144 sendDown(p);
145 }
146}
147
148void
150{
151 hdr_cmn *ch = HDR_CMN(p);
152 hdr_uwip *ih = HDR_UWIP(p);
153 nsaddr_t dst = ih->daddr();
154 hdr_ll *llh = HDR_LL(p);
155 hdr_mac *mh = HDR_MAC(p);
156
157 llh->seqno_ = ++seqno_;
158 llh->lltype() = LL_DATA;
159
160 mh->macSA() = getDownAddr();
161 mh->hdr_type() = ETHERTYPE_IP;
162 int tx = 0;
163
164 if (debug_) {
165 std::cout << NOW << "::UWMLL::SENDDOWN::SOURCE_MAC::" << mh->macSA()
166 << "::NEXT_HOP::" << ch->next_hop()
167 << "::DESTINATION::" << (uint)ih->daddr()
168 << std::endl;
169 }
170
171 switch (ch->addr_type()) {
172
173 case NS_AF_ILINK:
174 /* Uhh!? nsaddr_t to int!? */
175 mh->macDA() = ch->next_hop();
176 // cout << "ILINK mode " << endl;
177 break;
178
179 case NS_AF_INET:
180 if (ch->next_hop() == UWIP_BROADCAST)
181 dst = ch->next_hop();
182 else if (ch->next_hop() <= 0)
183 dst = ih->daddr();
184 else
185 dst = ch->next_hop();
186 // cout << "INET mode " << endl;
187
188 case NS_AF_NONE:
189
190 if (UWIP_BROADCAST == (u_int32_t) dst) {
191 mh->macDA() = MAC_BROADCAST;
192 break;
193 }
194
195 /* Resolv ARP */
196 tx = arpResolve(dst, p);
197 break;
198
199 default:
200 mh->macDA() = MAC_BROADCAST;
201 break;
202 }
203
204 if (tx == 0) {
205 Module::sendDown(p);
206 } else {
207 if (enable_addr_copy == 0) {
208 std::cerr << NOW << "Node(" << mh->macSA()
209 << ")::UwMLL_Module -> WARNING: Entry not found for IP "
210 "address "
211 << dst << " Packet dropped" << endl;
213 } else {
214 mh->macDA() = dst;
215 Module::sendDown(p);
216 }
217 }
218}
219
220int
221UWMllModule::arpResolve(nsaddr_t dst, Packet *p)
222{
223 hdr_mac *mh = HDR_MAC(p);
224 UWARPEntry *llinfo;
225 llinfo = arptable_->lookup(dst);
226
227 // Found entry, set dest and return
228 if (llinfo && llinfo->up_) {
229 mh->macDA() = llinfo->macaddr_;
230 return 0;
231 }
232 return EADDRNOTAVAIL;
233}
234
235void
237{
238 Module::sendUp(p);
239}
240
241int
243{
244 MacClMsgGetAddr *c;
245 if (downId != -1) {
246 c = new MacClMsgGetAddr(UNICAST, downId);
247 sendSyncClMsgDown(c);
248 } else {
249 c = new MacClMsgGetAddr(BROADCAST, CLBROADCASTADDR);
250 sendSyncClMsgDown(c);
251 }
252 int val = c->getAddr();
253 delete c;
254 return val;
255}
ARP table entry.
Definition marptable.h:64
int macaddr_
mac address
Definition marptable.h:84
int up_
Is address up?
Definition marptable.h:80
ARP table.
Definition marptable.h:95
UWARPEntry * lookup(nsaddr_t addr)
Lookup entry in table.
Definition marptable.cpp:61
void clear()
Remove all entries in table.
Definition marptable.cpp:72
void addEntry(UWARPEntry *entry)
Add entry to ARP table.
Definition marptable.cpp:55
Class that represents the binding with the tcl configuration script.
UWMllModuleClass()
Constructor of the class.
TclObject * create(int, const char *const *)
Creates the TCL object needed for the tcl language interpretation.
Module for ARP-resolve.
virtual int arpResolve(nsaddr_t dst, Packet *p)
Resolve MAC address for given dst address.
virtual int getDownAddr(int downId=-1)
Retrieve MAC address for lower layer.
virtual void recv(Packet *p)
Retrieve packets from other modules.
virtual void sendUp(Packet *p)
Handle packet going up.
virtual int recvAsyncClMsg(ClMessage *m)
UWMllModule()
Constructor.
virtual int recvSyncClMsg(ClMessage *m)
virtual void sendDown(Packet *p)
Handle packet going down.
virtual int command(int argc, const char *const *argv)
TCL Command handler.
virtual int crLayCommand(ClMessage *m)
Receive asyncronous commands In Practive only IPModule telling us its IP-address.
int getArpPktDropped()
int seqno_
Link layer sequence number.
~UWMllModule()
Desctructor.
UWARPTable * arptable_
Pointer to an arptable.
#define EADDRNOTAVAIL
Definition marptable.h:51
hdr_uwip describes UWIP packets.
Definition uwip-module.h:70
uint8_t & daddr()
Reference to the daddr_ variable.
Cross layer messages definition for the UWIP Module.
static const uint8_t UWIP_BROADCAST
Variable used to represent a broadcast UWIP.
Definition uwip-module.h:62
#define HDR_UWIP(P)
Definition uwip-module.h:58
UWMllModuleClass class_uwll
Provides the declaration of UWMllModule class that represents the MLL class.
#define UWMLL_DROP_REASON_NOT_IN_ARP_LIST