xref: /openbmc/qemu/net/util.h (revision 764a6ee9)
1 /*
2  * QEMU System Emulator
3  *
4  * Copyright (c) 2003-2008 Fabrice Bellard
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 
25 #ifndef QEMU_NET_UTIL_H
26 #define QEMU_NET_UTIL_H
27 
28 
29 /*
30  * Structure of an internet header, naked of options.
31  */
32 struct ip {
33 #if HOST_BIG_ENDIAN
34     uint8_t ip_v:4,         /* version */
35             ip_hl:4;        /* header length */
36 #else
37     uint8_t ip_hl:4,        /* header length */
38             ip_v:4;         /* version */
39 #endif
40     uint8_t ip_tos;         /* type of service */
41     uint16_t ip_len;        /* total length */
42     uint16_t ip_id;         /* identification */
43     uint16_t ip_off;        /* fragment offset field */
44 #define IP_DF 0x4000        /* don't fragment flag */
45 #define IP_MF 0x2000        /* more fragments flag */
46 #define IP_OFFMASK 0x1fff   /* mask for fragmenting bits */
47     uint8_t ip_ttl;         /* time to live */
48     uint8_t ip_p;           /* protocol */
49     uint16_t ip_sum;        /* checksum */
50     struct in_addr ip_src, ip_dst;  /* source and dest address */
51 } QEMU_PACKED;
52 
53 static inline bool in6_equal_net(const struct in6_addr *a,
54                                  const struct in6_addr *b,
55                                  int prefix_len)
56 {
57     if (memcmp(a, b, prefix_len / 8) != 0) {
58         return 0;
59     }
60 
61     if (prefix_len % 8 == 0) {
62         return 1;
63     }
64 
65     return a->s6_addr[prefix_len / 8] >> (8 - (prefix_len % 8))
66         == b->s6_addr[prefix_len / 8] >> (8 - (prefix_len % 8));
67 }
68 
69 #define TCPS_CLOSED             0       /* closed */
70 #define TCPS_LISTEN             1       /* listening for connection */
71 #define TCPS_SYN_SENT           2       /* active, have sent syn */
72 #define TCPS_SYN_RECEIVED       3       /* have send and received syn */
73 /* states < TCPS_ESTABLISHED are those where connections not established */
74 #define TCPS_ESTABLISHED        4       /* established */
75 #define TCPS_CLOSE_WAIT         5       /* rcvd fin, waiting for close */
76 /* states > TCPS_CLOSE_WAIT are those where user has closed */
77 #define TCPS_FIN_WAIT_1         6       /* have closed, sent fin */
78 #define TCPS_CLOSING            7       /* closed xchd FIN; await FIN ACK */
79 #define TCPS_LAST_ACK           8       /* had fin and close; await FIN ACK */
80 /* states > TCPS_CLOSE_WAIT && < TCPS_FIN_WAIT_2 await ACK of FIN */
81 #define TCPS_FIN_WAIT_2         9       /* have closed, fin is acked */
82 #define TCPS_TIME_WAIT          10      /* in 2*msl quiet wait after close */
83 
84 int net_parse_macaddr(uint8_t *macaddr, const char *p);
85 
86 #endif /* QEMU_NET_UTIL_H */
87