summaryrefslogtreecommitdiffstats
path: root/extras/libmemif/examples/icmp_responder/icmp_proto.c
blob: b27abb6a231c30e0d2c4391e2e66e320355c84a9 (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
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
328
329
330
331
332
333
334
335
336
337
338
339
/*
 *------------------------------------------------------------------
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * 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 <stdint.h>
#include <net/if.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/uio.h>
#include <sys/mman.h>
#include <sys/prctl.h>
#include <inttypes.h>
#include <string.h>
#include <stdio.h>
#include <netdb.h>
#include <linux/ip.h>
#include <linux/icmp.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <netinet/if_ether.h>
#include <net/if_arp.h>
#include <asm/byteorder.h>
#include <byteswap.h>

#include <icmp_proto.h>

static uint16_t
cksum (void *addr, ssize_t len)
{
  char *data = (char *) addr;

  uint32_t acc = 0xffff;

  ssize_t i;
  for (i = 0; (i + 1) < len; i += 2)
    {
      uint16_t word;
      memcpy (&word, data + i, 2);
      acc += ntohs (word);
      if (acc > 0xffff)
	acc -= 0xffff;
    }

  if (len & 1)
    {
      uint16_t word = 0;
      memcpy (&word, data + len - 1, 1);
      acc += ntohs (word);
      if (acc > 0xffff)
	acc -= 0xffff;
    }
  return htons (~acc);
}

int
print_packet (void *pck)
{
  if (pck == NULL)
    {
      printf ("ICMP_PROTO: no data\n");
      return -1;
    }
  struct iphdr *ip;
  struct icmphdr *icmp;
  ip = (struct iphdr *) pck;
  icmp = (struct icmphdr *) (pck + sizeof (struct iphdr));
  printf ("received packet:\n");
  printf ("\tiphdr:\n");
  printf ("\t\tihl: %u\n\t\tversion: %u\n\t\tlen: %u\n\t\tid: %u\n",
	  ip->ihl, ip->version, __bswap_16 (ip->tot_len), ip->id);
  printf ("\t\tprotocol: %u\n", ip->protocol);

  printf ("\t\tsaddr: ");
  int i;
  for (i = 0; i < 4; i++)
    {
      printf ("%u.", ((uint8_t *) & ip->saddr)[i]);
    }
  printf ("\n");

  printf ("\t\tdaddr: ");
  for (i = 0; i < 4; i++)
    {
      printf ("%u.", ((uint8_t *) & ip->daddr)[i]);
    }
  printf ("\n");
  printf ("\ticmphdr:\n");
  printf ("\t\ttype: %s\n",
	  (icmp->type == ICMP_ECHO) ? "ICMP_ECHO" : "ICMP_ECHOREPLY");

  return 0;
}

static ssize_t
resolve_arp (void *arp)
{
  struct arphdr *resp = (struct arphdr *) arp;

  resp->ar_hrd = __bswap_16 (ARPHRD_ETHER);

  resp->ar_pro = __bswap_16 (0x0800);

  resp->ar_hln = 6;
  resp->ar_pln = 4;

  resp->ar_op = __bswap_16 (ARPOP_REPLY);

  return sizeof (struct arphdr);
}

static ssize_t
resolve_eth_arp (struct ether_arp *eth_arp, void *eth_arp_resp,
		 uint8_t ip_addr[4])
{
  struct ether_arp *resp = (struct ether_arp *) eth_arp_resp;

  resolve_arp (&resp->ea_hdr);

  memcpy (resp->arp_tha, eth_arp->arp_sha, 6);
  memcpy (resp->arp_tpa, eth_arp->arp_spa, 4);

  memcpy (resp->arp_sha,
	  (((struct ether_header *) (eth_arp_resp -
				     sizeof (struct
					     ether_header)))->ether_shost),
	  6);

  memcpy (resp->arp_spa, ip_addr, 4);

  return sizeof (struct ether_arp);
}

static ssize_t
resolve_eth (struct ether_header *eth, void *eth_resp)
{
  struct ether_header *resp = (struct ether_header *) eth_resp;
  memcpy (resp->ether_dhost, eth->ether_shost, 6);

  uint8_t hw_addr[6];
  int i;
  for (i = 0; i < 6; i++)
    {
      hw_addr[i] = 'a';
    }
  memcpy (resp->ether_shost, hw_addr, 6);

  resp->ether_type = eth->ether_type;

  return sizeof (struct ether_header);
}

static ssize_t
resolve_ip (struct iphdr *ip, void *ip_resp, uint8_t ip_addr[4])
{
  struct iphdr *resp = (struct iphdr *) ip_resp;
  resp->ihl = 5;
  resp->version = 4;
  resp->tos = 0;
  /*len updated later */
  resp->tot_len = 0x0000;
  resp->id = 0;
  resp->frag_off = 0;
  resp->ttl = 0x40;
  resp->protocol = 1;
  ((uint8_t *) & resp->saddr)[0] = ip_addr[0];
  ((uint8_t *) & resp->saddr)[1] = ip_addr[1];
  ((uint8_t *) & resp->saddr)[2] = ip_addr[2];
  ((uint8_t *) & resp->saddr)[3] = ip_addr[3];
  resp->daddr = ip->saddr;

  /* resp->check =  cksum (resp, sizeof (struct iphdr)); */

  return sizeof (struct iphdr);
}

