xref: /openbmc/google-misc/ncsid/src/net_iface.h (revision a9a98258)
11285115cSWilliam A. Kennington III /*
21285115cSWilliam A. Kennington III  * Copyright 2021 Google LLC
31285115cSWilliam A. Kennington III  *
41285115cSWilliam A. Kennington III  * Licensed under the Apache License, Version 2.0 (the "License");
51285115cSWilliam A. Kennington III  * you may not use this file except in compliance with the License.
61285115cSWilliam A. Kennington III  * You may obtain a copy of the License at
71285115cSWilliam A. Kennington III  *
81285115cSWilliam A. Kennington III  *      http://www.apache.org/licenses/LICENSE-2.0
91285115cSWilliam A. Kennington III  *
101285115cSWilliam A. Kennington III  * Unless required by applicable law or agreed to in writing, software
111285115cSWilliam A. Kennington III  * distributed under the License is distributed on an "AS IS" BASIS,
121285115cSWilliam A. Kennington III  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
131285115cSWilliam A. Kennington III  * See the License for the specific language governing permissions and
141285115cSWilliam A. Kennington III  * limitations under the License.
151285115cSWilliam A. Kennington III  */
161285115cSWilliam A. Kennington III 
171285115cSWilliam A. Kennington III #pragma once
181285115cSWilliam A. Kennington III 
191285115cSWilliam A. Kennington III #include <linux/if.h>
201285115cSWilliam A. Kennington III #include <linux/if_packet.h>
211285115cSWilliam A. Kennington III #include <sys/socket.h>
221285115cSWilliam A. Kennington III 
231285115cSWilliam A. Kennington III #include <string>
241285115cSWilliam A. Kennington III 
251285115cSWilliam A. Kennington III namespace net
261285115cSWilliam A. Kennington III {
271285115cSWilliam A. Kennington III 
281285115cSWilliam A. Kennington III class IFaceBase
291285115cSWilliam A. Kennington III {
301285115cSWilliam A. Kennington III   public:
311285115cSWilliam A. Kennington III     explicit IFaceBase(const std::string& name);
321285115cSWilliam A. Kennington III     virtual ~IFaceBase() = default;
331285115cSWilliam A. Kennington III 
341285115cSWilliam A. Kennington III     /** @brief Get the index of the network interface corresponding
351285115cSWilliam A. Kennington III      * to this object.
361285115cSWilliam A. Kennington III      */
371285115cSWilliam A. Kennington III     int get_index() const;
381285115cSWilliam A. Kennington III 
391285115cSWilliam A. Kennington III     /** @brief Set interface flags using provided socket.
401285115cSWilliam A. Kennington III      *
411285115cSWilliam A. Kennington III      *  @param[in] sockfd - Socket to use for SIOCSIFFLAGS ioctl call.
421285115cSWilliam A. Kennington III      *  @param[in] flags - Flags to set.
431285115cSWilliam A. Kennington III      */
441285115cSWilliam A. Kennington III     int set_sock_flags(int sockfd, short flags) const;
451285115cSWilliam A. Kennington III 
461285115cSWilliam A. Kennington III     /** @brief Clear interface flags using provided socket.
471285115cSWilliam A. Kennington III      *
481285115cSWilliam A. Kennington III      *  @param[in] sockfd - Socket to use for SIOCSIFFLAGS/SIOCGIFFLAGS
491285115cSWilliam A. Kennington III      *      ioctl call.
501285115cSWilliam A. Kennington III      *  @param[in] flags - Flags to clear.
511285115cSWilliam A. Kennington III      */
521285115cSWilliam A. Kennington III     int clear_sock_flags(int sockfd, short flags) const;
531285115cSWilliam A. Kennington III 
541285115cSWilliam A. Kennington III     /** @brief Bind given socket to this interface. Similar to bind
551285115cSWilliam A. Kennington III      *     syscall, except that it fills in sll_ifindex field
561285115cSWilliam A. Kennington III      *     of struct sockaddr_ll with the index of this interface.
571285115cSWilliam A. Kennington III      */
58*a9a98258SWilly Tu     virtual int bind_sock(int sockfd) const = 0;
591285115cSWilliam A. Kennington III 
601285115cSWilliam A. Kennington III   protected:
611285115cSWilliam A. Kennington III     std::string name_;
621285115cSWilliam A. Kennington III 
631285115cSWilliam A. Kennington III   private:
641285115cSWilliam A. Kennington III     /** @brief Similar to ioctl syscall, but the socket is created inside
651285115cSWilliam A. Kennington III      *      the function and the interface name in struct ifreq is
661285115cSWilliam A. Kennington III      *      properly populated with the index of this interface.
671285115cSWilliam A. Kennington III      */
681285115cSWilliam A. Kennington III     virtual int ioctl(int request, struct ifreq* ifr) const = 0;
691285115cSWilliam A. Kennington III 
701285115cSWilliam A. Kennington III     /** @brief Similar to ioctl syscall. The interface index in
711285115cSWilliam A. Kennington III      *      struct ifreq is
721285115cSWilliam A. Kennington III      *      properly populated with the index of this interface.
731285115cSWilliam A. Kennington III      */
741285115cSWilliam A. Kennington III     virtual int ioctl_sock(int sockfd, int request,
751285115cSWilliam A. Kennington III                            struct ifreq* ifr) const = 0;
761285115cSWilliam A. Kennington III 
771285115cSWilliam A. Kennington III     /** @brief Modify interface flags, using the given socket for
781285115cSWilliam A. Kennington III      *      ioctl call.
791285115cSWilliam A. Kennington III      */
801285115cSWilliam A. Kennington III     int mod_sock_flags(int sockfd, short flags, bool set) const;
811285115cSWilliam A. Kennington III };
821285115cSWilliam A. Kennington III 
831285115cSWilliam A. Kennington III class IFace : public IFaceBase
841285115cSWilliam A. Kennington III {
851285115cSWilliam A. Kennington III   public:
IFace(const std::string & name)862be45238SPatrick Williams     explicit IFace(const std::string& name) : IFaceBase(name) {}
871285115cSWilliam A. Kennington III 
881285115cSWilliam A. Kennington III     /** @brief Bind given socket to this interface. Similar to bind
891285115cSWilliam A. Kennington III      *     syscall, except that it fills in sll_ifindex field
901285115cSWilliam A. Kennington III      *     of struct sockaddr_ll with the index of this interface.
911285115cSWilliam A. Kennington III      */
92*a9a98258SWilly Tu     int bind_sock(int sockfd) const override;
931285115cSWilliam A. Kennington III 
941285115cSWilliam A. Kennington III   private:
951285115cSWilliam A. Kennington III     /** @brief Similar to ioctl syscall, but the socket is created inside
961285115cSWilliam A. Kennington III      *      the function and the interface name in struct ifreq is
971285115cSWilliam A. Kennington III      *      properly populated with the index of this interface.
981285115cSWilliam A. Kennington III      */
991285115cSWilliam A. Kennington III     int ioctl(int request, struct ifreq* ifr) const override;
1001285115cSWilliam A. Kennington III     /** @brief Similar to ioctl syscall. The interface index in
1011285115cSWilliam A. Kennington III      *      struct ifreq is
1021285115cSWilliam A. Kennington III      *      properly populated with the index of this interface.
1031285115cSWilliam A. Kennington III      */
1041285115cSWilliam A. Kennington III     int ioctl_sock(int sockfd, int request, struct ifreq* ifr) const override;
1051285115cSWilliam A. Kennington III };
1061285115cSWilliam A. Kennington III 
1071285115cSWilliam A. Kennington III } // namespace net
108