wext-core.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  1. /*
  2. * This file implement the Wireless Extensions core API.
  3. *
  4. * Authors : Jean Tourrilhes - HPL - <jt@hpl.hp.com>
  5. * Copyright (c) 1997-2007 Jean Tourrilhes, All Rights Reserved.
  6. * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
  7. *
  8. * (As all part of the Linux kernel, this file is GPL)
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/netdevice.h>
  12. #include <linux/rtnetlink.h>
  13. #include <linux/slab.h>
  14. #include <linux/wireless.h>
  15. #include <linux/uaccess.h>
  16. #include <linux/export.h>
  17. #include <net/cfg80211.h>
  18. #include <net/iw_handler.h>
  19. #include <net/netlink.h>
  20. #include <net/wext.h>
  21. #include <net/net_namespace.h>
  22. #ifdef KW_TAINT_ANALYSIS
  23. extern void * get_tainted_stuff();
  24. #endif
  25. typedef int (*wext_ioctl_func)(struct net_device *, struct iwreq *,
  26. unsigned int, struct iw_request_info *,
  27. iw_handler);
  28. /*
  29. * Meta-data about all the standard Wireless Extension request we
  30. * know about.
  31. */
  32. static const struct iw_ioctl_description standard_ioctl[] = {
  33. [IW_IOCTL_IDX(SIOCSIWCOMMIT)] = {
  34. .header_type = IW_HEADER_TYPE_NULL,
  35. },
  36. [IW_IOCTL_IDX(SIOCGIWNAME)] = {
  37. .header_type = IW_HEADER_TYPE_CHAR,
  38. .flags = IW_DESCR_FLAG_DUMP,
  39. },
  40. [IW_IOCTL_IDX(SIOCSIWNWID)] = {
  41. .header_type = IW_HEADER_TYPE_PARAM,
  42. .flags = IW_DESCR_FLAG_EVENT,
  43. },
  44. [IW_IOCTL_IDX(SIOCGIWNWID)] = {
  45. .header_type = IW_HEADER_TYPE_PARAM,
  46. .flags = IW_DESCR_FLAG_DUMP,
  47. },
  48. [IW_IOCTL_IDX(SIOCSIWFREQ)] = {
  49. .header_type = IW_HEADER_TYPE_FREQ,
  50. .flags = IW_DESCR_FLAG_EVENT,
  51. },
  52. [IW_IOCTL_IDX(SIOCGIWFREQ)] = {
  53. .header_type = IW_HEADER_TYPE_FREQ,
  54. .flags = IW_DESCR_FLAG_DUMP,
  55. },
  56. [IW_IOCTL_IDX(SIOCSIWMODE)] = {
  57. .header_type = IW_HEADER_TYPE_UINT,
  58. .flags = IW_DESCR_FLAG_EVENT,
  59. },
  60. [IW_IOCTL_IDX(SIOCGIWMODE)] = {
  61. .header_type = IW_HEADER_TYPE_UINT,
  62. .flags = IW_DESCR_FLAG_DUMP,
  63. },
  64. [IW_IOCTL_IDX(SIOCSIWSENS)] = {
  65. .header_type = IW_HEADER_TYPE_PARAM,
  66. },
  67. [IW_IOCTL_IDX(SIOCGIWSENS)] = {
  68. .header_type = IW_HEADER_TYPE_PARAM,
  69. },
  70. [IW_IOCTL_IDX(SIOCSIWRANGE)] = {
  71. .header_type = IW_HEADER_TYPE_NULL,
  72. },
  73. [IW_IOCTL_IDX(SIOCGIWRANGE)] = {
  74. .header_type = IW_HEADER_TYPE_POINT,
  75. .token_size = 1,
  76. .max_tokens = sizeof(struct iw_range),
  77. .flags = IW_DESCR_FLAG_DUMP,
  78. },
  79. [IW_IOCTL_IDX(SIOCSIWPRIV)] = {
  80. .header_type = IW_HEADER_TYPE_NULL,
  81. },
  82. [IW_IOCTL_IDX(SIOCGIWPRIV)] = { /* (handled directly by us) */
  83. .header_type = IW_HEADER_TYPE_POINT,
  84. .token_size = sizeof(struct iw_priv_args),
  85. .max_tokens = 16,
  86. .flags = IW_DESCR_FLAG_NOMAX,
  87. },
  88. [IW_IOCTL_IDX(SIOCSIWSTATS)] = {
  89. .header_type = IW_HEADER_TYPE_NULL,
  90. },
  91. [IW_IOCTL_IDX(SIOCGIWSTATS)] = { /* (handled directly by us) */
  92. .header_type = IW_HEADER_TYPE_POINT,
  93. .token_size = 1,
  94. .max_tokens = sizeof(struct iw_statistics),
  95. .flags = IW_DESCR_FLAG_DUMP,
  96. },
  97. [IW_IOCTL_IDX(SIOCSIWSPY)] = {
  98. .header_type = IW_HEADER_TYPE_POINT,
  99. .token_size = sizeof(struct sockaddr),
  100. .max_tokens = IW_MAX_SPY,
  101. },
  102. [IW_IOCTL_IDX(SIOCGIWSPY)] = {
  103. .header_type = IW_HEADER_TYPE_POINT,
  104. .token_size = sizeof(struct sockaddr) +
  105. sizeof(struct iw_quality),
  106. .max_tokens = IW_MAX_SPY,
  107. },
  108. [IW_IOCTL_IDX(SIOCSIWTHRSPY)] = {
  109. .header_type = IW_HEADER_TYPE_POINT,
  110. .token_size = sizeof(struct iw_thrspy),
  111. .min_tokens = 1,
  112. .max_tokens = 1,
  113. },
  114. [IW_IOCTL_IDX(SIOCGIWTHRSPY)] = {
  115. .header_type = IW_HEADER_TYPE_POINT,
  116. .token_size = sizeof(struct iw_thrspy),
  117. .min_tokens = 1,
  118. .max_tokens = 1,
  119. },
  120. [IW_IOCTL_IDX(SIOCSIWAP)] = {
  121. .header_type = IW_HEADER_TYPE_ADDR,
  122. },
  123. [IW_IOCTL_IDX(SIOCGIWAP)] = {
  124. .header_type = IW_HEADER_TYPE_ADDR,
  125. .flags = IW_DESCR_FLAG_DUMP,
  126. },
  127. [IW_IOCTL_IDX(SIOCSIWMLME)] = {
  128. .header_type = IW_HEADER_TYPE_POINT,
  129. .token_size = 1,
  130. .min_tokens = sizeof(struct iw_mlme),
  131. .max_tokens = sizeof(struct iw_mlme),
  132. },
  133. [IW_IOCTL_IDX(SIOCGIWAPLIST)] = {
  134. .header_type = IW_HEADER_TYPE_POINT,
  135. .token_size = sizeof(struct sockaddr) +
  136. sizeof(struct iw_quality),
  137. .max_tokens = IW_MAX_AP,
  138. .flags = IW_DESCR_FLAG_NOMAX,
  139. },
  140. [IW_IOCTL_IDX(SIOCSIWSCAN)] = {
  141. .header_type = IW_HEADER_TYPE_POINT,
  142. .token_size = 1,
  143. .min_tokens = 0,
  144. .max_tokens = sizeof(struct iw_scan_req),
  145. },
  146. [IW_IOCTL_IDX(SIOCGIWSCAN)] = {
  147. .header_type = IW_HEADER_TYPE_POINT,
  148. .token_size = 1,
  149. .max_tokens = IW_SCAN_MAX_DATA,
  150. .flags = IW_DESCR_FLAG_NOMAX,
  151. },
  152. [IW_IOCTL_IDX(SIOCSIWESSID)] = {
  153. .header_type = IW_HEADER_TYPE_POINT,
  154. .token_size = 1,
  155. .max_tokens = IW_ESSID_MAX_SIZE,
  156. .flags = IW_DESCR_FLAG_EVENT,
  157. },
  158. [IW_IOCTL_IDX(SIOCGIWESSID)] = {
  159. .header_type = IW_HEADER_TYPE_POINT,
  160. .token_size = 1,
  161. .max_tokens = IW_ESSID_MAX_SIZE,
  162. .flags = IW_DESCR_FLAG_DUMP,
  163. },
  164. [IW_IOCTL_IDX(SIOCSIWNICKN)] = {
  165. .header_type = IW_HEADER_TYPE_POINT,
  166. .token_size = 1,
  167. .max_tokens = IW_ESSID_MAX_SIZE,
  168. },
  169. [IW_IOCTL_IDX(SIOCGIWNICKN)] = {
  170. .header_type = IW_HEADER_TYPE_POINT,
  171. .token_size = 1,
  172. .max_tokens = IW_ESSID_MAX_SIZE,
  173. },
  174. [IW_IOCTL_IDX(SIOCSIWRATE)] = {
  175. .header_type = IW_HEADER_TYPE_PARAM,
  176. },
  177. [IW_IOCTL_IDX(SIOCGIWRATE)] = {
  178. .header_type = IW_HEADER_TYPE_PARAM,
  179. },
  180. [IW_IOCTL_IDX(SIOCSIWRTS)] = {
  181. .header_type = IW_HEADER_TYPE_PARAM,
  182. },
  183. [IW_IOCTL_IDX(SIOCGIWRTS)] = {
  184. .header_type = IW_HEADER_TYPE_PARAM,
  185. },
  186. [IW_IOCTL_IDX(SIOCSIWFRAG)] = {
  187. .header_type = IW_HEADER_TYPE_PARAM,
  188. },
  189. [IW_IOCTL_IDX(SIOCGIWFRAG)] = {
  190. .header_type = IW_HEADER_TYPE_PARAM,
  191. },
  192. [IW_IOCTL_IDX(SIOCSIWTXPOW)] = {
  193. .header_type = IW_HEADER_TYPE_PARAM,
  194. },
  195. [IW_IOCTL_IDX(SIOCGIWTXPOW)] = {
  196. .header_type = IW_HEADER_TYPE_PARAM,
  197. },
  198. [IW_IOCTL_IDX(SIOCSIWRETRY)] = {
  199. .header_type = IW_HEADER_TYPE_PARAM,
  200. },
  201. [IW_IOCTL_IDX(SIOCGIWRETRY)] = {
  202. .header_type = IW_HEADER_TYPE_PARAM,
  203. },
  204. [IW_IOCTL_IDX(SIOCSIWENCODE)] = {
  205. .header_type = IW_HEADER_TYPE_POINT,
  206. .token_size = 1,
  207. .max_tokens = IW_ENCODING_TOKEN_MAX,
  208. .flags = IW_DESCR_FLAG_EVENT | IW_DESCR_FLAG_RESTRICT,
  209. },
  210. [IW_IOCTL_IDX(SIOCGIWENCODE)] = {
  211. .header_type = IW_HEADER_TYPE_POINT,
  212. .token_size = 1,
  213. .max_tokens = IW_ENCODING_TOKEN_MAX,
  214. .flags = IW_DESCR_FLAG_DUMP | IW_DESCR_FLAG_RESTRICT,
  215. },
  216. [IW_IOCTL_IDX(SIOCSIWPOWER)] = {
  217. .header_type = IW_HEADER_TYPE_PARAM,
  218. },
  219. [IW_IOCTL_IDX(SIOCGIWPOWER)] = {
  220. .header_type = IW_HEADER_TYPE_PARAM,
  221. },
  222. [IW_IOCTL_IDX(SIOCSIWGENIE)] = {
  223. .header_type = IW_HEADER_TYPE_POINT,
  224. .token_size = 1,
  225. .max_tokens = IW_GENERIC_IE_MAX,
  226. },
  227. [IW_IOCTL_IDX(SIOCGIWGENIE)] = {
  228. .header_type = IW_HEADER_TYPE_POINT,
  229. .token_size = 1,
  230. .max_tokens = IW_GENERIC_IE_MAX,
  231. },
  232. [IW_IOCTL_IDX(SIOCSIWAUTH)] = {
  233. .header_type = IW_HEADER_TYPE_PARAM,
  234. },
  235. [IW_IOCTL_IDX(SIOCGIWAUTH)] = {
  236. .header_type = IW_HEADER_TYPE_PARAM,
  237. },
  238. [IW_IOCTL_IDX(SIOCSIWENCODEEXT)] = {
  239. .header_type = IW_HEADER_TYPE_POINT,
  240. .token_size = 1,
  241. .min_tokens = sizeof(struct iw_encode_ext),
  242. .max_tokens = sizeof(struct iw_encode_ext) +
  243. IW_ENCODING_TOKEN_MAX,
  244. },
  245. [IW_IOCTL_IDX(SIOCGIWENCODEEXT)] = {
  246. .header_type = IW_HEADER_TYPE_POINT,
  247. .token_size = 1,
  248. .min_tokens = sizeof(struct iw_encode_ext),
  249. .max_tokens = sizeof(struct iw_encode_ext) +
  250. IW_ENCODING_TOKEN_MAX,
  251. },
  252. [IW_IOCTL_IDX(SIOCSIWPMKSA)] = {
  253. .header_type = IW_HEADER_TYPE_POINT,
  254. .token_size = 1,
  255. .min_tokens = sizeof(struct iw_pmksa),
  256. .max_tokens = sizeof(struct iw_pmksa),
  257. },
  258. };
  259. static const unsigned standard_ioctl_num = ARRAY_SIZE(standard_ioctl);
  260. /*
  261. * Meta-data about all the additional standard Wireless Extension events
  262. * we know about.
  263. */
  264. static const struct iw_ioctl_description standard_event[] = {
  265. [IW_EVENT_IDX(IWEVTXDROP)] = {
  266. .header_type = IW_HEADER_TYPE_ADDR,
  267. },
  268. [IW_EVENT_IDX(IWEVQUAL)] = {
  269. .header_type = IW_HEADER_TYPE_QUAL,
  270. },
  271. [IW_EVENT_IDX(IWEVCUSTOM)] = {
  272. .header_type = IW_HEADER_TYPE_POINT,
  273. .token_size = 1,
  274. .max_tokens = IW_CUSTOM_MAX,
  275. },
  276. [IW_EVENT_IDX(IWEVREGISTERED)] = {
  277. .header_type = IW_HEADER_TYPE_ADDR,
  278. },
  279. [IW_EVENT_IDX(IWEVEXPIRED)] = {
  280. .header_type = IW_HEADER_TYPE_ADDR,
  281. },
  282. [IW_EVENT_IDX(IWEVGENIE)] = {
  283. .header_type = IW_HEADER_TYPE_POINT,
  284. .token_size = 1,
  285. .max_tokens = IW_GENERIC_IE_MAX,
  286. },
  287. [IW_EVENT_IDX(IWEVMICHAELMICFAILURE)] = {
  288. .header_type = IW_HEADER_TYPE_POINT,
  289. .token_size = 1,
  290. .max_tokens = sizeof(struct iw_michaelmicfailure),
  291. },
  292. [IW_EVENT_IDX(IWEVASSOCREQIE)] = {
  293. .header_type = IW_HEADER_TYPE_POINT,
  294. .token_size = 1,
  295. .max_tokens = IW_GENERIC_IE_MAX,
  296. },
  297. [IW_EVENT_IDX(IWEVASSOCRESPIE)] = {
  298. .header_type = IW_HEADER_TYPE_POINT,
  299. .token_size = 1,
  300. .max_tokens = IW_GENERIC_IE_MAX,
  301. },
  302. [IW_EVENT_IDX(IWEVPMKIDCAND)] = {
  303. .header_type = IW_HEADER_TYPE_POINT,
  304. .token_size = 1,
  305. .max_tokens = sizeof(struct iw_pmkid_cand),
  306. },
  307. };
  308. static const unsigned standard_event_num = ARRAY_SIZE(standard_event);
  309. /* Size (in bytes) of various events */
  310. static const int event_type_size[] = {
  311. IW_EV_LCP_LEN, /* IW_HEADER_TYPE_NULL */
  312. 0,
  313. IW_EV_CHAR_LEN, /* IW_HEADER_TYPE_CHAR */
  314. 0,
  315. IW_EV_UINT_LEN, /* IW_HEADER_TYPE_UINT */
  316. IW_EV_FREQ_LEN, /* IW_HEADER_TYPE_FREQ */
  317. IW_EV_ADDR_LEN, /* IW_HEADER_TYPE_ADDR */
  318. 0,
  319. IW_EV_POINT_LEN, /* Without variable payload */
  320. IW_EV_PARAM_LEN, /* IW_HEADER_TYPE_PARAM */
  321. IW_EV_QUAL_LEN, /* IW_HEADER_TYPE_QUAL */
  322. };
  323. #ifdef CONFIG_COMPAT
  324. static const int compat_event_type_size[] = {
  325. IW_EV_COMPAT_LCP_LEN, /* IW_HEADER_TYPE_NULL */
  326. 0,
  327. IW_EV_COMPAT_CHAR_LEN, /* IW_HEADER_TYPE_CHAR */
  328. 0,
  329. IW_EV_COMPAT_UINT_LEN, /* IW_HEADER_TYPE_UINT */
  330. IW_EV_COMPAT_FREQ_LEN, /* IW_HEADER_TYPE_FREQ */
  331. IW_EV_COMPAT_ADDR_LEN, /* IW_HEADER_TYPE_ADDR */
  332. 0,
  333. IW_EV_COMPAT_POINT_LEN, /* Without variable payload */
  334. IW_EV_COMPAT_PARAM_LEN, /* IW_HEADER_TYPE_PARAM */
  335. IW_EV_COMPAT_QUAL_LEN, /* IW_HEADER_TYPE_QUAL */
  336. };
  337. #endif
  338. /* IW event code */
  339. static int __net_init wext_pernet_init(struct net *net)
  340. {
  341. skb_queue_head_init(&net->wext_nlevents);
  342. return 0;
  343. }
  344. static void __net_exit wext_pernet_exit(struct net *net)
  345. {
  346. skb_queue_purge(&net->wext_nlevents);
  347. }
  348. static struct pernet_operations wext_pernet_ops = {
  349. .init = wext_pernet_init,
  350. .exit = wext_pernet_exit,
  351. };
  352. static int __init wireless_nlevent_init(void)
  353. {
  354. return register_pernet_subsys(&wext_pernet_ops);
  355. }
  356. subsys_initcall(wireless_nlevent_init);
  357. /* Process events generated by the wireless layer or the driver. */
  358. static void wireless_nlevent_process(struct work_struct *work)
  359. {
  360. struct sk_buff *skb;
  361. struct net *net;
  362. rtnl_lock();
  363. for_each_net(net) {
  364. while ((skb = skb_dequeue(&net->wext_nlevents)))
  365. rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL,
  366. GFP_KERNEL);
  367. }
  368. rtnl_unlock();
  369. }
  370. static DECLARE_WORK(wireless_nlevent_work, wireless_nlevent_process);
  371. static struct nlmsghdr *rtnetlink_ifinfo_prep(struct net_device *dev,
  372. struct sk_buff *skb)
  373. {
  374. struct ifinfomsg *r;
  375. struct nlmsghdr *nlh;
  376. nlh = nlmsg_put(skb, 0, 0, RTM_NEWLINK, sizeof(*r), 0);
  377. if (!nlh)
  378. return NULL;
  379. r = nlmsg_data(nlh);
  380. r->ifi_family = AF_UNSPEC;
  381. r->__ifi_pad = 0;
  382. r->ifi_type = dev->type;
  383. r->ifi_index = dev->ifindex;
  384. r->ifi_flags = dev_get_flags(dev);
  385. r->ifi_change = 0; /* Wireless changes don't affect those flags */
  386. NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
  387. return nlh;
  388. nla_put_failure:
  389. nlmsg_cancel(skb, nlh);
  390. return NULL;
  391. }
  392. /*
  393. * Main event dispatcher. Called from other parts and drivers.
  394. * Send the event on the appropriate channels.
  395. * May be called from interrupt context.
  396. */
  397. void wireless_send_event(struct net_device * dev,
  398. unsigned int cmd,
  399. union iwreq_data * wrqu,
  400. const char * extra)
  401. {
  402. const struct iw_ioctl_description * descr = NULL;
  403. int extra_len = 0;
  404. struct iw_event *event; /* Mallocated whole event */
  405. int event_len; /* Its size */
  406. int hdr_len; /* Size of the event header */
  407. int wrqu_off = 0; /* Offset in wrqu */
  408. /* Don't "optimise" the following variable, it will crash */
  409. unsigned cmd_index; /* *MUST* be unsigned */
  410. struct sk_buff *skb;
  411. struct nlmsghdr *nlh;
  412. struct nlattr *nla;
  413. #ifdef CONFIG_COMPAT
  414. struct __compat_iw_event *compat_event;
  415. struct compat_iw_point compat_wrqu;
  416. struct sk_buff *compskb;
  417. #endif
  418. /*
  419. * Nothing in the kernel sends scan events with data, be safe.
  420. * This is necessary because we cannot fix up scan event data
  421. * for compat, due to being contained in 'extra', but normally
  422. * applications are required to retrieve the scan data anyway
  423. * and no data is included in the event, this codifies that
  424. * practice.
  425. */
  426. if (WARN_ON(cmd == SIOCGIWSCAN && extra))
  427. extra = NULL;
  428. /* Get the description of the Event */
  429. if (cmd <= SIOCIWLAST) {
  430. cmd_index = IW_IOCTL_IDX(cmd);
  431. if (cmd_index < standard_ioctl_num)
  432. descr = &(standard_ioctl[cmd_index]);
  433. } else {
  434. cmd_index = IW_EVENT_IDX(cmd);
  435. if (cmd_index < standard_event_num)
  436. descr = &(standard_event[cmd_index]);
  437. }
  438. /* Don't accept unknown events */
  439. if (descr == NULL) {
  440. /* Note : we don't return an error to the driver, because
  441. * the driver would not know what to do about it. It can't
  442. * return an error to the user, because the event is not
  443. * initiated by a user request.
  444. * The best the driver could do is to log an error message.
  445. * We will do it ourselves instead...
  446. */
  447. netdev_err(dev, "(WE) : Invalid/Unknown Wireless Event (0x%04X)\n",
  448. cmd);
  449. return;
  450. }
  451. /* Check extra parameters and set extra_len */
  452. if (descr->header_type == IW_HEADER_TYPE_POINT) {
  453. /* Check if number of token fits within bounds */
  454. if (wrqu->data.length > descr->max_tokens) {
  455. netdev_err(dev, "(WE) : Wireless Event too big (%d)\n",
  456. wrqu->data.length);
  457. return;
  458. }
  459. if (wrqu->data.length < descr->min_tokens) {
  460. netdev_err(dev, "(WE) : Wireless Event too small (%d)\n",
  461. wrqu->data.length);
  462. return;
  463. }
  464. /* Calculate extra_len - extra is NULL for restricted events */
  465. if (extra != NULL)
  466. extra_len = wrqu->data.length * descr->token_size;
  467. /* Always at an offset in wrqu */
  468. wrqu_off = IW_EV_POINT_OFF;
  469. }
  470. /* Total length of the event */
  471. hdr_len = event_type_size[descr->header_type];
  472. event_len = hdr_len + extra_len;
  473. /*
  474. * The problem for 64/32 bit.
  475. *
  476. * On 64-bit, a regular event is laid out as follows:
  477. * | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
  478. * | event.len | event.cmd | p a d d i n g |
  479. * | wrqu data ... (with the correct size) |
  480. *
  481. * This padding exists because we manipulate event->u,
  482. * and 'event' is not packed.
  483. *
  484. * An iw_point event is laid out like this instead:
  485. * | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
  486. * | event.len | event.cmd | p a d d i n g |
  487. * | iwpnt.len | iwpnt.flg | p a d d i n g |
  488. * | extra data ...
  489. *
  490. * The second padding exists because struct iw_point is extended,
  491. * but this depends on the platform...
  492. *
  493. * On 32-bit, all the padding shouldn't be there.
  494. */
  495. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  496. if (!skb)
  497. return;
  498. /* Send via the RtNetlink event channel */
  499. nlh = rtnetlink_ifinfo_prep(dev, skb);
  500. if (WARN_ON(!nlh)) {
  501. kfree_skb(skb);
  502. return;
  503. }
  504. /* Add the wireless events in the netlink packet */
  505. nla = nla_reserve(skb, IFLA_WIRELESS, event_len);
  506. if (!nla) {
  507. kfree_skb(skb);
  508. return;
  509. }
  510. event = nla_data(nla);
  511. /* Fill event - first clear to avoid data leaking */
  512. memset(event, 0, hdr_len);
  513. event->len = event_len;
  514. event->cmd = cmd;
  515. memcpy(&event->u, ((char *) wrqu) + wrqu_off, hdr_len - IW_EV_LCP_LEN);
  516. if (extra_len)
  517. memcpy(((char *) event) + hdr_len, extra, extra_len);
  518. nlmsg_end(skb, nlh);
  519. #ifdef CONFIG_COMPAT
  520. hdr_len = compat_event_type_size[descr->header_type];
  521. event_len = hdr_len + extra_len;
  522. compskb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  523. if (!compskb) {
  524. kfree_skb(skb);
  525. return;
  526. }
  527. /* Send via the RtNetlink event channel */
  528. nlh = rtnetlink_ifinfo_prep(dev, compskb);
  529. if (WARN_ON(!nlh)) {
  530. kfree_skb(skb);
  531. kfree_skb(compskb);
  532. return;
  533. }
  534. /* Add the wireless events in the netlink packet */
  535. nla = nla_reserve(compskb, IFLA_WIRELESS, event_len);
  536. if (!nla) {
  537. kfree_skb(skb);
  538. kfree_skb(compskb);
  539. return;
  540. }
  541. compat_event = nla_data(nla);
  542. compat_event->len = event_len;
  543. compat_event->cmd = cmd;
  544. if (descr->header_type == IW_HEADER_TYPE_POINT) {
  545. compat_wrqu.length = wrqu->data.length;
  546. compat_wrqu.flags = wrqu->data.flags;
  547. memcpy(&compat_event->pointer,
  548. ((char *) &compat_wrqu) + IW_EV_COMPAT_POINT_OFF,
  549. hdr_len - IW_EV_COMPAT_LCP_LEN);
  550. if (extra_len)
  551. memcpy(((char *) compat_event) + hdr_len,
  552. extra, extra_len);
  553. } else {
  554. /* extra_len must be zero, so no if (extra) needed */
  555. memcpy(&compat_event->pointer, wrqu,
  556. hdr_len - IW_EV_COMPAT_LCP_LEN);
  557. }
  558. nlmsg_end(compskb, nlh);
  559. skb_shinfo(skb)->frag_list = compskb;
  560. #endif
  561. skb_queue_tail(&dev_net(dev)->wext_nlevents, skb);
  562. schedule_work(&wireless_nlevent_work);
  563. }
  564. EXPORT_SYMBOL(wireless_send_event);
  565. /* IW handlers */
  566. struct iw_statistics *get_wireless_stats(struct net_device *dev)
  567. {
  568. #ifdef CONFIG_WIRELESS_EXT
  569. if ((dev->wireless_handlers != NULL) &&
  570. (dev->wireless_handlers->get_wireless_stats != NULL))
  571. return dev->wireless_handlers->get_wireless_stats(dev);
  572. #endif
  573. #ifdef CONFIG_CFG80211_WEXT
  574. if (dev->ieee80211_ptr &&
  575. dev->ieee80211_ptr->wiphy &&
  576. dev->ieee80211_ptr->wiphy->wext &&
  577. dev->ieee80211_ptr->wiphy->wext->get_wireless_stats)
  578. return dev->ieee80211_ptr->wiphy->wext->get_wireless_stats(dev);
  579. #endif
  580. /* not found */
  581. return NULL;
  582. }
  583. static int iw_handler_get_iwstats(struct net_device * dev,
  584. struct iw_request_info * info,
  585. union iwreq_data * wrqu,
  586. char * extra)
  587. {
  588. /* Get stats from the driver */
  589. struct iw_statistics *stats;
  590. stats = get_wireless_stats(dev);
  591. if (stats) {
  592. /* Copy statistics to extra */
  593. memcpy(extra, stats, sizeof(struct iw_statistics));
  594. wrqu->data.length = sizeof(struct iw_statistics);
  595. /* Check if we need to clear the updated flag */
  596. if (wrqu->data.flags != 0)
  597. stats->qual.updated &= ~IW_QUAL_ALL_UPDATED;
  598. return 0;
  599. } else
  600. return -EOPNOTSUPP;
  601. }
  602. static iw_handler get_handler(struct net_device *dev, unsigned int cmd)
  603. {
  604. /* Don't "optimise" the following variable, it will crash */
  605. unsigned int index; /* *MUST* be unsigned */
  606. const struct iw_handler_def *handlers = NULL;
  607. #ifdef CONFIG_CFG80211_WEXT
  608. if (dev->ieee80211_ptr && dev->ieee80211_ptr->wiphy)
  609. handlers = dev->ieee80211_ptr->wiphy->wext;
  610. #endif
  611. #ifdef CONFIG_WIRELESS_EXT
  612. if (dev->wireless_handlers)
  613. handlers = dev->wireless_handlers;
  614. #endif
  615. if (!handlers)
  616. return NULL;
  617. /* Try as a standard command */
  618. index = IW_IOCTL_IDX(cmd);
  619. if (index < handlers->num_standard)
  620. return handlers->standard[index];
  621. #ifdef CONFIG_WEXT_PRIV
  622. /* Try as a private command */
  623. index = cmd - SIOCIWFIRSTPRIV;
  624. if (index < handlers->num_private)
  625. return handlers->private[index];
  626. #endif
  627. /* Not found */
  628. return NULL;
  629. }
  630. static int ioctl_standard_iw_point(struct iw_point *iwp, unsigned int cmd,
  631. const struct iw_ioctl_description *descr,
  632. iw_handler handler, struct net_device *dev,
  633. struct iw_request_info *info)
  634. {
  635. int err, extra_size, user_length = 0, essid_compat = 0;
  636. char *extra;
  637. /* Calculate space needed by arguments. Always allocate
  638. * for max space.
  639. */
  640. extra_size = descr->max_tokens * descr->token_size;
  641. /* Check need for ESSID compatibility for WE < 21 */
  642. switch (cmd) {
  643. case SIOCSIWESSID:
  644. case SIOCGIWESSID:
  645. case SIOCSIWNICKN:
  646. case SIOCGIWNICKN:
  647. if (iwp->length == descr->max_tokens + 1)
  648. essid_compat = 1;
  649. else if (IW_IS_SET(cmd) && (iwp->length != 0)) {
  650. char essid[IW_ESSID_MAX_SIZE + 1];
  651. unsigned int len;
  652. len = iwp->length * descr->token_size;
  653. if (len > IW_ESSID_MAX_SIZE)
  654. return -EFAULT;
  655. err = copy_from_user(essid, iwp->pointer, len);
  656. if (err)
  657. return -EFAULT;
  658. if (essid[iwp->length - 1] == '\0')
  659. essid_compat = 1;
  660. }
  661. break;
  662. default:
  663. break;
  664. }
  665. iwp->length -= essid_compat;
  666. /* Check what user space is giving us */
  667. if (IW_IS_SET(cmd)) {
  668. /* Check NULL pointer */
  669. if (!iwp->pointer && iwp->length != 0)
  670. return -EFAULT;
  671. /* Check if number of token fits within bounds */
  672. if (iwp->length > descr->max_tokens)
  673. return -E2BIG;
  674. if (iwp->length < descr->min_tokens)
  675. return -EINVAL;
  676. } else {
  677. /* Check NULL pointer */
  678. if (!iwp->pointer)
  679. return -EFAULT;
  680. /* Save user space buffer size for checking */
  681. user_length = iwp->length;
  682. /* Don't check if user_length > max to allow forward
  683. * compatibility. The test user_length < min is
  684. * implied by the test at the end.
  685. */
  686. /* Support for very large requests */
  687. if ((descr->flags & IW_DESCR_FLAG_NOMAX) &&
  688. (user_length > descr->max_tokens)) {
  689. /* Allow userspace to GET more than max so
  690. * we can support any size GET requests.
  691. * There is still a limit : -ENOMEM.
  692. */
  693. extra_size = user_length * descr->token_size;
  694. /* Note : user_length is originally a __u16,
  695. * and token_size is controlled by us,
  696. * so extra_size won't get negative and
  697. * won't overflow...
  698. */
  699. }
  700. }
  701. /* kzalloc() ensures NULL-termination for essid_compat. */
  702. extra = kzalloc(extra_size, GFP_KERNEL);
  703. if (!extra)
  704. return -ENOMEM;
  705. /* If it is a SET, get all the extra data in here */
  706. if (IW_IS_SET(cmd) && (iwp->length != 0)) {
  707. if (copy_from_user(extra, iwp->pointer,
  708. iwp->length *
  709. descr->token_size)) {
  710. err = -EFAULT;
  711. goto out;
  712. }
  713. if (cmd == SIOCSIWENCODEEXT) {
  714. struct iw_encode_ext *ee = (void *) extra;
  715. if (iwp->length < sizeof(*ee) + ee->key_len) {
  716. err = -EFAULT;
  717. goto out;
  718. }
  719. }
  720. }
  721. if (IW_IS_GET(cmd) && !(descr->flags & IW_DESCR_FLAG_NOMAX)) {
  722. /*
  723. * If this is a GET, but not NOMAX, it means that the extra
  724. * data is not bounded by userspace, but by max_tokens. Thus
  725. * set the length to max_tokens. This matches the extra data
  726. * allocation.
  727. * The driver should fill it with the number of tokens it
  728. * provided, and it may check iwp->length rather than having
  729. * knowledge of max_tokens. If the driver doesn't change the
  730. * iwp->length, this ioctl just copies back max_token tokens
  731. * filled with zeroes. Hopefully the driver isn't claiming
  732. * them to be valid data.
  733. */
  734. iwp->length = descr->max_tokens;
  735. }
  736. err = handler(dev, info, (union iwreq_data *) iwp, extra);
  737. iwp->length += essid_compat;
  738. /* If we have something to return to the user */
  739. if (!err && IW_IS_GET(cmd)) {
  740. /* Check if there is enough buffer up there */
  741. if (user_length < iwp->length) {
  742. err = -E2BIG;
  743. goto out;
  744. }
  745. if (copy_to_user(iwp->pointer, extra,
  746. iwp->length *
  747. descr->token_size)) {
  748. err = -EFAULT;
  749. goto out;
  750. }
  751. }
  752. /* Generate an event to notify listeners of the change */
  753. if ((descr->flags & IW_DESCR_FLAG_EVENT) &&
  754. ((err == 0) || (err == -EIWCOMMIT))) {
  755. union iwreq_data *data = (union iwreq_data *) iwp;
  756. if (descr->flags & IW_DESCR_FLAG_RESTRICT)
  757. /* If the event is restricted, don't
  758. * export the payload.
  759. */
  760. wireless_send_event(dev, cmd, data, NULL);
  761. else
  762. wireless_send_event(dev, cmd, data, extra);
  763. }
  764. out:
  765. kfree(extra);
  766. return err;
  767. }
  768. /*
  769. * Call the commit handler in the driver
  770. * (if exist and if conditions are right)
  771. *
  772. * Note : our current commit strategy is currently pretty dumb,
  773. * but we will be able to improve on that...
  774. * The goal is to try to agreagate as many changes as possible
  775. * before doing the commit. Drivers that will define a commit handler
  776. * are usually those that need a reset after changing parameters, so
  777. * we want to minimise the number of reset.
  778. * A cool idea is to use a timer : at each "set" command, we re-set the
  779. * timer, when the timer eventually fires, we call the driver.
  780. * Hopefully, more on that later.
  781. *
  782. * Also, I'm waiting to see how many people will complain about the
  783. * netif_running(dev) test. I'm open on that one...
  784. * Hopefully, the driver will remember to do a commit in "open()" ;-)
  785. */
  786. int call_commit_handler(struct net_device *dev)
  787. {
  788. #ifdef CONFIG_WIRELESS_EXT
  789. if ((netif_running(dev)) &&
  790. (dev->wireless_handlers->standard[0] != NULL))
  791. /* Call the commit handler on the driver */
  792. return dev->wireless_handlers->standard[0](dev, NULL,
  793. NULL, NULL);
  794. else
  795. return 0; /* Command completed successfully */
  796. #else
  797. /* cfg80211 has no commit */
  798. return 0;
  799. #endif
  800. }
  801. /*
  802. * Main IOCTl dispatcher.
  803. * Check the type of IOCTL and call the appropriate wrapper...
  804. */
  805. static int wireless_process_ioctl(struct net *net, struct ifreq *ifr,
  806. unsigned int cmd,
  807. struct iw_request_info *info,
  808. wext_ioctl_func standard,
  809. wext_ioctl_func private)
  810. {
  811. struct iwreq *iwr = (struct iwreq *) ifr;
  812. struct net_device *dev;
  813. iw_handler handler;
  814. /* Permissions are already checked in dev_ioctl() before calling us.
  815. * The copy_to/from_user() of ifr is also dealt with in there */
  816. /* Make sure the device exist */
  817. if ((dev = __dev_get_by_name(net, ifr->ifr_name)) == NULL)
  818. return -ENODEV;
  819. /* A bunch of special cases, then the generic case...
  820. * Note that 'cmd' is already filtered in dev_ioctl() with
  821. * (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) */
  822. if (cmd == SIOCGIWSTATS)
  823. return standard(dev, iwr, cmd, info,
  824. &iw_handler_get_iwstats);
  825. #ifdef CONFIG_WEXT_PRIV
  826. if (cmd == SIOCGIWPRIV && dev->wireless_handlers)
  827. return standard(dev, iwr, cmd, info,
  828. iw_handler_get_private);
  829. #endif
  830. /* Basic check */
  831. if (!netif_device_present(dev))
  832. return -ENODEV;
  833. /* New driver API : try to find the handler */
  834. handler = get_handler(dev, cmd);
  835. if (handler) {
  836. /* Standard and private are not the same */
  837. if (cmd < SIOCIWFIRSTPRIV)
  838. return standard(dev, iwr, cmd, info, handler);
  839. else if (private)
  840. return private(dev, iwr, cmd, info, handler);
  841. }
  842. /* Old driver API : call driver ioctl handler */
  843. if (dev->netdev_ops->ndo_do_ioctl)
  844. return dev->netdev_ops->ndo_do_ioctl(dev, ifr, cmd);
  845. return -EOPNOTSUPP;
  846. }
  847. /* If command is `set a parameter', or `get the encoding parameters',
  848. * check if the user has the right to do it.
  849. */
  850. static int wext_permission_check(unsigned int cmd)
  851. {
  852. if ((IW_IS_SET(cmd) || cmd == SIOCGIWENCODE ||
  853. cmd == SIOCGIWENCODEEXT) &&
  854. !capable(CAP_NET_ADMIN))
  855. return -EPERM;
  856. return 0;
  857. }
  858. /* entry point from dev ioctl */
  859. static int wext_ioctl_dispatch(struct net *net, struct ifreq *ifr,
  860. unsigned int cmd, struct iw_request_info *info,
  861. wext_ioctl_func standard,
  862. wext_ioctl_func private)
  863. {
  864. int ret = wext_permission_check(cmd);
  865. if (ret)
  866. return ret;
  867. dev_load(net, ifr->ifr_name);
  868. rtnl_lock();
  869. ret = wireless_process_ioctl(net, ifr, cmd, info, standard, private);
  870. rtnl_unlock();
  871. return ret;
  872. }
  873. /*
  874. * Wrapper to call a standard Wireless Extension handler.
  875. * We do various checks and also take care of moving data between
  876. * user space and kernel space.
  877. */
  878. static int ioctl_standard_call(struct net_device * dev,
  879. struct iwreq *iwr,
  880. unsigned int cmd,
  881. struct iw_request_info *info,
  882. iw_handler handler)
  883. {
  884. const struct iw_ioctl_description * descr;
  885. int ret = -EINVAL;
  886. /* Get the description of the IOCTL */
  887. if (IW_IOCTL_IDX(cmd) >= standard_ioctl_num)
  888. return -EOPNOTSUPP;
  889. descr = &(standard_ioctl[IW_IOCTL_IDX(cmd)]);
  890. /* Check if we have a pointer to user space data or not */
  891. if (descr->header_type != IW_HEADER_TYPE_POINT) {
  892. /* No extra arguments. Trivial to handle */
  893. ret = handler(dev, info, &(iwr->u), NULL);
  894. /* Generate an event to notify listeners of the change */
  895. if ((descr->flags & IW_DESCR_FLAG_EVENT) &&
  896. ((ret == 0) || (ret == -EIWCOMMIT)))
  897. wireless_send_event(dev, cmd, &(iwr->u), NULL);
  898. } else {
  899. ret = ioctl_standard_iw_point(&iwr->u.data, cmd, descr,
  900. handler, dev, info);
  901. }
  902. /* Call commit handler if needed and defined */
  903. if (ret == -EIWCOMMIT)
  904. ret = call_commit_handler(dev);
  905. /* Here, we will generate the appropriate event if needed */
  906. return ret;
  907. }
  908. int wext_handle_ioctl(struct net *net, struct ifreq *ifr, unsigned int cmd,
  909. void __user *arg_actual)
  910. {
  911. #ifdef KW_TAINT_ANALYSIS
  912. void __user *arg = (void __user *)get_tainted_stuff();
  913. #else
  914. void __user *arg = (void __user *)arg_actual;
  915. #endif
  916. struct iw_request_info info = { .cmd = cmd, .flags = 0 };
  917. int ret;
  918. ret = wext_ioctl_dispatch(net, ifr, cmd, &info,
  919. ioctl_standard_call,
  920. ioctl_private_call);
  921. if (ret >= 0 &&
  922. IW_IS_GET(cmd) &&
  923. copy_to_user(arg, ifr, sizeof(struct iwreq)))
  924. return -EFAULT;
  925. return ret;
  926. }
  927. #ifdef CONFIG_COMPAT
  928. static int compat_standard_call(struct net_device *dev,
  929. struct iwreq *iwr,
  930. unsigned int cmd,
  931. struct iw_request_info *info,
  932. iw_handler handler)
  933. {
  934. const struct iw_ioctl_description *descr;
  935. struct compat_iw_point *iwp_compat;
  936. struct iw_point iwp;
  937. int err;
  938. descr = standard_ioctl + IW_IOCTL_IDX(cmd);
  939. if (descr->header_type != IW_HEADER_TYPE_POINT)
  940. return ioctl_standard_call(dev, iwr, cmd, info, handler);
  941. iwp_compat = (struct compat_iw_point *) &iwr->u.data;
  942. iwp.pointer = compat_ptr(iwp_compat->pointer);
  943. iwp.length = iwp_compat->length;
  944. iwp.flags = iwp_compat->flags;
  945. err = ioctl_standard_iw_point(&iwp, cmd, descr, handler, dev, info);
  946. iwp_compat->pointer = ptr_to_compat(iwp.pointer);
  947. iwp_compat->length = iwp.length;
  948. iwp_compat->flags = iwp.flags;
  949. return err;
  950. }
  951. int compat_wext_handle_ioctl(struct net *net, unsigned int cmd,
  952. unsigned long arg)
  953. {
  954. #ifdef KW_TAINT_ANALYSIS
  955. void __user *argp = (void __user *)get_tainted_stuff();
  956. #else
  957. void __user *argp = (void __user *)arg;
  958. #endif
  959. struct iw_request_info info;
  960. struct iwreq iwr;
  961. char *colon;
  962. int ret;
  963. if (copy_from_user(&iwr, argp, sizeof(struct iwreq)))
  964. return -EFAULT;
  965. iwr.ifr_name[IFNAMSIZ-1] = 0;
  966. colon = strchr(iwr.ifr_name, ':');
  967. if (colon)
  968. *colon = 0;
  969. info.cmd = cmd;
  970. info.flags = IW_REQUEST_FLAG_COMPAT;
  971. ret = wext_ioctl_dispatch(net, (struct ifreq *) &iwr, cmd, &info,
  972. compat_standard_call,
  973. compat_private_call);
  974. if (ret >= 0 &&
  975. IW_IS_GET(cmd) &&
  976. copy_to_user(argp, &iwr, sizeof(struct iwreq)))
  977. return -EFAULT;
  978. return ret;
  979. }
  980. #endif