aboutsummaryrefslogtreecommitdiffstats
path: root/tools/vnet/vnet-module/etherip.c
blob: 2548d1a80aeede420292fa919c310cf88541ad85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/*
 * Copyright (C) 2004, 2005 Mike Wray <mike.wray@hp.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by the 
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free software Foundation, Inc.,
 * 59 Temple Place, suite 330, Boston, MA 02111-1307 USA
 *
 */
#ifdef __KERNEL__

#include <linux/config.h>
#include <linux/module.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/init.h>

#include <linux/version.h>

#include <linux/skbuff.h>
#include <linux/net.h>
#include <linux/netdevice.h>
#include <linux/in.h>
#include <linux/inet.h>
#include <linux/netfilter_bridge.h>
#include <linux/netfilter_ipv4.h>
#include <linux/icmp.h>
#include <linux/udp.h>

#include <net/ip.h>
#include <net/protocol.h>
#include <net/route.h>
#include <net/checksum.h>

#else

#include <netinet/in.h>
#include <arpa/inet.h>

#include "sys_kernel.h"
#include "spinlock.h"
#include "skbuff.h"
#include <linux/ip.h>
#include <linux/udp.h>

#define IP_DF		0x4000		/* Flag: "Don't Fragment"	*/

#endif

#include <etherip.h>
#include <tunnel.h>
#include <vnet.h>
#include <varp.h>
#include <if_varp.h>
#include <varp.h>
#include <skb_util.h>
#include <skb_context.h>

#define MODULE_NAME "VNET"
#define DEBUG 1
#undef DEBUG
#include "debug.h"

/** @file Etherip implementation.
 * The etherip protocol is used to transport Ethernet frames in IP packets.
 */

/** Flag controlling whether to use etherip-in-udp encapsulation.
 * If false we send etherip protocol in IP packets.
 * If true we send etherip protocol in UDP packets with a vnet header.
 */
int etherip_in_udp = 1;

/** Get the vnet label from an etherip header.
 *
 * @param hdr header
 * @@param vnet (in net order)
 */
void etheriphdr_get_vnet(struct etheriphdr *hdr, VnetId *vnet){
#ifdef CONFIG_ETHERIP_EXT
    *vnet = *(VnetId*)hdr->vnet;
#else
    *vnet = (VnetId){};
    vnet->u.vnet16[VNET_SIZE16 - 1] = (unsigned short)hdr->reserved;
    
#endif
}

/** Set the vnet label in an etherip header.
 * Also sets the etherip version.
 *
 * @param hdr header
 * @param vnet vnet label (in net order)
 */
void etheriphdr_set_vnet(struct etheriphdr *hdr, VnetId *vnet){
#ifdef CONFIG_ETHERIP_EXT
    hdr->version = ETHERIP_VERSION;
    *(VnetId*)hdr->vnet = *vnet;
#else
    hdr->version = ETHERIP_VERSION;
    hdr->reserved = (vnet->u.vnet16[VNET_SIZE16 - 1] & 0x0fff);
#endif
}

/** Open an etherip tunnel.
 *
 * @param tunnel to open
 * @return 0 on success, error code otherwise
 */
static int etherip_tunnel_open(Tunnel *tunnel){
    return 0;
}

/** Close an etherip tunnel.
 *
 * @param tunnel to close
 */
static void etherip_tunnel_close(Tunnel *tunnel){
}


/** Send a packet via an etherip tunnel.
 * Adds etherip header, new ip header, new ethernet header around
 * ethernet frame.
 *
 * @param tunnel tunnel
 * @param skb packet
 * @return 0 on success, error code otherwise
 */
