vxlan.c 76 KB

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