edit_packet.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164
  1. /* $Id$ */
  2. /*
  3. * Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
  4. * Copyright (c) 2013-2018 Fred Klassen <tcpreplay at appneta dot com> - AppNeta
  5. *
  6. * The Tcpreplay Suite of tools is free software: you can redistribute it
  7. * and/or modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation, either version 3 of the
  9. * License, or with the authors permission any later version.
  10. *
  11. * The Tcpreplay Suite is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with the Tcpreplay Suite. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "config.h"
  20. #include "defines.h"
  21. #include "common.h"
  22. #include "tcpedit.h"
  23. #include "edit_packet.h"
  24. #include "checksum.h"
  25. #include "incremental_checksum.h"
  26. #include "lib/sll.h"
  27. #include "dlt.h"
  28. #include <string.h>
  29. #include <stdlib.h>
  30. #include <sys/types.h>
  31. #include <sys/socket.h>
  32. #include <netinet/in.h>
  33. #include <arpa/inet.h>
  34. static uint32_t randomize_ipv4_addr(tcpedit_t *tcpedit, uint32_t ip);
  35. static uint32_t remap_ipv4(tcpedit_t *tcpedit, tcpr_cidr_t *cidr, const uint32_t original);
  36. static int is_unicast_ipv4(tcpedit_t *tcpedit, uint32_t ip);
  37. static void randomize_ipv6_addr(tcpedit_t *tcpedit, struct tcpr_in6_addr *addr);
  38. static int remap_ipv6(tcpedit_t *tcpedit, tcpr_cidr_t *cidr, struct tcpr_in6_addr *addr);
  39. static int is_multicast_ipv6(tcpedit_t *tcpedit, struct tcpr_in6_addr *addr);
  40. static int ipv6_header_length(ipv6_hdr_t const * ip6_hdr, int pkt_len);
  41. /**
  42. * this code re-calcs the IP and Layer 4 checksums
  43. * the IMPORTANT THING is that the Layer 4 header
  44. * is contiguous in memory after *ip_hdr we're actually
  45. * writing to the layer 4 header via the ip_hdr ptr.
  46. * (Yes, this sucks, but that's the way libnet works, and
  47. * I was too lazy to re-invent the wheel.
  48. * Returns 0 on success, -1 on error
  49. */
  50. int
  51. fix_ipv4_checksums(tcpedit_t *tcpedit, struct pcap_pkthdr *pkthdr, ipv4_hdr_t *ip_hdr)
  52. {
  53. int ret1 = 0, ret2 = 0, ip_len;
  54. assert(tcpedit);
  55. assert(pkthdr);
  56. assert(ip_hdr);
  57. if (pkthdr->caplen < sizeof(*ip_hdr)) {
  58. tcpedit_setwarn(tcpedit, "caplen too small to read IPv4 header: %u",
  59. pkthdr->caplen);
  60. return TCPEDIT_WARN;
  61. }
  62. if (ip_hdr->ip_v != 4) {
  63. tcpedit_seterr(tcpedit, "Invalid packet: Expected IPv4 packet: got %u", ip_hdr->ip_v);
  64. return TCPEDIT_ERROR;
  65. }
  66. /* calc the L4 checksum if we have the whole packet && not a frag or first frag */
  67. if (pkthdr->caplen == pkthdr->len &&
  68. (htons(ip_hdr->ip_off) & (IP_MF | IP_OFFMASK)) == 0) {
  69. ip_len = (int)ntohs(ip_hdr->ip_len);
  70. ret1 = do_checksum(tcpedit, (u_char *) ip_hdr, ip_hdr->ip_p,
  71. ip_len - (ip_hdr->ip_hl << 2));
  72. if (ret1 < 0)
  73. return TCPEDIT_ERROR;
  74. }
  75. /* calc IP checksum */
  76. ip_len = (int)ntohs(ip_hdr->ip_len);
  77. ret2 = do_checksum(tcpedit, (u_char *) ip_hdr, IPPROTO_IP, ip_len);
  78. if (ret2 < 0)
  79. return TCPEDIT_ERROR;
  80. /* what do we return? */
  81. if (ret1 == TCPEDIT_WARN || ret2 == TCPEDIT_WARN)
  82. return TCPEDIT_WARN;
  83. return TCPEDIT_OK;
  84. }
  85. /**
  86. * Returns ipv6 header length with all ipv6 options on success
  87. * -1 on error
  88. */
  89. static int
  90. ipv6_header_length(ipv6_hdr_t const * ip6_hdr, int pkt_len)
  91. {
  92. struct tcpr_ipv6_ext_hdr_base const * nhdr;
  93. uint8_t next_header;
  94. int offset;
  95. offset = sizeof(*ip6_hdr);
  96. next_header = ip6_hdr->ip_nh;
  97. while (sizeof(*nhdr) + offset < (size_t)pkt_len)
  98. {
  99. if (next_header != TCPR_IPV6_NH_HBH
  100. && next_header != TCPR_IPV6_NH_ROUTING
  101. && next_header != TCPR_IPV6_NH_FRAGMENT) {
  102. return offset;
  103. }
  104. nhdr = (struct tcpr_ipv6_ext_hdr_base const *) (((uint8_t const *)ip6_hdr) + offset);
  105. next_header = nhdr->ip_nh;
  106. offset += ((nhdr->ip_len + 1) << 3);
  107. }
  108. return -1;
  109. }
  110. int
  111. fix_ipv6_checksums(tcpedit_t *tcpedit, struct pcap_pkthdr *pkthdr, ipv6_hdr_t *ip6_hdr)
  112. {
  113. int ret = 0;
  114. assert(tcpedit);
  115. assert(pkthdr);
  116. assert(ip6_hdr);
  117. if (pkthdr->caplen < sizeof(*ip6_hdr)) {
  118. tcpedit_setwarn(tcpedit, "caplen too small to read IPv6 header: %u",
  119. pkthdr->caplen);
  120. return TCPEDIT_WARN;
  121. }
  122. ipv4_hdr_t *ip_hdr = (ipv4_hdr_t*)ip6_hdr;
  123. if (ip_hdr->ip_v != 6) {
  124. tcpedit_seterr(tcpedit, "Invalid packet: Expected IPv6 packet: got %u", ip_hdr->ip_v);
  125. return TCPEDIT_ERROR;
  126. }
  127. /* calc the L4 checksum if we have the whole packet && not a frag or first frag */
  128. if (pkthdr->caplen == pkthdr->len) {
  129. if (ip6_hdr->ip_len < ipv6_header_length(ip6_hdr, pkthdr->len)) {
  130. tcpedit_setwarn(tcpedit, "Unable to checksum IPv6 packet with invalid length %u",
  131. ip6_hdr->ip_len);
  132. return TCPEDIT_WARN;
  133. }
  134. ret = do_checksum(tcpedit, (u_char *)ip6_hdr, ip6_hdr->ip_nh,
  135. htons(ip6_hdr->ip_len));
  136. if (ret < 0)
  137. return TCPEDIT_ERROR;
  138. }
  139. /* what do we return? */
  140. if (ret == TCPEDIT_WARN)
  141. return TCPEDIT_WARN;
  142. return TCPEDIT_OK;
  143. }
  144. /*
  145. * #406 fix IP headers which may be not be set properly due to TCP segmentation
  146. */
  147. void fix_ipv4_length(struct pcap_pkthdr *pkthdr, ipv4_hdr_t *ip_hdr)
  148. {
  149. if (!ip_hdr->ip_len)
  150. ip_hdr->ip_len = htons((uint16_t)pkthdr->len);
  151. }
  152. void fix_ipv6_length(struct pcap_pkthdr *pkthdr, ipv6_hdr_t *ip6_hdr)
  153. {
  154. if (!ip6_hdr->ip_len)
  155. ip6_hdr->ip_len = htons((uint16_t)pkthdr->len);
  156. }
  157. static void ipv4_l34_csum_replace(uint8_t *data, uint8_t protocol,
  158. uint32_t old, uint32_t new)
  159. {
  160. ipv4_hdr_t *ipv4;
  161. tcp_hdr_t *tcp_hdr;
  162. udp_hdr_t *udp_hdr;
  163. assert(data);
  164. switch (protocol) {
  165. case IPPROTO_IP:
  166. ipv4 = (ipv4_hdr_t *)data;
  167. csum_replace4(&ipv4->ip_sum, old, new);
  168. break;
  169. case IPPROTO_TCP:
  170. tcp_hdr = (tcp_hdr_t *)data;
  171. csum_replace4(&tcp_hdr->th_sum, old, new);
  172. break;
  173. case IPPROTO_UDP:
  174. udp_hdr = (udp_hdr_t *)data;
  175. if (udp_hdr->uh_sum)
  176. csum_replace4(&udp_hdr->uh_sum, old, new);
  177. break;
  178. default:
  179. assert(false);
  180. }
  181. }
  182. static void ipv6_l34_csum_replace(uint8_t *data, uint8_t protocol,
  183. uint32_t *old, uint32_t *new)
  184. {
  185. tcp_hdr_t *tcp_hdr;
  186. udp_hdr_t *udp_hdr;
  187. icmpv4_hdr_t *icmp;
  188. icmpv6_hdr_t *icmp6;
  189. assert(data);
  190. switch (protocol) {
  191. case IPPROTO_TCP:
  192. tcp_hdr = (tcp_hdr_t *)data;
  193. csum_replace16(&tcp_hdr->th_sum, old, new);
  194. break;
  195. case IPPROTO_UDP:
  196. udp_hdr = (udp_hdr_t *)data;
  197. if (udp_hdr->uh_sum)
  198. csum_replace16(&udp_hdr->uh_sum, old, new);
  199. break;
  200. case IPPROTO_ICMP:
  201. icmp = (icmpv4_hdr_t *)data;
  202. csum_replace16(&icmp->icmp_sum, old, new);
  203. break;
  204. case IPPROTO_ICMP6:
  205. icmp6 = (icmpv6_hdr_t *)data;
  206. csum_replace16(&icmp6->icmp_sum, old, new);
  207. break;
  208. default:
  209. assert(false);
  210. }
  211. }
  212. static void ipv4_addr_csum_replace(ipv4_hdr_t *ip_hdr, uint32_t old_ip,
  213. uint32_t new_ip, const int l3len)
  214. {
  215. uint8_t *l4, protocol;
  216. int len = l3len;
  217. assert(ip_hdr);
  218. if ((size_t)len < sizeof(*ip_hdr))
  219. return;
  220. ipv4_l34_csum_replace((uint8_t*)ip_hdr, IPPROTO_IP, old_ip, new_ip);
  221. protocol = ip_hdr->ip_p;
  222. switch (protocol) {
  223. case IPPROTO_UDP:
  224. l4 = get_layer4_v4(ip_hdr, len);
  225. len -= ip_hdr->ip_hl << 2;
  226. len -= TCPR_UDP_H;
  227. break;
  228. case IPPROTO_TCP:
  229. l4 = get_layer4_v4(ip_hdr, len);
  230. len -= ip_hdr->ip_hl << 2;
  231. len -= TCPR_TCP_H;
  232. break;
  233. default:
  234. l4 = NULL;
  235. }
  236. if (!l4 || len < 0)
  237. return;
  238. /* if this is a fragment, don't attempt to checksum the Layer4 header */
  239. if ((htons(ip_hdr->ip_off) & IP_OFFMASK) == 0)
  240. ipv4_l34_csum_replace(l4, protocol, old_ip, new_ip);
  241. }
  242. static void ipv6_addr_csum_replace(ipv6_hdr_t *ip6_hdr,
  243. struct tcpr_in6_addr *old_ip, struct tcpr_in6_addr *new_ip,
  244. const int l3len)
  245. {
  246. uint8_t *l4, protocol;
  247. int len = l3len;
  248. assert(ip6_hdr);
  249. if ((size_t)len < sizeof(*ip6_hdr))
  250. return;
  251. protocol = get_ipv6_l4proto(ip6_hdr, len);
  252. switch (protocol) {
  253. case IPPROTO_UDP:
  254. l4 = get_layer4_v6(ip6_hdr, len);
  255. len -= sizeof(*ip6_hdr);
  256. len -= TCPR_UDP_H;
  257. break;
  258. case IPPROTO_TCP:
  259. l4 = get_layer4_v6(ip6_hdr, len);
  260. len -= sizeof(*ip6_hdr);
  261. len -= TCPR_TCP_H;
  262. break;
  263. default:
  264. l4 = NULL;
  265. }
  266. if (!l4 || len < 0)
  267. return;
  268. ipv6_l34_csum_replace(l4, protocol, (uint32_t*)old_ip, (uint32_t*)new_ip);
  269. }
  270. /**
  271. * returns a new 32bit integer which is the randomized IP
  272. * based upon the user specified seed
  273. */
  274. static uint32_t
  275. randomize_ipv4_addr(tcpedit_t *tcpedit, uint32_t ip)
  276. {
  277. assert(tcpedit);
  278. /* don't rewrite broadcast addresses */
  279. if (tcpedit->skip_broadcast && !is_unicast_ipv4(tcpedit, ip))
  280. return ip;
  281. return ((ip ^ htonl(tcpedit->seed)) - (ip & htonl(tcpedit->seed)));
  282. }
  283. static void
  284. randomize_ipv6_addr(tcpedit_t *tcpedit, struct tcpr_in6_addr *addr)
  285. {
  286. uint32_t *p;
  287. int i;
  288. u_char was_multicast;
  289. assert(tcpedit);
  290. p = &addr->__u6_addr.__u6_addr32[0];
  291. was_multicast = is_multicast_ipv6(tcpedit, addr);
  292. for (i = 0; i < 4; ++i) {
  293. p[i] = ((p[i] ^ htonl(tcpedit->seed)) - (p[i] & htonl(tcpedit->seed)));
  294. }
  295. if (was_multicast) {
  296. addr->tcpr_s6_addr[0] = 0xff;
  297. } else if (is_multicast_ipv6(tcpedit, addr)) {
  298. addr->tcpr_s6_addr[0] = 0xaa;
  299. }
  300. }
  301. /**
  302. * randomizes the source and destination IP addresses based on a
  303. * pseudo-random number which is generated via the seed.
  304. * return 1 since we changed one or more IP addresses
  305. */
  306. int
  307. randomize_ipv4(tcpedit_t *tcpedit, struct pcap_pkthdr *pkthdr,
  308. u_char *pktdata, ipv4_hdr_t *ip_hdr, const int l3len)
  309. {
  310. #ifdef DEBUG
  311. char srcip[16], dstip[16];
  312. #endif
  313. assert(tcpedit);
  314. assert(pkthdr);
  315. assert(pktdata);
  316. assert(ip_hdr);
  317. #ifdef DEBUG
  318. strlcpy(srcip, get_addr2name4(ip_hdr->ip_src.s_addr, RESOLVE), 16);
  319. strlcpy(dstip, get_addr2name4(ip_hdr->ip_dst.s_addr, RESOLVE), 16);
  320. #endif
  321. /* randomize IP addresses based on the value of random */
  322. dbgx(1, "Old Src IP: %s\tOld Dst IP: %s", srcip, dstip);
  323. if (l3len < (int)ip_hdr->ip_hl << 2) {
  324. tcpedit_seterr(tcpedit, "Unable to randomize IP header due to packet capture snap length %u",
  325. pkthdr->caplen);
  326. return TCPEDIT_ERROR;
  327. }
  328. /* don't rewrite broadcast addresses */
  329. if ((tcpedit->skip_broadcast && is_unicast_ipv4(tcpedit, (u_int32_t)ip_hdr->ip_dst.s_addr))
  330. || !tcpedit->skip_broadcast) {
  331. uint32_t old_ip = ip_hdr->ip_dst.s_addr;
  332. ip_hdr->ip_dst.s_addr = randomize_ipv4_addr(tcpedit, ip_hdr->ip_dst.s_addr);
  333. ipv4_addr_csum_replace(ip_hdr, old_ip, ip_hdr->ip_dst.s_addr, l3len);
  334. }
  335. if ((tcpedit->skip_broadcast && is_unicast_ipv4(tcpedit, (u_int32_t)ip_hdr->ip_src.s_addr))
  336. || !tcpedit->skip_broadcast) {
  337. uint32_t old_ip = ip_hdr->ip_src.s_addr;
  338. ip_hdr->ip_src.s_addr = randomize_ipv4_addr(tcpedit, ip_hdr->ip_src.s_addr);
  339. ipv4_addr_csum_replace(ip_hdr, old_ip, ip_hdr->ip_src.s_addr, l3len);
  340. }
  341. #ifdef DEBUG
  342. strlcpy(srcip, get_addr2name4(ip_hdr->ip_src.s_addr, RESOLVE), 16);
  343. strlcpy(dstip, get_addr2name4(ip_hdr->ip_dst.s_addr, RESOLVE), 16);
  344. #endif
  345. dbgx(1, "New Src IP: %s\tNew Dst IP: %s\n", srcip, dstip);
  346. return 0;
  347. }
  348. int
  349. randomize_ipv6(tcpedit_t *tcpedit, struct pcap_pkthdr *pkthdr,
  350. u_char *pktdata, ipv6_hdr_t *ip6_hdr, const int l3len)
  351. {
  352. #ifdef DEBUG
  353. char srcip[INET6_ADDRSTRLEN], dstip[INET6_ADDRSTRLEN];
  354. #endif
  355. assert(tcpedit);
  356. assert(pkthdr);
  357. assert(pktdata);
  358. assert(ip6_hdr);
  359. #ifdef DEBUG
  360. strlcpy(srcip, get_addr2name6(&ip6_hdr->ip_src, RESOLVE), INET6_ADDRSTRLEN);
  361. strlcpy(dstip, get_addr2name6(&ip6_hdr->ip_dst, RESOLVE), INET6_ADDRSTRLEN);
  362. #endif
  363. /* randomize IP addresses based on the value of random */
  364. dbgx(1, "Old Src IP: %s\tOld Dst IP: %s", srcip, dstip);
  365. if (l3len < (int)sizeof(ipv6_hdr_t)) {
  366. tcpedit_seterr(tcpedit, "Unable to randomize IPv6 header due to packet capture snap length %u",
  367. pkthdr->caplen);
  368. return TCPEDIT_ERROR;
  369. }
  370. /* don't rewrite broadcast addresses */
  371. if ((tcpedit->skip_broadcast && !is_multicast_ipv6(tcpedit, &ip6_hdr->ip_dst))
  372. || !tcpedit->skip_broadcast) {
  373. struct tcpr_in6_addr old_ip6;
  374. memcpy(&old_ip6, &ip6_hdr->ip_dst, sizeof(old_ip6));
  375. randomize_ipv6_addr(tcpedit, &ip6_hdr->ip_dst);
  376. ipv6_addr_csum_replace(ip6_hdr, &old_ip6, &ip6_hdr->ip_dst, l3len);
  377. }
  378. if ((tcpedit->skip_broadcast && !is_multicast_ipv6(tcpedit, &ip6_hdr->ip_src))
  379. || !tcpedit->skip_broadcast) {
  380. struct tcpr_in6_addr old_ip6;
  381. memcpy(&old_ip6, &ip6_hdr->ip_src, sizeof(old_ip6));
  382. randomize_ipv6_addr(tcpedit, &ip6_hdr->ip_src);
  383. ipv6_addr_csum_replace(ip6_hdr, &old_ip6, &ip6_hdr->ip_src, l3len);
  384. }
  385. #ifdef DEBUG
  386. strlcpy(srcip, get_addr2name6(&ip6_hdr->ip_src, RESOLVE), INET6_ADDRSTRLEN);
  387. strlcpy(dstip, get_addr2name6(&ip6_hdr->ip_dst, RESOLVE), INET6_ADDRSTRLEN);
  388. #endif
  389. dbgx(1, "New Src IP: %s\tNew Dst IP: %s\n", srcip, dstip);
  390. return 0;
  391. }
  392. /**
  393. * this code will untruncate a packet via padding it with null
  394. * or resetting the actual IPv4 packet len to the snaplen - L2 header.
  395. * return 0 if no change, 1 if change, -1 on error.
  396. */
  397. int
  398. untrunc_packet(tcpedit_t *tcpedit, struct pcap_pkthdr *pkthdr,
  399. u_char **pktdata, ipv4_hdr_t *ip_hdr, ipv6_hdr_t *ip6_hdr)
  400. {
  401. int l2len;
  402. int chksum = 1;
  403. u_char *packet;
  404. udp_hdr_t *udp_hdr;
  405. u_char *dataptr = NULL;
  406. assert(tcpedit);
  407. assert(pkthdr);
  408. assert(pktdata);
  409. packet = *pktdata;
  410. assert(packet);
  411. /* if actual len == cap len or there's no IP header, don't do anything */
  412. if ((pkthdr->caplen == pkthdr->len) || (ip_hdr == NULL && ip6_hdr == NULL)) {
  413. /* unless we're in MTU truncate mode */
  414. if (! tcpedit->mtu_truncate)
  415. return(0);
  416. }
  417. if ((l2len = layer2len(tcpedit)) < 0) {
  418. tcpedit_seterr(tcpedit, "Non-sensical layer 2 length: %d", l2len);
  419. return -1;
  420. }
  421. /*
  422. * cannot checksum fragments, but we can do some
  423. * work on UDP fragments. Setting checksum to 0
  424. * means checksum will be ignored.
  425. */
  426. if (ip_hdr) {
  427. if ((htons(ip_hdr->ip_off) & IP_OFFMASK) != 0) {
  428. chksum = 0;
  429. } else if (ip_hdr->ip_p == IPPROTO_UDP &&
  430. (htons(ip_hdr->ip_off) & IP_MF) != 0) {
  431. dataptr = (u_char*)ip_hdr;
  432. dataptr += ip_hdr->ip_hl << 2;
  433. udp_hdr = (udp_hdr_t*)dataptr;
  434. udp_hdr->uh_sum = 0;
  435. chksum = 0;
  436. }
  437. }
  438. /* Pad packet or truncate it */
  439. if (tcpedit->fixlen == TCPEDIT_FIXLEN_PAD) {
  440. /*
  441. * this should be an unnecessary check
  442. * but I've gotten a report that sometimes the caplen > len
  443. * which seems like a corrupted pcap
  444. */
  445. if (pkthdr->len > pkthdr->caplen) {
  446. packet = safe_realloc(packet, pkthdr->len + PACKET_HEADROOM);
  447. memset(packet + pkthdr->caplen, '\0', pkthdr->len - pkthdr->caplen);
  448. pkthdr->caplen = pkthdr->len;
  449. } else if (pkthdr->len < pkthdr->caplen) {
  450. /* i guess this is necessary if we've got a bogus pcap */
  451. //ip_hdr->ip_len = htons(pkthdr->caplen - l2len);
  452. tcpedit_seterr(tcpedit, "%s", "WTF? Why is your packet larger then the capture len?");
  453. chksum = -1;
  454. goto done;
  455. }
  456. }
  457. else if (tcpedit->fixlen == TCPEDIT_FIXLEN_TRUNC) {
  458. if (ip_hdr && pkthdr->len != pkthdr->caplen)
  459. ip_hdr->ip_len = htons(pkthdr->caplen - l2len);
  460. pkthdr->len = pkthdr->caplen;
  461. }
  462. else if (tcpedit->mtu_truncate) {
  463. if (pkthdr->len > (uint32_t)(tcpedit->mtu + l2len)) {
  464. /* first truncate the packet */
  465. pkthdr->len = pkthdr->caplen = l2len + tcpedit->mtu;
  466. /* if ip_hdr exists, update the length */
  467. if (ip_hdr != NULL) {
  468. ip_hdr->ip_len = htons(tcpedit->mtu);
  469. } else if (ip6_hdr != NULL) {
  470. ip6_hdr->ip_len = htons(tcpedit->mtu - sizeof(*ip6_hdr));
  471. } else {
  472. /* for non-IP frames, don't try to fix checksums */
  473. chksum = 0;
  474. goto done;
  475. }
  476. }
  477. }
  478. else {
  479. tcpedit_seterr(tcpedit, "Invalid fixlen value: 0x%x", tcpedit->fixlen);
  480. chksum = -1;
  481. goto done;
  482. }
  483. done:
  484. *pktdata = packet;
  485. return chksum;
  486. }
  487. /**
  488. * rewrites an IPv4 packet's TTL based on the rules
  489. * return 0 if no change, 1 if changed
  490. */
  491. int
  492. rewrite_ipv4_ttl(tcpedit_t *tcpedit, ipv4_hdr_t *ip_hdr)
  493. {
  494. volatile uint16_t oldval, newval;
  495. assert(tcpedit);
  496. /* make sure there's something to edit */
  497. if (ip_hdr == NULL || tcpedit->ttl_mode == false)
  498. return(0);
  499. oldval = (uint16_t)ip_hdr->ip_ttl;
  500. switch(tcpedit->ttl_mode) {
  501. case TCPEDIT_TTL_MODE_SET:
  502. if (ip_hdr->ip_ttl == tcpedit->ttl_value)
  503. return 0; /* no change required */
  504. ip_hdr->ip_ttl = tcpedit->ttl_value;
  505. break;
  506. case TCPEDIT_TTL_MODE_ADD:
  507. if (((int)ip_hdr->ip_ttl + tcpedit->ttl_value) > 255) {
  508. ip_hdr->ip_ttl = 255;
  509. } else {
  510. ip_hdr->ip_ttl += tcpedit->ttl_value;
  511. }
  512. break;
  513. case TCPEDIT_TTL_MODE_SUB:
  514. if (ip_hdr->ip_ttl <= tcpedit->ttl_value) {
  515. ip_hdr->ip_ttl = 1;
  516. } else {
  517. ip_hdr->ip_ttl -= tcpedit->ttl_value;
  518. }
  519. break;
  520. default:
  521. errx(1, "invalid ttl_mode: %d", tcpedit->ttl_mode);
  522. }
  523. newval = (uint16_t)ip_hdr->ip_ttl;
  524. csum_replace2(&ip_hdr->ip_sum, oldval, newval);
  525. return 0;
  526. }
  527. /**
  528. * rewrites an IPv6 packet's hop limit based on the rules
  529. * return 0 if no change, 1 if changed
  530. */
  531. int
  532. rewrite_ipv6_hlim(tcpedit_t *tcpedit, ipv6_hdr_t *ip6_hdr)
  533. {
  534. assert(tcpedit);
  535. /* make sure there's something to edit */
  536. if (ip6_hdr == NULL || tcpedit->ttl_mode == TCPEDIT_TTL_MODE_OFF)
  537. return(0);
  538. switch(tcpedit->ttl_mode) {
  539. case TCPEDIT_TTL_MODE_SET:
  540. if (ip6_hdr->ip_hl == tcpedit->ttl_value)
  541. return(0); /* no change required */
  542. ip6_hdr->ip_hl = tcpedit->ttl_value;
  543. break;
  544. case TCPEDIT_TTL_MODE_ADD:
  545. if (((int)ip6_hdr->ip_hl + tcpedit->ttl_value) > 255) {
  546. ip6_hdr->ip_hl = 255;
  547. } else {
  548. ip6_hdr->ip_hl += tcpedit->ttl_value;
  549. }
  550. break;
  551. case TCPEDIT_TTL_MODE_SUB:
  552. if (ip6_hdr->ip_hl <= tcpedit->ttl_value) {
  553. ip6_hdr->ip_hl = 1;
  554. } else {
  555. ip6_hdr->ip_hl -= tcpedit->ttl_value;
  556. }
  557. break;
  558. default:
  559. errx(1, "invalid ttl_mode: %d", tcpedit->ttl_mode);
  560. }
  561. return 0;
  562. }
  563. /**
  564. * takes a CIDR notation netblock and uses that to "remap" given IP
  565. * onto that netblock. ie: 10.0.0.0/8 and 192.168.55.123 -> 10.168.55.123
  566. * while 10.150.9.0/24 and 192.168.55.123 -> 10.150.9.123
  567. */
  568. static uint32_t
  569. remap_ipv4(tcpedit_t *tcpedit, tcpr_cidr_t *cidr, const uint32_t original)
  570. {
  571. uint32_t ipaddr = 0, network = 0, mask = 0, result = 0;
  572. assert(tcpedit);
  573. assert(cidr);
  574. if (cidr->family != AF_INET) {
  575. return 0;
  576. }
  577. /* don't rewrite broadcast addresses */
  578. if (tcpedit->skip_broadcast && !is_unicast_ipv4(tcpedit, original))
  579. return original;
  580. mask = 0xffffffff; /* turn on all the bits */
  581. /* shift over by correct # of bits */
  582. mask = mask << (32 - cidr->masklen);
  583. /* apply the mask to the network */
  584. network = htonl(cidr->u.network) & mask;
  585. /* apply the reverse of the mask to the IP */
  586. mask = mask ^ 0xffffffff;
  587. ipaddr = ntohl(original) & mask;
  588. /* merge the network portion and ip portions */
  589. result = network ^ ipaddr;
  590. /* return the result in network byte order */
  591. return(htonl(result));
  592. }
  593. static int
  594. remap_ipv6(tcpedit_t *tcpedit, tcpr_cidr_t *cidr, struct tcpr_in6_addr *addr)
  595. {
  596. uint32_t i, j, k;
  597. assert(tcpedit);
  598. assert(cidr);
  599. if (cidr->family != AF_INET6) {
  600. return 0;
  601. }
  602. /* don't rewrite broadcast addresses */
  603. if (tcpedit->skip_broadcast && is_multicast_ipv6(tcpedit, addr))
  604. return 0;
  605. j = cidr->masklen / 8;
  606. for (i = 0; i < j; i++)
  607. addr->tcpr_s6_addr[i] = cidr->u.network6.tcpr_s6_addr[i];
  608. if ((k = cidr->masklen % 8) == 0)
  609. return 1;
  610. k = (uint32_t)~0 << (8 - k);
  611. i = addr->tcpr_s6_addr[i] & k;
  612. addr->tcpr_s6_addr[i] = (cidr->u.network6.tcpr_s6_addr[j] & (0xff << (8 - k))) |
  613. (addr->tcpr_s6_addr[i] & (0xff >> k));
  614. return 1;
  615. }
  616. /**
  617. * rewrite IP address (layer3)
  618. * uses -N to rewrite (map) one subnet onto another subnet
  619. * also support --srcipmap and --dstipmap
  620. * return 0 if no change, 1 or 2 if changed
  621. */
  622. int
  623. rewrite_ipv4l3(tcpedit_t *tcpedit, ipv4_hdr_t *ip_hdr, tcpr_dir_t direction,
  624. int len)
  625. {
  626. tcpr_cidrmap_t *cidrmap1 = NULL, *cidrmap2 = NULL;
  627. int didsrc = 0, diddst = 0, loop = 1;
  628. tcpr_cidrmap_t *ipmap;
  629. assert(tcpedit);
  630. assert(ip_hdr);
  631. /* first check the src/dst IP maps */
  632. ipmap = tcpedit->srcipmap;
  633. while (ipmap != NULL) {
  634. if (ip_in_cidr(ipmap->from, ip_hdr->ip_src.s_addr)) {
  635. uint32_t old_ip = ip_hdr->ip_src.s_addr;
  636. ip_hdr->ip_src.s_addr = remap_ipv4(tcpedit, ipmap->to, ip_hdr->ip_src.s_addr);
  637. ipv4_addr_csum_replace(ip_hdr, old_ip, ip_hdr->ip_src.s_addr, len);
  638. dbgx(2, "Remapped src addr to: %s", get_addr2name4(ip_hdr->ip_src.s_addr, RESOLVE));
  639. break;
  640. }
  641. ipmap = ipmap->next;
  642. }
  643. ipmap = tcpedit->dstipmap;
  644. while (ipmap != NULL) {
  645. if (ip_in_cidr(ipmap->from, ip_hdr->ip_dst.s_addr)) {
  646. uint32_t old_ip = ip_hdr->ip_dst.s_addr;
  647. ip_hdr->ip_dst.s_addr = remap_ipv4(tcpedit, ipmap->to, ip_hdr->ip_dst.s_addr);
  648. ipv4_addr_csum_replace(ip_hdr, old_ip, ip_hdr->ip_dst.s_addr, len);
  649. dbgx(2, "Remapped dst addr to: %s", get_addr2name4(ip_hdr->ip_dst.s_addr, RESOLVE));
  650. break;
  651. }
  652. ipmap = ipmap->next;
  653. }
  654. /* anything else to rewrite? */
  655. if (tcpedit->cidrmap1 == NULL)
  656. return(0);
  657. /* don't play with the main pointers */
  658. if (direction == TCPR_DIR_C2S) {
  659. cidrmap1 = tcpedit->cidrmap1;
  660. cidrmap2 = tcpedit->cidrmap2;
  661. } else {
  662. cidrmap1 = tcpedit->cidrmap2;
  663. cidrmap2 = tcpedit->cidrmap1;
  664. }
  665. /* loop through the cidrmap to rewrite */
  666. do {
  667. if ((! diddst) && ip_in_cidr(cidrmap2->from, ip_hdr->ip_dst.s_addr)) {
  668. uint32_t old_ip = ip_hdr->ip_dst.s_addr;
  669. ip_hdr->ip_dst.s_addr = remap_ipv4(tcpedit, cidrmap2->to, ip_hdr->ip_dst.s_addr);
  670. ipv4_addr_csum_replace(ip_hdr, old_ip, ip_hdr->ip_dst.s_addr, len);
  671. dbgx(2, "Remapped dst addr to: %s", get_addr2name4(ip_hdr->ip_dst.s_addr, RESOLVE));
  672. diddst = 1;
  673. }
  674. if ((! didsrc) && ip_in_cidr(cidrmap1->from, ip_hdr->ip_src.s_addr)) {
  675. uint32_t old_ip = ip_hdr->ip_src.s_addr;
  676. ip_hdr->ip_src.s_addr = remap_ipv4(tcpedit, cidrmap1->to, ip_hdr->ip_src.s_addr);
  677. ipv4_addr_csum_replace(ip_hdr, old_ip, ip_hdr->ip_src.s_addr, len);
  678. dbgx(2, "Remapped src addr to: %s", get_addr2name4(ip_hdr->ip_src.s_addr, RESOLVE));
  679. didsrc = 1;
  680. }
  681. /*
  682. * loop while we haven't modified both src/dst AND
  683. * at least one of the cidr maps have a next pointer
  684. */
  685. if ((! (diddst && didsrc)) &&
  686. (! ((cidrmap1->next == NULL) && (cidrmap2->next == NULL)))) {
  687. /* increment our ptr's if possible */
  688. if (cidrmap1->next != NULL)
  689. cidrmap1 = cidrmap1->next;
  690. if (cidrmap2->next != NULL)
  691. cidrmap2 = cidrmap2->next;
  692. } else {
  693. loop = 0;
  694. }
  695. /* Later on we should support various IP protocols which embed
  696. * the IP address in the application layer. Things like
  697. * DNS and FTP.
  698. */
  699. } while (loop);
  700. /* return how many changes we require checksum updates (none) */
  701. return 0;
  702. }
  703. int
  704. rewrite_ipv6l3(tcpedit_t *tcpedit, ipv6_hdr_t *ip6_hdr, tcpr_dir_t direction,
  705. int l3len)
  706. {
  707. tcpr_cidrmap_t *cidrmap1 = NULL, *cidrmap2 = NULL;
  708. int didsrc = 0, diddst = 0, loop = 1;
  709. tcpr_cidrmap_t *ipmap;
  710. assert(tcpedit);
  711. assert(ip6_hdr);
  712. /* first check the src/dst IP maps */
  713. ipmap = tcpedit->srcipmap;
  714. while (ipmap != NULL) {
  715. if (ip6_in_cidr(ipmap->from, &ip6_hdr->ip_src)) {
  716. struct tcpr_in6_addr old_ip6;
  717. memcpy(&old_ip6, &ip6_hdr->ip_src, sizeof(old_ip6));
  718. remap_ipv6(tcpedit, ipmap->to, &ip6_hdr->ip_src);
  719. ipv6_addr_csum_replace(ip6_hdr, &old_ip6, &ip6_hdr->ip_src, l3len);
  720. dbgx(2, "Remapped src addr to: %s", get_addr2name6(&ip6_hdr->ip_src, RESOLVE));
  721. break;
  722. }
  723. ipmap = ipmap->next;
  724. }
  725. ipmap = tcpedit->dstipmap;
  726. while (ipmap != NULL) {
  727. if (ip6_in_cidr(ipmap->from, &ip6_hdr->ip_dst)) {
  728. struct tcpr_in6_addr old_ip6;
  729. memcpy(&old_ip6, &ip6_hdr->ip_dst, sizeof(old_ip6));
  730. remap_ipv6(tcpedit, ipmap->to, &ip6_hdr->ip_dst);
  731. ipv6_addr_csum_replace(ip6_hdr, &old_ip6, &ip6_hdr->ip_dst, l3len);
  732. dbgx(2, "Remapped dst addr to: %s", get_addr2name6(&ip6_hdr->ip_dst, RESOLVE));
  733. break;
  734. }
  735. ipmap = ipmap->next;
  736. }
  737. /* anything else to rewrite? */
  738. if (tcpedit->cidrmap1 == NULL)
  739. return(0);
  740. /* don't play with the main pointers */
  741. if (direction == TCPR_DIR_C2S) {
  742. cidrmap1 = tcpedit->cidrmap1;
  743. cidrmap2 = tcpedit->cidrmap2;
  744. } else {
  745. cidrmap1 = tcpedit->cidrmap2;
  746. cidrmap2 = tcpedit->cidrmap1;
  747. }
  748. /* loop through the cidrmap to rewrite */
  749. do {
  750. if ((! diddst) && ip6_in_cidr(cidrmap2->from, &ip6_hdr->ip_dst)) {
  751. struct tcpr_in6_addr old_ip6;
  752. memcpy(&old_ip6, &ip6_hdr->ip_dst, sizeof(old_ip6));
  753. remap_ipv6(tcpedit, cidrmap2->to, &ip6_hdr->ip_dst);
  754. ipv6_addr_csum_replace(ip6_hdr, &old_ip6, &ip6_hdr->ip_dst, l3len);
  755. dbgx(2, "Remapped dst addr to: %s", get_addr2name6(&ip6_hdr->ip_dst, RESOLVE));
  756. diddst = 1;
  757. }
  758. if ((! didsrc) && ip6_in_cidr(cidrmap1->from, &ip6_hdr->ip_src)) {
  759. struct tcpr_in6_addr old_ip6;
  760. memcpy(&old_ip6, &ip6_hdr->ip_src, sizeof(old_ip6));
  761. remap_ipv6(tcpedit, cidrmap1->to, &ip6_hdr->ip_src);
  762. ipv6_addr_csum_replace(ip6_hdr, &old_ip6, &ip6_hdr->ip_src, l3len);
  763. dbgx(2, "Remapped src addr to: %s", get_addr2name6(&ip6_hdr->ip_src, RESOLVE));
  764. didsrc = 1;
  765. }
  766. /*
  767. * loop while we haven't modified both src/dst AND
  768. * at least one of the cidr maps have a next pointer
  769. */
  770. if ((! (diddst && didsrc)) &&
  771. (! ((cidrmap1->next == NULL) && (cidrmap2->next == NULL)))) {
  772. /* increment our ptr's if possible */
  773. if (cidrmap1->next != NULL)
  774. cidrmap1 = cidrmap1->next;
  775. if (cidrmap2->next != NULL)
  776. cidrmap2 = cidrmap2->next;
  777. } else {
  778. loop = 0;
  779. }
  780. /* Later on we should support various IP protocols which embed
  781. * the IP address in the application layer. Things like
  782. * DNS and FTP.
  783. */
  784. } while (loop);
  785. /* return how many changes we made (none) */
  786. return 0;
  787. }
  788. /**
  789. * Randomize the IP addresses in an ARP packet based on the user seed
  790. * return 0 if no change, or 1 for a change
  791. */
  792. int
  793. randomize_iparp(tcpedit_t *tcpedit, struct pcap_pkthdr *pkthdr,
  794. u_char *pktdata, int datalink, const int l3len)
  795. {
  796. arp_hdr_t *arp_hdr ;
  797. int l2len;
  798. #ifdef FORCE_ALIGN
  799. uint32_t iptemp;
  800. #endif
  801. assert(tcpedit);
  802. assert(pkthdr);
  803. assert(pktdata);
  804. if (l3len < (int)sizeof(arp_hdr_t)) {
  805. tcpedit_seterr(tcpedit, "Unable to randomize ARP packet due to packet capture snap length %u",
  806. pkthdr->caplen);
  807. return TCPEDIT_ERROR;
  808. }
  809. l2len = get_l2len(pktdata, pkthdr->caplen, datalink);
  810. arp_hdr = (arp_hdr_t *)(pktdata + l2len);
  811. /*
  812. * only rewrite IP addresses from REPLY/REQUEST's
  813. */
  814. if ((ntohs(arp_hdr->ar_pro) == ETHERTYPE_IP) &&
  815. ((ntohs(arp_hdr->ar_op) == ARPOP_REQUEST) ||
  816. (ntohs(arp_hdr->ar_op) == ARPOP_REPLY))) {
  817. /* jump to the addresses */
  818. uint32_t *ip;
  819. u_char *add_hdr = ((u_char *)arp_hdr) + sizeof(arp_hdr_t) +
  820. arp_hdr->ar_hln;
  821. #ifdef FORCE_ALIGN
  822. /* copy IP to a temporary buffer for processing */
  823. memcpy(&iptemp, add_hdr, sizeof(uint32_t));
  824. ip = &iptemp;
  825. #else
  826. ip = (uint32_t *)add_hdr;
  827. #endif
  828. *ip = randomize_ipv4_addr(tcpedit, *ip);
  829. #ifdef FORCE_ALIGN
  830. memcpy(add_hdr, &iptemp, sizeof(uint32_t));
  831. #endif
  832. add_hdr += arp_hdr->ar_pln + arp_hdr->ar_hln;
  833. #ifdef FORCE_ALIGN
  834. /* copy IP2 to a temporary buffer for processing */
  835. memcpy(&iptemp, add_hdr, sizeof(uint32_t));
  836. ip = &iptemp;
  837. #else
  838. ip = (uint32_t *)add_hdr;
  839. #endif
  840. *ip = randomize_ipv4_addr(tcpedit, *ip);
  841. #ifdef FORCE_ALIGN
  842. memcpy(add_hdr, &iptemp, sizeof(uint32_t));
  843. #endif
  844. }
  845. return 1; /* yes we changed the packet */
  846. }
  847. /**
  848. * rewrite IP address (arp)
  849. * uses -a to rewrite (map) one subnet onto another subnet
  850. * pointer must point to the WHOLE and CONTIGUOUS memory buffer
  851. * because the arp_hdr_t doesn't have the space for the IP/MAC
  852. * addresses
  853. * return 0 if no change, 1 or 2 if changed
  854. */
  855. int
  856. rewrite_iparp(tcpedit_t *tcpedit, arp_hdr_t *arp_hdr, int cache_mode)
  857. {
  858. u_char *add_hdr = NULL;
  859. uint32_t *ip1 = NULL, *ip2 = NULL;
  860. uint32_t newip = 0;
  861. tcpr_cidrmap_t *cidrmap1 = NULL, *cidrmap2 = NULL;
  862. int didsrc = 0, diddst = 0, loop = 1;
  863. #ifdef FORCE_ALIGN
  864. uint32_t iptemp;
  865. #endif
  866. assert(tcpedit);
  867. assert(arp_hdr);
  868. /* figure out what mapping to use */
  869. if (cache_mode == TCPR_DIR_C2S) {
  870. cidrmap1 = tcpedit->cidrmap1;
  871. cidrmap2 = tcpedit->cidrmap2;
  872. } else if (cache_mode == TCPR_DIR_S2C) {
  873. cidrmap1 = tcpedit->cidrmap2;
  874. cidrmap2 = tcpedit->cidrmap1;
  875. }
  876. /* anything to rewrite? */
  877. if (cidrmap1 == NULL || cidrmap2 == NULL)
  878. return(0);
  879. /*
  880. * must be IPv4 and request or reply
  881. * Do other op codes use the same subheader stub?
  882. * If so we won't need to check the op code.
  883. */
  884. if ((ntohs(arp_hdr->ar_pro) == ETHERTYPE_IP) &&
  885. ((ntohs(arp_hdr->ar_op) == ARPOP_REQUEST) ||
  886. (ntohs(arp_hdr->ar_op) == ARPOP_REPLY)))
  887. {
  888. /* jump to the addresses */
  889. add_hdr = (u_char *)arp_hdr;
  890. add_hdr += sizeof(arp_hdr_t) + arp_hdr->ar_hln;
  891. ip1 = (uint32_t *)add_hdr;
  892. add_hdr += arp_hdr->ar_pln + arp_hdr->ar_hln;
  893. #ifdef FORCE_ALIGN
  894. /* copy IP2 to a temporary buffer for processing */
  895. memcpy(&iptemp, add_hdr, sizeof(uint32_t));
  896. ip2 = &iptemp;
  897. #else
  898. ip2 = (uint32_t *)add_hdr;
  899. #endif
  900. /* loop through the cidrmap to rewrite */
  901. do {
  902. /* arp request ? */
  903. if (ntohs(arp_hdr->ar_op) == ARPOP_REQUEST) {
  904. if ((!diddst) && ip_in_cidr(cidrmap2->from, *ip1)) {
  905. newip = remap_ipv4(tcpedit, cidrmap2->to, *ip1);
  906. memcpy(ip1, &newip, 4);
  907. diddst = 1;
  908. }
  909. if ((!didsrc) && ip_in_cidr(cidrmap1->from, *ip2)) {
  910. newip = remap_ipv4(tcpedit, cidrmap1->to, *ip2);
  911. memcpy(ip2, &newip, 4);
  912. didsrc = 1;
  913. }
  914. }
  915. /* else it's an arp reply */
  916. else {
  917. if ((!diddst) && ip_in_cidr(cidrmap2->from, *ip2)) {
  918. newip = remap_ipv4(tcpedit, cidrmap2->to, *ip2);
  919. memcpy(ip2, &newip, 4);
  920. diddst = 1;
  921. }
  922. if ((!didsrc) && ip_in_cidr(cidrmap1->from, *ip1)) {
  923. newip = remap_ipv4(tcpedit, cidrmap1->to, *ip1);
  924. memcpy(ip1, &newip, 4);
  925. didsrc = 1;
  926. }
  927. }
  928. #ifdef FORCE_ALIGN
  929. /* copy temporary IP to IP2 location in buffer */
  930. memcpy(add_hdr, &iptemp, sizeof(uint32_t));
  931. #endif
  932. /*
  933. * loop while we haven't modified both src/dst AND
  934. * at least one of the cidr maps have a next pointer
  935. */
  936. if ((! (diddst && didsrc)) &&
  937. (! ((cidrmap1->next == NULL) && (cidrmap2->next == NULL)))) {
  938. /* increment our ptr's if possible */
  939. if (cidrmap1->next != NULL)
  940. cidrmap1 = cidrmap1->next;
  941. if (cidrmap2->next != NULL)
  942. cidrmap2 = cidrmap2->next;
  943. } else {
  944. loop = 0;
  945. }
  946. } while (loop);
  947. } else {
  948. warn("ARP packet isn't for IPv4! Can't rewrite IP's");
  949. }
  950. return(didsrc + diddst);
  951. }
  952. /**
  953. * returns 1 if the IP address is a unicast address, otherwise, returns 0
  954. * for broadcast/multicast addresses. Returns -1 on error
  955. */
  956. static int
  957. is_unicast_ipv4(tcpedit_t *tcpedit, uint32_t ip)
  958. {
  959. assert(tcpedit);
  960. /* multicast/broadcast is 224.0.0.0 or greater */
  961. if (ntohl(ip) > 3758096384)
  962. return 0;
  963. return 1;
  964. }
  965. /**
  966. * returns 1 if the IPv6 address is a multicast address, otherwise, returns 0
  967. * for unicast/anycast addresses. Returns -1 on error
  968. */
  969. static int
  970. is_multicast_ipv6(tcpedit_t *tcpedit, struct tcpr_in6_addr *addr)
  971. {
  972. assert(tcpedit);
  973. if (addr->tcpr_s6_addr[0] == 0xff)
  974. return 1;
  975. return 0;
  976. }