vxlan.c 73 KB

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