No empty .Rs/.Re
[netbsd-mini2440.git] / sys / net / if_atmsubr.c
blobd66b153598683c2e8076c76d0ecd053849a89304
1 /* $NetBSD: if_atmsubr.c,v 1.45 2009/03/18 17:06:51 cegger Exp $ */
3 /*
5 * Copyright (c) 1996 Charles D. Cranor and Washington University.
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Charles D. Cranor and
19 * Washington University.
20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 * if_atmsubr.c
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: if_atmsubr.c,v 1.45 2009/03/18 17:06:51 cegger Exp $");
42 #include "opt_inet.h"
43 #include "opt_gateway.h"
44 #include "opt_natm.h"
46 #include "bpfilter.h"
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/malloc.h>
52 #include <sys/mbuf.h>
53 #include <sys/protosw.h>
54 #include <sys/socket.h>
55 #include <sys/ioctl.h>
56 #include <sys/errno.h>
57 #include <sys/syslog.h>
59 #include <sys/cpu.h>
61 #include <net/if.h>
62 #include <net/netisr.h>
63 #include <net/route.h>
64 #include <net/if_dl.h>
65 #include <net/if_types.h>
66 #include <net/if_atm.h>
67 #include <net/ethertypes.h> /* XXX: for ETHERTYPE_* */
69 #if NBPFILTER > 0
70 #include <net/bpf.h>
71 #endif
73 #include <netinet/in.h>
74 #include <netinet/if_atm.h>
76 #if defined(INET) || defined(INET6)
77 #include <netinet/in_var.h>
78 #endif
79 #ifdef NATM
80 #include <netnatm/natm.h>
81 #endif
83 #define senderr(e) { error = (e); goto bad;}
86 * atm_output: ATM output routine
87 * inputs:
88 * "ifp" = ATM interface to output to
89 * "m0" = the packet to output
90 * "dst" = the sockaddr to send to (either IP addr, or raw VPI/VCI)
91 * "rt0" = the route to use
92 * returns: error code [0 == ok]
94 * note: special semantic: if (dst == NULL) then we assume "m" already
95 * has an atm_pseudohdr on it and just send it directly.
96 * [for native mode ATM output] if dst is null, then
97 * rt0 must also be NULL.
101 atm_output(struct ifnet *ifp, struct mbuf *m0, const struct sockaddr *dst,
102 struct rtentry *rt0)
104 uint16_t etype = 0; /* if using LLC/SNAP */
105 int error = 0, sz;
106 struct atm_pseudohdr atmdst, *ad;
107 struct mbuf *m = m0;
108 struct rtentry *rt;
109 struct atmllc *atmllc;
110 uint32_t atm_flags;
111 ALTQ_DECL(struct altq_pktattr pktattr;)
113 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
114 senderr(ENETDOWN);
117 * If the queueing discipline needs packet classification,
118 * do it before prepending link headers.
120 IFQ_CLASSIFY(&ifp->if_snd, m,
121 (dst != NULL ? dst->sa_family : AF_UNSPEC), &pktattr);
124 * check route
126 if ((rt = rt0) != NULL) {
128 if ((rt->rt_flags & RTF_UP) == 0) { /* route went down! */
129 if ((rt0 = rt = RTALLOC1(dst, 0)) != NULL)
130 rt->rt_refcnt--;
131 else
132 senderr(EHOSTUNREACH);
135 if (rt->rt_flags & RTF_GATEWAY) {
136 if (rt->rt_gwroute == 0)
137 goto lookup;
138 if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
139 rtfree(rt); rt = rt0;
140 lookup: rt->rt_gwroute = RTALLOC1(rt->rt_gateway, 0);
141 if ((rt = rt->rt_gwroute) == 0)
142 senderr(EHOSTUNREACH);
146 /* XXX: put RTF_REJECT code here if doing ATMARP */
151 * check for non-native ATM traffic (dst != NULL)
153 if (dst) {
154 switch (dst->sa_family) {
155 #ifdef INET
156 case AF_INET:
157 #endif
158 #ifdef INET6
159 case AF_INET6:
160 #endif
161 #if defined(INET) || defined(INET6)
162 if (dst->sa_family == AF_INET)
163 etype = ETHERTYPE_IP;
164 else
165 etype = ETHERTYPE_IPV6;
166 # ifdef ATM_PVCEXT
167 if (ifp->if_flags & IFF_POINTOPOINT) {
168 /* pvc subinterface */
169 struct pvcsif *pvcsif = (struct pvcsif *)ifp;
170 atmdst = pvcsif->sif_aph;
171 break;
173 # endif
174 if (!atmresolve(rt, m, dst, &atmdst)) {
175 m = NULL;
176 /* XXX: atmresolve already free'd it */
177 senderr(EHOSTUNREACH);
178 /* XXX: put ATMARP stuff here */
179 /* XXX: watch who frees m on failure */
181 break;
182 #endif
184 case AF_UNSPEC:
186 * XXX: bpfwrite or output from a pvc shadow if.
187 * assuming dst contains 12 bytes (atm pseudo
188 * header (4) + LLC/SNAP (8))
190 memcpy(&atmdst, dst->sa_data, sizeof(atmdst));
191 break;
193 default:
194 #if defined(__NetBSD__) || defined(__OpenBSD__)
195 printf("%s: can't handle af%d\n", ifp->if_xname,
196 dst->sa_family);
197 #elif defined(__FreeBSD__) || defined(__bsdi__)
198 printf("%s%d: can't handle af%d\n", ifp->if_name,
199 ifp->if_unit, dst->sa_family);
200 #endif
201 senderr(EAFNOSUPPORT);
205 * must add atm_pseudohdr to data
207 sz = sizeof(atmdst);
208 atm_flags = ATM_PH_FLAGS(&atmdst);
209 if (atm_flags & ATM_PH_LLCSNAP) sz += 8; /* sizeof snap == 8 */
210 M_PREPEND(m, sz, M_DONTWAIT);
211 if (m == 0)
212 senderr(ENOBUFS);
213 ad = mtod(m, struct atm_pseudohdr *);
214 *ad = atmdst;
215 if (atm_flags & ATM_PH_LLCSNAP) {
216 atmllc = (struct atmllc *)(ad + 1);
217 memcpy(atmllc->llchdr, ATMLLC_HDR,
218 sizeof(atmllc->llchdr));
219 ATM_LLC_SETTYPE(atmllc, etype);
223 return ifq_enqueue(ifp, m ALTQ_COMMA ALTQ_DECL(&pktattr));
225 bad:
226 if (m)
227 m_freem(m);
228 return (error);
232 * Process a received ATM packet;
233 * the packet is in the mbuf chain m.
235 void
236 atm_input(struct ifnet *ifp, struct atm_pseudohdr *ah, struct mbuf *m,
237 void *rxhand)
239 struct ifqueue *inq;
240 uint16_t etype = ETHERTYPE_IP; /* default */
241 int s;
243 if ((ifp->if_flags & IFF_UP) == 0) {
244 m_freem(m);
245 return;
247 ifp->if_ibytes += m->m_pkthdr.len;
249 if (rxhand) {
250 #ifdef NATM
251 struct natmpcb *npcb = rxhand;
252 s = splnet(); /* in case 2 atm cards @ diff lvls */
253 npcb->npcb_inq++; /* count # in queue */
254 splx(s);
255 schednetisr(NETISR_NATM);
256 inq = &natmintrq;
257 m->m_pkthdr.rcvif = rxhand; /* XXX: overload */
258 #else
259 printf("atm_input: NATM detected but not configured in kernel\n");
260 m_freem(m);
261 return;
262 #endif
263 } else {
265 * handle LLC/SNAP header, if present
267 if (ATM_PH_FLAGS(ah) & ATM_PH_LLCSNAP) {
268 struct atmllc *alc;
269 if (m->m_len < sizeof(*alc) && (m = m_pullup(m, sizeof(*alc))) == 0)
270 return; /* failed */
271 alc = mtod(m, struct atmllc *);
272 if (memcmp(alc, ATMLLC_HDR, 6)) {
273 #if defined(__NetBSD__) || defined(__OpenBSD__)
274 printf("%s: recv'd invalid LLC/SNAP frame [vp=%d,vc=%d]\n",
275 ifp->if_xname, ATM_PH_VPI(ah), ATM_PH_VCI(ah));
276 #elif defined(__FreeBSD__) || defined(__bsdi__)
277 printf("%s%d: recv'd invalid LLC/SNAP frame [vp=%d,vc=%d]\n",
278 ifp->if_name, ifp->if_unit, ATM_PH_VPI(ah), ATM_PH_VCI(ah));
279 #endif
280 m_freem(m);
281 return;
283 etype = ATM_LLC_TYPE(alc);
284 m_adj(m, sizeof(*alc));
287 switch (etype) {
288 #ifdef INET
289 case ETHERTYPE_IP:
290 #ifdef GATEWAY
291 if (ipflow_fastforward(m))
292 return;
293 #endif
294 schednetisr(NETISR_IP);
295 inq = &ipintrq;
296 break;
297 #endif /* INET */
298 #ifdef INET6
299 case ETHERTYPE_IPV6:
300 #ifdef GATEWAY
301 if (ip6flow_fastforward(m))
302 return;
303 #endif
304 schednetisr(NETISR_IPV6);
305 inq = &ip6intrq;
306 break;
307 #endif
308 default:
309 m_freem(m);
310 return;
314 s = splnet();
315 if (IF_QFULL(inq)) {
316 IF_DROP(inq);
317 m_freem(m);
318 } else
319 IF_ENQUEUE(inq, m);
320 splx(s);
324 * Perform common duties while attaching to interface list
326 void
327 atm_ifattach(struct ifnet *ifp)
330 ifp->if_type = IFT_ATM;
331 ifp->if_addrlen = 0;
332 ifp->if_hdrlen = 0;
333 ifp->if_dlt = DLT_ATM_RFC1483;
334 ifp->if_mtu = ATMMTU;
335 ifp->if_output = atm_output;
336 #if 0 /* XXX XXX XXX */
337 ifp->if_input = atm_input;
338 #endif
340 if_alloc_sadl(ifp);
341 /* XXX Store LLADDR for ATMARP. */
343 #if NBPFILTER > 0
344 bpfattach(ifp, DLT_ATM_RFC1483, sizeof(struct atmllc));
345 #endif
348 #ifdef ATM_PVCEXT
350 static int pvc_max_number = 16; /* max number of PVCs */
351 static int pvc_number = 0; /* pvc unit number */
353 struct ifnet *
354 pvcsif_alloc(void)
356 struct pvcsif *pvcsif;
358 if (pvc_number >= pvc_max_number)
359 return (NULL);
360 pvcsif = malloc(sizeof(struct pvcsif),
361 M_DEVBUF, M_WAITOK|M_ZERO);
362 if (pvcsif == NULL)
363 return (NULL);
365 #ifdef __NetBSD__
366 snprintf(pvcsif->sif_if.if_xname, sizeof(pvcsif->sif_if.if_xname),
367 "pvc%d", pvc_number++);
368 #else
369 pvcsif->sif_if.if_name = "pvc";
370 pvcsif->sif_if.if_unit = pvc_number++;
371 #endif
372 return (&pvcsif->sif_if);
374 #endif /* ATM_PVCEXT */