ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/ether_emu.C
Revision: 1.8
Committed: Thu Aug 7 17:54:27 2008 UTC (15 years, 9 months ago) by pcg
Content type: text/plain
Branch: MAIN
CVS Tags: rel-2_2, rel-2_21, rel-2_22
Changes since 1.7: +24 -14 lines
Log Message:
update to gplv3, finally

File Contents

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