vxlan.c 72 KB

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