vxlan.c 76 KB

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