service_linux.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  1. package libnetwork
  2. import (
  3. "fmt"
  4. "io"
  5. "io/ioutil"
  6. "net"
  7. "os"
  8. "os/exec"
  9. "path/filepath"
  10. "runtime"
  11. "strconv"
  12. "strings"
  13. "sync"
  14. "syscall"
  15. "github.com/docker/docker/pkg/reexec"
  16. "github.com/docker/libnetwork/iptables"
  17. "github.com/docker/libnetwork/ipvs"
  18. "github.com/docker/libnetwork/ns"
  19. "github.com/gogo/protobuf/proto"
  20. "github.com/ishidawataru/sctp"
  21. "github.com/sirupsen/logrus"
  22. "github.com/vishvananda/netlink/nl"
  23. "github.com/vishvananda/netns"
  24. )
  25. func init() {
  26. reexec.Register("fwmarker", fwMarker)
  27. reexec.Register("redirecter", redirecter)
  28. }
  29. // Get all loadbalancers on this network that is currently discovered
  30. // on this node.
  31. func (n *network) connectedLoadbalancers() []*loadBalancer {
  32. c := n.getController()
  33. c.Lock()
  34. serviceBindings := make([]*service, 0, len(c.serviceBindings))
  35. for _, s := range c.serviceBindings {
  36. serviceBindings = append(serviceBindings, s)
  37. }
  38. c.Unlock()
  39. var lbs []*loadBalancer
  40. for _, s := range serviceBindings {
  41. s.Lock()
  42. // Skip the serviceBindings that got deleted
  43. if s.deleted {
  44. s.Unlock()
  45. continue
  46. }
  47. if lb, ok := s.loadBalancers[n.ID()]; ok {
  48. lbs = append(lbs, lb)
  49. }
  50. s.Unlock()
  51. }
  52. return lbs
  53. }
  54. // Populate all loadbalancers on the network that the passed endpoint
  55. // belongs to, into this sandbox.
  56. func (sb *sandbox) populateLoadbalancers(ep *endpoint) {
  57. var gwIP net.IP
  58. // This is an interface less endpoint. Nothing to do.
  59. if ep.Iface() == nil {
  60. return
  61. }
  62. n := ep.getNetwork()
  63. eIP := ep.Iface().Address()
  64. if n.ingress {
  65. if err := addRedirectRules(sb.Key(), eIP, ep.ingressPorts); err != nil {
  66. logrus.Errorf("Failed to add redirect rules for ep %s (%s): %v", ep.Name(), ep.ID()[0:7], err)
  67. }
  68. }
  69. if sb.ingress {
  70. // For the ingress sandbox if this is not gateway
  71. // endpoint do nothing.
  72. if ep != sb.getGatewayEndpoint() {
  73. return
  74. }
  75. // This is the gateway endpoint. Now get the ingress
  76. // network and plumb the loadbalancers.
  77. gwIP = ep.Iface().Address().IP
  78. for _, ep := range sb.getConnectedEndpoints() {
  79. if !ep.endpointInGWNetwork() {
  80. n = ep.getNetwork()
  81. eIP = ep.Iface().Address()
  82. }
  83. }
  84. }
  85. for _, lb := range n.connectedLoadbalancers() {
  86. // Skip if vip is not valid.
  87. if len(lb.vip) == 0 {
  88. continue
  89. }
  90. lb.service.Lock()
  91. for _, be := range lb.backEnds {
  92. if !be.disabled {
  93. sb.addLBBackend(be.ip, lb.vip, lb.fwMark, lb.service.ingressPorts, eIP, gwIP, n.ingress)
  94. }
  95. }
  96. lb.service.Unlock()
  97. }
  98. }
  99. // Add loadbalancer backend to all sandboxes which has a connection to
  100. // this network. If needed add the service as well.
  101. func (n *network) addLBBackend(ip, vip net.IP, lb *loadBalancer, ingressPorts []*PortConfig) {
  102. n.WalkEndpoints(func(e Endpoint) bool {
  103. ep := e.(*endpoint)
  104. if sb, ok := ep.getSandbox(); ok {
  105. if !sb.isEndpointPopulated(ep) {
  106. return false
  107. }
  108. var gwIP net.IP
  109. if ep := sb.getGatewayEndpoint(); ep != nil {
  110. gwIP = ep.Iface().Address().IP
  111. }
  112. sb.addLBBackend(ip, vip, lb.fwMark, ingressPorts, ep.Iface().Address(), gwIP, n.ingress)
  113. }
  114. return false
  115. })
  116. }
  117. // Remove loadbalancer backend from all sandboxes which has a
  118. // connection to this network. If needed remove the service entry as
  119. // well, as specified by the rmService bool.
  120. func (n *network) rmLBBackend(ip, vip net.IP, lb *loadBalancer, ingressPorts []*PortConfig, rmService bool, fullRemove bool) {
  121. n.WalkEndpoints(func(e Endpoint) bool {
  122. ep := e.(*endpoint)
  123. if sb, ok := ep.getSandbox(); ok {
  124. if !sb.isEndpointPopulated(ep) {
  125. return false
  126. }
  127. var gwIP net.IP
  128. if ep := sb.getGatewayEndpoint(); ep != nil {
  129. gwIP = ep.Iface().Address().IP
  130. }
  131. sb.rmLBBackend(ip, vip, lb.fwMark, ingressPorts, ep.Iface().Address(), gwIP, rmService, fullRemove, n.ingress)
  132. }
  133. return false
  134. })
  135. }
  136. // Add loadbalancer backend into one connected sandbox.
  137. func (sb *sandbox) addLBBackend(ip, vip net.IP, fwMark uint32, ingressPorts []*PortConfig, eIP *net.IPNet, gwIP net.IP, isIngressNetwork bool) {
  138. if sb.osSbox == nil {
  139. return
  140. }
  141. if isIngressNetwork && !sb.ingress {
  142. return
  143. }
  144. i, err := ipvs.New(sb.Key())
  145. if err != nil {
  146. logrus.Errorf("Failed to create an ipvs handle for sbox %s (%s,%s) for lb addition: %v", sb.ID()[0:7], sb.ContainerID()[0:7], sb.Key(), err)
  147. return
  148. }
  149. defer i.Close()
  150. s := &ipvs.Service{
  151. AddressFamily: nl.FAMILY_V4,
  152. FWMark: fwMark,
  153. SchedName: ipvs.RoundRobin,
  154. }
  155. if !i.IsServicePresent(s) {
  156. var filteredPorts []*PortConfig
  157. if sb.ingress {
  158. filteredPorts = filterPortConfigs(ingressPorts, false)
  159. if err := programIngress(gwIP, filteredPorts, false); err != nil {
  160. logrus.Errorf("Failed to add ingress: %v", err)
  161. return
  162. }
  163. }
  164. logrus.Debugf("Creating service for vip %s fwMark %d ingressPorts %#v in sbox %s (%s)", vip, fwMark, ingressPorts, sb.ID()[0:7], sb.ContainerID()[0:7])
  165. if err := invokeFWMarker(sb.Key(), vip, fwMark, ingressPorts, eIP, false); err != nil {
  166. logrus.Errorf("Failed to add firewall mark rule in sbox %s (%s): %v", sb.ID()[0:7], sb.ContainerID()[0:7], err)
  167. return
  168. }
  169. if err := i.NewService(s); err != nil && err != syscall.EEXIST {
  170. logrus.Errorf("Failed to create a new service for vip %s fwmark %d in sbox %s (%s): %v", vip, fwMark, sb.ID()[0:7], sb.ContainerID()[0:7], err)
  171. return
  172. }
  173. }
  174. d := &ipvs.Destination{
  175. AddressFamily: nl.FAMILY_V4,
  176. Address: ip,
  177. Weight: 1,
  178. }
  179. // Remove the sched name before using the service to add
  180. // destination.
  181. s.SchedName = ""
  182. if err := i.NewDestination(s, d); err != nil && err != syscall.EEXIST {
  183. logrus.Errorf("Failed to create real server %s for vip %s fwmark %d in sbox %s (%s): %v", ip, vip, fwMark, sb.ID()[0:7], sb.ContainerID()[0:7], err)
  184. }
  185. }
  186. // Remove loadbalancer backend from one connected sandbox.
  187. func (sb *sandbox) rmLBBackend(ip, vip net.IP, fwMark uint32, ingressPorts []*PortConfig, eIP *net.IPNet, gwIP net.IP, rmService bool, fullRemove bool, isIngressNetwork bool) {
  188. if sb.osSbox == nil {
  189. return
  190. }
  191. if isIngressNetwork && !sb.ingress {
  192. return
  193. }
  194. i, err := ipvs.New(sb.Key())
  195. if err != nil {
  196. logrus.Errorf("Failed to create an ipvs handle for sbox %s (%s,%s) for lb removal: %v", sb.ID()[0:7], sb.ContainerID()[0:7], sb.Key(), err)
  197. return
  198. }
  199. defer i.Close()
  200. s := &ipvs.Service{
  201. AddressFamily: nl.FAMILY_V4,
  202. FWMark: fwMark,
  203. }
  204. d := &ipvs.Destination{
  205. AddressFamily: nl.FAMILY_V4,
  206. Address: ip,
  207. Weight: 1,
  208. }
  209. if fullRemove {
  210. if err := i.DelDestination(s, d); err != nil && err != syscall.ENOENT {
  211. logrus.Errorf("Failed to delete real server %s for vip %s fwmark %d in sbox %s (%s): %v", ip, vip, fwMark, sb.ID()[0:7], sb.ContainerID()[0:7], err)
  212. }
  213. } else {
  214. d.Weight = 0
  215. if err := i.UpdateDestination(s, d); err != nil && err != syscall.ENOENT {
  216. logrus.Errorf("Failed to set LB weight of real server %s to 0 for vip %s fwmark %d in sbox %s (%s): %v", ip, vip, fwMark, sb.ID()[0:7], sb.ContainerID()[0:7], err)
  217. }
  218. }
  219. if rmService {
  220. s.SchedName = ipvs.RoundRobin
  221. if err := i.DelService(s); err != nil && err != syscall.ENOENT {
  222. logrus.Errorf("Failed to delete service for vip %s fwmark %d in sbox %s (%s): %v", vip, fwMark, sb.ID()[0:7], sb.ContainerID()[0:7], err)
  223. }
  224. var filteredPorts []*PortConfig
  225. if sb.ingress {
  226. filteredPorts = filterPortConfigs(ingressPorts, true)
  227. if err := programIngress(gwIP, filteredPorts, true); err != nil {
  228. logrus.Errorf("Failed to delete ingress: %v", err)
  229. }
  230. }
  231. if err := invokeFWMarker(sb.Key(), vip, fwMark, ingressPorts, eIP, true); err != nil {
  232. logrus.Errorf("Failed to delete firewall mark rule in sbox %s (%s): %v", sb.ID()[0:7], sb.ContainerID()[0:7], err)
  233. }
  234. }
  235. }
  236. const ingressChain = "DOCKER-INGRESS"
  237. var (
  238. ingressOnce sync.Once
  239. ingressMu sync.Mutex // lock for operations on ingress
  240. ingressProxyTbl = make(map[string]io.Closer)
  241. portConfigMu sync.Mutex
  242. portConfigTbl = make(map[PortConfig]int)
  243. )
  244. func filterPortConfigs(ingressPorts []*PortConfig, isDelete bool) []*PortConfig {
  245. portConfigMu.Lock()
  246. iPorts := make([]*PortConfig, 0, len(ingressPorts))
  247. for _, pc := range ingressPorts {
  248. if isDelete {
  249. if cnt, ok := portConfigTbl[*pc]; ok {
  250. // This is the last reference to this
  251. // port config. Delete the port config
  252. // and add it to filtered list to be
  253. // plumbed.
  254. if cnt == 1 {
  255. delete(portConfigTbl, *pc)
  256. iPorts = append(iPorts, pc)
  257. continue
  258. }
  259. portConfigTbl[*pc] = cnt - 1
  260. }
  261. continue
  262. }
  263. if cnt, ok := portConfigTbl[*pc]; ok {
  264. portConfigTbl[*pc] = cnt + 1
  265. continue
  266. }
  267. // We are adding it for the first time. Add it to the
  268. // filter list to be plumbed.
  269. portConfigTbl[*pc] = 1
  270. iPorts = append(iPorts, pc)
  271. }
  272. portConfigMu.Unlock()
  273. return iPorts
  274. }
  275. func programIngress(gwIP net.IP, ingressPorts []*PortConfig, isDelete bool) error {
  276. addDelOpt := "-I"
  277. if isDelete {
  278. addDelOpt = "-D"
  279. }
  280. ingressMu.Lock()
  281. defer ingressMu.Unlock()
  282. chainExists := iptables.ExistChain(ingressChain, iptables.Nat)
  283. filterChainExists := iptables.ExistChain(ingressChain, iptables.Filter)
  284. ingressOnce.Do(func() {
  285. // Flush nat table and filter table ingress chain rules during init if it
  286. // exists. It might contain stale rules from previous life.
  287. if chainExists {
  288. if err := iptables.RawCombinedOutput("-t", "nat", "-F", ingressChain); err != nil {
  289. logrus.Errorf("Could not flush nat table ingress chain rules during init: %v", err)
  290. }
  291. }
  292. if filterChainExists {
  293. if err := iptables.RawCombinedOutput("-F", ingressChain); err != nil {
  294. logrus.Errorf("Could not flush filter table ingress chain rules during init: %v", err)
  295. }
  296. }
  297. })
  298. if !isDelete {
  299. if !chainExists {
  300. if err := iptables.RawCombinedOutput("-t", "nat", "-N", ingressChain); err != nil {
  301. return fmt.Errorf("failed to create ingress chain: %v", err)
  302. }
  303. }
  304. if !filterChainExists {
  305. if err := iptables.RawCombinedOutput("-N", ingressChain); err != nil {
  306. return fmt.Errorf("failed to create filter table ingress chain: %v", err)
  307. }
  308. }
  309. if !iptables.Exists(iptables.Nat, ingressChain, "-j", "RETURN") {
  310. if err := iptables.RawCombinedOutput("-t", "nat", "-A", ingressChain, "-j", "RETURN"); err != nil {
  311. return fmt.Errorf("failed to add return rule in nat table ingress chain: %v", err)
  312. }
  313. }
  314. if !iptables.Exists(iptables.Filter, ingressChain, "-j", "RETURN") {
  315. if err := iptables.RawCombinedOutput("-A", ingressChain, "-j", "RETURN"); err != nil {
  316. return fmt.Errorf("failed to add return rule to filter table ingress chain: %v", err)
  317. }
  318. }
  319. for _, chain := range []string{"OUTPUT", "PREROUTING"} {
  320. if !iptables.Exists(iptables.Nat, chain, "-m", "addrtype", "--dst-type", "LOCAL", "-j", ingressChain) {
  321. if err := iptables.RawCombinedOutput("-t", "nat", "-I", chain, "-m", "addrtype", "--dst-type", "LOCAL", "-j", ingressChain); err != nil {
  322. return fmt.Errorf("failed to add jump rule in %s to ingress chain: %v", chain, err)
  323. }
  324. }
  325. }
  326. if !iptables.Exists(iptables.Filter, "FORWARD", "-j", ingressChain) {
  327. if err := iptables.RawCombinedOutput("-I", "FORWARD", "-j", ingressChain); err != nil {
  328. return fmt.Errorf("failed to add jump rule to %s in filter table forward chain: %v", ingressChain, err)
  329. }
  330. arrangeUserFilterRule()
  331. }
  332. oifName, err := findOIFName(gwIP)
  333. if err != nil {
  334. return fmt.Errorf("failed to find gateway bridge interface name for %s: %v", gwIP, err)
  335. }
  336. path := filepath.Join("/proc/sys/net/ipv4/conf", oifName, "route_localnet")
  337. if err := ioutil.WriteFile(path, []byte{'1', '\n'}, 0644); err != nil {
  338. return fmt.Errorf("could not write to %s: %v", path, err)
  339. }
  340. ruleArgs := strings.Fields(fmt.Sprintf("-m addrtype --src-type LOCAL -o %s -j MASQUERADE", oifName))
  341. if !iptables.Exists(iptables.Nat, "POSTROUTING", ruleArgs...) {
  342. if err := iptables.RawCombinedOutput(append([]string{"-t", "nat", "-I", "POSTROUTING"}, ruleArgs...)...); err != nil {
  343. return fmt.Errorf("failed to add ingress localhost POSTROUTING rule for %s: %v", oifName, err)
  344. }
  345. }
  346. }
  347. for _, iPort := range ingressPorts {
  348. if iptables.ExistChain(ingressChain, iptables.Nat) {
  349. rule := strings.Fields(fmt.Sprintf("-t nat %s %s -p %s --dport %d -j DNAT --to-destination %s:%d",
  350. addDelOpt, ingressChain, strings.ToLower(PortConfig_Protocol_name[int32(iPort.Protocol)]), iPort.PublishedPort, gwIP, iPort.PublishedPort))
  351. if err := iptables.RawCombinedOutput(rule...); err != nil {
  352. errStr := fmt.Sprintf("setting up rule failed, %v: %v", rule, err)
  353. if !isDelete {
  354. return fmt.Errorf("%s", errStr)
  355. }
  356. logrus.Infof("%s", errStr)
  357. }
  358. }
  359. // Filter table rules to allow a published service to be accessible in the local node from..
  360. // 1) service tasks attached to other networks
  361. // 2) unmanaged containers on bridge networks
  362. rule := strings.Fields(fmt.Sprintf("%s %s -m state -p %s --sport %d --state ESTABLISHED,RELATED -j ACCEPT",
  363. addDelOpt, ingressChain, strings.ToLower(PortConfig_Protocol_name[int32(iPort.Protocol)]), iPort.PublishedPort))
  364. if err := iptables.RawCombinedOutput(rule...); err != nil {
  365. errStr := fmt.Sprintf("setting up rule failed, %v: %v", rule, err)
  366. if !isDelete {
  367. return fmt.Errorf("%s", errStr)
  368. }
  369. logrus.Warnf("%s", errStr)
  370. }
  371. rule = strings.Fields(fmt.Sprintf("%s %s -p %s --dport %d -j ACCEPT",
  372. addDelOpt, ingressChain, strings.ToLower(PortConfig_Protocol_name[int32(iPort.Protocol)]), iPort.PublishedPort))
  373. if err := iptables.RawCombinedOutput(rule...); err != nil {
  374. errStr := fmt.Sprintf("setting up rule failed, %v: %v", rule, err)
  375. if !isDelete {
  376. return fmt.Errorf("%s", errStr)
  377. }
  378. logrus.Warnf("%s", errStr)
  379. }
  380. if err := plumbProxy(iPort, isDelete); err != nil {
  381. logrus.Warnf("failed to create proxy for port %d: %v", iPort.PublishedPort, err)
  382. }
  383. }
  384. return nil
  385. }
  386. // In the filter table FORWARD chain the first rule should be to jump to
  387. // DOCKER-USER so the user is able to filter packet first.
  388. // The second rule should be jump to INGRESS-CHAIN.
  389. // This chain has the rules to allow access to the published ports for swarm tasks
  390. // from local bridge networks and docker_gwbridge (ie:taks on other swarm netwroks)
  391. func arrangeIngressFilterRule() {
  392. if iptables.ExistChain(ingressChain, iptables.Filter) {
  393. if iptables.Exists(iptables.Filter, "FORWARD", "-j", ingressChain) {
  394. if err := iptables.RawCombinedOutput("-D", "FORWARD", "-j", ingressChain); err != nil {
  395. logrus.Warnf("failed to delete jump rule to ingressChain in filter table: %v", err)
  396. }
  397. }
  398. if err := iptables.RawCombinedOutput("-I", "FORWARD", "-j", ingressChain); err != nil {
  399. logrus.Warnf("failed to add jump rule to ingressChain in filter table: %v", err)
  400. }
  401. }
  402. }
  403. func findOIFName(ip net.IP) (string, error) {
  404. nlh := ns.NlHandle()
  405. routes, err := nlh.RouteGet(ip)
  406. if err != nil {
  407. return "", err
  408. }
  409. if len(routes) == 0 {
  410. return "", fmt.Errorf("no route to %s", ip)
  411. }
  412. // Pick the first route(typically there is only one route). We
  413. // don't support multipath.
  414. link, err := nlh.LinkByIndex(routes[0].LinkIndex)
  415. if err != nil {
  416. return "", err
  417. }
  418. return link.Attrs().Name, nil
  419. }
  420. func plumbProxy(iPort *PortConfig, isDelete bool) error {
  421. var (
  422. err error
  423. l io.Closer
  424. )
  425. portSpec := fmt.Sprintf("%d/%s", iPort.PublishedPort, strings.ToLower(PortConfig_Protocol_name[int32(iPort.Protocol)]))
  426. if isDelete {
  427. if listener, ok := ingressProxyTbl[portSpec]; ok {
  428. if listener != nil {
  429. listener.Close()
  430. }
  431. }
  432. return nil
  433. }
  434. switch iPort.Protocol {
  435. case ProtocolTCP:
  436. l, err = net.ListenTCP("tcp", &net.TCPAddr{Port: int(iPort.PublishedPort)})
  437. case ProtocolUDP:
  438. l, err = net.ListenUDP("udp", &net.UDPAddr{Port: int(iPort.PublishedPort)})
  439. case ProtocolSCTP:
  440. l, err = sctp.ListenSCTP("sctp", &sctp.SCTPAddr{Port: int(iPort.PublishedPort)})
  441. default:
  442. err = fmt.Errorf("unknown protocol %v", iPort.Protocol)
  443. }
  444. if err != nil {
  445. return err
  446. }
  447. ingressProxyTbl[portSpec] = l
  448. return nil
  449. }
  450. func writePortsToFile(ports []*PortConfig) (string, error) {
  451. f, err := ioutil.TempFile("", "port_configs")
  452. if err != nil {
  453. return "", err
  454. }
  455. defer f.Close()
  456. buf, _ := proto.Marshal(&EndpointRecord{
  457. IngressPorts: ports,
  458. })
  459. n, err := f.Write(buf)
  460. if err != nil {
  461. return "", err
  462. }
  463. if n < len(buf) {
  464. return "", io.ErrShortWrite
  465. }
  466. return f.Name(), nil
  467. }
  468. func readPortsFromFile(fileName string) ([]*PortConfig, error) {
  469. buf, err := ioutil.ReadFile(fileName)
  470. if err != nil {
  471. return nil, err
  472. }
  473. var epRec EndpointRecord
  474. err = proto.Unmarshal(buf, &epRec)
  475. if err != nil {
  476. return nil, err
  477. }
  478. return epRec.IngressPorts, nil
  479. }
  480. // Invoke fwmarker reexec routine to mark vip destined packets with
  481. // the passed firewall mark.
  482. func invokeFWMarker(path string, vip net.IP, fwMark uint32, ingressPorts []*PortConfig, eIP *net.IPNet, isDelete bool) error {
  483. var ingressPortsFile string
  484. if len(ingressPorts) != 0 {
  485. var err error
  486. ingressPortsFile, err = writePortsToFile(ingressPorts)
  487. if err != nil {
  488. return err
  489. }
  490. defer os.Remove(ingressPortsFile)
  491. }
  492. addDelOpt := "-A"
  493. if isDelete {
  494. addDelOpt = "-D"
  495. }
  496. cmd := &exec.Cmd{
  497. Path: reexec.Self(),
  498. Args: append([]string{"fwmarker"}, path, vip.String(), fmt.Sprintf("%d", fwMark), addDelOpt, ingressPortsFile, eIP.String()),
  499. Stdout: os.Stdout,
  500. Stderr: os.Stderr,
  501. }
  502. if err := cmd.Run(); err != nil {
  503. return fmt.Errorf("reexec failed: %v", err)
  504. }
  505. return nil
  506. }
  507. // Firewall marker reexec function.
  508. func fwMarker() {
  509. runtime.LockOSThread()
  510. defer runtime.UnlockOSThread()
  511. if len(os.Args) < 7 {
  512. logrus.Error("invalid number of arguments..")
  513. os.Exit(1)
  514. }
  515. var ingressPorts []*PortConfig
  516. if os.Args[5] != "" {
  517. var err error
  518. ingressPorts, err = readPortsFromFile(os.Args[5])
  519. if err != nil {
  520. logrus.Errorf("Failed reading ingress ports file: %v", err)
  521. os.Exit(6)
  522. }
  523. }
  524. vip := os.Args[2]
  525. fwMark, err := strconv.ParseUint(os.Args[3], 10, 32)
  526. if err != nil {
  527. logrus.Errorf("bad fwmark value(%s) passed: %v", os.Args[3], err)
  528. os.Exit(2)
  529. }
  530. addDelOpt := os.Args[4]
  531. rules := [][]string{}
  532. for _, iPort := range ingressPorts {
  533. rule := strings.Fields(fmt.Sprintf("-t mangle %s PREROUTING -p %s --dport %d -j MARK --set-mark %d",
  534. addDelOpt, strings.ToLower(PortConfig_Protocol_name[int32(iPort.Protocol)]), iPort.PublishedPort, fwMark))
  535. rules = append(rules, rule)
  536. }
  537. ns, err := netns.GetFromPath(os.Args[1])
  538. if err != nil {
  539. logrus.Errorf("failed get network namespace %q: %v", os.Args[1], err)
  540. os.Exit(3)
  541. }
  542. defer ns.Close()
  543. if err := netns.Set(ns); err != nil {
  544. logrus.Errorf("setting into container net ns %v failed, %v", os.Args[1], err)
  545. os.Exit(4)
  546. }
  547. if addDelOpt == "-A" {
  548. eIP, subnet, err := net.ParseCIDR(os.Args[6])
  549. if err != nil {
  550. logrus.Errorf("Failed to parse endpoint IP %s: %v", os.Args[6], err)
  551. os.Exit(9)
  552. }
  553. ruleParams := strings.Fields(fmt.Sprintf("-m ipvs --ipvs -d %s -j SNAT --to-source %s", subnet, eIP))
  554. if !iptables.Exists("nat", "POSTROUTING", ruleParams...) {
  555. rule := append(strings.Fields("-t nat -A POSTROUTING"), ruleParams...)
  556. rules = append(rules, rule)
  557. err := ioutil.WriteFile("/proc/sys/net/ipv4/vs/conntrack", []byte{'1', '\n'}, 0644)
  558. if err != nil {
  559. logrus.Errorf("Failed to write to /proc/sys/net/ipv4/vs/conntrack: %v", err)
  560. os.Exit(8)
  561. }
  562. }
  563. }
  564. rule := strings.Fields(fmt.Sprintf("-t mangle %s OUTPUT -d %s/32 -j MARK --set-mark %d", addDelOpt, vip, fwMark))
  565. rules = append(rules, rule)
  566. rule = strings.Fields(fmt.Sprintf("-t nat %s OUTPUT -p icmp --icmp echo-request -d %s -j DNAT --to 127.0.0.1", addDelOpt, vip))
  567. rules = append(rules, rule)
  568. for _, rule := range rules {
  569. if err := iptables.RawCombinedOutputNative(rule...); err != nil {
  570. logrus.Errorf("setting up rule failed, %v: %v", rule, err)
  571. os.Exit(5)
  572. }
  573. }
  574. }
  575. func addRedirectRules(path string, eIP *net.IPNet, ingressPorts []*PortConfig) error {
  576. var ingressPortsFile string
  577. if len(ingressPorts) != 0 {
  578. var err error
  579. ingressPortsFile, err = writePortsToFile(ingressPorts)
  580. if err != nil {
  581. return err
  582. }
  583. defer os.Remove(ingressPortsFile)
  584. }
  585. cmd := &exec.Cmd{
  586. Path: reexec.Self(),
  587. Args: append([]string{"redirecter"}, path, eIP.String(), ingressPortsFile),
  588. Stdout: os.Stdout,
  589. Stderr: os.Stderr,
  590. }
  591. if err := cmd.Run(); err != nil {
  592. return fmt.Errorf("reexec failed: %v", err)
  593. }
  594. return nil
  595. }
  596. // Redirecter reexec function.
  597. func redirecter() {
  598. runtime.LockOSThread()
  599. defer runtime.UnlockOSThread()
  600. if len(os.Args) < 4 {
  601. logrus.Error("invalid number of arguments..")
  602. os.Exit(1)
  603. }
  604. var ingressPorts []*PortConfig
  605. if os.Args[3] != "" {
  606. var err error
  607. ingressPorts, err = readPortsFromFile(os.Args[3])
  608. if err != nil {
  609. logrus.Errorf("Failed reading ingress ports file: %v", err)
  610. os.Exit(2)
  611. }
  612. }
  613. eIP, _, err := net.ParseCIDR(os.Args[2])
  614. if err != nil {
  615. logrus.Errorf("Failed to parse endpoint IP %s: %v", os.Args[2], err)
  616. os.Exit(3)
  617. }
  618. rules := [][]string{}
  619. for _, iPort := range ingressPorts {
  620. rule := strings.Fields(fmt.Sprintf("-t nat -A PREROUTING -d %s -p %s --dport %d -j REDIRECT --to-port %d",
  621. eIP.String(), strings.ToLower(PortConfig_Protocol_name[int32(iPort.Protocol)]), iPort.PublishedPort, iPort.TargetPort))
  622. rules = append(rules, rule)
  623. // Allow only incoming connections to exposed ports
  624. iRule := strings.Fields(fmt.Sprintf("-I INPUT -d %s -p %s --dport %d -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT",
  625. eIP.String(), strings.ToLower(PortConfig_Protocol_name[int32(iPort.Protocol)]), iPort.TargetPort))
  626. rules = append(rules, iRule)
  627. // Allow only outgoing connections from exposed ports
  628. oRule := strings.Fields(fmt.Sprintf("-I OUTPUT -s %s -p %s --sport %d -m conntrack --ctstate ESTABLISHED -j ACCEPT",
  629. eIP.String(), strings.ToLower(PortConfig_Protocol_name[int32(iPort.Protocol)]), iPort.TargetPort))
  630. rules = append(rules, oRule)
  631. }
  632. ns, err := netns.GetFromPath(os.Args[1])
  633. if err != nil {
  634. logrus.Errorf("failed get network namespace %q: %v", os.Args[1], err)
  635. os.Exit(4)
  636. }
  637. defer ns.Close()
  638. if err := netns.Set(ns); err != nil {
  639. logrus.Errorf("setting into container net ns %v failed, %v", os.Args[1], err)
  640. os.Exit(5)
  641. }
  642. for _, rule := range rules {
  643. if err := iptables.RawCombinedOutputNative(rule...); err != nil {
  644. logrus.Errorf("setting up rule failed, %v: %v", rule, err)
  645. os.Exit(6)
  646. }
  647. }
  648. if len(ingressPorts) == 0 {
  649. return
  650. }
  651. // Ensure blocking rules for anything else in/to ingress network
  652. for _, rule := range [][]string{
  653. {"-d", eIP.String(), "-p", "sctp", "-j", "DROP"},
  654. {"-d", eIP.String(), "-p", "udp", "-j", "DROP"},
  655. {"-d", eIP.String(), "-p", "tcp", "-j", "DROP"},
  656. } {
  657. if !iptables.ExistsNative(iptables.Filter, "INPUT", rule...) {
  658. if err := iptables.RawCombinedOutputNative(append([]string{"-A", "INPUT"}, rule...)...); err != nil {
  659. logrus.Errorf("setting up rule failed, %v: %v", rule, err)
  660. os.Exit(7)
  661. }
  662. }
  663. rule[0] = "-s"
  664. if !iptables.ExistsNative(iptables.Filter, "OUTPUT", rule...) {
  665. if err := iptables.RawCombinedOutputNative(append([]string{"-A", "OUTPUT"}, rule...)...); err != nil {
  666. logrus.Errorf("setting up rule failed, %v: %v", rule, err)
  667. os.Exit(8)
  668. }
  669. }
  670. }
  671. }