2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 * Copyright (c) 1987, 1988, 1989 Apple Computer, Inc.
27 * Modified for MP, 1996 by Tuyen Nguyen
28 * Added AURP support, April 8, 1996 by Tuyen Nguyen
29 * Modified, March 17, 1997 by Tuyen Nguyen for MacOSX.
32 #define RESOLVE_DBG /* define debug globals in debug.h */
34 #include <sys/errno.h>
35 #include <sys/types.h>
36 #include <sys/param.h>
37 #include <machine/spl.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
41 #include <sys/filedesc.h>
42 #include <sys/fcntl.h>
44 #include <sys/ioctl.h>
45 #include <sys/malloc.h>
46 #include <sys/socket.h>
47 #include <sys/socketvar.h>
48 #include <sys/protosw.h>
53 #include <netat/sysglue.h>
54 #include <netat/appletalk.h>
55 #include <netat/at_var.h>
56 #include <netat/ddp.h>
58 #include <netat/nbp.h>
59 #include <netat/rtmp.h>
60 #include <netat/zip.h>
61 #include <netat/at_pcb.h>
62 #include <netat/routing_tables.h>
63 #include <netat/at_snmp.h>
64 #include <netat/aurp.h>
65 #include <netat/debug.h>
66 #include <netat/at_ddp_brt.h>
67 #include <netat/at_aarp.h>
68 #include <netat/adsp.h>
69 #include <netat/adsp_internal.h>
73 /* Queue of LAP interfaces which have registered themselves with DDP */
74 struct at_ifQueueHd at_ifQueueHd
;
76 extern at_state_t at_state
;
77 extern TAILQ_HEAD(name_registry
, _nve_
) name_registry
;
79 snmpStats_t snmpStats
; /* snmp ddp & echo stats */
81 extern at_ddp_stats_t at_ddp_stats
; /* DDP statistics */
82 extern struct atpcb ddp_head
;
83 extern at_ifaddr_t
*ifID_home
, *ifID_table
[];
84 extern aarp_amt_array
*aarp_table
[];
85 extern at_ifaddr_t at_interfaces
[];
87 /* routing mode special */
88 void (*ddp_AURPsendx
)();
89 at_ifaddr_t
*aurp_ifID
= 0;
90 extern pktsIn
,pktsOut
;
91 int pktsDropped
,pktsHome
;
96 extern int *adsp_pidM
;
97 extern struct atpcb
*atp_inputQ
[];
98 extern CCB
*adsp_inputQ
[];
100 at_ifaddr_t
*forUs(at_ddp_t
*);
102 void ddp_input(), ddp_notify_nbp();
104 extern void routing_needed();
105 extern void ddp_brt_sweep();
111 void init_ddp_handler()
113 bzero(ddp_handler
, sizeof(ddp_handler
));
116 void add_ddp_handler(ddp_socket
, input_func
)
118 void (*input_func
)();
120 ddp_handler
[ddp_socket
].func
= input_func
;
130 * Raw DDP socket option processing.
132 int ddp_ctloutput(so
, sopt
)
134 struct sockopt
*sopt
;
136 struct atpcb
*at_pcb
= sotoatpcb(so
);
137 int optval
, error
= 0;
139 if (sopt
->sopt_level
!= ATPROTO_NONE
)
142 switch (sopt
->sopt_dir
) {
145 switch (sopt
->sopt_name
) {
147 optval
= at_pcb
->ddp_flags
& DDPFLG_HDRINCL
;
148 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
151 optval
= at_pcb
->ddp_flags
& DDPFLG_CHKSUM
;
152 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
155 optval
= at_pcb
->ddp_flags
& DDPFLG_STRIPHDR
;
156 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
159 optval
= at_pcb
->ddp_flags
& DDPFLG_SLFSND
;
160 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
162 case DDP_GETSOCKNAME
:
165 addr
.inet
.net
= at_pcb
->laddr
.s_net
;
166 addr
.inet
.node
= at_pcb
->laddr
.s_node
;
167 addr
.inet
.socket
= at_pcb
->lport
;
168 addr
.ddptype
= at_pcb
->ddptype
;
169 error
= sooptcopyout(sopt
, &addr
, sizeof addr
);
178 switch (sopt
->sopt_name
) {
180 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
185 at_pcb
->ddp_flags
|= DDPFLG_HDRINCL
;
187 at_pcb
->ddp_flags
&= ~DDPFLG_HDRINCL
;
190 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
195 at_pcb
->ddp_flags
|= DDPFLG_CHKSUM
;
197 at_pcb
->ddp_flags
&= ~DDPFLG_CHKSUM
;
200 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
205 at_pcb
->ddp_flags
|= DDPFLG_STRIPHDR
;
207 at_pcb
->ddp_flags
&= ~DDPFLG_STRIPHDR
;
210 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
215 at_pcb
->ddp_flags
|= DDPFLG_SLFSND
;
217 at_pcb
->ddp_flags
&= ~DDPFLG_SLFSND
;
229 /****************************************************************/
232 /* Support Routines */
235 /****************************************************************/
242 * This procedure determines the checksum of an extended DDP datagram.
243 * Add the unsigned bytes into an unsigned 16-bit accumulator.
244 * After each add, rotate the sign bit into the low order bit of
245 * the accumulator. When done, if the checksum is 0, changed into 0xFFFF.
248 * checksum = ddp_checksum(mp, offset)
251 * mp pointer to the datagram gbuf_t
252 * offset offset to start at in first gbuf_t block
259 u_short
ddp_checksum(mp
, offset
)
263 register u_char
*data
;
265 register u_short checksum
;
270 if (offset
>= gbuf_len(mp
))
271 offset
-= gbuf_len(mp
);
273 data
= ((unsigned char *) gbuf_rptr(mp
)) + offset
;
274 length
= gbuf_len(mp
) - offset
;
276 /* Portable checksum from 3.0 */
279 checksum
= (checksum
& 0x8000) ?
280 ((checksum
<< 1) | 1) : (checksum
<< 1);
283 } while ( (mp
= gbuf_cont(mp
)) );
295 * This procedure is called by each LAP interface when it wants to place
296 * itself online. The LAP interfaces passes in a pointer to its at_if
297 * struct, which is added to DDP's list of active interfaces (at_ifQueueHd).
298 * When DDP wants to transmit a packet, it searches this list for the
301 * If AT_IFF_DEFAULT is set, then this interface is to be brought online
302 * as the interface DDP socket addresses are tied to. Of course there can
303 * be only one default interface; we return an error if it's already set.
306 * ret_status = ddp_add_if(ifID)
309 * ifID pointer to LAP interface's at_if struct.
312 * 0 Procedure successfully completed.
313 * EALREADY This interface is already online, or there is
314 * already a default interface.
315 * ENOBUFS Cannot allocate input queue
319 register at_ifaddr_t
*ifID
;
323 dPrintf(D_M_DDP
, D_L_STARTUP
,
324 ("ddp_add_if: called, ifID:0x%x\n", (u_int
) ifID
));
326 if (ifID
->ifFlags
& AT_IFF_DEFAULT
) {
328 return(EEXIST
); /* home port already set */
334 for (port
=IFID_HOME
+1; port
<IF_TOTAL_MAX
; port
++)
335 if (!ifID_table
[port
]) {
338 if (port
== IF_TOTAL_MAX
) /* no space left */
342 /* allocate an et_aarp_amt structure */
343 if ((aarp_table
[port
] =
344 (aarp_amt_array
*)_MALLOC(sizeof(aarp_amt_array
),
345 M_RTABLE
, M_WAITOK
)) == NULL
)
348 dPrintf(D_M_DDP
, D_L_STARTUP
, ("ddp:adding ifID_table[%d]\n", port
));
350 /* add i/f to port list */
351 ifID_table
[port
] = ifID
;
352 ifID
->ifPort
= port
; /* set ddp port # in ifID */
354 /* Add this interface to the list of online interfaces */
355 TAILQ_INSERT_TAIL(&at_ifQueueHd
, ifID
, aa_link
);
364 * This procedure is called by each LAP interface when it wants to take
365 * itself offline. The LAP interfaces passes in a pointer to its at_if
366 * struct; DDP's list of active interfaces (at_ifQueueHd) is searched and
367 * this interface is removed from the list. DDP can still transmit
368 * packets as long as this interface is not the default interface; the
369 * sender will just get ENETUNREACH errors when it tries to send to an
370 * interface that went offline. However, if the default interface is
371 * taken offline, we no longer have a node ID to use as a source address
372 * and DDP must return ENETDOWN when a caller tries to send a packet.
375 * ifID pointer to LAP interface's at_if struct.
378 void ddp_rem_if(ifID
)
379 register at_ifaddr_t
*ifID
;
381 struct ifaddr
*ifa
= &ifID
->aa_ifa
;
383 /* un-do processing done in SIOCSIFADDR */
385 ifnet_lock_exclusive(ifID
->aa_ifp
);
386 if_detach_ifa(ifID
->aa_ifp
, ifa
);
387 ifa
->ifa_addr
= NULL
;
388 ifnet_lock_done(ifID
->aa_ifp
);
390 if (ifID
->at_dl_tag
) {
391 /* dlil_detach_protocol(ifID->at_dl_tag); */
392 ether_detach_at(ifID
->aa_ifp
);
396 /* un-do processing done in ddp_add_if() */
398 if (aarp_table
[ifID
->ifPort
]) {
399 FREE(aarp_table
[ifID
->ifPort
], M_RTABLE
);
400 aarp_table
[ifID
->ifPort
] = NULL
;
403 at_state
.flags
|= AT_ST_IF_CHANGED
;
406 trackrouter_rem_if(ifID
);
407 TAILQ_REMOVE(&at_ifQueueHd
, ifID
, aa_link
);
408 ifID_table
[ifID
->ifPort
] = NULL
;
409 ifID
->ifName
[0] = '\0';
413 /* *** deallocate ifID, eventually *** */
417 * The user may have registered an NVE with the NBP on a socket. When the
418 * socket is closed, the NVE should be deleted from NBP's name table. The
419 * user should delete the NVE before the socket is shut down, but there
420 * may be circumstances when he can't. So, whenever a DDP socket is closed,
421 * this routine is used to notify NBP of the socket closure. This would
422 * help NBP get rid of all NVE's registered on the socket.
425 /* *** Do we still need to do this? *** */
426 int ot_ddp_check_socket(socket
, pid
)
427 unsigned char socket
;
433 dPrintf(D_M_DDP
, D_L_INFO
, ("ot_ddp_check_socket: %d\n", socket
));
434 for (gref
= ddp_head
.atpcb_next
; gref
!= &ddp_head
; gref
= gref
->atpcb_next
)
435 if (gref
->lport
== socket
&& gref
->pid
== pid
)
437 if ((atp_inputQ
[socket
] != NULL
) && (atp_inputQ
[socket
] != (gref_t
*)1)
438 && (atp_pidM
[socket
] == pid
))
440 if ((adsp_inputQ
[socket
] != NULL
) && (adsp_pidM
[socket
] == pid
))
446 void ddp_notify_nbp(socket
, pid
, ddptype
)
447 unsigned char socket
;
449 unsigned char ddptype
; /* not used */
452 nve_entry_t
*nve_entry
, *nve_next
;
454 if (at_state
.flags
& AT_ST_STARTED
) {
455 /* *** NBP_CLOSE_NOTE processing (from ddp_nbp.c) *** */
456 ATDISABLE(nve_lock
, NVE_LOCK
);
457 for ((nve_entry
= TAILQ_FIRST(&name_registry
)); nve_entry
; nve_entry
= nve_next
) {
458 nve_next
= TAILQ_NEXT(nve_entry
, nve_link
);
459 if ((at_socket
)socket
== nve_entry
->address
.socket
&&
460 /* *** check complete address and ddptype here *** */
461 pid
== nve_entry
->pid
&&
462 ot_ddp_check_socket(nve_entry
->address
.socket
,
463 nve_entry
->pid
) < 2) {
464 /* NB: nbp_delete_entry calls TAILQ_REMOVE */
465 nbp_delete_entry(nve_entry
);
468 ATENABLE(nve_lock
, NVE_LOCK
);
470 } /* ddp_notify_nbp */
472 static void fillin_pkt_chain(m
)
477 *ddp
= (at_ddp_t
*)gbuf_rptr(m
),
481 if (UAS_VALUE(ddp
->checksum
)) {
482 tmp
= ddp_checksum(m
, 4);
483 UAS_ASSIGN(ddp
->checksum
, tmp
);
486 for (tmp_m
=gbuf_next(tmp_m
); tmp_m
; tmp_m
=gbuf_next(tmp_m
)) {
487 tmp_ddp
= (at_ddp_t
*)gbuf_rptr(tmp_m
);
488 tmp_ddp
->length
= gbuf_msgsize(tmp_m
);
491 NET_NET(tmp_ddp
->src_net
, ddp
->src_net
);
492 tmp_ddp
->src_node
= ddp
->src_node
;
493 tmp_ddp
->src_socket
= ddp
->src_socket
;
494 if (UAS_VALUE(tmp_ddp
->checksum
)) {
495 tmp
= ddp_checksum(tmp_m
, 4);
496 UAS_ASSIGN(tmp_ddp
->checksum
, tmp
);
501 /* There are various ways a packet may go out.... it may be sent out
502 * directly to destination node, or sent to a random router or sent
503 * to a router whose entry exists in Best Router Cache. Following are
504 * constants used WITHIN this routine to keep track of choice of destination
506 #define DIRECT_ADDR 1
508 #define BRIDGE_ADDR 3
514 * Called to queue a atp/ddp data packet on the network interface.
515 * It returns 0 normally, and an errno in case of error.
516 * The mbuf chain pointed to by *mp is consumed on success, and
517 * freed in case of error.
520 int ddp_output(mp
, src_socket
, src_addr_included
)
521 register gbuf_t
**mp
;
522 at_socket src_socket
;
523 int src_addr_included
;
525 register at_ifaddr_t
*ifID
= ifID_home
, *ifIDTmp
= NULL
;
526 register at_ddp_t
*ddp
;
527 register ddp_brt_t
*brt
;
528 register at_net_al dst_net
;
530 struct atalk_addr at_dest
;
531 at_ifaddr_t
*ARouterIf
= NULL
;
539 KERNEL_DEBUG(DBG_AT_DDP_OUTPUT
| DBG_FUNC_START
, 0,
542 snmpStats
.dd_outReq
++;
545 ddp
= (at_ddp_t
*)gbuf_rptr(m
);
548 /* Device/Interface not configured */
549 dPrintf(D_M_DDP
, D_L_ERROR
, ("Device/Interface not configured"));
552 goto exit_ddp_output
;
555 if ((ddp
->dst_socket
> (unsigned) (DDP_SOCKET_LAST
+ 1)) ||
556 (ddp
->dst_socket
< DDP_SOCKET_1st_RESERVED
)) {
557 dPrintf(D_M_DDP
, D_L_ERROR
,
558 ("Illegal destination socket on outgoing packet (0x%x)",
560 at_ddp_stats
.xmit_bad_addr
++;
563 goto exit_ddp_output
;
565 if ((len
= gbuf_msgsize(*mp
)) > DDP_DATAGRAM_SIZE
) {
566 /* the packet is too large */
567 dPrintf(D_M_DDP
, D_L_ERROR
,
568 ("Outgoing packet too long (len=%d bytes)", len
));
569 at_ddp_stats
.xmit_bad_length
++;
572 goto exit_ddp_output
;
574 at_ddp_stats
.xmit_bytes
+= len
;
575 at_ddp_stats
.xmit_packets
++;
581 /* If this packet is for the same node, loop it back
582 * up... Note that for LocalTalk, dst_net zero means "THIS_NET", so
583 * address 0.nn is eligible for loopback. For Extended EtherTalk,
584 * dst_net 0 can be used only for cable-wide or zone-wide
585 * broadcasts (0.ff) and as such, address of the form 0.nn is NOT
586 * eligible for loopback.
588 dst_net
= NET_VALUE(ddp
->dst_net
);
590 /* If our packet is destined for the 'virtual' bridge
591 * address of NODE==0xFE, replace that address with a
592 * real bridge address.
594 if ((ddp
->dst_node
== 0xfe) &&
595 ((dst_net
== ATADDR_ANYNET
) ||
596 (dst_net
>= ifID_home
->ifThisCableStart
&&
597 dst_net
<= ifID_home
->ifThisCableEnd
))) {
598 /* if there's a router that's not us, it's in ifID_home */
599 NET_ASSIGN(ddp
->dst_net
, ifID_home
->ifARouter
.s_net
);
600 dst_net
= ifID_home
->ifARouter
.s_net
;
601 ddp
->dst_node
= ifID_home
->ifARouter
.s_node
;
604 if (MULTIHOME_MODE
&& (ifIDTmp
= forUs(ddp
))) {
607 dPrintf(D_M_DDP_LOW
, D_L_USR1
,
608 ("ddp_out: for us if:%s\n", ifIDTmp
->ifName
));
612 loop
= ((ddp
->dst_node
== ifID
->ifThisNode
.s_node
) &&
613 (dst_net
== ifID
->ifThisNode
.s_net
)
616 gbuf_t
*mdata
, *mdata_next
;
618 if (!MULTIHOME_MODE
|| !src_addr_included
) {
619 NET_ASSIGN(ddp
->src_net
, ifID
->ifThisNode
.s_net
);
620 ddp
->src_node
= ifID
->ifThisNode
.s_node
;
622 ddp
->src_socket
= src_socket
;
624 dPrintf(D_M_DDP_LOW
, D_L_OUTPUT
,
625 ("ddp_output: loop to %d:%d port=%d\n",
626 NET_VALUE(ddp
->dst_net
),
630 fillin_pkt_chain(*mp
);
632 dPrintf(D_M_DDP
, D_L_VERBOSE
,
633 ("Looping back packet from skt 0x%x to skt 0x%x\n",
634 ddp
->src_socket
, ddp
->dst_socket
));
636 for (mdata
= *mp
; mdata
; mdata
= mdata_next
) {
637 mdata_next
= gbuf_next(mdata
);
638 gbuf_next(mdata
) = 0;
639 ddp_input(mdata
, ifID
);
641 goto exit_ddp_output
;
643 if ((ddp
->dst_socket
== ZIP_SOCKET
) &&
644 (zip_type_packet(*mp
) == ZIP_GETMYZONE
)) {
645 ddp
->src_socket
= src_socket
;
646 error
= zip_handle_getmyzone(ifID
, *mp
);
648 goto exit_ddp_output
;
651 * find out the interface on which the packet should go out
653 TAILQ_FOREACH(ifID
, &at_ifQueueHd
, aa_link
) {
654 if ((ifID
->ifThisNode
.s_net
== dst_net
) || (dst_net
== 0))
655 /* the message is either going out (i) on the same
656 * NETWORK in case of LocalTalk, or (ii) on the same
657 * CABLE in case of Extended AppleTalk (EtherTalk).
661 if ((ifID
->ifThisCableStart
<= dst_net
) &&
662 (ifID
->ifThisCableEnd
>= dst_net
)
664 /* We're on EtherTalk and the message is going out to
665 * some other network on the same cable.
669 if (ARouterIf
== NULL
&& ATALK_VALUE(ifID
->ifARouter
))
672 dPrintf(D_M_DDP_LOW
, D_L_USR1
,
673 ("ddp_output: after search ifid:0x%x %s ifID_home:0x%x\n",
674 (u_int
)ifID
, ifID
? ifID
->ifName
: "",
678 /* located the interface where the packet should
679 * go.... the "first-hop" destination address
680 * must be the same as real destination address.
682 addr_type
= DIRECT_ADDR
;
684 /* no, the destination network number does
685 * not match known network numbers. If we have
686 * heard from this network recently, BRT table
687 * may have address of a router we could use!
689 if (!MULTIPORT_MODE
) {
690 BRT_LOOK (brt
, dst_net
);
692 /* Bingo... BRT has an entry for this network.
693 * Use the link address as is.
695 dPrintf(D_M_DDP
, D_L_VERBOSE
,
696 ("Found BRT entry to send to net 0x%x", dst_net
));
697 at_ddp_stats
.xmit_BRT_used
++;
698 addr_type
= BRT_ENTRY
;
701 /* No BRT entry available for dest network... do we
702 * know of any router at all??
704 if ((ifID
= ARouterIf
) != NULL
)
705 addr_type
= BRIDGE_ADDR
;
707 dPrintf(D_M_DDP
, D_L_WARNING
,
708 ("Found no interface to send pkt"));
709 at_ddp_stats
.xmit_bad_addr
++;
712 goto exit_ddp_output
;
716 else { /* We are in multiport mode, so we can bypass all the rest
717 * and directly ask for the routing of the packet
719 at_ddp_stats
.xmit_BRT_used
++;
722 if (!src_addr_included
) {
723 ddp
->src_node
= ifID
->ifThisNode
.s_node
;
724 NET_ASSIGN(ddp
->src_net
, ifID
->ifThisNode
.s_net
);
726 ddp
->src_socket
= src_socket
;
727 routing_needed(*mp
, ifID
, TRUE
);
729 goto exit_ddp_output
;
732 /* by the time we land here, we know the interface on
733 * which this packet is going out.... ifID.
735 if (ifID
->ifState
== LAP_OFFLINE
) {
737 goto exit_ddp_output
;
743 at_dest.atalk_unused = 0;
745 NET_ASSIGN(at_dest
.atalk_net
, dst_net
);
746 at_dest
.atalk_node
= ddp
->dst_node
;
748 addr
= (char *)&at_dest
;
752 addr
= (char *)&brt
->et_addr
;
755 NET_ASSIGN(at_dest
.atalk_net
, ifID
->ifARouter
.s_net
);
756 at_dest
.atalk_node
= ifID
->ifARouter
.s_node
;
758 addr
= (char *)&at_dest
;
762 /* Irrespective of the interface on which
763 * the packet is going out, we always put the
764 * same source address on the packet (unless multihoming mode).
766 if (MULTIHOME_MODE
) {
767 if (!src_addr_included
) {
768 ddp
->src_node
= ifID
->ifThisNode
.s_node
;
769 NET_ASSIGN(ddp
->src_net
, ifID
->ifThisNode
.s_net
);
773 ddp
->src_node
= ifID_home
->ifThisNode
.s_node
;
774 NET_ASSIGN(ddp
->src_net
, ifID_home
->ifThisNode
.s_net
);
776 ddp
->src_socket
= src_socket
;
778 dPrintf(D_M_DDP_LOW
, D_L_OUTPUT
,
779 ("ddp_output: going out to %d:%d skt%d on %s\n",
780 dst_net
, ddp
->dst_node
, ddp
->dst_socket
, ifID
->ifName
));
782 fillin_pkt_chain(*mp
);
785 struct etalk_addr dest_addr
;
786 struct atalk_addr dest_at_addr
;
787 int loop
= TRUE
; /* flag to aarp to loopback (default) */
791 /* the incoming frame is of the form {flag, address, ddp...}
792 * where "flag" indicates whether the address is an 802.3
793 * (link) address, or an appletalk address. If it's an
794 * 802.3 address, the packet can just go out to the network
795 * through PAT, if it's an appletalk address, AT->802.3 address
796 * resolution needs to be done.
797 * If 802.3 address is known, strip off the flag and 802.3
798 * address, and prepend 802.2 and 802.3 headers.
802 addr_flag
= *(u_char
*)gbuf_rptr(m
);
807 case AT_ADDR_NO_LOOP
:
812 dest_at_addr
= *(struct atalk_addr
*)gbuf_rptr(m
);
813 gbuf_rinc(m
,sizeof(struct atalk_addr
));
815 dest_at_addr
= *(struct atalk_addr
*)addr
;
819 dest_addr
= *(struct etalk_addr
*)gbuf_rptr(m
);
820 gbuf_rinc(m
,sizeof(struct etalk_addr
));
822 dest_addr
= *(struct etalk_addr
*)addr
;
825 dPrintf(D_M_DDP_LOW
,D_L_ERROR
,
826 ("ddp_output: Unknown addr_flag = 0x%x\n", addr_flag
));
827 gbuf_freel(m
); /* unknown address type, chuck it */
828 goto exit_ddp_output
;
833 /* At this point, rptr points to ddp header for sure */
834 if (ifID
->ifState
== LAP_ONLINE_FOR_ZIP
) {
835 /* see if this is a ZIP packet that we need
836 * to let through even though network is
839 if (zip_type_packet(m
) == 0) {
841 goto exit_ddp_output
;
845 ifID
->stats
.xmit_packets
++;
846 ifID
->stats
.xmit_bytes
+= gbuf_msgsize(m
);
847 snmpStats
.dd_outLong
++;
850 case AT_ADDR_NO_LOOP
:
853 * we don't want elap to be looking into ddp header, so
854 * it doesn't know net#, consequently can't do
855 * AMT_LOOKUP. That task left to aarp now.
857 aarp_send_data(m
,ifID
,&dest_at_addr
, loop
);
860 pat_output(ifID
, m
, &dest_addr
, 0);
865 KERNEL_DEBUG(DBG_AT_DDP_OUTPUT
| DBG_FUNC_END
, 0,
870 void ddp_input(mp
, ifID
)
872 register at_ifaddr_t
*ifID
;
874 register at_ddp_t
*ddp
; /* DDP header */
875 register int msgsize
;
876 register at_socket socket
;
878 register at_net_al dst_net
;
880 KERNEL_DEBUG(DBG_AT_DDP_INPUT
| DBG_FUNC_START
, 0,
881 ifID
, mp
, gbuf_len(mp
),0);
883 /* Makes sure we know the default interface before starting to
884 * accept incomming packets. If we don't we may end up with a
885 * null ifID_table[0] and have impredicable results (specially
886 * in router mode. This is a transitory state (because we can
887 * begin to receive packet while we're not completly set up yet.
890 if (ifID_home
== (at_ifaddr_t
*)NULL
) {
891 dPrintf(D_M_DDP
, D_L_ERROR
,
892 ("dropped incoming packet ifID_home not set yet\n"));
894 goto out
; /* return */
898 * if a DDP packet has been broadcast, we're going to get a copy of
899 * it here; if it originated at user level via a write on a DDP
900 * socket; when it gets here, the first block in the chain will be
901 * empty since it only contained the lap level header which will be
902 * stripped in the lap level immediately below ddp
905 if ((mp
= (gbuf_t
*)ddp_compress_msg(mp
)) == NULL
) {
906 dPrintf(D_M_DDP
, D_L_ERROR
,
907 ("dropped short incoming ET packet (len %d)", 0));
908 snmpStats
.dd_inTotal
++;
909 at_ddp_stats
.rcv_bad_length
++;
910 goto out
; /* return; */
912 msgsize
= gbuf_msgsize(mp
);
914 at_ddp_stats
.rcv_bytes
+= msgsize
;
915 at_ddp_stats
.rcv_packets
++;
917 /* if the interface pointer is 0, the packet has been
918 * looped back by 'write' half of DDP. It is of the
919 * form {extended ddp,...}. The packet is meant to go
920 * up to some socket on the same node.
922 if (!ifID
) /* if loop back is specified */
923 ifID
= ifID_home
; /* that means the home port */
925 /* the incoming datagram has extended DDP header and is of
926 * the form {ddp,...}.
928 if (msgsize
< DDP_X_HDR_SIZE
) {
929 dPrintf(D_M_DDP
, D_L_ERROR
,
930 ("dropped short incoming ET packet (len %d)", msgsize
));
931 at_ddp_stats
.rcv_bad_length
++;
933 goto out
; /* return; */
936 * At this point, the message is always of the form
937 * {extended ddp, ... }.
939 ddp
= (at_ddp_t
*)gbuf_rptr(mp
);
942 if (msgsize
!= len
) {
943 if ((unsigned) msgsize
> len
) {
944 if (len
< DDP_X_HDR_SIZE
) {
945 dPrintf(D_M_DDP
, D_L_ERROR
,
946 ("Length problems, ddp length %d, buffer length %d",
948 snmpStats
.dd_tooLong
++;
949 at_ddp_stats
.rcv_bad_length
++;
951 goto out
; /* return; */
954 * shave off the extra bytes from the end of message
956 mp
= ddp_adjmsg(mp
, -(msgsize
- len
)) ? mp
: 0;
958 goto out
; /* return; */
960 dPrintf(D_M_DDP
, D_L_ERROR
,
961 ("Length problems, ddp length %d, buffer length %d",
963 snmpStats
.dd_tooShort
++;
964 at_ddp_stats
.rcv_bad_length
++;
966 goto out
; /* return; */
969 socket
= ddp
->dst_socket
;
972 * We want everything in router mode, specially socket 254 for nbp so we need
973 * to bypass this test when we are a router.
976 if (!MULTIPORT_MODE
&& (socket
> DDP_SOCKET_LAST
||
977 socket
< DDP_SOCKET_1st_RESERVED
)) {
978 dPrintf(D_M_DDP
, D_L_WARNING
,
979 ("Bad dst socket on incoming packet (0x%x)",
981 at_ddp_stats
.rcv_bad_socket
++;
983 goto out
; /* return; */
986 * if the checksum is true, then upstream wants us to calc
988 if (UAS_VALUE(ddp
->checksum
) &&
989 (UAS_VALUE(ddp
->checksum
) != ddp_checksum(mp
, 4))) {
990 dPrintf(D_M_DDP
, D_L_WARNING
,
991 ("Checksum error on incoming pkt, calc 0x%x, exp 0x%x",
992 ddp_checksum(mp
, 4), UAS_VALUE(ddp
->checksum
)));
993 snmpStats
.dd_checkSum
++;
994 at_ddp_stats
.rcv_bad_checksum
++;
996 goto out
; /* return; */
999 /*############### routing input checking */
1001 /* Router mode special: we send "up-stack" packets for this node or coming from any
1002 * other ports, but for the reserved atalk sockets (RTMP, ZIP, NBP [and EP])
1003 * BTW, the way we know it's for the router and not the home port is that the
1004 * MAC (ethernet) address is always the one of the interface we're on, but
1005 * the AppleTalk address must be the one of the home port. If it's a multicast
1006 * or another AppleTalk address, this is the router job's to figure out where it's
1009 /* *** a duplicate should be sent to any other client that is listening
1010 for packets of this type on a raw DDP socket *** */
1011 if (ddp_handler
[socket
].func
) {
1012 dPrintf(D_M_DDP
,D_L_INPUT
,
1013 ("ddp_input: skt %d hdnlr:0x%x\n",
1014 (u_int
) socket
, ddp_handler
[socket
].func
));
1016 snmpStats
.dd_inLocal
++;
1018 (*ddp_handler
[socket
].func
)(mp
, ifID
);
1019 goto out
; /* return; */
1021 dst_net
= NET_VALUE(ddp
->dst_net
);
1025 /* any node, wildcard or matching net */
1026 ((ddp
->dst_node
== 255) &&
1027 (((dst_net
>= ifID_home
->ifThisCableStart
) &&
1028 (dst_net
<= ifID_home
->ifThisCableEnd
)) ||
1030 /* this node is not online yet(?) */
1031 (ifID
->ifRoutingState
< PORT_ONLINE
)
1035 snmpStats
.dd_inLocal
++;
1037 if (ddp
->type
== DDP_ATP
) {
1038 if (atp_inputQ
[socket
] && (atp_inputQ
[socket
] != (gref_t
*)1)) {
1039 /* if there's an ATP pcb */
1041 goto out
; /* return; */
1043 } else if (ddp
->type
== DDP_ADSP
) {
1044 if (adsp_inputQ
[socket
]) {
1045 /* if there's an ADSP pcb */
1047 goto out
; /* return; */
1051 /* otherwise look for a DDP pcb;
1052 ATP / raw-DDP and ADSP / raw-DDP are possible */
1053 for (gref
= ddp_head
.atpcb_next
; gref
!= &ddp_head
;
1054 gref
= gref
->atpcb_next
)
1055 if (gref
->lport
== socket
&&
1056 (gref
->ddptype
== 0 || gref
->ddptype
== ddp
->type
)) {
1057 dPrintf(D_M_DDP
, D_L_INPUT
,
1058 ("ddp_input: streamq, skt %d\n", socket
));
1059 if (gref
->atpcb_socket
) {
1060 struct sockaddr_at ddp_in
;
1061 ddp_in
.sat_len
= sizeof(ddp_in
);
1062 ddp_in
.sat_family
= AF_APPLETALK
;
1063 ddp_in
.sat_addr
.s_net
= NET_VALUE(ddp
->src_net
);
1064 ddp_in
.sat_addr
.s_node
= ddp
->src_node
;
1065 ddp_in
.sat_port
= ddp
->src_socket
;
1067 /* strip off DDP header if so indicated by
1069 if (gref
->ddp_flags
& DDPFLG_STRIPHDR
) {
1070 mp
= m_pullup((struct mbuf
*)mp
,
1073 gbuf_rinc(mp
, DDP_X_HDR_SIZE
);
1075 /* this should never happen because
1076 msgsize was checked earlier */
1077 at_ddp_stats
.rcv_bad_length
++;
1078 goto out
; /* return */
1082 if (sbappendaddr(&((gref
->atpcb_socket
)->so_rcv
),
1083 (struct sockaddr
*)&ddp_in
,
1084 mp
, 0, NULL
) != 0) {
1085 sorwakeup(gref
->atpcb_socket
);
1088 atalk_putnext(gref
, mp
);
1090 goto out
; /* return */
1093 at_ddp_stats
.rcv_bad_socket
++;
1095 snmpStats
.dd_noHandler
++;
1096 dPrintf(D_M_DDP
, D_L_WARNING
,
1097 ("ddp_input: dropped pkt for socket %d\n", socket
));
1099 dPrintf(D_M_DDP
, D_L_ROUTING
,
1100 ("ddp_input: routing_needed from port=%d sock=%d\n",
1101 ifID
->ifPort
, ddp
->dst_socket
));
1103 snmpStats
.dd_fwdReq
++;
1104 if (((pktsIn
-pktsHome
+200) >= RouterMix
) && ((++pktsDropped
% 5) == 0)) {
1105 at_ddp_stats
.rcv_dropped_nobuf
++;
1109 routing_needed(mp
, ifID
, FALSE
);
1113 KERNEL_DEBUG(DBG_AT_DDP_INPUT
| DBG_FUNC_END
, 0,0,0,0,0);
1118 * ddp_router_output()
1121 * This is a modified version of ddp_output for router use.
1122 * The main difference is that the interface on which the packet needs
1123 * to be sent is specified and a *destination* AppleTalk address is passed
1124 * as an argument, this address may or may not be the same as the destination
1125 * address found in the ddp packet... This is the trick about routing, the
1126 * AppleTalk destination of the packet may not be the same as the Enet address
1127 * we send the packet too (ie, we may pass the baby to another router).
1130 int ddp_router_output(mp
, ifID
, addr_type
, router_net
, router_node
, enet_addr
)
1134 at_net_al router_net
;
1135 at_node router_node
;
1136 etalk_addr_t
*enet_addr
;
1138 register at_ddp_t
*ddp
;
1139 struct atalk_addr at_dest
;
1145 dPrintf(D_M_DDP
, D_L_WARNING
, ("BAD BAD ifID\n"));
1149 ddp
= (at_ddp_t
*)gbuf_rptr(mp
);
1151 if (ifID
->ifFlags
& AT_IFF_AURP
) { /* AURP link? */
1152 if (ddp_AURPsendx
) {
1153 fillin_pkt_chain(mp
);
1154 if (router_node
== 255)
1156 ddp_AURPsendx(AURPCODE_DATAPKT
, mp
, router_node
);
1164 /* keep some of the tests for now ####### */
1166 if (gbuf_msgsize(mp
) > DDP_DATAGRAM_SIZE
) {
1167 /* the packet is too large */
1168 dPrintf(D_M_DDP
, D_L_WARNING
,
1169 ("ddp_router_output: Packet too large size=%d\n",
1175 switch (addr_type
) {
1180 * Check for packet destined to the home stack
1183 if ((ddp
->dst_node
== ifID
->ifThisNode
.s_node
) &&
1184 (NET_VALUE(ddp
->dst_net
) == ifID
->ifThisNode
.s_net
)) {
1185 dPrintf(D_M_DDP_LOW
, D_L_ROUTING
,
1186 ("ddp_r_output: sending back home from port=%d socket=%d\n",
1187 ifID
->ifPort
, ddp
->dst_socket
));
1189 UAS_ASSIGN(ddp
->checksum
, 0);
1190 ddp_input(mp
, ifID
);
1194 NET_ASSIGN(at_dest
.atalk_net
, router_net
);
1195 at_dest
.atalk_node
= router_node
;
1197 addr_flag
= AT_ADDR_NO_LOOP
;
1198 addr
= (char *)&at_dest
;
1199 dPrintf(D_M_DDP_LOW
, D_L_ROUTING_AT
,
1200 ("ddp_r_output: AT_ADDR out port=%d net %d:%d via rte %d:%d",
1201 ifID
->ifPort
, NET_VALUE(ddp
->dst_net
), ddp
->dst_node
, router_net
,
1206 addr_flag
= ET_ADDR
;
1207 addr
= (char *)enet_addr
;
1208 dPrintf(D_M_DDP_LOW
, D_L_ROUTING
,
1209 ("ddp_r_output: ET_ADDR out port=%d net %d:%d\n",
1210 ifID
->ifPort
, NET_VALUE(ddp
->dst_net
), ddp
->dst_node
));
1214 if (ifID
->ifState
== LAP_OFFLINE
) {
1219 fillin_pkt_chain(mp
);
1222 struct etalk_addr dest_addr
;
1223 struct atalk_addr dest_at_addr
;
1224 int loop
= TRUE
; /* flag to aarp to loopback (default) */
1228 /* the incoming frame is of the form {flag, address, ddp...}
1229 * where "flag" indicates whether the address is an 802.3
1230 * (link) address, or an appletalk address. If it's an
1231 * 802.3 address, the packet can just go out to the network
1232 * through PAT, if it's an appletalk address, AT->802.3 address
1233 * resolution needs to be done.
1234 * If 802.3 address is known, strip off the flag and 802.3
1235 * address, and prepend 802.2 and 802.3 headers.
1239 addr_flag
= *(u_char
*)gbuf_rptr(m
);
1243 switch (addr_flag
) {
1244 case AT_ADDR_NO_LOOP
:
1249 dest_at_addr
= *(struct atalk_addr
*)gbuf_rptr(m
);
1250 gbuf_rinc(m
,sizeof(struct atalk_addr
));
1252 dest_at_addr
= *(struct atalk_addr
*)addr
;
1256 dest_addr
= *(struct etalk_addr
*)gbuf_rptr(m
);
1257 gbuf_rinc(m
,sizeof(struct etalk_addr
));
1259 dest_addr
= *(struct etalk_addr
*)addr
;
1262 dPrintf(D_M_DDP_LOW
,D_L_ERROR
,
1263 ("ddp_router_output: Unknown addr_flag = 0x%x\n", addr_flag
));
1265 gbuf_freel(m
); /* unknown address type, chuck it */
1271 /* At this point, rptr points to ddp header for sure */
1272 if (ifID
->ifState
== LAP_ONLINE_FOR_ZIP
) {
1273 /* see if this is a ZIP packet that we need
1274 * to let through even though network is
1277 if (zip_type_packet(m
) == 0) {
1283 ifID
->stats
.xmit_packets
++;
1284 ifID
->stats
.xmit_bytes
+= gbuf_msgsize(m
);
1285 snmpStats
.dd_outLong
++;
1287 switch (addr_flag
) {
1288 case AT_ADDR_NO_LOOP
:
1291 * we don't want elap to be looking into ddp header, so
1292 * it doesn't know net#, consequently can't do
1293 * AMT_LOOKUP. That task left to aarp now.
1295 aarp_send_data(m
,ifID
,&dest_at_addr
, loop
);
1298 pat_output(ifID
, m
, &dest_addr
, 0);
1304 } /* ddp_router_output */
1306 /*****************************************/
1308 void rt_delete(NetStop
, NetStart
)
1309 unsigned short NetStop
;
1310 unsigned short NetStart
;
1315 ATDISABLE(s
, ddpinp_lock
);
1316 if ((found
= rt_bdelete(NetStop
, NetStart
)) != 0) {
1317 bzero(found
, sizeof(RT_entry
));
1318 found
->right
= RT_table_freelist
;
1319 RT_table_freelist
= found
;
1321 ATENABLE(s
, ddpinp_lock
);
1324 int ddp_AURPfuncx(code
, param
, node
)
1329 extern void rtmp_timeout();
1330 extern void rtmp_send_port();
1335 case AURPCODE_DATAPKT
: /* data packet */
1337 dPrintf(D_M_DDP
, D_L_TRACE
, ("ddp_AURPfuncx: data, 0x%x, %d\n",
1338 (u_int
) aurp_ifID
, node
));
1340 ddp_input((gbuf_t
*)param
, aurp_ifID
);
1342 gbuf_freem((gbuf_t
*)param
);
1345 case AURPCODE_REG
: /* register/deregister */
1348 ddp_AURPsendx
= (void(*)())param
;
1351 /* register AURP callback function */
1354 for (k
=(IFID_HOME
+1); k
< IF_TOTAL_MAX
; k
++) {
1355 if (ifID_table
[k
] == 0) {
1356 aurp_ifID
= &at_interfaces
[k
];
1357 aurp_ifID
->ifFlags
= RTR_XNET_PORT
;
1358 ddp_add_if(aurp_ifID
);
1359 aurp_ifID
->ifState
= LAP_ONLINE
;
1360 aurp_ifID
->ifRoutingState
= PORT_ONLINE
;
1361 dPrintf(D_M_DDP
, D_L_TRACE
,
1362 ("ddp_AURPfuncx: on, 0x%x\n",
1363 (u_int
) aurp_ifID
));
1365 ddp_AURPsendx(AURPCODE_DEBUGINFO
,
1366 &dbgBits
, aurp_ifID
->ifPort
);
1373 /* deregister AURP callback function */
1375 rtmp_purge(aurp_ifID
);
1376 ddp_rem_if(aurp_ifID
);
1377 aurp_ifID
->ifState
= LAP_OFFLINE
;
1378 aurp_ifID
->ifRoutingState
= PORT_OFFLINE
;
1379 dPrintf(D_M_DDP
, D_L_TRACE
,
1380 ("ddp_AURPfuncx: off, 0x%x\n", (u_int
) aurp_ifID
));
1386 case AURPCODE_AURPPROTO
: /* proto type - AURP */
1388 aurp_ifID
->ifFlags
|= AT_IFF_AURP
;
1397 /* checks to see if address of packet is for one of our interfaces
1398 returns *ifID if it's for us, NULL if not
1400 at_ifaddr_t
*forUs(ddp
)
1401 register at_ddp_t
*ddp
;
1405 TAILQ_FOREACH(ifID
, &at_ifQueueHd
, aa_link
) {
1406 if ((ddp
->dst_node
== ifID
->ifThisNode
.s_node
) &&
1407 (NET_VALUE(ddp
->dst_net
) == ifID
->ifThisNode
.s_net
)
1409 dPrintf(D_M_DDP_LOW
, D_L_ROUTING
,
1410 ("pkt was for port %d\n", ifID
->ifPort
));
1416 return((at_ifaddr_t
*)NULL
);