1 /* $NetBSD: if_atmsubr.c,v 1.45 2009/03/18 17:06:51 cegger Exp $ */
5 * Copyright (c) 1996 Charles D. Cranor and Washington University.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: if_atmsubr.c,v 1.45 2009/03/18 17:06:51 cegger Exp $");
43 #include "opt_gateway.h"
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/malloc.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>
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_* */
73 #include <netinet/in.h>
74 #include <netinet/if_atm.h>
76 #if defined(INET) || defined(INET6)
77 #include <netinet/in_var.h>
80 #include <netnatm/natm.h>
83 #define senderr(e) { error = (e); goto bad;}
86 * atm_output: ATM output routine
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
,
104 uint16_t etype
= 0; /* if using LLC/SNAP */
106 struct atm_pseudohdr atmdst
, *ad
;
109 struct atmllc
*atmllc
;
111 ALTQ_DECL(struct altq_pktattr pktattr
;)
113 if ((ifp
->if_flags
& (IFF_UP
|IFF_RUNNING
)) != (IFF_UP
|IFF_RUNNING
))
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
);
126 if ((rt
= rt0
) != NULL
) {
128 if ((rt
->rt_flags
& RTF_UP
) == 0) { /* route went down! */
129 if ((rt0
= rt
= RTALLOC1(dst
, 0)) != NULL
)
132 senderr(EHOSTUNREACH
);
135 if (rt
->rt_flags
& RTF_GATEWAY
) {
136 if (rt
->rt_gwroute
== 0)
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)
154 switch (dst
->sa_family
) {
161 #if defined(INET) || defined(INET6)
162 if (dst
->sa_family
== AF_INET
)
163 etype
= ETHERTYPE_IP
;
165 etype
= ETHERTYPE_IPV6
;
167 if (ifp
->if_flags
& IFF_POINTOPOINT
) {
168 /* pvc subinterface */
169 struct pvcsif
*pvcsif
= (struct pvcsif
*)ifp
;
170 atmdst
= pvcsif
->sif_aph
;
174 if (!atmresolve(rt
, m
, dst
, &atmdst
)) {
176 /* XXX: atmresolve already free'd it */
177 senderr(EHOSTUNREACH
);
178 /* XXX: put ATMARP stuff here */
179 /* XXX: watch who frees m on failure */
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
));
194 #if defined(__NetBSD__) || defined(__OpenBSD__)
195 printf("%s: can't handle af%d\n", ifp
->if_xname
,
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
);
201 senderr(EAFNOSUPPORT
);
205 * must add atm_pseudohdr to data
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
);
213 ad
= mtod(m
, struct atm_pseudohdr
*);
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
));
232 * Process a received ATM packet;
233 * the packet is in the mbuf chain m.
236 atm_input(struct ifnet
*ifp
, struct atm_pseudohdr
*ah
, struct mbuf
*m
,
240 uint16_t etype
= ETHERTYPE_IP
; /* default */
243 if ((ifp
->if_flags
& IFF_UP
) == 0) {
247 ifp
->if_ibytes
+= m
->m_pkthdr
.len
;
251 struct natmpcb
*npcb
= rxhand
;
252 s
= splnet(); /* in case 2 atm cards @ diff lvls */
253 npcb
->npcb_inq
++; /* count # in queue */
255 schednetisr(NETISR_NATM
);
257 m
->m_pkthdr
.rcvif
= rxhand
; /* XXX: overload */
259 printf("atm_input: NATM detected but not configured in kernel\n");
265 * handle LLC/SNAP header, if present
267 if (ATM_PH_FLAGS(ah
) & ATM_PH_LLCSNAP
) {
269 if (m
->m_len
< sizeof(*alc
) && (m
= m_pullup(m
, sizeof(*alc
))) == 0)
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
));
283 etype
= ATM_LLC_TYPE(alc
);
284 m_adj(m
, sizeof(*alc
));
291 if (ipflow_fastforward(m
))
294 schednetisr(NETISR_IP
);
301 if (ip6flow_fastforward(m
))
304 schednetisr(NETISR_IPV6
);
324 * Perform common duties while attaching to interface list
327 atm_ifattach(struct ifnet
*ifp
)
330 ifp
->if_type
= IFT_ATM
;
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
;
341 /* XXX Store LLADDR for ATMARP. */
344 bpfattach(ifp
, DLT_ATM_RFC1483
, sizeof(struct atmllc
));
350 static int pvc_max_number
= 16; /* max number of PVCs */
351 static int pvc_number
= 0; /* pvc unit number */
356 struct pvcsif
*pvcsif
;
358 if (pvc_number
>= pvc_max_number
)
360 pvcsif
= malloc(sizeof(struct pvcsif
),
361 M_DEVBUF
, M_WAITOK
|M_ZERO
);
366 snprintf(pvcsif
->sif_if
.if_xname
, sizeof(pvcsif
->sif_if
.if_xname
),
367 "pvc%d", pvc_number
++);
369 pvcsif
->sif_if
.if_name
= "pvc";
370 pvcsif
->sif_if
.if_unit
= pvc_number
++;
372 return (&pvcsif
->sif_if
);
374 #endif /* ATM_PVCEXT */