aboutsummaryrefslogtreecommitdiffstats
path: root/lib/libtle_glue/icmp.c
blob: aba1c4b2be9464c74188856cbf79cae83b02c305 (plain)
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
 * Copyright (c) 2018 Ant Financial Services Group.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <time.h>
#include <netinet/icmp6.h>

#include <rte_common.h>
#include <rte_byteorder.h>
#include <rte_ethdev.h>
#include <rte_icmp.h>
#include <rte_ip.h>

#include "log.h"
#include "ctx.h"
#include "internal.h"

#define ICMP_ECHOREPLY          0    /* Echo Reply                      */
#define ICMP_ECHO               8    /* Echo Request                    */
#define ICMP_TIMESTAMP          13   /* Timestamp Request               */
#define ICMP_TIMESTAMPREPLY     14   /* Timestamp Reply                 */

/* Codes for TIME_EXCEEDED. */
#define ICMP_EXC_TTL            0    /* TTL count exceeded              */
#define ICMP_EXC_FRAGTIME       1    /* Fragment Reass time exceeded    */

/* Parameters used to convert the timespec values */
#define SECONDS_PER_DAY         86400L
#define MSEC_PER_SEC            1000L
#define USEC_PER_MSEC           1000L
#define NSEC_PER_USEC           1000L
#define NSEC_PER_MSEC           (NSEC_PER_USEC * USEC_PER_MSEC)

#define IS_IPV4_BCAST(x)        ((x) == (uint32_t)0xFFFFFFFF)

struct icmp_pkt {
	struct icmp_hdr icmp_h;
	uint32_t times[3];
};

/* Return remainder for ``dividend / divisor`` */
static inline uint32_t
div_uint64_rem(uint64_t dividend, uint32_t divisor)
{
	return dividend % divisor;
}

/* Return milliseconds since midnight (UTC) in network byte order. */
static uint32_t
current_timestamp(void)
{
	struct timespec ts;
	uint32_t msecs;
	uint32_t secs;

	(void)clock_gettime(CLOCK_REALTIME, &ts);

	/* Get secs since midnight. */
	secs = div_uint64_rem(ts.tv_sec, SECONDS_PER_DAY);
	/* Convert to msecs. */
	msecs = secs * MSEC_PER_SEC;
	/* Convert nsec to msec. */
	msecs += (uint32_t)ts.tv_nsec / NSEC_PER_MSEC;

	/* Convert to network byte order. */
	return rte_cpu_to_be_32(msecs);
}

/*
 * Process the checksum of an ICMP packet. The checksum field must be set
 * to 0 by the caller.
 */
static uint16_t
icmp_cksum(const struct icmp_hdr *icmp, uint32_t data_len)
{
	uint16_t cksum;

	cksum = rte_raw_cksum(icmp, sizeof(struct icmp_hdr) + data_len);
	return (cksum == 0xffff) ? cksum : ~cksum;
}

/**
 * Receive and handle an ICMP packet.
 *
 * @param ctx
 *   The pointer to the glue context.
 * @param pkt
 *   The pointer to the raw packet data.
 * @param l2_len
 *   The the size of the l2 header.
 * @return
 *   MUST return NULL now. :-)
 */
