Fixed ipv6 default address; tunnel broker is by default turned off
[ZeXOS.git] / kernel / core / net / tun6.c
blob32922a0e7db2200c8a6b690590fc0e127271d7cc
1 /*
2 * ZeX/OS
3 * Copyright (C) 2008 Tomas 'ZeXx86' Jedrzejek (zexx86@gmail.com)
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 3 of the License, or
8 * (at your option) any later version.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <system.h>
21 #include <string.h>
22 #include <dev.h>
23 #include <net/eth.h>
24 #include <net/net.h>
25 #include <net/if.h>
26 #include <net/packet.h>
27 #include <net/ip.h>
28 #include <net/checksum.h>
31 net_ipv4 tun6_ip;
33 #define DEFAULT_TUN6_ADDRESS NET_IPV4_TO_ADDR (216,66,80,30)
36 unsigned tun6_addr (net_ipv4 tun6)
38 tun6_ip = tun6;
40 return 1;
43 unsigned tun6_addr_get ()
45 return tun6_ip;
48 unsigned net_proto_tun6_send (netif_t *netif, packet_t *packet, proto_ipv6_t *ip, char *buf, unsigned len)
50 char *buf_ip = (char *) kmalloc ((len + 1 + sizeof (proto_ipv6_t)));
52 if (!buf_ip)
53 return 0;
55 unsigned l = sizeof (proto_ipv6_t);
57 /* put ip header and buf data to one buffer */
58 memcpy (buf_ip, (char *) ip, l);
59 memcpy (buf_ip+l, buf, len);
61 buf_ip[l+len] = '\0';
64 packet->type = NET_PACKET_TYPE_IPV4;
66 /* ip layer */
67 proto_ip_t *ipv4 = (proto_ip_t *) kmalloc (sizeof (proto_ip_t));
69 if (!ipv4)
70 return 0;
72 /* there are some fixed values - yeah it is horrible */
73 ipv4->ver = 4;
74 ipv4->head_len = 5;
76 ipv4->flags = 0;
77 ipv4->frag = 0;
78 ipv4->ttl = 64;
79 ipv4->checksum = 0;
80 ipv4->proto = NET_PROTO_IP_TYPE_TUN6;
81 ipv4->ip_source = netif->ip;
82 ipv4->ip_dest = tun6_addr_get ();
84 /* calculate total length of packet (ipv6/ip) */
85 unsigned i = ipv4->head_len * 4;
86 ipv4->total_len = swap16 (i+l+len);
88 unsigned ret = net_proto_ip_send (netif, packet, ipv4, buf_ip, l+len);
90 kfree (ipv4);
91 kfree (buf_ip);
93 return ret;
96 unsigned net_proto_tun6_handler (packet_t *packet, proto_ip_t *ip, char *buf, unsigned len)
98 proto_ipv6_t *ipv6 = (proto_ipv6_t *) buf;
100 /* ipv6 provide directly length of data - payload length */
101 unsigned data_len = swap16 (ipv6->pl_len);
103 //printf ("tun6 -> 0x%x\n", ipv6->nhead);
105 switch (ipv6->nhead) {
106 case NET_PROTO_IP_TYPE_TCP6:
107 return net_proto_tcp6_handler (packet, ipv6, buf+sizeof (proto_ipv6_t), data_len);
108 case NET_PROTO_IP_TYPE_UDP6:
109 return net_proto_udp6_handler (packet, ipv6, buf+sizeof (proto_ipv6_t), data_len);
110 case NET_PROTO_IP_TYPE_ICMP6:
111 return net_proto_icmp6_handler (packet, ipv6, buf+sizeof (proto_ipv6_t), data_len);
114 return 0;
117 unsigned init_net_proto_tun6 ()
119 tun6_addr (0);
121 return 1;