vxlan.c 73 KB

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