github.com/datadog/cilium@v1.6.12/bpf/lib/csum.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  
    19  #ifndef __LIB_CSUM_H_
    20  #define __LIB_CSUM_H_
    21  
    22  #include <linux/tcp.h>
    23  #include <linux/udp.h>
    24  #include <linux/icmpv6.h>
    25  
    26  #define TCP_CSUM_OFF (offsetof(struct tcphdr, check))
    27  #define UDP_CSUM_OFF (offsetof(struct udphdr, check))
    28  
    29  struct csum_offset
    30  {
    31  	__u16 offset;
    32  	__u16 flags;
    33  };
    34  
    35  /**
    36   * Determins the L4 checksum field offset and required flags
    37   * @arg nexthdr	L3 nextheader field
    38   * @arg off	Pointer to uninitialied struct csum_offset struct
    39   *
    40   * Sets off.offset to offset from start of L4 header to L4 checksum field
    41   * and off.flags to the required flags, namely BPF_F_MARK_MANGLED_0 for UDP.
    42   * For unknown L4 protocols or L4 protocols which do not have a checksum
    43   * field, off is initialied to 0.
    44   */
    45  static inline void csum_l4_offset_and_flags(__u8 nexthdr, struct csum_offset *off)
    46  {
    47  	switch (nexthdr) {
    48  	case IPPROTO_TCP:
    49  		off->offset = TCP_CSUM_OFF;
    50  		break;
    51  
    52  	case IPPROTO_UDP:
    53  		off->offset = UDP_CSUM_OFF;
    54  		off->flags = BPF_F_MARK_MANGLED_0;
    55  		break;
    56  
    57  	case IPPROTO_ICMPV6:
    58  		off->offset = offsetof(struct icmp6hdr, icmp6_cksum);
    59  		break;
    60  
    61  	case IPPROTO_ICMP:
    62  		break;
    63  	}
    64  }
    65  
    66  /**
    67   * Helper to change L4 checksum
    68   * @arg skb	Packet
    69   * @arg l4_off	Offset to L4 header
    70   * @arg csum	Pointer to csum_offset as extracted by csum_l4_offset_and_flags()
    71   * @arg from	From value or 0 if to contains csum diff
    72   * @arg to	To value or a csum diff
    73   * @arg flags	Additional flags to be passed to l4_csum_replace()
    74   */
    75  static inline int csum_l4_replace(struct __sk_buff *skb, int l4_off, struct csum_offset *csum,
    76  				  __be32 from, __be32 to, int flags)
    77  {
    78  	return l4_csum_replace(skb, l4_off + csum->offset, from, to, flags | csum->flags);
    79  }
    80  
    81  #endif /* __LB_H_ */