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