Skip to content

Commit d9154c8

Browse files
lanphgphmaboch
authored andcommitted
route: conditionally not serialize empty IP address
1 parent bab08b3 commit d9154c8

2 files changed

Lines changed: 63 additions & 1 deletion

File tree

route_linux.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -929,7 +929,12 @@ func (h *Handle) prepareRouteReq(route *Route, req *nl.NetlinkRequest, msg *nl.R
929929
} else {
930930
dstData = route.Dst.IP.To16()
931931
}
932-
rtAttrs = append(rtAttrs, nl.NewRtAttr(unix.RTA_DST, dstData))
932+
shouldSkipDst := (route.Type == unix.RTN_UNREACHABLE ||
933+
route.Type == unix.RTN_BLACKHOLE ||
934+
route.Type == unix.RTN_PROHIBIT) && (dstLen == 0)
935+
if !shouldSkipDst {
936+
rtAttrs = append(rtAttrs, nl.NewRtAttr(unix.RTA_DST, dstData))
937+
}
933938
} else if route.MPLSDst != nil {
934939
family = nl.FAMILY_MPLS
935940
msg.Dst_len = uint8(20)

route_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,63 @@ func TestRouteAddDel(t *testing.T) {
152152
}
153153
}
154154

155+
func TestRouteUnreachableEmptyDst(t *testing.T) {
156+
t.Cleanup(setUpNetlinkTest(t))
157+
158+
// Test adding unreachable/blackhole/prohibit routes with empty Dst.IP
159+
// These route types don't need RTA_DST to be serialized
160+
testCases := []struct {
161+
name string
162+
routeType int
163+
}{
164+
{"unreachable", unix.RTN_UNREACHABLE},
165+
{"blackhole", unix.RTN_BLACKHOLE},
166+
{"prohibit", unix.RTN_PROHIBIT},
167+
}
168+
169+
for _, tc := range testCases {
170+
t.Run(tc.name, func(t *testing.T) {
171+
route := &Route{
172+
Table: 100,
173+
Dst: &net.IPNet{
174+
IP: net.IP{},
175+
Mask: net.IPMask{},
176+
},
177+
Priority: 100,
178+
Type: tc.routeType,
179+
Scope: unix.RT_SCOPE_UNIVERSE,
180+
Family: FAMILY_V4,
181+
}
182+
183+
if err := RouteAdd(route); err != nil {
184+
t.Fatalf("failed to add %s route with empty Dst.IP: %v", tc.name, err)
185+
}
186+
187+
t.Cleanup(func() {
188+
if err := RouteDel(route); err != nil {
189+
t.Errorf("failed to delete route %s: %v", tc.name, err)
190+
}
191+
})
192+
193+
routes, err := RouteListFiltered(FAMILY_V4, &Route{Table: 100}, RT_FILTER_TABLE)
194+
if err != nil {
195+
t.Fatal(err)
196+
}
197+
198+
found := false
199+
for _, r := range routes {
200+
if r.Type == tc.routeType {
201+
found = true
202+
break
203+
}
204+
}
205+
if !found {
206+
t.Fatalf("%s route not found after adding", tc.name)
207+
}
208+
})
209+
}
210+
}
211+
155212
func TestRoute6AddDel(t *testing.T) {
156213
t.Cleanup(setUpNetlinkTest(t))
157214

0 commit comments

Comments
 (0)