On 19/08/15(Wed) 20:10, Alexander Bluhm wrote:
> On Wed, Aug 19, 2015 at 02:41:22PM +0200, Martin Pieuchot wrote:
> > > Diff below converts one of the few remaining iteration on the global
> > > list of interfaces (&ifnet).
>
> > /* Check for local address spoofing. */
> > - if (((ifp = if_get(m->m_pkthdr.ph_ifidx)) == NULL ||
> > - !(ifp->if_flags & IFF_LOOPBACK)) && ipip_allow != 2) {
>
> If the packet was coming from a loopback interface it was always
> passed. Your code always drops if it is coming from a local address.
> I think we need this check, see also the comment mentioning ph_ifidx
> above the function ipip_input().
Right, I'm still really interested in an explanation. The comment you
mentioned talks about enc(4) which is not a IFF_LOOPBACK interface. I
just hate these loopback hacks, we have so many of them always checking
for the IFF_LOOPBACK type and I'd rather see fewer if_get(9) usage in
the stack.
Anyway if somebody could comment on that; I'm interested, but I left
it.
> > > + struct sockaddr_storage ss;
> > > + struct rtentry *rt;
> > > +
> > > + if (ipo) {
> > > + sin = (struct sockaddr_in *)&ss;
> > > + sin->sin_family = AF_INET;
> > > + sin->sin_len = sizeof(*sin);
> > > + sin->sin_addr = ipo->ip_src;
> > > +#ifdef INET6
> > > + } else if (ip6) {
> > > + sin6 = (struct sockaddr_in6 *)&ss;
> > > + sin6->sin6_family = AF_INET6;
> > > + sin6->sin6_len = sizeof(*sin6);
> > > + sin6->sin6_addr = ip6->ip6_src;
> > > #endif /* INET6 */
> > > + }
>
> Normaly you should memset(0) sin and sin6 because of the padding.
> At least in user land and with sobind() this is necessary. Most
> of the kernel code does this. But there are a few places where it
> is not done before rtalloc(). I think we should initialize it
> everywhere. I can build a diff to make it consistent.
Thanks, here's an updated diff.
Index: netinet/ip_ipip.c
===================================================================
RCS file: /cvs/src/sys/netinet/ip_ipip.c,v
retrieving revision 1.64
diff -u -p -r1.64 ip_ipip.c
--- netinet/ip_ipip.c 14 Aug 2015 18:07:28 -0000 1.64
+++ netinet/ip_ipip.c 20 Aug 2015 11:42:26 -0000
@@ -146,10 +146,8 @@ ipip_input(struct mbuf *m, int iphlen, s
{
struct sockaddr_in *sin;
struct ifnet *ifp;
- struct ifaddr *ifa;
struct niqueue *ifq = NULL;
struct ip *ipo;
- u_int rdomain;
#ifdef INET6
struct sockaddr_in6 *sin6;
struct ip6_hdr *ip6;
@@ -297,43 +295,34 @@ ipip_input(struct mbuf *m, int iphlen, s
/* Check for local address spoofing. */
if (((ifp = if_get(m->m_pkthdr.ph_ifidx)) == NULL ||
!(ifp->if_flags & IFF_LOOPBACK)) && ipip_allow != 2) {
- rdomain = rtable_l2(m->m_pkthdr.ph_rtableid);
- TAILQ_FOREACH(ifp, &ifnet, if_list) {
- if (ifp->if_rdomain != rdomain)
- continue;
- TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
- if (ipo) {
- if (ifa->ifa_addr->sa_family !=
- AF_INET)
- continue;
-
- sin = satosin(ifa->ifa_addr);
- if (sin->sin_addr.s_addr ==
- ipo->ip_src.s_addr) {
- ipipstat.ipips_spoof++;
- m_freem(m);
- return;
- }
- }
-#ifdef INET6
- if (ip6) {
- if (ifa->ifa_addr->sa_family !=
- AF_INET6)
- continue;
-
- sin6 = satosin6(ifa->ifa_addr);
- if (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
- &ip6->ip6_src)) {
- ipipstat.ipips_spoof++;
- m_freem(m);
- return;
- }
+ struct sockaddr_storage ss;
+ struct rtentry *rt;
- }
+ memset(&ss, 0, sizeof(ss));
+
+ if (ipo) {
+ sin = (struct sockaddr_in *)&ss;
+ sin->sin_family = AF_INET;
+ sin->sin_len = sizeof(*sin);
+ sin->sin_addr = ipo->ip_src;
+#ifdef INET6
+ } else if (ip6) {
+ sin6 = (struct sockaddr_in6 *)&ss;
+ sin6->sin6_family = AF_INET6;
+ sin6->sin6_len = sizeof(*sin6);
+ sin6->sin6_addr = ip6->ip6_src;
#endif /* INET6 */
- }
}
- }
+ rt = rtalloc((struct sockaddr *)&ss, 0,
+ m->m_pkthdr.ph_rtableid);
+ if ((rt != NULL) && (rt->rt_flags & RTF_LOCAL)) {
+ ipipstat.ipips_spoof++;
+ m_freem(m);
+ rtfree(rt);
+ return;
+ }
+ rtfree(rt);
+ }
/* Statistics */
ipipstat.ipips_ibytes += m->m_pkthdr.len - iphlen;