if_bridge.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772
  1. /* $OpenBSD: if_bridge.c,v 1.257 2015/07/20 22:54:29 mpi Exp $ */
  2. /*
  3. * Copyright (c) 1999, 2000 Jason L. Wright (jason@thought.net)
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  16. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  19. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  23. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  24. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. * POSSIBILITY OF SUCH DAMAGE.
  26. *
  27. * Effort sponsored in part by the Defense Advanced Research Projects
  28. * Agency (DARPA) and Air Force Research Laboratory, Air Force
  29. * Materiel Command, USAF, under agreement number F30602-01-2-0537.
  30. *
  31. */
  32. #include "bpfilter.h"
  33. #include "gif.h"
  34. #include "pf.h"
  35. #include "carp.h"
  36. #include "vlan.h"
  37. #include "mpw.h"
  38. #include <sys/param.h>
  39. #include <sys/systm.h>
  40. #include <sys/mbuf.h>
  41. #include <sys/socket.h>
  42. #include <sys/timeout.h>
  43. #include <sys/ioctl.h>
  44. #include <sys/errno.h>
  45. #include <sys/kernel.h>
  46. #include <crypto/siphash.h>
  47. #include <net/if.h>
  48. #include <net/if_types.h>
  49. #include <net/if_llc.h>
  50. #include <net/netisr.h>
  51. #include <netinet/in.h>
  52. #include <netinet/ip.h>
  53. #include <netinet/ip_var.h>
  54. #include <netinet/if_ether.h>
  55. #include <netinet/ip_icmp.h>
  56. #ifdef IPSEC
  57. #include <netinet/ip_ipsp.h>
  58. #include <net/if_enc.h>
  59. #endif
  60. #ifdef INET6
  61. #include <netinet6/in6_var.h>
  62. #include <netinet/ip6.h>
  63. #include <netinet6/ip6_var.h>
  64. #endif
  65. #if NPF > 0
  66. #include <net/pfvar.h>
  67. #define BRIDGE_IN PF_IN
  68. #define BRIDGE_OUT PF_OUT
  69. #else
  70. #define BRIDGE_IN 0
  71. #define BRIDGE_OUT 1
  72. #endif
  73. #if NBPFILTER > 0
  74. #include <net/bpf.h>
  75. #endif
  76. #if NCARP > 0
  77. #include <netinet/ip_carp.h>
  78. #endif
  79. #if NVLAN > 0
  80. #include <net/if_vlan_var.h>
  81. #endif
  82. #if NGIF > 0
  83. #include <net/if_gif.h>
  84. #endif
  85. #include <net/if_bridge.h>
  86. /*
  87. * Maximum number of addresses to cache
  88. */
  89. #ifndef BRIDGE_RTABLE_MAX
  90. #define BRIDGE_RTABLE_MAX 100
  91. #endif
  92. /*
  93. * Timeout (in seconds) for entries learned dynamically
  94. */
  95. #ifndef BRIDGE_RTABLE_TIMEOUT
  96. #define BRIDGE_RTABLE_TIMEOUT 240
  97. #endif
  98. void bridgeattach(int);
  99. int bridge_ioctl(struct ifnet *, u_long, caddr_t);
  100. void bridge_start(struct ifnet *);
  101. void bridgeintr_frame(struct bridge_softc *, struct mbuf *);
  102. void bridge_broadcast(struct bridge_softc *, struct ifnet *,
  103. struct ether_header *, struct mbuf *);
  104. void bridge_localbroadcast(struct bridge_softc *, struct ifnet *,
  105. struct ether_header *, struct mbuf *);
  106. void bridge_span(struct bridge_softc *, struct mbuf *);
  107. void bridge_stop(struct bridge_softc *);
  108. void bridge_init(struct bridge_softc *);
  109. int bridge_bifconf(struct bridge_softc *, struct ifbifconf *);
  110. void bridge_timer(void *);
  111. int bridge_rtfind(struct bridge_softc *, struct ifbaconf *);
  112. void bridge_rtage(struct bridge_softc *);
  113. int bridge_rtdaddr(struct bridge_softc *, struct ether_addr *);
  114. void bridge_rtflush(struct bridge_softc *, int);
  115. struct ifnet *bridge_rtupdate(struct bridge_softc *,
  116. struct ether_addr *, struct ifnet *ifp, int, u_int8_t, struct mbuf *);
  117. struct bridge_rtnode *bridge_rtlookup(struct bridge_softc *,
  118. struct ether_addr *);
  119. u_int32_t bridge_hash(struct bridge_softc *, struct ether_addr *);
  120. int bridge_blocknonip(struct ether_header *, struct mbuf *);
  121. int bridge_addrule(struct bridge_iflist *,
  122. struct ifbrlreq *, int out);
  123. void bridge_flushrule(struct bridge_iflist *);
  124. int bridge_brlconf(struct bridge_softc *, struct ifbrlconf *);
  125. u_int8_t bridge_filterrule(struct brl_head *, struct ether_header *,
  126. struct mbuf *);
  127. struct mbuf *bridge_ip(struct bridge_softc *, int, struct ifnet *,
  128. struct ether_header *, struct mbuf *m);
  129. int bridge_ifenqueue(struct bridge_softc *, struct ifnet *, struct mbuf *);
  130. void bridge_fragment(struct bridge_softc *, struct ifnet *,
  131. struct ether_header *, struct mbuf *);
  132. void bridge_send_icmp_err(struct bridge_softc *, struct ifnet *,
  133. struct ether_header *, struct mbuf *, int, struct llc *, int, int, int);
  134. #ifdef IPSEC
  135. int bridge_ipsec(struct bridge_softc *, struct ifnet *,
  136. struct ether_header *, int, struct llc *,
  137. int, int, int, struct mbuf *);
  138. #endif
  139. int bridge_clone_create(struct if_clone *, int);
  140. int bridge_clone_destroy(struct ifnet *ifp);
  141. int bridge_delete(struct bridge_softc *, struct bridge_iflist *);
  142. void bridge_copyaddr(struct sockaddr *, struct sockaddr *);
  143. struct mbuf *bridge_m_dup(struct mbuf *);
  144. #define ETHERADDR_IS_IP_MCAST(a) \
  145. /* struct etheraddr *a; */ \
  146. ((a)->ether_addr_octet[0] == 0x01 && \
  147. (a)->ether_addr_octet[1] == 0x00 && \
  148. (a)->ether_addr_octet[2] == 0x5e)
  149. LIST_HEAD(, bridge_softc) bridge_list;
  150. struct if_clone bridge_cloner =
  151. IF_CLONE_INITIALIZER("bridge", bridge_clone_create, bridge_clone_destroy);
  152. /* ARGSUSED */
  153. void
  154. bridgeattach(int n)
  155. {
  156. LIST_INIT(&bridge_list);
  157. if_clone_attach(&bridge_cloner);
  158. }
  159. int
  160. bridge_clone_create(struct if_clone *ifc, int unit)
  161. {
  162. struct bridge_softc *sc;
  163. struct ifih *bridge_ifih;
  164. struct ifnet *ifp;
  165. int i, s;
  166. sc = malloc(sizeof(*sc), M_DEVBUF, M_NOWAIT|M_ZERO);
  167. if (!sc)
  168. return (ENOMEM);
  169. bridge_ifih = malloc(sizeof(*bridge_ifih), M_DEVBUF, M_NOWAIT);
  170. if (bridge_ifih == NULL) {
  171. free(sc, M_DEVBUF, 0);
  172. return (ENOMEM);
  173. }
  174. sc->sc_stp = bstp_create(&sc->sc_if);
  175. if (!sc->sc_stp) {
  176. free(bridge_ifih, M_DEVBUF, sizeof(*bridge_ifih));
  177. free(sc, M_DEVBUF, 0);
  178. return (ENOMEM);
  179. }
  180. sc->sc_brtmax = BRIDGE_RTABLE_MAX;
  181. sc->sc_brttimeout = BRIDGE_RTABLE_TIMEOUT;
  182. timeout_set(&sc->sc_brtimeout, bridge_timer, sc);
  183. TAILQ_INIT(&sc->sc_iflist);
  184. TAILQ_INIT(&sc->sc_spanlist);
  185. for (i = 0; i < BRIDGE_RTABLE_SIZE; i++)
  186. LIST_INIT(&sc->sc_rts[i]);
  187. arc4random_buf(&sc->sc_hashkey, sizeof(sc->sc_hashkey));
  188. ifp = &sc->sc_if;
  189. snprintf(ifp->if_xname, sizeof ifp->if_xname, "%s%d", ifc->ifc_name,
  190. unit);
  191. ifp->if_softc = sc;
  192. ifp->if_mtu = ETHERMTU;
  193. ifp->if_ioctl = bridge_ioctl;
  194. ifp->if_output = bridge_output;
  195. ifp->if_start = bridge_start;
  196. ifp->if_type = IFT_BRIDGE;
  197. ifp->if_hdrlen = ETHER_HDR_LEN;
  198. IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
  199. IFQ_SET_READY(&ifp->if_snd);
  200. if_attach(ifp);
  201. if_alloc_sadl(ifp);
  202. #if NBPFILTER > 0
  203. bpfattach(&sc->sc_if.if_bpf, ifp,
  204. DLT_EN10MB, ETHER_HDR_LEN);
  205. #endif
  206. bridge_ifih->ifih_input = ether_input;
  207. SLIST_INSERT_HEAD(&ifp->if_inputs, bridge_ifih, ifih_next);
  208. s = splnet();
  209. LIST_INSERT_HEAD(&bridge_list, sc, sc_list);
  210. splx(s);
  211. return (0);
  212. }
  213. int
  214. bridge_clone_destroy(struct ifnet *ifp)
  215. {
  216. struct bridge_softc *sc = ifp->if_softc;
  217. struct bridge_iflist *bif;
  218. struct ifih *bridge_ifih;
  219. int s;
  220. bridge_stop(sc);
  221. bridge_rtflush(sc, IFBF_FLUSHALL);
  222. while ((bif = TAILQ_FIRST(&sc->sc_iflist)) != NULL)
  223. bridge_delete(sc, bif);
  224. while ((bif = TAILQ_FIRST(&sc->sc_spanlist)) != NULL) {
  225. TAILQ_REMOVE(&sc->sc_spanlist, bif, next);
  226. free(bif, M_DEVBUF, 0);
  227. }
  228. s = splnet();
  229. LIST_REMOVE(sc, sc_list);
  230. splx(s);
  231. bstp_destroy(sc->sc_stp);
  232. /* Undo pseudo-driver changes. */
  233. if_deactivate(ifp);
  234. bridge_ifih = SLIST_FIRST(&ifp->if_inputs);
  235. SLIST_REMOVE_HEAD(&ifp->if_inputs, ifih_next);
  236. KASSERT(SLIST_EMPTY(&ifp->if_inputs));
  237. free(bridge_ifih, M_DEVBUF, sizeof(*bridge_ifih));
  238. if_detach(ifp);
  239. free(sc, M_DEVBUF, 0);
  240. return (0);
  241. }
  242. int
  243. bridge_delete(struct bridge_softc *sc, struct bridge_iflist *p)
  244. {
  245. int error;
  246. if (p->bif_flags & IFBIF_STP)
  247. bstp_delete(p->bif_stp);
  248. p->ifp->if_bridgeport = NULL;
  249. error = ifpromisc(p->ifp, 0);
  250. TAILQ_REMOVE(&sc->sc_iflist, p, next);
  251. bridge_rtdelete(sc, p->ifp, 0);
  252. bridge_flushrule(p);
  253. free(p, M_DEVBUF, 0);
  254. return (error);
  255. }
  256. int
  257. bridge_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
  258. {
  259. struct bridge_softc *sc = (struct bridge_softc *)ifp->if_softc;
  260. struct ifbreq *req = (struct ifbreq *)data;
  261. struct ifbareq *bareq = (struct ifbareq *)data;
  262. struct ifbrparam *bparam = (struct ifbrparam *)data;
  263. struct ifbrlreq *brlreq = (struct ifbrlreq *)data;
  264. struct ifbropreq *brop = (struct ifbropreq *)data;
  265. struct ifnet *ifs;
  266. struct bridge_iflist *p;
  267. struct bstp_port *bp;
  268. struct bstp_state *bs = sc->sc_stp;
  269. int error = 0, s;
  270. s = splnet();
  271. switch (cmd) {
  272. case SIOCBRDGADD:
  273. if ((error = suser(curproc, 0)) != 0)
  274. break;
  275. ifs = ifunit(req->ifbr_ifsname);
  276. if (ifs == NULL) { /* no such interface */
  277. error = ENOENT;
  278. break;
  279. }
  280. if (ifs->if_bridgeport != NULL) {
  281. p = (struct bridge_iflist *)ifs->if_bridgeport;
  282. if (p->bridge_sc == sc)
  283. error = EEXIST;
  284. else
  285. error = EBUSY;
  286. break;
  287. }
  288. /* If it's in the span list, it can't be a member. */
  289. TAILQ_FOREACH(p, &sc->sc_spanlist, next)
  290. if (p->ifp == ifs)
  291. break;
  292. if (p != NULL) {
  293. error = EBUSY;
  294. break;
  295. }
  296. if (ifs->if_type == IFT_ETHER) {
  297. if ((ifs->if_flags & IFF_UP) == 0) {
  298. struct ifreq ifreq;
  299. /*
  300. * Bring interface up long enough to set
  301. * promiscuous flag, then shut it down again.
  302. */
  303. strlcpy(ifreq.ifr_name, req->ifbr_ifsname,
  304. IFNAMSIZ);
  305. ifs->if_flags |= IFF_UP;
  306. ifreq.ifr_flags = ifs->if_flags;
  307. error = (*ifs->if_ioctl)(ifs, SIOCSIFFLAGS,
  308. (caddr_t)&ifreq);
  309. if (error != 0)
  310. break;
  311. error = ifpromisc(ifs, 1);
  312. if (error != 0)
  313. break;
  314. strlcpy(ifreq.ifr_name, req->ifbr_ifsname,
  315. IFNAMSIZ);
  316. ifs->if_flags &= ~IFF_UP;
  317. ifreq.ifr_flags = ifs->if_flags;
  318. error = (*ifs->if_ioctl)(ifs, SIOCSIFFLAGS,
  319. (caddr_t)&ifreq);
  320. if (error != 0) {
  321. ifpromisc(ifs, 0);
  322. break;
  323. }
  324. } else {
  325. error = ifpromisc(ifs, 1);
  326. if (error != 0)
  327. break;
  328. }
  329. }
  330. #if NGIF > 0
  331. else if (ifs->if_type == IFT_GIF) {
  332. /* Nothing needed */
  333. }
  334. #endif /* NGIF */
  335. #if NMPW > 0
  336. else if (ifs->if_type == IFT_MPLSTUNNEL) {
  337. /* Nothing needed */
  338. }
  339. #endif /* NMPW */
  340. else {
  341. error = EINVAL;
  342. break;
  343. }
  344. p = malloc(sizeof(*p), M_DEVBUF, M_NOWAIT|M_ZERO);
  345. if (p == NULL) {
  346. if (ifs->if_type == IFT_ETHER)
  347. ifpromisc(ifs, 0);
  348. error = ENOMEM;
  349. break;
  350. }
  351. p->bridge_sc = sc;
  352. p->ifp = ifs;
  353. p->bif_flags = IFBIF_LEARNING | IFBIF_DISCOVER;
  354. SIMPLEQ_INIT(&p->bif_brlin);
  355. SIMPLEQ_INIT(&p->bif_brlout);
  356. ifs->if_bridgeport = (caddr_t)p;
  357. TAILQ_INSERT_TAIL(&sc->sc_iflist, p, next);
  358. break;
  359. case SIOCBRDGDEL:
  360. if ((error = suser(curproc, 0)) != 0)
  361. break;
  362. ifs = ifunit(req->ifbr_ifsname);
  363. if (ifs == NULL) {
  364. error = ENOENT;
  365. break;
  366. }
  367. p = (struct bridge_iflist *)ifs->if_bridgeport;
  368. if (p == NULL || p->bridge_sc != sc) {
  369. error = ESRCH;
  370. break;
  371. }
  372. error = bridge_delete(sc, p);
  373. break;
  374. case SIOCBRDGIFS:
  375. error = bridge_bifconf(sc, (struct ifbifconf *)data);
  376. break;
  377. case SIOCBRDGADDS:
  378. if ((error = suser(curproc, 0)) != 0)
  379. break;
  380. ifs = ifunit(req->ifbr_ifsname);
  381. if (ifs == NULL) { /* no such interface */
  382. error = ENOENT;
  383. break;
  384. }
  385. if (ifs->if_bridgeport != NULL) {
  386. error = EBUSY;
  387. break;
  388. }
  389. TAILQ_FOREACH(p, &sc->sc_spanlist, next) {
  390. if (p->ifp == ifs)
  391. break;
  392. }
  393. if (p != NULL) {
  394. error = EEXIST;
  395. break;
  396. }
  397. p = malloc(sizeof(*p), M_DEVBUF, M_NOWAIT|M_ZERO);
  398. if (p == NULL) {
  399. error = ENOMEM;
  400. break;
  401. }
  402. p->ifp = ifs;
  403. p->bif_flags = IFBIF_SPAN;
  404. SIMPLEQ_INIT(&p->bif_brlin);
  405. SIMPLEQ_INIT(&p->bif_brlout);
  406. TAILQ_INSERT_TAIL(&sc->sc_spanlist, p, next);
  407. break;
  408. case SIOCBRDGDELS:
  409. if ((error = suser(curproc, 0)) != 0)
  410. break;
  411. TAILQ_FOREACH(p, &sc->sc_spanlist, next) {
  412. if (strncmp(p->ifp->if_xname, req->ifbr_ifsname,
  413. sizeof(p->ifp->if_xname)) == 0) {
  414. TAILQ_REMOVE(&sc->sc_spanlist, p, next);
  415. free(p, M_DEVBUF, 0);
  416. break;
  417. }
  418. }
  419. if (p == NULL) {
  420. error = ENOENT;
  421. break;
  422. }
  423. break;
  424. case SIOCBRDGGIFFLGS:
  425. ifs = ifunit(req->ifbr_ifsname);
  426. if (ifs == NULL) {
  427. error = ENOENT;
  428. break;
  429. }
  430. p = (struct bridge_iflist *)ifs->if_bridgeport;
  431. if (p == NULL || p->bridge_sc != sc) {
  432. error = ESRCH;
  433. break;
  434. }
  435. req->ifbr_ifsflags = p->bif_flags;
  436. req->ifbr_portno = p->ifp->if_index & 0xfff;
  437. if (p->bif_flags & IFBIF_STP) {
  438. bp = p->bif_stp;
  439. req->ifbr_state = bstp_getstate(bs, bp);
  440. req->ifbr_priority = bp->bp_priority;
  441. req->ifbr_path_cost = bp->bp_path_cost;
  442. req->ifbr_proto = bp->bp_protover;
  443. req->ifbr_role = bp->bp_role;
  444. req->ifbr_stpflags = bp->bp_flags;
  445. req->ifbr_fwd_trans = bp->bp_forward_transitions;
  446. req->ifbr_desg_bridge = bp->bp_desg_pv.pv_dbridge_id;
  447. req->ifbr_desg_port = bp->bp_desg_pv.pv_dport_id;
  448. req->ifbr_root_bridge = bp->bp_desg_pv.pv_root_id;
  449. req->ifbr_root_cost = bp->bp_desg_pv.pv_cost;
  450. req->ifbr_root_port = bp->bp_desg_pv.pv_port_id;
  451. /* Copy STP state options as flags */
  452. if (bp->bp_operedge)
  453. req->ifbr_ifsflags |= IFBIF_BSTP_EDGE;
  454. if (bp->bp_flags & BSTP_PORT_AUTOEDGE)
  455. req->ifbr_ifsflags |= IFBIF_BSTP_AUTOEDGE;
  456. if (bp->bp_ptp_link)
  457. req->ifbr_ifsflags |= IFBIF_BSTP_PTP;
  458. if (bp->bp_flags & BSTP_PORT_AUTOPTP)
  459. req->ifbr_ifsflags |= IFBIF_BSTP_AUTOPTP;
  460. }
  461. break;
  462. case SIOCBRDGSIFFLGS:
  463. if ((error = suser(curproc, 0)) != 0)
  464. break;
  465. ifs = ifunit(req->ifbr_ifsname);
  466. if (ifs == NULL) {
  467. error = ENOENT;
  468. break;
  469. }
  470. p = (struct bridge_iflist *)ifs->if_bridgeport;
  471. if (p == NULL || p->bridge_sc != sc) {
  472. error = ESRCH;
  473. break;
  474. }
  475. if (req->ifbr_ifsflags & IFBIF_RO_MASK) {
  476. error = EINVAL;
  477. break;
  478. }
  479. if (req->ifbr_ifsflags & IFBIF_STP) {
  480. if ((p->bif_flags & IFBIF_STP) == 0) {
  481. /* Enable STP */
  482. if ((p->bif_stp = bstp_add(sc->sc_stp,
  483. p->ifp)) == NULL) {
  484. error = ENOMEM;
  485. break;
  486. }
  487. } else {
  488. /* Update STP flags */
  489. bstp_ifsflags(p->bif_stp, req->ifbr_ifsflags);
  490. }
  491. } else if (p->bif_flags & IFBIF_STP) {
  492. bstp_delete(p->bif_stp);
  493. p->bif_stp = NULL;
  494. }
  495. p->bif_flags = req->ifbr_ifsflags;
  496. break;
  497. case SIOCBRDGRTS:
  498. error = bridge_rtfind(sc, (struct ifbaconf *)data);
  499. break;
  500. case SIOCBRDGFLUSH:
  501. if ((error = suser(curproc, 0)) != 0)
  502. break;
  503. bridge_rtflush(sc, req->ifbr_ifsflags);
  504. break;
  505. case SIOCBRDGSADDR:
  506. if ((error = suser(curproc, 0)) != 0)
  507. break;
  508. ifs = ifunit(bareq->ifba_ifsname);
  509. if (ifs == NULL) { /* no such interface */
  510. error = ENOENT;
  511. break;
  512. }
  513. p = (struct bridge_iflist *)ifs->if_bridgeport;
  514. if (p == NULL || p->bridge_sc != sc) {
  515. error = ESRCH;
  516. break;
  517. }
  518. ifs = bridge_rtupdate(sc, &bareq->ifba_dst, ifs, 1,
  519. bareq->ifba_flags, NULL);
  520. if (ifs == NULL)
  521. error = ENOMEM;
  522. break;
  523. case SIOCBRDGDADDR:
  524. if ((error = suser(curproc, 0)) != 0)
  525. break;
  526. error = bridge_rtdaddr(sc, &bareq->ifba_dst);
  527. break;
  528. case SIOCBRDGGCACHE:
  529. bparam->ifbrp_csize = sc->sc_brtmax;
  530. break;
  531. case SIOCBRDGSCACHE:
  532. if ((error = suser(curproc, 0)) != 0)
  533. break;
  534. sc->sc_brtmax = bparam->ifbrp_csize;
  535. break;
  536. case SIOCBRDGSTO:
  537. if ((error = suser(curproc, 0)) != 0)
  538. break;
  539. if (bparam->ifbrp_ctime < 0 ||
  540. bparam->ifbrp_ctime > INT_MAX / hz) {
  541. error = EINVAL;
  542. break;
  543. }
  544. sc->sc_brttimeout = bparam->ifbrp_ctime;
  545. if (bparam->ifbrp_ctime != 0)
  546. timeout_add_sec(&sc->sc_brtimeout, sc->sc_brttimeout);
  547. else
  548. timeout_del(&sc->sc_brtimeout);
  549. break;
  550. case SIOCBRDGGTO:
  551. bparam->ifbrp_ctime = sc->sc_brttimeout;
  552. break;
  553. case SIOCSIFFLAGS:
  554. if ((ifp->if_flags & IFF_UP) == IFF_UP)
  555. bridge_init(sc);
  556. if ((ifp->if_flags & IFF_UP) == 0)
  557. bridge_stop(sc);
  558. break;
  559. case SIOCBRDGARL:
  560. if ((error = suser(curproc, 0)) != 0)
  561. break;
  562. ifs = ifunit(brlreq->ifbr_ifsname);
  563. if (ifs == NULL) {
  564. error = ENOENT;
  565. break;
  566. }
  567. p = (struct bridge_iflist *)ifs->if_bridgeport;
  568. if (p == NULL || p->bridge_sc != sc) {
  569. error = ESRCH;
  570. break;
  571. }
  572. if ((brlreq->ifbr_action != BRL_ACTION_BLOCK &&
  573. brlreq->ifbr_action != BRL_ACTION_PASS) ||
  574. (brlreq->ifbr_flags & (BRL_FLAG_IN|BRL_FLAG_OUT)) == 0) {
  575. error = EINVAL;
  576. break;
  577. }
  578. if (brlreq->ifbr_flags & BRL_FLAG_IN) {
  579. error = bridge_addrule(p, brlreq, 0);
  580. if (error)
  581. break;
  582. }
  583. if (brlreq->ifbr_flags & BRL_FLAG_OUT) {
  584. error = bridge_addrule(p, brlreq, 1);
  585. if (error)
  586. break;
  587. }
  588. break;
  589. case SIOCBRDGFRL:
  590. if ((error = suser(curproc, 0)) != 0)
  591. break;
  592. ifs = ifunit(brlreq->ifbr_ifsname);
  593. if (ifs == NULL) {
  594. error = ENOENT;
  595. break;
  596. }
  597. p = (struct bridge_iflist *)ifs->if_bridgeport;
  598. if (p == NULL || p->bridge_sc != sc) {
  599. error = ESRCH;
  600. break;
  601. }
  602. bridge_flushrule(p);
  603. break;
  604. case SIOCBRDGGRL:
  605. error = bridge_brlconf(sc, (struct ifbrlconf *)data);
  606. break;
  607. case SIOCBRDGGPARAM:
  608. if ((bp = bs->bs_root_port) == NULL)
  609. brop->ifbop_root_port = 0;
  610. else
  611. brop->ifbop_root_port = bp->bp_ifp->if_index;
  612. brop->ifbop_maxage = bs->bs_bridge_max_age >> 8;
  613. brop->ifbop_hellotime = bs->bs_bridge_htime >> 8;
  614. brop->ifbop_fwddelay = bs->bs_bridge_fdelay >> 8;
  615. brop->ifbop_holdcount = bs->bs_txholdcount;
  616. brop->ifbop_priority = bs->bs_bridge_priority;
  617. brop->ifbop_protocol = bs->bs_protover;
  618. brop->ifbop_root_bridge = bs->bs_root_pv.pv_root_id;
  619. brop->ifbop_root_path_cost = bs->bs_root_pv.pv_cost;
  620. brop->ifbop_root_port = bs->bs_root_pv.pv_port_id;
  621. brop->ifbop_desg_bridge = bs->bs_root_pv.pv_dbridge_id;
  622. brop->ifbop_last_tc_time.tv_sec = bs->bs_last_tc_time.tv_sec;
  623. brop->ifbop_last_tc_time.tv_usec = bs->bs_last_tc_time.tv_usec;
  624. break;
  625. case SIOCBRDGGPRI:
  626. case SIOCBRDGGMA:
  627. case SIOCBRDGGHT:
  628. case SIOCBRDGGFD:
  629. break;
  630. case SIOCBRDGSPRI:
  631. case SIOCBRDGSFD:
  632. case SIOCBRDGSMA:
  633. case SIOCBRDGSHT:
  634. case SIOCBRDGSTXHC:
  635. case SIOCBRDGSPROTO:
  636. case SIOCBRDGSIFPRIO:
  637. case SIOCBRDGSIFCOST:
  638. error = suser(curproc, 0);
  639. break;
  640. default:
  641. error = ENOTTY;
  642. break;
  643. }
  644. if (!error)
  645. error = bstp_ioctl(ifp, cmd, data);
  646. splx(s);
  647. return (error);
  648. }
  649. /* Detach an interface from a bridge. */
  650. void
  651. bridge_ifdetach(struct ifnet *ifp)
  652. {
  653. struct bridge_softc *sc;
  654. struct bridge_iflist *bif;
  655. bif = (struct bridge_iflist *)ifp->if_bridgeport;
  656. sc = bif->bridge_sc;
  657. bridge_delete(sc, bif);
  658. }
  659. void
  660. bridge_update(struct ifnet *ifp, struct ether_addr *ea, int delete)
  661. {
  662. struct bridge_softc *sc;
  663. struct bridge_iflist *bif;
  664. u_int8_t *addr;
  665. addr = (u_int8_t *)ea;
  666. bif = (struct bridge_iflist *)ifp->if_bridgeport;
  667. sc = bif->bridge_sc;
  668. /*
  669. * Update the bridge interface if it is in
  670. * the learning state.
  671. */
  672. if ((bif->bif_flags & IFBIF_LEARNING) &&
  673. (ETHER_IS_MULTICAST(addr) == 0) &&
  674. !(addr[0] == 0 && addr[1] == 0 && addr[2] == 0 &&
  675. addr[3] == 0 && addr[4] == 0 && addr[5] == 0)) {
  676. /* Care must be taken with spanning tree */
  677. if ((bif->bif_flags & IFBIF_STP) &&
  678. (bif->bif_state == BSTP_IFSTATE_DISCARDING))
  679. return;
  680. /* Delete the address from the bridge */
  681. bridge_rtdaddr(sc, ea);
  682. if (!delete) {
  683. /* Update the bridge table */
  684. bridge_rtupdate(sc, ea, ifp, 0, IFBAF_DYNAMIC, NULL);
  685. }
  686. }
  687. }
  688. int
  689. bridge_bifconf(struct bridge_softc *sc, struct ifbifconf *bifc)
  690. {
  691. struct bridge_iflist *p;
  692. struct bstp_port *bp;
  693. struct bstp_state *bs = sc->sc_stp;
  694. u_int32_t total = 0, i = 0;
  695. int error = 0;
  696. struct ifbreq *breq = NULL;
  697. TAILQ_FOREACH(p, &sc->sc_iflist, next)
  698. total++;
  699. TAILQ_FOREACH(p, &sc->sc_spanlist, next)
  700. total++;
  701. if (bifc->ifbic_len == 0) {
  702. i = total;
  703. goto done;
  704. }
  705. if ((breq = (struct ifbreq *)
  706. malloc(sizeof(*breq), M_DEVBUF, M_NOWAIT)) == NULL)
  707. goto done;
  708. TAILQ_FOREACH(p, &sc->sc_iflist, next) {
  709. bzero(breq, sizeof(*breq));
  710. if (bifc->ifbic_len < sizeof(*breq))
  711. break;
  712. strlcpy(breq->ifbr_name, sc->sc_if.if_xname, IFNAMSIZ);
  713. strlcpy(breq->ifbr_ifsname, p->ifp->if_xname, IFNAMSIZ);
  714. breq->ifbr_ifsflags = p->bif_flags;
  715. breq->ifbr_portno = p->ifp->if_index & 0xfff;
  716. if (p->bif_flags & IFBIF_STP) {
  717. bp = p->bif_stp;
  718. breq->ifbr_state = bstp_getstate(sc->sc_stp, bp);
  719. breq->ifbr_priority = bp->bp_priority;
  720. breq->ifbr_path_cost = bp->bp_path_cost;
  721. breq->ifbr_proto = bp->bp_protover;
  722. breq->ifbr_role = bp->bp_role;
  723. breq->ifbr_stpflags = bp->bp_flags;
  724. breq->ifbr_fwd_trans = bp->bp_forward_transitions;
  725. breq->ifbr_root_bridge = bs->bs_root_pv.pv_root_id;
  726. breq->ifbr_root_cost = bs->bs_root_pv.pv_cost;
  727. breq->ifbr_root_port = bs->bs_root_pv.pv_port_id;
  728. breq->ifbr_desg_bridge = bs->bs_root_pv.pv_dbridge_id;
  729. breq->ifbr_desg_port = bs->bs_root_pv.pv_dport_id;
  730. /* Copy STP state options as flags */
  731. if (bp->bp_operedge)
  732. breq->ifbr_ifsflags |= IFBIF_BSTP_EDGE;
  733. if (bp->bp_flags & BSTP_PORT_AUTOEDGE)
  734. breq->ifbr_ifsflags |= IFBIF_BSTP_AUTOEDGE;
  735. if (bp->bp_ptp_link)
  736. breq->ifbr_ifsflags |= IFBIF_BSTP_PTP;
  737. if (bp->bp_flags & BSTP_PORT_AUTOPTP)
  738. breq->ifbr_ifsflags |= IFBIF_BSTP_AUTOPTP;
  739. }
  740. error = copyout((caddr_t)breq,
  741. (caddr_t)(bifc->ifbic_req + i), sizeof(*breq));
  742. if (error)
  743. goto done;
  744. i++;
  745. bifc->ifbic_len -= sizeof(*breq);
  746. }
  747. TAILQ_FOREACH(p, &sc->sc_spanlist, next) {
  748. bzero(breq, sizeof(*breq));
  749. if (bifc->ifbic_len < sizeof(*breq))
  750. break;
  751. strlcpy(breq->ifbr_name, sc->sc_if.if_xname, IFNAMSIZ);
  752. strlcpy(breq->ifbr_ifsname, p->ifp->if_xname, IFNAMSIZ);
  753. breq->ifbr_ifsflags = p->bif_flags | IFBIF_SPAN;
  754. breq->ifbr_portno = p->ifp->if_index & 0xfff;
  755. error = copyout((caddr_t)breq,
  756. (caddr_t)(bifc->ifbic_req + i), sizeof(*breq));
  757. if (error)
  758. goto done;
  759. i++;
  760. bifc->ifbic_len -= sizeof(*breq);
  761. }
  762. done:
  763. if (breq != NULL)
  764. free(breq, M_DEVBUF, 0);
  765. bifc->ifbic_len = i * sizeof(*breq);
  766. return (error);
  767. }
  768. int
  769. bridge_brlconf(struct bridge_softc *sc, struct ifbrlconf *bc)
  770. {
  771. struct ifnet *ifp;
  772. struct bridge_iflist *ifl;
  773. struct brl_node *n;
  774. struct ifbrlreq req;
  775. int error = 0;
  776. u_int32_t i = 0, total = 0;
  777. ifp = ifunit(bc->ifbrl_ifsname);
  778. if (ifp == NULL)
  779. return (ENOENT);
  780. ifl = (struct bridge_iflist *)ifp->if_bridgeport;
  781. if (ifl == NULL || ifl->bridge_sc != sc)
  782. return (ESRCH);
  783. SIMPLEQ_FOREACH(n, &ifl->bif_brlin, brl_next) {
  784. total++;
  785. }
  786. SIMPLEQ_FOREACH(n, &ifl->bif_brlout, brl_next) {
  787. total++;
  788. }
  789. if (bc->ifbrl_len == 0) {
  790. i = total;
  791. goto done;
  792. }
  793. SIMPLEQ_FOREACH(n, &ifl->bif_brlin, brl_next) {
  794. bzero(&req, sizeof req);
  795. if (bc->ifbrl_len < sizeof(req))
  796. goto done;
  797. strlcpy(req.ifbr_name, sc->sc_if.if_xname, IFNAMSIZ);
  798. strlcpy(req.ifbr_ifsname, ifl->ifp->if_xname, IFNAMSIZ);
  799. req.ifbr_action = n->brl_action;
  800. req.ifbr_flags = n->brl_flags;
  801. req.ifbr_src = n->brl_src;
  802. req.ifbr_dst = n->brl_dst;
  803. #if NPF > 0
  804. req.ifbr_tagname[0] = '\0';
  805. if (n->brl_tag)
  806. pf_tag2tagname(n->brl_tag, req.ifbr_tagname);
  807. #endif
  808. error = copyout((caddr_t)&req,
  809. (caddr_t)(bc->ifbrl_buf + (i * sizeof(req))), sizeof(req));
  810. if (error)
  811. goto done;
  812. i++;
  813. bc->ifbrl_len -= sizeof(req);
  814. }
  815. SIMPLEQ_FOREACH(n, &ifl->bif_brlout, brl_next) {
  816. bzero(&req, sizeof req);
  817. if (bc->ifbrl_len < sizeof(req))
  818. goto done;
  819. strlcpy(req.ifbr_name, sc->sc_if.if_xname, IFNAMSIZ);
  820. strlcpy(req.ifbr_ifsname, ifl->ifp->if_xname, IFNAMSIZ);
  821. req.ifbr_action = n->brl_action;
  822. req.ifbr_flags = n->brl_flags;
  823. req.ifbr_src = n->brl_src;
  824. req.ifbr_dst = n->brl_dst;
  825. #if NPF > 0
  826. req.ifbr_tagname[0] = '\0';
  827. if (n->brl_tag)
  828. pf_tag2tagname(n->brl_tag, req.ifbr_tagname);
  829. #endif
  830. error = copyout((caddr_t)&req,
  831. (caddr_t)(bc->ifbrl_buf + (i * sizeof(req))), sizeof(req));
  832. if (error)
  833. goto done;
  834. i++;
  835. bc->ifbrl_len -= sizeof(req);
  836. }
  837. done:
  838. bc->ifbrl_len = i * sizeof(req);
  839. return (error);
  840. }
  841. void
  842. bridge_init(struct bridge_softc *sc)
  843. {
  844. struct ifnet *ifp = &sc->sc_if;
  845. if ((ifp->if_flags & IFF_RUNNING) == IFF_RUNNING)
  846. return;
  847. ifp->if_flags |= IFF_RUNNING;
  848. bstp_initialization(sc->sc_stp);
  849. if (sc->sc_brttimeout != 0)
  850. timeout_add_sec(&sc->sc_brtimeout, sc->sc_brttimeout);
  851. }
  852. /*
  853. * Stop the bridge and deallocate the routing table.
  854. */
  855. void
  856. bridge_stop(struct bridge_softc *sc)
  857. {
  858. struct ifnet *ifp = &sc->sc_if;
  859. /*
  860. * If we're not running, there's nothing to do.
  861. */
  862. if ((ifp->if_flags & IFF_RUNNING) == 0)
  863. return;
  864. timeout_del(&sc->sc_brtimeout);
  865. bridge_rtflush(sc, IFBF_FLUSHDYN);
  866. ifp->if_flags &= ~IFF_RUNNING;
  867. }
  868. /*
  869. * Send output from the bridge. The mbuf has the ethernet header
  870. * already attached. We must enqueue or free the mbuf before exiting.
  871. */
  872. int
  873. bridge_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *sa,
  874. struct rtentry *rt)
  875. {
  876. struct ether_header *eh;
  877. struct ifnet *dst_if = NULL;
  878. struct bridge_rtnode *dst_p = NULL;
  879. struct ether_addr *dst;
  880. struct bridge_softc *sc;
  881. int error;
  882. /* ifp must be a member interface of the bridge. */
  883. if (ifp->if_bridgeport == NULL) {
  884. m_freem(m);
  885. return (EINVAL);
  886. }
  887. sc = ((struct bridge_iflist *)ifp->if_bridgeport)->bridge_sc;
  888. if (m->m_len < sizeof(*eh)) {
  889. m = m_pullup(m, sizeof(*eh));
  890. if (m == NULL)
  891. return (ENOBUFS);
  892. }
  893. eh = mtod(m, struct ether_header *);
  894. dst = (struct ether_addr *)&eh->ether_dhost[0];
  895. /*
  896. * If bridge is down, but original output interface is up,
  897. * go ahead and send out that interface. Otherwise the packet
  898. * is dropped below.
  899. */
  900. if ((sc->sc_if.if_flags & IFF_RUNNING) == 0) {
  901. dst_if = ifp;
  902. goto sendunicast;
  903. }
  904. #if NBPFILTER > 0
  905. if (sc->sc_if.if_bpf)
  906. bpf_mtap(sc->sc_if.if_bpf, m, BPF_DIRECTION_OUT);
  907. #endif
  908. ifp->if_opackets++;
  909. ifp->if_obytes += m->m_pkthdr.len;
  910. /*
  911. * If the packet is a broadcast or we don't know a better way to
  912. * get there, send to all interfaces.
  913. */
  914. if ((dst_p = bridge_rtlookup(sc, dst)) != NULL)
  915. dst_if = dst_p->brt_if;
  916. if (dst_if == NULL || ETHER_IS_MULTICAST(eh->ether_dhost)) {
  917. struct bridge_iflist *p;
  918. struct mbuf *mc;
  919. int used = 0;
  920. bridge_span(sc, m);
  921. TAILQ_FOREACH(p, &sc->sc_iflist, next) {
  922. dst_if = p->ifp;
  923. if ((dst_if->if_flags & IFF_RUNNING) == 0)
  924. continue;
  925. /*
  926. * If this is not the original output interface,
  927. * and the interface is participating in spanning
  928. * tree, make sure the port is in a state that
  929. * allows forwarding.
  930. */
  931. if (dst_if != ifp &&
  932. (p->bif_flags & IFBIF_STP) &&
  933. (p->bif_state == BSTP_IFSTATE_DISCARDING))
  934. continue;
  935. #if NMPW > 0
  936. /*
  937. * Split horizon: avoid broadcasting messages from
  938. * wire to another wire.
  939. */
  940. if (ifp->if_type == IFT_MPLSTUNNEL &&
  941. dst_if->if_type == IFT_MPLSTUNNEL)
  942. continue;
  943. #endif /* NMPW */
  944. if ((p->bif_flags & IFBIF_DISCOVER) == 0 &&
  945. (m->m_flags & (M_BCAST | M_MCAST)) == 0)
  946. continue;
  947. if (IF_QFULL(&dst_if->if_snd)) {
  948. IF_DROP(&dst_if->if_snd);
  949. sc->sc_if.if_oerrors++;
  950. continue;
  951. }
  952. if (TAILQ_NEXT(p, next) == NULL) {
  953. used = 1;
  954. mc = m;
  955. } else {
  956. mc = bridge_m_dup(m);
  957. if (mc == NULL) {
  958. sc->sc_if.if_oerrors++;
  959. continue;
  960. }
  961. }
  962. error = bridge_ifenqueue(sc, dst_if, mc);
  963. if (error)
  964. continue;
  965. }
  966. if (!used)
  967. m_freem(m);
  968. return (0);
  969. }
  970. sendunicast:
  971. if (dst_p != NULL && dst_p->brt_tunnel.sa.sa_family != AF_UNSPEC &&
  972. (sa = bridge_tunneltag(m, dst_p->brt_tunnel.sa.sa_family)) != NULL)
  973. memcpy(sa, &dst_p->brt_tunnel.sa, dst_p->brt_tunnel.sa.sa_len);
  974. bridge_span(sc, m);
  975. if ((dst_if->if_flags & IFF_RUNNING) == 0) {
  976. m_freem(m);
  977. return (ENETDOWN);
  978. }
  979. bridge_ifenqueue(sc, dst_if, m);
  980. return (0);
  981. }
  982. /*
  983. * Start output on the bridge. This function should never be called.
  984. */
  985. void
  986. bridge_start(struct ifnet *ifp)
  987. {
  988. }
  989. /*
  990. * Loop through each bridge interface and process their input queues.
  991. */
  992. void
  993. bridgeintr(void)
  994. {
  995. struct bridge_softc *sc;
  996. struct mbuf *m;
  997. int s;
  998. LIST_FOREACH(sc, &bridge_list, sc_list) {
  999. for (;;) {
  1000. s = splnet();
  1001. IF_DEQUEUE(&sc->sc_if.if_snd, m);
  1002. splx(s);
  1003. if (m == NULL)
  1004. break;
  1005. bridgeintr_frame(sc, m);
  1006. }
  1007. }
  1008. }
  1009. /*
  1010. * Process a single frame. Frame must be freed or queued before returning.
  1011. */
  1012. void
  1013. bridgeintr_frame(struct bridge_softc *sc, struct mbuf *m)
  1014. {
  1015. struct ifnet *src_if, *dst_if;
  1016. struct bridge_iflist *ifl;
  1017. struct bridge_rtnode *dst_p;
  1018. struct ether_addr *dst, *src;
  1019. struct ether_header eh;
  1020. int len;
  1021. if ((sc->sc_if.if_flags & IFF_RUNNING) == 0) {
  1022. m_freem(m);
  1023. return;
  1024. }
  1025. src_if = if_get(m->m_pkthdr.ph_ifidx);
  1026. if (src_if == NULL) {
  1027. m_freem(m);
  1028. return;
  1029. }
  1030. sc->sc_if.if_ipackets++;
  1031. sc->sc_if.if_ibytes += m->m_pkthdr.len;
  1032. ifl = (struct bridge_iflist *)src_if->if_bridgeport;
  1033. if (ifl == NULL) {
  1034. m_freem(m);
  1035. return;
  1036. }
  1037. if ((ifl->bif_flags & IFBIF_STP) &&
  1038. (ifl->bif_state == BSTP_IFSTATE_DISCARDING)) {
  1039. m_freem(m);
  1040. return;
  1041. }
  1042. if (m->m_pkthdr.len < sizeof(eh)) {
  1043. m_freem(m);
  1044. return;
  1045. }
  1046. m_copydata(m, 0, ETHER_HDR_LEN, (caddr_t)&eh);
  1047. dst = (struct ether_addr *)&eh.ether_dhost[0];
  1048. src = (struct ether_addr *)&eh.ether_shost[0];
  1049. /*
  1050. * If interface is learning, and if source address
  1051. * is not broadcast or multicast, record its address.
  1052. */
  1053. if ((ifl->bif_flags & IFBIF_LEARNING) &&
  1054. (eh.ether_shost[0] & 1) == 0 &&
  1055. !(eh.ether_shost[0] == 0 && eh.ether_shost[1] == 0 &&
  1056. eh.ether_shost[2] == 0 && eh.ether_shost[3] == 0 &&
  1057. eh.ether_shost[4] == 0 && eh.ether_shost[5] == 0))
  1058. bridge_rtupdate(sc, src, src_if, 0, IFBAF_DYNAMIC, m);
  1059. if ((ifl->bif_flags & IFBIF_STP) &&
  1060. (ifl->bif_state == BSTP_IFSTATE_LEARNING)) {
  1061. m_freem(m);
  1062. return;
  1063. }
  1064. /*
  1065. * At this point, the port either doesn't participate in stp or
  1066. * it's in the forwarding state
  1067. */
  1068. /*
  1069. * If packet is unicast, destined for someone on "this"
  1070. * side of the bridge, drop it.
  1071. */
  1072. if (!ETHER_IS_MULTICAST(eh.ether_dhost)) {
  1073. if ((dst_p = bridge_rtlookup(sc, dst)) != NULL)
  1074. dst_if = dst_p->brt_if;
  1075. else
  1076. dst_if = NULL;
  1077. if (dst_if == src_if) {
  1078. m_freem(m);
  1079. return;
  1080. }
  1081. } else {
  1082. if (memcmp(etherbroadcastaddr, eh.ether_dhost,
  1083. sizeof(etherbroadcastaddr)) == 0)
  1084. m->m_flags |= M_BCAST;
  1085. else
  1086. m->m_flags |= M_MCAST;
  1087. dst_if = NULL;
  1088. }
  1089. /*
  1090. * Multicast packets get handled a little differently:
  1091. * If interface is:
  1092. * -link0,-link1 (default) Forward all multicast
  1093. * as broadcast.
  1094. * -link0,link1 Drop non-IP multicast, forward
  1095. * as broadcast IP multicast.
  1096. * link0,-link1 Drop IP multicast, forward as
  1097. * broadcast non-IP multicast.
  1098. * link0,link1 Drop all multicast.
  1099. */
  1100. if (m->m_flags & M_MCAST) {
  1101. if ((sc->sc_if.if_flags &
  1102. (IFF_LINK0 | IFF_LINK1)) ==
  1103. (IFF_LINK0 | IFF_LINK1)) {
  1104. m_freem(m);
  1105. return;
  1106. }
  1107. if (sc->sc_if.if_flags & IFF_LINK0 &&
  1108. ETHERADDR_IS_IP_MCAST(dst)) {
  1109. m_freem(m);
  1110. return;
  1111. }
  1112. if (sc->sc_if.if_flags & IFF_LINK1 &&
  1113. !ETHERADDR_IS_IP_MCAST(dst)) {
  1114. m_freem(m);
  1115. return;
  1116. }
  1117. }
  1118. if (ifl->bif_flags & IFBIF_BLOCKNONIP && bridge_blocknonip(&eh, m)) {
  1119. m_freem(m);
  1120. return;
  1121. }
  1122. if (bridge_filterrule(&ifl->bif_brlin, &eh, m) == BRL_ACTION_BLOCK) {
  1123. m_freem(m);
  1124. return;
  1125. }
  1126. m = bridge_ip(sc, BRIDGE_IN, src_if, &eh, m);
  1127. if (m == NULL)
  1128. return;
  1129. /*
  1130. * If the packet is a multicast or broadcast OR if we don't
  1131. * know any better, forward it to all interfaces.
  1132. */
  1133. if ((m->m_flags & (M_BCAST | M_MCAST)) || dst_if == NULL) {
  1134. sc->sc_if.if_imcasts++;
  1135. bridge_broadcast(sc, src_if, &eh, m);
  1136. return;
  1137. }
  1138. /*
  1139. * At this point, we're dealing with a unicast frame going to a
  1140. * different interface
  1141. */
  1142. if ((dst_if->if_flags & IFF_RUNNING) == 0) {
  1143. m_freem(m);
  1144. return;
  1145. }
  1146. ifl = (struct bridge_iflist *)dst_if->if_bridgeport;
  1147. if ((ifl->bif_flags & IFBIF_STP) &&
  1148. (ifl->bif_state == BSTP_IFSTATE_DISCARDING)) {
  1149. m_freem(m);
  1150. return;
  1151. }
  1152. if (bridge_filterrule(&ifl->bif_brlout, &eh, m) == BRL_ACTION_BLOCK) {
  1153. m_freem(m);
  1154. return;
  1155. }
  1156. m = bridge_ip(sc, BRIDGE_OUT, dst_if, &eh, m);
  1157. if (m == NULL)
  1158. return;
  1159. len = m->m_pkthdr.len;
  1160. #if NVLAN > 0
  1161. if ((m->m_flags & M_VLANTAG) &&
  1162. (dst_if->if_capabilities & IFCAP_VLAN_HWTAGGING) == 0)
  1163. len += ETHER_VLAN_ENCAP_LEN;
  1164. #endif
  1165. if ((len - ETHER_HDR_LEN) > dst_if->if_mtu)
  1166. bridge_fragment(sc, dst_if, &eh, m);
  1167. else {
  1168. bridge_ifenqueue(sc, dst_if, m);
  1169. }
  1170. }
  1171. /*
  1172. * Receive input from an interface. Queue the packet for bridging if its
  1173. * not for us, and schedule an interrupt.
  1174. */
  1175. struct mbuf *
  1176. bridge_input(struct ifnet *ifp, struct mbuf *m)
  1177. {
  1178. struct bridge_softc *sc;
  1179. struct bridge_iflist *ifl;
  1180. struct bridge_iflist *srcifl;
  1181. struct ether_header *eh;
  1182. struct arpcom *ac;
  1183. struct mbuf_list ml = MBUF_LIST_INITIALIZER();
  1184. struct mbuf *mc;
  1185. int s;
  1186. if ((m->m_flags & M_PKTHDR) == 0)
  1187. panic("bridge_input(): no HDR");
  1188. ifl = (struct bridge_iflist *)ifp->if_bridgeport;
  1189. if (ifl == NULL)
  1190. return (m);
  1191. sc = ifl->bridge_sc;
  1192. if ((sc->sc_if.if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
  1193. return (m);
  1194. #if NBPFILTER > 0
  1195. if (sc->sc_if.if_bpf)
  1196. bpf_mtap_ether(sc->sc_if.if_bpf, m, BPF_DIRECTION_IN);
  1197. #endif
  1198. bridge_span(sc, m);
  1199. eh = mtod(m, struct ether_header *);
  1200. if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
  1201. /*
  1202. * Reserved destination MAC addresses (01:80:C2:00:00:0x)
  1203. * should not be forwarded to bridge members according to
  1204. * section 7.12.6 of the 802.1D-2004 specification. The
  1205. * STP destination address (as stored in bstp_etheraddr)
  1206. * is the first of these.
  1207. */
  1208. if (bcmp(eh->ether_dhost, bstp_etheraddr, ETHER_ADDR_LEN - 1)
  1209. == 0) {
  1210. if (eh->ether_dhost[ETHER_ADDR_LEN - 1] == 0) {
  1211. /* STP traffic */
  1212. if ((m = bstp_input(sc->sc_stp, ifl->bif_stp,
  1213. eh, m)) == NULL)
  1214. return (NULL);
  1215. } else if (eh->ether_dhost[ETHER_ADDR_LEN - 1] <= 0xf) {
  1216. m_freem(m);
  1217. return (NULL);
  1218. }
  1219. }
  1220. /*
  1221. * No need to queue frames for ifs in the discarding state
  1222. */
  1223. if ((ifl->bif_flags & IFBIF_STP) &&
  1224. (ifl->bif_state == BSTP_IFSTATE_DISCARDING))
  1225. return (m);
  1226. mc = bridge_m_dup(m);
  1227. if (mc == NULL)
  1228. return (m);
  1229. s = splnet();
  1230. if (IF_QFULL(&sc->sc_if.if_snd)) {
  1231. m_freem(mc);
  1232. splx(s);
  1233. return (m);
  1234. }
  1235. IF_ENQUEUE(&sc->sc_if.if_snd, mc);
  1236. splx(s);
  1237. schednetisr(NETISR_BRIDGE);
  1238. #if NGIF > 0
  1239. if (ifp->if_type == IFT_GIF) {
  1240. TAILQ_FOREACH(ifl, &sc->sc_iflist, next) {
  1241. if (ifl->ifp->if_type != IFT_ETHER)
  1242. continue;
  1243. m->m_flags |= M_PROTO1;
  1244. ml_enqueue(&ml, m);
  1245. if_input(ifl->ifp, &ml);
  1246. return (NULL);
  1247. }
  1248. }
  1249. #endif /* NGIF */
  1250. return (m);
  1251. }
  1252. /*
  1253. * No need to queue frames for ifs in the discarding state
  1254. */
  1255. if ((ifl->bif_flags & IFBIF_STP) &&
  1256. (ifl->bif_state == BSTP_IFSTATE_DISCARDING))
  1257. return (m);
  1258. /*
  1259. * Unicast, make sure it's not for us.
  1260. */
  1261. srcifl = ifl;
  1262. TAILQ_FOREACH(ifl, &sc->sc_iflist, next) {
  1263. if (ifl->ifp->if_type != IFT_ETHER)
  1264. continue;
  1265. ac = (struct arpcom *)ifl->ifp;
  1266. if (bcmp(ac->ac_enaddr, eh->ether_dhost, ETHER_ADDR_LEN) == 0
  1267. #if NCARP > 0
  1268. || (ifl->ifp->if_carp && carp_ourether(ifl->ifp->if_carp,
  1269. (u_int8_t *)&eh->ether_dhost) != NULL)
  1270. #endif
  1271. ) {
  1272. if (srcifl->bif_flags & IFBIF_LEARNING)
  1273. bridge_rtupdate(sc,
  1274. (struct ether_addr *)&eh->ether_shost,
  1275. ifp, 0, IFBAF_DYNAMIC, m);
  1276. if (bridge_filterrule(&srcifl->bif_brlin, eh, m) ==
  1277. BRL_ACTION_BLOCK) {
  1278. m_freem(m);
  1279. return (NULL);
  1280. }
  1281. /* Count for the bridge */
  1282. sc->sc_if.if_ipackets++;
  1283. sc->sc_if.if_ibytes += m->m_pkthdr.len;
  1284. m->m_flags |= M_PROTO1;
  1285. ml_enqueue(&ml, m);
  1286. if_input(ifl->ifp, &ml);
  1287. return (NULL);
  1288. }
  1289. if (bcmp(ac->ac_enaddr, eh->ether_shost, ETHER_ADDR_LEN) == 0
  1290. #if NCARP > 0
  1291. || (ifl->ifp->if_carp && carp_ourether(ifl->ifp->if_carp,
  1292. (u_int8_t *)&eh->ether_shost) != NULL)
  1293. #endif
  1294. ) {
  1295. m_freem(m);
  1296. return (NULL);
  1297. }
  1298. }
  1299. s = splnet();
  1300. if (IF_QFULL(&sc->sc_if.if_snd)) {
  1301. m_freem(m);
  1302. splx(s);
  1303. return (NULL);
  1304. }
  1305. IF_ENQUEUE(&sc->sc_if.if_snd, m);
  1306. splx(s);
  1307. schednetisr(NETISR_BRIDGE);
  1308. return (NULL);
  1309. }
  1310. /*
  1311. * Send a frame to all interfaces that are members of the bridge
  1312. * (except the one it came in on).
  1313. */
  1314. void
  1315. bridge_broadcast(struct bridge_softc *sc, struct ifnet *ifp,
  1316. struct ether_header *eh, struct mbuf *m)
  1317. {
  1318. struct bridge_iflist *p;
  1319. struct mbuf *mc;
  1320. struct ifnet *dst_if;
  1321. int len, used = 0;
  1322. TAILQ_FOREACH(p, &sc->sc_iflist, next) {
  1323. dst_if = p->ifp;
  1324. if ((dst_if->if_flags & IFF_RUNNING) == 0)
  1325. continue;
  1326. if ((p->bif_flags & IFBIF_STP) &&
  1327. (p->bif_state == BSTP_IFSTATE_DISCARDING))
  1328. continue;
  1329. if ((p->bif_flags & IFBIF_DISCOVER) == 0 &&
  1330. (m->m_flags & (M_BCAST | M_MCAST)) == 0)
  1331. continue;
  1332. /* Drop non-IP frames if the appropriate flag is set. */
  1333. if (p->bif_flags & IFBIF_BLOCKNONIP &&
  1334. bridge_blocknonip(eh, m))
  1335. continue;
  1336. if (bridge_filterrule(&p->bif_brlout, eh, m) == BRL_ACTION_BLOCK)
  1337. continue;
  1338. bridge_localbroadcast(sc, dst_if, eh, m);
  1339. /*
  1340. * Don't retransmit out of the same interface where
  1341. * the packet was received from.
  1342. */
  1343. if (dst_if->if_index == ifp->if_index)
  1344. continue;
  1345. #if NMPW > 0
  1346. /*
  1347. * Split horizon: avoid broadcasting messages from wire to
  1348. * another wire.
  1349. */
  1350. if (ifp->if_type == IFT_MPLSTUNNEL &&
  1351. dst_if->if_type == IFT_MPLSTUNNEL)
  1352. continue;
  1353. #endif /* NMPW */
  1354. if (IF_QFULL(&dst_if->if_snd)) {
  1355. IF_DROP(&dst_if->if_snd);
  1356. sc->sc_if.if_oerrors++;
  1357. continue;
  1358. }
  1359. /* If last one, reuse the passed-in mbuf */
  1360. if (TAILQ_NEXT(p, next) == NULL) {
  1361. mc = m;
  1362. used = 1;
  1363. } else {
  1364. mc = bridge_m_dup(m);
  1365. if (mc == NULL) {
  1366. sc->sc_if.if_oerrors++;
  1367. continue;
  1368. }
  1369. }
  1370. mc = bridge_ip(sc, BRIDGE_OUT, dst_if, eh, mc);
  1371. if (mc == NULL)
  1372. continue;
  1373. len = mc->m_pkthdr.len;
  1374. #if NVLAN > 0
  1375. if ((mc->m_flags & M_VLANTAG) &&
  1376. (dst_if->if_capabilities & IFCAP_VLAN_HWTAGGING) == 0)
  1377. len += ETHER_VLAN_ENCAP_LEN;
  1378. #endif
  1379. if ((len - ETHER_HDR_LEN) > dst_if->if_mtu)
  1380. bridge_fragment(sc, dst_if, eh, mc);
  1381. else {
  1382. bridge_ifenqueue(sc, dst_if, mc);
  1383. }
  1384. }
  1385. if (!used)
  1386. m_freem(m);
  1387. }
  1388. void
  1389. bridge_localbroadcast(struct bridge_softc *sc, struct ifnet *ifp,
  1390. struct ether_header *eh, struct mbuf *m)
  1391. {
  1392. struct mbuf_list ml = MBUF_LIST_INITIALIZER();
  1393. struct mbuf *m1;
  1394. u_int16_t etype;
  1395. /*
  1396. * quick optimisation, don't send packets up the stack if no
  1397. * corresponding address has been specified.
  1398. */
  1399. etype = ntohs(eh->ether_type);
  1400. if (!(m->m_flags & M_VLANTAG) && etype == ETHERTYPE_IP) {
  1401. struct ifaddr *ifa;
  1402. TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
  1403. if (ifa->ifa_addr->sa_family == AF_INET)
  1404. break;
  1405. }
  1406. if (ifa == NULL)
  1407. return;
  1408. }
  1409. m1 = m_copym2(m, 0, M_COPYALL, M_DONTWAIT);
  1410. if (m1 == NULL) {
  1411. sc->sc_if.if_oerrors++;
  1412. return;
  1413. }
  1414. m1->m_flags |= M_PROTO1;
  1415. ml_enqueue(&ml, m1);
  1416. if_input(ifp, &ml);
  1417. }
  1418. void
  1419. bridge_span(struct bridge_softc *sc, struct mbuf *m)
  1420. {
  1421. struct bridge_iflist *p;
  1422. struct ifnet *ifp;
  1423. struct mbuf *mc;
  1424. int error;
  1425. TAILQ_FOREACH(p, &sc->sc_spanlist, next) {
  1426. ifp = p->ifp;
  1427. if ((ifp->if_flags & IFF_RUNNING) == 0)
  1428. continue;
  1429. if (IF_QFULL(&ifp->if_snd)) {
  1430. IF_DROP(&ifp->if_snd);
  1431. sc->sc_if.if_oerrors++;
  1432. continue;
  1433. }
  1434. mc = m_copym(m, 0, M_COPYALL, M_DONTWAIT);
  1435. if (mc == NULL) {
  1436. sc->sc_if.if_oerrors++;
  1437. continue;
  1438. }
  1439. error = bridge_ifenqueue(sc, ifp, mc);
  1440. if (error)
  1441. continue;
  1442. }
  1443. }
  1444. struct ifnet *
  1445. bridge_rtupdate(struct bridge_softc *sc, struct ether_addr *ea,
  1446. struct ifnet *ifp, int setflags, u_int8_t flags, struct mbuf *m)
  1447. {
  1448. struct bridge_rtnode *p, *q;
  1449. struct sockaddr *sa = NULL;
  1450. u_int32_t h;
  1451. int dir;
  1452. if (m != NULL) {
  1453. /* Check if the mbuf was tagged with a tunnel endpoint addr */
  1454. sa = bridge_tunnel(m);
  1455. }
  1456. h = bridge_hash(sc, ea);
  1457. p = LIST_FIRST(&sc->sc_rts[h]);
  1458. if (p == NULL) {
  1459. if (sc->sc_brtcnt >= sc->sc_brtmax)
  1460. goto done;
  1461. p = malloc(sizeof(*p), M_DEVBUF, M_NOWAIT);
  1462. if (p == NULL)
  1463. goto done;
  1464. bcopy(ea, &p->brt_addr, sizeof(p->brt_addr));
  1465. p->brt_if = ifp;
  1466. p->brt_age = 1;
  1467. bridge_copyaddr(sa, (struct sockaddr *)&p->brt_tunnel);
  1468. if (setflags)
  1469. p->brt_flags = flags;
  1470. else
  1471. p->brt_flags = IFBAF_DYNAMIC;
  1472. LIST_INSERT_HEAD(&sc->sc_rts[h], p, brt_next);
  1473. sc->sc_brtcnt++;
  1474. goto want;
  1475. }
  1476. do {
  1477. q = p;
  1478. p = LIST_NEXT(p, brt_next);
  1479. dir = memcmp(ea, &q->brt_addr, sizeof(q->brt_addr));
  1480. if (dir == 0) {
  1481. if (setflags) {
  1482. q->brt_if = ifp;
  1483. q->brt_flags = flags;
  1484. } else if (!(q->brt_flags & IFBAF_STATIC))
  1485. q->brt_if = ifp;
  1486. if (q->brt_if == ifp)
  1487. q->brt_age = 1;
  1488. ifp = q->brt_if;
  1489. bridge_copyaddr(sa,
  1490. (struct sockaddr *)&q->brt_tunnel);
  1491. goto want;
  1492. }
  1493. if (dir > 0) {
  1494. if (sc->sc_brtcnt >= sc->sc_brtmax)
  1495. goto done;
  1496. p = malloc(sizeof(*p), M_DEVBUF, M_NOWAIT);
  1497. if (p == NULL)
  1498. goto done;
  1499. bcopy(ea, &p->brt_addr, sizeof(p->brt_addr));
  1500. p->brt_if = ifp;
  1501. p->brt_age = 1;
  1502. bridge_copyaddr(sa,
  1503. (struct sockaddr *)&p->brt_tunnel);
  1504. if (setflags)
  1505. p->brt_flags = flags;
  1506. else
  1507. p->brt_flags = IFBAF_DYNAMIC;
  1508. LIST_INSERT_BEFORE(q, p, brt_next);
  1509. sc->sc_brtcnt++;
  1510. goto want;
  1511. }
  1512. if (p == NULL) {
  1513. if (sc->sc_brtcnt >= sc->sc_brtmax)
  1514. goto done;
  1515. p = malloc(sizeof(*p), M_DEVBUF, M_NOWAIT);
  1516. if (p == NULL)
  1517. goto done;
  1518. bcopy(ea, &p->brt_addr, sizeof(p->brt_addr));
  1519. p->brt_if = ifp;
  1520. p->brt_age = 1;
  1521. bridge_copyaddr(sa,
  1522. (struct sockaddr *)&p->brt_tunnel);
  1523. if (setflags)
  1524. p->brt_flags = flags;
  1525. else
  1526. p->brt_flags = IFBAF_DYNAMIC;
  1527. LIST_INSERT_AFTER(q, p, brt_next);
  1528. sc->sc_brtcnt++;
  1529. goto want;
  1530. }
  1531. } while (p != NULL);
  1532. done:
  1533. ifp = NULL;
  1534. want:
  1535. return (ifp);
  1536. }
  1537. struct bridge_rtnode *
  1538. bridge_rtlookup(struct bridge_softc *sc, struct ether_addr *ea)
  1539. {
  1540. struct bridge_rtnode *p;
  1541. u_int32_t h;
  1542. int dir;
  1543. h = bridge_hash(sc, ea);
  1544. LIST_FOREACH(p, &sc->sc_rts[h], brt_next) {
  1545. dir = memcmp(ea, &p->brt_addr, sizeof(p->brt_addr));
  1546. if (dir == 0)
  1547. return (p);
  1548. if (dir > 0)
  1549. goto fail;
  1550. }
  1551. fail:
  1552. return (NULL);
  1553. }
  1554. u_int32_t
  1555. bridge_hash(struct bridge_softc *sc, struct ether_addr *addr)
  1556. {
  1557. return SipHash24((SIPHASH_KEY *)sc->sc_hashkey, addr, ETHER_ADDR_LEN) &
  1558. BRIDGE_RTABLE_MASK;
  1559. }
  1560. void
  1561. bridge_timer(void *vsc)
  1562. {
  1563. struct bridge_softc *sc = vsc;
  1564. int s;
  1565. s = splsoftnet();
  1566. bridge_rtage(sc);
  1567. splx(s);
  1568. }
  1569. /*
  1570. * Perform an aging cycle
  1571. */
  1572. void
  1573. bridge_rtage(struct bridge_softc *sc)
  1574. {
  1575. struct bridge_rtnode *n, *p;
  1576. int i;
  1577. for (i = 0; i < BRIDGE_RTABLE_SIZE; i++) {
  1578. n = LIST_FIRST(&sc->sc_rts[i]);
  1579. while (n != NULL) {
  1580. if ((n->brt_flags & IFBAF_TYPEMASK) == IFBAF_STATIC) {
  1581. n->brt_age = !n->brt_age;
  1582. if (n->brt_age)
  1583. n->brt_age = 0;
  1584. n = LIST_NEXT(n, brt_next);
  1585. } else if (n->brt_age) {
  1586. n->brt_age = 0;
  1587. n = LIST_NEXT(n, brt_next);
  1588. } else {
  1589. p = LIST_NEXT(n, brt_next);
  1590. LIST_REMOVE(n, brt_next);
  1591. sc->sc_brtcnt--;
  1592. free(n, M_DEVBUF, 0);
  1593. n = p;
  1594. }
  1595. }
  1596. }
  1597. if (sc->sc_brttimeout != 0)
  1598. timeout_add_sec(&sc->sc_brtimeout, sc->sc_brttimeout);
  1599. }
  1600. void
  1601. bridge_rtagenode(struct ifnet *ifp, int age)
  1602. {
  1603. struct bridge_softc *sc;
  1604. struct bridge_rtnode *n;
  1605. int i;
  1606. sc = ((struct bridge_iflist *)ifp->if_bridgeport)->bridge_sc;
  1607. if (sc == NULL)
  1608. return;
  1609. /*
  1610. * If the age is zero then flush, otherwise set all the expiry times to
  1611. * age for the interface
  1612. */
  1613. if (age == 0)
  1614. bridge_rtdelete(sc, ifp, 1);
  1615. else {
  1616. for (i = 0; i < BRIDGE_RTABLE_SIZE; i++) {
  1617. LIST_FOREACH(n, &sc->sc_rts[i], brt_next) {
  1618. /* Cap the expiry time to 'age' */
  1619. if (n->brt_if == ifp &&
  1620. n->brt_age > time_uptime + age &&
  1621. (n->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC)
  1622. n->brt_age = time_uptime + age;
  1623. }
  1624. }
  1625. }
  1626. }
  1627. /*
  1628. * Remove all dynamic addresses from the cache
  1629. */
  1630. void
  1631. bridge_rtflush(struct bridge_softc *sc, int full)
  1632. {
  1633. int i;
  1634. struct bridge_rtnode *p, *n;
  1635. for (i = 0; i < BRIDGE_RTABLE_SIZE; i++) {
  1636. n = LIST_FIRST(&sc->sc_rts[i]);
  1637. while (n != NULL) {
  1638. if (full ||
  1639. (n->brt_flags & IFBAF_TYPEMASK) == IFBAF_DYNAMIC) {
  1640. p = LIST_NEXT(n, brt_next);
  1641. LIST_REMOVE(n, brt_next);
  1642. sc->sc_brtcnt--;
  1643. free(n, M_DEVBUF, 0);
  1644. n = p;
  1645. } else
  1646. n = LIST_NEXT(n, brt_next);
  1647. }
  1648. }
  1649. }
  1650. /*
  1651. * Remove an address from the cache
  1652. */
  1653. int
  1654. bridge_rtdaddr(struct bridge_softc *sc, struct ether_addr *ea)
  1655. {
  1656. int h;
  1657. struct bridge_rtnode *p;
  1658. h = bridge_hash(sc, ea);
  1659. LIST_FOREACH(p, &sc->sc_rts[h], brt_next) {
  1660. if (bcmp(ea, &p->brt_addr, sizeof(p->brt_addr)) == 0) {
  1661. LIST_REMOVE(p, brt_next);
  1662. sc->sc_brtcnt--;
  1663. free(p, M_DEVBUF, 0);
  1664. return (0);
  1665. }
  1666. }
  1667. return (ENOENT);
  1668. }
  1669. /*
  1670. * Delete routes to a specific interface member.
  1671. */
  1672. void
  1673. bridge_rtdelete(struct bridge_softc *sc, struct ifnet *ifp, int dynonly)
  1674. {
  1675. int i;
  1676. struct bridge_rtnode *n, *p;
  1677. /*
  1678. * Loop through all of the hash buckets and traverse each
  1679. * chain looking for routes to this interface.
  1680. */
  1681. for (i = 0; i < BRIDGE_RTABLE_SIZE; i++) {
  1682. n = LIST_FIRST(&sc->sc_rts[i]);
  1683. while (n != NULL) {
  1684. if (n->brt_if != ifp) {
  1685. /* Not ours */
  1686. n = LIST_NEXT(n, brt_next);
  1687. continue;
  1688. }
  1689. if (dynonly &&
  1690. (n->brt_flags & IFBAF_TYPEMASK) != IFBAF_DYNAMIC) {
  1691. /* only deleting dynamics */
  1692. n = LIST_NEXT(n, brt_next);
  1693. continue;
  1694. }
  1695. p = LIST_NEXT(n, brt_next);
  1696. LIST_REMOVE(n, brt_next);
  1697. sc->sc_brtcnt--;
  1698. free(n, M_DEVBUF, 0);
  1699. n = p;
  1700. }
  1701. }
  1702. }
  1703. /*
  1704. * Gather all of the routes for this interface.
  1705. */
  1706. int
  1707. bridge_rtfind(struct bridge_softc *sc, struct ifbaconf *baconf)
  1708. {
  1709. int i, error = 0, onlycnt = 0;
  1710. u_int32_t cnt = 0;
  1711. struct bridge_rtnode *n;
  1712. struct ifbareq bareq;
  1713. if (baconf->ifbac_len == 0)
  1714. onlycnt = 1;
  1715. for (i = 0, cnt = 0; i < BRIDGE_RTABLE_SIZE; i++) {
  1716. LIST_FOREACH(n, &sc->sc_rts[i], brt_next) {
  1717. if (!onlycnt) {
  1718. if (baconf->ifbac_len < sizeof(struct ifbareq))
  1719. goto done;
  1720. bcopy(sc->sc_if.if_xname, bareq.ifba_name,
  1721. sizeof(bareq.ifba_name));
  1722. bcopy(n->brt_if->if_xname, bareq.ifba_ifsname,
  1723. sizeof(bareq.ifba_ifsname));
  1724. bcopy(&n->brt_addr, &bareq.ifba_dst,
  1725. sizeof(bareq.ifba_dst));
  1726. bridge_copyaddr(&n->brt_tunnel.sa,
  1727. (struct sockaddr *)&bareq.ifba_dstsa);
  1728. bareq.ifba_age = n->brt_age;
  1729. bareq.ifba_flags = n->brt_flags;
  1730. error = copyout((caddr_t)&bareq,
  1731. (caddr_t)(baconf->ifbac_req + cnt), sizeof(bareq));
  1732. if (error)
  1733. goto done;
  1734. baconf->ifbac_len -= sizeof(struct ifbareq);
  1735. }
  1736. cnt++;
  1737. }
  1738. }
  1739. done:
  1740. baconf->ifbac_len = cnt * sizeof(struct ifbareq);
  1741. return (error);
  1742. }
  1743. /*
  1744. * Block non-ip frames:
  1745. * Returns 0 if frame is ip, and 1 if it should be dropped.
  1746. */
  1747. int
  1748. bridge_blocknonip(struct ether_header *eh, struct mbuf *m)
  1749. {
  1750. struct llc llc;
  1751. u_int16_t etype;
  1752. if (m->m_pkthdr.len < ETHER_HDR_LEN)
  1753. return (1);
  1754. #if NVLAN > 0
  1755. if (m->m_flags & M_VLANTAG)
  1756. return (1);
  1757. #endif
  1758. etype = ntohs(eh->ether_type);
  1759. switch (etype) {
  1760. case ETHERTYPE_ARP:
  1761. case ETHERTYPE_REVARP:
  1762. case ETHERTYPE_IP:
  1763. case ETHERTYPE_IPV6:
  1764. return (0);
  1765. }
  1766. if (etype > ETHERMTU)
  1767. return (1);
  1768. if (m->m_pkthdr.len <
  1769. (ETHER_HDR_LEN + LLC_SNAPFRAMELEN))
  1770. return (1);
  1771. m_copydata(m, ETHER_HDR_LEN, LLC_SNAPFRAMELEN,
  1772. (caddr_t)&llc);
  1773. etype = ntohs(llc.llc_snap.ether_type);
  1774. if (llc.llc_dsap == LLC_SNAP_LSAP &&
  1775. llc.llc_ssap == LLC_SNAP_LSAP &&
  1776. llc.llc_control == LLC_UI &&
  1777. llc.llc_snap.org_code[0] == 0 &&
  1778. llc.llc_snap.org_code[1] == 0 &&
  1779. llc.llc_snap.org_code[2] == 0 &&
  1780. (etype == ETHERTYPE_ARP || etype == ETHERTYPE_REVARP ||
  1781. etype == ETHERTYPE_IP || etype == ETHERTYPE_IPV6)) {
  1782. return (0);
  1783. }
  1784. return (1);
  1785. }
  1786. u_int8_t
  1787. bridge_filterrule(struct brl_head *h, struct ether_header *eh, struct mbuf *m)
  1788. {
  1789. struct brl_node *n;
  1790. u_int8_t flags;
  1791. SIMPLEQ_FOREACH(n, h, brl_next) {
  1792. flags = n->brl_flags & (BRL_FLAG_SRCVALID|BRL_FLAG_DSTVALID);
  1793. if (flags == 0)
  1794. goto return_action;
  1795. if (flags == (BRL_FLAG_SRCVALID|BRL_FLAG_DSTVALID)) {
  1796. if (bcmp(eh->ether_shost, &n->brl_src, ETHER_ADDR_LEN))
  1797. continue;
  1798. if (bcmp(eh->ether_dhost, &n->brl_dst, ETHER_ADDR_LEN))
  1799. continue;
  1800. goto return_action;
  1801. }
  1802. if (flags == BRL_FLAG_SRCVALID) {
  1803. if (bcmp(eh->ether_shost, &n->brl_src, ETHER_ADDR_LEN))
  1804. continue;
  1805. goto return_action;
  1806. }
  1807. if (flags == BRL_FLAG_DSTVALID) {
  1808. if (bcmp(eh->ether_dhost, &n->brl_dst, ETHER_ADDR_LEN))
  1809. continue;
  1810. goto return_action;
  1811. }
  1812. }
  1813. return (BRL_ACTION_PASS);
  1814. return_action:
  1815. #if NPF > 0
  1816. pf_tag_packet(m, n->brl_tag, -1);
  1817. #endif
  1818. return (n->brl_action);
  1819. }
  1820. int
  1821. bridge_addrule(struct bridge_iflist *bif, struct ifbrlreq *req, int out)
  1822. {
  1823. struct brl_node *n;
  1824. n = malloc(sizeof(*n), M_DEVBUF, M_NOWAIT);
  1825. if (n == NULL)
  1826. return (ENOMEM);
  1827. bcopy(&req->ifbr_src, &n->brl_src, sizeof(struct ether_addr));
  1828. bcopy(&req->ifbr_dst, &n->brl_dst, sizeof(struct ether_addr));
  1829. n->brl_action = req->ifbr_action;
  1830. n->brl_flags = req->ifbr_flags;
  1831. #if NPF > 0
  1832. if (req->ifbr_tagname[0])
  1833. n->brl_tag = pf_tagname2tag(req->ifbr_tagname, 1);
  1834. else
  1835. n->brl_tag = 0;
  1836. #endif
  1837. if (out) {
  1838. n->brl_flags &= ~BRL_FLAG_IN;
  1839. n->brl_flags |= BRL_FLAG_OUT;
  1840. SIMPLEQ_INSERT_TAIL(&bif->bif_brlout, n, brl_next);
  1841. } else {
  1842. n->brl_flags &= ~BRL_FLAG_OUT;
  1843. n->brl_flags |= BRL_FLAG_IN;
  1844. SIMPLEQ_INSERT_TAIL(&bif->bif_brlin, n, brl_next);
  1845. }
  1846. return (0);
  1847. }
  1848. void
  1849. bridge_flushrule(struct bridge_iflist *bif)
  1850. {
  1851. struct brl_node *p;
  1852. while (!SIMPLEQ_EMPTY(&bif->bif_brlin)) {
  1853. p = SIMPLEQ_FIRST(&bif->bif_brlin);
  1854. SIMPLEQ_REMOVE_HEAD(&bif->bif_brlin, brl_next);
  1855. #if NPF > 0
  1856. pf_tag_unref(p->brl_tag);
  1857. #endif
  1858. free(p, M_DEVBUF, 0);
  1859. }
  1860. while (!SIMPLEQ_EMPTY(&bif->bif_brlout)) {
  1861. p = SIMPLEQ_FIRST(&bif->bif_brlout);
  1862. SIMPLEQ_REMOVE_HEAD(&bif->bif_brlout, brl_next);
  1863. #if NPF > 0
  1864. pf_tag_unref(p->brl_tag);
  1865. #endif
  1866. free(p, M_DEVBUF, 0);
  1867. }
  1868. }
  1869. #ifdef IPSEC
  1870. int
  1871. bridge_ipsec(struct bridge_softc *sc, struct ifnet *ifp,
  1872. struct ether_header *eh, int hassnap, struct llc *llc,
  1873. int dir, int af, int hlen, struct mbuf *m)
  1874. {
  1875. union sockaddr_union dst;
  1876. struct tdb *tdb;
  1877. u_int32_t spi;
  1878. u_int16_t cpi;
  1879. int error, off, s;
  1880. u_int8_t proto = 0;
  1881. struct ip *ip;
  1882. #ifdef INET6
  1883. struct ip6_hdr *ip6;
  1884. #endif /* INET6 */
  1885. #if NPF > 0
  1886. struct ifnet *encif;
  1887. #endif
  1888. if (dir == BRIDGE_IN) {
  1889. switch (af) {
  1890. case AF_INET:
  1891. if (m->m_pkthdr.len - hlen < 2 * sizeof(u_int32_t))
  1892. break;
  1893. ip = mtod(m, struct ip *);
  1894. proto = ip->ip_p;
  1895. off = offsetof(struct ip, ip_p);
  1896. if (proto != IPPROTO_ESP && proto != IPPROTO_AH &&
  1897. proto != IPPROTO_IPCOMP)
  1898. goto skiplookup;
  1899. bzero(&dst, sizeof(union sockaddr_union));
  1900. dst.sa.sa_family = AF_INET;
  1901. dst.sin.sin_len = sizeof(struct sockaddr_in);
  1902. m_copydata(m, offsetof(struct ip, ip_dst),
  1903. sizeof(struct in_addr),
  1904. (caddr_t)&dst.sin.sin_addr);
  1905. if (ip->ip_p == IPPROTO_ESP)
  1906. m_copydata(m, hlen, sizeof(u_int32_t),
  1907. (caddr_t)&spi);
  1908. else if (ip->ip_p == IPPROTO_AH)
  1909. m_copydata(m, hlen + sizeof(u_int32_t),
  1910. sizeof(u_int32_t), (caddr_t)&spi);
  1911. else if (ip->ip_p == IPPROTO_IPCOMP) {
  1912. m_copydata(m, hlen + sizeof(u_int16_t),
  1913. sizeof(u_int16_t), (caddr_t)&cpi);
  1914. spi = ntohl(htons(cpi));
  1915. }
  1916. break;
  1917. #ifdef INET6
  1918. case AF_INET6:
  1919. if (m->m_pkthdr.len - hlen < 2 * sizeof(u_int32_t))
  1920. break;
  1921. ip6 = mtod(m, struct ip6_hdr *);
  1922. /* XXX We should chase down the header chain */
  1923. proto = ip6->ip6_nxt;
  1924. off = offsetof(struct ip6_hdr, ip6_nxt);
  1925. if (proto != IPPROTO_ESP && proto != IPPROTO_AH &&
  1926. proto != IPPROTO_IPCOMP)
  1927. goto skiplookup;
  1928. bzero(&dst, sizeof(union sockaddr_union));
  1929. dst.sa.sa_family = AF_INET6;
  1930. dst.sin6.sin6_len = sizeof(struct sockaddr_in6);
  1931. m_copydata(m, offsetof(struct ip6_hdr, ip6_nxt),
  1932. sizeof(struct in6_addr),
  1933. (caddr_t)&dst.sin6.sin6_addr);
  1934. if (proto == IPPROTO_ESP)
  1935. m_copydata(m, hlen, sizeof(u_int32_t),
  1936. (caddr_t)&spi);
  1937. else if (proto == IPPROTO_AH)
  1938. m_copydata(m, hlen + sizeof(u_int32_t),
  1939. sizeof(u_int32_t), (caddr_t)&spi);
  1940. else if (proto == IPPROTO_IPCOMP) {
  1941. m_copydata(m, hlen + sizeof(u_int16_t),
  1942. sizeof(u_int16_t), (caddr_t)&cpi);
  1943. spi = ntohl(htons(cpi));
  1944. }
  1945. break;
  1946. #endif /* INET6 */
  1947. default:
  1948. return (0);
  1949. }
  1950. if (proto == 0)
  1951. goto skiplookup;
  1952. s = splsoftnet();
  1953. tdb = gettdb(ifp->if_rdomain, spi, &dst, proto);
  1954. if (tdb != NULL && (tdb->tdb_flags & TDBF_INVALID) == 0 &&
  1955. tdb->tdb_xform != NULL) {
  1956. if (tdb->tdb_first_use == 0) {
  1957. tdb->tdb_first_use = time_second;
  1958. if (tdb->tdb_flags & TDBF_FIRSTUSE)
  1959. timeout_add_sec(&tdb->tdb_first_tmo,
  1960. tdb->tdb_exp_first_use);
  1961. if (tdb->tdb_flags & TDBF_SOFT_FIRSTUSE)
  1962. timeout_add_sec(&tdb->tdb_sfirst_tmo,
  1963. tdb->tdb_soft_first_use);
  1964. }
  1965. (*(tdb->tdb_xform->xf_input))(m, tdb, hlen, off);
  1966. splx(s);
  1967. return (1);
  1968. } else {
  1969. splx(s);
  1970. skiplookup:
  1971. /* XXX do an input policy lookup */
  1972. return (0);
  1973. }
  1974. } else { /* Outgoing from the bridge. */
  1975. tdb = ipsp_spd_lookup(m, af, hlen, &error,
  1976. IPSP_DIRECTION_OUT, NULL, NULL, 0);
  1977. if (tdb != NULL) {
  1978. /*
  1979. * We don't need to do loop detection, the
  1980. * bridge will do that for us.
  1981. */
  1982. #if NPF > 0
  1983. if ((encif = enc_getif(tdb->tdb_rdomain,
  1984. tdb->tdb_tap)) == NULL ||
  1985. pf_test(af, dir, encif, &m) != PF_PASS) {
  1986. m_freem(m);
  1987. return (1);
  1988. }
  1989. if (m == NULL)
  1990. return (1);
  1991. else if (af == AF_INET)
  1992. in_proto_cksum_out(m, encif);
  1993. #ifdef INET6
  1994. else if (af == AF_INET6)
  1995. in6_proto_cksum_out(m, encif);
  1996. #endif /* INET6 */
  1997. #endif /* NPF */
  1998. ip = mtod(m, struct ip *);
  1999. if ((af == AF_INET) &&
  2000. ip_mtudisc && (ip->ip_off & htons(IP_DF)) &&
  2001. tdb->tdb_mtu && ntohs(ip->ip_len) > tdb->tdb_mtu &&
  2002. tdb->tdb_mtutimeout > time_second)
  2003. bridge_send_icmp_err(sc, ifp, eh, m,
  2004. hassnap, llc, tdb->tdb_mtu,
  2005. ICMP_UNREACH, ICMP_UNREACH_NEEDFRAG);
  2006. else
  2007. error = ipsp_process_packet(m, tdb, af, 0);
  2008. return (1);
  2009. } else
  2010. return (0);
  2011. }
  2012. return (0);
  2013. }
  2014. #endif /* IPSEC */
  2015. /*
  2016. * Filter IP packets by peeking into the ethernet frame. This violates
  2017. * the ISO model, but allows us to act as a IP filter at the data link
  2018. * layer. As a result, most of this code will look familiar to those
  2019. * who've read net/if_ethersubr.c and netinet/ip_input.c
  2020. */
  2021. struct mbuf *
  2022. bridge_ip(struct bridge_softc *sc, int dir, struct ifnet *ifp,
  2023. struct ether_header *eh, struct mbuf *m)
  2024. {
  2025. struct llc llc;
  2026. int hassnap = 0;
  2027. struct ip *ip;
  2028. int hlen;
  2029. u_int16_t etype;
  2030. #if NVLAN > 0
  2031. if (m->m_flags & M_VLANTAG)
  2032. return (m);
  2033. #endif
  2034. etype = ntohs(eh->ether_type);
  2035. if (etype != ETHERTYPE_IP && etype != ETHERTYPE_IPV6) {
  2036. if (etype > ETHERMTU ||
  2037. m->m_pkthdr.len < (LLC_SNAPFRAMELEN +
  2038. ETHER_HDR_LEN))
  2039. return (m);
  2040. m_copydata(m, ETHER_HDR_LEN,
  2041. LLC_SNAPFRAMELEN, (caddr_t)&llc);
  2042. if (llc.llc_dsap != LLC_SNAP_LSAP ||
  2043. llc.llc_ssap != LLC_SNAP_LSAP ||
  2044. llc.llc_control != LLC_UI ||
  2045. llc.llc_snap.org_code[0] ||
  2046. llc.llc_snap.org_code[1] ||
  2047. llc.llc_snap.org_code[2])
  2048. return (m);
  2049. etype = ntohs(llc.llc_snap.ether_type);
  2050. if (etype != ETHERTYPE_IP && etype != ETHERTYPE_IPV6)
  2051. return (m);
  2052. hassnap = 1;
  2053. }
  2054. m_adj(m, ETHER_HDR_LEN);
  2055. if (hassnap)
  2056. m_adj(m, LLC_SNAPFRAMELEN);
  2057. switch (etype) {
  2058. case ETHERTYPE_IP:
  2059. if (m->m_pkthdr.len < sizeof(struct ip))
  2060. goto dropit;
  2061. /* Copy minimal header, and drop invalids */
  2062. if (m->m_len < sizeof(struct ip) &&
  2063. (m = m_pullup(m, sizeof(struct ip))) == NULL) {
  2064. ipstat.ips_toosmall++;
  2065. return (NULL);
  2066. }
  2067. ip = mtod(m, struct ip *);
  2068. if (ip->ip_v != IPVERSION) {
  2069. ipstat.ips_badvers++;
  2070. goto dropit;
  2071. }
  2072. hlen = ip->ip_hl << 2; /* get whole header length */
  2073. if (hlen < sizeof(struct ip)) {
  2074. ipstat.ips_badhlen++;
  2075. goto dropit;
  2076. }
  2077. if (hlen > m->m_len) {
  2078. if ((m = m_pullup(m, hlen)) == NULL) {
  2079. ipstat.ips_badhlen++;
  2080. return (NULL);
  2081. }
  2082. ip = mtod(m, struct ip *);
  2083. }
  2084. if ((m->m_pkthdr.csum_flags & M_IPV4_CSUM_IN_OK) == 0) {
  2085. if (m->m_pkthdr.csum_flags & M_IPV4_CSUM_IN_BAD) {
  2086. ipstat.ips_badsum++;
  2087. goto dropit;
  2088. }
  2089. ipstat.ips_inswcsum++;
  2090. if (in_cksum(m, hlen) != 0) {
  2091. ipstat.ips_badsum++;
  2092. goto dropit;
  2093. }
  2094. }
  2095. if (ntohs(ip->ip_len) < hlen)
  2096. goto dropit;
  2097. if (m->m_pkthdr.len < ntohs(ip->ip_len))
  2098. goto dropit;
  2099. if (m->m_pkthdr.len > ntohs(ip->ip_len)) {
  2100. if (m->m_len == m->m_pkthdr.len) {
  2101. m->m_len = ntohs(ip->ip_len);
  2102. m->m_pkthdr.len = ntohs(ip->ip_len);
  2103. } else
  2104. m_adj(m, ntohs(ip->ip_len) - m->m_pkthdr.len);
  2105. }
  2106. #ifdef IPSEC
  2107. if ((sc->sc_if.if_flags & IFF_LINK2) == IFF_LINK2 &&
  2108. bridge_ipsec(sc, ifp, eh, hassnap, &llc,
  2109. dir, AF_INET, hlen, m))
  2110. return (NULL);
  2111. #endif /* IPSEC */
  2112. #if NPF > 0
  2113. /* Finally, we get to filter the packet! */
  2114. if (pf_test(AF_INET, dir, ifp, &m) != PF_PASS)
  2115. goto dropit;
  2116. if (m == NULL)
  2117. goto dropit;
  2118. #endif /* NPF > 0 */
  2119. /* Rebuild the IP header */
  2120. if (m->m_len < hlen && ((m = m_pullup(m, hlen)) == NULL))
  2121. return (NULL);
  2122. if (m->m_len < sizeof(struct ip))
  2123. goto dropit;
  2124. in_proto_cksum_out(m, ifp);
  2125. ip = mtod(m, struct ip *);
  2126. ip->ip_sum = 0;
  2127. if (0 && (ifp->if_capabilities & IFCAP_CSUM_IPv4))
  2128. m->m_pkthdr.csum_flags |= M_IPV4_CSUM_OUT;
  2129. else {
  2130. ipstat.ips_outswcsum++;
  2131. ip->ip_sum = in_cksum(m, hlen);
  2132. }
  2133. break;
  2134. #ifdef INET6
  2135. case ETHERTYPE_IPV6: {
  2136. struct ip6_hdr *ip6;
  2137. if (m->m_len < sizeof(struct ip6_hdr)) {
  2138. if ((m = m_pullup(m, sizeof(struct ip6_hdr)))
  2139. == NULL) {
  2140. ip6stat.ip6s_toosmall++;
  2141. return (NULL);
  2142. }
  2143. }
  2144. ip6 = mtod(m, struct ip6_hdr *);
  2145. if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
  2146. struct ifnet *ifp;
  2147. ip6stat.ip6s_badvers++;
  2148. ifp = if_get(m->m_pkthdr.ph_ifidx);
  2149. if (ifp != NULL)
  2150. in6_ifstat_inc(ifp, ifs6_in_hdrerr);
  2151. goto dropit;
  2152. }
  2153. #ifdef IPSEC
  2154. hlen = sizeof(struct ip6_hdr);
  2155. if ((sc->sc_if.if_flags & IFF_LINK2) == IFF_LINK2 &&
  2156. bridge_ipsec(sc, ifp, eh, hassnap, &llc,
  2157. dir, AF_INET6, hlen, m))
  2158. return (NULL);
  2159. #endif /* IPSEC */
  2160. #if NPF > 0
  2161. if (pf_test(AF_INET6, dir, ifp, &m) != PF_PASS)
  2162. goto dropit;
  2163. if (m == NULL)
  2164. return (NULL);
  2165. #endif /* NPF > 0 */
  2166. in6_proto_cksum_out(m, ifp);
  2167. break;
  2168. }
  2169. #endif /* INET6 */
  2170. default:
  2171. goto dropit;
  2172. break;
  2173. }
  2174. /* Reattach SNAP header */
  2175. if (hassnap) {
  2176. M_PREPEND(m, LLC_SNAPFRAMELEN, M_DONTWAIT);
  2177. if (m == NULL)
  2178. goto dropit;
  2179. bcopy(&llc, mtod(m, caddr_t), LLC_SNAPFRAMELEN);
  2180. }
  2181. /* Reattach ethernet header */
  2182. M_PREPEND(m, sizeof(*eh), M_DONTWAIT);
  2183. if (m == NULL)
  2184. goto dropit;
  2185. bcopy(eh, mtod(m, caddr_t), sizeof(*eh));
  2186. return (m);
  2187. dropit:
  2188. m_freem(m);
  2189. return (NULL);
  2190. }
  2191. void
  2192. bridge_fragment(struct bridge_softc *sc, struct ifnet *ifp,
  2193. struct ether_header *eh, struct mbuf *m)
  2194. {
  2195. struct llc llc;
  2196. struct mbuf *m0;
  2197. int error = 0;
  2198. int hassnap = 0;
  2199. u_int16_t etype;
  2200. struct ip *ip;
  2201. etype = ntohs(eh->ether_type);
  2202. #if NVLAN > 0
  2203. if ((m->m_flags & M_VLANTAG) || etype == ETHERTYPE_VLAN ||
  2204. etype == ETHERTYPE_QINQ) {
  2205. int len = m->m_pkthdr.len;
  2206. if (m->m_flags & M_VLANTAG)
  2207. len += ETHER_VLAN_ENCAP_LEN;
  2208. if ((ifp->if_capabilities & IFCAP_VLAN_MTU) &&
  2209. (len - sizeof(struct ether_vlan_header) <= ifp->if_mtu)) {
  2210. bridge_ifenqueue(sc, ifp, m);
  2211. return;
  2212. }
  2213. goto dropit;
  2214. }
  2215. #endif
  2216. if (etype != ETHERTYPE_IP) {
  2217. if (etype > ETHERMTU ||
  2218. m->m_pkthdr.len < (LLC_SNAPFRAMELEN +
  2219. ETHER_HDR_LEN))
  2220. goto dropit;
  2221. m_copydata(m, ETHER_HDR_LEN,
  2222. LLC_SNAPFRAMELEN, (caddr_t)&llc);
  2223. if (llc.llc_dsap != LLC_SNAP_LSAP ||
  2224. llc.llc_ssap != LLC_SNAP_LSAP ||
  2225. llc.llc_control != LLC_UI ||
  2226. llc.llc_snap.org_code[0] ||
  2227. llc.llc_snap.org_code[1] ||
  2228. llc.llc_snap.org_code[2] ||
  2229. llc.llc_snap.ether_type != htons(ETHERTYPE_IP))
  2230. goto dropit;
  2231. hassnap = 1;
  2232. }
  2233. m_adj(m, ETHER_HDR_LEN);
  2234. if (hassnap)
  2235. m_adj(m, LLC_SNAPFRAMELEN);
  2236. if (m->m_len < sizeof(struct ip) &&
  2237. (m = m_pullup(m, sizeof(struct ip))) == NULL)
  2238. goto dropit;
  2239. ip = mtod(m, struct ip *);
  2240. /* Respect IP_DF, return a ICMP_UNREACH_NEEDFRAG. */
  2241. if (ip->ip_off & htons(IP_DF)) {
  2242. bridge_send_icmp_err(sc, ifp, eh, m, hassnap, &llc,
  2243. ifp->if_mtu, ICMP_UNREACH, ICMP_UNREACH_NEEDFRAG);
  2244. return;
  2245. }
  2246. error = ip_fragment(m, ifp, ifp->if_mtu);
  2247. if (error) {
  2248. m = NULL;
  2249. goto dropit;
  2250. }
  2251. for (; m; m = m0) {
  2252. m0 = m->m_nextpkt;
  2253. m->m_nextpkt = NULL;
  2254. if (error == 0) {
  2255. if (hassnap) {
  2256. M_PREPEND(m, LLC_SNAPFRAMELEN, M_DONTWAIT);
  2257. if (m == NULL) {
  2258. error = ENOBUFS;
  2259. continue;
  2260. }
  2261. bcopy(&llc, mtod(m, caddr_t),
  2262. LLC_SNAPFRAMELEN);
  2263. }
  2264. M_PREPEND(m, sizeof(*eh), M_DONTWAIT);
  2265. if (m == NULL) {
  2266. error = ENOBUFS;
  2267. continue;
  2268. }
  2269. bcopy(eh, mtod(m, caddr_t), sizeof(*eh));
  2270. error = bridge_ifenqueue(sc, ifp, m);
  2271. if (error) {
  2272. continue;
  2273. }
  2274. } else
  2275. m_freem(m);
  2276. }
  2277. if (error == 0)
  2278. ipstat.ips_fragmented++;
  2279. return;
  2280. dropit:
  2281. m_freem(m);
  2282. }
  2283. int
  2284. bridge_ifenqueue(struct bridge_softc *sc, struct ifnet *ifp, struct mbuf *m)
  2285. {
  2286. int error, len;
  2287. /* Loop prevention. */
  2288. m->m_flags |= M_PROTO1;
  2289. #if NGIF > 0
  2290. /* Packet needs etherip encapsulation. */
  2291. if (ifp->if_type == IFT_GIF) {
  2292. /* Count packets input into the gif from outside */
  2293. ifp->if_ipackets++;
  2294. ifp->if_ibytes += m->m_pkthdr.len;
  2295. error = gif_encap(ifp, &m, AF_LINK);
  2296. if (error)
  2297. return (error);
  2298. }
  2299. #endif /* NGIF */
  2300. len = m->m_pkthdr.len;
  2301. error = if_enqueue(ifp, m);
  2302. if (error) {
  2303. sc->sc_if.if_oerrors++;
  2304. return (error);
  2305. }
  2306. sc->sc_if.if_opackets++;
  2307. sc->sc_if.if_obytes += len;
  2308. return (0);
  2309. }
  2310. void
  2311. bridge_send_icmp_err(struct bridge_softc *sc, struct ifnet *ifp,
  2312. struct ether_header *eh, struct mbuf *n, int hassnap, struct llc *llc,
  2313. int mtu, int type, int code)
  2314. {
  2315. struct ip *ip;
  2316. struct icmp *icp;
  2317. struct in_addr t;
  2318. struct mbuf *m, *n2;
  2319. int hlen;
  2320. u_int8_t ether_tmp[ETHER_ADDR_LEN];
  2321. n2 = m_copym(n, 0, M_COPYALL, M_DONTWAIT);
  2322. if (!n2) {
  2323. m_freem(n);
  2324. return;
  2325. }
  2326. m = icmp_do_error(n, type, code, 0, mtu);
  2327. if (m == NULL) {
  2328. m_freem(n2);
  2329. return;
  2330. }
  2331. n = n2;
  2332. ip = mtod(m, struct ip *);
  2333. hlen = ip->ip_hl << 2;
  2334. t = ip->ip_dst;
  2335. ip->ip_dst = ip->ip_src;
  2336. ip->ip_src = t;
  2337. m->m_data += hlen;
  2338. m->m_len -= hlen;
  2339. icp = mtod(m, struct icmp *);
  2340. icp->icmp_cksum = 0;
  2341. icp->icmp_cksum = in_cksum(m, ntohs(ip->ip_len) - hlen);
  2342. m->m_data -= hlen;
  2343. m->m_len += hlen;
  2344. ip->ip_v = IPVERSION;
  2345. ip->ip_off &= htons(IP_DF);
  2346. ip->ip_id = htons(ip_randomid());
  2347. ip->ip_ttl = MAXTTL;
  2348. ip->ip_sum = 0;
  2349. ip->ip_sum = in_cksum(m, hlen);
  2350. /* Swap ethernet addresses */
  2351. bcopy(&eh->ether_dhost, &ether_tmp, sizeof(ether_tmp));
  2352. bcopy(&eh->ether_shost, &eh->ether_dhost, sizeof(ether_tmp));
  2353. bcopy(&ether_tmp, &eh->ether_shost, sizeof(ether_tmp));
  2354. /* Reattach SNAP header */
  2355. if (hassnap) {
  2356. M_PREPEND(m, LLC_SNAPFRAMELEN, M_DONTWAIT);
  2357. if (m == NULL)
  2358. goto dropit;
  2359. bcopy(llc, mtod(m, caddr_t), LLC_SNAPFRAMELEN);
  2360. }
  2361. /* Reattach ethernet header */
  2362. M_PREPEND(m, sizeof(*eh), M_DONTWAIT);
  2363. if (m == NULL)
  2364. goto dropit;
  2365. bcopy(eh, mtod(m, caddr_t), sizeof(*eh));
  2366. bridge_output(ifp, m, NULL, NULL);
  2367. m_freem(n);
  2368. return;
  2369. dropit:
  2370. m_freem(n);
  2371. }
  2372. struct sockaddr *
  2373. bridge_tunnel(struct mbuf *m)
  2374. {
  2375. struct m_tag *mtag;
  2376. if ((mtag = m_tag_find(m, PACKET_TAG_TUNNEL, NULL)) == NULL)
  2377. return (NULL);
  2378. return ((struct sockaddr *)(mtag + 1));
  2379. }
  2380. struct sockaddr *
  2381. bridge_tunneltag(struct mbuf *m, int af)
  2382. {
  2383. struct m_tag *mtag;
  2384. size_t len;
  2385. struct sockaddr *sa;
  2386. if ((mtag = m_tag_find(m, PACKET_TAG_TUNNEL, NULL)) != NULL) {
  2387. sa = (struct sockaddr *)(mtag + 1);
  2388. if (sa->sa_family != af) {
  2389. m_tag_delete(m, mtag);
  2390. mtag = NULL;
  2391. }
  2392. }
  2393. if (mtag == NULL) {
  2394. if (af == AF_INET)
  2395. len = sizeof(struct sockaddr_in);
  2396. else if (af == AF_INET6)
  2397. len = sizeof(struct sockaddr_in6);
  2398. else
  2399. return (NULL);
  2400. mtag = m_tag_get(PACKET_TAG_TUNNEL, len, M_NOWAIT);
  2401. if (mtag == NULL)
  2402. return (NULL);
  2403. bzero(mtag + 1, len);
  2404. sa = (struct sockaddr *)(mtag + 1);
  2405. sa->sa_family = af;
  2406. sa->sa_len = len;
  2407. m_tag_prepend(m, mtag);
  2408. }
  2409. return ((struct sockaddr *)(mtag + 1));
  2410. }
  2411. void
  2412. bridge_tunneluntag(struct mbuf *m)
  2413. {
  2414. struct m_tag *mtag;
  2415. if ((mtag = m_tag_find(m, PACKET_TAG_TUNNEL, NULL)) != NULL)
  2416. m_tag_delete(m, mtag);
  2417. }
  2418. void
  2419. bridge_copyaddr(struct sockaddr *src, struct sockaddr *dst)
  2420. {
  2421. if (src != NULL && src->sa_family != AF_UNSPEC)
  2422. memcpy(dst, src, src->sa_len);
  2423. else
  2424. dst->sa_family = AF_UNSPEC;
  2425. }
  2426. /*
  2427. * Specialized deep copy to ensure that the payload after the Ethernet
  2428. * header is nicely aligned.
  2429. */
  2430. struct mbuf *
  2431. bridge_m_dup(struct mbuf *m)
  2432. {
  2433. struct mbuf *m1, *m2, *mx;
  2434. m1 = m_copym2(m, 0, ETHER_HDR_LEN, M_DONTWAIT);
  2435. if (m1 == NULL) {
  2436. return (NULL);
  2437. }
  2438. m2 = m_copym2(m, ETHER_HDR_LEN, M_COPYALL, M_DONTWAIT);
  2439. if (m2 == NULL) {
  2440. m_freem(m1);
  2441. return (NULL);
  2442. }
  2443. for (mx = m1; mx->m_next != NULL; mx = mx->m_next)
  2444. /*EMPTY*/;
  2445. mx->m_next = m2;
  2446. if (m1->m_flags & M_PKTHDR) {
  2447. int len = 0;
  2448. for (mx = m1; mx != NULL; mx = mx->m_next)
  2449. len += mx->m_len;
  2450. m1->m_pkthdr.len = len;
  2451. }
  2452. return (m1);
  2453. }