static ssize_t
resolve_icmp (struct icmphdr *icmp, void *icmp_resp)
{
  struct icmphdr *resp = (struct icmphdr *) icmp_resp;
  resp->type = 0x00;
  resp->code = 0;
  resp->un.echo.id = icmp->un.echo.id;
  resp->un.echo.sequence = icmp->un.echo.sequence;

  /*resp->checksum = cksum (resp, sizeof (struct icmphdr)); */

  return sizeof (struct icmphdr);
}

int
resolve_packet (void *in_pck, ssize_t in_size,
		void *out_pck, uint32_t * out_size, uint8_t ip_addr[4])
{
  struct ether_header *eh;
  struct ether_arp *eah;
  struct iphdr *ip, *ip_out;
  struct icmphdr *icmp;
  *out_size = 0;

  if ((in_pck == NULL) || (out_pck == NULL))
    return -1;

  eh = (struct ether_header *) in_pck;
  *out_size = resolve_eth (eh, out_pck);

  if (eh->ether_type == 0x0608)
    {
      eah = (struct ether_arp *) (in_pck + *out_size);
      *out_size += resolve_eth_arp (eah, out_pck + *out_size, ip_addr);

    }
  else if (eh->ether_type == 0x0008)
    {
#ifdef ICMP_DBG
      print_packet (in_pck + *out_size);
#endif
      ip = (struct iphdr *) (in_pck + *out_size);
      ip_out = (struct iphdr *) (out_pck + *out_size);
      *out_size += resolve_ip (ip, out_pck + *out_size, ip_addr);
      if (ip->protocol == 1)
	{
	  icmp = (struct icmphdr *) (in_pck + *out_size);
	  *out_size += resolve_icmp (icmp, out_pck + *out_size);
	  ((struct icmphdr *) (out_pck + *out_size -
			       sizeof (struct icmphdr)))->checksum =
	    cksum (out_pck + *out_size - sizeof (struct icmphdr),
		   sizeof (struct icmphdr));
	  /* payload */
	  memcpy (out_pck + *out_size, in_pck + *out_size,
		  in_size - *out_size);
	  *out_size = in_size;
	  ip_out->tot_len =
	    __bswap_16 (*out_size - sizeof (struct ether_header));
	  ip_out->check = cksum (ip_out, sizeof (struct iphdr));
	}
    }
  return 0;
}

static ssize_t
generate_eth (struct ether_header *eh, uint8_t hw_daddr[6])
{
  uint8_t hw_addr[6];
  int i;
  for (i = 0; i < 6; i++)
    {
      hw_addr[i] = 'a';
    }
  memcpy (eh->ether_shost, hw_addr, 6);
  memcpy (eh->ether_dhost, hw_daddr, 6);

  eh->ether_type = 0x0008;

  return sizeof (struct ether_header);
}

static ssize_t
generate_ip (struct iphdr *ip, uint8_t saddr[4], uint8_t daddr[4])
{
  ip->ihl = 5;
  ip->version = 4;
  ip->tos = 0;
  /*len updated later */
  ip->tot_len = 0x5400;
  ip->id = 0;
  ip->frag_off = 0;
  ip->ttl = 0x40;
  ip->protocol = 1;
  /* saddr */
  ((uint8_t *) & ip->saddr)[0] = saddr[0];
  ((uint8_t *) & ip->saddr)[1] = saddr[1];
  ((uint8_t *) & ip->saddr)[2] = saddr[2];
  ((uint8_t *) & ip->saddr)[3] = saddr[3];
  /* daddr */
  ((uint8_t *) & ip->daddr)[0] = daddr[0];
  ((uint8_t *) & ip->daddr)[1] = daddr[1];
  ((uint8_t *) & ip->daddr)[2] = daddr[2];
  ((uint8_t *) & ip->daddr)[3] = daddr[3];

  ip->check = cksum (ip, sizeof (struct iphdr));

  return sizeof (struct iphdr);
}

static ssize_t
generate_icmp (struct icmphdr *icmp, uint32_t seq)
{
  icmp->type = ICMP_ECHO;
  icmp->code = 0;
  icmp->un.echo.id = 0;
  icmp->un.echo.sequence = seq;

  return sizeof (struct icmphdr);
}

int
generate_packet (void *pck, uint32_t * size, uint8_t saddr[4],
		 uint8_t daddr[4], uint8_t hw_daddr[6], uint32_t seq)
{
  struct ether_header *eh;
  struct iphdr *ip;
  struct icmphdr *icmp;

  *size = 0;

  eh = (struct ether_header *) pck;
  *size += generate_eth (eh, hw_daddr);

  ip = (struct iphdr *) (pck + *size);
  *size += generate_ip (ip, saddr, daddr);

  icmp = (struct icmphdr *) (pck + *size);
  *size += generate_icmp (icmp, seq);

  ((struct icmphdr *) (pck + *size - sizeof (struct icmphdr)))->checksum =
    cksum (pck + *size - sizeof (struct icmphdr), sizeof (struct icmphdr));

  ip->tot_len = __bswap_16 (*size - sizeof (struct ether_header));
  ip->check = 0;
  ip->check = cksum (ip, sizeof (struct iphdr));

  return 0;
}