raw.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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/inet.h"
  44. #include "lwip/ip_addr.h"
  45. #include "lwip/netif.h"
  46. #include "lwip/raw.h"
  47. #include "lwip/stats.h"
  48. #include "lwip/snmp.h"
  49. #include "arch/perf.h"
  50. #include <string.h>
  51. /** The list of RAW PCBs */
  52. static struct raw_pcb *raw_pcbs;
  53. /**
  54. * Determine if in incoming IP packet is covered by a RAW PCB
  55. * and if so, pass it to a user-provided receive callback function.
  56. *
  57. * Given an incoming IP datagram (as a chain of pbufs) this function
  58. * finds a corresponding RAW PCB and calls the corresponding receive
  59. * callback function.
  60. *
  61. * @param p pbuf to be demultiplexed to a RAW PCB.
  62. * @param inp network interface on which the datagram was received.
  63. * @return - 1 if the packet has been eaten by a RAW PCB receive
  64. * callback function. The caller MAY NOT not reference the
  65. * packet any longer, and MAY NOT call pbuf_free().
  66. * @return - 0 if packet is not eaten (pbuf is still referenced by the
  67. * caller).
  68. *
  69. */
  70. u8_t
  71. raw_input(struct pbuf *p, struct netif *inp)
  72. {
  73. struct raw_pcb *pcb, *prev;
  74. struct ip_hdr *iphdr;
  75. s16_t proto;
  76. u8_t eaten = 0;
  77. LWIP_UNUSED_ARG(inp);
  78. iphdr = p->payload;
  79. proto = IPH_PROTO(iphdr);
  80. prev = NULL;
  81. pcb = raw_pcbs;
  82. /* loop through all raw pcbs until the packet is eaten by one */
  83. /* this allows multiple pcbs to match against the packet by design */
  84. while ((eaten == 0) && (pcb != NULL)) {
  85. if (pcb->protocol == proto) {
  86. #if IP_SOF_BROADCAST_RECV
  87. /* broadcast filter? */
  88. if ((pcb->so_options & SOF_BROADCAST) || !ip_addr_isbroadcast(&(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, &(iphdr->src)) != 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, struct ip_addr *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, struct ip_addr *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,
  170. u8_t (* recv)(void *arg, struct raw_pcb *upcb, struct pbuf *p,
  171. struct ip_addr *addr),
  172. void *recv_arg)
  173. {
  174. /* remember recv() callback and user data */
  175. pcb->recv = recv;
  176. pcb->recv_arg = recv_arg;
  177. }
  178. /**
  179. * Send the raw IP packet to the given address. Note that actually you cannot
  180. * modify the IP headers (this is inconsistent with the receive callback where
  181. * you actually get the IP headers), you can only specify the IP payload here.
  182. * It requires some more changes in lwIP. (there will be a raw_send() function
  183. * then.)
  184. *
  185. * @param pcb the raw pcb which to send
  186. * @param p the IP payload to send
  187. * @param ipaddr the destination address of the IP packet
  188. *
  189. */
  190. err_t
  191. raw_sendto(struct raw_pcb *pcb, struct pbuf *p, struct ip_addr *ipaddr)
  192. {
  193. err_t err;
  194. struct netif *netif;
  195. struct ip_addr *src_ip;
  196. struct pbuf *q; /* q will be sent down the stack */
  197. LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE, ("raw_sendto\n"));
  198. /* not enough space to add an IP header to first pbuf in given p chain? */
  199. if (pbuf_header(p, IP_HLEN)) {
  200. /* allocate header in new pbuf */
  201. q = pbuf_alloc(PBUF_IP, 0, PBUF_RAM);
  202. /* new header pbuf could not be allocated? */
  203. if (q == NULL) {
  204. LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("raw_sendto: could not allocate header\n"));
  205. return ERR_MEM;
  206. }
  207. /* chain header q in front of given pbuf p */
  208. pbuf_chain(q, p);
  209. /* { first pbuf q points to header pbuf } */
  210. LWIP_DEBUGF(RAW_DEBUG, ("raw_sendto: added header pbuf %p before given pbuf %p\n", (void *)q, (void *)p));
  211. } else {
  212. /* first pbuf q equals given pbuf */
  213. q = p;
  214. if(pbuf_header(q, -IP_HLEN)) {
  215. LWIP_ASSERT("Can't restore header we just removed!", 0);
  216. return ERR_MEM;
  217. }
  218. }
  219. if ((netif = ip_route(ipaddr)) == NULL) {
  220. LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_LEVEL_WARNING, ("raw_sendto: No route to 0x%"X32_F"\n", ipaddr->addr));
  221. /* free any temporary header pbuf allocated by pbuf_header() */
  222. if (q != p) {
  223. pbuf_free(q);
  224. }
  225. return ERR_RTE;
  226. }
  227. #if IP_SOF_BROADCAST
  228. /* broadcast filter? */
  229. if ( ((pcb->so_options & SOF_BROADCAST) == 0) && ip_addr_isbroadcast(ipaddr, netif) ) {
  230. LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_LEVEL_WARNING, ("raw_sendto: SOF_BROADCAST not enabled on pcb %p\n", (void *)pcb));
  231. /* free any temporary header pbuf allocated by pbuf_header() */
  232. if (q != p) {
  233. pbuf_free(q);
  234. }
  235. return ERR_VAL;
  236. }
  237. #endif /* IP_SOF_BROADCAST */
  238. if (ip_addr_isany(&pcb->local_ip)) {
  239. /* use outgoing network interface IP address as source address */
  240. src_ip = &(netif->ip_addr);
  241. } else {
  242. /* use RAW PCB local IP address as source address */
  243. src_ip = &(pcb->local_ip);
  244. }
  245. #if LWIP_NETIF_HWADDRHINT
  246. netif->addr_hint = &(pcb->addr_hint);
  247. #endif /* LWIP_NETIF_HWADDRHINT*/
  248. err = ip_output_if (q, src_ip, ipaddr, pcb->ttl, pcb->tos, pcb->protocol, netif);
  249. #if LWIP_NETIF_HWADDRHINT
  250. netif->addr_hint = NULL;
  251. #endif /* LWIP_NETIF_HWADDRHINT*/
  252. /* did we chain a header earlier? */
  253. if (q != p) {
  254. /* free the header */
  255. pbuf_free(q);
  256. }
  257. return err;
  258. }
  259. /**
  260. * Send the raw IP packet to the address given by raw_connect()
  261. *
  262. * @param pcb the raw pcb which to send
  263. * @param p the IP payload to send
  264. *
  265. */
  266. err_t
  267. raw_send(struct raw_pcb *pcb, struct pbuf *p)
  268. {
  269. return raw_sendto(pcb, p, &pcb->remote_ip);
  270. }
  271. /**
  272. * Remove an RAW PCB.
  273. *
  274. * @param pcb RAW PCB to be removed. The PCB is removed from the list of
  275. * RAW PCB's and the data structure is freed from memory.
  276. *
  277. * @see raw_new()
  278. */
  279. void
  280. raw_remove(struct raw_pcb *pcb)
  281. {
  282. struct raw_pcb *pcb2;
  283. /* pcb to be removed is first in list? */
  284. if (raw_pcbs == pcb) {
  285. /* make list start at 2nd pcb */
  286. raw_pcbs = raw_pcbs->next;
  287. /* pcb not 1st in list */
  288. } else {
  289. for(pcb2 = raw_pcbs; pcb2 != NULL; pcb2 = pcb2->next) {
  290. /* find pcb in raw_pcbs list */
  291. if (pcb2->next != NULL && pcb2->next == pcb) {
  292. /* remove pcb from list */
  293. pcb2->next = pcb->next;
  294. }
  295. }
  296. }
  297. memp_free(MEMP_RAW_PCB, pcb);
  298. }
  299. /**
  300. * Create a RAW PCB.
  301. *
  302. * @return The RAW PCB which was created. NULL if the PCB data structure
  303. * could not be allocated.
  304. *
  305. * @param proto the protocol number of the IPs payload (e.g. IP_PROTO_ICMP)
  306. *
  307. * @see raw_remove()
  308. */
  309. struct raw_pcb *
  310. raw_new(u8_t proto) {
  311. struct raw_pcb *pcb;
  312. LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE, ("raw_new\n"));
  313. pcb = memp_malloc(MEMP_RAW_PCB);
  314. /* could allocate RAW PCB? */
  315. if (pcb != NULL) {
  316. /* initialize PCB to all zeroes */
  317. memset(pcb, 0, sizeof(struct raw_pcb));
  318. pcb->protocol = proto;
  319. pcb->ttl = RAW_TTL;
  320. pcb->next = raw_pcbs;
  321. raw_pcbs = pcb;
  322. }
  323. return pcb;
  324. }
  325. #endif /* LWIP_RAW */