vxlan.c 71 KB

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