Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / netproto / atalk / ddp_output.c
1 /*
2  * Copyright (c) 1990,1991 Regents of The University of Michigan.
3  * All Rights Reserved.
4  *
5  * Permission to use, copy, modify, and distribute this software and
6  * its documentation for any purpose and without fee is hereby granted,
7  * provided that the above copyright notice appears in all copies and
8  * that both that copyright notice and this permission notice appear
9  * in supporting documentation, and that the name of The University
10  * of Michigan not be used in advertising or publicity pertaining to
11  * distribution of the software without specific, written prior
12  * permission. This software is supplied as is without expressed or
13  * implied warranties of any kind.
14  *
15  *      Research Systems Unix Group
16  *      The University of Michigan
17  *      c/o Mike Clark
18  *      535 W. William Street
19  *      Ann Arbor, Michigan
20  *      +1-313-763-0525
21  *      netatalk@itd.umich.edu
22  */
23
24 /* $FreeBSD: src/sys/netatalk/ddp_output.c,v 1.13.6.1 2000/06/02 22:39:07 archie Exp $ */
25
26 #include <sys/param.h>
27 #include <sys/systm.h>
28 #include <sys/mbuf.h>
29 #include <sys/socket.h>
30 #include <sys/socketvar.h>
31
32 #include <net/if.h>
33 #include <net/route.h>
34
35 #undef s_net
36
37 #include <netatalk/at.h>
38 #include <netatalk/at_var.h>
39 #include <netatalk/ddp.h>
40 #include <netatalk/ddp_var.h>
41 #include <netatalk/at_extern.h>
42
43 int     ddp_cksum = 1;
44
45 int
46 ddp_output( struct mbuf *m, struct socket *so)
47 {
48     struct ddpehdr      *deh;
49     struct ddpcb *ddp = sotoddpcb( so );
50
51     M_PREPEND( m, sizeof( struct ddpehdr ), M_WAIT );
52
53     deh = mtod( m, struct ddpehdr *);
54     deh->deh_pad = 0;
55     deh->deh_hops = 0;
56
57     deh->deh_len = m->m_pkthdr.len;
58
59     deh->deh_dnet = ddp->ddp_fsat.sat_addr.s_net;
60     deh->deh_dnode = ddp->ddp_fsat.sat_addr.s_node;
61     deh->deh_dport = ddp->ddp_fsat.sat_port;
62     deh->deh_snet = ddp->ddp_lsat.sat_addr.s_net;
63     deh->deh_snode = ddp->ddp_lsat.sat_addr.s_node;
64     deh->deh_sport = ddp->ddp_lsat.sat_port;
65
66     /*
67      * The checksum calculation is done after all of the other bytes have
68      * been filled in.
69      */
70     if ( ddp_cksum ) {
71         deh->deh_sum = at_cksum( m, sizeof( int ));
72     } else {
73         deh->deh_sum = 0;
74     }
75     deh->deh_bytes = htonl( deh->deh_bytes );
76
77 #ifdef NETATALK_DEBUG
78     printf ("ddp_output: from %d.%d:%d to %d.%d:%d\n",
79         ntohs(deh->deh_snet), deh->deh_snode, deh->deh_sport,
80         ntohs(deh->deh_dnet), deh->deh_dnode, deh->deh_dport);
81 #endif
82     return( ddp_route( m, &ddp->ddp_route ));
83 }
84
85 u_short
86 at_cksum( struct mbuf *m, int skip)
87 {
88     u_char      *data, *end;
89     u_long      cksum = 0;
90
91     for (; m; m = m->m_next ) {
92         for ( data = mtod( m, u_char * ), end = data + m->m_len; data < end;
93                 data++ ) {
94             if ( skip ) {
95                 skip--;
96                 continue;
97             }
98             cksum = ( cksum + *data ) << 1;
99             if ( cksum & 0x00010000 ) {
100                 cksum++;
101             }
102             cksum &= 0x0000ffff;
103         }
104     }
105
106     if ( cksum == 0 ) {
107         cksum = 0x0000ffff;
108     }
109     return( (u_short)cksum );
110 }
111
112 int
113 ddp_route( struct mbuf *m, struct route *ro)
114 {
115     struct sockaddr_at  gate;
116     struct elaphdr      *elh;
117     struct mbuf         *m0;
118     struct at_ifaddr    *aa = NULL;
119     struct ifnet        *ifp = NULL;
120     u_short             net;
121
122 #if 0
123     /* Check for net zero, node zero ("myself") */
124     if (satosat(&ro->ro_dst)->sat_addr.s_net == ATADDR_ANYNET
125         && satosat(&ro->ro_dst)->sat_addr.s_node == ATADDR_ANYNODE) {
126             /* Find the loopback interface */
127     }
128 #endif
129
130     /*
131      * if we have a route, find the ifa that refers to this route.
132      * I.e The ifa used to get to the gateway.
133      */
134     if ( (ro->ro_rt == NULL)
135     || ( ro->ro_rt->rt_ifa == NULL )
136     || ( (ifp = ro->ro_rt->rt_ifa->ifa_ifp) == NULL )) {
137         rtalloc(ro);
138     }
139     if ( (ro->ro_rt != NULL)
140     && ( ro->ro_rt->rt_ifa )
141     && ( ifp = ro->ro_rt->rt_ifa->ifa_ifp )) {
142         net = ntohs(satosat(ro->ro_rt->rt_gateway)->sat_addr.s_net);
143         for ( aa = at_ifaddr; aa; aa = aa->aa_next ) {
144             if (((net == 0) || (aa->aa_ifp == ifp)) &&
145                     net >= ntohs( aa->aa_firstnet ) &&
146                     net <= ntohs( aa->aa_lastnet )) {
147                 break;
148             }
149         }
150     } else {
151         m_freem( m );
152 #ifdef NETATALK_DEBUG
153         if (ro->ro_rt == NULL)
154             printf ("ddp_route: no ro_rt.\n");
155         else if (ro->ro_rt->rt_ifa == NULL)
156             printf ("ddp_route: no ro_rt->rt_ifa\n");
157         else
158             printf ("ddp_route: no ro_rt->rt_ifa->ifa_ifp\n");
159 #endif
160         return( ENETUNREACH );
161     }
162
163     if ( aa == NULL ) {
164 #ifdef NETATALK_DEBUG
165         printf( "ddp_route: no atalk address found for %s%d\n", 
166             ifp->if_name, ifp->if_unit);
167 #endif
168         m_freem( m );
169         return( ENETUNREACH );
170     }
171
172     /*
173      * if the destination address is on a directly attached node use that,
174      * else use the official gateway.
175      */
176     if ( ntohs( satosat( &ro->ro_dst )->sat_addr.s_net ) >=
177             ntohs( aa->aa_firstnet ) &&
178             ntohs( satosat( &ro->ro_dst )->sat_addr.s_net ) <=
179             ntohs( aa->aa_lastnet )) {
180         gate = *satosat( &ro->ro_dst );
181     } else {
182         gate = *satosat( ro->ro_rt->rt_gateway );
183     }
184
185     /*
186      * There are several places in the kernel where data is added to
187      * an mbuf without ensuring that the mbuf pointer is aligned.
188      * This is bad for transition routing, since phase 1 and phase 2
189      * packets end up poorly aligned due to the three byte elap header.
190      */
191     if ( !(aa->aa_flags & AFA_PHASE2) ) {
192         MGET( m0, M_WAIT, MT_HEADER );
193         if ( m0 == 0 ) {
194             m_freem( m );
195             printf("ddp_route: no buffers\n");
196             return( ENOBUFS );
197         }
198         m0->m_next = m;
199         /* XXX perhaps we ought to align the header? */
200         m0->m_len = SZ_ELAPHDR;
201         m = m0;
202
203         elh = mtod( m, struct elaphdr *);
204         elh->el_snode = satosat( &aa->aa_addr )->sat_addr.s_node;
205         elh->el_type = ELAP_DDPEXTEND;
206         elh->el_dnode = gate.sat_addr.s_node;
207     }
208     ro->ro_rt->rt_use++;
209
210 #ifdef NETATALK_DEBUG
211     printf ("ddp_route: from %d.%d to %d.%d, via %d.%d (%s%d)\n",
212         ntohs(satosat(&aa->aa_addr)->sat_addr.s_net),
213         satosat(&aa->aa_addr)->sat_addr.s_node,
214         ntohs(satosat(&ro->ro_dst)->sat_addr.s_net),
215         satosat(&ro->ro_dst)->sat_addr.s_node,
216         ntohs(gate.sat_addr.s_net),
217         gate.sat_addr.s_node,
218         ifp->if_name, ifp->if_unit);
219 #endif
220
221     /* short-circuit the output if we're sending this to ourself */
222     if ((satosat(&aa->aa_addr)->sat_addr.s_net  == satosat(&ro->ro_dst)->sat_addr.s_net) &&
223         (satosat(&aa->aa_addr)->sat_addr.s_node == satosat(&ro->ro_dst)->sat_addr.s_node))
224     {
225         return (if_simloop(ifp, m, gate.sat_family, 0));
226     }
227
228     return((*ifp->if_output)( ifp,
229         m, (struct sockaddr *)&gate, NULL)); /* XXX */
230 }