vxlan.c 81 KB

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