ndis_events.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  1. /*
  2. * ndis_events - Receive NdisMIndicateStatus() events using WMI
  3. * Copyright (c) 2004-2006, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #define _WIN32_WINNT 0x0400
  15. #include "includes.h"
  16. #ifndef COBJMACROS
  17. #define COBJMACROS
  18. #endif /* COBJMACROS */
  19. #include <wbemidl.h>
  20. #include "common.h"
  21. static int wmi_refcnt = 0;
  22. static int wmi_first = 1;
  23. struct ndis_events_data {
  24. IWbemObjectSink sink;
  25. IWbemObjectSinkVtbl sink_vtbl;
  26. IWbemServices *pSvc;
  27. IWbemLocator *pLoc;
  28. HANDLE read_pipe, write_pipe, event_avail;
  29. UINT ref;
  30. int terminating;
  31. char *ifname; /* {GUID..} */
  32. WCHAR *adapter_desc;
  33. };
  34. #define BstrAlloc(x) (x) ? SysAllocString(x) : NULL
  35. #define BstrFree(x) if (x) SysFreeString(x)
  36. /* WBEM / WMI wrapper functions, to perform in-place conversion of WCHARs to
  37. * BSTRs */
  38. HRESULT STDMETHODCALLTYPE call_IWbemServices_ExecQuery(
  39. IWbemServices *pSvc, LPCWSTR strQueryLanguage, LPCWSTR strQuery,
  40. long lFlags, IWbemContext *pCtx, IEnumWbemClassObject **ppEnum)
  41. {
  42. BSTR bsQueryLanguage, bsQuery;
  43. HRESULT hr;
  44. bsQueryLanguage = BstrAlloc(strQueryLanguage);
  45. bsQuery = BstrAlloc(strQuery);
  46. hr = IWbemServices_ExecQuery(pSvc, bsQueryLanguage, bsQuery, lFlags,
  47. pCtx, ppEnum);
  48. BstrFree(bsQueryLanguage);
  49. BstrFree(bsQuery);
  50. return hr;
  51. }
  52. HRESULT STDMETHODCALLTYPE call_IWbemServices_ExecNotificationQueryAsync(
  53. IWbemServices *pSvc, LPCWSTR strQueryLanguage, LPCWSTR strQuery,
  54. long lFlags, IWbemContext *pCtx, IWbemObjectSink *pResponseHandler)
  55. {
  56. BSTR bsQueryLanguage, bsQuery;
  57. HRESULT hr;
  58. bsQueryLanguage = BstrAlloc(strQueryLanguage);
  59. bsQuery = BstrAlloc(strQuery);
  60. hr = IWbemServices_ExecNotificationQueryAsync(pSvc, bsQueryLanguage,
  61. bsQuery, lFlags, pCtx,
  62. pResponseHandler);
  63. BstrFree(bsQueryLanguage);
  64. BstrFree(bsQuery);
  65. return hr;
  66. }
  67. HRESULT STDMETHODCALLTYPE call_IWbemLocator_ConnectServer(
  68. IWbemLocator *pLoc, LPCWSTR strNetworkResource, LPCWSTR strUser,
  69. LPCWSTR strPassword, LPCWSTR strLocale, long lSecurityFlags,
  70. LPCWSTR strAuthority, IWbemContext *pCtx, IWbemServices **ppNamespace)
  71. {
  72. BSTR bsNetworkResource, bsUser, bsPassword, bsLocale, bsAuthority;
  73. HRESULT hr;
  74. bsNetworkResource = BstrAlloc(strNetworkResource);
  75. bsUser = BstrAlloc(strUser);
  76. bsPassword = BstrAlloc(strPassword);
  77. bsLocale = BstrAlloc(strLocale);
  78. bsAuthority = BstrAlloc(strAuthority);
  79. hr = IWbemLocator_ConnectServer(pLoc, bsNetworkResource, bsUser,
  80. bsPassword, bsLocale, lSecurityFlags,
  81. bsAuthority, pCtx, ppNamespace);
  82. BstrFree(bsNetworkResource);
  83. BstrFree(bsUser);
  84. BstrFree(bsPassword);
  85. BstrFree(bsLocale);
  86. BstrFree(bsAuthority);
  87. return hr;
  88. }
  89. enum event_types { EVENT_CONNECT, EVENT_DISCONNECT, EVENT_MEDIA_SPECIFIC,
  90. EVENT_ADAPTER_ARRIVAL, EVENT_ADAPTER_REMOVAL };
  91. static int ndis_events_get_adapter(struct ndis_events_data *events,
  92. const char *ifname, const char *desc);
  93. static int ndis_events_constructor(struct ndis_events_data *events)
  94. {
  95. events->ref = 1;
  96. if (!CreatePipe(&events->read_pipe, &events->write_pipe, NULL, 512)) {
  97. wpa_printf(MSG_ERROR, "CreatePipe() failed: %d",
  98. (int) GetLastError());
  99. return -1;
  100. }
  101. events->event_avail = CreateEvent(NULL, TRUE, FALSE, NULL);
  102. if (events->event_avail == NULL) {
  103. wpa_printf(MSG_ERROR, "CreateEvent() failed: %d",
  104. (int) GetLastError());
  105. CloseHandle(events->read_pipe);
  106. CloseHandle(events->write_pipe);
  107. return -1;
  108. }
  109. return 0;
  110. }
  111. static void ndis_events_destructor(struct ndis_events_data *events)
  112. {
  113. CloseHandle(events->read_pipe);
  114. CloseHandle(events->write_pipe);
  115. CloseHandle(events->event_avail);
  116. IWbemServices_Release(events->pSvc);
  117. IWbemLocator_Release(events->pLoc);
  118. if (--wmi_refcnt == 0)
  119. CoUninitialize();
  120. }
  121. static HRESULT STDMETHODCALLTYPE
  122. ndis_events_query_interface(IWbemObjectSink *this, REFIID riid, void **obj)
  123. {
  124. *obj = NULL;
  125. if (IsEqualIID(riid, &IID_IUnknown) ||
  126. IsEqualIID(riid, &IID_IWbemObjectSink)) {
  127. *obj = this;
  128. IWbemObjectSink_AddRef(this);
  129. return NOERROR;
  130. }
  131. return E_NOINTERFACE;
  132. }
  133. static ULONG STDMETHODCALLTYPE ndis_events_add_ref(IWbemObjectSink *this)
  134. {
  135. struct ndis_events_data *events = (struct ndis_events_data *) this;
  136. return ++events->ref;
  137. }
  138. static ULONG STDMETHODCALLTYPE ndis_events_release(IWbemObjectSink *this)
  139. {
  140. struct ndis_events_data *events = (struct ndis_events_data *) this;
  141. if (--events->ref != 0)
  142. return events->ref;
  143. ndis_events_destructor(events);
  144. wpa_printf(MSG_DEBUG, "ndis_events: terminated");
  145. os_free(events->adapter_desc);
  146. os_free(events->ifname);
  147. os_free(events);
  148. return 0;
  149. }
  150. static int ndis_events_send_event(struct ndis_events_data *events,
  151. enum event_types type,
  152. char *data, size_t data_len)
  153. {
  154. char buf[512], *pos, *end;
  155. int _type;
  156. DWORD written;
  157. end = buf + sizeof(buf);
  158. _type = (int) type;
  159. os_memcpy(buf, &_type, sizeof(_type));
  160. pos = buf + sizeof(_type);
  161. if (data) {
  162. if (2 + data_len > (size_t) (end - pos)) {
  163. wpa_printf(MSG_DEBUG, "Not enough room for send_event "
  164. "data (%d)", data_len);
  165. return -1;
  166. }
  167. *pos++ = data_len >> 8;
  168. *pos++ = data_len & 0xff;
  169. os_memcpy(pos, data, data_len);
  170. pos += data_len;
  171. }
  172. if (WriteFile(events->write_pipe, buf, pos - buf, &written, NULL)) {
  173. SetEvent(events->event_avail);
  174. return 0;
  175. }
  176. wpa_printf(MSG_INFO, "WriteFile() failed: %d", (int) GetLastError());
  177. return -1;
  178. }
  179. static void ndis_events_media_connect(struct ndis_events_data *events)
  180. {
  181. wpa_printf(MSG_DEBUG, "MSNdis_StatusMediaConnect");
  182. ndis_events_send_event(events, EVENT_CONNECT, NULL, 0);
  183. }
  184. static void ndis_events_media_disconnect(struct ndis_events_data *events)
  185. {
  186. wpa_printf(MSG_DEBUG, "MSNdis_StatusMediaDisconnect");
  187. ndis_events_send_event(events, EVENT_DISCONNECT, NULL, 0);
  188. }
  189. static void ndis_events_media_specific(struct ndis_events_data *events,
  190. IWbemClassObject *pObj)
  191. {
  192. VARIANT vt;
  193. HRESULT hr;
  194. LONG lower, upper, k;
  195. UCHAR ch;
  196. char *data, *pos;
  197. size_t data_len;
  198. wpa_printf(MSG_DEBUG, "MSNdis_StatusMediaSpecificIndication");
  199. /* This is the StatusBuffer from NdisMIndicateStatus() call */
  200. hr = IWbemClassObject_Get(pObj, L"NdisStatusMediaSpecificIndication",
  201. 0, &vt, NULL, NULL);
  202. if (FAILED(hr)) {
  203. wpa_printf(MSG_DEBUG, "Could not get "
  204. "NdisStatusMediaSpecificIndication from "
  205. "the object?!");
  206. return;
  207. }
  208. SafeArrayGetLBound(V_ARRAY(&vt), 1, &lower);
  209. SafeArrayGetUBound(V_ARRAY(&vt), 1, &upper);
  210. data_len = upper - lower + 1;
  211. data = os_malloc(data_len);
  212. if (data == NULL) {
  213. wpa_printf(MSG_DEBUG, "Failed to allocate buffer for event "
  214. "data");
  215. VariantClear(&vt);
  216. return;
  217. }
  218. pos = data;
  219. for (k = lower; k <= upper; k++) {
  220. SafeArrayGetElement(V_ARRAY(&vt), &k, &ch);
  221. *pos++ = ch;
  222. }
  223. wpa_hexdump(MSG_DEBUG, "MediaSpecificEvent", (u8 *) data, data_len);
  224. VariantClear(&vt);
  225. ndis_events_send_event(events, EVENT_MEDIA_SPECIFIC, data, data_len);
  226. os_free(data);
  227. }
  228. static void ndis_events_adapter_arrival(struct ndis_events_data *events)
  229. {
  230. wpa_printf(MSG_DEBUG, "MSNdis_NotifyAdapterArrival");
  231. ndis_events_send_event(events, EVENT_ADAPTER_ARRIVAL, NULL, 0);
  232. }
  233. static void ndis_events_adapter_removal(struct ndis_events_data *events)
  234. {
  235. wpa_printf(MSG_DEBUG, "MSNdis_NotifyAdapterRemoval");
  236. ndis_events_send_event(events, EVENT_ADAPTER_REMOVAL, NULL, 0);
  237. }
  238. static HRESULT STDMETHODCALLTYPE
  239. ndis_events_indicate(IWbemObjectSink *this, long lObjectCount,
  240. IWbemClassObject __RPC_FAR *__RPC_FAR *ppObjArray)
  241. {
  242. struct ndis_events_data *events = (struct ndis_events_data *) this;
  243. long i;
  244. if (events->terminating) {
  245. wpa_printf(MSG_DEBUG, "ndis_events_indicate: Ignore "
  246. "indication - terminating");
  247. return WBEM_NO_ERROR;
  248. }
  249. /* wpa_printf(MSG_DEBUG, "Notification received - %d object(s)",
  250. lObjectCount); */
  251. for (i = 0; i < lObjectCount; i++) {
  252. IWbemClassObject *pObj = ppObjArray[i];
  253. HRESULT hr;
  254. VARIANT vtClass, vt;
  255. hr = IWbemClassObject_Get(pObj, L"__CLASS", 0, &vtClass, NULL,
  256. NULL);
  257. if (FAILED(hr)) {
  258. wpa_printf(MSG_DEBUG, "Failed to get __CLASS from "
  259. "event.");
  260. break;
  261. }
  262. /* wpa_printf(MSG_DEBUG, "CLASS: '%S'", vtClass.bstrVal); */
  263. hr = IWbemClassObject_Get(pObj, L"InstanceName", 0, &vt, NULL,
  264. NULL);
  265. if (FAILED(hr)) {
  266. wpa_printf(MSG_DEBUG, "Failed to get InstanceName "
  267. "from event.");
  268. VariantClear(&vtClass);
  269. break;
  270. }
  271. if (wcscmp(vtClass.bstrVal,
  272. L"MSNdis_NotifyAdapterArrival") == 0) {
  273. wpa_printf(MSG_DEBUG, "ndis_events_indicate: Try to "
  274. "update adapter description since it may "
  275. "have changed with new adapter instance");
  276. ndis_events_get_adapter(events, events->ifname, NULL);
  277. }
  278. if (wcscmp(events->adapter_desc, vt.bstrVal) != 0) {
  279. wpa_printf(MSG_DEBUG, "ndis_events_indicate: Ignore "
  280. "indication for foreign adapter: "
  281. "InstanceName: '%S' __CLASS: '%S'",
  282. vt.bstrVal, vtClass.bstrVal);
  283. VariantClear(&vtClass);
  284. VariantClear(&vt);
  285. continue;
  286. }
  287. VariantClear(&vt);
  288. if (wcscmp(vtClass.bstrVal,
  289. L"MSNdis_StatusMediaSpecificIndication") == 0) {
  290. ndis_events_media_specific(events, pObj);
  291. } else if (wcscmp(vtClass.bstrVal,
  292. L"MSNdis_StatusMediaConnect") == 0) {
  293. ndis_events_media_connect(events);
  294. } else if (wcscmp(vtClass.bstrVal,
  295. L"MSNdis_StatusMediaDisconnect") == 0) {
  296. ndis_events_media_disconnect(events);
  297. } else if (wcscmp(vtClass.bstrVal,
  298. L"MSNdis_NotifyAdapterArrival") == 0) {
  299. ndis_events_adapter_arrival(events);
  300. } else if (wcscmp(vtClass.bstrVal,
  301. L"MSNdis_NotifyAdapterRemoval") == 0) {
  302. ndis_events_adapter_removal(events);
  303. } else {
  304. wpa_printf(MSG_DEBUG, "Unepected event - __CLASS: "
  305. "'%S'", vtClass.bstrVal);
  306. }
  307. VariantClear(&vtClass);
  308. }
  309. return WBEM_NO_ERROR;
  310. }
  311. static HRESULT STDMETHODCALLTYPE
  312. ndis_events_set_status(IWbemObjectSink *this, long lFlags, HRESULT hResult,
  313. BSTR strParam, IWbemClassObject __RPC_FAR *pObjParam)
  314. {
  315. return WBEM_NO_ERROR;
  316. }
  317. static int notification_query(IWbemObjectSink *pDestSink,
  318. IWbemServices *pSvc, const char *class_name)
  319. {
  320. HRESULT hr;
  321. WCHAR query[256];
  322. _snwprintf(query, 256,
  323. L"SELECT * FROM %S", class_name);
  324. wpa_printf(MSG_DEBUG, "ndis_events: WMI: %S", query);
  325. hr = call_IWbemServices_ExecNotificationQueryAsync(
  326. pSvc, L"WQL", query, 0, 0, pDestSink);
  327. if (FAILED(hr)) {
  328. wpa_printf(MSG_DEBUG, "ExecNotificationQueryAsync for %s "
  329. "failed with hresult of 0x%x",
  330. class_name, (int) hr);
  331. return -1;
  332. }
  333. return 0;
  334. }
  335. static int register_async_notification(IWbemObjectSink *pDestSink,
  336. IWbemServices *pSvc)
  337. {
  338. int i;
  339. const char *class_list[] = {
  340. "MSNdis_StatusMediaConnect",
  341. "MSNdis_StatusMediaDisconnect",
  342. "MSNdis_StatusMediaSpecificIndication",
  343. "MSNdis_NotifyAdapterArrival",
  344. "MSNdis_NotifyAdapterRemoval",
  345. NULL
  346. };
  347. for (i = 0; class_list[i]; i++) {
  348. if (notification_query(pDestSink, pSvc, class_list[i]) < 0)
  349. return -1;
  350. }
  351. return 0;
  352. }
  353. void ndis_events_deinit(struct ndis_events_data *events)
  354. {
  355. events->terminating = 1;
  356. IWbemServices_CancelAsyncCall(events->pSvc, &events->sink);
  357. IWbemObjectSink_Release(&events->sink);
  358. /*
  359. * Rest of deinitialization is done in ndis_events_destructor() once
  360. * all reference count drops to zero.
  361. */
  362. }
  363. static int ndis_events_use_desc(struct ndis_events_data *events,
  364. const char *desc)
  365. {
  366. char *tmp, *pos;
  367. size_t len;
  368. if (desc == NULL) {
  369. if (events->adapter_desc == NULL)
  370. return -1;
  371. /* Continue using old description */
  372. return 0;
  373. }
  374. tmp = os_strdup(desc);
  375. if (tmp == NULL)
  376. return -1;
  377. pos = os_strstr(tmp, " (Microsoft's Packet Scheduler)");
  378. if (pos)
  379. *pos = '\0';
  380. len = os_strlen(tmp);
  381. events->adapter_desc = os_malloc((len + 1) * sizeof(WCHAR));
  382. if (events->adapter_desc == NULL) {
  383. os_free(tmp);
  384. return -1;
  385. }
  386. _snwprintf(events->adapter_desc, len + 1, L"%S", tmp);
  387. os_free(tmp);
  388. return 0;
  389. }
  390. static int ndis_events_get_adapter(struct ndis_events_data *events,
  391. const char *ifname, const char *desc)
  392. {
  393. HRESULT hr;
  394. IWbemServices *pSvc;
  395. #define MAX_QUERY_LEN 256
  396. WCHAR query[MAX_QUERY_LEN];
  397. IEnumWbemClassObject *pEnumerator;
  398. IWbemClassObject *pObj;
  399. ULONG uReturned;
  400. VARIANT vt;
  401. int len, pos;
  402. /*
  403. * Try to get adapter descriptor through WMI CIMv2 Win32_NetworkAdapter
  404. * to have better probability of matching with InstanceName from
  405. * MSNdis events. If this fails, use the provided description.
  406. */
  407. os_free(events->adapter_desc);
  408. events->adapter_desc = NULL;
  409. hr = call_IWbemLocator_ConnectServer(
  410. events->pLoc, L"ROOT\\CIMV2", NULL, NULL, 0, 0, 0, 0, &pSvc);
  411. if (FAILED(hr)) {
  412. wpa_printf(MSG_ERROR, "ndis_events: Could not connect to WMI "
  413. "server (ROOT\\CIMV2) - error 0x%x", (int) hr);
  414. return ndis_events_use_desc(events, desc);
  415. }
  416. wpa_printf(MSG_DEBUG, "ndis_events: Connected to ROOT\\CIMV2.");
  417. _snwprintf(query, MAX_QUERY_LEN,
  418. L"SELECT Index FROM Win32_NetworkAdapterConfiguration "
  419. L"WHERE SettingID='%S'", ifname);
  420. wpa_printf(MSG_DEBUG, "ndis_events: WMI: %S", query);
  421. hr = call_IWbemServices_ExecQuery(
  422. pSvc, L"WQL", query,
  423. WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
  424. NULL, &pEnumerator);
  425. if (!SUCCEEDED(hr)) {
  426. wpa_printf(MSG_DEBUG, "ndis_events: Failed to query interface "
  427. "GUID from Win32_NetworkAdapterConfiguration: "
  428. "0x%x", (int) hr);
  429. IWbemServices_Release(pSvc);
  430. return ndis_events_use_desc(events, desc);
  431. }
  432. uReturned = 0;
  433. hr = IEnumWbemClassObject_Next(pEnumerator, WBEM_INFINITE, 1,
  434. &pObj, &uReturned);
  435. if (!SUCCEEDED(hr) || uReturned == 0) {
  436. wpa_printf(MSG_DEBUG, "ndis_events: Failed to find interface "
  437. "GUID from Win32_NetworkAdapterConfiguration: "
  438. "0x%x", (int) hr);
  439. IEnumWbemClassObject_Release(pEnumerator);
  440. IWbemServices_Release(pSvc);
  441. return ndis_events_use_desc(events, desc);
  442. }
  443. IEnumWbemClassObject_Release(pEnumerator);
  444. VariantInit(&vt);
  445. hr = IWbemClassObject_Get(pObj, L"Index", 0, &vt, NULL, NULL);
  446. if (!SUCCEEDED(hr)) {
  447. wpa_printf(MSG_DEBUG, "ndis_events: Failed to get Index from "
  448. "Win32_NetworkAdapterConfiguration: 0x%x",
  449. (int) hr);
  450. IWbemServices_Release(pSvc);
  451. return ndis_events_use_desc(events, desc);
  452. }
  453. _snwprintf(query, MAX_QUERY_LEN,
  454. L"SELECT Name,PNPDeviceID FROM Win32_NetworkAdapter WHERE "
  455. L"Index=%d",
  456. vt.uintVal);
  457. wpa_printf(MSG_DEBUG, "ndis_events: WMI: %S", query);
  458. VariantClear(&vt);
  459. IWbemClassObject_Release(pObj);
  460. hr = call_IWbemServices_ExecQuery(
  461. pSvc, L"WQL", query,
  462. WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
  463. NULL, &pEnumerator);
  464. if (!SUCCEEDED(hr)) {
  465. wpa_printf(MSG_DEBUG, "ndis_events: Failed to query interface "
  466. "from Win32_NetworkAdapter: 0x%x", (int) hr);
  467. IWbemServices_Release(pSvc);
  468. return ndis_events_use_desc(events, desc);
  469. }
  470. uReturned = 0;
  471. hr = IEnumWbemClassObject_Next(pEnumerator, WBEM_INFINITE, 1,
  472. &pObj, &uReturned);
  473. if (!SUCCEEDED(hr) || uReturned == 0) {
  474. wpa_printf(MSG_DEBUG, "ndis_events: Failed to find interface "
  475. "from Win32_NetworkAdapter: 0x%x", (int) hr);
  476. IEnumWbemClassObject_Release(pEnumerator);
  477. IWbemServices_Release(pSvc);
  478. return ndis_events_use_desc(events, desc);
  479. }
  480. IEnumWbemClassObject_Release(pEnumerator);
  481. hr = IWbemClassObject_Get(pObj, L"Name", 0, &vt, NULL, NULL);
  482. if (!SUCCEEDED(hr)) {
  483. wpa_printf(MSG_DEBUG, "ndis_events: Failed to get Name from "
  484. "Win32_NetworkAdapter: 0x%x", (int) hr);
  485. IWbemClassObject_Release(pObj);
  486. IWbemServices_Release(pSvc);
  487. return ndis_events_use_desc(events, desc);
  488. }
  489. wpa_printf(MSG_DEBUG, "ndis_events: Win32_NetworkAdapter::Name='%S'",
  490. vt.bstrVal);
  491. events->adapter_desc = _wcsdup(vt.bstrVal);
  492. VariantClear(&vt);
  493. /*
  494. * Try to get even better candidate for matching with InstanceName
  495. * from Win32_PnPEntity. This is needed at least for some USB cards
  496. * that can change the InstanceName whenever being unplugged and
  497. * plugged again.
  498. */
  499. hr = IWbemClassObject_Get(pObj, L"PNPDeviceID", 0, &vt, NULL, NULL);
  500. if (!SUCCEEDED(hr)) {
  501. wpa_printf(MSG_DEBUG, "ndis_events: Failed to get PNPDeviceID "
  502. "from Win32_NetworkAdapter: 0x%x", (int) hr);
  503. IWbemClassObject_Release(pObj);
  504. IWbemServices_Release(pSvc);
  505. if (events->adapter_desc == NULL)
  506. return ndis_events_use_desc(events, desc);
  507. return 0; /* use Win32_NetworkAdapter::Name */
  508. }
  509. wpa_printf(MSG_DEBUG, "ndis_events: Win32_NetworkAdapter::PNPDeviceID="
  510. "'%S'", vt.bstrVal);
  511. len = _snwprintf(query, MAX_QUERY_LEN,
  512. L"SELECT Name FROM Win32_PnPEntity WHERE DeviceID='");
  513. if (len < 0 || len >= MAX_QUERY_LEN - 1) {
  514. VariantClear(&vt);
  515. IWbemClassObject_Release(pObj);
  516. IWbemServices_Release(pSvc);
  517. if (events->adapter_desc == NULL)
  518. return ndis_events_use_desc(events, desc);
  519. return 0; /* use Win32_NetworkAdapter::Name */
  520. }
  521. /* Escape \ as \\ */
  522. for (pos = 0; vt.bstrVal[pos] && len < MAX_QUERY_LEN - 2; pos++) {
  523. if (vt.bstrVal[pos] == '\\') {
  524. if (len >= MAX_QUERY_LEN - 3)
  525. break;
  526. query[len++] = '\\';
  527. }
  528. query[len++] = vt.bstrVal[pos];
  529. }
  530. query[len++] = L'\'';
  531. query[len] = L'\0';
  532. VariantClear(&vt);
  533. IWbemClassObject_Release(pObj);
  534. wpa_printf(MSG_DEBUG, "ndis_events: WMI: %S", query);
  535. hr = call_IWbemServices_ExecQuery(
  536. pSvc, L"WQL", query,
  537. WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
  538. NULL, &pEnumerator);
  539. if (!SUCCEEDED(hr)) {
  540. wpa_printf(MSG_DEBUG, "ndis_events: Failed to query interface "
  541. "Name from Win32_PnPEntity: 0x%x", (int) hr);
  542. IWbemServices_Release(pSvc);
  543. if (events->adapter_desc == NULL)
  544. return ndis_events_use_desc(events, desc);
  545. return 0; /* use Win32_NetworkAdapter::Name */
  546. }
  547. uReturned = 0;
  548. hr = IEnumWbemClassObject_Next(pEnumerator, WBEM_INFINITE, 1,
  549. &pObj, &uReturned);
  550. if (!SUCCEEDED(hr) || uReturned == 0) {
  551. wpa_printf(MSG_DEBUG, "ndis_events: Failed to find interface "
  552. "from Win32_PnPEntity: 0x%x", (int) hr);
  553. IEnumWbemClassObject_Release(pEnumerator);
  554. IWbemServices_Release(pSvc);
  555. if (events->adapter_desc == NULL)
  556. return ndis_events_use_desc(events, desc);
  557. return 0; /* use Win32_NetworkAdapter::Name */
  558. }
  559. IEnumWbemClassObject_Release(pEnumerator);
  560. hr = IWbemClassObject_Get(pObj, L"Name", 0, &vt, NULL, NULL);
  561. if (!SUCCEEDED(hr)) {
  562. wpa_printf(MSG_DEBUG, "ndis_events: Failed to get Name from "
  563. "Win32_PnPEntity: 0x%x", (int) hr);
  564. IWbemClassObject_Release(pObj);
  565. IWbemServices_Release(pSvc);
  566. if (events->adapter_desc == NULL)
  567. return ndis_events_use_desc(events, desc);
  568. return 0; /* use Win32_NetworkAdapter::Name */
  569. }
  570. wpa_printf(MSG_DEBUG, "ndis_events: Win32_PnPEntity::Name='%S'",
  571. vt.bstrVal);
  572. os_free(events->adapter_desc);
  573. events->adapter_desc = _wcsdup(vt.bstrVal);
  574. VariantClear(&vt);
  575. IWbemClassObject_Release(pObj);
  576. IWbemServices_Release(pSvc);
  577. if (events->adapter_desc == NULL)
  578. return ndis_events_use_desc(events, desc);
  579. return 0;
  580. }
  581. struct ndis_events_data *
  582. ndis_events_init(HANDLE *read_pipe, HANDLE *event_avail,
  583. const char *ifname, const char *desc)
  584. {
  585. HRESULT hr;
  586. IWbemObjectSink *pSink;
  587. struct ndis_events_data *events;
  588. events = os_zalloc(sizeof(*events));
  589. if (events == NULL) {
  590. wpa_printf(MSG_ERROR, "Could not allocate sink for events.");
  591. return NULL;
  592. }
  593. events->ifname = os_strdup(ifname);
  594. if (events->ifname == NULL) {
  595. os_free(events);
  596. return NULL;
  597. }
  598. if (wmi_refcnt++ == 0) {
  599. hr = CoInitializeEx(0, COINIT_MULTITHREADED);
  600. if (FAILED(hr)) {
  601. wpa_printf(MSG_ERROR, "CoInitializeEx() failed - "
  602. "returned 0x%x", (int) hr);
  603. os_free(events);
  604. return NULL;
  605. }
  606. }
  607. if (wmi_first) {
  608. /* CoInitializeSecurity() must be called once and only once
  609. * per process, so let's use wmi_first flag to protect against
  610. * multiple calls. */
  611. wmi_first = 0;
  612. hr = CoInitializeSecurity(NULL, -1, NULL, NULL,
  613. RPC_C_AUTHN_LEVEL_PKT_PRIVACY,
  614. RPC_C_IMP_LEVEL_IMPERSONATE,
  615. NULL, EOAC_SECURE_REFS, NULL);
  616. if (FAILED(hr)) {
  617. wpa_printf(MSG_ERROR, "CoInitializeSecurity() failed "
  618. "- returned 0x%x", (int) hr);
  619. os_free(events);
  620. return NULL;
  621. }
  622. }
  623. hr = CoCreateInstance(&CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER,
  624. &IID_IWbemLocator,
  625. (LPVOID *) (void *) &events->pLoc);
  626. if (FAILED(hr)) {
  627. wpa_printf(MSG_ERROR, "CoCreateInstance() failed - returned "
  628. "0x%x", (int) hr);
  629. CoUninitialize();
  630. os_free(events);
  631. return NULL;
  632. }
  633. if (ndis_events_get_adapter(events, ifname, desc) < 0) {
  634. CoUninitialize();
  635. os_free(events);
  636. return NULL;
  637. }
  638. wpa_printf(MSG_DEBUG, "ndis_events: use adapter descriptor '%S'",
  639. events->adapter_desc);
  640. hr = call_IWbemLocator_ConnectServer(
  641. events->pLoc, L"ROOT\\WMI", NULL, NULL,
  642. 0, 0, 0, 0, &events->pSvc);
  643. if (FAILED(hr)) {
  644. wpa_printf(MSG_ERROR, "Could not connect to server - error "
  645. "0x%x", (int) hr);
  646. CoUninitialize();
  647. os_free(events->adapter_desc);
  648. os_free(events);
  649. return NULL;
  650. }
  651. wpa_printf(MSG_DEBUG, "Connected to ROOT\\WMI.");
  652. ndis_events_constructor(events);
  653. pSink = &events->sink;
  654. pSink->lpVtbl = &events->sink_vtbl;
  655. events->sink_vtbl.QueryInterface = ndis_events_query_interface;
  656. events->sink_vtbl.AddRef = ndis_events_add_ref;
  657. events->sink_vtbl.Release = ndis_events_release;
  658. events->sink_vtbl.Indicate = ndis_events_indicate;
  659. events->sink_vtbl.SetStatus = ndis_events_set_status;
  660. if (register_async_notification(pSink, events->pSvc) < 0) {
  661. wpa_printf(MSG_DEBUG, "Failed to register async "
  662. "notifications");
  663. ndis_events_destructor(events);
  664. os_free(events->adapter_desc);
  665. os_free(events);
  666. return NULL;
  667. }
  668. *read_pipe = events->read_pipe;
  669. *event_avail = events->event_avail;
  670. return events;
  671. }