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