]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netatalk/ddp_output.c
Synchronize perforce Id tags
[FreeBSD/FreeBSD.git] / sys / netatalk / 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$ */
25
26 #include "opt_mac.h"
27
28 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/mac.h>
31 #include <sys/mbuf.h>
32 #include <sys/socket.h>
33 #include <sys/socketvar.h>
34
35 #include <net/if.h>
36 #include <net/route.h>
37
38 #undef s_net
39
40 #include <netatalk/at.h>
41 #include <netatalk/at_var.h>
42 #include <netatalk/ddp.h>
43 #include <netatalk/ddp_var.h>
44 #include <netatalk/at_extern.h>
45
46 int     ddp_cksum = 1;
47
48 int
49 ddp_output( struct mbuf *m, struct socket *so)
50 {
51     struct ddpehdr      *deh;
52     struct ddpcb *ddp = sotoddpcb( so );
53
54 #ifdef MAC
55     mac_create_mbuf_from_socket(so, m);
56 #endif
57
58     M_PREPEND( m, sizeof( struct ddpehdr ), M_TRYWAIT );
59
60     deh = mtod( m, struct ddpehdr *);
61     deh->deh_pad = 0;
62     deh->deh_hops = 0;
63
64     deh->deh_len = m->m_pkthdr.len;
65
66     deh->deh_dnet = ddp->ddp_fsat.sat_addr.s_net;
67     deh->deh_dnode = ddp->ddp_fsat.sat_addr.s_node;
68     deh->deh_dport = ddp->ddp_fsat.sat_port;
69     deh->deh_snet = ddp->ddp_lsat.sat_addr.s_net;
70     deh->deh_snode = ddp->ddp_lsat.sat_addr.s_node;
71     deh->deh_sport = ddp->ddp_lsat.sat_port;
72
73     /*
74      * The checksum calculation is done after all of the other bytes have
75      * been filled in.
76      */
77     if ( ddp_cksum ) {
78         deh->deh_sum = at_cksum( m, sizeof( int ));
79     } else {
80         deh->deh_sum = 0;
81     }
82     deh->deh_bytes = htonl( deh->deh_bytes );
83
84 #ifdef NETATALK_DEBUG
85     printf ("ddp_output: from %d.%d:%d to %d.%d:%d\n",
86         ntohs(deh->deh_snet), deh->deh_snode, deh->deh_sport,
87         ntohs(deh->deh_dnet), deh->deh_dnode, deh->deh_dport);
88 #endif
89     return( ddp_route( m, &ddp->ddp_route ));
90 }
91
92 u_short
93 at_cksum( struct mbuf *m, int skip)
94 {
95     u_char      *data, *end;
96     u_long      cksum = 0;
97
98     for (; m; m = m->m_next ) {
99         for ( data = mtod( m, u_char * ), end = data + m->m_len; data < end;
100                 data++ ) {
101             if ( skip ) {
102                 skip--;
103                 continue;
104             }
105             cksum = ( cksum + *data ) << 1;
106             if ( cksum & 0x00010000 ) {
107                 cksum++;
108             }
109             cksum &= 0x0000ffff;
110         }
111     }
112
113     if ( cksum == 0 ) {
114         cksum = 0x0000ffff;
115     }
116     return( (u_short)cksum );
117 }
118
119 int
120 ddp_route( struct mbuf *m, struct route *ro)
121 {
122     struct sockaddr_at  gate;
123     struct elaphdr      *elh;
124     struct mbuf         *m0;
125     struct at_ifaddr    *aa = NULL;
126     struct ifnet        *ifp = NULL;
127     u_short             net;
128
129 #if 0
130     /* Check for net zero, node zero ("myself") */
131     if (satosat(&ro->ro_dst)->sat_addr.s_net == ATADDR_ANYNET
132         && satosat(&ro->ro_dst)->sat_addr.s_node == ATADDR_ANYNODE) {
133             /* Find the loopback interface */
134     }
135 #endif
136
137     /*
138      * if we have a route, find the ifa that refers to this route.
139      * I.e The ifa used to get to the gateway.
140      */
141     if ( (ro->ro_rt == NULL)
142     || ( ro->ro_rt->rt_ifa == NULL )
143     || ( (ifp = ro->ro_rt->rt_ifa->ifa_ifp) == NULL )) {
144         rtalloc(ro);
145     }
146     if ( (ro->ro_rt != NULL)
147     && ( ro->ro_rt->rt_ifa )
148     && ( ifp = ro->ro_rt->rt_ifa->ifa_ifp )) {
149         net = ntohs(satosat(ro->ro_rt->rt_gateway)->sat_addr.s_net);
150         for ( aa = at_ifaddr; aa; aa = aa->aa_next ) {
151             if (((net == 0) || (aa->aa_ifp == ifp)) &&
152                     net >= ntohs( aa->aa_firstnet ) &&
153                     net <= ntohs( aa->aa_lastnet )) {
154                 break;
155             }
156         }
157     } else {
158         m_freem( m );
159 #ifdef NETATALK_DEBUG
160         if (ro->ro_rt == NULL)
161             printf ("ddp_route: no ro_rt.\n");
162         else if (ro->ro_rt->rt_ifa == NULL)
163             printf ("ddp_route: no ro_rt->rt_ifa\n");
164         else
165             printf ("ddp_route: no ro_rt->rt_ifa->ifa_ifp\n");
166 #endif
167         return( ENETUNREACH );
168     }
169
170     if ( aa == NULL ) {
171 #ifdef NETATALK_DEBUG
172         printf( "ddp_route: no atalk address found for %s%d\n", 
173             ifp->if_name, ifp->if_unit);
174 #endif
175         m_freem( m );
176         return( ENETUNREACH );
177     }
178
179     /*
180      * if the destination address is on a directly attached node use that,
181      * else use the official gateway.
182      */
183     if ( ntohs( satosat( &ro->ro_dst )->sat_addr.s_net ) >=
184             ntohs( aa->aa_firstnet ) &&
185             ntohs( satosat( &ro->ro_dst )->sat_addr.s_net ) <=
186             ntohs( aa->aa_lastnet )) {
187         gate = *satosat( &ro->ro_dst );
188     } else {
189         gate = *satosat( ro->ro_rt->rt_gateway );
190     }
191
192     /*
193      * There are several places in the kernel where data is added to
194      * an mbuf without ensuring that the mbuf pointer is aligned.
195      * This is bad for transition routing, since phase 1 and phase 2
196      * packets end up poorly aligned due to the three byte elap header.
197      */
198     if ( !(aa->aa_flags & AFA_PHASE2) ) {
199         MGET( m0, M_TRYWAIT, MT_HEADER );
200         if ( m0 == 0 ) {
201             m_freem( m );
202             printf("ddp_route: no buffers\n");
203             return( ENOBUFS );
204         }
205 #ifdef MAC
206         mac_create_mbuf_from_mbuf(m, m0);
207 #endif
208         m0->m_next = m;
209         /* XXX perhaps we ought to align the header? */
210         m0->m_len = SZ_ELAPHDR;
211         m = m0;
212
213         elh = mtod( m, struct elaphdr *);
214         elh->el_snode = satosat( &aa->aa_addr )->sat_addr.s_node;
215         elh->el_type = ELAP_DDPEXTEND;
216         elh->el_dnode = gate.sat_addr.s_node;
217     }
218     ro->ro_rt->rt_use++;
219
220 #ifdef NETATALK_DEBUG
221     printf ("ddp_route: from %d.%d to %d.%d, via %d.%d (%s%d)\n",
222         ntohs(satosat(&aa->aa_addr)->sat_addr.s_net),
223         satosat(&aa->aa_addr)->sat_addr.s_node,
224         ntohs(satosat(&ro->ro_dst)->sat_addr.s_net),
225         satosat(&ro->ro_dst)->sat_addr.s_node,
226         ntohs(gate.sat_addr.s_net),
227         gate.sat_addr.s_node,
228         ifp->if_name, ifp->if_unit);
229 #endif
230
231     /* short-circuit the output if we're sending this to ourself */
232     if ((satosat(&aa->aa_addr)->sat_addr.s_net  == satosat(&ro->ro_dst)->sat_addr.s_net) &&
233         (satosat(&aa->aa_addr)->sat_addr.s_node == satosat(&ro->ro_dst)->sat_addr.s_node))
234     {
235         return (if_simloop(ifp, m, gate.sat_family, 0));
236     }
237
238     return((*ifp->if_output)( ifp,
239         m, (struct sockaddr *)&gate, NULL)); /* XXX */
240 }