rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2014 Chris Anderson |
| 3 | * Copyright (c) 2014 Brian Swetland |
| 4 | * |
| 5 | * Permission is hereby granted, free of charge, to any person obtaining |
| 6 | * a copy of this software and associated documentation files |
| 7 | * (the "Software"), to deal in the Software without restriction, |
| 8 | * including without limitation the rights to use, copy, modify, merge, |
| 9 | * publish, distribute, sublicense, and/or sell copies of the Software, |
| 10 | * and to permit persons to whom the Software is furnished to do so, |
| 11 | * subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be |
| 14 | * included in all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 19 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 20 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 21 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 22 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 23 | */ |
| 24 | #pragma once |
| 25 | |
| 26 | #include <lib/minip.h> |
| 27 | |
| 28 | #include <compiler.h> |
| 29 | #include <endian.h> |
| 30 | #include <list.h> |
| 31 | #include <stdint.h> |
| 32 | #include <string.h> |
| 33 | |
| 34 | /* Lib configuration */ |
| 35 | #define MINIP_USE_UDP_CHECKSUM 0 |
| 36 | #define MINIP_MTU_SIZE 1536 |
| 37 | #define MINIP_USE_ARP 1 |
| 38 | |
| 39 | #pragma pack(push, 1) |
| 40 | struct arp_pkt { |
| 41 | uint16_t htype; |
| 42 | uint16_t ptype; |
| 43 | uint8_t hlen; |
| 44 | uint8_t plen; |
| 45 | uint16_t oper; |
| 46 | uint8_t sha[6]; |
| 47 | uint32_t spa; |
| 48 | uint8_t tha[6]; |
| 49 | uint32_t tpa; |
| 50 | }; |
| 51 | |
| 52 | struct ipv4_hdr { |
| 53 | uint8_t ver_ihl; |
| 54 | uint8_t dscp_ecn; |
| 55 | uint16_t len; |
| 56 | uint16_t id; |
| 57 | uint16_t flags_frags; |
| 58 | uint8_t ttl; |
| 59 | uint8_t proto; |
| 60 | uint16_t chksum; |
| 61 | uint32_t src_addr; |
| 62 | uint32_t dst_addr; |
| 63 | uint8_t data[]; |
| 64 | }; |
| 65 | |
| 66 | struct icmp_pkt { |
| 67 | uint8_t type; |
| 68 | uint8_t code; |
| 69 | uint16_t chksum; |
| 70 | uint8_t hdr_data[4]; |
| 71 | uint8_t data[]; |
| 72 | }; |
| 73 | |
| 74 | struct eth_hdr { |
| 75 | uint8_t dst_mac[6]; |
| 76 | uint8_t src_mac[6]; |
| 77 | uint16_t type; |
| 78 | }; |
| 79 | |
| 80 | #pragma pack(pop) |
| 81 | |
| 82 | enum { |
| 83 | ICMP_ECHO_REPLY = 0, |
| 84 | ICMP_ECHO_REQUEST = 8, |
| 85 | }; |
| 86 | |
| 87 | enum { |
| 88 | IP_PROTO_ICMP = 0x1, |
| 89 | IP_PROTO_TCP = 0x6, |
| 90 | IP_PROTO_UDP = 0x11, |
| 91 | }; |
| 92 | |
| 93 | enum { |
| 94 | ETH_TYPE_IPV4 = 0x0800, |
| 95 | ETH_TYPE_ARP = 0x0806, |
| 96 | }; |
| 97 | |
| 98 | enum { |
| 99 | ARP_OPER_REQUEST = 0x0001, |
| 100 | ARP_OPER_REPLY = 0x0002, |
| 101 | }; |
| 102 | |
| 103 | extern tx_func_t minip_tx_handler; |
| 104 | typedef struct udp_hdr udp_hdr_t; |
| 105 | static const uint8_t bcast_mac[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; |
| 106 | |
| 107 | void arp_cache_init(void); |
| 108 | void arp_cache_update(uint32_t addr, const uint8_t mac[6]); |
| 109 | uint8_t *arp_cache_lookup(uint32_t addr); |
| 110 | void arp_cache_dump(void); |
| 111 | int arp_send_request(uint32_t addr); |
| 112 | const uint8_t *arp_get_dest_mac(uint32_t host); |
| 113 | |
| 114 | uint16_t rfc1701_chksum(const uint8_t *buf, size_t len); |
| 115 | uint16_t rfc768_chksum(struct ipv4_hdr *ipv4, udp_hdr_t *udp); |
| 116 | uint16_t ones_sum16(uint32_t sum, const void *_buf, int len); |
| 117 | |
| 118 | /* Helper methods for building headers */ |
| 119 | void minip_build_mac_hdr(struct eth_hdr *pkt, const uint8_t *dst, uint16_t type); |
| 120 | void minip_build_ipv4_hdr(struct ipv4_hdr *ipv4, uint32_t dst, uint8_t proto, uint16_t len); |
| 121 | |
| 122 | status_t minip_ipv4_send(pktbuf_t *p, uint32_t dest_addr, uint8_t proto); |
| 123 | |
| 124 | void tcp_input(pktbuf_t *p, uint32_t src_ip, uint32_t dst_ip); |
| 125 | void udp_input(pktbuf_t *p, uint32_t src_ip); |
| 126 | |
| 127 | const uint8_t *get_dest_mac(uint32_t host); |
| 128 | |
| 129 | // timers |
| 130 | typedef void (*net_timer_callback_t)(void *); |
| 131 | |
| 132 | typedef struct net_timer { |
| 133 | struct list_node node; |
| 134 | |
| 135 | lk_time_t sched_time; |
| 136 | |
| 137 | net_timer_callback_t cb; |
| 138 | void *arg; |
| 139 | } net_timer_t; |
| 140 | |
| 141 | /* set a net timer. returns true if the timer was not set before and is now */ |
| 142 | bool net_timer_set(net_timer_t *, net_timer_callback_t, void *callback_args, lk_time_t delay) __NONNULL((1)); |
| 143 | |
| 144 | /* cancels a net timer. returns true if it was previously set and is not now */ |
| 145 | bool net_timer_cancel(net_timer_t *) __NONNULL(); |
| 146 | |
| 147 | void net_timer_init(void); |
| 148 | |
| 149 | static inline void mac_addr_copy(uint8_t *dest, const uint8_t *src) { |
| 150 | *(uint32_t *)dest = *(const uint32_t *)src; |
| 151 | *(uint16_t *)(dest + 4) = *(const uint16_t *)(src + 4); |
| 152 | } |
| 153 | |
| 154 | // vim: set ts=4 sw=4 expandtab: |
| 155 | |