]> git.ipfire.org Git - thirdparty/iw.git/blob - interface.c
simplify netlink handling
[thirdparty/iw.git] / interface.c
1 #include <linux/nl80211.h>
2 #include <net/if.h>
3 #include <errno.h>
4 #include <string.h>
5
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>
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;
36 } else if (strcmp(tpstr, "__ap") == 0) {
37 *type = NL80211_IFTYPE_AP;
38 return 1;
39 } else if (strcmp(tpstr, "__ap_vlan") == 0) {
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;
48 } else if (strcmp(tpstr, "mp") == 0 ||
49 strcmp(tpstr, "mesh") == 0) {
50 *type = NL80211_IFTYPE_MESH_POINT;
51 return 1;
52 }
53
54
55 fprintf(stderr, "invalid interface type %s\n", tpstr);
56 return -1;
57 }
58
59 static int handle_interface_add(struct nl_cb *cb,
60 struct nl_msg *msg,
61 int argc, char **argv)
62 {
63 char *name;
64 char *mesh_id = NULL;
65 enum nl80211_iftype type;
66 int tpset;
67
68 if (argc < 1)
69 return 1;
70
71 name = argv[0];
72 argc--;
73 argv++;
74
75 tpset = get_if_type(&argc, &argv, &type);
76 if (tpset <= 0)
77 return 1;
78
79 if (argc) {
80 if (strcmp(argv[0], "mesh_id") != 0)
81 return 1;
82 argc--;
83 argv++;
84
85 if (!argc)
86 return 1;
87 mesh_id = argv[0];
88 argc--;
89 argv++;
90 }
91
92 if (argc)
93 return 1;
94
95 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, name);
96 if (tpset)
97 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, type);
98 if (mesh_id)
99 NLA_PUT(msg, NL80211_ATTR_MESH_ID, strlen(mesh_id), mesh_id);
100
101 return 0;
102 nla_put_failure:
103 return -ENOBUFS;
104 }
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);
109
110 static int handle_interface_del(struct nl_cb *cb,
111 struct nl_msg *msg,
112 int argc, char **argv)
113 {
114 return 0;
115 }
116 TOPLEVEL(del, NULL, NL80211_CMD_DEL_INTERFACE, 0, CIB_NETDEV, handle_interface_del);
117
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
136 static int handle_interface_info(struct nl_cb *cb,
137 struct nl_msg *msg,
138 int argc, char **argv)
139 {
140 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_iface_handler, NULL);
141 return 0;
142 }
143 TOPLEVEL(info, NULL, NL80211_CMD_GET_INTERFACE, 0, CIB_NETDEV, handle_interface_info);