raw.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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 <[email protected]>
  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 <string.h>
  49. /** The list of RAW PCBs */
  50. static struct raw_pcb *raw_pcbs;
  51. /**
  52. * Determine if in incoming IP packet is covered by a RAW PCB
  53. * and if so, pass it to a user-provided receive callback function.
  54. *
  55. * Given an incoming IP datagram (as a chain of pbufs) this function
  56. * finds a corresponding RAW PCB and calls the corresponding receive
  57. * callback function.
  58. *
  59. * @param p pbuf to be demultiplexed to a RAW PCB.
  60. * @param inp network interface on which the datagram was received.
  61. * @return - 1 if the packet has been eaten by a RAW PCB receive
  62. * callback function. The caller MAY NOT not reference the
  63. * packet any longer, and MAY NOT call pbuf_free().
  64. * @return - 0 if packet is not eaten (pbuf is still referenced by the
  65. * caller).
  66. *
  67. */
  68. u8_t
  69. raw_input(struct pbuf *p, struct netif *inp)
  70. {
  71. struct raw_pcb *pcb, *prev;
  72. struct ip_hdr *iphdr;
  73. s16_t proto;
  74. u8_t eaten = 0;
  75. LWIP_UNUSED_ARG(inp);
  76. iphdr = (struct ip_hdr *)p->payload;
  77. proto = IPH_PROTO(iphdr);
  78. prev = NULL;
  79. pcb = raw_pcbs;
  80. /* loop through all raw pcbs until the packet is eaten by one */
  81. /* this allows multiple pcbs to match against the packet by design */
  82. while ((eaten == 0) && (pcb != NULL)) {
  83. if ((pcb->protocol == proto) &&
  84. (ip_addr_isany(&pcb->local_ip) ||
  85. ip_addr_cmp(&(pcb->local_ip), &current_iphdr_dest))) {
  86. #if IP_SOF_BROADCAST_RECV
  87. /* broadcast filter? */
  88. if ((pcb->so_options & SOF_BROADCAST) || !ip_addr_isbroadcast(&current_iphdr_dest, inp))
  89. #endif /* IP_SOF_BROADCAST_RECV */
  90. {
  91. /* receive callback function available? */
  92. if (pcb->recv != NULL) {
  93. /* the receive callback function did not eat the packet? */
  94. if (pcb->recv(pcb->recv_arg, pcb, p, ip_current_src_addr()) != 0) {
  95. /* receive function ate the packet */
  96. p = NULL;
  97. eaten = 1;
  98. if (prev != NULL) {
  99. /* move the pcb to the front of raw_pcbs so that is
  100. found faster next time */
  101. prev->next = pcb->next;
  102. pcb->next = raw_pcbs;
  103. raw_pcbs = pcb;
  104. }
  105. }
  106. }
  107. /* no receive callback function was set for this raw PCB */
  108. }
  109. /* drop the packet */
  110. }
  111. prev = pcb;
  112. pcb = pcb->next;
  113. }
  114. return eaten;
  115. }
  116. /**
  117. * Bind a RAW PCB.
  118. *
  119. * @param pcb RAW PCB to be bound with a local address ipaddr.
  120. * @param ipaddr local IP address to bind with. Use IP_ADDR_ANY to
  121. * bind to all local interfaces.
  122. *
  123. * @return lwIP error code.
  124. * - ERR_OK. Successful. No error occured.
  125. * - ERR_USE. The specified IP address is already bound to by
  126. * another RAW PCB.
  127. *
  128. * @see raw_disconnect()
  129. */
  130. err_t
  131. raw_bind(struct raw_pcb *pcb, ip_addr_t *ipaddr)
  132. {
  133. ip_addr_set(&pcb->local_ip, ipaddr);
  134. return ERR_OK;
  135. }
  136. /**
  137. * Connect an RAW PCB. This function is required by upper layers
  138. * of lwip. Using the raw api you could use raw_sendto() instead
  139. *
  140. * This will associate the RAW PCB with the remote address.
  141. *
  142. * @param pcb RAW PCB to be connected with remote address ipaddr and port.
  143. * @param ipaddr remote IP address to connect with.
  144. *
  145. * @return lwIP error code
  146. *
  147. * @see raw_disconnect() and raw_sendto()
  148. */
  149. err_t
  150. raw_connect(struct raw_pcb *pcb, ip_addr_t *ipaddr)
  151. {
  152. ip_addr_set(&pcb->remote_ip, ipaddr);
  153. return ERR_OK;
  154. }
  155. /**
  156. * Set the callback function for received packets that match the
  157. * raw PCB's protocol and binding.
  158. *
  159. * The callback function MUST either
  160. * - eat the packet by calling pbuf_free() and returning non-zero. The
  161. * packet will not be passed to other raw PCBs or other protocol layers.
  162. * - not free the packet, and return zero. The packet will be matched
  163. * against further PCBs and/or forwarded to another protocol layers.
  164. *
  165. * @return non-zero if the packet was free()d, zero if the packet remains
  166. * available for others.
  167. */
  168. void
  169. raw_recv(struct raw_pcb *pcb, raw_recv_fn recv, void *recv_arg)
  170. {
  171. /* remember recv() callback and user data */
  172. pcb->recv = recv;
  173. pcb->recv_arg = recv_arg;
  174. }
  175. /**
  176. * Send the raw IP packet to the given address. Note that actually you cannot
  177. * modify the IP headers (this is inconsistent with the receive callback where
  178. * you actually get the IP headers), you can only specify the IP payload here.
  179. * It requires some more changes in lwIP. (there will be a raw_send() function
  180. * then.)
  181. *
  182. * @param pcb the raw pcb which to send
  183. * @param p the IP payload to send
  184. * @param ipaddr the destination address of the IP packet
  185. *
  186. */
  187. err_t
  188. raw_sendto(struct raw_pcb *pcb, struct pbuf *p, ip_addr_t *ipaddr)
  189. {
  190. err_t err;
  191. struct netif *netif;
  192. ip_addr_t *src_ip;
  193. struct pbuf *q; /* q will be sent down the stack */
  194. LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE, ("raw_sendto\n"));
  195. /* not enough space to add an IP header to first pbuf in given p chain? */
  196. if (pbuf_header(p, IP_HLEN)) {
  197. /* allocate header in new pbuf */
  198. q = pbuf_alloc(PBUF_IP, 0, PBUF_RAM);
  199. /* new header pbuf could not be allocated? */
  200. if (q == NULL) {
  201. LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("raw_sendto: could not allocate header\n"));
  202. return ERR_MEM;
  203. }
  204. /* chain header q in front of given pbuf p */
  205. pbuf_chain(q, p);
  206. /* { first pbuf q points to header pbuf } */
  207. LWIP_DEBUGF(RAW_DEBUG, ("raw_sendto: added header pbuf %p before given pbuf %p\n", (void *)q, (void *)p));
  208. } else {
  209. /* first pbuf q equals given pbuf */
  210. q = p;
  211. if(pbuf_header(q, -IP_HLEN)) {
  212. LWIP_ASSERT("Can't restore header we just removed!", 0);
  213. return ERR_MEM;
  214. }
  215. }
  216. if ((netif = ip_route(ipaddr)) == NULL) {
  217. LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_LEVEL_WARNING, ("raw_sendto: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
  218. ip4_addr1_16(ipaddr), ip4_addr2_16(ipaddr), ip4_addr3_16(ipaddr), ip4_addr4_16(ipaddr)));
  219. /* free any temporary header pbuf allocated by pbuf_header() */
  220. if (q != p) {
  221. pbuf_free(q);
  222. }
  223. return ERR_RTE;
  224. }
  225. #if IP_SOF_BROADCAST
  226. /* broadcast filter? */
  227. if (((pcb->so_options & SOF_BROADCAST) == 0) && ip_addr_isbroadcast(ipaddr, netif)) {
  228. LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_LEVEL_WARNING, ("raw_sendto: SOF_BROADCAST not enabled on pcb %p\n", (void *)pcb));
  229. /* free any temporary header pbuf allocated by pbuf_header() */
  230. if (q != p) {
  231. pbuf_free(q);
  232. }
  233. return ERR_VAL;
  234. }
  235. #endif /* IP_SOF_BROADCAST */
  236. if (ip_addr_isany(&pcb->local_ip)) {
  237. /* use outgoing network interface IP address as source address */
  238. src_ip = &(netif->ip_addr);
  239. } else {
  240. /* use RAW PCB local IP address as source address */
  241. src_ip = &(pcb->local_ip);
  242. }
  243. #if LWIP_NETIF_HWADDRHINT
  244. netif->addr_hint = &(pcb->addr_hint);
  245. #endif /* LWIP_NETIF_HWADDRHINT*/
  246. err = ip_output_if (q, src_ip, ipaddr, pcb->ttl, pcb->tos, pcb->protocol, netif);
  247. #if LWIP_NETIF_HWADDRHINT
  248. netif->addr_hint = NULL;
  249. #endif /* LWIP_NETIF_HWADDRHINT*/
  250. /* did we chain a header earlier? */
  251. if (q != p) {
  252. /* free the header */
  253. pbuf_free(q);
  254. }
  255. return err;
  256. }
  257. /**
  258. * Send the raw IP packet to the address given by raw_connect()
  259. *
  260. * @param pcb the raw pcb which to send
  261. * @param p the IP payload to send
  262. *
  263. */
  264. err_t
  265. raw_send(struct raw_pcb *pcb, struct pbuf *p)
  266. {
  267. return raw_sendto(pcb, p, &pcb->remote_ip);
  268. }
  269. /**
  270. * Remove an RAW PCB.
  271. *
  272. * @param pcb RAW PCB to be removed. The PCB is removed from the list of
  273. * RAW PCB's and the data structure is freed from memory.
  274. *
  275. * @see raw_new()
  276. */
  277. void
  278. raw_remove(struct raw_pcb *pcb)
  279. {
  280. struct raw_pcb *pcb2;
  281. /* pcb to be removed is first in list? */
  282. if (raw_pcbs == pcb) {
  283. /* make list start at 2nd pcb */
  284. raw_pcbs = raw_pcbs->next;
  285. /* pcb not 1st in list */
  286. } else {
  287. for(pcb2 = raw_pcbs; pcb2 != NULL; pcb2 = pcb2->next) {
  288. /* find pcb in raw_pcbs list */
  289. if (pcb2->next != NULL && pcb2->next == pcb) {
  290. /* remove pcb from list */
  291. pcb2->next = pcb->next;
  292. }
  293. }
  294. }
  295. memp_free(MEMP_RAW_PCB, pcb);
  296. }
  297. /**
  298. * Create a RAW PCB.
  299. *
  300. * @return The RAW PCB which was created. NULL if the PCB data structure
  301. * could not be allocated.
  302. *
  303. * @param proto the protocol number of the IPs payload (e.g. IP_PROTO_ICMP)
  304. *
  305. * @see raw_remove()
  306. */
  307. struct raw_pcb *
  308. raw_new(u8_t proto)
  309. {
  310. struct raw_pcb *pcb;
  311. LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE, ("raw_new\n"));
  312. pcb = (struct raw_pcb *)memp_malloc(MEMP_RAW_PCB);
  313. /* could allocate RAW PCB? */
  314. if (pcb != NULL) {
  315. /* initialize PCB to all zeroes */
  316. memset(pcb, 0, sizeof(struct raw_pcb));
  317. pcb->protocol = proto;
  318. pcb->ttl = RAW_TTL;
  319. pcb->next = raw_pcbs;
  320. raw_pcbs = pcb;
  321. }
  322. return pcb;
  323. }
  324. #endif /* LWIP_RAW */