static int etherip_tunnel_send(Tunnel *tunnel, struct sk_buff *skb){
    int err = 0;
    const int ip_n = sizeof(struct iphdr);
    const int etherip_n = sizeof(struct etheriphdr);
    const int udp_n = sizeof(struct udphdr);
    const int vnet_n = sizeof(struct VnetMsgHdr);
    int head_n = etherip_n + ip_n /* +  ETH_HLEN */;
    VnetId *vnet = &tunnel->key.vnet;
    struct etheriphdr *etheriph;
    u32 saddr = 0;

    if(etherip_in_udp){
        head_n += vnet_n + udp_n;
    }
    err = skb_make_room(&skb, skb, head_n, 0);
    if(err) goto exit;

    // Null the pointer as we are pushing a new IP header.
    skb->mac.raw = NULL;

    // Setup the etherip header.
    etheriph = (void*)skb_push(skb, etherip_n);
    etheriphdr_set_vnet(etheriph, vnet);

    if(etherip_in_udp){
        // Vnet header.
        struct VnetMsgHdr *vhdr = (void*)skb_push(skb, vnet_n);
        vhdr->id     = htons(VUDP_ID);
        vhdr->opcode = 0;

        // Setup the UDP header.
        skb->h.raw = skb_push(skb, udp_n);
        skb->h.uh->source = varp_port;		// Source port.
        skb->h.uh->dest   = varp_port;		// Destination port.
        skb->h.uh->len    = htons(skb->len);	// Total packet length (bytes).
        skb->h.uh->check  = 0;
    }

    // Setup the IP header.
    skb->nh.raw = skb_push(skb, ip_n); 
    skb->nh.iph->version  = 4;			// Standard version.
    skb->nh.iph->ihl      = ip_n / 4;		// IP header length (32-bit words).
    skb->nh.iph->tos      = 0;			// No special type-of-service.
    skb->nh.iph->tot_len  = htons(skb->len);    // Total packet length (bytes).
    skb->nh.iph->id       = 0;			// No flow id (since no frags).
    if(etherip_in_udp){
        skb->nh.iph->protocol = IPPROTO_UDP;    // IP protocol number.
        skb->nh.iph->frag_off = 0;
    } else {
        skb->nh.iph->protocol = IPPROTO_ETHERIP;// IP protocol number.
        skb->nh.iph->frag_off = htons(IP_DF);	// Don't fragment - can't handle frags.
    }
    skb->nh.iph->ttl      = 64;			// Linux default time-to-live.
    skb->nh.iph->saddr    = saddr;		// Source address.
    skb->nh.iph->daddr    = tunnel->key.addr.u.ip4.s_addr; // Destination address.
    skb->nh.iph->check    = 0;			// Zero the checksum.

    // Ethernet header will be filled-in by device.
    err = Tunnel_send(tunnel->base, skb);
    skb = NULL;
  exit:
    if(err && skb){
        wprintf("< err=%d\n", err);
        kfree_skb(skb);
    }
    return err;
}

/** Tunnel type for etherip.
 */
static TunnelType _etherip_tunnel_type = {
    .name	= "ETHERIP",
    .open	= etherip_tunnel_open,
    .close	= etherip_tunnel_close,
    .send 	= etherip_tunnel_send
};

TunnelType *etherip_tunnel_type = &_etherip_tunnel_type;

int etherip_tunnel_create(VnetId *vnet, VarpAddr *addr, Tunnel *base, Tunnel **tunnel){
    return Tunnel_create(etherip_tunnel_type, vnet, addr, base, tunnel);
}

/** Do etherip receive processing.
 * Strips the etherip header to extract the ethernet frame, sets
 * the vnet from the header and re-receives the frame.
 *
 * Return code 1 means we now own the packet - the caller must not free it.
 * Return code < 0 means an error - caller still owns the packet.
 *
 * @param skb packet
 * @return 1 on success, error code otherwise
 */
