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