struct rte_mbuf *
icmp_recv(struct glue_ctx *ctx, struct rte_mbuf *pkt,
	  uint32_t l2_len, uint32_t l3_len)
{
	struct ether_addr eth_addr;
	struct icmp_pkt *icmp_pkt;
	struct ether_hdr *eth_h;
	struct icmp_hdr *icmp_h;
	struct ipv4_hdr *ip_h;
	uint32_t ip_addr;
	uint32_t cksum;

	eth_h = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
	ip_h = (struct ipv4_hdr *) ((char *)eth_h + l2_len);

	icmp_h = (struct icmp_hdr *)((char *)ip_h + l3_len);
	if (icmp_h->icmp_type != IP_ICMP_ECHO_REQUEST &&
	    icmp_h->icmp_type != ICMP_TIMESTAMP)
		goto drop_pkt;

	icmp_pkt = (struct icmp_pkt *)icmp_h;

	ether_addr_copy(&eth_h->s_addr, &eth_addr);
	ether_addr_copy(&eth_h->d_addr, &eth_h->s_addr);
	ether_addr_copy(&eth_addr, &eth_h->d_addr);

	/*
	 * Similar to Linux implementation, we silently drop the broadcast or
	 * multicast ICMP pakcets.
	 *
	 *     RFC 1122: 3.2.2.6 An ICMP_ECHO to broadcast MAY be
	 *         silently ignored.
	 *     RFC 1122: 3.2.2.8 An ICMP_TIMESTAMP MAY be silently
	 *         discarded if to broadcast/multicast.
	 */
	ip_addr = rte_be_to_cpu_32(ip_h->dst_addr);
	if (IS_IPV4_MCAST(ip_addr) || IS_IPV4_BCAST(ip_addr))
		goto drop_pkt;

	ip_addr = ip_h->src_addr;
	ip_h->src_addr = ip_h->dst_addr;
	ip_h->dst_addr = ip_addr;

	if (icmp_h->icmp_type == IP_ICMP_ECHO_REQUEST &&
	    icmp_h->icmp_code == 0) {

		/* Must clear checksum field before calling the helper. */
		ip_h->hdr_checksum = 0;
		ip_h->hdr_checksum = rte_ipv4_cksum(ip_h);

		icmp_h->icmp_type = IP_ICMP_ECHO_REPLY;
		icmp_h->icmp_code = 0;

		/*
		 * Fix me: the data part of an ICMP echo request/reply
		 * message is implementation specific, we don't know
		 * how to verify or calculate the checksum.
		 *
		 * Need to see BSD or LINUX implementation.
		 */
		cksum = ~icmp_h->icmp_cksum & 0xffff;
		cksum += ~rte_cpu_to_be_16(IP_ICMP_ECHO_REQUEST << 8) & 0xffff;
		cksum += rte_cpu_to_be_16(IP_ICMP_ECHO_REPLY << 8);
		cksum = (cksum & 0xffff) + (cksum >> 16);
		cksum = (cksum & 0xffff) + (cksum >> 16);
		icmp_h->icmp_cksum = ~cksum;

	} else if (icmp_h->icmp_type == ICMP_TIMESTAMP &&
		   icmp_h->icmp_code == 0) {

		/*
		 * RFC 1122: 3.2.2.8 MAY implement ICMP timestamp requests.
		 *     SHOULD be in the kernel for minimum random latency.
		 *     MUST be accurate to a few minutes.
		 *     MUST be updated at least at 15Hz.
		 */
		icmp_h->icmp_type = ICMP_TIMESTAMPREPLY;
		icmp_h->icmp_code = 0;
		icmp_pkt->times[1] = current_timestamp();
		icmp_pkt->times[2] = icmp_pkt->times[1];

		icmp_h->icmp_cksum = 0;
		/* the data part of an ICMP timestamp reply is 12 bytes. */
		icmp_h->icmp_cksum = icmp_cksum(icmp_h, 12);
	} else
		goto drop_pkt;

	if (pkt->pkt_len < ETHER_MIN_LEN)
		rte_pktmbuf_append(pkt, ETHER_MIN_LEN - pkt->pkt_len);

	if (rte_eth_tx_burst(ctx->port_id, ctx->queue_id, &pkt, 1))
		GLUE_LOG(DEBUG, "Send ICMP echo reply OK");

	return NULL;

drop_pkt:
	rte_pktmbuf_free(pkt);
	return NULL;
}

