Transfer of static routes to DHCP



Write in the main section of the configuration file of DHCP server:

option ms-classless-static-routes code 249 = array of unsigned integer 8;
option rfc3442-classless-static-routes code 121 = array of unsigned integer 8;

add this in section subnet or host:

option ms-classless-static-routes 16, 172,16, 10,16,40,100;
option rfc3442-classless-static-routes 16, 172,16, 10,16,40,100;

where

16 (mask)
172.16.0.0 (network)
10.16.40.100 (gateway)

here is script to generate content “option classless-route” in hexadecimal notation for older versions of dhcpd:

 

#!/usr/bin/perl
use strict;
# Usage:
# make_classless_option({ "subnet/mask" => "router", "subnet/mask" => "router", ... });
# subnet the subnet address, 4 dot-separated numbers
# mask the subnet mask length (e.g. /24 corresponds to 255.255.255.0, /8 corresponds to 255.0.0.0)
# router the router address, 4 dot-separated numbers

sub make_classless_option{
my $routes = shift;
my ($s1, $s2, $s3, $s4, $len, @bytes, $net, $mask, $destination, $router);

$len = 2;
@bytes = ();
foreach $destination(keys %{$routes}) {
($net, $mask) = split('/', $destination);
$router = $routes->{$destination};
($s1, $s2, $s3, $s4) = split(/\./, $net);
push(@bytes, sprintf('%02x', $mask));
push(@bytes, sprintf('%02x', $s1));
push(@bytes, sprintf('%02x', $s2)) if($mask > 8);
push(@bytes, sprintf('%02x', $s3)) if($mask > 16);
push(@bytes, sprintf('%02x', $s4)) if($mask > 24);
($s1, $s2, $s3, $s4) = split(/\./, $router);
push(@bytes, sprintf('%02x', $s1));
 push(@bytes, sprintf('%02x', $s2));
 push(@bytes, sprintf('%02x', $s3));
 push(@bytes, sprintf('%02x', $s4));
 }

 return join(':', @bytes);
 }

 # Example of use

 print make_classless_option({
 "10.230.0.0/16" => "10.230.178.145"
 });

One Responseto “Transfer of static routes to DHCP”

  1. A default route should be pushed when using 121 (classless-routes), so you want:
    print make_classless_option({
    “0.0.0.0/0” => “10.230.178.254”, # or what ever your gateway is
    “10.230.0.0/16” => “10.230.178.145”
    });

    But this fails as the first octal (0) is printed as well as the mask (0) so you get 00:00 where you should only use a single zero.

    To fix replace:

    push(@bytes, sprintf(‘%02x’, $mask));
    push(@bytes, sprintf(‘%02x’, $s1));
    push(@bytes, sprintf(‘%02x’, $s2)) if($mask > 8);
    push(@bytes, sprintf(‘%02x’, $s3)) if($mask > 16);
    push(@bytes, sprintf(‘%02x’, $s4)) if($mask > 24);

    with:

    push(@bytes, sprintf(‘%02x’, $mask));
    push(@bytes, sprintf(‘%02x’, $s1)) if($mask);
    push(@bytes, sprintf(‘%02x’, $s2)) if($mask > 8);
    push(@bytes, sprintf(‘%02x’, $s3)) if($mask > 16);
    push(@bytes, sprintf(‘%02x’, $s4)) if($mask > 24);

    Thanks for the handy script!

    Cheers,
    Alex

Leave a Reply

Your email address will not be published. Required fields are marked *