vxlan.c 67 KB

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