edit_packet.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  1. /* $Id: edit_packet.c 2289 2009-05-03 19:43:01Z aturner $ */
  2. /*
  3. * Copyright (c) 2001-2007 Aaron Turner.
  4. * All rights reserved.
  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 copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the names of the copyright owners nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  20. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  23. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  25. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  27. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  28. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  29. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #include "config.h"
  32. #include "defines.h"
  33. #include "common.h"
  34. #include "tcpedit-int.h"
  35. #include "edit_packet.h"
  36. #include "checksum.h"
  37. #include "lib/sll.h"
  38. #include "dlt.h"
  39. #include <string.h>
  40. #include <stdlib.h>
  41. #include <sys/types.h>
  42. #include <sys/socket.h>
  43. #include <netinet/in.h>
  44. #include <arpa/inet.h>
  45. static u_int32_t randomize_ipv4_addr(tcpedit_t *tcpedit, u_int32_t ip);
  46. static u_int32_t remap_ipv4(tcpedit_t *tcpedit, tcpr_cidr_t *cidr, const u_int32_t original);
  47. static int is_unicast_ipv4(tcpedit_t *tcpedit, u_int32_t ip);
  48. static void randomize_ipv6_addr(tcpedit_t *tcpedit, struct tcpr_in6_addr *addr);
  49. static int remap_ipv6(tcpedit_t *tcpedit, tcpr_cidr_t *cidr, struct tcpr_in6_addr *addr);
  50. static int is_multicast_ipv6(tcpedit_t *tcpedit, struct tcpr_in6_addr *addr);
  51. /**
  52. * this code re-calcs the IP and Layer 4 checksums
  53. * the IMPORTANT THING is that the Layer 4 header
  54. * is contiguious in memory after *ip_hdr we're actually
  55. * writing to the layer 4 header via the ip_hdr ptr.
  56. * (Yes, this sucks, but that's the way libnet works, and
  57. * I was too lazy to re-invent the wheel.
  58. * Returns 0 on sucess, -1 on error
  59. */
  60. int
  61. fix_ipv4_checksums(tcpedit_t *tcpedit, struct pcap_pkthdr *pkthdr, ipv4_hdr_t *ip_hdr)
  62. {
  63. int ret1 = 0, ret2 = 0;
  64. assert(tcpedit);
  65. assert(pkthdr);
  66. assert(ip_hdr);
  67. /* calc the L4 checksum if we have the whole packet && not a frag or first frag */
  68. if (pkthdr->caplen == pkthdr->len && (htons(ip_hdr->ip_off) & IP_OFFMASK) == 0) {
  69. ret1 = do_checksum(tcpedit, (u_char *) ip_hdr,
  70. ip_hdr->ip_p, ntohs(ip_hdr->ip_len) - (ip_hdr->ip_hl << 2));
  71. if (ret1 < 0)
  72. return TCPEDIT_ERROR;
  73. }
  74. /* calc IP checksum */
  75. ret2 = do_checksum(tcpedit, (u_char *) ip_hdr, IPPROTO_IP, ntohs(ip_hdr->ip_len));
  76. if (ret2 < 0)
  77. return TCPEDIT_ERROR;
  78. /* what do we return? */
  79. if (ret1 == TCPEDIT_WARN || ret2 == TCPEDIT_WARN)
  80. return TCPEDIT_WARN;
  81. return TCPEDIT_OK;
  82. }
  83. int
  84. fix_ipv6_checksums(tcpedit_t *tcpedit, struct pcap_pkthdr *pkthdr, ipv6_hdr_t *ip6_hdr)
  85. {
  86. int ret = 0;
  87. assert(tcpedit);
  88. assert(pkthdr);
  89. assert(ip6_hdr);
  90. /* calc the L4 checksum if we have the whole packet && not a frag or first frag */
  91. if (pkthdr->caplen == pkthdr->len) {
  92. ret = do_checksum(tcpedit, (u_char *) ip6_hdr, ip6_hdr->ip_nh,
  93. htons(ip6_hdr->ip_len));
  94. if (ret < 0)
  95. return TCPEDIT_ERROR;
  96. }
  97. /* what do we return? */
  98. if (ret == TCPEDIT_WARN)
  99. return TCPEDIT_WARN;
  100. return TCPEDIT_OK;
  101. }
  102. /**
  103. * returns a new 32bit integer which is the randomized IP
  104. * based upon the user specified seed
  105. */
  106. static u_int32_t
  107. randomize_ipv4_addr(tcpedit_t *tcpedit, u_int32_t ip)
  108. {
  109. assert(tcpedit);
  110. /* don't rewrite broadcast addresses */
  111. if (tcpedit->skip_broadcast && !is_unicast_ipv4(tcpedit, ip))
  112. return ip;
  113. return ((ip ^ htonl(tcpedit->seed)) - (ip & htonl(tcpedit->seed)));
  114. }
  115. static void
  116. randomize_ipv6_addr(tcpedit_t *tcpedit, struct tcpr_in6_addr *addr)
  117. {
  118. u_int32_t *p;
  119. int i;
  120. u_char was_multicast;
  121. assert(tcpedit);
  122. p = &addr->__u6_addr.__u6_addr32[0];
  123. was_multicast = is_multicast_ipv6(tcpedit, addr);
  124. for (i = 0; i < 4; ++i) {
  125. p[i] = ((p[i] ^ htonl(tcpedit->seed)) - (p[i] & htonl(tcpedit->seed)));
  126. }
  127. if (was_multicast) {
  128. addr->tcpr_s6_addr[0] = 0xff;
  129. } else if (is_multicast_ipv6(tcpedit, addr)) {
  130. addr->tcpr_s6_addr[0] = 0xaa;
  131. }
  132. }
  133. /**
  134. * randomizes the source and destination IP addresses based on a
  135. * pseudo-random number which is generated via the seed.
  136. * return 1 since we changed one or more IP addresses
  137. */
  138. int
  139. randomize_ipv4(tcpedit_t *tcpedit, struct pcap_pkthdr *pkthdr,
  140. u_char *pktdata, ipv4_hdr_t *ip_hdr)
  141. {
  142. #ifdef DEBUG
  143. char srcip[16], dstip[16];
  144. #endif
  145. assert(tcpedit);
  146. assert(pkthdr);
  147. assert(pktdata);
  148. assert(ip_hdr);
  149. #ifdef DEBUG
  150. strlcpy(srcip, get_addr2name4(ip_hdr->ip_src.s_addr, RESOLVE), 16);
  151. strlcpy(dstip, get_addr2name4(ip_hdr->ip_dst.s_addr, RESOLVE), 16);
  152. #endif
  153. /* randomize IP addresses based on the value of random */
  154. dbgx(1, "Old Src IP: %s\tOld Dst IP: %s", srcip, dstip);
  155. /* don't rewrite broadcast addresses */
  156. if ((tcpedit->skip_broadcast && is_unicast_ipv4(tcpedit, (u_int32_t)ip_hdr->ip_dst.s_addr))
  157. || !tcpedit->skip_broadcast) {
  158. ip_hdr->ip_dst.s_addr = randomize_ipv4_addr(tcpedit, ip_hdr->ip_dst.s_addr);
  159. }
  160. if ((tcpedit->skip_broadcast && is_unicast_ipv4(tcpedit, (u_int32_t)ip_hdr->ip_src.s_addr))
  161. || !tcpedit->skip_broadcast) {
  162. ip_hdr->ip_src.s_addr = randomize_ipv4_addr(tcpedit, ip_hdr->ip_src.s_addr);
  163. }
  164. #ifdef DEBUG
  165. strlcpy(srcip, get_addr2name4(ip_hdr->ip_src.s_addr, RESOLVE), 16);
  166. strlcpy(dstip, get_addr2name4(ip_hdr->ip_dst.s_addr, RESOLVE), 16);
  167. #endif
  168. dbgx(1, "New Src IP: %s\tNew Dst IP: %s\n", srcip, dstip);
  169. return(1);
  170. }
  171. int
  172. randomize_ipv6(tcpedit_t *tcpedit, struct pcap_pkthdr *pkthdr,
  173. u_char *pktdata, ipv6_hdr_t *ip6_hdr)
  174. {
  175. #ifdef DEBUG
  176. char srcip[INET6_ADDRSTRLEN], dstip[INET6_ADDRSTRLEN];
  177. #endif
  178. assert(tcpedit);
  179. assert(pkthdr);
  180. assert(pktdata);
  181. assert(ip6_hdr);
  182. #ifdef DEBUG
  183. strlcpy(srcip, get_addr2name6(&ip6_hdr->ip_src, RESOLVE), INET6_ADDRSTRLEN);
  184. strlcpy(dstip, get_addr2name6(&ip6_hdr->ip_dst, RESOLVE), INET6_ADDRSTRLEN);
  185. #endif
  186. /* randomize IP addresses based on the value of random */
  187. dbgx(1, "Old Src IP: %s\tOld Dst IP: %s", srcip, dstip);
  188. /* don't rewrite broadcast addresses */
  189. if ((tcpedit->skip_broadcast && !is_multicast_ipv6(tcpedit, &ip6_hdr->ip_dst))
  190. || !tcpedit->skip_broadcast) {
  191. randomize_ipv6_addr(tcpedit, &ip6_hdr->ip_dst);
  192. }
  193. if ((tcpedit->skip_broadcast && !is_multicast_ipv6(tcpedit, &ip6_hdr->ip_src))
  194. || !tcpedit->skip_broadcast) {
  195. randomize_ipv6_addr(tcpedit, &ip6_hdr->ip_src);
  196. }
  197. #ifdef DEBUG
  198. strlcpy(srcip, get_addr2name6(&ip6_hdr->ip_src, RESOLVE), INET6_ADDRSTRLEN);
  199. strlcpy(dstip, get_addr2name6(&ip6_hdr->ip_dst, RESOLVE), INET6_ADDRSTRLEN);
  200. #endif
  201. dbgx(1, "New Src IP: %s\tNew Dst IP: %s\n", srcip, dstip);
  202. return(1);
  203. }
  204. /**
  205. * this code will untruncate a packet via padding it with null
  206. * or resetting the actual IPv4 packet len to the snaplen - L2 header.
  207. * return 0 if no change, 1 if change, -1 on error.
  208. */
  209. int
  210. untrunc_packet(tcpedit_t *tcpedit, struct pcap_pkthdr *pkthdr,
  211. u_char *pktdata, ipv4_hdr_t *ip_hdr, ipv6_hdr_t *ip6_hdr)
  212. {
  213. int l2len;
  214. assert(tcpedit);
  215. assert(pkthdr);
  216. assert(pktdata);
  217. /* if actual len == cap len or there's no IP header, don't do anything */
  218. if ((pkthdr->caplen == pkthdr->len) || (ip_hdr == NULL && ip6_hdr == NULL)) {
  219. /* unless we're in MTU truncate mode */
  220. if (! tcpedit->mtu_truncate)
  221. return(0);
  222. }
  223. if ((l2len = layer2len(tcpedit)) < 0) {
  224. tcpedit_seterr(tcpedit, "Non-sensical layer 2 length: %d", l2len);
  225. return -1;
  226. }
  227. /* Pad packet or truncate it */
  228. if (tcpedit->fixlen == TCPEDIT_FIXLEN_PAD) {
  229. /*
  230. * this should be an unnecessary check
  231. * but I've gotten a report that sometimes the caplen > len
  232. * which seems like a corrupted pcap
  233. */
  234. if (pkthdr->len > pkthdr->caplen) {
  235. memset(pktdata + pkthdr->caplen, '\0', pkthdr->len - pkthdr->caplen);
  236. pkthdr->caplen = pkthdr->len;
  237. } else if (pkthdr->len < pkthdr->caplen) {
  238. /* i guess this is necessary if we've got a bogus pcap */
  239. //ip_hdr->ip_len = htons(pkthdr->caplen - l2len);
  240. tcpedit_seterr(tcpedit, "%s", "WTF? Why is your packet larger then the capture len?");
  241. return -1;
  242. }
  243. }
  244. else if (tcpedit->fixlen == TCPEDIT_FIXLEN_TRUNC) {
  245. if (pkthdr->len != pkthdr->caplen)
  246. ip_hdr->ip_len = htons(pkthdr->caplen - l2len);
  247. pkthdr->len = pkthdr->caplen;
  248. }
  249. else if (tcpedit->mtu_truncate) {
  250. if (pkthdr->len > (u_int16_t)(tcpedit->mtu + l2len)) {
  251. /* first truncate the packet */
  252. pkthdr->len = pkthdr->caplen = l2len + tcpedit->mtu;
  253. /* if ip_hdr exists, update the length */
  254. if (ip_hdr != NULL) {
  255. ip_hdr->ip_len = htons(tcpedit->mtu);
  256. } else if (ip6_hdr != NULL) {
  257. ip6_hdr->ip_len = htons(tcpedit->mtu - sizeof(*ip6_hdr));
  258. } else {
  259. /* for non-IP frames, don't try to fix checksums */
  260. return 0;
  261. }
  262. }
  263. }
  264. else {
  265. tcpedit_seterr(tcpedit, "Invalid fixlen value: 0x%x", tcpedit->fixlen);
  266. return -1;
  267. }
  268. return(1);
  269. }
  270. /**
  271. * Extracts the layer 7 data from the packet for TCP, UDP, ICMP
  272. * returns the number of bytes and a pointer to the layer 7 data.
  273. * Returns 0 for no data
  274. */
  275. int
  276. extract_data(tcpedit_t *tcpedit, const u_char *pktdata, int caplen,
  277. char *l7data[])
  278. {
  279. int datalen = 0; /* amount of data beyond ip header */
  280. ipv4_hdr_t *ip_hdr = NULL;
  281. tcp_hdr_t *tcp_hdr = NULL;
  282. udp_hdr_t *udp_hdr = NULL;
  283. u_char ipbuff[MAXPACKET];
  284. u_char *dataptr = NULL;
  285. assert(tcpedit);
  286. assert(pktdata);
  287. assert(l7data);
  288. /* grab our IPv4 header */
  289. dataptr = ipbuff;
  290. if ((ip_hdr = (ipv4_hdr_t*)get_ipv4(pktdata, caplen,
  291. tcpedit->runtime.dlt1, &dataptr)) == NULL)
  292. return 0;
  293. /*
  294. * figure out the actual datalen which might be < the caplen
  295. * due to ethernet padding
  296. */
  297. if (caplen > ntohs(ip_hdr->ip_len)) {
  298. datalen = ntohs(ip_hdr->ip_len);
  299. } else {
  300. datalen = caplen - tcpedit->dlt_ctx->l2len;
  301. }
  302. /* update the datlen to not include the IP header len */
  303. datalen -= ip_hdr->ip_hl << 2;
  304. dataptr += ip_hdr->ip_hl << 2;
  305. if (datalen <= 0)
  306. goto nodata;
  307. /* TCP ? */
  308. if (ip_hdr->ip_p == IPPROTO_TCP) {
  309. tcp_hdr = (tcp_hdr_t *) get_layer4_v4(ip_hdr);
  310. datalen -= tcp_hdr->th_off << 2;
  311. if (datalen <= 0)
  312. goto nodata;
  313. dataptr += tcp_hdr->th_off << 2;
  314. }
  315. /* UDP ? */
  316. else if (ip_hdr->ip_p == IPPROTO_UDP) {
  317. udp_hdr = (udp_hdr_t *) get_layer4_v4(ip_hdr);
  318. datalen -= TCPR_UDP_H;
  319. if (datalen <= 0)
  320. goto nodata;
  321. dataptr += TCPR_UDP_H;
  322. }
  323. /* ICMP ? just ignore it for now */
  324. else if (ip_hdr->ip_p == IPPROTO_ICMP) {
  325. dbg(2, "Ignoring any possible data in ICMP packet");
  326. goto nodata;
  327. }
  328. /* unknown proto, just dump everything past the IP header */
  329. else {
  330. dbg(2, "Unknown protocol, dumping everything past the IP header");
  331. dataptr = (u_char *)ip_hdr;
  332. }
  333. dbgx(2, "packet had %d bytes of layer 7 data", datalen);
  334. memcpy(l7data, dataptr, datalen);
  335. return datalen;
  336. nodata:
  337. dbg(2, "packet has no data, skipping...");
  338. return 0;
  339. }
  340. /**
  341. * rewrites an IPv4 packet's TTL based on the rules
  342. * return 0 if no change, 1 if changed
  343. */
  344. int
  345. rewrite_ipv4_ttl(tcpedit_t *tcpedit, ipv4_hdr_t *ip_hdr)
  346. {
  347. assert(tcpedit);
  348. /* make sure there's something to edit */
  349. if (ip_hdr == NULL || tcpedit->ttl_mode == TCPEDIT_TTL_OFF)
  350. return(0);
  351. switch(tcpedit->ttl_mode) {
  352. case TCPEDIT_TTL_SET:
  353. if (ip_hdr->ip_ttl == tcpedit->ttl_value)
  354. return(0); /* no change required */
  355. ip_hdr->ip_ttl = tcpedit->ttl_value;
  356. break;
  357. case TCPEDIT_TTL_ADD:
  358. if (((int)ip_hdr->ip_ttl + tcpedit->ttl_value) > 255) {
  359. ip_hdr->ip_ttl = 255;
  360. } else {
  361. ip_hdr->ip_ttl += tcpedit->ttl_value;
  362. }
  363. break;
  364. case TCPEDIT_TTL_SUB:
  365. if (ip_hdr->ip_ttl <= tcpedit->ttl_value) {
  366. ip_hdr->ip_ttl = 1;
  367. } else {
  368. ip_hdr->ip_ttl -= tcpedit->ttl_value;
  369. }
  370. break;
  371. default:
  372. errx(1, "invalid ttl_mode: %d", tcpedit->ttl_mode);
  373. }
  374. return(1);
  375. }
  376. /**
  377. * rewrites an IPv6 packet's hop limit based on the rules
  378. * return 0 if no change, 1 if changed
  379. */
  380. int
  381. rewrite_ipv6_hlim(tcpedit_t *tcpedit, ipv6_hdr_t *ip6_hdr)
  382. {
  383. assert(tcpedit);
  384. /* make sure there's something to edit */
  385. if (ip6_hdr == NULL || tcpedit->ttl_mode == TCPEDIT_TTL_OFF)
  386. return(0);
  387. switch(tcpedit->ttl_mode) {
  388. case TCPEDIT_TTL_SET:
  389. if (ip6_hdr->ip_hl == tcpedit->ttl_value)
  390. return(0); /* no change required */
  391. ip6_hdr->ip_hl = tcpedit->ttl_value;
  392. break;
  393. case TCPEDIT_TTL_ADD:
  394. if (((int)ip6_hdr->ip_hl + tcpedit->ttl_value) > 255) {
  395. ip6_hdr->ip_hl = 255;
  396. } else {
  397. ip6_hdr->ip_hl += tcpedit->ttl_value;
  398. }
  399. break;
  400. case TCPEDIT_TTL_SUB:
  401. if (ip6_hdr->ip_hl <= tcpedit->ttl_value) {
  402. ip6_hdr->ip_hl = 1;
  403. } else {
  404. ip6_hdr->ip_hl -= tcpedit->ttl_value;
  405. }
  406. break;
  407. default:
  408. errx(1, "invalid ttl_mode: %d", tcpedit->ttl_mode);
  409. }
  410. return(1);
  411. }
  412. /**
  413. * takes a CIDR notation netblock and uses that to "remap" given IP
  414. * onto that netblock. ie: 10.0.0.0/8 and 192.168.55.123 -> 10.168.55.123
  415. * while 10.150.9.0/24 and 192.168.55.123 -> 10.150.9.123
  416. */
  417. static u_int32_t
  418. remap_ipv4(tcpedit_t *tcpedit, tcpr_cidr_t *cidr, const u_int32_t original)
  419. {
  420. u_int32_t ipaddr = 0, network = 0, mask = 0, result = 0;
  421. assert(tcpedit);
  422. assert(cidr);
  423. if (cidr->family != AF_INET) {
  424. return 0;
  425. }
  426. /* don't rewrite broadcast addresses */
  427. if (tcpedit->skip_broadcast && !is_unicast_ipv4(tcpedit, original))
  428. return original;
  429. mask = 0xffffffff; /* turn on all the bits */
  430. /* shift over by correct # of bits */
  431. mask = mask << (32 - cidr->masklen);
  432. /* apply the mask to the network */
  433. network = htonl(cidr->u.network) & mask;
  434. /* apply the reverse of the mask to the IP */
  435. mask = mask ^ 0xffffffff;
  436. ipaddr = ntohl(original) & mask;
  437. /* merge the network portion and ip portions */
  438. result = network ^ ipaddr;
  439. /* return the result in network byte order */
  440. return(htonl(result));
  441. }
  442. static int
  443. remap_ipv6(tcpedit_t *tcpedit, tcpr_cidr_t *cidr, struct tcpr_in6_addr *addr)
  444. {
  445. int i, j, k;
  446. assert(tcpedit);
  447. assert(cidr);
  448. if (cidr->family != AF_INET6) {
  449. return 0;
  450. }
  451. /* don't rewrite broadcast addresses */
  452. if (tcpedit->skip_broadcast && is_multicast_ipv6(tcpedit, addr))
  453. return 0;
  454. j = cidr->masklen / 8;
  455. for (i = 0; i < j; i++)
  456. addr->tcpr_s6_addr[i] = cidr->u.network6.tcpr_s6_addr[i];
  457. if ((k = cidr->masklen % 8) == 0)
  458. return 1;
  459. k = ~0 << (8 - k);
  460. i = addr->tcpr_s6_addr[i] & k;
  461. addr->tcpr_s6_addr[i] = (cidr->u.network6.tcpr_s6_addr[j] & (0xff << (8 - k))) |
  462. (addr->tcpr_s6_addr[i] & (0xff >> k));
  463. return 1;
  464. }
  465. /**
  466. * rewrite IP address (layer3)
  467. * uses -N to rewrite (map) one subnet onto another subnet
  468. * also support --srcipmap and --dstipmap
  469. * return 0 if no change, 1 or 2 if changed
  470. */
  471. int
  472. rewrite_ipv4l3(tcpedit_t *tcpedit, ipv4_hdr_t *ip_hdr, tcpr_dir_t direction)
  473. {
  474. tcpr_cidrmap_t *cidrmap1 = NULL, *cidrmap2 = NULL;
  475. int didsrc = 0, diddst = 0, loop = 1;
  476. assert(tcpedit);
  477. assert(ip_hdr);
  478. /* first check the src/dst IP maps */
  479. if (tcpedit->srcipmap != NULL) {
  480. if (ip_in_cidr(tcpedit->srcipmap->from, ip_hdr->ip_src.s_addr)) {
  481. ip_hdr->ip_src.s_addr = remap_ipv4(tcpedit, tcpedit->srcipmap->to, ip_hdr->ip_src.s_addr);
  482. dbgx(2, "Remapped src addr to: %s", get_addr2name4(ip_hdr->ip_src.s_addr, RESOLVE));
  483. }
  484. }
  485. if (tcpedit->dstipmap != NULL) {
  486. if (ip_in_cidr(tcpedit->dstipmap->from, ip_hdr->ip_dst.s_addr)) {
  487. ip_hdr->ip_dst.s_addr = remap_ipv4(tcpedit, tcpedit->dstipmap->to, ip_hdr->ip_dst.s_addr);
  488. dbgx(2, "Remapped src addr to: %s", get_addr2name4(ip_hdr->ip_dst.s_addr, RESOLVE));
  489. }
  490. }
  491. /* anything else to rewrite? */
  492. if (tcpedit->cidrmap1 == NULL)
  493. return(0);
  494. /* don't play with the main pointers */
  495. if (direction == TCPR_DIR_C2S) {
  496. cidrmap1 = tcpedit->cidrmap1;
  497. cidrmap2 = tcpedit->cidrmap2;
  498. } else {
  499. cidrmap1 = tcpedit->cidrmap2;
  500. cidrmap2 = tcpedit->cidrmap1;
  501. }
  502. /* loop through the cidrmap to rewrite */
  503. do {
  504. if ((! diddst) && ip_in_cidr(cidrmap2->from, ip_hdr->ip_dst.s_addr)) {
  505. ip_hdr->ip_dst.s_addr = remap_ipv4(tcpedit, cidrmap2->to, ip_hdr->ip_dst.s_addr);
  506. dbgx(2, "Remapped dst addr to: %s", get_addr2name4(ip_hdr->ip_dst.s_addr, RESOLVE));
  507. diddst = 1;
  508. }
  509. if ((! didsrc) && ip_in_cidr(cidrmap1->from, ip_hdr->ip_src.s_addr)) {
  510. ip_hdr->ip_src.s_addr = remap_ipv4(tcpedit, cidrmap1->to, ip_hdr->ip_src.s_addr);
  511. dbgx(2, "Remapped src addr to: %s", get_addr2name4(ip_hdr->ip_src.s_addr, RESOLVE));
  512. didsrc = 1;
  513. }
  514. /*
  515. * loop while we haven't modified both src/dst AND
  516. * at least one of the cidr maps have a next pointer
  517. */
  518. if ((! (diddst && didsrc)) &&
  519. (! ((cidrmap1->next == NULL) && (cidrmap2->next == NULL)))) {
  520. /* increment our ptr's if possible */
  521. if (cidrmap1->next != NULL)
  522. cidrmap1 = cidrmap1->next;
  523. if (cidrmap2->next != NULL)
  524. cidrmap2 = cidrmap2->next;
  525. } else {
  526. loop = 0;
  527. }
  528. /* Later on we should support various IP protocols which embed
  529. * the IP address in the application layer. Things like
  530. * DNS and FTP.
  531. */
  532. } while (loop);
  533. /* return how many changes we made */
  534. return (diddst + didsrc);
  535. }
  536. int
  537. rewrite_ipv6l3(tcpedit_t *tcpedit, ipv6_hdr_t *ip6_hdr, tcpr_dir_t direction)
  538. {
  539. tcpr_cidrmap_t *cidrmap1 = NULL, *cidrmap2 = NULL;
  540. int didsrc = 0, diddst = 0, loop = 1;
  541. assert(tcpedit);
  542. assert(ip6_hdr);
  543. /* first check the src/dst IP maps */
  544. if (tcpedit->srcipmap != NULL) {
  545. if (ip6_in_cidr(tcpedit->srcipmap->from, &ip6_hdr->ip_src)) {
  546. remap_ipv6(tcpedit, tcpedit->srcipmap->to, &ip6_hdr->ip_src);
  547. dbgx(2, "Remapped src addr to: %s", get_addr2name6(&ip6_hdr->ip_src, RESOLVE));
  548. }
  549. }
  550. if (tcpedit->dstipmap != NULL) {
  551. if (ip6_in_cidr(tcpedit->dstipmap->from, &ip6_hdr->ip_dst)) {
  552. remap_ipv6(tcpedit, tcpedit->dstipmap->to, &ip6_hdr->ip_dst);
  553. dbgx(2, "Remapped src addr to: %s", get_addr2name6(&ip6_hdr->ip_dst, RESOLVE));
  554. }
  555. }
  556. /* anything else to rewrite? */
  557. if (tcpedit->cidrmap1 == NULL)
  558. return(0);
  559. /* don't play with the main pointers */
  560. if (direction == TCPR_DIR_C2S) {
  561. cidrmap1 = tcpedit->cidrmap1;
  562. cidrmap2 = tcpedit->cidrmap2;
  563. } else {
  564. cidrmap1 = tcpedit->cidrmap2;
  565. cidrmap2 = tcpedit->cidrmap1;
  566. }
  567. /* loop through the cidrmap to rewrite */
  568. do {
  569. if ((! diddst) && ip6_in_cidr(cidrmap2->from, &ip6_hdr->ip_dst)) {
  570. remap_ipv6(tcpedit, cidrmap2->to, &ip6_hdr->ip_dst);
  571. dbgx(2, "Remapped dst addr to: %s", get_addr2name6(&ip6_hdr->ip_dst, RESOLVE));
  572. diddst = 1;
  573. }
  574. if ((! didsrc) && ip6_in_cidr(cidrmap1->from, &ip6_hdr->ip_src)) {
  575. remap_ipv6(tcpedit, cidrmap1->to, &ip6_hdr->ip_src);
  576. dbgx(2, "Remapped src addr to: %s", get_addr2name6(&ip6_hdr->ip_src, RESOLVE));
  577. didsrc = 1;
  578. }
  579. /*
  580. * loop while we haven't modified both src/dst AND
  581. * at least one of the cidr maps have a next pointer
  582. */
  583. if ((! (diddst && didsrc)) &&
  584. (! ((cidrmap1->next == NULL) && (cidrmap2->next == NULL)))) {
  585. /* increment our ptr's if possible */
  586. if (cidrmap1->next != NULL)
  587. cidrmap1 = cidrmap1->next;
  588. if (cidrmap2->next != NULL)
  589. cidrmap2 = cidrmap2->next;
  590. } else {
  591. loop = 0;
  592. }
  593. /* Later on we should support various IP protocols which embed
  594. * the IP address in the application layer. Things like
  595. * DNS and FTP.
  596. */
  597. } while (loop);
  598. /* return how many changes we made */
  599. return (diddst + didsrc);
  600. }
  601. /**
  602. * Randomize the IP addresses in an ARP packet based on the user seed
  603. * return 0 if no change, or 1 for a change
  604. */
  605. int
  606. randomize_iparp(tcpedit_t *tcpedit, struct pcap_pkthdr *pkthdr,
  607. u_char *pktdata, int datalink)
  608. {
  609. arp_hdr_t *arp_hdr = NULL;
  610. int l2len = 0;
  611. u_int32_t *ip, tempip;
  612. u_char *add_hdr;
  613. assert(tcpedit);
  614. assert(pkthdr);
  615. assert(pktdata);
  616. l2len = get_l2len(pktdata, pkthdr->caplen, datalink);
  617. arp_hdr = (arp_hdr_t *)(pktdata + l2len);
  618. /*
  619. * only rewrite IP addresses from REPLY/REQUEST's
  620. */
  621. if ((ntohs(arp_hdr->ar_pro) == ETHERTYPE_IP) &&
  622. ((ntohs(arp_hdr->ar_op) == ARPOP_REQUEST) ||
  623. (ntohs(arp_hdr->ar_op) == ARPOP_REPLY))) {
  624. /* jump to the addresses */
  625. add_hdr = (u_char *)arp_hdr;
  626. add_hdr += sizeof(arp_hdr_t) + arp_hdr->ar_hln;
  627. ip = (u_int32_t *)add_hdr;
  628. tempip = randomize_ipv4_addr(tcpedit, *ip);
  629. memcpy(ip, &tempip, sizeof(u_int32_t));
  630. add_hdr += arp_hdr->ar_pln + arp_hdr->ar_hln;
  631. ip = (u_int32_t *)add_hdr;
  632. tempip = randomize_ipv4_addr(tcpedit, *ip);
  633. memcpy(ip, &tempip, sizeof(u_int32_t));
  634. }
  635. return 1; /* yes we changed the packet */
  636. }
  637. /**
  638. * rewrite IP address (arp)
  639. * uses -a to rewrite (map) one subnet onto another subnet
  640. * pointer must point to the WHOLE and CONTIGOUS memory buffer
  641. * because the arp_hdr_t doesn't have the space for the IP/MAC
  642. * addresses
  643. * return 0 if no change, 1 or 2 if changed
  644. */
  645. int
  646. rewrite_iparp(tcpedit_t *tcpedit, arp_hdr_t *arp_hdr, int cache_mode)
  647. {
  648. u_char *add_hdr = NULL;
  649. u_int32_t *ip1 = NULL, *ip2 = NULL;
  650. u_int32_t newip = 0;
  651. tcpr_cidrmap_t *cidrmap1 = NULL, *cidrmap2 = NULL;
  652. int didsrc = 0, diddst = 0, loop = 1;
  653. #ifdef FORCE_ALIGN
  654. u_int32_t iptemp;
  655. #endif
  656. assert(tcpedit);
  657. assert(arp_hdr);
  658. /* figure out what mapping to use */
  659. if (cache_mode == TCPR_DIR_C2S) {
  660. cidrmap1 = tcpedit->cidrmap1;
  661. cidrmap2 = tcpedit->cidrmap2;
  662. } else if (cache_mode == TCPR_DIR_S2C) {
  663. cidrmap1 = tcpedit->cidrmap2;
  664. cidrmap2 = tcpedit->cidrmap1;
  665. }
  666. /* anything to rewrite? */
  667. if (cidrmap1 == NULL || cidrmap2 == NULL)
  668. return(0);
  669. /*
  670. * must be IPv4 and request or reply
  671. * Do other op codes use the same subheader stub?
  672. * If so we won't need to check the op code.
  673. */
  674. if ((ntohs(arp_hdr->ar_pro) == ETHERTYPE_IP) &&
  675. ((ntohs(arp_hdr->ar_op) == ARPOP_REQUEST) ||
  676. (ntohs(arp_hdr->ar_op) == ARPOP_REPLY)))
  677. {
  678. /* jump to the addresses */
  679. add_hdr = (u_char *)arp_hdr;
  680. add_hdr += sizeof(arp_hdr_t) + arp_hdr->ar_hln;
  681. ip1 = (u_int32_t *)add_hdr;
  682. add_hdr += arp_hdr->ar_pln + arp_hdr->ar_hln;
  683. #ifdef FORCE_ALIGN
  684. /* copy IP2 to a temporary buffer for processing */
  685. memcpy(&iptemp, add_hdr, sizeof(u_int32_t));
  686. ip2 = &iptemp;
  687. #else
  688. ip2 = (u_int32_t *)add_hdr;
  689. #endif
  690. /* loop through the cidrmap to rewrite */
  691. do {
  692. /* arp request ? */
  693. if (ntohs(arp_hdr->ar_op) == ARPOP_REQUEST) {
  694. if ((!diddst) && ip_in_cidr(cidrmap2->from, *ip1)) {
  695. newip = remap_ipv4(tcpedit, cidrmap2->to, *ip1);
  696. memcpy(ip1, &newip, 4);
  697. diddst = 1;
  698. }
  699. if ((!didsrc) && ip_in_cidr(cidrmap1->from, *ip2)) {
  700. newip = remap_ipv4(tcpedit, cidrmap1->to, *ip2);
  701. memcpy(ip2, &newip, 4);
  702. didsrc = 1;
  703. }
  704. }
  705. /* else it's an arp reply */
  706. else {
  707. if ((!diddst) && ip_in_cidr(cidrmap2->from, *ip2)) {
  708. newip = remap_ipv4(tcpedit, cidrmap2->to, *ip2);
  709. memcpy(ip2, &newip, 4);
  710. diddst = 1;
  711. }
  712. if ((!didsrc) && ip_in_cidr(cidrmap1->from, *ip1)) {
  713. newip = remap_ipv4(tcpedit, cidrmap1->to, *ip1);
  714. memcpy(ip1, &newip, 4);
  715. didsrc = 1;
  716. }
  717. }
  718. #ifdef FORCE_ALIGN
  719. /* copy temporary IP to IP2 location in buffer */
  720. memcpy(add_hdr, &iptemp, sizeof(u_int32_t));
  721. #endif
  722. /*
  723. * loop while we haven't modified both src/dst AND
  724. * at least one of the cidr maps have a next pointer
  725. */
  726. if ((! (diddst && didsrc)) &&
  727. (! ((cidrmap1->next == NULL) && (cidrmap2->next == NULL)))) {
  728. /* increment our ptr's if possible */
  729. if (cidrmap1->next != NULL)
  730. cidrmap1 = cidrmap1->next;
  731. if (cidrmap2->next != NULL)
  732. cidrmap2 = cidrmap2->next;
  733. } else {
  734. loop = 0;
  735. }
  736. } while (loop);
  737. } else {
  738. warn("ARP packet isn't for IPv4! Can't rewrite IP's");
  739. }
  740. return(didsrc + diddst);
  741. }
  742. /**
  743. * returns 1 if the IP address is a unicast address, otherwise, returns 0
  744. * for broadcast/multicast addresses. Returns -1 on error
  745. */
  746. static int
  747. is_unicast_ipv4(tcpedit_t *tcpedit, u_int32_t ip)
  748. {
  749. assert(tcpedit);
  750. /* multicast/broadcast is 224.0.0.0 or greater */
  751. if (ip > 3758096384)
  752. return 0;
  753. return 1;
  754. }
  755. /**
  756. * returns 1 if the IPv6 address is a multicast address, otherwise, returns 0
  757. * for unicast/anycast addresses. Returns -1 on error
  758. */
  759. static int
  760. is_multicast_ipv6(tcpedit_t *tcpedit, struct tcpr_in6_addr *addr)
  761. {
  762. assert(tcpedit);
  763. if (addr->tcpr_s6_addr[0] == 0xff)
  764. return 1;
  765. return 0;
  766. }