2005-01-07 02:35:34 +00:00
|
|
|
/*-
|
1996-05-24 01:35:45 +00:00
|
|
|
* Copyright (c) 1990,1991 Regents of The University of Michigan.
|
|
|
|
* All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software and
|
|
|
|
* its documentation for any purpose and without fee is hereby granted,
|
|
|
|
* provided that the above copyright notice appears in all copies and
|
|
|
|
* that both that copyright notice and this permission notice appear
|
|
|
|
* in supporting documentation, and that the name of The University
|
|
|
|
* of Michigan not be used in advertising or publicity pertaining to
|
|
|
|
* distribution of the software without specific, written prior
|
|
|
|
* permission. This software is supplied as is without expressed or
|
|
|
|
* implied warranties of any kind.
|
|
|
|
*
|
|
|
|
* Research Systems Unix Group
|
|
|
|
* The University of Michigan
|
|
|
|
* c/o Mike Clark
|
|
|
|
* 535 W. William Street
|
|
|
|
* Ann Arbor, Michigan
|
|
|
|
* +1-313-763-0525
|
|
|
|
* netatalk@itd.umich.edu
|
|
|
|
*/
|
|
|
|
|
2000-05-24 21:16:56 +00:00
|
|
|
/* $FreeBSD$ */
|
|
|
|
|
2002-08-15 18:58:44 +00:00
|
|
|
#include "opt_mac.h"
|
|
|
|
|
1996-05-24 01:35:45 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
2002-08-15 18:58:44 +00:00
|
|
|
#include <sys/mac.h>
|
1996-05-24 01:35:45 +00:00
|
|
|
#include <sys/mbuf.h>
|
|
|
|
#include <sys/socket.h>
|
1997-03-05 09:17:38 +00:00
|
|
|
#include <sys/socketvar.h>
|
1996-05-24 01:35:45 +00:00
|
|
|
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <net/route.h>
|
|
|
|
|
|
|
|
#undef s_net
|
|
|
|
|
1996-09-10 08:32:01 +00:00
|
|
|
#include <netatalk/at.h>
|
|
|
|
#include <netatalk/at_var.h>
|
|
|
|
#include <netatalk/ddp.h>
|
|
|
|
#include <netatalk/ddp_var.h>
|
1996-05-24 01:35:45 +00:00
|
|
|
#include <netatalk/at_extern.h>
|
|
|
|
|
|
|
|
int ddp_cksum = 1;
|
|
|
|
|
|
|
|
int
|
2004-03-22 03:24:10 +00:00
|
|
|
ddp_output(struct mbuf *m, struct socket *so)
|
1996-05-24 01:35:45 +00:00
|
|
|
{
|
|
|
|
struct ddpehdr *deh;
|
2004-03-22 03:24:10 +00:00
|
|
|
struct ddpcb *ddp = sotoddpcb(so);
|
1996-05-24 01:35:45 +00:00
|
|
|
|
2002-08-15 18:58:44 +00:00
|
|
|
#ifdef MAC
|
2004-06-13 02:50:07 +00:00
|
|
|
SOCK_LOCK(so);
|
2002-08-15 18:58:44 +00:00
|
|
|
mac_create_mbuf_from_socket(so, m);
|
2004-06-13 02:50:07 +00:00
|
|
|
SOCK_UNLOCK(so);
|
2002-08-15 18:58:44 +00:00
|
|
|
#endif
|
|
|
|
|
2005-02-22 14:22:09 +00:00
|
|
|
M_PREPEND(m, sizeof(struct ddpehdr), M_DONTWAIT);
|
2004-03-22 03:24:10 +00:00
|
|
|
if (m == NULL)
|
|
|
|
return (ENOBUFS);
|
1996-05-24 01:35:45 +00:00
|
|
|
|
2004-03-22 03:24:10 +00:00
|
|
|
deh = mtod(m, struct ddpehdr *);
|
1996-05-24 01:35:45 +00:00
|
|
|
deh->deh_pad = 0;
|
|
|
|
deh->deh_hops = 0;
|
|
|
|
|
|
|
|
deh->deh_len = m->m_pkthdr.len;
|
|
|
|
|
|
|
|
deh->deh_dnet = ddp->ddp_fsat.sat_addr.s_net;
|
|
|
|
deh->deh_dnode = ddp->ddp_fsat.sat_addr.s_node;
|
|
|
|
deh->deh_dport = ddp->ddp_fsat.sat_port;
|
|
|
|
deh->deh_snet = ddp->ddp_lsat.sat_addr.s_net;
|
|
|
|
deh->deh_snode = ddp->ddp_lsat.sat_addr.s_node;
|
|
|
|
deh->deh_sport = ddp->ddp_lsat.sat_port;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The checksum calculation is done after all of the other bytes have
|
|
|
|
* been filled in.
|
|
|
|
*/
|
2004-03-22 03:24:10 +00:00
|
|
|
if (ddp_cksum) {
|
|
|
|
deh->deh_sum = at_cksum(m, sizeof(int));
|
1996-05-24 01:35:45 +00:00
|
|
|
} else {
|
|
|
|
deh->deh_sum = 0;
|
|
|
|
}
|
2004-03-22 03:24:10 +00:00
|
|
|
deh->deh_bytes = htonl(deh->deh_bytes);
|
1996-05-24 01:35:45 +00:00
|
|
|
|
1998-08-04 23:17:05 +00:00
|
|
|
#ifdef NETATALK_DEBUG
|
|
|
|
printf ("ddp_output: from %d.%d:%d to %d.%d:%d\n",
|
|
|
|
ntohs(deh->deh_snet), deh->deh_snode, deh->deh_sport,
|
|
|
|
ntohs(deh->deh_dnet), deh->deh_dnode, deh->deh_dport);
|
|
|
|
#endif
|
2004-03-22 03:24:10 +00:00
|
|
|
return (ddp_route(m, &ddp->ddp_route));
|
1996-05-24 01:35:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
u_short
|
2004-03-22 03:24:10 +00:00
|
|
|
at_cksum(struct mbuf *m, int skip)
|
1996-05-24 01:35:45 +00:00
|
|
|
{
|
|
|
|
u_char *data, *end;
|
|
|
|
u_long cksum = 0;
|
|
|
|
|
2004-03-22 03:24:10 +00:00
|
|
|
for (; m; m = m->m_next) {
|
|
|
|
for (data = mtod(m, u_char *), end = data + m->m_len; data < end;
|
|
|
|
data++) {
|
|
|
|
if (skip) {
|
1996-05-24 01:35:45 +00:00
|
|
|
skip--;
|
|
|
|
continue;
|
|
|
|
}
|
2004-03-22 03:24:10 +00:00
|
|
|
cksum = (cksum + *data) << 1;
|
|
|
|
if (cksum & 0x00010000) {
|
1996-05-24 01:35:45 +00:00
|
|
|
cksum++;
|
|
|
|
}
|
|
|
|
cksum &= 0x0000ffff;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-03-22 03:24:10 +00:00
|
|
|
if (cksum == 0) {
|
1996-05-24 01:35:45 +00:00
|
|
|
cksum = 0x0000ffff;
|
|
|
|
}
|
2004-03-22 03:24:10 +00:00
|
|
|
return ((u_short)cksum);
|
1996-05-24 01:35:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2004-03-22 03:24:10 +00:00
|
|
|
ddp_route(struct mbuf *m, struct route *ro)
|
1996-05-24 01:35:45 +00:00
|
|
|
{
|
|
|
|
struct sockaddr_at gate;
|
|
|
|
struct elaphdr *elh;
|
|
|
|
struct mbuf *m0;
|
|
|
|
struct at_ifaddr *aa = NULL;
|
|
|
|
struct ifnet *ifp = NULL;
|
|
|
|
u_short net;
|
|
|
|
|
1998-06-12 03:48:19 +00:00
|
|
|
#if 0
|
|
|
|
/* Check for net zero, node zero ("myself") */
|
|
|
|
if (satosat(&ro->ro_dst)->sat_addr.s_net == ATADDR_ANYNET
|
|
|
|
&& satosat(&ro->ro_dst)->sat_addr.s_node == ATADDR_ANYNODE) {
|
|
|
|
/* Find the loopback interface */
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1997-08-28 01:17:12 +00:00
|
|
|
/*
|
|
|
|
* if we have a route, find the ifa that refers to this route.
|
|
|
|
* I.e The ifa used to get to the gateway.
|
|
|
|
*/
|
2004-03-22 03:24:10 +00:00
|
|
|
if ((ro->ro_rt == NULL)
|
|
|
|
|| (ro->ro_rt->rt_ifa == NULL)
|
|
|
|
|| ((ifp = ro->ro_rt->rt_ifa->ifa_ifp) == NULL)) {
|
1997-08-28 01:17:12 +00:00
|
|
|
rtalloc(ro);
|
|
|
|
}
|
2004-03-22 03:24:10 +00:00
|
|
|
if ((ro->ro_rt != NULL)
|
|
|
|
&& (ro->ro_rt->rt_ifa)
|
|
|
|
&& (ifp = ro->ro_rt->rt_ifa->ifa_ifp)) {
|
1997-10-29 00:30:52 +00:00
|
|
|
net = ntohs(satosat(ro->ro_rt->rt_gateway)->sat_addr.s_net);
|
2004-03-22 04:50:36 +00:00
|
|
|
for (aa = at_ifaddr_list; aa != NULL; aa = aa->aa_next) {
|
1997-08-28 01:17:12 +00:00
|
|
|
if (((net == 0) || (aa->aa_ifp == ifp)) &&
|
2004-03-22 03:24:10 +00:00
|
|
|
net >= ntohs(aa->aa_firstnet) &&
|
|
|
|
net <= ntohs(aa->aa_lastnet)) {
|
1996-05-24 01:35:45 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
1997-08-28 01:17:12 +00:00
|
|
|
} else {
|
2004-03-22 03:24:10 +00:00
|
|
|
m_freem(m);
|
1998-08-04 23:17:05 +00:00
|
|
|
#ifdef NETATALK_DEBUG
|
|
|
|
if (ro->ro_rt == NULL)
|
|
|
|
printf ("ddp_route: no ro_rt.\n");
|
|
|
|
else if (ro->ro_rt->rt_ifa == NULL)
|
|
|
|
printf ("ddp_route: no ro_rt->rt_ifa\n");
|
|
|
|
else
|
|
|
|
printf ("ddp_route: no ro_rt->rt_ifa->ifa_ifp\n");
|
|
|
|
#endif
|
2004-03-22 03:24:10 +00:00
|
|
|
return (ENETUNREACH);
|
1996-05-24 01:35:45 +00:00
|
|
|
}
|
1997-08-28 01:17:12 +00:00
|
|
|
|
2004-03-22 03:24:10 +00:00
|
|
|
if (aa == NULL) {
|
1998-08-04 23:17:05 +00:00
|
|
|
#ifdef NETATALK_DEBUG
|
2004-03-22 03:24:10 +00:00
|
|
|
printf("ddp_route: no atalk address found for %s\n",
|
2003-10-31 18:32:15 +00:00
|
|
|
ifp->if_xname);
|
1998-08-04 23:17:05 +00:00
|
|
|
#endif
|
2004-03-22 03:24:10 +00:00
|
|
|
m_freem(m);
|
|
|
|
return (ENETUNREACH);
|
1996-05-24 01:35:45 +00:00
|
|
|
}
|
|
|
|
|
1998-07-09 02:00:02 +00:00
|
|
|
/*
|
|
|
|
* if the destination address is on a directly attached node use that,
|
|
|
|
* else use the official gateway.
|
|
|
|
*/
|
2004-03-22 03:24:10 +00:00
|
|
|
if (ntohs(satosat(&ro->ro_dst)->sat_addr.s_net) >=
|
|
|
|
ntohs(aa->aa_firstnet) &&
|
|
|
|
ntohs(satosat(&ro->ro_dst)->sat_addr.s_net) <=
|
|
|
|
ntohs(aa->aa_lastnet)) {
|
|
|
|
gate = *satosat(&ro->ro_dst);
|
1998-07-09 02:00:02 +00:00
|
|
|
} else {
|
2004-03-22 03:24:10 +00:00
|
|
|
gate = *satosat(ro->ro_rt->rt_gateway);
|
1998-07-09 02:00:02 +00:00
|
|
|
}
|
|
|
|
|
1996-05-24 01:35:45 +00:00
|
|
|
/*
|
|
|
|
* There are several places in the kernel where data is added to
|
|
|
|
* an mbuf without ensuring that the mbuf pointer is aligned.
|
|
|
|
* This is bad for transition routing, since phase 1 and phase 2
|
|
|
|
* packets end up poorly aligned due to the three byte elap header.
|
|
|
|
*/
|
2004-03-22 03:24:10 +00:00
|
|
|
if (!(aa->aa_flags & AFA_PHASE2)) {
|
2005-08-30 16:28:46 +00:00
|
|
|
MGET(m0, M_DONTWAIT, MT_DATA);
|
2004-03-22 03:24:10 +00:00
|
|
|
if (m0 == NULL) {
|
|
|
|
m_freem(m);
|
1996-05-24 01:35:45 +00:00
|
|
|
printf("ddp_route: no buffers\n");
|
2004-03-22 03:24:10 +00:00
|
|
|
return (ENOBUFS);
|
1996-05-24 01:35:45 +00:00
|
|
|
}
|
2002-08-15 18:58:44 +00:00
|
|
|
#ifdef MAC
|
2005-07-05 23:39:51 +00:00
|
|
|
mac_copy_mbuf(m, m0);
|
2002-08-15 18:58:44 +00:00
|
|
|
#endif
|
1996-05-24 01:35:45 +00:00
|
|
|
m0->m_next = m;
|
|
|
|
/* XXX perhaps we ought to align the header? */
|
|
|
|
m0->m_len = SZ_ELAPHDR;
|
|
|
|
m = m0;
|
|
|
|
|
2004-03-22 03:24:10 +00:00
|
|
|
elh = mtod(m, struct elaphdr *);
|
|
|
|
elh->el_snode = satosat(&aa->aa_addr)->sat_addr.s_node;
|
1996-05-24 01:35:45 +00:00
|
|
|
elh->el_type = ELAP_DDPEXTEND;
|
1998-07-13 06:34:02 +00:00
|
|
|
elh->el_dnode = gate.sat_addr.s_node;
|
1996-05-24 01:35:45 +00:00
|
|
|
}
|
|
|
|
ro->ro_rt->rt_use++;
|
|
|
|
|
1998-08-04 23:17:05 +00:00
|
|
|
#ifdef NETATALK_DEBUG
|
2003-10-31 18:32:15 +00:00
|
|
|
printf ("ddp_route: from %d.%d to %d.%d, via %d.%d (%s)\n",
|
1998-08-04 23:17:05 +00:00
|
|
|
ntohs(satosat(&aa->aa_addr)->sat_addr.s_net),
|
|
|
|
satosat(&aa->aa_addr)->sat_addr.s_node,
|
|
|
|
ntohs(satosat(&ro->ro_dst)->sat_addr.s_net),
|
|
|
|
satosat(&ro->ro_dst)->sat_addr.s_node,
|
|
|
|
ntohs(gate.sat_addr.s_net),
|
|
|
|
gate.sat_addr.s_node,
|
2003-10-31 18:32:15 +00:00
|
|
|
ifp->if_xname);
|
1998-08-04 23:17:05 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* short-circuit the output if we're sending this to ourself */
|
|
|
|
if ((satosat(&aa->aa_addr)->sat_addr.s_net == satosat(&ro->ro_dst)->sat_addr.s_net) &&
|
|
|
|
(satosat(&aa->aa_addr)->sat_addr.s_node == satosat(&ro->ro_dst)->sat_addr.s_node))
|
|
|
|
{
|
2000-05-24 21:16:56 +00:00
|
|
|
return (if_simloop(ifp, m, gate.sat_family, 0));
|
1998-08-04 23:17:05 +00:00
|
|
|
}
|
|
|
|
|
2004-03-22 03:24:10 +00:00
|
|
|
return ((*ifp->if_output)(ifp,
|
1996-07-23 01:18:47 +00:00
|
|
|
m, (struct sockaddr *)&gate, NULL)); /* XXX */
|
1996-05-24 01:35:45 +00:00
|
|
|
}
|