Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef __NET_GUE_H
0003 #define __NET_GUE_H
0004 
0005 /* Definitions for the GUE header, standard and private flags, lengths
0006  * of optional fields are below.
0007  *
0008  * Diagram of GUE header:
0009  *
0010  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
0011  * |Ver|C|  Hlen   | Proto/ctype   |        Standard flags       |P|
0012  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
0013  * |                                                               |
0014  * ~                      Fields (optional)                        ~
0015  * |                                                               |
0016  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
0017  * |            Private flags (optional, P bit is set)             |
0018  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
0019  * |                                                               |
0020  * ~                   Private fields (optional)                   ~
0021  * |                                                               |
0022  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
0023  *
0024  * C bit indicates control message when set, data message when unset.
0025  * For a control message, proto/ctype is interpreted as a type of
0026  * control message. For data messages, proto/ctype is the IP protocol
0027  * of the next header.
0028  *
0029  * P bit indicates private flags field is present. The private flags
0030  * may refer to options placed after this field.
0031  */
0032 
0033 #include <asm/byteorder.h>
0034 #include <linux/types.h>
0035 
0036 struct guehdr {
0037     union {
0038         struct {
0039 #if defined(__LITTLE_ENDIAN_BITFIELD)
0040             __u8    hlen:5,
0041                 control:1,
0042                 version:2;
0043 #elif defined (__BIG_ENDIAN_BITFIELD)
0044             __u8    version:2,
0045                 control:1,
0046                 hlen:5;
0047 #else
0048 #error  "Please fix <asm/byteorder.h>"
0049 #endif
0050             __u8    proto_ctype;
0051             __be16  flags;
0052         };
0053         __be32  word;
0054     };
0055 };
0056 
0057 /* Standard flags in GUE header */
0058 
0059 #define GUE_FLAG_PRIV   htons(1<<0) /* Private flags are in options */
0060 #define GUE_LEN_PRIV    4
0061 
0062 #define GUE_FLAGS_ALL   (GUE_FLAG_PRIV)
0063 
0064 /* Private flags in the private option extension */
0065 
0066 #define GUE_PFLAG_REMCSUM   htonl(1U << 31)
0067 #define GUE_PLEN_REMCSUM    4
0068 
0069 #define GUE_PFLAGS_ALL  (GUE_PFLAG_REMCSUM)
0070 
0071 /* Functions to compute options length corresponding to flags.
0072  * If we ever have a lot of flags this can be potentially be
0073  * converted to a more optimized algorithm (table lookup
0074  * for instance).
0075  */
0076 static inline size_t guehdr_flags_len(__be16 flags)
0077 {
0078     return ((flags & GUE_FLAG_PRIV) ? GUE_LEN_PRIV : 0);
0079 }
0080 
0081 static inline size_t guehdr_priv_flags_len(__be32 flags)
0082 {
0083     return 0;
0084 }
0085 
0086 /* Validate standard and private flags. Returns non-zero (meaning invalid)
0087  * if there is an unknown standard or private flags, or the options length for
0088  * the flags exceeds the options length specific in hlen of the GUE header.
0089  */
0090 static inline int validate_gue_flags(struct guehdr *guehdr, size_t optlen)
0091 {
0092     __be16 flags = guehdr->flags;
0093     size_t len;
0094 
0095     if (flags & ~GUE_FLAGS_ALL)
0096         return 1;
0097 
0098     len = guehdr_flags_len(flags);
0099     if (len > optlen)
0100         return 1;
0101 
0102     if (flags & GUE_FLAG_PRIV) {
0103         /* Private flags are last four bytes accounted in
0104          * guehdr_flags_len
0105          */
0106         __be32 pflags = *(__be32 *)((void *)&guehdr[1] +
0107                         len - GUE_LEN_PRIV);
0108 
0109         if (pflags & ~GUE_PFLAGS_ALL)
0110             return 1;
0111 
0112         len += guehdr_priv_flags_len(pflags);
0113         if (len > optlen)
0114             return 1;
0115     }
0116 
0117     return 0;
0118 }
0119 
0120 #endif