2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2012 Daniel Borkmann.
4 * Subject to the GPL, version 2.
5 * Parts derived from iw, subject to ISC license.
6 * Copyright 2007, 2008 Johannes Berg
7 * Copyright 2007 Andy Lutomirski
8 * Copyright 2007 Mike Kershaw
9 * Copyright 2008-2009 Luis R. Rodriguez
18 #include <linux/nl80211.h>
19 #include <libnl3/netlink/genl/genl.h>
20 #include <libnl3/netlink/genl/family.h>
21 #include <libnl3/netlink/genl/ctrl.h>
22 #include <libnl3/netlink/msg.h>
23 #include <libnl3/netlink/attr.h>
31 struct nl80211_state
{
32 struct nl_sock
*nl_sock
;
33 struct nl_cache
*nl_cache
;
34 struct genl_family
*nl80211
;
37 static void get_mac80211_phydev(const char *device
, char *phydev_path
,
44 ret
= asprintf(&pathstr
, "/sys/class/net/%s/phy80211", device
);
46 panic("Can't generate path name string for /sys/class/net device");
48 num
= readlink(pathstr
, phydev_path
, phydev_len
);
50 if (errno
== ENOENT
|| errno
== EINVAL
)
51 panic("It's probably not a mac80211 device!\n");
52 panic("Can't readlink %s: %s!\n", pathstr
, strerror(errno
));
56 phydev_path
[min(num
, phydev_len
- 1)] = 0;
59 static inline struct nl_msg
*nl80211_nlmsg_xalloc(void)
61 struct nl_msg
*ret
= nlmsg_alloc();
63 panic("Cannot allocate nlmsg memory!\n");
67 static inline struct nl_sock
*nl80211_nl_socket_xalloc(void)
69 struct nl_sock
*ret
= nl_socket_alloc();
71 panic("Cannot allocate nl socket memory!\n");
75 static void nl80211_init(struct nl80211_state
*state
, const char *device
)
79 state
->nl_sock
= nl80211_nl_socket_xalloc();
81 ret
= genl_connect(state
->nl_sock
);
83 panic("Cannot connect generic netlink!\n");
85 ret
= genl_ctrl_alloc_cache(state
->nl_sock
, &state
->nl_cache
);
87 panic("Failed to allocate generic netlink cache: %s!",
90 state
->nl80211
= genl_ctrl_search_by_name(state
->nl_cache
, "nl80211");
92 panic("nl80211 not found in netlink cache!\n");
95 static void nl80211_cleanup(struct nl80211_state
*state
)
97 genl_family_put(state
->nl80211
);
99 nl_cache_free(state
->nl_cache
);
100 nl_socket_free(state
->nl_sock
);
103 static int nl80211_add_mon_if(struct nl80211_state
*state
, const char *device
,
104 const char *mondevice
)
109 ifindex
= device_ifindex(device
);
111 msg
= nl80211_nlmsg_xalloc();
113 genlmsg_put(msg
, 0, 0, genl_family_get_id(state
->nl80211
), 0,
114 0, NL80211_CMD_NEW_INTERFACE
, 0);
116 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, ifindex
);
117 NLA_PUT_STRING(msg
, NL80211_ATTR_IFNAME
, mondevice
);
118 NLA_PUT_U32(msg
, NL80211_ATTR_IFTYPE
, NL80211_IFTYPE_MONITOR
);
120 ret
= nl_send_auto_complete(state
->nl_sock
, msg
);
122 if (ret
== -ENFILE
) {
127 panic("Cannot send_auto_complete!\n");
130 ret
= nl_wait_for_ack(state
->nl_sock
);
132 if (ret
== -ENFILE
) {
137 panic("Waiting for netlink ack failed!\n");
144 panic("nla put failure!\n");
145 return -EIO
; /* dummy */
148 static int nl80211_del_mon_if(struct nl80211_state
*state
, const char *device
,
149 const char *mondevice
)
154 ifindex
= device_ifindex(mondevice
);
156 msg
= nl80211_nlmsg_xalloc();
158 genlmsg_put(msg
, 0, 0, genl_family_get_id(state
->nl80211
), 0,
159 0, NL80211_CMD_DEL_INTERFACE
, 0);
161 NLA_PUT_U32(msg
, NL80211_ATTR_IFINDEX
, ifindex
);
163 ret
= nl_send_auto_complete(state
->nl_sock
, msg
);
165 panic("Cannot send_auto_complete!\n");
167 ret
= nl_wait_for_ack(state
->nl_sock
);
169 panic("Waiting for netlink ack failed!\n");
175 panic("nla put failure!\n");
176 return -EIO
; /* dummy */
179 void enter_rfmon_mac80211(const char *device
, char **mondev
)
184 char phydev_path
[256];
185 struct nl80211_state nlstate
;
187 /* XXX: is this already a monN device? */
188 get_mac80211_phydev(device
, phydev_path
, sizeof(phydev_path
));
189 nl80211_init(&nlstate
, device
);
191 for (n
= 0; n
< UINT_MAX
; n
++) {
194 slprintf(mondevice
, sizeof(mondevice
), "mon%u", n
);
195 ret
= nl80211_add_mon_if(&nlstate
, device
, mondevice
);
197 *mondev
= xstrdup(mondevice
);
199 flags
= device_get_flags(*mondev
);
200 flags
|= IFF_UP
| IFF_RUNNING
;
201 device_set_flags(*mondev
, flags
);
203 nl80211_cleanup(&nlstate
);
208 panic("No free monN interfaces!\n");
211 void leave_rfmon_mac80211(const char *device
, const char *mondev
)
214 struct nl80211_state nlstate
;
216 flags
= device_get_flags(mondev
);
217 flags
&= ~(IFF_UP
| IFF_RUNNING
);
218 device_set_flags(mondev
, flags
);
220 nl80211_init(&nlstate
, device
);
221 nl80211_del_mon_if(&nlstate
, device
, mondev
);
222 nl80211_cleanup(&nlstate
);