net.c 86 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715
  1. // SPDX-License-Identifier: LGPL-2.1-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2018-2019 Intel Corporation. All rights reserved.
  7. *
  8. *
  9. */
  10. #ifdef HAVE_CONFIG_H
  11. #include <config.h>
  12. #endif
  13. #define _GNU_SOURCE
  14. #include <sys/time.h>
  15. #include <ell/ell.h>
  16. #include "mesh/mesh-defs.h"
  17. #include "mesh/util.h"
  18. #include "mesh/crypto.h"
  19. #include "mesh/net-keys.h"
  20. #include "mesh/node.h"
  21. #include "mesh/net.h"
  22. #include "mesh/mesh-io.h"
  23. #include "mesh/friend.h"
  24. #include "mesh/mesh-config.h"
  25. #include "mesh/model.h"
  26. #include "mesh/appkey.h"
  27. #include "mesh/rpl.h"
  28. #define abs_diff(a, b) ((a) > (b) ? (a) - (b) : (b) - (a))
  29. #define IV_IDX_DIFF_RANGE 42
  30. /*#define IV_IDX_UPD_MIN (5 * 60) * 5 minute for Testing */
  31. #define IV_IDX_UPD_MIN (60 * 60 * 96) /* 96 Hours - per Spec */
  32. #define IV_IDX_UPD_HOLD (IV_IDX_UPD_MIN/2)
  33. #define IV_IDX_UPD_MAX (IV_IDX_UPD_MIN + IV_IDX_UPD_HOLD)
  34. #define iv_is_updating(net) ((net)->iv_upd_state == IV_UPD_UPDATING)
  35. #define IV_UPDATE_SEQ_TRIGGER 0x800000 /* Half of Seq-Nums expended */
  36. #define SEG_TO 2
  37. #define MSG_TO 60
  38. #define DEFAULT_TRANSMIT_COUNT 1
  39. #define DEFAULT_TRANSMIT_INTERVAL 100
  40. #define SAR_KEY(src, seq0) ((((uint32_t)(seq0)) << 16) | (src))
  41. #define FAST_CACHE_SIZE 8
  42. enum _relay_advice {
  43. RELAY_NONE, /* Relay not enabled in node */
  44. RELAY_ALLOWED, /* Relay enabled, msg not to node's unicast */
  45. RELAY_DISALLOWED, /* Msg was unicast handled by this node */
  46. RELAY_ALWAYS /* Relay enabled, msg to a group */
  47. };
  48. enum _iv_upd_state {
  49. /* Allows acceptance of any iv_index secure net beacon */
  50. IV_UPD_INIT,
  51. /* Normal, can transition, accept current or old */
  52. IV_UPD_NORMAL,
  53. /* Updating proc running, we use old, accept old or new */
  54. IV_UPD_UPDATING,
  55. /* Normal, can *not* transition, accept current or old iv_index */
  56. IV_UPD_NORMAL_HOLD,
  57. };
  58. struct net_key {
  59. struct mesh_key_set key_set;
  60. unsigned int beacon_id;
  61. uint8_t key[16];
  62. uint8_t beacon_key[16];
  63. uint8_t network_id[8];
  64. };
  65. struct mesh_subnet {
  66. struct mesh_net *net;
  67. uint16_t idx;
  68. uint32_t net_key_tx;
  69. uint32_t net_key_cur;
  70. uint32_t net_key_upd;
  71. uint8_t key_refresh;
  72. uint8_t kr_phase;
  73. };
  74. struct mesh_net {
  75. struct mesh_io *io;
  76. struct mesh_node *node;
  77. struct mesh_prov *prov;
  78. struct l_queue *app_keys;
  79. unsigned int pkt_id;
  80. unsigned int bea_id;
  81. unsigned int beacon_id;
  82. unsigned int sar_id_next;
  83. bool friend_enable;
  84. bool beacon_enable;
  85. bool proxy_enable;
  86. bool friend_seq;
  87. struct l_timeout *iv_update_timeout;
  88. enum _iv_upd_state iv_upd_state;
  89. bool iv_update;
  90. uint32_t instant; /* Controller Instant of recent Rx */
  91. uint32_t iv_index;
  92. uint32_t seq_num;
  93. uint16_t src_addr;
  94. uint16_t last_addr;
  95. uint16_t tx_interval;
  96. uint16_t tx_cnt;
  97. uint8_t chan; /* Channel of recent Rx */
  98. uint8_t default_ttl;
  99. uint8_t tid;
  100. struct {
  101. bool enable;
  102. uint16_t interval;
  103. uint8_t count;
  104. } relay;
  105. /* Heartbeat info */
  106. struct mesh_net_heartbeat_sub hb_sub;
  107. struct mesh_net_heartbeat_pub hb_pub;
  108. uint16_t features;
  109. struct l_queue *subnets;
  110. struct l_queue *msg_cache;
  111. struct l_queue *replay_cache;
  112. struct l_queue *sar_in;
  113. struct l_queue *sar_out;
  114. struct l_queue *sar_queue;
  115. struct l_queue *frnd_msgs;
  116. struct l_queue *friends;
  117. struct l_queue *negotiations;
  118. struct l_queue *destinations;
  119. };
  120. struct mesh_msg {
  121. uint16_t src;
  122. uint32_t seq;
  123. uint32_t mic;
  124. };
  125. struct mesh_sar {
  126. unsigned int id;
  127. struct l_timeout *seg_timeout;
  128. struct l_timeout *msg_timeout;
  129. uint32_t flags;
  130. uint32_t last_nak;
  131. uint32_t iv_index;
  132. uint32_t seqAuth;
  133. uint16_t seqZero;
  134. uint16_t app_idx;
  135. uint16_t net_idx;
  136. uint16_t src;
  137. uint16_t remote;
  138. uint16_t len;
  139. bool szmic;
  140. bool segmented;
  141. bool frnd;
  142. bool frnd_cred;
  143. uint8_t ttl;
  144. uint8_t last_seg;
  145. uint8_t key_aid;
  146. uint8_t buf[4]; /* Large enough for ACK-Flags and MIC */
  147. };
  148. struct mesh_destination {
  149. uint16_t dst;
  150. uint16_t ref_cnt;
  151. };
  152. struct net_decode {
  153. struct mesh_net *net;
  154. struct mesh_friend *frnd;
  155. struct mesh_key_set *key_set;
  156. uint8_t *packet;
  157. uint32_t iv_index;
  158. uint8_t size;
  159. uint8_t nid;
  160. bool proxy;
  161. };
  162. struct net_queue_data {
  163. struct mesh_io_recv_info *info;
  164. struct mesh_net *net;
  165. const uint8_t *data;
  166. uint8_t *out;
  167. size_t out_size;
  168. enum _relay_advice relay_advice;
  169. uint32_t key_id;
  170. uint32_t iv_index;
  171. uint16_t len;
  172. bool seen;
  173. };
  174. struct oneshot_tx {
  175. struct mesh_net *net;
  176. uint16_t interval;
  177. uint8_t cnt;
  178. uint8_t size;
  179. uint8_t packet[30];
  180. };
  181. struct net_beacon_data {
  182. uint32_t key_id;
  183. uint32_t ivi;
  184. bool ivu;
  185. bool kr;
  186. bool processed;
  187. };
  188. static struct l_queue *fast_cache;
  189. static struct l_queue *nets;
  190. static void net_rx(void *net_ptr, void *user_data);
  191. static inline struct mesh_subnet *get_primary_subnet(struct mesh_net *net)
  192. {
  193. return l_queue_peek_head(net->subnets);
  194. }
  195. static bool match_key_index(const void *a, const void *b)
  196. {
  197. const struct mesh_subnet *subnet = a;
  198. uint16_t idx = L_PTR_TO_UINT(b);
  199. return subnet->idx == idx;
  200. }
  201. static bool match_key_id(const void *a, const void *b)
  202. {
  203. const struct mesh_subnet *subnet = a;
  204. uint32_t key_id = L_PTR_TO_UINT(b);
  205. return (key_id == subnet->net_key_cur) ||
  206. (key_id == subnet->net_key_upd);
  207. }
  208. static bool match_friend_key_id(const void *a, const void *b)
  209. {
  210. const struct mesh_friend *friend = a;
  211. uint32_t key_id = L_PTR_TO_UINT(b);
  212. return (key_id == friend->net_key_cur) ||
  213. (key_id == friend->net_key_upd);
  214. }
  215. static void send_hb_publication(void *data)
  216. {
  217. struct mesh_net *net = data;
  218. struct mesh_net_heartbeat_pub *pub = &net->hb_pub;
  219. uint8_t msg[4];
  220. int n = 0;
  221. if (pub->dst == UNASSIGNED_ADDRESS)
  222. return;
  223. msg[n++] = NET_OP_HEARTBEAT;
  224. msg[n++] = pub->ttl;
  225. l_put_be16(net->features, msg + n);
  226. n += 2;
  227. mesh_net_transport_send(net, 0, 0, mesh_net_get_iv_index(net),
  228. pub->ttl, 0, 0, pub->dst, msg, n);
  229. }
  230. static void trigger_heartbeat(struct mesh_net *net, uint16_t feature,
  231. bool enable)
  232. {
  233. l_debug("HB: %4.4x --> %d", feature, enable);
  234. if (enable) {
  235. if (net->features & feature)
  236. return; /* no change */
  237. net->features |= feature;
  238. } else {
  239. if (!(net->features & feature))
  240. return; /* no change */
  241. net->features &= ~feature;
  242. }
  243. if (!(net->hb_pub.features & feature))
  244. return; /* no interest in this feature */
  245. l_idle_oneshot(send_hb_publication, net, NULL);
  246. }
  247. static bool match_by_friend(const void *a, const void *b)
  248. {
  249. const struct mesh_friend *frnd = a;
  250. uint16_t dst = L_PTR_TO_UINT(b);
  251. return frnd->lp_addr == dst;
  252. }
  253. static void free_friend_internals(struct mesh_friend *frnd)
  254. {
  255. if (frnd->pkt_cache)
  256. l_queue_destroy(frnd->pkt_cache, l_free);
  257. l_free(frnd->u.active.grp_list);
  258. frnd->u.active.grp_list = NULL;
  259. frnd->pkt_cache = NULL;
  260. net_key_unref(frnd->net_key_cur);
  261. net_key_unref(frnd->net_key_upd);
  262. frnd->net_key_cur = 0;
  263. frnd->net_key_upd = 0;
  264. }
  265. static void frnd_kr_phase1(void *a, void *b)
  266. {
  267. struct mesh_friend *frnd = a;
  268. uint32_t key_id = L_PTR_TO_UINT(b);
  269. frnd->net_key_upd = net_key_frnd_add(key_id, frnd->lp_addr,
  270. frnd->net->src_addr, frnd->lp_cnt, frnd->fn_cnt);
  271. }
  272. static void frnd_kr_phase2(void *a, void *b)
  273. {
  274. struct mesh_friend *frnd = a;
  275. /*
  276. * I think that a Friend should use Old Key as long as possible
  277. * Because a Friend Node will enter Phase 3 before it's LPN.
  278. * Alternatively, the FN could keep the Old Friend Keys until it
  279. * receives it's first Poll using the new keys (?)
  280. */
  281. l_debug("Use Both KeySet %d && %d for %4.4x",
  282. frnd->net_key_cur, frnd->net_key_upd, frnd->lp_addr);
  283. }
  284. static void frnd_kr_phase3(void *a, void *b)
  285. {
  286. struct mesh_friend *frnd = a;
  287. l_debug("Replace KeySet %d with %d for %4.4x",
  288. frnd->net_key_cur, frnd->net_key_upd, frnd->lp_addr);
  289. net_key_unref(frnd->net_key_cur);
  290. frnd->net_key_cur = frnd->net_key_upd;
  291. frnd->net_key_upd = 0;
  292. }
  293. /* TODO: add net key idx? For now, use primary net key */
  294. struct mesh_friend *mesh_friend_new(struct mesh_net *net, uint16_t dst,
  295. uint8_t ele_cnt, uint8_t frd,
  296. uint8_t frw, uint32_t fpt,
  297. uint16_t fn_cnt, uint16_t lp_cnt)
  298. {
  299. struct mesh_subnet *subnet;
  300. struct mesh_friend *frnd = l_queue_find(net->friends,
  301. match_by_friend, L_UINT_TO_PTR(dst));
  302. if (frnd) {
  303. /* Kill all timers and empty cache for this friend */
  304. free_friend_internals(frnd);
  305. l_timeout_remove(frnd->timeout);
  306. frnd->timeout = NULL;
  307. } else {
  308. frnd = l_new(struct mesh_friend, 1);
  309. l_queue_push_head(net->friends, frnd);
  310. }
  311. /* add _k2 */
  312. frnd->net = net;
  313. frnd->lp_addr = dst;
  314. frnd->frd = frd;
  315. frnd->frw = frw;
  316. frnd->fn_cnt = fn_cnt;
  317. frnd->lp_cnt = lp_cnt;
  318. frnd->poll_timeout = fpt;
  319. frnd->ele_cnt = ele_cnt;
  320. frnd->pkt_cache = l_queue_new();
  321. frnd->net_key_upd = 0;
  322. subnet = get_primary_subnet(net);
  323. /* TODO: the primary key must be present, do we need to add check?. */
  324. frnd->net_key_cur = net_key_frnd_add(subnet->net_key_cur, dst,
  325. net->src_addr, lp_cnt, fn_cnt);
  326. if (!subnet->net_key_upd)
  327. return frnd;
  328. frnd->net_idx = subnet->idx;
  329. frnd->net_key_upd = net_key_frnd_add(subnet->net_key_upd, dst,
  330. net->src_addr, lp_cnt, fn_cnt);
  331. return frnd;
  332. }
  333. void mesh_friend_free(void *data)
  334. {
  335. struct mesh_friend *frnd = data;
  336. free_friend_internals(frnd);
  337. l_timeout_remove(frnd->timeout);
  338. l_free(frnd);
  339. }
  340. bool mesh_friend_clear(struct mesh_net *net, struct mesh_friend *frnd)
  341. {
  342. bool removed = l_queue_remove(net->friends, frnd);
  343. free_friend_internals(frnd);
  344. return removed;
  345. }
  346. void mesh_friend_sub_add(struct mesh_net *net, uint16_t lpn, uint8_t ele_cnt,
  347. uint8_t grp_cnt, const uint8_t *list)
  348. {
  349. uint16_t *new_list;
  350. uint16_t *grp_list;
  351. struct mesh_friend *frnd = l_queue_find(net->friends,
  352. match_by_friend,
  353. L_UINT_TO_PTR(lpn));
  354. if (!frnd)
  355. return;
  356. new_list = l_malloc((grp_cnt +
  357. frnd->u.active.grp_cnt) * sizeof(uint16_t));
  358. grp_list = frnd->u.active.grp_list;
  359. if (grp_list && frnd->u.active.grp_cnt)
  360. memcpy(new_list, grp_list,
  361. frnd->u.active.grp_cnt * sizeof(uint16_t));
  362. memcpy(&new_list[frnd->u.active.grp_cnt], list,
  363. grp_cnt * sizeof(uint16_t));
  364. l_free(grp_list);
  365. frnd->ele_cnt = ele_cnt;
  366. frnd->u.active.grp_list = new_list;
  367. frnd->u.active.grp_cnt += grp_cnt;
  368. }
  369. void mesh_friend_sub_del(struct mesh_net *net, uint16_t lpn, uint8_t cnt,
  370. const uint8_t *del_list)
  371. {
  372. uint16_t *grp_list;
  373. int16_t i, grp_cnt;
  374. size_t cnt16 = cnt * sizeof(uint16_t);
  375. struct mesh_friend *frnd = l_queue_find(net->friends, match_by_friend,
  376. L_UINT_TO_PTR(lpn));
  377. if (!frnd)
  378. return;
  379. grp_cnt = frnd->u.active.grp_cnt;
  380. grp_list = frnd->u.active.grp_list;
  381. while (cnt-- && grp_cnt) {
  382. cnt16 -= sizeof(uint16_t);
  383. for (i = grp_cnt - 1; i >= 0; i--) {
  384. if (l_get_le16(del_list + cnt16) == grp_list[i]) {
  385. grp_cnt--;
  386. memcpy(&grp_list[i], &grp_list[i + 1],
  387. (grp_cnt - i) * sizeof(uint16_t));
  388. break;
  389. }
  390. }
  391. }
  392. frnd->u.active.grp_cnt = grp_cnt;
  393. if (!grp_cnt) {
  394. l_free(frnd->u.active.grp_list);
  395. frnd->u.active.grp_list = NULL;
  396. }
  397. }
  398. uint32_t mesh_net_next_seq_num(struct mesh_net *net)
  399. {
  400. uint32_t seq = net->seq_num++;
  401. /*
  402. * Cap out-of-range seq_num max value to +1. Out of range
  403. * seq_nums will not be sent as they would violate spec.
  404. * This condition signals a runaway seq_num condition, and
  405. * the node must wait for a completed IV Index update procedure
  406. * before it can send again.
  407. */
  408. if (net->seq_num > SEQ_MASK)
  409. net->seq_num = SEQ_MASK + 1;
  410. node_set_sequence_number(net->node, net->seq_num);
  411. return seq;
  412. }
  413. static struct mesh_sar *mesh_sar_new(size_t len)
  414. {
  415. size_t size = sizeof(struct mesh_sar) + len;
  416. struct mesh_sar *sar;
  417. sar = l_malloc(size);
  418. memset(sar, 0, size);
  419. return sar;
  420. }
  421. static void mesh_sar_free(void *data)
  422. {
  423. struct mesh_sar *sar = data;
  424. if (!sar)
  425. return;
  426. l_timeout_remove(sar->seg_timeout);
  427. l_timeout_remove(sar->msg_timeout);
  428. l_free(sar);
  429. }
  430. static void subnet_free(void *data)
  431. {
  432. struct mesh_subnet *subnet = data;
  433. net_key_unref(subnet->net_key_cur);
  434. net_key_unref(subnet->net_key_upd);
  435. l_free(subnet);
  436. }
  437. static struct mesh_subnet *subnet_new(struct mesh_net *net, uint16_t idx)
  438. {
  439. struct mesh_subnet *subnet;
  440. subnet = l_new(struct mesh_subnet, 1);
  441. if (!subnet)
  442. return NULL;
  443. subnet->net = net;
  444. subnet->idx = idx;
  445. return subnet;
  446. }
  447. static void enable_beacon(void *a, void *b)
  448. {
  449. struct mesh_subnet *subnet = a;
  450. struct mesh_net *net = b;
  451. if (net->beacon_enable)
  452. net_key_beacon_enable(subnet->net_key_tx);
  453. else
  454. net_key_beacon_disable(subnet->net_key_tx);
  455. }
  456. static void enqueue_update(void *a, void *b);
  457. static void queue_friend_update(struct mesh_net *net)
  458. {
  459. struct mesh_subnet *subnet;
  460. struct mesh_friend *frnd;
  461. uint8_t flags = 0;
  462. if (l_queue_length(net->friends)) {
  463. struct mesh_friend_msg update = {
  464. .src = net->src_addr,
  465. .iv_index = mesh_net_get_iv_index(net),
  466. .last_len = 7,
  467. .ctl = true,
  468. };
  469. frnd = l_queue_peek_head(net->friends);
  470. subnet = l_queue_find(net->subnets, match_key_index,
  471. L_UINT_TO_PTR(frnd->net_idx));
  472. if (!subnet)
  473. return;
  474. if (subnet->kr_phase == KEY_REFRESH_PHASE_TWO)
  475. flags |= KEY_REFRESH;
  476. if (net->iv_update)
  477. flags |= IV_INDEX_UPDATE;
  478. update.u.one[0].hdr = NET_OP_FRND_UPDATE << OPCODE_HDR_SHIFT;
  479. update.u.one[0].seq = mesh_net_next_seq_num(net);
  480. update.u.one[0].data[0] = NET_OP_FRND_UPDATE;
  481. update.u.one[0].data[1] = flags;
  482. l_put_be32(net->iv_index, update.u.one[0].data + 2);
  483. update.u.one[0].data[6] = 0x01; /* More Data */
  484. l_queue_foreach(net->friends, enqueue_update, &update);
  485. }
  486. }
  487. static void refresh_beacon(void *a, void *b)
  488. {
  489. struct mesh_subnet *subnet = a;
  490. struct mesh_net *net = b;
  491. net_key_beacon_refresh(subnet->net_key_tx, net->iv_index,
  492. !!(subnet->kr_phase == KEY_REFRESH_PHASE_TWO), net->iv_update);
  493. }
  494. struct mesh_net *mesh_net_new(struct mesh_node *node)
  495. {
  496. struct mesh_net *net;
  497. net = l_new(struct mesh_net, 1);
  498. net->node = node;
  499. net->seq_num = DEFAULT_SEQUENCE_NUMBER;
  500. net->default_ttl = TTL_MASK;
  501. net->tx_cnt = DEFAULT_TRANSMIT_COUNT;
  502. net->tx_interval = DEFAULT_TRANSMIT_INTERVAL;
  503. net->subnets = l_queue_new();
  504. net->msg_cache = l_queue_new();
  505. net->sar_in = l_queue_new();
  506. net->sar_out = l_queue_new();
  507. net->sar_queue = l_queue_new();
  508. net->frnd_msgs = l_queue_new();
  509. net->destinations = l_queue_new();
  510. net->app_keys = l_queue_new();
  511. net->replay_cache = l_queue_new();
  512. if (!nets)
  513. nets = l_queue_new();
  514. if (!fast_cache)
  515. fast_cache = l_queue_new();
  516. return net;
  517. }
  518. void mesh_net_free(void *user_data)
  519. {
  520. struct mesh_net *net = user_data;
  521. if (!net)
  522. return;
  523. l_queue_destroy(net->subnets, subnet_free);
  524. l_queue_destroy(net->msg_cache, l_free);
  525. l_queue_destroy(net->replay_cache, l_free);
  526. l_queue_destroy(net->sar_in, mesh_sar_free);
  527. l_queue_destroy(net->sar_out, mesh_sar_free);
  528. l_queue_destroy(net->sar_queue, mesh_sar_free);
  529. l_queue_destroy(net->frnd_msgs, l_free);
  530. l_queue_destroy(net->friends, mesh_friend_free);
  531. l_queue_destroy(net->negotiations, mesh_friend_free);
  532. l_queue_destroy(net->destinations, l_free);
  533. l_queue_destroy(net->app_keys, appkey_key_free);
  534. l_free(net);
  535. }
  536. void mesh_net_cleanup(void)
  537. {
  538. l_queue_destroy(fast_cache, l_free);
  539. fast_cache = NULL;
  540. l_queue_destroy(nets, mesh_net_free);
  541. nets = NULL;
  542. }
  543. bool mesh_net_set_seq_num(struct mesh_net *net, uint32_t seq)
  544. {
  545. if (!net)
  546. return false;
  547. net->seq_num = seq;
  548. node_set_sequence_number(net->node, net->seq_num);
  549. return true;
  550. }
  551. bool mesh_net_set_default_ttl(struct mesh_net *net, uint8_t ttl)
  552. {
  553. if (!net)
  554. return false;
  555. net->default_ttl = ttl;
  556. return true;
  557. }
  558. uint32_t mesh_net_get_seq_num(struct mesh_net *net)
  559. {
  560. if (!net)
  561. return 0;
  562. return net->seq_num;
  563. }
  564. uint8_t mesh_net_get_default_ttl(struct mesh_net *net)
  565. {
  566. if (!net)
  567. return 0;
  568. return net->default_ttl;
  569. }
  570. uint16_t mesh_net_get_address(struct mesh_net *net)
  571. {
  572. if (!net)
  573. return 0;
  574. return net->src_addr;
  575. }
  576. bool mesh_net_register_unicast(struct mesh_net *net,
  577. uint16_t address, uint8_t num_ele)
  578. {
  579. if (!net || !IS_UNICAST(address) || !num_ele)
  580. return false;
  581. net->src_addr = address;
  582. net->last_addr = address + num_ele - 1;
  583. if (net->last_addr < net->src_addr)
  584. return false;
  585. do {
  586. mesh_net_dst_reg(net, address);
  587. address++;
  588. num_ele--;
  589. } while (num_ele > 0);
  590. return true;
  591. }
  592. bool mesh_net_set_proxy_mode(struct mesh_net *net, bool enable)
  593. {
  594. if (!net)
  595. return false;
  596. /* No support for proxy yet */
  597. if (enable) {
  598. l_error("Proxy not supported!");
  599. return false;
  600. }
  601. trigger_heartbeat(net, FEATURE_PROXY, enable);
  602. return true;
  603. }
  604. bool mesh_net_set_friend_mode(struct mesh_net *net, bool enable)
  605. {
  606. l_debug("mesh_net_set_friend_mode - %d", enable);
  607. if (!net)
  608. return false;
  609. if (net->friend_enable == enable)
  610. return true;
  611. if (enable) {
  612. net->friends = l_queue_new();
  613. net->negotiations = l_queue_new();
  614. } else {
  615. l_queue_destroy(net->friends, mesh_friend_free);
  616. l_queue_destroy(net->negotiations, mesh_friend_free);
  617. net->friends = net->negotiations = NULL;
  618. }
  619. net->friend_enable = enable;
  620. trigger_heartbeat(net, FEATURE_FRIEND, enable);
  621. return true;
  622. }
  623. bool mesh_net_set_relay_mode(struct mesh_net *net, bool enable,
  624. uint8_t cnt, uint8_t interval)
  625. {
  626. if (!net)
  627. return false;
  628. net->relay.enable = enable;
  629. net->relay.count = cnt;
  630. net->relay.interval = interval;
  631. trigger_heartbeat(net, FEATURE_RELAY, enable);
  632. return true;
  633. }
  634. int mesh_net_get_identity_mode(struct mesh_net *net, uint16_t idx,
  635. uint8_t *mode)
  636. {
  637. struct mesh_subnet *subnet;
  638. if (!net)
  639. return MESH_STATUS_UNSPECIFIED_ERROR;
  640. subnet = l_queue_find(net->subnets, match_key_index,
  641. L_UINT_TO_PTR(idx));
  642. if (!subnet)
  643. return MESH_STATUS_INVALID_NETKEY;
  644. /* Currently, proxy mode is not supported */
  645. *mode = MESH_MODE_UNSUPPORTED;
  646. return MESH_STATUS_SUCCESS;
  647. }
  648. int mesh_net_del_key(struct mesh_net *net, uint16_t idx)
  649. {
  650. struct mesh_subnet *subnet;
  651. if (!net)
  652. return MESH_STATUS_UNSPECIFIED_ERROR;
  653. subnet = l_queue_find(net->subnets, match_key_index,
  654. L_UINT_TO_PTR(idx));
  655. if (!subnet)
  656. return MESH_STATUS_SUCCESS;
  657. /* Cannot remove primary key */
  658. if (l_queue_length(net->subnets) <= 1)
  659. return MESH_STATUS_CANNOT_REMOVE;
  660. /* Delete associated app keys */
  661. appkey_delete_bound_keys(net, idx);
  662. /* Disable hearbeat publication on this subnet */
  663. if (idx == net->hb_pub.net_idx)
  664. net->hb_pub.dst = UNASSIGNED_ADDRESS;
  665. /* TODO: cancel beacon_enable on this subnet */
  666. l_queue_remove(net->subnets, subnet);
  667. subnet_free(subnet);
  668. if (!mesh_config_net_key_del(node_config_get(net->node), idx))
  669. return MESH_STATUS_STORAGE_FAIL;
  670. return MESH_STATUS_SUCCESS;
  671. }
  672. static struct mesh_subnet *add_key(struct mesh_net *net, uint16_t idx,
  673. const uint8_t *value)
  674. {
  675. struct mesh_subnet *subnet;
  676. subnet = subnet_new(net, idx);
  677. if (!subnet)
  678. return NULL;
  679. subnet->net_key_tx = subnet->net_key_cur = net_key_add(value);
  680. if (!subnet->net_key_cur) {
  681. l_free(subnet);
  682. return NULL;
  683. }
  684. net_key_beacon_refresh(subnet->net_key_tx, net->iv_index,
  685. false, net->iv_update);
  686. if (net->beacon_enable)
  687. net_key_beacon_enable(subnet->net_key_tx);
  688. l_queue_push_tail(net->subnets, subnet);
  689. return subnet;
  690. }
  691. /*
  692. * This function is called when Configuration Server Model receives
  693. * a NETKEY_ADD command
  694. */
  695. int mesh_net_add_key(struct mesh_net *net, uint16_t idx, const uint8_t *value)
  696. {
  697. struct mesh_subnet *subnet;
  698. subnet = l_queue_find(net->subnets, match_key_index,
  699. L_UINT_TO_PTR(idx));
  700. if (subnet) {
  701. if (net_key_confirm(subnet->net_key_cur, value))
  702. return MESH_STATUS_SUCCESS;
  703. else
  704. return MESH_STATUS_IDX_ALREADY_STORED;
  705. }
  706. subnet = add_key(net, idx, value);
  707. if (!subnet)
  708. return MESH_STATUS_INSUFF_RESOURCES;
  709. if (!mesh_config_net_key_add(node_config_get(net->node), idx, value)) {
  710. l_queue_remove(net->subnets, subnet);
  711. subnet_free(subnet);
  712. return MESH_STATUS_STORAGE_FAIL;
  713. }
  714. return MESH_STATUS_SUCCESS;
  715. }
  716. uint32_t mesh_net_get_iv_index(struct mesh_net *net)
  717. {
  718. if (!net)
  719. return 0xffffffff;
  720. return net->iv_index - net->iv_update;
  721. }
  722. /* TODO: net key index? */
  723. void mesh_net_get_snb_state(struct mesh_net *net, uint8_t *flags,
  724. uint32_t *iv_index)
  725. {
  726. struct mesh_subnet *subnet;
  727. if (!net || !flags || !iv_index)
  728. return;
  729. *iv_index = net->iv_index;
  730. *flags = net->iv_update ? IV_INDEX_UPDATE : 0x00;
  731. subnet = get_primary_subnet(net);
  732. if (subnet)
  733. *flags |= subnet->key_refresh ? KEY_REFRESH : 0x00;
  734. }
  735. bool mesh_net_get_key(struct mesh_net *net, bool new_key, uint16_t idx,
  736. uint32_t *key_id)
  737. {
  738. struct mesh_subnet *subnet;
  739. if (!net)
  740. return false;
  741. subnet = l_queue_find(net->subnets, match_key_index,
  742. L_UINT_TO_PTR(idx));
  743. if (!subnet)
  744. return false;
  745. if (!new_key) {
  746. *key_id = subnet->net_key_cur;
  747. return true;
  748. }
  749. if (!subnet->net_key_upd)
  750. return false;
  751. *key_id = subnet->net_key_upd;
  752. return true;
  753. }
  754. bool mesh_net_key_list_get(struct mesh_net *net, uint8_t *buf, uint16_t *size)
  755. {
  756. const struct l_queue_entry *entry;
  757. uint16_t num_keys, req_size, buf_size;
  758. struct mesh_subnet *subnet;
  759. if (!net || !buf || !size)
  760. return false;
  761. buf_size = *size;
  762. num_keys = l_queue_length(net->subnets);
  763. req_size = (num_keys / 2) * 3 + (num_keys % 2) * 2;
  764. if (buf_size < req_size)
  765. return false;
  766. *size = req_size;
  767. /* Pack NetKey indices in 3 octets */
  768. for (entry = l_queue_get_entries(net->subnets); num_keys > 1;) {
  769. uint32_t idx_pair;
  770. subnet = entry->data;
  771. idx_pair = subnet->idx;
  772. idx_pair <<= 12;
  773. subnet = entry->next->data;
  774. idx_pair += subnet->idx;
  775. l_put_le32(idx_pair, buf);
  776. buf += 3;
  777. num_keys -= 2;
  778. entry = entry->next->next;
  779. }
  780. /* If odd number of NetKeys, fill in the end of the buffer */
  781. if (num_keys % 2) {
  782. subnet = entry->data;
  783. l_put_le16(subnet->idx, buf);
  784. }
  785. return true;
  786. }
  787. bool mesh_net_get_frnd_seq(struct mesh_net *net)
  788. {
  789. if (!net)
  790. return false;
  791. return net->friend_seq;
  792. }
  793. void mesh_net_set_frnd_seq(struct mesh_net *net, bool seq)
  794. {
  795. if (!net)
  796. return;
  797. net->friend_seq = seq;
  798. }
  799. static bool match_cache(const void *a, const void *b)
  800. {
  801. const struct mesh_msg *msg = a;
  802. const struct mesh_msg *tst = b;
  803. if (msg->seq != tst->seq || msg->mic != tst->mic ||
  804. msg->src != tst->src)
  805. return false;
  806. return true;
  807. }
  808. static bool msg_in_cache(struct mesh_net *net, uint16_t src, uint32_t seq,
  809. uint32_t mic)
  810. {
  811. struct mesh_msg *msg;
  812. struct mesh_msg tst = {
  813. .src = src,
  814. .seq = seq,
  815. .mic = mic,
  816. };
  817. msg = l_queue_remove_if(net->msg_cache, match_cache, &tst);
  818. if (msg) {
  819. l_debug("Supressing duplicate %4.4x + %6.6x + %8.8x",
  820. src, seq, mic);
  821. l_queue_push_head(net->msg_cache, msg);
  822. return true;
  823. }
  824. msg = l_new(struct mesh_msg, 1);
  825. *msg = tst;
  826. l_queue_push_head(net->msg_cache, msg);
  827. l_debug("Add %4.4x + %6.6x + %8.8x", src, seq, mic);
  828. if (l_queue_length(net->msg_cache) > MSG_CACHE_SIZE) {
  829. msg = l_queue_peek_tail(net->msg_cache);
  830. /* Remove Tail (oldest msg in cache) */
  831. l_debug("Remove %4.4x + %6.6x + %8.8x",
  832. msg->src, msg->seq, msg->mic);
  833. if (l_queue_remove(net->msg_cache, msg))
  834. l_free(msg);
  835. }
  836. return false;
  837. }
  838. static bool match_sar_seq0(const void *a, const void *b)
  839. {
  840. const struct mesh_sar *sar = a;
  841. uint16_t seqZero = L_PTR_TO_UINT(b);
  842. return sar->seqZero == seqZero;
  843. }
  844. static bool match_sar_remote(const void *a, const void *b)
  845. {
  846. const struct mesh_sar *sar = a;
  847. uint16_t remote = L_PTR_TO_UINT(b);
  848. return sar->remote == remote;
  849. }
  850. static bool match_msg_timeout(const void *a, const void *b)
  851. {
  852. const struct mesh_sar *sar = a;
  853. const struct l_timeout *msg_timeout = b;
  854. return sar->msg_timeout == msg_timeout;
  855. }
  856. static bool match_seg_timeout(const void *a, const void *b)
  857. {
  858. const struct mesh_sar *sar = a;
  859. const struct l_timeout *seg_timeout = b;
  860. return sar->seg_timeout == seg_timeout;
  861. }
  862. static bool match_dest_dst(const void *a, const void *b)
  863. {
  864. const struct mesh_destination *dest = a;
  865. uint16_t dst = L_PTR_TO_UINT(b);
  866. return dst == dest->dst;
  867. }
  868. static bool match_frnd_dst(const void *a, const void *b)
  869. {
  870. const struct mesh_friend *frnd = a;
  871. uint16_t dst = L_PTR_TO_UINT(b);
  872. int16_t i, grp_cnt = frnd->u.active.grp_cnt;
  873. uint16_t *grp_list = frnd->u.active.grp_list;
  874. /*
  875. * Determine if this message is for this friends unicast
  876. * address, and/or one of it's group/virtual addresses
  877. */
  878. if (dst >= frnd->lp_addr && dst < (frnd->lp_addr + frnd->ele_cnt))
  879. return true;
  880. if (!(dst & 0x8000))
  881. return false;
  882. for (i = 0; i < grp_cnt; i++) {
  883. if (dst == grp_list[i])
  884. return true;
  885. }
  886. return false;
  887. }
  888. static bool is_lpn_friend(struct mesh_net *net, uint16_t addr)
  889. {
  890. void *tst;
  891. tst = l_queue_find(net->friends, match_frnd_dst, L_UINT_TO_PTR(addr));
  892. return tst != NULL;
  893. }
  894. static bool is_us(struct mesh_net *net, uint16_t addr, bool src)
  895. {
  896. void *tst;
  897. if (IS_ALL_NODES(addr))
  898. return true;
  899. if (addr == FRIENDS_ADDRESS)
  900. return net->friend_enable;
  901. if (addr == RELAYS_ADDRESS)
  902. return net->relay.enable;
  903. if (addr == PROXIES_ADDRESS)
  904. return net->proxy_enable;
  905. if (addr >= net->src_addr && addr <= net->last_addr)
  906. return true;
  907. tst = l_queue_find(net->destinations, match_dest_dst,
  908. L_UINT_TO_PTR(addr));
  909. if (tst == NULL && !src)
  910. tst = l_queue_find(net->friends, match_frnd_dst,
  911. L_UINT_TO_PTR(addr));
  912. return tst != NULL;
  913. }
  914. static struct mesh_friend_msg *mesh_friend_msg_new(uint8_t seg_max)
  915. {
  916. struct mesh_friend_msg *frnd_msg;
  917. if (seg_max) {
  918. size_t size = sizeof(struct mesh_friend_msg) -
  919. sizeof(struct mesh_friend_seg_one);
  920. size += (seg_max + 1) * sizeof(struct mesh_friend_seg_12);
  921. frnd_msg = l_malloc(size);
  922. memset(frnd_msg, 0, size);
  923. } else
  924. frnd_msg = l_new(struct mesh_friend_msg, 1);
  925. return frnd_msg;
  926. }
  927. static bool match_ack(const void *a, const void *b)
  928. {
  929. const struct mesh_friend_msg *old = a;
  930. const struct mesh_friend_msg *rx = b;
  931. uint32_t old_hdr;
  932. uint32_t new_hdr;
  933. /* Determine if old pkt is ACK to same SAR message that new ACK is */
  934. if (!old->ctl || old->src != rx->src)
  935. return false;
  936. /* Check the quickest items first before digging deeper */
  937. old_hdr = old->u.one[0].hdr & HDR_ACK_MASK;
  938. new_hdr = rx->u.one[0].hdr & HDR_ACK_MASK;
  939. return old_hdr == new_hdr;
  940. }
  941. static void enqueue_friend_pkt(void *a, void *b)
  942. {
  943. struct mesh_friend *frnd = a;
  944. struct mesh_friend_msg *pkt, *rx = b;
  945. size_t size;
  946. int16_t i;
  947. if (rx->done)
  948. return;
  949. /*
  950. * Determine if this message is for this friends unicast
  951. * address, and/or one of it's group/virtual addresses
  952. */
  953. if (rx->dst >= frnd->lp_addr && (rx->dst - frnd->lp_addr) <
  954. frnd->ele_cnt) {
  955. rx->done = true;
  956. goto enqueue;
  957. }
  958. if (!(rx->dst & 0x8000))
  959. return;
  960. if (!IS_ALL_NODES(rx->dst)) {
  961. for (i = 0; i < frnd->u.active.grp_cnt; i++) {
  962. if (rx->dst == frnd->u.active.grp_list[i])
  963. goto enqueue;
  964. }
  965. return;
  966. }
  967. enqueue:
  968. /* Special handling for Seg Ack -- Only one per message queue */
  969. if (((rx->u.one[0].hdr >> OPCODE_HDR_SHIFT) & OPCODE_MASK) ==
  970. NET_OP_SEG_ACKNOWLEDGE) {
  971. void *old_head = l_queue_peek_head(frnd->pkt_cache);
  972. /* Suppress duplicate ACKs */
  973. do {
  974. void *old = l_queue_remove_if(frnd->pkt_cache,
  975. match_ack, rx);
  976. if (!old)
  977. break;
  978. if (old_head == old)
  979. /*
  980. * If we are discarding head for any
  981. * reason, reset FRND SEQ
  982. */
  983. frnd->u.active.last = frnd->u.active.seq;
  984. l_free(old);
  985. } while (true);
  986. }
  987. l_debug("%s for %4.4x from %4.4x ttl: %2.2x (seq: %6.6x) (ctl: %d)",
  988. __func__, frnd->lp_addr, rx->src, rx->ttl,
  989. rx->u.one[0].seq, rx->ctl);
  990. if (rx->cnt_in) {
  991. size = sizeof(struct mesh_friend_msg) -
  992. sizeof(struct mesh_friend_seg_one);
  993. size += (rx->cnt_in + 1) * sizeof(struct mesh_friend_seg_12);
  994. } else
  995. size = sizeof(struct mesh_friend_msg);
  996. pkt = l_malloc(size);
  997. memcpy(pkt, rx, size);
  998. l_queue_push_tail(frnd->pkt_cache, pkt);
  999. if (l_queue_length(frnd->pkt_cache) > FRND_CACHE_MAX) {
  1000. /*
  1001. * TODO: Guard against popping UPDATE packets
  1002. * (disallowed per spec)
  1003. */
  1004. pkt = l_queue_pop_head(frnd->pkt_cache);
  1005. l_free(pkt);
  1006. frnd->u.active.last = frnd->u.active.seq;
  1007. }
  1008. }
  1009. static void enqueue_update(void *a, void *b)
  1010. {
  1011. struct mesh_friend *frnd = a;
  1012. struct mesh_friend_msg *pkt = b;
  1013. pkt->dst = frnd->lp_addr;
  1014. pkt->done = false;
  1015. enqueue_friend_pkt(frnd, pkt);
  1016. }
  1017. static uint32_t seq_auth(uint32_t seq, uint16_t seqZero)
  1018. {
  1019. uint32_t seqAuth = seqZero & SEQ_ZERO_MASK;
  1020. seqAuth |= seq & (~SEQ_ZERO_MASK);
  1021. if (seqAuth > seq)
  1022. seqAuth -= (SEQ_ZERO_MASK + 1);
  1023. return seqAuth;
  1024. }
  1025. static bool friend_packet_queue(struct mesh_net *net,
  1026. uint32_t iv_index,
  1027. bool ctl, uint8_t ttl,
  1028. uint32_t seq,
  1029. uint16_t src, uint16_t dst,
  1030. uint32_t hdr,
  1031. const uint8_t *data, uint16_t size)
  1032. {
  1033. struct mesh_friend_msg *frnd_msg;
  1034. uint8_t seg_max = SEG_TOTAL(hdr);
  1035. bool ret;
  1036. if (seg_max && !IS_SEGMENTED(hdr))
  1037. return false;
  1038. frnd_msg = mesh_friend_msg_new(seg_max);
  1039. if (IS_SEGMENTED(hdr)) {
  1040. uint32_t seqAuth = seq_auth(seq, hdr >> SEQ_ZERO_HDR_SHIFT);
  1041. uint8_t i;
  1042. for (i = 0; i <= seg_max; i++) {
  1043. memcpy(frnd_msg->u.s12[i].data, data, 12);
  1044. frnd_msg->u.s12[i].hdr = hdr;
  1045. frnd_msg->u.s12[i].seq = seqAuth + i;
  1046. data += 12;
  1047. hdr += (1 << SEGO_HDR_SHIFT);
  1048. }
  1049. frnd_msg->u.s12[seg_max].seq = seq;
  1050. frnd_msg->cnt_in = seg_max;
  1051. frnd_msg->last_len = size % 12;
  1052. if (!frnd_msg->last_len)
  1053. frnd_msg->last_len = 12;
  1054. } else {
  1055. uint8_t opcode = hdr >> OPCODE_HDR_SHIFT;
  1056. if (ctl && opcode != NET_OP_SEG_ACKNOWLEDGE) {
  1057. /* Don't cache Friend Ctl opcodes */
  1058. if (FRND_OPCODE(opcode)) {
  1059. l_free(frnd_msg);
  1060. return false;
  1061. }
  1062. memcpy(frnd_msg->u.one[0].data + 1, data, size);
  1063. frnd_msg->last_len = size + 1;
  1064. frnd_msg->u.one[0].data[0] = opcode;
  1065. } else {
  1066. memcpy(frnd_msg->u.one[0].data, data, size);
  1067. frnd_msg->last_len = size;
  1068. }
  1069. frnd_msg->u.one[0].hdr = hdr;
  1070. frnd_msg->u.one[0].seq = seq;
  1071. }
  1072. frnd_msg->iv_index = iv_index;
  1073. frnd_msg->src = src;
  1074. frnd_msg->dst = dst;
  1075. frnd_msg->ctl = ctl;
  1076. frnd_msg->ttl = ttl;
  1077. /* Re-Package into Friend Delivery payload */
  1078. l_queue_foreach(net->friends, enqueue_friend_pkt, frnd_msg);
  1079. ret = frnd_msg->done;
  1080. /* TODO Optimization(?): Unicast messages keep this buffer */
  1081. l_free(frnd_msg);
  1082. return ret;
  1083. }
  1084. static void friend_ack_rxed(struct mesh_net *net, uint32_t iv_index,
  1085. uint32_t seq,
  1086. uint16_t src, uint16_t dst,
  1087. const uint8_t *pkt)
  1088. {
  1089. uint32_t hdr = l_get_be32(pkt) &
  1090. ((SEQ_ZERO_MASK << SEQ_ZERO_HDR_SHIFT) | /* Preserve SeqZero */
  1091. (true << RELAY_HDR_SHIFT)); /* Preserve Relay bit */
  1092. uint32_t flags = l_get_be32(pkt + 3);
  1093. struct mesh_friend_msg frnd_ack = {
  1094. .ctl = true,
  1095. .iv_index = iv_index,
  1096. .src = src,
  1097. .dst = dst,
  1098. .last_len = sizeof(flags),
  1099. .u.one[0].seq = seq,
  1100. .done = false,
  1101. };
  1102. hdr |= NET_OP_SEG_ACKNOWLEDGE << OPCODE_HDR_SHIFT;
  1103. frnd_ack.u.one[0].hdr = hdr;
  1104. l_put_be32(flags, frnd_ack.u.one[0].data);
  1105. l_queue_foreach(net->friends, enqueue_friend_pkt, &frnd_ack);
  1106. }
  1107. static bool send_seg(struct mesh_net *net, uint8_t cnt, uint16_t interval,
  1108. struct mesh_sar *msg, uint8_t seg);
  1109. static void send_frnd_ack(struct mesh_net *net, uint16_t src, uint16_t dst,
  1110. uint32_t hdr, uint32_t flags)
  1111. {
  1112. uint32_t expected;
  1113. uint8_t msg[7];
  1114. /* We don't ACK from multicast destinations */
  1115. if (src & 0x8000)
  1116. return;
  1117. /* Calculate the "Full ACK" mask */
  1118. expected = 0xffffffff >> (31 - SEG_TOTAL(hdr));
  1119. /* Clear Hdr bits that don't apply to Seg ACK */
  1120. hdr &= ~((true << SEG_HDR_SHIFT) |
  1121. (OPCODE_MASK << OPCODE_HDR_SHIFT) |
  1122. (true << SZMIC_HDR_SHIFT) |
  1123. (SEG_MASK << SEGO_HDR_SHIFT) |
  1124. (SEG_MASK << SEGN_HDR_SHIFT));
  1125. hdr |= NET_OP_SEG_ACKNOWLEDGE << OPCODE_HDR_SHIFT;
  1126. hdr |= true << RELAY_HDR_SHIFT;
  1127. /* Clear all unexpected bits */
  1128. flags &= expected;
  1129. l_put_be32(hdr, msg);
  1130. l_put_be32(flags, msg + 3);
  1131. l_debug("Send Friend ACK to Segs: %8.8x", flags);
  1132. if (is_lpn_friend(net, dst)) {
  1133. /* If we are acking our LPN Friend, queue, don't send */
  1134. friend_ack_rxed(net, mesh_net_get_iv_index(net),
  1135. mesh_net_next_seq_num(net), 0, dst, msg);
  1136. } else {
  1137. mesh_net_transport_send(net, 0, 0,
  1138. mesh_net_get_iv_index(net), DEFAULT_TTL,
  1139. 0, 0, dst, msg, sizeof(msg));
  1140. }
  1141. }
  1142. static void send_net_ack(struct mesh_net *net, struct mesh_sar *sar,
  1143. uint32_t flags)
  1144. {
  1145. uint8_t msg[7];
  1146. uint32_t hdr;
  1147. uint16_t src = sar->src;
  1148. uint16_t dst = sar->remote;
  1149. /* We don't ACK from multicast destinations */
  1150. if (src & 0x8000)
  1151. return;
  1152. hdr = NET_OP_SEG_ACKNOWLEDGE << OPCODE_HDR_SHIFT;
  1153. hdr |= sar->seqZero << SEQ_ZERO_HDR_SHIFT;
  1154. if (is_lpn_friend(net, src))
  1155. hdr |= true << RELAY_HDR_SHIFT;
  1156. l_put_be32(hdr, msg);
  1157. l_put_be32(flags, msg + 3);
  1158. l_debug("Send%s ACK to Segs: %8.8x", sar->frnd ? " Friend" : "", flags);
  1159. if (is_lpn_friend(net, dst)) {
  1160. /* If we are acking our LPN Friend, queue, don't send */
  1161. friend_ack_rxed(net, mesh_net_get_iv_index(net),
  1162. mesh_net_next_seq_num(net), src, dst, msg);
  1163. return;
  1164. }
  1165. mesh_net_transport_send(net, 0, sar->net_idx,
  1166. mesh_net_get_iv_index(net), DEFAULT_TTL,
  1167. 0, src, dst, msg,
  1168. sizeof(msg));
  1169. }
  1170. static void inseg_to(struct l_timeout *seg_timeout, void *user_data)
  1171. {
  1172. struct mesh_net *net = user_data;
  1173. struct mesh_sar *sar = l_queue_find(net->sar_in,
  1174. match_seg_timeout, seg_timeout);
  1175. l_timeout_remove(seg_timeout);
  1176. if (!sar)
  1177. return;
  1178. /* Send NAK */
  1179. l_debug("Timeout %p %3.3x", sar, sar->app_idx);
  1180. send_net_ack(net, sar, sar->flags);
  1181. sar->seg_timeout = l_timeout_create(SEG_TO, inseg_to, net, NULL);
  1182. }
  1183. static void inmsg_to(struct l_timeout *msg_timeout, void *user_data)
  1184. {
  1185. struct mesh_net *net = user_data;
  1186. struct mesh_sar *sar = l_queue_remove_if(net->sar_in,
  1187. match_msg_timeout, msg_timeout);
  1188. l_timeout_remove(msg_timeout);
  1189. if (!sar)
  1190. return;
  1191. sar->msg_timeout = NULL;
  1192. mesh_sar_free(sar);
  1193. }
  1194. static void outmsg_to(struct l_timeout *msg_timeout, void *user_data)
  1195. {
  1196. struct mesh_net *net = user_data;
  1197. struct mesh_sar *sar = l_queue_remove_if(net->sar_out,
  1198. match_msg_timeout, msg_timeout);
  1199. l_timeout_remove(msg_timeout);
  1200. if (!sar)
  1201. return;
  1202. sar->msg_timeout = NULL;
  1203. mesh_sar_free(sar);
  1204. }
  1205. static void outseg_to(struct l_timeout *seg_timeout, void *user_data);
  1206. static void send_queued_sar(struct mesh_net *net, uint16_t dst)
  1207. {
  1208. struct mesh_sar *sar = l_queue_remove_if(net->sar_queue,
  1209. match_sar_remote, L_UINT_TO_PTR(dst));
  1210. if (!sar)
  1211. return;
  1212. /* Out to current outgoing, and immediate expire Seg TO */
  1213. l_queue_push_head(net->sar_out, sar);
  1214. sar->seg_timeout = NULL;
  1215. sar->msg_timeout = l_timeout_create(MSG_TO, outmsg_to, net, NULL);
  1216. outseg_to(NULL, net);
  1217. }
  1218. static void ack_received(struct mesh_net *net, bool timeout,
  1219. uint16_t src, uint16_t dst,
  1220. uint16_t seq0, uint32_t ack_flag)
  1221. {
  1222. struct mesh_sar *outgoing;
  1223. uint32_t seg_flag = 0x00000001;
  1224. uint32_t ack_copy = ack_flag;
  1225. uint16_t i;
  1226. l_debug("ACK Rxed (%x) (to:%d): %8.8x", seq0, timeout, ack_flag);
  1227. outgoing = l_queue_find(net->sar_out, match_sar_seq0,
  1228. L_UINT_TO_PTR(seq0));
  1229. if (!outgoing) {
  1230. l_debug("Not Found: %4.4x", seq0);
  1231. return;
  1232. }
  1233. /*
  1234. * TODO -- If we receive from different
  1235. * SRC than we are sending to, make sure the OBO flag is set
  1236. */
  1237. if ((!timeout && !ack_flag) ||
  1238. (outgoing->flags & ack_flag) == outgoing->flags) {
  1239. l_debug("ob_sar_removal (%x)", outgoing->flags);
  1240. /* Note: ack_flags == 0x00000000 is a remote Cancel request */
  1241. l_queue_remove(net->sar_out, outgoing);
  1242. send_queued_sar(net, outgoing->remote);
  1243. mesh_sar_free(outgoing);
  1244. return;
  1245. }
  1246. outgoing->last_nak |= ack_flag;
  1247. ack_copy &= outgoing->flags;
  1248. for (i = 0; i <= SEG_MAX(true, outgoing->len); i++, seg_flag <<= 1) {
  1249. if (seg_flag & ack_flag) {
  1250. l_debug("Skipping Seg %d of %d",
  1251. i, SEG_MAX(true, outgoing->len));
  1252. continue;
  1253. }
  1254. ack_copy |= seg_flag;
  1255. l_debug("Resend Seg %d net:%p dst:%x app_idx:%3.3x",
  1256. i, net, outgoing->remote, outgoing->app_idx);
  1257. send_seg(net, net->tx_cnt, net->tx_interval, outgoing, i);
  1258. }
  1259. l_timeout_remove(outgoing->seg_timeout);
  1260. outgoing->seg_timeout = l_timeout_create(SEG_TO, outseg_to, net, NULL);
  1261. }
  1262. static void outseg_to(struct l_timeout *seg_timeout, void *user_data)
  1263. {
  1264. struct mesh_net *net = user_data;
  1265. struct mesh_sar *sar = l_queue_find(net->sar_out,
  1266. match_seg_timeout, seg_timeout);
  1267. l_timeout_remove(seg_timeout);
  1268. if (!sar)
  1269. return;
  1270. sar->seg_timeout = NULL;
  1271. /* Re-Send missing segments by faking NACK */
  1272. ack_received(net, true, sar->remote, sar->src,
  1273. sar->seqZero, sar->last_nak);
  1274. }
  1275. static bool match_replay_cache(const void *a, const void *b)
  1276. {
  1277. const struct mesh_rpl *rpe = a;
  1278. uint16_t src = L_PTR_TO_UINT(b);
  1279. return src == rpe->src;
  1280. }
  1281. static bool clean_old_iv_index(void *a, void *b)
  1282. {
  1283. struct mesh_rpl *rpe = a;
  1284. uint32_t iv_index = L_PTR_TO_UINT(b);
  1285. if (iv_index < 2)
  1286. return false;
  1287. if (rpe->iv_index < iv_index - 1) {
  1288. l_free(rpe);
  1289. return true;
  1290. }
  1291. return false;
  1292. }
  1293. static bool msg_check_replay_cache(struct mesh_net *net, uint16_t src,
  1294. uint16_t crpl, uint32_t seq, uint32_t iv_index)
  1295. {
  1296. struct mesh_rpl *rpe;
  1297. /* If anything missing reject this message by returning true */
  1298. if (!net || !net->node)
  1299. return true;
  1300. rpe = l_queue_find(net->replay_cache, match_replay_cache,
  1301. L_UINT_TO_PTR(src));
  1302. if (rpe) {
  1303. if (iv_index > rpe->iv_index)
  1304. return false;
  1305. /* Return true if (iv_index | seq) too low */
  1306. if (iv_index < rpe->iv_index || seq <= rpe->seq) {
  1307. l_debug("Ignoring replayed packet");
  1308. return true;
  1309. }
  1310. } else if (l_queue_length(net->replay_cache) >= crpl) {
  1311. /* SRC not in Replay Cache... see if there is space for it */
  1312. int ret = l_queue_foreach_remove(net->replay_cache,
  1313. clean_old_iv_index, L_UINT_TO_PTR(iv_index));
  1314. /* Return true if no space could be freed */
  1315. if (!ret) {
  1316. l_debug("Replay cache full");
  1317. return true;
  1318. }
  1319. }
  1320. return false;
  1321. }
  1322. static void msg_add_replay_cache(struct mesh_net *net, uint16_t src,
  1323. uint32_t seq, uint32_t iv_index)
  1324. {
  1325. struct mesh_rpl *rpe;
  1326. if (!net || !net->replay_cache)
  1327. return;
  1328. rpe = l_queue_remove_if(net->replay_cache, match_replay_cache,
  1329. L_UINT_TO_PTR(src));
  1330. if (!rpe) {
  1331. rpe = l_new(struct mesh_rpl, 1);
  1332. rpe->src = src;
  1333. }
  1334. rpe->seq = seq;
  1335. rpe->iv_index = iv_index;
  1336. rpl_put_entry(net->node, src, iv_index, seq);
  1337. /* Optimize so that most recent conversations stay earliest in cache */
  1338. l_queue_push_head(net->replay_cache, rpe);
  1339. }
  1340. static bool msg_rxed(struct mesh_net *net, bool frnd, uint32_t iv_index,
  1341. uint8_t ttl, uint32_t seq,
  1342. uint16_t net_idx,
  1343. uint16_t src, uint16_t dst,
  1344. uint8_t key_aid, bool segmented,
  1345. bool szmic, uint16_t seqZero,
  1346. const uint8_t *data, uint16_t size)
  1347. {
  1348. uint32_t seqAuth = seq_auth(seq, seqZero);
  1349. uint16_t crpl;
  1350. /* Sanity check seqAuth */
  1351. if (seqAuth > seq)
  1352. return false;
  1353. /* Save un-decrypted messages for our friends */
  1354. if (!frnd && l_queue_length(net->friends)) {
  1355. uint32_t hdr = key_aid << KEY_HDR_SHIFT;
  1356. uint8_t frnd_ttl = ttl;
  1357. /* If not from us, decrement for our hop */
  1358. if (src < net->src_addr || src > net->last_addr) {
  1359. if (frnd_ttl > 1)
  1360. frnd_ttl--;
  1361. else
  1362. goto not_for_friend;
  1363. }
  1364. if (szmic || size > 15) {
  1365. hdr |= true << SEG_HDR_SHIFT;
  1366. hdr |= szmic << SZMIC_HDR_SHIFT;
  1367. hdr |= (seqZero & SEQ_ZERO_MASK) << SEQ_ZERO_HDR_SHIFT;
  1368. hdr |= SEG_MAX(true, size) << SEGN_HDR_SHIFT;
  1369. }
  1370. if (friend_packet_queue(net, iv_index, false, frnd_ttl,
  1371. seq, src, dst,
  1372. hdr, data, size))
  1373. return true;
  1374. }
  1375. not_for_friend:
  1376. if (dst == FRIENDS_ADDRESS && !net->friend_enable)
  1377. return false;
  1378. if (dst == RELAYS_ADDRESS && !net->relay.enable)
  1379. return false;
  1380. if (dst == PROXIES_ADDRESS && !net->proxy_enable)
  1381. return false;
  1382. /* Don't process if already in RPL */
  1383. crpl = node_get_crpl(net->node);
  1384. if (msg_check_replay_cache(net, src, crpl, seq, iv_index))
  1385. return false;
  1386. if (!mesh_model_rx(net->node, szmic, seqAuth, iv_index, net_idx, src,
  1387. dst, key_aid, data, size))
  1388. return false;
  1389. /* If message has been handled by us, add to RPL */
  1390. msg_add_replay_cache(net, src, seq, iv_index);
  1391. return true;
  1392. }
  1393. static uint16_t key_id_to_net_idx(struct mesh_net *net, uint32_t key_id)
  1394. {
  1395. struct mesh_subnet *subnet;
  1396. struct mesh_friend *friend;
  1397. if (!net)
  1398. return NET_IDX_INVALID;
  1399. subnet = l_queue_find(net->subnets, match_key_id,
  1400. L_UINT_TO_PTR(key_id));
  1401. if (subnet)
  1402. return subnet->idx;
  1403. friend = l_queue_find(net->friends, match_friend_key_id,
  1404. L_UINT_TO_PTR(key_id));
  1405. if (friend)
  1406. return friend->net_idx;
  1407. friend = l_queue_find(net->negotiations, match_friend_key_id,
  1408. L_UINT_TO_PTR(key_id));
  1409. if (friend)
  1410. return friend->net_idx;
  1411. else
  1412. return NET_IDX_INVALID;
  1413. }
  1414. static bool match_frnd_sar_dst(const void *a, const void *b)
  1415. {
  1416. const struct mesh_friend_msg *frnd_msg = a;
  1417. uint16_t dst = L_PTR_TO_UINT(b);
  1418. return frnd_msg->dst == dst;
  1419. }
  1420. static void friend_seg_rxed(struct mesh_net *net,
  1421. uint32_t iv_index,
  1422. uint8_t ttl, uint32_t seq,
  1423. uint16_t src, uint16_t dst, uint32_t hdr,
  1424. const uint8_t *data, uint8_t size)
  1425. {
  1426. struct mesh_friend *frnd = NULL;
  1427. struct mesh_friend_msg *frnd_msg = NULL;
  1428. uint8_t cnt;
  1429. uint8_t segN = hdr & 0x1f;
  1430. uint8_t segO = ((hdr >> 5) & 0x1f);
  1431. uint32_t expected = 0xffffffff >> (31 - segN);
  1432. uint32_t this_seg_flag = 0x00000001 << segO;
  1433. uint32_t largest = (0xffffffff << segO) & expected;
  1434. uint32_t hdr_key = hdr & HDR_KEY_MASK;
  1435. frnd = l_queue_find(net->friends, match_frnd_dst,
  1436. L_UINT_TO_PTR(dst));
  1437. if (!frnd)
  1438. return;
  1439. if (frnd->u.active.last_hdr == hdr_key) {
  1440. /* We are no longer receiving this msg. Resend final ACK */
  1441. send_frnd_ack(net, dst, src, frnd->u.active.last_hdr,
  1442. 0xffffffff);
  1443. return;
  1444. }
  1445. /* Check if we have a SAR-in-progress that matches incoming segment */
  1446. frnd_msg = l_queue_find(net->frnd_msgs, match_frnd_sar_dst,
  1447. L_UINT_TO_PTR(dst));
  1448. if (frnd_msg) {
  1449. /* Flush if SZMICN or IV Index has changed */
  1450. if (frnd_msg->iv_index != iv_index)
  1451. frnd_msg->u.s12[0].hdr = 0;
  1452. /* Flush incomplete old SAR message if it doesn't match */
  1453. if ((frnd_msg->u.s12[0].hdr & HDR_KEY_MASK) != hdr_key) {
  1454. l_queue_remove(net->frnd_msgs, frnd_msg);
  1455. l_free(frnd_msg);
  1456. frnd_msg = NULL;
  1457. }
  1458. }
  1459. if (!frnd_msg) {
  1460. frnd_msg = mesh_friend_msg_new(segN);
  1461. frnd_msg->iv_index = iv_index;
  1462. frnd_msg->src = src;
  1463. frnd_msg->dst = dst;
  1464. frnd_msg->ttl = ttl;
  1465. l_queue_push_tail(net->frnd_msgs, frnd_msg);
  1466. } else if (frnd_msg->flags & this_seg_flag) /* Ignore dup segs */
  1467. return;
  1468. cnt = frnd_msg->cnt_in;
  1469. frnd_msg->flags |= this_seg_flag;
  1470. frnd_msg->u.s12[cnt].hdr = hdr;
  1471. frnd_msg->u.s12[cnt].seq = seq;
  1472. memcpy(frnd_msg->u.s12[cnt].data, data, size);
  1473. /* Last segment could be short */
  1474. if (segN == segO)
  1475. frnd_msg->last_len = size;
  1476. l_debug("RXed Seg %d, Flags %8.8x (cnt: %d)",
  1477. segO, frnd_msg->flags, cnt);
  1478. /* In reality, if one of these is true, then *both* must be true */
  1479. if ((cnt == segN) || (frnd_msg->flags == expected)) {
  1480. l_debug("Full ACK");
  1481. send_frnd_ack(net, dst, src, hdr, frnd_msg->flags);
  1482. if (frnd_msg->ttl > 1) {
  1483. frnd_msg->ttl--;
  1484. /* Add to friends cache */
  1485. l_queue_foreach(net->friends,
  1486. enqueue_friend_pkt, frnd_msg);
  1487. }
  1488. /* Remove from "in progress" queue */
  1489. l_queue_remove(net->frnd_msgs, frnd_msg);
  1490. /* TODO Optimization(?): Unicast messages keep this buffer */
  1491. l_free(frnd_msg);
  1492. return;
  1493. }
  1494. /* Always ACK if this is the largest outstanding segment */
  1495. if ((largest & frnd_msg->flags) == largest) {
  1496. l_debug("Partial ACK");
  1497. send_frnd_ack(net, dst, src, hdr, frnd_msg->flags);
  1498. }
  1499. frnd_msg->cnt_in++;
  1500. }
  1501. static bool seg_rxed(struct mesh_net *net, bool frnd, uint32_t iv_index,
  1502. uint8_t ttl, uint32_t seq,
  1503. uint16_t net_idx,
  1504. uint16_t src, uint16_t dst,
  1505. uint8_t key_aid,
  1506. bool szmic, uint16_t seqZero,
  1507. uint8_t segO, uint8_t segN,
  1508. const uint8_t *data, uint8_t size)
  1509. {
  1510. struct mesh_sar *sar_in = NULL;
  1511. uint16_t seg_off = 0;
  1512. uint32_t expected, this_seg_flag, largest, seqAuth;
  1513. bool reset_seg_to = true;
  1514. /*
  1515. * DST could receive additional Segments after
  1516. * completing due to a lost ACK, so re-ACK and discard
  1517. */
  1518. sar_in = l_queue_find(net->sar_in, match_sar_remote,
  1519. L_UINT_TO_PTR(src));
  1520. /* Discard *old* incoming-SAR-in-progress if this segment newer */
  1521. seqAuth = seq_auth(seq, seqZero);
  1522. if (sar_in && (sar_in->seqAuth != seqAuth ||
  1523. sar_in->iv_index != iv_index)) {
  1524. bool newer;
  1525. if (iv_index > sar_in->iv_index)
  1526. newer = true;
  1527. else if (iv_index == sar_in->iv_index)
  1528. newer = seqAuth > sar_in->seqAuth;
  1529. else
  1530. newer = false;
  1531. if (newer) {
  1532. /* Cancel Old, start New */
  1533. l_queue_remove(net->sar_in, sar_in);
  1534. mesh_sar_free(sar_in);
  1535. sar_in = NULL;
  1536. } else
  1537. /* Ignore Old */
  1538. return false;
  1539. }
  1540. expected = 0xffffffff >> (31 - segN);
  1541. if (sar_in) {
  1542. l_debug("RXed (old: %04x %06x size:%d) %d of %d",
  1543. seqZero, seq, size, segO, segN);
  1544. /* Sanity Check--> certain things must match */
  1545. if (SEG_MAX(true, sar_in->len) != segN ||
  1546. sar_in->key_aid != key_aid)
  1547. return false;
  1548. if (sar_in->flags == expected) {
  1549. /* Re-Send ACK for full msg */
  1550. send_net_ack(net, sar_in, expected);
  1551. return true;
  1552. }
  1553. } else {
  1554. uint16_t len = MAX_SEG_TO_LEN(segN);
  1555. l_debug("RXed (new: %04x %06x size: %d len: %d) %d of %d",
  1556. seqZero, seq, size, len, segO, segN);
  1557. l_debug("Queue Size: %d", l_queue_length(net->sar_in));
  1558. sar_in = mesh_sar_new(len);
  1559. sar_in->seqAuth = seqAuth;
  1560. sar_in->iv_index = iv_index;
  1561. sar_in->src = dst;
  1562. sar_in->remote = src;
  1563. sar_in->seqZero = seqZero;
  1564. sar_in->key_aid = key_aid;
  1565. sar_in->len = len;
  1566. sar_in->last_seg = 0xff;
  1567. sar_in->net_idx = net_idx;
  1568. sar_in->msg_timeout = l_timeout_create(MSG_TO,
  1569. inmsg_to, net, NULL);
  1570. l_debug("First Seg %4.4x", sar_in->flags);
  1571. l_queue_push_head(net->sar_in, sar_in);
  1572. }
  1573. seg_off = segO * MAX_SEG_LEN;
  1574. memcpy(sar_in->buf + seg_off, data, size);
  1575. this_seg_flag = 0x00000001 << segO;
  1576. /* Don't reset Seg TO or NAK if we already have this seg */
  1577. if (this_seg_flag & sar_in->flags)
  1578. reset_seg_to = false;
  1579. sar_in->flags |= this_seg_flag;
  1580. sar_in->ttl = ttl;
  1581. /* Msg length only definitive on last segment */
  1582. if (segO == segN)
  1583. sar_in->len = segN * MAX_SEG_LEN + size;
  1584. if (sar_in->flags == expected) {
  1585. /* Got it all */
  1586. send_net_ack(net, sar_in, expected);
  1587. msg_rxed(net, frnd, iv_index, ttl, seq, net_idx,
  1588. sar_in->remote, dst, key_aid, true, szmic,
  1589. sar_in->seqZero, sar_in->buf, sar_in->len);
  1590. /* Kill Inter-Seg timeout */
  1591. l_timeout_remove(sar_in->seg_timeout);
  1592. sar_in->seg_timeout = NULL;
  1593. return true;
  1594. }
  1595. if (reset_seg_to) {
  1596. /* Restart Inter-Seg Timeout */
  1597. l_timeout_remove(sar_in->seg_timeout);
  1598. /* if this is the largest outstanding segment, send NAK now */
  1599. largest = (0xffffffff << segO) & expected;
  1600. if ((largest & sar_in->flags) == largest)
  1601. send_net_ack(net, sar_in, sar_in->flags);
  1602. sar_in->seg_timeout = l_timeout_create(SEG_TO,
  1603. inseg_to, net, NULL);
  1604. } else
  1605. largest = 0;
  1606. l_debug("NAK: %d expected:%08x largest:%08x flags:%08x",
  1607. reset_seg_to, expected, largest, sar_in->flags);
  1608. return false;
  1609. }
  1610. static bool ctl_received(struct mesh_net *net, uint16_t key_id,
  1611. uint32_t iv_index, uint8_t ttl,
  1612. uint32_t seq,
  1613. uint16_t src, uint16_t dst,
  1614. uint8_t opcode, int8_t rssi,
  1615. const uint8_t *pkt, uint8_t len)
  1616. {
  1617. uint8_t msg[12];
  1618. uint8_t rsp_ttl = DEFAULT_TTL;
  1619. uint8_t n = 0;
  1620. uint16_t net_idx;
  1621. if (ttl > 1) {
  1622. uint32_t hdr = opcode << OPCODE_HDR_SHIFT;
  1623. uint8_t frnd_ttl = ttl - 1;
  1624. if (friend_packet_queue(net, iv_index, true, frnd_ttl, seq,
  1625. src, dst, hdr, pkt, len))
  1626. return true;
  1627. }
  1628. /* Don't process other peoples Unicast destinations */
  1629. if (dst < 0x8000 && (dst < net->src_addr || dst > net->last_addr))
  1630. return false;
  1631. switch (opcode) {
  1632. default:
  1633. l_error("Unsupported Ctl Opcode: %2.2x", opcode);
  1634. break;
  1635. case NET_OP_FRND_POLL:
  1636. if (len != 1 || ttl)
  1637. return false;
  1638. print_packet("Rx-NET_OP_FRND_POLL", pkt, len);
  1639. friend_poll(net, src, !!(pkt[0]), l_queue_find(net->friends,
  1640. match_by_friend, L_UINT_TO_PTR(src)));
  1641. break;
  1642. case NET_OP_FRND_REQUEST:
  1643. if (!net->friend_enable)
  1644. return false;
  1645. if (!IS_ALL_NODES(dst) && dst != FRIENDS_ADDRESS)
  1646. return false;
  1647. if (len != 10 || ttl)
  1648. return false;
  1649. print_packet("Rx-NET_OP_FRND_REQUEST", pkt, len);
  1650. net_idx = key_id_to_net_idx(net, key_id);
  1651. friend_request(net, net_idx, src, pkt[0], pkt[1],
  1652. l_get_be32(pkt + 1) & 0xffffff,
  1653. l_get_be16(pkt + 5), pkt[7],
  1654. l_get_be16(pkt + 8), rssi);
  1655. break;
  1656. case NET_OP_FRND_CLEAR_CONFIRM:
  1657. if (len != 4)
  1658. return false;
  1659. print_packet("Rx-NET_OP_FRND_CLEAR_CONFIRM", pkt, len);
  1660. friend_clear_confirm(net, src, l_get_be16(pkt),
  1661. l_get_be16(pkt + 2));
  1662. break;
  1663. case NET_OP_FRND_CLEAR:
  1664. if (len != 4 || dst != net->src_addr)
  1665. return false;
  1666. print_packet("Rx-NET_OP_FRND_CLEAR", pkt, len);
  1667. friend_clear(net, src, l_get_be16(pkt), l_get_be16(pkt + 2),
  1668. l_queue_find(net->friends, match_by_friend,
  1669. L_UINT_TO_PTR(l_get_be16(pkt))));
  1670. l_debug("Remaining Friends: %d", l_queue_length(net->friends));
  1671. break;
  1672. case NET_OP_PROXY_SUB_ADD:
  1673. if (ttl)
  1674. return false;
  1675. print_packet("Rx-NET_OP_PROXY_SUB_ADD", pkt, len);
  1676. friend_sub_add(net, l_queue_find(net->friends,
  1677. match_by_friend, L_UINT_TO_PTR(src)),
  1678. pkt, len);
  1679. break;
  1680. case NET_OP_PROXY_SUB_REMOVE:
  1681. if (ttl)
  1682. return false;
  1683. print_packet("Rx-NET_OP_PROXY_SUB_REMOVE", pkt, len);
  1684. friend_sub_del(net, l_queue_find(net->friends, match_by_friend,
  1685. L_UINT_TO_PTR(src)), pkt, len);
  1686. break;
  1687. case NET_OP_PROXY_SUB_CONFIRM:
  1688. if (ttl)
  1689. return false;
  1690. print_packet("Rx-NET_OP_PROXY_SUB_CONFIRM", pkt, len);
  1691. break;
  1692. case NET_OP_HEARTBEAT:
  1693. if (net->hb_sub.enabled && src == net->hb_sub.src) {
  1694. uint8_t hops = pkt[0] - ttl + 1;
  1695. print_packet("Rx-NET_OP_HEARTBEAT", pkt, len);
  1696. if (net->hb_sub.count != 0xffff)
  1697. net->hb_sub.count++;
  1698. if (net->hb_sub.min_hops > hops)
  1699. net->hb_sub.min_hops = hops;
  1700. if (net->hb_sub.max_hops < hops)
  1701. net->hb_sub.max_hops = hops;
  1702. l_debug("HB: cnt:%4.4x min:%2.2x max:%2.2x",
  1703. net->hb_sub.count, net->hb_sub.min_hops,
  1704. net->hb_sub.max_hops);
  1705. }
  1706. break;
  1707. }
  1708. if (n)
  1709. mesh_net_transport_send(net, 0, 0, mesh_net_get_iv_index(net),
  1710. rsp_ttl, 0, dst & 0x8000 ? 0 : dst,
  1711. src, msg, n);
  1712. return true;
  1713. }
  1714. static bool find_fast_hash(const void *a, const void *b)
  1715. {
  1716. const uint64_t *entry = a;
  1717. const uint64_t *test = b;
  1718. return *entry == *test;
  1719. }
  1720. static bool check_fast_cache(uint64_t hash)
  1721. {
  1722. void *found = l_queue_find(fast_cache, find_fast_hash, &hash);
  1723. uint64_t *new_hash;
  1724. if (found)
  1725. return false;
  1726. if (l_queue_length(fast_cache) >= FAST_CACHE_SIZE)
  1727. new_hash = l_queue_pop_head(fast_cache);
  1728. else
  1729. new_hash = l_malloc(sizeof(hash));
  1730. *new_hash = hash;
  1731. l_queue_push_tail(fast_cache, new_hash);
  1732. return true;
  1733. }
  1734. static bool match_by_dst(const void *a, const void *b)
  1735. {
  1736. const struct mesh_destination *dest = a;
  1737. uint16_t dst = L_PTR_TO_UINT(b);
  1738. return dest->dst == dst;
  1739. }
  1740. static void send_relay_pkt(struct mesh_net *net, uint8_t *data, uint8_t size)
  1741. {
  1742. uint8_t packet[30];
  1743. struct mesh_io *io = net->io;
  1744. struct mesh_io_send_info info = {
  1745. .type = MESH_IO_TIMING_TYPE_GENERAL,
  1746. .u.gen.interval = net->relay.interval,
  1747. .u.gen.cnt = net->relay.count,
  1748. .u.gen.min_delay = DEFAULT_MIN_DELAY,
  1749. .u.gen.max_delay = DEFAULT_MAX_DELAY
  1750. };
  1751. packet[0] = MESH_AD_TYPE_NETWORK;
  1752. memcpy(packet + 1, data, size);
  1753. mesh_io_send(io, &info, packet, size + 1);
  1754. }
  1755. static bool simple_match(const void *a, const void *b)
  1756. {
  1757. return a == b;
  1758. }
  1759. static void send_msg_pkt_oneshot(void *user_data)
  1760. {
  1761. struct oneshot_tx *tx = user_data;
  1762. struct mesh_net *net;
  1763. struct mesh_io_send_info info;
  1764. struct net_queue_data net_data = {
  1765. .info = NULL,
  1766. .data = tx->packet + 1,
  1767. .len = tx->size - 1,
  1768. .relay_advice = RELAY_NONE,
  1769. };
  1770. /* Send to local nodes first */
  1771. l_queue_foreach(nets, net_rx, &net_data);
  1772. /* Make sure specific network still valid */
  1773. net = l_queue_find(nets, simple_match, tx->net);
  1774. if (!net || net_data.relay_advice == RELAY_DISALLOWED) {
  1775. l_free(tx);
  1776. return;
  1777. }
  1778. tx->packet[0] = MESH_AD_TYPE_NETWORK;
  1779. info.type = MESH_IO_TIMING_TYPE_GENERAL;
  1780. info.u.gen.interval = tx->interval;
  1781. info.u.gen.cnt = tx->cnt;
  1782. info.u.gen.min_delay = DEFAULT_MIN_DELAY;
  1783. /* No extra randomization when sending regular mesh messages */
  1784. info.u.gen.max_delay = DEFAULT_MIN_DELAY;
  1785. mesh_io_send(net->io, &info, tx->packet, tx->size);
  1786. l_free(tx);
  1787. }
  1788. static void send_msg_pkt(struct mesh_net *net, uint8_t cnt, uint16_t interval,
  1789. uint8_t *packet, uint8_t size)
  1790. {
  1791. struct oneshot_tx *tx = l_new(struct oneshot_tx, 1);
  1792. tx->net = net;
  1793. tx->interval = interval;
  1794. tx->cnt = cnt;
  1795. tx->size = size;
  1796. memcpy(tx->packet, packet, size);
  1797. l_idle_oneshot(send_msg_pkt_oneshot, tx, NULL);
  1798. }
  1799. static enum _relay_advice packet_received(void *user_data,
  1800. uint32_t key_id, uint32_t iv_index,
  1801. const void *data, uint8_t size, int8_t rssi)
  1802. {
  1803. struct mesh_net *net = user_data;
  1804. const uint8_t *msg = data;
  1805. uint8_t app_msg_len;
  1806. uint8_t net_ttl, net_key_id, net_segO, net_segN, net_opcode;
  1807. uint32_t net_seq, cache_cookie;
  1808. uint16_t net_src, net_dst, net_seqZero;
  1809. uint16_t net_idx;
  1810. uint8_t packet[31];
  1811. bool net_ctl, net_segmented, net_szmic, net_relay;
  1812. memcpy(packet + 2, data, size);
  1813. net_idx = key_id_to_net_idx(net, key_id);
  1814. if (net_idx == NET_IDX_INVALID)
  1815. return RELAY_NONE;
  1816. print_packet("RX: Network [clr] :", packet + 2, size);
  1817. if (!mesh_crypto_packet_parse(packet + 2, size, &net_ctl, &net_ttl,
  1818. &net_seq, &net_src, &net_dst,
  1819. &cache_cookie, &net_opcode,
  1820. &net_segmented, &net_key_id, &net_szmic,
  1821. &net_relay, &net_seqZero, &net_segO,
  1822. &net_segN, &msg, &app_msg_len)) {
  1823. l_error("Failed to parse packet content");
  1824. return RELAY_NONE;
  1825. }
  1826. if (net_dst == 0) {
  1827. l_error("illegal parms: DST: %4.4x Ctl: %d TTL: %2.2x",
  1828. net_dst, net_ctl, net_ttl);
  1829. return RELAY_NONE;
  1830. }
  1831. /* Ignore if we originally sent this */
  1832. if (is_us(net, net_src, true))
  1833. return RELAY_NONE;
  1834. /*
  1835. * As a Relay, suppress repeats of last N packets that pass through
  1836. * The "cache_cookie" should be unique part of App message.
  1837. */
  1838. if (msg_in_cache(net, net_src, net_seq, cache_cookie))
  1839. return RELAY_NONE;
  1840. l_debug("RX: Network %04x -> %04x : TTL 0x%02x : IV : %8.8x SEQ 0x%06x",
  1841. net_src, net_dst, net_ttl, iv_index, net_seq);
  1842. if (is_us(net, net_dst, false) ||
  1843. (net_ctl && net_opcode == NET_OP_HEARTBEAT)) {
  1844. l_debug("RX: App 0x%04x -> 0x%04x : TTL 0x%02x : SEQ 0x%06x",
  1845. net_src, net_dst, net_ttl, net_seq);
  1846. if (net_ctl) {
  1847. l_debug("CTL - %4.4x RX", net_seqZero);
  1848. if (net_opcode == NET_OP_SEG_ACKNOWLEDGE) {
  1849. /* Illegal to send ACK to non-Unicast Addr */
  1850. if (net_dst & 0x8000)
  1851. return RELAY_NONE;
  1852. /* Pedantic check for correct size */
  1853. if (app_msg_len != 7)
  1854. return RELAY_NONE;
  1855. /* If this is an ACK to our friend queue-only */
  1856. if (is_lpn_friend(net, net_dst))
  1857. friend_ack_rxed(net, iv_index, net_seq,
  1858. net_src, net_dst, msg);
  1859. else
  1860. ack_received(net, false,
  1861. net_src, net_dst,
  1862. net_seqZero,
  1863. l_get_be32(msg + 3));
  1864. } else {
  1865. ctl_received(net, key_id, iv_index, net_ttl,
  1866. net_seq, net_src, net_dst,
  1867. net_opcode, rssi, msg,
  1868. app_msg_len);
  1869. }
  1870. } else if (net_segmented) {
  1871. /*
  1872. * If we accept SAR packets to non-Unicast, then
  1873. * Friend Sar at least needs to be Unicast Only
  1874. */
  1875. if (is_lpn_friend(net, net_dst) &&
  1876. !(net_dst & 0x8000)) {
  1877. /*
  1878. * Check TTL >= 2 before accepting segments
  1879. * for Friends
  1880. */
  1881. if (net_ttl >= 2) {
  1882. friend_seg_rxed(net, iv_index, net_ttl,
  1883. net_seq, net_src, net_dst,
  1884. l_get_be32(packet + 2 + 9),
  1885. msg, app_msg_len);
  1886. }
  1887. } else {
  1888. seg_rxed(net, NULL, iv_index, net_ttl,
  1889. net_seq, net_idx, net_src,
  1890. net_dst, net_key_id, net_szmic,
  1891. net_seqZero, net_segO, net_segN,
  1892. msg, app_msg_len);
  1893. }
  1894. } else {
  1895. msg_rxed(net, NULL, iv_index, net_ttl, net_seq, net_idx,
  1896. net_src, net_dst, net_key_id, false,
  1897. false, net_seq & SEQ_ZERO_MASK, msg,
  1898. app_msg_len);
  1899. }
  1900. /* If this is one of our Unicast addresses, disallow relay */
  1901. if (IS_UNICAST(net_dst))
  1902. return RELAY_DISALLOWED;
  1903. }
  1904. /* If relay not enable, or no more hops allowed */
  1905. if (!net->relay.enable || net_ttl < 0x02)
  1906. return RELAY_NONE;
  1907. /* Group or Virtual destinations should *always* be relayed */
  1908. if (IS_GROUP(net_dst) || IS_VIRTUAL(net_dst))
  1909. return RELAY_ALWAYS;
  1910. /* Unicast destinations for other nodes *may* be relayed */
  1911. else if (IS_UNICAST(net_dst))
  1912. return RELAY_ALLOWED;
  1913. /* Otherwise, do not make a relay decision */
  1914. else
  1915. return RELAY_NONE;
  1916. }
  1917. static void net_rx(void *net_ptr, void *user_data)
  1918. {
  1919. struct net_queue_data *data = user_data;
  1920. struct mesh_net *net = net_ptr;
  1921. enum _relay_advice relay_advice;
  1922. uint8_t *out;
  1923. size_t out_size;
  1924. uint32_t key_id;
  1925. int8_t rssi = 0;
  1926. bool ivi_net = !!(net->iv_index & 1);
  1927. bool ivi_pkt = !!(data->data[0] & 0x80);
  1928. /* if IVI flag differs, use previous IV Index */
  1929. uint32_t iv_index = net->iv_index - (ivi_pkt ^ ivi_net);
  1930. key_id = net_key_decrypt(iv_index, data->data, data->len,
  1931. &out, &out_size);
  1932. if (!key_id)
  1933. return;
  1934. if (!data->seen) {
  1935. data->seen = true;
  1936. print_packet("RX: Network [enc] :", data->data, data->len);
  1937. }
  1938. if (data->info) {
  1939. net->instant = data->info->instant;
  1940. net->chan = data->info->chan;
  1941. rssi = data->info->rssi;
  1942. }
  1943. relay_advice = packet_received(net, key_id, iv_index, out, out_size,
  1944. rssi);
  1945. if (relay_advice > data->relay_advice) {
  1946. data->iv_index = iv_index;
  1947. data->relay_advice = relay_advice;
  1948. data->key_id = key_id;
  1949. data->net = net;
  1950. data->out = out;
  1951. data->out_size = out_size;
  1952. }
  1953. }
  1954. static void net_msg_recv(void *user_data, struct mesh_io_recv_info *info,
  1955. const uint8_t *data, uint16_t len)
  1956. {
  1957. uint64_t hash;
  1958. bool isNew;
  1959. struct net_queue_data net_data = {
  1960. .info = info,
  1961. .data = data + 1,
  1962. .len = len - 1,
  1963. .relay_advice = RELAY_NONE,
  1964. .seen = false,
  1965. };
  1966. if (len < 9)
  1967. return;
  1968. hash = l_get_le64(data + 1);
  1969. /* Only process packet once per reception */
  1970. isNew = check_fast_cache(hash);
  1971. if (!isNew)
  1972. return;
  1973. l_queue_foreach(nets, net_rx, &net_data);
  1974. if (net_data.relay_advice == RELAY_ALWAYS ||
  1975. net_data.relay_advice == RELAY_ALLOWED) {
  1976. uint8_t ttl = net_data.out[1] & TTL_MASK;
  1977. net_data.out[1] &= ~TTL_MASK;
  1978. net_data.out[1] |= ttl - 1;
  1979. net_key_encrypt(net_data.key_id, net_data.iv_index,
  1980. net_data.out, net_data.out_size);
  1981. send_relay_pkt(net_data.net, net_data.out, net_data.out_size);
  1982. }
  1983. }
  1984. static void iv_upd_to(struct l_timeout *upd_timeout, void *user_data)
  1985. {
  1986. struct mesh_net *net = user_data;
  1987. switch (net->iv_upd_state) {
  1988. case IV_UPD_UPDATING:
  1989. if (l_queue_length(net->sar_out) ||
  1990. l_queue_length(net->sar_queue)) {
  1991. l_debug("don't leave IV Update until sar_out empty");
  1992. l_timeout_modify(net->iv_update_timeout, 10);
  1993. break;
  1994. }
  1995. l_debug("iv_upd_state = IV_UPD_NORMAL_HOLD");
  1996. net->iv_upd_state = IV_UPD_NORMAL_HOLD;
  1997. l_timeout_modify(net->iv_update_timeout, IV_IDX_UPD_MIN);
  1998. if (net->iv_update)
  1999. mesh_net_set_seq_num(net, 0);
  2000. net->iv_update = false;
  2001. mesh_config_write_iv_index(node_config_get(net->node),
  2002. net->iv_index, false);
  2003. l_queue_foreach(net->subnets, refresh_beacon, net);
  2004. queue_friend_update(net);
  2005. l_queue_clear(net->msg_cache, l_free);
  2006. break;
  2007. case IV_UPD_INIT:
  2008. case IV_UPD_NORMAL_HOLD:
  2009. case IV_UPD_NORMAL:
  2010. l_timeout_remove(upd_timeout);
  2011. net->iv_update_timeout = NULL;
  2012. l_debug("iv_upd_state = IV_UPD_NORMAL");
  2013. net->iv_upd_state = IV_UPD_NORMAL;
  2014. if (net->iv_update)
  2015. mesh_net_set_seq_num(net, 0);
  2016. net->iv_update = false;
  2017. if (net->seq_num > IV_UPDATE_SEQ_TRIGGER)
  2018. mesh_net_iv_index_update(net);
  2019. break;
  2020. }
  2021. }
  2022. static int key_refresh_phase_two(struct mesh_net *net, uint16_t idx)
  2023. {
  2024. struct mesh_subnet *subnet;
  2025. if (!net)
  2026. return MESH_STATUS_UNSPECIFIED_ERROR;
  2027. subnet = l_queue_find(net->subnets, match_key_index,
  2028. L_UINT_TO_PTR(idx));
  2029. if (!subnet || !subnet->net_key_upd)
  2030. return MESH_STATUS_INVALID_NETKEY;
  2031. l_debug("Key refresh procedure phase 2: start using new net TX keys");
  2032. if (subnet->kr_phase == KEY_REFRESH_PHASE_TWO)
  2033. return MESH_STATUS_SUCCESS;
  2034. subnet->key_refresh = 1;
  2035. subnet->net_key_tx = subnet->net_key_upd;
  2036. /*
  2037. * TODO: Provisioner may need to stay in phase three until
  2038. * it hears beacons from all the nodes
  2039. */
  2040. subnet->kr_phase = KEY_REFRESH_PHASE_TWO;
  2041. refresh_beacon(subnet, net);
  2042. queue_friend_update(net);
  2043. l_queue_foreach(net->friends, frnd_kr_phase2, net);
  2044. if (!mesh_config_net_key_set_phase(node_config_get(net->node), idx,
  2045. KEY_REFRESH_PHASE_TWO))
  2046. return MESH_STATUS_STORAGE_FAIL;
  2047. return MESH_STATUS_SUCCESS;
  2048. }
  2049. static int key_refresh_finish(struct mesh_net *net, uint16_t idx)
  2050. {
  2051. struct mesh_subnet *subnet;
  2052. if (!net)
  2053. return MESH_STATUS_UNSPECIFIED_ERROR;
  2054. subnet = l_queue_find(net->subnets, match_key_index,
  2055. L_UINT_TO_PTR(idx));
  2056. if (!subnet || !subnet->net_key_upd)
  2057. return MESH_STATUS_INVALID_NETKEY;
  2058. if (subnet->kr_phase == KEY_REFRESH_PHASE_NONE)
  2059. return MESH_STATUS_SUCCESS;
  2060. l_debug("Key refresh phase 3: use new keys only, discard old ones");
  2061. /* Switch to using new keys, discard old ones */
  2062. net_key_unref(subnet->net_key_cur);
  2063. subnet->net_key_tx = subnet->net_key_cur = subnet->net_key_upd;
  2064. subnet->net_key_upd = 0;
  2065. subnet->key_refresh = 0;
  2066. subnet->kr_phase = KEY_REFRESH_PHASE_NONE;
  2067. refresh_beacon(subnet, net);
  2068. queue_friend_update(net);
  2069. l_queue_foreach(net->friends, frnd_kr_phase3, net);
  2070. appkey_finalize(net, idx);
  2071. if (!mesh_config_net_key_set_phase(node_config_get(net->node), idx,
  2072. KEY_REFRESH_PHASE_NONE))
  2073. return MESH_STATUS_STORAGE_FAIL;
  2074. return MESH_STATUS_SUCCESS;
  2075. }
  2076. static bool update_kr_state(struct mesh_subnet *subnet, bool kr, uint32_t id)
  2077. {
  2078. /* Figure out the key refresh phase */
  2079. if (kr) {
  2080. if (id == subnet->net_key_upd) {
  2081. l_debug("Beacon based KR phase 2 change");
  2082. return (key_refresh_phase_two(subnet->net, subnet->idx)
  2083. == MESH_STATUS_SUCCESS);
  2084. }
  2085. } else {
  2086. if (id == subnet->net_key_upd) {
  2087. l_debug("Beacon based KR phase 3 change");
  2088. return (key_refresh_finish(subnet->net, subnet->idx)
  2089. == MESH_STATUS_SUCCESS);
  2090. }
  2091. }
  2092. return false;
  2093. }
  2094. static bool update_iv_ivu_state(struct mesh_net *net, uint32_t iv_index,
  2095. bool ivu)
  2096. {
  2097. if ((iv_index - ivu) > (net->iv_index - net->iv_update)) {
  2098. /* Don't accept IV_Index changes when performing SAR Out */
  2099. if (l_queue_length(net->sar_out))
  2100. return false;
  2101. }
  2102. /* If first beacon seen, accept without judgement */
  2103. if (net->iv_upd_state == IV_UPD_INIT) {
  2104. if (ivu) {
  2105. /* Ignore beacons with IVU if IV already updated */
  2106. if (iv_index == net->iv_index && !net->iv_update)
  2107. return false;
  2108. /*
  2109. * Other devices will be accepting old or new iv_index,
  2110. * but we don't know how far through update they are.
  2111. * Starting permissive state will allow us maximum
  2112. * (96 hours) to resync
  2113. */
  2114. l_debug("iv_upd_state = IV_UPD_UPDATING");
  2115. net->iv_upd_state = IV_UPD_UPDATING;
  2116. net->iv_update_timeout = l_timeout_create(
  2117. IV_IDX_UPD_MIN, iv_upd_to, net, NULL);
  2118. } else {
  2119. l_debug("iv_upd_state = IV_UPD_NORMAL");
  2120. net->iv_upd_state = IV_UPD_NORMAL;
  2121. }
  2122. } else if (ivu) {
  2123. /* Ignore beacons with IVU if they come too soon */
  2124. if (!net->iv_update &&
  2125. net->iv_upd_state == IV_UPD_NORMAL_HOLD) {
  2126. l_error("Update attempted too soon");
  2127. return false;
  2128. }
  2129. /* Ignore beacons with IVU if IV already updated */
  2130. if (iv_index == net->iv_index)
  2131. return false;
  2132. if (!net->iv_update) {
  2133. l_debug("iv_upd_state = IV_UPD_UPDATING");
  2134. net->iv_upd_state = IV_UPD_UPDATING;
  2135. net->iv_update_timeout = l_timeout_create(
  2136. IV_IDX_UPD_MIN, iv_upd_to, net, NULL);
  2137. }
  2138. } else if (net->iv_update) {
  2139. l_error("IVU clear attempted too soon");
  2140. return false;
  2141. }
  2142. if ((iv_index - ivu) > (net->iv_index - net->iv_update))
  2143. mesh_net_set_seq_num(net, 0);
  2144. if (ivu != net->iv_update || iv_index != net->iv_index) {
  2145. struct mesh_config *cfg = node_config_get(net->node);
  2146. mesh_config_write_iv_index(cfg, iv_index, ivu);
  2147. /* Cleanup Replay Protection List NVM */
  2148. rpl_update(net->node, iv_index);
  2149. }
  2150. node_property_changed(net->node, "IVIndex");
  2151. net->iv_index = iv_index;
  2152. net->iv_update = ivu;
  2153. return true;
  2154. }
  2155. static void process_beacon(void *net_ptr, void *user_data)
  2156. {
  2157. bool updated = false;
  2158. struct mesh_net *net = net_ptr;
  2159. struct net_beacon_data *beacon_data = user_data;
  2160. uint32_t ivi;
  2161. bool ivu, kr, local_kr;
  2162. struct mesh_subnet *subnet;
  2163. ivi = beacon_data->ivi;
  2164. /* Ignore out-of-range IV_Index for this network */
  2165. if ((net->iv_index + IV_IDX_DIFF_RANGE < ivi) || (ivi < net->iv_index))
  2166. return;
  2167. /* Ignore beacons not in this universe */
  2168. subnet = l_queue_find(net->subnets, match_key_id,
  2169. L_UINT_TO_PTR(beacon_data->key_id));
  2170. if (!subnet)
  2171. return;
  2172. /* Get IVU and KR boolean bits from beacon */
  2173. ivu = beacon_data->ivu;
  2174. kr = beacon_data->kr;
  2175. local_kr = !!(subnet->kr_phase == KEY_REFRESH_PHASE_TWO);
  2176. /* We have officially *seen* this beacon now */
  2177. beacon_data->processed = true;
  2178. /*
  2179. * Ignore the beacon if it doesn't change anything, unless we're
  2180. * doing IV Recovery
  2181. */
  2182. if (net->iv_upd_state == IV_UPD_INIT || ivi != net->iv_index ||
  2183. ivu != net->iv_update)
  2184. updated |= update_iv_ivu_state(net, ivi, ivu);
  2185. if (kr != local_kr)
  2186. updated |= update_kr_state(subnet, kr, beacon_data->key_id);
  2187. if (updated)
  2188. net_key_beacon_refresh(beacon_data->key_id, net->iv_index,
  2189. !!(subnet->kr_phase == KEY_REFRESH_PHASE_TWO),
  2190. net->iv_update);
  2191. }
  2192. static void beacon_recv(void *user_data, struct mesh_io_recv_info *info,
  2193. const uint8_t *data, uint16_t len)
  2194. {
  2195. struct net_beacon_data beacon_data = {
  2196. .processed = false,
  2197. };
  2198. if (len != 23 || data[1] != 0x01)
  2199. return;
  2200. /* Ignore Network IDs unknown to this daemon */
  2201. beacon_data.key_id = net_key_network_id(data + 3);
  2202. if (!beacon_data.key_id)
  2203. return;
  2204. /* Get data bits from beacon */
  2205. beacon_data.ivu = !!(data[2] & 0x02);
  2206. beacon_data.kr = !!(data[2] & 0x01);
  2207. beacon_data.ivi = l_get_be32(data + 11);
  2208. /* Validate beacon before accepting */
  2209. if (!net_key_snb_check(beacon_data.key_id, beacon_data.ivi,
  2210. beacon_data.kr, beacon_data.ivu,
  2211. l_get_be64(data + 15))) {
  2212. l_error("mesh_crypto_beacon verify failed");
  2213. return;
  2214. }
  2215. l_queue_foreach(nets, process_beacon, &beacon_data);
  2216. if (beacon_data.processed)
  2217. net_key_beacon_seen(beacon_data.key_id);
  2218. }
  2219. void net_local_beacon(uint32_t key_id, uint8_t *beacon)
  2220. {
  2221. struct net_beacon_data beacon_data = {
  2222. .key_id = key_id,
  2223. .ivu = !!(beacon[2] & 0x02),
  2224. .kr = !!(beacon[2] & 0x01),
  2225. .ivi = l_get_be32(beacon + 11),
  2226. };
  2227. /* Deliver locally generated beacons to all nodes */
  2228. l_queue_foreach(nets, process_beacon, &beacon_data);
  2229. }
  2230. bool mesh_net_set_beacon_mode(struct mesh_net *net, bool enable)
  2231. {
  2232. if (!net)
  2233. return false;
  2234. if (net->beacon_enable == enable)
  2235. return true;
  2236. net->beacon_enable = enable;
  2237. if (enable)
  2238. l_queue_foreach(net->subnets, refresh_beacon, net);
  2239. l_queue_foreach(net->subnets, enable_beacon, net);
  2240. queue_friend_update(net);
  2241. return true;
  2242. }
  2243. /* This function is called when network keys are restored from storage. */
  2244. bool mesh_net_set_key(struct mesh_net *net, uint16_t idx, const uint8_t *key,
  2245. const uint8_t *new_key, uint8_t phase)
  2246. {
  2247. struct mesh_subnet *subnet;
  2248. /* Current key must be always present */
  2249. if (!key)
  2250. return false;
  2251. /* If key refresh is in progress, a new key must be present */
  2252. if (phase != KEY_REFRESH_PHASE_NONE && !new_key)
  2253. return false;
  2254. /* Check if the subnet with the specified index already exists */
  2255. subnet = l_queue_find(net->subnets, match_key_index,
  2256. L_UINT_TO_PTR(idx));
  2257. if (subnet)
  2258. return false;
  2259. subnet = add_key(net, idx, key);
  2260. if (!subnet)
  2261. return false;
  2262. if (new_key && phase)
  2263. subnet->net_key_upd = net_key_add(new_key);
  2264. /* Preserve key refresh state to generate secure beacon flags*/
  2265. if (phase == KEY_REFRESH_PHASE_TWO) {
  2266. subnet->key_refresh = 1;
  2267. subnet->net_key_tx = subnet->net_key_upd;
  2268. if (net->beacon_enable) {
  2269. /* Switch beaconing key */
  2270. net_key_beacon_disable(subnet->net_key_cur);
  2271. net_key_beacon_enable(subnet->net_key_upd);
  2272. }
  2273. }
  2274. subnet->kr_phase = phase;
  2275. net_key_beacon_refresh(subnet->net_key_tx, net->iv_index,
  2276. !!(subnet->kr_phase == KEY_REFRESH_PHASE_TWO), net->iv_update);
  2277. return true;
  2278. }
  2279. bool mesh_net_attach(struct mesh_net *net, struct mesh_io *io)
  2280. {
  2281. bool first;
  2282. if (!net)
  2283. return false;
  2284. first = l_queue_isempty(nets);
  2285. if (first) {
  2286. uint8_t snb[] = {MESH_AD_TYPE_BEACON, 0x01};
  2287. uint8_t pkt[] = {MESH_AD_TYPE_NETWORK};
  2288. if (!nets)
  2289. nets = l_queue_new();
  2290. if (!fast_cache)
  2291. fast_cache = l_queue_new();
  2292. mesh_io_register_recv_cb(io, snb, sizeof(snb),
  2293. beacon_recv, NULL);
  2294. mesh_io_register_recv_cb(io, pkt, sizeof(pkt),
  2295. net_msg_recv, NULL);
  2296. }
  2297. if (l_queue_find(nets, simple_match, net))
  2298. return false;
  2299. l_queue_push_head(nets, net);
  2300. net->io = io;
  2301. return true;
  2302. }
  2303. struct mesh_io *mesh_net_detach(struct mesh_net *net)
  2304. {
  2305. uint8_t snb[] = {MESH_AD_TYPE_BEACON, 0x01};
  2306. uint8_t pkt[] = {MESH_AD_TYPE_NETWORK};
  2307. struct mesh_io *io;
  2308. uint8_t type = 0;
  2309. if (!net || !net->io)
  2310. return NULL;
  2311. io = net->io;
  2312. mesh_io_send_cancel(net->io, &type, 1);
  2313. /* Only deregister io if this is the last network detached.*/
  2314. if (l_queue_length(nets) < 2) {
  2315. mesh_io_deregister_recv_cb(io, snb, sizeof(snb));
  2316. mesh_io_deregister_recv_cb(io, pkt, sizeof(pkt));
  2317. }
  2318. net->io = NULL;
  2319. l_queue_remove(nets, net);
  2320. return io;
  2321. }
  2322. bool mesh_net_iv_index_update(struct mesh_net *net)
  2323. {
  2324. if (net->iv_upd_state != IV_UPD_NORMAL)
  2325. return false;
  2326. l_debug("iv_upd_state = IV_UPD_UPDATING");
  2327. l_queue_clear(net->msg_cache, l_free);
  2328. if (!mesh_config_write_iv_index(node_config_get(net->node),
  2329. net->iv_index + 1, true))
  2330. return false;
  2331. net->iv_upd_state = IV_UPD_UPDATING;
  2332. net->iv_index++;
  2333. net->iv_update = true;
  2334. l_queue_foreach(net->subnets, refresh_beacon, net);
  2335. queue_friend_update(net);
  2336. net->iv_update_timeout = l_timeout_create(
  2337. IV_IDX_UPD_MIN,
  2338. iv_upd_to, net, NULL);
  2339. return true;
  2340. }
  2341. bool mesh_net_dst_reg(struct mesh_net *net, uint16_t dst)
  2342. {
  2343. struct mesh_destination *dest = l_queue_find(net->destinations,
  2344. match_by_dst, L_UINT_TO_PTR(dst));
  2345. if (IS_UNASSIGNED(dst) || IS_ALL_NODES(dst))
  2346. return false;
  2347. if (!dest) {
  2348. dest = l_new(struct mesh_destination, 1);
  2349. if (dst < 0x8000)
  2350. l_queue_push_head(net->destinations, dest);
  2351. else
  2352. l_queue_push_tail(net->destinations, dest);
  2353. }
  2354. dest->dst = dst;
  2355. dest->ref_cnt++;
  2356. return true;
  2357. }
  2358. bool mesh_net_dst_unreg(struct mesh_net *net, uint16_t dst)
  2359. {
  2360. struct mesh_destination *dest = l_queue_find(net->destinations,
  2361. match_by_dst, L_UINT_TO_PTR(dst));
  2362. if (!dest)
  2363. return false;
  2364. if (dest->ref_cnt)
  2365. dest->ref_cnt--;
  2366. if (dest->ref_cnt)
  2367. return true;
  2368. l_queue_remove(net->destinations, dest);
  2369. l_free(dest);
  2370. return true;
  2371. }
  2372. static bool send_seg(struct mesh_net *net, uint8_t cnt, uint16_t interval,
  2373. struct mesh_sar *msg, uint8_t segO)
  2374. {
  2375. struct mesh_subnet *subnet;
  2376. uint8_t seg_len;
  2377. uint8_t gatt_data[30];
  2378. uint8_t *packet = gatt_data;
  2379. uint8_t packet_len;
  2380. uint8_t segN = SEG_MAX(msg->segmented, msg->len);
  2381. uint16_t seg_off = SEG_OFF(segO);
  2382. uint32_t seq_num;
  2383. if (msg->segmented) {
  2384. /* Send each segment on unique seq_num */
  2385. seq_num = mesh_net_next_seq_num(net);
  2386. if (msg->len - seg_off > SEG_OFF(1))
  2387. seg_len = SEG_OFF(1);
  2388. else
  2389. seg_len = msg->len - seg_off;
  2390. } else {
  2391. /* Send on same seq_num used for Access Layer */
  2392. seq_num = msg->seqAuth;
  2393. seg_len = msg->len;
  2394. }
  2395. /* Start IV Update procedure when we hit our trigger point */
  2396. if (!msg->frnd && net->seq_num > IV_UPDATE_SEQ_TRIGGER)
  2397. mesh_net_iv_index_update(net);
  2398. l_debug("segN %d segment %d seg_off %d", segN, segO, seg_off);
  2399. /* TODO: Are we RXing on an LPN's behalf? Then set RLY bit */
  2400. if (!mesh_crypto_packet_build(false, msg->ttl, seq_num, msg->src,
  2401. msg->remote, 0, msg->segmented,
  2402. msg->key_aid, msg->szmic, false,
  2403. msg->seqZero, segO, segN,
  2404. msg->buf + seg_off, seg_len,
  2405. packet + 1, &packet_len)) {
  2406. l_error("Failed to build packet");
  2407. return false;
  2408. }
  2409. print_packet("Clr-Net Tx", packet + 1, packet_len);
  2410. subnet = l_queue_find(net->subnets, match_key_index,
  2411. L_UINT_TO_PTR(msg->net_idx));
  2412. if (!subnet)
  2413. return false;
  2414. if (!net_key_encrypt(subnet->net_key_tx, msg->iv_index, packet + 1,
  2415. packet_len)) {
  2416. l_error("Failed to encode packet");
  2417. return false;
  2418. }
  2419. send_msg_pkt(net, cnt, interval, packet, packet_len + 1);
  2420. msg->last_seg = segO;
  2421. return true;
  2422. }
  2423. void mesh_net_send_seg(struct mesh_net *net, uint32_t net_key_id,
  2424. uint32_t iv_index, uint8_t ttl, uint32_t seq,
  2425. uint16_t src, uint16_t dst, uint32_t hdr,
  2426. const void *seg, uint16_t seg_len)
  2427. {
  2428. uint8_t packet[30];
  2429. uint8_t packet_len;
  2430. bool segmented = !!((hdr >> SEG_HDR_SHIFT) & true);
  2431. uint8_t app_key_id = (hdr >> KEY_HDR_SHIFT) & KEY_ID_MASK;
  2432. bool szmic = !!((hdr >> SZMIC_HDR_SHIFT) & true);
  2433. uint16_t seqZero = (hdr >> SEQ_ZERO_HDR_SHIFT) & SEQ_ZERO_MASK;
  2434. uint8_t segO = (hdr >> SEGO_HDR_SHIFT) & SEG_MASK;
  2435. uint8_t segN = (hdr >> SEGN_HDR_SHIFT) & SEG_MASK;
  2436. /* TODO: Only used for current POLLed segments to LPNs */
  2437. l_debug("SEQ: %6.6x", seq + segO);
  2438. l_debug("SEQ0: %6.6x", seq);
  2439. l_debug("segO: %d", segO);
  2440. if (!mesh_crypto_packet_build(false, ttl, seq, src, dst, 0,
  2441. segmented, app_key_id, szmic, false,
  2442. seqZero, segO, segN, seg, seg_len,
  2443. packet + 1, &packet_len)) {
  2444. l_error("Failed to build packet");
  2445. return;
  2446. }
  2447. if (!net_key_encrypt(net_key_id, iv_index, packet + 1, packet_len)) {
  2448. l_error("Failed to encode packet");
  2449. return;
  2450. }
  2451. send_msg_pkt(net, net->tx_cnt, net->tx_interval, packet,
  2452. packet_len + 1);
  2453. l_debug("TX: Friend Seg-%d %04x -> %04x : len %u) : TTL %d : SEQ %06x",
  2454. segO, src, dst, packet_len, ttl, seq);
  2455. print_packet("TX: Friend", packet + 1, packet_len);
  2456. }
  2457. bool mesh_net_app_send(struct mesh_net *net, bool frnd_cred, uint16_t src,
  2458. uint16_t dst, uint8_t key_aid, uint16_t net_idx,
  2459. uint8_t ttl, uint8_t cnt, uint16_t interval,
  2460. uint32_t seq, uint32_t iv_index, bool segmented,
  2461. bool szmic, const void *msg, uint16_t msg_len)
  2462. {
  2463. struct mesh_sar *payload = NULL;
  2464. uint8_t seg, seg_max;
  2465. bool result;
  2466. if (!net || msg_len > 384)
  2467. return false;
  2468. if (!src)
  2469. src = net->src_addr;
  2470. if (!src || !dst)
  2471. return false;
  2472. if (ttl == DEFAULT_TTL)
  2473. ttl = net->default_ttl;
  2474. /* Long and sizmic messages *require* segmenting */
  2475. segmented |= szmic;
  2476. seg_max = SEG_MAX(segmented, msg_len);
  2477. segmented |= !!(seg_max);
  2478. /* First enqueue to any Friends and internal models */
  2479. result = msg_rxed(net, false, iv_index, ttl, seq, net_idx, src, dst,
  2480. key_aid, segmented, szmic, seq & SEQ_ZERO_MASK,
  2481. msg, msg_len);
  2482. /*
  2483. * If addressed to a unicast address and successfully enqueued,
  2484. * or delivered to one of our Unicast addresses we are done
  2485. */
  2486. if ((result && IS_UNICAST(dst)) || src == dst ||
  2487. (dst >= net->src_addr && dst <= net->last_addr))
  2488. return true;
  2489. /* Setup OTA Network send */
  2490. payload = mesh_sar_new(msg_len);
  2491. memcpy(payload->buf, msg, msg_len);
  2492. payload->len = msg_len;
  2493. payload->src = src;
  2494. payload->remote = dst;
  2495. payload->ttl = ttl;
  2496. payload->szmic = szmic;
  2497. payload->frnd_cred = frnd_cred;
  2498. payload->key_aid = key_aid;
  2499. payload->net_idx = net_idx;
  2500. payload->iv_index = mesh_net_get_iv_index(net);
  2501. payload->seqAuth = seq;
  2502. payload->segmented = segmented;
  2503. if (segmented) {
  2504. payload->flags = 0xffffffff >> (31 - seg_max);
  2505. payload->seqZero = seq & SEQ_ZERO_MASK;
  2506. payload->id = ++net->sar_id_next;
  2507. /* Single thread SAR messages to same Unicast DST */
  2508. if (l_queue_find(net->sar_out, match_sar_remote,
  2509. L_UINT_TO_PTR(dst))) {
  2510. /* Delay sending Outbound SAR unless prior
  2511. * SAR to same DST has completed */
  2512. l_debug("OB-Queued SeqZero: %4.4x", payload->seqZero);
  2513. l_queue_push_tail(net->sar_queue, payload);
  2514. return true;
  2515. }
  2516. }
  2517. result = true;
  2518. if (!IS_UNICAST(dst) && segmented) {
  2519. int i;
  2520. for (i = 0; i < 4; i++) {
  2521. for (seg = 0; seg <= seg_max && result; seg++)
  2522. result = send_seg(net, cnt, interval, payload,
  2523. seg);
  2524. }
  2525. } else {
  2526. for (seg = 0; seg <= seg_max && result; seg++)
  2527. result = send_seg(net, cnt, interval, payload, seg);
  2528. }
  2529. /* Reliable: Cache; Unreliable: Flush*/
  2530. if (result && segmented && IS_UNICAST(dst)) {
  2531. l_queue_push_head(net->sar_out, payload);
  2532. payload->seg_timeout =
  2533. l_timeout_create(SEG_TO, outseg_to, net, NULL);
  2534. payload->msg_timeout =
  2535. l_timeout_create(MSG_TO, outmsg_to, net, NULL);
  2536. payload->id = ++net->sar_id_next;
  2537. } else
  2538. mesh_sar_free(payload);
  2539. return result;
  2540. }
  2541. void mesh_net_ack_send(struct mesh_net *net, uint32_t key_id, uint32_t iv_index,
  2542. uint8_t ttl, uint32_t seq, uint16_t src, uint16_t dst,
  2543. bool rly, uint16_t seqZero, uint32_t ack_flags)
  2544. {
  2545. uint32_t hdr;
  2546. uint8_t data[7];
  2547. uint8_t pkt_len;
  2548. uint8_t pkt[30];
  2549. hdr = NET_OP_SEG_ACKNOWLEDGE << OPCODE_HDR_SHIFT;
  2550. hdr |= rly << RELAY_HDR_SHIFT;
  2551. hdr |= (seqZero & SEQ_ZERO_MASK) << SEQ_ZERO_HDR_SHIFT;
  2552. l_put_be32(hdr, data);
  2553. l_put_be32(ack_flags, data + 3);
  2554. /* Not Segmented, no Key ID associated, no segO or segN */
  2555. if (!mesh_crypto_packet_build(true, ttl, seq, src, dst,
  2556. NET_OP_SEG_ACKNOWLEDGE, false, 0, false,
  2557. rly, seqZero, 0, 0, data + 1, 6,
  2558. pkt + 1, &pkt_len))
  2559. return;
  2560. if (!key_id) {
  2561. struct mesh_subnet *subnet = get_primary_subnet(net);
  2562. key_id = subnet->net_key_tx;
  2563. }
  2564. if (!net_key_encrypt(key_id, iv_index, pkt + 1, pkt_len)) {
  2565. l_error("Failed to encode packet");
  2566. return;
  2567. }
  2568. send_msg_pkt(net, net->tx_cnt, net->tx_interval, pkt, pkt_len + 1);
  2569. l_debug("TX: Friend ACK %04x -> %04x : len %u : TTL %d : SEQ %06x",
  2570. src, dst, pkt_len, ttl, seq);
  2571. print_packet("TX: Friend ACK", pkt + 1, pkt_len);
  2572. }
  2573. void mesh_net_transport_send(struct mesh_net *net, uint32_t key_id,
  2574. uint16_t net_idx, uint32_t iv_index,
  2575. uint8_t ttl, uint32_t seq, uint16_t src,
  2576. uint16_t dst, const uint8_t *msg,
  2577. uint16_t msg_len)
  2578. {
  2579. uint32_t use_seq = seq;
  2580. uint8_t pkt_len;
  2581. uint8_t pkt[30];
  2582. bool result = false;
  2583. if (!net->src_addr)
  2584. return;
  2585. if (!src)
  2586. src = net->src_addr;
  2587. if (src == dst)
  2588. return;
  2589. if (ttl == DEFAULT_TTL)
  2590. ttl = net->default_ttl;
  2591. /* Range check the Opcode and msg length*/
  2592. if (*msg & 0xc0 || (9 + msg_len + 8 > 29))
  2593. return;
  2594. /* Enqueue for Friend if forwardable and from us */
  2595. if (!key_id && src >= net->src_addr && src <= net->last_addr) {
  2596. uint32_t hdr = msg[0] << OPCODE_HDR_SHIFT;
  2597. uint8_t frnd_ttl = ttl;
  2598. if (friend_packet_queue(net, iv_index, true, frnd_ttl,
  2599. mesh_net_next_seq_num(net), src, dst,
  2600. hdr, msg + 1, msg_len - 1))
  2601. return;
  2602. }
  2603. /* Deliver to Local entities if applicable */
  2604. if (!(dst & 0x8000) && src >= net->src_addr && src <= net->last_addr)
  2605. result = ctl_received(net, key_id, iv_index, ttl,
  2606. mesh_net_next_seq_num(net), src, dst,
  2607. msg[0], 0, msg + 1, msg_len - 1);
  2608. if (!key_id) {
  2609. struct mesh_subnet *subnet = l_queue_find(net->subnets,
  2610. match_key_index, L_UINT_TO_PTR(net_idx));
  2611. if (!subnet)
  2612. return;
  2613. key_id = subnet->net_key_tx;
  2614. use_seq = mesh_net_next_seq_num(net);
  2615. if (result || (dst >= net->src_addr && dst <= net->last_addr))
  2616. return;
  2617. }
  2618. if (!mesh_crypto_packet_build(true, ttl, use_seq, src, dst, msg[0],
  2619. false, 0, false, false, 0, 0, 0, msg + 1,
  2620. msg_len - 1, pkt + 1, &pkt_len))
  2621. return;
  2622. if (!net_key_encrypt(key_id, iv_index, pkt + 1, pkt_len)) {
  2623. l_error("Failed to encode packet");
  2624. return;
  2625. }
  2626. if (!(IS_UNASSIGNED(dst)))
  2627. send_msg_pkt(net, net->tx_cnt, net->tx_interval, pkt,
  2628. pkt_len + 1);
  2629. }
  2630. int mesh_net_key_refresh_phase_set(struct mesh_net *net, uint16_t idx,
  2631. uint8_t transition)
  2632. {
  2633. switch (transition) {
  2634. case KEY_REFRESH_TRANS_TWO:
  2635. return key_refresh_phase_two(net, idx);
  2636. case KEY_REFRESH_TRANS_THREE:
  2637. return key_refresh_finish(net, idx);
  2638. default:
  2639. return MESH_STATUS_UNSPECIFIED_ERROR;
  2640. }
  2641. }
  2642. int mesh_net_key_refresh_phase_get(struct mesh_net *net, uint16_t idx,
  2643. uint8_t *phase)
  2644. {
  2645. struct mesh_subnet *subnet;
  2646. if (!net)
  2647. return MESH_STATUS_UNSPECIFIED_ERROR;
  2648. subnet = l_queue_find(net->subnets, match_key_index,
  2649. L_UINT_TO_PTR(idx));
  2650. if (!subnet)
  2651. return MESH_STATUS_INVALID_NETKEY;
  2652. *phase = subnet->kr_phase;
  2653. return MESH_STATUS_SUCCESS;
  2654. }
  2655. /*
  2656. * This function is called when Configuration Server Model receives
  2657. * a NETKEY_UPDATE command
  2658. */
  2659. int mesh_net_update_key(struct mesh_net *net, uint16_t idx,
  2660. const uint8_t *value)
  2661. {
  2662. struct mesh_subnet *subnet;
  2663. if (!net)
  2664. return MESH_STATUS_UNSPECIFIED_ERROR;
  2665. subnet = l_queue_find(net->subnets, match_key_index,
  2666. L_UINT_TO_PTR(idx));
  2667. if (!subnet)
  2668. return MESH_STATUS_INVALID_NETKEY;
  2669. /* Check if the key has been already successfully updated */
  2670. if (subnet->kr_phase == KEY_REFRESH_PHASE_ONE &&
  2671. net_key_confirm(subnet->net_key_upd, value))
  2672. return MESH_STATUS_SUCCESS;
  2673. if (subnet->kr_phase != KEY_REFRESH_PHASE_NONE)
  2674. return MESH_STATUS_CANNOT_UPDATE;
  2675. if (subnet->net_key_upd) {
  2676. net_key_unref(subnet->net_key_upd);
  2677. l_debug("Warning: overwriting new keys");
  2678. }
  2679. /* Preserve starting data */
  2680. subnet->net_key_upd = net_key_add(value);
  2681. if (!subnet->net_key_upd) {
  2682. l_error("Failed to start key refresh phase one");
  2683. return MESH_STATUS_CANNOT_UPDATE;
  2684. }
  2685. /* If we are a Friend-Node, generate all our new keys */
  2686. l_queue_foreach(net->friends, frnd_kr_phase1,
  2687. L_UINT_TO_PTR(subnet->net_key_upd));
  2688. l_debug("key refresh phase 1: Key ID %d", subnet->net_key_upd);
  2689. if (!mesh_config_net_key_update(node_config_get(net->node), idx, value))
  2690. return MESH_STATUS_STORAGE_FAIL;
  2691. subnet->kr_phase = KEY_REFRESH_PHASE_ONE;
  2692. return MESH_STATUS_SUCCESS;
  2693. }
  2694. struct mesh_net_heartbeat_sub *mesh_net_get_heartbeat_sub(struct mesh_net *net)
  2695. {
  2696. return &net->hb_sub;
  2697. }
  2698. struct mesh_net_heartbeat_pub *mesh_net_get_heartbeat_pub(struct mesh_net *net)
  2699. {
  2700. return &net->hb_pub;
  2701. }
  2702. void mesh_net_set_iv_index(struct mesh_net *net, uint32_t index, bool update)
  2703. {
  2704. net->iv_index = index;
  2705. net->iv_update = update;
  2706. }
  2707. uint16_t mesh_net_get_primary_idx(struct mesh_net *net)
  2708. {
  2709. struct mesh_subnet *subnet;
  2710. if (!net)
  2711. return NET_IDX_INVALID;
  2712. subnet = get_primary_subnet(net);
  2713. if (!subnet)
  2714. return NET_IDX_INVALID;
  2715. return subnet->idx;
  2716. }
  2717. uint32_t mesh_net_friend_timeout(struct mesh_net *net, uint16_t addr)
  2718. {
  2719. struct mesh_friend *frnd = l_queue_find(net->friends, match_by_friend,
  2720. L_UINT_TO_PTR(addr));
  2721. if (!frnd)
  2722. return 0;
  2723. else
  2724. return frnd->poll_timeout;
  2725. }
  2726. struct l_queue *mesh_net_get_friends(struct mesh_net *net)
  2727. {
  2728. if (net)
  2729. return net->friends;
  2730. return NULL;
  2731. }
  2732. struct l_queue *mesh_net_get_negotiations(struct mesh_net *net)
  2733. {
  2734. if (net)
  2735. return net->negotiations;
  2736. return NULL;
  2737. }
  2738. struct mesh_node *mesh_net_node_get(struct mesh_net *net)
  2739. {
  2740. return net->node;
  2741. }
  2742. struct l_queue *mesh_net_get_app_keys(struct mesh_net *net)
  2743. {
  2744. if (!net)
  2745. return NULL;
  2746. if (!net->app_keys)
  2747. net->app_keys = l_queue_new();
  2748. return net->app_keys;
  2749. }
  2750. bool mesh_net_have_key(struct mesh_net *net, uint16_t idx)
  2751. {
  2752. if (!net)
  2753. return false;
  2754. return (l_queue_find(net->subnets, match_key_index,
  2755. L_UINT_TO_PTR(idx)) != NULL);
  2756. }
  2757. bool mesh_net_is_local_address(struct mesh_net *net, uint16_t src,
  2758. uint16_t count)
  2759. {
  2760. const uint16_t last = src + count - 1;
  2761. if (!net)
  2762. return false;
  2763. return (src >= net->src_addr && src <= net->last_addr) &&
  2764. (last >= net->src_addr && last <= net->last_addr);
  2765. }
  2766. void mesh_net_transmit_params_set(struct mesh_net *net, uint8_t count,
  2767. uint16_t interval)
  2768. {
  2769. if (!net)
  2770. return;
  2771. net->tx_interval = interval;
  2772. net->tx_cnt = count;
  2773. }
  2774. void mesh_net_transmit_params_get(struct mesh_net *net, uint8_t *count,
  2775. uint16_t *interval)
  2776. {
  2777. if (!net)
  2778. return;
  2779. *interval = net->tx_interval;
  2780. *count = net->tx_cnt;
  2781. }
  2782. struct mesh_io *mesh_net_get_io(struct mesh_net *net)
  2783. {
  2784. if (!net)
  2785. return NULL;
  2786. return net->io;
  2787. }
  2788. struct mesh_prov *mesh_net_get_prov(struct mesh_net *net)
  2789. {
  2790. if (!net)
  2791. return NULL;
  2792. return net->prov;
  2793. }
  2794. void mesh_net_set_prov(struct mesh_net *net, struct mesh_prov *prov)
  2795. {
  2796. if (!net)
  2797. return;
  2798. net->prov = prov;
  2799. }
  2800. static void refresh_instant(void *a, void *b)
  2801. {
  2802. struct mesh_subnet *subnet = a;
  2803. struct mesh_net *net = b;
  2804. uint32_t instant = net_key_beacon_last_seen(subnet->net_key_tx);
  2805. if (net->instant < instant)
  2806. net->instant = instant;
  2807. }
  2808. uint32_t mesh_net_get_instant(struct mesh_net *net)
  2809. {
  2810. if (!net)
  2811. return 0;
  2812. l_queue_foreach(net->subnets, refresh_instant, net);
  2813. return net->instant;
  2814. }
  2815. static void hb_sub_timeout_func(struct l_timeout *timeout, void *user_data)
  2816. {
  2817. struct mesh_net *net = user_data;
  2818. struct mesh_net_heartbeat_sub *sub = &net->hb_sub;
  2819. l_debug("HB Subscription Ended");
  2820. l_timeout_remove(sub->timer);
  2821. sub->timer = NULL;
  2822. sub->enabled = false;
  2823. }
  2824. static uint32_t log_to_uint32(uint8_t log)
  2825. {
  2826. if (!log)
  2827. return 0x0000;
  2828. return (1 << (log - 1));
  2829. }
  2830. int mesh_net_set_heartbeat_sub(struct mesh_net *net, uint16_t src, uint16_t dst,
  2831. uint8_t period_log)
  2832. {
  2833. struct mesh_net_heartbeat_sub *sub = &net->hb_sub;
  2834. struct timeval time_now;
  2835. if (!net)
  2836. return MESH_STATUS_UNSPECIFIED_ERROR;
  2837. /* Check if the subscription should be disabled */
  2838. if (IS_UNASSIGNED(src) || IS_UNASSIGNED(dst)) {
  2839. if (IS_GROUP(sub->dst))
  2840. mesh_net_dst_unreg(net, sub->dst);
  2841. sub->enabled = false;
  2842. sub->dst = UNASSIGNED_ADDRESS;
  2843. sub->src = UNASSIGNED_ADDRESS;
  2844. sub->count = 0;
  2845. sub->period = 0;
  2846. sub->min_hops = 0;
  2847. sub->max_hops = 0;
  2848. } else if (!period_log && src == sub->src && dst == sub->dst) {
  2849. if (IS_GROUP(sub->dst))
  2850. mesh_net_dst_unreg(net, sub->dst);
  2851. /* Preserve collected data, but disable */
  2852. sub->enabled = false;
  2853. sub->period = 0;
  2854. } else {
  2855. if (sub->dst != dst) {
  2856. if (IS_GROUP(sub->dst))
  2857. mesh_net_dst_unreg(net, sub->dst);
  2858. if (IS_GROUP(dst))
  2859. mesh_net_dst_reg(net, dst);
  2860. }
  2861. sub->enabled = !!period_log;
  2862. sub->src = src;
  2863. sub->dst = dst;
  2864. sub->count = 0;
  2865. sub->period = log_to_uint32(period_log);
  2866. sub->min_hops = 0x00;
  2867. sub->max_hops = 0x00;
  2868. gettimeofday(&time_now, NULL);
  2869. sub->start = time_now.tv_sec;
  2870. }
  2871. /* TODO: Save to node config */
  2872. if (!sub->enabled) {
  2873. l_timeout_remove(sub->timer);
  2874. sub->timer = NULL;
  2875. return MESH_STATUS_SUCCESS;
  2876. }
  2877. sub->min_hops = 0xff;
  2878. if (!sub->timer)
  2879. sub->timer = l_timeout_create(sub->period, hb_sub_timeout_func,
  2880. net, NULL);
  2881. else
  2882. l_timeout_modify(sub->timer, sub->period);
  2883. return MESH_STATUS_SUCCESS;
  2884. }
  2885. static void hb_pub_timeout_func(struct l_timeout *timeout, void *user_data)
  2886. {
  2887. struct mesh_net *net = user_data;
  2888. struct mesh_net_heartbeat_pub *pub = &net->hb_pub;
  2889. send_hb_publication(net);
  2890. if (pub->count != 0xffff)
  2891. pub->count--;
  2892. if (pub->count > 0)
  2893. l_timeout_modify(pub->timer, pub->period);
  2894. else {
  2895. l_timeout_remove(pub->timer);
  2896. pub->timer = NULL;
  2897. }
  2898. }
  2899. static void update_hb_pub_timer(struct mesh_net *net,
  2900. struct mesh_net_heartbeat_pub *pub)
  2901. {
  2902. if (IS_UNASSIGNED(pub->dst) || pub->count == 0) {
  2903. l_timeout_remove(pub->timer);
  2904. pub->timer = NULL;
  2905. return;
  2906. }
  2907. if (!pub->timer)
  2908. pub->timer = l_timeout_create(pub->period,
  2909. hb_pub_timeout_func, net, NULL);
  2910. else
  2911. l_timeout_modify(pub->timer, pub->period);
  2912. }
  2913. int mesh_net_set_heartbeat_pub(struct mesh_net *net, uint16_t dst,
  2914. uint16_t features, uint16_t idx, uint8_t ttl,
  2915. uint8_t count_log, uint8_t period_log)
  2916. {
  2917. struct mesh_subnet *subnet;
  2918. struct mesh_net_heartbeat_pub *pub = &net->hb_pub;
  2919. if (!net)
  2920. return MESH_STATUS_UNSPECIFIED_ERROR;
  2921. subnet = l_queue_find(net->subnets, match_key_index,
  2922. L_UINT_TO_PTR(idx));
  2923. if (!subnet)
  2924. return MESH_STATUS_INVALID_NETKEY;
  2925. pub->dst = dst;
  2926. if (pub->dst == UNASSIGNED_ADDRESS) {
  2927. pub->count = 0;
  2928. pub->period = 0;
  2929. pub->ttl = 0;
  2930. } else {
  2931. pub->count = (count_log != 0xff) ?
  2932. log_to_uint32(count_log) : 0xffff;
  2933. pub->period = log_to_uint32(period_log);
  2934. }
  2935. pub->ttl = ttl;
  2936. pub->features = features;
  2937. pub->net_idx = idx;
  2938. update_hb_pub_timer(net, pub);
  2939. /* TODO: Save to node config */
  2940. return MESH_STATUS_SUCCESS;
  2941. }
  2942. bool mesh_net_load_rpl(struct mesh_net *net)
  2943. {
  2944. return rpl_get_list(net->node, net->replay_cache);
  2945. }