OpenSDN source code
icmp_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 <stdint.h>
6 #include "base/os.h"
7 #include <vr_defs.h>
8 #include <cmn/agent_cmn.h>
10 #include <oper/vn.h>
11 
12 #include <pkt/pkt_init.h>
13 #include <pkt/flow_table.h>
16 
18  boost::shared_ptr<PktInfo> info,
19  boost::asio::io_context *io) :
20  ProtoHandler(agent, info, *io), proto_(proto) {
21 }
22 
24 }
25 
27  if (pkt_info_->len < (sizeof(struct ether_header) + sizeof(struct ip)))
28  return false;
29  return true;
30 }
31 
33  if (ValidatePacket() == false) {
35  return true;
36  }
37 
38  VmInterface *vm_itf = dynamic_cast<VmInterface *>
40  if (vm_itf == NULL || vm_itf->layer3_forwarding() == false ||
41  vm_itf->vn() == NULL) {
43  return true;
44  }
45 
46  return SendIcmpError(vm_itf);
47 }
48 
49 // Generate ICMP error
51  char data[ICMP_PAYLOAD_LEN];
52  uint16_t data_len = ntohs(pkt_info_->ip->ip_len);
53  if (data_len > ICMP_PAYLOAD_LEN)
54  data_len = ICMP_PAYLOAD_LEN;
55  memcpy(data, pkt_info_->ip, data_len);
56 
57  uint32_t src_ip = 0;
58  FlowKey key;
59  bool is_nat_flow = false;
60 
61  if (pkt_info_->agent_hdr.flow_index == (uint32_t)-1) {
62  // flow index is -1 for 255.255.255.255
63  if (pkt_info_->ip_daddr.to_v4().to_ulong() != 0xFFFFFFFF) {
65  return true;
66  }
67  src_ip = pkt_info_->ip_saddr.to_v4().to_ulong();
68  } else {
69  if (proto_->FlowIndexToKey(pkt_info_->agent_hdr.flow_index, &key, &is_nat_flow)
70  == false ||
71  key.family != Address::INET ||
72  ((!is_nat_flow) &&
73  (key.src_addr != pkt_info_->ip_saddr ||
74  key.dst_addr != pkt_info_->ip_daddr ||
75  key.protocol != pkt_info_->ip_proto ||
76  key.src_port != pkt_info_->sport ||
77  key.dst_port != pkt_info_->dport))) {
79  return true;
80  }
81 
82  src_ip = key.src_addr.to_v4().to_ulong();
83  }
84 
85  // Get IPAM to find default gateway
86  const VnIpam *ipam = intf->vn()->GetIpam(Ip4Address(src_ip));
87  if (ipam == NULL || ipam->default_gw.is_v4() == false) {
89  return true;
90  }
91 
92  // Retain the agent-header before ethernet header
93  uint16_t len = (char *)pkt_info_->eth - (char *)pkt_info_->pkt;
94  uint16_t buf_len = pkt_info_->max_pkt_len;
95 
96  // Form ICMP Packet with following
97  // EthHdr - IP Header - ICMP Header - User IP Packet(max 128 bytes)
98  char *ptr = (char *)pkt_info_->pkt;
99  len += EthHdr(ptr + len, buf_len - len,
100  agent()->vhost_interface()->mac(),
101  MacAddress(pkt_info_->eth->ether_shost),
102  ETHERTYPE_IP, intf->tx_vlan_id());
103 
104  uint16_t icmp_len = ICMP_UNREACH_HDR_LEN;
105  uint16_t ip_len = sizeof(struct ip) + icmp_len + data_len;
106  len += IpHdr(ptr + len, buf_len - len, ip_len,
107  htonl(ipam->default_gw.to_v4().to_ulong()),
108  htonl(src_ip), IPPROTO_ICMP, DEFAULT_IP_ID, DEFAULT_IP_TTL);
109 
110  char *icmp = ptr + len;
111  len += IcmpHdr(ptr + len, buf_len - len, ICMP_UNREACH,
112  ICMP_UNREACH_NEEDFRAG, 0, pkt_info_->agent_hdr.mtu);
113 
114  if (pkt_info_->agent_hdr.flow_index != (uint32_t)-1) {
115  // Its possible that user payload has gone thru NAT processing already.
116  // Restore the original fields from flow_key
117  struct ip *ip = (struct ip *)data;
118  uint16_t ip_hlen = ip->ip_hl * 4;
119 
120  ip->ip_src.s_addr = htonl(key.src_addr.to_v4().to_ulong());
121  ip->ip_dst.s_addr = htonl(key.dst_addr.to_v4().to_ulong());
122  ip->ip_sum = 0;
123  ip->ip_sum = Csum((uint16_t *)data, ip_hlen, 0);
124  if (ip->ip_p == IPPROTO_UDP) {
125  udphdr *udp = (udphdr *)(data + ip_hlen);
126  udp->uh_sport = ntohs(key.src_port);
127  udp->uh_dport = ntohs(key.dst_port);
128  } else if (ip->ip_p == IPPROTO_TCP) {
129  tcphdr *tcp = (tcphdr *)(data + ip_hlen);
130  tcp->th_sport = ntohs(key.src_port);
131  tcp->th_dport = ntohs(key.dst_port);
132  }
133  }
134  memcpy(ptr + len, data, data_len);
135  len += data_len;
136  IcmpChecksum(icmp, icmp_len + data_len);
137  pkt_info_->set_len(len);
138 
141  return true;
142 }
boost::asio::ip::address_v4 Ip4Address
Definition: address.h:15
@ INET
Definition: address.h:27
Definition: agent.h:361
InterfaceTable * interface_table() const
Definition: agent.h:468
const Interface * vhost_interface() const
Definition: agent.h:938
IcmpErrorProto * proto_
static const int ICMP_PAYLOAD_LEN
bool SendIcmpError(VmInterface *intf)
IcmpErrorHandler(Agent *agent, IcmpErrorProto *proto, boost::shared_ptr< PktInfo > info, boost::asio::io_context *io)
bool FlowIndexToKey(uint32_t index, FlowKey *key, bool *is_nat_flow)
void increment_interface_errors()
void increment_invalid_flow_index()
const Interface * FindInterface(size_t index) const
Definition: interface.cc:323
const MacAddress & mac() const
Definition: interface.h:134
Agent * agent() const
Definition: proto_handler.h:81
void Send(uint32_t itf, uint32_t vrf, uint16_t, PktHandler::PktModuleName)
void IpHdr(uint16_t len, in_addr_t src, in_addr_t dest, uint8_t protocol, uint16_t id, uint8_t ttl)
uint16_t IcmpHdr(char *buff, uint16_t buf_len, uint8_t type, uint8_t code, uint16_t word1, uint16_t word2)
int EthHdr(const MacAddress &src, const MacAddress &dest, const uint16_t proto)
uint16_t Csum(uint16_t *, std::size_t, uint32_t) const
void IcmpChecksum(char *buff, uint16_t buf_len)
boost::shared_ptr< PktInfo > pkt_info_
Definition: proto_handler.h:93
uint32_t GetInterfaceIndex() const
Definition: proto_handler.h:83
bool layer3_forwarding() const
uint16_t tx_vlan_id() const
const VnEntry * vn() const
const VnIpam * GetIpam(const IpAddress &ip) const
Definition: vn.cc:653
Definition: io_utils.cc:12
#define DEFAULT_IP_ID
Definition: pkt_handler.h:48
#define DEFAULT_IP_TTL
Definition: pkt_handler.h:44
#define ICMP_UNREACH_HDR_LEN
Definition: pkt_handler.h:51
uint8_t protocol
Definition: flow_entry.h:216
IpAddress src_addr
Definition: flow_entry.h:214
IpAddress dst_addr
Definition: flow_entry.h:215
uint16_t src_port
Definition: flow_entry.h:217
Address::Family family
Definition: flow_entry.h:212
uint16_t dst_port
Definition: flow_entry.h:218
Definition: vn.h:29
IpAddress default_gw
Definition: vn.h:32