vxlan.c 50 KB

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