raw.c 16 KB

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