Can a user set custom MAC address for ns3 nodes?

312 views
Skip to first unread message

Surabhi Karandikar

unread,
Jul 11, 2022, 2:10:41 AM7/11/22
to ns-3-users
Greetings.
Instead of the dynamic MAC addresses which ns3 sets for the nodes in the network created, is it possible for a user to set MAC address manually for the nodes? I wish to do this while running a simulation example in the ns3 UAN module.

I would appreciate all the help from your experience.

Surabhi Karandikar

unread,
Jul 11, 2022, 2:15:18 AM7/11/22
to ns-3-users
I see something similar for this idea on the link (https://www.nsnam.org/docs/release/3.5/manual/manual_35.html ); Can this be done for the UAN module scripts, where the mac8-address class sets the MAC addresses...

The only other change that may be required is to make sure that the address spaces (MAC and IP) on the client and server simulations are compatible. First the MAC address is set to a unique well-known value in both places (illustrated here for one side).

// // We've got the devices in place. Since we're using MAC address // spoofing under the sheets, we need to make sure that the MAC addresses // we have assigned to our devices are unique. Ns-3 will happily // automatically assign the same MAC addresses to the devices in both halves // of our two-script pair, so let's go ahead and just manually change them // to something we ensure is unique. // Ptr<NetDevice> nd = d.Get (0); Ptr<EmuNetDevice> ed = nd->GetObject<EmuNetDevice> (); ed->SetAddress ("00:00:00:00:00:02");

Geovany Teca

unread,
Jul 11, 2022, 4:05:07 AM7/11/22
to ns-3-...@googlegroups.com
Hi,
You can change the MAC Address of a node. Take a look at mac48-address.h, There you can find how ns-3 allocate MAC in sequence Mac48Address::Allocate (void). The methods and operations you can perform on the mac address is also there. The mac address is uint8_t m_address[6].
If you want to implement your own functionality, add it in this file inside the Mac48Address as static so you can call anywhere from outside, something like that.

Mac48Address
Mac48Address::GenerateLocalMac (void)
{
 
  Mac48Address address;
  address.m_address[0] = 0xfb;
  address.m_address[1] = 0xf1;
  address.m_address[2] = 0xf2;
  address.m_address[3] = 0xf3;
  address.m_address[4] = 0xf4;
  address.m_address[5] = 0xf5;
 
  return address;
}

Change the values as you wish or you can implement a random generator inside a loop to make it easier for you.
How to you call it? 
Every time you want to send a frame,when is the moment to set the source MAC in the header , instead of calling set address which will return the default node address you could call 
Mac48Address::GenerateLocalMac (void). That would be temporary STA address in that specific frame. If you wish to make permant change then take a look at this function Mac48Address::Allocate (void) Because is where permante MAC address allocation takes place.

Regards
Geovani



--
Posting to this group should follow these guidelines https://www.nsnam.org/wiki/Ns-3-users-guidelines-for-posting
---
You received this message because you are subscribed to the Google Groups "ns-3-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ns-3-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ns-3-users/c7951324-008a-455b-ae0d-810d88bf83dbn%40googlegroups.com.

Surabhi Karandikar

unread,
Jul 12, 2022, 3:00:03 AM7/12/22
to ns-3-users
Greetings. 
I am very thankful for your idea.. I saw in the uan-mac.cc file, that we have an option to set the MAC address. Will it work the same?
MAC_address.PNG

Thanks and regards,
Surabhi Karandikar

Geovany Teca

unread,
Jul 12, 2022, 3:03:38 AM7/12/22
to ns-3-...@googlegroups.com
Yes, if you fill up your MAC address and then pass to the function SetAdress() it is suppose to work.

Regards 

Surabhi Karandikar

unread,
Jul 12, 2022, 5:02:12 AM7/12/22
to ns-3-users
Greetings,
But ultimately, to fill up the address, we need to go back to the mac8-address file, right.. because the parameter m_address is set in the mac8-address files. Will this snippet work-
mac8-address_custom.PNG

Thanks and regards,
Surabhi Karandikar

Geovany Teca

unread,
Jul 12, 2022, 6:43:03 AM7/12/22
to ns-3-...@googlegroups.com
Hi,
I am not sure if I follow your question. I referred to 48 bit MAC address when I gave the prototype, and if you use that prototype you will be setting temporary MAC address. From what you have shown it seems to be 8 bit MAC address. I am not sure, but I think even using your model in NS-3, when you call create() on NodeContainer method the node MAC address is 48 bits. When you create the node this function is called to assign a 48-MAC address to the node.
mac48-address.cc
image.png
What happens here is just that each node MAC is incremented by 1:
Node 1 = 01:00:00:00:00:00
Node 2 = 02:00:00:00:00:00
Node 3 = 03:00:00:00:00:00
...
I mean you have two options depending on what you want to achieve:
1) Change node MAC permanently: If you want to change the MAC address here is where you can change permanently.
2) Change node MAC per frame: use the GenerateLocalMac () function and sets the frame MAC address to the one you generate.

Anyways, be careful because some MAC addresses are reserved if you see the functions in this file you will see.

Regards
Geovani


Surabhi Karandikar

unread,
Jul 12, 2022, 7:54:03 AM7/12/22
to ns-3-users
Greetings. Based on the mac48 prototype which you first guided me for GenerateLocalMac, I tried to write a similar function for 8 bit MAC addresses, because I observed the following working with the ns3 UAN files-
File: uan-sfama-remi.cc (The main file which I am working upon)
image.png

File: uan-mac.cc
image (1).png

File: mac8-address.h
image (2).png

File: mac8-address.cc
image (3).png
File: mac8-address.cc
image (4).png

Does my observations make sense to go on this approach.
Thanks and regards,
Surabhi Karandikar

Geovany Teca

unread,
Jul 12, 2022, 8:10:41 AM7/12/22
to ns-3-...@googlegroups.com
Greetings,
In this case just change the behavior in the Allocate function. Instead of incrementing, you could place the value you want or generate it randomly.
Doing so you will change the MAC address of the node permanently. If you want to change per frame, then you can set the m_address to some value and before send the frame
, call  SetAddress (m_address).

Regards


Reply all
Reply to author
Forward
0 new messages