Luis R. Rodriguez | 14a0380 | 2008-08-29 16:01:56 -0700 | [diff] [blame] | 1 | #include <net/if.h> |
| 2 | #include <errno.h> |
| 3 | #include <string.h> |
Luis R. Rodriguez | ffc08bc | 2009-03-09 22:28:49 -0400 | [diff] [blame] | 4 | #include <stdbool.h> |
Luis R. Rodriguez | 14a0380 | 2008-08-29 16:01:56 -0700 | [diff] [blame] | 5 | |
| 6 | #include <netlink/genl/genl.h> |
| 7 | #include <netlink/genl/family.h> |
| 8 | #include <netlink/genl/ctrl.h> |
| 9 | #include <netlink/msg.h> |
| 10 | #include <netlink/attr.h> |
| 11 | |
Johannes Berg | f408e01 | 2008-09-18 19:32:11 +0200 | [diff] [blame] | 12 | #include "nl80211.h" |
Luis R. Rodriguez | 14a0380 | 2008-08-29 16:01:56 -0700 | [diff] [blame] | 13 | #include "iw.h" |
| 14 | |
Johannes Berg | 4698bfc | 2009-08-24 12:53:34 +0200 | [diff] [blame] | 15 | SECTION(reg); |
| 16 | |
Luis R. Rodriguez | 7ab6522 | 2009-01-28 08:57:59 -0800 | [diff] [blame] | 17 | #define MHZ_TO_KHZ(freq) ((freq) * 1000) |
| 18 | #define KHZ_TO_MHZ(freq) ((freq) / 1000) |
| 19 | #define DBI_TO_MBI(gain) ((gain) * 100) |
| 20 | #define MBI_TO_DBI(gain) ((gain) / 100) |
| 21 | #define DBM_TO_MBM(gain) ((gain) * 100) |
| 22 | #define MBM_TO_DBM(gain) ((gain) / 100) |
| 23 | |
Luis R. Rodriguez | ffc08bc | 2009-03-09 22:28:49 -0400 | [diff] [blame] | 24 | static bool isalpha_upper(char letter) |
Luis R. Rodriguez | 14a0380 | 2008-08-29 16:01:56 -0700 | [diff] [blame] | 25 | { |
| 26 | if (letter >= 65 && letter <= 90) |
Luis R. Rodriguez | ffc08bc | 2009-03-09 22:28:49 -0400 | [diff] [blame] | 27 | return true; |
| 28 | return false; |
Luis R. Rodriguez | 14a0380 | 2008-08-29 16:01:56 -0700 | [diff] [blame] | 29 | } |
| 30 | |
Luis R. Rodriguez | ffc08bc | 2009-03-09 22:28:49 -0400 | [diff] [blame] | 31 | static bool is_alpha2(char *alpha2) |
Luis R. Rodriguez | 14a0380 | 2008-08-29 16:01:56 -0700 | [diff] [blame] | 32 | { |
| 33 | if (isalpha_upper(alpha2[0]) && isalpha_upper(alpha2[1])) |
Luis R. Rodriguez | ffc08bc | 2009-03-09 22:28:49 -0400 | [diff] [blame] | 34 | return true; |
| 35 | return false; |
Luis R. Rodriguez | 14a0380 | 2008-08-29 16:01:56 -0700 | [diff] [blame] | 36 | } |
| 37 | |
Luis R. Rodriguez | ffc08bc | 2009-03-09 22:28:49 -0400 | [diff] [blame] | 38 | static bool is_world_regdom(char *alpha2) |
Luis R. Rodriguez | 14a0380 | 2008-08-29 16:01:56 -0700 | [diff] [blame] | 39 | { |
| 40 | /* ASCII 0 */ |
| 41 | if (alpha2[0] == 48 && alpha2[1] == 48) |
Luis R. Rodriguez | ffc08bc | 2009-03-09 22:28:49 -0400 | [diff] [blame] | 42 | return true; |
| 43 | return false; |
Luis R. Rodriguez | 14a0380 | 2008-08-29 16:01:56 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Luis R. Rodriguez | 601c6ab | 2009-03-09 22:28:50 -0400 | [diff] [blame] | 46 | char *reg_initiator_to_string(__u8 initiator) |
| 47 | { |
| 48 | switch (initiator) { |
| 49 | case NL80211_REGDOM_SET_BY_CORE: |
| 50 | return "the wireless core upon initialization"; |
| 51 | case NL80211_REGDOM_SET_BY_USER: |
| 52 | return "a user"; |
| 53 | case NL80211_REGDOM_SET_BY_DRIVER: |
| 54 | return "a driver"; |
| 55 | case NL80211_REGDOM_SET_BY_COUNTRY_IE: |
| 56 | return "a country IE"; |
| 57 | default: |
| 58 | return "BUG"; |
| 59 | } |
| 60 | } |
| 61 | |
Zefir Kurtisi | 9e8a208 | 2013-04-03 14:51:56 +0200 | [diff] [blame] | 62 | static const char *dfs_domain_name(enum nl80211_dfs_regions region) |
| 63 | { |
| 64 | switch (region) { |
| 65 | case NL80211_DFS_UNSET: |
| 66 | return "DFS-UNSET"; |
| 67 | case NL80211_DFS_FCC: |
| 68 | return "DFS-FCC"; |
| 69 | case NL80211_DFS_ETSI: |
| 70 | return "DFS-ETSI"; |
| 71 | case NL80211_DFS_JP: |
| 72 | return "DFS-JP"; |
| 73 | default: |
| 74 | return "DFS-invalid"; |
| 75 | } |
| 76 | } |
| 77 | |
Johannes Berg | 7c37a24 | 2009-04-08 13:13:28 +0200 | [diff] [blame] | 78 | static int handle_reg_set(struct nl80211_state *state, |
| 79 | struct nl_cb *cb, |
Johannes Berg | 1c05c10 | 2008-09-16 17:09:04 +0200 | [diff] [blame] | 80 | struct nl_msg *msg, |
Johannes Berg | 05514f9 | 2012-07-19 11:50:50 +0200 | [diff] [blame] | 81 | int argc, char **argv, |
| 82 | enum id_input id) |
Luis R. Rodriguez | 14a0380 | 2008-08-29 16:01:56 -0700 | [diff] [blame] | 83 | { |
Luis R. Rodriguez | 14a0380 | 2008-08-29 16:01:56 -0700 | [diff] [blame] | 84 | char alpha2[3]; |
| 85 | |
Johannes Berg | 1c05c10 | 2008-09-16 17:09:04 +0200 | [diff] [blame] | 86 | if (argc < 1) |
Johannes Berg | 5e75fd0 | 2008-09-16 18:13:12 +0200 | [diff] [blame] | 87 | return 1; |
Luis R. Rodriguez | 14a0380 | 2008-08-29 16:01:56 -0700 | [diff] [blame] | 88 | |
| 89 | if (!is_alpha2(argv[0]) && !is_world_regdom(argv[0])) { |
| 90 | fprintf(stderr, "not a valid ISO/IEC 3166-1 alpha2\n"); |
Johannes Berg | 40ffa98 | 2008-09-16 19:12:57 +0200 | [diff] [blame] | 91 | fprintf(stderr, "Special non-alpha2 usable entries:\n"); |
Luis R. Rodriguez | 14a0380 | 2008-08-29 16:01:56 -0700 | [diff] [blame] | 92 | fprintf(stderr, "\t00\tWorld Regulatory domain\n"); |
Johannes Berg | 5e75fd0 | 2008-09-16 18:13:12 +0200 | [diff] [blame] | 93 | return 2; |
Luis R. Rodriguez | 14a0380 | 2008-08-29 16:01:56 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | alpha2[0] = argv[0][0]; |
| 97 | alpha2[1] = argv[0][1]; |
| 98 | alpha2[2] = '\0'; |
| 99 | |
| 100 | argc--; |
| 101 | argv++; |
| 102 | |
Johannes Berg | 1c05c10 | 2008-09-16 17:09:04 +0200 | [diff] [blame] | 103 | if (argc) |
Johannes Berg | 5e75fd0 | 2008-09-16 18:13:12 +0200 | [diff] [blame] | 104 | return 1; |
Luis R. Rodriguez | 14a0380 | 2008-08-29 16:01:56 -0700 | [diff] [blame] | 105 | |
| 106 | NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, alpha2); |
| 107 | |
Johannes Berg | 70391cc | 2008-09-16 18:35:06 +0200 | [diff] [blame] | 108 | return 0; |
Luis R. Rodriguez | 14a0380 | 2008-08-29 16:01:56 -0700 | [diff] [blame] | 109 | nla_put_failure: |
Johannes Berg | 70391cc | 2008-09-16 18:35:06 +0200 | [diff] [blame] | 110 | return -ENOBUFS; |
Luis R. Rodriguez | 14a0380 | 2008-08-29 16:01:56 -0700 | [diff] [blame] | 111 | } |
Johannes Berg | 1c05c10 | 2008-09-16 17:09:04 +0200 | [diff] [blame] | 112 | COMMAND(reg, set, "<ISO/IEC 3166-1 alpha2>", |
Johannes Berg | 70cf454 | 2009-05-05 15:24:39 +0200 | [diff] [blame] | 113 | NL80211_CMD_REQ_SET_REG, 0, CIB_NONE, handle_reg_set, |
| 114 | "Notify the kernel about the current regulatory domain."); |
Luis R. Rodriguez | 7ab6522 | 2009-01-28 08:57:59 -0800 | [diff] [blame] | 115 | |
| 116 | static int print_reg_handler(struct nl_msg *msg, void *arg) |
| 117 | |
| 118 | { |
| 119 | #define PARSE_FLAG(nl_flag, string_value) do { \ |
| 120 | if ((flags & nl_flag)) { \ |
| 121 | printf(", %s", string_value); \ |
| 122 | } \ |
| 123 | } while (0) |
| 124 | struct nlattr *tb_msg[NL80211_ATTR_MAX + 1]; |
| 125 | struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); |
| 126 | char *alpha2; |
| 127 | struct nlattr *nl_rule; |
| 128 | int rem_rule; |
Zefir Kurtisi | 9e8a208 | 2013-04-03 14:51:56 +0200 | [diff] [blame] | 129 | enum nl80211_dfs_regions dfs_domain; |
Luis R. Rodriguez | 7ab6522 | 2009-01-28 08:57:59 -0800 | [diff] [blame] | 130 | static struct nla_policy reg_rule_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = { |
Johannes Berg | c551449 | 2011-12-07 09:08:40 +0100 | [diff] [blame] | 131 | [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 }, |
| 132 | [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 }, |
| 133 | [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 }, |
| 134 | [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 }, |
| 135 | [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 }, |
| 136 | [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 }, |
Luis R. Rodriguez | 7ab6522 | 2009-01-28 08:57:59 -0800 | [diff] [blame] | 137 | }; |
| 138 | |
| 139 | nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), |
| 140 | genlmsg_attrlen(gnlh, 0), NULL); |
| 141 | |
| 142 | if (!tb_msg[NL80211_ATTR_REG_ALPHA2]) { |
| 143 | printf("No alpha2\n"); |
| 144 | return NL_SKIP; |
| 145 | } |
| 146 | |
| 147 | if (!tb_msg[NL80211_ATTR_REG_RULES]) { |
| 148 | printf("No reg rules\n"); |
| 149 | return NL_SKIP; |
| 150 | } |
| 151 | |
Zefir Kurtisi | 9e8a208 | 2013-04-03 14:51:56 +0200 | [diff] [blame] | 152 | if (tb_msg[NL80211_ATTR_DFS_REGION]) |
| 153 | dfs_domain = nla_get_u8(tb_msg[NL80211_ATTR_DFS_REGION]); |
| 154 | else |
| 155 | dfs_domain = NL80211_DFS_UNSET; |
| 156 | |
Luis R. Rodriguez | 7ab6522 | 2009-01-28 08:57:59 -0800 | [diff] [blame] | 157 | alpha2 = nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]); |
Zefir Kurtisi | 9e8a208 | 2013-04-03 14:51:56 +0200 | [diff] [blame] | 158 | printf("country %c%c: %s\n", alpha2[0], alpha2[1], dfs_domain_name(dfs_domain)); |
Luis R. Rodriguez | 7ab6522 | 2009-01-28 08:57:59 -0800 | [diff] [blame] | 159 | |
| 160 | nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule) { |
| 161 | struct nlattr *tb_rule[NL80211_FREQUENCY_ATTR_MAX + 1]; |
| 162 | __u32 flags, start_freq_khz, end_freq_khz, max_bw_khz, max_ant_gain_mbi, max_eirp_mbm; |
| 163 | |
| 164 | nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_rule), nla_len(nl_rule), reg_rule_policy); |
| 165 | |
| 166 | flags = nla_get_u32(tb_rule[NL80211_ATTR_REG_RULE_FLAGS]); |
| 167 | start_freq_khz = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_START]); |
| 168 | end_freq_khz = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_END]); |
| 169 | max_bw_khz = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_MAX_BW]); |
| 170 | max_ant_gain_mbi = nla_get_u32(tb_rule[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]); |
| 171 | max_eirp_mbm = nla_get_u32(tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP]); |
| 172 | |
| 173 | |
| 174 | printf("\t(%d - %d @ %d), (", |
| 175 | KHZ_TO_MHZ(start_freq_khz), KHZ_TO_MHZ(end_freq_khz), KHZ_TO_MHZ(max_bw_khz)); |
| 176 | |
| 177 | if (MBI_TO_DBI(max_ant_gain_mbi)) |
| 178 | printf("%d", MBI_TO_DBI(max_ant_gain_mbi)); |
| 179 | else |
| 180 | printf("N/A"); |
| 181 | |
| 182 | printf(", %d)", MBM_TO_DBM(max_eirp_mbm)); |
| 183 | |
| 184 | if (!flags) { |
| 185 | printf("\n"); |
| 186 | continue; |
| 187 | } |
| 188 | |
| 189 | /* Sync this output format to match that of dbparse.py from wireless-regdb.git */ |
| 190 | PARSE_FLAG(NL80211_RRF_NO_OFDM, "NO-OFDM"); |
| 191 | PARSE_FLAG(NL80211_RRF_NO_CCK, "NO-CCK"); |
| 192 | PARSE_FLAG(NL80211_RRF_NO_INDOOR, "NO-INDOOR"); |
| 193 | PARSE_FLAG(NL80211_RRF_NO_OUTDOOR, "NO-OUTDOOR"); |
| 194 | PARSE_FLAG(NL80211_RRF_DFS, "DFS"); |
| 195 | PARSE_FLAG(NL80211_RRF_PTP_ONLY, "PTP-ONLY"); |
| 196 | PARSE_FLAG(NL80211_RRF_PASSIVE_SCAN, "PASSIVE-SCAN"); |
| 197 | PARSE_FLAG(NL80211_RRF_NO_IBSS, "NO-IBSS"); |
| 198 | |
| 199 | printf("\n"); |
| 200 | } |
| 201 | return NL_OK; |
| 202 | #undef PARSE_FLAG |
| 203 | } |
| 204 | |
Johannes Berg | 7c37a24 | 2009-04-08 13:13:28 +0200 | [diff] [blame] | 205 | static int handle_reg_get(struct nl80211_state *state, |
| 206 | struct nl_cb *cb, |
Luis R. Rodriguez | 7ab6522 | 2009-01-28 08:57:59 -0800 | [diff] [blame] | 207 | struct nl_msg *msg, |
Johannes Berg | 05514f9 | 2012-07-19 11:50:50 +0200 | [diff] [blame] | 208 | int argc, char **argv, |
| 209 | enum id_input id) |
Luis R. Rodriguez | 7ab6522 | 2009-01-28 08:57:59 -0800 | [diff] [blame] | 210 | { |
| 211 | nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_reg_handler, NULL); |
| 212 | return 0; |
| 213 | } |
Johannes Berg | 70cf454 | 2009-05-05 15:24:39 +0200 | [diff] [blame] | 214 | COMMAND(reg, get, NULL, NL80211_CMD_GET_REG, 0, CIB_NONE, handle_reg_get, |
| 215 | "Print out the kernel's current regulatory domain information."); |