1 #ifndef	_IPXE_IN_H
2 #define	_IPXE_IN_H
3 
4 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
5 
6 #include <stdint.h>
7 #include <byteswap.h>
8 #include <ipxe/socket.h>
9 
10 /* Protocol numbers */
11 
12 #define IP_ICMP		1
13 #define IP_TCP		6
14 #define IP_UDP		17
15 #define IP_ICMP6	58
16 
17 /* IP address constants */
18 
19 #define INADDR_NONE		htonl ( 0xffffffff )
20 
21 #define INADDR_BROADCAST	htonl ( 0xffffffff )
22 
23 #define	INADDR_NET_CLASSA	htonl ( 0xff000000 )
24 #define	INADDR_NET_CLASSB	htonl ( 0xffff0000 )
25 #define	INADDR_NET_CLASSC	htonl ( 0xffffff00 )
26 
27 #define	IN_IS_CLASSA( addr ) \
28 	( ( (addr) & htonl ( 0x80000000 ) ) == htonl ( 0x00000000 ) )
29 #define	IN_IS_CLASSB( addr ) \
30 	( ( (addr) & htonl ( 0xc0000000 ) ) == htonl ( 0x80000000 ) )
31 #define	IN_IS_CLASSC( addr ) \
32 	( ( (addr) & htonl ( 0xe0000000 ) ) == htonl ( 0xc0000000 ) )
33 #define IN_IS_MULTICAST( addr ) \
34 	( ( (addr) & htonl ( 0xf0000000 ) ) == htonl ( 0xe0000000 ) )
35 
36 /**
37  * IP address structure
38  */
39 struct in_addr {
40 	uint32_t	s_addr;
41 };
42 
43 typedef struct in_addr in_addr;
44 
45 /**
46  * IP6 address structure
47  */
48 struct in6_addr {
49         union {
50                 uint8_t u6_addr8[16];
51                 uint16_t u6_addr16[8];
52                 uint32_t u6_addr32[4];
53         } in6_u;
54 #define s6_addr         in6_u.u6_addr8
55 #define s6_addr16       in6_u.u6_addr16
56 #define s6_addr32       in6_u.u6_addr32
57 };
58 
59 #define IN6_IS_ADDR_UNSPECIFIED( addr )					\
60 	( ( ( ( ( const uint32_t * ) (addr) )[0] ) |			\
61 	    ( ( ( const uint32_t * ) (addr) )[1] ) |			\
62 	    ( ( ( const uint32_t * ) (addr) )[2] ) |			\
63 	    ( ( ( const uint32_t * ) (addr) )[3] ) ) == 0 )
64 
65 #define IN6_IS_ADDR_MULTICAST( addr )					\
66 	( *( ( const uint8_t * ) (addr) ) == 0xff )
67 
68 #define IN6_IS_ADDR_LINKLOCAL( addr )					\
69 	( ( *( ( const uint16_t * ) (addr) ) & htons ( 0xffc0 ) ) ==	\
70 	  htons ( 0xfe80 ) )
71 
72 #define IN6_IS_ADDR_SITELOCAL( addr )					\
73 	( ( *( ( const uint16_t * ) (addr) ) & htons ( 0xffc0 ) ) ==	\
74 	  htons ( 0xfec0 ) )
75 
76 #define IN6_IS_ADDR_ULA( addr )						\
77 	( ( *( ( const uint8_t * ) (addr) ) & 0xfe ) == 0xfc )
78 
79 /**
80  * IPv4 socket address
81  */
82 struct sockaddr_in {
83 	/** Socket address family (part of struct @c sockaddr)
84 	 *
85 	 * Always set to @c AF_INET for IPv4 addresses
86 	 */
87 	sa_family_t sin_family;
88 	/** Flags (part of struct @c sockaddr_tcpip) */
89 	uint16_t sin_flags;
90 	/** TCP/IP port (part of struct @c sockaddr_tcpip) */
91 	uint16_t sin_port;
92 	/** Scope ID (part of struct @c sockaddr_tcpip)
93 	 *
94 	 * For multicast addresses, this is the network device index.
95 	 */
96         uint16_t sin_scope_id;
97 	/** IPv4 address */
98 	struct in_addr sin_addr;
99 	/** Padding
100 	 *
101 	 * This ensures that a struct @c sockaddr_in is large enough
102 	 * to hold a socket address for any TCP/IP address family.
103 	 */
104 	char pad[ sizeof ( struct sockaddr ) -
105 		  ( sizeof ( sa_family_t ) /* sin_family */ +
106 		    sizeof ( uint16_t ) /* sin_flags */ +
107 		    sizeof ( uint16_t ) /* sin_port */ +
108 		    sizeof ( uint16_t ) /* sin_scope_id */ +
109 		    sizeof ( struct in_addr ) /* sin_addr */ ) ];
110 } __attribute__ (( packed, may_alias ));
111 
112 /**
113  * IPv6 socket address
114  */
115 struct sockaddr_in6 {
116 	/** Socket address family (part of struct @c sockaddr)
117 	 *
118 	 * Always set to @c AF_INET6 for IPv6 addresses
119 	 */
120 	sa_family_t sin6_family;
121 	/** Flags (part of struct @c sockaddr_tcpip) */
122 	uint16_t sin6_flags;
123 	/** TCP/IP port (part of struct @c sockaddr_tcpip) */
124 	uint16_t sin6_port;
125 	/** Scope ID (part of struct @c sockaddr_tcpip)
126 	 *
127 	 * For link-local or multicast addresses, this is the network
128 	 * device index.
129 	 */
130         uint16_t sin6_scope_id;
131 	/** IPv6 address */
132         struct in6_addr sin6_addr;
133 	/** Padding
134 	 *
135 	 * This ensures that a struct @c sockaddr_in6 is large
136 	 * enough to hold a socket address for any TCP/IP address
137 	 * family.
138 	 */
139 	char pad[ sizeof ( struct sockaddr ) -
140 		  ( sizeof ( sa_family_t ) /* sin6_family */ +
141 		    sizeof ( uint16_t ) /* sin6_flags */ +
142 		    sizeof ( uint16_t ) /* sin6_port */ +
143 		    sizeof ( uint16_t ) /* sin6_scope_id */ +
144 		    sizeof ( struct in6_addr ) /* sin6_addr */ ) ];
145 } __attribute__ (( packed, may_alias ));
146 
147 extern int inet_aton ( const char *cp, struct in_addr *inp );
148 extern char * inet_ntoa ( struct in_addr in );
149 extern int inet6_aton ( const char *string, struct in6_addr *in );
150 extern char * inet6_ntoa ( const struct in6_addr *in );
151 
152 #endif	/* _IPXE_IN_H */
153