blob: 4db5b9e23ed9b4902c8a1494290b148e03b375dc [file] [log] [blame]
Johannes Berg2ef1be62008-04-02 17:40:11 +02001#include <net/if.h>
Luis Carlos Cobo3d1e8702008-04-01 12:03:41 -07002#include <errno.h>
Johannes Bergd5ac8ad2008-04-03 15:24:13 +02003#include <string.h>
Johannes Berg2ef1be62008-04-02 17:40:11 +02004
Luis Carlos Cobo3d1e8702008-04-01 12:03:41 -07005#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>
Luis Carlos Cobo3d1e8702008-04-01 12:03:41 -070010
Johannes Bergf408e012008-09-18 19:32:11 +020011#include "nl80211.h"
Luis Carlos Cobo3d1e8702008-04-01 12:03:41 -070012#include "iw.h"
13
14enum plink_state {
15 LISTEN,
16 OPN_SNT,
17 OPN_RCVD,
18 CNF_RCVD,
19 ESTAB,
20 HOLDING,
21 BLOCKED
22};
23
24enum plink_actions {
25 PLINK_ACTION_UNDEFINED,
26 PLINK_ACTION_OPEN,
27 PLINK_ACTION_BLOCK,
28};
29
30
Luis Carlos Cobo3d1e8702008-04-01 12:03:41 -070031static int print_mpath_handler(struct nl_msg *msg, void *arg)
32{
33 struct nlattr *tb[NL80211_ATTR_MAX + 1];
34 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
35 struct nlattr *pinfo[NL80211_MPATH_INFO_MAX + 1];
36 char dst[20], next_hop[20], dev[20];
37 static struct nla_policy mpath_policy[NL80211_MPATH_INFO_MAX + 1] = {
38 [NL80211_MPATH_INFO_FRAME_QLEN] = { .type = NLA_U32 },
39 [NL80211_MPATH_INFO_DSN] = { .type = NLA_U32 },
40 [NL80211_MPATH_INFO_METRIC] = { .type = NLA_U32 },
41 [NL80211_MPATH_INFO_EXPTIME] = { .type = NLA_U32 },
42 [NL80211_MPATH_INFO_DISCOVERY_TIMEOUT] = { .type = NLA_U32 },
43 [NL80211_MPATH_INFO_DISCOVERY_RETRIES] = { .type = NLA_U8 },
44 [NL80211_MPATH_INFO_FLAGS] = { .type = NLA_U8 },
45 };
46
47 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
48 genlmsg_attrlen(gnlh, 0), NULL);
49
50 /*
51 * TODO: validate the interface and mac address!
52 * Otherwise, there's a race condition as soon as
53 * the kernel starts sending mpath notifications.
54 */
55
56 if (!tb[NL80211_ATTR_MPATH_INFO]) {
57 fprintf(stderr, "mpath info missing!");
58 return NL_SKIP;
59 }
60 if (nla_parse_nested(pinfo, NL80211_MPATH_INFO_MAX,
61 tb[NL80211_ATTR_MPATH_INFO],
62 mpath_policy)) {
63 fprintf(stderr, "failed to parse nested attributes!");
64 return NL_SKIP;
65 }
66
67 mac_addr_n2a(dst, nla_data(tb[NL80211_ATTR_MAC]));
68 mac_addr_n2a(next_hop, nla_data(tb[NL80211_ATTR_MPATH_NEXT_HOP]));
69 if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), dev);
70 printf("%s %s %s", dst, next_hop, dev);
71 if (pinfo[NL80211_MPATH_INFO_DSN])
72 printf("\t%u",
73 nla_get_u32(pinfo[NL80211_MPATH_INFO_DSN]));
74 if (pinfo[NL80211_MPATH_INFO_METRIC])
75 printf("\t%u",
76 nla_get_u32(pinfo[NL80211_MPATH_INFO_METRIC]));
77 if (pinfo[NL80211_MPATH_INFO_FRAME_QLEN])
78 printf("\t%u",
79 nla_get_u32(pinfo[NL80211_MPATH_INFO_FRAME_QLEN]));
80 if (pinfo[NL80211_MPATH_INFO_EXPTIME])
81 printf("\t%u",
82 nla_get_u32(pinfo[NL80211_MPATH_INFO_EXPTIME]));
83 if (pinfo[NL80211_MPATH_INFO_DISCOVERY_TIMEOUT])
84 printf("\t%u",
85 nla_get_u32(pinfo[NL80211_MPATH_INFO_DISCOVERY_TIMEOUT]));
86 if (pinfo[NL80211_MPATH_INFO_DISCOVERY_RETRIES])
87 printf("\t%u",
88 nla_get_u8(pinfo[NL80211_MPATH_INFO_DISCOVERY_RETRIES]));
89 if (pinfo[NL80211_MPATH_INFO_FLAGS])
90 printf("\t0x%x",
91 nla_get_u8(pinfo[NL80211_MPATH_INFO_FLAGS]));
92
93 printf("\n");
94 return NL_SKIP;
95}
96
Johannes Berg70391cc2008-09-16 18:35:06 +020097static int handle_mpath_get(struct nl_cb *cb,
Johannes Bergf5f937f2008-09-16 17:20:32 +020098 struct nl_msg *msg,
99 int argc, char **argv)
Luis Carlos Cobo3d1e8702008-04-01 12:03:41 -0700100{
Luis Carlos Cobo3d1e8702008-04-01 12:03:41 -0700101 unsigned char dst[ETH_ALEN];
102
Johannes Bergf5f937f2008-09-16 17:20:32 +0200103 if (argc < 1)
Johannes Berg5e75fd02008-09-16 18:13:12 +0200104 return 1;
Luis Carlos Cobo3d1e8702008-04-01 12:03:41 -0700105
106 if (mac_addr_a2n(dst, argv[0])) {
107 fprintf(stderr, "invalid mac address\n");
Johannes Berg5e75fd02008-09-16 18:13:12 +0200108 return 2;
Luis Carlos Cobo3d1e8702008-04-01 12:03:41 -0700109 }
110 argc--;
111 argv++;
112
Johannes Bergf5f937f2008-09-16 17:20:32 +0200113 if (argc)
Johannes Berg5e75fd02008-09-16 18:13:12 +0200114 return 1;
Luis Carlos Cobo3d1e8702008-04-01 12:03:41 -0700115
116 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
Luis Carlos Cobo3d1e8702008-04-01 12:03:41 -0700117
Luis Carlos Cobo3d1e8702008-04-01 12:03:41 -0700118 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_mpath_handler, NULL);
Luis Carlos Cobo3d1e8702008-04-01 12:03:41 -0700119
Johannes Berg70391cc2008-09-16 18:35:06 +0200120 return 0;
Luis Carlos Cobo3d1e8702008-04-01 12:03:41 -0700121 nla_put_failure:
Johannes Berg70391cc2008-09-16 18:35:06 +0200122 return -ENOBUFS;
Luis Carlos Cobo3d1e8702008-04-01 12:03:41 -0700123}
Johannes Bergf5f937f2008-09-16 17:20:32 +0200124COMMAND(mpath, get, "<MAC address>",
125 NL80211_CMD_GET_MPATH, 0, CIB_NETDEV, handle_mpath_get);
126COMMAND(mpath, del, "<MAC address>",
127 NL80211_CMD_DEL_MPATH, 0, CIB_NETDEV, handle_mpath_get);
Luis Carlos Cobo3d1e8702008-04-01 12:03:41 -0700128
Johannes Berg70391cc2008-09-16 18:35:06 +0200129static int handle_mpath_set(struct nl_cb *cb,
Johannes Bergf5f937f2008-09-16 17:20:32 +0200130 struct nl_msg *msg,
131 int argc, char **argv)
Luis Carlos Cobo3d1e8702008-04-01 12:03:41 -0700132{
Luis Carlos Cobo3d1e8702008-04-01 12:03:41 -0700133 unsigned char dst[ETH_ALEN];
134 unsigned char next_hop[ETH_ALEN];
135
Johannes Bergf5f937f2008-09-16 17:20:32 +0200136 if (argc < 3)
Johannes Berg5e75fd02008-09-16 18:13:12 +0200137 return 1;
Luis Carlos Cobo3d1e8702008-04-01 12:03:41 -0700138
139 if (mac_addr_a2n(dst, argv[0])) {
140 fprintf(stderr, "invalid destination mac address\n");
Johannes Berg5e75fd02008-09-16 18:13:12 +0200141 return 2;
Luis Carlos Cobo3d1e8702008-04-01 12:03:41 -0700142 }
143 argc--;
144 argv++;
145
Johannes Bergf5f937f2008-09-16 17:20:32 +0200146 if (strcmp("next_hop", argv[0]) != 0)
Johannes Berg5e75fd02008-09-16 18:13:12 +0200147 return 1;
Luis Carlos Cobo3d1e8702008-04-01 12:03:41 -0700148 argc--;
149 argv++;
150
151 if (mac_addr_a2n(next_hop, argv[0])) {
152 fprintf(stderr, "invalid next hop mac address\n");
Johannes Berg5e75fd02008-09-16 18:13:12 +0200153 return 2;
Luis Carlos Cobo3d1e8702008-04-01 12:03:41 -0700154 }
155 argc--;
156 argv++;
157
Johannes Bergf5f937f2008-09-16 17:20:32 +0200158 if (argc)
Johannes Berg5e75fd02008-09-16 18:13:12 +0200159 return 1;
Luis Carlos Cobo3d1e8702008-04-01 12:03:41 -0700160
Luis Carlos Cobo3d1e8702008-04-01 12:03:41 -0700161 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
Luis Carlos Cobo3d1e8702008-04-01 12:03:41 -0700162 NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
163
Luis Carlos Cobo3d1e8702008-04-01 12:03:41 -0700164 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_mpath_handler, NULL);
Johannes Berg70391cc2008-09-16 18:35:06 +0200165 return 0;
Luis Carlos Cobo3d1e8702008-04-01 12:03:41 -0700166 nla_put_failure:
Johannes Berg70391cc2008-09-16 18:35:06 +0200167 return -ENOBUFS;
Luis Carlos Cobo3d1e8702008-04-01 12:03:41 -0700168}
Johannes Bergf5f937f2008-09-16 17:20:32 +0200169COMMAND(mpath, new, "<destination MAC address> next_hop <next hop MAC address>",
170 NL80211_CMD_NEW_MPATH, 0, CIB_NETDEV, handle_mpath_set);
171COMMAND(mpath, set, "<destination MAC address> next_hop <next hop MAC address>",
172 NL80211_CMD_SET_MPATH, 0, CIB_NETDEV, handle_mpath_set);
Luis Carlos Cobo3d1e8702008-04-01 12:03:41 -0700173
Johannes Berg70391cc2008-09-16 18:35:06 +0200174static int handle_mpath_dump(struct nl_cb *cb,
Johannes Bergf5f937f2008-09-16 17:20:32 +0200175 struct nl_msg *msg,
176 int argc, char **argv)
Luis Carlos Cobo3d1e8702008-04-01 12:03:41 -0700177{
Luis Carlos Cobo3d1e8702008-04-01 12:03:41 -0700178 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_mpath_handler, NULL);
Johannes Berg70391cc2008-09-16 18:35:06 +0200179 return 0;
Luis Carlos Cobo3d1e8702008-04-01 12:03:41 -0700180}
Johannes Bergf5f937f2008-09-16 17:20:32 +0200181COMMAND(mpath, dump, NULL,
182 NL80211_CMD_GET_MPATH, NLM_F_DUMP, CIB_NETDEV, handle_mpath_dump);