vxlan_core.c 127 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * VXLAN: Virtual eXtensible Local Area Network
  4. *
  5. * Copyright (c) 2012-2013 Vyatta Inc.
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/errno.h>
  11. #include <linux/slab.h>
  12. #include <linux/udp.h>
  13. #include <linux/igmp.h>
  14. #include <linux/if_ether.h>
  15. #include <linux/ethtool.h>
  16. #include <net/arp.h>
  17. #include <net/ndisc.h>
  18. #include <net/gro.h>
  19. #include <net/ipv6_stubs.h>
  20. #include <net/ip.h>
  21. #include <net/icmp.h>
  22. #include <net/rtnetlink.h>
  23. #include <net/inet_ecn.h>
  24. #include <net/net_namespace.h>
  25. #include <net/netns/generic.h>
  26. #include <net/tun_proto.h>
  27. #include <net/vxlan.h>
  28. #include <net/nexthop.h>
  29. #if IS_ENABLED(CONFIG_IPV6)
  30. #include <net/ip6_tunnel.h>
  31. #include <net/ip6_checksum.h>
  32. #endif
  33. #include "vxlan_private.h"
  34. #define VXLAN_VERSION "0.1"
  35. #define FDB_AGE_DEFAULT 300 /* 5 min */
  36. #define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
  37. /* UDP port for VXLAN traffic.
  38. * The IANA assigned port is 4789, but the Linux default is 8472
  39. * for compatibility with early adopters.
  40. */
  41. static unsigned short vxlan_port __read_mostly = 8472;
  42. module_param_named(udp_port, vxlan_port, ushort, 0444);
  43. MODULE_PARM_DESC(udp_port, "Destination UDP port");
  44. static bool log_ecn_error = true;
  45. module_param(log_ecn_error, bool, 0644);
  46. MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
  47. unsigned int vxlan_net_id;
  48. const u8 all_zeros_mac[ETH_ALEN + 2];
  49. static struct rtnl_link_ops vxlan_link_ops;
  50. static int vxlan_sock_add(struct vxlan_dev *vxlan);
  51. static void vxlan_vs_del_dev(struct vxlan_dev *vxlan);
  52. /* salt for hash table */
  53. static u32 vxlan_salt __read_mostly;
  54. static inline bool vxlan_collect_metadata(struct vxlan_sock *vs)
  55. {
  56. return vs->flags & VXLAN_F_COLLECT_METADATA ||
  57. ip_tunnel_collect_metadata();
  58. }
  59. /* Find VXLAN socket based on network namespace, address family, UDP port,
  60. * enabled unshareable flags and socket device binding (see l3mdev with
  61. * non-default VRF).
  62. */
  63. static struct vxlan_sock *vxlan_find_sock(struct net *net, sa_family_t family,
  64. __be16 port, u32 flags, int ifindex)
  65. {
  66. struct vxlan_sock *vs;
  67. flags &= VXLAN_F_RCV_FLAGS;
  68. hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
  69. if (inet_sk(vs->sock->sk)->inet_sport == port &&
  70. vxlan_get_sk_family(vs) == family &&
  71. vs->flags == flags &&
  72. vs->sock->sk->sk_bound_dev_if == ifindex)
  73. return vs;
  74. }
  75. return NULL;
  76. }
  77. static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs,
  78. int ifindex, __be32 vni,
  79. struct vxlan_vni_node **vninode)
  80. {
  81. struct vxlan_vni_node *vnode;
  82. struct vxlan_dev_node *node;
  83. /* For flow based devices, map all packets to VNI 0 */
  84. if (vs->flags & VXLAN_F_COLLECT_METADATA &&
  85. !(vs->flags & VXLAN_F_VNIFILTER))
  86. vni = 0;
  87. hlist_for_each_entry_rcu(node, vni_head(vs, vni), hlist) {
  88. if (!node->vxlan)
  89. continue;
  90. vnode = NULL;
  91. if (node->vxlan->cfg.flags & VXLAN_F_VNIFILTER) {
  92. vnode = vxlan_vnifilter_lookup(node->vxlan, vni);
  93. if (!vnode)
  94. continue;
  95. } else if (node->vxlan->default_dst.remote_vni != vni) {
  96. continue;
  97. }
  98. if (IS_ENABLED(CONFIG_IPV6)) {
  99. const struct vxlan_config *cfg = &node->vxlan->cfg;
  100. if ((cfg->flags & VXLAN_F_IPV6_LINKLOCAL) &&
  101. cfg->remote_ifindex != ifindex)
  102. continue;
  103. }
  104. if (vninode)
  105. *vninode = vnode;
  106. return node->vxlan;
  107. }
  108. return NULL;
  109. }
  110. /* Look up VNI in a per net namespace table */
  111. static struct vxlan_dev *vxlan_find_vni(struct net *net, int ifindex,
  112. __be32 vni, sa_family_t family,
  113. __be16 port, u32 flags)
  114. {
  115. struct vxlan_sock *vs;
  116. vs = vxlan_find_sock(net, family, port, flags, ifindex);
  117. if (!vs)
  118. return NULL;
  119. return vxlan_vs_find_vni(vs, ifindex, vni, NULL);
  120. }
  121. /* Fill in neighbour message in skbuff. */
  122. static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
  123. const struct vxlan_fdb *fdb,
  124. u32 portid, u32 seq, int type, unsigned int flags,
  125. const struct vxlan_rdst *rdst)
  126. {
  127. unsigned long now = jiffies;
  128. struct nda_cacheinfo ci;
  129. bool send_ip, send_eth;
  130. struct nlmsghdr *nlh;
  131. struct nexthop *nh;
  132. struct ndmsg *ndm;
  133. int nh_family;
  134. u32 nh_id;
  135. nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
  136. if (nlh == NULL)
  137. return -EMSGSIZE;
  138. ndm = nlmsg_data(nlh);
  139. memset(ndm, 0, sizeof(*ndm));
  140. send_eth = send_ip = true;
  141. rcu_read_lock();
  142. nh = rcu_dereference(fdb->nh);
  143. if (nh) {
  144. nh_family = nexthop_get_family(nh);
  145. nh_id = nh->id;
  146. }
  147. rcu_read_unlock();
  148. if (type == RTM_GETNEIGH) {
  149. if (rdst) {
  150. send_ip = !vxlan_addr_any(&rdst->remote_ip);
  151. ndm->ndm_family = send_ip ? rdst->remote_ip.sa.sa_family : AF_INET;
  152. } else if (nh) {
  153. ndm->ndm_family = nh_family;
  154. }
  155. send_eth = !is_zero_ether_addr(fdb->eth_addr);
  156. } else
  157. ndm->ndm_family = AF_BRIDGE;
  158. ndm->ndm_state = fdb->state;
  159. ndm->ndm_ifindex = vxlan->dev->ifindex;
  160. ndm->ndm_flags = fdb->flags;
  161. if (rdst && rdst->offloaded)
  162. ndm->ndm_flags |= NTF_OFFLOADED;
  163. ndm->ndm_type = RTN_UNICAST;
  164. if (!net_eq(dev_net(vxlan->dev), vxlan->net) &&
  165. nla_put_s32(skb, NDA_LINK_NETNSID,
  166. peernet2id(dev_net(vxlan->dev), vxlan->net)))
  167. goto nla_put_failure;
  168. if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
  169. goto nla_put_failure;
  170. if (nh) {
  171. if (nla_put_u32(skb, NDA_NH_ID, nh_id))
  172. goto nla_put_failure;
  173. } else if (rdst) {
  174. if (send_ip && vxlan_nla_put_addr(skb, NDA_DST,
  175. &rdst->remote_ip))
  176. goto nla_put_failure;
  177. if (rdst->remote_port &&
  178. rdst->remote_port != vxlan->cfg.dst_port &&
  179. nla_put_be16(skb, NDA_PORT, rdst->remote_port))
  180. goto nla_put_failure;
  181. if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
  182. nla_put_u32(skb, NDA_VNI, be32_to_cpu(rdst->remote_vni)))
  183. goto nla_put_failure;
  184. if (rdst->remote_ifindex &&
  185. nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
  186. goto nla_put_failure;
  187. }
  188. if ((vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) && fdb->vni &&
  189. nla_put_u32(skb, NDA_SRC_VNI,
  190. be32_to_cpu(fdb->vni)))
  191. goto nla_put_failure;
  192. ci.ndm_used = jiffies_to_clock_t(now - READ_ONCE(fdb->used));
  193. ci.ndm_confirmed = 0;
  194. ci.ndm_updated = jiffies_to_clock_t(now - READ_ONCE(fdb->updated));
  195. ci.ndm_refcnt = 0;
  196. if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
  197. goto nla_put_failure;
  198. nlmsg_end(skb, nlh);
  199. return 0;
  200. nla_put_failure:
  201. nlmsg_cancel(skb, nlh);
  202. return -EMSGSIZE;
  203. }
  204. static inline size_t vxlan_nlmsg_size(void)
  205. {
  206. return NLMSG_ALIGN(sizeof(struct ndmsg))
  207. + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
  208. + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */
  209. + nla_total_size(sizeof(__be16)) /* NDA_PORT */
  210. + nla_total_size(sizeof(__be32)) /* NDA_VNI */
  211. + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
  212. + nla_total_size(sizeof(__s32)) /* NDA_LINK_NETNSID */
  213. + nla_total_size(sizeof(struct nda_cacheinfo));
  214. }
  215. static void __vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
  216. struct vxlan_rdst *rd, int type)
  217. {
  218. struct net *net = dev_net(vxlan->dev);
  219. struct sk_buff *skb;
  220. int err = -ENOBUFS;
  221. skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
  222. if (skb == NULL)
  223. goto errout;
  224. err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, rd);
  225. if (err < 0) {
  226. /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
  227. WARN_ON(err == -EMSGSIZE);
  228. kfree_skb(skb);
  229. goto errout;
  230. }
  231. rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
  232. return;
  233. errout:
  234. rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
  235. }
  236. static void vxlan_fdb_switchdev_notifier_info(const struct vxlan_dev *vxlan,
  237. const struct vxlan_fdb *fdb,
  238. const struct vxlan_rdst *rd,
  239. struct netlink_ext_ack *extack,
  240. struct switchdev_notifier_vxlan_fdb_info *fdb_info)
  241. {
  242. fdb_info->info.dev = vxlan->dev;
  243. fdb_info->info.extack = extack;
  244. fdb_info->remote_ip = rd->remote_ip;
  245. fdb_info->remote_port = rd->remote_port;
  246. fdb_info->remote_vni = rd->remote_vni;
  247. fdb_info->remote_ifindex = rd->remote_ifindex;
  248. memcpy(fdb_info->eth_addr, fdb->eth_addr, ETH_ALEN);
  249. fdb_info->vni = fdb->vni;
  250. fdb_info->offloaded = rd->offloaded;
  251. fdb_info->added_by_user = fdb->flags & NTF_VXLAN_ADDED_BY_USER;
  252. }
  253. static int vxlan_fdb_switchdev_call_notifiers(struct vxlan_dev *vxlan,
  254. struct vxlan_fdb *fdb,
  255. struct vxlan_rdst *rd,
  256. bool adding,
  257. struct netlink_ext_ack *extack)
  258. {
  259. struct switchdev_notifier_vxlan_fdb_info info;
  260. enum switchdev_notifier_type notifier_type;
  261. int ret;
  262. if (WARN_ON(!rd))
  263. return 0;
  264. notifier_type = adding ? SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE
  265. : SWITCHDEV_VXLAN_FDB_DEL_TO_DEVICE;
  266. vxlan_fdb_switchdev_notifier_info(vxlan, fdb, rd, NULL, &info);
  267. ret = call_switchdev_notifiers(notifier_type, vxlan->dev,
  268. &info.info, extack);
  269. return notifier_to_errno(ret);
  270. }
  271. static int vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
  272. struct vxlan_rdst *rd, int type, bool swdev_notify,
  273. struct netlink_ext_ack *extack)
  274. {
  275. int err;
  276. if (swdev_notify && rd) {
  277. switch (type) {
  278. case RTM_NEWNEIGH:
  279. err = vxlan_fdb_switchdev_call_notifiers(vxlan, fdb, rd,
  280. true, extack);
  281. if (err)
  282. return err;
  283. break;
  284. case RTM_DELNEIGH:
  285. vxlan_fdb_switchdev_call_notifiers(vxlan, fdb, rd,
  286. false, extack);
  287. break;
  288. }
  289. }
  290. __vxlan_fdb_notify(vxlan, fdb, rd, type);
  291. return 0;
  292. }
  293. static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa)
  294. {
  295. struct vxlan_dev *vxlan = netdev_priv(dev);
  296. struct vxlan_fdb f = {
  297. .state = NUD_STALE,
  298. };
  299. struct vxlan_rdst remote = {
  300. .remote_ip = *ipa, /* goes to NDA_DST */
  301. .remote_vni = cpu_to_be32(VXLAN_N_VID),
  302. };
  303. vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH, true, NULL);
  304. }
  305. static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
  306. {
  307. struct vxlan_fdb f = {
  308. .state = NUD_STALE,
  309. };
  310. struct vxlan_rdst remote = { };
  311. memcpy(f.eth_addr, eth_addr, ETH_ALEN);
  312. vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH, true, NULL);
  313. }
  314. /* Hash Ethernet address */
  315. static u32 eth_hash(const unsigned char *addr)
  316. {
  317. u64 value = get_unaligned((u64 *)addr);
  318. /* only want 6 bytes */
  319. #ifdef __BIG_ENDIAN
  320. value >>= 16;
  321. #else
  322. value <<= 16;
  323. #endif
  324. return hash_64(value, FDB_HASH_BITS);
  325. }
  326. u32 eth_vni_hash(const unsigned char *addr, __be32 vni)
  327. {
  328. /* use 1 byte of OUI and 3 bytes of NIC */
  329. u32 key = get_unaligned((u32 *)(addr + 2));
  330. return jhash_2words(key, vni, vxlan_salt) & (FDB_HASH_SIZE - 1);
  331. }
  332. u32 fdb_head_index(struct vxlan_dev *vxlan, const u8 *mac, __be32 vni)
  333. {
  334. if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA)
  335. return eth_vni_hash(mac, vni);
  336. else
  337. return eth_hash(mac);
  338. }
  339. /* Hash chain to use given mac address */
  340. static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
  341. const u8 *mac, __be32 vni)
  342. {
  343. return &vxlan->fdb_head[fdb_head_index(vxlan, mac, vni)];
  344. }
  345. /* Look up Ethernet address in forwarding table */
  346. static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
  347. const u8 *mac, __be32 vni)
  348. {
  349. struct hlist_head *head = vxlan_fdb_head(vxlan, mac, vni);
  350. struct vxlan_fdb *f;
  351. hlist_for_each_entry_rcu(f, head, hlist) {
  352. if (ether_addr_equal(mac, f->eth_addr)) {
  353. if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) {
  354. if (vni == f->vni)
  355. return f;
  356. } else {
  357. return f;
  358. }
  359. }
  360. }
  361. return NULL;
  362. }
  363. static struct vxlan_fdb *vxlan_find_mac_tx(struct vxlan_dev *vxlan,
  364. const u8 *mac, __be32 vni)
  365. {
  366. struct vxlan_fdb *f;
  367. f = __vxlan_find_mac(vxlan, mac, vni);
  368. if (f && READ_ONCE(f->used) != jiffies)
  369. WRITE_ONCE(f->used, jiffies);
  370. return f;
  371. }
  372. /* caller should hold vxlan->hash_lock */
  373. static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
  374. union vxlan_addr *ip, __be16 port,
  375. __be32 vni, __u32 ifindex)
  376. {
  377. struct vxlan_rdst *rd;
  378. list_for_each_entry(rd, &f->remotes, list) {
  379. if (vxlan_addr_equal(&rd->remote_ip, ip) &&
  380. rd->remote_port == port &&
  381. rd->remote_vni == vni &&
  382. rd->remote_ifindex == ifindex)
  383. return rd;
  384. }
  385. return NULL;
  386. }
  387. int vxlan_fdb_find_uc(struct net_device *dev, const u8 *mac, __be32 vni,
  388. struct switchdev_notifier_vxlan_fdb_info *fdb_info)
  389. {
  390. struct vxlan_dev *vxlan = netdev_priv(dev);
  391. u8 eth_addr[ETH_ALEN + 2] = { 0 };
  392. struct vxlan_rdst *rdst;
  393. struct vxlan_fdb *f;
  394. int rc = 0;
  395. if (is_multicast_ether_addr(mac) ||
  396. is_zero_ether_addr(mac))
  397. return -EINVAL;
  398. ether_addr_copy(eth_addr, mac);
  399. rcu_read_lock();
  400. f = __vxlan_find_mac(vxlan, eth_addr, vni);
  401. if (!f) {
  402. rc = -ENOENT;
  403. goto out;
  404. }
  405. rdst = first_remote_rcu(f);
  406. vxlan_fdb_switchdev_notifier_info(vxlan, f, rdst, NULL, fdb_info);
  407. out:
  408. rcu_read_unlock();
  409. return rc;
  410. }
  411. EXPORT_SYMBOL_GPL(vxlan_fdb_find_uc);
  412. static int vxlan_fdb_notify_one(struct notifier_block *nb,
  413. const struct vxlan_dev *vxlan,
  414. const struct vxlan_fdb *f,
  415. const struct vxlan_rdst *rdst,
  416. struct netlink_ext_ack *extack)
  417. {
  418. struct switchdev_notifier_vxlan_fdb_info fdb_info;
  419. int rc;
  420. vxlan_fdb_switchdev_notifier_info(vxlan, f, rdst, extack, &fdb_info);
  421. rc = nb->notifier_call(nb, SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE,
  422. &fdb_info);
  423. return notifier_to_errno(rc);
  424. }
  425. int vxlan_fdb_replay(const struct net_device *dev, __be32 vni,
  426. struct notifier_block *nb,
  427. struct netlink_ext_ack *extack)
  428. {
  429. struct vxlan_dev *vxlan;
  430. struct vxlan_rdst *rdst;
  431. struct vxlan_fdb *f;
  432. unsigned int h;
  433. int rc = 0;
  434. if (!netif_is_vxlan(dev))
  435. return -EINVAL;
  436. vxlan = netdev_priv(dev);
  437. for (h = 0; h < FDB_HASH_SIZE; ++h) {
  438. spin_lock_bh(&vxlan->hash_lock[h]);
  439. hlist_for_each_entry(f, &vxlan->fdb_head[h], hlist) {
  440. if (f->vni == vni) {
  441. list_for_each_entry(rdst, &f->remotes, list) {
  442. rc = vxlan_fdb_notify_one(nb, vxlan,
  443. f, rdst,
  444. extack);
  445. if (rc)
  446. goto unlock;
  447. }
  448. }
  449. }
  450. spin_unlock_bh(&vxlan->hash_lock[h]);
  451. }
  452. return 0;
  453. unlock:
  454. spin_unlock_bh(&vxlan->hash_lock[h]);
  455. return rc;
  456. }
  457. EXPORT_SYMBOL_GPL(vxlan_fdb_replay);
  458. void vxlan_fdb_clear_offload(const struct net_device *dev, __be32 vni)
  459. {
  460. struct vxlan_dev *vxlan;
  461. struct vxlan_rdst *rdst;
  462. struct vxlan_fdb *f;
  463. unsigned int h;
  464. if (!netif_is_vxlan(dev))
  465. return;
  466. vxlan = netdev_priv(dev);
  467. for (h = 0; h < FDB_HASH_SIZE; ++h) {
  468. spin_lock_bh(&vxlan->hash_lock[h]);
  469. hlist_for_each_entry(f, &vxlan->fdb_head[h], hlist)
  470. if (f->vni == vni)
  471. list_for_each_entry(rdst, &f->remotes, list)
  472. rdst->offloaded = false;
  473. spin_unlock_bh(&vxlan->hash_lock[h]);
  474. }
  475. }
  476. EXPORT_SYMBOL_GPL(vxlan_fdb_clear_offload);
  477. /* Replace destination of unicast mac */
  478. static int vxlan_fdb_replace(struct vxlan_fdb *f,
  479. union vxlan_addr *ip, __be16 port, __be32 vni,
  480. __u32 ifindex, struct vxlan_rdst *oldrd)
  481. {
  482. struct vxlan_rdst *rd;
  483. rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
  484. if (rd)
  485. return 0;
  486. rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
  487. if (!rd)
  488. return 0;
  489. *oldrd = *rd;
  490. dst_cache_reset(&rd->dst_cache);
  491. rd->remote_ip = *ip;
  492. rd->remote_port = port;
  493. rd->remote_vni = vni;
  494. rd->remote_ifindex = ifindex;
  495. rd->offloaded = false;
  496. return 1;
  497. }
  498. /* Add/update destinations for multicast */
  499. static int vxlan_fdb_append(struct vxlan_fdb *f,
  500. union vxlan_addr *ip, __be16 port, __be32 vni,
  501. __u32 ifindex, struct vxlan_rdst **rdp)
  502. {
  503. struct vxlan_rdst *rd;
  504. rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
  505. if (rd)
  506. return 0;
  507. rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
  508. if (rd == NULL)
  509. return -ENOMEM;
  510. /* The driver can work correctly without a dst cache, so do not treat
  511. * dst cache initialization errors as fatal.
  512. */
  513. dst_cache_init(&rd->dst_cache, GFP_ATOMIC | __GFP_NOWARN);
  514. rd->remote_ip = *ip;
  515. rd->remote_port = port;
  516. rd->offloaded = false;
  517. rd->remote_vni = vni;
  518. rd->remote_ifindex = ifindex;
  519. list_add_tail_rcu(&rd->list, &f->remotes);
  520. *rdp = rd;
  521. return 1;
  522. }
  523. static bool vxlan_parse_gpe_proto(struct vxlanhdr *hdr, __be16 *protocol)
  524. {
  525. struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)hdr;
  526. /* Need to have Next Protocol set for interfaces in GPE mode. */
  527. if (!gpe->np_applied)
  528. return false;
  529. /* "The initial version is 0. If a receiver does not support the
  530. * version indicated it MUST drop the packet.
  531. */
  532. if (gpe->version != 0)
  533. return false;
  534. /* "When the O bit is set to 1, the packet is an OAM packet and OAM
  535. * processing MUST occur." However, we don't implement OAM
  536. * processing, thus drop the packet.
  537. */
  538. if (gpe->oam_flag)
  539. return false;
  540. *protocol = tun_p_to_eth_p(gpe->next_protocol);
  541. if (!*protocol)
  542. return false;
  543. return true;
  544. }
  545. static struct vxlanhdr *vxlan_gro_remcsum(struct sk_buff *skb,
  546. unsigned int off,
  547. struct vxlanhdr *vh, size_t hdrlen,
  548. __be32 vni_field,
  549. struct gro_remcsum *grc,
  550. bool nopartial)
  551. {
  552. size_t start, offset;
  553. if (skb->remcsum_offload)
  554. return vh;
  555. if (!NAPI_GRO_CB(skb)->csum_valid)
  556. return NULL;
  557. start = vxlan_rco_start(vni_field);
  558. offset = start + vxlan_rco_offset(vni_field);
  559. vh = skb_gro_remcsum_process(skb, (void *)vh, off, hdrlen,
  560. start, offset, grc, nopartial);
  561. skb->remcsum_offload = 1;
  562. return vh;
  563. }
  564. static struct vxlanhdr *vxlan_gro_prepare_receive(struct sock *sk,
  565. struct list_head *head,
  566. struct sk_buff *skb,
  567. struct gro_remcsum *grc)
  568. {
  569. struct sk_buff *p;
  570. struct vxlanhdr *vh, *vh2;
  571. unsigned int hlen, off_vx;
  572. struct vxlan_sock *vs = rcu_dereference_sk_user_data(sk);
  573. __be32 flags;
  574. skb_gro_remcsum_init(grc);
  575. off_vx = skb_gro_offset(skb);
  576. hlen = off_vx + sizeof(*vh);
  577. vh = skb_gro_header(skb, hlen, off_vx);
  578. if (unlikely(!vh))
  579. return NULL;
  580. skb_gro_postpull_rcsum(skb, vh, sizeof(struct vxlanhdr));
  581. flags = vh->vx_flags;
  582. if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) {
  583. vh = vxlan_gro_remcsum(skb, off_vx, vh, sizeof(struct vxlanhdr),
  584. vh->vx_vni, grc,
  585. !!(vs->flags &
  586. VXLAN_F_REMCSUM_NOPARTIAL));
  587. if (!vh)
  588. return NULL;
  589. }
  590. skb_gro_pull(skb, sizeof(struct vxlanhdr)); /* pull vxlan header */
  591. list_for_each_entry(p, head, list) {
  592. if (!NAPI_GRO_CB(p)->same_flow)
  593. continue;
  594. vh2 = (struct vxlanhdr *)(p->data + off_vx);
  595. if (vh->vx_flags != vh2->vx_flags ||
  596. vh->vx_vni != vh2->vx_vni) {
  597. NAPI_GRO_CB(p)->same_flow = 0;
  598. continue;
  599. }
  600. }
  601. return vh;
  602. }
  603. static struct sk_buff *vxlan_gro_receive(struct sock *sk,
  604. struct list_head *head,
  605. struct sk_buff *skb)
  606. {
  607. struct sk_buff *pp = NULL;
  608. struct gro_remcsum grc;
  609. int flush = 1;
  610. if (vxlan_gro_prepare_receive(sk, head, skb, &grc)) {
  611. pp = call_gro_receive(eth_gro_receive, head, skb);
  612. flush = 0;
  613. }
  614. skb_gro_flush_final_remcsum(skb, pp, flush, &grc);
  615. return pp;
  616. }
  617. static struct sk_buff *vxlan_gpe_gro_receive(struct sock *sk,
  618. struct list_head *head,
  619. struct sk_buff *skb)
  620. {
  621. const struct packet_offload *ptype;
  622. struct sk_buff *pp = NULL;
  623. struct gro_remcsum grc;
  624. struct vxlanhdr *vh;
  625. __be16 protocol;
  626. int flush = 1;
  627. vh = vxlan_gro_prepare_receive(sk, head, skb, &grc);
  628. if (vh) {
  629. if (!vxlan_parse_gpe_proto(vh, &protocol))
  630. goto out;
  631. ptype = gro_find_receive_by_type(protocol);
  632. if (!ptype)
  633. goto out;
  634. pp = call_gro_receive(ptype->callbacks.gro_receive, head, skb);
  635. flush = 0;
  636. }
  637. out:
  638. skb_gro_flush_final_remcsum(skb, pp, flush, &grc);
  639. return pp;
  640. }
  641. static int vxlan_gro_complete(struct sock *sk, struct sk_buff *skb, int nhoff)
  642. {
  643. /* Sets 'skb->inner_mac_header' since we are always called with
  644. * 'skb->encapsulation' set.
  645. */
  646. return eth_gro_complete(skb, nhoff + sizeof(struct vxlanhdr));
  647. }
  648. static int vxlan_gpe_gro_complete(struct sock *sk, struct sk_buff *skb, int nhoff)
  649. {
  650. struct vxlanhdr *vh = (struct vxlanhdr *)(skb->data + nhoff);
  651. const struct packet_offload *ptype;
  652. int err = -ENOSYS;
  653. __be16 protocol;
  654. if (!vxlan_parse_gpe_proto(vh, &protocol))
  655. return err;
  656. ptype = gro_find_complete_by_type(protocol);
  657. if (ptype)
  658. err = ptype->callbacks.gro_complete(skb, nhoff + sizeof(struct vxlanhdr));
  659. return err;
  660. }
  661. static struct vxlan_fdb *vxlan_fdb_alloc(struct vxlan_dev *vxlan, const u8 *mac,
  662. __u16 state, __be32 src_vni,
  663. __u16 ndm_flags)
  664. {
  665. struct vxlan_fdb *f;
  666. f = kmalloc(sizeof(*f), GFP_ATOMIC);
  667. if (!f)
  668. return NULL;
  669. f->state = state;
  670. f->flags = ndm_flags;
  671. f->updated = f->used = jiffies;
  672. f->vni = src_vni;
  673. f->nh = NULL;
  674. RCU_INIT_POINTER(f->vdev, vxlan);
  675. INIT_LIST_HEAD(&f->nh_list);
  676. INIT_LIST_HEAD(&f->remotes);
  677. memcpy(f->eth_addr, mac, ETH_ALEN);
  678. return f;
  679. }
  680. static void vxlan_fdb_insert(struct vxlan_dev *vxlan, const u8 *mac,
  681. __be32 src_vni, struct vxlan_fdb *f)
  682. {
  683. ++vxlan->addrcnt;
  684. hlist_add_head_rcu(&f->hlist,
  685. vxlan_fdb_head(vxlan, mac, src_vni));
  686. }
  687. static int vxlan_fdb_nh_update(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
  688. u32 nhid, struct netlink_ext_ack *extack)
  689. {
  690. struct nexthop *old_nh = rtnl_dereference(fdb->nh);
  691. struct nexthop *nh;
  692. int err = -EINVAL;
  693. if (old_nh && old_nh->id == nhid)
  694. return 0;
  695. nh = nexthop_find_by_id(vxlan->net, nhid);
  696. if (!nh) {
  697. NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
  698. goto err_inval;
  699. }
  700. if (!nexthop_get(nh)) {
  701. NL_SET_ERR_MSG(extack, "Nexthop has been deleted");
  702. nh = NULL;
  703. goto err_inval;
  704. }
  705. if (!nexthop_is_fdb(nh)) {
  706. NL_SET_ERR_MSG(extack, "Nexthop is not a fdb nexthop");
  707. goto err_inval;
  708. }
  709. if (!nexthop_is_multipath(nh)) {
  710. NL_SET_ERR_MSG(extack, "Nexthop is not a multipath group");
  711. goto err_inval;
  712. }
  713. /* check nexthop group family */
  714. switch (vxlan->default_dst.remote_ip.sa.sa_family) {
  715. case AF_INET:
  716. if (!nexthop_has_v4(nh)) {
  717. err = -EAFNOSUPPORT;
  718. NL_SET_ERR_MSG(extack, "Nexthop group family not supported");
  719. goto err_inval;
  720. }
  721. break;
  722. case AF_INET6:
  723. if (nexthop_has_v4(nh)) {
  724. err = -EAFNOSUPPORT;
  725. NL_SET_ERR_MSG(extack, "Nexthop group family not supported");
  726. goto err_inval;
  727. }
  728. }
  729. if (old_nh) {
  730. list_del_rcu(&fdb->nh_list);
  731. nexthop_put(old_nh);
  732. }
  733. rcu_assign_pointer(fdb->nh, nh);
  734. list_add_tail_rcu(&fdb->nh_list, &nh->fdb_list);
  735. return 1;
  736. err_inval:
  737. if (nh)
  738. nexthop_put(nh);
  739. return err;
  740. }
  741. int vxlan_fdb_create(struct vxlan_dev *vxlan,
  742. const u8 *mac, union vxlan_addr *ip,
  743. __u16 state, __be16 port, __be32 src_vni,
  744. __be32 vni, __u32 ifindex, __u16 ndm_flags,
  745. u32 nhid, struct vxlan_fdb **fdb,
  746. struct netlink_ext_ack *extack)
  747. {
  748. struct vxlan_rdst *rd = NULL;
  749. struct vxlan_fdb *f;
  750. int rc;
  751. if (vxlan->cfg.addrmax &&
  752. vxlan->addrcnt >= vxlan->cfg.addrmax)
  753. return -ENOSPC;
  754. netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
  755. f = vxlan_fdb_alloc(vxlan, mac, state, src_vni, ndm_flags);
  756. if (!f)
  757. return -ENOMEM;
  758. if (nhid)
  759. rc = vxlan_fdb_nh_update(vxlan, f, nhid, extack);
  760. else
  761. rc = vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
  762. if (rc < 0)
  763. goto errout;
  764. *fdb = f;
  765. return 0;
  766. errout:
  767. kfree(f);
  768. return rc;
  769. }
  770. static void __vxlan_fdb_free(struct vxlan_fdb *f)
  771. {
  772. struct vxlan_rdst *rd, *nd;
  773. struct nexthop *nh;
  774. nh = rcu_dereference_raw(f->nh);
  775. if (nh) {
  776. rcu_assign_pointer(f->nh, NULL);
  777. rcu_assign_pointer(f->vdev, NULL);
  778. nexthop_put(nh);
  779. }
  780. list_for_each_entry_safe(rd, nd, &f->remotes, list) {
  781. dst_cache_destroy(&rd->dst_cache);
  782. kfree(rd);
  783. }
  784. kfree(f);
  785. }
  786. static void vxlan_fdb_free(struct rcu_head *head)
  787. {
  788. struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
  789. __vxlan_fdb_free(f);
  790. }
  791. static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f,
  792. bool do_notify, bool swdev_notify)
  793. {
  794. struct vxlan_rdst *rd;
  795. netdev_dbg(vxlan->dev, "delete %pM\n", f->eth_addr);
  796. --vxlan->addrcnt;
  797. if (do_notify) {
  798. if (rcu_access_pointer(f->nh))
  799. vxlan_fdb_notify(vxlan, f, NULL, RTM_DELNEIGH,
  800. swdev_notify, NULL);
  801. else
  802. list_for_each_entry(rd, &f->remotes, list)
  803. vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH,
  804. swdev_notify, NULL);
  805. }
  806. hlist_del_rcu(&f->hlist);
  807. list_del_rcu(&f->nh_list);
  808. call_rcu(&f->rcu, vxlan_fdb_free);
  809. }
  810. static void vxlan_dst_free(struct rcu_head *head)
  811. {
  812. struct vxlan_rdst *rd = container_of(head, struct vxlan_rdst, rcu);
  813. dst_cache_destroy(&rd->dst_cache);
  814. kfree(rd);
  815. }
  816. static int vxlan_fdb_update_existing(struct vxlan_dev *vxlan,
  817. union vxlan_addr *ip,
  818. __u16 state, __u16 flags,
  819. __be16 port, __be32 vni,
  820. __u32 ifindex, __u16 ndm_flags,
  821. struct vxlan_fdb *f, u32 nhid,
  822. bool swdev_notify,
  823. struct netlink_ext_ack *extack)
  824. {
  825. __u16 fdb_flags = (ndm_flags & ~NTF_USE);
  826. struct vxlan_rdst *rd = NULL;
  827. struct vxlan_rdst oldrd;
  828. int notify = 0;
  829. int rc = 0;
  830. int err;
  831. if (nhid && !rcu_access_pointer(f->nh)) {
  832. NL_SET_ERR_MSG(extack,
  833. "Cannot replace an existing non nexthop fdb with a nexthop");
  834. return -EOPNOTSUPP;
  835. }
  836. if (nhid && (flags & NLM_F_APPEND)) {
  837. NL_SET_ERR_MSG(extack,
  838. "Cannot append to a nexthop fdb");
  839. return -EOPNOTSUPP;
  840. }
  841. /* Do not allow an externally learned entry to take over an entry added
  842. * by the user.
  843. */
  844. if (!(fdb_flags & NTF_EXT_LEARNED) ||
  845. !(f->flags & NTF_VXLAN_ADDED_BY_USER)) {
  846. if (f->state != state) {
  847. f->state = state;
  848. WRITE_ONCE(f->updated, jiffies);
  849. notify = 1;
  850. }
  851. if (f->flags != fdb_flags) {
  852. f->flags = fdb_flags;
  853. WRITE_ONCE(f->updated, jiffies);
  854. notify = 1;
  855. }
  856. }
  857. if ((flags & NLM_F_REPLACE)) {
  858. /* Only change unicasts */
  859. if (!(is_multicast_ether_addr(f->eth_addr) ||
  860. is_zero_ether_addr(f->eth_addr))) {
  861. if (nhid) {
  862. rc = vxlan_fdb_nh_update(vxlan, f, nhid, extack);
  863. if (rc < 0)
  864. return rc;
  865. } else {
  866. rc = vxlan_fdb_replace(f, ip, port, vni,
  867. ifindex, &oldrd);
  868. }
  869. notify |= rc;
  870. } else {
  871. NL_SET_ERR_MSG(extack, "Cannot replace non-unicast fdb entries");
  872. return -EOPNOTSUPP;
  873. }
  874. }
  875. if ((flags & NLM_F_APPEND) &&
  876. (is_multicast_ether_addr(f->eth_addr) ||
  877. is_zero_ether_addr(f->eth_addr))) {
  878. rc = vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
  879. if (rc < 0)
  880. return rc;
  881. notify |= rc;
  882. }
  883. if (ndm_flags & NTF_USE)
  884. WRITE_ONCE(f->updated, jiffies);
  885. if (notify) {
  886. if (rd == NULL)
  887. rd = first_remote_rtnl(f);
  888. err = vxlan_fdb_notify(vxlan, f, rd, RTM_NEWNEIGH,
  889. swdev_notify, extack);
  890. if (err)
  891. goto err_notify;
  892. }
  893. return 0;
  894. err_notify:
  895. if (nhid)
  896. return err;
  897. if ((flags & NLM_F_REPLACE) && rc)
  898. *rd = oldrd;
  899. else if ((flags & NLM_F_APPEND) && rc) {
  900. list_del_rcu(&rd->list);
  901. call_rcu(&rd->rcu, vxlan_dst_free);
  902. }
  903. return err;
  904. }
  905. static int vxlan_fdb_update_create(struct vxlan_dev *vxlan,
  906. const u8 *mac, union vxlan_addr *ip,
  907. __u16 state, __u16 flags,
  908. __be16 port, __be32 src_vni, __be32 vni,
  909. __u32 ifindex, __u16 ndm_flags, u32 nhid,
  910. bool swdev_notify,
  911. struct netlink_ext_ack *extack)
  912. {
  913. __u16 fdb_flags = (ndm_flags & ~NTF_USE);
  914. struct vxlan_fdb *f;
  915. int rc;
  916. /* Disallow replace to add a multicast entry */
  917. if ((flags & NLM_F_REPLACE) &&
  918. (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
  919. return -EOPNOTSUPP;
  920. netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
  921. rc = vxlan_fdb_create(vxlan, mac, ip, state, port, src_vni,
  922. vni, ifindex, fdb_flags, nhid, &f, extack);
  923. if (rc < 0)
  924. return rc;
  925. vxlan_fdb_insert(vxlan, mac, src_vni, f);
  926. rc = vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_NEWNEIGH,
  927. swdev_notify, extack);
  928. if (rc)
  929. goto err_notify;
  930. return 0;
  931. err_notify:
  932. vxlan_fdb_destroy(vxlan, f, false, false);
  933. return rc;
  934. }
  935. /* Add new entry to forwarding table -- assumes lock held */
  936. int vxlan_fdb_update(struct vxlan_dev *vxlan,
  937. const u8 *mac, union vxlan_addr *ip,
  938. __u16 state, __u16 flags,
  939. __be16 port, __be32 src_vni, __be32 vni,
  940. __u32 ifindex, __u16 ndm_flags, u32 nhid,
  941. bool swdev_notify,
  942. struct netlink_ext_ack *extack)
  943. {
  944. struct vxlan_fdb *f;
  945. f = __vxlan_find_mac(vxlan, mac, src_vni);
  946. if (f) {
  947. if (flags & NLM_F_EXCL) {
  948. netdev_dbg(vxlan->dev,
  949. "lost race to create %pM\n", mac);
  950. return -EEXIST;
  951. }
  952. return vxlan_fdb_update_existing(vxlan, ip, state, flags, port,
  953. vni, ifindex, ndm_flags, f,
  954. nhid, swdev_notify, extack);
  955. } else {
  956. if (!(flags & NLM_F_CREATE))
  957. return -ENOENT;
  958. return vxlan_fdb_update_create(vxlan, mac, ip, state, flags,
  959. port, src_vni, vni, ifindex,
  960. ndm_flags, nhid, swdev_notify,
  961. extack);
  962. }
  963. }
  964. static void vxlan_fdb_dst_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f,
  965. struct vxlan_rdst *rd, bool swdev_notify)
  966. {
  967. list_del_rcu(&rd->list);
  968. vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH, swdev_notify, NULL);
  969. call_rcu(&rd->rcu, vxlan_dst_free);
  970. }
  971. static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
  972. union vxlan_addr *ip, __be16 *port, __be32 *src_vni,
  973. __be32 *vni, u32 *ifindex, u32 *nhid,
  974. struct netlink_ext_ack *extack)
  975. {
  976. struct net *net = dev_net(vxlan->dev);
  977. int err;
  978. if (tb[NDA_NH_ID] &&
  979. (tb[NDA_DST] || tb[NDA_VNI] || tb[NDA_IFINDEX] || tb[NDA_PORT])) {
  980. NL_SET_ERR_MSG(extack, "DST, VNI, ifindex and port are mutually exclusive with NH_ID");
  981. return -EINVAL;
  982. }
  983. if (tb[NDA_DST]) {
  984. err = vxlan_nla_get_addr(ip, tb[NDA_DST]);
  985. if (err) {
  986. NL_SET_ERR_MSG(extack, "Unsupported address family");
  987. return err;
  988. }
  989. } else {
  990. union vxlan_addr *remote = &vxlan->default_dst.remote_ip;
  991. if (remote->sa.sa_family == AF_INET) {
  992. ip->sin.sin_addr.s_addr = htonl(INADDR_ANY);
  993. ip->sa.sa_family = AF_INET;
  994. #if IS_ENABLED(CONFIG_IPV6)
  995. } else {
  996. ip->sin6.sin6_addr = in6addr_any;
  997. ip->sa.sa_family = AF_INET6;
  998. #endif
  999. }
  1000. }
  1001. if (tb[NDA_PORT]) {
  1002. if (nla_len(tb[NDA_PORT]) != sizeof(__be16)) {
  1003. NL_SET_ERR_MSG(extack, "Invalid vxlan port");
  1004. return -EINVAL;
  1005. }
  1006. *port = nla_get_be16(tb[NDA_PORT]);
  1007. } else {
  1008. *port = vxlan->cfg.dst_port;
  1009. }
  1010. if (tb[NDA_VNI]) {
  1011. if (nla_len(tb[NDA_VNI]) != sizeof(u32)) {
  1012. NL_SET_ERR_MSG(extack, "Invalid vni");
  1013. return -EINVAL;
  1014. }
  1015. *vni = cpu_to_be32(nla_get_u32(tb[NDA_VNI]));
  1016. } else {
  1017. *vni = vxlan->default_dst.remote_vni;
  1018. }
  1019. if (tb[NDA_SRC_VNI]) {
  1020. if (nla_len(tb[NDA_SRC_VNI]) != sizeof(u32)) {
  1021. NL_SET_ERR_MSG(extack, "Invalid src vni");
  1022. return -EINVAL;
  1023. }
  1024. *src_vni = cpu_to_be32(nla_get_u32(tb[NDA_SRC_VNI]));
  1025. } else {
  1026. *src_vni = vxlan->default_dst.remote_vni;
  1027. }
  1028. if (tb[NDA_IFINDEX]) {
  1029. struct net_device *tdev;
  1030. if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32)) {
  1031. NL_SET_ERR_MSG(extack, "Invalid ifindex");
  1032. return -EINVAL;
  1033. }
  1034. *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
  1035. tdev = __dev_get_by_index(net, *ifindex);
  1036. if (!tdev) {
  1037. NL_SET_ERR_MSG(extack, "Device not found");
  1038. return -EADDRNOTAVAIL;
  1039. }
  1040. } else {
  1041. *ifindex = 0;
  1042. }
  1043. if (tb[NDA_NH_ID])
  1044. *nhid = nla_get_u32(tb[NDA_NH_ID]);
  1045. else
  1046. *nhid = 0;
  1047. return 0;
  1048. }
  1049. /* Add static entry (via netlink) */
  1050. static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
  1051. struct net_device *dev,
  1052. const unsigned char *addr, u16 vid, u16 flags,
  1053. struct netlink_ext_ack *extack)
  1054. {
  1055. struct vxlan_dev *vxlan = netdev_priv(dev);
  1056. /* struct net *net = dev_net(vxlan->dev); */
  1057. union vxlan_addr ip;
  1058. __be16 port;
  1059. __be32 src_vni, vni;
  1060. u32 ifindex, nhid;
  1061. u32 hash_index;
  1062. int err;
  1063. if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
  1064. pr_info("RTM_NEWNEIGH with invalid state %#x\n",
  1065. ndm->ndm_state);
  1066. return -EINVAL;
  1067. }
  1068. if (!tb || (!tb[NDA_DST] && !tb[NDA_NH_ID]))
  1069. return -EINVAL;
  1070. err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &src_vni, &vni, &ifindex,
  1071. &nhid, extack);
  1072. if (err)
  1073. return err;
  1074. if (vxlan->default_dst.remote_ip.sa.sa_family != ip.sa.sa_family)
  1075. return -EAFNOSUPPORT;
  1076. hash_index = fdb_head_index(vxlan, addr, src_vni);
  1077. spin_lock_bh(&vxlan->hash_lock[hash_index]);
  1078. err = vxlan_fdb_update(vxlan, addr, &ip, ndm->ndm_state, flags,
  1079. port, src_vni, vni, ifindex,
  1080. ndm->ndm_flags | NTF_VXLAN_ADDED_BY_USER,
  1081. nhid, true, extack);
  1082. spin_unlock_bh(&vxlan->hash_lock[hash_index]);
  1083. return err;
  1084. }
  1085. int __vxlan_fdb_delete(struct vxlan_dev *vxlan,
  1086. const unsigned char *addr, union vxlan_addr ip,
  1087. __be16 port, __be32 src_vni, __be32 vni,
  1088. u32 ifindex, bool swdev_notify)
  1089. {
  1090. struct vxlan_rdst *rd = NULL;
  1091. struct vxlan_fdb *f;
  1092. int err = -ENOENT;
  1093. f = __vxlan_find_mac(vxlan, addr, src_vni);
  1094. if (!f)
  1095. return err;
  1096. if (!vxlan_addr_any(&ip)) {
  1097. rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex);
  1098. if (!rd)
  1099. goto out;
  1100. }
  1101. /* remove a destination if it's not the only one on the list,
  1102. * otherwise destroy the fdb entry
  1103. */
  1104. if (rd && !list_is_singular(&f->remotes)) {
  1105. vxlan_fdb_dst_destroy(vxlan, f, rd, swdev_notify);
  1106. goto out;
  1107. }
  1108. vxlan_fdb_destroy(vxlan, f, true, swdev_notify);
  1109. out:
  1110. return 0;
  1111. }
  1112. /* Delete entry (via netlink) */
  1113. static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
  1114. struct net_device *dev,
  1115. const unsigned char *addr, u16 vid,
  1116. struct netlink_ext_ack *extack)
  1117. {
  1118. struct vxlan_dev *vxlan = netdev_priv(dev);
  1119. union vxlan_addr ip;
  1120. __be32 src_vni, vni;
  1121. u32 ifindex, nhid;
  1122. u32 hash_index;
  1123. __be16 port;
  1124. int err;
  1125. err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &src_vni, &vni, &ifindex,
  1126. &nhid, extack);
  1127. if (err)
  1128. return err;
  1129. hash_index = fdb_head_index(vxlan, addr, src_vni);
  1130. spin_lock_bh(&vxlan->hash_lock[hash_index]);
  1131. err = __vxlan_fdb_delete(vxlan, addr, ip, port, src_vni, vni, ifindex,
  1132. true);
  1133. spin_unlock_bh(&vxlan->hash_lock[hash_index]);
  1134. return err;
  1135. }
  1136. /* Dump forwarding table */
  1137. static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
  1138. struct net_device *dev,
  1139. struct net_device *filter_dev, int *idx)
  1140. {
  1141. struct vxlan_dev *vxlan = netdev_priv(dev);
  1142. unsigned int h;
  1143. int err = 0;
  1144. for (h = 0; h < FDB_HASH_SIZE; ++h) {
  1145. struct vxlan_fdb *f;
  1146. rcu_read_lock();
  1147. hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
  1148. struct vxlan_rdst *rd;
  1149. if (rcu_access_pointer(f->nh)) {
  1150. if (*idx < cb->args[2])
  1151. goto skip_nh;
  1152. err = vxlan_fdb_info(skb, vxlan, f,
  1153. NETLINK_CB(cb->skb).portid,
  1154. cb->nlh->nlmsg_seq,
  1155. RTM_NEWNEIGH,
  1156. NLM_F_MULTI, NULL);
  1157. if (err < 0) {
  1158. rcu_read_unlock();
  1159. goto out;
  1160. }
  1161. skip_nh:
  1162. *idx += 1;
  1163. continue;
  1164. }
  1165. list_for_each_entry_rcu(rd, &f->remotes, list) {
  1166. if (*idx < cb->args[2])
  1167. goto skip;
  1168. err = vxlan_fdb_info(skb, vxlan, f,
  1169. NETLINK_CB(cb->skb).portid,
  1170. cb->nlh->nlmsg_seq,
  1171. RTM_NEWNEIGH,
  1172. NLM_F_MULTI, rd);
  1173. if (err < 0) {
  1174. rcu_read_unlock();
  1175. goto out;
  1176. }
  1177. skip:
  1178. *idx += 1;
  1179. }
  1180. }
  1181. rcu_read_unlock();
  1182. }
  1183. out:
  1184. return err;
  1185. }
  1186. static int vxlan_fdb_get(struct sk_buff *skb,
  1187. struct nlattr *tb[],
  1188. struct net_device *dev,
  1189. const unsigned char *addr,
  1190. u16 vid, u32 portid, u32 seq,
  1191. struct netlink_ext_ack *extack)
  1192. {
  1193. struct vxlan_dev *vxlan = netdev_priv(dev);
  1194. struct vxlan_fdb *f;
  1195. __be32 vni;
  1196. int err;
  1197. if (tb[NDA_VNI])
  1198. vni = cpu_to_be32(nla_get_u32(tb[NDA_VNI]));
  1199. else
  1200. vni = vxlan->default_dst.remote_vni;
  1201. rcu_read_lock();
  1202. f = __vxlan_find_mac(vxlan, addr, vni);
  1203. if (!f) {
  1204. NL_SET_ERR_MSG(extack, "Fdb entry not found");
  1205. err = -ENOENT;
  1206. goto errout;
  1207. }
  1208. err = vxlan_fdb_info(skb, vxlan, f, portid, seq,
  1209. RTM_NEWNEIGH, 0, first_remote_rcu(f));
  1210. errout:
  1211. rcu_read_unlock();
  1212. return err;
  1213. }
  1214. /* Watch incoming packets to learn mapping between Ethernet address
  1215. * and Tunnel endpoint.
  1216. * Return true if packet is bogus and should be dropped.
  1217. */
  1218. static enum skb_drop_reason vxlan_snoop(struct net_device *dev,
  1219. union vxlan_addr *src_ip,
  1220. const u8 *src_mac, u32 src_ifindex,
  1221. __be32 vni)
  1222. {
  1223. struct vxlan_dev *vxlan = netdev_priv(dev);
  1224. struct vxlan_fdb *f;
  1225. u32 ifindex = 0;
  1226. /* Ignore packets from invalid src-address */
  1227. if (!is_valid_ether_addr(src_mac))
  1228. return SKB_DROP_REASON_MAC_INVALID_SOURCE;
  1229. #if IS_ENABLED(CONFIG_IPV6)
  1230. if (src_ip->sa.sa_family == AF_INET6 &&
  1231. (ipv6_addr_type(&src_ip->sin6.sin6_addr) & IPV6_ADDR_LINKLOCAL))
  1232. ifindex = src_ifindex;
  1233. #endif
  1234. f = __vxlan_find_mac(vxlan, src_mac, vni);
  1235. if (likely(f)) {
  1236. struct vxlan_rdst *rdst = first_remote_rcu(f);
  1237. /* Don't override an fdb with nexthop with a learnt entry */
  1238. if (rcu_access_pointer(f->nh))
  1239. return SKB_DROP_REASON_VXLAN_ENTRY_EXISTS;
  1240. if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip) &&
  1241. rdst->remote_ifindex == ifindex))
  1242. return SKB_NOT_DROPPED_YET;
  1243. /* Don't migrate static entries, drop packets */
  1244. if (f->state & (NUD_PERMANENT | NUD_NOARP))
  1245. return SKB_DROP_REASON_VXLAN_ENTRY_EXISTS;
  1246. if (net_ratelimit())
  1247. netdev_info(dev,
  1248. "%pM migrated from %pIS to %pIS\n",
  1249. src_mac, &rdst->remote_ip.sa, &src_ip->sa);
  1250. rdst->remote_ip = *src_ip;
  1251. WRITE_ONCE(f->updated, jiffies);
  1252. vxlan_fdb_notify(vxlan, f, rdst, RTM_NEWNEIGH, true, NULL);
  1253. } else {
  1254. u32 hash_index = fdb_head_index(vxlan, src_mac, vni);
  1255. /* learned new entry */
  1256. spin_lock(&vxlan->hash_lock[hash_index]);
  1257. /* close off race between vxlan_flush and incoming packets */
  1258. if (netif_running(dev))
  1259. vxlan_fdb_update(vxlan, src_mac, src_ip,
  1260. NUD_REACHABLE,
  1261. NLM_F_EXCL|NLM_F_CREATE,
  1262. vxlan->cfg.dst_port,
  1263. vni,
  1264. vxlan->default_dst.remote_vni,
  1265. ifindex, NTF_SELF, 0, true, NULL);
  1266. spin_unlock(&vxlan->hash_lock[hash_index]);
  1267. }
  1268. return SKB_NOT_DROPPED_YET;
  1269. }
  1270. static bool __vxlan_sock_release_prep(struct vxlan_sock *vs)
  1271. {
  1272. struct vxlan_net *vn;
  1273. if (!vs)
  1274. return false;
  1275. if (!refcount_dec_and_test(&vs->refcnt))
  1276. return false;
  1277. vn = net_generic(sock_net(vs->sock->sk), vxlan_net_id);
  1278. spin_lock(&vn->sock_lock);
  1279. hlist_del_rcu(&vs->hlist);
  1280. udp_tunnel_notify_del_rx_port(vs->sock,
  1281. (vs->flags & VXLAN_F_GPE) ?
  1282. UDP_TUNNEL_TYPE_VXLAN_GPE :
  1283. UDP_TUNNEL_TYPE_VXLAN);
  1284. spin_unlock(&vn->sock_lock);
  1285. return true;
  1286. }
  1287. static void vxlan_sock_release(struct vxlan_dev *vxlan)
  1288. {
  1289. struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
  1290. #if IS_ENABLED(CONFIG_IPV6)
  1291. struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
  1292. RCU_INIT_POINTER(vxlan->vn6_sock, NULL);
  1293. #endif
  1294. RCU_INIT_POINTER(vxlan->vn4_sock, NULL);
  1295. synchronize_net();
  1296. if (vxlan->cfg.flags & VXLAN_F_VNIFILTER)
  1297. vxlan_vs_del_vnigrp(vxlan);
  1298. else
  1299. vxlan_vs_del_dev(vxlan);
  1300. if (__vxlan_sock_release_prep(sock4)) {
  1301. udp_tunnel_sock_release(sock4->sock);
  1302. kfree(sock4);
  1303. }
  1304. #if IS_ENABLED(CONFIG_IPV6)
  1305. if (__vxlan_sock_release_prep(sock6)) {
  1306. udp_tunnel_sock_release(sock6->sock);
  1307. kfree(sock6);
  1308. }
  1309. #endif
  1310. }
  1311. static bool vxlan_remcsum(struct vxlanhdr *unparsed,
  1312. struct sk_buff *skb, u32 vxflags)
  1313. {
  1314. size_t start, offset;
  1315. if (!(unparsed->vx_flags & VXLAN_HF_RCO) || skb->remcsum_offload)
  1316. goto out;
  1317. start = vxlan_rco_start(unparsed->vx_vni);
  1318. offset = start + vxlan_rco_offset(unparsed->vx_vni);
  1319. if (!pskb_may_pull(skb, offset + sizeof(u16)))
  1320. return false;
  1321. skb_remcsum_process(skb, (void *)(vxlan_hdr(skb) + 1), start, offset,
  1322. !!(vxflags & VXLAN_F_REMCSUM_NOPARTIAL));
  1323. out:
  1324. unparsed->vx_flags &= ~VXLAN_HF_RCO;
  1325. unparsed->vx_vni &= VXLAN_VNI_MASK;
  1326. return true;
  1327. }
  1328. static void vxlan_parse_gbp_hdr(struct vxlanhdr *unparsed,
  1329. struct sk_buff *skb, u32 vxflags,
  1330. struct vxlan_metadata *md)
  1331. {
  1332. struct vxlanhdr_gbp *gbp = (struct vxlanhdr_gbp *)unparsed;
  1333. struct metadata_dst *tun_dst;
  1334. if (!(unparsed->vx_flags & VXLAN_HF_GBP))
  1335. goto out;
  1336. md->gbp = ntohs(gbp->policy_id);
  1337. tun_dst = (struct metadata_dst *)skb_dst(skb);
  1338. if (tun_dst) {
  1339. __set_bit(IP_TUNNEL_VXLAN_OPT_BIT,
  1340. tun_dst->u.tun_info.key.tun_flags);
  1341. tun_dst->u.tun_info.options_len = sizeof(*md);
  1342. }
  1343. if (gbp->dont_learn)
  1344. md->gbp |= VXLAN_GBP_DONT_LEARN;
  1345. if (gbp->policy_applied)
  1346. md->gbp |= VXLAN_GBP_POLICY_APPLIED;
  1347. /* In flow-based mode, GBP is carried in dst_metadata */
  1348. if (!(vxflags & VXLAN_F_COLLECT_METADATA))
  1349. skb->mark = md->gbp;
  1350. out:
  1351. unparsed->vx_flags &= ~VXLAN_GBP_USED_BITS;
  1352. }
  1353. static enum skb_drop_reason vxlan_set_mac(struct vxlan_dev *vxlan,
  1354. struct vxlan_sock *vs,
  1355. struct sk_buff *skb, __be32 vni)
  1356. {
  1357. union vxlan_addr saddr;
  1358. u32 ifindex = skb->dev->ifindex;
  1359. skb_reset_mac_header(skb);
  1360. skb->protocol = eth_type_trans(skb, vxlan->dev);
  1361. skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
  1362. /* Ignore packet loops (and multicast echo) */
  1363. if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr))
  1364. return SKB_DROP_REASON_LOCAL_MAC;
  1365. /* Get address from the outer IP header */
  1366. if (vxlan_get_sk_family(vs) == AF_INET) {
  1367. saddr.sin.sin_addr.s_addr = ip_hdr(skb)->saddr;
  1368. saddr.sa.sa_family = AF_INET;
  1369. #if IS_ENABLED(CONFIG_IPV6)
  1370. } else {
  1371. saddr.sin6.sin6_addr = ipv6_hdr(skb)->saddr;
  1372. saddr.sa.sa_family = AF_INET6;
  1373. #endif
  1374. }
  1375. if (!(vxlan->cfg.flags & VXLAN_F_LEARN))
  1376. return SKB_NOT_DROPPED_YET;
  1377. return vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source,
  1378. ifindex, vni);
  1379. }
  1380. static bool vxlan_ecn_decapsulate(struct vxlan_sock *vs, void *oiph,
  1381. struct sk_buff *skb)
  1382. {
  1383. int err = 0;
  1384. if (vxlan_get_sk_family(vs) == AF_INET)
  1385. err = IP_ECN_decapsulate(oiph, skb);
  1386. #if IS_ENABLED(CONFIG_IPV6)
  1387. else
  1388. err = IP6_ECN_decapsulate(oiph, skb);
  1389. #endif
  1390. if (unlikely(err) && log_ecn_error) {
  1391. if (vxlan_get_sk_family(vs) == AF_INET)
  1392. net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
  1393. &((struct iphdr *)oiph)->saddr,
  1394. ((struct iphdr *)oiph)->tos);
  1395. else
  1396. net_info_ratelimited("non-ECT from %pI6\n",
  1397. &((struct ipv6hdr *)oiph)->saddr);
  1398. }
  1399. return err <= 1;
  1400. }
  1401. /* Callback from net/ipv4/udp.c to receive packets */
  1402. static int vxlan_rcv(struct sock *sk, struct sk_buff *skb)
  1403. {
  1404. struct vxlan_vni_node *vninode = NULL;
  1405. struct vxlan_dev *vxlan;
  1406. struct vxlan_sock *vs;
  1407. struct vxlanhdr unparsed;
  1408. struct vxlan_metadata _md;
  1409. struct vxlan_metadata *md = &_md;
  1410. __be16 protocol = htons(ETH_P_TEB);
  1411. enum skb_drop_reason reason;
  1412. bool raw_proto = false;
  1413. void *oiph;
  1414. __be32 vni = 0;
  1415. int nh;
  1416. /* Need UDP and VXLAN header to be present */
  1417. reason = pskb_may_pull_reason(skb, VXLAN_HLEN);
  1418. if (reason)
  1419. goto drop;
  1420. unparsed = *vxlan_hdr(skb);
  1421. /* VNI flag always required to be set */
  1422. if (!(unparsed.vx_flags & VXLAN_HF_VNI)) {
  1423. netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
  1424. ntohl(vxlan_hdr(skb)->vx_flags),
  1425. ntohl(vxlan_hdr(skb)->vx_vni));
  1426. reason = SKB_DROP_REASON_VXLAN_INVALID_HDR;
  1427. /* Return non vxlan pkt */
  1428. goto drop;
  1429. }
  1430. unparsed.vx_flags &= ~VXLAN_HF_VNI;
  1431. unparsed.vx_vni &= ~VXLAN_VNI_MASK;
  1432. vs = rcu_dereference_sk_user_data(sk);
  1433. if (!vs)
  1434. goto drop;
  1435. vni = vxlan_vni(vxlan_hdr(skb)->vx_vni);
  1436. vxlan = vxlan_vs_find_vni(vs, skb->dev->ifindex, vni, &vninode);
  1437. if (!vxlan) {
  1438. reason = SKB_DROP_REASON_VXLAN_VNI_NOT_FOUND;
  1439. goto drop;
  1440. }
  1441. /* For backwards compatibility, only allow reserved fields to be
  1442. * used by VXLAN extensions if explicitly requested.
  1443. */
  1444. if (vs->flags & VXLAN_F_GPE) {
  1445. if (!vxlan_parse_gpe_proto(&unparsed, &protocol))
  1446. goto drop;
  1447. unparsed.vx_flags &= ~VXLAN_GPE_USED_BITS;
  1448. raw_proto = true;
  1449. }
  1450. if (__iptunnel_pull_header(skb, VXLAN_HLEN, protocol, raw_proto,
  1451. !net_eq(vxlan->net, dev_net(vxlan->dev)))) {
  1452. reason = SKB_DROP_REASON_NOMEM;
  1453. goto drop;
  1454. }
  1455. if (vs->flags & VXLAN_F_REMCSUM_RX)
  1456. if (unlikely(!vxlan_remcsum(&unparsed, skb, vs->flags)))
  1457. goto drop;
  1458. if (vxlan_collect_metadata(vs)) {
  1459. IP_TUNNEL_DECLARE_FLAGS(flags) = { };
  1460. struct metadata_dst *tun_dst;
  1461. __set_bit(IP_TUNNEL_KEY_BIT, flags);
  1462. tun_dst = udp_tun_rx_dst(skb, vxlan_get_sk_family(vs), flags,
  1463. key32_to_tunnel_id(vni), sizeof(*md));
  1464. if (!tun_dst) {
  1465. reason = SKB_DROP_REASON_NOMEM;
  1466. goto drop;
  1467. }
  1468. md = ip_tunnel_info_opts(&tun_dst->u.tun_info);
  1469. skb_dst_set(skb, (struct dst_entry *)tun_dst);
  1470. } else {
  1471. memset(md, 0, sizeof(*md));
  1472. }
  1473. if (vs->flags & VXLAN_F_GBP)
  1474. vxlan_parse_gbp_hdr(&unparsed, skb, vs->flags, md);
  1475. /* Note that GBP and GPE can never be active together. This is
  1476. * ensured in vxlan_dev_configure.
  1477. */
  1478. if (unparsed.vx_flags || unparsed.vx_vni) {
  1479. /* If there are any unprocessed flags remaining treat
  1480. * this as a malformed packet. This behavior diverges from
  1481. * VXLAN RFC (RFC7348) which stipulates that bits in reserved
  1482. * in reserved fields are to be ignored. The approach here
  1483. * maintains compatibility with previous stack code, and also
  1484. * is more robust and provides a little more security in
  1485. * adding extensions to VXLAN.
  1486. */
  1487. reason = SKB_DROP_REASON_VXLAN_INVALID_HDR;
  1488. goto drop;
  1489. }
  1490. if (!raw_proto) {
  1491. reason = vxlan_set_mac(vxlan, vs, skb, vni);
  1492. if (reason)
  1493. goto drop;
  1494. } else {
  1495. skb_reset_mac_header(skb);
  1496. skb->dev = vxlan->dev;
  1497. skb->pkt_type = PACKET_HOST;
  1498. }
  1499. /* Save offset of outer header relative to skb->head,
  1500. * because we are going to reset the network header to the inner header
  1501. * and might change skb->head.
  1502. */
  1503. nh = skb_network_header(skb) - skb->head;
  1504. skb_reset_network_header(skb);
  1505. reason = pskb_inet_may_pull_reason(skb);
  1506. if (reason) {
  1507. DEV_STATS_INC(vxlan->dev, rx_length_errors);
  1508. DEV_STATS_INC(vxlan->dev, rx_errors);
  1509. vxlan_vnifilter_count(vxlan, vni, vninode,
  1510. VXLAN_VNI_STATS_RX_ERRORS, 0);
  1511. goto drop;
  1512. }
  1513. /* Get the outer header. */
  1514. oiph = skb->head + nh;
  1515. if (!vxlan_ecn_decapsulate(vs, oiph, skb)) {
  1516. reason = SKB_DROP_REASON_IP_TUNNEL_ECN;
  1517. DEV_STATS_INC(vxlan->dev, rx_frame_errors);
  1518. DEV_STATS_INC(vxlan->dev, rx_errors);
  1519. vxlan_vnifilter_count(vxlan, vni, vninode,
  1520. VXLAN_VNI_STATS_RX_ERRORS, 0);
  1521. goto drop;
  1522. }
  1523. rcu_read_lock();
  1524. if (unlikely(!(vxlan->dev->flags & IFF_UP))) {
  1525. rcu_read_unlock();
  1526. dev_core_stats_rx_dropped_inc(vxlan->dev);
  1527. vxlan_vnifilter_count(vxlan, vni, vninode,
  1528. VXLAN_VNI_STATS_RX_DROPS, 0);
  1529. reason = SKB_DROP_REASON_DEV_READY;
  1530. goto drop;
  1531. }
  1532. dev_sw_netstats_rx_add(vxlan->dev, skb->len);
  1533. vxlan_vnifilter_count(vxlan, vni, vninode, VXLAN_VNI_STATS_RX, skb->len);
  1534. gro_cells_receive(&vxlan->gro_cells, skb);
  1535. rcu_read_unlock();
  1536. return 0;
  1537. drop:
  1538. reason = reason ?: SKB_DROP_REASON_NOT_SPECIFIED;
  1539. /* Consume bad packet */
  1540. kfree_skb_reason(skb, reason);
  1541. return 0;
  1542. }
  1543. /* Callback from net/ipv{4,6}/udp.c to check that we have a VNI for errors */
  1544. static int vxlan_err_lookup(struct sock *sk, struct sk_buff *skb)
  1545. {
  1546. struct vxlan_dev *vxlan;
  1547. struct vxlan_sock *vs;
  1548. struct vxlanhdr *hdr;
  1549. __be32 vni;
  1550. if (!pskb_may_pull(skb, skb_transport_offset(skb) + VXLAN_HLEN))
  1551. return -EINVAL;
  1552. hdr = vxlan_hdr(skb);
  1553. if (!(hdr->vx_flags & VXLAN_HF_VNI))
  1554. return -EINVAL;
  1555. vs = rcu_dereference_sk_user_data(sk);
  1556. if (!vs)
  1557. return -ENOENT;
  1558. vni = vxlan_vni(hdr->vx_vni);
  1559. vxlan = vxlan_vs_find_vni(vs, skb->dev->ifindex, vni, NULL);
  1560. if (!vxlan)
  1561. return -ENOENT;
  1562. return 0;
  1563. }
  1564. static int arp_reduce(struct net_device *dev, struct sk_buff *skb, __be32 vni)
  1565. {
  1566. struct vxlan_dev *vxlan = netdev_priv(dev);
  1567. struct arphdr *parp;
  1568. u8 *arpptr, *sha;
  1569. __be32 sip, tip;
  1570. struct neighbour *n;
  1571. if (dev->flags & IFF_NOARP)
  1572. goto out;
  1573. if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
  1574. dev_core_stats_tx_dropped_inc(dev);
  1575. vxlan_vnifilter_count(vxlan, vni, NULL,
  1576. VXLAN_VNI_STATS_TX_DROPS, 0);
  1577. goto out;
  1578. }
  1579. parp = arp_hdr(skb);
  1580. if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
  1581. parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
  1582. parp->ar_pro != htons(ETH_P_IP) ||
  1583. parp->ar_op != htons(ARPOP_REQUEST) ||
  1584. parp->ar_hln != dev->addr_len ||
  1585. parp->ar_pln != 4)
  1586. goto out;
  1587. arpptr = (u8 *)parp + sizeof(struct arphdr);
  1588. sha = arpptr;
  1589. arpptr += dev->addr_len; /* sha */
  1590. memcpy(&sip, arpptr, sizeof(sip));
  1591. arpptr += sizeof(sip);
  1592. arpptr += dev->addr_len; /* tha */
  1593. memcpy(&tip, arpptr, sizeof(tip));
  1594. if (ipv4_is_loopback(tip) ||
  1595. ipv4_is_multicast(tip))
  1596. goto out;
  1597. n = neigh_lookup(&arp_tbl, &tip, dev);
  1598. if (n) {
  1599. struct vxlan_rdst *rdst = NULL;
  1600. struct vxlan_fdb *f;
  1601. struct sk_buff *reply;
  1602. if (!(READ_ONCE(n->nud_state) & NUD_CONNECTED)) {
  1603. neigh_release(n);
  1604. goto out;
  1605. }
  1606. rcu_read_lock();
  1607. f = vxlan_find_mac_tx(vxlan, n->ha, vni);
  1608. if (f)
  1609. rdst = first_remote_rcu(f);
  1610. if (rdst && vxlan_addr_any(&rdst->remote_ip)) {
  1611. /* bridge-local neighbor */
  1612. neigh_release(n);
  1613. rcu_read_unlock();
  1614. goto out;
  1615. }
  1616. rcu_read_unlock();
  1617. reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
  1618. n->ha, sha);
  1619. neigh_release(n);
  1620. if (reply == NULL)
  1621. goto out;
  1622. skb_reset_mac_header(reply);
  1623. __skb_pull(reply, skb_network_offset(reply));
  1624. reply->ip_summed = CHECKSUM_UNNECESSARY;
  1625. reply->pkt_type = PACKET_HOST;
  1626. if (netif_rx(reply) == NET_RX_DROP) {
  1627. dev_core_stats_rx_dropped_inc(dev);
  1628. vxlan_vnifilter_count(vxlan, vni, NULL,
  1629. VXLAN_VNI_STATS_RX_DROPS, 0);
  1630. }
  1631. } else if (vxlan->cfg.flags & VXLAN_F_L3MISS) {
  1632. union vxlan_addr ipa = {
  1633. .sin.sin_addr.s_addr = tip,
  1634. .sin.sin_family = AF_INET,
  1635. };
  1636. vxlan_ip_miss(dev, &ipa);
  1637. }
  1638. out:
  1639. consume_skb(skb);
  1640. return NETDEV_TX_OK;
  1641. }
  1642. #if IS_ENABLED(CONFIG_IPV6)
  1643. static struct sk_buff *vxlan_na_create(struct sk_buff *request,
  1644. struct neighbour *n, bool isrouter)
  1645. {
  1646. struct net_device *dev = request->dev;
  1647. struct sk_buff *reply;
  1648. struct nd_msg *ns, *na;
  1649. struct ipv6hdr *pip6;
  1650. u8 *daddr;
  1651. int na_olen = 8; /* opt hdr + ETH_ALEN for target */
  1652. int ns_olen;
  1653. int i, len;
  1654. if (dev == NULL || !pskb_may_pull(request, request->len))
  1655. return NULL;
  1656. len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) +
  1657. sizeof(*na) + na_olen + dev->needed_tailroom;
  1658. reply = alloc_skb(len, GFP_ATOMIC);
  1659. if (reply == NULL)
  1660. return NULL;
  1661. reply->protocol = htons(ETH_P_IPV6);
  1662. reply->dev = dev;
  1663. skb_reserve(reply, LL_RESERVED_SPACE(request->dev));
  1664. skb_push(reply, sizeof(struct ethhdr));
  1665. skb_reset_mac_header(reply);
  1666. ns = (struct nd_msg *)(ipv6_hdr(request) + 1);
  1667. daddr = eth_hdr(request)->h_source;
  1668. ns_olen = request->len - skb_network_offset(request) -
  1669. sizeof(struct ipv6hdr) - sizeof(*ns);
  1670. for (i = 0; i < ns_olen-1; i += (ns->opt[i+1]<<3)) {
  1671. if (!ns->opt[i + 1]) {
  1672. kfree_skb(reply);
  1673. return NULL;
  1674. }
  1675. if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) {
  1676. daddr = ns->opt + i + sizeof(struct nd_opt_hdr);
  1677. break;
  1678. }
  1679. }
  1680. /* Ethernet header */
  1681. ether_addr_copy(eth_hdr(reply)->h_dest, daddr);
  1682. ether_addr_copy(eth_hdr(reply)->h_source, n->ha);
  1683. eth_hdr(reply)->h_proto = htons(ETH_P_IPV6);
  1684. reply->protocol = htons(ETH_P_IPV6);
  1685. skb_pull(reply, sizeof(struct ethhdr));
  1686. skb_reset_network_header(reply);
  1687. skb_put(reply, sizeof(struct ipv6hdr));
  1688. /* IPv6 header */
  1689. pip6 = ipv6_hdr(reply);
  1690. memset(pip6, 0, sizeof(struct ipv6hdr));
  1691. pip6->version = 6;
  1692. pip6->priority = ipv6_hdr(request)->priority;
  1693. pip6->nexthdr = IPPROTO_ICMPV6;
  1694. pip6->hop_limit = 255;
  1695. pip6->daddr = ipv6_hdr(request)->saddr;
  1696. pip6->saddr = *(struct in6_addr *)n->primary_key;
  1697. skb_pull(reply, sizeof(struct ipv6hdr));
  1698. skb_reset_transport_header(reply);
  1699. /* Neighbor Advertisement */
  1700. na = skb_put_zero(reply, sizeof(*na) + na_olen);
  1701. na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
  1702. na->icmph.icmp6_router = isrouter;
  1703. na->icmph.icmp6_override = 1;
  1704. na->icmph.icmp6_solicited = 1;
  1705. na->target = ns->target;
  1706. ether_addr_copy(&na->opt[2], n->ha);
  1707. na->opt[0] = ND_OPT_TARGET_LL_ADDR;
  1708. na->opt[1] = na_olen >> 3;
  1709. na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr,
  1710. &pip6->daddr, sizeof(*na)+na_olen, IPPROTO_ICMPV6,
  1711. csum_partial(na, sizeof(*na)+na_olen, 0));
  1712. pip6->payload_len = htons(sizeof(*na)+na_olen);
  1713. skb_push(reply, sizeof(struct ipv6hdr));
  1714. reply->ip_summed = CHECKSUM_UNNECESSARY;
  1715. return reply;
  1716. }
  1717. static int neigh_reduce(struct net_device *dev, struct sk_buff *skb, __be32 vni)
  1718. {
  1719. struct vxlan_dev *vxlan = netdev_priv(dev);
  1720. const struct in6_addr *daddr;
  1721. const struct ipv6hdr *iphdr;
  1722. struct inet6_dev *in6_dev;
  1723. struct neighbour *n;
  1724. struct nd_msg *msg;
  1725. rcu_read_lock();
  1726. in6_dev = __in6_dev_get(dev);
  1727. if (!in6_dev)
  1728. goto out;
  1729. iphdr = ipv6_hdr(skb);
  1730. daddr = &iphdr->daddr;
  1731. msg = (struct nd_msg *)(iphdr + 1);
  1732. if (ipv6_addr_loopback(daddr) ||
  1733. ipv6_addr_is_multicast(&msg->target))
  1734. goto out;
  1735. n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, dev);
  1736. if (n) {
  1737. struct vxlan_rdst *rdst = NULL;
  1738. struct vxlan_fdb *f;
  1739. struct sk_buff *reply;
  1740. if (!(READ_ONCE(n->nud_state) & NUD_CONNECTED)) {
  1741. neigh_release(n);
  1742. goto out;
  1743. }
  1744. f = vxlan_find_mac_tx(vxlan, n->ha, vni);
  1745. if (f)
  1746. rdst = first_remote_rcu(f);
  1747. if (rdst && vxlan_addr_any(&rdst->remote_ip)) {
  1748. /* bridge-local neighbor */
  1749. neigh_release(n);
  1750. goto out;
  1751. }
  1752. reply = vxlan_na_create(skb, n,
  1753. !!(f ? f->flags & NTF_ROUTER : 0));
  1754. neigh_release(n);
  1755. if (reply == NULL)
  1756. goto out;
  1757. if (netif_rx(reply) == NET_RX_DROP) {
  1758. dev_core_stats_rx_dropped_inc(dev);
  1759. vxlan_vnifilter_count(vxlan, vni, NULL,
  1760. VXLAN_VNI_STATS_RX_DROPS, 0);
  1761. }
  1762. } else if (vxlan->cfg.flags & VXLAN_F_L3MISS) {
  1763. union vxlan_addr ipa = {
  1764. .sin6.sin6_addr = msg->target,
  1765. .sin6.sin6_family = AF_INET6,
  1766. };
  1767. vxlan_ip_miss(dev, &ipa);
  1768. }
  1769. out:
  1770. rcu_read_unlock();
  1771. consume_skb(skb);
  1772. return NETDEV_TX_OK;
  1773. }
  1774. #endif
  1775. static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
  1776. {
  1777. struct vxlan_dev *vxlan = netdev_priv(dev);
  1778. struct neighbour *n;
  1779. if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
  1780. return false;
  1781. n = NULL;
  1782. switch (ntohs(eth_hdr(skb)->h_proto)) {
  1783. case ETH_P_IP:
  1784. {
  1785. struct iphdr *pip;
  1786. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  1787. return false;
  1788. pip = ip_hdr(skb);
  1789. n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
  1790. if (!n && (vxlan->cfg.flags & VXLAN_F_L3MISS)) {
  1791. union vxlan_addr ipa = {
  1792. .sin.sin_addr.s_addr = pip->daddr,
  1793. .sin.sin_family = AF_INET,
  1794. };
  1795. vxlan_ip_miss(dev, &ipa);
  1796. return false;
  1797. }
  1798. break;
  1799. }
  1800. #if IS_ENABLED(CONFIG_IPV6)
  1801. case ETH_P_IPV6:
  1802. {
  1803. struct ipv6hdr *pip6;
  1804. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
  1805. return false;
  1806. pip6 = ipv6_hdr(skb);
  1807. n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
  1808. if (!n && (vxlan->cfg.flags & VXLAN_F_L3MISS)) {
  1809. union vxlan_addr ipa = {
  1810. .sin6.sin6_addr = pip6->daddr,
  1811. .sin6.sin6_family = AF_INET6,
  1812. };
  1813. vxlan_ip_miss(dev, &ipa);
  1814. return false;
  1815. }
  1816. break;
  1817. }
  1818. #endif
  1819. default:
  1820. return false;
  1821. }
  1822. if (n) {
  1823. bool diff;
  1824. diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha);
  1825. if (diff) {
  1826. memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
  1827. dev->addr_len);
  1828. memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
  1829. }
  1830. neigh_release(n);
  1831. return diff;
  1832. }
  1833. return false;
  1834. }
  1835. static int vxlan_build_gpe_hdr(struct vxlanhdr *vxh, __be16 protocol)
  1836. {
  1837. struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)vxh;
  1838. gpe->np_applied = 1;
  1839. gpe->next_protocol = tun_p_from_eth_p(protocol);
  1840. if (!gpe->next_protocol)
  1841. return -EPFNOSUPPORT;
  1842. return 0;
  1843. }
  1844. static int vxlan_build_skb(struct sk_buff *skb, struct dst_entry *dst,
  1845. int iphdr_len, __be32 vni,
  1846. struct vxlan_metadata *md, u32 vxflags,
  1847. bool udp_sum)
  1848. {
  1849. struct vxlanhdr *vxh;
  1850. int min_headroom;
  1851. int err;
  1852. int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
  1853. __be16 inner_protocol = htons(ETH_P_TEB);
  1854. if ((vxflags & VXLAN_F_REMCSUM_TX) &&
  1855. skb->ip_summed == CHECKSUM_PARTIAL) {
  1856. int csum_start = skb_checksum_start_offset(skb);
  1857. if (csum_start <= VXLAN_MAX_REMCSUM_START &&
  1858. !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
  1859. (skb->csum_offset == offsetof(struct udphdr, check) ||
  1860. skb->csum_offset == offsetof(struct tcphdr, check)))
  1861. type |= SKB_GSO_TUNNEL_REMCSUM;
  1862. }
  1863. min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
  1864. + VXLAN_HLEN + iphdr_len;
  1865. /* Need space for new headers (invalidates iph ptr) */
  1866. err = skb_cow_head(skb, min_headroom);
  1867. if (unlikely(err))
  1868. return err;
  1869. err = iptunnel_handle_offloads(skb, type);
  1870. if (err)
  1871. return err;
  1872. vxh = __skb_push(skb, sizeof(*vxh));
  1873. vxh->vx_flags = VXLAN_HF_VNI;
  1874. vxh->vx_vni = vxlan_vni_field(vni);
  1875. if (type & SKB_GSO_TUNNEL_REMCSUM) {
  1876. unsigned int start;
  1877. start = skb_checksum_start_offset(skb) - sizeof(struct vxlanhdr);
  1878. vxh->vx_vni |= vxlan_compute_rco(start, skb->csum_offset);
  1879. vxh->vx_flags |= VXLAN_HF_RCO;
  1880. if (!skb_is_gso(skb)) {
  1881. skb->ip_summed = CHECKSUM_NONE;
  1882. skb->encapsulation = 0;
  1883. }
  1884. }
  1885. if (vxflags & VXLAN_F_GBP)
  1886. vxlan_build_gbp_hdr(vxh, md);
  1887. if (vxflags & VXLAN_F_GPE) {
  1888. err = vxlan_build_gpe_hdr(vxh, skb->protocol);
  1889. if (err < 0)
  1890. return err;
  1891. inner_protocol = skb->protocol;
  1892. }
  1893. skb_set_inner_protocol(skb, inner_protocol);
  1894. return 0;
  1895. }
  1896. /* Bypass encapsulation if the destination is local */
  1897. static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
  1898. struct vxlan_dev *dst_vxlan, __be32 vni,
  1899. bool snoop)
  1900. {
  1901. union vxlan_addr loopback;
  1902. union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
  1903. struct net_device *dev;
  1904. int len = skb->len;
  1905. skb->pkt_type = PACKET_HOST;
  1906. skb->encapsulation = 0;
  1907. skb->dev = dst_vxlan->dev;
  1908. __skb_pull(skb, skb_network_offset(skb));
  1909. if (remote_ip->sa.sa_family == AF_INET) {
  1910. loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  1911. loopback.sa.sa_family = AF_INET;
  1912. #if IS_ENABLED(CONFIG_IPV6)
  1913. } else {
  1914. loopback.sin6.sin6_addr = in6addr_loopback;
  1915. loopback.sa.sa_family = AF_INET6;
  1916. #endif
  1917. }
  1918. rcu_read_lock();
  1919. dev = skb->dev;
  1920. if (unlikely(!(dev->flags & IFF_UP))) {
  1921. kfree_skb(skb);
  1922. goto drop;
  1923. }
  1924. if ((dst_vxlan->cfg.flags & VXLAN_F_LEARN) && snoop)
  1925. vxlan_snoop(dev, &loopback, eth_hdr(skb)->h_source, 0, vni);
  1926. dev_sw_netstats_tx_add(src_vxlan->dev, 1, len);
  1927. vxlan_vnifilter_count(src_vxlan, vni, NULL, VXLAN_VNI_STATS_TX, len);
  1928. if (__netif_rx(skb) == NET_RX_SUCCESS) {
  1929. dev_sw_netstats_rx_add(dst_vxlan->dev, len);
  1930. vxlan_vnifilter_count(dst_vxlan, vni, NULL, VXLAN_VNI_STATS_RX,
  1931. len);
  1932. } else {
  1933. drop:
  1934. dev_core_stats_rx_dropped_inc(dev);
  1935. vxlan_vnifilter_count(dst_vxlan, vni, NULL,
  1936. VXLAN_VNI_STATS_RX_DROPS, 0);
  1937. }
  1938. rcu_read_unlock();
  1939. }
  1940. static int encap_bypass_if_local(struct sk_buff *skb, struct net_device *dev,
  1941. struct vxlan_dev *vxlan,
  1942. int addr_family,
  1943. __be16 dst_port, int dst_ifindex, __be32 vni,
  1944. struct dst_entry *dst,
  1945. u32 rt_flags)
  1946. {
  1947. #if IS_ENABLED(CONFIG_IPV6)
  1948. /* IPv6 rt-flags are checked against RTF_LOCAL, but the value of
  1949. * RTF_LOCAL is equal to RTCF_LOCAL. So to keep code simple
  1950. * we can use RTCF_LOCAL which works for ipv4 and ipv6 route entry.
  1951. */
  1952. BUILD_BUG_ON(RTCF_LOCAL != RTF_LOCAL);
  1953. #endif
  1954. /* Bypass encapsulation if the destination is local */
  1955. if (rt_flags & RTCF_LOCAL &&
  1956. !(rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST)) &&
  1957. vxlan->cfg.flags & VXLAN_F_LOCALBYPASS) {
  1958. struct vxlan_dev *dst_vxlan;
  1959. dst_release(dst);
  1960. dst_vxlan = vxlan_find_vni(vxlan->net, dst_ifindex, vni,
  1961. addr_family, dst_port,
  1962. vxlan->cfg.flags);
  1963. if (!dst_vxlan) {
  1964. DEV_STATS_INC(dev, tx_errors);
  1965. vxlan_vnifilter_count(vxlan, vni, NULL,
  1966. VXLAN_VNI_STATS_TX_ERRORS, 0);
  1967. kfree_skb(skb);
  1968. return -ENOENT;
  1969. }
  1970. vxlan_encap_bypass(skb, vxlan, dst_vxlan, vni, true);
  1971. return 1;
  1972. }
  1973. return 0;
  1974. }
  1975. void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
  1976. __be32 default_vni, struct vxlan_rdst *rdst, bool did_rsc)
  1977. {
  1978. struct dst_cache *dst_cache;
  1979. struct ip_tunnel_info *info;
  1980. struct ip_tunnel_key *pkey;
  1981. struct ip_tunnel_key key;
  1982. struct vxlan_dev *vxlan = netdev_priv(dev);
  1983. const struct iphdr *old_iph;
  1984. struct vxlan_metadata _md;
  1985. struct vxlan_metadata *md = &_md;
  1986. unsigned int pkt_len = skb->len;
  1987. __be16 src_port = 0, dst_port;
  1988. struct dst_entry *ndst = NULL;
  1989. int addr_family;
  1990. __u8 tos, ttl;
  1991. int ifindex;
  1992. int err;
  1993. u32 flags = vxlan->cfg.flags;
  1994. bool use_cache;
  1995. bool udp_sum = false;
  1996. bool xnet = !net_eq(vxlan->net, dev_net(vxlan->dev));
  1997. bool no_eth_encap;
  1998. __be32 vni = 0;
  1999. no_eth_encap = flags & VXLAN_F_GPE && skb->protocol != htons(ETH_P_TEB);
  2000. if (!skb_vlan_inet_prepare(skb, no_eth_encap))
  2001. goto drop;
  2002. old_iph = ip_hdr(skb);
  2003. info = skb_tunnel_info(skb);
  2004. use_cache = ip_tunnel_dst_cache_usable(skb, info);
  2005. if (rdst) {
  2006. memset(&key, 0, sizeof(key));
  2007. pkey = &key;
  2008. if (vxlan_addr_any(&rdst->remote_ip)) {
  2009. if (did_rsc) {
  2010. /* short-circuited back to local bridge */
  2011. vxlan_encap_bypass(skb, vxlan, vxlan,
  2012. default_vni, true);
  2013. return;
  2014. }
  2015. goto drop;
  2016. }
  2017. addr_family = vxlan->cfg.saddr.sa.sa_family;
  2018. dst_port = rdst->remote_port ? rdst->remote_port : vxlan->cfg.dst_port;
  2019. vni = (rdst->remote_vni) ? : default_vni;
  2020. ifindex = rdst->remote_ifindex;
  2021. if (addr_family == AF_INET) {
  2022. key.u.ipv4.src = vxlan->cfg.saddr.sin.sin_addr.s_addr;
  2023. key.u.ipv4.dst = rdst->remote_ip.sin.sin_addr.s_addr;
  2024. } else {
  2025. key.u.ipv6.src = vxlan->cfg.saddr.sin6.sin6_addr;
  2026. key.u.ipv6.dst = rdst->remote_ip.sin6.sin6_addr;
  2027. }
  2028. dst_cache = &rdst->dst_cache;
  2029. md->gbp = skb->mark;
  2030. if (flags & VXLAN_F_TTL_INHERIT) {
  2031. ttl = ip_tunnel_get_ttl(old_iph, skb);
  2032. } else {
  2033. ttl = vxlan->cfg.ttl;
  2034. if (!ttl && vxlan_addr_multicast(&rdst->remote_ip))
  2035. ttl = 1;
  2036. }
  2037. tos = vxlan->cfg.tos;
  2038. if (tos == 1)
  2039. tos = ip_tunnel_get_dsfield(old_iph, skb);
  2040. if (tos && !info)
  2041. use_cache = false;
  2042. if (addr_family == AF_INET)
  2043. udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM_TX);
  2044. else
  2045. udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM6_TX);
  2046. #if IS_ENABLED(CONFIG_IPV6)
  2047. switch (vxlan->cfg.label_policy) {
  2048. case VXLAN_LABEL_FIXED:
  2049. key.label = vxlan->cfg.label;
  2050. break;
  2051. case VXLAN_LABEL_INHERIT:
  2052. key.label = ip_tunnel_get_flowlabel(old_iph, skb);
  2053. break;
  2054. default:
  2055. DEBUG_NET_WARN_ON_ONCE(1);
  2056. goto drop;
  2057. }
  2058. #endif
  2059. } else {
  2060. if (!info) {
  2061. WARN_ONCE(1, "%s: Missing encapsulation instructions\n",
  2062. dev->name);
  2063. goto drop;
  2064. }
  2065. pkey = &info->key;
  2066. addr_family = ip_tunnel_info_af(info);
  2067. dst_port = info->key.tp_dst ? : vxlan->cfg.dst_port;
  2068. vni = tunnel_id_to_key32(info->key.tun_id);
  2069. ifindex = 0;
  2070. dst_cache = &info->dst_cache;
  2071. if (test_bit(IP_TUNNEL_VXLAN_OPT_BIT, info->key.tun_flags)) {
  2072. if (info->options_len < sizeof(*md))
  2073. goto drop;
  2074. md = ip_tunnel_info_opts(info);
  2075. }
  2076. ttl = info->key.ttl;
  2077. tos = info->key.tos;
  2078. udp_sum = test_bit(IP_TUNNEL_CSUM_BIT, info->key.tun_flags);
  2079. }
  2080. src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
  2081. vxlan->cfg.port_max, true);
  2082. rcu_read_lock();
  2083. if (addr_family == AF_INET) {
  2084. struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock);
  2085. struct rtable *rt;
  2086. __be16 df = 0;
  2087. __be32 saddr;
  2088. if (!ifindex)
  2089. ifindex = sock4->sock->sk->sk_bound_dev_if;
  2090. rt = udp_tunnel_dst_lookup(skb, dev, vxlan->net, ifindex,
  2091. &saddr, pkey, src_port, dst_port,
  2092. tos, use_cache ? dst_cache : NULL);
  2093. if (IS_ERR(rt)) {
  2094. err = PTR_ERR(rt);
  2095. goto tx_error;
  2096. }
  2097. if (!info) {
  2098. /* Bypass encapsulation if the destination is local */
  2099. err = encap_bypass_if_local(skb, dev, vxlan, AF_INET,
  2100. dst_port, ifindex, vni,
  2101. &rt->dst, rt->rt_flags);
  2102. if (err)
  2103. goto out_unlock;
  2104. if (vxlan->cfg.df == VXLAN_DF_SET) {
  2105. df = htons(IP_DF);
  2106. } else if (vxlan->cfg.df == VXLAN_DF_INHERIT) {
  2107. struct ethhdr *eth = eth_hdr(skb);
  2108. if (ntohs(eth->h_proto) == ETH_P_IPV6 ||
  2109. (ntohs(eth->h_proto) == ETH_P_IP &&
  2110. old_iph->frag_off & htons(IP_DF)))
  2111. df = htons(IP_DF);
  2112. }
  2113. } else if (test_bit(IP_TUNNEL_DONT_FRAGMENT_BIT,
  2114. info->key.tun_flags)) {
  2115. df = htons(IP_DF);
  2116. }
  2117. ndst = &rt->dst;
  2118. err = skb_tunnel_check_pmtu(skb, ndst, vxlan_headroom(flags & VXLAN_F_GPE),
  2119. netif_is_any_bridge_port(dev));
  2120. if (err < 0) {
  2121. goto tx_error;
  2122. } else if (err) {
  2123. if (info) {
  2124. struct ip_tunnel_info *unclone;
  2125. unclone = skb_tunnel_info_unclone(skb);
  2126. if (unlikely(!unclone))
  2127. goto tx_error;
  2128. unclone->key.u.ipv4.src = pkey->u.ipv4.dst;
  2129. unclone->key.u.ipv4.dst = saddr;
  2130. }
  2131. vxlan_encap_bypass(skb, vxlan, vxlan, vni, false);
  2132. dst_release(ndst);
  2133. goto out_unlock;
  2134. }
  2135. tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
  2136. ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
  2137. err = vxlan_build_skb(skb, ndst, sizeof(struct iphdr),
  2138. vni, md, flags, udp_sum);
  2139. if (err < 0)
  2140. goto tx_error;
  2141. udp_tunnel_xmit_skb(rt, sock4->sock->sk, skb, saddr,
  2142. pkey->u.ipv4.dst, tos, ttl, df,
  2143. src_port, dst_port, xnet, !udp_sum);
  2144. #if IS_ENABLED(CONFIG_IPV6)
  2145. } else {
  2146. struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock);
  2147. struct in6_addr saddr;
  2148. if (!ifindex)
  2149. ifindex = sock6->sock->sk->sk_bound_dev_if;
  2150. ndst = udp_tunnel6_dst_lookup(skb, dev, vxlan->net, sock6->sock,
  2151. ifindex, &saddr, pkey,
  2152. src_port, dst_port, tos,
  2153. use_cache ? dst_cache : NULL);
  2154. if (IS_ERR(ndst)) {
  2155. err = PTR_ERR(ndst);
  2156. ndst = NULL;
  2157. goto tx_error;
  2158. }
  2159. if (!info) {
  2160. u32 rt6i_flags = dst_rt6_info(ndst)->rt6i_flags;
  2161. err = encap_bypass_if_local(skb, dev, vxlan, AF_INET6,
  2162. dst_port, ifindex, vni,
  2163. ndst, rt6i_flags);
  2164. if (err)
  2165. goto out_unlock;
  2166. }
  2167. err = skb_tunnel_check_pmtu(skb, ndst,
  2168. vxlan_headroom((flags & VXLAN_F_GPE) | VXLAN_F_IPV6),
  2169. netif_is_any_bridge_port(dev));
  2170. if (err < 0) {
  2171. goto tx_error;
  2172. } else if (err) {
  2173. if (info) {
  2174. struct ip_tunnel_info *unclone;
  2175. unclone = skb_tunnel_info_unclone(skb);
  2176. if (unlikely(!unclone))
  2177. goto tx_error;
  2178. unclone->key.u.ipv6.src = pkey->u.ipv6.dst;
  2179. unclone->key.u.ipv6.dst = saddr;
  2180. }
  2181. vxlan_encap_bypass(skb, vxlan, vxlan, vni, false);
  2182. dst_release(ndst);
  2183. goto out_unlock;
  2184. }
  2185. tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
  2186. ttl = ttl ? : ip6_dst_hoplimit(ndst);
  2187. skb_scrub_packet(skb, xnet);
  2188. err = vxlan_build_skb(skb, ndst, sizeof(struct ipv6hdr),
  2189. vni, md, flags, udp_sum);
  2190. if (err < 0)
  2191. goto tx_error;
  2192. udp_tunnel6_xmit_skb(ndst, sock6->sock->sk, skb, dev,
  2193. &saddr, &pkey->u.ipv6.dst, tos, ttl,
  2194. pkey->label, src_port, dst_port, !udp_sum);
  2195. #endif
  2196. }
  2197. vxlan_vnifilter_count(vxlan, vni, NULL, VXLAN_VNI_STATS_TX, pkt_len);
  2198. out_unlock:
  2199. rcu_read_unlock();
  2200. return;
  2201. drop:
  2202. dev_core_stats_tx_dropped_inc(dev);
  2203. vxlan_vnifilter_count(vxlan, vni, NULL, VXLAN_VNI_STATS_TX_DROPS, 0);
  2204. dev_kfree_skb(skb);
  2205. return;
  2206. tx_error:
  2207. rcu_read_unlock();
  2208. if (err == -ELOOP)
  2209. DEV_STATS_INC(dev, collisions);
  2210. else if (err == -ENETUNREACH)
  2211. DEV_STATS_INC(dev, tx_carrier_errors);
  2212. dst_release(ndst);
  2213. DEV_STATS_INC(dev, tx_errors);
  2214. vxlan_vnifilter_count(vxlan, vni, NULL, VXLAN_VNI_STATS_TX_ERRORS, 0);
  2215. kfree_skb(skb);
  2216. }
  2217. static void vxlan_xmit_nh(struct sk_buff *skb, struct net_device *dev,
  2218. struct vxlan_fdb *f, __be32 vni, bool did_rsc)
  2219. {
  2220. struct vxlan_rdst nh_rdst;
  2221. struct nexthop *nh;
  2222. bool do_xmit;
  2223. u32 hash;
  2224. memset(&nh_rdst, 0, sizeof(struct vxlan_rdst));
  2225. hash = skb_get_hash(skb);
  2226. nh = rcu_dereference(f->nh);
  2227. if (!nh)
  2228. goto drop;
  2229. do_xmit = vxlan_fdb_nh_path_select(nh, hash, &nh_rdst);
  2230. if (likely(do_xmit))
  2231. vxlan_xmit_one(skb, dev, vni, &nh_rdst, did_rsc);
  2232. else
  2233. goto drop;
  2234. return;
  2235. drop:
  2236. dev_core_stats_tx_dropped_inc(dev);
  2237. vxlan_vnifilter_count(netdev_priv(dev), vni, NULL,
  2238. VXLAN_VNI_STATS_TX_DROPS, 0);
  2239. dev_kfree_skb(skb);
  2240. }
  2241. static netdev_tx_t vxlan_xmit_nhid(struct sk_buff *skb, struct net_device *dev,
  2242. u32 nhid, __be32 vni)
  2243. {
  2244. struct vxlan_dev *vxlan = netdev_priv(dev);
  2245. struct vxlan_rdst nh_rdst;
  2246. struct nexthop *nh;
  2247. bool do_xmit;
  2248. u32 hash;
  2249. memset(&nh_rdst, 0, sizeof(struct vxlan_rdst));
  2250. hash = skb_get_hash(skb);
  2251. rcu_read_lock();
  2252. nh = nexthop_find_by_id(dev_net(dev), nhid);
  2253. if (unlikely(!nh || !nexthop_is_fdb(nh) || !nexthop_is_multipath(nh))) {
  2254. rcu_read_unlock();
  2255. goto drop;
  2256. }
  2257. do_xmit = vxlan_fdb_nh_path_select(nh, hash, &nh_rdst);
  2258. rcu_read_unlock();
  2259. if (vxlan->cfg.saddr.sa.sa_family != nh_rdst.remote_ip.sa.sa_family)
  2260. goto drop;
  2261. if (likely(do_xmit))
  2262. vxlan_xmit_one(skb, dev, vni, &nh_rdst, false);
  2263. else
  2264. goto drop;
  2265. return NETDEV_TX_OK;
  2266. drop:
  2267. dev_core_stats_tx_dropped_inc(dev);
  2268. vxlan_vnifilter_count(netdev_priv(dev), vni, NULL,
  2269. VXLAN_VNI_STATS_TX_DROPS, 0);
  2270. dev_kfree_skb(skb);
  2271. return NETDEV_TX_OK;
  2272. }
  2273. /* Transmit local packets over Vxlan
  2274. *
  2275. * Outer IP header inherits ECN and DF from inner header.
  2276. * Outer UDP destination is the VXLAN assigned port.
  2277. * source port is based on hash of flow
  2278. */
  2279. static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
  2280. {
  2281. struct vxlan_dev *vxlan = netdev_priv(dev);
  2282. struct vxlan_rdst *rdst, *fdst = NULL;
  2283. const struct ip_tunnel_info *info;
  2284. struct vxlan_fdb *f;
  2285. struct ethhdr *eth;
  2286. __be32 vni = 0;
  2287. u32 nhid = 0;
  2288. bool did_rsc;
  2289. info = skb_tunnel_info(skb);
  2290. skb_reset_mac_header(skb);
  2291. if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) {
  2292. if (info && info->mode & IP_TUNNEL_INFO_BRIDGE &&
  2293. info->mode & IP_TUNNEL_INFO_TX) {
  2294. vni = tunnel_id_to_key32(info->key.tun_id);
  2295. nhid = info->key.nhid;
  2296. } else {
  2297. if (info && info->mode & IP_TUNNEL_INFO_TX)
  2298. vxlan_xmit_one(skb, dev, vni, NULL, false);
  2299. else
  2300. kfree_skb_reason(skb, SKB_DROP_REASON_TUNNEL_TXINFO);
  2301. return NETDEV_TX_OK;
  2302. }
  2303. }
  2304. if (vxlan->cfg.flags & VXLAN_F_PROXY) {
  2305. eth = eth_hdr(skb);
  2306. if (ntohs(eth->h_proto) == ETH_P_ARP)
  2307. return arp_reduce(dev, skb, vni);
  2308. #if IS_ENABLED(CONFIG_IPV6)
  2309. else if (ntohs(eth->h_proto) == ETH_P_IPV6 &&
  2310. pskb_may_pull(skb, sizeof(struct ipv6hdr) +
  2311. sizeof(struct nd_msg)) &&
  2312. ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
  2313. struct nd_msg *m = (struct nd_msg *)(ipv6_hdr(skb) + 1);
  2314. if (m->icmph.icmp6_code == 0 &&
  2315. m->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
  2316. return neigh_reduce(dev, skb, vni);
  2317. }
  2318. #endif
  2319. }
  2320. if (nhid)
  2321. return vxlan_xmit_nhid(skb, dev, nhid, vni);
  2322. if (vxlan->cfg.flags & VXLAN_F_MDB) {
  2323. struct vxlan_mdb_entry *mdb_entry;
  2324. rcu_read_lock();
  2325. mdb_entry = vxlan_mdb_entry_skb_get(vxlan, skb, vni);
  2326. if (mdb_entry) {
  2327. netdev_tx_t ret;
  2328. ret = vxlan_mdb_xmit(vxlan, mdb_entry, skb);
  2329. rcu_read_unlock();
  2330. return ret;
  2331. }
  2332. rcu_read_unlock();
  2333. }
  2334. eth = eth_hdr(skb);
  2335. rcu_read_lock();
  2336. f = vxlan_find_mac_tx(vxlan, eth->h_dest, vni);
  2337. did_rsc = false;
  2338. if (f && (f->flags & NTF_ROUTER) && (vxlan->cfg.flags & VXLAN_F_RSC) &&
  2339. (ntohs(eth->h_proto) == ETH_P_IP ||
  2340. ntohs(eth->h_proto) == ETH_P_IPV6)) {
  2341. did_rsc = route_shortcircuit(dev, skb);
  2342. if (did_rsc)
  2343. f = vxlan_find_mac_tx(vxlan, eth->h_dest, vni);
  2344. }
  2345. if (f == NULL) {
  2346. f = vxlan_find_mac_tx(vxlan, all_zeros_mac, vni);
  2347. if (f == NULL) {
  2348. if ((vxlan->cfg.flags & VXLAN_F_L2MISS) &&
  2349. !is_multicast_ether_addr(eth->h_dest))
  2350. vxlan_fdb_miss(vxlan, eth->h_dest);
  2351. dev_core_stats_tx_dropped_inc(dev);
  2352. vxlan_vnifilter_count(vxlan, vni, NULL,
  2353. VXLAN_VNI_STATS_TX_DROPS, 0);
  2354. kfree_skb_reason(skb, SKB_DROP_REASON_NO_TX_TARGET);
  2355. goto out;
  2356. }
  2357. }
  2358. if (rcu_access_pointer(f->nh)) {
  2359. vxlan_xmit_nh(skb, dev, f,
  2360. (vni ? : vxlan->default_dst.remote_vni), did_rsc);
  2361. } else {
  2362. list_for_each_entry_rcu(rdst, &f->remotes, list) {
  2363. struct sk_buff *skb1;
  2364. if (!fdst) {
  2365. fdst = rdst;
  2366. continue;
  2367. }
  2368. skb1 = skb_clone(skb, GFP_ATOMIC);
  2369. if (skb1)
  2370. vxlan_xmit_one(skb1, dev, vni, rdst, did_rsc);
  2371. }
  2372. if (fdst)
  2373. vxlan_xmit_one(skb, dev, vni, fdst, did_rsc);
  2374. else
  2375. kfree_skb_reason(skb, SKB_DROP_REASON_NO_TX_TARGET);
  2376. }
  2377. out:
  2378. rcu_read_unlock();
  2379. return NETDEV_TX_OK;
  2380. }
  2381. /* Walk the forwarding table and purge stale entries */
  2382. static void vxlan_cleanup(struct timer_list *t)
  2383. {
  2384. struct vxlan_dev *vxlan = from_timer(vxlan, t, age_timer);
  2385. unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
  2386. unsigned int h;
  2387. if (!netif_running(vxlan->dev))
  2388. return;
  2389. for (h = 0; h < FDB_HASH_SIZE; ++h) {
  2390. struct hlist_node *p, *n;
  2391. spin_lock(&vxlan->hash_lock[h]);
  2392. hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
  2393. struct vxlan_fdb *f
  2394. = container_of(p, struct vxlan_fdb, hlist);
  2395. unsigned long timeout;
  2396. if (f->state & (NUD_PERMANENT | NUD_NOARP))
  2397. continue;
  2398. if (f->flags & NTF_EXT_LEARNED)
  2399. continue;
  2400. timeout = READ_ONCE(f->used) + vxlan->cfg.age_interval * HZ;
  2401. if (time_before_eq(timeout, jiffies)) {
  2402. netdev_dbg(vxlan->dev,
  2403. "garbage collect %pM\n",
  2404. f->eth_addr);
  2405. f->state = NUD_STALE;
  2406. vxlan_fdb_destroy(vxlan, f, true, true);
  2407. } else if (time_before(timeout, next_timer))
  2408. next_timer = timeout;
  2409. }
  2410. spin_unlock(&vxlan->hash_lock[h]);
  2411. }
  2412. mod_timer(&vxlan->age_timer, next_timer);
  2413. }
  2414. static void vxlan_vs_del_dev(struct vxlan_dev *vxlan)
  2415. {
  2416. struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
  2417. spin_lock(&vn->sock_lock);
  2418. hlist_del_init_rcu(&vxlan->hlist4.hlist);
  2419. #if IS_ENABLED(CONFIG_IPV6)
  2420. hlist_del_init_rcu(&vxlan->hlist6.hlist);
  2421. #endif
  2422. spin_unlock(&vn->sock_lock);
  2423. }
  2424. static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan,
  2425. struct vxlan_dev_node *node)
  2426. {
  2427. struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
  2428. __be32 vni = vxlan->default_dst.remote_vni;
  2429. node->vxlan = vxlan;
  2430. spin_lock(&vn->sock_lock);
  2431. hlist_add_head_rcu(&node->hlist, vni_head(vs, vni));
  2432. spin_unlock(&vn->sock_lock);
  2433. }
  2434. /* Setup stats when device is created */
  2435. static int vxlan_init(struct net_device *dev)
  2436. {
  2437. struct vxlan_dev *vxlan = netdev_priv(dev);
  2438. int err;
  2439. if (vxlan->cfg.flags & VXLAN_F_VNIFILTER) {
  2440. err = vxlan_vnigroup_init(vxlan);
  2441. if (err)
  2442. return err;
  2443. }
  2444. err = gro_cells_init(&vxlan->gro_cells, dev);
  2445. if (err)
  2446. goto err_vnigroup_uninit;
  2447. err = vxlan_mdb_init(vxlan);
  2448. if (err)
  2449. goto err_gro_cells_destroy;
  2450. netdev_lockdep_set_classes(dev);
  2451. return 0;
  2452. err_gro_cells_destroy:
  2453. gro_cells_destroy(&vxlan->gro_cells);
  2454. err_vnigroup_uninit:
  2455. if (vxlan->cfg.flags & VXLAN_F_VNIFILTER)
  2456. vxlan_vnigroup_uninit(vxlan);
  2457. return err;
  2458. }
  2459. static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan, __be32 vni)
  2460. {
  2461. struct vxlan_fdb *f;
  2462. u32 hash_index = fdb_head_index(vxlan, all_zeros_mac, vni);
  2463. spin_lock_bh(&vxlan->hash_lock[hash_index]);
  2464. f = __vxlan_find_mac(vxlan, all_zeros_mac, vni);
  2465. if (f)
  2466. vxlan_fdb_destroy(vxlan, f, true, true);
  2467. spin_unlock_bh(&vxlan->hash_lock[hash_index]);
  2468. }
  2469. static void vxlan_uninit(struct net_device *dev)
  2470. {
  2471. struct vxlan_dev *vxlan = netdev_priv(dev);
  2472. vxlan_mdb_fini(vxlan);
  2473. if (vxlan->cfg.flags & VXLAN_F_VNIFILTER)
  2474. vxlan_vnigroup_uninit(vxlan);
  2475. gro_cells_destroy(&vxlan->gro_cells);
  2476. vxlan_fdb_delete_default(vxlan, vxlan->cfg.vni);
  2477. }
  2478. /* Start ageing timer and join group when device is brought up */
  2479. static int vxlan_open(struct net_device *dev)
  2480. {
  2481. struct vxlan_dev *vxlan = netdev_priv(dev);
  2482. int ret;
  2483. ret = vxlan_sock_add(vxlan);
  2484. if (ret < 0)
  2485. return ret;
  2486. ret = vxlan_multicast_join(vxlan);
  2487. if (ret) {
  2488. vxlan_sock_release(vxlan);
  2489. return ret;
  2490. }
  2491. if (vxlan->cfg.age_interval)
  2492. mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
  2493. return ret;
  2494. }
  2495. struct vxlan_fdb_flush_desc {
  2496. bool ignore_default_entry;
  2497. unsigned long state;
  2498. unsigned long state_mask;
  2499. unsigned long flags;
  2500. unsigned long flags_mask;
  2501. __be32 src_vni;
  2502. u32 nhid;
  2503. __be32 vni;
  2504. __be16 port;
  2505. union vxlan_addr dst_ip;
  2506. };
  2507. static bool vxlan_fdb_is_default_entry(const struct vxlan_fdb *f,
  2508. const struct vxlan_dev *vxlan)
  2509. {
  2510. return is_zero_ether_addr(f->eth_addr) && f->vni == vxlan->cfg.vni;
  2511. }
  2512. static bool vxlan_fdb_nhid_matches(const struct vxlan_fdb *f, u32 nhid)
  2513. {
  2514. struct nexthop *nh = rtnl_dereference(f->nh);
  2515. return nh && nh->id == nhid;
  2516. }
  2517. static bool vxlan_fdb_flush_matches(const struct vxlan_fdb *f,
  2518. const struct vxlan_dev *vxlan,
  2519. const struct vxlan_fdb_flush_desc *desc)
  2520. {
  2521. if (desc->state_mask && (f->state & desc->state_mask) != desc->state)
  2522. return false;
  2523. if (desc->flags_mask && (f->flags & desc->flags_mask) != desc->flags)
  2524. return false;
  2525. if (desc->ignore_default_entry && vxlan_fdb_is_default_entry(f, vxlan))
  2526. return false;
  2527. if (desc->src_vni && f->vni != desc->src_vni)
  2528. return false;
  2529. if (desc->nhid && !vxlan_fdb_nhid_matches(f, desc->nhid))
  2530. return false;
  2531. return true;
  2532. }
  2533. static bool
  2534. vxlan_fdb_flush_should_match_remotes(const struct vxlan_fdb_flush_desc *desc)
  2535. {
  2536. return desc->vni || desc->port || desc->dst_ip.sa.sa_family;
  2537. }
  2538. static bool
  2539. vxlan_fdb_flush_remote_matches(const struct vxlan_fdb_flush_desc *desc,
  2540. const struct vxlan_rdst *rd)
  2541. {
  2542. if (desc->vni && rd->remote_vni != desc->vni)
  2543. return false;
  2544. if (desc->port && rd->remote_port != desc->port)
  2545. return false;
  2546. if (desc->dst_ip.sa.sa_family &&
  2547. !vxlan_addr_equal(&rd->remote_ip, &desc->dst_ip))
  2548. return false;
  2549. return true;
  2550. }
  2551. static void
  2552. vxlan_fdb_flush_match_remotes(struct vxlan_fdb *f, struct vxlan_dev *vxlan,
  2553. const struct vxlan_fdb_flush_desc *desc,
  2554. bool *p_destroy_fdb)
  2555. {
  2556. bool remotes_flushed = false;
  2557. struct vxlan_rdst *rd, *tmp;
  2558. list_for_each_entry_safe(rd, tmp, &f->remotes, list) {
  2559. if (!vxlan_fdb_flush_remote_matches(desc, rd))
  2560. continue;
  2561. vxlan_fdb_dst_destroy(vxlan, f, rd, true);
  2562. remotes_flushed = true;
  2563. }
  2564. *p_destroy_fdb = remotes_flushed && list_empty(&f->remotes);
  2565. }
  2566. /* Purge the forwarding table */
  2567. static void vxlan_flush(struct vxlan_dev *vxlan,
  2568. const struct vxlan_fdb_flush_desc *desc)
  2569. {
  2570. bool match_remotes = vxlan_fdb_flush_should_match_remotes(desc);
  2571. unsigned int h;
  2572. for (h = 0; h < FDB_HASH_SIZE; ++h) {
  2573. struct hlist_node *p, *n;
  2574. spin_lock_bh(&vxlan->hash_lock[h]);
  2575. hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
  2576. struct vxlan_fdb *f
  2577. = container_of(p, struct vxlan_fdb, hlist);
  2578. if (!vxlan_fdb_flush_matches(f, vxlan, desc))
  2579. continue;
  2580. if (match_remotes) {
  2581. bool destroy_fdb = false;
  2582. vxlan_fdb_flush_match_remotes(f, vxlan, desc,
  2583. &destroy_fdb);
  2584. if (!destroy_fdb)
  2585. continue;
  2586. }
  2587. vxlan_fdb_destroy(vxlan, f, true, true);
  2588. }
  2589. spin_unlock_bh(&vxlan->hash_lock[h]);
  2590. }
  2591. }
  2592. static const struct nla_policy vxlan_del_bulk_policy[NDA_MAX + 1] = {
  2593. [NDA_SRC_VNI] = { .type = NLA_U32 },
  2594. [NDA_NH_ID] = { .type = NLA_U32 },
  2595. [NDA_VNI] = { .type = NLA_U32 },
  2596. [NDA_PORT] = { .type = NLA_U16 },
  2597. [NDA_DST] = NLA_POLICY_RANGE(NLA_BINARY, sizeof(struct in_addr),
  2598. sizeof(struct in6_addr)),
  2599. [NDA_NDM_STATE_MASK] = { .type = NLA_U16 },
  2600. [NDA_NDM_FLAGS_MASK] = { .type = NLA_U8 },
  2601. };
  2602. #define VXLAN_FDB_FLUSH_IGNORED_NDM_FLAGS (NTF_MASTER | NTF_SELF)
  2603. #define VXLAN_FDB_FLUSH_ALLOWED_NDM_STATES (NUD_PERMANENT | NUD_NOARP)
  2604. #define VXLAN_FDB_FLUSH_ALLOWED_NDM_FLAGS (NTF_EXT_LEARNED | NTF_OFFLOADED | \
  2605. NTF_ROUTER)
  2606. static int vxlan_fdb_delete_bulk(struct nlmsghdr *nlh, struct net_device *dev,
  2607. struct netlink_ext_ack *extack)
  2608. {
  2609. struct vxlan_dev *vxlan = netdev_priv(dev);
  2610. struct vxlan_fdb_flush_desc desc = {};
  2611. struct ndmsg *ndm = nlmsg_data(nlh);
  2612. struct nlattr *tb[NDA_MAX + 1];
  2613. u8 ndm_flags;
  2614. int err;
  2615. ndm_flags = ndm->ndm_flags & ~VXLAN_FDB_FLUSH_IGNORED_NDM_FLAGS;
  2616. err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, vxlan_del_bulk_policy,
  2617. extack);
  2618. if (err)
  2619. return err;
  2620. if (ndm_flags & ~VXLAN_FDB_FLUSH_ALLOWED_NDM_FLAGS) {
  2621. NL_SET_ERR_MSG(extack, "Unsupported fdb flush ndm flag bits set");
  2622. return -EINVAL;
  2623. }
  2624. if (ndm->ndm_state & ~VXLAN_FDB_FLUSH_ALLOWED_NDM_STATES) {
  2625. NL_SET_ERR_MSG(extack, "Unsupported fdb flush ndm state bits set");
  2626. return -EINVAL;
  2627. }
  2628. desc.state = ndm->ndm_state;
  2629. desc.flags = ndm_flags;
  2630. if (tb[NDA_NDM_STATE_MASK])
  2631. desc.state_mask = nla_get_u16(tb[NDA_NDM_STATE_MASK]);
  2632. if (tb[NDA_NDM_FLAGS_MASK])
  2633. desc.flags_mask = nla_get_u8(tb[NDA_NDM_FLAGS_MASK]);
  2634. if (tb[NDA_SRC_VNI])
  2635. desc.src_vni = cpu_to_be32(nla_get_u32(tb[NDA_SRC_VNI]));
  2636. if (tb[NDA_NH_ID])
  2637. desc.nhid = nla_get_u32(tb[NDA_NH_ID]);
  2638. if (tb[NDA_VNI])
  2639. desc.vni = cpu_to_be32(nla_get_u32(tb[NDA_VNI]));
  2640. if (tb[NDA_PORT])
  2641. desc.port = nla_get_be16(tb[NDA_PORT]);
  2642. if (tb[NDA_DST]) {
  2643. union vxlan_addr ip;
  2644. err = vxlan_nla_get_addr(&ip, tb[NDA_DST]);
  2645. if (err) {
  2646. NL_SET_ERR_MSG_ATTR(extack, tb[NDA_DST],
  2647. "Unsupported address family");
  2648. return err;
  2649. }
  2650. desc.dst_ip = ip;
  2651. }
  2652. vxlan_flush(vxlan, &desc);
  2653. return 0;
  2654. }
  2655. /* Cleanup timer and forwarding table on shutdown */
  2656. static int vxlan_stop(struct net_device *dev)
  2657. {
  2658. struct vxlan_dev *vxlan = netdev_priv(dev);
  2659. struct vxlan_fdb_flush_desc desc = {
  2660. /* Default entry is deleted at vxlan_uninit. */
  2661. .ignore_default_entry = true,
  2662. .state = 0,
  2663. .state_mask = NUD_PERMANENT | NUD_NOARP,
  2664. };
  2665. vxlan_multicast_leave(vxlan);
  2666. del_timer_sync(&vxlan->age_timer);
  2667. vxlan_flush(vxlan, &desc);
  2668. vxlan_sock_release(vxlan);
  2669. return 0;
  2670. }
  2671. /* Stub, nothing needs to be done. */
  2672. static void vxlan_set_multicast_list(struct net_device *dev)
  2673. {
  2674. }
  2675. static int vxlan_change_mtu(struct net_device *dev, int new_mtu)
  2676. {
  2677. struct vxlan_dev *vxlan = netdev_priv(dev);
  2678. struct vxlan_rdst *dst = &vxlan->default_dst;
  2679. struct net_device *lowerdev = __dev_get_by_index(vxlan->net,
  2680. dst->remote_ifindex);
  2681. /* This check is different than dev->max_mtu, because it looks at
  2682. * the lowerdev->mtu, rather than the static dev->max_mtu
  2683. */
  2684. if (lowerdev) {
  2685. int max_mtu = lowerdev->mtu - vxlan_headroom(vxlan->cfg.flags);
  2686. if (new_mtu > max_mtu)
  2687. return -EINVAL;
  2688. }
  2689. WRITE_ONCE(dev->mtu, new_mtu);
  2690. return 0;
  2691. }
  2692. static int vxlan_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
  2693. {
  2694. struct vxlan_dev *vxlan = netdev_priv(dev);
  2695. struct ip_tunnel_info *info = skb_tunnel_info(skb);
  2696. __be16 sport, dport;
  2697. sport = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
  2698. vxlan->cfg.port_max, true);
  2699. dport = info->key.tp_dst ? : vxlan->cfg.dst_port;
  2700. if (ip_tunnel_info_af(info) == AF_INET) {
  2701. struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock);
  2702. struct rtable *rt;
  2703. if (!sock4)
  2704. return -EIO;
  2705. rt = udp_tunnel_dst_lookup(skb, dev, vxlan->net, 0,
  2706. &info->key.u.ipv4.src,
  2707. &info->key,
  2708. sport, dport, info->key.tos,
  2709. &info->dst_cache);
  2710. if (IS_ERR(rt))
  2711. return PTR_ERR(rt);
  2712. ip_rt_put(rt);
  2713. } else {
  2714. #if IS_ENABLED(CONFIG_IPV6)
  2715. struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock);
  2716. struct dst_entry *ndst;
  2717. if (!sock6)
  2718. return -EIO;
  2719. ndst = udp_tunnel6_dst_lookup(skb, dev, vxlan->net, sock6->sock,
  2720. 0, &info->key.u.ipv6.src,
  2721. &info->key,
  2722. sport, dport, info->key.tos,
  2723. &info->dst_cache);
  2724. if (IS_ERR(ndst))
  2725. return PTR_ERR(ndst);
  2726. dst_release(ndst);
  2727. #else /* !CONFIG_IPV6 */
  2728. return -EPFNOSUPPORT;
  2729. #endif
  2730. }
  2731. info->key.tp_src = sport;
  2732. info->key.tp_dst = dport;
  2733. return 0;
  2734. }
  2735. static const struct net_device_ops vxlan_netdev_ether_ops = {
  2736. .ndo_init = vxlan_init,
  2737. .ndo_uninit = vxlan_uninit,
  2738. .ndo_open = vxlan_open,
  2739. .ndo_stop = vxlan_stop,
  2740. .ndo_start_xmit = vxlan_xmit,
  2741. .ndo_set_rx_mode = vxlan_set_multicast_list,
  2742. .ndo_change_mtu = vxlan_change_mtu,
  2743. .ndo_validate_addr = eth_validate_addr,
  2744. .ndo_set_mac_address = eth_mac_addr,
  2745. .ndo_fdb_add = vxlan_fdb_add,
  2746. .ndo_fdb_del = vxlan_fdb_delete,
  2747. .ndo_fdb_del_bulk = vxlan_fdb_delete_bulk,
  2748. .ndo_fdb_dump = vxlan_fdb_dump,
  2749. .ndo_fdb_get = vxlan_fdb_get,
  2750. .ndo_mdb_add = vxlan_mdb_add,
  2751. .ndo_mdb_del = vxlan_mdb_del,
  2752. .ndo_mdb_del_bulk = vxlan_mdb_del_bulk,
  2753. .ndo_mdb_dump = vxlan_mdb_dump,
  2754. .ndo_mdb_get = vxlan_mdb_get,
  2755. .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
  2756. };
  2757. static const struct net_device_ops vxlan_netdev_raw_ops = {
  2758. .ndo_init = vxlan_init,
  2759. .ndo_uninit = vxlan_uninit,
  2760. .ndo_open = vxlan_open,
  2761. .ndo_stop = vxlan_stop,
  2762. .ndo_start_xmit = vxlan_xmit,
  2763. .ndo_change_mtu = vxlan_change_mtu,
  2764. .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
  2765. };
  2766. /* Info for udev, that this is a virtual tunnel endpoint */
  2767. static const struct device_type vxlan_type = {
  2768. .name = "vxlan",
  2769. };
  2770. /* Calls the ndo_udp_tunnel_add of the caller in order to
  2771. * supply the listening VXLAN udp ports. Callers are expected
  2772. * to implement the ndo_udp_tunnel_add.
  2773. */
  2774. static void vxlan_offload_rx_ports(struct net_device *dev, bool push)
  2775. {
  2776. struct vxlan_sock *vs;
  2777. struct net *net = dev_net(dev);
  2778. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  2779. unsigned int i;
  2780. spin_lock(&vn->sock_lock);
  2781. for (i = 0; i < PORT_HASH_SIZE; ++i) {
  2782. hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist) {
  2783. unsigned short type;
  2784. if (vs->flags & VXLAN_F_GPE)
  2785. type = UDP_TUNNEL_TYPE_VXLAN_GPE;
  2786. else
  2787. type = UDP_TUNNEL_TYPE_VXLAN;
  2788. if (push)
  2789. udp_tunnel_push_rx_port(dev, vs->sock, type);
  2790. else
  2791. udp_tunnel_drop_rx_port(dev, vs->sock, type);
  2792. }
  2793. }
  2794. spin_unlock(&vn->sock_lock);
  2795. }
  2796. /* Initialize the device structure. */
  2797. static void vxlan_setup(struct net_device *dev)
  2798. {
  2799. struct vxlan_dev *vxlan = netdev_priv(dev);
  2800. unsigned int h;
  2801. eth_hw_addr_random(dev);
  2802. ether_setup(dev);
  2803. dev->needs_free_netdev = true;
  2804. SET_NETDEV_DEVTYPE(dev, &vxlan_type);
  2805. dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_FRAGLIST;
  2806. dev->features |= NETIF_F_RXCSUM;
  2807. dev->features |= NETIF_F_GSO_SOFTWARE;
  2808. dev->vlan_features = dev->features;
  2809. dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_FRAGLIST;
  2810. dev->hw_features |= NETIF_F_RXCSUM;
  2811. dev->hw_features |= NETIF_F_GSO_SOFTWARE;
  2812. netif_keep_dst(dev);
  2813. dev->priv_flags |= IFF_NO_QUEUE;
  2814. dev->change_proto_down = true;
  2815. dev->lltx = true;
  2816. /* MTU range: 68 - 65535 */
  2817. dev->min_mtu = ETH_MIN_MTU;
  2818. dev->max_mtu = ETH_MAX_MTU;
  2819. dev->pcpu_stat_type = NETDEV_PCPU_STAT_TSTATS;
  2820. INIT_LIST_HEAD(&vxlan->next);
  2821. timer_setup(&vxlan->age_timer, vxlan_cleanup, TIMER_DEFERRABLE);
  2822. vxlan->dev = dev;
  2823. for (h = 0; h < FDB_HASH_SIZE; ++h) {
  2824. spin_lock_init(&vxlan->hash_lock[h]);
  2825. INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
  2826. }
  2827. }
  2828. static void vxlan_ether_setup(struct net_device *dev)
  2829. {
  2830. dev->priv_flags &= ~IFF_TX_SKB_SHARING;
  2831. dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
  2832. dev->netdev_ops = &vxlan_netdev_ether_ops;
  2833. }
  2834. static void vxlan_raw_setup(struct net_device *dev)
  2835. {
  2836. dev->header_ops = NULL;
  2837. dev->type = ARPHRD_NONE;
  2838. dev->hard_header_len = 0;
  2839. dev->addr_len = 0;
  2840. dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
  2841. dev->netdev_ops = &vxlan_netdev_raw_ops;
  2842. }
  2843. static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
  2844. [IFLA_VXLAN_UNSPEC] = { .strict_start_type = IFLA_VXLAN_LOCALBYPASS },
  2845. [IFLA_VXLAN_ID] = { .type = NLA_U32 },
  2846. [IFLA_VXLAN_GROUP] = { .len = sizeof_field(struct iphdr, daddr) },
  2847. [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
  2848. [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
  2849. [IFLA_VXLAN_LOCAL] = { .len = sizeof_field(struct iphdr, saddr) },
  2850. [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
  2851. [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
  2852. [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
  2853. [IFLA_VXLAN_LABEL] = { .type = NLA_U32 },
  2854. [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
  2855. [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
  2856. [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
  2857. [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
  2858. [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
  2859. [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
  2860. [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
  2861. [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
  2862. [IFLA_VXLAN_COLLECT_METADATA] = { .type = NLA_U8 },
  2863. [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
  2864. [IFLA_VXLAN_UDP_CSUM] = { .type = NLA_U8 },
  2865. [IFLA_VXLAN_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
  2866. [IFLA_VXLAN_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
  2867. [IFLA_VXLAN_REMCSUM_TX] = { .type = NLA_U8 },
  2868. [IFLA_VXLAN_REMCSUM_RX] = { .type = NLA_U8 },
  2869. [IFLA_VXLAN_GBP] = { .type = NLA_FLAG, },
  2870. [IFLA_VXLAN_GPE] = { .type = NLA_FLAG, },
  2871. [IFLA_VXLAN_REMCSUM_NOPARTIAL] = { .type = NLA_FLAG },
  2872. [IFLA_VXLAN_TTL_INHERIT] = { .type = NLA_FLAG },
  2873. [IFLA_VXLAN_DF] = { .type = NLA_U8 },
  2874. [IFLA_VXLAN_VNIFILTER] = { .type = NLA_U8 },
  2875. [IFLA_VXLAN_LOCALBYPASS] = NLA_POLICY_MAX(NLA_U8, 1),
  2876. [IFLA_VXLAN_LABEL_POLICY] = NLA_POLICY_MAX(NLA_U32, VXLAN_LABEL_MAX),
  2877. };
  2878. static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[],
  2879. struct netlink_ext_ack *extack)
  2880. {
  2881. if (tb[IFLA_ADDRESS]) {
  2882. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
  2883. NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
  2884. "Provided link layer address is not Ethernet");
  2885. return -EINVAL;
  2886. }
  2887. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
  2888. NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
  2889. "Provided Ethernet address is not unicast");
  2890. return -EADDRNOTAVAIL;
  2891. }
  2892. }
  2893. if (tb[IFLA_MTU]) {
  2894. u32 mtu = nla_get_u32(tb[IFLA_MTU]);
  2895. if (mtu < ETH_MIN_MTU || mtu > ETH_MAX_MTU) {
  2896. NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_MTU],
  2897. "MTU must be between 68 and 65535");
  2898. return -EINVAL;
  2899. }
  2900. }
  2901. if (!data) {
  2902. NL_SET_ERR_MSG(extack,
  2903. "Required attributes not provided to perform the operation");
  2904. return -EINVAL;
  2905. }
  2906. if (data[IFLA_VXLAN_ID]) {
  2907. u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
  2908. if (id >= VXLAN_N_VID) {
  2909. NL_SET_ERR_MSG_ATTR(extack, data[IFLA_VXLAN_ID],
  2910. "VXLAN ID must be lower than 16777216");
  2911. return -ERANGE;
  2912. }
  2913. }
  2914. if (data[IFLA_VXLAN_PORT_RANGE]) {
  2915. const struct ifla_vxlan_port_range *p
  2916. = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
  2917. if (ntohs(p->high) < ntohs(p->low)) {
  2918. NL_SET_ERR_MSG_ATTR(extack, data[IFLA_VXLAN_PORT_RANGE],
  2919. "Invalid source port range");
  2920. return -EINVAL;
  2921. }
  2922. }
  2923. if (data[IFLA_VXLAN_DF]) {
  2924. enum ifla_vxlan_df df = nla_get_u8(data[IFLA_VXLAN_DF]);
  2925. if (df < 0 || df > VXLAN_DF_MAX) {
  2926. NL_SET_ERR_MSG_ATTR(extack, data[IFLA_VXLAN_DF],
  2927. "Invalid DF attribute");
  2928. return -EINVAL;
  2929. }
  2930. }
  2931. return 0;
  2932. }
  2933. static void vxlan_get_drvinfo(struct net_device *netdev,
  2934. struct ethtool_drvinfo *drvinfo)
  2935. {
  2936. strscpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
  2937. strscpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
  2938. }
  2939. static int vxlan_get_link_ksettings(struct net_device *dev,
  2940. struct ethtool_link_ksettings *cmd)
  2941. {
  2942. struct vxlan_dev *vxlan = netdev_priv(dev);
  2943. struct vxlan_rdst *dst = &vxlan->default_dst;
  2944. struct net_device *lowerdev = __dev_get_by_index(vxlan->net,
  2945. dst->remote_ifindex);
  2946. if (!lowerdev) {
  2947. cmd->base.duplex = DUPLEX_UNKNOWN;
  2948. cmd->base.port = PORT_OTHER;
  2949. cmd->base.speed = SPEED_UNKNOWN;
  2950. return 0;
  2951. }
  2952. return __ethtool_get_link_ksettings(lowerdev, cmd);
  2953. }
  2954. static const struct ethtool_ops vxlan_ethtool_ops = {
  2955. .get_drvinfo = vxlan_get_drvinfo,
  2956. .get_link = ethtool_op_get_link,
  2957. .get_link_ksettings = vxlan_get_link_ksettings,
  2958. };
  2959. static struct socket *vxlan_create_sock(struct net *net, bool ipv6,
  2960. __be16 port, u32 flags, int ifindex)
  2961. {
  2962. struct socket *sock;
  2963. struct udp_port_cfg udp_conf;
  2964. int err;
  2965. memset(&udp_conf, 0, sizeof(udp_conf));
  2966. if (ipv6) {
  2967. udp_conf.family = AF_INET6;
  2968. udp_conf.use_udp6_rx_checksums =
  2969. !(flags & VXLAN_F_UDP_ZERO_CSUM6_RX);
  2970. udp_conf.ipv6_v6only = 1;
  2971. } else {
  2972. udp_conf.family = AF_INET;
  2973. }
  2974. udp_conf.local_udp_port = port;
  2975. udp_conf.bind_ifindex = ifindex;
  2976. /* Open UDP socket */
  2977. err = udp_sock_create(net, &udp_conf, &sock);
  2978. if (err < 0)
  2979. return ERR_PTR(err);
  2980. udp_allow_gso(sock->sk);
  2981. return sock;
  2982. }
  2983. /* Create new listen socket if needed */
  2984. static struct vxlan_sock *vxlan_socket_create(struct net *net, bool ipv6,
  2985. __be16 port, u32 flags,
  2986. int ifindex)
  2987. {
  2988. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  2989. struct vxlan_sock *vs;
  2990. struct socket *sock;
  2991. unsigned int h;
  2992. struct udp_tunnel_sock_cfg tunnel_cfg;
  2993. vs = kzalloc(sizeof(*vs), GFP_KERNEL);
  2994. if (!vs)
  2995. return ERR_PTR(-ENOMEM);
  2996. for (h = 0; h < VNI_HASH_SIZE; ++h)
  2997. INIT_HLIST_HEAD(&vs->vni_list[h]);
  2998. sock = vxlan_create_sock(net, ipv6, port, flags, ifindex);
  2999. if (IS_ERR(sock)) {
  3000. kfree(vs);
  3001. return ERR_CAST(sock);
  3002. }
  3003. vs->sock = sock;
  3004. refcount_set(&vs->refcnt, 1);
  3005. vs->flags = (flags & VXLAN_F_RCV_FLAGS);
  3006. spin_lock(&vn->sock_lock);
  3007. hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
  3008. udp_tunnel_notify_add_rx_port(sock,
  3009. (vs->flags & VXLAN_F_GPE) ?
  3010. UDP_TUNNEL_TYPE_VXLAN_GPE :
  3011. UDP_TUNNEL_TYPE_VXLAN);
  3012. spin_unlock(&vn->sock_lock);
  3013. /* Mark socket as an encapsulation socket. */
  3014. memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
  3015. tunnel_cfg.sk_user_data = vs;
  3016. tunnel_cfg.encap_type = 1;
  3017. tunnel_cfg.encap_rcv = vxlan_rcv;
  3018. tunnel_cfg.encap_err_lookup = vxlan_err_lookup;
  3019. tunnel_cfg.encap_destroy = NULL;
  3020. if (vs->flags & VXLAN_F_GPE) {
  3021. tunnel_cfg.gro_receive = vxlan_gpe_gro_receive;
  3022. tunnel_cfg.gro_complete = vxlan_gpe_gro_complete;
  3023. } else {
  3024. tunnel_cfg.gro_receive = vxlan_gro_receive;
  3025. tunnel_cfg.gro_complete = vxlan_gro_complete;
  3026. }
  3027. setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
  3028. return vs;
  3029. }
  3030. static int __vxlan_sock_add(struct vxlan_dev *vxlan, bool ipv6)
  3031. {
  3032. struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
  3033. bool metadata = vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA;
  3034. struct vxlan_sock *vs = NULL;
  3035. struct vxlan_dev_node *node;
  3036. int l3mdev_index = 0;
  3037. if (vxlan->cfg.remote_ifindex)
  3038. l3mdev_index = l3mdev_master_upper_ifindex_by_index(
  3039. vxlan->net, vxlan->cfg.remote_ifindex);
  3040. if (!vxlan->cfg.no_share) {
  3041. spin_lock(&vn->sock_lock);
  3042. vs = vxlan_find_sock(vxlan->net, ipv6 ? AF_INET6 : AF_INET,
  3043. vxlan->cfg.dst_port, vxlan->cfg.flags,
  3044. l3mdev_index);
  3045. if (vs && !refcount_inc_not_zero(&vs->refcnt)) {
  3046. spin_unlock(&vn->sock_lock);
  3047. return -EBUSY;
  3048. }
  3049. spin_unlock(&vn->sock_lock);
  3050. }
  3051. if (!vs)
  3052. vs = vxlan_socket_create(vxlan->net, ipv6,
  3053. vxlan->cfg.dst_port, vxlan->cfg.flags,
  3054. l3mdev_index);
  3055. if (IS_ERR(vs))
  3056. return PTR_ERR(vs);
  3057. #if IS_ENABLED(CONFIG_IPV6)
  3058. if (ipv6) {
  3059. rcu_assign_pointer(vxlan->vn6_sock, vs);
  3060. node = &vxlan->hlist6;
  3061. } else
  3062. #endif
  3063. {
  3064. rcu_assign_pointer(vxlan->vn4_sock, vs);
  3065. node = &vxlan->hlist4;
  3066. }
  3067. if (metadata && (vxlan->cfg.flags & VXLAN_F_VNIFILTER))
  3068. vxlan_vs_add_vnigrp(vxlan, vs, ipv6);
  3069. else
  3070. vxlan_vs_add_dev(vs, vxlan, node);
  3071. return 0;
  3072. }
  3073. static int vxlan_sock_add(struct vxlan_dev *vxlan)
  3074. {
  3075. bool metadata = vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA;
  3076. bool ipv6 = vxlan->cfg.flags & VXLAN_F_IPV6 || metadata;
  3077. bool ipv4 = !ipv6 || metadata;
  3078. int ret = 0;
  3079. RCU_INIT_POINTER(vxlan->vn4_sock, NULL);
  3080. #if IS_ENABLED(CONFIG_IPV6)
  3081. RCU_INIT_POINTER(vxlan->vn6_sock, NULL);
  3082. if (ipv6) {
  3083. ret = __vxlan_sock_add(vxlan, true);
  3084. if (ret < 0 && ret != -EAFNOSUPPORT)
  3085. ipv4 = false;
  3086. }
  3087. #endif
  3088. if (ipv4)
  3089. ret = __vxlan_sock_add(vxlan, false);
  3090. if (ret < 0)
  3091. vxlan_sock_release(vxlan);
  3092. return ret;
  3093. }
  3094. int vxlan_vni_in_use(struct net *src_net, struct vxlan_dev *vxlan,
  3095. struct vxlan_config *conf, __be32 vni)
  3096. {
  3097. struct vxlan_net *vn = net_generic(src_net, vxlan_net_id);
  3098. struct vxlan_dev *tmp;
  3099. list_for_each_entry(tmp, &vn->vxlan_list, next) {
  3100. if (tmp == vxlan)
  3101. continue;
  3102. if (tmp->cfg.flags & VXLAN_F_VNIFILTER) {
  3103. if (!vxlan_vnifilter_lookup(tmp, vni))
  3104. continue;
  3105. } else if (tmp->cfg.vni != vni) {
  3106. continue;
  3107. }
  3108. if (tmp->cfg.dst_port != conf->dst_port)
  3109. continue;
  3110. if ((tmp->cfg.flags & (VXLAN_F_RCV_FLAGS | VXLAN_F_IPV6)) !=
  3111. (conf->flags & (VXLAN_F_RCV_FLAGS | VXLAN_F_IPV6)))
  3112. continue;
  3113. if ((conf->flags & VXLAN_F_IPV6_LINKLOCAL) &&
  3114. tmp->cfg.remote_ifindex != conf->remote_ifindex)
  3115. continue;
  3116. return -EEXIST;
  3117. }
  3118. return 0;
  3119. }
  3120. static int vxlan_config_validate(struct net *src_net, struct vxlan_config *conf,
  3121. struct net_device **lower,
  3122. struct vxlan_dev *old,
  3123. struct netlink_ext_ack *extack)
  3124. {
  3125. bool use_ipv6 = false;
  3126. if (conf->flags & VXLAN_F_GPE) {
  3127. /* For now, allow GPE only together with
  3128. * COLLECT_METADATA. This can be relaxed later; in such
  3129. * case, the other side of the PtP link will have to be
  3130. * provided.
  3131. */
  3132. if ((conf->flags & ~VXLAN_F_ALLOWED_GPE) ||
  3133. !(conf->flags & VXLAN_F_COLLECT_METADATA)) {
  3134. NL_SET_ERR_MSG(extack,
  3135. "VXLAN GPE does not support this combination of attributes");
  3136. return -EINVAL;
  3137. }
  3138. }
  3139. if (!conf->remote_ip.sa.sa_family && !conf->saddr.sa.sa_family) {
  3140. /* Unless IPv6 is explicitly requested, assume IPv4 */
  3141. conf->remote_ip.sa.sa_family = AF_INET;
  3142. conf->saddr.sa.sa_family = AF_INET;
  3143. } else if (!conf->remote_ip.sa.sa_family) {
  3144. conf->remote_ip.sa.sa_family = conf->saddr.sa.sa_family;
  3145. } else if (!conf->saddr.sa.sa_family) {
  3146. conf->saddr.sa.sa_family = conf->remote_ip.sa.sa_family;
  3147. }
  3148. if (conf->saddr.sa.sa_family != conf->remote_ip.sa.sa_family) {
  3149. NL_SET_ERR_MSG(extack,
  3150. "Local and remote address must be from the same family");
  3151. return -EINVAL;
  3152. }
  3153. if (vxlan_addr_multicast(&conf->saddr)) {
  3154. NL_SET_ERR_MSG(extack, "Local address cannot be multicast");
  3155. return -EINVAL;
  3156. }
  3157. if (conf->saddr.sa.sa_family == AF_INET6) {
  3158. if (!IS_ENABLED(CONFIG_IPV6)) {
  3159. NL_SET_ERR_MSG(extack,
  3160. "IPv6 support not enabled in the kernel");
  3161. return -EPFNOSUPPORT;
  3162. }
  3163. use_ipv6 = true;
  3164. conf->flags |= VXLAN_F_IPV6;
  3165. if (!(conf->flags & VXLAN_F_COLLECT_METADATA)) {
  3166. int local_type =
  3167. ipv6_addr_type(&conf->saddr.sin6.sin6_addr);
  3168. int remote_type =
  3169. ipv6_addr_type(&conf->remote_ip.sin6.sin6_addr);
  3170. if (local_type & IPV6_ADDR_LINKLOCAL) {
  3171. if (!(remote_type & IPV6_ADDR_LINKLOCAL) &&
  3172. (remote_type != IPV6_ADDR_ANY)) {
  3173. NL_SET_ERR_MSG(extack,
  3174. "Invalid combination of local and remote address scopes");
  3175. return -EINVAL;
  3176. }
  3177. conf->flags |= VXLAN_F_IPV6_LINKLOCAL;
  3178. } else {
  3179. if (remote_type ==
  3180. (IPV6_ADDR_UNICAST | IPV6_ADDR_LINKLOCAL)) {
  3181. NL_SET_ERR_MSG(extack,
  3182. "Invalid combination of local and remote address scopes");
  3183. return -EINVAL;
  3184. }
  3185. conf->flags &= ~VXLAN_F_IPV6_LINKLOCAL;
  3186. }
  3187. }
  3188. }
  3189. if (conf->label && !use_ipv6) {
  3190. NL_SET_ERR_MSG(extack,
  3191. "Label attribute only applies to IPv6 VXLAN devices");
  3192. return -EINVAL;
  3193. }
  3194. if (conf->label_policy && !use_ipv6) {
  3195. NL_SET_ERR_MSG(extack,
  3196. "Label policy only applies to IPv6 VXLAN devices");
  3197. return -EINVAL;
  3198. }
  3199. if (conf->remote_ifindex) {
  3200. struct net_device *lowerdev;
  3201. lowerdev = __dev_get_by_index(src_net, conf->remote_ifindex);
  3202. if (!lowerdev) {
  3203. NL_SET_ERR_MSG(extack,
  3204. "Invalid local interface, device not found");
  3205. return -ENODEV;
  3206. }
  3207. #if IS_ENABLED(CONFIG_IPV6)
  3208. if (use_ipv6) {
  3209. struct inet6_dev *idev = __in6_dev_get(lowerdev);
  3210. if (idev && idev->cnf.disable_ipv6) {
  3211. NL_SET_ERR_MSG(extack,
  3212. "IPv6 support disabled by administrator");
  3213. return -EPERM;
  3214. }
  3215. }
  3216. #endif
  3217. *lower = lowerdev;
  3218. } else {
  3219. if (vxlan_addr_multicast(&conf->remote_ip)) {
  3220. NL_SET_ERR_MSG(extack,
  3221. "Local interface required for multicast remote destination");
  3222. return -EINVAL;
  3223. }
  3224. #if IS_ENABLED(CONFIG_IPV6)
  3225. if (conf->flags & VXLAN_F_IPV6_LINKLOCAL) {
  3226. NL_SET_ERR_MSG(extack,
  3227. "Local interface required for link-local local/remote addresses");
  3228. return -EINVAL;
  3229. }
  3230. #endif
  3231. *lower = NULL;
  3232. }
  3233. if (!conf->dst_port) {
  3234. if (conf->flags & VXLAN_F_GPE)
  3235. conf->dst_port = htons(IANA_VXLAN_GPE_UDP_PORT);
  3236. else
  3237. conf->dst_port = htons(vxlan_port);
  3238. }
  3239. if (!conf->age_interval)
  3240. conf->age_interval = FDB_AGE_DEFAULT;
  3241. if (vxlan_vni_in_use(src_net, old, conf, conf->vni)) {
  3242. NL_SET_ERR_MSG(extack,
  3243. "A VXLAN device with the specified VNI already exists");
  3244. return -EEXIST;
  3245. }
  3246. return 0;
  3247. }
  3248. static void vxlan_config_apply(struct net_device *dev,
  3249. struct vxlan_config *conf,
  3250. struct net_device *lowerdev,
  3251. struct net *src_net,
  3252. bool changelink)
  3253. {
  3254. struct vxlan_dev *vxlan = netdev_priv(dev);
  3255. struct vxlan_rdst *dst = &vxlan->default_dst;
  3256. unsigned short needed_headroom = ETH_HLEN;
  3257. int max_mtu = ETH_MAX_MTU;
  3258. u32 flags = conf->flags;
  3259. if (!changelink) {
  3260. if (flags & VXLAN_F_GPE)
  3261. vxlan_raw_setup(dev);
  3262. else
  3263. vxlan_ether_setup(dev);
  3264. if (conf->mtu)
  3265. dev->mtu = conf->mtu;
  3266. vxlan->net = src_net;
  3267. }
  3268. dst->remote_vni = conf->vni;
  3269. memcpy(&dst->remote_ip, &conf->remote_ip, sizeof(conf->remote_ip));
  3270. if (lowerdev) {
  3271. dst->remote_ifindex = conf->remote_ifindex;
  3272. netif_inherit_tso_max(dev, lowerdev);
  3273. needed_headroom = lowerdev->hard_header_len;
  3274. needed_headroom += lowerdev->needed_headroom;
  3275. dev->needed_tailroom = lowerdev->needed_tailroom;
  3276. max_mtu = lowerdev->mtu - vxlan_headroom(flags);
  3277. if (max_mtu < ETH_MIN_MTU)
  3278. max_mtu = ETH_MIN_MTU;
  3279. if (!changelink && !conf->mtu)
  3280. dev->mtu = max_mtu;
  3281. }
  3282. if (dev->mtu > max_mtu)
  3283. dev->mtu = max_mtu;
  3284. if (flags & VXLAN_F_COLLECT_METADATA)
  3285. flags |= VXLAN_F_IPV6;
  3286. needed_headroom += vxlan_headroom(flags);
  3287. dev->needed_headroom = needed_headroom;
  3288. memcpy(&vxlan->cfg, conf, sizeof(*conf));
  3289. }
  3290. static int vxlan_dev_configure(struct net *src_net, struct net_device *dev,
  3291. struct vxlan_config *conf, bool changelink,
  3292. struct netlink_ext_ack *extack)
  3293. {
  3294. struct vxlan_dev *vxlan = netdev_priv(dev);
  3295. struct net_device *lowerdev;
  3296. int ret;
  3297. ret = vxlan_config_validate(src_net, conf, &lowerdev, vxlan, extack);
  3298. if (ret)
  3299. return ret;
  3300. vxlan_config_apply(dev, conf, lowerdev, src_net, changelink);
  3301. return 0;
  3302. }
  3303. static int __vxlan_dev_create(struct net *net, struct net_device *dev,
  3304. struct vxlan_config *conf,
  3305. struct netlink_ext_ack *extack)
  3306. {
  3307. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  3308. struct vxlan_dev *vxlan = netdev_priv(dev);
  3309. struct net_device *remote_dev = NULL;
  3310. struct vxlan_fdb *f = NULL;
  3311. bool unregister = false;
  3312. struct vxlan_rdst *dst;
  3313. int err;
  3314. dst = &vxlan->default_dst;
  3315. err = vxlan_dev_configure(net, dev, conf, false, extack);
  3316. if (err)
  3317. return err;
  3318. dev->ethtool_ops = &vxlan_ethtool_ops;
  3319. /* create an fdb entry for a valid default destination */
  3320. if (!vxlan_addr_any(&dst->remote_ip)) {
  3321. err = vxlan_fdb_create(vxlan, all_zeros_mac,
  3322. &dst->remote_ip,
  3323. NUD_REACHABLE | NUD_PERMANENT,
  3324. vxlan->cfg.dst_port,
  3325. dst->remote_vni,
  3326. dst->remote_vni,
  3327. dst->remote_ifindex,
  3328. NTF_SELF, 0, &f, extack);
  3329. if (err)
  3330. return err;
  3331. }
  3332. err = register_netdevice(dev);
  3333. if (err)
  3334. goto errout;
  3335. unregister = true;
  3336. if (dst->remote_ifindex) {
  3337. remote_dev = __dev_get_by_index(net, dst->remote_ifindex);
  3338. if (!remote_dev) {
  3339. err = -ENODEV;
  3340. goto errout;
  3341. }
  3342. err = netdev_upper_dev_link(remote_dev, dev, extack);
  3343. if (err)
  3344. goto errout;
  3345. }
  3346. err = rtnl_configure_link(dev, NULL, 0, NULL);
  3347. if (err < 0)
  3348. goto unlink;
  3349. if (f) {
  3350. vxlan_fdb_insert(vxlan, all_zeros_mac, dst->remote_vni, f);
  3351. /* notify default fdb entry */
  3352. err = vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f),
  3353. RTM_NEWNEIGH, true, extack);
  3354. if (err) {
  3355. vxlan_fdb_destroy(vxlan, f, false, false);
  3356. if (remote_dev)
  3357. netdev_upper_dev_unlink(remote_dev, dev);
  3358. goto unregister;
  3359. }
  3360. }
  3361. list_add(&vxlan->next, &vn->vxlan_list);
  3362. if (remote_dev)
  3363. dst->remote_dev = remote_dev;
  3364. return 0;
  3365. unlink:
  3366. if (remote_dev)
  3367. netdev_upper_dev_unlink(remote_dev, dev);
  3368. errout:
  3369. /* unregister_netdevice() destroys the default FDB entry with deletion
  3370. * notification. But the addition notification was not sent yet, so
  3371. * destroy the entry by hand here.
  3372. */
  3373. if (f)
  3374. __vxlan_fdb_free(f);
  3375. unregister:
  3376. if (unregister)
  3377. unregister_netdevice(dev);
  3378. return err;
  3379. }
  3380. /* Set/clear flags based on attribute */
  3381. static int vxlan_nl2flag(struct vxlan_config *conf, struct nlattr *tb[],
  3382. int attrtype, unsigned long mask, bool changelink,
  3383. bool changelink_supported,
  3384. struct netlink_ext_ack *extack)
  3385. {
  3386. unsigned long flags;
  3387. if (!tb[attrtype])
  3388. return 0;
  3389. if (changelink && !changelink_supported) {
  3390. vxlan_flag_attr_error(attrtype, extack);
  3391. return -EOPNOTSUPP;
  3392. }
  3393. if (vxlan_policy[attrtype].type == NLA_FLAG)
  3394. flags = conf->flags | mask;
  3395. else if (nla_get_u8(tb[attrtype]))
  3396. flags = conf->flags | mask;
  3397. else
  3398. flags = conf->flags & ~mask;
  3399. conf->flags = flags;
  3400. return 0;
  3401. }
  3402. static int vxlan_nl2conf(struct nlattr *tb[], struct nlattr *data[],
  3403. struct net_device *dev, struct vxlan_config *conf,
  3404. bool changelink, struct netlink_ext_ack *extack)
  3405. {
  3406. struct vxlan_dev *vxlan = netdev_priv(dev);
  3407. int err = 0;
  3408. memset(conf, 0, sizeof(*conf));
  3409. /* if changelink operation, start with old existing cfg */
  3410. if (changelink)
  3411. memcpy(conf, &vxlan->cfg, sizeof(*conf));
  3412. if (data[IFLA_VXLAN_ID]) {
  3413. __be32 vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID]));
  3414. if (changelink && (vni != conf->vni)) {
  3415. NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_ID], "Cannot change VNI");
  3416. return -EOPNOTSUPP;
  3417. }
  3418. conf->vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID]));
  3419. }
  3420. if (data[IFLA_VXLAN_GROUP]) {
  3421. if (changelink && (conf->remote_ip.sa.sa_family != AF_INET)) {
  3422. NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_GROUP], "New group address family does not match old group");
  3423. return -EOPNOTSUPP;
  3424. }
  3425. conf->remote_ip.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_GROUP]);
  3426. conf->remote_ip.sa.sa_family = AF_INET;
  3427. } else if (data[IFLA_VXLAN_GROUP6]) {
  3428. if (!IS_ENABLED(CONFIG_IPV6)) {
  3429. NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_GROUP6], "IPv6 support not enabled in the kernel");
  3430. return -EPFNOSUPPORT;
  3431. }
  3432. if (changelink && (conf->remote_ip.sa.sa_family != AF_INET6)) {
  3433. NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_GROUP6], "New group address family does not match old group");
  3434. return -EOPNOTSUPP;
  3435. }
  3436. conf->remote_ip.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_GROUP6]);
  3437. conf->remote_ip.sa.sa_family = AF_INET6;
  3438. }
  3439. if (data[IFLA_VXLAN_LOCAL]) {
  3440. if (changelink && (conf->saddr.sa.sa_family != AF_INET)) {
  3441. NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_LOCAL], "New local address family does not match old");
  3442. return -EOPNOTSUPP;
  3443. }
  3444. conf->saddr.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_LOCAL]);
  3445. conf->saddr.sa.sa_family = AF_INET;
  3446. } else if (data[IFLA_VXLAN_LOCAL6]) {
  3447. if (!IS_ENABLED(CONFIG_IPV6)) {
  3448. NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_LOCAL6], "IPv6 support not enabled in the kernel");
  3449. return -EPFNOSUPPORT;
  3450. }
  3451. if (changelink && (conf->saddr.sa.sa_family != AF_INET6)) {
  3452. NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_LOCAL6], "New local address family does not match old");
  3453. return -EOPNOTSUPP;
  3454. }
  3455. /* TODO: respect scope id */
  3456. conf->saddr.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_LOCAL6]);
  3457. conf->saddr.sa.sa_family = AF_INET6;
  3458. }
  3459. if (data[IFLA_VXLAN_LINK])
  3460. conf->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]);
  3461. if (data[IFLA_VXLAN_TOS])
  3462. conf->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
  3463. if (data[IFLA_VXLAN_TTL])
  3464. conf->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
  3465. if (data[IFLA_VXLAN_TTL_INHERIT]) {
  3466. err = vxlan_nl2flag(conf, data, IFLA_VXLAN_TTL_INHERIT,
  3467. VXLAN_F_TTL_INHERIT, changelink, false,
  3468. extack);
  3469. if (err)
  3470. return err;
  3471. }
  3472. if (data[IFLA_VXLAN_LABEL])
  3473. conf->label = nla_get_be32(data[IFLA_VXLAN_LABEL]) &
  3474. IPV6_FLOWLABEL_MASK;
  3475. if (data[IFLA_VXLAN_LABEL_POLICY])
  3476. conf->label_policy = nla_get_u32(data[IFLA_VXLAN_LABEL_POLICY]);
  3477. if (data[IFLA_VXLAN_LEARNING]) {
  3478. err = vxlan_nl2flag(conf, data, IFLA_VXLAN_LEARNING,
  3479. VXLAN_F_LEARN, changelink, true,
  3480. extack);
  3481. if (err)
  3482. return err;
  3483. } else if (!changelink) {
  3484. /* default to learn on a new device */
  3485. conf->flags |= VXLAN_F_LEARN;
  3486. }
  3487. if (data[IFLA_VXLAN_AGEING])
  3488. conf->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
  3489. if (data[IFLA_VXLAN_PROXY]) {
  3490. err = vxlan_nl2flag(conf, data, IFLA_VXLAN_PROXY,
  3491. VXLAN_F_PROXY, changelink, false,
  3492. extack);
  3493. if (err)
  3494. return err;
  3495. }
  3496. if (data[IFLA_VXLAN_RSC]) {
  3497. err = vxlan_nl2flag(conf, data, IFLA_VXLAN_RSC,
  3498. VXLAN_F_RSC, changelink, false,
  3499. extack);
  3500. if (err)
  3501. return err;
  3502. }
  3503. if (data[IFLA_VXLAN_L2MISS]) {
  3504. err = vxlan_nl2flag(conf, data, IFLA_VXLAN_L2MISS,
  3505. VXLAN_F_L2MISS, changelink, false,
  3506. extack);
  3507. if (err)
  3508. return err;
  3509. }
  3510. if (data[IFLA_VXLAN_L3MISS]) {
  3511. err = vxlan_nl2flag(conf, data, IFLA_VXLAN_L3MISS,
  3512. VXLAN_F_L3MISS, changelink, false,
  3513. extack);
  3514. if (err)
  3515. return err;
  3516. }
  3517. if (data[IFLA_VXLAN_LIMIT]) {
  3518. if (changelink) {
  3519. NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_LIMIT],
  3520. "Cannot change limit");
  3521. return -EOPNOTSUPP;
  3522. }
  3523. conf->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
  3524. }
  3525. if (data[IFLA_VXLAN_COLLECT_METADATA]) {
  3526. err = vxlan_nl2flag(conf, data, IFLA_VXLAN_COLLECT_METADATA,
  3527. VXLAN_F_COLLECT_METADATA, changelink, false,
  3528. extack);
  3529. if (err)
  3530. return err;
  3531. }
  3532. if (data[IFLA_VXLAN_PORT_RANGE]) {
  3533. if (!changelink) {
  3534. const struct ifla_vxlan_port_range *p
  3535. = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
  3536. conf->port_min = ntohs(p->low);
  3537. conf->port_max = ntohs(p->high);
  3538. } else {
  3539. NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_PORT_RANGE],
  3540. "Cannot change port range");
  3541. return -EOPNOTSUPP;
  3542. }
  3543. }
  3544. if (data[IFLA_VXLAN_PORT]) {
  3545. if (changelink) {
  3546. NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_PORT],
  3547. "Cannot change port");
  3548. return -EOPNOTSUPP;
  3549. }
  3550. conf->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
  3551. }
  3552. if (data[IFLA_VXLAN_UDP_CSUM]) {
  3553. if (changelink) {
  3554. NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_UDP_CSUM],
  3555. "Cannot change UDP_CSUM flag");
  3556. return -EOPNOTSUPP;
  3557. }
  3558. if (!nla_get_u8(data[IFLA_VXLAN_UDP_CSUM]))
  3559. conf->flags |= VXLAN_F_UDP_ZERO_CSUM_TX;
  3560. }
  3561. if (data[IFLA_VXLAN_LOCALBYPASS]) {
  3562. err = vxlan_nl2flag(conf, data, IFLA_VXLAN_LOCALBYPASS,
  3563. VXLAN_F_LOCALBYPASS, changelink,
  3564. true, extack);
  3565. if (err)
  3566. return err;
  3567. } else if (!changelink) {
  3568. /* default to local bypass on a new device */
  3569. conf->flags |= VXLAN_F_LOCALBYPASS;
  3570. }
  3571. if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]) {
  3572. err = vxlan_nl2flag(conf, data, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
  3573. VXLAN_F_UDP_ZERO_CSUM6_TX, changelink,
  3574. false, extack);
  3575. if (err)
  3576. return err;
  3577. }
  3578. if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]) {
  3579. err = vxlan_nl2flag(conf, data, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
  3580. VXLAN_F_UDP_ZERO_CSUM6_RX, changelink,
  3581. false, extack);
  3582. if (err)
  3583. return err;
  3584. }
  3585. if (data[IFLA_VXLAN_REMCSUM_TX]) {
  3586. err = vxlan_nl2flag(conf, data, IFLA_VXLAN_REMCSUM_TX,
  3587. VXLAN_F_REMCSUM_TX, changelink, false,
  3588. extack);
  3589. if (err)
  3590. return err;
  3591. }
  3592. if (data[IFLA_VXLAN_REMCSUM_RX]) {
  3593. err = vxlan_nl2flag(conf, data, IFLA_VXLAN_REMCSUM_RX,
  3594. VXLAN_F_REMCSUM_RX, changelink, false,
  3595. extack);
  3596. if (err)
  3597. return err;
  3598. }
  3599. if (data[IFLA_VXLAN_GBP]) {
  3600. err = vxlan_nl2flag(conf, data, IFLA_VXLAN_GBP,
  3601. VXLAN_F_GBP, changelink, false, extack);
  3602. if (err)
  3603. return err;
  3604. }
  3605. if (data[IFLA_VXLAN_GPE]) {
  3606. err = vxlan_nl2flag(conf, data, IFLA_VXLAN_GPE,
  3607. VXLAN_F_GPE, changelink, false,
  3608. extack);
  3609. if (err)
  3610. return err;
  3611. }
  3612. if (data[IFLA_VXLAN_REMCSUM_NOPARTIAL]) {
  3613. err = vxlan_nl2flag(conf, data, IFLA_VXLAN_REMCSUM_NOPARTIAL,
  3614. VXLAN_F_REMCSUM_NOPARTIAL, changelink,
  3615. false, extack);
  3616. if (err)
  3617. return err;
  3618. }
  3619. if (tb[IFLA_MTU]) {
  3620. if (changelink) {
  3621. NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_MTU],
  3622. "Cannot change mtu");
  3623. return -EOPNOTSUPP;
  3624. }
  3625. conf->mtu = nla_get_u32(tb[IFLA_MTU]);
  3626. }
  3627. if (data[IFLA_VXLAN_DF])
  3628. conf->df = nla_get_u8(data[IFLA_VXLAN_DF]);
  3629. if (data[IFLA_VXLAN_VNIFILTER]) {
  3630. err = vxlan_nl2flag(conf, data, IFLA_VXLAN_VNIFILTER,
  3631. VXLAN_F_VNIFILTER, changelink, false,
  3632. extack);
  3633. if (err)
  3634. return err;
  3635. if ((conf->flags & VXLAN_F_VNIFILTER) &&
  3636. !(conf->flags & VXLAN_F_COLLECT_METADATA)) {
  3637. NL_SET_ERR_MSG_ATTR(extack, data[IFLA_VXLAN_VNIFILTER],
  3638. "vxlan vnifilter only valid in collect metadata mode");
  3639. return -EINVAL;
  3640. }
  3641. }
  3642. return 0;
  3643. }
  3644. static int vxlan_newlink(struct net *src_net, struct net_device *dev,
  3645. struct nlattr *tb[], struct nlattr *data[],
  3646. struct netlink_ext_ack *extack)
  3647. {
  3648. struct vxlan_config conf;
  3649. int err;
  3650. err = vxlan_nl2conf(tb, data, dev, &conf, false, extack);
  3651. if (err)
  3652. return err;
  3653. return __vxlan_dev_create(src_net, dev, &conf, extack);
  3654. }
  3655. static int vxlan_changelink(struct net_device *dev, struct nlattr *tb[],
  3656. struct nlattr *data[],
  3657. struct netlink_ext_ack *extack)
  3658. {
  3659. struct vxlan_dev *vxlan = netdev_priv(dev);
  3660. bool rem_ip_changed, change_igmp;
  3661. struct net_device *lowerdev;
  3662. struct vxlan_config conf;
  3663. struct vxlan_rdst *dst;
  3664. int err;
  3665. dst = &vxlan->default_dst;
  3666. err = vxlan_nl2conf(tb, data, dev, &conf, true, extack);
  3667. if (err)
  3668. return err;
  3669. err = vxlan_config_validate(vxlan->net, &conf, &lowerdev,
  3670. vxlan, extack);
  3671. if (err)
  3672. return err;
  3673. if (dst->remote_dev == lowerdev)
  3674. lowerdev = NULL;
  3675. err = netdev_adjacent_change_prepare(dst->remote_dev, lowerdev, dev,
  3676. extack);
  3677. if (err)
  3678. return err;
  3679. rem_ip_changed = !vxlan_addr_equal(&conf.remote_ip, &dst->remote_ip);
  3680. change_igmp = vxlan->dev->flags & IFF_UP &&
  3681. (rem_ip_changed ||
  3682. dst->remote_ifindex != conf.remote_ifindex);
  3683. /* handle default dst entry */
  3684. if (rem_ip_changed) {
  3685. u32 hash_index = fdb_head_index(vxlan, all_zeros_mac, conf.vni);
  3686. spin_lock_bh(&vxlan->hash_lock[hash_index]);
  3687. if (!vxlan_addr_any(&conf.remote_ip)) {
  3688. err = vxlan_fdb_update(vxlan, all_zeros_mac,
  3689. &conf.remote_ip,
  3690. NUD_REACHABLE | NUD_PERMANENT,
  3691. NLM_F_APPEND | NLM_F_CREATE,
  3692. vxlan->cfg.dst_port,
  3693. conf.vni, conf.vni,
  3694. conf.remote_ifindex,
  3695. NTF_SELF, 0, true, extack);
  3696. if (err) {
  3697. spin_unlock_bh(&vxlan->hash_lock[hash_index]);
  3698. netdev_adjacent_change_abort(dst->remote_dev,
  3699. lowerdev, dev);
  3700. return err;
  3701. }
  3702. }
  3703. if (!vxlan_addr_any(&dst->remote_ip))
  3704. __vxlan_fdb_delete(vxlan, all_zeros_mac,
  3705. dst->remote_ip,
  3706. vxlan->cfg.dst_port,
  3707. dst->remote_vni,
  3708. dst->remote_vni,
  3709. dst->remote_ifindex,
  3710. true);
  3711. spin_unlock_bh(&vxlan->hash_lock[hash_index]);
  3712. /* If vni filtering device, also update fdb entries of
  3713. * all vnis that were using default remote ip
  3714. */
  3715. if (vxlan->cfg.flags & VXLAN_F_VNIFILTER) {
  3716. err = vxlan_vnilist_update_group(vxlan, &dst->remote_ip,
  3717. &conf.remote_ip, extack);
  3718. if (err) {
  3719. netdev_adjacent_change_abort(dst->remote_dev,
  3720. lowerdev, dev);
  3721. return err;
  3722. }
  3723. }
  3724. }
  3725. if (change_igmp && vxlan_addr_multicast(&dst->remote_ip))
  3726. err = vxlan_multicast_leave(vxlan);
  3727. if (conf.age_interval != vxlan->cfg.age_interval)
  3728. mod_timer(&vxlan->age_timer, jiffies);
  3729. netdev_adjacent_change_commit(dst->remote_dev, lowerdev, dev);
  3730. if (lowerdev && lowerdev != dst->remote_dev)
  3731. dst->remote_dev = lowerdev;
  3732. vxlan_config_apply(dev, &conf, lowerdev, vxlan->net, true);
  3733. if (!err && change_igmp &&
  3734. vxlan_addr_multicast(&dst->remote_ip))
  3735. err = vxlan_multicast_join(vxlan);
  3736. return err;
  3737. }
  3738. static void vxlan_dellink(struct net_device *dev, struct list_head *head)
  3739. {
  3740. struct vxlan_dev *vxlan = netdev_priv(dev);
  3741. struct vxlan_fdb_flush_desc desc = {
  3742. /* Default entry is deleted at vxlan_uninit. */
  3743. .ignore_default_entry = true,
  3744. };
  3745. vxlan_flush(vxlan, &desc);
  3746. list_del(&vxlan->next);
  3747. unregister_netdevice_queue(dev, head);
  3748. if (vxlan->default_dst.remote_dev)
  3749. netdev_upper_dev_unlink(vxlan->default_dst.remote_dev, dev);
  3750. }
  3751. static size_t vxlan_get_size(const struct net_device *dev)
  3752. {
  3753. return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
  3754. nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
  3755. nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
  3756. nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
  3757. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
  3758. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL_INHERIT */
  3759. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
  3760. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_DF */
  3761. nla_total_size(sizeof(__be32)) + /* IFLA_VXLAN_LABEL */
  3762. nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LABEL_POLICY */
  3763. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
  3764. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
  3765. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
  3766. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
  3767. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
  3768. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_COLLECT_METADATA */
  3769. nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
  3770. nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
  3771. nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */
  3772. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */
  3773. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
  3774. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
  3775. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_TX */
  3776. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_RX */
  3777. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LOCALBYPASS */
  3778. /* IFLA_VXLAN_PORT_RANGE */
  3779. nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
  3780. nla_total_size(0) + /* IFLA_VXLAN_GBP */
  3781. nla_total_size(0) + /* IFLA_VXLAN_GPE */
  3782. nla_total_size(0) + /* IFLA_VXLAN_REMCSUM_NOPARTIAL */
  3783. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_VNIFILTER */
  3784. 0;
  3785. }
  3786. static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
  3787. {
  3788. const struct vxlan_dev *vxlan = netdev_priv(dev);
  3789. const struct vxlan_rdst *dst = &vxlan->default_dst;
  3790. struct ifla_vxlan_port_range ports = {
  3791. .low = htons(vxlan->cfg.port_min),
  3792. .high = htons(vxlan->cfg.port_max),
  3793. };
  3794. if (nla_put_u32(skb, IFLA_VXLAN_ID, be32_to_cpu(dst->remote_vni)))
  3795. goto nla_put_failure;
  3796. if (!vxlan_addr_any(&dst->remote_ip)) {
  3797. if (dst->remote_ip.sa.sa_family == AF_INET) {
  3798. if (nla_put_in_addr(skb, IFLA_VXLAN_GROUP,
  3799. dst->remote_ip.sin.sin_addr.s_addr))
  3800. goto nla_put_failure;
  3801. #if IS_ENABLED(CONFIG_IPV6)
  3802. } else {
  3803. if (nla_put_in6_addr(skb, IFLA_VXLAN_GROUP6,
  3804. &dst->remote_ip.sin6.sin6_addr))
  3805. goto nla_put_failure;
  3806. #endif
  3807. }
  3808. }
  3809. if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
  3810. goto nla_put_failure;
  3811. if (!vxlan_addr_any(&vxlan->cfg.saddr)) {
  3812. if (vxlan->cfg.saddr.sa.sa_family == AF_INET) {
  3813. if (nla_put_in_addr(skb, IFLA_VXLAN_LOCAL,
  3814. vxlan->cfg.saddr.sin.sin_addr.s_addr))
  3815. goto nla_put_failure;
  3816. #if IS_ENABLED(CONFIG_IPV6)
  3817. } else {
  3818. if (nla_put_in6_addr(skb, IFLA_VXLAN_LOCAL6,
  3819. &vxlan->cfg.saddr.sin6.sin6_addr))
  3820. goto nla_put_failure;
  3821. #endif
  3822. }
  3823. }
  3824. if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->cfg.ttl) ||
  3825. nla_put_u8(skb, IFLA_VXLAN_TTL_INHERIT,
  3826. !!(vxlan->cfg.flags & VXLAN_F_TTL_INHERIT)) ||
  3827. nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->cfg.tos) ||
  3828. nla_put_u8(skb, IFLA_VXLAN_DF, vxlan->cfg.df) ||
  3829. nla_put_be32(skb, IFLA_VXLAN_LABEL, vxlan->cfg.label) ||
  3830. nla_put_u32(skb, IFLA_VXLAN_LABEL_POLICY, vxlan->cfg.label_policy) ||
  3831. nla_put_u8(skb, IFLA_VXLAN_LEARNING,
  3832. !!(vxlan->cfg.flags & VXLAN_F_LEARN)) ||
  3833. nla_put_u8(skb, IFLA_VXLAN_PROXY,
  3834. !!(vxlan->cfg.flags & VXLAN_F_PROXY)) ||
  3835. nla_put_u8(skb, IFLA_VXLAN_RSC,
  3836. !!(vxlan->cfg.flags & VXLAN_F_RSC)) ||
  3837. nla_put_u8(skb, IFLA_VXLAN_L2MISS,
  3838. !!(vxlan->cfg.flags & VXLAN_F_L2MISS)) ||
  3839. nla_put_u8(skb, IFLA_VXLAN_L3MISS,
  3840. !!(vxlan->cfg.flags & VXLAN_F_L3MISS)) ||
  3841. nla_put_u8(skb, IFLA_VXLAN_COLLECT_METADATA,
  3842. !!(vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA)) ||
  3843. nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->cfg.age_interval) ||
  3844. nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->cfg.addrmax) ||
  3845. nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->cfg.dst_port) ||
  3846. nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM,
  3847. !(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM_TX)) ||
  3848. nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
  3849. !!(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) ||
  3850. nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
  3851. !!(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM6_RX)) ||
  3852. nla_put_u8(skb, IFLA_VXLAN_REMCSUM_TX,
  3853. !!(vxlan->cfg.flags & VXLAN_F_REMCSUM_TX)) ||
  3854. nla_put_u8(skb, IFLA_VXLAN_REMCSUM_RX,
  3855. !!(vxlan->cfg.flags & VXLAN_F_REMCSUM_RX)) ||
  3856. nla_put_u8(skb, IFLA_VXLAN_LOCALBYPASS,
  3857. !!(vxlan->cfg.flags & VXLAN_F_LOCALBYPASS)))
  3858. goto nla_put_failure;
  3859. if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
  3860. goto nla_put_failure;
  3861. if (vxlan->cfg.flags & VXLAN_F_GBP &&
  3862. nla_put_flag(skb, IFLA_VXLAN_GBP))
  3863. goto nla_put_failure;
  3864. if (vxlan->cfg.flags & VXLAN_F_GPE &&
  3865. nla_put_flag(skb, IFLA_VXLAN_GPE))
  3866. goto nla_put_failure;
  3867. if (vxlan->cfg.flags & VXLAN_F_REMCSUM_NOPARTIAL &&
  3868. nla_put_flag(skb, IFLA_VXLAN_REMCSUM_NOPARTIAL))
  3869. goto nla_put_failure;
  3870. if (vxlan->cfg.flags & VXLAN_F_VNIFILTER &&
  3871. nla_put_u8(skb, IFLA_VXLAN_VNIFILTER,
  3872. !!(vxlan->cfg.flags & VXLAN_F_VNIFILTER)))
  3873. goto nla_put_failure;
  3874. return 0;
  3875. nla_put_failure:
  3876. return -EMSGSIZE;
  3877. }
  3878. static struct net *vxlan_get_link_net(const struct net_device *dev)
  3879. {
  3880. struct vxlan_dev *vxlan = netdev_priv(dev);
  3881. return READ_ONCE(vxlan->net);
  3882. }
  3883. static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
  3884. .kind = "vxlan",
  3885. .maxtype = IFLA_VXLAN_MAX,
  3886. .policy = vxlan_policy,
  3887. .priv_size = sizeof(struct vxlan_dev),
  3888. .setup = vxlan_setup,
  3889. .validate = vxlan_validate,
  3890. .newlink = vxlan_newlink,
  3891. .changelink = vxlan_changelink,
  3892. .dellink = vxlan_dellink,
  3893. .get_size = vxlan_get_size,
  3894. .fill_info = vxlan_fill_info,
  3895. .get_link_net = vxlan_get_link_net,
  3896. };
  3897. struct net_device *vxlan_dev_create(struct net *net, const char *name,
  3898. u8 name_assign_type,
  3899. struct vxlan_config *conf)
  3900. {
  3901. struct nlattr *tb[IFLA_MAX + 1];
  3902. struct net_device *dev;
  3903. int err;
  3904. memset(&tb, 0, sizeof(tb));
  3905. dev = rtnl_create_link(net, name, name_assign_type,
  3906. &vxlan_link_ops, tb, NULL);
  3907. if (IS_ERR(dev))
  3908. return dev;
  3909. err = __vxlan_dev_create(net, dev, conf, NULL);
  3910. if (err < 0) {
  3911. free_netdev(dev);
  3912. return ERR_PTR(err);
  3913. }
  3914. err = rtnl_configure_link(dev, NULL, 0, NULL);
  3915. if (err < 0) {
  3916. LIST_HEAD(list_kill);
  3917. vxlan_dellink(dev, &list_kill);
  3918. unregister_netdevice_many(&list_kill);
  3919. return ERR_PTR(err);
  3920. }
  3921. return dev;
  3922. }
  3923. EXPORT_SYMBOL_GPL(vxlan_dev_create);
  3924. static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn,
  3925. struct net_device *dev)
  3926. {
  3927. struct vxlan_dev *vxlan, *next;
  3928. LIST_HEAD(list_kill);
  3929. list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
  3930. struct vxlan_rdst *dst = &vxlan->default_dst;
  3931. /* In case we created vxlan device with carrier
  3932. * and we loose the carrier due to module unload
  3933. * we also need to remove vxlan device. In other
  3934. * cases, it's not necessary and remote_ifindex
  3935. * is 0 here, so no matches.
  3936. */
  3937. if (dst->remote_ifindex == dev->ifindex)
  3938. vxlan_dellink(vxlan->dev, &list_kill);
  3939. }
  3940. unregister_netdevice_many(&list_kill);
  3941. }
  3942. static int vxlan_netdevice_event(struct notifier_block *unused,
  3943. unsigned long event, void *ptr)
  3944. {
  3945. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  3946. struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
  3947. if (event == NETDEV_UNREGISTER)
  3948. vxlan_handle_lowerdev_unregister(vn, dev);
  3949. else if (event == NETDEV_UDP_TUNNEL_PUSH_INFO)
  3950. vxlan_offload_rx_ports(dev, true);
  3951. else if (event == NETDEV_UDP_TUNNEL_DROP_INFO)
  3952. vxlan_offload_rx_ports(dev, false);
  3953. return NOTIFY_DONE;
  3954. }
  3955. static struct notifier_block vxlan_notifier_block __read_mostly = {
  3956. .notifier_call = vxlan_netdevice_event,
  3957. };
  3958. static void
  3959. vxlan_fdb_offloaded_set(struct net_device *dev,
  3960. struct switchdev_notifier_vxlan_fdb_info *fdb_info)
  3961. {
  3962. struct vxlan_dev *vxlan = netdev_priv(dev);
  3963. struct vxlan_rdst *rdst;
  3964. struct vxlan_fdb *f;
  3965. u32 hash_index;
  3966. hash_index = fdb_head_index(vxlan, fdb_info->eth_addr, fdb_info->vni);
  3967. spin_lock_bh(&vxlan->hash_lock[hash_index]);
  3968. f = __vxlan_find_mac(vxlan, fdb_info->eth_addr, fdb_info->vni);
  3969. if (!f)
  3970. goto out;
  3971. rdst = vxlan_fdb_find_rdst(f, &fdb_info->remote_ip,
  3972. fdb_info->remote_port,
  3973. fdb_info->remote_vni,
  3974. fdb_info->remote_ifindex);
  3975. if (!rdst)
  3976. goto out;
  3977. rdst->offloaded = fdb_info->offloaded;
  3978. out:
  3979. spin_unlock_bh(&vxlan->hash_lock[hash_index]);
  3980. }
  3981. static int
  3982. vxlan_fdb_external_learn_add(struct net_device *dev,
  3983. struct switchdev_notifier_vxlan_fdb_info *fdb_info)
  3984. {
  3985. struct vxlan_dev *vxlan = netdev_priv(dev);
  3986. struct netlink_ext_ack *extack;
  3987. u32 hash_index;
  3988. int err;
  3989. hash_index = fdb_head_index(vxlan, fdb_info->eth_addr, fdb_info->vni);
  3990. extack = switchdev_notifier_info_to_extack(&fdb_info->info);
  3991. spin_lock_bh(&vxlan->hash_lock[hash_index]);
  3992. err = vxlan_fdb_update(vxlan, fdb_info->eth_addr, &fdb_info->remote_ip,
  3993. NUD_REACHABLE,
  3994. NLM_F_CREATE | NLM_F_REPLACE,
  3995. fdb_info->remote_port,
  3996. fdb_info->vni,
  3997. fdb_info->remote_vni,
  3998. fdb_info->remote_ifindex,
  3999. NTF_USE | NTF_SELF | NTF_EXT_LEARNED,
  4000. 0, false, extack);
  4001. spin_unlock_bh(&vxlan->hash_lock[hash_index]);
  4002. return err;
  4003. }
  4004. static int
  4005. vxlan_fdb_external_learn_del(struct net_device *dev,
  4006. struct switchdev_notifier_vxlan_fdb_info *fdb_info)
  4007. {
  4008. struct vxlan_dev *vxlan = netdev_priv(dev);
  4009. struct vxlan_fdb *f;
  4010. u32 hash_index;
  4011. int err = 0;
  4012. hash_index = fdb_head_index(vxlan, fdb_info->eth_addr, fdb_info->vni);
  4013. spin_lock_bh(&vxlan->hash_lock[hash_index]);
  4014. f = __vxlan_find_mac(vxlan, fdb_info->eth_addr, fdb_info->vni);
  4015. if (!f)
  4016. err = -ENOENT;
  4017. else if (f->flags & NTF_EXT_LEARNED)
  4018. err = __vxlan_fdb_delete(vxlan, fdb_info->eth_addr,
  4019. fdb_info->remote_ip,
  4020. fdb_info->remote_port,
  4021. fdb_info->vni,
  4022. fdb_info->remote_vni,
  4023. fdb_info->remote_ifindex,
  4024. false);
  4025. spin_unlock_bh(&vxlan->hash_lock[hash_index]);
  4026. return err;
  4027. }
  4028. static int vxlan_switchdev_event(struct notifier_block *unused,
  4029. unsigned long event, void *ptr)
  4030. {
  4031. struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
  4032. struct switchdev_notifier_vxlan_fdb_info *fdb_info;
  4033. int err = 0;
  4034. switch (event) {
  4035. case SWITCHDEV_VXLAN_FDB_OFFLOADED:
  4036. vxlan_fdb_offloaded_set(dev, ptr);
  4037. break;
  4038. case SWITCHDEV_VXLAN_FDB_ADD_TO_BRIDGE:
  4039. fdb_info = ptr;
  4040. err = vxlan_fdb_external_learn_add(dev, fdb_info);
  4041. if (err) {
  4042. err = notifier_from_errno(err);
  4043. break;
  4044. }
  4045. fdb_info->offloaded = true;
  4046. vxlan_fdb_offloaded_set(dev, fdb_info);
  4047. break;
  4048. case SWITCHDEV_VXLAN_FDB_DEL_TO_BRIDGE:
  4049. fdb_info = ptr;
  4050. err = vxlan_fdb_external_learn_del(dev, fdb_info);
  4051. if (err) {
  4052. err = notifier_from_errno(err);
  4053. break;
  4054. }
  4055. fdb_info->offloaded = false;
  4056. vxlan_fdb_offloaded_set(dev, fdb_info);
  4057. break;
  4058. }
  4059. return err;
  4060. }
  4061. static struct notifier_block vxlan_switchdev_notifier_block __read_mostly = {
  4062. .notifier_call = vxlan_switchdev_event,
  4063. };
  4064. static void vxlan_fdb_nh_flush(struct nexthop *nh)
  4065. {
  4066. struct vxlan_fdb *fdb;
  4067. struct vxlan_dev *vxlan;
  4068. u32 hash_index;
  4069. rcu_read_lock();
  4070. list_for_each_entry_rcu(fdb, &nh->fdb_list, nh_list) {
  4071. vxlan = rcu_dereference(fdb->vdev);
  4072. WARN_ON(!vxlan);
  4073. hash_index = fdb_head_index(vxlan, fdb->eth_addr,
  4074. vxlan->default_dst.remote_vni);
  4075. spin_lock_bh(&vxlan->hash_lock[hash_index]);
  4076. if (!hlist_unhashed(&fdb->hlist))
  4077. vxlan_fdb_destroy(vxlan, fdb, false, false);
  4078. spin_unlock_bh(&vxlan->hash_lock[hash_index]);
  4079. }
  4080. rcu_read_unlock();
  4081. }
  4082. static int vxlan_nexthop_event(struct notifier_block *nb,
  4083. unsigned long event, void *ptr)
  4084. {
  4085. struct nh_notifier_info *info = ptr;
  4086. struct nexthop *nh;
  4087. if (event != NEXTHOP_EVENT_DEL)
  4088. return NOTIFY_DONE;
  4089. nh = nexthop_find_by_id(info->net, info->id);
  4090. if (!nh)
  4091. return NOTIFY_DONE;
  4092. vxlan_fdb_nh_flush(nh);
  4093. return NOTIFY_DONE;
  4094. }
  4095. static __net_init int vxlan_init_net(struct net *net)
  4096. {
  4097. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  4098. unsigned int h;
  4099. INIT_LIST_HEAD(&vn->vxlan_list);
  4100. spin_lock_init(&vn->sock_lock);
  4101. vn->nexthop_notifier_block.notifier_call = vxlan_nexthop_event;
  4102. for (h = 0; h < PORT_HASH_SIZE; ++h)
  4103. INIT_HLIST_HEAD(&vn->sock_list[h]);
  4104. return register_nexthop_notifier(net, &vn->nexthop_notifier_block,
  4105. NULL);
  4106. }
  4107. static void __net_exit vxlan_destroy_tunnels(struct vxlan_net *vn,
  4108. struct list_head *dev_to_kill)
  4109. {
  4110. struct vxlan_dev *vxlan, *next;
  4111. list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next)
  4112. vxlan_dellink(vxlan->dev, dev_to_kill);
  4113. }
  4114. static void __net_exit vxlan_exit_batch_rtnl(struct list_head *net_list,
  4115. struct list_head *dev_to_kill)
  4116. {
  4117. struct net *net;
  4118. ASSERT_RTNL();
  4119. list_for_each_entry(net, net_list, exit_list) {
  4120. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  4121. __unregister_nexthop_notifier(net, &vn->nexthop_notifier_block);
  4122. vxlan_destroy_tunnels(vn, dev_to_kill);
  4123. }
  4124. }
  4125. static void __net_exit vxlan_exit_net(struct net *net)
  4126. {
  4127. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  4128. unsigned int h;
  4129. for (h = 0; h < PORT_HASH_SIZE; ++h)
  4130. WARN_ON_ONCE(!hlist_empty(&vn->sock_list[h]));
  4131. }
  4132. static struct pernet_operations vxlan_net_ops = {
  4133. .init = vxlan_init_net,
  4134. .exit_batch_rtnl = vxlan_exit_batch_rtnl,
  4135. .exit = vxlan_exit_net,
  4136. .id = &vxlan_net_id,
  4137. .size = sizeof(struct vxlan_net),
  4138. };
  4139. static int __init vxlan_init_module(void)
  4140. {
  4141. int rc;
  4142. get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
  4143. rc = register_pernet_subsys(&vxlan_net_ops);
  4144. if (rc)
  4145. goto out1;
  4146. rc = register_netdevice_notifier(&vxlan_notifier_block);
  4147. if (rc)
  4148. goto out2;
  4149. rc = register_switchdev_notifier(&vxlan_switchdev_notifier_block);
  4150. if (rc)
  4151. goto out3;
  4152. rc = rtnl_link_register(&vxlan_link_ops);
  4153. if (rc)
  4154. goto out4;
  4155. rc = vxlan_vnifilter_init();
  4156. if (rc)
  4157. goto out5;
  4158. return 0;
  4159. out5:
  4160. rtnl_link_unregister(&vxlan_link_ops);
  4161. out4:
  4162. unregister_switchdev_notifier(&vxlan_switchdev_notifier_block);
  4163. out3:
  4164. unregister_netdevice_notifier(&vxlan_notifier_block);
  4165. out2:
  4166. unregister_pernet_subsys(&vxlan_net_ops);
  4167. out1:
  4168. return rc;
  4169. }
  4170. late_initcall(vxlan_init_module);
  4171. static void __exit vxlan_cleanup_module(void)
  4172. {
  4173. vxlan_vnifilter_uninit();
  4174. rtnl_link_unregister(&vxlan_link_ops);
  4175. unregister_switchdev_notifier(&vxlan_switchdev_notifier_block);
  4176. unregister_netdevice_notifier(&vxlan_notifier_block);
  4177. unregister_pernet_subsys(&vxlan_net_ops);
  4178. /* rcu_barrier() is called by netns */
  4179. }
  4180. module_exit(vxlan_cleanup_module);
  4181. MODULE_LICENSE("GPL");
  4182. MODULE_VERSION(VXLAN_VERSION);
  4183. MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
  4184. MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
  4185. MODULE_ALIAS_RTNL_LINK("vxlan");