int etherip_protocol_recv(struct sk_buff *skb){
    int err = 0;
    const int etherip_n = sizeof(struct etheriphdr);
    struct etheriphdr *etheriph;
    Vnet *vinfo = NULL;
    VnetId vnet = {};
    u32 saddr, daddr;
    char vnetbuf[VNET_ID_BUF];
    struct ethhdr *eth;

    dprintf(">\n");
    saddr = skb->nh.iph->saddr;
    daddr = skb->nh.iph->daddr;
    if(MULTICAST(daddr) && (daddr != varp_mcast_addr)){
        // Ignore multicast packets not addressed to us.
        wprintf("> Ignoring mcast skb: src=%u.%u.%u.%u dst=%u.%u.%u.%u"
                " varp_mcast_addr=%u.%u.%u.%u\n",
                NIPQUAD(saddr), NIPQUAD(daddr), NIPQUAD(varp_mcast_addr));
        goto exit;
    }
    if(skb->data == skb->mac.raw){
        // skb->data points at ethernet header.
        //FIXME: Does this ever happen?
        //dprintf("> len=%d\n", skb->len);
        int ip_n = (skb->nh.iph->ihl << 2);
        int pull_n = ETH_HLEN + ip_n;
        if (!pskb_may_pull(skb, pull_n)){
            wprintf("> Malformed skb (eth+ip) src=%u.%u.%u.%u\n",
                    NIPQUAD(saddr));
            err = -EINVAL;
            goto exit;
        }
        skb_pull(skb, pull_n);
    }
    // Assume skb->data points at etherip header.
    etheriph = (void*)skb->data;
    if(etheriph->version != ETHERIP_VERSION){
        wprintf("> Bad etherip version=%d src=%u.%u.%u.%u\n",
                etheriph->version, NIPQUAD(saddr));
        err = -EINVAL;
        goto exit;
    }
    if(!pskb_may_pull(skb, etherip_n)){
        wprintf("> Malformed skb (etherip) src=%u.%u.%u.%u\n",
                NIPQUAD(saddr));
        err = -EINVAL;
        goto exit;
    }
    etheriphdr_get_vnet(etheriph, &vnet);
    // If vnet is secure, context must include IPSEC ESP.
    err = vnet_check_context(&vnet, SKB_CONTEXT(skb), &vinfo);
    if(err){
        wprintf("> Failed security check vnet=%s src=%u.%u.%u.%u\n",
                VnetId_ntoa(&vnet, vnetbuf), NIPQUAD(saddr));
        goto exit;
    }
    // Point at the headers in the contained ethernet frame.
    skb->mac.raw = skb_pull(skb, etherip_n);
    eth = eth_hdr(skb);

    // Simulate the logic from eth_type_trans()
    // to set skb->pkt_type and skb->protocol.
    if(mac_is_multicast(eth->h_dest)){
        if(mac_is_broadcast(eth->h_dest)){
            skb->pkt_type = PACKET_BROADCAST;
        } else {
            skb->pkt_type = PACKET_MULTICAST;
        }
    } else {
        skb->pkt_type = PACKET_HOST;
    }
    if(ntohs(eth->h_proto) >= 1536){
        skb->protocol = eth->h_proto;
    } else {
        skb->protocol = htons(ETH_P_802_2);
    }
    
    // Assuming a standard Ethernet frame.
    // Should check for protocol? Support ETH_P_8021Q too.
    skb->nh.raw = skb_pull(skb, ETH_HLEN);

#ifdef __KERNEL__
    // Fix IP options, checksum, skb dst, netfilter state.
    memset(&(IPCB(skb)->opt), 0, sizeof(struct ip_options));
    if (skb->ip_summed == CHECKSUM_HW){
        skb->ip_summed = CHECKSUM_NONE;
    }
    dst_release(skb->dst);
    skb->dst = NULL;
    nf_reset(skb);
#ifdef CONFIG_BRIDGE_NETFILTER
    // Stop the eth header being clobbered by nf_bridge_maybe_copy_header().
    // Were using this modified to use h_proto instead of skb->protocol.
    if(skb->nf_bridge){
        nf_bridge_save_header(skb);
    }
#endif
#endif // __KERNEL__

    dprintf("> Unpacked srcaddr=" IPFMT " vnet=%s srcmac=" MACFMT " dstmac=" MACFMT "\n",
            NIPQUAD(skb->nh.iph->saddr),
            VnetId_ntoa(&vnet, vnetbuf),
            MAC6TUPLE(eth->h_source),
            MAC6TUPLE(eth->h_dest));
    //print_skb(__FUNCTION__, 0, skb);

    {
        // Know source ip, vnet, vmac, so update the varp cache.
        // For this to work forwarded vnet packets must have the
        // original source address.
        VarpAddr addr = { .family = AF_INET };
        addr.u.ip4.s_addr = saddr;
        varp_update(&vnet, eth->h_source, &addr);
    }

    err = vnet_skb_recv(skb, vinfo);
  exit:
    if(vinfo) Vnet_decref(vinfo);
    dprintf("< skb=%p err=%d\n", skb, err);
    return err;
}


#ifdef __KERNEL__

/** Handle an ICMP error related to etherip.
 *
 * @param skb ICMP error packet
 * @param info
 */
static void etherip_protocol_icmp_err(struct sk_buff *skb, u32 info){
    struct iphdr *iph = (struct iphdr*)skb->data;
    
    wprintf("> ICMP error type=%d code=%d addr=" IPFMT "\n",
            skb->h.icmph->type, skb->h.icmph->code, NIPQUAD(iph->daddr));

    if (skb->h.icmph->type != ICMP_DEST_UNREACH ||
        skb->h.icmph->code != ICMP_FRAG_NEEDED){
        return;
    }
    wprintf("> MTU too big addr= " IPFMT "\n", NIPQUAD(iph->daddr)); 
}

//============================================================================
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
// Code for 2.6 kernel.

/** Etherip protocol. */
static struct net_protocol etherip_protocol = {
    .handler	 = etherip_protocol_recv,
    .err_handler = etherip_protocol_icmp_err,
};

static int etherip_protocol_add(void){
    return inet_add_protocol(&etherip_protocol, IPPROTO_ETHERIP);
}

static int etherip_protocol_del(void){
    return inet_del_protocol(&etherip_protocol, IPPROTO_ETHERIP);
}

//============================================================================
#else
//============================================================================
// Code for 2.4 kernel.

/** Etherip protocol. */
static struct inet_protocol etherip_protocol = {
    .name        = "ETHERIP",
    .protocol    = IPPROTO_ETHERIP,
    .handler	 = etherip_protocol_recv,
    .err_handler = etherip_protocol_icmp_err,
};

static int etherip_protocol_add(void){
    inet_add_protocol(&etherip_protocol);
    return 0;
}

static int etherip_protocol_del(void){
    return inet_del_protocol(&etherip_protocol);
}

#endif
//============================================================================


/** Initialize the etherip module.
 * Registers the etherip protocol.
 *
 * @return 0 on success, error code otherwise
 */
int __init etherip_module_init(void) {
    int err = 0;
    etherip_protocol_add();
    return err;
}

/** Finalize the etherip module.
 * Deregisters the etherip protocol.
 */
void __exit etherip_module_exit(void) {
    if(etherip_protocol_del() < 0){
        printk(KERN_INFO "%s: can't remove etherip protocol\n", __FUNCTION__);
    }
}

#endif // __KERNEL__