Commit | Line | Data |
---|---|---|
8ae0e4b4 | 1 | /* |
240edf1f KS |
2 | * Copyright (c) 1984, 1985, 1986, 1987 Regents of the University of California. |
3 | * All rights reserved. | |
8ae0e4b4 | 4 | * |
240edf1f | 5 | * Redistribution and use in source and binary forms are permitted |
616d42db KB |
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. | |
240edf1f | 16 | * |
bf8d7cff | 17 | * @(#)ns_output.c 7.4 (Berkeley) %G% |
8ae0e4b4 | 18 | */ |
91fdd342 KS |
19 | |
20 | #include "param.h" | |
bf8d7cff | 21 | #include "malloc.h" |
91fdd342 KS |
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 | |
6bbc5804 KS |
38 | int ns_hold_output = 0; |
39 | int ns_copy_output = 0; | |
40 | int ns_output_cnt = 0; | |
91fdd342 KS |
41 | struct mbuf *ns_lastout; |
42 | ||
91fdd342 KS |
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 *); | |
d0c6cd85 | 49 | register struct ifnet *ifp = 0; |
f97be0c9 | 50 | int error = 0; |
91fdd342 KS |
51 | struct route idproute; |
52 | struct sockaddr_ns *dst; | |
53 | extern int idpcksum; | |
54 | ||
5fe5db7d KS |
55 | if (ns_hold_output) { |
56 | if (ns_lastout) { | |
8011f5df | 57 | (void)m_free(ns_lastout); |
91fdd342 | 58 | } |
f97be0c9 | 59 | ns_lastout = m_copy(m0, 0, (int)M_COPYALL); |
91fdd342 | 60 | } |
91fdd342 KS |
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; | |
5fe5db7d | 72 | dst->sns_addr.x_port = 0; |
91fdd342 KS |
73 | /* |
74 | * If routing to interface only, | |
75 | * short circuit routing lookup. | |
76 | */ | |
77 | if (flags & NS_ROUTETOIF) { | |
d0c6cd85 | 78 | struct ns_ifaddr *ia = ns_iaonnetof(&idp->idp_dna); |
b8a7d828 | 79 | |
91fdd342 KS |
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); | |
e89cd9c7 | 93 | ro->ro_rt = NULL; |
91fdd342 KS |
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++; | |
d0c6cd85 KS |
123 | if (ns_copy_output) { |
124 | ns_watch_output(m0, ifp); | |
125 | } | |
91fdd342 KS |
126 | error = (*ifp->if_output)(ifp, m0, (struct sockaddr *)dst); |
127 | goto done; | |
128 | } else error = EMSGSIZE; | |
129 | ||
130 | ||
131 | bad: | |
d0c6cd85 KS |
132 | if (ns_copy_output) { |
133 | ns_watch_output(m0, ifp); | |
134 | } | |
91fdd342 KS |
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 | } |