Quellcode durchsuchen

Renamed net/interface.c and include/gpxe/interface.h to net/tcpip_if.c and include/gpxe/tcpip_if.h respectively. Made changes in the other files.

tags/v0.9.3
Nikhil Chandru Rao vor 18 Jahren
Ursprung
Commit
c9ea710930
6 geänderte Dateien mit 273 neuen und 15 gelöschten Zeilen
  1. 3
    1
      src/include/gpxe/ip.h
  2. 91
    0
      src/include/gpxe/tcpip_if.h
  3. 5
    0
      src/include/gpxe/udp.h
  4. 32
    10
      src/net/ipv4.c
  5. 136
    0
      src/net/tcpip_if.c
  6. 6
    4
      src/net/udp.c

+ 3
- 1
src/include/gpxe/ip.h Datei anzeigen

@@ -32,6 +32,7 @@ struct ipv4_pseudo_header {
32 32
 struct pk_buff;
33 33
 struct net_device;
34 34
 struct net_protocol;
35
+struct tcpip_protocol;
35 36
 
36 37
 extern struct net_protocol ipv4_protocol;
37 38
 
@@ -41,6 +42,7 @@ extern int add_ipv4_address ( struct net_device *netdev,
41 42
 extern void del_ipv4_address ( struct net_device *netdev );
42 43
 
43 44
 extern int ipv4_uip_tx ( struct pk_buff *pkb );
44
-extern int ipv4_tx ( struct pk_buff *pkb, uint16_t trans_proto, struct in_addr *dest );
45
+extern int ipv4_tx ( struct pk_buff *pkb, struct tcpip_protocol *tcpip,
46
+		     struct in_addr *dest );
45 47
 
46 48
 #endif /* _GPXE_IP_H */

+ 91
- 0
src/include/gpxe/tcpip_if.h Datei anzeigen

@@ -0,0 +1,91 @@
1
+#ifndef _GPXE_INTERFACE_H
2
+#define _GPXE_INTERFACE_H
3
+
4
+/** @file
5
+ *
6
+ * Transport-network layer interface
7
+ *
8
+ */
9
+
10
+#include <stdint.h>
11
+#include <gpxe/in.h>
12
+#include <gpxe/tables.h>
13
+
14
+struct pk_buff;
15
+struct net_protocol;
16
+struct tcpip_protocol;
17
+struct tcpip_net_protocol;
18
+
19
+/** 
20
+ * A transport-layer protocol of the TCPIP stack (eg. UDP, TCP, etc)
21
+ */
22
+struct tcpip_protocol {
23
+	/** Protocol name */
24
+	const char *name;
25
+       	/**
26
+         * Process received packet
27
+         *
28
+         * @v pkb       Packet buffer
29
+         * @v netdev    Network device
30
+         * @v ll_source Link-layer source address
31
+         *
32
+         * This method takes ownership of the packet buffer.
33
+         */
34
+        void ( * rx ) ( struct pk_buff *pkb, struct in_addr *src_net_addr, struct in_addr *dest_net_addr );
35
+        /** 
36
+	 * Transport-layer protocol number
37
+	 *
38
+	 * This is a constant of the type IP_XXX
39
+         */
40
+        uint8_t trans_proto;
41
+	/**
42
+	 * Checksum offset
43
+	 *
44
+	 * A negative number indicates that the protocol does not require
45
+	 * checksumming to be performed by the network layer. A positive number is
46
+	 * the offset of the checksum field in the transport-layer header.
47
+	 */
48
+	int csum_offset;
49
+};
50
+
51
+/**
52
+ * A TCPIP supporting network-layer protocol
53
+ */
54
+struct tcpip_net_protocol {
55
+	/** Network protocol */
56
+	struct net_protocol *net_protocol;
57
+	/** Network address family */
58
+	sa_family_t sa_family;
59
+	/** Complete transport-layer checksum calculation
60
+	 *
61
+	 * @v pkb		Packet buffer
62
+	 * @v tcpip		Transport-layer protocol
63
+	 *
64
+	 */
65
+	void ( * tx_csum ) ( struct pk_buff *pkb,
66
+			     struct tcpip_protocol *tcpip );
67
+};
68
+
69
+/**
70
+ * Register a transport-layer protocol
71
+ *
72
+ * @v protocol          Transport-layer protocol
73
+ */
74
+#define TCPIP_PROTOCOL( protocol ) \
75
+        struct tcpip_protocol protocol __table ( tcpip_protocols, 01 )
76
+
77
+#define TCPIP_NET_PROTOCOL( protocol ) \
78
+        struct tcpip_net_protocol protocol __table ( tcpip_net_protocols, 01 )
79
+
80
+extern void trans_rx ( struct pk_buff *pkb, uint8_t trans_proto, 
81
+		       struct in_addr *src, struct in_addr *dest );
82
+
83
+extern int trans_tx ( struct pk_buff *pkb, struct tcpip_protocol *tcpip, 
84
+		      struct sockaddr *dest );
85
+
86
+extern uint16_t calc_chksum ( void *data, size_t len );
87
+
88
+extern struct tcpip_protocol * find_tcpip_protocol ( uint8_t trans_proto );
89
+extern struct tcpip_net_protocol * find_tcpip_net_protocol ( sa_family_t sa_family );
90
+
91
+#endif /* _GPXE_INTERFACE_H */

+ 5
- 0
src/include/gpxe/udp.h Datei anzeigen

@@ -74,6 +74,11 @@ struct udp_connection {
74 74
  */
75 75
 static LIST_HEAD ( udp_conns );
76 76
 
77
+/**
78
+ * UDP protocol
79
+ */
80
+extern struct tcpip_protocol udp_protocol;
81
+
77 82
 /**
78 83
  * Functions provided to the application layer
79 84
  */

+ 32
- 10
src/net/ipv4.c Datei anzeigen

@@ -12,7 +12,7 @@
12 12
 #include <gpxe/netdevice.h>
13 13
 #include "uip/uip.h"
14 14
 #include <gpxe/ip.h>
15
-#include <gpxe/interface.h>
15
+#include <gpxe/tcpip_if.h>
16 16
 
17 17
 /** @file
18 18
  *
@@ -103,6 +103,7 @@ void del_ipv4_address ( struct net_device *netdev ) {
103 103
  * @v iphdr	IPv4 header
104 104
  */
105 105
 static void ipv4_dump ( struct iphdr *iphdr __unused ) {
106
+
106 107
 	DBG ( "IP4 header at %p+%zx\n", iphdr, sizeof ( *iphdr ) );
107 108
 	DBG ( "\tVersion = %d\n", ( iphdr->verhdrlen & IP_MASK_VER ) / 16 );
108 109
 	DBG ( "\tHeader length = %d\n", iphdr->verhdrlen & IP_MASK_HLEN );
@@ -120,12 +121,19 @@ static void ipv4_dump ( struct iphdr *iphdr __unused ) {
120 121
 
121 122
 /**
122 123
  * Complete the transport-layer checksum
124
+ *
125
+ * @v pkb	Packet buffer
126
+ * @v tcpip	Transport-layer protocol
127
+ *
128
+ * This function calculates the tcpip 
123 129
  */
124
-void ipv4_tx_csum ( struct pk_buff *pkb, uint8_t trans_proto ) {
130
+void ipv4_tx_csum ( struct pk_buff *pkb, struct tcpip_protocol *tcpip ) {
125 131
 
126 132
 	struct iphdr *iphdr = pkb->data;
127 133
 	struct ipv4_pseudo_header pshdr;
128
-	void *csum_offset = iphdr + sizeof ( *iphdr ) + ( trans_proto == IP_UDP ? 6 : 16 );
134
+	void *csum_offset = iphdr + sizeof ( *iphdr ) + tcpip->csum_offset;
135
+	uint16_t partial_csum = *( ( uint16_t* ) csum_offset );
136
+	uint16_t csum;
129 137
 
130 138
 	/* Calculate pseudo header */
131 139
 	pshdr.src = iphdr->src;
@@ -135,14 +143,19 @@ void ipv4_tx_csum ( struct pk_buff *pkb, uint8_t trans_proto ) {
135 143
 	pshdr.len = htons ( pkb_len ( pkb ) - sizeof ( *iphdr ) );
136 144
 
137 145
 	/* Update the checksum value */
138
-	*( ( uint16_t* ) csum_offset ) = *( ( uint16_t* ) csum_offset ) + calc_chksum ( &pshdr, IP_PSHLEN );
146
+	csum = partial_csum + calc_chksum ( &pshdr, sizeof ( pshdr ) );
147
+	memcpy ( csum_offset, &csum, 2 );
139 148
 }
140 149
 
141 150
 /**
142 151
  * Calculate the transport-layer checksum while processing packets
143 152
  */
144
-uint16_t ipv4_rx_csum ( struct pk_buff *pkb __unused, uint8_t trans_proto __unused ) {
145
-	/** This function needs to be implemented. Until then, it will return 0xffffffff every time */
153
+uint16_t ipv4_rx_csum ( struct pk_buff *pkb __unused,
154
+			uint8_t trans_proto __unused ) {
155
+	/** 
156
+	 * This function needs to be implemented. Until then, it will return
157
+	 * 0xffffffff every time
158
+	 */
146 159
 	return 0xffff;
147 160
 }
148 161
 
@@ -222,13 +235,14 @@ int ipv4_uip_tx ( struct pk_buff *pkb ) {
222 235
  * Transmit IP packet (without uIP)
223 236
  *
224 237
  * @v pkb		Packet buffer
225
- * @v trans_proto	Transport-layer protocol number
238
+ * @v tcpip		Transport-layer protocol
226 239
  * @v dest		Destination network-layer address
227 240
  * @ret rc		Status
228 241
  *
229 242
  * This function expects a transport-layer segment and prepends the IP header
230 243
  */
231
-int ipv4_tx ( struct pk_buff *pkb, uint16_t trans_proto, struct in_addr *dest ) {
244
+int ipv4_tx ( struct pk_buff *pkb, struct tcpip_protocol *tcpip,
245
+	      struct in_addr *dest ) {
232 246
 	struct iphdr *iphdr = pkb_push ( pkb, sizeof ( *iphdr ) );
233 247
 	struct ipv4_miniroute *miniroute;
234 248
 	struct net_device *netdev = NULL;
@@ -244,7 +258,7 @@ int ipv4_tx ( struct pk_buff *pkb, uint16_t trans_proto, struct in_addr *dest )
244 258
 	iphdr->ident = htons ( next_ident++ );
245 259
 	iphdr->frags = 0;
246 260
 	iphdr->ttl = IP_TTL;
247
-	iphdr->protocol = trans_proto;
261
+	iphdr->protocol = tcpip->trans_proto;
248 262
 
249 263
 	/* Copy destination address */
250 264
 	iphdr->dest = *dest;
@@ -280,7 +294,7 @@ int ipv4_tx ( struct pk_buff *pkb, uint16_t trans_proto, struct in_addr *dest )
280 294
 	}
281 295
 
282 296
 	/* Calculate the transport layer checksum */
283
-	ipv4_tx_csum ( pkb, trans_proto );
297
+	ipv4_tx_csum ( pkb, tcpip );
284 298
 
285 299
 	/* Calculate header checksum, in network byte order */
286 300
 	iphdr->chksum = 0;
@@ -477,6 +491,14 @@ struct net_protocol ipv4_protocol = {
477 491
 
478 492
 NET_PROTOCOL ( ipv4_protocol );
479 493
 
494
+/** IPv4 TCPIP net protocol */
495
+struct tcpip_net_protocol ipv4_tcpip_protocol = {
496
+	.net_protocol = &ipv4_protocol,
497
+	.tx_csum = ipv4_tx_csum,
498
+};
499
+
500
+TCPIP_NET_PROTOCOL ( ipv4_tcpip_protocol );
501
+
480 502
 /** IPv4 ARP protocol */
481 503
 struct arp_net_protocol ipv4_arp_protocol = {
482 504
 	.net_protocol = &ipv4_protocol,

+ 136
- 0
src/net/tcpip_if.c Datei anzeigen

@@ -0,0 +1,136 @@
1
+#include <stdint.h>
2
+#include <string.h>
3
+#include <errno.h>
4
+#include <malloc.h>
5
+#include <byteswap.h>
6
+#include <gpxe/in.h>
7
+#include <gpxe/ip.h>
8
+#include <gpxe/pkbuff.h>
9
+#include <gpxe/tables.h>
10
+#include <gpxe/netdevice.h>
11
+#include <gpxe/tcpip_if.h>
12
+
13
+/** @file
14
+ *
15
+ * Transport-network layer interface
16
+ *
17
+ * This file contains functions and utilities for the transport-network layer interface
18
+ */
19
+
20
+/** Registered network-layer protocols that support TCPIP */
21
+static struct tcpip_net_protocol tcpip_net_protocols[0] __table_start ( tcpip_net_protocols );
22
+static struct tcpip_net_protocol tcpip_net_protocols_end[0] __table_end ( tcpip_net_protocols );
23
+
24
+struct tcpip_protocol;
25
+
26
+/** Registered transport-layer protocols that support TCPIP */
27
+static struct tcpip_protocol tcpip_protocols[0] __table_start ( tcpip_protocols );
28
+static struct tcpip_protocol tcpip_protocols_end[0] __table_end ( tcpip_protocols );
29
+
30
+/** Identify TCPIP network-layer protocol
31
+ *
32
+ * @v sa_family	 Network address family
33
+ * @ret tcpip	   Protocol supporting TCPIP, or NULL
34
+ */
35
+static struct tcpip_net_protocol * tcpip_find_protocol ( sa_family_t sa_family ) {
36
+	struct tcpip_net_protocol *tcpip_net;
37
+
38
+	for ( tcpip_net = tcpip_net_protocols; 
39
+		tcpip_net < tcpip_net_protocols_end; ++tcpip_net ) {
40
+		if ( tcpip_net->sa_family == sa_family ) {
41
+			return tcpip_net;
42
+		}
43
+	}
44
+	return NULL;
45
+}
46
+
47
+/** Identify TCPIP transport-layer protocol
48
+ *
49
+ * @v trans_proto	Transport-layer protocol number, IP_XXX
50
+ * @ret tcpip_protocol	Transport-layer protocol, or NULL
51
+ */
52
+struct tcpip_protocol* find_tcpip_protocol ( uint8_t trans_proto ) {
53
+	struct tcpip_protocol *tcpip;
54
+
55
+	for ( tcpip = tcpip_protocols; tcpip <= tcpip_protocols_end; 
56
+		++tcpip ) {
57
+		if ( tcpip->trans_proto == trans_proto ) {
58
+			return tcpip;
59
+		}
60
+	}
61
+	return NULL;
62
+}
63
+
64
+/** Process a received packet
65
+ *
66
+ * @v pkb		Packet buffer
67
+ * @v trans_proto	Transport-layer protocol number
68
+ * @v src		Source network-layer address
69
+ * @v dest		Destination network-layer address
70
+ *
71
+ * This function expects a transport-layer segment from the network-layer
72
+ */
73
+void trans_rx ( struct pk_buff *pkb, uint8_t trans_proto, struct in_addr *src,
74
+		struct in_addr *dest ) {
75
+	struct tcpip_protocol *tcpip;
76
+
77
+	/* Identify the transport layer protocol */
78
+	for ( tcpip = tcpip_protocols; tcpip <= tcpip_protocols_end; ++tcpip ) {
79
+		if ( tcpip->trans_proto == trans_proto ) {
80
+			DBG ( "Packet sent to %s module", tcpip->name );
81
+			tcpip->rx ( pkb, src, dest );
82
+		}
83
+	}
84
+}
85
+
86
+/** Transmit a transport-layer segment
87
+ *
88
+ * @v pkb		Packet buffer
89
+ * @v trans_proto	Transport-layer protocol
90
+ * @v sock		Destination socket address
91
+ * @ret			Status
92
+ */
93
+int trans_tx ( struct pk_buff *pkb, struct tcpip_protocol *tcpip,
94
+	       struct sockaddr *sock ) {
95
+
96
+	/* Identify the network layer protocol and send it using xxx_tx() */
97
+	switch ( sock->sa_family ) {
98
+	case AF_INET: /* IPv4 network family */
99
+		return ipv4_tx ( pkb, tcpip, &sock->sin.sin_addr );
100
+	case AF_INET6: /* IPv6 network family */
101
+		return ipv6_tx ( pkb, tcpip, &sock->sin6.sin6_addr );
102
+	}
103
+	DBG ( "Network family %d not supported", sock->sa_family );
104
+	return -EAFNOSUPPORT;
105
+}
106
+
107
+/**
108
+ * Calculate internet checksum
109
+ *
110
+ * @v data      Pointer to the data
111
+ * @v len       Length of data to be checksummed
112
+ * @ret chksum  16 bit internet checksum
113
+ *
114
+ * This function calculates the internet checksum (refer RFC1071) for "len"
115
+ * bytes beginning at the location "data"
116
+ */
117
+uint16_t calc_chksum ( void *data, size_t len ) {
118
+	register long sum = 0;
119
+	uint16_t checksum;
120
+	unsigned short *temp;
121
+	while ( len > 1 ) {
122
+		temp = (unsigned short*) data++;
123
+		sum += *temp;
124
+		len -= 2;
125
+	}
126
+	if ( len > 0 ) {
127
+		sum += *(unsigned char *)data;
128
+	}
129
+	while ( sum >> 16 ) {
130
+		sum = ( sum & 0xffff ) + ( sum >> 16 );
131
+	}
132
+	checksum = ~sum;
133
+	return checksum;
134
+}
135
+
136
+

+ 6
- 4
src/net/udp.c Datei anzeigen

@@ -6,11 +6,12 @@
6 6
 #include <errno.h>
7 7
 #include <gpxe/in.h>
8 8
 #include <gpxe/ip.h>
9
+#include <gpxe/ip6.h>
9 10
 #include <gpxe/udp.h>
10 11
 #include <gpxe/init.h>
11 12
 #include <gpxe/pkbuff.h>
12 13
 #include <gpxe/netdevice.h>
13
-#include <gpxe/interface.h>
14
+#include <gpxe/tcpip_if.h>
14 15
 
15 16
 /** @file
16 17
  *
@@ -149,7 +150,7 @@ int udp_send ( struct udp_connection *conn, const void *data, size_t len ) {
149 150
 	udp_dump ( udphdr );
150 151
 
151 152
 	/* Send it to the next layer for processing */
152
-	return trans_tx ( conn->tx_pkb, IP_UDP, sock );
153
+	return trans_tx ( conn->tx_pkb, &udp_protocol, sock );
153 154
 }
154 155
 
155 156
 /**
@@ -268,10 +269,11 @@ void udp_rx ( struct pk_buff *pkb, struct in_addr *src_net_addr __unused,
268 269
 	conn->udp_op->newdata ( conn, pkb->data, ulen - sizeof ( *udphdr ) );
269 270
 }
270 271
 
271
-struct trans_protocol udp_protocol  = {
272
+struct tcpip_protocol udp_protocol  = {
272 273
 	.name = "UDP",
273 274
 	.rx = udp_rx,
274 275
 	.trans_proto = IP_UDP,
276
+	.csum_offset = 6,
275 277
 };
276 278
 
277
-TRANS_PROTOCOL ( udp_protocol );
279
+TCPIP_PROTOCOL ( udp_protocol );

Laden…
Abbrechen
Speichern