| Commit | Line | Data |
|---|---|---|
| 984263bc MD |
1 | /* |
| 2 | * Copyright (c) 1982, 1989, 1993 | |
| 3 | * The Regents of the University of California. All rights reserved. | |
| 4 | * | |
| 5 | * Redistribution and use in source and binary forms, with or without | |
| 6 | * modification, are permitted provided that the following conditions | |
| 7 | * are met: | |
| 8 | * 1. Redistributions of source code must retain the above copyright | |
| 9 | * notice, this list of conditions and the following disclaimer. | |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | |
| 11 | * notice, this list of conditions and the following disclaimer in the | |
| 12 | * documentation and/or other materials provided with the distribution. | |
| 13 | * 3. All advertising materials mentioning features or use of this software | |
| 14 | * must display the following acknowledgement: | |
| 15 | * This product includes software developed by the University of | |
| 16 | * California, Berkeley and its contributors. | |
| 17 | * 4. Neither the name of the University nor the names of its contributors | |
| 18 | * may be used to endorse or promote products derived from this software | |
| 19 | * without specific prior written permission. | |
| 20 | * | |
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
| 31 | * SUCH DAMAGE. | |
| 32 | * | |
| 33 | * @(#)if_ethersubr.c 8.1 (Berkeley) 6/10/93 | |
| 34 | * $FreeBSD: src/sys/net/if_ethersubr.c,v 1.70.2.33 2003/04/28 15:45:53 archie Exp $ | |
| b9ed4403 | 35 | * $DragonFly: src/sys/net/if_ethersubr.c,v 1.96 2008/11/22 04:00:53 sephe Exp $ |
| 984263bc MD |
36 | */ |
| 37 | ||
| 38 | #include "opt_atalk.h" | |
| 39 | #include "opt_inet.h" | |
| 40 | #include "opt_inet6.h" | |
| 41 | #include "opt_ipx.h" | |
| 9b42cabe | 42 | #include "opt_mpls.h" |
| 984263bc | 43 | #include "opt_netgraph.h" |
| 0d16ba1d | 44 | #include "opt_carp.h" |
| 7c7f9646 | 45 | #include "opt_rss.h" |
| 984263bc MD |
46 | |
| 47 | #include <sys/param.h> | |
| 48 | #include <sys/systm.h> | |
| 68b67450 | 49 | #include <sys/globaldata.h> |
| 984263bc | 50 | #include <sys/kernel.h> |
| f3e0b5f0 | 51 | #include <sys/ktr.h> |
| 8b3db995 | 52 | #include <sys/lock.h> |
| 984263bc MD |
53 | #include <sys/malloc.h> |
| 54 | #include <sys/mbuf.h> | |
| 68b67450 | 55 | #include <sys/msgport.h> |
| 984263bc MD |
56 | #include <sys/socket.h> |
| 57 | #include <sys/sockio.h> | |
| 58 | #include <sys/sysctl.h> | |
| 68b67450 | 59 | #include <sys/thread.h> |
| 684a93c4 | 60 | |
| 68b67450 | 61 | #include <sys/thread2.h> |
| 684a93c4 | 62 | #include <sys/mplock2.h> |
| 984263bc MD |
63 | |
| 64 | #include <net/if.h> | |
| 65 | #include <net/netisr.h> | |
| 66 | #include <net/route.h> | |
| 67 | #include <net/if_llc.h> | |
| 68 | #include <net/if_dl.h> | |
| 69 | #include <net/if_types.h> | |
| 4d723e5a | 70 | #include <net/ifq_var.h> |
| 984263bc MD |
71 | #include <net/bpf.h> |
| 72 | #include <net/ethernet.h> | |
| e6b5847c | 73 | #include <net/vlan/if_vlan_ether.h> |
| 29bc1092 | 74 | #include <net/netmsg2.h> |
| 984263bc MD |
75 | |
| 76 | #if defined(INET) || defined(INET6) | |
| 77 | #include <netinet/in.h> | |
| 8697599b | 78 | #include <netinet/ip_var.h> |
| 984263bc | 79 | #include <netinet/if_ether.h> |
| 4639df5f | 80 | #include <netinet/ip_flow.h> |
| 1f2de5d4 MD |
81 | #include <net/ipfw/ip_fw.h> |
| 82 | #include <net/dummynet/ip_dummynet.h> | |
| 984263bc MD |
83 | #endif |
| 84 | #ifdef INET6 | |
| 85 | #include <netinet6/nd6.h> | |
| 86 | #endif | |
| 87 | ||
| 0d16ba1d MD |
88 | #ifdef CARP |
| 89 | #include <netinet/ip_carp.h> | |
| 90 | #endif | |
| 91 | ||
| 984263bc | 92 | #ifdef IPX |
| d2438d69 MD |
93 | #include <netproto/ipx/ipx.h> |
| 94 | #include <netproto/ipx/ipx_if.h> | |
| 32211831 | 95 | int (*ef_inputp)(struct ifnet*, const struct ether_header *eh, struct mbuf *m); |
| f23061d4 JH |
96 | int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp, struct sockaddr *dst, |
| 97 | short *tp, int *hlen); | |
| 984263bc MD |
98 | #endif |
| 99 | ||
| 984263bc | 100 | #ifdef NETATALK |
| d2438d69 MD |
101 | #include <netproto/atalk/at.h> |
| 102 | #include <netproto/atalk/at_var.h> | |
| 103 | #include <netproto/atalk/at_extern.h> | |
| 984263bc | 104 | |
| f23061d4 JH |
105 | #define llc_snap_org_code llc_un.type_snap.org_code |
| 106 | #define llc_snap_ether_type llc_un.type_snap.ether_type | |
| 984263bc MD |
107 | |
| 108 | extern u_char at_org_code[3]; | |
| 109 | extern u_char aarp_org_code[3]; | |
| 110 | #endif /* NETATALK */ | |
| 111 | ||
| 9b42cabe NA |
112 | #ifdef MPLS |
| 113 | #include <netproto/mpls/mpls.h> | |
| 114 | #endif | |
| 115 | ||
| 984263bc | 116 | /* netgraph node hooks for ng_ether(4) */ |
| 601fa0f9 | 117 | void (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp); |
| 984263bc | 118 | void (*ng_ether_input_orphan_p)(struct ifnet *ifp, |
| 36f48e5e | 119 | struct mbuf *m, const struct ether_header *eh); |
| 984263bc MD |
120 | int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp); |
| 121 | void (*ng_ether_attach_p)(struct ifnet *ifp); | |
| 122 | void (*ng_ether_detach_p)(struct ifnet *ifp); | |
| 123 | ||
| 50098e2e | 124 | void (*vlan_input_p)(struct mbuf *); |
| 3013ac0e | 125 | |
| 5fe66e68 JH |
126 | static int ether_output(struct ifnet *, struct mbuf *, struct sockaddr *, |
| 127 | struct rtentry *); | |
| 9b77ea6e SZ |
128 | static void ether_restore_header(struct mbuf **, const struct ether_header *, |
| 129 | const struct ether_header *); | |
| 984263bc | 130 | |
| f23061d4 | 131 | /* |
| a8d45119 | 132 | * if_bridge support |
| f23061d4 | 133 | */ |
| db37145f | 134 | struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *); |
| eb366364 | 135 | int (*bridge_output_p)(struct ifnet *, struct mbuf *); |
| db37145f | 136 | void (*bridge_dn_p)(struct mbuf *, struct ifnet *); |
| 70d9a675 | 137 | struct ifnet *(*bridge_interface_p)(void *if_bridge); |
| db37145f | 138 | |
| 5fe66e68 JH |
139 | static int ether_resolvemulti(struct ifnet *, struct sockaddr **, |
| 140 | struct sockaddr *); | |
| 141 | ||
| 142 | const uint8_t etherbroadcastaddr[ETHER_ADDR_LEN] = { | |
| c401f0fd JS |
143 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff |
| 144 | }; | |
| 145 | ||
| 5fe66e68 | 146 | #define gotoerr(e) do { error = (e); goto bad; } while (0) |
| f23061d4 | 147 | #define IFP2AC(ifp) ((struct arpcom *)(ifp)) |
| 984263bc | 148 | |
| 5fe66e68 | 149 | static boolean_t ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst, |
| 7c5bb821 | 150 | struct ip_fw **rule, |
| 90ca9293 | 151 | const struct ether_header *eh); |
| 5fe66e68 | 152 | |
| 984263bc | 153 | static int ether_ipfw; |
| 9b77ea6e SZ |
154 | static u_int ether_restore_hdr; |
| 155 | static u_int ether_prepend_hdr; | |
| 70d9a675 | 156 | static int ether_debug; |
| 9b77ea6e | 157 | |
| 7c7f9646 SZ |
158 | #ifdef RSS_DEBUG |
| 159 | static u_int ether_pktinfo_try; | |
| 160 | static u_int ether_pktinfo_hit; | |
| 161 | static u_int ether_rss_nopi; | |
| 162 | static u_int ether_rss_nohash; | |
| 163 | #endif | |
| 164 | ||
| 5fe66e68 JH |
165 | SYSCTL_DECL(_net_link); |
| 166 | SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet"); | |
| 70d9a675 MD |
167 | SYSCTL_INT(_net_link_ether, OID_AUTO, debug, CTLFLAG_RW, |
| 168 | ðer_debug, 0, "Ether debug"); | |
| 5fe66e68 JH |
169 | SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW, |
| 170 | ðer_ipfw, 0, "Pass ether pkts through firewall"); | |
| 9b77ea6e SZ |
171 | SYSCTL_UINT(_net_link_ether, OID_AUTO, restore_hdr, CTLFLAG_RW, |
| 172 | ðer_restore_hdr, 0, "# of ether header restoration"); | |
| 173 | SYSCTL_UINT(_net_link_ether, OID_AUTO, prepend_hdr, CTLFLAG_RW, | |
| 174 | ðer_prepend_hdr, 0, | |
| 175 | "# of ether header restoration which prepends mbuf"); | |
| 7c7f9646 SZ |
176 | #ifdef RSS_DEBUG |
| 177 | SYSCTL_UINT(_net_link_ether, OID_AUTO, rss_nopi, CTLFLAG_RW, | |
| 178 | ðer_rss_nopi, 0, "# of packets do not have pktinfo"); | |
| 179 | SYSCTL_UINT(_net_link_ether, OID_AUTO, rss_nohash, CTLFLAG_RW, | |
| 180 | ðer_rss_nohash, 0, "# of packets do not have hash"); | |
| 181 | SYSCTL_UINT(_net_link_ether, OID_AUTO, pktinfo_try, CTLFLAG_RW, | |
| 182 | ðer_pktinfo_try, 0, | |
| 183 | "# of tries to find packets' msgport using pktinfo"); | |
| 184 | SYSCTL_UINT(_net_link_ether, OID_AUTO, pktinfo_hit, CTLFLAG_RW, | |
| 185 | ðer_pktinfo_hit, 0, | |
| 186 | "# of packets whose msgport are found using pktinfo"); | |
| 187 | #endif | |
| 984263bc | 188 | |
| f3e0b5f0 SZ |
189 | #define ETHER_KTR_STR "ifp=%p" |
| 190 | #define ETHER_KTR_ARG_SIZE (sizeof(void *)) | |
| 191 | #ifndef KTR_ETHERNET | |
| 192 | #define KTR_ETHERNET KTR_ALL | |
| 193 | #endif | |
| 194 | KTR_INFO_MASTER(ether); | |
| 195 | KTR_INFO(KTR_ETHERNET, ether, chain_beg, 0, ETHER_KTR_STR, ETHER_KTR_ARG_SIZE); | |
| 196 | KTR_INFO(KTR_ETHERNET, ether, chain_end, 1, ETHER_KTR_STR, ETHER_KTR_ARG_SIZE); | |
| 197 | KTR_INFO(KTR_ETHERNET, ether, disp_beg, 2, ETHER_KTR_STR, ETHER_KTR_ARG_SIZE); | |
| 198 | KTR_INFO(KTR_ETHERNET, ether, disp_end, 3, ETHER_KTR_STR, ETHER_KTR_ARG_SIZE); | |
| 199 | #define logether(name, arg) KTR_LOG(ether_ ## name, arg) | |
| 200 | ||
| 984263bc MD |
201 | /* |
| 202 | * Ethernet output routine. | |
| 203 | * Encapsulate a packet of type family for the local net. | |
| 204 | * Use trailer local net encapsulation if enough data in first | |
| 205 | * packet leaves a multiple of 512 bytes of data in remainder. | |
| 206 | * Assumes that ifp is actually pointer to arpcom structure. | |
| 207 | */ | |
| 3013ac0e JS |
208 | static int |
| 209 | ether_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst, | |
| f23061d4 | 210 | struct rtentry *rt) |
| 984263bc | 211 | { |
| f23061d4 JH |
212 | struct ether_header *eh, *deh; |
| 213 | u_char *edst; | |
| 984263bc | 214 | int loop_copy = 0; |
| f23061d4 | 215 | int hlen = ETHER_HDR_LEN; /* link layer header length */ |
| 984263bc | 216 | struct arpcom *ac = IFP2AC(ifp); |
| f23061d4 | 217 | int error; |
| 984263bc | 218 | |
| 2c9effcf | 219 | ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp); |
| 57dff79c | 220 | |
| 3a593c54 MD |
221 | if (ifp->if_flags & IFF_MONITOR) |
| 222 | gotoerr(ENETDOWN); | |
| f23061d4 JH |
223 | if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING)) |
| 224 | gotoerr(ENETDOWN); | |
| 225 | ||
| 226 | M_PREPEND(m, sizeof(struct ether_header), MB_DONTWAIT); | |
| 227 | if (m == NULL) | |
| 5fe66e68 | 228 | return (ENOBUFS); |
| f23061d4 JH |
229 | eh = mtod(m, struct ether_header *); |
| 230 | edst = eh->ether_dhost; | |
| 231 | ||
| 5fe66e68 JH |
232 | /* |
| 233 | * Fill in the destination ethernet address and frame type. | |
| 234 | */ | |
| 984263bc MD |
235 | switch (dst->sa_family) { |
| 236 | #ifdef INET | |
| 237 | case AF_INET: | |
| f23061d4 | 238 | if (!arpresolve(ifp, rt, m, dst, edst)) |
| 984263bc | 239 | return (0); /* if not yet resolved */ |
| cb8d752c NA |
240 | #ifdef MPLS |
| 241 | if (m->m_flags & M_MPLSLABELED) | |
| 242 | eh->ether_type = htons(ETHERTYPE_MPLS); | |
| 243 | else | |
| 244 | #endif | |
| 245 | eh->ether_type = htons(ETHERTYPE_IP); | |
| 984263bc MD |
246 | break; |
| 247 | #endif | |
| 248 | #ifdef INET6 | |
| 249 | case AF_INET6: | |
| f23061d4 | 250 | if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, edst)) |
| 5fe66e68 | 251 | return (0); /* Something bad happenned. */ |
| f23061d4 | 252 | eh->ether_type = htons(ETHERTYPE_IPV6); |
| 984263bc MD |
253 | break; |
| 254 | #endif | |
| 255 | #ifdef IPX | |
| 256 | case AF_IPX: | |
| f23061d4 | 257 | if (ef_outputp != NULL) { |
| ff54734e SZ |
258 | /* |
| 259 | * Hold BGL and recheck ef_outputp | |
| 260 | */ | |
| 261 | get_mplock(); | |
| 262 | if (ef_outputp != NULL) { | |
| 263 | error = ef_outputp(ifp, &m, dst, | |
| 264 | &eh->ether_type, &hlen); | |
| 265 | rel_mplock(); | |
| 266 | if (error) | |
| 267 | goto bad; | |
| 268 | else | |
| 269 | break; | |
| 270 | } | |
| 271 | rel_mplock(); | |
| f23061d4 | 272 | } |
| ff54734e SZ |
273 | eh->ether_type = htons(ETHERTYPE_IPX); |
| 274 | bcopy(&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host), | |
| 275 | edst, ETHER_ADDR_LEN); | |
| 984263bc MD |
276 | break; |
| 277 | #endif | |
| 278 | #ifdef NETATALK | |
| f23061d4 JH |
279 | case AF_APPLETALK: { |
| 280 | struct at_ifaddr *aa; | |
| 281 | ||
| ff54734e SZ |
282 | /* |
| 283 | * Hold BGL | |
| 284 | */ | |
| 285 | get_mplock(); | |
| 286 | ||
| f23061d4 JH |
287 | if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL) { |
| 288 | error = 0; /* XXX */ | |
| ff54734e | 289 | rel_mplock(); |
| f23061d4 JH |
290 | goto bad; |
| 291 | } | |
| 292 | /* | |
| 293 | * In the phase 2 case, need to prepend an mbuf for | |
| 294 | * the llc header. Since we must preserve the value | |
| 295 | * of m, which is passed to us by value, we m_copy() | |
| 296 | * the first mbuf, and use it for our llc header. | |
| 297 | */ | |
| 298 | if (aa->aa_flags & AFA_PHASE2) { | |
| 299 | struct llc llc; | |
| 300 | ||
| 301 | M_PREPEND(m, sizeof(struct llc), MB_DONTWAIT); | |
| 5fe66e68 JH |
302 | eh = mtod(m, struct ether_header *); |
| 303 | edst = eh->ether_dhost; | |
| f23061d4 JH |
304 | llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP; |
| 305 | llc.llc_control = LLC_UI; | |
| 306 | bcopy(at_org_code, llc.llc_snap_org_code, | |
| 307 | sizeof at_org_code); | |
| 308 | llc.llc_snap_ether_type = htons(ETHERTYPE_AT); | |
| 5fe66e68 JH |
309 | bcopy(&llc, |
| 310 | mtod(m, caddr_t) + sizeof(struct ether_header), | |
| 311 | sizeof(struct llc)); | |
| f23061d4 JH |
312 | eh->ether_type = htons(m->m_pkthdr.len); |
| 313 | hlen = sizeof(struct llc) + ETHER_HDR_LEN; | |
| 314 | } else { | |
| 315 | eh->ether_type = htons(ETHERTYPE_AT); | |
| 316 | } | |
| ff54734e SZ |
317 | if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst)) { |
| 318 | rel_mplock(); | |
| f23061d4 | 319 | return (0); |
| ff54734e SZ |
320 | } |
| 321 | ||
| 322 | rel_mplock(); | |
| f23061d4 | 323 | break; |
| 984263bc | 324 | } |
| 5fe66e68 | 325 | #endif |
| 984263bc | 326 | case pseudo_AF_HDRCMPLT: |
| 984263bc MD |
327 | case AF_UNSPEC: |
| 328 | loop_copy = -1; /* if this is for us, don't do it */ | |
| f23061d4 JH |
329 | deh = (struct ether_header *)dst->sa_data; |
| 330 | memcpy(edst, deh->ether_dhost, ETHER_ADDR_LEN); | |
| 331 | eh->ether_type = deh->ether_type; | |
| 984263bc MD |
332 | break; |
| 333 | ||
| 334 | default: | |
| 8f0777ca | 335 | if_printf(ifp, "can't handle af%d\n", dst->sa_family); |
| f23061d4 | 336 | gotoerr(EAFNOSUPPORT); |
| 984263bc MD |
337 | } |
| 338 | ||
| f23061d4 JH |
339 | if (dst->sa_family == pseudo_AF_HDRCMPLT) /* unlikely */ |
| 340 | memcpy(eh->ether_shost, | |
| 341 | ((struct ether_header *)dst->sa_data)->ether_shost, | |
| 342 | ETHER_ADDR_LEN); | |
| 984263bc | 343 | else |
| f23061d4 | 344 | memcpy(eh->ether_shost, ac->ac_enaddr, ETHER_ADDR_LEN); |
| 984263bc MD |
345 | |
| 346 | /* | |
| db37145f SS |
347 | * Bridges require special output handling. |
| 348 | */ | |
| 349 | if (ifp->if_bridge) { | |
| 8f0777ca SZ |
350 | KASSERT(bridge_output_p != NULL, |
| 351 | ("%s: if_bridge not loaded!", __func__)); | |
| ad8c8b44 | 352 | return bridge_output_p(ifp, m); |
| db37145f SS |
353 | } |
| 354 | ||
| 355 | /* | |
| 984263bc MD |
356 | * If a simplex interface, and the packet is being sent to our |
| 357 | * Ethernet address or a broadcast address, loopback a copy. | |
| 358 | * XXX To make a simplex device behave exactly like a duplex | |
| 359 | * device, we should copy in the case of sending to our own | |
| 360 | * ethernet address (thus letting the original actually appear | |
| 361 | * on the wire). However, we don't do that here for security | |
| 362 | * reasons and compatibility with the original behavior. | |
| 363 | */ | |
| 364 | if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) { | |
| 365 | int csum_flags = 0; | |
| 366 | ||
| 367 | if (m->m_pkthdr.csum_flags & CSUM_IP) | |
| f23061d4 | 368 | csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID); |
| 984263bc | 369 | if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) |
| f23061d4 | 370 | csum_flags |= (CSUM_DATA_VALID | CSUM_PSEUDO_HDR); |
| 984263bc MD |
371 | if ((m->m_flags & M_BCAST) || (loop_copy > 0)) { |
| 372 | struct mbuf *n; | |
| 373 | ||
| f23061d4 | 374 | if ((n = m_copypacket(m, MB_DONTWAIT)) != NULL) { |
| 984263bc MD |
375 | n->m_pkthdr.csum_flags |= csum_flags; |
| 376 | if (csum_flags & CSUM_DATA_VALID) | |
| 377 | n->m_pkthdr.csum_data = 0xffff; | |
| f23061d4 | 378 | if_simloop(ifp, n, dst->sa_family, hlen); |
| 984263bc MD |
379 | } else |
| 380 | ifp->if_iqdrops++; | |
| f23061d4 JH |
381 | } else if (bcmp(eh->ether_dhost, eh->ether_shost, |
| 382 | ETHER_ADDR_LEN) == 0) { | |
| 984263bc MD |
383 | m->m_pkthdr.csum_flags |= csum_flags; |
| 384 | if (csum_flags & CSUM_DATA_VALID) | |
| 385 | m->m_pkthdr.csum_data = 0xffff; | |
| f23061d4 | 386 | if_simloop(ifp, m, dst->sa_family, hlen); |
| 984263bc MD |
387 | return (0); /* XXX */ |
| 388 | } | |
| 389 | } | |
| 390 | ||
| 0d16ba1d | 391 | #ifdef CARP |
| ff54734e SZ |
392 | if (ifp->if_carp) { |
| 393 | /* | |
| 394 | * Hold BGL and recheck ifp->if_carp | |
| 395 | */ | |
| 396 | get_mplock(); | |
| 397 | if (ifp->if_carp && (error = carp_output(ifp, m, dst, NULL))) { | |
| 398 | rel_mplock(); | |
| 399 | goto bad; | |
| 400 | } | |
| 401 | rel_mplock(); | |
| 402 | } | |
| 0d16ba1d MD |
403 | #endif |
| 404 | ||
| 405 | ||
| 984263bc MD |
406 | /* Handle ng_ether(4) processing, if any */ |
| 407 | if (ng_ether_output_p != NULL) { | |
| ff54734e SZ |
408 | /* |
| 409 | * Hold BGL and recheck ng_ether_output_p | |
| 410 | */ | |
| 411 | get_mplock(); | |
| 412 | if (ng_ether_output_p != NULL) { | |
| 413 | if ((error = ng_ether_output_p(ifp, &m)) != 0) { | |
| 414 | rel_mplock(); | |
| 415 | goto bad; | |
| 416 | } | |
| 417 | if (m == NULL) { | |
| 418 | rel_mplock(); | |
| 419 | return (0); | |
| 420 | } | |
| 421 | } | |
| 422 | rel_mplock(); | |
| 984263bc MD |
423 | } |
| 424 | ||
| 425 | /* Continue with link-layer output */ | |
| 426 | return ether_output_frame(ifp, m); | |
| 0c3c561c JH |
427 | |
| 428 | bad: | |
| 429 | m_freem(m); | |
| 430 | return (error); | |
| 984263bc MD |
431 | } |
| 432 | ||
| 433 | /* | |
| 70d9a675 MD |
434 | * Returns the bridge interface an ifp is associated |
| 435 | * with. | |
| 436 | * | |
| 437 | * Only call if ifp->if_bridge != NULL. | |
| 438 | */ | |
| 439 | struct ifnet * | |
| 440 | ether_bridge_interface(struct ifnet *ifp) | |
| 441 | { | |
| 442 | if (bridge_interface_p) | |
| 443 | return(bridge_interface_p(ifp->if_bridge)); | |
| 444 | return (ifp); | |
| 445 | } | |
| 446 | ||
| 447 | /* | |
| 984263bc MD |
448 | * Ethernet link layer output routine to send a raw frame to the device. |
| 449 | * | |
| 450 | * This assumes that the 14 byte Ethernet header is present and contiguous | |
| a8d45119 | 451 | * in the first mbuf. |
| 984263bc MD |
452 | */ |
| 453 | int | |
| f23061d4 | 454 | ether_output_frame(struct ifnet *ifp, struct mbuf *m) |
| 984263bc | 455 | { |
| f23061d4 | 456 | struct ip_fw *rule = NULL; |
| 984263bc | 457 | int error = 0; |
| 4d723e5a | 458 | struct altq_pktattr pktattr; |
| 984263bc | 459 | |
| 2c9effcf | 460 | ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp); |
| 57dff79c | 461 | |
| eb241549 SZ |
462 | if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) { |
| 463 | struct m_tag *mtag; | |
| 464 | ||
| 465 | /* Extract info from dummynet tag */ | |
| 466 | mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL); | |
| 467 | KKASSERT(mtag != NULL); | |
| 84a3e25a | 468 | rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv; |
| eb241549 | 469 | KKASSERT(rule != NULL); |
| 4c7020ad SZ |
470 | |
| 471 | m_tag_delete(m, mtag); | |
| eb241549 | 472 | m->m_pkthdr.fw_flags &= ~DUMMYNET_MBUF_TAGGED; |
| 5fe66e68 | 473 | } |
| 984263bc | 474 | |
| 4d723e5a JS |
475 | if (ifq_is_enabled(&ifp->if_snd)) |
| 476 | altq_etherclassify(&ifp->if_snd, m, &pktattr); | |
| 4986965b | 477 | crit_enter(); |
| 984263bc MD |
478 | if (IPFW_LOADED && ether_ipfw != 0) { |
| 479 | struct ether_header save_eh, *eh; | |
| 480 | ||
| 481 | eh = mtod(m, struct ether_header *); | |
| 482 | save_eh = *eh; | |
| 483 | m_adj(m, ETHER_HDR_LEN); | |
| 90ca9293 | 484 | if (!ether_ipfw_chk(&m, ifp, &rule, eh)) { |
| 4986965b | 485 | crit_exit(); |
| 5fe66e68 | 486 | if (m != NULL) { |
| 984263bc | 487 | m_freem(m); |
| f23061d4 | 488 | return ENOBUFS; /* pkt dropped */ |
| 984263bc MD |
489 | } else |
| 490 | return 0; /* consumed e.g. in a pipe */ | |
| 491 | } | |
| 9b77ea6e | 492 | |
| 984263bc | 493 | /* packet was ok, restore the ethernet header */ |
| 9b77ea6e SZ |
494 | ether_restore_header(&m, eh, &save_eh); |
| 495 | if (m == NULL) { | |
| 496 | crit_exit(); | |
| 497 | return ENOBUFS; | |
| 984263bc MD |
498 | } |
| 499 | } | |
| 78195a76 | 500 | crit_exit(); |
| 984263bc MD |
501 | |
| 502 | /* | |
| 503 | * Queue message on interface, update output statistics if | |
| 504 | * successful, and start output if interface not yet active. | |
| 505 | */ | |
| 9db4b353 | 506 | error = ifq_dispatch(ifp, m, &pktattr); |
| 984263bc MD |
507 | return (error); |
| 508 | } | |
| 509 | ||
| 510 | /* | |
| 511 | * ipfw processing for ethernet packets (in and out). | |
| 5fe66e68 | 512 | * The second parameter is NULL from ether_demux(), and ifp from |
| a8d45119 | 513 | * ether_output_frame(). |
| 984263bc | 514 | */ |
| 5fe66e68 | 515 | static boolean_t |
| 90ca9293 SZ |
516 | ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst, struct ip_fw **rule, |
| 517 | const struct ether_header *eh) | |
| 984263bc | 518 | { |
| 29b27cb7 | 519 | struct ether_header save_eh = *eh; /* might be a ptr in *m0 */ |
| 984263bc | 520 | struct ip_fw_args args; |
| e5ecc832 | 521 | struct m_tag *mtag; |
| 29b27cb7 | 522 | struct mbuf *m; |
| f23061d4 | 523 | int i; |
| 984263bc MD |
524 | |
| 525 | if (*rule != NULL && fw_one_pass) | |
| 5fe66e68 | 526 | return TRUE; /* dummynet packet, already partially processed */ |
| 984263bc MD |
527 | |
| 528 | /* | |
| 90ca9293 | 529 | * I need some amount of data to be contiguous. |
| 984263bc | 530 | */ |
| f23061d4 | 531 | i = min((*m0)->m_pkthdr.len, max_protohdr); |
| 90ca9293 | 532 | if ((*m0)->m_len < i) { |
| 984263bc MD |
533 | *m0 = m_pullup(*m0, i); |
| 534 | if (*m0 == NULL) | |
| 5fe66e68 | 535 | return FALSE; |
| 984263bc MD |
536 | } |
| 537 | ||
| 5de23090 SZ |
538 | /* |
| 539 | * Clean up tags | |
| 540 | */ | |
| e5ecc832 JS |
541 | if ((mtag = m_tag_find(*m0, PACKET_TAG_IPFW_DIVERT, NULL)) != NULL) |
| 542 | m_tag_delete(*m0, mtag); | |
| 5de23090 SZ |
543 | if ((*m0)->m_pkthdr.fw_flags & IPFORWARD_MBUF_TAGGED) { |
| 544 | mtag = m_tag_find(*m0, PACKET_TAG_IPFORWARD, NULL); | |
| 545 | KKASSERT(mtag != NULL); | |
| 546 | m_tag_delete(*m0, mtag); | |
| 547 | (*m0)->m_pkthdr.fw_flags &= ~IPFORWARD_MBUF_TAGGED; | |
| 548 | } | |
| 549 | ||
| 550 | args.m = *m0; /* the packet we are looking at */ | |
| 551 | args.oif = dst; /* destination, if any */ | |
| 984263bc | 552 | args.rule = *rule; /* matching rule to restart */ |
| 984263bc MD |
553 | args.eh = &save_eh; /* MAC header for bridged/MAC packets */ |
| 554 | i = ip_fw_chk_ptr(&args); | |
| 555 | *m0 = args.m; | |
| 556 | *rule = args.rule; | |
| 557 | ||
| 29b27cb7 | 558 | if (*m0 == NULL) |
| 5fe66e68 | 559 | return FALSE; |
| 984263bc | 560 | |
| 29b27cb7 SZ |
561 | switch (i) { |
| 562 | case IP_FW_PASS: | |
| 5fe66e68 | 563 | return TRUE; |
| 984263bc | 564 | |
| 29b27cb7 SZ |
565 | case IP_FW_DIVERT: |
| 566 | case IP_FW_TEE: | |
| 567 | case IP_FW_DENY: | |
| 984263bc | 568 | /* |
| 29b27cb7 SZ |
569 | * XXX at some point add support for divert/forward actions. |
| 570 | * If none of the above matches, we have to drop the pkt. | |
| 984263bc | 571 | */ |
| 29b27cb7 | 572 | return FALSE; |
| 984263bc | 573 | |
| 29b27cb7 SZ |
574 | case IP_FW_DUMMYNET: |
| 575 | /* | |
| 576 | * Pass the pkt to dummynet, which consumes it. | |
| 577 | */ | |
| 90ca9293 SZ |
578 | m = *m0; /* pass the original to dummynet */ |
| 579 | *m0 = NULL; /* and nothing back to the caller */ | |
| 9b77ea6e SZ |
580 | |
| 581 | ether_restore_header(&m, eh, &save_eh); | |
| 582 | if (m == NULL) | |
| 583 | return FALSE; | |
| 584 | ||
| 29b27cb7 SZ |
585 | ip_fw_dn_io_ptr(m, args.cookie, |
| 586 | dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args); | |
| e4d4f9c3 | 587 | ip_dn_queue(m); |
| 5fe66e68 | 588 | return FALSE; |
| 29b27cb7 SZ |
589 | |
| 590 | default: | |
| 591 | panic("unknown ipfw return value: %d\n", i); | |
| 984263bc | 592 | } |
| 984263bc MD |
593 | } |
| 594 | ||
| 4853cd0f | 595 | static void |
| 68b67450 SZ |
596 | ether_input(struct ifnet *ifp, struct mbuf *m) |
| 597 | { | |
| 2eb0d069 | 598 | ether_input_chain(ifp, m, NULL, NULL); |
| 984263bc MD |
599 | } |
| 600 | ||
| 601 | /* | |
| 602 | * Perform common duties while attaching to interface list | |
| 603 | */ | |
| 604 | void | |
| 78195a76 | 605 | ether_ifattach(struct ifnet *ifp, uint8_t *lla, lwkt_serialize_t serializer) |
| 984263bc | 606 | { |
| 78195a76 MD |
607 | ether_ifattach_bpf(ifp, lla, DLT_EN10MB, sizeof(struct ether_header), |
| 608 | serializer); | |
| c0f6c904 JS |
609 | } |
| 610 | ||
| 611 | void | |
| 78195a76 MD |
612 | ether_ifattach_bpf(struct ifnet *ifp, uint8_t *lla, u_int dlt, u_int hdrlen, |
| 613 | lwkt_serialize_t serializer) | |
| c0f6c904 | 614 | { |
| 82ed7fc2 | 615 | struct sockaddr_dl *sdl; |
| 984263bc MD |
616 | |
| 617 | ifp->if_type = IFT_ETHER; | |
| c401f0fd | 618 | ifp->if_addrlen = ETHER_ADDR_LEN; |
| 5fe66e68 | 619 | ifp->if_hdrlen = ETHER_HDR_LEN; |
| 78195a76 | 620 | if_attach(ifp, serializer); |
| 984263bc | 621 | ifp->if_mtu = ETHERMTU; |
| 984263bc | 622 | if (ifp->if_baudrate == 0) |
| f23061d4 | 623 | ifp->if_baudrate = 10000000; |
| 5fe66e68 | 624 | ifp->if_output = ether_output; |
| 0b076e92 | 625 | ifp->if_input = ether_input; |
| 5fe66e68 JH |
626 | ifp->if_resolvemulti = ether_resolvemulti; |
| 627 | ifp->if_broadcastaddr = etherbroadcastaddr; | |
| f2682cb9 | 628 | sdl = IF_LLSOCKADDR(ifp); |
| 984263bc MD |
629 | sdl->sdl_type = IFT_ETHER; |
| 630 | sdl->sdl_alen = ifp->if_addrlen; | |
| 0a8b5977 | 631 | bcopy(lla, LLADDR(sdl), ifp->if_addrlen); |
| c568d5be JS |
632 | /* |
| 633 | * XXX Keep the current drivers happy. | |
| 634 | * XXX Remove once all drivers have been cleaned up | |
| 635 | */ | |
| 636 | if (lla != IFP2AC(ifp)->ac_enaddr) | |
| 637 | bcopy(lla, IFP2AC(ifp)->ac_enaddr, ifp->if_addrlen); | |
| c0f6c904 | 638 | bpfattach(ifp, dlt, hdrlen); |
| 984263bc MD |
639 | if (ng_ether_attach_p != NULL) |
| 640 | (*ng_ether_attach_p)(ifp); | |
| 267caeeb JS |
641 | |
| 642 | if_printf(ifp, "MAC address: %6D\n", lla, ":"); | |
| 984263bc MD |
643 | } |
| 644 | ||
| 645 | /* | |
| 646 | * Perform common duties while detaching an Ethernet interface | |
| 647 | */ | |
| 648 | void | |
| 0a8b5977 | 649 | ether_ifdetach(struct ifnet *ifp) |
| 984263bc | 650 | { |
| 45b8be9e | 651 | if_down(ifp); |
| 45b8be9e | 652 | |
| 984263bc MD |
653 | if (ng_ether_detach_p != NULL) |
| 654 | (*ng_ether_detach_p)(ifp); | |
| 0a8b5977 | 655 | bpfdetach(ifp); |
| 984263bc | 656 | if_detach(ifp); |
| 984263bc MD |
657 | } |
| 658 | ||
| 984263bc | 659 | int |
| f23061d4 | 660 | ether_ioctl(struct ifnet *ifp, int command, caddr_t data) |
| 984263bc MD |
661 | { |
| 662 | struct ifaddr *ifa = (struct ifaddr *) data; | |
| 663 | struct ifreq *ifr = (struct ifreq *) data; | |
| 664 | int error = 0; | |
| 665 | ||
| cbf2eda6 SZ |
666 | #define IF_INIT(ifp) \ |
| 667 | do { \ | |
| 668 | if (((ifp)->if_flags & IFF_UP) == 0) { \ | |
| 669 | (ifp)->if_flags |= IFF_UP; \ | |
| 670 | (ifp)->if_init((ifp)->if_softc); \ | |
| 671 | } \ | |
| 672 | } while (0) | |
| 673 | ||
| 2c9effcf | 674 | ASSERT_IFNET_SERIALIZED_ALL(ifp); |
| 78195a76 | 675 | |
| 984263bc MD |
676 | switch (command) { |
| 677 | case SIOCSIFADDR: | |
| 984263bc MD |
678 | switch (ifa->ifa_addr->sa_family) { |
| 679 | #ifdef INET | |
| 680 | case AF_INET: | |
| cbf2eda6 | 681 | IF_INIT(ifp); /* before arpwhohas */ |
| 984263bc MD |
682 | arp_ifinit(ifp, ifa); |
| 683 | break; | |
| 684 | #endif | |
| 685 | #ifdef IPX | |
| 686 | /* | |
| 687 | * XXX - This code is probably wrong | |
| 688 | */ | |
| 689 | case AF_IPX: | |
| 690 | { | |
| f23061d4 | 691 | struct ipx_addr *ina = &IA_SIPX(ifa)->sipx_addr; |
| 984263bc MD |
692 | struct arpcom *ac = IFP2AC(ifp); |
| 693 | ||
| 694 | if (ipx_nullhost(*ina)) | |
| f23061d4 JH |
695 | ina->x_host = *(union ipx_host *) ac->ac_enaddr; |
| 696 | else | |
| 697 | bcopy(ina->x_host.c_host, ac->ac_enaddr, | |
| 698 | sizeof ac->ac_enaddr); | |
| 984263bc | 699 | |
| cbf2eda6 | 700 | IF_INIT(ifp); /* Set new address. */ |
| 984263bc MD |
701 | break; |
| 702 | } | |
| 703 | #endif | |
| 984263bc | 704 | default: |
| cbf2eda6 | 705 | IF_INIT(ifp); |
| 984263bc MD |
706 | break; |
| 707 | } | |
| 708 | break; | |
| 709 | ||
| 710 | case SIOCGIFADDR: | |
| f23061d4 JH |
711 | bcopy(IFP2AC(ifp)->ac_enaddr, |
| 712 | ((struct sockaddr *)ifr->ifr_data)->sa_data, | |
| 713 | ETHER_ADDR_LEN); | |
| 984263bc MD |
714 | break; |
| 715 | ||
| 716 | case SIOCSIFMTU: | |
| 717 | /* | |
| 718 | * Set the interface MTU. | |
| 719 | */ | |
| 720 | if (ifr->ifr_mtu > ETHERMTU) { | |
| 721 | error = EINVAL; | |
| 722 | } else { | |
| 723 | ifp->if_mtu = ifr->ifr_mtu; | |
| 724 | } | |
| 725 | break; | |
| c2d9fd91 JS |
726 | default: |
| 727 | error = EINVAL; | |
| 728 | break; | |
| 984263bc MD |
729 | } |
| 730 | return (error); | |
| cbf2eda6 SZ |
731 | |
| 732 | #undef IF_INIT | |
| 984263bc MD |
733 | } |
| 734 | ||
| 735 | int | |
| f23061d4 JH |
736 | ether_resolvemulti( |
| 737 | struct ifnet *ifp, | |
| 738 | struct sockaddr **llsa, | |
| 739 | struct sockaddr *sa) | |
| 984263bc MD |
740 | { |
| 741 | struct sockaddr_dl *sdl; | |
| 742 | struct sockaddr_in *sin; | |
| 743 | #ifdef INET6 | |
| 744 | struct sockaddr_in6 *sin6; | |
| 745 | #endif | |
| 746 | u_char *e_addr; | |
| 747 | ||
| 748 | switch(sa->sa_family) { | |
| 749 | case AF_LINK: | |
| 750 | /* | |
| 751 | * No mapping needed. Just check that it's a valid MC address. | |
| 752 | */ | |
| 753 | sdl = (struct sockaddr_dl *)sa; | |
| 754 | e_addr = LLADDR(sdl); | |
| 755 | if ((e_addr[0] & 1) != 1) | |
| 756 | return EADDRNOTAVAIL; | |
| 757 | *llsa = 0; | |
| 758 | return 0; | |
| 759 | ||
| 760 | #ifdef INET | |
| 761 | case AF_INET: | |
| 762 | sin = (struct sockaddr_in *)sa; | |
| 763 | if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) | |
| 764 | return EADDRNOTAVAIL; | |
| 765 | MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR, | |
| 5fe66e68 | 766 | M_WAITOK | M_ZERO); |
| 984263bc MD |
767 | sdl->sdl_len = sizeof *sdl; |
| 768 | sdl->sdl_family = AF_LINK; | |
| 769 | sdl->sdl_index = ifp->if_index; | |
| 770 | sdl->sdl_type = IFT_ETHER; | |
| 771 | sdl->sdl_alen = ETHER_ADDR_LEN; | |
| 772 | e_addr = LLADDR(sdl); | |
| 773 | ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr); | |
| 774 | *llsa = (struct sockaddr *)sdl; | |
| 775 | return 0; | |
| 776 | #endif | |
| 777 | #ifdef INET6 | |
| 778 | case AF_INET6: | |
| 779 | sin6 = (struct sockaddr_in6 *)sa; | |
| 780 | if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { | |
| 781 | /* | |
| 782 | * An IP6 address of 0 means listen to all | |
| 783 | * of the Ethernet multicast address used for IP6. | |
| 784 | * (This is used for multicast routers.) | |
| 785 | */ | |
| 786 | ifp->if_flags |= IFF_ALLMULTI; | |
| 787 | *llsa = 0; | |
| 788 | return 0; | |
| 789 | } | |
| 790 | if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) | |
| 791 | return EADDRNOTAVAIL; | |
| 792 | MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR, | |
| 5fe66e68 | 793 | M_WAITOK | M_ZERO); |
| 984263bc MD |
794 | sdl->sdl_len = sizeof *sdl; |
| 795 | sdl->sdl_family = AF_LINK; | |
| 796 | sdl->sdl_index = ifp->if_index; | |
| 797 | sdl->sdl_type = IFT_ETHER; | |
| 798 | sdl->sdl_alen = ETHER_ADDR_LEN; | |
| 799 | e_addr = LLADDR(sdl); | |
| 800 | ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr); | |
| 801 | *llsa = (struct sockaddr *)sdl; | |
| 802 | return 0; | |
| 803 | #endif | |
| 804 | ||
| 805 | default: | |
| 806 | /* | |
| 807 | * Well, the text isn't quite right, but it's the name | |
| 808 | * that counts... | |
| 809 | */ | |
| 810 | return EAFNOSUPPORT; | |
| 811 | } | |
| 812 | } | |
| d6018c31 JS |
813 | |
| 814 | #if 0 | |
| 815 | /* | |
| 816 | * This is for reference. We have a table-driven version | |
| 817 | * of the little-endian crc32 generator, which is faster | |
| 818 | * than the double-loop. | |
| 819 | */ | |
| 820 | uint32_t | |
| 821 | ether_crc32_le(const uint8_t *buf, size_t len) | |
| 822 | { | |
| 823 | uint32_t c, crc, carry; | |
| 824 | size_t i, j; | |
| 825 | ||
| 826 | crc = 0xffffffffU; /* initial value */ | |
| 827 | ||
| 828 | for (i = 0; i < len; i++) { | |
| 829 | c = buf[i]; | |
| 830 | for (j = 0; j < 8; j++) { | |
| 831 | carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01); | |
| 832 | crc >>= 1; | |
| 833 | c >>= 1; | |
| 834 | if (carry) | |
| 835 | crc = (crc ^ ETHER_CRC_POLY_LE); | |
| 836 | } | |
| 837 | } | |
| 838 | ||
| f23061d4 | 839 | return (crc); |
| d6018c31 JS |
840 | } |
| 841 | #else | |
| 842 | uint32_t | |
| 843 | ether_crc32_le(const uint8_t *buf, size_t len) | |
| 844 | { | |
| 845 | static const uint32_t crctab[] = { | |
| 846 | 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac, | |
| 847 | 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c, | |
| 848 | 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, | |
| 849 | 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c | |
| 850 | }; | |
| 851 | uint32_t crc; | |
| 852 | size_t i; | |
| 853 | ||
| 854 | crc = 0xffffffffU; /* initial value */ | |
| 855 | ||
| 856 | for (i = 0; i < len; i++) { | |
| 857 | crc ^= buf[i]; | |
| 858 | crc = (crc >> 4) ^ crctab[crc & 0xf]; | |
| 859 | crc = (crc >> 4) ^ crctab[crc & 0xf]; | |
| 860 | } | |
| 861 | ||
| f23061d4 | 862 | return (crc); |
| d6018c31 JS |
863 | } |
| 864 | #endif | |
| 865 | ||
| 866 | uint32_t | |
| 867 | ether_crc32_be(const uint8_t *buf, size_t len) | |
| 868 | { | |
| 869 | uint32_t c, crc, carry; | |
| 870 | size_t i, j; | |
| 871 | ||
| 872 | crc = 0xffffffffU; /* initial value */ | |
| 873 | ||
| 874 | for (i = 0; i < len; i++) { | |
| 875 | c = buf[i]; | |
| 876 | for (j = 0; j < 8; j++) { | |
| 877 | carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01); | |
| 878 | crc <<= 1; | |
| 879 | c >>= 1; | |
| 880 | if (carry) | |
| 881 | crc = (crc ^ ETHER_CRC_POLY_BE) | carry; | |
| 882 | } | |
| 883 | } | |
| 884 | ||
| f23061d4 | 885 | return (crc); |
| d6018c31 | 886 | } |
| 4d723e5a | 887 | |
| 4d723e5a JS |
888 | /* |
| 889 | * find the size of ethernet header, and call classifier | |
| 890 | */ | |
| 891 | void | |
| 892 | altq_etherclassify(struct ifaltq *ifq, struct mbuf *m, | |
| 893 | struct altq_pktattr *pktattr) | |
| 894 | { | |
| 895 | struct ether_header *eh; | |
| 896 | uint16_t ether_type; | |
| 897 | int hlen, af, hdrsize; | |
| 898 | caddr_t hdr; | |
| 899 | ||
| 900 | hlen = sizeof(struct ether_header); | |
| 901 | eh = mtod(m, struct ether_header *); | |
| 902 | ||
| 903 | ether_type = ntohs(eh->ether_type); | |
| 904 | if (ether_type < ETHERMTU) { | |
| 905 | /* ick! LLC/SNAP */ | |
| 906 | struct llc *llc = (struct llc *)(eh + 1); | |
| 907 | hlen += 8; | |
| 908 | ||
| 909 | if (m->m_len < hlen || | |
| 910 | llc->llc_dsap != LLC_SNAP_LSAP || | |
| 911 | llc->llc_ssap != LLC_SNAP_LSAP || | |
| 912 | llc->llc_control != LLC_UI) | |
| 913 | goto bad; /* not snap! */ | |
| 914 | ||
| 915 | ether_type = ntohs(llc->llc_un.type_snap.ether_type); | |
| 916 | } | |
| 917 | ||
| 918 | if (ether_type == ETHERTYPE_IP) { | |
| 919 | af = AF_INET; | |
| 920 | hdrsize = 20; /* sizeof(struct ip) */ | |
| 921 | #ifdef INET6 | |
| 922 | } else if (ether_type == ETHERTYPE_IPV6) { | |
| 923 | af = AF_INET6; | |
| 924 | hdrsize = 40; /* sizeof(struct ip6_hdr) */ | |
| 925 | #endif | |
| 926 | } else | |
| 927 | goto bad; | |
| 928 | ||
| 929 | while (m->m_len <= hlen) { | |
| 930 | hlen -= m->m_len; | |
| 931 | m = m->m_next; | |
| 932 | } | |
| 933 | hdr = m->m_data + hlen; | |
| 934 | if (m->m_len < hlen + hdrsize) { | |
| 935 | /* | |
| 936 | * ip header is not in a single mbuf. this should not | |
| 937 | * happen in the current code. | |
| 938 | * (todo: use m_pulldown in the future) | |
| 939 | */ | |
| 940 | goto bad; | |
| 941 | } | |
| 942 | m->m_data += hlen; | |
| 943 | m->m_len -= hlen; | |
| 944 | ifq_classify(ifq, m, af, pktattr); | |
| 945 | m->m_data -= hlen; | |
| 946 | m->m_len += hlen; | |
| 947 | ||
| 948 | return; | |
| 949 | ||
| 950 | bad: | |
| 951 | pktattr->pattr_class = NULL; | |
| 952 | pktattr->pattr_hdr = NULL; | |
| 953 | pktattr->pattr_af = AF_UNSPEC; | |
| 954 | } | |
| 9b77ea6e SZ |
955 | |
| 956 | static void | |
| 957 | ether_restore_header(struct mbuf **m0, const struct ether_header *eh, | |
| 958 | const struct ether_header *save_eh) | |
| 959 | { | |
| 960 | struct mbuf *m = *m0; | |
| 961 | ||
| 962 | ether_restore_hdr++; | |
| 963 | ||
| 964 | /* | |
| 965 | * Prepend the header, optimize for the common case of | |
| 966 | * eh pointing into the mbuf. | |
| 967 | */ | |
| 968 | if ((const void *)(eh + 1) == (void *)m->m_data) { | |
| 969 | m->m_data -= ETHER_HDR_LEN; | |
| 970 | m->m_len += ETHER_HDR_LEN; | |
| 971 | m->m_pkthdr.len += ETHER_HDR_LEN; | |
| 972 | } else { | |
| 973 | ether_prepend_hdr++; | |
| 974 | ||
| 975 | M_PREPEND(m, ETHER_HDR_LEN, MB_DONTWAIT); | |
| 976 | if (m != NULL) { | |
| 3d26a382 | 977 | bcopy(save_eh, mtod(m, struct ether_header *), |
| 9b77ea6e SZ |
978 | ETHER_HDR_LEN); |
| 979 | } | |
| 980 | } | |
| 981 | *m0 = m; | |
| 982 | } | |
| 68b67450 | 983 | |
| 68b67450 SZ |
984 | static void |
| 985 | ether_input_ipifunc(void *arg) | |
| 986 | { | |
| 987 | struct mbuf *m, *next; | |
| c3c96e44 | 988 | lwkt_port_t port = cpu_portfn(mycpu->gd_cpuid); |
| 68b67450 SZ |
989 | |
| 990 | m = arg; | |
| 991 | do { | |
| 992 | next = m->m_nextpkt; | |
| 993 | m->m_nextpkt = NULL; | |
| 002c1265 | 994 | lwkt_sendmsg(port, &m->m_hdr.mh_netmsg.base.lmsg); |
| 68b67450 SZ |
995 | m = next; |
| 996 | } while (m != NULL); | |
| 997 | } | |
| 998 | ||
| 999 | void | |
| 1000 | ether_input_dispatch(struct mbuf_chain *chain) | |
| 1001 | { | |
| 1002 | #ifdef SMP | |
| 1003 | int i; | |
| 1004 | ||
| f3e0b5f0 | 1005 | logether(disp_beg, NULL); |
| 68b67450 SZ |
1006 | for (i = 0; i < ncpus; ++i) { |
| 1007 | if (chain[i].mc_head != NULL) { | |
| 1008 | lwkt_send_ipiq(globaldata_find(i), | |
| c3c96e44 | 1009 | ether_input_ipifunc, chain[i].mc_head); |
| 68b67450 SZ |
1010 | } |
| 1011 | } | |
| 1012 | #else | |
| f3e0b5f0 | 1013 | logether(disp_beg, NULL); |
| b358f907 SZ |
1014 | if (chain->mc_head != NULL) |
| 1015 | ether_input_ipifunc(chain->mc_head); | |
| 68b67450 | 1016 | #endif |
| f3e0b5f0 | 1017 | logether(disp_end, NULL); |
| 68b67450 SZ |
1018 | } |
| 1019 | ||
| 57ccf5a2 SZ |
1020 | void |
| 1021 | ether_input_chain_init(struct mbuf_chain *chain) | |
| 1022 | { | |
| 1023 | #ifdef SMP | |
| 1024 | int i; | |
| 1025 | ||
| 1026 | for (i = 0; i < ncpus; ++i) | |
| 1027 | chain[i].mc_head = chain[i].mc_tail = NULL; | |
| 1028 | #else | |
| 1029 | chain->mc_head = chain->mc_tail = NULL; | |
| 1030 | #endif | |
| 1031 | } | |
| 1032 | ||
| 5f32d321 SZ |
1033 | /* |
| 1034 | * Upper layer processing for a received Ethernet packet. | |
| 1035 | */ | |
| 297c8124 | 1036 | void |
| 29bc1092 SZ |
1037 | ether_demux_oncpu(struct ifnet *ifp, struct mbuf *m) |
| 1038 | { | |
| 1039 | struct ether_header *eh; | |
| 21b74198 | 1040 | int isr, discard = 0; |
| 29bc1092 SZ |
1041 | u_short ether_type; |
| 1042 | struct ip_fw *rule = NULL; | |
| 29bc1092 SZ |
1043 | #ifdef NETATALK |
| 1044 | struct llc *l; | |
| 1045 | #endif | |
| 1046 | ||
| 1047 | M_ASSERTPKTHDR(m); | |
| 1048 | KASSERT(m->m_len >= ETHER_HDR_LEN, | |
| 1049 | ("ether header is no contiguous!\n")); | |
| 1050 | ||
| 1051 | eh = mtod(m, struct ether_header *); | |
| 1052 | ||
| eb241549 SZ |
1053 | if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) { |
| 1054 | struct m_tag *mtag; | |
| 1055 | ||
| 1056 | /* Extract info from dummynet tag */ | |
| 1057 | mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL); | |
| 1058 | KKASSERT(mtag != NULL); | |
| 29bc1092 | 1059 | rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv; |
| eb241549 SZ |
1060 | KKASSERT(rule != NULL); |
| 1061 | ||
| 29bc1092 | 1062 | m_tag_delete(m, mtag); |
| eb241549 SZ |
1063 | m->m_pkthdr.fw_flags &= ~DUMMYNET_MBUF_TAGGED; |
| 1064 | ||
| 1065 | /* packet is passing the second time */ | |
| 29bc1092 | 1066 | goto post_stats; |
| eb241549 | 1067 | } |
| 29bc1092 SZ |
1068 | |
| 1069 | #ifdef CARP | |
| 1070 | /* | |
| 1071 | * XXX: Okay, we need to call carp_forus() and - if it is for | |
| 1072 | * us jump over code that does the normal check | |
| 1073 | * "ac_enaddr == ether_dhost". The check sequence is a bit | |
| 1074 | * different from OpenBSD, so we jump over as few code as | |
| 1075 | * possible, to catch _all_ sanity checks. This needs | |
| 1076 | * evaluation, to see if the carp ether_dhost values break any | |
| 1077 | * of these checks! | |
| 1078 | */ | |
| c6690c74 SZ |
1079 | if (ifp->if_carp) { |
| 1080 | /* | |
| 1081 | * Hold BGL and recheck ifp->if_carp | |
| 1082 | */ | |
| 1083 | get_mplock(); | |
| 1084 | if (ifp->if_carp && carp_forus(ifp->if_carp, eh->ether_dhost)) { | |
| 1085 | rel_mplock(); | |
| 1086 | goto post_stats; | |
| 1087 | } | |
| 1088 | rel_mplock(); | |
| 1089 | } | |
| 29bc1092 SZ |
1090 | #endif |
| 1091 | ||
| 1092 | /* | |
| 469c71d1 SZ |
1093 | * We got a packet which was unicast to a different Ethernet |
| 1094 | * address. If the driver is working properly, then this | |
| 1095 | * situation can only happen when the interface is in | |
| 1096 | * promiscuous mode. We defer the packet discarding until the | |
| 1097 | * vlan processing is done, so that vlan/bridge or vlan/netgraph | |
| 1098 | * could work. | |
| 29bc1092 SZ |
1099 | */ |
| 1100 | if (((ifp->if_flags & (IFF_PROMISC | IFF_PPROMISC)) == IFF_PROMISC) && | |
| d1859e78 | 1101 | !ETHER_IS_MULTICAST(eh->ether_dhost) && |
| 70d9a675 MD |
1102 | bcmp(eh->ether_dhost, IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN)) { |
| 1103 | if (ether_debug & 1) { | |
| 1104 | kprintf("%02x:%02x:%02x:%02x:%02x:%02x " | |
| 1105 | "%02x:%02x:%02x:%02x:%02x:%02x " | |
| 1106 | "%04x vs %02x:%02x:%02x:%02x:%02x:%02x\n", | |
| 1107 | eh->ether_dhost[0], | |
| 1108 | eh->ether_dhost[1], | |
| 1109 | eh->ether_dhost[2], | |
| 1110 | eh->ether_dhost[3], | |
| 1111 | eh->ether_dhost[4], | |
| 1112 | eh->ether_dhost[5], | |
| 1113 | eh->ether_shost[0], | |
| 1114 | eh->ether_shost[1], | |
| 1115 | eh->ether_shost[2], | |
| 1116 | eh->ether_shost[3], | |
| 1117 | eh->ether_shost[4], | |
| 1118 | eh->ether_shost[5], | |
| 1119 | eh->ether_type, | |
| 1120 | ((u_char *)IFP2AC(ifp)->ac_enaddr)[0], | |
| 1121 | ((u_char *)IFP2AC(ifp)->ac_enaddr)[1], | |
| 1122 | ((u_char *)IFP2AC(ifp)->ac_enaddr)[2], | |
| 1123 | ((u_char *)IFP2AC(ifp)->ac_enaddr)[3], | |
| 1124 | ((u_char *)IFP2AC(ifp)->ac_enaddr)[4], | |
| 1125 | ((u_char *)IFP2AC(ifp)->ac_enaddr)[5] | |
| 1126 | ); | |
| 1127 | } | |
| 1128 | if ((ether_debug & 2) == 0) | |
| 1129 | discard = 1; | |
| 1130 | } | |
| 29bc1092 SZ |
1131 | |
| 1132 | post_stats: | |
| 469c71d1 | 1133 | if (IPFW_LOADED && ether_ipfw != 0 && !discard) { |
| 29bc1092 SZ |
1134 | struct ether_header save_eh = *eh; |
| 1135 | ||
| 1136 | /* XXX old crufty stuff, needs to be removed */ | |
| 1137 | m_adj(m, sizeof(struct ether_header)); | |
| 1138 | ||
| 1139 | if (!ether_ipfw_chk(&m, NULL, &rule, eh)) { | |
| 1140 | m_freem(m); | |
| 1141 | return; | |
| 1142 | } | |
| 1143 | ||
| 1144 | ether_restore_header(&m, eh, &save_eh); | |
| 1145 | if (m == NULL) | |
| 1146 | return; | |
| 1147 | eh = mtod(m, struct ether_header *); | |
| 1148 | } | |
| 1149 | ||
| 1150 | ether_type = ntohs(eh->ether_type); | |
| 1151 | KKASSERT(ether_type != ETHERTYPE_VLAN); | |
| 1152 | ||
| 1153 | if (m->m_flags & M_VLANTAG) { | |
| b327296f SZ |
1154 | void (*vlan_input_func)(struct mbuf *); |
| 1155 | ||
| 1156 | vlan_input_func = vlan_input_p; | |
| 1157 | if (vlan_input_func != NULL) { | |
| 1158 | vlan_input_func(m); | |
| 29bc1092 SZ |
1159 | } else { |
| 1160 | m->m_pkthdr.rcvif->if_noproto++; | |
| 1161 | m_freem(m); | |
| 1162 | } | |
| 29bc1092 SZ |
1163 | return; |
| 1164 | } | |
| 1165 | ||
| 4d895293 | 1166 | /* |
| 469c71d1 SZ |
1167 | * If we have been asked to discard this packet |
| 1168 | * (e.g. not for us), drop it before entering | |
| 1169 | * the upper layer. | |
| 1170 | */ | |
| 1171 | if (discard) { | |
| 1172 | m_freem(m); | |
| 1173 | return; | |
| 1174 | } | |
| 1175 | ||
| 1176 | /* | |
| 4d895293 SZ |
1177 | * Clear protocol specific flags, |
| 1178 | * before entering the upper layer. | |
| 1179 | */ | |
| da1604af | 1180 | m->m_flags &= ~M_ETHER_FLAGS; |
| 4d895293 SZ |
1181 | |
| 1182 | /* Strip ethernet header. */ | |
| 29bc1092 | 1183 | m_adj(m, sizeof(struct ether_header)); |
| 4d895293 | 1184 | |
| 29bc1092 SZ |
1185 | switch (ether_type) { |
| 1186 | #ifdef INET | |
| 1187 | case ETHERTYPE_IP: | |
| 8697599b | 1188 | if ((m->m_flags & M_LENCHECKED) == 0) { |
| c3c96e44 | 1189 | if (!ip_lengthcheck(&m, 0)) |
| 8697599b SZ |
1190 | return; |
| 1191 | } | |
| 297c8124 | 1192 | if (ipflow_fastforward(m)) |
| 29bc1092 | 1193 | return; |
| 29bc1092 SZ |
1194 | isr = NETISR_IP; |
| 1195 | break; | |
| 1196 | ||
| 1197 | case ETHERTYPE_ARP: | |
| 1198 | if (ifp->if_flags & IFF_NOARP) { | |
| 1199 | /* Discard packet if ARP is disabled on interface */ | |
| 1200 | m_freem(m); | |
| 1201 | return; | |
| 1202 | } | |
| 1203 | isr = NETISR_ARP; | |
| 1204 | break; | |
| 1205 | #endif | |
| 1206 | ||
| 1207 | #ifdef INET6 | |
| 1208 | case ETHERTYPE_IPV6: | |
| 1209 | isr = NETISR_IPV6; | |
| 1210 | break; | |
| 1211 | #endif | |
| 1212 | ||
| 1213 | #ifdef IPX | |
| 1214 | case ETHERTYPE_IPX: | |
| c6690c74 SZ |
1215 | if (ef_inputp) { |
| 1216 | /* | |
| 1217 | * Hold BGL and recheck ef_inputp | |
| 1218 | */ | |
| 1219 | get_mplock(); | |
| 1220 | if (ef_inputp && ef_inputp(ifp, eh, m) == 0) { | |
| 1221 | rel_mplock(); | |
| 1222 | return; | |
| 1223 | } | |
| 1224 | rel_mplock(); | |
| 1225 | } | |
| 29bc1092 SZ |
1226 | isr = NETISR_IPX; |
| 1227 | break; | |
| 1228 | #endif | |
| 1229 | ||
| 29bc1092 SZ |
1230 | #ifdef NETATALK |
| 1231 | case ETHERTYPE_AT: | |
| 1232 | isr = NETISR_ATALK1; | |
| 1233 | break; | |
| 1234 | case ETHERTYPE_AARP: | |
| 1235 | isr = NETISR_AARP; | |
| 1236 | break; | |
| 1237 | #endif | |
| 1238 | ||
| a020e9d5 SZ |
1239 | #ifdef MPLS |
| 1240 | case ETHERTYPE_MPLS: | |
| 1241 | case ETHERTYPE_MPLS_MCAST: | |
| 50098e2e | 1242 | /* Should have been set by ether_input_chain(). */ |
| cb8d752c | 1243 | KKASSERT(m->m_flags & M_MPLSLABELED); |
| a020e9d5 SZ |
1244 | isr = NETISR_MPLS; |
| 1245 | break; | |
| 1246 | #endif | |
| 1247 | ||
| 29bc1092 SZ |
1248 | default: |
| 1249 | /* | |
| 1250 | * The accurate msgport is not determined before | |
| 1251 | * we reach here, so redo the dispatching | |
| 1252 | */ | |
| 29bc1092 | 1253 | #ifdef IPX |
| c6690c74 SZ |
1254 | if (ef_inputp) { |
| 1255 | /* | |
| 1256 | * Hold BGL and recheck ef_inputp | |
| 1257 | */ | |
| 1258 | get_mplock(); | |
| 1259 | if (ef_inputp && ef_inputp(ifp, eh, m) == 0) { | |
| 1260 | rel_mplock(); | |
| 1261 | return; | |
| 1262 | } | |
| 1263 | rel_mplock(); | |
| 1264 | } | |
| 29bc1092 | 1265 | #endif |
| 29bc1092 SZ |
1266 | #ifdef NETATALK |
| 1267 | if (ether_type > ETHERMTU) | |
| 1268 | goto dropanyway; | |
| 1269 | l = mtod(m, struct llc *); | |
| 1270 | if (l->llc_dsap == LLC_SNAP_LSAP && | |
| 1271 | l->llc_ssap == LLC_SNAP_LSAP && | |
| 1272 | l->llc_control == LLC_UI) { | |
| 1273 | if (bcmp(&(l->llc_snap_org_code)[0], at_org_code, | |
| 1274 | sizeof at_org_code) == 0 && | |
| 1275 | ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) { | |
| 1276 | m_adj(m, sizeof(struct llc)); | |
| 1277 | isr = NETISR_ATALK2; | |
| 1278 | break; | |
| 1279 | } | |
| 1280 | if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code, | |
| 1281 | sizeof aarp_org_code) == 0 && | |
| 1282 | ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) { | |
| 1283 | m_adj(m, sizeof(struct llc)); | |
| 1284 | isr = NETISR_AARP; | |
| 1285 | break; | |
| 1286 | } | |
| 1287 | } | |
| 1288 | dropanyway: | |
| 1289 | #endif | |
| c6690c74 SZ |
1290 | if (ng_ether_input_orphan_p != NULL) { |
| 1291 | /* | |
| 1292 | * Hold BGL and recheck ng_ether_input_orphan_p | |
| 1293 | */ | |
| 1294 | get_mplock(); | |
| 1295 | if (ng_ether_input_orphan_p != NULL) { | |
| 1296 | ng_ether_input_orphan_p(ifp, m, eh); | |
| 1297 | rel_mplock(); | |
| 1298 | return; | |
| 1299 | } | |
| 1300 | rel_mplock(); | |
| 1301 | } | |
| 1302 | m_freem(m); | |
| 29bc1092 SZ |
1303 | return; |
| 1304 | } | |
| 1305 | ||
| c3c96e44 | 1306 | netisr_queue(isr, m); |
| 29bc1092 SZ |
1307 | } |
| 1308 | ||
| 5f32d321 SZ |
1309 | /* |
| 1310 | * First we perform any link layer operations, then continue to the | |
| 1311 | * upper layers with ether_demux_oncpu(). | |
| 1312 | */ | |
| c3c96e44 | 1313 | static void |
| 29bc1092 SZ |
1314 | ether_input_oncpu(struct ifnet *ifp, struct mbuf *m) |
| 1315 | { | |
| 160af078 SZ |
1316 | if ((ifp->if_flags & (IFF_UP | IFF_MONITOR)) != IFF_UP) { |
| 1317 | /* | |
| 1318 | * Receiving interface's flags are changed, when this | |
| 1319 | * packet is waiting for processing; discard it. | |
| 1320 | */ | |
| 1321 | m_freem(m); | |
| 1322 | return; | |
| 1323 | } | |
| 1324 | ||
| 29bc1092 SZ |
1325 | /* |
| 1326 | * Tap the packet off here for a bridge. bridge_input() | |
| 1327 | * will return NULL if it has consumed the packet, otherwise | |
| 1328 | * it gets processed as normal. Note that bridge_input() | |
| 1329 | * will always return the original packet if we need to | |
| 1330 | * process it locally. | |
| 1331 | */ | |
| 1332 | if (ifp->if_bridge) { | |
| 1333 | KASSERT(bridge_input_p != NULL, | |
| 1334 | ("%s: if_bridge not loaded!", __func__)); | |
| 1335 | ||
| 0899cb3e SZ |
1336 | if(m->m_flags & M_ETHER_BRIDGED) { |
| 1337 | m->m_flags &= ~M_ETHER_BRIDGED; | |
| 29bc1092 | 1338 | } else { |
| 29bc1092 SZ |
1339 | m = bridge_input_p(ifp, m); |
| 1340 | if (m == NULL) | |
| 1341 | return; | |
| 1342 | ||
| 1343 | KASSERT(ifp == m->m_pkthdr.rcvif, | |
| 1344 | ("bridge_input_p changed rcvif\n")); | |
| 1345 | } | |
| 1346 | } | |
| 1347 | ||
| 1348 | /* Handle ng_ether(4) processing, if any */ | |
| 1349 | if (ng_ether_input_p != NULL) { | |
| c6690c74 SZ |
1350 | /* |
| 1351 | * Hold BGL and recheck ng_ether_input_p | |
| 1352 | */ | |
| 1353 | get_mplock(); | |
| 1354 | if (ng_ether_input_p != NULL) | |
| 1355 | ng_ether_input_p(ifp, &m); | |
| 1356 | rel_mplock(); | |
| 1357 | ||
| 29bc1092 SZ |
1358 | if (m == NULL) |
| 1359 | return; | |
| 1360 | } | |
| 1361 | ||
| 1362 | /* Continue with upper layer processing */ | |
| 1363 | ether_demux_oncpu(ifp, m); | |
| 1364 | } | |
| 1365 | ||
| b9ed4403 SZ |
1366 | /* |
| 1367 | * Perform certain functions of ether_input_chain(): | |
| 1368 | * - Test IFF_UP | |
| 1369 | * - Update statistics | |
| 1370 | * - Run bpf(4) tap if requested | |
| 1371 | * Then pass the packet to ether_input_oncpu(). | |
| 1372 | * | |
| 1373 | * This function should be used by pseudo interface (e.g. vlan(4)), | |
| 1374 | * when it tries to claim that the packet is received by it. | |
| 4ee4f753 MD |
1375 | * |
| 1376 | * REINPUT_KEEPRCVIF | |
| 1377 | * REINPUT_RUNBPF | |
| b9ed4403 SZ |
1378 | */ |
| 1379 | void | |
| 4ee4f753 | 1380 | ether_reinput_oncpu(struct ifnet *ifp, struct mbuf *m, int reinput_flags) |
| b9ed4403 SZ |
1381 | { |
| 1382 | /* Discard packet if interface is not up */ | |
| 1383 | if (!(ifp->if_flags & IFF_UP)) { | |
| 1384 | m_freem(m); | |
| 1385 | return; | |
| 1386 | } | |
| 1387 | ||
| 4ee4f753 MD |
1388 | /* |
| 1389 | * Change receiving interface. The bridge will often pass a flag to | |
| 1390 | * ask that this not be done so ARPs get applied to the correct | |
| 1391 | * side. | |
| 1392 | */ | |
| 1393 | if ((reinput_flags & REINPUT_KEEPRCVIF) == 0 || | |
| 1394 | m->m_pkthdr.rcvif == NULL) { | |
| 1395 | m->m_pkthdr.rcvif = ifp; | |
| 1396 | } | |
| b9ed4403 SZ |
1397 | |
| 1398 | /* Update statistics */ | |
| 1399 | ifp->if_ipackets++; | |
| 1400 | ifp->if_ibytes += m->m_pkthdr.len; | |
| 1401 | if (m->m_flags & (M_MCAST | M_BCAST)) | |
| 1402 | ifp->if_imcasts++; | |
| 1403 | ||
| 4ee4f753 | 1404 | if (reinput_flags & REINPUT_RUNBPF) |
| b9ed4403 SZ |
1405 | BPF_MTAP(ifp, m); |
| 1406 | ||
| 1407 | ether_input_oncpu(ifp, m); | |
| 1408 | } | |
| 1409 | ||
| 057441be SZ |
1410 | static __inline boolean_t |
| 1411 | ether_vlancheck(struct mbuf **m0) | |
| 1412 | { | |
| 1413 | struct mbuf *m = *m0; | |
| 1414 | struct ether_header *eh; | |
| 1415 | uint16_t ether_type; | |
| 1416 | ||
| 1417 | eh = mtod(m, struct ether_header *); | |
| 1418 | ether_type = ntohs(eh->ether_type); | |
| 1419 | ||
| 1420 | if (ether_type == ETHERTYPE_VLAN && (m->m_flags & M_VLANTAG) == 0) { | |
| 1421 | /* | |
| 1422 | * Extract vlan tag if hardware does not do it for us | |
| 1423 | */ | |
| 1424 | vlan_ether_decap(&m); | |
| 1425 | if (m == NULL) | |
| 1426 | goto failed; | |
| 1427 | ||
| 1428 | eh = mtod(m, struct ether_header *); | |
| 1429 | ether_type = ntohs(eh->ether_type); | |
| 1430 | } | |
| 1431 | ||
| 1432 | if (ether_type == ETHERTYPE_VLAN && (m->m_flags & M_VLANTAG)) { | |
| 1433 | /* | |
| 1434 | * To prevent possible dangerous recursion, | |
| 1435 | * we don't do vlan-in-vlan | |
| 1436 | */ | |
| 1437 | m->m_pkthdr.rcvif->if_noproto++; | |
| 1438 | goto failed; | |
| 1439 | } | |
| 1440 | KKASSERT(ether_type != ETHERTYPE_VLAN); | |
| 1441 | ||
| da1604af | 1442 | m->m_flags |= M_ETHER_VLANCHECKED; |
| 057441be SZ |
1443 | *m0 = m; |
| 1444 | return TRUE; | |
| 1445 | failed: | |
| 1446 | if (m != NULL) | |
| 1447 | m_freem(m); | |
| 1448 | *m0 = NULL; | |
| 1449 | return FALSE; | |
| 1450 | } | |
| 1451 | ||
| 29bc1092 | 1452 | static void |
| 002c1265 | 1453 | ether_input_handler(netmsg_t nmsg) |
| 29bc1092 | 1454 | { |
| 002c1265 | 1455 | struct netmsg_packet *nmp = &nmsg->packet; /* actual size */ |
| 828c9923 | 1456 | struct ether_header *eh; |
| 29bc1092 SZ |
1457 | struct ifnet *ifp; |
| 1458 | struct mbuf *m; | |
| 1459 | ||
| 1460 | m = nmp->nm_packet; | |
| 1461 | M_ASSERTPKTHDR(m); | |
| 1462 | ifp = m->m_pkthdr.rcvif; | |
| 1463 | ||
| 828c9923 SZ |
1464 | eh = mtod(m, struct ether_header *); |
| 1465 | if (ETHER_IS_MULTICAST(eh->ether_dhost)) { | |
| 1466 | if (bcmp(ifp->if_broadcastaddr, eh->ether_dhost, | |
| 1467 | ifp->if_addrlen) == 0) | |
| 1468 | m->m_flags |= M_BCAST; | |
| 1469 | else | |
| 1470 | m->m_flags |= M_MCAST; | |
| 1471 | ifp->if_imcasts++; | |
| 1472 | } | |
| 1473 | ||
| da1604af SZ |
1474 | if ((m->m_flags & M_ETHER_VLANCHECKED) == 0) { |
| 1475 | if (!ether_vlancheck(&m)) { | |
| 1476 | KKASSERT(m == NULL); | |
| 1477 | return; | |
| 1478 | } | |
| 1479 | } | |
| 1480 | ||
| 29bc1092 SZ |
1481 | ether_input_oncpu(ifp, m); |
| 1482 | } | |
| 1483 | ||
| c3c96e44 MD |
1484 | /* |
| 1485 | * Send the packet to the target msgport or queue it into 'chain'. | |
| 21b74198 MD |
1486 | * |
| 1487 | * At this point the packet had better be characterized (M_HASH set), | |
| 1488 | * so we know which cpu to send it to. | |
| c3c96e44 MD |
1489 | */ |
| 1490 | static void | |
| 1491 | ether_dispatch(int isr, struct mbuf *m, struct mbuf_chain *chain) | |
| 29bc1092 SZ |
1492 | { |
| 1493 | struct netmsg_packet *pmsg; | |
| 1494 | ||
| c3c96e44 | 1495 | KKASSERT(m->m_flags & M_HASH); |
| 29bc1092 | 1496 | pmsg = &m->m_hdr.mh_netmsg; |
| 002c1265 | 1497 | netmsg_init(&pmsg->base, NULL, &netisr_apanic_rport, |
| c3c96e44 | 1498 | 0, ether_input_handler); |
| 29bc1092 | 1499 | pmsg->nm_packet = m; |
| 002c1265 | 1500 | pmsg->base.lmsg.u.ms_result = isr; |
| 74f66604 SZ |
1501 | |
| 1502 | if (chain != NULL) { | |
| c3c96e44 | 1503 | int cpuid = m->m_pkthdr.hash; |
| 74f66604 | 1504 | struct mbuf_chain *c; |
| 74f66604 SZ |
1505 | |
| 1506 | c = &chain[cpuid]; | |
| 1507 | if (c->mc_head == NULL) { | |
| 1508 | c->mc_head = c->mc_tail = m; | |
| 1509 | } else { | |
| 1510 | c->mc_tail->m_nextpkt = m; | |
| 1511 | c->mc_tail = m; | |
| 1512 | } | |
| 1513 | m->m_nextpkt = NULL; | |
| 1514 | } else { | |
| 002c1265 | 1515 | lwkt_sendmsg(cpu_portfn(m->m_pkthdr.hash), &pmsg->base.lmsg); |
| 74f66604 SZ |
1516 | } |
| 1517 | } | |
| 1518 | ||
| 62f35c44 SZ |
1519 | /* |
| 1520 | * Process a received Ethernet packet. | |
| 1521 | * | |
| 1522 | * The ethernet header is assumed to be in the mbuf so the caller | |
| 1523 | * MUST MAKE SURE that there are at least sizeof(struct ether_header) | |
| 1524 | * bytes in the first mbuf. | |
| 1525 | * | |
| 62f35c44 | 1526 | * - If 'chain' is NULL, this ether frame is sent to the target msgport |
| 50098e2e | 1527 | * immediately. This situation happens when ether_input_chain is |
| 62f35c44 | 1528 | * accessed through ifnet.if_input. |
| c3c96e44 | 1529 | * |
| 62f35c44 SZ |
1530 | * - If 'chain' is not NULL, this ether frame is queued to the 'chain' |
| 1531 | * bucket indexed by the target msgport's cpuid and the target msgport | |
| 50098e2e | 1532 | * is saved in mbuf's m_pkthdr.m_head. Caller of ether_input_chain |
| 62f35c44 SZ |
1533 | * must initialize 'chain' by calling ether_input_chain_init(). |
| 1534 | * ether_input_dispatch must be called later to send ether frames | |
| 1535 | * queued on 'chain' to their target msgport. | |
| 1536 | */ | |
| 29bc1092 | 1537 | void |
| 2eb0d069 SZ |
1538 | ether_input_chain(struct ifnet *ifp, struct mbuf *m, const struct pktinfo *pi, |
| 1539 | struct mbuf_chain *chain) | |
| 29bc1092 | 1540 | { |
| c3c96e44 | 1541 | struct ether_header *eh; |
| 29bc1092 SZ |
1542 | uint16_t ether_type; |
| 1543 | int isr; | |
| 1544 | ||
| 29bc1092 SZ |
1545 | M_ASSERTPKTHDR(m); |
| 1546 | ||
| 1547 | /* Discard packet if interface is not up */ | |
| 1548 | if (!(ifp->if_flags & IFF_UP)) { | |
| 1549 | m_freem(m); | |
| 1550 | return; | |
| 1551 | } | |
| 1552 | ||
| 1553 | if (m->m_len < sizeof(struct ether_header)) { | |
| 1554 | /* XXX error in the caller. */ | |
| 1555 | m_freem(m); | |
| 1556 | return; | |
| 1557 | } | |
| 29bc1092 SZ |
1558 | |
| 1559 | m->m_pkthdr.rcvif = ifp; | |
| 1560 | ||
| f3e0b5f0 SZ |
1561 | logether(chain_beg, ifp); |
| 1562 | ||
| 29bc1092 SZ |
1563 | ETHER_BPF_MTAP(ifp, m); |
| 1564 | ||
| 1565 | ifp->if_ibytes += m->m_pkthdr.len; | |
| 1566 | ||
| 1567 | if (ifp->if_flags & IFF_MONITOR) { | |
| 828c9923 SZ |
1568 | eh = mtod(m, struct ether_header *); |
| 1569 | if (ETHER_IS_MULTICAST(eh->ether_dhost)) | |
| 1570 | ifp->if_imcasts++; | |
| 1571 | ||
| 29bc1092 SZ |
1572 | /* |
| 1573 | * Interface marked for monitoring; discard packet. | |
| 1574 | */ | |
| b5a65047 | 1575 | m_freem(m); |
| f3e0b5f0 SZ |
1576 | |
| 1577 | logether(chain_end, ifp); | |
| b5a65047 | 1578 | return; |
| 29bc1092 SZ |
1579 | } |
| 1580 | ||
| c3c96e44 MD |
1581 | /* |
| 1582 | * If the packet has been characterized (pi->pi_netisr / M_HASH) | |
| 1583 | * we can dispatch it immediately without further inspection. | |
| 1584 | */ | |
| 2eb0d069 | 1585 | if (pi != NULL && (m->m_flags & M_HASH)) { |
| 7c7f9646 SZ |
1586 | #ifdef RSS_DEBUG |
| 1587 | ether_pktinfo_try++; | |
| 1588 | #endif | |
| c3c96e44 MD |
1589 | ether_dispatch(pi->pi_netisr, m, chain); |
| 1590 | ||
| 7c7f9646 | 1591 | #ifdef RSS_DEBUG |
| c3c96e44 | 1592 | ether_pktinfo_hit++; |
| 7c7f9646 | 1593 | #endif |
| c3c96e44 MD |
1594 | logether(chain_end, ifp); |
| 1595 | return; | |
| 2eb0d069 | 1596 | } |
| 7c7f9646 SZ |
1597 | #ifdef RSS_DEBUG |
| 1598 | else if (ifp->if_capenable & IFCAP_RSS) { | |
| 1599 | if (pi == NULL) | |
| 1600 | ether_rss_nopi++; | |
| 1601 | else | |
| 1602 | ether_rss_nohash++; | |
| 1603 | } | |
| 1604 | #endif | |
| 2eb0d069 SZ |
1605 | |
| 1606 | /* | |
| 1607 | * Packet hash will be recalculated by software, | |
| 1608 | * so clear the M_HASH flag set by the driver; | |
| 1609 | * the hash value calculated by the hardware may | |
| 1610 | * not be exactly what we want. | |
| 1611 | */ | |
| 1612 | m->m_flags &= ~M_HASH; | |
| 1613 | ||
| 057441be | 1614 | if (!ether_vlancheck(&m)) { |
| 057441be | 1615 | KKASSERT(m == NULL); |
| a4923720 | 1616 | logether(chain_end, ifp); |
| 29bc1092 SZ |
1617 | return; |
| 1618 | } | |
| 057441be SZ |
1619 | eh = mtod(m, struct ether_header *); |
| 1620 | ether_type = ntohs(eh->ether_type); | |
| 29bc1092 SZ |
1621 | |
| 1622 | /* | |
| 1623 | * Map ether type to netisr id. | |
| 1624 | */ | |
| 1625 | switch (ether_type) { | |
| 1626 | #ifdef INET | |
| 1627 | case ETHERTYPE_IP: | |
| 1628 | isr = NETISR_IP; | |
| 1629 | break; | |
| 1630 | ||
| 1631 | case ETHERTYPE_ARP: | |
| 1632 | isr = NETISR_ARP; | |
| 1633 | break; | |
| 1634 | #endif | |
| 1635 | ||
| 1636 | #ifdef INET6 | |
| 1637 | case ETHERTYPE_IPV6: | |
| 1638 | isr = NETISR_IPV6; | |
| 1639 | break; | |
| 1640 | #endif | |
| 1641 | ||
| 1642 | #ifdef IPX | |
| 1643 | case ETHERTYPE_IPX: | |
| 1644 | isr = NETISR_IPX; | |
| 1645 | break; | |
| 1646 | #endif | |
| 1647 | ||
| 29bc1092 SZ |
1648 | #ifdef NETATALK |
| 1649 | case ETHERTYPE_AT: | |
| 1650 | isr = NETISR_ATALK1; | |
| 1651 | break; | |
| 1652 | case ETHERTYPE_AARP: | |
| 1653 | isr = NETISR_AARP; | |
| 1654 | break; | |
| 1655 | #endif | |
| 1656 | ||
| a020e9d5 SZ |
1657 | #ifdef MPLS |
| 1658 | case ETHERTYPE_MPLS: | |
| 1659 | case ETHERTYPE_MPLS_MCAST: | |
| cb8d752c | 1660 | m->m_flags |= M_MPLSLABELED; |
| a020e9d5 SZ |
1661 | isr = NETISR_MPLS; |
| 1662 | break; | |
| 1663 | #endif | |
| 1664 | ||
| 29bc1092 SZ |
1665 | default: |
| 1666 | /* | |
| 1667 | * NETISR_MAX is an invalid value; it is chosen to let | |
| 29bc1092 SZ |
1668 | */ |
| 1669 | isr = NETISR_MAX; | |
| 1670 | break; | |
| 1671 | } | |
| 1672 | ||
| 1673 | /* | |
| c3c96e44 MD |
1674 | * Ask the isr to characterize the packet since we couldn't. |
| 1675 | * This is an attempt to optimally get us onto the correct protocol | |
| 1676 | * thread. | |
| 29bc1092 | 1677 | */ |
| c3c96e44 MD |
1678 | netisr_characterize(isr, &m, sizeof(struct ether_header)); |
| 1679 | if (m == NULL) { | |
| a4923720 | 1680 | logether(chain_end, ifp); |
| 29bc1092 SZ |
1681 | return; |
| 1682 | } | |
| 1683 | ||
| 1684 | /* | |
| c3c96e44 | 1685 | * Finally dispatch it |
| 29bc1092 | 1686 | */ |
| c3c96e44 | 1687 | ether_dispatch(isr, m, chain); |
| 29bc1092 | 1688 | |
| f3e0b5f0 | 1689 | logether(chain_end, ifp); |
| 29bc1092 | 1690 | } |
| 09c280ec SS |
1691 | |
| 1692 | MODULE_VERSION(ether, 1); |