github.com/datadog/cilium@v1.6.12/bpf/lib/ipv4.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_IPV4__
    19  #define __LIB_IPV4__
    20  
    21  #include <linux/ip.h>
    22  
    23  #include "dbg.h"
    24  
    25  static inline int ipv4_load_daddr(struct __sk_buff *skb, int off, __u32 *dst)
    26  {
    27  	return skb_load_bytes(skb, off + offsetof(struct iphdr, daddr), dst, 4);
    28  }
    29  
    30  static inline int ipv4_dec_ttl(struct __sk_buff *skb, int off, struct iphdr *ip4)
    31  {
    32  	__u8 new_ttl, ttl = ip4->ttl;
    33  
    34  	if (ttl <= 1)
    35  		return 1;
    36  
    37  	new_ttl = ttl - 1;
    38  	/* l3_csum_replace() takes at min 2 bytes, zero extended. */
    39  	l3_csum_replace(skb, off + offsetof(struct iphdr, check), ttl, new_ttl, 2);
    40  	skb_store_bytes(skb, off + offsetof(struct iphdr, ttl), &new_ttl, sizeof(new_ttl), 0);
    41  
    42  	return 0;
    43  }
    44  
    45  static inline int ipv4_hdrlen(struct iphdr *ip4)
    46  {
    47  	return ip4->ihl * 4;
    48  }
    49  
    50  static inline bool ipv4_is_fragment(struct iphdr *ip4)
    51  {
    52  	// The frag_off portion of the header consists of:
    53  	//
    54  	// +----+----+----+----------------------------------+
    55  	// | RS | DF | MF | ...13 bits of fragment offset... |
    56  	// +----+----+----+----------------------------------+
    57  	//
    58  	// If "More fragments" or the offset is nonzero, then this is an IP
    59  	// fragment. The evil bit must be set to 0 (RFC791, RFC3514).
    60  	return ip4->frag_off & bpf_htons(0xBFFF);
    61  }
    62  
    63  #endif /* __LIB_IPV4__ */