vxlan.c 76 KB

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