vxlan.c 79 KB

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