vxlan.c 71 KB

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