|
@@ -0,0 +1,226 @@
|
|
1
|
+#include <string.h>
|
|
2
|
+#include <stdint.h>
|
|
3
|
+#include <byteswap.h>
|
|
4
|
+#include <gpxe/in.h>
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+#include <ip.h>
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+#include <gpxe/if_ether.h>
|
|
11
|
+#include <gpxe/pkbuff.h>
|
|
12
|
+#include <gpxe/netdevice.h>
|
|
13
|
+#include "../proto/uip/uip.h"
|
|
14
|
+
|
|
15
|
+/** @file
|
|
16
|
+ *
|
|
17
|
+ * IPv4 protocol
|
|
18
|
+ *
|
|
19
|
+ * The gPXE IP stack is currently implemented on top of the uIP
|
|
20
|
+ * protocol stack. This file provides wrappers around uIP so that
|
|
21
|
+ * higher-level protocol implementations do not need to talk directly
|
|
22
|
+ * to uIP (which has a somewhat baroque API).
|
|
23
|
+ *
|
|
24
|
+ */
|
|
25
|
+
|
|
26
|
+/** An IPv4 routing table entry */
|
|
27
|
+struct ipv4_route {
|
|
28
|
+ /** Network address */
|
|
29
|
+ struct in_addr network;
|
|
30
|
+ /** Subnet mask */
|
|
31
|
+ struct in_addr netmask;
|
|
32
|
+ /** Gateway address */
|
|
33
|
+ struct in_addr gateway;
|
|
34
|
+ /** Gateway device */
|
|
35
|
+ struct in_addr gatewaydev;
|
|
36
|
+};
|
|
37
|
+
|
|
38
|
+enum {
|
|
39
|
+ STATIC_SINGLE_NETDEV_ROUTE = 0,
|
|
40
|
+ DEFAULT_ROUTE,
|
|
41
|
+ NUM_ROUTES
|
|
42
|
+};
|
|
43
|
+
|
|
44
|
+/** IPv4 routing table */
|
|
45
|
+static struct ipv4_route routing_table[NUM_ROUTES];
|
|
46
|
+
|
|
47
|
+#define routing_table_end ( routing_table + NUM_ROUTES )
|
|
48
|
+
|
|
49
|
+#if 0
|
|
50
|
+/**
|
|
51
|
+ * Set IP address
|
|
52
|
+ *
|
|
53
|
+ */
|
|
54
|
+void set_ipaddr ( struct in_addr address ) {
|
|
55
|
+ union {
|
|
56
|
+ struct in_addr address;
|
|
57
|
+ uint16_t uip_address[2];
|
|
58
|
+ } u;
|
|
59
|
+
|
|
60
|
+ u.address = address;
|
|
61
|
+ uip_sethostaddr ( u.uip_address );
|
|
62
|
+}
|
|
63
|
+
|
|
64
|
+/**
|
|
65
|
+ * Set netmask
|
|
66
|
+ *
|
|
67
|
+ */
|
|
68
|
+void set_netmask ( struct in_addr address ) {
|
|
69
|
+ union {
|
|
70
|
+ struct in_addr address;
|
|
71
|
+ uint16_t uip_address[2];
|
|
72
|
+ } u;
|
|
73
|
+
|
|
74
|
+ u.address = address;
|
|
75
|
+ uip_setnetmask ( u.uip_address );
|
|
76
|
+}
|
|
77
|
+
|
|
78
|
+/**
|
|
79
|
+ * Set default gateway
|
|
80
|
+ *
|
|
81
|
+ */
|
|
82
|
+void set_gateway ( struct in_addr address ) {
|
|
83
|
+ union {
|
|
84
|
+ struct in_addr address;
|
|
85
|
+ uint16_t uip_address[2];
|
|
86
|
+ } u;
|
|
87
|
+
|
|
88
|
+ u.address = address;
|
|
89
|
+ uip_setdraddr ( u.uip_address );
|
|
90
|
+}
|
|
91
|
+
|
|
92
|
+/**
|
|
93
|
+ * Run the TCP/IP stack
|
|
94
|
+ *
|
|
95
|
+ * Call this function in a loop in order to allow TCP/IP processing to
|
|
96
|
+ * take place. This call takes the stack through a single iteration;
|
|
97
|
+ * it will typically be used in a loop such as
|
|
98
|
+ *
|
|
99
|
+ * @code
|
|
100
|
+ *
|
|
101
|
+ * struct tcp_connection *my_connection;
|
|
102
|
+ * ...
|
|
103
|
+ * tcp_connect ( my_connection );
|
|
104
|
+ * while ( ! my_connection->finished ) {
|
|
105
|
+ * run_tcpip();
|
|
106
|
+ * }
|
|
107
|
+ *
|
|
108
|
+ * @endcode
|
|
109
|
+ *
|
|
110
|
+ * where @c my_connection->finished is set by one of the connection's
|
|
111
|
+ * #tcp_operations methods to indicate completion.
|
|
112
|
+ */
|
|
113
|
+void run_tcpip ( void ) {
|
|
114
|
+ void *data;
|
|
115
|
+ size_t len;
|
|
116
|
+ uint16_t type;
|
|
117
|
+ int i;
|
|
118
|
+
|
|
119
|
+ if ( netdev_poll ( 1, &data, &len ) ) {
|
|
120
|
+ /* We have data */
|
|
121
|
+ memcpy ( uip_buf, data, len );
|
|
122
|
+ uip_len = len;
|
|
123
|
+ type = ntohs ( *( ( uint16_t * ) ( uip_buf + 12 ) ) );
|
|
124
|
+ if ( type == UIP_ETHTYPE_ARP ) {
|
|
125
|
+ uip_arp_arpin();
|
|
126
|
+ } else {
|
|
127
|
+ uip_arp_ipin();
|
|
128
|
+ uip_input();
|
|
129
|
+ }
|
|
130
|
+ if ( uip_len > 0 )
|
|
131
|
+ uip_transmit();
|
|
132
|
+ } else {
|
|
133
|
+ for ( i = 0 ; i < UIP_CONNS ; i++ ) {
|
|
134
|
+ uip_periodic ( i );
|
|
135
|
+ if ( uip_len > 0 )
|
|
136
|
+ uip_transmit();
|
|
137
|
+ }
|
|
138
|
+ }
|
|
139
|
+}
|
|
140
|
+#endif
|
|
141
|
+
|
|
142
|
+/**
|
|
143
|
+ * Process incoming IP packets
|
|
144
|
+ *
|
|
145
|
+ * @v pkb Packet buffer
|
|
146
|
+ * @ret rc Return status code
|
|
147
|
+ *
|
|
148
|
+ * This handles IP packets by handing them off to the uIP protocol
|
|
149
|
+ * stack.
|
|
150
|
+ */
|
|
151
|
+static int ipv4_rx ( struct pk_buff *pkb ) {
|
|
152
|
+
|
|
153
|
+ /* Transfer to uIP buffer. Horrendously space-inefficient,
|
|
154
|
+ * but will do as a proof-of-concept for now.
|
|
155
|
+ */
|
|
156
|
+ memcpy ( uip_buf, pkb->data, pkb_len ( pkb ) );
|
|
157
|
+
|
|
158
|
+ /* Hand to uIP for processing */
|
|
159
|
+ uip_input ();
|
|
160
|
+ if ( uip_len > 0 ) {
|
|
161
|
+ pkb_empty ( pkb );
|
|
162
|
+ pkb_put ( pkb, uip_len );
|
|
163
|
+ memcpy ( pkb->data, uip_buf, uip_len );
|
|
164
|
+ if ( net_transmit ( pkb ) != 0 )
|
|
165
|
+ free_pkb ( pkb );
|
|
166
|
+ } else {
|
|
167
|
+ free_pkb ( pkb );
|
|
168
|
+ }
|
|
169
|
+ return 0;
|
|
170
|
+}
|
|
171
|
+
|
|
172
|
+/**
|
|
173
|
+ * Perform IP layer routing
|
|
174
|
+ *
|
|
175
|
+ * @v pkb Packet buffer
|
|
176
|
+ * @ret source Network-layer source address
|
|
177
|
+ * @ret dest Network-layer destination address
|
|
178
|
+ * @ret rc Return status code
|
|
179
|
+ */
|
|
180
|
+static int ipv4_route ( const struct pk_buff *pkb,
|
|
181
|
+ struct net_header *nethdr ) {
|
|
182
|
+ struct iphdr *iphdr = pkb->data;
|
|
183
|
+ struct in_addr *source = ( struct in_addr * ) nethdr->source_net_addr;
|
|
184
|
+ struct in_addr *dest = ( struct in_addr * ) nethdr->dest_net_addr;
|
|
185
|
+ struct ipv4_route *route;
|
|
186
|
+
|
|
187
|
+ /* Route IP packet according to routing table */
|
|
188
|
+ source->s_addr = INADDR_NONE;
|
|
189
|
+ dest->s_addr = iphdr->dest.s_addr;
|
|
190
|
+ for ( route = routing_table ; route < routing_table_end ; route++ ) {
|
|
191
|
+ if ( ( dest->s_addr & route->netmask.s_addr )
|
|
192
|
+ == route->network.s_addr ) {
|
|
193
|
+ source->s_addr = route->gatewaydev.s_addr;
|
|
194
|
+ if ( route->gateway.s_addr )
|
|
195
|
+ dest->s_addr = route->gateway.s_addr;
|
|
196
|
+ break;
|
|
197
|
+ }
|
|
198
|
+ }
|
|
199
|
+
|
|
200
|
+ /* Set broadcast and multicast flags as applicable */
|
|
201
|
+ nethdr->dest_flags = 0;
|
|
202
|
+ if ( dest->s_addr == htonl ( INADDR_BROADCAST ) ) {
|
|
203
|
+ nethdr->dest_flags = NETADDR_FL_BROADCAST;
|
|
204
|
+ } else if ( IN_MULTICAST ( dest->s_addr ) ) {
|
|
205
|
+ nethdr->dest_flags = NETADDR_FL_MULTICAST;
|
|
206
|
+ }
|
|
207
|
+
|
|
208
|
+ return 0;
|
|
209
|
+}
|
|
210
|
+
|
|
211
|
+/** IPv4 protocol */
|
|
212
|
+struct net_protocol ipv4_protocol = {
|
|
213
|
+ .net_proto = ETH_P_IP,
|
|
214
|
+ .net_addr_len = sizeof ( struct in_addr ),
|
|
215
|
+ .rx = ipv4_rx,
|
|
216
|
+ .route = ipv4_route,
|
|
217
|
+};
|
|
218
|
+
|
|
219
|
+NET_PROTOCOL ( ipv4_protocol );
|
|
220
|
+
|
|
221
|
+/** IPv4 address for the static single net device */
|
|
222
|
+struct net_address static_single_ipv4_address = {
|
|
223
|
+ .net_protocol = &ipv4_protocol,
|
|
224
|
+};
|
|
225
|
+
|
|
226
|
+STATIC_SINGLE_NETDEV_ADDRESS ( static_single_ipv4_address );
|