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