edit_packet.c 33 KB

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