ソースを参照

Fixed endianness of 16- and 32- bit values

tags/v0.9.3
Nikhil Chandru Rao 18年前
コミット
41b399d672
1個のファイルの変更40行の追加30行の削除
  1. 40
    30
      src/net/ipv4.c

+ 40
- 30
src/net/ipv4.c ファイルの表示

@@ -107,21 +107,19 @@ static void ipv4_dump ( struct iphdr *iphdr __unused ) {
107 107
 	DBG ( "\tVersion = %d\n", ( iphdr->verhdrlen & IP_MASK_VER ) / 16 );
108 108
 	DBG ( "\tHeader length = %d\n", iphdr->verhdrlen & IP_MASK_HLEN );
109 109
 	DBG ( "\tService = %d\n", iphdr->service );
110
-	DBG ( "\tTotal length = %d\n", iphdr->len );
111
-	DBG ( "\tIdent = %d\n", iphdr->ident );
112
-	DBG ( "\tFrags/Offset = %d\n", iphdr->frags );
110
+	DBG ( "\tTotal length = %d\n", ntohs ( iphdr->len ) );
111
+	DBG ( "\tIdent = %d\n", ntohs ( iphdr->ident ) );
112
+	DBG ( "\tFrags/Offset = %d\n", ntohs ( iphdr->frags ) );
113 113
 	DBG ( "\tIP TTL = %d\n", iphdr->ttl );
114 114
 	DBG ( "\tProtocol = %d\n", iphdr->protocol );
115
-	DBG ( "\tHeader Checksum (at %p) = %x\n", &iphdr->chksum,
116
-	      iphdr->chksum );
115
+	DBG ( "\tHeader Checksum (at %p) = %x\n", &iphdr->chksum, 
116
+				ntohs ( iphdr->chksum ) );
117 117
 	DBG ( "\tSource = %s\n", inet_ntoa ( iphdr->src ) );
118 118
 	DBG ( "\tDestination = %s\n", inet_ntoa ( iphdr->dest ) );
119 119
 }
120 120
 
121 121
 /**
122 122
  * Complete the transport-layer checksum
123
- *
124
- * Refer to the note made in net/interface.c about this function
125 123
  */
