KallistiOS git master
Independent SDK for the Sega Dreamcast
Loading...
Searching...
No Matches
net.h
Go to the documentation of this file.
1/* KallistiOS ##version##
2
3 include/kos/net.h
4 Copyright (C) 2002 Megan Potter
5 Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2012, 2013,
6 2016 Lawrence Sebald
7
8*/
9
10/** \file kos/net.h
11 \brief Network support.
12 \ingroup networking_drivers
13
14 This file contains declarations related to networking support.
15
16 \author Lawrence Sebald
17 \author Megan Potter
18*/
19
20#ifndef __KOS_NET_H
21#define __KOS_NET_H
22
23#include <sys/cdefs.h>
24__BEGIN_DECLS
25
26#include <arch/types.h>
27#include <sys/queue.h>
28#include <netinet/in.h>
29
30/* All functions in this header return < 0 on failure, and 0 on success. */
31
32/** \defgroup networking Networking
33 \brief APIs and drivers for network and internet connectivity
34
35 KOS' built-in network stack supports UDP over IPv4, and to some degree has
36 some basic IPv6 support as well. This will change over time, hopefully
37 leaving us with full TCP and UDP support both over IPv4 and IPv6.
38*/
39
40/** \defgroup networking_drivers Drivers
41 \brief Low-level Drivers for Network Devices
42 \ingroup networking
43*/
44
45/** \brief Structure describing one usable network device.
46 \ingroup networking_drivers
47
48 Each usable network device should have one of these describing it. These
49 must be registered to the network layer before the device is usable.
50
51 \headerfile kos/net.h
52*/
53typedef struct knetif {
54 /** \brief Device list handle (not a function!) */
55 LIST_ENTRY(knetif) if_list;
56
57 /** \brief Device name ("bba", "la", etc) */
58 const char *name;
59
60 /** \brief Long description of the device */
61 const char *descr;
62
63 /** \brief Unit index (starts at zero and counts upwards for multiple
64 network devices of the same type) */
65 int index;
66
67 /** \brief Internal device ID (for whatever the driver wants) */
69
70 /** \brief Interface flags */
72
73 /** \brief The device's MAC address */
74 uint8 mac_addr[6];
75
76 /** \brief The device's IP address (if any) */
77 uint8 ip_addr[4];
78
79 /** \brief The device's netmask */
80 uint8 netmask[4];
81
82 /** \brief The device's gateway's IP address */
83 uint8 gateway[4];
84
85 /** \brief The device's broadcast address */
86 uint8 broadcast[4];
87
88 /** \brief The device's DNS server address */
89 uint8 dns[4];
90
91 /** \brief The device's MTU */
92 int mtu;
93
94 /** \brief The device's Link-local IPv6 address */
95 struct in6_addr ip6_lladdr;
96
97 /** \brief Any further IPv6 addresses the device has.
98 The first address in this list will always be used, unless otherwise
99 specified. */
102
103 /** \brief The device's gateway's IPv6 address */
104 struct in6_addr ip6_gateway;
105
106 /** \brief Default MTU over IPv6 */
108
109 /** \brief Default hop limit over IPv6 */
111
112 /* All of the following callback functions should return a negative
113 value on failure, and a zero or positive value on success. Some
114 functions have special values, as noted. */
115
116 /** \brief Attempt to detect the device.
117 \param self The network device in question.
118 \return 0 on success, <0 on failure.
119 */
120 int (*if_detect)(struct knetif * self);
121
122 /** \brief Initialize the device.
123 \param self The network device in question.
124 \return 0 on success, <0 on failure.
125 */
126 int (*if_init)(struct knetif * self);
127
128 /** \brief Shutdown the device.
129 \param self The network device in question.
130 \return 0 on success, <0 on failure.
131 */
132 int (*if_shutdown)(struct knetif * self);
133
134 /** \brief Start the device (after init or stop).
135 \param self The network device in question.
136 \return 0 on success, <0 on failure.
137 */
138 int (*if_start)(struct knetif * self);
139
140 /** \brief Stop (hibernate) the device.
141 \param self The network device in question.
142 \return 0 on success, <0 on failure
143 */
144 int (*if_stop)(struct knetif * self);
145
146 /** \brief Queue a packet for transmission.
147 \param self The network device in question.
148 \param data The packet to transmit.
149 \param len The length of the packet in bytes.
150 \param blocking 1 if we should block if needed, 0 otherwise.
151 \retval NETIF_TX_OK On success.
152 \retval NETIF_TX_ERROR On general failure.
153 \retval NETIF_TX_AGAIN If non-blocking and we must block to send.
154 */
155 int (*if_tx)(struct knetif * self, const uint8 * data, int len,
156 int blocking);
157
158 /** \brief Commit any queued output packets.
159 \param self The network device in question.
160 \return 0 on success, <0 on failure.
161 */
162 int (*if_tx_commit)(struct knetif * self);
163
164 /** \brief Poll for queued receive packets, if necessary.
165 \param self The network device in question.
166 \return 0 on success, <0 on failure.
167 */
168 int (*if_rx_poll)(struct knetif * self);
169
170 /** \brief Set flags; you should generally manipulate flags through here so
171 that the driver gets a chance to act on the info.
172 \param self The network device in question.
173 \param flags_and Bitmask to and with the flags.
174 \param flags_or Bitmask to or with the flags.
175 */
176 int (*if_set_flags)(struct knetif * self, uint32 flags_and, uint32 flags_or);
177
178 /** \brief Set the device's multicast list.
179 \param self The network device in question.
180 \param list The list of MAC addresses (6 * count bytes).
181 \param count The number of addresses in list.
182 */
183 int (*if_set_mc)(struct knetif *self, const uint8 *list, int count);
184} netif_t;
185
186/** \defgroup net_drivers_flags netif_t Flags
187 \brief Network interface flags
188 \ingroup networking_drivers
189 @{
190*/
191#define NETIF_NO_FLAGS 0x00000000 /**< \brief No flags set */
192#define NETIF_REGISTERED 0x00000001 /**< \brief Is it registered? */
193#define NETIF_DETECTED 0x00000002 /**< \brief Is it detected? */
194#define NETIF_INITIALIZED 0x00000004 /**< \brief Has it been initialized? */
195#define NETIF_RUNNING 0x00000008 /**< \brief Has start() been called? */
196#define NETIF_PROMISC 0x00010000 /**< \brief Promiscuous mode */
197#define NETIF_NEEDSPOLL 0x01000000 /**< \brief Needs to be polled for input */
198#define NETIF_NOETH 0x10000000 /**< \brief Does not use ethernet */
199/** @} */
200
201/** \defgroup net_drivers_returns TX Return Values
202 \brief Driver return values for TX network interfaces
203 \ingroup networking_drivers
204 @{
205 */
206#define NETIF_TX_OK 0 /**< \brief Tx success */
207#define NETIF_TX_ERROR -1 /**< \brief Tx general error */
208#define NETIF_TX_AGAIN -2 /**< \brief Retry Tx later */
209/** @} */
210
211/** \defgroup net_drivers_blocking Blocking Types
212 \brief Blocking type avlues for network interfaces
213 \ingroup networking_drivers
214 @{
215 */
216#define NETIF_NOBLOCK 0 /**< \brief Don't block for Tx */
217#define NETIF_BLOCK 1 /**< \brief Blocking is OK for Tx */
218/** @} */
219
220/** \cond */
221/* Define the list type */
222LIST_HEAD(netif_list, knetif);
223
224#ifdef PACKED
225#undef PACKED
226#endif
227
228#define PACKED __attribute__((packed))
229/** \endcond */
230
231/** \defgroup networking_ip IP
232 \brief API for the Internet Protocol
233 \ingroup networking
234*/
235
236/** \defgroup networking_ipv4 IPv4
237 \brief IPv4 Network Stack
238 \ingroup networking_ip
239*/
240
241/** \brief IPv4 Packet header.
242 \headerfile kos/net.h
243 \ingroup networking_ipv4
244*/
245typedef struct ip_hdr_s {
246 uint8 version_ihl; /**< \brief IP version and header length */
247 uint8 tos; /**< \brief Type of Service */
248 uint16 length; /**< \brief Length */
249 uint16 packet_id; /**< \brief Packet ID */
250 uint16 flags_frag_offs; /**< \brief Flags and fragment offset */
251 uint8 ttl; /**< \brief Time to live */
252 uint8 protocol; /**< \brief IP protocol */
253 uint16 checksum; /**< \brief IP checksum */
254 uint32 src; /**< \brief Source IP address */
255 uint32 dest; /**< \brief Destination IP address */
256} PACKED ip_hdr_t;
257
258/** \defgroup networking_ipv6 IPv6
259 \brief IPv6 Network Stack
260 \ingroup networking_ip
261*/
262
263/** \brief IPv6 Packet header.
264 \headerfile kos/net.h
265 \ingroup networking_ipv6
266*/
267typedef struct ipv6_hdr_s {
268 uint8 version_lclass; /**< \brief Version and low-order class
269 byte */
270 uint8 hclass_lflow; /**< \brief High-order class byte, low-order
271 flow byte */
272 uint16 lclass; /**< \brief Low-order class byte */
273 uint16 length; /**< \brief Length */
274 uint8 next_header; /**< \brief Next header type */
275 uint8 hop_limit; /**< \brief Hop limit */
276 struct in6_addr src_addr; /**< \brief Source IP address */
277 struct in6_addr dst_addr; /**< \brief Destination IP address */
278} PACKED ipv6_hdr_t;
279
280#undef PACKED
281
282
283/***** net_arp.c **********************************************************/
284
285/** \defgroup networking_arp ARP
286 \brief API for the Address Resolution Protocol
287 \ingroup networking
288*/
289
290/** \brief Init ARP.
291 \ingroup networking_arp
292
293 \retval 0 On success (no error conditions defined).
294*/
295int net_arp_init(void);
296
297/** \brief Shutdown ARP.
298 \ingroup networking_arp
299 */
301
302/** \brief Add an entry to the ARP cache manually.
303 \ingroup networking_arp
304
305 \param nif The network device in use.
306 \param mac The MAC address of the entry.
307 \param ip The IPv4 address of the entry.
308 \param timestamp The entry's timestamp. Set to 0 for a permanent
309 entry, otherwise set to the current number of
310 milliseconds since boot (i.e, timer_ms_gettime64()).
311
312 \retval 0 On success.
313 \retval -1 Error allocating memory.
314*/
315int net_arp_insert(netif_t *nif, const uint8 mac[6], const uint8 ip[4],
316 uint64 timestamp);
317
318/** \brief Look up an entry from the ARP cache.
319 \ingroup networking_arp
320
321 If no entry is found, then an ARP query will be sent and an error will be
322 returned. If you specify a packet with the call, it will be sent when the
323 reply comes in.
324
325 \param nif The network device in use.
326 \param ip_in The IP address to lookup.
327 \param mac_out Storage for the MAC address, if found.
328 \param pkt A simple IPv4 header, if you want to send one when a
329 response comes in (if not found immediately).
330 \param data Packet data to go with the header.
331 \param data_size The size of data.
332
333 \retval 0 On success.
334 \retval -1 A query is outstanding for that address.
335 \retval -2 Address not found, query generated.
336 \retval -3 Error allocating memory.
337*/
338int net_arp_lookup(netif_t *nif, const uint8 ip_in[4], uint8 mac_out[6],
339 const ip_hdr_t *pkt, const uint8 *data, int data_size);
340
341/** \brief Do a reverse ARP lookup.
342 \ingroup networking_arp
343
344 This function looks for an IP for a given mac address; note that if this
345 fails, you have no recourse.
346
347 \param nif The network device in use.
348 \param ip_out Storage for the IPv4 address.
349 \param mac_in The MAC address to look up.
350
351 \retval 0 On success.
352 \retval -1 On failure.
353*/
354int net_arp_revlookup(netif_t *nif, uint8 ip_out[4], const uint8 mac_in[6]);
355
356/** \brief Receive an ARP packet and process it (called by net_input).
357 \ingroup networking_arp
358
359 \param nif The network device in use.
360 \param pkt The packet received.
361 \param len The length of the packet.
362
363 \retval 0 On success (no error conditions defined).
364*/
365int net_arp_input(netif_t *nif, const uint8 *pkt, int len);
366
367/** \brief Generate an ARP who-has query on the given device.
368 \ingroup networking_arp
369
370 \param nif The network device to use.
371 \param ip The IP to query.
372
373 \retval 0 On success (no error conditions defined).
374*/
375int net_arp_query(netif_t *nif, const uint8 ip[4]);
376
377
378/***** net_input.c *********************************************************/
379
380/** \brief Network input callback type.
381 \ingroup networking_drivers
382
383 \param nif The network device in use.
384 \param pkt The packet received.
385 \param len The length of the packet, in bytes.
386
387 \return 0 on success, <0 on failure.
388*/
389typedef int (*net_input_func)(netif_t *nif, const uint8 *pkt, int len);
390
391/** \brief Where will input packets be routed?
392 \ingroup networking_drivers
393 */
395
396/** \brief Device drivers should call this function to submit packets received
397 in the background.
398 \ingroup networking_drivers
399
400 This function may or may not return immediately but it won't take an
401 infinitely long time (so it's safe to call inside interrupt handlers).
402
403 \param device The network device submitting packets.
404 \param data The packet to submit.
405 \param len The length of the packet, in bytes.
406
407 \return 0 on success, <0 on failure.
408*/
409int net_input(netif_t *device, const uint8 *data, int len);
410
411/** \brief Setup a network input target.
412 \ingroup networking_drivers
413
414 \param t The new target callback.
415
416 \return The old target.
417*/
419
420/***** net_icmp.c *********************************************************/
421
422/** \defgroup networking_icmp ICMP
423 \brief API for the Internet Control Message Protocol
424 \ingroup networking
425*/
426
427/** \defgroup networking_icmpv4 ICMPv4
428 \brief API for v4 of the Internet Control Message
429 Protocol
430 \ingroup networking_icmp
431*/
432
433/** \brief ICMPv4 echo reply callback type.
434 \ingroup networking_icmpv4
435
436 \param ip The IPv4 address the reply is from.
437 \param seq The sequence number of the packet.
438 \param delta_us The time difference, in microseconds.
439 \param ttl The TTL value in the packet.
440 \param data Any data in the packet.
441 \param len The length of the data, in bytes.
442*/
443typedef void (*net_echo_cb)(const uint8 *ip, uint16 seq, uint64 delta_us,
444 uint8 ttl, const uint8 *data, size_t len);
445
446/** \brief Where will we handle possibly notifying the user of ping replies?
447 \ingroup networking_icmp
448 */
450
451/** \brief Send an ICMP Echo packet to the specified IP.
452 \ingroup networking_icmpv4
453
454 \param net The network device to use.
455 \param ipaddr The IPv4 address to send to.
456 \param ident A packet identifier.
457 \param seq A packet sequence number.
458 \param data Data to send with the packet.
459 \param size The size of the data to send.
460
461 \return 0 on success, <0 on failure.
462*/
463int net_icmp_send_echo(netif_t *net, const uint8 ipaddr[4], uint16 ident,
464 uint16 seq, const uint8 *data, size_t size);
465
466/** \defgroup networking_icmp_unreach Unreachable Values
467 \brief Valid values for net_icmp_send_dest_unreach().
468 \ingroup networking_icmpv4
469 @{
470*/
471#define ICMP_PROTOCOL_UNREACHABLE 2 /**< \brief Protocol unreachable */
472#define ICMP_PORT_UNREACHABLE 3 /**< \brief Port unreachable */
473/** @} */
474
475/** \brief Send an ICMP Destination Unreachable packet in reply to the given
476 message.
477 \ingroup networking_icmpv4
478
479 \param net The network device to use.
480 \param code The type of message this is.
481 \param msg The message that caused this error.
482
483 \return 0 on success, <0 on failure.
484*/
485int net_icmp_send_dest_unreach(netif_t *net, uint8 code, const uint8 *msg);
486
487/** \brief Valid values for the code in the net_icmp_send_time_exceeded()
488 function.
489 \ingroup networking_icmpv4
490 */
491#define ICMP_REASSEMBLY_TIME_EXCEEDED 1
492
493/** \brief Send an ICMP Time Exceeded packet in reply to the given message.
494 \ingroup networking_icmp
495
496 \param net The network device to use.
497 \param code The type of message this is.
498 \param msg The message that caused this error.
499
500 \return 0 on success, <0 on failure.
501*/
502int net_icmp_send_time_exceeded(netif_t *net, uint8 code, const uint8 *msg);
503
504/***** net_ipv4.c *********************************************************/
505
506/** \brief IPv4 statistics structure.
507 \ingroup networking_ipv4
508
509 This structure holds some basic statistics about the IPv4 layer of the
510 stack, and can be retrieved with the appropriate function.
511
512 \headerfile kos/net.h
513*/
514typedef struct net_ipv4_stats {
515 uint32 pkt_sent; /** \brief Packets sent out successfully */
516 uint32 pkt_send_failed; /** \brief Packets that failed to send */
517 uint32 pkt_recv; /** \brief Packets received successfully */
518 uint32 pkt_recv_bad_size; /** \brief Packets of a bad size */
519 uint32 pkt_recv_bad_chksum; /** \brief Packets with a bad checksum */
520 uint32 pkt_recv_bad_proto; /** \brief Packets with an unknown proto */
522
523/** \brief Retrieve statistics from the IPv4 layer.
524 \ingroup networking_ipv4
525
526 \return The net_ipv4_stats_t structure.
527*/
529
530/** \brief Create a 32-bit IP address, based on the individual numbers
531 contained within the IP.
532 \ingroup networking_ipv4
533
534 \param addr Array of IP address octets.
535
536 \return The address, in host byte order.
537*/
539
540/** \brief Parse an IP address that is packet into a uint32 into an array of
541 the individual bytes.
542 \ingroup networking_ipv4
543
544 \param addr The full address, in host byte order.
545 \param out The output buffer.
546*/
548
549/***** net_icmp6.c ********************************************************/
550
551/** \defgroup networking_icmpv6 ICMPv6
552 \brief API for v6 of the Internet Control Message
553 Protocol
554 \ingroup networking_icmp
555*/
556
557/** \brief ICMPv6 echo reply callback type.
558 \ingroup networking_icmpv6
559
560 \param ip The IPv6 address the reply is from.
561 \param seq The sequence number of the packet.
562 \param delta_us The time difference, in microseconds.
563 \param hlim The hop limit value in the packet.
564 \param data Any data in the packet.
565 \param len The length of the data, in bytes.
566*/
567typedef void (*net6_echo_cb)(const struct in6_addr *ip, uint16 seq,
568 uint64 delta_us, uint8 hlim, const uint8 *data,
569 size_t len);
570
571/** \brief Where will we handle possibly notifying the user of ping replies?
572 \ingroup networking_icmpv6
573 */
575
576/** \brief Send an ICMPv6 Echo (PING6) packet to the specified device.
577 \ingroup networking_icmpv6
578
579 \param net The network device to use.
580 \param dst The address to send to.
581 \param ident A packet identifier.
582 \param seq A packet sequence number.
583 \param data Data to send with the packet.
584 \param size Length of the data, in bytes.
585
586 \return 0 on success, <0 on failure.
587*/
588int net_icmp6_send_echo(netif_t *net, const struct in6_addr *dst, uint16 ident,
589 uint16 seq, const uint8 *data, size_t size);
590
591/** \brief Send a Neighbor Solicitation packet on the specified device.
592 \ingroup networking_icmpv6
593
594 \param net The network device to use.
595 \param dst The destination address.
596 \param target The target address.
597 \param dupdet 1 if this is for duplicate detection.
598
599 \return 0 on success, <0 on failure.
600*/
601int net_icmp6_send_nsol(netif_t *net, const struct in6_addr *dst,
602 const struct in6_addr *target, int dupdet);
603
604/** \brief Send a Neighbor Advertisement packet on the specified device.
605 \ingroup networking_icmpv6
606
607 \param net The network device to use.
608 \param dst The destination address.
609 \param target The target address.
610 \param sol 1 if solicited, 0 otherwise.
611
612 \return 0 on success, <0 on failure.
613*/
614int net_icmp6_send_nadv(netif_t *net, const struct in6_addr *dst,
615 const struct in6_addr *target, int sol);
616
617/** \brief Send a Router Solicitation request on the specified interface.
618 \ingroup networking_icmpv6
619
620 \param net The network device to use.
621
622 \return 0 on success, <0 on failure.
623*/
625
626/** \defgroup networking_icmpv6_unreachable Destination Unreachable Codes
627 \brief Destination unreachable packet types
628 \ingroup networking_icmpv6
629
630 Only port unreachable really makes sense
631
632 @{
633*/
634#define ICMP6_DEST_UNREACH_NO_ROUTE 0 /**< \brief No route available */
635#define ICMP6_DEST_UNREACH_PROHIBITED 1 /**< \brief Access prohibited */
636#define ICMP6_DEST_UNREACH_BEYOND_SCOPE 2 /**< \brief Gone beyond scope */
637#define ICMP6_DEST_UNREACH_ADDR_UNREACH 3 /**< \brief Address unreachable */
638#define ICMP6_DEST_UNREACH_PORT_UNREACH 4 /**< \brief Port unreachable */
639#define ICMP6_DEST_UNREACH_FAIL_EGRESS 5 /**< \brief Egress failure */
640#define ICMP6_DEST_UNREACH_BAD_ROUTE 6 /**< \brief Bad route specified */
641/** @} */
642
643/** \brief Send a destination unreachable packet on the specified interface.
644 \ingroup networking_icmpv6
645
646 \param net The network device to use.
647 \param code The type of message this is.
648 \param ppkt The message that caused this error.
649 \param psz Size of the original message.
650
651 \return 0 on success, <0 on failure.
652*/
653int net_icmp6_send_dest_unreach(netif_t *net, uint8 code, const uint8 *ppkt,
654 size_t psz);
655
656/** \defgroup networking_icmpv6_time_exceeded Time Exceeded Codes
657 \brief Time exceeded codes for ICMPv6
658 \ingroup networking_icmpv6
659
660 Only fragment reassembly time exceeded makes sense
661
662 @{
663*/
664#define ICMP6_TIME_EXCEEDED_HOPS_EXC 0 /**< \brief Hops exceeded */
665#define ICMP6_TIME_EXCEEDED_FRAGMENT 1 /**< \brief Reassembly time gone */
666/** @} */
667
668/** \brief Send a time exceeded message on the specified interface.
669 \ingroup networking_icmpv6
670
671 \param net The network device to use.
672 \param code The error code.
673 \param ppkt The message that caused this error.
674 \param psz Size of the original packet.
675
676 \return 0 on success, <0 on failure.
677*/
679 size_t psz);
680
681/** \defgroup networking_icmpv6_param_problem Parameter Problem Codes
682 \brief Codes for ICMPv6 parameter problem packets
683 \ingroup networking_icmpv6
684 @{
685 */
686#define ICMP6_PARAM_PROB_BAD_HEADER 0 /**< \brief Malformed header */
687#define ICMP6_PARAM_PROB_UNK_HEADER 1 /**< \brief Unknown header */
688#define ICMP6_PARAM_PROB_UNK_OPTION 2 /**< \brief Unknown header option */
689/** @} */
690
691/** \brief Send an ICMPv6 Parameter Problem about the given packet.
692 \ingroup networking_icmpv6
693
694 \param net The network device to use.
695 \param code The error code.
696 \param ptr Where in the packet is the error?
697 \param ppkt The message that caused the error.
698 \param psz Size of the original packet.
699
700 \return 0 on success, <0 on failure.
701*/
703 const uint8 *ppkt, size_t psz);
704
705/***** net_ipv6.c *********************************************************/
706
707/** \brief IPv6 statistics structure.
708 \ingroup networking_ipv6
709
710 This structure holds some basic statistics about the IPv6 layer of the
711 stack, and can be retrieved with the appropriate function.
712
713 \headerfile kos/net.h
714*/
715typedef struct net_ipv6_stats {
716 uint32 pkt_sent; /**< \brief Packets sent out successfully */
717 uint32 pkt_send_failed; /**< \brief Packets that failed to send */
718 uint32 pkt_recv; /**< \brief Packets received successfully */
719 uint32 pkt_recv_bad_size; /**< \brief Packets of a bad size */
720 uint32 pkt_recv_bad_proto; /**< \brief Packets with an unknown proto */
721 uint32 pkt_recv_bad_ext; /**< \brief Packets with an unknown hdr */
723
724/** \brief Retrieve statistics from the IPv6 layer.
725 \ingroup networking_ipv6
726
727 \return The global IPv6 stats structure.
728*/
730
731/***** net_ndp.c **********************************************************/
732
733/** \defgroup networking_ndp NDP
734 \brief API for the Neighbor Discovery Protocol
735 \ingroup networking
736 @{
737*/
738
739/** \brief Init NDP.
740 \retval 0 On success (no error conditions defined).
741*/
742int net_ndp_init(void);
743
744/** \brief Shutdown NDP. */
746
747/** \brief Garbage collect timed out NDP entries.
748 This will be called periodically as NDP queries come in.
749*/
750void net_ndp_gc(void);
751
752/** \brief Add an entry to the NDP cache.
753 \param nif The network device in question.
754 \param mac The MAC address for the entry.
755 \param ip The IPv6 address for the entry.
756 \param unsol Was this unsolicited?
757 \return 0 on success, <0 on failure.
758*/
759int net_ndp_insert(netif_t *nif, const uint8 mac[6], const struct in6_addr *ip,
760 int unsol);
761
762/** \brief Look up an entry from the NDP cache.
763
764 If no entry is found, then an NDP query will be sent and an error will be
765 returned. If you specify a packet with the call, it will be sent when the
766 reply comes in.
767
768 \param net The network device to use.
769 \param ip The IPv6 address to query.
770 \param mac_out Storage for the MAC address on success.
771 \param pkt A simple IPv6 header, if you want to send a packet
772 when a reply comes in.
773 \param data Anything that comes after the header.
774 \param data_size The size of data.
775 \return 0 on success, <0 on failure.
776*/
777int net_ndp_lookup(netif_t *net, const struct in6_addr *ip, uint8 mac_out[6],
778 const ipv6_hdr_t *pkt, const uint8 *data, int data_size);
779
780/** @} */
781
782/***** net_udp.c **********************************************************/
783
784/** \defgroup networking_udp UDP
785 \brief API for the User Datagram Protocol
786 \ingroup networking
787 @{
788*/
789
790/** \brief UDP statistics structure.
791
792 This structure holds some basic statistics about the UDP layer of the stack,
793 and can be retrieved with the appropriate function.
794
795 \headerfile kos/net.h
796*/
797typedef struct net_udp_stats {
798 uint32 pkt_sent; /**< \brief Packets sent out successfully */
799 uint32 pkt_send_failed; /**< \brief Packets that failed to send */
800 uint32 pkt_recv; /**< \brief Packets received successfully */
801 uint32 pkt_recv_bad_size; /**< \brief Packets of a bad size */
802 uint32 pkt_recv_bad_chksum; /**< \brief Packets with a bad checksum */
803 uint32 pkt_recv_no_sock; /**< \brief Packets with to a closed port */
805
806/** \brief Retrieve statistics from the UDP layer.
807
808 \return The global UDP stats struct.
809*/
811
812/** \brief Init UDP.
813
814 \retval 0 On success (no error conditions defined).
815*/
816int net_udp_init(void);
817
818/** \brief Shutdown UDP. */
820
821/** @} */
822
823/***** net_tcp.c **********************************************************/
824
825/** \defgroup networking_tcp TCP
826 \brief API for the Transmission Control Protocol
827 \ingroup networking
828 @{
829*/
830
831/** \brief Init TCP.
832 \retval 0 On success (no error conditions defined).
833*/
834int net_tcp_init(void);
835
836/** \brief Shutdown TCP. */
838
839/** @} */
840
841/***** net_crc.c **********************************************************/
842
843/** \defgroup networking_crc CRC
844 \brief API for Cyclic Redundancy Checking
845 \ingroup networking
846 @{
847*/
848
849/** \brief Calculate a "little-endian" CRC-32 over a block of data.
850
851 \param data The data to calculate over.
852 \param size The size of the data, in bytes.
853
854 \return The calculated CRC-32.
855*/
856uint32 net_crc32le(const uint8 *data, int size);
857
858/** \brief Calculate a "big-endian" CRC-32 over a block of data.
859
860 \param data The data to calculate over.
861 \param size The size of the data, in bytes.
862
863 \return The calculated CRC-32.
864*/
865uint32 net_crc32be(const uint8 *data, int size);
866
867/** \brief Calculate a CRC16-CCITT over a block of data.
868
869 \note Based on code found online at
870 http://www.ccsinfo.com/forum/viewtopic.php?t=24977
871
872 \param data The data to calculate over.
873 \param size The size of the data, in bytes.
874 \param start The value to start with. This could be a previous
875 return value from this function (if continuing a
876 previous calculation) or some initial seed value
877 (typically 0xFFFF or 0x0000).
878
879 \return The calculated CRC16-CCITT.
880*/
881uint16 net_crc16ccitt(const uint8 *data, int size, uint16 start);
882
883/** @} */
884
885/***** net_multicast.c ****************************************************/
886
887/** \defgroup networking_multicast Multicast
888 \brief API for Managing the Multicast List
889 \ingroup networking
890 @{
891*/
892
893/** \brief Add a entry to our multicast list.
894
895 This function will auto-commit the multicast list to the network interface
896 in the process.
897
898 \param mac The MAC address to add.
899 \return 0 on success, <0 on failure.
900*/
901int net_multicast_add(const uint8 mac[6]);
902
903/** \brief Delete a entry from our multicast list.
904
905 This function will auto-commit the multicast list to the network interface
906 in the process.
907
908 \param mac The MAC address to add.
909 \return 0 on success, <0 on failure.
910*/
911int net_multicast_del(const uint8 mac[6]);
912
913/** \brief Check if an address is on the multicast list.
914 \param mac The MAC address to check for.
915 \retval 0 The address is not in the list.
916 \retval 1 The address is in the list.
917 \retval -1 On error.
918*/
919int net_multicast_check(const uint8 mac[6]);
920
921/** \brief Init multicast support.
922 \return 0 on success, !0 on error.
923*/
925
926/** \brief Shutdown multicast support. */
928
929/** @} */
930
931/***** net_core.c *********************************************************/
932
933/** \brief Interface list; note: do not manipulate directly!
934 \ingroup networking_drivers
935 */
936extern struct netif_list net_if_list;
937
938/** \brief Function to retrieve the interface list.
939 \ingroup networking_drivers
940
941 \warning
942 Do not manipulate what this returns to you!
943
944 \return The network interface list.
945*/
946struct netif_list * net_get_if_list(void);
947
948/** \brief The default network device, used with sockets (read-only).
949 \ingroup networking_drivers
950*/
952
953/** \brief Set our default device to an arbitrary device.
954 \ingroup networking_drivers
955
956 \param n The device to set as default.
957
958 \return The old default device.
959*/
961
962/** \brief Register a network device.
963 \ingroup networking_drivers
964
965 \param device The device to register.
966 \
967 \return 0 on success, <0 on failure.
968*/
970
971/** \brief Unregister a network device.
972 \ingroup networking_drivers
973
974 \param device The device to unregister.
975
976 \return 0 on success, <0 on failure.
977*/
979
980/** \brief Init network support.
981 \ingroup networking_drivers
982
983 \note To auto-detect the IP address to assign to the
984 default device (i.e, over DHCP or from the flashrom
985 on the Dreamcast), pass 0 as the IP parameter.
986
987 \param ip The IPv4 address to set on the default device, in
988 host byte order.
989
990 \return 0 on success, <0 on failure.
991*/
993
994/** \brief Shutdown network support.
995 \ingroup networking_drivers
996*/
997void net_shutdown(void);
998
999__END_DECLS
1000
1001#endif /* __KOS_NET_H */
int net_arp_input(netif_t *nif, const uint8 *pkt, int len)
Receive an ARP packet and process it (called by net_input).
int net_arp_init(void)
Init ARP.
void net_arp_shutdown(void)
Shutdown ARP.
int net_arp_lookup(netif_t *nif, const uint8 ip_in[4], uint8 mac_out[6], const ip_hdr_t *pkt, const uint8 *data, int data_size)
Look up an entry from the ARP cache.
int net_arp_revlookup(netif_t *nif, uint8 ip_out[4], const uint8 mac_in[6])
Do a reverse ARP lookup.
int net_arp_query(netif_t *nif, const uint8 ip[4])
Generate an ARP who-has query on the given device.
int net_arp_insert(netif_t *nif, const uint8 mac[6], const uint8 ip[4], uint64 timestamp)
Add an entry to the ARP cache manually.
uint32 net_crc32le(const uint8 *data, int size)
Calculate a "little-endian" CRC-32 over a block of data.
uint16 net_crc16ccitt(const uint8 *data, int size, uint16 start)
Calculate a CRC16-CCITT over a block of data.
uint32 net_crc32be(const uint8 *data, int size)
Calculate a "big-endian" CRC-32 over a block of data.
int net_input(netif_t *device, const uint8 *data, int len)
Device drivers should call this function to submit packets received in the background.
netif_t * net_set_default(netif_t *n)
Set our default device to an arbitrary device.
struct netif_list * net_get_if_list(void)
Function to retrieve the interface list.
net_input_func net_input_target
Where will input packets be routed?
netif_t * net_default_dev
The default network device, used with sockets (read-only).
int net_reg_device(netif_t *device)
Register a network device.
int(* net_input_func)(netif_t *nif, const uint8 *pkt, int len)
Network input callback type.
Definition net.h:389
void net_shutdown(void)
Shutdown network support.
int net_init(uint32 ip)
Init network support.
net_input_func net_input_set_target(net_input_func t)
Setup a network input target.
int net_unreg_device(netif_t *device)
Unregister a network device.
struct netif_list net_if_list
Interface list; note: do not manipulate directly!
net_echo_cb net_icmp_echo_cb
Where will we handle possibly notifying the user of ping replies?
int net_icmp_send_time_exceeded(netif_t *net, uint8 code, const uint8 *msg)
Send an ICMP Time Exceeded packet in reply to the given message.
void(* net_echo_cb)(const uint8 *ip, uint16 seq, uint64 delta_us, uint8 ttl, const uint8 *data, size_t len)
ICMPv4 echo reply callback type.
Definition net.h:443
int net_icmp_send_echo(netif_t *net, const uint8 ipaddr[4], uint16 ident, uint16 seq, const uint8 *data, size_t size)
Send an ICMP Echo packet to the specified IP.
int net_icmp_send_dest_unreach(netif_t *net, uint8 code, const uint8 *msg)
Send an ICMP Destination Unreachable packet in reply to the given message.
int net_icmp6_send_echo(netif_t *net, const struct in6_addr *dst, uint16 ident, uint16 seq, const uint8 *data, size_t size)
Send an ICMPv6 Echo (PING6) packet to the specified device.
int net_icmp6_send_param_prob(netif_t *net, uint8 code, uint32 ptr, const uint8 *ppkt, size_t psz)
Send an ICMPv6 Parameter Problem about the given packet.
net6_echo_cb net_icmp6_echo_cb
Where will we handle possibly notifying the user of ping replies?
void(* net6_echo_cb)(const struct in6_addr *ip, uint16 seq, uint64 delta_us, uint8 hlim, const uint8 *data, size_t len)
ICMPv6 echo reply callback type.
Definition net.h:567
int net_icmp6_send_time_exceeded(netif_t *net, uint8 code, const uint8 *ppkt, size_t psz)
Send a time exceeded message on the specified interface.
int net_icmp6_send_rsol(netif_t *net)
Send a Router Solicitation request on the specified interface.
int net_icmp6_send_nadv(netif_t *net, const struct in6_addr *dst, const struct in6_addr *target, int sol)
Send a Neighbor Advertisement packet on the specified device.
int net_icmp6_send_nsol(netif_t *net, const struct in6_addr *dst, const struct in6_addr *target, int dupdet)
Send a Neighbor Solicitation packet on the specified device.
int net_icmp6_send_dest_unreach(netif_t *net, uint8 code, const uint8 *ppkt, size_t psz)
Send a destination unreachable packet on the specified interface.
void net_ipv4_parse_address(uint32 addr, uint8 out[4])
Parse an IP address that is packet into a uint32 into an array of the individual bytes.
uint32 net_ipv4_address(const uint8 addr[4])
Create a 32-bit IP address, based on the individual numbers contained within the IP.
net_ipv4_stats_t net_ipv4_get_stats(void)
Retrieve statistics from the IPv4 layer.
net_ipv6_stats_t net_ipv6_get_stats(void)
Retrieve statistics from the IPv6 layer.
void net_multicast_shutdown(void)
Shutdown multicast support.
int net_multicast_check(const uint8 mac[6])
Check if an address is on the multicast list.
int net_multicast_init(void)
Init multicast support.
int net_multicast_del(const uint8 mac[6])
Delete a entry from our multicast list.
int net_multicast_add(const uint8 mac[6])
Add a entry to our multicast list.
int net_ndp_insert(netif_t *nif, const uint8 mac[6], const struct in6_addr *ip, int unsol)
Add an entry to the NDP cache.
void net_ndp_shutdown(void)
Shutdown NDP.
void net_ndp_gc(void)
Garbage collect timed out NDP entries.
int net_ndp_lookup(netif_t *net, const struct in6_addr *ip, uint8 mac_out[6], const ipv6_hdr_t *pkt, const uint8 *data, int data_size)
Look up an entry from the NDP cache.
int net_ndp_init(void)
Init NDP.
void net_tcp_shutdown(void)
Shutdown TCP.
int net_tcp_init(void)
Init TCP.
net_udp_stats_t net_udp_get_stats(void)
Retrieve statistics from the UDP layer.
void net_udp_shutdown(void)
Shutdown UDP.
int net_udp_init(void)
Init UDP.
typedef LIST_HEAD(nmmgr_list, nmmgr_handler) nmmgr_list_t
Name handler list type.
unsigned short uint16
16-bit unsigned integer
Definition types.h:34
unsigned long long uint64
64-bit unsigned integer
Definition types.h:32
unsigned long uint32
32-bit unsigned integer
Definition types.h:33
unsigned char uint8
8-bit unsigned integer
Definition types.h:35
Definitions for the Internet address family.
Structure used to store an IPv6 address.
Definition in.h:57
IPv4 Packet header.
Definition net.h:245
uint16 length
Length.
Definition net.h:248
uint8 tos
Type of Service.
Definition net.h:247
uint16 checksum
IP checksum.
Definition net.h:253
uint32 dest
Destination IP address.
Definition net.h:255
uint32 src
Source IP address.
Definition net.h:254
uint16 flags_frag_offs
Flags and fragment offset.
Definition net.h:250
uint8 ttl
Time to live.
Definition net.h:251
uint16 packet_id
Packet ID.
Definition net.h:249
uint8 protocol
IP protocol.
Definition net.h:252
uint8 version_ihl
IP version and header length.
Definition net.h:246
IPv6 Packet header.
Definition net.h:267
uint16 length
Length.
Definition net.h:273
uint8 next_header
Next header type.
Definition net.h:274
uint16 lclass
Low-order class byte.
Definition net.h:272
uint8 hop_limit
Hop limit.
Definition net.h:275
uint8 hclass_lflow
High-order class byte, low-order flow byte.
Definition net.h:270
uint8 version_lclass
Version and low-order class byte.
Definition net.h:268
IPv4 statistics structure.
Definition net.h:514
uint32 pkt_sent
Definition net.h:515
uint32 pkt_recv
Packets that failed to send.
Definition net.h:517
uint32 pkt_send_failed
Packets sent out successfully.
Definition net.h:516
uint32 pkt_recv_bad_proto
Packets with a bad checksum.
Definition net.h:520
uint32 pkt_recv_bad_size
Packets received successfully.
Definition net.h:518
uint32 pkt_recv_bad_chksum
Packets of a bad size.
Definition net.h:519
IPv6 statistics structure.
Definition net.h:715
uint32 pkt_recv
Packets received successfully.
Definition net.h:718
uint32 pkt_recv_bad_ext
Packets with an unknown hdr.
Definition net.h:721
uint32 pkt_sent
Packets sent out successfully.
Definition net.h:716
uint32 pkt_send_failed
Packets that failed to send.
Definition net.h:717
uint32 pkt_recv_bad_size
Packets of a bad size.
Definition net.h:719
uint32 pkt_recv_bad_proto
Packets with an unknown proto.
Definition net.h:720
UDP statistics structure.
Definition net.h:797
uint32 pkt_recv_bad_size
Packets of a bad size.
Definition net.h:801
uint32 pkt_send_failed
Packets that failed to send.
Definition net.h:799
uint32 pkt_sent
Packets sent out successfully.
Definition net.h:798
uint32 pkt_recv_no_sock
Packets with to a closed port.
Definition net.h:803
uint32 pkt_recv_bad_chksum
Packets with a bad checksum.
Definition net.h:802
uint32 pkt_recv
Packets received successfully.
Definition net.h:800
Structure describing one usable network device.
Definition net.h:53
uint32 mtu6
Default MTU over IPv6.
Definition net.h:107
int ip6_addr_count
Definition net.h:101
int index
Unit index (starts at zero and counts upwards for multiple network devices of the same type)
Definition net.h:65
struct in6_addr * ip6_addrs
Any further IPv6 addresses the device has.
Definition net.h:100
int mtu
The device's MTU.
Definition net.h:92
uint32 dev_id
Internal device ID (for whatever the driver wants)
Definition net.h:68
LIST_ENTRY(knetif) if_list
Device list handle (not a function!)
const char * descr
Long description of the device.
Definition net.h:61
int hop_limit
Default hop limit over IPv6.
Definition net.h:110
const char * name
Device name ("bba", "la", etc)
Definition net.h:58
uint32 flags
Interface flags.
Definition net.h:71
Common integer types.