PageRenderTime 54ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/sdk/external/lwip/src/core/raw.c

https://gitlab.com/VD-EDU/vd-iot-evalkit-sdk
C | 436 lines | 227 code | 31 blank | 178 comment | 58 complexity | 58848b9753e29044e2340bcd7279adaa MD5 | raw file
  1. /**
  2. * @file
  3. * Implementation of raw protocol PCBs for low-level handling of
  4. * different types of protocols besides (or overriding) those
  5. * already available in lwIP.
  6. *
  7. */
  8. /*
  9. * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without modification,
  13. * are permitted provided that the following conditions are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright notice,
  18. * this list of conditions and the following disclaimer in the documentation
  19. * and/or other materials provided with the distribution.
  20. * 3. The name of the author may not be used to endorse or promote products
  21. * derived from this software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  24. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  25. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  26. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  27. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  28. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  31. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  32. * OF SUCH DAMAGE.
  33. *
  34. * This file is part of the lwIP TCP/IP stack.
  35. *
  36. * Author: Adam Dunkels <adam@sics.se>
  37. *
  38. */
  39. #include "lwip/opt.h"
  40. #if LWIP_RAW /* don't build if not configured for use in lwipopts.h */
  41. #include "lwip/def.h"
  42. #include "lwip/memp.h"
  43. #include "lwip/ip_addr.h"
  44. #include "lwip/netif.h"
  45. #include "lwip/raw.h"
  46. #include "lwip/stats.h"
  47. #include "arch/perf.h"
  48. #include "lwip/ip6.h"
  49. #include "lwip/ip6_addr.h"
  50. #include "lwip/inet_chksum.h"
  51. #include <string.h>
  52. /** The list of RAW PCBs */
  53. static struct raw_pcb *raw_pcbs;
  54. /**
  55. * Determine if in incoming IP packet is covered by a RAW PCB
  56. * and if so, pass it to a user-provided receive callback function.
  57. *
  58. * Given an incoming IP datagram (as a chain of pbufs) this function
  59. * finds a corresponding RAW PCB and calls the corresponding receive
  60. * callback function.
  61. *
  62. * @param p pbuf to be demultiplexed to a RAW PCB.
  63. * @param inp network interface on which the datagram was received.
  64. * @return - 1 if the packet has been eaten by a RAW PCB receive
  65. * callback function. The caller MAY NOT not reference the
  66. * packet any longer, and MAY NOT call pbuf_free().
  67. * @return - 0 if packet is not eaten (pbuf is still referenced by the
  68. * caller).
  69. *
  70. */
  71. u8_t
  72. raw_input(struct pbuf *p, struct netif *inp)
  73. {
  74. struct raw_pcb *pcb, *prev;
  75. struct ip_hdr *iphdr;
  76. s16_t proto;
  77. u8_t eaten = 0;
  78. LWIP_UNUSED_ARG(inp);
  79. iphdr = (struct ip_hdr *)p->payload;
  80. #if LWIP_IPV6
  81. if (IPH_V(iphdr) == 6) {
  82. struct ip6_hdr *ip6hdr = (struct ip6_hdr *)p->payload;
  83. proto = IP6H_NEXTH(ip6hdr);
  84. iphdr = NULL;
  85. }
  86. else
  87. #endif /* LWIP_IPV6 */
  88. {
  89. proto = IPH_PROTO(iphdr);
  90. }
  91. prev = NULL;
  92. pcb = raw_pcbs;
  93. /* loop through all raw pcbs until the packet is eaten by one */
  94. /* this allows multiple pcbs to match against the packet by design */
  95. while ((eaten == 0) && (pcb != NULL)) {
  96. if ((pcb->protocol == proto) && IP_PCB_IPVER_INPUT_MATCH(pcb) &&
  97. (ipX_addr_isany(PCB_ISIPV6(pcb), &pcb->local_ip) ||
  98. ipX_addr_cmp(PCB_ISIPV6(pcb), &(pcb->local_ip), ipX_current_dest_addr()))) {
  99. #if IP_SOF_BROADCAST_RECV
  100. /* broadcast filter? */
  101. if ((ip_get_option(pcb, SOF_BROADCAST) || !ip_addr_isbroadcast(ip_current_dest_addr(), inp))
  102. #if LWIP_IPV6
  103. || PCB_ISIPV6(pcb)
  104. #endif /* LWIP_IPV6 */
  105. )
  106. #endif /* IP_SOF_BROADCAST_RECV */
  107. {
  108. /* receive callback function available? */
  109. if (pcb->recv.ip4 != NULL) {
  110. #ifndef LWIP_NOASSERT
  111. void* old_payload = p->payload;
  112. #endif
  113. /* the receive callback function did not eat the packet? */
  114. #if LWIP_IPV6
  115. if (PCB_ISIPV6(pcb)) {
  116. eaten = pcb->recv.ip6(pcb->recv_arg, pcb, p, ip6_current_src_addr());
  117. } else
  118. #endif /* LWIP_IPV6 */
  119. {
  120. eaten = pcb->recv.ip4(pcb->recv_arg, pcb, p, ip_current_src_addr());
  121. }
  122. if (eaten != 0) {
  123. /* receive function ate the packet */
  124. p = NULL;
  125. eaten = 1;
  126. if (prev != NULL) {
  127. /* move the pcb to the front of raw_pcbs so that is
  128. found faster next time */
  129. prev->next = pcb->next;
  130. pcb->next = raw_pcbs;
  131. raw_pcbs = pcb;
  132. }
  133. } else {
  134. /* sanity-check that the receive callback did not alter the pbuf */
  135. LWIP_ASSERT("raw pcb recv callback altered pbuf payload pointer without eating packet",
  136. p->payload == old_payload);
  137. }
  138. }
  139. /* no receive callback function was set for this raw PCB */
  140. }
  141. /* drop the packet */
  142. }
  143. prev = pcb;
  144. pcb = pcb->next;
  145. }
  146. return eaten;
  147. }
  148. /**
  149. * Bind a RAW PCB.
  150. *
  151. * @param pcb RAW PCB to be bound with a local address ipaddr.
  152. * @param ipaddr local IP address to bind with. Use IP_ADDR_ANY to
  153. * bind to all local interfaces.
  154. *
  155. * @return lwIP error code.
  156. * - ERR_OK. Successful. No error occured.
  157. * - ERR_USE. The specified IP address is already bound to by
  158. * another RAW PCB.
  159. *
  160. * @see raw_disconnect()
  161. */
  162. err_t
  163. raw_bind(struct raw_pcb *pcb, ip_addr_t *ipaddr)
  164. {
  165. ipX_addr_set_ipaddr(PCB_ISIPV6(pcb), &pcb->local_ip, ipaddr);
  166. return ERR_OK;
  167. }
  168. /**
  169. * Connect an RAW PCB. This function is required by upper layers
  170. * of lwip. Using the raw api you could use raw_sendto() instead
  171. *
  172. * This will associate the RAW PCB with the remote address.
  173. *
  174. * @param pcb RAW PCB to be connected with remote address ipaddr and port.
  175. * @param ipaddr remote IP address to connect with.
  176. *
  177. * @return lwIP error code
  178. *
  179. * @see raw_disconnect() and raw_sendto()
  180. */
  181. err_t
  182. raw_connect(struct raw_pcb *pcb, ip_addr_t *ipaddr)
  183. {
  184. ipX_addr_set_ipaddr(PCB_ISIPV6(pcb), &pcb->remote_ip, ipaddr);
  185. return ERR_OK;
  186. }
  187. /**
  188. * Set the callback function for received packets that match the
  189. * raw PCB's protocol and binding.
  190. *
  191. * The callback function MUST either
  192. * - eat the packet by calling pbuf_free() and returning non-zero. The
  193. * packet will not be passed to other raw PCBs or other protocol layers.
  194. * - not free the packet, and return zero. The packet will be matched
  195. * against further PCBs and/or forwarded to another protocol layers.
  196. *
  197. * @return non-zero if the packet was free()d, zero if the packet remains
  198. * available for others.
  199. */
  200. void
  201. raw_recv(struct raw_pcb *pcb, raw_recv_fn recv, void *recv_arg)
  202. {
  203. /* remember recv() callback and user data */
  204. pcb->recv.ip4 = recv;
  205. pcb->recv_arg = recv_arg;
  206. }
  207. /**
  208. * Send the raw IP packet to the given address. Note that actually you cannot
  209. * modify the IP headers (this is inconsistent with the receive callback where
  210. * you actually get the IP headers), you can only specify the IP payload here.
  211. * It requires some more changes in lwIP. (there will be a raw_send() function
  212. * then.)
  213. *
  214. * @param pcb the raw pcb which to send
  215. * @param p the IP payload to send
  216. * @param ipaddr the destination address of the IP packet
  217. *
  218. */
  219. err_t
  220. raw_sendto(struct raw_pcb *pcb, struct pbuf *p, ip_addr_t *ipaddr)
  221. {
  222. err_t err;
  223. struct netif *netif;
  224. ipX_addr_t *src_ip;
  225. struct pbuf *q; /* q will be sent down the stack */
  226. s16_t header_size;
  227. ipX_addr_t *dst_ip = ip_2_ipX(ipaddr);
  228. LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE, ("raw_sendto\n"));
  229. header_size = (
  230. #if LWIP_IPV6
  231. PCB_ISIPV6(pcb) ? IP6_HLEN :
  232. #endif /* LWIP_IPV6 */
  233. IP_HLEN);
  234. /* not enough space to add an IP header to first pbuf in given p chain? */
  235. if (pbuf_header(p, header_size)) {
  236. /* allocate header in new pbuf */
  237. q = pbuf_alloc(PBUF_IP, 0, PBUF_RAM);
  238. /* new header pbuf could not be allocated? */
  239. if (q == NULL) {
  240. LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("raw_sendto: could not allocate header\n"));
  241. return ERR_MEM;
  242. }
  243. if (p->tot_len != 0) {
  244. /* chain header q in front of given pbuf p */
  245. pbuf_chain(q, p);
  246. }
  247. /* { first pbuf q points to header pbuf } */
  248. LWIP_DEBUGF(RAW_DEBUG, ("raw_sendto: added header pbuf %p before given pbuf %p\n", (void *)q, (void *)p));
  249. } else {
  250. /* first pbuf q equals given pbuf */
  251. q = p;
  252. if(pbuf_header(q, -header_size)) {
  253. LWIP_ASSERT("Can't restore header we just removed!", 0);
  254. return ERR_MEM;
  255. }
  256. }
  257. netif = ipX_route(PCB_ISIPV6(pcb), &pcb->local_ip, dst_ip);
  258. if (netif == NULL) {
  259. LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_LEVEL_WARNING, ("raw_sendto: No route to "));
  260. ipX_addr_debug_print(PCB_ISIPV6(pcb), RAW_DEBUG | LWIP_DBG_LEVEL_WARNING, dst_ip);
  261. /* free any temporary header pbuf allocated by pbuf_header() */
  262. if (q != p) {
  263. pbuf_free(q);
  264. }
  265. return ERR_RTE;
  266. }
  267. #if IP_SOF_BROADCAST
  268. #if LWIP_IPV6
  269. if (!PCB_ISIPV6(pcb))
  270. #endif /* LWIP_IPV6 */
  271. {
  272. /* broadcast filter? */
  273. if (!ip_get_option(pcb, SOF_BROADCAST) && ip_addr_isbroadcast(ipaddr, netif)) {
  274. LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_LEVEL_WARNING, ("raw_sendto: SOF_BROADCAST not enabled on pcb %p\n", (void *)pcb));
  275. /* free any temporary header pbuf allocated by pbuf_header() */
  276. if (q != p) {
  277. pbuf_free(q);
  278. }
  279. return ERR_VAL;
  280. }
  281. }
  282. #endif /* IP_SOF_BROADCAST */
  283. if (ipX_addr_isany(PCB_ISIPV6(pcb), &pcb->local_ip)) {
  284. /* use outgoing network interface IP address as source address */
  285. src_ip = ipX_netif_get_local_ipX(PCB_ISIPV6(pcb), netif, dst_ip);
  286. #if LWIP_IPV6
  287. if (src_ip == NULL) {
  288. if (q != p) {
  289. pbuf_free(q);
  290. }
  291. return ERR_RTE;
  292. }
  293. #endif /* LWIP_IPV6 */
  294. } else {
  295. /* use RAW PCB local IP address as source address */
  296. src_ip = &pcb->local_ip;
  297. }
  298. #if LWIP_IPV6
  299. /* If requested, based on the IPV6_CHECKSUM socket option per RFC3542,
  300. compute the checksum and update the checksum in the payload. */
  301. if (PCB_ISIPV6(pcb) && pcb->chksum_reqd) {
  302. u16_t chksum = ip6_chksum_pseudo(p, pcb->protocol, p->tot_len, ipX_2_ip6(src_ip), ipX_2_ip6(dst_ip));
  303. LWIP_ASSERT("Checksum must fit into first pbuf", p->len >= (pcb->chksum_offset + 2));
  304. SMEMCPY(((u8_t *)p->payload) + pcb->chksum_offset, &chksum, sizeof(u16_t));
  305. }
  306. #endif
  307. NETIF_SET_HWADDRHINT(netif, &pcb->addr_hint);
  308. err = ipX_output_if(PCB_ISIPV6(pcb), q, ipX_2_ip(src_ip), ipX_2_ip(dst_ip), pcb->ttl, pcb->tos, pcb->protocol, netif);
  309. NETIF_SET_HWADDRHINT(netif, NULL);
  310. /* did we chain a header earlier? */
  311. if (q != p) {
  312. /* free the header */
  313. pbuf_free(q);
  314. }
  315. return err;
  316. }
  317. /**
  318. * Send the raw IP packet to the address given by raw_connect()
  319. *
  320. * @param pcb the raw pcb which to send
  321. * @param p the IP payload to send
  322. *
  323. */
  324. err_t
  325. raw_send(struct raw_pcb *pcb, struct pbuf *p)
  326. {
  327. return raw_sendto(pcb, p, ipX_2_ip(&pcb->remote_ip));
  328. }
  329. /**
  330. * Remove an RAW PCB.
  331. *
  332. * @param pcb RAW PCB to be removed. The PCB is removed from the list of
  333. * RAW PCB's and the data structure is freed from memory.
  334. *
  335. * @see raw_new()
  336. */
  337. void
  338. raw_remove(struct raw_pcb *pcb)
  339. {
  340. struct raw_pcb *pcb2;
  341. /* pcb to be removed is first in list? */
  342. if (raw_pcbs == pcb) {
  343. /* make list start at 2nd pcb */
  344. raw_pcbs = raw_pcbs->next;
  345. /* pcb not 1st in list */
  346. } else {
  347. for(pcb2 = raw_pcbs; pcb2 != NULL; pcb2 = pcb2->next) {
  348. /* find pcb in raw_pcbs list */
  349. if (pcb2->next != NULL && pcb2->next == pcb) {
  350. /* remove pcb from list */
  351. pcb2->next = pcb->next;
  352. }
  353. }
  354. }
  355. memp_free(MEMP_RAW_PCB, pcb);
  356. }
  357. /**
  358. * Create a RAW PCB.
  359. *
  360. * @return The RAW PCB which was created. NULL if the PCB data structure
  361. * could not be allocated.
  362. *
  363. * @param proto the protocol number of the IPs payload (e.g. IP_PROTO_ICMP)
  364. *
  365. * @see raw_remove()
  366. */
  367. struct raw_pcb *
  368. raw_new(u8_t proto)
  369. {
  370. struct raw_pcb *pcb;
  371. LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE, ("raw_new\n"));
  372. pcb = (struct raw_pcb *)memp_malloc(MEMP_RAW_PCB);
  373. /* could allocate RAW PCB? */
  374. if (pcb != NULL) {
  375. /* initialize PCB to all zeroes */
  376. memset(pcb, 0, sizeof(struct raw_pcb));
  377. pcb->protocol = proto;
  378. pcb->ttl = RAW_TTL;
  379. pcb->next = raw_pcbs;
  380. raw_pcbs = pcb;
  381. }
  382. return pcb;
  383. }
  384. #if LWIP_IPV6
  385. /**
  386. * Create a RAW PCB for IPv6.
  387. *
  388. * @return The RAW PCB which was created. NULL if the PCB data structure
  389. * could not be allocated.
  390. *
  391. * @param proto the protocol number (next header) of the IPv6 packet payload
  392. * (e.g. IP6_NEXTH_ICMP6)
  393. *
  394. * @see raw_remove()
  395. */
  396. struct raw_pcb *
  397. raw_new_ip6(u8_t proto)
  398. {
  399. struct raw_pcb *pcb;
  400. pcb = raw_new(proto);
  401. ip_set_v6(pcb, 1);
  402. return pcb;
  403. }
  404. #endif /* LWIP_IPV6 */
  405. #endif /* LWIP_RAW */