get.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  1. /* $Id$ */
  2. /*
  3. * Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
  4. * Copyright (c) 2013-2026 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 "defines.h"
  20. #include "config.h"
  21. #include "common.h"
  22. #include <lib/sll.h>
  23. #include <arpa/inet.h>
  24. #include <ctype.h>
  25. #include <netinet/in.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <sys/socket.h>
  29. #include <sys/types.h>
  30. #if defined HAVE_PCAP_VERSION && !defined HAVE_WIN32
  31. extern const char pcap_version[];
  32. #endif
  33. #define JUNIPER_FLAG_NO_L2 0x02 /* L2 header */
  34. #define JUNIPER_FLAG_EXT 0x80 /* Juniper extensions present */
  35. #define JUNIPER_PCAP_MAGIC "MGC"
  36. static void *get_ipv6_next(struct tcpr_ipv6_ext_hdr_base *exthdr, const u_char *end_ptr);
  37. /**
  38. * Depending on what version of libpcap there are different ways to get
  39. * the version of the libpcap library. This presents a unified way to
  40. * get that information.
  41. */
  42. const char *
  43. get_pcap_version(void)
  44. {
  45. #if defined HAVE_WINPCAP
  46. static char ourver[255];
  47. char *last, *version;
  48. /* WinPcap returns a string like:
  49. * WinPcap version 4.0 (packet.dll version 4.0.0.755), based on libpcap version 0.9.5
  50. */
  51. version = safe_strdup(pcap_lib_version());
  52. strtok_r(version, " ", &last);
  53. strtok_r(NULL, " ", &last);
  54. strlcpy(ourver, strtok_r(NULL, " ", &last), 255);
  55. safe_free(version);
  56. return ourver;
  57. #elif defined HAVE_PCAP_VERSION
  58. return pcap_version;
  59. #else
  60. return pcap_lib_version();
  61. #endif
  62. }
  63. /*
  64. * Advance L2 protocol and L2 length past any MPLS labels.
  65. * e.g. https://www.cloudshark.org/captures/20f210391b21
  66. *
  67. * If EoMPLS is detected, also advance L2 offset to point to the
  68. * encapsulated L2.
  69. * e.g. https://www.cloudshark.org/captures/b15412060b3d
  70. *
  71. * pktdata: pointer to the raw packet
  72. * datalen: number of bytes captured in the packet
  73. * next_protocol: reference to the next L2 protocol to be examined and possibly updated
  74. * l2len: reference to the length of the L2 header discovered so far
  75. * l2offset: reference to the offset to the start of the L2 header - typically 0
  76. *
  77. * return 0 on success, -1 on failure
  78. */
  79. int
  80. parse_mpls(const u_char *pktdata, uint32_t datalen, uint16_t *next_protocol, uint32_t *l2len, uint32_t *l2offset)
  81. {
  82. struct tcpr_mpls_label *mpls_label;
  83. const u_char *end_ptr = pktdata + datalen;
  84. u_char first_nibble;
  85. eth_hdr_t *eth_hdr;
  86. bool bos = false;
  87. uint32_t label;
  88. int len;
  89. assert(next_protocol);
  90. assert(l2len);
  91. assert(l2offset);
  92. len = (int)*l2len;
  93. /* move over MPLS labels until we get to the last one */
  94. while (!bos) {
  95. if (pktdata + len + sizeof(*mpls_label) > end_ptr) {
  96. warnx("parse_mpls: Need at least %zu bytes for MPLS header but only %u available",
  97. sizeof(*mpls_label) + len,
  98. datalen);
  99. return -1;
  100. }
  101. mpls_label = (struct tcpr_mpls_label *)(pktdata + len);
  102. len += sizeof(*mpls_label);
  103. bos = (ntohl(mpls_label->entry) & MPLS_LS_S_MASK) != 0;
  104. label = ntohl(mpls_label->entry) >> MPLS_LS_LABEL_SHIFT;
  105. if (label == MPLS_LABEL_GACH) {
  106. /* Generic Associated Channel Header */
  107. warn("GACH MPLS label not supported at this time");
  108. return -1;
  109. }
  110. }
  111. if ((u_char *)(mpls_label + 1) + 1 > end_ptr) {
  112. warnx("parse_mpls: Need at least %zu bytes for MPLS label but only %u available",
  113. sizeof(*mpls_label) + 1,
  114. datalen);
  115. return -1;
  116. }
  117. first_nibble = *((u_char *)(mpls_label + 1)) >> 4;
  118. switch (first_nibble) {
  119. case 4:
  120. *next_protocol = ETHERTYPE_IP;
  121. break;
  122. case 6:
  123. *next_protocol = ETHERTYPE_IP6;
  124. break;
  125. case 0:
  126. /* EoMPLS - jump over PW Ethernet Control Word and handle
  127. * inner Ethernet header
  128. */
  129. if (pktdata + len + 4 + sizeof(*eth_hdr) > end_ptr) {
  130. warnx("parse_mpls: Need at least %zu bytes for EoMPLS header but only %u available",
  131. sizeof(*eth_hdr) + len + 4,
  132. datalen);
  133. return -1;
  134. }
  135. len += 4;
  136. *l2offset = len;
  137. eth_hdr = (eth_hdr_t *)(pktdata + len);
  138. len += sizeof(*eth_hdr);
  139. *next_protocol = ntohs(eth_hdr->ether_type);
  140. break;
  141. default:
  142. warn("parse_mpls:suspect Generic Associated Channel Header");
  143. return -1;
  144. }
  145. *l2len = (uint32_t)len;
  146. return 0;
  147. }
  148. /*
  149. * Advance L2 protocol and L2 length past any VLAN tags.
  150. * e.g. https://www.cloudshark.org/captures/e4fa464563d2
  151. *
  152. * pktdata: pointer to the raw packet
  153. * datalen: number of bytes captured in the packet
  154. * next_protocol: reference to the next L2 protocol to be examined and possibly updated
  155. * l2len: reference to the length of the L2 header discovered so far
  156. *
  157. * return 0 on success, -1 on failure
  158. */
  159. int
  160. parse_vlan(const u_char *pktdata, uint32_t datalen, uint16_t *next_protocol, uint32_t *l2len)
  161. {
  162. vlan_hdr_t *vlan_hdr;
  163. if ((size_t)datalen < *l2len + sizeof(*vlan_hdr)) {
  164. warnx("parse_vlan: Need at least %zu bytes for VLAN header but only %u available", sizeof(*vlan_hdr), datalen);
  165. return -1;
  166. }
  167. vlan_hdr = (vlan_hdr_t *)(pktdata + *l2len);
  168. *next_protocol = ntohs(vlan_hdr->vlan_tpid);
  169. *l2len += sizeof(vlan_hdr_t);
  170. return 0;
  171. }
  172. /*
  173. * Loop through all non-protocol L2 headers while updating key variables
  174. *
  175. * pktdata: pointer to the raw packet
  176. * datalen: number of bytes captured in the packet
  177. * next_protocol: reference to the next L2 protocol to be examined and possibly updated
  178. * l2len: reference to the length of the L2 header discovered so far
  179. * l2offset: reference to the offset to the start of the L2 header - typically 0
  180. * vlan_offset: reference to the offset to the start of the VLAN headers, if any
  181. *
  182. * return 0 on success, -1 on failure
  183. */
  184. static int
  185. parse_metadata(const u_char *pktdata,
  186. uint32_t datalen,
  187. uint16_t *next_protocol,
  188. uint32_t *l2len,
  189. uint32_t *l2offset,
  190. uint32_t *vlan_offset)
  191. {
  192. bool done = false;
  193. assert(next_protocol);
  194. assert(l2len);
  195. assert(l2offset);
  196. assert(vlan_offset);
  197. if (!pktdata || !datalen)
  198. errx(-1, "parse_metadata: invalid L2 parameters: pktdata=0x%p len=%d", pktdata, datalen);
  199. while (!done) {
  200. switch (*next_protocol) {
  201. case ETHERTYPE_VLAN:
  202. case ETHERTYPE_Q_IN_Q:
  203. case ETHERTYPE_8021QINQ:
  204. if (*vlan_offset == 0)
  205. *vlan_offset = *l2len;
  206. if (parse_vlan(pktdata, datalen, next_protocol, l2len))
  207. return -1;
  208. break;
  209. case ETHERTYPE_MPLS:
  210. case ETHERTYPE_MPLS_MULTI:
  211. if (parse_mpls(pktdata, datalen, next_protocol, l2len, l2offset))
  212. return -1;
  213. break;
  214. default:
  215. done = true;
  216. }
  217. }
  218. return 0;
  219. }
  220. /*
  221. * Parse raw packet and get the L3 protocol and L2 length. In cases where the
  222. * L2 header is not at the beginning of the packet
  223. * (e.g. DLT_JUNIPER_ETHER or EoMPLS), report the offset to the start of the
  224. * L2 header
  225. *
  226. * pktdata: pointer to the raw packet
  227. * datalen: number of bytes captured in the packet
  228. * datalink: data link type of the packet
  229. * protocol: reference to the L3 protocol as discovered in the L2 header
  230. * l2len: reference to the total length of the L2 header
  231. * l2offset: reference to the offset to the start of the L2 header (typically 0)
  232. * vlan_offset: reference to the offset to the start of the VLAN headers, if any
  233. *
  234. * return 0 on success, -1 on failure
  235. */
  236. int
  237. get_l2len_protocol(const u_char *pktdata,
  238. uint32_t datalen,
  239. int datalink,
  240. uint16_t *protocol,
  241. uint32_t *l2len,
  242. uint32_t *l2offset,
  243. uint32_t *vlan_offset)
  244. {
  245. assert(protocol);
  246. assert(l2len);
  247. assert(l2offset);
  248. assert(vlan_offset);
  249. if (!pktdata || !datalen) {
  250. err_no_exitx("get_l2len_protocol: invalid L2 parameters: pktdata=0x%p len=%d", pktdata, datalen);
  251. return -1;
  252. }
  253. *protocol = 0;
  254. *l2len = 0;
  255. *l2offset = 0;
  256. *vlan_offset = 0;
  257. switch (datalink) {
  258. case DLT_NULL:
  259. case DLT_RAW:
  260. if ((pktdata[0] >> 4) == 4)
  261. *protocol = ETHERTYPE_IP;
  262. else if ((pktdata[0] >> 4) == 6)
  263. *protocol = ETHERTYPE_IP6;
  264. break;
  265. case DLT_JUNIPER_ETHER:
  266. if (datalen < 4) {
  267. warnx("%s (0x%x): Need at least 4 bytes for DLT_JUNIPER_ETHER but only %u available",
  268. pcap_datalink_val_to_description(datalink),
  269. datalink,
  270. datalen);
  271. return -1;
  272. }
  273. if (memcmp(pktdata, JUNIPER_PCAP_MAGIC, 3) != 0) {
  274. warnx("%s (0x%x): No JUNIPER_PCAP_MAGIC Magic Number found during protocol lookup",
  275. pcap_datalink_val_to_description(datalink),
  276. datalink);
  277. return -1;
  278. }
  279. if ((pktdata[3] & JUNIPER_FLAG_EXT) == JUNIPER_FLAG_EXT) {
  280. if (datalen < 6) {
  281. warnx("%s (0x%x): Need at least 6 bytes for JUNIPER_FLAG_EXT but only %u available",
  282. pcap_datalink_val_to_description(datalink),
  283. datalink,
  284. datalen);
  285. return -1;
  286. }
  287. *l2offset = ntohs(*((uint16_t *)&pktdata[4]));
  288. *l2offset += 6; /* MGC + flags + ext_total_len */
  289. } else {
  290. *l2offset = 4; /* MGC + flags (no header extensions) */
  291. }
  292. if ((pktdata[3] & JUNIPER_FLAG_NO_L2) == JUNIPER_FLAG_NO_L2) {
  293. /* no L2 header present - *l2offset is actually IP offset */
  294. uint32_t ip_hdr_offset = *l2offset;
  295. uint32_t hdrSpaceNeeded = ip_hdr_offset + 1;
  296. if (datalen < hdrSpaceNeeded) {
  297. warnx("%s (0x%x): Need at least %u bytes for JUNIPER_FLAG_NO_L2 but only %u available",
  298. pcap_datalink_val_to_description(datalink),
  299. hdrSpaceNeeded,
  300. datalink,
  301. datalen);
  302. return -1;
  303. }
  304. if ((pktdata[ip_hdr_offset] >> 4) == 4)
  305. *protocol = ETHERTYPE_IP;
  306. else if ((pktdata[ip_hdr_offset] >> 4) == 6)
  307. *protocol = ETHERTYPE_IP6;
  308. return 0;
  309. }
  310. /* fall through */
  311. case DLT_EN10MB: {
  312. eth_hdr_t *eth_hdr;
  313. uint16_t ether_type;
  314. uint32_t l2_net_off = sizeof(*eth_hdr) + *l2offset;
  315. if (datalen <= l2_net_off + 4) {
  316. warnx("%s (0x%x): Need at least %u bytes for DLT_EN10MB but only %u available",
  317. pcap_datalink_val_to_description(datalink),
  318. datalink,
  319. l2_net_off + 4,
  320. datalen);
  321. return -1;
  322. }
  323. eth_hdr = (eth_hdr_t *)(pktdata + *l2offset);
  324. ether_type = ntohs(eth_hdr->ether_type);
  325. if (parse_metadata(pktdata, datalen, &ether_type, &l2_net_off, l2offset, vlan_offset))
  326. return -1;
  327. *l2len = l2_net_off;
  328. if (ether_type >= 1536) {
  329. /* Ethernet II frame - return in host order */
  330. *protocol = ether_type;
  331. } else if (ether_type > 1500) {
  332. warnx("%s (0x%x): unsupported 802.3 length %u",
  333. pcap_datalink_val_to_description(datalink),
  334. datalink,
  335. ether_type);
  336. return -1;
  337. } else {
  338. /* 803.3 frame */
  339. /* we don't modify 802.3 protocols */
  340. return -1;
  341. }
  342. break;
  343. }
  344. case DLT_PPP_SERIAL:
  345. if ((size_t)datalen < sizeof(struct tcpr_pppserial_hdr)) {
  346. warnx("%s (0x%x): Need at least %zu bytes for DLT_PPP_SERIAL but only %u available",
  347. pcap_datalink_val_to_description(datalink),
  348. datalink,
  349. sizeof(struct tcpr_pppserial_hdr),
  350. datalen);
  351. return -1;
  352. }
  353. struct tcpr_pppserial_hdr *ppp = (struct tcpr_pppserial_hdr *)pktdata;
  354. *l2len = sizeof(*ppp);
  355. if (ntohs(ppp->protocol) == 0x0021)
  356. *protocol = ETHERTYPE_IP;
  357. else
  358. *protocol = ntohs(ppp->protocol);
  359. break;
  360. case DLT_C_HDLC:
  361. if (datalen < CISCO_HDLC_LEN) {
  362. warnx("%s (0x%x): Need at least %u bytes for DLT_C_HDLC but only %u available",
  363. pcap_datalink_val_to_description(datalink),
  364. datalink,
  365. CISCO_HDLC_LEN,
  366. datalen);
  367. return -1;
  368. }
  369. hdlc_hdr_t *hdlc_hdr = (hdlc_hdr_t *)pktdata;
  370. *l2len = sizeof(*hdlc_hdr);
  371. *protocol = ntohs(hdlc_hdr->protocol);
  372. break;
  373. case DLT_LINUX_SLL:
  374. if (datalen < SLL_HDR_LEN) {
  375. warnx("%s (0x%x): Need at least %u bytes for DLT_LINUX_SLL but only %u available",
  376. pcap_datalink_val_to_description(datalink),
  377. datalink,
  378. SLL_HDR_LEN,
  379. datalen);
  380. return -1;
  381. }
  382. *l2len = SLL_HDR_LEN;
  383. sll_hdr_t *sll_hdr = (sll_hdr_t *)pktdata;
  384. *protocol = ntohs(sll_hdr->sll_protocol);
  385. break;
  386. case DLT_LINUX_SLL2:
  387. if (datalen < SLL2_HDR_LEN) {
  388. warnx("%s (0x%x): Need at least %u bytes for DLT_LINUX_SLL2 but only %u available",
  389. pcap_datalink_val_to_description(datalink),
  390. datalink,
  391. SLL2_HDR_LEN,
  392. datalen);
  393. return -1;
  394. }
  395. *l2len = SLL2_HDR_LEN;
  396. sll2_hdr_t *sll2_hdr = (sll2_hdr_t *)pktdata;
  397. *protocol = ntohs(sll2_hdr->sll2_protocol);
  398. break;
  399. default:
  400. errx(-1,
  401. "Unable to process unsupported DLT type: %s (0x%x)",
  402. pcap_datalink_val_to_description(datalink),
  403. datalink);
  404. }
  405. return 0;
  406. }
  407. /**
  408. * returns the length in number of bytes of the L2 header, or -1 on error
  409. */
  410. int
  411. get_l2len(const u_char *pktdata, int datalen, int datalink)
  412. {
  413. uint16_t _U_ protocol;
  414. uint32_t _U_ l2offset;
  415. uint32_t _U_ vlan_offset;
  416. uint32_t l2len = 0;
  417. int res = get_l2len_protocol(pktdata, datalen, datalink, &protocol, &l2len, &l2offset, &vlan_offset);
  418. if (res == -1)
  419. return 0;
  420. return (int)l2len;
  421. }
  422. /**
  423. * \brief returns a ptr to the ipv4 header + data or NULL if it's not IP
  424. *
  425. * we may use an extra buffer for the IP header (and above)
  426. * on strictly aligned systems where the layer 2 header doesn't
  427. * fall on a 4 byte boundary (like a standard Ethernet header)
  428. *
  429. * Note: you can cast the result as an ip_hdr_t, but you'll be able
  430. * to access data above the header minus any stripped L2 data
  431. */
  432. const u_char *
  433. get_ipv4(const u_char *pktdata, int datalen, int datalink, u_char **newbuff)
  434. {
  435. const u_char *packet = pktdata;
  436. const u_char *ip_hdr = NULL;
  437. ssize_t pkt_len = datalen;
  438. uint32_t _U_ vlan_offset;
  439. uint32_t l2offset;
  440. uint16_t proto;
  441. uint32_t l2len;
  442. int res;
  443. assert(packet);
  444. assert(pkt_len);
  445. assert(*newbuff);
  446. res = get_l2len_protocol(packet, pkt_len, datalink, &proto, &l2len, &l2offset, &vlan_offset);
  447. /* sanity... pkt_len must be > l2len + IP header len*/
  448. if (res == -1 || l2len + TCPR_IPV4_H > pkt_len) {
  449. dbg(1, "get_ipv4(): Layer 2 len > total packet len, hence no IP header");
  450. return NULL;
  451. }
  452. if (proto != ETHERTYPE_IP)
  453. return NULL;
  454. packet += l2offset;
  455. l2len -= l2offset;
  456. #ifdef FORCE_ALIGN
  457. pkt_len -= l2offset;
  458. /*
  459. * copy layer 3 and up to our temp packet buffer
  460. * for now on, we have to edit the packetbuff because
  461. * just before we send the packet, we copy the packetbuff
  462. * back onto the pkt.data + l2len buffer
  463. * we do all this work to prevent byte alignment issues
  464. */
  465. if (l2len % sizeof(long)) {
  466. memcpy(*newbuff, (packet + l2len), (pkt_len - l2len));
  467. ip_hdr = *newbuff;
  468. } else {
  469. /* we don't have to do a memcpy if l2len lands on a boundary */
  470. ip_hdr = (packet + l2len);
  471. }
  472. #else
  473. /*
  474. * on non-strict byte align systems, don't need to memcpy(),
  475. * just point to l2len bytes into the existing buffer
  476. */
  477. ip_hdr = (packet + l2len);
  478. #endif
  479. return ip_hdr;
  480. }
  481. /**
  482. * \brief returns a ptr to the ipv6 header + data or NULL if it's not IP
  483. *
  484. * we may use an extra buffer for the IP header (and above)
  485. * on strictly aligned systems where the layer 2 header doesn't
  486. * fall on a 4 byte boundary (like a standard Ethernet header)
  487. *
  488. * Note: you can cast the result as an ip_hdr_t, but you'll be able
  489. * to access data above the header minus any stripped L2 data
  490. */
  491. const u_char *
  492. get_ipv6(const u_char *pktdata, int datalen, int datalink, u_char **newbuff)
  493. {
  494. const u_char *packet = pktdata;
  495. const u_char *ip6_hdr = NULL;
  496. ssize_t pkt_len = datalen;
  497. uint32_t _U_ vlan_offset;
  498. uint32_t l2offset;
  499. uint16_t proto;
  500. uint32_t l2len;
  501. int res;
  502. assert(packet);
  503. assert(pkt_len);
  504. assert(*newbuff);
  505. res = get_l2len_protocol(packet, pkt_len, datalink, &proto, &l2len, &l2offset, &vlan_offset);
  506. /* sanity... pkt_len must be > l2len + IP header len*/
  507. if (res == -1 || l2len + TCPR_IPV6_H > pkt_len) {
  508. dbg(1, "get_ipv6(): Layer 2 len > total packet len, hence no IPv6 header");
  509. return NULL;
  510. }
  511. if (proto != ETHERTYPE_IP6)
  512. return NULL;
  513. packet += l2offset;
  514. l2len -= l2offset;
  515. #ifdef FORCE_ALIGN
  516. pkt_len -= l2offset;
  517. /*
  518. * copy layer 3 and up to our temp packet buffer
  519. * for now on, we have to edit the packetbuff because
  520. * just before we send the packet, we copy the packetbuff
  521. * back onto the pkt.data + l2len buffer
  522. * we do all this work to prevent byte alignment issues
  523. */
  524. if (l2len % sizeof(long)) {
  525. memcpy(*newbuff, (packet + l2len), (pkt_len - l2len));
  526. ip6_hdr = *newbuff;
  527. } else {
  528. /* we don't have to do a memcpy if l2len lands on a boundary */
  529. ip6_hdr = (packet + l2len);
  530. }
  531. #else
  532. /*
  533. * on non-strict byte align systems, don't need to memcpy(),
  534. * just point to l2len bytes into the existing buffer
  535. */
  536. ip6_hdr = (packet + l2len);
  537. #endif
  538. return ip6_hdr;
  539. }
  540. /**
  541. * \brief returns a pointer to the layer 4 header which is just beyond the IPv4 header
  542. *
  543. * If the packet is to short, returns NULL
  544. */
  545. void *
  546. get_layer4_v4(const ipv4_hdr_t *ip_hdr, const u_char *end_ptr)
  547. {
  548. void *ptr;
  549. assert(ip_hdr);
  550. assert(end_ptr);
  551. ptr = (u_char *)ip_hdr + (ip_hdr->ip_hl << 2);
  552. /* make sure we don't jump over the end of the buffer */
  553. if ((u_char *)ptr > end_ptr)
  554. return NULL;
  555. return ((void *)ptr);
  556. }
  557. /**
  558. * returns a pointer to the layer 4 header which is just beyond the IPv6 header
  559. * and any extension headers or NULL when there is none as in the case of
  560. * v6 Frag or ESP header. Function is recursive.
  561. */
  562. void *
  563. get_layer4_v6(const ipv6_hdr_t *ip6_hdr, const u_char *end_ptr)
  564. {
  565. struct tcpr_ipv6_ext_hdr_base *next, *exthdr;
  566. bool done = false;
  567. uint8_t proto;
  568. assert(ip6_hdr);
  569. assert(end_ptr);
  570. /* jump to the end of the IPv6 header */
  571. next = (struct tcpr_ipv6_ext_hdr_base *)((u_char *)ip6_hdr + TCPR_IPV6_H);
  572. if ((u_char *)next > end_ptr)
  573. return NULL;
  574. proto = ip6_hdr->ip_nh;
  575. while (!done) {
  576. dbgx(3, "Processing proto: 0x%hx", (uint16_t)proto);
  577. switch (proto) {
  578. /* recurse due to v6-in-v6, need to recast next as an IPv6 Header */
  579. case TCPR_IPV6_NH_IPV6:
  580. dbg(3, "recursing due to v6-in-v6");
  581. next = get_layer4_v6((ipv6_hdr_t *)next, end_ptr);
  582. break;
  583. /* loop again */
  584. case TCPR_IPV6_NH_AH:
  585. case TCPR_IPV6_NH_ROUTING:
  586. case TCPR_IPV6_NH_DESTOPTS:
  587. case TCPR_IPV6_NH_HBH:
  588. dbgx(3, "Going deeper due to extension header 0x%02X", proto);
  589. exthdr = get_ipv6_next(next, end_ptr);
  590. if (exthdr == NULL) {
  591. next = NULL;
  592. done = true;
  593. break;
  594. }
  595. proto = exthdr->ip_nh;
  596. next = exthdr;
  597. break;
  598. /*
  599. * handle (unparsable) IPv6 fragment data
  600. */
  601. case TCPR_IPV6_NH_FRAGMENT:
  602. // next points to l4 data
  603. dbgx(3, "Go deeper due to fragment extension header 0x%02X", proto);
  604. exthdr = get_ipv6_next(next, end_ptr);
  605. if ((exthdr == NULL) || ((u_char *)exthdr > end_ptr)) {
  606. next = NULL;
  607. done = true;
  608. break;
  609. }
  610. proto = exthdr->ip_nh;
  611. next = exthdr;
  612. // done = true;
  613. break;
  614. /*
  615. * Can't handle. Unparsable IPv6 encrypted data
  616. */
  617. case TCPR_IPV6_NH_ESP:
  618. next = NULL;
  619. done = true;
  620. break;
  621. /*
  622. * no further processing, either TCP, UDP, ICMP, etc...
  623. */
  624. default:
  625. if (proto != ip6_hdr->ip_nh && next) {
  626. dbgx(3, "Returning byte offset of this ext header: %u", IPV6_EXTLEN_TO_BYTES(next->ip_len));
  627. next = (void *)((u_char *)next + IPV6_EXTLEN_TO_BYTES(next->ip_len));
  628. if ((u_char*)next > end_ptr)
  629. return NULL;
  630. } else {
  631. dbgx(3, "%s", "Returning end of IPv6 Header");
  632. }
  633. done = true;
  634. } /* switch */
  635. if (next == NULL)
  636. done = true;
  637. } /* while */
  638. return next;
  639. }
  640. /**
  641. * returns the next payload or header of the current extension header
  642. * returns NULL for none/ESP.
  643. */
  644. static void *
  645. get_ipv6_next(struct tcpr_ipv6_ext_hdr_base *exthdr, const u_char *end_ptr)
  646. {
  647. uint8_t extlen;
  648. u_char *ptr;
  649. assert(exthdr);
  650. if ((u_char *)exthdr + sizeof(*exthdr) > end_ptr)
  651. return NULL;
  652. dbgx(3, "Jumping to next IPv6 header. Processing 0x%02x", exthdr->ip_nh);
  653. switch (exthdr->ip_nh) {
  654. /* no further processing */
  655. case TCPR_IPV6_NH_NO_NEXT:
  656. case TCPR_IPV6_NH_ESP:
  657. dbg(3, "No-Next or ESP... can't go any further...");
  658. return NULL;
  659. /*
  660. * fragment header is fixed size
  661. * FIXME: Frag header has further ext headers (has a ip_nh field)
  662. * but I don't support it because there's never a full L4 + payload beyond.
  663. */
  664. case TCPR_IPV6_NH_FRAGMENT:
  665. dbg(3, "Looks like were a fragment header. Returning some frag'd data.");
  666. ptr = (void *)((u_char *)exthdr + sizeof(struct tcpr_ipv6_frag_hdr));
  667. if (ptr > end_ptr)
  668. return NULL;
  669. return (void *)ptr;
  670. /* all the rest require us to go deeper using the ip_len field */
  671. case TCPR_IPV6_NH_IPV6:
  672. case TCPR_IPV6_NH_ROUTING:
  673. case TCPR_IPV6_NH_DESTOPTS:
  674. case TCPR_IPV6_NH_HBH:
  675. case TCPR_IPV6_NH_AH:
  676. extlen = IPV6_EXTLEN_TO_BYTES(exthdr->ip_len);
  677. if (extlen == 0) {
  678. dbg(3, "Malformed IPv6 extension header...");
  679. return NULL;
  680. }
  681. dbgx(3,
  682. "Looks like we're an ext header (0x%hhx). Jumping %u bytes"
  683. " to the next",
  684. exthdr->ip_nh,
  685. extlen);
  686. ptr = (u_char *)exthdr + extlen;
  687. if (ptr > end_ptr)
  688. return NULL;
  689. return (void *)ptr;
  690. default:
  691. dbg(3, "Must not be a v6 extension header... returning self");
  692. return (void *)exthdr;
  693. }
  694. }
  695. /**
  696. * returns the protocol of the actual layer4 header by processing through
  697. * the extension headers
  698. */
  699. uint8_t
  700. get_ipv6_l4proto(const ipv6_hdr_t *ip6_hdr, const u_char *end_ptr)
  701. {
  702. u_char *ptr = (u_char *)ip6_hdr + TCPR_IPV6_H; /* jump to the end of the IPv6 header */
  703. uint8_t proto;
  704. struct tcpr_ipv6_ext_hdr_base *exthdr = NULL;
  705. assert(ip6_hdr);
  706. if (ptr > end_ptr)
  707. return TCPR_IPV6_NH_NO_NEXT;
  708. proto = ip6_hdr->ip_nh;
  709. while (TRUE) {
  710. dbgx(3, "Processing next proto 0x%02X", proto);
  711. switch (proto) {
  712. /* no further processing for IPV6 types with nothing beyond them */
  713. case TCPR_IPV6_NH_NO_NEXT:
  714. case TCPR_IPV6_NH_FRAGMENT:
  715. case TCPR_IPV6_NH_ESP:
  716. dbg(3, "No-Next or ESP... can't go any further...");
  717. return proto;
  718. /* recurse */
  719. case TCPR_IPV6_NH_IPV6:
  720. dbg(3, "Recursing due to v6 in v6");
  721. return get_ipv6_l4proto((ipv6_hdr_t *)ptr, end_ptr);
  722. /* loop again */
  723. case TCPR_IPV6_NH_AH:
  724. case TCPR_IPV6_NH_ROUTING:
  725. case TCPR_IPV6_NH_DESTOPTS:
  726. case TCPR_IPV6_NH_HBH:
  727. dbgx(3, "Jumping to next extension header (0x%hhx)", proto);
  728. exthdr = get_ipv6_next((struct tcpr_ipv6_ext_hdr_base *)ptr, end_ptr);
  729. if (exthdr == NULL || (u_char *)exthdr + sizeof(*exthdr) > end_ptr)
  730. return TCPR_IPV6_NH_NO_NEXT;
  731. proto = exthdr->ip_nh;
  732. ptr = (u_char *)exthdr;
  733. break;
  734. /* should be TCP, UDP or the like */
  735. default:
  736. dbgx(3, "Selecting next L4 Proto as: 0x%02x", proto);
  737. return proto;
  738. } /* switch */
  739. } /* while */
  740. }
  741. /**
  742. * \brief Converts a human readable IPv4 address to a binary one
  743. *
  744. * stolen from LIBNET since I didn't want to have to deal with
  745. * passing a libnet_t around. Returns 0xFFFFFFFF (255.255.255.255)
  746. * on error
  747. */
  748. uint32_t
  749. get_name2addr4(const char *hostname, bool dnslookup)
  750. {
  751. struct in_addr addr;
  752. #if !defined HAVE_INET_ATON && defined HAVE_INET_ADDR
  753. struct hostent *host_ent;
  754. #endif
  755. if (dnslookup) {
  756. #ifdef HAVE_INET_ATON
  757. if (inet_aton(hostname, &addr) != 1) {
  758. return (0xffffffff);
  759. }
  760. #elif defined HAVE_INET_ADDR
  761. if ((addr.s_addr = inet_addr(hostname)) == INADDR_NONE) {
  762. if (!(host_ent = gethostbyname(hostname))) {
  763. warnx("unable to resolve %s: %s", hostname, strerror(errno));
  764. /* this is actually 255.255.255.255 */
  765. return (0xffffffff);
  766. }
  767. /* was: host_ent->h_length); */
  768. memcpy(&addr.s_addr, host_ent->h_addr, sizeof(addr.s_addr));
  769. }
  770. #else
  771. warn("Unable to support get_name2addr4 w/ resolve");
  772. /* call ourselves recursively once w/o resolving the hostname */
  773. return get_name2addr4(hostname, DNS_DONT_RESOLVE);
  774. #endif
  775. /* return in network byte order */
  776. return (addr.s_addr);
  777. } else {
  778. /*
  779. * We only want dots 'n decimals.
  780. */
  781. int i;
  782. uint32_t m;
  783. if (!isdigit(hostname[0])) {
  784. warnx("Expected dotted-quad notation (%s) when DNS lookups are disabled", hostname);
  785. /* XXX - this is actually 255.255.255.255 */
  786. return (-1);
  787. }
  788. m = 0;
  789. for (i = 0; i < 4; i++) {
  790. u_int val;
  791. m <<= 8;
  792. if (*hostname) {
  793. val = 0;
  794. while (*hostname && *hostname != '.') {
  795. val *= 10;
  796. val += *hostname - '0';
  797. if (val > 255) {
  798. dbgx(4, "value %d > 255 for dotted quad", val);
  799. /* this is actually 255.255.255.255 */
  800. return (-1);
  801. }
  802. hostname++;
  803. }
  804. m |= val;
  805. if (*hostname) {
  806. hostname++;
  807. }
  808. }
  809. }
  810. /* host byte order */
  811. return (ntohl(m));
  812. }
  813. }
  814. /**
  815. * \brief Converts human readable IPv6 address to binary value
  816. *
  817. * Wrapper around inet_pton
  818. * Returns 1 for valid, 0 for not parsable and -1 for system error.
  819. * Does not support DNS.
  820. */
  821. int
  822. get_name2addr6(const char *hostname, bool dnslookup, struct tcpr_in6_addr *addr)
  823. {
  824. (void)dnslookup; /* prevent warning about unused arg */
  825. #ifdef HAVE_INET_PTON
  826. return inet_pton(AF_INET6, hostname, addr);
  827. #else
  828. #error "Unable to support get_name2addr6: Missing inet_pton() support."
  829. #endif
  830. }
  831. /**
  832. * \brief Converts binary IPv4 address to a string.
  833. *
  834. * Generic wrapper around inet_ntop() and inet_ntoa() depending on whichever
  835. * is available on your system. Does not support DNS.
  836. */
  837. const char *
  838. get_addr2name4(uint32_t ip, bool _U_ dnslookup)
  839. {
  840. struct in_addr addr;
  841. static char *new_string = NULL;
  842. if (new_string == NULL)
  843. new_string = (char *)safe_malloc(255);
  844. new_string[0] = '\0';
  845. addr.s_addr = ip;
  846. #ifdef HAVE_INET_NTOP
  847. if (inet_ntop(AF_INET, &addr, new_string, 255) == NULL) {
  848. warnx("Unable to convert 0x%x to a string", ip);
  849. new_string[0] = 0;
  850. }
  851. return new_string;
  852. #elif defined HAVE_INET_NTOA
  853. return inet_ntoa(&addr);
  854. #else
  855. #error "Unable to support get_addr2name4."
  856. #endif
  857. }
  858. /**
  859. * \brief Converts a IPv6 binary address to a string.a
  860. *
  861. * Does not support DNS.
  862. */
  863. const char *
  864. get_addr2name6(const struct tcpr_in6_addr *addr, _U_ bool dnslookup)
  865. {
  866. static char *new_string = NULL;
  867. if (new_string == NULL)
  868. new_string = (char *)safe_malloc(255);
  869. new_string[0] = '\0';
  870. #ifdef HAVE_INET_NTOP
  871. if (inet_ntop(AF_INET6, addr, new_string, 255) == NULL) {
  872. warn("Unable to convert addr to a string");
  873. new_string[0] = 0;
  874. }
  875. return new_string;
  876. #else
  877. #error "Unable to support get_addr2name6."
  878. #endif
  879. }
  880. /**
  881. * \brief Converts the binary network address of a tcpr_cidr_t to a string
  882. */
  883. const char *
  884. get_cidr2name(const tcpr_cidr_t *cidr_ptr, bool dnslookup)
  885. {
  886. if (cidr_ptr->family == AF_INET) {
  887. return get_addr2name4(cidr_ptr->u.network, dnslookup);
  888. } else if (cidr_ptr->family == AF_INET6) {
  889. return get_addr2name6(&cidr_ptr->u.network6, dnslookup);
  890. } else {
  891. return NULL;
  892. }
  893. }