/**
 * Receive and handle an ICMPv6 packet.
 *
 * @param ctx
 *   The pointer to the glue context.
 * @param pkt
 *   The pointer to the raw packet data.
 * @param l2_len
 *   The the size of the l2 header.
 * @return
 *   MUST return NULL now. :-)
 */
struct rte_mbuf *
icmp6_recv(struct glue_ctx *ctx, struct rte_mbuf *pkt,
	   uint32_t l2_len, uint32_t l3_len)
{
	struct ether_addr eth_addr;
	struct ether_hdr *eth_h;
	struct icmp6_hdr *icmp6_h;
	struct ipv6_hdr *ipv6_h;
	struct in6_addr ipv6_addr;
	uint32_t cksum;

	eth_h = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
	ipv6_h = (struct ipv6_hdr *) ((char *)eth_h + l2_len);

	icmp6_h = (struct icmp6_hdr *)((char *)ipv6_h + l3_len);

	/* NDP pkt */
	if ((icmp6_h->icmp6_type == ND_NEIGHBOR_SOLICIT ||
	     icmp6_h->icmp6_type == ND_NEIGHBOR_ADVERT) &&
	    icmp6_h->icmp6_code == 0)
		return ndp_recv(ctx, pkt, l2_len, l3_len);

	/* only support ECHO now, other types of pkts are dropped */
	if ((icmp6_h->icmp6_type != ICMP6_ECHO_REQUEST &&
	     icmp6_h->icmp6_type != ICMP6_ECHO_REPLY) ||
	    icmp6_h->icmp6_code != 0)
		goto drop_pkt;

	ether_addr_copy(&eth_h->s_addr, &eth_addr);
	ether_addr_copy(&eth_h->d_addr, &eth_h->s_addr);
	ether_addr_copy(&eth_addr, &eth_h->d_addr);

	/*
	 * Now, we silently drop the anycast or multicast ICMP pakcets.
	 * But it does not conform to RFC 4443. Maybe fix it latter.
	 *
	 *     RFC 4443: 4.2  An Echo Reply SHOULD be sent in response to an
	 *     Echo Request message sent to an IPv6 multicast or anycast address.
	 *     In this case, thesource address of the reply MUST be a unicast
	 *     address belonging to the interface on which the Echo Request
	 *     message was received.
	 */
	switch (icmp6_h->icmp6_type) {
	case ICMP6_ECHO_REQUEST:
		if (memcmp(ipv6_h->dst_addr, &ctx->ipv6,
			   sizeof(struct in6_addr)) != 0)
			goto drop_pkt;

		rte_memcpy(&ipv6_addr, ipv6_h->src_addr,
			   sizeof(struct in6_addr));
		rte_memcpy(ipv6_h->src_addr, ipv6_h->dst_addr,
			   sizeof(struct in6_addr));
		rte_memcpy(ipv6_h->dst_addr, &ipv6_addr,
			   sizeof(struct in6_addr));

		icmp6_h->icmp6_type = ICMP6_ECHO_REPLY;

		cksum = ~icmp6_h->icmp6_cksum & 0xffff;
		cksum += ~rte_cpu_to_be_16(ICMP6_ECHO_REQUEST << 8) & 0xffff;
		cksum += rte_cpu_to_be_16(ICMP6_ECHO_REPLY << 8);
		cksum = (cksum & 0xffff) + (cksum >> 16);
		cksum = (cksum & 0xffff) + (cksum >> 16);
		icmp6_h->icmp6_cksum = ~cksum;

		break;
	default:
		goto drop_pkt;
	}

	if (pkt->pkt_len < ETHER_MIN_LEN)
		rte_pktmbuf_append(pkt, ETHER_MIN_LEN - pkt->pkt_len);

	if (rte_eth_tx_burst(ctx->port_id, ctx->queue_id, &pkt, 1))
		GLUE_LOG(DEBUG, "Send ICMP echo reply OK");

	return NULL;

drop_pkt:
	rte_pktmbuf_free(pkt);
	return NULL;
}