pcap-bpf.c 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292
  1. /*
  2. * Copyright (c) 1993, 1994, 1995, 1996, 1998
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that: (1) source code distributions
  7. * retain the above copyright notice and this paragraph in its entirety, (2)
  8. * distributions including binary code include the above copyright notice and
  9. * this paragraph in its entirety in the documentation or other materials
  10. * provided with the distribution, and (3) all advertising materials mentioning
  11. * features or use of this software display the following acknowledgement:
  12. * ``This product includes software developed by the University of California,
  13. * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  14. * the University nor the names of its contributors may be used to endorse
  15. * or promote products derived from this software without specific prior
  16. * written permission.
  17. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  18. * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  19. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #include <sys/param.h> /* optionally get BSD define */
  25. #include <sys/socket.h>
  26. #include <time.h>
  27. /*
  28. * <net/bpf.h> defines ioctls, but doesn't include <sys/ioccom.h>.
  29. *
  30. * We include <sys/ioctl.h> as it might be necessary to declare ioctl();
  31. * at least on *BSD and macOS, it also defines various SIOC ioctls -
  32. * we could include <sys/sockio.h>, but if we're already including
  33. * <sys/ioctl.h>, which includes <sys/sockio.h> on those platforms,
  34. * there's not much point in doing so.
  35. *
  36. * If we have <sys/ioccom.h>, we include it as well, to handle systems
  37. * such as Solaris which don't arrange to include <sys/ioccom.h> if you
  38. * include <sys/ioctl.h>
  39. */
  40. #include <sys/ioctl.h>
  41. #ifdef HAVE_SYS_IOCCOM_H
  42. #include <sys/ioccom.h>
  43. #endif
  44. #include <sys/utsname.h>
  45. #if defined(__FreeBSD__) && defined(SIOCIFCREATE2)
  46. /*
  47. * Add support for capturing on FreeBSD usbusN interfaces.
  48. */
  49. static const char usbus_prefix[] = "usbus";
  50. #define USBUS_PREFIX_LEN (sizeof(usbus_prefix) - 1)
  51. #include <dirent.h>
  52. #endif
  53. #include <net/if.h>
  54. #ifdef _AIX
  55. /*
  56. * Make "pcap.h" not include "pcap/bpf.h"; we are going to include the
  57. * native OS version, as we need "struct bpf_config" from it.
  58. */
  59. #define PCAP_DONT_INCLUDE_PCAP_BPF_H
  60. #include <sys/types.h>
  61. /*
  62. * Prevent bpf.h from redefining the DLT_ values to their
  63. * IFT_ values, as we're going to return the standard libpcap
  64. * values, not IBM's non-standard IFT_ values.
  65. */
  66. #undef _AIX
  67. #include <net/bpf.h>
  68. #define _AIX
  69. /*
  70. * If both BIOCROTZBUF and BPF_BUFMODE_ZBUF are defined, we have
  71. * zero-copy BPF.
  72. */
  73. #if defined(BIOCROTZBUF) && defined(BPF_BUFMODE_ZBUF)
  74. #define HAVE_ZEROCOPY_BPF
  75. #include <sys/mman.h>
  76. #include <machine/atomic.h>
  77. #endif
  78. #include <net/if_types.h> /* for IFT_ values */
  79. #include <sys/sysconfig.h>
  80. #include <sys/device.h>
  81. #include <sys/cfgodm.h>
  82. #include <cf.h>
  83. #ifdef __64BIT__
  84. #define domakedev makedev64
  85. #define getmajor major64
  86. #define bpf_hdr bpf_hdr32
  87. #else /* __64BIT__ */
  88. #define domakedev makedev
  89. #define getmajor major
  90. #endif /* __64BIT__ */
  91. #define BPF_NAME "bpf"
  92. #define BPF_MINORS 4
  93. #define DRIVER_PATH "/usr/lib/drivers"
  94. #define BPF_NODE "/dev/bpf"
  95. static int bpfloadedflag = 0;
  96. static int odmlockid = 0;
  97. static int bpf_load(char *errbuf);
  98. #else /* _AIX */
  99. #include <net/bpf.h>
  100. #endif /* _AIX */
  101. #include <ctype.h>
  102. #include <fcntl.h>
  103. #include <errno.h>
  104. #include <netdb.h>
  105. #include <stdio.h>
  106. #include <stdlib.h>
  107. #include <string.h>
  108. #include <unistd.h>
  109. #ifdef SIOCGIFMEDIA
  110. # include <net/if_media.h>
  111. #endif
  112. #include "pcap-int.h"
  113. #ifdef HAVE_OS_PROTO_H
  114. #include "os-proto.h"
  115. #endif
  116. /*
  117. * Later versions of NetBSD stick padding in front of FDDI frames
  118. * to align the IP header on a 4-byte boundary.
  119. */
  120. #if defined(__NetBSD__) && __NetBSD_Version__ > 106000000
  121. #define PCAP_FDDIPAD 3
  122. #endif
  123. /*
  124. * Private data for capturing on BPF devices.
  125. */
  126. struct pcap_bpf {
  127. #ifdef HAVE_ZEROCOPY_BPF
  128. /*
  129. * Zero-copy read buffer -- for zero-copy BPF. 'buffer' above will
  130. * alternative between these two actual mmap'd buffers as required.
  131. * As there is a header on the front size of the mmap'd buffer, only
  132. * some of the buffer is exposed to libpcap as a whole via bufsize;
  133. * zbufsize is the true size. zbuffer tracks the current zbuf
  134. * assocated with buffer so that it can be used to decide which the
  135. * next buffer to read will be.
  136. */
  137. u_char *zbuf1, *zbuf2, *zbuffer;
  138. u_int zbufsize;
  139. u_int zerocopy;
  140. u_int interrupted;
  141. struct timespec firstsel;
  142. /*
  143. * If there's currently a buffer being actively processed, then it is
  144. * referenced here; 'buffer' is also pointed at it, but offset by the
  145. * size of the header.
  146. */
  147. struct bpf_zbuf_header *bzh;
  148. int nonblock; /* true if in nonblocking mode */
  149. #endif /* HAVE_ZEROCOPY_BPF */
  150. char *device; /* device name */
  151. int filtering_in_kernel; /* using kernel filter */
  152. int must_do_on_close; /* stuff we must do when we close */
  153. };
  154. /*
  155. * Stuff to do when we close.
  156. */
  157. #define MUST_CLEAR_RFMON 0x00000001 /* clear rfmon (monitor) mode */
  158. #define MUST_DESTROY_USBUS 0x00000002 /* destroy usbusN interface */
  159. #ifdef BIOCGDLTLIST
  160. # if (defined(HAVE_NET_IF_MEDIA_H) && defined(IFM_IEEE80211)) && !defined(__APPLE__)
  161. #define HAVE_BSD_IEEE80211
  162. /*
  163. * The ifm_ulist member of a struct ifmediareq is an int * on most systems,
  164. * but it's a uint64_t on newer versions of OpenBSD.
  165. *
  166. * We check this by checking whether IFM_GMASK is defined and > 2^32-1.
  167. */
  168. # if defined(IFM_GMASK) && IFM_GMASK > 0xFFFFFFFF
  169. # define IFM_ULIST_TYPE uint64_t
  170. # else
  171. # define IFM_ULIST_TYPE int
  172. # endif
  173. # endif
  174. # if defined(__APPLE__) || defined(HAVE_BSD_IEEE80211)
  175. static int find_802_11(struct bpf_dltlist *);
  176. # ifdef HAVE_BSD_IEEE80211
  177. static int monitor_mode(pcap_t *, int);
  178. # endif
  179. # if defined(__APPLE__)
  180. static void remove_en(pcap_t *);
  181. static void remove_802_11(pcap_t *);
  182. # endif
  183. # endif /* defined(__APPLE__) || defined(HAVE_BSD_IEEE80211) */
  184. #endif /* BIOCGDLTLIST */
  185. #if defined(sun) && defined(LIFNAMSIZ) && defined(lifr_zoneid)
  186. #include <zone.h>
  187. #endif
  188. /*
  189. * We include the OS's <net/bpf.h>, not our "pcap/bpf.h", so we probably
  190. * don't get DLT_DOCSIS defined.
  191. */
  192. #ifndef DLT_DOCSIS
  193. #define DLT_DOCSIS 143
  194. #endif
  195. /*
  196. * In some versions of macOS, we might not even get any of the
  197. * 802.11-plus-radio-header DLT_'s defined, even though some
  198. * of them are used by various Airport drivers in those versions.
  199. */
  200. #ifndef DLT_PRISM_HEADER
  201. #define DLT_PRISM_HEADER 119
  202. #endif
  203. #ifndef DLT_AIRONET_HEADER
  204. #define DLT_AIRONET_HEADER 120
  205. #endif
  206. #ifndef DLT_IEEE802_11_RADIO
  207. #define DLT_IEEE802_11_RADIO 127
  208. #endif
  209. #ifndef DLT_IEEE802_11_RADIO_AVS
  210. #define DLT_IEEE802_11_RADIO_AVS 163
  211. #endif
  212. static int pcap_can_set_rfmon_bpf(pcap_t *p);
  213. static int pcap_activate_bpf(pcap_t *p);
  214. static int pcap_setfilter_bpf(pcap_t *p, struct bpf_program *fp);
  215. static int pcap_setdirection_bpf(pcap_t *, pcap_direction_t);
  216. static int pcap_set_datalink_bpf(pcap_t *p, int dlt);
  217. /*
  218. * For zerocopy bpf, the setnonblock/getnonblock routines need to modify
  219. * pb->nonblock so we don't call select(2) if the pcap handle is in non-
  220. * blocking mode.
  221. */
  222. static int
  223. pcap_getnonblock_bpf(pcap_t *p)
  224. {
  225. #ifdef HAVE_ZEROCOPY_BPF
  226. struct pcap_bpf *pb = p->priv;
  227. if (pb->zerocopy)
  228. return (pb->nonblock);
  229. #endif
  230. return (pcap_getnonblock_fd(p));
  231. }
  232. static int
  233. pcap_setnonblock_bpf(pcap_t *p, int nonblock)
  234. {
  235. #ifdef HAVE_ZEROCOPY_BPF
  236. struct pcap_bpf *pb = p->priv;
  237. if (pb->zerocopy) {
  238. pb->nonblock = nonblock;
  239. return (0);
  240. }
  241. #endif
  242. return (pcap_setnonblock_fd(p, nonblock));
  243. }
  244. #ifdef HAVE_ZEROCOPY_BPF
  245. /*
  246. * Zero-copy BPF buffer routines to check for and acknowledge BPF data in
  247. * shared memory buffers.
  248. *
  249. * pcap_next_zbuf_shm(): Check for a newly available shared memory buffer,
  250. * and set up p->buffer and cc to reflect one if available. Notice that if
  251. * there was no prior buffer, we select zbuf1 as this will be the first
  252. * buffer filled for a fresh BPF session.
  253. */
  254. static int
  255. pcap_next_zbuf_shm(pcap_t *p, int *cc)
  256. {
  257. struct pcap_bpf *pb = p->priv;
  258. struct bpf_zbuf_header *bzh;
  259. if (pb->zbuffer == pb->zbuf2 || pb->zbuffer == NULL) {
  260. bzh = (struct bpf_zbuf_header *)pb->zbuf1;
  261. if (bzh->bzh_user_gen !=
  262. atomic_load_acq_int(&bzh->bzh_kernel_gen)) {
  263. pb->bzh = bzh;
  264. pb->zbuffer = (u_char *)pb->zbuf1;
  265. p->buffer = pb->zbuffer + sizeof(*bzh);
  266. *cc = bzh->bzh_kernel_len;
  267. return (1);
  268. }
  269. } else if (pb->zbuffer == pb->zbuf1) {
  270. bzh = (struct bpf_zbuf_header *)pb->zbuf2;
  271. if (bzh->bzh_user_gen !=
  272. atomic_load_acq_int(&bzh->bzh_kernel_gen)) {
  273. pb->bzh = bzh;
  274. pb->zbuffer = (u_char *)pb->zbuf2;
  275. p->buffer = pb->zbuffer + sizeof(*bzh);
  276. *cc = bzh->bzh_kernel_len;
  277. return (1);
  278. }
  279. }
  280. *cc = 0;
  281. return (0);
  282. }
  283. /*
  284. * pcap_next_zbuf() -- Similar to pcap_next_zbuf_shm(), except wait using
  285. * select() for data or a timeout, and possibly force rotation of the buffer
  286. * in the event we time out or are in immediate mode. Invoke the shared
  287. * memory check before doing system calls in order to avoid doing avoidable
  288. * work.
  289. */
  290. static int
  291. pcap_next_zbuf(pcap_t *p, int *cc)
  292. {
  293. struct pcap_bpf *pb = p->priv;
  294. struct bpf_zbuf bz;
  295. struct timeval tv;
  296. struct timespec cur;
  297. fd_set r_set;
  298. int data, r;
  299. int expire, tmout;
  300. #define TSTOMILLI(ts) (((ts)->tv_sec * 1000) + ((ts)->tv_nsec / 1000000))
  301. /*
  302. * Start out by seeing whether anything is waiting by checking the
  303. * next shared memory buffer for data.
  304. */
  305. data = pcap_next_zbuf_shm(p, cc);
  306. if (data)
  307. return (data);
  308. /*
  309. * If a previous sleep was interrupted due to signal delivery, make
  310. * sure that the timeout gets adjusted accordingly. This requires
  311. * that we analyze when the timeout should be been expired, and
  312. * subtract the current time from that. If after this operation,
  313. * our timeout is less then or equal to zero, handle it like a
  314. * regular timeout.
  315. */
  316. tmout = p->opt.timeout;
  317. if (tmout)
  318. (void) clock_gettime(CLOCK_MONOTONIC, &cur);
  319. if (pb->interrupted && p->opt.timeout) {
  320. expire = TSTOMILLI(&pb->firstsel) + p->opt.timeout;
  321. tmout = expire - TSTOMILLI(&cur);
  322. #undef TSTOMILLI
  323. if (tmout <= 0) {
  324. pb->interrupted = 0;
  325. data = pcap_next_zbuf_shm(p, cc);
  326. if (data)
  327. return (data);
  328. if (ioctl(p->fd, BIOCROTZBUF, &bz) < 0) {
  329. pcap_fmt_errmsg_for_errno(p->errbuf,
  330. PCAP_ERRBUF_SIZE, errno, "BIOCROTZBUF");
  331. return (PCAP_ERROR);
  332. }
  333. return (pcap_next_zbuf_shm(p, cc));
  334. }
  335. }
  336. /*
  337. * No data in the buffer, so must use select() to wait for data or
  338. * the next timeout. Note that we only call select if the handle
  339. * is in blocking mode.
  340. */
  341. if (!pb->nonblock) {
  342. FD_ZERO(&r_set);
  343. FD_SET(p->fd, &r_set);
  344. if (tmout != 0) {
  345. tv.tv_sec = tmout / 1000;
  346. tv.tv_usec = (tmout * 1000) % 1000000;
  347. }
  348. r = select(p->fd + 1, &r_set, NULL, NULL,
  349. p->opt.timeout != 0 ? &tv : NULL);
  350. if (r < 0 && errno == EINTR) {
  351. if (!pb->interrupted && p->opt.timeout) {
  352. pb->interrupted = 1;
  353. pb->firstsel = cur;
  354. }
  355. return (0);
  356. } else if (r < 0) {
  357. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  358. errno, "select");
  359. return (PCAP_ERROR);
  360. }
  361. }
  362. pb->interrupted = 0;
  363. /*
  364. * Check again for data, which may exist now that we've either been
  365. * woken up as a result of data or timed out. Try the "there's data"
  366. * case first since it doesn't require a system call.
  367. */
  368. data = pcap_next_zbuf_shm(p, cc);
  369. if (data)
  370. return (data);
  371. /*
  372. * Try forcing a buffer rotation to dislodge timed out or immediate
  373. * data.
  374. */
  375. if (ioctl(p->fd, BIOCROTZBUF, &bz) < 0) {
  376. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  377. errno, "BIOCROTZBUF");
  378. return (PCAP_ERROR);
  379. }
  380. return (pcap_next_zbuf_shm(p, cc));
  381. }
  382. /*
  383. * Notify kernel that we are done with the buffer. We don't reset zbuffer so
  384. * that we know which buffer to use next time around.
  385. */
  386. static int
  387. pcap_ack_zbuf(pcap_t *p)
  388. {
  389. struct pcap_bpf *pb = p->priv;
  390. atomic_store_rel_int(&pb->bzh->bzh_user_gen,
  391. pb->bzh->bzh_kernel_gen);
  392. pb->bzh = NULL;
  393. p->buffer = NULL;
  394. return (0);
  395. }
  396. #endif /* HAVE_ZEROCOPY_BPF */
  397. pcap_t *
  398. pcap_create_interface(const char *device _U_, char *ebuf)
  399. {
  400. pcap_t *p;
  401. p = pcap_create_common(ebuf, sizeof (struct pcap_bpf));
  402. if (p == NULL)
  403. return (NULL);
  404. p->activate_op = pcap_activate_bpf;
  405. p->can_set_rfmon_op = pcap_can_set_rfmon_bpf;
  406. #ifdef BIOCSTSTAMP
  407. /*
  408. * We claim that we support microsecond and nanosecond time
  409. * stamps.
  410. */
  411. p->tstamp_precision_count = 2;
  412. p->tstamp_precision_list = malloc(2 * sizeof(u_int));
  413. if (p->tstamp_precision_list == NULL) {
  414. pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, errno,
  415. "malloc");
  416. free(p);
  417. return (NULL);
  418. }
  419. p->tstamp_precision_list[0] = PCAP_TSTAMP_PRECISION_MICRO;
  420. p->tstamp_precision_list[1] = PCAP_TSTAMP_PRECISION_NANO;
  421. #endif /* BIOCSTSTAMP */
  422. return (p);
  423. }
  424. /*
  425. * On success, returns a file descriptor for a BPF device.
  426. * On failure, returns a PCAP_ERROR_ value, and sets p->errbuf.
  427. */
  428. static int
  429. bpf_open(char *errbuf)
  430. {
  431. int fd = -1;
  432. static const char cloning_device[] = "/dev/bpf";
  433. int n = 0;
  434. char device[sizeof "/dev/bpf0000000000"];
  435. static int no_cloning_bpf = 0;
  436. #ifdef _AIX
  437. /*
  438. * Load the bpf driver, if it isn't already loaded,
  439. * and create the BPF device entries, if they don't
  440. * already exist.
  441. */
  442. if (bpf_load(errbuf) == PCAP_ERROR)
  443. return (PCAP_ERROR);
  444. #endif
  445. /*
  446. * First, unless we've already tried opening /dev/bpf and
  447. * gotten ENOENT, try opening /dev/bpf.
  448. * If it fails with ENOENT, remember that, so we don't try
  449. * again, and try /dev/bpfN.
  450. */
  451. if (!no_cloning_bpf &&
  452. (fd = open(cloning_device, O_RDWR)) == -1 &&
  453. ((errno != EACCES && errno != ENOENT) ||
  454. (fd = open(cloning_device, O_RDONLY)) == -1)) {
  455. if (errno != ENOENT) {
  456. if (errno == EACCES)
  457. fd = PCAP_ERROR_PERM_DENIED;
  458. else
  459. fd = PCAP_ERROR;
  460. pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
  461. errno, "(cannot open device) %s", cloning_device);
  462. return (fd);
  463. }
  464. no_cloning_bpf = 1;
  465. }
  466. if (no_cloning_bpf) {
  467. /*
  468. * We don't have /dev/bpf.
  469. * Go through all the /dev/bpfN minors and find one
  470. * that isn't in use.
  471. */
  472. do {
  473. (void)pcap_snprintf(device, sizeof(device), "/dev/bpf%d", n++);
  474. /*
  475. * Initially try a read/write open (to allow the inject
  476. * method to work). If that fails due to permission
  477. * issues, fall back to read-only. This allows a
  478. * non-root user to be granted specific access to pcap
  479. * capabilities via file permissions.
  480. *
  481. * XXX - we should have an API that has a flag that
  482. * controls whether to open read-only or read-write,
  483. * so that denial of permission to send (or inability
  484. * to send, if sending packets isn't supported on
  485. * the device in question) can be indicated at open
  486. * time.
  487. */
  488. fd = open(device, O_RDWR);
  489. if (fd == -1 && errno == EACCES)
  490. fd = open(device, O_RDONLY);
  491. } while (fd < 0 && errno == EBUSY);
  492. }
  493. /*
  494. * XXX better message for all minors used
  495. */
  496. if (fd < 0) {
  497. switch (errno) {
  498. case ENOENT:
  499. fd = PCAP_ERROR;
  500. if (n == 1) {
  501. /*
  502. * /dev/bpf0 doesn't exist, which
  503. * means we probably have no BPF
  504. * devices.
  505. */
  506. pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
  507. "(there are no BPF devices)");
  508. } else {
  509. /*
  510. * We got EBUSY on at least one
  511. * BPF device, so we have BPF
  512. * devices, but all the ones
  513. * that exist are busy.
  514. */
  515. pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
  516. "(all BPF devices are busy)");
  517. }
  518. break;
  519. case EACCES:
  520. /*
  521. * Got EACCES on the last device we tried,
  522. * and EBUSY on all devices before that,
  523. * if any.
  524. */
  525. fd = PCAP_ERROR_PERM_DENIED;
  526. pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
  527. errno, "(cannot open BPF device) %s", device);
  528. break;
  529. default:
  530. /*
  531. * Some other problem.
  532. */
  533. fd = PCAP_ERROR;
  534. pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
  535. errno, "(cannot open BPF device) %s", device);
  536. break;
  537. }
  538. }
  539. return (fd);
  540. }
  541. /*
  542. * Open and bind to a device; used if we're not actually going to use
  543. * the device, but are just testing whether it can be opened, or opening
  544. * it to get information about it.
  545. *
  546. * Returns an error code on failure (always negative), and an FD for
  547. * the now-bound BPF device on success (always non-negative).
  548. */
  549. static int
  550. bpf_open_and_bind(const char *name, char *errbuf)
  551. {
  552. int fd;
  553. struct ifreq ifr;
  554. /*
  555. * First, open a BPF device.
  556. */
  557. fd = bpf_open(errbuf);
  558. if (fd < 0)
  559. return (fd); /* fd is the appropriate error code */
  560. /*
  561. * Now bind to the device.
  562. */
  563. (void)strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
  564. if (ioctl(fd, BIOCSETIF, (caddr_t)&ifr) < 0) {
  565. switch (errno) {
  566. case ENXIO:
  567. /*
  568. * There's no such device.
  569. */
  570. close(fd);
  571. return (PCAP_ERROR_NO_SUCH_DEVICE);
  572. case ENETDOWN:
  573. /*
  574. * Return a "network down" indication, so that
  575. * the application can report that rather than
  576. * saying we had a mysterious failure and
  577. * suggest that they report a problem to the
  578. * libpcap developers.
  579. */
  580. close(fd);
  581. return (PCAP_ERROR_IFACE_NOT_UP);
  582. default:
  583. pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
  584. errno, "BIOCSETIF: %s", name);
  585. close(fd);
  586. return (PCAP_ERROR);
  587. }
  588. }
  589. /*
  590. * Success.
  591. */
  592. return (fd);
  593. }
  594. #ifdef BIOCGDLTLIST
  595. static int
  596. get_dlt_list(int fd, int v, struct bpf_dltlist *bdlp, char *ebuf)
  597. {
  598. memset(bdlp, 0, sizeof(*bdlp));
  599. if (ioctl(fd, BIOCGDLTLIST, (caddr_t)bdlp) == 0) {
  600. u_int i;
  601. int is_ethernet;
  602. bdlp->bfl_list = (u_int *) malloc(sizeof(u_int) * (bdlp->bfl_len + 1));
  603. if (bdlp->bfl_list == NULL) {
  604. pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
  605. errno, "malloc");
  606. return (PCAP_ERROR);
  607. }
  608. if (ioctl(fd, BIOCGDLTLIST, (caddr_t)bdlp) < 0) {
  609. pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
  610. errno, "BIOCGDLTLIST");
  611. free(bdlp->bfl_list);
  612. return (PCAP_ERROR);
  613. }
  614. /*
  615. * OK, for real Ethernet devices, add DLT_DOCSIS to the
  616. * list, so that an application can let you choose it,
  617. * in case you're capturing DOCSIS traffic that a Cisco
  618. * Cable Modem Termination System is putting out onto
  619. * an Ethernet (it doesn't put an Ethernet header onto
  620. * the wire, it puts raw DOCSIS frames out on the wire
  621. * inside the low-level Ethernet framing).
  622. *
  623. * A "real Ethernet device" is defined here as a device
  624. * that has a link-layer type of DLT_EN10MB and that has
  625. * no alternate link-layer types; that's done to exclude
  626. * 802.11 interfaces (which might or might not be the
  627. * right thing to do, but I suspect it is - Ethernet <->
  628. * 802.11 bridges would probably badly mishandle frames
  629. * that don't have Ethernet headers).
  630. *
  631. * On Solaris with BPF, Ethernet devices also offer
  632. * DLT_IPNET, so we, if DLT_IPNET is defined, we don't
  633. * treat it as an indication that the device isn't an
  634. * Ethernet.
  635. */
  636. if (v == DLT_EN10MB) {
  637. is_ethernet = 1;
  638. for (i = 0; i < bdlp->bfl_len; i++) {
  639. if (bdlp->bfl_list[i] != DLT_EN10MB
  640. #ifdef DLT_IPNET
  641. && bdlp->bfl_list[i] != DLT_IPNET
  642. #endif
  643. ) {
  644. is_ethernet = 0;
  645. break;
  646. }
  647. }
  648. if (is_ethernet) {
  649. /*
  650. * We reserved one more slot at the end of
  651. * the list.
  652. */
  653. bdlp->bfl_list[bdlp->bfl_len] = DLT_DOCSIS;
  654. bdlp->bfl_len++;
  655. }
  656. }
  657. } else {
  658. /*
  659. * EINVAL just means "we don't support this ioctl on
  660. * this device"; don't treat it as an error.
  661. */
  662. if (errno != EINVAL) {
  663. pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
  664. errno, "BIOCGDLTLIST");
  665. return (PCAP_ERROR);
  666. }
  667. }
  668. return (0);
  669. }
  670. #endif
  671. static int
  672. pcap_can_set_rfmon_bpf(pcap_t *p)
  673. {
  674. #if defined(__APPLE__)
  675. struct utsname osinfo;
  676. struct ifreq ifr;
  677. int fd;
  678. #ifdef BIOCGDLTLIST
  679. struct bpf_dltlist bdl;
  680. #endif
  681. /*
  682. * The joys of monitor mode on Mac OS X/OS X/macOS.
  683. *
  684. * Prior to 10.4, it's not supported at all.
  685. *
  686. * In 10.4, if adapter enN supports monitor mode, there's a
  687. * wltN adapter corresponding to it; you open it, instead of
  688. * enN, to get monitor mode. You get whatever link-layer
  689. * headers it supplies.
  690. *
  691. * In 10.5, and, we assume, later releases, if adapter enN
  692. * supports monitor mode, it offers, among its selectable
  693. * DLT_ values, values that let you get the 802.11 header;
  694. * selecting one of those values puts the adapter into monitor
  695. * mode (i.e., you can't get 802.11 headers except in monitor
  696. * mode, and you can't get Ethernet headers in monitor mode).
  697. */
  698. if (uname(&osinfo) == -1) {
  699. /*
  700. * Can't get the OS version; just say "no".
  701. */
  702. return (0);
  703. }
  704. /*
  705. * We assume osinfo.sysname is "Darwin", because
  706. * __APPLE__ is defined. We just check the version.
  707. */
  708. if (osinfo.release[0] < '8' && osinfo.release[1] == '.') {
  709. /*
  710. * 10.3 (Darwin 7.x) or earlier.
  711. * Monitor mode not supported.
  712. */
  713. return (0);
  714. }
  715. if (osinfo.release[0] == '8' && osinfo.release[1] == '.') {
  716. /*
  717. * 10.4 (Darwin 8.x). s/en/wlt/, and check
  718. * whether the device exists.
  719. */
  720. if (strncmp(p->opt.device, "en", 2) != 0) {
  721. /*
  722. * Not an enN device; no monitor mode.
  723. */
  724. return (0);
  725. }
  726. fd = socket(AF_INET, SOCK_DGRAM, 0);
  727. if (fd == -1) {
  728. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  729. errno, "socket");
  730. return (PCAP_ERROR);
  731. }
  732. strlcpy(ifr.ifr_name, "wlt", sizeof(ifr.ifr_name));
  733. strlcat(ifr.ifr_name, p->opt.device + 2, sizeof(ifr.ifr_name));
  734. if (ioctl(fd, SIOCGIFFLAGS, (char *)&ifr) < 0) {
  735. /*
  736. * No such device?
  737. */
  738. close(fd);
  739. return (0);
  740. }
  741. close(fd);
  742. return (1);
  743. }
  744. #ifdef BIOCGDLTLIST
  745. /*
  746. * Everything else is 10.5 or later; for those,
  747. * we just open the enN device, and check whether
  748. * we have any 802.11 devices.
  749. *
  750. * First, open a BPF device.
  751. */
  752. fd = bpf_open(p->errbuf);
  753. if (fd < 0)
  754. return (fd); /* fd is the appropriate error code */
  755. /*
  756. * Now bind to the device.
  757. */
  758. (void)strncpy(ifr.ifr_name, p->opt.device, sizeof(ifr.ifr_name));
  759. if (ioctl(fd, BIOCSETIF, (caddr_t)&ifr) < 0) {
  760. switch (errno) {
  761. case ENXIO:
  762. /*
  763. * There's no such device.
  764. */
  765. close(fd);
  766. return (PCAP_ERROR_NO_SUCH_DEVICE);
  767. case ENETDOWN:
  768. /*
  769. * Return a "network down" indication, so that
  770. * the application can report that rather than
  771. * saying we had a mysterious failure and
  772. * suggest that they report a problem to the
  773. * libpcap developers.
  774. */
  775. close(fd);
  776. return (PCAP_ERROR_IFACE_NOT_UP);
  777. default:
  778. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  779. errno, "BIOCSETIF: %s", p->opt.device);
  780. close(fd);
  781. return (PCAP_ERROR);
  782. }
  783. }
  784. /*
  785. * We know the default link type -- now determine all the DLTs
  786. * this interface supports. If this fails with EINVAL, it's
  787. * not fatal; we just don't get to use the feature later.
  788. * (We don't care about DLT_DOCSIS, so we pass DLT_NULL
  789. * as the default DLT for this adapter.)
  790. */
  791. if (get_dlt_list(fd, DLT_NULL, &bdl, p->errbuf) == PCAP_ERROR) {
  792. close(fd);
  793. return (PCAP_ERROR);
  794. }
  795. if (find_802_11(&bdl) != -1) {
  796. /*
  797. * We have an 802.11 DLT, so we can set monitor mode.
  798. */
  799. free(bdl.bfl_list);
  800. close(fd);
  801. return (1);
  802. }
  803. free(bdl.bfl_list);
  804. close(fd);
  805. #endif /* BIOCGDLTLIST */
  806. return (0);
  807. #elif defined(HAVE_BSD_IEEE80211)
  808. int ret;
  809. ret = monitor_mode(p, 0);
  810. if (ret == PCAP_ERROR_RFMON_NOTSUP)
  811. return (0); /* not an error, just a "can't do" */
  812. if (ret == 0)
  813. return (1); /* success */
  814. return (ret);
  815. #else
  816. return (0);
  817. #endif
  818. }
  819. static int
  820. pcap_stats_bpf(pcap_t *p, struct pcap_stat *ps)
  821. {
  822. struct bpf_stat s;
  823. /*
  824. * "ps_recv" counts packets handed to the filter, not packets
  825. * that passed the filter. This includes packets later dropped
  826. * because we ran out of buffer space.
  827. *
  828. * "ps_drop" counts packets dropped inside the BPF device
  829. * because we ran out of buffer space. It doesn't count
  830. * packets dropped by the interface driver. It counts
  831. * only packets that passed the filter.
  832. *
  833. * Both statistics include packets not yet read from the kernel
  834. * by libpcap, and thus not yet seen by the application.
  835. */
  836. if (ioctl(p->fd, BIOCGSTATS, (caddr_t)&s) < 0) {
  837. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  838. errno, "BIOCGSTATS");
  839. return (PCAP_ERROR);
  840. }
  841. ps->ps_recv = s.bs_recv;
  842. ps->ps_drop = s.bs_drop;
  843. ps->ps_ifdrop = 0;
  844. return (0);
  845. }
  846. static int
  847. pcap_read_bpf(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
  848. {
  849. struct pcap_bpf *pb = p->priv;
  850. int cc;
  851. int n = 0;
  852. register u_char *bp, *ep;
  853. u_char *datap;
  854. #ifdef PCAP_FDDIPAD
  855. register u_int pad;
  856. #endif
  857. #ifdef HAVE_ZEROCOPY_BPF
  858. int i;
  859. #endif
  860. again:
  861. /*
  862. * Has "pcap_breakloop()" been called?
  863. */
  864. if (p->break_loop) {
  865. /*
  866. * Yes - clear the flag that indicates that it
  867. * has, and return PCAP_ERROR_BREAK to indicate
  868. * that we were told to break out of the loop.
  869. */
  870. p->break_loop = 0;
  871. return (PCAP_ERROR_BREAK);
  872. }
  873. cc = p->cc;
  874. if (p->cc == 0) {
  875. /*
  876. * When reading without zero-copy from a file descriptor, we
  877. * use a single buffer and return a length of data in the
  878. * buffer. With zero-copy, we update the p->buffer pointer
  879. * to point at whatever underlying buffer contains the next
  880. * data and update cc to reflect the data found in the
  881. * buffer.
  882. */
  883. #ifdef HAVE_ZEROCOPY_BPF
  884. if (pb->zerocopy) {
  885. if (p->buffer != NULL)
  886. pcap_ack_zbuf(p);
  887. i = pcap_next_zbuf(p, &cc);
  888. if (i == 0)
  889. goto again;
  890. if (i < 0)
  891. return (PCAP_ERROR);
  892. } else
  893. #endif
  894. {
  895. cc = read(p->fd, p->buffer, p->bufsize);
  896. }
  897. if (cc < 0) {
  898. /* Don't choke when we get ptraced */
  899. switch (errno) {
  900. case EINTR:
  901. goto again;
  902. #ifdef _AIX
  903. case EFAULT:
  904. /*
  905. * Sigh. More AIX wonderfulness.
  906. *
  907. * For some unknown reason the uiomove()
  908. * operation in the bpf kernel extension
  909. * used to copy the buffer into user
  910. * space sometimes returns EFAULT. I have
  911. * no idea why this is the case given that
  912. * a kernel debugger shows the user buffer
  913. * is correct. This problem appears to
  914. * be mostly mitigated by the memset of
  915. * the buffer before it is first used.
  916. * Very strange.... Shaun Clowes
  917. *
  918. * In any case this means that we shouldn't
  919. * treat EFAULT as a fatal error; as we
  920. * don't have an API for returning
  921. * a "some packets were dropped since
  922. * the last packet you saw" indication,
  923. * we just ignore EFAULT and keep reading.
  924. */
  925. goto again;
  926. #endif
  927. case EWOULDBLOCK:
  928. return (0);
  929. case ENXIO:
  930. /*
  931. * The device on which we're capturing
  932. * went away.
  933. *
  934. * XXX - we should really return
  935. * PCAP_ERROR_IFACE_NOT_UP, but
  936. * pcap_dispatch() etc. aren't
  937. * defined to retur that.
  938. */
  939. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  940. "The interface went down");
  941. return (PCAP_ERROR);
  942. #if defined(sun) && !defined(BSD) && !defined(__svr4__) && !defined(__SVR4)
  943. /*
  944. * Due to a SunOS bug, after 2^31 bytes, the kernel
  945. * file offset overflows and read fails with EINVAL.
  946. * The lseek() to 0 will fix things.
  947. */
  948. case EINVAL:
  949. if (lseek(p->fd, 0L, SEEK_CUR) +
  950. p->bufsize < 0) {
  951. (void)lseek(p->fd, 0L, SEEK_SET);
  952. goto again;
  953. }
  954. /* fall through */
  955. #endif
  956. }
  957. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  958. errno, "read");
  959. return (PCAP_ERROR);
  960. }
  961. bp = (u_char *)p->buffer;
  962. } else
  963. bp = p->bp;
  964. /*
  965. * Loop through each packet.
  966. */
  967. #ifdef BIOCSTSTAMP
  968. #define bhp ((struct bpf_xhdr *)bp)
  969. #else
  970. #define bhp ((struct bpf_hdr *)bp)
  971. #endif
  972. ep = bp + cc;
  973. #ifdef PCAP_FDDIPAD
  974. pad = p->fddipad;
  975. #endif
  976. while (bp < ep) {
  977. register u_int caplen, hdrlen;
  978. /*
  979. * Has "pcap_breakloop()" been called?
  980. * If so, return immediately - if we haven't read any
  981. * packets, clear the flag and return PCAP_ERROR_BREAK
  982. * to indicate that we were told to break out of the loop,
  983. * otherwise leave the flag set, so that the *next* call
  984. * will break out of the loop without having read any
  985. * packets, and return the number of packets we've
  986. * processed so far.
  987. */
  988. if (p->break_loop) {
  989. p->bp = bp;
  990. p->cc = ep - bp;
  991. /*
  992. * ep is set based on the return value of read(),
  993. * but read() from a BPF device doesn't necessarily
  994. * return a value that's a multiple of the alignment
  995. * value for BPF_WORDALIGN(). However, whenever we
  996. * increment bp, we round up the increment value by
  997. * a value rounded up by BPF_WORDALIGN(), so we
  998. * could increment bp past ep after processing the
  999. * last packet in the buffer.
  1000. *
  1001. * We treat ep < bp as an indication that this
  1002. * happened, and just set p->cc to 0.
  1003. */
  1004. if (p->cc < 0)
  1005. p->cc = 0;
  1006. if (n == 0) {
  1007. p->break_loop = 0;
  1008. return (PCAP_ERROR_BREAK);
  1009. } else
  1010. return (n);
  1011. }
  1012. caplen = bhp->bh_caplen;
  1013. hdrlen = bhp->bh_hdrlen;
  1014. datap = bp + hdrlen;
  1015. /*
  1016. * Short-circuit evaluation: if using BPF filter
  1017. * in kernel, no need to do it now - we already know
  1018. * the packet passed the filter.
  1019. *
  1020. #ifdef PCAP_FDDIPAD
  1021. * Note: the filter code was generated assuming
  1022. * that p->fddipad was the amount of padding
  1023. * before the header, as that's what's required
  1024. * in the kernel, so we run the filter before
  1025. * skipping that padding.
  1026. #endif
  1027. */
  1028. if (pb->filtering_in_kernel ||
  1029. bpf_filter(p->fcode.bf_insns, datap, bhp->bh_datalen, caplen)) {
  1030. struct pcap_pkthdr pkthdr;
  1031. #ifdef BIOCSTSTAMP
  1032. struct bintime bt;
  1033. bt.sec = bhp->bh_tstamp.bt_sec;
  1034. bt.frac = bhp->bh_tstamp.bt_frac;
  1035. if (p->opt.tstamp_precision == PCAP_TSTAMP_PRECISION_NANO) {
  1036. struct timespec ts;
  1037. bintime2timespec(&bt, &ts);
  1038. pkthdr.ts.tv_sec = ts.tv_sec;
  1039. pkthdr.ts.tv_usec = ts.tv_nsec;
  1040. } else {
  1041. struct timeval tv;
  1042. bintime2timeval(&bt, &tv);
  1043. pkthdr.ts.tv_sec = tv.tv_sec;
  1044. pkthdr.ts.tv_usec = tv.tv_usec;
  1045. }
  1046. #else
  1047. pkthdr.ts.tv_sec = bhp->bh_tstamp.tv_sec;
  1048. #ifdef _AIX
  1049. /*
  1050. * AIX's BPF returns seconds/nanoseconds time
  1051. * stamps, not seconds/microseconds time stamps.
  1052. */
  1053. pkthdr.ts.tv_usec = bhp->bh_tstamp.tv_usec/1000;
  1054. #else
  1055. pkthdr.ts.tv_usec = bhp->bh_tstamp.tv_usec;
  1056. #endif
  1057. #endif /* BIOCSTSTAMP */
  1058. #ifdef PCAP_FDDIPAD
  1059. if (caplen > pad)
  1060. pkthdr.caplen = caplen - pad;
  1061. else
  1062. pkthdr.caplen = 0;
  1063. if (bhp->bh_datalen > pad)
  1064. pkthdr.len = bhp->bh_datalen - pad;
  1065. else
  1066. pkthdr.len = 0;
  1067. datap += pad;
  1068. #else
  1069. pkthdr.caplen = caplen;
  1070. pkthdr.len = bhp->bh_datalen;
  1071. #endif
  1072. (*callback)(user, &pkthdr, datap);
  1073. bp += BPF_WORDALIGN(caplen + hdrlen);
  1074. if (++n >= cnt && !PACKET_COUNT_IS_UNLIMITED(cnt)) {
  1075. p->bp = bp;
  1076. p->cc = ep - bp;
  1077. /*
  1078. * See comment above about p->cc < 0.
  1079. */
  1080. if (p->cc < 0)
  1081. p->cc = 0;
  1082. return (n);
  1083. }
  1084. } else {
  1085. /*
  1086. * Skip this packet.
  1087. */
  1088. bp += BPF_WORDALIGN(caplen + hdrlen);
  1089. }
  1090. }
  1091. #undef bhp
  1092. p->cc = 0;
  1093. return (n);
  1094. }
  1095. static int
  1096. pcap_inject_bpf(pcap_t *p, const void *buf, size_t size)
  1097. {
  1098. int ret;
  1099. ret = write(p->fd, buf, size);
  1100. #ifdef __APPLE__
  1101. if (ret == -1 && errno == EAFNOSUPPORT) {
  1102. /*
  1103. * In some versions of macOS, there's a bug wherein setting
  1104. * the BIOCSHDRCMPLT flag causes writes to fail; see, for
  1105. * example:
  1106. *
  1107. * http://cerberus.sourcefire.com/~jeff/archives/patches/macosx/BIOCSHDRCMPLT-10.3.3.patch
  1108. *
  1109. * So, if, on macOS, we get EAFNOSUPPORT from the write, we
  1110. * assume it's due to that bug, and turn off that flag
  1111. * and try again. If we succeed, it either means that
  1112. * somebody applied the fix from that URL, or other patches
  1113. * for that bug from
  1114. *
  1115. * http://cerberus.sourcefire.com/~jeff/archives/patches/macosx/
  1116. *
  1117. * and are running a Darwin kernel with those fixes, or
  1118. * that Apple fixed the problem in some macOS release.
  1119. */
  1120. u_int spoof_eth_src = 0;
  1121. if (ioctl(p->fd, BIOCSHDRCMPLT, &spoof_eth_src) == -1) {
  1122. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  1123. errno, "send: can't turn off BIOCSHDRCMPLT");
  1124. return (PCAP_ERROR);
  1125. }
  1126. /*
  1127. * Now try the write again.
  1128. */
  1129. ret = write(p->fd, buf, size);
  1130. }
  1131. #endif /* __APPLE__ */
  1132. if (ret == -1) {
  1133. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  1134. errno, "send");
  1135. return (PCAP_ERROR);
  1136. }
  1137. return (ret);
  1138. }
  1139. #ifdef _AIX
  1140. static int
  1141. bpf_odminit(char *errbuf)
  1142. {
  1143. char *errstr;
  1144. if (odm_initialize() == -1) {
  1145. if (odm_err_msg(odmerrno, &errstr) == -1)
  1146. errstr = "Unknown error";
  1147. pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
  1148. "bpf_load: odm_initialize failed: %s",
  1149. errstr);
  1150. return (PCAP_ERROR);
  1151. }
  1152. if ((odmlockid = odm_lock("/etc/objrepos/config_lock", ODM_WAIT)) == -1) {
  1153. if (odm_err_msg(odmerrno, &errstr) == -1)
  1154. errstr = "Unknown error";
  1155. pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
  1156. "bpf_load: odm_lock of /etc/objrepos/config_lock failed: %s",
  1157. errstr);
  1158. (void)odm_terminate();
  1159. return (PCAP_ERROR);
  1160. }
  1161. return (0);
  1162. }
  1163. static int
  1164. bpf_odmcleanup(char *errbuf)
  1165. {
  1166. char *errstr;
  1167. if (odm_unlock(odmlockid) == -1) {
  1168. if (errbuf != NULL) {
  1169. if (odm_err_msg(odmerrno, &errstr) == -1)
  1170. errstr = "Unknown error";
  1171. pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
  1172. "bpf_load: odm_unlock failed: %s",
  1173. errstr);
  1174. }
  1175. return (PCAP_ERROR);
  1176. }
  1177. if (odm_terminate() == -1) {
  1178. if (errbuf != NULL) {
  1179. if (odm_err_msg(odmerrno, &errstr) == -1)
  1180. errstr = "Unknown error";
  1181. pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
  1182. "bpf_load: odm_terminate failed: %s",
  1183. errstr);
  1184. }
  1185. return (PCAP_ERROR);
  1186. }
  1187. return (0);
  1188. }
  1189. static int
  1190. bpf_load(char *errbuf)
  1191. {
  1192. long major;
  1193. int *minors;
  1194. int numminors, i, rc;
  1195. char buf[1024];
  1196. struct stat sbuf;
  1197. struct bpf_config cfg_bpf;
  1198. struct cfg_load cfg_ld;
  1199. struct cfg_kmod cfg_km;
  1200. /*
  1201. * This is very very close to what happens in the real implementation
  1202. * but I've fixed some (unlikely) bug situations.
  1203. */
  1204. if (bpfloadedflag)
  1205. return (0);
  1206. if (bpf_odminit(errbuf) == PCAP_ERROR)
  1207. return (PCAP_ERROR);
  1208. major = genmajor(BPF_NAME);
  1209. if (major == -1) {
  1210. pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
  1211. errno, "bpf_load: genmajor failed");
  1212. (void)bpf_odmcleanup(NULL);
  1213. return (PCAP_ERROR);
  1214. }
  1215. minors = getminor(major, &numminors, BPF_NAME);
  1216. if (!minors) {
  1217. minors = genminor("bpf", major, 0, BPF_MINORS, 1, 1);
  1218. if (!minors) {
  1219. pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
  1220. errno, "bpf_load: genminor failed");
  1221. (void)bpf_odmcleanup(NULL);
  1222. return (PCAP_ERROR);
  1223. }
  1224. }
  1225. if (bpf_odmcleanup(errbuf) == PCAP_ERROR)
  1226. return (PCAP_ERROR);
  1227. rc = stat(BPF_NODE "0", &sbuf);
  1228. if (rc == -1 && errno != ENOENT) {
  1229. pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
  1230. errno, "bpf_load: can't stat %s", BPF_NODE "0");
  1231. return (PCAP_ERROR);
  1232. }
  1233. if (rc == -1 || getmajor(sbuf.st_rdev) != major) {
  1234. for (i = 0; i < BPF_MINORS; i++) {
  1235. pcap_snprintf(buf, sizeof(buf), "%s%d", BPF_NODE, i);
  1236. unlink(buf);
  1237. if (mknod(buf, S_IRUSR | S_IFCHR, domakedev(major, i)) == -1) {
  1238. pcap_fmt_errmsg_for_errno(errbuf,
  1239. PCAP_ERRBUF_SIZE, errno,
  1240. "bpf_load: can't mknod %s", buf);
  1241. return (PCAP_ERROR);
  1242. }
  1243. }
  1244. }
  1245. /* Check if the driver is loaded */
  1246. memset(&cfg_ld, 0x0, sizeof(cfg_ld));
  1247. cfg_ld.path = buf;
  1248. pcap_snprintf(cfg_ld.path, sizeof(cfg_ld.path), "%s/%s", DRIVER_PATH, BPF_NAME);
  1249. if ((sysconfig(SYS_QUERYLOAD, (void *)&cfg_ld, sizeof(cfg_ld)) == -1) ||
  1250. (cfg_ld.kmid == 0)) {
  1251. /* Driver isn't loaded, load it now */
  1252. if (sysconfig(SYS_SINGLELOAD, (void *)&cfg_ld, sizeof(cfg_ld)) == -1) {
  1253. pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
  1254. errno, "bpf_load: could not load driver");
  1255. return (PCAP_ERROR);
  1256. }
  1257. }
  1258. /* Configure the driver */
  1259. cfg_km.cmd = CFG_INIT;
  1260. cfg_km.kmid = cfg_ld.kmid;
  1261. cfg_km.mdilen = sizeof(cfg_bpf);
  1262. cfg_km.mdiptr = (void *)&cfg_bpf;
  1263. for (i = 0; i < BPF_MINORS; i++) {
  1264. cfg_bpf.devno = domakedev(major, i);
  1265. if (sysconfig(SYS_CFGKMOD, (void *)&cfg_km, sizeof(cfg_km)) == -1) {
  1266. pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
  1267. errno, "bpf_load: could not configure driver");
  1268. return (PCAP_ERROR);
  1269. }
  1270. }
  1271. bpfloadedflag = 1;
  1272. return (0);
  1273. }
  1274. #endif
  1275. /*
  1276. * Undo any operations done when opening the device when necessary.
  1277. */
  1278. static void
  1279. pcap_cleanup_bpf(pcap_t *p)
  1280. {
  1281. struct pcap_bpf *pb = p->priv;
  1282. #ifdef HAVE_BSD_IEEE80211
  1283. int sock;
  1284. struct ifmediareq req;
  1285. struct ifreq ifr;
  1286. #endif
  1287. if (pb->must_do_on_close != 0) {
  1288. /*
  1289. * There's something we have to do when closing this
  1290. * pcap_t.
  1291. */
  1292. #ifdef HAVE_BSD_IEEE80211
  1293. if (pb->must_do_on_close & MUST_CLEAR_RFMON) {
  1294. /*
  1295. * We put the interface into rfmon mode;
  1296. * take it out of rfmon mode.
  1297. *
  1298. * XXX - if somebody else wants it in rfmon
  1299. * mode, this code cannot know that, so it'll take
  1300. * it out of rfmon mode.
  1301. */
  1302. sock = socket(AF_INET, SOCK_DGRAM, 0);
  1303. if (sock == -1) {
  1304. fprintf(stderr,
  1305. "Can't restore interface flags (socket() failed: %s).\n"
  1306. "Please adjust manually.\n",
  1307. strerror(errno));
  1308. } else {
  1309. memset(&req, 0, sizeof(req));
  1310. strncpy(req.ifm_name, pb->device,
  1311. sizeof(req.ifm_name));
  1312. if (ioctl(sock, SIOCGIFMEDIA, &req) < 0) {
  1313. fprintf(stderr,
  1314. "Can't restore interface flags (SIOCGIFMEDIA failed: %s).\n"
  1315. "Please adjust manually.\n",
  1316. strerror(errno));
  1317. } else {
  1318. if (req.ifm_current & IFM_IEEE80211_MONITOR) {
  1319. /*
  1320. * Rfmon mode is currently on;
  1321. * turn it off.
  1322. */
  1323. memset(&ifr, 0, sizeof(ifr));
  1324. (void)strncpy(ifr.ifr_name,
  1325. pb->device,
  1326. sizeof(ifr.ifr_name));
  1327. ifr.ifr_media =
  1328. req.ifm_current & ~IFM_IEEE80211_MONITOR;
  1329. if (ioctl(sock, SIOCSIFMEDIA,
  1330. &ifr) == -1) {
  1331. fprintf(stderr,
  1332. "Can't restore interface flags (SIOCSIFMEDIA failed: %s).\n"
  1333. "Please adjust manually.\n",
  1334. strerror(errno));
  1335. }
  1336. }
  1337. }
  1338. close(sock);
  1339. }
  1340. }
  1341. #endif /* HAVE_BSD_IEEE80211 */
  1342. #if defined(__FreeBSD__) && defined(SIOCIFCREATE2)
  1343. /*
  1344. * Attempt to destroy the usbusN interface that we created.
  1345. */
  1346. if (pb->must_do_on_close & MUST_DESTROY_USBUS) {
  1347. if (if_nametoindex(pb->device) > 0) {
  1348. int s;
  1349. s = socket(AF_LOCAL, SOCK_DGRAM, 0);
  1350. if (s >= 0) {
  1351. strlcpy(ifr.ifr_name, pb->device,
  1352. sizeof(ifr.ifr_name));
  1353. ioctl(s, SIOCIFDESTROY, &ifr);
  1354. close(s);
  1355. }
  1356. }
  1357. }
  1358. #endif /* defined(__FreeBSD__) && defined(SIOCIFCREATE2) */
  1359. /*
  1360. * Take this pcap out of the list of pcaps for which we
  1361. * have to take the interface out of some mode.
  1362. */
  1363. pcap_remove_from_pcaps_to_close(p);
  1364. pb->must_do_on_close = 0;
  1365. }
  1366. #ifdef HAVE_ZEROCOPY_BPF
  1367. if (pb->zerocopy) {
  1368. /*
  1369. * Delete the mappings. Note that p->buffer gets
  1370. * initialized to one of the mmapped regions in
  1371. * this case, so do not try and free it directly;
  1372. * null it out so that pcap_cleanup_live_common()
  1373. * doesn't try to free it.
  1374. */
  1375. if (pb->zbuf1 != MAP_FAILED && pb->zbuf1 != NULL)
  1376. (void) munmap(pb->zbuf1, pb->zbufsize);
  1377. if (pb->zbuf2 != MAP_FAILED && pb->zbuf2 != NULL)
  1378. (void) munmap(pb->zbuf2, pb->zbufsize);
  1379. p->buffer = NULL;
  1380. }
  1381. #endif
  1382. if (pb->device != NULL) {
  1383. free(pb->device);
  1384. pb->device = NULL;
  1385. }
  1386. pcap_cleanup_live_common(p);
  1387. }
  1388. static int
  1389. check_setif_failure(pcap_t *p, int error)
  1390. {
  1391. #ifdef __APPLE__
  1392. int fd;
  1393. struct ifreq ifr;
  1394. int err;
  1395. #endif
  1396. if (error == ENXIO) {
  1397. /*
  1398. * No such device exists.
  1399. */
  1400. #ifdef __APPLE__
  1401. if (p->opt.rfmon && strncmp(p->opt.device, "wlt", 3) == 0) {
  1402. /*
  1403. * Monitor mode was requested, and we're trying
  1404. * to open a "wltN" device. Assume that this
  1405. * is 10.4 and that we were asked to open an
  1406. * "enN" device; if that device exists, return
  1407. * "monitor mode not supported on the device".
  1408. */
  1409. fd = socket(AF_INET, SOCK_DGRAM, 0);
  1410. if (fd != -1) {
  1411. strlcpy(ifr.ifr_name, "en",
  1412. sizeof(ifr.ifr_name));
  1413. strlcat(ifr.ifr_name, p->opt.device + 3,
  1414. sizeof(ifr.ifr_name));
  1415. if (ioctl(fd, SIOCGIFFLAGS, (char *)&ifr) < 0) {
  1416. /*
  1417. * We assume this failed because
  1418. * the underlying device doesn't
  1419. * exist.
  1420. */
  1421. err = PCAP_ERROR_NO_SUCH_DEVICE;
  1422. pcap_fmt_errmsg_for_errno(p->errbuf,
  1423. PCAP_ERRBUF_SIZE, errno,
  1424. "SIOCGIFFLAGS on %s failed",
  1425. ifr.ifr_name);
  1426. } else {
  1427. /*
  1428. * The underlying "enN" device
  1429. * exists, but there's no
  1430. * corresponding "wltN" device;
  1431. * that means that the "enN"
  1432. * device doesn't support
  1433. * monitor mode, probably because
  1434. * it's an Ethernet device rather
  1435. * than a wireless device.
  1436. */
  1437. err = PCAP_ERROR_RFMON_NOTSUP;
  1438. }
  1439. close(fd);
  1440. } else {
  1441. /*
  1442. * We can't find out whether there's
  1443. * an underlying "enN" device, so
  1444. * just report "no such device".
  1445. */
  1446. err = PCAP_ERROR_NO_SUCH_DEVICE;
  1447. pcap_fmt_errmsg_for_errno(p->errbuf,
  1448. errno, PCAP_ERRBUF_SIZE,
  1449. "socket() failed");
  1450. }
  1451. return (err);
  1452. }
  1453. #endif
  1454. /*
  1455. * No such device.
  1456. */
  1457. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  1458. errno, "BIOCSETIF failed");
  1459. return (PCAP_ERROR_NO_SUCH_DEVICE);
  1460. } else if (errno == ENETDOWN) {
  1461. /*
  1462. * Return a "network down" indication, so that
  1463. * the application can report that rather than
  1464. * saying we had a mysterious failure and
  1465. * suggest that they report a problem to the
  1466. * libpcap developers.
  1467. */
  1468. return (PCAP_ERROR_IFACE_NOT_UP);
  1469. } else {
  1470. /*
  1471. * Some other error; fill in the error string, and
  1472. * return PCAP_ERROR.
  1473. */
  1474. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  1475. errno, "BIOCSETIF: %s", p->opt.device);
  1476. return (PCAP_ERROR);
  1477. }
  1478. }
  1479. /*
  1480. * Default capture buffer size.
  1481. * 32K isn't very much for modern machines with fast networks; we
  1482. * pick .5M, as that's the maximum on at least some systems with BPF.
  1483. *
  1484. * However, on AIX 3.5, the larger buffer sized caused unrecoverable
  1485. * read failures under stress, so we leave it as 32K; yet another
  1486. * place where AIX's BPF is broken.
  1487. */
  1488. #ifdef _AIX
  1489. #define DEFAULT_BUFSIZE 32768
  1490. #else
  1491. #define DEFAULT_BUFSIZE 524288
  1492. #endif
  1493. static int
  1494. pcap_activate_bpf(pcap_t *p)
  1495. {
  1496. struct pcap_bpf *pb = p->priv;
  1497. int status = 0;
  1498. #ifdef HAVE_BSD_IEEE80211
  1499. int retv;
  1500. #endif
  1501. int fd;
  1502. #ifdef LIFNAMSIZ
  1503. char *zonesep;
  1504. struct lifreq ifr;
  1505. char *ifrname = ifr.lifr_name;
  1506. const size_t ifnamsiz = sizeof(ifr.lifr_name);
  1507. #else
  1508. struct ifreq ifr;
  1509. char *ifrname = ifr.ifr_name;
  1510. const size_t ifnamsiz = sizeof(ifr.ifr_name);
  1511. #endif
  1512. struct bpf_version bv;
  1513. #ifdef __APPLE__
  1514. int sockfd;
  1515. char *wltdev = NULL;
  1516. #endif
  1517. #ifdef BIOCGDLTLIST
  1518. struct bpf_dltlist bdl;
  1519. #if defined(__APPLE__) || defined(HAVE_BSD_IEEE80211)
  1520. int new_dlt;
  1521. #endif
  1522. #endif /* BIOCGDLTLIST */
  1523. #if defined(BIOCGHDRCMPLT) && defined(BIOCSHDRCMPLT)
  1524. u_int spoof_eth_src = 1;
  1525. #endif
  1526. u_int v;
  1527. struct bpf_insn total_insn;
  1528. struct bpf_program total_prog;
  1529. struct utsname osinfo;
  1530. int have_osinfo = 0;
  1531. #ifdef HAVE_ZEROCOPY_BPF
  1532. struct bpf_zbuf bz;
  1533. u_int bufmode, zbufmax;
  1534. #endif
  1535. fd = bpf_open(p->errbuf);
  1536. if (fd < 0) {
  1537. status = fd;
  1538. goto bad;
  1539. }
  1540. p->fd = fd;
  1541. if (ioctl(fd, BIOCVERSION, (caddr_t)&bv) < 0) {
  1542. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  1543. errno, "BIOCVERSION");
  1544. status = PCAP_ERROR;
  1545. goto bad;
  1546. }
  1547. if (bv.bv_major != BPF_MAJOR_VERSION ||
  1548. bv.bv_minor < BPF_MINOR_VERSION) {
  1549. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  1550. "kernel bpf filter out of date");
  1551. status = PCAP_ERROR;
  1552. goto bad;
  1553. }
  1554. /*
  1555. * Turn a negative snapshot value (invalid), a snapshot value of
  1556. * 0 (unspecified), or a value bigger than the normal maximum
  1557. * value, into the maximum allowed value.
  1558. *
  1559. * If some application really *needs* a bigger snapshot
  1560. * length, we should just increase MAXIMUM_SNAPLEN.
  1561. */
  1562. if (p->snapshot <= 0 || p->snapshot > MAXIMUM_SNAPLEN)
  1563. p->snapshot = MAXIMUM_SNAPLEN;
  1564. #if defined(LIFNAMSIZ) && defined(ZONENAME_MAX) && defined(lifr_zoneid)
  1565. /*
  1566. * Retrieve the zoneid of the zone we are currently executing in.
  1567. */
  1568. if ((ifr.lifr_zoneid = getzoneid()) == -1) {
  1569. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  1570. errno, "getzoneid()");
  1571. status = PCAP_ERROR;
  1572. goto bad;
  1573. }
  1574. /*
  1575. * Check if the given source datalink name has a '/' separated
  1576. * zonename prefix string. The zonename prefixed source datalink can
  1577. * be used by pcap consumers in the Solaris global zone to capture
  1578. * traffic on datalinks in non-global zones. Non-global zones
  1579. * do not have access to datalinks outside of their own namespace.
  1580. */
  1581. if ((zonesep = strchr(p->opt.device, '/')) != NULL) {
  1582. char path_zname[ZONENAME_MAX];
  1583. int znamelen;
  1584. char *lnamep;
  1585. if (ifr.lifr_zoneid != GLOBAL_ZONEID) {
  1586. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  1587. "zonename/linkname only valid in global zone.");
  1588. status = PCAP_ERROR;
  1589. goto bad;
  1590. }
  1591. znamelen = zonesep - p->opt.device;
  1592. (void) strlcpy(path_zname, p->opt.device, znamelen + 1);
  1593. ifr.lifr_zoneid = getzoneidbyname(path_zname);
  1594. if (ifr.lifr_zoneid == -1) {
  1595. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  1596. errno, "getzoneidbyname(%s)", path_zname);
  1597. status = PCAP_ERROR;
  1598. goto bad;
  1599. }
  1600. lnamep = strdup(zonesep + 1);
  1601. if (lnamep == NULL) {
  1602. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  1603. errno, "strdup");
  1604. status = PCAP_ERROR;
  1605. goto bad;
  1606. }
  1607. free(p->opt.device);
  1608. p->opt.device = lnamep;
  1609. }
  1610. #endif
  1611. pb->device = strdup(p->opt.device);
  1612. if (pb->device == NULL) {
  1613. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  1614. errno, "strdup");
  1615. status = PCAP_ERROR;
  1616. goto bad;
  1617. }
  1618. /*
  1619. * Attempt to find out the version of the OS on which we're running.
  1620. */
  1621. if (uname(&osinfo) == 0)
  1622. have_osinfo = 1;
  1623. #ifdef __APPLE__
  1624. /*
  1625. * See comment in pcap_can_set_rfmon_bpf() for an explanation
  1626. * of why we check the version number.
  1627. */
  1628. if (p->opt.rfmon) {
  1629. if (have_osinfo) {
  1630. /*
  1631. * We assume osinfo.sysname is "Darwin", because
  1632. * __APPLE__ is defined. We just check the version.
  1633. */
  1634. if (osinfo.release[0] < '8' &&
  1635. osinfo.release[1] == '.') {
  1636. /*
  1637. * 10.3 (Darwin 7.x) or earlier.
  1638. */
  1639. status = PCAP_ERROR_RFMON_NOTSUP;
  1640. goto bad;
  1641. }
  1642. if (osinfo.release[0] == '8' &&
  1643. osinfo.release[1] == '.') {
  1644. /*
  1645. * 10.4 (Darwin 8.x). s/en/wlt/
  1646. */
  1647. if (strncmp(p->opt.device, "en", 2) != 0) {
  1648. /*
  1649. * Not an enN device; check
  1650. * whether the device even exists.
  1651. */
  1652. sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  1653. if (sockfd != -1) {
  1654. strlcpy(ifrname,
  1655. p->opt.device, ifnamsiz);
  1656. if (ioctl(sockfd, SIOCGIFFLAGS,
  1657. (char *)&ifr) < 0) {
  1658. /*
  1659. * We assume this
  1660. * failed because
  1661. * the underlying
  1662. * device doesn't
  1663. * exist.
  1664. */
  1665. status = PCAP_ERROR_NO_SUCH_DEVICE;
  1666. pcap_fmt_errmsg_for_errno(p->errbuf,
  1667. PCAP_ERRBUF_SIZE,
  1668. errno,
  1669. "SIOCGIFFLAGS failed");
  1670. } else
  1671. status = PCAP_ERROR_RFMON_NOTSUP;
  1672. close(sockfd);
  1673. } else {
  1674. /*
  1675. * We can't find out whether
  1676. * the device exists, so just
  1677. * report "no such device".
  1678. */
  1679. status = PCAP_ERROR_NO_SUCH_DEVICE;
  1680. pcap_fmt_errmsg_for_errno(p->errbuf,
  1681. PCAP_ERRBUF_SIZE, errno,
  1682. "socket() failed");
  1683. }
  1684. goto bad;
  1685. }
  1686. wltdev = malloc(strlen(p->opt.device) + 2);
  1687. if (wltdev == NULL) {
  1688. pcap_fmt_errmsg_for_errno(p->errbuf,
  1689. PCAP_ERRBUF_SIZE, errno,
  1690. "malloc");
  1691. status = PCAP_ERROR;
  1692. goto bad;
  1693. }
  1694. strcpy(wltdev, "wlt");
  1695. strcat(wltdev, p->opt.device + 2);
  1696. free(p->opt.device);
  1697. p->opt.device = wltdev;
  1698. }
  1699. /*
  1700. * Everything else is 10.5 or later; for those,
  1701. * we just open the enN device, and set the DLT.
  1702. */
  1703. }
  1704. }
  1705. #endif /* __APPLE__ */
  1706. /*
  1707. * If this is FreeBSD, and the device name begins with "usbus",
  1708. * try to create the interface if it's not available.
  1709. */
  1710. #if defined(__FreeBSD__) && defined(SIOCIFCREATE2)
  1711. if (strncmp(p->opt.device, usbus_prefix, USBUS_PREFIX_LEN) == 0) {
  1712. /*
  1713. * Do we already have an interface with that name?
  1714. */
  1715. if (if_nametoindex(p->opt.device) == 0) {
  1716. /*
  1717. * No. We need to create it, and, if we
  1718. * succeed, remember that we should destroy
  1719. * it when the pcap_t is closed.
  1720. */
  1721. int s;
  1722. /*
  1723. * Open a socket to use for ioctls to
  1724. * create the interface.
  1725. */
  1726. s = socket(AF_LOCAL, SOCK_DGRAM, 0);
  1727. if (s < 0) {
  1728. pcap_fmt_errmsg_for_errno(p->errbuf,
  1729. PCAP_ERRBUF_SIZE, errno,
  1730. "Can't open socket");
  1731. status = PCAP_ERROR;
  1732. goto bad;
  1733. }
  1734. /*
  1735. * If we haven't already done so, arrange to have
  1736. * "pcap_close_all()" called when we exit.
  1737. */
  1738. if (!pcap_do_addexit(p)) {
  1739. /*
  1740. * "atexit()" failed; don't create the
  1741. * interface, just give up.
  1742. */
  1743. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  1744. "atexit failed");
  1745. close(s);
  1746. status = PCAP_ERROR;
  1747. goto bad;
  1748. }
  1749. /*
  1750. * Create the interface.
  1751. */
  1752. strlcpy(ifr.ifr_name, p->opt.device, sizeof(ifr.ifr_name));
  1753. if (ioctl(s, SIOCIFCREATE2, &ifr) < 0) {
  1754. if (errno == EINVAL) {
  1755. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  1756. "Invalid USB bus interface %s",
  1757. p->opt.device);
  1758. } else {
  1759. pcap_fmt_errmsg_for_errno(p->errbuf,
  1760. PCAP_ERRBUF_SIZE, errno,
  1761. "Can't create interface for %s",
  1762. p->opt.device);
  1763. }
  1764. close(s);
  1765. status = PCAP_ERROR;
  1766. goto bad;
  1767. }
  1768. /*
  1769. * Make sure we clean this up when we close.
  1770. */
  1771. pb->must_do_on_close |= MUST_DESTROY_USBUS;
  1772. /*
  1773. * Add this to the list of pcaps to close when we exit.
  1774. */
  1775. pcap_add_to_pcaps_to_close(p);
  1776. }
  1777. }
  1778. #endif /* defined(__FreeBSD__) && defined(SIOCIFCREATE2) */
  1779. #ifdef HAVE_ZEROCOPY_BPF
  1780. /*
  1781. * If the BPF extension to set buffer mode is present, try setting
  1782. * the mode to zero-copy. If that fails, use regular buffering. If
  1783. * it succeeds but other setup fails, return an error to the user.
  1784. */
  1785. bufmode = BPF_BUFMODE_ZBUF;
  1786. if (ioctl(fd, BIOCSETBUFMODE, (caddr_t)&bufmode) == 0) {
  1787. /*
  1788. * We have zerocopy BPF; use it.
  1789. */
  1790. pb->zerocopy = 1;
  1791. /*
  1792. * How to pick a buffer size: first, query the maximum buffer
  1793. * size supported by zero-copy. This also lets us quickly
  1794. * determine whether the kernel generally supports zero-copy.
  1795. * Then, if a buffer size was specified, use that, otherwise
  1796. * query the default buffer size, which reflects kernel
  1797. * policy for a desired default. Round to the nearest page
  1798. * size.
  1799. */
  1800. if (ioctl(fd, BIOCGETZMAX, (caddr_t)&zbufmax) < 0) {
  1801. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  1802. errno, "BIOCGETZMAX");
  1803. status = PCAP_ERROR;
  1804. goto bad;
  1805. }
  1806. if (p->opt.buffer_size != 0) {
  1807. /*
  1808. * A buffer size was explicitly specified; use it.
  1809. */
  1810. v = p->opt.buffer_size;
  1811. } else {
  1812. if ((ioctl(fd, BIOCGBLEN, (caddr_t)&v) < 0) ||
  1813. v < DEFAULT_BUFSIZE)
  1814. v = DEFAULT_BUFSIZE;
  1815. }
  1816. #ifndef roundup
  1817. #define roundup(x, y) ((((x)+((y)-1))/(y))*(y)) /* to any y */
  1818. #endif
  1819. pb->zbufsize = roundup(v, getpagesize());
  1820. if (pb->zbufsize > zbufmax)
  1821. pb->zbufsize = zbufmax;
  1822. pb->zbuf1 = mmap(NULL, pb->zbufsize, PROT_READ | PROT_WRITE,
  1823. MAP_ANON, -1, 0);
  1824. pb->zbuf2 = mmap(NULL, pb->zbufsize, PROT_READ | PROT_WRITE,
  1825. MAP_ANON, -1, 0);
  1826. if (pb->zbuf1 == MAP_FAILED || pb->zbuf2 == MAP_FAILED) {
  1827. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  1828. errno, "mmap");
  1829. status = PCAP_ERROR;
  1830. goto bad;
  1831. }
  1832. memset(&bz, 0, sizeof(bz)); /* bzero() deprecated, replaced with memset() */
  1833. bz.bz_bufa = pb->zbuf1;
  1834. bz.bz_bufb = pb->zbuf2;
  1835. bz.bz_buflen = pb->zbufsize;
  1836. if (ioctl(fd, BIOCSETZBUF, (caddr_t)&bz) < 0) {
  1837. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  1838. errno, "BIOCSETZBUF");
  1839. status = PCAP_ERROR;
  1840. goto bad;
  1841. }
  1842. (void)strncpy(ifrname, p->opt.device, ifnamsiz);
  1843. if (ioctl(fd, BIOCSETIF, (caddr_t)&ifr) < 0) {
  1844. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  1845. errno, "BIOCSETIF: %s", p->opt.device);
  1846. status = PCAP_ERROR;
  1847. goto bad;
  1848. }
  1849. v = pb->zbufsize - sizeof(struct bpf_zbuf_header);
  1850. } else
  1851. #endif
  1852. {
  1853. /*
  1854. * We don't have zerocopy BPF.
  1855. * Set the buffer size.
  1856. */
  1857. if (p->opt.buffer_size != 0) {
  1858. /*
  1859. * A buffer size was explicitly specified; use it.
  1860. */
  1861. if (ioctl(fd, BIOCSBLEN,
  1862. (caddr_t)&p->opt.buffer_size) < 0) {
  1863. pcap_fmt_errmsg_for_errno(p->errbuf,
  1864. PCAP_ERRBUF_SIZE, errno,
  1865. "BIOCSBLEN: %s", p->opt.device);
  1866. status = PCAP_ERROR;
  1867. goto bad;
  1868. }
  1869. /*
  1870. * Now bind to the device.
  1871. */
  1872. (void)strncpy(ifrname, p->opt.device, ifnamsiz);
  1873. #ifdef BIOCSETLIF
  1874. if (ioctl(fd, BIOCSETLIF, (caddr_t)&ifr) < 0)
  1875. #else
  1876. if (ioctl(fd, BIOCSETIF, (caddr_t)&ifr) < 0)
  1877. #endif
  1878. {
  1879. status = check_setif_failure(p, errno);
  1880. goto bad;
  1881. }
  1882. } else {
  1883. /*
  1884. * No buffer size was explicitly specified.
  1885. *
  1886. * Try finding a good size for the buffer;
  1887. * DEFAULT_BUFSIZE may be too big, so keep
  1888. * cutting it in half until we find a size
  1889. * that works, or run out of sizes to try.
  1890. * If the default is larger, don't make it smaller.
  1891. */
  1892. if ((ioctl(fd, BIOCGBLEN, (caddr_t)&v) < 0) ||
  1893. v < DEFAULT_BUFSIZE)
  1894. v = DEFAULT_BUFSIZE;
  1895. for ( ; v != 0; v >>= 1) {
  1896. /*
  1897. * Ignore the return value - this is because the
  1898. * call fails on BPF systems that don't have
  1899. * kernel malloc. And if the call fails, it's
  1900. * no big deal, we just continue to use the
  1901. * standard buffer size.
  1902. */
  1903. (void) ioctl(fd, BIOCSBLEN, (caddr_t)&v);
  1904. (void)strncpy(ifrname, p->opt.device, ifnamsiz);
  1905. #ifdef BIOCSETLIF
  1906. if (ioctl(fd, BIOCSETLIF, (caddr_t)&ifr) >= 0)
  1907. #else
  1908. if (ioctl(fd, BIOCSETIF, (caddr_t)&ifr) >= 0)
  1909. #endif
  1910. break; /* that size worked; we're done */
  1911. if (errno != ENOBUFS) {
  1912. status = check_setif_failure(p, errno);
  1913. goto bad;
  1914. }
  1915. }
  1916. if (v == 0) {
  1917. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
  1918. "BIOCSBLEN: %s: No buffer size worked",
  1919. p->opt.device);
  1920. status = PCAP_ERROR;
  1921. goto bad;
  1922. }
  1923. }
  1924. }
  1925. /* Get the data link layer type. */
  1926. if (ioctl(fd, BIOCGDLT, (caddr_t)&v) < 0) {
  1927. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  1928. errno, "BIOCGDLT");
  1929. status = PCAP_ERROR;
  1930. goto bad;
  1931. }
  1932. #ifdef _AIX
  1933. /*
  1934. * AIX's BPF returns IFF_ types, not DLT_ types, in BIOCGDLT.
  1935. */
  1936. switch (v) {
  1937. case IFT_ETHER:
  1938. case IFT_ISO88023:
  1939. v = DLT_EN10MB;
  1940. break;
  1941. case IFT_FDDI:
  1942. v = DLT_FDDI;
  1943. break;
  1944. case IFT_ISO88025:
  1945. v = DLT_IEEE802;
  1946. break;
  1947. case IFT_LOOP:
  1948. v = DLT_NULL;
  1949. break;
  1950. default:
  1951. /*
  1952. * We don't know what to map this to yet.
  1953. */
  1954. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "unknown interface type %u",
  1955. v);
  1956. status = PCAP_ERROR;
  1957. goto bad;
  1958. }
  1959. #endif
  1960. #if _BSDI_VERSION - 0 >= 199510
  1961. /* The SLIP and PPP link layer header changed in BSD/OS 2.1 */
  1962. switch (v) {
  1963. case DLT_SLIP:
  1964. v = DLT_SLIP_BSDOS;
  1965. break;
  1966. case DLT_PPP:
  1967. v = DLT_PPP_BSDOS;
  1968. break;
  1969. case 11: /*DLT_FR*/
  1970. v = DLT_FRELAY;
  1971. break;
  1972. case 12: /*DLT_C_HDLC*/
  1973. v = DLT_CHDLC;
  1974. break;
  1975. }
  1976. #endif
  1977. #ifdef BIOCGDLTLIST
  1978. /*
  1979. * We know the default link type -- now determine all the DLTs
  1980. * this interface supports. If this fails with EINVAL, it's
  1981. * not fatal; we just don't get to use the feature later.
  1982. */
  1983. if (get_dlt_list(fd, v, &bdl, p->errbuf) == -1) {
  1984. status = PCAP_ERROR;
  1985. goto bad;
  1986. }
  1987. p->dlt_count = bdl.bfl_len;
  1988. p->dlt_list = bdl.bfl_list;
  1989. #ifdef __APPLE__
  1990. /*
  1991. * Monitor mode fun, continued.
  1992. *
  1993. * For 10.5 and, we're assuming, later releases, as noted above,
  1994. * 802.1 adapters that support monitor mode offer both DLT_EN10MB,
  1995. * DLT_IEEE802_11, and possibly some 802.11-plus-radio-information
  1996. * DLT_ value. Choosing one of the 802.11 DLT_ values will turn
  1997. * monitor mode on.
  1998. *
  1999. * Therefore, if the user asked for monitor mode, we filter out
  2000. * the DLT_EN10MB value, as you can't get that in monitor mode,
  2001. * and, if the user didn't ask for monitor mode, we filter out
  2002. * the 802.11 DLT_ values, because selecting those will turn
  2003. * monitor mode on. Then, for monitor mode, if an 802.11-plus-
  2004. * radio DLT_ value is offered, we try to select that, otherwise
  2005. * we try to select DLT_IEEE802_11.
  2006. */
  2007. if (have_osinfo) {
  2008. if (isdigit((unsigned)osinfo.release[0]) &&
  2009. (osinfo.release[0] == '9' ||
  2010. isdigit((unsigned)osinfo.release[1]))) {
  2011. /*
  2012. * 10.5 (Darwin 9.x), or later.
  2013. */
  2014. new_dlt = find_802_11(&bdl);
  2015. if (new_dlt != -1) {
  2016. /*
  2017. * We have at least one 802.11 DLT_ value,
  2018. * so this is an 802.11 interface.
  2019. * new_dlt is the best of the 802.11
  2020. * DLT_ values in the list.
  2021. */
  2022. if (p->opt.rfmon) {
  2023. /*
  2024. * Our caller wants monitor mode.
  2025. * Purge DLT_EN10MB from the list
  2026. * of link-layer types, as selecting
  2027. * it will keep monitor mode off.
  2028. */
  2029. remove_en(p);
  2030. /*
  2031. * If the new mode we want isn't
  2032. * the default mode, attempt to
  2033. * select the new mode.
  2034. */
  2035. if ((u_int)new_dlt != v) {
  2036. if (ioctl(p->fd, BIOCSDLT,
  2037. &new_dlt) != -1) {
  2038. /*
  2039. * We succeeded;
  2040. * make this the
  2041. * new DLT_ value.
  2042. */
  2043. v = new_dlt;
  2044. }
  2045. }
  2046. } else {
  2047. /*
  2048. * Our caller doesn't want
  2049. * monitor mode. Unless this
  2050. * is being done by pcap_open_live(),
  2051. * purge the 802.11 link-layer types
  2052. * from the list, as selecting
  2053. * one of them will turn monitor
  2054. * mode on.
  2055. */
  2056. if (!p->oldstyle)
  2057. remove_802_11(p);
  2058. }
  2059. } else {
  2060. if (p->opt.rfmon) {
  2061. /*
  2062. * The caller requested monitor
  2063. * mode, but we have no 802.11
  2064. * link-layer types, so they
  2065. * can't have it.
  2066. */
  2067. status = PCAP_ERROR_RFMON_NOTSUP;
  2068. goto bad;
  2069. }
  2070. }
  2071. }
  2072. }
  2073. #elif defined(HAVE_BSD_IEEE80211)
  2074. /*
  2075. * *BSD with the new 802.11 ioctls.
  2076. * Do we want monitor mode?
  2077. */
  2078. if (p->opt.rfmon) {
  2079. /*
  2080. * Try to put the interface into monitor mode.
  2081. */
  2082. retv = monitor_mode(p, 1);
  2083. if (retv != 0) {
  2084. /*
  2085. * We failed.
  2086. */
  2087. status = retv;
  2088. goto bad;
  2089. }
  2090. /*
  2091. * We're in monitor mode.
  2092. * Try to find the best 802.11 DLT_ value and, if we
  2093. * succeed, try to switch to that mode if we're not
  2094. * already in that mode.
  2095. */
  2096. new_dlt = find_802_11(&bdl);
  2097. if (new_dlt != -1) {
  2098. /*
  2099. * We have at least one 802.11 DLT_ value.
  2100. * new_dlt is the best of the 802.11
  2101. * DLT_ values in the list.
  2102. *
  2103. * If the new mode we want isn't the default mode,
  2104. * attempt to select the new mode.
  2105. */
  2106. if ((u_int)new_dlt != v) {
  2107. if (ioctl(p->fd, BIOCSDLT, &new_dlt) != -1) {
  2108. /*
  2109. * We succeeded; make this the
  2110. * new DLT_ value.
  2111. */
  2112. v = new_dlt;
  2113. }
  2114. }
  2115. }
  2116. }
  2117. #endif /* various platforms */
  2118. #endif /* BIOCGDLTLIST */
  2119. /*
  2120. * If this is an Ethernet device, and we don't have a DLT_ list,
  2121. * give it a list with DLT_EN10MB and DLT_DOCSIS. (That'd give
  2122. * 802.11 interfaces DLT_DOCSIS, which isn't the right thing to
  2123. * do, but there's not much we can do about that without finding
  2124. * some other way of determining whether it's an Ethernet or 802.11
  2125. * device.)
  2126. */
  2127. if (v == DLT_EN10MB && p->dlt_count == 0) {
  2128. p->dlt_list = (u_int *) malloc(sizeof(u_int) * 2);
  2129. /*
  2130. * If that fails, just leave the list empty.
  2131. */
  2132. if (p->dlt_list != NULL) {
  2133. p->dlt_list[0] = DLT_EN10MB;
  2134. p->dlt_list[1] = DLT_DOCSIS;
  2135. p->dlt_count = 2;
  2136. }
  2137. }
  2138. #ifdef PCAP_FDDIPAD
  2139. if (v == DLT_FDDI)
  2140. p->fddipad = PCAP_FDDIPAD;
  2141. else
  2142. #endif
  2143. p->fddipad = 0;
  2144. p->linktype = v;
  2145. #if defined(BIOCGHDRCMPLT) && defined(BIOCSHDRCMPLT)
  2146. /*
  2147. * Do a BIOCSHDRCMPLT, if defined, to turn that flag on, so
  2148. * the link-layer source address isn't forcibly overwritten.
  2149. * (Should we ignore errors? Should we do this only if
  2150. * we're open for writing?)
  2151. *
  2152. * XXX - I seem to remember some packet-sending bug in some
  2153. * BSDs - check CVS log for "bpf.c"?
  2154. */
  2155. if (ioctl(fd, BIOCSHDRCMPLT, &spoof_eth_src) == -1) {
  2156. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  2157. errno, "BIOCSHDRCMPLT");
  2158. status = PCAP_ERROR;
  2159. goto bad;
  2160. }
  2161. #endif
  2162. /* set timeout */
  2163. #ifdef HAVE_ZEROCOPY_BPF
  2164. /*
  2165. * In zero-copy mode, we just use the timeout in select().
  2166. * XXX - what if we're in non-blocking mode and the *application*
  2167. * is using select() or poll() or kqueues or....?
  2168. */
  2169. if (p->opt.timeout && !pb->zerocopy) {
  2170. #else
  2171. if (p->opt.timeout) {
  2172. #endif
  2173. /*
  2174. * XXX - is this seconds/nanoseconds in AIX?
  2175. * (Treating it as such doesn't fix the timeout
  2176. * problem described below.)
  2177. *
  2178. * XXX - Mac OS X 10.6 mishandles BIOCSRTIMEOUT in
  2179. * 64-bit userland - it takes, as an argument, a
  2180. * "struct BPF_TIMEVAL", which has 32-bit tv_sec
  2181. * and tv_usec, rather than a "struct timeval".
  2182. *
  2183. * If this platform defines "struct BPF_TIMEVAL",
  2184. * we check whether the structure size in BIOCSRTIMEOUT
  2185. * is that of a "struct timeval" and, if not, we use
  2186. * a "struct BPF_TIMEVAL" rather than a "struct timeval".
  2187. * (That way, if the bug is fixed in a future release,
  2188. * we will still do the right thing.)
  2189. */
  2190. struct timeval to;
  2191. #ifdef HAVE_STRUCT_BPF_TIMEVAL
  2192. struct BPF_TIMEVAL bpf_to;
  2193. if (IOCPARM_LEN(BIOCSRTIMEOUT) != sizeof(struct timeval)) {
  2194. bpf_to.tv_sec = p->opt.timeout / 1000;
  2195. bpf_to.tv_usec = (p->opt.timeout * 1000) % 1000000;
  2196. if (ioctl(p->fd, BIOCSRTIMEOUT, (caddr_t)&bpf_to) < 0) {
  2197. pcap_fmt_errmsg_for_errno(p->errbuf,
  2198. errno, PCAP_ERRBUF_SIZE, "BIOCSRTIMEOUT");
  2199. status = PCAP_ERROR;
  2200. goto bad;
  2201. }
  2202. } else {
  2203. #endif
  2204. to.tv_sec = p->opt.timeout / 1000;
  2205. to.tv_usec = (p->opt.timeout * 1000) % 1000000;
  2206. if (ioctl(p->fd, BIOCSRTIMEOUT, (caddr_t)&to) < 0) {
  2207. pcap_fmt_errmsg_for_errno(p->errbuf,
  2208. errno, PCAP_ERRBUF_SIZE, "BIOCSRTIMEOUT");
  2209. status = PCAP_ERROR;
  2210. goto bad;
  2211. }
  2212. #ifdef HAVE_STRUCT_BPF_TIMEVAL
  2213. }
  2214. #endif
  2215. }
  2216. #ifdef BIOCIMMEDIATE
  2217. /*
  2218. * Darren Reed notes that
  2219. *
  2220. * On AIX (4.2 at least), if BIOCIMMEDIATE is not set, the
  2221. * timeout appears to be ignored and it waits until the buffer
  2222. * is filled before returning. The result of not having it
  2223. * set is almost worse than useless if your BPF filter
  2224. * is reducing things to only a few packets (i.e. one every
  2225. * second or so).
  2226. *
  2227. * so we always turn BIOCIMMEDIATE mode on if this is AIX.
  2228. *
  2229. * For other platforms, we don't turn immediate mode on by default,
  2230. * as that would mean we get woken up for every packet, which
  2231. * probably isn't what you want for a packet sniffer.
  2232. *
  2233. * We set immediate mode if the caller requested it by calling
  2234. * pcap_set_immediate() before calling pcap_activate().
  2235. */
  2236. #ifndef _AIX
  2237. if (p->opt.immediate) {
  2238. #endif /* _AIX */
  2239. v = 1;
  2240. if (ioctl(p->fd, BIOCIMMEDIATE, &v) < 0) {
  2241. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  2242. errno, "BIOCIMMEDIATE");
  2243. status = PCAP_ERROR;
  2244. goto bad;
  2245. }
  2246. #ifndef _AIX
  2247. }
  2248. #endif /* _AIX */
  2249. #else /* BIOCIMMEDIATE */
  2250. if (p->opt.immediate) {
  2251. /*
  2252. * We don't support immediate mode. Fail.
  2253. */
  2254. pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "Immediate mode not supported");
  2255. status = PCAP_ERROR;
  2256. goto bad;
  2257. }
  2258. #endif /* BIOCIMMEDIATE */
  2259. if (p->opt.promisc) {
  2260. /* set promiscuous mode, just warn if it fails */
  2261. if (ioctl(p->fd, BIOCPROMISC, NULL) < 0) {
  2262. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  2263. errno, "BIOCPROMISC");
  2264. status = PCAP_WARNING_PROMISC_NOTSUP;
  2265. }
  2266. }
  2267. #ifdef BIOCSTSTAMP
  2268. v = BPF_T_BINTIME;
  2269. if (ioctl(p->fd, BIOCSTSTAMP, &v) < 0) {
  2270. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  2271. errno, "BIOCSTSTAMP");
  2272. status = PCAP_ERROR;
  2273. goto bad;
  2274. }
  2275. #endif /* BIOCSTSTAMP */
  2276. if (ioctl(fd, BIOCGBLEN, (caddr_t)&v) < 0) {
  2277. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  2278. errno, "BIOCGBLEN");
  2279. status = PCAP_ERROR;
  2280. goto bad;
  2281. }
  2282. p->bufsize = v;
  2283. #ifdef HAVE_ZEROCOPY_BPF
  2284. if (!pb->zerocopy) {
  2285. #endif
  2286. p->buffer = malloc(p->bufsize);
  2287. if (p->buffer == NULL) {
  2288. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  2289. errno, "malloc");
  2290. status = PCAP_ERROR;
  2291. goto bad;
  2292. }
  2293. #ifdef _AIX
  2294. /* For some strange reason this seems to prevent the EFAULT
  2295. * problems we have experienced from AIX BPF. */
  2296. memset(p->buffer, 0x0, p->bufsize);
  2297. #endif
  2298. #ifdef HAVE_ZEROCOPY_BPF
  2299. }
  2300. #endif
  2301. /*
  2302. * If there's no filter program installed, there's
  2303. * no indication to the kernel of what the snapshot
  2304. * length should be, so no snapshotting is done.
  2305. *
  2306. * Therefore, when we open the device, we install
  2307. * an "accept everything" filter with the specified
  2308. * snapshot length.
  2309. */
  2310. total_insn.code = (u_short)(BPF_RET | BPF_K);
  2311. total_insn.jt = 0;
  2312. total_insn.jf = 0;
  2313. total_insn.k = p->snapshot;
  2314. total_prog.bf_len = 1;
  2315. total_prog.bf_insns = &total_insn;
  2316. if (ioctl(p->fd, BIOCSETF, (caddr_t)&total_prog) < 0) {
  2317. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  2318. errno, "BIOCSETF");
  2319. status = PCAP_ERROR;
  2320. goto bad;
  2321. }
  2322. /*
  2323. * On most BPF platforms, either you can do a "select()" or
  2324. * "poll()" on a BPF file descriptor and it works correctly,
  2325. * or you can do it and it will return "readable" if the
  2326. * hold buffer is full but not if the timeout expires *and*
  2327. * a non-blocking read will, if the hold buffer is empty
  2328. * but the store buffer isn't empty, rotate the buffers
  2329. * and return what packets are available.
  2330. *
  2331. * In the latter case, the fact that a non-blocking read
  2332. * will give you the available packets means you can work
  2333. * around the failure of "select()" and "poll()" to wake up
  2334. * and return "readable" when the timeout expires by using
  2335. * the timeout as the "select()" or "poll()" timeout, putting
  2336. * the BPF descriptor into non-blocking mode, and read from
  2337. * it regardless of whether "select()" reports it as readable
  2338. * or not.
  2339. *
  2340. * However, in FreeBSD 4.3 and 4.4, "select()" and "poll()"
  2341. * won't wake up and return "readable" if the timer expires
  2342. * and non-blocking reads return EWOULDBLOCK if the hold
  2343. * buffer is empty, even if the store buffer is non-empty.
  2344. *
  2345. * This means the workaround in question won't work.
  2346. *
  2347. * Therefore, on FreeBSD 4.3 and 4.4, we set "p->selectable_fd"
  2348. * to -1, which means "sorry, you can't use 'select()' or 'poll()'
  2349. * here". On all other BPF platforms, we set it to the FD for
  2350. * the BPF device; in NetBSD, OpenBSD, and Darwin, a non-blocking
  2351. * read will, if the hold buffer is empty and the store buffer
  2352. * isn't empty, rotate the buffers and return what packets are
  2353. * there (and in sufficiently recent versions of OpenBSD
  2354. * "select()" and "poll()" should work correctly).
  2355. *
  2356. * XXX - what about AIX?
  2357. */
  2358. p->selectable_fd = p->fd; /* assume select() works until we know otherwise */
  2359. if (have_osinfo) {
  2360. /*
  2361. * We can check what OS this is.
  2362. */
  2363. if (strcmp(osinfo.sysname, "FreeBSD") == 0) {
  2364. if (strncmp(osinfo.release, "4.3-", 4) == 0 ||
  2365. strncmp(osinfo.release, "4.4-", 4) == 0)
  2366. p->selectable_fd = -1;
  2367. }
  2368. }
  2369. p->read_op = pcap_read_bpf;
  2370. p->inject_op = pcap_inject_bpf;
  2371. p->setfilter_op = pcap_setfilter_bpf;
  2372. p->setdirection_op = pcap_setdirection_bpf;
  2373. p->set_datalink_op = pcap_set_datalink_bpf;
  2374. p->getnonblock_op = pcap_getnonblock_bpf;
  2375. p->setnonblock_op = pcap_setnonblock_bpf;
  2376. p->stats_op = pcap_stats_bpf;
  2377. p->cleanup_op = pcap_cleanup_bpf;
  2378. return (status);
  2379. bad:
  2380. pcap_cleanup_bpf(p);
  2381. return (status);
  2382. }
  2383. /*
  2384. * Not all interfaces can be bound to by BPF, so try to bind to
  2385. * the specified interface; return 0 if we fail with
  2386. * PCAP_ERROR_NO_SUCH_DEVICE (which means we got an ENXIO when we tried
  2387. * to bind, which means this interface isn't in the list of interfaces
  2388. * attached to BPF) and 1 otherwise.
  2389. */
  2390. static int
  2391. check_bpf_bindable(const char *name)
  2392. {
  2393. int fd;
  2394. char errbuf[PCAP_ERRBUF_SIZE];
  2395. /*
  2396. * On macOS, we don't do this check if the device name begins
  2397. * with "wlt"; at least some versions of macOS (actually, it
  2398. * was called "Mac OS X" then...) offer monitor mode capturing
  2399. * by having a separate "monitor mode" device for each wireless
  2400. * adapter, rather than by implementing the ioctls that
  2401. * {Free,Net,Open,DragonFly}BSD provide. Opening that device
  2402. * puts the adapter into monitor mode, which, at least for
  2403. * some adapters, causes them to deassociate from the network
  2404. * with which they're associated.
  2405. *
  2406. * Instead, we try to open the corresponding "en" device (so
  2407. * that we don't end up with, for users without sufficient
  2408. * privilege to open capture devices, a list of adapters that
  2409. * only includes the wlt devices).
  2410. */
  2411. #ifdef __APPLE__
  2412. if (strncmp(name, "wlt", 3) == 0) {
  2413. char *en_name;
  2414. size_t en_name_len;
  2415. /*
  2416. * Try to allocate a buffer for the "en"
  2417. * device's name.
  2418. */
  2419. en_name_len = strlen(name) - 1;
  2420. en_name = malloc(en_name_len + 1);
  2421. if (en_name == NULL) {
  2422. pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
  2423. errno, "malloc");
  2424. return (-1);
  2425. }
  2426. strcpy(en_name, "en");
  2427. strcat(en_name, name + 3);
  2428. fd = bpf_open_and_bind(en_name, errbuf);
  2429. free(en_name);
  2430. } else
  2431. #endif /* __APPLE */
  2432. fd = bpf_open_and_bind(name, errbuf);
  2433. if (fd < 0) {
  2434. /*
  2435. * Error - was it PCAP_ERROR_NO_SUCH_DEVICE?
  2436. */
  2437. if (fd == PCAP_ERROR_NO_SUCH_DEVICE) {
  2438. /*
  2439. * Yes, so we can't bind to this because it's
  2440. * not something supported by BPF.
  2441. */
  2442. return (0);
  2443. }
  2444. /*
  2445. * No, so we don't know whether it's supported or not;
  2446. * say it is, so that the user can at least try to
  2447. * open it and report the error (which is probably
  2448. * "you don't have permission to open BPF devices";
  2449. * reporting those interfaces means users will ask
  2450. * "why am I getting a permissions error when I try
  2451. * to capture" rather than "why am I not seeing any
  2452. * interfaces", making the underlying problem clearer).
  2453. */
  2454. return (1);
  2455. }
  2456. /*
  2457. * Success.
  2458. */
  2459. close(fd);
  2460. return (1);
  2461. }
  2462. #if defined(__FreeBSD__) && defined(SIOCIFCREATE2)
  2463. static int
  2464. get_usb_if_flags(const char *name _U_, bpf_u_int32 *flags _U_, char *errbuf _U_)
  2465. {
  2466. /*
  2467. * XXX - if there's a way to determine whether there's something
  2468. * plugged into a given USB bus, use that to determine whether
  2469. * this device is "connected" or not.
  2470. */
  2471. return (0);
  2472. }
  2473. static int
  2474. finddevs_usb(pcap_if_list_t *devlistp, char *errbuf)
  2475. {
  2476. DIR *usbdir;
  2477. struct dirent *usbitem;
  2478. size_t name_max;
  2479. char *name;
  2480. /*
  2481. * We might have USB sniffing support, so try looking for USB
  2482. * interfaces.
  2483. *
  2484. * We want to report a usbusN device for each USB bus, but
  2485. * usbusN interfaces might, or might not, exist for them -
  2486. * we create one if there isn't already one.
  2487. *
  2488. * So, instead, we look in /dev/usb for all buses and create
  2489. * a "usbusN" device for each one.
  2490. */
  2491. usbdir = opendir("/dev/usb");
  2492. if (usbdir == NULL) {
  2493. /*
  2494. * Just punt.
  2495. */
  2496. return (0);
  2497. }
  2498. /*
  2499. * Leave enough room for a 32-bit (10-digit) bus number.
  2500. * Yes, that's overkill, but we won't be using
  2501. * the buffer very long.
  2502. */
  2503. name_max = USBUS_PREFIX_LEN + 10 + 1;
  2504. name = malloc(name_max);
  2505. if (name == NULL) {
  2506. closedir(usbdir);
  2507. return (0);
  2508. }
  2509. while ((usbitem = readdir(usbdir)) != NULL) {
  2510. char *p;
  2511. size_t busnumlen;
  2512. if (strcmp(usbitem->d_name, ".") == 0 ||
  2513. strcmp(usbitem->d_name, "..") == 0) {
  2514. /*
  2515. * Ignore these.
  2516. */
  2517. continue;
  2518. }
  2519. p = strchr(usbitem->d_name, '.');
  2520. if (p == NULL)
  2521. continue;
  2522. busnumlen = p - usbitem->d_name;
  2523. memcpy(name, usbus_prefix, USBUS_PREFIX_LEN);
  2524. memcpy(name + USBUS_PREFIX_LEN, usbitem->d_name, busnumlen);
  2525. *(name + USBUS_PREFIX_LEN + busnumlen) = '\0';
  2526. /*
  2527. * There's an entry in this directory for every USB device,
  2528. * not for every bus; if there's more than one device on
  2529. * the bus, there'll be more than one entry for that bus,
  2530. * so we need to avoid adding multiple capture devices
  2531. * for each bus.
  2532. */
  2533. if (find_or_add_dev(devlistp, name, PCAP_IF_UP,
  2534. get_usb_if_flags, NULL, errbuf) == NULL) {
  2535. free(name);
  2536. closedir(usbdir);
  2537. return (PCAP_ERROR);
  2538. }
  2539. }
  2540. free(name);
  2541. closedir(usbdir);
  2542. return (0);
  2543. }
  2544. #endif
  2545. /*
  2546. * Get additional flags for a device, using SIOCGIFMEDIA.
  2547. */
  2548. #ifdef SIOCGIFMEDIA
  2549. static int
  2550. get_if_flags(const char *name, bpf_u_int32 *flags, char *errbuf)
  2551. {
  2552. int sock;
  2553. struct ifmediareq req;
  2554. sock = socket(AF_INET, SOCK_DGRAM, 0);
  2555. if (sock == -1) {
  2556. pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, errno,
  2557. "Can't create socket to get media information for %s",
  2558. name);
  2559. return (-1);
  2560. }
  2561. memset(&req, 0, sizeof(req));
  2562. strncpy(req.ifm_name, name, sizeof(req.ifm_name));
  2563. if (ioctl(sock, SIOCGIFMEDIA, &req) < 0) {
  2564. if (errno == EOPNOTSUPP || errno == EINVAL || errno == ENOTTY ||
  2565. errno == ENODEV) {
  2566. /*
  2567. * Not supported, so we can't provide any
  2568. * additional information. Assume that
  2569. * this means that "connected" vs.
  2570. * "disconnected" doesn't apply.
  2571. */
  2572. *flags |= PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE;
  2573. close(sock);
  2574. return (0);
  2575. }
  2576. pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, errno,
  2577. "SIOCGIFMEDIA on %s failed", name);
  2578. close(sock);
  2579. return (-1);
  2580. }
  2581. close(sock);
  2582. /*
  2583. * OK, what type of network is this?
  2584. */
  2585. switch (IFM_TYPE(req.ifm_active)) {
  2586. case IFM_IEEE80211:
  2587. /*
  2588. * Wireless.
  2589. */
  2590. *flags |= PCAP_IF_WIRELESS;
  2591. break;
  2592. }
  2593. /*
  2594. * Do we know whether it's connected?
  2595. */
  2596. if (req.ifm_status & IFM_AVALID) {
  2597. /*
  2598. * Yes.
  2599. */
  2600. if (req.ifm_status & IFM_ACTIVE) {
  2601. /*
  2602. * It's connected.
  2603. */
  2604. *flags |= PCAP_IF_CONNECTION_STATUS_CONNECTED;
  2605. } else {
  2606. /*
  2607. * It's disconnected.
  2608. */
  2609. *flags |= PCAP_IF_CONNECTION_STATUS_DISCONNECTED;
  2610. }
  2611. }
  2612. return (0);
  2613. }
  2614. #else
  2615. static int
  2616. get_if_flags(const char *name _U_, bpf_u_int32 *flags _U_, char *errbuf _U_)
  2617. {
  2618. /*
  2619. * Nothing we can do other than mark loopback devices as "the
  2620. * connected/disconnected status doesn't apply".
  2621. *
  2622. * XXX - on Solaris, can we do what the dladm command does,
  2623. * i.e. get a connected/disconnected indication from a kstat?
  2624. * (Note that you can also get the link speed, and possibly
  2625. * other information, from a kstat as well.)
  2626. */
  2627. if (*flags & PCAP_IF_LOOPBACK) {
  2628. /*
  2629. * Loopback devices aren't wireless, and "connected"/
  2630. * "disconnected" doesn't apply to them.
  2631. */
  2632. *flags |= PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE;
  2633. return (0);
  2634. }
  2635. return (0);
  2636. }
  2637. #endif
  2638. int
  2639. pcap_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf)
  2640. {
  2641. /*
  2642. * Get the list of regular interfaces first.
  2643. */
  2644. if (pcap_findalldevs_interfaces(devlistp, errbuf, check_bpf_bindable,
  2645. get_if_flags) == -1)
  2646. return (-1); /* failure */
  2647. #if defined(__FreeBSD__) && defined(SIOCIFCREATE2)
  2648. if (finddevs_usb(devlistp, errbuf) == -1)
  2649. return (-1);
  2650. #endif
  2651. return (0);
  2652. }
  2653. #ifdef HAVE_BSD_IEEE80211
  2654. static int
  2655. monitor_mode(pcap_t *p, int set)
  2656. {
  2657. struct pcap_bpf *pb = p->priv;
  2658. int sock;
  2659. struct ifmediareq req;
  2660. IFM_ULIST_TYPE *media_list;
  2661. int i;
  2662. int can_do;
  2663. struct ifreq ifr;
  2664. sock = socket(AF_INET, SOCK_DGRAM, 0);
  2665. if (sock == -1) {
  2666. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  2667. errno, "can't open socket");
  2668. return (PCAP_ERROR);
  2669. }
  2670. memset(&req, 0, sizeof req);
  2671. strncpy(req.ifm_name, p->opt.device, sizeof req.ifm_name);
  2672. /*
  2673. * Find out how many media types we have.
  2674. */
  2675. if (ioctl(sock, SIOCGIFMEDIA, &req) < 0) {
  2676. /*
  2677. * Can't get the media types.
  2678. */
  2679. switch (errno) {
  2680. case ENXIO:
  2681. /*
  2682. * There's no such device.
  2683. */
  2684. close(sock);
  2685. return (PCAP_ERROR_NO_SUCH_DEVICE);
  2686. case EINVAL:
  2687. /*
  2688. * Interface doesn't support SIOC{G,S}IFMEDIA.
  2689. */
  2690. close(sock);
  2691. return (PCAP_ERROR_RFMON_NOTSUP);
  2692. default:
  2693. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  2694. errno, "SIOCGIFMEDIA 1");
  2695. close(sock);
  2696. return (PCAP_ERROR);
  2697. }
  2698. }
  2699. if (req.ifm_count == 0) {
  2700. /*
  2701. * No media types.
  2702. */
  2703. close(sock);
  2704. return (PCAP_ERROR_RFMON_NOTSUP);
  2705. }
  2706. /*
  2707. * Allocate a buffer to hold all the media types, and
  2708. * get the media types.
  2709. */
  2710. media_list = malloc(req.ifm_count * sizeof(*media_list));
  2711. if (media_list == NULL) {
  2712. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  2713. errno, "malloc");
  2714. close(sock);
  2715. return (PCAP_ERROR);
  2716. }
  2717. req.ifm_ulist = media_list;
  2718. if (ioctl(sock, SIOCGIFMEDIA, &req) < 0) {
  2719. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  2720. errno, "SIOCGIFMEDIA");
  2721. free(media_list);
  2722. close(sock);
  2723. return (PCAP_ERROR);
  2724. }
  2725. /*
  2726. * Look for an 802.11 "automatic" media type.
  2727. * We assume that all 802.11 adapters have that media type,
  2728. * and that it will carry the monitor mode supported flag.
  2729. */
  2730. can_do = 0;
  2731. for (i = 0; i < req.ifm_count; i++) {
  2732. if (IFM_TYPE(media_list[i]) == IFM_IEEE80211
  2733. && IFM_SUBTYPE(media_list[i]) == IFM_AUTO) {
  2734. /* OK, does it do monitor mode? */
  2735. if (media_list[i] & IFM_IEEE80211_MONITOR) {
  2736. can_do = 1;
  2737. break;
  2738. }
  2739. }
  2740. }
  2741. free(media_list);
  2742. if (!can_do) {
  2743. /*
  2744. * This adapter doesn't support monitor mode.
  2745. */
  2746. close(sock);
  2747. return (PCAP_ERROR_RFMON_NOTSUP);
  2748. }
  2749. if (set) {
  2750. /*
  2751. * Don't just check whether we can enable monitor mode,
  2752. * do so, if it's not already enabled.
  2753. */
  2754. if ((req.ifm_current & IFM_IEEE80211_MONITOR) == 0) {
  2755. /*
  2756. * Monitor mode isn't currently on, so turn it on,
  2757. * and remember that we should turn it off when the
  2758. * pcap_t is closed.
  2759. */
  2760. /*
  2761. * If we haven't already done so, arrange to have
  2762. * "pcap_close_all()" called when we exit.
  2763. */
  2764. if (!pcap_do_addexit(p)) {
  2765. /*
  2766. * "atexit()" failed; don't put the interface
  2767. * in monitor mode, just give up.
  2768. */
  2769. close(sock);
  2770. return (PCAP_ERROR);
  2771. }
  2772. memset(&ifr, 0, sizeof(ifr));
  2773. (void)strncpy(ifr.ifr_name, p->opt.device,
  2774. sizeof(ifr.ifr_name));
  2775. ifr.ifr_media = req.ifm_current | IFM_IEEE80211_MONITOR;
  2776. if (ioctl(sock, SIOCSIFMEDIA, &ifr) == -1) {
  2777. pcap_fmt_errmsg_for_errno(p->errbuf,
  2778. PCAP_ERRBUF_SIZE, errno, "SIOCSIFMEDIA");
  2779. close(sock);
  2780. return (PCAP_ERROR);
  2781. }
  2782. pb->must_do_on_close |= MUST_CLEAR_RFMON;
  2783. /*
  2784. * Add this to the list of pcaps to close when we exit.
  2785. */
  2786. pcap_add_to_pcaps_to_close(p);
  2787. }
  2788. }
  2789. return (0);
  2790. }
  2791. #endif /* HAVE_BSD_IEEE80211 */
  2792. #if defined(BIOCGDLTLIST) && (defined(__APPLE__) || defined(HAVE_BSD_IEEE80211))
  2793. /*
  2794. * Check whether we have any 802.11 link-layer types; return the best
  2795. * of the 802.11 link-layer types if we find one, and return -1
  2796. * otherwise.
  2797. *
  2798. * DLT_IEEE802_11_RADIO, with the radiotap header, is considered the
  2799. * best 802.11 link-layer type; any of the other 802.11-plus-radio
  2800. * headers are second-best; 802.11 with no radio information is
  2801. * the least good.
  2802. */
  2803. static int
  2804. find_802_11(struct bpf_dltlist *bdlp)
  2805. {
  2806. int new_dlt;
  2807. u_int i;
  2808. /*
  2809. * Scan the list of DLT_ values, looking for 802.11 values,
  2810. * and, if we find any, choose the best of them.
  2811. */
  2812. new_dlt = -1;
  2813. for (i = 0; i < bdlp->bfl_len; i++) {
  2814. switch (bdlp->bfl_list[i]) {
  2815. case DLT_IEEE802_11:
  2816. /*
  2817. * 802.11, but no radio.
  2818. *
  2819. * Offer this, and select it as the new mode
  2820. * unless we've already found an 802.11
  2821. * header with radio information.
  2822. */
  2823. if (new_dlt == -1)
  2824. new_dlt = bdlp->bfl_list[i];
  2825. break;
  2826. case DLT_PRISM_HEADER:
  2827. case DLT_AIRONET_HEADER:
  2828. case DLT_IEEE802_11_RADIO_AVS:
  2829. /*
  2830. * 802.11 with radio, but not radiotap.
  2831. *
  2832. * Offer this, and select it as the new mode
  2833. * unless we've already found the radiotap DLT_.
  2834. */
  2835. if (new_dlt != DLT_IEEE802_11_RADIO)
  2836. new_dlt = bdlp->bfl_list[i];
  2837. break;
  2838. case DLT_IEEE802_11_RADIO:
  2839. /*
  2840. * 802.11 with radiotap.
  2841. *
  2842. * Offer this, and select it as the new mode.
  2843. */
  2844. new_dlt = bdlp->bfl_list[i];
  2845. break;
  2846. default:
  2847. /*
  2848. * Not 802.11.
  2849. */
  2850. break;
  2851. }
  2852. }
  2853. return (new_dlt);
  2854. }
  2855. #endif /* defined(BIOCGDLTLIST) && (defined(__APPLE__) || defined(HAVE_BSD_IEEE80211)) */
  2856. #if defined(__APPLE__) && defined(BIOCGDLTLIST)
  2857. /*
  2858. * Remove DLT_EN10MB from the list of DLT_ values, as we're in monitor mode,
  2859. * and DLT_EN10MB isn't supported in monitor mode.
  2860. */
  2861. static void
  2862. remove_en(pcap_t *p)
  2863. {
  2864. int i, j;
  2865. /*
  2866. * Scan the list of DLT_ values and discard DLT_EN10MB.
  2867. */
  2868. j = 0;
  2869. for (i = 0; i < p->dlt_count; i++) {
  2870. switch (p->dlt_list[i]) {
  2871. case DLT_EN10MB:
  2872. /*
  2873. * Don't offer this one.
  2874. */
  2875. continue;
  2876. default:
  2877. /*
  2878. * Just copy this mode over.
  2879. */
  2880. break;
  2881. }
  2882. /*
  2883. * Copy this DLT_ value to its new position.
  2884. */
  2885. p->dlt_list[j] = p->dlt_list[i];
  2886. j++;
  2887. }
  2888. /*
  2889. * Set the DLT_ count to the number of entries we copied.
  2890. */
  2891. p->dlt_count = j;
  2892. }
  2893. /*
  2894. * Remove 802.11 link-layer types from the list of DLT_ values, as
  2895. * we're not in monitor mode, and those DLT_ values will switch us
  2896. * to monitor mode.
  2897. */
  2898. static void
  2899. remove_802_11(pcap_t *p)
  2900. {
  2901. int i, j;
  2902. /*
  2903. * Scan the list of DLT_ values and discard 802.11 values.
  2904. */
  2905. j = 0;
  2906. for (i = 0; i < p->dlt_count; i++) {
  2907. switch (p->dlt_list[i]) {
  2908. case DLT_IEEE802_11:
  2909. case DLT_PRISM_HEADER:
  2910. case DLT_AIRONET_HEADER:
  2911. case DLT_IEEE802_11_RADIO:
  2912. case DLT_IEEE802_11_RADIO_AVS:
  2913. /*
  2914. * 802.11. Don't offer this one.
  2915. */
  2916. continue;
  2917. default:
  2918. /*
  2919. * Just copy this mode over.
  2920. */
  2921. break;
  2922. }
  2923. /*
  2924. * Copy this DLT_ value to its new position.
  2925. */
  2926. p->dlt_list[j] = p->dlt_list[i];
  2927. j++;
  2928. }
  2929. /*
  2930. * Set the DLT_ count to the number of entries we copied.
  2931. */
  2932. p->dlt_count = j;
  2933. }
  2934. #endif /* defined(__APPLE__) && defined(BIOCGDLTLIST) */
  2935. static int
  2936. pcap_setfilter_bpf(pcap_t *p, struct bpf_program *fp)
  2937. {
  2938. struct pcap_bpf *pb = p->priv;
  2939. /*
  2940. * Free any user-mode filter we might happen to have installed.
  2941. */
  2942. pcap_freecode(&p->fcode);
  2943. /*
  2944. * Try to install the kernel filter.
  2945. */
  2946. if (ioctl(p->fd, BIOCSETF, (caddr_t)fp) == 0) {
  2947. /*
  2948. * It worked.
  2949. */
  2950. pb->filtering_in_kernel = 1; /* filtering in the kernel */
  2951. /*
  2952. * Discard any previously-received packets, as they might
  2953. * have passed whatever filter was formerly in effect, but
  2954. * might not pass this filter (BIOCSETF discards packets
  2955. * buffered in the kernel, so you can lose packets in any
  2956. * case).
  2957. */
  2958. p->cc = 0;
  2959. return (0);
  2960. }
  2961. /*
  2962. * We failed.
  2963. *
  2964. * If it failed with EINVAL, that's probably because the program
  2965. * is invalid or too big. Validate it ourselves; if we like it
  2966. * (we currently allow backward branches, to support protochain),
  2967. * run it in userland. (There's no notion of "too big" for
  2968. * userland.)
  2969. *
  2970. * Otherwise, just give up.
  2971. * XXX - if the copy of the program into the kernel failed,
  2972. * we will get EINVAL rather than, say, EFAULT on at least
  2973. * some kernels.
  2974. */
  2975. if (errno != EINVAL) {
  2976. pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
  2977. errno, "BIOCSETF");
  2978. return (-1);
  2979. }
  2980. /*
  2981. * install_bpf_program() validates the program.
  2982. *
  2983. * XXX - what if we already have a filter in the kernel?
  2984. */
  2985. if (install_bpf_program(p, fp) < 0)
  2986. return (-1);
  2987. pb->filtering_in_kernel = 0; /* filtering in userland */
  2988. return (0);
  2989. }
  2990. /*
  2991. * Set direction flag: Which packets do we accept on a forwarding
  2992. * single device? IN, OUT or both?
  2993. */
  2994. static int
  2995. pcap_setdirection_bpf(pcap_t *p, pcap_direction_t d)
  2996. {
  2997. #if defined(BIOCSDIRECTION)
  2998. u_int direction;
  2999. direction = (d == PCAP_D_IN) ? BPF_D_IN :
  3000. ((d == PCAP_D_OUT) ? BPF_D_OUT : BPF_D_INOUT);
  3001. if (ioctl(p->fd, BIOCSDIRECTION, &direction) == -1) {
  3002. pcap_fmt_errmsg_for_errno(p->errbuf, sizeof(p->errbuf),
  3003. errno, "Cannot set direction to %s",
  3004. (d == PCAP_D_IN) ? "PCAP_D_IN" :
  3005. ((d == PCAP_D_OUT) ? "PCAP_D_OUT" : "PCAP_D_INOUT"));
  3006. return (-1);
  3007. }
  3008. return (0);
  3009. #elif defined(BIOCSSEESENT)
  3010. u_int seesent;
  3011. /*
  3012. * We don't support PCAP_D_OUT.
  3013. */
  3014. if (d == PCAP_D_OUT) {
  3015. pcap_snprintf(p->errbuf, sizeof(p->errbuf),
  3016. "Setting direction to PCAP_D_OUT is not supported on BPF");
  3017. return -1;
  3018. }
  3019. seesent = (d == PCAP_D_INOUT);
  3020. if (ioctl(p->fd, BIOCSSEESENT, &seesent) == -1) {
  3021. pcap_fmt_errmsg_for_errno(p->errbuf, sizeof(p->errbuf),
  3022. errno, "Cannot set direction to %s",
  3023. (d == PCAP_D_INOUT) ? "PCAP_D_INOUT" : "PCAP_D_IN");
  3024. return (-1);
  3025. }
  3026. return (0);
  3027. #else
  3028. (void) pcap_snprintf(p->errbuf, sizeof(p->errbuf),
  3029. "This system doesn't support BIOCSSEESENT, so the direction can't be set");
  3030. return (-1);
  3031. #endif
  3032. }
  3033. static int
  3034. pcap_set_datalink_bpf(pcap_t *p, int dlt)
  3035. {
  3036. #ifdef BIOCSDLT
  3037. if (ioctl(p->fd, BIOCSDLT, &dlt) == -1) {
  3038. pcap_fmt_errmsg_for_errno(p->errbuf, sizeof(p->errbuf),
  3039. errno, "Cannot set DLT %d", dlt);
  3040. return (-1);
  3041. }
  3042. #endif
  3043. return (0);
  3044. }
  3045. /*
  3046. * Platform-specific information.
  3047. */
  3048. const char *
  3049. pcap_lib_version(void)
  3050. {
  3051. #ifdef HAVE_ZEROCOPY_BPF
  3052. return (PCAP_VERSION_STRING " (with zerocopy support)");
  3053. #else
  3054. return (PCAP_VERSION_STRING);
  3055. #endif
  3056. }