Johannes Berg | 79f99b9 | 2008-01-16 00:21:59 +0100 | [diff] [blame] | 1 | #include <errno.h> |
| 2 | #include <netlink/genl/genl.h> |
| 3 | #include <netlink/genl/family.h> |
| 4 | #include <netlink/genl/ctrl.h> |
| 5 | #include <netlink/msg.h> |
| 6 | #include <netlink/attr.h> |
| 7 | #include <linux/nl80211.h> |
| 8 | |
| 9 | #include <net/if.h> |
| 10 | |
| 11 | #include "iw.h" |
| 12 | |
| 13 | static void print_flag(const char *name, int *open) |
| 14 | { |
| 15 | if (!*open) |
| 16 | printf(" ("); |
| 17 | else |
| 18 | printf(", "); |
| 19 | printf(name); |
| 20 | *open = 1; |
| 21 | } |
| 22 | |
| 23 | static int print_phy_handler(struct nl_msg *msg, void *arg) |
| 24 | { |
| 25 | struct nlattr *tb_msg[NL80211_ATTR_MAX + 1]; |
| 26 | struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); |
| 27 | |
| 28 | struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1]; |
| 29 | |
| 30 | struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1]; |
| 31 | static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = { |
| 32 | [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 }, |
| 33 | [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG }, |
| 34 | [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG }, |
| 35 | [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG }, |
| 36 | [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG }, |
| 37 | }; |
| 38 | |
| 39 | struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1]; |
| 40 | static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = { |
| 41 | [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 }, |
| 42 | [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] = { .type = NLA_FLAG }, |
| 43 | }; |
| 44 | |
| 45 | struct nlattr *nl_band; |
| 46 | struct nlattr *nl_freq; |
| 47 | struct nlattr *nl_rate; |
| 48 | int bandidx = 1; |
| 49 | int rem_band, rem_freq, rem_rate; |
| 50 | int open; |
| 51 | |
| 52 | nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), |
| 53 | genlmsg_attrlen(gnlh, 0), NULL); |
| 54 | |
| 55 | if (!tb_msg[NL80211_ATTR_WIPHY_BANDS]) |
| 56 | return NL_SKIP; |
| 57 | |
| 58 | nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band) { |
| 59 | printf("Band %d:\n", bandidx); |
| 60 | bandidx++; |
| 61 | |
| 62 | nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band), |
| 63 | nla_len(nl_band), NULL); |
| 64 | |
| 65 | printf("\tFrequencies:\n"); |
| 66 | |
| 67 | nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) { |
| 68 | nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq), |
| 69 | nla_len(nl_freq), freq_policy); |
| 70 | if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ]) |
| 71 | continue; |
| 72 | printf("\t\t* %d MHz", nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ])); |
| 73 | open = 0; |
| 74 | if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED]) |
| 75 | print_flag("disabled", &open); |
| 76 | if (tb_freq[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN]) |
| 77 | print_flag("passive scanning", &open); |
| 78 | if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IBSS]) |
| 79 | print_flag("no IBSS", &open); |
| 80 | if (tb_freq[NL80211_FREQUENCY_ATTR_RADAR]) |
| 81 | print_flag("radar detection", &open); |
| 82 | if (open) |
| 83 | printf(")"); |
| 84 | printf("\n"); |
| 85 | } |
| 86 | |
| 87 | printf("\tBitrates:\n"); |
| 88 | |
| 89 | nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) { |
| 90 | nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate), |
| 91 | nla_len(nl_rate), rate_policy); |
| 92 | if (!tb_rate[NL80211_BITRATE_ATTR_RATE]) |
| 93 | continue; |
| 94 | printf("\t\t* %2.1f Mbps", 0.1 * nla_get_u32(tb_rate[NL80211_BITRATE_ATTR_RATE])); |
| 95 | open = 0; |
| 96 | if (tb_rate[NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE]) |
| 97 | print_flag("short preamble supported", &open); |
| 98 | if (open) |
| 99 | printf(")"); |
| 100 | printf("\n"); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | return NL_SKIP; |
| 105 | } |
| 106 | |
| 107 | |
| 108 | |
| 109 | static int ack_wait_handler(struct nl_msg *msg, void *arg) |
| 110 | { |
| 111 | int *finished = arg; |
| 112 | |
| 113 | *finished = 1; |
| 114 | return NL_STOP; |
| 115 | } |
| 116 | |
| 117 | int handle_info(struct nl80211_state *state, char *phy, char *dev) |
| 118 | { |
| 119 | struct nl_msg *msg; |
| 120 | int err = -1; |
| 121 | struct nl_cb *cb = NULL; |
| 122 | int finished; |
| 123 | |
| 124 | msg = nlmsg_alloc(); |
| 125 | if (!msg) { |
| 126 | fprintf(stderr, "failed to allocate netlink msg\n"); |
| 127 | return -1; |
| 128 | } |
| 129 | |
| 130 | genlmsg_put(msg, 0, 0, genl_family_get_id(state->nl80211), 0, |
| 131 | 0, NL80211_CMD_GET_WIPHY, 0); |
| 132 | if (dev) |
| 133 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(dev)); |
| 134 | if (phy) |
| 135 | return -1; /* XXX TODO */ |
| 136 | |
| 137 | cb = nl_cb_alloc(NL_CB_CUSTOM); |
| 138 | if (!cb) |
| 139 | goto out; |
| 140 | |
| 141 | if (nl_send_auto_complete(state->nl_handle, msg) < 0) |
| 142 | goto out; |
| 143 | |
| 144 | nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_phy_handler, NULL); |
| 145 | nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_wait_handler, &finished); |
| 146 | |
| 147 | err = nl_recvmsgs(state->nl_handle, cb); |
| 148 | |
| 149 | if (!finished) |
| 150 | err = nl_wait_for_ack(state->nl_handle); |
| 151 | |
| 152 | if (err < 0) |
| 153 | goto out; |
| 154 | err = 0; |
| 155 | |
| 156 | out: |
| 157 | nl_cb_put(cb); |
| 158 | nla_put_failure: |
| 159 | if (err) |
| 160 | fprintf(stderr, "failed to get information: %d\n", err); |
| 161 | nlmsg_free(msg); |
| 162 | return err; |
| 163 | } |