Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 | /** @file @brief Network stack private header This is not to be included by the application. */ /* * Copyright (c) 2016 Intel Corporation * * SPDX-License-Identifier: Apache-2.0 */ #include <errno.h> #include <misc/printk.h> #include <net/net_context.h> #include <net/net_pkt.h> #ifdef CONFIG_NET_MGMT_EVENT_INFO #include <net/net_event.h> /* Maximum size of "struct net_event_ipv6_addr" or * "struct net_event_ipv6_nbr" or "struct net_event_ipv6_route". * NOTE: Update comments here and calculate which struct occupies max size. */ #ifdef CONFIG_NET_L2_WIFI_MGMT #include <net/wifi_mgmt.h> #define NET_EVENT_INFO_MAX_SIZE sizeof(struct wifi_scan_result) #else #define NET_EVENT_INFO_MAX_SIZE sizeof(struct net_event_ipv6_route) #endif /* CONFIG_NET_L2_WIFI_MGMT */ #endif /* CONFIG_NET_MGMT_EVENT_INFO */ #include "connection.h" extern void net_pkt_init(void); extern void net_if_init(void); extern void net_if_post_init(void); extern void net_if_carrier_down(struct net_if *iface); extern void net_context_init(void); enum net_verdict net_ipv4_process_pkt(struct net_pkt *pkt); enum net_verdict net_ipv6_process_pkt(struct net_pkt *pkt); extern void net_ipv6_init(void); extern void net_tc_tx_init(void); extern void net_tc_rx_init(void); extern void net_tc_submit_to_tx_queue(u8_t tc, struct net_pkt *pkt); extern void net_tc_submit_to_rx_queue(u8_t tc, struct net_pkt *pkt); extern enum net_verdict net_promisc_mode_input(struct net_pkt *pkt); char *net_sprint_addr(sa_family_t af, const void *addr); #define net_sprint_ipv4_addr(_addr) net_sprint_addr(AF_INET, _addr) #define net_sprint_ipv6_addr(_addr) net_sprint_addr(AF_INET6, _addr) #if defined(CONFIG_NET_GPTP) /** * @brief Initialize Precision Time Protocol Layer. */ void net_gptp_init(void); /** * @brief Process a ptp message. * * @param buf Buffer with a valid PTP Ethernet type. * * @return Return the policy for network buffer. */ enum net_verdict net_gptp_recv(struct net_if *iface, struct net_pkt *pkt); #else #define net_gptp_init() #define net_gptp_recv(iface, pkt) #endif /* CONFIG_NET_GPTP */ #if defined(CONFIG_NET_IPV6_FRAGMENT) int net_ipv6_send_fragmented_pkt(struct net_if *iface, struct net_pkt *pkt, u16_t pkt_len); #endif extern const char *net_proto2str(enum net_ip_protocol proto); extern char *net_byte_to_hex(char *ptr, u8_t byte, char base, bool pad); extern char *net_sprint_ll_addr_buf(const u8_t *ll, u8_t ll_len, char *buf, int buflen); extern u16_t net_calc_chksum(struct net_pkt *pkt, u8_t proto); bool net_header_fits(struct net_pkt *pkt, u8_t *hdr, size_t hdr_size); struct net_icmp_hdr *net_pkt_icmp_data(struct net_pkt *pkt); u8_t *net_pkt_icmp_opt_data(struct net_pkt *pkt, size_t opt_len); /* Check if ICMP header can be directly accessed from memory. * If returned value is NULL, then the header was split into * multiple fragments and user must use net_pkt_read/write() etc to get/set * the ICMP header values. * If returned value is not NULL, then the first fragment will * hold the ICMP header and returned value will point to start of ICMP header * inside net_pkt. */ static inline struct net_icmp_hdr *net_icmp_header_fits(struct net_pkt *pkt, struct net_icmp_hdr *hdr) { if (net_header_fits(pkt, (u8_t *)hdr, sizeof(*hdr))) { return hdr; } return NULL; } /* Header may be split between data fragments. In most cases, * net_udp_get_hdr() should be used instead. */ struct net_udp_hdr *net_pkt_udp_data(struct net_pkt *pkt); static inline struct net_udp_hdr *net_udp_header_fits(struct net_pkt *pkt, struct net_udp_hdr *hdr) { if (net_header_fits(pkt, (u8_t *)hdr, sizeof(*hdr))) { return hdr; } return NULL; } /* Header may be split between data fragments. In most cases, * net_tcp_get_hdr() should be used instead. */ struct net_tcp_hdr *net_pkt_tcp_data(struct net_pkt *pkt); static inline struct net_tcp_hdr *net_tcp_header_fits(struct net_pkt *pkt, struct net_tcp_hdr *hdr) { if (net_header_fits(pkt, (u8_t *)hdr, sizeof(*hdr))) { return hdr; } return NULL; } void net_pkt_set_appdata_values(struct net_pkt *pkt, enum net_ip_protocol proto); enum net_verdict net_context_packet_received(struct net_conn *conn, struct net_pkt *pkt, void *user_data); #if defined(CONFIG_NET_IPV4) extern u16_t net_calc_chksum_ipv4(struct net_pkt *pkt); #endif /* CONFIG_NET_IPV4 */ static inline u16_t net_calc_chksum_icmpv6(struct net_pkt *pkt) { return net_calc_chksum(pkt, IPPROTO_ICMPV6); } static inline u16_t net_calc_chksum_icmpv4(struct net_pkt *pkt) { return net_calc_chksum(pkt, IPPROTO_ICMP); } static inline u16_t net_calc_chksum_udp(struct net_pkt *pkt) { return net_calc_chksum(pkt, IPPROTO_UDP); } static inline u16_t net_calc_chksum_tcp(struct net_pkt *pkt) { return net_calc_chksum(pkt, IPPROTO_TCP); } #if NET_LOG_ENABLED > 0 static inline char *net_sprint_ll_addr(const u8_t *ll, u8_t ll_len) { static char buf[sizeof("xx:xx:xx:xx:xx:xx:xx:xx")]; return net_sprint_ll_addr_buf(ll, ll_len, (char *)buf, sizeof(buf)); } static inline void _hexdump(const u8_t *packet, size_t length, u8_t reserve) { char output[sizeof("xxxxyyyy xxxxyyyy")]; int n = 0, k = 0; u8_t byte; #if defined(CONFIG_SYS_LOG) && (SYS_LOG_LEVEL > SYS_LOG_LEVEL_OFF) u8_t r = reserve; #endif while (length--) { if (n % 16 == 0) { printk(" %08X ", n); } byte = *packet++; #if defined(CONFIG_SYS_LOG) && (SYS_LOG_LEVEL > SYS_LOG_LEVEL_OFF) if (reserve) { if (r) { printk(SYS_LOG_COLOR_YELLOW); r--; } else { printk(SYS_LOG_COLOR_OFF); } } #endif printk("%02X ", byte); if (byte < 0x20 || byte > 0x7f) { output[k++] = '.'; } else { output[k++] = byte; } n++; if (n % 8 == 0) { if (n % 16 == 0) { output[k] = '\0'; printk(" [%s]\n", output); k = 0; } else { printk(" "); } } } if (n % 16) { int i; output[k] = '\0'; for (i = 0; i < (16 - (n % 16)); i++) { printk(" "); } if ((n % 16) < 8) { printk(" "); /* one extra delimiter after 8 chars */ } printk(" [%s]\n", output); } } static inline void net_hexdump(const char *str, const u8_t *packet, size_t length) { if (!length) { SYS_LOG_DBG("%s zero-length packet", str); return; } printk("%s\n", str); _hexdump(packet, length, 0); } /* Hexdump from all fragments * Set full as true to get also the L2 reserve part printed out */ static inline void net_hexdump_frags(const char *str, struct net_pkt *pkt, bool full) { u8_t reserve = full ? net_pkt_ll_reserve(pkt) : 0; struct net_buf *frag = pkt->frags; printk("%s\n", str); while (frag) { _hexdump(full ? frag->data - reserve : frag->data, frag->len + reserve, reserve); frag = frag->frags; if (full && reserve) { reserve -= net_pkt_ll_reserve(pkt); } } } /* Print fragment chain */ static inline void net_print_frags(const char *str, struct net_pkt *pkt) { struct net_buf *frag = pkt->frags; if (str) { printk("%s", str); } printk("%p[%d]", pkt, pkt->ref); if (frag) { printk("->"); } while (frag) { printk("%p[%d/%d]", frag, frag->ref, frag->len); frag = frag->frags; if (frag) { printk("->"); } } printk("\n"); } #else /* NET_LOG_ENABLED */ static inline char *net_sprint_ll_addr(const u8_t *ll, u8_t ll_len) { ARG_UNUSED(ll); ARG_UNUSED(ll_len); return NULL; } #define net_hexdump(...) #define net_hexdump_frags(...) #define net_print_frags(...) #endif /* NET_LOG_ENABLED */ |