1 #include <linux/nl80211.h>
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>
13 /* return 0 if not found, 1 if ok, -1 on error */
14 static int get_if_type(int *argc
, char ***argv
, enum nl80211_iftype
*type
)
21 if (strcmp((*argv
)[0], "type"))
28 if (strcmp(tpstr
, "adhoc") == 0 ||
29 strcmp(tpstr
, "ibss") == 0) {
30 *type
= NL80211_IFTYPE_ADHOC
;
32 } else if (strcmp(tpstr
, "monitor") == 0) {
33 *type
= NL80211_IFTYPE_MONITOR
;
35 } else if (strcmp(tpstr
, "ap") == 0 || strcmp(tpstr
, "master") == 0) {
36 *type
= NL80211_IFTYPE_AP
;
38 } else if (strcmp(tpstr
, "ap_vlan") == 0) {
39 *type
= NL80211_IFTYPE_AP_VLAN
;
41 } else if (strcmp(tpstr
, "wds") == 0) {
42 *type
= NL80211_IFTYPE_WDS
;
44 } else if (strcmp(tpstr
, "station") == 0) {
45 *type
= NL80211_IFTYPE_STATION
;
47 } else if (strcmp(tpstr
, "mp") == 0 ||
48 strcmp(tpstr
, "mesh") == 0) {
49 *type
= NL80211_IFTYPE_MESH_POINT
;
54 fprintf(stderr
, "invalid interface type %s\n", tpstr
);
58 static int handle_interface_add(struct nl80211_state
*state
,
59 char *phy
, char *dev
, int argc
, char **argv
)
63 enum nl80211_iftype type
;
68 fprintf(stderr
, "not enough arguments\n");
76 tpset
= get_if_type(&argc
, &argv
, &type
);
78 fprintf(stderr
, "you must specify an interface type\n");
83 if (strcmp(argv
[0], "mesh_id") != 0) {
84 fprintf(stderr
, "option %s not supported\n", argv
[0]);
91 fprintf(stderr
, "not enough arguments\n");
100 fprintf(stderr
, "too many arguments\n");
106 fprintf(stderr
, "failed to allocate netlink msg\n");
110 genlmsg_put(msg
, 0, 0, genl_family_get_id(state
->nl80211
), 0,
111 0, NL80211_CMD_NEW_INTERFACE
, 0);
113 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, if_nametoindex(dev
));
115 return -1; /* XXX TODO */
116 NLA_PUT_STRING(msg
, NL80211_ATTR_IFNAME
, name
);
118 NLA_PUT_U32(msg
, NL80211_ATTR_IFTYPE
, type
);
120 NLA_PUT(msg
, NL80211_ATTR_MESH_ID
, strlen(mesh_id
), mesh_id
);
122 if ((err
= nl_send_auto_complete(state
->nl_handle
, msg
)) < 0 ||
123 (err
= nl_wait_for_ack(state
->nl_handle
)) < 0) {
125 fprintf(stderr
, "failed to create interface: %d\n", err
);
135 static int handle_interface_del(struct nl80211_state
*state
,
136 char *phy
, char *dev
, int argc
, char **argv
)
142 fprintf(stderr
, "too many arguments\n");
150 genlmsg_put(msg
, 0, 0, genl_family_get_id(state
->nl80211
), 0,
151 0, NL80211_CMD_DEL_INTERFACE
, 0);
153 fprintf(stderr
, "need device\n");
157 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, if_nametoindex(dev
));
159 if ((err
= nl_send_auto_complete(state
->nl_handle
, msg
)) < 0 ||
160 (err
= nl_wait_for_ack(state
->nl_handle
)) < 0) {
162 fprintf(stderr
, "failed to remove interface: %d\n", err
);
172 int handle_interface(struct nl80211_state
*state
,
173 char *phy
, char *dev
, int argc
, char **argv
)
178 fprintf(stderr
, "you must specify an interface command\n");
185 if (strcmp(cmd
, "add") == 0)
186 return handle_interface_add(state
, phy
, dev
, argc
, argv
);
187 else if (strcmp(cmd
, "del") == 0)
188 return handle_interface_del(state
, phy
, dev
, argc
, argv
);
190 printf("invalid interface command %s\n", cmd
);