get.c 28 KB

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