2 * hostapd / VLAN netlink api
3 * Copyright (c) 2012, Michael Braun <michael-dev@fami-braun.de>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 #include "utils/includes.h"
10 #include <sys/ioctl.h>
11 #include <linux/sockios.h>
12 #include <linux/if_vlan.h>
13 #include <netlink/genl/genl.h>
14 #include <netlink/genl/family.h>
15 #include <netlink/genl/ctrl.h>
16 #include <netlink/route/link.h>
17 #include <netlink/route/link/vlan.h>
19 #include "utils/common.h"
20 #include "utils/eloop.h"
22 #include "vlan_util.h"
25 * Add a vlan interface with name 'vlan_if_name', VLAN ID 'vid' and
26 * tagged interface 'if_name'.
29 * returns 1 if the interface already exists
32 int vlan_add(const char *if_name, int vid, const char *vlan_if_name)
35 struct nl_sock *handle = NULL;
36 struct nl_cache *cache = NULL;
37 struct rtnl_link *rlink = NULL;
40 wpa_printf(MSG_DEBUG, "VLAN: vlan_add(if_name=%s, vid=%d, "
41 "vlan_if_name=%s)", if_name, vid, vlan_if_name);
43 if ((os_strlen(if_name) + 1) > IFNAMSIZ) {
44 wpa_printf(MSG_ERROR, "VLAN: Interface name too long: '%s'",
49 if ((os_strlen(vlan_if_name) + 1) > IFNAMSIZ) {
50 wpa_printf(MSG_ERROR, "VLAN: Interface name too long: '%s'",
55 handle = nl_socket_alloc();
57 wpa_printf(MSG_ERROR, "VLAN: failed to open netlink socket");
61 err = nl_connect(handle, NETLINK_ROUTE);
63 wpa_printf(MSG_ERROR, "VLAN: failed to connect to netlink: %s",
68 err = rtnl_link_alloc_cache(handle, AF_UNSPEC, &cache);
71 wpa_printf(MSG_ERROR, "VLAN: failed to alloc cache: %s",
76 if (!(if_idx = rtnl_link_name2i(cache, if_name))) {
77 /* link does not exist */
78 wpa_printf(MSG_ERROR, "VLAN: interface %s does not exist",
83 if ((rlink = rtnl_link_get_by_name(cache, vlan_if_name))) {
87 wpa_printf(MSG_ERROR, "VLAN: interface %s already exists",
93 rlink = rtnl_link_alloc();
95 wpa_printf(MSG_ERROR, "VLAN: failed to allocate new link");
99 err = rtnl_link_set_type(rlink, "vlan");
101 wpa_printf(MSG_ERROR, "VLAN: failed to set link type: %s",
106 rtnl_link_set_link(rlink, if_idx);
107 rtnl_link_set_name(rlink, vlan_if_name);
109 err = rtnl_link_vlan_set_id(rlink, vid);
111 wpa_printf(MSG_ERROR, "VLAN: failed to set link vlan id: %s",
116 err = rtnl_link_add(handle, rlink, NLM_F_CREATE);
118 wpa_printf(MSG_ERROR, "VLAN: failed to create link %s for "
119 "vlan %d on %s (%d): %s",
120 vlan_if_name, vid, if_name, if_idx,
129 rtnl_link_put(rlink);
131 nl_cache_free(cache);
133 nl_socket_free(handle);
138 int vlan_rem(const char *if_name)
141 struct nl_sock *handle = NULL;
142 struct nl_cache *cache = NULL;
143 struct rtnl_link *rlink = NULL;
145 wpa_printf(MSG_DEBUG, "VLAN: vlan_rem(if_name=%s)", if_name);
147 handle = nl_socket_alloc();
149 wpa_printf(MSG_ERROR, "VLAN: failed to open netlink socket");
153 err = nl_connect(handle, NETLINK_ROUTE);
155 wpa_printf(MSG_ERROR, "VLAN: failed to connect to netlink: %s",
160 err = rtnl_link_alloc_cache(handle, AF_UNSPEC, &cache);
163 wpa_printf(MSG_ERROR, "VLAN: failed to alloc cache: %s",
168 if (!(rlink = rtnl_link_get_by_name(cache, if_name))) {
169 /* link does not exist */
170 wpa_printf(MSG_ERROR, "VLAN: interface %s does not exists",
175 err = rtnl_link_delete(handle, rlink);
177 wpa_printf(MSG_ERROR, "VLAN: failed to remove link %s: %s",
178 if_name, nl_geterror(err));
186 rtnl_link_put(rlink);
188 nl_cache_free(cache);
190 nl_socket_free(handle);