Explorar el Código

Add concept of DHCP option applicators.

tags/v0.9.3
Michael Brown hace 17 años
padre
commit
1567b69895
Se han modificado 4 ficheros con 97 adiciones y 28 borrados
  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 Ver fichero

@@ -11,6 +11,7 @@
11 11
 #include <gpxe/list.h>
12 12
 #include <gpxe/in.h>
13 13
 #include <gpxe/refcnt.h>
14
+#include <gpxe/tables.h>
14 15
 
15 16
 struct net_device;
16 17
 struct job_interface;
@@ -438,6 +439,23 @@ struct dhcp_packet {
438 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 460
  * Get reference to DHCP options block
443 461
  *
@@ -485,6 +503,7 @@ extern void find_global_dhcp_ipv4_option ( unsigned int tag,
485 503
 					   struct in_addr *inp );
486 504
 extern void delete_dhcp_option ( struct dhcp_option_block *options,
487 505
 				 unsigned int tag );
506
+extern int apply_dhcp_options ( struct dhcp_option_block *options );
488 507
 
489 508
 extern struct dhcp_option_block dhcp_request_options;
490 509
 extern int create_dhcp_packet ( struct net_device *netdev, uint8_t msgtype,

+ 49
- 0
src/net/dhcpopts.c Ver fichero

@@ -25,6 +25,7 @@
25 25
 #include <assert.h>
26 26
 #include <gpxe/list.h>
27 27
 #include <gpxe/in.h>
28
+#include <gpxe/uri.h>
28 29
 #include <gpxe/dhcp.h>
29 30
 
30 31
 /** @file
@@ -36,6 +37,12 @@
36 37
 /** List of registered DHCP option blocks */
37 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 47
  * Obtain printable version of a DHCP option tag
41 48
  *
@@ -560,3 +567,45 @@ void delete_dhcp_option ( struct dhcp_option_block *options,
560 567
 			  unsigned int tag ) {
561 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 Ver fichero

@@ -30,7 +30,6 @@
30 30
 #include <gpxe/retry.h>
31 31
 #include <gpxe/tcpip.h>
32 32
 #include <gpxe/ip.h>
33
-#include <gpxe/uri.h>
34 33
 #include <gpxe/dhcp.h>
35 34
 
36 35
 /** @file
@@ -826,12 +825,6 @@ int start_dhcp ( struct job_interface *job, struct net_device *netdev,
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 829
  * Configure network device from DHCP options
837 830
  *
@@ -844,10 +837,6 @@ int dhcp_configure_netdev ( struct net_device *netdev,
844 837
 	struct in_addr address = { 0 };
845 838
 	struct in_addr netmask = { 0 };
846 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 840
 	int rc;
852 841
 
853 842
 	/* Clear any existing routing table entry */
@@ -866,23 +855,12 @@ int dhcp_configure_netdev ( struct net_device *netdev,
866 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 865
 	return 0;
888 866
 }

+ 23
- 0
src/net/udp/dns.c Ver fichero

@@ -30,6 +30,7 @@
30 30
 #include <gpxe/resolv.h>
31 31
 #include <gpxe/retry.h>
32 32
 #include <gpxe/tcpip.h>
33
+#include <gpxe/dhcp.h>
33 34
 #include <gpxe/dns.h>
34 35
 
35 36
 /** @file
@@ -503,3 +504,25 @@ struct resolver dns_resolver __resolver ( RESOLV_NORMAL ) = {
503 504
 	.name = "DNS",
504 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…
Cancelar
Guardar