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 /* return 0 if not found, 1 if ok, -1 on error */
15 static int get_if_type(int *argc
, char ***argv
, enum nl80211_iftype
*type
)
22 if (strcmp((*argv
)[0], "type"))
29 if (strcmp(tpstr
, "adhoc") == 0 ||
30 strcmp(tpstr
, "ibss") == 0) {
31 *type
= NL80211_IFTYPE_ADHOC
;
33 } else if (strcmp(tpstr
, "monitor") == 0) {
34 *type
= NL80211_IFTYPE_MONITOR
;
36 } else if (strcmp(tpstr
, "__ap") == 0) {
37 *type
= NL80211_IFTYPE_AP
;
39 } else if (strcmp(tpstr
, "__ap_vlan") == 0) {
40 *type
= NL80211_IFTYPE_AP_VLAN
;
42 } else if (strcmp(tpstr
, "wds") == 0) {
43 *type
= NL80211_IFTYPE_WDS
;
45 } else if (strcmp(tpstr
, "station") == 0) {
46 *type
= NL80211_IFTYPE_STATION
;
48 } else if (strcmp(tpstr
, "mp") == 0 ||
49 strcmp(tpstr
, "mesh") == 0) {
50 *type
= NL80211_IFTYPE_MESH_POINT
;
55 fprintf(stderr
, "invalid interface type %s\n", tpstr
);
59 static int handle_interface_add(struct nl_cb
*cb
,
61 int argc
, char **argv
)
65 enum nl80211_iftype type
;
75 tpset
= get_if_type(&argc
, &argv
, &type
);
80 if (strcmp(argv
[0], "mesh_id") != 0)
95 NLA_PUT_STRING(msg
, NL80211_ATTR_IFNAME
, name
);
97 NLA_PUT_U32(msg
, NL80211_ATTR_IFTYPE
, type
);
99 NLA_PUT(msg
, NL80211_ATTR_MESH_ID
, strlen(mesh_id
), mesh_id
);
105 COMMAND(interface
, add
, "<name> type <type> [mesh_id <meshid>]",
106 NL80211_CMD_NEW_INTERFACE
, 0, CIB_PHY
, handle_interface_add
);
107 COMMAND(interface
, add
, "<name> type <type> [mesh_id <meshid>]",
108 NL80211_CMD_NEW_INTERFACE
, 0, CIB_NETDEV
, handle_interface_add
);
110 static int handle_interface_del(struct nl_cb
*cb
,
112 int argc
, char **argv
)
116 TOPLEVEL(del
, NULL
, NL80211_CMD_DEL_INTERFACE
, 0, CIB_NETDEV
, handle_interface_del
);
118 static int print_iface_handler(struct nl_msg
*msg
, void *arg
)
120 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
121 struct nlattr
*tb_msg
[NL80211_ATTR_MAX
+ 1];
123 nla_parse(tb_msg
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
124 genlmsg_attrlen(gnlh
, 0), NULL
);
126 if (tb_msg
[NL80211_ATTR_IFNAME
])
127 printf("Interface %s\n", nla_get_string(tb_msg
[NL80211_ATTR_IFNAME
]));
128 if (tb_msg
[NL80211_ATTR_IFINDEX
])
129 printf("\tifindex %d\n", nla_get_u32(tb_msg
[NL80211_ATTR_IFINDEX
]));
130 if (tb_msg
[NL80211_ATTR_IFTYPE
])
131 printf("\ttype %s\n", iftype_name(nla_get_u32(tb_msg
[NL80211_ATTR_IFTYPE
])));
136 static int handle_interface_info(struct nl_cb
*cb
,
138 int argc
, char **argv
)
140 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, print_iface_handler
, NULL
);
143 TOPLEVEL(info
, NULL
, NL80211_CMD_GET_INTERFACE
, 0, CIB_NETDEV
, handle_interface_info
);