vxlan.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064
  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_rdst(struct rcu_head *head)
  455. {
  456. struct vxlan_rdst *rd = container_of(head, struct vxlan_rdst, rcu);
  457. kfree(rd);
  458. }
  459. static void vxlan_fdb_free(struct rcu_head *head)
  460. {
  461. struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
  462. struct vxlan_rdst *rd, *nd;
  463. list_for_each_entry_safe(rd, nd, &f->remotes, list)
  464. kfree(rd);
  465. kfree(f);
  466. }
  467. static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
  468. {
  469. netdev_dbg(vxlan->dev,
  470. "delete %pM\n", f->eth_addr);
  471. --vxlan->addrcnt;
  472. vxlan_fdb_notify(vxlan, f, RTM_DELNEIGH);
  473. hlist_del_rcu(&f->hlist);
  474. call_rcu(&f->rcu, vxlan_fdb_free);
  475. }
  476. static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
  477. __be32 *ip, __be16 *port, u32 *vni, u32 *ifindex)
  478. {
  479. struct net *net = dev_net(vxlan->dev);
  480. if (tb[NDA_DST]) {
  481. if (nla_len(tb[NDA_DST]) != sizeof(__be32))
  482. return -EAFNOSUPPORT;
  483. *ip = nla_get_be32(tb[NDA_DST]);
  484. } else {
  485. *ip = htonl(INADDR_ANY);
  486. }
  487. if (tb[NDA_PORT]) {
  488. if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
  489. return -EINVAL;
  490. *port = nla_get_be16(tb[NDA_PORT]);
  491. } else {
  492. *port = vxlan->dst_port;
  493. }
  494. if (tb[NDA_VNI]) {
  495. if (nla_len(tb[NDA_VNI]) != sizeof(u32))
  496. return -EINVAL;
  497. *vni = nla_get_u32(tb[NDA_VNI]);
  498. } else {
  499. *vni = vxlan->default_dst.remote_vni;
  500. }
  501. if (tb[NDA_IFINDEX]) {
  502. struct net_device *tdev;
  503. if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
  504. return -EINVAL;
  505. *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
  506. tdev = dev_get_by_index(net, *ifindex);
  507. if (!tdev)
  508. return -EADDRNOTAVAIL;
  509. dev_put(tdev);
  510. } else {
  511. *ifindex = 0;
  512. }
  513. return 0;
  514. }
  515. /* Add static entry (via netlink) */
  516. static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
  517. struct net_device *dev,
  518. const unsigned char *addr, u16 flags)
  519. {
  520. struct vxlan_dev *vxlan = netdev_priv(dev);
  521. /* struct net *net = dev_net(vxlan->dev); */
  522. __be32 ip;
  523. __be16 port;
  524. u32 vni, ifindex;
  525. int err;
  526. if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
  527. pr_info("RTM_NEWNEIGH with invalid state %#x\n",
  528. ndm->ndm_state);
  529. return -EINVAL;
  530. }
  531. if (tb[NDA_DST] == NULL)
  532. return -EINVAL;
  533. err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
  534. if (err)
  535. return err;
  536. spin_lock_bh(&vxlan->hash_lock);
  537. err = vxlan_fdb_create(vxlan, addr, ip, ndm->ndm_state, flags,
  538. port, vni, ifindex, ndm->ndm_flags);
  539. spin_unlock_bh(&vxlan->hash_lock);
  540. return err;
  541. }
  542. /* Delete entry (via netlink) */
  543. static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
  544. struct net_device *dev,
  545. const unsigned char *addr)
  546. {
  547. struct vxlan_dev *vxlan = netdev_priv(dev);
  548. struct vxlan_fdb *f;
  549. struct vxlan_rdst *rd = NULL;
  550. __be32 ip;
  551. __be16 port;
  552. u32 vni, ifindex;
  553. int err;
  554. err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
  555. if (err)
  556. return err;
  557. err = -ENOENT;
  558. spin_lock_bh(&vxlan->hash_lock);
  559. f = vxlan_find_mac(vxlan, addr);
  560. if (!f)
  561. goto out;
  562. if (ip != htonl(INADDR_ANY)) {
  563. rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
  564. if (!rd)
  565. goto out;
  566. }
  567. err = 0;
  568. /* remove a destination if it's not the only one on the list,
  569. * otherwise destroy the fdb entry
  570. */
  571. if (rd && !list_is_singular(&f->remotes)) {
  572. list_del_rcu(&rd->list);
  573. call_rcu(&rd->rcu, vxlan_fdb_free_rdst);
  574. goto out;
  575. }
  576. vxlan_fdb_destroy(vxlan, f);
  577. out:
  578. spin_unlock_bh(&vxlan->hash_lock);
  579. return err;
  580. }
  581. /* Dump forwarding table */
  582. static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
  583. struct net_device *dev, int idx)
  584. {
  585. struct vxlan_dev *vxlan = netdev_priv(dev);
  586. unsigned int h;
  587. for (h = 0; h < FDB_HASH_SIZE; ++h) {
  588. struct vxlan_fdb *f;
  589. int err;
  590. hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
  591. struct vxlan_rdst *rd;
  592. if (idx < cb->args[0])
  593. goto skip;
  594. list_for_each_entry_rcu(rd, &f->remotes, list) {
  595. err = vxlan_fdb_info(skb, vxlan, f,
  596. NETLINK_CB(cb->skb).portid,
  597. cb->nlh->nlmsg_seq,
  598. RTM_NEWNEIGH,
  599. NLM_F_MULTI, rd);
  600. if (err < 0)
  601. goto out;
  602. }
  603. skip:
  604. ++idx;
  605. }
  606. }
  607. out:
  608. return idx;
  609. }
  610. /* Watch incoming packets to learn mapping between Ethernet address
  611. * and Tunnel endpoint.
  612. * Return true if packet is bogus and should be droppped.
  613. */
  614. static bool vxlan_snoop(struct net_device *dev,
  615. __be32 src_ip, const u8 *src_mac)
  616. {
  617. struct vxlan_dev *vxlan = netdev_priv(dev);
  618. struct vxlan_fdb *f;
  619. f = vxlan_find_mac(vxlan, src_mac);
  620. if (likely(f)) {
  621. struct vxlan_rdst *rdst = first_remote_rcu(f);
  622. if (likely(rdst->remote_ip == src_ip))
  623. return false;
  624. /* Don't migrate static entries, drop packets */
  625. if (f->state & NUD_NOARP)
  626. return true;
  627. if (net_ratelimit())
  628. netdev_info(dev,
  629. "%pM migrated from %pI4 to %pI4\n",
  630. src_mac, &rdst->remote_ip, &src_ip);
  631. rdst->remote_ip = src_ip;
  632. f->updated = jiffies;
  633. vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
  634. } else {
  635. /* learned new entry */
  636. spin_lock(&vxlan->hash_lock);
  637. /* close off race between vxlan_flush and incoming packets */
  638. if (netif_running(dev))
  639. vxlan_fdb_create(vxlan, src_mac, src_ip,
  640. NUD_REACHABLE,
  641. NLM_F_EXCL|NLM_F_CREATE,
  642. vxlan->dst_port,
  643. vxlan->default_dst.remote_vni,
  644. 0, NTF_SELF);
  645. spin_unlock(&vxlan->hash_lock);
  646. }
  647. return false;
  648. }
  649. /* See if multicast group is already in use by other ID */
  650. static bool vxlan_group_used(struct vxlan_net *vn, __be32 remote_ip)
  651. {
  652. struct vxlan_dev *vxlan;
  653. list_for_each_entry(vxlan, &vn->vxlan_list, next) {
  654. if (!netif_running(vxlan->dev))
  655. continue;
  656. if (vxlan->default_dst.remote_ip == remote_ip)
  657. return true;
  658. }
  659. return false;
  660. }
  661. static void vxlan_sock_hold(struct vxlan_sock *vs)
  662. {
  663. atomic_inc(&vs->refcnt);
  664. }
  665. void vxlan_sock_release(struct vxlan_sock *vs)
  666. {
  667. struct vxlan_net *vn = net_generic(sock_net(vs->sock->sk), vxlan_net_id);
  668. if (!atomic_dec_and_test(&vs->refcnt))
  669. return;
  670. spin_lock(&vn->sock_lock);
  671. hlist_del_rcu(&vs->hlist);
  672. spin_unlock(&vn->sock_lock);
  673. queue_work(vxlan_wq, &vs->del_work);
  674. }
  675. EXPORT_SYMBOL_GPL(vxlan_sock_release);
  676. /* Callback to update multicast group membership when first VNI on
  677. * multicast asddress is brought up
  678. * Done as workqueue because ip_mc_join_group acquires RTNL.
  679. */
  680. static void vxlan_igmp_join(struct work_struct *work)
  681. {
  682. struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_join);
  683. struct vxlan_sock *vs = vxlan->vn_sock;
  684. struct sock *sk = vs->sock->sk;
  685. struct ip_mreqn mreq = {
  686. .imr_multiaddr.s_addr = vxlan->default_dst.remote_ip,
  687. .imr_ifindex = vxlan->default_dst.remote_ifindex,
  688. };
  689. lock_sock(sk);
  690. ip_mc_join_group(sk, &mreq);
  691. release_sock(sk);
  692. vxlan_sock_release(vs);
  693. dev_put(vxlan->dev);
  694. }
  695. /* Inverse of vxlan_igmp_join when last VNI is brought down */
  696. static void vxlan_igmp_leave(struct work_struct *work)
  697. {
  698. struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_leave);
  699. struct vxlan_sock *vs = vxlan->vn_sock;
  700. struct sock *sk = vs->sock->sk;
  701. struct ip_mreqn mreq = {
  702. .imr_multiaddr.s_addr = vxlan->default_dst.remote_ip,
  703. .imr_ifindex = vxlan->default_dst.remote_ifindex,
  704. };
  705. lock_sock(sk);
  706. ip_mc_leave_group(sk, &mreq);
  707. release_sock(sk);
  708. vxlan_sock_release(vs);
  709. dev_put(vxlan->dev);
  710. }
  711. /* Callback from net/ipv4/udp.c to receive packets */
  712. static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
  713. {
  714. struct vxlan_sock *vs;
  715. struct vxlanhdr *vxh;
  716. __be16 port;
  717. /* Need Vxlan and inner Ethernet header to be present */
  718. if (!pskb_may_pull(skb, VXLAN_HLEN))
  719. goto error;
  720. /* Return packets with reserved bits set */
  721. vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
  722. if (vxh->vx_flags != htonl(VXLAN_FLAGS) ||
  723. (vxh->vx_vni & htonl(0xff))) {
  724. netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
  725. ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
  726. goto error;
  727. }
  728. if (iptunnel_pull_header(skb, VXLAN_HLEN, htons(ETH_P_TEB)))
  729. goto drop;
  730. port = inet_sk(sk)->inet_sport;
  731. vs = vxlan_find_sock(sock_net(sk), port);
  732. if (!vs)
  733. goto drop;
  734. vs->rcv(vs, skb, vxh->vx_vni);
  735. return 0;
  736. drop:
  737. /* Consume bad packet */
  738. kfree_skb(skb);
  739. return 0;
  740. error:
  741. /* Return non vxlan pkt */
  742. return 1;
  743. }
  744. static void vxlan_rcv(struct vxlan_sock *vs,
  745. struct sk_buff *skb, __be32 vx_vni)
  746. {
  747. struct iphdr *oip;
  748. struct vxlan_dev *vxlan;
  749. struct pcpu_tstats *stats;
  750. __u32 vni;
  751. int err;
  752. vni = ntohl(vx_vni) >> 8;
  753. /* Is this VNI defined? */
  754. vxlan = vxlan_vs_find_vni(vs, vni);
  755. if (!vxlan)
  756. goto drop;
  757. skb_reset_mac_header(skb);
  758. skb->protocol = eth_type_trans(skb, vxlan->dev);
  759. /* Ignore packet loops (and multicast echo) */
  760. if (compare_ether_addr(eth_hdr(skb)->h_source,
  761. vxlan->dev->dev_addr) == 0)
  762. goto drop;
  763. /* Re-examine inner Ethernet packet */
  764. oip = ip_hdr(skb);
  765. if ((vxlan->flags & VXLAN_F_LEARN) &&
  766. vxlan_snoop(skb->dev, oip->saddr, eth_hdr(skb)->h_source))
  767. goto drop;
  768. skb_reset_network_header(skb);
  769. /* If the NIC driver gave us an encapsulated packet with
  770. * CHECKSUM_UNNECESSARY and Rx checksum feature is enabled,
  771. * leave the CHECKSUM_UNNECESSARY, the device checksummed it
  772. * for us. Otherwise force the upper layers to verify it.
  773. */
  774. if (skb->ip_summed != CHECKSUM_UNNECESSARY || !skb->encapsulation ||
  775. !(vxlan->dev->features & NETIF_F_RXCSUM))
  776. skb->ip_summed = CHECKSUM_NONE;
  777. skb->encapsulation = 0;
  778. err = IP_ECN_decapsulate(oip, skb);
  779. if (unlikely(err)) {
  780. if (log_ecn_error)
  781. net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
  782. &oip->saddr, oip->tos);
  783. if (err > 1) {
  784. ++vxlan->dev->stats.rx_frame_errors;
  785. ++vxlan->dev->stats.rx_errors;
  786. goto drop;
  787. }
  788. }
  789. stats = this_cpu_ptr(vxlan->dev->tstats);
  790. u64_stats_update_begin(&stats->syncp);
  791. stats->rx_packets++;
  792. stats->rx_bytes += skb->len;
  793. u64_stats_update_end(&stats->syncp);
  794. netif_rx(skb);
  795. return;
  796. drop:
  797. /* Consume bad packet */
  798. kfree_skb(skb);
  799. }
  800. static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
  801. {
  802. struct vxlan_dev *vxlan = netdev_priv(dev);
  803. struct arphdr *parp;
  804. u8 *arpptr, *sha;
  805. __be32 sip, tip;
  806. struct neighbour *n;
  807. if (dev->flags & IFF_NOARP)
  808. goto out;
  809. if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
  810. dev->stats.tx_dropped++;
  811. goto out;
  812. }
  813. parp = arp_hdr(skb);
  814. if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
  815. parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
  816. parp->ar_pro != htons(ETH_P_IP) ||
  817. parp->ar_op != htons(ARPOP_REQUEST) ||
  818. parp->ar_hln != dev->addr_len ||
  819. parp->ar_pln != 4)
  820. goto out;
  821. arpptr = (u8 *)parp + sizeof(struct arphdr);
  822. sha = arpptr;
  823. arpptr += dev->addr_len; /* sha */
  824. memcpy(&sip, arpptr, sizeof(sip));
  825. arpptr += sizeof(sip);
  826. arpptr += dev->addr_len; /* tha */
  827. memcpy(&tip, arpptr, sizeof(tip));
  828. if (ipv4_is_loopback(tip) ||
  829. ipv4_is_multicast(tip))
  830. goto out;
  831. n = neigh_lookup(&arp_tbl, &tip, dev);
  832. if (n) {
  833. struct vxlan_fdb *f;
  834. struct sk_buff *reply;
  835. if (!(n->nud_state & NUD_CONNECTED)) {
  836. neigh_release(n);
  837. goto out;
  838. }
  839. f = vxlan_find_mac(vxlan, n->ha);
  840. if (f && first_remote_rcu(f)->remote_ip == htonl(INADDR_ANY)) {
  841. /* bridge-local neighbor */
  842. neigh_release(n);
  843. goto out;
  844. }
  845. reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
  846. n->ha, sha);
  847. neigh_release(n);
  848. skb_reset_mac_header(reply);
  849. __skb_pull(reply, skb_network_offset(reply));
  850. reply->ip_summed = CHECKSUM_UNNECESSARY;
  851. reply->pkt_type = PACKET_HOST;
  852. if (netif_rx_ni(reply) == NET_RX_DROP)
  853. dev->stats.rx_dropped++;
  854. } else if (vxlan->flags & VXLAN_F_L3MISS)
  855. vxlan_ip_miss(dev, tip);
  856. out:
  857. consume_skb(skb);
  858. return NETDEV_TX_OK;
  859. }
  860. static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
  861. {
  862. struct vxlan_dev *vxlan = netdev_priv(dev);
  863. struct neighbour *n;
  864. struct iphdr *pip;
  865. if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
  866. return false;
  867. n = NULL;
  868. switch (ntohs(eth_hdr(skb)->h_proto)) {
  869. case ETH_P_IP:
  870. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  871. return false;
  872. pip = ip_hdr(skb);
  873. n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
  874. break;
  875. default:
  876. return false;
  877. }
  878. if (n) {
  879. bool diff;
  880. diff = compare_ether_addr(eth_hdr(skb)->h_dest, n->ha) != 0;
  881. if (diff) {
  882. memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
  883. dev->addr_len);
  884. memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
  885. }
  886. neigh_release(n);
  887. return diff;
  888. } else if (vxlan->flags & VXLAN_F_L3MISS)
  889. vxlan_ip_miss(dev, pip->daddr);
  890. return false;
  891. }
  892. static void vxlan_sock_put(struct sk_buff *skb)
  893. {
  894. sock_put(skb->sk);
  895. }
  896. /* On transmit, associate with the tunnel socket */
  897. static void vxlan_set_owner(struct sock *sk, struct sk_buff *skb)
  898. {
  899. skb_orphan(skb);
  900. sock_hold(sk);
  901. skb->sk = sk;
  902. skb->destructor = vxlan_sock_put;
  903. }
  904. /* Compute source port for outgoing packet
  905. * first choice to use L4 flow hash since it will spread
  906. * better and maybe available from hardware
  907. * secondary choice is to use jhash on the Ethernet header
  908. */
  909. __be16 vxlan_src_port(__u16 port_min, __u16 port_max, struct sk_buff *skb)
  910. {
  911. unsigned int range = (port_max - port_min) + 1;
  912. u32 hash;
  913. hash = skb_get_rxhash(skb);
  914. if (!hash)
  915. hash = jhash(skb->data, 2 * ETH_ALEN,
  916. (__force u32) skb->protocol);
  917. return htons((((u64) hash * range) >> 32) + port_min);
  918. }
  919. EXPORT_SYMBOL_GPL(vxlan_src_port);
  920. static int handle_offloads(struct sk_buff *skb)
  921. {
  922. if (skb_is_gso(skb)) {
  923. int err = skb_unclone(skb, GFP_ATOMIC);
  924. if (unlikely(err))
  925. return err;
  926. skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
  927. } else if (skb->ip_summed != CHECKSUM_PARTIAL)
  928. skb->ip_summed = CHECKSUM_NONE;
  929. return 0;
  930. }
  931. int vxlan_xmit_skb(struct net *net, struct vxlan_sock *vs,
  932. struct rtable *rt, struct sk_buff *skb,
  933. __be32 src, __be32 dst, __u8 tos, __u8 ttl, __be16 df,
  934. __be16 src_port, __be16 dst_port, __be32 vni)
  935. {
  936. struct vxlanhdr *vxh;
  937. struct udphdr *uh;
  938. int min_headroom;
  939. int err;
  940. if (!skb->encapsulation) {
  941. skb_reset_inner_headers(skb);
  942. skb->encapsulation = 1;
  943. }
  944. min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
  945. + VXLAN_HLEN + sizeof(struct iphdr)
  946. + (vlan_tx_tag_present(skb) ? VLAN_HLEN : 0);
  947. /* Need space for new headers (invalidates iph ptr) */
  948. err = skb_cow_head(skb, min_headroom);
  949. if (unlikely(err))
  950. return err;
  951. if (vlan_tx_tag_present(skb)) {
  952. if (WARN_ON(!__vlan_put_tag(skb,
  953. skb->vlan_proto,
  954. vlan_tx_tag_get(skb))))
  955. return -ENOMEM;
  956. skb->vlan_tci = 0;
  957. }
  958. vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
  959. vxh->vx_flags = htonl(VXLAN_FLAGS);
  960. vxh->vx_vni = vni;
  961. __skb_push(skb, sizeof(*uh));
  962. skb_reset_transport_header(skb);
  963. uh = udp_hdr(skb);
  964. uh->dest = dst_port;
  965. uh->source = src_port;
  966. uh->len = htons(skb->len);
  967. uh->check = 0;
  968. vxlan_set_owner(vs->sock->sk, skb);
  969. err = handle_offloads(skb);
  970. if (err)
  971. return err;
  972. return iptunnel_xmit(net, rt, skb, src, dst,
  973. IPPROTO_UDP, tos, ttl, df);
  974. }
  975. EXPORT_SYMBOL_GPL(vxlan_xmit_skb);
  976. /* Bypass encapsulation if the destination is local */
  977. static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
  978. struct vxlan_dev *dst_vxlan)
  979. {
  980. struct pcpu_tstats *tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
  981. struct pcpu_tstats *rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
  982. skb->pkt_type = PACKET_HOST;
  983. skb->encapsulation = 0;
  984. skb->dev = dst_vxlan->dev;
  985. __skb_pull(skb, skb_network_offset(skb));
  986. if (dst_vxlan->flags & VXLAN_F_LEARN)
  987. vxlan_snoop(skb->dev, htonl(INADDR_LOOPBACK),
  988. eth_hdr(skb)->h_source);
  989. u64_stats_update_begin(&tx_stats->syncp);
  990. tx_stats->tx_packets++;
  991. tx_stats->tx_bytes += skb->len;
  992. u64_stats_update_end(&tx_stats->syncp);
  993. if (netif_rx(skb) == NET_RX_SUCCESS) {
  994. u64_stats_update_begin(&rx_stats->syncp);
  995. rx_stats->rx_packets++;
  996. rx_stats->rx_bytes += skb->len;
  997. u64_stats_update_end(&rx_stats->syncp);
  998. } else {
  999. skb->dev->stats.rx_dropped++;
  1000. }
  1001. }
  1002. static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
  1003. struct vxlan_rdst *rdst, bool did_rsc)
  1004. {
  1005. struct vxlan_dev *vxlan = netdev_priv(dev);
  1006. struct rtable *rt;
  1007. const struct iphdr *old_iph;
  1008. struct flowi4 fl4;
  1009. __be32 dst;
  1010. __be16 src_port, dst_port;
  1011. u32 vni;
  1012. __be16 df = 0;
  1013. __u8 tos, ttl;
  1014. int err;
  1015. dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port;
  1016. vni = rdst->remote_vni;
  1017. dst = rdst->remote_ip;
  1018. if (!dst) {
  1019. if (did_rsc) {
  1020. /* short-circuited back to local bridge */
  1021. vxlan_encap_bypass(skb, vxlan, vxlan);
  1022. return;
  1023. }
  1024. goto drop;
  1025. }
  1026. old_iph = ip_hdr(skb);
  1027. ttl = vxlan->ttl;
  1028. if (!ttl && IN_MULTICAST(ntohl(dst)))
  1029. ttl = 1;
  1030. tos = vxlan->tos;
  1031. if (tos == 1)
  1032. tos = ip_tunnel_get_dsfield(old_iph, skb);
  1033. src_port = vxlan_src_port(vxlan->port_min, vxlan->port_max, skb);
  1034. memset(&fl4, 0, sizeof(fl4));
  1035. fl4.flowi4_oif = rdst->remote_ifindex;
  1036. fl4.flowi4_tos = RT_TOS(tos);
  1037. fl4.daddr = dst;
  1038. fl4.saddr = vxlan->saddr;
  1039. rt = ip_route_output_key(dev_net(dev), &fl4);
  1040. if (IS_ERR(rt)) {
  1041. netdev_dbg(dev, "no route to %pI4\n", &dst);
  1042. dev->stats.tx_carrier_errors++;
  1043. goto tx_error;
  1044. }
  1045. if (rt->dst.dev == dev) {
  1046. netdev_dbg(dev, "circular route to %pI4\n", &dst);
  1047. dev->stats.collisions++;
  1048. goto rt_tx_error;
  1049. }
  1050. /* Bypass encapsulation if the destination is local */
  1051. if (rt->rt_flags & RTCF_LOCAL &&
  1052. !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
  1053. struct vxlan_dev *dst_vxlan;
  1054. ip_rt_put(rt);
  1055. dst_vxlan = vxlan_find_vni(dev_net(dev), vni, dst_port);
  1056. if (!dst_vxlan)
  1057. goto tx_error;
  1058. vxlan_encap_bypass(skb, vxlan, dst_vxlan);
  1059. return;
  1060. }
  1061. tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
  1062. ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
  1063. err = vxlan_xmit_skb(dev_net(dev), vxlan->vn_sock, rt, skb,
  1064. fl4.saddr, dst, tos, ttl, df,
  1065. src_port, dst_port, htonl(vni << 8));
  1066. if (err < 0)
  1067. goto rt_tx_error;
  1068. iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
  1069. return;
  1070. drop:
  1071. dev->stats.tx_dropped++;
  1072. goto tx_free;
  1073. rt_tx_error:
  1074. ip_rt_put(rt);
  1075. tx_error:
  1076. dev->stats.tx_errors++;
  1077. tx_free:
  1078. dev_kfree_skb(skb);
  1079. }
  1080. /* Transmit local packets over Vxlan
  1081. *
  1082. * Outer IP header inherits ECN and DF from inner header.
  1083. * Outer UDP destination is the VXLAN assigned port.
  1084. * source port is based on hash of flow
  1085. */
  1086. static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
  1087. {
  1088. struct vxlan_dev *vxlan = netdev_priv(dev);
  1089. struct ethhdr *eth;
  1090. bool did_rsc = false;
  1091. struct vxlan_rdst *rdst;
  1092. struct vxlan_fdb *f;
  1093. skb_reset_mac_header(skb);
  1094. eth = eth_hdr(skb);
  1095. if ((vxlan->flags & VXLAN_F_PROXY) && ntohs(eth->h_proto) == ETH_P_ARP)
  1096. return arp_reduce(dev, skb);
  1097. f = vxlan_find_mac(vxlan, eth->h_dest);
  1098. did_rsc = false;
  1099. if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
  1100. ntohs(eth->h_proto) == ETH_P_IP) {
  1101. did_rsc = route_shortcircuit(dev, skb);
  1102. if (did_rsc)
  1103. f = vxlan_find_mac(vxlan, eth->h_dest);
  1104. }
  1105. if (f == NULL) {
  1106. f = vxlan_find_mac(vxlan, all_zeros_mac);
  1107. if (f == NULL) {
  1108. if ((vxlan->flags & VXLAN_F_L2MISS) &&
  1109. !is_multicast_ether_addr(eth->h_dest))
  1110. vxlan_fdb_miss(vxlan, eth->h_dest);
  1111. dev->stats.tx_dropped++;
  1112. dev_kfree_skb(skb);
  1113. return NETDEV_TX_OK;
  1114. }
  1115. }
  1116. list_for_each_entry_rcu(rdst, &f->remotes, list) {
  1117. struct sk_buff *skb1;
  1118. skb1 = skb_clone(skb, GFP_ATOMIC);
  1119. if (skb1)
  1120. vxlan_xmit_one(skb1, dev, rdst, did_rsc);
  1121. }
  1122. dev_kfree_skb(skb);
  1123. return NETDEV_TX_OK;
  1124. }
  1125. /* Walk the forwarding table and purge stale entries */
  1126. static void vxlan_cleanup(unsigned long arg)
  1127. {
  1128. struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
  1129. unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
  1130. unsigned int h;
  1131. if (!netif_running(vxlan->dev))
  1132. return;
  1133. spin_lock_bh(&vxlan->hash_lock);
  1134. for (h = 0; h < FDB_HASH_SIZE; ++h) {
  1135. struct hlist_node *p, *n;
  1136. hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
  1137. struct vxlan_fdb *f
  1138. = container_of(p, struct vxlan_fdb, hlist);
  1139. unsigned long timeout;
  1140. if (f->state & NUD_PERMANENT)
  1141. continue;
  1142. timeout = f->used + vxlan->age_interval * HZ;
  1143. if (time_before_eq(timeout, jiffies)) {
  1144. netdev_dbg(vxlan->dev,
  1145. "garbage collect %pM\n",
  1146. f->eth_addr);
  1147. f->state = NUD_STALE;
  1148. vxlan_fdb_destroy(vxlan, f);
  1149. } else if (time_before(timeout, next_timer))
  1150. next_timer = timeout;
  1151. }
  1152. }
  1153. spin_unlock_bh(&vxlan->hash_lock);
  1154. mod_timer(&vxlan->age_timer, next_timer);
  1155. }
  1156. static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan)
  1157. {
  1158. __u32 vni = vxlan->default_dst.remote_vni;
  1159. vxlan->vn_sock = vs;
  1160. hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
  1161. }
  1162. /* Setup stats when device is created */
  1163. static int vxlan_init(struct net_device *dev)
  1164. {
  1165. struct vxlan_dev *vxlan = netdev_priv(dev);
  1166. struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
  1167. struct vxlan_sock *vs;
  1168. dev->tstats = alloc_percpu(struct pcpu_tstats);
  1169. if (!dev->tstats)
  1170. return -ENOMEM;
  1171. spin_lock(&vn->sock_lock);
  1172. vs = vxlan_find_sock(dev_net(dev), vxlan->dst_port);
  1173. if (vs) {
  1174. /* If we have a socket with same port already, reuse it */
  1175. atomic_inc(&vs->refcnt);
  1176. vxlan_vs_add_dev(vs, vxlan);
  1177. } else {
  1178. /* otherwise make new socket outside of RTNL */
  1179. dev_hold(dev);
  1180. queue_work(vxlan_wq, &vxlan->sock_work);
  1181. }
  1182. spin_unlock(&vn->sock_lock);
  1183. return 0;
  1184. }
  1185. static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan)
  1186. {
  1187. struct vxlan_fdb *f;
  1188. spin_lock_bh(&vxlan->hash_lock);
  1189. f = __vxlan_find_mac(vxlan, all_zeros_mac);
  1190. if (f)
  1191. vxlan_fdb_destroy(vxlan, f);
  1192. spin_unlock_bh(&vxlan->hash_lock);
  1193. }
  1194. static void vxlan_uninit(struct net_device *dev)
  1195. {
  1196. struct vxlan_dev *vxlan = netdev_priv(dev);
  1197. struct vxlan_sock *vs = vxlan->vn_sock;
  1198. vxlan_fdb_delete_default(vxlan);
  1199. if (vs)
  1200. vxlan_sock_release(vs);
  1201. free_percpu(dev->tstats);
  1202. }
  1203. /* Start ageing timer and join group when device is brought up */
  1204. static int vxlan_open(struct net_device *dev)
  1205. {
  1206. struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
  1207. struct vxlan_dev *vxlan = netdev_priv(dev);
  1208. struct vxlan_sock *vs = vxlan->vn_sock;
  1209. /* socket hasn't been created */
  1210. if (!vs)
  1211. return -ENOTCONN;
  1212. if (IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip)) &&
  1213. vxlan_group_used(vn, vxlan->default_dst.remote_ip)) {
  1214. vxlan_sock_hold(vs);
  1215. dev_hold(dev);
  1216. queue_work(vxlan_wq, &vxlan->igmp_join);
  1217. }
  1218. if (vxlan->age_interval)
  1219. mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
  1220. return 0;
  1221. }
  1222. /* Purge the forwarding table */
  1223. static void vxlan_flush(struct vxlan_dev *vxlan)
  1224. {
  1225. unsigned int h;
  1226. spin_lock_bh(&vxlan->hash_lock);
  1227. for (h = 0; h < FDB_HASH_SIZE; ++h) {
  1228. struct hlist_node *p, *n;
  1229. hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
  1230. struct vxlan_fdb *f
  1231. = container_of(p, struct vxlan_fdb, hlist);
  1232. /* the all_zeros_mac entry is deleted at vxlan_uninit */
  1233. if (!is_zero_ether_addr(f->eth_addr))
  1234. vxlan_fdb_destroy(vxlan, f);
  1235. }
  1236. }
  1237. spin_unlock_bh(&vxlan->hash_lock);
  1238. }
  1239. /* Cleanup timer and forwarding table on shutdown */
  1240. static int vxlan_stop(struct net_device *dev)
  1241. {
  1242. struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
  1243. struct vxlan_dev *vxlan = netdev_priv(dev);
  1244. struct vxlan_sock *vs = vxlan->vn_sock;
  1245. if (vs && IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip)) &&
  1246. ! vxlan_group_used(vn, vxlan->default_dst.remote_ip)) {
  1247. vxlan_sock_hold(vs);
  1248. dev_hold(dev);
  1249. queue_work(vxlan_wq, &vxlan->igmp_leave);
  1250. }
  1251. del_timer_sync(&vxlan->age_timer);
  1252. vxlan_flush(vxlan);
  1253. return 0;
  1254. }
  1255. /* Stub, nothing needs to be done. */
  1256. static void vxlan_set_multicast_list(struct net_device *dev)
  1257. {
  1258. }
  1259. static const struct net_device_ops vxlan_netdev_ops = {
  1260. .ndo_init = vxlan_init,
  1261. .ndo_uninit = vxlan_uninit,
  1262. .ndo_open = vxlan_open,
  1263. .ndo_stop = vxlan_stop,
  1264. .ndo_start_xmit = vxlan_xmit,
  1265. .ndo_get_stats64 = ip_tunnel_get_stats64,
  1266. .ndo_set_rx_mode = vxlan_set_multicast_list,
  1267. .ndo_change_mtu = eth_change_mtu,
  1268. .ndo_validate_addr = eth_validate_addr,
  1269. .ndo_set_mac_address = eth_mac_addr,
  1270. .ndo_fdb_add = vxlan_fdb_add,
  1271. .ndo_fdb_del = vxlan_fdb_delete,
  1272. .ndo_fdb_dump = vxlan_fdb_dump,
  1273. };
  1274. /* Info for udev, that this is a virtual tunnel endpoint */
  1275. static struct device_type vxlan_type = {
  1276. .name = "vxlan",
  1277. };
  1278. /* Initialize the device structure. */
  1279. static void vxlan_setup(struct net_device *dev)
  1280. {
  1281. struct vxlan_dev *vxlan = netdev_priv(dev);
  1282. unsigned int h;
  1283. int low, high;
  1284. eth_hw_addr_random(dev);
  1285. ether_setup(dev);
  1286. dev->hard_header_len = ETH_HLEN + VXLAN_HEADROOM;
  1287. dev->netdev_ops = &vxlan_netdev_ops;
  1288. dev->destructor = free_netdev;
  1289. SET_NETDEV_DEVTYPE(dev, &vxlan_type);
  1290. dev->tx_queue_len = 0;
  1291. dev->features |= NETIF_F_LLTX;
  1292. dev->features |= NETIF_F_NETNS_LOCAL;
  1293. dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
  1294. dev->features |= NETIF_F_RXCSUM;
  1295. dev->features |= NETIF_F_GSO_SOFTWARE;
  1296. dev->vlan_features = dev->features;
  1297. dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
  1298. dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
  1299. dev->hw_features |= NETIF_F_GSO_SOFTWARE;
  1300. dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
  1301. dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
  1302. dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
  1303. INIT_LIST_HEAD(&vxlan->next);
  1304. spin_lock_init(&vxlan->hash_lock);
  1305. INIT_WORK(&vxlan->igmp_join, vxlan_igmp_join);
  1306. INIT_WORK(&vxlan->igmp_leave, vxlan_igmp_leave);
  1307. INIT_WORK(&vxlan->sock_work, vxlan_sock_work);
  1308. init_timer_deferrable(&vxlan->age_timer);
  1309. vxlan->age_timer.function = vxlan_cleanup;
  1310. vxlan->age_timer.data = (unsigned long) vxlan;
  1311. inet_get_local_port_range(&low, &high);
  1312. vxlan->port_min = low;
  1313. vxlan->port_max = high;
  1314. vxlan->dst_port = htons(vxlan_port);
  1315. vxlan->dev = dev;
  1316. for (h = 0; h < FDB_HASH_SIZE; ++h)
  1317. INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
  1318. }
  1319. static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
  1320. [IFLA_VXLAN_ID] = { .type = NLA_U32 },
  1321. [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
  1322. [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
  1323. [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
  1324. [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
  1325. [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
  1326. [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
  1327. [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
  1328. [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
  1329. [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
  1330. [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
  1331. [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
  1332. [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
  1333. [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
  1334. [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
  1335. };
  1336. static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
  1337. {
  1338. if (tb[IFLA_ADDRESS]) {
  1339. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
  1340. pr_debug("invalid link address (not ethernet)\n");
  1341. return -EINVAL;
  1342. }
  1343. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
  1344. pr_debug("invalid all zero ethernet address\n");
  1345. return -EADDRNOTAVAIL;
  1346. }
  1347. }
  1348. if (!data)
  1349. return -EINVAL;
  1350. if (data[IFLA_VXLAN_ID]) {
  1351. __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
  1352. if (id >= VXLAN_VID_MASK)
  1353. return -ERANGE;
  1354. }
  1355. if (data[IFLA_VXLAN_PORT_RANGE]) {
  1356. const struct ifla_vxlan_port_range *p
  1357. = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
  1358. if (ntohs(p->high) < ntohs(p->low)) {
  1359. pr_debug("port range %u .. %u not valid\n",
  1360. ntohs(p->low), ntohs(p->high));
  1361. return -EINVAL;
  1362. }
  1363. }
  1364. return 0;
  1365. }
  1366. static void vxlan_get_drvinfo(struct net_device *netdev,
  1367. struct ethtool_drvinfo *drvinfo)
  1368. {
  1369. strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
  1370. strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
  1371. }
  1372. static const struct ethtool_ops vxlan_ethtool_ops = {
  1373. .get_drvinfo = vxlan_get_drvinfo,
  1374. .get_link = ethtool_op_get_link,
  1375. };
  1376. static void vxlan_del_work(struct work_struct *work)
  1377. {
  1378. struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
  1379. sk_release_kernel(vs->sock->sk);
  1380. kfree_rcu(vs, rcu);
  1381. }
  1382. static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port,
  1383. vxlan_rcv_t *rcv, void *data)
  1384. {
  1385. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  1386. struct vxlan_sock *vs;
  1387. struct sock *sk;
  1388. struct sockaddr_in vxlan_addr = {
  1389. .sin_family = AF_INET,
  1390. .sin_addr.s_addr = htonl(INADDR_ANY),
  1391. .sin_port = port,
  1392. };
  1393. int rc;
  1394. unsigned int h;
  1395. vs = kmalloc(sizeof(*vs), GFP_KERNEL);
  1396. if (!vs) {
  1397. pr_debug("memory alocation failure\n");
  1398. return ERR_PTR(-ENOMEM);
  1399. }
  1400. for (h = 0; h < VNI_HASH_SIZE; ++h)
  1401. INIT_HLIST_HEAD(&vs->vni_list[h]);
  1402. INIT_WORK(&vs->del_work, vxlan_del_work);
  1403. /* Create UDP socket for encapsulation receive. */
  1404. rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &vs->sock);
  1405. if (rc < 0) {
  1406. pr_debug("UDP socket create failed\n");
  1407. kfree(vs);
  1408. return ERR_PTR(rc);
  1409. }
  1410. /* Put in proper namespace */
  1411. sk = vs->sock->sk;
  1412. sk_change_net(sk, net);
  1413. rc = kernel_bind(vs->sock, (struct sockaddr *) &vxlan_addr,
  1414. sizeof(vxlan_addr));
  1415. if (rc < 0) {
  1416. pr_debug("bind for UDP socket %pI4:%u (%d)\n",
  1417. &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc);
  1418. sk_release_kernel(sk);
  1419. kfree(vs);
  1420. return ERR_PTR(rc);
  1421. }
  1422. atomic_set(&vs->refcnt, 1);
  1423. vs->rcv = rcv;
  1424. vs->data = data;
  1425. /* Disable multicast loopback */
  1426. inet_sk(sk)->mc_loop = 0;
  1427. spin_lock(&vn->sock_lock);
  1428. hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
  1429. spin_unlock(&vn->sock_lock);
  1430. /* Mark socket as an encapsulation socket. */
  1431. udp_sk(sk)->encap_type = 1;
  1432. udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
  1433. udp_encap_enable();
  1434. return vs;
  1435. }
  1436. struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port,
  1437. vxlan_rcv_t *rcv, void *data,
  1438. bool no_share)
  1439. {
  1440. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  1441. struct vxlan_sock *vs;
  1442. vs = vxlan_socket_create(net, port, rcv, data);
  1443. if (!IS_ERR(vs))
  1444. return vs;
  1445. if (no_share) /* Return error if sharing is not allowed. */
  1446. return vs;
  1447. spin_lock(&vn->sock_lock);
  1448. vs = vxlan_find_sock(net, port);
  1449. if (vs) {
  1450. if (vs->rcv == rcv)
  1451. atomic_inc(&vs->refcnt);
  1452. else
  1453. vs = ERR_PTR(-EBUSY);
  1454. }
  1455. spin_unlock(&vn->sock_lock);
  1456. if (!vs)
  1457. vs = ERR_PTR(-EINVAL);
  1458. return vs;
  1459. }
  1460. EXPORT_SYMBOL_GPL(vxlan_sock_add);
  1461. /* Scheduled at device creation to bind to a socket */
  1462. static void vxlan_sock_work(struct work_struct *work)
  1463. {
  1464. struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, sock_work);
  1465. struct net *net = dev_net(vxlan->dev);
  1466. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  1467. __be16 port = vxlan->dst_port;
  1468. struct vxlan_sock *nvs;
  1469. nvs = vxlan_sock_add(net, port, vxlan_rcv, NULL, false);
  1470. spin_lock(&vn->sock_lock);
  1471. if (!IS_ERR(nvs))
  1472. vxlan_vs_add_dev(nvs, vxlan);
  1473. spin_unlock(&vn->sock_lock);
  1474. dev_put(vxlan->dev);
  1475. }
  1476. static int vxlan_newlink(struct net *net, struct net_device *dev,
  1477. struct nlattr *tb[], struct nlattr *data[])
  1478. {
  1479. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  1480. struct vxlan_dev *vxlan = netdev_priv(dev);
  1481. struct vxlan_rdst *dst = &vxlan->default_dst;
  1482. __u32 vni;
  1483. int err;
  1484. if (!data[IFLA_VXLAN_ID])
  1485. return -EINVAL;
  1486. vni = nla_get_u32(data[IFLA_VXLAN_ID]);
  1487. dst->remote_vni = vni;
  1488. if (data[IFLA_VXLAN_GROUP])
  1489. dst->remote_ip = nla_get_be32(data[IFLA_VXLAN_GROUP]);
  1490. if (data[IFLA_VXLAN_LOCAL])
  1491. vxlan->saddr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
  1492. if (data[IFLA_VXLAN_LINK] &&
  1493. (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
  1494. struct net_device *lowerdev
  1495. = __dev_get_by_index(net, dst->remote_ifindex);
  1496. if (!lowerdev) {
  1497. pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
  1498. return -ENODEV;
  1499. }
  1500. if (!tb[IFLA_MTU])
  1501. dev->mtu = lowerdev->mtu - VXLAN_HEADROOM;
  1502. /* update header length based on lower device */
  1503. dev->hard_header_len = lowerdev->hard_header_len +
  1504. VXLAN_HEADROOM;
  1505. }
  1506. if (data[IFLA_VXLAN_TOS])
  1507. vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
  1508. if (data[IFLA_VXLAN_TTL])
  1509. vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
  1510. if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
  1511. vxlan->flags |= VXLAN_F_LEARN;
  1512. if (data[IFLA_VXLAN_AGEING])
  1513. vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
  1514. else
  1515. vxlan->age_interval = FDB_AGE_DEFAULT;
  1516. if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
  1517. vxlan->flags |= VXLAN_F_PROXY;
  1518. if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
  1519. vxlan->flags |= VXLAN_F_RSC;
  1520. if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
  1521. vxlan->flags |= VXLAN_F_L2MISS;
  1522. if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
  1523. vxlan->flags |= VXLAN_F_L3MISS;
  1524. if (data[IFLA_VXLAN_LIMIT])
  1525. vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
  1526. if (data[IFLA_VXLAN_PORT_RANGE]) {
  1527. const struct ifla_vxlan_port_range *p
  1528. = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
  1529. vxlan->port_min = ntohs(p->low);
  1530. vxlan->port_max = ntohs(p->high);
  1531. }
  1532. if (data[IFLA_VXLAN_PORT])
  1533. vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
  1534. if (vxlan_find_vni(net, vni, vxlan->dst_port)) {
  1535. pr_info("duplicate VNI %u\n", vni);
  1536. return -EEXIST;
  1537. }
  1538. SET_ETHTOOL_OPS(dev, &vxlan_ethtool_ops);
  1539. /* create an fdb entry for default destination */
  1540. err = vxlan_fdb_create(vxlan, all_zeros_mac,
  1541. vxlan->default_dst.remote_ip,
  1542. NUD_REACHABLE|NUD_PERMANENT,
  1543. NLM_F_EXCL|NLM_F_CREATE,
  1544. vxlan->dst_port, vxlan->default_dst.remote_vni,
  1545. vxlan->default_dst.remote_ifindex, NTF_SELF);
  1546. if (err)
  1547. return err;
  1548. err = register_netdevice(dev);
  1549. if (err) {
  1550. vxlan_fdb_delete_default(vxlan);
  1551. return err;
  1552. }
  1553. list_add(&vxlan->next, &vn->vxlan_list);
  1554. return 0;
  1555. }
  1556. static void vxlan_dellink(struct net_device *dev, struct list_head *head)
  1557. {
  1558. struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
  1559. struct vxlan_dev *vxlan = netdev_priv(dev);
  1560. spin_lock(&vn->sock_lock);
  1561. if (!hlist_unhashed(&vxlan->hlist))
  1562. hlist_del_rcu(&vxlan->hlist);
  1563. spin_unlock(&vn->sock_lock);
  1564. list_del(&vxlan->next);
  1565. unregister_netdevice_queue(dev, head);
  1566. }
  1567. static size_t vxlan_get_size(const struct net_device *dev)
  1568. {
  1569. return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
  1570. nla_total_size(sizeof(__be32)) +/* IFLA_VXLAN_GROUP */
  1571. nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
  1572. nla_total_size(sizeof(__be32))+ /* IFLA_VXLAN_LOCAL */
  1573. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
  1574. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
  1575. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
  1576. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
  1577. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
  1578. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
  1579. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
  1580. nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
  1581. nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
  1582. nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
  1583. nla_total_size(sizeof(__be16))+ /* IFLA_VXLAN_PORT */
  1584. 0;
  1585. }
  1586. static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
  1587. {
  1588. const struct vxlan_dev *vxlan = netdev_priv(dev);
  1589. const struct vxlan_rdst *dst = &vxlan->default_dst;
  1590. struct ifla_vxlan_port_range ports = {
  1591. .low = htons(vxlan->port_min),
  1592. .high = htons(vxlan->port_max),
  1593. };
  1594. if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni))
  1595. goto nla_put_failure;
  1596. if (dst->remote_ip && nla_put_be32(skb, IFLA_VXLAN_GROUP, dst->remote_ip))
  1597. goto nla_put_failure;
  1598. if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
  1599. goto nla_put_failure;
  1600. if (vxlan->saddr && nla_put_be32(skb, IFLA_VXLAN_LOCAL, vxlan->saddr))
  1601. goto nla_put_failure;
  1602. if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
  1603. nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
  1604. nla_put_u8(skb, IFLA_VXLAN_LEARNING,
  1605. !!(vxlan->flags & VXLAN_F_LEARN)) ||
  1606. nla_put_u8(skb, IFLA_VXLAN_PROXY,
  1607. !!(vxlan->flags & VXLAN_F_PROXY)) ||
  1608. nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
  1609. nla_put_u8(skb, IFLA_VXLAN_L2MISS,
  1610. !!(vxlan->flags & VXLAN_F_L2MISS)) ||
  1611. nla_put_u8(skb, IFLA_VXLAN_L3MISS,
  1612. !!(vxlan->flags & VXLAN_F_L3MISS)) ||
  1613. nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
  1614. nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) ||
  1615. nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port))
  1616. goto nla_put_failure;
  1617. if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
  1618. goto nla_put_failure;
  1619. return 0;
  1620. nla_put_failure:
  1621. return -EMSGSIZE;
  1622. }
  1623. static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
  1624. .kind = "vxlan",
  1625. .maxtype = IFLA_VXLAN_MAX,
  1626. .policy = vxlan_policy,
  1627. .priv_size = sizeof(struct vxlan_dev),
  1628. .setup = vxlan_setup,
  1629. .validate = vxlan_validate,
  1630. .newlink = vxlan_newlink,
  1631. .dellink = vxlan_dellink,
  1632. .get_size = vxlan_get_size,
  1633. .fill_info = vxlan_fill_info,
  1634. };
  1635. static __net_init int vxlan_init_net(struct net *net)
  1636. {
  1637. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  1638. unsigned int h;
  1639. INIT_LIST_HEAD(&vn->vxlan_list);
  1640. spin_lock_init(&vn->sock_lock);
  1641. for (h = 0; h < PORT_HASH_SIZE; ++h)
  1642. INIT_HLIST_HEAD(&vn->sock_list[h]);
  1643. return 0;
  1644. }
  1645. static __net_exit void vxlan_exit_net(struct net *net)
  1646. {
  1647. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  1648. struct vxlan_dev *vxlan;
  1649. LIST_HEAD(list);
  1650. rtnl_lock();
  1651. list_for_each_entry(vxlan, &vn->vxlan_list, next)
  1652. unregister_netdevice_queue(vxlan->dev, &list);
  1653. unregister_netdevice_many(&list);
  1654. rtnl_unlock();
  1655. }
  1656. static struct pernet_operations vxlan_net_ops = {
  1657. .init = vxlan_init_net,
  1658. .exit = vxlan_exit_net,
  1659. .id = &vxlan_net_id,
  1660. .size = sizeof(struct vxlan_net),
  1661. };
  1662. static int __init vxlan_init_module(void)
  1663. {
  1664. int rc;
  1665. vxlan_wq = alloc_workqueue("vxlan", 0, 0);
  1666. if (!vxlan_wq)
  1667. return -ENOMEM;
  1668. get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
  1669. rc = register_pernet_device(&vxlan_net_ops);
  1670. if (rc)
  1671. goto out1;
  1672. rc = rtnl_link_register(&vxlan_link_ops);
  1673. if (rc)
  1674. goto out2;
  1675. return 0;
  1676. out2:
  1677. unregister_pernet_device(&vxlan_net_ops);
  1678. out1:
  1679. destroy_workqueue(vxlan_wq);
  1680. return rc;
  1681. }
  1682. late_initcall(vxlan_init_module);
  1683. static void __exit vxlan_cleanup_module(void)
  1684. {
  1685. rtnl_link_unregister(&vxlan_link_ops);
  1686. destroy_workqueue(vxlan_wq);
  1687. unregister_pernet_device(&vxlan_net_ops);
  1688. rcu_barrier();
  1689. }
  1690. module_exit(vxlan_cleanup_module);
  1691. MODULE_LICENSE("GPL");
  1692. MODULE_VERSION(VXLAN_VERSION);
  1693. MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
  1694. MODULE_ALIAS_RTNL_LINK("vxlan");