debuggers.hg

view tools/vnet/vnet-module/etherip.c @ 0:7d21f7218375

Exact replica of unstable on 051908 + README-this
author Mukesh Rathor
date Mon May 19 15:34:57 2008 -0700 (2008-05-19)
parents
children
line source
1 /*
2 * Copyright (C) 2004, 2005 Mike Wray <mike.wray@hp.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free software Foundation, Inc.,
16 * 59 Temple Place, suite 330, Boston, MA 02111-1307 USA
17 *
18 */
19 #ifdef __KERNEL__
21 #include <linux/config.h>
22 #include <linux/module.h>
23 #include <linux/types.h>
24 #include <linux/kernel.h>
25 #include <linux/init.h>
27 #include <linux/version.h>
29 #include <linux/skbuff.h>
30 #include <linux/net.h>
31 #include <linux/netdevice.h>
32 #include <linux/in.h>
33 #include <linux/inet.h>
34 #include <linux/netfilter_bridge.h>
35 #include <linux/netfilter_ipv4.h>
36 #include <linux/icmp.h>
37 #include <linux/udp.h>
39 #include <net/ip.h>
40 #include <net/protocol.h>
41 #include <net/route.h>
42 #include <net/checksum.h>
44 #else
46 #include <netinet/in.h>
47 #include <arpa/inet.h>
49 #include "sys_kernel.h"
50 #include "spinlock.h"
51 #include "skbuff.h"
52 #include <linux/ip.h>
53 #include <linux/udp.h>
55 #define IP_DF 0x4000 /* Flag: "Don't Fragment" */
57 #endif
59 #include <etherip.h>
60 #include <tunnel.h>
61 #include <vnet.h>
62 #include <varp.h>
63 #include <if_varp.h>
64 #include <varp.h>
65 #include <skb_util.h>
66 #include <skb_context.h>
68 #define MODULE_NAME "VNET"
69 #define DEBUG 1
70 #undef DEBUG
71 #include "debug.h"
73 /** @file Etherip implementation.
74 * The etherip protocol is used to transport Ethernet frames in IP packets.
75 */
77 /** Flag controlling whether to use etherip-in-udp encapsulation.
78 * If false we send etherip protocol in IP packets.
79 * If true we send etherip protocol in UDP packets with a vnet header.
80 */
81 int etherip_in_udp = 1;
83 /** Get the vnet label from an etherip header.
84 *
85 * @param hdr header
86 * @@param vnet (in net order)
87 */
88 void etheriphdr_get_vnet(struct etheriphdr *hdr, VnetId *vnet){
89 #ifdef CONFIG_ETHERIP_EXT
90 *vnet = *(VnetId*)hdr->vnet;
91 #else
92 *vnet = (VnetId){};
93 vnet->u.vnet16[VNET_SIZE16 - 1] = (unsigned short)hdr->reserved;
95 #endif
96 }
98 /** Set the vnet label in an etherip header.
99 * Also sets the etherip version.
100 *
101 * @param hdr header
102 * @param vnet vnet label (in net order)
103 */
104 void etheriphdr_set_vnet(struct etheriphdr *hdr, VnetId *vnet){
105 #ifdef CONFIG_ETHERIP_EXT
106 hdr->version = ETHERIP_VERSION;
107 *(VnetId*)hdr->vnet = *vnet;
108 #else
109 hdr->version = ETHERIP_VERSION;
110 hdr->reserved = (vnet->u.vnet16[VNET_SIZE16 - 1] & 0x0fff);
111 #endif
112 }
114 /** Open an etherip tunnel.
115 *
116 * @param tunnel to open
117 * @return 0 on success, error code otherwise
118 */
119 static int etherip_tunnel_open(Tunnel *tunnel){
120 return 0;
121 }
123 /** Close an etherip tunnel.
124 *
125 * @param tunnel to close
126 */
127 static void etherip_tunnel_close(Tunnel *tunnel){
128 }
131 static inline int skb_make_headroom(struct sk_buff **pskb, struct sk_buff *skb, int head_n){
132 int err = 0;
133 dprintf("> skb=%p headroom=%d head_n=%d\n", skb, skb_headroom(skb), head_n);
134 if(head_n > skb_headroom(skb) || skb_cloned(skb) || skb_shared(skb)){
135 // Expand header the way GRE does.
136 struct sk_buff *new_skb = skb_realloc_headroom(skb, head_n + 16);
137 if(!new_skb){
138 err = -ENOMEM;
139 goto exit;
140 }
141 kfree_skb(skb);
142 *pskb = new_skb;
143 } else {
144 *pskb = skb;
145 }
146 exit:
147 return err;
148 }
150 /** Send a packet via an etherip tunnel.
151 * Adds etherip header and new ip header around ethernet frame.
152 *
153 * @param tunnel tunnel
154 * @param skb packet
155 * @return 0 on success, error code otherwise
156 */
157 static int etherip_tunnel_send(Tunnel *tunnel, struct sk_buff *skb){
158 int err = 0;
159 const int ip_n = sizeof(struct iphdr);
160 const int etherip_n = sizeof(struct etheriphdr);
161 const int udp_n = sizeof(struct udphdr);
162 const int vnet_n = sizeof(struct VnetMsgHdr);
163 int head_n = etherip_n + ip_n /* + ETH_HLEN */;
164 VnetId *vnet = &tunnel->key.vnet;
165 struct etheriphdr *etheriph;
166 u32 saddr = 0;
168 if(etherip_in_udp){
169 head_n += vnet_n + udp_n;
170 }
171 err = skb_make_headroom(&skb, skb, head_n);
172 if(err) goto exit;
174 // Null the pointer as we are pushing a new IP header.
175 skb->mac.raw = NULL;
177 // Setup the etherip header.
178 etheriph = (void*)skb_push(skb, etherip_n);
179 etheriphdr_set_vnet(etheriph, vnet);
181 if(etherip_in_udp){
182 // Vnet header.
183 struct VnetMsgHdr *vhdr = (void*)skb_push(skb, vnet_n);
184 vhdr->id = htons(VUDP_ID);
185 vhdr->opcode = 0;
187 // Setup the UDP header.
188 skb->h.raw = skb_push(skb, udp_n);
189 skb->h.uh->source = varp_port; // Source port.
190 skb->h.uh->dest = varp_port; // Destination port.
191 skb->h.uh->len = htons(skb->len); // Total packet length (bytes).
192 skb->h.uh->check = 0;
193 }
195 // Setup the IP header.
196 skb->nh.raw = skb_push(skb, ip_n);
197 skb->nh.iph->version = 4; // Standard version.
198 skb->nh.iph->ihl = ip_n / 4; // IP header length (32-bit words).
199 skb->nh.iph->tos = 0; // No special type-of-service.
200 skb->nh.iph->tot_len = htons(skb->len); // Total packet length (bytes).
201 skb->nh.iph->id = 0; // No flow id (since no frags).
202 if(etherip_in_udp){
203 skb->nh.iph->protocol = IPPROTO_UDP; // IP protocol number.
204 skb->nh.iph->frag_off = 0;
205 } else {
206 skb->nh.iph->protocol = IPPROTO_ETHERIP;// IP protocol number.
207 skb->nh.iph->frag_off = htons(IP_DF); // Don't fragment - can't handle frags.
208 }
209 skb->nh.iph->ttl = 64; // Linux default time-to-live.
210 skb->nh.iph->saddr = saddr; // Source address.
211 skb->nh.iph->daddr = tunnel->key.addr.u.ip4.s_addr; // Destination address.
212 skb->nh.iph->check = 0; // Zero the checksum.
214 // Ethernet header will be filled-in by device.
215 err = Tunnel_send(tunnel->base, skb);
216 skb = NULL;
217 exit:
218 if(err && skb){
219 wprintf("< err=%d\n", err);
220 kfree_skb(skb);
221 }
222 return err;
223 }
225 /** Tunnel type for etherip.
226 */
227 static TunnelType _etherip_tunnel_type = {
228 .name = "ETHERIP",
229 .open = etherip_tunnel_open,
230 .close = etherip_tunnel_close,
231 .send = etherip_tunnel_send
232 };
234 TunnelType *etherip_tunnel_type = &_etherip_tunnel_type;
236 int etherip_tunnel_create(VnetId *vnet, VarpAddr *addr, Tunnel *base, Tunnel **tunnel){
237 return Tunnel_create(etherip_tunnel_type, vnet, addr, base, tunnel);
238 }
240 #if defined(__KERNEL__) && defined(CONFIG_BRIDGE_NETFILTER)
241 /** We need our own copy of this as it is no longer exported from the bridge module.
242 */
243 static inline void _nf_bridge_save_header(struct sk_buff *skb){
244 int header_size = 16;
246 // Were using this modified to use h_proto instead of skb->protocol.
247 if(skb->protocol == htons(ETH_P_8021Q)){
248 header_size = 18;
249 }
250 memcpy(skb->nf_bridge->data, skb->data - header_size, header_size);
251 }
252 #endif
254 /** Do etherip receive processing.
255 * Strips the etherip header to extract the ethernet frame, sets
256 * the vnet from the header and re-receives the frame.
257 *
258 * Return code 1 means we now own the packet - the caller must not free it.
259 * Return code < 0 means an error - caller still owns the packet.
260 *
261 * @param skb packet
262 * @return 1 on success, error code otherwise
263 */
264 int etherip_protocol_recv(struct sk_buff *skb){
265 int err = 0;
266 const int etherip_n = sizeof(struct etheriphdr);
267 struct etheriphdr *etheriph;
268 Vnet *vinfo = NULL;
269 VnetId vnet = {};
270 u32 saddr, daddr;
271 char vnetbuf[VNET_ID_BUF];
272 struct ethhdr *eth;
273 struct sk_buff *newskb;
275 dprintf(">\n");
276 saddr = skb->nh.iph->saddr;
277 daddr = skb->nh.iph->daddr;
278 if(MULTICAST(daddr) && (daddr != varp_mcast_addr)){
279 // Ignore multicast packets not addressed to us.
280 wprintf("> Ignoring mcast skb: src=%u.%u.%u.%u dst=%u.%u.%u.%u"
281 " varp_mcast_addr=%u.%u.%u.%u\n",
282 NIPQUAD(saddr), NIPQUAD(daddr), NIPQUAD(varp_mcast_addr));
283 goto exit;
284 }
285 if(skb->data == skb->mac.raw){
286 // skb->data points at ethernet header.
287 //FIXME: Does this ever happen?
288 //dprintf("> len=%d\n", skb->len);
289 int ip_n = (skb->nh.iph->ihl << 2);
290 int pull_n = ETH_HLEN + ip_n;
291 if (!pskb_may_pull(skb, pull_n)){
292 wprintf("> Malformed skb (eth+ip) src=%u.%u.%u.%u\n",
293 NIPQUAD(saddr));
294 err = -EINVAL;
295 goto exit;
296 }
297 skb_pull_vn(skb, pull_n);
298 }
299 // Assume skb->data points at etherip header.
300 etheriph = (void*)skb->data;
301 if(etheriph->version != ETHERIP_VERSION){
302 wprintf("> Bad etherip version=%d src=%u.%u.%u.%u\n",
303 etheriph->version, NIPQUAD(saddr));
304 err = -EINVAL;
305 goto exit;
306 }
307 if(!pskb_may_pull(skb, etherip_n)){
308 wprintf("> Malformed skb (etherip) src=%u.%u.%u.%u\n",
309 NIPQUAD(saddr));
310 err = -EINVAL;
311 goto exit;
312 }
313 etheriphdr_get_vnet(etheriph, &vnet);
314 // If vnet is secure, context must include IPSEC ESP.
315 err = vnet_check_context(&vnet, SKB_CONTEXT(skb), &vinfo);
316 if(err){
317 wprintf("> Failed security check vnet=%s src=%u.%u.%u.%u\n",
318 VnetId_ntoa(&vnet, vnetbuf), NIPQUAD(saddr));
319 goto exit;
320 }
321 // Point at the headers in the contained ethernet frame.
322 skb->mac.raw = skb_pull_vn(skb, etherip_n);
324 newskb = alloc_skb(skb->len, GFP_ATOMIC);
325 if (!newskb) {
326 wprintf("> alloc new sk_buff failed \n");
327 goto exit;
328 }
329 newskb->mac.raw = skb_put(newskb, skb->len);
330 skb_copy_bits(skb, 0, newskb->data, skb->len);
331 kfree_skb(skb);
332 skb = newskb;
334 eth = eth_hdr(skb);
336 // Simulate the logic from eth_type_trans()
337 // to set skb->pkt_type and skb->protocol.
338 if(mac_is_multicast(eth->h_dest)){
339 if(mac_is_broadcast(eth->h_dest)){
340 skb->pkt_type = PACKET_BROADCAST;
341 } else {
342 skb->pkt_type = PACKET_MULTICAST;
343 }
344 } else {
345 skb->pkt_type = PACKET_HOST;
346 }
347 if(ntohs(eth->h_proto) >= 1536){
348 skb->protocol = eth->h_proto;
349 } else {
350 skb->protocol = htons(ETH_P_802_2);
351 }
353 // Assuming a standard Ethernet frame.
354 // Should check for protocol? Support ETH_P_8021Q too.
355 skb->nh.raw = skb_pull_vn(skb, ETH_HLEN);
356 skb->h.raw = newskb->nh.raw + sizeof(struct iphdr);
358 dprintf("> Unpacked srcaddr=" IPFMT " dstaddr=" IPFMT " vnet=%s srcmac=" MACFMT " dstmac=" MACFMT "\n",
359 NIPQUAD(skb->nh.iph->saddr),
360 NIPQUAD(skb->nh.iph->daddr),
361 VnetId_ntoa(&vnet, vnetbuf),
362 MAC6TUPLE(eth->h_source),
363 MAC6TUPLE(eth->h_dest));
364 //print_skb(__FUNCTION__, 0, skb);
366 {
367 // Know source ip, vnet, vmac, so update the varp cache.
368 // For this to work forwarded vnet packets must have the
369 // original source address.
370 VarpAddr addr = { .family = AF_INET };
371 addr.u.ip4.s_addr = saddr;
372 varp_update(&vnet, eth->h_source, &addr);
373 }
375 err = vnet_skb_recv(skb, vinfo);
376 exit:
377 if(vinfo) Vnet_decref(vinfo);
378 dprintf("< skb=%p err=%d\n", skb, err);
379 return err;
380 }
383 #ifdef __KERNEL__
385 /** Handle an ICMP error related to etherip.
386 *
387 * @param skb ICMP error packet
388 * @param info
389 */
390 static void etherip_protocol_icmp_err(struct sk_buff *skb, u32 info){
391 struct iphdr *iph = (struct iphdr*)skb->data;
393 wprintf("> ICMP error type=%d code=%d addr=" IPFMT "\n",
394 skb->h.icmph->type, skb->h.icmph->code, NIPQUAD(iph->daddr));
396 if (skb->h.icmph->type != ICMP_DEST_UNREACH ||
397 skb->h.icmph->code != ICMP_FRAG_NEEDED){
398 return;
399 }
400 wprintf("> MTU too big addr= " IPFMT "\n", NIPQUAD(iph->daddr));
401 }
403 //============================================================================
404 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
405 // Code for 2.6 kernel.
407 /** Etherip protocol. */
408 static struct net_protocol etherip_protocol = {
409 .handler = etherip_protocol_recv,
410 .err_handler = etherip_protocol_icmp_err,
411 };
413 static int etherip_protocol_add(void){
414 return inet_add_protocol(&etherip_protocol, IPPROTO_ETHERIP);
415 }
417 static int etherip_protocol_del(void){
418 return inet_del_protocol(&etherip_protocol, IPPROTO_ETHERIP);
419 }
421 //============================================================================
422 #else
423 //============================================================================
424 // Code for 2.4 kernel.
426 /** Etherip protocol. */
427 static struct inet_protocol etherip_protocol = {
428 .name = "ETHERIP",
429 .protocol = IPPROTO_ETHERIP,
430 .handler = etherip_protocol_recv,
431 .err_handler = etherip_protocol_icmp_err,
432 };
434 static int etherip_protocol_add(void){
435 inet_add_protocol(&etherip_protocol);
436 return 0;
437 }
439 static int etherip_protocol_del(void){
440 return inet_del_protocol(&etherip_protocol);
441 }
443 #endif
444 //============================================================================
447 /** Initialize the etherip module.
448 * Registers the etherip protocol.
449 *
450 * @return 0 on success, error code otherwise
451 */
452 int __init etherip_module_init(void) {
453 int err = 0;
454 etherip_protocol_add();
455 return err;
456 }
458 /** Finalize the etherip module.
459 * Deregisters the etherip protocol.
460 */
461 void __exit etherip_module_exit(void) {
462 if(etherip_protocol_del() < 0){
463 printk(KERN_INFO "%s: can't remove etherip protocol\n", __FUNCTION__);
464 }
465 }
467 #endif // __KERNEL__