func setupIpMasq(ipn ip.IP4Net, iface string) error { ipt, err := ip.NewIPTables() if err != nil { return fmt.Errorf("failed to setup IP Masquerade. iptables was not found") } err = ipt.ClearChain("nat", "FLANNEL") if err != nil { return fmt.Errorf("Failed to create/clear FLANNEL chain in NAT table: %v", err) } rules := [][]string{ // This rule makes sure we don't NAT traffic within overlay network (e.g. coming out of docker0) {"FLANNEL", "-d", ipn.String(), "-j", "ACCEPT"}, // This rule makes sure we don't NAT multicast traffic within overlay network {"FLANNEL", "-d", "224.0.0.0/4", "-j", "ACCEPT"}, // This rule will NAT everything originating from our overlay network and {"FLANNEL", "!", "-o", iface, "-j", "MASQUERADE"}, // This rule will take everything coming from overlay and sent it to FLANNEL chain {"POSTROUTING", "-s", ipn.String(), "-j", "FLANNEL"}, } for _, args := range rules { log.Info("Adding iptables rule: ", strings.Join(args, " ")) err = ipt.AppendUnique("nat", args...) if err != nil { return fmt.Errorf("Failed to insert IP masquerade rule: %v", err) } } return nil }
func configureIface(ifname string, ipn ip.IP4Net, mtu int) error { iface, err := netlink.LinkByName(ifname) if err != nil { return fmt.Errorf("failed to lookup interface %v", ifname) } err = netlink.AddrAdd(iface, &netlink.Addr{ipn.ToIPNet(), ""}) if err != nil { return fmt.Errorf("failed to add IP address %v to %v: %v", ipn.String(), ifname, err) } err = netlink.LinkSetMTU(iface, mtu) if err != nil { return fmt.Errorf("failed to set MTU for %v: %v", ifname, err) } err = netlink.LinkSetUp(iface) if err != nil { return fmt.Errorf("failed to set interface %v to UP state: %v", ifname, err) } // explicitly add a route since there might be a route for a subnet already // installed by Docker and then it won't get auto added err = netlink.RouteAdd(&netlink.Route{ LinkIndex: iface.Attrs().Index, Scope: netlink.SCOPE_UNIVERSE, Dst: ipn.Network().ToIPNet(), }) if err != nil && err != syscall.EEXIST { return fmt.Errorf("Failed to add route (%v -> %v): %v", ipn.Network().String(), ifname, err) } return nil }
func setupIpMasq(localNet ip.IP4Net, overlayNet ip.IP4Net) error { ipt, err := ip.NewIPTables() if err != nil { return err } err = ipt.ClearChain("nat", "FLANNEL") if err != nil { return fmt.Errorf("Failed to create/clear FLANNEL chain in NAT table: %v", err) } rules := [][]string{ []string{"FLANNEL", "-s", localNet.String(), "-o", "lo", "-j", "ACCEPT"}, []string{"FLANNEL", "-s", localNet.String(), "!", "-d", overlayNet.String(), "-j", "MASQUERADE"}, []string{"POSTROUTING", "-s", localNet.String(), "-j", "FLANNEL"}, } for _, args := range rules { log.Info("Adding iptables rule: ", strings.Join(args, " ")) if err := ipt.AppendUnique("nat", args...); err != nil { return fmt.Errorf("Failed to insert IP masquerade rule: %v", err) } } return nil }
func (sm *SubnetManager) allocateSubnet() (ip.IP4Net, error) { log.Infof("Picking subnet in range %s ... %s", sm.config.SubnetMin, sm.config.SubnetMax) var bag []ip.IP4 sn := ip.IP4Net{IP: sm.config.SubnetMin, PrefixLen: sm.config.SubnetLen} OuterLoop: for ; sn.IP <= sm.config.SubnetMax && len(bag) < 100; sn = sn.Next() { for _, l := range sm.leases { if sn.Overlaps(l.Network) { continue OuterLoop } } bag = append(bag, sn.IP) } if len(bag) == 0 { return ip.IP4Net{}, errors.New("out of subnets") } else { i := randInt(0, len(bag)) return ip.IP4Net{IP: bag[i], PrefixLen: sm.config.SubnetLen}, nil } }