get.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  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. /*
  409. * on non-strict byte align systems, don't need to memcpy(),
  410. * just point to l2len bytes into the existing buffer
  411. */
  412. ip_hdr = (packet + l2len);
  413. return ip_hdr;
  414. }
  415. /**
  416. * \brief returns a ptr to the ipv6 header + data or NULL if it's not IP
  417. *
  418. * we may use an extra buffer for the IP header (and above)
  419. * on strictly aligned systems where the layer 2 header doesn't
  420. * fall on a 4 byte boundary (like a standard Ethernet header)
  421. *
  422. * Note: you can cast the result as an ip_hdr_t, but you'll be able
  423. * to access data above the header minus any stripped L2 data
  424. */
  425. const u_char *
  426. get_ipv6(const u_char *pktdata, int datalen, int datalink, u_char **newbuff)
  427. {
  428. const u_char *packet = pktdata;
  429. const u_char *ip6_hdr = NULL;
  430. ssize_t pkt_len = datalen;
  431. uint32_t _U_ vlan_offset;
  432. uint32_t l2offset;
  433. uint16_t proto;
  434. uint32_t l2len;
  435. int res;
  436. assert(packet);
  437. assert(pkt_len);
  438. assert(*newbuff);
  439. res = get_l2len_protocol(packet,
  440. pkt_len,
  441. datalink,
  442. &proto,
  443. &l2len,
  444. &l2offset,
  445. &vlan_offset);
  446. /* sanity... pkt_len must be > l2len + IP header len*/
  447. if (res == -1 || l2len + TCPR_IPV6_H > pkt_len) {
  448. dbg(1, "get_ipv6(): Layer 2 len > total packet len, hence no IPv6 header");
  449. return NULL;
  450. }
  451. if (proto != ETHERTYPE_IP6)
  452. return NULL;
  453. packet += l2offset;
  454. l2len -= l2offset;
  455. pkt_len -= l2offset;
  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. return ip6_hdr;
  462. }
  463. /**
  464. * \brief returns a pointer to the layer 4 header which is just beyond the IPv4 header
  465. *
  466. * If the packet is to short, returns NULL
  467. */
  468. void *
  469. get_layer4_v4(const ipv4_hdr_t *ip_hdr, const int l3len)
  470. {
  471. void *ptr;
  472. assert(ip_hdr);
  473. ptr = (u_char *)ip_hdr + (ip_hdr->ip_hl << 2);
  474. /* make sure we don't jump over the end of the buffer */
  475. if ((u_char *)ptr > ((u_char *)ip_hdr + l3len))
  476. return NULL;
  477. return ((void *)ptr);
  478. }
  479. /**
  480. * returns a pointer to the layer 4 header which is just beyond the IPv6 header
  481. * and any extension headers or NULL when there is none as in the case of
  482. * v6 Frag or ESP header. Function is recursive.
  483. */
  484. void *
  485. get_layer4_v6(const ipv6_hdr_t *ip6_hdr, const int l3len)
  486. {
  487. struct tcpr_ipv6_ext_hdr_base *next, *exthdr;
  488. bool done = false;
  489. uint32_t maxlen;
  490. uint8_t proto;
  491. int min_len;
  492. assert(ip6_hdr);
  493. min_len = TCPR_IPV6_H + sizeof(struct tcpr_ipv6_ext_hdr_base);
  494. if (l3len < min_len)
  495. return NULL;
  496. /* jump to the end of the IPv6 header */
  497. next = (struct tcpr_ipv6_ext_hdr_base *)((u_char *)ip6_hdr + TCPR_IPV6_H);
  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, l3len - min_len);
  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. maxlen = l3len - (int)((u_char *)ip6_hdr - (u_char *)next);
  514. exthdr = get_ipv6_next(next, maxlen);
  515. if (exthdr == 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",
  536. IPV6_EXTLEN_TO_BYTES(next->ip_len));
  537. next = (void *)((u_char *)next + IPV6_EXTLEN_TO_BYTES(next->ip_len));
  538. } else {
  539. dbgx(3, "%s", "Returning end of IPv6 Header");
  540. }
  541. done = true;
  542. } /* switch */
  543. } /* while */
  544. if (!next || (u_char*)next > (u_char*)ip6_hdr + l3len)
  545. return NULL;
  546. return next;
  547. }
  548. /**
  549. * returns the next payload or header of the current extension header
  550. * returns NULL for none/ESP.
  551. */
  552. void *
  553. get_ipv6_next(struct tcpr_ipv6_ext_hdr_base *exthdr, const int len)
  554. {
  555. int extlen = 0;
  556. int maxlen;
  557. void *ptr;
  558. assert(exthdr);
  559. maxlen = *((int*)((u_char *)exthdr + len));
  560. dbgx(3, "Jumping to next IPv6 header. Processing 0x%02x", exthdr->ip_nh);
  561. switch (exthdr->ip_nh) {
  562. /* no further processing */
  563. case TCPR_IPV6_NH_NO_NEXT:
  564. case TCPR_IPV6_NH_ESP:
  565. dbg(3, "No-Next or ESP... can't go any further...");
  566. return NULL;
  567. break;
  568. /*
  569. * fragment header is fixed size
  570. * FIXME: Frag header has further ext headers (has a ip_nh field)
  571. * but I don't support it because there's never a full L4 + payload beyond.
  572. */
  573. case TCPR_IPV6_NH_FRAGMENT:
  574. dbg(3, "Looks like were a fragment header. Returning some frag'd data.");
  575. ptr = (void *)((u_char *)exthdr + sizeof(struct tcpr_ipv6_frag_hdr));
  576. if (*(int*)ptr > maxlen)
  577. return NULL;
  578. return ptr;
  579. break;
  580. /* all the rest require us to go deeper using the ip_len field */
  581. case TCPR_IPV6_NH_IPV6:
  582. case TCPR_IPV6_NH_ROUTING:
  583. case TCPR_IPV6_NH_DESTOPTS:
  584. case TCPR_IPV6_NH_HBH:
  585. case TCPR_IPV6_NH_AH:
  586. extlen = IPV6_EXTLEN_TO_BYTES(exthdr->ip_len);
  587. dbgx(3, "Looks like we're an ext header (0x%hhx). Jumping %u bytes"
  588. " to the next", exthdr->ip_nh, extlen);
  589. ptr = (void *)((u_char *)exthdr + extlen);
  590. if (*(int*)ptr > maxlen)
  591. return NULL;
  592. return ptr;
  593. break;
  594. default:
  595. dbg(3, "Must not be a v6 extension header... returning self");
  596. return (void *)exthdr;
  597. break;
  598. }
  599. }
  600. /**
  601. * returns the protocol of the actual layer4 header by processing through
  602. * the extension headers
  603. */
  604. uint8_t
  605. get_ipv6_l4proto(const ipv6_hdr_t *ip6_hdr, const int l3len)
  606. {
  607. u_char *ptr = (u_char *)ip6_hdr + TCPR_IPV6_H; /* jump to the end of the IPv6 header */
  608. uint8_t proto;
  609. struct tcpr_ipv6_ext_hdr_base *exthdr = NULL;
  610. assert(ip6_hdr);
  611. proto = ip6_hdr->ip_nh;
  612. int l4len = l3len - TCPR_IPV6_H;
  613. if (l4len < 0)
  614. return proto;
  615. while (TRUE) {
  616. dbgx(3, "Processing next proto 0x%02X", proto);
  617. switch (proto) {
  618. /* no further processing for IPV6 types with nothing beyond them */
  619. case TCPR_IPV6_NH_FRAGMENT:
  620. case TCPR_IPV6_NH_ESP:
  621. dbg(3, "No-Next or ESP... can't go any further...");
  622. return proto;
  623. break;
  624. /* recurse */
  625. case TCPR_IPV6_NH_IPV6:
  626. dbg(3, "Recursing due to v6 in v6");
  627. return get_ipv6_l4proto((ipv6_hdr_t *)ptr, l4len);
  628. break;
  629. /* loop again */
  630. case TCPR_IPV6_NH_AH:
  631. case TCPR_IPV6_NH_ROUTING:
  632. case TCPR_IPV6_NH_DESTOPTS:
  633. case TCPR_IPV6_NH_HBH:
  634. dbgx(3, "Jumping to next extension header (0x%hhx)", proto);
  635. exthdr = get_ipv6_next((struct tcpr_ipv6_ext_hdr_base *)ptr,
  636. l4len);
  637. if (exthdr == NULL)
  638. return proto;
  639. proto = exthdr->ip_nh;
  640. l4len -= (u_char *)exthdr - ptr;
  641. ptr = (u_char *)exthdr;
  642. break;
  643. /* should be TCP, UDP or the like */
  644. default:
  645. dbgx(3, "Selecting next L4 Proto as: 0x%02x", proto);
  646. return proto;
  647. } /* switch */
  648. } /* while */
  649. }
  650. /**
  651. * \brief Converts a human readable IPv4 address to a binary one
  652. *
  653. * stolen from LIBNET since I didn't want to have to deal with
  654. * passing a libnet_t around. Returns 0xFFFFFFFF (255.255.255.255)
  655. * on error
  656. */
  657. uint32_t
  658. get_name2addr4(const char *hostname, bool dnslookup)
  659. {
  660. struct in_addr addr;
  661. #if ! defined HAVE_INET_ATON && defined HAVE_INET_ADDR
  662. struct hostent *host_ent;
  663. #endif
  664. if (dnslookup) {
  665. #ifdef HAVE_INET_ATON
  666. if (inet_aton(hostname, &addr) != 1) {
  667. return(0xffffffff);
  668. }
  669. #elif defined HAVE_INET_ADDR
  670. if ((addr.s_addr = inet_addr(hostname)) == INADDR_NONE) {
  671. if (!(host_ent = gethostbyname(hostname))) {
  672. warnx("unable to resolve %s: %s", hostname, strerror(errno));
  673. /* this is actually 255.255.255.255 */
  674. return (0xffffffff);
  675. }
  676. /* was: host_ent->h_length); */
  677. memcpy(&addr.s_addr, host_ent->h_addr, sizeof(addr.s_addr));
  678. }
  679. #else
  680. warn("Unable to support get_name2addr4 w/ resolve");
  681. /* call ourselves recursively once w/o resolving the hostname */
  682. return get_name2addr4(hostname, DNS_DONT_RESOLVE);
  683. #endif
  684. /* return in network byte order */
  685. return (addr.s_addr);
  686. } else {
  687. /*
  688. * We only want dots 'n decimals.
  689. */
  690. int i;
  691. uint32_t m;
  692. if (!isdigit(hostname[0])) {
  693. warnx("Expected dotted-quad notation (%s) when DNS lookups are disabled",
  694. hostname);
  695. /* XXX - this is actually 255.255.255.255 */
  696. return (-1);
  697. }
  698. m = 0;
  699. for (i = 0; i < 4; i++) {
  700. u_int val;
  701. m <<= 8;
  702. if (*hostname) {
  703. val = 0;
  704. while (*hostname && *hostname != '.') {
  705. val *= 10;
  706. val += *hostname - '0';
  707. if (val > 255) {
  708. dbgx(4, "value %d > 255 for dotted quad", val);
  709. /* this is actually 255.255.255.255 */
  710. return (-1);
  711. }
  712. hostname++;
  713. }
  714. m |= val;
  715. if (*hostname) {
  716. hostname++;
  717. }
  718. }
  719. }
  720. /* host byte order */
  721. return (ntohl(m));
  722. }
  723. }
  724. /**
  725. * \brief Converts human readable IPv6 address to binary value
  726. *
  727. * Wrapper around inet_pton
  728. * Returns 1 for valid, 0 for not parsable and -1 for system error.
  729. * Does not support DNS.
  730. */
  731. int
  732. get_name2addr6(const char *hostname, bool dnslookup, struct tcpr_in6_addr *addr)
  733. {
  734. (void)dnslookup; /* prevent warning about unused arg */
  735. #ifdef HAVE_INET_PTON
  736. return inet_pton(AF_INET6, hostname, addr);
  737. #else
  738. #error "Unable to support get_name2addr6: Missing inet_pton() support."
  739. #endif
  740. return -1;
  741. }
  742. /**
  743. * \brief Converts binary IPv4 address to a string.
  744. *
  745. * Generic wrapper around inet_ntop() and inet_ntoa() depending on whichever
  746. * is available on your system. Does not support DNS.
  747. */
  748. const char *
  749. get_addr2name4(const uint32_t ip, bool _U_ dnslookup)
  750. {
  751. struct in_addr addr;
  752. static char *new_string = NULL;
  753. if (new_string == NULL)
  754. new_string = (char *)safe_malloc(255);
  755. new_string[0] = '\0';
  756. addr.s_addr = ip;
  757. #ifdef HAVE_INET_NTOP
  758. if (inet_ntop(AF_INET, &addr, new_string, 255) == NULL) {
  759. warnx("Unable to convert 0x%x to a string", ip);
  760. new_string[0] = 0;
  761. }
  762. return new_string;
  763. #elif defined HAVE_INET_NTOA
  764. return inet_ntoa(&addr);
  765. #else
  766. #error "Unable to support get_addr2name4."
  767. #endif
  768. }
  769. /**
  770. * \brief Converts a IPv6 binary address to a string.a
  771. *
  772. * Does not support DNS.
  773. */
  774. const char *
  775. get_addr2name6(const struct tcpr_in6_addr *addr, _U_ bool dnslookup)
  776. {
  777. static char *new_string = NULL;
  778. if (new_string == NULL)
  779. new_string = (char *)safe_malloc(255);
  780. new_string[0] = '\0';
  781. #ifdef HAVE_INET_NTOP
  782. if (inet_ntop(AF_INET6, addr, new_string, 255) == NULL) {
  783. warn("Unable to convert addr to a string");
  784. new_string[0] = 0;
  785. }
  786. return new_string;
  787. #else
  788. #error "Unable to support get_addr2name6."
  789. #endif
  790. }
  791. /**
  792. * \brief Converts the binary network address of a tcpr_cidr_t to a string
  793. */
  794. const char *
  795. get_cidr2name(const tcpr_cidr_t *cidr_ptr, bool dnslookup)
  796. {
  797. if (cidr_ptr->family == AF_INET) {
  798. return get_addr2name4(cidr_ptr->u.network, dnslookup);
  799. } else if (cidr_ptr->family == AF_INET6) {
  800. return get_addr2name6(&cidr_ptr->u.network6, dnslookup);
  801. } else {
  802. return NULL;
  803. }
  804. }