vxlan.c 72 KB

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