3 * Copyright (C) 2008 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
4 * Copyright (C) 2009 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
5 * Copyright (C) 2010 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
29 #include <net/packet.h>
31 #include <net/socket.h>
35 extern netif_t netif_list
;
37 proto_udp6_conn_t proto_udp6_conn_list
;
39 static proto_ipv6_t proto_ipv6_prealloc
;
40 static proto_udp_t proto_udp_prealloc
;
41 static packet_t packet_prealloc
;
42 static char buf_udp_prealloc
[NET_PACKET_MTU
+ sizeof (proto_udp_t
) + 1];
45 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
);
46 int net_proto_udp6_read_cache (proto_udp6_conn_t
*conn
, char *data
, unsigned len
);
47 int net_proto_udp6_write (proto_udp6_conn_t
*conn
, char *data
, unsigned len
);
48 proto_udp6_conn_t
*net_proto_udp6_conn_find (int fd
);
49 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
);
50 unsigned net_proto_udp6_conn_del (proto_udp6_conn_t
*conn
);
51 int net_proto_udp6_conn_add ();
54 * User-friendly socket functions
56 int net_proto_udp6_socket (fd_t
*fd
)
58 return net_proto_udp6_conn_add (fd
);
61 int net_proto_udp6_connect (int fd
, sockaddr_in6
*addr
)
65 proto_udp6_conn_t
*conn
= net_proto_udp6_conn_find (fd
);
71 for (netif
= netif_list
.next
; netif
!= &netif_list
; netif
= netif
->next
) {
72 ret
= net_proto_udp6_conn_set (conn
, netif
, swap16 (netif_port_get ()), addr
->sin6_addr
, addr
->sin6_port
);
74 /* TODO: connect timeout */
77 if (!(conn
->flags
& O_NONBLOCK
)) {
81 /* non-blocking mode */
90 int net_proto_udp6_send (int fd
, char *msg
, unsigned size
)
92 proto_udp6_conn_t
*conn
= net_proto_udp6_conn_find (fd
);
97 int ret
= net_proto_udp6_write (conn
, msg
, size
);
101 if (!(conn
->flags
& O_NONBLOCK
)) {
104 /* non-blocking mode */
113 int net_proto_udp6_sendto (int fd
, char *msg
, unsigned size
, sockaddr_in6
*to
)
115 proto_udp6_conn_t
*conn
= net_proto_udp6_conn_find (fd
);
122 unsigned mtu_udp
= NET_PACKET_MTU
- sizeof (proto_udp_t
) - sizeof (net_ipv6
) - 16;
125 if (size
> mtu_udp
) {
126 int r
= net_proto_udp6_sendto (fd
, msg
, mtu_udp
, to
);
137 int ret
= net_proto_udp6_write (conn
, msg
, size
);
141 if (!(conn
->flags
& O_NONBLOCK
)) {
144 /* non-blocking mode */
153 extern unsigned long timer_ticks
;
154 int net_proto_udp6_recv (int fd
, char *msg
, unsigned size
)
156 proto_udp6_conn_t
*conn
= net_proto_udp6_conn_find (fd
);
167 if (!(conn
->flags
& O_NONBLOCK
)) {
168 unsigned long stime
= timer_ticks
;
171 if ((stime
+10000) < timer_ticks
)
184 if (conn
->len
>= size
)
190 memcpy (msg
, conn
->data
, conn
->len
);
199 int net_proto_udp6_recvfrom (int fd
, char *msg
, unsigned size
, sockaddr_in6
*from
)
201 proto_udp6_conn_t
*conn
= net_proto_udp6_conn_find (fd
);
211 //int ip = conn->ip_dest;
213 memcpy (from
->sin6_addr
, (void *) conn
->ip_dest
, sizeof (net_ipv6
));
214 //kprintf ("recvfrom -> ip: 0x%x\n", from->sin6_addr);
217 if (!(conn
->flags
& O_NONBLOCK
)) {
223 //conn->ip_dest = ip;
228 //conn->ip_dest = ip; // return old ip address back to structure
233 if (conn
->len
>= size
)
239 memcpy (msg
, conn
->data
, conn
->len
);
248 int net_proto_udp6_close (int fd
)
250 proto_udp6_conn_t
*conn
= net_proto_udp6_conn_find (fd
);
255 int ret
= net_proto_udp6_conn_del (conn
);
260 int net_proto_udp6_fcntl (int fd
, int cmd
, long arg
)
262 proto_udp6_conn_t
*conn
= net_proto_udp6_conn_find (fd
);
278 int net_proto_udp6_bind (int fd
, sockaddr_in6
*addr
, socklen_t len
)
280 proto_udp6_conn_t
*conn
= net_proto_udp6_conn_find (fd
);
286 for (netif
= netif_list
.next
; netif
!= &netif_list
; netif
= netif
->next
) {
287 net_proto_udp6_conn_set (conn
, netif
, addr
->sin6_port
, 0, 0);
295 int net_proto_udp6_select (int readfd
, int writefd
, int exceptfd
)
306 proto_udp6_conn_t
*conn
= net_proto_udp6_conn_find (fd
);
311 /* Are some data available ? */
323 /* handler for received udp datagrams */
324 unsigned net_proto_udp6_handler (packet_t
*packet
, proto_ipv6_t
*ip
, char *buf
, unsigned len
)
327 proto_udp_t
*udp
= (proto_udp_t
*) buf
;
329 /* First check ip address and ports, that we want this data */
330 proto_udp6_conn_t
*conn
= net_proto_udp6_conn_check (ip
->ip_dest
, udp
->port_dest
, ip
->ip_source
, udp
->port_source
, &ret
);
332 if (!conn
&& ret
== 0)
336 memcpy (conn
->ip_dest
, (void *) ip
->ip_source
, sizeof (net_ipv6
));
338 /* HACK: very ugly */
339 conn
->port_dest
= udp
->port_source
;
341 /* save data to buffer; +8 because udp packet is 8byte header and then data */
342 int ret2
= net_proto_udp6_read_cache (conn
, buf
+8, len
-8);
350 /* save received data to buffer */
351 int net_proto_udp6_read_cache (proto_udp6_conn_t
*conn
, char *data
, unsigned len
)
353 if (!data
|| !conn
|| !len
)
357 conn
->data
= (char *) kmalloc (sizeof (char) * (len
+ 1));
359 conn
->data
= (char *) krealloc (conn
->data
, (sizeof (char) * (conn
->len
+len
)));
364 memcpy (conn
->data
+conn
->len
, data
, len
);
368 //conn->data[conn->len] = '\0';
373 int net_proto_udp6_write (proto_udp6_conn_t
*conn
, char *data
, unsigned len
)
375 if (!conn
|| !len
|| !data
)
379 if (!tun6_addr_get ()) {
380 unsigned get
= ndp_cache_get (conn
->ip_dest
, &mac_dest
);
383 ndp_send_nbrequest (conn
->netif
, conn
->ip_dest
);
386 /* 100ms for waiting on ARP reply */
388 get
= ndp_cache_get (conn
->ip_dest
, &mac_dest
);
393 /* TODO: make better waiting for ARP reply */
405 /* IPv6 tunnel stuff */
406 net_ipv4 tunnel
= tun6_addr_get ();
407 unsigned get
= arp_cache_get (tunnel
, &mac_dest
);
410 arp_send_request (conn
->netif
, tunnel
);
413 /* 100ms for waiting on ARP reply */
415 get
= arp_cache_get (tunnel
, &mac_dest
);
420 /* TODO: make better waiting for ARP reply */
434 packet_t
*packet
= (packet_t
*) &packet_prealloc
;
439 memcpy (&packet
->mac_source
, conn
->netif
->dev
->dev_addr
, 6);
440 memcpy (&packet
->mac_dest
, mac_dest
, 6);
441 packet
->type
= NET_PACKET_TYPE_IPV6
;
444 proto_ipv6_t
*ip
= (proto_ipv6_t
*) &proto_ipv6_prealloc
;
449 /* there are some fixed values - yeah it is horrible */
453 ip
->pl_len
= swap16 (len
);
454 ip
->nhead
= NET_PROTO_IP_TYPE_UDP6
;
456 memcpy (ip
->ip_source
, (void *) conn
->ip_source
, sizeof (net_ipv6
));
457 memcpy (ip
->ip_dest
, (void *) conn
->ip_dest
, sizeof (net_ipv6
));
460 proto_udp_t
*udp
= (proto_udp_t
*) &proto_udp_prealloc
;
465 udp
->port_source
= conn
->port_source
;
466 udp
->port_dest
= conn
->port_dest
;
467 udp
->len
= ip
->pl_len
;
470 if ((len
+ 8) > (NET_PACKET_MTU
+ sizeof (proto_udp_t
) + 1)) {
471 DPRINT (DBG_NET
| DBG_SOCKET
| DBG_UDP
, "UDP -> data lenght is exceed: %d/%d bytes", len
+8, NET_PACKET_MTU
+ sizeof (proto_udp_t
) + 1);
475 char *buf_udp
= (char *) &buf_udp_prealloc
;
480 memcpy (buf_udp
, (char *) udp
, 8);
481 memcpy (buf_udp
+8, data
, len
);
483 buf_udp
[8 + len
] = '\0';
485 /* calculate checksum and put it to udp header */
486 proto_udp_t
*udp_
= (proto_udp_t
*) buf_udp
;
487 udp_
->checksum
= checksum16_ipv6 (conn
->ip_source
, conn
->ip_dest
, buf_udp
, 8 + len
, NET_PROTO_IP_TYPE_UDP6
);
491 /* send udp header+data to ip layer */
492 if (!tun6_addr_get ())
493 ret
= net_proto_ipv6_send (conn
->netif
, packet
, ip
, (char *) buf_udp
, 8 + len
);
495 ret
= net_proto_tun6_send (conn
->netif
, packet
, ip
, (char *) buf_udp
, 8 + len
);
501 /* Create new UDP connection */
502 int net_proto_udp6_conn_add (fd_t
*fd
)
504 proto_udp6_conn_t
*conn
;
506 /* alloc and init context */
507 conn
= (proto_udp6_conn_t
*) kmalloc (sizeof (proto_udp6_conn_t
));
514 memset (conn
, 0, sizeof (proto_udp6_conn_t
));
521 conn
->next
= &proto_udp6_conn_list
;
522 conn
->prev
= proto_udp6_conn_list
.prev
;
523 conn
->prev
->next
= conn
;
524 conn
->next
->prev
= conn
;
529 /* Setup new connection */
530 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
)
535 memcpy (conn
->ip_source
, (void *) eth
->ipv6
, sizeof (net_ipv6
));
536 memcpy (conn
->ip_dest
, (void *) ip_dest
, sizeof (net_ipv6
));
538 conn
->port_source
= port_source
;
539 conn
->port_dest
= port_dest
;
551 /* Delete existing connection from list */
552 unsigned net_proto_udp6_conn_del (proto_udp6_conn_t
*conn
)
562 conn
->next
->prev
= conn
->prev
;
563 conn
->prev
->next
= conn
->next
;
570 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
)
573 proto_udp6_conn_t
*conn
= NULL
;
574 proto_udp6_conn_t
*conn_ret
= NULL
;
576 for (conn
= proto_udp6_conn_list
.next
; conn
!= &proto_udp6_conn_list
; conn
= conn
->next
) {
577 if (conn
->ip_source
== ip_source
&& conn
->port_source
== port_source
) {
578 if (conn
->ip_dest
== ip_dest
/*&& conn->port_dest == port_dest*/) {
590 if (!conn_ret
->bind
) {
593 for (conn
= proto_udp6_conn_list
.next
; conn
!= &proto_udp6_conn_list
; conn
= conn
->next
) {
595 if (conn
->ip_source
== ip_source
&& conn
->port_source
== port_source
)
604 proto_udp6_conn_t
*net_proto_udp6_conn_find (int fd
)
606 proto_udp6_conn_t
*conn
= NULL
;
607 for (conn
= proto_udp6_conn_list
.next
; conn
!= &proto_udp6_conn_list
; conn
= conn
->next
) {
615 /* init of udpv6 protocol */
616 unsigned init_net_proto_udp6 ()
618 proto_udp6_conn_list
.next
= &proto_udp6_conn_list
;
619 proto_udp6_conn_list
.prev
= &proto_udp6_conn_list
;