raw.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  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.<br>
  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.<br>
  13. * @see @ref 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/priv/raw_priv.h"
  54. #include "lwip/stats.h"
  55. #include "lwip/ip6.h"
  56. #include "lwip/ip6_addr.h"
  57. #include "lwip/inet_chksum.h"
  58. #include <string.h>
  59. /** The list of RAW PCBs */
  60. static struct raw_pcb *raw_pcbs;
  61. static u8_t
  62. raw_input_local_match(struct raw_pcb *pcb, u8_t broadcast)
  63. {
  64. LWIP_UNUSED_ARG(broadcast); /* in IPv6 only case */
  65. /* check if PCB is bound to specific netif */
  66. if ((pcb->netif_idx != NETIF_NO_INDEX) &&
  67. (pcb->netif_idx != netif_get_index(ip_data.current_input_netif))) {
  68. return 0;
  69. }
  70. #if LWIP_IPV4 && LWIP_IPV6
  71. /* Dual-stack: PCBs listening to any IP type also listen to any IP address */
  72. if (IP_IS_ANY_TYPE_VAL(pcb->local_ip)) {
  73. #if IP_SOF_BROADCAST_RECV
  74. if ((broadcast != 0) && !ip_get_option(pcb, SOF_BROADCAST)) {
  75. return 0;
  76. }
  77. #endif /* IP_SOF_BROADCAST_RECV */
  78. return 1;
  79. }
  80. #endif /* LWIP_IPV4 && LWIP_IPV6 */
  81. /* Only need to check PCB if incoming IP version matches PCB IP version */
  82. if (IP_ADDR_PCB_VERSION_MATCH_EXACT(pcb, ip_current_dest_addr())) {
  83. #if LWIP_IPV4
  84. /* Special case: IPv4 broadcast: receive all broadcasts
  85. * Note: broadcast variable can only be 1 if it is an IPv4 broadcast */
  86. if (broadcast != 0) {
  87. #if IP_SOF_BROADCAST_RECV
  88. if (ip_get_option(pcb, SOF_BROADCAST))
  89. #endif /* IP_SOF_BROADCAST_RECV */
  90. {
  91. if (ip4_addr_isany(ip_2_ip4(&pcb->local_ip))) {
  92. return 1;
  93. }
  94. }
  95. } else
  96. #endif /* LWIP_IPV4 */
  97. /* Handle IPv4 and IPv6: catch all or exact match */
  98. if (ip_addr_isany(&pcb->local_ip) ||
  99. ip_addr_eq(&pcb->local_ip, ip_current_dest_addr())) {
  100. return 1;
  101. }
  102. }
  103. return 0;
  104. }
  105. /**
  106. * Determine if in incoming IP packet is covered by a RAW PCB
  107. * and if so, pass it to a user-provided receive callback function.
  108. *
  109. * Given an incoming IP datagram (as a chain of pbufs) this function
  110. * finds a corresponding RAW PCB and calls the corresponding receive
  111. * callback function.
  112. *
  113. * @param p pbuf to be demultiplexed to a RAW PCB.
  114. * @param inp network interface on which the datagram was received.
  115. * @return - 1 if the packet has been eaten by a RAW PCB receive
  116. * callback function. The caller MAY NOT not reference the
  117. * packet any longer, and MAY NOT call pbuf_free().
  118. * @return - 0 if packet is not eaten (pbuf is still referenced by the
  119. * caller).
  120. *
  121. */
  122. raw_input_state_t
  123. raw_input(struct pbuf *p, struct netif *inp)
  124. {
  125. struct raw_pcb *pcb, *prev;
  126. s16_t proto;
  127. raw_input_state_t ret = RAW_INPUT_NONE;
  128. u8_t broadcast = ip_addr_isbroadcast(ip_current_dest_addr(), ip_current_netif());
  129. LWIP_UNUSED_ARG(inp);
  130. #if LWIP_IPV6
  131. #if LWIP_IPV4
  132. if (IP_HDR_GET_VERSION(p->payload) == 6)
  133. #endif /* LWIP_IPV4 */
  134. {
  135. struct ip6_hdr *ip6hdr = (struct ip6_hdr *)p->payload;
  136. proto = IP6H_NEXTH(ip6hdr);
  137. }
  138. #if LWIP_IPV4
  139. else
  140. #endif /* LWIP_IPV4 */
  141. #endif /* LWIP_IPV6 */
  142. #if LWIP_IPV4
  143. {
  144. proto = IPH_PROTO((struct ip_hdr *)p->payload);
  145. }
  146. #endif /* LWIP_IPV4 */
  147. prev = NULL;
  148. pcb = raw_pcbs;
  149. /* loop through all raw pcbs until the packet is eaten by one */
  150. /* this allows multiple pcbs to match against the packet by design */
  151. while (pcb != NULL) {
  152. if ((pcb->protocol == proto) && raw_input_local_match(pcb, broadcast) &&
  153. (((pcb->flags & RAW_FLAGS_CONNECTED) == 0) ||
  154. ip_addr_eq(&pcb->remote_ip, ip_current_src_addr()))) {
  155. /* receive callback function available? */
  156. if (pcb->recv != NULL) {
  157. u8_t eaten;
  158. #ifndef LWIP_NOASSERT
  159. void *old_payload = p->payload;
  160. #endif
  161. ret = RAW_INPUT_DELIVERED;
  162. /* the receive callback function did not eat the packet? */
  163. eaten = pcb->recv(pcb->recv_arg, pcb, p, ip_current_src_addr());
  164. if (eaten != 0) {
  165. /* receive function ate the packet */
  166. p = NULL;
  167. if (prev != NULL) {
  168. /* move the pcb to the front of raw_pcbs so that is
  169. found faster next time */
  170. prev->next = pcb->next;
  171. pcb->next = raw_pcbs;
  172. raw_pcbs = pcb;
  173. }
  174. return RAW_INPUT_EATEN;
  175. } else {
  176. /* sanity-check that the receive callback did not alter the pbuf */
  177. LWIP_ASSERT("raw pcb recv callback altered pbuf payload pointer without eating packet",
  178. p->payload == old_payload);
  179. }
  180. }
  181. /* no receive callback function was set for this raw PCB */
  182. }
  183. /* drop the packet */
  184. prev = pcb;
  185. pcb = pcb->next;
  186. }
  187. return ret;
  188. }
  189. /**
  190. * @ingroup raw_raw
  191. * Bind a RAW PCB.
  192. *
  193. * @param pcb RAW PCB to be bound with a local address ipaddr.
  194. * @param ipaddr local IP address to bind with. Use IP4_ADDR_ANY to
  195. * bind to all local interfaces.
  196. *
  197. * @return lwIP error code.
  198. * - ERR_OK. Successful. No error occurred.
  199. * - ERR_USE. The specified IP address is already bound to by
  200. * another RAW PCB.
  201. *
  202. * @see raw_disconnect()
  203. */
  204. err_t
  205. raw_bind(struct raw_pcb *pcb, const ip_addr_t *ipaddr)
  206. {
  207. LWIP_ASSERT_CORE_LOCKED();
  208. if ((pcb == NULL) || (ipaddr == NULL)) {
  209. return ERR_VAL;
  210. }
  211. ip_addr_set_ipaddr(&pcb->local_ip, ipaddr);
  212. #if LWIP_IPV6 && LWIP_IPV6_SCOPES
  213. /* If the given IP address should have a zone but doesn't, assign one now.
  214. * This is legacy support: scope-aware callers should always provide properly
  215. * zoned source addresses. */
  216. if (IP_IS_V6(&pcb->local_ip) &&
  217. ip6_addr_lacks_zone(ip_2_ip6(&pcb->local_ip), IP6_UNKNOWN)) {
  218. ip6_addr_select_zone(ip_2_ip6(&pcb->local_ip), ip_2_ip6(&pcb->local_ip));
  219. }
  220. #endif /* LWIP_IPV6 && LWIP_IPV6_SCOPES */
  221. return ERR_OK;
  222. }
  223. /**
  224. * @ingroup raw_raw
  225. * Bind an RAW PCB to a specific netif.
  226. * After calling this function, all packets received via this PCB
  227. * are guaranteed to have come in via the specified netif, and all
  228. * outgoing packets will go out via the specified netif.
  229. *
  230. * @param pcb RAW PCB to be bound with netif.
  231. * @param netif netif to bind to. Can be NULL.
  232. *
  233. * @see raw_disconnect()
  234. */
  235. void
  236. raw_bind_netif(struct raw_pcb *pcb, const struct netif *netif)
  237. {
  238. LWIP_ASSERT_CORE_LOCKED();
  239. if (netif != NULL) {
  240. pcb->netif_idx = netif_get_index(netif);
  241. } else {
  242. pcb->netif_idx = NETIF_NO_INDEX;
  243. }
  244. }
  245. /**
  246. * @ingroup raw_raw
  247. * Connect an RAW PCB. This function is required by upper layers
  248. * of lwip. Using the raw api you could use raw_sendto() instead
  249. *
  250. * This will associate the RAW PCB with the remote address.
  251. *
  252. * @param pcb RAW PCB to be connected with remote address ipaddr and port.
  253. * @param ipaddr remote IP address to connect with.
  254. *
  255. * @return lwIP error code
  256. *
  257. * @see raw_disconnect() and raw_sendto()
  258. */
  259. err_t
  260. raw_connect(struct raw_pcb *pcb, const ip_addr_t *ipaddr)
  261. {
  262. LWIP_ASSERT_CORE_LOCKED();
  263. if ((pcb == NULL) || (ipaddr == NULL)) {
  264. return ERR_VAL;
  265. }
  266. ip_addr_set_ipaddr(&pcb->remote_ip, ipaddr);
  267. #if LWIP_IPV6 && LWIP_IPV6_SCOPES
  268. /* If the given IP address should have a zone but doesn't, assign one now,
  269. * using the bound address to make a more informed decision when possible. */
  270. if (IP_IS_V6(&pcb->remote_ip) &&
  271. ip6_addr_lacks_zone(ip_2_ip6(&pcb->remote_ip), IP6_UNKNOWN)) {
  272. ip6_addr_select_zone(ip_2_ip6(&pcb->remote_ip), ip_2_ip6(&pcb->local_ip));
  273. }
  274. #endif /* LWIP_IPV6 && LWIP_IPV6_SCOPES */
  275. raw_set_flags(pcb, RAW_FLAGS_CONNECTED);
  276. return ERR_OK;
  277. }
  278. /**
  279. * @ingroup raw_raw
  280. * Disconnect a RAW PCB.
  281. *
  282. * @param pcb the raw pcb to disconnect.
  283. */
  284. void
  285. raw_disconnect(struct raw_pcb *pcb)
  286. {
  287. LWIP_ASSERT_CORE_LOCKED();
  288. /* reset remote address association */
  289. #if LWIP_IPV4 && LWIP_IPV6
  290. if (IP_IS_ANY_TYPE_VAL(pcb->local_ip)) {
  291. ip_addr_copy(pcb->remote_ip, *IP_ANY_TYPE);
  292. } else {
  293. #endif
  294. ip_addr_set_any(IP_IS_V6_VAL(pcb->remote_ip), &pcb->remote_ip);
  295. #if LWIP_IPV4 && LWIP_IPV6
  296. }
  297. #endif
  298. pcb->netif_idx = NETIF_NO_INDEX;
  299. /* mark PCB as unconnected */
  300. raw_clear_flags(pcb, RAW_FLAGS_CONNECTED);
  301. }
  302. /**
  303. * @ingroup raw_raw
  304. * Set the callback function for received packets that match the
  305. * raw PCB's protocol and binding.
  306. *
  307. * The callback function MUST either
  308. * - eat the packet by calling pbuf_free() and returning non-zero. The
  309. * packet will not be passed to other raw PCBs or other protocol layers.
  310. * - not free the packet, and return zero. The packet will be matched
  311. * against further PCBs and/or forwarded to another protocol layers.
  312. */
  313. void
  314. raw_recv(struct raw_pcb *pcb, raw_recv_fn recv, void *recv_arg)
  315. {
  316. LWIP_ASSERT_CORE_LOCKED();
  317. /* remember recv() callback and user data */
  318. pcb->recv = recv;
  319. pcb->recv_arg = recv_arg;
  320. }
  321. /**
  322. * @ingroup raw_raw
  323. * Send the raw IP packet to the given address. An IP header will be prepended
  324. * to the packet, unless the RAW_FLAGS_HDRINCL flag is set on the PCB. In that
  325. * case, the packet must include an IP header, which will then be sent as is.
  326. *
  327. * @param pcb the raw pcb which to send
  328. * @param p the IP payload to send
  329. * @param ipaddr the destination address of the IP packet
  330. *
  331. */
  332. err_t
  333. raw_sendto(struct raw_pcb *pcb, struct pbuf *p, const ip_addr_t *ipaddr)
  334. {
  335. struct netif *netif;
  336. const ip_addr_t *src_ip;
  337. if ((pcb == NULL) || (ipaddr == NULL) || !IP_ADDR_PCB_VERSION_MATCH(pcb, ipaddr)) {
  338. return ERR_VAL;
  339. }
  340. LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE, ("raw_sendto\n"));
  341. if (pcb->netif_idx != NETIF_NO_INDEX) {
  342. netif = netif_get_by_index(pcb->netif_idx);
  343. } else {
  344. #if LWIP_MULTICAST_TX_OPTIONS
  345. netif = NULL;
  346. if (ip_addr_ismulticast(ipaddr)) {
  347. /* For multicast-destined packets, use the user-provided interface index to
  348. * determine the outgoing interface, if an interface index is set and a
  349. * matching netif can be found. Otherwise, fall back to regular routing. */
  350. netif = netif_get_by_index(pcb->mcast_ifindex);
  351. }
  352. if (netif == NULL)
  353. #endif /* LWIP_MULTICAST_TX_OPTIONS */
  354. {
  355. netif = ip_route(&pcb->local_ip, ipaddr);
  356. }
  357. }
  358. if (netif == NULL) {
  359. LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_LEVEL_WARNING, ("raw_sendto: No route to "));
  360. ip_addr_debug_print(RAW_DEBUG | LWIP_DBG_LEVEL_WARNING, ipaddr);
  361. LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_LEVEL_WARNING, ("\n"));
  362. return ERR_RTE;
  363. }
  364. if (ip_addr_isany(&pcb->local_ip) || ip_addr_ismulticast(&pcb->local_ip)) {
  365. /* use outgoing network interface IP address as source address */
  366. src_ip = ip_netif_get_local_ip(netif, ipaddr);
  367. #if LWIP_IPV6
  368. if (src_ip == NULL) {
  369. return ERR_RTE;
  370. }
  371. #endif /* LWIP_IPV6 */
  372. } else {
  373. /* use RAW PCB local IP address as source address */
  374. src_ip = &pcb->local_ip;
  375. }
  376. return raw_sendto_if_src(pcb, p, ipaddr, netif, src_ip);
  377. }
  378. /**
  379. * @ingroup raw_raw
  380. * Send the raw IP packet to the given address, using a particular outgoing
  381. * netif and source IP address. An IP header will be prepended to the packet,
  382. * unless the RAW_FLAGS_HDRINCL flag is set on the PCB. In that case, the
  383. * packet must include an IP header, which will then be sent as is.
  384. *
  385. * @param pcb RAW PCB used to send the data
  386. * @param p chain of pbufs to be sent
  387. * @param dst_ip destination IP address
  388. * @param netif the netif used for sending
  389. * @param src_ip source IP address
  390. */
  391. err_t
  392. raw_sendto_if_src(struct raw_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_ip,
  393. struct netif *netif, const ip_addr_t *src_ip)
  394. {
  395. err_t err;
  396. struct pbuf *q; /* q will be sent down the stack */
  397. u16_t header_size;
  398. u8_t ttl;
  399. LWIP_ASSERT_CORE_LOCKED();
  400. if ((pcb == NULL) || (dst_ip == NULL) || (netif == NULL) || (src_ip == NULL) ||
  401. !IP_ADDR_PCB_VERSION_MATCH(pcb, src_ip) || !IP_ADDR_PCB_VERSION_MATCH(pcb, dst_ip)) {
  402. return ERR_VAL;
  403. }
  404. header_size = (
  405. #if LWIP_IPV4 && LWIP_IPV6
  406. IP_IS_V6(dst_ip) ? IP6_HLEN : IP_HLEN);
  407. #elif LWIP_IPV4
  408. IP_HLEN);
  409. #else
  410. IP6_HLEN);
  411. #endif
  412. /* Handle the HDRINCL option as an exception: none of the code below applies
  413. * to this case, and sending the packet needs to be done differently too. */
  414. if (pcb->flags & RAW_FLAGS_HDRINCL) {
  415. /* A full header *must* be present in the first pbuf of the chain, as the
  416. * output routines may access its fields directly. */
  417. if (p->len < header_size) {
  418. return ERR_VAL;
  419. }
  420. /* @todo multicast loop support, if at all desired for this scenario.. */
  421. NETIF_SET_HINTS(netif, &pcb->netif_hints);
  422. err = ip_output_if_hdrincl(p, src_ip, dst_ip, netif);
  423. NETIF_RESET_HINTS(netif);
  424. return err;
  425. }
  426. /* packet too large to add an IP header without causing an overflow? */
  427. if ((u16_t)(p->tot_len + header_size) < p->tot_len) {
  428. return ERR_MEM;
  429. }
  430. /* not enough space to add an IP header to first pbuf in given p chain? */
  431. if (pbuf_add_header(p, header_size)) {
  432. /* allocate header in new pbuf */
  433. q = pbuf_alloc(PBUF_IP, 0, PBUF_RAM);
  434. /* new header pbuf could not be allocated? */
  435. if (q == NULL) {
  436. LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("raw_sendto: could not allocate header\n"));
  437. return ERR_MEM;
  438. }
  439. if (p->tot_len != 0) {
  440. /* chain header q in front of given pbuf p */
  441. pbuf_chain(q, p);
  442. }
  443. /* { first pbuf q points to header pbuf } */
  444. LWIP_DEBUGF(RAW_DEBUG, ("raw_sendto: added header pbuf %p before given pbuf %p\n", (void *)q, (void *)p));
  445. } else {
  446. /* first pbuf q equals given pbuf */
  447. q = p;
  448. if (pbuf_remove_header(q, header_size)) {
  449. LWIP_ASSERT("Can't restore header we just removed!", 0);
  450. return ERR_MEM;
  451. }
  452. }
  453. #if IP_SOF_BROADCAST
  454. if (IP_IS_V4(dst_ip)) {
  455. /* broadcast filter? */
  456. if (!ip_get_option(pcb, SOF_BROADCAST) && ip_addr_isbroadcast(dst_ip, netif)) {
  457. LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_LEVEL_WARNING, ("raw_sendto: SOF_BROADCAST not enabled on pcb %p\n", (void *)pcb));
  458. /* free any temporary header pbuf allocated by pbuf_header() */
  459. if (q != p) {
  460. pbuf_free(q);
  461. }
  462. return ERR_VAL;
  463. }
  464. }
  465. #endif /* IP_SOF_BROADCAST */
  466. /* Multicast Loop? */
  467. #if LWIP_MULTICAST_TX_OPTIONS
  468. if (((pcb->flags & RAW_FLAGS_MULTICAST_LOOP) != 0) && ip_addr_ismulticast(dst_ip)) {
  469. q->flags |= PBUF_FLAG_MCASTLOOP;
  470. }
  471. #endif /* LWIP_MULTICAST_TX_OPTIONS */
  472. #if LWIP_IPV6
  473. /* If requested, based on the IPV6_CHECKSUM socket option per RFC3542,
  474. compute the checksum and update the checksum in the payload. */
  475. if (IP_IS_V6(dst_ip) && pcb->chksum_reqd) {
  476. u16_t chksum = ip6_chksum_pseudo(p, pcb->protocol, p->tot_len, ip_2_ip6(src_ip), ip_2_ip6(dst_ip));
  477. LWIP_ASSERT("Checksum must fit into first pbuf", p->len >= (pcb->chksum_offset + 2));
  478. SMEMCPY(((u8_t *)p->payload) + pcb->chksum_offset, &chksum, sizeof(u16_t));
  479. }
  480. #endif
  481. /* Determine TTL to use */
  482. #if LWIP_MULTICAST_TX_OPTIONS
  483. ttl = (ip_addr_ismulticast(dst_ip) ? raw_get_multicast_ttl(pcb) : pcb->ttl);
  484. #else /* LWIP_MULTICAST_TX_OPTIONS */
  485. ttl = pcb->ttl;
  486. #endif /* LWIP_MULTICAST_TX_OPTIONS */
  487. NETIF_SET_HINTS(netif, &pcb->netif_hints);
  488. err = ip_output_if(q, src_ip, dst_ip, ttl, pcb->tos, pcb->protocol, netif);
  489. NETIF_RESET_HINTS(netif);
  490. /* did we chain a header earlier? */
  491. if (q != p) {
  492. /* free the header */
  493. pbuf_free(q);
  494. }
  495. return err;
  496. }
  497. /**
  498. * @ingroup raw_raw
  499. * Send the raw IP packet to the address given by raw_connect()
  500. *
  501. * @param pcb the raw pcb which to send
  502. * @param p the IP payload to send
  503. *
  504. */
  505. err_t
  506. raw_send(struct raw_pcb *pcb, struct pbuf *p)
  507. {
  508. return raw_sendto(pcb, p, &pcb->remote_ip);
  509. }
  510. /**
  511. * @ingroup raw_raw
  512. * Remove an RAW PCB.
  513. *
  514. * @param pcb RAW PCB to be removed. The PCB is removed from the list of
  515. * RAW PCB's and the data structure is freed from memory.
  516. *
  517. * @see raw_new()
  518. */
  519. void
  520. raw_remove(struct raw_pcb *pcb)
  521. {
  522. struct raw_pcb *pcb2;
  523. LWIP_ASSERT_CORE_LOCKED();
  524. /* pcb to be removed is first in list? */
  525. if (raw_pcbs == pcb) {
  526. /* make list start at 2nd pcb */
  527. raw_pcbs = raw_pcbs->next;
  528. /* pcb not 1st in list */
  529. } else {
  530. for (pcb2 = raw_pcbs; pcb2 != NULL; pcb2 = pcb2->next) {
  531. /* find pcb in raw_pcbs list */
  532. if (pcb2->next != NULL && pcb2->next == pcb) {
  533. /* remove pcb from list */
  534. pcb2->next = pcb->next;
  535. break;
  536. }
  537. }
  538. }
  539. memp_free(MEMP_RAW_PCB, pcb);
  540. }
  541. /**
  542. * @ingroup raw_raw
  543. * Create a RAW PCB.
  544. *
  545. * @return The RAW PCB which was created. NULL if the PCB data structure
  546. * could not be allocated.
  547. *
  548. * @param proto the protocol number of the IPs payload (e.g. IP_PROTO_ICMP)
  549. *
  550. * @see raw_remove()
  551. */
  552. struct raw_pcb *
  553. raw_new(u8_t proto)
  554. {
  555. struct raw_pcb *pcb;
  556. LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE, ("raw_new\n"));
  557. LWIP_ASSERT_CORE_LOCKED();
  558. pcb = (struct raw_pcb *)memp_malloc(MEMP_RAW_PCB);
  559. /* could allocate RAW PCB? */
  560. if (pcb != NULL) {
  561. /* initialize PCB to all zeroes */
  562. memset(pcb, 0, sizeof(struct raw_pcb));
  563. pcb->protocol = proto;
  564. pcb->ttl = RAW_TTL;
  565. #if LWIP_MULTICAST_TX_OPTIONS
  566. raw_set_multicast_ttl(pcb, RAW_TTL);
  567. #endif /* LWIP_MULTICAST_TX_OPTIONS */
  568. pcb_tci_init(pcb);
  569. pcb->next = raw_pcbs;
  570. raw_pcbs = pcb;
  571. }
  572. return pcb;
  573. }
  574. /**
  575. * @ingroup raw_raw
  576. * Create a RAW PCB for specific IP type.
  577. *
  578. * @return The RAW PCB which was created. NULL if the PCB data structure
  579. * could not be allocated.
  580. *
  581. * @param type IP address type, see @ref lwip_ip_addr_type definitions.
  582. * If you want to listen to IPv4 and IPv6 (dual-stack) packets,
  583. * supply @ref IPADDR_TYPE_ANY as argument and bind to @ref IP_ANY_TYPE.
  584. * @param proto the protocol number (next header) of the IPv6 packet payload
  585. * (e.g. IP6_NEXTH_ICMP6)
  586. *
  587. * @see raw_remove()
  588. */
  589. struct raw_pcb *
  590. raw_new_ip_type(u8_t type, u8_t proto)
  591. {
  592. struct raw_pcb *pcb;
  593. LWIP_ASSERT_CORE_LOCKED();
  594. pcb = raw_new(proto);
  595. #if LWIP_IPV4 && LWIP_IPV6
  596. if (pcb != NULL) {
  597. IP_SET_TYPE_VAL(pcb->local_ip, type);
  598. IP_SET_TYPE_VAL(pcb->remote_ip, type);
  599. }
  600. #else /* LWIP_IPV4 && LWIP_IPV6 */
  601. LWIP_UNUSED_ARG(type);
  602. #endif /* LWIP_IPV4 && LWIP_IPV6 */
  603. return pcb;
  604. }
  605. /** This function is called from netif.c when address is changed
  606. *
  607. * @param old_addr IP address of the netif before change
  608. * @param new_addr IP address of the netif after change
  609. */
  610. void raw_netif_ip_addr_changed(const ip_addr_t *old_addr, const ip_addr_t *new_addr)
  611. {
  612. struct raw_pcb *rpcb;
  613. if (!ip_addr_isany(old_addr) && !ip_addr_isany(new_addr)) {
  614. for (rpcb = raw_pcbs; rpcb != NULL; rpcb = rpcb->next) {
  615. /* PCB bound to current local interface address? */
  616. if (ip_addr_eq(&rpcb->local_ip, old_addr)) {
  617. /* The PCB is bound to the old ipaddr and
  618. * is set to bound to the new one instead */
  619. ip_addr_copy(rpcb->local_ip, *new_addr);
  620. }
  621. }
  622. }
  623. }
  624. #endif /* LWIP_RAW */