OpenSDN source code
icmpv6_error_handler.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 #include "base/os.h"
6 #include <netinet/icmp6.h>
7 #include <vr_defs.h>
8 #include <cmn/agent_cmn.h>
10 #include <oper/vn.h>
11 #include <pkt/pkt_init.h>
15 #include <services/services_init.h>
16 #include <services/icmpv6_proto.h>
17 
18 
20 
22  boost::shared_ptr<PktInfo> info,
23  boost::asio::io_context *io) :
24  ProtoHandler(agent, info, *io), proto_(proto) {
25 }
26 
28 }
29 
31  if (pkt_info_->len < (sizeof(struct ether_header) + sizeof(struct ip6_hdr)))
32  return false;
33  return true;
34 }
35 
37 
38  if (ValidatePacket() == false) {
40  return true;
41  }
42 
43  VmInterface *vm_itf = dynamic_cast<VmInterface *>
45  if (vm_itf == NULL || vm_itf->layer3_forwarding() == false ||
46  vm_itf->IsActive() == false) {
48  return true;
49  }
50  return SendIcmpv6Error(vm_itf);
51 }
52 
53 // Generate ICMP error
55 
56  char *buff = (char *)pkt_info_->pkt;
57  uint16_t buff_len = pkt_info_->packet_buffer()->data_len();
58  char data[ICMPV6_PAYLOAD_LEN];
59  int len = ntohs(pkt_info_->ip6->ip6_plen);
60 
61  if (len > ICMPV6_PAYLOAD_LEN)
62  len = ICMPV6_PAYLOAD_LEN;
63 
64  memcpy(data, pkt_info_->ip6, len);
65 
66  Ip6Address source_address;
67  boost::system::error_code ec;
68  const VnIpam *ipam = intf->vn()->GetIpam(pkt_info_->ip_saddr.to_v6());
69  if (ipam != NULL) {
70  if (ipam->default_gw.is_v6())
71  source_address = ipam->default_gw.to_v6();
72  }
73  // If source address is not found, use vhost address mapped v6 address.
74  // vrouter forwards the icmp error packets as part of the original flow.
75  if (source_address.is_unspecified())
76  source_address = Ip6Address::v4_mapped(agent()->router_id());
77 
78  uint32_t interface =
79  (pkt_info_->agent_hdr.cmd == AgentHdr::TRAP_TOR_CONTROL_PKT) ?
80  pkt_info_->agent_hdr.cmd_param : GetInterfaceIndex();
81 
82  uint16_t eth_len = EthHdr(buff, buff_len, interface,
83  agent()->vhost_interface()->mac(),
84  MacAddress(pkt_info_->eth->ether_shost),
85  ETHERTYPE_IPV6);
86 
87  pkt_info_->ip6 = (struct ip6_hdr *)(buff + eth_len);
89  255, source_address.to_bytes().data(),
90  pkt_info_->ip_saddr.to_v6().to_bytes().data());
91 
92  icmp6_hdr *icmp = pkt_info_->transp.icmp6 =
93  (icmp6_hdr *)(pkt_info_->pkt + eth_len
94  + sizeof(ip6_hdr));
95  icmp->icmp6_type = ICMP6_PACKET_TOO_BIG;
96  icmp->icmp6_code = 0;
97  icmp->icmp6_mtu = htonl(pkt_info_->agent_hdr.mtu);
98  icmp->icmp6_cksum = 0;
99  memcpy(buff + sizeof(ip6_hdr) + eth_len+ICMP_UNREACH_HDR_LEN, data, len);
100  icmp->icmp6_cksum =
101  Icmpv6Csum(source_address.to_bytes().data(),
102  pkt_info_->ip_saddr.to_v6().to_bytes().data(),
103  icmp, len + ICMP_UNREACH_HDR_LEN);
104  pkt_info_->set_len(len + sizeof(ip6_hdr) + eth_len+ICMP_UNREACH_HDR_LEN);
105 
106  uint16_t command =
107  (pkt_info_->agent_hdr.cmd == AgentHdr::TRAP_TOR_CONTROL_PKT) ?
109 
110  Send(GetInterfaceIndex(), pkt_info_->vrf, command,
112  return true;
113 }
boost::asio::ip::address_v6 Ip6Address
Definition: address.h:16
Definition: agent.h:361
InterfaceTable * interface_table() const
Definition: agent.h:468
bool SendIcmpv6Error(VmInterface *intf)
Icmpv6ErrorProto * proto_
Icmpv6ErrorHandler(Agent *agent, Icmpv6ErrorProto *proto, boost::shared_ptr< PktInfo > info, boost::asio::io_context *io)
static const int ICMPV6_PAYLOAD_LEN
void increment_interface_errors()
const Interface * FindInterface(size_t index) const
Definition: interface.cc:323
Agent * agent() const
Definition: proto_handler.h:81
void Send(uint32_t itf, uint32_t vrf, uint16_t, PktHandler::PktModuleName)
int EthHdr(const MacAddress &src, const MacAddress &dest, const uint16_t proto)
uint16_t Icmpv6Csum(const uint8_t *src, const uint8_t *dest, icmp6_hdr *icmp, uint16_t plen) const
void Ip6Hdr(ip6_hdr *ip, uint16_t plen, uint8_t next_header, uint8_t hlim, uint8_t *src, uint8_t *dest)
boost::shared_ptr< PktInfo > pkt_info_
Definition: proto_handler.h:93
uint32_t GetInterfaceIndex() const
Definition: proto_handler.h:83
bool layer3_forwarding() const
bool IsActive() const
const VnEntry * vn() const
const VnIpam * GetIpam(const IpAddress &ip) const
Definition: vn.cc:653
SandeshTraceBufferPtr Icmpv6TraceBuf
#define IPV6_ICMP_NEXT_HEADER
Definition: io_utils.cc:12
#define ICMP_UNREACH_HDR_LEN
Definition: pkt_handler.h:51
boost::shared_ptr< TraceBuffer< SandeshTrace > > SandeshTraceBufferPtr
Definition: sandesh_trace.h:19
@ TRAP_TOR_CONTROL_PKT
Definition: pkt_handler.h:143
Definition: vn.h:29
IpAddress default_gw
Definition: vn.h:32