vxlan.c 63 KB

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