sctp_bsd_addr.c revision 170094
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 170094 2007-05-29 11:53: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
52/* Declare all of our malloc named types */
53
54/* Note to Michael/Peter for mac-os,
55 * I think mac has this too since I
56 * do see the M_PCB type, so I
57 * will also put in the mac file the
58 * MALLOC_DECLARE. If this does not
59 * work for mac uncomment the defines for
60 * the strings that we use in Panda, I put
61 * them in comments in the mac-os file.
62 */
63MALLOC_DEFINE(SCTP_M_MAP, "sctp_map", "sctp asoc map descriptor");
64MALLOC_DEFINE(SCTP_M_STRMI, "sctp_stri", "sctp stream in array");
65MALLOC_DEFINE(SCTP_M_STRMO, "sctp_stro", "sctp stream out array");
66MALLOC_DEFINE(SCTP_M_ASC_ADDR, "sctp_aadr", "sctp asconf address");
67MALLOC_DEFINE(SCTP_M_ASC_IT, "sctp_a_it", "sctp asconf iterator");
68MALLOC_DEFINE(SCTP_M_AUTH_CL, "sctp_atcl", "sctp auth chunklist");
69MALLOC_DEFINE(SCTP_M_AUTH_KY, "sctp_atky", "sctp auth key");
70MALLOC_DEFINE(SCTP_M_AUTH_HL, "sctp_athm", "sctp auth hmac list");
71MALLOC_DEFINE(SCTP_M_AUTH_IF, "sctp_athi", "sctp auth info");
72MALLOC_DEFINE(SCTP_M_STRESET, "sctp_stre", "sctp stream reset");
73MALLOC_DEFINE(SCTP_M_CMSG, "sctp_cmsg", "sctp CMSG buffer");
74MALLOC_DEFINE(SCTP_M_COPYAL, "sctp_cpal", "sctp copy all");
75MALLOC_DEFINE(SCTP_M_VRF, "sctp_vrf", "sctp vrf struct");
76MALLOC_DEFINE(SCTP_M_IFA, "sctp_ifa", "sctp ifa struct");
77MALLOC_DEFINE(SCTP_M_IFN, "sctp_ifn", "sctp ifn struct");
78MALLOC_DEFINE(SCTP_M_TIMW, "sctp_timw", "sctp time block");
79MALLOC_DEFINE(SCTP_M_MVRF, "sctp_mvrf", "sctp mvrf pcb list");
80MALLOC_DEFINE(SCTP_M_ITER, "sctp_iter", "sctp iterator control");
81MALLOC_DEFINE(SCTP_M_SOCKOPT, "sctp_socko", "sctp socket option");
82
83
84#if defined(SCTP_USE_THREAD_BASED_ITERATOR)
85void
86sctp_wakeup_iterator(void)
87{
88	wakeup(&sctppcbinfo.iterator_running);
89}
90
91static void
92sctp_iterator_thread(void *v)
93{
94	SCTP_IPI_ITERATOR_WQ_LOCK();
95	sctppcbinfo.iterator_running = 0;
96	while (1) {
97		msleep(&sctppcbinfo.iterator_running,
98		    &sctppcbinfo.ipi_iterator_wq_mtx,
99		    0, "waiting_for_work", 0);
100		sctp_iterator_worker();
101	}
102}
103
104void
105sctp_startup_iterator(void)
106{
107	int ret;
108
109	ret = kthread_create(sctp_iterator_thread,
110	    (void *)NULL,
111	    &sctppcbinfo.thread_proc,
112	    RFPROC,
113	    SCTP_KTHREAD_PAGES,
114	    SCTP_KTRHEAD_NAME);
115}
116
117#endif
118
119
120void
121sctp_gather_internal_ifa_flags(struct sctp_ifa *ifa)
122{
123	struct in6_ifaddr *ifa6;
124
125	ifa6 = (struct in6_ifaddr *)ifa->ifa;
126	ifa->flags = ifa6->ia6_flags;
127	if (!ip6_use_deprecated) {
128		if (ifa->flags &
129		    IN6_IFF_DEPRECATED) {
130			ifa->localifa_flags |= SCTP_ADDR_IFA_UNUSEABLE;
131		} else {
132			ifa->localifa_flags &= ~SCTP_ADDR_IFA_UNUSEABLE;
133		}
134	} else {
135		ifa->localifa_flags &= ~SCTP_ADDR_IFA_UNUSEABLE;
136	}
137	if (ifa->flags &
138	    (IN6_IFF_DETACHED |
139	    IN6_IFF_ANYCAST |
140	    IN6_IFF_NOTREADY)) {
141		ifa->localifa_flags |= SCTP_ADDR_IFA_UNUSEABLE;
142	} else {
143		ifa->localifa_flags &= ~SCTP_ADDR_IFA_UNUSEABLE;
144	}
145}
146
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_PPP:
171	case IFT_LOOP:
172	case IFT_SLIP:
173	case IFT_IP:
174	case IFT_IPOVERCDLC:
175	case IFT_IPOVERCLAW:
176	case IFT_VIRTUALIPADDRESS:
177		result = 1;
178		break;
179	default:
180		result = 0;
181	}
182
183	return (result);
184}
185
186static void
187sctp_init_ifns_for_vrf(int vrfid)
188{
189	/*
190	 * Here we must apply ANY locks needed by the IFN we access and also
191	 * make sure we lock any IFA that exists as we float through the
192	 * list of IFA's
193	 */
194	struct ifnet *ifn;
195	struct ifaddr *ifa;
196	struct in6_ifaddr *ifa6;
197	struct sctp_ifa *sctp_ifa;
198	uint32_t ifa_flags;
199
200	TAILQ_FOREACH(ifn, &ifnet, if_list) {
201		TAILQ_FOREACH(ifa, &ifn->if_addrlist, ifa_list) {
202			if (ifa->ifa_addr == NULL) {
203				continue;
204			}
205			if ((ifa->ifa_addr->sa_family != AF_INET) &&
206			    (ifa->ifa_addr->sa_family != AF_INET6)
207			    ) {
208				/* non inet/inet6 skip */
209				continue;
210			}
211			if (ifa->ifa_addr->sa_family == AF_INET6) {
212				ifa6 = (struct in6_ifaddr *)ifa;
213				ifa_flags = ifa6->ia6_flags;
214				if (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr)) {
215					/* skip unspecifed addresses */
216					continue;
217				}
218			} else if (ifa->ifa_addr->sa_family == AF_INET) {
219				if (((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr == 0) {
220					continue;
221				}
222			}
223			if (sctp_is_desired_interface_type(ifa) == 0) {
224				/* non desired type */
225				continue;
226			}
227			if ((ifa->ifa_addr->sa_family == AF_INET6) ||
228			    (ifa->ifa_addr->sa_family == AF_INET)) {
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, 0
243				    );
244				if (sctp_ifa) {
245					sctp_ifa->localifa_flags &= ~SCTP_ADDR_DEFER_USE;
246				}
247			}
248		}
249	}
250}
251
252
253void
254sctp_init_vrf_list(int vrfid)
255{
256	if (vrfid > SCTP_MAX_VRF_ID)
257		/* can't do that */
258		return;
259
260	/* Don't care about return here */
261	(void)sctp_allocate_vrf(vrfid);
262
263	/*
264	 * Now we need to build all the ifn's for this vrf and there
265	 * addresses
266	 */
267	sctp_init_ifns_for_vrf(vrfid);
268}
269
270static uint8_t first_time = 0;
271
272
273void
274sctp_addr_change(struct ifaddr *ifa, int cmd)
275{
276	struct sctp_ifa *ifap = NULL;
277	uint32_t ifa_flags = 0;
278	struct in6_ifaddr *ifa6;
279
280	/*
281	 * BSD only has one VRF, if this changes we will need to hook in the
282	 * right things here to get the id to pass to the address managment
283	 * routine.
284	 */
285	if (first_time == 0) {
286		/* Special test to see if my ::1 will showup with this */
287		first_time = 1;
288		sctp_init_ifns_for_vrf(SCTP_DEFAULT_VRFID);
289	}
290	if ((cmd != RTM_ADD) && (cmd != RTM_DELETE)) {
291		/* don't know what to do with this */
292		return;
293	}
294	if (ifa->ifa_addr == NULL) {
295		return;
296	}
297	if ((ifa->ifa_addr->sa_family != AF_INET) &&
298	    (ifa->ifa_addr->sa_family != AF_INET6)
299	    ) {
300		/* non inet/inet6 skip */
301		return;
302	}
303	if (ifa->ifa_addr->sa_family == AF_INET6) {
304		ifa6 = (struct in6_ifaddr *)ifa;
305		ifa_flags = ifa6->ia6_flags;
306		if (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr)) {
307			/* skip unspecifed addresses */
308			return;
309		}
310	} else if (ifa->ifa_addr->sa_family == AF_INET) {
311		if (((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr == 0) {
312			return;
313		}
314	}
315	if (sctp_is_desired_interface_type(ifa) == 0) {
316		/* non desired type */
317		return;
318	}
319	if (cmd == RTM_ADD) {
320		ifap = sctp_add_addr_to_vrf(SCTP_DEFAULT_VRFID, (void *)ifa->ifa_ifp,
321		    ifa->ifa_ifp->if_index, ifa->ifa_ifp->if_type,
322		    ifa->ifa_ifp->if_xname,
323		    (void *)ifa, ifa->ifa_addr, ifa_flags, 1);
324
325	} else if (cmd == RTM_DELETE) {
326
327		sctp_del_addr_from_vrf(SCTP_DEFAULT_VRFID, ifa->ifa_addr, ifa->ifa_ifp->if_index);
328		/*
329		 * We don't bump refcount here so when it completes the
330		 * final delete will happen.
331		 */
332	}
333}
334
335struct mbuf *
336sctp_get_mbuf_for_msg(unsigned int space_needed, int want_header,
337    int how, int allonebuf, int type)
338{
339	struct mbuf *m = NULL;
340
341	m = m_getm2(NULL, space_needed, how, type, want_header ? M_PKTHDR : 0);
342	if (m == NULL) {
343		/* bad, no memory */
344		return (m);
345	}
346	if (allonebuf) {
347		int siz;
348
349		if (SCTP_BUF_IS_EXTENDED(m)) {
350			siz = SCTP_BUF_EXTEND_SIZE(m);
351		} else {
352			if (want_header)
353				siz = MHLEN;
354			else
355				siz = MLEN;
356		}
357		if (siz < space_needed) {
358			m_freem(m);
359			return (NULL);
360		}
361	}
362	if (SCTP_BUF_NEXT(m)) {
363		sctp_m_freem(SCTP_BUF_NEXT(m));
364		SCTP_BUF_NEXT(m) = NULL;
365	}
366#ifdef SCTP_MBUF_LOGGING
367	if (SCTP_BUF_IS_EXTENDED(m)) {
368		sctp_log_mb(m, SCTP_MBUF_IALLOC);
369	}
370#endif
371	return (m);
372}
373
374
375#ifdef SCTP_PACKET_LOGGING
376
377int packet_log_start = 0;
378int packet_log_end = 0;
379int packet_log_old_end = SCTP_PACKET_LOG_SIZE;
380int packet_log_wrapped = 0;
381uint8_t packet_log_buffer[SCTP_PACKET_LOG_SIZE];
382
383
384void
385sctp_packet_log(struct mbuf *m, int length)
386{
387	int *lenat, needed, thisone;
388	void *copyto;
389	uint32_t *tick_tock;
390	int total_len, spare;
391
392	total_len = SCTP_SIZE32((length + (2 * sizeof(int))));
393	/* Log a packet to the buffer. */
394	if (total_len > SCTP_PACKET_LOG_SIZE) {
395		/* Can't log this packet I have not a buffer big enough */
396		return;
397	}
398	if (length < (SCTP_MIN_V4_OVERHEAD + sizeof(struct sctp_cookie_ack_chunk))) {
399		printf("Huh, length is %d to small for sctp min:%d\n",
400		    length,
401		    (SCTP_MIN_V4_OVERHEAD + sizeof(struct sctp_cookie_ack_chunk)));
402		return;
403	}
404	SCTP_IP_PKTLOG_LOCK();
405	if ((SCTP_PACKET_LOG_SIZE - packet_log_end) <= total_len) {
406		/*
407		 * it won't fit on the end. We must go back to the
408		 * beginning. To do this we go back and cahnge
409		 * packet_log_start.
410		 */
411		int orig_end;
412
413		lenat = (int *)packet_log_buffer;
414		orig_end = packet_log_end;
415		packet_log_old_end = packet_log_end;
416		packet_log_end = 0;
417		if (packet_log_start > packet_log_old_end) {
418			/* calculate the head room */
419			spare = packet_log_start - packet_log_old_end;
420		} else {
421			spare = 0;
422		}
423		needed = total_len - spare;
424		packet_log_wrapped = 1;
425		/* Now update the start */
426		while (needed > 0) {
427			thisone = (*(int *)(&packet_log_buffer[packet_log_start]));
428			needed -= thisone;
429			if (thisone == 0) {
430				int *foo;
431
432				foo = (int *)(&packet_log_buffer[packet_log_start]);
433				goto insane;
434			}
435			/* move to next one */
436			packet_log_start += thisone;
437		}
438	} else {
439		lenat = (int *)&packet_log_buffer[packet_log_end];
440		if (packet_log_start > packet_log_end) {
441			if ((packet_log_end + total_len) > packet_log_start) {
442				/* Now need to update killing some packets  */
443				needed = total_len - ((packet_log_start - packet_log_end));
444				while (needed > 0) {
445					thisone = (*(int *)(&packet_log_buffer[packet_log_start]));
446					needed -= thisone;
447					if (thisone == 0) {
448						goto insane;
449					}
450					/* move to next one */
451					packet_log_start += thisone;
452					if (((packet_log_start + sizeof(struct ip)) > SCTP_PACKET_LOG_SIZE) ||
453					    (packet_log_wrapped && (packet_log_start >= packet_log_old_end))) {
454						packet_log_start = 0;
455						packet_log_old_end = 0;
456						packet_log_wrapped = 0;
457						break;
458					}
459				}
460			}
461		}
462	}
463	if (((packet_log_end + total_len) >= SCTP_PACKET_LOG_SIZE) ||
464	    ((void *)((caddr_t)lenat) < (void *)packet_log_buffer) ||
465	    ((void *)((caddr_t)lenat + total_len) > (void *)&packet_log_buffer[SCTP_PACKET_LOG_SIZE])) {
466		/* Madness protection */
467insane:
468		printf("Went mad, end:%d start:%d len:%d wrapped:%d oe:%d - zapping\n",
469		    packet_log_end, packet_log_start, total_len, packet_log_wrapped, packet_log_old_end);
470		packet_log_start = packet_log_end = packet_log_old_end = packet_log_wrapped = 0;
471		lenat = (int *)&packet_log_buffer[0];
472	}
473	*lenat = total_len;
474	lenat++;
475	tick_tock = (uint32_t *) lenat;
476	lenat++;
477	*tick_tock = sctp_get_tick_count();
478	copyto = (void *)lenat;
479	packet_log_end = (((caddr_t)copyto + length) - (caddr_t)packet_log_buffer);
480	SCTP_IP_PKTLOG_UNLOCK();
481	m_copydata(m, 0, length, (caddr_t)copyto);
482
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, copied = 0;
494	void *at;
495
496	tocopy = length;
497	if (packet_log_start == packet_log_end) {
498		/* no data */
499		return (0);
500	}
501	if (packet_log_wrapped) {
502		/*
503		 * we have a wrapped buffer, we must copy from start to the
504		 * old end. Then copy from the top of the buffer to the end.
505		 */
506		SCTP_IP_PKTLOG_LOCK();
507		at = (void *)&packet_log_buffer[packet_log_start];
508		this_copy = min(tocopy, (packet_log_old_end - packet_log_start));
509		memcpy(target, at, this_copy);
510		tocopy -= this_copy;
511		copied += this_copy;
512		if (tocopy == 0) {
513			SCTP_IP_PKTLOG_UNLOCK();
514			return (copied);
515		}
516		this_copy = min(tocopy, packet_log_end);
517		at = (void *)&packet_log_buffer;
518		memcpy(&target[copied], at, this_copy);
519		copied += this_copy;
520		SCTP_IP_PKTLOG_UNLOCK();
521		return (copied);
522	} else {
523		/* we have one contiguous buffer */
524		SCTP_IP_PKTLOG_LOCK();
525		at = (void *)&packet_log_buffer;
526		this_copy = min(length, packet_log_end);
527		memcpy(target, at, this_copy);
528		SCTP_IP_PKTLOG_UNLOCK();
529		return (this_copy);
530	}
531}
532
533#endif
534