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 if (nl_connect(handle, NETLINK_ROUTE) < 0) {
62 wpa_printf(MSG_ERROR, "VLAN: failed to connect to netlink");
66 if (rtnl_link_alloc_cache(handle, AF_UNSPEC, &cache) < 0) {
68 wpa_printf(MSG_ERROR, "VLAN: failed to alloc cache");
72 if (!(if_idx = rtnl_link_name2i(cache, if_name))) {
73 /* link does not exist */
74 wpa_printf(MSG_ERROR, "VLAN: interface %s does not exist",
79 if ((rlink = rtnl_link_get_by_name(cache, vlan_if_name))) {
83 wpa_printf(MSG_ERROR, "VLAN: interface %s already exists",
89 rlink = rtnl_link_alloc();
91 wpa_printf(MSG_ERROR, "VLAN: failed to allocate new link");
95 if (rtnl_link_set_type(rlink, "vlan") < 0) {
96 wpa_printf(MSG_ERROR, "VLAN: failed to set link type");
100 rtnl_link_set_link(rlink, if_idx);
101 rtnl_link_set_name(rlink, vlan_if_name);
103 if (rtnl_link_vlan_set_id(rlink, vid) < 0) {
104 wpa_printf(MSG_ERROR, "VLAN: failed to set link vlan id");
108 if (rtnl_link_add(handle, rlink, NLM_F_CREATE) < 0) {
109 wpa_printf(MSG_ERROR, "VLAN: failed to create link %s for "
110 "vlan %d on %s (%d)",
111 vlan_if_name, vid, if_name, if_idx);
119 rtnl_link_put(rlink);
121 nl_cache_free(cache);
123 nl_socket_free(handle);
128 int vlan_rem(const char *if_name)
131 struct nl_sock *handle = NULL;
132 struct nl_cache *cache = NULL;
133 struct rtnl_link *rlink = NULL;
135 wpa_printf(MSG_DEBUG, "VLAN: vlan_rem(if_name=%s)", if_name);
137 handle = nl_socket_alloc();
139 wpa_printf(MSG_ERROR, "VLAN: failed to open netlink socket");
143 if (nl_connect(handle, NETLINK_ROUTE) < 0) {
144 wpa_printf(MSG_ERROR, "VLAN: failed to connect to netlink");
148 if (rtnl_link_alloc_cache(handle, AF_UNSPEC, &cache) < 0) {
150 wpa_printf(MSG_ERROR, "VLAN: failed to alloc cache");
154 if (!(rlink = rtnl_link_get_by_name(cache, if_name))) {
155 /* link does not exist */
156 wpa_printf(MSG_ERROR, "VLAN: interface %s does not exists",
161 if (rtnl_link_delete(handle, rlink) < 0) {
162 wpa_printf(MSG_ERROR, "VLAN: failed to remove link %s",
171 rtnl_link_put(rlink);
173 nl_cache_free(cache);
175 nl_socket_free(handle);