vxlan.c 77 KB

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