LCOV - code coverage report
Current view: top level - lib - packets.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 153 162 94.4 %
Date: 2016-09-14 01:02:56 Functions: 46 49 93.9 %
Branches: 46 60 76.7 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Nicira, Inc.
       3                 :            :  *
       4                 :            :  * Licensed under the Apache License, Version 2.0 (the "License");
       5                 :            :  * you may not use this file except in compliance with the License.
       6                 :            :  * You may obtain a copy of the License at:
       7                 :            :  *
       8                 :            :  *     http://www.apache.org/licenses/LICENSE-2.0
       9                 :            :  *
      10                 :            :  * Unless required by applicable law or agreed to in writing, software
      11                 :            :  * distributed under the License is distributed on an "AS IS" BASIS,
      12                 :            :  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      13                 :            :  * See the License for the specific language governing permissions and
      14                 :            :  * limitations under the License.
      15                 :            :  */
      16                 :            : 
      17                 :            : #ifndef PACKETS_H
      18                 :            : #define PACKETS_H 1
      19                 :            : 
      20                 :            : #include <inttypes.h>
      21                 :            : #include <sys/types.h>
      22                 :            : #include <stdint.h>
      23                 :            : #include <string.h>
      24                 :            : #include "compiler.h"
      25                 :            : #include "openvswitch/geneve.h"
      26                 :            : #include "openvswitch/packets.h"
      27                 :            : #include "openvswitch/types.h"
      28                 :            : #include "odp-netlink.h"
      29                 :            : #include "random.h"
      30                 :            : #include "hash.h"
      31                 :            : #include "tun-metadata.h"
      32                 :            : #include "unaligned.h"
      33                 :            : #include "util.h"
      34                 :            : 
      35                 :            : struct dp_packet;
      36                 :            : struct ds;
      37                 :            : 
      38                 :            : /* Purely internal to OVS userspace. These flags should never be exposed to
      39                 :            :  * the outside world and so aren't included in the flags mask. */
      40                 :            : 
      41                 :            : /* Tunnel information is in userspace datapath format. */
      42                 :            : #define FLOW_TNL_F_UDPIF (1 << 4)
      43                 :            : 
      44                 :            : static inline bool ipv6_addr_is_set(const struct in6_addr *addr);
      45                 :            : 
      46                 :            : static inline bool
      47                 :     363533 : flow_tnl_dst_is_set(const struct flow_tnl *tnl)
      48                 :            : {
      49 [ +  + ][ +  + ]:     363533 :     return tnl->ip_dst || ipv6_addr_is_set(&tnl->ipv6_dst);
      50                 :            : }
      51                 :            : 
      52                 :            : struct in6_addr flow_tnl_dst(const struct flow_tnl *tnl);
      53                 :            : struct in6_addr flow_tnl_src(const struct flow_tnl *tnl);
      54                 :            : 
      55                 :            : /* Returns an offset to 'src' covering all the meaningful fields in 'src'. */
      56                 :            : static inline size_t
      57                 :      16561 : flow_tnl_size(const struct flow_tnl *src)
      58                 :            : {
      59         [ +  + ]:      16561 :     if (!flow_tnl_dst_is_set(src)) {
      60                 :            :         /* Covers ip_dst and ipv6_dst only. */
      61                 :      16514 :         return offsetof(struct flow_tnl, ip_src);
      62                 :            :     }
      63         [ +  + ]:         47 :     if (src->flags & FLOW_TNL_F_UDPIF) {
      64                 :            :         /* Datapath format, cover all options we have. */
      65                 :          5 :         return offsetof(struct flow_tnl, metadata.opts)
      66                 :          5 :             + src->metadata.present.len;
      67                 :            :     }
      68         [ +  - ]:         42 :     if (!src->metadata.present.map) {
      69                 :            :         /* No TLVs, opts is irrelevant. */
      70                 :         42 :         return offsetof(struct flow_tnl, metadata.opts);
      71                 :            :     }
      72                 :            :     /* Have decoded TLVs, opts is relevant. */
      73                 :          0 :     return sizeof *src;
      74                 :            : }
      75                 :            : 
      76                 :            : /* Copy flow_tnl, but avoid copying unused portions of tun_metadata.  Unused
      77                 :            :  * data in 'dst' is NOT cleared, so this must not be used in cases where the
      78                 :            :  * uninitialized portion may be hashed over. */
      79                 :            : static inline void
      80                 :      15151 : flow_tnl_copy__(struct flow_tnl *dst, const struct flow_tnl *src)
      81                 :            : {
      82                 :      15151 :     memcpy(dst, src, flow_tnl_size(src));
      83                 :      15151 : }
      84                 :            : 
      85                 :            : static inline bool
      86                 :        705 : flow_tnl_equal(const struct flow_tnl *a, const struct flow_tnl *b)
      87                 :            : {
      88                 :        705 :     size_t a_size = flow_tnl_size(a);
      89                 :            : 
      90 [ +  - ][ +  - ]:        705 :     return a_size == flow_tnl_size(b) && !memcmp(a, b, a_size);
      91                 :            : }
      92                 :            : 
      93                 :            : /* Datapath packet metadata */
      94                 :            : struct pkt_metadata {
      95                 :            :     uint32_t recirc_id;         /* Recirculation id carried with the
      96                 :            :                                    recirculating packets. 0 for packets
      97                 :            :                                    received from the wire. */
      98                 :            :     uint32_t dp_hash;           /* hash value computed by the recirculation
      99                 :            :                                    action. */
     100                 :            :     uint32_t skb_priority;      /* Packet priority for QoS. */
     101                 :            :     uint32_t pkt_mark;          /* Packet mark. */
     102                 :            :     uint16_t ct_state;          /* Connection state. */
     103                 :            :     uint16_t ct_zone;           /* Connection zone. */
     104                 :            :     uint32_t ct_mark;           /* Connection mark. */
     105                 :            :     ovs_u128 ct_label;          /* Connection label. */
     106                 :            :     union flow_in_port in_port; /* Input port. */
     107                 :            :     struct flow_tnl tunnel;     /* Encapsulating tunnel parameters. Note that
     108                 :            :                                  * if 'ip_dst' == 0, the rest of the fields may
     109                 :            :                                  * be uninitialized. */
     110                 :            : };
     111                 :            : 
     112                 :            : static inline void
     113                 :       1932 : pkt_metadata_init_tnl(struct pkt_metadata *md)
     114                 :            : {
     115                 :            :     /* Zero up through the tunnel metadata options. The length and table
     116                 :            :      * are before this and as long as they are empty, the options won't
     117                 :            :      * be looked at. */
     118                 :       1932 :     memset(md, 0, offsetof(struct pkt_metadata, tunnel.metadata.opts));
     119                 :       1932 : }
     120                 :            : 
     121                 :            : static inline void
     122                 :     115457 : pkt_metadata_init(struct pkt_metadata *md, odp_port_t port)
     123                 :            : {
     124                 :            :     /* It can be expensive to zero out all of the tunnel metadata. However,
     125                 :            :      * we can just zero out ip_dst and the rest of the data will never be
     126                 :            :      * looked at. */
     127                 :     115457 :     memset(md, 0, offsetof(struct pkt_metadata, in_port));
     128                 :     115457 :     md->tunnel.ip_dst = 0;
     129                 :     115457 :     md->tunnel.ipv6_dst = in6addr_any;
     130                 :            : 
     131                 :     115457 :     md->in_port.odp_port = port;
     132                 :     115457 : }
     133                 :            : 
     134                 :            : /* This function prefetches the cachelines touched by pkt_metadata_init()
     135                 :            :  * For performance reasons the two functions should be kept in sync. */
     136                 :            : static inline void
     137                 :          0 : pkt_metadata_prefetch_init(struct pkt_metadata *md)
     138                 :            : {
     139                 :          0 :     ovs_prefetch_range(md, offsetof(struct pkt_metadata, tunnel.ip_src));
     140                 :          0 : }
     141                 :            : 
     142                 :            : bool dpid_from_string(const char *s, uint64_t *dpidp);
     143                 :            : 
     144                 :            : #define ETH_ADDR_LEN           6
     145                 :            : 
     146                 :            : static const struct eth_addr eth_addr_broadcast OVS_UNUSED
     147                 :            :     = { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } } };
     148                 :            : 
     149                 :            : static const struct eth_addr eth_addr_exact OVS_UNUSED
     150                 :            :     = { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } } };
     151                 :            : 
     152                 :            : static const struct eth_addr eth_addr_zero OVS_UNUSED
     153                 :            :     = { { { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } };
     154                 :            : 
     155                 :            : static const struct eth_addr eth_addr_stp OVS_UNUSED
     156                 :            :     = { { { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x00 } } };
     157                 :            : 
     158                 :            : static const struct eth_addr eth_addr_lacp OVS_UNUSED
     159                 :            :     = { { { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x02 } } };
     160                 :            : 
     161                 :            : static const struct eth_addr eth_addr_bfd OVS_UNUSED
     162                 :            :     = { { { 0x00, 0x23, 0x20, 0x00, 0x00, 0x01 } } };
     163                 :            : 
     164                 :        304 : static inline bool eth_addr_is_broadcast(const struct eth_addr a)
     165                 :            : {
     166                 :        304 :     return (a.be16[0] & a.be16[1] & a.be16[2]) == htons(0xffff);
     167                 :            : }
     168                 :            : 
     169                 :      61280 : static inline bool eth_addr_is_multicast(const struct eth_addr a)
     170                 :            : {
     171                 :      61280 :     return a.ea[0] & 1;
     172                 :            : }
     173                 :            : 
     174                 :       5453 : static inline bool eth_addr_is_local(const struct eth_addr a)
     175                 :            : {
     176                 :            :     /* Local if it is either a locally administered address or a Nicira random
     177                 :            :      * address. */
     178                 :       5453 :     return a.ea[0] & 2
     179 [ -  + ][ #  # ]:       5453 :         || (a.be16[0] == htons(0x0023)
     180         [ #  # ]:          0 :             && (a.be16[1] & htons(0xff80)) == htons(0x2080));
     181                 :            : }
     182                 :    1044169 : static inline bool eth_addr_is_zero(const struct eth_addr a)
     183                 :            : {
     184                 :    1044169 :     return !(a.be16[0] | a.be16[1] | a.be16[2]);
     185                 :            : }
     186                 :            : 
     187                 :      28704 : static inline int eth_mask_is_exact(const struct eth_addr a)
     188                 :            : {
     189                 :      28704 :     return (a.be16[0] & a.be16[1] & a.be16[2]) == htons(0xffff);
     190                 :            : }
     191                 :            : 
     192                 :    1112104 : static inline int eth_addr_compare_3way(const struct eth_addr a,
     193                 :            :                                         const struct eth_addr b)
     194                 :            : {
     195                 :    1112104 :     return memcmp(&a, &b, sizeof a);
     196                 :            : }
     197                 :            : 
     198                 :    1112080 : static inline bool eth_addr_equals(const struct eth_addr a,
     199                 :            :                                    const struct eth_addr b)
     200                 :            : {
     201                 :    1112080 :     return !eth_addr_compare_3way(a, b);
     202                 :            : }
     203                 :            : 
     204                 :     656643 : static inline bool eth_addr_equal_except(const struct eth_addr a,
     205                 :            :                                          const struct eth_addr b,
     206                 :            :                                          const struct eth_addr mask)
     207                 :            : {
     208 [ +  + ][ +  - ]:     989612 :     return !(((a.be16[0] ^ b.be16[0]) & mask.be16[0])
     209         [ +  - ]:     332969 :              || ((a.be16[1] ^ b.be16[1]) & mask.be16[1])
     210                 :     332969 :              || ((a.be16[2] ^ b.be16[2]) & mask.be16[2]));
     211                 :            : }
     212                 :            : 
     213                 :     101740 : static inline uint64_t eth_addr_to_uint64(const struct eth_addr ea)
     214                 :            : {
     215                 :     203480 :     return (((uint64_t) ntohs(ea.be16[0]) << 32)
     216                 :     101740 :             | ((uint64_t) ntohs(ea.be16[1]) << 16)
     217                 :     101740 :             | ntohs(ea.be16[2]));
     218                 :            : }
     219                 :            : 
     220                 :      42404 : static inline uint64_t eth_addr_vlan_to_uint64(const struct eth_addr ea,
     221                 :            :                                                uint16_t vlan)
     222                 :            : {
     223                 :      42404 :     return (((uint64_t)vlan << 48) | eth_addr_to_uint64(ea));
     224                 :            : }
     225                 :            : 
     226                 :         65 : static inline void eth_addr_from_uint64(uint64_t x, struct eth_addr *ea)
     227                 :            : {
     228                 :         65 :     ea->be16[0] = htons(x >> 32);
     229                 :         65 :     ea->be16[1] = htons((x & 0xFFFF0000) >> 16);
     230                 :         65 :     ea->be16[2] = htons(x & 0xFFFF);
     231                 :         65 : }
     232                 :            : 
     233                 :        696 : static inline struct eth_addr eth_addr_invert(const struct eth_addr src)
     234                 :            : {
     235                 :            :     struct eth_addr dst;
     236                 :            : 
     237         [ +  + ]:       2784 :     for (int i = 0; i < ARRAY_SIZE(src.be16); i++) {
     238                 :       2088 :         dst.be16[i] = ~src.be16[i];
     239                 :            :     }
     240                 :            : 
     241                 :        696 :     return dst;
     242                 :            : }
     243                 :            : 
     244                 :   33432370 : static inline void eth_addr_mark_random(struct eth_addr *ea)
     245                 :            : {
     246                 :   33432370 :     ea->ea[0] &= ~1;                /* Unicast. */
     247                 :   33432370 :     ea->ea[0] |= 2;                 /* Private. */
     248                 :   33432370 : }
     249                 :            : 
     250                 :   33431618 : static inline void eth_addr_random(struct eth_addr *ea)
     251                 :            : {
     252                 :   33431618 :     random_bytes((uint8_t *)ea, sizeof *ea);
     253                 :   33431618 :     eth_addr_mark_random(ea);
     254                 :   33431618 : }
     255                 :            : 
     256                 :        749 : static inline void eth_addr_nicira_random(struct eth_addr *ea)
     257                 :            : {
     258                 :        749 :     eth_addr_random(ea);
     259                 :            : 
     260                 :            :     /* Set the OUI to the Nicira one. */
     261                 :        749 :     ea->ea[0] = 0x00;
     262                 :        749 :     ea->ea[1] = 0x23;
     263                 :        749 :     ea->ea[2] = 0x20;
     264                 :            : 
     265                 :            :     /* Set the top bit to indicate random Nicira address. */
     266                 :        749 :     ea->ea[3] |= 0x80;
     267                 :        749 : }
     268                 :      42404 : static inline uint32_t hash_mac(const struct eth_addr ea,
     269                 :            :                                 const uint16_t vlan, const uint32_t basis)
     270                 :            : {
     271                 :      42404 :     return hash_uint64_basis(eth_addr_vlan_to_uint64(ea, vlan), basis);
     272                 :            : }
     273                 :            : 
     274                 :            : bool eth_addr_is_reserved(const struct eth_addr);
     275                 :            : bool eth_addr_from_string(const char *, struct eth_addr *);
     276                 :            : 
     277                 :            : void compose_rarp(struct dp_packet *, const struct eth_addr);
     278                 :            : 
     279                 :            : void eth_push_vlan(struct dp_packet *, ovs_be16 tpid, ovs_be16 tci);
     280                 :            : void eth_pop_vlan(struct dp_packet *);
     281                 :            : 
     282                 :            : const char *eth_from_hex(const char *hex, struct dp_packet **packetp);
     283                 :            : void eth_format_masked(const struct eth_addr ea,
     284                 :            :                        const struct eth_addr *mask, struct ds *s);
     285                 :            : 
     286                 :            : void set_mpls_lse(struct dp_packet *, ovs_be32 label);
     287                 :            : void push_mpls(struct dp_packet *packet, ovs_be16 ethtype, ovs_be32 lse);
     288                 :            : void pop_mpls(struct dp_packet *, ovs_be16 ethtype);
     289                 :            : 
     290                 :            : void set_mpls_lse_ttl(ovs_be32 *lse, uint8_t ttl);
     291                 :            : void set_mpls_lse_tc(ovs_be32 *lse, uint8_t tc);
     292                 :            : void set_mpls_lse_label(ovs_be32 *lse, ovs_be32 label);
     293                 :            : void set_mpls_lse_bos(ovs_be32 *lse, uint8_t bos);
     294                 :            : ovs_be32 set_mpls_lse_values(uint8_t ttl, uint8_t tc, uint8_t bos,
     295                 :            :                              ovs_be32 label);
     296                 :            : 
     297                 :            : /* Example:
     298                 :            :  *
     299                 :            :  * struct eth_addr mac;
     300                 :            :  *    [...]
     301                 :            :  * printf("The Ethernet address is "ETH_ADDR_FMT"\n", ETH_ADDR_ARGS(mac));
     302                 :            :  *
     303                 :            :  */
     304                 :            : #define ETH_ADDR_FMT                                                    \
     305                 :            :     "%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8":%02"PRIx8
     306                 :            : #define ETH_ADDR_ARGS(EA) ETH_ADDR_BYTES_ARGS((EA).ea)
     307                 :            : #define ETH_ADDR_BYTES_ARGS(EAB) \
     308                 :            :          (EAB)[0], (EAB)[1], (EAB)[2], (EAB)[3], (EAB)[4], (EAB)[5]
     309                 :            : #define ETH_ADDR_STRLEN 17
     310                 :            : 
     311                 :            : /* Example:
     312                 :            :  *
     313                 :            :  * char *string = "1 00:11:22:33:44:55 2";
     314                 :            :  * struct eth_addr mac;
     315                 :            :  * int a, b;
     316                 :            :  *
     317                 :            :  * if (ovs_scan(string, "%d"ETH_ADDR_SCAN_FMT"%d",
     318                 :            :  *              &a, ETH_ADDR_SCAN_ARGS(mac), &b)) {
     319                 :            :  *     ...
     320                 :            :  * }
     321                 :            :  */
     322                 :            : #define ETH_ADDR_SCAN_FMT "%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8
     323                 :            : #define ETH_ADDR_SCAN_ARGS(EA) \
     324                 :            :     &(EA).ea[0], &(EA).ea[1], &(EA).ea[2], &(EA).ea[3], &(EA).ea[4], &(EA).ea[5]
     325                 :            : 
     326                 :            : #define ETH_TYPE_IP            0x0800
     327                 :            : #define ETH_TYPE_ARP           0x0806
     328                 :            : #define ETH_TYPE_TEB           0x6558
     329                 :            : #define ETH_TYPE_VLAN_8021Q    0x8100
     330                 :            : #define ETH_TYPE_VLAN          ETH_TYPE_VLAN_8021Q
     331                 :            : #define ETH_TYPE_VLAN_8021AD   0x88a8
     332                 :            : #define ETH_TYPE_IPV6          0x86dd
     333                 :            : #define ETH_TYPE_LACP          0x8809
     334                 :            : #define ETH_TYPE_RARP          0x8035
     335                 :            : #define ETH_TYPE_MPLS          0x8847
     336                 :            : #define ETH_TYPE_MPLS_MCAST    0x8848
     337                 :            : 
     338                 :     539183 : static inline bool eth_type_mpls(ovs_be16 eth_type)
     339                 :            : {
     340   [ +  +  +  + ]:    1051390 :     return eth_type == htons(ETH_TYPE_MPLS) ||
     341                 :     512207 :         eth_type == htons(ETH_TYPE_MPLS_MCAST);
     342                 :            : }
     343                 :            : 
     344                 :       3656 : static inline bool eth_type_vlan(ovs_be16 eth_type)
     345                 :            : {
     346   [ +  +  -  + ]:       7220 :     return eth_type == htons(ETH_TYPE_VLAN_8021Q) ||
     347                 :       3564 :         eth_type == htons(ETH_TYPE_VLAN_8021AD);
     348                 :            : }
     349                 :            : 
     350                 :            : 
     351                 :            : /* Minimum value for an Ethernet type.  Values below this are IEEE 802.2 frame
     352                 :            :  * lengths. */
     353                 :            : #define ETH_TYPE_MIN           0x600
     354                 :            : 
     355                 :            : #define ETH_HEADER_LEN 14
     356                 :            : #define ETH_PAYLOAD_MIN 46
     357                 :            : #define ETH_PAYLOAD_MAX 1500
     358                 :            : #define ETH_TOTAL_MIN (ETH_HEADER_LEN + ETH_PAYLOAD_MIN)
     359                 :            : #define ETH_TOTAL_MAX (ETH_HEADER_LEN + ETH_PAYLOAD_MAX)
     360                 :            : #define ETH_VLAN_TOTAL_MAX (ETH_HEADER_LEN + VLAN_HEADER_LEN + ETH_PAYLOAD_MAX)
     361                 :            : OVS_PACKED(
     362                 :            : struct eth_header {
     363                 :            :     struct eth_addr eth_dst;
     364                 :            :     struct eth_addr eth_src;
     365                 :            :     ovs_be16 eth_type;
     366                 :            : });
     367                 :            : BUILD_ASSERT_DECL(ETH_HEADER_LEN == sizeof(struct eth_header));
     368                 :            : 
     369                 :            : #define LLC_DSAP_SNAP 0xaa
     370                 :            : #define LLC_SSAP_SNAP 0xaa
     371                 :            : #define LLC_CNTL_SNAP 3
     372                 :            : 
     373                 :            : #define LLC_HEADER_LEN 3
     374                 :            : OVS_PACKED(
     375                 :            : struct llc_header {
     376                 :            :     uint8_t llc_dsap;
     377                 :            :     uint8_t llc_ssap;
     378                 :            :     uint8_t llc_cntl;
     379                 :            : });
     380                 :            : BUILD_ASSERT_DECL(LLC_HEADER_LEN == sizeof(struct llc_header));
     381                 :            : 
     382                 :            : /* LLC field values used for STP frames. */
     383                 :            : #define STP_LLC_SSAP 0x42
     384                 :            : #define STP_LLC_DSAP 0x42
     385                 :            : #define STP_LLC_CNTL 0x03
     386                 :            : 
     387                 :            : #define SNAP_ORG_ETHERNET "\0\0" /* The compiler adds a null byte, so
     388                 :            :                                     sizeof(SNAP_ORG_ETHERNET) == 3. */
     389                 :            : #define SNAP_HEADER_LEN 5
     390                 :            : OVS_PACKED(
     391                 :            : struct snap_header {
     392                 :            :     uint8_t snap_org[3];
     393                 :            :     ovs_be16 snap_type;
     394                 :            : });
     395                 :            : BUILD_ASSERT_DECL(SNAP_HEADER_LEN == sizeof(struct snap_header));
     396                 :            : 
     397                 :            : #define LLC_SNAP_HEADER_LEN (LLC_HEADER_LEN + SNAP_HEADER_LEN)
     398                 :            : OVS_PACKED(
     399                 :            : struct llc_snap_header {
     400                 :            :     struct llc_header llc;
     401                 :            :     struct snap_header snap;
     402                 :            : });
     403                 :            : BUILD_ASSERT_DECL(LLC_SNAP_HEADER_LEN == sizeof(struct llc_snap_header));
     404                 :            : 
     405                 :            : #define VLAN_VID_MASK 0x0fff
     406                 :            : #define VLAN_VID_SHIFT 0
     407                 :            : 
     408                 :            : #define VLAN_PCP_MASK 0xe000
     409                 :            : #define VLAN_PCP_SHIFT 13
     410                 :            : 
     411                 :            : #define VLAN_CFI 0x1000
     412                 :            : #define VLAN_CFI_SHIFT 12
     413                 :            : 
     414                 :            : /* Given the vlan_tci field from an 802.1Q header, in network byte order,
     415                 :            :  * returns the VLAN ID in host byte order. */
     416                 :            : static inline uint16_t
     417                 :      67422 : vlan_tci_to_vid(ovs_be16 vlan_tci)
     418                 :            : {
     419                 :      67422 :     return (ntohs(vlan_tci) & VLAN_VID_MASK) >> VLAN_VID_SHIFT;
     420                 :            : }
     421                 :            : 
     422                 :            : /* Given the vlan_tci field from an 802.1Q header, in network byte order,
     423                 :            :  * returns the priority code point (PCP) in host byte order. */
     424                 :            : static inline int
     425                 :       2386 : vlan_tci_to_pcp(ovs_be16 vlan_tci)
     426                 :            : {
     427                 :       2386 :     return (ntohs(vlan_tci) & VLAN_PCP_MASK) >> VLAN_PCP_SHIFT;
     428                 :            : }
     429                 :            : 
     430                 :            : /* Given the vlan_tci field from an 802.1Q header, in network byte order,
     431                 :            :  * returns the Canonical Format Indicator (CFI). */
     432                 :            : static inline int
     433                 :            : vlan_tci_to_cfi(ovs_be16 vlan_tci)
     434                 :            : {
     435                 :            :     return (vlan_tci & htons(VLAN_CFI)) != 0;
     436                 :            : }
     437                 :            : 
     438                 :            : #define VLAN_HEADER_LEN 4
     439                 :            : struct vlan_header {
     440                 :            :     ovs_be16 vlan_tci;          /* Lowest 12 bits are VLAN ID. */
     441                 :            :     ovs_be16 vlan_next_type;
     442                 :            : };
     443                 :            : BUILD_ASSERT_DECL(VLAN_HEADER_LEN == sizeof(struct vlan_header));
     444                 :            : 
     445                 :            : #define VLAN_ETH_HEADER_LEN (ETH_HEADER_LEN + VLAN_HEADER_LEN)
     446                 :            : OVS_PACKED(
     447                 :            : struct vlan_eth_header {
     448                 :            :     struct eth_addr veth_dst;
     449                 :            :     struct eth_addr veth_src;
     450                 :            :     ovs_be16 veth_type;         /* Always htons(ETH_TYPE_VLAN). */
     451                 :            :     ovs_be16 veth_tci;          /* Lowest 12 bits are VLAN ID. */
     452                 :            :     ovs_be16 veth_next_type;
     453                 :            : });
     454                 :            : BUILD_ASSERT_DECL(VLAN_ETH_HEADER_LEN == sizeof(struct vlan_eth_header));
     455                 :            : 
     456                 :            : /* MPLS related definitions */
     457                 :            : #define MPLS_TTL_MASK       0x000000ff
     458                 :            : #define MPLS_TTL_SHIFT      0
     459                 :            : 
     460                 :            : #define MPLS_BOS_MASK       0x00000100
     461                 :            : #define MPLS_BOS_SHIFT      8
     462                 :            : 
     463                 :            : #define MPLS_TC_MASK        0x00000e00
     464                 :            : #define MPLS_TC_SHIFT       9
     465                 :            : 
     466                 :            : #define MPLS_LABEL_MASK     0xfffff000
     467                 :            : #define MPLS_LABEL_SHIFT    12
     468                 :            : 
     469                 :            : #define MPLS_HLEN           4
     470                 :            : 
     471                 :            : struct mpls_hdr {
     472                 :            :     ovs_16aligned_be32 mpls_lse;
     473                 :            : };
     474                 :            : BUILD_ASSERT_DECL(MPLS_HLEN == sizeof(struct mpls_hdr));
     475                 :            : 
     476                 :            : /* Given a mpls label stack entry in network byte order
     477                 :            :  * return mpls label in host byte order */
     478                 :            : static inline uint32_t
     479                 :        523 : mpls_lse_to_label(ovs_be32 mpls_lse)
     480                 :            : {
     481                 :        523 :     return (ntohl(mpls_lse) & MPLS_LABEL_MASK) >> MPLS_LABEL_SHIFT;
     482                 :            : }
     483                 :            : 
     484                 :            : /* Given a mpls label stack entry in network byte order
     485                 :            :  * return mpls tc */
     486                 :            : static inline uint8_t
     487                 :        396 : mpls_lse_to_tc(ovs_be32 mpls_lse)
     488                 :            : {
     489                 :        396 :     return (ntohl(mpls_lse) & MPLS_TC_MASK) >> MPLS_TC_SHIFT;
     490                 :            : }
     491                 :            : 
     492                 :            : /* Given a mpls label stack entry in network byte order
     493                 :            :  * return mpls ttl */
     494                 :            : static inline uint8_t
     495                 :        362 : mpls_lse_to_ttl(ovs_be32 mpls_lse)
     496                 :            : {
     497                 :        362 :     return (ntohl(mpls_lse) & MPLS_TTL_MASK) >> MPLS_TTL_SHIFT;
     498                 :            : }
     499                 :            : 
     500                 :            : /* Set TTL in mpls lse. */
     501                 :            : static inline void
     502                 :            : flow_set_mpls_lse_ttl(ovs_be32 *mpls_lse, uint8_t ttl)
     503                 :            : {
     504                 :            :     *mpls_lse &= ~htonl(MPLS_TTL_MASK);
     505                 :            :     *mpls_lse |= htonl(ttl << MPLS_TTL_SHIFT);
     506                 :            : }
     507                 :            : 
     508                 :            : /* Given a mpls label stack entry in network byte order
     509                 :            :  * return mpls BoS bit  */
     510                 :            : static inline uint8_t
     511                 :        322 : mpls_lse_to_bos(ovs_be32 mpls_lse)
     512                 :            : {
     513                 :        322 :     return (mpls_lse & htonl(MPLS_BOS_MASK)) != 0;
     514                 :            : }
     515                 :            : 
     516                 :            : #define IP_FMT "%"PRIu32".%"PRIu32".%"PRIu32".%"PRIu32
     517                 :            : #define IP_ARGS(ip)                             \
     518                 :            :     ntohl(ip) >> 24,                            \
     519                 :            :     (ntohl(ip) >> 16) & 0xff,                   \
     520                 :            :     (ntohl(ip) >> 8) & 0xff,                    \
     521                 :            :     ntohl(ip) & 0xff
     522                 :            : 
     523                 :            : /* Example:
     524                 :            :  *
     525                 :            :  * char *string = "1 33.44.55.66 2";
     526                 :            :  * ovs_be32 ip;
     527                 :            :  * int a, b;
     528                 :            :  *
     529                 :            :  * if (ovs_scan(string, "%d"IP_SCAN_FMT"%d", &a, IP_SCAN_ARGS(&ip), &b)) {
     530                 :            :  *     ...
     531                 :            :  * }
     532                 :            :  */
     533                 :            : #define IP_SCAN_FMT "%"SCNu8".%"SCNu8".%"SCNu8".%"SCNu8
     534                 :            : #define IP_SCAN_ARGS(ip)                                    \
     535                 :            :         ((void) (ovs_be32) *(ip), &((uint8_t *) ip)[0]),    \
     536                 :            :         &((uint8_t *) ip)[1],                               \
     537                 :            :         &((uint8_t *) ip)[2],                               \
     538                 :            :         &((uint8_t *) ip)[3]
     539                 :            : 
     540                 :            : /* Returns true if 'netmask' is a CIDR netmask, that is, if it consists of N
     541                 :            :  * high-order 1-bits and 32-N low-order 0-bits. */
     542                 :            : static inline bool
     543                 :      71094 : ip_is_cidr(ovs_be32 netmask)
     544                 :            : {
     545                 :      71094 :     uint32_t x = ~ntohl(netmask);
     546                 :      71094 :     return !(x & (x + 1));
     547                 :            : }
     548                 :            : static inline bool
     549                 :      20342 : ip_is_multicast(ovs_be32 ip)
     550                 :            : {
     551                 :      20342 :     return (ip & htonl(0xf0000000)) == htonl(0xe0000000);
     552                 :            : }
     553                 :            : static inline bool
     554                 :          0 : ip_is_local_multicast(ovs_be32 ip)
     555                 :            : {
     556                 :          0 :     return (ip & htonl(0xffffff00)) == htonl(0xe0000000);
     557                 :            : }
     558                 :            : int ip_count_cidr_bits(ovs_be32 netmask);
     559                 :            : void ip_format_masked(ovs_be32 ip, ovs_be32 mask, struct ds *);
     560                 :            : bool ip_parse(const char *s, ovs_be32 *ip);
     561                 :            : char *ip_parse_masked(const char *s, ovs_be32 *ip, ovs_be32 *mask)
     562                 :            :     OVS_WARN_UNUSED_RESULT;
     563                 :            : char *ip_parse_cidr(const char *s, ovs_be32 *ip, unsigned int *plen)
     564                 :            :     OVS_WARN_UNUSED_RESULT;
     565                 :            : char *ip_parse_masked_len(const char *s, int *n, ovs_be32 *ip, ovs_be32 *mask)
     566                 :            :     OVS_WARN_UNUSED_RESULT;
     567                 :            : char *ip_parse_cidr_len(const char *s, int *n, ovs_be32 *ip,
     568                 :            :                         unsigned int *plen)
     569                 :            :     OVS_WARN_UNUSED_RESULT;
     570                 :            : 
     571                 :            : #define IP_VER(ip_ihl_ver) ((ip_ihl_ver) >> 4)
     572                 :            : #define IP_IHL(ip_ihl_ver) ((ip_ihl_ver) & 15)
     573                 :            : #define IP_IHL_VER(ihl, ver) (((ver) << 4) | (ihl))
     574                 :            : 
     575                 :            : #ifndef IPPROTO_SCTP
     576                 :            : #define IPPROTO_SCTP 132
     577                 :            : #endif
     578                 :            : 
     579                 :            : /* TOS fields. */
     580                 :            : #define IP_ECN_NOT_ECT 0x0
     581                 :            : #define IP_ECN_ECT_1 0x01
     582                 :            : #define IP_ECN_ECT_0 0x02
     583                 :            : #define IP_ECN_CE 0x03
     584                 :            : #define IP_ECN_MASK 0x03
     585                 :            : #define IP_DSCP_MASK 0xfc
     586                 :            : 
     587                 :            : static inline int
     588                 :       6794 : IP_ECN_is_ce(uint8_t dsfield)
     589                 :            : {
     590                 :       6794 :     return (dsfield & IP_ECN_MASK) == IP_ECN_CE;
     591                 :            : }
     592                 :            : 
     593                 :            : #define IP_VERSION 4
     594                 :            : 
     595                 :            : #define IP_DONT_FRAGMENT  0x4000 /* Don't fragment. */
     596                 :            : #define IP_MORE_FRAGMENTS 0x2000 /* More fragments. */
     597                 :            : #define IP_FRAG_OFF_MASK  0x1fff /* Fragment offset. */
     598                 :            : #define IP_IS_FRAGMENT(ip_frag_off) \
     599                 :            :         ((ip_frag_off) & htons(IP_MORE_FRAGMENTS | IP_FRAG_OFF_MASK))
     600                 :            : 
     601                 :            : #define IP_HEADER_LEN 20
     602                 :            : struct ip_header {
     603                 :            :     uint8_t ip_ihl_ver;
     604                 :            :     uint8_t ip_tos;
     605                 :            :     ovs_be16 ip_tot_len;
     606                 :            :     ovs_be16 ip_id;
     607                 :            :     ovs_be16 ip_frag_off;
     608                 :            :     uint8_t ip_ttl;
     609                 :            :     uint8_t ip_proto;
     610                 :            :     ovs_be16 ip_csum;
     611                 :            :     ovs_16aligned_be32 ip_src;
     612                 :            :     ovs_16aligned_be32 ip_dst;
     613                 :            : };
     614                 :            : BUILD_ASSERT_DECL(IP_HEADER_LEN == sizeof(struct ip_header));
     615                 :            : 
     616                 :            : /* ICMPv4 types. */
     617                 :            : #define ICMP4_ECHO_REPLY 0
     618                 :            : #define ICMP4_DST_UNREACH 3
     619                 :            : #define ICMP4_SOURCEQUENCH 4
     620                 :            : #define ICMP4_REDIRECT 5
     621                 :            : #define ICMP4_ECHO_REQUEST 8
     622                 :            : #define ICMP4_TIME_EXCEEDED 11
     623                 :            : #define ICMP4_PARAM_PROB 12
     624                 :            : #define ICMP4_TIMESTAMP 13
     625                 :            : #define ICMP4_TIMESTAMPREPLY 14
     626                 :            : #define ICMP4_INFOREQUEST 15
     627                 :            : #define ICMP4_INFOREPLY 16
     628                 :            : 
     629                 :            : #define ICMP_HEADER_LEN 8
     630                 :            : struct icmp_header {
     631                 :            :     uint8_t icmp_type;
     632                 :            :     uint8_t icmp_code;
     633                 :            :     ovs_be16 icmp_csum;
     634                 :            :     union {
     635                 :            :         struct {
     636                 :            :             ovs_be16 id;
     637                 :            :             ovs_be16 seq;
     638                 :            :         } echo;
     639                 :            :         struct {
     640                 :            :             ovs_be16 empty;
     641                 :            :             ovs_be16 mtu;
     642                 :            :         } frag;
     643                 :            :         ovs_16aligned_be32 gateway;
     644                 :            :     } icmp_fields;
     645                 :            : };
     646                 :            : BUILD_ASSERT_DECL(ICMP_HEADER_LEN == sizeof(struct icmp_header));
     647                 :            : 
     648                 :            : #define IGMP_HEADER_LEN 8
     649                 :            : struct igmp_header {
     650                 :            :     uint8_t igmp_type;
     651                 :            :     uint8_t igmp_code;
     652                 :            :     ovs_be16 igmp_csum;
     653                 :            :     ovs_16aligned_be32 group;
     654                 :            : };
     655                 :            : BUILD_ASSERT_DECL(IGMP_HEADER_LEN == sizeof(struct igmp_header));
     656                 :            : 
     657                 :            : #define IGMPV3_HEADER_LEN 8
     658                 :            : struct igmpv3_header {
     659                 :            :     uint8_t type;
     660                 :            :     uint8_t rsvr1;
     661                 :            :     ovs_be16 csum;
     662                 :            :     ovs_be16 rsvr2;
     663                 :            :     ovs_be16 ngrp;
     664                 :            : };
     665                 :            : BUILD_ASSERT_DECL(IGMPV3_HEADER_LEN == sizeof(struct igmpv3_header));
     666                 :            : 
     667                 :            : #define IGMPV3_RECORD_LEN 8
     668                 :            : struct igmpv3_record {
     669                 :            :     uint8_t type;
     670                 :            :     uint8_t aux_len;
     671                 :            :     ovs_be16 nsrcs;
     672                 :            :     ovs_16aligned_be32 maddr;
     673                 :            : };
     674                 :            : BUILD_ASSERT_DECL(IGMPV3_RECORD_LEN == sizeof(struct igmpv3_record));
     675                 :            : 
     676                 :            : #define IGMP_HOST_MEMBERSHIP_QUERY    0x11 /* From RFC1112 */
     677                 :            : #define IGMP_HOST_MEMBERSHIP_REPORT   0x12 /* Ditto */
     678                 :            : #define IGMPV2_HOST_MEMBERSHIP_REPORT 0x16 /* V2 version of 0x12 */
     679                 :            : #define IGMP_HOST_LEAVE_MESSAGE       0x17
     680                 :            : #define IGMPV3_HOST_MEMBERSHIP_REPORT 0x22 /* V3 version of 0x12 */
     681                 :            : 
     682                 :            : /*
     683                 :            :  * IGMPv3 and MLDv2 use the same codes.
     684                 :            :  */
     685                 :            : #define IGMPV3_MODE_IS_INCLUDE 1
     686                 :            : #define IGMPV3_MODE_IS_EXCLUDE 2
     687                 :            : #define IGMPV3_CHANGE_TO_INCLUDE_MODE 3
     688                 :            : #define IGMPV3_CHANGE_TO_EXCLUDE_MODE 4
     689                 :            : #define IGMPV3_ALLOW_NEW_SOURCES 5
     690                 :            : #define IGMPV3_BLOCK_OLD_SOURCES 6
     691                 :            : 
     692                 :            : #define SCTP_HEADER_LEN 12
     693                 :            : struct sctp_header {
     694                 :            :     ovs_be16 sctp_src;
     695                 :            :     ovs_be16 sctp_dst;
     696                 :            :     ovs_16aligned_be32 sctp_vtag;
     697                 :            :     ovs_16aligned_be32 sctp_csum;
     698                 :            : };
     699                 :            : BUILD_ASSERT_DECL(SCTP_HEADER_LEN == sizeof(struct sctp_header));
     700                 :            : 
     701                 :            : #define UDP_HEADER_LEN 8
     702                 :            : struct udp_header {
     703                 :            :     ovs_be16 udp_src;
     704                 :            :     ovs_be16 udp_dst;
     705                 :            :     ovs_be16 udp_len;
     706                 :            :     ovs_be16 udp_csum;
     707                 :            : };
     708                 :            : BUILD_ASSERT_DECL(UDP_HEADER_LEN == sizeof(struct udp_header));
     709                 :            : 
     710                 :            : #define TCP_FIN 0x001
     711                 :            : #define TCP_SYN 0x002
     712                 :            : #define TCP_RST 0x004
     713                 :            : #define TCP_PSH 0x008
     714                 :            : #define TCP_ACK 0x010
     715                 :            : #define TCP_URG 0x020
     716                 :            : #define TCP_ECE 0x040
     717                 :            : #define TCP_CWR 0x080
     718                 :            : #define TCP_NS  0x100
     719                 :            : 
     720                 :            : #define TCP_CTL(flags, offset) (htons((flags) | ((offset) << 12)))
     721                 :            : #define TCP_FLAGS(tcp_ctl) (ntohs(tcp_ctl) & 0x0fff)
     722                 :            : #define TCP_FLAGS_BE16(tcp_ctl) ((tcp_ctl) & htons(0x0fff))
     723                 :            : #define TCP_OFFSET(tcp_ctl) (ntohs(tcp_ctl) >> 12)
     724                 :            : 
     725                 :            : #define TCP_HEADER_LEN 20
     726                 :            : struct tcp_header {
     727                 :            :     ovs_be16 tcp_src;
     728                 :            :     ovs_be16 tcp_dst;
     729                 :            :     ovs_16aligned_be32 tcp_seq;
     730                 :            :     ovs_16aligned_be32 tcp_ack;
     731                 :            :     ovs_be16 tcp_ctl;
     732                 :            :     ovs_be16 tcp_winsz;
     733                 :            :     ovs_be16 tcp_csum;
     734                 :            :     ovs_be16 tcp_urg;
     735                 :            : };
     736                 :            : BUILD_ASSERT_DECL(TCP_HEADER_LEN == sizeof(struct tcp_header));
     737                 :            : 
     738                 :            : /* Connection states */
     739                 :            : enum {
     740                 :            :     CS_NEW_BIT =         0,
     741                 :            :     CS_ESTABLISHED_BIT = 1,
     742                 :            :     CS_RELATED_BIT =     2,
     743                 :            :     CS_REPLY_DIR_BIT =   3,
     744                 :            :     CS_INVALID_BIT =     4,
     745                 :            :     CS_TRACKED_BIT =     5,
     746                 :            :     CS_SRC_NAT_BIT =     6,
     747                 :            :     CS_DST_NAT_BIT =     7,
     748                 :            : };
     749                 :            : 
     750                 :            : enum {
     751                 :            :     CS_NEW =         (1 << CS_NEW_BIT),
     752                 :            :     CS_ESTABLISHED = (1 << CS_ESTABLISHED_BIT),
     753                 :            :     CS_RELATED =     (1 << CS_RELATED_BIT),
     754                 :            :     CS_REPLY_DIR =   (1 << CS_REPLY_DIR_BIT),
     755                 :            :     CS_INVALID =     (1 << CS_INVALID_BIT),
     756                 :            :     CS_TRACKED =     (1 << CS_TRACKED_BIT),
     757                 :            :     CS_SRC_NAT =     (1 << CS_SRC_NAT_BIT),
     758                 :            :     CS_DST_NAT =     (1 << CS_DST_NAT_BIT),
     759                 :            : };
     760                 :            : 
     761                 :            : /* Undefined connection state bits. */
     762                 :            : #define CS_SUPPORTED_MASK    (CS_NEW | CS_ESTABLISHED | CS_RELATED \
     763                 :            :                               | CS_INVALID | CS_REPLY_DIR | CS_TRACKED \
     764                 :            :                               | CS_SRC_NAT | CS_DST_NAT)
     765                 :            : #define CS_UNSUPPORTED_MASK  (~(uint32_t)CS_SUPPORTED_MASK)
     766                 :            : 
     767                 :            : #define ARP_HRD_ETHERNET 1
     768                 :            : #define ARP_PRO_IP 0x0800
     769                 :            : #define ARP_OP_REQUEST 1
     770                 :            : #define ARP_OP_REPLY 2
     771                 :            : #define ARP_OP_RARP 3
     772                 :            : 
     773                 :            : #define ARP_ETH_HEADER_LEN 28
     774                 :            : struct arp_eth_header {
     775                 :            :     /* Generic members. */
     776                 :            :     ovs_be16 ar_hrd;           /* Hardware type. */
     777                 :            :     ovs_be16 ar_pro;           /* Protocol type. */
     778                 :            :     uint8_t ar_hln;            /* Hardware address length. */
     779                 :            :     uint8_t ar_pln;            /* Protocol address length. */
     780                 :            :     ovs_be16 ar_op;            /* Opcode. */
     781                 :            : 
     782                 :            :     /* Ethernet+IPv4 specific members. */
     783                 :            :     struct eth_addr ar_sha;     /* Sender hardware address. */
     784                 :            :     ovs_16aligned_be32 ar_spa;  /* Sender protocol address. */
     785                 :            :     struct eth_addr ar_tha;     /* Target hardware address. */
     786                 :            :     ovs_16aligned_be32 ar_tpa;  /* Target protocol address. */
     787                 :            : };
     788                 :            : BUILD_ASSERT_DECL(ARP_ETH_HEADER_LEN == sizeof(struct arp_eth_header));
     789                 :            : 
     790                 :            : #define IPV6_HEADER_LEN 40
     791                 :            : 
     792                 :            : /* Like struct in6_addr, but whereas that struct requires 32-bit alignment on
     793                 :            :  * most implementations, this one only requires 16-bit alignment. */
     794                 :            : union ovs_16aligned_in6_addr {
     795                 :            :     ovs_be16 be16[8];
     796                 :            :     ovs_16aligned_be32 be32[4];
     797                 :            : };
     798                 :            : 
     799                 :            : /* Like struct in6_hdr, but whereas that struct requires 32-bit alignment, this
     800                 :            :  * one only requires 16-bit alignment. */
     801                 :            : struct ovs_16aligned_ip6_hdr {
     802                 :            :     union {
     803                 :            :         struct ovs_16aligned_ip6_hdrctl {
     804                 :            :             ovs_16aligned_be32 ip6_un1_flow;
     805                 :            :             ovs_be16 ip6_un1_plen;
     806                 :            :             uint8_t ip6_un1_nxt;
     807                 :            :             uint8_t ip6_un1_hlim;
     808                 :            :         } ip6_un1;
     809                 :            :         uint8_t ip6_un2_vfc;
     810                 :            :     } ip6_ctlun;
     811                 :            :     union ovs_16aligned_in6_addr ip6_src;
     812                 :            :     union ovs_16aligned_in6_addr ip6_dst;
     813                 :            : };
     814                 :            : 
     815                 :            : /* Like struct in6_frag, but whereas that struct requires 32-bit alignment,
     816                 :            :  * this one only requires 16-bit alignment. */
     817                 :            : struct ovs_16aligned_ip6_frag {
     818                 :            :     uint8_t ip6f_nxt;
     819                 :            :     uint8_t ip6f_reserved;
     820                 :            :     ovs_be16 ip6f_offlg;
     821                 :            :     ovs_16aligned_be32 ip6f_ident;
     822                 :            : };
     823                 :            : 
     824                 :            : #define ICMP6_HEADER_LEN 4
     825                 :            : struct icmp6_header {
     826                 :            :     uint8_t icmp6_type;
     827                 :            :     uint8_t icmp6_code;
     828                 :            :     ovs_be16 icmp6_cksum;
     829                 :            : };
     830                 :            : BUILD_ASSERT_DECL(ICMP6_HEADER_LEN == sizeof(struct icmp6_header));
     831                 :            : 
     832                 :            : uint32_t packet_csum_pseudoheader6(const struct ovs_16aligned_ip6_hdr *);
     833                 :            : 
     834                 :            : /* Neighbor Discovery option field.
     835                 :            :  * ND options are always a multiple of 8 bytes in size. */
     836                 :            : #define ND_OPT_LEN 8
     837                 :            : struct ovs_nd_opt {
     838                 :            :     uint8_t  nd_opt_type;      /* Values defined in icmp6.h */
     839                 :            :     uint8_t  nd_opt_len;       /* in units of 8 octets (the size of this struct) */
     840                 :            :     struct eth_addr nd_opt_mac;   /* Ethernet address in the case of SLL or TLL options */
     841                 :            : };
     842                 :            : BUILD_ASSERT_DECL(ND_OPT_LEN == sizeof(struct ovs_nd_opt));
     843                 :            : 
     844                 :            : /* Like struct nd_msg (from ndisc.h), but whereas that struct requires 32-bit
     845                 :            :  * alignment, this one only requires 16-bit alignment. */
     846                 :            : #define ND_MSG_LEN 24
     847                 :            : struct ovs_nd_msg {
     848                 :            :     struct icmp6_header icmph;
     849                 :            :     ovs_16aligned_be32 rso_flags;
     850                 :            :     union ovs_16aligned_in6_addr target;
     851                 :            :     struct ovs_nd_opt options[0];
     852                 :            : };
     853                 :            : BUILD_ASSERT_DECL(ND_MSG_LEN == sizeof(struct ovs_nd_msg));
     854                 :            : 
     855                 :            : #define ND_RSO_ROUTER    0x80000000
     856                 :            : #define ND_RSO_SOLICITED 0x40000000
     857                 :            : #define ND_RSO_OVERRIDE  0x20000000
     858                 :            : 
     859                 :            : /*
     860                 :            :  * Use the same struct for MLD and MLD2, naming members as the defined fields in
     861                 :            :  * in the corresponding version of the protocol, though they are reserved in the
     862                 :            :  * other one.
     863                 :            :  */
     864                 :            : #define MLD_HEADER_LEN 8
     865                 :            : struct mld_header {
     866                 :            :     uint8_t type;
     867                 :            :     uint8_t code;
     868                 :            :     ovs_be16 csum;
     869                 :            :     ovs_be16 mrd;
     870                 :            :     ovs_be16 ngrp;
     871                 :            : };
     872                 :            : BUILD_ASSERT_DECL(MLD_HEADER_LEN == sizeof(struct mld_header));
     873                 :            : 
     874                 :            : #define MLD2_RECORD_LEN 20
     875                 :            : struct mld2_record {
     876                 :            :     uint8_t type;
     877                 :            :     uint8_t aux_len;
     878                 :            :     ovs_be16 nsrcs;
     879                 :            :     union ovs_16aligned_in6_addr maddr;
     880                 :            : };
     881                 :            : BUILD_ASSERT_DECL(MLD2_RECORD_LEN == sizeof(struct mld2_record));
     882                 :            : 
     883                 :            : #define MLD_QUERY 130
     884                 :            : #define MLD_REPORT 131
     885                 :            : #define MLD_DONE 132
     886                 :            : #define MLD2_REPORT 143
     887                 :            : 
     888                 :            : /* The IPv6 flow label is in the lower 20 bits of the first 32-bit word. */
     889                 :            : #define IPV6_LABEL_MASK 0x000fffff
     890                 :            : 
     891                 :            : /* Example:
     892                 :            :  *
     893                 :            :  * char *string = "1 ::1 2";
     894                 :            :  * char ipv6_s[IPV6_SCAN_LEN + 1];
     895                 :            :  * struct in6_addr ipv6;
     896                 :            :  *
     897                 :            :  * if (ovs_scan(string, "%d"IPV6_SCAN_FMT"%d", &a, ipv6_s, &b)
     898                 :            :  *     && inet_pton(AF_INET6, ipv6_s, &ipv6) == 1) {
     899                 :            :  *     ...
     900                 :            :  * }
     901                 :            :  */
     902                 :            : #define IPV6_SCAN_FMT "%46[0123456789abcdefABCDEF:.]"
     903                 :            : #define IPV6_SCAN_LEN 46
     904                 :            : 
     905                 :            : extern const struct in6_addr in6addr_exact;
     906                 :            : #define IN6ADDR_EXACT_INIT { { { 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, \
     907                 :            :                                  0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff } } }
     908                 :            : 
     909                 :            : extern const struct in6_addr in6addr_all_hosts;
     910                 :            : #define IN6ADDR_ALL_HOSTS_INIT { { { 0xff,0x02,0x00,0x00,0x00,0x00,0x00,0x00, \
     911                 :            :                                      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01 } } }
     912                 :            : 
     913                 :    1447674 : static inline bool ipv6_addr_equals(const struct in6_addr *a,
     914                 :            :                                     const struct in6_addr *b)
     915                 :            : {
     916                 :            : #ifdef IN6_ARE_ADDR_EQUAL
     917 [ +  + ][ +  + ]:    1447674 :     return IN6_ARE_ADDR_EQUAL(a, b);
         [ +  + ][ +  + ]
     918                 :            : #else
     919                 :            :     return !memcmp(a, b, sizeof(*a));
     920                 :            : #endif
     921                 :            : }
     922                 :            : 
     923                 :            : /* Checks the IPv6 address in 'mask' for all zeroes. */
     924                 :     240624 : static inline bool ipv6_mask_is_any(const struct in6_addr *mask) {
     925                 :     240624 :     return ipv6_addr_equals(mask, &in6addr_any);
     926                 :            : }
     927                 :            : 
     928                 :       3827 : static inline bool ipv6_mask_is_exact(const struct in6_addr *mask) {
     929                 :       3827 :     return ipv6_addr_equals(mask, &in6addr_exact);
     930                 :            : }
     931                 :            : 
     932                 :          0 : static inline bool ipv6_is_all_hosts(const struct in6_addr *addr) {
     933                 :          0 :     return ipv6_addr_equals(addr, &in6addr_all_hosts);
     934                 :            : }
     935                 :            : 
     936                 :     419559 : static inline bool ipv6_addr_is_set(const struct in6_addr *addr) {
     937                 :     419559 :     return !ipv6_addr_equals(addr, &in6addr_any);
     938                 :            : }
     939                 :            : 
     940                 :          7 : static inline bool ipv6_addr_is_multicast(const struct in6_addr *ip) {
     941                 :          7 :     return ip->s6_addr[0] == 0xff;
     942                 :            : }
     943                 :            : 
     944                 :            : static inline struct in6_addr
     945                 :     923971 : in6_addr_mapped_ipv4(ovs_be32 ip4)
     946                 :            : {
     947                 :     923971 :     struct in6_addr ip6 = { .s6_addr = { [10] = 0xff, [11] = 0xff } };
     948                 :     923971 :     memcpy(&ip6.s6_addr[12], &ip4, 4);
     949                 :     923971 :     return ip6;
     950                 :            : }
     951                 :            : 
     952                 :            : static inline void
     953                 :     117900 : in6_addr_set_mapped_ipv4(struct in6_addr *ip6, ovs_be32 ip4)
     954                 :            : {
     955                 :     117900 :     *ip6 = in6_addr_mapped_ipv4(ip4);
     956                 :     117900 : }
     957                 :            : 
     958                 :            : static inline ovs_be32
     959                 :      33564 : in6_addr_get_mapped_ipv4(const struct in6_addr *addr)
     960                 :            : {
     961                 :      33564 :     union ovs_16aligned_in6_addr *taddr = (void *) addr;
     962 [ +  + ][ +  - ]:      33564 :     if (IN6_IS_ADDR_V4MAPPED(addr)) {
         [ +  + ][ +  + ]
     963                 :      25244 :         return get_16aligned_be32(&taddr->be32[3]);
     964                 :            :     } else {
     965                 :       8320 :         return INADDR_ANY;
     966                 :            :     }
     967                 :            : }
     968                 :            : 
     969                 :            : static inline void
     970                 :      11155 : in6_addr_solicited_node(struct in6_addr *addr, const struct in6_addr *ip6)
     971                 :            : {
     972                 :      11155 :     union ovs_16aligned_in6_addr *taddr = (void *) addr;
     973                 :      11155 :     memset(taddr->be16, 0, sizeof(taddr->be16));
     974                 :      11155 :     taddr->be16[0] = htons(0xff02);
     975                 :      11155 :     taddr->be16[5] = htons(0x1);
     976                 :      11155 :     taddr->be16[6] = htons(0xff00);
     977                 :      11155 :     memcpy(&addr->s6_addr[13], &ip6->s6_addr[13], 3);
     978                 :      11155 : }
     979                 :            : 
     980                 :            : /*
     981                 :            :  * Generates ipv6 link local address from the given eth addr
     982                 :            :  * with prefix 'fe80::/64' and stores it in 'lla'
     983                 :            :  */
     984                 :            : static inline void
     985                 :      10927 : in6_generate_lla(struct eth_addr ea, struct in6_addr *lla)
     986                 :            : {
     987                 :      10927 :     union ovs_16aligned_in6_addr *taddr = (void *) lla;
     988                 :      10927 :     memset(taddr->be16, 0, sizeof(taddr->be16));
     989                 :      10927 :     taddr->be16[0] = htons(0xfe80);
     990                 :      10927 :     taddr->be16[4] = htons(((ea.ea[0] ^ 0x02) << 8) | ea.ea[1]);
     991                 :      10927 :     taddr->be16[5] = htons(ea.ea[2] << 8 | 0x00ff);
     992                 :      10927 :     taddr->be16[6] = htons(0xfe << 8 | ea.ea[3]);
     993                 :      10927 :     taddr->be16[7] = ea.be16[2];
     994                 :      10927 : }
     995                 :            : 
     996                 :            : /* Returns true if 'addr' is a link local address.  Otherwise, false. */
     997                 :            : static inline bool
     998                 :       4972 : in6_is_lla(struct in6_addr *addr)
     999                 :            : {
    1000                 :            : #ifdef s6_addr32
    1001 [ +  - ][ +  - ]:       4972 :     return addr->s6_addr32[0] == htonl(0xfe800000) && !(addr->s6_addr32[1]);
    1002                 :            : #else
    1003                 :            :     return addr->s6_addr[0] == 0xfe && addr->s6_addr[1] == 0x80 &&
    1004                 :            :          !(addr->s6_addr[2] | addr->s6_addr[3] | addr->s6_addr[4] |
    1005                 :            :            addr->s6_addr[5] | addr->s6_addr[6] | addr->s6_addr[7]);
    1006                 :            : #endif
    1007                 :            : }
    1008                 :            : 
    1009                 :            : static inline void
    1010                 :          4 : ipv6_multicast_to_ethernet(struct eth_addr *eth, const struct in6_addr *ip6)
    1011                 :            : {
    1012                 :          4 :     eth->ea[0] = 0x33;
    1013                 :          4 :     eth->ea[1] = 0x33;
    1014                 :          4 :     eth->ea[2] = ip6->s6_addr[12];
    1015                 :          4 :     eth->ea[3] = ip6->s6_addr[13];
    1016                 :          4 :     eth->ea[4] = ip6->s6_addr[14];
    1017                 :          4 :     eth->ea[5] = ip6->s6_addr[15];
    1018                 :          4 : }
    1019                 :            : 
    1020                 :   18787331 : static inline bool dl_type_is_ip_any(ovs_be16 dl_type)
    1021                 :            : {
    1022                 :   37574662 :     return dl_type == htons(ETH_TYPE_IP)
    1023 [ +  + ][ +  + ]:   18787331 :         || dl_type == htons(ETH_TYPE_IPV6);
    1024                 :            : }
    1025                 :            : 
    1026                 :            : /* Tunnel header */
    1027                 :            : 
    1028                 :            : /* GRE protocol header */
    1029                 :            : struct gre_base_hdr {
    1030                 :            :     ovs_be16 flags;
    1031                 :            :     ovs_be16 protocol;
    1032                 :            : };
    1033                 :            : 
    1034                 :            : #define GRE_CSUM        0x8000
    1035                 :            : #define GRE_ROUTING     0x4000
    1036                 :            : #define GRE_KEY         0x2000
    1037                 :            : #define GRE_SEQ         0x1000
    1038                 :            : #define GRE_STRICT      0x0800
    1039                 :            : #define GRE_REC         0x0700
    1040                 :            : #define GRE_FLAGS       0x00F8
    1041                 :            : #define GRE_VERSION     0x0007
    1042                 :            : 
    1043                 :            : /* VXLAN protocol header */
    1044                 :            : struct vxlanhdr {
    1045                 :            :     ovs_16aligned_be32 vx_flags;
    1046                 :            :     ovs_16aligned_be32 vx_vni;
    1047                 :            : };
    1048                 :            : 
    1049                 :            : #define VXLAN_FLAGS 0x08000000  /* struct vxlanhdr.vx_flags required value. */
    1050                 :            : 
    1051                 :            : void ipv6_format_addr(const struct in6_addr *addr, struct ds *);
    1052                 :            : void ipv6_format_addr_bracket(const struct in6_addr *addr, struct ds *,
    1053                 :            :                               bool bracket);
    1054                 :            : void ipv6_format_mapped(const struct in6_addr *addr, struct ds *);
    1055                 :            : void ipv6_format_masked(const struct in6_addr *addr,
    1056                 :            :                         const struct in6_addr *mask, struct ds *);
    1057                 :            : const char * ipv6_string_mapped(char *addr_str, const struct in6_addr *addr);
    1058                 :            : struct in6_addr ipv6_addr_bitand(const struct in6_addr *src,
    1059                 :            :                                  const struct in6_addr *mask);
    1060                 :            : struct in6_addr ipv6_addr_bitxor(const struct in6_addr *a,
    1061                 :            :                                  const struct in6_addr *b);
    1062                 :            : bool ipv6_is_zero(const struct in6_addr *a);
    1063                 :            : struct in6_addr ipv6_create_mask(int mask);
    1064                 :            : int ipv6_count_cidr_bits(const struct in6_addr *netmask);
    1065                 :            : bool ipv6_is_cidr(const struct in6_addr *netmask);
    1066                 :            : 
    1067                 :            : bool ipv6_parse(const char *s, struct in6_addr *ip);
    1068                 :            : char *ipv6_parse_masked(const char *s, struct in6_addr *ipv6,
    1069                 :            :                         struct in6_addr *mask);
    1070                 :            : char *ipv6_parse_cidr(const char *s, struct in6_addr *ip, unsigned int *plen)
    1071                 :            :     OVS_WARN_UNUSED_RESULT;
    1072                 :            : char *ipv6_parse_masked_len(const char *s, int *n, struct in6_addr *ipv6,
    1073                 :            :                             struct in6_addr *mask);
    1074                 :            : char *ipv6_parse_cidr_len(const char *s, int *n, struct in6_addr *ip,
    1075                 :            :                           unsigned int *plen)
    1076                 :            :     OVS_WARN_UNUSED_RESULT;
    1077                 :            : 
    1078                 :            : void *eth_compose(struct dp_packet *, const struct eth_addr eth_dst,
    1079                 :            :                   const struct eth_addr eth_src, uint16_t eth_type,
    1080                 :            :                   size_t size);
    1081                 :            : void *snap_compose(struct dp_packet *, const struct eth_addr eth_dst,
    1082                 :            :                    const struct eth_addr eth_src,
    1083                 :            :                    unsigned int oui, uint16_t snap_type, size_t size);
    1084                 :            : void packet_set_ipv4(struct dp_packet *, ovs_be32 src, ovs_be32 dst, uint8_t tos,
    1085                 :            :                      uint8_t ttl);
    1086                 :            : void packet_set_ipv6(struct dp_packet *, const ovs_be32 src[4],
    1087                 :            :                      const ovs_be32 dst[4], uint8_t tc,
    1088                 :            :                      ovs_be32 fl, uint8_t hlmit);
    1089                 :            : void packet_set_tcp_port(struct dp_packet *, ovs_be16 src, ovs_be16 dst);
    1090                 :            : void packet_set_udp_port(struct dp_packet *, ovs_be16 src, ovs_be16 dst);
    1091                 :            : void packet_set_sctp_port(struct dp_packet *, ovs_be16 src, ovs_be16 dst);
    1092                 :            : void packet_set_icmp(struct dp_packet *, uint8_t type, uint8_t code);
    1093                 :            : void packet_set_nd(struct dp_packet *, const ovs_be32 target[4],
    1094                 :            :                    const struct eth_addr sll, const struct eth_addr tll);
    1095                 :            : 
    1096                 :            : void packet_format_tcp_flags(struct ds *, uint16_t);
    1097                 :            : const char *packet_tcp_flag_to_string(uint32_t flag);
    1098                 :            : void compose_arp__(struct dp_packet *);
    1099                 :            : void compose_arp(struct dp_packet *, uint16_t arp_op,
    1100                 :            :                  const struct eth_addr arp_sha,
    1101                 :            :                  const struct eth_addr arp_tha, bool broadcast,
    1102                 :            :                  ovs_be32 arp_spa, ovs_be32 arp_tpa);
    1103                 :            : void compose_nd_ns(struct dp_packet *, const struct eth_addr eth_src,
    1104                 :            :                    const struct in6_addr *ipv6_src,
    1105                 :            :                    const struct in6_addr *ipv6_dst);
    1106                 :            : void compose_nd_na(struct dp_packet *, const struct eth_addr eth_src,
    1107                 :            :                    const struct eth_addr eth_dst,
    1108                 :            :                    const struct in6_addr *ipv6_src,
    1109                 :            :                    const struct in6_addr *ipv6_dst,
    1110                 :            :                    ovs_be32 rso_flags);
    1111                 :            : uint32_t packet_csum_pseudoheader(const struct ip_header *);
    1112                 :            : void IP_ECN_set_ce(struct dp_packet *pkt, bool is_ipv6);
    1113                 :            : 
    1114                 :            : #endif /* packets.h */

Generated by: LCOV version 1.12