github.com/datadog/cilium@v1.6.12/bpf/lib/l4.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_L4_H_
    19  #define __LIB_L4_H_
    20  
    21  #include <linux/tcp.h>
    22  #include <linux/udp.h>
    23  #include "common.h"
    24  #include "dbg.h"
    25  #include "csum.h"
    26  
    27  #define TCP_DPORT_OFF (offsetof(struct tcphdr, dest))
    28  #define TCP_SPORT_OFF (offsetof(struct tcphdr, source))
    29  #define UDP_DPORT_OFF (offsetof(struct udphdr, dest))
    30  #define UDP_SPORT_OFF (offsetof(struct udphdr, source))
    31  
    32  
    33  /**
    34   * Modify L4 port and correct checksum
    35   * @arg skb:      packet
    36   * @arg l4_off:   offset to L4 header
    37   * @arg off:      offset from L4 header to source or destination port
    38   * @arg csum_off: offset from L4 header to 16bit checksum field in L4 header
    39   * @arg port:     new port value
    40   * @arg old_port: old port value (for checksum correction)
    41   *
    42   * Overwrites a TCP or UDP port with new value and fixes up the checksum
    43   * in the L4 header and of skb->csum.
    44   *
    45   * NOTE: Calling this function will invalidate any pkt context offset
    46   * validation for direct packet access.
    47   *
    48   * Return 0 on success or a negative DROP_* reason
    49   */
    50  static inline int l4_modify_port(struct __sk_buff *skb, int l4_off, int off,
    51  				 struct csum_offset *csum_off, __be16 port, __be16 old_port)
    52  {
    53  	if (csum_l4_replace(skb, l4_off, csum_off, old_port, port, sizeof(port)) < 0)
    54  		return DROP_CSUM_L4;
    55  
    56  	if (skb_store_bytes(skb, l4_off + off, &port, sizeof(port), 0) < 0)
    57  		return DROP_WRITE_ERROR;
    58  
    59  	return 0;
    60  }
    61  
    62  static inline int l4_load_port(struct __sk_buff *skb, int off, __be16 *port)
    63  {
    64          return skb_load_bytes(skb, off, port, sizeof(__be16));
    65  }
    66  #endif