Deleted Added
full compact
sctp_bsd_addr.c (235828) sctp_bsd_addr.c (237540)
1/*-
2 * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
3 * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
4 * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * a) Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 *
12 * b) Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the distribution.
15 *
16 * c) Neither the name of Cisco Systems, Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
3 * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
4 * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * a) Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 *
12 * b) Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the distribution.
15 *
16 * c) Neither the name of Cisco Systems, Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include <sys/cdefs.h>
34__FBSDID("$FreeBSD: head/sys/netinet/sctp_bsd_addr.c 235828 2012-05-23 11:26:28Z tuexen $");
34__FBSDID("$FreeBSD: head/sys/netinet/sctp_bsd_addr.c 237540 2012-06-24 21:25:54Z tuexen $");
35
36#include <netinet/sctp_os.h>
37#include <netinet/sctp_var.h>
38#include <netinet/sctp_pcb.h>
39#include <netinet/sctp_header.h>
40#include <netinet/sctputil.h>
41#include <netinet/sctp_output.h>
42#include <netinet/sctp_bsd_addr.h>
43#include <netinet/sctp_uio.h>
44#include <netinet/sctputil.h>
45#include <netinet/sctp_timer.h>
46#include <netinet/sctp_asconf.h>
47#include <netinet/sctp_sysctl.h>
48#include <netinet/sctp_indata.h>
49#include <sys/unistd.h>
50
51/* Declare all of our malloc named types */
52MALLOC_DEFINE(SCTP_M_MAP, "sctp_map", "sctp asoc map descriptor");
53MALLOC_DEFINE(SCTP_M_STRMI, "sctp_stri", "sctp stream in array");
54MALLOC_DEFINE(SCTP_M_STRMO, "sctp_stro", "sctp stream out array");
55MALLOC_DEFINE(SCTP_M_ASC_ADDR, "sctp_aadr", "sctp asconf address");
56MALLOC_DEFINE(SCTP_M_ASC_IT, "sctp_a_it", "sctp asconf iterator");
57MALLOC_DEFINE(SCTP_M_AUTH_CL, "sctp_atcl", "sctp auth chunklist");
58MALLOC_DEFINE(SCTP_M_AUTH_KY, "sctp_atky", "sctp auth key");
59MALLOC_DEFINE(SCTP_M_AUTH_HL, "sctp_athm", "sctp auth hmac list");
60MALLOC_DEFINE(SCTP_M_AUTH_IF, "sctp_athi", "sctp auth info");
61MALLOC_DEFINE(SCTP_M_STRESET, "sctp_stre", "sctp stream reset");
62MALLOC_DEFINE(SCTP_M_CMSG, "sctp_cmsg", "sctp CMSG buffer");
63MALLOC_DEFINE(SCTP_M_COPYAL, "sctp_cpal", "sctp copy all");
64MALLOC_DEFINE(SCTP_M_VRF, "sctp_vrf", "sctp vrf struct");
65MALLOC_DEFINE(SCTP_M_IFA, "sctp_ifa", "sctp ifa struct");
66MALLOC_DEFINE(SCTP_M_IFN, "sctp_ifn", "sctp ifn struct");
67MALLOC_DEFINE(SCTP_M_TIMW, "sctp_timw", "sctp time block");
68MALLOC_DEFINE(SCTP_M_MVRF, "sctp_mvrf", "sctp mvrf pcb list");
69MALLOC_DEFINE(SCTP_M_ITER, "sctp_iter", "sctp iterator control");
70MALLOC_DEFINE(SCTP_M_SOCKOPT, "sctp_socko", "sctp socket option");
71MALLOC_DEFINE(SCTP_M_MCORE, "sctp_mcore", "sctp mcore queue");
72
73/* Global NON-VNET structure that controls the iterator */
74struct iterator_control sctp_it_ctl;
75
76
77void
78sctp_wakeup_iterator(void)
79{
80 wakeup(&sctp_it_ctl.iterator_running);
81}
82
83static void
84sctp_iterator_thread(void *v SCTP_UNUSED)
85{
86 SCTP_IPI_ITERATOR_WQ_LOCK();
87 /* In FreeBSD this thread never terminates. */
88 for (;;) {
89 msleep(&sctp_it_ctl.iterator_running,
90 &sctp_it_ctl.ipi_iterator_wq_mtx,
91 0, "waiting_for_work", 0);
92 sctp_iterator_worker();
93 }
94}
95
96void
97sctp_startup_iterator(void)
98{
99 static int called = 0;
100 int ret;
101
102 if (called) {
103 /* You only get one */
104 return;
105 }
106 /* init the iterator head */
107 called = 1;
108 sctp_it_ctl.iterator_running = 0;
109 sctp_it_ctl.iterator_flags = 0;
110 sctp_it_ctl.cur_it = NULL;
111 SCTP_ITERATOR_LOCK_INIT();
112 SCTP_IPI_ITERATOR_WQ_INIT();
113 TAILQ_INIT(&sctp_it_ctl.iteratorhead);
114 ret = kproc_create(sctp_iterator_thread,
115 (void *)NULL,
116 &sctp_it_ctl.thread_proc,
117 RFPROC,
118 SCTP_KTHREAD_PAGES,
119 SCTP_KTRHEAD_NAME);
120}
121
122#ifdef INET6
123
124void
125sctp_gather_internal_ifa_flags(struct sctp_ifa *ifa)
126{
127 struct in6_ifaddr *ifa6;
128
129 ifa6 = (struct in6_ifaddr *)ifa->ifa;
130 ifa->flags = ifa6->ia6_flags;
131 if (!MODULE_GLOBAL(ip6_use_deprecated)) {
132 if (ifa->flags &
133 IN6_IFF_DEPRECATED) {
134 ifa->localifa_flags |= SCTP_ADDR_IFA_UNUSEABLE;
135 } else {
136 ifa->localifa_flags &= ~SCTP_ADDR_IFA_UNUSEABLE;
137 }
138 } else {
139 ifa->localifa_flags &= ~SCTP_ADDR_IFA_UNUSEABLE;
140 }
141 if (ifa->flags &
142 (IN6_IFF_DETACHED |
143 IN6_IFF_ANYCAST |
144 IN6_IFF_NOTREADY)) {
145 ifa->localifa_flags |= SCTP_ADDR_IFA_UNUSEABLE;
146 } else {
147 ifa->localifa_flags &= ~SCTP_ADDR_IFA_UNUSEABLE;
148 }
149}
150
151#endif /* INET6 */
152
153
154static uint32_t
155sctp_is_desired_interface_type(struct ifaddr *ifa)
156{
157 int result;
158
159 /* check the interface type to see if it's one we care about */
160 switch (ifa->ifa_ifp->if_type) {
161 case IFT_ETHER:
162 case IFT_ISO88023:
163 case IFT_ISO88024:
164 case IFT_ISO88025:
165 case IFT_ISO88026:
166 case IFT_STARLAN:
167 case IFT_P10:
168 case IFT_P80:
169 case IFT_HY:
170 case IFT_FDDI:
171 case IFT_XETHER:
172 case IFT_ISDNBASIC:
173 case IFT_ISDNPRIMARY:
174 case IFT_PTPSERIAL:
175 case IFT_OTHER:
176 case IFT_PPP:
177 case IFT_LOOP:
178 case IFT_SLIP:
179 case IFT_GIF:
180 case IFT_L2VLAN:
181 case IFT_STF:
182 case IFT_IP:
183 case IFT_IPOVERCDLC:
184 case IFT_IPOVERCLAW:
185 case IFT_PROPVIRTUAL: /* NetGraph Virtual too */
186 case IFT_VIRTUALIPADDRESS:
187 result = 1;
188 break;
189 default:
190 result = 0;
191 }
192
193 return (result);
194}
195
196
197
198
199static void
200sctp_init_ifns_for_vrf(int vrfid)
201{
202 /*
203 * Here we must apply ANY locks needed by the IFN we access and also
204 * make sure we lock any IFA that exists as we float through the
205 * list of IFA's
206 */
207 struct ifnet *ifn;
208 struct ifaddr *ifa;
209 struct sctp_ifa *sctp_ifa;
210 uint32_t ifa_flags;
211
212#ifdef INET6
213 struct in6_ifaddr *ifa6;
214
215#endif
216
217 IFNET_RLOCK();
218 TAILQ_FOREACH(ifn, &MODULE_GLOBAL(ifnet), if_list) {
219 IF_ADDR_RLOCK(ifn);
220 TAILQ_FOREACH(ifa, &ifn->if_addrlist, ifa_list) {
221 if (ifa->ifa_addr == NULL) {
222 continue;
223 }
224 switch (ifa->ifa_addr->sa_family) {
225#ifdef INET
226 case AF_INET:
227 if (((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr == 0) {
228 continue;
229 }
230 break;
231#endif
232#ifdef INET6
233 case AF_INET6:
234 if (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr)) {
235 /* skip unspecifed addresses */
236 continue;
237 }
238 break;
239#endif
240 default:
241 continue;
242 }
243 if (sctp_is_desired_interface_type(ifa) == 0) {
244 /* non desired type */
245 continue;
246 }
247 switch (ifa->ifa_addr->sa_family) {
248#ifdef INET
249 case AF_INET:
250 ifa_flags = 0;
251 break;
252#endif
253#ifdef INET6
254 case AF_INET6:
255 ifa6 = (struct in6_ifaddr *)ifa;
256 ifa_flags = ifa6->ia6_flags;
257 break;
258#endif
259 default:
260 ifa_flags = 0;
261 break;
262 }
263 sctp_ifa = sctp_add_addr_to_vrf(vrfid,
264 (void *)ifn,
265 ifn->if_index,
266 ifn->if_type,
267 ifn->if_xname,
268 (void *)ifa,
269 ifa->ifa_addr,
270 ifa_flags,
271 0);
272 if (sctp_ifa) {
273 sctp_ifa->localifa_flags &= ~SCTP_ADDR_DEFER_USE;
274 }
275 }
276 IF_ADDR_RUNLOCK(ifn);
277 }
278 IFNET_RUNLOCK();
279}
280
281void
282sctp_init_vrf_list(int vrfid)
283{
284 if (vrfid > SCTP_MAX_VRF_ID)
285 /* can't do that */
286 return;
287
288 /* Don't care about return here */
289 (void)sctp_allocate_vrf(vrfid);
290
291 /*
292 * Now we need to build all the ifn's for this vrf and there
293 * addresses
294 */
295 sctp_init_ifns_for_vrf(vrfid);
296}
297
298void
299sctp_addr_change(struct ifaddr *ifa, int cmd)
300{
301 uint32_t ifa_flags = 0;
302
303 /*
304 * BSD only has one VRF, if this changes we will need to hook in the
305 * right things here to get the id to pass to the address managment
306 * routine.
307 */
308 if (SCTP_BASE_VAR(first_time) == 0) {
309 /* Special test to see if my ::1 will showup with this */
310 SCTP_BASE_VAR(first_time) = 1;
311 sctp_init_ifns_for_vrf(SCTP_DEFAULT_VRFID);
312 }
313 if ((cmd != RTM_ADD) && (cmd != RTM_DELETE)) {
314 /* don't know what to do with this */
315 return;
316 }
317 if (ifa->ifa_addr == NULL) {
318 return;
319 }
320 switch (ifa->ifa_addr->sa_family) {
321#ifdef INET
322 case AF_INET:
323 if (((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr == 0) {
324 return;
325 }
326 break;
327#endif
328#ifdef INET6
329 case AF_INET6:
330 ifa_flags = ((struct in6_ifaddr *)ifa)->ia6_flags;
331 if (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr)) {
332 /* skip unspecifed addresses */
333 return;
334 }
335 break;
336#endif
337 default:
338 /* non inet/inet6 skip */
339 return;
340 }
341
342 if (sctp_is_desired_interface_type(ifa) == 0) {
343 /* non desired type */
344 return;
345 }
346 if (cmd == RTM_ADD) {
347 (void)sctp_add_addr_to_vrf(SCTP_DEFAULT_VRFID, (void *)ifa->ifa_ifp,
348 ifa->ifa_ifp->if_index, ifa->ifa_ifp->if_type,
349 ifa->ifa_ifp->if_xname,
350 (void *)ifa, ifa->ifa_addr, ifa_flags, 1);
351 } else {
352
353 sctp_del_addr_from_vrf(SCTP_DEFAULT_VRFID, ifa->ifa_addr,
354 ifa->ifa_ifp->if_index,
355 ifa->ifa_ifp->if_xname
356 );
357 /*
358 * We don't bump refcount here so when it completes the
359 * final delete will happen.
360 */
361 }
362}
363
364void
365 sctp_add_or_del_interfaces(int (*pred) (struct ifnet *), int add){
366 struct ifnet *ifn;
367 struct ifaddr *ifa;
368
369 IFNET_RLOCK();
370 TAILQ_FOREACH(ifn, &MODULE_GLOBAL(ifnet), if_list) {
371 if (!(*pred) (ifn)) {
372 continue;
373 }
374 TAILQ_FOREACH(ifa, &ifn->if_addrlist, ifa_list) {
375 sctp_addr_change(ifa, add ? RTM_ADD : RTM_DELETE);
376 }
377 }
378 IFNET_RUNLOCK();
379}
380
381struct mbuf *
382sctp_get_mbuf_for_msg(unsigned int space_needed, int want_header,
383 int how, int allonebuf, int type)
384{
385 struct mbuf *m = NULL;
386
387 m = m_getm2(NULL, space_needed, how, type, want_header ? M_PKTHDR : 0);
388 if (m == NULL) {
389 /* bad, no memory */
390 return (m);
391 }
392 if (allonebuf) {
393 int siz;
394
395 if (SCTP_BUF_IS_EXTENDED(m)) {
396 siz = SCTP_BUF_EXTEND_SIZE(m);
397 } else {
398 if (want_header)
399 siz = MHLEN;
400 else
401 siz = MLEN;
402 }
403 if (siz < space_needed) {
404 m_freem(m);
405 return (NULL);
406 }
407 }
408 if (SCTP_BUF_NEXT(m)) {
409 sctp_m_freem(SCTP_BUF_NEXT(m));
410 SCTP_BUF_NEXT(m) = NULL;
411 }
412#ifdef SCTP_MBUF_LOGGING
413 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
414 if (SCTP_BUF_IS_EXTENDED(m)) {
415 sctp_log_mb(m, SCTP_MBUF_IALLOC);
416 }
417 }
418#endif
419 return (m);
420}
421
422
423#ifdef SCTP_PACKET_LOGGING
424void
35
36#include <netinet/sctp_os.h>
37#include <netinet/sctp_var.h>
38#include <netinet/sctp_pcb.h>
39#include <netinet/sctp_header.h>
40#include <netinet/sctputil.h>
41#include <netinet/sctp_output.h>
42#include <netinet/sctp_bsd_addr.h>
43#include <netinet/sctp_uio.h>
44#include <netinet/sctputil.h>
45#include <netinet/sctp_timer.h>
46#include <netinet/sctp_asconf.h>
47#include <netinet/sctp_sysctl.h>
48#include <netinet/sctp_indata.h>
49#include <sys/unistd.h>
50
51/* Declare all of our malloc named types */
52MALLOC_DEFINE(SCTP_M_MAP, "sctp_map", "sctp asoc map descriptor");
53MALLOC_DEFINE(SCTP_M_STRMI, "sctp_stri", "sctp stream in array");
54MALLOC_DEFINE(SCTP_M_STRMO, "sctp_stro", "sctp stream out array");
55MALLOC_DEFINE(SCTP_M_ASC_ADDR, "sctp_aadr", "sctp asconf address");
56MALLOC_DEFINE(SCTP_M_ASC_IT, "sctp_a_it", "sctp asconf iterator");
57MALLOC_DEFINE(SCTP_M_AUTH_CL, "sctp_atcl", "sctp auth chunklist");
58MALLOC_DEFINE(SCTP_M_AUTH_KY, "sctp_atky", "sctp auth key");
59MALLOC_DEFINE(SCTP_M_AUTH_HL, "sctp_athm", "sctp auth hmac list");
60MALLOC_DEFINE(SCTP_M_AUTH_IF, "sctp_athi", "sctp auth info");
61MALLOC_DEFINE(SCTP_M_STRESET, "sctp_stre", "sctp stream reset");
62MALLOC_DEFINE(SCTP_M_CMSG, "sctp_cmsg", "sctp CMSG buffer");
63MALLOC_DEFINE(SCTP_M_COPYAL, "sctp_cpal", "sctp copy all");
64MALLOC_DEFINE(SCTP_M_VRF, "sctp_vrf", "sctp vrf struct");
65MALLOC_DEFINE(SCTP_M_IFA, "sctp_ifa", "sctp ifa struct");
66MALLOC_DEFINE(SCTP_M_IFN, "sctp_ifn", "sctp ifn struct");
67MALLOC_DEFINE(SCTP_M_TIMW, "sctp_timw", "sctp time block");
68MALLOC_DEFINE(SCTP_M_MVRF, "sctp_mvrf", "sctp mvrf pcb list");
69MALLOC_DEFINE(SCTP_M_ITER, "sctp_iter", "sctp iterator control");
70MALLOC_DEFINE(SCTP_M_SOCKOPT, "sctp_socko", "sctp socket option");
71MALLOC_DEFINE(SCTP_M_MCORE, "sctp_mcore", "sctp mcore queue");
72
73/* Global NON-VNET structure that controls the iterator */
74struct iterator_control sctp_it_ctl;
75
76
77void
78sctp_wakeup_iterator(void)
79{
80 wakeup(&sctp_it_ctl.iterator_running);
81}
82
83static void
84sctp_iterator_thread(void *v SCTP_UNUSED)
85{
86 SCTP_IPI_ITERATOR_WQ_LOCK();
87 /* In FreeBSD this thread never terminates. */
88 for (;;) {
89 msleep(&sctp_it_ctl.iterator_running,
90 &sctp_it_ctl.ipi_iterator_wq_mtx,
91 0, "waiting_for_work", 0);
92 sctp_iterator_worker();
93 }
94}
95
96void
97sctp_startup_iterator(void)
98{
99 static int called = 0;
100 int ret;
101
102 if (called) {
103 /* You only get one */
104 return;
105 }
106 /* init the iterator head */
107 called = 1;
108 sctp_it_ctl.iterator_running = 0;
109 sctp_it_ctl.iterator_flags = 0;
110 sctp_it_ctl.cur_it = NULL;
111 SCTP_ITERATOR_LOCK_INIT();
112 SCTP_IPI_ITERATOR_WQ_INIT();
113 TAILQ_INIT(&sctp_it_ctl.iteratorhead);
114 ret = kproc_create(sctp_iterator_thread,
115 (void *)NULL,
116 &sctp_it_ctl.thread_proc,
117 RFPROC,
118 SCTP_KTHREAD_PAGES,
119 SCTP_KTRHEAD_NAME);
120}
121
122#ifdef INET6
123
124void
125sctp_gather_internal_ifa_flags(struct sctp_ifa *ifa)
126{
127 struct in6_ifaddr *ifa6;
128
129 ifa6 = (struct in6_ifaddr *)ifa->ifa;
130 ifa->flags = ifa6->ia6_flags;
131 if (!MODULE_GLOBAL(ip6_use_deprecated)) {
132 if (ifa->flags &
133 IN6_IFF_DEPRECATED) {
134 ifa->localifa_flags |= SCTP_ADDR_IFA_UNUSEABLE;
135 } else {
136 ifa->localifa_flags &= ~SCTP_ADDR_IFA_UNUSEABLE;
137 }
138 } else {
139 ifa->localifa_flags &= ~SCTP_ADDR_IFA_UNUSEABLE;
140 }
141 if (ifa->flags &
142 (IN6_IFF_DETACHED |
143 IN6_IFF_ANYCAST |
144 IN6_IFF_NOTREADY)) {
145 ifa->localifa_flags |= SCTP_ADDR_IFA_UNUSEABLE;
146 } else {
147 ifa->localifa_flags &= ~SCTP_ADDR_IFA_UNUSEABLE;
148 }
149}
150
151#endif /* INET6 */
152
153
154static uint32_t
155sctp_is_desired_interface_type(struct ifaddr *ifa)
156{
157 int result;
158
159 /* check the interface type to see if it's one we care about */
160 switch (ifa->ifa_ifp->if_type) {
161 case IFT_ETHER:
162 case IFT_ISO88023:
163 case IFT_ISO88024:
164 case IFT_ISO88025:
165 case IFT_ISO88026:
166 case IFT_STARLAN:
167 case IFT_P10:
168 case IFT_P80:
169 case IFT_HY:
170 case IFT_FDDI:
171 case IFT_XETHER:
172 case IFT_ISDNBASIC:
173 case IFT_ISDNPRIMARY:
174 case IFT_PTPSERIAL:
175 case IFT_OTHER:
176 case IFT_PPP:
177 case IFT_LOOP:
178 case IFT_SLIP:
179 case IFT_GIF:
180 case IFT_L2VLAN:
181 case IFT_STF:
182 case IFT_IP:
183 case IFT_IPOVERCDLC:
184 case IFT_IPOVERCLAW:
185 case IFT_PROPVIRTUAL: /* NetGraph Virtual too */
186 case IFT_VIRTUALIPADDRESS:
187 result = 1;
188 break;
189 default:
190 result = 0;
191 }
192
193 return (result);
194}
195
196
197
198
199static void
200sctp_init_ifns_for_vrf(int vrfid)
201{
202 /*
203 * Here we must apply ANY locks needed by the IFN we access and also
204 * make sure we lock any IFA that exists as we float through the
205 * list of IFA's
206 */
207 struct ifnet *ifn;
208 struct ifaddr *ifa;
209 struct sctp_ifa *sctp_ifa;
210 uint32_t ifa_flags;
211
212#ifdef INET6
213 struct in6_ifaddr *ifa6;
214
215#endif
216
217 IFNET_RLOCK();
218 TAILQ_FOREACH(ifn, &MODULE_GLOBAL(ifnet), if_list) {
219 IF_ADDR_RLOCK(ifn);
220 TAILQ_FOREACH(ifa, &ifn->if_addrlist, ifa_list) {
221 if (ifa->ifa_addr == NULL) {
222 continue;
223 }
224 switch (ifa->ifa_addr->sa_family) {
225#ifdef INET
226 case AF_INET:
227 if (((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr == 0) {
228 continue;
229 }
230 break;
231#endif
232#ifdef INET6
233 case AF_INET6:
234 if (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr)) {
235 /* skip unspecifed addresses */
236 continue;
237 }
238 break;
239#endif
240 default:
241 continue;
242 }
243 if (sctp_is_desired_interface_type(ifa) == 0) {
244 /* non desired type */
245 continue;
246 }
247 switch (ifa->ifa_addr->sa_family) {
248#ifdef INET
249 case AF_INET:
250 ifa_flags = 0;
251 break;
252#endif
253#ifdef INET6
254 case AF_INET6:
255 ifa6 = (struct in6_ifaddr *)ifa;
256 ifa_flags = ifa6->ia6_flags;
257 break;
258#endif
259 default:
260 ifa_flags = 0;
261 break;
262 }
263 sctp_ifa = sctp_add_addr_to_vrf(vrfid,
264 (void *)ifn,
265 ifn->if_index,
266 ifn->if_type,
267 ifn->if_xname,
268 (void *)ifa,
269 ifa->ifa_addr,
270 ifa_flags,
271 0);
272 if (sctp_ifa) {
273 sctp_ifa->localifa_flags &= ~SCTP_ADDR_DEFER_USE;
274 }
275 }
276 IF_ADDR_RUNLOCK(ifn);
277 }
278 IFNET_RUNLOCK();
279}
280
281void
282sctp_init_vrf_list(int vrfid)
283{
284 if (vrfid > SCTP_MAX_VRF_ID)
285 /* can't do that */
286 return;
287
288 /* Don't care about return here */
289 (void)sctp_allocate_vrf(vrfid);
290
291 /*
292 * Now we need to build all the ifn's for this vrf and there
293 * addresses
294 */
295 sctp_init_ifns_for_vrf(vrfid);
296}
297
298void
299sctp_addr_change(struct ifaddr *ifa, int cmd)
300{
301 uint32_t ifa_flags = 0;
302
303 /*
304 * BSD only has one VRF, if this changes we will need to hook in the
305 * right things here to get the id to pass to the address managment
306 * routine.
307 */
308 if (SCTP_BASE_VAR(first_time) == 0) {
309 /* Special test to see if my ::1 will showup with this */
310 SCTP_BASE_VAR(first_time) = 1;
311 sctp_init_ifns_for_vrf(SCTP_DEFAULT_VRFID);
312 }
313 if ((cmd != RTM_ADD) && (cmd != RTM_DELETE)) {
314 /* don't know what to do with this */
315 return;
316 }
317 if (ifa->ifa_addr == NULL) {
318 return;
319 }
320 switch (ifa->ifa_addr->sa_family) {
321#ifdef INET
322 case AF_INET:
323 if (((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr == 0) {
324 return;
325 }
326 break;
327#endif
328#ifdef INET6
329 case AF_INET6:
330 ifa_flags = ((struct in6_ifaddr *)ifa)->ia6_flags;
331 if (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr)) {
332 /* skip unspecifed addresses */
333 return;
334 }
335 break;
336#endif
337 default:
338 /* non inet/inet6 skip */
339 return;
340 }
341
342 if (sctp_is_desired_interface_type(ifa) == 0) {
343 /* non desired type */
344 return;
345 }
346 if (cmd == RTM_ADD) {
347 (void)sctp_add_addr_to_vrf(SCTP_DEFAULT_VRFID, (void *)ifa->ifa_ifp,
348 ifa->ifa_ifp->if_index, ifa->ifa_ifp->if_type,
349 ifa->ifa_ifp->if_xname,
350 (void *)ifa, ifa->ifa_addr, ifa_flags, 1);
351 } else {
352
353 sctp_del_addr_from_vrf(SCTP_DEFAULT_VRFID, ifa->ifa_addr,
354 ifa->ifa_ifp->if_index,
355 ifa->ifa_ifp->if_xname
356 );
357 /*
358 * We don't bump refcount here so when it completes the
359 * final delete will happen.
360 */
361 }
362}
363
364void
365 sctp_add_or_del_interfaces(int (*pred) (struct ifnet *), int add){
366 struct ifnet *ifn;
367 struct ifaddr *ifa;
368
369 IFNET_RLOCK();
370 TAILQ_FOREACH(ifn, &MODULE_GLOBAL(ifnet), if_list) {
371 if (!(*pred) (ifn)) {
372 continue;
373 }
374 TAILQ_FOREACH(ifa, &ifn->if_addrlist, ifa_list) {
375 sctp_addr_change(ifa, add ? RTM_ADD : RTM_DELETE);
376 }
377 }
378 IFNET_RUNLOCK();
379}
380
381struct mbuf *
382sctp_get_mbuf_for_msg(unsigned int space_needed, int want_header,
383 int how, int allonebuf, int type)
384{
385 struct mbuf *m = NULL;
386
387 m = m_getm2(NULL, space_needed, how, type, want_header ? M_PKTHDR : 0);
388 if (m == NULL) {
389 /* bad, no memory */
390 return (m);
391 }
392 if (allonebuf) {
393 int siz;
394
395 if (SCTP_BUF_IS_EXTENDED(m)) {
396 siz = SCTP_BUF_EXTEND_SIZE(m);
397 } else {
398 if (want_header)
399 siz = MHLEN;
400 else
401 siz = MLEN;
402 }
403 if (siz < space_needed) {
404 m_freem(m);
405 return (NULL);
406 }
407 }
408 if (SCTP_BUF_NEXT(m)) {
409 sctp_m_freem(SCTP_BUF_NEXT(m));
410 SCTP_BUF_NEXT(m) = NULL;
411 }
412#ifdef SCTP_MBUF_LOGGING
413 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
414 if (SCTP_BUF_IS_EXTENDED(m)) {
415 sctp_log_mb(m, SCTP_MBUF_IALLOC);
416 }
417 }
418#endif
419 return (m);
420}
421
422
423#ifdef SCTP_PACKET_LOGGING
424void
425sctp_packet_log(struct mbuf *m, int length)
425sctp_packet_log(struct mbuf *m)
426{
427 int *lenat, thisone;
428 void *copyto;
429 uint32_t *tick_tock;
426{
427 int *lenat, thisone;
428 void *copyto;
429 uint32_t *tick_tock;
430 int length;
430 int total_len;
431 int grabbed_lock = 0;
432 int value, newval, thisend, thisbegin;
433
434 /*
435 * Buffer layout. -sizeof this entry (total_len) -previous end
436 * (value) -ticks of log (ticks) o -ip packet o -as logged -
437 * where this started (thisbegin) x <--end points here
438 */
431 int total_len;
432 int grabbed_lock = 0;
433 int value, newval, thisend, thisbegin;
434
435 /*
436 * Buffer layout. -sizeof this entry (total_len) -previous end
437 * (value) -ticks of log (ticks) o -ip packet o -as logged -
438 * where this started (thisbegin) x <--end points here
439 */
440 length = SCTP_HEADER_LEN(m);
439 total_len = SCTP_SIZE32((length + (4 * sizeof(int))));
440 /* Log a packet to the buffer. */
441 if (total_len > SCTP_PACKET_LOG_SIZE) {
442 /* Can't log this packet I have not a buffer big enough */
443 return;
444 }
445 if (length < (int)(SCTP_MIN_V4_OVERHEAD + sizeof(struct sctp_cookie_ack_chunk))) {
446 return;
447 }
448 atomic_add_int(&SCTP_BASE_VAR(packet_log_writers), 1);
449try_again:
450 if (SCTP_BASE_VAR(packet_log_writers) > SCTP_PKTLOG_WRITERS_NEED_LOCK) {
451 SCTP_IP_PKTLOG_LOCK();
452 grabbed_lock = 1;
453again_locked:
454 value = SCTP_BASE_VAR(packet_log_end);
455 newval = SCTP_BASE_VAR(packet_log_end) + total_len;
456 if (newval >= SCTP_PACKET_LOG_SIZE) {
457 /* we wrapped */
458 thisbegin = 0;
459 thisend = total_len;
460 } else {
461 thisbegin = SCTP_BASE_VAR(packet_log_end);
462 thisend = newval;
463 }
464 if (!(atomic_cmpset_int(&SCTP_BASE_VAR(packet_log_end), value, thisend))) {
465 goto again_locked;
466 }
467 } else {
468 value = SCTP_BASE_VAR(packet_log_end);
469 newval = SCTP_BASE_VAR(packet_log_end) + total_len;
470 if (newval >= SCTP_PACKET_LOG_SIZE) {
471 /* we wrapped */
472 thisbegin = 0;
473 thisend = total_len;
474 } else {
475 thisbegin = SCTP_BASE_VAR(packet_log_end);
476 thisend = newval;
477 }
478 if (!(atomic_cmpset_int(&SCTP_BASE_VAR(packet_log_end), value, thisend))) {
479 goto try_again;
480 }
481 }
482 /* Sanity check */
483 if (thisend >= SCTP_PACKET_LOG_SIZE) {
484 SCTP_PRINTF("Insanity stops a log thisbegin:%d thisend:%d writers:%d lock:%d end:%d\n",
485 thisbegin,
486 thisend,
487 SCTP_BASE_VAR(packet_log_writers),
488 grabbed_lock,
489 SCTP_BASE_VAR(packet_log_end));
490 SCTP_BASE_VAR(packet_log_end) = 0;
491 goto no_log;
492
493 }
494 lenat = (int *)&SCTP_BASE_VAR(packet_log_buffer)[thisbegin];
495 *lenat = total_len;
496 lenat++;
497 *lenat = value;
498 lenat++;
499 tick_tock = (uint32_t *) lenat;
500 lenat++;
501 *tick_tock = sctp_get_tick_count();
502 copyto = (void *)lenat;
503 thisone = thisend - sizeof(int);
504 lenat = (int *)&SCTP_BASE_VAR(packet_log_buffer)[thisone];
505 *lenat = thisbegin;
506 if (grabbed_lock) {
507 SCTP_IP_PKTLOG_UNLOCK();
508 grabbed_lock = 0;
509 }
510 m_copydata(m, 0, length, (caddr_t)copyto);
511no_log:
512 if (grabbed_lock) {
513 SCTP_IP_PKTLOG_UNLOCK();
514 }
515 atomic_subtract_int(&SCTP_BASE_VAR(packet_log_writers), 1);
516}
517
518
519int
520sctp_copy_out_packet_log(uint8_t * target, int length)
521{
522 /*
523 * We wind through the packet log starting at start copying up to
524 * length bytes out. We return the number of bytes copied.
525 */
526 int tocopy, this_copy;
527 int *lenat;
528 int did_delay = 0;
529
530 tocopy = length;
531 if (length < (int)(2 * sizeof(int))) {
532 /* not enough room */
533 return (0);
534 }
535 if (SCTP_PKTLOG_WRITERS_NEED_LOCK) {
536 atomic_add_int(&SCTP_BASE_VAR(packet_log_writers), SCTP_PKTLOG_WRITERS_NEED_LOCK);
537again:
538 if ((did_delay == 0) && (SCTP_BASE_VAR(packet_log_writers) != SCTP_PKTLOG_WRITERS_NEED_LOCK)) {
539 /*
540 * we delay here for just a moment hoping the
541 * writer(s) that were present when we entered will
542 * have left and we only have locking ones that will
543 * contend with us for the lock. This does not
544 * assure 100% access, but its good enough for a
545 * logging facility like this.
546 */
547 did_delay = 1;
548 DELAY(10);
549 goto again;
550 }
551 }
552 SCTP_IP_PKTLOG_LOCK();
553 lenat = (int *)target;
554 *lenat = SCTP_BASE_VAR(packet_log_end);
555 lenat++;
556 this_copy = min((length - sizeof(int)), SCTP_PACKET_LOG_SIZE);
557 memcpy((void *)lenat, (void *)SCTP_BASE_VAR(packet_log_buffer), this_copy);
558 if (SCTP_PKTLOG_WRITERS_NEED_LOCK) {
559 atomic_subtract_int(&SCTP_BASE_VAR(packet_log_writers),
560 SCTP_PKTLOG_WRITERS_NEED_LOCK);
561 }
562 SCTP_IP_PKTLOG_UNLOCK();
563 return (this_copy + sizeof(int));
564}
565
566#endif
441 total_len = SCTP_SIZE32((length + (4 * sizeof(int))));
442 /* Log a packet to the buffer. */
443 if (total_len > SCTP_PACKET_LOG_SIZE) {
444 /* Can't log this packet I have not a buffer big enough */
445 return;
446 }
447 if (length < (int)(SCTP_MIN_V4_OVERHEAD + sizeof(struct sctp_cookie_ack_chunk))) {
448 return;
449 }
450 atomic_add_int(&SCTP_BASE_VAR(packet_log_writers), 1);
451try_again:
452 if (SCTP_BASE_VAR(packet_log_writers) > SCTP_PKTLOG_WRITERS_NEED_LOCK) {
453 SCTP_IP_PKTLOG_LOCK();
454 grabbed_lock = 1;
455again_locked:
456 value = SCTP_BASE_VAR(packet_log_end);
457 newval = SCTP_BASE_VAR(packet_log_end) + total_len;
458 if (newval >= SCTP_PACKET_LOG_SIZE) {
459 /* we wrapped */
460 thisbegin = 0;
461 thisend = total_len;
462 } else {
463 thisbegin = SCTP_BASE_VAR(packet_log_end);
464 thisend = newval;
465 }
466 if (!(atomic_cmpset_int(&SCTP_BASE_VAR(packet_log_end), value, thisend))) {
467 goto again_locked;
468 }
469 } else {
470 value = SCTP_BASE_VAR(packet_log_end);
471 newval = SCTP_BASE_VAR(packet_log_end) + total_len;
472 if (newval >= SCTP_PACKET_LOG_SIZE) {
473 /* we wrapped */
474 thisbegin = 0;
475 thisend = total_len;
476 } else {
477 thisbegin = SCTP_BASE_VAR(packet_log_end);
478 thisend = newval;
479 }
480 if (!(atomic_cmpset_int(&SCTP_BASE_VAR(packet_log_end), value, thisend))) {
481 goto try_again;
482 }
483 }
484 /* Sanity check */
485 if (thisend >= SCTP_PACKET_LOG_SIZE) {
486 SCTP_PRINTF("Insanity stops a log thisbegin:%d thisend:%d writers:%d lock:%d end:%d\n",
487 thisbegin,
488 thisend,
489 SCTP_BASE_VAR(packet_log_writers),
490 grabbed_lock,
491 SCTP_BASE_VAR(packet_log_end));
492 SCTP_BASE_VAR(packet_log_end) = 0;
493 goto no_log;
494
495 }
496 lenat = (int *)&SCTP_BASE_VAR(packet_log_buffer)[thisbegin];
497 *lenat = total_len;
498 lenat++;
499 *lenat = value;
500 lenat++;
501 tick_tock = (uint32_t *) lenat;
502 lenat++;
503 *tick_tock = sctp_get_tick_count();
504 copyto = (void *)lenat;
505 thisone = thisend - sizeof(int);
506 lenat = (int *)&SCTP_BASE_VAR(packet_log_buffer)[thisone];
507 *lenat = thisbegin;
508 if (grabbed_lock) {
509 SCTP_IP_PKTLOG_UNLOCK();
510 grabbed_lock = 0;
511 }
512 m_copydata(m, 0, length, (caddr_t)copyto);
513no_log:
514 if (grabbed_lock) {
515 SCTP_IP_PKTLOG_UNLOCK();
516 }
517 atomic_subtract_int(&SCTP_BASE_VAR(packet_log_writers), 1);
518}
519
520
521int
522sctp_copy_out_packet_log(uint8_t * target, int length)
523{
524 /*
525 * We wind through the packet log starting at start copying up to
526 * length bytes out. We return the number of bytes copied.
527 */
528 int tocopy, this_copy;
529 int *lenat;
530 int did_delay = 0;
531
532 tocopy = length;
533 if (length < (int)(2 * sizeof(int))) {
534 /* not enough room */
535 return (0);
536 }
537 if (SCTP_PKTLOG_WRITERS_NEED_LOCK) {
538 atomic_add_int(&SCTP_BASE_VAR(packet_log_writers), SCTP_PKTLOG_WRITERS_NEED_LOCK);
539again:
540 if ((did_delay == 0) && (SCTP_BASE_VAR(packet_log_writers) != SCTP_PKTLOG_WRITERS_NEED_LOCK)) {
541 /*
542 * we delay here for just a moment hoping the
543 * writer(s) that were present when we entered will
544 * have left and we only have locking ones that will
545 * contend with us for the lock. This does not
546 * assure 100% access, but its good enough for a
547 * logging facility like this.
548 */
549 did_delay = 1;
550 DELAY(10);
551 goto again;
552 }
553 }
554 SCTP_IP_PKTLOG_LOCK();
555 lenat = (int *)target;
556 *lenat = SCTP_BASE_VAR(packet_log_end);
557 lenat++;
558 this_copy = min((length - sizeof(int)), SCTP_PACKET_LOG_SIZE);
559 memcpy((void *)lenat, (void *)SCTP_BASE_VAR(packet_log_buffer), this_copy);
560 if (SCTP_PKTLOG_WRITERS_NEED_LOCK) {
561 atomic_subtract_int(&SCTP_BASE_VAR(packet_log_writers),
562 SCTP_PKTLOG_WRITERS_NEED_LOCK);
563 }
564 SCTP_IP_PKTLOG_UNLOCK();
565 return (this_copy + sizeof(int));
566}
567
568#endif