vxlan.c 75 KB

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