Browse Source

Add concept of DHCP option applicators.

tags/v0.9.3
Michael Brown 17 years ago
parent
commit
1567b69895
4 changed files with 97 additions and 28 deletions
  1. 19
    0
      src/include/gpxe/dhcp.h
  2. 49
    0
      src/net/dhcpopts.c
  3. 6
    28
      src/net/udp/dhcp.c
  4. 23
    0
      src/net/udp/dns.c

+ 19
- 0
src/include/gpxe/dhcp.h View File

11
 #include <gpxe/list.h>
11
 #include <gpxe/list.h>
12
 #include <gpxe/in.h>
12
 #include <gpxe/in.h>
13
 #include <gpxe/refcnt.h>
13
 #include <gpxe/refcnt.h>
14
+#include <gpxe/tables.h>
14
 
15
 
15
 struct net_device;
16
 struct net_device;
16
 struct job_interface;
17
 struct job_interface;
438
 	struct dhcp_option_block options;
439
 	struct dhcp_option_block options;
439
 };
440
 };
440
 
441
 
442
+/** A DHCP option applicator */
443
+struct dhcp_option_applicator {
444
+	/** DHCP option tag */
445
+	unsigned int tag;
446
+	/** Applicator
447
+	 *
448
+	 * @v tag	DHCP option tag
449
+	 * @v option	DHCP option
450
+	 * @ret rc	Return status code
451
+	 */
452
+	int ( * apply ) ( unsigned int tag, struct dhcp_option *option );
453
+};
454
+
455
+/** Declare a DHCP option applicator */
456
+#define __dhcp_applicator \
457
+	__table ( struct dhcp_option_applicator, dhcp_appicators, 01 )
458
+
441
 /**
459
 /**
442
  * Get reference to DHCP options block
460
  * Get reference to DHCP options block
443
  *
461
  *
485
 					   struct in_addr *inp );
503
 					   struct in_addr *inp );
486
 extern void delete_dhcp_option ( struct dhcp_option_block *options,
504
 extern void delete_dhcp_option ( struct dhcp_option_block *options,
487
 				 unsigned int tag );
505
 				 unsigned int tag );
506
+extern int apply_dhcp_options ( struct dhcp_option_block *options );
488
 
507
 
489
 extern struct dhcp_option_block dhcp_request_options;
508
 extern struct dhcp_option_block dhcp_request_options;
490
 extern int create_dhcp_packet ( struct net_device *netdev, uint8_t msgtype,
509
 extern int create_dhcp_packet ( struct net_device *netdev, uint8_t msgtype,

+ 49
- 0
src/net/dhcpopts.c View File

25
 #include <assert.h>
25
 #include <assert.h>
26
 #include <gpxe/list.h>
26
 #include <gpxe/list.h>
27
 #include <gpxe/in.h>
27
 #include <gpxe/in.h>
28
+#include <gpxe/uri.h>
28
 #include <gpxe/dhcp.h>
29
 #include <gpxe/dhcp.h>
29
 
30
 
30
 /** @file
31
 /** @file
36
 /** List of registered DHCP option blocks */
37
 /** List of registered DHCP option blocks */
37
 static LIST_HEAD ( option_blocks );
38
 static LIST_HEAD ( option_blocks );
38
 
39
 
40
+/** Registered DHCP option applicators */
41
+static struct dhcp_option_applicator dhcp_option_applicators[0]
42
+	__table_start ( struct dhcp_option_applicator, dhcp_applicators );
43
+static struct dhcp_option_applicator dhcp_option_applicators_end[0]
44
+	__table_end ( struct dhcp_option_applicator, dhcp_applicators );
45
+
39
 /**
46
 /**
40
  * Obtain printable version of a DHCP option tag
47
  * Obtain printable version of a DHCP option tag
41
  *
48
  *
560
 			  unsigned int tag ) {
567
 			  unsigned int tag ) {
561
 	set_dhcp_option ( options, tag, NULL, 0 );
568
 	set_dhcp_option ( options, tag, NULL, 0 );
562
 }
569
 }
570
+
571
+/**
572
+ * Apply DHCP options
573
+ *
574
+ * @v options		DHCP options block, or NULL
575
+ * @ret rc		Return status code
576
+ */
577
+int apply_dhcp_options ( struct dhcp_option_block *options ) {
578
+	struct dhcp_option_applicator *applicator;
579
+	struct dhcp_option *option;
580
+	struct in_addr tftp_server;
581
+	struct uri *uri;
582
+	char uri_string[32];
583
+	unsigned int tag;
584
+	int rc;
585
+
586
+	/* Set current working URI based on TFTP server */
587
+	find_dhcp_ipv4_option ( options, DHCP_EB_SIADDR, &tftp_server );
588
+	snprintf ( uri_string, sizeof ( uri_string ),
589
+		   "tftp://%s/", inet_ntoa ( tftp_server ) );
590
+	uri = parse_uri ( uri_string );
591
+	if ( ! uri )
592
+		return -ENOMEM;
593
+	churi ( uri );
594
+	uri_put ( uri );
595
+
596
+	/* Call all registered DHCP option applicators */
597
+	for ( applicator = dhcp_option_applicators ;
598
+	      applicator < dhcp_option_applicators_end ; applicator++ ) {
599
+		tag = applicator->tag;
600
+		option = find_dhcp_option ( options, tag );
601
+		if ( ! option )
602
+			continue;
603
+		if ( ( rc = applicator->apply ( tag, option ) ) != 0 ) {
604
+			DBG ( "Could not apply DHCP option %s: %s\n",
605
+			      dhcp_tag_name ( tag ), strerror ( rc ) );
606
+			return rc;
607
+		}
608
+	}
609
+
610
+	return 0;
611
+}

