blob: 82ef0d9239b5c7eb4c826b62574e527f1c1c552a [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
19#include "NetlinkRequest.h"
20#include "NetlinkSocket.h"
21#include "common.h"
22
23#include <android-base/logging.h>
24
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
34 NetlinkRequest<struct ifinfomsg> req(RTM_NEWLINK, NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL);
35 req.addattr(IFLA_IFNAME, vlan);
36 req.addattr<uint32_t>(IFLA_LINK, ethidx);
37
38 {
39 auto linkinfo = req.nest(IFLA_LINKINFO);
40 req.addattr(IFLA_INFO_KIND, "vlan");
41
42 {
43 auto linkinfo = req.nest(IFLA_INFO_DATA);
44 req.addattr(IFLA_VLAN_ID, id);
45 }
46 }
47
48 NetlinkSocket sock(NETLINK_ROUTE);
49 return sock.send(req) && sock.receiveAck();
50}
51
52} // namespace android::netdevice::vlan