|
| 1 | +package firewall |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | +) |
| 7 | + |
| 8 | +// RedirectPort redirects a source port to a destination port on the interface |
| 9 | +// intf. If intf is empty, it is set to "*" which means all interfaces. |
| 10 | +// If a redirection for the source port given already exists, it is removed first. |
| 11 | +// If the destination port is zero, the redirection for the source port is removed |
| 12 | +// and no new redirection is added. |
| 13 | +func (c *Config) RedirectPort(ctx context.Context, intf string, sourcePort, |
| 14 | + destinationPort uint16) (err error) { |
| 15 | + c.stateMutex.Lock() |
| 16 | + defer c.stateMutex.Unlock() |
| 17 | + |
| 18 | + if sourcePort == 0 { |
| 19 | + panic("source port cannot be 0") |
| 20 | + } |
| 21 | + |
| 22 | + newRedirection := portRedirection{ |
| 23 | + interfaceName: intf, |
| 24 | + sourcePort: sourcePort, |
| 25 | + destinationPort: destinationPort, |
| 26 | + } |
| 27 | + |
| 28 | + if !c.enabled { |
| 29 | + c.logger.Info("firewall disabled, only updating redirected ports internal state") |
| 30 | + if destinationPort == 0 { |
| 31 | + c.portRedirections.remove(intf, sourcePort) |
| 32 | + return nil |
| 33 | + } |
| 34 | + exists, conflict := c.portRedirections.check(newRedirection) |
| 35 | + switch { |
| 36 | + case exists: |
| 37 | + return nil |
| 38 | + case conflict != nil: |
| 39 | + c.portRedirections.remove(conflict.interfaceName, |
| 40 | + conflict.sourcePort) |
| 41 | + } |
| 42 | + c.portRedirections.append(newRedirection) |
| 43 | + return nil |
| 44 | + } |
| 45 | + |
| 46 | + exists, conflict := c.portRedirections.check(newRedirection) |
| 47 | + switch { |
| 48 | + case exists: |
| 49 | + return nil |
| 50 | + case conflict != nil: |
| 51 | + const remove = true |
| 52 | + err = c.redirectPort(ctx, conflict.interfaceName, conflict.sourcePort, |
| 53 | + conflict.destinationPort, remove) |
| 54 | + if err != nil { |
| 55 | + return fmt.Errorf("removing conflicting redirection: %w", err) |
| 56 | + } |
| 57 | + c.portRedirections.remove(conflict.interfaceName, |
| 58 | + conflict.sourcePort) |
| 59 | + } |
| 60 | + |
| 61 | + const remove = false |
| 62 | + err = c.redirectPort(ctx, intf, sourcePort, destinationPort, remove) |
| 63 | + if err != nil { |
| 64 | + return fmt.Errorf("redirecting port: %w", err) |
| 65 | + } |
| 66 | + c.portRedirections.append(newRedirection) |
| 67 | + |
| 68 | + return nil |
| 69 | +} |
| 70 | + |
| 71 | +type portRedirection struct { |
| 72 | + interfaceName string |
| 73 | + sourcePort uint16 |
| 74 | + destinationPort uint16 |
| 75 | +} |
| 76 | + |
| 77 | +type portRedirections []portRedirection |
| 78 | + |
| 79 | +func (p *portRedirections) remove(intf string, sourcePort uint16) { |
| 80 | + slice := *p |
| 81 | + for i, redirection := range slice { |
| 82 | + interfaceMatch := intf == "" || intf == redirection.interfaceName |
| 83 | + if redirection.sourcePort == sourcePort && interfaceMatch { |
| 84 | + // Remove redirection - note: order does not matter |
| 85 | + slice[i] = slice[len(slice)-1] |
| 86 | + slice = slice[:len(slice)-1] |
| 87 | + } |
| 88 | + } |
| 89 | + *p = slice |
| 90 | +} |
| 91 | + |
| 92 | +func (p *portRedirections) check(dryRun portRedirection) (alreadyExists bool, |
| 93 | + conflict *portRedirection) { |
| 94 | + slice := *p |
| 95 | + for _, redirection := range slice { |
| 96 | + interfaceMatch := redirection.interfaceName == "" || |
| 97 | + redirection.interfaceName == dryRun.interfaceName |
| 98 | + |
| 99 | + if redirection.sourcePort == dryRun.sourcePort && |
| 100 | + redirection.destinationPort == dryRun.destinationPort && |
| 101 | + interfaceMatch { |
| 102 | + return true, nil |
| 103 | + } |
| 104 | + |
| 105 | + if redirection.sourcePort == dryRun.sourcePort && |
| 106 | + interfaceMatch { |
| 107 | + // Source port has a redirection already for the same interface or all interfaces |
| 108 | + return false, &redirection |
| 109 | + } |
| 110 | + } |
| 111 | + return false, nil |
| 112 | +} |
| 113 | + |
| 114 | +// append should be called after running `check` to avoid rule conflicts. |
| 115 | +func (p *portRedirections) append(newRedirection portRedirection) { |
| 116 | + slice := *p |
| 117 | + slice = append(slice, newRedirection) |
| 118 | + *p = slice |
| 119 | +} |
0 commit comments