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>
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
);
126 HIDDEN(interface
, del
, NULL
, NL80211_CMD_DEL_INTERFACE
, 0, CIB_NETDEV
, handle_interface_del
);
128 static int print_iface_handler(struct nl_msg
*msg
, void *arg
)
130 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
131 struct nlattr
*tb_msg
[NL80211_ATTR_MAX
+ 1];
133 nla_parse(tb_msg
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
134 genlmsg_attrlen(gnlh
, 0), NULL
);
136 if (tb_msg
[NL80211_ATTR_IFNAME
])
137 printf("Interface %s\n", nla_get_string(tb_msg
[NL80211_ATTR_IFNAME
]));
138 if (tb_msg
[NL80211_ATTR_IFINDEX
])
139 printf("\tifindex %d\n", nla_get_u32(tb_msg
[NL80211_ATTR_IFINDEX
]));
140 if (tb_msg
[NL80211_ATTR_IFTYPE
])
141 printf("\ttype %s\n", iftype_name(nla_get_u32(tb_msg
[NL80211_ATTR_IFTYPE
])));
146 static int handle_interface_info(struct nl_cb
*cb
,
148 int argc
, char **argv
)
150 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, print_iface_handler
, NULL
);
153 TOPLEVEL(info
, NULL
, NL80211_CMD_GET_INTERFACE
, 0, CIB_NETDEV
, handle_interface_info
);
155 static int handle_interface_set(struct nl_cb
*cb
,
157 int argc
, char **argv
)
159 enum nl80211_mntr_flags flag
;
160 struct nl_msg
*flags
;
166 flags
= nlmsg_alloc();
168 fprintf(stderr
, "failed to allocate flags\n");
172 NLA_PUT_U32(msg
, NL80211_ATTR_IFTYPE
, NL80211_IFTYPE_MONITOR
);
176 for (flag
= __NL80211_MNTR_FLAG_INVALID
+ 1;
177 flag
< NL80211_MNTR_FLAG_MAX
; flag
++) {
178 if (strcmp(*argv
, mntr_flags
[flag
]) == 0) {
180 NLA_PUT_FLAG(flags
, flag
);
185 fprintf(stderr
, "unknown flag %s\n", *argv
);
193 nla_put_nested(msg
, NL80211_ATTR_MNTR_FLAGS
, flags
);
203 COMMAND(set
, monitor
, "<flag> [...]",
204 NL80211_CMD_SET_INTERFACE
, 0, CIB_NETDEV
, handle_interface_set
);
206 static int handle_interface_meshid(struct nl_cb
*cb
,
208 int argc
, char **argv
)
210 char *mesh_id
= NULL
;
217 NLA_PUT(msg
, NL80211_ATTR_MESH_ID
, strlen(mesh_id
), mesh_id
);
223 COMMAND(set
, meshid
, "<meshid>",
224 NL80211_CMD_SET_INTERFACE
, 0, CIB_NETDEV
, handle_interface_meshid
);