vxlan.c 82 KB

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