ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/ether_emu.C
Revision: 1.2
Committed: Thu Oct 16 02:41:21 2003 UTC (20 years, 7 months ago) by pcg
Content type: text/plain
Branch: MAIN
Changes since 1.1: +1 -0 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 pcg 1.1 /*
2     ether_emu.C -- ethernet "emulator" library
3 pcg 1.2 Copyright (C) 2003 Marc Lehmann <pcg@goof.com>
4 pcg 1.1
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9    
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13     GNU General Public License for more details.
14    
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18     */
19    
20     #include "config.h"
21    
22     #include <map>
23    
24     #include "vpn.h"
25    
26     struct ether_emu : map<u32, int> {
27     typedef map<u32, int> ipv4map;
28     ipv4map ipv4;
29    
30     bool tun_to_tap (tap_packet *pkt);
31     bool tap_to_tun (tap_packet *pkt);
32    
33     void set_ipv4 (u32 ip, int dst)
34     {
35     (ipv4.insert (pair<u32, int>(ip, dst)).first)->second = dst;
36     }
37     };
38    
39     static struct ether_emu ether_emu;
40    
41     bool
42     ether_emu::tun_to_tap (tap_packet *pkt)
43     {
44     int dst;
45    
46     if (pkt->is_ipv4 ())
47     {
48     // update arp cache for _local_ hosts
49     set_ipv4 (pkt->ipv4_src (), THISNODE->id);
50    
51     ipv4map::iterator i = ipv4.find (pkt->ipv4_dst ());
52    
53     if (i == ipv4.end ())
54     {
55     u32 ip_src = pkt->ipv4_src ();
56     u32 ip_dst = pkt->ipv4_dst ();
57    
58     // throw away current packet and make it an arp request
59     (*pkt)[12] = 0x08; (*pkt)[13] = 0x06;
60     (*pkt)[14] = 0x00; (*pkt)[15] = 0x01; // hw
61     (*pkt)[16] = 0x08; (*pkt)[17] = 0x00; // prot
62     (*pkt)[18] = 0x06; // hw_len
63     (*pkt)[19] = 0x04; // prot_len
64     (*pkt)[20] = 0x00; (*pkt)[21] = 0x01; // op
65    
66     id2mac (THISNODE->id, &(*pkt)[22]);
67     *(u32 *)&(*pkt)[28] = ip_src;
68     id2mac (0, &(*pkt)[32]);
69     *(u32 *)&(*pkt)[38] = ip_dst;
70    
71     pkt->len = 42;
72    
73     dst = 0;
74     }
75     else
76     dst = i->second;
77     }
78     else
79     dst = 0; // broadcast non-ip
80    
81     id2mac (THISNODE->id, pkt->src);
82     id2mac (dst, pkt->dst);
83    
84     return true;
85     }
86    
87     bool
88     ether_emu::tap_to_tun (tap_packet *pkt)
89     {
90     if (pkt->is_arp ())
91     {
92     u32 ip_src = *(u32 *)&(*pkt)[28];
93    
94     // always update with all info we can get. in this case, the arp sender.
95     set_ipv4 (ip_src, mac2id (&(*pkt)[22]));
96    
97     //TODO: remove cache dumper
98     //for (ipv4map::iterator i = ipv4.begin (); i != ipv4.end (); ++i) printf ("%08lx => %d\n", i->first, i->second);
99    
100     if ((*pkt)[20] == 0x00 && (*pkt)[21] == 0x01) // arp request
101     {
102     // send a reply, if applicable
103     u32 ip_dst = *(u32 *)&(*pkt)[38];
104     ipv4map::iterator i = ipv4.find (ip_dst);
105    
106     // TODO: look up list of local networks and answer for them
107     if (i != ipv4.end () && i->second == THISNODE->id)
108     {
109     // create an arp reply
110     tap_packet *rep = new tap_packet;
111    
112     id2mac (THISNODE->id, rep->src);
113     memcpy (rep->dst, pkt->src, sizeof (mac));
114    
115     (*rep)[12] = 0x08; (*rep)[13] = 0x06;
116     (*rep)[14] = 0x00; (*rep)[15] = 0x01; // hw
117     (*rep)[16] = 0x08; (*rep)[17] = 0x00; // prot
118     (*rep)[18] = 0x06; // hw_len
119     (*rep)[19] = 0x04; // prot_len
120     (*rep)[20] = 0x00; (*rep)[21] = 0x02; // op
121    
122     id2mac (THISNODE->id, &(*rep)[22]);
123     *(u32 *)&(*rep)[28] = ip_dst;
124     memcpy (&(*rep)[32], &(*pkt)[22], sizeof (mac));
125     *(u32 *)&(*rep)[38] = ip_src;
126    
127     rep->len = 42;
128    
129     network.inject_data_packet (rep, mac2id (rep->dst));
130    
131     delete rep;
132     }
133     }
134     else if ((*pkt)[20] == 0x00 && (*pkt)[21] == 0x02) // arp reply
135     set_ipv4 (*(u32 *)&(*pkt)[28], mac2id (&(*pkt)[22]));
136    
137     return false;
138     }
139     else if (pkt->is_ipv4 ())
140     {
141     // update arp cache
142     set_ipv4 (pkt->ipv4_src (), mac2id (pkt->src));
143     set_ipv4 (pkt->ipv4_dst (), mac2id (pkt->dst));
144     }
145    
146     return true;
147     }
148