STM32F769IDiscovery  1.00
uDANTE Audio Networking with STM32F7 DISCO board
mld6.c
Go to the documentation of this file.
1 
8 /*
9  * Copyright (c) 2010 Inico Technologies Ltd.
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: Ivan Delamer <delamer@inicotech.com>
37  *
38  *
39  * Please coordinate changes and requests with Ivan Delamer
40  * <delamer@inicotech.com>
41  */
42 
43 /* Based on igmp.c implementation of igmp v2 protocol */
44 
45 #include "lwip/opt.h"
46 
47 #if LWIP_IPV6 && LWIP_IPV6_MLD /* don't build if not configured for use in lwipopts.h */
48 
49 #include "lwip/mld6.h"
50 #include "lwip/icmp6.h"
51 #include "lwip/ip6.h"
52 #include "lwip/ip6_addr.h"
53 #include "lwip/ip.h"
54 #include "lwip/inet_chksum.h"
55 #include "lwip/pbuf.h"
56 #include "lwip/netif.h"
57 #include "lwip/memp.h"
58 #include "lwip/stats.h"
59 
60 #include <string.h>
61 
62 
63 /*
64  * MLD constants
65  */
66 #define MLD6_HL 1
67 #define MLD6_JOIN_DELAYING_MEMBER_TMR_MS (500)
68 
69 #define MLD6_GROUP_NON_MEMBER 0
70 #define MLD6_GROUP_DELAYING_MEMBER 1
71 #define MLD6_GROUP_IDLE_MEMBER 2
72 
73 
74 /* The list of joined groups. */
75 static struct mld_group* mld_group_list;
76 
77 
78 /* Forward declarations. */
79 static struct mld_group * mld6_new_group(struct netif *ifp, const ip6_addr_t *addr);
80 static err_t mld6_free_group(struct mld_group *group);
81 static void mld6_delayed_report(struct mld_group *group, u16_t maxresp);
82 static void mld6_send(struct mld_group *group, u8_t type);
83 
84 
90 err_t
91 mld6_stop(struct netif *netif)
92 {
93  struct mld_group *group = mld_group_list;
94  struct mld_group *prev = NULL;
95  struct mld_group *next;
96 
97  /* look for groups joined on this interface further down the list */
98  while (group != NULL) {
99  next = group->next;
100  /* is it a group joined on this interface? */
101  if (group->netif == netif) {
102  /* is it the first group of the list? */
103  if (group == mld_group_list) {
104  mld_group_list = next;
105  }
106  /* is there a "previous" group defined? */
107  if (prev != NULL) {
108  prev->next = next;
109  }
110  /* disable the group at the MAC level */
111  if (netif->mld_mac_filter != NULL) {
112  netif->mld_mac_filter(netif, &(group->group_address), MLD6_DEL_MAC_FILTER);
113  }
114  /* free group */
115  memp_free(MEMP_MLD6_GROUP, group);
116  } else {
117  /* change the "previous" */
118  prev = group;
119  }
120  /* move to "next" */
121  group = next;
122  }
123  return ERR_OK;
124 }
125 
131 void
132 mld6_report_groups(struct netif *netif)
133 {
134  struct mld_group *group = mld_group_list;
135 
136  while (group != NULL) {
137  if (group->netif == netif) {
138  mld6_delayed_report(group, MLD6_JOIN_DELAYING_MEMBER_TMR_MS);
139  }
140  group = group->next;
141  }
142 }
143 
152 struct mld_group *
153 mld6_lookfor_group(struct netif *ifp, const ip6_addr_t *addr)
154 {
155  struct mld_group *group = mld_group_list;
156 
157  while (group != NULL) {
158  if ((group->netif == ifp) && (ip6_addr_cmp(&(group->group_address), addr))) {
159  return group;
160  }
161  group = group->next;
162  }
163 
164  return NULL;
165 }
166 
167 
176 static struct mld_group *
177 mld6_new_group(struct netif *ifp, const ip6_addr_t *addr)
178 {
179  struct mld_group *group;
180 
181  group = (struct mld_group *)memp_malloc(MEMP_MLD6_GROUP);
182  if (group != NULL) {
183  group->netif = ifp;
184  ip6_addr_set(&(group->group_address), addr);
185  group->timer = 0; /* Not running */
186  group->group_state = MLD6_GROUP_IDLE_MEMBER;
187  group->last_reporter_flag = 0;
188  group->use = 0;
189  group->next = mld_group_list;
190 
191  mld_group_list = group;
192  }
193 
194  return group;
195 }
196 
203 static err_t
204 mld6_free_group(struct mld_group *group)
205 {
206  err_t err = ERR_OK;
207 
208  /* Is it the first group? */
209  if (mld_group_list == group) {
210  mld_group_list = group->next;
211  } else {
212  /* look for group further down the list */
213  struct mld_group *tmpGroup;
214  for (tmpGroup = mld_group_list; tmpGroup != NULL; tmpGroup = tmpGroup->next) {
215  if (tmpGroup->next == group) {
216  tmpGroup->next = group->next;
217  break;
218  }
219  }
220  /* Group not find group */
221  if (tmpGroup == NULL) {
222  err = ERR_ARG;
223  }
224  }
225  /* free group */
226  memp_free(MEMP_MLD6_GROUP, group);
227 
228  return err;
229 }
230 
231 
238 void
239 mld6_input(struct pbuf *p, struct netif *inp)
240 {
241  struct mld_header * mld_hdr;
242  struct mld_group* group;
243 
244  MLD6_STATS_INC(mld6.recv);
245 
246  /* Check that mld header fits in packet. */
247  if (p->len < sizeof(struct mld_header)) {
248  /* TODO debug message */
249  pbuf_free(p);
250  MLD6_STATS_INC(mld6.lenerr);
251  MLD6_STATS_INC(mld6.drop);
252  return;
253  }
254 
255  mld_hdr = (struct mld_header *)p->payload;
256 
257  switch (mld_hdr->type) {
258  case ICMP6_TYPE_MLQ: /* Multicast listener query. */
259  /* Is it a general query? */
260  if (ip6_addr_isallnodes_linklocal(ip6_current_dest_addr()) &&
261  ip6_addr_isany(&(mld_hdr->multicast_address))) {
262  MLD6_STATS_INC(mld6.rx_general);
263  /* Report all groups, except all nodes group, and if-local groups. */
264  group = mld_group_list;
265  while (group != NULL) {
266  if ((group->netif == inp) &&
267  (!(ip6_addr_ismulticast_iflocal(&(group->group_address)))) &&
268  (!(ip6_addr_isallnodes_linklocal(&(group->group_address))))) {
269  mld6_delayed_report(group, mld_hdr->max_resp_delay);
270  }
271  group = group->next;
272  }
273  } else {
274  /* Have we joined this group?
275  * We use IP6 destination address to have a memory aligned copy.
276  * mld_hdr->multicast_address should be the same. */
277  MLD6_STATS_INC(mld6.rx_group);
278  group = mld6_lookfor_group(inp, ip6_current_dest_addr());
279  if (group != NULL) {
280  /* Schedule a report. */
281  mld6_delayed_report(group, mld_hdr->max_resp_delay);
282  }
283  }
284  break; /* ICMP6_TYPE_MLQ */
285  case ICMP6_TYPE_MLR: /* Multicast listener report. */
286  /* Have we joined this group?
287  * We use IP6 destination address to have a memory aligned copy.
288  * mld_hdr->multicast_address should be the same. */
289  MLD6_STATS_INC(mld6.rx_report);
290  group = mld6_lookfor_group(inp, ip6_current_dest_addr());
291  if (group != NULL) {
292  /* If we are waiting to report, cancel it. */
293  if (group->group_state == MLD6_GROUP_DELAYING_MEMBER) {
294  group->timer = 0; /* stopped */
295  group->group_state = MLD6_GROUP_IDLE_MEMBER;
296  group->last_reporter_flag = 0;
297  }
298  }
299  break; /* ICMP6_TYPE_MLR */
300  case ICMP6_TYPE_MLD: /* Multicast listener done. */
301  /* Do nothing, router will query us. */
302  break; /* ICMP6_TYPE_MLD */
303  default:
304  MLD6_STATS_INC(mld6.proterr);
305  MLD6_STATS_INC(mld6.drop);
306  break;
307  }
308 
309  pbuf_free(p);
310 }
311 
320 err_t
321 mld6_joingroup(const ip6_addr_t *srcaddr, const ip6_addr_t *groupaddr)
322 {
323  err_t err = ERR_VAL; /* no matching interface */
324  struct netif *netif;
325 
326  /* loop through netif's */
327  netif = netif_list;
328  while (netif != NULL) {
329  /* Should we join this interface ? */
330  if (ip6_addr_isany(srcaddr) ||
331  netif_get_ip6_addr_match(netif, srcaddr) >= 0) {
332  err = mld6_joingroup_netif(netif, groupaddr);
333  if (err != ERR_OK) {
334  return err;
335  }
336  }
337 
338  /* proceed to next network interface */
339  netif = netif->next;
340  }
341 
342  return err;
343 }
344 
352 err_t
353 mld6_joingroup_netif(struct netif *netif, const ip6_addr_t *groupaddr)
354 {
355  struct mld_group *group;
356 
357  /* find group or create a new one if not found */
358  group = mld6_lookfor_group(netif, groupaddr);
359 
360  if (group == NULL) {
361  /* Joining a new group. Create a new group entry. */
362  group = mld6_new_group(netif, groupaddr);
363  if (group == NULL) {
364  return ERR_MEM;
365  }
366 
367  /* Activate this address on the MAC layer. */
368  if (netif->mld_mac_filter != NULL) {
369  netif->mld_mac_filter(netif, groupaddr, MLD6_ADD_MAC_FILTER);
370  }
371 
372  /* Report our membership. */
373  MLD6_STATS_INC(mld6.tx_report);
374  mld6_send(group, ICMP6_TYPE_MLR);
375  mld6_delayed_report(group, MLD6_JOIN_DELAYING_MEMBER_TMR_MS);
376  }
377 
378  /* Increment group use */
379  group->use++;
380  return ERR_OK;
381 }
382 
391 err_t
392 mld6_leavegroup(const ip6_addr_t *srcaddr, const ip6_addr_t *groupaddr)
393 {
394  err_t err = ERR_VAL; /* no matching interface */
395  struct netif *netif;
396 
397  /* loop through netif's */
398  netif = netif_list;
399  while (netif != NULL) {
400  /* Should we leave this interface ? */
401  if (ip6_addr_isany(srcaddr) ||
402  netif_get_ip6_addr_match(netif, srcaddr) >= 0) {
403  err_t res = mld6_leavegroup_netif(netif, groupaddr);
404  if (err != ERR_OK) {
405  /* Store this result if we have not yet gotten a success */
406  err = res;
407  }
408  }
409  /* proceed to next network interface */
410  netif = netif->next;
411  }
412 
413  return err;
414 }
415 
423 err_t
424 mld6_leavegroup_netif(struct netif *netif, const ip6_addr_t *groupaddr)
425 {
426  struct mld_group *group;
427 
428  /* find group */
429  group = mld6_lookfor_group(netif, groupaddr);
430 
431  if (group != NULL) {
432  /* Leave if there is no other use of the group */
433  if (group->use <= 1) {
434  /* If we are the last reporter for this group */
435  if (group->last_reporter_flag) {
436  MLD6_STATS_INC(mld6.tx_leave);
437  mld6_send(group, ICMP6_TYPE_MLD);
438  }
439 
440  /* Disable the group at the MAC level */
441  if (netif->mld_mac_filter != NULL) {
442  netif->mld_mac_filter(netif, groupaddr, MLD6_DEL_MAC_FILTER);
443  }
444 
445  /* Free the group */
446  mld6_free_group(group);
447  } else {
448  /* Decrement group use */
449  group->use--;
450  }
451 
452  /* Left group */
453  return ERR_OK;
454  }
455 
456  /* Group not found */
457  return ERR_VAL;
458 }
459 
460 
467 void
468 mld6_tmr(void)
469 {
470  struct mld_group *group = mld_group_list;
471 
472  while (group != NULL) {
473  if (group->timer > 0) {
474  group->timer--;
475  if (group->timer == 0) {
476  /* If the state is MLD6_GROUP_DELAYING_MEMBER then we send a report for this group */
477  if (group->group_state == MLD6_GROUP_DELAYING_MEMBER) {
478  MLD6_STATS_INC(mld6.tx_report);
479  mld6_send(group, ICMP6_TYPE_MLR);
480  group->group_state = MLD6_GROUP_IDLE_MEMBER;
481  }
482  }
483  }
484  group = group->next;
485  }
486 }
487 
495 static void
496 mld6_delayed_report(struct mld_group *group, u16_t maxresp)
497 {
498  /* Convert maxresp from milliseconds to tmr ticks */
499  maxresp = maxresp / MLD6_TMR_INTERVAL;
500  if (maxresp == 0) {
501  maxresp = 1;
502  }
503 
504 #ifdef LWIP_RAND
505  /* Randomize maxresp. (if LWIP_RAND is supported) */
506  maxresp = LWIP_RAND() % maxresp;
507  if (maxresp == 0) {
508  maxresp = 1;
509  }
510 #endif /* LWIP_RAND */
511 
512  /* Apply timer value if no report has been scheduled already. */
513  if ((group->group_state == MLD6_GROUP_IDLE_MEMBER) ||
514  ((group->group_state == MLD6_GROUP_DELAYING_MEMBER) &&
515  ((group->timer == 0) || (maxresp < group->timer)))) {
516  group->timer = maxresp;
517  group->group_state = MLD6_GROUP_DELAYING_MEMBER;
518  }
519 }
520 
530 static void
531 mld6_send(struct mld_group *group, u8_t type)
532 {
533  struct mld_header * mld_hdr;
534  struct pbuf * p;
535  const ip6_addr_t * src_addr;
536 
537  /* Allocate a packet. Size is MLD header + IPv6 Hop-by-hop options header. */
538  p = pbuf_alloc(PBUF_IP, sizeof(struct mld_header) + sizeof(struct ip6_hbh_hdr), PBUF_RAM);
539  if (p == NULL) {
540  MLD6_STATS_INC(mld6.memerr);
541  return;
542  }
543 
544  /* Move to make room for Hop-by-hop options header. */
545  if (pbuf_header(p, -IP6_HBH_HLEN)) {
546  pbuf_free(p);
547  MLD6_STATS_INC(mld6.lenerr);
548  return;
549  }
550 
551  /* Select our source address. */
552  if (!ip6_addr_isvalid(netif_ip6_addr_state(group->netif, 0))) {
553  /* This is a special case, when we are performing duplicate address detection.
554  * We must join the multicast group, but we don't have a valid address yet. */
555  src_addr = IP6_ADDR_ANY6;
556  } else {
557  /* Use link-local address as source address. */
558  src_addr = netif_ip6_addr(group->netif, 0);
559  }
560 
561  /* MLD message header pointer. */
562  mld_hdr = (struct mld_header *)p->payload;
563 
564  /* Set fields. */
565  mld_hdr->type = type;
566  mld_hdr->code = 0;
567  mld_hdr->chksum = 0;
568  mld_hdr->max_resp_delay = 0;
569  mld_hdr->reserved = 0;
570  ip6_addr_set(&(mld_hdr->multicast_address), &(group->group_address));
571 
572 #if CHECKSUM_GEN_ICMP6
573  IF__NETIF_CHECKSUM_ENABLED(group->netif, NETIF_CHECKSUM_GEN_ICMP6) {
574  mld_hdr->chksum = ip6_chksum_pseudo(p, IP6_NEXTH_ICMP6, p->len,
575  src_addr, &(group->group_address));
576  }
577 #endif /* CHECKSUM_GEN_ICMP6 */
578 
579  /* Add hop-by-hop headers options: router alert with MLD value. */
580  ip6_options_add_hbh_ra(p, IP6_NEXTH_ICMP6, IP6_ROUTER_ALERT_VALUE_MLD);
581 
582  /* Send the packet out. */
583  MLD6_STATS_INC(mld6.xmit);
584  ip6_output_if(p, (ip6_addr_isany(src_addr)) ? NULL : src_addr, &(group->group_address),
585  MLD6_HL, 0, IP6_NEXTH_HOPBYHOP, group->netif);
586  pbuf_free(p);
587 }
588 
589 #endif /* LWIP_IPV6 */
590 
struct netif * netif_list
Definition: netif.c:84
u8_t pbuf_header(struct pbuf *p, s16_t header_size_increment)
Definition: pbuf.c:603
#define ERR_VAL
Definition: err.h:58
#define MLD6_STATS_INC(x)
Definition: stats.h:392
struct netif * next
Definition: netif.h:184
#define IF__NETIF_CHECKSUM_ENABLED(netif, chksumflag)
Definition: netif.h:313
void memp_free(memp_t type, void *mem)
Definition: memp.c:399
u16_t len
Definition: pbuf.h:125
struct pbuf * pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type)
Definition: pbuf.c:199
#define LWIP_RAND()
Definition: cc.h:95
Definition: pbuf.h:77
#define NULL
Definition: usbd_def.h:53
u8_t pbuf_free(struct pbuf *p)
Definition: pbuf.c:652
#define ERR_OK
Definition: err.h:52
Definition: pbuf.h:108
s8_t err_t
Definition: err.h:47
Definition: netif.h:182
#define ERR_ARG
Definition: err.h:71
unsigned char u8_t
Definition: cc.h:38
Definition: pbuf.h:65
void * memp_malloc(memp_t type)
Definition: memp.c:303
#define ERR_MEM
Definition: err.h:53
unsigned short u16_t
Definition: cc.h:40
void * payload
Definition: pbuf.h:113