blob: bcc9345e69d0322bce329e5b03133f42f5bc9d1c [file] [log] [blame]
Tomasz Wasilczykad107412020-05-29 16:32:43 -07001/*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <libnetdevice/vlan.h>
18
Tomasz Wasilczykad107412020-05-29 16:32:43 -070019#include "common.h"
20
21#include <android-base/logging.h>
Tomasz Wasilczyk16954592020-07-28 09:42:55 -070022#include <libnl++/NetlinkRequest.h>
23#include <libnl++/NetlinkSocket.h>
Tomasz Wasilczykad107412020-05-29 16:32:43 -070024
25namespace android::netdevice::vlan {
26
27bool add(const std::string& eth, const std::string& vlan, uint16_t id) {
28 const auto ethidx = nametoindex(eth);
29 if (ethidx == 0) {
30 LOG(ERROR) << "Ethernet interface " << eth << " doesn't exist";
31 return false;
32 }
33
Tomasz Wasilczyk1d5beb82020-07-29 09:37:44 -070034 nl::NetlinkRequest<struct ifinfomsg> req(RTM_NEWLINK,
35 NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL);
Tomasz Wasilczykad107412020-05-29 16:32:43 -070036 req.addattr(IFLA_IFNAME, vlan);
37 req.addattr<uint32_t>(IFLA_LINK, ethidx);
38
39 {
40 auto linkinfo = req.nest(IFLA_LINKINFO);
41 req.addattr(IFLA_INFO_KIND, "vlan");
42
43 {
44 auto linkinfo = req.nest(IFLA_INFO_DATA);
45 req.addattr(IFLA_VLAN_ID, id);
46 }
47 }
48
Tomasz Wasilczyk1d5beb82020-07-29 09:37:44 -070049 nl::NetlinkSocket sock(NETLINK_ROUTE);
Tomasz Wasilczykad107412020-05-29 16:32:43 -070050 return sock.send(req) && sock.receiveAck();
51}
52
53} // namespace android::netdevice::vlan