@@ -27,7 +27,6 @@ import (
2727
2828var (
2929 newRuleHeaderType = nftMsgNewRule .HeaderType ()
30- delRuleHeaderType = nftMsgDelRule .HeaderType ()
3130)
3231
3332// This constant is missing at unix.NFTA_RULE_POSITION_ID.
@@ -321,9 +320,9 @@ func (cc *Conn) InsertRule(r *Rule) *Rule {
321320 return cc .newRule (r , operationInsert )
322321}
323322
324- // DelRule deletes the specified Rule. Either the Handle or ID of the
325- // rule must be set .
326- func (cc * Conn ) DelRule (r * Rule ) error {
323+ // delRule deletes the specified Rule. If the destroy flag is set, then the
324+ // message type used is NFT_MSG_DESTROYRULE instead of NFT_MSG_DELRULE .
325+ func (cc * Conn ) delRule (r * Rule , destroy bool ) error {
327326 cc .mu .Lock ()
328327 defer cc .mu .Unlock ()
329328 data := cc .marshalAttr ([]netlink.Attribute {
@@ -345,9 +344,14 @@ func (cc *Conn) DelRule(r *Rule) error {
345344 }
346345 flags := netlink .Request
347346
347+ msgType := nftMsgDelRule
348+ if destroy {
349+ msgType = nftMsgDestroyRule
350+ }
351+
348352 cc .messages = append (cc .messages , netlinkMessage {
349353 Header : netlink.Header {
350- Type : delRuleHeaderType ,
354+ Type : msgType . HeaderType () ,
351355 Flags : flags ,
352356 },
353357 Data : append (extraHeader (uint8 (r .Table .Family ), 0 ), data ... ),
@@ -356,9 +360,26 @@ func (cc *Conn) DelRule(r *Rule) error {
356360 return nil
357361}
358362
363+ // DelRule deletes the specified Rule. Either the Handle or ID of the
364+ // rule must be set.
365+ func (cc * Conn ) DelRule (r * Rule ) error {
366+ return cc .delRule (r , false )
367+ }
368+
369+ // DestroyRule deletes the specified rule but unlike DelRule, it will not
370+ // return an error upon Flush if the rule does not exist. Either the Handle
371+ // or ID of the rule must be set.
372+ // Requires a kernel version >= 6.3.
373+ func (cc * Conn ) DestroyRule (r * Rule ) error {
374+ return cc .delRule (r , true )
375+ }
376+
359377func ruleFromMsg (fam TableFamily , msg netlink.Message ) (* Rule , error ) {
360- if got , want1 , want2 := msg .Header .Type , newRuleHeaderType , delRuleHeaderType ; got != want1 && got != want2 {
361- return nil , fmt .Errorf ("unexpected header type: got %v, want %v or %v" , msg .Header .Type , want1 , want2 )
378+ switch msg .Header .Type {
379+ case nftMsgNewRule .HeaderType (), nftMsgDelRule .HeaderType (), nftMsgDestroyRule .HeaderType ():
380+ // Valid message type, continue processing
381+ default :
382+ return nil , fmt .Errorf ("unexpected header type: %v" , msg .Header .Type )
362383 }
363384 ad , err := netlink .NewAttributeDecoder (msg .Data [4 :])
364385 if err != nil {
0 commit comments