From 0de956d53f25741de9c96dd2e682e297782a9148 Mon Sep 17 00:00:00 2001 From: Ravi kumar Veeramally Date: Tue, 24 May 2016 13:22:10 +0300 Subject: [PATCH] net: dhcp: Add DHCP client support. DHCP client support provided for IPv4 over ethernet. Change-Id: Ia89bc4123842cf109813b80a90b70cf50cc52e0a Signed-off-by: Ravi kumar Veeramally --- net/ip/Kconfig | 23 ++ net/ip/Kconfig.debug | 7 + net/ip/Makefile | 2 + net/ip/contiki/ip/dhcpc.c | 495 ++++++++++++++++++++++++++++++++++++++ net/ip/contiki/ip/dhcpc.h | 61 +++++ net/ip/contiki/ip/tcpip.c | 13 + net/ip/contiki/ip/uip.h | 4 + net/ip/contiki/ipv4/uip.c | 44 +++- net/ip/net_core.c | 7 + 9 files changed, 654 insertions(+), 2 deletions(-) create mode 100644 net/ip/contiki/ip/dhcpc.c create mode 100644 net/ip/contiki/ip/dhcpc.h diff --git a/net/ip/Kconfig b/net/ip/Kconfig index 2a63f89bffd..a36528dea1e 100644 --- a/net/ip/Kconfig +++ b/net/ip/Kconfig @@ -521,6 +521,29 @@ config ER_COAP_CLIENT help Compile in client CoAP support. +config DHCP + bool + prompt "Enable DHCP support." + depends on NETWORKING && NETWORKING_WITH_IPV4 + default n + help + Enable DHCP support so that applications can use it. +choice +prompt "DHCP message type" +depends on DHCP +help + Select the DHCP message type broadcast or unicast. +default DHCP_BROADCAST +config DHCP_BROADCAST + bool "Broadcast message" + help + Choose this if unsure. +config DHCP_UNICAST + bool "Unicast message" + help + DHCP message type +endchoice + config NET_SANITY_TEST bool prompt "Enable networking sanity test" diff --git a/net/ip/Kconfig.debug b/net/ip/Kconfig.debug index 411913f2da2..9aaf40c9198 100644 --- a/net/ip/Kconfig.debug +++ b/net/ip/Kconfig.debug @@ -252,4 +252,11 @@ config NETWORK_IP_STACK_DEBUG_REST_ENGINE help Enables REST engine output debug messages +config NETWORK_IP_STACK_DEBUG_DHCP + bool "Debug DHCP" + depends on DHCP + default n + help + Enables DHCP output debug messages + endif diff --git a/net/ip/Makefile b/net/ip/Makefile index 7d1d2170d09..de505dd955b 100644 --- a/net/ip/Makefile +++ b/net/ip/Makefile @@ -178,3 +178,5 @@ obj-$(CONFIG_ER_COAP) += er-coap/er-coap.o \ rest-engine/rest-engine.o obj-$(CONFIG_ER_COAP_CLIENT) += er-coap/er-coap-observe-client.o + +obj-$(CONFIG_DHCP) += contiki/ip/dhcpc.o diff --git a/net/ip/contiki/ip/dhcpc.c b/net/ip/contiki/ip/dhcpc.c new file mode 100644 index 00000000000..d2a8f8add47 --- /dev/null +++ b/net/ip/contiki/ip/dhcpc.c @@ -0,0 +1,495 @@ +/* + * Copyright (c) 2005, Swedish Institute of Computer Science + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + */ + +#include +#include + +#include + +#include "contiki.h" +#include "contiki-net.h" +#include "contiki/ip/dhcpc.h" + +#ifdef CONFIG_NETWORK_IP_STACK_DEBUG_DHCP +#define DEBUG 1 +#endif +#include "contiki/ip/uip-debug.h" + +#define STATE_INITIAL 0 +#define STATE_SENDING 1 +#define STATE_OFFER_RECEIVED 2 +#define STATE_CONFIG_RECEIVED 3 + +static struct dhcpc_state s; +dhcpc_configured configure_cb = NULL; +dhcpc_unconfigured unconfigure_cb = NULL; + +struct dhcp_msg { + uint8_t op, htype, hlen, hops; + uint8_t xid[4]; + uint16_t secs, flags; + uint8_t ciaddr[4]; + uint8_t yiaddr[4]; + uint8_t siaddr[4]; + uint8_t giaddr[4]; + uint8_t chaddr[16]; +#ifndef UIP_CONF_DHCP_LIGHT + uint8_t sname[64]; + uint8_t file[128]; +#endif + uint8_t options[312]; +} __packed; + +#ifdef CONFIG_DHCP_BROADCAST +#define BOOTP_BROADCAST 0x8000 +#elif CONFIG_DHCP_UNICAST +#define BOOTP_UNICAST 0x0000 +#endif + +#define DHCP_REQUEST 1 +#define DHCP_REPLY 2 +#define DHCP_HTYPE_ETHERNET 1 +#define DHCP_HLEN_ETHERNET 6 +#define DHCP_MSG_LEN 236 + +#define DHCPC_SERVER_PORT 67 +#define DHCPC_CLIENT_PORT 68 + +#define DHCPDISCOVER 1 +#define DHCPOFFER 2 +#define DHCPREQUEST 3 +#define DHCPDECLINE 4 +#define DHCPACK 5 +#define DHCPNAK 6 +#define DHCPRELEASE 7 + +#define DHCP_OPTION_SUBNET_MASK 1 +#define DHCP_OPTION_ROUTER 3 +#define DHCP_OPTION_DNS_SERVER 6 +#define DHCP_OPTION_REQ_IPADDR 50 +#define DHCP_OPTION_LEASE_TIME 51 +#define DHCP_OPTION_MSG_TYPE 53 +#define DHCP_OPTION_SERVER_ID 54 +#define DHCP_OPTION_REQ_LIST 55 +#define DHCP_OPTION_END 255 + +static uint32_t xid; +static const uint8_t magic_cookie[4] = {99, 130, 83, 99}; + +/*---------------------------------------------------------------------------*/ +static void +configure_dhcpc(struct dhcpc_state *s) +{ + uip_sethostaddr(&s->ipaddr); + uip_setnetmask(&s->netmask); + uip_setdraddr(&s->default_router); + uip_nameserver_update(&s->dnsaddr, UIP_NAMESERVER_INFINITE_LIFETIME); + + if(configure_cb) + configure_cb(); +} +/*---------------------------------------------------------------------------*/ +static void +unconfigure_dhcpc(struct dhcpc_state *s) +{ + if(unconfigure_cb) + unconfigure_cb(); +} +/*---------------------------------------------------------------------------*/ +static uint8_t * +add_msg_type(uint8_t *optptr, uint8_t type) +{ + *optptr++ = DHCP_OPTION_MSG_TYPE; + *optptr++ = 1; + *optptr++ = type; + return optptr; +} +/*---------------------------------------------------------------------------*/ +static uint8_t * +add_server_id(uint8_t *optptr) +{ + *optptr++ = DHCP_OPTION_SERVER_ID; + *optptr++ = 4; + memcpy(optptr, s.serverid, 4); + return optptr + 4; +} +/*---------------------------------------------------------------------------*/ +static uint8_t * +add_req_ipaddr(uint8_t *optptr) +{ + *optptr++ = DHCP_OPTION_REQ_IPADDR; + *optptr++ = 4; + memcpy(optptr, s.ipaddr.u16, 4); + return optptr + 4; +} +/*---------------------------------------------------------------------------*/ +static uint8_t * +add_req_options(uint8_t *optptr) +{ + *optptr++ = DHCP_OPTION_REQ_LIST; + *optptr++ = 3; + *optptr++ = DHCP_OPTION_SUBNET_MASK; + *optptr++ = DHCP_OPTION_ROUTER; + *optptr++ = DHCP_OPTION_DNS_SERVER; + return optptr; +} +/*---------------------------------------------------------------------------*/ +static uint8_t * +add_end(uint8_t *optptr) +{ + *optptr++ = DHCP_OPTION_END; + return optptr; +} +/*---------------------------------------------------------------------------*/ +static void +create_msg(struct dhcp_msg *m) +{ + m->op = DHCP_REQUEST; + m->htype = DHCP_HTYPE_ETHERNET; + m->hlen = s.mac_len; + m->hops = 0; + memcpy(m->xid, &xid, sizeof(m->xid)); + m->secs = 0; +#ifdef CONFIG_DHCP_BROADCAST + m->flags = UIP_HTONS(BOOTP_BROADCAST); /* Broadcast bit. */ +#elif CONFIG_DHCP_UNICAST + m->flags = UIP_HTONS(BOOTP_UNICAST); /* Unicast bit. */ +#endif + + /* uip_ipaddr_copy(m->ciaddr, uip_hostaddr);*/ + memcpy(m->ciaddr, uip_hostaddr.u16, sizeof(m->ciaddr)); + memset(m->yiaddr, 0, sizeof(m->yiaddr)); + memset(m->siaddr, 0, sizeof(m->siaddr)); + memset(m->giaddr, 0, sizeof(m->giaddr)); + memcpy(m->chaddr, s.mac_addr, s.mac_len); + memset(&m->chaddr[s.mac_len], 0, sizeof(m->chaddr) - s.mac_len); +#ifndef UIP_CONF_DHCP_LIGHT + memset(m->sname, 0, sizeof(m->sname)); + memset(m->file, 0, sizeof(m->file)); +#endif + + memcpy(m->options, magic_cookie, sizeof(magic_cookie)); +} +/*---------------------------------------------------------------------------*/ +static void +send_discover(void) +{ + uint8_t *end; + struct dhcp_msg *m; + struct net_buf *buf; + int len; + + buf = ip_buf_get_reserve_tx(UIP_LLH_LEN + UIP_IPUDPH_LEN); + if(!buf) { + PRINTF("dhcp: failed to get buffer\n"); + return; + } + + uip_set_udp_conn(buf) = s.conn; + m = (struct dhcp_msg *)uip_appdata(buf); + + create_msg(m); + + end = add_msg_type(&m->options[4], DHCPDISCOVER); + end = add_req_options(end); + end = add_end(end); + len = (int)(end - (uint8_t *)uip_appdata(buf)); + uip_appdatalen(buf) = len; + + uip_send_udp(buf, uip_appdata(buf), len); +} +/*---------------------------------------------------------------------------*/ +static void +send_request(void) +{ + uint8_t *end; + struct dhcp_msg *m; + struct net_buf *buf; + int len; + + buf = ip_buf_get_reserve_tx(UIP_LLH_LEN + UIP_IPUDPH_LEN); + if(!buf) { + PRINTF("dhcp: failed to get buffer\n"); + return; + } + + uip_set_udp_conn(buf) = s.conn; + m = (struct dhcp_msg *)uip_appdata(buf); + + create_msg(m); + + end = add_msg_type(&m->options[4], DHCPREQUEST); + end = add_server_id(end); + end = add_req_ipaddr(end); + end = add_end(end); + len = (int)(end - (uint8_t *)uip_appdata(buf)); + uip_appdatalen(buf) = len; + + uip_send_udp(buf, uip_appdata(buf), len); +} +/*---------------------------------------------------------------------------*/ +static uint8_t +parse_options(uint8_t *optptr, int len) +{ + uint8_t *end = optptr + len; + uint8_t type = 0; + + while(optptr < end) { + switch(*optptr) { + case DHCP_OPTION_SUBNET_MASK: + memcpy(s.netmask.u16, optptr + 2, 4); + break; + case DHCP_OPTION_ROUTER: + memcpy(s.default_router.u16, optptr + 2, 4); + break; + case DHCP_OPTION_DNS_SERVER: + memcpy(s.dnsaddr.u16, optptr + 2, 4); + break; + case DHCP_OPTION_MSG_TYPE: + type = *(optptr + 2); + break; + case DHCP_OPTION_SERVER_ID: + memcpy(s.serverid, optptr + 2, 4); + break; + case DHCP_OPTION_LEASE_TIME: + memcpy(s.lease_time, optptr + 2, 4); + break; + case DHCP_OPTION_END: + return type; + } + + optptr += optptr[1] + 2; + } + return type; +} +/*---------------------------------------------------------------------------*/ +static uint8_t +parse_msg(struct net_buf *buf) +{ + struct dhcp_msg *m = (struct dhcp_msg *)uip_appdata(buf); + uint8_t ret = 0; + + if(m->op == DHCP_REPLY && + memcmp(m->xid, &xid, sizeof(xid)) == 0 && + memcmp(m->chaddr, s.mac_addr, s.mac_len) == 0) { + memcpy(s.ipaddr.u16, m->yiaddr, 4); + ret = parse_options(&m->options[4], uip_datalen(buf)); + } + + ip_buf_unref(buf); + return ret; +} +/*---------------------------------------------------------------------------*/ +/* + * Is this a "fresh" reply for me? If it is, return the type. + */ +static int +msg_for_me(struct net_buf *buf) +{ + struct dhcp_msg *m = (struct dhcp_msg *)uip_appdata(buf); + uint8_t *optptr = &m->options[4]; + uint8_t *end = (uint8_t*)uip_appdata(buf) + uip_datalen(buf); + + if(m->op == DHCP_REPLY && + memcmp(m->xid, &xid, sizeof(xid)) == 0 && + memcmp(m->chaddr, s.mac_addr, s.mac_len) == 0) { + while(optptr < end) { + if(*optptr == DHCP_OPTION_MSG_TYPE) { + return *(optptr + 2); + } else if (*optptr == DHCP_OPTION_END) { + return -1; + } + optptr += optptr[1] + 2; + } + } + return -1; +} +/*---------------------------------------------------------------------------*/ +PROCESS(dhcp_process, "DHCP process"); +PROCESS_THREAD(dhcp_process, ev, data, buf, user_data) +{ + clock_time_t ticks; + PROCESS_BEGIN(); + + init: + xid++; + s.state = STATE_SENDING; + s.ticks = CLOCK_SECOND * 8; + while (1) { + send_discover(); + etimer_set(&s.etimer, s.ticks, &dhcp_process); + do { + PROCESS_YIELD(); + if(ev == tcpip_event && uip_newdata(buf) && msg_for_me(buf) == DHCPOFFER) { + parse_msg(buf); + s.state = STATE_OFFER_RECEIVED; + goto selecting; + } + } while (!etimer_expired(&s.etimer)); + + if(s.ticks < CLOCK_SECOND * 60) { + s.ticks *= 2; + } + } + + selecting: + s.ticks = CLOCK_SECOND; + do { + send_request(); + etimer_set(&s.etimer, s.ticks, &dhcp_process); + do { + PROCESS_YIELD(); + if(ev == tcpip_event && uip_newdata(buf) && msg_for_me(buf) == DHCPACK) { + parse_msg(buf); + s.state = STATE_CONFIG_RECEIVED; + goto bound; + } + } while (!etimer_expired(&s.etimer)); + + if(s.ticks <= CLOCK_SECOND * 10) { + s.ticks += CLOCK_SECOND; + } else { + goto init; + } + } while(s.state != STATE_CONFIG_RECEIVED); + + bound: + + PRINTF("Got IP address %d.%d.%d.%d\n", uip_ipaddr_to_quad(&s.ipaddr)); + PRINTF("Got netmask %d.%d.%d.%d\n", uip_ipaddr_to_quad(&s.netmask)); + PRINTF("Got DNS server %d.%d.%d.%d\n", uip_ipaddr_to_quad(&s.dnsaddr)); + PRINTF("Got default router %d.%d.%d.%d\n", + uip_ipaddr_to_quad(&s.default_router)); + PRINTF("Lease expires in %ld seconds\n", + uip_ntohs(s.lease_time[0])*65536ul + uip_ntohs(s.lease_time[1])); + + configure_dhcpc(&s); + +#define MAX_TICKS (~((clock_time_t)0) / 2) +#define MAX_TICKS32 (~((uint32_t)0)) +#define IMIN(a, b) ((a) < (b) ? (a) : (b)) + + if((uip_ntohs(s.lease_time[0])*65536ul + uip_ntohs(s.lease_time[1]))*CLOCK_SECOND/2 + <= MAX_TICKS32) { + s.ticks = (uip_ntohs(s.lease_time[0])*65536ul + uip_ntohs(s.lease_time[1]) + )*CLOCK_SECOND/2; + } else { + s.ticks = MAX_TICKS32; + } + + while(s.ticks > 0) { + ticks = IMIN(s.ticks, MAX_TICKS); + s.ticks -= ticks; + etimer_set(&s.etimer, ticks, &dhcp_process); + PROCESS_YIELD_UNTIL(ev == PROCESS_EVENT_TIMER); + } + + if((uip_ntohs(s.lease_time[0])*65536ul + uip_ntohs(s.lease_time[1]))*CLOCK_SECOND/2 + <= MAX_TICKS32) { + s.ticks = (uip_ntohs(s.lease_time[0])*65536ul + uip_ntohs(s.lease_time[1]) + )*CLOCK_SECOND/2; + } else { + s.ticks = MAX_TICKS32; + } + + /* renewing: */ + do { + send_request(); + ticks = IMIN(s.ticks / 2, MAX_TICKS); + s.ticks -= ticks; + etimer_set(&s.etimer, ticks, &dhcp_process); + do { + PROCESS_YIELD(); + if(ev == tcpip_event && uip_newdata(buf) && msg_for_me(buf) == DHCPACK) { + parse_msg(buf); + goto bound; + } + } while(!etimer_expired(&s.etimer)); + } while(s.ticks >= CLOCK_SECOND*3); + + /* rebinding: */ + + /* lease_expired: */ + unconfigure_dhcpc(&s); + goto init; + + PROCESS_END(); +} +/*---------------------------------------------------------------------------*/ +void +dhcpc_init(const void *mac_addr, int mac_len) +{ + uip_ipaddr_t addr; + uip_ipaddr_t ipaddr; + + s.mac_addr = mac_addr; + s.mac_len = mac_len; + + s.state = STATE_INITIAL; + + uip_ipaddr(&ipaddr, 0,0,0,0); + uip_sethostaddr(&ipaddr); + + uip_ipaddr(&addr, 255,255,255,255); + s.conn = udp_new(&addr, UIP_HTONS(DHCPC_SERVER_PORT), NULL); + if(s.conn != NULL) { + udp_bind(s.conn, UIP_HTONS(DHCPC_CLIENT_PORT)); + } + process_start(&dhcp_process, NULL, NULL); +} + +/*---------------------------------------------------------------------------*/ +void +dhcpc_appcall(process_event_t ev, struct net_buf *buf) +{ + if(ev == tcpip_event || ev == PROCESS_EVENT_TIMER) { + process_post_synch(&dhcp_process, ev, NULL, buf); + } +} +/*---------------------------------------------------------------------------*/ +int msg_for_dhcpc(struct net_buf *buf) +{ + int msg; + + msg = msg_for_me(buf); + if(msg == DHCPOFFER || msg == DHCPACK) + return 1; + + return 0; +} +/*---------------------------------------------------------------------------*/ +void dhcpc_set_callbacks(dhcpc_configured conf, dhcpc_unconfigured unconf) +{ + configure_cb = conf; + unconfigure_cb = unconf; +} +/*---------------------------------------------------------------------------*/ diff --git a/net/ip/contiki/ip/dhcpc.h b/net/ip/contiki/ip/dhcpc.h new file mode 100644 index 00000000000..a34e691fbb9 --- /dev/null +++ b/net/ip/contiki/ip/dhcpc.h @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2005, Swedish Institute of Computer Science + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + */ +#ifndef DHCPC_H_ +#define DHCPC_H_ +typedef void (*dhcpc_configured)(void); +typedef void (*dhcpc_unconfigured)(void); + +struct dhcpc_state { + struct pt pt; + char state; + struct uip_udp_conn *conn; + struct etimer etimer; + uint32_t ticks; + const void *mac_addr; + int mac_len; + + uint8_t serverid[4]; + + uint16_t lease_time[2]; + uip_ipaddr_t ipaddr; + uip_ipaddr_t netmask; + uip_ipaddr_t dnsaddr; + uip_ipaddr_t default_router; +}; + +void dhcpc_init(const void *mac_addr, int mac_len); +void dhcpc_appcall(process_event_t ev, struct net_buf *buf); +int msg_for_dhcpc(struct net_buf *buf); + +void dhcpc_set_callbacks(dhcpc_configured conf, dhcpc_unconfigured unconf); + +#endif /* DHCPC_H_ */ diff --git a/net/ip/contiki/ip/tcpip.c b/net/ip/contiki/ip/tcpip.c index 0a6dc4fed21..10d9033137a 100644 --- a/net/ip/contiki/ip/tcpip.c +++ b/net/ip/contiki/ip/tcpip.c @@ -57,6 +57,10 @@ #endif #include "contiki/ip/uip-debug.h" +#ifdef CONFIG_DHCP +#include "contiki/ip/dhcpc.h" +#endif + #if UIP_LOGGING #include void uip_log(char *msg); @@ -905,6 +909,15 @@ tcpip_uipcall(struct net_buf *buf) } } #endif /* UIP_TCP */ + +#ifdef CONFIG_DHCP + if(msg_for_dhcpc(buf)) { + PRINTF("msg for dhcpc\n"); + dhcpc_appcall(tcpip_event, buf); + return; + } +#endif + if(ts->p != NULL) { process_post_synch(ts->p, tcpip_event, ts->state, buf); } diff --git a/net/ip/contiki/ip/uip.h b/net/ip/contiki/ip/uip.h index cae581bcf99..707df6b8ace 100644 --- a/net/ip/contiki/ip/uip.h +++ b/net/ip/contiki/ip/uip.h @@ -586,6 +586,10 @@ struct uip_conn *uip_connect(const uip_ipaddr_t *ripaddr, uint16_t port); CCIF void uip_send(struct net_buf *buf, const void *data, int len); #endif +#if UIP_UDP +CCIF void uip_send_udp(struct net_buf *buf, const void *data, int len); +#endif + /** * The length of any incoming data that is currently available (if available) * in the uip_appdata buffer. diff --git a/net/ip/contiki/ipv4/uip.c b/net/ip/contiki/ipv4/uip.c index 5ad713fd5ae..6809ba561fa 100644 --- a/net/ip/contiki/ipv4/uip.c +++ b/net/ip/contiki/ipv4/uip.c @@ -85,6 +85,10 @@ #include #include +#ifdef CONFIG_DHCP +#include "contiki/ip/dhcpc.h" +#endif + extern void net_context_set_connection_status(struct net_context *context, int status); void *net_context_get_internal_connection(struct net_context *context); @@ -1036,6 +1040,23 @@ uip_process(struct net_buf **buf_out, uint8_t flag) /* Check if the packet is destined for our IP address. */ #if !NETSTACK_CONF_WITH_IPV6 +#ifdef CONFIG_DHCP + /* DHCP message destination address in DHCP OFFER and ACK + * packets, destination address is 255.255.255.255, so skip + * addres comparison in this case + */ + if(BUF(buf)->proto == UIP_PROTO_UDP) { + uip_appdata(buf) = &uip_buf(buf)[UIP_LLH_LEN + UIP_IPUDPH_LEN]; + } + + if(msg_for_dhcpc(buf)) { + if(!(uip_ipaddr_cmp(&BUF(buf)->destipaddr, &uip_hostaddr) || + uip_ipaddr_cmp(&BUF(buf)->destipaddr, &uip_broadcast_addr))) { + UIP_STAT(++uip_stat.ip.drop); + goto drop; + } + } else +#endif if(!uip_ipaddr_cmp(&BUF(buf)->destipaddr, &uip_hostaddr)) { UIP_STAT(++uip_stat.ip.drop); goto drop; @@ -1303,6 +1324,7 @@ uip_process(struct net_buf **buf_out, uint8_t flag) goto drop; } uip_len(buf) = uip_slen(buf) + UIP_IPUDPH_LEN; + ip_buf_len(buf) = uip_len(buf); #if NETSTACK_CONF_WITH_IPV6 /* For IPv6, the IP length field does not include the IPv6 IP header @@ -2151,13 +2173,13 @@ uip_process(struct net_buf **buf_out, uint8_t flag) /* Calculate IP checksum. */ BUF(buf)->ipchksum = 0; BUF(buf)->ipchksum = ~(uip_ipchksum(buf)); - DEBUG_PRINTF("uip ip_send_nolen: chkecum 0x%04x\n", uip_ipchksum(buf)); + PRINTF("uip ip_send_nolen: chkecum 0x%04x\n", uip_ipchksum(buf)); #endif /* NETSTACK_CONF_WITH_IPV6 */ UIP_STAT(++uip_stat.tcp.sent); #if NETSTACK_CONF_WITH_IPV6 send: #endif /* NETSTACK_CONF_WITH_IPV6 */ - DEBUG_PRINTF("Sending packet with length %d (%d)\n", uip_len(buf), + PRINTF("Sending packet with length %d (%d)\n", uip_len(buf), (BUF(buf)->len[0] << 8) | BUF(buf)->len[1]); UIP_STAT(++uip_stat.ip.sent); @@ -2235,5 +2257,23 @@ uip_send(struct net_buf *buf, const void *data, int len) } } #endif + +#if UIP_UDP +void +uip_send_udp(struct net_buf *buf, const void *data, int len) +{ + uip_slen(buf) = len; + + if (uip_process(&buf, UIP_UDP_SEND_CONN)) { + int ret = tcpip_output(buf, NULL); + if (!ret) { + PRINTF("Packet %p sending failed.\n", buf); + ip_buf_unref(buf); + } else { + ip_buf_sent_status(buf) = 0; + } + } +} +#endif /*---------------------------------------------------------------------------*/ /** @}*/ diff --git a/net/ip/net_core.c b/net/ip/net_core.c index 774cee5a121..303a6fe0434 100644 --- a/net/ip/net_core.c +++ b/net/ip/net_core.c @@ -57,6 +57,10 @@ #include "contiki/mac/handler-802154.h" #endif +#ifdef CONFIG_DHCP +#include "contiki/ip/dhcpc.h" +#endif + /* Declare some private functions only to be used in this file so the * prototypes are not found in .h file. */ @@ -1076,6 +1080,9 @@ static int network_initialization(void) NETSTACK_RADIO.set_value(RADIO_PARAM_PAN_ID, IEEE802154_PANID); #endif +#ifdef CONFIG_DHCP + dhcpc_init(uip_lladdr.addr, sizeof(uip_lladdr.addr)); +#endif return 0; }