ieee80211_regdomain.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* $OpenBSD: ieee80211_regdomain.c,v 1.9 2014/12/23 03:24:08 tedu Exp $ */
  2. /*
  3. * Copyright (c) 2004, 2005 Reyk Floeter <reyk@openbsd.org>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. /*
  18. * Basic regulation domain extensions for the IEEE 802.11 stack
  19. */
  20. #include <sys/param.h>
  21. #include <sys/systm.h>
  22. #include <sys/mbuf.h>
  23. #include <sys/kernel.h>
  24. #include <sys/socket.h>
  25. #include <sys/sockio.h>
  26. #include <sys/endian.h>
  27. #include <sys/errno.h>
  28. #include <net/if.h>
  29. #include <net/if_dl.h>
  30. #include <net/if_media.h>
  31. #include <net/if_arp.h>
  32. #include <net/if_llc.h>
  33. #include <netinet/in.h>
  34. #include <netinet/if_ether.h>
  35. #include <net80211/ieee80211_var.h>
  36. #include <net80211/ieee80211_regdomain.h>
  37. int ieee80211_regdomain_compare_cn(const void *, const void *);
  38. int ieee80211_regdomain_compare_rn(const void *, const void *);
  39. static const struct ieee80211_regdomainname
  40. ieee80211_r_names[] = IEEE80211_REGDOMAIN_NAMES;
  41. static const struct ieee80211_regdomainmap
  42. ieee80211_r_map[] = IEEE80211_REGDOMAIN_MAP;
  43. static const struct ieee80211_countryname
  44. ieee80211_r_ctry[] = IEEE80211_REGDOMAIN_COUNTRY_NAMES;
  45. #ifndef bsearch
  46. const void *bsearch(const void *, const void *, size_t, size_t,
  47. int (*)(const void *, const void *));
  48. const void *
  49. bsearch(const void *key, const void *base0, size_t nmemb, size_t size,
  50. int (*compar)(const void *, const void *))
  51. {
  52. const char *base = base0;
  53. int lim, cmp;
  54. const void *p;
  55. for (lim = nmemb; lim != 0; lim >>= 1) {
  56. p = base + (lim >> 1) * size;
  57. cmp = (*compar)(key, p);
  58. if (cmp == 0)
  59. return ((const void *)p);
  60. if (cmp > 0) { /* key > p: move right */
  61. base = (const char *)p + size;
  62. lim--;
  63. } /* else move left */
  64. }
  65. return (NULL);
  66. }
  67. #endif
  68. int
  69. ieee80211_regdomain_compare_cn(const void *a, const void *b)
  70. {
  71. return (strcmp(((const struct ieee80211_countryname*)a)->cn_name,
  72. ((const struct ieee80211_countryname*)b)->cn_name));
  73. }
  74. int
  75. ieee80211_regdomain_compare_rn(const void *a, const void *b)
  76. {
  77. return (strcmp(((const struct ieee80211_regdomainname*)a)->rn_name,
  78. ((const struct ieee80211_regdomainname*)b)->rn_name));
  79. }
  80. u_int16_t
  81. ieee80211_name2countrycode(const char *name)
  82. {
  83. const struct ieee80211_countryname key = { CTRY_DEFAULT, name }, *value;
  84. if ((value = bsearch(&key, &ieee80211_r_ctry,
  85. nitems(ieee80211_r_ctry), sizeof(struct ieee80211_countryname),
  86. ieee80211_regdomain_compare_cn)) != NULL)
  87. return (value->cn_code);
  88. return (CTRY_DEFAULT);
  89. }
  90. u_int32_t
  91. ieee80211_name2regdomain(const char *name)
  92. {
  93. const struct ieee80211_regdomainname *value;
  94. struct ieee80211_regdomainname key;
  95. key.rn_domain = DMN_DEFAULT;
  96. key.rn_name = name;
  97. if ((value = bsearch(&key, &ieee80211_r_names,
  98. nitems(ieee80211_r_names), sizeof(struct ieee80211_regdomainname),
  99. ieee80211_regdomain_compare_rn)) != NULL)
  100. return ((u_int32_t)value->rn_domain);
  101. return ((u_int32_t)DMN_DEFAULT);
  102. }
  103. const char *
  104. ieee80211_countrycode2name(u_int16_t code)
  105. {
  106. int i;
  107. /* Linear search over the table */
  108. for (i = 0; i < (sizeof(ieee80211_r_ctry) /
  109. sizeof(ieee80211_r_ctry[0])); i++)
  110. if (ieee80211_r_ctry[i].cn_code == code)
  111. return (ieee80211_r_ctry[i].cn_name);
  112. return (NULL);
  113. }
  114. const char *
  115. ieee80211_regdomain2name(u_int32_t regdomain)
  116. {
  117. int i;
  118. /* Linear search over the table */
  119. for (i = 0; i < (sizeof(ieee80211_r_names) /
  120. sizeof(ieee80211_r_names[0])); i++)
  121. if (ieee80211_r_names[i].rn_domain == regdomain)
  122. return (ieee80211_r_names[i].rn_name);
  123. return (ieee80211_r_names[0].rn_name);
  124. }
  125. u_int32_t
  126. ieee80211_regdomain2flag(u_int16_t regdomain, u_int16_t mhz)
  127. {
  128. int i;
  129. for (i = 0; i < (sizeof(ieee80211_r_map) /
  130. sizeof(ieee80211_r_map[0])); i++) {
  131. if (ieee80211_r_map[i].rm_domain == regdomain) {
  132. if (mhz >= 2000 && mhz <= 3000)
  133. return ((u_int32_t)
  134. ieee80211_r_map[i].rm_domain_2ghz);
  135. if (mhz >= IEEE80211_CHANNELS_5GHZ_MIN &&
  136. mhz <= IEEE80211_CHANNELS_5GHZ_MAX)
  137. return ((u_int32_t)
  138. ieee80211_r_map[i].rm_domain_5ghz);
  139. }
  140. }
  141. return ((u_int32_t)DMN_DEBUG);
  142. }
  143. u_int32_t
  144. ieee80211_countrycode2regdomain(u_int16_t code)
  145. {
  146. int i;
  147. for (i = 0; i < nitems(ieee80211_r_ctry); i++)
  148. if (ieee80211_r_ctry[i].cn_code == code)
  149. return (ieee80211_r_ctry[i].cn_domain);
  150. return ((u_int32_t)DMN_DEFAULT);
  151. }