github.com/datadog/cilium@v1.6.12/bpf/lib/eth.h (about)

     1  /*
     2   *  Copyright (C) 2016-2017 Authors of Cilium
     3   *
     4   *  This program is free software; you can redistribute it and/or modify
     5   *  it under the terms of the GNU General Public License as published by
     6   *  the Free Software Foundation; either version 2 of the License, or
     7   *  (at your option) any later version.
     8   *
     9   *  This program is distributed in the hope that it will be useful,
    10   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   *  GNU General Public License for more details.
    13   *
    14   *  You should have received a copy of the GNU General Public License
    15   *  along with this program; if not, write to the Free Software
    16   *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    17   */
    18  #ifndef __LIB_ETH__
    19  #define __LIB_ETH__
    20  
    21  #include <linux/if_ether.h>
    22  
    23  #ifndef ETH_HLEN
    24  #define ETH_HLEN 14
    25  #endif
    26  
    27  #ifndef ETH_ALEN
    28  #define ETH_ALEN 6
    29  #endif
    30  
    31  union macaddr {
    32  	struct {
    33  		__u32 p1;
    34  		__u16 p2;
    35  	};
    36  	__u8 addr[6];
    37  };
    38  
    39  static inline int eth_addrcmp(const union macaddr *a, const union macaddr *b)
    40  {
    41  	int tmp;
    42  
    43  	tmp = a->p1 - b->p1;
    44  	if (!tmp)
    45  		tmp = a->p2 - b->p2;
    46  
    47  	return tmp;
    48  }
    49  
    50  static inline int eth_is_bcast(const union macaddr *a)
    51  {
    52  	union macaddr bcast;
    53  
    54  	bcast.p1 = 0xffffffff;
    55  	bcast.p2 = 0xffff;
    56  
    57  	if (!eth_addrcmp(a, &bcast))
    58  		return 1;
    59  	else
    60  		return 0;
    61  }
    62  
    63  static inline int eth_load_saddr(struct __sk_buff *skb, __u8 *mac, int off)
    64  {
    65  	return skb_load_bytes(skb, off + ETH_ALEN, mac, ETH_ALEN);
    66  }
    67  
    68  static inline int eth_store_saddr(struct __sk_buff *skb, __u8 *mac, int off)
    69  {
    70  	return skb_store_bytes(skb, off + ETH_ALEN, mac, ETH_ALEN, 0);
    71  }
    72  
    73  static inline int eth_load_daddr(struct __sk_buff *skb, __u8 *mac, int off)
    74  {
    75  	return skb_load_bytes(skb, off, mac, ETH_ALEN);
    76  }
    77  
    78  static inline int eth_store_daddr(struct __sk_buff *skb, __u8 *mac, int off)
    79  {
    80  	return skb_store_bytes(skb, off, mac, ETH_ALEN, 0);
    81  }
    82  
    83  #endif /* __LIB_ETH__ */