net.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* Minimal network stack: ARP + ICMP echo (ping) over a netdev NIC.
  2. Parsing, decoding, reply generation, and the card's IPv4 address. */
  3. #ifndef NET_H
  4. #define NET_H
  5. /* Ethernet ethertypes. */
  6. #define ET_IPV4 0x0800
  7. #define ET_ARP 0x0806
  8. #define ET_IPV6 0x86DD
  9. /* IPv4 protocol numbers. */
  10. #define IPPROTO_ICMP 1
  11. #define IPPROTO_UDP 17
  12. #define IPPROTO_TCP 6
  13. /* ICMP message types. */
  14. #define ICMP_ECHO_REPLY 0
  15. #define ICMP_ECHO_REQUEST 8
  16. void net_set_ip(const unsigned char ip[4]);
  17. void net_get_ip(unsigned char ip[4]);
  18. int net_parse_ip(const char *s, unsigned char ip[4]); /* 0 on success, -1 on error */
  19. void net_print_ip(void);
  20. /* Full configuration (mask/gateway stay zero when unknown). net_set_ip()
  21. * keeps the manual override semantics and clears mask/gateway; DHCP uses
  22. * net_configure(). */
  23. void net_configure(const unsigned char ip[4], const unsigned char mask[4],
  24. const unsigned char gw[4]);
  25. void net_get_mask(unsigned char mask[4]);
  26. void net_get_gateway(unsigned char gw[4]);
  27. int net_is_configured(void); /* ip != 0.0.0.0 */
  28. /* DNS resolver address (DHCP option 6 or the `netdns` command); zero when
  29. * unknown. Kept across net_set_ip()/net_configure() so a manual setting
  30. * survives a dhcp run without option 6. */
  31. void net_get_dns(unsigned char dns[4]);
  32. void net_set_dns(const unsigned char dns[4]);
  33. /* Next hop towards dst: the gateway when dst is off-subnet and a gateway is
  34. * known, else dst itself. */
  35. void net_next_hop(const unsigned char dst[4], unsigned char hop[4]);
  36. /* Resolve dst_ip's MAC by ARP (3 tries, 1 s apart), using a small cache.
  37. * Returns 0 on success, -1 when no reply arrived, -2 on transmit error.
  38. * A raw owner (netdev_acquire) polls the NIC directly; stack users are driven
  39. * by net_pump() and must not hold the raw lease. */
  40. int net_arp_resolve(const unsigned char *dst_ip, unsigned char mac[6]);
  41. /* Feed one received frame to the ARP cache. Returns 1 when it was an ARP
  42. * reply (consumed), 0 otherwise. Called by net_pump(). */
  43. int net_arp_input(const unsigned char *frame, unsigned int len);
  44. const char *net_ethertype_name(unsigned short et);
  45. /* Internet checksum (16-bit one's complement, network byte order). */
  46. unsigned short net_checksum(const void *data, unsigned int len);
  47. /* TCP checksum over the IPv4 pseudo-header (src/dst/proto/length) plus the
  48. * TCP header and payload; result is in network byte order. */
  49. unsigned short net_checksum_tcp(const unsigned char *src_ip,
  50. const unsigned char *dst_ip,
  51. const unsigned char *tcp, unsigned int len);
  52. /* Largest UDP payload net_udp_send() accepts. */
  53. #define NET_UDP_MAX 1400
  54. /* Send one UDP datagram (IPv4, checksum disabled) in a single Ethernet frame
  55. * to dst_mac/dst_ip, with the configured source address. Returns 0 on
  56. * success, -1 on error. */
  57. int net_udp_send(const unsigned char *dst_mac, const unsigned char *dst_ip,
  58. unsigned int src_port, unsigned int dst_port,
  59. const void *payload, unsigned int len);
  60. /* Same, but with an explicit source address (DHCP uses 0.0.0.0). */
  61. int net_udp_send_from(const unsigned char *src_ip,
  62. const unsigned char *dst_mac, const unsigned char *dst_ip,
  63. unsigned int src_port, unsigned int dst_port,
  64. const void *payload, unsigned int len);
  65. /* Same, but forced onto a specific NIC (netlog/netkexec management link). */
  66. struct netdev;
  67. int net_udp_send_from_dev(const struct netdev *nd,
  68. const unsigned char *src_ip,
  69. const unsigned char *dst_mac, const unsigned char *dst_ip,
  70. unsigned int src_port, unsigned int dst_port,
  71. const void *payload, unsigned int len);
  72. /* Parse one Ethernet frame as a UDP datagram addressed to us or to the
  73. * broadcast address. Fills the addresses/ports and the payload location.
  74. * Returns 1 on match. */
  75. int net_udp_parse(const unsigned char *frame, unsigned int len,
  76. unsigned char src_ip[4], unsigned char dst_ip[4],
  77. unsigned int *src_port, unsigned int *dst_port,
  78. const unsigned char **payload, unsigned int *plen);
  79. /* Same, but without the destination-IP filter. DHCP uses this because a
  80. * server may send its OFFER unicast to the address it is offering, which is
  81. * not our current (static) IP. The protocol layer still validates the DHCP
  82. * transaction ID and chaddr. */
  83. int net_udp_parse_any(const unsigned char *frame, unsigned int len,
  84. unsigned char src_ip[4], unsigned char dst_ip[4],
  85. unsigned int *src_port, unsigned int *dst_port,
  86. const unsigned char **payload, unsigned int *plen);
  87. /* Print one decoded frame summary line (dst/src/ethertype + ARP/IP/ICMP). */
  88. void net_decode_frame(const unsigned char *frame, unsigned int len);
  89. /* Build a reply to an ARP request or ICMP echo request addressed to us.
  90. Returns reply length (> 0) or 0 if the frame needs no reply. */
  91. int net_build_reply(const unsigned char *frame, unsigned int len,
  92. unsigned char *out, unsigned int maxout);
  93. #endif