vxlan.c 96 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811
  1. /*
  2. * VXLAN: Virtual eXtensible Local Area Network
  3. *
  4. * Copyright (c) 2012-2013 Vyatta Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/errno.h>
  14. #include <linux/slab.h>
  15. #include <linux/udp.h>
  16. #include <linux/igmp.h>
  17. #include <linux/if_ether.h>
  18. #include <linux/ethtool.h>
  19. #include <net/arp.h>
  20. #include <net/ndisc.h>
  21. #include <net/ip.h>
  22. #include <net/icmp.h>
  23. #include <net/rtnetlink.h>
  24. #include <net/inet_ecn.h>
  25. #include <net/net_namespace.h>
  26. #include <net/netns/generic.h>
  27. #include <net/tun_proto.h>
  28. #include <net/vxlan.h>
  29. #if IS_ENABLED(CONFIG_IPV6)
  30. #include <net/ip6_tunnel.h>
  31. #include <net/ip6_checksum.h>
  32. #endif
  33. #define VXLAN_VERSION "0.1"
  34. #define PORT_HASH_BITS 8
  35. #define PORT_HASH_SIZE (1<<PORT_HASH_BITS)
  36. #define FDB_AGE_DEFAULT 300 /* 5 min */
  37. #define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
  38. /* UDP port for VXLAN traffic.
  39. * The IANA assigned port is 4789, but the Linux default is 8472
  40. * for compatibility with early adopters.
  41. */
  42. static unsigned short vxlan_port __read_mostly = 8472;
  43. module_param_named(udp_port, vxlan_port, ushort, 0444);
  44. MODULE_PARM_DESC(udp_port, "Destination UDP port");
  45. static bool log_ecn_error = true;
  46. module_param(log_ecn_error, bool, 0644);
  47. MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
  48. static unsigned int vxlan_net_id;
  49. static struct rtnl_link_ops vxlan_link_ops;
  50. static const u8 all_zeros_mac[ETH_ALEN + 2];
  51. static int vxlan_sock_add(struct vxlan_dev *vxlan);
  52. static void vxlan_vs_del_dev(struct vxlan_dev *vxlan);
  53. /* per-network namespace private data for this module */
  54. struct vxlan_net {
  55. struct list_head vxlan_list;
  56. struct hlist_head sock_list[PORT_HASH_SIZE];
  57. spinlock_t sock_lock;
  58. };
  59. /* Forwarding table entry */
  60. struct vxlan_fdb {
  61. struct hlist_node hlist; /* linked list of entries */
  62. struct rcu_head rcu;
  63. unsigned long updated; /* jiffies */
  64. unsigned long used;
  65. struct list_head remotes;
  66. u8 eth_addr[ETH_ALEN];
  67. u16 state; /* see ndm_state */
  68. __be32 vni;
  69. u8 flags; /* see ndm_flags */
  70. };
  71. /* salt for hash table */
  72. static u32 vxlan_salt __read_mostly;
  73. static inline bool vxlan_collect_metadata(struct vxlan_sock *vs)
  74. {
  75. return vs->flags & VXLAN_F_COLLECT_METADATA ||
  76. ip_tunnel_collect_metadata();
  77. }
  78. #if IS_ENABLED(CONFIG_IPV6)
  79. static inline
  80. bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
  81. {
  82. if (a->sa.sa_family != b->sa.sa_family)
  83. return false;
  84. if (a->sa.sa_family == AF_INET6)
  85. return ipv6_addr_equal(&a->sin6.sin6_addr, &b->sin6.sin6_addr);
  86. else
  87. return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
  88. }
  89. static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
  90. {
  91. if (ipa->sa.sa_family == AF_INET6)
  92. return ipv6_addr_any(&ipa->sin6.sin6_addr);
  93. else
  94. return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
  95. }
  96. static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
  97. {
  98. if (ipa->sa.sa_family == AF_INET6)
  99. return ipv6_addr_is_multicast(&ipa->sin6.sin6_addr);
  100. else
  101. return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
  102. }
  103. static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
  104. {
  105. if (nla_len(nla) >= sizeof(struct in6_addr)) {
  106. ip->sin6.sin6_addr = nla_get_in6_addr(nla);
  107. ip->sa.sa_family = AF_INET6;
  108. return 0;
  109. } else if (nla_len(nla) >= sizeof(__be32)) {
  110. ip->sin.sin_addr.s_addr = nla_get_in_addr(nla);
  111. ip->sa.sa_family = AF_INET;
  112. return 0;
  113. } else {
  114. return -EAFNOSUPPORT;
  115. }
  116. }
  117. static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
  118. const union vxlan_addr *ip)
  119. {
  120. if (ip->sa.sa_family == AF_INET6)
  121. return nla_put_in6_addr(skb, attr, &ip->sin6.sin6_addr);
  122. else
  123. return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr);
  124. }
  125. #else /* !CONFIG_IPV6 */
  126. static inline
  127. bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
  128. {
  129. return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
  130. }
  131. static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
  132. {
  133. return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
  134. }
  135. static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
  136. {
  137. return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
  138. }
  139. static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
  140. {
  141. if (nla_len(nla) >= sizeof(struct in6_addr)) {
  142. return -EAFNOSUPPORT;
  143. } else if (nla_len(nla) >= sizeof(__be32)) {
  144. ip->sin.sin_addr.s_addr = nla_get_in_addr(nla);
  145. ip->sa.sa_family = AF_INET;
  146. return 0;
  147. } else {
  148. return -EAFNOSUPPORT;
  149. }
  150. }
  151. static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
  152. const union vxlan_addr *ip)
  153. {
  154. return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr);
  155. }
  156. #endif
  157. /* Virtual Network hash table head */
  158. static inline struct hlist_head *vni_head(struct vxlan_sock *vs, __be32 vni)
  159. {
  160. return &vs->vni_list[hash_32((__force u32)vni, VNI_HASH_BITS)];
  161. }
  162. /* Socket hash table head */
  163. static inline struct hlist_head *vs_head(struct net *net, __be16 port)
  164. {
  165. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  166. return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
  167. }
  168. /* First remote destination for a forwarding entry.
  169. * Guaranteed to be non-NULL because remotes are never deleted.
  170. */
  171. static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb)
  172. {
  173. return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list);
  174. }
  175. static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb)
  176. {
  177. return list_first_entry(&fdb->remotes, struct vxlan_rdst, list);
  178. }
  179. /* Find VXLAN socket based on network namespace, address family and UDP port
  180. * and enabled unshareable flags.
  181. */
  182. static struct vxlan_sock *vxlan_find_sock(struct net *net, sa_family_t family,
  183. __be16 port, u32 flags)
  184. {
  185. struct vxlan_sock *vs;
  186. flags &= VXLAN_F_RCV_FLAGS;
  187. hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
  188. if (inet_sk(vs->sock->sk)->inet_sport == port &&
  189. vxlan_get_sk_family(vs) == family &&
  190. vs->flags == flags)
  191. return vs;
  192. }
  193. return NULL;
  194. }
  195. static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, int ifindex,
  196. __be32 vni)
  197. {
  198. struct vxlan_dev_node *node;
  199. /* For flow based devices, map all packets to VNI 0 */
  200. if (vs->flags & VXLAN_F_COLLECT_METADATA)
  201. vni = 0;
  202. hlist_for_each_entry_rcu(node, vni_head(vs, vni), hlist) {
  203. if (node->vxlan->default_dst.remote_vni != vni)
  204. continue;
  205. if (IS_ENABLED(CONFIG_IPV6)) {
  206. const struct vxlan_config *cfg = &node->vxlan->cfg;
  207. if ((cfg->flags & VXLAN_F_IPV6_LINKLOCAL) &&
  208. cfg->remote_ifindex != ifindex)
  209. continue;
  210. }
  211. return node->vxlan;
  212. }
  213. return NULL;
  214. }
  215. /* Look up VNI in a per net namespace table */
  216. static struct vxlan_dev *vxlan_find_vni(struct net *net, int ifindex,
  217. __be32 vni, sa_family_t family,
  218. __be16 port, u32 flags)
  219. {
  220. struct vxlan_sock *vs;
  221. vs = vxlan_find_sock(net, family, port, flags);
  222. if (!vs)
  223. return NULL;
  224. return vxlan_vs_find_vni(vs, ifindex, vni);
  225. }
  226. /* Fill in neighbour message in skbuff. */
  227. static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
  228. const struct vxlan_fdb *fdb,
  229. u32 portid, u32 seq, int type, unsigned int flags,
  230. const struct vxlan_rdst *rdst)
  231. {
  232. unsigned long now = jiffies;
  233. struct nda_cacheinfo ci;
  234. struct nlmsghdr *nlh;
  235. struct ndmsg *ndm;
  236. bool send_ip, send_eth;
  237. nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
  238. if (nlh == NULL)
  239. return -EMSGSIZE;
  240. ndm = nlmsg_data(nlh);
  241. memset(ndm, 0, sizeof(*ndm));
  242. send_eth = send_ip = true;
  243. if (type == RTM_GETNEIGH) {
  244. send_ip = !vxlan_addr_any(&rdst->remote_ip);
  245. send_eth = !is_zero_ether_addr(fdb->eth_addr);
  246. ndm->ndm_family = send_ip ? rdst->remote_ip.sa.sa_family : AF_INET;
  247. } else
  248. ndm->ndm_family = AF_BRIDGE;
  249. ndm->ndm_state = fdb->state;
  250. ndm->ndm_ifindex = vxlan->dev->ifindex;
  251. ndm->ndm_flags = fdb->flags;
  252. ndm->ndm_type = RTN_UNICAST;
  253. if (!net_eq(dev_net(vxlan->dev), vxlan->net) &&
  254. nla_put_s32(skb, NDA_LINK_NETNSID,
  255. peernet2id(dev_net(vxlan->dev), vxlan->net)))
  256. goto nla_put_failure;
  257. if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
  258. goto nla_put_failure;
  259. if (send_ip && vxlan_nla_put_addr(skb, NDA_DST, &rdst->remote_ip))
  260. goto nla_put_failure;
  261. if (rdst->remote_port && rdst->remote_port != vxlan->cfg.dst_port &&
  262. nla_put_be16(skb, NDA_PORT, rdst->remote_port))
  263. goto nla_put_failure;
  264. if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
  265. nla_put_u32(skb, NDA_VNI, be32_to_cpu(rdst->remote_vni)))
  266. goto nla_put_failure;
  267. if ((vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) && fdb->vni &&
  268. nla_put_u32(skb, NDA_SRC_VNI,
  269. be32_to_cpu(fdb->vni)))
  270. goto nla_put_failure;
  271. if (rdst->remote_ifindex &&
  272. nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
  273. goto nla_put_failure;
  274. ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
  275. ci.ndm_confirmed = 0;
  276. ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
  277. ci.ndm_refcnt = 0;
  278. if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
  279. goto nla_put_failure;
  280. nlmsg_end(skb, nlh);
  281. return 0;
  282. nla_put_failure:
  283. nlmsg_cancel(skb, nlh);
  284. return -EMSGSIZE;
  285. }
  286. static inline size_t vxlan_nlmsg_size(void)
  287. {
  288. return NLMSG_ALIGN(sizeof(struct ndmsg))
  289. + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
  290. + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */
  291. + nla_total_size(sizeof(__be16)) /* NDA_PORT */
  292. + nla_total_size(sizeof(__be32)) /* NDA_VNI */
  293. + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
  294. + nla_total_size(sizeof(__s32)) /* NDA_LINK_NETNSID */
  295. + nla_total_size(sizeof(struct nda_cacheinfo));
  296. }
  297. static void vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
  298. struct vxlan_rdst *rd, int type)
  299. {
  300. struct net *net = dev_net(vxlan->dev);
  301. struct sk_buff *skb;
  302. int err = -ENOBUFS;
  303. skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
  304. if (skb == NULL)
  305. goto errout;
  306. err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, rd);
  307. if (err < 0) {
  308. /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
  309. WARN_ON(err == -EMSGSIZE);
  310. kfree_skb(skb);
  311. goto errout;
  312. }
  313. rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
  314. return;
  315. errout:
  316. if (err < 0)
  317. rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
  318. }
  319. static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa)
  320. {
  321. struct vxlan_dev *vxlan = netdev_priv(dev);
  322. struct vxlan_fdb f = {
  323. .state = NUD_STALE,
  324. };
  325. struct vxlan_rdst remote = {
  326. .remote_ip = *ipa, /* goes to NDA_DST */
  327. .remote_vni = cpu_to_be32(VXLAN_N_VID),
  328. };
  329. vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
  330. }
  331. static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
  332. {
  333. struct vxlan_fdb f = {
  334. .state = NUD_STALE,
  335. };
  336. struct vxlan_rdst remote = { };
  337. memcpy(f.eth_addr, eth_addr, ETH_ALEN);
  338. vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
  339. }
  340. /* Hash Ethernet address */
  341. static u32 eth_hash(const unsigned char *addr)
  342. {
  343. u64 value = get_unaligned((u64 *)addr);
  344. /* only want 6 bytes */
  345. #ifdef __BIG_ENDIAN
  346. value >>= 16;
  347. #else
  348. value <<= 16;
  349. #endif
  350. return hash_64(value, FDB_HASH_BITS);
  351. }
  352. static u32 eth_vni_hash(const unsigned char *addr, __be32 vni)
  353. {
  354. /* use 1 byte of OUI and 3 bytes of NIC */
  355. u32 key = get_unaligned((u32 *)(addr + 2));
  356. return jhash_2words(key, vni, vxlan_salt) & (FDB_HASH_SIZE - 1);
  357. }
  358. /* Hash chain to use given mac address */
  359. static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
  360. const u8 *mac, __be32 vni)
  361. {
  362. if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA)
  363. return &vxlan->fdb_head[eth_vni_hash(mac, vni)];
  364. else
  365. return &vxlan->fdb_head[eth_hash(mac)];
  366. }
  367. /* Look up Ethernet address in forwarding table */
  368. static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
  369. const u8 *mac, __be32 vni)
  370. {
  371. struct hlist_head *head = vxlan_fdb_head(vxlan, mac, vni);
  372. struct vxlan_fdb *f;
  373. hlist_for_each_entry_rcu(f, head, hlist) {
  374. if (ether_addr_equal(mac, f->eth_addr)) {
  375. if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) {
  376. if (vni == f->vni)
  377. return f;
  378. } else {
  379. return f;
  380. }
  381. }
  382. }
  383. return NULL;
  384. }
  385. static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
  386. const u8 *mac, __be32 vni)
  387. {
  388. struct vxlan_fdb *f;
  389. f = __vxlan_find_mac(vxlan, mac, vni);
  390. if (f)
  391. f->used = jiffies;
  392. return f;
  393. }
  394. /* caller should hold vxlan->hash_lock */
  395. static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
  396. union vxlan_addr *ip, __be16 port,
  397. __be32 vni, __u32 ifindex)
  398. {
  399. struct vxlan_rdst *rd;
  400. list_for_each_entry(rd, &f->remotes, list) {
  401. if (vxlan_addr_equal(&rd->remote_ip, ip) &&
  402. rd->remote_port == port &&
  403. rd->remote_vni == vni &&
  404. rd->remote_ifindex == ifindex)
  405. return rd;
  406. }
  407. return NULL;
  408. }
  409. /* Replace destination of unicast mac */
  410. static int vxlan_fdb_replace(struct vxlan_fdb *f,
  411. union vxlan_addr *ip, __be16 port, __be32 vni,
  412. __u32 ifindex)
  413. {
  414. struct vxlan_rdst *rd;
  415. rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
  416. if (rd)
  417. return 0;
  418. rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
  419. if (!rd)
  420. return 0;
  421. dst_cache_reset(&rd->dst_cache);
  422. rd->remote_ip = *ip;
  423. rd->remote_port = port;
  424. rd->remote_vni = vni;
  425. rd->remote_ifindex = ifindex;
  426. return 1;
  427. }
  428. /* Add/update destinations for multicast */
  429. static int vxlan_fdb_append(struct vxlan_fdb *f,
  430. union vxlan_addr *ip, __be16 port, __be32 vni,
  431. __u32 ifindex, struct vxlan_rdst **rdp)
  432. {
  433. struct vxlan_rdst *rd;
  434. rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
  435. if (rd)
  436. return 0;
  437. rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
  438. if (rd == NULL)
  439. return -ENOBUFS;
  440. if (dst_cache_init(&rd->dst_cache, GFP_ATOMIC)) {
  441. kfree(rd);
  442. return -ENOBUFS;
  443. }
  444. rd->remote_ip = *ip;
  445. rd->remote_port = port;
  446. rd->remote_vni = vni;
  447. rd->remote_ifindex = ifindex;
  448. list_add_tail_rcu(&rd->list, &f->remotes);
  449. *rdp = rd;
  450. return 1;
  451. }
  452. static struct vxlanhdr *vxlan_gro_remcsum(struct sk_buff *skb,
  453. unsigned int off,
  454. struct vxlanhdr *vh, size_t hdrlen,
  455. __be32 vni_field,
  456. struct gro_remcsum *grc,
  457. bool nopartial)
  458. {
  459. size_t start, offset;
  460. if (skb->remcsum_offload)
  461. return vh;
  462. if (!NAPI_GRO_CB(skb)->csum_valid)
  463. return NULL;
  464. start = vxlan_rco_start(vni_field);
  465. offset = start + vxlan_rco_offset(vni_field);
  466. vh = skb_gro_remcsum_process(skb, (void *)vh, off, hdrlen,
  467. start, offset, grc, nopartial);
  468. skb->remcsum_offload = 1;
  469. return vh;
  470. }
  471. static struct sk_buff *vxlan_gro_receive(struct sock *sk,
  472. struct list_head *head,
  473. struct sk_buff *skb)
  474. {
  475. struct sk_buff *pp = NULL;
  476. struct sk_buff *p;
  477. struct vxlanhdr *vh, *vh2;
  478. unsigned int hlen, off_vx;
  479. int flush = 1;
  480. struct vxlan_sock *vs = rcu_dereference_sk_user_data(sk);
  481. __be32 flags;
  482. struct gro_remcsum grc;
  483. skb_gro_remcsum_init(&grc);
  484. off_vx = skb_gro_offset(skb);
  485. hlen = off_vx + sizeof(*vh);
  486. vh = skb_gro_header_fast(skb, off_vx);
  487. if (skb_gro_header_hard(skb, hlen)) {
  488. vh = skb_gro_header_slow(skb, hlen, off_vx);
  489. if (unlikely(!vh))
  490. goto out;
  491. }
  492. skb_gro_postpull_rcsum(skb, vh, sizeof(struct vxlanhdr));
  493. flags = vh->vx_flags;
  494. if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) {
  495. vh = vxlan_gro_remcsum(skb, off_vx, vh, sizeof(struct vxlanhdr),
  496. vh->vx_vni, &grc,
  497. !!(vs->flags &
  498. VXLAN_F_REMCSUM_NOPARTIAL));
  499. if (!vh)
  500. goto out;
  501. }
  502. skb_gro_pull(skb, sizeof(struct vxlanhdr)); /* pull vxlan header */
  503. list_for_each_entry(p, head, list) {
  504. if (!NAPI_GRO_CB(p)->same_flow)
  505. continue;
  506. vh2 = (struct vxlanhdr *)(p->data + off_vx);
  507. if (vh->vx_flags != vh2->vx_flags ||
  508. vh->vx_vni != vh2->vx_vni) {
  509. NAPI_GRO_CB(p)->same_flow = 0;
  510. continue;
  511. }
  512. }
  513. pp = call_gro_receive(eth_gro_receive, head, skb);
  514. flush = 0;
  515. out:
  516. skb_gro_remcsum_cleanup(skb, &grc);
  517. skb->remcsum_offload = 0;
  518. NAPI_GRO_CB(skb)->flush |= flush;
  519. return pp;
  520. }
  521. static int vxlan_gro_complete(struct sock *sk, struct sk_buff *skb, int nhoff)
  522. {
  523. /* Sets 'skb->inner_mac_header' since we are always called with
  524. * 'skb->encapsulation' set.
  525. */
  526. return eth_gro_complete(skb, nhoff + sizeof(struct vxlanhdr));
  527. }
  528. /* Add new entry to forwarding table -- assumes lock held */
  529. static int vxlan_fdb_create(struct vxlan_dev *vxlan,
  530. const u8 *mac, union vxlan_addr *ip,
  531. __u16 state, __u16 flags,
  532. __be16 port, __be32 src_vni, __be32 vni,
  533. __u32 ifindex, __u8 ndm_flags)
  534. {
  535. struct vxlan_rdst *rd = NULL;
  536. struct vxlan_fdb *f;
  537. int notify = 0;
  538. int rc;
  539. f = __vxlan_find_mac(vxlan, mac, src_vni);
  540. if (f) {
  541. if (flags & NLM_F_EXCL) {
  542. netdev_dbg(vxlan->dev,
  543. "lost race to create %pM\n", mac);
  544. return -EEXIST;
  545. }
  546. if (f->state != state) {
  547. f->state = state;
  548. f->updated = jiffies;
  549. notify = 1;
  550. }
  551. if (f->flags != ndm_flags) {
  552. f->flags = ndm_flags;
  553. f->updated = jiffies;
  554. notify = 1;
  555. }
  556. if ((flags & NLM_F_REPLACE)) {
  557. /* Only change unicasts */
  558. if (!(is_multicast_ether_addr(f->eth_addr) ||
  559. is_zero_ether_addr(f->eth_addr))) {
  560. notify |= vxlan_fdb_replace(f, ip, port, vni,
  561. ifindex);
  562. } else
  563. return -EOPNOTSUPP;
  564. }
  565. if ((flags & NLM_F_APPEND) &&
  566. (is_multicast_ether_addr(f->eth_addr) ||
  567. is_zero_ether_addr(f->eth_addr))) {
  568. rc = vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
  569. if (rc < 0)
  570. return rc;
  571. notify |= rc;
  572. }
  573. } else {
  574. if (!(flags & NLM_F_CREATE))
  575. return -ENOENT;
  576. if (vxlan->cfg.addrmax &&
  577. vxlan->addrcnt >= vxlan->cfg.addrmax)
  578. return -ENOSPC;
  579. /* Disallow replace to add a multicast entry */
  580. if ((flags & NLM_F_REPLACE) &&
  581. (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
  582. return -EOPNOTSUPP;
  583. netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
  584. f = kmalloc(sizeof(*f), GFP_ATOMIC);
  585. if (!f)
  586. return -ENOMEM;
  587. notify = 1;
  588. f->state = state;
  589. f->flags = ndm_flags;
  590. f->updated = f->used = jiffies;
  591. f->vni = src_vni;
  592. INIT_LIST_HEAD(&f->remotes);
  593. memcpy(f->eth_addr, mac, ETH_ALEN);
  594. rc = vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
  595. if (rc < 0) {
  596. kfree(f);
  597. return rc;
  598. }
  599. ++vxlan->addrcnt;
  600. hlist_add_head_rcu(&f->hlist,
  601. vxlan_fdb_head(vxlan, mac, src_vni));
  602. }
  603. if (notify) {
  604. if (rd == NULL)
  605. rd = first_remote_rtnl(f);
  606. vxlan_fdb_notify(vxlan, f, rd, RTM_NEWNEIGH);
  607. }
  608. return 0;
  609. }
  610. static void vxlan_fdb_free(struct rcu_head *head)
  611. {
  612. struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
  613. struct vxlan_rdst *rd, *nd;
  614. list_for_each_entry_safe(rd, nd, &f->remotes, list) {
  615. dst_cache_destroy(&rd->dst_cache);
  616. kfree(rd);
  617. }
  618. kfree(f);
  619. }
  620. static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
  621. {
  622. netdev_dbg(vxlan->dev,
  623. "delete %pM\n", f->eth_addr);
  624. --vxlan->addrcnt;
  625. vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_DELNEIGH);
  626. hlist_del_rcu(&f->hlist);
  627. call_rcu(&f->rcu, vxlan_fdb_free);
  628. }
  629. static void vxlan_dst_free(struct rcu_head *head)
  630. {
  631. struct vxlan_rdst *rd = container_of(head, struct vxlan_rdst, rcu);
  632. dst_cache_destroy(&rd->dst_cache);
  633. kfree(rd);
  634. }
  635. static void vxlan_fdb_dst_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f,
  636. struct vxlan_rdst *rd)
  637. {
  638. list_del_rcu(&rd->list);
  639. vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH);
  640. call_rcu(&rd->rcu, vxlan_dst_free);
  641. }
  642. static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
  643. union vxlan_addr *ip, __be16 *port, __be32 *src_vni,
  644. __be32 *vni, u32 *ifindex)
  645. {
  646. struct net *net = dev_net(vxlan->dev);
  647. int err;
  648. if (tb[NDA_DST]) {
  649. err = vxlan_nla_get_addr(ip, tb[NDA_DST]);
  650. if (err)
  651. return err;
  652. } else {
  653. union vxlan_addr *remote = &vxlan->default_dst.remote_ip;
  654. if (remote->sa.sa_family == AF_INET) {
  655. ip->sin.sin_addr.s_addr = htonl(INADDR_ANY);
  656. ip->sa.sa_family = AF_INET;
  657. #if IS_ENABLED(CONFIG_IPV6)
  658. } else {
  659. ip->sin6.sin6_addr = in6addr_any;
  660. ip->sa.sa_family = AF_INET6;
  661. #endif
  662. }
  663. }
  664. if (tb[NDA_PORT]) {
  665. if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
  666. return -EINVAL;
  667. *port = nla_get_be16(tb[NDA_PORT]);
  668. } else {
  669. *port = vxlan->cfg.dst_port;
  670. }
  671. if (tb[NDA_VNI]) {
  672. if (nla_len(tb[NDA_VNI]) != sizeof(u32))
  673. return -EINVAL;
  674. *vni = cpu_to_be32(nla_get_u32(tb[NDA_VNI]));
  675. } else {
  676. *vni = vxlan->default_dst.remote_vni;
  677. }
  678. if (tb[NDA_SRC_VNI]) {
  679. if (nla_len(tb[NDA_SRC_VNI]) != sizeof(u32))
  680. return -EINVAL;
  681. *src_vni = cpu_to_be32(nla_get_u32(tb[NDA_SRC_VNI]));
  682. } else {
  683. *src_vni = vxlan->default_dst.remote_vni;
  684. }
  685. if (tb[NDA_IFINDEX]) {
  686. struct net_device *tdev;
  687. if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
  688. return -EINVAL;
  689. *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
  690. tdev = __dev_get_by_index(net, *ifindex);
  691. if (!tdev)
  692. return -EADDRNOTAVAIL;
  693. } else {
  694. *ifindex = 0;
  695. }
  696. return 0;
  697. }
  698. /* Add static entry (via netlink) */
  699. static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
  700. struct net_device *dev,
  701. const unsigned char *addr, u16 vid, u16 flags)
  702. {
  703. struct vxlan_dev *vxlan = netdev_priv(dev);
  704. /* struct net *net = dev_net(vxlan->dev); */
  705. union vxlan_addr ip;
  706. __be16 port;
  707. __be32 src_vni, vni;
  708. u32 ifindex;
  709. int err;
  710. if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
  711. pr_info("RTM_NEWNEIGH with invalid state %#x\n",
  712. ndm->ndm_state);
  713. return -EINVAL;
  714. }
  715. if (tb[NDA_DST] == NULL)
  716. return -EINVAL;
  717. err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &src_vni, &vni, &ifindex);
  718. if (err)
  719. return err;
  720. if (vxlan->default_dst.remote_ip.sa.sa_family != ip.sa.sa_family)
  721. return -EAFNOSUPPORT;
  722. spin_lock_bh(&vxlan->hash_lock);
  723. err = vxlan_fdb_create(vxlan, addr, &ip, ndm->ndm_state, flags,
  724. port, src_vni, vni, ifindex, ndm->ndm_flags);
  725. spin_unlock_bh(&vxlan->hash_lock);
  726. return err;
  727. }
  728. static int __vxlan_fdb_delete(struct vxlan_dev *vxlan,
  729. const unsigned char *addr, union vxlan_addr ip,
  730. __be16 port, __be32 src_vni, __be32 vni,
  731. u32 ifindex, u16 vid)
  732. {
  733. struct vxlan_fdb *f;
  734. struct vxlan_rdst *rd = NULL;
  735. int err = -ENOENT;
  736. f = vxlan_find_mac(vxlan, addr, src_vni);
  737. if (!f)
  738. return err;
  739. if (!vxlan_addr_any(&ip)) {
  740. rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex);
  741. if (!rd)
  742. goto out;
  743. }
  744. /* remove a destination if it's not the only one on the list,
  745. * otherwise destroy the fdb entry
  746. */
  747. if (rd && !list_is_singular(&f->remotes)) {
  748. vxlan_fdb_dst_destroy(vxlan, f, rd);
  749. goto out;
  750. }
  751. vxlan_fdb_destroy(vxlan, f);
  752. out:
  753. return 0;
  754. }
  755. /* Delete entry (via netlink) */
  756. static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
  757. struct net_device *dev,
  758. const unsigned char *addr, u16 vid)
  759. {
  760. struct vxlan_dev *vxlan = netdev_priv(dev);
  761. union vxlan_addr ip;
  762. __be32 src_vni, vni;
  763. __be16 port;
  764. u32 ifindex;
  765. int err;
  766. err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &src_vni, &vni, &ifindex);
  767. if (err)
  768. return err;
  769. spin_lock_bh(&vxlan->hash_lock);
  770. err = __vxlan_fdb_delete(vxlan, addr, ip, port, src_vni, vni, ifindex,
  771. vid);
  772. spin_unlock_bh(&vxlan->hash_lock);
  773. return err;
  774. }
  775. /* Dump forwarding table */
  776. static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
  777. struct net_device *dev,
  778. struct net_device *filter_dev, int *idx)
  779. {
  780. struct vxlan_dev *vxlan = netdev_priv(dev);
  781. unsigned int h;
  782. int err = 0;
  783. for (h = 0; h < FDB_HASH_SIZE; ++h) {
  784. struct vxlan_fdb *f;
  785. hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
  786. struct vxlan_rdst *rd;
  787. list_for_each_entry_rcu(rd, &f->remotes, list) {
  788. if (*idx < cb->args[2])
  789. goto skip;
  790. err = vxlan_fdb_info(skb, vxlan, f,
  791. NETLINK_CB(cb->skb).portid,
  792. cb->nlh->nlmsg_seq,
  793. RTM_NEWNEIGH,
  794. NLM_F_MULTI, rd);
  795. if (err < 0)
  796. goto out;
  797. skip:
  798. *idx += 1;
  799. }
  800. }
  801. }
  802. out:
  803. return err;
  804. }
  805. /* Watch incoming packets to learn mapping between Ethernet address
  806. * and Tunnel endpoint.
  807. * Return true if packet is bogus and should be dropped.
  808. */
  809. static bool vxlan_snoop(struct net_device *dev,
  810. union vxlan_addr *src_ip, const u8 *src_mac,
  811. u32 src_ifindex, __be32 vni)
  812. {
  813. struct vxlan_dev *vxlan = netdev_priv(dev);
  814. struct vxlan_fdb *f;
  815. u32 ifindex = 0;
  816. #if IS_ENABLED(CONFIG_IPV6)
  817. if (src_ip->sa.sa_family == AF_INET6 &&
  818. (ipv6_addr_type(&src_ip->sin6.sin6_addr) & IPV6_ADDR_LINKLOCAL))
  819. ifindex = src_ifindex;
  820. #endif
  821. f = vxlan_find_mac(vxlan, src_mac, vni);
  822. if (likely(f)) {
  823. struct vxlan_rdst *rdst = first_remote_rcu(f);
  824. if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip) &&
  825. rdst->remote_ifindex == ifindex))
  826. return false;
  827. /* Don't migrate static entries, drop packets */
  828. if (f->state & (NUD_PERMANENT | NUD_NOARP))
  829. return true;
  830. if (net_ratelimit())
  831. netdev_info(dev,
  832. "%pM migrated from %pIS to %pIS\n",
  833. src_mac, &rdst->remote_ip.sa, &src_ip->sa);
  834. rdst->remote_ip = *src_ip;
  835. f->updated = jiffies;
  836. vxlan_fdb_notify(vxlan, f, rdst, RTM_NEWNEIGH);
  837. } else {
  838. /* learned new entry */
  839. spin_lock(&vxlan->hash_lock);
  840. /* close off race between vxlan_flush and incoming packets */
  841. if (netif_running(dev))
  842. vxlan_fdb_create(vxlan, src_mac, src_ip,
  843. NUD_REACHABLE,
  844. NLM_F_EXCL|NLM_F_CREATE,
  845. vxlan->cfg.dst_port,
  846. vni,
  847. vxlan->default_dst.remote_vni,
  848. ifindex, NTF_SELF);
  849. spin_unlock(&vxlan->hash_lock);
  850. }
  851. return false;
  852. }
  853. /* See if multicast group is already in use by other ID */
  854. static bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev)
  855. {
  856. struct vxlan_dev *vxlan;
  857. struct vxlan_sock *sock4;
  858. #if IS_ENABLED(CONFIG_IPV6)
  859. struct vxlan_sock *sock6;
  860. #endif
  861. unsigned short family = dev->default_dst.remote_ip.sa.sa_family;
  862. sock4 = rtnl_dereference(dev->vn4_sock);
  863. /* The vxlan_sock is only used by dev, leaving group has
  864. * no effect on other vxlan devices.
  865. */
  866. if (family == AF_INET && sock4 && refcount_read(&sock4->refcnt) == 1)
  867. return false;
  868. #if IS_ENABLED(CONFIG_IPV6)
  869. sock6 = rtnl_dereference(dev->vn6_sock);
  870. if (family == AF_INET6 && sock6 && refcount_read(&sock6->refcnt) == 1)
  871. return false;
  872. #endif
  873. list_for_each_entry(vxlan, &vn->vxlan_list, next) {
  874. if (!netif_running(vxlan->dev) || vxlan == dev)
  875. continue;
  876. if (family == AF_INET &&
  877. rtnl_dereference(vxlan->vn4_sock) != sock4)
  878. continue;
  879. #if IS_ENABLED(CONFIG_IPV6)
  880. if (family == AF_INET6 &&
  881. rtnl_dereference(vxlan->vn6_sock) != sock6)
  882. continue;
  883. #endif
  884. if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip,
  885. &dev->default_dst.remote_ip))
  886. continue;
  887. if (vxlan->default_dst.remote_ifindex !=
  888. dev->default_dst.remote_ifindex)
  889. continue;
  890. return true;
  891. }
  892. return false;
  893. }
  894. static bool __vxlan_sock_release_prep(struct vxlan_sock *vs)
  895. {
  896. struct vxlan_net *vn;
  897. if (!vs)
  898. return false;
  899. if (!refcount_dec_and_test(&vs->refcnt))
  900. return false;
  901. vn = net_generic(sock_net(vs->sock->sk), vxlan_net_id);
  902. spin_lock(&vn->sock_lock);
  903. hlist_del_rcu(&vs->hlist);
  904. udp_tunnel_notify_del_rx_port(vs->sock,
  905. (vs->flags & VXLAN_F_GPE) ?
  906. UDP_TUNNEL_TYPE_VXLAN_GPE :
  907. UDP_TUNNEL_TYPE_VXLAN);
  908. spin_unlock(&vn->sock_lock);
  909. return true;
  910. }
  911. static void vxlan_sock_release(struct vxlan_dev *vxlan)
  912. {
  913. struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
  914. #if IS_ENABLED(CONFIG_IPV6)
  915. struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
  916. RCU_INIT_POINTER(vxlan->vn6_sock, NULL);
  917. #endif
  918. RCU_INIT_POINTER(vxlan->vn4_sock, NULL);
  919. synchronize_net();
  920. vxlan_vs_del_dev(vxlan);
  921. if (__vxlan_sock_release_prep(sock4)) {
  922. udp_tunnel_sock_release(sock4->sock);
  923. kfree(sock4);
  924. }
  925. #if IS_ENABLED(CONFIG_IPV6)
  926. if (__vxlan_sock_release_prep(sock6)) {
  927. udp_tunnel_sock_release(sock6->sock);
  928. kfree(sock6);
  929. }
  930. #endif
  931. }
  932. /* Update multicast group membership when first VNI on
  933. * multicast address is brought up
  934. */
  935. static int vxlan_igmp_join(struct vxlan_dev *vxlan)
  936. {
  937. struct sock *sk;
  938. union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
  939. int ifindex = vxlan->default_dst.remote_ifindex;
  940. int ret = -EINVAL;
  941. if (ip->sa.sa_family == AF_INET) {
  942. struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
  943. struct ip_mreqn mreq = {
  944. .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
  945. .imr_ifindex = ifindex,
  946. };
  947. sk = sock4->sock->sk;
  948. lock_sock(sk);
  949. ret = ip_mc_join_group(sk, &mreq);
  950. release_sock(sk);
  951. #if IS_ENABLED(CONFIG_IPV6)
  952. } else {
  953. struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
  954. sk = sock6->sock->sk;
  955. lock_sock(sk);
  956. ret = ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
  957. &ip->sin6.sin6_addr);
  958. release_sock(sk);
  959. #endif
  960. }
  961. return ret;
  962. }
  963. /* Inverse of vxlan_igmp_join when last VNI is brought down */
  964. static int vxlan_igmp_leave(struct vxlan_dev *vxlan)
  965. {
  966. struct sock *sk;
  967. union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
  968. int ifindex = vxlan->default_dst.remote_ifindex;
  969. int ret = -EINVAL;
  970. if (ip->sa.sa_family == AF_INET) {
  971. struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
  972. struct ip_mreqn mreq = {
  973. .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
  974. .imr_ifindex = ifindex,
  975. };
  976. sk = sock4->sock->sk;
  977. lock_sock(sk);
  978. ret = ip_mc_leave_group(sk, &mreq);
  979. release_sock(sk);
  980. #if IS_ENABLED(CONFIG_IPV6)
  981. } else {
  982. struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
  983. sk = sock6->sock->sk;
  984. lock_sock(sk);
  985. ret = ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
  986. &ip->sin6.sin6_addr);
  987. release_sock(sk);
  988. #endif
  989. }
  990. return ret;
  991. }
  992. static bool vxlan_remcsum(struct vxlanhdr *unparsed,
  993. struct sk_buff *skb, u32 vxflags)
  994. {
  995. size_t start, offset;
  996. if (!(unparsed->vx_flags & VXLAN_HF_RCO) || skb->remcsum_offload)
  997. goto out;
  998. start = vxlan_rco_start(unparsed->vx_vni);
  999. offset = start + vxlan_rco_offset(unparsed->vx_vni);
  1000. if (!pskb_may_pull(skb, offset + sizeof(u16)))
  1001. return false;
  1002. skb_remcsum_process(skb, (void *)(vxlan_hdr(skb) + 1), start, offset,
  1003. !!(vxflags & VXLAN_F_REMCSUM_NOPARTIAL));
  1004. out:
  1005. unparsed->vx_flags &= ~VXLAN_HF_RCO;
  1006. unparsed->vx_vni &= VXLAN_VNI_MASK;
  1007. return true;
  1008. }
  1009. static void vxlan_parse_gbp_hdr(struct vxlanhdr *unparsed,
  1010. struct sk_buff *skb, u32 vxflags,
  1011. struct vxlan_metadata *md)
  1012. {
  1013. struct vxlanhdr_gbp *gbp = (struct vxlanhdr_gbp *)unparsed;
  1014. struct metadata_dst *tun_dst;
  1015. if (!(unparsed->vx_flags & VXLAN_HF_GBP))
  1016. goto out;
  1017. md->gbp = ntohs(gbp->policy_id);
  1018. tun_dst = (struct metadata_dst *)skb_dst(skb);
  1019. if (tun_dst) {
  1020. tun_dst->u.tun_info.key.tun_flags |= TUNNEL_VXLAN_OPT;
  1021. tun_dst->u.tun_info.options_len = sizeof(*md);
  1022. }
  1023. if (gbp->dont_learn)
  1024. md->gbp |= VXLAN_GBP_DONT_LEARN;
  1025. if (gbp->policy_applied)
  1026. md->gbp |= VXLAN_GBP_POLICY_APPLIED;
  1027. /* In flow-based mode, GBP is carried in dst_metadata */
  1028. if (!(vxflags & VXLAN_F_COLLECT_METADATA))
  1029. skb->mark = md->gbp;
  1030. out:
  1031. unparsed->vx_flags &= ~VXLAN_GBP_USED_BITS;
  1032. }
  1033. static bool vxlan_parse_gpe_hdr(struct vxlanhdr *unparsed,
  1034. __be16 *protocol,
  1035. struct sk_buff *skb, u32 vxflags)
  1036. {
  1037. struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)unparsed;
  1038. /* Need to have Next Protocol set for interfaces in GPE mode. */
  1039. if (!gpe->np_applied)
  1040. return false;
  1041. /* "The initial version is 0. If a receiver does not support the
  1042. * version indicated it MUST drop the packet.
  1043. */
  1044. if (gpe->version != 0)
  1045. return false;
  1046. /* "When the O bit is set to 1, the packet is an OAM packet and OAM
  1047. * processing MUST occur." However, we don't implement OAM
  1048. * processing, thus drop the packet.
  1049. */
  1050. if (gpe->oam_flag)
  1051. return false;
  1052. *protocol = tun_p_to_eth_p(gpe->next_protocol);
  1053. if (!*protocol)
  1054. return false;
  1055. unparsed->vx_flags &= ~VXLAN_GPE_USED_BITS;
  1056. return true;
  1057. }
  1058. static bool vxlan_set_mac(struct vxlan_dev *vxlan,
  1059. struct vxlan_sock *vs,
  1060. struct sk_buff *skb, __be32 vni)
  1061. {
  1062. union vxlan_addr saddr;
  1063. u32 ifindex = skb->dev->ifindex;
  1064. skb_reset_mac_header(skb);
  1065. skb->protocol = eth_type_trans(skb, vxlan->dev);
  1066. skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
  1067. /* Ignore packet loops (and multicast echo) */
  1068. if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr))
  1069. return false;
  1070. /* Get address from the outer IP header */
  1071. if (vxlan_get_sk_family(vs) == AF_INET) {
  1072. saddr.sin.sin_addr.s_addr = ip_hdr(skb)->saddr;
  1073. saddr.sa.sa_family = AF_INET;
  1074. #if IS_ENABLED(CONFIG_IPV6)
  1075. } else {
  1076. saddr.sin6.sin6_addr = ipv6_hdr(skb)->saddr;
  1077. saddr.sa.sa_family = AF_INET6;
  1078. #endif
  1079. }
  1080. if ((vxlan->cfg.flags & VXLAN_F_LEARN) &&
  1081. vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source, ifindex, vni))
  1082. return false;
  1083. return true;
  1084. }
  1085. static bool vxlan_ecn_decapsulate(struct vxlan_sock *vs, void *oiph,
  1086. struct sk_buff *skb)
  1087. {
  1088. int err = 0;
  1089. if (vxlan_get_sk_family(vs) == AF_INET)
  1090. err = IP_ECN_decapsulate(oiph, skb);
  1091. #if IS_ENABLED(CONFIG_IPV6)
  1092. else
  1093. err = IP6_ECN_decapsulate(oiph, skb);
  1094. #endif
  1095. if (unlikely(err) && log_ecn_error) {
  1096. if (vxlan_get_sk_family(vs) == AF_INET)
  1097. net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
  1098. &((struct iphdr *)oiph)->saddr,
  1099. ((struct iphdr *)oiph)->tos);
  1100. else
  1101. net_info_ratelimited("non-ECT from %pI6\n",
  1102. &((struct ipv6hdr *)oiph)->saddr);
  1103. }
  1104. return err <= 1;
  1105. }
  1106. /* Callback from net/ipv4/udp.c to receive packets */
  1107. static int vxlan_rcv(struct sock *sk, struct sk_buff *skb)
  1108. {
  1109. struct pcpu_sw_netstats *stats;
  1110. struct vxlan_dev *vxlan;
  1111. struct vxlan_sock *vs;
  1112. struct vxlanhdr unparsed;
  1113. struct vxlan_metadata _md;
  1114. struct vxlan_metadata *md = &_md;
  1115. __be16 protocol = htons(ETH_P_TEB);
  1116. bool raw_proto = false;
  1117. void *oiph;
  1118. __be32 vni = 0;
  1119. /* Need UDP and VXLAN header to be present */
  1120. if (!pskb_may_pull(skb, VXLAN_HLEN))
  1121. goto drop;
  1122. unparsed = *vxlan_hdr(skb);
  1123. /* VNI flag always required to be set */
  1124. if (!(unparsed.vx_flags & VXLAN_HF_VNI)) {
  1125. netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
  1126. ntohl(vxlan_hdr(skb)->vx_flags),
  1127. ntohl(vxlan_hdr(skb)->vx_vni));
  1128. /* Return non vxlan pkt */
  1129. goto drop;
  1130. }
  1131. unparsed.vx_flags &= ~VXLAN_HF_VNI;
  1132. unparsed.vx_vni &= ~VXLAN_VNI_MASK;
  1133. vs = rcu_dereference_sk_user_data(sk);
  1134. if (!vs)
  1135. goto drop;
  1136. vni = vxlan_vni(vxlan_hdr(skb)->vx_vni);
  1137. vxlan = vxlan_vs_find_vni(vs, skb->dev->ifindex, vni);
  1138. if (!vxlan)
  1139. goto drop;
  1140. /* For backwards compatibility, only allow reserved fields to be
  1141. * used by VXLAN extensions if explicitly requested.
  1142. */
  1143. if (vs->flags & VXLAN_F_GPE) {
  1144. if (!vxlan_parse_gpe_hdr(&unparsed, &protocol, skb, vs->flags))
  1145. goto drop;
  1146. raw_proto = true;
  1147. }
  1148. if (__iptunnel_pull_header(skb, VXLAN_HLEN, protocol, raw_proto,
  1149. !net_eq(vxlan->net, dev_net(vxlan->dev))))
  1150. goto drop;
  1151. if (vxlan_collect_metadata(vs)) {
  1152. struct metadata_dst *tun_dst;
  1153. tun_dst = udp_tun_rx_dst(skb, vxlan_get_sk_family(vs), TUNNEL_KEY,
  1154. key32_to_tunnel_id(vni), sizeof(*md));
  1155. if (!tun_dst)
  1156. goto drop;
  1157. md = ip_tunnel_info_opts(&tun_dst->u.tun_info);
  1158. skb_dst_set(skb, (struct dst_entry *)tun_dst);
  1159. } else {
  1160. memset(md, 0, sizeof(*md));
  1161. }
  1162. if (vs->flags & VXLAN_F_REMCSUM_RX)
  1163. if (!vxlan_remcsum(&unparsed, skb, vs->flags))
  1164. goto drop;
  1165. if (vs->flags & VXLAN_F_GBP)
  1166. vxlan_parse_gbp_hdr(&unparsed, skb, vs->flags, md);
  1167. /* Note that GBP and GPE can never be active together. This is
  1168. * ensured in vxlan_dev_configure.
  1169. */
  1170. if (unparsed.vx_flags || unparsed.vx_vni) {
  1171. /* If there are any unprocessed flags remaining treat
  1172. * this as a malformed packet. This behavior diverges from
  1173. * VXLAN RFC (RFC7348) which stipulates that bits in reserved
  1174. * in reserved fields are to be ignored. The approach here
  1175. * maintains compatibility with previous stack code, and also
  1176. * is more robust and provides a little more security in
  1177. * adding extensions to VXLAN.
  1178. */
  1179. goto drop;
  1180. }
  1181. if (!raw_proto) {
  1182. if (!vxlan_set_mac(vxlan, vs, skb, vni))
  1183. goto drop;
  1184. } else {
  1185. skb_reset_mac_header(skb);
  1186. skb->dev = vxlan->dev;
  1187. skb->pkt_type = PACKET_HOST;
  1188. }
  1189. oiph = skb_network_header(skb);
  1190. skb_reset_network_header(skb);
  1191. if (!vxlan_ecn_decapsulate(vs, oiph, skb)) {
  1192. ++vxlan->dev->stats.rx_frame_errors;
  1193. ++vxlan->dev->stats.rx_errors;
  1194. goto drop;
  1195. }
  1196. stats = this_cpu_ptr(vxlan->dev->tstats);
  1197. u64_stats_update_begin(&stats->syncp);
  1198. stats->rx_packets++;
  1199. stats->rx_bytes += skb->len;
  1200. u64_stats_update_end(&stats->syncp);
  1201. gro_cells_receive(&vxlan->gro_cells, skb);
  1202. return 0;
  1203. drop:
  1204. /* Consume bad packet */
  1205. kfree_skb(skb);
  1206. return 0;
  1207. }
  1208. static int arp_reduce(struct net_device *dev, struct sk_buff *skb, __be32 vni)
  1209. {
  1210. struct vxlan_dev *vxlan = netdev_priv(dev);
  1211. struct arphdr *parp;
  1212. u8 *arpptr, *sha;
  1213. __be32 sip, tip;
  1214. struct neighbour *n;
  1215. if (dev->flags & IFF_NOARP)
  1216. goto out;
  1217. if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
  1218. dev->stats.tx_dropped++;
  1219. goto out;
  1220. }
  1221. parp = arp_hdr(skb);
  1222. if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
  1223. parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
  1224. parp->ar_pro != htons(ETH_P_IP) ||
  1225. parp->ar_op != htons(ARPOP_REQUEST) ||
  1226. parp->ar_hln != dev->addr_len ||
  1227. parp->ar_pln != 4)
  1228. goto out;
  1229. arpptr = (u8 *)parp + sizeof(struct arphdr);
  1230. sha = arpptr;
  1231. arpptr += dev->addr_len; /* sha */
  1232. memcpy(&sip, arpptr, sizeof(sip));
  1233. arpptr += sizeof(sip);
  1234. arpptr += dev->addr_len; /* tha */
  1235. memcpy(&tip, arpptr, sizeof(tip));
  1236. if (ipv4_is_loopback(tip) ||
  1237. ipv4_is_multicast(tip))
  1238. goto out;
  1239. n = neigh_lookup(&arp_tbl, &tip, dev);
  1240. if (n) {
  1241. struct vxlan_fdb *f;
  1242. struct sk_buff *reply;
  1243. if (!(n->nud_state & NUD_CONNECTED)) {
  1244. neigh_release(n);
  1245. goto out;
  1246. }
  1247. f = vxlan_find_mac(vxlan, n->ha, vni);
  1248. if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
  1249. /* bridge-local neighbor */
  1250. neigh_release(n);
  1251. goto out;
  1252. }
  1253. reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
  1254. n->ha, sha);
  1255. neigh_release(n);
  1256. if (reply == NULL)
  1257. goto out;
  1258. skb_reset_mac_header(reply);
  1259. __skb_pull(reply, skb_network_offset(reply));
  1260. reply->ip_summed = CHECKSUM_UNNECESSARY;
  1261. reply->pkt_type = PACKET_HOST;
  1262. if (netif_rx_ni(reply) == NET_RX_DROP)
  1263. dev->stats.rx_dropped++;
  1264. } else if (vxlan->cfg.flags & VXLAN_F_L3MISS) {
  1265. union vxlan_addr ipa = {
  1266. .sin.sin_addr.s_addr = tip,
  1267. .sin.sin_family = AF_INET,
  1268. };
  1269. vxlan_ip_miss(dev, &ipa);
  1270. }
  1271. out:
  1272. consume_skb(skb);
  1273. return NETDEV_TX_OK;
  1274. }
  1275. #if IS_ENABLED(CONFIG_IPV6)
  1276. static struct sk_buff *vxlan_na_create(struct sk_buff *request,
  1277. struct neighbour *n, bool isrouter)
  1278. {
  1279. struct net_device *dev = request->dev;
  1280. struct sk_buff *reply;
  1281. struct nd_msg *ns, *na;
  1282. struct ipv6hdr *pip6;
  1283. u8 *daddr;
  1284. int na_olen = 8; /* opt hdr + ETH_ALEN for target */
  1285. int ns_olen;
  1286. int i, len;
  1287. if (dev == NULL || !pskb_may_pull(request, request->len))
  1288. return NULL;
  1289. len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) +
  1290. sizeof(*na) + na_olen + dev->needed_tailroom;
  1291. reply = alloc_skb(len, GFP_ATOMIC);
  1292. if (reply == NULL)
  1293. return NULL;
  1294. reply->protocol = htons(ETH_P_IPV6);
  1295. reply->dev = dev;
  1296. skb_reserve(reply, LL_RESERVED_SPACE(request->dev));
  1297. skb_push(reply, sizeof(struct ethhdr));
  1298. skb_reset_mac_header(reply);
  1299. ns = (struct nd_msg *)(ipv6_hdr(request) + 1);
  1300. daddr = eth_hdr(request)->h_source;
  1301. ns_olen = request->len - skb_network_offset(request) -
  1302. sizeof(struct ipv6hdr) - sizeof(*ns);
  1303. for (i = 0; i < ns_olen-1; i += (ns->opt[i+1]<<3)) {
  1304. if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) {
  1305. daddr = ns->opt + i + sizeof(struct nd_opt_hdr);
  1306. break;
  1307. }
  1308. }
  1309. /* Ethernet header */
  1310. ether_addr_copy(eth_hdr(reply)->h_dest, daddr);
  1311. ether_addr_copy(eth_hdr(reply)->h_source, n->ha);
  1312. eth_hdr(reply)->h_proto = htons(ETH_P_IPV6);
  1313. reply->protocol = htons(ETH_P_IPV6);
  1314. skb_pull(reply, sizeof(struct ethhdr));
  1315. skb_reset_network_header(reply);
  1316. skb_put(reply, sizeof(struct ipv6hdr));
  1317. /* IPv6 header */
  1318. pip6 = ipv6_hdr(reply);
  1319. memset(pip6, 0, sizeof(struct ipv6hdr));
  1320. pip6->version = 6;
  1321. pip6->priority = ipv6_hdr(request)->priority;
  1322. pip6->nexthdr = IPPROTO_ICMPV6;
  1323. pip6->hop_limit = 255;
  1324. pip6->daddr = ipv6_hdr(request)->saddr;
  1325. pip6->saddr = *(struct in6_addr *)n->primary_key;
  1326. skb_pull(reply, sizeof(struct ipv6hdr));
  1327. skb_reset_transport_header(reply);
  1328. /* Neighbor Advertisement */
  1329. na = skb_put_zero(reply, sizeof(*na) + na_olen);
  1330. na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
  1331. na->icmph.icmp6_router = isrouter;
  1332. na->icmph.icmp6_override = 1;
  1333. na->icmph.icmp6_solicited = 1;
  1334. na->target = ns->target;
  1335. ether_addr_copy(&na->opt[2], n->ha);
  1336. na->opt[0] = ND_OPT_TARGET_LL_ADDR;
  1337. na->opt[1] = na_olen >> 3;
  1338. na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr,
  1339. &pip6->daddr, sizeof(*na)+na_olen, IPPROTO_ICMPV6,
  1340. csum_partial(na, sizeof(*na)+na_olen, 0));
  1341. pip6->payload_len = htons(sizeof(*na)+na_olen);
  1342. skb_push(reply, sizeof(struct ipv6hdr));
  1343. reply->ip_summed = CHECKSUM_UNNECESSARY;
  1344. return reply;
  1345. }
  1346. static int neigh_reduce(struct net_device *dev, struct sk_buff *skb, __be32 vni)
  1347. {
  1348. struct vxlan_dev *vxlan = netdev_priv(dev);
  1349. const struct in6_addr *daddr;
  1350. const struct ipv6hdr *iphdr;
  1351. struct inet6_dev *in6_dev;
  1352. struct neighbour *n;
  1353. struct nd_msg *msg;
  1354. in6_dev = __in6_dev_get(dev);
  1355. if (!in6_dev)
  1356. goto out;
  1357. iphdr = ipv6_hdr(skb);
  1358. daddr = &iphdr->daddr;
  1359. msg = (struct nd_msg *)(iphdr + 1);
  1360. if (ipv6_addr_loopback(daddr) ||
  1361. ipv6_addr_is_multicast(&msg->target))
  1362. goto out;
  1363. n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, dev);
  1364. if (n) {
  1365. struct vxlan_fdb *f;
  1366. struct sk_buff *reply;
  1367. if (!(n->nud_state & NUD_CONNECTED)) {
  1368. neigh_release(n);
  1369. goto out;
  1370. }
  1371. f = vxlan_find_mac(vxlan, n->ha, vni);
  1372. if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
  1373. /* bridge-local neighbor */
  1374. neigh_release(n);
  1375. goto out;
  1376. }
  1377. reply = vxlan_na_create(skb, n,
  1378. !!(f ? f->flags & NTF_ROUTER : 0));
  1379. neigh_release(n);
  1380. if (reply == NULL)
  1381. goto out;
  1382. if (netif_rx_ni(reply) == NET_RX_DROP)
  1383. dev->stats.rx_dropped++;
  1384. } else if (vxlan->cfg.flags & VXLAN_F_L3MISS) {
  1385. union vxlan_addr ipa = {
  1386. .sin6.sin6_addr = msg->target,
  1387. .sin6.sin6_family = AF_INET6,
  1388. };
  1389. vxlan_ip_miss(dev, &ipa);
  1390. }
  1391. out:
  1392. consume_skb(skb);
  1393. return NETDEV_TX_OK;
  1394. }
  1395. #endif
  1396. static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
  1397. {
  1398. struct vxlan_dev *vxlan = netdev_priv(dev);
  1399. struct neighbour *n;
  1400. if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
  1401. return false;
  1402. n = NULL;
  1403. switch (ntohs(eth_hdr(skb)->h_proto)) {
  1404. case ETH_P_IP:
  1405. {
  1406. struct iphdr *pip;
  1407. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  1408. return false;
  1409. pip = ip_hdr(skb);
  1410. n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
  1411. if (!n && (vxlan->cfg.flags & VXLAN_F_L3MISS)) {
  1412. union vxlan_addr ipa = {
  1413. .sin.sin_addr.s_addr = pip->daddr,
  1414. .sin.sin_family = AF_INET,
  1415. };
  1416. vxlan_ip_miss(dev, &ipa);
  1417. return false;
  1418. }
  1419. break;
  1420. }
  1421. #if IS_ENABLED(CONFIG_IPV6)
  1422. case ETH_P_IPV6:
  1423. {
  1424. struct ipv6hdr *pip6;
  1425. if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
  1426. return false;
  1427. pip6 = ipv6_hdr(skb);
  1428. n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
  1429. if (!n && (vxlan->cfg.flags & VXLAN_F_L3MISS)) {
  1430. union vxlan_addr ipa = {
  1431. .sin6.sin6_addr = pip6->daddr,
  1432. .sin6.sin6_family = AF_INET6,
  1433. };
  1434. vxlan_ip_miss(dev, &ipa);
  1435. return false;
  1436. }
  1437. break;
  1438. }
  1439. #endif
  1440. default:
  1441. return false;
  1442. }
  1443. if (n) {
  1444. bool diff;
  1445. diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha);
  1446. if (diff) {
  1447. memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
  1448. dev->addr_len);
  1449. memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
  1450. }
  1451. neigh_release(n);
  1452. return diff;
  1453. }
  1454. return false;
  1455. }
  1456. static void vxlan_build_gbp_hdr(struct vxlanhdr *vxh, u32 vxflags,
  1457. struct vxlan_metadata *md)
  1458. {
  1459. struct vxlanhdr_gbp *gbp;
  1460. if (!md->gbp)
  1461. return;
  1462. gbp = (struct vxlanhdr_gbp *)vxh;
  1463. vxh->vx_flags |= VXLAN_HF_GBP;
  1464. if (md->gbp & VXLAN_GBP_DONT_LEARN)
  1465. gbp->dont_learn = 1;
  1466. if (md->gbp & VXLAN_GBP_POLICY_APPLIED)
  1467. gbp->policy_applied = 1;
  1468. gbp->policy_id = htons(md->gbp & VXLAN_GBP_ID_MASK);
  1469. }
  1470. static int vxlan_build_gpe_hdr(struct vxlanhdr *vxh, u32 vxflags,
  1471. __be16 protocol)
  1472. {
  1473. struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)vxh;
  1474. gpe->np_applied = 1;
  1475. gpe->next_protocol = tun_p_from_eth_p(protocol);
  1476. if (!gpe->next_protocol)
  1477. return -EPFNOSUPPORT;
  1478. return 0;
  1479. }
  1480. static int vxlan_build_skb(struct sk_buff *skb, struct dst_entry *dst,
  1481. int iphdr_len, __be32 vni,
  1482. struct vxlan_metadata *md, u32 vxflags,
  1483. bool udp_sum)
  1484. {
  1485. struct vxlanhdr *vxh;
  1486. int min_headroom;
  1487. int err;
  1488. int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
  1489. __be16 inner_protocol = htons(ETH_P_TEB);
  1490. if ((vxflags & VXLAN_F_REMCSUM_TX) &&
  1491. skb->ip_summed == CHECKSUM_PARTIAL) {
  1492. int csum_start = skb_checksum_start_offset(skb);
  1493. if (csum_start <= VXLAN_MAX_REMCSUM_START &&
  1494. !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
  1495. (skb->csum_offset == offsetof(struct udphdr, check) ||
  1496. skb->csum_offset == offsetof(struct tcphdr, check)))
  1497. type |= SKB_GSO_TUNNEL_REMCSUM;
  1498. }
  1499. min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
  1500. + VXLAN_HLEN + iphdr_len;
  1501. /* Need space for new headers (invalidates iph ptr) */
  1502. err = skb_cow_head(skb, min_headroom);
  1503. if (unlikely(err))
  1504. return err;
  1505. err = iptunnel_handle_offloads(skb, type);
  1506. if (err)
  1507. return err;
  1508. vxh = __skb_push(skb, sizeof(*vxh));
  1509. vxh->vx_flags = VXLAN_HF_VNI;
  1510. vxh->vx_vni = vxlan_vni_field(vni);
  1511. if (type & SKB_GSO_TUNNEL_REMCSUM) {
  1512. unsigned int start;
  1513. start = skb_checksum_start_offset(skb) - sizeof(struct vxlanhdr);
  1514. vxh->vx_vni |= vxlan_compute_rco(start, skb->csum_offset);
  1515. vxh->vx_flags |= VXLAN_HF_RCO;
  1516. if (!skb_is_gso(skb)) {
  1517. skb->ip_summed = CHECKSUM_NONE;
  1518. skb->encapsulation = 0;
  1519. }
  1520. }
  1521. if (vxflags & VXLAN_F_GBP)
  1522. vxlan_build_gbp_hdr(vxh, vxflags, md);
  1523. if (vxflags & VXLAN_F_GPE) {
  1524. err = vxlan_build_gpe_hdr(vxh, vxflags, skb->protocol);
  1525. if (err < 0)
  1526. return err;
  1527. inner_protocol = skb->protocol;
  1528. }
  1529. skb_set_inner_protocol(skb, inner_protocol);
  1530. return 0;
  1531. }
  1532. static struct rtable *vxlan_get_route(struct vxlan_dev *vxlan, struct net_device *dev,
  1533. struct vxlan_sock *sock4,
  1534. struct sk_buff *skb, int oif, u8 tos,
  1535. __be32 daddr, __be32 *saddr, __be16 dport, __be16 sport,
  1536. struct dst_cache *dst_cache,
  1537. const struct ip_tunnel_info *info)
  1538. {
  1539. bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
  1540. struct rtable *rt = NULL;
  1541. struct flowi4 fl4;
  1542. if (!sock4)
  1543. return ERR_PTR(-EIO);
  1544. if (tos && !info)
  1545. use_cache = false;
  1546. if (use_cache) {
  1547. rt = dst_cache_get_ip4(dst_cache, saddr);
  1548. if (rt)
  1549. return rt;
  1550. }
  1551. memset(&fl4, 0, sizeof(fl4));
  1552. fl4.flowi4_oif = oif;
  1553. fl4.flowi4_tos = RT_TOS(tos);
  1554. fl4.flowi4_mark = skb->mark;
  1555. fl4.flowi4_proto = IPPROTO_UDP;
  1556. fl4.daddr = daddr;
  1557. fl4.saddr = *saddr;
  1558. fl4.fl4_dport = dport;
  1559. fl4.fl4_sport = sport;
  1560. rt = ip_route_output_key(vxlan->net, &fl4);
  1561. if (likely(!IS_ERR(rt))) {
  1562. if (rt->dst.dev == dev) {
  1563. netdev_dbg(dev, "circular route to %pI4\n", &daddr);
  1564. ip_rt_put(rt);
  1565. return ERR_PTR(-ELOOP);
  1566. }
  1567. *saddr = fl4.saddr;
  1568. if (use_cache)
  1569. dst_cache_set_ip4(dst_cache, &rt->dst, fl4.saddr);
  1570. } else {
  1571. netdev_dbg(dev, "no route to %pI4\n", &daddr);
  1572. return ERR_PTR(-ENETUNREACH);
  1573. }
  1574. return rt;
  1575. }
  1576. #if IS_ENABLED(CONFIG_IPV6)
  1577. static struct dst_entry *vxlan6_get_route(struct vxlan_dev *vxlan,
  1578. struct net_device *dev,
  1579. struct vxlan_sock *sock6,
  1580. struct sk_buff *skb, int oif, u8 tos,
  1581. __be32 label,
  1582. const struct in6_addr *daddr,
  1583. struct in6_addr *saddr,
  1584. __be16 dport, __be16 sport,
  1585. struct dst_cache *dst_cache,
  1586. const struct ip_tunnel_info *info)
  1587. {
  1588. bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
  1589. struct dst_entry *ndst;
  1590. struct flowi6 fl6;
  1591. int err;
  1592. if (!sock6)
  1593. return ERR_PTR(-EIO);
  1594. if (tos && !info)
  1595. use_cache = false;
  1596. if (use_cache) {
  1597. ndst = dst_cache_get_ip6(dst_cache, saddr);
  1598. if (ndst)
  1599. return ndst;
  1600. }
  1601. memset(&fl6, 0, sizeof(fl6));
  1602. fl6.flowi6_oif = oif;
  1603. fl6.daddr = *daddr;
  1604. fl6.saddr = *saddr;
  1605. fl6.flowlabel = ip6_make_flowinfo(RT_TOS(tos), label);
  1606. fl6.flowi6_mark = skb->mark;
  1607. fl6.flowi6_proto = IPPROTO_UDP;
  1608. fl6.fl6_dport = dport;
  1609. fl6.fl6_sport = sport;
  1610. err = ipv6_stub->ipv6_dst_lookup(vxlan->net,
  1611. sock6->sock->sk,
  1612. &ndst, &fl6);
  1613. if (unlikely(err < 0)) {
  1614. netdev_dbg(dev, "no route to %pI6\n", daddr);
  1615. return ERR_PTR(-ENETUNREACH);
  1616. }
  1617. if (unlikely(ndst->dev == dev)) {
  1618. netdev_dbg(dev, "circular route to %pI6\n", daddr);
  1619. dst_release(ndst);
  1620. return ERR_PTR(-ELOOP);
  1621. }
  1622. *saddr = fl6.saddr;
  1623. if (use_cache)
  1624. dst_cache_set_ip6(dst_cache, ndst, saddr);
  1625. return ndst;
  1626. }
  1627. #endif
  1628. /* Bypass encapsulation if the destination is local */
  1629. static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
  1630. struct vxlan_dev *dst_vxlan, __be32 vni)
  1631. {
  1632. struct pcpu_sw_netstats *tx_stats, *rx_stats;
  1633. union vxlan_addr loopback;
  1634. union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
  1635. struct net_device *dev = skb->dev;
  1636. int len = skb->len;
  1637. tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
  1638. rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
  1639. skb->pkt_type = PACKET_HOST;
  1640. skb->encapsulation = 0;
  1641. skb->dev = dst_vxlan->dev;
  1642. __skb_pull(skb, skb_network_offset(skb));
  1643. if (remote_ip->sa.sa_family == AF_INET) {
  1644. loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  1645. loopback.sa.sa_family = AF_INET;
  1646. #if IS_ENABLED(CONFIG_IPV6)
  1647. } else {
  1648. loopback.sin6.sin6_addr = in6addr_loopback;
  1649. loopback.sa.sa_family = AF_INET6;
  1650. #endif
  1651. }
  1652. if (dst_vxlan->cfg.flags & VXLAN_F_LEARN)
  1653. vxlan_snoop(skb->dev, &loopback, eth_hdr(skb)->h_source, 0,
  1654. vni);
  1655. u64_stats_update_begin(&tx_stats->syncp);
  1656. tx_stats->tx_packets++;
  1657. tx_stats->tx_bytes += len;
  1658. u64_stats_update_end(&tx_stats->syncp);
  1659. if (netif_rx(skb) == NET_RX_SUCCESS) {
  1660. u64_stats_update_begin(&rx_stats->syncp);
  1661. rx_stats->rx_packets++;
  1662. rx_stats->rx_bytes += len;
  1663. u64_stats_update_end(&rx_stats->syncp);
  1664. } else {
  1665. dev->stats.rx_dropped++;
  1666. }
  1667. }
  1668. static int encap_bypass_if_local(struct sk_buff *skb, struct net_device *dev,
  1669. struct vxlan_dev *vxlan,
  1670. union vxlan_addr *daddr,
  1671. __be16 dst_port, int dst_ifindex, __be32 vni,
  1672. struct dst_entry *dst,
  1673. u32 rt_flags)
  1674. {
  1675. #if IS_ENABLED(CONFIG_IPV6)
  1676. /* IPv6 rt-flags are checked against RTF_LOCAL, but the value of
  1677. * RTF_LOCAL is equal to RTCF_LOCAL. So to keep code simple
  1678. * we can use RTCF_LOCAL which works for ipv4 and ipv6 route entry.
  1679. */
  1680. BUILD_BUG_ON(RTCF_LOCAL != RTF_LOCAL);
  1681. #endif
  1682. /* Bypass encapsulation if the destination is local */
  1683. if (rt_flags & RTCF_LOCAL &&
  1684. !(rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
  1685. struct vxlan_dev *dst_vxlan;
  1686. dst_release(dst);
  1687. dst_vxlan = vxlan_find_vni(vxlan->net, dst_ifindex, vni,
  1688. daddr->sa.sa_family, dst_port,
  1689. vxlan->cfg.flags);
  1690. if (!dst_vxlan) {
  1691. dev->stats.tx_errors++;
  1692. kfree_skb(skb);
  1693. return -ENOENT;
  1694. }
  1695. vxlan_encap_bypass(skb, vxlan, dst_vxlan, vni);
  1696. return 1;
  1697. }
  1698. return 0;
  1699. }
  1700. static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
  1701. __be32 default_vni, struct vxlan_rdst *rdst,
  1702. bool did_rsc)
  1703. {
  1704. struct dst_cache *dst_cache;
  1705. struct ip_tunnel_info *info;
  1706. struct vxlan_dev *vxlan = netdev_priv(dev);
  1707. const struct iphdr *old_iph = ip_hdr(skb);
  1708. union vxlan_addr *dst;
  1709. union vxlan_addr remote_ip, local_ip;
  1710. struct vxlan_metadata _md;
  1711. struct vxlan_metadata *md = &_md;
  1712. __be16 src_port = 0, dst_port;
  1713. struct dst_entry *ndst = NULL;
  1714. __be32 vni, label;
  1715. __u8 tos, ttl;
  1716. int ifindex;
  1717. int err;
  1718. u32 flags = vxlan->cfg.flags;
  1719. bool udp_sum = false;
  1720. bool xnet = !net_eq(vxlan->net, dev_net(vxlan->dev));
  1721. info = skb_tunnel_info(skb);
  1722. if (rdst) {
  1723. dst = &rdst->remote_ip;
  1724. if (vxlan_addr_any(dst)) {
  1725. if (did_rsc) {
  1726. /* short-circuited back to local bridge */
  1727. vxlan_encap_bypass(skb, vxlan, vxlan, default_vni);
  1728. return;
  1729. }
  1730. goto drop;
  1731. }
  1732. dst_port = rdst->remote_port ? rdst->remote_port : vxlan->cfg.dst_port;
  1733. vni = (rdst->remote_vni) ? : default_vni;
  1734. ifindex = rdst->remote_ifindex;
  1735. local_ip = vxlan->cfg.saddr;
  1736. dst_cache = &rdst->dst_cache;
  1737. md->gbp = skb->mark;
  1738. if (flags & VXLAN_F_TTL_INHERIT) {
  1739. ttl = ip_tunnel_get_ttl(old_iph, skb);
  1740. } else {
  1741. ttl = vxlan->cfg.ttl;
  1742. if (!ttl && vxlan_addr_multicast(dst))
  1743. ttl = 1;
  1744. }
  1745. tos = vxlan->cfg.tos;
  1746. if (tos == 1)
  1747. tos = ip_tunnel_get_dsfield(old_iph, skb);
  1748. if (dst->sa.sa_family == AF_INET)
  1749. udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM_TX);
  1750. else
  1751. udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM6_TX);
  1752. label = vxlan->cfg.label;
  1753. } else {
  1754. if (!info) {
  1755. WARN_ONCE(1, "%s: Missing encapsulation instructions\n",
  1756. dev->name);
  1757. goto drop;
  1758. }
  1759. remote_ip.sa.sa_family = ip_tunnel_info_af(info);
  1760. if (remote_ip.sa.sa_family == AF_INET) {
  1761. remote_ip.sin.sin_addr.s_addr = info->key.u.ipv4.dst;
  1762. local_ip.sin.sin_addr.s_addr = info->key.u.ipv4.src;
  1763. } else {
  1764. remote_ip.sin6.sin6_addr = info->key.u.ipv6.dst;
  1765. local_ip.sin6.sin6_addr = info->key.u.ipv6.src;
  1766. }
  1767. dst = &remote_ip;
  1768. dst_port = info->key.tp_dst ? : vxlan->cfg.dst_port;
  1769. vni = tunnel_id_to_key32(info->key.tun_id);
  1770. ifindex = 0;
  1771. dst_cache = &info->dst_cache;
  1772. if (info->options_len &&
  1773. info->key.tun_flags & TUNNEL_VXLAN_OPT)
  1774. md = ip_tunnel_info_opts(info);
  1775. ttl = info->key.ttl;
  1776. tos = info->key.tos;
  1777. label = info->key.label;
  1778. udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
  1779. }
  1780. src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
  1781. vxlan->cfg.port_max, true);
  1782. rcu_read_lock();
  1783. if (dst->sa.sa_family == AF_INET) {
  1784. struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock);
  1785. struct rtable *rt;
  1786. __be16 df = 0;
  1787. rt = vxlan_get_route(vxlan, dev, sock4, skb, ifindex, tos,
  1788. dst->sin.sin_addr.s_addr,
  1789. &local_ip.sin.sin_addr.s_addr,
  1790. dst_port, src_port,
  1791. dst_cache, info);
  1792. if (IS_ERR(rt)) {
  1793. err = PTR_ERR(rt);
  1794. goto tx_error;
  1795. }
  1796. /* Bypass encapsulation if the destination is local */
  1797. if (!info) {
  1798. err = encap_bypass_if_local(skb, dev, vxlan, dst,
  1799. dst_port, ifindex, vni,
  1800. &rt->dst, rt->rt_flags);
  1801. if (err)
  1802. goto out_unlock;
  1803. } else if (info->key.tun_flags & TUNNEL_DONT_FRAGMENT) {
  1804. df = htons(IP_DF);
  1805. }
  1806. ndst = &rt->dst;
  1807. if (skb_dst(skb)) {
  1808. int mtu = dst_mtu(ndst) - VXLAN_HEADROOM;
  1809. skb_dst_update_pmtu(skb, mtu);
  1810. }
  1811. tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
  1812. ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
  1813. err = vxlan_build_skb(skb, ndst, sizeof(struct iphdr),
  1814. vni, md, flags, udp_sum);
  1815. if (err < 0)
  1816. goto tx_error;
  1817. udp_tunnel_xmit_skb(rt, sock4->sock->sk, skb, local_ip.sin.sin_addr.s_addr,
  1818. dst->sin.sin_addr.s_addr, tos, ttl, df,
  1819. src_port, dst_port, xnet, !udp_sum);
  1820. #if IS_ENABLED(CONFIG_IPV6)
  1821. } else {
  1822. struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock);
  1823. ndst = vxlan6_get_route(vxlan, dev, sock6, skb, ifindex, tos,
  1824. label, &dst->sin6.sin6_addr,
  1825. &local_ip.sin6.sin6_addr,
  1826. dst_port, src_port,
  1827. dst_cache, info);
  1828. if (IS_ERR(ndst)) {
  1829. err = PTR_ERR(ndst);
  1830. ndst = NULL;
  1831. goto tx_error;
  1832. }
  1833. if (!info) {
  1834. u32 rt6i_flags = ((struct rt6_info *)ndst)->rt6i_flags;
  1835. err = encap_bypass_if_local(skb, dev, vxlan, dst,
  1836. dst_port, ifindex, vni,
  1837. ndst, rt6i_flags);
  1838. if (err)
  1839. goto out_unlock;
  1840. }
  1841. if (skb_dst(skb)) {
  1842. int mtu = dst_mtu(ndst) - VXLAN6_HEADROOM;
  1843. skb_dst_update_pmtu(skb, mtu);
  1844. }
  1845. tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
  1846. ttl = ttl ? : ip6_dst_hoplimit(ndst);
  1847. skb_scrub_packet(skb, xnet);
  1848. err = vxlan_build_skb(skb, ndst, sizeof(struct ipv6hdr),
  1849. vni, md, flags, udp_sum);
  1850. if (err < 0)
  1851. goto tx_error;
  1852. udp_tunnel6_xmit_skb(ndst, sock6->sock->sk, skb, dev,
  1853. &local_ip.sin6.sin6_addr,
  1854. &dst->sin6.sin6_addr, tos, ttl,
  1855. label, src_port, dst_port, !udp_sum);
  1856. #endif
  1857. }
  1858. out_unlock:
  1859. rcu_read_unlock();
  1860. return;
  1861. drop:
  1862. dev->stats.tx_dropped++;
  1863. dev_kfree_skb(skb);
  1864. return;
  1865. tx_error:
  1866. rcu_read_unlock();
  1867. if (err == -ELOOP)
  1868. dev->stats.collisions++;
  1869. else if (err == -ENETUNREACH)
  1870. dev->stats.tx_carrier_errors++;
  1871. dst_release(ndst);
  1872. dev->stats.tx_errors++;
  1873. kfree_skb(skb);
  1874. }
  1875. /* Transmit local packets over Vxlan
  1876. *
  1877. * Outer IP header inherits ECN and DF from inner header.
  1878. * Outer UDP destination is the VXLAN assigned port.
  1879. * source port is based on hash of flow
  1880. */
  1881. static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
  1882. {
  1883. struct vxlan_dev *vxlan = netdev_priv(dev);
  1884. struct vxlan_rdst *rdst, *fdst = NULL;
  1885. const struct ip_tunnel_info *info;
  1886. bool did_rsc = false;
  1887. struct vxlan_fdb *f;
  1888. struct ethhdr *eth;
  1889. __be32 vni = 0;
  1890. info = skb_tunnel_info(skb);
  1891. skb_reset_mac_header(skb);
  1892. if (vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA) {
  1893. if (info && info->mode & IP_TUNNEL_INFO_BRIDGE &&
  1894. info->mode & IP_TUNNEL_INFO_TX) {
  1895. vni = tunnel_id_to_key32(info->key.tun_id);
  1896. } else {
  1897. if (info && info->mode & IP_TUNNEL_INFO_TX)
  1898. vxlan_xmit_one(skb, dev, vni, NULL, false);
  1899. else
  1900. kfree_skb(skb);
  1901. return NETDEV_TX_OK;
  1902. }
  1903. }
  1904. if (vxlan->cfg.flags & VXLAN_F_PROXY) {
  1905. eth = eth_hdr(skb);
  1906. if (ntohs(eth->h_proto) == ETH_P_ARP)
  1907. return arp_reduce(dev, skb, vni);
  1908. #if IS_ENABLED(CONFIG_IPV6)
  1909. else if (ntohs(eth->h_proto) == ETH_P_IPV6 &&
  1910. pskb_may_pull(skb, sizeof(struct ipv6hdr) +
  1911. sizeof(struct nd_msg)) &&
  1912. ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
  1913. struct nd_msg *m = (struct nd_msg *)(ipv6_hdr(skb) + 1);
  1914. if (m->icmph.icmp6_code == 0 &&
  1915. m->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
  1916. return neigh_reduce(dev, skb, vni);
  1917. }
  1918. #endif
  1919. }
  1920. eth = eth_hdr(skb);
  1921. f = vxlan_find_mac(vxlan, eth->h_dest, vni);
  1922. did_rsc = false;
  1923. if (f && (f->flags & NTF_ROUTER) && (vxlan->cfg.flags & VXLAN_F_RSC) &&
  1924. (ntohs(eth->h_proto) == ETH_P_IP ||
  1925. ntohs(eth->h_proto) == ETH_P_IPV6)) {
  1926. did_rsc = route_shortcircuit(dev, skb);
  1927. if (did_rsc)
  1928. f = vxlan_find_mac(vxlan, eth->h_dest, vni);
  1929. }
  1930. if (f == NULL) {
  1931. f = vxlan_find_mac(vxlan, all_zeros_mac, vni);
  1932. if (f == NULL) {
  1933. if ((vxlan->cfg.flags & VXLAN_F_L2MISS) &&
  1934. !is_multicast_ether_addr(eth->h_dest))
  1935. vxlan_fdb_miss(vxlan, eth->h_dest);
  1936. dev->stats.tx_dropped++;
  1937. kfree_skb(skb);
  1938. return NETDEV_TX_OK;
  1939. }
  1940. }
  1941. list_for_each_entry_rcu(rdst, &f->remotes, list) {
  1942. struct sk_buff *skb1;
  1943. if (!fdst) {
  1944. fdst = rdst;
  1945. continue;
  1946. }
  1947. skb1 = skb_clone(skb, GFP_ATOMIC);
  1948. if (skb1)
  1949. vxlan_xmit_one(skb1, dev, vni, rdst, did_rsc);
  1950. }
  1951. if (fdst)
  1952. vxlan_xmit_one(skb, dev, vni, fdst, did_rsc);
  1953. else
  1954. kfree_skb(skb);
  1955. return NETDEV_TX_OK;
  1956. }
  1957. /* Walk the forwarding table and purge stale entries */
  1958. static void vxlan_cleanup(struct timer_list *t)
  1959. {
  1960. struct vxlan_dev *vxlan = from_timer(vxlan, t, age_timer);
  1961. unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
  1962. unsigned int h;
  1963. if (!netif_running(vxlan->dev))
  1964. return;
  1965. for (h = 0; h < FDB_HASH_SIZE; ++h) {
  1966. struct hlist_node *p, *n;
  1967. spin_lock_bh(&vxlan->hash_lock);
  1968. hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
  1969. struct vxlan_fdb *f
  1970. = container_of(p, struct vxlan_fdb, hlist);
  1971. unsigned long timeout;
  1972. if (f->state & (NUD_PERMANENT | NUD_NOARP))
  1973. continue;
  1974. if (f->flags & NTF_EXT_LEARNED)
  1975. continue;
  1976. timeout = f->used + vxlan->cfg.age_interval * HZ;
  1977. if (time_before_eq(timeout, jiffies)) {
  1978. netdev_dbg(vxlan->dev,
  1979. "garbage collect %pM\n",
  1980. f->eth_addr);
  1981. f->state = NUD_STALE;
  1982. vxlan_fdb_destroy(vxlan, f);
  1983. } else if (time_before(timeout, next_timer))
  1984. next_timer = timeout;
  1985. }
  1986. spin_unlock_bh(&vxlan->hash_lock);
  1987. }
  1988. mod_timer(&vxlan->age_timer, next_timer);
  1989. }
  1990. static void vxlan_vs_del_dev(struct vxlan_dev *vxlan)
  1991. {
  1992. struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
  1993. spin_lock(&vn->sock_lock);
  1994. hlist_del_init_rcu(&vxlan->hlist4.hlist);
  1995. #if IS_ENABLED(CONFIG_IPV6)
  1996. hlist_del_init_rcu(&vxlan->hlist6.hlist);
  1997. #endif
  1998. spin_unlock(&vn->sock_lock);
  1999. }
  2000. static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan,
  2001. struct vxlan_dev_node *node)
  2002. {
  2003. struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
  2004. __be32 vni = vxlan->default_dst.remote_vni;
  2005. node->vxlan = vxlan;
  2006. spin_lock(&vn->sock_lock);
  2007. hlist_add_head_rcu(&node->hlist, vni_head(vs, vni));
  2008. spin_unlock(&vn->sock_lock);
  2009. }
  2010. /* Setup stats when device is created */
  2011. static int vxlan_init(struct net_device *dev)
  2012. {
  2013. dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
  2014. if (!dev->tstats)
  2015. return -ENOMEM;
  2016. return 0;
  2017. }
  2018. static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan, __be32 vni)
  2019. {
  2020. struct vxlan_fdb *f;
  2021. spin_lock_bh(&vxlan->hash_lock);
  2022. f = __vxlan_find_mac(vxlan, all_zeros_mac, vni);
  2023. if (f)
  2024. vxlan_fdb_destroy(vxlan, f);
  2025. spin_unlock_bh(&vxlan->hash_lock);
  2026. }
  2027. static void vxlan_uninit(struct net_device *dev)
  2028. {
  2029. struct vxlan_dev *vxlan = netdev_priv(dev);
  2030. vxlan_fdb_delete_default(vxlan, vxlan->cfg.vni);
  2031. free_percpu(dev->tstats);
  2032. }
  2033. /* Start ageing timer and join group when device is brought up */
  2034. static int vxlan_open(struct net_device *dev)
  2035. {
  2036. struct vxlan_dev *vxlan = netdev_priv(dev);
  2037. int ret;
  2038. ret = vxlan_sock_add(vxlan);
  2039. if (ret < 0)
  2040. return ret;
  2041. if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) {
  2042. ret = vxlan_igmp_join(vxlan);
  2043. if (ret == -EADDRINUSE)
  2044. ret = 0;
  2045. if (ret) {
  2046. vxlan_sock_release(vxlan);
  2047. return ret;
  2048. }
  2049. }
  2050. if (vxlan->cfg.age_interval)
  2051. mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
  2052. return ret;
  2053. }
  2054. /* Purge the forwarding table */
  2055. static void vxlan_flush(struct vxlan_dev *vxlan, bool do_all)
  2056. {
  2057. unsigned int h;
  2058. spin_lock_bh(&vxlan->hash_lock);
  2059. for (h = 0; h < FDB_HASH_SIZE; ++h) {
  2060. struct hlist_node *p, *n;
  2061. hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
  2062. struct vxlan_fdb *f
  2063. = container_of(p, struct vxlan_fdb, hlist);
  2064. if (!do_all && (f->state & (NUD_PERMANENT | NUD_NOARP)))
  2065. continue;
  2066. /* the all_zeros_mac entry is deleted at vxlan_uninit */
  2067. if (!is_zero_ether_addr(f->eth_addr))
  2068. vxlan_fdb_destroy(vxlan, f);
  2069. }
  2070. }
  2071. spin_unlock_bh(&vxlan->hash_lock);
  2072. }
  2073. /* Cleanup timer and forwarding table on shutdown */
  2074. static int vxlan_stop(struct net_device *dev)
  2075. {
  2076. struct vxlan_dev *vxlan = netdev_priv(dev);
  2077. struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
  2078. int ret = 0;
  2079. if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
  2080. !vxlan_group_used(vn, vxlan))
  2081. ret = vxlan_igmp_leave(vxlan);
  2082. del_timer_sync(&vxlan->age_timer);
  2083. vxlan_flush(vxlan, false);
  2084. vxlan_sock_release(vxlan);
  2085. return ret;
  2086. }
  2087. /* Stub, nothing needs to be done. */
  2088. static void vxlan_set_multicast_list(struct net_device *dev)
  2089. {
  2090. }
  2091. static int vxlan_change_mtu(struct net_device *dev, int new_mtu)
  2092. {
  2093. struct vxlan_dev *vxlan = netdev_priv(dev);
  2094. struct vxlan_rdst *dst = &vxlan->default_dst;
  2095. struct net_device *lowerdev = __dev_get_by_index(vxlan->net,
  2096. dst->remote_ifindex);
  2097. bool use_ipv6 = !!(vxlan->cfg.flags & VXLAN_F_IPV6);
  2098. /* This check is different than dev->max_mtu, because it looks at
  2099. * the lowerdev->mtu, rather than the static dev->max_mtu
  2100. */
  2101. if (lowerdev) {
  2102. int max_mtu = lowerdev->mtu -
  2103. (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
  2104. if (new_mtu > max_mtu)
  2105. return -EINVAL;
  2106. }
  2107. dev->mtu = new_mtu;
  2108. return 0;
  2109. }
  2110. static int vxlan_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
  2111. {
  2112. struct vxlan_dev *vxlan = netdev_priv(dev);
  2113. struct ip_tunnel_info *info = skb_tunnel_info(skb);
  2114. __be16 sport, dport;
  2115. sport = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
  2116. vxlan->cfg.port_max, true);
  2117. dport = info->key.tp_dst ? : vxlan->cfg.dst_port;
  2118. if (ip_tunnel_info_af(info) == AF_INET) {
  2119. struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock);
  2120. struct rtable *rt;
  2121. rt = vxlan_get_route(vxlan, dev, sock4, skb, 0, info->key.tos,
  2122. info->key.u.ipv4.dst,
  2123. &info->key.u.ipv4.src, dport, sport,
  2124. &info->dst_cache, info);
  2125. if (IS_ERR(rt))
  2126. return PTR_ERR(rt);
  2127. ip_rt_put(rt);
  2128. } else {
  2129. #if IS_ENABLED(CONFIG_IPV6)
  2130. struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock);
  2131. struct dst_entry *ndst;
  2132. ndst = vxlan6_get_route(vxlan, dev, sock6, skb, 0, info->key.tos,
  2133. info->key.label, &info->key.u.ipv6.dst,
  2134. &info->key.u.ipv6.src, dport, sport,
  2135. &info->dst_cache, info);
  2136. if (IS_ERR(ndst))
  2137. return PTR_ERR(ndst);
  2138. dst_release(ndst);
  2139. #else /* !CONFIG_IPV6 */
  2140. return -EPFNOSUPPORT;
  2141. #endif
  2142. }
  2143. info->key.tp_src = sport;
  2144. info->key.tp_dst = dport;
  2145. return 0;
  2146. }
  2147. static const struct net_device_ops vxlan_netdev_ether_ops = {
  2148. .ndo_init = vxlan_init,
  2149. .ndo_uninit = vxlan_uninit,
  2150. .ndo_open = vxlan_open,
  2151. .ndo_stop = vxlan_stop,
  2152. .ndo_start_xmit = vxlan_xmit,
  2153. .ndo_get_stats64 = ip_tunnel_get_stats64,
  2154. .ndo_set_rx_mode = vxlan_set_multicast_list,
  2155. .ndo_change_mtu = vxlan_change_mtu,
  2156. .ndo_validate_addr = eth_validate_addr,
  2157. .ndo_set_mac_address = eth_mac_addr,
  2158. .ndo_fdb_add = vxlan_fdb_add,
  2159. .ndo_fdb_del = vxlan_fdb_delete,
  2160. .ndo_fdb_dump = vxlan_fdb_dump,
  2161. .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
  2162. };
  2163. static const struct net_device_ops vxlan_netdev_raw_ops = {
  2164. .ndo_init = vxlan_init,
  2165. .ndo_uninit = vxlan_uninit,
  2166. .ndo_open = vxlan_open,
  2167. .ndo_stop = vxlan_stop,
  2168. .ndo_start_xmit = vxlan_xmit,
  2169. .ndo_get_stats64 = ip_tunnel_get_stats64,
  2170. .ndo_change_mtu = vxlan_change_mtu,
  2171. .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
  2172. };
  2173. /* Info for udev, that this is a virtual tunnel endpoint */
  2174. static struct device_type vxlan_type = {
  2175. .name = "vxlan",
  2176. };
  2177. /* Calls the ndo_udp_tunnel_add of the caller in order to
  2178. * supply the listening VXLAN udp ports. Callers are expected
  2179. * to implement the ndo_udp_tunnel_add.
  2180. */
  2181. static void vxlan_offload_rx_ports(struct net_device *dev, bool push)
  2182. {
  2183. struct vxlan_sock *vs;
  2184. struct net *net = dev_net(dev);
  2185. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  2186. unsigned int i;
  2187. spin_lock(&vn->sock_lock);
  2188. for (i = 0; i < PORT_HASH_SIZE; ++i) {
  2189. hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist) {
  2190. unsigned short type;
  2191. if (vs->flags & VXLAN_F_GPE)
  2192. type = UDP_TUNNEL_TYPE_VXLAN_GPE;
  2193. else
  2194. type = UDP_TUNNEL_TYPE_VXLAN;
  2195. if (push)
  2196. udp_tunnel_push_rx_port(dev, vs->sock, type);
  2197. else
  2198. udp_tunnel_drop_rx_port(dev, vs->sock, type);
  2199. }
  2200. }
  2201. spin_unlock(&vn->sock_lock);
  2202. }
  2203. /* Initialize the device structure. */
  2204. static void vxlan_setup(struct net_device *dev)
  2205. {
  2206. struct vxlan_dev *vxlan = netdev_priv(dev);
  2207. unsigned int h;
  2208. eth_hw_addr_random(dev);
  2209. ether_setup(dev);
  2210. dev->needs_free_netdev = true;
  2211. SET_NETDEV_DEVTYPE(dev, &vxlan_type);
  2212. dev->features |= NETIF_F_LLTX;
  2213. dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
  2214. dev->features |= NETIF_F_RXCSUM;
  2215. dev->features |= NETIF_F_GSO_SOFTWARE;
  2216. dev->vlan_features = dev->features;
  2217. dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
  2218. dev->hw_features |= NETIF_F_GSO_SOFTWARE;
  2219. netif_keep_dst(dev);
  2220. dev->priv_flags |= IFF_NO_QUEUE;
  2221. /* MTU range: 68 - 65535 */
  2222. dev->min_mtu = ETH_MIN_MTU;
  2223. dev->max_mtu = ETH_MAX_MTU;
  2224. INIT_LIST_HEAD(&vxlan->next);
  2225. spin_lock_init(&vxlan->hash_lock);
  2226. timer_setup(&vxlan->age_timer, vxlan_cleanup, TIMER_DEFERRABLE);
  2227. vxlan->dev = dev;
  2228. gro_cells_init(&vxlan->gro_cells, dev);
  2229. for (h = 0; h < FDB_HASH_SIZE; ++h)
  2230. INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
  2231. }
  2232. static void vxlan_ether_setup(struct net_device *dev)
  2233. {
  2234. dev->priv_flags &= ~IFF_TX_SKB_SHARING;
  2235. dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
  2236. dev->netdev_ops = &vxlan_netdev_ether_ops;
  2237. }
  2238. static void vxlan_raw_setup(struct net_device *dev)
  2239. {
  2240. dev->header_ops = NULL;
  2241. dev->type = ARPHRD_NONE;
  2242. dev->hard_header_len = 0;
  2243. dev->addr_len = 0;
  2244. dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
  2245. dev->netdev_ops = &vxlan_netdev_raw_ops;
  2246. }
  2247. static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
  2248. [IFLA_VXLAN_ID] = { .type = NLA_U32 },
  2249. [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
  2250. [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
  2251. [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
  2252. [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
  2253. [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
  2254. [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
  2255. [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
  2256. [IFLA_VXLAN_LABEL] = { .type = NLA_U32 },
  2257. [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
  2258. [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
  2259. [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
  2260. [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
  2261. [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
  2262. [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
  2263. [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
  2264. [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
  2265. [IFLA_VXLAN_COLLECT_METADATA] = { .type = NLA_U8 },
  2266. [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
  2267. [IFLA_VXLAN_UDP_CSUM] = { .type = NLA_U8 },
  2268. [IFLA_VXLAN_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
  2269. [IFLA_VXLAN_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
  2270. [IFLA_VXLAN_REMCSUM_TX] = { .type = NLA_U8 },
  2271. [IFLA_VXLAN_REMCSUM_RX] = { .type = NLA_U8 },
  2272. [IFLA_VXLAN_GBP] = { .type = NLA_FLAG, },
  2273. [IFLA_VXLAN_GPE] = { .type = NLA_FLAG, },
  2274. [IFLA_VXLAN_REMCSUM_NOPARTIAL] = { .type = NLA_FLAG },
  2275. [IFLA_VXLAN_TTL_INHERIT] = { .type = NLA_FLAG },
  2276. };
  2277. static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[],
  2278. struct netlink_ext_ack *extack)
  2279. {
  2280. if (tb[IFLA_ADDRESS]) {
  2281. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
  2282. NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
  2283. "Provided link layer address is not Ethernet");
  2284. return -EINVAL;
  2285. }
  2286. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
  2287. NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
  2288. "Provided Ethernet address is not unicast");
  2289. return -EADDRNOTAVAIL;
  2290. }
  2291. }
  2292. if (tb[IFLA_MTU]) {
  2293. u32 mtu = nla_get_u32(tb[IFLA_MTU]);
  2294. if (mtu < ETH_MIN_MTU || mtu > ETH_MAX_MTU) {
  2295. NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_MTU],
  2296. "MTU must be between 68 and 65535");
  2297. return -EINVAL;
  2298. }
  2299. }
  2300. if (!data) {
  2301. NL_SET_ERR_MSG(extack,
  2302. "Required attributes not provided to perform the operation");
  2303. return -EINVAL;
  2304. }
  2305. if (data[IFLA_VXLAN_ID]) {
  2306. u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
  2307. if (id >= VXLAN_N_VID) {
  2308. NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_ID],
  2309. "VXLAN ID must be lower than 16777216");
  2310. return -ERANGE;
  2311. }
  2312. }
  2313. if (data[IFLA_VXLAN_PORT_RANGE]) {
  2314. const struct ifla_vxlan_port_range *p
  2315. = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
  2316. if (ntohs(p->high) < ntohs(p->low)) {
  2317. NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_PORT_RANGE],
  2318. "Invalid source port range");
  2319. return -EINVAL;
  2320. }
  2321. }
  2322. return 0;
  2323. }
  2324. static void vxlan_get_drvinfo(struct net_device *netdev,
  2325. struct ethtool_drvinfo *drvinfo)
  2326. {
  2327. strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
  2328. strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
  2329. }
  2330. static const struct ethtool_ops vxlan_ethtool_ops = {
  2331. .get_drvinfo = vxlan_get_drvinfo,
  2332. .get_link = ethtool_op_get_link,
  2333. };
  2334. static struct socket *vxlan_create_sock(struct net *net, bool ipv6,
  2335. __be16 port, u32 flags)
  2336. {
  2337. struct socket *sock;
  2338. struct udp_port_cfg udp_conf;
  2339. int err;
  2340. memset(&udp_conf, 0, sizeof(udp_conf));
  2341. if (ipv6) {
  2342. udp_conf.family = AF_INET6;
  2343. udp_conf.use_udp6_rx_checksums =
  2344. !(flags & VXLAN_F_UDP_ZERO_CSUM6_RX);
  2345. udp_conf.ipv6_v6only = 1;
  2346. } else {
  2347. udp_conf.family = AF_INET;
  2348. }
  2349. udp_conf.local_udp_port = port;
  2350. /* Open UDP socket */
  2351. err = udp_sock_create(net, &udp_conf, &sock);
  2352. if (err < 0)
  2353. return ERR_PTR(err);
  2354. return sock;
  2355. }
  2356. /* Create new listen socket if needed */
  2357. static struct vxlan_sock *vxlan_socket_create(struct net *net, bool ipv6,
  2358. __be16 port, u32 flags)
  2359. {
  2360. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  2361. struct vxlan_sock *vs;
  2362. struct socket *sock;
  2363. unsigned int h;
  2364. struct udp_tunnel_sock_cfg tunnel_cfg;
  2365. vs = kzalloc(sizeof(*vs), GFP_KERNEL);
  2366. if (!vs)
  2367. return ERR_PTR(-ENOMEM);
  2368. for (h = 0; h < VNI_HASH_SIZE; ++h)
  2369. INIT_HLIST_HEAD(&vs->vni_list[h]);
  2370. sock = vxlan_create_sock(net, ipv6, port, flags);
  2371. if (IS_ERR(sock)) {
  2372. kfree(vs);
  2373. return ERR_CAST(sock);
  2374. }
  2375. vs->sock = sock;
  2376. refcount_set(&vs->refcnt, 1);
  2377. vs->flags = (flags & VXLAN_F_RCV_FLAGS);
  2378. spin_lock(&vn->sock_lock);
  2379. hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
  2380. udp_tunnel_notify_add_rx_port(sock,
  2381. (vs->flags & VXLAN_F_GPE) ?
  2382. UDP_TUNNEL_TYPE_VXLAN_GPE :
  2383. UDP_TUNNEL_TYPE_VXLAN);
  2384. spin_unlock(&vn->sock_lock);
  2385. /* Mark socket as an encapsulation socket. */
  2386. memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
  2387. tunnel_cfg.sk_user_data = vs;
  2388. tunnel_cfg.encap_type = 1;
  2389. tunnel_cfg.encap_rcv = vxlan_rcv;
  2390. tunnel_cfg.encap_destroy = NULL;
  2391. tunnel_cfg.gro_receive = vxlan_gro_receive;
  2392. tunnel_cfg.gro_complete = vxlan_gro_complete;
  2393. setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
  2394. return vs;
  2395. }
  2396. static int __vxlan_sock_add(struct vxlan_dev *vxlan, bool ipv6)
  2397. {
  2398. struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
  2399. struct vxlan_sock *vs = NULL;
  2400. struct vxlan_dev_node *node;
  2401. if (!vxlan->cfg.no_share) {
  2402. spin_lock(&vn->sock_lock);
  2403. vs = vxlan_find_sock(vxlan->net, ipv6 ? AF_INET6 : AF_INET,
  2404. vxlan->cfg.dst_port, vxlan->cfg.flags);
  2405. if (vs && !refcount_inc_not_zero(&vs->refcnt)) {
  2406. spin_unlock(&vn->sock_lock);
  2407. return -EBUSY;
  2408. }
  2409. spin_unlock(&vn->sock_lock);
  2410. }
  2411. if (!vs)
  2412. vs = vxlan_socket_create(vxlan->net, ipv6,
  2413. vxlan->cfg.dst_port, vxlan->cfg.flags);
  2414. if (IS_ERR(vs))
  2415. return PTR_ERR(vs);
  2416. #if IS_ENABLED(CONFIG_IPV6)
  2417. if (ipv6) {
  2418. rcu_assign_pointer(vxlan->vn6_sock, vs);
  2419. node = &vxlan->hlist6;
  2420. } else
  2421. #endif
  2422. {
  2423. rcu_assign_pointer(vxlan->vn4_sock, vs);
  2424. node = &vxlan->hlist4;
  2425. }
  2426. vxlan_vs_add_dev(vs, vxlan, node);
  2427. return 0;
  2428. }
  2429. static int vxlan_sock_add(struct vxlan_dev *vxlan)
  2430. {
  2431. bool metadata = vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA;
  2432. bool ipv6 = vxlan->cfg.flags & VXLAN_F_IPV6 || metadata;
  2433. bool ipv4 = !ipv6 || metadata;
  2434. int ret = 0;
  2435. RCU_INIT_POINTER(vxlan->vn4_sock, NULL);
  2436. #if IS_ENABLED(CONFIG_IPV6)
  2437. RCU_INIT_POINTER(vxlan->vn6_sock, NULL);
  2438. if (ipv6) {
  2439. ret = __vxlan_sock_add(vxlan, true);
  2440. if (ret < 0 && ret != -EAFNOSUPPORT)
  2441. ipv4 = false;
  2442. }
  2443. #endif
  2444. if (ipv4)
  2445. ret = __vxlan_sock_add(vxlan, false);
  2446. if (ret < 0)
  2447. vxlan_sock_release(vxlan);
  2448. return ret;
  2449. }
  2450. static int vxlan_config_validate(struct net *src_net, struct vxlan_config *conf,
  2451. struct net_device **lower,
  2452. struct vxlan_dev *old,
  2453. struct netlink_ext_ack *extack)
  2454. {
  2455. struct vxlan_net *vn = net_generic(src_net, vxlan_net_id);
  2456. struct vxlan_dev *tmp;
  2457. bool use_ipv6 = false;
  2458. if (conf->flags & VXLAN_F_GPE) {
  2459. /* For now, allow GPE only together with
  2460. * COLLECT_METADATA. This can be relaxed later; in such
  2461. * case, the other side of the PtP link will have to be
  2462. * provided.
  2463. */
  2464. if ((conf->flags & ~VXLAN_F_ALLOWED_GPE) ||
  2465. !(conf->flags & VXLAN_F_COLLECT_METADATA)) {
  2466. NL_SET_ERR_MSG(extack,
  2467. "VXLAN GPE does not support this combination of attributes");
  2468. return -EINVAL;
  2469. }
  2470. }
  2471. if (!conf->remote_ip.sa.sa_family && !conf->saddr.sa.sa_family) {
  2472. /* Unless IPv6 is explicitly requested, assume IPv4 */
  2473. conf->remote_ip.sa.sa_family = AF_INET;
  2474. conf->saddr.sa.sa_family = AF_INET;
  2475. } else if (!conf->remote_ip.sa.sa_family) {
  2476. conf->remote_ip.sa.sa_family = conf->saddr.sa.sa_family;
  2477. } else if (!conf->saddr.sa.sa_family) {
  2478. conf->saddr.sa.sa_family = conf->remote_ip.sa.sa_family;
  2479. }
  2480. if (conf->saddr.sa.sa_family != conf->remote_ip.sa.sa_family) {
  2481. NL_SET_ERR_MSG(extack,
  2482. "Local and remote address must be from the same family");
  2483. return -EINVAL;
  2484. }
  2485. if (vxlan_addr_multicast(&conf->saddr)) {
  2486. NL_SET_ERR_MSG(extack, "Local address cannot be multicast");
  2487. return -EINVAL;
  2488. }
  2489. if (conf->saddr.sa.sa_family == AF_INET6) {
  2490. if (!IS_ENABLED(CONFIG_IPV6)) {
  2491. NL_SET_ERR_MSG(extack,
  2492. "IPv6 support not enabled in the kernel");
  2493. return -EPFNOSUPPORT;
  2494. }
  2495. use_ipv6 = true;
  2496. conf->flags |= VXLAN_F_IPV6;
  2497. if (!(conf->flags & VXLAN_F_COLLECT_METADATA)) {
  2498. int local_type =
  2499. ipv6_addr_type(&conf->saddr.sin6.sin6_addr);
  2500. int remote_type =
  2501. ipv6_addr_type(&conf->remote_ip.sin6.sin6_addr);
  2502. if (local_type & IPV6_ADDR_LINKLOCAL) {
  2503. if (!(remote_type & IPV6_ADDR_LINKLOCAL) &&
  2504. (remote_type != IPV6_ADDR_ANY)) {
  2505. NL_SET_ERR_MSG(extack,
  2506. "Invalid combination of local and remote address scopes");
  2507. return -EINVAL;
  2508. }
  2509. conf->flags |= VXLAN_F_IPV6_LINKLOCAL;
  2510. } else {
  2511. if (remote_type ==
  2512. (IPV6_ADDR_UNICAST | IPV6_ADDR_LINKLOCAL)) {
  2513. NL_SET_ERR_MSG(extack,
  2514. "Invalid combination of local and remote address scopes");
  2515. return -EINVAL;
  2516. }
  2517. conf->flags &= ~VXLAN_F_IPV6_LINKLOCAL;
  2518. }
  2519. }
  2520. }
  2521. if (conf->label && !use_ipv6) {
  2522. NL_SET_ERR_MSG(extack,
  2523. "Label attribute only applies to IPv6 VXLAN devices");
  2524. return -EINVAL;
  2525. }
  2526. if (conf->remote_ifindex) {
  2527. struct net_device *lowerdev;
  2528. lowerdev = __dev_get_by_index(src_net, conf->remote_ifindex);
  2529. if (!lowerdev) {
  2530. NL_SET_ERR_MSG(extack,
  2531. "Invalid local interface, device not found");
  2532. return -ENODEV;
  2533. }
  2534. #if IS_ENABLED(CONFIG_IPV6)
  2535. if (use_ipv6) {
  2536. struct inet6_dev *idev = __in6_dev_get(lowerdev);
  2537. if (idev && idev->cnf.disable_ipv6) {
  2538. NL_SET_ERR_MSG(extack,
  2539. "IPv6 support disabled by administrator");
  2540. return -EPERM;
  2541. }
  2542. }
  2543. #endif
  2544. *lower = lowerdev;
  2545. } else {
  2546. if (vxlan_addr_multicast(&conf->remote_ip)) {
  2547. NL_SET_ERR_MSG(extack,
  2548. "Local interface required for multicast remote destination");
  2549. return -EINVAL;
  2550. }
  2551. #if IS_ENABLED(CONFIG_IPV6)
  2552. if (conf->flags & VXLAN_F_IPV6_LINKLOCAL) {
  2553. NL_SET_ERR_MSG(extack,
  2554. "Local interface required for link-local local/remote addresses");
  2555. return -EINVAL;
  2556. }
  2557. #endif
  2558. *lower = NULL;
  2559. }
  2560. if (!conf->dst_port) {
  2561. if (conf->flags & VXLAN_F_GPE)
  2562. conf->dst_port = htons(4790); /* IANA VXLAN-GPE port */
  2563. else
  2564. conf->dst_port = htons(vxlan_port);
  2565. }
  2566. if (!conf->age_interval)
  2567. conf->age_interval = FDB_AGE_DEFAULT;
  2568. list_for_each_entry(tmp, &vn->vxlan_list, next) {
  2569. if (tmp == old)
  2570. continue;
  2571. if (tmp->cfg.vni != conf->vni)
  2572. continue;
  2573. if (tmp->cfg.dst_port != conf->dst_port)
  2574. continue;
  2575. if ((tmp->cfg.flags & (VXLAN_F_RCV_FLAGS | VXLAN_F_IPV6)) !=
  2576. (conf->flags & (VXLAN_F_RCV_FLAGS | VXLAN_F_IPV6)))
  2577. continue;
  2578. if ((conf->flags & VXLAN_F_IPV6_LINKLOCAL) &&
  2579. tmp->cfg.remote_ifindex != conf->remote_ifindex)
  2580. continue;
  2581. NL_SET_ERR_MSG(extack,
  2582. "A VXLAN device with the specified VNI already exists");
  2583. return -EEXIST;
  2584. }
  2585. return 0;
  2586. }
  2587. static void vxlan_config_apply(struct net_device *dev,
  2588. struct vxlan_config *conf,
  2589. struct net_device *lowerdev,
  2590. struct net *src_net,
  2591. bool changelink)
  2592. {
  2593. struct vxlan_dev *vxlan = netdev_priv(dev);
  2594. struct vxlan_rdst *dst = &vxlan->default_dst;
  2595. unsigned short needed_headroom = ETH_HLEN;
  2596. bool use_ipv6 = !!(conf->flags & VXLAN_F_IPV6);
  2597. int max_mtu = ETH_MAX_MTU;
  2598. if (!changelink) {
  2599. if (conf->flags & VXLAN_F_GPE)
  2600. vxlan_raw_setup(dev);
  2601. else
  2602. vxlan_ether_setup(dev);
  2603. if (conf->mtu)
  2604. dev->mtu = conf->mtu;
  2605. vxlan->net = src_net;
  2606. }
  2607. dst->remote_vni = conf->vni;
  2608. memcpy(&dst->remote_ip, &conf->remote_ip, sizeof(conf->remote_ip));
  2609. if (lowerdev) {
  2610. dst->remote_ifindex = conf->remote_ifindex;
  2611. dev->gso_max_size = lowerdev->gso_max_size;
  2612. dev->gso_max_segs = lowerdev->gso_max_segs;
  2613. needed_headroom = lowerdev->hard_header_len;
  2614. max_mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM :
  2615. VXLAN_HEADROOM);
  2616. if (max_mtu < ETH_MIN_MTU)
  2617. max_mtu = ETH_MIN_MTU;
  2618. if (!changelink && !conf->mtu)
  2619. dev->mtu = max_mtu;
  2620. }
  2621. if (dev->mtu > max_mtu)
  2622. dev->mtu = max_mtu;
  2623. if (use_ipv6 || conf->flags & VXLAN_F_COLLECT_METADATA)
  2624. needed_headroom += VXLAN6_HEADROOM;
  2625. else
  2626. needed_headroom += VXLAN_HEADROOM;
  2627. dev->needed_headroom = needed_headroom;
  2628. memcpy(&vxlan->cfg, conf, sizeof(*conf));
  2629. }
  2630. static int vxlan_dev_configure(struct net *src_net, struct net_device *dev,
  2631. struct vxlan_config *conf, bool changelink,
  2632. struct netlink_ext_ack *extack)
  2633. {
  2634. struct vxlan_dev *vxlan = netdev_priv(dev);
  2635. struct net_device *lowerdev;
  2636. int ret;
  2637. ret = vxlan_config_validate(src_net, conf, &lowerdev, vxlan, extack);
  2638. if (ret)
  2639. return ret;
  2640. vxlan_config_apply(dev, conf, lowerdev, src_net, changelink);
  2641. return 0;
  2642. }
  2643. static int __vxlan_dev_create(struct net *net, struct net_device *dev,
  2644. struct vxlan_config *conf,
  2645. struct netlink_ext_ack *extack)
  2646. {
  2647. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  2648. struct vxlan_dev *vxlan = netdev_priv(dev);
  2649. int err;
  2650. err = vxlan_dev_configure(net, dev, conf, false, extack);
  2651. if (err)
  2652. return err;
  2653. dev->ethtool_ops = &vxlan_ethtool_ops;
  2654. /* create an fdb entry for a valid default destination */
  2655. if (!vxlan_addr_any(&vxlan->default_dst.remote_ip)) {
  2656. err = vxlan_fdb_create(vxlan, all_zeros_mac,
  2657. &vxlan->default_dst.remote_ip,
  2658. NUD_REACHABLE | NUD_PERMANENT,
  2659. NLM_F_EXCL | NLM_F_CREATE,
  2660. vxlan->cfg.dst_port,
  2661. vxlan->default_dst.remote_vni,
  2662. vxlan->default_dst.remote_vni,
  2663. vxlan->default_dst.remote_ifindex,
  2664. NTF_SELF);
  2665. if (err)
  2666. return err;
  2667. }
  2668. err = register_netdevice(dev);
  2669. if (err) {
  2670. vxlan_fdb_delete_default(vxlan, vxlan->default_dst.remote_vni);
  2671. return err;
  2672. }
  2673. list_add(&vxlan->next, &vn->vxlan_list);
  2674. return 0;
  2675. }
  2676. static int vxlan_nl2conf(struct nlattr *tb[], struct nlattr *data[],
  2677. struct net_device *dev, struct vxlan_config *conf,
  2678. bool changelink)
  2679. {
  2680. struct vxlan_dev *vxlan = netdev_priv(dev);
  2681. memset(conf, 0, sizeof(*conf));
  2682. /* if changelink operation, start with old existing cfg */
  2683. if (changelink)
  2684. memcpy(conf, &vxlan->cfg, sizeof(*conf));
  2685. if (data[IFLA_VXLAN_ID]) {
  2686. __be32 vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID]));
  2687. if (changelink && (vni != conf->vni))
  2688. return -EOPNOTSUPP;
  2689. conf->vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID]));
  2690. }
  2691. if (data[IFLA_VXLAN_GROUP]) {
  2692. if (changelink && (conf->remote_ip.sa.sa_family != AF_INET))
  2693. return -EOPNOTSUPP;
  2694. conf->remote_ip.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_GROUP]);
  2695. conf->remote_ip.sa.sa_family = AF_INET;
  2696. } else if (data[IFLA_VXLAN_GROUP6]) {
  2697. if (!IS_ENABLED(CONFIG_IPV6))
  2698. return -EPFNOSUPPORT;
  2699. if (changelink && (conf->remote_ip.sa.sa_family != AF_INET6))
  2700. return -EOPNOTSUPP;
  2701. conf->remote_ip.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_GROUP6]);
  2702. conf->remote_ip.sa.sa_family = AF_INET6;
  2703. }
  2704. if (data[IFLA_VXLAN_LOCAL]) {
  2705. if (changelink && (conf->saddr.sa.sa_family != AF_INET))
  2706. return -EOPNOTSUPP;
  2707. conf->saddr.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_LOCAL]);
  2708. conf->saddr.sa.sa_family = AF_INET;
  2709. } else if (data[IFLA_VXLAN_LOCAL6]) {
  2710. if (!IS_ENABLED(CONFIG_IPV6))
  2711. return -EPFNOSUPPORT;
  2712. if (changelink && (conf->saddr.sa.sa_family != AF_INET6))
  2713. return -EOPNOTSUPP;
  2714. /* TODO: respect scope id */
  2715. conf->saddr.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_LOCAL6]);
  2716. conf->saddr.sa.sa_family = AF_INET6;
  2717. }
  2718. if (data[IFLA_VXLAN_LINK])
  2719. conf->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]);
  2720. if (data[IFLA_VXLAN_TOS])
  2721. conf->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
  2722. if (data[IFLA_VXLAN_TTL])
  2723. conf->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
  2724. if (data[IFLA_VXLAN_TTL_INHERIT]) {
  2725. if (changelink)
  2726. return -EOPNOTSUPP;
  2727. conf->flags |= VXLAN_F_TTL_INHERIT;
  2728. }
  2729. if (data[IFLA_VXLAN_LABEL])
  2730. conf->label = nla_get_be32(data[IFLA_VXLAN_LABEL]) &
  2731. IPV6_FLOWLABEL_MASK;
  2732. if (data[IFLA_VXLAN_LEARNING]) {
  2733. if (nla_get_u8(data[IFLA_VXLAN_LEARNING]))
  2734. conf->flags |= VXLAN_F_LEARN;
  2735. else
  2736. conf->flags &= ~VXLAN_F_LEARN;
  2737. } else if (!changelink) {
  2738. /* default to learn on a new device */
  2739. conf->flags |= VXLAN_F_LEARN;
  2740. }
  2741. if (data[IFLA_VXLAN_AGEING]) {
  2742. if (changelink)
  2743. return -EOPNOTSUPP;
  2744. conf->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
  2745. }
  2746. if (data[IFLA_VXLAN_PROXY]) {
  2747. if (changelink)
  2748. return -EOPNOTSUPP;
  2749. if (nla_get_u8(data[IFLA_VXLAN_PROXY]))
  2750. conf->flags |= VXLAN_F_PROXY;
  2751. }
  2752. if (data[IFLA_VXLAN_RSC]) {
  2753. if (changelink)
  2754. return -EOPNOTSUPP;
  2755. if (nla_get_u8(data[IFLA_VXLAN_RSC]))
  2756. conf->flags |= VXLAN_F_RSC;
  2757. }
  2758. if (data[IFLA_VXLAN_L2MISS]) {
  2759. if (changelink)
  2760. return -EOPNOTSUPP;
  2761. if (nla_get_u8(data[IFLA_VXLAN_L2MISS]))
  2762. conf->flags |= VXLAN_F_L2MISS;
  2763. }
  2764. if (data[IFLA_VXLAN_L3MISS]) {
  2765. if (changelink)
  2766. return -EOPNOTSUPP;
  2767. if (nla_get_u8(data[IFLA_VXLAN_L3MISS]))
  2768. conf->flags |= VXLAN_F_L3MISS;
  2769. }
  2770. if (data[IFLA_VXLAN_LIMIT]) {
  2771. if (changelink)
  2772. return -EOPNOTSUPP;
  2773. conf->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
  2774. }
  2775. if (data[IFLA_VXLAN_COLLECT_METADATA]) {
  2776. if (changelink)
  2777. return -EOPNOTSUPP;
  2778. if (nla_get_u8(data[IFLA_VXLAN_COLLECT_METADATA]))
  2779. conf->flags |= VXLAN_F_COLLECT_METADATA;
  2780. }
  2781. if (data[IFLA_VXLAN_PORT_RANGE]) {
  2782. if (!changelink) {
  2783. const struct ifla_vxlan_port_range *p
  2784. = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
  2785. conf->port_min = ntohs(p->low);
  2786. conf->port_max = ntohs(p->high);
  2787. } else {
  2788. return -EOPNOTSUPP;
  2789. }
  2790. }
  2791. if (data[IFLA_VXLAN_PORT]) {
  2792. if (changelink)
  2793. return -EOPNOTSUPP;
  2794. conf->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
  2795. }
  2796. if (data[IFLA_VXLAN_UDP_CSUM]) {
  2797. if (changelink)
  2798. return -EOPNOTSUPP;
  2799. if (!nla_get_u8(data[IFLA_VXLAN_UDP_CSUM]))
  2800. conf->flags |= VXLAN_F_UDP_ZERO_CSUM_TX;
  2801. }
  2802. if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]) {
  2803. if (changelink)
  2804. return -EOPNOTSUPP;
  2805. if (nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]))
  2806. conf->flags |= VXLAN_F_UDP_ZERO_CSUM6_TX;
  2807. }
  2808. if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]) {
  2809. if (changelink)
  2810. return -EOPNOTSUPP;
  2811. if (nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]))
  2812. conf->flags |= VXLAN_F_UDP_ZERO_CSUM6_RX;
  2813. }
  2814. if (data[IFLA_VXLAN_REMCSUM_TX]) {
  2815. if (changelink)
  2816. return -EOPNOTSUPP;
  2817. if (nla_get_u8(data[IFLA_VXLAN_REMCSUM_TX]))
  2818. conf->flags |= VXLAN_F_REMCSUM_TX;
  2819. }
  2820. if (data[IFLA_VXLAN_REMCSUM_RX]) {
  2821. if (changelink)
  2822. return -EOPNOTSUPP;
  2823. if (nla_get_u8(data[IFLA_VXLAN_REMCSUM_RX]))
  2824. conf->flags |= VXLAN_F_REMCSUM_RX;
  2825. }
  2826. if (data[IFLA_VXLAN_GBP]) {
  2827. if (changelink)
  2828. return -EOPNOTSUPP;
  2829. conf->flags |= VXLAN_F_GBP;
  2830. }
  2831. if (data[IFLA_VXLAN_GPE]) {
  2832. if (changelink)
  2833. return -EOPNOTSUPP;
  2834. conf->flags |= VXLAN_F_GPE;
  2835. }
  2836. if (data[IFLA_VXLAN_REMCSUM_NOPARTIAL]) {
  2837. if (changelink)
  2838. return -EOPNOTSUPP;
  2839. conf->flags |= VXLAN_F_REMCSUM_NOPARTIAL;
  2840. }
  2841. if (tb[IFLA_MTU]) {
  2842. if (changelink)
  2843. return -EOPNOTSUPP;
  2844. conf->mtu = nla_get_u32(tb[IFLA_MTU]);
  2845. }
  2846. return 0;
  2847. }
  2848. static int vxlan_newlink(struct net *src_net, struct net_device *dev,
  2849. struct nlattr *tb[], struct nlattr *data[],
  2850. struct netlink_ext_ack *extack)
  2851. {
  2852. struct vxlan_config conf;
  2853. int err;
  2854. err = vxlan_nl2conf(tb, data, dev, &conf, false);
  2855. if (err)
  2856. return err;
  2857. return __vxlan_dev_create(src_net, dev, &conf, extack);
  2858. }
  2859. static int vxlan_changelink(struct net_device *dev, struct nlattr *tb[],
  2860. struct nlattr *data[],
  2861. struct netlink_ext_ack *extack)
  2862. {
  2863. struct vxlan_dev *vxlan = netdev_priv(dev);
  2864. struct vxlan_rdst *dst = &vxlan->default_dst;
  2865. struct vxlan_rdst old_dst;
  2866. struct vxlan_config conf;
  2867. int err;
  2868. err = vxlan_nl2conf(tb, data,
  2869. dev, &conf, true);
  2870. if (err)
  2871. return err;
  2872. memcpy(&old_dst, dst, sizeof(struct vxlan_rdst));
  2873. err = vxlan_dev_configure(vxlan->net, dev, &conf, true, extack);
  2874. if (err)
  2875. return err;
  2876. /* handle default dst entry */
  2877. if (!vxlan_addr_equal(&dst->remote_ip, &old_dst.remote_ip)) {
  2878. spin_lock_bh(&vxlan->hash_lock);
  2879. if (!vxlan_addr_any(&old_dst.remote_ip))
  2880. __vxlan_fdb_delete(vxlan, all_zeros_mac,
  2881. old_dst.remote_ip,
  2882. vxlan->cfg.dst_port,
  2883. old_dst.remote_vni,
  2884. old_dst.remote_vni,
  2885. old_dst.remote_ifindex, 0);
  2886. if (!vxlan_addr_any(&dst->remote_ip)) {
  2887. err = vxlan_fdb_create(vxlan, all_zeros_mac,
  2888. &dst->remote_ip,
  2889. NUD_REACHABLE | NUD_PERMANENT,
  2890. NLM_F_CREATE | NLM_F_APPEND,
  2891. vxlan->cfg.dst_port,
  2892. dst->remote_vni,
  2893. dst->remote_vni,
  2894. dst->remote_ifindex,
  2895. NTF_SELF);
  2896. if (err) {
  2897. spin_unlock_bh(&vxlan->hash_lock);
  2898. return err;
  2899. }
  2900. }
  2901. spin_unlock_bh(&vxlan->hash_lock);
  2902. }
  2903. return 0;
  2904. }
  2905. static void vxlan_dellink(struct net_device *dev, struct list_head *head)
  2906. {
  2907. struct vxlan_dev *vxlan = netdev_priv(dev);
  2908. vxlan_flush(vxlan, true);
  2909. gro_cells_destroy(&vxlan->gro_cells);
  2910. list_del(&vxlan->next);
  2911. unregister_netdevice_queue(dev, head);
  2912. }
  2913. static size_t vxlan_get_size(const struct net_device *dev)
  2914. {
  2915. return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
  2916. nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
  2917. nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
  2918. nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
  2919. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
  2920. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
  2921. nla_total_size(sizeof(__be32)) + /* IFLA_VXLAN_LABEL */
  2922. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
  2923. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
  2924. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
  2925. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
  2926. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
  2927. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_COLLECT_METADATA */
  2928. nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
  2929. nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
  2930. nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
  2931. nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */
  2932. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */
  2933. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
  2934. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
  2935. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_TX */
  2936. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_RX */
  2937. 0;
  2938. }
  2939. static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
  2940. {
  2941. const struct vxlan_dev *vxlan = netdev_priv(dev);
  2942. const struct vxlan_rdst *dst = &vxlan->default_dst;
  2943. struct ifla_vxlan_port_range ports = {
  2944. .low = htons(vxlan->cfg.port_min),
  2945. .high = htons(vxlan->cfg.port_max),
  2946. };
  2947. if (nla_put_u32(skb, IFLA_VXLAN_ID, be32_to_cpu(dst->remote_vni)))
  2948. goto nla_put_failure;
  2949. if (!vxlan_addr_any(&dst->remote_ip)) {
  2950. if (dst->remote_ip.sa.sa_family == AF_INET) {
  2951. if (nla_put_in_addr(skb, IFLA_VXLAN_GROUP,
  2952. dst->remote_ip.sin.sin_addr.s_addr))
  2953. goto nla_put_failure;
  2954. #if IS_ENABLED(CONFIG_IPV6)
  2955. } else {
  2956. if (nla_put_in6_addr(skb, IFLA_VXLAN_GROUP6,
  2957. &dst->remote_ip.sin6.sin6_addr))
  2958. goto nla_put_failure;
  2959. #endif
  2960. }
  2961. }
  2962. if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
  2963. goto nla_put_failure;
  2964. if (!vxlan_addr_any(&vxlan->cfg.saddr)) {
  2965. if (vxlan->cfg.saddr.sa.sa_family == AF_INET) {
  2966. if (nla_put_in_addr(skb, IFLA_VXLAN_LOCAL,
  2967. vxlan->cfg.saddr.sin.sin_addr.s_addr))
  2968. goto nla_put_failure;
  2969. #if IS_ENABLED(CONFIG_IPV6)
  2970. } else {
  2971. if (nla_put_in6_addr(skb, IFLA_VXLAN_LOCAL6,
  2972. &vxlan->cfg.saddr.sin6.sin6_addr))
  2973. goto nla_put_failure;
  2974. #endif
  2975. }
  2976. }
  2977. if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->cfg.ttl) ||
  2978. nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->cfg.tos) ||
  2979. nla_put_be32(skb, IFLA_VXLAN_LABEL, vxlan->cfg.label) ||
  2980. nla_put_u8(skb, IFLA_VXLAN_LEARNING,
  2981. !!(vxlan->cfg.flags & VXLAN_F_LEARN)) ||
  2982. nla_put_u8(skb, IFLA_VXLAN_PROXY,
  2983. !!(vxlan->cfg.flags & VXLAN_F_PROXY)) ||
  2984. nla_put_u8(skb, IFLA_VXLAN_RSC,
  2985. !!(vxlan->cfg.flags & VXLAN_F_RSC)) ||
  2986. nla_put_u8(skb, IFLA_VXLAN_L2MISS,
  2987. !!(vxlan->cfg.flags & VXLAN_F_L2MISS)) ||
  2988. nla_put_u8(skb, IFLA_VXLAN_L3MISS,
  2989. !!(vxlan->cfg.flags & VXLAN_F_L3MISS)) ||
  2990. nla_put_u8(skb, IFLA_VXLAN_COLLECT_METADATA,
  2991. !!(vxlan->cfg.flags & VXLAN_F_COLLECT_METADATA)) ||
  2992. nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->cfg.age_interval) ||
  2993. nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->cfg.addrmax) ||
  2994. nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->cfg.dst_port) ||
  2995. nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM,
  2996. !(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM_TX)) ||
  2997. nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
  2998. !!(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) ||
  2999. nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
  3000. !!(vxlan->cfg.flags & VXLAN_F_UDP_ZERO_CSUM6_RX)) ||
  3001. nla_put_u8(skb, IFLA_VXLAN_REMCSUM_TX,
  3002. !!(vxlan->cfg.flags & VXLAN_F_REMCSUM_TX)) ||
  3003. nla_put_u8(skb, IFLA_VXLAN_REMCSUM_RX,
  3004. !!(vxlan->cfg.flags & VXLAN_F_REMCSUM_RX)))
  3005. goto nla_put_failure;
  3006. if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
  3007. goto nla_put_failure;
  3008. if (vxlan->cfg.flags & VXLAN_F_GBP &&
  3009. nla_put_flag(skb, IFLA_VXLAN_GBP))
  3010. goto nla_put_failure;
  3011. if (vxlan->cfg.flags & VXLAN_F_GPE &&
  3012. nla_put_flag(skb, IFLA_VXLAN_GPE))
  3013. goto nla_put_failure;
  3014. if (vxlan->cfg.flags & VXLAN_F_REMCSUM_NOPARTIAL &&
  3015. nla_put_flag(skb, IFLA_VXLAN_REMCSUM_NOPARTIAL))
  3016. goto nla_put_failure;
  3017. return 0;
  3018. nla_put_failure:
  3019. return -EMSGSIZE;
  3020. }
  3021. static struct net *vxlan_get_link_net(const struct net_device *dev)
  3022. {
  3023. struct vxlan_dev *vxlan = netdev_priv(dev);
  3024. return vxlan->net;
  3025. }
  3026. static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
  3027. .kind = "vxlan",
  3028. .maxtype = IFLA_VXLAN_MAX,
  3029. .policy = vxlan_policy,
  3030. .priv_size = sizeof(struct vxlan_dev),
  3031. .setup = vxlan_setup,
  3032. .validate = vxlan_validate,
  3033. .newlink = vxlan_newlink,
  3034. .changelink = vxlan_changelink,
  3035. .dellink = vxlan_dellink,
  3036. .get_size = vxlan_get_size,
  3037. .fill_info = vxlan_fill_info,
  3038. .get_link_net = vxlan_get_link_net,
  3039. };
  3040. struct net_device *vxlan_dev_create(struct net *net, const char *name,
  3041. u8 name_assign_type,
  3042. struct vxlan_config *conf)
  3043. {
  3044. struct nlattr *tb[IFLA_MAX + 1];
  3045. struct net_device *dev;
  3046. int err;
  3047. memset(&tb, 0, sizeof(tb));
  3048. dev = rtnl_create_link(net, name, name_assign_type,
  3049. &vxlan_link_ops, tb);
  3050. if (IS_ERR(dev))
  3051. return dev;
  3052. err = __vxlan_dev_create(net, dev, conf, NULL);
  3053. if (err < 0) {
  3054. free_netdev(dev);
  3055. return ERR_PTR(err);
  3056. }
  3057. err = rtnl_configure_link(dev, NULL);
  3058. if (err < 0) {
  3059. LIST_HEAD(list_kill);
  3060. vxlan_dellink(dev, &list_kill);
  3061. unregister_netdevice_many(&list_kill);
  3062. return ERR_PTR(err);
  3063. }
  3064. return dev;
  3065. }
  3066. EXPORT_SYMBOL_GPL(vxlan_dev_create);
  3067. static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn,
  3068. struct net_device *dev)
  3069. {
  3070. struct vxlan_dev *vxlan, *next;
  3071. LIST_HEAD(list_kill);
  3072. list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
  3073. struct vxlan_rdst *dst = &vxlan->default_dst;
  3074. /* In case we created vxlan device with carrier
  3075. * and we loose the carrier due to module unload
  3076. * we also need to remove vxlan device. In other
  3077. * cases, it's not necessary and remote_ifindex
  3078. * is 0 here, so no matches.
  3079. */
  3080. if (dst->remote_ifindex == dev->ifindex)
  3081. vxlan_dellink(vxlan->dev, &list_kill);
  3082. }
  3083. unregister_netdevice_many(&list_kill);
  3084. }
  3085. static int vxlan_netdevice_event(struct notifier_block *unused,
  3086. unsigned long event, void *ptr)
  3087. {
  3088. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  3089. struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
  3090. if (event == NETDEV_UNREGISTER) {
  3091. vxlan_offload_rx_ports(dev, false);
  3092. vxlan_handle_lowerdev_unregister(vn, dev);
  3093. } else if (event == NETDEV_REGISTER) {
  3094. vxlan_offload_rx_ports(dev, true);
  3095. } else if (event == NETDEV_UDP_TUNNEL_PUSH_INFO ||
  3096. event == NETDEV_UDP_TUNNEL_DROP_INFO) {
  3097. vxlan_offload_rx_ports(dev, event == NETDEV_UDP_TUNNEL_PUSH_INFO);
  3098. }
  3099. return NOTIFY_DONE;
  3100. }
  3101. static struct notifier_block vxlan_notifier_block __read_mostly = {
  3102. .notifier_call = vxlan_netdevice_event,
  3103. };
  3104. static __net_init int vxlan_init_net(struct net *net)
  3105. {
  3106. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  3107. unsigned int h;
  3108. INIT_LIST_HEAD(&vn->vxlan_list);
  3109. spin_lock_init(&vn->sock_lock);
  3110. for (h = 0; h < PORT_HASH_SIZE; ++h)
  3111. INIT_HLIST_HEAD(&vn->sock_list[h]);
  3112. return 0;
  3113. }
  3114. static void vxlan_destroy_tunnels(struct net *net, struct list_head *head)
  3115. {
  3116. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  3117. struct vxlan_dev *vxlan, *next;
  3118. struct net_device *dev, *aux;
  3119. unsigned int h;
  3120. for_each_netdev_safe(net, dev, aux)
  3121. if (dev->rtnl_link_ops == &vxlan_link_ops)
  3122. unregister_netdevice_queue(dev, head);
  3123. list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
  3124. /* If vxlan->dev is in the same netns, it has already been added
  3125. * to the list by the previous loop.
  3126. */
  3127. if (!net_eq(dev_net(vxlan->dev), net)) {
  3128. gro_cells_destroy(&vxlan->gro_cells);
  3129. unregister_netdevice_queue(vxlan->dev, head);
  3130. }
  3131. }
  3132. for (h = 0; h < PORT_HASH_SIZE; ++h)
  3133. WARN_ON_ONCE(!hlist_empty(&vn->sock_list[h]));
  3134. }
  3135. static void __net_exit vxlan_exit_batch_net(struct list_head *net_list)
  3136. {
  3137. struct net *net;
  3138. LIST_HEAD(list);
  3139. rtnl_lock();
  3140. list_for_each_entry(net, net_list, exit_list)
  3141. vxlan_destroy_tunnels(net, &list);
  3142. unregister_netdevice_many(&list);
  3143. rtnl_unlock();
  3144. }
  3145. static struct pernet_operations vxlan_net_ops = {
  3146. .init = vxlan_init_net,
  3147. .exit_batch = vxlan_exit_batch_net,
  3148. .id = &vxlan_net_id,
  3149. .size = sizeof(struct vxlan_net),
  3150. };
  3151. static int __init vxlan_init_module(void)
  3152. {
  3153. int rc;
  3154. get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
  3155. rc = register_pernet_subsys(&vxlan_net_ops);
  3156. if (rc)
  3157. goto out1;
  3158. rc = register_netdevice_notifier(&vxlan_notifier_block);
  3159. if (rc)
  3160. goto out2;
  3161. rc = rtnl_link_register(&vxlan_link_ops);
  3162. if (rc)
  3163. goto out3;
  3164. return 0;
  3165. out3:
  3166. unregister_netdevice_notifier(&vxlan_notifier_block);
  3167. out2:
  3168. unregister_pernet_subsys(&vxlan_net_ops);
  3169. out1:
  3170. return rc;
  3171. }
  3172. late_initcall(vxlan_init_module);
  3173. static void __exit vxlan_cleanup_module(void)
  3174. {
  3175. rtnl_link_unregister(&vxlan_link_ops);
  3176. unregister_netdevice_notifier(&vxlan_notifier_block);
  3177. unregister_pernet_subsys(&vxlan_net_ops);
  3178. /* rcu_barrier() is called by netns */
  3179. }
  3180. module_exit(vxlan_cleanup_module);
  3181. MODULE_LICENSE("GPL");
  3182. MODULE_VERSION(VXLAN_VERSION);
  3183. MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
  3184. MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
  3185. MODULE_ALIAS_RTNL_LINK("vxlan");