rewrite_l2.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. /* $Id:$ */
  2. /*
  3. * Copyright (c) 2005 Aaron Turner.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the names of the copyright owners nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  20. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  23. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  25. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  27. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  28. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  29. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #include "config.h"
  32. #include "defines.h"
  33. #include "common.h"
  34. /*
  35. * rewrite_l2.c can be compiled for tcprewrite or tcpbridge
  36. * in both cases, it requires a number of variables to be
  37. * available in the global options structure
  38. */
  39. #ifdef TCPREWRITE
  40. #include "tcprewrite.h"
  41. #include "tcprewrite_opts.h"
  42. extern tcprewrite_opt_t options;
  43. #elif defined TCPBRIDGE
  44. #include "tcpbridge.h"
  45. #include "tcpbridge_opts.h"
  46. extern tcpbridge_opt_t options;
  47. #endif
  48. #include "lib/sll.h"
  49. #include "dlt.h"
  50. #include "rewrite_l2.h"
  51. extern int maxpacket;
  52. static int check_pkt_len(struct pcap_pkthdr *pkthdr, int oldl2len, int newl2len);
  53. /*
  54. * Do all the layer 2 rewriting. Change ethernet header or even rewrite mac addresses
  55. * return layer 2 length on success or 0 on fail (don't send packet)
  56. */
  57. int
  58. rewrite_l2(pcap_t *pcap, struct pcap_pkthdr **pkthdr_ptr, u_char * pktdata, int cache_mode)
  59. {
  60. u_char *l2data = NULL; /* ptr to the user specified layer2 data if any */
  61. int newl2len = 0;
  62. struct pcap_pkthdr *pkthdr;
  63. pkthdr = *pkthdr_ptr;
  64. /* do we need a ptr for l2data ? */
  65. if (options.l2.linktype == LINKTYPE_USER) {
  66. if (cache_mode == CACHE_SECONDARY) {
  67. l2data = options.l2.data2;
  68. } else {
  69. l2data = options.l2.data1;
  70. }
  71. }
  72. /*
  73. * figure out what the CURRENT packet encapsulation is and we'll call
  74. * the appropriate function to:
  75. * 1) resize the L2 header
  76. * 2) copy over existing L2 header info (protocol, MAC's) to a new
  77. * standard 802.3 ethernet header where applicable
  78. * We do NOT apply any src/dst mac rewriting, as that is common
  79. * to all conversions, so that happens at the bottom of this function
  80. */
  81. switch (pcap_datalink(pcap)) {
  82. case DLT_EN10MB: /* Standard 802.3 Ethernet */
  83. newl2len = rewrite_en10mb(pktdata, pkthdr_ptr, l2data);
  84. break;
  85. case DLT_LINUX_SLL: /* Linux Cooked sockets */
  86. newl2len = rewrite_linux_sll(pktdata, pkthdr_ptr, l2data);
  87. break;
  88. case DLT_RAW: /* No ethernet header, raw IP */
  89. newl2len = rewrite_raw(pktdata, pkthdr_ptr, l2data);
  90. break;
  91. case DLT_C_HDLC: /* Cisco HDLC */
  92. newl2len = rewrite_c_hdlc(pktdata, pkthdr_ptr, l2data);
  93. break;
  94. } /* switch (linktype) */
  95. /* if newl2len == 0, then return zero so we don't send the packet */
  96. if (! newl2len)
  97. return 0;
  98. /*
  99. * Okay... we've got our new layer 2 header
  100. * if required. The next question, is do we have to
  101. * replace the src/dst MAC??
  102. */
  103. if (cache_mode == CACHE_SECONDARY) {
  104. if (options.mac_mask & SMAC2) {
  105. memcpy(&pktdata[ETHER_ADDR_LEN], options.intf2_smac, ETHER_ADDR_LEN);
  106. }
  107. if (options.mac_mask & DMAC2) {
  108. memcpy(pktdata, options.intf2_dmac, ETHER_ADDR_LEN);
  109. }
  110. } else {
  111. if (options.mac_mask & SMAC1) {
  112. memcpy(&pktdata[ETHER_ADDR_LEN], options.intf1_smac, ETHER_ADDR_LEN);
  113. }
  114. if (options.mac_mask & DMAC1) {
  115. memcpy(pktdata, options.intf1_dmac, ETHER_ADDR_LEN);
  116. }
  117. }
  118. /* return the updated layer 2 len */
  119. return (newl2len);
  120. }
  121. /*
  122. * All of these functions return the NEW layer two length and update the
  123. * total packet length in pkthdr->caplen
  124. */
  125. /*
  126. * logic to rewrite packets using DLT_EN10MB
  127. */
  128. int
  129. rewrite_en10mb(u_char *pktdata, struct pcap_pkthdr **pkthdr_ptr, u_char *l2data)
  130. {
  131. eth_hdr_t *eth_hdr = NULL;
  132. vlan_hdr_t *vlan_hdr = NULL;
  133. int oldl2len = 0, newl2len = 0, lendiff;
  134. u_char tmpbuff[MAXPACKET];
  135. struct pcap_pkthdr *pkthdr;
  136. pkthdr = *pkthdr_ptr;
  137. /*
  138. * is the header ethernet or 802.1q?
  139. */
  140. eth_hdr = (eth_hdr_t *)pktdata;
  141. if (eth_hdr->ether_type == ETHERTYPE_VLAN) {
  142. newl2len = oldl2len = LIBNET_802_1Q_H;
  143. } else {
  144. newl2len = oldl2len = LIBNET_ETH_H;
  145. }
  146. switch (options.l2.linktype) {
  147. case LINKTYPE_USER:
  148. /* track the new L2 len */
  149. newl2len = options.l2.len;
  150. if (! check_pkt_len(pkthdr, oldl2len, newl2len))
  151. return 0; /* unable to send packet */
  152. /*
  153. * remove the old header and copy our header back
  154. */
  155. dbg(3, "Rewriting packet via --dlink...");
  156. /* do we need a temp buff? */
  157. if (newl2len > oldl2len) {
  158. memcpy(tmpbuff, pktdata, pkthdr->caplen);
  159. memcpy(pktdata, l2data, options.l2.len);
  160. memcpy(&pktdata[newl2len], (tmpbuff + oldl2len),
  161. pkthdr->caplen);
  162. } else {
  163. memcpy(pktdata, l2data, newl2len);
  164. memmove(&pktdata[newl2len], (pktdata + oldl2len),
  165. pkthdr->caplen);
  166. }
  167. break;
  168. case LINKTYPE_VLAN:
  169. /* are we adding/modifying a VLAN header? */
  170. if (options.vlan == VLAN_ADD) {
  171. newl2len = LIBNET_802_1Q_H;
  172. if (! check_pkt_len(pkthdr, oldl2len, newl2len))
  173. return 0; /* unable to send packet */
  174. vlan_hdr = (vlan_hdr_t *)pktdata;
  175. /* do we modify the VLAN header? */
  176. if (oldl2len == newl2len) {
  177. /* user must always specify a tag */
  178. vlan_hdr->vlan_priority_c_vid |= htons((u_int16_t)OPT_VALUE_VLAN_TAG & LIBNET_802_1Q_VIDMASK);
  179. /* these are optional */
  180. if (HAVE_OPT(VLAN_PRI))
  181. vlan_hdr->vlan_priority_c_vid |= htons((u_int16_t)OPT_VALUE_VLAN_PRI) << 13;
  182. if (HAVE_OPT(VLAN_CFI))
  183. vlan_hdr->vlan_priority_c_vid |= htons((u_int16_t)OPT_VALUE_VLAN_CFI) << 12;
  184. }
  185. /* else we are adding a VLAN header */
  186. else if (oldl2len == LIBNET_ETH_H) {
  187. /* zero out our L2 header */
  188. memset(tmpbuff, 0, newl2len);
  189. /* copy the dst/src MAC's over to our temp buffer */
  190. memcpy(tmpbuff, pktdata, ETHER_ADDR_LEN * 2);
  191. vlan_hdr = (vlan_hdr_t *)tmpbuff;
  192. eth_hdr = (eth_hdr_t *)pktdata;
  193. /* these fields are always set this way */
  194. vlan_hdr->vlan_tpi = htons(ETHERTYPE_VLAN);
  195. vlan_hdr->vlan_len = eth_hdr->ether_type;
  196. /* user must always specify a tag */
  197. vlan_hdr->vlan_priority_c_vid |= htons((u_int16_t)OPT_VALUE_VLAN_TAG & LIBNET_802_1Q_VIDMASK);
  198. /* other things are optional */
  199. if (HAVE_OPT(VLAN_PRI))
  200. vlan_hdr->vlan_priority_c_vid |= htons((u_int16_t)OPT_VALUE_VLAN_PRI) << 13;
  201. if (HAVE_OPT(VLAN_CFI))
  202. vlan_hdr->vlan_priority_c_vid |= htons((u_int16_t)OPT_VALUE_VLAN_CFI) << 12;
  203. /* move around our buffers */
  204. memcpy(&tmpbuff[newl2len], (pktdata + oldl2len), (pkthdr->caplen - oldl2len));
  205. memcpy(pktdata, tmpbuff, (pkthdr->caplen + newl2len - oldl2len));
  206. } else {
  207. err(1, "Uh, how are we supposed to rewrite the header when the oldl2len != LIBNET_ETH_H?");
  208. }
  209. }
  210. else {
  211. /* remove VLAN header */
  212. newl2len = LIBNET_ETH_H;
  213. /* we still verify packet len incase MTU has shrunk */
  214. if (! check_pkt_len(pkthdr, oldl2len, newl2len))
  215. return 0; /* unable to send packet */
  216. memcpy(tmpbuff, pktdata, pkthdr->caplen);
  217. eth_hdr = (eth_hdr_t *)pktdata;
  218. vlan_hdr = (vlan_hdr_t *)tmpbuff;
  219. eth_hdr->ether_type = vlan_hdr->vlan_len;
  220. memcpy(&pktdata[LIBNET_ETH_H], (tmpbuff + LIBNET_802_1Q_H), pkthdr->caplen - oldl2len);
  221. }
  222. break;
  223. case LINKTYPE_ETHER:
  224. /* nothing to do here since we're already ethernet! */
  225. break;
  226. default:
  227. errx(1, "Invalid options.l2.linktype value: 0x%04x", options.l2.linktype);
  228. break;
  229. }
  230. /* new packet len */
  231. lendiff = newl2len - oldl2len;
  232. pkthdr->caplen += lendiff;
  233. pkthdr->len += lendiff;
  234. return newl2len;
  235. }
  236. /*
  237. * logic to rewrite packets using DLT_RAW
  238. */
  239. int
  240. rewrite_raw(u_char *pktdata, struct pcap_pkthdr **pkthdr_ptr, u_char *l2data)
  241. {
  242. int oldl2len = 0, newl2len = 0, lendiff;
  243. u_char tmpbuff[MAXPACKET];
  244. vlan_hdr_t *vlan_hdr = NULL;
  245. eth_hdr_t *eth_hdr = NULL;
  246. struct pcap_pkthdr *pkthdr;
  247. pkthdr = *pkthdr_ptr;
  248. /* we have no ethernet header, but we know we're IP */
  249. switch (options.l2.linktype) {
  250. case LINKTYPE_USER:
  251. newl2len = options.l2.len;
  252. if (! check_pkt_len(pkthdr, oldl2len, newl2len))
  253. return 0; /* unable to send packet */
  254. /*
  255. * add our user specified header
  256. */
  257. dbg(3, "Rewriting packet via --dlink...");
  258. /* backup the old packet */
  259. memcpy(tmpbuff, pktdata, pkthdr->caplen);
  260. memcpy(pktdata, l2data, newl2len);
  261. memcpy(&pktdata[newl2len], tmpbuff, pkthdr->caplen);
  262. break;
  263. case LINKTYPE_VLAN:
  264. newl2len = LIBNET_802_1Q_H;
  265. if (! check_pkt_len(pkthdr, oldl2len, newl2len))
  266. return 0; /* unable to send packet */
  267. /* prep a 802.1q tagged frame */
  268. /* make space for the header */
  269. memcpy(tmpbuff, pktdata, pkthdr->caplen);
  270. memcpy(&pktdata[LIBNET_802_1Q_H], tmpbuff, pkthdr->caplen);
  271. vlan_hdr = (vlan_hdr_t *)pktdata;
  272. /* these fields are always set this way */
  273. vlan_hdr->vlan_tpi = ETHERTYPE_VLAN;
  274. vlan_hdr->vlan_len = options.l2proto;
  275. /* user must always specify a tag */
  276. vlan_hdr->vlan_priority_c_vid |= htons((u_int16_t)OPT_VALUE_VLAN_TAG & LIBNET_802_1Q_VIDMASK);
  277. /* other things are optional */
  278. if (HAVE_OPT(VLAN_PRI))
  279. vlan_hdr->vlan_priority_c_vid |= htons((u_int16_t)OPT_VALUE_VLAN_PRI) << 13;
  280. if (HAVE_OPT(VLAN_CFI))
  281. vlan_hdr->vlan_priority_c_vid |= htons((u_int16_t)OPT_VALUE_VLAN_CFI) << 12;
  282. /* new packet len */
  283. newl2len = LIBNET_802_1Q_H;
  284. break;
  285. case LINKTYPE_ETHER:
  286. newl2len = LIBNET_ETH_H;
  287. if (! check_pkt_len(pkthdr, oldl2len, newl2len))
  288. return 0; /* unable to send packet */
  289. /* make room for L2 header */
  290. memmove(&pktdata[LIBNET_ETH_H], pktdata, pkthdr->caplen);
  291. /* these fields are always set this way */
  292. eth_hdr = (eth_hdr_t *)pktdata;
  293. eth_hdr->ether_type = options.l2proto;
  294. break;
  295. default:
  296. errx(1, "Invalid options.l2.linktype value: 0x%x", options.l2.linktype);
  297. break;
  298. }
  299. /* new packet len */
  300. lendiff = newl2len - oldl2len;
  301. pkthdr->caplen += lendiff;
  302. pkthdr->len += lendiff;
  303. return newl2len;
  304. }
  305. /*
  306. * logic to rewrite packets using DLT_LINUX_SLL
  307. */
  308. int
  309. rewrite_linux_sll(u_char *pktdata, struct pcap_pkthdr **pkthdr_ptr, u_char *l2data)
  310. {
  311. int oldl2len = 0, newl2len = 0, lendiff;
  312. u_char tmpbuff[MAXPACKET];
  313. vlan_hdr_t *vlan_hdr = NULL;
  314. eth_hdr_t *eth_hdr = NULL;
  315. sll_hdr_t *sll_hdr = NULL;
  316. struct pcap_pkthdr *pkthdr;
  317. pkthdr = *pkthdr_ptr;
  318. newl2len = oldl2len = SLL_HDR_LEN;
  319. switch (options.l2.linktype) {
  320. case LINKTYPE_USER:
  321. newl2len = options.l2.len;
  322. if (! check_pkt_len(pkthdr, oldl2len, newl2len))
  323. return 0; /* unable to send packet */
  324. /*
  325. * add our user specified header
  326. */
  327. dbg(3, "Rewriting packet via --dlink...");
  328. /* backup the old packet */
  329. memcpy(tmpbuff, pktdata, pkthdr->caplen);
  330. memcpy(pktdata, l2data, newl2len);
  331. memcpy(&pktdata[newl2len], tmpbuff, pkthdr->caplen);
  332. break;
  333. case LINKTYPE_VLAN:
  334. /* prep a 802.1q tagged frame */
  335. newl2len = LIBNET_802_1Q_H;
  336. if (! check_pkt_len(pkthdr, oldl2len, newl2len))
  337. return 0; /* unable to send packet */
  338. /* make space for the header */
  339. memcpy(tmpbuff, pktdata, pkthdr->caplen);
  340. memcpy(&pktdata[LIBNET_802_1Q_H], tmpbuff, pkthdr->caplen - oldl2len);
  341. vlan_hdr = (vlan_hdr_t *)pktdata;
  342. sll_hdr = (sll_hdr_t *)tmpbuff;
  343. /* these fields are always set this way */
  344. vlan_hdr->vlan_tpi = ETHERTYPE_VLAN;
  345. vlan_hdr->vlan_len = sll_hdr->sll_protocol;
  346. /* the sll header might have a src mac */
  347. if (sll_hdr->sll_halen == ETHER_ADDR_LEN)
  348. memcpy(vlan_hdr->vlan_shost, sll_hdr->sll_addr, ETHER_ADDR_LEN);
  349. /* user must always specify a tag */
  350. vlan_hdr->vlan_priority_c_vid |= htons((u_int16_t)(OPT_VALUE_VLAN_TAG & LIBNET_802_1Q_VIDMASK));
  351. /* other things are optional */
  352. if (HAVE_OPT(VLAN_PRI))
  353. vlan_hdr->vlan_priority_c_vid |= htons((u_int16_t)OPT_VALUE_VLAN_PRI) << 13;
  354. if (HAVE_OPT(VLAN_CFI))
  355. vlan_hdr->vlan_priority_c_vid |= htons((u_int16_t)OPT_VALUE_VLAN_CFI) << 12;
  356. break;
  357. case LINKTYPE_ETHER:
  358. newl2len = LIBNET_ETH_H;
  359. if (! check_pkt_len(pkthdr, oldl2len, newl2len))
  360. return 0; /* unable to send packet */
  361. /* make room for L2 header */
  362. memcpy(tmpbuff, pktdata, pkthdr->caplen);
  363. memcpy(&pktdata[LIBNET_ETH_H], (tmpbuff + oldl2len), pkthdr->caplen - oldl2len);
  364. /* these fields are always set this way */
  365. sll_hdr = (sll_hdr_t *)tmpbuff;
  366. eth_hdr = (eth_hdr_t *)pktdata;
  367. eth_hdr->ether_type = sll_hdr->sll_protocol;
  368. /* the sll header might have a src mac */
  369. if (sll_hdr->sll_halen == ETHER_ADDR_LEN)
  370. memcpy(eth_hdr->ether_shost, sll_hdr->sll_addr, ETHER_ADDR_LEN);
  371. break;
  372. default:
  373. errx(1, "Invalid options.l2.linktype value: 0x%x", options.l2.linktype);
  374. break;
  375. }
  376. /* new packet len */
  377. lendiff = newl2len - oldl2len;
  378. pkthdr->caplen += lendiff;
  379. pkthdr->len += lendiff;
  380. return newl2len;
  381. }
  382. /*
  383. * logic to rewrite packets using DLT_C_HDLC
  384. */
  385. int
  386. rewrite_c_hdlc(u_char *pktdata, struct pcap_pkthdr **pkthdr_ptr, u_char *l2data)
  387. {
  388. int oldl2len = 0, newl2len = 0, lendiff;
  389. u_char tmpbuff[MAXPACKET];
  390. hdlc_hdr_t *hdlc_hdr = NULL;
  391. eth_hdr_t *eth_hdr = NULL;
  392. vlan_hdr_t *vlan_hdr = NULL;
  393. struct pcap_pkthdr *pkthdr;
  394. pkthdr = *pkthdr_ptr;
  395. newl2len = oldl2len = CISCO_HDLC_LEN;
  396. switch (options.l2.linktype) {
  397. case LINKTYPE_USER:
  398. /* track the new L2 len */
  399. newl2len = options.l2.len;
  400. if (! check_pkt_len(pkthdr, oldl2len, newl2len))
  401. return 0; /* unable to send packet */
  402. /*
  403. * add our user specified header
  404. */
  405. dbg(3, "Rewriting packet via --dlink...");
  406. /* backup the old packet */
  407. memcpy(tmpbuff, pktdata, pkthdr->caplen);
  408. memcpy(pktdata, l2data, options.l2.len);
  409. memcpy(&pktdata[options.l2.len], (tmpbuff + oldl2len), pkthdr->caplen - oldl2len);
  410. break;
  411. case LINKTYPE_VLAN:
  412. /* new l2 len */
  413. newl2len = LIBNET_802_1Q_H;
  414. if (! check_pkt_len(pkthdr, oldl2len, newl2len))
  415. return 0; /* unable to send packet */
  416. memcpy(tmpbuff, pktdata, pkthdr->caplen);
  417. hdlc_hdr = (hdlc_hdr_t *)tmpbuff;
  418. vlan_hdr = (vlan_hdr_t *)pktdata;
  419. memcpy(&pktdata[LIBNET_802_1Q_H], tmpbuff + oldl2len, pkthdr->caplen - oldl2len);
  420. vlan_hdr->vlan_tpi = ETHERTYPE_VLAN;
  421. vlan_hdr->vlan_len = hdlc_hdr->protocol;
  422. /* user must always specify a tag */
  423. vlan_hdr->vlan_priority_c_vid |= htons((u_int16_t)OPT_VALUE_VLAN_TAG & LIBNET_802_1Q_VIDMASK);
  424. /* other things are optional */
  425. if (HAVE_OPT(VLAN_PRI))
  426. vlan_hdr->vlan_priority_c_vid |= htons((u_int16_t)OPT_VALUE_VLAN_PRI) << 13;
  427. if (HAVE_OPT(VLAN_CFI))
  428. vlan_hdr->vlan_priority_c_vid |= htons((u_int16_t)OPT_VALUE_VLAN_CFI) << 12;
  429. break;
  430. case LINKTYPE_ETHER:
  431. newl2len = LIBNET_ETH_H;
  432. if (! check_pkt_len(pkthdr, oldl2len, newl2len))
  433. return 0; /* unable to send packet */
  434. memcpy(tmpbuff, pktdata, pkthdr->caplen);
  435. hdlc_hdr = (hdlc_hdr_t *)tmpbuff;
  436. eth_hdr = (eth_hdr_t *)pktdata;
  437. memcpy(&pktdata[LIBNET_ETH_H], tmpbuff + oldl2len, pkthdr->caplen - oldl2len);
  438. eth_hdr->ether_type = hdlc_hdr->protocol;
  439. break;
  440. default:
  441. errx(1, "Invalid options.l2.linktype value: 0x%x", options.l2.linktype);
  442. break;
  443. }
  444. /* new packet len */
  445. lendiff = newl2len - oldl2len;
  446. pkthdr->caplen += lendiff;
  447. pkthdr->len += lendiff;
  448. return newl2len;
  449. }
  450. /*
  451. * will the new packet be too big?
  452. * If so, we have to change the pkthdr->caplen to be artifically lower
  453. * so we don't go beyond options.maxpacket
  454. */
  455. static int
  456. check_pkt_len(struct pcap_pkthdr *pkthdr, int oldl2len, int newl2len)
  457. {
  458. /*
  459. * is new packet too big?
  460. */
  461. if ((pkthdr->caplen - oldl2len + newl2len) > options.maxpacket) {
  462. if (options.fixlen) {
  463. warnx("Packet length (%u) is greater then MTU (%u); "
  464. "truncating packet.",
  465. (pkthdr->caplen - oldl2len + newl2len), options.maxpacket);
  466. pkthdr->caplen = options.maxpacket - (newl2len - oldl2len);
  467. }
  468. else {
  469. warnx("Packet length (%u) is greater then MTU (%u); "
  470. "skipping packet.",
  471. (pkthdr->caplen - oldl2len + newl2len), options.maxpacket);
  472. return (0);
  473. }
  474. }
  475. /* all is fine */
  476. return 1;
  477. }
  478. /*
  479. Local Variables:
  480. mode:c
  481. indent-tabs-mode:nil
  482. c-basic-offset:4
  483. End:
  484. */