1 #include <linux/nl80211.h>
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>
14 static char *mntr_flags
[NL80211_MNTR_FLAG_MAX
+ 1] = {
23 /* return 0 if not found, 1 if ok, -1 on error */
24 static int get_if_type(int *argc
, char ***argv
, enum nl80211_iftype
*type
)
31 if (strcmp((*argv
)[0], "type"))
38 if (strcmp(tpstr
, "adhoc") == 0 ||
39 strcmp(tpstr
, "ibss") == 0) {
40 *type
= NL80211_IFTYPE_ADHOC
;
42 } else if (strcmp(tpstr
, "monitor") == 0) {
43 *type
= NL80211_IFTYPE_MONITOR
;
45 } else if (strcmp(tpstr
, "__ap") == 0) {
46 *type
= NL80211_IFTYPE_AP
;
48 } else if (strcmp(tpstr
, "__ap_vlan") == 0) {
49 *type
= NL80211_IFTYPE_AP_VLAN
;
51 } else if (strcmp(tpstr
, "wds") == 0) {
52 *type
= NL80211_IFTYPE_WDS
;
54 } else if (strcmp(tpstr
, "station") == 0) {
55 *type
= NL80211_IFTYPE_STATION
;
57 } else if (strcmp(tpstr
, "mp") == 0 ||
58 strcmp(tpstr
, "mesh") == 0) {
59 *type
= NL80211_IFTYPE_MESH_POINT
;
64 fprintf(stderr
, "invalid interface type %s\n", tpstr
);
68 static int handle_interface_add(struct nl_cb
*cb
,
70 int argc
, char **argv
)
74 enum nl80211_iftype type
;
84 tpset
= get_if_type(&argc
, &argv
, &type
);
89 if (strcmp(argv
[0], "mesh_id") != 0)
104 NLA_PUT_STRING(msg
, NL80211_ATTR_IFNAME
, name
);
106 NLA_PUT_U32(msg
, NL80211_ATTR_IFTYPE
, type
);
108 NLA_PUT(msg
, NL80211_ATTR_MESH_ID
, strlen(mesh_id
), mesh_id
);
114 COMMAND(interface
, add
, "<name> type <type> [mesh_id <meshid>]",
115 NL80211_CMD_NEW_INTERFACE
, 0, CIB_PHY
, handle_interface_add
);
116 COMMAND(interface
, add
, "<name> type <type> [mesh_id <meshid>]",
117 NL80211_CMD_NEW_INTERFACE
, 0, CIB_NETDEV
, handle_interface_add
);
119 static int handle_interface_del(struct nl_cb
*cb
,
121 int argc
, char **argv
)
125 TOPLEVEL(del
, NULL
, NL80211_CMD_DEL_INTERFACE
, 0, CIB_NETDEV
, handle_interface_del
);
127 static int print_iface_handler(struct nl_msg
*msg
, void *arg
)
129 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
130 struct nlattr
*tb_msg
[NL80211_ATTR_MAX
+ 1];
132 nla_parse(tb_msg
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
133 genlmsg_attrlen(gnlh
, 0), NULL
);
135 if (tb_msg
[NL80211_ATTR_IFNAME
])
136 printf("Interface %s\n", nla_get_string(tb_msg
[NL80211_ATTR_IFNAME
]));
137 if (tb_msg
[NL80211_ATTR_IFINDEX
])
138 printf("\tifindex %d\n", nla_get_u32(tb_msg
[NL80211_ATTR_IFINDEX
]));
139 if (tb_msg
[NL80211_ATTR_IFTYPE
])
140 printf("\ttype %s\n", iftype_name(nla_get_u32(tb_msg
[NL80211_ATTR_IFTYPE
])));
145 static int handle_interface_info(struct nl_cb
*cb
,
147 int argc
, char **argv
)
149 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, print_iface_handler
, NULL
);
152 TOPLEVEL(info
, NULL
, NL80211_CMD_GET_INTERFACE
, 0, CIB_NETDEV
, handle_interface_info
);
154 static int handle_interface_set(struct nl_cb
*cb
,
156 int argc
, char **argv
)
158 enum nl80211_mntr_flags flag
;
159 struct nl_msg
*flags
;
165 flags
= nlmsg_alloc();
167 fprintf(stderr
, "failed to allocate flags\n");
171 NLA_PUT_U32(msg
, NL80211_ATTR_IFTYPE
, NL80211_IFTYPE_MONITOR
);
175 for (flag
= __NL80211_MNTR_FLAG_INVALID
+ 1;
176 flag
< NL80211_MNTR_FLAG_MAX
; flag
++) {
177 if (strcmp(*argv
, mntr_flags
[flag
]) == 0) {
179 NLA_PUT_FLAG(flags
, flag
);
184 fprintf(stderr
, "unknown flag %s\n", *argv
);
192 nla_put_nested(msg
, NL80211_ATTR_MNTR_FLAGS
, flags
);
202 COMMAND(set
, monitor
, "<flag> [...]",
203 NL80211_CMD_SET_INTERFACE
, 0, CIB_NETDEV
, handle_interface_set
);