forked from projectcalico/felix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
static.go
836 lines (752 loc) · 27.9 KB
/
static.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
// Copyright (c) 2016-2018 Tigera, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package rules
import (
log "github.com/sirupsen/logrus"
. "github.com/projectcalico/felix/iptables"
"github.com/projectcalico/felix/proto"
)
func (r *DefaultRuleRenderer) StaticFilterTableChains(ipVersion uint8) (chains []*Chain) {
chains = append(chains, r.StaticFilterForwardChains()...)
chains = append(chains, r.StaticFilterInputChains(ipVersion)...)
chains = append(chains, r.StaticFilterOutputChains(ipVersion)...)
return
}
const (
ProtoIPIP = 4
ProtoTCP = 6
ProtoUDP = 17
ProtoICMPv6 = 58
)
func (r *DefaultRuleRenderer) StaticFilterInputChains(ipVersion uint8) []*Chain {
result := []*Chain{}
result = append(result,
r.filterInputChain(ipVersion),
r.filterWorkloadToHostChain(ipVersion),
r.failsafeInChain("filter"),
)
if r.KubeIPVSSupportEnabled {
result = append(result, r.StaticFilterInputForwardCheckChain(ipVersion))
}
return result
}
func (r *DefaultRuleRenderer) acceptAlreadyAccepted() []Rule {
return []Rule{
{
Match: Match().MarkSingleBitSet(r.IptablesMarkAccept),
Action: r.filterAllowAction,
},
}
}
// Forward check chain is to check if a packet belongs to a forwarded traffic or not.
// With kube-proxy running in ipvs mode, both local or forwarded traffic goes through INPUT filter chain.
func (r *DefaultRuleRenderer) StaticFilterInputForwardCheckChain(ipVersion uint8) *Chain {
var fwRules []Rule
var portRanges []*proto.PortRange
// Assembly port ranges for kubernetes node ports.
for _, portRange := range r.KubeNodePortRanges {
pr := &proto.PortRange{
First: int32(portRange.MinPort),
Last: int32(portRange.MaxPort),
}
portRanges = append(portRanges, pr)
}
// Get ipsets name for local host ips.
nameForIPSet := func(ipsetID string) string {
if ipVersion == 4 {
return r.IPSetConfigV4.NameForMainIPSet(ipsetID)
} else {
return r.IPSetConfigV6.NameForMainIPSet(ipsetID)
}
}
hostIPSet := nameForIPSet(IPSetIDThisHostIPs)
fwRules = append(fwRules,
// If packet belongs to an existing conntrack connection, it does not belong to a forwarded traffic even destination ip is a
// service ip. This could happen when pod send back response to a local host process accessing a service ip.
Rule{
Match: Match().ConntrackState("RELATED,ESTABLISHED"),
Action: ReturnAction{},
},
)
// If packet is accessing local host within kubernetes NodePort range, it belongs to a forwarded traffic.
for _, portSplit := range SplitPortList(portRanges) {
fwRules = append(fwRules,
Rule{
Match: Match().Protocol("tcp").
DestPortRanges(portSplit).
DestIPSet(hostIPSet),
Action: GotoAction{Target: ChainDispatchSetEndPointMark},
Comment: "To kubernetes NodePort service",
},
Rule{
Match: Match().Protocol("udp").
DestPortRanges(portSplit).
DestIPSet(hostIPSet),
Action: GotoAction{Target: ChainDispatchSetEndPointMark},
Comment: "To kubernetes NodePort service",
},
)
}
fwRules = append(fwRules,
// If packet is accessing non local host ip, it belongs to a forwarded traffic.
Rule{
Match: Match().NotDestIPSet(hostIPSet),
Action: JumpAction{Target: ChainDispatchSetEndPointMark},
Comment: "To kubernetes service",
},
)
return &Chain{
Name: ChainForwardCheck,
Rules: fwRules,
}
}
// With kube-proxy running in ipvs mode, we categorise traffic going through OUTPUT chain into three classes.
// Class 1. forwarded packet originated from a calico workload or host endpoint --> INPUT filter --> OUTPUT filter
// Class 2. forwarded packet originated from a non calico endpoint --> INPUT filter --> OUTPUT filter
// Class 3. local process originated packet --> OUTPUT filter
// This function handles traffic in Class 1 and Class 2.
func (r *DefaultRuleRenderer) StaticFilterOutputForwardEndpointMarkChain() *Chain {
var fwRules []Rule
fwRules = append(fwRules,
// Only packets that we know are really being forwarded reach this chain. However, since
// we're called from the OUTPUT chain, we're forbidden from using the input interface match.
// Instead, we rely on the INPUT chain to mark the packet with a per-endpoint mark value
// and do our dispatch on that mark value. So that we don't touch "Class 2" packets, we
// mark them with mark pattern IptablesMarkNonCaliEndpoint and exclude them here. This
// prevents the default drop at the end of the dispatch chain from dropping non-Calico
// traffic.
Rule{
Match: Match().NotMarkMatchesWithMask(r.IptablesMarkNonCaliEndpoint, r.IptablesMarkEndpoint),
Action: JumpAction{Target: ChainDispatchFromEndPointMark},
},
)
// The packet may be going to a workload interface. Send any such packets to the normal,
// interface-name-based dispatch chains.
for _, prefix := range r.WorkloadIfacePrefixes {
log.WithField("ifacePrefix", prefix).Debug("Adding workload match rules")
ifaceMatch := prefix + "+"
fwRules = append(fwRules,
Rule{
Match: Match().OutInterface(ifaceMatch),
Action: JumpAction{Target: ChainToWorkloadDispatch},
},
)
}
fwRules = append(fwRules,
// The packet may be going to a host endpoint, send it to the host endpoint
// apply-on-forward dispatch chain. That chain returns any packets that are not going to a
// known host endpoint for further processing.
Rule{
Action: JumpAction{Target: ChainDispatchToHostEndpointForward},
},
// Before we ACCEPT the packet, clear the per-interface mark bit. This is required because
// the packet may get encapsulated and pass through iptables again. Since the new encapped
// packet would inherit the mark bits, it would be (incorrectly) treated as a forwarded
// packet.
Rule{
Action: ClearMarkAction{Mark: r.IptablesMarkEndpoint},
},
// If a packet reaches here, one of the following must be true:
//
// - it is going to a workload endpoint and it has passed that endpoint's policy
// - it is going to a host interface with a Calico host endpoint and it has passed that
// endpoint's policy
// - it is going to a host interface with no Calico host endpoint.
//
// In the first two cases, the policy will have set the accept bit in the mark and we "own"
// the packet so it's right for us to ACCEPT it here (unless configured otherwise). In
// the other case, we don't own the packet so we always return it to the OUTPUT chain
// for further processing.
Rule{
Match: Match().MarkSingleBitSet(r.IptablesMarkAccept),
Action: r.filterAllowAction,
Comment: "Policy explicitly accepted packet.",
},
)
return &Chain{
Name: ChainForwardEndpointMark,
Rules: fwRules,
}
}
func (r *DefaultRuleRenderer) filterInputChain(ipVersion uint8) *Chain {
var inputRules []Rule
if ipVersion == 4 && r.IPIPEnabled {
// IPIP is enabled, filter incoming IPIP packets to ensure they come from a
// recognised host and are going to a local address on the host. We use the protocol
// number rather than its name because the name is not guaranteed to be known by the kernel.
inputRules = append(inputRules,
Rule{
Match: Match().ProtocolNum(ProtoIPIP).
SourceIPSet(r.IPSetConfigV4.NameForMainIPSet(IPSetIDAllHostNets)).
DestAddrType(AddrTypeLocal),
Action: r.filterAllowAction,
Comment: "Allow IPIP packets from Calico hosts",
},
Rule{
Match: Match().ProtocolNum(ProtoIPIP),
Action: DropAction{},
Comment: "Drop IPIP packets from non-Calico hosts",
},
)
}
if r.KubeIPVSSupportEnabled {
// Check if packet belongs to forwarded traffic. (e.g. part of an ipvs connection).
// If it is, set endpoint mark and skip "to local host" rules below.
inputRules = append(inputRules,
Rule{
Action: ClearMarkAction{Mark: r.IptablesMarkEndpoint},
},
Rule{
Action: JumpAction{Target: ChainForwardCheck},
},
Rule{
Match: Match().MarkNotClear(r.IptablesMarkEndpoint),
Action: ReturnAction{},
},
)
}
// Apply our policy to packets coming from workload endpoints.
for _, prefix := range r.WorkloadIfacePrefixes {
log.WithField("ifacePrefix", prefix).Debug("Adding workload match rules")
ifaceMatch := prefix + "+"
inputRules = append(inputRules, Rule{
Match: Match().InInterface(ifaceMatch),
Action: GotoAction{Target: ChainWorkloadToHost},
})
}
// Now we only have ingress host endpoint processing to do. The ingress host endpoint may
// have already accepted this packet in the raw or mangle table. In that case, accept the
// packet immediately here too.
inputRules = append(inputRules, r.acceptAlreadyAccepted()...)
// Apply host endpoint policy.
inputRules = append(inputRules,
Rule{
Action: ClearMarkAction{Mark: r.allCalicoMarkBits()},
},
Rule{
Action: JumpAction{Target: ChainDispatchFromHostEndpoint},
},
Rule{
Match: Match().MarkSingleBitSet(r.IptablesMarkAccept),
Action: r.filterAllowAction,
Comment: "Host endpoint policy accepted packet.",
},
)
return &Chain{
Name: ChainFilterInput,
Rules: inputRules,
}
}
func (r *DefaultRuleRenderer) filterWorkloadToHostChain(ipVersion uint8) *Chain {
var rules []Rule
// For IPv6, we need to white-list certain ICMP traffic from workloads in order to to act
// as a router. Note: we do this before the policy chains, so we're bypassing the egress
// rules for this traffic. While that might be unexpected, it makes sure that the user
// doesn't cut off their own connectivity in subtle ways that they shouldn't have to worry
// about.
//
// - 130: multicast listener query.
// - 131: multicast listener report.
// - 132: multicast listener done.
// - 133: router solicitation, which an endpoint uses to request
// configuration information rather than waiting for an
// unsolicited router advertisement.
// - 135: neighbor solicitation.
// - 136: neighbor advertisement.
if ipVersion == 6 {
for _, icmpType := range []uint8{130, 131, 132, 133, 135, 136} {
rules = append(rules, Rule{
Match: Match().
ProtocolNum(ProtoICMPv6).
ICMPV6Type(icmpType),
Action: r.filterAllowAction,
})
}
}
if r.OpenStackSpecialCasesEnabled {
log.Info("Adding OpenStack special-case rules.")
if ipVersion == 4 && r.OpenStackMetadataIP != nil {
// For OpenStack compatibility, we support a special-case to allow incoming traffic
// to the OpenStack metadata IP/port.
// TODO(smc) Long-term, it'd be nice if the OpenStack plugin programmed a policy to
// do this instead.
log.WithField("ip", r.OpenStackMetadataIP).Info(
"OpenStack metadata IP specified, installing whitelist rule.")
rules = append(rules, Rule{
Match: Match().
Protocol("tcp").
DestNet(r.OpenStackMetadataIP.String()).
DestPorts(r.OpenStackMetadataPort),
Action: r.filterAllowAction,
})
}
// Again, for OpenStack compatibility, white-list certain protocols.
// TODO(smc) Long-term, it'd be nice if the OpenStack plugin programmed a policy to
// do this instead.
dhcpSrcPort := uint16(68)
dhcpDestPort := uint16(67)
if ipVersion == 6 {
dhcpSrcPort = uint16(546)
dhcpDestPort = uint16(547)
}
dnsDestPort := uint16(53)
rules = append(rules,
Rule{
Match: Match().
Protocol("udp").
SourcePorts(dhcpSrcPort).
DestPorts(dhcpDestPort),
Action: r.filterAllowAction,
},
Rule{
Match: Match().
Protocol("udp").
DestPorts(dnsDestPort),
Action: r.filterAllowAction,
},
)
}
// Now send traffic to the policy chains to apply the egress policy.
rules = append(rules, Rule{
Action: JumpAction{Target: ChainFromWorkloadDispatch},
})
// If the dispatch chain accepts the packet, it returns to us here. Apply the configured
// action. Note: we may have done work above to allow the packet and then end up dropping
// it here. We can't optimize that away because there may be other rules (such as log
// rules in the policy).
for _, action := range r.inputAcceptActions {
rules = append(rules, Rule{
Action: action,
Comment: "Configured DefaultEndpointToHostAction",
})
}
return &Chain{
Name: ChainWorkloadToHost,
Rules: rules,
}
}
func (r *DefaultRuleRenderer) failsafeInChain(table string) *Chain {
rules := []Rule{}
for _, protoPort := range r.Config.FailsafeInboundHostPorts {
rules = append(rules, Rule{
Match: Match().
Protocol(protoPort.Protocol).
DestPorts(protoPort.Port),
Action: AcceptAction{},
})
}
if table == "raw" {
// We're in the raw table, before conntrack, so we need to whitelist response traffic.
// Otherwise, it could fall through to some doNotTrack policy and half of the connection
// would get untracked. If we ACCEPT here then the traffic falls through to the filter
// table, where it'll only be accepted if there's a conntrack entry.
for _, protoPort := range r.Config.FailsafeOutboundHostPorts {
rules = append(rules, Rule{
Match: Match().
Protocol(protoPort.Protocol).
SourcePorts(protoPort.Port),
Action: AcceptAction{},
})
}
}
return &Chain{
Name: ChainFailsafeIn,
Rules: rules,
}
}
func (r *DefaultRuleRenderer) failsafeOutChain(table string) *Chain {
rules := []Rule{}
for _, protoPort := range r.Config.FailsafeOutboundHostPorts {
rules = append(rules, Rule{
Match: Match().
Protocol(protoPort.Protocol).
DestPorts(protoPort.Port),
Action: AcceptAction{},
})
}
if table == "raw" {
// We're in the raw table, before conntrack, so we need to whitelist response traffic.
// Otherwise, it could fall through to some doNotTrack policy and half of the connection
// would get untracked. If we ACCEPT here then the traffic falls through to the filter
// table, where it'll only be accepted if there's a conntrack entry.
for _, protoPort := range r.Config.FailsafeInboundHostPorts {
rules = append(rules, Rule{
Match: Match().
Protocol(protoPort.Protocol).
SourcePorts(protoPort.Port),
Action: AcceptAction{},
})
}
}
return &Chain{
Name: ChainFailsafeOut,
Rules: rules,
}
}
func (r *DefaultRuleRenderer) StaticFilterForwardChains() []*Chain {
rules := []Rule{}
// Rules for filter forward chains dispatches the packet to our dispatch chains if it is going
// to/from an interface that we're responsible for. Note: the dispatch chains represent "allow"
// by returning to this chain for further processing; this is required to handle traffic that
// is going between endpoints on the same host. In that case we need to apply the egress policy
// for one endpoint and the ingress policy for the other.
//
// Packets will be accepted if they passed through both workload and host endpoint policy
// and were returned.
// Jump to from-host-endpoint dispatch chains.
rules = append(rules,
Rule{
// we're clearing all our mark bits to minimise non-determinism caused by rules in other chains.
// We exclude the accept bit because we use that to communicate from the raw/pre-dnat chains.
Action: ClearMarkAction{Mark: r.allCalicoMarkBits() &^ r.IptablesMarkAccept},
},
Rule{
// Apply forward policy for the incoming Host endpoint if accept bit is clear which means the packet
// was not accepted in a previous raw or pre-DNAT chain.
Match: Match().MarkClear(r.IptablesMarkAccept),
Action: JumpAction{Target: ChainDispatchFromHostEndPointForward},
},
)
// Jump to workload dispatch chains.
for _, prefix := range r.WorkloadIfacePrefixes {
log.WithField("ifacePrefix", prefix).Debug("Adding workload match rules")
ifaceMatch := prefix + "+"
rules = append(rules,
Rule{
Match: Match().InInterface(ifaceMatch),
Action: JumpAction{Target: ChainFromWorkloadDispatch},
},
Rule{
Match: Match().OutInterface(ifaceMatch),
Action: JumpAction{Target: ChainToWorkloadDispatch},
},
)
}
// Jump to to-host-endpoint dispatch chains.
rules = append(rules,
Rule{
// Apply forward policy for the outgoing host endpoint.
Action: JumpAction{Target: ChainDispatchToHostEndpointForward},
},
)
// Accept packet if policies above set ACCEPT mark.
rules = append(rules,
Rule{
Match: Match().MarkSingleBitSet(r.IptablesMarkAccept),
Action: r.filterAllowAction,
Comment: "Policy explicitly accepted packet.",
},
)
return []*Chain{{
Name: ChainFilterForward,
Rules: rules,
}}
}
func (r *DefaultRuleRenderer) StaticFilterOutputChains(ipVersion uint8) []*Chain {
result := []*Chain{}
result = append(result,
r.filterOutputChain(ipVersion),
r.failsafeOutChain("filter"),
)
if r.KubeIPVSSupportEnabled {
result = append(result, r.StaticFilterOutputForwardEndpointMarkChain())
}
return result
}
func (r *DefaultRuleRenderer) filterOutputChain(ipVersion uint8) *Chain {
var rules []Rule
// Accept immediately if we've already accepted this packet in the raw or mangle table.
rules = append(rules, r.acceptAlreadyAccepted()...)
if r.KubeIPVSSupportEnabled {
// Special case: packets that are forwarded through IPVS hit the INPUT and OUTPUT chains
// instead of FORWARD. In the INPUT chain, we mark such packets with a per-interface ID.
// Divert those packets to a chain that handles them as we would if they had hit the FORWARD
// chain.
//
// We use a goto so that a RETURN from that chain will continue execution in the OUTPUT
// chain.
rules = append(rules,
Rule{
Match: Match().MarkNotClear(r.IptablesMarkEndpoint),
Action: GotoAction{Target: ChainForwardEndpointMark},
},
)
}
// We don't currently police host -> endpoint according to the endpoint's ingress policy.
// That decision is based on pragmatism; it's generally very useful to be able to contact
// any local workload from the host and policing the traffic doesn't really protect
// against host compromise. If a host is compromised, then the rules could be removed!
// However, we do apply policy to workload ingress traffic if it belongs to an IPVS connection.
for _, prefix := range r.WorkloadIfacePrefixes {
// If the packet is going to a workload endpoint, apply workload ingress policy if traffic
// belongs to an IPVS connection and return at the end.
log.WithField("ifacePrefix", prefix).Debug("Adding workload match rules")
ifaceMatch := prefix + "+"
rules = append(rules,
Rule{
// if packet goes to a workload endpoint. set return action properly.
Match: Match().OutInterface(ifaceMatch),
Action: ReturnAction{},
},
)
}
// If we reach here, the packet is not going to a workload so it must be going to a
// host endpoint. It also has no endpoint mark so it must be going from a process.
if ipVersion == 4 && r.IPIPEnabled {
// When IPIP is enabled, auto-allow IPIP traffic to other Calico nodes. Without this,
// it's too easy to make a host policy that blocks IPIP traffic, resulting in very confusing
// connectivity problems.
rules = append(rules,
Rule{
Match: Match().ProtocolNum(ProtoIPIP).
DestIPSet(r.IPSetConfigV4.NameForMainIPSet(IPSetIDAllHostNets)).
SrcAddrType(AddrTypeLocal, false),
Action: r.filterAllowAction,
Comment: "Allow IPIP packets to other Calico hosts",
},
)
}
// Apply host endpoint policy.
rules = append(rules,
Rule{
Action: ClearMarkAction{Mark: r.allCalicoMarkBits()},
},
Rule{
Action: JumpAction{Target: ChainDispatchToHostEndpoint},
},
Rule{
Match: Match().MarkSingleBitSet(r.IptablesMarkAccept),
Action: r.filterAllowAction,
Comment: "Host endpoint policy accepted packet.",
},
)
return &Chain{
Name: ChainFilterOutput,
Rules: rules,
}
}
func (r *DefaultRuleRenderer) StaticNATTableChains(ipVersion uint8) (chains []*Chain) {
chains = append(chains, r.StaticNATPreroutingChains(ipVersion)...)
chains = append(chains, r.StaticNATPostroutingChains(ipVersion)...)
chains = append(chains, r.StaticNATOutputChains(ipVersion)...)
return
}
func (r *DefaultRuleRenderer) StaticNATPreroutingChains(ipVersion uint8) []*Chain {
rules := []Rule{
{
Action: JumpAction{Target: ChainFIPDnat},
},
}
if ipVersion == 4 && r.OpenStackSpecialCasesEnabled && r.OpenStackMetadataIP != nil {
rules = append(rules, Rule{
Match: Match().
Protocol("tcp").
DestPorts(80).
DestNet("169.254.169.254/32"),
Action: DNATAction{
DestAddr: r.OpenStackMetadataIP.String(),
DestPort: r.OpenStackMetadataPort,
},
})
}
return []*Chain{{
Name: ChainNATPrerouting,
Rules: rules,
}}
}
func (r *DefaultRuleRenderer) StaticNATPostroutingChains(ipVersion uint8) []*Chain {
rules := []Rule{
{
Action: JumpAction{Target: ChainFIPSnat},
},
{
Action: JumpAction{Target: ChainNATOutgoing},
},
}
if ipVersion == 4 && r.IPIPEnabled && len(r.IPIPTunnelAddress) > 0 {
// Add a rule to catch packets that are being sent down the IPIP tunnel from an
// incorrect local IP address of the host and NAT them to use the tunnel IP as its
// source. This happens if:
//
// - the user explicitly binds their socket to the wrong source IP accidentally
// - the user sends traffic to, for example, a Kubernetes service IP, which is
// implemented via NAT instead of routing, leading the kernel to choose the
// wrong source IP.
//
// We NAT the source of the packet to use the tunnel IP. We assume that
// non-local IPs have been correctly routed. Since Calico-assigned IPs are
// non-local (because they're down a veth), they won't get caught by the rule.
// Other remote sources will only reach the tunnel if they're being NATted
// already (for example, a Kubernetes "NodePort"). The kernel will then
// choose the correct source on its own.
rules = append(rules, Rule{
Match: Match().
// Only match packets going out the tunnel.
OutInterface("tunl0").
// Match packets that don't have the correct source address. This
// matches local addresses (i.e. ones assigned to this host)
// limiting the match to the output interface (which we matched
// above as the tunnel). Avoiding embedding the IP address lets
// us use a static rule, which is easier to manage.
NotSrcAddrType(AddrTypeLocal, true).
// Only match if the IP is also some local IP on the box. This
// prevents us from matching packets from workloads, which are
// remote as far as the routing table is concerned.
SrcAddrType(AddrTypeLocal, false),
Action: MasqAction{},
})
}
return []*Chain{{
Name: ChainNATPostrouting,
Rules: rules,
}}
}
func (r *DefaultRuleRenderer) StaticNATOutputChains(ipVersion uint8) []*Chain {
rules := []Rule{
{
Action: JumpAction{Target: ChainFIPDnat},
},
}
return []*Chain{{
Name: ChainNATOutput,
Rules: rules,
}}
}
func (r *DefaultRuleRenderer) StaticMangleTableChains(ipVersion uint8) (chains []*Chain) {
return []*Chain{
r.failsafeInChain("mangle"),
r.StaticManglePreroutingChain(ipVersion),
}
}
func (r *DefaultRuleRenderer) StaticManglePreroutingChain(ipVersion uint8) *Chain {
rules := []Rule{}
// ACCEPT or RETURN immediately if packet matches an existing connection. Note that we also
// have a rule like this at the start of each pre-endpoint chain; the functional difference
// with placing this rule here is that it will also apply to packets that may be unrelated
// to Calico (i.e. not to or from Calico workloads, and not via Calico host endpoints). We
// think this is appropriate in the mangle table here - whereas we don't have a rule like
// this in the filter table - because the mangle table is generally not used (except by us)
// for dropping packets, so it is very unlikely that we would be circumventing someone
// else's rule to drop a packet. (And in that case, the user can configure
// IptablesMangleAllowAction to be RETURN.)
rules = append(rules,
Rule{
Match: Match().ConntrackState("RELATED,ESTABLISHED"),
Action: r.mangleAllowAction,
},
)
// Or if we've already accepted this packet in the raw table.
rules = append(rules,
Rule{
Match: Match().MarkSingleBitSet(r.IptablesMarkAccept),
Action: r.mangleAllowAction,
},
)
// Now (=> not from a workload) dispatch to host endpoint chain for the incoming interface.
rules = append(rules,
Rule{
Action: JumpAction{Target: ChainDispatchFromHostEndpoint},
},
// Following that... If the packet was explicitly allowed by a pre-DNAT policy, it
// will have MarkAccept set. If the packet was denied, it will have been dropped
// already. If the incoming interface isn't one that we're policing, or the packet
// isn't governed by any pre-DNAT policy on that interface, it will fall through to
// here without any Calico bits set.
// In the MarkAccept case, we ACCEPT or RETURN according to
// IptablesMangleAllowAction.
Rule{
Match: Match().MarkSingleBitSet(r.IptablesMarkAccept),
Action: r.mangleAllowAction,
Comment: "Host endpoint policy accepted packet.",
},
)
return &Chain{
Name: ChainManglePrerouting,
Rules: rules,
}
}
func (r *DefaultRuleRenderer) StaticRawTableChains(ipVersion uint8) []*Chain {
return []*Chain{
r.failsafeInChain("raw"),
r.failsafeOutChain("raw"),
r.StaticRawPreroutingChain(ipVersion),
r.StaticRawOutputChain(),
}
}
func (r *DefaultRuleRenderer) StaticRawPreroutingChain(ipVersion uint8) *Chain {
rules := []Rule{}
// For safety, clear all our mark bits before we start. (We could be in append mode and
// another process' rules could have left the mark bit set.)
rules = append(rules,
Rule{Action: ClearMarkAction{Mark: r.allCalicoMarkBits()}},
)
// Set a mark on the packet if it's from a workload interface.
markFromWorkload := r.IptablesMarkScratch0
for _, ifacePrefix := range r.WorkloadIfacePrefixes {
rules = append(rules, Rule{
Match: Match().InInterface(ifacePrefix + "+"),
Action: SetMarkAction{Mark: markFromWorkload},
})
}
if ipVersion == 6 {
// Apply strict RPF check to packets from workload interfaces. This prevents
// workloads from spoofing their IPs. Note: non-privileged containers can't
// usually spoof but privileged containers and VMs can.
//
// We only do this for IPv6 because the IPv4 RPF check is handled via a sysctl.
// In addition, the IPv4 check is complicated by the fact that we have special
// case handling for DHCP to the host, which would require an exclusion.
rules = append(rules, Rule{
Match: Match().MarkSingleBitSet(markFromWorkload).RPFCheckFailed(),
Action: DropAction{},
})
}
rules = append(rules,
// Send non-workload traffic to the untracked policy chains.
Rule{Match: Match().MarkClear(markFromWorkload),
Action: JumpAction{Target: ChainDispatchFromHostEndpoint}},
// Then, if the packet was marked as allowed, accept it. Packets also return here
// without the mark bit set if the interface wasn't one that we're policing. We
// let those packets fall through to the user's policy.
Rule{Match: Match().MarkSingleBitSet(r.IptablesMarkAccept),
Action: AcceptAction{}},
)
return &Chain{
Name: ChainRawPrerouting,
Rules: rules,
}
}
func (r *DefaultRuleRenderer) allCalicoMarkBits() uint32 {
return r.IptablesMarkAccept |
r.IptablesMarkPass |
r.IptablesMarkScratch0 |
r.IptablesMarkScratch1
}
func (r *DefaultRuleRenderer) StaticRawOutputChain() *Chain {
return &Chain{
Name: ChainRawOutput,
Rules: []Rule{
// For safety, clear all our mark bits before we start. (We could be in
// append mode and another process' rules could have left the mark bit set.)
{Action: ClearMarkAction{Mark: r.allCalicoMarkBits()}},
// Then, jump to the untracked policy chains.
{Action: JumpAction{Target: ChainDispatchToHostEndpoint}},
// Then, if the packet was marked as allowed, accept it. Packets also
// return here without the mark bit set if the interface wasn't one that
// we're policing.
{Match: Match().MarkSingleBitSet(r.IptablesMarkAccept),
Action: AcceptAction{}},
},
}
}