+ 6
- 28
src/net/udp/dhcp.c View File

30
 #include <gpxe/retry.h>
30
 #include <gpxe/retry.h>
31
 #include <gpxe/tcpip.h>
31
 #include <gpxe/tcpip.h>
32
 #include <gpxe/ip.h>
32
 #include <gpxe/ip.h>
33
-#include <gpxe/uri.h>
34
 #include <gpxe/dhcp.h>
33
 #include <gpxe/dhcp.h>
35
 
34
 
36
 /** @file
35
 /** @file
826
  *
825
  *
827
  */
826
  */
828
 
827
 
829
-/* Avoid dragging in dns.o */
830
-struct sockaddr_tcpip nameserver;
831
-
832
-/* Avoid dragging in syslog.o */
833
-struct in_addr syslogserver;
834
-
835
 /**
828
 /**
836
  * Configure network device from DHCP options
829
  * Configure network device from DHCP options
837
  *
830
  *
844
 	struct in_addr address = { 0 };
837
 	struct in_addr address = { 0 };
845
 	struct in_addr netmask = { 0 };
838
 	struct in_addr netmask = { 0 };
846
 	struct in_addr gateway = { INADDR_NONE };
839
 	struct in_addr gateway = { INADDR_NONE };
847
-	struct sockaddr_in *sin_nameserver;
848
-	struct in_addr tftp_server;
849
-	struct uri *uri;
850
-	char uri_string[32];
851
 	int rc;
840
 	int rc;
852
 
841
 
853
 	/* Clear any existing routing table entry */
842
 	/* Clear any existing routing table entry */
866
 		return rc;
855
 		return rc;
867
 	}
856
 	}
868
 
857
 
869
-	/* Retrieve other DHCP options that we care about */
870
-	sin_nameserver = ( struct sockaddr_in * ) &nameserver;
871
-	sin_nameserver->sin_family = AF_INET;
872
-	find_dhcp_ipv4_option ( options, DHCP_DNS_SERVERS,
873
-				&sin_nameserver->sin_addr );
874
-	find_dhcp_ipv4_option ( options, DHCP_LOG_SERVERS,
875
-				&syslogserver );
876
-
877
-	/* Set current working URI based on TFTP server */
878
-	find_dhcp_ipv4_option ( options, DHCP_EB_SIADDR, &tftp_server );
879
-	snprintf ( uri_string, sizeof ( uri_string ),
880
-		   "tftp://%s/", inet_ntoa ( tftp_server ) );
881
-	uri = parse_uri ( uri_string );
882
-	if ( ! uri )
883
-		return -ENOMEM;
884
-	churi ( uri );
885
-	uri_put ( uri );
858
+	/* Apply other DHCP options */
859
+	if ( ( rc = apply_dhcp_options ( options ) ) != 0 ) {
860
+		DBG ( "Could not apply %s DHCP result options: %s\n",
861
+		      netdev->name, strerror ( rc ) );
862
+		return rc;
863
+	}
886
 
864
 
887
 	return 0;
865
 	return 0;
888
 }
866
 }

+ 23
- 0
src/net/udp/dns.c View File

30
 #include <gpxe/resolv.h>
30
 #include <gpxe/resolv.h>
31
 #include <gpxe/retry.h>
31
 #include <gpxe/retry.h>
32
 #include <gpxe/tcpip.h>
32
 #include <gpxe/tcpip.h>
33
+#include <gpxe/dhcp.h>
33
 #include <gpxe/dns.h>
34
 #include <gpxe/dns.h>
34
 
35
 
35
 /** @file
36
 /** @file
503
 	.name = "DNS",
504
 	.name = "DNS",
504
 	.resolv = dns_resolv,
505
 	.resolv = dns_resolv,
505
 };
506
 };
507
+
508
+/**
509
+ * Apply DHCP nameserver option
510
+ *
511
+ * @v tag		DHCP option tag
512
+ * @v option		DHCP option
513
+ */
514
+static int apply_dhcp_nameserver ( unsigned int tag __unused,
515
+				   struct dhcp_option *option ) {
516
+	struct sockaddr_in *sin_nameserver;
517
+
518
+	sin_nameserver = ( struct sockaddr_in * ) &nameserver;
519
+	sin_nameserver->sin_family = AF_INET;
520
+	dhcp_ipv4_option ( option, &sin_nameserver->sin_addr );
521
+	return 0;
522
+}
523
+
524
+/** DHCP nameserver applicator */
525
+struct dhcp_option_applicator dhcp_nameserver_applicator __dhcp_applicator = {
526
+	.tag = DHCP_DNS_SERVERS,
527
+	.apply = apply_dhcp_nameserver,
528
+};

Loading…
Cancel
Save