get.c 26 KB

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