blob: 3e07f670df55d0d01539212359eab9536beb85a2 [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 Wasilczyk66fc9392020-08-03 10:22:52 -070022#include <libnl++/MessageFactory.h>
23#include <libnl++/Socket.h>
Tomasz Wasilczykad107412020-05-29 16:32:43 -070024
Tomasz Wasilczyk390c7112020-07-30 11:08:29 -070025#include <linux/rtnetlink.h>
26
Tomasz Wasilczykad107412020-05-29 16:32:43 -070027namespace android::netdevice::vlan {
28
29bool add(const std::string& eth, const std::string& vlan, uint16_t id) {
30 const auto ethidx = nametoindex(eth);
31 if (ethidx == 0) {
32 LOG(ERROR) << "Ethernet interface " << eth << " doesn't exist";
33 return false;
34 }
35
Tomasz Wasilczyk66fc9392020-08-03 10:22:52 -070036 nl::MessageFactory<struct ifinfomsg> req(RTM_NEWLINK,
Tomasz Wasilczyk1d5beb82020-07-29 09:37:44 -070037 NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL);
Tomasz Wasilczykad107412020-05-29 16:32:43 -070038 req.addattr(IFLA_IFNAME, vlan);
39 req.addattr<uint32_t>(IFLA_LINK, ethidx);
40
41 {
42 auto linkinfo = req.nest(IFLA_LINKINFO);
43 req.addattr(IFLA_INFO_KIND, "vlan");
44
45 {
46 auto linkinfo = req.nest(IFLA_INFO_DATA);
47 req.addattr(IFLA_VLAN_ID, id);
48 }
49 }
50
Tomasz Wasilczyk66fc9392020-08-03 10:22:52 -070051 nl::Socket sock(NETLINK_ROUTE);
Tomasz Wasilczykad107412020-05-29 16:32:43 -070052 return sock.send(req) && sock.receiveAck();
53}
54
55} // namespace android::netdevice::vlan