vxlan.c 84 KB

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