/*1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright 2021 Lutz Donnerhacke4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8*9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above12* copyright notice, this list of conditions and the following13* disclaimer in the documentation and/or other materials provided14* with the distribution.15* 3. Neither the name of the copyright holder nor the names of its16* contributors may be used to endorse or promote products derived17* from this software without specific prior written permission.18*19* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND20* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,21* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF22* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE23* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS24* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,25* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED26* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,27* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON28* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR29* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF30* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF31* SUCH DAMAGE.32*/33#include <stdio.h>34#include <stdlib.h>3536#include <netinet/in.h>3738#include "util.h"3940/* common ip ranges */41struct in_addr masq = { htonl(0x01020304) };42struct in_addr pub = { htonl(0x0102dead) };43struct in_addr pub2 = { htonl(0x0102beef) };44struct in_addr prv1 = { htonl(0x0a00dead) };45struct in_addr prv2 = { htonl(0xac10dead) };46struct in_addr prv3 = { htonl(0xc0a8dead) };47struct in_addr cgn = { htonl(0x6440dead) };48struct in_addr ext = { htonl(0x12345678) };49struct in_addr ANY_ADDR = { 0 };5051#define REQUIRE(x) do { \52if (!(x)) { \53fprintf(stderr, "Failed in %s %s:%d.\n",\54__FUNCTION__, __FILE__, __LINE__); \55exit(-1); \56} \57} while(0)5859int60randcmp(const void *a, const void *b)61{62int res, r = rand();6364(void)a;65(void)b;66res = (r/4 < RAND_MAX/9) ? 167: (r/5 < RAND_MAX/9) ? 068: -1;69return (res);70}7172void73hexdump(void *p, size_t len)74{75size_t i;76unsigned char *c = p;7778for (i = 0; i < len; i++) {79printf(" %02x", c[i]);80switch (i & 0xf) {81case 0xf: printf("\n"); break;82case 0x7: printf(" "); break;83default: break;84}85}86if ((i & 0xf) != 0x0)87printf("\n");88}8990struct ip *91ip_packet(u_char protocol, size_t len)92{93struct ip * p;9495REQUIRE(len >= 64 && len <= IP_MAXPACKET);9697p = calloc(1, len);98REQUIRE(p != NULL);99100p->ip_v = IPVERSION;101p->ip_hl = sizeof(*p)/4;102p->ip_len = htons(len);103p->ip_ttl = IPDEFTTL;104p->ip_p = protocol;105REQUIRE(p->ip_hl == 5);106107return (p);108}109110struct udphdr *111set_udp(struct ip *p, u_short sport, u_short dport) {112int hlen = p->ip_hl << 2;113struct udphdr *u = (struct udphdr *)((uintptr_t)p + hlen);114int payload = ntohs(p->ip_len) - hlen;115116REQUIRE(payload >= (int)sizeof(*u));117p->ip_p = IPPROTO_UDP;118u->uh_sport = htons(sport);119u->uh_dport = htons(dport);120u->uh_ulen = htons(payload);121return (u);122}123124125