2 * Copyright (c) 2000 Apple Computer, 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 * Copyright (c) 1987, 1988, 1989 Apple Computer, Inc.
32 * Modified for MP, 1996 by Tuyen Nguyen
33 * Added AURP support, April 8, 1996 by Tuyen Nguyen
34 * Modified, March 17, 1997 by Tuyen Nguyen for MacOSX.
37 #define RESOLVE_DBG /* define debug globals in debug.h */
39 #include <sys/errno.h>
40 #include <sys/types.h>
41 #include <sys/param.h>
42 #include <machine/spl.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
46 #include <sys/filedesc.h>
47 #include <sys/fcntl.h>
49 #include <sys/ioctl.h>
50 #include <sys/malloc.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
53 #include <sys/protosw.h>
58 #include <netat/sysglue.h>
59 #include <netat/appletalk.h>
60 #include <netat/at_var.h>
61 #include <netat/ddp.h>
63 #include <netat/nbp.h>
64 #include <netat/rtmp.h>
65 #include <netat/zip.h>
66 #include <netat/at_pcb.h>
67 #include <netat/routing_tables.h>
68 #include <netat/at_snmp.h>
69 #include <netat/aurp.h>
70 #include <netat/debug.h>
71 #include <netat/at_ddp_brt.h>
72 #include <netat/at_aarp.h>
73 #include <netat/adsp.h>
74 #include <netat/adsp_internal.h>
78 /* Queue of LAP interfaces which have registered themselves with DDP */
79 struct at_ifQueueHd at_ifQueueHd
;
81 extern at_state_t at_state
;
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
)();
94 at_ifaddr_t
*aurp_ifID
= 0;
95 extern pktsIn
,pktsOut
;
96 int pktsDropped
,pktsHome
;
100 extern int *atp_pidM
;
101 extern int *adsp_pidM
;
102 extern struct atpcb
*atp_inputQ
[];
103 extern CCB
*adsp_inputQ
[];
105 at_ifaddr_t
*forUs(at_ddp_t
*);
107 void ddp_input(), ddp_notify_nbp();
109 extern void routing_needed();
110 extern void ddp_brt_sweep();
116 void init_ddp_handler()
118 bzero(ddp_handler
, sizeof(ddp_handler
));
121 void add_ddp_handler(ddp_socket
, input_func
)
123 void (*input_func
)();
125 ddp_handler
[ddp_socket
].func
= input_func
;
135 * Raw DDP socket option processing.
137 int ddp_ctloutput(so
, sopt
)
139 struct sockopt
*sopt
;
141 struct atpcb
*at_pcb
= sotoatpcb(so
);
142 int optval
, error
= 0;
144 if (sopt
->sopt_level
!= ATPROTO_NONE
)
147 switch (sopt
->sopt_dir
) {
150 switch (sopt
->sopt_name
) {
152 optval
= at_pcb
->ddp_flags
& DDPFLG_HDRINCL
;
153 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
156 optval
= at_pcb
->ddp_flags
& DDPFLG_CHKSUM
;
157 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
160 optval
= at_pcb
->ddp_flags
& DDPFLG_STRIPHDR
;
161 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
164 optval
= at_pcb
->ddp_flags
& DDPFLG_SLFSND
;
165 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
167 case DDP_GETSOCKNAME
:
170 addr
.inet
.net
= at_pcb
->laddr
.s_net
;
171 addr
.inet
.node
= at_pcb
->laddr
.s_node
;
172 addr
.inet
.socket
= at_pcb
->lport
;
173 addr
.ddptype
= at_pcb
->ddptype
;
174 error
= sooptcopyout(sopt
, &addr
, sizeof addr
);
183 switch (sopt
->sopt_name
) {
185 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
190 at_pcb
->ddp_flags
|= DDPFLG_HDRINCL
;
192 at_pcb
->ddp_flags
&= ~DDPFLG_HDRINCL
;
195 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
200 at_pcb
->ddp_flags
|= DDPFLG_CHKSUM
;
202 at_pcb
->ddp_flags
&= ~DDPFLG_CHKSUM
;
205 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
210 at_pcb
->ddp_flags
|= DDPFLG_STRIPHDR
;
212 at_pcb
->ddp_flags
&= ~DDPFLG_STRIPHDR
;
215 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
220 at_pcb
->ddp_flags
|= DDPFLG_SLFSND
;
222 at_pcb
->ddp_flags
&= ~DDPFLG_SLFSND
;
234 /****************************************************************/
237 /* Support Routines */
240 /****************************************************************/
247 * This procedure determines the checksum of an extended DDP datagram.
248 * Add the unsigned bytes into an unsigned 16-bit accumulator.
249 * After each add, rotate the sign bit into the low order bit of
250 * the accumulator. When done, if the checksum is 0, changed into 0xFFFF.
253 * checksum = ddp_checksum(mp, offset)
256 * mp pointer to the datagram gbuf_t
257 * offset offset to start at in first gbuf_t block
264 u_short
ddp_checksum(mp
, offset
)
268 register u_char
*data
;
270 register u_short checksum
;
275 if (offset
>= gbuf_len(mp
))
276 offset
-= gbuf_len(mp
);
278 data
= ((unsigned char *) gbuf_rptr(mp
)) + offset
;
279 length
= gbuf_len(mp
) - offset
;
281 /* Portable checksum from 3.0 */
284 checksum
= (checksum
& 0x8000) ?
285 ((checksum
<< 1) | 1) : (checksum
<< 1);
288 } while ( (mp
= gbuf_cont(mp
)) );
300 * This procedure is called by each LAP interface when it wants to place
301 * itself online. The LAP interfaces passes in a pointer to its at_if
302 * struct, which is added to DDP's list of active interfaces (at_ifQueueHd).
303 * When DDP wants to transmit a packet, it searches this list for the
306 * If AT_IFF_DEFAULT is set, then this interface is to be brought online
307 * as the interface DDP socket addresses are tied to. Of course there can
308 * be only one default interface; we return an error if it's already set.
311 * ret_status = ddp_add_if(ifID)
314 * ifID pointer to LAP interface's at_if struct.
317 * 0 Procedure successfully completed.
318 * EALREADY This interface is already online, or there is
319 * already a default interface.
320 * ENOBUFS Cannot allocate input queue
324 register at_ifaddr_t
*ifID
;
328 dPrintf(D_M_DDP
, D_L_STARTUP
,
329 ("ddp_add_if: called, ifID:0x%x\n", (u_int
) ifID
));
331 if (ifID
->ifFlags
& AT_IFF_DEFAULT
) {
333 return(EEXIST
); /* home port already set */
339 for (port
=IFID_HOME
+1; port
<IF_TOTAL_MAX
; port
++)
340 if (!ifID_table
[port
]) {
343 if (port
== IF_TOTAL_MAX
) /* no space left */
347 /* allocate an et_aarp_amt structure */
348 if ((aarp_table
[port
] =
349 (aarp_amt_array
*)_MALLOC(sizeof(aarp_amt_array
),
350 M_RTABLE
, M_WAITOK
)) == NULL
)
353 dPrintf(D_M_DDP
, D_L_STARTUP
, ("ddp:adding ifID_table[%d]\n", port
));
355 /* add i/f to port list */
356 ifID_table
[port
] = ifID
;
357 ifID
->ifPort
= port
; /* set ddp port # in ifID */
359 /* Add this interface to the list of online interfaces */
360 TAILQ_INSERT_TAIL(&at_ifQueueHd
, ifID
, aa_link
);
369 * This procedure is called by each LAP interface when it wants to take
370 * itself offline. The LAP interfaces passes in a pointer to its at_if
371 * struct; DDP's list of active interfaces (at_ifQueueHd) is searched and
372 * this interface is removed from the list. DDP can still transmit
373 * packets as long as this interface is not the default interface; the
374 * sender will just get ENETUNREACH errors when it tries to send to an
375 * interface that went offline. However, if the default interface is
376 * taken offline, we no longer have a node ID to use as a source address
377 * and DDP must return ENETDOWN when a caller tries to send a packet.
380 * ifID pointer to LAP interface's at_if struct.
383 void ddp_rem_if(ifID
)
384 register at_ifaddr_t
*ifID
;
386 struct ifaddr
*ifa
= &ifID
->aa_ifa
;
388 /* un-do processing done in SIOCSIFADDR */
390 ifnet_lock_exclusive(ifID
->aa_ifp
);
391 if_detach_ifa(ifID
->aa_ifp
, ifa
);
392 ifa
->ifa_addr
= NULL
;
393 ifnet_lock_done(ifID
->aa_ifp
);
395 if (ifID
->at_dl_tag
) {
396 /* dlil_detach_protocol(ifID->at_dl_tag); */
397 ether_detach_at(ifID
->aa_ifp
);
401 /* un-do processing done in ddp_add_if() */
403 if (aarp_table
[ifID
->ifPort
]) {
404 FREE(aarp_table
[ifID
->ifPort
], M_RTABLE
);
405 aarp_table
[ifID
->ifPort
] = NULL
;
408 at_state
.flags
|= AT_ST_IF_CHANGED
;
411 trackrouter_rem_if(ifID
);
412 TAILQ_REMOVE(&at_ifQueueHd
, ifID
, aa_link
);
413 ifID_table
[ifID
->ifPort
] = NULL
;
414 ifID
->ifName
[0] = '\0';
418 /* *** deallocate ifID, eventually *** */
422 * The user may have registered an NVE with the NBP on a socket. When the
423 * socket is closed, the NVE should be deleted from NBP's name table. The
424 * user should delete the NVE before the socket is shut down, but there
425 * may be circumstances when he can't. So, whenever a DDP socket is closed,
426 * this routine is used to notify NBP of the socket closure. This would
427 * help NBP get rid of all NVE's registered on the socket.
430 /* *** Do we still need to do this? *** */
431 int ot_ddp_check_socket(socket
, pid
)
432 unsigned char socket
;
438 dPrintf(D_M_DDP
, D_L_INFO
, ("ot_ddp_check_socket: %d\n", socket
));
439 for (gref
= ddp_head
.atpcb_next
; gref
!= &ddp_head
; gref
= gref
->atpcb_next
)
440 if (gref
->lport
== socket
&& gref
->pid
== pid
)
442 if ((atp_inputQ
[socket
] != NULL
) && (atp_inputQ
[socket
] != (gref_t
*)1)
443 && (atp_pidM
[socket
] == pid
))
445 if ((adsp_inputQ
[socket
] != NULL
) && (adsp_pidM
[socket
] == pid
))
451 void ddp_notify_nbp(socket
, pid
, ddptype
)
452 unsigned char socket
;
454 unsigned char ddptype
; /* not used */
457 nve_entry_t
*nve_entry
, *nve_next
;
459 if (at_state
.flags
& AT_ST_STARTED
) {
460 /* *** NBP_CLOSE_NOTE processing (from ddp_nbp.c) *** */
461 ATDISABLE(nve_lock
, NVE_LOCK
);
462 for ((nve_entry
= TAILQ_FIRST(&name_registry
)); nve_entry
; nve_entry
= nve_next
) {
463 nve_next
= TAILQ_NEXT(nve_entry
, nve_link
);
464 if ((at_socket
)socket
== nve_entry
->address
.socket
&&
465 /* *** check complete address and ddptype here *** */
466 pid
== nve_entry
->pid
&&
467 ot_ddp_check_socket(nve_entry
->address
.socket
,
468 nve_entry
->pid
) < 2) {
469 /* NB: nbp_delete_entry calls TAILQ_REMOVE */
470 nbp_delete_entry(nve_entry
);
473 ATENABLE(nve_lock
, NVE_LOCK
);
475 } /* ddp_notify_nbp */
477 static void fillin_pkt_chain(m
)
482 *ddp
= (at_ddp_t
*)gbuf_rptr(m
),
486 if (UAS_VALUE(ddp
->checksum
)) {
487 tmp
= ddp_checksum(m
, 4);
488 UAS_ASSIGN(ddp
->checksum
, tmp
);
491 for (tmp_m
=gbuf_next(tmp_m
); tmp_m
; tmp_m
=gbuf_next(tmp_m
)) {
492 tmp_ddp
= (at_ddp_t
*)gbuf_rptr(tmp_m
);
493 tmp_ddp
->length
= gbuf_msgsize(tmp_m
);
496 NET_NET(tmp_ddp
->src_net
, ddp
->src_net
);
497 tmp_ddp
->src_node
= ddp
->src_node
;
498 tmp_ddp
->src_socket
= ddp
->src_socket
;
499 if (UAS_VALUE(tmp_ddp
->checksum
)) {
500 tmp
= ddp_checksum(tmp_m
, 4);
501 UAS_ASSIGN(tmp_ddp
->checksum
, tmp
);
506 /* There are various ways a packet may go out.... it may be sent out
507 * directly to destination node, or sent to a random router or sent
508 * to a router whose entry exists in Best Router Cache. Following are
509 * constants used WITHIN this routine to keep track of choice of destination
511 #define DIRECT_ADDR 1
513 #define BRIDGE_ADDR 3
519 * Called to queue a atp/ddp data packet on the network interface.
520 * It returns 0 normally, and an errno in case of error.
521 * The mbuf chain pointed to by *mp is consumed on success, and
522 * freed in case of error.
525 int ddp_output(mp
, src_socket
, src_addr_included
)
526 register gbuf_t
**mp
;
527 at_socket src_socket
;
528 int src_addr_included
;
530 register at_ifaddr_t
*ifID
= ifID_home
, *ifIDTmp
= NULL
;
531 register at_ddp_t
*ddp
;
532 register ddp_brt_t
*brt
;
533 register at_net_al dst_net
;
535 struct atalk_addr at_dest
;
536 at_ifaddr_t
*ARouterIf
= NULL
;
544 KERNEL_DEBUG(DBG_AT_DDP_OUTPUT
| DBG_FUNC_START
, 0,
547 snmpStats
.dd_outReq
++;
550 ddp
= (at_ddp_t
*)gbuf_rptr(m
);
553 /* Device/Interface not configured */
554 dPrintf(D_M_DDP
, D_L_ERROR
, ("Device/Interface not configured"));
557 goto exit_ddp_output
;
560 if ((ddp
->dst_socket
> (unsigned) (DDP_SOCKET_LAST
+ 1)) ||
561 (ddp
->dst_socket
< DDP_SOCKET_1st_RESERVED
)) {
562 dPrintf(D_M_DDP
, D_L_ERROR
,
563 ("Illegal destination socket on outgoing packet (0x%x)",
565 at_ddp_stats
.xmit_bad_addr
++;
568 goto exit_ddp_output
;
570 if ((len
= gbuf_msgsize(*mp
)) > DDP_DATAGRAM_SIZE
) {
571 /* the packet is too large */
572 dPrintf(D_M_DDP
, D_L_ERROR
,
573 ("Outgoing packet too long (len=%d bytes)", len
));
574 at_ddp_stats
.xmit_bad_length
++;
577 goto exit_ddp_output
;
579 at_ddp_stats
.xmit_bytes
+= len
;
580 at_ddp_stats
.xmit_packets
++;
586 /* If this packet is for the same node, loop it back
587 * up... Note that for LocalTalk, dst_net zero means "THIS_NET", so
588 * address 0.nn is eligible for loopback. For Extended EtherTalk,
589 * dst_net 0 can be used only for cable-wide or zone-wide
590 * broadcasts (0.ff) and as such, address of the form 0.nn is NOT
591 * eligible for loopback.
593 dst_net
= NET_VALUE(ddp
->dst_net
);
595 /* If our packet is destined for the 'virtual' bridge
596 * address of NODE==0xFE, replace that address with a
597 * real bridge address.
599 if ((ddp
->dst_node
== 0xfe) &&
600 ((dst_net
== ATADDR_ANYNET
) ||
601 (dst_net
>= ifID_home
->ifThisCableStart
&&
602 dst_net
<= ifID_home
->ifThisCableEnd
))) {
603 /* if there's a router that's not us, it's in ifID_home */
604 NET_ASSIGN(ddp
->dst_net
, ifID_home
->ifARouter
.s_net
);
605 dst_net
= ifID_home
->ifARouter
.s_net
;
606 ddp
->dst_node
= ifID_home
->ifARouter
.s_node
;
609 if (MULTIHOME_MODE
&& (ifIDTmp
= forUs(ddp
))) {
612 dPrintf(D_M_DDP_LOW
, D_L_USR1
,
613 ("ddp_out: for us if:%s\n", ifIDTmp
->ifName
));
617 loop
= ((ddp
->dst_node
== ifID
->ifThisNode
.s_node
) &&
618 (dst_net
== ifID
->ifThisNode
.s_net
)
621 gbuf_t
*mdata
, *mdata_next
;
623 if (!MULTIHOME_MODE
|| !src_addr_included
) {
624 NET_ASSIGN(ddp
->src_net
, ifID
->ifThisNode
.s_net
);
625 ddp
->src_node
= ifID
->ifThisNode
.s_node
;
627 ddp
->src_socket
= src_socket
;
629 dPrintf(D_M_DDP_LOW
, D_L_OUTPUT
,
630 ("ddp_output: loop to %d:%d port=%d\n",
631 NET_VALUE(ddp
->dst_net
),
635 fillin_pkt_chain(*mp
);
637 dPrintf(D_M_DDP
, D_L_VERBOSE
,
638 ("Looping back packet from skt 0x%x to skt 0x%x\n",
639 ddp
->src_socket
, ddp
->dst_socket
));
641 for (mdata
= *mp
; mdata
; mdata
= mdata_next
) {
642 mdata_next
= gbuf_next(mdata
);
643 gbuf_next(mdata
) = 0;
644 ddp_input(mdata
, ifID
);
646 goto exit_ddp_output
;
648 if ((ddp
->dst_socket
== ZIP_SOCKET
) &&
649 (zip_type_packet(*mp
) == ZIP_GETMYZONE
)) {
650 ddp
->src_socket
= src_socket
;
651 error
= zip_handle_getmyzone(ifID
, *mp
);
653 goto exit_ddp_output
;
656 * find out the interface on which the packet should go out
658 TAILQ_FOREACH(ifID
, &at_ifQueueHd
, aa_link
) {
659 if ((ifID
->ifThisNode
.s_net
== dst_net
) || (dst_net
== 0))
660 /* the message is either going out (i) on the same
661 * NETWORK in case of LocalTalk, or (ii) on the same
662 * CABLE in case of Extended AppleTalk (EtherTalk).
666 if ((ifID
->ifThisCableStart
<= dst_net
) &&
667 (ifID
->ifThisCableEnd
>= dst_net
)
669 /* We're on EtherTalk and the message is going out to
670 * some other network on the same cable.
674 if (ARouterIf
== NULL
&& ATALK_VALUE(ifID
->ifARouter
))
677 dPrintf(D_M_DDP_LOW
, D_L_USR1
,
678 ("ddp_output: after search ifid:0x%x %s ifID_home:0x%x\n",
679 (u_int
)ifID
, ifID
? ifID
->ifName
: "",
683 /* located the interface where the packet should
684 * go.... the "first-hop" destination address
685 * must be the same as real destination address.
687 addr_type
= DIRECT_ADDR
;
689 /* no, the destination network number does
690 * not match known network numbers. If we have
691 * heard from this network recently, BRT table
692 * may have address of a router we could use!
694 if (!MULTIPORT_MODE
) {
695 BRT_LOOK (brt
, dst_net
);
697 /* Bingo... BRT has an entry for this network.
698 * Use the link address as is.
700 dPrintf(D_M_DDP
, D_L_VERBOSE
,
701 ("Found BRT entry to send to net 0x%x", dst_net
));
702 at_ddp_stats
.xmit_BRT_used
++;
703 addr_type
= BRT_ENTRY
;
706 /* No BRT entry available for dest network... do we
707 * know of any router at all??
709 if ((ifID
= ARouterIf
) != NULL
)
710 addr_type
= BRIDGE_ADDR
;
712 dPrintf(D_M_DDP
, D_L_WARNING
,
713 ("Found no interface to send pkt"));
714 at_ddp_stats
.xmit_bad_addr
++;
717 goto exit_ddp_output
;
721 else { /* We are in multiport mode, so we can bypass all the rest
722 * and directly ask for the routing of the packet
724 at_ddp_stats
.xmit_BRT_used
++;
727 if (!src_addr_included
) {
728 ddp
->src_node
= ifID
->ifThisNode
.s_node
;
729 NET_ASSIGN(ddp
->src_net
, ifID
->ifThisNode
.s_net
);
731 ddp
->src_socket
= src_socket
;
732 routing_needed(*mp
, ifID
, TRUE
);
734 goto exit_ddp_output
;
737 /* by the time we land here, we know the interface on
738 * which this packet is going out.... ifID.
740 if (ifID
->ifState
== LAP_OFFLINE
) {
742 goto exit_ddp_output
;
748 at_dest.atalk_unused = 0;
750 NET_ASSIGN(at_dest
.atalk_net
, dst_net
);
751 at_dest
.atalk_node
= ddp
->dst_node
;
753 addr
= (char *)&at_dest
;
757 addr
= (char *)&brt
->et_addr
;
760 NET_ASSIGN(at_dest
.atalk_net
, ifID
->ifARouter
.s_net
);
761 at_dest
.atalk_node
= ifID
->ifARouter
.s_node
;
763 addr
= (char *)&at_dest
;
767 /* Irrespective of the interface on which
768 * the packet is going out, we always put the
769 * same source address on the packet (unless multihoming mode).
771 if (MULTIHOME_MODE
) {
772 if (!src_addr_included
) {
773 ddp
->src_node
= ifID
->ifThisNode
.s_node
;
774 NET_ASSIGN(ddp
->src_net
, ifID
->ifThisNode
.s_net
);
778 ddp
->src_node
= ifID_home
->ifThisNode
.s_node
;
779 NET_ASSIGN(ddp
->src_net
, ifID_home
->ifThisNode
.s_net
);
781 ddp
->src_socket
= src_socket
;
783 dPrintf(D_M_DDP_LOW
, D_L_OUTPUT
,
784 ("ddp_output: going out to %d:%d skt%d on %s\n",
785 dst_net
, ddp
->dst_node
, ddp
->dst_socket
, ifID
->ifName
));
787 fillin_pkt_chain(*mp
);
790 struct etalk_addr dest_addr
;
791 struct atalk_addr dest_at_addr
;
792 int loop
= TRUE
; /* flag to aarp to loopback (default) */
796 /* the incoming frame is of the form {flag, address, ddp...}
797 * where "flag" indicates whether the address is an 802.3
798 * (link) address, or an appletalk address. If it's an
799 * 802.3 address, the packet can just go out to the network
800 * through PAT, if it's an appletalk address, AT->802.3 address
801 * resolution needs to be done.
802 * If 802.3 address is known, strip off the flag and 802.3
803 * address, and prepend 802.2 and 802.3 headers.
807 addr_flag
= *(u_char
*)gbuf_rptr(m
);
812 case AT_ADDR_NO_LOOP
:
817 dest_at_addr
= *(struct atalk_addr
*)gbuf_rptr(m
);
818 gbuf_rinc(m
,sizeof(struct atalk_addr
));
820 dest_at_addr
= *(struct atalk_addr
*)addr
;
824 dest_addr
= *(struct etalk_addr
*)gbuf_rptr(m
);
825 gbuf_rinc(m
,sizeof(struct etalk_addr
));
827 dest_addr
= *(struct etalk_addr
*)addr
;
830 dPrintf(D_M_DDP_LOW
,D_L_ERROR
,
831 ("ddp_output: Unknown addr_flag = 0x%x\n", addr_flag
));
832 gbuf_freel(m
); /* unknown address type, chuck it */
833 goto exit_ddp_output
;
838 /* At this point, rptr points to ddp header for sure */
839 if (ifID
->ifState
== LAP_ONLINE_FOR_ZIP
) {
840 /* see if this is a ZIP packet that we need
841 * to let through even though network is
844 if (zip_type_packet(m
) == 0) {
846 goto exit_ddp_output
;
850 ifID
->stats
.xmit_packets
++;
851 ifID
->stats
.xmit_bytes
+= gbuf_msgsize(m
);
852 snmpStats
.dd_outLong
++;
855 case AT_ADDR_NO_LOOP
:
858 * we don't want elap to be looking into ddp header, so
859 * it doesn't know net#, consequently can't do
860 * AMT_LOOKUP. That task left to aarp now.
862 aarp_send_data(m
,ifID
,&dest_at_addr
, loop
);
865 pat_output(ifID
, m
, &dest_addr
, 0);
870 KERNEL_DEBUG(DBG_AT_DDP_OUTPUT
| DBG_FUNC_END
, 0,
875 void ddp_input(mp
, ifID
)
877 register at_ifaddr_t
*ifID
;
879 register at_ddp_t
*ddp
; /* DDP header */
880 register int msgsize
;
881 register at_socket socket
;
883 register at_net_al dst_net
;
885 KERNEL_DEBUG(DBG_AT_DDP_INPUT
| DBG_FUNC_START
, 0,
886 ifID
, mp
, gbuf_len(mp
),0);
888 /* Makes sure we know the default interface before starting to
889 * accept incomming packets. If we don't we may end up with a
890 * null ifID_table[0] and have impredicable results (specially
891 * in router mode. This is a transitory state (because we can
892 * begin to receive packet while we're not completly set up yet.
895 if (ifID_home
== (at_ifaddr_t
*)NULL
) {
896 dPrintf(D_M_DDP
, D_L_ERROR
,
897 ("dropped incoming packet ifID_home not set yet\n"));
899 goto out
; /* return */
903 * if a DDP packet has been broadcast, we're going to get a copy of
904 * it here; if it originated at user level via a write on a DDP
905 * socket; when it gets here, the first block in the chain will be
906 * empty since it only contained the lap level header which will be
907 * stripped in the lap level immediately below ddp
910 if ((mp
= (gbuf_t
*)ddp_compress_msg(mp
)) == NULL
) {
911 dPrintf(D_M_DDP
, D_L_ERROR
,
912 ("dropped short incoming ET packet (len %d)", 0));
913 snmpStats
.dd_inTotal
++;
914 at_ddp_stats
.rcv_bad_length
++;
915 goto out
; /* return; */
917 msgsize
= gbuf_msgsize(mp
);
919 at_ddp_stats
.rcv_bytes
+= msgsize
;
920 at_ddp_stats
.rcv_packets
++;
922 /* if the interface pointer is 0, the packet has been
923 * looped back by 'write' half of DDP. It is of the
924 * form {extended ddp,...}. The packet is meant to go
925 * up to some socket on the same node.
927 if (!ifID
) /* if loop back is specified */
928 ifID
= ifID_home
; /* that means the home port */
930 /* the incoming datagram has extended DDP header and is of
931 * the form {ddp,...}.
933 if (msgsize
< DDP_X_HDR_SIZE
) {
934 dPrintf(D_M_DDP
, D_L_ERROR
,
935 ("dropped short incoming ET packet (len %d)", msgsize
));
936 at_ddp_stats
.rcv_bad_length
++;
938 goto out
; /* return; */
941 * At this point, the message is always of the form
942 * {extended ddp, ... }.
944 ddp
= (at_ddp_t
*)gbuf_rptr(mp
);
947 if (msgsize
!= len
) {
948 if ((unsigned) msgsize
> len
) {
949 if (len
< DDP_X_HDR_SIZE
) {
950 dPrintf(D_M_DDP
, D_L_ERROR
,
951 ("Length problems, ddp length %d, buffer length %d",
953 snmpStats
.dd_tooLong
++;
954 at_ddp_stats
.rcv_bad_length
++;
956 goto out
; /* return; */
959 * shave off the extra bytes from the end of message
961 mp
= ddp_adjmsg(mp
, -(msgsize
- len
)) ? mp
: 0;
963 goto out
; /* return; */
965 dPrintf(D_M_DDP
, D_L_ERROR
,
966 ("Length problems, ddp length %d, buffer length %d",
968 snmpStats
.dd_tooShort
++;
969 at_ddp_stats
.rcv_bad_length
++;
971 goto out
; /* return; */
974 socket
= ddp
->dst_socket
;
977 * We want everything in router mode, specially socket 254 for nbp so we need
978 * to bypass this test when we are a router.
981 if (!MULTIPORT_MODE
&& (socket
> DDP_SOCKET_LAST
||
982 socket
< DDP_SOCKET_1st_RESERVED
)) {
983 dPrintf(D_M_DDP
, D_L_WARNING
,
984 ("Bad dst socket on incoming packet (0x%x)",
986 at_ddp_stats
.rcv_bad_socket
++;
988 goto out
; /* return; */
991 * if the checksum is true, then upstream wants us to calc
993 if (UAS_VALUE(ddp
->checksum
) &&
994 (UAS_VALUE(ddp
->checksum
) != ddp_checksum(mp
, 4))) {
995 dPrintf(D_M_DDP
, D_L_WARNING
,
996 ("Checksum error on incoming pkt, calc 0x%x, exp 0x%x",
997 ddp_checksum(mp
, 4), UAS_VALUE(ddp
->checksum
)));
998 snmpStats
.dd_checkSum
++;
999 at_ddp_stats
.rcv_bad_checksum
++;
1001 goto out
; /* return; */
1004 /*############### routing input checking */
1006 /* Router mode special: we send "up-stack" packets for this node or coming from any
1007 * other ports, but for the reserved atalk sockets (RTMP, ZIP, NBP [and EP])
1008 * BTW, the way we know it's for the router and not the home port is that the
1009 * MAC (ethernet) address is always the one of the interface we're on, but
1010 * the AppleTalk address must be the one of the home port. If it's a multicast
1011 * or another AppleTalk address, this is the router job's to figure out where it's
1014 /* *** a duplicate should be sent to any other client that is listening
1015 for packets of this type on a raw DDP socket *** */
1016 if (ddp_handler
[socket
].func
) {
1017 dPrintf(D_M_DDP
,D_L_INPUT
,
1018 ("ddp_input: skt %d hdnlr:0x%x\n",
1019 (u_int
) socket
, ddp_handler
[socket
].func
));
1021 snmpStats
.dd_inLocal
++;
1023 (*ddp_handler
[socket
].func
)(mp
, ifID
);
1024 goto out
; /* return; */
1026 dst_net
= NET_VALUE(ddp
->dst_net
);
1030 /* any node, wildcard or matching net */
1031 ((ddp
->dst_node
== 255) &&
1032 (((dst_net
>= ifID_home
->ifThisCableStart
) &&
1033 (dst_net
<= ifID_home
->ifThisCableEnd
)) ||
1035 /* this node is not online yet(?) */
1036 (ifID
->ifRoutingState
< PORT_ONLINE
)
1040 snmpStats
.dd_inLocal
++;
1042 if (ddp
->type
== DDP_ATP
) {
1043 if (atp_inputQ
[socket
] && (atp_inputQ
[socket
] != (gref_t
*)1)) {
1044 /* if there's an ATP pcb */
1046 goto out
; /* return; */
1048 } else if (ddp
->type
== DDP_ADSP
) {
1049 if (adsp_inputQ
[socket
]) {
1050 /* if there's an ADSP pcb */
1052 goto out
; /* return; */
1056 /* otherwise look for a DDP pcb;
1057 ATP / raw-DDP and ADSP / raw-DDP are possible */
1058 for (gref
= ddp_head
.atpcb_next
; gref
!= &ddp_head
;
1059 gref
= gref
->atpcb_next
)
1060 if (gref
->lport
== socket
&&
1061 (gref
->ddptype
== 0 || gref
->ddptype
== ddp
->type
)) {
1062 dPrintf(D_M_DDP
, D_L_INPUT
,
1063 ("ddp_input: streamq, skt %d\n", socket
));
1064 if (gref
->atpcb_socket
) {
1065 struct sockaddr_at ddp_in
;
1066 ddp_in
.sat_len
= sizeof(ddp_in
);
1067 ddp_in
.sat_family
= AF_APPLETALK
;
1068 ddp_in
.sat_addr
.s_net
= NET_VALUE(ddp
->src_net
);
1069 ddp_in
.sat_addr
.s_node
= ddp
->src_node
;
1070 ddp_in
.sat_port
= ddp
->src_socket
;
1072 /* strip off DDP header if so indicated by
1074 if (gref
->ddp_flags
& DDPFLG_STRIPHDR
) {
1075 mp
= m_pullup((struct mbuf
*)mp
,
1078 gbuf_rinc(mp
, DDP_X_HDR_SIZE
);
1080 /* this should never happen because
1081 msgsize was checked earlier */
1082 at_ddp_stats
.rcv_bad_length
++;
1083 goto out
; /* return */
1087 if (sbappendaddr(&((gref
->atpcb_socket
)->so_rcv
),
1088 (struct sockaddr
*)&ddp_in
,
1089 mp
, 0, NULL
) != 0) {
1090 sorwakeup(gref
->atpcb_socket
);
1093 atalk_putnext(gref
, mp
);
1095 goto out
; /* return */
1098 at_ddp_stats
.rcv_bad_socket
++;
1100 snmpStats
.dd_noHandler
++;
1101 dPrintf(D_M_DDP
, D_L_WARNING
,
1102 ("ddp_input: dropped pkt for socket %d\n", socket
));
1104 dPrintf(D_M_DDP
, D_L_ROUTING
,
1105 ("ddp_input: routing_needed from port=%d sock=%d\n",
1106 ifID
->ifPort
, ddp
->dst_socket
));
1108 snmpStats
.dd_fwdReq
++;
1109 if (((pktsIn
-pktsHome
+200) >= RouterMix
) && ((++pktsDropped
% 5) == 0)) {
1110 at_ddp_stats
.rcv_dropped_nobuf
++;
1114 routing_needed(mp
, ifID
, FALSE
);
1118 KERNEL_DEBUG(DBG_AT_DDP_INPUT
| DBG_FUNC_END
, 0,0,0,0,0);
1123 * ddp_router_output()
1126 * This is a modified version of ddp_output for router use.
1127 * The main difference is that the interface on which the packet needs
1128 * to be sent is specified and a *destination* AppleTalk address is passed
1129 * as an argument, this address may or may not be the same as the destination
1130 * address found in the ddp packet... This is the trick about routing, the
1131 * AppleTalk destination of the packet may not be the same as the Enet address
1132 * we send the packet too (ie, we may pass the baby to another router).
1135 int ddp_router_output(mp
, ifID
, addr_type
, router_net
, router_node
, enet_addr
)
1139 at_net_al router_net
;
1140 at_node router_node
;
1141 etalk_addr_t
*enet_addr
;
1143 register at_ddp_t
*ddp
;
1144 struct atalk_addr at_dest
;
1150 dPrintf(D_M_DDP
, D_L_WARNING
, ("BAD BAD ifID\n"));
1154 ddp
= (at_ddp_t
*)gbuf_rptr(mp
);
1156 if (ifID
->ifFlags
& AT_IFF_AURP
) { /* AURP link? */
1157 if (ddp_AURPsendx
) {
1158 fillin_pkt_chain(mp
);
1159 if (router_node
== 255)
1161 ddp_AURPsendx(AURPCODE_DATAPKT
, mp
, router_node
);
1169 /* keep some of the tests for now ####### */
1171 if (gbuf_msgsize(mp
) > DDP_DATAGRAM_SIZE
) {
1172 /* the packet is too large */
1173 dPrintf(D_M_DDP
, D_L_WARNING
,
1174 ("ddp_router_output: Packet too large size=%d\n",
1180 switch (addr_type
) {
1185 * Check for packet destined to the home stack
1188 if ((ddp
->dst_node
== ifID
->ifThisNode
.s_node
) &&
1189 (NET_VALUE(ddp
->dst_net
) == ifID
->ifThisNode
.s_net
)) {
1190 dPrintf(D_M_DDP_LOW
, D_L_ROUTING
,
1191 ("ddp_r_output: sending back home from port=%d socket=%d\n",
1192 ifID
->ifPort
, ddp
->dst_socket
));
1194 UAS_ASSIGN(ddp
->checksum
, 0);
1195 ddp_input(mp
, ifID
);
1199 NET_ASSIGN(at_dest
.atalk_net
, router_net
);
1200 at_dest
.atalk_node
= router_node
;
1202 addr_flag
= AT_ADDR_NO_LOOP
;
1203 addr
= (char *)&at_dest
;
1204 dPrintf(D_M_DDP_LOW
, D_L_ROUTING_AT
,
1205 ("ddp_r_output: AT_ADDR out port=%d net %d:%d via rte %d:%d",
1206 ifID
->ifPort
, NET_VALUE(ddp
->dst_net
), ddp
->dst_node
, router_net
,
1211 addr_flag
= ET_ADDR
;
1212 addr
= (char *)enet_addr
;
1213 dPrintf(D_M_DDP_LOW
, D_L_ROUTING
,
1214 ("ddp_r_output: ET_ADDR out port=%d net %d:%d\n",
1215 ifID
->ifPort
, NET_VALUE(ddp
->dst_net
), ddp
->dst_node
));
1219 if (ifID
->ifState
== LAP_OFFLINE
) {
1224 fillin_pkt_chain(mp
);
1227 struct etalk_addr dest_addr
;
1228 struct atalk_addr dest_at_addr
;
1229 int loop
= TRUE
; /* flag to aarp to loopback (default) */
1233 /* the incoming frame is of the form {flag, address, ddp...}
1234 * where "flag" indicates whether the address is an 802.3
1235 * (link) address, or an appletalk address. If it's an
1236 * 802.3 address, the packet can just go out to the network
1237 * through PAT, if it's an appletalk address, AT->802.3 address
1238 * resolution needs to be done.
1239 * If 802.3 address is known, strip off the flag and 802.3
1240 * address, and prepend 802.2 and 802.3 headers.
1244 addr_flag
= *(u_char
*)gbuf_rptr(m
);
1248 switch (addr_flag
) {
1249 case AT_ADDR_NO_LOOP
:
1254 dest_at_addr
= *(struct atalk_addr
*)gbuf_rptr(m
);
1255 gbuf_rinc(m
,sizeof(struct atalk_addr
));
1257 dest_at_addr
= *(struct atalk_addr
*)addr
;
1261 dest_addr
= *(struct etalk_addr
*)gbuf_rptr(m
);
1262 gbuf_rinc(m
,sizeof(struct etalk_addr
));
1264 dest_addr
= *(struct etalk_addr
*)addr
;
1267 dPrintf(D_M_DDP_LOW
,D_L_ERROR
,
1268 ("ddp_router_output: Unknown addr_flag = 0x%x\n", addr_flag
));
1270 gbuf_freel(m
); /* unknown address type, chuck it */
1276 /* At this point, rptr points to ddp header for sure */
1277 if (ifID
->ifState
== LAP_ONLINE_FOR_ZIP
) {
1278 /* see if this is a ZIP packet that we need
1279 * to let through even though network is
1282 if (zip_type_packet(m
) == 0) {
1288 ifID
->stats
.xmit_packets
++;
1289 ifID
->stats
.xmit_bytes
+= gbuf_msgsize(m
);
1290 snmpStats
.dd_outLong
++;
1292 switch (addr_flag
) {
1293 case AT_ADDR_NO_LOOP
:
1296 * we don't want elap to be looking into ddp header, so
1297 * it doesn't know net#, consequently can't do
1298 * AMT_LOOKUP. That task left to aarp now.
1300 aarp_send_data(m
,ifID
,&dest_at_addr
, loop
);
1303 pat_output(ifID
, m
, &dest_addr
, 0);
1309 } /* ddp_router_output */
1311 /*****************************************/
1313 void rt_delete(NetStop
, NetStart
)
1314 unsigned short NetStop
;
1315 unsigned short NetStart
;
1320 ATDISABLE(s
, ddpinp_lock
);
1321 if ((found
= rt_bdelete(NetStop
, NetStart
)) != 0) {
1322 bzero(found
, sizeof(RT_entry
));
1323 found
->right
= RT_table_freelist
;
1324 RT_table_freelist
= found
;
1326 ATENABLE(s
, ddpinp_lock
);
1329 int ddp_AURPfuncx(code
, param
, node
)
1334 extern void rtmp_timeout();
1335 extern void rtmp_send_port();
1340 case AURPCODE_DATAPKT
: /* data packet */
1342 dPrintf(D_M_DDP
, D_L_TRACE
, ("ddp_AURPfuncx: data, 0x%x, %d\n",
1343 (u_int
) aurp_ifID
, node
));
1345 ddp_input((gbuf_t
*)param
, aurp_ifID
);
1347 gbuf_freem((gbuf_t
*)param
);
1350 case AURPCODE_REG
: /* register/deregister */
1353 ddp_AURPsendx
= (void(*)())param
;
1356 /* register AURP callback function */
1359 for (k
=(IFID_HOME
+1); k
< IF_TOTAL_MAX
; k
++) {
1360 if (ifID_table
[k
] == 0) {
1361 aurp_ifID
= &at_interfaces
[k
];
1362 aurp_ifID
->ifFlags
= RTR_XNET_PORT
;
1363 ddp_add_if(aurp_ifID
);
1364 aurp_ifID
->ifState
= LAP_ONLINE
;
1365 aurp_ifID
->ifRoutingState
= PORT_ONLINE
;
1366 dPrintf(D_M_DDP
, D_L_TRACE
,
1367 ("ddp_AURPfuncx: on, 0x%x\n",
1368 (u_int
) aurp_ifID
));
1370 ddp_AURPsendx(AURPCODE_DEBUGINFO
,
1371 &dbgBits
, aurp_ifID
->ifPort
);
1378 /* deregister AURP callback function */
1380 rtmp_purge(aurp_ifID
);
1381 ddp_rem_if(aurp_ifID
);
1382 aurp_ifID
->ifState
= LAP_OFFLINE
;
1383 aurp_ifID
->ifRoutingState
= PORT_OFFLINE
;
1384 dPrintf(D_M_DDP
, D_L_TRACE
,
1385 ("ddp_AURPfuncx: off, 0x%x\n", (u_int
) aurp_ifID
));
1391 case AURPCODE_AURPPROTO
: /* proto type - AURP */
1393 aurp_ifID
->ifFlags
|= AT_IFF_AURP
;
1402 /* checks to see if address of packet is for one of our interfaces
1403 returns *ifID if it's for us, NULL if not
1405 at_ifaddr_t
*forUs(ddp
)
1406 register at_ddp_t
*ddp
;
1410 TAILQ_FOREACH(ifID
, &at_ifQueueHd
, aa_link
) {
1411 if ((ddp
->dst_node
== ifID
->ifThisNode
.s_node
) &&
1412 (NET_VALUE(ddp
->dst_net
) == ifID
->ifThisNode
.s_net
)
1414 dPrintf(D_M_DDP_LOW
, D_L_ROUTING
,
1415 ("pkt was for port %d\n", ifID
->ifPort
));
1421 return((at_ifaddr_t
*)NULL
);