vxlan.c 78 KB

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