vxlan.c 71 KB

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