get.c 28 KB

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