3 #include <netlink/genl/genl.h>
4 #include <netlink/genl/family.h>
5 #include <netlink/genl/ctrl.h>
6 #include <netlink/msg.h>
7 #include <netlink/attr.h>
8 #include <linux/nl80211.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
;
51 fprintf(stderr
, "invalid interface type %s\n", tpstr
);
55 static int handle_interface_add(struct nl80211_state
*state
,
56 char *phy
, char *dev
, int argc
, char **argv
)
59 enum nl80211_iftype type
;
64 fprintf(stderr
, "not enough arguments\n");
72 tpset
= get_if_type(&argc
, &argv
, &type
);
78 fprintf(stderr
, "too many arguments\n");
86 genlmsg_put(msg
, 0, 0, genl_family_get_id(state
->nl80211
), 0,
87 0, NL80211_CMD_NEW_INTERFACE
, 0);
89 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, if_nametoindex(dev
));
91 return -1; /* XXX TODO */
92 NLA_PUT_STRING(msg
, NL80211_ATTR_IFNAME
, name
);
94 NLA_PUT_U32(msg
, NL80211_ATTR_IFTYPE
, type
);
96 if ((err
= nl_send_auto_complete(state
->nl_handle
, msg
)) < 0 ||
97 (err
= nl_wait_for_ack(state
->nl_handle
)) < 0) {
99 fprintf(stderr
, "failed to create interface: %d\n", err
);
109 static int handle_interface_del(struct nl80211_state
*state
,
110 char *phy
, char *dev
, int argc
, char **argv
)
116 fprintf(stderr
, "too many arguments\n");
124 genlmsg_put(msg
, 0, 0, genl_family_get_id(state
->nl80211
), 0,
125 0, NL80211_CMD_DEL_INTERFACE
, 0);
127 fprintf(stderr
, "need device\n");
131 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, if_nametoindex(dev
));
133 if ((err
= nl_send_auto_complete(state
->nl_handle
, msg
)) < 0 ||
134 (err
= nl_wait_for_ack(state
->nl_handle
)) < 0) {
136 fprintf(stderr
, "failed to remove interface: %d\n", err
);
146 int handle_interface(struct nl80211_state
*state
,
147 char *phy
, char *dev
, int argc
, char **argv
)
157 if (strcmp(cmd
, "add") == 0)
158 return handle_interface_add(state
, phy
, dev
, argc
, argv
);
159 else if (strcmp(cmd
, "del") == 0)
160 return handle_interface_del(state
, phy
, dev
, argc
, argv
);
162 printf("invalid interface command %s\n", cmd
);