xref: /csrg-svn/sys/netns/ns_output.c (revision 23501)
1 /*
2  * Copyright (c) 1982 Regents of the University of California.
3  * All rights reserved.  The Berkeley software License Agreement
4  * specifies the terms and conditions for redistribution.
5  *
6  *	@(#)ns_output.c	6.4 (Berkeley) 06/16/85
7  */
8 
9 #include "param.h"
10 #include "mbuf.h"
11 #include "errno.h"
12 #include "socket.h"
13 #include "socketvar.h"
14 
15 #include "../net/if.h"
16 #include "../net/route.h"
17 
18 #include "ns.h"
19 #include "ns_if.h"
20 #include "idp.h"
21 #include "idp_var.h"
22 
23 #ifdef vax
24 #include "../vax/mtpr.h"
25 #endif
26 int ns_hold_output = 0;
27 int ns_copy_output = 0;
28 int ns_output_cnt = 0;
29 struct mbuf *ns_lastout;
30 
31 ns_output(m0, ro, flags)
32 	struct mbuf *m0;
33 	struct route *ro;
34 	int flags;
35 {
36 	register struct idp *idp = mtod(m0, struct idp *);
37 	register struct ifnet *ifp;
38 	register struct mbuf *m;
39 	int len, rlen, off, error = 0;
40 	struct route idproute;
41 	struct sockaddr_ns *dst;
42 	extern int idpcksum;
43 
44 	if (ns_hold_output) {
45 		if (ns_lastout) {
46 			m_free(ns_lastout);
47 		}
48 		ns_lastout = m_copy(m0, 0, M_COPYALL);
49 	}
50 	if (ns_copy_output) {
51 		ns_watch_output(m0);
52 	}
53 
54 	/*
55 	 * Route packet.
56 	 */
57 	if (ro == 0) {
58 		ro = &idproute;
59 		bzero((caddr_t)ro, sizeof (*ro));
60 	}
61 	dst = (struct sockaddr_ns *)&ro->ro_dst;
62 	if (ro->ro_rt == 0) {
63 		dst->sns_family = AF_NS;
64 		dst->sns_addr = idp->idp_dna;
65 		dst->sns_addr.x_port = 0;
66 		/*
67 		 * If routing to interface only,
68 		 * short circuit routing lookup.
69 		 */
70 		if (flags & NS_ROUTETOIF) {
71 			struct ns_ifaddr *ia;
72 			ia = ns_iaonnetof(idp->idp_dna.x_net);
73 			if (ia == 0) {
74 				error = ENETUNREACH;
75 				goto bad;
76 			}
77 			ifp = ia->ia_ifp;
78 			goto gotif;
79 		}
80 		rtalloc(ro);
81 	} else if ((ro->ro_rt->rt_flags & RTF_UP) == 0) {
82 		/*
83 		 * The old route has gone away; try for a new one.
84 		 */
85 		rtfree(ro->ro_rt);
86 		rtalloc(ro);
87 	}
88 	if (ro->ro_rt == 0 || (ifp = ro->ro_rt->rt_ifp) == 0) {
89 		error = ENETUNREACH;
90 		goto bad;
91 	}
92 	ro->ro_rt->rt_use++;
93 	if (ro->ro_rt->rt_flags & (RTF_GATEWAY|RTF_HOST))
94 		dst = (struct sockaddr_ns *)&ro->ro_rt->rt_gateway;
95 gotif:
96 
97 	/*
98 	 * Look for multicast addresses and
99 	 * and verify user is allowed to send
100 	 * such a packet.
101 	 */
102 	if (dst->sns_addr.x_host.c_host[0]&1) {
103 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
104 			error = EADDRNOTAVAIL;
105 			goto bad;
106 		}
107 		if ((flags & NS_ALLOWBROADCAST) == 0) {
108 			error = EACCES;
109 			goto bad;
110 		}
111 	}
112 
113 	if (htons(idp->idp_len) <= ifp->if_mtu) {
114 		ns_output_cnt++;
115 		error = (*ifp->if_output)(ifp, m0, (struct sockaddr *)dst);
116 		goto done;
117 	} else error = EMSGSIZE;
118 
119 
120 bad:
121 	m_freem(m0);
122 done:
123 	if (ro == &idproute && (flags & NS_ROUTETOIF) == 0 && ro->ro_rt)
124 		RTFREE(ro->ro_rt);
125 	return (error);
126 }
127