forked from iqiyi/dpvs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathicmp.c
327 lines (281 loc) · 8.68 KB
/
icmp.c
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
* DPVS is a software load balancer (Virtual Server) based on DPDK.
*
* Copyright (C) 2017 iQIYI (www.iqiyi.com).
* All Rights Reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#include <assert.h>
#include "ipv4.h"
#include "icmp.h"
#include "netinet/in.h"
#include "netinet/ip_icmp.h"
#define ICMP
#define RTE_LOGTYPE_ICMP RTE_LOGTYPE_USER1
struct icmp_ctrl {
int (*handler)(struct rte_mbuf *mbuf);
bool is_error; /* ICMP error message */
};
#define MAX_ICMP_CTRL 256 /* cannot change */
#ifdef CONFIG_DPVS_ICMP_DEBUG
static void icmp_dump_hdr(const struct rte_mbuf *mbuf)
{
struct icmp_hdr *ich = rte_pktmbuf_mtod(mbuf, struct icmp_hdr *);
lcoreid_t lcore = rte_lcore_id();
fprintf(stderr, "lcore %d port %d icmp type %u code %u id %u seq %u\n",
lcore, mbuf->port, ich->icmp_type, ich->icmp_code,
ntohs(ich->icmp_ident), ntohs(ich->icmp_seq_nb));
return;
}
#endif
static int icmp_echo(struct rte_mbuf *mbuf)
{
struct ipv4_hdr *iph = mbuf->userdata;
struct icmp_hdr *ich = rte_pktmbuf_mtod(mbuf, struct icmp_hdr *);
uint16_t csum;
struct flow4 fl4;
if (ich->icmp_type != IP_ICMP_ECHO_REQUEST || ich->icmp_code != 0) {
RTE_LOG(WARNING, ICMP, "%s: not echo-request\n", __func__);
goto errout;
}
if (mbuf_may_pull(mbuf, mbuf->pkt_len) != 0)
goto errout;
if (rte_raw_cksum(ich, mbuf->pkt_len) != 0xffff) {
RTE_LOG(WARNING, ICMP, "%s: bad checksum\n", __func__);
goto errout;
}
ich->icmp_type = IP_ICMP_ECHO_REPLY;
/* recalc the checksum */
ich->icmp_cksum = 0;
csum = rte_raw_cksum(ich, mbuf->pkt_len);
ich->icmp_cksum = (csum == 0xffff) ? csum : ~csum;
memset(&fl4, 0, sizeof(struct flow4));
fl4.daddr.s_addr = iph->src_addr;
fl4.saddr.s_addr = iph->dst_addr;
fl4.oif = netif_port_get(mbuf->port);
fl4.proto = IPPROTO_ICMP;
fl4.tos = iph->type_of_service;
return ipv4_xmit(mbuf, &fl4);
errout:
rte_pktmbuf_free(mbuf);
return EDPVS_INVPKT;
}
/* cannot reply ICMP error on ICMP error,
* so all error type must be marked */
static struct icmp_ctrl icmp_ctrls[MAX_ICMP_CTRL] = {
[ICMP_ECHOREPLY] = {
.is_error = true,
},
[1] = {
.is_error = true,
},
[2] = {
.is_error = true,
},
[ICMP_DEST_UNREACH] = {
.is_error = true,
},
[ICMP_SOURCE_QUENCH] = {
.is_error = true,
},
[ICMP_REDIRECT] = {
.is_error = true,
},
[6] = {
.is_error = true,
},
[7] = {
.is_error = true,
},
[ICMP_ECHO] = {
.handler = icmp_echo,
},
[9] = {
.is_error = true,
},
[10] = {
.is_error = true,
},
[ICMP_TIME_EXCEEDED] = {
.is_error = true,
},
[ICMP_PARAMETERPROB] = {
.is_error = true,
},
[ICMP_TIMESTAMP] = {
.is_error = true,
},
[ICMP_TIMESTAMPREPLY] = {
.is_error = true,
},
[ICMP_INFO_REQUEST] = {
.is_error = true,
},
[ICMP_INFO_REPLY] = {
.is_error = true,
},
[ICMP_ADDRESS] = {
.is_error = true,
},
[ICMP_ADDRESSREPLY] = {
.is_error = true,
},
};
/* @imbuf is input (original) IP packet to trigger ICMP. */
void icmp_send(struct rte_mbuf *imbuf, int type, int code, uint32_t info)
{
struct route_entry *rt = imbuf->userdata;
struct ipv4_hdr *iph = ip4_hdr(imbuf);
eth_type_t etype = imbuf->packet_type; /* FIXME: use other field ? */
struct in_addr saddr;
uint8_t tos;
struct icmphdr *icmph;
struct rte_mbuf *mbuf;
struct flow4 fl4;
uint16_t csum;
int room, err;
if (!rt) {
RTE_LOG(DEBUG, ICMP, "%s: no route.\n", __func__);
return;
}
/* no replies to physical multicast/broadcast */
if (etype != ETH_PKT_HOST) {
RTE_LOG(DEBUG, ICMP, "%s: phy-multi/broadcast.\n", __func__);
return;
}
/* no replies to IP multicast/broadcast. */
/* FIXME: use route to check if dest is multicast or broadcast.
* since broadcast is not only ffffffff but in/to network. */
if (IN_MULTICAST(ntohl(iph->dst_addr))
|| INADDR_BROADCAST == ntohl(iph->dst_addr)) {
RTE_LOG(DEBUG, ICMP, "%s: multi/broadcast.\n", __func__);
return;
}
/* reply only first fragment. */
if (iph->fragment_offset & htons(IPV4_HDR_OFFSET_MASK))
return;
if (type > NR_ICMP_TYPES)
return;
/* cannot reply ICMP-error to ICMP-error message */
if (icmp_ctrls[type].is_error) {
if (iph->next_proto_id == IPPROTO_ICMP) {
struct icmphdr _oich, *oich; /* original ICMP */
oich = mbuf_header_pointer(imbuf, ip4_hdrlen(imbuf),
sizeof(_oich), &_oich);
if (!oich || oich->type > NR_ICMP_TYPES
|| icmp_ctrls[oich->type].is_error) {
RTE_LOG(DEBUG, ICMP,
"%s: cannot reply error on error.\n", __func__);
return;
}
}
}
/* determing source address */
if (rt->flag & RTF_LOCALIN) { /* original pkt's dest is us ? */
saddr.s_addr = iph->dst_addr;
} else {
/* linux select IP of ingress iface only when param
* net.ipv4.icmp_errors_use_inbound_ifaddr is true.
* and by default, this param is false. */
saddr.s_addr = 0;
}
tos = icmp_ctrls[type].is_error ? \
((iph->type_of_service & IPTOS_TOS_MASK)
| IPTOS_PREC_INTERNETCONTROL) : iph->type_of_service;
memset(&fl4, 0, sizeof(struct flow4));
fl4.daddr.s_addr = iph->src_addr;
fl4.saddr = saddr;
fl4.oif = netif_port_get(imbuf->port);
fl4.proto = IPPROTO_ICMP;
fl4.tos = tos;
if (!fl4.oif) {
RTE_LOG(DEBUG, ICMP, "%s: no output iface.\n", __func__);
return;
}
mbuf = rte_pktmbuf_alloc(fl4.oif->mbuf_pool);
if (!mbuf) {
RTE_LOG(DEBUG, ICMP, "%s: no memory.\n", __func__);
return;
}
assert(rte_pktmbuf_headroom(mbuf) >= 128); /* for L2/L3 */
/* prepare ICMP message */
icmph = (struct icmphdr *)rte_pktmbuf_append(mbuf, sizeof(struct icmphdr));
if (!icmph) {
RTE_LOG(DEBUG, ICMP, "%s: no room in mbuf.\n", __func__);
rte_pktmbuf_free(mbuf);
return;
}
icmph->type = type;
icmph->code = code;
icmph->un.gateway = info; /* not good */
/* copy as much as we can without exceeding 576 (min-MTU) */
room = fl4.oif->mtu > 576 ? 576 : fl4.oif->mtu;
room -= sizeof(struct ipv4_hdr);
room -= sizeof(struct icmphdr);
/* we support only linear mbuf now, use m.data_len
* instead of m.pkt_len */
room = imbuf->data_len > room ? room : imbuf->data_len;
if (rte_pktmbuf_append(mbuf, room) == NULL) {
RTE_LOG(DEBUG, ICMP, "%s: no room in mbuf.\n", __func__);
rte_pktmbuf_free(mbuf);
return;
}
memcpy(icmph + 1, iph, room);
/* recalc the checksum */
icmph->checksum = 0;
csum = rte_raw_cksum(icmph, mbuf->pkt_len);
icmph->checksum = (csum == 0xffff) ? csum : ~csum;
if ((err = ipv4_xmit(mbuf, &fl4)) != EDPVS_OK)
RTE_LOG(DEBUG, ICMP, "%s: ipv4_xmit: %s.\n",
__func__, dpvs_strerror(err));
return;
}
static int icmp_rcv(struct rte_mbuf *mbuf)
{
struct ipv4_hdr *iph = mbuf->userdata;
struct icmp_hdr *ich;
struct icmp_ctrl *ctrl;
if (mbuf_may_pull(mbuf, sizeof(struct icmp_hdr)) != 0)
goto invpkt;
ich = rte_pktmbuf_mtod(mbuf, struct icmp_hdr *);
if (unlikely(!iph)) {
RTE_LOG(WARNING, ICMP, "%s: no ipv4 header\n", __func__);
goto invpkt;
}
#ifdef CONFIG_DPVS_ICMP_DEBUG
icmp_dump_hdr(mbuf);
#endif
ctrl = &icmp_ctrls[ich->icmp_type];
if (ctrl->handler)
return ctrl->handler(mbuf);
else
return EDPVS_KNICONTINUE; /* KNI may like it, don't drop */
invpkt:
rte_pktmbuf_free(mbuf);
return EDPVS_INVPKT;
}
static struct inet_protocol icmp_protocol = {
.handler = icmp_rcv,
};
int icmp_init(void)
{
int err;
err = ipv4_register_protocol(&icmp_protocol, IPPROTO_ICMP);
return err;
}
int icmp_term(void)
{
int err;
err = ipv4_unregister_protocol(&icmp_protocol, IPPROTO_ICMP);
return err;
}