polynomial of degree n and its divisor

Let $p(x)=x^2 +x+1$ divides $f(x)=(x+1)^n + x^n+1$. I need to find values of $n$.

Logically, I can see that $n=2,4$ are the values. But I need to find the rule.

Anyone can help?

HINT: Let $\omega$ be the complex cube root of unity. Then $f(\omega )=0$ and this implies $$f(\omega )=(-1)^n \omega^{2n}+\omega^n+1=0$$
First, suppose $n\equiv 1\pmod{3}$, then we have $(-1)^n\omega^2+\omega +1=0$ i.e. $(-1)^n\omega^2=\omega^2$ and hence $n$ is even. Similarly $\omega^2$ is also a root of $f$ when $n$ is even. Hence $p\mid f$ when $n$ is even and $n\equiv 1\pmod{3}$. Similarly check the cases $n\equiv 0,2\pmod{3}$.