af_mpls.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559
  1. #include <linux/types.h>
  2. #include <linux/skbuff.h>
  3. #include <linux/socket.h>
  4. #include <linux/sysctl.h>
  5. #include <linux/net.h>
  6. #include <linux/module.h>
  7. #include <linux/if_arp.h>
  8. #include <linux/ipv6.h>
  9. #include <linux/mpls.h>
  10. #include <linux/netconf.h>
  11. #include <linux/nospec.h>
  12. #include <linux/vmalloc.h>
  13. #include <linux/percpu.h>
  14. #include <net/ip.h>
  15. #include <net/dst.h>
  16. #include <net/sock.h>
  17. #include <net/arp.h>
  18. #include <net/ip_fib.h>
  19. #include <net/netevent.h>
  20. #include <net/ip_tunnels.h>
  21. #include <net/netns/generic.h>
  22. #if IS_ENABLED(CONFIG_IPV6)
  23. #include <net/ipv6.h>
  24. #endif
  25. #include <net/addrconf.h>
  26. #include <net/nexthop.h>
  27. #include "internal.h"
  28. /* max memory we will use for mpls_route */
  29. #define MAX_MPLS_ROUTE_MEM 4096
  30. /* Maximum number of labels to look ahead at when selecting a path of
  31. * a multipath route
  32. */
  33. #define MAX_MP_SELECT_LABELS 4
  34. #define MPLS_NEIGH_TABLE_UNSPEC (NEIGH_LINK_TABLE + 1)
  35. static int zero = 0;
  36. static int one = 1;
  37. static int label_limit = (1 << 20) - 1;
  38. static int ttl_max = 255;
  39. #if IS_ENABLED(CONFIG_NET_IP_TUNNEL)
  40. static size_t ipgre_mpls_encap_hlen(struct ip_tunnel_encap *e)
  41. {
  42. return sizeof(struct mpls_shim_hdr);
  43. }
  44. static const struct ip_tunnel_encap_ops mpls_iptun_ops = {
  45. .encap_hlen = ipgre_mpls_encap_hlen,
  46. };
  47. static int ipgre_tunnel_encap_add_mpls_ops(void)
  48. {
  49. return ip_tunnel_encap_add_ops(&mpls_iptun_ops, TUNNEL_ENCAP_MPLS);
  50. }
  51. static void ipgre_tunnel_encap_del_mpls_ops(void)
  52. {
  53. ip_tunnel_encap_del_ops(&mpls_iptun_ops, TUNNEL_ENCAP_MPLS);
  54. }
  55. #else
  56. static int ipgre_tunnel_encap_add_mpls_ops(void)
  57. {
  58. return 0;
  59. }
  60. static void ipgre_tunnel_encap_del_mpls_ops(void)
  61. {
  62. }
  63. #endif
  64. static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
  65. struct nlmsghdr *nlh, struct net *net, u32 portid,
  66. unsigned int nlm_flags);
  67. static struct mpls_route *mpls_route_input_rcu(struct net *net, unsigned index)
  68. {
  69. struct mpls_route *rt = NULL;
  70. if (index < net->mpls.platform_labels) {
  71. struct mpls_route __rcu **platform_label =
  72. rcu_dereference(net->mpls.platform_label);
  73. rt = rcu_dereference(platform_label[index]);
  74. }
  75. return rt;
  76. }
  77. bool mpls_output_possible(const struct net_device *dev)
  78. {
  79. return dev && (dev->flags & IFF_UP) && netif_carrier_ok(dev);
  80. }
  81. EXPORT_SYMBOL_GPL(mpls_output_possible);
  82. static u8 *__mpls_nh_via(struct mpls_route *rt, struct mpls_nh *nh)
  83. {
  84. return (u8 *)nh + rt->rt_via_offset;
  85. }
  86. static const u8 *mpls_nh_via(const struct mpls_route *rt,
  87. const struct mpls_nh *nh)
  88. {
  89. return __mpls_nh_via((struct mpls_route *)rt, (struct mpls_nh *)nh);
  90. }
  91. static unsigned int mpls_nh_header_size(const struct mpls_nh *nh)
  92. {
  93. /* The size of the layer 2.5 labels to be added for this route */
  94. return nh->nh_labels * sizeof(struct mpls_shim_hdr);
  95. }
  96. unsigned int mpls_dev_mtu(const struct net_device *dev)
  97. {
  98. /* The amount of data the layer 2 frame can hold */
  99. return dev->mtu;
  100. }
  101. EXPORT_SYMBOL_GPL(mpls_dev_mtu);
  102. bool mpls_pkt_too_big(const struct sk_buff *skb, unsigned int mtu)
  103. {
  104. if (skb->len <= mtu)
  105. return false;
  106. if (skb_is_gso(skb) && skb_gso_validate_network_len(skb, mtu))
  107. return false;
  108. return true;
  109. }
  110. EXPORT_SYMBOL_GPL(mpls_pkt_too_big);
  111. void mpls_stats_inc_outucastpkts(struct net_device *dev,
  112. const struct sk_buff *skb)
  113. {
  114. struct mpls_dev *mdev;
  115. if (skb->protocol == htons(ETH_P_MPLS_UC)) {
  116. mdev = mpls_dev_get(dev);
  117. if (mdev)
  118. MPLS_INC_STATS_LEN(mdev, skb->len,
  119. tx_packets,
  120. tx_bytes);
  121. } else if (skb->protocol == htons(ETH_P_IP)) {
  122. IP_UPD_PO_STATS(dev_net(dev), IPSTATS_MIB_OUT, skb->len);
  123. #if IS_ENABLED(CONFIG_IPV6)
  124. } else if (skb->protocol == htons(ETH_P_IPV6)) {
  125. struct inet6_dev *in6dev = __in6_dev_get(dev);
  126. if (in6dev)
  127. IP6_UPD_PO_STATS(dev_net(dev), in6dev,
  128. IPSTATS_MIB_OUT, skb->len);
  129. #endif
  130. }
  131. }
  132. EXPORT_SYMBOL_GPL(mpls_stats_inc_outucastpkts);
  133. static u32 mpls_multipath_hash(struct mpls_route *rt, struct sk_buff *skb)
  134. {
  135. struct mpls_entry_decoded dec;
  136. unsigned int mpls_hdr_len = 0;
  137. struct mpls_shim_hdr *hdr;
  138. bool eli_seen = false;
  139. int label_index;
  140. u32 hash = 0;
  141. for (label_index = 0; label_index < MAX_MP_SELECT_LABELS;
  142. label_index++) {
  143. mpls_hdr_len += sizeof(*hdr);
  144. if (!pskb_may_pull(skb, mpls_hdr_len))
  145. break;
  146. /* Read and decode the current label */
  147. hdr = mpls_hdr(skb) + label_index;
  148. dec = mpls_entry_decode(hdr);
  149. /* RFC6790 - reserved labels MUST NOT be used as keys
  150. * for the load-balancing function
  151. */
  152. if (likely(dec.label >= MPLS_LABEL_FIRST_UNRESERVED)) {
  153. hash = jhash_1word(dec.label, hash);
  154. /* The entropy label follows the entropy label
  155. * indicator, so this means that the entropy
  156. * label was just added to the hash - no need to
  157. * go any deeper either in the label stack or in the
  158. * payload
  159. */
  160. if (eli_seen)
  161. break;
  162. } else if (dec.label == MPLS_LABEL_ENTROPY) {
  163. eli_seen = true;
  164. }
  165. if (!dec.bos)
  166. continue;
  167. /* found bottom label; does skb have room for a header? */
  168. if (pskb_may_pull(skb, mpls_hdr_len + sizeof(struct iphdr))) {
  169. const struct iphdr *v4hdr;
  170. v4hdr = (const struct iphdr *)(hdr + 1);
  171. if (v4hdr->version == 4) {
  172. hash = jhash_3words(ntohl(v4hdr->saddr),
  173. ntohl(v4hdr->daddr),
  174. v4hdr->protocol, hash);
  175. } else if (v4hdr->version == 6 &&
  176. pskb_may_pull(skb, mpls_hdr_len +
  177. sizeof(struct ipv6hdr))) {
  178. const struct ipv6hdr *v6hdr;
  179. v6hdr = (const struct ipv6hdr *)(hdr + 1);
  180. hash = __ipv6_addr_jhash(&v6hdr->saddr, hash);
  181. hash = __ipv6_addr_jhash(&v6hdr->daddr, hash);
  182. hash = jhash_1word(v6hdr->nexthdr, hash);
  183. }
  184. }
  185. break;
  186. }
  187. return hash;
  188. }
  189. static struct mpls_nh *mpls_get_nexthop(struct mpls_route *rt, u8 index)
  190. {
  191. return (struct mpls_nh *)((u8 *)rt->rt_nh + index * rt->rt_nh_size);
  192. }
  193. /* number of alive nexthops (rt->rt_nhn_alive) and the flags for
  194. * a next hop (nh->nh_flags) are modified by netdev event handlers.
  195. * Since those fields can change at any moment, use READ_ONCE to
  196. * access both.
  197. */
  198. static struct mpls_nh *mpls_select_multipath(struct mpls_route *rt,
  199. struct sk_buff *skb)
  200. {
  201. u32 hash = 0;
  202. int nh_index = 0;
  203. int n = 0;
  204. u8 alive;
  205. /* No need to look further into packet if there's only
  206. * one path
  207. */
  208. if (rt->rt_nhn == 1)
  209. return rt->rt_nh;
  210. alive = READ_ONCE(rt->rt_nhn_alive);
  211. if (alive == 0)
  212. return NULL;
  213. hash = mpls_multipath_hash(rt, skb);
  214. nh_index = hash % alive;
  215. if (alive == rt->rt_nhn)
  216. goto out;
  217. for_nexthops(rt) {
  218. unsigned int nh_flags = READ_ONCE(nh->nh_flags);
  219. if (nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
  220. continue;
  221. if (n == nh_index)
  222. return nh;
  223. n++;
  224. } endfor_nexthops(rt);
  225. out:
  226. return mpls_get_nexthop(rt, nh_index);
  227. }
  228. static bool mpls_egress(struct net *net, struct mpls_route *rt,
  229. struct sk_buff *skb, struct mpls_entry_decoded dec)
  230. {
  231. enum mpls_payload_type payload_type;
  232. bool success = false;
  233. /* The IPv4 code below accesses through the IPv4 header
  234. * checksum, which is 12 bytes into the packet.
  235. * The IPv6 code below accesses through the IPv6 hop limit
  236. * which is 8 bytes into the packet.
  237. *
  238. * For all supported cases there should always be at least 12
  239. * bytes of packet data present. The IPv4 header is 20 bytes
  240. * without options and the IPv6 header is always 40 bytes
  241. * long.
  242. */
  243. if (!pskb_may_pull(skb, 12))
  244. return false;
  245. payload_type = rt->rt_payload_type;
  246. if (payload_type == MPT_UNSPEC)
  247. payload_type = ip_hdr(skb)->version;
  248. switch (payload_type) {
  249. case MPT_IPV4: {
  250. struct iphdr *hdr4 = ip_hdr(skb);
  251. u8 new_ttl;
  252. skb->protocol = htons(ETH_P_IP);
  253. /* If propagating TTL, take the decremented TTL from
  254. * the incoming MPLS header, otherwise decrement the
  255. * TTL, but only if not 0 to avoid underflow.
  256. */
  257. if (rt->rt_ttl_propagate == MPLS_TTL_PROP_ENABLED ||
  258. (rt->rt_ttl_propagate == MPLS_TTL_PROP_DEFAULT &&
  259. net->mpls.ip_ttl_propagate))
  260. new_ttl = dec.ttl;
  261. else
  262. new_ttl = hdr4->ttl ? hdr4->ttl - 1 : 0;
  263. csum_replace2(&hdr4->check,
  264. htons(hdr4->ttl << 8),
  265. htons(new_ttl << 8));
  266. hdr4->ttl = new_ttl;
  267. success = true;
  268. break;
  269. }
  270. case MPT_IPV6: {
  271. struct ipv6hdr *hdr6 = ipv6_hdr(skb);
  272. skb->protocol = htons(ETH_P_IPV6);
  273. /* If propagating TTL, take the decremented TTL from
  274. * the incoming MPLS header, otherwise decrement the
  275. * hop limit, but only if not 0 to avoid underflow.
  276. */
  277. if (rt->rt_ttl_propagate == MPLS_TTL_PROP_ENABLED ||
  278. (rt->rt_ttl_propagate == MPLS_TTL_PROP_DEFAULT &&
  279. net->mpls.ip_ttl_propagate))
  280. hdr6->hop_limit = dec.ttl;
  281. else if (hdr6->hop_limit)
  282. hdr6->hop_limit = hdr6->hop_limit - 1;
  283. success = true;
  284. break;
  285. }
  286. case MPT_UNSPEC:
  287. /* Should have decided which protocol it is by now */
  288. break;
  289. }
  290. return success;
  291. }
  292. static int mpls_forward(struct sk_buff *skb, struct net_device *dev,
  293. struct packet_type *pt, struct net_device *orig_dev)
  294. {
  295. struct net *net = dev_net(dev);
  296. struct mpls_shim_hdr *hdr;
  297. struct mpls_route *rt;
  298. struct mpls_nh *nh;
  299. struct mpls_entry_decoded dec;
  300. struct net_device *out_dev;
  301. struct mpls_dev *out_mdev;
  302. struct mpls_dev *mdev;
  303. unsigned int hh_len;
  304. unsigned int new_header_size;
  305. unsigned int mtu;
  306. int err;
  307. /* Careful this entire function runs inside of an rcu critical section */
  308. mdev = mpls_dev_get(dev);
  309. if (!mdev)
  310. goto drop;
  311. MPLS_INC_STATS_LEN(mdev, skb->len, rx_packets,
  312. rx_bytes);
  313. if (!mdev->input_enabled) {
  314. MPLS_INC_STATS(mdev, rx_dropped);
  315. goto drop;
  316. }
  317. if (skb->pkt_type != PACKET_HOST)
  318. goto err;
  319. if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
  320. goto err;
  321. if (!pskb_may_pull(skb, sizeof(*hdr)))
  322. goto err;
  323. /* Read and decode the label */
  324. hdr = mpls_hdr(skb);
  325. dec = mpls_entry_decode(hdr);
  326. rt = mpls_route_input_rcu(net, dec.label);
  327. if (!rt) {
  328. MPLS_INC_STATS(mdev, rx_noroute);
  329. goto drop;
  330. }
  331. nh = mpls_select_multipath(rt, skb);
  332. if (!nh)
  333. goto err;
  334. /* Pop the label */
  335. skb_pull(skb, sizeof(*hdr));
  336. skb_reset_network_header(skb);
  337. skb_orphan(skb);
  338. if (skb_warn_if_lro(skb))
  339. goto err;
  340. skb_forward_csum(skb);
  341. /* Verify ttl is valid */
  342. if (dec.ttl <= 1)
  343. goto err;
  344. dec.ttl -= 1;
  345. /* Find the output device */
  346. out_dev = rcu_dereference(nh->nh_dev);
  347. if (!mpls_output_possible(out_dev))
  348. goto tx_err;
  349. /* Verify the destination can hold the packet */
  350. new_header_size = mpls_nh_header_size(nh);
  351. mtu = mpls_dev_mtu(out_dev);
  352. if (mpls_pkt_too_big(skb, mtu - new_header_size))
  353. goto tx_err;
  354. hh_len = LL_RESERVED_SPACE(out_dev);
  355. if (!out_dev->header_ops)
  356. hh_len = 0;
  357. /* Ensure there is enough space for the headers in the skb */
  358. if (skb_cow(skb, hh_len + new_header_size))
  359. goto tx_err;
  360. skb->dev = out_dev;
  361. skb->protocol = htons(ETH_P_MPLS_UC);
  362. if (unlikely(!new_header_size && dec.bos)) {
  363. /* Penultimate hop popping */
  364. if (!mpls_egress(dev_net(out_dev), rt, skb, dec))
  365. goto err;
  366. } else {
  367. bool bos;
  368. int i;
  369. skb_push(skb, new_header_size);
  370. skb_reset_network_header(skb);
  371. /* Push the new labels */
  372. hdr = mpls_hdr(skb);
  373. bos = dec.bos;
  374. for (i = nh->nh_labels - 1; i >= 0; i--) {
  375. hdr[i] = mpls_entry_encode(nh->nh_label[i],
  376. dec.ttl, 0, bos);
  377. bos = false;
  378. }
  379. }
  380. mpls_stats_inc_outucastpkts(out_dev, skb);
  381. /* If via wasn't specified then send out using device address */
  382. if (nh->nh_via_table == MPLS_NEIGH_TABLE_UNSPEC)
  383. err = neigh_xmit(NEIGH_LINK_TABLE, out_dev,
  384. out_dev->dev_addr, skb);
  385. else
  386. err = neigh_xmit(nh->nh_via_table, out_dev,
  387. mpls_nh_via(rt, nh), skb);
  388. if (err)
  389. net_dbg_ratelimited("%s: packet transmission failed: %d\n",
  390. __func__, err);
  391. return 0;
  392. tx_err:
  393. out_mdev = out_dev ? mpls_dev_get(out_dev) : NULL;
  394. if (out_mdev)
  395. MPLS_INC_STATS(out_mdev, tx_errors);
  396. goto drop;
  397. err:
  398. MPLS_INC_STATS(mdev, rx_errors);
  399. drop:
  400. kfree_skb(skb);
  401. return NET_RX_DROP;
  402. }
  403. static struct packet_type mpls_packet_type __read_mostly = {
  404. .type = cpu_to_be16(ETH_P_MPLS_UC),
  405. .func = mpls_forward,
  406. };
  407. static const struct nla_policy rtm_mpls_policy[RTA_MAX+1] = {
  408. [RTA_DST] = { .type = NLA_U32 },
  409. [RTA_OIF] = { .type = NLA_U32 },
  410. [RTA_TTL_PROPAGATE] = { .type = NLA_U8 },
  411. };
  412. struct mpls_route_config {
  413. u32 rc_protocol;
  414. u32 rc_ifindex;
  415. u8 rc_via_table;
  416. u8 rc_via_alen;
  417. u8 rc_via[MAX_VIA_ALEN];
  418. u32 rc_label;
  419. u8 rc_ttl_propagate;
  420. u8 rc_output_labels;
  421. u32 rc_output_label[MAX_NEW_LABELS];
  422. u32 rc_nlflags;
  423. enum mpls_payload_type rc_payload_type;
  424. struct nl_info rc_nlinfo;
  425. struct rtnexthop *rc_mp;
  426. int rc_mp_len;
  427. };
  428. /* all nexthops within a route have the same size based on max
  429. * number of labels and max via length for a hop
  430. */
  431. static struct mpls_route *mpls_rt_alloc(u8 num_nh, u8 max_alen, u8 max_labels)
  432. {
  433. u8 nh_size = MPLS_NH_SIZE(max_labels, max_alen);
  434. struct mpls_route *rt;
  435. size_t size;
  436. size = sizeof(*rt) + num_nh * nh_size;
  437. if (size > MAX_MPLS_ROUTE_MEM)
  438. return ERR_PTR(-EINVAL);
  439. rt = kzalloc(size, GFP_KERNEL);
  440. if (!rt)
  441. return ERR_PTR(-ENOMEM);
  442. rt->rt_nhn = num_nh;
  443. rt->rt_nhn_alive = num_nh;
  444. rt->rt_nh_size = nh_size;
  445. rt->rt_via_offset = MPLS_NH_VIA_OFF(max_labels);
  446. return rt;
  447. }
  448. static void mpls_rt_free(struct mpls_route *rt)
  449. {
  450. if (rt)
  451. kfree_rcu(rt, rt_rcu);
  452. }
  453. static void mpls_notify_route(struct net *net, unsigned index,
  454. struct mpls_route *old, struct mpls_route *new,
  455. const struct nl_info *info)
  456. {
  457. struct nlmsghdr *nlh = info ? info->nlh : NULL;
  458. unsigned portid = info ? info->portid : 0;
  459. int event = new ? RTM_NEWROUTE : RTM_DELROUTE;
  460. struct mpls_route *rt = new ? new : old;
  461. unsigned nlm_flags = (old && new) ? NLM_F_REPLACE : 0;
  462. /* Ignore reserved labels for now */
  463. if (rt && (index >= MPLS_LABEL_FIRST_UNRESERVED))
  464. rtmsg_lfib(event, index, rt, nlh, net, portid, nlm_flags);
  465. }
  466. static void mpls_route_update(struct net *net, unsigned index,
  467. struct mpls_route *new,
  468. const struct nl_info *info)
  469. {
  470. struct mpls_route __rcu **platform_label;
  471. struct mpls_route *rt;
  472. ASSERT_RTNL();
  473. platform_label = rtnl_dereference(net->mpls.platform_label);
  474. rt = rtnl_dereference(platform_label[index]);
  475. rcu_assign_pointer(platform_label[index], new);
  476. mpls_notify_route(net, index, rt, new, info);
  477. /* If we removed a route free it now */
  478. mpls_rt_free(rt);
  479. }
  480. static unsigned find_free_label(struct net *net)
  481. {
  482. struct mpls_route __rcu **platform_label;
  483. size_t platform_labels;
  484. unsigned index;
  485. platform_label = rtnl_dereference(net->mpls.platform_label);
  486. platform_labels = net->mpls.platform_labels;
  487. for (index = MPLS_LABEL_FIRST_UNRESERVED; index < platform_labels;
  488. index++) {
  489. if (!rtnl_dereference(platform_label[index]))
  490. return index;
  491. }
  492. return LABEL_NOT_SPECIFIED;
  493. }
  494. #if IS_ENABLED(CONFIG_INET)
  495. static struct net_device *inet_fib_lookup_dev(struct net *net,
  496. const void *addr)
  497. {
  498. struct net_device *dev;
  499. struct rtable *rt;
  500. struct in_addr daddr;
  501. memcpy(&daddr, addr, sizeof(struct in_addr));
  502. rt = ip_route_output(net, daddr.s_addr, 0, 0, 0);
  503. if (IS_ERR(rt))
  504. return ERR_CAST(rt);
  505. dev = rt->dst.dev;
  506. dev_hold(dev);
  507. ip_rt_put(rt);
  508. return dev;
  509. }
  510. #else
  511. static struct net_device *inet_fib_lookup_dev(struct net *net,
  512. const void *addr)
  513. {
  514. return ERR_PTR(-EAFNOSUPPORT);
  515. }
  516. #endif
  517. #if IS_ENABLED(CONFIG_IPV6)
  518. static struct net_device *inet6_fib_lookup_dev(struct net *net,
  519. const void *addr)
  520. {
  521. struct net_device *dev;
  522. struct dst_entry *dst;
  523. struct flowi6 fl6;
  524. if (!ipv6_stub)
  525. return ERR_PTR(-EAFNOSUPPORT);
  526. memset(&fl6, 0, sizeof(fl6));
  527. memcpy(&fl6.daddr, addr, sizeof(struct in6_addr));
  528. dst = ipv6_stub->ipv6_dst_lookup_flow(net, NULL, &fl6, NULL);
  529. if (IS_ERR(dst))
  530. return ERR_CAST(dst);
  531. dev = dst->dev;
  532. dev_hold(dev);
  533. dst_release(dst);
  534. return dev;
  535. }
  536. #else
  537. static struct net_device *inet6_fib_lookup_dev(struct net *net,
  538. const void *addr)
  539. {
  540. return ERR_PTR(-EAFNOSUPPORT);
  541. }
  542. #endif
  543. static struct net_device *find_outdev(struct net *net,
  544. struct mpls_route *rt,
  545. struct mpls_nh *nh, int oif)
  546. {
  547. struct net_device *dev = NULL;
  548. if (!oif) {
  549. switch (nh->nh_via_table) {
  550. case NEIGH_ARP_TABLE:
  551. dev = inet_fib_lookup_dev(net, mpls_nh_via(rt, nh));
  552. break;
  553. case NEIGH_ND_TABLE:
  554. dev = inet6_fib_lookup_dev(net, mpls_nh_via(rt, nh));
  555. break;
  556. case NEIGH_LINK_TABLE:
  557. break;
  558. }
  559. } else {
  560. dev = dev_get_by_index(net, oif);
  561. }
  562. if (!dev)
  563. return ERR_PTR(-ENODEV);
  564. if (IS_ERR(dev))
  565. return dev;
  566. /* The caller is holding rtnl anyways, so release the dev reference */
  567. dev_put(dev);
  568. return dev;
  569. }
  570. static int mpls_nh_assign_dev(struct net *net, struct mpls_route *rt,
  571. struct mpls_nh *nh, int oif)
  572. {
  573. struct net_device *dev = NULL;
  574. int err = -ENODEV;
  575. dev = find_outdev(net, rt, nh, oif);
  576. if (IS_ERR(dev)) {
  577. err = PTR_ERR(dev);
  578. dev = NULL;
  579. goto errout;
  580. }
  581. /* Ensure this is a supported device */
  582. err = -EINVAL;
  583. if (!mpls_dev_get(dev))
  584. goto errout;
  585. if ((nh->nh_via_table == NEIGH_LINK_TABLE) &&
  586. (dev->addr_len != nh->nh_via_alen))
  587. goto errout;
  588. RCU_INIT_POINTER(nh->nh_dev, dev);
  589. if (!(dev->flags & IFF_UP)) {
  590. nh->nh_flags |= RTNH_F_DEAD;
  591. } else {
  592. unsigned int flags;
  593. flags = dev_get_flags(dev);
  594. if (!(flags & (IFF_RUNNING | IFF_LOWER_UP)))
  595. nh->nh_flags |= RTNH_F_LINKDOWN;
  596. }
  597. return 0;
  598. errout:
  599. return err;
  600. }
  601. static int nla_get_via(const struct nlattr *nla, u8 *via_alen, u8 *via_table,
  602. u8 via_addr[], struct netlink_ext_ack *extack)
  603. {
  604. struct rtvia *via = nla_data(nla);
  605. int err = -EINVAL;
  606. int alen;
  607. if (nla_len(nla) < offsetof(struct rtvia, rtvia_addr)) {
  608. NL_SET_ERR_MSG_ATTR(extack, nla,
  609. "Invalid attribute length for RTA_VIA");
  610. goto errout;
  611. }
  612. alen = nla_len(nla) -
  613. offsetof(struct rtvia, rtvia_addr);
  614. if (alen > MAX_VIA_ALEN) {
  615. NL_SET_ERR_MSG_ATTR(extack, nla,
  616. "Invalid address length for RTA_VIA");
  617. goto errout;
  618. }
  619. /* Validate the address family */
  620. switch (via->rtvia_family) {
  621. case AF_PACKET:
  622. *via_table = NEIGH_LINK_TABLE;
  623. break;
  624. case AF_INET:
  625. *via_table = NEIGH_ARP_TABLE;
  626. if (alen != 4)
  627. goto errout;
  628. break;
  629. case AF_INET6:
  630. *via_table = NEIGH_ND_TABLE;
  631. if (alen != 16)
  632. goto errout;
  633. break;
  634. default:
  635. /* Unsupported address family */
  636. goto errout;
  637. }
  638. memcpy(via_addr, via->rtvia_addr, alen);
  639. *via_alen = alen;
  640. err = 0;
  641. errout:
  642. return err;
  643. }
  644. static int mpls_nh_build_from_cfg(struct mpls_route_config *cfg,
  645. struct mpls_route *rt)
  646. {
  647. struct net *net = cfg->rc_nlinfo.nl_net;
  648. struct mpls_nh *nh = rt->rt_nh;
  649. int err;
  650. int i;
  651. if (!nh)
  652. return -ENOMEM;
  653. nh->nh_labels = cfg->rc_output_labels;
  654. for (i = 0; i < nh->nh_labels; i++)
  655. nh->nh_label[i] = cfg->rc_output_label[i];
  656. nh->nh_via_table = cfg->rc_via_table;
  657. memcpy(__mpls_nh_via(rt, nh), cfg->rc_via, cfg->rc_via_alen);
  658. nh->nh_via_alen = cfg->rc_via_alen;
  659. err = mpls_nh_assign_dev(net, rt, nh, cfg->rc_ifindex);
  660. if (err)
  661. goto errout;
  662. if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
  663. rt->rt_nhn_alive--;
  664. return 0;
  665. errout:
  666. return err;
  667. }
  668. static int mpls_nh_build(struct net *net, struct mpls_route *rt,
  669. struct mpls_nh *nh, int oif, struct nlattr *via,
  670. struct nlattr *newdst, u8 max_labels,
  671. struct netlink_ext_ack *extack)
  672. {
  673. int err = -ENOMEM;
  674. if (!nh)
  675. goto errout;
  676. if (newdst) {
  677. err = nla_get_labels(newdst, max_labels, &nh->nh_labels,
  678. nh->nh_label, extack);
  679. if (err)
  680. goto errout;
  681. }
  682. if (via) {
  683. err = nla_get_via(via, &nh->nh_via_alen, &nh->nh_via_table,
  684. __mpls_nh_via(rt, nh), extack);
  685. if (err)
  686. goto errout;
  687. } else {
  688. nh->nh_via_table = MPLS_NEIGH_TABLE_UNSPEC;
  689. }
  690. err = mpls_nh_assign_dev(net, rt, nh, oif);
  691. if (err)
  692. goto errout;
  693. return 0;
  694. errout:
  695. return err;
  696. }
  697. static u8 mpls_count_nexthops(struct rtnexthop *rtnh, int len,
  698. u8 cfg_via_alen, u8 *max_via_alen,
  699. u8 *max_labels)
  700. {
  701. int remaining = len;
  702. u8 nhs = 0;
  703. *max_via_alen = 0;
  704. *max_labels = 0;
  705. while (rtnh_ok(rtnh, remaining)) {
  706. struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
  707. int attrlen;
  708. u8 n_labels = 0;
  709. attrlen = rtnh_attrlen(rtnh);
  710. nla = nla_find(attrs, attrlen, RTA_VIA);
  711. if (nla && nla_len(nla) >=
  712. offsetof(struct rtvia, rtvia_addr)) {
  713. int via_alen = nla_len(nla) -
  714. offsetof(struct rtvia, rtvia_addr);
  715. if (via_alen <= MAX_VIA_ALEN)
  716. *max_via_alen = max_t(u16, *max_via_alen,
  717. via_alen);
  718. }
  719. nla = nla_find(attrs, attrlen, RTA_NEWDST);
  720. if (nla &&
  721. nla_get_labels(nla, MAX_NEW_LABELS, &n_labels,
  722. NULL, NULL) != 0)
  723. return 0;
  724. *max_labels = max_t(u8, *max_labels, n_labels);
  725. /* number of nexthops is tracked by a u8.
  726. * Check for overflow.
  727. */
  728. if (nhs == 255)
  729. return 0;
  730. nhs++;
  731. rtnh = rtnh_next(rtnh, &remaining);
  732. }
  733. /* leftover implies invalid nexthop configuration, discard it */
  734. return remaining > 0 ? 0 : nhs;
  735. }
  736. static int mpls_nh_build_multi(struct mpls_route_config *cfg,
  737. struct mpls_route *rt, u8 max_labels,
  738. struct netlink_ext_ack *extack)
  739. {
  740. struct rtnexthop *rtnh = cfg->rc_mp;
  741. struct nlattr *nla_via, *nla_newdst;
  742. int remaining = cfg->rc_mp_len;
  743. int err = 0;
  744. u8 nhs = 0;
  745. change_nexthops(rt) {
  746. int attrlen;
  747. nla_via = NULL;
  748. nla_newdst = NULL;
  749. err = -EINVAL;
  750. if (!rtnh_ok(rtnh, remaining))
  751. goto errout;
  752. /* neither weighted multipath nor any flags
  753. * are supported
  754. */
  755. if (rtnh->rtnh_hops || rtnh->rtnh_flags)
  756. goto errout;
  757. attrlen = rtnh_attrlen(rtnh);
  758. if (attrlen > 0) {
  759. struct nlattr *attrs = rtnh_attrs(rtnh);
  760. nla_via = nla_find(attrs, attrlen, RTA_VIA);
  761. nla_newdst = nla_find(attrs, attrlen, RTA_NEWDST);
  762. }
  763. err = mpls_nh_build(cfg->rc_nlinfo.nl_net, rt, nh,
  764. rtnh->rtnh_ifindex, nla_via, nla_newdst,
  765. max_labels, extack);
  766. if (err)
  767. goto errout;
  768. if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
  769. rt->rt_nhn_alive--;
  770. rtnh = rtnh_next(rtnh, &remaining);
  771. nhs++;
  772. } endfor_nexthops(rt);
  773. rt->rt_nhn = nhs;
  774. return 0;
  775. errout:
  776. return err;
  777. }
  778. static bool mpls_label_ok(struct net *net, unsigned int *index,
  779. struct netlink_ext_ack *extack)
  780. {
  781. bool is_ok = true;
  782. /* Reserved labels may not be set */
  783. if (*index < MPLS_LABEL_FIRST_UNRESERVED) {
  784. NL_SET_ERR_MSG(extack,
  785. "Invalid label - must be MPLS_LABEL_FIRST_UNRESERVED or higher");
  786. is_ok = false;
  787. }
  788. /* The full 20 bit range may not be supported. */
  789. if (is_ok && *index >= net->mpls.platform_labels) {
  790. NL_SET_ERR_MSG(extack,
  791. "Label >= configured maximum in platform_labels");
  792. is_ok = false;
  793. }
  794. *index = array_index_nospec(*index, net->mpls.platform_labels);
  795. return is_ok;
  796. }
  797. static int mpls_route_add(struct mpls_route_config *cfg,
  798. struct netlink_ext_ack *extack)
  799. {
  800. struct mpls_route __rcu **platform_label;
  801. struct net *net = cfg->rc_nlinfo.nl_net;
  802. struct mpls_route *rt, *old;
  803. int err = -EINVAL;
  804. u8 max_via_alen;
  805. unsigned index;
  806. u8 max_labels;
  807. u8 nhs;
  808. index = cfg->rc_label;
  809. /* If a label was not specified during insert pick one */
  810. if ((index == LABEL_NOT_SPECIFIED) &&
  811. (cfg->rc_nlflags & NLM_F_CREATE)) {
  812. index = find_free_label(net);
  813. }
  814. if (!mpls_label_ok(net, &index, extack))
  815. goto errout;
  816. /* Append makes no sense with mpls */
  817. err = -EOPNOTSUPP;
  818. if (cfg->rc_nlflags & NLM_F_APPEND) {
  819. NL_SET_ERR_MSG(extack, "MPLS does not support route append");
  820. goto errout;
  821. }
  822. err = -EEXIST;
  823. platform_label = rtnl_dereference(net->mpls.platform_label);
  824. old = rtnl_dereference(platform_label[index]);
  825. if ((cfg->rc_nlflags & NLM_F_EXCL) && old)
  826. goto errout;
  827. err = -EEXIST;
  828. if (!(cfg->rc_nlflags & NLM_F_REPLACE) && old)
  829. goto errout;
  830. err = -ENOENT;
  831. if (!(cfg->rc_nlflags & NLM_F_CREATE) && !old)
  832. goto errout;
  833. err = -EINVAL;
  834. if (cfg->rc_mp) {
  835. nhs = mpls_count_nexthops(cfg->rc_mp, cfg->rc_mp_len,
  836. cfg->rc_via_alen, &max_via_alen,
  837. &max_labels);
  838. } else {
  839. max_via_alen = cfg->rc_via_alen;
  840. max_labels = cfg->rc_output_labels;
  841. nhs = 1;
  842. }
  843. if (nhs == 0) {
  844. NL_SET_ERR_MSG(extack, "Route does not contain a nexthop");
  845. goto errout;
  846. }
  847. err = -ENOMEM;
  848. rt = mpls_rt_alloc(nhs, max_via_alen, max_labels);
  849. if (IS_ERR(rt)) {
  850. err = PTR_ERR(rt);
  851. goto errout;
  852. }
  853. rt->rt_protocol = cfg->rc_protocol;
  854. rt->rt_payload_type = cfg->rc_payload_type;
  855. rt->rt_ttl_propagate = cfg->rc_ttl_propagate;
  856. if (cfg->rc_mp)
  857. err = mpls_nh_build_multi(cfg, rt, max_labels, extack);
  858. else
  859. err = mpls_nh_build_from_cfg(cfg, rt);
  860. if (err)
  861. goto freert;
  862. mpls_route_update(net, index, rt, &cfg->rc_nlinfo);
  863. return 0;
  864. freert:
  865. mpls_rt_free(rt);
  866. errout:
  867. return err;
  868. }
  869. static int mpls_route_del(struct mpls_route_config *cfg,
  870. struct netlink_ext_ack *extack)
  871. {
  872. struct net *net = cfg->rc_nlinfo.nl_net;
  873. unsigned index;
  874. int err = -EINVAL;
  875. index = cfg->rc_label;
  876. if (!mpls_label_ok(net, &index, extack))
  877. goto errout;
  878. mpls_route_update(net, index, NULL, &cfg->rc_nlinfo);
  879. err = 0;
  880. errout:
  881. return err;
  882. }
  883. static void mpls_get_stats(struct mpls_dev *mdev,
  884. struct mpls_link_stats *stats)
  885. {
  886. struct mpls_pcpu_stats *p;
  887. int i;
  888. memset(stats, 0, sizeof(*stats));
  889. for_each_possible_cpu(i) {
  890. struct mpls_link_stats local;
  891. unsigned int start;
  892. p = per_cpu_ptr(mdev->stats, i);
  893. do {
  894. start = u64_stats_fetch_begin(&p->syncp);
  895. local = p->stats;
  896. } while (u64_stats_fetch_retry(&p->syncp, start));
  897. stats->rx_packets += local.rx_packets;
  898. stats->rx_bytes += local.rx_bytes;
  899. stats->tx_packets += local.tx_packets;
  900. stats->tx_bytes += local.tx_bytes;
  901. stats->rx_errors += local.rx_errors;
  902. stats->tx_errors += local.tx_errors;
  903. stats->rx_dropped += local.rx_dropped;
  904. stats->tx_dropped += local.tx_dropped;
  905. stats->rx_noroute += local.rx_noroute;
  906. }
  907. }
  908. static int mpls_fill_stats_af(struct sk_buff *skb,
  909. const struct net_device *dev)
  910. {
  911. struct mpls_link_stats *stats;
  912. struct mpls_dev *mdev;
  913. struct nlattr *nla;
  914. mdev = mpls_dev_get(dev);
  915. if (!mdev)
  916. return -ENODATA;
  917. nla = nla_reserve_64bit(skb, MPLS_STATS_LINK,
  918. sizeof(struct mpls_link_stats),
  919. MPLS_STATS_UNSPEC);
  920. if (!nla)
  921. return -EMSGSIZE;
  922. stats = nla_data(nla);
  923. mpls_get_stats(mdev, stats);
  924. return 0;
  925. }
  926. static size_t mpls_get_stats_af_size(const struct net_device *dev)
  927. {
  928. struct mpls_dev *mdev;
  929. mdev = mpls_dev_get(dev);
  930. if (!mdev)
  931. return 0;
  932. return nla_total_size_64bit(sizeof(struct mpls_link_stats));
  933. }
  934. static int mpls_netconf_fill_devconf(struct sk_buff *skb, struct mpls_dev *mdev,
  935. u32 portid, u32 seq, int event,
  936. unsigned int flags, int type)
  937. {
  938. struct nlmsghdr *nlh;
  939. struct netconfmsg *ncm;
  940. bool all = false;
  941. nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
  942. flags);
  943. if (!nlh)
  944. return -EMSGSIZE;
  945. if (type == NETCONFA_ALL)
  946. all = true;
  947. ncm = nlmsg_data(nlh);
  948. ncm->ncm_family = AF_MPLS;
  949. if (nla_put_s32(skb, NETCONFA_IFINDEX, mdev->dev->ifindex) < 0)
  950. goto nla_put_failure;
  951. if ((all || type == NETCONFA_INPUT) &&
  952. nla_put_s32(skb, NETCONFA_INPUT,
  953. mdev->input_enabled) < 0)
  954. goto nla_put_failure;
  955. nlmsg_end(skb, nlh);
  956. return 0;
  957. nla_put_failure:
  958. nlmsg_cancel(skb, nlh);
  959. return -EMSGSIZE;
  960. }
  961. static int mpls_netconf_msgsize_devconf(int type)
  962. {
  963. int size = NLMSG_ALIGN(sizeof(struct netconfmsg))
  964. + nla_total_size(4); /* NETCONFA_IFINDEX */
  965. bool all = false;
  966. if (type == NETCONFA_ALL)
  967. all = true;
  968. if (all || type == NETCONFA_INPUT)
  969. size += nla_total_size(4);
  970. return size;
  971. }
  972. static void mpls_netconf_notify_devconf(struct net *net, int event,
  973. int type, struct mpls_dev *mdev)
  974. {
  975. struct sk_buff *skb;
  976. int err = -ENOBUFS;
  977. skb = nlmsg_new(mpls_netconf_msgsize_devconf(type), GFP_KERNEL);
  978. if (!skb)
  979. goto errout;
  980. err = mpls_netconf_fill_devconf(skb, mdev, 0, 0, event, 0, type);
  981. if (err < 0) {
  982. /* -EMSGSIZE implies BUG in mpls_netconf_msgsize_devconf() */
  983. WARN_ON(err == -EMSGSIZE);
  984. kfree_skb(skb);
  985. goto errout;
  986. }
  987. rtnl_notify(skb, net, 0, RTNLGRP_MPLS_NETCONF, NULL, GFP_KERNEL);
  988. return;
  989. errout:
  990. if (err < 0)
  991. rtnl_set_sk_err(net, RTNLGRP_MPLS_NETCONF, err);
  992. }
  993. static const struct nla_policy devconf_mpls_policy[NETCONFA_MAX + 1] = {
  994. [NETCONFA_IFINDEX] = { .len = sizeof(int) },
  995. };
  996. static int mpls_netconf_get_devconf(struct sk_buff *in_skb,
  997. struct nlmsghdr *nlh,
  998. struct netlink_ext_ack *extack)
  999. {
  1000. struct net *net = sock_net(in_skb->sk);
  1001. struct nlattr *tb[NETCONFA_MAX + 1];
  1002. struct netconfmsg *ncm;
  1003. struct net_device *dev;
  1004. struct mpls_dev *mdev;
  1005. struct sk_buff *skb;
  1006. int ifindex;
  1007. int err;
  1008. err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX,
  1009. devconf_mpls_policy, NULL);
  1010. if (err < 0)
  1011. goto errout;
  1012. err = -EINVAL;
  1013. if (!tb[NETCONFA_IFINDEX])
  1014. goto errout;
  1015. ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
  1016. dev = __dev_get_by_index(net, ifindex);
  1017. if (!dev)
  1018. goto errout;
  1019. mdev = mpls_dev_get(dev);
  1020. if (!mdev)
  1021. goto errout;
  1022. err = -ENOBUFS;
  1023. skb = nlmsg_new(mpls_netconf_msgsize_devconf(NETCONFA_ALL), GFP_KERNEL);
  1024. if (!skb)
  1025. goto errout;
  1026. err = mpls_netconf_fill_devconf(skb, mdev,
  1027. NETLINK_CB(in_skb).portid,
  1028. nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
  1029. NETCONFA_ALL);
  1030. if (err < 0) {
  1031. /* -EMSGSIZE implies BUG in mpls_netconf_msgsize_devconf() */
  1032. WARN_ON(err == -EMSGSIZE);
  1033. kfree_skb(skb);
  1034. goto errout;
  1035. }
  1036. err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
  1037. errout:
  1038. return err;
  1039. }
  1040. static int mpls_netconf_dump_devconf(struct sk_buff *skb,
  1041. struct netlink_callback *cb)
  1042. {
  1043. struct net *net = sock_net(skb->sk);
  1044. struct hlist_head *head;
  1045. struct net_device *dev;
  1046. struct mpls_dev *mdev;
  1047. int idx, s_idx;
  1048. int h, s_h;
  1049. s_h = cb->args[0];
  1050. s_idx = idx = cb->args[1];
  1051. for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
  1052. idx = 0;
  1053. head = &net->dev_index_head[h];
  1054. rcu_read_lock();
  1055. cb->seq = net->dev_base_seq;
  1056. hlist_for_each_entry_rcu(dev, head, index_hlist) {
  1057. if (idx < s_idx)
  1058. goto cont;
  1059. mdev = mpls_dev_get(dev);
  1060. if (!mdev)
  1061. goto cont;
  1062. if (mpls_netconf_fill_devconf(skb, mdev,
  1063. NETLINK_CB(cb->skb).portid,
  1064. cb->nlh->nlmsg_seq,
  1065. RTM_NEWNETCONF,
  1066. NLM_F_MULTI,
  1067. NETCONFA_ALL) < 0) {
  1068. rcu_read_unlock();
  1069. goto done;
  1070. }
  1071. nl_dump_check_consistent(cb, nlmsg_hdr(skb));
  1072. cont:
  1073. idx++;
  1074. }
  1075. rcu_read_unlock();
  1076. }
  1077. done:
  1078. cb->args[0] = h;
  1079. cb->args[1] = idx;
  1080. return skb->len;
  1081. }
  1082. #define MPLS_PERDEV_SYSCTL_OFFSET(field) \
  1083. (&((struct mpls_dev *)0)->field)
  1084. static int mpls_conf_proc(struct ctl_table *ctl, int write,
  1085. void __user *buffer,
  1086. size_t *lenp, loff_t *ppos)
  1087. {
  1088. int oval = *(int *)ctl->data;
  1089. int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
  1090. if (write) {
  1091. struct mpls_dev *mdev = ctl->extra1;
  1092. int i = (int *)ctl->data - (int *)mdev;
  1093. struct net *net = ctl->extra2;
  1094. int val = *(int *)ctl->data;
  1095. if (i == offsetof(struct mpls_dev, input_enabled) &&
  1096. val != oval) {
  1097. mpls_netconf_notify_devconf(net, RTM_NEWNETCONF,
  1098. NETCONFA_INPUT, mdev);
  1099. }
  1100. }
  1101. return ret;
  1102. }
  1103. static const struct ctl_table mpls_dev_table[] = {
  1104. {
  1105. .procname = "input",
  1106. .maxlen = sizeof(int),
  1107. .mode = 0644,
  1108. .proc_handler = mpls_conf_proc,
  1109. .data = MPLS_PERDEV_SYSCTL_OFFSET(input_enabled),
  1110. },
  1111. { }
  1112. };
  1113. static int mpls_dev_sysctl_register(struct net_device *dev,
  1114. struct mpls_dev *mdev)
  1115. {
  1116. char path[sizeof("net/mpls/conf/") + IFNAMSIZ];
  1117. struct net *net = dev_net(dev);
  1118. struct ctl_table *table;
  1119. int i;
  1120. table = kmemdup(&mpls_dev_table, sizeof(mpls_dev_table), GFP_KERNEL);
  1121. if (!table)
  1122. goto out;
  1123. /* Table data contains only offsets relative to the base of
  1124. * the mdev at this point, so make them absolute.
  1125. */
  1126. for (i = 0; i < ARRAY_SIZE(mpls_dev_table); i++) {
  1127. table[i].data = (char *)mdev + (uintptr_t)table[i].data;
  1128. table[i].extra1 = mdev;
  1129. table[i].extra2 = net;
  1130. }
  1131. snprintf(path, sizeof(path), "net/mpls/conf/%s", dev->name);
  1132. mdev->sysctl = register_net_sysctl(net, path, table);
  1133. if (!mdev->sysctl)
  1134. goto free;
  1135. mpls_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_ALL, mdev);
  1136. return 0;
  1137. free:
  1138. kfree(table);
  1139. out:
  1140. return -ENOBUFS;
  1141. }
  1142. static void mpls_dev_sysctl_unregister(struct net_device *dev,
  1143. struct mpls_dev *mdev)
  1144. {
  1145. struct net *net = dev_net(dev);
  1146. struct ctl_table *table;
  1147. table = mdev->sysctl->ctl_table_arg;
  1148. unregister_net_sysctl_table(mdev->sysctl);
  1149. kfree(table);
  1150. mpls_netconf_notify_devconf(net, RTM_DELNETCONF, 0, mdev);
  1151. }
  1152. static struct mpls_dev *mpls_add_dev(struct net_device *dev)
  1153. {
  1154. struct mpls_dev *mdev;
  1155. int err = -ENOMEM;
  1156. int i;
  1157. ASSERT_RTNL();
  1158. mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
  1159. if (!mdev)
  1160. return ERR_PTR(err);
  1161. mdev->stats = alloc_percpu(struct mpls_pcpu_stats);
  1162. if (!mdev->stats)
  1163. goto free;
  1164. for_each_possible_cpu(i) {
  1165. struct mpls_pcpu_stats *mpls_stats;
  1166. mpls_stats = per_cpu_ptr(mdev->stats, i);
  1167. u64_stats_init(&mpls_stats->syncp);
  1168. }
  1169. mdev->dev = dev;
  1170. err = mpls_dev_sysctl_register(dev, mdev);
  1171. if (err)
  1172. goto free;
  1173. rcu_assign_pointer(dev->mpls_ptr, mdev);
  1174. return mdev;
  1175. free:
  1176. free_percpu(mdev->stats);
  1177. kfree(mdev);
  1178. return ERR_PTR(err);
  1179. }
  1180. static void mpls_dev_destroy_rcu(struct rcu_head *head)
  1181. {
  1182. struct mpls_dev *mdev = container_of(head, struct mpls_dev, rcu);
  1183. free_percpu(mdev->stats);
  1184. kfree(mdev);
  1185. }
  1186. static void mpls_ifdown(struct net_device *dev, int event)
  1187. {
  1188. struct mpls_route __rcu **platform_label;
  1189. struct net *net = dev_net(dev);
  1190. u8 alive, deleted;
  1191. unsigned index;
  1192. platform_label = rtnl_dereference(net->mpls.platform_label);
  1193. for (index = 0; index < net->mpls.platform_labels; index++) {
  1194. struct mpls_route *rt = rtnl_dereference(platform_label[index]);
  1195. if (!rt)
  1196. continue;
  1197. alive = 0;
  1198. deleted = 0;
  1199. change_nexthops(rt) {
  1200. unsigned int nh_flags = nh->nh_flags;
  1201. if (rtnl_dereference(nh->nh_dev) != dev)
  1202. goto next;
  1203. switch (event) {
  1204. case NETDEV_DOWN:
  1205. case NETDEV_UNREGISTER:
  1206. nh_flags |= RTNH_F_DEAD;
  1207. /* fall through */
  1208. case NETDEV_CHANGE:
  1209. nh_flags |= RTNH_F_LINKDOWN;
  1210. break;
  1211. }
  1212. if (event == NETDEV_UNREGISTER)
  1213. RCU_INIT_POINTER(nh->nh_dev, NULL);
  1214. if (nh->nh_flags != nh_flags)
  1215. WRITE_ONCE(nh->nh_flags, nh_flags);
  1216. next:
  1217. if (!(nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)))
  1218. alive++;
  1219. if (!rtnl_dereference(nh->nh_dev))
  1220. deleted++;
  1221. } endfor_nexthops(rt);
  1222. WRITE_ONCE(rt->rt_nhn_alive, alive);
  1223. /* if there are no more nexthops, delete the route */
  1224. if (event == NETDEV_UNREGISTER && deleted == rt->rt_nhn)
  1225. mpls_route_update(net, index, NULL, NULL);
  1226. }
  1227. }
  1228. static void mpls_ifup(struct net_device *dev, unsigned int flags)
  1229. {
  1230. struct mpls_route __rcu **platform_label;
  1231. struct net *net = dev_net(dev);
  1232. unsigned index;
  1233. u8 alive;
  1234. platform_label = rtnl_dereference(net->mpls.platform_label);
  1235. for (index = 0; index < net->mpls.platform_labels; index++) {
  1236. struct mpls_route *rt = rtnl_dereference(platform_label[index]);
  1237. if (!rt)
  1238. continue;
  1239. alive = 0;
  1240. change_nexthops(rt) {
  1241. unsigned int nh_flags = nh->nh_flags;
  1242. struct net_device *nh_dev =
  1243. rtnl_dereference(nh->nh_dev);
  1244. if (!(nh_flags & flags)) {
  1245. alive++;
  1246. continue;
  1247. }
  1248. if (nh_dev != dev)
  1249. continue;
  1250. alive++;
  1251. nh_flags &= ~flags;
  1252. WRITE_ONCE(nh->nh_flags, nh_flags);
  1253. } endfor_nexthops(rt);
  1254. WRITE_ONCE(rt->rt_nhn_alive, alive);
  1255. }
  1256. }
  1257. static int mpls_dev_notify(struct notifier_block *this, unsigned long event,
  1258. void *ptr)
  1259. {
  1260. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  1261. struct mpls_dev *mdev;
  1262. unsigned int flags;
  1263. if (event == NETDEV_REGISTER) {
  1264. /* For now just support Ethernet, IPGRE, IP6GRE, SIT and
  1265. * IPIP devices
  1266. */
  1267. if (dev->type == ARPHRD_ETHER ||
  1268. dev->type == ARPHRD_LOOPBACK ||
  1269. dev->type == ARPHRD_IPGRE ||
  1270. dev->type == ARPHRD_IP6GRE ||
  1271. dev->type == ARPHRD_SIT ||
  1272. dev->type == ARPHRD_TUNNEL) {
  1273. mdev = mpls_add_dev(dev);
  1274. if (IS_ERR(mdev))
  1275. return notifier_from_errno(PTR_ERR(mdev));
  1276. }
  1277. return NOTIFY_OK;
  1278. }
  1279. mdev = mpls_dev_get(dev);
  1280. if (!mdev)
  1281. return NOTIFY_OK;
  1282. switch (event) {
  1283. case NETDEV_DOWN:
  1284. mpls_ifdown(dev, event);
  1285. break;
  1286. case NETDEV_UP:
  1287. flags = dev_get_flags(dev);
  1288. if (flags & (IFF_RUNNING | IFF_LOWER_UP))
  1289. mpls_ifup(dev, RTNH_F_DEAD | RTNH_F_LINKDOWN);
  1290. else
  1291. mpls_ifup(dev, RTNH_F_DEAD);
  1292. break;
  1293. case NETDEV_CHANGE:
  1294. flags = dev_get_flags(dev);
  1295. if (flags & (IFF_RUNNING | IFF_LOWER_UP))
  1296. mpls_ifup(dev, RTNH_F_DEAD | RTNH_F_LINKDOWN);
  1297. else
  1298. mpls_ifdown(dev, event);
  1299. break;
  1300. case NETDEV_UNREGISTER:
  1301. mpls_ifdown(dev, event);
  1302. mdev = mpls_dev_get(dev);
  1303. if (mdev) {
  1304. mpls_dev_sysctl_unregister(dev, mdev);
  1305. RCU_INIT_POINTER(dev->mpls_ptr, NULL);
  1306. call_rcu(&mdev->rcu, mpls_dev_destroy_rcu);
  1307. }
  1308. break;
  1309. case NETDEV_CHANGENAME:
  1310. mdev = mpls_dev_get(dev);
  1311. if (mdev) {
  1312. int err;
  1313. mpls_dev_sysctl_unregister(dev, mdev);
  1314. err = mpls_dev_sysctl_register(dev, mdev);
  1315. if (err)
  1316. return notifier_from_errno(err);
  1317. }
  1318. break;
  1319. }
  1320. return NOTIFY_OK;
  1321. }
  1322. static struct notifier_block mpls_dev_notifier = {
  1323. .notifier_call = mpls_dev_notify,
  1324. };
  1325. static int nla_put_via(struct sk_buff *skb,
  1326. u8 table, const void *addr, int alen)
  1327. {
  1328. static const int table_to_family[NEIGH_NR_TABLES + 1] = {
  1329. AF_INET, AF_INET6, AF_DECnet, AF_PACKET,
  1330. };
  1331. struct nlattr *nla;
  1332. struct rtvia *via;
  1333. int family = AF_UNSPEC;
  1334. nla = nla_reserve(skb, RTA_VIA, alen + 2);
  1335. if (!nla)
  1336. return -EMSGSIZE;
  1337. if (table <= NEIGH_NR_TABLES)
  1338. family = table_to_family[table];
  1339. via = nla_data(nla);
  1340. via->rtvia_family = family;
  1341. memcpy(via->rtvia_addr, addr, alen);
  1342. return 0;
  1343. }
  1344. int nla_put_labels(struct sk_buff *skb, int attrtype,
  1345. u8 labels, const u32 label[])
  1346. {
  1347. struct nlattr *nla;
  1348. struct mpls_shim_hdr *nla_label;
  1349. bool bos;
  1350. int i;
  1351. nla = nla_reserve(skb, attrtype, labels*4);
  1352. if (!nla)
  1353. return -EMSGSIZE;
  1354. nla_label = nla_data(nla);
  1355. bos = true;
  1356. for (i = labels - 1; i >= 0; i--) {
  1357. nla_label[i] = mpls_entry_encode(label[i], 0, 0, bos);
  1358. bos = false;
  1359. }
  1360. return 0;
  1361. }
  1362. EXPORT_SYMBOL_GPL(nla_put_labels);
  1363. int nla_get_labels(const struct nlattr *nla, u8 max_labels, u8 *labels,
  1364. u32 label[], struct netlink_ext_ack *extack)
  1365. {
  1366. unsigned len = nla_len(nla);
  1367. struct mpls_shim_hdr *nla_label;
  1368. u8 nla_labels;
  1369. bool bos;
  1370. int i;
  1371. /* len needs to be an even multiple of 4 (the label size). Number
  1372. * of labels is a u8 so check for overflow.
  1373. */
  1374. if (len & 3 || len / 4 > 255) {
  1375. NL_SET_ERR_MSG_ATTR(extack, nla,
  1376. "Invalid length for labels attribute");
  1377. return -EINVAL;
  1378. }
  1379. /* Limit the number of new labels allowed */
  1380. nla_labels = len/4;
  1381. if (nla_labels > max_labels) {
  1382. NL_SET_ERR_MSG(extack, "Too many labels");
  1383. return -EINVAL;
  1384. }
  1385. /* when label == NULL, caller wants number of labels */
  1386. if (!label)
  1387. goto out;
  1388. nla_label = nla_data(nla);
  1389. bos = true;
  1390. for (i = nla_labels - 1; i >= 0; i--, bos = false) {
  1391. struct mpls_entry_decoded dec;
  1392. dec = mpls_entry_decode(nla_label + i);
  1393. /* Ensure the bottom of stack flag is properly set
  1394. * and ttl and tc are both clear.
  1395. */
  1396. if (dec.ttl) {
  1397. NL_SET_ERR_MSG_ATTR(extack, nla,
  1398. "TTL in label must be 0");
  1399. return -EINVAL;
  1400. }
  1401. if (dec.tc) {
  1402. NL_SET_ERR_MSG_ATTR(extack, nla,
  1403. "Traffic class in label must be 0");
  1404. return -EINVAL;
  1405. }
  1406. if (dec.bos != bos) {
  1407. NL_SET_BAD_ATTR(extack, nla);
  1408. if (bos) {
  1409. NL_SET_ERR_MSG(extack,
  1410. "BOS bit must be set in first label");
  1411. } else {
  1412. NL_SET_ERR_MSG(extack,
  1413. "BOS bit can only be set in first label");
  1414. }
  1415. return -EINVAL;
  1416. }
  1417. switch (dec.label) {
  1418. case MPLS_LABEL_IMPLNULL:
  1419. /* RFC3032: This is a label that an LSR may
  1420. * assign and distribute, but which never
  1421. * actually appears in the encapsulation.
  1422. */
  1423. NL_SET_ERR_MSG_ATTR(extack, nla,
  1424. "Implicit NULL Label (3) can not be used in encapsulation");
  1425. return -EINVAL;
  1426. }
  1427. label[i] = dec.label;
  1428. }
  1429. out:
  1430. *labels = nla_labels;
  1431. return 0;
  1432. }
  1433. EXPORT_SYMBOL_GPL(nla_get_labels);
  1434. static int rtm_to_route_config(struct sk_buff *skb,
  1435. struct nlmsghdr *nlh,
  1436. struct mpls_route_config *cfg,
  1437. struct netlink_ext_ack *extack)
  1438. {
  1439. struct rtmsg *rtm;
  1440. struct nlattr *tb[RTA_MAX+1];
  1441. int index;
  1442. int err;
  1443. err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_mpls_policy,
  1444. extack);
  1445. if (err < 0)
  1446. goto errout;
  1447. err = -EINVAL;
  1448. rtm = nlmsg_data(nlh);
  1449. if (rtm->rtm_family != AF_MPLS) {
  1450. NL_SET_ERR_MSG(extack, "Invalid address family in rtmsg");
  1451. goto errout;
  1452. }
  1453. if (rtm->rtm_dst_len != 20) {
  1454. NL_SET_ERR_MSG(extack, "rtm_dst_len must be 20 for MPLS");
  1455. goto errout;
  1456. }
  1457. if (rtm->rtm_src_len != 0) {
  1458. NL_SET_ERR_MSG(extack, "rtm_src_len must be 0 for MPLS");
  1459. goto errout;
  1460. }
  1461. if (rtm->rtm_tos != 0) {
  1462. NL_SET_ERR_MSG(extack, "rtm_tos must be 0 for MPLS");
  1463. goto errout;
  1464. }
  1465. if (rtm->rtm_table != RT_TABLE_MAIN) {
  1466. NL_SET_ERR_MSG(extack,
  1467. "MPLS only supports the main route table");
  1468. goto errout;
  1469. }
  1470. /* Any value is acceptable for rtm_protocol */
  1471. /* As mpls uses destination specific addresses
  1472. * (or source specific address in the case of multicast)
  1473. * all addresses have universal scope.
  1474. */
  1475. if (rtm->rtm_scope != RT_SCOPE_UNIVERSE) {
  1476. NL_SET_ERR_MSG(extack,
  1477. "Invalid route scope - MPLS only supports UNIVERSE");
  1478. goto errout;
  1479. }
  1480. if (rtm->rtm_type != RTN_UNICAST) {
  1481. NL_SET_ERR_MSG(extack,
  1482. "Invalid route type - MPLS only supports UNICAST");
  1483. goto errout;
  1484. }
  1485. if (rtm->rtm_flags != 0) {
  1486. NL_SET_ERR_MSG(extack, "rtm_flags must be 0 for MPLS");
  1487. goto errout;
  1488. }
  1489. cfg->rc_label = LABEL_NOT_SPECIFIED;
  1490. cfg->rc_protocol = rtm->rtm_protocol;
  1491. cfg->rc_via_table = MPLS_NEIGH_TABLE_UNSPEC;
  1492. cfg->rc_ttl_propagate = MPLS_TTL_PROP_DEFAULT;
  1493. cfg->rc_nlflags = nlh->nlmsg_flags;
  1494. cfg->rc_nlinfo.portid = NETLINK_CB(skb).portid;
  1495. cfg->rc_nlinfo.nlh = nlh;
  1496. cfg->rc_nlinfo.nl_net = sock_net(skb->sk);
  1497. for (index = 0; index <= RTA_MAX; index++) {
  1498. struct nlattr *nla = tb[index];
  1499. if (!nla)
  1500. continue;
  1501. switch (index) {
  1502. case RTA_OIF:
  1503. cfg->rc_ifindex = nla_get_u32(nla);
  1504. break;
  1505. case RTA_NEWDST:
  1506. if (nla_get_labels(nla, MAX_NEW_LABELS,
  1507. &cfg->rc_output_labels,
  1508. cfg->rc_output_label, extack))
  1509. goto errout;
  1510. break;
  1511. case RTA_DST:
  1512. {
  1513. u8 label_count;
  1514. if (nla_get_labels(nla, 1, &label_count,
  1515. &cfg->rc_label, extack))
  1516. goto errout;
  1517. if (!mpls_label_ok(cfg->rc_nlinfo.nl_net,
  1518. &cfg->rc_label, extack))
  1519. goto errout;
  1520. break;
  1521. }
  1522. case RTA_GATEWAY:
  1523. NL_SET_ERR_MSG(extack, "MPLS does not support RTA_GATEWAY attribute");
  1524. goto errout;
  1525. case RTA_VIA:
  1526. {
  1527. if (nla_get_via(nla, &cfg->rc_via_alen,
  1528. &cfg->rc_via_table, cfg->rc_via,
  1529. extack))
  1530. goto errout;
  1531. break;
  1532. }
  1533. case RTA_MULTIPATH:
  1534. {
  1535. cfg->rc_mp = nla_data(nla);
  1536. cfg->rc_mp_len = nla_len(nla);
  1537. break;
  1538. }
  1539. case RTA_TTL_PROPAGATE:
  1540. {
  1541. u8 ttl_propagate = nla_get_u8(nla);
  1542. if (ttl_propagate > 1) {
  1543. NL_SET_ERR_MSG_ATTR(extack, nla,
  1544. "RTA_TTL_PROPAGATE can only be 0 or 1");
  1545. goto errout;
  1546. }
  1547. cfg->rc_ttl_propagate = ttl_propagate ?
  1548. MPLS_TTL_PROP_ENABLED :
  1549. MPLS_TTL_PROP_DISABLED;
  1550. break;
  1551. }
  1552. default:
  1553. NL_SET_ERR_MSG_ATTR(extack, nla, "Unknown attribute");
  1554. /* Unsupported attribute */
  1555. goto errout;
  1556. }
  1557. }
  1558. err = 0;
  1559. errout:
  1560. return err;
  1561. }
  1562. static int mpls_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
  1563. struct netlink_ext_ack *extack)
  1564. {
  1565. struct mpls_route_config *cfg;
  1566. int err;
  1567. cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
  1568. if (!cfg)
  1569. return -ENOMEM;
  1570. err = rtm_to_route_config(skb, nlh, cfg, extack);
  1571. if (err < 0)
  1572. goto out;
  1573. err = mpls_route_del(cfg, extack);
  1574. out:
  1575. kfree(cfg);
  1576. return err;
  1577. }
  1578. static int mpls_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
  1579. struct netlink_ext_ack *extack)
  1580. {
  1581. struct mpls_route_config *cfg;
  1582. int err;
  1583. cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
  1584. if (!cfg)
  1585. return -ENOMEM;
  1586. err = rtm_to_route_config(skb, nlh, cfg, extack);
  1587. if (err < 0)
  1588. goto out;
  1589. err = mpls_route_add(cfg, extack);
  1590. out:
  1591. kfree(cfg);
  1592. return err;
  1593. }
  1594. static int mpls_dump_route(struct sk_buff *skb, u32 portid, u32 seq, int event,
  1595. u32 label, struct mpls_route *rt, int flags)
  1596. {
  1597. struct net_device *dev;
  1598. struct nlmsghdr *nlh;
  1599. struct rtmsg *rtm;
  1600. nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
  1601. if (nlh == NULL)
  1602. return -EMSGSIZE;
  1603. rtm = nlmsg_data(nlh);
  1604. rtm->rtm_family = AF_MPLS;
  1605. rtm->rtm_dst_len = 20;
  1606. rtm->rtm_src_len = 0;
  1607. rtm->rtm_tos = 0;
  1608. rtm->rtm_table = RT_TABLE_MAIN;
  1609. rtm->rtm_protocol = rt->rt_protocol;
  1610. rtm->rtm_scope = RT_SCOPE_UNIVERSE;
  1611. rtm->rtm_type = RTN_UNICAST;
  1612. rtm->rtm_flags = 0;
  1613. if (nla_put_labels(skb, RTA_DST, 1, &label))
  1614. goto nla_put_failure;
  1615. if (rt->rt_ttl_propagate != MPLS_TTL_PROP_DEFAULT) {
  1616. bool ttl_propagate =
  1617. rt->rt_ttl_propagate == MPLS_TTL_PROP_ENABLED;
  1618. if (nla_put_u8(skb, RTA_TTL_PROPAGATE,
  1619. ttl_propagate))
  1620. goto nla_put_failure;
  1621. }
  1622. if (rt->rt_nhn == 1) {
  1623. const struct mpls_nh *nh = rt->rt_nh;
  1624. if (nh->nh_labels &&
  1625. nla_put_labels(skb, RTA_NEWDST, nh->nh_labels,
  1626. nh->nh_label))
  1627. goto nla_put_failure;
  1628. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
  1629. nla_put_via(skb, nh->nh_via_table, mpls_nh_via(rt, nh),
  1630. nh->nh_via_alen))
  1631. goto nla_put_failure;
  1632. dev = rtnl_dereference(nh->nh_dev);
  1633. if (dev && nla_put_u32(skb, RTA_OIF, dev->ifindex))
  1634. goto nla_put_failure;
  1635. if (nh->nh_flags & RTNH_F_LINKDOWN)
  1636. rtm->rtm_flags |= RTNH_F_LINKDOWN;
  1637. if (nh->nh_flags & RTNH_F_DEAD)
  1638. rtm->rtm_flags |= RTNH_F_DEAD;
  1639. } else {
  1640. struct rtnexthop *rtnh;
  1641. struct nlattr *mp;
  1642. u8 linkdown = 0;
  1643. u8 dead = 0;
  1644. mp = nla_nest_start(skb, RTA_MULTIPATH);
  1645. if (!mp)
  1646. goto nla_put_failure;
  1647. for_nexthops(rt) {
  1648. dev = rtnl_dereference(nh->nh_dev);
  1649. if (!dev)
  1650. continue;
  1651. rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
  1652. if (!rtnh)
  1653. goto nla_put_failure;
  1654. rtnh->rtnh_ifindex = dev->ifindex;
  1655. if (nh->nh_flags & RTNH_F_LINKDOWN) {
  1656. rtnh->rtnh_flags |= RTNH_F_LINKDOWN;
  1657. linkdown++;
  1658. }
  1659. if (nh->nh_flags & RTNH_F_DEAD) {
  1660. rtnh->rtnh_flags |= RTNH_F_DEAD;
  1661. dead++;
  1662. }
  1663. if (nh->nh_labels && nla_put_labels(skb, RTA_NEWDST,
  1664. nh->nh_labels,
  1665. nh->nh_label))
  1666. goto nla_put_failure;
  1667. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
  1668. nla_put_via(skb, nh->nh_via_table,
  1669. mpls_nh_via(rt, nh),
  1670. nh->nh_via_alen))
  1671. goto nla_put_failure;
  1672. /* length of rtnetlink header + attributes */
  1673. rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
  1674. } endfor_nexthops(rt);
  1675. if (linkdown == rt->rt_nhn)
  1676. rtm->rtm_flags |= RTNH_F_LINKDOWN;
  1677. if (dead == rt->rt_nhn)
  1678. rtm->rtm_flags |= RTNH_F_DEAD;
  1679. nla_nest_end(skb, mp);
  1680. }
  1681. nlmsg_end(skb, nlh);
  1682. return 0;
  1683. nla_put_failure:
  1684. nlmsg_cancel(skb, nlh);
  1685. return -EMSGSIZE;
  1686. }
  1687. static int mpls_dump_routes(struct sk_buff *skb, struct netlink_callback *cb)
  1688. {
  1689. struct net *net = sock_net(skb->sk);
  1690. struct mpls_route __rcu **platform_label;
  1691. size_t platform_labels;
  1692. unsigned int index;
  1693. ASSERT_RTNL();
  1694. index = cb->args[0];
  1695. if (index < MPLS_LABEL_FIRST_UNRESERVED)
  1696. index = MPLS_LABEL_FIRST_UNRESERVED;
  1697. platform_label = rtnl_dereference(net->mpls.platform_label);
  1698. platform_labels = net->mpls.platform_labels;
  1699. for (; index < platform_labels; index++) {
  1700. struct mpls_route *rt;
  1701. rt = rtnl_dereference(platform_label[index]);
  1702. if (!rt)
  1703. continue;
  1704. if (mpls_dump_route(skb, NETLINK_CB(cb->skb).portid,
  1705. cb->nlh->nlmsg_seq, RTM_NEWROUTE,
  1706. index, rt, NLM_F_MULTI) < 0)
  1707. break;
  1708. }
  1709. cb->args[0] = index;
  1710. return skb->len;
  1711. }
  1712. static inline size_t lfib_nlmsg_size(struct mpls_route *rt)
  1713. {
  1714. size_t payload =
  1715. NLMSG_ALIGN(sizeof(struct rtmsg))
  1716. + nla_total_size(4) /* RTA_DST */
  1717. + nla_total_size(1); /* RTA_TTL_PROPAGATE */
  1718. if (rt->rt_nhn == 1) {
  1719. struct mpls_nh *nh = rt->rt_nh;
  1720. if (nh->nh_dev)
  1721. payload += nla_total_size(4); /* RTA_OIF */
  1722. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC) /* RTA_VIA */
  1723. payload += nla_total_size(2 + nh->nh_via_alen);
  1724. if (nh->nh_labels) /* RTA_NEWDST */
  1725. payload += nla_total_size(nh->nh_labels * 4);
  1726. } else {
  1727. /* each nexthop is packed in an attribute */
  1728. size_t nhsize = 0;
  1729. for_nexthops(rt) {
  1730. if (!rtnl_dereference(nh->nh_dev))
  1731. continue;
  1732. nhsize += nla_total_size(sizeof(struct rtnexthop));
  1733. /* RTA_VIA */
  1734. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC)
  1735. nhsize += nla_total_size(2 + nh->nh_via_alen);
  1736. if (nh->nh_labels)
  1737. nhsize += nla_total_size(nh->nh_labels * 4);
  1738. } endfor_nexthops(rt);
  1739. /* nested attribute */
  1740. payload += nla_total_size(nhsize);
  1741. }
  1742. return payload;
  1743. }
  1744. static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
  1745. struct nlmsghdr *nlh, struct net *net, u32 portid,
  1746. unsigned int nlm_flags)
  1747. {
  1748. struct sk_buff *skb;
  1749. u32 seq = nlh ? nlh->nlmsg_seq : 0;
  1750. int err = -ENOBUFS;
  1751. skb = nlmsg_new(lfib_nlmsg_size(rt), GFP_KERNEL);
  1752. if (skb == NULL)
  1753. goto errout;
  1754. err = mpls_dump_route(skb, portid, seq, event, label, rt, nlm_flags);
  1755. if (err < 0) {
  1756. /* -EMSGSIZE implies BUG in lfib_nlmsg_size */
  1757. WARN_ON(err == -EMSGSIZE);
  1758. kfree_skb(skb);
  1759. goto errout;
  1760. }
  1761. rtnl_notify(skb, net, portid, RTNLGRP_MPLS_ROUTE, nlh, GFP_KERNEL);
  1762. return;
  1763. errout:
  1764. if (err < 0)
  1765. rtnl_set_sk_err(net, RTNLGRP_MPLS_ROUTE, err);
  1766. }
  1767. static int mpls_getroute(struct sk_buff *in_skb, struct nlmsghdr *in_nlh,
  1768. struct netlink_ext_ack *extack)
  1769. {
  1770. struct net *net = sock_net(in_skb->sk);
  1771. u32 portid = NETLINK_CB(in_skb).portid;
  1772. u32 in_label = LABEL_NOT_SPECIFIED;
  1773. struct nlattr *tb[RTA_MAX + 1];
  1774. u32 labels[MAX_NEW_LABELS];
  1775. struct mpls_shim_hdr *hdr;
  1776. unsigned int hdr_size = 0;
  1777. struct net_device *dev;
  1778. struct mpls_route *rt;
  1779. struct rtmsg *rtm, *r;
  1780. struct nlmsghdr *nlh;
  1781. struct sk_buff *skb;
  1782. struct mpls_nh *nh;
  1783. u8 n_labels;
  1784. int err;
  1785. err = nlmsg_parse(in_nlh, sizeof(*rtm), tb, RTA_MAX,
  1786. rtm_mpls_policy, extack);
  1787. if (err < 0)
  1788. goto errout;
  1789. rtm = nlmsg_data(in_nlh);
  1790. if (tb[RTA_DST]) {
  1791. u8 label_count;
  1792. if (nla_get_labels(tb[RTA_DST], 1, &label_count,
  1793. &in_label, extack)) {
  1794. err = -EINVAL;
  1795. goto errout;
  1796. }
  1797. if (!mpls_label_ok(net, &in_label, extack)) {
  1798. err = -EINVAL;
  1799. goto errout;
  1800. }
  1801. }
  1802. rt = mpls_route_input_rcu(net, in_label);
  1803. if (!rt) {
  1804. err = -ENETUNREACH;
  1805. goto errout;
  1806. }
  1807. if (rtm->rtm_flags & RTM_F_FIB_MATCH) {
  1808. skb = nlmsg_new(lfib_nlmsg_size(rt), GFP_KERNEL);
  1809. if (!skb) {
  1810. err = -ENOBUFS;
  1811. goto errout;
  1812. }
  1813. err = mpls_dump_route(skb, portid, in_nlh->nlmsg_seq,
  1814. RTM_NEWROUTE, in_label, rt, 0);
  1815. if (err < 0) {
  1816. /* -EMSGSIZE implies BUG in lfib_nlmsg_size */
  1817. WARN_ON(err == -EMSGSIZE);
  1818. goto errout_free;
  1819. }
  1820. return rtnl_unicast(skb, net, portid);
  1821. }
  1822. if (tb[RTA_NEWDST]) {
  1823. if (nla_get_labels(tb[RTA_NEWDST], MAX_NEW_LABELS, &n_labels,
  1824. labels, extack) != 0) {
  1825. err = -EINVAL;
  1826. goto errout;
  1827. }
  1828. hdr_size = n_labels * sizeof(struct mpls_shim_hdr);
  1829. }
  1830. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  1831. if (!skb) {
  1832. err = -ENOBUFS;
  1833. goto errout;
  1834. }
  1835. skb->protocol = htons(ETH_P_MPLS_UC);
  1836. if (hdr_size) {
  1837. bool bos;
  1838. int i;
  1839. if (skb_cow(skb, hdr_size)) {
  1840. err = -ENOBUFS;
  1841. goto errout_free;
  1842. }
  1843. skb_reserve(skb, hdr_size);
  1844. skb_push(skb, hdr_size);
  1845. skb_reset_network_header(skb);
  1846. /* Push new labels */
  1847. hdr = mpls_hdr(skb);
  1848. bos = true;
  1849. for (i = n_labels - 1; i >= 0; i--) {
  1850. hdr[i] = mpls_entry_encode(labels[i],
  1851. 1, 0, bos);
  1852. bos = false;
  1853. }
  1854. }
  1855. nh = mpls_select_multipath(rt, skb);
  1856. if (!nh) {
  1857. err = -ENETUNREACH;
  1858. goto errout_free;
  1859. }
  1860. if (hdr_size) {
  1861. skb_pull(skb, hdr_size);
  1862. skb_reset_network_header(skb);
  1863. }
  1864. nlh = nlmsg_put(skb, portid, in_nlh->nlmsg_seq,
  1865. RTM_NEWROUTE, sizeof(*r), 0);
  1866. if (!nlh) {
  1867. err = -EMSGSIZE;
  1868. goto errout_free;
  1869. }
  1870. r = nlmsg_data(nlh);
  1871. r->rtm_family = AF_MPLS;
  1872. r->rtm_dst_len = 20;
  1873. r->rtm_src_len = 0;
  1874. r->rtm_table = RT_TABLE_MAIN;
  1875. r->rtm_type = RTN_UNICAST;
  1876. r->rtm_scope = RT_SCOPE_UNIVERSE;
  1877. r->rtm_protocol = rt->rt_protocol;
  1878. r->rtm_flags = 0;
  1879. if (nla_put_labels(skb, RTA_DST, 1, &in_label))
  1880. goto nla_put_failure;
  1881. if (nh->nh_labels &&
  1882. nla_put_labels(skb, RTA_NEWDST, nh->nh_labels,
  1883. nh->nh_label))
  1884. goto nla_put_failure;
  1885. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
  1886. nla_put_via(skb, nh->nh_via_table, mpls_nh_via(rt, nh),
  1887. nh->nh_via_alen))
  1888. goto nla_put_failure;
  1889. dev = rtnl_dereference(nh->nh_dev);
  1890. if (dev && nla_put_u32(skb, RTA_OIF, dev->ifindex))
  1891. goto nla_put_failure;
  1892. nlmsg_end(skb, nlh);
  1893. err = rtnl_unicast(skb, net, portid);
  1894. errout:
  1895. return err;
  1896. nla_put_failure:
  1897. nlmsg_cancel(skb, nlh);
  1898. err = -EMSGSIZE;
  1899. errout_free:
  1900. kfree_skb(skb);
  1901. return err;
  1902. }
  1903. static int resize_platform_label_table(struct net *net, size_t limit)
  1904. {
  1905. size_t size = sizeof(struct mpls_route *) * limit;
  1906. size_t old_limit;
  1907. size_t cp_size;
  1908. struct mpls_route __rcu **labels = NULL, **old;
  1909. struct mpls_route *rt0 = NULL, *rt2 = NULL;
  1910. unsigned index;
  1911. if (size) {
  1912. labels = kvzalloc(size, GFP_KERNEL);
  1913. if (!labels)
  1914. goto nolabels;
  1915. }
  1916. /* In case the predefined labels need to be populated */
  1917. if (limit > MPLS_LABEL_IPV4NULL) {
  1918. struct net_device *lo = net->loopback_dev;
  1919. rt0 = mpls_rt_alloc(1, lo->addr_len, 0);
  1920. if (IS_ERR(rt0))
  1921. goto nort0;
  1922. RCU_INIT_POINTER(rt0->rt_nh->nh_dev, lo);
  1923. rt0->rt_protocol = RTPROT_KERNEL;
  1924. rt0->rt_payload_type = MPT_IPV4;
  1925. rt0->rt_ttl_propagate = MPLS_TTL_PROP_DEFAULT;
  1926. rt0->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
  1927. rt0->rt_nh->nh_via_alen = lo->addr_len;
  1928. memcpy(__mpls_nh_via(rt0, rt0->rt_nh), lo->dev_addr,
  1929. lo->addr_len);
  1930. }
  1931. if (limit > MPLS_LABEL_IPV6NULL) {
  1932. struct net_device *lo = net->loopback_dev;
  1933. rt2 = mpls_rt_alloc(1, lo->addr_len, 0);
  1934. if (IS_ERR(rt2))
  1935. goto nort2;
  1936. RCU_INIT_POINTER(rt2->rt_nh->nh_dev, lo);
  1937. rt2->rt_protocol = RTPROT_KERNEL;
  1938. rt2->rt_payload_type = MPT_IPV6;
  1939. rt2->rt_ttl_propagate = MPLS_TTL_PROP_DEFAULT;
  1940. rt2->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
  1941. rt2->rt_nh->nh_via_alen = lo->addr_len;
  1942. memcpy(__mpls_nh_via(rt2, rt2->rt_nh), lo->dev_addr,
  1943. lo->addr_len);
  1944. }
  1945. rtnl_lock();
  1946. /* Remember the original table */
  1947. old = rtnl_dereference(net->mpls.platform_label);
  1948. old_limit = net->mpls.platform_labels;
  1949. /* Free any labels beyond the new table */
  1950. for (index = limit; index < old_limit; index++)
  1951. mpls_route_update(net, index, NULL, NULL);
  1952. /* Copy over the old labels */
  1953. cp_size = size;
  1954. if (old_limit < limit)
  1955. cp_size = old_limit * sizeof(struct mpls_route *);
  1956. memcpy(labels, old, cp_size);
  1957. /* If needed set the predefined labels */
  1958. if ((old_limit <= MPLS_LABEL_IPV6NULL) &&
  1959. (limit > MPLS_LABEL_IPV6NULL)) {
  1960. RCU_INIT_POINTER(labels[MPLS_LABEL_IPV6NULL], rt2);
  1961. rt2 = NULL;
  1962. }
  1963. if ((old_limit <= MPLS_LABEL_IPV4NULL) &&
  1964. (limit > MPLS_LABEL_IPV4NULL)) {
  1965. RCU_INIT_POINTER(labels[MPLS_LABEL_IPV4NULL], rt0);
  1966. rt0 = NULL;
  1967. }
  1968. /* Update the global pointers */
  1969. net->mpls.platform_labels = limit;
  1970. rcu_assign_pointer(net->mpls.platform_label, labels);
  1971. rtnl_unlock();
  1972. mpls_rt_free(rt2);
  1973. mpls_rt_free(rt0);
  1974. if (old) {
  1975. synchronize_rcu();
  1976. kvfree(old);
  1977. }
  1978. return 0;
  1979. nort2:
  1980. mpls_rt_free(rt0);
  1981. nort0:
  1982. kvfree(labels);
  1983. nolabels:
  1984. return -ENOMEM;
  1985. }
  1986. static int mpls_platform_labels(struct ctl_table *table, int write,
  1987. void __user *buffer, size_t *lenp, loff_t *ppos)
  1988. {
  1989. struct net *net = table->data;
  1990. int platform_labels = net->mpls.platform_labels;
  1991. int ret;
  1992. struct ctl_table tmp = {
  1993. .procname = table->procname,
  1994. .data = &platform_labels,
  1995. .maxlen = sizeof(int),
  1996. .mode = table->mode,
  1997. .extra1 = &zero,
  1998. .extra2 = &label_limit,
  1999. };
  2000. ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
  2001. if (write && ret == 0)
  2002. ret = resize_platform_label_table(net, platform_labels);
  2003. return ret;
  2004. }
  2005. #define MPLS_NS_SYSCTL_OFFSET(field) \
  2006. (&((struct net *)0)->field)
  2007. static const struct ctl_table mpls_table[] = {
  2008. {
  2009. .procname = "platform_labels",
  2010. .data = NULL,
  2011. .maxlen = sizeof(int),
  2012. .mode = 0644,
  2013. .proc_handler = mpls_platform_labels,
  2014. },
  2015. {
  2016. .procname = "ip_ttl_propagate",
  2017. .data = MPLS_NS_SYSCTL_OFFSET(mpls.ip_ttl_propagate),
  2018. .maxlen = sizeof(int),
  2019. .mode = 0644,
  2020. .proc_handler = proc_dointvec_minmax,
  2021. .extra1 = &zero,
  2022. .extra2 = &one,
  2023. },
  2024. {
  2025. .procname = "default_ttl",
  2026. .data = MPLS_NS_SYSCTL_OFFSET(mpls.default_ttl),
  2027. .maxlen = sizeof(int),
  2028. .mode = 0644,
  2029. .proc_handler = proc_dointvec_minmax,
  2030. .extra1 = &one,
  2031. .extra2 = &ttl_max,
  2032. },
  2033. { }
  2034. };
  2035. static int mpls_net_init(struct net *net)
  2036. {
  2037. struct ctl_table *table;
  2038. int i;
  2039. net->mpls.platform_labels = 0;
  2040. net->mpls.platform_label = NULL;
  2041. net->mpls.ip_ttl_propagate = 1;
  2042. net->mpls.default_ttl = 255;
  2043. table = kmemdup(mpls_table, sizeof(mpls_table), GFP_KERNEL);
  2044. if (table == NULL)
  2045. return -ENOMEM;
  2046. /* Table data contains only offsets relative to the base of
  2047. * the mdev at this point, so make them absolute.
  2048. */
  2049. for (i = 0; i < ARRAY_SIZE(mpls_table) - 1; i++)
  2050. table[i].data = (char *)net + (uintptr_t)table[i].data;
  2051. net->mpls.ctl = register_net_sysctl(net, "net/mpls", table);
  2052. if (net->mpls.ctl == NULL) {
  2053. kfree(table);
  2054. return -ENOMEM;
  2055. }
  2056. return 0;
  2057. }
  2058. static void mpls_net_exit(struct net *net)
  2059. {
  2060. struct mpls_route __rcu **platform_label;
  2061. size_t platform_labels;
  2062. struct ctl_table *table;
  2063. unsigned int index;
  2064. table = net->mpls.ctl->ctl_table_arg;
  2065. unregister_net_sysctl_table(net->mpls.ctl);
  2066. kfree(table);
  2067. /* An rcu grace period has passed since there was a device in
  2068. * the network namespace (and thus the last in flight packet)
  2069. * left this network namespace. This is because
  2070. * unregister_netdevice_many and netdev_run_todo has completed
  2071. * for each network device that was in this network namespace.
  2072. *
  2073. * As such no additional rcu synchronization is necessary when
  2074. * freeing the platform_label table.
  2075. */
  2076. rtnl_lock();
  2077. platform_label = rtnl_dereference(net->mpls.platform_label);
  2078. platform_labels = net->mpls.platform_labels;
  2079. for (index = 0; index < platform_labels; index++) {
  2080. struct mpls_route *rt = rtnl_dereference(platform_label[index]);
  2081. RCU_INIT_POINTER(platform_label[index], NULL);
  2082. mpls_notify_route(net, index, rt, NULL, NULL);
  2083. mpls_rt_free(rt);
  2084. }
  2085. rtnl_unlock();
  2086. kvfree(platform_label);
  2087. }
  2088. static struct pernet_operations mpls_net_ops = {
  2089. .init = mpls_net_init,
  2090. .exit = mpls_net_exit,
  2091. };
  2092. static struct rtnl_af_ops mpls_af_ops __read_mostly = {
  2093. .family = AF_MPLS,
  2094. .fill_stats_af = mpls_fill_stats_af,
  2095. .get_stats_af_size = mpls_get_stats_af_size,
  2096. };
  2097. static int __init mpls_init(void)
  2098. {
  2099. int err;
  2100. BUILD_BUG_ON(sizeof(struct mpls_shim_hdr) != 4);
  2101. err = register_pernet_subsys(&mpls_net_ops);
  2102. if (err)
  2103. goto out;
  2104. err = register_netdevice_notifier(&mpls_dev_notifier);
  2105. if (err)
  2106. goto out_unregister_pernet;
  2107. dev_add_pack(&mpls_packet_type);
  2108. rtnl_af_register(&mpls_af_ops);
  2109. rtnl_register_module(THIS_MODULE, PF_MPLS, RTM_NEWROUTE,
  2110. mpls_rtm_newroute, NULL, 0);
  2111. rtnl_register_module(THIS_MODULE, PF_MPLS, RTM_DELROUTE,
  2112. mpls_rtm_delroute, NULL, 0);
  2113. rtnl_register_module(THIS_MODULE, PF_MPLS, RTM_GETROUTE,
  2114. mpls_getroute, mpls_dump_routes, 0);
  2115. rtnl_register_module(THIS_MODULE, PF_MPLS, RTM_GETNETCONF,
  2116. mpls_netconf_get_devconf,
  2117. mpls_netconf_dump_devconf, 0);
  2118. err = ipgre_tunnel_encap_add_mpls_ops();
  2119. if (err)
  2120. pr_err("Can't add mpls over gre tunnel ops\n");
  2121. err = 0;
  2122. out:
  2123. return err;
  2124. out_unregister_pernet:
  2125. unregister_pernet_subsys(&mpls_net_ops);
  2126. goto out;
  2127. }
  2128. module_init(mpls_init);
  2129. static void __exit mpls_exit(void)
  2130. {
  2131. rtnl_unregister_all(PF_MPLS);
  2132. rtnl_af_unregister(&mpls_af_ops);
  2133. dev_remove_pack(&mpls_packet_type);
  2134. unregister_netdevice_notifier(&mpls_dev_notifier);
  2135. unregister_pernet_subsys(&mpls_net_ops);
  2136. ipgre_tunnel_encap_del_mpls_ops();
  2137. }
  2138. module_exit(mpls_exit);
  2139. MODULE_DESCRIPTION("MultiProtocol Label Switching");
  2140. MODULE_LICENSE("GPL v2");
  2141. MODULE_ALIAS_NETPROTO(PF_MPLS);