sctp_bsd_addr.c revision 197326
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 197326 2009-09-19 14:02:16Z 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 */
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#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	CURVNET_SET((struct vnet *)v);
93	SCTP_IPI_ITERATOR_WQ_LOCK();
94	SCTP_BASE_INFO(iterator_running) = 0;
95	while (1) {
96		msleep(&SCTP_BASE_INFO(iterator_running),
97		    &SCTP_BASE_INFO(ipi_iterator_wq_mtx),
98		    0, "waiting_for_work", 0);
99		if (SCTP_BASE_INFO(threads_must_exit)) {
100			kthread_exit();
101		}
102		sctp_iterator_worker();
103	}
104	CURVNET_RESTORE();
105}
106
107void
108sctp_startup_iterator(void)
109{
110	int ret;
111
112	ret = kproc_create(sctp_iterator_thread,
113	    (void *)curvnet,
114	    &SCTP_BASE_INFO(thread_proc),
115	    RFPROC,
116	    SCTP_KTHREAD_PAGES,
117	    SCTP_KTRHEAD_NAME);
118}
119
120#endif
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_IP:
182	case IFT_IPOVERCDLC:
183	case IFT_IPOVERCLAW:
184	case IFT_VIRTUALIPADDRESS:
185		result = 1;
186		break;
187	default:
188		result = 0;
189	}
190
191	return (result);
192}
193
194
195
196
197static void
198sctp_init_ifns_for_vrf(int vrfid)
199{
200	/*
201	 * Here we must apply ANY locks needed by the IFN we access and also
202	 * make sure we lock any IFA that exists as we float through the
203	 * list of IFA's
204	 */
205	struct ifnet *ifn;
206	struct ifaddr *ifa;
207	struct in6_ifaddr *ifa6;
208	struct sctp_ifa *sctp_ifa;
209	uint32_t ifa_flags;
210
211#if 0
212	IFNET_RLOCK();
213#endif
214	TAILQ_FOREACH(ifn, &MODULE_GLOBAL(ifnet), if_list) {
215		IF_ADDR_LOCK(ifn);
216		TAILQ_FOREACH(ifa, &ifn->if_addrlist, ifa_list) {
217			if (ifa->ifa_addr == NULL) {
218				continue;
219			}
220			if ((ifa->ifa_addr->sa_family != AF_INET) && (ifa->ifa_addr->sa_family != AF_INET6)) {
221				/* non inet/inet6 skip */
222				continue;
223			}
224			if (ifa->ifa_addr->sa_family == AF_INET6) {
225				if (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr)) {
226					/* skip unspecifed addresses */
227					continue;
228				}
229			} else {
230				if (((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr == 0) {
231					continue;
232				}
233			}
234			if (sctp_is_desired_interface_type(ifa) == 0) {
235				/* non desired type */
236				continue;
237			}
238			if (ifa->ifa_addr->sa_family == AF_INET6) {
239				ifa6 = (struct in6_ifaddr *)ifa;
240				ifa_flags = ifa6->ia6_flags;
241			} else {
242				ifa_flags = 0;
243			}
244			sctp_ifa = sctp_add_addr_to_vrf(vrfid,
245			    (void *)ifn,
246			    ifn->if_index,
247			    ifn->if_type,
248			    ifn->if_xname,
249			    (void *)ifa,
250			    ifa->ifa_addr,
251			    ifa_flags,
252			    0);
253			if (sctp_ifa) {
254				sctp_ifa->localifa_flags &= ~SCTP_ADDR_DEFER_USE;
255			}
256		}
257		IF_ADDR_UNLOCK(ifn);
258	}
259#if 0
260	IFNET_RUNLOCK();
261#endif
262}
263
264void
265sctp_init_vrf_list(int vrfid)
266{
267	if (vrfid > SCTP_MAX_VRF_ID)
268		/* can't do that */
269		return;
270
271	/* Don't care about return here */
272	(void)sctp_allocate_vrf(vrfid);
273
274	/*
275	 * Now we need to build all the ifn's for this vrf and there
276	 * addresses
277	 */
278	sctp_init_ifns_for_vrf(vrfid);
279}
280
281void
282sctp_addr_change(struct ifaddr *ifa, int cmd)
283{
284	struct sctp_ifa *ifap = NULL;
285	uint32_t ifa_flags = 0;
286
287	/*
288	 * BSD only has one VRF, if this changes we will need to hook in the
289	 * right things here to get the id to pass to the address managment
290	 * routine.
291	 */
292	if (SCTP_BASE_VAR(first_time) == 0) {
293		/* Special test to see if my ::1 will showup with this */
294		SCTP_BASE_VAR(first_time) = 1;
295		sctp_init_ifns_for_vrf(SCTP_DEFAULT_VRFID);
296	}
297	if ((cmd != RTM_ADD) && (cmd != RTM_DELETE)) {
298		/* don't know what to do with this */
299		return;
300	}
301	if (ifa->ifa_addr == NULL) {
302		return;
303	}
304	if ((ifa->ifa_addr->sa_family != AF_INET) && (ifa->ifa_addr->sa_family != AF_INET6)) {
305		/* non inet/inet6 skip */
306		return;
307	}
308	if (ifa->ifa_addr->sa_family == AF_INET6) {
309		ifa_flags = ((struct in6_ifaddr *)ifa)->ia6_flags;
310		if (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr)) {
311			/* skip unspecifed addresses */
312			return;
313		}
314	} else {
315		if (((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr == 0) {
316			return;
317		}
318	}
319
320	if (sctp_is_desired_interface_type(ifa) == 0) {
321		/* non desired type */
322		return;
323	}
324	if (cmd == RTM_ADD) {
325		ifap = sctp_add_addr_to_vrf(SCTP_DEFAULT_VRFID, (void *)ifa->ifa_ifp,
326		    ifa->ifa_ifp->if_index, ifa->ifa_ifp->if_type,
327		    ifa->ifa_ifp->if_xname,
328		    (void *)ifa, ifa->ifa_addr, ifa_flags, 1);
329	} else {
330
331		sctp_del_addr_from_vrf(SCTP_DEFAULT_VRFID, ifa->ifa_addr,
332		    ifa->ifa_ifp->if_index,
333		    ifa->ifa_ifp->if_xname
334		    );
335		/*
336		 * We don't bump refcount here so when it completes the
337		 * final delete will happen.
338		 */
339	}
340}
341
342void
343     sctp_add_or_del_interfaces(int (*pred) (struct ifnet *), int add){
344	struct ifnet *ifn;
345	struct ifaddr *ifa;
346
347#if 0
348	IFNET_RLOCK();
349#endif
350	TAILQ_FOREACH(ifn, &MODULE_GLOBAL(ifnet), if_list) {
351		if (!(*pred) (ifn)) {
352			continue;
353		}
354		TAILQ_FOREACH(ifa, &ifn->if_addrlist, ifa_list) {
355			sctp_addr_change(ifa, add ? RTM_ADD : RTM_DELETE);
356		}
357	}
358#if 0
359	IFNET_RUNLOCK();
360#endif
361}
362
363struct mbuf *
364sctp_get_mbuf_for_msg(unsigned int space_needed, int want_header,
365    int how, int allonebuf, int type)
366{
367	struct mbuf *m = NULL;
368
369	m = m_getm2(NULL, space_needed, how, type, want_header ? M_PKTHDR : 0);
370	if (m == NULL) {
371		/* bad, no memory */
372		return (m);
373	}
374	if (allonebuf) {
375		int siz;
376
377		if (SCTP_BUF_IS_EXTENDED(m)) {
378			siz = SCTP_BUF_EXTEND_SIZE(m);
379		} else {
380			if (want_header)
381				siz = MHLEN;
382			else
383				siz = MLEN;
384		}
385		if (siz < space_needed) {
386			m_freem(m);
387			return (NULL);
388		}
389	}
390	if (SCTP_BUF_NEXT(m)) {
391		sctp_m_freem(SCTP_BUF_NEXT(m));
392		SCTP_BUF_NEXT(m) = NULL;
393	}
394#ifdef SCTP_MBUF_LOGGING
395	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
396		if (SCTP_BUF_IS_EXTENDED(m)) {
397			sctp_log_mb(m, SCTP_MBUF_IALLOC);
398		}
399	}
400#endif
401	return (m);
402}
403
404
405#ifdef SCTP_PACKET_LOGGING
406void
407sctp_packet_log(struct mbuf *m, int length)
408{
409	int *lenat, thisone;
410	void *copyto;
411	uint32_t *tick_tock;
412	int total_len;
413	int grabbed_lock = 0;
414	int value, newval, thisend, thisbegin;
415
416	/*
417	 * Buffer layout. -sizeof this entry (total_len) -previous end
418	 * (value) -ticks of log      (ticks) o -ip packet o -as logged -
419	 * where this started (thisbegin) x <--end points here
420	 */
421	total_len = SCTP_SIZE32((length + (4 * sizeof(int))));
422	/* Log a packet to the buffer. */
423	if (total_len > SCTP_PACKET_LOG_SIZE) {
424		/* Can't log this packet I have not a buffer big enough */
425		return;
426	}
427	if (length < (int)(SCTP_MIN_V4_OVERHEAD + sizeof(struct sctp_cookie_ack_chunk))) {
428		return;
429	}
430	atomic_add_int(&SCTP_BASE_VAR(packet_log_writers), 1);
431try_again:
432	if (SCTP_BASE_VAR(packet_log_writers) > SCTP_PKTLOG_WRITERS_NEED_LOCK) {
433		SCTP_IP_PKTLOG_LOCK();
434		grabbed_lock = 1;
435again_locked:
436		value = SCTP_BASE_VAR(packet_log_end);
437		newval = SCTP_BASE_VAR(packet_log_end) + total_len;
438		if (newval >= SCTP_PACKET_LOG_SIZE) {
439			/* we wrapped */
440			thisbegin = 0;
441			thisend = total_len;
442		} else {
443			thisbegin = SCTP_BASE_VAR(packet_log_end);
444			thisend = newval;
445		}
446		if (!(atomic_cmpset_int(&SCTP_BASE_VAR(packet_log_end), value, thisend))) {
447			goto again_locked;
448		}
449	} else {
450		value = SCTP_BASE_VAR(packet_log_end);
451		newval = SCTP_BASE_VAR(packet_log_end) + total_len;
452		if (newval >= SCTP_PACKET_LOG_SIZE) {
453			/* we wrapped */
454			thisbegin = 0;
455			thisend = total_len;
456		} else {
457			thisbegin = SCTP_BASE_VAR(packet_log_end);
458			thisend = newval;
459		}
460		if (!(atomic_cmpset_int(&SCTP_BASE_VAR(packet_log_end), value, thisend))) {
461			goto try_again;
462		}
463	}
464	/* Sanity check */
465	if (thisend >= SCTP_PACKET_LOG_SIZE) {
466		printf("Insanity stops a log thisbegin:%d thisend:%d writers:%d lock:%d end:%d\n",
467		    thisbegin,
468		    thisend,
469		    SCTP_BASE_VAR(packet_log_writers),
470		    grabbed_lock,
471		    SCTP_BASE_VAR(packet_log_end));
472		SCTP_BASE_VAR(packet_log_end) = 0;
473		goto no_log;
474
475	}
476	lenat = (int *)&SCTP_BASE_VAR(packet_log_buffer)[thisbegin];
477	*lenat = total_len;
478	lenat++;
479	*lenat = value;
480	lenat++;
481	tick_tock = (uint32_t *) lenat;
482	lenat++;
483	*tick_tock = sctp_get_tick_count();
484	copyto = (void *)lenat;
485	thisone = thisend - sizeof(int);
486	lenat = (int *)&SCTP_BASE_VAR(packet_log_buffer)[thisone];
487	*lenat = thisbegin;
488	if (grabbed_lock) {
489		SCTP_IP_PKTLOG_UNLOCK();
490		grabbed_lock = 0;
491	}
492	m_copydata(m, 0, length, (caddr_t)copyto);
493no_log:
494	if (grabbed_lock) {
495		SCTP_IP_PKTLOG_UNLOCK();
496	}
497	atomic_subtract_int(&SCTP_BASE_VAR(packet_log_writers), 1);
498}
499
500
501int
502sctp_copy_out_packet_log(uint8_t * target, int length)
503{
504	/*
505	 * We wind through the packet log starting at start copying up to
506	 * length bytes out. We return the number of bytes copied.
507	 */
508	int tocopy, this_copy;
509	int *lenat;
510	int did_delay = 0;
511
512	tocopy = length;
513	if (length < (int)(2 * sizeof(int))) {
514		/* not enough room */
515		return (0);
516	}
517	if (SCTP_PKTLOG_WRITERS_NEED_LOCK) {
518		atomic_add_int(&SCTP_BASE_VAR(packet_log_writers), SCTP_PKTLOG_WRITERS_NEED_LOCK);
519again:
520		if ((did_delay == 0) && (SCTP_BASE_VAR(packet_log_writers) != SCTP_PKTLOG_WRITERS_NEED_LOCK)) {
521			/*
522			 * we delay here for just a moment hoping the
523			 * writer(s) that were present when we entered will
524			 * have left and we only have locking ones that will
525			 * contend with us for the lock. This does not
526			 * assure 100% access, but its good enough for a
527			 * logging facility like this.
528			 */
529			did_delay = 1;
530			DELAY(10);
531			goto again;
532		}
533	}
534	SCTP_IP_PKTLOG_LOCK();
535	lenat = (int *)target;
536	*lenat = SCTP_BASE_VAR(packet_log_end);
537	lenat++;
538	this_copy = min((length - sizeof(int)), SCTP_PACKET_LOG_SIZE);
539	memcpy((void *)lenat, (void *)SCTP_BASE_VAR(packet_log_buffer), this_copy);
540	if (SCTP_PKTLOG_WRITERS_NEED_LOCK) {
541		atomic_subtract_int(&SCTP_BASE_VAR(packet_log_writers),
542		    SCTP_PKTLOG_WRITERS_NEED_LOCK);
543	}
544	SCTP_IP_PKTLOG_UNLOCK();
545	return (this_copy + sizeof(int));
546}
547
548#endif
549