1 /*
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright 2021 Lutz Donnerhacke
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials provided
15 * with the distribution.
16 * 3. Neither the name of the copyright holder nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
21 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
22 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
29 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
31 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34 #include <stdio.h>
35 #include <stdlib.h>
36
37 #include <netinet/in.h>
38
39 #include "util.h"
40
41 /* common ip ranges */
42 struct in_addr masq = { htonl(0x01020304) };
43 struct in_addr pub = { htonl(0x0102dead) };
44 struct in_addr pub2 = { htonl(0x0102beef) };
45 struct in_addr prv1 = { htonl(0x0a00dead) };
46 struct in_addr prv2 = { htonl(0xac10dead) };
47 struct in_addr prv3 = { htonl(0xc0a8dead) };
48 struct in_addr cgn = { htonl(0x6440dead) };
49 struct in_addr ext = { htonl(0x12345678) };
50 struct in_addr ANY_ADDR = { 0 };
51
52 #define REQUIRE(x) do { \
53 if (!(x)) { \
54 fprintf(stderr, "Failed in %s %s:%d.\n",\
55 __FUNCTION__, __FILE__, __LINE__); \
56 exit(-1); \
57 } \
58 } while(0)
59
60 int
randcmp(const void * a,const void * b)61 randcmp(const void *a, const void *b)
62 {
63 int res, r = rand();
64
65 (void)a;
66 (void)b;
67 res = (r/4 < RAND_MAX/9) ? 1
68 : (r/5 < RAND_MAX/9) ? 0
69 : -1;
70 return (res);
71 }
72
73 void
hexdump(void * p,size_t len)74 hexdump(void *p, size_t len)
75 {
76 size_t i;
77 unsigned char *c = p;
78
79 for (i = 0; i < len; i++) {
80 printf(" %02x", c[i]);
81 switch (i & 0xf) {
82 case 0xf: printf("\n"); break;
83 case 0x7: printf(" "); break;
84 default: break;
85 }
86 }
87 if ((i & 0xf) != 0x0)
88 printf("\n");
89 }
90
91 struct ip *
ip_packet(u_char protocol,size_t len)92 ip_packet(u_char protocol, size_t len)
93 {
94 struct ip * p;
95
96 REQUIRE(len >= 64 && len <= IP_MAXPACKET);
97
98 p = calloc(1, len);
99 REQUIRE(p != NULL);
100
101 p->ip_v = IPVERSION;
102 p->ip_hl = sizeof(*p)/4;
103 p->ip_len = htons(len);
104 p->ip_ttl = IPDEFTTL;
105 p->ip_p = protocol;
106 REQUIRE(p->ip_hl == 5);
107
108 return (p);
109 }
110
111 struct udphdr *
set_udp(struct ip * p,u_short sport,u_short dport)112 set_udp(struct ip *p, u_short sport, u_short dport) {
113 int hlen = p->ip_hl << 2;
114 struct udphdr *u = (struct udphdr *)((uintptr_t)p + hlen);
115 int payload = ntohs(p->ip_len) - hlen;
116
117 REQUIRE(payload >= (int)sizeof(*u));
118 p->ip_p = IPPROTO_UDP;
119 u->uh_sport = htons(sport);
120 u->uh_dport = htons(dport);
121 u->uh_ulen = htons(payload);
122 return (u);
123 }
124