2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1987, 1988, 1989 Apple Computer, Inc.
29 * Modified for MP, 1996 by Tuyen Nguyen
30 * Added AURP support, April 8, 1996 by Tuyen Nguyen
31 * Modified, March 17, 1997 by Tuyen Nguyen for MacOSX.
34 #define RESOLVE_DBG /* define debug globals in debug.h */
36 #include <sys/errno.h>
37 #include <sys/types.h>
38 #include <sys/param.h>
39 #include <machine/spl.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
43 #include <sys/filedesc.h>
44 #include <sys/fcntl.h>
46 #include <sys/ioctl.h>
47 #include <sys/malloc.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/protosw.h>
55 #include <netat/sysglue.h>
56 #include <netat/appletalk.h>
57 #include <netat/at_var.h>
58 #include <netat/ddp.h>
60 #include <netat/nbp.h>
61 #include <netat/rtmp.h>
62 #include <netat/zip.h>
63 #include <netat/at_pcb.h>
64 #include <netat/routing_tables.h>
65 #include <netat/at_snmp.h>
66 #include <netat/aurp.h>
67 #include <netat/debug.h>
68 #include <netat/at_ddp_brt.h>
69 #include <netat/at_aarp.h>
70 #include <netat/adsp.h>
71 #include <netat/adsp_internal.h>
75 /* Queue of LAP interfaces which have registered themselves with DDP */
76 struct at_ifQueueHd at_ifQueueHd
;
78 extern at_state_t at_state
;
79 extern TAILQ_HEAD(name_registry
, _nve_
) name_registry
;
81 snmpStats_t snmpStats
; /* snmp ddp & echo stats */
83 extern at_ddp_stats_t at_ddp_stats
; /* DDP statistics */
84 extern struct atpcb ddp_head
;
85 extern at_ifaddr_t
*ifID_home
, *ifID_table
[];
86 extern aarp_amt_array
*aarp_table
[];
87 extern at_ifaddr_t at_interfaces
[];
89 /* routing mode special */
90 void (*ddp_AURPsendx
)();
91 at_ifaddr_t
*aurp_ifID
= 0;
92 extern pktsIn
,pktsOut
;
93 int pktsDropped
,pktsHome
;
98 extern int *adsp_pidM
;
99 extern struct atpcb
*atp_inputQ
[];
100 extern CCB
*adsp_inputQ
[];
102 at_ifaddr_t
*forUs(at_ddp_t
*);
104 void ddp_input(), ddp_notify_nbp();
106 extern void routing_needed();
107 extern void ddp_brt_sweep();
113 void init_ddp_handler()
115 bzero(ddp_handler
, sizeof(ddp_handler
));
118 void add_ddp_handler(ddp_socket
, input_func
)
120 void (*input_func
)();
122 ddp_handler
[ddp_socket
].func
= input_func
;
132 * Raw DDP socket option processing.
134 int ddp_ctloutput(so
, sopt
)
136 struct sockopt
*sopt
;
138 struct atpcb
*at_pcb
= sotoatpcb(so
);
139 int optval
, error
= 0;
141 if (sopt
->sopt_level
!= ATPROTO_NONE
)
144 switch (sopt
->sopt_dir
) {
147 switch (sopt
->sopt_name
) {
149 optval
= at_pcb
->ddp_flags
& DDPFLG_HDRINCL
;
150 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
153 optval
= at_pcb
->ddp_flags
& DDPFLG_CHKSUM
;
154 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
157 optval
= at_pcb
->ddp_flags
& DDPFLG_STRIPHDR
;
158 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
161 optval
= at_pcb
->ddp_flags
& DDPFLG_SLFSND
;
162 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
164 case DDP_GETSOCKNAME
:
167 addr
.inet
.net
= at_pcb
->laddr
.s_net
;
168 addr
.inet
.node
= at_pcb
->laddr
.s_node
;
169 addr
.inet
.socket
= at_pcb
->lport
;
170 addr
.ddptype
= at_pcb
->ddptype
;
171 error
= sooptcopyout(sopt
, &addr
, sizeof addr
);
180 switch (sopt
->sopt_name
) {
182 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
187 at_pcb
->ddp_flags
|= DDPFLG_HDRINCL
;
189 at_pcb
->ddp_flags
&= ~DDPFLG_HDRINCL
;
192 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
197 at_pcb
->ddp_flags
|= DDPFLG_CHKSUM
;
199 at_pcb
->ddp_flags
&= ~DDPFLG_CHKSUM
;
202 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
207 at_pcb
->ddp_flags
|= DDPFLG_STRIPHDR
;
209 at_pcb
->ddp_flags
&= ~DDPFLG_STRIPHDR
;
212 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
217 at_pcb
->ddp_flags
|= DDPFLG_SLFSND
;
219 at_pcb
->ddp_flags
&= ~DDPFLG_SLFSND
;
231 /****************************************************************/
234 /* Support Routines */
237 /****************************************************************/
244 * This procedure determines the checksum of an extended DDP datagram.
245 * Add the unsigned bytes into an unsigned 16-bit accumulator.
246 * After each add, rotate the sign bit into the low order bit of
247 * the accumulator. When done, if the checksum is 0, changed into 0xFFFF.
250 * checksum = ddp_checksum(mp, offset)
253 * mp pointer to the datagram gbuf_t
254 * offset offset to start at in first gbuf_t block
261 u_short
ddp_checksum(mp
, offset
)
265 register u_char
*data
;
267 register u_short checksum
;
272 if (offset
>= gbuf_len(mp
))
273 offset
-= gbuf_len(mp
);
275 data
= ((unsigned char *) gbuf_rptr(mp
)) + offset
;
276 length
= gbuf_len(mp
) - offset
;
278 /* Portable checksum from 3.0 */
281 checksum
= (checksum
& 0x8000) ?
282 ((checksum
<< 1) | 1) : (checksum
<< 1);
285 } while ( (mp
= gbuf_cont(mp
)) );
297 * This procedure is called by each LAP interface when it wants to place
298 * itself online. The LAP interfaces passes in a pointer to its at_if
299 * struct, which is added to DDP's list of active interfaces (at_ifQueueHd).
300 * When DDP wants to transmit a packet, it searches this list for the
303 * If AT_IFF_DEFAULT is set, then this interface is to be brought online
304 * as the interface DDP socket addresses are tied to. Of course there can
305 * be only one default interface; we return an error if it's already set.
308 * ret_status = ddp_add_if(ifID)
311 * ifID pointer to LAP interface's at_if struct.
314 * 0 Procedure successfully completed.
315 * EALREADY This interface is already online, or there is
316 * already a default interface.
317 * ENOBUFS Cannot allocate input queue
321 register at_ifaddr_t
*ifID
;
325 dPrintf(D_M_DDP
, D_L_STARTUP
,
326 ("ddp_add_if: called, ifID:0x%x\n", (u_int
) ifID
));
328 if (ifID
->ifFlags
& AT_IFF_DEFAULT
) {
330 return(EEXIST
); /* home port already set */
336 for (port
=IFID_HOME
+1; port
<IF_TOTAL_MAX
; port
++)
337 if (!ifID_table
[port
]) {
340 if (port
== IF_TOTAL_MAX
) /* no space left */
344 /* allocate an et_aarp_amt structure */
345 if ((aarp_table
[port
] =
346 (aarp_amt_array
*)_MALLOC(sizeof(aarp_amt_array
),
347 M_RTABLE
, M_WAITOK
)) == NULL
)
350 dPrintf(D_M_DDP
, D_L_STARTUP
, ("ddp:adding ifID_table[%d]\n", port
));
352 /* add i/f to port list */
353 ifID_table
[port
] = ifID
;
354 ifID
->ifPort
= port
; /* set ddp port # in ifID */
356 /* Add this interface to the list of online interfaces */
357 TAILQ_INSERT_TAIL(&at_ifQueueHd
, ifID
, aa_link
);
366 * This procedure is called by each LAP interface when it wants to take
367 * itself offline. The LAP interfaces passes in a pointer to its at_if
368 * struct; DDP's list of active interfaces (at_ifQueueHd) is searched and
369 * this interface is removed from the list. DDP can still transmit
370 * packets as long as this interface is not the default interface; the
371 * sender will just get ENETUNREACH errors when it tries to send to an
372 * interface that went offline. However, if the default interface is
373 * taken offline, we no longer have a node ID to use as a source address
374 * and DDP must return ENETDOWN when a caller tries to send a packet.
377 * ifID pointer to LAP interface's at_if struct.
380 void ddp_rem_if(ifID
)
381 register at_ifaddr_t
*ifID
;
383 struct ifaddr
*ifa
= &ifID
->aa_ifa
;
385 /* un-do processing done in SIOCSIFADDR */
388 TAILQ_REMOVE(&ifID
->aa_ifp
->if_addrhead
, ifa
, ifa_link
);
389 ifa
->ifa_addr
= NULL
;
392 if (ifID
->at_dl_tag
) {
393 /* dlil_detach_protocol(ifID->at_dl_tag); */
394 ether_detach_at(ifID
->aa_ifp
);
398 /* un-do processing done in ddp_add_if() */
400 if (aarp_table
[ifID
->ifPort
]) {
401 FREE(aarp_table
[ifID
->ifPort
], M_RTABLE
);
402 aarp_table
[ifID
->ifPort
] = NULL
;
405 at_state
.flags
|= AT_ST_IF_CHANGED
;
408 trackrouter_rem_if(ifID
);
409 TAILQ_REMOVE(&at_ifQueueHd
, ifID
, aa_link
);
410 ifID_table
[ifID
->ifPort
] = NULL
;
411 ifID
->ifName
[0] = '\0';
415 /* *** deallocate ifID, eventually *** */
419 * The user may have registered an NVE with the NBP on a socket. When the
420 * socket is closed, the NVE should be deleted from NBP's name table. The
421 * user should delete the NVE before the socket is shut down, but there
422 * may be circumstances when he can't. So, whenever a DDP socket is closed,
423 * this routine is used to notify NBP of the socket closure. This would
424 * help NBP get rid of all NVE's registered on the socket.
427 /* *** Do we still need to do this? *** */
428 int ot_ddp_check_socket(socket
, pid
)
429 unsigned char socket
;
435 dPrintf(D_M_DDP
, D_L_INFO
, ("ot_ddp_check_socket: %d\n", socket
));
436 for (gref
= ddp_head
.atpcb_next
; gref
!= &ddp_head
; gref
= gref
->atpcb_next
)
437 if (gref
->lport
== socket
&& gref
->pid
== pid
)
439 if ((atp_inputQ
[socket
] != NULL
) && (atp_inputQ
[socket
] != (gref_t
*)1)
440 && (atp_pidM
[socket
] == pid
))
442 if ((adsp_inputQ
[socket
] != NULL
) && (adsp_pidM
[socket
] == pid
))
448 void ddp_notify_nbp(socket
, pid
, ddptype
)
449 unsigned char socket
;
451 unsigned char ddptype
; /* not used */
454 nve_entry_t
*nve_entry
, *nve_next
;
456 if (at_state
.flags
& AT_ST_STARTED
) {
457 /* *** NBP_CLOSE_NOTE processing (from ddp_nbp.c) *** */
458 ATDISABLE(nve_lock
, NVE_LOCK
);
459 for ((nve_entry
= TAILQ_FIRST(&name_registry
)); nve_entry
; nve_entry
= nve_next
) {
460 nve_next
= TAILQ_NEXT(nve_entry
, nve_link
);
461 if ((at_socket
)socket
== nve_entry
->address
.socket
&&
462 /* *** check complete address and ddptype here *** */
463 pid
== nve_entry
->pid
&&
464 ot_ddp_check_socket(nve_entry
->address
.socket
,
465 nve_entry
->pid
) < 2) {
466 /* NB: nbp_delete_entry calls TAILQ_REMOVE */
467 nbp_delete_entry(nve_entry
);
470 ATENABLE(nve_lock
, NVE_LOCK
);
472 } /* ddp_notify_nbp */
474 static void fillin_pkt_chain(m
)
479 *ddp
= (at_ddp_t
*)gbuf_rptr(m
),
483 if (UAS_VALUE(ddp
->checksum
)) {
484 tmp
= ddp_checksum(m
, 4);
485 UAS_ASSIGN(ddp
->checksum
, tmp
);
488 for (tmp_m
=gbuf_next(tmp_m
); tmp_m
; tmp_m
=gbuf_next(tmp_m
)) {
489 tmp_ddp
= (at_ddp_t
*)gbuf_rptr(tmp_m
);
490 tmp_ddp
->length
= gbuf_msgsize(tmp_m
);
493 NET_NET(tmp_ddp
->src_net
, ddp
->src_net
);
494 tmp_ddp
->src_node
= ddp
->src_node
;
495 tmp_ddp
->src_socket
= ddp
->src_socket
;
496 if (UAS_VALUE(tmp_ddp
->checksum
)) {
497 tmp
= ddp_checksum(tmp_m
, 4);
498 UAS_ASSIGN(tmp_ddp
->checksum
, tmp
);
503 /* There are various ways a packet may go out.... it may be sent out
504 * directly to destination node, or sent to a random router or sent
505 * to a router whose entry exists in Best Router Cache. Following are
506 * constants used WITHIN this routine to keep track of choice of destination
508 #define DIRECT_ADDR 1
510 #define BRIDGE_ADDR 3
516 * Called to queue a atp/ddp data packet on the network interface.
517 * It returns 0 normally, and an errno in case of error.
518 * The mbuf chain pointed to by *mp is consumed on success, and
519 * freed in case of error.
522 int ddp_output(mp
, src_socket
, src_addr_included
)
523 register gbuf_t
**mp
;
524 at_socket src_socket
;
525 int src_addr_included
;
527 register at_ifaddr_t
*ifID
= ifID_home
, *ifIDTmp
= NULL
;
528 register at_ddp_t
*ddp
;
529 register ddp_brt_t
*brt
;
530 register at_net_al dst_net
;
532 struct atalk_addr at_dest
;
533 at_ifaddr_t
*ARouterIf
= NULL
;
541 KERNEL_DEBUG(DBG_AT_DDP_OUTPUT
| DBG_FUNC_START
, 0,
544 snmpStats
.dd_outReq
++;
547 ddp
= (at_ddp_t
*)gbuf_rptr(m
);
550 /* Device/Interface not configured */
551 dPrintf(D_M_DDP
, D_L_ERROR
, ("Device/Interface not configured"));
554 goto exit_ddp_output
;
557 if ((ddp
->dst_socket
> (unsigned) (DDP_SOCKET_LAST
+ 1)) ||
558 (ddp
->dst_socket
< DDP_SOCKET_1st_RESERVED
)) {
559 dPrintf(D_M_DDP
, D_L_ERROR
,
560 ("Illegal destination socket on outgoing packet (0x%x)",
562 at_ddp_stats
.xmit_bad_addr
++;
565 goto exit_ddp_output
;
567 if ((len
= gbuf_msgsize(*mp
)) > DDP_DATAGRAM_SIZE
) {
568 /* the packet is too large */
569 dPrintf(D_M_DDP
, D_L_ERROR
,
570 ("Outgoing packet too long (len=%d bytes)", len
));
571 at_ddp_stats
.xmit_bad_length
++;
574 goto exit_ddp_output
;
576 at_ddp_stats
.xmit_bytes
+= len
;
577 at_ddp_stats
.xmit_packets
++;
583 /* If this packet is for the same node, loop it back
584 * up... Note that for LocalTalk, dst_net zero means "THIS_NET", so
585 * address 0.nn is eligible for loopback. For Extended EtherTalk,
586 * dst_net 0 can be used only for cable-wide or zone-wide
587 * broadcasts (0.ff) and as such, address of the form 0.nn is NOT
588 * eligible for loopback.
590 dst_net
= NET_VALUE(ddp
->dst_net
);
592 /* If our packet is destined for the 'virtual' bridge
593 * address of NODE==0xFE, replace that address with a
594 * real bridge address.
596 if ((ddp
->dst_node
== 0xfe) &&
597 ((dst_net
== ATADDR_ANYNET
) ||
598 (dst_net
>= ifID_home
->ifThisCableStart
&&
599 dst_net
<= ifID_home
->ifThisCableEnd
))) {
600 /* if there's a router that's not us, it's in ifID_home */
601 NET_ASSIGN(ddp
->dst_net
, ifID_home
->ifARouter
.s_net
);
602 dst_net
= ifID_home
->ifARouter
.s_net
;
603 ddp
->dst_node
= ifID_home
->ifARouter
.s_node
;
606 if (MULTIHOME_MODE
&& (ifIDTmp
= forUs(ddp
))) {
609 dPrintf(D_M_DDP_LOW
, D_L_USR1
,
610 ("ddp_out: for us if:%s\n", ifIDTmp
->ifName
));
614 loop
= ((ddp
->dst_node
== ifID
->ifThisNode
.s_node
) &&
615 (dst_net
== ifID
->ifThisNode
.s_net
)
618 gbuf_t
*mdata
, *mdata_next
;
620 if (!MULTIHOME_MODE
|| !src_addr_included
) {
621 NET_ASSIGN(ddp
->src_net
, ifID
->ifThisNode
.s_net
);
622 ddp
->src_node
= ifID
->ifThisNode
.s_node
;
624 ddp
->src_socket
= src_socket
;
626 dPrintf(D_M_DDP_LOW
, D_L_OUTPUT
,
627 ("ddp_output: loop to %d:%d port=%d\n",
628 NET_VALUE(ddp
->dst_net
),
632 fillin_pkt_chain(*mp
);
634 dPrintf(D_M_DDP
, D_L_VERBOSE
,
635 ("Looping back packet from skt 0x%x to skt 0x%x\n",
636 ddp
->src_socket
, ddp
->dst_socket
));
638 for (mdata
= *mp
; mdata
; mdata
= mdata_next
) {
639 mdata_next
= gbuf_next(mdata
);
640 gbuf_next(mdata
) = 0;
641 ddp_input(mdata
, ifID
);
643 goto exit_ddp_output
;
645 if ((ddp
->dst_socket
== ZIP_SOCKET
) &&
646 (zip_type_packet(*mp
) == ZIP_GETMYZONE
)) {
647 ddp
->src_socket
= src_socket
;
648 error
= zip_handle_getmyzone(ifID
, *mp
);
650 goto exit_ddp_output
;
653 * find out the interface on which the packet should go out
655 TAILQ_FOREACH(ifID
, &at_ifQueueHd
, aa_link
) {
656 if ((ifID
->ifThisNode
.s_net
== dst_net
) || (dst_net
== 0))
657 /* the message is either going out (i) on the same
658 * NETWORK in case of LocalTalk, or (ii) on the same
659 * CABLE in case of Extended AppleTalk (EtherTalk).
663 if ((ifID
->ifThisCableStart
<= dst_net
) &&
664 (ifID
->ifThisCableEnd
>= dst_net
)
666 /* We're on EtherTalk and the message is going out to
667 * some other network on the same cable.
671 if (ARouterIf
== NULL
&& ATALK_VALUE(ifID
->ifARouter
))
674 dPrintf(D_M_DDP_LOW
, D_L_USR1
,
675 ("ddp_output: after search ifid:0x%x %s ifID_home:0x%x\n",
676 (u_int
)ifID
, ifID
? ifID
->ifName
: "",
680 /* located the interface where the packet should
681 * go.... the "first-hop" destination address
682 * must be the same as real destination address.
684 addr_type
= DIRECT_ADDR
;
686 /* no, the destination network number does
687 * not match known network numbers. If we have
688 * heard from this network recently, BRT table
689 * may have address of a router we could use!
691 if (!MULTIPORT_MODE
) {
692 BRT_LOOK (brt
, dst_net
);
694 /* Bingo... BRT has an entry for this network.
695 * Use the link address as is.
697 dPrintf(D_M_DDP
, D_L_VERBOSE
,
698 ("Found BRT entry to send to net 0x%x", dst_net
));
699 at_ddp_stats
.xmit_BRT_used
++;
700 addr_type
= BRT_ENTRY
;
703 /* No BRT entry available for dest network... do we
704 * know of any router at all??
706 if ((ifID
= ARouterIf
) != NULL
)
707 addr_type
= BRIDGE_ADDR
;
709 dPrintf(D_M_DDP
, D_L_WARNING
,
710 ("Found no interface to send pkt"));
711 at_ddp_stats
.xmit_bad_addr
++;
714 goto exit_ddp_output
;
718 else { /* We are in multiport mode, so we can bypass all the rest
719 * and directly ask for the routing of the packet
721 at_ddp_stats
.xmit_BRT_used
++;
724 if (!src_addr_included
) {
725 ddp
->src_node
= ifID
->ifThisNode
.s_node
;
726 NET_ASSIGN(ddp
->src_net
, ifID
->ifThisNode
.s_net
);
728 ddp
->src_socket
= src_socket
;
729 routing_needed(*mp
, ifID
, TRUE
);
731 goto exit_ddp_output
;
734 /* by the time we land here, we know the interface on
735 * which this packet is going out.... ifID.
737 if (ifID
->ifState
== LAP_OFFLINE
) {
739 goto exit_ddp_output
;
745 at_dest.atalk_unused = 0;
747 NET_ASSIGN(at_dest
.atalk_net
, dst_net
);
748 at_dest
.atalk_node
= ddp
->dst_node
;
750 addr
= (char *)&at_dest
;
754 addr
= (char *)&brt
->et_addr
;
757 NET_ASSIGN(at_dest
.atalk_net
, ifID
->ifARouter
.s_net
);
758 at_dest
.atalk_node
= ifID
->ifARouter
.s_node
;
760 addr
= (char *)&at_dest
;
764 /* Irrespective of the interface on which
765 * the packet is going out, we always put the
766 * same source address on the packet (unless multihoming mode).
768 if (MULTIHOME_MODE
) {
769 if (!src_addr_included
) {
770 ddp
->src_node
= ifID
->ifThisNode
.s_node
;
771 NET_ASSIGN(ddp
->src_net
, ifID
->ifThisNode
.s_net
);
775 ddp
->src_node
= ifID_home
->ifThisNode
.s_node
;
776 NET_ASSIGN(ddp
->src_net
, ifID_home
->ifThisNode
.s_net
);
778 ddp
->src_socket
= src_socket
;
780 dPrintf(D_M_DDP_LOW
, D_L_OUTPUT
,
781 ("ddp_output: going out to %d:%d skt%d on %s\n",
782 dst_net
, ddp
->dst_node
, ddp
->dst_socket
, ifID
->ifName
));
784 fillin_pkt_chain(*mp
);
787 struct etalk_addr dest_addr
;
788 struct atalk_addr dest_at_addr
;
789 int loop
= TRUE
; /* flag to aarp to loopback (default) */
793 /* the incoming frame is of the form {flag, address, ddp...}
794 * where "flag" indicates whether the address is an 802.3
795 * (link) address, or an appletalk address. If it's an
796 * 802.3 address, the packet can just go out to the network
797 * through PAT, if it's an appletalk address, AT->802.3 address
798 * resolution needs to be done.
799 * If 802.3 address is known, strip off the flag and 802.3
800 * address, and prepend 802.2 and 802.3 headers.
804 addr_flag
= *(u_char
*)gbuf_rptr(m
);
809 case AT_ADDR_NO_LOOP
:
814 dest_at_addr
= *(struct atalk_addr
*)gbuf_rptr(m
);
815 gbuf_rinc(m
,sizeof(struct atalk_addr
));
817 dest_at_addr
= *(struct atalk_addr
*)addr
;
821 dest_addr
= *(struct etalk_addr
*)gbuf_rptr(m
);
822 gbuf_rinc(m
,sizeof(struct etalk_addr
));
824 dest_addr
= *(struct etalk_addr
*)addr
;
827 dPrintf(D_M_DDP_LOW
,D_L_ERROR
,
828 ("ddp_output: Unknown addr_flag = 0x%x\n", addr_flag
));
829 gbuf_freel(m
); /* unknown address type, chuck it */
830 goto exit_ddp_output
;
835 /* At this point, rptr points to ddp header for sure */
836 if (ifID
->ifState
== LAP_ONLINE_FOR_ZIP
) {
837 /* see if this is a ZIP packet that we need
838 * to let through even though network is
841 if (zip_type_packet(m
) == 0) {
843 goto exit_ddp_output
;
847 ifID
->stats
.xmit_packets
++;
848 ifID
->stats
.xmit_bytes
+= gbuf_msgsize(m
);
849 snmpStats
.dd_outLong
++;
852 case AT_ADDR_NO_LOOP
:
855 * we don't want elap to be looking into ddp header, so
856 * it doesn't know net#, consequently can't do
857 * AMT_LOOKUP. That task left to aarp now.
859 aarp_send_data(m
,ifID
,&dest_at_addr
, loop
);
862 pat_output(ifID
, m
, &dest_addr
, 0);
867 KERNEL_DEBUG(DBG_AT_DDP_OUTPUT
| DBG_FUNC_END
, 0,
872 void ddp_input(mp
, ifID
)
874 register at_ifaddr_t
*ifID
;
876 register at_ddp_t
*ddp
; /* DDP header */
877 register int msgsize
;
878 register at_socket socket
;
880 register at_net_al dst_net
;
882 KERNEL_DEBUG(DBG_AT_DDP_INPUT
| DBG_FUNC_START
, 0,
883 ifID
, mp
, gbuf_len(mp
),0);
885 /* Makes sure we know the default interface before starting to
886 * accept incomming packets. If we don't we may end up with a
887 * null ifID_table[0] and have impredicable results (specially
888 * in router mode. This is a transitory state (because we can
889 * begin to receive packet while we're not completly set up yet.
892 if (ifID_home
== (at_ifaddr_t
*)NULL
) {
893 dPrintf(D_M_DDP
, D_L_ERROR
,
894 ("dropped incoming packet ifID_home not set yet\n"));
896 goto out
; /* return */
900 * if a DDP packet has been broadcast, we're going to get a copy of
901 * it here; if it originated at user level via a write on a DDP
902 * socket; when it gets here, the first block in the chain will be
903 * empty since it only contained the lap level header which will be
904 * stripped in the lap level immediately below ddp
907 if ((mp
= (gbuf_t
*)ddp_compress_msg(mp
)) == NULL
) {
908 dPrintf(D_M_DDP
, D_L_ERROR
,
909 ("dropped short incoming ET packet (len %d)", 0));
910 snmpStats
.dd_inTotal
++;
911 at_ddp_stats
.rcv_bad_length
++;
912 goto out
; /* return; */
914 msgsize
= gbuf_msgsize(mp
);
916 at_ddp_stats
.rcv_bytes
+= msgsize
;
917 at_ddp_stats
.rcv_packets
++;
919 /* if the interface pointer is 0, the packet has been
920 * looped back by 'write' half of DDP. It is of the
921 * form {extended ddp,...}. The packet is meant to go
922 * up to some socket on the same node.
924 if (!ifID
) /* if loop back is specified */
925 ifID
= ifID_home
; /* that means the home port */
927 /* the incoming datagram has extended DDP header and is of
928 * the form {ddp,...}.
930 if (msgsize
< DDP_X_HDR_SIZE
) {
931 dPrintf(D_M_DDP
, D_L_ERROR
,
932 ("dropped short incoming ET packet (len %d)", msgsize
));
933 at_ddp_stats
.rcv_bad_length
++;
935 goto out
; /* return; */
938 * At this point, the message is always of the form
939 * {extended ddp, ... }.
941 ddp
= (at_ddp_t
*)gbuf_rptr(mp
);
944 if (msgsize
!= len
) {
945 if ((unsigned) msgsize
> len
) {
946 if (len
< DDP_X_HDR_SIZE
) {
947 dPrintf(D_M_DDP
, D_L_ERROR
,
948 ("Length problems, ddp length %d, buffer length %d",
950 snmpStats
.dd_tooLong
++;
951 at_ddp_stats
.rcv_bad_length
++;
953 goto out
; /* return; */
956 * shave off the extra bytes from the end of message
958 mp
= ddp_adjmsg(mp
, -(msgsize
- len
)) ? mp
: 0;
960 goto out
; /* return; */
962 dPrintf(D_M_DDP
, D_L_ERROR
,
963 ("Length problems, ddp length %d, buffer length %d",
965 snmpStats
.dd_tooShort
++;
966 at_ddp_stats
.rcv_bad_length
++;
968 goto out
; /* return; */
971 socket
= ddp
->dst_socket
;
974 * We want everything in router mode, specially socket 254 for nbp so we need
975 * to bypass this test when we are a router.
978 if (!MULTIPORT_MODE
&& (socket
> DDP_SOCKET_LAST
||
979 socket
< DDP_SOCKET_1st_RESERVED
)) {
980 dPrintf(D_M_DDP
, D_L_WARNING
,
981 ("Bad dst socket on incoming packet (0x%x)",
983 at_ddp_stats
.rcv_bad_socket
++;
985 goto out
; /* return; */
988 * if the checksum is true, then upstream wants us to calc
990 if (UAS_VALUE(ddp
->checksum
) &&
991 (UAS_VALUE(ddp
->checksum
) != ddp_checksum(mp
, 4))) {
992 dPrintf(D_M_DDP
, D_L_WARNING
,
993 ("Checksum error on incoming pkt, calc 0x%x, exp 0x%x",
994 ddp_checksum(mp
, 4), UAS_VALUE(ddp
->checksum
)));
995 snmpStats
.dd_checkSum
++;
996 at_ddp_stats
.rcv_bad_checksum
++;
998 goto out
; /* return; */
1001 /*############### routing input checking */
1003 /* Router mode special: we send "up-stack" packets for this node or coming from any
1004 * other ports, but for the reserved atalk sockets (RTMP, ZIP, NBP [and EP])
1005 * BTW, the way we know it's for the router and not the home port is that the
1006 * MAC (ethernet) address is always the one of the interface we're on, but
1007 * the AppleTalk address must be the one of the home port. If it's a multicast
1008 * or another AppleTalk address, this is the router job's to figure out where it's
1011 /* *** a duplicate should be sent to any other client that is listening
1012 for packets of this type on a raw DDP socket *** */
1013 if (ddp_handler
[socket
].func
) {
1014 dPrintf(D_M_DDP
,D_L_INPUT
,
1015 ("ddp_input: skt %d hdnlr:0x%x\n",
1016 (u_int
) socket
, ddp_handler
[socket
].func
));
1018 snmpStats
.dd_inLocal
++;
1020 (*ddp_handler
[socket
].func
)(mp
, ifID
);
1021 goto out
; /* return; */
1023 dst_net
= NET_VALUE(ddp
->dst_net
);
1027 /* any node, wildcard or matching net */
1028 ((ddp
->dst_node
== 255) &&
1029 (((dst_net
>= ifID_home
->ifThisCableStart
) &&
1030 (dst_net
<= ifID_home
->ifThisCableEnd
)) ||
1032 /* this node is not online yet(?) */
1033 (ifID
->ifRoutingState
< PORT_ONLINE
)
1037 snmpStats
.dd_inLocal
++;
1039 if (ddp
->type
== DDP_ATP
) {
1040 if (atp_inputQ
[socket
] && (atp_inputQ
[socket
] != (gref_t
*)1)) {
1041 /* if there's an ATP pcb */
1043 goto out
; /* return; */
1045 } else if (ddp
->type
== DDP_ADSP
) {
1046 if (adsp_inputQ
[socket
]) {
1047 /* if there's an ADSP pcb */
1049 goto out
; /* return; */
1053 /* otherwise look for a DDP pcb;
1054 ATP / raw-DDP and ADSP / raw-DDP are possible */
1055 for (gref
= ddp_head
.atpcb_next
; gref
!= &ddp_head
;
1056 gref
= gref
->atpcb_next
)
1057 if (gref
->lport
== socket
&&
1058 (gref
->ddptype
== 0 || gref
->ddptype
== ddp
->type
)) {
1059 dPrintf(D_M_DDP
, D_L_INPUT
,
1060 ("ddp_input: streamq, skt %d\n", socket
));
1061 if (gref
->atpcb_socket
) {
1062 struct sockaddr_at ddp_in
;
1063 ddp_in
.sat_len
= sizeof(ddp_in
);
1064 ddp_in
.sat_family
= AF_APPLETALK
;
1065 ddp_in
.sat_addr
.s_net
= NET_VALUE(ddp
->src_net
);
1066 ddp_in
.sat_addr
.s_node
= ddp
->src_node
;
1067 ddp_in
.sat_port
= ddp
->src_socket
;
1069 /* strip off DDP header if so indicated by
1071 if (gref
->ddp_flags
& DDPFLG_STRIPHDR
) {
1072 mp
= m_pullup((struct mbuf
*)mp
,
1075 gbuf_rinc(mp
, DDP_X_HDR_SIZE
);
1077 /* this should never happen because
1078 msgsize was checked earlier */
1079 at_ddp_stats
.rcv_bad_length
++;
1080 goto out
; /* return */
1084 if (sbappendaddr(&((gref
->atpcb_socket
)->so_rcv
),
1085 (struct sockaddr
*)&ddp_in
,
1089 sorwakeup(gref
->atpcb_socket
);
1091 atalk_putnext(gref
, mp
);
1093 goto out
; /* return */
1096 at_ddp_stats
.rcv_bad_socket
++;
1098 snmpStats
.dd_noHandler
++;
1099 dPrintf(D_M_DDP
, D_L_WARNING
,
1100 ("ddp_input: dropped pkt for socket %d\n", socket
));
1102 dPrintf(D_M_DDP
, D_L_ROUTING
,
1103 ("ddp_input: routing_needed from port=%d sock=%d\n",
1104 ifID
->ifPort
, ddp
->dst_socket
));
1106 snmpStats
.dd_fwdReq
++;
1107 if (((pktsIn
-pktsHome
+200) >= RouterMix
) && ((++pktsDropped
% 5) == 0)) {
1108 at_ddp_stats
.rcv_dropped_nobuf
++;
1112 routing_needed(mp
, ifID
, FALSE
);
1116 KERNEL_DEBUG(DBG_AT_DDP_INPUT
| DBG_FUNC_END
, 0,0,0,0,0);
1121 * ddp_router_output()
1124 * This is a modified version of ddp_output for router use.
1125 * The main difference is that the interface on which the packet needs
1126 * to be sent is specified and a *destination* AppleTalk address is passed
1127 * as an argument, this address may or may not be the same as the destination
1128 * address found in the ddp packet... This is the trick about routing, the
1129 * AppleTalk destination of the packet may not be the same as the Enet address
1130 * we send the packet too (ie, we may pass the baby to another router).
1133 int ddp_router_output(mp
, ifID
, addr_type
, router_net
, router_node
, enet_addr
)
1137 at_net_al router_net
;
1138 at_node router_node
;
1139 etalk_addr_t
*enet_addr
;
1141 register at_ddp_t
*ddp
;
1142 struct atalk_addr at_dest
;
1148 dPrintf(D_M_DDP
, D_L_WARNING
, ("BAD BAD ifID\n"));
1152 ddp
= (at_ddp_t
*)gbuf_rptr(mp
);
1154 if (ifID
->ifFlags
& AT_IFF_AURP
) { /* AURP link? */
1155 if (ddp_AURPsendx
) {
1156 fillin_pkt_chain(mp
);
1157 if (router_node
== 255)
1159 ddp_AURPsendx(AURPCODE_DATAPKT
, mp
, router_node
);
1167 /* keep some of the tests for now ####### */
1169 if (gbuf_msgsize(mp
) > DDP_DATAGRAM_SIZE
) {
1170 /* the packet is too large */
1171 dPrintf(D_M_DDP
, D_L_WARNING
,
1172 ("ddp_router_output: Packet too large size=%d\n",
1178 switch (addr_type
) {
1183 * Check for packet destined to the home stack
1186 if ((ddp
->dst_node
== ifID
->ifThisNode
.s_node
) &&
1187 (NET_VALUE(ddp
->dst_net
) == ifID
->ifThisNode
.s_net
)) {
1188 dPrintf(D_M_DDP_LOW
, D_L_ROUTING
,
1189 ("ddp_r_output: sending back home from port=%d socket=%d\n",
1190 ifID
->ifPort
, ddp
->dst_socket
));
1192 UAS_ASSIGN(ddp
->checksum
, 0);
1193 ddp_input(mp
, ifID
);
1197 NET_ASSIGN(at_dest
.atalk_net
, router_net
);
1198 at_dest
.atalk_node
= router_node
;
1200 addr_flag
= AT_ADDR_NO_LOOP
;
1201 addr
= (char *)&at_dest
;
1202 dPrintf(D_M_DDP_LOW
, D_L_ROUTING_AT
,
1203 ("ddp_r_output: AT_ADDR out port=%d net %d:%d via rte %d:%d",
1204 ifID
->ifPort
, NET_VALUE(ddp
->dst_net
), ddp
->dst_node
, router_net
,
1209 addr_flag
= ET_ADDR
;
1210 addr
= (char *)enet_addr
;
1211 dPrintf(D_M_DDP_LOW
, D_L_ROUTING
,
1212 ("ddp_r_output: ET_ADDR out port=%d net %d:%d\n",
1213 ifID
->ifPort
, NET_VALUE(ddp
->dst_net
), ddp
->dst_node
));
1217 if (ifID
->ifState
== LAP_OFFLINE
) {
1222 fillin_pkt_chain(mp
);
1225 struct etalk_addr dest_addr
;
1226 struct atalk_addr dest_at_addr
;
1227 int loop
= TRUE
; /* flag to aarp to loopback (default) */
1231 /* the incoming frame is of the form {flag, address, ddp...}
1232 * where "flag" indicates whether the address is an 802.3
1233 * (link) address, or an appletalk address. If it's an
1234 * 802.3 address, the packet can just go out to the network
1235 * through PAT, if it's an appletalk address, AT->802.3 address
1236 * resolution needs to be done.
1237 * If 802.3 address is known, strip off the flag and 802.3
1238 * address, and prepend 802.2 and 802.3 headers.
1242 addr_flag
= *(u_char
*)gbuf_rptr(m
);
1246 switch (addr_flag
) {
1247 case AT_ADDR_NO_LOOP
:
1252 dest_at_addr
= *(struct atalk_addr
*)gbuf_rptr(m
);
1253 gbuf_rinc(m
,sizeof(struct atalk_addr
));
1255 dest_at_addr
= *(struct atalk_addr
*)addr
;
1259 dest_addr
= *(struct etalk_addr
*)gbuf_rptr(m
);
1260 gbuf_rinc(m
,sizeof(struct etalk_addr
));
1262 dest_addr
= *(struct etalk_addr
*)addr
;
1265 dPrintf(D_M_DDP_LOW
,D_L_ERROR
,
1266 ("ddp_router_output: Unknown addr_flag = 0x%x\n", addr_flag
));
1268 gbuf_freel(m
); /* unknown address type, chuck it */
1274 /* At this point, rptr points to ddp header for sure */
1275 if (ifID
->ifState
== LAP_ONLINE_FOR_ZIP
) {
1276 /* see if this is a ZIP packet that we need
1277 * to let through even though network is
1280 if (zip_type_packet(m
) == 0) {
1286 ifID
->stats
.xmit_packets
++;
1287 ifID
->stats
.xmit_bytes
+= gbuf_msgsize(m
);
1288 snmpStats
.dd_outLong
++;
1290 switch (addr_flag
) {
1291 case AT_ADDR_NO_LOOP
:
1294 * we don't want elap to be looking into ddp header, so
1295 * it doesn't know net#, consequently can't do
1296 * AMT_LOOKUP. That task left to aarp now.
1298 aarp_send_data(m
,ifID
,&dest_at_addr
, loop
);
1301 pat_output(ifID
, m
, &dest_addr
, 0);
1307 } /* ddp_router_output */
1309 /*****************************************/
1311 void rt_delete(NetStop
, NetStart
)
1312 unsigned short NetStop
;
1313 unsigned short NetStart
;
1318 ATDISABLE(s
, ddpinp_lock
);
1319 if ((found
= rt_bdelete(NetStop
, NetStart
)) != 0) {
1320 bzero(found
, sizeof(RT_entry
));
1321 found
->right
= RT_table_freelist
;
1322 RT_table_freelist
= found
;
1324 ATENABLE(s
, ddpinp_lock
);
1327 int ddp_AURPfuncx(code
, param
, node
)
1332 extern void rtmp_timeout();
1333 extern void rtmp_send_port();
1338 case AURPCODE_DATAPKT
: /* data packet */
1340 dPrintf(D_M_DDP
, D_L_TRACE
, ("ddp_AURPfuncx: data, 0x%x, %d\n",
1341 (u_int
) aurp_ifID
, node
));
1343 ddp_input((gbuf_t
*)param
, aurp_ifID
);
1345 gbuf_freem((gbuf_t
*)param
);
1348 case AURPCODE_REG
: /* register/deregister */
1351 ddp_AURPsendx
= (void(*)())param
;
1354 /* register AURP callback function */
1357 for (k
=(IFID_HOME
+1); k
< IF_TOTAL_MAX
; k
++) {
1358 if (ifID_table
[k
] == 0) {
1359 aurp_ifID
= &at_interfaces
[k
];
1360 aurp_ifID
->ifFlags
= RTR_XNET_PORT
;
1361 ddp_add_if(aurp_ifID
);
1362 aurp_ifID
->ifState
= LAP_ONLINE
;
1363 aurp_ifID
->ifRoutingState
= PORT_ONLINE
;
1364 dPrintf(D_M_DDP
, D_L_TRACE
,
1365 ("ddp_AURPfuncx: on, 0x%x\n",
1366 (u_int
) aurp_ifID
));
1368 ddp_AURPsendx(AURPCODE_DEBUGINFO
,
1369 &dbgBits
, aurp_ifID
->ifPort
);
1376 /* deregister AURP callback function */
1378 rtmp_purge(aurp_ifID
);
1379 ddp_rem_if(aurp_ifID
);
1380 aurp_ifID
->ifState
= LAP_OFFLINE
;
1381 aurp_ifID
->ifRoutingState
= PORT_OFFLINE
;
1382 dPrintf(D_M_DDP
, D_L_TRACE
,
1383 ("ddp_AURPfuncx: off, 0x%x\n", (u_int
) aurp_ifID
));
1389 case AURPCODE_AURPPROTO
: /* proto type - AURP */
1391 aurp_ifID
->ifFlags
|= AT_IFF_AURP
;
1400 /* checks to see if address of packet is for one of our interfaces
1401 returns *ifID if it's for us, NULL if not
1403 at_ifaddr_t
*forUs(ddp
)
1404 register at_ddp_t
*ddp
;
1408 TAILQ_FOREACH(ifID
, &at_ifQueueHd
, aa_link
) {
1409 if ((ddp
->dst_node
== ifID
->ifThisNode
.s_node
) &&
1410 (NET_VALUE(ddp
->dst_net
) == ifID
->ifThisNode
.s_net
)
1412 dPrintf(D_M_DDP_LOW
, D_L_ROUTING
,
1413 ("pkt was for port %d\n", ifID
->ifPort
));
1419 return((at_ifaddr_t
*)NULL
);