1 | /* $Id: VBoxNetARP.cpp 28714 2010-04-25 20:04:02Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxNetARP - IntNet ARP Client Routines.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009 Sun Microsystems, Inc.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
18 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
19 | * additional information or have any questions.
|
---|
20 | */
|
---|
21 |
|
---|
22 | /*******************************************************************************
|
---|
23 | * Header Files *
|
---|
24 | *******************************************************************************/
|
---|
25 | #define LOG_GROUP LOG_GROUP_DEFAULT
|
---|
26 | #include "VBoxNetLib.h"
|
---|
27 | #include <iprt/string.h>
|
---|
28 | #include <VBox/intnetinline.h>
|
---|
29 | #include <VBox/log.h>
|
---|
30 |
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Deal with ARP queries.
|
---|
34 | *
|
---|
35 | * @returns true if ARP.
|
---|
36 | *
|
---|
37 | * @param pSession The support driver session.
|
---|
38 | * @param hIf The internal network interface handle.
|
---|
39 | * @param pBuf The internal network interface buffer.
|
---|
40 | * @param pMacAddr Our MAC address.
|
---|
41 | * @param IPv4Addr Our IPv4 address.
|
---|
42 | */
|
---|
43 | bool VBoxNetArpHandleIt(PSUPDRVSESSION pSession, INTNETIFHANDLE hIf, PINTNETBUF pBuf, PCRTMAC pMacAddr, RTNETADDRIPV4 IPv4Addr)
|
---|
44 | {
|
---|
45 | /*
|
---|
46 | * Valid IntNet Ethernet frame? Skip GSO, no ARP in there.
|
---|
47 | */
|
---|
48 | PCINTNETHDR pHdr = IntNetRingGetNextFrameToRead(&pBuf->Recv);
|
---|
49 | if ( !pHdr
|
---|
50 | || pHdr->u16Type != INTNETHDR_TYPE_FRAME)
|
---|
51 | return false;
|
---|
52 |
|
---|
53 | size_t cbFrame = pHdr->cbFrame;
|
---|
54 | const void *pvFrame = IntNetHdrGetFramePtr(pHdr, pBuf);
|
---|
55 | PCRTNETETHERHDR pEthHdr = (PCRTNETETHERHDR)pvFrame;
|
---|
56 |
|
---|
57 | /*
|
---|
58 | * Arp frame?
|
---|
59 | */
|
---|
60 | if (pEthHdr->EtherType != RT_H2N_U16_C(RTNET_ETHERTYPE_ARP))
|
---|
61 | return false;
|
---|
62 | if ( ( pEthHdr->DstMac.au16[0] != 0xffff
|
---|
63 | || pEthHdr->DstMac.au16[1] != 0xffff
|
---|
64 | || pEthHdr->DstMac.au16[2] != 0xffff)
|
---|
65 | && ( pEthHdr->DstMac.au16[0] != pMacAddr->au16[0]
|
---|
66 | || pEthHdr->DstMac.au16[1] != pMacAddr->au16[1]
|
---|
67 | || pEthHdr->DstMac.au16[2] != pMacAddr->au16[2])
|
---|
68 | )
|
---|
69 | return false;
|
---|
70 | if (cbFrame < sizeof(RTNETARPIPV4) + sizeof(RTNETETHERHDR))
|
---|
71 | return false;
|
---|
72 |
|
---|
73 | PCRTNETARPHDR pArpHdr = (PCRTNETARPHDR)(pEthHdr + 1);
|
---|
74 | if (pArpHdr->ar_htype != RT_H2N_U16_C(RTNET_ARP_ETHER))
|
---|
75 | return false;
|
---|
76 | if (pArpHdr->ar_hlen != sizeof(RTMAC))
|
---|
77 | return false;
|
---|
78 | if (pArpHdr->ar_ptype != RT_H2N_U16_C(RTNET_ETHERTYPE_IPV4))
|
---|
79 | return false;
|
---|
80 | if (pArpHdr->ar_plen != sizeof(RTNETADDRIPV4))
|
---|
81 | return false;
|
---|
82 |
|
---|
83 | /* It's ARP, alright. Anything we need to do something about. */
|
---|
84 | PCRTNETARPIPV4 pArp = (PCRTNETARPIPV4)pArpHdr;
|
---|
85 | switch (pArp->Hdr.ar_oper)
|
---|
86 | {
|
---|
87 | case RT_H2N_U16_C(RTNET_ARPOP_REQUEST):
|
---|
88 | case RT_H2N_U16_C(RTNET_ARPOP_REVREQUEST):
|
---|
89 | case RT_H2N_U16_C(RTNET_ARPOP_INVREQUEST):
|
---|
90 | break;
|
---|
91 | default:
|
---|
92 | return true;
|
---|
93 | }
|
---|
94 |
|
---|
95 | /*
|
---|
96 | * Deal with the queries.
|
---|
97 | */
|
---|
98 | RTNETARPIPV4 Reply;
|
---|
99 | switch (pArp->Hdr.ar_oper)
|
---|
100 | {
|
---|
101 | /* 'Who has ar_tpa? Tell ar_spa.' */
|
---|
102 | case RT_H2N_U16_C(RTNET_ARPOP_REQUEST):
|
---|
103 | if (pArp->ar_tpa.u != IPv4Addr.u)
|
---|
104 | return true;
|
---|
105 | Reply.Hdr.ar_oper = RT_H2N_U16_C(RTNET_ARPOP_REPLY);
|
---|
106 | break;
|
---|
107 |
|
---|
108 | case RT_H2N_U16_C(RTNET_ARPOP_REVREQUEST):
|
---|
109 | if ( pArp->ar_tha.au16[0] != pMacAddr->au16[0]
|
---|
110 | || pArp->ar_tha.au16[1] != pMacAddr->au16[1]
|
---|
111 | || pArp->ar_tha.au16[2] != pMacAddr->au16[2])
|
---|
112 | return true;
|
---|
113 | Reply.Hdr.ar_oper = RT_H2N_U16_C(RTNET_ARPOP_REVREPLY);
|
---|
114 | break;
|
---|
115 |
|
---|
116 | case RT_H2N_U16_C(RTNET_ARPOP_INVREQUEST):
|
---|
117 | /** @todo RTNET_ARPOP_INVREQUEST */
|
---|
118 | return true;
|
---|
119 | //Reply.Hdr.ar_oper = RT_H2N_U16_C(RTNET_ARPOP_INVREPLY);
|
---|
120 | //break;
|
---|
121 | }
|
---|
122 |
|
---|
123 | /*
|
---|
124 | * Complete the reply and send it.
|
---|
125 | */
|
---|
126 | Reply.Hdr.ar_htype = RT_H2N_U16_C(RTNET_ARP_ETHER);
|
---|
127 | Reply.Hdr.ar_ptype = RT_H2N_U16_C(RTNET_ETHERTYPE_IPV4);
|
---|
128 | Reply.Hdr.ar_hlen = sizeof(RTMAC);
|
---|
129 | Reply.Hdr.ar_plen = sizeof(RTNETADDRIPV4);
|
---|
130 | Reply.ar_sha = *pMacAddr;
|
---|
131 | Reply.ar_spa = IPv4Addr;
|
---|
132 | Reply.ar_tha = pArp->ar_sha;
|
---|
133 | Reply.ar_tpa = pArp->ar_spa;
|
---|
134 |
|
---|
135 |
|
---|
136 | RTNETETHERHDR EthHdr;
|
---|
137 | EthHdr.DstMac = pArp->ar_sha;
|
---|
138 | EthHdr.SrcMac = *pMacAddr;
|
---|
139 | EthHdr.EtherType = RT_H2N_U16_C(RTNET_ETHERTYPE_ARP);
|
---|
140 |
|
---|
141 | uint8_t abTrailer[60 - sizeof(Reply) - sizeof(EthHdr)];
|
---|
142 | memset(abTrailer, '\0', sizeof(abTrailer));
|
---|
143 |
|
---|
144 | INTNETSEG aSegs[3];
|
---|
145 | aSegs[0].cb = sizeof(EthHdr);
|
---|
146 | aSegs[0].pv = &EthHdr;
|
---|
147 |
|
---|
148 | aSegs[1].pv = &Reply;
|
---|
149 | aSegs[1].cb = sizeof(Reply);
|
---|
150 |
|
---|
151 | aSegs[2].pv = &abTrailer[0];
|
---|
152 | aSegs[2].cb = sizeof(abTrailer);
|
---|
153 |
|
---|
154 | VBoxNetIntIfSend(pSession, hIf, pBuf, RT_ELEMENTS(aSegs), &aSegs[0], true /* fFlush */);
|
---|
155 |
|
---|
156 | return true;
|
---|
157 | }
|
---|
158 |
|
---|