From 808d4e20ae364e4daa6f3a926589e3608214f365 Mon Sep 17 00:00:00 2001 From: Matt Zahorik Date: Wed, 3 Jan 2018 15:04:51 -0500 Subject: [PATCH] Append a default route to the CNI reply if there's a gateway advertised. Classless static routes (DHCP option 121) are now processed first. If CSRs exist, static routes (DHCP option 33) and the gateway default route are ignored as per RFC 3442. --- plugins/ipam/dhcp/lease.go | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/plugins/ipam/dhcp/lease.go b/plugins/ipam/dhcp/lease.go index ca34eec4..d95057da 100644 --- a/plugins/ipam/dhcp/lease.go +++ b/plugins/ipam/dhcp/lease.go @@ -292,8 +292,26 @@ func (l *DHCPLease) Gateway() net.IP { } func (l *DHCPLease) Routes() []*types.Route { - routes := parseRoutes(l.opts) - return append(routes, parseCIDRRoutes(l.opts)...) + routes := []*types.Route{} + + // RFC 3442 states that if Classless Static Routes (option 121) + // exist, we ignore Static Routes (option 33) and the Router/Gateway. + opt121_routes := parseCIDRRoutes(l.opts) + if len(opt121_routes) > 0 { + return append(routes, opt121_routes...) + } + + // Append Static Routes + routes = append(routes, parseRoutes(l.opts)...) + + // The CNI spec says even if there is a gateway specified, we must + // add a default route in the routes section. + if gw := l.Gateway(); gw != nil { + _, defaultRoute, _ := net.ParseCIDR("0.0.0.0/0") + routes = append(routes, &types.Route{Dst: *defaultRoute, GW: gw}) + } + + return routes } // jitter returns a random value within [-span, span) range