/** * Determine if an address is a broadcast address on a network interface * * @param addr address to be checked * @param netif the network interface against which the address is checked * @return returns non-zero if the address is a broadcast address */ u8_t ip4_addr_isbroadcast_u32(u32_t addr, const struct netif *netif) { ip4_addr_t ipaddr; ip4_addr_set_u32(&ipaddr, addr); /* all ones (broadcast) or all zeroes (old skool broadcast) */ if ((~addr == IPADDR_ANY) || (addr == IPADDR_ANY)) { return 1; /* no broadcast support on this network interface? */ } else if ((netif->flags & NETIF_FLAG_BROADCAST) == 0) { /* the given address cannot be a broadcast address * nor can we check against any broadcast addresses */ return 0; /* address matches network interface address exactly? => no broadcast */ } else if (addr == ip4_addr_get_u32(netif_ip4_addr(netif))) { return 0; /* on the same (sub) network... */ } else if (ip4_addr_netcmp(&ipaddr, netif_ip4_addr(netif), netif_ip4_netmask(netif)) /* ...and host identifier bits are all ones? =>... */ && ((addr & ~ip4_addr_get_u32(netif_ip4_netmask(netif))) == (IPADDR_BROADCAST & ~ip4_addr_get_u32(netif_ip4_netmask(netif))))) { /* => network broadcast address */ return 1; } else { return 0; } }
/** * Change the netmask of a network interface * * @param netif the network interface to change * @param netmask the new netmask * * @note call netif_set_addr() if you also want to change ip address and * default gateway */ void netif_set_netmask(struct netif *netif, const ip4_addr_t *netmask) { mib2_remove_route_ip4(0, netif); /* set new netmask to netif */ ip4_addr_set(ip_2_ip4(&netif->netmask), netmask); IP_SET_TYPE_VAL(netif->netmask, IPADDR_TYPE_V4); mib2_add_route_ip4(0, netif); LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("netif: netmask of interface %c%c set to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n", netif->name[0], netif->name[1], ip4_addr1_16(netif_ip4_netmask(netif)), ip4_addr2_16(netif_ip4_netmask(netif)), ip4_addr3_16(netif_ip4_netmask(netif)), ip4_addr4_16(netif_ip4_netmask(netif)))); }
char *LWIP::Interface::get_netmask(char *buf, nsapi_size_t buflen) { #if LWIP_IPV4 const ip4_addr_t *addr = netif_ip4_netmask(&netif); if (!ip4_addr_isany(addr)) { return ip4addr_ntoa_r(addr, buf, buflen); } else { return NULL; } #else return NULL; #endif }
/** Common code to see if the current input packet matches the pcb * (current input packet is accessed via ip(4/6)_current_* macros) * * @param pcb pcb to check * @param inp network interface on which the datagram was received (only used for IPv4) * @param broadcast 1 if his is an IPv4 broadcast (global or subnet-only), 0 otherwise (only used for IPv4) * @return 1 on match, 0 otherwise */ static u8_t ESP_IRAM_ATTR udp_input_local_match(struct udp_pcb *pcb, struct netif *inp, u8_t broadcast) { LWIP_UNUSED_ARG(inp); /* in IPv6 only case */ LWIP_UNUSED_ARG(broadcast); /* in IPv6 only case */ /* Dual-stack: PCBs listening to any IP type also listen to any IP address */ if(IP_IS_ANY_TYPE_VAL(pcb->local_ip)) { #if LWIP_IPV4 && IP_SOF_BROADCAST_RECV if((broadcast != 0) && !ip_get_option(pcb, SOF_BROADCAST)) { return 0; } #endif /* LWIP_IPV4 && IP_SOF_BROADCAST_RECV */ return 1; } /* Only need to check PCB if incoming IP version matches PCB IP version */ if(IP_ADDR_PCB_VERSION_MATCH_EXACT(pcb, ip_current_dest_addr())) { LWIP_ASSERT("UDP PCB: inconsistent local/remote IP versions", IP_IS_V6_VAL(pcb->local_ip) == IP_IS_V6_VAL(pcb->remote_ip)); #if LWIP_IPV4 /* Special case: IPv4 broadcast: all or broadcasts in my subnet * Note: broadcast variable can only be 1 if it is an IPv4 broadcast */ if(broadcast != 0) { #if IP_SOF_BROADCAST_RECV if(ip_get_option(pcb, SOF_BROADCAST)) #endif /* IP_SOF_BROADCAST_RECV */ { if(ip4_addr_isany(ip_2_ip4(&pcb->local_ip)) || ((ip4_current_dest_addr()->addr == IPADDR_BROADCAST)) || ip4_addr_netcmp(ip_2_ip4(&pcb->local_ip), ip4_current_dest_addr(), netif_ip4_netmask(inp))) { return 1; } } } else #endif /* LWIP_IPV4 */ /* Handle IPv4 and IPv6: all, multicast or exact match */ if(ip_addr_isany(&pcb->local_ip) || #if LWIP_IPV6_MLD (ip_current_is_v6() && ip6_addr_ismulticast(ip6_current_dest_addr())) || #endif /* LWIP_IPV6_MLD */ #if LWIP_IGMP (!ip_current_is_v6() && ip4_addr_ismulticast(ip4_current_dest_addr())) || #endif /* LWIP_IGMP */ ip_addr_cmp(&pcb->local_ip, ip_current_dest_addr())) { return 1; } } return 0; }
static void netif_status_callback(struct netif *nif) { printf("NETIF: %c%c%d is %s\n", nif->name[0], nif->name[1], nif->num, netif_is_up(nif) ? "UP" : "DOWN"); #if LWIP_IPV4 printf("IPV4: Host at %s ", ip4addr_ntoa(netif_ip4_addr(nif))); printf("mask %s ", ip4addr_ntoa(netif_ip4_netmask(nif))); printf("gateway %s\n", ip4addr_ntoa(netif_ip4_gw(nif))); #endif /* LWIP_IPV4 */ #if LWIP_IPV6 printf("IPV6: Host at %s\n", ip6addr_ntoa(netif_ip6_addr(nif, 0))); #endif /* LWIP_IPV6 */ #if LWIP_NETIF_HOSTNAME printf("FQDN: %s\n", netif_get_hostname(nif)); #endif /* LWIP_NETIF_HOSTNAME */ }
static snmp_err_t ip_RouteTable_get_next_cell_instance_and_value(const u32_t* column, struct snmp_obj_id* row_oid, union snmp_variant_value* value, u32_t* value_len) { struct netif *netif; struct snmp_next_oid_state state; u32_t result_temp[LWIP_ARRAYSIZE(ip_RouteTable_oid_ranges)]; u32_t test_oid[LWIP_ARRAYSIZE(ip_RouteTable_oid_ranges)]; /* init struct to search next oid */ snmp_next_oid_init(&state, row_oid->id, row_oid->len, result_temp, LWIP_ARRAYSIZE(ip_RouteTable_oid_ranges)); /* check default route */ if (netif_default != NULL) { snmp_ip4_to_oid(IP4_ADDR_ANY4, &test_oid[0]); snmp_next_oid_check(&state, test_oid, LWIP_ARRAYSIZE(ip_RouteTable_oid_ranges), netif_default); } /* iterate over all possible OIDs to find the next one */ netif = netif_list; while (netif != NULL) { ip4_addr_t dst; ip4_addr_get_network(&dst, netif_ip4_addr(netif), netif_ip4_netmask(netif)); /* check generated OID: is it a candidate for the next one? */ if (!ip4_addr_isany_val(dst)) { snmp_ip4_to_oid(&dst, &test_oid[0]); snmp_next_oid_check(&state, test_oid, LWIP_ARRAYSIZE(ip_RouteTable_oid_ranges), netif); } netif = netif->next; } /* did we find a next one? */ if (state.status == SNMP_NEXT_OID_STATUS_SUCCESS) { ip4_addr_t dst; snmp_oid_to_ip4(&result_temp[0], &dst); snmp_oid_assign(row_oid, state.next_oid, state.next_oid_len); /* fill in object properties */ return ip_RouteTable_get_cell_value_core((struct netif*)state.reference, ip4_addr_isany_val(dst), column, value, value_len); } else { /* not found */ return SNMP_ERR_NOSUCHINSTANCE; } }
/*-----------------------------------------------------------------------------------*/ err_t delif_init_thread(struct netif *netif) { struct delif *del; LWIP_DEBUGF(DELIF_DEBUG, ("delif_init_thread\n")); del = (struct delif*)malloc(sizeof(struct delif)); if (!del) { return ERR_MEM; } netif->state = del; netif->name[0] = 'd'; netif->name[1] = 'e'; del->netif = (struct netif*)malloc(sizeof(struct netif)); if (!del->netif) { free(del); return ERR_MEM; } #if LWIP_IPV4 netif->output = delif_output4; netif_set_addr(del->netif, netif_ip4_addr(netif), netif_ip4_netmask(netif), netif_ip4_gw(netif)); #endif /* LWIP_IPV4 */ #if LWIP_IPV6 { s8_t i; netif->output_ip6 = delif_output6; for(i=0; i < LWIP_IPV6_NUM_ADDRESSES; i++) { netif_ip6_addr_set(del->netif, i, netif_ip6_addr(netif, i)); } } #endif /* LWIP_IPV6 */ del->input = netif->input; del->netif->input = delif_input; sys_thread_new("delif_thread", delif_thread, netif, DEFAULT_THREAD_STACKSIZE, DEFAULT_THREAD_PRIO); return ERR_OK; }
static snmp_err_t ip_AddrTable_get_cell_value_core(struct netif *netif, const u32_t* column, union snmp_variant_value* value, u32_t* value_len) { LWIP_UNUSED_ARG(value_len); switch (*column) { case 1: /* ipAdEntAddr */ value->u32 = netif_ip4_addr(netif)->addr; break; case 2: /* ipAdEntIfIndex */ value->u32 = netif_to_num(netif); break; case 3: /* ipAdEntNetMask */ value->u32 = netif_ip4_netmask(netif)->addr; break; case 4: /* ipAdEntBcastAddr */ /* lwIP oddity, there's no broadcast address in the netif we can rely on */ value->u32 = IPADDR_BROADCAST & 1; break; case 5: /* ipAdEntReasmMaxSize */ #if IP_REASSEMBLY /* @todo The theoretical maximum is IP_REASS_MAX_PBUFS * size of the pbufs, * but only if receiving one fragmented packet at a time. * The current solution is to calculate for 2 simultaneous packets... */ value->u32 = (IP_HLEN + ((IP_REASS_MAX_PBUFS/2) * (PBUF_POOL_BUFSIZE - PBUF_LINK_ENCAPSULATION_HLEN - PBUF_LINK_HLEN - IP_HLEN))); #else /** @todo returning MTU would be a bad thing and returning a wild guess like '576' isn't good either */ value->u32 = 0; #endif break; default: return SNMP_ERR_NOSUCHINSTANCE; } return SNMP_ERR_NOERROR; }
static snmp_err_t ip_RouteTable_get_cell_value(const u32_t* column, const u32_t* row_oid, u8_t row_oid_len, union snmp_variant_value* value, u32_t* value_len) { ip4_addr_t test_ip; struct netif *netif; /* check if incoming OID length and if values are in plausible range */ if (!snmp_oid_in_range(row_oid, row_oid_len, ip_RouteTable_oid_ranges, LWIP_ARRAYSIZE(ip_RouteTable_oid_ranges))) { return SNMP_ERR_NOSUCHINSTANCE; } /* get IP and port from incoming OID */ snmp_oid_to_ip4(&row_oid[0], &test_ip); /* we know it succeeds because of oid_in_range check above */ /* default route is on default netif */ if (ip4_addr_isany_val(test_ip) && (netif_default != NULL)) { /* fill in object properties */ return ip_RouteTable_get_cell_value_core(netif_default, 1, column, value, value_len); } /* find netif with requested route */ netif = netif_list; while (netif != NULL) { ip4_addr_t dst; ip4_addr_get_network(&dst, netif_ip4_addr(netif), netif_ip4_netmask(netif)); if (ip4_addr_cmp(&dst, &test_ip)) { /* fill in object properties */ return ip_RouteTable_get_cell_value_core(netif, 0, column, value, value_len); } netif = netif->next; } /* not found */ return SNMP_ERR_NOSUCHINSTANCE; }
static snmp_err_t ip_RouteTable_get_cell_value_core(struct netif *netif, u8_t default_route, const u32_t* column, union snmp_variant_value* value, u32_t* value_len) { switch (*column) { case 1: /* ipRouteDest */ if (default_route) { /* default rte has 0.0.0.0 dest */ value->u32 = IP4_ADDR_ANY4->addr; } else { /* netifs have netaddress dest */ ip4_addr_t tmp; ip4_addr_get_network(&tmp, netif_ip4_addr(netif), netif_ip4_netmask(netif)); value->u32 = tmp.addr; } break; case 2: /* ipRouteIfIndex */ value->u32 = netif_to_num(netif); break; case 3: /* ipRouteMetric1 */ if (default_route) { value->s32 = 1; /* default */ } else { value->s32 = 0; /* normal */ } break; case 4: /* ipRouteMetric2 */ case 5: /* ipRouteMetric3 */ case 6: /* ipRouteMetric4 */ value->s32 = -1; /* none */ break; case 7: /* ipRouteNextHop */ if (default_route) { /* default rte: gateway */ value->u32 = netif_ip4_gw(netif)->addr; } else { /* other rtes: netif ip_addr */ value->u32 = netif_ip4_addr(netif)->addr; } break; case 8: /* ipRouteType */ if (default_route) { /* default rte is indirect */ value->u32 = 4; /* indirect */ } else { /* other rtes are direct */ value->u32 = 3; /* direct */ } break; case 9: /* ipRouteProto */ /* locally defined routes */ value->u32 = 2; /* local */ break; case 10: /* ipRouteAge */ /* @todo (sysuptime - timestamp last change) / 100 */ value->u32 = 0; break; case 11: /* ipRouteMask */ if (default_route) { /* default rte use 0.0.0.0 mask */ value->u32 = IP4_ADDR_ANY4->addr; } else { /* other rtes use netmask */ value->u32 = netif_ip4_netmask(netif)->addr; } break; case 12: /* ipRouteMetric5 */ value->s32 = -1; /* none */ break; case 13: /* ipRouteInfo */ value->const_ptr = snmp_zero_dot_zero.id; *value_len = snmp_zero_dot_zero.len * sizeof(u32_t); break; default: return SNMP_ERR_NOSUCHINSTANCE; } return SNMP_ERR_NOERROR; }
static void ppp_link_status_cb(ppp_pcb *pcb, int err_code, void *ctx) { struct netif *pppif = ppp_netif(pcb); LWIP_UNUSED_ARG(ctx); switch(err_code) { case PPPERR_NONE: /* No error. */ { #if LWIP_DNS ip_addr_t ns; #endif /* LWIP_DNS */ fprintf(stderr, "ppp_link_status_cb: PPPERR_NONE\n\r"); #if LWIP_IPV4 fprintf(stderr, " our_ip4addr = %s\n\r", ip4addr_ntoa(netif_ip4_addr(pppif))); fprintf(stderr, " his_ipaddr = %s\n\r", ip4addr_ntoa(netif_ip4_gw(pppif))); fprintf(stderr, " netmask = %s\n\r", ip4addr_ntoa(netif_ip4_netmask(pppif))); #endif /* LWIP_IPV4 */ #if LWIP_IPV6 fprintf(stderr, " our_ip6addr = %s\n\r", ip6addr_ntoa(netif_ip6_addr(pppif, 0))); #endif /* LWIP_IPV6 */ #if LWIP_DNS ns = dns_getserver(0); fprintf(stderr, " dns1 = %s\n\r", ipaddr_ntoa(&ns)); ns = dns_getserver(1); fprintf(stderr, " dns2 = %s\n\r", ipaddr_ntoa(&ns)); #endif /* LWIP_DNS */ #if PPP_IPV6_SUPPORT fprintf(stderr, " our6_ipaddr = %s\n\r", ip6addr_ntoa(netif_ip6_addr(pppif, 0))); #endif /* PPP_IPV6_SUPPORT */ } break; case PPPERR_PARAM: /* Invalid parameter. */ printf("ppp_link_status_cb: PPPERR_PARAM\n"); break; case PPPERR_OPEN: /* Unable to open PPP session. */ printf("ppp_link_status_cb: PPPERR_OPEN\n"); break; case PPPERR_DEVICE: /* Invalid I/O device for PPP. */ printf("ppp_link_status_cb: PPPERR_DEVICE\n"); break; case PPPERR_ALLOC: /* Unable to allocate resources. */ printf("ppp_link_status_cb: PPPERR_ALLOC\n"); break; case PPPERR_USER: /* User interrupt. */ printf("ppp_link_status_cb: PPPERR_USER\n"); break; case PPPERR_CONNECT: /* Connection lost. */ printf("ppp_link_status_cb: PPPERR_CONNECT\n"); break; case PPPERR_AUTHFAIL: /* Failed authentication challenge. */ printf("ppp_link_status_cb: PPPERR_AUTHFAIL\n"); break; case PPPERR_PROTOCOL: /* Failed to meet protocol. */ printf("ppp_link_status_cb: PPPERR_PROTOCOL\n"); break; case PPPERR_PEERDEAD: /* Connection timeout. */ printf("ppp_link_status_cb: PPPERR_PEERDEAD\n"); break; case PPPERR_IDLETIMEOUT: /* Idle Timeout. */ printf("ppp_link_status_cb: PPPERR_IDLETIMEOUT\n"); break; case PPPERR_CONNECTTIME: /* PPPERR_CONNECTTIME. */ printf("ppp_link_status_cb: PPPERR_CONNECTTIME\n"); break; case PPPERR_LOOPBACK: /* Connection timeout. */ printf("ppp_link_status_cb: PPPERR_LOOPBACK\n"); break; default: printf("ppp_link_status_cb: unknown errCode %d\n", err_code); break; } }
static void pppLinkStatusCallback(ppp_pcb *pcb, int errCode, void *ctx) { struct netif *pppif = ppp_netif(pcb); LWIP_UNUSED_ARG(ctx); switch(errCode) { case PPPERR_NONE: { /* No error. */ printf("pppLinkStatusCallback: PPPERR_NONE\n"); #if LWIP_IPV4 printf(" our_ipaddr = %s\n", ip4addr_ntoa(netif_ip4_addr(pppif))); printf(" his_ipaddr = %s\n", ip4addr_ntoa(netif_ip4_gw(pppif))); printf(" netmask = %s\n", ip4addr_ntoa(netif_ip4_netmask(pppif))); #endif /* LWIP_IPV4 */ #if LWIP_DNS printf(" dns1 = %s\n", ipaddr_ntoa(dns_getserver(0))); printf(" dns2 = %s\n", ipaddr_ntoa(dns_getserver(1))); #endif /* LWIP_DNS */ #if PPP_IPV6_SUPPORT printf(" our6_ipaddr = %s\n", ip6addr_ntoa(netif_ip6_addr(pppif, 0))); #endif /* PPP_IPV6_SUPPORT */ break; } case PPPERR_PARAM: { /* Invalid parameter. */ printf("pppLinkStatusCallback: PPPERR_PARAM\n"); break; } case PPPERR_OPEN: { /* Unable to open PPP session. */ printf("pppLinkStatusCallback: PPPERR_OPEN\n"); break; } case PPPERR_DEVICE: { /* Invalid I/O device for PPP. */ printf("pppLinkStatusCallback: PPPERR_DEVICE\n"); break; } case PPPERR_ALLOC: { /* Unable to allocate resources. */ printf("pppLinkStatusCallback: PPPERR_ALLOC\n"); break; } case PPPERR_USER: { /* User interrupt. */ printf("pppLinkStatusCallback: PPPERR_USER\n"); break; } case PPPERR_CONNECT: { /* Connection lost. */ printf("pppLinkStatusCallback: PPPERR_CONNECT\n"); break; } case PPPERR_AUTHFAIL: { /* Failed authentication challenge. */ printf("pppLinkStatusCallback: PPPERR_AUTHFAIL\n"); break; } case PPPERR_PROTOCOL: { /* Failed to meet protocol. */ printf("pppLinkStatusCallback: PPPERR_PROTOCOL\n"); break; } case PPPERR_PEERDEAD: { /* Connection timeout */ printf("pppLinkStatusCallback: PPPERR_PEERDEAD\n"); break; } case PPPERR_IDLETIMEOUT: { /* Idle Timeout */ printf("pppLinkStatusCallback: PPPERR_IDLETIMEOUT\n"); break; } case PPPERR_CONNECTTIME: { /* Max connect time reached */ printf("pppLinkStatusCallback: PPPERR_CONNECTTIME\n"); break; } case PPPERR_LOOPBACK: { /* Loopback detected */ printf("pppLinkStatusCallback: PPPERR_LOOPBACK\n"); break; } default: { printf("pppLinkStatusCallback: unknown errCode %d\n", errCode); break; } } }