Can you flank from above?

If a character is on top of a creature, does this count for flanking?

Context: a group I play in was killing a dragon, and one of my fellow players (a fighter) grappled it and hung on to its neck. One of the other fighters stabbed it on her next turn, and the DM was unsure whether the dragon was flanked or not. Eventually, he said that sure, it was flanked, because technically there was a straight line drawn between the two characters. This group runs pretty heavily on RAF (as evidenced by the polymorph incidents), so this isn’t a “was this right” question. The decision worked for us, but I was curious about what RAW says on it.

Thus: Can you be counted as an enemy for flanking when you are on top of the target? When you are grappling the target? What rules prove this one way or another?