xref: /original-bsd/sys/netns/ns_output.c (revision 5fd6b0d9)
1 /*
2  * Copyright (c) 1984, 1985, 1986, 1987 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  *
17  *	@(#)ns_output.c	7.4 (Berkeley) 10/12/88
18  */
19 
20 #include "param.h"
21 #include "malloc.h"
22 #include "mbuf.h"
23 #include "errno.h"
24 #include "socket.h"
25 #include "socketvar.h"
26 
27 #include "../net/if.h"
28 #include "../net/route.h"
29 
30 #include "ns.h"
31 #include "ns_if.h"
32 #include "idp.h"
33 #include "idp_var.h"
34 
35 #ifdef vax
36 #include "../vax/mtpr.h"
37 #endif
38 int ns_hold_output = 0;
39 int ns_copy_output = 0;
40 int ns_output_cnt = 0;
41 struct mbuf *ns_lastout;
42 
43 ns_output(m0, ro, flags)
44 	struct mbuf *m0;
45 	struct route *ro;
46 	int flags;
47 {
48 	register struct idp *idp = mtod(m0, struct idp *);
49 	register struct ifnet *ifp = 0;
50 	int error = 0;
51 	struct route idproute;
52 	struct sockaddr_ns *dst;
53 	extern int idpcksum;
54 
55 	if (ns_hold_output) {
56 		if (ns_lastout) {
57 			(void)m_free(ns_lastout);
58 		}
59 		ns_lastout = m_copy(m0, 0, (int)M_COPYALL);
60 	}
61 	/*
62 	 * Route packet.
63 	 */
64 	if (ro == 0) {
65 		ro = &idproute;
66 		bzero((caddr_t)ro, sizeof (*ro));
67 	}
68 	dst = (struct sockaddr_ns *)&ro->ro_dst;
69 	if (ro->ro_rt == 0) {
70 		dst->sns_family = AF_NS;
71 		dst->sns_addr = idp->idp_dna;
72 		dst->sns_addr.x_port = 0;
73 		/*
74 		 * If routing to interface only,
75 		 * short circuit routing lookup.
76 		 */
77 		if (flags & NS_ROUTETOIF) {
78 			struct ns_ifaddr *ia = ns_iaonnetof(&idp->idp_dna);
79 
80 			if (ia == 0) {
81 				error = ENETUNREACH;
82 				goto bad;
83 			}
84 			ifp = ia->ia_ifp;
85 			goto gotif;
86 		}
87 		rtalloc(ro);
88 	} else if ((ro->ro_rt->rt_flags & RTF_UP) == 0) {
89 		/*
90 		 * The old route has gone away; try for a new one.
91 		 */
92 		rtfree(ro->ro_rt);
93 		ro->ro_rt = NULL;
94 		rtalloc(ro);
95 	}
96 	if (ro->ro_rt == 0 || (ifp = ro->ro_rt->rt_ifp) == 0) {
97 		error = ENETUNREACH;
98 		goto bad;
99 	}
100 	ro->ro_rt->rt_use++;
101 	if (ro->ro_rt->rt_flags & (RTF_GATEWAY|RTF_HOST))
102 		dst = (struct sockaddr_ns *)&ro->ro_rt->rt_gateway;
103 gotif:
104 
105 	/*
106 	 * Look for multicast addresses and
107 	 * and verify user is allowed to send
108 	 * such a packet.
109 	 */
110 	if (dst->sns_addr.x_host.c_host[0]&1) {
111 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
112 			error = EADDRNOTAVAIL;
113 			goto bad;
114 		}
115 		if ((flags & NS_ALLOWBROADCAST) == 0) {
116 			error = EACCES;
117 			goto bad;
118 		}
119 	}
120 
121 	if (htons(idp->idp_len) <= ifp->if_mtu) {
122 		ns_output_cnt++;
123 		if (ns_copy_output) {
124 			ns_watch_output(m0, ifp);
125 		}
126 		error = (*ifp->if_output)(ifp, m0, (struct sockaddr *)dst);
127 		goto done;
128 	} else error = EMSGSIZE;
129 
130 
131 bad:
132 	if (ns_copy_output) {
133 		ns_watch_output(m0, ifp);
134 	}
135 	m_freem(m0);
136 done:
137 	if (ro == &idproute && (flags & NS_ROUTETOIF) == 0 && ro->ro_rt)
138 		RTFREE(ro->ro_rt);
139 	return (error);
140 }
141