raw.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  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 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_cmp(&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_cmp(&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. return ERR_RTE;
  362. }
  363. if (ip_addr_isany(&pcb->local_ip) || ip_addr_ismulticast(&pcb->local_ip)) {
  364. /* use outgoing network interface IP address as source address */
  365. src_ip = ip_netif_get_local_ip(netif, ipaddr);
  366. #if LWIP_IPV6
  367. if (src_ip == NULL) {
  368. return ERR_RTE;
  369. }
  370. #endif /* LWIP_IPV6 */
  371. } else {
  372. /* use RAW PCB local IP address as source address */
  373. src_ip = &pcb->local_ip;
  374. }
  375. return raw_sendto_if_src(pcb, p, ipaddr, netif, src_ip);
  376. }
  377. /**
  378. * @ingroup raw_raw
  379. * Send the raw IP packet to the given address, using a particular outgoing
  380. * netif and source IP address. An IP header will be prepended to the packet,
  381. * unless the RAW_FLAGS_HDRINCL flag is set on the PCB. In that case, the
  382. * packet must include an IP header, which will then be sent as is.
  383. *
  384. * @param pcb RAW PCB used to send the data
  385. * @param p chain of pbufs to be sent
  386. * @param dst_ip destination IP address
  387. * @param netif the netif used for sending
  388. * @param src_ip source IP address
  389. */
  390. err_t
  391. raw_sendto_if_src(struct raw_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_ip,
  392. struct netif *netif, const ip_addr_t *src_ip)
  393. {
  394. err_t err;
  395. struct pbuf *q; /* q will be sent down the stack */
  396. u16_t header_size;
  397. u8_t ttl;
  398. LWIP_ASSERT_CORE_LOCKED();
  399. if ((pcb == NULL) || (dst_ip == NULL) || (netif == NULL) || (src_ip == NULL) ||
  400. !IP_ADDR_PCB_VERSION_MATCH(pcb, src_ip) || !IP_ADDR_PCB_VERSION_MATCH(pcb, dst_ip)) {
  401. return ERR_VAL;
  402. }
  403. header_size = (
  404. #if LWIP_IPV4 && LWIP_IPV6
  405. IP_IS_V6(dst_ip) ? IP6_HLEN : IP_HLEN);
  406. #elif LWIP_IPV4
  407. IP_HLEN);
  408. #else
  409. IP6_HLEN);
  410. #endif
  411. /* Handle the HDRINCL option as an exception: none of the code below applies
  412. * to this case, and sending the packet needs to be done differently too. */
  413. if (pcb->flags & RAW_FLAGS_HDRINCL) {
  414. /* A full header *must* be present in the first pbuf of the chain, as the
  415. * output routines may access its fields directly. */
  416. if (p->len < header_size) {
  417. return ERR_VAL;
  418. }
  419. /* @todo multicast loop support, if at all desired for this scenario.. */
  420. NETIF_SET_HINTS(netif, &pcb->netif_hints);
  421. err = ip_output_if_hdrincl(p, src_ip, dst_ip, netif);
  422. NETIF_RESET_HINTS(netif);
  423. return err;
  424. }
  425. /* packet too large to add an IP header without causing an overflow? */
  426. if ((u16_t)(p->tot_len + header_size) < p->tot_len) {
  427. return ERR_MEM;
  428. }
  429. /* not enough space to add an IP header to first pbuf in given p chain? */
  430. if (pbuf_add_header(p, header_size)) {
  431. /* allocate header in new pbuf */
  432. q = pbuf_alloc(PBUF_IP, 0, PBUF_RAM);
  433. /* new header pbuf could not be allocated? */
  434. if (q == NULL) {
  435. LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("raw_sendto: could not allocate header\n"));
  436. return ERR_MEM;
  437. }
  438. if (p->tot_len != 0) {
  439. /* chain header q in front of given pbuf p */
  440. pbuf_chain(q, p);
  441. }
  442. /* { first pbuf q points to header pbuf } */
  443. LWIP_DEBUGF(RAW_DEBUG, ("raw_sendto: added header pbuf %p before given pbuf %p\n", (void *)q, (void *)p));
  444. } else {
  445. /* first pbuf q equals given pbuf */
  446. q = p;
  447. if (pbuf_remove_header(q, header_size)) {
  448. LWIP_ASSERT("Can't restore header we just removed!", 0);
  449. return ERR_MEM;
  450. }
  451. }
  452. #if IP_SOF_BROADCAST
  453. if (IP_IS_V4(dst_ip)) {
  454. /* broadcast filter? */
  455. if (!ip_get_option(pcb, SOF_BROADCAST) && ip_addr_isbroadcast(dst_ip, netif)) {
  456. LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_LEVEL_WARNING, ("raw_sendto: SOF_BROADCAST not enabled on pcb %p\n", (void *)pcb));
  457. /* free any temporary header pbuf allocated by pbuf_header() */
  458. if (q != p) {
  459. pbuf_free(q);
  460. }
  461. return ERR_VAL;
  462. }
  463. }
  464. #endif /* IP_SOF_BROADCAST */
  465. /* Multicast Loop? */
  466. #if LWIP_MULTICAST_TX_OPTIONS
  467. if (((pcb->flags & RAW_FLAGS_MULTICAST_LOOP) != 0) && ip_addr_ismulticast(dst_ip)) {
  468. q->flags |= PBUF_FLAG_MCASTLOOP;
  469. }
  470. #endif /* LWIP_MULTICAST_TX_OPTIONS */
  471. #if LWIP_IPV6
  472. /* If requested, based on the IPV6_CHECKSUM socket option per RFC3542,
  473. compute the checksum and update the checksum in the payload. */
  474. if (IP_IS_V6(dst_ip) && pcb->chksum_reqd) {
  475. u16_t chksum = ip6_chksum_pseudo(p, pcb->protocol, p->tot_len, ip_2_ip6(src_ip), ip_2_ip6(dst_ip));
  476. LWIP_ASSERT("Checksum must fit into first pbuf", p->len >= (pcb->chksum_offset + 2));
  477. SMEMCPY(((u8_t *)p->payload) + pcb->chksum_offset, &chksum, sizeof(u16_t));
  478. }
  479. #endif
  480. /* Determine TTL to use */
  481. #if LWIP_MULTICAST_TX_OPTIONS
  482. ttl = (ip_addr_ismulticast(dst_ip) ? raw_get_multicast_ttl(pcb) : pcb->ttl);
  483. #else /* LWIP_MULTICAST_TX_OPTIONS */
  484. ttl = pcb->ttl;
  485. #endif /* LWIP_MULTICAST_TX_OPTIONS */
  486. NETIF_SET_HINTS(netif, &pcb->netif_hints);
  487. err = ip_output_if(q, src_ip, dst_ip, ttl, pcb->tos, pcb->protocol, netif);
  488. NETIF_RESET_HINTS(netif);
  489. /* did we chain a header earlier? */
  490. if (q != p) {
  491. /* free the header */
  492. pbuf_free(q);
  493. }
  494. return err;
  495. }
  496. /**
  497. * @ingroup raw_raw
  498. * Send the raw IP packet to the address given by raw_connect()
  499. *
  500. * @param pcb the raw pcb which to send
  501. * @param p the IP payload to send
  502. *
  503. */
  504. err_t
  505. raw_send(struct raw_pcb *pcb, struct pbuf *p)
  506. {
  507. return raw_sendto(pcb, p, &pcb->remote_ip);
  508. }
  509. /**
  510. * @ingroup raw_raw
  511. * Remove an RAW PCB.
  512. *
  513. * @param pcb RAW PCB to be removed. The PCB is removed from the list of
  514. * RAW PCB's and the data structure is freed from memory.
  515. *
  516. * @see raw_new()
  517. */
  518. void
  519. raw_remove(struct raw_pcb *pcb)
  520. {
  521. struct raw_pcb *pcb2;
  522. LWIP_ASSERT_CORE_LOCKED();
  523. /* pcb to be removed is first in list? */
  524. if (raw_pcbs == pcb) {
  525. /* make list start at 2nd pcb */
  526. raw_pcbs = raw_pcbs->next;
  527. /* pcb not 1st in list */
  528. } else {
  529. for (pcb2 = raw_pcbs; pcb2 != NULL; pcb2 = pcb2->next) {
  530. /* find pcb in raw_pcbs list */
  531. if (pcb2->next != NULL && pcb2->next == pcb) {
  532. /* remove pcb from list */
  533. pcb2->next = pcb->next;
  534. break;
  535. }
  536. }
  537. }
  538. memp_free(MEMP_RAW_PCB, pcb);
  539. }
  540. /**
  541. * @ingroup raw_raw
  542. * Create a RAW PCB.
  543. *
  544. * @return The RAW PCB which was created. NULL if the PCB data structure
  545. * could not be allocated.
  546. *
  547. * @param proto the protocol number of the IPs payload (e.g. IP_PROTO_ICMP)
  548. *
  549. * @see raw_remove()
  550. */
  551. struct raw_pcb *
  552. raw_new(u8_t proto)
  553. {
  554. struct raw_pcb *pcb;
  555. LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE, ("raw_new\n"));
  556. LWIP_ASSERT_CORE_LOCKED();
  557. pcb = (struct raw_pcb *)memp_malloc(MEMP_RAW_PCB);
  558. /* could allocate RAW PCB? */
  559. if (pcb != NULL) {
  560. /* initialize PCB to all zeroes */
  561. memset(pcb, 0, sizeof(struct raw_pcb));
  562. pcb->protocol = proto;
  563. pcb->ttl = RAW_TTL;
  564. #if LWIP_MULTICAST_TX_OPTIONS
  565. raw_set_multicast_ttl(pcb, RAW_TTL);
  566. #endif /* LWIP_MULTICAST_TX_OPTIONS */
  567. pcb->next = raw_pcbs;
  568. raw_pcbs = pcb;
  569. }
  570. return pcb;
  571. }
  572. /**
  573. * @ingroup raw_raw
  574. * Create a RAW PCB for specific IP type.
  575. *
  576. * @return The RAW PCB which was created. NULL if the PCB data structure
  577. * could not be allocated.
  578. *
  579. * @param type IP address type, see @ref lwip_ip_addr_type definitions.
  580. * If you want to listen to IPv4 and IPv6 (dual-stack) packets,
  581. * supply @ref IPADDR_TYPE_ANY as argument and bind to @ref IP_ANY_TYPE.
  582. * @param proto the protocol number (next header) of the IPv6 packet payload
  583. * (e.g. IP6_NEXTH_ICMP6)
  584. *
  585. * @see raw_remove()
  586. */
  587. struct raw_pcb *
  588. raw_new_ip_type(u8_t type, u8_t proto)
  589. {
  590. struct raw_pcb *pcb;
  591. LWIP_ASSERT_CORE_LOCKED();
  592. pcb = raw_new(proto);
  593. #if LWIP_IPV4 && LWIP_IPV6
  594. if (pcb != NULL) {
  595. IP_SET_TYPE_VAL(pcb->local_ip, type);
  596. IP_SET_TYPE_VAL(pcb->remote_ip, type);
  597. }
  598. #else /* LWIP_IPV4 && LWIP_IPV6 */
  599. LWIP_UNUSED_ARG(type);
  600. #endif /* LWIP_IPV4 && LWIP_IPV6 */
  601. return pcb;
  602. }
  603. /** This function is called from netif.c when address is changed
  604. *
  605. * @param old_addr IP address of the netif before change
  606. * @param new_addr IP address of the netif after change
  607. */
  608. void raw_netif_ip_addr_changed(const ip_addr_t *old_addr, const ip_addr_t *new_addr)
  609. {
  610. struct raw_pcb *rpcb;
  611. if (!ip_addr_isany(old_addr) && !ip_addr_isany(new_addr)) {
  612. for (rpcb = raw_pcbs; rpcb != NULL; rpcb = rpcb->next) {
  613. /* PCB bound to current local interface address? */
  614. if (ip_addr_cmp(&rpcb->local_ip, old_addr)) {
  615. /* The PCB is bound to the old ipaddr and
  616. * is set to bound to the new one instead */
  617. ip_addr_copy(rpcb->local_ip, *new_addr);
  618. }
  619. }
  620. }
  621. }
  622. #endif /* LWIP_RAW */