]>
Commit | Line | Data |
---|---|---|
2ef1be68 JB |
1 | #include <linux/nl80211.h> |
2 | #include <net/if.h> | |
45c7212c | 3 | #include <errno.h> |
d5ac8ad3 | 4 | #include <string.h> |
2ef1be68 | 5 | |
45c7212c JB |
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> | |
45c7212c JB |
11 | |
12 | #include "iw.h" | |
13 | ||
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) | |
16 | { | |
17 | char *tpstr; | |
18 | ||
19 | if (*argc < 2) | |
20 | return 0; | |
21 | ||
22 | if (strcmp((*argv)[0], "type")) | |
23 | return 0; | |
24 | ||
25 | tpstr = (*argv)[1]; | |
26 | *argc -= 2; | |
27 | *argv += 2; | |
28 | ||
29 | if (strcmp(tpstr, "adhoc") == 0 || | |
30 | strcmp(tpstr, "ibss") == 0) { | |
31 | *type = NL80211_IFTYPE_ADHOC; | |
32 | return 1; | |
33 | } else if (strcmp(tpstr, "monitor") == 0) { | |
34 | *type = NL80211_IFTYPE_MONITOR; | |
35 | return 1; | |
4d3a72da | 36 | } else if (strcmp(tpstr, "__ap") == 0) { |
45c7212c JB |
37 | *type = NL80211_IFTYPE_AP; |
38 | return 1; | |
4d3a72da | 39 | } else if (strcmp(tpstr, "__ap_vlan") == 0) { |
45c7212c JB |
40 | *type = NL80211_IFTYPE_AP_VLAN; |
41 | return 1; | |
42 | } else if (strcmp(tpstr, "wds") == 0) { | |
43 | *type = NL80211_IFTYPE_WDS; | |
44 | return 1; | |
45 | } else if (strcmp(tpstr, "station") == 0) { | |
46 | *type = NL80211_IFTYPE_STATION; | |
47 | return 1; | |
3d1e8704 LCC |
48 | } else if (strcmp(tpstr, "mp") == 0 || |
49 | strcmp(tpstr, "mesh") == 0) { | |
50 | *type = NL80211_IFTYPE_MESH_POINT; | |
51 | return 1; | |
45c7212c JB |
52 | } |
53 | ||
54 | ||
55 | fprintf(stderr, "invalid interface type %s\n", tpstr); | |
56 | return -1; | |
57 | } | |
58 | ||
70391ccf | 59 | static int handle_interface_add(struct nl_cb *cb, |
bd396f2a JB |
60 | struct nl_msg *msg, |
61 | int argc, char **argv) | |
45c7212c | 62 | { |
2dfd6bfa | 63 | char *name; |
3d1e8704 | 64 | char *mesh_id = NULL; |
45c7212c | 65 | enum nl80211_iftype type; |
70391ccf | 66 | int tpset; |
45c7212c | 67 | |
bd396f2a | 68 | if (argc < 1) |
5e75fd04 | 69 | return 1; |
45c7212c | 70 | |
2dfd6bfa | 71 | name = argv[0]; |
45c7212c JB |
72 | argc--; |
73 | argv++; | |
74 | ||
2dfd6bfa | 75 | tpset = get_if_type(&argc, &argv, &type); |
2dfd6bfa | 76 | if (tpset <= 0) |
5e75fd04 | 77 | return 1; |
45c7212c | 78 | |
3d1e8704 | 79 | if (argc) { |
5e75fd04 JB |
80 | if (strcmp(argv[0], "mesh_id") != 0) |
81 | return 1; | |
3d1e8704 LCC |
82 | argc--; |
83 | argv++; | |
84 | ||
5e75fd04 JB |
85 | if (!argc) |
86 | return 1; | |
3d1e8704 LCC |
87 | mesh_id = argv[0]; |
88 | argc--; | |
89 | argv++; | |
90 | } | |
91 | ||
5e75fd04 JB |
92 | if (argc) |
93 | return 1; | |
45c7212c | 94 | |
45c7212c JB |
95 | NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, name); |
96 | if (tpset) | |
97 | NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, type); | |
3d1e8704 LCC |
98 | if (mesh_id) |
99 | NLA_PUT(msg, NL80211_ATTR_MESH_ID, strlen(mesh_id), mesh_id); | |
45c7212c | 100 | |
70391ccf | 101 | return 0; |
5e75fd04 | 102 | nla_put_failure: |
70391ccf | 103 | return -ENOBUFS; |
45c7212c | 104 | } |
bd396f2a JB |
105 | COMMAND(interface, add, "<name> type <type> [mesh_id <meshid>]", |
106 | NL80211_CMD_NEW_INTERFACE, 0, CIB_PHY, handle_interface_add); | |
1cd3b6c6 JB |
107 | COMMAND(interface, add, "<name> type <type> [mesh_id <meshid>]", |
108 | NL80211_CMD_NEW_INTERFACE, 0, CIB_NETDEV, handle_interface_add); | |
45c7212c | 109 | |
70391ccf | 110 | static int handle_interface_del(struct nl_cb *cb, |
bd396f2a JB |
111 | struct nl_msg *msg, |
112 | int argc, char **argv) | |
3fcfe40e | 113 | { |
70391ccf | 114 | return 0; |
3fcfe40e | 115 | } |
bd396f2a | 116 | TOPLEVEL(del, NULL, NL80211_CMD_DEL_INTERFACE, 0, CIB_NETDEV, handle_interface_del); |
3fcfe40e | 117 | |
541ef425 JB |
118 | static int print_iface_handler(struct nl_msg *msg, void *arg) |
119 | { | |
120 | struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); | |
121 | struct nlattr *tb_msg[NL80211_ATTR_MAX + 1]; | |
122 | ||
123 | nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), | |
124 | genlmsg_attrlen(gnlh, 0), NULL); | |
125 | ||
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]))); | |
132 | ||
133 | return NL_SKIP; | |
134 | } | |
135 | ||
70391ccf | 136 | static int handle_interface_info(struct nl_cb *cb, |
bd396f2a JB |
137 | struct nl_msg *msg, |
138 | int argc, char **argv) | |
541ef425 | 139 | { |
541ef425 | 140 | nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_iface_handler, NULL); |
70391ccf | 141 | return 0; |
541ef425 | 142 | } |
bd396f2a | 143 | TOPLEVEL(info, NULL, NL80211_CMD_GET_INTERFACE, 0, CIB_NETDEV, handle_interface_info); |