gnunet-helper-transport-wlan.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148
  1. /*
  2. This file is part of GNUnet.
  3. (C) 2010, 2011, 2012 Christian Grothoff (and other contributing authors)
  4. Copyright (c) 2007, 2008, Andy Green <andy@warmcat.com>
  5. Copyright (C) 2009 Thomas d'Otreppe
  6. GNUnet is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published
  8. by the Free Software Foundation; either version 3, or (at your
  9. option) any later version.
  10. GNUnet is distributed in the hope that it will be useful, but
  11. WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with GNUnet; see the file COPYING. If not, write to the
  16. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  17. Boston, MA 02111-1307, USA.
  18. */
  19. /**
  20. * @file src/transport/gnunet-helper-transport-wlan.c
  21. * @brief mediator between the wlan interface and gnunet; must run as root (SUID will do)
  22. * This code will work under GNU/Linux only.
  23. * @author David Brodski
  24. * @author Christian Grothoff
  25. *
  26. * This program will allow receiving and sending traffic from the WLAN
  27. * interface. It will force traffic to be in 'ad-hoc' mode, use the
  28. * proper MAC address of the WLAN interface and use a GNUnet-specific
  29. * SSID (and a GNUnet-specific SNAP header). It only takes a single
  30. * argument, which is the name of the WLAN interface to use. The
  31. * program detects if the interface is not a WLAN interface and exits
  32. * with an error in that case.
  33. *
  34. * Once initialized, the program will first send a 'struct
  35. * GNUNET_TRANSPORT_WLAN_HelperControlMessage' to 'stdout'. That
  36. * message contains the MAC address of the WLAN interface. It will
  37. * then read messages from the WLAN interface and send them together
  38. * with performance information as 'struct
  39. * GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage' messages to 'stdout'.
  40. * Furthermore, it will read a stream of messages from 'stdin' that
  41. * have the format from 'struct
  42. * GNUNET_TRANSPORT_WLAN_RadiotapSendMessage'. Those messages will
  43. * then be sent via the WLAN interface; however, the sender MAC
  44. * address will be forced to be the correct address from our WLAN
  45. * card. If 'stdin' closes, receiving from the WLAN interface will
  46. * continue. If 'stdout' causes a SIGPIPE, the process dies from the
  47. * signal. Errors cause an error message to be reported to 'stderr',
  48. * in most cases the process also exits (with status code '1'). The
  49. * program never terminates normally; it is safe to kill the
  50. * process with SIGTERM or SIGKILL at any time.
  51. *
  52. * Since it uses RAW sockets, the binary must be installed SUID or run
  53. * as 'root'. In order to keep the security risk of the resulting
  54. * SUID binary minimal, the program ONLY opens the RAW socket with
  55. * root privileges, then drops them and only then starts to process
  56. * command line arguments. The code also does not link against any
  57. * shared libraries (except libc) and is strictly minimal (except for
  58. * checking for errors). The following list of people have reviewed
  59. * this code and considered it safe since the last modification (if
  60. * you reviewed it, please have your name added to the list):
  61. *
  62. * - Christian Grothoff (Apr 3rd 2012)
  63. */
  64. /*-
  65. * we use our local copy of ieee80211_radiotap.h
  66. *
  67. * - since we can't support extensions we don't understand
  68. * - since linux does not include it in userspace headers
  69. *
  70. * Portions of this code were taken from the ieee80211_radiotap.h header,
  71. * which is
  72. *
  73. * Copyright (c) 2003, 2004 David Young. All rights reserved.
  74. *
  75. * Redistribution and use in source and binary forms, with or without
  76. * modification, are permitted provided that the following conditions
  77. * are met:
  78. * 1. Redistributions of source code must retain the above copyright
  79. * notice, this list of conditions and the following disclaimer.
  80. * 2. Redistributions in binary form must reproduce the above copyright
  81. * notice, this list of conditions and the following disclaimer in the
  82. * documentation and/or other materials provided with the distribution.
  83. * 3. The name of David Young may not be used to endorse or promote
  84. * products derived from this software without specific prior
  85. * written permission.
  86. *
  87. * THIS SOFTWARE IS PROVIDED BY DAVID YOUNG ``AS IS'' AND ANY
  88. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  89. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  90. * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DAVID
  91. * YOUNG BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  92. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  93. * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  94. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  95. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  96. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  97. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  98. * OF SUCH DAMAGE.
  99. */
  100. /*
  101. * Modifications to fit into the linux IEEE 802.11 stack,
  102. * Mike Kershaw (dragorn@kismetwireless.net)
  103. */
  104. /*
  105. * parts taken from aircrack-ng, parts changend.
  106. */
  107. #include "gnunet_config.h"
  108. #define SOCKTYPE int
  109. #define FDTYPE int
  110. #include <sys/socket.h>
  111. #include <sys/ioctl.h>
  112. #include <sys/types.h>
  113. #include <unistd.h>
  114. #include <sys/wait.h>
  115. #include <sys/time.h>
  116. #include <sys/stat.h>
  117. #include <netpacket/packet.h>
  118. #include <linux/if_ether.h>
  119. #include <linux/if.h>
  120. #include <linux/wireless.h>
  121. #include <netinet/in.h>
  122. #include <linux/if_tun.h>
  123. #include <stdio.h>
  124. #include <stdlib.h>
  125. #include <string.h>
  126. #include <stdarg.h>
  127. #include <fcntl.h>
  128. #include <errno.h>
  129. #include <dirent.h>
  130. #include <sys/param.h>
  131. #include <unistd.h>
  132. #include <stdint.h>
  133. #include "gnunet_protocols.h"
  134. #include "plugin_transport_wlan.h"
  135. /**
  136. * Packet format type for the messages we receive from
  137. * the kernel. This is for Ethernet 10Mbps format (no
  138. * performance information included).
  139. */
  140. #define ARPHRD_ETHER 1
  141. /**
  142. * Packet format type for the messages we receive from
  143. * the kernel. This is for plain messages (with no
  144. * performance information included).
  145. */
  146. #define ARPHRD_IEEE80211 801
  147. /**
  148. * Packet format type for the messages we receive from
  149. * the kernel. This is for the PRISM format.
  150. */
  151. #define ARPHRD_IEEE80211_PRISM 802
  152. /**
  153. * Packet format type for the messages we receive from
  154. * the kernel. This is for messages with a
  155. * 'struct Ieee80211RadiotapHeader' (see below).
  156. */
  157. #define ARPHRD_IEEE80211_FULL 803
  158. /**
  159. * Maximum size of a message allowed in either direction
  160. * (used for our receive and sent buffers).
  161. */
  162. #define MAXLINE 4096
  163. /* ********* structure of messages of type ARPHRD_IEEE80211_PRISM *********** */
  164. /**
  165. * Device name length in PRISM frames.
  166. * (In the kernel, this is "WLAN_DEVNAMELEN_MAX")
  167. */
  168. #define PRISM_DEVICE_NAME_LENGTH 16
  169. /**
  170. * Monitor Frame (indicator that we have a 'struct PrismHeader').
  171. */
  172. #define PRISM_MSGCODE_MONITOR 0x0041
  173. /**
  174. * Mac time element. In micro-seconds.
  175. * Drivers appear to use a 64bit counter to hold mactime internal
  176. * the then fill the prism header with the lower 32 bits
  177. */
  178. #define PRISM_DID_MACTIME 0x2041
  179. /**
  180. * Channel element
  181. */
  182. #define PRISM_DID_CHANNEL 0x3041
  183. /**
  184. * Signal element. Should be the signal strength in dbm, some people
  185. * suggest that instead "100 - (strength in dbm)" is used (to make this
  186. * a positive integer).
  187. */
  188. #define PRISM_DID_SIGNAL 0x6041
  189. /**
  190. * Noise element
  191. */
  192. #define PRISM_DID_NOISE 0x7041
  193. /**
  194. * Rate element, in units/multiples of 500Khz
  195. */
  196. #define PRISM_DID_RATE 0x8041
  197. /**
  198. * Value is set (supplied)
  199. */
  200. #define PRISM_STATUS_OK 0
  201. /**
  202. * Value not supplied.
  203. */
  204. #define PRISM_STATUS_NO_VALUE 1
  205. /**
  206. * Values in the 'struct PrismHeader'. All in host byte order (!).
  207. */
  208. struct PrismValue
  209. {
  210. /**
  211. * This has a different ID for each parameter, see
  212. * PRISM_DID_* constants.
  213. */
  214. uint32_t did;
  215. /**
  216. * See PRISM_STATUS_*-constants. Note that they are unusual: 0 = set; 1 = not set
  217. */
  218. uint16_t status;
  219. /**
  220. * length of data (which is always a uint32_t, but presumably this can be used
  221. * to specify that fewer bytes are used (with values in 'len' from 0-4). We
  222. * ignore this field.
  223. */
  224. uint16_t len;
  225. /**
  226. * The data value
  227. */
  228. uint32_t data;
  229. } __attribute__ ((packed));
  230. /**
  231. * Prism header format ('struct p80211msg' in Linux). All in host byte order (!).
  232. */
  233. struct PrismHeader
  234. {
  235. /**
  236. * We expect this to be a PRISM_MSGCODE_*.
  237. */
  238. uint32_t msgcode;
  239. /**
  240. * The length of the entire header.
  241. */
  242. uint32_t msglen;
  243. /**
  244. * Name of the device that captured the packet.
  245. */
  246. char devname[PRISM_DEVICE_NAME_LENGTH];
  247. /* followed by 'struct PrismValue's. Documentation suggests that these
  248. are typically the hosttime, mactime, channel, rssi, sq, signal, noise,
  249. rate, istx and frmlen values, but documentation is sparse. So we
  250. will use the 'did' fields to find out what we actually got. */
  251. } __attribute__ ((packed));
  252. /* ****** end of structure of messages of type ARPHRD_IEEE80211_PRISM ******* */
  253. /* ********** structure of messages of type ARPHRD_IEEE80211_FULL *********** */
  254. /**
  255. * Bits in the 'it_present' bitmask from the 'struct
  256. * Ieee80211RadiotapHeader'. For each value, we give the name, data
  257. * type, unit and then a description below. Note that the actual size
  258. * of the extension can be bigger as arguments must be padded so that
  259. * args of a given length must begin at a boundary of that length.
  260. * However, note that compound args are allowed (eg, 2 x uint16_t for
  261. * IEEE80211_RADIOTAP_CHANNEL) so total argument length is not a
  262. * reliable indicator of alignment requirement. See also
  263. * 'man 9 ieee80211_radiotap'.
  264. */
  265. enum RadiotapType
  266. {
  267. /**
  268. * IEEE80211_RADIOTAP_TSFT __le64 microseconds
  269. *
  270. * Value in microseconds of the MAC's 64-bit 802.11 Time
  271. * Synchronization Function timer when the first bit of the
  272. * MPDU arrived at the MAC. For received frames, only.
  273. */
  274. IEEE80211_RADIOTAP_TSFT = 0,
  275. /**
  276. * IEEE80211_RADIOTAP_FLAGS uint8_t bitmap
  277. *
  278. * Properties of transmitted and received frames. See flags
  279. * defined below.
  280. */
  281. IEEE80211_RADIOTAP_FLAGS = 1,
  282. /**
  283. * IEEE80211_RADIOTAP_RATE uint8_t 500kb/s
  284. *
  285. * Tx/Rx data rate
  286. */
  287. IEEE80211_RADIOTAP_RATE = 2,
  288. /**
  289. * IEEE80211_RADIOTAP_CHANNEL 2 x __le16 MHz, bitmap
  290. *
  291. * Tx/Rx frequency in MHz, followed by flags (see below).
  292. */
  293. IEEE80211_RADIOTAP_CHANNEL = 3,
  294. /**
  295. * IEEE80211_RADIOTAP_FHSS __le16 see below
  296. *
  297. * For frequency-hopping radios, the hop set (first byte)
  298. * and pattern (second byte).
  299. */
  300. IEEE80211_RADIOTAP_FHSS = 4,
  301. /**
  302. * IEEE80211_RADIOTAP_DBM_ANTSIGNAL s8 decibels from
  303. * one milliwatt (dBm)
  304. *
  305. * RF signal power at the antenna, decibel difference from
  306. * one milliwatt.
  307. */
  308. IEEE80211_RADIOTAP_DBM_ANTSIGNAL = 5,
  309. /**
  310. * IEEE80211_RADIOTAP_DBM_ANTNOISE s8 decibels from
  311. * one milliwatt (dBm)
  312. *
  313. * RF noise power at the antenna, decibel difference from one
  314. * milliwatt.
  315. */
  316. IEEE80211_RADIOTAP_DBM_ANTNOISE = 6,
  317. /**
  318. * IEEE80211_RADIOTAP_LOCK_QUALITY __le16 unitless
  319. *
  320. * Quality of Barker code lock. Unitless. Monotonically
  321. * nondecreasing with "better" lock strength. Called "Signal
  322. * Quality" in datasheets. (Is there a standard way to measure
  323. * this?)
  324. */
  325. IEEE80211_RADIOTAP_LOCK_QUALITY = 7,
  326. /**
  327. * IEEE80211_RADIOTAP_TX_ATTENUATION __le16 unitless
  328. *
  329. * Transmit power expressed as unitless distance from max
  330. * power set at factory calibration. 0 is max power.
  331. * Monotonically nondecreasing with lower power levels.
  332. */
  333. IEEE80211_RADIOTAP_TX_ATTENUATION = 8,
  334. /**
  335. * IEEE80211_RADIOTAP_DB_TX_ATTENUATION __le16 decibels (dB)
  336. *
  337. * Transmit power expressed as decibel distance from max power
  338. * set at factory calibration. 0 is max power. Monotonically
  339. * nondecreasing with lower power levels.
  340. */
  341. IEEE80211_RADIOTAP_DB_TX_ATTENUATION = 9,
  342. /**
  343. * IEEE80211_RADIOTAP_DBM_TX_POWER s8 decibels from
  344. * one milliwatt (dBm)
  345. *
  346. * Transmit power expressed as dBm (decibels from a 1 milliwatt
  347. * reference). This is the absolute power level measured at
  348. * the antenna port.
  349. */
  350. IEEE80211_RADIOTAP_DBM_TX_POWER = 10,
  351. /**
  352. * IEEE80211_RADIOTAP_ANTENNA uint8_t antenna index
  353. *
  354. * Unitless indication of the Rx/Tx antenna for this packet.
  355. * The first antenna is antenna 0.
  356. */
  357. IEEE80211_RADIOTAP_ANTENNA = 11,
  358. /**
  359. * IEEE80211_RADIOTAP_DB_ANTSIGNAL uint8_t decibel (dB)
  360. *
  361. * RF signal power at the antenna, decibel difference from an
  362. * arbitrary, fixed reference.
  363. */
  364. IEEE80211_RADIOTAP_DB_ANTSIGNAL = 12,
  365. /**
  366. * IEEE80211_RADIOTAP_DB_ANTNOISE uint8_t decibel (dB)
  367. *
  368. * RF noise power at the antenna, decibel difference from an
  369. * arbitrary, fixed reference point.
  370. */
  371. IEEE80211_RADIOTAP_DB_ANTNOISE = 13,
  372. /**
  373. * IEEE80211_RADIOTAP_RX_FLAGS __le16 bitmap
  374. *
  375. * Properties of received frames. See flags defined below.
  376. */
  377. IEEE80211_RADIOTAP_RX_FLAGS = 14,
  378. /**
  379. * IEEE80211_RADIOTAP_TX_FLAGS __le16 bitmap
  380. *
  381. * Properties of transmitted frames. See flags defined below.
  382. */
  383. IEEE80211_RADIOTAP_TX_FLAGS = 15,
  384. /**
  385. * IEEE80211_RADIOTAP_RTS_RETRIES uint8_t data
  386. *
  387. * Number of rts retries a transmitted frame used.
  388. */
  389. IEEE80211_RADIOTAP_RTS_RETRIES = 16,
  390. /**
  391. * IEEE80211_RADIOTAP_DATA_RETRIES uint8_t data
  392. *
  393. * Number of unicast retries a transmitted frame used.
  394. */
  395. IEEE80211_RADIOTAP_DATA_RETRIES = 17,
  396. /**
  397. * Extension bit, used to indicate that more bits are needed for
  398. * the bitmask.
  399. */
  400. IEEE80211_RADIOTAP_EXT = 31
  401. };
  402. /**
  403. * Bitmask indicating an extension of the bitmask is used.
  404. * (Mask corresponding to IEEE80211_RADIOTAP_EXT).
  405. */
  406. #define IEEE80211_RADIOTAP_PRESENT_EXTEND_MASK (1 << IEEE80211_RADIOTAP_EXT)
  407. /**
  408. * Bit in IEEE80211_RADIOTAP_FLAGS (which we might get
  409. * as part of a 'struct Ieee80211RadiotapHeader' extension
  410. * if the IEEE80211_RADIOTAP_FLAGS bit is set in
  411. * 'it_present'). The radiotap flags are an 8-bit field.
  412. *
  413. * Frame was sent/received during CFP (Contention Free Period)
  414. */
  415. #define IEEE80211_RADIOTAP_F_CFP 0x01
  416. /**
  417. * Bit in IEEE80211_RADIOTAP_FLAGS (which we might get
  418. * as part of a 'struct Ieee80211RadiotapHeader' extension
  419. * if the IEEE80211_RADIOTAP_FLAGS bit is set in
  420. * 'it_present'). The radiotap flags are an 8-bit field.
  421. *
  422. * Frame was sent/received with short preamble
  423. */
  424. #define IEEE80211_RADIOTAP_F_SHORTPRE 0x02
  425. /**
  426. * Bit in IEEE80211_RADIOTAP_FLAGS (which we might get
  427. * as part of a 'struct Ieee80211RadiotapHeader' extension
  428. * if the IEEE80211_RADIOTAP_FLAGS bit is set in
  429. * 'it_present'). The radiotap flags are an 8-bit field.
  430. *
  431. * Frame was sent/received with WEP encryption
  432. */
  433. #define IEEE80211_RADIOTAP_F_WEP 0x04
  434. /**
  435. * Bit in IEEE80211_RADIOTAP_FLAGS (which we might get
  436. * as part of a 'struct Ieee80211RadiotapHeader' extension
  437. * if the IEEE80211_RADIOTAP_FLAGS bit is set in
  438. * 'it_present'). The radiotap flags are an 8-bit field.
  439. *
  440. * Frame was sent/received with fragmentation
  441. */
  442. #define IEEE80211_RADIOTAP_F_FRAG 0x08
  443. /**
  444. * Bit in IEEE80211_RADIOTAP_FLAGS (which we might get
  445. * as part of a 'struct Ieee80211RadiotapHeader' extension
  446. * if the IEEE80211_RADIOTAP_FLAGS bit is set in
  447. * 'it_present'). The radiotap flags are an 8-bit field.
  448. *
  449. * Frame includes FCS (CRC at the end that needs to be removeD).
  450. */
  451. #define IEEE80211_RADIOTAP_F_FCS 0x10
  452. /**
  453. * Bit in IEEE80211_RADIOTAP_FLAGS (which we might get
  454. * as part of a 'struct Ieee80211RadiotapHeader' extension
  455. * if the IEEE80211_RADIOTAP_FLAGS bit is set in
  456. * 'it_present'). The radiotap flags are an 8-bit field.
  457. *
  458. * Frame has padding between 802.11 header and payload
  459. * (to 32-bit boundary)
  460. */
  461. #define IEEE80211_RADIOTAP_F_DATAPAD 0x20
  462. /**
  463. * For IEEE80211_RADIOTAP_RX_FLAGS:
  464. * frame failed crc check
  465. */
  466. #define IEEE80211_RADIOTAP_F_RX_BADFCS 0x0001
  467. /**
  468. * For IEEE80211_RADIOTAP_TX_FLAGS ('txflags' in 'struct RadiotapTransmissionHeader'):
  469. * failed due to excessive retries
  470. */
  471. #define IEEE80211_RADIOTAP_F_TX_FAIL 0x0001
  472. /**
  473. * For IEEE80211_RADIOTAP_TX_FLAGS ('txflags' in 'struct RadiotapTransmissionHeader'):
  474. * used cts 'protection'
  475. */
  476. #define IEEE80211_RADIOTAP_F_TX_CTS 0x0002
  477. /**
  478. * For IEEE80211_RADIOTAP_TX_FLAGS ('txflags' in 'struct RadiotapTransmissionHeader'):
  479. * used rts/cts handshake
  480. */
  481. #define IEEE80211_RADIOTAP_F_TX_RTS 0x0004
  482. /**
  483. * For IEEE80211_RADIOTAP_TX_FLAGS ('txflags' in 'struct RadiotapTransmissionHeader'):
  484. * frame should not be ACKed
  485. */
  486. #define IEEE80211_RADIOTAP_F_TX_NOACK 0x0008
  487. /**
  488. * For IEEE80211_RADIOTAP_TX_FLAGS ('txflags' in 'struct RadiotapTransmissionHeader'):
  489. * sequence number handled by userspace
  490. */
  491. #define IEEE80211_RADIOTAP_F_TX_NOSEQ 0x0010
  492. /**
  493. * Generic header for radiotap messages (receiving and sending). A
  494. * bit mask (it_present) determines which specific records follow.
  495. *
  496. * I am trying to describe precisely what the application programmer
  497. * should expect in the following, and for that reason I tell the
  498. * units and origin of each measurement (where it applies), or else I
  499. * use sufficiently weaselly language ("is a monotonically nondecreasing
  500. * function of...") that I cannot set false expectations for lawyerly
  501. * readers.
  502. *
  503. * The radio capture header precedes the 802.11 header.
  504. * All data in the header is little endian on all platforms.
  505. */
  506. struct Ieee80211RadiotapHeader
  507. {
  508. /**
  509. * Version 0. Only increases for drastic changes, introduction of
  510. * compatible new fields does not count.
  511. */
  512. uint8_t it_version;
  513. /**
  514. * Padding. Set to 0.
  515. */
  516. uint8_t it_pad;
  517. /**
  518. * length of the whole header in bytes, including it_version,
  519. * it_pad, it_len, and data fields.
  520. */
  521. uint16_t it_len;
  522. /**
  523. * A bitmap telling which fields are present. Set bit 31
  524. * (0x80000000) to extend the bitmap by another 32 bits. Additional
  525. * extensions are made by setting bit 31.
  526. */
  527. uint32_t it_present;
  528. };
  529. /**
  530. * Format of the header we need to prepend to messages to be sent to the
  531. * Kernel.
  532. */
  533. struct RadiotapTransmissionHeader
  534. {
  535. /**
  536. * First we begin with the 'generic' header we also get when receiving
  537. * messages.
  538. */
  539. struct Ieee80211RadiotapHeader header;
  540. /**
  541. * Transmission rate (we use 0, kernel makes up its mind anyway).
  542. */
  543. uint8_t rate;
  544. /**
  545. * Padding (we use 0). There is a requirement to pad args, so that
  546. * args of a given length must begin at a boundary of that length.
  547. * As our next argument is the 'it_len' with 2 bytes, we need 1 byte
  548. * of padding.
  549. */
  550. uint8_t pad1;
  551. /**
  552. * Transmission flags from on the IEEE80211_RADIOTAP_F_TX_* constant family.
  553. */
  554. uint16_t txflags;
  555. };
  556. /**
  557. * The above 'struct RadiotapTransmissionHeader' should have the
  558. * following value for 'header.it_present' based on the presence of
  559. * the 'rate' and 'txflags' in the overall struct.
  560. */
  561. #define IEEE80211_RADIOTAP_OUR_TRANSMISSION_HEADER_MASK ((1 << IEEE80211_RADIOTAP_RATE) | (1 << IEEE80211_RADIOTAP_TX_FLAGS))
  562. /**
  563. * struct Ieee80211RadiotapHeaderIterator - tracks walk through present radiotap arguments
  564. * in the radiotap header. Used when we parse radiotap packets received from the kernel.
  565. */
  566. struct Ieee80211RadiotapHeaderIterator
  567. {
  568. /**
  569. * pointer to the radiotap header we are walking through
  570. */
  571. const struct Ieee80211RadiotapHeader *rtheader;
  572. /**
  573. * pointer to current radiotap arg
  574. */
  575. const uint8_t *this_arg;
  576. /**
  577. * internal next argument pointer
  578. */
  579. const uint8_t *arg;
  580. /**
  581. * internal pointer to next present uint32_t (if IEEE80211_RADIOTAP_EXT is used).
  582. */
  583. const uint32_t *next_bitmap;
  584. /**
  585. * length of radiotap header in host byte ordering
  586. */
  587. size_t max_length;
  588. /**
  589. * internal shifter for current uint32_t bitmap, (it_present in host byte order),
  590. * If bit 0 is set, the 'arg_index' argument is present.
  591. */
  592. uint32_t bitmap_shifter;
  593. /**
  594. * IEEE80211_RADIOTAP_... index of current arg
  595. */
  596. unsigned int this_arg_index;
  597. /**
  598. * internal next argument index
  599. */
  600. unsigned int arg_index;
  601. };
  602. /* ************** end of structure of ARPHRD_IEEE80211_FULL ************** */
  603. /* ************************** our globals ******************************* */
  604. /**
  605. * struct for storing the information of the hardware. There is only
  606. * one of these.
  607. */
  608. struct HardwareInfos
  609. {
  610. /**
  611. * file descriptor for the raw socket
  612. */
  613. int fd_raw;
  614. /**
  615. * Which format has the header that we're getting when receiving packets?
  616. * Some ARPHRD_IEEE80211_XXX-value.
  617. */
  618. int arptype_in;
  619. /**
  620. * Name of the interface, not necessarily 0-terminated (!).
  621. */
  622. char iface[IFNAMSIZ];
  623. /**
  624. * MAC address of our own WLAN interface.
  625. */
  626. struct GNUNET_TRANSPORT_WLAN_MacAddress pl_mac;
  627. };
  628. /**
  629. * IO buffer used for buffering data in transit (to wireless or to stdout).
  630. */
  631. struct SendBuffer
  632. {
  633. /**
  634. * How many bytes of data are stored in 'buf' for transmission right now?
  635. * Data always starts at offset 0 and extends to 'size'.
  636. */
  637. size_t size;
  638. /**
  639. * How many bytes that were stored in 'buf' did we already write to the
  640. * destination? Always smaller than 'size'.
  641. */
  642. size_t pos;
  643. /**
  644. * Buffered data; twice the maximum allowed message size as we add some
  645. * headers.
  646. */
  647. char buf[MAXLINE * 2];
  648. };
  649. /**
  650. * Buffer for data read from stdin to be transmitted to the wirless card.
  651. */
  652. static struct SendBuffer write_pout;
  653. /**
  654. * Buffer for data read from the wireless card to be transmitted to stdout.
  655. */
  656. static struct SendBuffer write_std;
  657. /* *********** specialized version of server_mst.c begins here ********** */
  658. /**
  659. * To what multiple do we align messages? 8 byte should suffice for everyone
  660. * for now.
  661. */
  662. #define ALIGN_FACTOR 8
  663. /**
  664. * Smallest supported message.
  665. */
  666. #define MIN_BUFFER_SIZE sizeof (struct GNUNET_MessageHeader)
  667. /**
  668. * Functions with this signature are called whenever a
  669. * complete message is received by the tokenizer.
  670. *
  671. * @param cls closure
  672. * @param message the actual message
  673. */
  674. typedef void (*MessageTokenizerCallback) (void *cls,
  675. const struct
  676. GNUNET_MessageHeader *
  677. message);
  678. /**
  679. * Handle to a message stream tokenizer.
  680. */
  681. struct MessageStreamTokenizer
  682. {
  683. /**
  684. * Function to call on completed messages.
  685. */
  686. MessageTokenizerCallback cb;
  687. /**
  688. * Closure for cb.
  689. */
  690. void *cb_cls;
  691. /**
  692. * Size of the buffer (starting at 'hdr').
  693. */
  694. size_t curr_buf;
  695. /**
  696. * How many bytes in buffer have we already processed?
  697. */
  698. size_t off;
  699. /**
  700. * How many bytes in buffer are valid right now?
  701. */
  702. size_t pos;
  703. /**
  704. * Beginning of the buffer. Typed like this to force alignment.
  705. */
  706. struct GNUNET_MessageHeader *hdr;
  707. };
  708. /**
  709. * Create a message stream tokenizer.
  710. *
  711. * @param cb function to call on completed messages
  712. * @param cb_cls closure for cb
  713. * @return handle to tokenizer
  714. */
  715. static struct MessageStreamTokenizer *
  716. mst_create (MessageTokenizerCallback cb,
  717. void *cb_cls)
  718. {
  719. struct MessageStreamTokenizer *ret;
  720. ret = malloc (sizeof (struct MessageStreamTokenizer));
  721. if (NULL == ret)
  722. {
  723. fprintf (stderr, "Failed to allocate buffer for tokenizer\n");
  724. exit (1);
  725. }
  726. ret->hdr = malloc (MIN_BUFFER_SIZE);
  727. if (NULL == ret->hdr)
  728. {
  729. fprintf (stderr, "Failed to allocate buffer for alignment\n");
  730. exit (1);
  731. }
  732. ret->curr_buf = MIN_BUFFER_SIZE;
  733. ret->cb = cb;
  734. ret->cb_cls = cb_cls;
  735. return ret;
  736. }
  737. /**
  738. * Add incoming data to the receive buffer and call the
  739. * callback for all complete messages.
  740. *
  741. * @param mst tokenizer to use
  742. * @param buf input data to add
  743. * @param size number of bytes in buf
  744. * @return GNUNET_OK if we are done processing (need more data)
  745. * GNUNET_SYSERR if the data stream is corrupt
  746. */
  747. static int
  748. mst_receive (struct MessageStreamTokenizer *mst,
  749. const char *buf, size_t size)
  750. {
  751. const struct GNUNET_MessageHeader *hdr;
  752. size_t delta;
  753. uint16_t want;
  754. char *ibuf;
  755. int need_align;
  756. unsigned long offset;
  757. int ret;
  758. ret = GNUNET_OK;
  759. ibuf = (char *) mst->hdr;
  760. while (mst->pos > 0)
  761. {
  762. do_align:
  763. if ((mst->curr_buf - mst->off < sizeof (struct GNUNET_MessageHeader)) ||
  764. (0 != (mst->off % ALIGN_FACTOR)))
  765. {
  766. /* need to align or need more space */
  767. mst->pos -= mst->off;
  768. memmove (ibuf, &ibuf[mst->off], mst->pos);
  769. mst->off = 0;
  770. }
  771. if (mst->pos - mst->off < sizeof (struct GNUNET_MessageHeader))
  772. {
  773. delta =
  774. GNUNET_MIN (sizeof (struct GNUNET_MessageHeader) -
  775. (mst->pos - mst->off), size);
  776. memcpy (&ibuf[mst->pos], buf, delta);
  777. mst->pos += delta;
  778. buf += delta;
  779. size -= delta;
  780. }
  781. if (mst->pos - mst->off < sizeof (struct GNUNET_MessageHeader))
  782. {
  783. return GNUNET_OK;
  784. }
  785. hdr = (const struct GNUNET_MessageHeader *) &ibuf[mst->off];
  786. want = ntohs (hdr->size);
  787. if (want < sizeof (struct GNUNET_MessageHeader))
  788. {
  789. fprintf (stderr,
  790. "Received invalid message from stdin\n");
  791. exit (1);
  792. }
  793. if (mst->curr_buf - mst->off < want)
  794. {
  795. /* need more space */
  796. mst->pos -= mst->off;
  797. memmove (ibuf, &ibuf[mst->off], mst->pos);
  798. mst->off = 0;
  799. }
  800. if (want > mst->curr_buf)
  801. {
  802. mst->hdr = realloc (mst->hdr, want);
  803. if (NULL == mst->hdr)
  804. {
  805. fprintf (stderr, "Failed to allocate buffer for alignment\n");
  806. exit (1);
  807. }
  808. ibuf = (char *) mst->hdr;
  809. mst->curr_buf = want;
  810. }
  811. hdr = (const struct GNUNET_MessageHeader *) &ibuf[mst->off];
  812. if (mst->pos - mst->off < want)
  813. {
  814. delta = GNUNET_MIN (want - (mst->pos - mst->off), size);
  815. memcpy (&ibuf[mst->pos], buf, delta);
  816. mst->pos += delta;
  817. buf += delta;
  818. size -= delta;
  819. }
  820. if (mst->pos - mst->off < want)
  821. {
  822. return GNUNET_OK;
  823. }
  824. mst->cb (mst->cb_cls, hdr);
  825. mst->off += want;
  826. if (mst->off == mst->pos)
  827. {
  828. /* reset to beginning of buffer, it's free right now! */
  829. mst->off = 0;
  830. mst->pos = 0;
  831. }
  832. }
  833. while (size > 0)
  834. {
  835. if (size < sizeof (struct GNUNET_MessageHeader))
  836. break;
  837. offset = (unsigned long) buf;
  838. need_align = (0 != offset % ALIGN_FACTOR) ? GNUNET_YES : GNUNET_NO;
  839. if (GNUNET_NO == need_align)
  840. {
  841. /* can try to do zero-copy and process directly from original buffer */
  842. hdr = (const struct GNUNET_MessageHeader *) buf;
  843. want = ntohs (hdr->size);
  844. if (want < sizeof (struct GNUNET_MessageHeader))
  845. {
  846. fprintf (stderr,
  847. "Received invalid message from stdin\n");
  848. exit (1);
  849. }
  850. if (size < want)
  851. break; /* or not, buffer incomplete, so copy to private buffer... */
  852. mst->cb (mst->cb_cls, hdr);
  853. buf += want;
  854. size -= want;
  855. }
  856. else
  857. {
  858. /* need to copy to private buffer to align;
  859. * yes, we go a bit more spagetti than usual here */
  860. goto do_align;
  861. }
  862. }
  863. if (size > 0)
  864. {
  865. if (size + mst->pos > mst->curr_buf)
  866. {
  867. mst->hdr = realloc (mst->hdr, size + mst->pos);
  868. if (NULL == mst->hdr)
  869. {
  870. fprintf (stderr, "Failed to allocate buffer for alignment\n");
  871. exit (1);
  872. }
  873. ibuf = (char *) mst->hdr;
  874. mst->curr_buf = size + mst->pos;
  875. }
  876. if (mst->pos + size > mst->curr_buf)
  877. {
  878. fprintf (stderr,
  879. "Assertion failed\n");
  880. exit (1);
  881. }
  882. memcpy (&ibuf[mst->pos], buf, size);
  883. mst->pos += size;
  884. }
  885. return ret;
  886. }
  887. /**
  888. * Destroys a tokenizer.
  889. *
  890. * @param mst tokenizer to destroy
  891. */
  892. static void
  893. mst_destroy (struct MessageStreamTokenizer *mst)
  894. {
  895. free (mst->hdr);
  896. free (mst);
  897. }
  898. /* ***************** end of server_mst.c clone ***************** **/
  899. /* ************** code for handling of ARPHRD_IEEE80211_FULL ************** */
  900. /**
  901. * Radiotap header iteration
  902. *
  903. * call __ieee80211_radiotap_iterator_init() to init a semi-opaque iterator
  904. * struct Ieee80211RadiotapHeaderIterator (no need to init the struct beforehand)
  905. * then loop calling __ieee80211_radiotap_iterator_next()... it returns -1
  906. * if there are no more args in the header, or the next argument type index
  907. * that is present. The iterator's this_arg member points to the start of the
  908. * argument associated with the current argument index that is present,
  909. * which can be found in the iterator's this_arg_index member. This arg
  910. * index corresponds to the IEEE80211_RADIOTAP_... defines.
  911. *
  912. * @param iterator iterator to initialize
  913. * @param radiotap_header message to parse
  914. * @param max_length number of valid bytes in radiotap_header
  915. * @return 0 on success, -1 on error
  916. */
  917. static int
  918. ieee80211_radiotap_iterator_init (struct Ieee80211RadiotapHeaderIterator *iterator,
  919. const struct Ieee80211RadiotapHeader *radiotap_header,
  920. size_t max_length)
  921. {
  922. if ( (iterator == NULL) ||
  923. (radiotap_header == NULL) )
  924. return -1;
  925. /* Linux only supports version 0 radiotap format */
  926. if (0 != radiotap_header->it_version)
  927. return -1;
  928. /* sanity check for allowed length and radiotap length field */
  929. if ( (max_length < sizeof (struct Ieee80211RadiotapHeader)) ||
  930. (max_length < (GNUNET_le16toh (radiotap_header->it_len))) )
  931. return -1;
  932. memset (iterator, 0, sizeof (struct Ieee80211RadiotapHeaderIterator));
  933. iterator->rtheader = radiotap_header;
  934. iterator->max_length = GNUNET_le16toh (radiotap_header->it_len);
  935. iterator->bitmap_shifter = GNUNET_le32toh (radiotap_header->it_present);
  936. iterator->arg = ((uint8_t *) radiotap_header) + sizeof (struct Ieee80211RadiotapHeader);
  937. /* find payload start allowing for extended bitmap(s) */
  938. if (0 != (iterator->bitmap_shifter & IEEE80211_RADIOTAP_PRESENT_EXTEND_MASK))
  939. {
  940. while (GNUNET_le32toh (*((uint32_t *) iterator->arg)) & IEEE80211_RADIOTAP_PRESENT_EXTEND_MASK)
  941. {
  942. iterator->arg += sizeof (uint32_t);
  943. /*
  944. * check for insanity where the present bitmaps
  945. * keep claiming to extend up to or even beyond the
  946. * stated radiotap header length
  947. */
  948. if (iterator->arg - ((uint8_t*) iterator->rtheader) > iterator->max_length)
  949. return -1;
  950. }
  951. iterator->arg += sizeof (uint32_t);
  952. /*
  953. * no need to check again for blowing past stated radiotap
  954. * header length, becuase ieee80211_radiotap_iterator_next
  955. * checks it before it is dereferenced
  956. */
  957. }
  958. /* we are all initialized happily */
  959. return 0;
  960. }
  961. /**
  962. * Returns the next radiotap parser iterator arg.
  963. *
  964. * This function returns the next radiotap arg index (IEEE80211_RADIOTAP_...)
  965. * and sets iterator->this_arg to point to the payload for the arg. It takes
  966. * care of alignment handling and extended present fields. interator->this_arg
  967. * can be changed by the caller. The args pointed to are in little-endian
  968. * format.
  969. *
  970. * @param iterator: radiotap_iterator to move to next arg (if any)
  971. * @return next present arg index on success or -1 if no more or error
  972. */
  973. static int
  974. ieee80211_radiotap_iterator_next (struct Ieee80211RadiotapHeaderIterator *iterator)
  975. {
  976. /*
  977. * small length lookup table for all radiotap types we heard of
  978. * starting from b0 in the bitmap, so we can walk the payload
  979. * area of the radiotap header
  980. *
  981. * There is a requirement to pad args, so that args
  982. * of a given length must begin at a boundary of that length
  983. * -- but note that compound args are allowed (eg, 2 x uint16_t
  984. * for IEEE80211_RADIOTAP_CHANNEL) so total arg length is not
  985. * a reliable indicator of alignment requirement.
  986. *
  987. * upper nybble: content alignment for arg
  988. * lower nybble: content length for arg
  989. */
  990. static const uint8_t rt_sizes[] = {
  991. [IEEE80211_RADIOTAP_TSFT] = 0x88,
  992. [IEEE80211_RADIOTAP_FLAGS] = 0x11,
  993. [IEEE80211_RADIOTAP_RATE] = 0x11,
  994. [IEEE80211_RADIOTAP_CHANNEL] = 0x24,
  995. [IEEE80211_RADIOTAP_FHSS] = 0x22,
  996. [IEEE80211_RADIOTAP_DBM_ANTSIGNAL] = 0x11,
  997. [IEEE80211_RADIOTAP_DBM_ANTNOISE] = 0x11,
  998. [IEEE80211_RADIOTAP_LOCK_QUALITY] = 0x22,
  999. [IEEE80211_RADIOTAP_TX_ATTENUATION] = 0x22,
  1000. [IEEE80211_RADIOTAP_DB_TX_ATTENUATION] = 0x22,
  1001. [IEEE80211_RADIOTAP_DBM_TX_POWER] = 0x11,
  1002. [IEEE80211_RADIOTAP_ANTENNA] = 0x11,
  1003. [IEEE80211_RADIOTAP_DB_ANTSIGNAL] = 0x11,
  1004. [IEEE80211_RADIOTAP_DB_ANTNOISE] = 0x11,
  1005. [IEEE80211_RADIOTAP_TX_FLAGS] = 0x22,
  1006. [IEEE80211_RADIOTAP_RX_FLAGS] = 0x22,
  1007. [IEEE80211_RADIOTAP_RTS_RETRIES] = 0x11,
  1008. [IEEE80211_RADIOTAP_DATA_RETRIES] = 0x11
  1009. /*
  1010. * add more here as they are defined in
  1011. * include/net/ieee80211_radiotap.h
  1012. */
  1013. };
  1014. /*
  1015. * for every radiotap entry we can at
  1016. * least skip (by knowing the length)...
  1017. */
  1018. while (iterator->arg_index < sizeof (rt_sizes))
  1019. {
  1020. int hit = (0 != (iterator->bitmap_shifter & 1));
  1021. if (hit)
  1022. {
  1023. unsigned int wanted_alignment;
  1024. unsigned int unalignment;
  1025. /*
  1026. * arg is present, account for alignment padding
  1027. * 8-bit args can be at any alignment
  1028. * 16-bit args must start on 16-bit boundary
  1029. * 32-bit args must start on 32-bit boundary
  1030. * 64-bit args must start on 64-bit boundary
  1031. *
  1032. * note that total arg size can differ from alignment of
  1033. * elements inside arg, so we use upper nybble of length table
  1034. * to base alignment on. First, 'wanted_alignment' is set to be
  1035. * 1 for 8-bit, 2 for 16-bit, 4 for 32-bit and 8 for 64-bit
  1036. * arguments. Then, we calculate the 'unalignment' (how many
  1037. * bytes we are over by taking the difference of 'arg' and the
  1038. * overall starting point modulo the desired alignment. As
  1039. * desired alignments are powers of two, we can do modulo with
  1040. * binary "&" (and also avoid the possibility of a division by
  1041. * zero if the 'rt_sizes' table contains bogus entries).
  1042. *
  1043. * also note: these alignments are relative to the start of the
  1044. * radiotap header. There is no guarantee that the radiotap
  1045. * header itself is aligned on any kind of boundary, thus we
  1046. * need to really look at the delta here.
  1047. */
  1048. wanted_alignment = rt_sizes[iterator->arg_index] >> 4;
  1049. unalignment = (((void *) iterator->arg) - ((void *) iterator->rtheader)) & (wanted_alignment - 1);
  1050. if (0 != unalignment)
  1051. {
  1052. /* need padding (by 'wanted_alignment - unalignment') */
  1053. iterator->arg_index += wanted_alignment - unalignment;
  1054. }
  1055. /*
  1056. * this is what we will return to user, but we need to
  1057. * move on first so next call has something fresh to test
  1058. */
  1059. iterator->this_arg_index = iterator->arg_index;
  1060. iterator->this_arg = iterator->arg;
  1061. /* internally move on the size of this arg (using lower nybble from
  1062. the table) */
  1063. iterator->arg += rt_sizes[iterator->arg_index] & 0x0f;
  1064. /*
  1065. * check for insanity where we are given a bitmap that
  1066. * claims to have more arg content than the length of the
  1067. * radiotap section. We will normally end up equalling this
  1068. * max_length on the last arg, never exceeding it.
  1069. */
  1070. if ((((void *) iterator->arg) - ((void *) iterator->rtheader)) > iterator->max_length)
  1071. return -1;
  1072. }
  1073. /* Now, move on to next bit / next entry */
  1074. iterator->arg_index++;
  1075. if (0 == (iterator->arg_index % 32))
  1076. {
  1077. /* completed current uint32_t bitmap */
  1078. if (0 != (iterator->bitmap_shifter & 1))
  1079. {
  1080. /* bit 31 was set, there is more; move to next uint32_t bitmap */
  1081. iterator->bitmap_shifter = GNUNET_le32toh (*iterator->next_bitmap);
  1082. iterator->next_bitmap++;
  1083. }
  1084. else
  1085. {
  1086. /* no more bitmaps: end (by setting arg_index to high, unsupported value) */
  1087. iterator->arg_index = sizeof (rt_sizes);
  1088. }
  1089. }
  1090. else
  1091. {
  1092. /* just try the next bit (while loop will move on) */
  1093. iterator->bitmap_shifter >>= 1;
  1094. }
  1095. /* if we found a valid arg earlier, return it now */
  1096. if (hit)
  1097. return iterator->this_arg_index;
  1098. }
  1099. /* we don't know how to handle any more args (or there are no more),
  1100. so we're done (this is not an error) */
  1101. return -1;
  1102. }
  1103. /**
  1104. * Calculate crc32, the start of the calculation
  1105. *
  1106. * @param buf buffer to calc the crc
  1107. * @param len len of the buffer
  1108. * @return crc sum
  1109. */
  1110. static unsigned long
  1111. calc_crc_osdep (const unsigned char *buf, size_t len)
  1112. {
  1113. static const unsigned long int crc_tbl_osdep[256] = {
  1114. 0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F,
  1115. 0xE963A535, 0x9E6495A3,
  1116. 0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988, 0x09B64C2B, 0x7EB17CBD,
  1117. 0xE7B82D07, 0x90BF1D91,
  1118. 0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE, 0x1ADAD47D, 0x6DDDE4EB,
  1119. 0xF4D4B551, 0x83D385C7,
  1120. 0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC, 0x14015C4F, 0x63066CD9,
  1121. 0xFA0F3D63, 0x8D080DF5,
  1122. 0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172, 0x3C03E4D1, 0x4B04D447,
  1123. 0xD20D85FD, 0xA50AB56B,
  1124. 0x35B5A8FA, 0x42B2986C, 0xDBBBC9D6, 0xACBCF940, 0x32D86CE3, 0x45DF5C75,
  1125. 0xDCD60DCF, 0xABD13D59,
  1126. 0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116, 0x21B4F4B5, 0x56B3C423,
  1127. 0xCFBA9599, 0xB8BDA50F,
  1128. 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924, 0x2F6F7C87, 0x58684C11,
  1129. 0xC1611DAB, 0xB6662D3D,
  1130. 0x76DC4190, 0x01DB7106, 0x98D220BC, 0xEFD5102A, 0x71B18589, 0x06B6B51F,
  1131. 0x9FBFE4A5, 0xE8B8D433,
  1132. 0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818, 0x7F6A0DBB, 0x086D3D2D,
  1133. 0x91646C97, 0xE6635C01,
  1134. 0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E, 0x6C0695ED, 0x1B01A57B,
  1135. 0x8208F4C1, 0xF50FC457,
  1136. 0x65B0D9C6, 0x12B7E950, 0x8BBEB8EA, 0xFCB9887C, 0x62DD1DDF, 0x15DA2D49,
  1137. 0x8CD37CF3, 0xFBD44C65,
  1138. 0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2, 0x4ADFA541, 0x3DD895D7,
  1139. 0xA4D1C46D, 0xD3D6F4FB,
  1140. 0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0, 0x44042D73, 0x33031DE5,
  1141. 0xAA0A4C5F, 0xDD0D7CC9,
  1142. 0x5005713C, 0x270241AA, 0xBE0B1010, 0xC90C2086, 0x5768B525, 0x206F85B3,
  1143. 0xB966D409, 0xCE61E49F,
  1144. 0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, 0x59B33D17, 0x2EB40D81,
  1145. 0xB7BD5C3B, 0xC0BA6CAD,
  1146. 0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A, 0xEAD54739, 0x9DD277AF,
  1147. 0x04DB2615, 0x73DC1683,
  1148. 0xE3630B12, 0x94643B84, 0x0D6D6A3E, 0x7A6A5AA8, 0xE40ECF0B, 0x9309FF9D,
  1149. 0x0A00AE27, 0x7D079EB1,
  1150. 0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE, 0xF762575D, 0x806567CB,
  1151. 0x196C3671, 0x6E6B06E7,
  1152. 0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC, 0xF9B9DF6F, 0x8EBEEFF9,
  1153. 0x17B7BE43, 0x60B08ED5,
  1154. 0xD6D6A3E8, 0xA1D1937E, 0x38D8C2C4, 0x4FDFF252, 0xD1BB67F1, 0xA6BC5767,
  1155. 0x3FB506DD, 0x48B2364B,
  1156. 0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60, 0xDF60EFC3, 0xA867DF55,
  1157. 0x316E8EEF, 0x4669BE79,
  1158. 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236, 0xCC0C7795, 0xBB0B4703,
  1159. 0x220216B9, 0x5505262F,
  1160. 0xC5BA3BBE, 0xB2BD0B28, 0x2BB45A92, 0x5CB36A04, 0xC2D7FFA7, 0xB5D0CF31,
  1161. 0x2CD99E8B, 0x5BDEAE1D,
  1162. 0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A, 0x9C0906A9, 0xEB0E363F,
  1163. 0x72076785, 0x05005713,
  1164. 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38, 0x92D28E9B, 0xE5D5BE0D,
  1165. 0x7CDCEFB7, 0x0BDBDF21,
  1166. 0x86D3D2D4, 0xF1D4E242, 0x68DDB3F8, 0x1FDA836E, 0x81BE16CD, 0xF6B9265B,
  1167. 0x6FB077E1, 0x18B74777,
  1168. 0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C, 0x8F659EFF, 0xF862AE69,
  1169. 0x616BFFD3, 0x166CCF45,
  1170. 0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2, 0xA7672661, 0xD06016F7,
  1171. 0x4969474D, 0x3E6E77DB,
  1172. 0xAED16A4A, 0xD9D65ADC, 0x40DF0B66, 0x37D83BF0, 0xA9BCAE53, 0xDEBB9EC5,
  1173. 0x47B2CF7F, 0x30B5FFE9,
  1174. 0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, 0xBAD03605, 0xCDD70693,
  1175. 0x54DE5729, 0x23D967BF,
  1176. 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94, 0xB40BBE37, 0xC30C8EA1,
  1177. 0x5A05DF1B, 0x2D02EF8D
  1178. };
  1179. unsigned long crc = 0xFFFFFFFF;
  1180. for (; len > 0; len--, buf++)
  1181. crc = crc_tbl_osdep[(crc ^ *buf) & 0xFF] ^ (crc >> 8);
  1182. return (~crc);
  1183. }
  1184. /**
  1185. * Calculate and check crc of the wlan packet
  1186. *
  1187. * @param buf buffer of the packet, with len + 4 bytes of data,
  1188. * the last 4 bytes being the checksum
  1189. * @param len length of the payload in data
  1190. * @return 0 on success (checksum matches), 1 on error
  1191. */
  1192. static int
  1193. check_crc_buf_osdep (const unsigned char *buf, size_t len)
  1194. {
  1195. unsigned long crc;
  1196. crc = calc_crc_osdep (buf, len);
  1197. buf += len;
  1198. if (((crc) & 0xFF) == buf[0] && ((crc >> 8) & 0xFF) == buf[1] &&
  1199. ((crc >> 16) & 0xFF) == buf[2] && ((crc >> 24) & 0xFF) == buf[3])
  1200. return 0;
  1201. return 1;
  1202. }
  1203. /* ************end of code for handling of ARPHRD_IEEE80211_FULL ************** */
  1204. /* ************beginning of code for reading packets from kernel ************** */
  1205. /**
  1206. * Return the channel from the frequency (in Mhz)
  1207. *
  1208. * @param frequency of the channel
  1209. * @return number of the channel
  1210. */
  1211. static int
  1212. get_channel_from_frequency (int32_t frequency)
  1213. {
  1214. if (frequency >= 2412 && frequency <= 2472)
  1215. return (frequency - 2407) / 5;
  1216. if (frequency == 2484)
  1217. return 14;
  1218. if (frequency >= 5000 && frequency <= 6100)
  1219. return (frequency - 5000) / 5;
  1220. return -1;
  1221. }
  1222. /**
  1223. * Get the channel used by our WLAN interface.
  1224. *
  1225. * @param dev pointer to the dev struct of the card
  1226. * @return channel number, -1 on error
  1227. */
  1228. static int
  1229. linux_get_channel (const struct HardwareInfos *dev)
  1230. {
  1231. struct iwreq wrq;
  1232. int32_t frequency;
  1233. memset (&wrq, 0, sizeof (struct iwreq));
  1234. strncpy (wrq.ifr_name, dev->iface, IFNAMSIZ);
  1235. if (0 > ioctl (dev->fd_raw, SIOCGIWFREQ, &wrq))
  1236. return -1;
  1237. frequency = wrq.u.freq.m; /* 'iw_freq' defines 'm' as '__s32', so we keep it signed */
  1238. if (100000000 < frequency)
  1239. frequency /= 100000;
  1240. else if (1000000 < frequency)
  1241. frequency /= 1000;
  1242. if (1000 < frequency)
  1243. return get_channel_from_frequency (frequency);
  1244. return frequency;
  1245. }
  1246. /**
  1247. * Read from the raw socket (the wlan card), parse the packet and
  1248. * put the result into the buffer for transmission to 'stdout'.
  1249. *
  1250. * @param dev pointer to the struct of the wlan card
  1251. * @param buf buffer to read to; first bytes will be the 'struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame',
  1252. * followed by the actual payload
  1253. * @param buf_size size of the buffer
  1254. * @param ri where to write radiotap_rx info
  1255. * @return number of bytes written to 'buf'
  1256. */
  1257. static ssize_t
  1258. linux_read (struct HardwareInfos *dev,
  1259. unsigned char *buf, size_t buf_size,
  1260. struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *ri)
  1261. {
  1262. unsigned char tmpbuf[buf_size];
  1263. ssize_t caplen;
  1264. size_t n;
  1265. int got_signal = 0;
  1266. int got_noise = 0;
  1267. int got_channel = 0;
  1268. int fcs_removed = 0;
  1269. caplen = read (dev->fd_raw, tmpbuf, buf_size);
  1270. if (0 > caplen)
  1271. {
  1272. if (EAGAIN == errno)
  1273. return 0;
  1274. fprintf (stderr, "Failed to read from RAW socket: %s\n", strerror (errno));
  1275. return -1;
  1276. }
  1277. memset (ri, 0, sizeof (*ri));
  1278. switch (dev->arptype_in)
  1279. {
  1280. case ARPHRD_IEEE80211_PRISM:
  1281. {
  1282. const struct PrismHeader *ph;
  1283. ph = (const struct PrismHeader*) tmpbuf;
  1284. n = ph->msglen;
  1285. if ( (n < 8) || (n >= caplen) )
  1286. return 0; /* invalid format */
  1287. if ( (PRISM_MSGCODE_MONITOR == ph->msgcode) &&
  1288. (n >= sizeof (struct PrismHeader)) )
  1289. {
  1290. const char *pos;
  1291. size_t left;
  1292. struct PrismValue pv;
  1293. left = n - sizeof (struct PrismHeader);
  1294. pos = (const char *) &ph[1];
  1295. while (left > sizeof (struct PrismValue))
  1296. {
  1297. left -= sizeof (struct PrismValue);
  1298. memcpy (&pv, pos, sizeof (struct PrismValue));
  1299. pos += sizeof (struct PrismValue);
  1300. switch (pv.did)
  1301. {
  1302. case PRISM_DID_NOISE:
  1303. if (PRISM_STATUS_OK == pv.status)
  1304. {
  1305. ri->ri_noise = pv.data;
  1306. /* got_noise = 1; */
  1307. }
  1308. break;
  1309. case PRISM_DID_RATE:
  1310. if (PRISM_STATUS_OK == pv.status)
  1311. ri->ri_rate = pv.data * 500000;
  1312. break;
  1313. case PRISM_DID_CHANNEL:
  1314. if (PRISM_STATUS_OK == pv.status)
  1315. {
  1316. ri->ri_channel = pv.data;
  1317. got_channel = 1;
  1318. }
  1319. break;
  1320. case PRISM_DID_MACTIME:
  1321. if (PRISM_STATUS_OK == pv.status)
  1322. ri->ri_mactime = pv.data;
  1323. break;
  1324. case PRISM_DID_SIGNAL:
  1325. if (PRISM_STATUS_OK == pv.status)
  1326. {
  1327. ri->ri_power = pv.data;
  1328. /* got_signal = 1; */
  1329. }
  1330. break;
  1331. }
  1332. }
  1333. }
  1334. if ( (n < 8) || (n >= caplen) )
  1335. return 0; /* invalid format */
  1336. }
  1337. break;
  1338. case ARPHRD_IEEE80211_FULL:
  1339. {
  1340. struct Ieee80211RadiotapHeaderIterator iterator;
  1341. struct Ieee80211RadiotapHeader *rthdr;
  1342. memset (&iterator, 0, sizeof (iterator));
  1343. rthdr = (struct Ieee80211RadiotapHeader *) tmpbuf;
  1344. n = GNUNET_le16toh (rthdr->it_len);
  1345. if ( (n < sizeof (struct Ieee80211RadiotapHeader)) || (n >= caplen))
  1346. return 0; /* invalid 'it_len' */
  1347. if (0 != ieee80211_radiotap_iterator_init (&iterator, rthdr, caplen))
  1348. return 0;
  1349. /* go through the radiotap arguments we have been given by the driver */
  1350. while (0 <= ieee80211_radiotap_iterator_next (&iterator))
  1351. {
  1352. switch (iterator.this_arg_index)
  1353. {
  1354. case IEEE80211_RADIOTAP_TSFT:
  1355. ri->ri_mactime = GNUNET_le64toh (*((uint64_t *) iterator.this_arg));
  1356. break;
  1357. case IEEE80211_RADIOTAP_DBM_ANTSIGNAL:
  1358. if (!got_signal)
  1359. {
  1360. ri->ri_power = * ((int8_t*) iterator.this_arg);
  1361. got_signal = 1;
  1362. }
  1363. break;
  1364. case IEEE80211_RADIOTAP_DB_ANTSIGNAL:
  1365. if (!got_signal)
  1366. {
  1367. ri->ri_power = * ((int8_t*) iterator.this_arg);
  1368. got_signal = 1;
  1369. }
  1370. break;
  1371. case IEEE80211_RADIOTAP_DBM_ANTNOISE:
  1372. if (!got_noise)
  1373. {
  1374. ri->ri_noise = * ((int8_t*) iterator.this_arg);
  1375. got_noise = 1;
  1376. }
  1377. break;
  1378. case IEEE80211_RADIOTAP_DB_ANTNOISE:
  1379. if (!got_noise)
  1380. {
  1381. ri->ri_noise = * ((int8_t*) iterator.this_arg);
  1382. got_noise = 1;
  1383. }
  1384. break;
  1385. case IEEE80211_RADIOTAP_ANTENNA:
  1386. ri->ri_antenna = *iterator.this_arg;
  1387. break;
  1388. case IEEE80211_RADIOTAP_CHANNEL:
  1389. ri->ri_channel = *iterator.this_arg;
  1390. got_channel = 1;
  1391. break;
  1392. case IEEE80211_RADIOTAP_RATE:
  1393. ri->ri_rate = (*iterator.this_arg) * 500000;
  1394. break;
  1395. case IEEE80211_RADIOTAP_FLAGS:
  1396. {
  1397. uint8_t flags = *iterator.this_arg;
  1398. /* is the CRC visible at the end? if so, remove */
  1399. if (0 != (flags & IEEE80211_RADIOTAP_F_FCS))
  1400. {
  1401. fcs_removed = 1;
  1402. caplen -= sizeof (uint32_t);
  1403. }
  1404. break;
  1405. }
  1406. case IEEE80211_RADIOTAP_RX_FLAGS:
  1407. {
  1408. uint16_t flags = ntohs (* ((uint16_t *) iterator.this_arg));
  1409. if (0 != (flags & IEEE80211_RADIOTAP_F_RX_BADFCS))
  1410. return 0;
  1411. }
  1412. break;
  1413. } /* end of 'switch' */
  1414. } /* end of the 'while' loop */
  1415. }
  1416. break;
  1417. case ARPHRD_IEEE80211:
  1418. n = 0; /* no header */
  1419. break;
  1420. case ARPHRD_ETHER:
  1421. {
  1422. if (sizeof (struct GNUNET_TRANSPORT_WLAN_Ieee8023Frame) > caplen)
  1423. return 0; /* invalid */
  1424. memcpy (&buf[sizeof (struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame)],
  1425. tmpbuf + sizeof (struct GNUNET_TRANSPORT_WLAN_Ieee8023Frame),
  1426. caplen - sizeof (struct GNUNET_TRANSPORT_WLAN_Ieee8023Frame) - 4 /* 4 byte FCS */);
  1427. return caplen - sizeof (struct GNUNET_TRANSPORT_WLAN_Ieee8023Frame) - 4;
  1428. }
  1429. default:
  1430. errno = ENOTSUP; /* unsupported format */
  1431. return -1;
  1432. }
  1433. caplen -= n;
  1434. if (! got_channel)
  1435. ri->ri_channel = linux_get_channel (dev);
  1436. /* detect CRC32 at the end, even if the flag wasn't set and remove it */
  1437. if ( (0 == fcs_removed) &&
  1438. (0 == check_crc_buf_osdep (tmpbuf + n, caplen - sizeof (uint32_t))) )
  1439. {
  1440. /* NOTE: this heuristic can of course fail if there happens to
  1441. be a matching checksum at the end. Would be good to have
  1442. some data to see how often this heuristic actually works. */
  1443. caplen -= sizeof (uint32_t);
  1444. }
  1445. /* copy payload to target buffer */
  1446. memcpy (buf, tmpbuf + n, caplen);
  1447. return caplen;
  1448. }
  1449. /* ************end of code for reading packets from kernel ************** */
  1450. /* ************other helper functions for main start here ************** */
  1451. /**
  1452. * Open the wireless network interface for reading/writing.
  1453. *
  1454. * @param dev pointer to the device struct
  1455. * @return 0 on success
  1456. */
  1457. static int
  1458. open_device_raw (struct HardwareInfos *dev)
  1459. {
  1460. struct ifreq ifr;
  1461. struct iwreq wrq;
  1462. struct packet_mreq mr;
  1463. struct sockaddr_ll sll;
  1464. /* find the interface index */
  1465. memset (&ifr, 0, sizeof (ifr));
  1466. strncpy (ifr.ifr_name, dev->iface, IFNAMSIZ);
  1467. if (-1 == ioctl (dev->fd_raw, SIOCGIFINDEX, &ifr))
  1468. {
  1469. fprintf (stderr, "ioctl(SIOCGIFINDEX) on interface `%.*s' failed: %s\n",
  1470. IFNAMSIZ, dev->iface, strerror (errno));
  1471. return 1;
  1472. }
  1473. /* lookup the hardware type */
  1474. memset (&sll, 0, sizeof (sll));
  1475. sll.sll_family = AF_PACKET;
  1476. sll.sll_ifindex = ifr.ifr_ifindex;
  1477. sll.sll_protocol = htons (ETH_P_ALL);
  1478. if (-1 == ioctl (dev->fd_raw, SIOCGIFHWADDR, &ifr))
  1479. {
  1480. fprintf (stderr, "ioctl(SIOCGIFHWADDR) on interface `%.*s' failed: %s\n",
  1481. IFNAMSIZ, dev->iface, strerror (errno));
  1482. return 1;
  1483. }
  1484. if (((ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211) &&
  1485. (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) &&
  1486. (ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211_PRISM) &&
  1487. (ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211_FULL)) )
  1488. {
  1489. fprintf (stderr, "Error: interface `%.*s' is not using a supported hardware address family (got %d)\n",
  1490. IFNAMSIZ, dev->iface,
  1491. ifr.ifr_hwaddr.sa_family);
  1492. return 1;
  1493. }
  1494. /* lookup iw mode */
  1495. memset (&wrq, 0, sizeof (struct iwreq));
  1496. strncpy (wrq.ifr_name, dev->iface, IFNAMSIZ);
  1497. if (-1 == ioctl (dev->fd_raw, SIOCGIWMODE, &wrq))
  1498. {
  1499. /* most probably not supported (ie for rtap ipw interface) *
  1500. * so just assume its correctly set... */
  1501. wrq.u.mode = IW_MODE_MONITOR;
  1502. }
  1503. if ( (wrq.u.mode != IW_MODE_MONITOR) &&
  1504. (wrq.u.mode != IW_MODE_ADHOC) )
  1505. {
  1506. fprintf (stderr, "Error: interface `%.*s' is not in monitor or ad-hoc mode (got %d)\n",
  1507. IFNAMSIZ, dev->iface,
  1508. wrq.u.mode);
  1509. return 1;
  1510. }
  1511. /* Is interface st to up, broadcast & running ? */
  1512. if ((ifr.ifr_flags | IFF_UP | IFF_BROADCAST | IFF_RUNNING) != ifr.ifr_flags)
  1513. {
  1514. /* Bring interface up */
  1515. ifr.ifr_flags |= IFF_UP | IFF_BROADCAST | IFF_RUNNING;
  1516. if (-1 == ioctl (dev->fd_raw, SIOCSIFFLAGS, &ifr))
  1517. {
  1518. fprintf (stderr, "ioctl(SIOCSIFFLAGS) on interface `%.*s' failed: %s\n",
  1519. IFNAMSIZ, dev->iface, strerror (errno));
  1520. return 1;
  1521. }
  1522. }
  1523. /* bind the raw socket to the interface */
  1524. if (-1 == bind (dev->fd_raw, (struct sockaddr *) &sll, sizeof (sll)))
  1525. {
  1526. fprintf (stderr, "Failed to bind interface `%.*s': %s\n", IFNAMSIZ,
  1527. dev->iface, strerror (errno));
  1528. return 1;
  1529. }
  1530. /* lookup the hardware type */
  1531. if (-1 == ioctl (dev->fd_raw, SIOCGIFHWADDR, &ifr))
  1532. {
  1533. fprintf (stderr, "ioctl(SIOCGIFHWADDR) on interface `%.*s' failed: %s\n",
  1534. IFNAMSIZ, dev->iface, strerror (errno));
  1535. return 1;
  1536. }
  1537. memcpy (&dev->pl_mac, ifr.ifr_hwaddr.sa_data, MAC_ADDR_SIZE);
  1538. dev->arptype_in = ifr.ifr_hwaddr.sa_family;
  1539. if ((ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) &&
  1540. (ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211) &&
  1541. (ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211_PRISM) &&
  1542. (ifr.ifr_hwaddr.sa_family != ARPHRD_IEEE80211_FULL))
  1543. {
  1544. fprintf (stderr, "Unsupported hardware link type %d on interface `%.*s'\n",
  1545. ifr.ifr_hwaddr.sa_family, IFNAMSIZ, dev->iface);
  1546. return 1;
  1547. }
  1548. /* enable promiscuous mode */
  1549. memset (&mr, 0, sizeof (mr));
  1550. mr.mr_ifindex = sll.sll_ifindex;
  1551. mr.mr_type = PACKET_MR_PROMISC;
  1552. if (0 !=
  1553. setsockopt (dev->fd_raw, SOL_PACKET, PACKET_ADD_MEMBERSHIP, &mr,
  1554. sizeof (mr)))
  1555. {
  1556. fprintf (stderr,
  1557. "Failed to enable promiscuous mode on interface `%.*s'\n",
  1558. IFNAMSIZ,
  1559. dev->iface);
  1560. return 1;
  1561. }
  1562. return 0;
  1563. }
  1564. /**
  1565. * Test if the given interface name really corresponds to a wireless
  1566. * device.
  1567. *
  1568. * @param iface name of the interface
  1569. * @return 0 on success, 1 on error
  1570. */
  1571. static int
  1572. test_wlan_interface (const char *iface)
  1573. {
  1574. char strbuf[512];
  1575. struct stat sbuf;
  1576. int ret;
  1577. ret = snprintf (strbuf, sizeof (strbuf),
  1578. "/sys/class/net/%s/phy80211/subsystem",
  1579. iface);
  1580. if ((ret < 0) || (ret >= sizeof (strbuf)) || (0 != stat (strbuf, &sbuf)))
  1581. {
  1582. fprintf (stderr,
  1583. "Did not find 802.11 interface `%s'. Exiting.\n",
  1584. iface);
  1585. exit (1);
  1586. }
  1587. return 0;
  1588. }
  1589. /**
  1590. * Test incoming packets mac for being our own.
  1591. *
  1592. * @param taIeeeHeader buffer of the packet
  1593. * @param dev the Hardware_Infos struct
  1594. * @return 0 if mac belongs to us, 1 if mac is for another target
  1595. */
  1596. static int
  1597. mac_test (const struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame *taIeeeHeader,
  1598. const struct HardwareInfos *dev)
  1599. {
  1600. static struct GNUNET_TRANSPORT_WLAN_MacAddress all_zeros;
  1601. if ( (0 == memcmp (&taIeeeHeader->addr3, &all_zeros, MAC_ADDR_SIZE)) ||
  1602. (0 == memcmp (&taIeeeHeader->addr1, &all_zeros, MAC_ADDR_SIZE)) )
  1603. return 0; /* some drivers set no Macs, then assume it is all for us! */
  1604. if (0 != memcmp (&taIeeeHeader->addr3, &mac_bssid_gnunet, MAC_ADDR_SIZE))
  1605. return 1; /* not a GNUnet ad-hoc package */
  1606. if ( (0 == memcmp (&taIeeeHeader->addr1, &dev->pl_mac, MAC_ADDR_SIZE)) ||
  1607. (0 == memcmp (&taIeeeHeader->addr1, &bc_all_mac, MAC_ADDR_SIZE)) )
  1608. return 0; /* for us, or broadcast */
  1609. return 1; /* not for us */
  1610. }
  1611. /**
  1612. * Set the wlan header to sane values to make attacks more difficult
  1613. *
  1614. * @param taIeeeHeader pointer to the header of the packet
  1615. * @param dev pointer to the Hardware_Infos struct
  1616. */
  1617. static void
  1618. mac_set (struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame *taIeeeHeader,
  1619. const struct HardwareInfos *dev)
  1620. {
  1621. taIeeeHeader->frame_control = htons (IEEE80211_FC0_TYPE_DATA);
  1622. taIeeeHeader->addr2 = dev->pl_mac;
  1623. taIeeeHeader->addr3 = mac_bssid_gnunet;
  1624. }
  1625. /**
  1626. * Process data from the stdin. Takes the message, prepends the
  1627. * radiotap transmission header, forces the sender MAC to be correct
  1628. * and puts it into our buffer for transmission to the kernel.
  1629. *
  1630. * @param cls pointer to the device struct ('struct HardwareInfos*')
  1631. * @param hdr pointer to the start of the packet
  1632. */
  1633. static void
  1634. stdin_send_hw (void *cls, const struct GNUNET_MessageHeader *hdr)
  1635. {
  1636. struct HardwareInfos *dev = cls;
  1637. const struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *header;
  1638. struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame *wlanheader;
  1639. size_t sendsize;
  1640. struct RadiotapTransmissionHeader rtheader;
  1641. struct GNUNET_TRANSPORT_WLAN_Ieee8023Frame etheader;
  1642. sendsize = ntohs (hdr->size);
  1643. if ( (sendsize <
  1644. sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage)) ||
  1645. (GNUNET_MESSAGE_TYPE_WLAN_DATA_TO_HELPER != ntohs (hdr->type)) )
  1646. {
  1647. fprintf (stderr, "Received malformed message\n");
  1648. exit (1);
  1649. }
  1650. sendsize -= (sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage) - sizeof (struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame));
  1651. if (MAXLINE < sendsize)
  1652. {
  1653. fprintf (stderr, "Packet too big for buffer\n");
  1654. exit (1);
  1655. }
  1656. header = (const struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *) hdr;
  1657. switch (dev->arptype_in)
  1658. {
  1659. case ARPHRD_IEEE80211_PRISM:
  1660. case ARPHRD_IEEE80211_FULL:
  1661. case ARPHRD_IEEE80211:
  1662. rtheader.header.it_version = 0;
  1663. rtheader.header.it_pad = 0;
  1664. rtheader.header.it_len = GNUNET_htole16 (sizeof (rtheader));
  1665. rtheader.header.it_present = GNUNET_htole16 (IEEE80211_RADIOTAP_OUR_TRANSMISSION_HEADER_MASK);
  1666. rtheader.rate = header->rate;
  1667. rtheader.pad1 = 0;
  1668. rtheader.txflags = GNUNET_htole16 (IEEE80211_RADIOTAP_F_TX_NOACK | IEEE80211_RADIOTAP_F_TX_NOSEQ);
  1669. memcpy (write_pout.buf, &rtheader, sizeof (rtheader));
  1670. memcpy (&write_pout.buf[sizeof (rtheader)], &header->frame, sendsize);
  1671. wlanheader = (struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame *) &write_pout.buf[sizeof (rtheader)];
  1672. /* payload contains MAC address, but we don't trust it, so we'll
  1673. * overwrite it with OUR MAC address to prevent mischief */
  1674. mac_set (wlanheader, dev);
  1675. write_pout.size = sendsize + sizeof (rtheader);
  1676. break;
  1677. case ARPHRD_ETHER:
  1678. etheader.dst = header->frame.addr1;
  1679. /* etheader.src = header->frame.addr2; --- untrusted input */
  1680. etheader.src = dev->pl_mac;
  1681. etheader.type = htons (ETH_P_IP);
  1682. memcpy (write_pout.buf, &etheader, sizeof (etheader));
  1683. memcpy (&write_pout.buf[sizeof (etheader)], &header[1], sendsize - sizeof (struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame));
  1684. write_pout.size = sendsize - sizeof (struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame) + sizeof (etheader);
  1685. break;
  1686. default:
  1687. fprintf (stderr,
  1688. "Unsupported ARPTYPE!\n");
  1689. break;
  1690. }
  1691. }
  1692. /**
  1693. * Main function of the helper. This code accesses a WLAN interface
  1694. * in monitoring mode (layer 2) and then forwards traffic in both
  1695. * directions between the WLAN interface and stdin/stdout of this
  1696. * process. Error messages are written to stdout.
  1697. *
  1698. * @param argc number of arguments, must be 2
  1699. * @param argv arguments only argument is the name of the interface (i.e. 'mon0')
  1700. * @return 0 on success (never happens, as we don't return unless aborted), 1 on error
  1701. */
  1702. int
  1703. main (int argc, char *argv[])
  1704. {
  1705. struct HardwareInfos dev;
  1706. char readbuf[MAXLINE];
  1707. int maxfd;
  1708. fd_set rfds;
  1709. fd_set wfds;
  1710. int stdin_open;
  1711. struct MessageStreamTokenizer *stdin_mst;
  1712. int raw_eno;
  1713. uid_t uid;
  1714. /* assert privs so we can modify the firewall rules! */
  1715. uid = getuid ();
  1716. #ifdef HAVE_SETRESUID
  1717. if (0 != setresuid (uid, 0, 0))
  1718. {
  1719. fprintf (stderr, "Failed to setresuid to root: %s\n", strerror (errno));
  1720. return 254;
  1721. }
  1722. #else
  1723. if (0 != seteuid (0))
  1724. {
  1725. fprintf (stderr, "Failed to seteuid back to root: %s\n", strerror (errno));
  1726. return 254;
  1727. }
  1728. #endif
  1729. /* make use of SGID capabilities on POSIX */
  1730. memset (&dev, 0, sizeof (dev));
  1731. dev.fd_raw = socket (PF_PACKET, SOCK_RAW, htons (ETH_P_ALL));
  1732. raw_eno = errno; /* remember for later */
  1733. /* now that we've dropped root rights, we can do error checking */
  1734. if (2 != argc)
  1735. {
  1736. fprintf (stderr,
  1737. "You must specify the name of the interface as the first and only argument to this program.\n");
  1738. if (-1 != dev.fd_raw)
  1739. (void) close (dev.fd_raw);
  1740. return 1;
  1741. }
  1742. if (-1 == dev.fd_raw)
  1743. {
  1744. fprintf (stderr, "Failed to create raw socket: %s\n", strerror (raw_eno));
  1745. return 1;
  1746. }
  1747. if (dev.fd_raw >= FD_SETSIZE)
  1748. {
  1749. fprintf (stderr, "File descriptor too large for select (%d > %d)\n",
  1750. dev.fd_raw, FD_SETSIZE);
  1751. (void) close (dev.fd_raw);
  1752. return 1;
  1753. }
  1754. if (0 != test_wlan_interface (argv[1]))
  1755. {
  1756. (void) close (dev.fd_raw);
  1757. return 1;
  1758. }
  1759. strncpy (dev.iface, argv[1], IFNAMSIZ);
  1760. if (0 != open_device_raw (&dev))
  1761. {
  1762. (void) close (dev.fd_raw);
  1763. return 1;
  1764. }
  1765. /* drop privs */
  1766. {
  1767. uid_t uid = getuid ();
  1768. #ifdef HAVE_SETRESUID
  1769. if (0 != setresuid (uid, uid, uid))
  1770. {
  1771. fprintf (stderr, "Failed to setresuid: %s\n", strerror (errno));
  1772. if (-1 != dev.fd_raw)
  1773. (void) close (dev.fd_raw);
  1774. return 1;
  1775. }
  1776. #else
  1777. if (0 != (setuid (uid) | seteuid (uid)))
  1778. {
  1779. fprintf (stderr, "Failed to setuid: %s\n", strerror (errno));
  1780. if (-1 != dev.fd_raw)
  1781. (void) close (dev.fd_raw);
  1782. return 1;
  1783. }
  1784. #endif
  1785. }
  1786. /* send MAC address of the WLAN interface to STDOUT first */
  1787. {
  1788. struct GNUNET_TRANSPORT_WLAN_HelperControlMessage macmsg;
  1789. macmsg.hdr.size = htons (sizeof (macmsg));
  1790. macmsg.hdr.type = htons (GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL);
  1791. memcpy (&macmsg.mac, &dev.pl_mac, sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress));
  1792. memcpy (write_std.buf, &macmsg, sizeof (macmsg));
  1793. write_std.size = sizeof (macmsg);
  1794. }
  1795. stdin_mst = mst_create (&stdin_send_hw, &dev);
  1796. stdin_open = 1;
  1797. while (1)
  1798. {
  1799. maxfd = -1;
  1800. FD_ZERO (&rfds);
  1801. if ((0 == write_pout.size) && (1 == stdin_open))
  1802. {
  1803. FD_SET (STDIN_FILENO, &rfds);
  1804. maxfd = MAX (maxfd, STDIN_FILENO);
  1805. }
  1806. if (0 == write_std.size)
  1807. {
  1808. FD_SET (dev.fd_raw, &rfds);
  1809. maxfd = MAX (maxfd, dev.fd_raw);
  1810. }
  1811. FD_ZERO (&wfds);
  1812. if (0 < write_std.size)
  1813. {
  1814. FD_SET (STDOUT_FILENO, &wfds);
  1815. maxfd = MAX (maxfd, STDOUT_FILENO);
  1816. }
  1817. if (0 < write_pout.size)
  1818. {
  1819. FD_SET (dev.fd_raw, &wfds);
  1820. maxfd = MAX (maxfd, dev.fd_raw);
  1821. }
  1822. {
  1823. int retval = select (maxfd + 1, &rfds, &wfds, NULL, NULL);
  1824. if ((-1 == retval) && (EINTR == errno))
  1825. continue;
  1826. if (0 > retval)
  1827. {
  1828. fprintf (stderr, "select failed: %s\n", strerror (errno));
  1829. break;
  1830. }
  1831. }
  1832. if (FD_ISSET (STDOUT_FILENO, &wfds))
  1833. {
  1834. ssize_t ret =
  1835. write (STDOUT_FILENO, write_std.buf + write_std.pos,
  1836. write_std.size - write_std.pos);
  1837. if (0 > ret)
  1838. {
  1839. fprintf (stderr, "Failed to write to STDOUT: %s\n", strerror (errno));
  1840. break;
  1841. }
  1842. write_std.pos += ret;
  1843. if (write_std.pos == write_std.size)
  1844. {
  1845. write_std.pos = 0;
  1846. write_std.size = 0;
  1847. }
  1848. }
  1849. if (FD_ISSET (dev.fd_raw, &wfds))
  1850. {
  1851. ssize_t ret =
  1852. write (dev.fd_raw, write_pout.buf + write_std.pos,
  1853. write_pout.size - write_pout.pos);
  1854. if (0 > ret)
  1855. {
  1856. fprintf (stderr, "Failed to write to WLAN device: %s\n",
  1857. strerror (errno));
  1858. break;
  1859. }
  1860. write_pout.pos += ret;
  1861. if ((write_pout.pos != write_pout.size) && (0 != ret))
  1862. {
  1863. /* we should not get partial sends with packet-oriented devices... */
  1864. fprintf (stderr, "Write error, partial send: %u/%u\n",
  1865. (unsigned int) write_pout.pos,
  1866. (unsigned int) write_pout.size);
  1867. break;
  1868. }
  1869. if (write_pout.pos == write_pout.size)
  1870. {
  1871. write_pout.pos = 0;
  1872. write_pout.size = 0;
  1873. }
  1874. }
  1875. if (FD_ISSET (STDIN_FILENO, &rfds))
  1876. {
  1877. ssize_t ret =
  1878. read (STDIN_FILENO, readbuf, sizeof (readbuf));
  1879. if (0 > ret)
  1880. {
  1881. fprintf (stderr, "Read error from STDIN: %s\n", strerror (errno));
  1882. break;
  1883. }
  1884. if (0 == ret)
  1885. {
  1886. /* stop reading... */
  1887. stdin_open = 0;
  1888. }
  1889. mst_receive (stdin_mst, readbuf, ret);
  1890. }
  1891. if (FD_ISSET (dev.fd_raw, &rfds))
  1892. {
  1893. struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *rrm;
  1894. ssize_t ret;
  1895. rrm = (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *) write_std.buf;
  1896. ret =
  1897. linux_read (&dev, (unsigned char *) &rrm->frame,
  1898. sizeof (write_std.buf)
  1899. - sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage)
  1900. + sizeof (struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame),
  1901. rrm);
  1902. if (0 > ret)
  1903. {
  1904. fprintf (stderr, "Read error from raw socket: %s\n", strerror (errno));
  1905. break;
  1906. }
  1907. if ((0 < ret) && (0 == mac_test (&rrm->frame, &dev)))
  1908. {
  1909. write_std.size = ret
  1910. + sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage)
  1911. - sizeof (struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame);
  1912. rrm->header.size = htons (write_std.size);
  1913. rrm->header.type = htons (GNUNET_MESSAGE_TYPE_WLAN_DATA_FROM_HELPER);
  1914. }
  1915. }
  1916. }
  1917. /* Error handling, try to clean up a bit at least */
  1918. mst_destroy (stdin_mst);
  1919. (void) close (dev.fd_raw);
  1920. return 1; /* we never exit 'normally' */
  1921. }
  1922. /* end of gnunet-helper-transport-wlan.c */