3 * Copyright (C) 2008 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
4 * Copyright (C) 2009 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 #include <net/packet.h>
30 #include <net/socket.h>
35 extern netif_t netif_list
;
36 proto_udp6_conn_t proto_udp6_conn_list
;
39 proto_udp6_conn_t
*net_proto_udp6_conn_check (net_ipv6 ip_source
, net_port port_source
, net_ipv6 ip_dest
, net_port port_dest
, unsigned char *ret
);
40 int net_proto_udp6_read_cache (proto_udp6_conn_t
*conn
, char *data
, unsigned len
);
41 int net_proto_udp6_write (proto_udp6_conn_t
*conn
, char *data
, unsigned len
);
42 proto_udp6_conn_t
*net_proto_udp6_conn_find (int fd
);
43 int net_proto_udp6_conn_set (proto_udp6_conn_t
*conn
, netif_t
*eth
, net_port port_source
, net_ipv6 ip_dest
, net_port port_dest
);
44 unsigned net_proto_udp6_conn_del (proto_udp6_conn_t
*conn
);
45 int net_proto_udp6_conn_add ();
48 * User-friendly socket functions
50 int net_proto_udp6_socket (fd_t
*fd
)
52 return net_proto_udp6_conn_add (fd
);
55 int net_proto_udp6_connect (int fd
, sockaddr_in6
*addr
)
59 proto_udp6_conn_t
*conn
= net_proto_udp6_conn_find (fd
);
65 for (netif
= netif_list
.next
; netif
!= &netif_list
; netif
= netif
->next
) {
66 ret
= net_proto_udp6_conn_set (conn
, netif
, swap16 (netif_port_get ()), addr
->sin6_addr
, addr
->sin6_port
);
68 /* TODO: connect timeout */
71 if (!(conn
->flags
& O_NONBLOCK
)) {
75 /* non-blocking mode */
84 int net_proto_udp6_send (int fd
, char *msg
, unsigned size
)
86 proto_udp6_conn_t
*conn
= net_proto_udp6_conn_find (fd
);
91 int ret
= net_proto_udp6_write (conn
, msg
, size
);
95 if (!(conn
->flags
& O_NONBLOCK
)) {
98 /* non-blocking mode */
107 int net_proto_udp6_sendto (int fd
, char *msg
, unsigned size
, sockaddr_in6
*to
)
109 proto_udp6_conn_t
*conn
= net_proto_udp6_conn_find (fd
);
114 //int ip = conn->ip_dest;
116 /*net_proto_ip_print (conn->ip_dest);
118 net_proto_ip_print (conn->ip_source);*/
120 //conn->ip_source = to->sin6_addr;
122 int ret
= net_proto_udp6_write (conn
, msg
, size
);
124 //conn->ip_dest = ip;
128 if (!(conn
->flags
& O_NONBLOCK
)) {
131 /* non-blocking mode */
140 extern unsigned long timer_ticks
;
141 int net_proto_udp6_recv (int fd
, char *msg
, unsigned size
)
143 proto_udp6_conn_t
*conn
= net_proto_udp6_conn_find (fd
);
154 if (!(conn
->flags
& O_NONBLOCK
)) {
155 unsigned long stime
= timer_ticks
;
158 if ((stime
+10000) < timer_ticks
)
171 if (conn
->len
>= size
)
177 memcpy (msg
, conn
->data
, conn
->len
);
186 int net_proto_udp6_recvfrom (int fd
, char *msg
, unsigned size
, sockaddr_in6
*from
)
188 proto_udp6_conn_t
*conn
= net_proto_udp6_conn_find (fd
);
198 //int ip = conn->ip_dest;
200 memcpy (from
->sin6_addr
, (void *) conn
->ip_dest
, sizeof (net_ipv6
));
201 //kprintf ("recvfrom -> ip: 0x%x\n", from->sin6_addr);
204 if (!(conn
->flags
& O_NONBLOCK
)) {
210 //conn->ip_dest = ip;
215 //conn->ip_dest = ip; // return old ip address back to structure
220 if (conn
->len
>= size
)
226 memcpy (msg
, conn
->data
, conn
->len
);
235 int net_proto_udp6_close (int fd
)
237 proto_udp6_conn_t
*conn
= net_proto_udp6_conn_find (fd
);
242 int ret
= net_proto_udp6_conn_del (conn
);
247 int net_proto_udp6_fcntl (int fd
, int cmd
, long arg
)
249 proto_udp6_conn_t
*conn
= net_proto_udp6_conn_find (fd
);
265 int net_proto_udp6_bind (int fd
, sockaddr_in6
*addr
, socklen_t len
)
267 proto_udp6_conn_t
*conn
= net_proto_udp6_conn_find (fd
);
273 for (netif
= netif_list
.next
; netif
!= &netif_list
; netif
= netif
->next
) {
274 net_proto_udp6_conn_set (conn
, netif
, addr
->sin6_port
, 0, 0);
286 /* handler for received udp datagrams */
287 unsigned net_proto_udp6_handler (packet_t
*packet
, proto_ipv6_t
*ip
, char *buf
, unsigned len
)
290 proto_udp_t
*udp
= (proto_udp_t
*) buf
;
292 /* First check ip address and ports, that we want this data */
293 proto_udp6_conn_t
*conn
= net_proto_udp6_conn_check (ip
->ip_dest
, udp
->port_dest
, ip
->ip_source
, udp
->port_source
, &ret
);
295 if (!conn
&& ret
== 0)
299 memcpy (conn
->ip_dest
, (void *) ip
->ip_source
, sizeof (net_ipv6
));
301 /* HACK: very ugly */
302 conn
->port_dest
= udp
->port_source
;
304 /* save data to buffer; +8 because udp packet is 8byte header and then data */
305 int ret2
= net_proto_udp6_read_cache (conn
, buf
+8, len
-8);
313 /* save received data to buffer */
314 int net_proto_udp6_read_cache (proto_udp6_conn_t
*conn
, char *data
, unsigned len
)
316 if (!data
|| !conn
|| !len
)
320 conn
->data
= (char *) kmalloc (sizeof (char) * (len
+ 1));
322 conn
->data
= (char *) krealloc (conn
->data
, (sizeof (char) * (conn
->len
+len
)));
327 memcpy (conn
->data
+conn
->len
, data
, len
);
331 //conn->data[conn->len] = '\0';
336 int net_proto_udp6_write (proto_udp6_conn_t
*conn
, char *data
, unsigned len
)
338 if (!conn
|| !len
|| !data
)
342 if (!tun6_addr_get ()) {
343 unsigned get
= ndp_cache_get (conn
->ip_dest
, &mac_dest
);
346 ndp_send_nbrequest (conn
->netif
, conn
->ip_dest
);
349 /* 100ms for waiting on ARP reply */
351 get
= ndp_cache_get (conn
->ip_dest
, &mac_dest
);
356 /* TODO: make better waiting for ARP reply */
368 /* IPv6 tunnel stuff */
369 net_ipv4 tunnel
= tun6_addr_get ();
370 unsigned get
= arp_cache_get (tunnel
, &mac_dest
);
373 arp_send_request (conn
->netif
, tunnel
);
376 /* 100ms for waiting on ARP reply */
378 get
= arp_cache_get (tunnel
, &mac_dest
);
383 /* TODO: make better waiting for ARP reply */
397 packet_t
*packet
= (packet_t
*) kmalloc (sizeof (packet_t
));
402 memcpy (&packet
->mac_source
, conn
->netif
->dev
->dev_addr
, 6);
403 memcpy (&packet
->mac_dest
, mac_dest
, 6);
404 packet
->type
= NET_PACKET_TYPE_IPV6
;
407 proto_ipv6_t
*ip
= (proto_ipv6_t
*) kmalloc (sizeof (proto_ipv6_t
));
412 /* there are some fixed values - yeah it is horrible */
416 ip
->pl_len
= swap16 (len
);
417 ip
->nhead
= NET_PROTO_IP_TYPE_UDP6
;
419 memcpy (ip
->ip_source
, (void *) conn
->ip_source
, sizeof (net_ipv6
));
420 memcpy (ip
->ip_dest
, (void *) conn
->ip_dest
, sizeof (net_ipv6
));
423 proto_udp_t
*udp
= (proto_udp_t
*) kmalloc (sizeof (proto_udp_t
));
428 udp
->port_source
= conn
->port_source
;
429 udp
->port_dest
= conn
->port_dest
;
431 udp
->len
= ip
->pl_len
;
436 /* calculate total length of packet (udp/ip) */
439 char *buf_udp
= (char *) kmalloc ((9 + len
) * sizeof (char));
444 memcpy (buf_udp
, (char *) udp
, 8);
445 memcpy (buf_udp
+8, data
, len
);
447 buf_udp
[8 + len
] = '\0';
449 /* calculate checksum and put it to udp header */
450 proto_udp_t
*udp_
= (proto_udp_t
*) buf_udp
;
451 udp_
->checksum
= checksum16_ipv6 (conn
->ip_source
, conn
->ip_dest
, buf_udp
, 8 + len
, NET_PROTO_IP_TYPE_UDP6
);
455 /* send udp header+data to ip layer */
456 if (!tun6_addr_get ())
457 ret
= net_proto_ipv6_send (conn
->netif
, packet
, ip
, (char *) buf_udp
, 8 + len
);
459 ret
= net_proto_tun6_send (conn
->netif
, packet
, ip
, (char *) buf_udp
, 8 + len
);
470 /* Create new UDP connection */
471 int net_proto_udp6_conn_add (fd_t
*fd
)
473 proto_udp6_conn_t
*conn
;
475 /* alloc and init context */
476 conn
= (proto_udp6_conn_t
*) kmalloc (sizeof (proto_udp6_conn_t
));
483 memset (conn
, 0, sizeof (proto_udp6_conn_t
));
490 conn
->next
= &proto_udp6_conn_list
;
491 conn
->prev
= proto_udp6_conn_list
.prev
;
492 conn
->prev
->next
= conn
;
493 conn
->next
->prev
= conn
;
498 /* Setup new connection */
499 int net_proto_udp6_conn_set (proto_udp6_conn_t
*conn
, netif_t
*eth
, net_port port_source
, net_ipv6 ip_dest
, net_port port_dest
)
504 memcpy (conn
->ip_source
, (void *) eth
->ipv6
, sizeof (net_ipv6
));
505 memcpy (conn
->ip_dest
, (void *) ip_dest
, sizeof (net_ipv6
));
507 conn
->port_source
= port_source
;
508 conn
->port_dest
= port_dest
;
520 /* Delete existing connection from list */
521 unsigned net_proto_udp6_conn_del (proto_udp6_conn_t
*conn
)
531 conn
->next
->prev
= conn
->prev
;
532 conn
->prev
->next
= conn
->next
;
539 proto_udp6_conn_t
*net_proto_udp6_conn_check (net_ipv6 ip_source
, net_port port_source
, net_ipv6 ip_dest
, net_port port_dest
, unsigned char *ret
)
542 proto_udp6_conn_t
*conn
= NULL
;
543 proto_udp6_conn_t
*conn_ret
= NULL
;
545 for (conn
= proto_udp6_conn_list
.next
; conn
!= &proto_udp6_conn_list
; conn
= conn
->next
) {
546 if (conn
->ip_source
== ip_source
&& conn
->port_source
== port_source
) {
547 if (conn
->ip_dest
== ip_dest
/*&& conn->port_dest == port_dest*/) {
559 if (!conn_ret
->bind
) {
562 for (conn
= proto_udp6_conn_list
.next
; conn
!= &proto_udp6_conn_list
; conn
= conn
->next
) {
564 if (conn
->ip_source
== ip_source
&& conn
->port_source
== port_source
)
573 proto_udp6_conn_t
*net_proto_udp6_conn_find (int fd
)
575 proto_udp6_conn_t
*conn
= NULL
;
576 for (conn
= proto_udp6_conn_list
.next
; conn
!= &proto_udp6_conn_list
; conn
= conn
->next
) {
584 /* init of udpv6 protocol */
585 unsigned init_net_proto_udp6 ()
587 proto_udp6_conn_list
.next
= &proto_udp6_conn_list
;
588 proto_udp6_conn_list
.prev
= &proto_udp6_conn_list
;