gnunet-daemon-exit.c 123 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2010-2013, 2017 Christian Grothoff
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file exit/gnunet-daemon-exit.c
  18. * @brief tool to allow IP traffic exit from the GNUnet cadet to the Internet
  19. * @author Philipp Toelke
  20. * @author Christian Grothoff
  21. *
  22. * TODO:
  23. * - test
  24. *
  25. * Design:
  26. * - which code should advertise services? the service model is right
  27. * now a bit odd, especially as this code DOES the exit and knows
  28. * the DNS "name", but OTOH this is clearly NOT the place to advertise
  29. * the service's existence; maybe the daemon should turn into a
  30. * service with an API to add local-exit services dynamically?
  31. */
  32. #include "platform.h"
  33. #include "gnunet_util_lib.h"
  34. #include "gnunet_protocols.h"
  35. #include "gnunet_applications.h"
  36. #include "gnunet_dht_service.h"
  37. #include "gnunet_cadet_service.h"
  38. #include "gnunet_dnsparser_lib.h"
  39. #include "gnunet_dnsstub_lib.h"
  40. #include "gnunet_statistics_service.h"
  41. #include "gnunet_constants.h"
  42. #include "gnunet_signatures.h"
  43. #include "gnunet_tun_lib.h"
  44. #include "gnunet_regex_service.h"
  45. #include "exit.h"
  46. #include "block_dns.h"
  47. /**
  48. * Maximum path compression length for cadet regex announcing for IPv4 address
  49. * based regex.
  50. */
  51. #define REGEX_MAX_PATH_LEN_IPV4 4
  52. /**
  53. * Maximum path compression length for cadet regex announcing for IPv6 address
  54. * based regex.
  55. */
  56. #define REGEX_MAX_PATH_LEN_IPV6 8
  57. /**
  58. * How frequently do we re-announce the regex for the exit?
  59. */
  60. #define REGEX_REFRESH_FREQUENCY GNUNET_TIME_relative_multiply ( \
  61. GNUNET_TIME_UNIT_MINUTES, 30)
  62. /**
  63. * How frequently do we re-announce the DNS exit in the DHT?
  64. */
  65. #define DHT_PUT_FREQUENCY GNUNET_TIME_relative_multiply ( \
  66. GNUNET_TIME_UNIT_MINUTES, 15)
  67. /**
  68. * How long do we typically sign the DNS exit advertisement for?
  69. */
  70. #define DNS_ADVERTISEMENT_TIMEOUT GNUNET_TIME_relative_multiply ( \
  71. GNUNET_TIME_UNIT_HOURS, 3)
  72. /**
  73. * Generic logging shorthand
  74. */
  75. #define LOG(kind, ...) \
  76. GNUNET_log_from (kind, "exit", __VA_ARGS__);
  77. /**
  78. * Information about an address.
  79. */
  80. struct SocketAddress
  81. {
  82. /**
  83. * AF_INET or AF_INET6.
  84. */
  85. int af;
  86. /**
  87. * Remote address information.
  88. */
  89. union
  90. {
  91. /**
  92. * Address, if af is AF_INET.
  93. */
  94. struct in_addr ipv4;
  95. /**
  96. * Address, if af is AF_INET6.
  97. */
  98. struct in6_addr ipv6;
  99. } address;
  100. /**
  101. * IPPROTO_TCP or IPPROTO_UDP;
  102. */
  103. uint8_t proto;
  104. /**
  105. * Remote port, in host byte order!
  106. */
  107. uint16_t port;
  108. };
  109. /**
  110. * This struct is saved into the services-hashmap to represent
  111. * a service this peer is specifically offering an exit for
  112. * (for a specific domain name).
  113. */
  114. struct LocalService
  115. {
  116. /**
  117. * Remote address to use for the service.
  118. */
  119. struct SocketAddress address;
  120. /**
  121. * Descriptor for the service (CADET port).
  122. */
  123. struct GNUNET_HashCode descriptor;
  124. /**
  125. * DNS name of the service.
  126. */
  127. char *name;
  128. /**
  129. * Open port with CADET.
  130. */
  131. struct GNUNET_CADET_Port *port;
  132. /**
  133. * #GNUNET_YES if this is a UDP service, otherwise TCP.
  134. */
  135. int16_t is_udp;
  136. };
  137. /**
  138. * Information we use to track a connection (the classical 6-tuple of
  139. * IP-version, protocol, source-IP, destination-IP, source-port and
  140. * destinatin-port.
  141. */
  142. struct RedirectInformation
  143. {
  144. /**
  145. * Address information for the other party (equivalent of the
  146. * arguments one would give to "connect").
  147. */
  148. struct SocketAddress remote_address;
  149. /**
  150. * Address information we used locally (AF and proto must match
  151. * "remote_address"). Equivalent of the arguments one would give to
  152. * "bind".
  153. */
  154. struct SocketAddress local_address;
  155. /*
  156. Note 1: additional information might be added here in the
  157. future to support protocols that require special handling,
  158. such as ftp/tftp
  159. Note 2: we might also sometimes not match on all components
  160. of the tuple, to support protocols where things do not always
  161. fully map.
  162. */
  163. };
  164. /**
  165. * This struct is saved into #connections_map to allow finding the
  166. * right channel given an IP packet from TUN. It is also associated
  167. * with the channel's closure so we can find it again for the next
  168. * message from the channel.
  169. */
  170. struct ChannelState
  171. {
  172. /**
  173. * Cadet channel that is used for this connection.
  174. */
  175. struct GNUNET_CADET_Channel *channel;
  176. /**
  177. * Who is the other end of this channel.
  178. * FIXME is this needed? Only used for debugging messages
  179. */
  180. struct GNUNET_PeerIdentity peer;
  181. /**
  182. * #GNUNET_NO if this is a channel for TCP/UDP,
  183. * #GNUNET_YES if this is a channel for DNS,
  184. * #GNUNET_SYSERR if the channel is not yet initialized.
  185. */
  186. int is_dns;
  187. union
  188. {
  189. struct
  190. {
  191. /**
  192. * Heap node for this state in the connections_heap.
  193. */
  194. struct GNUNET_CONTAINER_HeapNode *heap_node;
  195. /**
  196. * Key this state has in the #connections_map.
  197. */
  198. struct GNUNET_HashCode state_key;
  199. /**
  200. * Associated service record, or NULL for no service.
  201. */
  202. struct LocalService *serv;
  203. /**
  204. * Primary redirection information for this connection.
  205. */
  206. struct RedirectInformation ri;
  207. } tcp_udp;
  208. struct
  209. {
  210. /**
  211. * Socket we are using to transmit this request (must match if we receive
  212. * a response).
  213. */
  214. struct GNUNET_DNSSTUB_RequestSocket *rs;
  215. /**
  216. * Original DNS request ID as used by the client.
  217. */
  218. uint16_t original_id;
  219. /**
  220. * DNS request ID that we used for forwarding.
  221. */
  222. uint16_t my_id;
  223. } dns;
  224. } specifics;
  225. };
  226. /**
  227. * Return value from 'main'.
  228. */
  229. static int global_ret;
  230. /**
  231. * Handle to our regex announcement for IPv4.
  232. */
  233. static struct GNUNET_REGEX_Announcement *regex4;
  234. /**
  235. * Handle to our regex announcement for IPv4.
  236. */
  237. static struct GNUNET_REGEX_Announcement *regex6;
  238. /**
  239. * The handle to the configuration used throughout the process
  240. */
  241. static const struct GNUNET_CONFIGURATION_Handle *cfg;
  242. /**
  243. * The handle to the helper
  244. */
  245. static struct GNUNET_HELPER_Handle *helper_handle;
  246. /**
  247. * Arguments to the exit helper.
  248. */
  249. static char *exit_argv[8];
  250. /**
  251. * IPv6 address of our TUN interface.
  252. */
  253. static struct in6_addr exit_ipv6addr;
  254. /**
  255. * IPv6 prefix (0..127) from configuration file.
  256. */
  257. static unsigned long long ipv6prefix;
  258. /**
  259. * IPv4 address of our TUN interface.
  260. */
  261. static struct in_addr exit_ipv4addr;
  262. /**
  263. * IPv4 netmask of our TUN interface.
  264. */
  265. static struct in_addr exit_ipv4mask;
  266. /**
  267. * Statistics.
  268. */
  269. static struct GNUNET_STATISTICS_Handle *stats;
  270. /**
  271. * The handle to cadet
  272. */
  273. static struct GNUNET_CADET_Handle *cadet_handle;
  274. /**
  275. * This hashmaps contains the mapping from peer, service-descriptor,
  276. * source-port and destination-port to a struct ChannelState
  277. */
  278. static struct GNUNET_CONTAINER_MultiHashMap *connections_map;
  279. /**
  280. * Heap so we can quickly find "old" connections.
  281. */
  282. static struct GNUNET_CONTAINER_Heap *connections_heap;
  283. /**
  284. * If there are at least this many connections, old ones will be removed
  285. */
  286. static unsigned long long max_connections;
  287. /**
  288. * This hashmaps saves interesting things about the configured services
  289. */
  290. static struct GNUNET_CONTAINER_MultiHashMap *services;
  291. /**
  292. * Array of all open DNS requests from channels.
  293. */
  294. static struct ChannelState *channels[UINT16_MAX + 1];
  295. /**
  296. * Handle to the DNS Stub resolver.
  297. */
  298. static struct GNUNET_DNSSTUB_Context *dnsstub;
  299. /**
  300. * Handle for ongoing DHT PUT operations to advertise exit service.
  301. */
  302. static struct GNUNET_DHT_PutHandle *dht_put;
  303. /**
  304. * Handle to the DHT.
  305. */
  306. static struct GNUNET_DHT_Handle *dht;
  307. /**
  308. * Task for doing DHT PUTs to advertise exit service.
  309. */
  310. static struct GNUNET_SCHEDULER_Task *dht_task;
  311. /**
  312. * Advertisement message we put into the DHT to advertise us
  313. * as a DNS exit.
  314. */
  315. static struct GNUNET_DNS_Advertisement dns_advertisement;
  316. /**
  317. * Key we store the DNS advertismenet under.
  318. */
  319. static struct GNUNET_HashCode dht_put_key;
  320. /**
  321. * Private key for this peer.
  322. */
  323. static struct GNUNET_CRYPTO_EddsaPrivateKey *peer_key;
  324. /**
  325. * Port for DNS exit.
  326. */
  327. static struct GNUNET_CADET_Port *dns_port;
  328. /**
  329. * Port for IPv4 exit.
  330. */
  331. static struct GNUNET_CADET_Port *cadet_port4;
  332. /**
  333. * Port for IPv6 exit.
  334. */
  335. static struct GNUNET_CADET_Port *cadet_port6;
  336. /**
  337. * Are we an IPv4-exit?
  338. */
  339. static int ipv4_exit;
  340. /**
  341. * Are we an IPv6-exit?
  342. */
  343. static int ipv6_exit;
  344. /**
  345. * Do we support IPv4 at all on the TUN interface?
  346. */
  347. static int ipv4_enabled;
  348. /**
  349. * Do we support IPv6 at all on the TUN interface?
  350. */
  351. static int ipv6_enabled;
  352. GNUNET_NETWORK_STRUCT_BEGIN
  353. /**
  354. * Message with a DNS response.
  355. */
  356. struct DnsResponseMessage
  357. {
  358. /**
  359. * GNUnet header, of type #GNUNET_MESSAGE_TYPE_VPN_DNS_FROM_INTERNET
  360. */
  361. struct GNUNET_MessageHeader header;
  362. /**
  363. * DNS header.
  364. */
  365. struct GNUNET_TUN_DnsHeader dns;
  366. /* Followed by more DNS payload */
  367. };
  368. GNUNET_NETWORK_STRUCT_END
  369. /**
  370. * Callback called from DNSSTUB resolver when a resolution
  371. * succeeded.
  372. *
  373. * @param cls NULL
  374. * @param dns the response itself
  375. * @param r number of bytes in @a dns
  376. */
  377. static void
  378. process_dns_result (void *cls,
  379. const struct GNUNET_TUN_DnsHeader *dns,
  380. size_t r)
  381. {
  382. struct ChannelState *ts;
  383. struct GNUNET_MQ_Envelope *env;
  384. struct DnsResponseMessage *resp;
  385. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  386. "Processing DNS result from stub resolver\n");
  387. GNUNET_assert (NULL == cls);
  388. if (NULL == dns)
  389. return;
  390. /* Handle case that this is a reply to a request from a CADET DNS channel */
  391. ts = channels[dns->id];
  392. if (NULL == ts)
  393. return;
  394. LOG (GNUNET_ERROR_TYPE_DEBUG,
  395. "Got a response from the stub resolver for DNS request received via CADET!\n");
  396. channels[dns->id] = NULL;
  397. env = GNUNET_MQ_msg_extra (resp,
  398. r - sizeof(struct GNUNET_TUN_DnsHeader),
  399. GNUNET_MESSAGE_TYPE_VPN_DNS_FROM_INTERNET);
  400. GNUNET_memcpy (&resp->dns,
  401. dns,
  402. r);
  403. resp->dns.id = ts->specifics.dns.original_id;
  404. GNUNET_MQ_send (GNUNET_CADET_get_mq (ts->channel),
  405. env);
  406. }
  407. /**
  408. * Check a request via cadet to perform a DNS query.
  409. *
  410. * @param cls our `struct ChannelState *`
  411. * @param msg the actual message
  412. * @return #GNUNET_OK to keep the connection open,
  413. * #GNUNET_SYSERR to close it (signal serious error)
  414. */
  415. static int
  416. check_dns_request (void *cls,
  417. const struct DnsResponseMessage *msg)
  418. {
  419. struct ChannelState *ts = cls;
  420. if (NULL == dnsstub)
  421. {
  422. GNUNET_break (0);
  423. return GNUNET_SYSERR;
  424. }
  425. if (GNUNET_NO == ts->is_dns)
  426. {
  427. GNUNET_break_op (0);
  428. return GNUNET_SYSERR;
  429. }
  430. return GNUNET_OK;
  431. }
  432. /**
  433. * Process a request via cadet to perform a DNS query.
  434. *
  435. * @param cls our `struct ChannelState *`
  436. * @param msg the actual message
  437. */
  438. static void
  439. handle_dns_request (void *cls,
  440. const struct DnsResponseMessage *msg)
  441. {
  442. struct ChannelState *ts = cls;
  443. size_t mlen = ntohs (msg->header.size);
  444. size_t dlen = mlen - sizeof(struct GNUNET_MessageHeader);
  445. char buf[dlen] GNUNET_ALIGN;
  446. struct GNUNET_TUN_DnsHeader *dout;
  447. if (GNUNET_SYSERR == ts->is_dns)
  448. {
  449. /* channel is DNS from now on */
  450. ts->is_dns = GNUNET_YES;
  451. }
  452. ts->specifics.dns.original_id = msg->dns.id;
  453. if (channels[ts->specifics.dns.my_id] == ts)
  454. channels[ts->specifics.dns.my_id] = NULL;
  455. ts->specifics.dns.my_id = (uint16_t) GNUNET_CRYPTO_random_u32 (
  456. GNUNET_CRYPTO_QUALITY_WEAK,
  457. UINT16_MAX
  458. + 1);
  459. channels[ts->specifics.dns.my_id] = ts;
  460. GNUNET_memcpy (buf,
  461. &msg->dns,
  462. dlen);
  463. dout = (struct GNUNET_TUN_DnsHeader *) buf;
  464. dout->id = ts->specifics.dns.my_id;
  465. ts->specifics.dns.rs = GNUNET_DNSSTUB_resolve (dnsstub,
  466. buf,
  467. dlen,
  468. &process_dns_result,
  469. NULL);
  470. if (NULL == ts->specifics.dns.rs)
  471. {
  472. GNUNET_break_op (0);
  473. return;
  474. }
  475. GNUNET_CADET_receive_done (ts->channel);
  476. }
  477. /**
  478. * Given IP information about a connection, calculate the respective
  479. * hash we would use for the #connections_map.
  480. *
  481. * @param hash resulting hash
  482. * @param ri information about the connection
  483. */
  484. static void
  485. hash_redirect_info (struct GNUNET_HashCode *hash,
  486. const struct RedirectInformation *ri)
  487. {
  488. char *off;
  489. memset (hash,
  490. 0,
  491. sizeof(struct GNUNET_HashCode));
  492. /* the GNUnet hashmap only uses the first sizeof(unsigned int) of the hash,
  493. so we put the IP address in there (and hope for few collisions) */
  494. off = (char *) hash;
  495. switch (ri->remote_address.af)
  496. {
  497. case AF_INET:
  498. GNUNET_memcpy (off,
  499. &ri->remote_address.address.ipv4,
  500. sizeof(struct in_addr));
  501. off += sizeof(struct in_addr);
  502. break;
  503. case AF_INET6:
  504. GNUNET_memcpy (off,
  505. &ri->remote_address.address.ipv6,
  506. sizeof(struct in6_addr));
  507. off += sizeof(struct in_addr);
  508. break;
  509. default:
  510. GNUNET_assert (0);
  511. }
  512. GNUNET_memcpy (off,
  513. &ri->remote_address.port,
  514. sizeof(uint16_t));
  515. off += sizeof(uint16_t);
  516. switch (ri->local_address.af)
  517. {
  518. case AF_INET:
  519. GNUNET_memcpy (off,
  520. &ri->local_address.address.ipv4,
  521. sizeof(struct in_addr));
  522. off += sizeof(struct in_addr);
  523. break;
  524. case AF_INET6:
  525. GNUNET_memcpy (off,
  526. &ri->local_address.address.ipv6,
  527. sizeof(struct in6_addr));
  528. off += sizeof(struct in_addr);
  529. break;
  530. default:
  531. GNUNET_assert (0);
  532. }
  533. GNUNET_memcpy (off,
  534. &ri->local_address.port,
  535. sizeof(uint16_t));
  536. off += sizeof(uint16_t);
  537. GNUNET_memcpy (off,
  538. &ri->remote_address.proto,
  539. sizeof(uint8_t));
  540. /* off += sizeof (uint8_t); */
  541. }
  542. /**
  543. * Get our connection tracking state. Warns if it does not exists,
  544. * refreshes the timestamp if it does exist.
  545. *
  546. * @param af address family
  547. * @param protocol IPPROTO_UDP or IPPROTO_TCP
  548. * @param destination_ip target IP
  549. * @param destination_port target port
  550. * @param local_ip local IP
  551. * @param local_port local port
  552. * @param state_key set to hash's state if non-NULL
  553. * @return NULL if we have no tracking information for this tuple
  554. */
  555. static struct ChannelState *
  556. get_redirect_state (int af,
  557. int protocol,
  558. const void *destination_ip,
  559. uint16_t destination_port,
  560. const void *local_ip,
  561. uint16_t local_port,
  562. struct GNUNET_HashCode *state_key)
  563. {
  564. struct RedirectInformation ri;
  565. struct GNUNET_HashCode key;
  566. struct ChannelState *state;
  567. if (((af == AF_INET) && (protocol == IPPROTO_ICMP)) ||
  568. ((af == AF_INET6) && (protocol == IPPROTO_ICMPV6)))
  569. {
  570. /* ignore ports */
  571. destination_port = 0;
  572. local_port = 0;
  573. }
  574. ri.remote_address.af = af;
  575. if (af == AF_INET)
  576. ri.remote_address.address.ipv4 = *((struct in_addr*) destination_ip);
  577. else
  578. ri.remote_address.address.ipv6 = *((struct in6_addr*) destination_ip);
  579. ri.remote_address.port = destination_port;
  580. ri.remote_address.proto = protocol;
  581. ri.local_address.af = af;
  582. if (af == AF_INET)
  583. ri.local_address.address.ipv4 = *((struct in_addr*) local_ip);
  584. else
  585. ri.local_address.address.ipv6 = *((struct in6_addr*) local_ip);
  586. ri.local_address.port = local_port;
  587. ri.local_address.proto = protocol;
  588. hash_redirect_info (&key,
  589. &ri);
  590. if (NULL != state_key)
  591. *state_key = key;
  592. state = GNUNET_CONTAINER_multihashmap_get (connections_map,
  593. &key);
  594. if (NULL == state)
  595. return NULL;
  596. /* Mark this connection as freshly used */
  597. if (NULL == state_key)
  598. GNUNET_CONTAINER_heap_update_cost (state->specifics.tcp_udp.heap_node,
  599. GNUNET_TIME_absolute_get ().abs_value_us);
  600. return state;
  601. }
  602. /**
  603. * Check a request via cadet to send a request to a TCP service
  604. * offered by this system.
  605. *
  606. * @param cls our `struct ChannelState *`
  607. * @param start the actual message
  608. * @return #GNUNET_OK to keep the connection open,
  609. * #GNUNET_SYSERR to close it (signal serious error)
  610. */
  611. static int
  612. check_tcp_service (void *cls,
  613. const struct GNUNET_EXIT_TcpServiceStartMessage *start)
  614. {
  615. struct ChannelState *state = cls;
  616. if (NULL == state)
  617. {
  618. GNUNET_break_op (0);
  619. return GNUNET_SYSERR;
  620. }
  621. if (GNUNET_YES == state->is_dns)
  622. {
  623. GNUNET_break_op (0);
  624. return GNUNET_SYSERR;
  625. }
  626. if (NULL == state->specifics.tcp_udp.serv)
  627. {
  628. GNUNET_break_op (0);
  629. return GNUNET_SYSERR;
  630. }
  631. if (NULL != state->specifics.tcp_udp.heap_node)
  632. {
  633. GNUNET_break_op (0);
  634. return GNUNET_SYSERR;
  635. }
  636. if (start->tcp_header.off * 4 < sizeof(struct GNUNET_TUN_TcpHeader))
  637. {
  638. GNUNET_break_op (0);
  639. return GNUNET_SYSERR;
  640. }
  641. return GNUNET_OK;
  642. }
  643. /**
  644. * Prepare an IPv4 packet for transmission via the TUN interface.
  645. * Initializes the IP header and calculates checksums (IP+UDP/TCP).
  646. * For UDP, the UDP header will be fully created, whereas for TCP
  647. * only the ports and checksum will be filled in. So for TCP,
  648. * a skeleton TCP header must be part of the provided payload.
  649. *
  650. * @param payload payload of the packet (starting with UDP payload or
  651. * TCP header, depending on protocol)
  652. * @param payload_length number of bytes in @a payload
  653. * @param protocol IPPROTO_UDP or IPPROTO_TCP
  654. * @param tcp_header skeleton of the TCP header, NULL for UDP
  655. * @param src_address source address to use (IP and port)
  656. * @param dst_address destination address to use (IP and port)
  657. * @param pkt4 where to write the assembled packet; must
  658. * contain enough space for the IP header, UDP/TCP header
  659. * AND the payload
  660. */
  661. static void
  662. prepare_ipv4_packet (const void *payload,
  663. size_t payload_length,
  664. int protocol,
  665. const struct GNUNET_TUN_TcpHeader *tcp_header,
  666. const struct SocketAddress *src_address,
  667. const struct SocketAddress *dst_address,
  668. struct GNUNET_TUN_IPv4Header *pkt4)
  669. {
  670. size_t len;
  671. len = payload_length;
  672. switch (protocol)
  673. {
  674. case IPPROTO_UDP:
  675. len += sizeof(struct GNUNET_TUN_UdpHeader);
  676. break;
  677. case IPPROTO_TCP:
  678. len += sizeof(struct GNUNET_TUN_TcpHeader);
  679. GNUNET_assert (NULL != tcp_header);
  680. break;
  681. default:
  682. GNUNET_break (0);
  683. return;
  684. }
  685. if (len + sizeof(struct GNUNET_TUN_IPv4Header) > UINT16_MAX)
  686. {
  687. GNUNET_break (0);
  688. return;
  689. }
  690. GNUNET_TUN_initialize_ipv4_header (pkt4,
  691. protocol,
  692. len,
  693. &src_address->address.ipv4,
  694. &dst_address->address.ipv4);
  695. switch (protocol)
  696. {
  697. case IPPROTO_UDP:
  698. {
  699. struct GNUNET_TUN_UdpHeader *pkt4_udp = (struct
  700. GNUNET_TUN_UdpHeader *) &pkt4[1];
  701. pkt4_udp->source_port = htons (src_address->port);
  702. pkt4_udp->destination_port = htons (dst_address->port);
  703. pkt4_udp->len = htons ((uint16_t) payload_length);
  704. GNUNET_TUN_calculate_udp4_checksum (pkt4,
  705. pkt4_udp,
  706. payload,
  707. payload_length);
  708. GNUNET_memcpy (&pkt4_udp[1],
  709. payload,
  710. payload_length);
  711. }
  712. break;
  713. case IPPROTO_TCP:
  714. {
  715. struct GNUNET_TUN_TcpHeader *pkt4_tcp = (struct
  716. GNUNET_TUN_TcpHeader *) &pkt4[1];
  717. *pkt4_tcp = *tcp_header;
  718. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  719. "Sending TCP packet from port %u to port %u\n",
  720. src_address->port,
  721. dst_address->port);
  722. pkt4_tcp->source_port = htons (src_address->port);
  723. pkt4_tcp->destination_port = htons (dst_address->port);
  724. GNUNET_TUN_calculate_tcp4_checksum (pkt4,
  725. pkt4_tcp,
  726. payload,
  727. payload_length);
  728. GNUNET_memcpy (&pkt4_tcp[1],
  729. payload,
  730. payload_length);
  731. }
  732. break;
  733. default:
  734. GNUNET_assert (0);
  735. }
  736. }
  737. /**
  738. * Prepare an IPv6 packet for transmission via the TUN interface.
  739. * Initializes the IP header and calculates checksums (IP+UDP/TCP).
  740. * For UDP, the UDP header will be fully created, whereas for TCP
  741. * only the ports and checksum will be filled in. So for TCP,
  742. * a skeleton TCP header must be part of the provided payload.
  743. *
  744. * @param payload payload of the packet (starting with UDP payload or
  745. * TCP header, depending on protocol)
  746. * @param payload_length number of bytes in @a payload
  747. * @param protocol IPPROTO_UDP or IPPROTO_TCP
  748. * @param tcp_header skeleton TCP header data to send, NULL for UDP
  749. * @param src_address source address to use (IP and port)
  750. * @param dst_address destination address to use (IP and port)
  751. * @param pkt6 where to write the assembled packet; must
  752. * contain enough space for the IP header, UDP/TCP header
  753. * AND the payload
  754. */
  755. static void
  756. prepare_ipv6_packet (const void *payload,
  757. size_t payload_length,
  758. int protocol,
  759. const struct GNUNET_TUN_TcpHeader *tcp_header,
  760. const struct SocketAddress *src_address,
  761. const struct SocketAddress *dst_address,
  762. struct GNUNET_TUN_IPv6Header *pkt6)
  763. {
  764. size_t len;
  765. len = payload_length;
  766. switch (protocol)
  767. {
  768. case IPPROTO_UDP:
  769. len += sizeof(struct GNUNET_TUN_UdpHeader);
  770. break;
  771. case IPPROTO_TCP:
  772. len += sizeof(struct GNUNET_TUN_TcpHeader);
  773. break;
  774. default:
  775. GNUNET_break (0);
  776. return;
  777. }
  778. if (len > UINT16_MAX)
  779. {
  780. GNUNET_break (0);
  781. return;
  782. }
  783. GNUNET_TUN_initialize_ipv6_header (pkt6,
  784. protocol,
  785. len,
  786. &src_address->address.ipv6,
  787. &dst_address->address.ipv6);
  788. switch (protocol)
  789. {
  790. case IPPROTO_UDP:
  791. {
  792. struct GNUNET_TUN_UdpHeader *pkt6_udp = (struct
  793. GNUNET_TUN_UdpHeader *) &pkt6[1];
  794. pkt6_udp->source_port = htons (src_address->port);
  795. pkt6_udp->destination_port = htons (dst_address->port);
  796. pkt6_udp->len = htons ((uint16_t) payload_length);
  797. GNUNET_TUN_calculate_udp6_checksum (pkt6,
  798. pkt6_udp,
  799. payload,
  800. payload_length);
  801. GNUNET_memcpy (&pkt6_udp[1],
  802. payload,
  803. payload_length);
  804. }
  805. break;
  806. case IPPROTO_TCP:
  807. {
  808. struct GNUNET_TUN_TcpHeader *pkt6_tcp = (struct
  809. GNUNET_TUN_TcpHeader *) &pkt6[1];
  810. /* GNUNET_memcpy first here as some TCP header fields are initialized this way! */
  811. *pkt6_tcp = *tcp_header;
  812. pkt6_tcp->source_port = htons (src_address->port);
  813. pkt6_tcp->destination_port = htons (dst_address->port);
  814. GNUNET_TUN_calculate_tcp6_checksum (pkt6,
  815. pkt6_tcp,
  816. payload,
  817. payload_length);
  818. GNUNET_memcpy (&pkt6_tcp[1],
  819. payload,
  820. payload_length);
  821. }
  822. break;
  823. default:
  824. GNUNET_assert (0);
  825. break;
  826. }
  827. }
  828. /**
  829. * Send a TCP packet via the TUN interface.
  830. *
  831. * @param destination_address IP and port to use for the TCP packet's destination
  832. * @param source_address IP and port to use for the TCP packet's source
  833. * @param tcp_header header template to use
  834. * @param payload payload of the TCP packet
  835. * @param payload_length number of bytes in @a payload
  836. */
  837. static void
  838. send_tcp_packet_via_tun (const struct SocketAddress *destination_address,
  839. const struct SocketAddress *source_address,
  840. const struct GNUNET_TUN_TcpHeader *tcp_header,
  841. const void *payload,
  842. size_t payload_length)
  843. {
  844. size_t len;
  845. GNUNET_STATISTICS_update (stats,
  846. gettext_noop ("# TCP packets sent via TUN"),
  847. 1,
  848. GNUNET_NO);
  849. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  850. "Sending packet with %u bytes TCP payload via TUN\n",
  851. (unsigned int) payload_length);
  852. len = sizeof(struct GNUNET_MessageHeader) + sizeof(struct
  853. GNUNET_TUN_Layer2PacketHeader);
  854. switch (source_address->af)
  855. {
  856. case AF_INET:
  857. len += sizeof(struct GNUNET_TUN_IPv4Header);
  858. break;
  859. case AF_INET6:
  860. len += sizeof(struct GNUNET_TUN_IPv6Header);
  861. break;
  862. default:
  863. GNUNET_break (0);
  864. return;
  865. }
  866. len += sizeof(struct GNUNET_TUN_TcpHeader);
  867. len += payload_length;
  868. if (len >= GNUNET_MAX_MESSAGE_SIZE)
  869. {
  870. GNUNET_break (0);
  871. return;
  872. }
  873. {
  874. char buf[len] GNUNET_ALIGN;
  875. struct GNUNET_MessageHeader *hdr;
  876. struct GNUNET_TUN_Layer2PacketHeader *tun;
  877. hdr = (struct GNUNET_MessageHeader *) buf;
  878. hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
  879. hdr->size = htons (len);
  880. tun = (struct GNUNET_TUN_Layer2PacketHeader*) &hdr[1];
  881. tun->flags = htons (0);
  882. switch (source_address->af)
  883. {
  884. case AF_INET:
  885. {
  886. struct GNUNET_TUN_IPv4Header *ipv4
  887. = (struct GNUNET_TUN_IPv4Header*) &tun[1];
  888. tun->proto = htons (ETH_P_IPV4);
  889. prepare_ipv4_packet (payload,
  890. payload_length,
  891. IPPROTO_TCP,
  892. tcp_header,
  893. source_address,
  894. destination_address,
  895. ipv4);
  896. }
  897. break;
  898. case AF_INET6:
  899. {
  900. struct GNUNET_TUN_IPv6Header *ipv6
  901. = (struct GNUNET_TUN_IPv6Header*) &tun[1];
  902. tun->proto = htons (ETH_P_IPV6);
  903. prepare_ipv6_packet (payload,
  904. payload_length,
  905. IPPROTO_TCP,
  906. tcp_header,
  907. source_address,
  908. destination_address,
  909. ipv6);
  910. }
  911. break;
  912. default:
  913. GNUNET_assert (0);
  914. break;
  915. }
  916. if (NULL != helper_handle)
  917. (void) GNUNET_HELPER_send (helper_handle,
  918. (const struct GNUNET_MessageHeader*) buf,
  919. GNUNET_YES,
  920. NULL,
  921. NULL);
  922. }
  923. }
  924. /**
  925. * Send an ICMP packet via the TUN interface.
  926. *
  927. * @param destination_address IP to use for the ICMP packet's destination
  928. * @param source_address IP to use for the ICMP packet's source
  929. * @param icmp_header ICMP header to send
  930. * @param payload payload of the ICMP packet (does NOT include ICMP header)
  931. * @param payload_length number of bytes of data in @a payload
  932. */
  933. static void
  934. send_icmp_packet_via_tun (const struct SocketAddress *destination_address,
  935. const struct SocketAddress *source_address,
  936. const struct GNUNET_TUN_IcmpHeader *icmp_header,
  937. const void *payload, size_t payload_length)
  938. {
  939. size_t len;
  940. struct GNUNET_TUN_IcmpHeader *icmp;
  941. GNUNET_STATISTICS_update (stats,
  942. gettext_noop ("# ICMP packets sent via TUN"),
  943. 1, GNUNET_NO);
  944. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  945. "Sending packet with %u bytes ICMP payload via TUN\n",
  946. (unsigned int) payload_length);
  947. len = sizeof(struct GNUNET_MessageHeader) + sizeof(struct
  948. GNUNET_TUN_Layer2PacketHeader);
  949. switch (destination_address->af)
  950. {
  951. case AF_INET:
  952. len += sizeof(struct GNUNET_TUN_IPv4Header);
  953. break;
  954. case AF_INET6:
  955. len += sizeof(struct GNUNET_TUN_IPv6Header);
  956. break;
  957. default:
  958. GNUNET_break (0);
  959. return;
  960. }
  961. len += sizeof(struct GNUNET_TUN_IcmpHeader);
  962. len += payload_length;
  963. if (len >= GNUNET_MAX_MESSAGE_SIZE)
  964. {
  965. GNUNET_break (0);
  966. return;
  967. }
  968. {
  969. char buf[len] GNUNET_ALIGN;
  970. struct GNUNET_MessageHeader *hdr;
  971. struct GNUNET_TUN_Layer2PacketHeader *tun;
  972. hdr = (struct GNUNET_MessageHeader *) buf;
  973. hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
  974. hdr->size = htons (len);
  975. tun = (struct GNUNET_TUN_Layer2PacketHeader*) &hdr[1];
  976. tun->flags = htons (0);
  977. switch (source_address->af)
  978. {
  979. case AF_INET:
  980. {
  981. struct GNUNET_TUN_IPv4Header *ipv4 = (struct
  982. GNUNET_TUN_IPv4Header*) &tun[1];
  983. tun->proto = htons (ETH_P_IPV4);
  984. GNUNET_TUN_initialize_ipv4_header (ipv4,
  985. IPPROTO_ICMP,
  986. sizeof(struct
  987. GNUNET_TUN_IcmpHeader)
  988. + payload_length,
  989. &source_address->address.ipv4,
  990. &destination_address->address.ipv4);
  991. icmp = (struct GNUNET_TUN_IcmpHeader*) &ipv4[1];
  992. }
  993. break;
  994. case AF_INET6:
  995. {
  996. struct GNUNET_TUN_IPv6Header *ipv6 = (struct
  997. GNUNET_TUN_IPv6Header*) &tun[1];
  998. tun->proto = htons (ETH_P_IPV6);
  999. GNUNET_TUN_initialize_ipv6_header (ipv6,
  1000. IPPROTO_ICMPV6,
  1001. sizeof(struct
  1002. GNUNET_TUN_IcmpHeader)
  1003. + payload_length,
  1004. &source_address->address.ipv6,
  1005. &destination_address->address.ipv6);
  1006. icmp = (struct GNUNET_TUN_IcmpHeader*) &ipv6[1];
  1007. }
  1008. break;
  1009. default:
  1010. GNUNET_assert (0);
  1011. break;
  1012. }
  1013. *icmp = *icmp_header;
  1014. GNUNET_memcpy (&icmp[1],
  1015. payload,
  1016. payload_length);
  1017. GNUNET_TUN_calculate_icmp_checksum (icmp,
  1018. payload,
  1019. payload_length);
  1020. if (NULL != helper_handle)
  1021. (void) GNUNET_HELPER_send (helper_handle,
  1022. (const struct GNUNET_MessageHeader*) buf,
  1023. GNUNET_YES,
  1024. NULL, NULL);
  1025. }
  1026. }
  1027. /**
  1028. * We need to create a (unique) fresh local address (IP+port).
  1029. * Fill one in.
  1030. *
  1031. * @param af desired address family
  1032. * @param proto desired protocol (IPPROTO_UDP or IPPROTO_TCP)
  1033. * @param local_address address to initialize
  1034. */
  1035. static void
  1036. setup_fresh_address (int af,
  1037. uint8_t proto,
  1038. struct SocketAddress *local_address)
  1039. {
  1040. local_address->af = af;
  1041. local_address->proto = (uint8_t) proto;
  1042. /* default "local" port range is often 32768--61000,
  1043. so we pick a random value in that range */
  1044. if (((af == AF_INET) && (proto == IPPROTO_ICMP)) ||
  1045. ((af == AF_INET6) && (proto == IPPROTO_ICMPV6)))
  1046. local_address->port = 0;
  1047. else
  1048. local_address->port
  1049. = (uint16_t) 32768 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
  1050. 28232);
  1051. switch (af)
  1052. {
  1053. case AF_INET:
  1054. {
  1055. struct in_addr addr;
  1056. struct in_addr mask;
  1057. struct in_addr rnd;
  1058. addr = exit_ipv4addr;
  1059. mask = exit_ipv4mask;
  1060. if (0 == ~mask.s_addr)
  1061. {
  1062. /* only one valid IP anyway */
  1063. local_address->address.ipv4 = addr;
  1064. return;
  1065. }
  1066. /* Given 192.168.0.1/255.255.0.0, we want a mask
  1067. of '192.168.255.255', thus: */
  1068. mask.s_addr = addr.s_addr | ~mask.s_addr;
  1069. /* Pick random IPv4 address within the subnet, except 'addr' or 'mask' itself */
  1070. do
  1071. {
  1072. rnd.s_addr = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
  1073. UINT32_MAX);
  1074. local_address->address.ipv4.s_addr = (addr.s_addr | rnd.s_addr)
  1075. & mask.s_addr;
  1076. }
  1077. while ((local_address->address.ipv4.s_addr == addr.s_addr) ||
  1078. (local_address->address.ipv4.s_addr == mask.s_addr));
  1079. }
  1080. break;
  1081. case AF_INET6:
  1082. {
  1083. struct in6_addr addr;
  1084. struct in6_addr mask;
  1085. struct in6_addr rnd;
  1086. int i;
  1087. addr = exit_ipv6addr;
  1088. GNUNET_assert (ipv6prefix < 128);
  1089. if (ipv6prefix == 127)
  1090. {
  1091. /* only one valid IP anyway */
  1092. local_address->address.ipv6 = addr;
  1093. return;
  1094. }
  1095. /* Given ABCD::/96, we want a mask of 'ABCD::FFFF:FFFF,
  1096. thus: */
  1097. mask = addr;
  1098. for (i = 127; i >= ipv6prefix; i--)
  1099. mask.s6_addr[i / 8] |= (1 << (i % 8));
  1100. /* Pick random IPv6 address within the subnet, except 'addr' or 'mask' itself */
  1101. do
  1102. {
  1103. for (i = 0; i < 16; i++)
  1104. {
  1105. rnd.s6_addr[i] = (unsigned char) GNUNET_CRYPTO_random_u32 (
  1106. GNUNET_CRYPTO_QUALITY_WEAK,
  1107. 256);
  1108. local_address->address.ipv6.s6_addr[i]
  1109. = (addr.s6_addr[i] | rnd.s6_addr[i]) & mask.s6_addr[i];
  1110. }
  1111. }
  1112. while ((0 == GNUNET_memcmp (&local_address->address.ipv6,
  1113. &addr)) ||
  1114. (0 == GNUNET_memcmp (&local_address->address.ipv6,
  1115. &mask)));
  1116. }
  1117. break;
  1118. default:
  1119. GNUNET_assert (0);
  1120. }
  1121. }
  1122. /**
  1123. * We are starting a fresh connection (TCP or UDP) and need
  1124. * to pick a source port and IP address (within the correct
  1125. * range and address family) to associate replies with the
  1126. * connection / correct cadet channel. This function generates
  1127. * a "fresh" source IP and source port number for a connection
  1128. * After picking a good source address, this function sets up
  1129. * the state in the 'connections_map' and 'connections_heap'
  1130. * to allow finding the state when needed later. The function
  1131. * also makes sure that we remain within memory limits by
  1132. * cleaning up 'old' states.
  1133. *
  1134. * @param state skeleton state to setup a record for; should
  1135. * 'state->specifics.tcp_udp.ri.remote_address' filled in so that
  1136. * this code can determine which AF/protocol is
  1137. * going to be used (the 'channel' should also
  1138. * already be set); after calling this function,
  1139. * heap_node and the local_address will be
  1140. * also initialized (heap_node != NULL can be
  1141. * used to test if a state has been fully setup).
  1142. */
  1143. static void
  1144. setup_state_record (struct ChannelState *state)
  1145. {
  1146. struct GNUNET_HashCode key;
  1147. struct ChannelState *s;
  1148. /* generate fresh, unique address */
  1149. do
  1150. {
  1151. if (NULL == state->specifics.tcp_udp.serv)
  1152. setup_fresh_address (state->specifics.tcp_udp.ri.remote_address.af,
  1153. state->specifics.tcp_udp.ri.remote_address.proto,
  1154. &state->specifics.tcp_udp.ri.local_address);
  1155. else
  1156. setup_fresh_address (state->specifics.tcp_udp.serv->address.af,
  1157. state->specifics.tcp_udp.serv->address.proto,
  1158. &state->specifics.tcp_udp.ri.local_address);
  1159. }
  1160. while (NULL !=
  1161. get_redirect_state (state->specifics.tcp_udp.ri.remote_address.af,
  1162. state->specifics.tcp_udp.ri.remote_address.proto,
  1163. &state->specifics.tcp_udp.ri.remote_address.address,
  1164. state->specifics.tcp_udp.ri.remote_address.port,
  1165. &state->specifics.tcp_udp.ri.local_address.address,
  1166. state->specifics.tcp_udp.ri.local_address.port,
  1167. &key));
  1168. {
  1169. char buf[INET6_ADDRSTRLEN];
  1170. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1171. "Picked local address %s:%u for new connection\n",
  1172. inet_ntop (state->specifics.tcp_udp.ri.local_address.af,
  1173. &state->specifics.tcp_udp.ri.local_address.address,
  1174. buf,
  1175. sizeof(buf)),
  1176. (unsigned int) state->specifics.tcp_udp.ri.local_address.port);
  1177. }
  1178. state->specifics.tcp_udp.state_key = key;
  1179. GNUNET_assert (GNUNET_OK ==
  1180. GNUNET_CONTAINER_multihashmap_put (connections_map,
  1181. &key, state,
  1182. GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
  1183. state->specifics.tcp_udp.heap_node
  1184. = GNUNET_CONTAINER_heap_insert (connections_heap,
  1185. state,
  1186. GNUNET_TIME_absolute_get ().abs_value_us);
  1187. while (GNUNET_CONTAINER_heap_get_size (connections_heap) > max_connections)
  1188. {
  1189. s = GNUNET_CONTAINER_heap_remove_root (connections_heap);
  1190. GNUNET_assert (state != s);
  1191. s->specifics.tcp_udp.heap_node = NULL;
  1192. GNUNET_CADET_channel_destroy (s->channel);
  1193. GNUNET_assert (GNUNET_OK ==
  1194. GNUNET_CONTAINER_multihashmap_remove (connections_map,
  1195. &s->specifics.tcp_udp.
  1196. state_key,
  1197. s));
  1198. GNUNET_free (s);
  1199. }
  1200. }
  1201. /**
  1202. * Send a UDP packet via the TUN interface.
  1203. *
  1204. * @param destination_address IP and port to use for the UDP packet's destination
  1205. * @param source_address IP and port to use for the UDP packet's source
  1206. * @param payload payload of the UDP packet (does NOT include UDP header)
  1207. * @param payload_length number of bytes of data in @a payload
  1208. */
  1209. static void
  1210. send_udp_packet_via_tun (const struct SocketAddress *destination_address,
  1211. const struct SocketAddress *source_address,
  1212. const void *payload, size_t payload_length)
  1213. {
  1214. size_t len;
  1215. GNUNET_STATISTICS_update (stats,
  1216. gettext_noop ("# UDP packets sent via TUN"),
  1217. 1, GNUNET_NO);
  1218. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1219. "Sending packet with %u bytes UDP payload via TUN\n",
  1220. (unsigned int) payload_length);
  1221. len = sizeof(struct GNUNET_MessageHeader) + sizeof(struct
  1222. GNUNET_TUN_Layer2PacketHeader);
  1223. switch (source_address->af)
  1224. {
  1225. case AF_INET:
  1226. len += sizeof(struct GNUNET_TUN_IPv4Header);
  1227. break;
  1228. case AF_INET6:
  1229. len += sizeof(struct GNUNET_TUN_IPv6Header);
  1230. break;
  1231. default:
  1232. GNUNET_break (0);
  1233. return;
  1234. }
  1235. len += sizeof(struct GNUNET_TUN_UdpHeader);
  1236. len += payload_length;
  1237. if (len >= GNUNET_MAX_MESSAGE_SIZE)
  1238. {
  1239. GNUNET_break (0);
  1240. return;
  1241. }
  1242. {
  1243. char buf[len] GNUNET_ALIGN;
  1244. struct GNUNET_MessageHeader *hdr;
  1245. struct GNUNET_TUN_Layer2PacketHeader *tun;
  1246. hdr = (struct GNUNET_MessageHeader *) buf;
  1247. hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
  1248. hdr->size = htons (len);
  1249. tun = (struct GNUNET_TUN_Layer2PacketHeader*) &hdr[1];
  1250. tun->flags = htons (0);
  1251. switch (source_address->af)
  1252. {
  1253. case AF_INET:
  1254. {
  1255. struct GNUNET_TUN_IPv4Header *ipv4 = (struct
  1256. GNUNET_TUN_IPv4Header*) &tun[1];
  1257. tun->proto = htons (ETH_P_IPV4);
  1258. prepare_ipv4_packet (payload,
  1259. payload_length,
  1260. IPPROTO_UDP,
  1261. NULL,
  1262. source_address,
  1263. destination_address,
  1264. ipv4);
  1265. }
  1266. break;
  1267. case AF_INET6:
  1268. {
  1269. struct GNUNET_TUN_IPv6Header *ipv6 = (struct
  1270. GNUNET_TUN_IPv6Header*) &tun[1];
  1271. tun->proto = htons (ETH_P_IPV6);
  1272. prepare_ipv6_packet (payload,
  1273. payload_length,
  1274. IPPROTO_UDP,
  1275. NULL,
  1276. source_address,
  1277. destination_address,
  1278. ipv6);
  1279. }
  1280. break;
  1281. default:
  1282. GNUNET_assert (0);
  1283. break;
  1284. }
  1285. if (NULL != helper_handle)
  1286. (void) GNUNET_HELPER_send (helper_handle,
  1287. (const struct GNUNET_MessageHeader*) buf,
  1288. GNUNET_YES,
  1289. NULL, NULL);
  1290. }
  1291. }
  1292. /**
  1293. * Check a request to forward UDP data to the Internet via this peer.
  1294. *
  1295. * @param cls our `struct ChannelState *`
  1296. * @param msg the actual message
  1297. * @return #GNUNET_OK to keep the connection open,
  1298. * #GNUNET_SYSERR to close it (signal serious error)
  1299. */
  1300. static int
  1301. check_udp_remote (void *cls,
  1302. const struct GNUNET_EXIT_UdpInternetMessage *msg)
  1303. {
  1304. struct ChannelState *state = cls;
  1305. if (GNUNET_YES == state->is_dns)
  1306. {
  1307. GNUNET_break_op (0);
  1308. return GNUNET_SYSERR;
  1309. }
  1310. return GNUNET_OK;
  1311. }
  1312. /**
  1313. * Process a request to forward UDP data to the Internet via this peer.
  1314. *
  1315. * @param cls our `struct ChannelState *`
  1316. * @param msg the actual message
  1317. */
  1318. static void
  1319. handle_udp_remote (void *cls,
  1320. const struct GNUNET_EXIT_UdpInternetMessage *msg)
  1321. {
  1322. struct ChannelState *state = cls;
  1323. uint16_t pkt_len = ntohs (msg->header.size) - sizeof(struct
  1324. GNUNET_EXIT_UdpInternetMessage);
  1325. const struct in_addr *v4;
  1326. const struct in6_addr *v6;
  1327. const void *payload;
  1328. int af;
  1329. if (GNUNET_SYSERR == state->is_dns)
  1330. {
  1331. /* channel is UDP/TCP from now on */
  1332. state->is_dns = GNUNET_NO;
  1333. }
  1334. GNUNET_STATISTICS_update (stats,
  1335. gettext_noop ("# Bytes received from CADET"),
  1336. pkt_len, GNUNET_NO);
  1337. GNUNET_STATISTICS_update (stats,
  1338. gettext_noop (
  1339. "# UDP IP-exit requests received via cadet"),
  1340. 1, GNUNET_NO);
  1341. af = (int) ntohl (msg->af);
  1342. state->specifics.tcp_udp.ri.remote_address.af = af;
  1343. switch (af)
  1344. {
  1345. case AF_INET:
  1346. if (pkt_len < sizeof(struct in_addr))
  1347. {
  1348. GNUNET_break_op (0);
  1349. return;
  1350. }
  1351. if (! ipv4_exit)
  1352. {
  1353. GNUNET_break_op (0);
  1354. return;
  1355. }
  1356. v4 = (const struct in_addr*) &msg[1];
  1357. payload = &v4[1];
  1358. pkt_len -= sizeof(struct in_addr);
  1359. state->specifics.tcp_udp.ri.remote_address.address.ipv4 = *v4;
  1360. break;
  1361. case AF_INET6:
  1362. if (pkt_len < sizeof(struct in6_addr))
  1363. {
  1364. GNUNET_break_op (0);
  1365. return;
  1366. }
  1367. if (! ipv6_exit)
  1368. {
  1369. GNUNET_break_op (0);
  1370. return;
  1371. }
  1372. v6 = (const struct in6_addr*) &msg[1];
  1373. payload = &v6[1];
  1374. pkt_len -= sizeof(struct in6_addr);
  1375. state->specifics.tcp_udp.ri.remote_address.address.ipv6 = *v6;
  1376. break;
  1377. default:
  1378. GNUNET_break_op (0);
  1379. return;
  1380. }
  1381. {
  1382. char buf[INET6_ADDRSTRLEN];
  1383. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1384. "Received data from %s for forwarding to UDP %s:%u\n",
  1385. GNUNET_i2s (&state->peer),
  1386. inet_ntop (af,
  1387. &state->specifics.tcp_udp.ri.remote_address.address,
  1388. buf, sizeof(buf)),
  1389. (unsigned int) ntohs (msg->destination_port));
  1390. }
  1391. state->specifics.tcp_udp.ri.remote_address.proto = IPPROTO_UDP;
  1392. state->specifics.tcp_udp.ri.remote_address.port = msg->destination_port;
  1393. if (NULL == state->specifics.tcp_udp.heap_node)
  1394. setup_state_record (state);
  1395. if (0 != ntohs (msg->source_port))
  1396. state->specifics.tcp_udp.ri.local_address.port = msg->source_port;
  1397. send_udp_packet_via_tun (&state->specifics.tcp_udp.ri.remote_address,
  1398. &state->specifics.tcp_udp.ri.local_address,
  1399. payload,
  1400. pkt_len);
  1401. GNUNET_CADET_receive_done (state->channel);
  1402. }
  1403. /**
  1404. * Check a request via cadet to send a request to a UDP service
  1405. * offered by this system.
  1406. *
  1407. * @param cls our `struct ChannelState *`
  1408. * @param msg the actual message
  1409. * @return #GNUNET_OK to keep the connection open,
  1410. * #GNUNET_SYSERR to close it (signal serious error)
  1411. */
  1412. static int
  1413. check_udp_service (void *cls,
  1414. const struct GNUNET_EXIT_UdpServiceMessage *msg)
  1415. {
  1416. struct ChannelState *state = cls;
  1417. if (NULL == state->specifics.tcp_udp.serv)
  1418. {
  1419. GNUNET_break_op (0);
  1420. return GNUNET_SYSERR;
  1421. }
  1422. return GNUNET_OK;
  1423. }
  1424. /**
  1425. * Process a request via cadet to send a request to a UDP service
  1426. * offered by this system.
  1427. *
  1428. * @param cls our `struct ChannelState *`
  1429. * @param msg the actual message
  1430. */
  1431. static void
  1432. handle_udp_service (void *cls,
  1433. const struct GNUNET_EXIT_UdpServiceMessage *msg)
  1434. {
  1435. struct ChannelState *state = cls;
  1436. uint16_t pkt_len = ntohs (msg->header.size) - sizeof(struct
  1437. GNUNET_EXIT_UdpServiceMessage);
  1438. GNUNET_STATISTICS_update (stats,
  1439. gettext_noop ("# Bytes received from CADET"),
  1440. pkt_len, GNUNET_NO);
  1441. GNUNET_STATISTICS_update (stats,
  1442. gettext_noop (
  1443. "# UDP service requests received via cadet"),
  1444. 1, GNUNET_NO);
  1445. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1446. "Received data from %s for forwarding to UDP service %s on port %u\n",
  1447. GNUNET_i2s (&state->peer),
  1448. GNUNET_h2s (&state->specifics.tcp_udp.serv->descriptor),
  1449. (unsigned int) ntohs (msg->destination_port));
  1450. setup_state_record (state);
  1451. if (0 != ntohs (msg->source_port))
  1452. state->specifics.tcp_udp.ri.local_address.port = msg->source_port;
  1453. send_udp_packet_via_tun (&state->specifics.tcp_udp.ri.remote_address,
  1454. &state->specifics.tcp_udp.ri.local_address,
  1455. &msg[1],
  1456. pkt_len);
  1457. GNUNET_CADET_receive_done (state->channel);
  1458. }
  1459. /**
  1460. * Process a request via cadet to send a request to a TCP service
  1461. * offered by this system.
  1462. *
  1463. * @param cls our `struct ChannelState *`
  1464. * @param start the actual message
  1465. * @return #GNUNET_OK to keep the connection open,
  1466. * #GNUNET_SYSERR to close it (signal serious error)
  1467. */
  1468. static void
  1469. handle_tcp_service (void *cls,
  1470. const struct GNUNET_EXIT_TcpServiceStartMessage *start)
  1471. {
  1472. struct ChannelState *state = cls;
  1473. uint16_t pkt_len = ntohs (start->header.size) - sizeof(struct
  1474. GNUNET_EXIT_TcpServiceStartMessage);
  1475. if (GNUNET_SYSERR == state->is_dns)
  1476. {
  1477. /* channel is UDP/TCP from now on */
  1478. state->is_dns = GNUNET_NO;
  1479. }
  1480. GNUNET_STATISTICS_update (stats,
  1481. gettext_noop (
  1482. "# TCP service creation requests received via cadet"),
  1483. 1,
  1484. GNUNET_NO);
  1485. GNUNET_STATISTICS_update (stats,
  1486. gettext_noop ("# Bytes received from CADET"),
  1487. pkt_len,
  1488. GNUNET_NO);
  1489. GNUNET_break_op (ntohl (start->reserved) == 0);
  1490. /* setup fresh connection */
  1491. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1492. "Received data from %s for forwarding to TCP service %s on port %u\n",
  1493. GNUNET_i2s (&state->peer),
  1494. GNUNET_h2s (&state->specifics.tcp_udp.serv->descriptor),
  1495. (unsigned int) ntohs (start->tcp_header.destination_port));
  1496. setup_state_record (state);
  1497. send_tcp_packet_via_tun (&state->specifics.tcp_udp.ri.remote_address,
  1498. &state->specifics.tcp_udp.ri.local_address,
  1499. &start->tcp_header,
  1500. &start[1],
  1501. pkt_len);
  1502. GNUNET_CADET_receive_done (state->channel);
  1503. }
  1504. /**
  1505. * Check a request to forward TCP data to the Internet via this peer.
  1506. *
  1507. * @param cls our `struct ChannelState *`
  1508. * @param start the actual message
  1509. * @return #GNUNET_OK to keep the connection open,
  1510. * #GNUNET_SYSERR to close it (signal serious error)
  1511. */
  1512. static int
  1513. check_tcp_remote (void *cls,
  1514. const struct GNUNET_EXIT_TcpInternetStartMessage *start)
  1515. {
  1516. struct ChannelState *state = cls;
  1517. if (NULL == state)
  1518. {
  1519. GNUNET_break_op (0);
  1520. return GNUNET_SYSERR;
  1521. }
  1522. if (GNUNET_YES == state->is_dns)
  1523. {
  1524. GNUNET_break_op (0);
  1525. return GNUNET_SYSERR;
  1526. }
  1527. if ((NULL != state->specifics.tcp_udp.serv) ||
  1528. (NULL != state->specifics.tcp_udp.heap_node))
  1529. {
  1530. GNUNET_break_op (0);
  1531. return GNUNET_SYSERR;
  1532. }
  1533. if (start->tcp_header.off * 4 < sizeof(struct GNUNET_TUN_TcpHeader))
  1534. {
  1535. GNUNET_break_op (0);
  1536. return GNUNET_SYSERR;
  1537. }
  1538. return GNUNET_OK;
  1539. }
  1540. /**
  1541. * Process a request to forward TCP data to the Internet via this peer.
  1542. *
  1543. * @param cls our `struct ChannelState *`
  1544. * @param start the actual message
  1545. */
  1546. static void
  1547. handle_tcp_remote (void *cls,
  1548. const struct GNUNET_EXIT_TcpInternetStartMessage *start)
  1549. {
  1550. struct ChannelState *state = cls;
  1551. uint16_t pkt_len = ntohs (start->header.size) - sizeof(struct
  1552. GNUNET_EXIT_TcpInternetStartMessage);
  1553. const struct in_addr *v4;
  1554. const struct in6_addr *v6;
  1555. const void *payload;
  1556. int af;
  1557. if (GNUNET_SYSERR == state->is_dns)
  1558. {
  1559. /* channel is UDP/TCP from now on */
  1560. state->is_dns = GNUNET_NO;
  1561. }
  1562. GNUNET_STATISTICS_update (stats,
  1563. gettext_noop ("# Bytes received from CADET"),
  1564. pkt_len, GNUNET_NO);
  1565. GNUNET_STATISTICS_update (stats,
  1566. gettext_noop (
  1567. "# TCP IP-exit creation requests received via cadet"),
  1568. 1, GNUNET_NO);
  1569. af = (int) ntohl (start->af);
  1570. state->specifics.tcp_udp.ri.remote_address.af = af;
  1571. switch (af)
  1572. {
  1573. case AF_INET:
  1574. if (pkt_len < sizeof(struct in_addr))
  1575. {
  1576. GNUNET_break_op (0);
  1577. return;
  1578. }
  1579. if (! ipv4_exit)
  1580. {
  1581. GNUNET_break_op (0);
  1582. return;
  1583. }
  1584. v4 = (const struct in_addr*) &start[1];
  1585. payload = &v4[1];
  1586. pkt_len -= sizeof(struct in_addr);
  1587. state->specifics.tcp_udp.ri.remote_address.address.ipv4 = *v4;
  1588. break;
  1589. case AF_INET6:
  1590. if (pkt_len < sizeof(struct in6_addr))
  1591. {
  1592. GNUNET_break_op (0);
  1593. return;
  1594. }
  1595. if (! ipv6_exit)
  1596. {
  1597. GNUNET_break_op (0);
  1598. return;
  1599. }
  1600. v6 = (const struct in6_addr*) &start[1];
  1601. payload = &v6[1];
  1602. pkt_len -= sizeof(struct in6_addr);
  1603. state->specifics.tcp_udp.ri.remote_address.address.ipv6 = *v6;
  1604. break;
  1605. default:
  1606. GNUNET_break_op (0);
  1607. return;
  1608. }
  1609. {
  1610. char buf[INET6_ADDRSTRLEN];
  1611. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1612. "Received payload from %s for existing TCP stream to %s:%u\n",
  1613. GNUNET_i2s (&state->peer),
  1614. inet_ntop (af,
  1615. &state->specifics.tcp_udp.ri.remote_address.address,
  1616. buf, sizeof(buf)),
  1617. (unsigned int) ntohs (start->tcp_header.destination_port));
  1618. }
  1619. state->specifics.tcp_udp.ri.remote_address.proto = IPPROTO_TCP;
  1620. state->specifics.tcp_udp.ri.remote_address.port = ntohs (
  1621. start->tcp_header.destination_port);
  1622. setup_state_record (state);
  1623. send_tcp_packet_via_tun (&state->specifics.tcp_udp.ri.remote_address,
  1624. &state->specifics.tcp_udp.ri.local_address,
  1625. &start->tcp_header,
  1626. payload,
  1627. pkt_len);
  1628. GNUNET_CADET_receive_done (state->channel);
  1629. }
  1630. /**
  1631. * Check a request to forward TCP data on an established
  1632. * connection via this peer.
  1633. *
  1634. * @param cls our `struct ChannelState *`
  1635. * @param message the actual message
  1636. * @return #GNUNET_OK to keep the connection open,
  1637. * #GNUNET_SYSERR to close it (signal serious error)
  1638. */
  1639. static int
  1640. check_tcp_data (void *cls,
  1641. const struct GNUNET_EXIT_TcpDataMessage *data)
  1642. {
  1643. struct ChannelState *state = cls;
  1644. if ((NULL == state) ||
  1645. (NULL == state->specifics.tcp_udp.heap_node))
  1646. {
  1647. /* connection should have been up! */
  1648. GNUNET_STATISTICS_update (stats,
  1649. gettext_noop (
  1650. "# TCP DATA requests dropped (no session)"),
  1651. 1, GNUNET_NO);
  1652. GNUNET_break_op (0);
  1653. return GNUNET_SYSERR;
  1654. }
  1655. if (data->tcp_header.off * 4 < sizeof(struct GNUNET_TUN_TcpHeader))
  1656. {
  1657. GNUNET_break_op (0);
  1658. return GNUNET_SYSERR;
  1659. }
  1660. if (GNUNET_YES == state->is_dns)
  1661. {
  1662. GNUNET_break_op (0);
  1663. return GNUNET_SYSERR;
  1664. }
  1665. return GNUNET_OK;
  1666. }
  1667. /**
  1668. * Process a request to forward TCP data on an established
  1669. * connection via this peer.
  1670. *
  1671. * @param cls our `struct ChannelState *`
  1672. * @param message the actual message
  1673. */
  1674. static void
  1675. handle_tcp_data (void *cls,
  1676. const struct GNUNET_EXIT_TcpDataMessage *data)
  1677. {
  1678. struct ChannelState *state = cls;
  1679. uint16_t pkt_len = ntohs (data->header.size) - sizeof(struct
  1680. GNUNET_EXIT_TcpDataMessage);
  1681. GNUNET_STATISTICS_update (stats,
  1682. gettext_noop ("# Bytes received from CADET"),
  1683. pkt_len, GNUNET_NO);
  1684. GNUNET_STATISTICS_update (stats,
  1685. gettext_noop (
  1686. "# TCP data requests received via cadet"),
  1687. 1, GNUNET_NO);
  1688. if (GNUNET_SYSERR == state->is_dns)
  1689. {
  1690. /* channel is UDP/TCP from now on */
  1691. state->is_dns = GNUNET_NO;
  1692. }
  1693. GNUNET_break_op (ntohl (data->reserved) == 0);
  1694. {
  1695. char buf[INET6_ADDRSTRLEN];
  1696. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1697. "Received additional %u bytes of data from %s for TCP stream to %s:%u\n",
  1698. pkt_len,
  1699. GNUNET_i2s (&state->peer),
  1700. inet_ntop (state->specifics.tcp_udp.ri.remote_address.af,
  1701. &state->specifics.tcp_udp.ri.remote_address.address,
  1702. buf, sizeof(buf)),
  1703. (unsigned int) state->specifics.tcp_udp.ri.remote_address.port);
  1704. }
  1705. send_tcp_packet_via_tun (&state->specifics.tcp_udp.ri.remote_address,
  1706. &state->specifics.tcp_udp.ri.local_address,
  1707. &data->tcp_header,
  1708. &data[1], pkt_len);
  1709. GNUNET_CADET_receive_done (state->channel);
  1710. }
  1711. /**
  1712. * Synthesize a plausible ICMP payload for an ICMPv4 error
  1713. * response on the given channel.
  1714. *
  1715. * @param state channel information
  1716. * @param ipp IPv6 header to fill in (ICMP payload)
  1717. * @param udp "UDP" header to fill in (ICMP payload); might actually
  1718. * also be the first 8 bytes of the TCP header
  1719. */
  1720. static void
  1721. make_up_icmpv4_payload (struct ChannelState *state,
  1722. struct GNUNET_TUN_IPv4Header *ipp,
  1723. struct GNUNET_TUN_UdpHeader *udp)
  1724. {
  1725. GNUNET_TUN_initialize_ipv4_header (ipp,
  1726. state->specifics.tcp_udp.ri.remote_address.
  1727. proto,
  1728. sizeof(struct GNUNET_TUN_TcpHeader),
  1729. &state->specifics.tcp_udp.ri.remote_address
  1730. .address.ipv4,
  1731. &state->specifics.tcp_udp.ri.local_address.
  1732. address.ipv4);
  1733. udp->source_port = htons (state->specifics.tcp_udp.ri.remote_address.port);
  1734. udp->destination_port = htons (
  1735. state->specifics.tcp_udp.ri.local_address.port);
  1736. udp->len = htons (0);
  1737. udp->crc = htons (0);
  1738. }
  1739. /**
  1740. * Synthesize a plausible ICMP payload for an ICMPv6 error
  1741. * response on the given channel.
  1742. *
  1743. * @param state channel information
  1744. * @param ipp IPv6 header to fill in (ICMP payload)
  1745. * @param udp "UDP" header to fill in (ICMP payload); might actually
  1746. * also be the first 8 bytes of the TCP header
  1747. */
  1748. static void
  1749. make_up_icmpv6_payload (struct ChannelState *state,
  1750. struct GNUNET_TUN_IPv6Header *ipp,
  1751. struct GNUNET_TUN_UdpHeader *udp)
  1752. {
  1753. GNUNET_TUN_initialize_ipv6_header (ipp,
  1754. state->specifics.tcp_udp.ri.remote_address.
  1755. proto,
  1756. sizeof(struct GNUNET_TUN_TcpHeader),
  1757. &state->specifics.tcp_udp.ri.remote_address
  1758. .address.ipv6,
  1759. &state->specifics.tcp_udp.ri.local_address.
  1760. address.ipv6);
  1761. udp->source_port = htons (state->specifics.tcp_udp.ri.remote_address.port);
  1762. udp->destination_port = htons (
  1763. state->specifics.tcp_udp.ri.local_address.port);
  1764. udp->len = htons (0);
  1765. udp->crc = htons (0);
  1766. }
  1767. /**
  1768. * Check a request to forward ICMP data to the Internet via this peer.
  1769. *
  1770. * @param cls our `struct ChannelState *`
  1771. * @param msg the actual message
  1772. * @return #GNUNET_OK to keep the connection open,
  1773. * #GNUNET_SYSERR to close it (signal serious error)
  1774. */
  1775. static int
  1776. check_icmp_remote (void *cls,
  1777. const struct GNUNET_EXIT_IcmpInternetMessage *msg)
  1778. {
  1779. struct ChannelState *state = cls;
  1780. if (GNUNET_YES == state->is_dns)
  1781. {
  1782. GNUNET_break_op (0);
  1783. return GNUNET_SYSERR;
  1784. }
  1785. return GNUNET_OK;
  1786. }
  1787. /**
  1788. * Process a request to forward ICMP data to the Internet via this peer.
  1789. *
  1790. * @param cls our `struct ChannelState *`
  1791. * @param msg the actual message
  1792. */
  1793. static void
  1794. handle_icmp_remote (void *cls,
  1795. const struct GNUNET_EXIT_IcmpInternetMessage *msg)
  1796. {
  1797. struct ChannelState *state = cls;
  1798. uint16_t pkt_len = ntohs (msg->header.size) - sizeof(struct
  1799. GNUNET_EXIT_IcmpInternetMessage);
  1800. const struct in_addr *v4;
  1801. const struct in6_addr *v6;
  1802. const void *payload;
  1803. char buf[sizeof(struct GNUNET_TUN_IPv6Header) + 8] GNUNET_ALIGN;
  1804. int af;
  1805. if (GNUNET_SYSERR == state->is_dns)
  1806. {
  1807. /* channel is UDP/TCP from now on */
  1808. state->is_dns = GNUNET_NO;
  1809. }
  1810. GNUNET_STATISTICS_update (stats,
  1811. gettext_noop ("# Bytes received from CADET"),
  1812. pkt_len, GNUNET_NO);
  1813. GNUNET_STATISTICS_update (stats,
  1814. gettext_noop (
  1815. "# ICMP IP-exit requests received via cadet"),
  1816. 1, GNUNET_NO);
  1817. af = (int) ntohl (msg->af);
  1818. if ((NULL != state->specifics.tcp_udp.heap_node) &&
  1819. (af != state->specifics.tcp_udp.ri.remote_address.af))
  1820. {
  1821. /* other peer switched AF on this channel; not allowed */
  1822. GNUNET_break_op (0);
  1823. return;
  1824. }
  1825. switch (af)
  1826. {
  1827. case AF_INET:
  1828. if (pkt_len < sizeof(struct in_addr))
  1829. {
  1830. GNUNET_break_op (0);
  1831. return;
  1832. }
  1833. if (! ipv4_exit)
  1834. {
  1835. GNUNET_break_op (0);
  1836. return;
  1837. }
  1838. v4 = (const struct in_addr*) &msg[1];
  1839. payload = &v4[1];
  1840. pkt_len -= sizeof(struct in_addr);
  1841. state->specifics.tcp_udp.ri.remote_address.address.ipv4 = *v4;
  1842. if (NULL == state->specifics.tcp_udp.heap_node)
  1843. {
  1844. state->specifics.tcp_udp.ri.remote_address.af = af;
  1845. state->specifics.tcp_udp.ri.remote_address.proto = IPPROTO_ICMP;
  1846. setup_state_record (state);
  1847. }
  1848. /* check that ICMP type is something we want to support
  1849. and possibly make up payload! */
  1850. switch (msg->icmp_header.type)
  1851. {
  1852. case GNUNET_TUN_ICMPTYPE_ECHO_REPLY:
  1853. case GNUNET_TUN_ICMPTYPE_ECHO_REQUEST:
  1854. break;
  1855. case GNUNET_TUN_ICMPTYPE_DESTINATION_UNREACHABLE:
  1856. case GNUNET_TUN_ICMPTYPE_SOURCE_QUENCH:
  1857. case GNUNET_TUN_ICMPTYPE_TIME_EXCEEDED:
  1858. if (0 != pkt_len)
  1859. {
  1860. GNUNET_break_op (0);
  1861. return;
  1862. }
  1863. /* make up payload */
  1864. {
  1865. struct GNUNET_TUN_IPv4Header *ipp = (struct
  1866. GNUNET_TUN_IPv4Header *) buf;
  1867. struct GNUNET_TUN_UdpHeader *udp = (struct
  1868. GNUNET_TUN_UdpHeader *) &ipp[1];
  1869. GNUNET_assert (8 == sizeof(struct GNUNET_TUN_UdpHeader));
  1870. pkt_len = sizeof(struct GNUNET_TUN_IPv4Header) + 8;
  1871. make_up_icmpv4_payload (state,
  1872. ipp,
  1873. udp);
  1874. payload = ipp;
  1875. }
  1876. break;
  1877. default:
  1878. GNUNET_break_op (0);
  1879. GNUNET_STATISTICS_update (stats,
  1880. gettext_noop (
  1881. "# ICMPv4 packets dropped (type not allowed)"),
  1882. 1, GNUNET_NO);
  1883. return;
  1884. }
  1885. /* end AF_INET */
  1886. break;
  1887. case AF_INET6:
  1888. if (pkt_len < sizeof(struct in6_addr))
  1889. {
  1890. GNUNET_break_op (0);
  1891. return;
  1892. }
  1893. if (! ipv6_exit)
  1894. {
  1895. GNUNET_break_op (0);
  1896. return;
  1897. }
  1898. v6 = (const struct in6_addr*) &msg[1];
  1899. payload = &v6[1];
  1900. pkt_len -= sizeof(struct in6_addr);
  1901. state->specifics.tcp_udp.ri.remote_address.address.ipv6 = *v6;
  1902. if (NULL == state->specifics.tcp_udp.heap_node)
  1903. {
  1904. state->specifics.tcp_udp.ri.remote_address.af = af;
  1905. state->specifics.tcp_udp.ri.remote_address.proto = IPPROTO_ICMPV6;
  1906. setup_state_record (state);
  1907. }
  1908. /* check that ICMP type is something we want to support
  1909. and possibly make up payload! */
  1910. switch (msg->icmp_header.type)
  1911. {
  1912. case GNUNET_TUN_ICMPTYPE6_ECHO_REPLY:
  1913. case GNUNET_TUN_ICMPTYPE6_ECHO_REQUEST:
  1914. break;
  1915. case GNUNET_TUN_ICMPTYPE6_DESTINATION_UNREACHABLE:
  1916. case GNUNET_TUN_ICMPTYPE6_PACKET_TOO_BIG:
  1917. case GNUNET_TUN_ICMPTYPE6_TIME_EXCEEDED:
  1918. case GNUNET_TUN_ICMPTYPE6_PARAMETER_PROBLEM:
  1919. if (0 != pkt_len)
  1920. {
  1921. GNUNET_break_op (0);
  1922. return;
  1923. }
  1924. /* make up payload */
  1925. {
  1926. struct GNUNET_TUN_IPv6Header *ipp = (struct
  1927. GNUNET_TUN_IPv6Header *) buf;
  1928. struct GNUNET_TUN_UdpHeader *udp = (struct
  1929. GNUNET_TUN_UdpHeader *) &ipp[1];
  1930. GNUNET_assert (8 == sizeof(struct GNUNET_TUN_UdpHeader));
  1931. pkt_len = sizeof(struct GNUNET_TUN_IPv6Header) + 8;
  1932. make_up_icmpv6_payload (state,
  1933. ipp,
  1934. udp);
  1935. payload = ipp;
  1936. }
  1937. break;
  1938. default:
  1939. GNUNET_break_op (0);
  1940. GNUNET_STATISTICS_update (stats,
  1941. gettext_noop (
  1942. "# ICMPv6 packets dropped (type not allowed)"),
  1943. 1, GNUNET_NO);
  1944. return;
  1945. }
  1946. /* end AF_INET6 */
  1947. break;
  1948. default:
  1949. /* bad AF */
  1950. GNUNET_break_op (0);
  1951. return;
  1952. }
  1953. {
  1954. char buf[INET6_ADDRSTRLEN];
  1955. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1956. "Received ICMP data from %s for forwarding to %s\n",
  1957. GNUNET_i2s (&state->peer),
  1958. inet_ntop (af,
  1959. &state->specifics.tcp_udp.ri.remote_address.address,
  1960. buf, sizeof(buf)));
  1961. }
  1962. send_icmp_packet_via_tun (&state->specifics.tcp_udp.ri.remote_address,
  1963. &state->specifics.tcp_udp.ri.local_address,
  1964. &msg->icmp_header,
  1965. payload, pkt_len);
  1966. GNUNET_CADET_receive_done (state->channel);
  1967. }
  1968. /**
  1969. * Setup ICMP payload for ICMP error messages. Called
  1970. * for both IPv4 and IPv6 addresses.
  1971. *
  1972. * @param state context for creating the IP Packet
  1973. * @param buf where to create the payload, has at least
  1974. * sizeof (struct GNUNET_TUN_IPv6Header) + 8 bytes
  1975. * @return number of bytes of payload we created in buf
  1976. */
  1977. static uint16_t
  1978. make_up_icmp_service_payload (struct ChannelState *state,
  1979. char *buf)
  1980. {
  1981. switch (state->specifics.tcp_udp.serv->address.af)
  1982. {
  1983. case AF_INET:
  1984. {
  1985. struct GNUNET_TUN_IPv4Header *ipv4;
  1986. struct GNUNET_TUN_UdpHeader *udp;
  1987. ipv4 = (struct GNUNET_TUN_IPv4Header *) buf;
  1988. udp = (struct GNUNET_TUN_UdpHeader *) &ipv4[1];
  1989. make_up_icmpv4_payload (state,
  1990. ipv4,
  1991. udp);
  1992. GNUNET_assert (8 == sizeof(struct GNUNET_TUN_UdpHeader));
  1993. return sizeof(struct GNUNET_TUN_IPv4Header) + 8;
  1994. }
  1995. break;
  1996. case AF_INET6:
  1997. {
  1998. struct GNUNET_TUN_IPv6Header *ipv6;
  1999. struct GNUNET_TUN_UdpHeader *udp;
  2000. ipv6 = (struct GNUNET_TUN_IPv6Header *) buf;
  2001. udp = (struct GNUNET_TUN_UdpHeader *) &ipv6[1];
  2002. make_up_icmpv6_payload (state,
  2003. ipv6,
  2004. udp);
  2005. GNUNET_assert (8 == sizeof(struct GNUNET_TUN_UdpHeader));
  2006. return sizeof(struct GNUNET_TUN_IPv6Header) + 8;
  2007. }
  2008. break;
  2009. default:
  2010. GNUNET_break (0);
  2011. }
  2012. return 0;
  2013. }
  2014. /**
  2015. * Check a request via cadet to send ICMP data to a service
  2016. * offered by this system.
  2017. *
  2018. * @param cls our `struct ChannelState *`
  2019. * @param msg the actual message
  2020. * @return #GNUNET_OK to keep the connection open,
  2021. * #GNUNET_SYSERR to close it (signal serious error)
  2022. */
  2023. static int
  2024. check_icmp_service (void *cls,
  2025. const struct GNUNET_EXIT_IcmpServiceMessage *msg)
  2026. {
  2027. struct ChannelState *state = cls;
  2028. if (GNUNET_YES == state->is_dns)
  2029. {
  2030. GNUNET_break_op (0);
  2031. return GNUNET_SYSERR;
  2032. }
  2033. if (NULL == state->specifics.tcp_udp.serv)
  2034. {
  2035. GNUNET_break_op (0);
  2036. return GNUNET_SYSERR;
  2037. }
  2038. return GNUNET_OK;
  2039. }
  2040. /**
  2041. * Process a request via cadet to send ICMP data to a service
  2042. * offered by this system.
  2043. *
  2044. * @param cls our `struct ChannelState *`
  2045. * @param msg the actual message
  2046. */
  2047. static void
  2048. handle_icmp_service (void *cls,
  2049. const struct GNUNET_EXIT_IcmpServiceMessage *msg)
  2050. {
  2051. struct ChannelState *state = cls;
  2052. uint16_t pkt_len = ntohs (msg->header.size) - sizeof(struct
  2053. GNUNET_EXIT_IcmpServiceMessage);
  2054. struct GNUNET_TUN_IcmpHeader icmp;
  2055. char buf[sizeof(struct GNUNET_TUN_IPv6Header) + 8] GNUNET_ALIGN;
  2056. const void *payload;
  2057. GNUNET_STATISTICS_update (stats,
  2058. gettext_noop ("# Bytes received from CADET"),
  2059. pkt_len, GNUNET_NO);
  2060. GNUNET_STATISTICS_update (stats,
  2061. gettext_noop (
  2062. "# ICMP service requests received via cadet"),
  2063. 1, GNUNET_NO);
  2064. /* check that we got at least a valid header */
  2065. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2066. "Received data from %s for forwarding to ICMP service %s\n",
  2067. GNUNET_i2s (&state->peer),
  2068. GNUNET_h2s (&state->specifics.tcp_udp.serv->descriptor));
  2069. icmp = msg->icmp_header;
  2070. payload = &msg[1];
  2071. state->specifics.tcp_udp.ri.remote_address
  2072. = state->specifics.tcp_udp.serv->address;
  2073. setup_state_record (state);
  2074. /* check that ICMP type is something we want to support,
  2075. perform ICMP PT if needed ans possibly make up payload */
  2076. switch (msg->af)
  2077. {
  2078. case AF_INET:
  2079. switch (msg->icmp_header.type)
  2080. {
  2081. case GNUNET_TUN_ICMPTYPE_ECHO_REPLY:
  2082. if (state->specifics.tcp_udp.serv->address.af == AF_INET6)
  2083. icmp.type = GNUNET_TUN_ICMPTYPE6_ECHO_REPLY;
  2084. break;
  2085. case GNUNET_TUN_ICMPTYPE_ECHO_REQUEST:
  2086. if (state->specifics.tcp_udp.serv->address.af == AF_INET6)
  2087. icmp.type = GNUNET_TUN_ICMPTYPE6_ECHO_REQUEST;
  2088. break;
  2089. case GNUNET_TUN_ICMPTYPE_DESTINATION_UNREACHABLE:
  2090. if (state->specifics.tcp_udp.serv->address.af == AF_INET6)
  2091. icmp.type = GNUNET_TUN_ICMPTYPE6_DESTINATION_UNREACHABLE;
  2092. if (0 != pkt_len)
  2093. {
  2094. GNUNET_break_op (0);
  2095. return;
  2096. }
  2097. payload = buf;
  2098. pkt_len = make_up_icmp_service_payload (state, buf);
  2099. break;
  2100. case GNUNET_TUN_ICMPTYPE_TIME_EXCEEDED:
  2101. if (state->specifics.tcp_udp.serv->address.af == AF_INET6)
  2102. icmp.type = GNUNET_TUN_ICMPTYPE6_TIME_EXCEEDED;
  2103. if (0 != pkt_len)
  2104. {
  2105. GNUNET_break_op (0);
  2106. return;
  2107. }
  2108. payload = buf;
  2109. pkt_len = make_up_icmp_service_payload (state, buf);
  2110. break;
  2111. case GNUNET_TUN_ICMPTYPE_SOURCE_QUENCH:
  2112. if (state->specifics.tcp_udp.serv->address.af == AF_INET6)
  2113. {
  2114. GNUNET_STATISTICS_update (stats,
  2115. gettext_noop (
  2116. "# ICMPv4 packets dropped (impossible PT to v6)"),
  2117. 1, GNUNET_NO);
  2118. return;
  2119. }
  2120. if (0 != pkt_len)
  2121. {
  2122. GNUNET_break_op (0);
  2123. return;
  2124. }
  2125. payload = buf;
  2126. pkt_len = make_up_icmp_service_payload (state, buf);
  2127. break;
  2128. default:
  2129. GNUNET_break_op (0);
  2130. GNUNET_STATISTICS_update (stats,
  2131. gettext_noop (
  2132. "# ICMPv4 packets dropped (type not allowed)"),
  2133. 1, GNUNET_NO);
  2134. return;
  2135. }
  2136. /* end of AF_INET */
  2137. break;
  2138. case AF_INET6:
  2139. switch (msg->icmp_header.type)
  2140. {
  2141. case GNUNET_TUN_ICMPTYPE6_ECHO_REPLY:
  2142. if (state->specifics.tcp_udp.serv->address.af == AF_INET)
  2143. icmp.type = GNUNET_TUN_ICMPTYPE_ECHO_REPLY;
  2144. break;
  2145. case GNUNET_TUN_ICMPTYPE6_ECHO_REQUEST:
  2146. if (state->specifics.tcp_udp.serv->address.af == AF_INET)
  2147. icmp.type = GNUNET_TUN_ICMPTYPE_ECHO_REQUEST;
  2148. break;
  2149. case GNUNET_TUN_ICMPTYPE6_DESTINATION_UNREACHABLE:
  2150. if (state->specifics.tcp_udp.serv->address.af == AF_INET)
  2151. icmp.type = GNUNET_TUN_ICMPTYPE_DESTINATION_UNREACHABLE;
  2152. if (0 != pkt_len)
  2153. {
  2154. GNUNET_break_op (0);
  2155. return;
  2156. }
  2157. payload = buf;
  2158. pkt_len = make_up_icmp_service_payload (state, buf);
  2159. break;
  2160. case GNUNET_TUN_ICMPTYPE6_TIME_EXCEEDED:
  2161. if (state->specifics.tcp_udp.serv->address.af == AF_INET)
  2162. icmp.type = GNUNET_TUN_ICMPTYPE_TIME_EXCEEDED;
  2163. if (0 != pkt_len)
  2164. {
  2165. GNUNET_break_op (0);
  2166. return;
  2167. }
  2168. payload = buf;
  2169. pkt_len = make_up_icmp_service_payload (state, buf);
  2170. break;
  2171. case GNUNET_TUN_ICMPTYPE6_PACKET_TOO_BIG:
  2172. case GNUNET_TUN_ICMPTYPE6_PARAMETER_PROBLEM:
  2173. if (state->specifics.tcp_udp.serv->address.af == AF_INET)
  2174. {
  2175. GNUNET_STATISTICS_update (stats,
  2176. gettext_noop (
  2177. "# ICMPv6 packets dropped (impossible PT to v4)"),
  2178. 1, GNUNET_NO);
  2179. return;
  2180. }
  2181. if (0 != pkt_len)
  2182. {
  2183. GNUNET_break_op (0);
  2184. return;
  2185. }
  2186. payload = buf;
  2187. pkt_len = make_up_icmp_service_payload (state, buf);
  2188. break;
  2189. default:
  2190. GNUNET_break_op (0);
  2191. GNUNET_STATISTICS_update (stats,
  2192. gettext_noop (
  2193. "# ICMPv6 packets dropped (type not allowed)"),
  2194. 1, GNUNET_NO);
  2195. return;
  2196. }
  2197. /* end of AF_INET6 */
  2198. break;
  2199. default:
  2200. GNUNET_break_op (0);
  2201. return;
  2202. }
  2203. send_icmp_packet_via_tun (&state->specifics.tcp_udp.ri.remote_address,
  2204. &state->specifics.tcp_udp.ri.local_address,
  2205. &icmp,
  2206. payload,
  2207. pkt_len);
  2208. GNUNET_CADET_receive_done (state->channel);
  2209. }
  2210. /**
  2211. * Free memory associated with a service record.
  2212. *
  2213. * @param cls unused
  2214. * @param key service descriptor
  2215. * @param value service record to free
  2216. * @return #GNUNET_OK
  2217. */
  2218. static int
  2219. free_service_record (void *cls,
  2220. const struct GNUNET_HashCode *key,
  2221. void *value)
  2222. {
  2223. struct LocalService *service = value;
  2224. GNUNET_assert (GNUNET_YES ==
  2225. GNUNET_CONTAINER_multihashmap_remove (services,
  2226. key,
  2227. service));
  2228. GNUNET_CADET_close_port (service->port);
  2229. GNUNET_free_non_null (service->name);
  2230. GNUNET_free (service);
  2231. return GNUNET_OK;
  2232. }
  2233. /**
  2234. * Callback from CADET for new channels.
  2235. *
  2236. * @param cls closure
  2237. * @param channel new handle to the channel
  2238. * @param initiator peer that started the channel
  2239. * @return initial channel context for the channel
  2240. */
  2241. static void *
  2242. new_service_channel (void *cls,
  2243. struct GNUNET_CADET_Channel *channel,
  2244. const struct GNUNET_PeerIdentity *initiator)
  2245. {
  2246. struct LocalService *ls = cls;
  2247. struct ChannelState *s = GNUNET_new (struct ChannelState);
  2248. s->peer = *initiator;
  2249. GNUNET_STATISTICS_update (stats,
  2250. gettext_noop ("# Inbound CADET channels created"),
  2251. 1,
  2252. GNUNET_NO);
  2253. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2254. "Received inbound channel from `%s'\n",
  2255. GNUNET_i2s (initiator));
  2256. s->channel = channel;
  2257. s->specifics.tcp_udp.serv = ls;
  2258. s->specifics.tcp_udp.ri.remote_address = ls->address;
  2259. return s;
  2260. }
  2261. /**
  2262. * Function called by cadet whenever an inbound channel is destroyed.
  2263. * Should clean up any associated state.
  2264. *
  2265. * @param cls our `struct ChannelState *`
  2266. * @param channel connection to the other end (henceforth invalid)
  2267. */
  2268. static void
  2269. clean_channel (void *cls,
  2270. const struct GNUNET_CADET_Channel *channel)
  2271. {
  2272. struct ChannelState *s = cls;
  2273. LOG (GNUNET_ERROR_TYPE_DEBUG,
  2274. "Channel destroyed\n");
  2275. if (GNUNET_SYSERR == s->is_dns)
  2276. {
  2277. GNUNET_free (s);
  2278. return;
  2279. }
  2280. if (GNUNET_YES == s->is_dns)
  2281. {
  2282. if (channels[s->specifics.dns.my_id] == s)
  2283. channels[s->specifics.dns.my_id] = NULL;
  2284. }
  2285. else
  2286. {
  2287. if (NULL != s->specifics.tcp_udp.heap_node)
  2288. {
  2289. GNUNET_assert (GNUNET_YES ==
  2290. GNUNET_CONTAINER_multihashmap_remove (connections_map,
  2291. &s->specifics.tcp_udp
  2292. .state_key,
  2293. s));
  2294. GNUNET_CONTAINER_heap_remove_node (s->specifics.tcp_udp.heap_node);
  2295. s->specifics.tcp_udp.heap_node = NULL;
  2296. }
  2297. }
  2298. GNUNET_free (s);
  2299. }
  2300. /**
  2301. * Given a service descriptor and a destination port, find the
  2302. * respective service entry.
  2303. *
  2304. * @param proto IPPROTO_TCP or IPPROTO_UDP
  2305. * @param name name of the service
  2306. * @param destination_port destination port
  2307. * @param service service information record to store (service->name will be set).
  2308. */
  2309. static void
  2310. store_service (int proto,
  2311. const char *name,
  2312. uint16_t destination_port,
  2313. struct LocalService *service)
  2314. {
  2315. struct GNUNET_MQ_MessageHandler handlers[] = {
  2316. GNUNET_MQ_hd_var_size (icmp_service,
  2317. GNUNET_MESSAGE_TYPE_VPN_ICMP_TO_SERVICE,
  2318. struct GNUNET_EXIT_IcmpServiceMessage,
  2319. service),
  2320. GNUNET_MQ_hd_var_size (udp_service,
  2321. GNUNET_MESSAGE_TYPE_VPN_UDP_TO_SERVICE,
  2322. struct GNUNET_EXIT_UdpServiceMessage,
  2323. service),
  2324. GNUNET_MQ_hd_var_size (tcp_service,
  2325. GNUNET_MESSAGE_TYPE_VPN_TCP_TO_SERVICE_START,
  2326. struct GNUNET_EXIT_TcpServiceStartMessage,
  2327. service),
  2328. GNUNET_MQ_hd_var_size (tcp_data,
  2329. GNUNET_MESSAGE_TYPE_VPN_TCP_DATA_TO_EXIT,
  2330. struct GNUNET_EXIT_TcpDataMessage,
  2331. service),
  2332. GNUNET_MQ_handler_end ()
  2333. };
  2334. struct GNUNET_HashCode cadet_port;
  2335. service->name = GNUNET_strdup (name);
  2336. GNUNET_TUN_service_name_to_hash (name,
  2337. &service->descriptor);
  2338. GNUNET_TUN_compute_service_cadet_port (&service->descriptor,
  2339. destination_port,
  2340. &cadet_port);
  2341. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2342. "Opening CADET port %s for SERVICE exit %s on port %u\n",
  2343. GNUNET_h2s (&cadet_port),
  2344. name,
  2345. (unsigned int) destination_port);
  2346. service->port = GNUNET_CADET_open_port (cadet_handle,
  2347. &cadet_port,
  2348. &new_service_channel,
  2349. service,
  2350. NULL,
  2351. &clean_channel,
  2352. handlers);
  2353. service->is_udp = (IPPROTO_UDP == proto);
  2354. if (GNUNET_OK !=
  2355. GNUNET_CONTAINER_multihashmap_put (services,
  2356. &cadet_port,
  2357. service,
  2358. GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
  2359. {
  2360. GNUNET_CADET_close_port (service->port);
  2361. GNUNET_free_non_null (service->name);
  2362. GNUNET_free (service);
  2363. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  2364. _ ("Got duplicate service records for `%s:%u'\n"),
  2365. name,
  2366. (unsigned int) destination_port);
  2367. }
  2368. }
  2369. /**
  2370. * Send the given packet via the cadet channel.
  2371. *
  2372. * @param s channel destination
  2373. * @param env message to queue
  2374. */
  2375. static void
  2376. send_packet_to_cadet_channel (struct ChannelState *s,
  2377. struct GNUNET_MQ_Envelope *env)
  2378. {
  2379. GNUNET_assert (NULL != s);
  2380. GNUNET_STATISTICS_update (stats,
  2381. gettext_noop (
  2382. "# Messages transmitted via cadet channels"),
  2383. 1,
  2384. GNUNET_NO);
  2385. GNUNET_MQ_send (GNUNET_CADET_get_mq (s->channel),
  2386. env);
  2387. }
  2388. /**
  2389. * @brief Handles an ICMP packet received from the helper.
  2390. *
  2391. * @param icmp A pointer to the Packet
  2392. * @param pktlen number of bytes in @a icmp
  2393. * @param af address family (AFINET or AF_INET6)
  2394. * @param destination_ip destination IP-address of the IP packet (should
  2395. * be our local address)
  2396. * @param source_ip original source IP-address of the IP packet (should
  2397. * be the original destination address)
  2398. */
  2399. static void
  2400. icmp_from_helper (const struct GNUNET_TUN_IcmpHeader *icmp,
  2401. size_t pktlen,
  2402. int af,
  2403. const void *destination_ip,
  2404. const void *source_ip)
  2405. {
  2406. struct ChannelState *state;
  2407. struct GNUNET_MQ_Envelope *env;
  2408. struct GNUNET_EXIT_IcmpToVPNMessage *i2v;
  2409. const struct GNUNET_TUN_IPv4Header *ipv4;
  2410. const struct GNUNET_TUN_IPv6Header *ipv6;
  2411. const struct GNUNET_TUN_UdpHeader *udp;
  2412. uint16_t source_port;
  2413. uint16_t destination_port;
  2414. uint8_t protocol;
  2415. {
  2416. char sbuf[INET6_ADDRSTRLEN];
  2417. char dbuf[INET6_ADDRSTRLEN];
  2418. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2419. "Received ICMP packet going from %s to %s\n",
  2420. inet_ntop (af,
  2421. source_ip,
  2422. sbuf, sizeof(sbuf)),
  2423. inet_ntop (af,
  2424. destination_ip,
  2425. dbuf, sizeof(dbuf)));
  2426. }
  2427. if (pktlen < sizeof(struct GNUNET_TUN_IcmpHeader))
  2428. {
  2429. /* blame kernel */
  2430. GNUNET_break (0);
  2431. return;
  2432. }
  2433. /* Find out if this is an ICMP packet in response to an existing
  2434. TCP/UDP packet and if so, figure out ports / protocol of the
  2435. existing session from the IP data in the ICMP payload */
  2436. source_port = 0;
  2437. destination_port = 0;
  2438. switch (af)
  2439. {
  2440. case AF_INET:
  2441. protocol = IPPROTO_ICMP;
  2442. switch (icmp->type)
  2443. {
  2444. case GNUNET_TUN_ICMPTYPE_ECHO_REPLY:
  2445. case GNUNET_TUN_ICMPTYPE_ECHO_REQUEST:
  2446. break;
  2447. case GNUNET_TUN_ICMPTYPE_DESTINATION_UNREACHABLE:
  2448. case GNUNET_TUN_ICMPTYPE_SOURCE_QUENCH:
  2449. case GNUNET_TUN_ICMPTYPE_TIME_EXCEEDED:
  2450. if (pktlen <
  2451. sizeof(struct GNUNET_TUN_IcmpHeader)
  2452. + sizeof(struct GNUNET_TUN_IPv4Header) + 8)
  2453. {
  2454. /* blame kernel */
  2455. GNUNET_break (0);
  2456. return;
  2457. }
  2458. ipv4 = (const struct GNUNET_TUN_IPv4Header *) &icmp[1];
  2459. protocol = ipv4->protocol;
  2460. /* could be TCP or UDP, but both have the ports in the right
  2461. place, so that doesn't matter here */
  2462. udp = (const struct GNUNET_TUN_UdpHeader *) &ipv4[1];
  2463. /* swap ports, as they are from the original message */
  2464. destination_port = ntohs (udp->source_port);
  2465. source_port = ntohs (udp->destination_port);
  2466. /* throw away ICMP payload, won't be useful for the other side anyway */
  2467. pktlen = sizeof(struct GNUNET_TUN_IcmpHeader);
  2468. break;
  2469. default:
  2470. GNUNET_STATISTICS_update (stats,
  2471. gettext_noop (
  2472. "# ICMPv4 packets dropped (type not allowed)"),
  2473. 1, GNUNET_NO);
  2474. return;
  2475. }
  2476. break;
  2477. case AF_INET6:
  2478. protocol = IPPROTO_ICMPV6;
  2479. switch (icmp->type)
  2480. {
  2481. case GNUNET_TUN_ICMPTYPE6_DESTINATION_UNREACHABLE:
  2482. case GNUNET_TUN_ICMPTYPE6_PACKET_TOO_BIG:
  2483. case GNUNET_TUN_ICMPTYPE6_TIME_EXCEEDED:
  2484. case GNUNET_TUN_ICMPTYPE6_PARAMETER_PROBLEM:
  2485. if (pktlen <
  2486. sizeof(struct GNUNET_TUN_IcmpHeader)
  2487. + sizeof(struct GNUNET_TUN_IPv6Header) + 8)
  2488. {
  2489. /* blame kernel */
  2490. GNUNET_break (0);
  2491. return;
  2492. }
  2493. ipv6 = (const struct GNUNET_TUN_IPv6Header *) &icmp[1];
  2494. protocol = ipv6->next_header;
  2495. /* could be TCP or UDP, but both have the ports in the right
  2496. place, so that doesn't matter here */
  2497. udp = (const struct GNUNET_TUN_UdpHeader *) &ipv6[1];
  2498. /* swap ports, as they are from the original message */
  2499. destination_port = ntohs (udp->source_port);
  2500. source_port = ntohs (udp->destination_port);
  2501. /* throw away ICMP payload, won't be useful for the other side anyway */
  2502. pktlen = sizeof(struct GNUNET_TUN_IcmpHeader);
  2503. break;
  2504. case GNUNET_TUN_ICMPTYPE6_ECHO_REQUEST:
  2505. case GNUNET_TUN_ICMPTYPE6_ECHO_REPLY:
  2506. break;
  2507. default:
  2508. GNUNET_STATISTICS_update (stats,
  2509. gettext_noop (
  2510. "# ICMPv6 packets dropped (type not allowed)"),
  2511. 1, GNUNET_NO);
  2512. return;
  2513. }
  2514. break;
  2515. default:
  2516. GNUNET_assert (0);
  2517. }
  2518. switch (protocol)
  2519. {
  2520. case IPPROTO_ICMP:
  2521. state = get_redirect_state (af,
  2522. IPPROTO_ICMP,
  2523. source_ip,
  2524. 0,
  2525. destination_ip,
  2526. 0,
  2527. NULL);
  2528. break;
  2529. case IPPROTO_ICMPV6:
  2530. state = get_redirect_state (af,
  2531. IPPROTO_ICMPV6,
  2532. source_ip,
  2533. 0,
  2534. destination_ip,
  2535. 0,
  2536. NULL);
  2537. break;
  2538. case IPPROTO_UDP:
  2539. state = get_redirect_state (af,
  2540. IPPROTO_UDP,
  2541. source_ip,
  2542. source_port,
  2543. destination_ip,
  2544. destination_port,
  2545. NULL);
  2546. break;
  2547. case IPPROTO_TCP:
  2548. state = get_redirect_state (af,
  2549. IPPROTO_TCP,
  2550. source_ip,
  2551. source_port,
  2552. destination_ip,
  2553. destination_port,
  2554. NULL);
  2555. break;
  2556. default:
  2557. GNUNET_STATISTICS_update (stats,
  2558. gettext_noop (
  2559. "# ICMP packets dropped (not allowed)"),
  2560. 1,
  2561. GNUNET_NO);
  2562. return;
  2563. }
  2564. if (NULL == state)
  2565. {
  2566. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  2567. _ (
  2568. "ICMP Packet dropped, have no matching connection information\n"));
  2569. return;
  2570. }
  2571. env = GNUNET_MQ_msg_extra (i2v,
  2572. pktlen - sizeof(struct GNUNET_TUN_IcmpHeader),
  2573. GNUNET_MESSAGE_TYPE_VPN_ICMP_TO_VPN);
  2574. i2v->af = htonl (af);
  2575. GNUNET_memcpy (&i2v->icmp_header,
  2576. icmp,
  2577. pktlen);
  2578. send_packet_to_cadet_channel (state,
  2579. env);
  2580. }
  2581. /**
  2582. * @brief Handles an UDP packet received from the helper.
  2583. *
  2584. * @param udp A pointer to the Packet
  2585. * @param pktlen number of bytes in 'udp'
  2586. * @param af address family (AFINET or AF_INET6)
  2587. * @param destination_ip destination IP-address of the IP packet (should
  2588. * be our local address)
  2589. * @param source_ip original source IP-address of the IP packet (should
  2590. * be the original destination address)
  2591. */
  2592. static void
  2593. udp_from_helper (const struct GNUNET_TUN_UdpHeader *udp,
  2594. size_t pktlen,
  2595. int af,
  2596. const void *destination_ip,
  2597. const void *source_ip)
  2598. {
  2599. struct ChannelState *state;
  2600. struct GNUNET_MQ_Envelope *env;
  2601. struct GNUNET_EXIT_UdpReplyMessage *urm;
  2602. {
  2603. char sbuf[INET6_ADDRSTRLEN];
  2604. char dbuf[INET6_ADDRSTRLEN];
  2605. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2606. "Received UDP packet going from %s:%u to %s:%u\n",
  2607. inet_ntop (af,
  2608. source_ip,
  2609. sbuf, sizeof(sbuf)),
  2610. (unsigned int) ntohs (udp->source_port),
  2611. inet_ntop (af,
  2612. destination_ip,
  2613. dbuf, sizeof(dbuf)),
  2614. (unsigned int) ntohs (udp->destination_port));
  2615. }
  2616. if (pktlen < sizeof(struct GNUNET_TUN_UdpHeader))
  2617. {
  2618. /* blame kernel */
  2619. GNUNET_break (0);
  2620. return;
  2621. }
  2622. if (pktlen != ntohs (udp->len))
  2623. {
  2624. /* blame kernel */
  2625. GNUNET_break (0);
  2626. return;
  2627. }
  2628. state = get_redirect_state (af,
  2629. IPPROTO_UDP,
  2630. source_ip,
  2631. ntohs (udp->source_port),
  2632. destination_ip,
  2633. ntohs (udp->destination_port),
  2634. NULL);
  2635. if (NULL == state)
  2636. {
  2637. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  2638. _ (
  2639. "UDP Packet dropped, have no matching connection information\n"));
  2640. return;
  2641. }
  2642. env = GNUNET_MQ_msg_extra (urm,
  2643. pktlen - sizeof(struct GNUNET_TUN_UdpHeader),
  2644. GNUNET_MESSAGE_TYPE_VPN_UDP_REPLY);
  2645. urm->source_port = htons (0);
  2646. urm->destination_port = htons (0);
  2647. GNUNET_memcpy (&urm[1],
  2648. &udp[1],
  2649. pktlen - sizeof(struct GNUNET_TUN_UdpHeader));
  2650. send_packet_to_cadet_channel (state,
  2651. env);
  2652. }
  2653. /**
  2654. * @brief Handles a TCP packet received from the helper.
  2655. *
  2656. * @param tcp A pointer to the Packet
  2657. * @param pktlen the length of the packet, including its TCP header
  2658. * @param af address family (AFINET or AF_INET6)
  2659. * @param destination_ip destination IP-address of the IP packet (should
  2660. * be our local address)
  2661. * @param source_ip original source IP-address of the IP packet (should
  2662. * be the original destination address)
  2663. */
  2664. static void
  2665. tcp_from_helper (const struct GNUNET_TUN_TcpHeader *tcp,
  2666. size_t pktlen,
  2667. int af,
  2668. const void *destination_ip,
  2669. const void *source_ip)
  2670. {
  2671. struct ChannelState *state;
  2672. char buf[pktlen] GNUNET_ALIGN;
  2673. struct GNUNET_TUN_TcpHeader *mtcp;
  2674. struct GNUNET_EXIT_TcpDataMessage *tdm;
  2675. struct GNUNET_MQ_Envelope *env;
  2676. size_t mlen;
  2677. {
  2678. char sbuf[INET6_ADDRSTRLEN];
  2679. char dbuf[INET6_ADDRSTRLEN];
  2680. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2681. "Received TCP packet with %u bytes going from %s:%u to %s:%u\n",
  2682. (unsigned int) (pktlen - sizeof(struct GNUNET_TUN_TcpHeader)),
  2683. inet_ntop (af,
  2684. source_ip,
  2685. sbuf, sizeof(sbuf)),
  2686. (unsigned int) ntohs (tcp->source_port),
  2687. inet_ntop (af,
  2688. destination_ip,
  2689. dbuf, sizeof(dbuf)),
  2690. (unsigned int) ntohs (tcp->destination_port));
  2691. }
  2692. if (pktlen < sizeof(struct GNUNET_TUN_TcpHeader))
  2693. {
  2694. /* blame kernel */
  2695. GNUNET_break (0);
  2696. return;
  2697. }
  2698. state = get_redirect_state (af,
  2699. IPPROTO_TCP,
  2700. source_ip,
  2701. ntohs (tcp->source_port),
  2702. destination_ip,
  2703. ntohs (tcp->destination_port),
  2704. NULL);
  2705. if (NULL == state)
  2706. {
  2707. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  2708. _ (
  2709. "TCP Packet dropped, have no matching connection information\n"));
  2710. return;
  2711. }
  2712. /* mug port numbers and crc to avoid information leakage;
  2713. sender will need to lookup the correct values anyway */
  2714. GNUNET_memcpy (buf, tcp, pktlen);
  2715. mtcp = (struct GNUNET_TUN_TcpHeader *) buf;
  2716. mtcp->source_port = 0;
  2717. mtcp->destination_port = 0;
  2718. mtcp->crc = 0;
  2719. mlen = sizeof(struct GNUNET_EXIT_TcpDataMessage) + (pktlen - sizeof(struct
  2720. GNUNET_TUN_TcpHeader));
  2721. if (mlen >= GNUNET_MAX_MESSAGE_SIZE)
  2722. {
  2723. GNUNET_break (0);
  2724. return;
  2725. }
  2726. env = GNUNET_MQ_msg_extra (tdm,
  2727. pktlen - sizeof(struct GNUNET_TUN_TcpHeader),
  2728. GNUNET_MESSAGE_TYPE_VPN_TCP_DATA_TO_VPN);
  2729. tdm->reserved = htonl (0);
  2730. GNUNET_memcpy (&tdm->tcp_header,
  2731. buf,
  2732. pktlen);
  2733. send_packet_to_cadet_channel (state,
  2734. env);
  2735. }
  2736. /**
  2737. * Receive packets from the helper-process
  2738. *
  2739. * @param cls unused
  2740. * @param message message received from helper
  2741. */
  2742. static int
  2743. message_token (void *cls GNUNET_UNUSED,
  2744. const struct GNUNET_MessageHeader *message)
  2745. {
  2746. const struct GNUNET_TUN_Layer2PacketHeader *pkt_tun;
  2747. size_t size;
  2748. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2749. "Got %u-byte message of type %u from gnunet-helper-exit\n",
  2750. ntohs (message->size),
  2751. ntohs (message->type));
  2752. GNUNET_STATISTICS_update (stats,
  2753. gettext_noop ("# Packets received from TUN"),
  2754. 1, GNUNET_NO);
  2755. if (ntohs (message->type) != GNUNET_MESSAGE_TYPE_VPN_HELPER)
  2756. {
  2757. GNUNET_break (0);
  2758. return GNUNET_OK;
  2759. }
  2760. size = ntohs (message->size);
  2761. if (size < sizeof(struct GNUNET_TUN_Layer2PacketHeader) + sizeof(struct
  2762. GNUNET_MessageHeader))
  2763. {
  2764. GNUNET_break (0);
  2765. return GNUNET_OK;
  2766. }
  2767. GNUNET_STATISTICS_update (stats,
  2768. gettext_noop ("# Bytes received from TUN"),
  2769. size, GNUNET_NO);
  2770. pkt_tun = (const struct GNUNET_TUN_Layer2PacketHeader *) &message[1];
  2771. size -= sizeof(struct GNUNET_TUN_Layer2PacketHeader) + sizeof(struct
  2772. GNUNET_MessageHeader);
  2773. switch (ntohs (pkt_tun->proto))
  2774. {
  2775. case ETH_P_IPV4:
  2776. {
  2777. const struct GNUNET_TUN_IPv4Header *pkt4;
  2778. if (size < sizeof(struct GNUNET_TUN_IPv4Header))
  2779. {
  2780. /* Kernel to blame? */
  2781. GNUNET_break (0);
  2782. return GNUNET_OK;
  2783. }
  2784. pkt4 = (const struct GNUNET_TUN_IPv4Header *) &pkt_tun[1];
  2785. if (size != ntohs (pkt4->total_length))
  2786. {
  2787. /* Kernel to blame? */
  2788. GNUNET_break (0);
  2789. return GNUNET_OK;
  2790. }
  2791. if (pkt4->header_length * 4 != sizeof(struct GNUNET_TUN_IPv4Header))
  2792. {
  2793. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  2794. _ ("IPv4 packet options received. Ignored.\n"));
  2795. return GNUNET_OK;
  2796. }
  2797. size -= sizeof(struct GNUNET_TUN_IPv4Header);
  2798. switch (pkt4->protocol)
  2799. {
  2800. case IPPROTO_UDP:
  2801. udp_from_helper ((const struct GNUNET_TUN_UdpHeader *) &pkt4[1], size,
  2802. AF_INET,
  2803. &pkt4->destination_address,
  2804. &pkt4->source_address);
  2805. break;
  2806. case IPPROTO_TCP:
  2807. tcp_from_helper ((const struct GNUNET_TUN_TcpHeader *) &pkt4[1], size,
  2808. AF_INET,
  2809. &pkt4->destination_address,
  2810. &pkt4->source_address);
  2811. break;
  2812. case IPPROTO_ICMP:
  2813. icmp_from_helper ((const struct GNUNET_TUN_IcmpHeader *) &pkt4[1], size,
  2814. AF_INET,
  2815. &pkt4->destination_address,
  2816. &pkt4->source_address);
  2817. break;
  2818. default:
  2819. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  2820. _ (
  2821. "IPv4 packet with unsupported next header %u received. Ignored.\n"),
  2822. (int) pkt4->protocol);
  2823. return GNUNET_OK;
  2824. }
  2825. }
  2826. break;
  2827. case ETH_P_IPV6:
  2828. {
  2829. const struct GNUNET_TUN_IPv6Header *pkt6;
  2830. if (size < sizeof(struct GNUNET_TUN_IPv6Header))
  2831. {
  2832. /* Kernel to blame? */
  2833. GNUNET_break (0);
  2834. return GNUNET_OK;
  2835. }
  2836. pkt6 = (struct GNUNET_TUN_IPv6Header *) &pkt_tun[1];
  2837. if (size != ntohs (pkt6->payload_length) + sizeof(struct
  2838. GNUNET_TUN_IPv6Header))
  2839. {
  2840. /* Kernel to blame? */
  2841. GNUNET_break (0);
  2842. return GNUNET_OK;
  2843. }
  2844. size -= sizeof(struct GNUNET_TUN_IPv6Header);
  2845. switch (pkt6->next_header)
  2846. {
  2847. case IPPROTO_UDP:
  2848. udp_from_helper ((const struct GNUNET_TUN_UdpHeader *) &pkt6[1], size,
  2849. AF_INET6,
  2850. &pkt6->destination_address,
  2851. &pkt6->source_address);
  2852. break;
  2853. case IPPROTO_TCP:
  2854. tcp_from_helper ((const struct GNUNET_TUN_TcpHeader *) &pkt6[1], size,
  2855. AF_INET6,
  2856. &pkt6->destination_address,
  2857. &pkt6->source_address);
  2858. break;
  2859. case IPPROTO_ICMPV6:
  2860. icmp_from_helper ((const struct GNUNET_TUN_IcmpHeader *) &pkt6[1], size,
  2861. AF_INET6,
  2862. &pkt6->destination_address,
  2863. &pkt6->source_address);
  2864. break;
  2865. default:
  2866. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  2867. _ (
  2868. "IPv6 packet with unsupported next header %d received. Ignored.\n"),
  2869. pkt6->next_header);
  2870. return GNUNET_OK;
  2871. }
  2872. }
  2873. break;
  2874. default:
  2875. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  2876. _ ("Packet from unknown protocol %u received. Ignored.\n"),
  2877. ntohs (pkt_tun->proto));
  2878. break;
  2879. }
  2880. return GNUNET_OK;
  2881. }
  2882. /**
  2883. * Callback from CADET for new channels.
  2884. *
  2885. * @param cls closure
  2886. * @param channel new handle to the channel
  2887. * @param initiator peer that started the channel
  2888. * @return initial channel context for the channel
  2889. */
  2890. static void *
  2891. new_channel (void *cls,
  2892. struct GNUNET_CADET_Channel *channel,
  2893. const struct GNUNET_PeerIdentity *initiator)
  2894. {
  2895. struct ChannelState *s = GNUNET_new (struct ChannelState);
  2896. s->is_dns = GNUNET_SYSERR;
  2897. s->peer = *initiator;
  2898. GNUNET_STATISTICS_update (stats,
  2899. gettext_noop ("# Inbound CADET channels created"),
  2900. 1,
  2901. GNUNET_NO);
  2902. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2903. "Received inbound channel from `%s'\n",
  2904. GNUNET_i2s (initiator));
  2905. s->channel = channel;
  2906. return s;
  2907. }
  2908. /**
  2909. * Function that frees everything from a hashmap
  2910. *
  2911. * @param cls unused
  2912. * @param hash key
  2913. * @param value value to free
  2914. */
  2915. static int
  2916. free_iterate (void *cls,
  2917. const struct GNUNET_HashCode *hash,
  2918. void *value)
  2919. {
  2920. GNUNET_free (value);
  2921. return GNUNET_YES;
  2922. }
  2923. /**
  2924. * Function scheduled as very last function if the service
  2925. * disabled itself because the helper is not installed
  2926. * properly. Does nothing, except for keeping the
  2927. * service process alive by virtue of being scheduled.
  2928. *
  2929. * @param cls NULL
  2930. * @param tc scheduler context
  2931. */
  2932. static void
  2933. dummy_task (void *cls)
  2934. {
  2935. /* just terminate */
  2936. }
  2937. /**
  2938. * Function scheduled as very last function, cleans up after us
  2939. *
  2940. * @param cls NULL
  2941. */
  2942. static void
  2943. cleanup (void *cls)
  2944. {
  2945. unsigned int i;
  2946. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2947. "Exit service is shutting down now\n");
  2948. if (NULL != helper_handle)
  2949. {
  2950. GNUNET_HELPER_stop (helper_handle, GNUNET_NO);
  2951. helper_handle = NULL;
  2952. }
  2953. if (NULL != regex4)
  2954. {
  2955. GNUNET_REGEX_announce_cancel (regex4);
  2956. regex4 = NULL;
  2957. }
  2958. if (NULL != regex6)
  2959. {
  2960. GNUNET_REGEX_announce_cancel (regex6);
  2961. regex6 = NULL;
  2962. }
  2963. if (NULL != services)
  2964. {
  2965. GNUNET_CONTAINER_multihashmap_iterate (services,
  2966. &free_service_record,
  2967. NULL);
  2968. GNUNET_CONTAINER_multihashmap_destroy (services);
  2969. }
  2970. if (NULL != dns_port)
  2971. {
  2972. GNUNET_CADET_close_port (dns_port);
  2973. dns_port = NULL;
  2974. }
  2975. if (NULL != cadet_port4)
  2976. {
  2977. GNUNET_CADET_close_port (cadet_port4);
  2978. cadet_port4 = NULL;
  2979. }
  2980. if (NULL != cadet_port6)
  2981. {
  2982. GNUNET_CADET_close_port (cadet_port6);
  2983. cadet_port6 = NULL;
  2984. }
  2985. if (NULL != cadet_handle)
  2986. {
  2987. GNUNET_CADET_disconnect (cadet_handle);
  2988. cadet_handle = NULL;
  2989. }
  2990. if (NULL != connections_map)
  2991. {
  2992. GNUNET_CONTAINER_multihashmap_iterate (connections_map,
  2993. &free_iterate,
  2994. NULL);
  2995. GNUNET_CONTAINER_multihashmap_destroy (connections_map);
  2996. connections_map = NULL;
  2997. }
  2998. if (NULL != connections_heap)
  2999. {
  3000. GNUNET_CONTAINER_heap_destroy (connections_heap);
  3001. connections_heap = NULL;
  3002. }
  3003. if (NULL != dnsstub)
  3004. {
  3005. GNUNET_DNSSTUB_stop (dnsstub);
  3006. dnsstub = NULL;
  3007. }
  3008. if (NULL != peer_key)
  3009. {
  3010. GNUNET_free (peer_key);
  3011. peer_key = NULL;
  3012. }
  3013. if (NULL != dht_task)
  3014. {
  3015. GNUNET_SCHEDULER_cancel (dht_task);
  3016. dht_task = NULL;
  3017. }
  3018. if (NULL != dht_put)
  3019. {
  3020. GNUNET_DHT_put_cancel (dht_put);
  3021. dht_put = NULL;
  3022. }
  3023. if (NULL != dht)
  3024. {
  3025. GNUNET_DHT_disconnect (dht);
  3026. dht = NULL;
  3027. }
  3028. if (NULL != stats)
  3029. {
  3030. GNUNET_STATISTICS_destroy (stats,
  3031. GNUNET_NO);
  3032. stats = NULL;
  3033. }
  3034. for (i = 0; i < 8; i++)
  3035. GNUNET_free_non_null (exit_argv[i]);
  3036. }
  3037. /**
  3038. * Add services to the service map.
  3039. *
  3040. * @param proto IPPROTO_TCP or IPPROTO_UDP
  3041. * @param cpy copy of the service descriptor (can be mutilated)
  3042. * @param name DNS name of the service
  3043. */
  3044. static void
  3045. add_services (int proto,
  3046. char *cpy,
  3047. const char *name)
  3048. {
  3049. char *redirect;
  3050. char *hostname;
  3051. char *hostport;
  3052. struct LocalService *serv;
  3053. char *n;
  3054. size_t slen;
  3055. slen = strlen (name);
  3056. GNUNET_assert (slen >= 8);
  3057. n = GNUNET_strndup (name, slen - 8 /* remove .gnunet. */);
  3058. for (redirect = strtok (cpy, " ;"); redirect != NULL;
  3059. redirect = strtok (NULL, " ;"))
  3060. {
  3061. if (NULL == (hostname = strstr (redirect, ":")))
  3062. {
  3063. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  3064. _ (
  3065. "Option `%s' for domain `%s' is not formatted correctly!\n"),
  3066. redirect,
  3067. name);
  3068. continue;
  3069. }
  3070. hostname[0] = '\0';
  3071. hostname++;
  3072. if (NULL == (hostport = strstr (hostname, ":")))
  3073. {
  3074. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  3075. _ (
  3076. "Option `%s' for domain `%s' is not formatted correctly!\n"),
  3077. redirect,
  3078. name);
  3079. continue;
  3080. }
  3081. hostport[0] = '\0';
  3082. hostport++;
  3083. int local_port = atoi (redirect);
  3084. int remote_port = atoi (hostport);
  3085. if (! ((local_port > 0) && (local_port < 65536)))
  3086. {
  3087. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  3088. _ ("`%s' is not a valid port number (for domain `%s')!"),
  3089. redirect,
  3090. name);
  3091. continue;
  3092. }
  3093. if (! ((remote_port > 0) && (remote_port < 65536)))
  3094. {
  3095. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  3096. _ ("`%s' is not a valid port number (for domain `%s')!"),
  3097. hostport,
  3098. name);
  3099. continue;
  3100. }
  3101. serv = GNUNET_new (struct LocalService);
  3102. serv->address.proto = proto;
  3103. serv->address.port = remote_port;
  3104. if (0 == strcmp ("localhost4",
  3105. hostname))
  3106. {
  3107. const char *ip4addr = exit_argv[5];
  3108. serv->address.af = AF_INET;
  3109. GNUNET_assert (1 == inet_pton (AF_INET,
  3110. ip4addr,
  3111. &serv->address.address.ipv4));
  3112. }
  3113. else if (0 == strcmp ("localhost6",
  3114. hostname))
  3115. {
  3116. const char *ip6addr = exit_argv[3];
  3117. serv->address.af = AF_INET6;
  3118. GNUNET_assert (1 == inet_pton (AF_INET6,
  3119. ip6addr,
  3120. &serv->address.address.ipv6));
  3121. }
  3122. else
  3123. {
  3124. struct addrinfo *res;
  3125. int ret;
  3126. ret = getaddrinfo (hostname,
  3127. NULL,
  3128. NULL,
  3129. &res);
  3130. if ((0 != ret) || (NULL == res))
  3131. {
  3132. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  3133. _ (
  3134. "No addresses found for hostname `%s' of service `%s'!\n"),
  3135. hostname,
  3136. n);
  3137. GNUNET_free (serv);
  3138. continue;
  3139. }
  3140. serv->address.af = res->ai_family;
  3141. switch (res->ai_family)
  3142. {
  3143. case AF_INET:
  3144. if (! ipv4_enabled)
  3145. {
  3146. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  3147. _ (
  3148. "Service `%s' configured for IPv4, but IPv4 is disabled!\n"),
  3149. n);
  3150. freeaddrinfo (res);
  3151. GNUNET_free (serv);
  3152. continue;
  3153. }
  3154. serv->address.address.ipv4
  3155. = ((struct sockaddr_in *) res->ai_addr)->sin_addr;
  3156. break;
  3157. case AF_INET6:
  3158. if (! ipv6_enabled)
  3159. {
  3160. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  3161. _ (
  3162. "Service `%s' configured for IPv4, but IPv4 is disabled!\n"),
  3163. n);
  3164. freeaddrinfo (res);
  3165. GNUNET_free (serv);
  3166. continue;
  3167. }
  3168. serv->address.address.ipv6
  3169. = ((struct sockaddr_in6 *) res->ai_addr)->sin6_addr;
  3170. break;
  3171. default:
  3172. freeaddrinfo (res);
  3173. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  3174. _ (
  3175. "No IP addresses found for hostname `%s' of service `%s'!\n"),
  3176. hostname,
  3177. n);
  3178. GNUNET_free (serv);
  3179. continue;
  3180. }
  3181. freeaddrinfo (res);
  3182. }
  3183. store_service (proto,
  3184. n,
  3185. local_port,
  3186. serv);
  3187. }
  3188. GNUNET_free (n);
  3189. }
  3190. /**
  3191. * Reads the configuration and populates #udp_services and #tcp_services
  3192. *
  3193. * @param cls unused
  3194. * @param section name of section in config
  3195. */
  3196. static void
  3197. read_service_conf (void *cls,
  3198. const char *section)
  3199. {
  3200. char *cpy;
  3201. if ((strlen (section) < 8) ||
  3202. (0 != strcmp (".gnunet.", section + (strlen (section) - 8))))
  3203. return;
  3204. if (GNUNET_OK ==
  3205. GNUNET_CONFIGURATION_get_value_string (cfg,
  3206. section,
  3207. "UDP_REDIRECTS",
  3208. &cpy))
  3209. {
  3210. add_services (IPPROTO_UDP,
  3211. cpy,
  3212. section);
  3213. GNUNET_free (cpy);
  3214. }
  3215. if (GNUNET_OK ==
  3216. GNUNET_CONFIGURATION_get_value_string (cfg,
  3217. section,
  3218. "TCP_REDIRECTS",
  3219. &cpy))
  3220. {
  3221. add_services (IPPROTO_TCP,
  3222. cpy,
  3223. section);
  3224. GNUNET_free (cpy);
  3225. }
  3226. }
  3227. /**
  3228. * We are running a DNS exit service, advertise it in the
  3229. * DHT. This task is run periodically to do the DHT PUT.
  3230. *
  3231. * @param cls closure
  3232. */
  3233. static void
  3234. do_dht_put (void *cls);
  3235. /**
  3236. * Function called when the DHT PUT operation is complete.
  3237. * Schedules the next PUT.
  3238. *
  3239. * @param cls closure, NULL
  3240. */
  3241. static void
  3242. dht_put_cont (void *cls)
  3243. {
  3244. dht_put = NULL;
  3245. }
  3246. /**
  3247. * We are running a DNS exit service, advertise it in the
  3248. * DHT. This task is run periodically to do the DHT PUT.
  3249. *
  3250. * @param cls closure
  3251. */
  3252. static void
  3253. do_dht_put (void *cls)
  3254. {
  3255. struct GNUNET_TIME_Absolute expiration;
  3256. dht_task = GNUNET_SCHEDULER_add_delayed (DHT_PUT_FREQUENCY,
  3257. &do_dht_put,
  3258. NULL);
  3259. expiration = GNUNET_TIME_absolute_ntoh (dns_advertisement.expiration_time);
  3260. if (GNUNET_TIME_absolute_get_remaining (expiration).rel_value_us <
  3261. GNUNET_TIME_UNIT_HOURS.rel_value_us)
  3262. {
  3263. /* refresh advertisement */
  3264. expiration = GNUNET_TIME_relative_to_absolute (DNS_ADVERTISEMENT_TIMEOUT);
  3265. dns_advertisement.expiration_time = GNUNET_TIME_absolute_hton (expiration);
  3266. GNUNET_assert (GNUNET_OK ==
  3267. GNUNET_CRYPTO_eddsa_sign (peer_key,
  3268. &dns_advertisement.purpose,
  3269. &dns_advertisement.signature));
  3270. }
  3271. if (NULL != dht_put)
  3272. GNUNET_DHT_put_cancel (dht_put);
  3273. dht_put = GNUNET_DHT_put (dht,
  3274. &dht_put_key,
  3275. 1 /* replication */,
  3276. GNUNET_DHT_RO_NONE,
  3277. GNUNET_BLOCK_TYPE_DNS,
  3278. sizeof(struct GNUNET_DNS_Advertisement),
  3279. &dns_advertisement,
  3280. expiration,
  3281. &dht_put_cont,
  3282. NULL);
  3283. }
  3284. /**
  3285. * Figure out which IP versions we should support (and which
  3286. * are supported by the OS) according to our configuration.
  3287. */
  3288. static void
  3289. parse_ip_options ()
  3290. {
  3291. ipv4_exit = GNUNET_CONFIGURATION_get_value_yesno (cfg,
  3292. "exit",
  3293. "EXIT_IPV4");
  3294. ipv6_exit = GNUNET_CONFIGURATION_get_value_yesno (cfg,
  3295. "exit",
  3296. "EXIT_IPV6");
  3297. ipv4_enabled = GNUNET_CONFIGURATION_get_value_yesno (cfg,
  3298. "exit",
  3299. "ENABLE_IPV4");
  3300. ipv6_enabled = GNUNET_CONFIGURATION_get_value_yesno (cfg,
  3301. "exit",
  3302. "ENABLE_IPV6");
  3303. if ((ipv4_exit || ipv4_enabled) &&
  3304. (GNUNET_OK != GNUNET_NETWORK_test_pf (PF_INET)) )
  3305. {
  3306. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  3307. _ (
  3308. "This system does not support IPv4, will disable IPv4 functions despite them being enabled in the configuration\n"));
  3309. ipv4_exit = GNUNET_NO;
  3310. ipv4_enabled = GNUNET_NO;
  3311. }
  3312. if ((ipv6_exit || ipv6_enabled) &&
  3313. (GNUNET_OK != GNUNET_NETWORK_test_pf (PF_INET6)) )
  3314. {
  3315. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  3316. _ (
  3317. "This system does not support IPv6, will disable IPv6 functions despite them being enabled in the configuration\n"));
  3318. ipv6_exit = GNUNET_NO;
  3319. ipv6_enabled = GNUNET_NO;
  3320. }
  3321. if (ipv4_exit && (! ipv4_enabled))
  3322. {
  3323. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  3324. _ (
  3325. "Cannot enable IPv4 exit but disable IPv4 on TUN interface, will use ENABLE_IPv4=YES\n"));
  3326. ipv4_enabled = GNUNET_YES;
  3327. }
  3328. if (ipv6_exit && (! ipv6_enabled))
  3329. {
  3330. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  3331. _ (
  3332. "Cannot enable IPv6 exit but disable IPv6 on TUN interface, will use ENABLE_IPv6=YES\n"));
  3333. ipv6_enabled = GNUNET_YES;
  3334. }
  3335. }
  3336. /**
  3337. * Helper function to open the CADET port for DNS exits and to
  3338. * advertise the DNS exit (if applicable).
  3339. */
  3340. static void
  3341. advertise_dns_exit ()
  3342. {
  3343. struct GNUNET_MQ_MessageHandler handlers[] = {
  3344. GNUNET_MQ_hd_var_size (dns_request,
  3345. GNUNET_MESSAGE_TYPE_VPN_DNS_TO_INTERNET,
  3346. struct DnsResponseMessage,
  3347. NULL),
  3348. GNUNET_MQ_handler_end ()
  3349. };
  3350. char *dns_exit;
  3351. struct GNUNET_HashCode port;
  3352. if (GNUNET_YES !=
  3353. GNUNET_CONFIGURATION_get_value_yesno (cfg,
  3354. "exit",
  3355. "EXIT_DNS"))
  3356. return;
  3357. GNUNET_assert (NULL != (dnsstub = GNUNET_DNSSTUB_start (128)));
  3358. dns_exit = NULL;
  3359. /* TODO: support using multiple DNS resolvers */
  3360. if ((GNUNET_OK !=
  3361. GNUNET_CONFIGURATION_get_value_string (cfg,
  3362. "exit",
  3363. "DNS_RESOLVER",
  3364. &dns_exit)) ||
  3365. (GNUNET_OK !=
  3366. GNUNET_DNSSTUB_add_dns_ip (dnsstub,
  3367. dns_exit)))
  3368. {
  3369. GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
  3370. "dns",
  3371. "DNS_RESOLVER",
  3372. _ ("need a valid IPv4 or IPv6 address\n"));
  3373. GNUNET_free_non_null (dns_exit);
  3374. return;
  3375. }
  3376. /* open port */
  3377. GNUNET_CRYPTO_hash (GNUNET_APPLICATION_PORT_INTERNET_RESOLVER,
  3378. strlen (GNUNET_APPLICATION_PORT_INTERNET_RESOLVER),
  3379. &port);
  3380. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  3381. "Opening CADET port %s for DNS exit service\n",
  3382. GNUNET_h2s (&port));
  3383. dns_port = GNUNET_CADET_open_port (cadet_handle,
  3384. &port,
  3385. &new_channel,
  3386. NULL,
  3387. NULL,
  3388. &clean_channel,
  3389. handlers);
  3390. /* advertise exit */
  3391. dht = GNUNET_DHT_connect (cfg,
  3392. 1);
  3393. peer_key = GNUNET_CRYPTO_eddsa_key_create_from_configuration (cfg);
  3394. GNUNET_CRYPTO_eddsa_key_get_public (peer_key,
  3395. &dns_advertisement.peer.public_key);
  3396. dns_advertisement.purpose.size = htonl (sizeof(struct
  3397. GNUNET_DNS_Advertisement)
  3398. - sizeof(struct
  3399. GNUNET_CRYPTO_EddsaSignature));
  3400. dns_advertisement.purpose.purpose = htonl (
  3401. GNUNET_SIGNATURE_PURPOSE_DNS_RECORD);
  3402. GNUNET_CRYPTO_hash ("dns",
  3403. strlen ("dns"),
  3404. &dht_put_key);
  3405. dht_task = GNUNET_SCHEDULER_add_now (&do_dht_put,
  3406. NULL);
  3407. GNUNET_free (dns_exit);
  3408. }
  3409. /**
  3410. * Initialize #exit_argv.
  3411. *
  3412. * @return #GNUNET_OK on success, #GNUNET_SYSERR if we should shutdown
  3413. */
  3414. static int
  3415. setup_exit_helper_args ()
  3416. {
  3417. char *exit_ifname;
  3418. char *tun_ifname;
  3419. char *ipv6addr;
  3420. char *ipv6prefix_s;
  3421. char *ipv4addr;
  3422. char *ipv4mask;
  3423. exit_argv[0] = GNUNET_strdup ("exit-gnunet");
  3424. if (GNUNET_SYSERR ==
  3425. GNUNET_CONFIGURATION_get_value_string (cfg,
  3426. "exit",
  3427. "TUN_IFNAME",
  3428. &tun_ifname))
  3429. {
  3430. GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
  3431. "EXIT",
  3432. "TUN_IFNAME");
  3433. return GNUNET_SYSERR;
  3434. }
  3435. exit_argv[1] = tun_ifname;
  3436. if (ipv4_enabled)
  3437. {
  3438. if (GNUNET_SYSERR ==
  3439. GNUNET_CONFIGURATION_get_value_string (cfg,
  3440. "exit",
  3441. "EXIT_IFNAME",
  3442. &exit_ifname))
  3443. {
  3444. GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
  3445. "EXIT",
  3446. "EXIT_IFNAME");
  3447. return GNUNET_SYSERR;
  3448. }
  3449. exit_argv[2] = exit_ifname;
  3450. }
  3451. else
  3452. {
  3453. exit_argv[2] = GNUNET_strdup ("-");
  3454. }
  3455. if (GNUNET_YES == ipv6_enabled)
  3456. {
  3457. ipv6addr = NULL;
  3458. if (((GNUNET_SYSERR ==
  3459. GNUNET_CONFIGURATION_get_value_string (cfg,
  3460. "exit",
  3461. "IPV6ADDR",
  3462. &ipv6addr)) ||
  3463. (1 != inet_pton (AF_INET6,
  3464. ipv6addr,
  3465. &exit_ipv6addr))))
  3466. {
  3467. GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
  3468. "EXIT",
  3469. "IPV6ADDR");
  3470. GNUNET_free_non_null (ipv6addr);
  3471. return GNUNET_SYSERR;
  3472. }
  3473. exit_argv[3] = ipv6addr;
  3474. if (GNUNET_SYSERR ==
  3475. GNUNET_CONFIGURATION_get_value_string (cfg,
  3476. "exit",
  3477. "IPV6PREFIX",
  3478. &ipv6prefix_s))
  3479. {
  3480. GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
  3481. "EXIT",
  3482. "IPV6PREFIX");
  3483. return GNUNET_SYSERR;
  3484. }
  3485. exit_argv[4] = ipv6prefix_s;
  3486. if ((GNUNET_OK !=
  3487. GNUNET_CONFIGURATION_get_value_number (cfg,
  3488. "exit",
  3489. "IPV6PREFIX",
  3490. &ipv6prefix)) ||
  3491. (ipv6prefix >= 127))
  3492. {
  3493. GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
  3494. "EXIT",
  3495. "IPV6PREFIX",
  3496. _ ("Must be a number"));
  3497. return GNUNET_SYSERR;
  3498. }
  3499. }
  3500. else
  3501. {
  3502. /* IPv6 explicitly disabled */
  3503. exit_argv[3] = GNUNET_strdup ("-");
  3504. exit_argv[4] = GNUNET_strdup ("-");
  3505. }
  3506. if (GNUNET_YES == ipv4_enabled)
  3507. {
  3508. ipv4addr = NULL;
  3509. if (((GNUNET_SYSERR ==
  3510. GNUNET_CONFIGURATION_get_value_string (cfg,
  3511. "exit",
  3512. "IPV4ADDR",
  3513. &ipv4addr)) ||
  3514. (1 != inet_pton (AF_INET,
  3515. ipv4addr,
  3516. &exit_ipv4addr))))
  3517. {
  3518. GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
  3519. "EXIT",
  3520. "IPV4ADDR");
  3521. GNUNET_free_non_null (ipv4addr);
  3522. return GNUNET_SYSERR;
  3523. }
  3524. exit_argv[5] = ipv4addr;
  3525. ipv4mask = NULL;
  3526. if (((GNUNET_SYSERR ==
  3527. GNUNET_CONFIGURATION_get_value_string (cfg,
  3528. "exit",
  3529. "IPV4MASK",
  3530. &ipv4mask)) ||
  3531. (1 != inet_pton (AF_INET,
  3532. ipv4mask,
  3533. &exit_ipv4mask))))
  3534. {
  3535. GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
  3536. "EXIT",
  3537. "IPV4MASK");
  3538. GNUNET_free_non_null (ipv4mask);
  3539. return GNUNET_SYSERR;
  3540. }
  3541. exit_argv[6] = ipv4mask;
  3542. }
  3543. else
  3544. {
  3545. /* IPv4 explicitly disabled */
  3546. exit_argv[5] = GNUNET_strdup ("-");
  3547. exit_argv[6] = GNUNET_strdup ("-");
  3548. }
  3549. exit_argv[7] = NULL;
  3550. return GNUNET_OK;
  3551. }
  3552. /**
  3553. * @brief Main function that will be run by the scheduler.
  3554. *
  3555. * @param cls closure
  3556. * @param args remaining command-line arguments
  3557. * @param cfgfile name of the configuration file used (for saving, can be NULL!)
  3558. * @param cfg_ configuration
  3559. */
  3560. static void
  3561. run (void *cls,
  3562. char *const *args,
  3563. const char *cfgfile,
  3564. const struct GNUNET_CONFIGURATION_Handle *cfg_)
  3565. {
  3566. struct GNUNET_MQ_MessageHandler handlers[] = {
  3567. GNUNET_MQ_hd_var_size (icmp_remote,
  3568. GNUNET_MESSAGE_TYPE_VPN_ICMP_TO_INTERNET,
  3569. struct GNUNET_EXIT_IcmpInternetMessage,
  3570. NULL),
  3571. GNUNET_MQ_hd_var_size (udp_remote,
  3572. GNUNET_MESSAGE_TYPE_VPN_UDP_TO_INTERNET,
  3573. struct GNUNET_EXIT_UdpInternetMessage,
  3574. NULL),
  3575. GNUNET_MQ_hd_var_size (tcp_remote,
  3576. GNUNET_MESSAGE_TYPE_VPN_TCP_TO_INTERNET_START,
  3577. struct GNUNET_EXIT_TcpInternetStartMessage,
  3578. NULL),
  3579. GNUNET_MQ_hd_var_size (tcp_data,
  3580. GNUNET_MESSAGE_TYPE_VPN_TCP_DATA_TO_EXIT,
  3581. struct GNUNET_EXIT_TcpDataMessage,
  3582. NULL),
  3583. GNUNET_MQ_handler_end ()
  3584. };
  3585. struct GNUNET_HashCode port;
  3586. char *policy;
  3587. char *binary;
  3588. char *regex;
  3589. char *prefixed_regex;
  3590. cfg = cfg_;
  3591. if (GNUNET_OK !=
  3592. GNUNET_CONFIGURATION_get_value_number (cfg,
  3593. "exit",
  3594. "MAX_CONNECTIONS",
  3595. &max_connections))
  3596. max_connections = 1024;
  3597. parse_ip_options ();
  3598. binary = GNUNET_OS_get_suid_binary_path (cfg, "gnunet-helper-exit");
  3599. if ((ipv4_exit) || (ipv6_exit))
  3600. {
  3601. if (GNUNET_YES !=
  3602. GNUNET_OS_check_helper_binary (binary,
  3603. GNUNET_YES,
  3604. "gnunet-vpn - - - 169.1.3.7 255.255.255.0")) // no nat, ipv4 only
  3605. {
  3606. GNUNET_free (binary);
  3607. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  3608. _ (
  3609. "`%s' is not SUID or the path is invalid, EXIT will not work\n"),
  3610. "gnunet-helper-exit");
  3611. GNUNET_SCHEDULER_add_shutdown (&dummy_task,
  3612. NULL);
  3613. global_ret = 1;
  3614. return;
  3615. }
  3616. }
  3617. if (! (ipv4_enabled || ipv6_enabled))
  3618. {
  3619. GNUNET_free (binary);
  3620. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  3621. _ ("No useful service enabled. Exiting.\n"));
  3622. GNUNET_SCHEDULER_shutdown ();
  3623. return;
  3624. }
  3625. GNUNET_SCHEDULER_add_shutdown (&cleanup,
  3626. NULL);
  3627. stats = GNUNET_STATISTICS_create ("exit",
  3628. cfg);
  3629. cadet_handle = GNUNET_CADET_connect (cfg);
  3630. if (NULL == cadet_handle)
  3631. {
  3632. GNUNET_free (binary);
  3633. GNUNET_SCHEDULER_shutdown ();
  3634. return;
  3635. }
  3636. advertise_dns_exit ();
  3637. if (GNUNET_OK !=
  3638. setup_exit_helper_args ())
  3639. {
  3640. GNUNET_free (binary);
  3641. GNUNET_SCHEDULER_shutdown ();
  3642. return;
  3643. }
  3644. services = GNUNET_CONTAINER_multihashmap_create (65536,
  3645. GNUNET_NO);
  3646. connections_map = GNUNET_CONTAINER_multihashmap_create (65536,
  3647. GNUNET_NO);
  3648. connections_heap = GNUNET_CONTAINER_heap_create (
  3649. GNUNET_CONTAINER_HEAP_ORDER_MIN);
  3650. GNUNET_CONFIGURATION_iterate_sections (cfg,
  3651. &read_service_conf,
  3652. NULL);
  3653. /* Cadet handle acquired, now open ports and announce regular
  3654. expressions matching our exit */
  3655. if ((GNUNET_YES == ipv4_enabled) &&
  3656. (GNUNET_YES == ipv4_exit))
  3657. {
  3658. GNUNET_CRYPTO_hash (GNUNET_APPLICATION_PORT_IPV4_GATEWAY,
  3659. strlen (GNUNET_APPLICATION_PORT_IPV4_GATEWAY),
  3660. &port);
  3661. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  3662. "Opening CADET port %s for IPv4 gateway service\n",
  3663. GNUNET_h2s (&port));
  3664. cadet_port4 = GNUNET_CADET_open_port (cadet_handle,
  3665. &port,
  3666. &new_channel,
  3667. NULL,
  3668. NULL,
  3669. &clean_channel,
  3670. handlers);
  3671. policy = NULL;
  3672. if (GNUNET_OK !=
  3673. GNUNET_CONFIGURATION_get_value_string (cfg,
  3674. "exit",
  3675. "EXIT_RANGE_IPV4_POLICY",
  3676. &policy))
  3677. regex = NULL;
  3678. else
  3679. regex = GNUNET_TUN_ipv4policy2regex (policy);
  3680. GNUNET_free_non_null (policy);
  3681. if (NULL != regex)
  3682. {
  3683. (void) GNUNET_asprintf (&prefixed_regex,
  3684. "%s%s",
  3685. GNUNET_APPLICATION_TYPE_EXIT_REGEX_PREFIX,
  3686. regex);
  3687. regex4 = GNUNET_REGEX_announce (cfg,
  3688. prefixed_regex,
  3689. REGEX_REFRESH_FREQUENCY,
  3690. REGEX_MAX_PATH_LEN_IPV4);
  3691. GNUNET_free (regex);
  3692. GNUNET_free (prefixed_regex);
  3693. }
  3694. }
  3695. if ((GNUNET_YES == ipv6_enabled) && (GNUNET_YES == ipv6_exit))
  3696. {
  3697. GNUNET_CRYPTO_hash (GNUNET_APPLICATION_PORT_IPV6_GATEWAY,
  3698. strlen (GNUNET_APPLICATION_PORT_IPV6_GATEWAY),
  3699. &port);
  3700. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  3701. "Opening CADET port %s for IPv6 gateway service\n",
  3702. GNUNET_h2s (&port));
  3703. cadet_port6 = GNUNET_CADET_open_port (cadet_handle,
  3704. &port,
  3705. &new_channel,
  3706. NULL,
  3707. NULL,
  3708. &clean_channel,
  3709. handlers);
  3710. policy = NULL;
  3711. if (GNUNET_OK !=
  3712. GNUNET_CONFIGURATION_get_value_string (cfg,
  3713. "exit",
  3714. "EXIT_RANGE_IPV6_POLICY",
  3715. &policy))
  3716. regex = NULL;
  3717. else
  3718. regex = GNUNET_TUN_ipv6policy2regex (policy);
  3719. GNUNET_free_non_null (policy);
  3720. if (NULL != regex)
  3721. {
  3722. (void) GNUNET_asprintf (&prefixed_regex,
  3723. "%s%s",
  3724. GNUNET_APPLICATION_TYPE_EXIT_REGEX_PREFIX,
  3725. regex);
  3726. regex6 = GNUNET_REGEX_announce (cfg,
  3727. prefixed_regex,
  3728. REGEX_REFRESH_FREQUENCY,
  3729. REGEX_MAX_PATH_LEN_IPV6);
  3730. GNUNET_free (regex);
  3731. GNUNET_free (prefixed_regex);
  3732. }
  3733. }
  3734. helper_handle = GNUNET_HELPER_start (GNUNET_NO,
  3735. binary,
  3736. exit_argv,
  3737. &message_token,
  3738. NULL,
  3739. NULL);
  3740. GNUNET_free (binary);
  3741. }
  3742. /**
  3743. * The main function
  3744. *
  3745. * @param argc number of arguments from the command line
  3746. * @param argv command line arguments
  3747. * @return 0 ok, 1 on error
  3748. */
  3749. int
  3750. main (int argc,
  3751. char *const *argv)
  3752. {
  3753. static const struct GNUNET_GETOPT_CommandLineOption options[] = {
  3754. GNUNET_GETOPT_OPTION_END
  3755. };
  3756. if (GNUNET_OK !=
  3757. GNUNET_STRINGS_get_utf8_args (argc,
  3758. argv,
  3759. &argc,
  3760. &argv))
  3761. return 2;
  3762. return (GNUNET_OK ==
  3763. GNUNET_PROGRAM_run (argc,
  3764. argv,
  3765. "gnunet-daemon-exit",
  3766. gettext_noop (
  3767. "Daemon to run to provide an IP exit node for the VPN"),
  3768. options,
  3769. &run,
  3770. NULL)) ? global_ret : 1;
  3771. }
  3772. /* end of gnunet-daemon-exit.c */