vxlan.c 73 KB

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