Browse Source

Fixed endianness of 16- and 32- bit values

tags/v0.9.3
Nikhil Chandru Rao 18 years ago
parent
commit
41b399d672
1 changed files with 40 additions and 30 deletions
  1. 40
    30
      src/net/ipv4.c

+ 40
- 30
src/net/ipv4.c View File

107
 	DBG ( "\tVersion = %d\n", ( iphdr->verhdrlen & IP_MASK_VER ) / 16 );
107
 	DBG ( "\tVersion = %d\n", ( iphdr->verhdrlen & IP_MASK_VER ) / 16 );
108
 	DBG ( "\tHeader length = %d\n", iphdr->verhdrlen & IP_MASK_HLEN );
108
 	DBG ( "\tHeader length = %d\n", iphdr->verhdrlen & IP_MASK_HLEN );
109
 	DBG ( "\tService = %d\n", iphdr->service );
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
 	DBG ( "\tIP TTL = %d\n", iphdr->ttl );
113
 	DBG ( "\tIP TTL = %d\n", iphdr->ttl );
114
 	DBG ( "\tProtocol = %d\n", iphdr->protocol );
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
 	DBG ( "\tSource = %s\n", inet_ntoa ( iphdr->src ) );
117
 	DBG ( "\tSource = %s\n", inet_ntoa ( iphdr->src ) );
118
 	DBG ( "\tDestination = %s\n", inet_ntoa ( iphdr->dest ) );
118
 	DBG ( "\tDestination = %s\n", inet_ntoa ( iphdr->dest ) );
119
 }
119
 }
120
 
120
 
121
 /**
121
 /**
122
  * Complete the transport-layer checksum
122
  * Complete the transport-layer checksum
123
- *
124
- * Refer to the note made in net/interface.c about this function
125
  */
123
  */
126
 void ipv4_tx_csum ( struct pk_buff *pkb, uint8_t trans_proto ) {
124
 void ipv4_tx_csum ( struct pk_buff *pkb, uint8_t trans_proto ) {
127
 
125
 
234
  * This function expects a transport-layer segment and prepends the IP header
232
  * This function expects a transport-layer segment and prepends the IP header
235
  */
233
  */
236
 int ipv4_tx ( struct pk_buff *pkb, uint16_t trans_proto, struct in_addr *dest ) {
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
 	struct ipv4_miniroute *miniroute;
236
 	struct ipv4_miniroute *miniroute;
239
 	struct net_device *netdev = NULL;
237
 	struct net_device *netdev = NULL;
240
 	struct in_addr next_hop;
238
 	struct in_addr next_hop;
243
 	int rc;
241
 	int rc;
244
 
242
 
245
 	/* Fill up the IP header, except source address */
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
 	/* Copy destination address */
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
 	 * Continue processing as in ipv4_uip_tx()
262
 	 * Continue processing as in ipv4_uip_tx()
266
 	 */
263
 	 */
288
 	/* Calculate the transport layer checksum */
285
 	/* Calculate the transport layer checksum */
289
 	ipv4_tx_csum ( pkb, trans_proto );
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
 	/* Print IP4 header for debugging */
292
 	/* Print IP4 header for debugging */
292
 	ipv4_dump ( iphdr );
293
 	ipv4_dump ( iphdr );
293
 
294
 
367
  * @v netdev	Network device
368
  * @v netdev	Network device
368
  * @v ll_source	Link-layer destination source
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
 void ipv4_rx ( struct pk_buff *pkb, struct net_device *netdev __unused,
374
 void ipv4_rx ( struct pk_buff *pkb, struct net_device *netdev __unused,
373
 			const void *ll_source __unused ) {
375
 			const void *ll_source __unused ) {
376
 	struct in_addr *dest = &iphdr->dest;
378
 	struct in_addr *dest = &iphdr->dest;
377
 	uint16_t chksum;
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
 	/* Print IP4 header for debugging */
388
 	/* Print IP4 header for debugging */
380
 	ipv4_dump ( iphdr );
389
 	ipv4_dump ( iphdr );
381
 
390
 
382
-	/* Process headers */
391
+	/* Validate version and header length */
383
 	if ( iphdr->verhdrlen != 0x45 ) {
392
 	if ( iphdr->verhdrlen != 0x45 ) {
384
 		DBG ( "Bad version and header length %x\n", iphdr->verhdrlen );
393
 		DBG ( "Bad version and header length %x\n", iphdr->verhdrlen );
385
 		return;
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
 		return;
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
 		DBG ( "Bad checksum %x\n", chksum );
406
 		DBG ( "Bad checksum %x\n", chksum );
395
 	}
407
 	}
396
 
408
 
397
-	/* Todo: Compute and verify the header checksum */
398
-
399
 	/* To reduce code size, the following functions are not implemented:
409
 	/* To reduce code size, the following functions are not implemented:
400
 	 * 1. Check the destination address
410
 	 * 1. Check the destination address
401
 	 * 2. Check the TTL field
411
 	 * 2. Check the TTL field
403
 	 */
413
 	 */
404
 
414
 
405
 	/* Strip header */
415
 	/* Strip header */
406
-	pkb_pull ( pkb, IP_HLEN );
416
+	pkb_pull ( pkb, sizeof ( *iphdr ) );
407
 
417
 
408
 	/* Send it to the transport layer */
418
 	/* Send it to the transport layer */
409
 	trans_rx ( pkb, iphdr->protocol, src, dest );
419
 	trans_rx ( pkb, iphdr->protocol, src, dest );

Loading…
Cancel
Save