/
proto_handler.cc
253 lines (207 loc) · 8.13 KB
/
proto_handler.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
*/
#include "base/os.h"
#include "vr_defs.h"
#include "pkt/proto_handler.h"
#include "pkt/pkt_init.h"
#include "pkt/packet_buffer.h"
///////////////////////////////////////////////////////////////////////////////
ProtoHandler::ProtoHandler(Agent *agent, boost::shared_ptr<PktInfo> info,
boost::asio::io_service &io)
: agent_(agent), pkt_info_(info), io_(io) {}
ProtoHandler::~ProtoHandler() {
}
uint32_t ProtoHandler::EncapHeaderLen() const {
return agent_->pkt()->pkt_handler()->EncapHeaderLen();
}
// send packet to the pkt0 interface
void ProtoHandler::Send(uint32_t itf, uint32_t vrf, uint16_t cmd,
PktHandler::PktModuleName mod) {
Send(itf, vrf, cmd, 0, 0, mod);
}
void ProtoHandler::Send(uint32_t itf, uint32_t vrf, uint16_t cmd,
uint32_t param1, uint32_t param2,
PktHandler::PktModuleName mod) {
// If pkt_info_->pkt is non-NULL, pkt is freed in destructor of pkt_info_
if (agent_->pkt()->pkt_handler() == NULL) {
return;
}
AgentHdr hdr(itf, vrf, cmd, param1, param2);
agent_->pkt()->pkt_handler()->Send(hdr, pkt_info_->packet_buffer_ptr());
}
int ProtoHandler::EthHdr(char *buff, uint16_t len, const MacAddress &src,
const MacAddress &dest, const uint16_t proto,
uint16_t vlan_id) {
struct ether_header *eth = (struct ether_header *)buff;
uint16_t encap_len = sizeof(struct ether_header);
if (vlan_id != VmInterface::kInvalidVlanId) {
encap_len += 4;
}
if (len < encap_len) {
return 0;
}
dest.ToArray(eth->ether_dhost, sizeof(eth->ether_dhost));
src.ToArray(eth->ether_shost, sizeof(eth->ether_shost));
uint16_t *ptr = (uint16_t *) (buff + ETHER_ADDR_LEN * 2);
if (vlan_id != VmInterface::kInvalidVlanId) {
*ptr = htons(ETHERTYPE_VLAN);
ptr++;
*ptr = htons(vlan_id & 0xFFF);
ptr++;
}
*ptr = htons(proto);
return encap_len;
}
int ProtoHandler::EthHdr(const MacAddress &src, const MacAddress &dest,
const uint16_t proto) {
return EthHdr((char *)pkt_info_->eth, sizeof(struct ether_header), src,
dest, proto, VmInterface::kInvalidVlanId);
}
int ProtoHandler::EthHdr(char *buff, uint16_t len, const Interface *interface,
const MacAddress &src, const MacAddress &dest,
const uint16_t proto) {
uint16_t vlan_id = VmInterface::kInvalidVlanId;
if (interface && interface->type() == Interface::VM_INTERFACE) {
vlan_id = static_cast<const VmInterface *>(interface)->tx_vlan_id();
}
return EthHdr(buff, len, src, dest, proto, vlan_id);
}
int ProtoHandler::EthHdr(char *buff, uint16_t len, uint32_t ifindex,
const MacAddress &src, const MacAddress &dest,
const uint16_t proto) {
const Interface *intf = agent()->interface_table()->FindInterface(ifindex);
return EthHdr(buff, len, intf, src, dest, proto);
}
void ProtoHandler::VlanHdr(uint8_t *ptr, uint16_t tci) {
vlanhdr *vlan = reinterpret_cast<vlanhdr *>(ptr);
vlan->tpid = htons(0x8100);
vlan->tci = htons(tci);
vlan += 1;
vlan->tpid = htons(0x800);
return;
}
uint16_t ProtoHandler::IpHdr(char *buff, uint16_t buf_len, uint16_t len,
in_addr_t src, in_addr_t dest, uint8_t protocol,
uint16_t id, uint8_t ttl) {
struct ip *ip = (struct ip *)buff;
if (buf_len < sizeof(struct ip))
return 0;
ip->ip_hl = 5;
ip->ip_v = 4;
ip->ip_tos = 0;
ip->ip_len = htons(len);
ip->ip_id = htons(id);
ip->ip_off = 0;
ip->ip_ttl = ttl;
ip->ip_p = protocol;
ip->ip_sum = 0;
ip->ip_src.s_addr = src;
ip->ip_dst.s_addr = dest;
ip->ip_sum = Csum((uint16_t *)ip, ip->ip_hl * 4, 0);
return sizeof(struct ip);
}
void ProtoHandler::IpHdr(uint16_t len, in_addr_t src, in_addr_t dest,
uint8_t protocol, uint16_t id, uint8_t ttl) {
IpHdr((char *)pkt_info_->ip, sizeof(struct ip), len,
src, dest, protocol, id, ttl);
}
void ProtoHandler::Ip6Hdr(ip6_hdr *ip, uint16_t plen, uint8_t next_header,
uint8_t hlim, uint8_t *src, uint8_t *dest) {
ip->ip6_flow = htonl(0x60000000); // version 6, TC and Flow set to 0
ip->ip6_plen = htons(plen);
ip->ip6_nxt = next_header;
ip->ip6_hlim= hlim;
memcpy(ip->ip6_src.s6_addr, src, 16);
memcpy(ip->ip6_dst.s6_addr, dest, 16);
}
void ProtoHandler::FillUdpHdr(udphdr *udp, uint16_t len,
uint16_t src_port, uint16_t dest_port) {
udp->uh_sport = htons(src_port);
udp->uh_dport = htons(dest_port);
udp->uh_ulen = htons(len);
udp->uh_sum = 0;
}
uint16_t ProtoHandler::UdpHdr(udphdr *udp, uint16_t buf_len, uint16_t len,
in_addr_t src, uint16_t src_port, in_addr_t dest,
uint16_t dest_port) {
if (buf_len < sizeof(udphdr))
return 0;
FillUdpHdr(udp, len, src_port, dest_port);
#ifdef VNSW_AGENT_UDP_CSUM
udp->check = UdpCsum(src, dest, len, pkt_info_->transp.udp);
#endif
return sizeof(udphdr);
}
void ProtoHandler::UdpHdr(uint16_t len, in_addr_t src, uint16_t src_port,
in_addr_t dest, uint16_t dest_port) {
UdpHdr(pkt_info_->transp.udp, sizeof(udphdr), len, src, src_port,
dest, dest_port);
}
uint16_t ProtoHandler::IcmpHdr(char *buff, uint16_t buf_len, uint8_t type,
uint8_t code, uint16_t word1, uint16_t word2) {
struct icmp *hdr = ((struct icmp *)buff);
if (buf_len < sizeof(hdr))
return 0;
bzero(hdr, sizeof(struct icmp));
hdr->icmp_type = type;
hdr->icmp_code = code;
assert(type == ICMP_UNREACH);
hdr->icmp_nextmtu = htons(word2);
hdr->icmp_cksum = 0;
if (type == ICMP_UNREACH)
return ICMP_UNREACH_HDR_LEN;
assert(0);
return 0;
}
void ProtoHandler::IcmpChecksum(char *buff, uint16_t buf_len) {
struct icmp *hdr = ((struct icmp *)buff);
hdr->icmp_cksum = Csum((uint16_t *)buff, buf_len, 0);
}
void ProtoHandler::UdpHdr(uint16_t len, const uint8_t *src, uint16_t src_port,
const uint8_t *dest, uint16_t dest_port,
uint8_t next_hdr) {
FillUdpHdr(pkt_info_->transp.udp, len, src_port, dest_port);
pkt_info_->transp.udp->uh_sum = Ipv6Csum(src, dest, len, next_hdr,
(uint16_t *)pkt_info_->transp.udp);
}
uint32_t ProtoHandler::Sum(uint16_t *ptr, std::size_t len, uint32_t sum) {
while (len > 1) {
sum += *ptr++;
len -= 2;
if (sum & 0x80000000)
sum = (sum & 0xFFFF) + (sum >> 16);
}
if (len > 0)
sum += *(uint8_t *)ptr;
return sum;
}
uint16_t ProtoHandler::Csum(uint16_t *ptr, std::size_t len, uint32_t sum) {
sum = Sum(ptr, len, sum);
while (sum >> 16)
sum = (sum & 0xFFFF) + (sum >> 16);
return ~sum;
}
uint16_t ProtoHandler::UdpCsum(in_addr_t src, in_addr_t dest,
std::size_t len, udphdr *udp) {
uint32_t sum = 0;
PseudoUdpHdr phdr(src, dest, IPPROTO_UDP, htons(len));
sum = Sum((uint16_t *)&phdr, sizeof(PseudoUdpHdr), sum);
return Csum((uint16_t *)udp, len, sum);
}
uint16_t ProtoHandler::Ipv6Csum(const uint8_t *src, const uint8_t *dest,
uint16_t plen, uint8_t next_hdr, uint16_t *hdr) {
uint32_t len = htonl((uint32_t)plen);
uint32_t next = htonl((uint32_t)next_hdr);
uint32_t pseudo = 0;
pseudo = Sum((uint16_t *)src, 16, 0);
pseudo = Sum((uint16_t *)dest, 16, pseudo);
pseudo = Sum((uint16_t *)&len, 4, pseudo);
pseudo = Sum((uint16_t *)&next, 4, pseudo);
return Csum(hdr, plen, pseudo);
}
uint16_t ProtoHandler::Icmpv6Csum(const uint8_t *src, const uint8_t *dest,
icmp6_hdr *icmp, uint16_t plen) {
return Ipv6Csum(src, dest, plen, IPPROTO_ICMPV6, (uint16_t *)icmp);
}
///////////////////////////////////////////////////////////////////////////////