]>
Commit | Line | Data |
---|---|---|
2ef1be68 | 1 | #include <net/if.h> |
3d1e8704 | 2 | #include <errno.h> |
d5ac8ad3 | 3 | #include <string.h> |
2ef1be68 | 4 | |
3d1e8704 LCC |
5 | #include <netlink/genl/genl.h> |
6 | #include <netlink/genl/family.h> | |
7 | #include <netlink/genl/ctrl.h> | |
8 | #include <netlink/msg.h> | |
9 | #include <netlink/attr.h> | |
3d1e8704 | 10 | |
f408e01b | 11 | #include "nl80211.h" |
3d1e8704 LCC |
12 | #include "iw.h" |
13 | ||
4698bfc2 JB |
14 | SECTION(mpath); |
15 | ||
3d1e8704 LCC |
16 | enum plink_state { |
17 | LISTEN, | |
18 | OPN_SNT, | |
19 | OPN_RCVD, | |
20 | CNF_RCVD, | |
21 | ESTAB, | |
22 | HOLDING, | |
23 | BLOCKED | |
24 | }; | |
25 | ||
3d1e8704 | 26 | |
3d1e8704 LCC |
27 | static int print_mpath_handler(struct nl_msg *msg, void *arg) |
28 | { | |
29 | struct nlattr *tb[NL80211_ATTR_MAX + 1]; | |
30 | struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); | |
31 | struct nlattr *pinfo[NL80211_MPATH_INFO_MAX + 1]; | |
32 | char dst[20], next_hop[20], dev[20]; | |
33 | static struct nla_policy mpath_policy[NL80211_MPATH_INFO_MAX + 1] = { | |
34 | [NL80211_MPATH_INFO_FRAME_QLEN] = { .type = NLA_U32 }, | |
456ce438 | 35 | [NL80211_MPATH_INFO_SN] = { .type = NLA_U32 }, |
3d1e8704 LCC |
36 | [NL80211_MPATH_INFO_METRIC] = { .type = NLA_U32 }, |
37 | [NL80211_MPATH_INFO_EXPTIME] = { .type = NLA_U32 }, | |
38 | [NL80211_MPATH_INFO_DISCOVERY_TIMEOUT] = { .type = NLA_U32 }, | |
39 | [NL80211_MPATH_INFO_DISCOVERY_RETRIES] = { .type = NLA_U8 }, | |
40 | [NL80211_MPATH_INFO_FLAGS] = { .type = NLA_U8 }, | |
41 | }; | |
42 | ||
43 | nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), | |
44 | genlmsg_attrlen(gnlh, 0), NULL); | |
45 | ||
46 | /* | |
47 | * TODO: validate the interface and mac address! | |
48 | * Otherwise, there's a race condition as soon as | |
49 | * the kernel starts sending mpath notifications. | |
50 | */ | |
51 | ||
52 | if (!tb[NL80211_ATTR_MPATH_INFO]) { | |
5fe70c0e | 53 | fprintf(stderr, "mpath info missing!\n"); |
3d1e8704 LCC |
54 | return NL_SKIP; |
55 | } | |
56 | if (nla_parse_nested(pinfo, NL80211_MPATH_INFO_MAX, | |
57 | tb[NL80211_ATTR_MPATH_INFO], | |
58 | mpath_policy)) { | |
5fe70c0e | 59 | fprintf(stderr, "failed to parse nested attributes!\n"); |
3d1e8704 LCC |
60 | return NL_SKIP; |
61 | } | |
62 | ||
63 | mac_addr_n2a(dst, nla_data(tb[NL80211_ATTR_MAC])); | |
64 | mac_addr_n2a(next_hop, nla_data(tb[NL80211_ATTR_MPATH_NEXT_HOP])); | |
65 | if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), dev); | |
66 | printf("%s %s %s", dst, next_hop, dev); | |
456ce438 | 67 | if (pinfo[NL80211_MPATH_INFO_SN]) |
3d1e8704 | 68 | printf("\t%u", |
456ce438 | 69 | nla_get_u32(pinfo[NL80211_MPATH_INFO_SN])); |
3d1e8704 LCC |
70 | if (pinfo[NL80211_MPATH_INFO_METRIC]) |
71 | printf("\t%u", | |
72 | nla_get_u32(pinfo[NL80211_MPATH_INFO_METRIC])); | |
73 | if (pinfo[NL80211_MPATH_INFO_FRAME_QLEN]) | |
74 | printf("\t%u", | |
75 | nla_get_u32(pinfo[NL80211_MPATH_INFO_FRAME_QLEN])); | |
76 | if (pinfo[NL80211_MPATH_INFO_EXPTIME]) | |
77 | printf("\t%u", | |
78 | nla_get_u32(pinfo[NL80211_MPATH_INFO_EXPTIME])); | |
79 | if (pinfo[NL80211_MPATH_INFO_DISCOVERY_TIMEOUT]) | |
80 | printf("\t%u", | |
81 | nla_get_u32(pinfo[NL80211_MPATH_INFO_DISCOVERY_TIMEOUT])); | |
82 | if (pinfo[NL80211_MPATH_INFO_DISCOVERY_RETRIES]) | |
83 | printf("\t%u", | |
84 | nla_get_u8(pinfo[NL80211_MPATH_INFO_DISCOVERY_RETRIES])); | |
85 | if (pinfo[NL80211_MPATH_INFO_FLAGS]) | |
86 | printf("\t0x%x", | |
87 | nla_get_u8(pinfo[NL80211_MPATH_INFO_FLAGS])); | |
88 | ||
89 | printf("\n"); | |
90 | return NL_SKIP; | |
91 | } | |
92 | ||
24091b7f PKC |
93 | static int handle_mpath_probe(struct nl80211_state *state, |
94 | struct nl_msg *msg, | |
95 | int argc, char **argv, | |
96 | enum id_input id) | |
97 | { | |
98 | unsigned char dst[ETH_ALEN]; | |
99 | unsigned char *frame; | |
100 | size_t frame_len; | |
101 | ||
102 | if (argc < 3) | |
103 | return 1; | |
104 | ||
105 | if (mac_addr_a2n(dst, argv[0])) { | |
106 | fprintf(stderr, "invalid mac address\n"); | |
107 | return 2; | |
108 | } | |
109 | ||
110 | if (strcmp("frame", argv[1]) != 0) | |
111 | return 1; | |
112 | ||
113 | frame = parse_hex(argv[2], &frame_len); | |
114 | if (!frame) { | |
115 | fprintf(stderr, "invalid frame pattern: %p\n", frame); | |
116 | return 2; | |
117 | } | |
118 | ||
119 | NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst); | |
120 | NLA_PUT(msg, NL80211_ATTR_FRAME, frame_len, frame); | |
121 | ||
122 | return 0; | |
123 | nla_put_failure: | |
124 | return -ENOBUFS; | |
125 | } | |
126 | COMMAND(mpath, probe, "<destination MAC address> frame <frame>", | |
127 | NL80211_CMD_PROBE_MESH_LINK, 0, CIB_NETDEV, handle_mpath_probe, | |
128 | "Inject ethernet frame to given peer overriding the next hop\n" | |
129 | "lookup from mpath table.\n." | |
130 | "Example: iw dev wlan0 mpath probe xx:xx:xx:xx:xx:xx frame 01:xx:xx:00\n"); | |
131 | ||
7c37a24d | 132 | static int handle_mpath_get(struct nl80211_state *state, |
f5f937fb | 133 | struct nl_msg *msg, |
05514f95 JB |
134 | int argc, char **argv, |
135 | enum id_input id) | |
3d1e8704 | 136 | { |
3d1e8704 LCC |
137 | unsigned char dst[ETH_ALEN]; |
138 | ||
f5f937fb | 139 | if (argc < 1) |
5e75fd04 | 140 | return 1; |
3d1e8704 LCC |
141 | |
142 | if (mac_addr_a2n(dst, argv[0])) { | |
143 | fprintf(stderr, "invalid mac address\n"); | |
5e75fd04 | 144 | return 2; |
3d1e8704 LCC |
145 | } |
146 | argc--; | |
147 | argv++; | |
148 | ||
f5f937fb | 149 | if (argc) |
5e75fd04 | 150 | return 1; |
3d1e8704 LCC |
151 | |
152 | NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst); | |
3d1e8704 | 153 | |
34b23014 | 154 | register_handler(print_mpath_handler, NULL); |
3d1e8704 | 155 | |
70391ccf | 156 | return 0; |
3d1e8704 | 157 | nla_put_failure: |
70391ccf | 158 | return -ENOBUFS; |
3d1e8704 | 159 | } |
f5f937fb | 160 | COMMAND(mpath, get, "<MAC address>", |
70cf4544 JB |
161 | NL80211_CMD_GET_MPATH, 0, CIB_NETDEV, handle_mpath_get, |
162 | "Get information on mesh path to the given node."); | |
f5f937fb | 163 | COMMAND(mpath, del, "<MAC address>", |
70cf4544 JB |
164 | NL80211_CMD_DEL_MPATH, 0, CIB_NETDEV, handle_mpath_get, |
165 | "Remove the mesh path to the given node."); | |
f5f937fb | 166 | |
7c37a24d | 167 | static int handle_mpath_set(struct nl80211_state *state, |
f5f937fb | 168 | struct nl_msg *msg, |
05514f95 JB |
169 | int argc, char **argv, |
170 | enum id_input id) | |
3d1e8704 | 171 | { |
3d1e8704 LCC |
172 | unsigned char dst[ETH_ALEN]; |
173 | unsigned char next_hop[ETH_ALEN]; | |
174 | ||
f5f937fb | 175 | if (argc < 3) |
5e75fd04 | 176 | return 1; |
3d1e8704 LCC |
177 | |
178 | if (mac_addr_a2n(dst, argv[0])) { | |
179 | fprintf(stderr, "invalid destination mac address\n"); | |
5e75fd04 | 180 | return 2; |
3d1e8704 LCC |
181 | } |
182 | argc--; | |
183 | argv++; | |
184 | ||
f5f937fb | 185 | if (strcmp("next_hop", argv[0]) != 0) |
5e75fd04 | 186 | return 1; |
3d1e8704 LCC |
187 | argc--; |
188 | argv++; | |
189 | ||
190 | if (mac_addr_a2n(next_hop, argv[0])) { | |
191 | fprintf(stderr, "invalid next hop mac address\n"); | |
5e75fd04 | 192 | return 2; |
3d1e8704 LCC |
193 | } |
194 | argc--; | |
195 | argv++; | |
196 | ||
f5f937fb | 197 | if (argc) |
5e75fd04 | 198 | return 1; |
3d1e8704 | 199 | |
3d1e8704 | 200 | NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst); |
3d1e8704 LCC |
201 | NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop); |
202 | ||
34b23014 | 203 | register_handler(print_mpath_handler, NULL); |
70391ccf | 204 | return 0; |
3d1e8704 | 205 | nla_put_failure: |
70391ccf | 206 | return -ENOBUFS; |
3d1e8704 | 207 | } |
f5f937fb | 208 | COMMAND(mpath, new, "<destination MAC address> next_hop <next hop MAC address>", |
70cf4544 JB |
209 | NL80211_CMD_NEW_MPATH, 0, CIB_NETDEV, handle_mpath_set, |
210 | "Create a new mesh path (instead of relying on automatic discovery)."); | |
f5f937fb | 211 | COMMAND(mpath, set, "<destination MAC address> next_hop <next hop MAC address>", |
70cf4544 JB |
212 | NL80211_CMD_SET_MPATH, 0, CIB_NETDEV, handle_mpath_set, |
213 | "Set an existing mesh path's next hop."); | |
3d1e8704 | 214 | |
7c37a24d | 215 | static int handle_mpath_dump(struct nl80211_state *state, |
f5f937fb | 216 | struct nl_msg *msg, |
05514f95 JB |
217 | int argc, char **argv, |
218 | enum id_input id) | |
3d1e8704 | 219 | { |
e1fa918a | 220 | printf("DEST ADDR NEXT HOP IFACE\tSN\tMETRIC\tQLEN\t" |
794c1e00 | 221 | "EXPTIME\t\tDTIM\tDRET\tFLAGS\n"); |
34b23014 | 222 | register_handler(print_mpath_handler, NULL); |
70391ccf | 223 | return 0; |
3d1e8704 | 224 | } |
f5f937fb | 225 | COMMAND(mpath, dump, NULL, |
70cf4544 JB |
226 | NL80211_CMD_GET_MPATH, NLM_F_DUMP, CIB_NETDEV, handle_mpath_dump, |
227 | "List known mesh paths."); |