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 | |
Luis R. Rodriguez | 7ab6522 | 2009-01-28 08:57:59 -0800 | [diff] [blame] | 15 | #define MHZ_TO_KHZ(freq) ((freq) * 1000) |
| 16 | #define KHZ_TO_MHZ(freq) ((freq) / 1000) |
| 17 | #define DBI_TO_MBI(gain) ((gain) * 100) |
| 18 | #define MBI_TO_DBI(gain) ((gain) / 100) |
| 19 | #define DBM_TO_MBM(gain) ((gain) * 100) |
| 20 | #define MBM_TO_DBM(gain) ((gain) / 100) |
| 21 | |
Luis R. Rodriguez | ffc08bc | 2009-03-09 22:28:49 -0400 | [diff] [blame^] | 22 | static bool isalpha_upper(char letter) |
Luis R. Rodriguez | 14a0380 | 2008-08-29 16:01:56 -0700 | [diff] [blame] | 23 | { |
| 24 | if (letter >= 65 && letter <= 90) |
Luis R. Rodriguez | ffc08bc | 2009-03-09 22:28:49 -0400 | [diff] [blame^] | 25 | return true; |
| 26 | return false; |
Luis R. Rodriguez | 14a0380 | 2008-08-29 16:01:56 -0700 | [diff] [blame] | 27 | } |
| 28 | |
Luis R. Rodriguez | ffc08bc | 2009-03-09 22:28:49 -0400 | [diff] [blame^] | 29 | static bool is_alpha2(char *alpha2) |
Luis R. Rodriguez | 14a0380 | 2008-08-29 16:01:56 -0700 | [diff] [blame] | 30 | { |
| 31 | if (isalpha_upper(alpha2[0]) && isalpha_upper(alpha2[1])) |
Luis R. Rodriguez | ffc08bc | 2009-03-09 22:28:49 -0400 | [diff] [blame^] | 32 | return true; |
| 33 | return false; |
Luis R. Rodriguez | 14a0380 | 2008-08-29 16:01:56 -0700 | [diff] [blame] | 34 | } |
| 35 | |
Luis R. Rodriguez | ffc08bc | 2009-03-09 22:28:49 -0400 | [diff] [blame^] | 36 | static bool is_world_regdom(char *alpha2) |
Luis R. Rodriguez | 14a0380 | 2008-08-29 16:01:56 -0700 | [diff] [blame] | 37 | { |
| 38 | /* ASCII 0 */ |
| 39 | if (alpha2[0] == 48 && alpha2[1] == 48) |
Luis R. Rodriguez | ffc08bc | 2009-03-09 22:28:49 -0400 | [diff] [blame^] | 40 | return true; |
| 41 | return false; |
Luis R. Rodriguez | 14a0380 | 2008-08-29 16:01:56 -0700 | [diff] [blame] | 42 | } |
| 43 | |
Johannes Berg | 70391cc | 2008-09-16 18:35:06 +0200 | [diff] [blame] | 44 | static int handle_reg_set(struct nl_cb *cb, |
Johannes Berg | 1c05c10 | 2008-09-16 17:09:04 +0200 | [diff] [blame] | 45 | struct nl_msg *msg, |
| 46 | int argc, char **argv) |
Luis R. Rodriguez | 14a0380 | 2008-08-29 16:01:56 -0700 | [diff] [blame] | 47 | { |
Luis R. Rodriguez | 14a0380 | 2008-08-29 16:01:56 -0700 | [diff] [blame] | 48 | char alpha2[3]; |
| 49 | |
Johannes Berg | 1c05c10 | 2008-09-16 17:09:04 +0200 | [diff] [blame] | 50 | if (argc < 1) |
Johannes Berg | 5e75fd0 | 2008-09-16 18:13:12 +0200 | [diff] [blame] | 51 | return 1; |
Luis R. Rodriguez | 14a0380 | 2008-08-29 16:01:56 -0700 | [diff] [blame] | 52 | |
| 53 | if (!is_alpha2(argv[0]) && !is_world_regdom(argv[0])) { |
| 54 | fprintf(stderr, "not a valid ISO/IEC 3166-1 alpha2\n"); |
Johannes Berg | 40ffa98 | 2008-09-16 19:12:57 +0200 | [diff] [blame] | 55 | fprintf(stderr, "Special non-alpha2 usable entries:\n"); |
Luis R. Rodriguez | 14a0380 | 2008-08-29 16:01:56 -0700 | [diff] [blame] | 56 | fprintf(stderr, "\t00\tWorld Regulatory domain\n"); |
Johannes Berg | 5e75fd0 | 2008-09-16 18:13:12 +0200 | [diff] [blame] | 57 | return 2; |
Luis R. Rodriguez | 14a0380 | 2008-08-29 16:01:56 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | alpha2[0] = argv[0][0]; |
| 61 | alpha2[1] = argv[0][1]; |
| 62 | alpha2[2] = '\0'; |
| 63 | |
| 64 | argc--; |
| 65 | argv++; |
| 66 | |
Johannes Berg | 1c05c10 | 2008-09-16 17:09:04 +0200 | [diff] [blame] | 67 | if (argc) |
Johannes Berg | 5e75fd0 | 2008-09-16 18:13:12 +0200 | [diff] [blame] | 68 | return 1; |
Luis R. Rodriguez | 14a0380 | 2008-08-29 16:01:56 -0700 | [diff] [blame] | 69 | |
| 70 | NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, alpha2); |
| 71 | |
Johannes Berg | 70391cc | 2008-09-16 18:35:06 +0200 | [diff] [blame] | 72 | return 0; |
Luis R. Rodriguez | 14a0380 | 2008-08-29 16:01:56 -0700 | [diff] [blame] | 73 | nla_put_failure: |
Johannes Berg | 70391cc | 2008-09-16 18:35:06 +0200 | [diff] [blame] | 74 | return -ENOBUFS; |
Luis R. Rodriguez | 14a0380 | 2008-08-29 16:01:56 -0700 | [diff] [blame] | 75 | } |
Johannes Berg | 1c05c10 | 2008-09-16 17:09:04 +0200 | [diff] [blame] | 76 | COMMAND(reg, set, "<ISO/IEC 3166-1 alpha2>", |
| 77 | NL80211_CMD_REQ_SET_REG, 0, CIB_NONE, handle_reg_set); |
Luis R. Rodriguez | 7ab6522 | 2009-01-28 08:57:59 -0800 | [diff] [blame] | 78 | |
| 79 | static int print_reg_handler(struct nl_msg *msg, void *arg) |
| 80 | |
| 81 | { |
| 82 | #define PARSE_FLAG(nl_flag, string_value) do { \ |
| 83 | if ((flags & nl_flag)) { \ |
| 84 | printf(", %s", string_value); \ |
| 85 | } \ |
| 86 | } while (0) |
| 87 | struct nlattr *tb_msg[NL80211_ATTR_MAX + 1]; |
| 88 | struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); |
| 89 | char *alpha2; |
| 90 | struct nlattr *nl_rule; |
| 91 | int rem_rule; |
| 92 | static struct nla_policy reg_rule_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = { |
| 93 | [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 }, |
| 94 | [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 }, |
| 95 | [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 }, |
| 96 | [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 }, |
| 97 | [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 }, |
| 98 | [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 }, |
| 99 | }; |
| 100 | |
| 101 | nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), |
| 102 | genlmsg_attrlen(gnlh, 0), NULL); |
| 103 | |
| 104 | if (!tb_msg[NL80211_ATTR_REG_ALPHA2]) { |
| 105 | printf("No alpha2\n"); |
| 106 | return NL_SKIP; |
| 107 | } |
| 108 | |
| 109 | if (!tb_msg[NL80211_ATTR_REG_RULES]) { |
| 110 | printf("No reg rules\n"); |
| 111 | return NL_SKIP; |
| 112 | } |
| 113 | |
| 114 | alpha2 = nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]); |
| 115 | printf("country %s:\n", alpha2); |
| 116 | |
| 117 | nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule) { |
| 118 | struct nlattr *tb_rule[NL80211_FREQUENCY_ATTR_MAX + 1]; |
| 119 | __u32 flags, start_freq_khz, end_freq_khz, max_bw_khz, max_ant_gain_mbi, max_eirp_mbm; |
| 120 | |
| 121 | nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_rule), nla_len(nl_rule), reg_rule_policy); |
| 122 | |
| 123 | flags = nla_get_u32(tb_rule[NL80211_ATTR_REG_RULE_FLAGS]); |
| 124 | start_freq_khz = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_START]); |
| 125 | end_freq_khz = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_END]); |
| 126 | max_bw_khz = nla_get_u32(tb_rule[NL80211_ATTR_FREQ_RANGE_MAX_BW]); |
| 127 | max_ant_gain_mbi = nla_get_u32(tb_rule[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]); |
| 128 | max_eirp_mbm = nla_get_u32(tb_rule[NL80211_ATTR_POWER_RULE_MAX_EIRP]); |
| 129 | |
| 130 | |
| 131 | printf("\t(%d - %d @ %d), (", |
| 132 | KHZ_TO_MHZ(start_freq_khz), KHZ_TO_MHZ(end_freq_khz), KHZ_TO_MHZ(max_bw_khz)); |
| 133 | |
| 134 | if (MBI_TO_DBI(max_ant_gain_mbi)) |
| 135 | printf("%d", MBI_TO_DBI(max_ant_gain_mbi)); |
| 136 | else |
| 137 | printf("N/A"); |
| 138 | |
| 139 | printf(", %d)", MBM_TO_DBM(max_eirp_mbm)); |
| 140 | |
| 141 | if (!flags) { |
| 142 | printf("\n"); |
| 143 | continue; |
| 144 | } |
| 145 | |
| 146 | /* Sync this output format to match that of dbparse.py from wireless-regdb.git */ |
| 147 | PARSE_FLAG(NL80211_RRF_NO_OFDM, "NO-OFDM"); |
| 148 | PARSE_FLAG(NL80211_RRF_NO_CCK, "NO-CCK"); |
| 149 | PARSE_FLAG(NL80211_RRF_NO_INDOOR, "NO-INDOOR"); |
| 150 | PARSE_FLAG(NL80211_RRF_NO_OUTDOOR, "NO-OUTDOOR"); |
| 151 | PARSE_FLAG(NL80211_RRF_DFS, "DFS"); |
| 152 | PARSE_FLAG(NL80211_RRF_PTP_ONLY, "PTP-ONLY"); |
| 153 | PARSE_FLAG(NL80211_RRF_PASSIVE_SCAN, "PASSIVE-SCAN"); |
| 154 | PARSE_FLAG(NL80211_RRF_NO_IBSS, "NO-IBSS"); |
| 155 | |
| 156 | printf("\n"); |
| 157 | } |
| 158 | return NL_OK; |
| 159 | #undef PARSE_FLAG |
| 160 | } |
| 161 | |
| 162 | static int handle_reg_get(struct nl_cb *cb, |
| 163 | struct nl_msg *msg, |
| 164 | int argc, char **argv) |
| 165 | { |
| 166 | nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_reg_handler, NULL); |
| 167 | return 0; |
| 168 | } |
| 169 | COMMAND(reg, get, NULL, NL80211_CMD_GET_REG, 0, CIB_NONE, handle_reg_get); |