A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
ipv6-raw-socket-impl.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2007-2009 Strasbourg University
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Sebastien Vincent <vincent@clarinet.u-strasbg.fr>
7 */
8
10
11#include "icmpv6-header.h"
12#include "icmpv6-l4-protocol.h"
13#include "ipv6-l3-protocol.h"
15#include "ipv6-route.h"
17
18#include "ns3/inet6-socket-address.h"
19#include "ns3/log.h"
20#include "ns3/node.h"
21#include "ns3/packet.h"
22#include "ns3/uinteger.h"
23
24#ifdef __WIN32__
25#include "win32-internet.h"
26#else
27#include <netinet/in.h>
28#include <sys/socket.h>
29#endif
30
31#include <sys/types.h>
32
33namespace ns3
34{
35
36NS_LOG_COMPONENT_DEFINE("Ipv6RawSocketImpl");
37
39
42{
43 static TypeId tid = TypeId("ns3::Ipv6RawSocketImpl")
45 .SetGroupName("Internet")
46 .AddAttribute("Protocol",
47 "Protocol number to match.",
51 return tid;
52}
53
66
70
71void
78
79void
81{
82 NS_LOG_FUNCTION(this << node);
83 m_node = node;
84}
85
88{
89 return m_node;
90}
91
94{
95 NS_LOG_FUNCTION(this);
96 return m_err;
97}
98
101{
102 return NS3_SOCK_RAW;
103}
104
105int
107{
108 NS_LOG_FUNCTION(this << address);
109
111 {
113 return -1;
114 }
116 m_src = ad.GetIpv6();
117 if (m_src.IsMulticast())
118 {
119 // Register the multicast group so that the L3 protocol delivers groups
120 // other than all-nodes to this socket. Without this a raw socket bound
121 // to a multicast group never receives any packet (@issueid{1039}). Using
122 // the socket's own join machinery means Close() -> Ipv6LeaveGroup()
123 // cleans it up automatically.
124 Ipv6JoinGroup(m_src, Socket::EXCLUDE, std::vector<Ipv6Address>());
125 }
126 return 0;
127}
128
129int
131{
132 NS_LOG_FUNCTION(this);
134 return 0;
135}
136
137int
139{
140 return Bind();
141}
142
143int
145{
146 NS_LOG_FUNCTION(this);
147 address = Inet6SocketAddress(m_src, 0);
148 return 0;
149}
150
151int
153{
154 NS_LOG_FUNCTION(this << address);
155
156 if (m_dst.IsAny())
157 {
159 return -1;
160 }
161
162 address = Inet6SocketAddress(m_dst, 0);
163
164 return 0;
165}
166
167int
169{
170 NS_LOG_FUNCTION(this);
171 Ptr<Ipv6L3Protocol> ipv6 = m_node->GetObject<Ipv6L3Protocol>();
172
174 if (ipv6)
175 {
176 ipv6->DeleteRawSocket(this);
177 }
178 return 0;
179}
180
181int
183{
184 NS_LOG_FUNCTION(this);
185 m_shutdownSend = true;
186 return 0;
187}
188
189int
191{
192 NS_LOG_FUNCTION(this);
193 m_shutdownRecv = true;
194 return 0;
195}
196
197int
199{
200 NS_LOG_FUNCTION(this << address);
201
203 {
206 return -1;
207 }
209 m_dst = ad.GetIpv6();
211
212 return 0;
213}
214
215int
217{
218 NS_LOG_FUNCTION(this);
220 return -1;
221}
222
223int
225{
226 NS_LOG_FUNCTION(this << p << flags);
228 return SendTo(p, flags, to);
229}
230
231int
233{
234 NS_LOG_FUNCTION(this << p << flags << toAddress);
235
237 {
239 return -1;
240 }
241
242 if (m_shutdownSend)
243 {
244 return 0;
245 }
246
248 Ptr<Ipv6L3Protocol> ipv6 = m_node->GetObject<Ipv6L3Protocol>();
249 Ipv6Address dst = ad.GetIpv6();
250
251 if (IsManualIpv6Tclass())
252 {
253 SocketIpv6TclassTag ipTclassTag;
254 ipTclassTag.SetTclass(GetIpv6Tclass());
255 p->AddPacketTag(ipTclassTag);
256 }
257
258 if (IsManualIpv6HopLimit() && GetIpv6HopLimit() != 0 && !dst.IsMulticast())
259 {
262 p->AddPacketTag(tag);
263 }
264
265 if (ipv6->GetRoutingProtocol())
266 {
267 Ipv6Header hdr;
268 hdr.SetDestination(dst);
270 Ptr<Ipv6Route> route = nullptr;
271 Ptr<NetDevice> oif = m_boundnetdevice; // specify non-zero if bound to a specific device
272
273 if (!m_src.IsAny())
274 {
275 int32_t index = ipv6->GetInterfaceForAddress(m_src);
276 NS_ASSERT(index >= 0);
277 oif = ipv6->GetNetDevice(index);
278 }
279
280 route = ipv6->GetRoutingProtocol()->RouteOutput(p, hdr, oif, err);
281
282 if (route)
283 {
284 NS_LOG_LOGIC("Route exists");
286 {
287 /* calculate checksum here for ICMPv6 echo request (sent by ping6)
288 * as we cannot determine source IPv6 address at application level
289 */
290 uint8_t type;
291 p->CopyData(&type, sizeof(type));
293 {
294 Icmpv6Echo hdr(true);
295 p->RemoveHeader(hdr);
296 hdr.CalculatePseudoHeaderChecksum(route->GetSource(),
297 dst,
298 p->GetSize() + hdr.GetSerializedSize());
299 p->AddHeader(hdr);
300 }
301 }
302
303 uint32_t pktSize = p->GetSize();
304 if (m_src.IsAny())
305 {
306 ipv6->Send(p, route->GetSource(), dst, m_protocol, route);
307 }
308 else
309 {
310 ipv6->Send(p, m_src, dst, m_protocol, route);
311 }
312 // Return only payload size (as Linux does).
313 NotifyDataSent(pktSize);
315 return pktSize;
316 }
317 else
318 {
319 NS_LOG_DEBUG("No route, dropped!");
320 }
321 }
322 return 0;
323}
324
327{
328 NS_LOG_FUNCTION(this << maxSize << flags);
329 Address tmp;
330 return RecvFrom(maxSize, flags, tmp);
331}
332
335{
336 NS_LOG_FUNCTION(this << maxSize << flags << fromAddress);
337
338 if (m_data.empty())
339 {
340 return nullptr;
341 }
342
343 /* get packet */
344 Data data = m_data.front();
345 m_data.pop_front();
346 fromAddress = Inet6SocketAddress(data.fromIp, data.fromProtocol);
347 if (data.packet->GetSize() > maxSize)
348 {
349 Ptr<Packet> first = data.packet->CreateFragment(0, maxSize);
350 if (!(flags & MSG_PEEK))
351 {
352 data.packet->RemoveAtStart(maxSize);
353 }
354 m_data.push_front(data);
355 return first;
356 }
357
358 return data.packet;
359}
360
361void
364 std::vector<Ipv6Address> sourceAddresses)
365{
366 NS_LOG_FUNCTION(this << address << &filterMode << &sourceAddresses);
367
368 // We can join only one multicast group (or change its params)
370 "Can join only one IPv6 multicast group.");
371
373
374 Ptr<Ipv6L3Protocol> ipv6l3 = m_node->GetObject<Ipv6L3Protocol>();
375 if (ipv6l3)
376 {
377 if (filterMode == INCLUDE && sourceAddresses.empty())
378 {
379 // it is a leave
381 {
382 int32_t index = ipv6l3->GetInterfaceForDevice(m_boundnetdevice);
383 NS_ASSERT_MSG(index >= 0, "Interface without a valid index");
384 ipv6l3->RemoveMulticastAddress(address, index);
385 }
386 else
387 {
388 ipv6l3->RemoveMulticastAddress(address);
389 }
390 }
391 else
392 {
393 // it is a join or a modification
395 {
396 int32_t index = ipv6l3->GetInterfaceForDevice(m_boundnetdevice);
397 NS_ASSERT_MSG(index >= 0, "Interface without a valid index");
398 ipv6l3->AddMulticastAddress(address, index);
399 }
400 else
401 {
402 ipv6l3->AddMulticastAddress(address);
403 }
404 }
405 }
406}
407
410{
411 NS_LOG_FUNCTION(this);
412 return 0xffffffff;
413}
414
417{
418 NS_LOG_FUNCTION(this);
419 uint32_t rx = 0;
420
421 for (auto it = m_data.begin(); it != m_data.end(); ++it)
422 {
423 rx += (it->packet)->GetSize();
424 }
425
426 return rx;
427}
428
429bool
431{
432 NS_LOG_FUNCTION(this << *p << hdr << device);
433
434 if (m_shutdownRecv)
435 {
436 return false;
437 }
438
439 Ptr<NetDevice> boundNetDevice = Socket::GetBoundNetDevice();
440 if (boundNetDevice)
441 {
442 if (boundNetDevice != device)
443 {
444 return false;
445 }
446 }
447
448 if ((m_src == Ipv6Address::GetAny() || hdr.GetDestination() == m_src) &&
449 (m_dst == Ipv6Address::GetAny() || hdr.GetSource() == m_dst) &&
450 hdr.GetNextHeader() == m_protocol)
451 {
452 Ptr<Packet> copy = p->Copy();
453
455 {
456 /* filter */
457 Icmpv6Header icmpHeader;
458 copy->PeekHeader(icmpHeader);
459 uint8_t type = icmpHeader.GetType();
460
461 if (Icmpv6FilterWillBlock(type))
462 {
463 /* packet filtered */
464 return false;
465 }
466 }
467
468 // Should check via getsockopt ().
469 if (IsRecvPktInfo())
470 {
472 copy->RemovePacketTag(tag);
473 tag.SetAddress(hdr.GetDestination());
474 tag.SetHoplimit(hdr.GetHopLimit());
476 tag.SetRecvIf(device->GetIfIndex());
477 copy->AddPacketTag(tag);
478 }
479
480 // Check only version 6 options
481 if (IsIpv6RecvTclass())
482 {
483 SocketIpv6TclassTag ipTclassTag;
484 ipTclassTag.SetTclass(hdr.GetTrafficClass());
485 copy->AddPacketTag(ipTclassTag);
486 }
487
488 if (IsIpv6RecvHopLimit())
489 {
490 SocketIpv6HopLimitTag ipHopLimitTag;
491 ipHopLimitTag.SetHopLimit(hdr.GetHopLimit());
492 copy->AddPacketTag(ipHopLimitTag);
493 }
494
495 copy->AddHeader(hdr);
496 Data data;
497 data.packet = copy;
498 data.fromIp = hdr.GetSource();
499 data.fromProtocol = hdr.GetNextHeader();
500 m_data.push_back(data);
502 return true;
503 }
504 return false;
505}
506
507bool
509{
510 return allowBroadcast;
511}
512
513bool
515{
516 return true;
517}
518
519void
524
525void
530
531void
533{
534 m_icmpFilter.icmpv6Filt[type >> 5] |= (uint32_t(1) << (type & 31));
535}
536
537void
539{
540 m_icmpFilter.icmpv6Filt[(type) >> 5] &= ~(uint32_t(1) << (type & 31));
541}
542
543bool
545{
546 return ((m_icmpFilter.icmpv6Filt[type >> 5]) & (uint32_t(1) << (type & 31))) != 0;
547}
548
549bool
551{
552 return ((m_icmpFilter.icmpv6Filt[type >> 5]) & (uint32_t(1) << (type & 31))) == 0;
553}
554
555} /* namespace ns3 */
a polymophic address class
Definition address.h:114
ICMPv6 Echo message.
uint32_t GetSerializedSize() const override
Get the serialized size.
ICMPv6 header.
uint8_t GetType() const
Get the type field.
void CalculatePseudoHeaderChecksum(Ipv6Address src, Ipv6Address dst, uint16_t length)
Calculate pseudo header checksum for IPv6.
static uint16_t GetStaticProtocolNumber()
Get ICMPv6 protocol number.
An Inet6 address class.
static Inet6SocketAddress ConvertFrom(const Address &addr)
Convert the address to a InetSocketAddress.
static bool IsMatchingType(const Address &addr)
If the address match.
Ipv6Address GetIpv6() const
Get the IPv6 address.
Describes an IPv6 address.
static Ipv6Address GetAny()
Get the "any" (::) Ipv6Address.
Packet header for IPv6.
Definition ipv6-header.h:24
void SetDestination(Ipv6Address dst)
Set the "Destination address" field.
uint8_t GetHopLimit() const
Get the "Hop limit" field (TTL).
uint8_t GetNextHeader() const
Get the next header.
Ipv6Address GetDestination() const
Get the "Destination address" field.
uint8_t GetTrafficClass() const
Get the "Traffic class" field.
Ipv6Address GetSource() const
Get the "Source address" field.
IPv6 layer implementation.
This class implements a tag that carries socket ancillary data to the socket interface.
void SetTrafficClass(uint8_t tclass)
Set the tag's Traffic Class.
void SetRecvIf(uint32_t ifindex)
Set the tag's receiving interface.
void SetHoplimit(uint8_t ttl)
Set the tag's Hop Limit.
void SetAddress(Ipv6Address addr)
Set the tag's address.
bool Icmpv6FilterWillBlock(uint8_t type)
Ask the filter about the status of one ICMPv6 type.
Socket::SocketErrno m_err
Last error number.
void Icmpv6FilterSetBlockAll()
Set the filter to block all the ICMPv6 types.
int Listen() override
Listen for incoming connections.
bool SetAllowBroadcast(bool allowBroadcast) override
Configure whether broadcast datagram transmissions are allowed.
int SendTo(Ptr< Packet > p, uint32_t flags, const Address &toAddress) override
Send data to a specified peer.
void SetNode(Ptr< Node > node)
Set the node associated with this socket.
uint32_t GetRxAvailable() const override
Return number of bytes which can be returned from one or multiple calls to Recv.
void Icmpv6FilterSetBlock(uint8_t type)
Set the filter to block one ICMPv6 type.
Ipv6Address m_src
Source address.
Icmpv6Filter m_icmpFilter
ICMPv6 filter.
int Bind6() override
Allocate a local IPv6 endpoint for this socket.
int Close() override
Close a socket.
int Send(Ptr< Packet > p, uint32_t flags) override
Send data (or dummy data) to the remote host.
int GetPeerName(Address &address) const override
Get the peer address of a connected socket.
bool GetAllowBroadcast() const override
Query whether broadcast datagram transmissions are allowed.
Ptr< Node > GetNode() const override
Return the node this socket is associated with.
Socket::SocketType GetSocketType() const override
Get socket type (NS3_SOCK_RAW).
static TypeId GetTypeId()
Get the type ID of this class.
bool m_shutdownRecv
Flag to shutdown receive capability.
Ptr< Packet > RecvFrom(uint32_t maxSize, uint32_t flags, Address &fromAddress) override
Read a single packet from the socket and retrieve the sender address.
int Bind() override
Allocate a local IPv4 endpoint for this socket.
int GetSockName(Address &address) const override
Get socket address.
bool m_shutdownSend
Flag to shutdown send capability.
bool ForwardUp(Ptr< const Packet > p, Ipv6Header hdr, Ptr< NetDevice > device)
Forward up to receive method.
int Connect(const Address &address) override
Initiate a connection to a remote host.
bool Icmpv6FilterWillPass(uint8_t type)
Ask the filter about the status of one ICMPv6 type.
uint32_t GetTxAvailable() const override
Returns the number of bytes which can be sent in a single call to Send.
Socket::SocketErrno GetErrno() const override
Get last error number.
Ipv6Address m_dst
Destination address.
void DoDispose() override
Dispose object.
std::list< Data > m_data
Packet waiting to be processed.
void Icmpv6FilterSetPass(uint8_t type)
Set the filter to pass one ICMPv6 type.
void Ipv6JoinGroup(Ipv6Address address, Socket::Ipv6MulticastFilterMode filterMode, std::vector< Ipv6Address > sourceAddresses) override
Joins a IPv6 multicast group.
void Icmpv6FilterSetPassAll()
Clean the ICMPv6 filter structure.
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
Ptr< NetDevice > GetBoundNetDevice()
Returns socket's bound NetDevice, if any.
Definition socket.cc:336
Ptr< Packet > Recv()
Read a single packet from the socket.
Definition socket.cc:163
virtual void Ipv6LeaveGroup()
Leaves IPv6 multicast group this socket is joined to.
Definition socket.cc:569
void NotifySend(uint32_t spaceAvailable)
Notify through the callback (if set) that some data have been sent.
Definition socket.cc:281
bool IsRecvPktInfo() const
Get status indicating whether enable/disable packet information to socket.
Definition socket.cc:350
Ipv6Address m_ipv6MulticastGroupAddress
IPv6 multicast group address.
Definition socket.h:1074
SocketType
Enumeration of the possible socket types.
Definition socket.h:96
@ NS3_SOCK_RAW
Definition socket.h:100
void NotifyDataRecv()
Notify through the callback (if set) that some data have been received.
Definition socket.cc:291
Ipv6MulticastFilterMode
Enumeration of the possible filter of a socket.
Definition socket.h:132
Ptr< NetDevice > m_boundnetdevice
the device this socket is bound to (might be null).
Definition socket.h:1071
bool IsIpv6RecvTclass() const
Ask if the socket is currently passing information about IPv6 Traffic Class up the stack.
Definition socket.cc:493
bool IsIpv6RecvHopLimit() const
Ask if the socket is currently passing information about IPv6 Hop Limit up the stack.
Definition socket.cc:543
virtual uint8_t GetIpv6HopLimit() const
Query the value of IP Hop Limit field of this socket.
Definition socket.cc:531
SocketErrno
Enumeration of the possible errors returned by a socket.
Definition socket.h:73
@ ERROR_INVAL
Definition socket.h:82
@ ERROR_OPNOTSUPP
Definition socket.h:80
@ ERROR_NOTERROR
Definition socket.h:74
@ ERROR_NOTCONN
Definition socket.h:76
void NotifyDataSent(uint32_t size)
Notify through the callback (if set) that some data have been sent.
Definition socket.cc:271
void NotifyConnectionSucceeded()
Notify through the callback (if set) that the connection has been established.
Definition socket.cc:203
void DoDispose() override
Destructor implementation.
Definition socket.cc:301
uint8_t GetIpv6Tclass() const
Query the value of IPv6 Traffic Class field of this socket.
Definition socket.cc:481
bool IsManualIpv6HopLimit() const
Checks if the socket has a specific IPv6 Hop Limit set.
Definition socket.cc:369
bool IsManualIpv6Tclass() const
Checks if the socket has a specific IPv6 Tclass set.
Definition socket.cc:357
void NotifyConnectionFailed()
Notify through the callback (if set) that the connection has not been established due to an error.
Definition socket.cc:213
This class implements a tag that carries the socket-specific HOPLIMIT of a packet to the IPv6 layer.
Definition socket.h:1162
void SetHopLimit(uint8_t hopLimit)
Set the tag's Hop Limit.
Definition socket.cc:658
indicates whether the socket has IPV6_TCLASS set.
Definition socket.h:1356
void SetTclass(uint8_t tclass)
Set the tag's Tclass.
Definition socket.cc:901
a unique identifier for an interface.
Definition type-id.h:50
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1003
Hold an unsigned integer type.
Definition uinteger.h:34
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition assert.h:55
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition assert.h:76
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:201
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:267
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition log.h:281
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition object-base.h:35
Definition first.py:1
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeChecker > MakeUintegerChecker()
Definition uinteger.h:85
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Definition uinteger.h:35
uint32_t GetSize(Ptr< const Packet > packet, const WifiMacHeader *hdr, bool isAmpdu)
Return the total size of the packet after WifiMacHeader and FCS trailer have been added.
uint8_t data[writeSize]
IPv6 raw data and additional information.
Struct to hold the ICMPv6 filter.