OpenSDN source code
traceroute.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
6 #include <stdint.h>
7 #include <netinet/udp.h>
8 #include <netinet/tcp.h>
9 #include <netinet/icmp6.h>
10 #include "vr_defs.h"
11 #include "cmn/agent_cmn.h"
12 #include "oper/nexthop.h"
13 #include "oper/route_common.h"
14 #include "oper/mirror_table.h"
15 #include "pkt/proto.h"
16 #include "pkt/proto_handler.h"
17 #include "diag/diag_types.h"
18 #include "diag/diag_pkt_handler.h"
19 #include "diag/diag.h"
20 #include "diag/traceroute.h"
21 
22 void
23 TraceRoute::SendSandeshReply(const std::string &address,
24  const std::string &context,
25  bool more) {
26  TraceRouteResp *resp = new TraceRouteResp();
27  resp->set_hop(address);
28 
29  resp->set_context(context);
30  resp->set_more(more);
31  resp->Response();
32 }
33 
34 TraceRoute::TraceRoute(const TraceRouteReq *trace_route_req,
35  DiagTable *diag_table) :
36  DiagEntry(trace_route_req->get_source_ip(), trace_route_req->get_dest_ip(),
37  trace_route_req->get_protocol(), trace_route_req->get_source_port(),
38  trace_route_req->get_dest_port(), trace_route_req->get_vrf_name(),
39  trace_route_req->get_interval() * 100,
40  trace_route_req->get_max_attempts(), diag_table),
41  done_(false), ttl_(2),
42  max_ttl_(trace_route_req->get_max_hops()),
43  context_(trace_route_req->context()) {
44 }
45 
47 }
48 
50  data->op_ = htonl(AgentDiagPktData::DIAG_REQUEST);
51  data->key_ = htons(key_);
52  data->seq_no_ = htonl(seq_no_);
53  memset(data->data_, 0, sizeof(data->data_));
54  // data->rtt_ = microsec_clock::universal_time();
55 }
56 
58  Agent *agent = diag_table_->agent();
59 
61  if (sip_.is_v4()) {
63  } else {
65  }
66  AgentRoute *rt = table->FindRoute(sip_);
67  if (!rt) return;
68 
69  const NextHop *nh = rt->GetActiveNextHop();
70  if (!nh || nh->GetType() != NextHop::INTERFACE) return;
71 
72  const InterfaceNH *intf_nh;
73  intf_nh = static_cast<const InterfaceNH *>(nh);
74 
75  uint32_t intf_id = intf_nh->GetInterface()->id();
76  uint32_t vrf_id = agent->vrf_table()->FindVrfFromName(vrf_name_)->vrf_id();
77 
78  //Allocate buffer to hold packet
79  boost::shared_ptr<PktInfo> pkt_info(new PktInfo(agent, kBufferSize,
80  PktHandler::DIAG, 0));
81  uint8_t *msg = pkt_info->packet_buffer()->data();
82  memset(msg, 0, kBufferSize);
83 
84  DiagPktHandler *pkt_handler =
85  new DiagPktHandler(agent, pkt_info,
86  *(agent->event_manager())->io_service());
87  uint16_t len = sizeof(AgentDiagPktData);
88  uint8_t *data = NULL;
89  if (sip_.is_v4()) {
90  //Update pointers to ethernet header, ip header and l4 header
91  pkt_info->UpdateHeaderPtr();
92  switch (proto_) {
93  case IPPROTO_TCP:
94  len += sizeof(tcphdr);
95  data = (uint8_t *)pkt_handler->pkt_info()->transp.tcp + sizeof(tcphdr);
96  pkt_handler->TcpHdr(htonl(sip_.to_v4().to_ulong()), sport_,
97  htonl(dip_.to_v4().to_ulong()), dport_,
98  false, rand(), len);
99  pkt_handler->pkt_info()->transp.tcp->check = 0xffff;
100  break;
101 
102  case IPPROTO_UDP:
103  len += sizeof(udphdr);
104  data = (uint8_t *)pkt_handler->pkt_info()->transp.udp + sizeof(udphdr);
105  pkt_handler->UdpHdr(len, sip_.to_v4().to_ulong(), sport_,
106  dip_.to_v4().to_ulong(), dport_);
107  pkt_handler->pkt_info()->transp.udp->check = 0xffff;
108  break;
109 
110  case IPPROTO_ICMP:
111  len += 8;
112  data = (uint8_t *)pkt_handler->pkt_info()->transp.icmp + 8;
113  pkt_handler->pkt_info()->transp.icmp->icmp_type = ICMP_ECHO;
114  pkt_handler->pkt_info()->transp.icmp->icmp_code = 0;
115  pkt_handler->pkt_info()->transp.icmp->icmp_cksum = 0xffff;
116  break;
117  }
118  FillHeader((AgentDiagPktData *)data);
119  len += sizeof(struct ip);
120  pkt_handler->IpHdr(len, ntohl(sip_.to_v4().to_ulong()),
121  ntohl(dip_.to_v4().to_ulong()),
122  proto_, key_, ttl_);
123  len += sizeof(ether_header);
124  pkt_handler->EthHdr(agent->vhost_interface()->mac(),
125  agent->vrrp_mac(), ETHERTYPE_IP);
126  } else {
127  pkt_info->eth = (struct ether_header *)(pkt_handler->pkt_info()->pkt);
128  pkt_info->ip6 = (struct ip6_hdr *)(pkt_info->eth + 1);
129  switch (proto_) {
130  case IPPROTO_TCP:
131  pkt_info->transp.tcp = (struct tcphdr *)(pkt_info->ip6 + 1);
132  len += sizeof(tcphdr);
133  data = (uint8_t *)pkt_handler->pkt_info()->transp.tcp + sizeof(tcphdr);
134  pkt_handler->TcpHdr(len+sizeof(tcphdr),
135  sip_.to_v6().to_bytes().data(), sport_,
136  dip_.to_v6().to_bytes().data(), dport_,
137  false, rand(), IPPROTO_TCP);
138  pkt_handler->pkt_info()->transp.tcp->check = 0xffff;
139  break;
140 
141  case IPPROTO_UDP:
142  pkt_info->transp.udp = (struct udphdr *)(pkt_info->ip6 + 1);
143  len += sizeof(udphdr);
144  data = (uint8_t *)pkt_handler->pkt_info()->transp.udp + sizeof(udphdr);
145  pkt_handler->UdpHdr(len + sizeof(udphdr),
146  sip_.to_v6().to_bytes().data(), sport_,
147  dip_.to_v6().to_bytes().data(), dport_,
148  IPPROTO_UDP);
149  pkt_handler->pkt_info()->transp.udp->check = 0xffff;
150  break;
151 
152  case IPPROTO_ICMPV6:
153  len += 8;
154  data = (uint8_t *)pkt_handler->pkt_info()->transp.icmp + 8;
155  pkt_handler->pkt_info()->transp.icmp6->icmp6_type = ICMP6_ECHO_REQUEST;
156  pkt_handler->pkt_info()->transp.icmp6->icmp6_code = 0;
157  pkt_handler->pkt_info()->transp.icmp6->icmp6_cksum = 0xffff;
158  break;
159  }
160  FillHeader((AgentDiagPktData *)data);
161  len += sizeof(struct ip6_hdr);
162  pkt_handler->Ip6Hdr(pkt_info->ip6,
163  len + sizeof(udphdr) + sizeof(struct ip6_hdr),
164  IPPROTO_UDP, ttl_, sip_.to_v6().to_bytes().data(),
165  dip_.to_v6().to_bytes().data());
166  len += sizeof(ether_header);
167  pkt_handler->EthHdr(agent->vhost_interface()->mac(),
168  agent->vrrp_mac(), ETHERTYPE_IPV6);
169  }
170  //Increment the attempt count
171  seq_no_++;
172 
173  //Send request out
174  pkt_handler->pkt_info()->set_len(len);
175  pkt_handler->Send(intf_id, vrf_id, AgentHdr::TX_ROUTE,
176  CMD_PARAM_PACKET_CTRL, CMD_PARAM_1_DIAG, PktHandler::DIAG);
177  delete pkt_handler;
178  return;
179 }
180 
181 // if timed out max times for a TTL, reply and increment ttl
182 void TraceRoute::RequestTimedOut(uint32_t seqno) {
183  if (seq_no_ >= GetMaxAttempts()) {
184  std::string address;
185  for (uint32_t i = 0; i < GetMaxAttempts(); i++)
186  address += "* ";
187 
188  done_ = ((ttl_ >= max_ttl_) ? true : false);
189  SendSandeshReply(address, context_, !done_);
190  IncrementTtl();
191  }
192 }
193 
194 // Ready to send a response and increment ttl
196  if (ttl_ >= max_ttl_) {
197  handler->set_done(true);
198  done_ = true;
199  }
200  SendSandeshReply(handler->GetAddress(), context_, !handler->IsDone());
201  IncrementTtl();
202 }
203 
204 // Reply with local node as the first hop
206  SendSandeshReply(diag_table_->agent()->router_id().to_string(),
207  context_, true);
208 }
209 
211 }
212 
214  return done_;
215 }
216 
218  ttl_++;
219  seq_no_ = 0;
220 }
221 
222 void TraceRouteReq::HandleRequest() const {
223  std::string err_str;
224  boost::system::error_code ec;
225  TraceRoute *trace_route = NULL;
226 
227  {
228  IpAddress sip(IpAddress::from_string(get_source_ip(), ec));
229  if (ec.failed()) {
230  err_str = "Invalid source IP";
231  goto error;
232  }
233 
234  IpAddress dip(IpAddress::from_string(get_dest_ip(), ec));
235  if (ec.failed()) {
236  err_str = "Invalid destination IP";
237  goto error;
238  }
239 
240  uint8_t proto = get_protocol();
241  if (proto != IPPROTO_TCP && proto != IPPROTO_UDP && proto != IPPROTO_ICMP) {
242  err_str = "Invalid protocol - Supported protocols are TCP, UDP and ICMP";
243  goto error;
244  }
245 
246  if (Agent::GetInstance()->vrf_table()->FindVrfFromName(get_vrf_name()) == NULL) {
247  err_str = "Invalid VRF";
248  goto error;
249  }
250 
251  const NextHop *nh = NULL;
252  Agent *agent = Agent::GetInstance();
254  if (sip.is_v4()) {
255  table = agent->vrf_table()->GetInet4UnicastRouteTable(get_vrf_name());
256  } else {
257  table = agent->vrf_table()->GetInet6UnicastRouteTable(get_vrf_name());
258  }
259  AgentRoute *rt = table->FindRoute(sip);
260  if (rt) {
261  nh = rt->GetActiveNextHop();
262  }
263  if (!nh || nh->GetType() != NextHop::INTERFACE) {
264  err_str = "Source VM is not present in this server";
265  goto error;
266  }
267 
268  rt = table->FindRoute(dip);
269  if (rt) {
270  nh = rt->GetActiveNextHop();
271  if (nh && nh->GetType() == NextHop::INTERFACE) {
272  // Dest VM is also local
273  TraceRoute::SendSandeshReply(agent->router_id().to_string(),
274  context(), false);
275  return;
276  }
277  }
278  }
279 
280  trace_route = new TraceRoute(this, Agent::GetInstance()->diag_table());
281  trace_route->ReplyLocalHop();
282  trace_route->Init();
283  return;
284 
285 error:
286  TraceRouteErrResp *resp = new TraceRouteErrResp;
287  resp->set_error_response(err_str);
288  resp->set_context(context());
289  resp->Response();
290  return;
291 }
boost::asio::ip::address IpAddress
Definition: address.h:14
Base class for all Route entries in agent.
Definition: agent_route.h:225
const NextHop * GetActiveNextHop() const
Definition: agent_route.cc:883
Definition: agent.h:361
VrfTable * vrf_table() const
Definition: agent.h:488
EventManager * event_manager() const
Definition: agent.h:1106
static const MacAddress & vrrp_mac()
Definition: agent.h:442
static Agent * GetInstance()
Definition: agent.h:439
const Interface * vhost_interface() const
Definition: agent.h:938
Ip4Address router_id() const
Definition: agent.h:669
Definition: diag.h:22
uint32_t seq_no_
Definition: diag.h:71
DiagKey key_
Definition: diag.h:66
DiagTable * diag_table_
Definition: diag.h:65
void Init()
Definition: diag.cc:48
std::string vrf_name_
Definition: diag.h:62
uint16_t dport_
Definition: diag.h:61
IpAddress dip_
Definition: diag.h:58
uint16_t sport_
Definition: diag.h:60
IpAddress sip_
Definition: diag.h:57
uint8_t proto_
Definition: diag.h:59
uint32_t GetMaxAttempts()
Definition: diag.h:43
void TcpHdr(in_addr_t, uint16_t, in_addr_t, uint16_t, bool, uint32_t, uint16_t)
const std::string & GetAddress() const
bool IsDone() const
void set_done(bool done)
Agent * agent() const
Definition: diag.h:118
InetUnicastRouteEntry * FindRoute(const IpAddress &ip)
const Interface * GetInterface() const
Definition: nexthop.h:1294
const MacAddress & mac() const
Definition: interface.h:134
const uint32_t id() const
Definition: interface.h:126
@ INTERFACE
Definition: nexthop.h:352
Type GetType() const
Definition: nexthop.h:406
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)
void UdpHdr(uint16_t len, in_addr_t src, uint16_t src_port, in_addr_t dest, uint16_t dest_port)
int EthHdr(const MacAddress &src, const MacAddress &dest, const uint16_t proto)
PktInfo * pkt_info() const
Definition: proto_handler.h:89
void Ip6Hdr(ip6_hdr *ip, uint16_t plen, uint8_t next_header, uint8_t hlim, uint8_t *src, uint8_t *dest)
virtual ~TraceRoute()
Definition: traceroute.cc:46
void RequestTimedOut(uint32_t seqno)
Definition: traceroute.cc:182
uint16_t max_ttl_
Definition: traceroute.h:38
void SendSummary()
Definition: traceroute.cc:210
std::string context_
Definition: traceroute.h:39
static void SendSandeshReply(const std::string &address, const std::string &context, bool more)
Definition: traceroute.cc:23
uint8_t ttl_
Definition: traceroute.h:37
static const int kBufferSize
Definition: traceroute.h:16
bool done_
Definition: traceroute.h:36
virtual void SendRequest()
Definition: traceroute.cc:57
TraceRoute(const TraceRouteReq *req, DiagTable *diag_table)
Definition: traceroute.cc:34
void IncrementTtl()
Definition: traceroute.cc:217
void ReplyLocalHop()
Definition: traceroute.cc:205
virtual bool IsDone()
Definition: traceroute.cc:213
virtual void HandleReply(DiagPktHandler *handler)
Definition: traceroute.cc:195
void FillHeader(AgentDiagPktData *data)
Definition: traceroute.cc:49
const uint32_t vrf_id() const
Definition: vrf.h:103
VrfEntry * FindVrfFromName(const string &name)
Definition: vrf.cc:874
InetUnicastAgentRouteTable * GetInet6UnicastRouteTable(const std::string &vrf_name)
Definition: vrf.cc:923
InetUnicastAgentRouteTable * GetInet4UnicastRouteTable(const std::string &vrf_name)
Definition: vrf.cc:899
uint32_t op_
Definition: diag.h:83
uint32_t seq_no_
Definition: diag.h:85
DiagEntry::DiagKey key_
Definition: diag.h:84
@ DIAG_REQUEST
Definition: diag.h:76
char data_[8]
Definition: diag.h:86
uint8_t * pkt
Definition: pkt_handler.h:379
struct icmp * icmp
Definition: pkt_handler.h:428
struct udphdr * udp
Definition: pkt_handler.h:427
union PktInfo::@8 transp
void set_len(uint32_t len)
struct icmp6_hdr * icmp6
Definition: pkt_handler.h:429
struct tcphdr * tcp
Definition: pkt_handler.h:426