126 124
 void ipv4_tx_csum ( struct pk_buff *pkb, uint8_t trans_proto ) {
127 125
 
@@ -234,7 +232,7 @@ int ipv4_uip_tx ( struct pk_buff *pkb ) {
234 232
  * This function expects a transport-layer segment and prepends the IP header
235 233
  */
236 234
 int ipv4_tx ( struct pk_buff *pkb, uint16_t trans_proto, struct in_addr *dest ) {
237
-	struct iphdr *iphdr = pkb_push ( pkb, IP_HLEN );
235
+	struct iphdr *iphdr = pkb_push ( pkb, sizeof ( *iphdr ) );
238 236
 	struct ipv4_miniroute *miniroute;
239 237
 	struct net_device *netdev = NULL;
240 238
 	struct in_addr next_hop;
@@ -243,24 +241,23 @@ int ipv4_tx ( struct pk_buff *pkb, uint16_t trans_proto, struct in_addr *dest )
243 241
 	int rc;
244 242
 
245 243
 	/* Fill up the IP header, except source address */
246
-	iphdr->verhdrlen = ( IP_VER << 4 ) | ( IP_HLEN / 4 );	/* Version = 4, Header length = 5 */
247
-	iphdr->service = IP_TOS;				/* Service = 0, is not implemented */
248
-	iphdr->len = htons ( pkb_len ( pkb ) );			/* Total packet length, in network byte order */
249
-	iphdr->ident = next_ident++;				/* Identification number */
250
-	iphdr->frags = 0;					/* Fragmentation is not implemented at the host */
251
-	iphdr->ttl = IP_TTL;					/* Time to live */
252
-	iphdr->protocol = trans_proto;				/* Transport-layer protocol - IP_XXX */
244
+	iphdr->verhdrlen = ( IP_VER << 4 ) | ( sizeof ( *iphdr ) / 4 );
245
+	iphdr->service = IP_TOS;
246
+	iphdr->len = htons ( pkb_len ( pkb ) );	
247
+	iphdr->ident = htons ( next_ident++ );
248
+	iphdr->frags = 0;
249
+	iphdr->ttl = IP_TTL;
250
+	iphdr->protocol = trans_proto;
253 251
 
254
-	/* Calculate header checksum, in network byte order */
255
-	iphdr->chksum = 0;
256
-	iphdr->chksum = htons ( calc_chksum ( iphdr, IP_HLEN ) );
257 252
 	/* Copy destination address */
258
-	memcpy ( &iphdr->dest, dest, sizeof ( struct in_addr ) );
253
+	iphdr->dest = *dest;
259 254
 
260 255
 	/**
261
-	 * All fields in the IP header filled in except the source network address (which requires routing). As
262
-	 * the pseudo header requires the source address as well and updating the transport-layer checksum is
263
-	 * done after routing.
256
+	 * All fields in the IP header filled in except the source network
257
+	 * address (which requires routing) and the header checksum (which
258
+	 * requires the source network address). As the pseudo header requires
259
+	 * the source address as well and the transport-layer checksum is
260
+	 * updated after routing.
264 261
 	 *
265 262
 	 * Continue processing as in ipv4_uip_tx()
266 263
 	 */
@@ -288,6 +285,10 @@ int ipv4_tx ( struct pk_buff *pkb, uint16_t trans_proto, struct in_addr *dest )
288 285
 	/* Calculate the transport layer checksum */
289 286
 	ipv4_tx_csum ( pkb, trans_proto );
290 287
 
288
+	/* Calculate header checksum, in network byte order */
289
+	iphdr->chksum = 0;
290
+	iphdr->chksum = htons ( calc_chksum ( iphdr, sizeof ( *iphdr ) ) );
291
+
291 292
 	/* Print IP4 header for debugging */
292 293
 	ipv4_dump ( iphdr );
293 294
 
@@ -367,7 +368,8 @@ static int ipv4_uip_rx ( struct pk_buff *pkb,
367 368
  * @v netdev	Network device
368 369
  * @v ll_source	Link-layer destination source
369 370
  *
370
- * This function expects an IP4 network datagram. It will process the headers and send it to the transport layer
371
+ * This function expects an IP4 network datagram. It processes the headers 
372
+ * and sends it to the transport layer.
371 373
  */
372 374
 void ipv4_rx ( struct pk_buff *pkb, struct net_device *netdev __unused,
373 375
 			const void *ll_source __unused ) {
@@ -376,26 +378,34 @@ void ipv4_rx ( struct pk_buff *pkb, struct net_device *netdev __unused,
376 378
 	struct in_addr *dest = &iphdr->dest;
377 379
 	uint16_t chksum;
378 380
 
381
+	/* Sanity check */
382
+	if ( pkb_len ( pkb ) < sizeof ( *iphdr ) ) {
383
+		DBG ( "IP datagram too short (%d bytes)\n",
384
+			pkb_len ( pkb ) );
385
+		return;
386
+	}
387
+
379 388
 	/* Print IP4 header for debugging */
380 389
 	ipv4_dump ( iphdr );
381 390
 
382
-	/* Process headers */
391
+	/* Validate version and header length */
383 392
 	if ( iphdr->verhdrlen != 0x45 ) {
384 393
 		DBG ( "Bad version and header length %x\n", iphdr->verhdrlen );
385 394
 		return;
386 395
 	}
387 396
 
388
-	if ( iphdr->len != pkb_len ( pkb ) ) {
389
-		DBG ( "Bad total length %d\n", iphdr->len );
397
+	/* Validate length of IP packet */
398
+	if ( ntohs ( iphdr->len ) != pkb_len ( pkb ) ) {
399
+		DBG ( "Inconsistent packet length %d\n",
400
+					ntohs ( iphdr->len ) );
390 401
 		return;
391 402
 	}
392 403
 
393
-	if ( ( chksum = ipv4_rx_csum ( pkb, iphdr->protocol ) ) != 0xffff ) {
404
+	/* Verify the checksum */
405
+	if ( ( chksum = ipv4_rx_csum ( pkb, iphdr->protocol ) )	!= 0xffff ) {
394 406
 		DBG ( "Bad checksum %x\n", chksum );
395 407
 	}
396 408
 
397
-	/* Todo: Compute and verify the header checksum */
398
-
399 409
 	/* To reduce code size, the following functions are not implemented:
400 410
 	 * 1. Check the destination address
401 411
 	 * 2. Check the TTL field
@@ -403,7 +413,7 @@ void ipv4_rx ( struct pk_buff *pkb, struct net_device *netdev __unused,
403 413
 	 */
404 414
 
405 415
 	/* Strip header */
406
-	pkb_pull ( pkb, IP_HLEN );
416
+	pkb_pull ( pkb, sizeof ( *iphdr ) );
407 417
 
408 418
 	/* Send it to the transport layer */
409 419
 	trans_rx ( pkb, iphdr->protocol, src, dest );

読み込み中…
キャンセル
保存