2 * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
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_pcb.h>
58 #include <netat/at_var.h>
59 #include <netat/ddp.h>
61 #include <netat/nbp.h>
62 #include <netat/rtmp.h>
63 #include <netat/zip.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>
72 #include <netat/at_pat.h>
73 #include <netat/atp.h>
75 #include <net/kpi_protocol.h>
79 /* Queue of LAP interfaces which have registered themselves with DDP */
80 struct at_ifQueueHd at_ifQueueHd
;
82 extern TAILQ_HEAD(name_registry
, _nve_
) name_registry
;
84 snmpStats_t snmpStats
; /* snmp ddp & echo stats */
86 extern at_ddp_stats_t at_ddp_stats
; /* DDP statistics */
87 extern struct atpcb ddp_head
;
88 extern at_ifaddr_t
*ifID_home
, *ifID_table
[];
89 extern aarp_amt_array
*aarp_table
[];
90 extern at_ifaddr_t at_interfaces
[];
92 /* routing mode special */
93 void (*ddp_AURPsendx
)(void) = NULL
;
94 at_ifaddr_t
*aurp_ifID
= 0;
101 extern int *atp_pidM
;
102 extern int *adsp_pidM
;
103 extern struct atpcb
*atp_inputQ
[];
104 extern CCB
*adsp_inputQ
[];
106 static void fillin_pkt_chain(gbuf_t
*);
107 static int ot_ddp_check_socket(unsigned char ,int pid
);
111 ddp_handler_func func
;
114 void init_ddp_handler(void)
116 bzero(ddp_handler
, sizeof(ddp_handler
));
119 void add_ddp_handler(ddp_socket
, input_func
)
121 ddp_handler_func input_func
;
123 ddp_handler
[ddp_socket
].func
= input_func
;
133 * Raw DDP socket option processing.
135 int ddp_ctloutput(so
, sopt
)
137 struct sockopt
*sopt
;
139 struct atpcb
*at_pcb
= sotoatpcb(so
);
140 int optval
, error
= 0;
142 if (sopt
->sopt_level
!= ATPROTO_NONE
)
145 switch (sopt
->sopt_dir
) {
148 switch (sopt
->sopt_name
) {
150 optval
= at_pcb
->ddp_flags
& DDPFLG_HDRINCL
;
151 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
154 optval
= at_pcb
->ddp_flags
& DDPFLG_CHKSUM
;
155 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
158 optval
= at_pcb
->ddp_flags
& DDPFLG_STRIPHDR
;
159 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
162 optval
= at_pcb
->ddp_flags
& DDPFLG_SLFSND
;
163 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
165 case DDP_GETSOCKNAME
:
168 addr
.inet
.net
= at_pcb
->laddr
.s_net
;
169 addr
.inet
.node
= at_pcb
->laddr
.s_node
;
170 addr
.inet
.socket
= at_pcb
->lport
;
171 addr
.ddptype
= at_pcb
->ddptype
;
172 error
= sooptcopyout(sopt
, &addr
, sizeof addr
);
181 switch (sopt
->sopt_name
) {
183 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
188 at_pcb
->ddp_flags
|= DDPFLG_HDRINCL
;
190 at_pcb
->ddp_flags
&= ~DDPFLG_HDRINCL
;
193 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
198 at_pcb
->ddp_flags
|= DDPFLG_CHKSUM
;
200 at_pcb
->ddp_flags
&= ~DDPFLG_CHKSUM
;
203 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
208 at_pcb
->ddp_flags
|= DDPFLG_STRIPHDR
;
210 at_pcb
->ddp_flags
&= ~DDPFLG_STRIPHDR
;
213 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
218 at_pcb
->ddp_flags
|= DDPFLG_SLFSND
;
220 at_pcb
->ddp_flags
&= ~DDPFLG_SLFSND
;
232 /****************************************************************/
235 /* Support Routines */
238 /****************************************************************/
245 * This procedure determines the checksum of an extended DDP datagram.
246 * Add the unsigned bytes into an unsigned 16-bit accumulator.
247 * After each add, rotate the sign bit into the low order bit of
248 * the accumulator. When done, if the checksum is 0, changed into 0xFFFF.
251 * checksum = ddp_checksum(mp, offset)
254 * mp pointer to the datagram gbuf_t
255 * offset offset to start at in first gbuf_t block
262 u_short
ddp_checksum(mp
, offset
)
266 register u_char
*data
;
268 register u_short checksum
;
273 if (offset
>= gbuf_len(mp
))
274 offset
-= gbuf_len(mp
);
276 data
= ((unsigned char *) gbuf_rptr(mp
)) + offset
;
277 length
= gbuf_len(mp
) - offset
;
279 /* Portable checksum from 3.0 */
282 checksum
= (checksum
& 0x8000) ?
283 ((checksum
<< 1) | 1) : (checksum
<< 1);
286 } while ( (mp
= gbuf_cont(mp
)) );
298 * This procedure is called by each LAP interface when it wants to place
299 * itself online. The LAP interfaces passes in a pointer to its at_if
300 * struct, which is added to DDP's list of active interfaces (at_ifQueueHd).
301 * When DDP wants to transmit a packet, it searches this list for the
304 * If AT_IFF_DEFAULT is set, then this interface is to be brought online
305 * as the interface DDP socket addresses are tied to. Of course there can
306 * be only one default interface; we return an error if it's already set.
309 * ret_status = ddp_add_if(ifID)
312 * ifID pointer to LAP interface's at_if struct.
315 * 0 Procedure successfully completed.
316 * EALREADY This interface is already online, or there is
317 * already a default interface.
318 * ENOBUFS Cannot allocate input queue
322 register at_ifaddr_t
*ifID
;
326 dPrintf(D_M_DDP
, D_L_STARTUP
,
327 ("ddp_add_if: called, ifID:0x%x\n", (u_int
) ifID
));
329 if (ifID
->ifFlags
& AT_IFF_DEFAULT
) {
331 return(EEXIST
); /* home port already set */
337 for (port
=IFID_HOME
+1; port
<IF_TOTAL_MAX
; port
++)
338 if (!ifID_table
[port
]) {
341 if (port
== IF_TOTAL_MAX
) /* no space left */
345 /* allocate an et_aarp_amt structure */
346 if ((aarp_table
[port
] =
347 (aarp_amt_array
*)_MALLOC(sizeof(aarp_amt_array
),
348 M_RTABLE
, M_WAITOK
)) == NULL
)
351 dPrintf(D_M_DDP
, D_L_STARTUP
, ("ddp:adding ifID_table[%d]\n", port
));
353 /* add i/f to port list */
354 ifID_table
[port
] = ifID
;
355 ifID
->ifPort
= port
; /* set ddp port # in ifID */
357 /* Add this interface to the list of online interfaces */
358 TAILQ_INSERT_TAIL(&at_ifQueueHd
, ifID
, aa_link
);
367 * This procedure is called by each LAP interface when it wants to take
368 * itself offline. The LAP interfaces passes in a pointer to its at_if
369 * struct; DDP's list of active interfaces (at_ifQueueHd) is searched and
370 * this interface is removed from the list. DDP can still transmit
371 * packets as long as this interface is not the default interface; the
372 * sender will just get ENETUNREACH errors when it tries to send to an
373 * interface that went offline. However, if the default interface is
374 * taken offline, we no longer have a node ID to use as a source address
375 * and DDP must return ENETDOWN when a caller tries to send a packet.
378 * ifID pointer to LAP interface's at_if struct.
381 void ddp_rem_if(ifID
)
382 register at_ifaddr_t
*ifID
;
384 struct ifaddr
*ifa
= &ifID
->aa_ifa
;
386 /* un-do processing done in SIOCSIFADDR */
388 ifnet_lock_exclusive(ifID
->aa_ifp
);
389 if_detach_ifa(ifID
->aa_ifp
, ifa
);
390 ifa
->ifa_addr
= NULL
;
391 ifnet_lock_done(ifID
->aa_ifp
);
393 if (ifID
->at_was_attached
== 0 && ifID
->aa_ifp
!= NULL
) {
394 (void)proto_unplumb(PF_APPLETALK
, ifID
->aa_ifp
);
397 /* un-do processing done in ddp_add_if() */
399 if (aarp_table
[ifID
->ifPort
]) {
400 FREE(aarp_table
[ifID
->ifPort
], M_RTABLE
);
401 aarp_table
[ifID
->ifPort
] = NULL
;
404 at_state
.flags
|= AT_ST_IF_CHANGED
;
407 trackrouter_rem_if(ifID
);
408 TAILQ_REMOVE(&at_ifQueueHd
, ifID
, aa_link
);
409 ifID_table
[ifID
->ifPort
] = NULL
;
410 ifID
->ifName
[0] = '\0';
414 /* *** deallocate ifID, eventually *** */
418 * The user may have registered an NVE with the NBP on a socket. When the
419 * socket is closed, the NVE should be deleted from NBP's name table. The
420 * user should delete the NVE before the socket is shut down, but there
421 * may be circumstances when he can't. So, whenever a DDP socket is closed,
422 * this routine is used to notify NBP of the socket closure. This would
423 * help NBP get rid of all NVE's registered on the socket.
426 /* *** Do we still need to do this? *** */
427 static int ot_ddp_check_socket(socket
, pid
)
428 unsigned char socket
;
434 dPrintf(D_M_DDP
, D_L_INFO
, ("ot_ddp_check_socket: %d\n", socket
));
435 for (gref
= ddp_head
.atpcb_next
; gref
!= &ddp_head
; gref
= gref
->atpcb_next
)
436 if (gref
->lport
== socket
&& gref
->pid
== pid
)
438 if ((atp_inputQ
[socket
] != NULL
) && (atp_inputQ
[socket
] != (gref_t
*)1)
439 && (atp_pidM
[socket
] == pid
))
441 if ((adsp_inputQ
[socket
] != NULL
) && (adsp_pidM
[socket
] == pid
))
448 unsigned char socket
,
450 __unused
unsigned char ddptype
)
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 for ((nve_entry
= TAILQ_FIRST(&name_registry
)); nve_entry
; nve_entry
= nve_next
) {
457 nve_next
= TAILQ_NEXT(nve_entry
, nve_link
);
458 if ((at_socket
)socket
== nve_entry
->address
.socket
&&
459 /* *** check complete address and ddptype here *** */
460 pid
== nve_entry
->pid
&&
461 ot_ddp_check_socket(nve_entry
->address
.socket
,
462 nve_entry
->pid
) < 2) {
463 /* NB: nbp_delete_entry calls TAILQ_REMOVE */
464 nbp_delete_entry(nve_entry
);
468 } /* ddp_notify_nbp */
470 static void fillin_pkt_chain(m
)
475 *ddp
= (at_ddp_t
*)gbuf_rptr(m
),
479 if (UAS_VALUE(ddp
->checksum
)) {
480 tmp
= ddp_checksum(m
, 4);
481 UAS_ASSIGN_HTON(ddp
->checksum
, tmp
);
484 for (tmp_m
=gbuf_next(tmp_m
); tmp_m
; tmp_m
=gbuf_next(tmp_m
)) {
485 tmp_ddp
= (at_ddp_t
*)gbuf_rptr(tmp_m
);
486 DDPLEN_ASSIGN(tmp_ddp
, gbuf_msgsize(tmp_m
));
489 NET_NET(tmp_ddp
->src_net
, ddp
->src_net
);
490 tmp_ddp
->src_node
= ddp
->src_node
;
491 tmp_ddp
->src_socket
= ddp
->src_socket
;
492 if (UAS_VALUE(tmp_ddp
->checksum
)) {
493 tmp
= ddp_checksum(tmp_m
, 4);
494 UAS_ASSIGN_HTON(ddp
->checksum
, tmp
);
499 /* There are various ways a packet may go out.... it may be sent out
500 * directly to destination node, or sent to a random router or sent
501 * to a router whose entry exists in Best Router Cache. Following are
502 * constants used WITHIN this routine to keep track of choice of destination
504 #define DIRECT_ADDR 1
506 #define BRIDGE_ADDR 3
512 * Called to queue a atp/ddp data packet on the network interface.
513 * It returns 0 normally, and an errno in case of error.
514 * The mbuf chain pointed to by *mp is consumed on success, and
515 * freed in case of error.
518 int ddp_output(mp
, src_socket
, src_addr_included
)
519 register gbuf_t
**mp
;
520 at_socket src_socket
;
521 int src_addr_included
;
523 register at_ifaddr_t
*ifID
= ifID_home
, *ifIDTmp
= NULL
;
524 register at_ddp_t
*ddp
;
525 register ddp_brt_t
*brt
= NULL
;
526 register at_net_al dst_net
;
528 struct atalk_addr at_dest
;
529 at_ifaddr_t
*ARouterIf
= NULL
;
533 u_char addr_flag
= 0;
537 KERNEL_DEBUG(DBG_AT_DDP_OUTPUT
| DBG_FUNC_START
, 0,
540 snmpStats
.dd_outReq
++;
543 ddp
= (at_ddp_t
*)gbuf_rptr(m
);
546 /* Device/Interface not configured */
547 dPrintf(D_M_DDP
, D_L_ERROR
, ("Device/Interface not configured"));
550 goto exit_ddp_output
;
553 if ((ddp
->dst_socket
> (unsigned) (DDP_SOCKET_LAST
+ 1)) ||
554 (ddp
->dst_socket
< DDP_SOCKET_1st_RESERVED
)) {
555 dPrintf(D_M_DDP
, D_L_ERROR
,
556 ("Illegal destination socket on outgoing packet (0x%x)",
558 at_ddp_stats
.xmit_bad_addr
++;
561 goto exit_ddp_output
;
563 if ((len
= gbuf_msgsize(*mp
)) > DDP_DATAGRAM_SIZE
) {
564 /* the packet is too large */
565 dPrintf(D_M_DDP
, D_L_ERROR
,
566 ("Outgoing packet too long (len=%d bytes)", len
));
567 at_ddp_stats
.xmit_bad_length
++;
570 goto exit_ddp_output
;
572 at_ddp_stats
.xmit_bytes
+= len
;
573 at_ddp_stats
.xmit_packets
++;
575 DDPLEN_ASSIGN(ddp
, len
);
579 /* If this packet is for the same node, loop it back
580 * up... Note that for LocalTalk, dst_net zero means "THIS_NET", so
581 * address 0.nn is eligible for loopback. For Extended EtherTalk,
582 * dst_net 0 can be used only for cable-wide or zone-wide
583 * broadcasts (0.ff) and as such, address of the form 0.nn is NOT
584 * eligible for loopback.
586 dst_net
= NET_VALUE(ddp
->dst_net
);
588 /* If our packet is destined for the 'virtual' bridge
589 * address of NODE==0xFE, replace that address with a
590 * real bridge address.
592 if ((ddp
->dst_node
== 0xfe) &&
593 ((dst_net
== ATADDR_ANYNET
) ||
594 (dst_net
>= ifID_home
->ifThisCableStart
&&
595 dst_net
<= ifID_home
->ifThisCableEnd
))) {
596 /* if there's a router that's not us, it's in ifID_home */
597 NET_ASSIGN(ddp
->dst_net
, ifID_home
->ifARouter
.s_net
);
598 dst_net
= ifID_home
->ifARouter
.s_net
;
599 ddp
->dst_node
= ifID_home
->ifARouter
.s_node
;
602 if (MULTIHOME_MODE
&& (ifIDTmp
= forUs(ddp
))) {
605 dPrintf(D_M_DDP_LOW
, D_L_USR1
,
606 ("ddp_out: for us if:%s\n", ifIDTmp
->ifName
));
610 loop
= ((ddp
->dst_node
== ifID
->ifThisNode
.s_node
) &&
611 (dst_net
== ifID
->ifThisNode
.s_net
)
614 gbuf_t
*mdata
, *mdata_next
;
616 if (!MULTIHOME_MODE
|| !src_addr_included
) {
617 NET_ASSIGN(ddp
->src_net
, ifID
->ifThisNode
.s_net
);
618 ddp
->src_node
= ifID
->ifThisNode
.s_node
;
620 ddp
->src_socket
= src_socket
;
622 dPrintf(D_M_DDP_LOW
, D_L_OUTPUT
,
623 ("ddp_output: loop to %d:%d port=%d\n",
624 NET_VALUE(ddp
->dst_net
),
628 fillin_pkt_chain(*mp
);
630 dPrintf(D_M_DDP
, D_L_VERBOSE
,
631 ("Looping back packet from skt 0x%x to skt 0x%x\n",
632 ddp
->src_socket
, ddp
->dst_socket
));
634 for (mdata
= *mp
; mdata
; mdata
= mdata_next
) {
635 mdata_next
= gbuf_next(mdata
);
636 gbuf_next(mdata
) = 0;
637 ddp_input(mdata
, ifID
);
639 goto exit_ddp_output
;
641 if ((ddp
->dst_socket
== ZIP_SOCKET
) &&
642 (zip_type_packet(*mp
) == ZIP_GETMYZONE
)) {
643 ddp
->src_socket
= src_socket
;
644 error
= zip_handle_getmyzone(ifID
, *mp
);
646 goto exit_ddp_output
;
649 * find out the interface on which the packet should go out
651 TAILQ_FOREACH(ifID
, &at_ifQueueHd
, aa_link
) {
652 if ((ifID
->ifThisNode
.s_net
== dst_net
) || (dst_net
== 0))
653 /* the message is either going out (i) on the same
654 * NETWORK in case of LocalTalk, or (ii) on the same
655 * CABLE in case of Extended AppleTalk (EtherTalk).
659 if ((ifID
->ifThisCableStart
<= dst_net
) &&
660 (ifID
->ifThisCableEnd
>= dst_net
)
662 /* We're on EtherTalk and the message is going out to
663 * some other network on the same cable.
667 if (ARouterIf
== NULL
&& ATALK_VALUE(ifID
->ifARouter
))
670 dPrintf(D_M_DDP_LOW
, D_L_USR1
,
671 ("ddp_output: after search ifid:0x%x %s ifID_home:0x%x\n",
672 (u_int
)ifID
, ifID
? ifID
->ifName
: "",
676 /* located the interface where the packet should
677 * go.... the "first-hop" destination address
678 * must be the same as real destination address.
680 addr_type
= DIRECT_ADDR
;
682 /* no, the destination network number does
683 * not match known network numbers. If we have
684 * heard from this network recently, BRT table
685 * may have address of a router we could use!
687 if (!MULTIPORT_MODE
) {
688 BRT_LOOK (brt
, dst_net
);
690 /* Bingo... BRT has an entry for this network.
691 * Use the link address as is.
693 dPrintf(D_M_DDP
, D_L_VERBOSE
,
694 ("Found BRT entry to send to net 0x%x", dst_net
));
695 at_ddp_stats
.xmit_BRT_used
++;
696 addr_type
= BRT_ENTRY
;
699 /* No BRT entry available for dest network... do we
700 * know of any router at all??
702 if ((ifID
= ARouterIf
) != NULL
)
703 addr_type
= BRIDGE_ADDR
;
705 dPrintf(D_M_DDP
, D_L_WARNING
,
706 ("Found no interface to send pkt"));
707 at_ddp_stats
.xmit_bad_addr
++;
710 goto exit_ddp_output
;
714 else { /* We are in multiport mode, so we can bypass all the rest
715 * and directly ask for the routing of the packet
717 at_ddp_stats
.xmit_BRT_used
++;
720 if (!src_addr_included
) {
721 ddp
->src_node
= ifID
->ifThisNode
.s_node
;
722 NET_ASSIGN(ddp
->src_net
, ifID
->ifThisNode
.s_net
);
724 ddp
->src_socket
= src_socket
;
725 routing_needed(*mp
, ifID
, TRUE
);
727 goto exit_ddp_output
;
730 /* by the time we land here, we know the interface on
731 * which this packet is going out.... ifID.
733 if (ifID
->ifState
== LAP_OFFLINE
) {
735 goto exit_ddp_output
;
741 at_dest.atalk_unused = 0;
743 NET_ASSIGN(at_dest
.atalk_net
, dst_net
);
744 at_dest
.atalk_node
= ddp
->dst_node
;
746 addr
= (char *)&at_dest
;
750 addr
= (char *)&brt
->et_addr
;
753 NET_ASSIGN(at_dest
.atalk_net
, ifID
->ifARouter
.s_net
);
754 at_dest
.atalk_node
= ifID
->ifARouter
.s_node
;
756 addr
= (char *)&at_dest
;
760 /* Irrespective of the interface on which
761 * the packet is going out, we always put the
762 * same source address on the packet (unless multihoming mode).
764 if (MULTIHOME_MODE
) {
765 if (!src_addr_included
) {
766 ddp
->src_node
= ifID
->ifThisNode
.s_node
;
767 NET_ASSIGN(ddp
->src_net
, ifID
->ifThisNode
.s_net
);
771 ddp
->src_node
= ifID_home
->ifThisNode
.s_node
;
772 NET_ASSIGN(ddp
->src_net
, ifID_home
->ifThisNode
.s_net
);
774 ddp
->src_socket
= src_socket
;
776 dPrintf(D_M_DDP_LOW
, D_L_OUTPUT
,
777 ("ddp_output: going out to %d:%d skt%d on %s\n",
778 dst_net
, ddp
->dst_node
, ddp
->dst_socket
, ifID
->ifName
));
780 fillin_pkt_chain(*mp
);
783 struct etalk_addr dest_addr
;
784 struct atalk_addr dest_at_addr
;
786 loop
= TRUE
; /* flag to aarp to loopback (default) */
790 /* the incoming frame is of the form {flag, address, ddp...}
791 * where "flag" indicates whether the address is an 802.3
792 * (link) address, or an appletalk address. If it's an
793 * 802.3 address, the packet can just go out to the network
794 * through PAT, if it's an appletalk address, AT->802.3 address
795 * resolution needs to be done.
796 * If 802.3 address is known, strip off the flag and 802.3
797 * address, and prepend 802.2 and 802.3 headers.
801 addr_flag
= *(u_char
*)gbuf_rptr(m
);
806 case AT_ADDR_NO_LOOP
:
811 dest_at_addr
= *(struct atalk_addr
*)gbuf_rptr(m
);
812 gbuf_rinc(m
,sizeof(struct atalk_addr
));
814 dest_at_addr
= *(struct atalk_addr
*)addr
;
818 dest_addr
= *(struct etalk_addr
*)gbuf_rptr(m
);
819 gbuf_rinc(m
,sizeof(struct etalk_addr
));
821 dest_addr
= *(struct etalk_addr
*)addr
;
824 dPrintf(D_M_DDP_LOW
,D_L_ERROR
,
825 ("ddp_output: Unknown addr_flag = 0x%x\n", addr_flag
));
826 gbuf_freel(m
); /* unknown address type, chuck it */
827 goto exit_ddp_output
;
832 /* At this point, rptr points to ddp header for sure */
833 if (ifID
->ifState
== LAP_ONLINE_FOR_ZIP
) {
834 /* see if this is a ZIP packet that we need
835 * to let through even though network is
838 if (zip_type_packet(m
) == 0) {
840 goto exit_ddp_output
;
844 ifID
->stats
.xmit_packets
++;
845 ifID
->stats
.xmit_bytes
+= gbuf_msgsize(m
);
846 snmpStats
.dd_outLong
++;
849 case AT_ADDR_NO_LOOP
:
852 * we don't want elap to be looking into ddp header, so
853 * it doesn't know net#, consequently can't do
854 * AMT_LOOKUP. That task left to aarp now.
856 aarp_send_data(m
,ifID
, &dest_at_addr
, loop
);
859 pat_output(ifID
, m
, (unsigned char *)&dest_addr
, 0);
864 KERNEL_DEBUG(DBG_AT_DDP_OUTPUT
| DBG_FUNC_END
, 0,
869 void ddp_input(mp
, ifID
)
871 register at_ifaddr_t
*ifID
;
873 register at_ddp_t
*ddp
; /* DDP header */
874 register int msgsize
;
875 register at_socket socket
;
877 register at_net_al dst_net
;
879 KERNEL_DEBUG(DBG_AT_DDP_INPUT
| DBG_FUNC_START
, 0,
880 ifID
, mp
, gbuf_len(mp
),0);
882 /* Makes sure we know the default interface before starting to
883 * accept incomming packets. If we don't we may end up with a
884 * null ifID_table[0] and have impredicable results (specially
885 * in router mode. This is a transitory state (because we can
886 * begin to receive packet while we're not completly set up yet.
889 if (ifID_home
== (at_ifaddr_t
*)NULL
) {
890 dPrintf(D_M_DDP
, D_L_ERROR
,
891 ("dropped incoming packet ifID_home not set yet\n"));
893 goto out
; /* return */
897 * if a DDP packet has been broadcast, we're going to get a copy of
898 * it here; if it originated at user level via a write on a DDP
899 * socket; when it gets here, the first block in the chain will be
900 * empty since it only contained the lap level header which will be
901 * stripped in the lap level immediately below ddp
904 if ((mp
= (gbuf_t
*)ddp_compress_msg(mp
)) == NULL
) {
905 dPrintf(D_M_DDP
, D_L_ERROR
,
906 ("dropped short incoming ET packet (len %d)", 0));
907 snmpStats
.dd_inTotal
++;
908 at_ddp_stats
.rcv_bad_length
++;
909 goto out
; /* return; */
911 msgsize
= gbuf_msgsize(mp
);
913 at_ddp_stats
.rcv_bytes
+= msgsize
;
914 at_ddp_stats
.rcv_packets
++;
916 /* if the interface pointer is 0, the packet has been
917 * looped back by 'write' half of DDP. It is of the
918 * form {extended ddp,...}. The packet is meant to go
919 * up to some socket on the same node.
921 if (!ifID
) /* if loop back is specified */
922 ifID
= ifID_home
; /* that means the home port */
924 /* the incoming datagram has extended DDP header and is of
925 * the form {ddp,...}.
927 if (msgsize
< DDP_X_HDR_SIZE
) {
928 dPrintf(D_M_DDP
, D_L_ERROR
,
929 ("dropped short incoming ET packet (len %d)", msgsize
));
930 at_ddp_stats
.rcv_bad_length
++;
932 goto out
; /* return; */
935 * At this point, the message is always of the form
936 * {extended ddp, ... }.
938 ddp
= (at_ddp_t
*)gbuf_rptr(mp
);
939 len
= DDPLEN_VALUE(ddp
);
941 if (msgsize
!= len
) {
943 if (len
< DDP_X_HDR_SIZE
) {
944 dPrintf(D_M_DDP
, D_L_ERROR
,
945 ("Length problems, ddp length %d, buffer length %d",
947 snmpStats
.dd_tooLong
++;
948 at_ddp_stats
.rcv_bad_length
++;
950 goto out
; /* return; */
953 * shave off the extra bytes from the end of message
955 mp
= ddp_adjmsg(mp
, -(msgsize
- len
)) ? mp
: 0;
957 goto out
; /* return; */
959 dPrintf(D_M_DDP
, D_L_ERROR
,
960 ("Length problems, ddp length %d, buffer length %d",
962 snmpStats
.dd_tooShort
++;
963 at_ddp_stats
.rcv_bad_length
++;
965 goto out
; /* return; */
968 socket
= ddp
->dst_socket
;
971 * We want everything in router mode, specially socket 254 for nbp so we need
972 * to bypass this test when we are a router.
975 if (!MULTIPORT_MODE
&& (socket
> DDP_SOCKET_LAST
||
976 socket
< DDP_SOCKET_1st_RESERVED
)) {
977 dPrintf(D_M_DDP
, D_L_WARNING
,
978 ("Bad dst socket on incoming packet (0x%x)",
980 at_ddp_stats
.rcv_bad_socket
++;
982 goto out
; /* return; */
985 * if the checksum is true, then upstream wants us to calc
987 if (UAS_VALUE(ddp
->checksum
) &&
988 (UAS_VALUE_NTOH(ddp
->checksum
) != ddp_checksum(mp
, 4))) {
989 dPrintf(D_M_DDP
, D_L_WARNING
,
990 ("Checksum error on incoming pkt, calc 0x%x, exp 0x%x",
991 ddp_checksum(mp
, 4), UAS_VALUE_NTOH(ddp
->checksum
)));
992 snmpStats
.dd_checkSum
++;
993 at_ddp_stats
.rcv_bad_checksum
++;
995 goto out
; /* return; */
998 /*############### routing input checking */
1000 /* Router mode special: we send "up-stack" packets for this node or coming from any
1001 * other ports, but for the reserved atalk sockets (RTMP, ZIP, NBP [and EP])
1002 * BTW, the way we know it's for the router and not the home port is that the
1003 * MAC (ethernet) address is always the one of the interface we're on, but
1004 * the AppleTalk address must be the one of the home port. If it's a multicast
1005 * or another AppleTalk address, this is the router job's to figure out where it's
1008 /* *** a duplicate should be sent to any other client that is listening
1009 for packets of this type on a raw DDP socket *** */
1010 if (ddp_handler
[socket
].func
) {
1011 dPrintf(D_M_DDP
,D_L_INPUT
,
1012 ("ddp_input: skt %u hdnlr:0x%p\n",
1013 (u_int
) socket
, ddp_handler
[socket
].func
));
1015 snmpStats
.dd_inLocal
++;
1017 (*ddp_handler
[socket
].func
)(mp
, ifID
);
1018 goto out
; /* return; */
1020 dst_net
= NET_VALUE(ddp
->dst_net
);
1024 /* any node, wildcard or matching net */
1025 ((ddp
->dst_node
== 255) &&
1026 (((dst_net
>= ifID_home
->ifThisCableStart
) &&
1027 (dst_net
<= ifID_home
->ifThisCableEnd
)) ||
1029 /* this node is not online yet(?) */
1030 (ifID
->ifRoutingState
< PORT_ONLINE
)
1034 snmpStats
.dd_inLocal
++;
1036 if (ddp
->type
== DDP_ATP
) {
1037 if (atp_inputQ
[socket
] && (atp_inputQ
[socket
] != (gref_t
*)1)) {
1038 /* if there's an ATP pcb */
1040 goto out
; /* return; */
1042 } else if (ddp
->type
== DDP_ADSP
) {
1043 if (adsp_inputQ
[socket
]) {
1044 /* if there's an ADSP pcb */
1046 goto out
; /* return; */
1050 /* otherwise look for a DDP pcb;
1051 ATP / raw-DDP and ADSP / raw-DDP are possible */
1052 for (gref
= ddp_head
.atpcb_next
; gref
!= &ddp_head
;
1053 gref
= gref
->atpcb_next
)
1054 if (gref
->lport
== socket
&&
1055 (gref
->ddptype
== 0 || gref
->ddptype
== ddp
->type
)) {
1056 dPrintf(D_M_DDP
, D_L_INPUT
,
1057 ("ddp_input: streamq, skt %d\n", socket
));
1058 if (gref
->atpcb_socket
) {
1059 struct sockaddr_at ddp_in
;
1060 ddp_in
.sat_len
= sizeof(ddp_in
);
1061 ddp_in
.sat_family
= AF_APPLETALK
;
1062 ddp_in
.sat_addr
.s_net
= NET_VALUE(ddp
->src_net
);
1063 ddp_in
.sat_addr
.s_node
= ddp
->src_node
;
1064 ddp_in
.sat_port
= ddp
->src_socket
;
1066 /* strip off DDP header if so indicated by
1068 if (gref
->ddp_flags
& DDPFLG_STRIPHDR
) {
1069 mp
= m_pullup((struct mbuf
*)mp
,
1072 gbuf_rinc(mp
, DDP_X_HDR_SIZE
);
1074 /* this should never happen because
1075 msgsize was checked earlier */
1076 at_ddp_stats
.rcv_bad_length
++;
1077 goto out
; /* return */
1081 if (sbappendaddr(&((gref
->atpcb_socket
)->so_rcv
),
1082 (struct sockaddr
*)&ddp_in
,
1083 mp
, 0, NULL
) != 0) {
1084 sorwakeup(gref
->atpcb_socket
);
1087 atalk_putnext(gref
, mp
);
1089 goto out
; /* return */
1092 at_ddp_stats
.rcv_bad_socket
++;
1094 snmpStats
.dd_noHandler
++;
1095 dPrintf(D_M_DDP
, D_L_WARNING
,
1096 ("ddp_input: dropped pkt for socket %d\n", socket
));
1098 dPrintf(D_M_DDP
, D_L_ROUTING
,
1099 ("ddp_input: routing_needed from port=%d sock=%d\n",
1100 ifID
->ifPort
, ddp
->dst_socket
));
1102 snmpStats
.dd_fwdReq
++;
1103 if (((pktsIn
-pktsHome
+200) >= RouterMix
) && ((++pktsDropped
% 5) == 0)) {
1104 at_ddp_stats
.rcv_dropped_nobuf
++;
1108 routing_needed(mp
, ifID
, FALSE
);
1112 KERNEL_DEBUG(DBG_AT_DDP_INPUT
| DBG_FUNC_END
, 0,0,0,0,0);
1117 * ddp_router_output()
1120 * This is a modified version of ddp_output for router use.
1121 * The main difference is that the interface on which the packet needs
1122 * to be sent is specified and a *destination* AppleTalk address is passed
1123 * as an argument, this address may or may not be the same as the destination
1124 * address found in the ddp packet... This is the trick about routing, the
1125 * AppleTalk destination of the packet may not be the same as the Enet address
1126 * we send the packet too (ie, we may pass the baby to another router).
1129 int ddp_router_output(mp
, ifID
, addr_type
, router_net
, router_node
, enet_addr
)
1133 at_net_al router_net
;
1134 at_node router_node
;
1135 etalk_addr_t
*enet_addr
;
1137 register at_ddp_t
*ddp
;
1138 struct atalk_addr at_dest
;
1144 dPrintf(D_M_DDP
, D_L_WARNING
, ("BAD BAD ifID\n"));
1148 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
);
1165 /* keep some of the tests for now ####### */
1167 if (gbuf_msgsize(mp
) > DDP_DATAGRAM_SIZE
) {
1168 /* the packet is too large */
1169 dPrintf(D_M_DDP
, D_L_WARNING
,
1170 ("ddp_router_output: Packet too large size=%d\n",
1176 switch (addr_type
) {
1181 * Check for packet destined to the home stack
1184 if ((ddp
->dst_node
== ifID
->ifThisNode
.s_node
) &&
1185 (NET_VALUE(ddp
->dst_net
) == ifID
->ifThisNode
.s_net
)) {
1186 dPrintf(D_M_DDP_LOW
, D_L_ROUTING
,
1187 ("ddp_r_output: sending back home from port=%d socket=%d\n",
1188 ifID
->ifPort
, ddp
->dst_socket
));
1190 UAS_ASSIGN(ddp
->checksum
, 0);
1191 ddp_input(mp
, ifID
);
1195 NET_ASSIGN(at_dest
.atalk_net
, router_net
);
1196 at_dest
.atalk_node
= router_node
;
1198 addr_flag
= AT_ADDR_NO_LOOP
;
1199 addr
= (char *)&at_dest
;
1200 dPrintf(D_M_DDP_LOW
, D_L_ROUTING_AT
,
1201 ("ddp_r_output: AT_ADDR out port=%d net %d:%d via rte %d:%d",
1202 ifID
->ifPort
, NET_VALUE(ddp
->dst_net
), ddp
->dst_node
, router_net
,
1207 addr_flag
= ET_ADDR
;
1208 addr
= (char *)enet_addr
;
1209 dPrintf(D_M_DDP_LOW
, D_L_ROUTING
,
1210 ("ddp_r_output: ET_ADDR out port=%d net %d:%d\n",
1211 ifID
->ifPort
, NET_VALUE(ddp
->dst_net
), ddp
->dst_node
));
1215 if (ifID
->ifState
== LAP_OFFLINE
) {
1220 fillin_pkt_chain(mp
);
1223 struct etalk_addr dest_addr
;
1224 struct atalk_addr dest_at_addr
;
1225 int loop
= TRUE
; /* flag to aarp to loopback (default) */
1229 /* the incoming frame is of the form {flag, address, ddp...}
1230 * where "flag" indicates whether the address is an 802.3
1231 * (link) address, or an appletalk address. If it's an
1232 * 802.3 address, the packet can just go out to the network
1233 * through PAT, if it's an appletalk address, AT->802.3 address
1234 * resolution needs to be done.
1235 * If 802.3 address is known, strip off the flag and 802.3
1236 * address, and prepend 802.2 and 802.3 headers.
1240 addr_flag
= *(u_char
*)gbuf_rptr(m
);
1244 switch (addr_flag
) {
1245 case AT_ADDR_NO_LOOP
:
1250 dest_at_addr
= *(struct atalk_addr
*)gbuf_rptr(m
);
1251 gbuf_rinc(m
,sizeof(struct atalk_addr
));
1253 dest_at_addr
= *(struct atalk_addr
*)addr
;
1257 dest_addr
= *(struct etalk_addr
*)gbuf_rptr(m
);
1258 gbuf_rinc(m
,sizeof(struct etalk_addr
));
1260 dest_addr
= *(struct etalk_addr
*)addr
;
1263 dPrintf(D_M_DDP_LOW
,D_L_ERROR
,
1264 ("ddp_router_output: Unknown addr_flag = 0x%x\n", addr_flag
));
1266 gbuf_freel(m
); /* unknown address type, chuck it */
1272 /* At this point, rptr points to ddp header for sure */
1273 if (ifID
->ifState
== LAP_ONLINE_FOR_ZIP
) {
1274 /* see if this is a ZIP packet that we need
1275 * to let through even though network is
1278 if (zip_type_packet(m
) == 0) {
1284 ifID
->stats
.xmit_packets
++;
1285 ifID
->stats
.xmit_bytes
+= gbuf_msgsize(m
);
1286 snmpStats
.dd_outLong
++;
1288 switch (addr_flag
) {
1289 case AT_ADDR_NO_LOOP
:
1292 * we don't want elap to be looking into ddp header, so
1293 * it doesn't know net#, consequently can't do
1294 * AMT_LOOKUP. That task left to aarp now.
1296 aarp_send_data(m
,ifID
,&dest_at_addr
, loop
);
1299 pat_output(ifID
, m
, (unsigned char *)&dest_addr
, 0);
1305 } /* ddp_router_output */
1307 /*****************************************/
1311 void rt_delete(NetStop
, NetStart
)
1312 unsigned short NetStop
;
1313 unsigned short NetStart
;
1317 if ((found
= rt_bdelete(NetStop
, NetStart
)) != 0) {
1318 bzero(found
, sizeof(RT_entry
));
1319 found
->right
= RT_table_freelist
;
1320 RT_table_freelist
= found
;
1324 int ddp_AURPfuncx(code
, param
, node
)
1333 case AURPCODE_DATAPKT
: /* data packet */
1335 dPrintf(D_M_DDP
, D_L_TRACE
, ("ddp_AURPfuncx: data, 0x%x, %d\n",
1336 (u_int
) aurp_ifID
, node
));
1338 ddp_input((gbuf_t
*)param
, aurp_ifID
);
1340 gbuf_freem((gbuf_t
*)param
);
1343 case AURPCODE_REG
: /* register/deregister */
1346 ddp_AURPsendx
= (void(*)())param
;
1349 /* register AURP callback function */
1352 for (k
=(IFID_HOME
+1); k
< IF_TOTAL_MAX
; k
++) {
1353 if (ifID_table
[k
] == 0) {
1354 aurp_ifID
= &at_interfaces
[k
];
1355 aurp_ifID
->ifFlags
= RTR_XNET_PORT
;
1356 ddp_add_if(aurp_ifID
);
1357 aurp_ifID
->ifState
= LAP_ONLINE
;
1358 aurp_ifID
->ifRoutingState
= PORT_ONLINE
;
1359 dPrintf(D_M_DDP
, D_L_TRACE
,
1360 ("ddp_AURPfuncx: on, 0x%x\n",
1361 (u_int
) aurp_ifID
));
1363 ddp_AURPsendx(AURPCODE_DEBUGINFO
,
1364 &dbgBits
, aurp_ifID
->ifPort
);
1371 /* deregister AURP callback function */
1373 rtmp_purge(aurp_ifID
);
1374 ddp_rem_if(aurp_ifID
);
1375 aurp_ifID
->ifState
= LAP_OFFLINE
;
1376 aurp_ifID
->ifRoutingState
= PORT_OFFLINE
;
1377 dPrintf(D_M_DDP
, D_L_TRACE
,
1378 ("ddp_AURPfuncx: off, 0x%x\n", (u_int
) aurp_ifID
));
1384 case AURPCODE_AURPPROTO
: /* proto type - AURP */
1386 aurp_ifID
->ifFlags
|= AT_IFF_AURP
;
1395 /* checks to see if address of packet is for one of our interfaces
1396 returns *ifID if it's for us, NULL if not
1398 at_ifaddr_t
*forUs(ddp
)
1399 register at_ddp_t
*ddp
;
1403 TAILQ_FOREACH(ifID
, &at_ifQueueHd
, aa_link
) {
1404 if ((ddp
->dst_node
== ifID
->ifThisNode
.s_node
) &&
1405 (NET_VALUE(ddp
->dst_net
) == ifID
->ifThisNode
.s_net
)
1407 dPrintf(D_M_DDP_LOW
, D_L_ROUTING
,
1408 ("pkt was for port %d\n", ifID
->ifPort
));
1414 return((at_ifaddr_t
*)NULL
);