1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
/****************************************************************************
* net/igmp/igmp_send.c
*
* Copyright (C) 2010 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <debug.h>
#include <arpa/inet.h>
#include <nuttx/net/netconfig.h>
#include <nuttx/net/netdev.h>
#include <nuttx/net/netstats.h>
#include <nuttx/net/ipopt.h>
#include <nuttx/net/igmp.h>
#include "devif/devif.h"
#include "igmp/igmp.h"
#ifdef CONFIG_NET_IGMP
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* Debug */
#undef IGMP_DUMPPKT /* Define to enable packet dump */
#ifndef CONFIG_DEBUG_NET
# undef IGMP_DUMPPKT
#endif
#ifdef IGMP_DUMPPKT
# define igmp_dumppkt(b,n) lib_dumpbuffer("IGMP", (FAR const uint8_t*)(b), (n))
#else
# define igmp_dumppkt(b,n)
#endif
/* Buffer layout */
#define RASIZE (4)
#define IGMPBUF ((struct igmp_iphdr_s *)&dev->d_buf[NET_LLH_LEN])
/****************************************************************************
* Public Variables
****************************************************************************/
/****************************************************************************
* Private Variables
****************************************************************************/
/****************************************************************************
* Private Functions
****************************************************************************/
static uint16_t igmp_chksum(FAR uint8_t *buffer, int buflen)
{
uint16_t sum = net_chksum((FAR uint16_t*)buffer, buflen);
return sum ? sum : 0xffff;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: igmp_send
*
* Description:
* Sends an IGMP IP packet on a network interface. This function constructs
* the IP header and calculates the IP header checksum.
*
* Parameters:
* dev - The device driver structure to use in the send operation.
* group - Describes the multicast group member and identifies the
* message to be sent.
* destipaddr - The IP address of the recipient of the message
*
* Return:
* None
*
* Assumptions:
* Called from the interrupt level or with interrupts disabled.
*
****************************************************************************/
void igmp_send(FAR struct net_driver_s *dev, FAR struct igmp_group_s *group,
FAR net_ipaddr_t *destipaddr)
{
nllvdbg("msgid: %02x destipaddr: %08x\n", group->msgid, (int)*destipaddr);
/* The total length to send is the size of the IP and IGMP headers and 4
* bytes for the ROUTER ALERT (and, eventually, the Ethernet header)
*/
dev->d_len = UIP_IPIGMPH_LEN;
/* The total size of the data is the size of the IGMP header */
dev->d_sndlen = UIP_IGMPH_LEN;
/* Add the router alert option */
IGMPBUF->ra[0] = HTONS(IPOPT_RA >> 16);
IGMPBUF->ra[1] = HTONS(IPOPT_RA & 0xffff);
/* Initialize the IPv4 header */
IGMPBUF->vhl = 0x46; /* 4->IP; 6->24 bytes */
IGMPBUF->tos = 0;
IGMPBUF->len[0] = (dev->d_len >> 8);
IGMPBUF->len[1] = (dev->d_len & 0xff);
++g_ipid;
IGMPBUF->ipid[0] = g_ipid >> 8;
IGMPBUF->ipid[1] = g_ipid & 0xff;
IGMPBUF->ipoffset[0] = UIP_TCPFLAG_DONTFRAG >> 8;
IGMPBUF->ipoffset[1] = UIP_TCPFLAG_DONTFRAG & 0xff;
IGMPBUF->ttl = IGMP_TTL;
IGMPBUF->proto = UIP_PROTO_IGMP;
net_ipaddr_hdrcopy(IGMPBUF->srcipaddr, &dev->d_ipaddr);
net_ipaddr_hdrcopy(IGMPBUF->destipaddr, destipaddr);
/* Calculate IP checksum. */
IGMPBUF->ipchksum = 0;
IGMPBUF->ipchksum = ~igmp_chksum((FAR uint8_t *)IGMPBUF, IPHDR_LEN + RASIZE);
/* Set up the IGMP message */
IGMPBUF->type = group->msgid;
IGMPBUF->maxresp = 0;
net_ipaddr_hdrcopy(IGMPBUF->grpaddr, &group->grpaddr);
/* Calculate the IGMP checksum. */
IGMPBUF->chksum = 0;
IGMPBUF->chksum = ~igmp_chksum(&IGMPBUF->type, UIP_IPIGMPH_LEN);
IGMP_STATINCR(g_netstats.igmp.poll_send);
IGMP_STATINCR(g_netstats.ip.sent);
nllvdbg("Outgoing IGMP packet length: %d (%d)\n",
dev->d_len, (IGMPBUF->len[0] << 8) | IGMPBUF->len[1]);
igmp_dumppkt(RA, UIP_IPIGMPH_LEN + RASIZE);
}
#endif /* CONFIG_NET_IGMP */
|