Tomasz Wasilczyk | ad10741 | 2020-05-29 16:32:43 -0700 | [diff] [blame] | 1 | /* |
| 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 Wasilczyk | ad10741 | 2020-05-29 16:32:43 -0700 | [diff] [blame] | 19 | #include "common.h" |
| 20 | |
| 21 | #include <android-base/logging.h> |
Tomasz Wasilczyk | 66fc939 | 2020-08-03 10:22:52 -0700 | [diff] [blame] | 22 | #include <libnl++/MessageFactory.h> |
| 23 | #include <libnl++/Socket.h> |
Tomasz Wasilczyk | ad10741 | 2020-05-29 16:32:43 -0700 | [diff] [blame] | 24 | |
Tomasz Wasilczyk | 390c711 | 2020-07-30 11:08:29 -0700 | [diff] [blame] | 25 | #include <linux/rtnetlink.h> |
| 26 | |
Tomasz Wasilczyk | ad10741 | 2020-05-29 16:32:43 -0700 | [diff] [blame] | 27 | namespace android::netdevice::vlan { |
| 28 | |
Tomasz Wasilczyk | f2dcb64 | 2024-10-10 12:13:29 -0700 | [diff] [blame] | 29 | bool add(std::string_view eth, std::string_view vlan, uint16_t id) { |
Tomasz Wasilczyk | ad10741 | 2020-05-29 16:32:43 -0700 | [diff] [blame] | 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 Wasilczyk | 86a0ff9 | 2024-10-10 22:44:55 -0700 | [diff] [blame^] | 36 | nl::MessageFactory<ifinfomsg> req(RTM_NEWLINK, nl::kCreateFlags); |
Tomasz Wasilczyk | 3ab105b | 2020-08-05 14:07:40 -0700 | [diff] [blame] | 37 | req.add(IFLA_IFNAME, vlan); |
| 38 | req.add<uint32_t>(IFLA_LINK, ethidx); |
Tomasz Wasilczyk | ad10741 | 2020-05-29 16:32:43 -0700 | [diff] [blame] | 39 | |
| 40 | { |
Tomasz Wasilczyk | 3ab105b | 2020-08-05 14:07:40 -0700 | [diff] [blame] | 41 | auto linkinfo = req.addNested(IFLA_LINKINFO); |
Keith Mok | 6355aa9 | 2022-07-12 21:03:18 +0000 | [diff] [blame] | 42 | req.addBuffer(IFLA_INFO_KIND, "vlan"); |
Tomasz Wasilczyk | ad10741 | 2020-05-29 16:32:43 -0700 | [diff] [blame] | 43 | |
| 44 | { |
Tomasz Wasilczyk | 3ab105b | 2020-08-05 14:07:40 -0700 | [diff] [blame] | 45 | auto linkinfo = req.addNested(IFLA_INFO_DATA); |
| 46 | req.add(IFLA_VLAN_ID, id); |
Tomasz Wasilczyk | ad10741 | 2020-05-29 16:32:43 -0700 | [diff] [blame] | 47 | } |
| 48 | } |
| 49 | |
Tomasz Wasilczyk | 66fc939 | 2020-08-03 10:22:52 -0700 | [diff] [blame] | 50 | nl::Socket sock(NETLINK_ROUTE); |
Tomasz Wasilczyk | 7139605 | 2020-08-04 16:21:39 -0700 | [diff] [blame] | 51 | return sock.send(req) && sock.receiveAck(req); |
Tomasz Wasilczyk | ad10741 | 2020-05-29 16:32:43 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | } // namespace android::netdevice::vlan |